1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2022 NVIDIA Corporation and Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/types.h>
8 #include <linux/string.h>
9 #include <linux/workqueue.h>
10 #include <linux/gfp.h>
11 #include <linux/slab.h>
12 #include <linux/list.h>
13 #include <linux/vmalloc.h>
16 #include "../mlxfw/mlxfw.h"
18 struct mlxsw_linecard_ini_file {
32 struct mlxsw_linecard_types_info {
33 struct mlxsw_linecard_ini_file **ini_files;
39 #define MLXSW_LINECARD_STATUS_EVENT_TO (10 * MSEC_PER_SEC)
42 mlxsw_linecard_status_event_to_schedule(struct mlxsw_linecard *linecard,
43 enum mlxsw_linecard_status_event_type status_event_type)
45 cancel_delayed_work_sync(&linecard->status_event_to_dw);
46 linecard->status_event_type_to = status_event_type;
47 mlxsw_core_schedule_dw(&linecard->status_event_to_dw,
48 msecs_to_jiffies(MLXSW_LINECARD_STATUS_EVENT_TO));
52 mlxsw_linecard_status_event_done(struct mlxsw_linecard *linecard,
53 enum mlxsw_linecard_status_event_type status_event_type)
55 if (linecard->status_event_type_to == status_event_type)
56 cancel_delayed_work_sync(&linecard->status_event_to_dw);
60 mlxsw_linecard_types_lookup(struct mlxsw_linecards *linecards, u8 card_type)
62 struct mlxsw_linecard_types_info *types_info;
63 struct mlxsw_linecard_ini_file *ini_file;
66 types_info = linecards->types_info;
69 for (i = 0; i < types_info->count; i++) {
70 ini_file = linecards->types_info->ini_files[i];
71 if (ini_file->format.type == card_type)
72 return ini_file->format.name;
77 static const char *mlxsw_linecard_type_name(struct mlxsw_linecard *linecard)
79 struct mlxsw_core *mlxsw_core = linecard->linecards->mlxsw_core;
80 char mddq_pl[MLXSW_REG_MDDQ_LEN];
83 mlxsw_reg_mddq_slot_name_pack(mddq_pl, linecard->slot_index);
84 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mddq), mddq_pl);
87 mlxsw_reg_mddq_slot_name_unpack(mddq_pl, linecard->name);
88 return linecard->name;
91 struct mlxsw_linecard_device_fw_info {
92 struct mlxfw_dev mlxfw_dev;
93 struct mlxsw_core *mlxsw_core;
94 struct mlxsw_linecard *linecard;
97 static int mlxsw_linecard_device_fw_component_query(struct mlxfw_dev *mlxfw_dev,
101 u16 *p_max_write_size)
103 struct mlxsw_linecard_device_fw_info *info =
104 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
106 struct mlxsw_linecard *linecard = info->linecard;
107 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
108 char mddt_pl[MLXSW_REG_MDDT_LEN];
112 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
113 linecard->device.index,
114 MLXSW_REG_MDDT_METHOD_QUERY,
115 MLXSW_REG(mcqi), &mcqi_pl);
117 mlxsw_reg_mcqi_pack(mcqi_pl, component_index);
118 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
121 mlxsw_reg_mcqi_unpack(mcqi_pl, p_max_size, p_align_bits,
124 *p_align_bits = max_t(u8, *p_align_bits, 2);
125 *p_max_write_size = min_t(u16, *p_max_write_size,
126 MLXSW_REG_MCDA_MAX_DATA_LEN);
130 static int mlxsw_linecard_device_fw_fsm_lock(struct mlxfw_dev *mlxfw_dev,
133 struct mlxsw_linecard_device_fw_info *info =
134 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
136 struct mlxsw_linecard *linecard = info->linecard;
137 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
138 char mddt_pl[MLXSW_REG_MDDT_LEN];
143 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
144 linecard->device.index,
145 MLXSW_REG_MDDT_METHOD_QUERY,
146 MLXSW_REG(mcc), &mcc_pl);
147 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, 0, 0);
148 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
152 mlxsw_reg_mcc_unpack(mcc_pl, fwhandle, NULL, &control_state);
153 if (control_state != MLXFW_FSM_STATE_IDLE)
156 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
157 linecard->device.index,
158 MLXSW_REG_MDDT_METHOD_WRITE,
159 MLXSW_REG(mcc), &mcc_pl);
160 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_LOCK_UPDATE_HANDLE,
162 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
166 mlxsw_linecard_device_fw_fsm_component_update(struct mlxfw_dev *mlxfw_dev,
171 struct mlxsw_linecard_device_fw_info *info =
172 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
174 struct mlxsw_linecard *linecard = info->linecard;
175 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
176 char mddt_pl[MLXSW_REG_MDDT_LEN];
179 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
180 linecard->device.index,
181 MLXSW_REG_MDDT_METHOD_WRITE,
182 MLXSW_REG(mcc), &mcc_pl);
183 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_UPDATE_COMPONENT,
184 component_index, fwhandle, component_size);
185 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
189 mlxsw_linecard_device_fw_fsm_block_download(struct mlxfw_dev *mlxfw_dev,
190 u32 fwhandle, u8 *data,
191 u16 size, u32 offset)
193 struct mlxsw_linecard_device_fw_info *info =
194 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
196 struct mlxsw_linecard *linecard = info->linecard;
197 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
198 char mddt_pl[MLXSW_REG_MDDT_LEN];
201 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
202 linecard->device.index,
203 MLXSW_REG_MDDT_METHOD_WRITE,
204 MLXSW_REG(mcda), &mcda_pl);
205 mlxsw_reg_mcda_pack(mcda_pl, fwhandle, offset, size, data);
206 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
210 mlxsw_linecard_device_fw_fsm_component_verify(struct mlxfw_dev *mlxfw_dev,
211 u32 fwhandle, u16 component_index)
213 struct mlxsw_linecard_device_fw_info *info =
214 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
216 struct mlxsw_linecard *linecard = info->linecard;
217 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
218 char mddt_pl[MLXSW_REG_MDDT_LEN];
221 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
222 linecard->device.index,
223 MLXSW_REG_MDDT_METHOD_WRITE,
224 MLXSW_REG(mcc), &mcc_pl);
225 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_VERIFY_COMPONENT,
226 component_index, fwhandle, 0);
227 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
230 static int mlxsw_linecard_device_fw_fsm_activate(struct mlxfw_dev *mlxfw_dev,
233 struct mlxsw_linecard_device_fw_info *info =
234 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
236 struct mlxsw_linecard *linecard = info->linecard;
237 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
238 char mddt_pl[MLXSW_REG_MDDT_LEN];
241 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
242 linecard->device.index,
243 MLXSW_REG_MDDT_METHOD_WRITE,
244 MLXSW_REG(mcc), &mcc_pl);
245 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_ACTIVATE,
247 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
251 mlxsw_linecard_device_fw_fsm_query_state(struct mlxfw_dev *mlxfw_dev,
253 enum mlxfw_fsm_state *fsm_state,
254 enum mlxfw_fsm_state_err *fsm_state_err)
256 struct mlxsw_linecard_device_fw_info *info =
257 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
259 struct mlxsw_linecard *linecard = info->linecard;
260 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
261 char mddt_pl[MLXSW_REG_MDDT_LEN];
267 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
268 linecard->device.index,
269 MLXSW_REG_MDDT_METHOD_QUERY,
270 MLXSW_REG(mcc), &mcc_pl);
271 mlxsw_reg_mcc_pack(mcc_pl, 0, 0, fwhandle, 0);
272 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
276 mlxsw_reg_mcc_unpack(mcc_pl, NULL, &error_code, &control_state);
277 *fsm_state = control_state;
278 *fsm_state_err = min_t(enum mlxfw_fsm_state_err, error_code,
279 MLXFW_FSM_STATE_ERR_MAX);
283 static void mlxsw_linecard_device_fw_fsm_cancel(struct mlxfw_dev *mlxfw_dev,
286 struct mlxsw_linecard_device_fw_info *info =
287 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
289 struct mlxsw_linecard *linecard = info->linecard;
290 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
291 char mddt_pl[MLXSW_REG_MDDT_LEN];
294 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
295 linecard->device.index,
296 MLXSW_REG_MDDT_METHOD_WRITE,
297 MLXSW_REG(mcc), &mcc_pl);
298 mlxsw_reg_mcc_pack(mcc_pl, MLXSW_REG_MCC_INSTRUCTION_CANCEL,
300 mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
303 static void mlxsw_linecard_device_fw_fsm_release(struct mlxfw_dev *mlxfw_dev,
306 struct mlxsw_linecard_device_fw_info *info =
307 container_of(mlxfw_dev, struct mlxsw_linecard_device_fw_info,
309 struct mlxsw_linecard *linecard = info->linecard;
310 struct mlxsw_core *mlxsw_core = info->mlxsw_core;
311 char mddt_pl[MLXSW_REG_MDDT_LEN];
314 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index,
315 linecard->device.index,
316 MLXSW_REG_MDDT_METHOD_WRITE,
317 MLXSW_REG(mcc), &mcc_pl);
318 mlxsw_reg_mcc_pack(mcc_pl,
319 MLXSW_REG_MCC_INSTRUCTION_RELEASE_UPDATE_HANDLE,
321 mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
324 static const struct mlxfw_dev_ops mlxsw_linecard_device_dev_ops = {
325 .component_query = mlxsw_linecard_device_fw_component_query,
326 .fsm_lock = mlxsw_linecard_device_fw_fsm_lock,
327 .fsm_component_update = mlxsw_linecard_device_fw_fsm_component_update,
328 .fsm_block_download = mlxsw_linecard_device_fw_fsm_block_download,
329 .fsm_component_verify = mlxsw_linecard_device_fw_fsm_component_verify,
330 .fsm_activate = mlxsw_linecard_device_fw_fsm_activate,
331 .fsm_query_state = mlxsw_linecard_device_fw_fsm_query_state,
332 .fsm_cancel = mlxsw_linecard_device_fw_fsm_cancel,
333 .fsm_release = mlxsw_linecard_device_fw_fsm_release,
336 int mlxsw_linecard_flash_update(struct devlink *linecard_devlink,
337 struct mlxsw_linecard *linecard,
338 const struct firmware *firmware,
339 struct netlink_ext_ack *extack)
341 struct mlxsw_core *mlxsw_core = linecard->linecards->mlxsw_core;
342 struct mlxsw_linecard_device_fw_info info = {
344 .ops = &mlxsw_linecard_device_dev_ops,
345 .psid = linecard->device.info.psid,
346 .psid_size = strlen(linecard->device.info.psid),
347 .devlink = linecard_devlink,
349 .mlxsw_core = mlxsw_core,
350 .linecard = linecard,
354 mutex_lock(&linecard->lock);
355 if (!linecard->active) {
356 NL_SET_ERR_MSG_MOD(extack, "Only active line cards can be flashed");
360 err = mlxsw_core_fw_flash(mlxsw_core, &info.mlxfw_dev,
363 mutex_unlock(&linecard->lock);
367 static int mlxsw_linecard_device_psid_get(struct mlxsw_linecard *linecard,
368 u8 device_index, char *psid)
370 struct mlxsw_core *mlxsw_core = linecard->linecards->mlxsw_core;
371 char mddt_pl[MLXSW_REG_MDDT_LEN];
375 mlxsw_reg_mddt_pack(mddt_pl, linecard->slot_index, device_index,
376 MLXSW_REG_MDDT_METHOD_QUERY,
377 MLXSW_REG(mgir), &mgir_pl);
379 mlxsw_reg_mgir_pack(mgir_pl);
380 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mddt), mddt_pl);
384 mlxsw_reg_mgir_fw_info_psid_memcpy_from(mgir_pl, psid);
388 static int mlxsw_linecard_device_info_update(struct mlxsw_linecard *linecard)
390 struct mlxsw_core *mlxsw_core = linecard->linecards->mlxsw_core;
391 bool flashable_found = false;
395 struct mlxsw_linecard_device_info info;
396 char mddq_pl[MLXSW_REG_MDDQ_LEN];
402 mlxsw_reg_mddq_device_info_pack(mddq_pl, linecard->slot_index,
404 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mddq), mddq_pl);
407 mlxsw_reg_mddq_device_info_unpack(mddq_pl, &msg_seq,
408 &data_valid, &flash_owner,
415 if (!flash_owner) /* We care only about flashable ones. */
417 if (flashable_found) {
418 dev_warn_once(linecard->linecards->bus_info->dev, "linecard %u: More flashable devices present, exposing only the first one\n",
419 linecard->slot_index);
423 err = mlxsw_linecard_device_psid_get(linecard, device_index,
428 linecard->device.info = info;
429 linecard->device.index = device_index;
430 flashable_found = true;
436 static void mlxsw_linecard_provision_fail(struct mlxsw_linecard *linecard)
438 linecard->provisioned = false;
439 linecard->ready = false;
440 linecard->active = false;
441 devlink_linecard_provision_fail(linecard->devlink_linecard);
444 struct mlxsw_linecards_event_ops_item {
445 struct list_head list;
446 const struct mlxsw_linecards_event_ops *event_ops;
451 mlxsw_linecard_event_op_call(struct mlxsw_linecard *linecard,
452 mlxsw_linecards_event_op_t *op, void *priv)
454 struct mlxsw_core *mlxsw_core = linecard->linecards->mlxsw_core;
458 op(mlxsw_core, linecard->slot_index, priv);
462 mlxsw_linecard_active_ops_call(struct mlxsw_linecard *linecard)
464 struct mlxsw_linecards *linecards = linecard->linecards;
465 struct mlxsw_linecards_event_ops_item *item;
467 mutex_lock(&linecards->event_ops_list_lock);
468 list_for_each_entry(item, &linecards->event_ops_list, list)
469 mlxsw_linecard_event_op_call(linecard,
470 item->event_ops->got_active,
472 mutex_unlock(&linecards->event_ops_list_lock);
476 mlxsw_linecard_inactive_ops_call(struct mlxsw_linecard *linecard)
478 struct mlxsw_linecards *linecards = linecard->linecards;
479 struct mlxsw_linecards_event_ops_item *item;
481 mutex_lock(&linecards->event_ops_list_lock);
482 list_for_each_entry(item, &linecards->event_ops_list, list)
483 mlxsw_linecard_event_op_call(linecard,
484 item->event_ops->got_inactive,
486 mutex_unlock(&linecards->event_ops_list_lock);
490 mlxsw_linecards_event_ops_register_call(struct mlxsw_linecards *linecards,
491 const struct mlxsw_linecards_event_ops_item *item)
493 struct mlxsw_linecard *linecard;
496 for (i = 0; i < linecards->count; i++) {
497 linecard = mlxsw_linecard_get(linecards, i + 1);
498 mutex_lock(&linecard->lock);
499 if (linecard->active)
500 mlxsw_linecard_event_op_call(linecard,
501 item->event_ops->got_active,
503 mutex_unlock(&linecard->lock);
508 mlxsw_linecards_event_ops_unregister_call(struct mlxsw_linecards *linecards,
509 const struct mlxsw_linecards_event_ops_item *item)
511 struct mlxsw_linecard *linecard;
514 for (i = 0; i < linecards->count; i++) {
515 linecard = mlxsw_linecard_get(linecards, i + 1);
516 mutex_lock(&linecard->lock);
517 if (linecard->active)
518 mlxsw_linecard_event_op_call(linecard,
519 item->event_ops->got_inactive,
521 mutex_unlock(&linecard->lock);
525 int mlxsw_linecards_event_ops_register(struct mlxsw_core *mlxsw_core,
526 struct mlxsw_linecards_event_ops *ops,
529 struct mlxsw_linecards *linecards = mlxsw_core_linecards(mlxsw_core);
530 struct mlxsw_linecards_event_ops_item *item;
534 item = kzalloc(sizeof(*item), GFP_KERNEL);
537 item->event_ops = ops;
540 mutex_lock(&linecards->event_ops_list_lock);
541 list_add_tail(&item->list, &linecards->event_ops_list);
542 mutex_unlock(&linecards->event_ops_list_lock);
543 mlxsw_linecards_event_ops_register_call(linecards, item);
546 EXPORT_SYMBOL(mlxsw_linecards_event_ops_register);
548 void mlxsw_linecards_event_ops_unregister(struct mlxsw_core *mlxsw_core,
549 struct mlxsw_linecards_event_ops *ops,
552 struct mlxsw_linecards *linecards = mlxsw_core_linecards(mlxsw_core);
553 struct mlxsw_linecards_event_ops_item *item, *tmp;
558 mutex_lock(&linecards->event_ops_list_lock);
559 list_for_each_entry_safe(item, tmp, &linecards->event_ops_list, list) {
560 if (item->event_ops == ops && item->priv == priv) {
561 list_del(&item->list);
566 mutex_unlock(&linecards->event_ops_list_lock);
570 mlxsw_linecards_event_ops_unregister_call(linecards, item);
573 EXPORT_SYMBOL(mlxsw_linecards_event_ops_unregister);
575 int mlxsw_linecard_devlink_info_get(struct mlxsw_linecard *linecard,
576 struct devlink_info_req *req,
577 struct netlink_ext_ack *extack)
582 mutex_lock(&linecard->lock);
583 if (WARN_ON(!linecard->provisioned)) {
588 sprintf(buf, "%d", linecard->hw_revision);
589 err = devlink_info_version_fixed_put(req, "hw.revision", buf);
593 sprintf(buf, "%d", linecard->ini_version);
594 err = devlink_info_version_running_put(req, "ini.version", buf);
598 if (linecard->active) {
599 struct mlxsw_linecard_device_info *info = &linecard->device.info;
601 err = devlink_info_version_fixed_put(req,
602 DEVLINK_INFO_VERSION_GENERIC_FW_PSID,
605 sprintf(buf, "%u.%u.%u", info->fw_major, info->fw_minor,
607 err = devlink_info_version_running_put(req,
608 DEVLINK_INFO_VERSION_GENERIC_FW,
615 mutex_unlock(&linecard->lock);
620 mlxsw_linecard_provision_set(struct mlxsw_linecard *linecard, u8 card_type,
621 u16 hw_revision, u16 ini_version)
623 struct mlxsw_linecards *linecards = linecard->linecards;
627 type = mlxsw_linecard_types_lookup(linecards, card_type);
628 mlxsw_linecard_status_event_done(linecard,
629 MLXSW_LINECARD_STATUS_EVENT_TYPE_PROVISION);
631 /* It is possible for a line card to be provisioned before
632 * driver initialization. Due to a missing INI bundle file
633 * or an outdated one, the queried card's type might not
634 * be recognized by the driver. In this case, try to query
635 * the card's name from the device.
637 type = mlxsw_linecard_type_name(linecard);
639 mlxsw_linecard_provision_fail(linecard);
640 return PTR_ERR(type);
643 linecard->provisioned = true;
644 linecard->hw_revision = hw_revision;
645 linecard->ini_version = ini_version;
647 err = mlxsw_linecard_bdev_add(linecard);
649 linecard->provisioned = false;
650 mlxsw_linecard_provision_fail(linecard);
654 devlink_linecard_provision_set(linecard->devlink_linecard, type);
658 static void mlxsw_linecard_provision_clear(struct mlxsw_linecard *linecard)
660 mlxsw_linecard_status_event_done(linecard,
661 MLXSW_LINECARD_STATUS_EVENT_TYPE_UNPROVISION);
662 mlxsw_linecard_bdev_del(linecard);
663 linecard->provisioned = false;
664 devlink_linecard_provision_clear(linecard->devlink_linecard);
667 static int mlxsw_linecard_ready_set(struct mlxsw_linecard *linecard)
669 struct mlxsw_core *mlxsw_core = linecard->linecards->mlxsw_core;
670 char mddc_pl[MLXSW_REG_MDDC_LEN];
673 err = mlxsw_linecard_device_info_update(linecard);
677 mlxsw_reg_mddc_pack(mddc_pl, linecard->slot_index, false, true);
678 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddc), mddc_pl);
681 linecard->ready = true;
685 static int mlxsw_linecard_ready_clear(struct mlxsw_linecard *linecard)
687 struct mlxsw_core *mlxsw_core = linecard->linecards->mlxsw_core;
688 char mddc_pl[MLXSW_REG_MDDC_LEN];
691 mlxsw_reg_mddc_pack(mddc_pl, linecard->slot_index, false, false);
692 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddc), mddc_pl);
695 linecard->ready = false;
699 static void mlxsw_linecard_active_set(struct mlxsw_linecard *linecard)
701 mlxsw_linecard_active_ops_call(linecard);
702 linecard->active = true;
703 devlink_linecard_activate(linecard->devlink_linecard);
706 static void mlxsw_linecard_active_clear(struct mlxsw_linecard *linecard)
708 mlxsw_linecard_inactive_ops_call(linecard);
709 linecard->active = false;
710 devlink_linecard_deactivate(linecard->devlink_linecard);
713 static int mlxsw_linecard_status_process(struct mlxsw_linecards *linecards,
714 struct mlxsw_linecard *linecard,
717 enum mlxsw_reg_mddq_slot_info_ready ready;
718 bool provisioned, sr_valid, active;
719 u16 ini_version, hw_revision;
720 u8 slot_index, card_type;
723 mlxsw_reg_mddq_slot_info_unpack(mddq_pl, &slot_index, &provisioned,
724 &sr_valid, &ready, &active,
725 &hw_revision, &ini_version,
729 if (WARN_ON(slot_index != linecard->slot_index))
732 if (WARN_ON(slot_index > linecards->count))
734 linecard = mlxsw_linecard_get(linecards, slot_index);
737 mutex_lock(&linecard->lock);
739 if (provisioned && linecard->provisioned != provisioned) {
740 err = mlxsw_linecard_provision_set(linecard, card_type,
741 hw_revision, ini_version);
746 if (ready == MLXSW_REG_MDDQ_SLOT_INFO_READY_READY && !linecard->ready) {
747 err = mlxsw_linecard_ready_set(linecard);
752 if (active && linecard->active != active)
753 mlxsw_linecard_active_set(linecard);
755 if (!active && linecard->active != active)
756 mlxsw_linecard_active_clear(linecard);
758 if (ready != MLXSW_REG_MDDQ_SLOT_INFO_READY_READY &&
760 err = mlxsw_linecard_ready_clear(linecard);
765 if (!provisioned && linecard->provisioned != provisioned)
766 mlxsw_linecard_provision_clear(linecard);
769 mutex_unlock(&linecard->lock);
773 static int mlxsw_linecard_status_get_and_process(struct mlxsw_core *mlxsw_core,
774 struct mlxsw_linecards *linecards,
775 struct mlxsw_linecard *linecard)
777 char mddq_pl[MLXSW_REG_MDDQ_LEN];
780 mlxsw_reg_mddq_slot_info_pack(mddq_pl, linecard->slot_index, false);
781 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mddq), mddq_pl);
785 return mlxsw_linecard_status_process(linecards, linecard, mddq_pl);
788 static void mlxsw_linecards_irq_event_handler(struct mlxsw_core *mlxsw_core)
790 struct mlxsw_linecards *linecards = mlxsw_core_linecards(mlxsw_core);
793 /* Handle change of line card active state. */
794 for (i = 0; i < linecards->count; i++) {
795 struct mlxsw_linecard *linecard = mlxsw_linecard_get(linecards,
798 mlxsw_linecard_status_get_and_process(mlxsw_core, linecards,
803 static const char * const mlxsw_linecard_status_event_type_name[] = {
804 [MLXSW_LINECARD_STATUS_EVENT_TYPE_PROVISION] = "provision",
805 [MLXSW_LINECARD_STATUS_EVENT_TYPE_UNPROVISION] = "unprovision",
808 static void mlxsw_linecard_status_event_to_work(struct work_struct *work)
810 struct mlxsw_linecard *linecard =
811 container_of(work, struct mlxsw_linecard,
812 status_event_to_dw.work);
814 mutex_lock(&linecard->lock);
815 dev_err(linecard->linecards->bus_info->dev, "linecard %u: Timeout reached waiting on %s status event",
816 linecard->slot_index,
817 mlxsw_linecard_status_event_type_name[linecard->status_event_type_to]);
818 mlxsw_linecard_provision_fail(linecard);
819 mutex_unlock(&linecard->lock);
822 static int __mlxsw_linecard_fix_fsm_state(struct mlxsw_linecard *linecard)
824 dev_info(linecard->linecards->bus_info->dev, "linecard %u: Clearing FSM state error",
825 linecard->slot_index);
826 mlxsw_reg_mbct_pack(linecard->mbct_pl, linecard->slot_index,
827 MLXSW_REG_MBCT_OP_CLEAR_ERRORS, false);
828 return mlxsw_reg_write(linecard->linecards->mlxsw_core,
829 MLXSW_REG(mbct), linecard->mbct_pl);
832 static int mlxsw_linecard_fix_fsm_state(struct mlxsw_linecard *linecard,
833 enum mlxsw_reg_mbct_fsm_state fsm_state)
835 if (fsm_state != MLXSW_REG_MBCT_FSM_STATE_ERROR)
837 return __mlxsw_linecard_fix_fsm_state(linecard);
841 mlxsw_linecard_query_ini_status(struct mlxsw_linecard *linecard,
842 enum mlxsw_reg_mbct_status *status,
843 enum mlxsw_reg_mbct_fsm_state *fsm_state,
844 struct netlink_ext_ack *extack)
848 mlxsw_reg_mbct_pack(linecard->mbct_pl, linecard->slot_index,
849 MLXSW_REG_MBCT_OP_QUERY_STATUS, false);
850 err = mlxsw_reg_query(linecard->linecards->mlxsw_core, MLXSW_REG(mbct),
853 NL_SET_ERR_MSG_MOD(extack, "Failed to query linecard INI status");
856 mlxsw_reg_mbct_unpack(linecard->mbct_pl, NULL, status, fsm_state);
861 mlxsw_linecard_ini_transfer(struct mlxsw_core *mlxsw_core,
862 struct mlxsw_linecard *linecard,
863 const struct mlxsw_linecard_ini_file *ini_file,
864 struct netlink_ext_ack *extack)
866 enum mlxsw_reg_mbct_fsm_state fsm_state;
867 enum mlxsw_reg_mbct_status status;
872 size_left = le16_to_cpu(ini_file->size);
873 data = ini_file->data;
875 size_t data_size = MLXSW_REG_MBCT_DATA_LEN;
876 bool is_last = false;
878 if (size_left <= MLXSW_REG_MBCT_DATA_LEN) {
879 data_size = size_left;
883 mlxsw_reg_mbct_pack(linecard->mbct_pl, linecard->slot_index,
884 MLXSW_REG_MBCT_OP_DATA_TRANSFER, false);
885 mlxsw_reg_mbct_dt_pack(linecard->mbct_pl, data_size,
887 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(mbct),
890 NL_SET_ERR_MSG_MOD(extack, "Failed to issue linecard INI data transfer");
893 mlxsw_reg_mbct_unpack(linecard->mbct_pl, NULL,
894 &status, &fsm_state);
895 if ((!is_last && status != MLXSW_REG_MBCT_STATUS_PART_DATA) ||
896 (is_last && status != MLXSW_REG_MBCT_STATUS_LAST_DATA)) {
897 NL_SET_ERR_MSG_MOD(extack, "Failed to transfer linecard INI data");
898 mlxsw_linecard_fix_fsm_state(linecard, fsm_state);
901 size_left -= data_size;
909 mlxsw_linecard_ini_erase(struct mlxsw_core *mlxsw_core,
910 struct mlxsw_linecard *linecard,
911 struct netlink_ext_ack *extack)
913 enum mlxsw_reg_mbct_fsm_state fsm_state;
914 enum mlxsw_reg_mbct_status status;
917 mlxsw_reg_mbct_pack(linecard->mbct_pl, linecard->slot_index,
918 MLXSW_REG_MBCT_OP_ERASE_INI_IMAGE, false);
919 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(mbct),
922 NL_SET_ERR_MSG_MOD(extack, "Failed to issue linecard INI erase");
925 mlxsw_reg_mbct_unpack(linecard->mbct_pl, NULL, &status, &fsm_state);
927 case MLXSW_REG_MBCT_STATUS_ERASE_COMPLETE:
930 /* Should not happen */
932 case MLXSW_REG_MBCT_STATUS_ERASE_FAILED:
933 NL_SET_ERR_MSG_MOD(extack, "Failed to erase linecard INI");
934 goto fix_fsm_err_out;
935 case MLXSW_REG_MBCT_STATUS_ERROR_INI_IN_USE:
936 NL_SET_ERR_MSG_MOD(extack, "Failed to erase linecard INI while being used");
937 goto fix_fsm_err_out;
942 mlxsw_linecard_fix_fsm_state(linecard, fsm_state);
946 static void mlxsw_linecard_bct_process(struct mlxsw_core *mlxsw_core,
949 struct mlxsw_linecards *linecards = mlxsw_core_linecards(mlxsw_core);
950 enum mlxsw_reg_mbct_fsm_state fsm_state;
951 enum mlxsw_reg_mbct_status status;
952 struct mlxsw_linecard *linecard;
955 mlxsw_reg_mbct_unpack(mbct_pl, &slot_index, &status, &fsm_state);
956 if (WARN_ON(slot_index > linecards->count))
958 linecard = mlxsw_linecard_get(linecards, slot_index);
959 mutex_lock(&linecard->lock);
960 if (status == MLXSW_REG_MBCT_STATUS_ACTIVATION_FAILED) {
961 dev_err(linecards->bus_info->dev, "linecard %u: Failed to activate INI",
962 linecard->slot_index);
965 mutex_unlock(&linecard->lock);
969 mlxsw_linecard_fix_fsm_state(linecard, fsm_state);
970 mlxsw_linecard_provision_fail(linecard);
971 mutex_unlock(&linecard->lock);
975 mlxsw_linecard_ini_activate(struct mlxsw_core *mlxsw_core,
976 struct mlxsw_linecard *linecard,
977 struct netlink_ext_ack *extack)
979 enum mlxsw_reg_mbct_fsm_state fsm_state;
980 enum mlxsw_reg_mbct_status status;
983 mlxsw_reg_mbct_pack(linecard->mbct_pl, linecard->slot_index,
984 MLXSW_REG_MBCT_OP_ACTIVATE, true);
985 err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(mbct), linecard->mbct_pl);
987 NL_SET_ERR_MSG_MOD(extack, "Failed to issue linecard INI activation");
990 mlxsw_reg_mbct_unpack(linecard->mbct_pl, NULL, &status, &fsm_state);
991 if (status == MLXSW_REG_MBCT_STATUS_ACTIVATION_FAILED) {
992 NL_SET_ERR_MSG_MOD(extack, "Failed to activate linecard INI");
993 goto fix_fsm_err_out;
999 mlxsw_linecard_fix_fsm_state(linecard, fsm_state);
1003 #define MLXSW_LINECARD_INI_WAIT_RETRIES 10
1004 #define MLXSW_LINECARD_INI_WAIT_MS 500
1007 mlxsw_linecard_ini_in_use_wait(struct mlxsw_core *mlxsw_core,
1008 struct mlxsw_linecard *linecard,
1009 struct netlink_ext_ack *extack)
1011 enum mlxsw_reg_mbct_fsm_state fsm_state;
1012 enum mlxsw_reg_mbct_status status;
1013 unsigned int ini_wait_retries = 0;
1017 err = mlxsw_linecard_query_ini_status(linecard, &status,
1018 &fsm_state, extack);
1022 switch (fsm_state) {
1023 case MLXSW_REG_MBCT_FSM_STATE_INI_IN_USE:
1024 if (ini_wait_retries++ > MLXSW_LINECARD_INI_WAIT_RETRIES) {
1025 NL_SET_ERR_MSG_MOD(extack, "Failed to wait for linecard INI to be unused");
1028 mdelay(MLXSW_LINECARD_INI_WAIT_MS);
1029 goto query_ini_status;
1036 static bool mlxsw_linecard_port_selector(void *priv, u16 local_port)
1038 struct mlxsw_linecard *linecard = priv;
1039 struct mlxsw_core *mlxsw_core;
1041 mlxsw_core = linecard->linecards->mlxsw_core;
1042 return linecard == mlxsw_core_port_linecard_get(mlxsw_core, local_port);
1045 static int mlxsw_linecard_provision(struct devlink_linecard *devlink_linecard,
1046 void *priv, const char *type,
1047 const void *type_priv,
1048 struct netlink_ext_ack *extack)
1050 const struct mlxsw_linecard_ini_file *ini_file = type_priv;
1051 struct mlxsw_linecard *linecard = priv;
1052 struct mlxsw_core *mlxsw_core;
1055 mutex_lock(&linecard->lock);
1057 mlxsw_core = linecard->linecards->mlxsw_core;
1059 err = mlxsw_linecard_ini_erase(mlxsw_core, linecard, extack);
1063 err = mlxsw_linecard_ini_transfer(mlxsw_core, linecard,
1068 mlxsw_linecard_status_event_to_schedule(linecard,
1069 MLXSW_LINECARD_STATUS_EVENT_TYPE_PROVISION);
1070 err = mlxsw_linecard_ini_activate(mlxsw_core, linecard, extack);
1077 mlxsw_linecard_provision_fail(linecard);
1079 mutex_unlock(&linecard->lock);
1083 static int mlxsw_linecard_unprovision(struct devlink_linecard *devlink_linecard,
1085 struct netlink_ext_ack *extack)
1087 struct mlxsw_linecard *linecard = priv;
1088 struct mlxsw_core *mlxsw_core;
1091 mutex_lock(&linecard->lock);
1093 mlxsw_core = linecard->linecards->mlxsw_core;
1095 mlxsw_core_ports_remove_selected(mlxsw_core,
1096 mlxsw_linecard_port_selector,
1099 err = mlxsw_linecard_ini_in_use_wait(mlxsw_core, linecard, extack);
1103 mlxsw_linecard_status_event_to_schedule(linecard,
1104 MLXSW_LINECARD_STATUS_EVENT_TYPE_UNPROVISION);
1105 err = mlxsw_linecard_ini_erase(mlxsw_core, linecard, extack);
1112 mlxsw_linecard_provision_fail(linecard);
1114 mutex_unlock(&linecard->lock);
1118 static bool mlxsw_linecard_same_provision(struct devlink_linecard *devlink_linecard,
1119 void *priv, const char *type,
1120 const void *type_priv)
1122 const struct mlxsw_linecard_ini_file *ini_file = type_priv;
1123 struct mlxsw_linecard *linecard = priv;
1126 mutex_lock(&linecard->lock);
1127 ret = linecard->hw_revision == be16_to_cpu(ini_file->format.hw_revision) &&
1128 linecard->ini_version == be16_to_cpu(ini_file->format.ini_version);
1129 mutex_unlock(&linecard->lock);
1134 mlxsw_linecard_types_count(struct devlink_linecard *devlink_linecard,
1137 struct mlxsw_linecard *linecard = priv;
1139 return linecard->linecards->types_info ?
1140 linecard->linecards->types_info->count : 0;
1143 static void mlxsw_linecard_types_get(struct devlink_linecard *devlink_linecard,
1144 void *priv, unsigned int index,
1145 const char **type, const void **type_priv)
1147 struct mlxsw_linecard_types_info *types_info;
1148 struct mlxsw_linecard_ini_file *ini_file;
1149 struct mlxsw_linecard *linecard = priv;
1151 types_info = linecard->linecards->types_info;
1152 if (WARN_ON_ONCE(!types_info))
1154 ini_file = types_info->ini_files[index];
1155 *type = ini_file->format.name;
1156 *type_priv = ini_file;
1159 static const struct devlink_linecard_ops mlxsw_linecard_ops = {
1160 .provision = mlxsw_linecard_provision,
1161 .unprovision = mlxsw_linecard_unprovision,
1162 .same_provision = mlxsw_linecard_same_provision,
1163 .types_count = mlxsw_linecard_types_count,
1164 .types_get = mlxsw_linecard_types_get,
1167 struct mlxsw_linecard_status_event {
1168 struct mlxsw_core *mlxsw_core;
1169 char mddq_pl[MLXSW_REG_MDDQ_LEN];
1170 struct work_struct work;
1173 static void mlxsw_linecard_status_event_work(struct work_struct *work)
1175 struct mlxsw_linecard_status_event *event;
1176 struct mlxsw_linecards *linecards;
1177 struct mlxsw_core *mlxsw_core;
1179 event = container_of(work, struct mlxsw_linecard_status_event, work);
1180 mlxsw_core = event->mlxsw_core;
1181 linecards = mlxsw_core_linecards(mlxsw_core);
1182 mlxsw_linecard_status_process(linecards, NULL, event->mddq_pl);
1187 mlxsw_linecard_status_listener_func(const struct mlxsw_reg_info *reg,
1188 char *mddq_pl, void *priv)
1190 struct mlxsw_linecard_status_event *event;
1191 struct mlxsw_core *mlxsw_core = priv;
1193 event = kmalloc(sizeof(*event), GFP_ATOMIC);
1196 event->mlxsw_core = mlxsw_core;
1197 memcpy(event->mddq_pl, mddq_pl, sizeof(event->mddq_pl));
1198 INIT_WORK(&event->work, mlxsw_linecard_status_event_work);
1199 mlxsw_core_schedule_work(&event->work);
1202 struct mlxsw_linecard_bct_event {
1203 struct mlxsw_core *mlxsw_core;
1204 char mbct_pl[MLXSW_REG_MBCT_LEN];
1205 struct work_struct work;
1208 static void mlxsw_linecard_bct_event_work(struct work_struct *work)
1210 struct mlxsw_linecard_bct_event *event;
1211 struct mlxsw_core *mlxsw_core;
1213 event = container_of(work, struct mlxsw_linecard_bct_event, work);
1214 mlxsw_core = event->mlxsw_core;
1215 mlxsw_linecard_bct_process(mlxsw_core, event->mbct_pl);
1220 mlxsw_linecard_bct_listener_func(const struct mlxsw_reg_info *reg,
1221 char *mbct_pl, void *priv)
1223 struct mlxsw_linecard_bct_event *event;
1224 struct mlxsw_core *mlxsw_core = priv;
1226 event = kmalloc(sizeof(*event), GFP_ATOMIC);
1229 event->mlxsw_core = mlxsw_core;
1230 memcpy(event->mbct_pl, mbct_pl, sizeof(event->mbct_pl));
1231 INIT_WORK(&event->work, mlxsw_linecard_bct_event_work);
1232 mlxsw_core_schedule_work(&event->work);
1235 static const struct mlxsw_listener mlxsw_linecard_listener[] = {
1236 MLXSW_CORE_EVENTL(mlxsw_linecard_status_listener_func, DSDSC),
1237 MLXSW_CORE_EVENTL(mlxsw_linecard_bct_listener_func, BCTOE),
1240 static int mlxsw_linecard_event_delivery_set(struct mlxsw_core *mlxsw_core,
1241 struct mlxsw_linecard *linecard,
1244 char mddq_pl[MLXSW_REG_MDDQ_LEN];
1246 mlxsw_reg_mddq_slot_info_pack(mddq_pl, linecard->slot_index, enable);
1247 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(mddq), mddq_pl);
1250 static int mlxsw_linecard_init(struct mlxsw_core *mlxsw_core,
1251 struct mlxsw_linecards *linecards,
1254 struct devlink_linecard *devlink_linecard;
1255 struct mlxsw_linecard *linecard;
1257 linecard = mlxsw_linecard_get(linecards, slot_index);
1258 linecard->slot_index = slot_index;
1259 linecard->linecards = linecards;
1260 mutex_init(&linecard->lock);
1262 devlink_linecard = devl_linecard_create(priv_to_devlink(mlxsw_core),
1263 slot_index, &mlxsw_linecard_ops,
1265 if (IS_ERR(devlink_linecard))
1266 return PTR_ERR(devlink_linecard);
1268 linecard->devlink_linecard = devlink_linecard;
1269 INIT_DELAYED_WORK(&linecard->status_event_to_dw,
1270 &mlxsw_linecard_status_event_to_work);
1275 static void mlxsw_linecard_fini(struct mlxsw_core *mlxsw_core,
1276 struct mlxsw_linecards *linecards,
1279 struct mlxsw_linecard *linecard;
1281 linecard = mlxsw_linecard_get(linecards, slot_index);
1282 cancel_delayed_work_sync(&linecard->status_event_to_dw);
1283 /* Make sure all scheduled events are processed */
1284 mlxsw_core_flush_owq();
1285 if (linecard->active)
1286 mlxsw_linecard_active_clear(linecard);
1287 mlxsw_linecard_bdev_del(linecard);
1288 devl_linecard_destroy(linecard->devlink_linecard);
1289 mutex_destroy(&linecard->lock);
1293 mlxsw_linecard_event_delivery_init(struct mlxsw_core *mlxsw_core,
1294 struct mlxsw_linecards *linecards,
1297 struct mlxsw_linecard *linecard;
1300 linecard = mlxsw_linecard_get(linecards, slot_index);
1301 err = mlxsw_linecard_event_delivery_set(mlxsw_core, linecard, true);
1305 err = mlxsw_linecard_status_get_and_process(mlxsw_core, linecards,
1308 goto err_status_get_and_process;
1312 err_status_get_and_process:
1313 mlxsw_linecard_event_delivery_set(mlxsw_core, linecard, false);
1318 mlxsw_linecard_event_delivery_fini(struct mlxsw_core *mlxsw_core,
1319 struct mlxsw_linecards *linecards,
1322 struct mlxsw_linecard *linecard;
1324 linecard = mlxsw_linecard_get(linecards, slot_index);
1325 mlxsw_linecard_event_delivery_set(mlxsw_core, linecard, false);
1328 /* LINECARDS INI BUNDLE FILE
1329 * +----------------------------------+
1330 * | MAGIC ("NVLCINI+") |
1331 * +----------------------------------+ +--------------------+
1332 * | INI 0 +---> | __le16 size |
1333 * +----------------------------------+ | __be16 hw_revision |
1334 * | INI 1 | | __be16 ini_version |
1335 * +----------------------------------+ | u8 __dontcare[3] |
1336 * | ... | | u8 type |
1337 * +----------------------------------+ | u8 name[20] |
1339 * +----------------------------------+ +--------------------+
1342 #define MLXSW_LINECARDS_INI_BUNDLE_MAGIC "NVLCINI+"
1345 mlxsw_linecard_types_file_validate(struct mlxsw_linecards *linecards,
1346 struct mlxsw_linecard_types_info *types_info)
1348 size_t magic_size = strlen(MLXSW_LINECARDS_INI_BUNDLE_MAGIC);
1349 struct mlxsw_linecard_ini_file *ini_file;
1350 size_t size = types_info->data_size;
1351 const u8 *data = types_info->data;
1352 unsigned int count = 0;
1355 if (size < magic_size) {
1356 dev_warn(linecards->bus_info->dev, "Invalid linecards INIs file size, smaller than magic size\n");
1359 if (memcmp(data, MLXSW_LINECARDS_INI_BUNDLE_MAGIC, magic_size)) {
1360 dev_warn(linecards->bus_info->dev, "Invalid linecards INIs file magic pattern\n");
1368 if (size < sizeof(*ini_file)) {
1369 dev_warn(linecards->bus_info->dev, "Linecards INIs file contains INI which is smaller than bare minimum\n");
1372 ini_file = (struct mlxsw_linecard_ini_file *) data;
1373 ini_file_size = le16_to_cpu(ini_file->size);
1374 if (ini_file_size + sizeof(__le16) > size) {
1375 dev_warn(linecards->bus_info->dev, "Linecards INIs file appears to be truncated\n");
1378 if (ini_file_size % 4) {
1379 dev_warn(linecards->bus_info->dev, "Linecards INIs file contains INI with invalid size\n");
1382 data += ini_file_size + sizeof(__le16);
1383 size -= ini_file_size + sizeof(__le16);
1387 dev_warn(linecards->bus_info->dev, "Linecards INIs file does not contain any INI\n");
1390 types_info->count = count;
1395 mlxsw_linecard_types_file_parse(struct mlxsw_linecard_types_info *types_info)
1397 size_t magic_size = strlen(MLXSW_LINECARDS_INI_BUNDLE_MAGIC);
1398 size_t size = types_info->data_size - magic_size;
1399 const u8 *data = types_info->data + magic_size;
1400 struct mlxsw_linecard_ini_file *ini_file;
1401 unsigned int count = 0;
1406 ini_file = (struct mlxsw_linecard_ini_file *) data;
1407 ini_file_size = le16_to_cpu(ini_file->size);
1408 for (i = 0; i < ini_file_size / 4; i++) {
1409 u32 *val = &((u32 *) ini_file->data)[i];
1411 *val = swab32(*val);
1413 types_info->ini_files[count] = ini_file;
1414 data += ini_file_size + sizeof(__le16);
1415 size -= ini_file_size + sizeof(__le16);
1420 #define MLXSW_LINECARDS_INI_BUNDLE_FILENAME_FMT \
1421 "mellanox/lc_ini_bundle_%u_%u.bin"
1422 #define MLXSW_LINECARDS_INI_BUNDLE_FILENAME_LEN \
1423 (sizeof(MLXSW_LINECARDS_INI_BUNDLE_FILENAME_FMT) + 4)
1425 static int mlxsw_linecard_types_init(struct mlxsw_core *mlxsw_core,
1426 struct mlxsw_linecards *linecards)
1428 const struct mlxsw_fw_rev *rev = &linecards->bus_info->fw_rev;
1429 char filename[MLXSW_LINECARDS_INI_BUNDLE_FILENAME_LEN];
1430 struct mlxsw_linecard_types_info *types_info;
1431 const struct firmware *firmware;
1434 err = snprintf(filename, sizeof(filename),
1435 MLXSW_LINECARDS_INI_BUNDLE_FILENAME_FMT,
1436 rev->minor, rev->subminor);
1437 WARN_ON(err >= sizeof(filename));
1439 err = request_firmware_direct(&firmware, filename,
1440 linecards->bus_info->dev);
1442 dev_warn(linecards->bus_info->dev, "Could not request linecards INI file \"%s\", provisioning will not be possible\n",
1447 types_info = kzalloc(sizeof(*types_info), GFP_KERNEL);
1449 release_firmware(firmware);
1452 linecards->types_info = types_info;
1454 types_info->data_size = firmware->size;
1455 types_info->data = vmalloc(types_info->data_size);
1456 if (!types_info->data) {
1458 release_firmware(firmware);
1459 goto err_data_alloc;
1461 memcpy(types_info->data, firmware->data, types_info->data_size);
1462 release_firmware(firmware);
1464 err = mlxsw_linecard_types_file_validate(linecards, types_info);
1467 goto err_type_file_file_validate;
1470 types_info->ini_files = kmalloc_array(types_info->count,
1471 sizeof(struct mlxsw_linecard_ini_file *),
1473 if (!types_info->ini_files) {
1475 goto err_ini_files_alloc;
1478 mlxsw_linecard_types_file_parse(types_info);
1482 err_ini_files_alloc:
1483 err_type_file_file_validate:
1484 vfree(types_info->data);
1490 static void mlxsw_linecard_types_fini(struct mlxsw_linecards *linecards)
1492 struct mlxsw_linecard_types_info *types_info = linecards->types_info;
1496 kfree(types_info->ini_files);
1497 vfree(types_info->data);
1501 int mlxsw_linecards_init(struct mlxsw_core *mlxsw_core,
1502 const struct mlxsw_bus_info *bus_info)
1504 char mgpir_pl[MLXSW_REG_MGPIR_LEN];
1505 struct mlxsw_linecards *linecards;
1510 mlxsw_reg_mgpir_pack(mgpir_pl, 0);
1511 err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mgpir), mgpir_pl);
1515 mlxsw_reg_mgpir_unpack(mgpir_pl, NULL, NULL, NULL,
1520 linecards = vzalloc(struct_size(linecards, linecards, slot_count));
1523 linecards->count = slot_count;
1524 linecards->mlxsw_core = mlxsw_core;
1525 linecards->bus_info = bus_info;
1526 INIT_LIST_HEAD(&linecards->event_ops_list);
1527 mutex_init(&linecards->event_ops_list_lock);
1529 err = mlxsw_linecard_types_init(mlxsw_core, linecards);
1531 goto err_types_init;
1533 err = mlxsw_core_traps_register(mlxsw_core, mlxsw_linecard_listener,
1534 ARRAY_SIZE(mlxsw_linecard_listener),
1537 goto err_traps_register;
1539 err = mlxsw_core_irq_event_handler_register(mlxsw_core,
1540 mlxsw_linecards_irq_event_handler);
1542 goto err_irq_event_handler_register;
1544 mlxsw_core_linecards_set(mlxsw_core, linecards);
1546 for (i = 0; i < linecards->count; i++) {
1547 err = mlxsw_linecard_init(mlxsw_core, linecards, i + 1);
1549 goto err_linecard_init;
1552 for (i = 0; i < linecards->count; i++) {
1553 err = mlxsw_linecard_event_delivery_init(mlxsw_core, linecards,
1556 goto err_linecard_event_delivery_init;
1561 err_linecard_event_delivery_init:
1562 for (i--; i >= 0; i--)
1563 mlxsw_linecard_event_delivery_fini(mlxsw_core, linecards, i + 1);
1564 i = linecards->count;
1566 for (i--; i >= 0; i--)
1567 mlxsw_linecard_fini(mlxsw_core, linecards, i + 1);
1568 mlxsw_core_irq_event_handler_unregister(mlxsw_core,
1569 mlxsw_linecards_irq_event_handler);
1570 err_irq_event_handler_register:
1571 mlxsw_core_traps_unregister(mlxsw_core, mlxsw_linecard_listener,
1572 ARRAY_SIZE(mlxsw_linecard_listener),
1575 mlxsw_linecard_types_fini(linecards);
1581 void mlxsw_linecards_fini(struct mlxsw_core *mlxsw_core)
1583 struct mlxsw_linecards *linecards = mlxsw_core_linecards(mlxsw_core);
1588 for (i = 0; i < linecards->count; i++)
1589 mlxsw_linecard_event_delivery_fini(mlxsw_core, linecards, i + 1);
1590 for (i = 0; i < linecards->count; i++)
1591 mlxsw_linecard_fini(mlxsw_core, linecards, i + 1);
1592 mlxsw_core_irq_event_handler_unregister(mlxsw_core,
1593 mlxsw_linecards_irq_event_handler);
1594 mlxsw_core_traps_unregister(mlxsw_core, mlxsw_linecard_listener,
1595 ARRAY_SIZE(mlxsw_linecard_listener),
1597 mlxsw_linecard_types_fini(linecards);
1598 mutex_destroy(&linecards->event_ops_list_lock);
1599 WARN_ON(!list_empty(&linecards->event_ops_list));