blob: 7b2ab1ec1290334a12098b0e0c3181cf7f6b6df1 [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"
55
56static const char mlxsw_pci_driver_name[] = "mlxsw_pci";
57
58static const struct pci_device_id mlxsw_pci_id_table[] = {
Jiri Pirko31557f02015-07-29 23:33:49 +020059 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SWITCHX2), 0},
Jiri Pirko56ade8f2015-10-16 14:01:37 +020060 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SPECTRUM), 0},
Jiri Pirkoeda65002015-07-29 23:33:47 +020061 {0, }
62};
63
64static struct dentry *mlxsw_pci_dbg_root;
65
66static const char *mlxsw_pci_device_kind_get(const struct pci_device_id *id)
67{
68 switch (id->device) {
Jiri Pirko31557f02015-07-29 23:33:49 +020069 case PCI_DEVICE_ID_MELLANOX_SWITCHX2:
70 return MLXSW_DEVICE_KIND_SWITCHX2;
Jiri Pirko56ade8f2015-10-16 14:01:37 +020071 case PCI_DEVICE_ID_MELLANOX_SPECTRUM:
72 return MLXSW_DEVICE_KIND_SPECTRUM;
Jiri Pirkoeda65002015-07-29 23:33:47 +020073 default:
74 BUG();
75 }
76}
77
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))
82
83enum 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,
88};
89
90static const char *mlxsw_pci_queue_type_str(enum mlxsw_pci_queue_type q_type)
91{
92 switch (q_type) {
93 case MLXSW_PCI_QUEUE_TYPE_SDQ:
94 return "sdq";
95 case MLXSW_PCI_QUEUE_TYPE_RDQ:
96 return "rdq";
97 case MLXSW_PCI_QUEUE_TYPE_CQ:
98 return "cq";
99 case MLXSW_PCI_QUEUE_TYPE_EQ:
100 return "eq";
101 }
102 BUG();
103}
104
105#define MLXSW_PCI_QUEUE_TYPE_COUNT 4
106
107static 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 */
112};
113
114static const u16 mlxsw_pci_doorbell_arm_type_offset[] = {
115 0, /* unused */
116 0, /* unused */
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 */
119};
120
121struct mlxsw_pci_mem_item {
122 char *buf;
123 dma_addr_t mapaddr;
124 size_t size;
125};
126
127struct mlxsw_pci_queue_elem_info {
128 char *elem; /* pointer to actual dma mapped element mem chunk */
129 union {
130 struct {
131 struct sk_buff *skb;
132 } sdq;
133 struct {
134 struct sk_buff *skb;
135 } rdq;
136 } u;
137};
138
139struct 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;
151 union {
152 struct {
153 u32 comp_sdq_count;
154 u32 comp_rdq_count;
155 } cq;
156 struct {
157 u32 ev_cmd_count;
158 u32 ev_comp_count;
159 u32 ev_other_count;
160 } eq;
161 } u;
162};
163
164struct mlxsw_pci_queue_type_group {
165 struct mlxsw_pci_queue *q;
166 u8 count; /* number of queues in group */
167};
168
169struct mlxsw_pci {
170 struct pci_dev *pdev;
171 u8 __iomem *hw_addr;
172 struct mlxsw_pci_queue_type_group queues[MLXSW_PCI_QUEUE_TYPE_COUNT];
173 u32 doorbell_offset;
174 struct msix_entry msix_entry;
175 struct mlxsw_core *core;
176 struct {
Jiri Pirkoeda65002015-07-29 23:33:47 +0200177 struct mlxsw_pci_mem_item *items;
Jiri Pirko3e2206d2015-10-15 17:43:20 +0200178 unsigned int count;
Jiri Pirkoeda65002015-07-29 23:33:47 +0200179 } fw_area;
180 struct {
Ido Schimmel1e817792015-08-27 17:59:57 +0200181 struct mlxsw_pci_mem_item out_mbox;
182 struct mlxsw_pci_mem_item in_mbox;
Jiri Pirkoeda65002015-07-29 23:33:47 +0200183 struct mutex lock; /* Lock access to command registers */
184 bool nopoll;
185 wait_queue_head_t wait;
186 bool wait_done;
187 struct {
188 u8 status;
189 u64 out_param;
190 } comp;
191 } cmd;
192 struct mlxsw_bus_info bus_info;
193 struct dentry *dbg_dir;
194};
195
196static void mlxsw_pci_queue_tasklet_schedule(struct mlxsw_pci_queue *q)
197{
198 tasklet_schedule(&q->tasklet);
199}
200
201static char *__mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q,
202 size_t elem_size, int elem_index)
203{
204 return q->mem_item.buf + (elem_size * elem_index);
205}
206
207static struct mlxsw_pci_queue_elem_info *
208mlxsw_pci_queue_elem_info_get(struct mlxsw_pci_queue *q, int elem_index)
209{
210 return &q->elem_info[elem_index];
211}
212
213static struct mlxsw_pci_queue_elem_info *
214mlxsw_pci_queue_elem_info_producer_get(struct mlxsw_pci_queue *q)
215{
216 int index = q->producer_counter & (q->count - 1);
217
Ido Schimmel50917302016-03-07 15:15:30 +0100218 if ((u16) (q->producer_counter - q->consumer_counter) == q->count)
Jiri Pirkoeda65002015-07-29 23:33:47 +0200219 return NULL;
220 return mlxsw_pci_queue_elem_info_get(q, index);
221}
222
223static struct mlxsw_pci_queue_elem_info *
224mlxsw_pci_queue_elem_info_consumer_get(struct mlxsw_pci_queue *q)
225{
226 int index = q->consumer_counter & (q->count - 1);
227
228 return mlxsw_pci_queue_elem_info_get(q, index);
229}
230
231static char *mlxsw_pci_queue_elem_get(struct mlxsw_pci_queue *q, int elem_index)
232{
233 return mlxsw_pci_queue_elem_info_get(q, elem_index)->elem;
234}
235
236static bool mlxsw_pci_elem_hw_owned(struct mlxsw_pci_queue *q, bool owner_bit)
237{
238 return owner_bit != !!(q->consumer_counter & q->count);
239}
240
241static char *mlxsw_pci_queue_sw_elem_get(struct mlxsw_pci_queue *q,
242 u32 (*get_elem_owner_func)(char *))
243{
244 struct mlxsw_pci_queue_elem_info *elem_info;
245 char *elem;
246 bool owner_bit;
247
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))
252 return NULL;
253 q->consumer_counter++;
254 rmb(); /* make sure we read owned bit before the rest of elem */
255 return elem;
256}
257
258static struct mlxsw_pci_queue_type_group *
259mlxsw_pci_queue_type_group_get(struct mlxsw_pci *mlxsw_pci,
260 enum mlxsw_pci_queue_type q_type)
261{
262 return &mlxsw_pci->queues[q_type];
263}
264
265static u8 __mlxsw_pci_queue_count(struct mlxsw_pci *mlxsw_pci,
266 enum mlxsw_pci_queue_type q_type)
267{
268 struct mlxsw_pci_queue_type_group *queue_group;
269
270 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_type);
271 return queue_group->count;
272}
273
274static u8 mlxsw_pci_sdq_count(struct mlxsw_pci *mlxsw_pci)
275{
276 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_SDQ);
277}
278
279static u8 mlxsw_pci_rdq_count(struct mlxsw_pci *mlxsw_pci)
280{
281 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_RDQ);
282}
283
284static u8 mlxsw_pci_cq_count(struct mlxsw_pci *mlxsw_pci)
285{
286 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ);
287}
288
289static u8 mlxsw_pci_eq_count(struct mlxsw_pci *mlxsw_pci)
290{
291 return __mlxsw_pci_queue_count(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ);
292}
293
294static 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)
297{
298 return &mlxsw_pci->queues[q_type].q[q_num];
299}
300
301static struct mlxsw_pci_queue *mlxsw_pci_sdq_get(struct mlxsw_pci *mlxsw_pci,
302 u8 q_num)
303{
304 return __mlxsw_pci_queue_get(mlxsw_pci,
305 MLXSW_PCI_QUEUE_TYPE_SDQ, q_num);
306}
307
308static struct mlxsw_pci_queue *mlxsw_pci_rdq_get(struct mlxsw_pci *mlxsw_pci,
309 u8 q_num)
310{
311 return __mlxsw_pci_queue_get(mlxsw_pci,
312 MLXSW_PCI_QUEUE_TYPE_RDQ, q_num);
313}
314
315static struct mlxsw_pci_queue *mlxsw_pci_cq_get(struct mlxsw_pci *mlxsw_pci,
316 u8 q_num)
317{
318 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_CQ, q_num);
319}
320
321static struct mlxsw_pci_queue *mlxsw_pci_eq_get(struct mlxsw_pci *mlxsw_pci,
322 u8 q_num)
323{
324 return __mlxsw_pci_queue_get(mlxsw_pci, MLXSW_PCI_QUEUE_TYPE_EQ, q_num);
325}
326
327static void __mlxsw_pci_queue_doorbell_set(struct mlxsw_pci *mlxsw_pci,
328 struct mlxsw_pci_queue *q,
329 u16 val)
330{
331 mlxsw_pci_write32(mlxsw_pci,
332 DOORBELL(mlxsw_pci->doorbell_offset,
333 mlxsw_pci_doorbell_type_offset[q->type],
334 q->num), val);
335}
336
337static void __mlxsw_pci_queue_doorbell_arm_set(struct mlxsw_pci *mlxsw_pci,
338 struct mlxsw_pci_queue *q,
339 u16 val)
340{
341 mlxsw_pci_write32(mlxsw_pci,
342 DOORBELL(mlxsw_pci->doorbell_offset,
343 mlxsw_pci_doorbell_arm_type_offset[q->type],
344 q->num), val);
345}
346
347static void mlxsw_pci_queue_doorbell_producer_ring(struct mlxsw_pci *mlxsw_pci,
348 struct mlxsw_pci_queue *q)
349{
350 wmb(); /* ensure all writes are done before we ring a bell */
351 __mlxsw_pci_queue_doorbell_set(mlxsw_pci, q, q->producer_counter);
352}
353
354static void mlxsw_pci_queue_doorbell_consumer_ring(struct mlxsw_pci *mlxsw_pci,
355 struct mlxsw_pci_queue *q)
356{
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);
360}
361
362static void
363mlxsw_pci_queue_doorbell_arm_consumer_ring(struct mlxsw_pci *mlxsw_pci,
364 struct mlxsw_pci_queue *q)
365{
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);
368}
369
370static dma_addr_t __mlxsw_pci_queue_page_get(struct mlxsw_pci_queue *q,
371 int page_index)
372{
373 return q->mem_item.mapaddr + MLXSW_PCI_PAGE_SIZE * page_index;
374}
375
376static int mlxsw_pci_sdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
377 struct mlxsw_pci_queue *q)
378{
379 int i;
380 int err;
381
382 q->producer_counter = 0;
383 q->consumer_counter = 0;
384
385 /* Set CQ of same number of this SDQ. */
386 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, q->num);
Ido Schimmelf0138e22016-01-05 11:36:40 +0100387 mlxsw_cmd_mbox_sw2hw_dq_sdq_tclass_set(mbox, 3);
Jiri Pirkoeda65002015-07-29 23:33:47 +0200388 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);
391
392 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
393 }
394
395 err = mlxsw_cmd_sw2hw_sdq(mlxsw_pci->core, mbox, q->num);
396 if (err)
397 return err;
398 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
399 return 0;
400}
401
402static void mlxsw_pci_sdq_fini(struct mlxsw_pci *mlxsw_pci,
403 struct mlxsw_pci_queue *q)
404{
405 mlxsw_cmd_hw2sw_sdq(mlxsw_pci->core, q->num);
406}
407
408static int mlxsw_pci_sdq_dbg_read(struct seq_file *file, void *data)
409{
410 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
411 struct mlxsw_pci_queue *q;
412 int i;
413 static const char hdr[] =
414 "NUM PROD_COUNT CONS_COUNT COUNT\n";
415
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,
422 q->count);
423 spin_unlock_bh(&q->lock);
424 }
425 return 0;
426}
427
428static int mlxsw_pci_wqe_frag_map(struct mlxsw_pci *mlxsw_pci, char *wqe,
429 int index, char *frag_data, size_t frag_len,
430 int direction)
431{
432 struct pci_dev *pdev = mlxsw_pci->pdev;
433 dma_addr_t mapaddr;
434
435 mapaddr = pci_map_single(pdev, frag_data, frag_len, direction);
436 if (unlikely(pci_dma_mapping_error(pdev, mapaddr))) {
Jiri Pirko6cf9dc82015-10-15 17:43:22 +0200437 dev_err_ratelimited(&pdev->dev, "failed to dma map tx frag\n");
Jiri Pirkoeda65002015-07-29 23:33:47 +0200438 return -EIO;
439 }
440 mlxsw_pci_wqe_address_set(wqe, index, mapaddr);
441 mlxsw_pci_wqe_byte_count_set(wqe, index, frag_len);
442 return 0;
443}
444
445static void mlxsw_pci_wqe_frag_unmap(struct mlxsw_pci *mlxsw_pci, char *wqe,
446 int index, int direction)
447{
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);
451
452 if (!frag_len)
453 return;
454 pci_unmap_single(pdev, mapaddr, frag_len, direction);
455}
456
457static int mlxsw_pci_rdq_skb_alloc(struct mlxsw_pci *mlxsw_pci,
458 struct mlxsw_pci_queue_elem_info *elem_info)
459{
460 size_t buf_len = MLXSW_PORT_MAX_MTU;
461 char *wqe = elem_info->elem;
462 struct sk_buff *skb;
463 int err;
464
465 elem_info->u.rdq.skb = NULL;
466 skb = netdev_alloc_skb_ip_align(NULL, buf_len);
467 if (!skb)
468 return -ENOMEM;
469
470 /* Assume that wqe was previously zeroed. */
471
472 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
473 buf_len, DMA_FROM_DEVICE);
474 if (err)
475 goto err_frag_map;
476
477 elem_info->u.rdq.skb = skb;
478 return 0;
479
480err_frag_map:
481 dev_kfree_skb_any(skb);
482 return err;
483}
484
485static void mlxsw_pci_rdq_skb_free(struct mlxsw_pci *mlxsw_pci,
486 struct mlxsw_pci_queue_elem_info *elem_info)
487{
488 struct sk_buff *skb;
489 char *wqe;
490
491 skb = elem_info->u.rdq.skb;
492 wqe = elem_info->elem;
493
494 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
495 dev_kfree_skb_any(skb);
496}
497
498static int mlxsw_pci_rdq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
499 struct mlxsw_pci_queue *q)
500{
501 struct mlxsw_pci_queue_elem_info *elem_info;
Jiri Pirko424e1112015-10-15 17:43:18 +0200502 u8 sdq_count = mlxsw_pci_sdq_count(mlxsw_pci);
Jiri Pirkoeda65002015-07-29 23:33:47 +0200503 int i;
504 int err;
505
506 q->producer_counter = 0;
507 q->consumer_counter = 0;
508
509 /* Set CQ of same number of this RDQ with base
Jiri Pirko424e1112015-10-15 17:43:18 +0200510 * above SDQ count as the lower ones are assigned to SDQs.
Jiri Pirkoeda65002015-07-29 23:33:47 +0200511 */
Jiri Pirko424e1112015-10-15 17:43:18 +0200512 mlxsw_cmd_mbox_sw2hw_dq_cq_set(mbox, sdq_count + q->num);
Jiri Pirkoeda65002015-07-29 23:33:47 +0200513 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);
516
517 mlxsw_cmd_mbox_sw2hw_dq_pa_set(mbox, i, mapaddr);
518 }
519
520 err = mlxsw_cmd_sw2hw_rdq(mlxsw_pci->core, mbox, q->num);
521 if (err)
522 return err;
523
524 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
525
526 for (i = 0; i < q->count; i++) {
527 elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
528 BUG_ON(!elem_info);
529 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
530 if (err)
531 goto rollback;
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);
535 }
536
537 return 0;
538
539rollback:
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);
543 }
544 mlxsw_cmd_hw2sw_rdq(mlxsw_pci->core, q->num);
545
546 return err;
547}
548
549static void mlxsw_pci_rdq_fini(struct mlxsw_pci *mlxsw_pci,
550 struct mlxsw_pci_queue *q)
551{
552 struct mlxsw_pci_queue_elem_info *elem_info;
553 int i;
554
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);
559 }
560}
561
562static int mlxsw_pci_rdq_dbg_read(struct seq_file *file, void *data)
563{
564 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
565 struct mlxsw_pci_queue *q;
566 int i;
567 static const char hdr[] =
568 "NUM PROD_COUNT CONS_COUNT COUNT\n";
569
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,
576 q->count);
577 spin_unlock_bh(&q->lock);
578 }
579 return 0;
580}
581
582static int mlxsw_pci_cq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
583 struct mlxsw_pci_queue *q)
584{
585 int i;
586 int err;
587
588 q->consumer_counter = 0;
589
590 for (i = 0; i < q->count; i++) {
591 char *elem = mlxsw_pci_queue_elem_get(q, i);
592
593 mlxsw_pci_cqe_owner_set(elem, 1);
594 }
595
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);
603
604 mlxsw_cmd_mbox_sw2hw_cq_pa_set(mbox, i, mapaddr);
605 }
606 err = mlxsw_cmd_sw2hw_cq(mlxsw_pci->core, mbox, q->num);
607 if (err)
608 return err;
609 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
610 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
611 return 0;
612}
613
614static void mlxsw_pci_cq_fini(struct mlxsw_pci *mlxsw_pci,
615 struct mlxsw_pci_queue *q)
616{
617 mlxsw_cmd_hw2sw_cq(mlxsw_pci->core, q->num);
618}
619
620static int mlxsw_pci_cq_dbg_read(struct seq_file *file, void *data)
621{
622 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
623
624 struct mlxsw_pci_queue *q;
625 int i;
626 static const char hdr[] =
627 "NUM CONS_INDEX SDQ_COUNT RDQ_COUNT COUNT\n";
628
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);
637 }
638 return 0;
639}
640
641static void mlxsw_pci_cqe_sdq_handle(struct mlxsw_pci *mlxsw_pci,
642 struct mlxsw_pci_queue *q,
643 u16 consumer_counter_limit,
644 char *cqe)
645{
646 struct pci_dev *pdev = mlxsw_pci->pdev;
647 struct mlxsw_pci_queue_elem_info *elem_info;
648 char *wqe;
649 struct sk_buff *skb;
650 int i;
651
652 spin_lock(&q->lock);
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;
660
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);
664}
665
666static void mlxsw_pci_cqe_rdq_handle(struct mlxsw_pci *mlxsw_pci,
667 struct mlxsw_pci_queue *q,
668 u16 consumer_counter_limit,
669 char *cqe)
670{
671 struct pci_dev *pdev = mlxsw_pci->pdev;
672 struct mlxsw_pci_queue_elem_info *elem_info;
673 char *wqe;
674 struct sk_buff *skb;
675 struct mlxsw_rx_info rx_info;
Jiri Pirko7b7b9cf2015-08-06 16:41:55 +0200676 u16 byte_count;
Jiri Pirkoeda65002015-07-29 23:33:47 +0200677 int err;
678
679 elem_info = mlxsw_pci_queue_elem_info_consumer_get(q);
680 skb = elem_info->u.sdq.skb;
681 if (!skb)
682 return;
683 wqe = elem_info->elem;
684 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, 0, DMA_FROM_DEVICE);
685
686 if (q->consumer_counter++ != consumer_counter_limit)
687 dev_dbg_ratelimited(&pdev->dev, "Consumer counter does not match limit in RDQ\n");
688
Jiri Pirkod2292e82015-12-03 12:12:24 +0100689 if (mlxsw_pci_cqe_lag_get(cqe)) {
690 rx_info.is_lag = true;
691 rx_info.u.lag_id = mlxsw_pci_cqe_lag_id_get(cqe);
692 rx_info.lag_port_index = mlxsw_pci_cqe_lag_port_index_get(cqe);
693 } else {
694 rx_info.is_lag = false;
695 rx_info.u.sys_port = mlxsw_pci_cqe_system_port_get(cqe);
696 }
Jiri Pirko80606462015-12-03 12:12:23 +0100697
Jiri Pirkoeda65002015-07-29 23:33:47 +0200698 rx_info.trap_id = mlxsw_pci_cqe_trap_id_get(cqe);
699
Jiri Pirko7b7b9cf2015-08-06 16:41:55 +0200700 byte_count = mlxsw_pci_cqe_byte_count_get(cqe);
701 if (mlxsw_pci_cqe_crc_get(cqe))
702 byte_count -= ETH_FCS_LEN;
703 skb_put(skb, byte_count);
Jiri Pirkoeda65002015-07-29 23:33:47 +0200704 mlxsw_core_skb_receive(mlxsw_pci->core, skb, &rx_info);
705
Jiri Pirkoeda65002015-07-29 23:33:47 +0200706 memset(wqe, 0, q->elem_size);
707 err = mlxsw_pci_rdq_skb_alloc(mlxsw_pci, elem_info);
Jiri Pirko6cf9dc82015-10-15 17:43:22 +0200708 if (err)
709 dev_dbg_ratelimited(&pdev->dev, "Failed to alloc skb for RDQ\n");
Jiri Pirkoeda65002015-07-29 23:33:47 +0200710 /* Everything is set up, ring doorbell to pass elem to HW */
711 q->producer_counter++;
712 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
713 return;
Jiri Pirkoeda65002015-07-29 23:33:47 +0200714}
715
716static char *mlxsw_pci_cq_sw_cqe_get(struct mlxsw_pci_queue *q)
717{
718 return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_cqe_owner_get);
719}
720
721static void mlxsw_pci_cq_tasklet(unsigned long data)
722{
723 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
724 struct mlxsw_pci *mlxsw_pci = q->pci;
725 char *cqe;
726 int items = 0;
727 int credits = q->count >> 1;
728
729 while ((cqe = mlxsw_pci_cq_sw_cqe_get(q))) {
730 u16 wqe_counter = mlxsw_pci_cqe_wqe_counter_get(cqe);
731 u8 sendq = mlxsw_pci_cqe_sr_get(cqe);
732 u8 dqn = mlxsw_pci_cqe_dqn_get(cqe);
733
734 if (sendq) {
735 struct mlxsw_pci_queue *sdq;
736
737 sdq = mlxsw_pci_sdq_get(mlxsw_pci, dqn);
738 mlxsw_pci_cqe_sdq_handle(mlxsw_pci, sdq,
739 wqe_counter, cqe);
740 q->u.cq.comp_sdq_count++;
741 } else {
742 struct mlxsw_pci_queue *rdq;
743
744 rdq = mlxsw_pci_rdq_get(mlxsw_pci, dqn);
745 mlxsw_pci_cqe_rdq_handle(mlxsw_pci, rdq,
746 wqe_counter, cqe);
747 q->u.cq.comp_rdq_count++;
748 }
749 if (++items == credits)
750 break;
751 }
752 if (items) {
753 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
754 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
755 }
756}
757
758static int mlxsw_pci_eq_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
759 struct mlxsw_pci_queue *q)
760{
761 int i;
762 int err;
763
764 q->consumer_counter = 0;
765
766 for (i = 0; i < q->count; i++) {
767 char *elem = mlxsw_pci_queue_elem_get(q, i);
768
769 mlxsw_pci_eqe_owner_set(elem, 1);
770 }
771
772 mlxsw_cmd_mbox_sw2hw_eq_int_msix_set(mbox, 1); /* MSI-X used */
773 mlxsw_cmd_mbox_sw2hw_eq_oi_set(mbox, 0);
774 mlxsw_cmd_mbox_sw2hw_eq_st_set(mbox, 1); /* armed */
775 mlxsw_cmd_mbox_sw2hw_eq_log_eq_size_set(mbox, ilog2(q->count));
776 for (i = 0; i < MLXSW_PCI_AQ_PAGES; i++) {
777 dma_addr_t mapaddr = __mlxsw_pci_queue_page_get(q, i);
778
779 mlxsw_cmd_mbox_sw2hw_eq_pa_set(mbox, i, mapaddr);
780 }
781 err = mlxsw_cmd_sw2hw_eq(mlxsw_pci->core, mbox, q->num);
782 if (err)
783 return err;
784 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
785 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
786 return 0;
787}
788
789static void mlxsw_pci_eq_fini(struct mlxsw_pci *mlxsw_pci,
790 struct mlxsw_pci_queue *q)
791{
792 mlxsw_cmd_hw2sw_eq(mlxsw_pci->core, q->num);
793}
794
795static int mlxsw_pci_eq_dbg_read(struct seq_file *file, void *data)
796{
797 struct mlxsw_pci *mlxsw_pci = dev_get_drvdata(file->private);
798 struct mlxsw_pci_queue *q;
799 int i;
800 static const char hdr[] =
801 "NUM CONS_COUNT EV_CMD EV_COMP EV_OTHER COUNT\n";
802
803 seq_printf(file, hdr);
804 for (i = 0; i < mlxsw_pci_eq_count(mlxsw_pci); i++) {
805 q = mlxsw_pci_eq_get(mlxsw_pci, i);
806 spin_lock_bh(&q->lock);
807 seq_printf(file, "%3d %10d %10d %10d %10d %5d\n",
808 i, q->consumer_counter, q->u.eq.ev_cmd_count,
809 q->u.eq.ev_comp_count, q->u.eq.ev_other_count,
810 q->count);
811 spin_unlock_bh(&q->lock);
812 }
813 return 0;
814}
815
816static void mlxsw_pci_eq_cmd_event(struct mlxsw_pci *mlxsw_pci, char *eqe)
817{
818 mlxsw_pci->cmd.comp.status = mlxsw_pci_eqe_cmd_status_get(eqe);
819 mlxsw_pci->cmd.comp.out_param =
820 ((u64) mlxsw_pci_eqe_cmd_out_param_h_get(eqe)) << 32 |
821 mlxsw_pci_eqe_cmd_out_param_l_get(eqe);
822 mlxsw_pci->cmd.wait_done = true;
823 wake_up(&mlxsw_pci->cmd.wait);
824}
825
826static char *mlxsw_pci_eq_sw_eqe_get(struct mlxsw_pci_queue *q)
827{
828 return mlxsw_pci_queue_sw_elem_get(q, mlxsw_pci_eqe_owner_get);
829}
830
831static void mlxsw_pci_eq_tasklet(unsigned long data)
832{
833 struct mlxsw_pci_queue *q = (struct mlxsw_pci_queue *) data;
834 struct mlxsw_pci *mlxsw_pci = q->pci;
Jiri Pirkoe4c870b2015-10-15 17:43:17 +0200835 u8 cq_count = mlxsw_pci_cq_count(mlxsw_pci);
836 unsigned long active_cqns[BITS_TO_LONGS(MLXSW_PCI_CQS_MAX)];
Jiri Pirkoeda65002015-07-29 23:33:47 +0200837 char *eqe;
838 u8 cqn;
839 bool cq_handle = false;
840 int items = 0;
841 int credits = q->count >> 1;
842
843 memset(&active_cqns, 0, sizeof(active_cqns));
844
845 while ((eqe = mlxsw_pci_eq_sw_eqe_get(q))) {
846 u8 event_type = mlxsw_pci_eqe_event_type_get(eqe);
847
848 switch (event_type) {
849 case MLXSW_PCI_EQE_EVENT_TYPE_CMD:
850 mlxsw_pci_eq_cmd_event(mlxsw_pci, eqe);
851 q->u.eq.ev_cmd_count++;
852 break;
853 case MLXSW_PCI_EQE_EVENT_TYPE_COMP:
854 cqn = mlxsw_pci_eqe_cqn_get(eqe);
855 set_bit(cqn, active_cqns);
856 cq_handle = true;
857 q->u.eq.ev_comp_count++;
858 break;
859 default:
860 q->u.eq.ev_other_count++;
861 }
862 if (++items == credits)
863 break;
864 }
865 if (items) {
866 mlxsw_pci_queue_doorbell_consumer_ring(mlxsw_pci, q);
867 mlxsw_pci_queue_doorbell_arm_consumer_ring(mlxsw_pci, q);
868 }
869
870 if (!cq_handle)
871 return;
Jiri Pirkoe4c870b2015-10-15 17:43:17 +0200872 for_each_set_bit(cqn, active_cqns, cq_count) {
Jiri Pirkoeda65002015-07-29 23:33:47 +0200873 q = mlxsw_pci_cq_get(mlxsw_pci, cqn);
874 mlxsw_pci_queue_tasklet_schedule(q);
875 }
876}
877
878struct mlxsw_pci_queue_ops {
879 const char *name;
880 enum mlxsw_pci_queue_type type;
881 int (*init)(struct mlxsw_pci *mlxsw_pci, char *mbox,
882 struct mlxsw_pci_queue *q);
883 void (*fini)(struct mlxsw_pci *mlxsw_pci,
884 struct mlxsw_pci_queue *q);
885 void (*tasklet)(unsigned long data);
886 int (*dbg_read)(struct seq_file *s, void *data);
887 u16 elem_count;
888 u8 elem_size;
889};
890
891static const struct mlxsw_pci_queue_ops mlxsw_pci_sdq_ops = {
892 .type = MLXSW_PCI_QUEUE_TYPE_SDQ,
893 .init = mlxsw_pci_sdq_init,
894 .fini = mlxsw_pci_sdq_fini,
895 .dbg_read = mlxsw_pci_sdq_dbg_read,
896 .elem_count = MLXSW_PCI_WQE_COUNT,
897 .elem_size = MLXSW_PCI_WQE_SIZE,
898};
899
900static const struct mlxsw_pci_queue_ops mlxsw_pci_rdq_ops = {
901 .type = MLXSW_PCI_QUEUE_TYPE_RDQ,
902 .init = mlxsw_pci_rdq_init,
903 .fini = mlxsw_pci_rdq_fini,
904 .dbg_read = mlxsw_pci_rdq_dbg_read,
905 .elem_count = MLXSW_PCI_WQE_COUNT,
906 .elem_size = MLXSW_PCI_WQE_SIZE
907};
908
909static const struct mlxsw_pci_queue_ops mlxsw_pci_cq_ops = {
910 .type = MLXSW_PCI_QUEUE_TYPE_CQ,
911 .init = mlxsw_pci_cq_init,
912 .fini = mlxsw_pci_cq_fini,
913 .tasklet = mlxsw_pci_cq_tasklet,
914 .dbg_read = mlxsw_pci_cq_dbg_read,
915 .elem_count = MLXSW_PCI_CQE_COUNT,
916 .elem_size = MLXSW_PCI_CQE_SIZE
917};
918
919static const struct mlxsw_pci_queue_ops mlxsw_pci_eq_ops = {
920 .type = MLXSW_PCI_QUEUE_TYPE_EQ,
921 .init = mlxsw_pci_eq_init,
922 .fini = mlxsw_pci_eq_fini,
923 .tasklet = mlxsw_pci_eq_tasklet,
924 .dbg_read = mlxsw_pci_eq_dbg_read,
925 .elem_count = MLXSW_PCI_EQE_COUNT,
926 .elem_size = MLXSW_PCI_EQE_SIZE
927};
928
929static int mlxsw_pci_queue_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
930 const struct mlxsw_pci_queue_ops *q_ops,
931 struct mlxsw_pci_queue *q, u8 q_num)
932{
933 struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
934 int i;
935 int err;
936
937 spin_lock_init(&q->lock);
938 q->num = q_num;
939 q->count = q_ops->elem_count;
940 q->elem_size = q_ops->elem_size;
941 q->type = q_ops->type;
942 q->pci = mlxsw_pci;
943
944 if (q_ops->tasklet)
945 tasklet_init(&q->tasklet, q_ops->tasklet, (unsigned long) q);
946
947 mem_item->size = MLXSW_PCI_AQ_SIZE;
948 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
949 mem_item->size,
950 &mem_item->mapaddr);
951 if (!mem_item->buf)
952 return -ENOMEM;
953 memset(mem_item->buf, 0, mem_item->size);
954
955 q->elem_info = kcalloc(q->count, sizeof(*q->elem_info), GFP_KERNEL);
956 if (!q->elem_info) {
957 err = -ENOMEM;
958 goto err_elem_info_alloc;
959 }
960
961 /* Initialize dma mapped elements info elem_info for
962 * future easy access.
963 */
964 for (i = 0; i < q->count; i++) {
965 struct mlxsw_pci_queue_elem_info *elem_info;
966
967 elem_info = mlxsw_pci_queue_elem_info_get(q, i);
968 elem_info->elem =
969 __mlxsw_pci_queue_elem_get(q, q_ops->elem_size, i);
970 }
971
972 mlxsw_cmd_mbox_zero(mbox);
973 err = q_ops->init(mlxsw_pci, mbox, q);
974 if (err)
975 goto err_q_ops_init;
976 return 0;
977
978err_q_ops_init:
979 kfree(q->elem_info);
980err_elem_info_alloc:
981 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
982 mem_item->buf, mem_item->mapaddr);
983 return err;
984}
985
986static void mlxsw_pci_queue_fini(struct mlxsw_pci *mlxsw_pci,
987 const struct mlxsw_pci_queue_ops *q_ops,
988 struct mlxsw_pci_queue *q)
989{
990 struct mlxsw_pci_mem_item *mem_item = &q->mem_item;
991
992 q_ops->fini(mlxsw_pci, q);
993 kfree(q->elem_info);
994 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
995 mem_item->buf, mem_item->mapaddr);
996}
997
998static int mlxsw_pci_queue_group_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
999 const struct mlxsw_pci_queue_ops *q_ops,
1000 u8 num_qs)
1001{
1002 struct pci_dev *pdev = mlxsw_pci->pdev;
1003 struct mlxsw_pci_queue_type_group *queue_group;
1004 char tmp[16];
1005 int i;
1006 int err;
1007
1008 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
1009 queue_group->q = kcalloc(num_qs, sizeof(*queue_group->q), GFP_KERNEL);
1010 if (!queue_group->q)
1011 return -ENOMEM;
1012
1013 for (i = 0; i < num_qs; i++) {
1014 err = mlxsw_pci_queue_init(mlxsw_pci, mbox, q_ops,
1015 &queue_group->q[i], i);
1016 if (err)
1017 goto err_queue_init;
1018 }
1019 queue_group->count = num_qs;
1020
1021 sprintf(tmp, "%s_stats", mlxsw_pci_queue_type_str(q_ops->type));
1022 debugfs_create_devm_seqfile(&pdev->dev, tmp, mlxsw_pci->dbg_dir,
1023 q_ops->dbg_read);
1024
1025 return 0;
1026
1027err_queue_init:
1028 for (i--; i >= 0; i--)
1029 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1030 kfree(queue_group->q);
1031 return err;
1032}
1033
1034static void mlxsw_pci_queue_group_fini(struct mlxsw_pci *mlxsw_pci,
1035 const struct mlxsw_pci_queue_ops *q_ops)
1036{
1037 struct mlxsw_pci_queue_type_group *queue_group;
1038 int i;
1039
1040 queue_group = mlxsw_pci_queue_type_group_get(mlxsw_pci, q_ops->type);
1041 for (i = 0; i < queue_group->count; i++)
1042 mlxsw_pci_queue_fini(mlxsw_pci, q_ops, &queue_group->q[i]);
1043 kfree(queue_group->q);
1044}
1045
1046static int mlxsw_pci_aqs_init(struct mlxsw_pci *mlxsw_pci, char *mbox)
1047{
1048 struct pci_dev *pdev = mlxsw_pci->pdev;
1049 u8 num_sdqs;
1050 u8 sdq_log2sz;
1051 u8 num_rdqs;
1052 u8 rdq_log2sz;
1053 u8 num_cqs;
1054 u8 cq_log2sz;
1055 u8 num_eqs;
1056 u8 eq_log2sz;
1057 int err;
1058
1059 mlxsw_cmd_mbox_zero(mbox);
1060 err = mlxsw_cmd_query_aq_cap(mlxsw_pci->core, mbox);
1061 if (err)
1062 return err;
1063
1064 num_sdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_sdqs_get(mbox);
1065 sdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_sdq_sz_get(mbox);
1066 num_rdqs = mlxsw_cmd_mbox_query_aq_cap_max_num_rdqs_get(mbox);
1067 rdq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_rdq_sz_get(mbox);
1068 num_cqs = mlxsw_cmd_mbox_query_aq_cap_max_num_cqs_get(mbox);
1069 cq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_cq_sz_get(mbox);
1070 num_eqs = mlxsw_cmd_mbox_query_aq_cap_max_num_eqs_get(mbox);
1071 eq_log2sz = mlxsw_cmd_mbox_query_aq_cap_log_max_eq_sz_get(mbox);
1072
Jiri Pirkoc85c3882015-10-15 17:43:19 +02001073 if (num_sdqs + num_rdqs > num_cqs ||
Jiri Pirkoe4c870b2015-10-15 17:43:17 +02001074 num_cqs > MLXSW_PCI_CQS_MAX || num_eqs != MLXSW_PCI_EQS_COUNT) {
Jiri Pirkoeda65002015-07-29 23:33:47 +02001075 dev_err(&pdev->dev, "Unsupported number of queues\n");
1076 return -EINVAL;
1077 }
1078
1079 if ((1 << sdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1080 (1 << rdq_log2sz != MLXSW_PCI_WQE_COUNT) ||
1081 (1 << cq_log2sz != MLXSW_PCI_CQE_COUNT) ||
1082 (1 << eq_log2sz != MLXSW_PCI_EQE_COUNT)) {
1083 dev_err(&pdev->dev, "Unsupported number of async queue descriptors\n");
1084 return -EINVAL;
1085 }
1086
1087 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_eq_ops,
1088 num_eqs);
1089 if (err) {
1090 dev_err(&pdev->dev, "Failed to initialize event queues\n");
1091 return err;
1092 }
1093
1094 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_cq_ops,
1095 num_cqs);
1096 if (err) {
1097 dev_err(&pdev->dev, "Failed to initialize completion queues\n");
1098 goto err_cqs_init;
1099 }
1100
1101 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_sdq_ops,
1102 num_sdqs);
1103 if (err) {
1104 dev_err(&pdev->dev, "Failed to initialize send descriptor queues\n");
1105 goto err_sdqs_init;
1106 }
1107
1108 err = mlxsw_pci_queue_group_init(mlxsw_pci, mbox, &mlxsw_pci_rdq_ops,
1109 num_rdqs);
1110 if (err) {
1111 dev_err(&pdev->dev, "Failed to initialize receive descriptor queues\n");
1112 goto err_rdqs_init;
1113 }
1114
1115 /* We have to poll in command interface until queues are initialized */
1116 mlxsw_pci->cmd.nopoll = true;
1117 return 0;
1118
1119err_rdqs_init:
1120 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1121err_sdqs_init:
1122 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1123err_cqs_init:
1124 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1125 return err;
1126}
1127
1128static void mlxsw_pci_aqs_fini(struct mlxsw_pci *mlxsw_pci)
1129{
1130 mlxsw_pci->cmd.nopoll = false;
1131 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_rdq_ops);
1132 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_sdq_ops);
1133 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_cq_ops);
1134 mlxsw_pci_queue_group_fini(mlxsw_pci, &mlxsw_pci_eq_ops);
1135}
1136
1137static void
1138mlxsw_pci_config_profile_swid_config(struct mlxsw_pci *mlxsw_pci,
1139 char *mbox, int index,
1140 const struct mlxsw_swid_config *swid)
1141{
1142 u8 mask = 0;
1143
1144 if (swid->used_type) {
1145 mlxsw_cmd_mbox_config_profile_swid_config_type_set(
1146 mbox, index, swid->type);
1147 mask |= 1;
1148 }
1149 if (swid->used_properties) {
1150 mlxsw_cmd_mbox_config_profile_swid_config_properties_set(
1151 mbox, index, swid->properties);
1152 mask |= 2;
1153 }
1154 mlxsw_cmd_mbox_config_profile_swid_config_mask_set(mbox, index, mask);
1155}
1156
Nogah Frankel57d316b2016-07-21 12:03:09 +02001157#define MLXSW_RESOURCES_TABLE_END_ID 0xffff
Nogah Frankelded821c2016-07-21 12:03:10 +02001158#define MLXSW_MAX_SPAN_ID 0x2420
Nogah Frankel9f7f7972016-09-20 11:16:49 +02001159#define MLXSW_MAX_LAG_ID 0x2520
1160#define MLXSW_MAX_PORTS_IN_LAG_ID 0x2521
Nogah Frankel2acd10c2016-09-20 11:16:51 +02001161#define MLXSW_KVD_SIZE_ID 0x1001
1162#define MLXSW_KVD_SINGLE_MIN_SIZE_ID 0x1002
1163#define MLXSW_KVD_DOUBLE_MIN_SIZE_ID 0x1003
Nogah Frankel57d316b2016-07-21 12:03:09 +02001164#define MLXSW_RESOURCES_QUERY_MAX_QUERIES 100
1165#define MLXSW_RESOURCES_PER_QUERY 32
1166
1167static void mlxsw_pci_resources_query_parse(int id, u64 val,
1168 struct mlxsw_resources *resources)
1169{
1170 switch (id) {
Nogah Frankelded821c2016-07-21 12:03:10 +02001171 case MLXSW_MAX_SPAN_ID:
1172 resources->max_span = val;
1173 resources->max_span_valid = 1;
1174 break;
Nogah Frankel9f7f7972016-09-20 11:16:49 +02001175 case MLXSW_MAX_LAG_ID:
1176 resources->max_lag = val;
1177 resources->max_lag_valid = 1;
1178 break;
1179 case MLXSW_MAX_PORTS_IN_LAG_ID:
1180 resources->max_ports_in_lag = val;
1181 resources->max_ports_in_lag_valid = 1;
1182 break;
Nogah Frankel2acd10c2016-09-20 11:16:51 +02001183 case MLXSW_KVD_SIZE_ID:
1184 resources->kvd_size = val;
1185 resources->kvd_size_valid = 1;
1186 break;
1187 case MLXSW_KVD_SINGLE_MIN_SIZE_ID:
1188 resources->kvd_single_min_size = val;
1189 resources->kvd_single_min_size_valid = 1;
1190 break;
1191 case MLXSW_KVD_DOUBLE_MIN_SIZE_ID:
1192 resources->kvd_double_min_size = val;
1193 resources->kvd_double_min_size_valid = 1;
1194 break;
Nogah Frankel57d316b2016-07-21 12:03:09 +02001195 default:
1196 break;
1197 }
1198}
1199
1200static int mlxsw_pci_resources_query(struct mlxsw_pci *mlxsw_pci, char *mbox,
1201 struct mlxsw_resources *resources,
1202 u8 query_enabled)
1203{
1204 int index, i;
1205 u64 data;
1206 u16 id;
1207 int err;
1208
1209 /* Not all the versions support resources query */
1210 if (!query_enabled)
1211 return 0;
1212
1213 mlxsw_cmd_mbox_zero(mbox);
1214
1215 for (index = 0; index < MLXSW_RESOURCES_QUERY_MAX_QUERIES; index++) {
1216 err = mlxsw_cmd_query_resources(mlxsw_pci->core, mbox, index);
1217 if (err)
1218 return err;
1219
1220 for (i = 0; i < MLXSW_RESOURCES_PER_QUERY; i++) {
1221 id = mlxsw_cmd_mbox_query_resource_id_get(mbox, i);
1222 data = mlxsw_cmd_mbox_query_resource_data_get(mbox, i);
1223
1224 if (id == MLXSW_RESOURCES_TABLE_END_ID)
1225 return 0;
1226
1227 mlxsw_pci_resources_query_parse(id, data, resources);
1228 }
1229 }
1230
1231 /* If after MLXSW_RESOURCES_QUERY_MAX_QUERIES we still didn't get
1232 * MLXSW_RESOURCES_TABLE_END_ID, something went bad in the FW.
1233 */
1234 return -EIO;
1235}
1236
Jiri Pirkoeda65002015-07-29 23:33:47 +02001237static int mlxsw_pci_config_profile(struct mlxsw_pci *mlxsw_pci, char *mbox,
1238 const struct mlxsw_config_profile *profile)
1239{
1240 int i;
1241
1242 mlxsw_cmd_mbox_zero(mbox);
1243
1244 if (profile->used_max_vepa_channels) {
1245 mlxsw_cmd_mbox_config_profile_set_max_vepa_channels_set(
1246 mbox, 1);
1247 mlxsw_cmd_mbox_config_profile_max_vepa_channels_set(
1248 mbox, profile->max_vepa_channels);
1249 }
Jiri Pirkoeda65002015-07-29 23:33:47 +02001250 if (profile->used_max_mid) {
1251 mlxsw_cmd_mbox_config_profile_set_max_mid_set(
1252 mbox, 1);
1253 mlxsw_cmd_mbox_config_profile_max_mid_set(
1254 mbox, profile->max_mid);
1255 }
1256 if (profile->used_max_pgt) {
1257 mlxsw_cmd_mbox_config_profile_set_max_pgt_set(
1258 mbox, 1);
1259 mlxsw_cmd_mbox_config_profile_max_pgt_set(
1260 mbox, profile->max_pgt);
1261 }
1262 if (profile->used_max_system_port) {
1263 mlxsw_cmd_mbox_config_profile_set_max_system_port_set(
1264 mbox, 1);
1265 mlxsw_cmd_mbox_config_profile_max_system_port_set(
1266 mbox, profile->max_system_port);
1267 }
1268 if (profile->used_max_vlan_groups) {
1269 mlxsw_cmd_mbox_config_profile_set_max_vlan_groups_set(
1270 mbox, 1);
1271 mlxsw_cmd_mbox_config_profile_max_vlan_groups_set(
1272 mbox, profile->max_vlan_groups);
1273 }
1274 if (profile->used_max_regions) {
1275 mlxsw_cmd_mbox_config_profile_set_max_regions_set(
1276 mbox, 1);
1277 mlxsw_cmd_mbox_config_profile_max_regions_set(
1278 mbox, profile->max_regions);
1279 }
1280 if (profile->used_flood_tables) {
1281 mlxsw_cmd_mbox_config_profile_set_flood_tables_set(
1282 mbox, 1);
1283 mlxsw_cmd_mbox_config_profile_max_flood_tables_set(
1284 mbox, profile->max_flood_tables);
1285 mlxsw_cmd_mbox_config_profile_max_vid_flood_tables_set(
1286 mbox, profile->max_vid_flood_tables);
Ido Schimmel12fd35a2015-10-16 14:01:25 +02001287 mlxsw_cmd_mbox_config_profile_max_fid_offset_flood_tables_set(
1288 mbox, profile->max_fid_offset_flood_tables);
1289 mlxsw_cmd_mbox_config_profile_fid_offset_flood_table_size_set(
1290 mbox, profile->fid_offset_flood_table_size);
Ido Schimmel453b6a82015-10-16 14:01:24 +02001291 mlxsw_cmd_mbox_config_profile_max_fid_flood_tables_set(
1292 mbox, profile->max_fid_flood_tables);
1293 mlxsw_cmd_mbox_config_profile_fid_flood_table_size_set(
1294 mbox, profile->fid_flood_table_size);
Jiri Pirkoeda65002015-07-29 23:33:47 +02001295 }
1296 if (profile->used_flood_mode) {
1297 mlxsw_cmd_mbox_config_profile_set_flood_mode_set(
1298 mbox, 1);
1299 mlxsw_cmd_mbox_config_profile_flood_mode_set(
1300 mbox, profile->flood_mode);
1301 }
1302 if (profile->used_max_ib_mc) {
1303 mlxsw_cmd_mbox_config_profile_set_max_ib_mc_set(
1304 mbox, 1);
1305 mlxsw_cmd_mbox_config_profile_max_ib_mc_set(
1306 mbox, profile->max_ib_mc);
1307 }
1308 if (profile->used_max_pkey) {
1309 mlxsw_cmd_mbox_config_profile_set_max_pkey_set(
1310 mbox, 1);
1311 mlxsw_cmd_mbox_config_profile_max_pkey_set(
1312 mbox, profile->max_pkey);
1313 }
1314 if (profile->used_ar_sec) {
1315 mlxsw_cmd_mbox_config_profile_set_ar_sec_set(
1316 mbox, 1);
1317 mlxsw_cmd_mbox_config_profile_ar_sec_set(
1318 mbox, profile->ar_sec);
1319 }
1320 if (profile->used_adaptive_routing_group_cap) {
1321 mlxsw_cmd_mbox_config_profile_set_adaptive_routing_group_cap_set(
1322 mbox, 1);
1323 mlxsw_cmd_mbox_config_profile_adaptive_routing_group_cap_set(
1324 mbox, profile->adaptive_routing_group_cap);
1325 }
Jiri Pirko489107b2016-07-05 11:27:45 +02001326 if (profile->used_kvd_sizes) {
1327 mlxsw_cmd_mbox_config_profile_set_kvd_linear_size_set(
1328 mbox, 1);
1329 mlxsw_cmd_mbox_config_profile_kvd_linear_size_set(
1330 mbox, profile->kvd_linear_size);
1331 mlxsw_cmd_mbox_config_profile_set_kvd_hash_single_size_set(
1332 mbox, 1);
1333 mlxsw_cmd_mbox_config_profile_kvd_hash_single_size_set(
1334 mbox, profile->kvd_hash_single_size);
1335 mlxsw_cmd_mbox_config_profile_set_kvd_hash_double_size_set(
1336 mbox, 1);
1337 mlxsw_cmd_mbox_config_profile_kvd_hash_double_size_set(
1338 mbox, profile->kvd_hash_double_size);
1339 }
Jiri Pirkoeda65002015-07-29 23:33:47 +02001340
1341 for (i = 0; i < MLXSW_CONFIG_PROFILE_SWID_COUNT; i++)
1342 mlxsw_pci_config_profile_swid_config(mlxsw_pci, mbox, i,
1343 &profile->swid_config[i]);
1344
1345 return mlxsw_cmd_config_profile_set(mlxsw_pci->core, mbox);
1346}
1347
1348static int mlxsw_pci_boardinfo(struct mlxsw_pci *mlxsw_pci, char *mbox)
1349{
1350 struct mlxsw_bus_info *bus_info = &mlxsw_pci->bus_info;
1351 int err;
1352
1353 mlxsw_cmd_mbox_zero(mbox);
1354 err = mlxsw_cmd_boardinfo(mlxsw_pci->core, mbox);
1355 if (err)
1356 return err;
1357 mlxsw_cmd_mbox_boardinfo_vsd_memcpy_from(mbox, bus_info->vsd);
1358 mlxsw_cmd_mbox_boardinfo_psid_memcpy_from(mbox, bus_info->psid);
1359 return 0;
1360}
1361
1362static int mlxsw_pci_fw_area_init(struct mlxsw_pci *mlxsw_pci, char *mbox,
1363 u16 num_pages)
1364{
1365 struct mlxsw_pci_mem_item *mem_item;
Jiri Pirko3e2206d2015-10-15 17:43:20 +02001366 int nent = 0;
Jiri Pirkoeda65002015-07-29 23:33:47 +02001367 int i;
1368 int err;
1369
1370 mlxsw_pci->fw_area.items = kcalloc(num_pages, sizeof(*mem_item),
1371 GFP_KERNEL);
1372 if (!mlxsw_pci->fw_area.items)
1373 return -ENOMEM;
Jiri Pirko3e2206d2015-10-15 17:43:20 +02001374 mlxsw_pci->fw_area.count = num_pages;
Jiri Pirkoeda65002015-07-29 23:33:47 +02001375
1376 mlxsw_cmd_mbox_zero(mbox);
1377 for (i = 0; i < num_pages; i++) {
1378 mem_item = &mlxsw_pci->fw_area.items[i];
1379
1380 mem_item->size = MLXSW_PCI_PAGE_SIZE;
1381 mem_item->buf = pci_alloc_consistent(mlxsw_pci->pdev,
1382 mem_item->size,
1383 &mem_item->mapaddr);
1384 if (!mem_item->buf) {
1385 err = -ENOMEM;
1386 goto err_alloc;
1387 }
Jiri Pirko3e2206d2015-10-15 17:43:20 +02001388 mlxsw_cmd_mbox_map_fa_pa_set(mbox, nent, mem_item->mapaddr);
1389 mlxsw_cmd_mbox_map_fa_log2size_set(mbox, nent, 0); /* 1 page */
1390 if (++nent == MLXSW_CMD_MAP_FA_VPM_ENTRIES_MAX) {
1391 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1392 if (err)
1393 goto err_cmd_map_fa;
1394 nent = 0;
1395 mlxsw_cmd_mbox_zero(mbox);
1396 }
Jiri Pirkoeda65002015-07-29 23:33:47 +02001397 }
1398
Jiri Pirko3e2206d2015-10-15 17:43:20 +02001399 if (nent) {
1400 err = mlxsw_cmd_map_fa(mlxsw_pci->core, mbox, nent);
1401 if (err)
1402 goto err_cmd_map_fa;
1403 }
Jiri Pirkoeda65002015-07-29 23:33:47 +02001404
1405 return 0;
1406
1407err_cmd_map_fa:
1408err_alloc:
1409 for (i--; i >= 0; i--) {
1410 mem_item = &mlxsw_pci->fw_area.items[i];
1411
1412 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1413 mem_item->buf, mem_item->mapaddr);
1414 }
1415 kfree(mlxsw_pci->fw_area.items);
1416 return err;
1417}
1418
1419static void mlxsw_pci_fw_area_fini(struct mlxsw_pci *mlxsw_pci)
1420{
1421 struct mlxsw_pci_mem_item *mem_item;
1422 int i;
1423
1424 mlxsw_cmd_unmap_fa(mlxsw_pci->core);
1425
Jiri Pirko3e2206d2015-10-15 17:43:20 +02001426 for (i = 0; i < mlxsw_pci->fw_area.count; i++) {
Jiri Pirkoeda65002015-07-29 23:33:47 +02001427 mem_item = &mlxsw_pci->fw_area.items[i];
1428
1429 pci_free_consistent(mlxsw_pci->pdev, mem_item->size,
1430 mem_item->buf, mem_item->mapaddr);
1431 }
1432 kfree(mlxsw_pci->fw_area.items);
1433}
1434
1435static irqreturn_t mlxsw_pci_eq_irq_handler(int irq, void *dev_id)
1436{
1437 struct mlxsw_pci *mlxsw_pci = dev_id;
1438 struct mlxsw_pci_queue *q;
1439 int i;
1440
1441 for (i = 0; i < MLXSW_PCI_EQS_COUNT; i++) {
1442 q = mlxsw_pci_eq_get(mlxsw_pci, i);
1443 mlxsw_pci_queue_tasklet_schedule(q);
1444 }
1445 return IRQ_HANDLED;
1446}
1447
Ido Schimmel1e817792015-08-27 17:59:57 +02001448static int mlxsw_pci_mbox_alloc(struct mlxsw_pci *mlxsw_pci,
1449 struct mlxsw_pci_mem_item *mbox)
1450{
1451 struct pci_dev *pdev = mlxsw_pci->pdev;
1452 int err = 0;
1453
1454 mbox->size = MLXSW_CMD_MBOX_SIZE;
1455 mbox->buf = pci_alloc_consistent(pdev, MLXSW_CMD_MBOX_SIZE,
1456 &mbox->mapaddr);
1457 if (!mbox->buf) {
1458 dev_err(&pdev->dev, "Failed allocating memory for mailbox\n");
1459 err = -ENOMEM;
1460 }
1461
1462 return err;
1463}
1464
1465static void mlxsw_pci_mbox_free(struct mlxsw_pci *mlxsw_pci,
1466 struct mlxsw_pci_mem_item *mbox)
1467{
1468 struct pci_dev *pdev = mlxsw_pci->pdev;
1469
1470 pci_free_consistent(pdev, MLXSW_CMD_MBOX_SIZE, mbox->buf,
1471 mbox->mapaddr);
1472}
1473
Jiri Pirkoeda65002015-07-29 23:33:47 +02001474static int mlxsw_pci_init(void *bus_priv, struct mlxsw_core *mlxsw_core,
Nogah Frankel57d316b2016-07-21 12:03:09 +02001475 const struct mlxsw_config_profile *profile,
1476 struct mlxsw_resources *resources)
Jiri Pirkoeda65002015-07-29 23:33:47 +02001477{
1478 struct mlxsw_pci *mlxsw_pci = bus_priv;
1479 struct pci_dev *pdev = mlxsw_pci->pdev;
1480 char *mbox;
1481 u16 num_pages;
1482 int err;
1483
1484 mutex_init(&mlxsw_pci->cmd.lock);
1485 init_waitqueue_head(&mlxsw_pci->cmd.wait);
1486
1487 mlxsw_pci->core = mlxsw_core;
1488
1489 mbox = mlxsw_cmd_mbox_alloc();
1490 if (!mbox)
1491 return -ENOMEM;
Ido Schimmel1e817792015-08-27 17:59:57 +02001492
1493 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
1494 if (err)
1495 goto mbox_put;
1496
1497 err = mlxsw_pci_mbox_alloc(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1498 if (err)
1499 goto err_out_mbox_alloc;
1500
Jiri Pirkoeda65002015-07-29 23:33:47 +02001501 err = mlxsw_cmd_query_fw(mlxsw_core, mbox);
1502 if (err)
1503 goto err_query_fw;
1504
1505 mlxsw_pci->bus_info.fw_rev.major =
1506 mlxsw_cmd_mbox_query_fw_fw_rev_major_get(mbox);
1507 mlxsw_pci->bus_info.fw_rev.minor =
1508 mlxsw_cmd_mbox_query_fw_fw_rev_minor_get(mbox);
1509 mlxsw_pci->bus_info.fw_rev.subminor =
1510 mlxsw_cmd_mbox_query_fw_fw_rev_subminor_get(mbox);
1511
1512 if (mlxsw_cmd_mbox_query_fw_cmd_interface_rev_get(mbox) != 1) {
1513 dev_err(&pdev->dev, "Unsupported cmd interface revision ID queried from hw\n");
1514 err = -EINVAL;
1515 goto err_iface_rev;
1516 }
1517 if (mlxsw_cmd_mbox_query_fw_doorbell_page_bar_get(mbox) != 0) {
1518 dev_err(&pdev->dev, "Unsupported doorbell page bar queried from hw\n");
1519 err = -EINVAL;
1520 goto err_doorbell_page_bar;
1521 }
1522
1523 mlxsw_pci->doorbell_offset =
1524 mlxsw_cmd_mbox_query_fw_doorbell_page_offset_get(mbox);
1525
1526 num_pages = mlxsw_cmd_mbox_query_fw_fw_pages_get(mbox);
1527 err = mlxsw_pci_fw_area_init(mlxsw_pci, mbox, num_pages);
1528 if (err)
1529 goto err_fw_area_init;
1530
1531 err = mlxsw_pci_boardinfo(mlxsw_pci, mbox);
1532 if (err)
1533 goto err_boardinfo;
1534
Nogah Frankel57d316b2016-07-21 12:03:09 +02001535 err = mlxsw_pci_resources_query(mlxsw_pci, mbox, resources,
1536 profile->resource_query_enable);
1537 if (err)
1538 goto err_query_resources;
1539
Jiri Pirkoeda65002015-07-29 23:33:47 +02001540 err = mlxsw_pci_config_profile(mlxsw_pci, mbox, profile);
1541 if (err)
1542 goto err_config_profile;
1543
1544 err = mlxsw_pci_aqs_init(mlxsw_pci, mbox);
1545 if (err)
1546 goto err_aqs_init;
1547
1548 err = request_irq(mlxsw_pci->msix_entry.vector,
1549 mlxsw_pci_eq_irq_handler, 0,
1550 mlxsw_pci_driver_name, mlxsw_pci);
1551 if (err) {
1552 dev_err(&pdev->dev, "IRQ request failed\n");
1553 goto err_request_eq_irq;
1554 }
1555
1556 goto mbox_put;
1557
1558err_request_eq_irq:
1559 mlxsw_pci_aqs_fini(mlxsw_pci);
1560err_aqs_init:
1561err_config_profile:
Nogah Frankel57d316b2016-07-21 12:03:09 +02001562err_query_resources:
Jiri Pirkoeda65002015-07-29 23:33:47 +02001563err_boardinfo:
1564 mlxsw_pci_fw_area_fini(mlxsw_pci);
1565err_fw_area_init:
1566err_doorbell_page_bar:
1567err_iface_rev:
1568err_query_fw:
Ido Schimmel1e817792015-08-27 17:59:57 +02001569 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1570err_out_mbox_alloc:
1571 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
Jiri Pirkoeda65002015-07-29 23:33:47 +02001572mbox_put:
1573 mlxsw_cmd_mbox_free(mbox);
1574 return err;
1575}
1576
1577static void mlxsw_pci_fini(void *bus_priv)
1578{
1579 struct mlxsw_pci *mlxsw_pci = bus_priv;
1580
1581 free_irq(mlxsw_pci->msix_entry.vector, mlxsw_pci);
1582 mlxsw_pci_aqs_fini(mlxsw_pci);
1583 mlxsw_pci_fw_area_fini(mlxsw_pci);
Ido Schimmel1e817792015-08-27 17:59:57 +02001584 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.out_mbox);
1585 mlxsw_pci_mbox_free(mlxsw_pci, &mlxsw_pci->cmd.in_mbox);
Jiri Pirkoeda65002015-07-29 23:33:47 +02001586}
1587
1588static struct mlxsw_pci_queue *
1589mlxsw_pci_sdq_pick(struct mlxsw_pci *mlxsw_pci,
1590 const struct mlxsw_tx_info *tx_info)
1591{
1592 u8 sdqn = tx_info->local_port % mlxsw_pci_sdq_count(mlxsw_pci);
1593
1594 return mlxsw_pci_sdq_get(mlxsw_pci, sdqn);
1595}
1596
Ido Schimmeld0034622015-08-06 16:41:56 +02001597static bool mlxsw_pci_skb_transmit_busy(void *bus_priv,
1598 const struct mlxsw_tx_info *tx_info)
1599{
1600 struct mlxsw_pci *mlxsw_pci = bus_priv;
1601 struct mlxsw_pci_queue *q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1602
1603 return !mlxsw_pci_queue_elem_info_producer_get(q);
1604}
1605
Jiri Pirkoeda65002015-07-29 23:33:47 +02001606static int mlxsw_pci_skb_transmit(void *bus_priv, struct sk_buff *skb,
1607 const struct mlxsw_tx_info *tx_info)
1608{
1609 struct mlxsw_pci *mlxsw_pci = bus_priv;
1610 struct mlxsw_pci_queue *q;
1611 struct mlxsw_pci_queue_elem_info *elem_info;
1612 char *wqe;
1613 int i;
1614 int err;
1615
1616 if (skb_shinfo(skb)->nr_frags > MLXSW_PCI_WQE_SG_ENTRIES - 1) {
1617 err = skb_linearize(skb);
1618 if (err)
1619 return err;
1620 }
1621
1622 q = mlxsw_pci_sdq_pick(mlxsw_pci, tx_info);
1623 spin_lock_bh(&q->lock);
1624 elem_info = mlxsw_pci_queue_elem_info_producer_get(q);
1625 if (!elem_info) {
1626 /* queue is full */
1627 err = -EAGAIN;
1628 goto unlock;
1629 }
1630 elem_info->u.sdq.skb = skb;
1631
1632 wqe = elem_info->elem;
1633 mlxsw_pci_wqe_c_set(wqe, 1); /* always report completion */
1634 mlxsw_pci_wqe_lp_set(wqe, !!tx_info->is_emad);
1635 mlxsw_pci_wqe_type_set(wqe, MLXSW_PCI_WQE_TYPE_ETHERNET);
1636
1637 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, 0, skb->data,
1638 skb_headlen(skb), DMA_TO_DEVICE);
1639 if (err)
1640 goto unlock;
1641
1642 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1643 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1644
1645 err = mlxsw_pci_wqe_frag_map(mlxsw_pci, wqe, i + 1,
1646 skb_frag_address(frag),
1647 skb_frag_size(frag),
1648 DMA_TO_DEVICE);
1649 if (err)
1650 goto unmap_frags;
1651 }
1652
1653 /* Set unused sq entries byte count to zero. */
1654 for (i++; i < MLXSW_PCI_WQE_SG_ENTRIES; i++)
1655 mlxsw_pci_wqe_byte_count_set(wqe, i, 0);
1656
1657 /* Everything is set up, ring producer doorbell to get HW going */
1658 q->producer_counter++;
1659 mlxsw_pci_queue_doorbell_producer_ring(mlxsw_pci, q);
1660
1661 goto unlock;
1662
1663unmap_frags:
1664 for (; i >= 0; i--)
1665 mlxsw_pci_wqe_frag_unmap(mlxsw_pci, wqe, i, DMA_TO_DEVICE);
1666unlock:
1667 spin_unlock_bh(&q->lock);
1668 return err;
1669}
1670
1671static int mlxsw_pci_cmd_exec(void *bus_priv, u16 opcode, u8 opcode_mod,
1672 u32 in_mod, bool out_mbox_direct,
1673 char *in_mbox, size_t in_mbox_size,
1674 char *out_mbox, size_t out_mbox_size,
1675 u8 *p_status)
1676{
1677 struct mlxsw_pci *mlxsw_pci = bus_priv;
Ido Schimmel1e817792015-08-27 17:59:57 +02001678 dma_addr_t in_mapaddr = mlxsw_pci->cmd.in_mbox.mapaddr;
1679 dma_addr_t out_mapaddr = mlxsw_pci->cmd.out_mbox.mapaddr;
Jiri Pirkoeda65002015-07-29 23:33:47 +02001680 bool evreq = mlxsw_pci->cmd.nopoll;
1681 unsigned long timeout = msecs_to_jiffies(MLXSW_PCI_CIR_TIMEOUT_MSECS);
1682 bool *p_wait_done = &mlxsw_pci->cmd.wait_done;
1683 int err;
1684
1685 *p_status = MLXSW_CMD_STATUS_OK;
1686
1687 err = mutex_lock_interruptible(&mlxsw_pci->cmd.lock);
1688 if (err)
1689 return err;
1690
Ido Schimmel1e817792015-08-27 17:59:57 +02001691 if (in_mbox)
1692 memcpy(mlxsw_pci->cmd.in_mbox.buf, in_mbox, in_mbox_size);
Arnd Bergmannbcb9db42015-10-07 08:58:34 +02001693 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_HI, upper_32_bits(in_mapaddr));
1694 mlxsw_pci_write32(mlxsw_pci, CIR_IN_PARAM_LO, lower_32_bits(in_mapaddr));
Jiri Pirkoeda65002015-07-29 23:33:47 +02001695
Arnd Bergmannbcb9db42015-10-07 08:58:34 +02001696 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_HI, upper_32_bits(out_mapaddr));
1697 mlxsw_pci_write32(mlxsw_pci, CIR_OUT_PARAM_LO, lower_32_bits(out_mapaddr));
Jiri Pirkoeda65002015-07-29 23:33:47 +02001698
1699 mlxsw_pci_write32(mlxsw_pci, CIR_IN_MODIFIER, in_mod);
1700 mlxsw_pci_write32(mlxsw_pci, CIR_TOKEN, 0);
1701
1702 *p_wait_done = false;
1703
1704 wmb(); /* all needs to be written before we write control register */
1705 mlxsw_pci_write32(mlxsw_pci, CIR_CTRL,
1706 MLXSW_PCI_CIR_CTRL_GO_BIT |
1707 (evreq ? MLXSW_PCI_CIR_CTRL_EVREQ_BIT : 0) |
1708 (opcode_mod << MLXSW_PCI_CIR_CTRL_OPCODE_MOD_SHIFT) |
1709 opcode);
1710
1711 if (!evreq) {
1712 unsigned long end;
1713
1714 end = jiffies + timeout;
1715 do {
1716 u32 ctrl = mlxsw_pci_read32(mlxsw_pci, CIR_CTRL);
1717
1718 if (!(ctrl & MLXSW_PCI_CIR_CTRL_GO_BIT)) {
1719 *p_wait_done = true;
1720 *p_status = ctrl >> MLXSW_PCI_CIR_CTRL_STATUS_SHIFT;
1721 break;
1722 }
1723 cond_resched();
1724 } while (time_before(jiffies, end));
1725 } else {
1726 wait_event_timeout(mlxsw_pci->cmd.wait, *p_wait_done, timeout);
1727 *p_status = mlxsw_pci->cmd.comp.status;
1728 }
1729
1730 err = 0;
1731 if (*p_wait_done) {
1732 if (*p_status)
1733 err = -EIO;
1734 } else {
1735 err = -ETIMEDOUT;
1736 }
1737
1738 if (!err && out_mbox && out_mbox_direct) {
Ido Schimmel1e817792015-08-27 17:59:57 +02001739 /* Some commands don't use output param as address to mailbox
Jiri Pirkoeda65002015-07-29 23:33:47 +02001740 * but they store output directly into registers. In that case,
1741 * copy registers into mbox buffer.
1742 */
1743 __be32 tmp;
1744
1745 if (!evreq) {
1746 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1747 CIR_OUT_PARAM_HI));
1748 memcpy(out_mbox, &tmp, sizeof(tmp));
1749 tmp = cpu_to_be32(mlxsw_pci_read32(mlxsw_pci,
1750 CIR_OUT_PARAM_LO));
1751 memcpy(out_mbox + sizeof(tmp), &tmp, sizeof(tmp));
1752 }
Or Gerlitzd9324f62015-10-28 10:17:04 +01001753 } else if (!err && out_mbox) {
Ido Schimmel1e817792015-08-27 17:59:57 +02001754 memcpy(out_mbox, mlxsw_pci->cmd.out_mbox.buf, out_mbox_size);
Or Gerlitzd9324f62015-10-28 10:17:04 +01001755 }
Jiri Pirkoeda65002015-07-29 23:33:47 +02001756
Jiri Pirkoeda65002015-07-29 23:33:47 +02001757 mutex_unlock(&mlxsw_pci->cmd.lock);
1758
1759 return err;
1760}
1761
1762static const struct mlxsw_bus mlxsw_pci_bus = {
Ido Schimmeld0034622015-08-06 16:41:56 +02001763 .kind = "pci",
1764 .init = mlxsw_pci_init,
1765 .fini = mlxsw_pci_fini,
1766 .skb_transmit_busy = mlxsw_pci_skb_transmit_busy,
1767 .skb_transmit = mlxsw_pci_skb_transmit,
1768 .cmd_exec = mlxsw_pci_cmd_exec,
Jiri Pirkoeda65002015-07-29 23:33:47 +02001769};
1770
1771static int mlxsw_pci_sw_reset(struct mlxsw_pci *mlxsw_pci)
1772{
Jiri Pirko233fa442016-03-10 23:10:21 +01001773 unsigned long end;
1774
Jiri Pirkoeda65002015-07-29 23:33:47 +02001775 mlxsw_pci_write32(mlxsw_pci, SW_RESET, MLXSW_PCI_SW_RESET_RST_BIT);
Jiri Pirko233fa442016-03-10 23:10:21 +01001776 wmb(); /* reset needs to be written before we read control register */
1777 end = jiffies + msecs_to_jiffies(MLXSW_PCI_SW_RESET_TIMEOUT_MSECS);
1778 do {
1779 u32 val = mlxsw_pci_read32(mlxsw_pci, FW_READY);
1780
1781 if ((val & MLXSW_PCI_FW_READY_MASK) == MLXSW_PCI_FW_READY_MAGIC)
1782 break;
1783 cond_resched();
1784 } while (time_before(jiffies, end));
Jiri Pirkoeda65002015-07-29 23:33:47 +02001785 return 0;
1786}
1787
1788static int mlxsw_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1789{
1790 struct mlxsw_pci *mlxsw_pci;
1791 int err;
1792
1793 mlxsw_pci = kzalloc(sizeof(*mlxsw_pci), GFP_KERNEL);
1794 if (!mlxsw_pci)
1795 return -ENOMEM;
1796
1797 err = pci_enable_device(pdev);
1798 if (err) {
1799 dev_err(&pdev->dev, "pci_enable_device failed\n");
1800 goto err_pci_enable_device;
1801 }
1802
1803 err = pci_request_regions(pdev, mlxsw_pci_driver_name);
1804 if (err) {
1805 dev_err(&pdev->dev, "pci_request_regions failed\n");
1806 goto err_pci_request_regions;
1807 }
1808
1809 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1810 if (!err) {
1811 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1812 if (err) {
1813 dev_err(&pdev->dev, "pci_set_consistent_dma_mask failed\n");
1814 goto err_pci_set_dma_mask;
1815 }
1816 } else {
1817 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1818 if (err) {
1819 dev_err(&pdev->dev, "pci_set_dma_mask failed\n");
1820 goto err_pci_set_dma_mask;
1821 }
1822 }
1823
1824 if (pci_resource_len(pdev, 0) < MLXSW_PCI_BAR0_SIZE) {
1825 dev_err(&pdev->dev, "invalid PCI region size\n");
1826 err = -EINVAL;
1827 goto err_pci_resource_len_check;
1828 }
1829
1830 mlxsw_pci->hw_addr = ioremap(pci_resource_start(pdev, 0),
1831 pci_resource_len(pdev, 0));
1832 if (!mlxsw_pci->hw_addr) {
1833 dev_err(&pdev->dev, "ioremap failed\n");
1834 err = -EIO;
1835 goto err_ioremap;
1836 }
1837 pci_set_master(pdev);
1838
1839 mlxsw_pci->pdev = pdev;
1840 pci_set_drvdata(pdev, mlxsw_pci);
1841
1842 err = mlxsw_pci_sw_reset(mlxsw_pci);
1843 if (err) {
1844 dev_err(&pdev->dev, "Software reset failed\n");
1845 goto err_sw_reset;
1846 }
1847
1848 err = pci_enable_msix_exact(pdev, &mlxsw_pci->msix_entry, 1);
1849 if (err) {
1850 dev_err(&pdev->dev, "MSI-X init failed\n");
1851 goto err_msix_init;
1852 }
1853
1854 mlxsw_pci->bus_info.device_kind = mlxsw_pci_device_kind_get(id);
1855 mlxsw_pci->bus_info.device_name = pci_name(mlxsw_pci->pdev);
1856 mlxsw_pci->bus_info.dev = &pdev->dev;
1857
1858 mlxsw_pci->dbg_dir = debugfs_create_dir(mlxsw_pci->bus_info.device_name,
1859 mlxsw_pci_dbg_root);
1860 if (!mlxsw_pci->dbg_dir) {
1861 dev_err(&pdev->dev, "Failed to create debugfs dir\n");
Julia Lawall5c121972015-08-23 02:11:16 +02001862 err = -ENOMEM;
Jiri Pirkoeda65002015-07-29 23:33:47 +02001863 goto err_dbg_create_dir;
1864 }
1865
1866 err = mlxsw_core_bus_device_register(&mlxsw_pci->bus_info,
1867 &mlxsw_pci_bus, mlxsw_pci);
1868 if (err) {
1869 dev_err(&pdev->dev, "cannot register bus device\n");
1870 goto err_bus_device_register;
1871 }
1872
1873 return 0;
1874
1875err_bus_device_register:
1876 debugfs_remove_recursive(mlxsw_pci->dbg_dir);
1877err_dbg_create_dir:
1878 pci_disable_msix(mlxsw_pci->pdev);
1879err_msix_init:
1880err_sw_reset:
1881 iounmap(mlxsw_pci->hw_addr);
1882err_ioremap:
1883err_pci_resource_len_check:
1884err_pci_set_dma_mask:
1885 pci_release_regions(pdev);
1886err_pci_request_regions:
1887 pci_disable_device(pdev);
1888err_pci_enable_device:
1889 kfree(mlxsw_pci);
1890 return err;
1891}
1892
1893static void mlxsw_pci_remove(struct pci_dev *pdev)
1894{
1895 struct mlxsw_pci *mlxsw_pci = pci_get_drvdata(pdev);
1896
1897 mlxsw_core_bus_device_unregister(mlxsw_pci->core);
1898 debugfs_remove_recursive(mlxsw_pci->dbg_dir);
1899 pci_disable_msix(mlxsw_pci->pdev);
1900 iounmap(mlxsw_pci->hw_addr);
1901 pci_release_regions(mlxsw_pci->pdev);
1902 pci_disable_device(mlxsw_pci->pdev);
1903 kfree(mlxsw_pci);
1904}
1905
1906static struct pci_driver mlxsw_pci_driver = {
1907 .name = mlxsw_pci_driver_name,
1908 .id_table = mlxsw_pci_id_table,
1909 .probe = mlxsw_pci_probe,
1910 .remove = mlxsw_pci_remove,
1911};
1912
1913static int __init mlxsw_pci_module_init(void)
1914{
1915 int err;
1916
1917 mlxsw_pci_dbg_root = debugfs_create_dir(mlxsw_pci_driver_name, NULL);
1918 if (!mlxsw_pci_dbg_root)
1919 return -ENOMEM;
1920 err = pci_register_driver(&mlxsw_pci_driver);
1921 if (err)
1922 goto err_register_driver;
1923 return 0;
1924
1925err_register_driver:
1926 debugfs_remove_recursive(mlxsw_pci_dbg_root);
1927 return err;
1928}
1929
1930static void __exit mlxsw_pci_module_exit(void)
1931{
1932 pci_unregister_driver(&mlxsw_pci_driver);
1933 debugfs_remove_recursive(mlxsw_pci_dbg_root);
1934}
1935
1936module_init(mlxsw_pci_module_init);
1937module_exit(mlxsw_pci_module_exit);
1938
1939MODULE_LICENSE("Dual BSD/GPL");
1940MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1941MODULE_DESCRIPTION("Mellanox switch PCI interface driver");
1942MODULE_DEVICE_TABLE(pci, mlxsw_pci_id_table);