xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlxsw/pci.c (revision 4f139972b489f8bc2c821aa25ac65018d92af3f7)
1 /*
2  * drivers/net/ethernet/mellanox/mlxsw/pci.c
3  * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4  * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the names of the copyright holders nor the names of its
15  *    contributors may be used to endorse or promote products derived from
16  *    this software without specific prior written permission.
17  *
18  * Alternatively, this software may be distributed under the terms of the
19  * GNU General Public License ("GPL") version 2 as published by the Free
20  * Software Foundation.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/export.h>
38 #include <linux/err.h>
39 #include <linux/device.h>
40 #include <linux/pci.h>
41 #include <linux/interrupt.h>
42 #include <linux/wait.h>
43 #include <linux/types.h>
44 #include <linux/skbuff.h>
45 #include <linux/if_vlan.h>
46 #include <linux/log2.h>
47 #include <linux/string.h>
48 
49 #include "pci_hw.h"
50 #include "pci.h"
51 #include "core.h"
52 #include "cmd.h"
53 #include "port.h"
54 #include "resources.h"
55 
56 static const char mlxsw_pci_driver_name[] = "mlxsw_pci";
57 
58 #define mlxsw_pci_write32(mlxsw_pci, reg, val) \
59 	iowrite32be(val, (mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
60 #define mlxsw_pci_read32(mlxsw_pci, reg) \
61 	ioread32be((mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
62 
63 enum mlxsw_pci_queue_type {
64 	MLXSW_PCI_QUEUE_TYPE_SDQ,
65 	MLXSW_PCI_QUEUE_TYPE_RDQ,
66 	MLXSW_PCI_QUEUE_TYPE_CQ,
67 	MLXSW_PCI_QUEUE_TYPE_EQ,
68 };
69 
70 #define MLXSW_PCI_QUEUE_TYPE_COUNT	4
71 
72 static const u16 mlxsw_pci_doorbell_type_offset[] = {
73 	MLXSW_PCI_DOORBELL_SDQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_SDQ */
74 	MLXSW_PCI_DOORBELL_RDQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_RDQ */
75 	MLXSW_PCI_DOORBELL_CQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_CQ */
76 	MLXSW_PCI_DOORBELL_EQ_OFFSET,	/* for type MLXSW_PCI_QUEUE_TYPE_EQ */
77 };
78 
79 static const u16 mlxsw_pci_doorbell_arm_type_offset[] = {
80 	0, /* unused */
81 	0, /* unused */
82 	MLXSW_PCI_DOORBELL_ARM_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
83 	MLXSW_PCI_DOORBELL_ARM_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
84 };
85 
86 struct mlxsw_pci_mem_item {
87 	char *buf;
88 	dma_addr_t mapaddr;
89 	size_t size;
90 };
91 
92 struct mlxsw_pci_queue_elem_info {
93 	char *elem; /* pointer to actual dma mapped element mem chunk */
94 	union {
95 		struct {
96 			struct sk_buff *skb;
97 		} sdq;
98 		struct {
99 			struct sk_buff *skb;
100 		} rdq;
101 	} u;
102 };
103 
104 struct mlxsw_pci_queue {
105 	spinlock_t lock; /* for queue accesses */
106 	struct mlxsw_pci_mem_item mem_item;
107 	struct mlxsw_pci_queue_elem_info *elem_info;
108 	u16 producer_counter;
109 	u16 consumer_counter;
110 	u16 count; /* number of elements in queue */
111 	u8 num; /* queue number */
112 	u8 elem_size; /* size of one element */
113 	enum mlxsw_pci_queue_type type;
114 	struct tasklet_struct tasklet; /* queue processing tasklet */
115 	struct mlxsw_pci *pci;
116 	union {
117 		struct {
118 			u32 comp_sdq_count;
119 			u32 comp_rdq_count;
120 		} cq;
121 		struct {
122 			u32 ev_cmd_count;
123 			u32 ev_comp_count;
124 			u32 ev_other_count;
125 		} eq;
126 	} u;
127 };
128 
129 struct mlxsw_pci_queue_type_group {
130 	struct mlxsw_pci_queue *q;
131 	u8 count; /* number of queues in group */
132 };
133 
134 struct mlxsw_pci {
135 	struct pci_dev *pdev;
136 	u8 __iomem *hw_addr;
137 	struct mlxsw_pci_queue_type_group queues[MLXSW_PCI_QUEUE_TYPE_COUNT];
138 	u32 doorbell_offset;
139 	struct msix_entry msix_entry;
140 	struct mlxsw_core *core;
141 	struct {
142 		struct mlxsw_pci_mem_item *items;
143 		unsigned int count;
144 	} fw_area;
145 	struct {
146 		struct mlxsw_pci_mem_item out_mbox;
147 		struct mlxsw_pci_mem_item in_mbox;
148 		struct mutex lock; /* Lock access to command registers */
149 		bool nopoll;
150 		wait_queue_head_t wait;
151 		bool wait_done;
152 		struct {
153 			u8 status;
154 			u64 out_param;
155 		} comp;
156 	} cmd;
157 	struct mlxsw_bus_info bus_info;
158 };
159 
160 static void mlxsw_pci_queue_tasklet_schedule(struct mlxsw_pci_queue *q)
161 {
162 	tasklet_schedule(&q->tasklet);
163 }
164 
165 static char *__mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q,
166 					size_t elem_size, int elem_index)
167 {
168 	return q->mem_item.buf + (elem_size * elem_index);
169 }
170 
171 static struct mlxsw_pci_queue_elem_info *
172 mlxsw_pci_queue_elem_info_get(struct mlxsw_pci_queue *q, int elem_index)
173 {
174 	return &q->elem_info[elem_index];
175 }
176 
177 static struct mlxsw_pci_queue_elem_info *
178 mlxsw_pci_queue_elem_info_producer_get(struct mlxsw_pci_queue *q)
179 {
180 	int index = q->producer_counter & (q->count - 1);
181 
182 	if ((u16) (q->producer_counter - q->consumer_counter) == q->count)
183 		return NULL;
184 	return mlxsw_pci_queue_elem_info_get(q, index);
185 }
186 
187 static struct mlxsw_pci_queue_elem_info *
188 mlxsw_pci_queue_elem_info_consumer_get(struct mlxsw_pci_queue *q)
189 {
190 	int index = q->consumer_counter & (q->count - 1);
191 
192 	return mlxsw_pci_queue_elem_info_get(q, index);
193 }
194 
195 static char *mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, int elem_index)
196 {
197 	return mlxsw_pci_queue_elem_info_get(q, elem_index)->elem;
198 }
199 
200 static bool mlxsw_pci_elem_hw_owned(struct mlxsw_pci_queue *q, bool owner_bit)
201 {
202 	return owner_bit != !!(q->consumer_counter & q->count);
203 }
204 
205 static char *
206 mlxsw_pci_queue_sw_elem_get(struct mlxsw_pci_queue *q,
207 			    u32 (*get_elem_owner_func)(const char *))
208 {
209 	struct mlxsw_pci_queue_elem_info *elem_info;
210 	char *elem;
211 	bool owner_bit;
212 
213 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
214 	elem = elem_info->elem;
215 	owner_bit = get_elem_owner_func(elem);
216 	if (mlxsw_pci_elem_hw_owned(q, owner_bit))
217 		return NULL;
218 	q->consumer_counter++;
219 	rmb(); /* make sure we read owned bit before the rest of elem */
220 	return elem;
221 }
222 
223 static struct mlxsw_pci_queue_type_group *
224 mlxsw_pci_queue_type_group_get(struct mlxsw_pci *mlxsw_pci,
225 			       enum mlxsw_pci_queue_type q_type)
226 {
227 	return &mlxsw_pci->queues[q_type];
228 }
229 
230 static u8 __mlxsw_pci_queue_count(struct mlxsw_pci *mlxsw_pci,
231 				  enum mlxsw_pci_queue_type q_type)
232 {
233 	struct mlxsw_pci_queue_type_group *queue_group;
234 
235 	queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_type);
236 	return queue_group->count;
237 }
238 
239 static u8 mlxsw_pci_sdq_count(struct mlxsw_pci *mlxsw_pci)
240 {
241 	return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_SDQ);
242 }
243 
244 static u8 mlxsw_pci_cq_count(struct mlxsw_pci *mlxsw_pci)
245 {
246 	return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ);
247 }
248 
249 static struct mlxsw_pci_queue *
250 __mlxsw_pci_queue_get(struct mlxsw_pci *mlxsw_pci,
251 		      enum mlxsw_pci_queue_type q_type, u8 q_num)
252 {
253 	return &mlxsw_pci->queues[q_type].q[q_num];
254 }
255 
256 static struct mlxsw_pci_queue *mlxsw_pci_sdq_get(struct mlxsw_pci *mlxsw_pci,
257 						 u8 q_num)
258 {
259 	return __mlxsw_pci_queue_get(mlxsw_pci,
260 				     MLXSW_PCI_QUEUE_TYPE_SDQ, q_num);
261 }
262 
263 static struct mlxsw_pci_queue *mlxsw_pci_rdq_get(struct mlxsw_pci *mlxsw_pci,
264 						 u8 q_num)
265 {
266 	return __mlxsw_pci_queue_get(mlxsw_pci,
267 				     MLXSW_PCI_QUEUE_TYPE_RDQ, q_num);
268 }
269 
270 static struct mlxsw_pci_queue *mlxsw_pci_cq_get(struct mlxsw_pci *mlxsw_pci,
271 						u8 q_num)
272 {
273 	return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ, q_num);
274 }
275 
276 static struct mlxsw_pci_queue *mlxsw_pci_eq_get(struct mlxsw_pci *mlxsw_pci,
277 						u8 q_num)
278 {
279 	return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ, q_num);
280 }
281 
282 static void __mlxsw_pci_queue_doorbell_set(struct mlxsw_pci *mlxsw_pci,
283 					   struct mlxsw_pci_queue *q,
284 					   u16 val)
285 {
286 	mlxsw_pci_write32(mlxsw_pci,
287 			  DOORBELL(mlxsw_pci->doorbell_offset,
288 				   mlxsw_pci_doorbell_type_offset[q->type],
289 				   q->num), val);
290 }
291 
292 static void __mlxsw_pci_queue_doorbell_arm_set(struct mlxsw_pci *mlxsw_pci,
293 					       struct mlxsw_pci_queue *q,
294 					       u16 val)
295 {
296 	mlxsw_pci_write32(mlxsw_pci,
297 			  DOORBELL(mlxsw_pci->doorbell_offset,
298 				   mlxsw_pci_doorbell_arm_type_offset[q->type],
299 				   q->num), val);
300 }
301 
302 static void mlxsw_pci_queue_doorbell_producer_ring(struct mlxsw_pci *mlxsw_pci,
303 						   struct mlxsw_pci_queue *q)
304 {
305 	wmb(); /* ensure all writes are done before we ring a bell */
306 	__mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, q->producer_counter);
307 }
308 
309 static void mlxsw_pci_queue_doorbell_consumer_ring(struct mlxsw_pci *mlxsw_pci,
310 						   struct mlxsw_pci_queue *q)
311 {
312 	wmb(); /* ensure all writes are done before we ring a bell */
313 	__mlxsw_pci_queue_doorbell_set(mlxsw_pci, q,
314 				       q->consumer_counter + q->count);
315 }
316 
317 static void
318 mlxsw_pci_queue_doorbell_arm_consumer_ring(struct mlxsw_pci *mlxsw_pci,
319 					   struct mlxsw_pci_queue *q)
320 {
321 	wmb(); /* ensure all writes are done before we ring a bell */
322 	__mlxsw_pci_queue_doorbell_arm_set(mlxsw_pci, q, q->consumer_counter);
323 }
324 
325 static dma_addr_t __mlxsw_pci_queue_page_get(struct mlxsw_pci_queue *q,
326 					     int page_index)
327 {
328 	return q->mem_item.mapaddr + MLXSW_PCI_PAGE_SIZE * page_index;
329 }
330 
331 static int mlxsw_pci_sdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
332 			      struct mlxsw_pci_queue *q)
333 {
334 	int i;
335 	int err;
336 
337 	q->producer_counter = 0;
338 	q->consumer_counter = 0;
339 
340 	/* Set CQ of same number of this SDQ. */
341 	mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, q->num);
342 	mlxsw_cmd_mbox_sw2hw_dq_sdq_tclass_set(mbox, 3);
343 	mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
344 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
345 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
346 
347 		mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
348 	}
349 
350 	err = mlxsw_cmd_sw2hw_sdq(mlxsw_pci->core, mbox, q->num);
351 	if (err)
352 		return err;
353 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
354 	return 0;
355 }
356 
357 static void mlxsw_pci_sdq_fini(struct mlxsw_pci *mlxsw_pci,
358 			       struct mlxsw_pci_queue *q)
359 {
360 	mlxsw_cmd_hw2sw_sdq(mlxsw_pci->core, q->num);
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 = pci_map_single(pdev, frag_data, frag_len, direction);
371 	if (unlikely(pci_dma_mapping_error(pdev, 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 	pci_unmap_single(pdev, mapaddr, frag_len, direction);
390 }
391 
392 static int mlxsw_pci_rdq_skb_alloc(struct mlxsw_pci *mlxsw_pci,
393 				   struct mlxsw_pci_queue_elem_info *elem_info)
394 {
395 	size_t buf_len = MLXSW_PORT_MAX_MTU;
396 	char *wqe = elem_info->elem;
397 	struct sk_buff *skb;
398 	int err;
399 
400 	elem_info->u.rdq.skb = NULL;
401 	skb = netdev_alloc_skb_ip_align(NULL, buf_len);
402 	if (!skb)
403 		return -ENOMEM;
404 
405 	/* Assume that wqe was previously zeroed. */
406 
407 	err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
408 				     buf_len, DMA_FROM_DEVICE);
409 	if (err)
410 		goto err_frag_map;
411 
412 	elem_info->u.rdq.skb = skb;
413 	return 0;
414 
415 err_frag_map:
416 	dev_kfree_skb_any(skb);
417 	return err;
418 }
419 
420 static void mlxsw_pci_rdq_skb_free(struct mlxsw_pci *mlxsw_pci,
421 				   struct mlxsw_pci_queue_elem_info *elem_info)
422 {
423 	struct sk_buff *skb;
424 	char *wqe;
425 
426 	skb = elem_info->u.rdq.skb;
427 	wqe = elem_info->elem;
428 
429 	mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
430 	dev_kfree_skb_any(skb);
431 }
432 
433 static int mlxsw_pci_rdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
434 			      struct mlxsw_pci_queue *q)
435 {
436 	struct mlxsw_pci_queue_elem_info *elem_info;
437 	u8 sdq_count = mlxsw_pci_sdq_count(mlxsw_pci);
438 	int i;
439 	int err;
440 
441 	q->producer_counter = 0;
442 	q->consumer_counter = 0;
443 
444 	/* Set CQ of same number of this RDQ with base
445 	 * above SDQ count as the lower ones are assigned to SDQs.
446 	 */
447 	mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, sdq_count + q->num);
448 	mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
449 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
450 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
451 
452 		mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
453 	}
454 
455 	err = mlxsw_cmd_sw2hw_rdq(mlxsw_pci->core, mbox, q->num);
456 	if (err)
457 		return err;
458 
459 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
460 
461 	for (i = 0; i < q->count; i++) {
462 		elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
463 		BUG_ON(!elem_info);
464 		err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
465 		if (err)
466 			goto rollback;
467 		/* Everything is set up, ring doorbell to pass elem to HW */
468 		q->producer_counter++;
469 		mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
470 	}
471 
472 	return 0;
473 
474 rollback:
475 	for (i--; i >= 0; i--) {
476 		elem_info = mlxsw_pci_queue_elem_info_get(q, i);
477 		mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
478 	}
479 	mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
480 
481 	return err;
482 }
483 
484 static void mlxsw_pci_rdq_fini(struct mlxsw_pci *mlxsw_pci,
485 			       struct mlxsw_pci_queue *q)
486 {
487 	struct mlxsw_pci_queue_elem_info *elem_info;
488 	int i;
489 
490 	mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
491 	for (i = 0; i < q->count; i++) {
492 		elem_info = mlxsw_pci_queue_elem_info_get(q, i);
493 		mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
494 	}
495 }
496 
497 static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
498 			     struct mlxsw_pci_queue *q)
499 {
500 	int i;
501 	int err;
502 
503 	q->consumer_counter = 0;
504 
505 	for (i = 0; i < q->count; i++) {
506 		char *elem = mlxsw_pci_queue_elem_get(q, i);
507 
508 		mlxsw_pci_cqe_owner_set(elem, 1);
509 	}
510 
511 	mlxsw_cmd_mbox_sw2hw_cq_cv_set(mbox, 0); /* CQE ver 0 */
512 	mlxsw_cmd_mbox_sw2hw_cq_c_eqn_set(mbox, MLXSW_PCI_EQ_COMP_NUM);
513 	mlxsw_cmd_mbox_sw2hw_cq_st_set(mbox, 0);
514 	mlxsw_cmd_mbox_sw2hw_cq_log_cq_size_set(mbox, ilog2(q->count));
515 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
516 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
517 
518 		mlxsw_cmd_mbox_sw2hw_cq_pa_set(mbox, i, mapaddr);
519 	}
520 	err = mlxsw_cmd_sw2hw_cq(mlxsw_pci->core, mbox, q->num);
521 	if (err)
522 		return err;
523 	mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
524 	mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
525 	return 0;
526 }
527 
528 static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci,
529 			      struct mlxsw_pci_queue *q)
530 {
531 	mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num);
532 }
533 
534 static void mlxsw_pci_cqe_sdq_handle(struct mlxsw_pci *mlxsw_pci,
535 				     struct mlxsw_pci_queue *q,
536 				     u16 consumer_counter_limit,
537 				     char *cqe)
538 {
539 	struct pci_dev *pdev = mlxsw_pci->pdev;
540 	struct mlxsw_pci_queue_elem_info *elem_info;
541 	char *wqe;
542 	struct sk_buff *skb;
543 	int i;
544 
545 	spin_lock(&q->lock);
546 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
547 	skb = elem_info->u.sdq.skb;
548 	wqe = elem_info->elem;
549 	for (i = 0; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
550 		mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
551 	dev_kfree_skb_any(skb);
552 	elem_info->u.sdq.skb = NULL;
553 
554 	if (q->consumer_counter++ != consumer_counter_limit)
555 		dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in SDQ\n");
556 	spin_unlock(&q->lock);
557 }
558 
559 static void mlxsw_pci_cqe_rdq_handle(struct mlxsw_pci *mlxsw_pci,
560 				     struct mlxsw_pci_queue *q,
561 				     u16 consumer_counter_limit,
562 				     char *cqe)
563 {
564 	struct pci_dev *pdev = mlxsw_pci->pdev;
565 	struct mlxsw_pci_queue_elem_info *elem_info;
566 	char *wqe;
567 	struct sk_buff *skb;
568 	struct mlxsw_rx_info rx_info;
569 	u16 byte_count;
570 	int err;
571 
572 	elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
573 	skb = elem_info->u.sdq.skb;
574 	if (!skb)
575 		return;
576 	wqe = elem_info->elem;
577 	mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
578 
579 	if (q->consumer_counter++ != consumer_counter_limit)
580 		dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in RDQ\n");
581 
582 	if (mlxsw_pci_cqe_lag_get(cqe)) {
583 		rx_info.is_lag = true;
584 		rx_info.u.lag_id = mlxsw_pci_cqe_lag_id_get(cqe);
585 		rx_info.lag_port_index = mlxsw_pci_cqe_lag_port_index_get(cqe);
586 	} else {
587 		rx_info.is_lag = false;
588 		rx_info.u.sys_port = mlxsw_pci_cqe_system_port_get(cqe);
589 	}
590 
591 	rx_info.trap_id = mlxsw_pci_cqe_trap_id_get(cqe);
592 
593 	byte_count = mlxsw_pci_cqe_byte_count_get(cqe);
594 	if (mlxsw_pci_cqe_crc_get(cqe))
595 		byte_count -= ETH_FCS_LEN;
596 	skb_put(skb, byte_count);
597 	mlxsw_core_skb_receive(mlxsw_pci->core, skb, &rx_info);
598 
599 	memset(wqe, 0, q->elem_size);
600 	err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
601 	if (err)
602 		dev_dbg_ratelimited(&pdev->dev, "Failed to alloc skb for RDQ\n");
603 	/* Everything is set up, ring doorbell to pass elem to HW */
604 	q->producer_counter++;
605 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
606 	return;
607 }
608 
609 static char *mlxsw_pci_cq_sw_cqe_get(struct mlxsw_pci_queue *q)
610 {
611 	return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_cqe_owner_get);
612 }
613 
614 static void mlxsw_pci_cq_tasklet(unsigned long data)
615 {
616 	struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
617 	struct mlxsw_pci *mlxsw_pci = q->pci;
618 	char *cqe;
619 	int items = 0;
620 	int credits = q->count >> 1;
621 
622 	while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) {
623 		u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe);
624 		u8 sendq = mlxsw_pci_cqe_sr_get(cqe);
625 		u8 dqn = mlxsw_pci_cqe_dqn_get(cqe);
626 
627 		if (sendq) {
628 			struct mlxsw_pci_queue *sdq;
629 
630 			sdq = mlxsw_pci_sdq_get(mlxsw_pci, dqn);
631 			mlxsw_pci_cqe_sdq_handle(mlxsw_pci, sdq,
632 						 wqe_counter, cqe);
633 			q->u.cq.comp_sdq_count++;
634 		} else {
635 			struct mlxsw_pci_queue *rdq;
636 
637 			rdq = mlxsw_pci_rdq_get(mlxsw_pci, dqn);
638 			mlxsw_pci_cqe_rdq_handle(mlxsw_pci, rdq,
639 						 wqe_counter, cqe);
640 			q->u.cq.comp_rdq_count++;
641 		}
642 		if (++items == credits)
643 			break;
644 	}
645 	if (items) {
646 		mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
647 		mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
648 	}
649 }
650 
651 static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
652 			     struct mlxsw_pci_queue *q)
653 {
654 	int i;
655 	int err;
656 
657 	q->consumer_counter = 0;
658 
659 	for (i = 0; i < q->count; i++) {
660 		char *elem = mlxsw_pci_queue_elem_get(q, i);
661 
662 		mlxsw_pci_eqe_owner_set(elem, 1);
663 	}
664 
665 	mlxsw_cmd_mbox_sw2hw_eq_int_msix_set(mbox, 1); /* MSI-X used */
666 	mlxsw_cmd_mbox_sw2hw_eq_st_set(mbox, 1); /* armed */
667 	mlxsw_cmd_mbox_sw2hw_eq_log_eq_size_set(mbox, ilog2(q->count));
668 	for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
669 		dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
670 
671 		mlxsw_cmd_mbox_sw2hw_eq_pa_set(mbox, i, mapaddr);
672 	}
673 	err = mlxsw_cmd_sw2hw_eq(mlxsw_pci->core, mbox, q->num);
674 	if (err)
675 		return err;
676 	mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
677 	mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
678 	return 0;
679 }
680 
681 static void mlxsw_pci_eq_fini(struct mlxsw_pci *mlxsw_pci,
682 			      struct mlxsw_pci_queue *q)
683 {
684 	mlxsw_cmd_hw2sw_eq(mlxsw_pci->core, q->num);
685 }
686 
687 static void mlxsw_pci_eq_cmd_event(struct mlxsw_pci *mlxsw_pci, char *eqe)
688 {
689 	mlxsw_pci->cmd.comp.status = mlxsw_pci_eqe_cmd_status_get(eqe);
690 	mlxsw_pci->cmd.comp.out_param =
691 		((u64) mlxsw_pci_eqe_cmd_out_param_h_get(eqe)) << 32 |
692 		mlxsw_pci_eqe_cmd_out_param_l_get(eqe);
693 	mlxsw_pci->cmd.wait_done = true;
694 	wake_up(&mlxsw_pci->cmd.wait);
695 }
696 
697 static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q)
698 {
699 	return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_eqe_owner_get);
700 }
701 
702 static void mlxsw_pci_eq_tasklet(unsigned long data)
703 {
704 	struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
705 	struct mlxsw_pci *mlxsw_pci = q->pci;
706 	u8 cq_count = mlxsw_pci_cq_count(mlxsw_pci);
707 	unsigned long active_cqns[BITS_TO_LONGS(MLXSW_PCI_CQS_MAX)];
708 	char *eqe;
709 	u8 cqn;
710 	bool cq_handle = false;
711 	int items = 0;
712 	int credits = q->count >> 1;
713 
714 	memset(&active_cqns, 0, sizeof(active_cqns));
715 
716 	while ((eqe = mlxsw_pci_eq_sw_eqe_get(q))) {
717 		u8 event_type = mlxsw_pci_eqe_event_type_get(eqe);
718 
719 		switch (event_type) {
720 		case MLXSW_PCI_EQE_EVENT_TYPE_CMD:
721 			mlxsw_pci_eq_cmd_event(mlxsw_pci, eqe);
722 			q->u.eq.ev_cmd_count++;
723 			break;
724 		case MLXSW_PCI_EQE_EVENT_TYPE_COMP:
725 			cqn = mlxsw_pci_eqe_cqn_get(eqe);
726 			set_bit(cqn, active_cqns);
727 			cq_handle = true;
728 			q->u.eq.ev_comp_count++;
729 			break;
730 		default:
731 			q->u.eq.ev_other_count++;
732 		}
733 		if (++items == credits)
734 			break;
735 	}
736 	if (items) {
737 		mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
738 		mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
739 	}
740 
741 	if (!cq_handle)
742 		return;
743 	for_each_set_bit(cqn, active_cqns, cq_count) {
744 		q = mlxsw_pci_cq_get(mlxsw_pci, cqn);
745 		mlxsw_pci_queue_tasklet_schedule(q);
746 	}
747 }
748 
749 struct mlxsw_pci_queue_ops {
750 	const char *name;
751 	enum mlxsw_pci_queue_type type;
752 	int (*init)(struct mlxsw_pci *mlxsw_pci, char *mbox,
753 		    struct mlxsw_pci_queue *q);
754 	void (*fini)(struct mlxsw_pci *mlxsw_pci,
755 		     struct mlxsw_pci_queue *q);
756 	void (*tasklet)(unsigned long data);
757 	u16 elem_count;
758 	u8 elem_size;
759 };
760 
761 static const struct mlxsw_pci_queue_ops mlxsw_pci_sdq_ops = {
762 	.type		= MLXSW_PCI_QUEUE_TYPE_SDQ,
763 	.init		= mlxsw_pci_sdq_init,
764 	.fini		= mlxsw_pci_sdq_fini,
765 	.elem_count	= MLXSW_PCI_WQE_COUNT,
766 	.elem_size	= MLXSW_PCI_WQE_SIZE,
767 };
768 
769 static const struct mlxsw_pci_queue_ops mlxsw_pci_rdq_ops = {
770 	.type		= MLXSW_PCI_QUEUE_TYPE_RDQ,
771 	.init		= mlxsw_pci_rdq_init,
772 	.fini		= mlxsw_pci_rdq_fini,
773 	.elem_count	= MLXSW_PCI_WQE_COUNT,
774 	.elem_size	= MLXSW_PCI_WQE_SIZE
775 };
776 
777 static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = {
778 	.type		= MLXSW_PCI_QUEUE_TYPE_CQ,
779 	.init		= mlxsw_pci_cq_init,
780 	.fini		= mlxsw_pci_cq_fini,
781 	.tasklet	= mlxsw_pci_cq_tasklet,
782 	.elem_count	= MLXSW_PCI_CQE_COUNT,
783 	.elem_size	= MLXSW_PCI_CQE_SIZE
784 };
785 
786 static const struct mlxsw_pci_queue_ops mlxsw_pci_eq_ops = {
787 	.type		= MLXSW_PCI_QUEUE_TYPE_EQ,
788 	.init		= mlxsw_pci_eq_init,
789 	.fini		= mlxsw_pci_eq_fini,
790 	.tasklet	= mlxsw_pci_eq_tasklet,
791 	.elem_count	= MLXSW_PCI_EQE_COUNT,
792 	.elem_size	= MLXSW_PCI_EQE_SIZE
793 };
794 
795 static int mlxsw_pci_queue_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
796 				const struct mlxsw_pci_queue_ops *q_ops,
797 				struct mlxsw_pci_queue *q, u8 q_num)
798 {
799 	struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
800 	int i;
801 	int err;
802 
803 	spin_lock_init(&q->lock);
804 	q->num = q_num;
805 	q->count = q_ops->elem_count;
806 	q->elem_size = q_ops->elem_size;
807 	q->type = q_ops->type;
808 	q->pci = mlxsw_pci;
809 
810 	if (q_ops->tasklet)
811 		tasklet_init(&q->tasklet, q_ops->tasklet, (unsigned long) q);
812 
813 	mem_item->size = MLXSW_PCI_AQ_SIZE;
814 	mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
815 					     mem_item->size,
816 					     &mem_item->mapaddr);
817 	if (!mem_item->buf)
818 		return -ENOMEM;
819 	memset(mem_item->buf, 0, mem_item->size);
820 
821 	q->elem_info = kcalloc(q->count, sizeof(*q->elem_info), GFP_KERNEL);
822 	if (!q->elem_info) {
823 		err = -ENOMEM;
824 		goto err_elem_info_alloc;
825 	}
826 
827 	/* Initialize dma mapped elements info elem_info for
828 	 * future easy access.
829 	 */
830 	for (i = 0; i < q->count; i++) {
831 		struct mlxsw_pci_queue_elem_info *elem_info;
832 
833 		elem_info = mlxsw_pci_queue_elem_info_get(q, i);
834 		elem_info->elem =
835 			__mlxsw_pci_queue_elem_get(q, q_ops->elem_size, i);
836 	}
837 
838 	mlxsw_cmd_mbox_zero(mbox);
839 	err = q_ops->init(mlxsw_pci, mbox, q);
840 	if (err)
841 		goto err_q_ops_init;
842 	return 0;
843 
844 err_q_ops_init:
845 	kfree(q->elem_info);
846 err_elem_info_alloc:
847 	pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
848 			    mem_item->buf, mem_item->mapaddr);
849 	return err;
850 }
851 
852 static void mlxsw_pci_queue_fini(struct mlxsw_pci *mlxsw_pci,
853 				 const struct mlxsw_pci_queue_ops *q_ops,
854 				 struct mlxsw_pci_queue *q)
855 {
856 	struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
857 
858 	q_ops->fini(mlxsw_pci, q);
859 	kfree(q->elem_info);
860 	pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
861 			    mem_item->buf, mem_item->mapaddr);
862 }
863 
864 static int mlxsw_pci_queue_group_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
865 				      const struct mlxsw_pci_queue_ops *q_ops,
866 				      u8 num_qs)
867 {
868 	struct mlxsw_pci_queue_type_group *queue_group;
869 	int i;
870 	int err;
871 
872 	queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
873 	queue_group->q = kcalloc(num_qs, sizeof(*queue_group->q), GFP_KERNEL);
874 	if (!queue_group->q)
875 		return -ENOMEM;
876 
877 	for (i = 0; i < num_qs; i++) {
878 		err = mlxsw_pci_queue_init(mlxsw_pci, mbox, q_ops,
879 					   &queue_group->q[i], i);
880 		if (err)
881 			goto err_queue_init;
882 	}
883 	queue_group->count = num_qs;
884 
885 	return 0;
886 
887 err_queue_init:
888 	for (i--; i >= 0; i--)
889 		mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
890 	kfree(queue_group->q);
891 	return err;
892 }
893 
894 static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,
895 				       const struct mlxsw_pci_queue_ops *q_ops)
896 {
897 	struct mlxsw_pci_queue_type_group *queue_group;
898 	int i;
899 
900 	queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
901 	for (i = 0; i < queue_group->count; i++)
902 		mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
903 	kfree(queue_group->q);
904 }
905 
906 static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
907 {
908 	struct pci_dev *pdev = mlxsw_pci->pdev;
909 	u8 num_sdqs;
910 	u8 sdq_log2sz;
911 	u8 num_rdqs;
912 	u8 rdq_log2sz;
913 	u8 num_cqs;
914 	u8 cq_log2sz;
915 	u8 num_eqs;
916 	u8 eq_log2sz;
917 	int err;
918 
919 	mlxsw_cmd_mbox_zero(mbox);
920 	err = mlxsw_cmd_query_aq_cap(mlxsw_pci->core, mbox);
921 	if (err)
922 		return err;
923 
924 	num_sdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_sdqs_get(mbox);
925 	sdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_sdq_sz_get(mbox);
926 	num_rdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_rdqs_get(mbox);
927 	rdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_rdq_sz_get(mbox);
928 	num_cqs = mlxsw_cmd_mbox_query_aq_cap_max_num_cqs_get(mbox);
929 	cq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cq_sz_get(mbox);
930 	num_eqs = mlxsw_cmd_mbox_query_aq_cap_max_num_eqs_get(mbox);
931 	eq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_eq_sz_get(mbox);
932 
933 	if (num_sdqs + num_rdqs > num_cqs ||
934 	    num_cqs > MLXSW_PCI_CQS_MAX || num_eqs != MLXSW_PCI_EQS_COUNT) {
935 		dev_err(&pdev->dev, "Unsupported number of queues\n");
936 		return -EINVAL;
937 	}
938 
939 	if ((1 << sdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
940 	    (1 << rdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
941 	    (1 << cq_log2sz != MLXSW_PCI_CQE_COUNT) ||
942 	    (1 << eq_log2sz != MLXSW_PCI_EQE_COUNT)) {
943 		dev_err(&pdev->dev, "Unsupported number of async queue descriptors\n");
944 		return -EINVAL;
945 	}
946 
947 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_eq_ops,
948 					 num_eqs);
949 	if (err) {
950 		dev_err(&pdev->dev, "Failed to initialize event queues\n");
951 		return err;
952 	}
953 
954 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_cq_ops,
955 					 num_cqs);
956 	if (err) {
957 		dev_err(&pdev->dev, "Failed to initialize completion queues\n");
958 		goto err_cqs_init;
959 	}
960 
961 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_sdq_ops,
962 					 num_sdqs);
963 	if (err) {
964 		dev_err(&pdev->dev, "Failed to initialize send descriptor queues\n");
965 		goto err_sdqs_init;
966 	}
967 
968 	err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_rdq_ops,
969 					 num_rdqs);
970 	if (err) {
971 		dev_err(&pdev->dev, "Failed to initialize receive descriptor queues\n");
972 		goto err_rdqs_init;
973 	}
974 
975 	/* We have to poll in command interface until queues are initialized */
976 	mlxsw_pci->cmd.nopoll = true;
977 	return 0;
978 
979 err_rdqs_init:
980 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
981 err_sdqs_init:
982 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
983 err_cqs_init:
984 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
985 	return err;
986 }
987 
988 static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci)
989 {
990 	mlxsw_pci->cmd.nopoll = false;
991 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops);
992 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
993 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
994 	mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
995 }
996 
997 static void
998 mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci,
999 				     char *mbox, int index,
1000 				     const struct mlxsw_swid_config *swid)
1001 {
1002 	u8 mask = 0;
1003 
1004 	if (swid->used_type) {
1005 		mlxsw_cmd_mbox_config_profile_swid_config_type_set(
1006 			mbox, index, swid->type);
1007 		mask |= 1;
1008 	}
1009 	if (swid->used_properties) {
1010 		mlxsw_cmd_mbox_config_profile_swid_config_properties_set(
1011 			mbox, index, swid->properties);
1012 		mask |= 2;
1013 	}
1014 	mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask);
1015 }
1016 
1017 static int mlxsw_pci_resources_query(struct mlxsw_pci *mlxsw_pci, char *mbox,
1018 				     struct mlxsw_res *res,
1019 				     u8 query_enabled)
1020 {
1021 	int index, i;
1022 	u64 data;
1023 	u16 id;
1024 	int err;
1025 
1026 	/* Not all the versions support resources query */
1027 	if (!query_enabled)
1028 		return 0;
1029 
1030 	mlxsw_cmd_mbox_zero(mbox);
1031 
1032 	for (index = 0; index < MLXSW_CMD_QUERY_RESOURCES_MAX_QUERIES;
1033 	     index++) {
1034 		err = mlxsw_cmd_query_resources(mlxsw_pci->core, mbox, index);
1035 		if (err)
1036 			return err;
1037 
1038 		for (i = 0; i < MLXSW_CMD_QUERY_RESOURCES_PER_QUERY; i++) {
1039 			id = mlxsw_cmd_mbox_query_resource_id_get(mbox, i);
1040 			data = mlxsw_cmd_mbox_query_resource_data_get(mbox, i);
1041 
1042 			if (id == MLXSW_CMD_QUERY_RESOURCES_TABLE_END_ID)
1043 				return 0;
1044 
1045 			mlxsw_res_parse(res, id, data);
1046 		}
1047 	}
1048 
1049 	/* If after MLXSW_RESOURCES_QUERY_MAX_QUERIES we still didn't get
1050 	 * MLXSW_RESOURCES_TABLE_END_ID, something went bad in the FW.
1051 	 */
1052 	return -EIO;
1053 }
1054 
1055 static int
1056 mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_config_profile *profile,
1057 				struct mlxsw_res *res)
1058 {
1059 	u32 single_size, double_size, linear_size;
1060 
1061 	if (!MLXSW_RES_VALID(res, KVD_SINGLE_MIN_SIZE) ||
1062 	    !MLXSW_RES_VALID(res, KVD_DOUBLE_MIN_SIZE) ||
1063 	    !profile->used_kvd_split_data)
1064 		return -EIO;
1065 
1066 	linear_size = profile->kvd_linear_size;
1067 
1068 	/* The hash part is what left of the kvd without the
1069 	 * linear part. It is split to the single size and
1070 	 * double size by the parts ratio from the profile.
1071 	 * Both sizes must be a multiplications of the
1072 	 * granularity from the profile.
1073 	 */
1074 	double_size = MLXSW_RES_GET(res, KVD_SIZE) - linear_size;
1075 	double_size *= profile->kvd_hash_double_parts;
1076 	double_size /= profile->kvd_hash_double_parts +
1077 		       profile->kvd_hash_single_parts;
1078 	double_size /= profile->kvd_hash_granularity;
1079 	double_size *= profile->kvd_hash_granularity;
1080 	single_size = MLXSW_RES_GET(res, KVD_SIZE) - double_size -
1081 		      linear_size;
1082 
1083 	/* Check results are legal. */
1084 	if (single_size < MLXSW_RES_GET(res, KVD_SINGLE_MIN_SIZE) ||
1085 	    double_size < MLXSW_RES_GET(res, KVD_DOUBLE_MIN_SIZE) ||
1086 	    MLXSW_RES_GET(res, KVD_SIZE) < linear_size)
1087 		return -EIO;
1088 
1089 	MLXSW_RES_SET(res, KVD_SINGLE_SIZE, single_size);
1090 	MLXSW_RES_SET(res, KVD_DOUBLE_SIZE, double_size);
1091 	MLXSW_RES_SET(res, KVD_LINEAR_SIZE, linear_size);
1092 
1093 	return 0;
1094 }
1095 
1096 static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
1097 				    const struct mlxsw_config_profile *profile,
1098 				    struct mlxsw_res *res)
1099 {
1100 	int i;
1101 	int err;
1102 
1103 	mlxsw_cmd_mbox_zero(mbox);
1104 
1105 	if (profile->used_max_vepa_channels) {
1106 		mlxsw_cmd_mbox_config_profile_set_max_vepa_channels_set(
1107 			mbox, 1);
1108 		mlxsw_cmd_mbox_config_profile_max_vepa_channels_set(
1109 			mbox, profile->max_vepa_channels);
1110 	}
1111 	if (profile->used_max_mid) {
1112 		mlxsw_cmd_mbox_config_profile_set_max_mid_set(
1113 			mbox, 1);
1114 		mlxsw_cmd_mbox_config_profile_max_mid_set(
1115 			mbox, profile->max_mid);
1116 	}
1117 	if (profile->used_max_pgt) {
1118 		mlxsw_cmd_mbox_config_profile_set_max_pgt_set(
1119 			mbox, 1);
1120 		mlxsw_cmd_mbox_config_profile_max_pgt_set(
1121 			mbox, profile->max_pgt);
1122 	}
1123 	if (profile->used_max_system_port) {
1124 		mlxsw_cmd_mbox_config_profile_set_max_system_port_set(
1125 			mbox, 1);
1126 		mlxsw_cmd_mbox_config_profile_max_system_port_set(
1127 			mbox, profile->max_system_port);
1128 	}
1129 	if (profile->used_max_vlan_groups) {
1130 		mlxsw_cmd_mbox_config_profile_set_max_vlan_groups_set(
1131 			mbox, 1);
1132 		mlxsw_cmd_mbox_config_profile_max_vlan_groups_set(
1133 			mbox, profile->max_vlan_groups);
1134 	}
1135 	if (profile->used_max_regions) {
1136 		mlxsw_cmd_mbox_config_profile_set_max_regions_set(
1137 			mbox, 1);
1138 		mlxsw_cmd_mbox_config_profile_max_regions_set(
1139 			mbox, profile->max_regions);
1140 	}
1141 	if (profile->used_flood_tables) {
1142 		mlxsw_cmd_mbox_config_profile_set_flood_tables_set(
1143 			mbox, 1);
1144 		mlxsw_cmd_mbox_config_profile_max_flood_tables_set(
1145 			mbox, profile->max_flood_tables);
1146 		mlxsw_cmd_mbox_config_profile_max_vid_flood_tables_set(
1147 			mbox, profile->max_vid_flood_tables);
1148 		mlxsw_cmd_mbox_config_profile_max_fid_offset_flood_tables_set(
1149 			mbox, profile->max_fid_offset_flood_tables);
1150 		mlxsw_cmd_mbox_config_profile_fid_offset_flood_table_size_set(
1151 			mbox, profile->fid_offset_flood_table_size);
1152 		mlxsw_cmd_mbox_config_profile_max_fid_flood_tables_set(
1153 			mbox, profile->max_fid_flood_tables);
1154 		mlxsw_cmd_mbox_config_profile_fid_flood_table_size_set(
1155 			mbox, profile->fid_flood_table_size);
1156 	}
1157 	if (profile->used_flood_mode) {
1158 		mlxsw_cmd_mbox_config_profile_set_flood_mode_set(
1159 			mbox, 1);
1160 		mlxsw_cmd_mbox_config_profile_flood_mode_set(
1161 			mbox, profile->flood_mode);
1162 	}
1163 	if (profile->used_max_ib_mc) {
1164 		mlxsw_cmd_mbox_config_profile_set_max_ib_mc_set(
1165 			mbox, 1);
1166 		mlxsw_cmd_mbox_config_profile_max_ib_mc_set(
1167 			mbox, profile->max_ib_mc);
1168 	}
1169 	if (profile->used_max_pkey) {
1170 		mlxsw_cmd_mbox_config_profile_set_max_pkey_set(
1171 			mbox, 1);
1172 		mlxsw_cmd_mbox_config_profile_max_pkey_set(
1173 			mbox, profile->max_pkey);
1174 	}
1175 	if (profile->used_ar_sec) {
1176 		mlxsw_cmd_mbox_config_profile_set_ar_sec_set(
1177 			mbox, 1);
1178 		mlxsw_cmd_mbox_config_profile_ar_sec_set(
1179 			mbox, profile->ar_sec);
1180 	}
1181 	if (profile->used_adaptive_routing_group_cap) {
1182 		mlxsw_cmd_mbox_config_profile_set_adaptive_routing_group_cap_set(
1183 			mbox, 1);
1184 		mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set(
1185 			mbox, profile->adaptive_routing_group_cap);
1186 	}
1187 	if (MLXSW_RES_VALID(res, KVD_SIZE)) {
1188 		err = mlxsw_pci_profile_get_kvd_sizes(profile, res);
1189 		if (err)
1190 			return err;
1191 
1192 		mlxsw_cmd_mbox_config_profile_set_kvd_linear_size_set(mbox, 1);
1193 		mlxsw_cmd_mbox_config_profile_kvd_linear_size_set(mbox,
1194 					MLXSW_RES_GET(res, KVD_LINEAR_SIZE));
1195 		mlxsw_cmd_mbox_config_profile_set_kvd_hash_single_size_set(mbox,
1196 									   1);
1197 		mlxsw_cmd_mbox_config_profile_kvd_hash_single_size_set(mbox,
1198 					MLXSW_RES_GET(res, KVD_SINGLE_SIZE));
1199 		mlxsw_cmd_mbox_config_profile_set_kvd_hash_double_size_set(
1200 								mbox, 1);
1201 		mlxsw_cmd_mbox_config_profile_kvd_hash_double_size_set(mbox,
1202 					MLXSW_RES_GET(res, KVD_DOUBLE_SIZE));
1203 	}
1204 
1205 	for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++)
1206 		mlxsw_pci_config_profile_swid_config(mlxsw_pci, mbox, i,
1207 						     &profile->swid_config[i]);
1208 
1209 	return mlxsw_cmd_config_profile_set(mlxsw_pci->core, mbox);
1210 }
1211 
1212 static int mlxsw_pci_boardinfo(struct mlxsw_pci *mlxsw_pci, char *mbox)
1213 {
1214 	struct mlxsw_bus_info *bus_info = &mlxsw_pci->bus_info;
1215 	int err;
1216 
1217 	mlxsw_cmd_mbox_zero(mbox);
1218 	err = mlxsw_cmd_boardinfo(mlxsw_pci->core, mbox);
1219 	if (err)
1220 		return err;
1221 	mlxsw_cmd_mbox_boardinfo_vsd_memcpy_from(mbox, bus_info->vsd);
1222 	mlxsw_cmd_mbox_boardinfo_psid_memcpy_from(mbox, bus_info->psid);
1223 	return 0;
1224 }
1225 
1226 static int mlxsw_pci_fw_area_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1227 				  u16 num_pages)
1228 {
1229 	struct mlxsw_pci_mem_item *mem_item;
1230 	int nent = 0;
1231 	int i;
1232 	int err;
1233 
1234 	mlxsw_pci->fw_area.items = kcalloc(num_pages, sizeof(*mem_item),
1235 					   GFP_KERNEL);
1236 	if (!mlxsw_pci->fw_area.items)
1237 		return -ENOMEM;
1238 	mlxsw_pci->fw_area.count = num_pages;
1239 
1240 	mlxsw_cmd_mbox_zero(mbox);
1241 	for (i = 0; i < num_pages; i++) {
1242 		mem_item = &mlxsw_pci->fw_area.items[i];
1243 
1244 		mem_item->size = MLXSW_PCI_PAGE_SIZE;
1245 		mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
1246 						     mem_item->size,
1247 						     &mem_item->mapaddr);
1248 		if (!mem_item->buf) {
1249 			err = -ENOMEM;
1250 			goto err_alloc;
1251 		}
1252 		mlxsw_cmd_mbox_map_fa_pa_set(mbox, nent, mem_item->mapaddr);
1253 		mlxsw_cmd_mbox_map_fa_log2size_set(mbox, nent, 0); /* 1 page */
1254 		if (++nent == MLXSW_CMD_MAP_FA_VPM_ENTRIES_MAX) {
1255 			err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1256 			if (err)
1257 				goto err_cmd_map_fa;
1258 			nent = 0;
1259 			mlxsw_cmd_mbox_zero(mbox);
1260 		}
1261 	}
1262 
1263 	if (nent) {
1264 		err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1265 		if (err)
1266 			goto err_cmd_map_fa;
1267 	}
1268 
1269 	return 0;
1270 
1271 err_cmd_map_fa:
1272 err_alloc:
1273 	for (i--; i >= 0; i--) {
1274 		mem_item = &mlxsw_pci->fw_area.items[i];
1275 
1276 		pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1277 				    mem_item->buf, mem_item->mapaddr);
1278 	}
1279 	kfree(mlxsw_pci->fw_area.items);
1280 	return err;
1281 }
1282 
1283 static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci)
1284 {
1285 	struct mlxsw_pci_mem_item *mem_item;
1286 	int i;
1287 
1288 	mlxsw_cmd_unmap_fa(mlxsw_pci->core);
1289 
1290 	for (i = 0; i < mlxsw_pci->fw_area.count; i++) {
1291 		mem_item = &mlxsw_pci->fw_area.items[i];
1292 
1293 		pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1294 				    mem_item->buf, mem_item->mapaddr);
1295 	}
1296 	kfree(mlxsw_pci->fw_area.items);
1297 }
1298 
1299 static irqreturn_t mlxsw_pci_eq_irq_handler(int irq, void *dev_id)
1300 {
1301 	struct mlxsw_pci *mlxsw_pci = dev_id;
1302 	struct mlxsw_pci_queue *q;
1303 	int i;
1304 
1305 	for (i = 0; i < MLXSW_PCI_EQS_COUNT; i++) {
1306 		q = mlxsw_pci_eq_get(mlxsw_pci, i);
1307 		mlxsw_pci_queue_tasklet_schedule(q);
1308 	}
1309 	return IRQ_HANDLED;
1310 }
1311 
1312 static int mlxsw_pci_mbox_alloc(struct mlxsw_pci *mlxsw_pci,
1313 				struct mlxsw_pci_mem_item *mbox)
1314 {
1315 	struct pci_dev *pdev = mlxsw_pci->pdev;
1316 	int err = 0;
1317 
1318 	mbox->size = MLXSW_CMD_MBOX_SIZE;
1319 	mbox->buf = pci_alloc_consistent(pdev, MLXSW_CMD_MBOX_SIZE,
1320 					 &mbox->mapaddr);
1321 	if (!mbox->buf) {
1322 		dev_err(&pdev->dev, "Failed allocating memory for mailbox\n");
1323 		err = -ENOMEM;
1324 	}
1325 
1326 	return err;
1327 }
1328 
1329 static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci,
1330 				struct mlxsw_pci_mem_item *mbox)
1331 {
1332 	struct pci_dev *pdev = mlxsw_pci->pdev;
1333 
1334 	pci_free_consistent(pdev, MLXSW_CMD_MBOX_SIZE, mbox->buf,
1335 			    mbox->mapaddr);
1336 }
1337 
1338 static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
1339 			  const struct mlxsw_config_profile *profile,
1340 			  struct mlxsw_res *res)
1341 {
1342 	struct mlxsw_pci *mlxsw_pci = bus_priv;
1343 	struct pci_dev *pdev = mlxsw_pci->pdev;
1344 	char *mbox;
1345 	u16 num_pages;
1346 	int err;
1347 
1348 	mutex_init(&mlxsw_pci->cmd.lock);
1349 	init_waitqueue_head(&mlxsw_pci->cmd.wait);
1350 
1351 	mlxsw_pci->core = mlxsw_core;
1352 
1353 	mbox = mlxsw_cmd_mbox_alloc();
1354 	if (!mbox)
1355 		return -ENOMEM;
1356 
1357 	err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1358 	if (err)
1359 		goto mbox_put;
1360 
1361 	err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1362 	if (err)
1363 		goto err_out_mbox_alloc;
1364 
1365 	err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
1366 	if (err)
1367 		goto err_query_fw;
1368 
1369 	mlxsw_pci->bus_info.fw_rev.major =
1370 		mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
1371 	mlxsw_pci->bus_info.fw_rev.minor =
1372 		mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
1373 	mlxsw_pci->bus_info.fw_rev.subminor =
1374 		mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
1375 
1376 	if (mlxsw_cmd_mbox_query_fw_cmd_interface_rev_get(mbox) != 1) {
1377 		dev_err(&pdev->dev, "Unsupported cmd interface revision ID queried from hw\n");
1378 		err = -EINVAL;
1379 		goto err_iface_rev;
1380 	}
1381 	if (mlxsw_cmd_mbox_query_fw_doorbell_page_bar_get(mbox) != 0) {
1382 		dev_err(&pdev->dev, "Unsupported doorbell page bar queried from hw\n");
1383 		err = -EINVAL;
1384 		goto err_doorbell_page_bar;
1385 	}
1386 
1387 	mlxsw_pci->doorbell_offset =
1388 		mlxsw_cmd_mbox_query_fw_doorbell_page_offset_get(mbox);
1389 
1390 	num_pages = mlxsw_cmd_mbox_query_fw_fw_pages_get(mbox);
1391 	err = mlxsw_pci_fw_area_init(mlxsw_pci, mbox, num_pages);
1392 	if (err)
1393 		goto err_fw_area_init;
1394 
1395 	err = mlxsw_pci_boardinfo(mlxsw_pci, mbox);
1396 	if (err)
1397 		goto err_boardinfo;
1398 
1399 	err = mlxsw_pci_resources_query(mlxsw_pci, mbox, res,
1400 					profile->resource_query_enable);
1401 	if (err)
1402 		goto err_query_resources;
1403 
1404 	err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile, res);
1405 	if (err)
1406 		goto err_config_profile;
1407 
1408 	err = mlxsw_pci_aqs_init(mlxsw_pci, mbox);
1409 	if (err)
1410 		goto err_aqs_init;
1411 
1412 	err = request_irq(mlxsw_pci->msix_entry.vector,
1413 			  mlxsw_pci_eq_irq_handler, 0,
1414 			  mlxsw_pci->bus_info.device_kind, mlxsw_pci);
1415 	if (err) {
1416 		dev_err(&pdev->dev, "IRQ request failed\n");
1417 		goto err_request_eq_irq;
1418 	}
1419 
1420 	goto mbox_put;
1421 
1422 err_request_eq_irq:
1423 	mlxsw_pci_aqs_fini(mlxsw_pci);
1424 err_aqs_init:
1425 err_config_profile:
1426 err_query_resources:
1427 err_boardinfo:
1428 	mlxsw_pci_fw_area_fini(mlxsw_pci);
1429 err_fw_area_init:
1430 err_doorbell_page_bar:
1431 err_iface_rev:
1432 err_query_fw:
1433 	mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1434 err_out_mbox_alloc:
1435 	mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1436 mbox_put:
1437 	mlxsw_cmd_mbox_free(mbox);
1438 	return err;
1439 }
1440 
1441 static void mlxsw_pci_fini(void *bus_priv)
1442 {
1443 	struct mlxsw_pci *mlxsw_pci = bus_priv;
1444 
1445 	free_irq(mlxsw_pci->msix_entry.vector, mlxsw_pci);
1446 	mlxsw_pci_aqs_fini(mlxsw_pci);
1447 	mlxsw_pci_fw_area_fini(mlxsw_pci);
1448 	mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1449 	mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1450 }
1451 
1452 static struct mlxsw_pci_queue *
1453 mlxsw_pci_sdq_pick(struct mlxsw_pci *mlxsw_pci,
1454 		   const struct mlxsw_tx_info *tx_info)
1455 {
1456 	u8 sdqn = tx_info->local_port % mlxsw_pci_sdq_count(mlxsw_pci);
1457 
1458 	return mlxsw_pci_sdq_get(mlxsw_pci, sdqn);
1459 }
1460 
1461 static bool mlxsw_pci_skb_transmit_busy(void *bus_priv,
1462 					const struct mlxsw_tx_info *tx_info)
1463 {
1464 	struct mlxsw_pci *mlxsw_pci = bus_priv;
1465 	struct mlxsw_pci_queue *q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1466 
1467 	return !mlxsw_pci_queue_elem_info_producer_get(q);
1468 }
1469 
1470 static int mlxsw_pci_skb_transmit(void *bus_priv, struct sk_buff *skb,
1471 				  const struct mlxsw_tx_info *tx_info)
1472 {
1473 	struct mlxsw_pci *mlxsw_pci = bus_priv;
1474 	struct mlxsw_pci_queue *q;
1475 	struct mlxsw_pci_queue_elem_info *elem_info;
1476 	char *wqe;
1477 	int i;
1478 	int err;
1479 
1480 	if (skb_shinfo(skb)->nr_frags > MLXSW_PCI_WQE_SG_ENTRIES - 1) {
1481 		err = skb_linearize(skb);
1482 		if (err)
1483 			return err;
1484 	}
1485 
1486 	q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1487 	spin_lock_bh(&q->lock);
1488 	elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
1489 	if (!elem_info) {
1490 		/* queue is full */
1491 		err = -EAGAIN;
1492 		goto unlock;
1493 	}
1494 	elem_info->u.sdq.skb = skb;
1495 
1496 	wqe = elem_info->elem;
1497 	mlxsw_pci_wqe_c_set(wqe, 1); /* always report completion */
1498 	mlxsw_pci_wqe_lp_set(wqe, !!tx_info->is_emad);
1499 	mlxsw_pci_wqe_type_set(wqe, MLXSW_PCI_WQE_TYPE_ETHERNET);
1500 
1501 	err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
1502 				     skb_headlen(skb), DMA_TO_DEVICE);
1503 	if (err)
1504 		goto unlock;
1505 
1506 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1507 		const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1508 
1509 		err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, i + 1,
1510 					     skb_frag_address(frag),
1511 					     skb_frag_size(frag),
1512 					     DMA_TO_DEVICE);
1513 		if (err)
1514 			goto unmap_frags;
1515 	}
1516 
1517 	/* Set unused sq entries byte count to zero. */
1518 	for (i++; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
1519 		mlxsw_pci_wqe_byte_count_set(wqe, i, 0);
1520 
1521 	/* Everything is set up, ring producer doorbell to get HW going */
1522 	q->producer_counter++;
1523 	mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
1524 
1525 	goto unlock;
1526 
1527 unmap_frags:
1528 	for (; i >= 0; i--)
1529 		mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
1530 unlock:
1531 	spin_unlock_bh(&q->lock);
1532 	return err;
1533 }
1534 
1535 static int mlxsw_pci_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
1536 			      u32 in_mod, bool out_mbox_direct,
1537 			      char *in_mbox, size_t in_mbox_size,
1538 			      char *out_mbox, size_t out_mbox_size,
1539 			      u8 *p_status)
1540 {
1541 	struct mlxsw_pci *mlxsw_pci = bus_priv;
1542 	dma_addr_t in_mapaddr = mlxsw_pci->cmd.in_mbox.mapaddr;
1543 	dma_addr_t out_mapaddr = mlxsw_pci->cmd.out_mbox.mapaddr;
1544 	bool evreq = mlxsw_pci->cmd.nopoll;
1545 	unsigned long timeout = msecs_to_jiffies(MLXSW_PCI_CIR_TIMEOUT_MSECS);
1546 	bool *p_wait_done = &mlxsw_pci->cmd.wait_done;
1547 	int err;
1548 
1549 	*p_status = MLXSW_CMD_STATUS_OK;
1550 
1551 	err = mutex_lock_interruptible(&mlxsw_pci->cmd.lock);
1552 	if (err)
1553 		return err;
1554 
1555 	if (in_mbox)
1556 		memcpy(mlxsw_pci->cmd.in_mbox.buf, in_mbox, in_mbox_size);
1557 	mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_HI, upper_32_bits(in_mapaddr));
1558 	mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_LO, lower_32_bits(in_mapaddr));
1559 
1560 	mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_HI, upper_32_bits(out_mapaddr));
1561 	mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_LO, lower_32_bits(out_mapaddr));
1562 
1563 	mlxsw_pci_write32(mlxsw_pci, CIR_IN_MODIFIER, in_mod);
1564 	mlxsw_pci_write32(mlxsw_pci, CIR_TOKEN, 0);
1565 
1566 	*p_wait_done = false;
1567 
1568 	wmb(); /* all needs to be written before we write control register */
1569 	mlxsw_pci_write32(mlxsw_pci, CIR_CTRL,
1570 			  MLXSW_PCI_CIR_CTRL_GO_BIT |
1571 			  (evreq ? MLXSW_PCI_CIR_CTRL_EVREQ_BIT : 0) |
1572 			  (opcode_mod << MLXSW_PCI_CIR_CTRL_OPCODE_MOD_SHIFT) |
1573 			  opcode);
1574 
1575 	if (!evreq) {
1576 		unsigned long end;
1577 
1578 		end = jiffies + timeout;
1579 		do {
1580 			u32 ctrl = mlxsw_pci_read32(mlxsw_pci, CIR_CTRL);
1581 
1582 			if (!(ctrl & MLXSW_PCI_CIR_CTRL_GO_BIT)) {
1583 				*p_wait_done = true;
1584 				*p_status = ctrl >> MLXSW_PCI_CIR_CTRL_STATUS_SHIFT;
1585 				break;
1586 			}
1587 			cond_resched();
1588 		} while (time_before(jiffies, end));
1589 	} else {
1590 		wait_event_timeout(mlxsw_pci->cmd.wait, *p_wait_done, timeout);
1591 		*p_status = mlxsw_pci->cmd.comp.status;
1592 	}
1593 
1594 	err = 0;
1595 	if (*p_wait_done) {
1596 		if (*p_status)
1597 			err = -EIO;
1598 	} else {
1599 		err = -ETIMEDOUT;
1600 	}
1601 
1602 	if (!err && out_mbox && out_mbox_direct) {
1603 		/* Some commands don't use output param as address to mailbox
1604 		 * but they store output directly into registers. In that case,
1605 		 * copy registers into mbox buffer.
1606 		 */
1607 		__be32 tmp;
1608 
1609 		if (!evreq) {
1610 			tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1611 							   CIR_OUT_PARAM_HI));
1612 			memcpy(out_mbox, &tmp, sizeof(tmp));
1613 			tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1614 							   CIR_OUT_PARAM_LO));
1615 			memcpy(out_mbox + sizeof(tmp), &tmp, sizeof(tmp));
1616 		}
1617 	} else if (!err && out_mbox) {
1618 		memcpy(out_mbox, mlxsw_pci->cmd.out_mbox.buf, out_mbox_size);
1619 	}
1620 
1621 	mutex_unlock(&mlxsw_pci->cmd.lock);
1622 
1623 	return err;
1624 }
1625 
1626 static const struct mlxsw_bus mlxsw_pci_bus = {
1627 	.kind			= "pci",
1628 	.init			= mlxsw_pci_init,
1629 	.fini			= mlxsw_pci_fini,
1630 	.skb_transmit_busy	= mlxsw_pci_skb_transmit_busy,
1631 	.skb_transmit		= mlxsw_pci_skb_transmit,
1632 	.cmd_exec		= mlxsw_pci_cmd_exec,
1633 	.features		= MLXSW_BUS_F_TXRX,
1634 };
1635 
1636 static int mlxsw_pci_sw_reset(struct mlxsw_pci *mlxsw_pci,
1637 			      const struct pci_device_id *id)
1638 {
1639 	unsigned long end;
1640 
1641 	mlxsw_pci_write32(mlxsw_pci, SW_RESET, MLXSW_PCI_SW_RESET_RST_BIT);
1642 	if (id->device == PCI_DEVICE_ID_MELLANOX_SWITCHX2) {
1643 		msleep(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
1644 		return 0;
1645 	}
1646 
1647 	wmb(); /* reset needs to be written before we read control register */
1648 	end = jiffies + msecs_to_jiffies(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
1649 	do {
1650 		u32 val = mlxsw_pci_read32(mlxsw_pci, FW_READY);
1651 
1652 		if ((val & MLXSW_PCI_FW_READY_MASK) == MLXSW_PCI_FW_READY_MAGIC)
1653 			break;
1654 		cond_resched();
1655 	} while (time_before(jiffies, end));
1656 	return 0;
1657 }
1658 
1659 static int mlxsw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1660 {
1661 	const char *driver_name = pdev->driver->name;
1662 	struct mlxsw_pci *mlxsw_pci;
1663 	int err;
1664 
1665 	mlxsw_pci = kzalloc(sizeof(*mlxsw_pci), GFP_KERNEL);
1666 	if (!mlxsw_pci)
1667 		return -ENOMEM;
1668 
1669 	err = pci_enable_device(pdev);
1670 	if (err) {
1671 		dev_err(&pdev->dev, "pci_enable_device failed\n");
1672 		goto err_pci_enable_device;
1673 	}
1674 
1675 	err = pci_request_regions(pdev, driver_name);
1676 	if (err) {
1677 		dev_err(&pdev->dev, "pci_request_regions failed\n");
1678 		goto err_pci_request_regions;
1679 	}
1680 
1681 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1682 	if (!err) {
1683 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1684 		if (err) {
1685 			dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
1686 			goto err_pci_set_dma_mask;
1687 		}
1688 	} else {
1689 		err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1690 		if (err) {
1691 			dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
1692 			goto err_pci_set_dma_mask;
1693 		}
1694 	}
1695 
1696 	if (pci_resource_len(pdev, 0) < MLXSW_PCI_BAR0_SIZE) {
1697 		dev_err(&pdev->dev, "invalid PCI region size\n");
1698 		err = -EINVAL;
1699 		goto err_pci_resource_len_check;
1700 	}
1701 
1702 	mlxsw_pci->hw_addr = ioremap(pci_resource_start(pdev, 0),
1703 				     pci_resource_len(pdev, 0));
1704 	if (!mlxsw_pci->hw_addr) {
1705 		dev_err(&pdev->dev, "ioremap failed\n");
1706 		err = -EIO;
1707 		goto err_ioremap;
1708 	}
1709 	pci_set_master(pdev);
1710 
1711 	mlxsw_pci->pdev = pdev;
1712 	pci_set_drvdata(pdev, mlxsw_pci);
1713 
1714 	err = mlxsw_pci_sw_reset(mlxsw_pci, id);
1715 	if (err) {
1716 		dev_err(&pdev->dev, "Software reset failed\n");
1717 		goto err_sw_reset;
1718 	}
1719 
1720 	err = pci_enable_msix_exact(pdev, &mlxsw_pci->msix_entry, 1);
1721 	if (err) {
1722 		dev_err(&pdev->dev, "MSI-X init failed\n");
1723 		goto err_msix_init;
1724 	}
1725 
1726 	mlxsw_pci->bus_info.device_kind = driver_name;
1727 	mlxsw_pci->bus_info.device_name = pci_name(mlxsw_pci->pdev);
1728 	mlxsw_pci->bus_info.dev = &pdev->dev;
1729 
1730 	err = mlxsw_core_bus_device_register(&mlxsw_pci->bus_info,
1731 					     &mlxsw_pci_bus, mlxsw_pci);
1732 	if (err) {
1733 		dev_err(&pdev->dev, "cannot register bus device\n");
1734 		goto err_bus_device_register;
1735 	}
1736 
1737 	return 0;
1738 
1739 err_bus_device_register:
1740 	pci_disable_msix(mlxsw_pci->pdev);
1741 err_msix_init:
1742 err_sw_reset:
1743 	iounmap(mlxsw_pci->hw_addr);
1744 err_ioremap:
1745 err_pci_resource_len_check:
1746 err_pci_set_dma_mask:
1747 	pci_release_regions(pdev);
1748 err_pci_request_regions:
1749 	pci_disable_device(pdev);
1750 err_pci_enable_device:
1751 	kfree(mlxsw_pci);
1752 	return err;
1753 }
1754 
1755 static void mlxsw_pci_remove(struct pci_dev *pdev)
1756 {
1757 	struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
1758 
1759 	mlxsw_core_bus_device_unregister(mlxsw_pci->core);
1760 	pci_disable_msix(mlxsw_pci->pdev);
1761 	iounmap(mlxsw_pci->hw_addr);
1762 	pci_release_regions(mlxsw_pci->pdev);
1763 	pci_disable_device(mlxsw_pci->pdev);
1764 	kfree(mlxsw_pci);
1765 }
1766 
1767 int mlxsw_pci_driver_register(struct pci_driver *pci_driver)
1768 {
1769 	pci_driver->probe = mlxsw_pci_probe;
1770 	pci_driver->remove = mlxsw_pci_remove;
1771 	return pci_register_driver(pci_driver);
1772 }
1773 EXPORT_SYMBOL(mlxsw_pci_driver_register);
1774 
1775 void mlxsw_pci_driver_unregister(struct pci_driver *pci_driver)
1776 {
1777 	pci_unregister_driver(pci_driver);
1778 }
1779 EXPORT_SYMBOL(mlxsw_pci_driver_unregister);
1780 
1781 static int __init mlxsw_pci_module_init(void)
1782 {
1783 	return 0;
1784 }
1785 
1786 static void __exit mlxsw_pci_module_exit(void)
1787 {
1788 }
1789 
1790 module_init(mlxsw_pci_module_init);
1791 module_exit(mlxsw_pci_module_exit);
1792 
1793 MODULE_LICENSE("Dual BSD/GPL");
1794 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1795 MODULE_DESCRIPTION("Mellanox switch PCI interface driver");
1796