xref: /linux/drivers/net/ethernet/mellanox/mlxsw/pci.c (revision a3a02a52bcfcbcc4a637d4b68bf1bc391c9fad02)
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/export.h>
7 #include <linux/err.h>
8 #include <linux/device.h>
9 #include <linux/pci.h>
10 #include <linux/interrupt.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/if_vlan.h>
14 #include <linux/log2.h>
15 #include <linux/string.h>
16 #include <net/page_pool/helpers.h>
17 
18 #include "pci_hw.h"
19 #include "pci.h"
20 #include "core.h"
21 #include "cmd.h"
22 #include "port.h"
23 #include "resources.h"
24 
25 #define mlxsw_pci_write32(mlxsw_pci, reg, val) \
26 	iowrite32be(val, (mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
27 #define mlxsw_pci_read32(mlxsw_pci, reg) \
28 	ioread32be((mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
29 
30 enum mlxsw_pci_queue_type {
31 	MLXSW_PCI_QUEUE_TYPE_SDQ,
32 	MLXSW_PCI_QUEUE_TYPE_RDQ,
33 	MLXSW_PCI_QUEUE_TYPE_CQ,
34 	MLXSW_PCI_QUEUE_TYPE_EQ,
35 };
36 
37 #define MLXSW_PCI_QUEUE_TYPE_COUNT	4
38 
39 enum mlxsw_pci_cq_type {
40 	MLXSW_PCI_CQ_SDQ,
41 	MLXSW_PCI_CQ_RDQ,
42 };
43 
44 static const u16 mlxsw_pci_doorbell_type_offset[] = {
45 	MLXSW_PCI_DOORBELL_SDQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_SDQ */
46 	MLXSW_PCI_DOORBELL_RDQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_RDQ */
47 	MLXSW_PCI_DOORBELL_CQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_CQ */
48 	MLXSW_PCI_DOORBELL_EQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_EQ */
49 };
50 
51 static const u16 mlxsw_pci_doorbell_arm_type_offset[] = {
52 	0, /* unused */
53 	0, /* unused */
54 	MLXSW_PCI_DOORBELL_ARM_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
55 	MLXSW_PCI_DOORBELL_ARM_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
56 };
57 
58 struct mlxsw_pci_mem_item {
59 	char *buf;
60 	dma_addr_t mapaddr;
61 	size_t size;
62 };
63 
64 struct mlxsw_pci_queue_elem_info {
65 	struct page *pages[MLXSW_PCI_WQE_SG_ENTRIES];
66 	char *elem; /* pointer to actual dma mapped element mem chunk */
67 	struct {
68 		struct sk_buff *skb;
69 	} sdq;
70 };
71 
72 struct mlxsw_pci_queue {
73 	spinlock_t lock; /* for queue accesses */
74 	struct mlxsw_pci_mem_item mem_item;
75 	struct mlxsw_pci_queue_elem_info *elem_info;
76 	u16 producer_counter;
77 	u16 consumer_counter;
78 	u16 count; /* number of elements in queue */
79 	u8 num; /* queue number */
80 	u8 elem_size; /* size of one element */
81 	enum mlxsw_pci_queue_type type;
82 	struct mlxsw_pci *pci;
83 	union {
84 		struct {
85 			enum mlxsw_pci_cqe_v v;
86 			struct mlxsw_pci_queue *dq;
87 			struct napi_struct napi;
88 			struct page_pool *page_pool;
89 		} cq;
90 		struct {
91 			struct tasklet_struct tasklet;
92 		} eq;
93 		struct {
94 			struct mlxsw_pci_queue *cq;
95 		} rdq;
96 	} u;
97 };
98 
99 struct mlxsw_pci_queue_type_group {
100 	struct mlxsw_pci_queue *q;
101 	u8 count; /* number of queues in group */
102 };
103 
104 struct mlxsw_pci {
105 	struct pci_dev *pdev;
106 	u8 __iomem *hw_addr;
107 	u64 free_running_clock_offset;
108 	u64 utc_sec_offset;
109 	u64 utc_nsec_offset;
110 	bool lag_mode_support;
111 	bool cff_support;
112 	enum mlxsw_cmd_mbox_config_profile_lag_mode lag_mode;
113 	enum mlxsw_cmd_mbox_config_profile_flood_mode flood_mode;
114 	u8 num_sg_entries; /* Number of scatter/gather entries for packets. */
115 	struct mlxsw_pci_queue_type_group queues[MLXSW_PCI_QUEUE_TYPE_COUNT];
116 	u32 doorbell_offset;
117 	struct mlxsw_core *core;
118 	struct {
119 		struct mlxsw_pci_mem_item *items;
120 		unsigned int count;
121 	} fw_area;
122 	struct {
123 		struct mlxsw_pci_mem_item out_mbox;
124 		struct mlxsw_pci_mem_item in_mbox;
125 		struct mutex lock; /* Lock access to command registers */
126 		struct {
127 			u8 status;
128 			u64 out_param;
129 		} comp;
130 	} cmd;
131 	struct mlxsw_bus_info bus_info;
132 	const struct pci_device_id *id;
133 	enum mlxsw_pci_cqe_v max_cqe_ver; /* Maximal supported CQE version */
134 	u8 num_cqs; /* Number of CQs */
135 	u8 num_sdqs; /* Number of SDQs */
136 	bool skip_reset;
137 	struct net_device *napi_dev_tx;
138 	struct net_device *napi_dev_rx;
139 };
140 
141 static int mlxsw_pci_napi_devs_init(struct mlxsw_pci *mlxsw_pci)
142 {
143 	int err;
144 
145 	mlxsw_pci->napi_dev_tx = alloc_netdev_dummy(0);
146 	if (!mlxsw_pci->napi_dev_tx)
147 		return -ENOMEM;
148 	strscpy(mlxsw_pci->napi_dev_tx->name, "mlxsw_tx",
149 		sizeof(mlxsw_pci->napi_dev_tx->name));
150 
151 	mlxsw_pci->napi_dev_rx = alloc_netdev_dummy(0);
152 	if (!mlxsw_pci->napi_dev_rx) {
153 		err = -ENOMEM;
154 		goto err_alloc_rx;
155 	}
156 	strscpy(mlxsw_pci->napi_dev_rx->name, "mlxsw_rx",
157 		sizeof(mlxsw_pci->napi_dev_rx->name));
158 	dev_set_threaded(mlxsw_pci->napi_dev_rx, true);
159 
160 	return 0;
161 
162 err_alloc_rx:
163 	free_netdev(mlxsw_pci->napi_dev_tx);
164 	return err;
165 }
166 
167 static void mlxsw_pci_napi_devs_fini(struct mlxsw_pci *mlxsw_pci)
168 {
169 	free_netdev(mlxsw_pci->napi_dev_rx);
170 	free_netdev(mlxsw_pci->napi_dev_tx);
171 }
172 
173 static char *__mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q,
174 					size_t elem_size, int elem_index)
175 {
176 	return q->mem_item.buf + (elem_size * elem_index);
177 }
178 
179 static struct mlxsw_pci_queue_elem_info *
180 mlxsw_pci_queue_elem_info_get(struct mlxsw_pci_queue *q, int elem_index)
181 {
182 	return &q->elem_info[elem_index];
183 }
184 
185 static struct mlxsw_pci_queue_elem_info *
186 mlxsw_pci_queue_elem_info_producer_get(struct mlxsw_pci_queue *q)
187 {
188 	int index = q->producer_counter & (q->count - 1);
189 
190 	if ((u16) (q->producer_counter - q->consumer_counter) == q->count)
191 		return NULL;
192 	return mlxsw_pci_queue_elem_info_get(q, index);
193 }
194 
195 static struct mlxsw_pci_queue_elem_info *
196 mlxsw_pci_queue_elem_info_consumer_get(struct mlxsw_pci_queue *q)
197 {
198 	int index = q->consumer_counter & (q->count - 1);
199 
200 	return mlxsw_pci_queue_elem_info_get(q, index);
201 }
202 
203 static char *mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, int elem_index)
204 {
205 	return mlxsw_pci_queue_elem_info_get(q, elem_index)->elem;
206 }
207 
208 static bool mlxsw_pci_elem_hw_owned(struct mlxsw_pci_queue *q, bool owner_bit)
209 {
210 	return owner_bit != !!(q->consumer_counter & q->count);
211 }
212 
213 static struct mlxsw_pci_queue_type_group *
214 mlxsw_pci_queue_type_group_get(struct mlxsw_pci *mlxsw_pci,
215 			       enum mlxsw_pci_queue_type q_type)
216 {
217 	return &mlxsw_pci->queues[q_type];
218 }
219 
220 static struct mlxsw_pci_queue *
221 __mlxsw_pci_queue_get(struct mlxsw_pci *mlxsw_pci,
222 		      enum mlxsw_pci_queue_type q_type, u8 q_num)
223 {
224 	return &mlxsw_pci->queues[q_type].q[q_num];
225 }
226 
227 static struct mlxsw_pci_queue *mlxsw_pci_sdq_get(struct mlxsw_pci *mlxsw_pci,
228 						 u8 q_num)
229 {
230 	return __mlxsw_pci_queue_get(mlxsw_pci,
231 				     MLXSW_PCI_QUEUE_TYPE_SDQ, q_num);
232 }
233 
234 static struct mlxsw_pci_queue *mlxsw_pci_cq_get(struct mlxsw_pci *mlxsw_pci,
235 						u8 q_num)
236 {
237 	return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ, q_num);
238 }
239 
240 static struct mlxsw_pci_queue *mlxsw_pci_eq_get(struct mlxsw_pci *mlxsw_pci)
241 {
242 	/* There is only one EQ at index 0. */
243 	return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ, 0);
244 }
245 
246 static void __mlxsw_pci_queue_doorbell_set(struct mlxsw_pci *mlxsw_pci,
247 					   struct mlxsw_pci_queue *q,
248 					   u16 val)
249 {
250 	mlxsw_pci_write32(mlxsw_pci,
251 			  DOORBELL(mlxsw_pci->doorbell_offset,
252 				   mlxsw_pci_doorbell_type_offset[q->type],
253 				   q->num), val);
254 }
255 
256 static void __mlxsw_pci_queue_doorbell_arm_set(struct mlxsw_pci *mlxsw_pci,
257 					       struct mlxsw_pci_queue *q,
258 					       u16 val)
259 {
260 	mlxsw_pci_write32(mlxsw_pci,
261 			  DOORBELL(mlxsw_pci->doorbell_offset,
262 				   mlxsw_pci_doorbell_arm_type_offset[q->type],
263 				   q->num), val);
264 }
265 
266 static void mlxsw_pci_queue_doorbell_producer_ring(struct mlxsw_pci *mlxsw_pci,
267 						   struct mlxsw_pci_queue *q)
268 {
269 	wmb(); /* ensure all writes are done before we ring a bell */
270 	__mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, q->producer_counter);
271 }
272 
273 static void mlxsw_pci_queue_doorbell_consumer_ring(struct mlxsw_pci *mlxsw_pci,
274 						   struct mlxsw_pci_queue *q)
275 {
276 	wmb(); /* ensure all writes are done before we ring a bell */
277 	__mlxsw_pci_queue_doorbell_set(mlxsw_pci, q,
278 				       q->consumer_counter + q->count);
279 }
280 
281 static void
282 mlxsw_pci_queue_doorbell_arm_consumer_ring(struct mlxsw_pci *mlxsw_pci,
283 					   struct mlxsw_pci_queue *q)
284 {
285 	wmb(); /* ensure all writes are done before we ring a bell */
286 	__mlxsw_pci_queue_doorbell_arm_set(mlxsw_pci, q, q->consumer_counter);
287 }
288 
289 static dma_addr_t __mlxsw_pci_queue_page_get(struct mlxsw_pci_queue *q,
290 					     int page_index)
291 {
292 	return q->mem_item.mapaddr + MLXSW_PCI_PAGE_SIZE * page_index;
293 }
294 
295 static int mlxsw_pci_sdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
296 			      struct mlxsw_pci_queue *q)
297 {
298 	struct mlxsw_pci_queue *cq;
299 	int tclass;
300 	u8 cq_num;
301 	int lp;
302 	int i;
303 	int err;
304 
305 	q->producer_counter = 0;
306 	q->consumer_counter = 0;
307 	tclass = q->num == MLXSW_PCI_SDQ_EMAD_INDEX ? MLXSW_PCI_SDQ_EMAD_TC :
308 						      MLXSW_PCI_SDQ_CTL_TC;
309 	lp = q->num == MLXSW_PCI_SDQ_EMAD_INDEX ? MLXSW_CMD_MBOX_SW2HW_DQ_SDQ_LP_IGNORE_WQE :
310 						  MLXSW_CMD_MBOX_SW2HW_DQ_SDQ_LP_WQE;
311 
312 	/* Set CQ of same number of this SDQ. */
313 	cq_num = q->num;
314 	mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, cq_num);
315 	mlxsw_cmd_mbox_sw2hw_dq_sdq_lp_set(mbox, lp);
316 	mlxsw_cmd_mbox_sw2hw_dq_sdq_tclass_set(mbox, tclass);
317 	mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
318 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
319 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
320 
321 		mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
322 	}
323 
324 	err = mlxsw_cmd_sw2hw_sdq(mlxsw_pci->core, mbox, q->num);
325 	if (err)
326 		return err;
327 
328 	cq = mlxsw_pci_cq_get(mlxsw_pci, cq_num);
329 	cq->u.cq.dq = q;
330 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
331 	return 0;
332 }
333 
334 static void mlxsw_pci_sdq_fini(struct mlxsw_pci *mlxsw_pci,
335 			       struct mlxsw_pci_queue *q)
336 {
337 	mlxsw_cmd_hw2sw_sdq(mlxsw_pci->core, q->num);
338 }
339 
340 #define MLXSW_PCI_SKB_HEADROOM (NET_SKB_PAD + NET_IP_ALIGN)
341 
342 #define MLXSW_PCI_RX_BUF_SW_OVERHEAD		\
343 		(MLXSW_PCI_SKB_HEADROOM +	\
344 		SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
345 
346 static void
347 mlxsw_pci_wqe_rx_frag_set(struct mlxsw_pci *mlxsw_pci, struct page *page,
348 			  char *wqe, int index, size_t frag_len)
349 {
350 	dma_addr_t mapaddr;
351 
352 	mapaddr = page_pool_get_dma_addr(page);
353 
354 	if (index == 0) {
355 		mapaddr += MLXSW_PCI_SKB_HEADROOM;
356 		frag_len = frag_len - MLXSW_PCI_RX_BUF_SW_OVERHEAD;
357 	}
358 
359 	mlxsw_pci_wqe_address_set(wqe, index, mapaddr);
360 	mlxsw_pci_wqe_byte_count_set(wqe, index, frag_len);
361 }
362 
363 static int mlxsw_pci_wqe_frag_map(struct mlxsw_pci *mlxsw_pci, char *wqe,
364 				  int index, char *frag_data, size_t frag_len,
365 				  int direction)
366 {
367 	struct pci_dev *pdev = mlxsw_pci->pdev;
368 	dma_addr_t mapaddr;
369 
370 	mapaddr = dma_map_single(&pdev->dev, frag_data, frag_len, direction);
371 	if (unlikely(dma_mapping_error(&pdev->dev, mapaddr))) {
372 		dev_err_ratelimited(&pdev->dev, "failed to dma map tx frag\n");
373 		return -EIO;
374 	}
375 	mlxsw_pci_wqe_address_set(wqe, index, mapaddr);
376 	mlxsw_pci_wqe_byte_count_set(wqe, index, frag_len);
377 	return 0;
378 }
379 
380 static void mlxsw_pci_wqe_frag_unmap(struct mlxsw_pci *mlxsw_pci, char *wqe,
381 				     int index, int direction)
382 {
383 	struct pci_dev *pdev = mlxsw_pci->pdev;
384 	size_t frag_len = mlxsw_pci_wqe_byte_count_get(wqe, index);
385 	dma_addr_t mapaddr = mlxsw_pci_wqe_address_get(wqe, index);
386 
387 	if (!frag_len)
388 		return;
389 	dma_unmap_single(&pdev->dev, mapaddr, frag_len, direction);
390 }
391 
392 static struct sk_buff *mlxsw_pci_rdq_build_skb(struct page *pages[],
393 					       u16 byte_count)
394 {
395 	unsigned int linear_data_size;
396 	struct sk_buff *skb;
397 	int page_index = 0;
398 	bool linear_only;
399 	void *data;
400 
401 	data = page_address(pages[page_index]);
402 	net_prefetch(data);
403 
404 	skb = napi_build_skb(data, PAGE_SIZE);
405 	if (unlikely(!skb))
406 		return ERR_PTR(-ENOMEM);
407 
408 	linear_only = byte_count + MLXSW_PCI_RX_BUF_SW_OVERHEAD <= PAGE_SIZE;
409 	linear_data_size = linear_only ? byte_count :
410 					 PAGE_SIZE -
411 					 MLXSW_PCI_RX_BUF_SW_OVERHEAD;
412 
413 	skb_reserve(skb, MLXSW_PCI_SKB_HEADROOM);
414 	skb_put(skb, linear_data_size);
415 
416 	if (linear_only)
417 		return skb;
418 
419 	byte_count -= linear_data_size;
420 	page_index++;
421 
422 	while (byte_count > 0) {
423 		unsigned int frag_size;
424 		struct page *page;
425 
426 		page = pages[page_index];
427 		frag_size = min(byte_count, PAGE_SIZE);
428 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
429 				page, 0, frag_size, PAGE_SIZE);
430 		byte_count -= frag_size;
431 		page_index++;
432 	}
433 
434 	return skb;
435 }
436 
437 static int mlxsw_pci_rdq_page_alloc(struct mlxsw_pci_queue *q,
438 				    struct mlxsw_pci_queue_elem_info *elem_info,
439 				    int index)
440 {
441 	struct mlxsw_pci_queue *cq = q->u.rdq.cq;
442 	char *wqe = elem_info->elem;
443 	struct page *page;
444 
445 	page = page_pool_dev_alloc_pages(cq->u.cq.page_pool);
446 	if (unlikely(!page))
447 		return -ENOMEM;
448 
449 	mlxsw_pci_wqe_rx_frag_set(q->pci, page, wqe, index, PAGE_SIZE);
450 	elem_info->pages[index] = page;
451 	return 0;
452 }
453 
454 static void mlxsw_pci_rdq_page_free(struct mlxsw_pci_queue *q,
455 				    struct mlxsw_pci_queue_elem_info *elem_info,
456 				    int index)
457 {
458 	struct mlxsw_pci_queue *cq = q->u.rdq.cq;
459 
460 	page_pool_put_page(cq->u.cq.page_pool, elem_info->pages[index], -1,
461 			   false);
462 }
463 
464 static u8 mlxsw_pci_num_sg_entries_get(u16 byte_count)
465 {
466 	return DIV_ROUND_UP(byte_count + MLXSW_PCI_RX_BUF_SW_OVERHEAD,
467 			    PAGE_SIZE);
468 }
469 
470 static int
471 mlxsw_pci_elem_info_pages_ref_store(const struct mlxsw_pci_queue *q,
472 				    const struct mlxsw_pci_queue_elem_info *el,
473 				    u16 byte_count, struct page *pages[],
474 				    u8 *p_num_sg_entries)
475 {
476 	u8 num_sg_entries;
477 	int i;
478 
479 	num_sg_entries = mlxsw_pci_num_sg_entries_get(byte_count);
480 	if (WARN_ON_ONCE(num_sg_entries > q->pci->num_sg_entries))
481 		return -EINVAL;
482 
483 	for (i = 0; i < num_sg_entries; i++)
484 		pages[i] = el->pages[i];
485 
486 	*p_num_sg_entries = num_sg_entries;
487 	return 0;
488 }
489 
490 static int
491 mlxsw_pci_rdq_pages_alloc(struct mlxsw_pci_queue *q,
492 			  struct mlxsw_pci_queue_elem_info *elem_info,
493 			  u8 num_sg_entries)
494 {
495 	struct page *old_pages[MLXSW_PCI_WQE_SG_ENTRIES];
496 	struct mlxsw_pci_queue *cq = q->u.rdq.cq;
497 	int i, err;
498 
499 	for (i = 0; i < num_sg_entries; i++) {
500 		old_pages[i] = elem_info->pages[i];
501 		err = mlxsw_pci_rdq_page_alloc(q, elem_info, i);
502 		if (err) {
503 			dev_err_ratelimited(&q->pci->pdev->dev, "Failed to alloc page\n");
504 			goto err_page_alloc;
505 		}
506 	}
507 
508 	return 0;
509 
510 err_page_alloc:
511 	for (i--; i >= 0; i--)
512 		page_pool_recycle_direct(cq->u.cq.page_pool, old_pages[i]);
513 
514 	return err;
515 }
516 
517 static void
518 mlxsw_pci_rdq_pages_recycle(struct mlxsw_pci_queue *q, struct page *pages[],
519 			    u8 num_sg_entries)
520 {
521 	struct mlxsw_pci_queue *cq = q->u.rdq.cq;
522 	int i;
523 
524 	for (i = 0; i < num_sg_entries; i++)
525 		page_pool_recycle_direct(cq->u.cq.page_pool, pages[i]);
526 }
527 
528 static int mlxsw_pci_rdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
529 			      struct mlxsw_pci_queue *q)
530 {
531 	struct mlxsw_pci_queue_elem_info *elem_info;
532 	u8 sdq_count = mlxsw_pci->num_sdqs;
533 	struct mlxsw_pci_queue *cq;
534 	u8 cq_num;
535 	int i, j;
536 	int err;
537 
538 	q->producer_counter = 0;
539 	q->consumer_counter = 0;
540 
541 	/* Set CQ of same number of this RDQ with base
542 	 * above SDQ count as the lower ones are assigned to SDQs.
543 	 */
544 	cq_num = sdq_count + q->num;
545 	mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, cq_num);
546 	mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
547 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
548 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
549 
550 		mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
551 	}
552 
553 	err = mlxsw_cmd_sw2hw_rdq(mlxsw_pci->core, mbox, q->num);
554 	if (err)
555 		return err;
556 
557 	cq = mlxsw_pci_cq_get(mlxsw_pci, cq_num);
558 	cq->u.cq.dq = q;
559 	q->u.rdq.cq = cq;
560 
561 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
562 
563 	for (i = 0; i < q->count; i++) {
564 		elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
565 		BUG_ON(!elem_info);
566 
567 		for (j = 0; j < mlxsw_pci->num_sg_entries; j++) {
568 			err = mlxsw_pci_rdq_page_alloc(q, elem_info, j);
569 			if (err)
570 				goto rollback;
571 		}
572 		/* Everything is set up, ring doorbell to pass elem to HW */
573 		q->producer_counter++;
574 		mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
575 	}
576 
577 	return 0;
578 
579 rollback:
580 	for (i--; i >= 0; i--) {
581 		elem_info = mlxsw_pci_queue_elem_info_get(q, i);
582 		for (j--; j >= 0; j--)
583 			mlxsw_pci_rdq_page_free(q, elem_info, j);
584 		j = mlxsw_pci->num_sg_entries;
585 	}
586 	q->u.rdq.cq = NULL;
587 	cq->u.cq.dq = NULL;
588 	mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
589 
590 	return err;
591 }
592 
593 static void mlxsw_pci_rdq_fini(struct mlxsw_pci *mlxsw_pci,
594 			       struct mlxsw_pci_queue *q)
595 {
596 	struct mlxsw_pci_queue_elem_info *elem_info;
597 	int i, j;
598 
599 	mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
600 	for (i = 0; i < q->count; i++) {
601 		elem_info = mlxsw_pci_queue_elem_info_get(q, i);
602 		for (j = 0; j < mlxsw_pci->num_sg_entries; j++)
603 			mlxsw_pci_rdq_page_free(q, elem_info, j);
604 	}
605 }
606 
607 static void mlxsw_pci_cq_pre_init(struct mlxsw_pci *mlxsw_pci,
608 				  struct mlxsw_pci_queue *q)
609 {
610 	q->u.cq.v = mlxsw_pci->max_cqe_ver;
611 
612 	if (q->u.cq.v == MLXSW_PCI_CQE_V2 &&
613 	    q->num < mlxsw_pci->num_sdqs &&
614 	    !mlxsw_core_sdq_supports_cqe_v2(mlxsw_pci->core))
615 		q->u.cq.v = MLXSW_PCI_CQE_V1;
616 }
617 
618 static unsigned int mlxsw_pci_read32_off(struct mlxsw_pci *mlxsw_pci,
619 					 ptrdiff_t off)
620 {
621 	return ioread32be(mlxsw_pci->hw_addr + off);
622 }
623 
624 static void mlxsw_pci_skb_cb_ts_set(struct mlxsw_pci *mlxsw_pci,
625 				    struct sk_buff *skb,
626 				    enum mlxsw_pci_cqe_v cqe_v, char *cqe)
627 {
628 	u8 ts_type;
629 
630 	if (cqe_v != MLXSW_PCI_CQE_V2)
631 		return;
632 
633 	ts_type = mlxsw_pci_cqe2_time_stamp_type_get(cqe);
634 
635 	if (ts_type != MLXSW_PCI_CQE_TIME_STAMP_TYPE_UTC &&
636 	    ts_type != MLXSW_PCI_CQE_TIME_STAMP_TYPE_MIRROR_UTC)
637 		return;
638 
639 	mlxsw_skb_cb(skb)->cqe_ts.sec = mlxsw_pci_cqe2_time_stamp_sec_get(cqe);
640 	mlxsw_skb_cb(skb)->cqe_ts.nsec =
641 		mlxsw_pci_cqe2_time_stamp_nsec_get(cqe);
642 }
643 
644 static void mlxsw_pci_cqe_sdq_handle(struct mlxsw_pci *mlxsw_pci,
645 				     struct mlxsw_pci_queue *q,
646 				     u16 consumer_counter_limit,
647 				     enum mlxsw_pci_cqe_v cqe_v,
648 				     char *cqe, int budget)
649 {
650 	struct pci_dev *pdev = mlxsw_pci->pdev;
651 	struct mlxsw_pci_queue_elem_info *elem_info;
652 	struct mlxsw_tx_info tx_info;
653 	char *wqe;
654 	struct sk_buff *skb;
655 	int i;
656 
657 	spin_lock(&q->lock);
658 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
659 	tx_info = mlxsw_skb_cb(elem_info->sdq.skb)->tx_info;
660 	skb = elem_info->sdq.skb;
661 	wqe = elem_info->elem;
662 	for (i = 0; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
663 		mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
664 
665 	if (unlikely(!tx_info.is_emad &&
666 		     skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
667 		mlxsw_pci_skb_cb_ts_set(mlxsw_pci, skb, cqe_v, cqe);
668 		mlxsw_core_ptp_transmitted(mlxsw_pci->core, skb,
669 					   tx_info.local_port);
670 		skb = NULL;
671 	}
672 
673 	if (skb)
674 		napi_consume_skb(skb, budget);
675 	elem_info->sdq.skb = NULL;
676 
677 	if (q->consumer_counter++ != consumer_counter_limit)
678 		dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in SDQ\n");
679 	spin_unlock(&q->lock);
680 }
681 
682 static void mlxsw_pci_cqe_rdq_md_tx_port_init(struct sk_buff *skb,
683 					      const char *cqe)
684 {
685 	struct mlxsw_skb_cb *cb = mlxsw_skb_cb(skb);
686 
687 	if (mlxsw_pci_cqe2_tx_lag_get(cqe)) {
688 		cb->rx_md_info.tx_port_is_lag = true;
689 		cb->rx_md_info.tx_lag_id = mlxsw_pci_cqe2_tx_lag_id_get(cqe);
690 		cb->rx_md_info.tx_lag_port_index =
691 			mlxsw_pci_cqe2_tx_lag_subport_get(cqe);
692 	} else {
693 		cb->rx_md_info.tx_port_is_lag = false;
694 		cb->rx_md_info.tx_sys_port =
695 			mlxsw_pci_cqe2_tx_system_port_get(cqe);
696 	}
697 
698 	if (cb->rx_md_info.tx_sys_port != MLXSW_PCI_CQE2_TX_PORT_MULTI_PORT &&
699 	    cb->rx_md_info.tx_sys_port != MLXSW_PCI_CQE2_TX_PORT_INVALID)
700 		cb->rx_md_info.tx_port_valid = 1;
701 	else
702 		cb->rx_md_info.tx_port_valid = 0;
703 }
704 
705 static void mlxsw_pci_cqe_rdq_md_init(struct sk_buff *skb, const char *cqe)
706 {
707 	struct mlxsw_skb_cb *cb = mlxsw_skb_cb(skb);
708 
709 	cb->rx_md_info.tx_congestion = mlxsw_pci_cqe2_mirror_cong_get(cqe);
710 	if (cb->rx_md_info.tx_congestion != MLXSW_PCI_CQE2_MIRROR_CONG_INVALID)
711 		cb->rx_md_info.tx_congestion_valid = 1;
712 	else
713 		cb->rx_md_info.tx_congestion_valid = 0;
714 	cb->rx_md_info.tx_congestion <<= MLXSW_PCI_CQE2_MIRROR_CONG_SHIFT;
715 
716 	cb->rx_md_info.latency = mlxsw_pci_cqe2_mirror_latency_get(cqe);
717 	if (cb->rx_md_info.latency != MLXSW_PCI_CQE2_MIRROR_LATENCY_INVALID)
718 		cb->rx_md_info.latency_valid = 1;
719 	else
720 		cb->rx_md_info.latency_valid = 0;
721 
722 	cb->rx_md_info.tx_tc = mlxsw_pci_cqe2_mirror_tclass_get(cqe);
723 	if (cb->rx_md_info.tx_tc != MLXSW_PCI_CQE2_MIRROR_TCLASS_INVALID)
724 		cb->rx_md_info.tx_tc_valid = 1;
725 	else
726 		cb->rx_md_info.tx_tc_valid = 0;
727 
728 	mlxsw_pci_cqe_rdq_md_tx_port_init(skb, cqe);
729 }
730 
731 static void mlxsw_pci_cqe_rdq_handle(struct mlxsw_pci *mlxsw_pci,
732 				     struct mlxsw_pci_queue *q,
733 				     u16 consumer_counter_limit,
734 				     enum mlxsw_pci_cqe_v cqe_v, char *cqe)
735 {
736 	struct pci_dev *pdev = mlxsw_pci->pdev;
737 	struct page *pages[MLXSW_PCI_WQE_SG_ENTRIES];
738 	struct mlxsw_pci_queue_elem_info *elem_info;
739 	struct mlxsw_rx_info rx_info = {};
740 	struct sk_buff *skb;
741 	u8 num_sg_entries;
742 	u16 byte_count;
743 	int err;
744 
745 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
746 
747 	if (q->consumer_counter++ != consumer_counter_limit)
748 		dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in RDQ\n");
749 
750 	byte_count = mlxsw_pci_cqe_byte_count_get(cqe);
751 	if (mlxsw_pci_cqe_crc_get(cqe_v, cqe))
752 		byte_count -= ETH_FCS_LEN;
753 
754 	err = mlxsw_pci_elem_info_pages_ref_store(q, elem_info, byte_count,
755 						  pages, &num_sg_entries);
756 	if (err)
757 		goto out;
758 
759 	err = mlxsw_pci_rdq_pages_alloc(q, elem_info, num_sg_entries);
760 	if (err)
761 		goto out;
762 
763 	skb = mlxsw_pci_rdq_build_skb(pages, byte_count);
764 	if (IS_ERR(skb)) {
765 		dev_err_ratelimited(&pdev->dev, "Failed to build skb for RDQ\n");
766 		mlxsw_pci_rdq_pages_recycle(q, pages, num_sg_entries);
767 		goto out;
768 	}
769 
770 	skb_mark_for_recycle(skb);
771 
772 	if (mlxsw_pci_cqe_lag_get(cqe_v, cqe)) {
773 		rx_info.is_lag = true;
774 		rx_info.u.lag_id = mlxsw_pci_cqe_lag_id_get(cqe_v, cqe);
775 		rx_info.lag_port_index =
776 			mlxsw_pci_cqe_lag_subport_get(cqe_v, cqe);
777 	} else {
778 		rx_info.is_lag = false;
779 		rx_info.u.sys_port = mlxsw_pci_cqe_system_port_get(cqe);
780 	}
781 
782 	rx_info.trap_id = mlxsw_pci_cqe_trap_id_get(cqe);
783 
784 	if (rx_info.trap_id == MLXSW_TRAP_ID_DISCARD_INGRESS_ACL ||
785 	    rx_info.trap_id == MLXSW_TRAP_ID_DISCARD_EGRESS_ACL) {
786 		u32 cookie_index = 0;
787 
788 		if (mlxsw_pci->max_cqe_ver >= MLXSW_PCI_CQE_V2)
789 			cookie_index = mlxsw_pci_cqe2_user_def_val_orig_pkt_len_get(cqe);
790 		mlxsw_skb_cb(skb)->rx_md_info.cookie_index = cookie_index;
791 	} else if (rx_info.trap_id >= MLXSW_TRAP_ID_MIRROR_SESSION0 &&
792 		   rx_info.trap_id <= MLXSW_TRAP_ID_MIRROR_SESSION7 &&
793 		   mlxsw_pci->max_cqe_ver >= MLXSW_PCI_CQE_V2) {
794 		rx_info.mirror_reason = mlxsw_pci_cqe2_mirror_reason_get(cqe);
795 		mlxsw_pci_cqe_rdq_md_init(skb, cqe);
796 	} else if (rx_info.trap_id == MLXSW_TRAP_ID_PKT_SAMPLE &&
797 		   mlxsw_pci->max_cqe_ver >= MLXSW_PCI_CQE_V2) {
798 		mlxsw_pci_cqe_rdq_md_tx_port_init(skb, cqe);
799 	}
800 
801 	mlxsw_pci_skb_cb_ts_set(mlxsw_pci, skb, cqe_v, cqe);
802 
803 	mlxsw_core_skb_receive(mlxsw_pci->core, skb, &rx_info);
804 
805 out:
806 	q->producer_counter++;
807 	return;
808 }
809 
810 static char *mlxsw_pci_cq_sw_cqe_get(struct mlxsw_pci_queue *q)
811 {
812 	struct mlxsw_pci_queue_elem_info *elem_info;
813 	char *elem;
814 	bool owner_bit;
815 
816 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
817 	elem = elem_info->elem;
818 	owner_bit = mlxsw_pci_cqe_owner_get(q->u.cq.v, elem);
819 	if (mlxsw_pci_elem_hw_owned(q, owner_bit))
820 		return NULL;
821 	q->consumer_counter++;
822 	rmb(); /* make sure we read owned bit before the rest of elem */
823 	return elem;
824 }
825 
826 static bool mlxsw_pci_cq_cqe_to_handle(struct mlxsw_pci_queue *q)
827 {
828 	struct mlxsw_pci_queue_elem_info *elem_info;
829 	bool owner_bit;
830 
831 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
832 	owner_bit = mlxsw_pci_cqe_owner_get(q->u.cq.v, elem_info->elem);
833 	return !mlxsw_pci_elem_hw_owned(q, owner_bit);
834 }
835 
836 static int mlxsw_pci_napi_poll_cq_rx(struct napi_struct *napi, int budget)
837 {
838 	struct mlxsw_pci_queue *q = container_of(napi, struct mlxsw_pci_queue,
839 						 u.cq.napi);
840 	struct mlxsw_pci_queue *rdq = q->u.cq.dq;
841 	struct mlxsw_pci *mlxsw_pci = q->pci;
842 	int work_done = 0;
843 	char *cqe;
844 
845 	/* If the budget is 0, Rx processing should be skipped. */
846 	if (unlikely(!budget))
847 		return 0;
848 
849 	while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) {
850 		u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe);
851 		u8 sendq = mlxsw_pci_cqe_sr_get(q->u.cq.v, cqe);
852 		u8 dqn = mlxsw_pci_cqe_dqn_get(q->u.cq.v, cqe);
853 
854 		if (unlikely(sendq)) {
855 			WARN_ON_ONCE(1);
856 			continue;
857 		}
858 
859 		if (unlikely(dqn != rdq->num)) {
860 			WARN_ON_ONCE(1);
861 			continue;
862 		}
863 
864 		mlxsw_pci_cqe_rdq_handle(mlxsw_pci, rdq,
865 					 wqe_counter, q->u.cq.v, cqe);
866 
867 		if (++work_done == budget)
868 			break;
869 	}
870 
871 	mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
872 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, rdq);
873 
874 	if (work_done < budget)
875 		goto processing_completed;
876 
877 	/* The driver still has outstanding work to do, budget was exhausted.
878 	 * Return exactly budget. In that case, the NAPI instance will be polled
879 	 * again.
880 	 */
881 	if (mlxsw_pci_cq_cqe_to_handle(q))
882 		goto out;
883 
884 	/* The driver processed all the completions and handled exactly
885 	 * 'budget'. Return 'budget - 1' to distinguish from the case that
886 	 * driver still has completions to handle.
887 	 */
888 	if (work_done == budget)
889 		work_done--;
890 
891 processing_completed:
892 	if (napi_complete_done(napi, work_done))
893 		mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
894 out:
895 	return work_done;
896 }
897 
898 static int mlxsw_pci_napi_poll_cq_tx(struct napi_struct *napi, int budget)
899 {
900 	struct mlxsw_pci_queue *q = container_of(napi, struct mlxsw_pci_queue,
901 						 u.cq.napi);
902 	struct mlxsw_pci_queue *sdq = q->u.cq.dq;
903 	struct mlxsw_pci *mlxsw_pci = q->pci;
904 	int work_done = 0;
905 	char *cqe;
906 
907 	while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) {
908 		u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe);
909 		u8 sendq = mlxsw_pci_cqe_sr_get(q->u.cq.v, cqe);
910 		u8 dqn = mlxsw_pci_cqe_dqn_get(q->u.cq.v, cqe);
911 		char ncqe[MLXSW_PCI_CQE_SIZE_MAX];
912 
913 		if (unlikely(!sendq)) {
914 			WARN_ON_ONCE(1);
915 			continue;
916 		}
917 
918 		if (unlikely(dqn != sdq->num)) {
919 			WARN_ON_ONCE(1);
920 			continue;
921 		}
922 
923 		memcpy(ncqe, cqe, q->elem_size);
924 		mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
925 
926 		mlxsw_pci_cqe_sdq_handle(mlxsw_pci, sdq,
927 					 wqe_counter, q->u.cq.v, ncqe, budget);
928 
929 		work_done++;
930 	}
931 
932 	/* If the budget is 0 napi_complete_done() should never be called. */
933 	if (unlikely(!budget))
934 		goto processing_completed;
935 
936 	work_done = min(work_done, budget - 1);
937 	if (unlikely(!napi_complete_done(napi, work_done)))
938 		goto out;
939 
940 processing_completed:
941 	mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
942 out:
943 	return work_done;
944 }
945 
946 static enum mlxsw_pci_cq_type
947 mlxsw_pci_cq_type(const struct mlxsw_pci *mlxsw_pci,
948 		  const struct mlxsw_pci_queue *q)
949 {
950 	/* Each CQ is mapped to one DQ. The first 'num_sdqs' queues are used
951 	 * for SDQs and the rest are used for RDQs.
952 	 */
953 	if (q->num < mlxsw_pci->num_sdqs)
954 		return MLXSW_PCI_CQ_SDQ;
955 
956 	return MLXSW_PCI_CQ_RDQ;
957 }
958 
959 static void mlxsw_pci_cq_napi_setup(struct mlxsw_pci_queue *q,
960 				    enum mlxsw_pci_cq_type cq_type)
961 {
962 	struct mlxsw_pci *mlxsw_pci = q->pci;
963 
964 	switch (cq_type) {
965 	case MLXSW_PCI_CQ_SDQ:
966 		netif_napi_add(mlxsw_pci->napi_dev_tx, &q->u.cq.napi,
967 			       mlxsw_pci_napi_poll_cq_tx);
968 		break;
969 	case MLXSW_PCI_CQ_RDQ:
970 		netif_napi_add(mlxsw_pci->napi_dev_rx, &q->u.cq.napi,
971 			       mlxsw_pci_napi_poll_cq_rx);
972 		break;
973 	}
974 }
975 
976 static void mlxsw_pci_cq_napi_teardown(struct mlxsw_pci_queue *q)
977 {
978 	netif_napi_del(&q->u.cq.napi);
979 }
980 
981 static int mlxsw_pci_cq_page_pool_init(struct mlxsw_pci_queue *q,
982 				       enum mlxsw_pci_cq_type cq_type)
983 {
984 	struct page_pool_params pp_params = {};
985 	struct mlxsw_pci *mlxsw_pci = q->pci;
986 	struct page_pool *page_pool;
987 
988 	if (cq_type != MLXSW_PCI_CQ_RDQ)
989 		return 0;
990 
991 	pp_params.flags = PP_FLAG_DMA_MAP;
992 	pp_params.pool_size = MLXSW_PCI_WQE_COUNT * mlxsw_pci->num_sg_entries;
993 	pp_params.nid = dev_to_node(&mlxsw_pci->pdev->dev);
994 	pp_params.dev = &mlxsw_pci->pdev->dev;
995 	pp_params.napi = &q->u.cq.napi;
996 	pp_params.dma_dir = DMA_FROM_DEVICE;
997 
998 	page_pool = page_pool_create(&pp_params);
999 	if (IS_ERR(page_pool))
1000 		return PTR_ERR(page_pool);
1001 
1002 	q->u.cq.page_pool = page_pool;
1003 	return 0;
1004 }
1005 
1006 static void mlxsw_pci_cq_page_pool_fini(struct mlxsw_pci_queue *q,
1007 					enum mlxsw_pci_cq_type cq_type)
1008 {
1009 	if (cq_type != MLXSW_PCI_CQ_RDQ)
1010 		return;
1011 
1012 	page_pool_destroy(q->u.cq.page_pool);
1013 }
1014 
1015 static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1016 			     struct mlxsw_pci_queue *q)
1017 {
1018 	enum mlxsw_pci_cq_type cq_type = mlxsw_pci_cq_type(mlxsw_pci, q);
1019 	int i;
1020 	int err;
1021 
1022 	q->consumer_counter = 0;
1023 
1024 	for (i = 0; i < q->count; i++) {
1025 		char *elem = mlxsw_pci_queue_elem_get(q, i);
1026 
1027 		mlxsw_pci_cqe_owner_set(q->u.cq.v, elem, 1);
1028 	}
1029 
1030 	if (q->u.cq.v == MLXSW_PCI_CQE_V1)
1031 		mlxsw_cmd_mbox_sw2hw_cq_cqe_ver_set(mbox,
1032 				MLXSW_CMD_MBOX_SW2HW_CQ_CQE_VER_1);
1033 	else if (q->u.cq.v == MLXSW_PCI_CQE_V2)
1034 		mlxsw_cmd_mbox_sw2hw_cq_cqe_ver_set(mbox,
1035 				MLXSW_CMD_MBOX_SW2HW_CQ_CQE_VER_2);
1036 
1037 	mlxsw_cmd_mbox_sw2hw_cq_c_eqn_set(mbox, MLXSW_PCI_EQ_COMP_NUM);
1038 	mlxsw_cmd_mbox_sw2hw_cq_st_set(mbox, 0);
1039 	mlxsw_cmd_mbox_sw2hw_cq_log_cq_size_set(mbox, ilog2(q->count));
1040 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
1041 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
1042 
1043 		mlxsw_cmd_mbox_sw2hw_cq_pa_set(mbox, i, mapaddr);
1044 	}
1045 	err = mlxsw_cmd_sw2hw_cq(mlxsw_pci->core, mbox, q->num);
1046 	if (err)
1047 		return err;
1048 	mlxsw_pci_cq_napi_setup(q, cq_type);
1049 
1050 	err = mlxsw_pci_cq_page_pool_init(q, cq_type);
1051 	if (err)
1052 		goto err_page_pool_init;
1053 
1054 	napi_enable(&q->u.cq.napi);
1055 	mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
1056 	mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
1057 	return 0;
1058 
1059 err_page_pool_init:
1060 	mlxsw_pci_cq_napi_teardown(q);
1061 	return err;
1062 }
1063 
1064 static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci,
1065 			      struct mlxsw_pci_queue *q)
1066 {
1067 	enum mlxsw_pci_cq_type cq_type = mlxsw_pci_cq_type(mlxsw_pci, q);
1068 
1069 	napi_disable(&q->u.cq.napi);
1070 	mlxsw_pci_cq_page_pool_fini(q, cq_type);
1071 	mlxsw_pci_cq_napi_teardown(q);
1072 	mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num);
1073 }
1074 
1075 static u16 mlxsw_pci_cq_elem_count(const struct mlxsw_pci_queue *q)
1076 {
1077 	return q->u.cq.v == MLXSW_PCI_CQE_V2 ? MLXSW_PCI_CQE2_COUNT :
1078 					     MLXSW_PCI_CQE01_COUNT;
1079 }
1080 
1081 static u8 mlxsw_pci_cq_elem_size(const struct mlxsw_pci_queue *q)
1082 {
1083 	return q->u.cq.v == MLXSW_PCI_CQE_V2 ? MLXSW_PCI_CQE2_SIZE :
1084 					       MLXSW_PCI_CQE01_SIZE;
1085 }
1086 
1087 static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q)
1088 {
1089 	struct mlxsw_pci_queue_elem_info *elem_info;
1090 	char *elem;
1091 	bool owner_bit;
1092 
1093 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
1094 	elem = elem_info->elem;
1095 	owner_bit = mlxsw_pci_eqe_owner_get(elem);
1096 	if (mlxsw_pci_elem_hw_owned(q, owner_bit))
1097 		return NULL;
1098 	q->consumer_counter++;
1099 	rmb(); /* make sure we read owned bit before the rest of elem */
1100 	return elem;
1101 }
1102 
1103 static void mlxsw_pci_eq_tasklet(struct tasklet_struct *t)
1104 {
1105 	unsigned long active_cqns[BITS_TO_LONGS(MLXSW_PCI_CQS_MAX)];
1106 	struct mlxsw_pci_queue *q = from_tasklet(q, t, u.eq.tasklet);
1107 	struct mlxsw_pci *mlxsw_pci = q->pci;
1108 	int credits = q->count >> 1;
1109 	u8 cqn, cq_count;
1110 	int items = 0;
1111 	char *eqe;
1112 
1113 	memset(&active_cqns, 0, sizeof(active_cqns));
1114 
1115 	while ((eqe = mlxsw_pci_eq_sw_eqe_get(q))) {
1116 		cqn = mlxsw_pci_eqe_cqn_get(eqe);
1117 		set_bit(cqn, active_cqns);
1118 
1119 		if (++items == credits)
1120 			break;
1121 	}
1122 
1123 	if (!items)
1124 		return;
1125 
1126 	mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
1127 	mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
1128 
1129 	cq_count = mlxsw_pci->num_cqs;
1130 	for_each_set_bit(cqn, active_cqns, cq_count) {
1131 		q = mlxsw_pci_cq_get(mlxsw_pci, cqn);
1132 		napi_schedule(&q->u.cq.napi);
1133 	}
1134 }
1135 
1136 static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1137 			     struct mlxsw_pci_queue *q)
1138 {
1139 	int i;
1140 	int err;
1141 
1142 	/* We expect to initialize only one EQ, which gets num=0 as it is
1143 	 * located at index zero. We use the EQ as EQ1, so set the number for
1144 	 * future use.
1145 	 */
1146 	WARN_ON_ONCE(q->num);
1147 	q->num = MLXSW_PCI_EQ_COMP_NUM;
1148 
1149 	q->consumer_counter = 0;
1150 
1151 	for (i = 0; i < q->count; i++) {
1152 		char *elem = mlxsw_pci_queue_elem_get(q, i);
1153 
1154 		mlxsw_pci_eqe_owner_set(elem, 1);
1155 	}
1156 
1157 	mlxsw_cmd_mbox_sw2hw_eq_int_msix_set(mbox, 1); /* MSI-X used */
1158 	mlxsw_cmd_mbox_sw2hw_eq_st_set(mbox, 1); /* armed */
1159 	mlxsw_cmd_mbox_sw2hw_eq_log_eq_size_set(mbox, ilog2(q->count));
1160 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
1161 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
1162 
1163 		mlxsw_cmd_mbox_sw2hw_eq_pa_set(mbox, i, mapaddr);
1164 	}
1165 	err = mlxsw_cmd_sw2hw_eq(mlxsw_pci->core, mbox, q->num);
1166 	if (err)
1167 		return err;
1168 	tasklet_setup(&q->u.eq.tasklet, mlxsw_pci_eq_tasklet);
1169 	mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
1170 	mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
1171 	return 0;
1172 }
1173 
1174 static void mlxsw_pci_eq_fini(struct mlxsw_pci *mlxsw_pci,
1175 			      struct mlxsw_pci_queue *q)
1176 {
1177 	mlxsw_cmd_hw2sw_eq(mlxsw_pci->core, q->num);
1178 }
1179 
1180 struct mlxsw_pci_queue_ops {
1181 	const char *name;
1182 	enum mlxsw_pci_queue_type type;
1183 	void (*pre_init)(struct mlxsw_pci *mlxsw_pci,
1184 			 struct mlxsw_pci_queue *q);
1185 	int (*init)(struct mlxsw_pci *mlxsw_pci, char *mbox,
1186 		    struct mlxsw_pci_queue *q);
1187 	void (*fini)(struct mlxsw_pci *mlxsw_pci,
1188 		     struct mlxsw_pci_queue *q);
1189 	u16 (*elem_count_f)(const struct mlxsw_pci_queue *q);
1190 	u8 (*elem_size_f)(const struct mlxsw_pci_queue *q);
1191 	u16 elem_count;
1192 	u8 elem_size;
1193 };
1194 
1195 static const struct mlxsw_pci_queue_ops mlxsw_pci_sdq_ops = {
1196 	.type		= MLXSW_PCI_QUEUE_TYPE_SDQ,
1197 	.init		= mlxsw_pci_sdq_init,
1198 	.fini		= mlxsw_pci_sdq_fini,
1199 	.elem_count	= MLXSW_PCI_WQE_COUNT,
1200 	.elem_size	= MLXSW_PCI_WQE_SIZE,
1201 };
1202 
1203 static const struct mlxsw_pci_queue_ops mlxsw_pci_rdq_ops = {
1204 	.type		= MLXSW_PCI_QUEUE_TYPE_RDQ,
1205 	.init		= mlxsw_pci_rdq_init,
1206 	.fini		= mlxsw_pci_rdq_fini,
1207 	.elem_count	= MLXSW_PCI_WQE_COUNT,
1208 	.elem_size	= MLXSW_PCI_WQE_SIZE
1209 };
1210 
1211 static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = {
1212 	.type		= MLXSW_PCI_QUEUE_TYPE_CQ,
1213 	.pre_init	= mlxsw_pci_cq_pre_init,
1214 	.init		= mlxsw_pci_cq_init,
1215 	.fini		= mlxsw_pci_cq_fini,
1216 	.elem_count_f	= mlxsw_pci_cq_elem_count,
1217 	.elem_size_f	= mlxsw_pci_cq_elem_size
1218 };
1219 
1220 static const struct mlxsw_pci_queue_ops mlxsw_pci_eq_ops = {
1221 	.type		= MLXSW_PCI_QUEUE_TYPE_EQ,
1222 	.init		= mlxsw_pci_eq_init,
1223 	.fini		= mlxsw_pci_eq_fini,
1224 	.elem_count	= MLXSW_PCI_EQE_COUNT,
1225 	.elem_size	= MLXSW_PCI_EQE_SIZE
1226 };
1227 
1228 static int mlxsw_pci_queue_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1229 				const struct mlxsw_pci_queue_ops *q_ops,
1230 				struct mlxsw_pci_queue *q, u8 q_num)
1231 {
1232 	struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
1233 	int i;
1234 	int err;
1235 
1236 	q->num = q_num;
1237 	if (q_ops->pre_init)
1238 		q_ops->pre_init(mlxsw_pci, q);
1239 
1240 	spin_lock_init(&q->lock);
1241 	q->count = q_ops->elem_count_f ? q_ops->elem_count_f(q) :
1242 					 q_ops->elem_count;
1243 	q->elem_size = q_ops->elem_size_f ? q_ops->elem_size_f(q) :
1244 					    q_ops->elem_size;
1245 	q->type = q_ops->type;
1246 	q->pci = mlxsw_pci;
1247 
1248 	mem_item->size = MLXSW_PCI_AQ_SIZE;
1249 	mem_item->buf = dma_alloc_coherent(&mlxsw_pci->pdev->dev,
1250 					   mem_item->size, &mem_item->mapaddr,
1251 					   GFP_KERNEL);
1252 	if (!mem_item->buf)
1253 		return -ENOMEM;
1254 
1255 	q->elem_info = kcalloc(q->count, sizeof(*q->elem_info), GFP_KERNEL);
1256 	if (!q->elem_info) {
1257 		err = -ENOMEM;
1258 		goto err_elem_info_alloc;
1259 	}
1260 
1261 	/* Initialize dma mapped elements info elem_info for
1262 	 * future easy access.
1263 	 */
1264 	for (i = 0; i < q->count; i++) {
1265 		struct mlxsw_pci_queue_elem_info *elem_info;
1266 
1267 		elem_info = mlxsw_pci_queue_elem_info_get(q, i);
1268 		elem_info->elem =
1269 			__mlxsw_pci_queue_elem_get(q, q->elem_size, i);
1270 	}
1271 
1272 	mlxsw_cmd_mbox_zero(mbox);
1273 	err = q_ops->init(mlxsw_pci, mbox, q);
1274 	if (err)
1275 		goto err_q_ops_init;
1276 	return 0;
1277 
1278 err_q_ops_init:
1279 	kfree(q->elem_info);
1280 err_elem_info_alloc:
1281 	dma_free_coherent(&mlxsw_pci->pdev->dev, mem_item->size,
1282 			  mem_item->buf, mem_item->mapaddr);
1283 	return err;
1284 }
1285 
1286 static void mlxsw_pci_queue_fini(struct mlxsw_pci *mlxsw_pci,
1287 				 const struct mlxsw_pci_queue_ops *q_ops,
1288 				 struct mlxsw_pci_queue *q)
1289 {
1290 	struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
1291 
1292 	q_ops->fini(mlxsw_pci, q);
1293 	kfree(q->elem_info);
1294 	dma_free_coherent(&mlxsw_pci->pdev->dev, mem_item->size,
1295 			  mem_item->buf, mem_item->mapaddr);
1296 }
1297 
1298 static int mlxsw_pci_queue_group_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1299 				      const struct mlxsw_pci_queue_ops *q_ops,
1300 				      u8 num_qs)
1301 {
1302 	struct mlxsw_pci_queue_type_group *queue_group;
1303 	int i;
1304 	int err;
1305 
1306 	queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
1307 	queue_group->q = kcalloc(num_qs, sizeof(*queue_group->q), GFP_KERNEL);
1308 	if (!queue_group->q)
1309 		return -ENOMEM;
1310 
1311 	for (i = 0; i < num_qs; i++) {
1312 		err = mlxsw_pci_queue_init(mlxsw_pci, mbox, q_ops,
1313 					   &queue_group->q[i], i);
1314 		if (err)
1315 			goto err_queue_init;
1316 	}
1317 	queue_group->count = num_qs;
1318 
1319 	return 0;
1320 
1321 err_queue_init:
1322 	for (i--; i >= 0; i--)
1323 		mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1324 	kfree(queue_group->q);
1325 	return err;
1326 }
1327 
1328 static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,
1329 				       const struct mlxsw_pci_queue_ops *q_ops)
1330 {
1331 	struct mlxsw_pci_queue_type_group *queue_group;
1332 	int i;
1333 
1334 	queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
1335 	for (i = 0; i < queue_group->count; i++)
1336 		mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1337 	kfree(queue_group->q);
1338 }
1339 
1340 static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
1341 {
1342 	struct pci_dev *pdev = mlxsw_pci->pdev;
1343 	u8 num_sdqs;
1344 	u8 sdq_log2sz;
1345 	u8 num_rdqs;
1346 	u8 rdq_log2sz;
1347 	u8 num_cqs;
1348 	u8 cq_log2sz;
1349 	u8 cqv2_log2sz;
1350 	u8 num_eqs;
1351 	u8 eq_log2sz;
1352 	int err;
1353 
1354 	mlxsw_cmd_mbox_zero(mbox);
1355 	err = mlxsw_cmd_query_aq_cap(mlxsw_pci->core, mbox);
1356 	if (err)
1357 		return err;
1358 
1359 	num_sdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_sdqs_get(mbox);
1360 	sdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_sdq_sz_get(mbox);
1361 	num_rdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_rdqs_get(mbox);
1362 	rdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_rdq_sz_get(mbox);
1363 	num_cqs = mlxsw_cmd_mbox_query_aq_cap_max_num_cqs_get(mbox);
1364 	cq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cq_sz_get(mbox);
1365 	cqv2_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cqv2_sz_get(mbox);
1366 	num_eqs = mlxsw_cmd_mbox_query_aq_cap_max_num_eqs_get(mbox);
1367 	eq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_eq_sz_get(mbox);
1368 
1369 	if (num_sdqs + num_rdqs > num_cqs ||
1370 	    num_sdqs < MLXSW_PCI_SDQS_MIN ||
1371 	    num_cqs > MLXSW_PCI_CQS_MAX || num_eqs != MLXSW_PCI_EQS_MAX) {
1372 		dev_err(&pdev->dev, "Unsupported number of queues\n");
1373 		return -EINVAL;
1374 	}
1375 
1376 	if ((1 << sdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1377 	    (1 << rdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1378 	    (1 << cq_log2sz != MLXSW_PCI_CQE01_COUNT) ||
1379 	    (mlxsw_pci->max_cqe_ver == MLXSW_PCI_CQE_V2 &&
1380 	     (1 << cqv2_log2sz != MLXSW_PCI_CQE2_COUNT)) ||
1381 	    (1 << eq_log2sz != MLXSW_PCI_EQE_COUNT)) {
1382 		dev_err(&pdev->dev, "Unsupported number of async queue descriptors\n");
1383 		return -EINVAL;
1384 	}
1385 
1386 	mlxsw_pci->num_cqs = num_cqs;
1387 	mlxsw_pci->num_sdqs = num_sdqs;
1388 
1389 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_eq_ops,
1390 					 MLXSW_PCI_EQS_COUNT);
1391 	if (err) {
1392 		dev_err(&pdev->dev, "Failed to initialize event queues\n");
1393 		return err;
1394 	}
1395 
1396 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_cq_ops,
1397 					 num_cqs);
1398 	if (err) {
1399 		dev_err(&pdev->dev, "Failed to initialize completion queues\n");
1400 		goto err_cqs_init;
1401 	}
1402 
1403 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_sdq_ops,
1404 					 num_sdqs);
1405 	if (err) {
1406 		dev_err(&pdev->dev, "Failed to initialize send descriptor queues\n");
1407 		goto err_sdqs_init;
1408 	}
1409 
1410 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_rdq_ops,
1411 					 num_rdqs);
1412 	if (err) {
1413 		dev_err(&pdev->dev, "Failed to initialize receive descriptor queues\n");
1414 		goto err_rdqs_init;
1415 	}
1416 
1417 	return 0;
1418 
1419 err_rdqs_init:
1420 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1421 err_sdqs_init:
1422 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1423 err_cqs_init:
1424 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1425 	return err;
1426 }
1427 
1428 static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci)
1429 {
1430 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops);
1431 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1432 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1433 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1434 }
1435 
1436 static void
1437 mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci,
1438 				     char *mbox, int index,
1439 				     const struct mlxsw_swid_config *swid)
1440 {
1441 	u8 mask = 0;
1442 
1443 	if (swid->used_type) {
1444 		mlxsw_cmd_mbox_config_profile_swid_config_type_set(
1445 			mbox, index, swid->type);
1446 		mask |= 1;
1447 	}
1448 	if (swid->used_properties) {
1449 		mlxsw_cmd_mbox_config_profile_swid_config_properties_set(
1450 			mbox, index, swid->properties);
1451 		mask |= 2;
1452 	}
1453 	mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask);
1454 }
1455 
1456 static int
1457 mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_pci *mlxsw_pci,
1458 				const struct mlxsw_config_profile *profile,
1459 				struct mlxsw_res *res)
1460 {
1461 	u64 single_size, double_size, linear_size;
1462 	int err;
1463 
1464 	err = mlxsw_core_kvd_sizes_get(mlxsw_pci->core, profile,
1465 				       &single_size, &double_size,
1466 				       &linear_size);
1467 	if (err)
1468 		return err;
1469 
1470 	MLXSW_RES_SET(res, KVD_SINGLE_SIZE, single_size);
1471 	MLXSW_RES_SET(res, KVD_DOUBLE_SIZE, double_size);
1472 	MLXSW_RES_SET(res, KVD_LINEAR_SIZE, linear_size);
1473 
1474 	return 0;
1475 }
1476 
1477 static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
1478 				    const struct mlxsw_config_profile *profile,
1479 				    struct mlxsw_res *res)
1480 {
1481 	int i;
1482 	int err;
1483 
1484 	mlxsw_cmd_mbox_zero(mbox);
1485 
1486 	if (profile->used_max_vepa_channels) {
1487 		mlxsw_cmd_mbox_config_profile_set_max_vepa_channels_set(
1488 			mbox, 1);
1489 		mlxsw_cmd_mbox_config_profile_max_vepa_channels_set(
1490 			mbox, profile->max_vepa_channels);
1491 	}
1492 	if (profile->used_max_lag) {
1493 		mlxsw_cmd_mbox_config_profile_set_max_lag_set(mbox, 1);
1494 		mlxsw_cmd_mbox_config_profile_max_lag_set(mbox,
1495 							  profile->max_lag);
1496 	}
1497 	if (profile->used_max_mid) {
1498 		mlxsw_cmd_mbox_config_profile_set_max_mid_set(
1499 			mbox, 1);
1500 		mlxsw_cmd_mbox_config_profile_max_mid_set(
1501 			mbox, profile->max_mid);
1502 	}
1503 	if (profile->used_max_pgt) {
1504 		mlxsw_cmd_mbox_config_profile_set_max_pgt_set(
1505 			mbox, 1);
1506 		mlxsw_cmd_mbox_config_profile_max_pgt_set(
1507 			mbox, profile->max_pgt);
1508 	}
1509 	if (profile->used_max_system_port) {
1510 		mlxsw_cmd_mbox_config_profile_set_max_system_port_set(
1511 			mbox, 1);
1512 		mlxsw_cmd_mbox_config_profile_max_system_port_set(
1513 			mbox, profile->max_system_port);
1514 	}
1515 	if (profile->used_max_vlan_groups) {
1516 		mlxsw_cmd_mbox_config_profile_set_max_vlan_groups_set(
1517 			mbox, 1);
1518 		mlxsw_cmd_mbox_config_profile_max_vlan_groups_set(
1519 			mbox, profile->max_vlan_groups);
1520 	}
1521 	if (profile->used_max_regions) {
1522 		mlxsw_cmd_mbox_config_profile_set_max_regions_set(
1523 			mbox, 1);
1524 		mlxsw_cmd_mbox_config_profile_max_regions_set(
1525 			mbox, profile->max_regions);
1526 	}
1527 	if (profile->used_flood_tables) {
1528 		mlxsw_cmd_mbox_config_profile_set_flood_tables_set(
1529 			mbox, 1);
1530 		mlxsw_cmd_mbox_config_profile_max_flood_tables_set(
1531 			mbox, profile->max_flood_tables);
1532 		mlxsw_cmd_mbox_config_profile_max_vid_flood_tables_set(
1533 			mbox, profile->max_vid_flood_tables);
1534 		mlxsw_cmd_mbox_config_profile_max_fid_offset_flood_tables_set(
1535 			mbox, profile->max_fid_offset_flood_tables);
1536 		mlxsw_cmd_mbox_config_profile_fid_offset_flood_table_size_set(
1537 			mbox, profile->fid_offset_flood_table_size);
1538 		mlxsw_cmd_mbox_config_profile_max_fid_flood_tables_set(
1539 			mbox, profile->max_fid_flood_tables);
1540 		mlxsw_cmd_mbox_config_profile_fid_flood_table_size_set(
1541 			mbox, profile->fid_flood_table_size);
1542 	}
1543 	if (profile->flood_mode_prefer_cff && mlxsw_pci->cff_support) {
1544 		enum mlxsw_cmd_mbox_config_profile_flood_mode flood_mode =
1545 			MLXSW_CMD_MBOX_CONFIG_PROFILE_FLOOD_MODE_CFF;
1546 
1547 		mlxsw_cmd_mbox_config_profile_set_flood_mode_set(mbox, 1);
1548 		mlxsw_cmd_mbox_config_profile_flood_mode_set(mbox, flood_mode);
1549 		mlxsw_pci->flood_mode = flood_mode;
1550 	} else if (profile->used_flood_mode) {
1551 		mlxsw_cmd_mbox_config_profile_set_flood_mode_set(
1552 			mbox, 1);
1553 		mlxsw_cmd_mbox_config_profile_flood_mode_set(
1554 			mbox, profile->flood_mode);
1555 		mlxsw_pci->flood_mode = profile->flood_mode;
1556 	} else {
1557 		WARN_ON(1);
1558 		return -EINVAL;
1559 	}
1560 	if (profile->used_max_ib_mc) {
1561 		mlxsw_cmd_mbox_config_profile_set_max_ib_mc_set(
1562 			mbox, 1);
1563 		mlxsw_cmd_mbox_config_profile_max_ib_mc_set(
1564 			mbox, profile->max_ib_mc);
1565 	}
1566 	if (profile->used_max_pkey) {
1567 		mlxsw_cmd_mbox_config_profile_set_max_pkey_set(
1568 			mbox, 1);
1569 		mlxsw_cmd_mbox_config_profile_max_pkey_set(
1570 			mbox, profile->max_pkey);
1571 	}
1572 	if (profile->used_ar_sec) {
1573 		mlxsw_cmd_mbox_config_profile_set_ar_sec_set(
1574 			mbox, 1);
1575 		mlxsw_cmd_mbox_config_profile_ar_sec_set(
1576 			mbox, profile->ar_sec);
1577 	}
1578 	if (profile->used_adaptive_routing_group_cap) {
1579 		mlxsw_cmd_mbox_config_profile_set_adaptive_routing_group_cap_set(
1580 			mbox, 1);
1581 		mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set(
1582 			mbox, profile->adaptive_routing_group_cap);
1583 	}
1584 	if (profile->used_ubridge) {
1585 		mlxsw_cmd_mbox_config_profile_set_ubridge_set(mbox, 1);
1586 		mlxsw_cmd_mbox_config_profile_ubridge_set(mbox,
1587 							  profile->ubridge);
1588 	}
1589 	if (profile->used_kvd_sizes && MLXSW_RES_VALID(res, KVD_SIZE)) {
1590 		err = mlxsw_pci_profile_get_kvd_sizes(mlxsw_pci, profile, res);
1591 		if (err)
1592 			return err;
1593 
1594 		mlxsw_cmd_mbox_config_profile_set_kvd_linear_size_set(mbox, 1);
1595 		mlxsw_cmd_mbox_config_profile_kvd_linear_size_set(mbox,
1596 					MLXSW_RES_GET(res, KVD_LINEAR_SIZE));
1597 		mlxsw_cmd_mbox_config_profile_set_kvd_hash_single_size_set(mbox,
1598 									   1);
1599 		mlxsw_cmd_mbox_config_profile_kvd_hash_single_size_set(mbox,
1600 					MLXSW_RES_GET(res, KVD_SINGLE_SIZE));
1601 		mlxsw_cmd_mbox_config_profile_set_kvd_hash_double_size_set(
1602 								mbox, 1);
1603 		mlxsw_cmd_mbox_config_profile_kvd_hash_double_size_set(mbox,
1604 					MLXSW_RES_GET(res, KVD_DOUBLE_SIZE));
1605 	}
1606 
1607 	for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++)
1608 		mlxsw_pci_config_profile_swid_config(mlxsw_pci, mbox, i,
1609 						     &profile->swid_config[i]);
1610 
1611 	if (mlxsw_pci->max_cqe_ver > MLXSW_PCI_CQE_V0) {
1612 		mlxsw_cmd_mbox_config_profile_set_cqe_version_set(mbox, 1);
1613 		mlxsw_cmd_mbox_config_profile_cqe_version_set(mbox, 1);
1614 	}
1615 
1616 	if (profile->used_cqe_time_stamp_type) {
1617 		mlxsw_cmd_mbox_config_profile_set_cqe_time_stamp_type_set(mbox,
1618 									  1);
1619 		mlxsw_cmd_mbox_config_profile_cqe_time_stamp_type_set(mbox,
1620 					profile->cqe_time_stamp_type);
1621 	}
1622 
1623 	if (profile->lag_mode_prefer_sw && mlxsw_pci->lag_mode_support) {
1624 		enum mlxsw_cmd_mbox_config_profile_lag_mode lag_mode =
1625 			MLXSW_CMD_MBOX_CONFIG_PROFILE_LAG_MODE_SW;
1626 
1627 		mlxsw_cmd_mbox_config_profile_set_lag_mode_set(mbox, 1);
1628 		mlxsw_cmd_mbox_config_profile_lag_mode_set(mbox, lag_mode);
1629 		mlxsw_pci->lag_mode = lag_mode;
1630 	} else {
1631 		mlxsw_pci->lag_mode = MLXSW_CMD_MBOX_CONFIG_PROFILE_LAG_MODE_FW;
1632 	}
1633 	return mlxsw_cmd_config_profile_set(mlxsw_pci->core, mbox);
1634 }
1635 
1636 static int mlxsw_pci_boardinfo(struct mlxsw_pci *mlxsw_pci, char *mbox)
1637 {
1638 	struct mlxsw_bus_info *bus_info = &mlxsw_pci->bus_info;
1639 	int err;
1640 
1641 	mlxsw_cmd_mbox_zero(mbox);
1642 	err = mlxsw_cmd_boardinfo(mlxsw_pci->core, mbox);
1643 	if (err)
1644 		return err;
1645 	mlxsw_cmd_mbox_boardinfo_vsd_memcpy_from(mbox, bus_info->vsd);
1646 	mlxsw_cmd_mbox_boardinfo_psid_memcpy_from(mbox, bus_info->psid);
1647 	return 0;
1648 }
1649 
1650 static int mlxsw_pci_fw_area_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1651 				  u16 num_pages)
1652 {
1653 	struct mlxsw_pci_mem_item *mem_item;
1654 	int nent = 0;
1655 	int i;
1656 	int err;
1657 
1658 	mlxsw_pci->fw_area.items = kcalloc(num_pages, sizeof(*mem_item),
1659 					   GFP_KERNEL);
1660 	if (!mlxsw_pci->fw_area.items)
1661 		return -ENOMEM;
1662 	mlxsw_pci->fw_area.count = num_pages;
1663 
1664 	mlxsw_cmd_mbox_zero(mbox);
1665 	for (i = 0; i < num_pages; i++) {
1666 		mem_item = &mlxsw_pci->fw_area.items[i];
1667 
1668 		mem_item->size = MLXSW_PCI_PAGE_SIZE;
1669 		mem_item->buf = dma_alloc_coherent(&mlxsw_pci->pdev->dev,
1670 						   mem_item->size,
1671 						   &mem_item->mapaddr, GFP_KERNEL);
1672 		if (!mem_item->buf) {
1673 			err = -ENOMEM;
1674 			goto err_alloc;
1675 		}
1676 		mlxsw_cmd_mbox_map_fa_pa_set(mbox, nent, mem_item->mapaddr);
1677 		mlxsw_cmd_mbox_map_fa_log2size_set(mbox, nent, 0); /* 1 page */
1678 		if (++nent == MLXSW_CMD_MAP_FA_VPM_ENTRIES_MAX) {
1679 			err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1680 			if (err)
1681 				goto err_cmd_map_fa;
1682 			nent = 0;
1683 			mlxsw_cmd_mbox_zero(mbox);
1684 		}
1685 	}
1686 
1687 	if (nent) {
1688 		err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1689 		if (err)
1690 			goto err_cmd_map_fa;
1691 	}
1692 
1693 	return 0;
1694 
1695 err_cmd_map_fa:
1696 err_alloc:
1697 	for (i--; i >= 0; i--) {
1698 		mem_item = &mlxsw_pci->fw_area.items[i];
1699 
1700 		dma_free_coherent(&mlxsw_pci->pdev->dev, mem_item->size,
1701 				  mem_item->buf, mem_item->mapaddr);
1702 	}
1703 	kfree(mlxsw_pci->fw_area.items);
1704 	return err;
1705 }
1706 
1707 static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci)
1708 {
1709 	struct mlxsw_pci_mem_item *mem_item;
1710 	int i;
1711 
1712 	mlxsw_cmd_unmap_fa(mlxsw_pci->core);
1713 
1714 	for (i = 0; i < mlxsw_pci->fw_area.count; i++) {
1715 		mem_item = &mlxsw_pci->fw_area.items[i];
1716 
1717 		dma_free_coherent(&mlxsw_pci->pdev->dev, mem_item->size,
1718 				  mem_item->buf, mem_item->mapaddr);
1719 	}
1720 	kfree(mlxsw_pci->fw_area.items);
1721 }
1722 
1723 static irqreturn_t mlxsw_pci_eq_irq_handler(int irq, void *dev_id)
1724 {
1725 	struct mlxsw_pci *mlxsw_pci = dev_id;
1726 	struct mlxsw_pci_queue *q;
1727 
1728 	q = mlxsw_pci_eq_get(mlxsw_pci);
1729 	tasklet_schedule(&q->u.eq.tasklet);
1730 	return IRQ_HANDLED;
1731 }
1732 
1733 static int mlxsw_pci_mbox_alloc(struct mlxsw_pci *mlxsw_pci,
1734 				struct mlxsw_pci_mem_item *mbox)
1735 {
1736 	struct pci_dev *pdev = mlxsw_pci->pdev;
1737 	int err = 0;
1738 
1739 	mbox->size = MLXSW_CMD_MBOX_SIZE;
1740 	mbox->buf = dma_alloc_coherent(&pdev->dev, MLXSW_CMD_MBOX_SIZE,
1741 				       &mbox->mapaddr, GFP_KERNEL);
1742 	if (!mbox->buf) {
1743 		dev_err(&pdev->dev, "Failed allocating memory for mailbox\n");
1744 		err = -ENOMEM;
1745 	}
1746 
1747 	return err;
1748 }
1749 
1750 static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci,
1751 				struct mlxsw_pci_mem_item *mbox)
1752 {
1753 	struct pci_dev *pdev = mlxsw_pci->pdev;
1754 
1755 	dma_free_coherent(&pdev->dev, MLXSW_CMD_MBOX_SIZE, mbox->buf,
1756 			  mbox->mapaddr);
1757 }
1758 
1759 static int mlxsw_pci_sys_ready_wait(struct mlxsw_pci *mlxsw_pci,
1760 				    const struct pci_device_id *id,
1761 				    u32 *p_sys_status)
1762 {
1763 	unsigned long end;
1764 	u32 val;
1765 
1766 	/* We must wait for the HW to become responsive. */
1767 	msleep(MLXSW_PCI_SW_RESET_WAIT_MSECS);
1768 
1769 	end = jiffies + msecs_to_jiffies(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
1770 	do {
1771 		val = mlxsw_pci_read32(mlxsw_pci, FW_READY);
1772 		if ((val & MLXSW_PCI_FW_READY_MASK) == MLXSW_PCI_FW_READY_MAGIC)
1773 			return 0;
1774 		cond_resched();
1775 	} while (time_before(jiffies, end));
1776 
1777 	*p_sys_status = val & MLXSW_PCI_FW_READY_MASK;
1778 
1779 	return -EBUSY;
1780 }
1781 
1782 static int mlxsw_pci_reset_at_pci_disable(struct mlxsw_pci *mlxsw_pci,
1783 					  bool pci_reset_sbr_supported)
1784 {
1785 	struct pci_dev *pdev = mlxsw_pci->pdev;
1786 	char mrsr_pl[MLXSW_REG_MRSR_LEN];
1787 	struct pci_dev *bridge;
1788 	int err;
1789 
1790 	if (!pci_reset_sbr_supported) {
1791 		pci_dbg(pdev, "Performing PCI hot reset instead of \"all reset\"\n");
1792 		goto sbr;
1793 	}
1794 
1795 	mlxsw_reg_mrsr_pack(mrsr_pl,
1796 			    MLXSW_REG_MRSR_COMMAND_RESET_AT_PCI_DISABLE);
1797 	err = mlxsw_reg_write(mlxsw_pci->core, MLXSW_REG(mrsr), mrsr_pl);
1798 	if (err)
1799 		return err;
1800 
1801 sbr:
1802 	device_lock_assert(&pdev->dev);
1803 
1804 	bridge = pci_upstream_bridge(pdev);
1805 	if (bridge)
1806 		pci_cfg_access_lock(bridge);
1807 	pci_cfg_access_lock(pdev);
1808 	pci_save_state(pdev);
1809 
1810 	err = __pci_reset_function_locked(pdev);
1811 	if (err)
1812 		pci_err(pdev, "PCI function reset failed with %d\n", err);
1813 
1814 	pci_restore_state(pdev);
1815 	pci_cfg_access_unlock(pdev);
1816 	if (bridge)
1817 		pci_cfg_access_unlock(bridge);
1818 
1819 	return err;
1820 }
1821 
1822 static int mlxsw_pci_reset_sw(struct mlxsw_pci *mlxsw_pci)
1823 {
1824 	char mrsr_pl[MLXSW_REG_MRSR_LEN];
1825 
1826 	mlxsw_reg_mrsr_pack(mrsr_pl, MLXSW_REG_MRSR_COMMAND_SOFTWARE_RESET);
1827 	return mlxsw_reg_write(mlxsw_pci->core, MLXSW_REG(mrsr), mrsr_pl);
1828 }
1829 
1830 static int
1831 mlxsw_pci_reset(struct mlxsw_pci *mlxsw_pci, const struct pci_device_id *id)
1832 {
1833 	struct pci_dev *pdev = mlxsw_pci->pdev;
1834 	bool pci_reset_sbr_supported = false;
1835 	char mcam_pl[MLXSW_REG_MCAM_LEN];
1836 	bool pci_reset_supported = false;
1837 	u32 sys_status;
1838 	int err;
1839 
1840 	err = mlxsw_pci_sys_ready_wait(mlxsw_pci, id, &sys_status);
1841 	if (err) {
1842 		dev_err(&pdev->dev, "Failed to reach system ready status before reset. Status is 0x%x\n",
1843 			sys_status);
1844 		return err;
1845 	}
1846 
1847 	/* PCI core already issued a PCI reset, do not issue another reset. */
1848 	if (mlxsw_pci->skip_reset)
1849 		return 0;
1850 
1851 	mlxsw_reg_mcam_pack(mcam_pl,
1852 			    MLXSW_REG_MCAM_FEATURE_GROUP_ENHANCED_FEATURES);
1853 	err = mlxsw_reg_query(mlxsw_pci->core, MLXSW_REG(mcam), mcam_pl);
1854 	if (!err) {
1855 		mlxsw_reg_mcam_unpack(mcam_pl, MLXSW_REG_MCAM_PCI_RESET,
1856 				      &pci_reset_supported);
1857 		mlxsw_reg_mcam_unpack(mcam_pl, MLXSW_REG_MCAM_PCI_RESET_SBR,
1858 				      &pci_reset_sbr_supported);
1859 	}
1860 
1861 	if (pci_reset_supported) {
1862 		pci_dbg(pdev, "Starting PCI reset flow\n");
1863 		err = mlxsw_pci_reset_at_pci_disable(mlxsw_pci,
1864 						     pci_reset_sbr_supported);
1865 	} else {
1866 		pci_dbg(pdev, "Starting software reset flow\n");
1867 		err = mlxsw_pci_reset_sw(mlxsw_pci);
1868 	}
1869 	if (err)
1870 		return err;
1871 
1872 	err = mlxsw_pci_sys_ready_wait(mlxsw_pci, id, &sys_status);
1873 	if (err) {
1874 		dev_err(&pdev->dev, "Failed to reach system ready status after reset. Status is 0x%x\n",
1875 			sys_status);
1876 		return err;
1877 	}
1878 
1879 	return 0;
1880 }
1881 
1882 static int mlxsw_pci_alloc_irq_vectors(struct mlxsw_pci *mlxsw_pci)
1883 {
1884 	int err;
1885 
1886 	err = pci_alloc_irq_vectors(mlxsw_pci->pdev, 1, 1, PCI_IRQ_MSIX);
1887 	if (err < 0)
1888 		dev_err(&mlxsw_pci->pdev->dev, "MSI-X init failed\n");
1889 	return err;
1890 }
1891 
1892 static void mlxsw_pci_free_irq_vectors(struct mlxsw_pci *mlxsw_pci)
1893 {
1894 	pci_free_irq_vectors(mlxsw_pci->pdev);
1895 }
1896 
1897 static void mlxsw_pci_num_sg_entries_set(struct mlxsw_pci *mlxsw_pci)
1898 {
1899 	u8 num_sg_entries;
1900 
1901 	num_sg_entries = mlxsw_pci_num_sg_entries_get(MLXSW_PORT_MAX_MTU);
1902 	mlxsw_pci->num_sg_entries = min(num_sg_entries,
1903 					MLXSW_PCI_WQE_SG_ENTRIES);
1904 
1905 	WARN_ON(num_sg_entries > MLXSW_PCI_WQE_SG_ENTRIES);
1906 }
1907 
1908 static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
1909 			  const struct mlxsw_config_profile *profile,
1910 			  struct mlxsw_res *res)
1911 {
1912 	struct mlxsw_pci *mlxsw_pci = bus_priv;
1913 	struct pci_dev *pdev = mlxsw_pci->pdev;
1914 	char *mbox;
1915 	u16 num_pages;
1916 	int err;
1917 
1918 	mlxsw_pci->core = mlxsw_core;
1919 
1920 	mbox = mlxsw_cmd_mbox_alloc();
1921 	if (!mbox)
1922 		return -ENOMEM;
1923 
1924 	err = mlxsw_pci_reset(mlxsw_pci, mlxsw_pci->id);
1925 	if (err)
1926 		goto err_reset;
1927 
1928 	err = mlxsw_pci_alloc_irq_vectors(mlxsw_pci);
1929 	if (err < 0) {
1930 		dev_err(&pdev->dev, "MSI-X init failed\n");
1931 		goto err_alloc_irq;
1932 	}
1933 
1934 	err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
1935 	if (err)
1936 		goto err_query_fw;
1937 
1938 	mlxsw_pci->bus_info.fw_rev.major =
1939 		mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
1940 	mlxsw_pci->bus_info.fw_rev.minor =
1941 		mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
1942 	mlxsw_pci->bus_info.fw_rev.subminor =
1943 		mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
1944 
1945 	if (mlxsw_cmd_mbox_query_fw_cmd_interface_rev_get(mbox) != 1) {
1946 		dev_err(&pdev->dev, "Unsupported cmd interface revision ID queried from hw\n");
1947 		err = -EINVAL;
1948 		goto err_iface_rev;
1949 	}
1950 	if (mlxsw_cmd_mbox_query_fw_doorbell_page_bar_get(mbox) != 0) {
1951 		dev_err(&pdev->dev, "Unsupported doorbell page bar queried from hw\n");
1952 		err = -EINVAL;
1953 		goto err_doorbell_page_bar;
1954 	}
1955 
1956 	mlxsw_pci->doorbell_offset =
1957 		mlxsw_cmd_mbox_query_fw_doorbell_page_offset_get(mbox);
1958 
1959 	if (mlxsw_cmd_mbox_query_fw_fr_rn_clk_bar_get(mbox) != 0) {
1960 		dev_err(&pdev->dev, "Unsupported free running clock BAR queried from hw\n");
1961 		err = -EINVAL;
1962 		goto err_fr_rn_clk_bar;
1963 	}
1964 
1965 	mlxsw_pci->free_running_clock_offset =
1966 		mlxsw_cmd_mbox_query_fw_free_running_clock_offset_get(mbox);
1967 
1968 	if (mlxsw_cmd_mbox_query_fw_utc_sec_bar_get(mbox) != 0) {
1969 		dev_err(&pdev->dev, "Unsupported UTC sec BAR queried from hw\n");
1970 		err = -EINVAL;
1971 		goto err_utc_sec_bar;
1972 	}
1973 
1974 	mlxsw_pci->utc_sec_offset =
1975 		mlxsw_cmd_mbox_query_fw_utc_sec_offset_get(mbox);
1976 
1977 	if (mlxsw_cmd_mbox_query_fw_utc_nsec_bar_get(mbox) != 0) {
1978 		dev_err(&pdev->dev, "Unsupported UTC nsec BAR queried from hw\n");
1979 		err = -EINVAL;
1980 		goto err_utc_nsec_bar;
1981 	}
1982 
1983 	mlxsw_pci->utc_nsec_offset =
1984 		mlxsw_cmd_mbox_query_fw_utc_nsec_offset_get(mbox);
1985 
1986 	mlxsw_pci->lag_mode_support =
1987 		mlxsw_cmd_mbox_query_fw_lag_mode_support_get(mbox);
1988 	mlxsw_pci->cff_support =
1989 		mlxsw_cmd_mbox_query_fw_cff_support_get(mbox);
1990 
1991 	num_pages = mlxsw_cmd_mbox_query_fw_fw_pages_get(mbox);
1992 	err = mlxsw_pci_fw_area_init(mlxsw_pci, mbox, num_pages);
1993 	if (err)
1994 		goto err_fw_area_init;
1995 
1996 	err = mlxsw_pci_boardinfo(mlxsw_pci, mbox);
1997 	if (err)
1998 		goto err_boardinfo;
1999 
2000 	err = mlxsw_core_resources_query(mlxsw_core, mbox, res);
2001 	if (err)
2002 		goto err_query_resources;
2003 
2004 	if (MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V2) &&
2005 	    MLXSW_CORE_RES_GET(mlxsw_core, CQE_V2))
2006 		mlxsw_pci->max_cqe_ver = MLXSW_PCI_CQE_V2;
2007 	else if (MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V1) &&
2008 		 MLXSW_CORE_RES_GET(mlxsw_core, CQE_V1))
2009 		mlxsw_pci->max_cqe_ver = MLXSW_PCI_CQE_V1;
2010 	else if ((MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V0) &&
2011 		  MLXSW_CORE_RES_GET(mlxsw_core, CQE_V0)) ||
2012 		 !MLXSW_CORE_RES_VALID(mlxsw_core, CQE_V0)) {
2013 		mlxsw_pci->max_cqe_ver = MLXSW_PCI_CQE_V0;
2014 	} else {
2015 		dev_err(&pdev->dev, "Invalid supported CQE version combination reported\n");
2016 		goto err_cqe_v_check;
2017 	}
2018 
2019 	err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile, res);
2020 	if (err)
2021 		goto err_config_profile;
2022 
2023 	/* Some resources depend on details of config_profile, such as unified
2024 	 * bridge model. Query the resources again to get correct values.
2025 	 */
2026 	err = mlxsw_core_resources_query(mlxsw_core, mbox, res);
2027 	if (err)
2028 		goto err_requery_resources;
2029 
2030 	mlxsw_pci_num_sg_entries_set(mlxsw_pci);
2031 
2032 	err = mlxsw_pci_napi_devs_init(mlxsw_pci);
2033 	if (err)
2034 		goto err_napi_devs_init;
2035 
2036 	err = mlxsw_pci_aqs_init(mlxsw_pci, mbox);
2037 	if (err)
2038 		goto err_aqs_init;
2039 
2040 	err = request_irq(pci_irq_vector(pdev, 0),
2041 			  mlxsw_pci_eq_irq_handler, 0,
2042 			  mlxsw_pci->bus_info.device_kind, mlxsw_pci);
2043 	if (err) {
2044 		dev_err(&pdev->dev, "IRQ request failed\n");
2045 		goto err_request_eq_irq;
2046 	}
2047 
2048 	goto mbox_put;
2049 
2050 err_request_eq_irq:
2051 	mlxsw_pci_aqs_fini(mlxsw_pci);
2052 err_aqs_init:
2053 	mlxsw_pci_napi_devs_fini(mlxsw_pci);
2054 err_napi_devs_init:
2055 err_requery_resources:
2056 err_config_profile:
2057 err_cqe_v_check:
2058 err_query_resources:
2059 err_boardinfo:
2060 	mlxsw_pci_fw_area_fini(mlxsw_pci);
2061 err_fw_area_init:
2062 err_utc_nsec_bar:
2063 err_utc_sec_bar:
2064 err_fr_rn_clk_bar:
2065 err_doorbell_page_bar:
2066 err_iface_rev:
2067 err_query_fw:
2068 	mlxsw_pci_free_irq_vectors(mlxsw_pci);
2069 err_alloc_irq:
2070 err_reset:
2071 mbox_put:
2072 	mlxsw_cmd_mbox_free(mbox);
2073 	return err;
2074 }
2075 
2076 static void mlxsw_pci_fini(void *bus_priv)
2077 {
2078 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2079 
2080 	free_irq(pci_irq_vector(mlxsw_pci->pdev, 0), mlxsw_pci);
2081 	mlxsw_pci_aqs_fini(mlxsw_pci);
2082 	mlxsw_pci_napi_devs_fini(mlxsw_pci);
2083 	mlxsw_pci_fw_area_fini(mlxsw_pci);
2084 	mlxsw_pci_free_irq_vectors(mlxsw_pci);
2085 }
2086 
2087 static struct mlxsw_pci_queue *
2088 mlxsw_pci_sdq_pick(struct mlxsw_pci *mlxsw_pci,
2089 		   const struct mlxsw_tx_info *tx_info)
2090 {
2091 	u8 ctl_sdq_count = mlxsw_pci->num_sdqs - 1;
2092 	u8 sdqn;
2093 
2094 	if (tx_info->is_emad) {
2095 		sdqn = MLXSW_PCI_SDQ_EMAD_INDEX;
2096 	} else {
2097 		BUILD_BUG_ON(MLXSW_PCI_SDQ_EMAD_INDEX != 0);
2098 		sdqn = 1 + (tx_info->local_port % ctl_sdq_count);
2099 	}
2100 
2101 	return mlxsw_pci_sdq_get(mlxsw_pci, sdqn);
2102 }
2103 
2104 static bool mlxsw_pci_skb_transmit_busy(void *bus_priv,
2105 					const struct mlxsw_tx_info *tx_info)
2106 {
2107 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2108 	struct mlxsw_pci_queue *q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
2109 
2110 	return !mlxsw_pci_queue_elem_info_producer_get(q);
2111 }
2112 
2113 static int mlxsw_pci_skb_transmit(void *bus_priv, struct sk_buff *skb,
2114 				  const struct mlxsw_tx_info *tx_info)
2115 {
2116 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2117 	struct mlxsw_pci_queue *q;
2118 	struct mlxsw_pci_queue_elem_info *elem_info;
2119 	char *wqe;
2120 	int i;
2121 	int err;
2122 
2123 	if (skb_shinfo(skb)->nr_frags > MLXSW_PCI_WQE_SG_ENTRIES - 1) {
2124 		err = skb_linearize(skb);
2125 		if (err)
2126 			return err;
2127 	}
2128 
2129 	q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
2130 	spin_lock_bh(&q->lock);
2131 	elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
2132 	if (!elem_info) {
2133 		/* queue is full */
2134 		err = -EAGAIN;
2135 		goto unlock;
2136 	}
2137 	mlxsw_skb_cb(skb)->tx_info = *tx_info;
2138 	elem_info->sdq.skb = skb;
2139 
2140 	wqe = elem_info->elem;
2141 	mlxsw_pci_wqe_c_set(wqe, 1); /* always report completion */
2142 	mlxsw_pci_wqe_lp_set(wqe, 0);
2143 	mlxsw_pci_wqe_type_set(wqe, MLXSW_PCI_WQE_TYPE_ETHERNET);
2144 
2145 	err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
2146 				     skb_headlen(skb), DMA_TO_DEVICE);
2147 	if (err)
2148 		goto unlock;
2149 
2150 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2151 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2152 
2153 		err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, i + 1,
2154 					     skb_frag_address(frag),
2155 					     skb_frag_size(frag),
2156 					     DMA_TO_DEVICE);
2157 		if (err)
2158 			goto unmap_frags;
2159 	}
2160 
2161 	if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP))
2162 		skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2163 
2164 	/* Set unused sq entries byte count to zero. */
2165 	for (i++; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
2166 		mlxsw_pci_wqe_byte_count_set(wqe, i, 0);
2167 
2168 	/* Everything is set up, ring producer doorbell to get HW going */
2169 	q->producer_counter++;
2170 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
2171 
2172 	goto unlock;
2173 
2174 unmap_frags:
2175 	for (; i >= 0; i--)
2176 		mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
2177 unlock:
2178 	spin_unlock_bh(&q->lock);
2179 	return err;
2180 }
2181 
2182 static int mlxsw_pci_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
2183 			      u32 in_mod, bool out_mbox_direct,
2184 			      char *in_mbox, size_t in_mbox_size,
2185 			      char *out_mbox, size_t out_mbox_size,
2186 			      u8 *p_status)
2187 {
2188 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2189 	dma_addr_t in_mapaddr = 0, out_mapaddr = 0;
2190 	unsigned long timeout = msecs_to_jiffies(MLXSW_PCI_CIR_TIMEOUT_MSECS);
2191 	unsigned long end;
2192 	bool wait_done;
2193 	int err;
2194 
2195 	*p_status = MLXSW_CMD_STATUS_OK;
2196 
2197 	err = mutex_lock_interruptible(&mlxsw_pci->cmd.lock);
2198 	if (err)
2199 		return err;
2200 
2201 	if (in_mbox) {
2202 		memcpy(mlxsw_pci->cmd.in_mbox.buf, in_mbox, in_mbox_size);
2203 		in_mapaddr = mlxsw_pci->cmd.in_mbox.mapaddr;
2204 	}
2205 	mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_HI, upper_32_bits(in_mapaddr));
2206 	mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_LO, lower_32_bits(in_mapaddr));
2207 
2208 	if (out_mbox)
2209 		out_mapaddr = mlxsw_pci->cmd.out_mbox.mapaddr;
2210 	mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_HI, upper_32_bits(out_mapaddr));
2211 	mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_LO, lower_32_bits(out_mapaddr));
2212 
2213 	mlxsw_pci_write32(mlxsw_pci, CIR_IN_MODIFIER, in_mod);
2214 	mlxsw_pci_write32(mlxsw_pci, CIR_TOKEN, 0);
2215 
2216 	wait_done = false;
2217 
2218 	wmb(); /* all needs to be written before we write control register */
2219 	mlxsw_pci_write32(mlxsw_pci, CIR_CTRL,
2220 			  MLXSW_PCI_CIR_CTRL_GO_BIT |
2221 			  (opcode_mod << MLXSW_PCI_CIR_CTRL_OPCODE_MOD_SHIFT) |
2222 			  opcode);
2223 
2224 	end = jiffies + timeout;
2225 	do {
2226 		u32 ctrl = mlxsw_pci_read32(mlxsw_pci, CIR_CTRL);
2227 
2228 		if (!(ctrl & MLXSW_PCI_CIR_CTRL_GO_BIT)) {
2229 			wait_done = true;
2230 			*p_status = ctrl >> MLXSW_PCI_CIR_CTRL_STATUS_SHIFT;
2231 			break;
2232 		}
2233 		cond_resched();
2234 	} while (time_before(jiffies, end));
2235 
2236 	err = 0;
2237 	if (wait_done) {
2238 		if (*p_status)
2239 			err = -EIO;
2240 	} else {
2241 		err = -ETIMEDOUT;
2242 	}
2243 
2244 	if (!err && out_mbox && out_mbox_direct) {
2245 		/* Some commands don't use output param as address to mailbox
2246 		 * but they store output directly into registers. In that case,
2247 		 * copy registers into mbox buffer.
2248 		 */
2249 		__be32 tmp;
2250 
2251 		tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
2252 						   CIR_OUT_PARAM_HI));
2253 		memcpy(out_mbox, &tmp, sizeof(tmp));
2254 		tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
2255 						   CIR_OUT_PARAM_LO));
2256 		memcpy(out_mbox + sizeof(tmp), &tmp, sizeof(tmp));
2257 	} else if (!err && out_mbox) {
2258 		memcpy(out_mbox, mlxsw_pci->cmd.out_mbox.buf, out_mbox_size);
2259 	}
2260 
2261 	mutex_unlock(&mlxsw_pci->cmd.lock);
2262 
2263 	return err;
2264 }
2265 
2266 static u32 mlxsw_pci_read_frc_h(void *bus_priv)
2267 {
2268 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2269 	u64 frc_offset_h;
2270 
2271 	frc_offset_h = mlxsw_pci->free_running_clock_offset;
2272 	return mlxsw_pci_read32_off(mlxsw_pci, frc_offset_h);
2273 }
2274 
2275 static u32 mlxsw_pci_read_frc_l(void *bus_priv)
2276 {
2277 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2278 	u64 frc_offset_l;
2279 
2280 	frc_offset_l = mlxsw_pci->free_running_clock_offset + 4;
2281 	return mlxsw_pci_read32_off(mlxsw_pci, frc_offset_l);
2282 }
2283 
2284 static u32 mlxsw_pci_read_utc_sec(void *bus_priv)
2285 {
2286 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2287 
2288 	return mlxsw_pci_read32_off(mlxsw_pci, mlxsw_pci->utc_sec_offset);
2289 }
2290 
2291 static u32 mlxsw_pci_read_utc_nsec(void *bus_priv)
2292 {
2293 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2294 
2295 	return mlxsw_pci_read32_off(mlxsw_pci, mlxsw_pci->utc_nsec_offset);
2296 }
2297 
2298 static enum mlxsw_cmd_mbox_config_profile_lag_mode
2299 mlxsw_pci_lag_mode(void *bus_priv)
2300 {
2301 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2302 
2303 	return mlxsw_pci->lag_mode;
2304 }
2305 
2306 static enum mlxsw_cmd_mbox_config_profile_flood_mode
2307 mlxsw_pci_flood_mode(void *bus_priv)
2308 {
2309 	struct mlxsw_pci *mlxsw_pci = bus_priv;
2310 
2311 	return mlxsw_pci->flood_mode;
2312 }
2313 
2314 static const struct mlxsw_bus mlxsw_pci_bus = {
2315 	.kind			= "pci",
2316 	.init			= mlxsw_pci_init,
2317 	.fini			= mlxsw_pci_fini,
2318 	.skb_transmit_busy	= mlxsw_pci_skb_transmit_busy,
2319 	.skb_transmit		= mlxsw_pci_skb_transmit,
2320 	.cmd_exec		= mlxsw_pci_cmd_exec,
2321 	.read_frc_h		= mlxsw_pci_read_frc_h,
2322 	.read_frc_l		= mlxsw_pci_read_frc_l,
2323 	.read_utc_sec		= mlxsw_pci_read_utc_sec,
2324 	.read_utc_nsec		= mlxsw_pci_read_utc_nsec,
2325 	.lag_mode		= mlxsw_pci_lag_mode,
2326 	.flood_mode		= mlxsw_pci_flood_mode,
2327 	.features		= MLXSW_BUS_F_TXRX | MLXSW_BUS_F_RESET,
2328 };
2329 
2330 static int mlxsw_pci_cmd_init(struct mlxsw_pci *mlxsw_pci)
2331 {
2332 	int err;
2333 
2334 	mutex_init(&mlxsw_pci->cmd.lock);
2335 
2336 	err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
2337 	if (err)
2338 		goto err_in_mbox_alloc;
2339 
2340 	err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
2341 	if (err)
2342 		goto err_out_mbox_alloc;
2343 
2344 	return 0;
2345 
2346 err_out_mbox_alloc:
2347 	mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
2348 err_in_mbox_alloc:
2349 	mutex_destroy(&mlxsw_pci->cmd.lock);
2350 	return err;
2351 }
2352 
2353 static void mlxsw_pci_cmd_fini(struct mlxsw_pci *mlxsw_pci)
2354 {
2355 	mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
2356 	mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
2357 	mutex_destroy(&mlxsw_pci->cmd.lock);
2358 }
2359 
2360 static int mlxsw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
2361 {
2362 	const char *driver_name = dev_driver_string(&pdev->dev);
2363 	struct mlxsw_pci *mlxsw_pci;
2364 	int err;
2365 
2366 	mlxsw_pci = kzalloc(sizeof(*mlxsw_pci), GFP_KERNEL);
2367 	if (!mlxsw_pci)
2368 		return -ENOMEM;
2369 
2370 	err = pci_enable_device(pdev);
2371 	if (err) {
2372 		dev_err(&pdev->dev, "pci_enable_device failed\n");
2373 		goto err_pci_enable_device;
2374 	}
2375 
2376 	err = pci_request_regions(pdev, driver_name);
2377 	if (err) {
2378 		dev_err(&pdev->dev, "pci_request_regions failed\n");
2379 		goto err_pci_request_regions;
2380 	}
2381 
2382 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
2383 	if (err) {
2384 		err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
2385 		if (err) {
2386 			dev_err(&pdev->dev, "dma_set_mask failed\n");
2387 			goto err_pci_set_dma_mask;
2388 		}
2389 	}
2390 
2391 	if (pci_resource_len(pdev, 0) < MLXSW_PCI_BAR0_SIZE) {
2392 		dev_err(&pdev->dev, "invalid PCI region size\n");
2393 		err = -EINVAL;
2394 		goto err_pci_resource_len_check;
2395 	}
2396 
2397 	mlxsw_pci->hw_addr = ioremap(pci_resource_start(pdev, 0),
2398 				     pci_resource_len(pdev, 0));
2399 	if (!mlxsw_pci->hw_addr) {
2400 		dev_err(&pdev->dev, "ioremap failed\n");
2401 		err = -EIO;
2402 		goto err_ioremap;
2403 	}
2404 	pci_set_master(pdev);
2405 
2406 	mlxsw_pci->pdev = pdev;
2407 	pci_set_drvdata(pdev, mlxsw_pci);
2408 
2409 	err = mlxsw_pci_cmd_init(mlxsw_pci);
2410 	if (err)
2411 		goto err_pci_cmd_init;
2412 
2413 	mlxsw_pci->bus_info.device_kind = driver_name;
2414 	mlxsw_pci->bus_info.device_name = pci_name(mlxsw_pci->pdev);
2415 	mlxsw_pci->bus_info.dev = &pdev->dev;
2416 	mlxsw_pci->bus_info.read_clock_capable = true;
2417 	mlxsw_pci->id = id;
2418 
2419 	err = mlxsw_core_bus_device_register(&mlxsw_pci->bus_info,
2420 					     &mlxsw_pci_bus, mlxsw_pci, false,
2421 					     NULL, NULL);
2422 	if (err) {
2423 		dev_err(&pdev->dev, "cannot register bus device\n");
2424 		goto err_bus_device_register;
2425 	}
2426 
2427 	return 0;
2428 
2429 err_bus_device_register:
2430 	mlxsw_pci_cmd_fini(mlxsw_pci);
2431 err_pci_cmd_init:
2432 	iounmap(mlxsw_pci->hw_addr);
2433 err_ioremap:
2434 err_pci_resource_len_check:
2435 err_pci_set_dma_mask:
2436 	pci_release_regions(pdev);
2437 err_pci_request_regions:
2438 	pci_disable_device(pdev);
2439 err_pci_enable_device:
2440 	kfree(mlxsw_pci);
2441 	return err;
2442 }
2443 
2444 static void mlxsw_pci_remove(struct pci_dev *pdev)
2445 {
2446 	struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
2447 
2448 	mlxsw_core_bus_device_unregister(mlxsw_pci->core, false);
2449 	mlxsw_pci_cmd_fini(mlxsw_pci);
2450 	iounmap(mlxsw_pci->hw_addr);
2451 	pci_release_regions(mlxsw_pci->pdev);
2452 	pci_disable_device(mlxsw_pci->pdev);
2453 	kfree(mlxsw_pci);
2454 }
2455 
2456 static void mlxsw_pci_reset_prepare(struct pci_dev *pdev)
2457 {
2458 	struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
2459 
2460 	mlxsw_core_bus_device_unregister(mlxsw_pci->core, false);
2461 }
2462 
2463 static void mlxsw_pci_reset_done(struct pci_dev *pdev)
2464 {
2465 	struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
2466 
2467 	mlxsw_pci->skip_reset = true;
2468 	mlxsw_core_bus_device_register(&mlxsw_pci->bus_info, &mlxsw_pci_bus,
2469 				       mlxsw_pci, false, NULL, NULL);
2470 	mlxsw_pci->skip_reset = false;
2471 }
2472 
2473 static const struct pci_error_handlers mlxsw_pci_err_handler = {
2474 	.reset_prepare = mlxsw_pci_reset_prepare,
2475 	.reset_done = mlxsw_pci_reset_done,
2476 };
2477 
2478 int mlxsw_pci_driver_register(struct pci_driver *pci_driver)
2479 {
2480 	pci_driver->probe = mlxsw_pci_probe;
2481 	pci_driver->remove = mlxsw_pci_remove;
2482 	pci_driver->shutdown = mlxsw_pci_remove;
2483 	pci_driver->err_handler = &mlxsw_pci_err_handler;
2484 	return pci_register_driver(pci_driver);
2485 }
2486 EXPORT_SYMBOL(mlxsw_pci_driver_register);
2487 
2488 void mlxsw_pci_driver_unregister(struct pci_driver *pci_driver)
2489 {
2490 	pci_unregister_driver(pci_driver);
2491 }
2492 EXPORT_SYMBOL(mlxsw_pci_driver_unregister);
2493 
2494 static int __init mlxsw_pci_module_init(void)
2495 {
2496 	return 0;
2497 }
2498 
2499 static void __exit mlxsw_pci_module_exit(void)
2500 {
2501 }
2502 
2503 module_init(mlxsw_pci_module_init);
2504 module_exit(mlxsw_pci_module_exit);
2505 
2506 MODULE_LICENSE("Dual BSD/GPL");
2507 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
2508 MODULE_DESCRIPTION("Mellanox switch PCI interface driver");
2509