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