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 static const char mlxsw_pci_driver_name[] = "mlxsw_pci";
58 static const struct pci_device_id mlxsw_pci_id_table[] = {
59 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SWITCHX2), 0},
60 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
64 static struct dentry *mlxsw_pci_dbg_root;
66 static const char *mlxsw_pci_device_kind_get(const struct pci_device_id *id)
69 case PCI_DEVICE_ID_MELLANOX_SWITCHX2:
70 return MLXSW_DEVICE_KIND_SWITCHX2;
71 case PCI_DEVICE_ID_MELLANOX_SPECTRUM:
72 return MLXSW_DEVICE_KIND_SPECTRUM;
78 #define mlxsw_pci_write32(mlxsw_pci, reg, val) \
79 iowrite32be(val, (mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
80 #define mlxsw_pci_read32(mlxsw_pci, reg) \
81 ioread32be((mlxsw_pci)->hw_addr + (MLXSW_PCI_ ## reg))
83 enum mlxsw_pci_queue_type {
84 MLXSW_PCI_QUEUE_TYPE_SDQ,
85 MLXSW_PCI_QUEUE_TYPE_RDQ,
86 MLXSW_PCI_QUEUE_TYPE_CQ,
87 MLXSW_PCI_QUEUE_TYPE_EQ,
90 static const char *mlxsw_pci_queue_type_str(enum mlxsw_pci_queue_type q_type)
93 case MLXSW_PCI_QUEUE_TYPE_SDQ:
95 case MLXSW_PCI_QUEUE_TYPE_RDQ:
97 case MLXSW_PCI_QUEUE_TYPE_CQ:
99 case MLXSW_PCI_QUEUE_TYPE_EQ:
105 #define MLXSW_PCI_QUEUE_TYPE_COUNT 4
107 static const u16 mlxsw_pci_doorbell_type_offset[] = {
108 MLXSW_PCI_DOORBELL_SDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_SDQ */
109 MLXSW_PCI_DOORBELL_RDQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_RDQ */
110 MLXSW_PCI_DOORBELL_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
111 MLXSW_PCI_DOORBELL_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
114 static const u16 mlxsw_pci_doorbell_arm_type_offset[] = {
117 MLXSW_PCI_DOORBELL_ARM_CQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_CQ */
118 MLXSW_PCI_DOORBELL_ARM_EQ_OFFSET, /* for type MLXSW_PCI_QUEUE_TYPE_EQ */
121 struct mlxsw_pci_mem_item {
127 struct mlxsw_pci_queue_elem_info {
128 char *elem; /* pointer to actual dma mapped element mem chunk */
139 struct mlxsw_pci_queue {
140 spinlock_t lock; /* for queue accesses */
141 struct mlxsw_pci_mem_item mem_item;
142 struct mlxsw_pci_queue_elem_info *elem_info;
143 u16 producer_counter;
144 u16 consumer_counter;
145 u16 count; /* number of elements in queue */
146 u8 num; /* queue number */
147 u8 elem_size; /* size of one element */
148 enum mlxsw_pci_queue_type type;
149 struct tasklet_struct tasklet; /* queue processing tasklet */
150 struct mlxsw_pci *pci;
164 struct mlxsw_pci_queue_type_group {
165 struct mlxsw_pci_queue *q;
166 u8 count; /* number of queues in group */
170 struct pci_dev *pdev;
172 struct mlxsw_pci_queue_type_group queues[MLXSW_PCI_QUEUE_TYPE_COUNT];
174 struct msix_entry msix_entry;
175 struct mlxsw_core *core;
177 struct mlxsw_pci_mem_item *items;
181 struct mlxsw_pci_mem_item out_mbox;
182 struct mlxsw_pci_mem_item in_mbox;
183 struct mutex lock; /* Lock access to command registers */
185 wait_queue_head_t wait;
192 struct mlxsw_bus_info bus_info;
193 struct dentry *dbg_dir;
196 static void mlxsw_pci_queue_tasklet_schedule(struct mlxsw_pci_queue *q)
198 tasklet_schedule(&q->tasklet);
201 static char *__mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q,
202 size_t elem_size, int elem_index)
204 return q->mem_item.buf + (elem_size * elem_index);
207 static struct mlxsw_pci_queue_elem_info *
208 mlxsw_pci_queue_elem_info_get(struct mlxsw_pci_queue *q, int elem_index)
210 return &q->elem_info[elem_index];
213 static struct mlxsw_pci_queue_elem_info *
214 mlxsw_pci_queue_elem_info_producer_get(struct mlxsw_pci_queue *q)
216 int index = q->producer_counter & (q->count - 1);
218 if ((q->producer_counter - q->consumer_counter) == q->count)
220 return mlxsw_pci_queue_elem_info_get(q, index);
223 static struct mlxsw_pci_queue_elem_info *
224 mlxsw_pci_queue_elem_info_consumer_get(struct mlxsw_pci_queue *q)
226 int index = q->consumer_counter & (q->count - 1);
228 return mlxsw_pci_queue_elem_info_get(q, index);
231 static char *mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, int elem_index)
233 return mlxsw_pci_queue_elem_info_get(q, elem_index)->elem;
236 static bool mlxsw_pci_elem_hw_owned(struct mlxsw_pci_queue *q, bool owner_bit)
238 return owner_bit != !!(q->consumer_counter & q->count);
241 static char *mlxsw_pci_queue_sw_elem_get(struct mlxsw_pci_queue *q,
242 u32 (*get_elem_owner_func)(char *))
244 struct mlxsw_pci_queue_elem_info *elem_info;
248 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
249 elem = elem_info->elem;
250 owner_bit = get_elem_owner_func(elem);
251 if (mlxsw_pci_elem_hw_owned(q, owner_bit))
253 q->consumer_counter++;
254 rmb(); /* make sure we read owned bit before the rest of elem */
258 static struct mlxsw_pci_queue_type_group *
259 mlxsw_pci_queue_type_group_get(struct mlxsw_pci *mlxsw_pci,
260 enum mlxsw_pci_queue_type q_type)
262 return &mlxsw_pci->queues[q_type];
265 static u8 __mlxsw_pci_queue_count(struct mlxsw_pci *mlxsw_pci,
266 enum mlxsw_pci_queue_type q_type)
268 struct mlxsw_pci_queue_type_group *queue_group;
270 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_type);
271 return queue_group->count;
274 static u8 mlxsw_pci_sdq_count(struct mlxsw_pci *mlxsw_pci)
276 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_SDQ);
279 static u8 mlxsw_pci_rdq_count(struct mlxsw_pci *mlxsw_pci)
281 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_RDQ);
284 static u8 mlxsw_pci_cq_count(struct mlxsw_pci *mlxsw_pci)
286 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ);
289 static u8 mlxsw_pci_eq_count(struct mlxsw_pci *mlxsw_pci)
291 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ);
294 static struct mlxsw_pci_queue *
295 __mlxsw_pci_queue_get(struct mlxsw_pci *mlxsw_pci,
296 enum mlxsw_pci_queue_type q_type, u8 q_num)
298 return &mlxsw_pci->queues[q_type].q[q_num];
301 static struct mlxsw_pci_queue *mlxsw_pci_sdq_get(struct mlxsw_pci *mlxsw_pci,
304 return __mlxsw_pci_queue_get(mlxsw_pci,
305 MLXSW_PCI_QUEUE_TYPE_SDQ, q_num);
308 static struct mlxsw_pci_queue *mlxsw_pci_rdq_get(struct mlxsw_pci *mlxsw_pci,
311 return __mlxsw_pci_queue_get(mlxsw_pci,
312 MLXSW_PCI_QUEUE_TYPE_RDQ, q_num);
315 static struct mlxsw_pci_queue *mlxsw_pci_cq_get(struct mlxsw_pci *mlxsw_pci,
318 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ, q_num);
321 static struct mlxsw_pci_queue *mlxsw_pci_eq_get(struct mlxsw_pci *mlxsw_pci,
324 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ, q_num);
327 static void __mlxsw_pci_queue_doorbell_set(struct mlxsw_pci *mlxsw_pci,
328 struct mlxsw_pci_queue *q,
331 mlxsw_pci_write32(mlxsw_pci,
332 DOORBELL(mlxsw_pci->doorbell_offset,
333 mlxsw_pci_doorbell_type_offset[q->type],
337 static void __mlxsw_pci_queue_doorbell_arm_set(struct mlxsw_pci *mlxsw_pci,
338 struct mlxsw_pci_queue *q,
341 mlxsw_pci_write32(mlxsw_pci,
342 DOORBELL(mlxsw_pci->doorbell_offset,
343 mlxsw_pci_doorbell_arm_type_offset[q->type],
347 static void mlxsw_pci_queue_doorbell_producer_ring(struct mlxsw_pci *mlxsw_pci,
348 struct mlxsw_pci_queue *q)
350 wmb(); /* ensure all writes are done before we ring a bell */
351 __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, q->producer_counter);
354 static void mlxsw_pci_queue_doorbell_consumer_ring(struct mlxsw_pci *mlxsw_pci,
355 struct mlxsw_pci_queue *q)
357 wmb(); /* ensure all writes are done before we ring a bell */
358 __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q,
359 q->consumer_counter + q->count);
363 mlxsw_pci_queue_doorbell_arm_consumer_ring(struct mlxsw_pci *mlxsw_pci,
364 struct mlxsw_pci_queue *q)
366 wmb(); /* ensure all writes are done before we ring a bell */
367 __mlxsw_pci_queue_doorbell_arm_set(mlxsw_pci, q, q->consumer_counter);
370 static dma_addr_t __mlxsw_pci_queue_page_get(struct mlxsw_pci_queue *q,
373 return q->mem_item.mapaddr + MLXSW_PCI_PAGE_SIZE * page_index;
376 static int mlxsw_pci_sdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
377 struct mlxsw_pci_queue *q)
382 q->producer_counter = 0;
383 q->consumer_counter = 0;
385 /* Set CQ of same number of this SDQ. */
386 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, q->num);
387 mlxsw_cmd_mbox_sw2hw_dq_sdq_tclass_set(mbox, 7);
388 mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
389 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
390 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
392 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
395 err = mlxsw_cmd_sw2hw_sdq(mlxsw_pci->core, mbox, q->num);
398 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
402 static void mlxsw_pci_sdq_fini(struct mlxsw_pci *mlxsw_pci,
403 struct mlxsw_pci_queue *q)
405 mlxsw_cmd_hw2sw_sdq(mlxsw_pci->core, q->num);
408 static int mlxsw_pci_sdq_dbg_read(struct seq_file *file, void *data)
410 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
411 struct mlxsw_pci_queue *q;
413 static const char hdr[] =
414 "NUM PROD_COUNT CONS_COUNT COUNT\n";
416 seq_printf(file, hdr);
417 for (i = 0; i < mlxsw_pci_sdq_count(mlxsw_pci); i++) {
418 q = mlxsw_pci_sdq_get(mlxsw_pci, i);
419 spin_lock_bh(&q->lock);
420 seq_printf(file, "%3d %10d %10d %5d\n",
421 i, q->producer_counter, q->consumer_counter,
423 spin_unlock_bh(&q->lock);
428 static int mlxsw_pci_wqe_frag_map(struct mlxsw_pci *mlxsw_pci, char *wqe,
429 int index, char *frag_data, size_t frag_len,
432 struct pci_dev *pdev = mlxsw_pci->pdev;
435 mapaddr = pci_map_single(pdev, frag_data, frag_len, direction);
436 if (unlikely(pci_dma_mapping_error(pdev, mapaddr))) {
437 dev_err_ratelimited(&pdev->dev, "failed to dma map tx frag\n");
440 mlxsw_pci_wqe_address_set(wqe, index, mapaddr);
441 mlxsw_pci_wqe_byte_count_set(wqe, index, frag_len);
445 static void mlxsw_pci_wqe_frag_unmap(struct mlxsw_pci *mlxsw_pci, char *wqe,
446 int index, int direction)
448 struct pci_dev *pdev = mlxsw_pci->pdev;
449 size_t frag_len = mlxsw_pci_wqe_byte_count_get(wqe, index);
450 dma_addr_t mapaddr = mlxsw_pci_wqe_address_get(wqe, index);
454 pci_unmap_single(pdev, mapaddr, frag_len, direction);
457 static int mlxsw_pci_rdq_skb_alloc(struct mlxsw_pci *mlxsw_pci,
458 struct mlxsw_pci_queue_elem_info *elem_info)
460 size_t buf_len = MLXSW_PORT_MAX_MTU;
461 char *wqe = elem_info->elem;
465 elem_info->u.rdq.skb = NULL;
466 skb = netdev_alloc_skb_ip_align(NULL, buf_len);
470 /* Assume that wqe was previously zeroed. */
472 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
473 buf_len, DMA_FROM_DEVICE);
477 elem_info->u.rdq.skb = skb;
481 dev_kfree_skb_any(skb);
485 static void mlxsw_pci_rdq_skb_free(struct mlxsw_pci *mlxsw_pci,
486 struct mlxsw_pci_queue_elem_info *elem_info)
491 skb = elem_info->u.rdq.skb;
492 wqe = elem_info->elem;
494 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
495 dev_kfree_skb_any(skb);
498 static int mlxsw_pci_rdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
499 struct mlxsw_pci_queue *q)
501 struct mlxsw_pci_queue_elem_info *elem_info;
502 u8 sdq_count = mlxsw_pci_sdq_count(mlxsw_pci);
506 q->producer_counter = 0;
507 q->consumer_counter = 0;
509 /* Set CQ of same number of this RDQ with base
510 * above SDQ count as the lower ones are assigned to SDQs.
512 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, sdq_count + q->num);
513 mlxsw_cmd_mbox_sw2hw_dq_log2_dq_sz_set(mbox, 3); /* 8 pages */
514 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
515 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
517 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
520 err = mlxsw_cmd_sw2hw_rdq(mlxsw_pci->core, mbox, q->num);
524 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
526 for (i = 0; i < q->count; i++) {
527 elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
529 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
532 /* Everything is set up, ring doorbell to pass elem to HW */
533 q->producer_counter++;
534 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
540 for (i--; i >= 0; i--) {
541 elem_info = mlxsw_pci_queue_elem_info_get(q, i);
542 mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
544 mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
549 static void mlxsw_pci_rdq_fini(struct mlxsw_pci *mlxsw_pci,
550 struct mlxsw_pci_queue *q)
552 struct mlxsw_pci_queue_elem_info *elem_info;
555 mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
556 for (i = 0; i < q->count; i++) {
557 elem_info = mlxsw_pci_queue_elem_info_get(q, i);
558 mlxsw_pci_rdq_skb_free(mlxsw_pci, elem_info);
562 static int mlxsw_pci_rdq_dbg_read(struct seq_file *file, void *data)
564 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
565 struct mlxsw_pci_queue *q;
567 static const char hdr[] =
568 "NUM PROD_COUNT CONS_COUNT COUNT\n";
570 seq_printf(file, hdr);
571 for (i = 0; i < mlxsw_pci_rdq_count(mlxsw_pci); i++) {
572 q = mlxsw_pci_rdq_get(mlxsw_pci, i);
573 spin_lock_bh(&q->lock);
574 seq_printf(file, "%3d %10d %10d %5d\n",
575 i, q->producer_counter, q->consumer_counter,
577 spin_unlock_bh(&q->lock);
582 static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
583 struct mlxsw_pci_queue *q)
588 q->consumer_counter = 0;
590 for (i = 0; i < q->count; i++) {
591 char *elem = mlxsw_pci_queue_elem_get(q, i);
593 mlxsw_pci_cqe_owner_set(elem, 1);
596 mlxsw_cmd_mbox_sw2hw_cq_cv_set(mbox, 0); /* CQE ver 0 */
597 mlxsw_cmd_mbox_sw2hw_cq_c_eqn_set(mbox, MLXSW_PCI_EQ_COMP_NUM);
598 mlxsw_cmd_mbox_sw2hw_cq_oi_set(mbox, 0);
599 mlxsw_cmd_mbox_sw2hw_cq_st_set(mbox, 0);
600 mlxsw_cmd_mbox_sw2hw_cq_log_cq_size_set(mbox, ilog2(q->count));
601 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
602 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
604 mlxsw_cmd_mbox_sw2hw_cq_pa_set(mbox, i, mapaddr);
606 err = mlxsw_cmd_sw2hw_cq(mlxsw_pci->core, mbox, q->num);
609 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
610 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
614 static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci,
615 struct mlxsw_pci_queue *q)
617 mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num);
620 static int mlxsw_pci_cq_dbg_read(struct seq_file *file, void *data)
622 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
624 struct mlxsw_pci_queue *q;
626 static const char hdr[] =
627 "NUM CONS_INDEX SDQ_COUNT RDQ_COUNT COUNT\n";
629 seq_printf(file, hdr);
630 for (i = 0; i < mlxsw_pci_cq_count(mlxsw_pci); i++) {
631 q = mlxsw_pci_cq_get(mlxsw_pci, i);
632 spin_lock_bh(&q->lock);
633 seq_printf(file, "%3d %10d %10d %10d %5d\n",
634 i, q->consumer_counter, q->u.cq.comp_sdq_count,
635 q->u.cq.comp_rdq_count, q->count);
636 spin_unlock_bh(&q->lock);
641 static void mlxsw_pci_cqe_sdq_handle(struct mlxsw_pci *mlxsw_pci,
642 struct mlxsw_pci_queue *q,
643 u16 consumer_counter_limit,
646 struct pci_dev *pdev = mlxsw_pci->pdev;
647 struct mlxsw_pci_queue_elem_info *elem_info;
653 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
654 skb = elem_info->u.sdq.skb;
655 wqe = elem_info->elem;
656 for (i = 0; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
657 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
658 dev_kfree_skb_any(skb);
659 elem_info->u.sdq.skb = NULL;
661 if (q->consumer_counter++ != consumer_counter_limit)
662 dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in SDQ\n");
663 spin_unlock(&q->lock);
666 static void mlxsw_pci_cqe_rdq_handle(struct mlxsw_pci *mlxsw_pci,
667 struct mlxsw_pci_queue *q,
668 u16 consumer_counter_limit,
671 struct pci_dev *pdev = mlxsw_pci->pdev;
672 struct mlxsw_pci_queue_elem_info *elem_info;
675 struct mlxsw_rx_info rx_info;
679 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
680 skb = elem_info->u.sdq.skb;
683 wqe = elem_info->elem;
684 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
686 if (q->consumer_counter++ != consumer_counter_limit)
687 dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in RDQ\n");
689 /* We do not support lag now */
690 if (mlxsw_pci_cqe_lag_get(cqe))
693 rx_info.is_lag = false;
694 rx_info.u.sys_port = mlxsw_pci_cqe_system_port_get(cqe);
696 rx_info.trap_id = mlxsw_pci_cqe_trap_id_get(cqe);
698 byte_count = mlxsw_pci_cqe_byte_count_get(cqe);
699 if (mlxsw_pci_cqe_crc_get(cqe))
700 byte_count -= ETH_FCS_LEN;
701 skb_put(skb, byte_count);
702 mlxsw_core_skb_receive(mlxsw_pci->core, skb, &rx_info);
705 memset(wqe, 0, q->elem_size);
706 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
708 dev_dbg_ratelimited(&pdev->dev, "Failed to alloc skb for RDQ\n");
709 /* Everything is set up, ring doorbell to pass elem to HW */
710 q->producer_counter++;
711 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
715 dev_kfree_skb_any(skb);
719 static char *mlxsw_pci_cq_sw_cqe_get(struct mlxsw_pci_queue *q)
721 return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_cqe_owner_get);
724 static void mlxsw_pci_cq_tasklet(unsigned long data)
726 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
727 struct mlxsw_pci *mlxsw_pci = q->pci;
730 int credits = q->count >> 1;
732 while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) {
733 u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe);
734 u8 sendq = mlxsw_pci_cqe_sr_get(cqe);
735 u8 dqn = mlxsw_pci_cqe_dqn_get(cqe);
738 struct mlxsw_pci_queue *sdq;
740 sdq = mlxsw_pci_sdq_get(mlxsw_pci, dqn);
741 mlxsw_pci_cqe_sdq_handle(mlxsw_pci, sdq,
743 q->u.cq.comp_sdq_count++;
745 struct mlxsw_pci_queue *rdq;
747 rdq = mlxsw_pci_rdq_get(mlxsw_pci, dqn);
748 mlxsw_pci_cqe_rdq_handle(mlxsw_pci, rdq,
750 q->u.cq.comp_rdq_count++;
752 if (++items == credits)
756 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
757 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
761 static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
762 struct mlxsw_pci_queue *q)
767 q->consumer_counter = 0;
769 for (i = 0; i < q->count; i++) {
770 char *elem = mlxsw_pci_queue_elem_get(q, i);
772 mlxsw_pci_eqe_owner_set(elem, 1);
775 mlxsw_cmd_mbox_sw2hw_eq_int_msix_set(mbox, 1); /* MSI-X used */
776 mlxsw_cmd_mbox_sw2hw_eq_oi_set(mbox, 0);
777 mlxsw_cmd_mbox_sw2hw_eq_st_set(mbox, 1); /* armed */
778 mlxsw_cmd_mbox_sw2hw_eq_log_eq_size_set(mbox, ilog2(q->count));
779 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
780 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
782 mlxsw_cmd_mbox_sw2hw_eq_pa_set(mbox, i, mapaddr);
784 err = mlxsw_cmd_sw2hw_eq(mlxsw_pci->core, mbox, q->num);
787 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
788 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
792 static void mlxsw_pci_eq_fini(struct mlxsw_pci *mlxsw_pci,
793 struct mlxsw_pci_queue *q)
795 mlxsw_cmd_hw2sw_eq(mlxsw_pci->core, q->num);
798 static int mlxsw_pci_eq_dbg_read(struct seq_file *file, void *data)
800 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
801 struct mlxsw_pci_queue *q;
803 static const char hdr[] =
804 "NUM CONS_COUNT EV_CMD EV_COMP EV_OTHER COUNT\n";
806 seq_printf(file, hdr);
807 for (i = 0; i < mlxsw_pci_eq_count(mlxsw_pci); i++) {
808 q = mlxsw_pci_eq_get(mlxsw_pci, i);
809 spin_lock_bh(&q->lock);
810 seq_printf(file, "%3d %10d %10d %10d %10d %5d\n",
811 i, q->consumer_counter, q->u.eq.ev_cmd_count,
812 q->u.eq.ev_comp_count, q->u.eq.ev_other_count,
814 spin_unlock_bh(&q->lock);
819 static void mlxsw_pci_eq_cmd_event(struct mlxsw_pci *mlxsw_pci, char *eqe)
821 mlxsw_pci->cmd.comp.status = mlxsw_pci_eqe_cmd_status_get(eqe);
822 mlxsw_pci->cmd.comp.out_param =
823 ((u64) mlxsw_pci_eqe_cmd_out_param_h_get(eqe)) << 32 |
824 mlxsw_pci_eqe_cmd_out_param_l_get(eqe);
825 mlxsw_pci->cmd.wait_done = true;
826 wake_up(&mlxsw_pci->cmd.wait);
829 static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q)
831 return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_eqe_owner_get);
834 static void mlxsw_pci_eq_tasklet(unsigned long data)
836 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
837 struct mlxsw_pci *mlxsw_pci = q->pci;
838 u8 cq_count = mlxsw_pci_cq_count(mlxsw_pci);
839 unsigned long active_cqns[BITS_TO_LONGS(MLXSW_PCI_CQS_MAX)];
842 bool cq_handle = false;
844 int credits = q->count >> 1;
846 memset(&active_cqns, 0, sizeof(active_cqns));
848 while ((eqe = mlxsw_pci_eq_sw_eqe_get(q))) {
849 u8 event_type = mlxsw_pci_eqe_event_type_get(eqe);
851 switch (event_type) {
852 case MLXSW_PCI_EQE_EVENT_TYPE_CMD:
853 mlxsw_pci_eq_cmd_event(mlxsw_pci, eqe);
854 q->u.eq.ev_cmd_count++;
856 case MLXSW_PCI_EQE_EVENT_TYPE_COMP:
857 cqn = mlxsw_pci_eqe_cqn_get(eqe);
858 set_bit(cqn, active_cqns);
860 q->u.eq.ev_comp_count++;
863 q->u.eq.ev_other_count++;
865 if (++items == credits)
869 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
870 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
875 for_each_set_bit(cqn, active_cqns, cq_count) {
876 q = mlxsw_pci_cq_get(mlxsw_pci, cqn);
877 mlxsw_pci_queue_tasklet_schedule(q);
881 struct mlxsw_pci_queue_ops {
883 enum mlxsw_pci_queue_type type;
884 int (*init)(struct mlxsw_pci *mlxsw_pci, char *mbox,
885 struct mlxsw_pci_queue *q);
886 void (*fini)(struct mlxsw_pci *mlxsw_pci,
887 struct mlxsw_pci_queue *q);
888 void (*tasklet)(unsigned long data);
889 int (*dbg_read)(struct seq_file *s, void *data);
894 static const struct mlxsw_pci_queue_ops mlxsw_pci_sdq_ops = {
895 .type = MLXSW_PCI_QUEUE_TYPE_SDQ,
896 .init = mlxsw_pci_sdq_init,
897 .fini = mlxsw_pci_sdq_fini,
898 .dbg_read = mlxsw_pci_sdq_dbg_read,
899 .elem_count = MLXSW_PCI_WQE_COUNT,
900 .elem_size = MLXSW_PCI_WQE_SIZE,
903 static const struct mlxsw_pci_queue_ops mlxsw_pci_rdq_ops = {
904 .type = MLXSW_PCI_QUEUE_TYPE_RDQ,
905 .init = mlxsw_pci_rdq_init,
906 .fini = mlxsw_pci_rdq_fini,
907 .dbg_read = mlxsw_pci_rdq_dbg_read,
908 .elem_count = MLXSW_PCI_WQE_COUNT,
909 .elem_size = MLXSW_PCI_WQE_SIZE
912 static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = {
913 .type = MLXSW_PCI_QUEUE_TYPE_CQ,
914 .init = mlxsw_pci_cq_init,
915 .fini = mlxsw_pci_cq_fini,
916 .tasklet = mlxsw_pci_cq_tasklet,
917 .dbg_read = mlxsw_pci_cq_dbg_read,
918 .elem_count = MLXSW_PCI_CQE_COUNT,
919 .elem_size = MLXSW_PCI_CQE_SIZE
922 static const struct mlxsw_pci_queue_ops mlxsw_pci_eq_ops = {
923 .type = MLXSW_PCI_QUEUE_TYPE_EQ,
924 .init = mlxsw_pci_eq_init,
925 .fini = mlxsw_pci_eq_fini,
926 .tasklet = mlxsw_pci_eq_tasklet,
927 .dbg_read = mlxsw_pci_eq_dbg_read,
928 .elem_count = MLXSW_PCI_EQE_COUNT,
929 .elem_size = MLXSW_PCI_EQE_SIZE
932 static int mlxsw_pci_queue_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
933 const struct mlxsw_pci_queue_ops *q_ops,
934 struct mlxsw_pci_queue *q, u8 q_num)
936 struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
940 spin_lock_init(&q->lock);
942 q->count = q_ops->elem_count;
943 q->elem_size = q_ops->elem_size;
944 q->type = q_ops->type;
948 tasklet_init(&q->tasklet, q_ops->tasklet, (unsigned long) q);
950 mem_item->size = MLXSW_PCI_AQ_SIZE;
951 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
956 memset(mem_item->buf, 0, mem_item->size);
958 q->elem_info = kcalloc(q->count, sizeof(*q->elem_info), GFP_KERNEL);
961 goto err_elem_info_alloc;
964 /* Initialize dma mapped elements info elem_info for
965 * future easy access.
967 for (i = 0; i < q->count; i++) {
968 struct mlxsw_pci_queue_elem_info *elem_info;
970 elem_info = mlxsw_pci_queue_elem_info_get(q, i);
972 __mlxsw_pci_queue_elem_get(q, q_ops->elem_size, i);
975 mlxsw_cmd_mbox_zero(mbox);
976 err = q_ops->init(mlxsw_pci, mbox, q);
984 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
985 mem_item->buf, mem_item->mapaddr);
989 static void mlxsw_pci_queue_fini(struct mlxsw_pci *mlxsw_pci,
990 const struct mlxsw_pci_queue_ops *q_ops,
991 struct mlxsw_pci_queue *q)
993 struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
995 q_ops->fini(mlxsw_pci, q);
997 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
998 mem_item->buf, mem_item->mapaddr);
1001 static int mlxsw_pci_queue_group_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1002 const struct mlxsw_pci_queue_ops *q_ops,
1005 struct pci_dev *pdev = mlxsw_pci->pdev;
1006 struct mlxsw_pci_queue_type_group *queue_group;
1011 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
1012 queue_group->q = kcalloc(num_qs, sizeof(*queue_group->q), GFP_KERNEL);
1013 if (!queue_group->q)
1016 for (i = 0; i < num_qs; i++) {
1017 err = mlxsw_pci_queue_init(mlxsw_pci, mbox, q_ops,
1018 &queue_group->q[i], i);
1020 goto err_queue_init;
1022 queue_group->count = num_qs;
1024 sprintf(tmp, "%s_stats", mlxsw_pci_queue_type_str(q_ops->type));
1025 debugfs_create_devm_seqfile(&pdev->dev, tmp, mlxsw_pci->dbg_dir,
1031 for (i--; i >= 0; i--)
1032 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1033 kfree(queue_group->q);
1037 static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,
1038 const struct mlxsw_pci_queue_ops *q_ops)
1040 struct mlxsw_pci_queue_type_group *queue_group;
1043 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
1044 for (i = 0; i < queue_group->count; i++)
1045 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1046 kfree(queue_group->q);
1049 static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
1051 struct pci_dev *pdev = mlxsw_pci->pdev;
1062 mlxsw_cmd_mbox_zero(mbox);
1063 err = mlxsw_cmd_query_aq_cap(mlxsw_pci->core, mbox);
1067 num_sdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_sdqs_get(mbox);
1068 sdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_sdq_sz_get(mbox);
1069 num_rdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_rdqs_get(mbox);
1070 rdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_rdq_sz_get(mbox);
1071 num_cqs = mlxsw_cmd_mbox_query_aq_cap_max_num_cqs_get(mbox);
1072 cq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cq_sz_get(mbox);
1073 num_eqs = mlxsw_cmd_mbox_query_aq_cap_max_num_eqs_get(mbox);
1074 eq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_eq_sz_get(mbox);
1076 if (num_sdqs + num_rdqs > num_cqs ||
1077 num_cqs > MLXSW_PCI_CQS_MAX || num_eqs != MLXSW_PCI_EQS_COUNT) {
1078 dev_err(&pdev->dev, "Unsupported number of queues\n");
1082 if ((1 << sdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1083 (1 << rdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1084 (1 << cq_log2sz != MLXSW_PCI_CQE_COUNT) ||
1085 (1 << eq_log2sz != MLXSW_PCI_EQE_COUNT)) {
1086 dev_err(&pdev->dev, "Unsupported number of async queue descriptors\n");
1090 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_eq_ops,
1093 dev_err(&pdev->dev, "Failed to initialize event queues\n");
1097 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_cq_ops,
1100 dev_err(&pdev->dev, "Failed to initialize completion queues\n");
1104 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_sdq_ops,
1107 dev_err(&pdev->dev, "Failed to initialize send descriptor queues\n");
1111 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_rdq_ops,
1114 dev_err(&pdev->dev, "Failed to initialize receive descriptor queues\n");
1118 /* We have to poll in command interface until queues are initialized */
1119 mlxsw_pci->cmd.nopoll = true;
1123 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1125 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1127 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1131 static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci)
1133 mlxsw_pci->cmd.nopoll = false;
1134 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops);
1135 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1136 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1137 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1141 mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci,
1142 char *mbox, int index,
1143 const struct mlxsw_swid_config *swid)
1147 if (swid->used_type) {
1148 mlxsw_cmd_mbox_config_profile_swid_config_type_set(
1149 mbox, index, swid->type);
1152 if (swid->used_properties) {
1153 mlxsw_cmd_mbox_config_profile_swid_config_properties_set(
1154 mbox, index, swid->properties);
1157 mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask);
1160 static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
1161 const struct mlxsw_config_profile *profile)
1165 mlxsw_cmd_mbox_zero(mbox);
1167 if (profile->used_max_vepa_channels) {
1168 mlxsw_cmd_mbox_config_profile_set_max_vepa_channels_set(
1170 mlxsw_cmd_mbox_config_profile_max_vepa_channels_set(
1171 mbox, profile->max_vepa_channels);
1173 if (profile->used_max_lag) {
1174 mlxsw_cmd_mbox_config_profile_set_max_lag_set(
1176 mlxsw_cmd_mbox_config_profile_max_lag_set(
1177 mbox, profile->max_lag);
1179 if (profile->used_max_port_per_lag) {
1180 mlxsw_cmd_mbox_config_profile_set_max_port_per_lag_set(
1182 mlxsw_cmd_mbox_config_profile_max_port_per_lag_set(
1183 mbox, profile->max_port_per_lag);
1185 if (profile->used_max_mid) {
1186 mlxsw_cmd_mbox_config_profile_set_max_mid_set(
1188 mlxsw_cmd_mbox_config_profile_max_mid_set(
1189 mbox, profile->max_mid);
1191 if (profile->used_max_pgt) {
1192 mlxsw_cmd_mbox_config_profile_set_max_pgt_set(
1194 mlxsw_cmd_mbox_config_profile_max_pgt_set(
1195 mbox, profile->max_pgt);
1197 if (profile->used_max_system_port) {
1198 mlxsw_cmd_mbox_config_profile_set_max_system_port_set(
1200 mlxsw_cmd_mbox_config_profile_max_system_port_set(
1201 mbox, profile->max_system_port);
1203 if (profile->used_max_vlan_groups) {
1204 mlxsw_cmd_mbox_config_profile_set_max_vlan_groups_set(
1206 mlxsw_cmd_mbox_config_profile_max_vlan_groups_set(
1207 mbox, profile->max_vlan_groups);
1209 if (profile->used_max_regions) {
1210 mlxsw_cmd_mbox_config_profile_set_max_regions_set(
1212 mlxsw_cmd_mbox_config_profile_max_regions_set(
1213 mbox, profile->max_regions);
1215 if (profile->used_flood_tables) {
1216 mlxsw_cmd_mbox_config_profile_set_flood_tables_set(
1218 mlxsw_cmd_mbox_config_profile_max_flood_tables_set(
1219 mbox, profile->max_flood_tables);
1220 mlxsw_cmd_mbox_config_profile_max_vid_flood_tables_set(
1221 mbox, profile->max_vid_flood_tables);
1222 mlxsw_cmd_mbox_config_profile_max_fid_offset_flood_tables_set(
1223 mbox, profile->max_fid_offset_flood_tables);
1224 mlxsw_cmd_mbox_config_profile_fid_offset_flood_table_size_set(
1225 mbox, profile->fid_offset_flood_table_size);
1226 mlxsw_cmd_mbox_config_profile_max_fid_flood_tables_set(
1227 mbox, profile->max_fid_flood_tables);
1228 mlxsw_cmd_mbox_config_profile_fid_flood_table_size_set(
1229 mbox, profile->fid_flood_table_size);
1231 if (profile->used_flood_mode) {
1232 mlxsw_cmd_mbox_config_profile_set_flood_mode_set(
1234 mlxsw_cmd_mbox_config_profile_flood_mode_set(
1235 mbox, profile->flood_mode);
1237 if (profile->used_max_ib_mc) {
1238 mlxsw_cmd_mbox_config_profile_set_max_ib_mc_set(
1240 mlxsw_cmd_mbox_config_profile_max_ib_mc_set(
1241 mbox, profile->max_ib_mc);
1243 if (profile->used_max_pkey) {
1244 mlxsw_cmd_mbox_config_profile_set_max_pkey_set(
1246 mlxsw_cmd_mbox_config_profile_max_pkey_set(
1247 mbox, profile->max_pkey);
1249 if (profile->used_ar_sec) {
1250 mlxsw_cmd_mbox_config_profile_set_ar_sec_set(
1252 mlxsw_cmd_mbox_config_profile_ar_sec_set(
1253 mbox, profile->ar_sec);
1255 if (profile->used_adaptive_routing_group_cap) {
1256 mlxsw_cmd_mbox_config_profile_set_adaptive_routing_group_cap_set(
1258 mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set(
1259 mbox, profile->adaptive_routing_group_cap);
1262 for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++)
1263 mlxsw_pci_config_profile_swid_config(mlxsw_pci, mbox, i,
1264 &profile->swid_config[i]);
1266 return mlxsw_cmd_config_profile_set(mlxsw_pci->core, mbox);
1269 static int mlxsw_pci_boardinfo(struct mlxsw_pci *mlxsw_pci, char *mbox)
1271 struct mlxsw_bus_info *bus_info = &mlxsw_pci->bus_info;
1274 mlxsw_cmd_mbox_zero(mbox);
1275 err = mlxsw_cmd_boardinfo(mlxsw_pci->core, mbox);
1278 mlxsw_cmd_mbox_boardinfo_vsd_memcpy_from(mbox, bus_info->vsd);
1279 mlxsw_cmd_mbox_boardinfo_psid_memcpy_from(mbox, bus_info->psid);
1283 static int mlxsw_pci_fw_area_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1286 struct mlxsw_pci_mem_item *mem_item;
1291 mlxsw_pci->fw_area.items = kcalloc(num_pages, sizeof(*mem_item),
1293 if (!mlxsw_pci->fw_area.items)
1295 mlxsw_pci->fw_area.count = num_pages;
1297 mlxsw_cmd_mbox_zero(mbox);
1298 for (i = 0; i < num_pages; i++) {
1299 mem_item = &mlxsw_pci->fw_area.items[i];
1301 mem_item->size = MLXSW_PCI_PAGE_SIZE;
1302 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
1304 &mem_item->mapaddr);
1305 if (!mem_item->buf) {
1309 mlxsw_cmd_mbox_map_fa_pa_set(mbox, nent, mem_item->mapaddr);
1310 mlxsw_cmd_mbox_map_fa_log2size_set(mbox, nent, 0); /* 1 page */
1311 if (++nent == MLXSW_CMD_MAP_FA_VPM_ENTRIES_MAX) {
1312 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1314 goto err_cmd_map_fa;
1316 mlxsw_cmd_mbox_zero(mbox);
1321 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1323 goto err_cmd_map_fa;
1330 for (i--; i >= 0; i--) {
1331 mem_item = &mlxsw_pci->fw_area.items[i];
1333 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1334 mem_item->buf, mem_item->mapaddr);
1336 kfree(mlxsw_pci->fw_area.items);
1340 static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci)
1342 struct mlxsw_pci_mem_item *mem_item;
1345 mlxsw_cmd_unmap_fa(mlxsw_pci->core);
1347 for (i = 0; i < mlxsw_pci->fw_area.count; i++) {
1348 mem_item = &mlxsw_pci->fw_area.items[i];
1350 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1351 mem_item->buf, mem_item->mapaddr);
1353 kfree(mlxsw_pci->fw_area.items);
1356 static irqreturn_t mlxsw_pci_eq_irq_handler(int irq, void *dev_id)
1358 struct mlxsw_pci *mlxsw_pci = dev_id;
1359 struct mlxsw_pci_queue *q;
1362 for (i = 0; i < MLXSW_PCI_EQS_COUNT; i++) {
1363 q = mlxsw_pci_eq_get(mlxsw_pci, i);
1364 mlxsw_pci_queue_tasklet_schedule(q);
1369 static int mlxsw_pci_mbox_alloc(struct mlxsw_pci *mlxsw_pci,
1370 struct mlxsw_pci_mem_item *mbox)
1372 struct pci_dev *pdev = mlxsw_pci->pdev;
1375 mbox->size = MLXSW_CMD_MBOX_SIZE;
1376 mbox->buf = pci_alloc_consistent(pdev, MLXSW_CMD_MBOX_SIZE,
1379 dev_err(&pdev->dev, "Failed allocating memory for mailbox\n");
1386 static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci,
1387 struct mlxsw_pci_mem_item *mbox)
1389 struct pci_dev *pdev = mlxsw_pci->pdev;
1391 pci_free_consistent(pdev, MLXSW_CMD_MBOX_SIZE, mbox->buf,
1395 static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
1396 const struct mlxsw_config_profile *profile)
1398 struct mlxsw_pci *mlxsw_pci = bus_priv;
1399 struct pci_dev *pdev = mlxsw_pci->pdev;
1404 mutex_init(&mlxsw_pci->cmd.lock);
1405 init_waitqueue_head(&mlxsw_pci->cmd.wait);
1407 mlxsw_pci->core = mlxsw_core;
1409 mbox = mlxsw_cmd_mbox_alloc();
1413 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1417 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1419 goto err_out_mbox_alloc;
1421 err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
1425 mlxsw_pci->bus_info.fw_rev.major =
1426 mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
1427 mlxsw_pci->bus_info.fw_rev.minor =
1428 mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
1429 mlxsw_pci->bus_info.fw_rev.subminor =
1430 mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
1432 if (mlxsw_cmd_mbox_query_fw_cmd_interface_rev_get(mbox) != 1) {
1433 dev_err(&pdev->dev, "Unsupported cmd interface revision ID queried from hw\n");
1437 if (mlxsw_cmd_mbox_query_fw_doorbell_page_bar_get(mbox) != 0) {
1438 dev_err(&pdev->dev, "Unsupported doorbell page bar queried from hw\n");
1440 goto err_doorbell_page_bar;
1443 mlxsw_pci->doorbell_offset =
1444 mlxsw_cmd_mbox_query_fw_doorbell_page_offset_get(mbox);
1446 num_pages = mlxsw_cmd_mbox_query_fw_fw_pages_get(mbox);
1447 err = mlxsw_pci_fw_area_init(mlxsw_pci, mbox, num_pages);
1449 goto err_fw_area_init;
1451 err = mlxsw_pci_boardinfo(mlxsw_pci, mbox);
1455 err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile);
1457 goto err_config_profile;
1459 err = mlxsw_pci_aqs_init(mlxsw_pci, mbox);
1463 err = request_irq(mlxsw_pci->msix_entry.vector,
1464 mlxsw_pci_eq_irq_handler, 0,
1465 mlxsw_pci_driver_name, mlxsw_pci);
1467 dev_err(&pdev->dev, "IRQ request failed\n");
1468 goto err_request_eq_irq;
1474 mlxsw_pci_aqs_fini(mlxsw_pci);
1478 mlxsw_pci_fw_area_fini(mlxsw_pci);
1480 err_doorbell_page_bar:
1483 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1485 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1487 mlxsw_cmd_mbox_free(mbox);
1491 static void mlxsw_pci_fini(void *bus_priv)
1493 struct mlxsw_pci *mlxsw_pci = bus_priv;
1495 free_irq(mlxsw_pci->msix_entry.vector, mlxsw_pci);
1496 mlxsw_pci_aqs_fini(mlxsw_pci);
1497 mlxsw_pci_fw_area_fini(mlxsw_pci);
1498 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1499 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1502 static struct mlxsw_pci_queue *
1503 mlxsw_pci_sdq_pick(struct mlxsw_pci *mlxsw_pci,
1504 const struct mlxsw_tx_info *tx_info)
1506 u8 sdqn = tx_info->local_port % mlxsw_pci_sdq_count(mlxsw_pci);
1508 return mlxsw_pci_sdq_get(mlxsw_pci, sdqn);
1511 static bool mlxsw_pci_skb_transmit_busy(void *bus_priv,
1512 const struct mlxsw_tx_info *tx_info)
1514 struct mlxsw_pci *mlxsw_pci = bus_priv;
1515 struct mlxsw_pci_queue *q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1517 return !mlxsw_pci_queue_elem_info_producer_get(q);
1520 static int mlxsw_pci_skb_transmit(void *bus_priv, struct sk_buff *skb,
1521 const struct mlxsw_tx_info *tx_info)
1523 struct mlxsw_pci *mlxsw_pci = bus_priv;
1524 struct mlxsw_pci_queue *q;
1525 struct mlxsw_pci_queue_elem_info *elem_info;
1530 if (skb_shinfo(skb)->nr_frags > MLXSW_PCI_WQE_SG_ENTRIES - 1) {
1531 err = skb_linearize(skb);
1536 q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1537 spin_lock_bh(&q->lock);
1538 elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
1544 elem_info->u.sdq.skb = skb;
1546 wqe = elem_info->elem;
1547 mlxsw_pci_wqe_c_set(wqe, 1); /* always report completion */
1548 mlxsw_pci_wqe_lp_set(wqe, !!tx_info->is_emad);
1549 mlxsw_pci_wqe_type_set(wqe, MLXSW_PCI_WQE_TYPE_ETHERNET);
1551 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
1552 skb_headlen(skb), DMA_TO_DEVICE);
1556 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1557 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1559 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, i + 1,
1560 skb_frag_address(frag),
1561 skb_frag_size(frag),
1567 /* Set unused sq entries byte count to zero. */
1568 for (i++; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
1569 mlxsw_pci_wqe_byte_count_set(wqe, i, 0);
1571 /* Everything is set up, ring producer doorbell to get HW going */
1572 q->producer_counter++;
1573 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
1579 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
1581 spin_unlock_bh(&q->lock);
1585 static int mlxsw_pci_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
1586 u32 in_mod, bool out_mbox_direct,
1587 char *in_mbox, size_t in_mbox_size,
1588 char *out_mbox, size_t out_mbox_size,
1591 struct mlxsw_pci *mlxsw_pci = bus_priv;
1592 dma_addr_t in_mapaddr = mlxsw_pci->cmd.in_mbox.mapaddr;
1593 dma_addr_t out_mapaddr = mlxsw_pci->cmd.out_mbox.mapaddr;
1594 bool evreq = mlxsw_pci->cmd.nopoll;
1595 unsigned long timeout = msecs_to_jiffies(MLXSW_PCI_CIR_TIMEOUT_MSECS);
1596 bool *p_wait_done = &mlxsw_pci->cmd.wait_done;
1599 *p_status = MLXSW_CMD_STATUS_OK;
1601 err = mutex_lock_interruptible(&mlxsw_pci->cmd.lock);
1606 memcpy(mlxsw_pci->cmd.in_mbox.buf, in_mbox, in_mbox_size);
1607 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_HI, upper_32_bits(in_mapaddr));
1608 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_LO, lower_32_bits(in_mapaddr));
1610 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_HI, upper_32_bits(out_mapaddr));
1611 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_LO, lower_32_bits(out_mapaddr));
1613 mlxsw_pci_write32(mlxsw_pci, CIR_IN_MODIFIER, in_mod);
1614 mlxsw_pci_write32(mlxsw_pci, CIR_TOKEN, 0);
1616 *p_wait_done = false;
1618 wmb(); /* all needs to be written before we write control register */
1619 mlxsw_pci_write32(mlxsw_pci, CIR_CTRL,
1620 MLXSW_PCI_CIR_CTRL_GO_BIT |
1621 (evreq ? MLXSW_PCI_CIR_CTRL_EVREQ_BIT : 0) |
1622 (opcode_mod << MLXSW_PCI_CIR_CTRL_OPCODE_MOD_SHIFT) |
1628 end = jiffies + timeout;
1630 u32 ctrl = mlxsw_pci_read32(mlxsw_pci, CIR_CTRL);
1632 if (!(ctrl & MLXSW_PCI_CIR_CTRL_GO_BIT)) {
1633 *p_wait_done = true;
1634 *p_status = ctrl >> MLXSW_PCI_CIR_CTRL_STATUS_SHIFT;
1638 } while (time_before(jiffies, end));
1640 wait_event_timeout(mlxsw_pci->cmd.wait, *p_wait_done, timeout);
1641 *p_status = mlxsw_pci->cmd.comp.status;
1652 if (!err && out_mbox && out_mbox_direct) {
1653 /* Some commands don't use output param as address to mailbox
1654 * but they store output directly into registers. In that case,
1655 * copy registers into mbox buffer.
1660 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1662 memcpy(out_mbox, &tmp, sizeof(tmp));
1663 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1665 memcpy(out_mbox + sizeof(tmp), &tmp, sizeof(tmp));
1667 } else if (!err && out_mbox) {
1668 memcpy(out_mbox, mlxsw_pci->cmd.out_mbox.buf, out_mbox_size);
1671 mutex_unlock(&mlxsw_pci->cmd.lock);
1676 static const struct mlxsw_bus mlxsw_pci_bus = {
1678 .init = mlxsw_pci_init,
1679 .fini = mlxsw_pci_fini,
1680 .skb_transmit_busy = mlxsw_pci_skb_transmit_busy,
1681 .skb_transmit = mlxsw_pci_skb_transmit,
1682 .cmd_exec = mlxsw_pci_cmd_exec,
1685 static int mlxsw_pci_sw_reset(struct mlxsw_pci *mlxsw_pci)
1687 mlxsw_pci_write32(mlxsw_pci, SW_RESET, MLXSW_PCI_SW_RESET_RST_BIT);
1688 /* Current firware does not let us know when the reset is done.
1689 * So we just wait here for constant time and hope for the best.
1691 msleep(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
1695 static int mlxsw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1697 struct mlxsw_pci *mlxsw_pci;
1700 mlxsw_pci = kzalloc(sizeof(*mlxsw_pci), GFP_KERNEL);
1704 err = pci_enable_device(pdev);
1706 dev_err(&pdev->dev, "pci_enable_device failed\n");
1707 goto err_pci_enable_device;
1710 err = pci_request_regions(pdev, mlxsw_pci_driver_name);
1712 dev_err(&pdev->dev, "pci_request_regions failed\n");
1713 goto err_pci_request_regions;
1716 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1718 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1720 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
1721 goto err_pci_set_dma_mask;
1724 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1726 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
1727 goto err_pci_set_dma_mask;
1731 if (pci_resource_len(pdev, 0) < MLXSW_PCI_BAR0_SIZE) {
1732 dev_err(&pdev->dev, "invalid PCI region size\n");
1734 goto err_pci_resource_len_check;
1737 mlxsw_pci->hw_addr = ioremap(pci_resource_start(pdev, 0),
1738 pci_resource_len(pdev, 0));
1739 if (!mlxsw_pci->hw_addr) {
1740 dev_err(&pdev->dev, "ioremap failed\n");
1744 pci_set_master(pdev);
1746 mlxsw_pci->pdev = pdev;
1747 pci_set_drvdata(pdev, mlxsw_pci);
1749 err = mlxsw_pci_sw_reset(mlxsw_pci);
1751 dev_err(&pdev->dev, "Software reset failed\n");
1755 err = pci_enable_msix_exact(pdev, &mlxsw_pci->msix_entry, 1);
1757 dev_err(&pdev->dev, "MSI-X init failed\n");
1761 mlxsw_pci->bus_info.device_kind = mlxsw_pci_device_kind_get(id);
1762 mlxsw_pci->bus_info.device_name = pci_name(mlxsw_pci->pdev);
1763 mlxsw_pci->bus_info.dev = &pdev->dev;
1765 mlxsw_pci->dbg_dir = debugfs_create_dir(mlxsw_pci->bus_info.device_name,
1766 mlxsw_pci_dbg_root);
1767 if (!mlxsw_pci->dbg_dir) {
1768 dev_err(&pdev->dev, "Failed to create debugfs dir\n");
1770 goto err_dbg_create_dir;
1773 err = mlxsw_core_bus_device_register(&mlxsw_pci->bus_info,
1774 &mlxsw_pci_bus, mlxsw_pci);
1776 dev_err(&pdev->dev, "cannot register bus device\n");
1777 goto err_bus_device_register;
1782 err_bus_device_register:
1783 debugfs_remove_recursive(mlxsw_pci->dbg_dir);
1785 pci_disable_msix(mlxsw_pci->pdev);
1788 iounmap(mlxsw_pci->hw_addr);
1790 err_pci_resource_len_check:
1791 err_pci_set_dma_mask:
1792 pci_release_regions(pdev);
1793 err_pci_request_regions:
1794 pci_disable_device(pdev);
1795 err_pci_enable_device:
1800 static void mlxsw_pci_remove(struct pci_dev *pdev)
1802 struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
1804 mlxsw_core_bus_device_unregister(mlxsw_pci->core);
1805 debugfs_remove_recursive(mlxsw_pci->dbg_dir);
1806 pci_disable_msix(mlxsw_pci->pdev);
1807 iounmap(mlxsw_pci->hw_addr);
1808 pci_release_regions(mlxsw_pci->pdev);
1809 pci_disable_device(mlxsw_pci->pdev);
1813 static struct pci_driver mlxsw_pci_driver = {
1814 .name = mlxsw_pci_driver_name,
1815 .id_table = mlxsw_pci_id_table,
1816 .probe = mlxsw_pci_probe,
1817 .remove = mlxsw_pci_remove,
1820 static int __init mlxsw_pci_module_init(void)
1824 mlxsw_pci_dbg_root = debugfs_create_dir(mlxsw_pci_driver_name, NULL);
1825 if (!mlxsw_pci_dbg_root)
1827 err = pci_register_driver(&mlxsw_pci_driver);
1829 goto err_register_driver;
1832 err_register_driver:
1833 debugfs_remove_recursive(mlxsw_pci_dbg_root);
1837 static void __exit mlxsw_pci_module_exit(void)
1839 pci_unregister_driver(&mlxsw_pci_driver);
1840 debugfs_remove_recursive(mlxsw_pci_dbg_root);
1843 module_init(mlxsw_pci_module_init);
1844 module_exit(mlxsw_pci_module_exit);
1846 MODULE_LICENSE("Dual BSD/GPL");
1847 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1848 MODULE_DESCRIPTION("Mellanox switch PCI interface driver");
1849 MODULE_DEVICE_TABLE(pci, mlxsw_pci_id_table);