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