blob: d3a6a1c280537683414635b414cb73123aa18ae5 [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
Raghu Vatsavayi50579d32016-11-14 15:54:46 -08002 * Author: Cavium, Inc.
3 *
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
6 *
7 * Copyright (c) 2003-2016 Cavium, Inc.
8 *
9 * This file is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License, Version 2, as
11 * published by the Free Software Foundation.
12 *
13 * This file is distributed in the hope that it will be useful, but
14 * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
15 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
16 * NONINFRINGEMENT. See the GNU General Public License for more details.
17 ***********************************************************************/
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070018#include <linux/pci.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070019#include <linux/netdevice.h>
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -070020#include <linux/vmalloc.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070021#include "liquidio_common.h"
22#include "octeon_droq.h"
23#include "octeon_iq.h"
24#include "response_manager.h"
25#include "octeon_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070026#include "octeon_main.h"
27#include "octeon_network.h"
28#include "cn66xx_regs.h"
29#include "cn66xx_device.h"
Raghu Vatsavayi5b823512016-09-01 11:16:07 -070030#include "cn23xx_pf_device.h"
Raghu Vatsavayi9217c3c2016-12-07 08:54:37 -080031#include "cn23xx_vf_device.h"
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070032
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070033struct niclist {
34 struct list_head list;
35 void *ptr;
36};
37
38struct __dispatch {
39 struct list_head list;
40 struct octeon_recv_info *rinfo;
41 octeon_dispatch_fn_t disp_fn;
42};
43
44/** Get the argument that the user set when registering dispatch
45 * function for a given opcode/subcode.
46 * @param octeon_dev - the octeon device pointer.
47 * @param opcode - the opcode for which the dispatch argument
48 * is to be checked.
49 * @param subcode - the subcode for which the dispatch argument
50 * is to be checked.
51 * @return Success: void * (argument to the dispatch function)
52 * @return Failure: NULL
53 *
54 */
55static inline void *octeon_get_dispatch_arg(struct octeon_device *octeon_dev,
56 u16 opcode, u16 subcode)
57{
58 int idx;
59 struct list_head *dispatch;
60 void *fn_arg = NULL;
61 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
62
63 idx = combined_opcode & OCTEON_OPCODE_MASK;
64
65 spin_lock_bh(&octeon_dev->dispatch.lock);
66
67 if (octeon_dev->dispatch.count == 0) {
68 spin_unlock_bh(&octeon_dev->dispatch.lock);
69 return NULL;
70 }
71
72 if (octeon_dev->dispatch.dlist[idx].opcode == combined_opcode) {
73 fn_arg = octeon_dev->dispatch.dlist[idx].arg;
74 } else {
75 list_for_each(dispatch,
76 &octeon_dev->dispatch.dlist[idx].list) {
77 if (((struct octeon_dispatch *)dispatch)->opcode ==
78 combined_opcode) {
79 fn_arg = ((struct octeon_dispatch *)
80 dispatch)->arg;
81 break;
82 }
83 }
84 }
85
86 spin_unlock_bh(&octeon_dev->dispatch.lock);
87 return fn_arg;
88}
89
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -070090/** Check for packets on Droq. This function should be called with lock held.
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -070091 * @param droq - Droq on which count is checked.
92 * @return Returns packet count.
93 */
Raghu Vatsavayia7d5a3d2016-07-03 13:56:48 -070094u32 octeon_droq_check_hw_for_pkts(struct octeon_droq *droq)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070095{
96 u32 pkt_count = 0;
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -070097 u32 last_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070098
99 pkt_count = readl(droq->pkts_sent_reg);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700100
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700101 last_count = pkt_count - droq->pkt_count;
102 droq->pkt_count = pkt_count;
103
104 /* we shall write to cnts at napi irq enable or end of droq tasklet */
105 if (last_count)
106 atomic_add(last_count, &droq->pkts_pending);
107
108 return last_count;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700109}
110
111static void octeon_droq_compute_max_packet_bufs(struct octeon_droq *droq)
112{
113 u32 count = 0;
114
115 /* max_empty_descs is the max. no. of descs that can have no buffers.
116 * If the empty desc count goes beyond this value, we cannot safely
117 * read in a 64K packet sent by Octeon
118 * (64K is max pkt size from Octeon)
119 */
120 droq->max_empty_descs = 0;
121
122 do {
123 droq->max_empty_descs++;
124 count += droq->buffer_size;
125 } while (count < (64 * 1024));
126
127 droq->max_empty_descs = droq->max_count - droq->max_empty_descs;
128}
129
130static void octeon_droq_reset_indices(struct octeon_droq *droq)
131{
132 droq->read_idx = 0;
133 droq->write_idx = 0;
134 droq->refill_idx = 0;
135 droq->refill_count = 0;
136 atomic_set(&droq->pkts_pending, 0);
137}
138
139static void
140octeon_droq_destroy_ring_buffers(struct octeon_device *oct,
141 struct octeon_droq *droq)
142{
143 u32 i;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700144 struct octeon_skb_page_info *pg_info;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700145
146 for (i = 0; i < droq->max_count; i++) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700147 pg_info = &droq->recv_buf_list[i].pg_info;
148
149 if (pg_info->dma)
150 lio_unmap_ring(oct->pci_dev,
151 (u64)pg_info->dma);
152 pg_info->dma = 0;
153
154 if (pg_info->page)
155 recv_buffer_destroy(droq->recv_buf_list[i].buffer,
156 pg_info);
157
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700158 droq->recv_buf_list[i].buffer = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700159 }
160
161 octeon_droq_reset_indices(droq);
162}
163
164static int
165octeon_droq_setup_ring_buffers(struct octeon_device *oct,
166 struct octeon_droq *droq)
167{
168 u32 i;
169 void *buf;
170 struct octeon_droq_desc *desc_ring = droq->desc_ring;
171
172 for (i = 0; i < droq->max_count; i++) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700173 buf = recv_buffer_alloc(oct, &droq->recv_buf_list[i].pg_info);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700174
175 if (!buf) {
176 dev_err(&oct->pci_dev->dev, "%s buffer alloc failed\n",
177 __func__);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700178 droq->stats.rx_alloc_failure++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700179 return -ENOMEM;
180 }
181
182 droq->recv_buf_list[i].buffer = buf;
183 droq->recv_buf_list[i].data = get_rbd(buf);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700184 droq->info_list[i].length = 0;
185
186 /* map ring buffers into memory */
187 desc_ring[i].info_ptr = lio_map_ring_info(droq, i);
188 desc_ring[i].buffer_ptr =
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700189 lio_map_ring(droq->recv_buf_list[i].buffer);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700190 }
191
192 octeon_droq_reset_indices(droq);
193
194 octeon_droq_compute_max_packet_bufs(droq);
195
196 return 0;
197}
198
199int octeon_delete_droq(struct octeon_device *oct, u32 q_no)
200{
201 struct octeon_droq *droq = oct->droq[q_no];
202
203 dev_dbg(&oct->pci_dev->dev, "%s[%d]\n", __func__, q_no);
204
205 octeon_droq_destroy_ring_buffers(oct, droq);
Markus Elfring9686f312015-06-29 12:22:24 +0200206 vfree(droq->recv_buf_list);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700207
208 if (droq->info_base_addr)
VSR Burru67e303e2017-03-06 18:45:59 -0800209 lio_free_info_buffer(oct, droq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700210
211 if (droq->desc_ring)
212 lio_dma_free(oct, (droq->max_count * OCT_DROQ_DESC_SIZE),
213 droq->desc_ring, droq->desc_ring_dma);
214
215 memset(droq, 0, OCT_DROQ_SIZE);
216
217 return 0;
218}
219
220int octeon_init_droq(struct octeon_device *oct,
221 u32 q_no,
222 u32 num_descs,
223 u32 desc_size,
224 void *app_ctx)
225{
226 struct octeon_droq *droq;
227 u32 desc_ring_size = 0, c_num_descs = 0, c_buf_size = 0;
228 u32 c_pkts_per_intr = 0, c_refill_threshold = 0;
VSR Burrub3ca9af2017-03-09 17:03:24 -0800229 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700230
231 dev_dbg(&oct->pci_dev->dev, "%s[%d]\n", __func__, q_no);
232
233 droq = oct->droq[q_no];
234 memset(droq, 0, OCT_DROQ_SIZE);
235
236 droq->oct_dev = oct;
237 droq->q_no = q_no;
238 if (app_ctx)
239 droq->app_ctx = app_ctx;
240 else
241 droq->app_ctx = (void *)(size_t)q_no;
242
243 c_num_descs = num_descs;
244 c_buf_size = desc_size;
245 if (OCTEON_CN6XXX(oct)) {
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800246 struct octeon_config *conf6x = CHIP_CONF(oct, cn6xxx);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700247
248 c_pkts_per_intr = (u32)CFG_GET_OQ_PKTS_PER_INTR(conf6x);
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700249 c_refill_threshold =
250 (u32)CFG_GET_OQ_REFILL_THRESHOLD(conf6x);
Raghu Vatsavayi5b823512016-09-01 11:16:07 -0700251 } else if (OCTEON_CN23XX_PF(oct)) {
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800252 struct octeon_config *conf23 = CHIP_CONF(oct, cn23xx_pf);
Raghu Vatsavayi5b823512016-09-01 11:16:07 -0700253
254 c_pkts_per_intr = (u32)CFG_GET_OQ_PKTS_PER_INTR(conf23);
255 c_refill_threshold = (u32)CFG_GET_OQ_REFILL_THRESHOLD(conf23);
Raghu Vatsavayi9217c3c2016-12-07 08:54:37 -0800256 } else if (OCTEON_CN23XX_VF(oct)) {
257 struct octeon_config *conf23 = CHIP_CONF(oct, cn23xx_vf);
258
259 c_pkts_per_intr = (u32)CFG_GET_OQ_PKTS_PER_INTR(conf23);
260 c_refill_threshold = (u32)CFG_GET_OQ_REFILL_THRESHOLD(conf23);
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700261 } else {
262 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700263 }
264
265 droq->max_count = c_num_descs;
266 droq->buffer_size = c_buf_size;
267
268 desc_ring_size = droq->max_count * OCT_DROQ_DESC_SIZE;
269 droq->desc_ring = lio_dma_alloc(oct, desc_ring_size,
270 (dma_addr_t *)&droq->desc_ring_dma);
271
272 if (!droq->desc_ring) {
273 dev_err(&oct->pci_dev->dev,
274 "Output queue %d ring alloc failed\n", q_no);
275 return 1;
276 }
277
278 dev_dbg(&oct->pci_dev->dev, "droq[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
279 q_no, droq->desc_ring, droq->desc_ring_dma);
280 dev_dbg(&oct->pci_dev->dev, "droq[%d]: num_desc: %d\n", q_no,
281 droq->max_count);
282
VSR Burru67e303e2017-03-06 18:45:59 -0800283 droq->info_list = lio_alloc_info_buffer(oct, droq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700284 if (!droq->info_list) {
285 dev_err(&oct->pci_dev->dev, "Cannot allocate memory for info list.\n");
286 lio_dma_free(oct, (droq->max_count * OCT_DROQ_DESC_SIZE),
287 droq->desc_ring, droq->desc_ring_dma);
288 return 1;
289 }
290
291 droq->recv_buf_list = (struct octeon_recv_buffer *)
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700292 vmalloc_node(droq->max_count *
293 OCT_DROQ_RECVBUF_SIZE,
294 numa_node);
295 if (!droq->recv_buf_list)
296 droq->recv_buf_list = (struct octeon_recv_buffer *)
297 vmalloc(droq->max_count *
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700298 OCT_DROQ_RECVBUF_SIZE);
299 if (!droq->recv_buf_list) {
300 dev_err(&oct->pci_dev->dev, "Output queue recv buf list alloc failed\n");
301 goto init_droq_fail;
302 }
303
304 if (octeon_droq_setup_ring_buffers(oct, droq))
305 goto init_droq_fail;
306
307 droq->pkts_per_intr = c_pkts_per_intr;
308 droq->refill_threshold = c_refill_threshold;
309
310 dev_dbg(&oct->pci_dev->dev, "DROQ INIT: max_empty_descs: %d\n",
311 droq->max_empty_descs);
312
313 spin_lock_init(&droq->lock);
314
315 INIT_LIST_HEAD(&droq->dispatch_list);
316
317 /* For 56xx Pass1, this function won't be called, so no checks. */
318 oct->fn_list.setup_oq_regs(oct, q_no);
319
Raghu Vatsavayi763185a2016-11-14 15:54:45 -0800320 oct->io_qmask.oq |= BIT_ULL(q_no);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700321
322 return 0;
323
324init_droq_fail:
325 octeon_delete_droq(oct, q_no);
326 return 1;
327}
328
329/* octeon_create_recv_info
330 * Parameters:
331 * octeon_dev - pointer to the octeon device structure
332 * droq - droq in which the packet arrived.
333 * buf_cnt - no. of buffers used by the packet.
334 * idx - index in the descriptor for the first buffer in the packet.
335 * Description:
336 * Allocates a recv_info_t and copies the buffer addresses for packet data
337 * into the recv_pkt space which starts at an 8B offset from recv_info_t.
338 * Flags the descriptors for refill later. If available descriptors go
339 * below the threshold to receive a 64K pkt, new buffers are first allocated
340 * before the recv_pkt_t is created.
341 * This routine will be called in interrupt context.
342 * Returns:
343 * Success: Pointer to recv_info_t
344 * Failure: NULL.
345 * Locks:
346 * The droq->lock is held when this routine is called.
347 */
348static inline struct octeon_recv_info *octeon_create_recv_info(
349 struct octeon_device *octeon_dev,
350 struct octeon_droq *droq,
351 u32 buf_cnt,
352 u32 idx)
353{
354 struct octeon_droq_info *info;
355 struct octeon_recv_pkt *recv_pkt;
356 struct octeon_recv_info *recv_info;
357 u32 i, bytes_left;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700358 struct octeon_skb_page_info *pg_info;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700359
360 info = &droq->info_list[idx];
361
362 recv_info = octeon_alloc_recv_info(sizeof(struct __dispatch));
363 if (!recv_info)
364 return NULL;
365
366 recv_pkt = recv_info->recv_pkt;
367 recv_pkt->rh = info->rh;
368 recv_pkt->length = (u32)info->length;
369 recv_pkt->buffer_count = (u16)buf_cnt;
370 recv_pkt->octeon_id = (u16)octeon_dev->octeon_id;
371
372 i = 0;
373 bytes_left = (u32)info->length;
374
375 while (buf_cnt) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700376 {
377 pg_info = &droq->recv_buf_list[idx].pg_info;
378
379 lio_unmap_ring(octeon_dev->pci_dev,
380 (u64)pg_info->dma);
381 pg_info->page = NULL;
382 pg_info->dma = 0;
383 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700384
385 recv_pkt->buffer_size[i] =
386 (bytes_left >=
387 droq->buffer_size) ? droq->buffer_size : bytes_left;
388
389 recv_pkt->buffer_ptr[i] = droq->recv_buf_list[idx].buffer;
390 droq->recv_buf_list[idx].buffer = NULL;
391
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800392 idx = incr_index(idx, 1, droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700393 bytes_left -= droq->buffer_size;
394 i++;
395 buf_cnt--;
396 }
397
398 return recv_info;
399}
400
401/* If we were not able to refill all buffers, try to move around
402 * the buffers that were not dispatched.
403 */
404static inline u32
405octeon_droq_refill_pullup_descs(struct octeon_droq *droq,
406 struct octeon_droq_desc *desc_ring)
407{
408 u32 desc_refilled = 0;
409
410 u32 refill_index = droq->refill_idx;
411
412 while (refill_index != droq->read_idx) {
413 if (droq->recv_buf_list[refill_index].buffer) {
414 droq->recv_buf_list[droq->refill_idx].buffer =
415 droq->recv_buf_list[refill_index].buffer;
416 droq->recv_buf_list[droq->refill_idx].data =
417 droq->recv_buf_list[refill_index].data;
418 desc_ring[droq->refill_idx].buffer_ptr =
419 desc_ring[refill_index].buffer_ptr;
420 droq->recv_buf_list[refill_index].buffer = NULL;
421 desc_ring[refill_index].buffer_ptr = 0;
422 do {
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800423 droq->refill_idx = incr_index(droq->refill_idx,
424 1,
425 droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700426 desc_refilled++;
427 droq->refill_count--;
Satanand Burla9ae122c2017-05-31 10:45:15 -0700428 } while (droq->recv_buf_list[droq->refill_idx].buffer);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700429 }
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800430 refill_index = incr_index(refill_index, 1, droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700431 } /* while */
432 return desc_refilled;
433}
434
435/* octeon_droq_refill
436 * Parameters:
437 * droq - droq in which descriptors require new buffers.
438 * Description:
439 * Called during normal DROQ processing in interrupt mode or by the poll
440 * thread to refill the descriptors from which buffers were dispatched
441 * to upper layers. Attempts to allocate new buffers. If that fails, moves
442 * up buffers (that were not dispatched) to form a contiguous ring.
443 * Returns:
444 * No of descriptors refilled.
445 * Locks:
446 * This routine is called with droq->lock held.
447 */
448static u32
449octeon_droq_refill(struct octeon_device *octeon_dev, struct octeon_droq *droq)
450{
451 struct octeon_droq_desc *desc_ring;
452 void *buf = NULL;
453 u8 *data;
454 u32 desc_refilled = 0;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700455 struct octeon_skb_page_info *pg_info;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700456
457 desc_ring = droq->desc_ring;
458
459 while (droq->refill_count && (desc_refilled < droq->max_count)) {
460 /* If a valid buffer exists (happens if there is no dispatch),
461 * reuse
462 * the buffer, else allocate.
463 */
464 if (!droq->recv_buf_list[droq->refill_idx].buffer) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700465 pg_info =
466 &droq->recv_buf_list[droq->refill_idx].pg_info;
467 /* Either recycle the existing pages or go for
468 * new page alloc
469 */
470 if (pg_info->page)
471 buf = recv_buffer_reuse(octeon_dev, pg_info);
472 else
473 buf = recv_buffer_alloc(octeon_dev, pg_info);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700474 /* If a buffer could not be allocated, no point in
475 * continuing
476 */
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700477 if (!buf) {
478 droq->stats.rx_alloc_failure++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700479 break;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700480 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700481 droq->recv_buf_list[droq->refill_idx].buffer =
482 buf;
483 data = get_rbd(buf);
484 } else {
485 data = get_rbd(droq->recv_buf_list
486 [droq->refill_idx].buffer);
487 }
488
489 droq->recv_buf_list[droq->refill_idx].data = data;
490
491 desc_ring[droq->refill_idx].buffer_ptr =
Satanand Burla9ae122c2017-05-31 10:45:15 -0700492 lio_map_ring(droq->recv_buf_list[
493 droq->refill_idx].buffer);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700494 /* Reset any previous values in the length field. */
495 droq->info_list[droq->refill_idx].length = 0;
496
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800497 droq->refill_idx = incr_index(droq->refill_idx, 1,
498 droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700499 desc_refilled++;
500 droq->refill_count--;
501 }
502
503 if (droq->refill_count)
504 desc_refilled +=
505 octeon_droq_refill_pullup_descs(droq, desc_ring);
506
507 /* if droq->refill_count
508 * The refill count would not change in pass two. We only moved buffers
509 * to close the gap in the ring, but we would still have the same no. of
510 * buffers to refill.
511 */
512 return desc_refilled;
513}
514
Satanand Burla031d4f12017-03-22 11:31:13 -0700515/** check if we can allocate packets to get out of oom.
516 * @param droq - Droq being checked.
517 * @return does not return anything
518 */
519void octeon_droq_check_oom(struct octeon_droq *droq)
520{
521 int desc_refilled;
522 struct octeon_device *oct = droq->oct_dev;
523
524 if (readl(droq->pkts_credit_reg) <= CN23XX_SLI_DEF_BP) {
525 spin_lock_bh(&droq->lock);
526 desc_refilled = octeon_droq_refill(oct, droq);
527 if (desc_refilled) {
528 /* Flush the droq descriptor data to memory to be sure
529 * that when we update the credits the data in memory
530 * is accurate.
531 */
532 wmb();
533 writel(desc_refilled, droq->pkts_credit_reg);
534 /* make sure mmio write completes */
535 mmiowb();
536 }
537 spin_unlock_bh(&droq->lock);
538 }
539}
540
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700541static inline u32
542octeon_droq_get_bufcount(u32 buf_size, u32 total_len)
543{
544 u32 buf_cnt = 0;
545
546 while (total_len > (buf_size * buf_cnt))
547 buf_cnt++;
548 return buf_cnt;
549}
550
551static int
552octeon_droq_dispatch_pkt(struct octeon_device *oct,
553 struct octeon_droq *droq,
554 union octeon_rh *rh,
555 struct octeon_droq_info *info)
556{
557 u32 cnt;
558 octeon_dispatch_fn_t disp_fn;
559 struct octeon_recv_info *rinfo;
560
561 cnt = octeon_droq_get_bufcount(droq->buffer_size, (u32)info->length);
562
563 disp_fn = octeon_get_dispatch(oct, (u16)rh->r.opcode,
564 (u16)rh->r.subcode);
565 if (disp_fn) {
566 rinfo = octeon_create_recv_info(oct, droq, cnt, droq->read_idx);
567 if (rinfo) {
568 struct __dispatch *rdisp = rinfo->rsvd;
569
570 rdisp->rinfo = rinfo;
571 rdisp->disp_fn = disp_fn;
572 rinfo->recv_pkt->rh = *rh;
573 list_add_tail(&rdisp->list,
574 &droq->dispatch_list);
575 } else {
576 droq->stats.dropped_nomem++;
577 }
578 } else {
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -0700579 dev_err(&oct->pci_dev->dev, "DROQ: No dispatch function (opcode %u/%u)\n",
580 (unsigned int)rh->r.opcode,
581 (unsigned int)rh->r.subcode);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700582 droq->stats.dropped_nodispatch++;
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700583 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700584
585 return cnt;
586}
587
588static inline void octeon_droq_drop_packets(struct octeon_device *oct,
589 struct octeon_droq *droq,
590 u32 cnt)
591{
592 u32 i = 0, buf_cnt;
593 struct octeon_droq_info *info;
594
595 for (i = 0; i < cnt; i++) {
596 info = &droq->info_list[droq->read_idx];
597 octeon_swap_8B_data((u64 *)info, 2);
598
599 if (info->length) {
600 info->length -= OCT_RH_SIZE;
601 droq->stats.bytes_received += info->length;
602 buf_cnt = octeon_droq_get_bufcount(droq->buffer_size,
603 (u32)info->length);
604 } else {
605 dev_err(&oct->pci_dev->dev, "DROQ: In drop: pkt with len 0\n");
606 buf_cnt = 1;
607 }
608
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800609 droq->read_idx = incr_index(droq->read_idx, buf_cnt,
610 droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700611 droq->refill_count += buf_cnt;
612 }
613}
614
615static u32
616octeon_droq_fast_process_packets(struct octeon_device *oct,
617 struct octeon_droq *droq,
618 u32 pkts_to_process)
619{
620 struct octeon_droq_info *info;
621 union octeon_rh *rh;
622 u32 pkt, total_len = 0, pkt_count;
623
624 pkt_count = pkts_to_process;
625
626 for (pkt = 0; pkt < pkt_count; pkt++) {
627 u32 pkt_len = 0;
628 struct sk_buff *nicbuf = NULL;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700629 struct octeon_skb_page_info *pg_info;
630 void *buf;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700631
632 info = &droq->info_list[droq->read_idx];
633 octeon_swap_8B_data((u64 *)info, 2);
634
635 if (!info->length) {
636 dev_err(&oct->pci_dev->dev,
637 "DROQ[%d] idx: %d len:0, pkt_cnt: %d\n",
638 droq->q_no, droq->read_idx, pkt_count);
639 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS,
640 (u8 *)info,
641 OCT_DROQ_INFO_SIZE);
642 break;
643 }
644
645 /* Len of resp hdr in included in the received data len. */
646 info->length -= OCT_RH_SIZE;
647 rh = &info->rh;
648
649 total_len += (u32)info->length;
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800650 if (opcode_slow_path(rh)) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700651 u32 buf_cnt;
652
653 buf_cnt = octeon_droq_dispatch_pkt(oct, droq, rh, info);
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800654 droq->read_idx = incr_index(droq->read_idx,
655 buf_cnt, droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700656 droq->refill_count += buf_cnt;
657 } else {
658 if (info->length <= droq->buffer_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700659 pkt_len = (u32)info->length;
660 nicbuf = droq->recv_buf_list[
661 droq->read_idx].buffer;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700662 pg_info = &droq->recv_buf_list[
663 droq->read_idx].pg_info;
664 if (recv_buffer_recycle(oct, pg_info))
665 pg_info->page = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700666 droq->recv_buf_list[droq->read_idx].buffer =
667 NULL;
Raghu Vatsavayia2c64b62016-07-03 13:56:55 -0700668
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800669 droq->read_idx = incr_index(droq->read_idx, 1,
670 droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700671 droq->refill_count++;
672 } else {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700673 nicbuf = octeon_fast_packet_alloc((u32)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700674 info->length);
675 pkt_len = 0;
676 /* nicbuf allocation can fail. We'll handle it
677 * inside the loop.
678 */
679 while (pkt_len < info->length) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700680 int cpy_len, idx = droq->read_idx;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700681
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700682 cpy_len = ((pkt_len + droq->buffer_size)
683 > info->length) ?
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700684 ((u32)info->length - pkt_len) :
685 droq->buffer_size;
686
687 if (nicbuf) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700688 octeon_fast_packet_next(droq,
689 nicbuf,
690 cpy_len,
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700691 idx);
Satanand Burla9ae122c2017-05-31 10:45:15 -0700692 buf = droq->recv_buf_list[
693 idx].buffer;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700694 recv_buffer_fast_free(buf);
695 droq->recv_buf_list[idx].buffer
696 = NULL;
697 } else {
698 droq->stats.rx_alloc_failure++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700699 }
700
701 pkt_len += cpy_len;
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800702 droq->read_idx =
703 incr_index(droq->read_idx, 1,
704 droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700705 droq->refill_count++;
706 }
707 }
708
709 if (nicbuf) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700710 if (droq->ops.fptr) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700711 droq->ops.fptr(oct->octeon_id,
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700712 nicbuf, pkt_len,
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700713 rh, &droq->napi,
714 droq->ops.farg);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700715 } else {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700716 recv_buffer_free(nicbuf);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700717 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700718 }
719 }
720
721 if (droq->refill_count >= droq->refill_threshold) {
722 int desc_refilled = octeon_droq_refill(oct, droq);
723
724 /* Flush the droq descriptor data to memory to be sure
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700725 * that when we update the credits the data in memory
726 * is accurate.
727 */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700728 wmb();
729 writel((desc_refilled), droq->pkts_credit_reg);
730 /* make sure mmio write completes */
731 mmiowb();
732 }
733
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700734 } /* for (each packet)... */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700735
736 /* Increment refill_count by the number of buffers processed. */
737 droq->stats.pkts_received += pkt;
738 droq->stats.bytes_received += total_len;
739
740 if ((droq->ops.drop_on_max) && (pkts_to_process - pkt)) {
741 octeon_droq_drop_packets(oct, droq, (pkts_to_process - pkt));
742
743 droq->stats.dropped_toomany += (pkts_to_process - pkt);
744 return pkts_to_process;
745 }
746
747 return pkt;
748}
749
750int
751octeon_droq_process_packets(struct octeon_device *oct,
752 struct octeon_droq *droq,
753 u32 budget)
754{
755 u32 pkt_count = 0, pkts_processed = 0;
756 struct list_head *tmp, *tmp2;
757
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700758 /* Grab the droq lock */
759 spin_lock(&droq->lock);
760
761 octeon_droq_check_hw_for_pkts(droq);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700762 pkt_count = atomic_read(&droq->pkts_pending);
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700763
764 if (!pkt_count) {
765 spin_unlock(&droq->lock);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700766 return 0;
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700767 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700768
769 if (pkt_count > budget)
770 pkt_count = budget;
771
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700772 pkts_processed = octeon_droq_fast_process_packets(oct, droq, pkt_count);
773
774 atomic_sub(pkts_processed, &droq->pkts_pending);
775
776 /* Release the spin lock */
777 spin_unlock(&droq->lock);
778
779 list_for_each_safe(tmp, tmp2, &droq->dispatch_list) {
780 struct __dispatch *rdisp = (struct __dispatch *)tmp;
781
782 list_del(tmp);
783 rdisp->disp_fn(rdisp->rinfo,
784 octeon_get_dispatch_arg
785 (oct,
786 (u16)rdisp->rinfo->recv_pkt->rh.r.opcode,
787 (u16)rdisp->rinfo->recv_pkt->rh.r.subcode));
788 }
789
790 /* If there are packets pending. schedule tasklet again */
791 if (atomic_read(&droq->pkts_pending))
792 return 1;
793
794 return 0;
795}
796
797/**
798 * Utility function to poll for packets. check_hw_for_packets must be
799 * called before calling this routine.
800 */
801
802static int
803octeon_droq_process_poll_pkts(struct octeon_device *oct,
804 struct octeon_droq *droq, u32 budget)
805{
806 struct list_head *tmp, *tmp2;
807 u32 pkts_available = 0, pkts_processed = 0;
808 u32 total_pkts_processed = 0;
809
810 if (budget > droq->max_count)
811 budget = droq->max_count;
812
813 spin_lock(&droq->lock);
814
815 while (total_pkts_processed < budget) {
Raghu Vatsavayicd8b1eb2016-08-31 11:03:22 -0700816 octeon_droq_check_hw_for_pkts(droq);
817
Raghu Vatsavayi97a25322016-11-14 15:54:47 -0800818 pkts_available = min((budget - total_pkts_processed),
819 (u32)(atomic_read(&droq->pkts_pending)));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700820
821 if (pkts_available == 0)
822 break;
823
824 pkts_processed =
825 octeon_droq_fast_process_packets(oct, droq,
826 pkts_available);
827
828 atomic_sub(pkts_processed, &droq->pkts_pending);
829
830 total_pkts_processed += pkts_processed;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700831 }
832
833 spin_unlock(&droq->lock);
834
835 list_for_each_safe(tmp, tmp2, &droq->dispatch_list) {
836 struct __dispatch *rdisp = (struct __dispatch *)tmp;
837
838 list_del(tmp);
839 rdisp->disp_fn(rdisp->rinfo,
840 octeon_get_dispatch_arg
841 (oct,
842 (u16)rdisp->rinfo->recv_pkt->rh.r.opcode,
843 (u16)rdisp->rinfo->recv_pkt->rh.r.subcode));
844 }
845
846 return total_pkts_processed;
847}
848
849int
850octeon_process_droq_poll_cmd(struct octeon_device *oct, u32 q_no, int cmd,
851 u32 arg)
852{
853 struct octeon_droq *droq;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700854
855 droq = oct->droq[q_no];
856
857 if (cmd == POLL_EVENT_PROCESS_PKTS)
858 return octeon_droq_process_poll_pkts(oct, droq, arg);
859
860 if (cmd == POLL_EVENT_PENDING_PKTS) {
861 u32 pkt_cnt = atomic_read(&droq->pkts_pending);
862
863 return octeon_droq_process_packets(oct, droq, pkt_cnt);
864 }
865
866 if (cmd == POLL_EVENT_ENABLE_INTR) {
867 u32 value;
868 unsigned long flags;
869
870 /* Enable Pkt Interrupt */
871 switch (oct->chip_id) {
872 case OCTEON_CN66XX:
873 case OCTEON_CN68XX: {
874 struct octeon_cn6xxx *cn6xxx =
875 (struct octeon_cn6xxx *)oct->chip;
876 spin_lock_irqsave
877 (&cn6xxx->lock_for_droq_int_enb_reg, flags);
878 value =
879 octeon_read_csr(oct,
880 CN6XXX_SLI_PKT_TIME_INT_ENB);
881 value |= (1 << q_no);
882 octeon_write_csr(oct,
883 CN6XXX_SLI_PKT_TIME_INT_ENB,
884 value);
885 value =
886 octeon_read_csr(oct,
887 CN6XXX_SLI_PKT_CNT_INT_ENB);
888 value |= (1 << q_no);
889 octeon_write_csr(oct,
890 CN6XXX_SLI_PKT_CNT_INT_ENB,
891 value);
892
893 /* don't bother flushing the enables */
894
895 spin_unlock_irqrestore
896 (&cn6xxx->lock_for_droq_int_enb_reg, flags);
897 return 0;
898 }
899 break;
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700900 case OCTEON_CN23XX_PF_VID: {
901 lio_enable_irq(oct->droq[q_no], oct->instr_queue[q_no]);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700902 }
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700903 break;
Raghu Vatsavayi9217c3c2016-12-07 08:54:37 -0800904
905 case OCTEON_CN23XX_VF_VID:
906 lio_enable_irq(oct->droq[q_no], oct->instr_queue[q_no]);
907 break;
Raghu Vatsavayi9ded1a52016-09-01 11:16:10 -0700908 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700909 return 0;
910 }
911
912 dev_err(&oct->pci_dev->dev, "%s Unknown command: %d\n", __func__, cmd);
913 return -EINVAL;
914}
915
916int octeon_register_droq_ops(struct octeon_device *oct, u32 q_no,
917 struct octeon_droq_ops *ops)
918{
919 struct octeon_droq *droq;
920 unsigned long flags;
921 struct octeon_config *oct_cfg = NULL;
922
923 oct_cfg = octeon_get_conf(oct);
924
925 if (!oct_cfg)
926 return -EINVAL;
927
928 if (!(ops)) {
929 dev_err(&oct->pci_dev->dev, "%s: droq_ops pointer is NULL\n",
930 __func__);
931 return -EINVAL;
932 }
933
934 if (q_no >= CFG_GET_OQ_MAX_Q(oct_cfg)) {
935 dev_err(&oct->pci_dev->dev, "%s: droq id (%d) exceeds MAX (%d)\n",
936 __func__, q_no, (oct->num_oqs - 1));
937 return -EINVAL;
938 }
939
940 droq = oct->droq[q_no];
941
942 spin_lock_irqsave(&droq->lock, flags);
943
944 memcpy(&droq->ops, ops, sizeof(struct octeon_droq_ops));
945
946 spin_unlock_irqrestore(&droq->lock, flags);
947
948 return 0;
949}
950
951int octeon_unregister_droq_ops(struct octeon_device *oct, u32 q_no)
952{
953 unsigned long flags;
954 struct octeon_droq *droq;
955 struct octeon_config *oct_cfg = NULL;
956
957 oct_cfg = octeon_get_conf(oct);
958
959 if (!oct_cfg)
960 return -EINVAL;
961
962 if (q_no >= CFG_GET_OQ_MAX_Q(oct_cfg)) {
963 dev_err(&oct->pci_dev->dev, "%s: droq id (%d) exceeds MAX (%d)\n",
964 __func__, q_no, oct->num_oqs - 1);
965 return -EINVAL;
966 }
967
968 droq = oct->droq[q_no];
969
970 if (!droq) {
971 dev_info(&oct->pci_dev->dev,
972 "Droq id (%d) not available.\n", q_no);
973 return 0;
974 }
975
976 spin_lock_irqsave(&droq->lock, flags);
977
978 droq->ops.fptr = NULL;
Raghu Vatsavayi0cece6c2016-06-14 16:54:50 -0700979 droq->ops.farg = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700980 droq->ops.drop_on_max = 0;
981
982 spin_unlock_irqrestore(&droq->lock, flags);
983
984 return 0;
985}
986
987int octeon_create_droq(struct octeon_device *oct,
988 u32 q_no, u32 num_descs,
989 u32 desc_size, void *app_ctx)
990{
991 struct octeon_droq *droq;
VSR Burrub3ca9af2017-03-09 17:03:24 -0800992 int numa_node = dev_to_node(&oct->pci_dev->dev);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700993
994 if (oct->droq[q_no]) {
995 dev_dbg(&oct->pci_dev->dev, "Droq already in use. Cannot create droq %d again\n",
996 q_no);
997 return 1;
998 }
999
1000 /* Allocate the DS for the new droq. */
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -07001001 droq = vmalloc_node(sizeof(*droq), numa_node);
1002 if (!droq)
1003 droq = vmalloc(sizeof(*droq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001004 if (!droq)
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001005 return -1;
1006
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001007 memset(droq, 0, sizeof(struct octeon_droq));
1008
1009 /*Disable the pkt o/p for this Q */
1010 octeon_set_droq_pkt_op(oct, q_no, 0);
1011 oct->droq[q_no] = droq;
1012
1013 /* Initialize the Droq */
Raghu Vatsavayi515e7522016-11-14 15:54:44 -08001014 if (octeon_init_droq(oct, q_no, num_descs, desc_size, app_ctx)) {
1015 vfree(oct->droq[q_no]);
1016 oct->droq[q_no] = NULL;
1017 return -1;
1018 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001019
1020 oct->num_oqs++;
1021
1022 dev_dbg(&oct->pci_dev->dev, "%s: Total number of OQ: %d\n", __func__,
1023 oct->num_oqs);
1024
1025 /* Global Droq register settings */
1026
1027 /* As of now not required, as setting are done for all 32 Droqs at
1028 * the same time.
1029 */
1030 return 0;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001031}