1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
3 
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/device.h>
7 #include <linux/export.h>
8 #include <linux/err.h>
9 #include <linux/if_link.h>
10 #include <linux/netdevice.h>
11 #include <linux/completion.h>
12 #include <linux/skbuff.h>
13 #include <linux/etherdevice.h>
14 #include <linux/types.h>
15 #include <linux/string.h>
16 #include <linux/gfp.h>
17 #include <linux/random.h>
18 #include <linux/jiffies.h>
19 #include <linux/mutex.h>
20 #include <linux/rcupdate.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <asm/byteorder.h>
24 #include <net/devlink.h>
25 #include <trace/events/devlink.h>
26 
27 #include "core.h"
28 #include "item.h"
29 #include "cmd.h"
30 #include "port.h"
31 #include "trap.h"
32 #include "emad.h"
33 #include "reg.h"
34 #include "resources.h"
35 
36 static LIST_HEAD(mlxsw_core_driver_list);
37 static DEFINE_SPINLOCK(mlxsw_core_driver_list_lock);
38 
39 static const char mlxsw_core_driver_name[] = "mlxsw_core";
40 
41 static struct workqueue_struct *mlxsw_wq;
42 static struct workqueue_struct *mlxsw_owq;
43 
44 struct mlxsw_core_port {
45 	struct devlink_port devlink_port;
46 	void *port_driver_priv;
47 	u8 local_port;
48 };
49 
50 void *mlxsw_core_port_driver_priv(struct mlxsw_core_port *mlxsw_core_port)
51 {
52 	return mlxsw_core_port->port_driver_priv;
53 }
54 EXPORT_SYMBOL(mlxsw_core_port_driver_priv);
55 
56 static bool mlxsw_core_port_check(struct mlxsw_core_port *mlxsw_core_port)
57 {
58 	return mlxsw_core_port->port_driver_priv != NULL;
59 }
60 
61 struct mlxsw_core {
62 	struct mlxsw_driver *driver;
63 	const struct mlxsw_bus *bus;
64 	void *bus_priv;
65 	const struct mlxsw_bus_info *bus_info;
66 	struct workqueue_struct *emad_wq;
67 	struct list_head rx_listener_list;
68 	struct list_head event_listener_list;
69 	struct {
70 		atomic64_t tid;
71 		struct list_head trans_list;
72 		spinlock_t trans_list_lock; /* protects trans_list writes */
73 		bool use_emad;
74 		bool enable_string_tlv;
75 	} emad;
76 	struct {
77 		u8 *mapping; /* lag_id+port_index to local_port mapping */
78 	} lag;
79 	struct mlxsw_res res;
80 	struct mlxsw_hwmon *hwmon;
81 	struct mlxsw_thermal *thermal;
82 	struct mlxsw_core_port *ports;
83 	unsigned int max_ports;
84 	bool fw_flash_in_progress;
85 	unsigned long driver_priv[];
86 	/* driver_priv has to be always the last item */
87 };
88 
89 #define MLXSW_PORT_MAX_PORTS_DEFAULT	0x40
90 
91 static int mlxsw_ports_init(struct mlxsw_core *mlxsw_core)
92 {
93 	/* Switch ports are numbered from 1 to queried value */
94 	if (MLXSW_CORE_RES_VALID(mlxsw_core, MAX_SYSTEM_PORT))
95 		mlxsw_core->max_ports = MLXSW_CORE_RES_GET(mlxsw_core,
96 							   MAX_SYSTEM_PORT) + 1;
97 	else
98 		mlxsw_core->max_ports = MLXSW_PORT_MAX_PORTS_DEFAULT + 1;
99 
100 	mlxsw_core->ports = kcalloc(mlxsw_core->max_ports,
101 				    sizeof(struct mlxsw_core_port), GFP_KERNEL);
102 	if (!mlxsw_core->ports)
103 		return -ENOMEM;
104 
105 	return 0;
106 }
107 
108 static void mlxsw_ports_fini(struct mlxsw_core *mlxsw_core)
109 {
110 	kfree(mlxsw_core->ports);
111 }
112 
113 unsigned int mlxsw_core_max_ports(const struct mlxsw_core *mlxsw_core)
114 {
115 	return mlxsw_core->max_ports;
116 }
117 EXPORT_SYMBOL(mlxsw_core_max_ports);
118 
119 void *mlxsw_core_driver_priv(struct mlxsw_core *mlxsw_core)
120 {
121 	return mlxsw_core->driver_priv;
122 }
123 EXPORT_SYMBOL(mlxsw_core_driver_priv);
124 
125 bool mlxsw_core_res_query_enabled(const struct mlxsw_core *mlxsw_core)
126 {
127 	return mlxsw_core->driver->res_query_enabled;
128 }
129 EXPORT_SYMBOL(mlxsw_core_res_query_enabled);
130 
131 bool
132 mlxsw_core_fw_rev_minor_subminor_validate(const struct mlxsw_fw_rev *rev,
133 					  const struct mlxsw_fw_rev *req_rev)
134 {
135 	return rev->minor > req_rev->minor ||
136 	       (rev->minor == req_rev->minor &&
137 		rev->subminor >= req_rev->subminor);
138 }
139 EXPORT_SYMBOL(mlxsw_core_fw_rev_minor_subminor_validate);
140 
141 struct mlxsw_rx_listener_item {
142 	struct list_head list;
143 	struct mlxsw_rx_listener rxl;
144 	void *priv;
145 	bool enabled;
146 };
147 
148 struct mlxsw_event_listener_item {
149 	struct list_head list;
150 	struct mlxsw_event_listener el;
151 	void *priv;
152 };
153 
154 /******************
155  * EMAD processing
156  ******************/
157 
158 /* emad_eth_hdr_dmac
159  * Destination MAC in EMAD's Ethernet header.
160  * Must be set to 01:02:c9:00:00:01
161  */
162 MLXSW_ITEM_BUF(emad, eth_hdr, dmac, 0x00, 6);
163 
164 /* emad_eth_hdr_smac
165  * Source MAC in EMAD's Ethernet header.
166  * Must be set to 00:02:c9:01:02:03
167  */
168 MLXSW_ITEM_BUF(emad, eth_hdr, smac, 0x06, 6);
169 
170 /* emad_eth_hdr_ethertype
171  * Ethertype in EMAD's Ethernet header.
172  * Must be set to 0x8932
173  */
174 MLXSW_ITEM32(emad, eth_hdr, ethertype, 0x0C, 16, 16);
175 
176 /* emad_eth_hdr_mlx_proto
177  * Mellanox protocol.
178  * Must be set to 0x0.
179  */
180 MLXSW_ITEM32(emad, eth_hdr, mlx_proto, 0x0C, 8, 8);
181 
182 /* emad_eth_hdr_ver
183  * Mellanox protocol version.
184  * Must be set to 0x0.
185  */
186 MLXSW_ITEM32(emad, eth_hdr, ver, 0x0C, 4, 4);
187 
188 /* emad_op_tlv_type
189  * Type of the TLV.
190  * Must be set to 0x1 (operation TLV).
191  */
192 MLXSW_ITEM32(emad, op_tlv, type, 0x00, 27, 5);
193 
194 /* emad_op_tlv_len
195  * Length of the operation TLV in u32.
196  * Must be set to 0x4.
197  */
198 MLXSW_ITEM32(emad, op_tlv, len, 0x00, 16, 11);
199 
200 /* emad_op_tlv_dr
201  * Direct route bit. Setting to 1 indicates the EMAD is a direct route
202  * EMAD. DR TLV must follow.
203  *
204  * Note: Currently not supported and must not be set.
205  */
206 MLXSW_ITEM32(emad, op_tlv, dr, 0x00, 15, 1);
207 
208 /* emad_op_tlv_status
209  * Returned status in case of EMAD response. Must be set to 0 in case
210  * of EMAD request.
211  * 0x0 - success
212  * 0x1 - device is busy. Requester should retry
213  * 0x2 - Mellanox protocol version not supported
214  * 0x3 - unknown TLV
215  * 0x4 - register not supported
216  * 0x5 - operation class not supported
217  * 0x6 - EMAD method not supported
218  * 0x7 - bad parameter (e.g. port out of range)
219  * 0x8 - resource not available
220  * 0x9 - message receipt acknowledgment. Requester should retry
221  * 0x70 - internal error
222  */
223 MLXSW_ITEM32(emad, op_tlv, status, 0x00, 8, 7);
224 
225 /* emad_op_tlv_register_id
226  * Register ID of register within register TLV.
227  */
228 MLXSW_ITEM32(emad, op_tlv, register_id, 0x04, 16, 16);
229 
230 /* emad_op_tlv_r
231  * Response bit. Setting to 1 indicates Response, otherwise request.
232  */
233 MLXSW_ITEM32(emad, op_tlv, r, 0x04, 15, 1);
234 
235 /* emad_op_tlv_method
236  * EMAD method type.
237  * 0x1 - query
238  * 0x2 - write
239  * 0x3 - send (currently not supported)
240  * 0x4 - event
241  */
242 MLXSW_ITEM32(emad, op_tlv, method, 0x04, 8, 7);
243 
244 /* emad_op_tlv_class
245  * EMAD operation class. Must be set to 0x1 (REG_ACCESS).
246  */
247 MLXSW_ITEM32(emad, op_tlv, class, 0x04, 0, 8);
248 
249 /* emad_op_tlv_tid
250  * EMAD transaction ID. Used for pairing request and response EMADs.
251  */
252 MLXSW_ITEM64(emad, op_tlv, tid, 0x08, 0, 64);
253 
254 /* emad_string_tlv_type
255  * Type of the TLV.
256  * Must be set to 0x2 (string TLV).
257  */
258 MLXSW_ITEM32(emad, string_tlv, type, 0x00, 27, 5);
259 
260 /* emad_string_tlv_len
261  * Length of the string TLV in u32.
262  */
263 MLXSW_ITEM32(emad, string_tlv, len, 0x00, 16, 11);
264 
265 #define MLXSW_EMAD_STRING_TLV_STRING_LEN 128
266 
267 /* emad_string_tlv_string
268  * String provided by the device's firmware in case of erroneous register access
269  */
270 MLXSW_ITEM_BUF(emad, string_tlv, string, 0x04,
271 	       MLXSW_EMAD_STRING_TLV_STRING_LEN);
272 
273 /* emad_reg_tlv_type
274  * Type of the TLV.
275  * Must be set to 0x3 (register TLV).
276  */
277 MLXSW_ITEM32(emad, reg_tlv, type, 0x00, 27, 5);
278 
279 /* emad_reg_tlv_len
280  * Length of the operation TLV in u32.
281  */
282 MLXSW_ITEM32(emad, reg_tlv, len, 0x00, 16, 11);
283 
284 /* emad_end_tlv_type
285  * Type of the TLV.
286  * Must be set to 0x0 (end TLV).
287  */
288 MLXSW_ITEM32(emad, end_tlv, type, 0x00, 27, 5);
289 
290 /* emad_end_tlv_len
291  * Length of the end TLV in u32.
292  * Must be set to 1.
293  */
294 MLXSW_ITEM32(emad, end_tlv, len, 0x00, 16, 11);
295 
296 enum mlxsw_core_reg_access_type {
297 	MLXSW_CORE_REG_ACCESS_TYPE_QUERY,
298 	MLXSW_CORE_REG_ACCESS_TYPE_WRITE,
299 };
300 
301 static inline const char *
302 mlxsw_core_reg_access_type_str(enum mlxsw_core_reg_access_type type)
303 {
304 	switch (type) {
305 	case MLXSW_CORE_REG_ACCESS_TYPE_QUERY:
306 		return "query";
307 	case MLXSW_CORE_REG_ACCESS_TYPE_WRITE:
308 		return "write";
309 	}
310 	BUG();
311 }
312 
313 static void mlxsw_emad_pack_end_tlv(char *end_tlv)
314 {
315 	mlxsw_emad_end_tlv_type_set(end_tlv, MLXSW_EMAD_TLV_TYPE_END);
316 	mlxsw_emad_end_tlv_len_set(end_tlv, MLXSW_EMAD_END_TLV_LEN);
317 }
318 
319 static void mlxsw_emad_pack_reg_tlv(char *reg_tlv,
320 				    const struct mlxsw_reg_info *reg,
321 				    char *payload)
322 {
323 	mlxsw_emad_reg_tlv_type_set(reg_tlv, MLXSW_EMAD_TLV_TYPE_REG);
324 	mlxsw_emad_reg_tlv_len_set(reg_tlv, reg->len / sizeof(u32) + 1);
325 	memcpy(reg_tlv + sizeof(u32), payload, reg->len);
326 }
327 
328 static void mlxsw_emad_pack_string_tlv(char *string_tlv)
329 {
330 	mlxsw_emad_string_tlv_type_set(string_tlv, MLXSW_EMAD_TLV_TYPE_STRING);
331 	mlxsw_emad_string_tlv_len_set(string_tlv, MLXSW_EMAD_STRING_TLV_LEN);
332 }
333 
334 static void mlxsw_emad_pack_op_tlv(char *op_tlv,
335 				   const struct mlxsw_reg_info *reg,
336 				   enum mlxsw_core_reg_access_type type,
337 				   u64 tid)
338 {
339 	mlxsw_emad_op_tlv_type_set(op_tlv, MLXSW_EMAD_TLV_TYPE_OP);
340 	mlxsw_emad_op_tlv_len_set(op_tlv, MLXSW_EMAD_OP_TLV_LEN);
341 	mlxsw_emad_op_tlv_dr_set(op_tlv, 0);
342 	mlxsw_emad_op_tlv_status_set(op_tlv, 0);
343 	mlxsw_emad_op_tlv_register_id_set(op_tlv, reg->id);
344 	mlxsw_emad_op_tlv_r_set(op_tlv, MLXSW_EMAD_OP_TLV_REQUEST);
345 	if (type == MLXSW_CORE_REG_ACCESS_TYPE_QUERY)
346 		mlxsw_emad_op_tlv_method_set(op_tlv,
347 					     MLXSW_EMAD_OP_TLV_METHOD_QUERY);
348 	else
349 		mlxsw_emad_op_tlv_method_set(op_tlv,
350 					     MLXSW_EMAD_OP_TLV_METHOD_WRITE);
351 	mlxsw_emad_op_tlv_class_set(op_tlv,
352 				    MLXSW_EMAD_OP_TLV_CLASS_REG_ACCESS);
353 	mlxsw_emad_op_tlv_tid_set(op_tlv, tid);
354 }
355 
356 static int mlxsw_emad_construct_eth_hdr(struct sk_buff *skb)
357 {
358 	char *eth_hdr = skb_push(skb, MLXSW_EMAD_ETH_HDR_LEN);
359 
360 	mlxsw_emad_eth_hdr_dmac_memcpy_to(eth_hdr, MLXSW_EMAD_EH_DMAC);
361 	mlxsw_emad_eth_hdr_smac_memcpy_to(eth_hdr, MLXSW_EMAD_EH_SMAC);
362 	mlxsw_emad_eth_hdr_ethertype_set(eth_hdr, MLXSW_EMAD_EH_ETHERTYPE);
363 	mlxsw_emad_eth_hdr_mlx_proto_set(eth_hdr, MLXSW_EMAD_EH_MLX_PROTO);
364 	mlxsw_emad_eth_hdr_ver_set(eth_hdr, MLXSW_EMAD_EH_PROTO_VERSION);
365 
366 	skb_reset_mac_header(skb);
367 
368 	return 0;
369 }
370 
371 static void mlxsw_emad_construct(struct sk_buff *skb,
372 				 const struct mlxsw_reg_info *reg,
373 				 char *payload,
374 				 enum mlxsw_core_reg_access_type type,
375 				 u64 tid, bool enable_string_tlv)
376 {
377 	char *buf;
378 
379 	buf = skb_push(skb, MLXSW_EMAD_END_TLV_LEN * sizeof(u32));
380 	mlxsw_emad_pack_end_tlv(buf);
381 
382 	buf = skb_push(skb, reg->len + sizeof(u32));
383 	mlxsw_emad_pack_reg_tlv(buf, reg, payload);
384 
385 	if (enable_string_tlv) {
386 		buf = skb_push(skb, MLXSW_EMAD_STRING_TLV_LEN * sizeof(u32));
387 		mlxsw_emad_pack_string_tlv(buf);
388 	}
389 
390 	buf = skb_push(skb, MLXSW_EMAD_OP_TLV_LEN * sizeof(u32));
391 	mlxsw_emad_pack_op_tlv(buf, reg, type, tid);
392 
393 	mlxsw_emad_construct_eth_hdr(skb);
394 }
395 
396 struct mlxsw_emad_tlv_offsets {
397 	u16 op_tlv;
398 	u16 string_tlv;
399 	u16 reg_tlv;
400 };
401 
402 static bool mlxsw_emad_tlv_is_string_tlv(const char *tlv)
403 {
404 	u8 tlv_type = mlxsw_emad_string_tlv_type_get(tlv);
405 
406 	return tlv_type == MLXSW_EMAD_TLV_TYPE_STRING;
407 }
408 
409 static void mlxsw_emad_tlv_parse(struct sk_buff *skb)
410 {
411 	struct mlxsw_emad_tlv_offsets *offsets =
412 		(struct mlxsw_emad_tlv_offsets *) skb->cb;
413 
414 	offsets->op_tlv = MLXSW_EMAD_ETH_HDR_LEN;
415 	offsets->string_tlv = 0;
416 	offsets->reg_tlv = MLXSW_EMAD_ETH_HDR_LEN +
417 			   MLXSW_EMAD_OP_TLV_LEN * sizeof(u32);
418 
419 	/* If string TLV is present, it must come after the operation TLV. */
420 	if (mlxsw_emad_tlv_is_string_tlv(skb->data + offsets->reg_tlv)) {
421 		offsets->string_tlv = offsets->reg_tlv;
422 		offsets->reg_tlv += MLXSW_EMAD_STRING_TLV_LEN * sizeof(u32);
423 	}
424 }
425 
426 static char *mlxsw_emad_op_tlv(const struct sk_buff *skb)
427 {
428 	struct mlxsw_emad_tlv_offsets *offsets =
429 		(struct mlxsw_emad_tlv_offsets *) skb->cb;
430 
431 	return ((char *) (skb->data + offsets->op_tlv));
432 }
433 
434 static char *mlxsw_emad_string_tlv(const struct sk_buff *skb)
435 {
436 	struct mlxsw_emad_tlv_offsets *offsets =
437 		(struct mlxsw_emad_tlv_offsets *) skb->cb;
438 
439 	if (!offsets->string_tlv)
440 		return NULL;
441 
442 	return ((char *) (skb->data + offsets->string_tlv));
443 }
444 
445 static char *mlxsw_emad_reg_tlv(const struct sk_buff *skb)
446 {
447 	struct mlxsw_emad_tlv_offsets *offsets =
448 		(struct mlxsw_emad_tlv_offsets *) skb->cb;
449 
450 	return ((char *) (skb->data + offsets->reg_tlv));
451 }
452 
453 static char *mlxsw_emad_reg_payload(const char *reg_tlv)
454 {
455 	return ((char *) (reg_tlv + sizeof(u32)));
456 }
457 
458 static char *mlxsw_emad_reg_payload_cmd(const char *mbox)
459 {
460 	return ((char *) (mbox + (MLXSW_EMAD_OP_TLV_LEN + 1) * sizeof(u32)));
461 }
462 
463 static u64 mlxsw_emad_get_tid(const struct sk_buff *skb)
464 {
465 	char *op_tlv;
466 
467 	op_tlv = mlxsw_emad_op_tlv(skb);
468 	return mlxsw_emad_op_tlv_tid_get(op_tlv);
469 }
470 
471 static bool mlxsw_emad_is_resp(const struct sk_buff *skb)
472 {
473 	char *op_tlv;
474 
475 	op_tlv = mlxsw_emad_op_tlv(skb);
476 	return (mlxsw_emad_op_tlv_r_get(op_tlv) == MLXSW_EMAD_OP_TLV_RESPONSE);
477 }
478 
479 static int mlxsw_emad_process_status(char *op_tlv,
480 				     enum mlxsw_emad_op_tlv_status *p_status)
481 {
482 	*p_status = mlxsw_emad_op_tlv_status_get(op_tlv);
483 
484 	switch (*p_status) {
485 	case MLXSW_EMAD_OP_TLV_STATUS_SUCCESS:
486 		return 0;
487 	case MLXSW_EMAD_OP_TLV_STATUS_BUSY:
488 	case MLXSW_EMAD_OP_TLV_STATUS_MESSAGE_RECEIPT_ACK:
489 		return -EAGAIN;
490 	case MLXSW_EMAD_OP_TLV_STATUS_VERSION_NOT_SUPPORTED:
491 	case MLXSW_EMAD_OP_TLV_STATUS_UNKNOWN_TLV:
492 	case MLXSW_EMAD_OP_TLV_STATUS_REGISTER_NOT_SUPPORTED:
493 	case MLXSW_EMAD_OP_TLV_STATUS_CLASS_NOT_SUPPORTED:
494 	case MLXSW_EMAD_OP_TLV_STATUS_METHOD_NOT_SUPPORTED:
495 	case MLXSW_EMAD_OP_TLV_STATUS_BAD_PARAMETER:
496 	case MLXSW_EMAD_OP_TLV_STATUS_RESOURCE_NOT_AVAILABLE:
497 	case MLXSW_EMAD_OP_TLV_STATUS_INTERNAL_ERROR:
498 	default:
499 		return -EIO;
500 	}
501 }
502 
503 static int
504 mlxsw_emad_process_status_skb(struct sk_buff *skb,
505 			      enum mlxsw_emad_op_tlv_status *p_status)
506 {
507 	return mlxsw_emad_process_status(mlxsw_emad_op_tlv(skb), p_status);
508 }
509 
510 struct mlxsw_reg_trans {
511 	struct list_head list;
512 	struct list_head bulk_list;
513 	struct mlxsw_core *core;
514 	struct sk_buff *tx_skb;
515 	struct mlxsw_tx_info tx_info;
516 	struct delayed_work timeout_dw;
517 	unsigned int retries;
518 	u64 tid;
519 	struct completion completion;
520 	atomic_t active;
521 	mlxsw_reg_trans_cb_t *cb;
522 	unsigned long cb_priv;
523 	const struct mlxsw_reg_info *reg;
524 	enum mlxsw_core_reg_access_type type;
525 	int err;
526 	char *emad_err_string;
527 	enum mlxsw_emad_op_tlv_status emad_status;
528 	struct rcu_head rcu;
529 };
530 
531 static void mlxsw_emad_process_string_tlv(const struct sk_buff *skb,
532 					  struct mlxsw_reg_trans *trans)
533 {
534 	char *string_tlv;
535 	char *string;
536 
537 	string_tlv = mlxsw_emad_string_tlv(skb);
538 	if (!string_tlv)
539 		return;
540 
541 	trans->emad_err_string = kzalloc(MLXSW_EMAD_STRING_TLV_STRING_LEN,
542 					 GFP_ATOMIC);
543 	if (!trans->emad_err_string)
544 		return;
545 
546 	string = mlxsw_emad_string_tlv_string_data(string_tlv);
547 	strlcpy(trans->emad_err_string, string,
548 		MLXSW_EMAD_STRING_TLV_STRING_LEN);
549 }
550 
551 #define MLXSW_EMAD_TIMEOUT_DURING_FW_FLASH_MS	3000
552 #define MLXSW_EMAD_TIMEOUT_MS			200
553 
554 static void mlxsw_emad_trans_timeout_schedule(struct mlxsw_reg_trans *trans)
555 {
556 	unsigned long timeout = msecs_to_jiffies(MLXSW_EMAD_TIMEOUT_MS);
557 
558 	if (trans->core->fw_flash_in_progress)
559 		timeout = msecs_to_jiffies(MLXSW_EMAD_TIMEOUT_DURING_FW_FLASH_MS);
560 
561 	queue_delayed_work(trans->core->emad_wq, &trans->timeout_dw, timeout);
562 }
563 
564 static int mlxsw_emad_transmit(struct mlxsw_core *mlxsw_core,
565 			       struct mlxsw_reg_trans *trans)
566 {
567 	struct sk_buff *skb;
568 	int err;
569 
570 	skb = skb_copy(trans->tx_skb, GFP_KERNEL);
571 	if (!skb)
572 		return -ENOMEM;
573 
574 	trace_devlink_hwmsg(priv_to_devlink(mlxsw_core), false, 0,
575 			    skb->data + mlxsw_core->driver->txhdr_len,
576 			    skb->len - mlxsw_core->driver->txhdr_len);
577 
578 	atomic_set(&trans->active, 1);
579 	err = mlxsw_core_skb_transmit(mlxsw_core, skb, &trans->tx_info);
580 	if (err) {
581 		dev_kfree_skb(skb);
582 		return err;
583 	}
584 	mlxsw_emad_trans_timeout_schedule(trans);
585 	return 0;
586 }
587 
588 static void mlxsw_emad_trans_finish(struct mlxsw_reg_trans *trans, int err)
589 {
590 	struct mlxsw_core *mlxsw_core = trans->core;
591 
592 	dev_kfree_skb(trans->tx_skb);
593 	spin_lock_bh(&mlxsw_core->emad.trans_list_lock);
594 	list_del_rcu(&trans->list);
595 	spin_unlock_bh(&mlxsw_core->emad.trans_list_lock);
596 	trans->err = err;
597 	complete(&trans->completion);
598 }
599 
600 static void mlxsw_emad_transmit_retry(struct mlxsw_core *mlxsw_core,
601 				      struct mlxsw_reg_trans *trans)
602 {
603 	int err;
604 
605 	if (trans->retries < MLXSW_EMAD_MAX_RETRY) {
606 		trans->retries++;
607 		err = mlxsw_emad_transmit(trans->core, trans);
608 		if (err == 0)
609 			return;
610 	} else {
611 		err = -EIO;
612 	}
613 	mlxsw_emad_trans_finish(trans, err);
614 }
615 
616 static void mlxsw_emad_trans_timeout_work(struct work_struct *work)
617 {
618 	struct mlxsw_reg_trans *trans = container_of(work,
619 						     struct mlxsw_reg_trans,
620 						     timeout_dw.work);
621 
622 	if (!atomic_dec_and_test(&trans->active))
623 		return;
624 
625 	mlxsw_emad_transmit_retry(trans->core, trans);
626 }
627 
628 static void mlxsw_emad_process_response(struct mlxsw_core *mlxsw_core,
629 					struct mlxsw_reg_trans *trans,
630 					struct sk_buff *skb)
631 {
632 	int err;
633 
634 	if (!atomic_dec_and_test(&trans->active))
635 		return;
636 
637 	err = mlxsw_emad_process_status_skb(skb, &trans->emad_status);
638 	if (err == -EAGAIN) {
639 		mlxsw_emad_transmit_retry(mlxsw_core, trans);
640 	} else {
641 		if (err == 0) {
642 			char *reg_tlv = mlxsw_emad_reg_tlv(skb);
643 
644 			if (trans->cb)
645 				trans->cb(mlxsw_core,
646 					  mlxsw_emad_reg_payload(reg_tlv),
647 					  trans->reg->len, trans->cb_priv);
648 		} else {
649 			mlxsw_emad_process_string_tlv(skb, trans);
650 		}
651 		mlxsw_emad_trans_finish(trans, err);
652 	}
653 }
654 
655 /* called with rcu read lock held */
656 static void mlxsw_emad_rx_listener_func(struct sk_buff *skb, u8 local_port,
657 					void *priv)
658 {
659 	struct mlxsw_core *mlxsw_core = priv;
660 	struct mlxsw_reg_trans *trans;
661 
662 	trace_devlink_hwmsg(priv_to_devlink(mlxsw_core), true, 0,
663 			    skb->data, skb->len);
664 
665 	mlxsw_emad_tlv_parse(skb);
666 
667 	if (!mlxsw_emad_is_resp(skb))
668 		goto free_skb;
669 
670 	list_for_each_entry_rcu(trans, &mlxsw_core->emad.trans_list, list) {
671 		if (mlxsw_emad_get_tid(skb) == trans->tid) {
672 			mlxsw_emad_process_response(mlxsw_core, trans, skb);
673 			break;
674 		}
675 	}
676 
677 free_skb:
678 	dev_kfree_skb(skb);
679 }
680 
681 static const struct mlxsw_listener mlxsw_emad_rx_listener =
682 	MLXSW_RXL(mlxsw_emad_rx_listener_func, ETHEMAD, TRAP_TO_CPU, false,
683 		  EMAD, DISCARD);
684 
685 static int mlxsw_emad_init(struct mlxsw_core *mlxsw_core)
686 {
687 	struct workqueue_struct *emad_wq;
688 	u64 tid;
689 	int err;
690 
691 	if (!(mlxsw_core->bus->features & MLXSW_BUS_F_TXRX))
692 		return 0;
693 
694 	emad_wq = alloc_workqueue("mlxsw_core_emad", 0, 0);
695 	if (!emad_wq)
696 		return -ENOMEM;
697 	mlxsw_core->emad_wq = emad_wq;
698 
699 	/* Set the upper 32 bits of the transaction ID field to a random
700 	 * number. This allows us to discard EMADs addressed to other
701 	 * devices.
702 	 */
703 	get_random_bytes(&tid, 4);
704 	tid <<= 32;
705 	atomic64_set(&mlxsw_core->emad.tid, tid);
706 
707 	INIT_LIST_HEAD(&mlxsw_core->emad.trans_list);
708 	spin_lock_init(&mlxsw_core->emad.trans_list_lock);
709 
710 	err = mlxsw_core_trap_register(mlxsw_core, &mlxsw_emad_rx_listener,
711 				       mlxsw_core);
712 	if (err)
713 		return err;
714 
715 	err = mlxsw_core->driver->basic_trap_groups_set(mlxsw_core);
716 	if (err)
717 		goto err_emad_trap_set;
718 	mlxsw_core->emad.use_emad = true;
719 
720 	return 0;
721 
722 err_emad_trap_set:
723 	mlxsw_core_trap_unregister(mlxsw_core, &mlxsw_emad_rx_listener,
724 				   mlxsw_core);
725 	destroy_workqueue(mlxsw_core->emad_wq);
726 	return err;
727 }
728 
729 static void mlxsw_emad_fini(struct mlxsw_core *mlxsw_core)
730 {
731 
732 	if (!(mlxsw_core->bus->features & MLXSW_BUS_F_TXRX))
733 		return;
734 
735 	mlxsw_core->emad.use_emad = false;
736 	mlxsw_core_trap_unregister(mlxsw_core, &mlxsw_emad_rx_listener,
737 				   mlxsw_core);
738 	destroy_workqueue(mlxsw_core->emad_wq);
739 }
740 
741 static struct sk_buff *mlxsw_emad_alloc(const struct mlxsw_core *mlxsw_core,
742 					u16 reg_len, bool enable_string_tlv)
743 {
744 	struct sk_buff *skb;
745 	u16 emad_len;
746 
747 	emad_len = (reg_len + sizeof(u32) + MLXSW_EMAD_ETH_HDR_LEN +
748 		    (MLXSW_EMAD_OP_TLV_LEN + MLXSW_EMAD_END_TLV_LEN) *
749 		    sizeof(u32) + mlxsw_core->driver->txhdr_len);
750 	if (enable_string_tlv)
751 		emad_len += MLXSW_EMAD_STRING_TLV_LEN * sizeof(u32);
752 	if (emad_len > MLXSW_EMAD_MAX_FRAME_LEN)
753 		return NULL;
754 
755 	skb = netdev_alloc_skb(NULL, emad_len);
756 	if (!skb)
757 		return NULL;
758 	memset(skb->data, 0, emad_len);
759 	skb_reserve(skb, emad_len);
760 
761 	return skb;
762 }
763 
764 static int mlxsw_emad_reg_access(struct mlxsw_core *mlxsw_core,
765 				 const struct mlxsw_reg_info *reg,
766 				 char *payload,
767 				 enum mlxsw_core_reg_access_type type,
768 				 struct mlxsw_reg_trans *trans,
769 				 struct list_head *bulk_list,
770 				 mlxsw_reg_trans_cb_t *cb,
771 				 unsigned long cb_priv, u64 tid)
772 {
773 	bool enable_string_tlv;
774 	struct sk_buff *skb;
775 	int err;
776 
777 	dev_dbg(mlxsw_core->bus_info->dev, "EMAD reg access (tid=%llx,reg_id=%x(%s),type=%s)\n",
778 		tid, reg->id, mlxsw_reg_id_str(reg->id),
779 		mlxsw_core_reg_access_type_str(type));
780 
781 	/* Since this can be changed during emad_reg_access, read it once and
782 	 * use the value all the way.
783 	 */
784 	enable_string_tlv = mlxsw_core->emad.enable_string_tlv;
785 
786 	skb = mlxsw_emad_alloc(mlxsw_core, reg->len, enable_string_tlv);
787 	if (!skb)
788 		return -ENOMEM;
789 
790 	list_add_tail(&trans->bulk_list, bulk_list);
791 	trans->core = mlxsw_core;
792 	trans->tx_skb = skb;
793 	trans->tx_info.local_port = MLXSW_PORT_CPU_PORT;
794 	trans->tx_info.is_emad = true;
795 	INIT_DELAYED_WORK(&trans->timeout_dw, mlxsw_emad_trans_timeout_work);
796 	trans->tid = tid;
797 	init_completion(&trans->completion);
798 	trans->cb = cb;
799 	trans->cb_priv = cb_priv;
800 	trans->reg = reg;
801 	trans->type = type;
802 
803 	mlxsw_emad_construct(skb, reg, payload, type, trans->tid,
804 			     enable_string_tlv);
805 	mlxsw_core->driver->txhdr_construct(skb, &trans->tx_info);
806 
807 	spin_lock_bh(&mlxsw_core->emad.trans_list_lock);
808 	list_add_tail_rcu(&trans->list, &mlxsw_core->emad.trans_list);
809 	spin_unlock_bh(&mlxsw_core->emad.trans_list_lock);
810 	err = mlxsw_emad_transmit(mlxsw_core, trans);
811 	if (err)
812 		goto err_out;
813 	return 0;
814 
815 err_out:
816 	spin_lock_bh(&mlxsw_core->emad.trans_list_lock);
817 	list_del_rcu(&trans->list);
818 	spin_unlock_bh(&mlxsw_core->emad.trans_list_lock);
819 	list_del(&trans->bulk_list);
820 	dev_kfree_skb(trans->tx_skb);
821 	return err;
822 }
823 
824 /*****************
825  * Core functions
826  *****************/
827 
828 int mlxsw_core_driver_register(struct mlxsw_driver *mlxsw_driver)
829 {
830 	spin_lock(&mlxsw_core_driver_list_lock);
831 	list_add_tail(&mlxsw_driver->list, &mlxsw_core_driver_list);
832 	spin_unlock(&mlxsw_core_driver_list_lock);
833 	return 0;
834 }
835 EXPORT_SYMBOL(mlxsw_core_driver_register);
836 
837 void mlxsw_core_driver_unregister(struct mlxsw_driver *mlxsw_driver)
838 {
839 	spin_lock(&mlxsw_core_driver_list_lock);
840 	list_del(&mlxsw_driver->list);
841 	spin_unlock(&mlxsw_core_driver_list_lock);
842 }
843 EXPORT_SYMBOL(mlxsw_core_driver_unregister);
844 
845 static struct mlxsw_driver *__driver_find(const char *kind)
846 {
847 	struct mlxsw_driver *mlxsw_driver;
848 
849 	list_for_each_entry(mlxsw_driver, &mlxsw_core_driver_list, list) {
850 		if (strcmp(mlxsw_driver->kind, kind) == 0)
851 			return mlxsw_driver;
852 	}
853 	return NULL;
854 }
855 
856 static struct mlxsw_driver *mlxsw_core_driver_get(const char *kind)
857 {
858 	struct mlxsw_driver *mlxsw_driver;
859 
860 	spin_lock(&mlxsw_core_driver_list_lock);
861 	mlxsw_driver = __driver_find(kind);
862 	spin_unlock(&mlxsw_core_driver_list_lock);
863 	return mlxsw_driver;
864 }
865 
866 static int mlxsw_devlink_port_split(struct devlink *devlink,
867 				    unsigned int port_index,
868 				    unsigned int count,
869 				    struct netlink_ext_ack *extack)
870 {
871 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
872 
873 	if (port_index >= mlxsw_core->max_ports) {
874 		NL_SET_ERR_MSG_MOD(extack, "Port index exceeds maximum number of ports");
875 		return -EINVAL;
876 	}
877 	if (!mlxsw_core->driver->port_split)
878 		return -EOPNOTSUPP;
879 	return mlxsw_core->driver->port_split(mlxsw_core, port_index, count,
880 					      extack);
881 }
882 
883 static int mlxsw_devlink_port_unsplit(struct devlink *devlink,
884 				      unsigned int port_index,
885 				      struct netlink_ext_ack *extack)
886 {
887 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
888 
889 	if (port_index >= mlxsw_core->max_ports) {
890 		NL_SET_ERR_MSG_MOD(extack, "Port index exceeds maximum number of ports");
891 		return -EINVAL;
892 	}
893 	if (!mlxsw_core->driver->port_unsplit)
894 		return -EOPNOTSUPP;
895 	return mlxsw_core->driver->port_unsplit(mlxsw_core, port_index,
896 						extack);
897 }
898 
899 static int
900 mlxsw_devlink_sb_pool_get(struct devlink *devlink,
901 			  unsigned int sb_index, u16 pool_index,
902 			  struct devlink_sb_pool_info *pool_info)
903 {
904 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
905 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
906 
907 	if (!mlxsw_driver->sb_pool_get)
908 		return -EOPNOTSUPP;
909 	return mlxsw_driver->sb_pool_get(mlxsw_core, sb_index,
910 					 pool_index, pool_info);
911 }
912 
913 static int
914 mlxsw_devlink_sb_pool_set(struct devlink *devlink,
915 			  unsigned int sb_index, u16 pool_index, u32 size,
916 			  enum devlink_sb_threshold_type threshold_type,
917 			  struct netlink_ext_ack *extack)
918 {
919 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
920 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
921 
922 	if (!mlxsw_driver->sb_pool_set)
923 		return -EOPNOTSUPP;
924 	return mlxsw_driver->sb_pool_set(mlxsw_core, sb_index,
925 					 pool_index, size, threshold_type,
926 					 extack);
927 }
928 
929 static void *__dl_port(struct devlink_port *devlink_port)
930 {
931 	return container_of(devlink_port, struct mlxsw_core_port, devlink_port);
932 }
933 
934 static int mlxsw_devlink_port_type_set(struct devlink_port *devlink_port,
935 				       enum devlink_port_type port_type)
936 {
937 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink_port->devlink);
938 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
939 	struct mlxsw_core_port *mlxsw_core_port = __dl_port(devlink_port);
940 
941 	if (!mlxsw_driver->port_type_set)
942 		return -EOPNOTSUPP;
943 
944 	return mlxsw_driver->port_type_set(mlxsw_core,
945 					   mlxsw_core_port->local_port,
946 					   port_type);
947 }
948 
949 static int mlxsw_devlink_sb_port_pool_get(struct devlink_port *devlink_port,
950 					  unsigned int sb_index, u16 pool_index,
951 					  u32 *p_threshold)
952 {
953 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink_port->devlink);
954 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
955 	struct mlxsw_core_port *mlxsw_core_port = __dl_port(devlink_port);
956 
957 	if (!mlxsw_driver->sb_port_pool_get ||
958 	    !mlxsw_core_port_check(mlxsw_core_port))
959 		return -EOPNOTSUPP;
960 	return mlxsw_driver->sb_port_pool_get(mlxsw_core_port, sb_index,
961 					      pool_index, p_threshold);
962 }
963 
964 static int mlxsw_devlink_sb_port_pool_set(struct devlink_port *devlink_port,
965 					  unsigned int sb_index, u16 pool_index,
966 					  u32 threshold,
967 					  struct netlink_ext_ack *extack)
968 {
969 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink_port->devlink);
970 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
971 	struct mlxsw_core_port *mlxsw_core_port = __dl_port(devlink_port);
972 
973 	if (!mlxsw_driver->sb_port_pool_set ||
974 	    !mlxsw_core_port_check(mlxsw_core_port))
975 		return -EOPNOTSUPP;
976 	return mlxsw_driver->sb_port_pool_set(mlxsw_core_port, sb_index,
977 					      pool_index, threshold, extack);
978 }
979 
980 static int
981 mlxsw_devlink_sb_tc_pool_bind_get(struct devlink_port *devlink_port,
982 				  unsigned int sb_index, u16 tc_index,
983 				  enum devlink_sb_pool_type pool_type,
984 				  u16 *p_pool_index, u32 *p_threshold)
985 {
986 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink_port->devlink);
987 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
988 	struct mlxsw_core_port *mlxsw_core_port = __dl_port(devlink_port);
989 
990 	if (!mlxsw_driver->sb_tc_pool_bind_get ||
991 	    !mlxsw_core_port_check(mlxsw_core_port))
992 		return -EOPNOTSUPP;
993 	return mlxsw_driver->sb_tc_pool_bind_get(mlxsw_core_port, sb_index,
994 						 tc_index, pool_type,
995 						 p_pool_index, p_threshold);
996 }
997 
998 static int
999 mlxsw_devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
1000 				  unsigned int sb_index, u16 tc_index,
1001 				  enum devlink_sb_pool_type pool_type,
1002 				  u16 pool_index, u32 threshold,
1003 				  struct netlink_ext_ack *extack)
1004 {
1005 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink_port->devlink);
1006 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1007 	struct mlxsw_core_port *mlxsw_core_port = __dl_port(devlink_port);
1008 
1009 	if (!mlxsw_driver->sb_tc_pool_bind_set ||
1010 	    !mlxsw_core_port_check(mlxsw_core_port))
1011 		return -EOPNOTSUPP;
1012 	return mlxsw_driver->sb_tc_pool_bind_set(mlxsw_core_port, sb_index,
1013 						 tc_index, pool_type,
1014 						 pool_index, threshold, extack);
1015 }
1016 
1017 static int mlxsw_devlink_sb_occ_snapshot(struct devlink *devlink,
1018 					 unsigned int sb_index)
1019 {
1020 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1021 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1022 
1023 	if (!mlxsw_driver->sb_occ_snapshot)
1024 		return -EOPNOTSUPP;
1025 	return mlxsw_driver->sb_occ_snapshot(mlxsw_core, sb_index);
1026 }
1027 
1028 static int mlxsw_devlink_sb_occ_max_clear(struct devlink *devlink,
1029 					  unsigned int sb_index)
1030 {
1031 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1032 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1033 
1034 	if (!mlxsw_driver->sb_occ_max_clear)
1035 		return -EOPNOTSUPP;
1036 	return mlxsw_driver->sb_occ_max_clear(mlxsw_core, sb_index);
1037 }
1038 
1039 static int
1040 mlxsw_devlink_sb_occ_port_pool_get(struct devlink_port *devlink_port,
1041 				   unsigned int sb_index, u16 pool_index,
1042 				   u32 *p_cur, u32 *p_max)
1043 {
1044 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink_port->devlink);
1045 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1046 	struct mlxsw_core_port *mlxsw_core_port = __dl_port(devlink_port);
1047 
1048 	if (!mlxsw_driver->sb_occ_port_pool_get ||
1049 	    !mlxsw_core_port_check(mlxsw_core_port))
1050 		return -EOPNOTSUPP;
1051 	return mlxsw_driver->sb_occ_port_pool_get(mlxsw_core_port, sb_index,
1052 						  pool_index, p_cur, p_max);
1053 }
1054 
1055 static int
1056 mlxsw_devlink_sb_occ_tc_port_bind_get(struct devlink_port *devlink_port,
1057 				      unsigned int sb_index, u16 tc_index,
1058 				      enum devlink_sb_pool_type pool_type,
1059 				      u32 *p_cur, u32 *p_max)
1060 {
1061 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink_port->devlink);
1062 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1063 	struct mlxsw_core_port *mlxsw_core_port = __dl_port(devlink_port);
1064 
1065 	if (!mlxsw_driver->sb_occ_tc_port_bind_get ||
1066 	    !mlxsw_core_port_check(mlxsw_core_port))
1067 		return -EOPNOTSUPP;
1068 	return mlxsw_driver->sb_occ_tc_port_bind_get(mlxsw_core_port,
1069 						     sb_index, tc_index,
1070 						     pool_type, p_cur, p_max);
1071 }
1072 
1073 static int
1074 mlxsw_devlink_info_get(struct devlink *devlink, struct devlink_info_req *req,
1075 		       struct netlink_ext_ack *extack)
1076 {
1077 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1078 	char fw_info_psid[MLXSW_REG_MGIR_FW_INFO_PSID_SIZE];
1079 	u32 hw_rev, fw_major, fw_minor, fw_sub_minor;
1080 	char mgir_pl[MLXSW_REG_MGIR_LEN];
1081 	char buf[32];
1082 	int err;
1083 
1084 	err = devlink_info_driver_name_put(req,
1085 					   mlxsw_core->bus_info->device_kind);
1086 	if (err)
1087 		return err;
1088 
1089 	mlxsw_reg_mgir_pack(mgir_pl);
1090 	err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(mgir), mgir_pl);
1091 	if (err)
1092 		return err;
1093 	mlxsw_reg_mgir_unpack(mgir_pl, &hw_rev, fw_info_psid, &fw_major,
1094 			      &fw_minor, &fw_sub_minor);
1095 
1096 	sprintf(buf, "%X", hw_rev);
1097 	err = devlink_info_version_fixed_put(req, "hw.revision", buf);
1098 	if (err)
1099 		return err;
1100 
1101 	err = devlink_info_version_fixed_put(req, "fw.psid", fw_info_psid);
1102 	if (err)
1103 		return err;
1104 
1105 	sprintf(buf, "%d.%d.%d", fw_major, fw_minor, fw_sub_minor);
1106 	err = devlink_info_version_running_put(req, "fw.version", buf);
1107 	if (err)
1108 		return err;
1109 
1110 	return 0;
1111 }
1112 
1113 static int
1114 mlxsw_devlink_core_bus_device_reload_down(struct devlink *devlink,
1115 					  bool netns_change,
1116 					  struct netlink_ext_ack *extack)
1117 {
1118 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1119 
1120 	if (!(mlxsw_core->bus->features & MLXSW_BUS_F_RESET))
1121 		return -EOPNOTSUPP;
1122 
1123 	mlxsw_core_bus_device_unregister(mlxsw_core, true);
1124 	return 0;
1125 }
1126 
1127 static int
1128 mlxsw_devlink_core_bus_device_reload_up(struct devlink *devlink,
1129 					struct netlink_ext_ack *extack)
1130 {
1131 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1132 
1133 	return mlxsw_core_bus_device_register(mlxsw_core->bus_info,
1134 					      mlxsw_core->bus,
1135 					      mlxsw_core->bus_priv, true,
1136 					      devlink, extack);
1137 }
1138 
1139 static int mlxsw_devlink_flash_update(struct devlink *devlink,
1140 				      const char *file_name,
1141 				      const char *component,
1142 				      struct netlink_ext_ack *extack)
1143 {
1144 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1145 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1146 
1147 	if (!mlxsw_driver->flash_update)
1148 		return -EOPNOTSUPP;
1149 	return mlxsw_driver->flash_update(mlxsw_core, file_name,
1150 					  component, extack);
1151 }
1152 
1153 static int mlxsw_devlink_trap_init(struct devlink *devlink,
1154 				   const struct devlink_trap *trap,
1155 				   void *trap_ctx)
1156 {
1157 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1158 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1159 
1160 	if (!mlxsw_driver->trap_init)
1161 		return -EOPNOTSUPP;
1162 	return mlxsw_driver->trap_init(mlxsw_core, trap, trap_ctx);
1163 }
1164 
1165 static void mlxsw_devlink_trap_fini(struct devlink *devlink,
1166 				    const struct devlink_trap *trap,
1167 				    void *trap_ctx)
1168 {
1169 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1170 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1171 
1172 	if (!mlxsw_driver->trap_fini)
1173 		return;
1174 	mlxsw_driver->trap_fini(mlxsw_core, trap, trap_ctx);
1175 }
1176 
1177 static int mlxsw_devlink_trap_action_set(struct devlink *devlink,
1178 					 const struct devlink_trap *trap,
1179 					 enum devlink_trap_action action)
1180 {
1181 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1182 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1183 
1184 	if (!mlxsw_driver->trap_action_set)
1185 		return -EOPNOTSUPP;
1186 	return mlxsw_driver->trap_action_set(mlxsw_core, trap, action);
1187 }
1188 
1189 static int
1190 mlxsw_devlink_trap_group_init(struct devlink *devlink,
1191 			      const struct devlink_trap_group *group)
1192 {
1193 	struct mlxsw_core *mlxsw_core = devlink_priv(devlink);
1194 	struct mlxsw_driver *mlxsw_driver = mlxsw_core->driver;
1195 
1196 	if (!mlxsw_driver->trap_group_init)
1197 		return -EOPNOTSUPP;
1198 	return mlxsw_driver->trap_group_init(mlxsw_core, group);
1199 }
1200 
1201 static const struct devlink_ops mlxsw_devlink_ops = {
1202 	.reload_down		= mlxsw_devlink_core_bus_device_reload_down,
1203 	.reload_up		= mlxsw_devlink_core_bus_device_reload_up,
1204 	.port_type_set			= mlxsw_devlink_port_type_set,
1205 	.port_split			= mlxsw_devlink_port_split,
1206 	.port_unsplit			= mlxsw_devlink_port_unsplit,
1207 	.sb_pool_get			= mlxsw_devlink_sb_pool_get,
1208 	.sb_pool_set			= mlxsw_devlink_sb_pool_set,
1209 	.sb_port_pool_get		= mlxsw_devlink_sb_port_pool_get,
1210 	.sb_port_pool_set		= mlxsw_devlink_sb_port_pool_set,
1211 	.sb_tc_pool_bind_get		= mlxsw_devlink_sb_tc_pool_bind_get,
1212 	.sb_tc_pool_bind_set		= mlxsw_devlink_sb_tc_pool_bind_set,
1213 	.sb_occ_snapshot		= mlxsw_devlink_sb_occ_snapshot,
1214 	.sb_occ_max_clear		= mlxsw_devlink_sb_occ_max_clear,
1215 	.sb_occ_port_pool_get		= mlxsw_devlink_sb_occ_port_pool_get,
1216 	.sb_occ_tc_port_bind_get	= mlxsw_devlink_sb_occ_tc_port_bind_get,
1217 	.info_get			= mlxsw_devlink_info_get,
1218 	.flash_update			= mlxsw_devlink_flash_update,
1219 	.trap_init			= mlxsw_devlink_trap_init,
1220 	.trap_fini			= mlxsw_devlink_trap_fini,
1221 	.trap_action_set		= mlxsw_devlink_trap_action_set,
1222 	.trap_group_init		= mlxsw_devlink_trap_group_init,
1223 };
1224 
1225 static int
1226 __mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info,
1227 				 const struct mlxsw_bus *mlxsw_bus,
1228 				 void *bus_priv, bool reload,
1229 				 struct devlink *devlink,
1230 				 struct netlink_ext_ack *extack)
1231 {
1232 	const char *device_kind = mlxsw_bus_info->device_kind;
1233 	struct mlxsw_core *mlxsw_core;
1234 	struct mlxsw_driver *mlxsw_driver;
1235 	struct mlxsw_res *res;
1236 	size_t alloc_size;
1237 	int err;
1238 
1239 	mlxsw_driver = mlxsw_core_driver_get(device_kind);
1240 	if (!mlxsw_driver)
1241 		return -EINVAL;
1242 
1243 	if (!reload) {
1244 		alloc_size = sizeof(*mlxsw_core) + mlxsw_driver->priv_size;
1245 		devlink = devlink_alloc(&mlxsw_devlink_ops, alloc_size);
1246 		if (!devlink) {
1247 			err = -ENOMEM;
1248 			goto err_devlink_alloc;
1249 		}
1250 	}
1251 
1252 	mlxsw_core = devlink_priv(devlink);
1253 	INIT_LIST_HEAD(&mlxsw_core->rx_listener_list);
1254 	INIT_LIST_HEAD(&mlxsw_core->event_listener_list);
1255 	mlxsw_core->driver = mlxsw_driver;
1256 	mlxsw_core->bus = mlxsw_bus;
1257 	mlxsw_core->bus_priv = bus_priv;
1258 	mlxsw_core->bus_info = mlxsw_bus_info;
1259 
1260 	res = mlxsw_driver->res_query_enabled ? &mlxsw_core->res : NULL;
1261 	err = mlxsw_bus->init(bus_priv, mlxsw_core, mlxsw_driver->profile, res);
1262 	if (err)
1263 		goto err_bus_init;
1264 
1265 	if (mlxsw_driver->resources_register && !reload) {
1266 		err = mlxsw_driver->resources_register(mlxsw_core);
1267 		if (err)
1268 			goto err_register_resources;
1269 	}
1270 
1271 	err = mlxsw_ports_init(mlxsw_core);
1272 	if (err)
1273 		goto err_ports_init;
1274 
1275 	if (MLXSW_CORE_RES_VALID(mlxsw_core, MAX_LAG) &&
1276 	    MLXSW_CORE_RES_VALID(mlxsw_core, MAX_LAG_MEMBERS)) {
1277 		alloc_size = sizeof(u8) *
1278 			MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG) *
1279 			MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG_MEMBERS);
1280 		mlxsw_core->lag.mapping = kzalloc(alloc_size, GFP_KERNEL);
1281 		if (!mlxsw_core->lag.mapping) {
1282 			err = -ENOMEM;
1283 			goto err_alloc_lag_mapping;
1284 		}
1285 	}
1286 
1287 	err = mlxsw_emad_init(mlxsw_core);
1288 	if (err)
1289 		goto err_emad_init;
1290 
1291 	if (!reload) {
1292 		err = devlink_register(devlink, mlxsw_bus_info->dev);
1293 		if (err)
1294 			goto err_devlink_register;
1295 	}
1296 
1297 	if (mlxsw_driver->params_register && !reload) {
1298 		err = mlxsw_driver->params_register(mlxsw_core);
1299 		if (err)
1300 			goto err_register_params;
1301 	}
1302 
1303 	if (mlxsw_driver->init) {
1304 		err = mlxsw_driver->init(mlxsw_core, mlxsw_bus_info, extack);
1305 		if (err)
1306 			goto err_driver_init;
1307 	}
1308 
1309 	err = mlxsw_hwmon_init(mlxsw_core, mlxsw_bus_info, &mlxsw_core->hwmon);
1310 	if (err)
1311 		goto err_hwmon_init;
1312 
1313 	err = mlxsw_thermal_init(mlxsw_core, mlxsw_bus_info,
1314 				 &mlxsw_core->thermal);
1315 	if (err)
1316 		goto err_thermal_init;
1317 
1318 	if (mlxsw_driver->params_register)
1319 		devlink_params_publish(devlink);
1320 
1321 	if (!reload)
1322 		devlink_reload_enable(devlink);
1323 
1324 	return 0;
1325 
1326 err_thermal_init:
1327 	mlxsw_hwmon_fini(mlxsw_core->hwmon);
1328 err_hwmon_init:
1329 	if (mlxsw_core->driver->fini)
1330 		mlxsw_core->driver->fini(mlxsw_core);
1331 err_driver_init:
1332 	if (mlxsw_driver->params_unregister && !reload)
1333 		mlxsw_driver->params_unregister(mlxsw_core);
1334 err_register_params:
1335 	if (!reload)
1336 		devlink_unregister(devlink);
1337 err_devlink_register:
1338 	mlxsw_emad_fini(mlxsw_core);
1339 err_emad_init:
1340 	kfree(mlxsw_core->lag.mapping);
1341 err_alloc_lag_mapping:
1342 	mlxsw_ports_fini(mlxsw_core);
1343 err_ports_init:
1344 	if (!reload)
1345 		devlink_resources_unregister(devlink, NULL);
1346 err_register_resources:
1347 	mlxsw_bus->fini(bus_priv);
1348 err_bus_init:
1349 	if (!reload)
1350 		devlink_free(devlink);
1351 err_devlink_alloc:
1352 	return err;
1353 }
1354 
1355 int mlxsw_core_bus_device_register(const struct mlxsw_bus_info *mlxsw_bus_info,
1356 				   const struct mlxsw_bus *mlxsw_bus,
1357 				   void *bus_priv, bool reload,
1358 				   struct devlink *devlink,
1359 				   struct netlink_ext_ack *extack)
1360 {
1361 	bool called_again = false;
1362 	int err;
1363 
1364 again:
1365 	err = __mlxsw_core_bus_device_register(mlxsw_bus_info, mlxsw_bus,
1366 					       bus_priv, reload,
1367 					       devlink, extack);
1368 	/* -EAGAIN is returned in case the FW was updated. FW needs
1369 	 * a reset, so lets try to call __mlxsw_core_bus_device_register()
1370 	 * again.
1371 	 */
1372 	if (err == -EAGAIN && !called_again) {
1373 		called_again = true;
1374 		goto again;
1375 	}
1376 
1377 	return err;
1378 }
1379 EXPORT_SYMBOL(mlxsw_core_bus_device_register);
1380 
1381 void mlxsw_core_bus_device_unregister(struct mlxsw_core *mlxsw_core,
1382 				      bool reload)
1383 {
1384 	struct devlink *devlink = priv_to_devlink(mlxsw_core);
1385 
1386 	if (!reload)
1387 		devlink_reload_disable(devlink);
1388 	if (devlink_is_reload_failed(devlink)) {
1389 		if (!reload)
1390 			/* Only the parts that were not de-initialized in the
1391 			 * failed reload attempt need to be de-initialized.
1392 			 */
1393 			goto reload_fail_deinit;
1394 		else
1395 			return;
1396 	}
1397 
1398 	if (mlxsw_core->driver->params_unregister)
1399 		devlink_params_unpublish(devlink);
1400 	mlxsw_thermal_fini(mlxsw_core->thermal);
1401 	mlxsw_hwmon_fini(mlxsw_core->hwmon);
1402 	if (mlxsw_core->driver->fini)
1403 		mlxsw_core->driver->fini(mlxsw_core);
1404 	if (mlxsw_core->driver->params_unregister && !reload)
1405 		mlxsw_core->driver->params_unregister(mlxsw_core);
1406 	if (!reload)
1407 		devlink_unregister(devlink);
1408 	mlxsw_emad_fini(mlxsw_core);
1409 	kfree(mlxsw_core->lag.mapping);
1410 	mlxsw_ports_fini(mlxsw_core);
1411 	if (!reload)
1412 		devlink_resources_unregister(devlink, NULL);
1413 	mlxsw_core->bus->fini(mlxsw_core->bus_priv);
1414 
1415 	return;
1416 
1417 reload_fail_deinit:
1418 	if (mlxsw_core->driver->params_unregister)
1419 		mlxsw_core->driver->params_unregister(mlxsw_core);
1420 	devlink_unregister(devlink);
1421 	devlink_resources_unregister(devlink, NULL);
1422 	devlink_free(devlink);
1423 }
1424 EXPORT_SYMBOL(mlxsw_core_bus_device_unregister);
1425 
1426 bool mlxsw_core_skb_transmit_busy(struct mlxsw_core *mlxsw_core,
1427 				  const struct mlxsw_tx_info *tx_info)
1428 {
1429 	return mlxsw_core->bus->skb_transmit_busy(mlxsw_core->bus_priv,
1430 						  tx_info);
1431 }
1432 EXPORT_SYMBOL(mlxsw_core_skb_transmit_busy);
1433 
1434 int mlxsw_core_skb_transmit(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
1435 			    const struct mlxsw_tx_info *tx_info)
1436 {
1437 	return mlxsw_core->bus->skb_transmit(mlxsw_core->bus_priv, skb,
1438 					     tx_info);
1439 }
1440 EXPORT_SYMBOL(mlxsw_core_skb_transmit);
1441 
1442 void mlxsw_core_ptp_transmitted(struct mlxsw_core *mlxsw_core,
1443 				struct sk_buff *skb, u8 local_port)
1444 {
1445 	if (mlxsw_core->driver->ptp_transmitted)
1446 		mlxsw_core->driver->ptp_transmitted(mlxsw_core, skb,
1447 						    local_port);
1448 }
1449 EXPORT_SYMBOL(mlxsw_core_ptp_transmitted);
1450 
1451 static bool __is_rx_listener_equal(const struct mlxsw_rx_listener *rxl_a,
1452 				   const struct mlxsw_rx_listener *rxl_b)
1453 {
1454 	return (rxl_a->func == rxl_b->func &&
1455 		rxl_a->local_port == rxl_b->local_port &&
1456 		rxl_a->trap_id == rxl_b->trap_id);
1457 }
1458 
1459 static struct mlxsw_rx_listener_item *
1460 __find_rx_listener_item(struct mlxsw_core *mlxsw_core,
1461 			const struct mlxsw_rx_listener *rxl)
1462 {
1463 	struct mlxsw_rx_listener_item *rxl_item;
1464 
1465 	list_for_each_entry(rxl_item, &mlxsw_core->rx_listener_list, list) {
1466 		if (__is_rx_listener_equal(&rxl_item->rxl, rxl))
1467 			return rxl_item;
1468 	}
1469 	return NULL;
1470 }
1471 
1472 int mlxsw_core_rx_listener_register(struct mlxsw_core *mlxsw_core,
1473 				    const struct mlxsw_rx_listener *rxl,
1474 				    void *priv, bool enabled)
1475 {
1476 	struct mlxsw_rx_listener_item *rxl_item;
1477 
1478 	rxl_item = __find_rx_listener_item(mlxsw_core, rxl);
1479 	if (rxl_item)
1480 		return -EEXIST;
1481 	rxl_item = kmalloc(sizeof(*rxl_item), GFP_KERNEL);
1482 	if (!rxl_item)
1483 		return -ENOMEM;
1484 	rxl_item->rxl = *rxl;
1485 	rxl_item->priv = priv;
1486 	rxl_item->enabled = enabled;
1487 
1488 	list_add_rcu(&rxl_item->list, &mlxsw_core->rx_listener_list);
1489 	return 0;
1490 }
1491 EXPORT_SYMBOL(mlxsw_core_rx_listener_register);
1492 
1493 void mlxsw_core_rx_listener_unregister(struct mlxsw_core *mlxsw_core,
1494 				       const struct mlxsw_rx_listener *rxl)
1495 {
1496 	struct mlxsw_rx_listener_item *rxl_item;
1497 
1498 	rxl_item = __find_rx_listener_item(mlxsw_core, rxl);
1499 	if (!rxl_item)
1500 		return;
1501 	list_del_rcu(&rxl_item->list);
1502 	synchronize_rcu();
1503 	kfree(rxl_item);
1504 }
1505 EXPORT_SYMBOL(mlxsw_core_rx_listener_unregister);
1506 
1507 static void
1508 mlxsw_core_rx_listener_state_set(struct mlxsw_core *mlxsw_core,
1509 				 const struct mlxsw_rx_listener *rxl,
1510 				 bool enabled)
1511 {
1512 	struct mlxsw_rx_listener_item *rxl_item;
1513 
1514 	rxl_item = __find_rx_listener_item(mlxsw_core, rxl);
1515 	if (WARN_ON(!rxl_item))
1516 		return;
1517 	rxl_item->enabled = enabled;
1518 }
1519 
1520 static void mlxsw_core_event_listener_func(struct sk_buff *skb, u8 local_port,
1521 					   void *priv)
1522 {
1523 	struct mlxsw_event_listener_item *event_listener_item = priv;
1524 	struct mlxsw_reg_info reg;
1525 	char *payload;
1526 	char *reg_tlv;
1527 	char *op_tlv;
1528 
1529 	mlxsw_emad_tlv_parse(skb);
1530 	op_tlv = mlxsw_emad_op_tlv(skb);
1531 	reg_tlv = mlxsw_emad_reg_tlv(skb);
1532 
1533 	reg.id = mlxsw_emad_op_tlv_register_id_get(op_tlv);
1534 	reg.len = (mlxsw_emad_reg_tlv_len_get(reg_tlv) - 1) * sizeof(u32);
1535 	payload = mlxsw_emad_reg_payload(reg_tlv);
1536 	event_listener_item->el.func(&reg, payload, event_listener_item->priv);
1537 	dev_kfree_skb(skb);
1538 }
1539 
1540 static bool __is_event_listener_equal(const struct mlxsw_event_listener *el_a,
1541 				      const struct mlxsw_event_listener *el_b)
1542 {
1543 	return (el_a->func == el_b->func &&
1544 		el_a->trap_id == el_b->trap_id);
1545 }
1546 
1547 static struct mlxsw_event_listener_item *
1548 __find_event_listener_item(struct mlxsw_core *mlxsw_core,
1549 			   const struct mlxsw_event_listener *el)
1550 {
1551 	struct mlxsw_event_listener_item *el_item;
1552 
1553 	list_for_each_entry(el_item, &mlxsw_core->event_listener_list, list) {
1554 		if (__is_event_listener_equal(&el_item->el, el))
1555 			return el_item;
1556 	}
1557 	return NULL;
1558 }
1559 
1560 int mlxsw_core_event_listener_register(struct mlxsw_core *mlxsw_core,
1561 				       const struct mlxsw_event_listener *el,
1562 				       void *priv)
1563 {
1564 	int err;
1565 	struct mlxsw_event_listener_item *el_item;
1566 	const struct mlxsw_rx_listener rxl = {
1567 		.func = mlxsw_core_event_listener_func,
1568 		.local_port = MLXSW_PORT_DONT_CARE,
1569 		.trap_id = el->trap_id,
1570 	};
1571 
1572 	el_item = __find_event_listener_item(mlxsw_core, el);
1573 	if (el_item)
1574 		return -EEXIST;
1575 	el_item = kmalloc(sizeof(*el_item), GFP_KERNEL);
1576 	if (!el_item)
1577 		return -ENOMEM;
1578 	el_item->el = *el;
1579 	el_item->priv = priv;
1580 
1581 	err = mlxsw_core_rx_listener_register(mlxsw_core, &rxl, el_item, true);
1582 	if (err)
1583 		goto err_rx_listener_register;
1584 
1585 	/* No reason to save item if we did not manage to register an RX
1586 	 * listener for it.
1587 	 */
1588 	list_add_rcu(&el_item->list, &mlxsw_core->event_listener_list);
1589 
1590 	return 0;
1591 
1592 err_rx_listener_register:
1593 	kfree(el_item);
1594 	return err;
1595 }
1596 EXPORT_SYMBOL(mlxsw_core_event_listener_register);
1597 
1598 void mlxsw_core_event_listener_unregister(struct mlxsw_core *mlxsw_core,
1599 					  const struct mlxsw_event_listener *el)
1600 {
1601 	struct mlxsw_event_listener_item *el_item;
1602 	const struct mlxsw_rx_listener rxl = {
1603 		.func = mlxsw_core_event_listener_func,
1604 		.local_port = MLXSW_PORT_DONT_CARE,
1605 		.trap_id = el->trap_id,
1606 	};
1607 
1608 	el_item = __find_event_listener_item(mlxsw_core, el);
1609 	if (!el_item)
1610 		return;
1611 	mlxsw_core_rx_listener_unregister(mlxsw_core, &rxl);
1612 	list_del(&el_item->list);
1613 	kfree(el_item);
1614 }
1615 EXPORT_SYMBOL(mlxsw_core_event_listener_unregister);
1616 
1617 static int mlxsw_core_listener_register(struct mlxsw_core *mlxsw_core,
1618 					const struct mlxsw_listener *listener,
1619 					void *priv, bool enabled)
1620 {
1621 	if (listener->is_event) {
1622 		WARN_ON(!enabled);
1623 		return mlxsw_core_event_listener_register(mlxsw_core,
1624 						&listener->event_listener,
1625 						priv);
1626 	} else {
1627 		return mlxsw_core_rx_listener_register(mlxsw_core,
1628 						&listener->rx_listener,
1629 						priv, enabled);
1630 	}
1631 }
1632 
1633 static void mlxsw_core_listener_unregister(struct mlxsw_core *mlxsw_core,
1634 				      const struct mlxsw_listener *listener,
1635 				      void *priv)
1636 {
1637 	if (listener->is_event)
1638 		mlxsw_core_event_listener_unregister(mlxsw_core,
1639 						     &listener->event_listener);
1640 	else
1641 		mlxsw_core_rx_listener_unregister(mlxsw_core,
1642 						  &listener->rx_listener);
1643 }
1644 
1645 int mlxsw_core_trap_register(struct mlxsw_core *mlxsw_core,
1646 			     const struct mlxsw_listener *listener, void *priv)
1647 {
1648 	enum mlxsw_reg_htgt_trap_group trap_group;
1649 	enum mlxsw_reg_hpkt_action action;
1650 	char hpkt_pl[MLXSW_REG_HPKT_LEN];
1651 	int err;
1652 
1653 	err = mlxsw_core_listener_register(mlxsw_core, listener, priv,
1654 					   listener->enabled_on_register);
1655 	if (err)
1656 		return err;
1657 
1658 	action = listener->enabled_on_register ? listener->en_action :
1659 						 listener->dis_action;
1660 	trap_group = listener->enabled_on_register ? listener->en_trap_group :
1661 						     listener->dis_trap_group;
1662 	mlxsw_reg_hpkt_pack(hpkt_pl, action, listener->trap_id,
1663 			    trap_group, listener->is_ctrl);
1664 	err = mlxsw_reg_write(mlxsw_core,  MLXSW_REG(hpkt), hpkt_pl);
1665 	if (err)
1666 		goto err_trap_set;
1667 
1668 	return 0;
1669 
1670 err_trap_set:
1671 	mlxsw_core_listener_unregister(mlxsw_core, listener, priv);
1672 	return err;
1673 }
1674 EXPORT_SYMBOL(mlxsw_core_trap_register);
1675 
1676 void mlxsw_core_trap_unregister(struct mlxsw_core *mlxsw_core,
1677 				const struct mlxsw_listener *listener,
1678 				void *priv)
1679 {
1680 	char hpkt_pl[MLXSW_REG_HPKT_LEN];
1681 
1682 	if (!listener->is_event) {
1683 		mlxsw_reg_hpkt_pack(hpkt_pl, listener->dis_action,
1684 				    listener->trap_id, listener->dis_trap_group,
1685 				    listener->is_ctrl);
1686 		mlxsw_reg_write(mlxsw_core, MLXSW_REG(hpkt), hpkt_pl);
1687 	}
1688 
1689 	mlxsw_core_listener_unregister(mlxsw_core, listener, priv);
1690 }
1691 EXPORT_SYMBOL(mlxsw_core_trap_unregister);
1692 
1693 int mlxsw_core_trap_state_set(struct mlxsw_core *mlxsw_core,
1694 			      const struct mlxsw_listener *listener,
1695 			      bool enabled)
1696 {
1697 	enum mlxsw_reg_htgt_trap_group trap_group;
1698 	enum mlxsw_reg_hpkt_action action;
1699 	char hpkt_pl[MLXSW_REG_HPKT_LEN];
1700 	int err;
1701 
1702 	/* Not supported for event listener */
1703 	if (WARN_ON(listener->is_event))
1704 		return -EINVAL;
1705 
1706 	action = enabled ? listener->en_action : listener->dis_action;
1707 	trap_group = enabled ? listener->en_trap_group :
1708 			       listener->dis_trap_group;
1709 	mlxsw_reg_hpkt_pack(hpkt_pl, action, listener->trap_id,
1710 			    trap_group, listener->is_ctrl);
1711 	err = mlxsw_reg_write(mlxsw_core, MLXSW_REG(hpkt), hpkt_pl);
1712 	if (err)
1713 		return err;
1714 
1715 	mlxsw_core_rx_listener_state_set(mlxsw_core, &listener->rx_listener,
1716 					 enabled);
1717 	return 0;
1718 }
1719 EXPORT_SYMBOL(mlxsw_core_trap_state_set);
1720 
1721 static u64 mlxsw_core_tid_get(struct mlxsw_core *mlxsw_core)
1722 {
1723 	return atomic64_inc_return(&mlxsw_core->emad.tid);
1724 }
1725 
1726 static int mlxsw_core_reg_access_emad(struct mlxsw_core *mlxsw_core,
1727 				      const struct mlxsw_reg_info *reg,
1728 				      char *payload,
1729 				      enum mlxsw_core_reg_access_type type,
1730 				      struct list_head *bulk_list,
1731 				      mlxsw_reg_trans_cb_t *cb,
1732 				      unsigned long cb_priv)
1733 {
1734 	u64 tid = mlxsw_core_tid_get(mlxsw_core);
1735 	struct mlxsw_reg_trans *trans;
1736 	int err;
1737 
1738 	trans = kzalloc(sizeof(*trans), GFP_KERNEL);
1739 	if (!trans)
1740 		return -ENOMEM;
1741 
1742 	err = mlxsw_emad_reg_access(mlxsw_core, reg, payload, type, trans,
1743 				    bulk_list, cb, cb_priv, tid);
1744 	if (err) {
1745 		kfree(trans);
1746 		return err;
1747 	}
1748 	return 0;
1749 }
1750 
1751 int mlxsw_reg_trans_query(struct mlxsw_core *mlxsw_core,
1752 			  const struct mlxsw_reg_info *reg, char *payload,
1753 			  struct list_head *bulk_list,
1754 			  mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv)
1755 {
1756 	return mlxsw_core_reg_access_emad(mlxsw_core, reg, payload,
1757 					  MLXSW_CORE_REG_ACCESS_TYPE_QUERY,
1758 					  bulk_list, cb, cb_priv);
1759 }
1760 EXPORT_SYMBOL(mlxsw_reg_trans_query);
1761 
1762 int mlxsw_reg_trans_write(struct mlxsw_core *mlxsw_core,
1763 			  const struct mlxsw_reg_info *reg, char *payload,
1764 			  struct list_head *bulk_list,
1765 			  mlxsw_reg_trans_cb_t *cb, unsigned long cb_priv)
1766 {
1767 	return mlxsw_core_reg_access_emad(mlxsw_core, reg, payload,
1768 					  MLXSW_CORE_REG_ACCESS_TYPE_WRITE,
1769 					  bulk_list, cb, cb_priv);
1770 }
1771 EXPORT_SYMBOL(mlxsw_reg_trans_write);
1772 
1773 #define MLXSW_REG_TRANS_ERR_STRING_SIZE	256
1774 
1775 static int mlxsw_reg_trans_wait(struct mlxsw_reg_trans *trans)
1776 {
1777 	char err_string[MLXSW_REG_TRANS_ERR_STRING_SIZE];
1778 	struct mlxsw_core *mlxsw_core = trans->core;
1779 	int err;
1780 
1781 	wait_for_completion(&trans->completion);
1782 	cancel_delayed_work_sync(&trans->timeout_dw);
1783 	err = trans->err;
1784 
1785 	if (trans->retries)
1786 		dev_warn(mlxsw_core->bus_info->dev, "EMAD retries (%d/%d) (tid=%llx)\n",
1787 			 trans->retries, MLXSW_EMAD_MAX_RETRY, trans->tid);
1788 	if (err) {
1789 		dev_err(mlxsw_core->bus_info->dev, "EMAD reg access failed (tid=%llx,reg_id=%x(%s),type=%s,status=%x(%s))\n",
1790 			trans->tid, trans->reg->id,
1791 			mlxsw_reg_id_str(trans->reg->id),
1792 			mlxsw_core_reg_access_type_str(trans->type),
1793 			trans->emad_status,
1794 			mlxsw_emad_op_tlv_status_str(trans->emad_status));
1795 
1796 		snprintf(err_string, MLXSW_REG_TRANS_ERR_STRING_SIZE,
1797 			 "(tid=%llx,reg_id=%x(%s)) %s (%s)\n", trans->tid,
1798 			 trans->reg->id, mlxsw_reg_id_str(trans->reg->id),
1799 			 mlxsw_emad_op_tlv_status_str(trans->emad_status),
1800 			 trans->emad_err_string ? trans->emad_err_string : "");
1801 
1802 		trace_devlink_hwerr(priv_to_devlink(mlxsw_core),
1803 				    trans->emad_status, err_string);
1804 
1805 		kfree(trans->emad_err_string);
1806 	}
1807 
1808 	list_del(&trans->bulk_list);
1809 	kfree_rcu(trans, rcu);
1810 	return err;
1811 }
1812 
1813 int mlxsw_reg_trans_bulk_wait(struct list_head *bulk_list)
1814 {
1815 	struct mlxsw_reg_trans *trans;
1816 	struct mlxsw_reg_trans *tmp;
1817 	int sum_err = 0;
1818 	int err;
1819 
1820 	list_for_each_entry_safe(trans, tmp, bulk_list, bulk_list) {
1821 		err = mlxsw_reg_trans_wait(trans);
1822 		if (err && sum_err == 0)
1823 			sum_err = err; /* first error to be returned */
1824 	}
1825 	return sum_err;
1826 }
1827 EXPORT_SYMBOL(mlxsw_reg_trans_bulk_wait);
1828 
1829 static int mlxsw_core_reg_access_cmd(struct mlxsw_core *mlxsw_core,
1830 				     const struct mlxsw_reg_info *reg,
1831 				     char *payload,
1832 				     enum mlxsw_core_reg_access_type type)
1833 {
1834 	enum mlxsw_emad_op_tlv_status status;
1835 	int err, n_retry;
1836 	bool reset_ok;
1837 	char *in_mbox, *out_mbox, *tmp;
1838 
1839 	dev_dbg(mlxsw_core->bus_info->dev, "Reg cmd access (reg_id=%x(%s),type=%s)\n",
1840 		reg->id, mlxsw_reg_id_str(reg->id),
1841 		mlxsw_core_reg_access_type_str(type));
1842 
1843 	in_mbox = mlxsw_cmd_mbox_alloc();
1844 	if (!in_mbox)
1845 		return -ENOMEM;
1846 
1847 	out_mbox = mlxsw_cmd_mbox_alloc();
1848 	if (!out_mbox) {
1849 		err = -ENOMEM;
1850 		goto free_in_mbox;
1851 	}
1852 
1853 	mlxsw_emad_pack_op_tlv(in_mbox, reg, type,
1854 			       mlxsw_core_tid_get(mlxsw_core));
1855 	tmp = in_mbox + MLXSW_EMAD_OP_TLV_LEN * sizeof(u32);
1856 	mlxsw_emad_pack_reg_tlv(tmp, reg, payload);
1857 
1858 	/* There is a special treatment needed for MRSR (reset) register.
1859 	 * The command interface will return error after the command
1860 	 * is executed, so tell the lower layer to expect it
1861 	 * and cope accordingly.
1862 	 */
1863 	reset_ok = reg->id == MLXSW_REG_MRSR_ID;
1864 
1865 	n_retry = 0;
1866 retry:
1867 	err = mlxsw_cmd_access_reg(mlxsw_core, reset_ok, in_mbox, out_mbox);
1868 	if (!err) {
1869 		err = mlxsw_emad_process_status(out_mbox, &status);
1870 		if (err) {
1871 			if (err == -EAGAIN && n_retry++ < MLXSW_EMAD_MAX_RETRY)
1872 				goto retry;
1873 			dev_err(mlxsw_core->bus_info->dev, "Reg cmd access status failed (status=%x(%s))\n",
1874 				status, mlxsw_emad_op_tlv_status_str(status));
1875 		}
1876 	}
1877 
1878 	if (!err)
1879 		memcpy(payload, mlxsw_emad_reg_payload_cmd(out_mbox),
1880 		       reg->len);
1881 
1882 	mlxsw_cmd_mbox_free(out_mbox);
1883 free_in_mbox:
1884 	mlxsw_cmd_mbox_free(in_mbox);
1885 	if (err)
1886 		dev_err(mlxsw_core->bus_info->dev, "Reg cmd access failed (reg_id=%x(%s),type=%s)\n",
1887 			reg->id, mlxsw_reg_id_str(reg->id),
1888 			mlxsw_core_reg_access_type_str(type));
1889 	return err;
1890 }
1891 
1892 static void mlxsw_core_reg_access_cb(struct mlxsw_core *mlxsw_core,
1893 				     char *payload, size_t payload_len,
1894 				     unsigned long cb_priv)
1895 {
1896 	char *orig_payload = (char *) cb_priv;
1897 
1898 	memcpy(orig_payload, payload, payload_len);
1899 }
1900 
1901 static int mlxsw_core_reg_access(struct mlxsw_core *mlxsw_core,
1902 				 const struct mlxsw_reg_info *reg,
1903 				 char *payload,
1904 				 enum mlxsw_core_reg_access_type type)
1905 {
1906 	LIST_HEAD(bulk_list);
1907 	int err;
1908 
1909 	/* During initialization EMAD interface is not available to us,
1910 	 * so we default to command interface. We switch to EMAD interface
1911 	 * after setting the appropriate traps.
1912 	 */
1913 	if (!mlxsw_core->emad.use_emad)
1914 		return mlxsw_core_reg_access_cmd(mlxsw_core, reg,
1915 						 payload, type);
1916 
1917 	err = mlxsw_core_reg_access_emad(mlxsw_core, reg,
1918 					 payload, type, &bulk_list,
1919 					 mlxsw_core_reg_access_cb,
1920 					 (unsigned long) payload);
1921 	if (err)
1922 		return err;
1923 	return mlxsw_reg_trans_bulk_wait(&bulk_list);
1924 }
1925 
1926 int mlxsw_reg_query(struct mlxsw_core *mlxsw_core,
1927 		    const struct mlxsw_reg_info *reg, char *payload)
1928 {
1929 	return mlxsw_core_reg_access(mlxsw_core, reg, payload,
1930 				     MLXSW_CORE_REG_ACCESS_TYPE_QUERY);
1931 }
1932 EXPORT_SYMBOL(mlxsw_reg_query);
1933 
1934 int mlxsw_reg_write(struct mlxsw_core *mlxsw_core,
1935 		    const struct mlxsw_reg_info *reg, char *payload)
1936 {
1937 	return mlxsw_core_reg_access(mlxsw_core, reg, payload,
1938 				     MLXSW_CORE_REG_ACCESS_TYPE_WRITE);
1939 }
1940 EXPORT_SYMBOL(mlxsw_reg_write);
1941 
1942 void mlxsw_core_skb_receive(struct mlxsw_core *mlxsw_core, struct sk_buff *skb,
1943 			    struct mlxsw_rx_info *rx_info)
1944 {
1945 	struct mlxsw_rx_listener_item *rxl_item;
1946 	const struct mlxsw_rx_listener *rxl;
1947 	u8 local_port;
1948 	bool found = false;
1949 
1950 	if (rx_info->is_lag) {
1951 		dev_dbg_ratelimited(mlxsw_core->bus_info->dev, "%s: lag_id = %d, lag_port_index = 0x%x\n",
1952 				    __func__, rx_info->u.lag_id,
1953 				    rx_info->trap_id);
1954 		/* Upper layer does not care if the skb came from LAG or not,
1955 		 * so just get the local_port for the lag port and push it up.
1956 		 */
1957 		local_port = mlxsw_core_lag_mapping_get(mlxsw_core,
1958 							rx_info->u.lag_id,
1959 							rx_info->lag_port_index);
1960 	} else {
1961 		local_port = rx_info->u.sys_port;
1962 	}
1963 
1964 	dev_dbg_ratelimited(mlxsw_core->bus_info->dev, "%s: local_port = %d, trap_id = 0x%x\n",
1965 			    __func__, local_port, rx_info->trap_id);
1966 
1967 	if ((rx_info->trap_id >= MLXSW_TRAP_ID_MAX) ||
1968 	    (local_port >= mlxsw_core->max_ports))
1969 		goto drop;
1970 
1971 	rcu_read_lock();
1972 	list_for_each_entry_rcu(rxl_item, &mlxsw_core->rx_listener_list, list) {
1973 		rxl = &rxl_item->rxl;
1974 		if ((rxl->local_port == MLXSW_PORT_DONT_CARE ||
1975 		     rxl->local_port == local_port) &&
1976 		    rxl->trap_id == rx_info->trap_id) {
1977 			if (rxl_item->enabled)
1978 				found = true;
1979 			break;
1980 		}
1981 	}
1982 	rcu_read_unlock();
1983 	if (!found)
1984 		goto drop;
1985 
1986 	rxl->func(skb, local_port, rxl_item->priv);
1987 	return;
1988 
1989 drop:
1990 	dev_kfree_skb(skb);
1991 }
1992 EXPORT_SYMBOL(mlxsw_core_skb_receive);
1993 
1994 static int mlxsw_core_lag_mapping_index(struct mlxsw_core *mlxsw_core,
1995 					u16 lag_id, u8 port_index)
1996 {
1997 	return MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG_MEMBERS) * lag_id +
1998 	       port_index;
1999 }
2000 
2001 void mlxsw_core_lag_mapping_set(struct mlxsw_core *mlxsw_core,
2002 				u16 lag_id, u8 port_index, u8 local_port)
2003 {
2004 	int index = mlxsw_core_lag_mapping_index(mlxsw_core,
2005 						 lag_id, port_index);
2006 
2007 	mlxsw_core->lag.mapping[index] = local_port;
2008 }
2009 EXPORT_SYMBOL(mlxsw_core_lag_mapping_set);
2010 
2011 u8 mlxsw_core_lag_mapping_get(struct mlxsw_core *mlxsw_core,
2012 			      u16 lag_id, u8 port_index)
2013 {
2014 	int index = mlxsw_core_lag_mapping_index(mlxsw_core,
2015 						 lag_id, port_index);
2016 
2017 	return mlxsw_core->lag.mapping[index];
2018 }
2019 EXPORT_SYMBOL(mlxsw_core_lag_mapping_get);
2020 
2021 void mlxsw_core_lag_mapping_clear(struct mlxsw_core *mlxsw_core,
2022 				  u16 lag_id, u8 local_port)
2023 {
2024 	int i;
2025 
2026 	for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_core, MAX_LAG_MEMBERS); i++) {
2027 		int index = mlxsw_core_lag_mapping_index(mlxsw_core,
2028 							 lag_id, i);
2029 
2030 		if (mlxsw_core->lag.mapping[index] == local_port)
2031 			mlxsw_core->lag.mapping[index] = 0;
2032 	}
2033 }
2034 EXPORT_SYMBOL(mlxsw_core_lag_mapping_clear);
2035 
2036 bool mlxsw_core_res_valid(struct mlxsw_core *mlxsw_core,
2037 			  enum mlxsw_res_id res_id)
2038 {
2039 	return mlxsw_res_valid(&mlxsw_core->res, res_id);
2040 }
2041 EXPORT_SYMBOL(mlxsw_core_res_valid);
2042 
2043 u64 mlxsw_core_res_get(struct mlxsw_core *mlxsw_core,
2044 		       enum mlxsw_res_id res_id)
2045 {
2046 	return mlxsw_res_get(&mlxsw_core->res, res_id);
2047 }
2048 EXPORT_SYMBOL(mlxsw_core_res_get);
2049 
2050 static int __mlxsw_core_port_init(struct mlxsw_core *mlxsw_core, u8 local_port,
2051 				  enum devlink_port_flavour flavour,
2052 				  u32 port_number, bool split,
2053 				  u32 split_port_subnumber,
2054 				  const unsigned char *switch_id,
2055 				  unsigned char switch_id_len)
2056 {
2057 	struct devlink *devlink = priv_to_devlink(mlxsw_core);
2058 	struct mlxsw_core_port *mlxsw_core_port =
2059 					&mlxsw_core->ports[local_port];
2060 	struct devlink_port *devlink_port = &mlxsw_core_port->devlink_port;
2061 	int err;
2062 
2063 	mlxsw_core_port->local_port = local_port;
2064 	devlink_port_attrs_set(devlink_port, flavour, port_number,
2065 			       split, split_port_subnumber,
2066 			       switch_id, switch_id_len);
2067 	err = devlink_port_register(devlink, devlink_port, local_port);
2068 	if (err)
2069 		memset(mlxsw_core_port, 0, sizeof(*mlxsw_core_port));
2070 	return err;
2071 }
2072 
2073 static void __mlxsw_core_port_fini(struct mlxsw_core *mlxsw_core, u8 local_port)
2074 {
2075 	struct mlxsw_core_port *mlxsw_core_port =
2076 					&mlxsw_core->ports[local_port];
2077 	struct devlink_port *devlink_port = &mlxsw_core_port->devlink_port;
2078 
2079 	devlink_port_unregister(devlink_port);
2080 	memset(mlxsw_core_port, 0, sizeof(*mlxsw_core_port));
2081 }
2082 
2083 int mlxsw_core_port_init(struct mlxsw_core *mlxsw_core, u8 local_port,
2084 			 u32 port_number, bool split,
2085 			 u32 split_port_subnumber,
2086 			 const unsigned char *switch_id,
2087 			 unsigned char switch_id_len)
2088 {
2089 	return __mlxsw_core_port_init(mlxsw_core, local_port,
2090 				      DEVLINK_PORT_FLAVOUR_PHYSICAL,
2091 				      port_number, split, split_port_subnumber,
2092 				      switch_id, switch_id_len);
2093 }
2094 EXPORT_SYMBOL(mlxsw_core_port_init);
2095 
2096 void mlxsw_core_port_fini(struct mlxsw_core *mlxsw_core, u8 local_port)
2097 {
2098 	__mlxsw_core_port_fini(mlxsw_core, local_port);
2099 }
2100 EXPORT_SYMBOL(mlxsw_core_port_fini);
2101 
2102 int mlxsw_core_cpu_port_init(struct mlxsw_core *mlxsw_core,
2103 			     void *port_driver_priv,
2104 			     const unsigned char *switch_id,
2105 			     unsigned char switch_id_len)
2106 {
2107 	struct mlxsw_core_port *mlxsw_core_port =
2108 				&mlxsw_core->ports[MLXSW_PORT_CPU_PORT];
2109 	int err;
2110 
2111 	err = __mlxsw_core_port_init(mlxsw_core, MLXSW_PORT_CPU_PORT,
2112 				     DEVLINK_PORT_FLAVOUR_CPU,
2113 				     0, false, 0,
2114 				     switch_id, switch_id_len);
2115 	if (err)
2116 		return err;
2117 
2118 	mlxsw_core_port->port_driver_priv = port_driver_priv;
2119 	return 0;
2120 }
2121 EXPORT_SYMBOL(mlxsw_core_cpu_port_init);
2122 
2123 void mlxsw_core_cpu_port_fini(struct mlxsw_core *mlxsw_core)
2124 {
2125 	__mlxsw_core_port_fini(mlxsw_core, MLXSW_PORT_CPU_PORT);
2126 }
2127 EXPORT_SYMBOL(mlxsw_core_cpu_port_fini);
2128 
2129 void mlxsw_core_port_eth_set(struct mlxsw_core *mlxsw_core, u8 local_port,
2130 			     void *port_driver_priv, struct net_device *dev)
2131 {
2132 	struct mlxsw_core_port *mlxsw_core_port =
2133 					&mlxsw_core->ports[local_port];
2134 	struct devlink_port *devlink_port = &mlxsw_core_port->devlink_port;
2135 
2136 	mlxsw_core_port->port_driver_priv = port_driver_priv;
2137 	devlink_port_type_eth_set(devlink_port, dev);
2138 }
2139 EXPORT_SYMBOL(mlxsw_core_port_eth_set);
2140 
2141 void mlxsw_core_port_ib_set(struct mlxsw_core *mlxsw_core, u8 local_port,
2142 			    void *port_driver_priv)
2143 {
2144 	struct mlxsw_core_port *mlxsw_core_port =
2145 					&mlxsw_core->ports[local_port];
2146 	struct devlink_port *devlink_port = &mlxsw_core_port->devlink_port;
2147 
2148 	mlxsw_core_port->port_driver_priv = port_driver_priv;
2149 	devlink_port_type_ib_set(devlink_port, NULL);
2150 }
2151 EXPORT_SYMBOL(mlxsw_core_port_ib_set);
2152 
2153 void mlxsw_core_port_clear(struct mlxsw_core *mlxsw_core, u8 local_port,
2154 			   void *port_driver_priv)
2155 {
2156 	struct mlxsw_core_port *mlxsw_core_port =
2157 					&mlxsw_core->ports[local_port];
2158 	struct devlink_port *devlink_port = &mlxsw_core_port->devlink_port;
2159 
2160 	mlxsw_core_port->port_driver_priv = port_driver_priv;
2161 	devlink_port_type_clear(devlink_port);
2162 }
2163 EXPORT_SYMBOL(mlxsw_core_port_clear);
2164 
2165 enum devlink_port_type mlxsw_core_port_type_get(struct mlxsw_core *mlxsw_core,
2166 						u8 local_port)
2167 {
2168 	struct mlxsw_core_port *mlxsw_core_port =
2169 					&mlxsw_core->ports[local_port];
2170 	struct devlink_port *devlink_port = &mlxsw_core_port->devlink_port;
2171 
2172 	return devlink_port->type;
2173 }
2174 EXPORT_SYMBOL(mlxsw_core_port_type_get);
2175 
2176 
2177 struct devlink_port *
2178 mlxsw_core_port_devlink_port_get(struct mlxsw_core *mlxsw_core,
2179 				 u8 local_port)
2180 {
2181 	struct mlxsw_core_port *mlxsw_core_port =
2182 					&mlxsw_core->ports[local_port];
2183 	struct devlink_port *devlink_port = &mlxsw_core_port->devlink_port;
2184 
2185 	return devlink_port;
2186 }
2187 EXPORT_SYMBOL(mlxsw_core_port_devlink_port_get);
2188 
2189 int mlxsw_core_module_max_width(struct mlxsw_core *mlxsw_core, u8 module)
2190 {
2191 	enum mlxsw_reg_pmtm_module_type module_type;
2192 	char pmtm_pl[MLXSW_REG_PMTM_LEN];
2193 	int err;
2194 
2195 	mlxsw_reg_pmtm_pack(pmtm_pl, module);
2196 	err = mlxsw_reg_query(mlxsw_core, MLXSW_REG(pmtm), pmtm_pl);
2197 	if (err)
2198 		return err;
2199 	mlxsw_reg_pmtm_unpack(pmtm_pl, &module_type);
2200 
2201 	/* Here we need to get the module width according to the module type. */
2202 
2203 	switch (module_type) {
2204 	case MLXSW_REG_PMTM_MODULE_TYPE_BP_4X: /* fall through */
2205 	case MLXSW_REG_PMTM_MODULE_TYPE_BP_QSFP:
2206 		return 4;
2207 	case MLXSW_REG_PMTM_MODULE_TYPE_BP_2X:
2208 		return 2;
2209 	case MLXSW_REG_PMTM_MODULE_TYPE_BP_SFP: /* fall through */
2210 	case MLXSW_REG_PMTM_MODULE_TYPE_BP_1X:
2211 		return 1;
2212 	default:
2213 		return -EINVAL;
2214 	}
2215 }
2216 EXPORT_SYMBOL(mlxsw_core_module_max_width);
2217 
2218 static void mlxsw_core_buf_dump_dbg(struct mlxsw_core *mlxsw_core,
2219 				    const char *buf, size_t size)
2220 {
2221 	__be32 *m = (__be32 *) buf;
2222 	int i;
2223 	int count = size / sizeof(__be32);
2224 
2225 	for (i = count - 1; i >= 0; i--)
2226 		if (m[i])
2227 			break;
2228 	i++;
2229 	count = i ? i : 1;
2230 	for (i = 0; i < count; i += 4)
2231 		dev_dbg(mlxsw_core->bus_info->dev, "%04x - %08x %08x %08x %08x\n",
2232 			i * 4, be32_to_cpu(m[i]), be32_to_cpu(m[i + 1]),
2233 			be32_to_cpu(m[i + 2]), be32_to_cpu(m[i + 3]));
2234 }
2235 
2236 int mlxsw_cmd_exec(struct mlxsw_core *mlxsw_core, u16 opcode, u8 opcode_mod,
2237 		   u32 in_mod, bool out_mbox_direct, bool reset_ok,
2238 		   char *in_mbox, size_t in_mbox_size,
2239 		   char *out_mbox, size_t out_mbox_size)
2240 {
2241 	u8 status;
2242 	int err;
2243 
2244 	BUG_ON(in_mbox_size % sizeof(u32) || out_mbox_size % sizeof(u32));
2245 	if (!mlxsw_core->bus->cmd_exec)
2246 		return -EOPNOTSUPP;
2247 
2248 	dev_dbg(mlxsw_core->bus_info->dev, "Cmd exec (opcode=%x(%s),opcode_mod=%x,in_mod=%x)\n",
2249 		opcode, mlxsw_cmd_opcode_str(opcode), opcode_mod, in_mod);
2250 	if (in_mbox) {
2251 		dev_dbg(mlxsw_core->bus_info->dev, "Input mailbox:\n");
2252 		mlxsw_core_buf_dump_dbg(mlxsw_core, in_mbox, in_mbox_size);
2253 	}
2254 
2255 	err = mlxsw_core->bus->cmd_exec(mlxsw_core->bus_priv, opcode,
2256 					opcode_mod, in_mod, out_mbox_direct,
2257 					in_mbox, in_mbox_size,
2258 					out_mbox, out_mbox_size, &status);
2259 
2260 	if (!err && out_mbox) {
2261 		dev_dbg(mlxsw_core->bus_info->dev, "Output mailbox:\n");
2262 		mlxsw_core_buf_dump_dbg(mlxsw_core, out_mbox, out_mbox_size);
2263 	}
2264 
2265 	if (reset_ok && err == -EIO &&
2266 	    status == MLXSW_CMD_STATUS_RUNNING_RESET) {
2267 		err = 0;
2268 	} else if (err == -EIO && status != MLXSW_CMD_STATUS_OK) {
2269 		dev_err(mlxsw_core->bus_info->dev, "Cmd exec failed (opcode=%x(%s),opcode_mod=%x,in_mod=%x,status=%x(%s))\n",
2270 			opcode, mlxsw_cmd_opcode_str(opcode), opcode_mod,
2271 			in_mod, status, mlxsw_cmd_status_str(status));
2272 	} else if (err == -ETIMEDOUT) {
2273 		dev_err(mlxsw_core->bus_info->dev, "Cmd exec timed-out (opcode=%x(%s),opcode_mod=%x,in_mod=%x)\n",
2274 			opcode, mlxsw_cmd_opcode_str(opcode), opcode_mod,
2275 			in_mod);
2276 	}
2277 
2278 	return err;
2279 }
2280 EXPORT_SYMBOL(mlxsw_cmd_exec);
2281 
2282 int mlxsw_core_schedule_dw(struct delayed_work *dwork, unsigned long delay)
2283 {
2284 	return queue_delayed_work(mlxsw_wq, dwork, delay);
2285 }
2286 EXPORT_SYMBOL(mlxsw_core_schedule_dw);
2287 
2288 bool mlxsw_core_schedule_work(struct work_struct *work)
2289 {
2290 	return queue_work(mlxsw_owq, work);
2291 }
2292 EXPORT_SYMBOL(mlxsw_core_schedule_work);
2293 
2294 void mlxsw_core_flush_owq(void)
2295 {
2296 	flush_workqueue(mlxsw_owq);
2297 }
2298 EXPORT_SYMBOL(mlxsw_core_flush_owq);
2299 
2300 int mlxsw_core_kvd_sizes_get(struct mlxsw_core *mlxsw_core,
2301 			     const struct mlxsw_config_profile *profile,
2302 			     u64 *p_single_size, u64 *p_double_size,
2303 			     u64 *p_linear_size)
2304 {
2305 	struct mlxsw_driver *driver = mlxsw_core->driver;
2306 
2307 	if (!driver->kvd_sizes_get)
2308 		return -EINVAL;
2309 
2310 	return driver->kvd_sizes_get(mlxsw_core, profile,
2311 				     p_single_size, p_double_size,
2312 				     p_linear_size);
2313 }
2314 EXPORT_SYMBOL(mlxsw_core_kvd_sizes_get);
2315 
2316 void mlxsw_core_fw_flash_start(struct mlxsw_core *mlxsw_core)
2317 {
2318 	mlxsw_core->fw_flash_in_progress = true;
2319 }
2320 EXPORT_SYMBOL(mlxsw_core_fw_flash_start);
2321 
2322 void mlxsw_core_fw_flash_end(struct mlxsw_core *mlxsw_core)
2323 {
2324 	mlxsw_core->fw_flash_in_progress = false;
2325 }
2326 EXPORT_SYMBOL(mlxsw_core_fw_flash_end);
2327 
2328 int mlxsw_core_resources_query(struct mlxsw_core *mlxsw_core, char *mbox,
2329 			       struct mlxsw_res *res)
2330 {
2331 	int index, i;
2332 	u64 data;
2333 	u16 id;
2334 	int err;
2335 
2336 	if (!res)
2337 		return 0;
2338 
2339 	mlxsw_cmd_mbox_zero(mbox);
2340 
2341 	for (index = 0; index < MLXSW_CMD_QUERY_RESOURCES_MAX_QUERIES;
2342 	     index++) {
2343 		err = mlxsw_cmd_query_resources(mlxsw_core, mbox, index);
2344 		if (err)
2345 			return err;
2346 
2347 		for (i = 0; i < MLXSW_CMD_QUERY_RESOURCES_PER_QUERY; i++) {
2348 			id = mlxsw_cmd_mbox_query_resource_id_get(mbox, i);
2349 			data = mlxsw_cmd_mbox_query_resource_data_get(mbox, i);
2350 
2351 			if (id == MLXSW_CMD_QUERY_RESOURCES_TABLE_END_ID)
2352 				return 0;
2353 
2354 			mlxsw_res_parse(res, id, data);
2355 		}
2356 	}
2357 
2358 	/* If after MLXSW_RESOURCES_QUERY_MAX_QUERIES we still didn't get
2359 	 * MLXSW_RESOURCES_TABLE_END_ID, something went bad in the FW.
2360 	 */
2361 	return -EIO;
2362 }
2363 EXPORT_SYMBOL(mlxsw_core_resources_query);
2364 
2365 u32 mlxsw_core_read_frc_h(struct mlxsw_core *mlxsw_core)
2366 {
2367 	return mlxsw_core->bus->read_frc_h(mlxsw_core->bus_priv);
2368 }
2369 EXPORT_SYMBOL(mlxsw_core_read_frc_h);
2370 
2371 u32 mlxsw_core_read_frc_l(struct mlxsw_core *mlxsw_core)
2372 {
2373 	return mlxsw_core->bus->read_frc_l(mlxsw_core->bus_priv);
2374 }
2375 EXPORT_SYMBOL(mlxsw_core_read_frc_l);
2376 
2377 void mlxsw_core_emad_string_tlv_enable(struct mlxsw_core *mlxsw_core)
2378 {
2379 	mlxsw_core->emad.enable_string_tlv = true;
2380 }
2381 EXPORT_SYMBOL(mlxsw_core_emad_string_tlv_enable);
2382 
2383 static int __init mlxsw_core_module_init(void)
2384 {
2385 	int err;
2386 
2387 	mlxsw_wq = alloc_workqueue(mlxsw_core_driver_name, 0, 0);
2388 	if (!mlxsw_wq)
2389 		return -ENOMEM;
2390 	mlxsw_owq = alloc_ordered_workqueue("%s_ordered", 0,
2391 					    mlxsw_core_driver_name);
2392 	if (!mlxsw_owq) {
2393 		err = -ENOMEM;
2394 		goto err_alloc_ordered_workqueue;
2395 	}
2396 	return 0;
2397 
2398 err_alloc_ordered_workqueue:
2399 	destroy_workqueue(mlxsw_wq);
2400 	return err;
2401 }
2402 
2403 static void __exit mlxsw_core_module_exit(void)
2404 {
2405 	destroy_workqueue(mlxsw_owq);
2406 	destroy_workqueue(mlxsw_wq);
2407 }
2408 
2409 module_init(mlxsw_core_module_init);
2410 module_exit(mlxsw_core_module_exit);
2411 
2412 MODULE_LICENSE("Dual BSD/GPL");
2413 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
2414 MODULE_DESCRIPTION("Mellanox switch device core driver");
2415