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>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
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/debugfs.h>
48 #include <linux/seq_file.h>
49 #include <linux/string.h>
56 #include "resources.h"
58 static const char mlxsw_pci_driver_name[] = "mlxsw_pci";
60 static struct dentry *mlxsw_pci_dbg_root;
62 #define mlxsw_pci_write32(mlxsw_pci, reg, val) \
63 iowrite32be(val, (mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
64 #define mlxsw_pci_read32(mlxsw_pci, reg) \
65 ioread32be((mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
67 enum mlxsw_pci_queue_type {
68 MLXSW_PCI_QUEUE_TYPE_SDQ,
69 MLXSW_PCI_QUEUE_TYPE_RDQ,
70 MLXSW_PCI_QUEUE_TYPE_CQ,
71 MLXSW_PCI_QUEUE_TYPE_EQ,
74 static const char *mlxsw_pci_queue_type_str(enum mlxsw_pci_queue_type q_type)
77 case MLXSW_PCI_QUEUE_TYPE_SDQ:
79 case MLXSW_PCI_QUEUE_TYPE_RDQ:
81 case MLXSW_PCI_QUEUE_TYPE_CQ:
83 case MLXSW_PCI_QUEUE_TYPE_EQ:
89 #define MLXSW_PCI_QUEUE_TYPE_COUNT 4
91 static const u16 mlxsw_pci_doorbell_type_offset[] = {
92 MLXSW_PCI_DOORBELL_SDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_SDQ */
93 MLXSW_PCI_DOORBELL_RDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_RDQ */
94 MLXSW_PCI_DOORBELL_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
95 MLXSW_PCI_DOORBELL_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
98 static const u16 mlxsw_pci_doorbell_arm_type_offset[] = {
101 MLXSW_PCI_DOORBELL_ARM_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
102 MLXSW_PCI_DOORBELL_ARM_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
105 struct mlxsw_pci_mem_item {
111 struct mlxsw_pci_queue_elem_info {
112 char *elem; /* pointer to actual dma mapped element mem chunk */
123 struct mlxsw_pci_queue {
124 spinlock_t lock; /* for queue accesses */
125 struct mlxsw_pci_mem_item mem_item;
126 struct mlxsw_pci_queue_elem_info *elem_info;
127 u16 producer_counter;
128 u16 consumer_counter;
129 u16 count; /* number of elements in queue */
130 u8 num; /* queue number */
131 u8 elem_size; /* size of one element */
132 enum mlxsw_pci_queue_type type;
133 struct tasklet_struct tasklet; /* queue processing tasklet */
134 struct mlxsw_pci *pci;
148 struct mlxsw_pci_queue_type_group {
149 struct mlxsw_pci_queue *q;
150 u8 count; /* number of queues in group */
154 struct pci_dev *pdev;
156 struct mlxsw_pci_queue_type_group queues[MLXSW_PCI_QUEUE_TYPE_COUNT];
158 struct msix_entry msix_entry;
159 struct mlxsw_core *core;
161 struct mlxsw_pci_mem_item *items;
165 struct mlxsw_pci_mem_item out_mbox;
166 struct mlxsw_pci_mem_item in_mbox;
167 struct mutex lock; /* Lock access to command registers */
169 wait_queue_head_t wait;
176 struct mlxsw_bus_info bus_info;
177 struct dentry *dbg_dir;
180 static void mlxsw_pci_queue_tasklet_schedule(struct mlxsw_pci_queue *q)
182 tasklet_schedule(&q->tasklet);
185 static char *__mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q,
186 size_t elem_size, int elem_index)
188 return q->mem_item.buf + (elem_size * elem_index);
191 static struct mlxsw_pci_queue_elem_info *
192 mlxsw_pci_queue_elem_info_get(struct mlxsw_pci_queue *q, int elem_index)
194 return &q->elem_info[elem_index];
197 static struct mlxsw_pci_queue_elem_info *
198 mlxsw_pci_queue_elem_info_producer_get(struct mlxsw_pci_queue *q)
200 int index = q->producer_counter & (q->count - 1);
202 if ((u16) (q->producer_counter - q->consumer_counter) == q->count)
204 return mlxsw_pci_queue_elem_info_get(q, index);
207 static struct mlxsw_pci_queue_elem_info *
208 mlxsw_pci_queue_elem_info_consumer_get(struct mlxsw_pci_queue *q)
210 int index = q->consumer_counter & (q->count - 1);
212 return mlxsw_pci_queue_elem_info_get(q, index);
215 static char *mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, int elem_index)
217 return mlxsw_pci_queue_elem_info_get(q, elem_index)->elem;
220 static bool mlxsw_pci_elem_hw_owned(struct mlxsw_pci_queue *q, bool owner_bit)
222 return owner_bit != !!(q->consumer_counter & q->count);
226 mlxsw_pci_queue_sw_elem_get(struct mlxsw_pci_queue *q,
227 u32 (*get_elem_owner_func)(const char *))
229 struct mlxsw_pci_queue_elem_info *elem_info;
233 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
234 elem = elem_info->elem;
235 owner_bit = get_elem_owner_func(elem);
236 if (mlxsw_pci_elem_hw_owned(q, owner_bit))
238 q->consumer_counter++;
239 rmb(); /* make sure we read owned bit before the rest of elem */
243 static struct mlxsw_pci_queue_type_group *
244 mlxsw_pci_queue_type_group_get(struct mlxsw_pci *mlxsw_pci,
245 enum mlxsw_pci_queue_type q_type)
247 return &mlxsw_pci->queues[q_type];
250 static u8 __mlxsw_pci_queue_count(struct mlxsw_pci *mlxsw_pci,
251 enum mlxsw_pci_queue_type q_type)
253 struct mlxsw_pci_queue_type_group *queue_group;
255 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_type);
256 return queue_group->count;
259 static u8 mlxsw_pci_sdq_count(struct mlxsw_pci *mlxsw_pci)
261 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_SDQ);
264 static u8 mlxsw_pci_rdq_count(struct mlxsw_pci *mlxsw_pci)
266 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_RDQ);
269 static u8 mlxsw_pci_cq_count(struct mlxsw_pci *mlxsw_pci)
271 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ);
274 static u8 mlxsw_pci_eq_count(struct mlxsw_pci *mlxsw_pci)
276 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ);
279 static struct mlxsw_pci_queue *
280 __mlxsw_pci_queue_get(struct mlxsw_pci *mlxsw_pci,
281 enum mlxsw_pci_queue_type q_type, u8 q_num)
283 return &mlxsw_pci->queues[q_type].q[q_num];
286 static struct mlxsw_pci_queue *mlxsw_pci_sdq_get(struct mlxsw_pci *mlxsw_pci,
289 return __mlxsw_pci_queue_get(mlxsw_pci,
290 MLXSW_PCI_QUEUE_TYPE_SDQ, q_num);
293 static struct mlxsw_pci_queue *mlxsw_pci_rdq_get(struct mlxsw_pci *mlxsw_pci,
296 return __mlxsw_pci_queue_get(mlxsw_pci,
297 MLXSW_PCI_QUEUE_TYPE_RDQ, q_num);
300 static struct mlxsw_pci_queue *mlxsw_pci_cq_get(struct mlxsw_pci *mlxsw_pci,
303 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ, q_num);
306 static struct mlxsw_pci_queue *mlxsw_pci_eq_get(struct mlxsw_pci *mlxsw_pci,
309 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ, q_num);
312 static void __mlxsw_pci_queue_doorbell_set(struct mlxsw_pci *mlxsw_pci,
313 struct mlxsw_pci_queue *q,
316 mlxsw_pci_write32(mlxsw_pci,
317 DOORBELL(mlxsw_pci->doorbell_offset,
318 mlxsw_pci_doorbell_type_offset[q->type],
322 static void __mlxsw_pci_queue_doorbell_arm_set(struct mlxsw_pci *mlxsw_pci,
323 struct mlxsw_pci_queue *q,
326 mlxsw_pci_write32(mlxsw_pci,
327 DOORBELL(mlxsw_pci->doorbell_offset,
328 mlxsw_pci_doorbell_arm_type_offset[q->type],
332 static void mlxsw_pci_queue_doorbell_producer_ring(struct mlxsw_pci *mlxsw_pci,
333 struct mlxsw_pci_queue *q)
335 wmb(); /* ensure all writes are done before we ring a bell */
336 __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, q->producer_counter);
339 static void mlxsw_pci_queue_doorbell_consumer_ring(struct mlxsw_pci *mlxsw_pci,
340 struct mlxsw_pci_queue *q)
342 wmb(); /* ensure all writes are done before we ring a bell */
343 __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q,
344 q->consumer_counter + q->count);
348 mlxsw_pci_queue_doorbell_arm_consumer_ring(struct mlxsw_pci *mlxsw_pci,
349 struct mlxsw_pci_queue *q)
351 wmb(); /* ensure all writes are done before we ring a bell */
352 __mlxsw_pci_queue_doorbell_arm_set(mlxsw_pci, q, q->consumer_counter);
355 static dma_addr_t __mlxsw_pci_queue_page_get(struct mlxsw_pci_queue *q,
358 return q->mem_item.mapaddr + MLXSW_PCI_PAGE_SIZE * page_index;
361 static int mlxsw_pci_sdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
362 struct mlxsw_pci_queue *q)
367 q->producer_counter = 0;
368 q->consumer_counter = 0;
370 /* Set CQ of same number of this SDQ. */
371 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, q->num);
372 mlxsw_cmd_mbox_sw2hw_dq_sdq_tclass_set(mbox, 3);
373 mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
374 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
375 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
377 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
380 err = mlxsw_cmd_sw2hw_sdq(mlxsw_pci->core, mbox, q->num);
383 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
387 static void mlxsw_pci_sdq_fini(struct mlxsw_pci *mlxsw_pci,
388 struct mlxsw_pci_queue *q)
390 mlxsw_cmd_hw2sw_sdq(mlxsw_pci->core, q->num);
393 static int mlxsw_pci_sdq_dbg_read(struct seq_file *file, void *data)
395 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
396 struct mlxsw_pci_queue *q;
398 static const char hdr[] =
399 "NUM PROD_COUNT CONS_COUNT COUNT\n";
401 seq_printf(file, hdr);
402 for (i = 0; i < mlxsw_pci_sdq_count(mlxsw_pci); i++) {
403 q = mlxsw_pci_sdq_get(mlxsw_pci, i);
404 spin_lock_bh(&q->lock);
405 seq_printf(file, "%3d %10d %10d %5d\n",
406 i, q->producer_counter, q->consumer_counter,
408 spin_unlock_bh(&q->lock);
413 static int mlxsw_pci_wqe_frag_map(struct mlxsw_pci *mlxsw_pci, char *wqe,
414 int index, char *frag_data, size_t frag_len,
417 struct pci_dev *pdev = mlxsw_pci->pdev;
420 mapaddr = pci_map_single(pdev, frag_data, frag_len, direction);
421 if (unlikely(pci_dma_mapping_error(pdev, mapaddr))) {
422 dev_err_ratelimited(&pdev->dev, "failed to dma map tx frag\n");
425 mlxsw_pci_wqe_address_set(wqe, index, mapaddr);
426 mlxsw_pci_wqe_byte_count_set(wqe, index, frag_len);
430 static void mlxsw_pci_wqe_frag_unmap(struct mlxsw_pci *mlxsw_pci, char *wqe,
431 int index, int direction)
433 struct pci_dev *pdev = mlxsw_pci->pdev;
434 size_t frag_len = mlxsw_pci_wqe_byte_count_get(wqe, index);
435 dma_addr_t mapaddr = mlxsw_pci_wqe_address_get(wqe, index);
439 pci_unmap_single(pdev, mapaddr, frag_len, direction);
442 static int mlxsw_pci_rdq_skb_alloc(struct mlxsw_pci *mlxsw_pci,
443 struct mlxsw_pci_queue_elem_info *elem_info)
445 size_t buf_len = MLXSW_PORT_MAX_MTU;
446 char *wqe = elem_info->elem;
450 elem_info->u.rdq.skb = NULL;
451 skb = netdev_alloc_skb_ip_align(NULL, buf_len);
455 /* Assume that wqe was previously zeroed. */
457 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
458 buf_len, DMA_FROM_DEVICE);
462 elem_info->u.rdq.skb = skb;
466 dev_kfree_skb_any(skb);
470 static void mlxsw_pci_rdq_skb_free(struct mlxsw_pci *mlxsw_pci,
471 struct mlxsw_pci_queue_elem_info *elem_info)
476 skb = elem_info->u.rdq.skb;
477 wqe = elem_info->elem;
479 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
480 dev_kfree_skb_any(skb);
483 static int mlxsw_pci_rdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
484 struct mlxsw_pci_queue *q)
486 struct mlxsw_pci_queue_elem_info *elem_info;
487 u8 sdq_count = mlxsw_pci_sdq_count(mlxsw_pci);
491 q->producer_counter = 0;
492 q->consumer_counter = 0;
494 /* Set CQ of same number of this RDQ with base
495 * above SDQ count as the lower ones are assigned to SDQs.
497 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, sdq_count + q->num);
498 mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
499 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
500 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
502 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
505 err = mlxsw_cmd_sw2hw_rdq(mlxsw_pci->core, mbox, q->num);
509 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
511 for (i = 0; i < q->count; i++) {
512 elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
514 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
517 /* Everything is set up, ring doorbell to pass elem to HW */
518 q->producer_counter++;
519 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
525 for (i--; i >= 0; i--) {
526 elem_info = mlxsw_pci_queue_elem_info_get(q, i);
527 mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
529 mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
534 static void mlxsw_pci_rdq_fini(struct mlxsw_pci *mlxsw_pci,
535 struct mlxsw_pci_queue *q)
537 struct mlxsw_pci_queue_elem_info *elem_info;
540 mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
541 for (i = 0; i < q->count; i++) {
542 elem_info = mlxsw_pci_queue_elem_info_get(q, i);
543 mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
547 static int mlxsw_pci_rdq_dbg_read(struct seq_file *file, void *data)
549 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
550 struct mlxsw_pci_queue *q;
552 static const char hdr[] =
553 "NUM PROD_COUNT CONS_COUNT COUNT\n";
555 seq_printf(file, hdr);
556 for (i = 0; i < mlxsw_pci_rdq_count(mlxsw_pci); i++) {
557 q = mlxsw_pci_rdq_get(mlxsw_pci, i);
558 spin_lock_bh(&q->lock);
559 seq_printf(file, "%3d %10d %10d %5d\n",
560 i, q->producer_counter, q->consumer_counter,
562 spin_unlock_bh(&q->lock);
567 static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
568 struct mlxsw_pci_queue *q)
573 q->consumer_counter = 0;
575 for (i = 0; i < q->count; i++) {
576 char *elem = mlxsw_pci_queue_elem_get(q, i);
578 mlxsw_pci_cqe_owner_set(elem, 1);
581 mlxsw_cmd_mbox_sw2hw_cq_cv_set(mbox, 0); /* CQE ver 0 */
582 mlxsw_cmd_mbox_sw2hw_cq_c_eqn_set(mbox, MLXSW_PCI_EQ_COMP_NUM);
583 mlxsw_cmd_mbox_sw2hw_cq_st_set(mbox, 0);
584 mlxsw_cmd_mbox_sw2hw_cq_log_cq_size_set(mbox, ilog2(q->count));
585 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
586 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
588 mlxsw_cmd_mbox_sw2hw_cq_pa_set(mbox, i, mapaddr);
590 err = mlxsw_cmd_sw2hw_cq(mlxsw_pci->core, mbox, q->num);
593 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
594 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
598 static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci,
599 struct mlxsw_pci_queue *q)
601 mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num);
604 static int mlxsw_pci_cq_dbg_read(struct seq_file *file, void *data)
606 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
608 struct mlxsw_pci_queue *q;
610 static const char hdr[] =
611 "NUM CONS_INDEX SDQ_COUNT RDQ_COUNT COUNT\n";
613 seq_printf(file, hdr);
614 for (i = 0; i < mlxsw_pci_cq_count(mlxsw_pci); i++) {
615 q = mlxsw_pci_cq_get(mlxsw_pci, i);
616 spin_lock_bh(&q->lock);
617 seq_printf(file, "%3d %10d %10d %10d %5d\n",
618 i, q->consumer_counter, q->u.cq.comp_sdq_count,
619 q->u.cq.comp_rdq_count, q->count);
620 spin_unlock_bh(&q->lock);
625 static void mlxsw_pci_cqe_sdq_handle(struct mlxsw_pci *mlxsw_pci,
626 struct mlxsw_pci_queue *q,
627 u16 consumer_counter_limit,
630 struct pci_dev *pdev = mlxsw_pci->pdev;
631 struct mlxsw_pci_queue_elem_info *elem_info;
637 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
638 skb = elem_info->u.sdq.skb;
639 wqe = elem_info->elem;
640 for (i = 0; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
641 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
642 dev_kfree_skb_any(skb);
643 elem_info->u.sdq.skb = NULL;
645 if (q->consumer_counter++ != consumer_counter_limit)
646 dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in SDQ\n");
647 spin_unlock(&q->lock);
650 static void mlxsw_pci_cqe_rdq_handle(struct mlxsw_pci *mlxsw_pci,
651 struct mlxsw_pci_queue *q,
652 u16 consumer_counter_limit,
655 struct pci_dev *pdev = mlxsw_pci->pdev;
656 struct mlxsw_pci_queue_elem_info *elem_info;
659 struct mlxsw_rx_info rx_info;
663 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
664 skb = elem_info->u.sdq.skb;
667 wqe = elem_info->elem;
668 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
670 if (q->consumer_counter++ != consumer_counter_limit)
671 dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in RDQ\n");
673 if (mlxsw_pci_cqe_lag_get(cqe)) {
674 rx_info.is_lag = true;
675 rx_info.u.lag_id = mlxsw_pci_cqe_lag_id_get(cqe);
676 rx_info.lag_port_index = mlxsw_pci_cqe_lag_port_index_get(cqe);
678 rx_info.is_lag = false;
679 rx_info.u.sys_port = mlxsw_pci_cqe_system_port_get(cqe);
682 rx_info.trap_id = mlxsw_pci_cqe_trap_id_get(cqe);
684 byte_count = mlxsw_pci_cqe_byte_count_get(cqe);
685 if (mlxsw_pci_cqe_crc_get(cqe))
686 byte_count -= ETH_FCS_LEN;
687 skb_put(skb, byte_count);
688 mlxsw_core_skb_receive(mlxsw_pci->core, skb, &rx_info);
690 memset(wqe, 0, q->elem_size);
691 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
693 dev_dbg_ratelimited(&pdev->dev, "Failed to alloc skb for RDQ\n");
694 /* Everything is set up, ring doorbell to pass elem to HW */
695 q->producer_counter++;
696 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
700 static char *mlxsw_pci_cq_sw_cqe_get(struct mlxsw_pci_queue *q)
702 return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_cqe_owner_get);
705 static void mlxsw_pci_cq_tasklet(unsigned long data)
707 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
708 struct mlxsw_pci *mlxsw_pci = q->pci;
711 int credits = q->count >> 1;
713 while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) {
714 u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe);
715 u8 sendq = mlxsw_pci_cqe_sr_get(cqe);
716 u8 dqn = mlxsw_pci_cqe_dqn_get(cqe);
719 struct mlxsw_pci_queue *sdq;
721 sdq = mlxsw_pci_sdq_get(mlxsw_pci, dqn);
722 mlxsw_pci_cqe_sdq_handle(mlxsw_pci, sdq,
724 q->u.cq.comp_sdq_count++;
726 struct mlxsw_pci_queue *rdq;
728 rdq = mlxsw_pci_rdq_get(mlxsw_pci, dqn);
729 mlxsw_pci_cqe_rdq_handle(mlxsw_pci, rdq,
731 q->u.cq.comp_rdq_count++;
733 if (++items == credits)
737 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
738 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
742 static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
743 struct mlxsw_pci_queue *q)
748 q->consumer_counter = 0;
750 for (i = 0; i < q->count; i++) {
751 char *elem = mlxsw_pci_queue_elem_get(q, i);
753 mlxsw_pci_eqe_owner_set(elem, 1);
756 mlxsw_cmd_mbox_sw2hw_eq_int_msix_set(mbox, 1); /* MSI-X used */
757 mlxsw_cmd_mbox_sw2hw_eq_st_set(mbox, 1); /* armed */
758 mlxsw_cmd_mbox_sw2hw_eq_log_eq_size_set(mbox, ilog2(q->count));
759 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
760 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
762 mlxsw_cmd_mbox_sw2hw_eq_pa_set(mbox, i, mapaddr);
764 err = mlxsw_cmd_sw2hw_eq(mlxsw_pci->core, mbox, q->num);
767 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
768 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
772 static void mlxsw_pci_eq_fini(struct mlxsw_pci *mlxsw_pci,
773 struct mlxsw_pci_queue *q)
775 mlxsw_cmd_hw2sw_eq(mlxsw_pci->core, q->num);
778 static int mlxsw_pci_eq_dbg_read(struct seq_file *file, void *data)
780 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
781 struct mlxsw_pci_queue *q;
783 static const char hdr[] =
784 "NUM CONS_COUNT EV_CMD EV_COMP EV_OTHER COUNT\n";
786 seq_printf(file, hdr);
787 for (i = 0; i < mlxsw_pci_eq_count(mlxsw_pci); i++) {
788 q = mlxsw_pci_eq_get(mlxsw_pci, i);
789 spin_lock_bh(&q->lock);
790 seq_printf(file, "%3d %10d %10d %10d %10d %5d\n",
791 i, q->consumer_counter, q->u.eq.ev_cmd_count,
792 q->u.eq.ev_comp_count, q->u.eq.ev_other_count,
794 spin_unlock_bh(&q->lock);
799 static void mlxsw_pci_eq_cmd_event(struct mlxsw_pci *mlxsw_pci, char *eqe)
801 mlxsw_pci->cmd.comp.status = mlxsw_pci_eqe_cmd_status_get(eqe);
802 mlxsw_pci->cmd.comp.out_param =
803 ((u64) mlxsw_pci_eqe_cmd_out_param_h_get(eqe)) << 32 |
804 mlxsw_pci_eqe_cmd_out_param_l_get(eqe);
805 mlxsw_pci->cmd.wait_done = true;
806 wake_up(&mlxsw_pci->cmd.wait);
809 static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q)
811 return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_eqe_owner_get);
814 static void mlxsw_pci_eq_tasklet(unsigned long data)
816 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
817 struct mlxsw_pci *mlxsw_pci = q->pci;
818 u8 cq_count = mlxsw_pci_cq_count(mlxsw_pci);
819 unsigned long active_cqns[BITS_TO_LONGS(MLXSW_PCI_CQS_MAX)];
822 bool cq_handle = false;
824 int credits = q->count >> 1;
826 memset(&active_cqns, 0, sizeof(active_cqns));
828 while ((eqe = mlxsw_pci_eq_sw_eqe_get(q))) {
829 u8 event_type = mlxsw_pci_eqe_event_type_get(eqe);
831 switch (event_type) {
832 case MLXSW_PCI_EQE_EVENT_TYPE_CMD:
833 mlxsw_pci_eq_cmd_event(mlxsw_pci, eqe);
834 q->u.eq.ev_cmd_count++;
836 case MLXSW_PCI_EQE_EVENT_TYPE_COMP:
837 cqn = mlxsw_pci_eqe_cqn_get(eqe);
838 set_bit(cqn, active_cqns);
840 q->u.eq.ev_comp_count++;
843 q->u.eq.ev_other_count++;
845 if (++items == credits)
849 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
850 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
855 for_each_set_bit(cqn, active_cqns, cq_count) {
856 q = mlxsw_pci_cq_get(mlxsw_pci, cqn);
857 mlxsw_pci_queue_tasklet_schedule(q);
861 struct mlxsw_pci_queue_ops {
863 enum mlxsw_pci_queue_type type;
864 int (*init)(struct mlxsw_pci *mlxsw_pci, char *mbox,
865 struct mlxsw_pci_queue *q);
866 void (*fini)(struct mlxsw_pci *mlxsw_pci,
867 struct mlxsw_pci_queue *q);
868 void (*tasklet)(unsigned long data);
869 int (*dbg_read)(struct seq_file *s, void *data);
874 static const struct mlxsw_pci_queue_ops mlxsw_pci_sdq_ops = {
875 .type = MLXSW_PCI_QUEUE_TYPE_SDQ,
876 .init = mlxsw_pci_sdq_init,
877 .fini = mlxsw_pci_sdq_fini,
878 .dbg_read = mlxsw_pci_sdq_dbg_read,
879 .elem_count = MLXSW_PCI_WQE_COUNT,
880 .elem_size = MLXSW_PCI_WQE_SIZE,
883 static const struct mlxsw_pci_queue_ops mlxsw_pci_rdq_ops = {
884 .type = MLXSW_PCI_QUEUE_TYPE_RDQ,
885 .init = mlxsw_pci_rdq_init,
886 .fini = mlxsw_pci_rdq_fini,
887 .dbg_read = mlxsw_pci_rdq_dbg_read,
888 .elem_count = MLXSW_PCI_WQE_COUNT,
889 .elem_size = MLXSW_PCI_WQE_SIZE
892 static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = {
893 .type = MLXSW_PCI_QUEUE_TYPE_CQ,
894 .init = mlxsw_pci_cq_init,
895 .fini = mlxsw_pci_cq_fini,
896 .tasklet = mlxsw_pci_cq_tasklet,
897 .dbg_read = mlxsw_pci_cq_dbg_read,
898 .elem_count = MLXSW_PCI_CQE_COUNT,
899 .elem_size = MLXSW_PCI_CQE_SIZE
902 static const struct mlxsw_pci_queue_ops mlxsw_pci_eq_ops = {
903 .type = MLXSW_PCI_QUEUE_TYPE_EQ,
904 .init = mlxsw_pci_eq_init,
905 .fini = mlxsw_pci_eq_fini,
906 .tasklet = mlxsw_pci_eq_tasklet,
907 .dbg_read = mlxsw_pci_eq_dbg_read,
908 .elem_count = MLXSW_PCI_EQE_COUNT,
909 .elem_size = MLXSW_PCI_EQE_SIZE
912 static int mlxsw_pci_queue_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
913 const struct mlxsw_pci_queue_ops *q_ops,
914 struct mlxsw_pci_queue *q, u8 q_num)
916 struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
920 spin_lock_init(&q->lock);
922 q->count = q_ops->elem_count;
923 q->elem_size = q_ops->elem_size;
924 q->type = q_ops->type;
928 tasklet_init(&q->tasklet, q_ops->tasklet, (unsigned long) q);
930 mem_item->size = MLXSW_PCI_AQ_SIZE;
931 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
936 memset(mem_item->buf, 0, mem_item->size);
938 q->elem_info = kcalloc(q->count, sizeof(*q->elem_info), GFP_KERNEL);
941 goto err_elem_info_alloc;
944 /* Initialize dma mapped elements info elem_info for
945 * future easy access.
947 for (i = 0; i < q->count; i++) {
948 struct mlxsw_pci_queue_elem_info *elem_info;
950 elem_info = mlxsw_pci_queue_elem_info_get(q, i);
952 __mlxsw_pci_queue_elem_get(q, q_ops->elem_size, i);
955 mlxsw_cmd_mbox_zero(mbox);
956 err = q_ops->init(mlxsw_pci, mbox, q);
964 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
965 mem_item->buf, mem_item->mapaddr);
969 static void mlxsw_pci_queue_fini(struct mlxsw_pci *mlxsw_pci,
970 const struct mlxsw_pci_queue_ops *q_ops,
971 struct mlxsw_pci_queue *q)
973 struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
975 q_ops->fini(mlxsw_pci, q);
977 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
978 mem_item->buf, mem_item->mapaddr);
981 static int mlxsw_pci_queue_group_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
982 const struct mlxsw_pci_queue_ops *q_ops,
985 struct pci_dev *pdev = mlxsw_pci->pdev;
986 struct mlxsw_pci_queue_type_group *queue_group;
991 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
992 queue_group->q = kcalloc(num_qs, sizeof(*queue_group->q), GFP_KERNEL);
996 for (i = 0; i < num_qs; i++) {
997 err = mlxsw_pci_queue_init(mlxsw_pci, mbox, q_ops,
998 &queue_group->q[i], i);
1000 goto err_queue_init;
1002 queue_group->count = num_qs;
1004 sprintf(tmp, "%s_stats", mlxsw_pci_queue_type_str(q_ops->type));
1005 debugfs_create_devm_seqfile(&pdev->dev, tmp, mlxsw_pci->dbg_dir,
1011 for (i--; i >= 0; i--)
1012 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1013 kfree(queue_group->q);
1017 static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,
1018 const struct mlxsw_pci_queue_ops *q_ops)
1020 struct mlxsw_pci_queue_type_group *queue_group;
1023 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
1024 for (i = 0; i < queue_group->count; i++)
1025 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1026 kfree(queue_group->q);
1029 static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
1031 struct pci_dev *pdev = mlxsw_pci->pdev;
1042 mlxsw_cmd_mbox_zero(mbox);
1043 err = mlxsw_cmd_query_aq_cap(mlxsw_pci->core, mbox);
1047 num_sdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_sdqs_get(mbox);
1048 sdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_sdq_sz_get(mbox);
1049 num_rdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_rdqs_get(mbox);
1050 rdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_rdq_sz_get(mbox);
1051 num_cqs = mlxsw_cmd_mbox_query_aq_cap_max_num_cqs_get(mbox);
1052 cq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cq_sz_get(mbox);
1053 num_eqs = mlxsw_cmd_mbox_query_aq_cap_max_num_eqs_get(mbox);
1054 eq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_eq_sz_get(mbox);
1056 if (num_sdqs + num_rdqs > num_cqs ||
1057 num_cqs > MLXSW_PCI_CQS_MAX || num_eqs != MLXSW_PCI_EQS_COUNT) {
1058 dev_err(&pdev->dev, "Unsupported number of queues\n");
1062 if ((1 << sdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1063 (1 << rdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1064 (1 << cq_log2sz != MLXSW_PCI_CQE_COUNT) ||
1065 (1 << eq_log2sz != MLXSW_PCI_EQE_COUNT)) {
1066 dev_err(&pdev->dev, "Unsupported number of async queue descriptors\n");
1070 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_eq_ops,
1073 dev_err(&pdev->dev, "Failed to initialize event queues\n");
1077 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_cq_ops,
1080 dev_err(&pdev->dev, "Failed to initialize completion queues\n");
1084 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_sdq_ops,
1087 dev_err(&pdev->dev, "Failed to initialize send descriptor queues\n");
1091 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_rdq_ops,
1094 dev_err(&pdev->dev, "Failed to initialize receive descriptor queues\n");
1098 /* We have to poll in command interface until queues are initialized */
1099 mlxsw_pci->cmd.nopoll = true;
1103 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1105 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1107 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1111 static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci)
1113 mlxsw_pci->cmd.nopoll = false;
1114 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops);
1115 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1116 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1117 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1121 mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci,
1122 char *mbox, int index,
1123 const struct mlxsw_swid_config *swid)
1127 if (swid->used_type) {
1128 mlxsw_cmd_mbox_config_profile_swid_config_type_set(
1129 mbox, index, swid->type);
1132 if (swid->used_properties) {
1133 mlxsw_cmd_mbox_config_profile_swid_config_properties_set(
1134 mbox, index, swid->properties);
1137 mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask);
1140 static int mlxsw_pci_resources_query(struct mlxsw_pci *mlxsw_pci, char *mbox,
1141 struct mlxsw_res *res,
1149 /* Not all the versions support resources query */
1153 mlxsw_cmd_mbox_zero(mbox);
1155 for (index = 0; index < MLXSW_CMD_QUERY_RESOURCES_MAX_QUERIES;
1157 err = mlxsw_cmd_query_resources(mlxsw_pci->core, mbox, index);
1161 for (i = 0; i < MLXSW_CMD_QUERY_RESOURCES_PER_QUERY; i++) {
1162 id = mlxsw_cmd_mbox_query_resource_id_get(mbox, i);
1163 data = mlxsw_cmd_mbox_query_resource_data_get(mbox, i);
1165 if (id == MLXSW_CMD_QUERY_RESOURCES_TABLE_END_ID)
1168 mlxsw_res_parse(res, id, data);
1172 /* If after MLXSW_RESOURCES_QUERY_MAX_QUERIES we still didn't get
1173 * MLXSW_RESOURCES_TABLE_END_ID, something went bad in the FW.
1179 mlxsw_pci_profile_get_kvd_sizes(const struct mlxsw_config_profile *profile,
1180 struct mlxsw_res *res)
1182 u32 single_size, double_size, linear_size;
1184 if (!MLXSW_RES_VALID(res, KVD_SINGLE_MIN_SIZE) ||
1185 !MLXSW_RES_VALID(res, KVD_DOUBLE_MIN_SIZE) ||
1186 !profile->used_kvd_split_data)
1189 linear_size = profile->kvd_linear_size;
1191 /* The hash part is what left of the kvd without the
1192 * linear part. It is split to the single size and
1193 * double size by the parts ratio from the profile.
1194 * Both sizes must be a multiplications of the
1195 * granularity from the profile.
1197 double_size = MLXSW_RES_GET(res, KVD_SIZE) - linear_size;
1198 double_size *= profile->kvd_hash_double_parts;
1199 double_size /= profile->kvd_hash_double_parts +
1200 profile->kvd_hash_single_parts;
1201 double_size /= profile->kvd_hash_granularity;
1202 double_size *= profile->kvd_hash_granularity;
1203 single_size = MLXSW_RES_GET(res, KVD_SIZE) - double_size -
1206 /* Check results are legal. */
1207 if (single_size < MLXSW_RES_GET(res, KVD_SINGLE_MIN_SIZE) ||
1208 double_size < MLXSW_RES_GET(res, KVD_DOUBLE_MIN_SIZE) ||
1209 MLXSW_RES_GET(res, KVD_SIZE) < linear_size)
1212 MLXSW_RES_SET(res, KVD_SINGLE_SIZE, single_size);
1213 MLXSW_RES_SET(res, KVD_DOUBLE_SIZE, double_size);
1214 MLXSW_RES_SET(res, KVD_LINEAR_SIZE, linear_size);
1219 static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
1220 const struct mlxsw_config_profile *profile,
1221 struct mlxsw_res *res)
1226 mlxsw_cmd_mbox_zero(mbox);
1228 if (profile->used_max_vepa_channels) {
1229 mlxsw_cmd_mbox_config_profile_set_max_vepa_channels_set(
1231 mlxsw_cmd_mbox_config_profile_max_vepa_channels_set(
1232 mbox, profile->max_vepa_channels);
1234 if (profile->used_max_mid) {
1235 mlxsw_cmd_mbox_config_profile_set_max_mid_set(
1237 mlxsw_cmd_mbox_config_profile_max_mid_set(
1238 mbox, profile->max_mid);
1240 if (profile->used_max_pgt) {
1241 mlxsw_cmd_mbox_config_profile_set_max_pgt_set(
1243 mlxsw_cmd_mbox_config_profile_max_pgt_set(
1244 mbox, profile->max_pgt);
1246 if (profile->used_max_system_port) {
1247 mlxsw_cmd_mbox_config_profile_set_max_system_port_set(
1249 mlxsw_cmd_mbox_config_profile_max_system_port_set(
1250 mbox, profile->max_system_port);
1252 if (profile->used_max_vlan_groups) {
1253 mlxsw_cmd_mbox_config_profile_set_max_vlan_groups_set(
1255 mlxsw_cmd_mbox_config_profile_max_vlan_groups_set(
1256 mbox, profile->max_vlan_groups);
1258 if (profile->used_max_regions) {
1259 mlxsw_cmd_mbox_config_profile_set_max_regions_set(
1261 mlxsw_cmd_mbox_config_profile_max_regions_set(
1262 mbox, profile->max_regions);
1264 if (profile->used_flood_tables) {
1265 mlxsw_cmd_mbox_config_profile_set_flood_tables_set(
1267 mlxsw_cmd_mbox_config_profile_max_flood_tables_set(
1268 mbox, profile->max_flood_tables);
1269 mlxsw_cmd_mbox_config_profile_max_vid_flood_tables_set(
1270 mbox, profile->max_vid_flood_tables);
1271 mlxsw_cmd_mbox_config_profile_max_fid_offset_flood_tables_set(
1272 mbox, profile->max_fid_offset_flood_tables);
1273 mlxsw_cmd_mbox_config_profile_fid_offset_flood_table_size_set(
1274 mbox, profile->fid_offset_flood_table_size);
1275 mlxsw_cmd_mbox_config_profile_max_fid_flood_tables_set(
1276 mbox, profile->max_fid_flood_tables);
1277 mlxsw_cmd_mbox_config_profile_fid_flood_table_size_set(
1278 mbox, profile->fid_flood_table_size);
1280 if (profile->used_flood_mode) {
1281 mlxsw_cmd_mbox_config_profile_set_flood_mode_set(
1283 mlxsw_cmd_mbox_config_profile_flood_mode_set(
1284 mbox, profile->flood_mode);
1286 if (profile->used_max_ib_mc) {
1287 mlxsw_cmd_mbox_config_profile_set_max_ib_mc_set(
1289 mlxsw_cmd_mbox_config_profile_max_ib_mc_set(
1290 mbox, profile->max_ib_mc);
1292 if (profile->used_max_pkey) {
1293 mlxsw_cmd_mbox_config_profile_set_max_pkey_set(
1295 mlxsw_cmd_mbox_config_profile_max_pkey_set(
1296 mbox, profile->max_pkey);
1298 if (profile->used_ar_sec) {
1299 mlxsw_cmd_mbox_config_profile_set_ar_sec_set(
1301 mlxsw_cmd_mbox_config_profile_ar_sec_set(
1302 mbox, profile->ar_sec);
1304 if (profile->used_adaptive_routing_group_cap) {
1305 mlxsw_cmd_mbox_config_profile_set_adaptive_routing_group_cap_set(
1307 mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set(
1308 mbox, profile->adaptive_routing_group_cap);
1310 if (MLXSW_RES_VALID(res, KVD_SIZE)) {
1311 err = mlxsw_pci_profile_get_kvd_sizes(profile, res);
1315 mlxsw_cmd_mbox_config_profile_set_kvd_linear_size_set(mbox, 1);
1316 mlxsw_cmd_mbox_config_profile_kvd_linear_size_set(mbox,
1317 MLXSW_RES_GET(res, KVD_LINEAR_SIZE));
1318 mlxsw_cmd_mbox_config_profile_set_kvd_hash_single_size_set(mbox,
1320 mlxsw_cmd_mbox_config_profile_kvd_hash_single_size_set(mbox,
1321 MLXSW_RES_GET(res, KVD_SINGLE_SIZE));
1322 mlxsw_cmd_mbox_config_profile_set_kvd_hash_double_size_set(
1324 mlxsw_cmd_mbox_config_profile_kvd_hash_double_size_set(mbox,
1325 MLXSW_RES_GET(res, KVD_DOUBLE_SIZE));
1328 for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++)
1329 mlxsw_pci_config_profile_swid_config(mlxsw_pci, mbox, i,
1330 &profile->swid_config[i]);
1332 return mlxsw_cmd_config_profile_set(mlxsw_pci->core, mbox);
1335 static int mlxsw_pci_boardinfo(struct mlxsw_pci *mlxsw_pci, char *mbox)
1337 struct mlxsw_bus_info *bus_info = &mlxsw_pci->bus_info;
1340 mlxsw_cmd_mbox_zero(mbox);
1341 err = mlxsw_cmd_boardinfo(mlxsw_pci->core, mbox);
1344 mlxsw_cmd_mbox_boardinfo_vsd_memcpy_from(mbox, bus_info->vsd);
1345 mlxsw_cmd_mbox_boardinfo_psid_memcpy_from(mbox, bus_info->psid);
1349 static int mlxsw_pci_fw_area_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1352 struct mlxsw_pci_mem_item *mem_item;
1357 mlxsw_pci->fw_area.items = kcalloc(num_pages, sizeof(*mem_item),
1359 if (!mlxsw_pci->fw_area.items)
1361 mlxsw_pci->fw_area.count = num_pages;
1363 mlxsw_cmd_mbox_zero(mbox);
1364 for (i = 0; i < num_pages; i++) {
1365 mem_item = &mlxsw_pci->fw_area.items[i];
1367 mem_item->size = MLXSW_PCI_PAGE_SIZE;
1368 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
1370 &mem_item->mapaddr);
1371 if (!mem_item->buf) {
1375 mlxsw_cmd_mbox_map_fa_pa_set(mbox, nent, mem_item->mapaddr);
1376 mlxsw_cmd_mbox_map_fa_log2size_set(mbox, nent, 0); /* 1 page */
1377 if (++nent == MLXSW_CMD_MAP_FA_VPM_ENTRIES_MAX) {
1378 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1380 goto err_cmd_map_fa;
1382 mlxsw_cmd_mbox_zero(mbox);
1387 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1389 goto err_cmd_map_fa;
1396 for (i--; i >= 0; i--) {
1397 mem_item = &mlxsw_pci->fw_area.items[i];
1399 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1400 mem_item->buf, mem_item->mapaddr);
1402 kfree(mlxsw_pci->fw_area.items);
1406 static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci)
1408 struct mlxsw_pci_mem_item *mem_item;
1411 mlxsw_cmd_unmap_fa(mlxsw_pci->core);
1413 for (i = 0; i < mlxsw_pci->fw_area.count; i++) {
1414 mem_item = &mlxsw_pci->fw_area.items[i];
1416 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1417 mem_item->buf, mem_item->mapaddr);
1419 kfree(mlxsw_pci->fw_area.items);
1422 static irqreturn_t mlxsw_pci_eq_irq_handler(int irq, void *dev_id)
1424 struct mlxsw_pci *mlxsw_pci = dev_id;
1425 struct mlxsw_pci_queue *q;
1428 for (i = 0; i < MLXSW_PCI_EQS_COUNT; i++) {
1429 q = mlxsw_pci_eq_get(mlxsw_pci, i);
1430 mlxsw_pci_queue_tasklet_schedule(q);
1435 static int mlxsw_pci_mbox_alloc(struct mlxsw_pci *mlxsw_pci,
1436 struct mlxsw_pci_mem_item *mbox)
1438 struct pci_dev *pdev = mlxsw_pci->pdev;
1441 mbox->size = MLXSW_CMD_MBOX_SIZE;
1442 mbox->buf = pci_alloc_consistent(pdev, MLXSW_CMD_MBOX_SIZE,
1445 dev_err(&pdev->dev, "Failed allocating memory for mailbox\n");
1452 static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci,
1453 struct mlxsw_pci_mem_item *mbox)
1455 struct pci_dev *pdev = mlxsw_pci->pdev;
1457 pci_free_consistent(pdev, MLXSW_CMD_MBOX_SIZE, mbox->buf,
1461 static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
1462 const struct mlxsw_config_profile *profile,
1463 struct mlxsw_res *res)
1465 struct mlxsw_pci *mlxsw_pci = bus_priv;
1466 struct pci_dev *pdev = mlxsw_pci->pdev;
1471 mutex_init(&mlxsw_pci->cmd.lock);
1472 init_waitqueue_head(&mlxsw_pci->cmd.wait);
1474 mlxsw_pci->core = mlxsw_core;
1476 mbox = mlxsw_cmd_mbox_alloc();
1480 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1484 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1486 goto err_out_mbox_alloc;
1488 err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
1492 mlxsw_pci->bus_info.fw_rev.major =
1493 mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
1494 mlxsw_pci->bus_info.fw_rev.minor =
1495 mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
1496 mlxsw_pci->bus_info.fw_rev.subminor =
1497 mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
1499 if (mlxsw_cmd_mbox_query_fw_cmd_interface_rev_get(mbox) != 1) {
1500 dev_err(&pdev->dev, "Unsupported cmd interface revision ID queried from hw\n");
1504 if (mlxsw_cmd_mbox_query_fw_doorbell_page_bar_get(mbox) != 0) {
1505 dev_err(&pdev->dev, "Unsupported doorbell page bar queried from hw\n");
1507 goto err_doorbell_page_bar;
1510 mlxsw_pci->doorbell_offset =
1511 mlxsw_cmd_mbox_query_fw_doorbell_page_offset_get(mbox);
1513 num_pages = mlxsw_cmd_mbox_query_fw_fw_pages_get(mbox);
1514 err = mlxsw_pci_fw_area_init(mlxsw_pci, mbox, num_pages);
1516 goto err_fw_area_init;
1518 err = mlxsw_pci_boardinfo(mlxsw_pci, mbox);
1522 err = mlxsw_pci_resources_query(mlxsw_pci, mbox, res,
1523 profile->resource_query_enable);
1525 goto err_query_resources;
1527 err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile, res);
1529 goto err_config_profile;
1531 err = mlxsw_pci_aqs_init(mlxsw_pci, mbox);
1535 err = request_irq(mlxsw_pci->msix_entry.vector,
1536 mlxsw_pci_eq_irq_handler, 0,
1537 mlxsw_pci->bus_info.device_kind, mlxsw_pci);
1539 dev_err(&pdev->dev, "IRQ request failed\n");
1540 goto err_request_eq_irq;
1546 mlxsw_pci_aqs_fini(mlxsw_pci);
1549 err_query_resources:
1551 mlxsw_pci_fw_area_fini(mlxsw_pci);
1553 err_doorbell_page_bar:
1556 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1558 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1560 mlxsw_cmd_mbox_free(mbox);
1564 static void mlxsw_pci_fini(void *bus_priv)
1566 struct mlxsw_pci *mlxsw_pci = bus_priv;
1568 free_irq(mlxsw_pci->msix_entry.vector, mlxsw_pci);
1569 mlxsw_pci_aqs_fini(mlxsw_pci);
1570 mlxsw_pci_fw_area_fini(mlxsw_pci);
1571 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1572 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1575 static struct mlxsw_pci_queue *
1576 mlxsw_pci_sdq_pick(struct mlxsw_pci *mlxsw_pci,
1577 const struct mlxsw_tx_info *tx_info)
1579 u8 sdqn = tx_info->local_port % mlxsw_pci_sdq_count(mlxsw_pci);
1581 return mlxsw_pci_sdq_get(mlxsw_pci, sdqn);
1584 static bool mlxsw_pci_skb_transmit_busy(void *bus_priv,
1585 const struct mlxsw_tx_info *tx_info)
1587 struct mlxsw_pci *mlxsw_pci = bus_priv;
1588 struct mlxsw_pci_queue *q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1590 return !mlxsw_pci_queue_elem_info_producer_get(q);
1593 static int mlxsw_pci_skb_transmit(void *bus_priv, struct sk_buff *skb,
1594 const struct mlxsw_tx_info *tx_info)
1596 struct mlxsw_pci *mlxsw_pci = bus_priv;
1597 struct mlxsw_pci_queue *q;
1598 struct mlxsw_pci_queue_elem_info *elem_info;
1603 if (skb_shinfo(skb)->nr_frags > MLXSW_PCI_WQE_SG_ENTRIES - 1) {
1604 err = skb_linearize(skb);
1609 q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1610 spin_lock_bh(&q->lock);
1611 elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
1617 elem_info->u.sdq.skb = skb;
1619 wqe = elem_info->elem;
1620 mlxsw_pci_wqe_c_set(wqe, 1); /* always report completion */
1621 mlxsw_pci_wqe_lp_set(wqe, !!tx_info->is_emad);
1622 mlxsw_pci_wqe_type_set(wqe, MLXSW_PCI_WQE_TYPE_ETHERNET);
1624 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
1625 skb_headlen(skb), DMA_TO_DEVICE);
1629 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1630 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1632 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, i + 1,
1633 skb_frag_address(frag),
1634 skb_frag_size(frag),
1640 /* Set unused sq entries byte count to zero. */
1641 for (i++; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
1642 mlxsw_pci_wqe_byte_count_set(wqe, i, 0);
1644 /* Everything is set up, ring producer doorbell to get HW going */
1645 q->producer_counter++;
1646 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
1652 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
1654 spin_unlock_bh(&q->lock);
1658 static int mlxsw_pci_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
1659 u32 in_mod, bool out_mbox_direct,
1660 char *in_mbox, size_t in_mbox_size,
1661 char *out_mbox, size_t out_mbox_size,
1664 struct mlxsw_pci *mlxsw_pci = bus_priv;
1665 dma_addr_t in_mapaddr = mlxsw_pci->cmd.in_mbox.mapaddr;
1666 dma_addr_t out_mapaddr = mlxsw_pci->cmd.out_mbox.mapaddr;
1667 bool evreq = mlxsw_pci->cmd.nopoll;
1668 unsigned long timeout = msecs_to_jiffies(MLXSW_PCI_CIR_TIMEOUT_MSECS);
1669 bool *p_wait_done = &mlxsw_pci->cmd.wait_done;
1672 *p_status = MLXSW_CMD_STATUS_OK;
1674 err = mutex_lock_interruptible(&mlxsw_pci->cmd.lock);
1679 memcpy(mlxsw_pci->cmd.in_mbox.buf, in_mbox, in_mbox_size);
1680 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_HI, upper_32_bits(in_mapaddr));
1681 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_LO, lower_32_bits(in_mapaddr));
1683 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_HI, upper_32_bits(out_mapaddr));
1684 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_LO, lower_32_bits(out_mapaddr));
1686 mlxsw_pci_write32(mlxsw_pci, CIR_IN_MODIFIER, in_mod);
1687 mlxsw_pci_write32(mlxsw_pci, CIR_TOKEN, 0);
1689 *p_wait_done = false;
1691 wmb(); /* all needs to be written before we write control register */
1692 mlxsw_pci_write32(mlxsw_pci, CIR_CTRL,
1693 MLXSW_PCI_CIR_CTRL_GO_BIT |
1694 (evreq ? MLXSW_PCI_CIR_CTRL_EVREQ_BIT : 0) |
1695 (opcode_mod << MLXSW_PCI_CIR_CTRL_OPCODE_MOD_SHIFT) |
1701 end = jiffies + timeout;
1703 u32 ctrl = mlxsw_pci_read32(mlxsw_pci, CIR_CTRL);
1705 if (!(ctrl & MLXSW_PCI_CIR_CTRL_GO_BIT)) {
1706 *p_wait_done = true;
1707 *p_status = ctrl >> MLXSW_PCI_CIR_CTRL_STATUS_SHIFT;
1711 } while (time_before(jiffies, end));
1713 wait_event_timeout(mlxsw_pci->cmd.wait, *p_wait_done, timeout);
1714 *p_status = mlxsw_pci->cmd.comp.status;
1725 if (!err && out_mbox && out_mbox_direct) {
1726 /* Some commands don't use output param as address to mailbox
1727 * but they store output directly into registers. In that case,
1728 * copy registers into mbox buffer.
1733 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1735 memcpy(out_mbox, &tmp, sizeof(tmp));
1736 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1738 memcpy(out_mbox + sizeof(tmp), &tmp, sizeof(tmp));
1740 } else if (!err && out_mbox) {
1741 memcpy(out_mbox, mlxsw_pci->cmd.out_mbox.buf, out_mbox_size);
1744 mutex_unlock(&mlxsw_pci->cmd.lock);
1749 static const struct mlxsw_bus mlxsw_pci_bus = {
1751 .init = mlxsw_pci_init,
1752 .fini = mlxsw_pci_fini,
1753 .skb_transmit_busy = mlxsw_pci_skb_transmit_busy,
1754 .skb_transmit = mlxsw_pci_skb_transmit,
1755 .cmd_exec = mlxsw_pci_cmd_exec,
1756 .features = MLXSW_BUS_F_TXRX,
1759 static int mlxsw_pci_sw_reset(struct mlxsw_pci *mlxsw_pci,
1760 const struct pci_device_id *id)
1764 mlxsw_pci_write32(mlxsw_pci, SW_RESET, MLXSW_PCI_SW_RESET_RST_BIT);
1765 if (id->device == PCI_DEVICE_ID_MELLANOX_SWITCHX2) {
1766 msleep(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
1770 wmb(); /* reset needs to be written before we read control register */
1771 end = jiffies + msecs_to_jiffies(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
1773 u32 val = mlxsw_pci_read32(mlxsw_pci, FW_READY);
1775 if ((val & MLXSW_PCI_FW_READY_MASK) == MLXSW_PCI_FW_READY_MAGIC)
1778 } while (time_before(jiffies, end));
1782 static int mlxsw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1784 const char *driver_name = pdev->driver->name;
1785 struct mlxsw_pci *mlxsw_pci;
1788 mlxsw_pci = kzalloc(sizeof(*mlxsw_pci), GFP_KERNEL);
1792 err = pci_enable_device(pdev);
1794 dev_err(&pdev->dev, "pci_enable_device failed\n");
1795 goto err_pci_enable_device;
1798 err = pci_request_regions(pdev, driver_name);
1800 dev_err(&pdev->dev, "pci_request_regions failed\n");
1801 goto err_pci_request_regions;
1804 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1806 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1808 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
1809 goto err_pci_set_dma_mask;
1812 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1814 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
1815 goto err_pci_set_dma_mask;
1819 if (pci_resource_len(pdev, 0) < MLXSW_PCI_BAR0_SIZE) {
1820 dev_err(&pdev->dev, "invalid PCI region size\n");
1822 goto err_pci_resource_len_check;
1825 mlxsw_pci->hw_addr = ioremap(pci_resource_start(pdev, 0),
1826 pci_resource_len(pdev, 0));
1827 if (!mlxsw_pci->hw_addr) {
1828 dev_err(&pdev->dev, "ioremap failed\n");
1832 pci_set_master(pdev);
1834 mlxsw_pci->pdev = pdev;
1835 pci_set_drvdata(pdev, mlxsw_pci);
1837 err = mlxsw_pci_sw_reset(mlxsw_pci, id);
1839 dev_err(&pdev->dev, "Software reset failed\n");
1843 err = pci_enable_msix_exact(pdev, &mlxsw_pci->msix_entry, 1);
1845 dev_err(&pdev->dev, "MSI-X init failed\n");
1849 mlxsw_pci->bus_info.device_kind = driver_name;
1850 mlxsw_pci->bus_info.device_name = pci_name(mlxsw_pci->pdev);
1851 mlxsw_pci->bus_info.dev = &pdev->dev;
1853 mlxsw_pci->dbg_dir = debugfs_create_dir(mlxsw_pci->bus_info.device_name,
1854 mlxsw_pci_dbg_root);
1855 if (!mlxsw_pci->dbg_dir) {
1856 dev_err(&pdev->dev, "Failed to create debugfs dir\n");
1858 goto err_dbg_create_dir;
1861 err = mlxsw_core_bus_device_register(&mlxsw_pci->bus_info,
1862 &mlxsw_pci_bus, mlxsw_pci);
1864 dev_err(&pdev->dev, "cannot register bus device\n");
1865 goto err_bus_device_register;
1870 err_bus_device_register:
1871 debugfs_remove_recursive(mlxsw_pci->dbg_dir);
1873 pci_disable_msix(mlxsw_pci->pdev);
1876 iounmap(mlxsw_pci->hw_addr);
1878 err_pci_resource_len_check:
1879 err_pci_set_dma_mask:
1880 pci_release_regions(pdev);
1881 err_pci_request_regions:
1882 pci_disable_device(pdev);
1883 err_pci_enable_device:
1888 static void mlxsw_pci_remove(struct pci_dev *pdev)
1890 struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
1892 mlxsw_core_bus_device_unregister(mlxsw_pci->core);
1893 debugfs_remove_recursive(mlxsw_pci->dbg_dir);
1894 pci_disable_msix(mlxsw_pci->pdev);
1895 iounmap(mlxsw_pci->hw_addr);
1896 pci_release_regions(mlxsw_pci->pdev);
1897 pci_disable_device(mlxsw_pci->pdev);
1901 int mlxsw_pci_driver_register(struct pci_driver *pci_driver)
1903 pci_driver->probe = mlxsw_pci_probe;
1904 pci_driver->remove = mlxsw_pci_remove;
1905 return pci_register_driver(pci_driver);
1907 EXPORT_SYMBOL(mlxsw_pci_driver_register);
1909 void mlxsw_pci_driver_unregister(struct pci_driver *pci_driver)
1911 pci_unregister_driver(pci_driver);
1913 EXPORT_SYMBOL(mlxsw_pci_driver_unregister);
1915 static int __init mlxsw_pci_module_init(void)
1917 mlxsw_pci_dbg_root = debugfs_create_dir(mlxsw_pci_driver_name, NULL);
1918 if (!mlxsw_pci_dbg_root)
1923 static void __exit mlxsw_pci_module_exit(void)
1925 debugfs_remove_recursive(mlxsw_pci_dbg_root);
1928 module_init(mlxsw_pci_module_init);
1929 module_exit(mlxsw_pci_module_exit);
1931 MODULE_LICENSE("Dual BSD/GPL");
1932 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1933 MODULE_DESCRIPTION("Mellanox switch PCI interface driver");