blob: a12beaa0c0e205cafcf1637d17c2c2724d9df95c [file] [log] [blame]
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001/**********************************************************************
2* Author: Cavium, Inc.
3*
4* Contact: support@cavium.com
5* Please include "LiquidIO" in the subject.
6*
7* Copyright (c) 2003-2015 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
17* details.
18*
19* This file may also be available under a different license from Cavium.
20* Contact Cavium, Inc. for more information
21**********************************************************************/
22#include <linux/version.h>
23#include <linux/types.h>
24#include <linux/list.h>
25#include <linux/pci.h>
26#include <linux/kthread.h>
27#include <linux/netdevice.h>
Raghu Vatsavayi5b173cf2015-06-12 18:11:50 -070028#include <linux/vmalloc.h>
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -070029#include "octeon_config.h"
30#include "liquidio_common.h"
31#include "octeon_droq.h"
32#include "octeon_iq.h"
33#include "response_manager.h"
34#include "octeon_device.h"
35#include "octeon_nic.h"
36#include "octeon_main.h"
37#include "octeon_network.h"
38#include "cn66xx_regs.h"
39#include "cn66xx_device.h"
40#include "cn68xx_regs.h"
41#include "cn68xx_device.h"
42#include "liquidio_image.h"
43#include "octeon_mem_ops.h"
44
45/* #define CAVIUM_ONLY_PERF_MODE */
46
47#define CVM_MIN(d1, d2) (((d1) < (d2)) ? (d1) : (d2))
48#define CVM_MAX(d1, d2) (((d1) > (d2)) ? (d1) : (d2))
49
50struct niclist {
51 struct list_head list;
52 void *ptr;
53};
54
55struct __dispatch {
56 struct list_head list;
57 struct octeon_recv_info *rinfo;
58 octeon_dispatch_fn_t disp_fn;
59};
60
61/** Get the argument that the user set when registering dispatch
62 * function for a given opcode/subcode.
63 * @param octeon_dev - the octeon device pointer.
64 * @param opcode - the opcode for which the dispatch argument
65 * is to be checked.
66 * @param subcode - the subcode for which the dispatch argument
67 * is to be checked.
68 * @return Success: void * (argument to the dispatch function)
69 * @return Failure: NULL
70 *
71 */
72static inline void *octeon_get_dispatch_arg(struct octeon_device *octeon_dev,
73 u16 opcode, u16 subcode)
74{
75 int idx;
76 struct list_head *dispatch;
77 void *fn_arg = NULL;
78 u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
79
80 idx = combined_opcode & OCTEON_OPCODE_MASK;
81
82 spin_lock_bh(&octeon_dev->dispatch.lock);
83
84 if (octeon_dev->dispatch.count == 0) {
85 spin_unlock_bh(&octeon_dev->dispatch.lock);
86 return NULL;
87 }
88
89 if (octeon_dev->dispatch.dlist[idx].opcode == combined_opcode) {
90 fn_arg = octeon_dev->dispatch.dlist[idx].arg;
91 } else {
92 list_for_each(dispatch,
93 &octeon_dev->dispatch.dlist[idx].list) {
94 if (((struct octeon_dispatch *)dispatch)->opcode ==
95 combined_opcode) {
96 fn_arg = ((struct octeon_dispatch *)
97 dispatch)->arg;
98 break;
99 }
100 }
101 }
102
103 spin_unlock_bh(&octeon_dev->dispatch.lock);
104 return fn_arg;
105}
106
107u32 octeon_droq_check_hw_for_pkts(struct octeon_device *oct,
108 struct octeon_droq *droq)
109{
110 u32 pkt_count = 0;
111
112 pkt_count = readl(droq->pkts_sent_reg);
113 if (pkt_count) {
114 atomic_add(pkt_count, &droq->pkts_pending);
115 writel(pkt_count, droq->pkts_sent_reg);
116 }
117
118 return pkt_count;
119}
120
121static void octeon_droq_compute_max_packet_bufs(struct octeon_droq *droq)
122{
123 u32 count = 0;
124
125 /* max_empty_descs is the max. no. of descs that can have no buffers.
126 * If the empty desc count goes beyond this value, we cannot safely
127 * read in a 64K packet sent by Octeon
128 * (64K is max pkt size from Octeon)
129 */
130 droq->max_empty_descs = 0;
131
132 do {
133 droq->max_empty_descs++;
134 count += droq->buffer_size;
135 } while (count < (64 * 1024));
136
137 droq->max_empty_descs = droq->max_count - droq->max_empty_descs;
138}
139
140static void octeon_droq_reset_indices(struct octeon_droq *droq)
141{
142 droq->read_idx = 0;
143 droq->write_idx = 0;
144 droq->refill_idx = 0;
145 droq->refill_count = 0;
146 atomic_set(&droq->pkts_pending, 0);
147}
148
149static void
150octeon_droq_destroy_ring_buffers(struct octeon_device *oct,
151 struct octeon_droq *droq)
152{
153 u32 i;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700154 struct octeon_skb_page_info *pg_info;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700155
156 for (i = 0; i < droq->max_count; i++) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700157 pg_info = &droq->recv_buf_list[i].pg_info;
158
159 if (pg_info->dma)
160 lio_unmap_ring(oct->pci_dev,
161 (u64)pg_info->dma);
162 pg_info->dma = 0;
163
164 if (pg_info->page)
165 recv_buffer_destroy(droq->recv_buf_list[i].buffer,
166 pg_info);
167
168 if (droq->desc_ring && droq->desc_ring[i].info_ptr)
169 lio_unmap_ring_info(oct->pci_dev,
170 (u64)droq->
171 desc_ring[i].info_ptr,
172 OCT_DROQ_INFO_SIZE);
173 droq->recv_buf_list[i].buffer = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700174 }
175
176 octeon_droq_reset_indices(droq);
177}
178
179static int
180octeon_droq_setup_ring_buffers(struct octeon_device *oct,
181 struct octeon_droq *droq)
182{
183 u32 i;
184 void *buf;
185 struct octeon_droq_desc *desc_ring = droq->desc_ring;
186
187 for (i = 0; i < droq->max_count; i++) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700188 buf = recv_buffer_alloc(oct, &droq->recv_buf_list[i].pg_info);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700189
190 if (!buf) {
191 dev_err(&oct->pci_dev->dev, "%s buffer alloc failed\n",
192 __func__);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700193 droq->stats.rx_alloc_failure++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700194 return -ENOMEM;
195 }
196
197 droq->recv_buf_list[i].buffer = buf;
198 droq->recv_buf_list[i].data = get_rbd(buf);
199
200 droq->info_list[i].length = 0;
201
202 /* map ring buffers into memory */
203 desc_ring[i].info_ptr = lio_map_ring_info(droq, i);
204 desc_ring[i].buffer_ptr =
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700205 lio_map_ring(droq->recv_buf_list[i].buffer);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700206 }
207
208 octeon_droq_reset_indices(droq);
209
210 octeon_droq_compute_max_packet_bufs(droq);
211
212 return 0;
213}
214
215int octeon_delete_droq(struct octeon_device *oct, u32 q_no)
216{
217 struct octeon_droq *droq = oct->droq[q_no];
218
219 dev_dbg(&oct->pci_dev->dev, "%s[%d]\n", __func__, q_no);
220
221 octeon_droq_destroy_ring_buffers(oct, droq);
Markus Elfring9686f312015-06-29 12:22:24 +0200222 vfree(droq->recv_buf_list);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700223
224 if (droq->info_base_addr)
225 cnnic_free_aligned_dma(oct->pci_dev, droq->info_list,
226 droq->info_alloc_size,
227 droq->info_base_addr,
228 droq->info_list_dma);
229
230 if (droq->desc_ring)
231 lio_dma_free(oct, (droq->max_count * OCT_DROQ_DESC_SIZE),
232 droq->desc_ring, droq->desc_ring_dma);
233
234 memset(droq, 0, OCT_DROQ_SIZE);
235
236 return 0;
237}
238
239int octeon_init_droq(struct octeon_device *oct,
240 u32 q_no,
241 u32 num_descs,
242 u32 desc_size,
243 void *app_ctx)
244{
245 struct octeon_droq *droq;
246 u32 desc_ring_size = 0, c_num_descs = 0, c_buf_size = 0;
247 u32 c_pkts_per_intr = 0, c_refill_threshold = 0;
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700248 int orig_node = dev_to_node(&oct->pci_dev->dev);
249 int numa_node = cpu_to_node(q_no % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700250
251 dev_dbg(&oct->pci_dev->dev, "%s[%d]\n", __func__, q_no);
252
253 droq = oct->droq[q_no];
254 memset(droq, 0, OCT_DROQ_SIZE);
255
256 droq->oct_dev = oct;
257 droq->q_no = q_no;
258 if (app_ctx)
259 droq->app_ctx = app_ctx;
260 else
261 droq->app_ctx = (void *)(size_t)q_no;
262
263 c_num_descs = num_descs;
264 c_buf_size = desc_size;
265 if (OCTEON_CN6XXX(oct)) {
266 struct octeon_config *conf6x = CHIP_FIELD(oct, cn6xxx, conf);
267
268 c_pkts_per_intr = (u32)CFG_GET_OQ_PKTS_PER_INTR(conf6x);
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700269 c_refill_threshold =
270 (u32)CFG_GET_OQ_REFILL_THRESHOLD(conf6x);
271 } else {
272 return 1;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700273 }
274
275 droq->max_count = c_num_descs;
276 droq->buffer_size = c_buf_size;
277
278 desc_ring_size = droq->max_count * OCT_DROQ_DESC_SIZE;
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700279 set_dev_node(&oct->pci_dev->dev, numa_node);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700280 droq->desc_ring = lio_dma_alloc(oct, desc_ring_size,
281 (dma_addr_t *)&droq->desc_ring_dma);
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700282 set_dev_node(&oct->pci_dev->dev, orig_node);
283 if (!droq->desc_ring)
284 droq->desc_ring = lio_dma_alloc(oct, desc_ring_size,
285 (dma_addr_t *)&droq->desc_ring_dma);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700286
287 if (!droq->desc_ring) {
288 dev_err(&oct->pci_dev->dev,
289 "Output queue %d ring alloc failed\n", q_no);
290 return 1;
291 }
292
293 dev_dbg(&oct->pci_dev->dev, "droq[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
294 q_no, droq->desc_ring, droq->desc_ring_dma);
295 dev_dbg(&oct->pci_dev->dev, "droq[%d]: num_desc: %d\n", q_no,
296 droq->max_count);
297
298 droq->info_list =
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700299 cnnic_numa_alloc_aligned_dma((droq->max_count *
300 OCT_DROQ_INFO_SIZE),
301 &droq->info_alloc_size,
302 &droq->info_base_addr,
303 numa_node);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700304 if (!droq->info_list) {
305 dev_err(&oct->pci_dev->dev, "Cannot allocate memory for info list.\n");
306 lio_dma_free(oct, (droq->max_count * OCT_DROQ_DESC_SIZE),
307 droq->desc_ring, droq->desc_ring_dma);
308 return 1;
309 }
310
311 droq->recv_buf_list = (struct octeon_recv_buffer *)
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700312 vmalloc_node(droq->max_count *
313 OCT_DROQ_RECVBUF_SIZE,
314 numa_node);
315 if (!droq->recv_buf_list)
316 droq->recv_buf_list = (struct octeon_recv_buffer *)
317 vmalloc(droq->max_count *
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700318 OCT_DROQ_RECVBUF_SIZE);
319 if (!droq->recv_buf_list) {
320 dev_err(&oct->pci_dev->dev, "Output queue recv buf list alloc failed\n");
321 goto init_droq_fail;
322 }
323
324 if (octeon_droq_setup_ring_buffers(oct, droq))
325 goto init_droq_fail;
326
327 droq->pkts_per_intr = c_pkts_per_intr;
328 droq->refill_threshold = c_refill_threshold;
329
330 dev_dbg(&oct->pci_dev->dev, "DROQ INIT: max_empty_descs: %d\n",
331 droq->max_empty_descs);
332
333 spin_lock_init(&droq->lock);
334
335 INIT_LIST_HEAD(&droq->dispatch_list);
336
337 /* For 56xx Pass1, this function won't be called, so no checks. */
338 oct->fn_list.setup_oq_regs(oct, q_no);
339
340 oct->io_qmask.oq |= (1 << q_no);
341
342 return 0;
343
344init_droq_fail:
345 octeon_delete_droq(oct, q_no);
346 return 1;
347}
348
349/* octeon_create_recv_info
350 * Parameters:
351 * octeon_dev - pointer to the octeon device structure
352 * droq - droq in which the packet arrived.
353 * buf_cnt - no. of buffers used by the packet.
354 * idx - index in the descriptor for the first buffer in the packet.
355 * Description:
356 * Allocates a recv_info_t and copies the buffer addresses for packet data
357 * into the recv_pkt space which starts at an 8B offset from recv_info_t.
358 * Flags the descriptors for refill later. If available descriptors go
359 * below the threshold to receive a 64K pkt, new buffers are first allocated
360 * before the recv_pkt_t is created.
361 * This routine will be called in interrupt context.
362 * Returns:
363 * Success: Pointer to recv_info_t
364 * Failure: NULL.
365 * Locks:
366 * The droq->lock is held when this routine is called.
367 */
368static inline struct octeon_recv_info *octeon_create_recv_info(
369 struct octeon_device *octeon_dev,
370 struct octeon_droq *droq,
371 u32 buf_cnt,
372 u32 idx)
373{
374 struct octeon_droq_info *info;
375 struct octeon_recv_pkt *recv_pkt;
376 struct octeon_recv_info *recv_info;
377 u32 i, bytes_left;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700378 struct octeon_skb_page_info *pg_info;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700379
380 info = &droq->info_list[idx];
381
382 recv_info = octeon_alloc_recv_info(sizeof(struct __dispatch));
383 if (!recv_info)
384 return NULL;
385
386 recv_pkt = recv_info->recv_pkt;
387 recv_pkt->rh = info->rh;
388 recv_pkt->length = (u32)info->length;
389 recv_pkt->buffer_count = (u16)buf_cnt;
390 recv_pkt->octeon_id = (u16)octeon_dev->octeon_id;
391
392 i = 0;
393 bytes_left = (u32)info->length;
394
395 while (buf_cnt) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700396 {
397 pg_info = &droq->recv_buf_list[idx].pg_info;
398
399 lio_unmap_ring(octeon_dev->pci_dev,
400 (u64)pg_info->dma);
401 pg_info->page = NULL;
402 pg_info->dma = 0;
403 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700404
405 recv_pkt->buffer_size[i] =
406 (bytes_left >=
407 droq->buffer_size) ? droq->buffer_size : bytes_left;
408
409 recv_pkt->buffer_ptr[i] = droq->recv_buf_list[idx].buffer;
410 droq->recv_buf_list[idx].buffer = NULL;
411
412 INCR_INDEX_BY1(idx, droq->max_count);
413 bytes_left -= droq->buffer_size;
414 i++;
415 buf_cnt--;
416 }
417
418 return recv_info;
419}
420
421/* If we were not able to refill all buffers, try to move around
422 * the buffers that were not dispatched.
423 */
424static inline u32
425octeon_droq_refill_pullup_descs(struct octeon_droq *droq,
426 struct octeon_droq_desc *desc_ring)
427{
428 u32 desc_refilled = 0;
429
430 u32 refill_index = droq->refill_idx;
431
432 while (refill_index != droq->read_idx) {
433 if (droq->recv_buf_list[refill_index].buffer) {
434 droq->recv_buf_list[droq->refill_idx].buffer =
435 droq->recv_buf_list[refill_index].buffer;
436 droq->recv_buf_list[droq->refill_idx].data =
437 droq->recv_buf_list[refill_index].data;
438 desc_ring[droq->refill_idx].buffer_ptr =
439 desc_ring[refill_index].buffer_ptr;
440 droq->recv_buf_list[refill_index].buffer = NULL;
441 desc_ring[refill_index].buffer_ptr = 0;
442 do {
443 INCR_INDEX_BY1(droq->refill_idx,
444 droq->max_count);
445 desc_refilled++;
446 droq->refill_count--;
447 } while (droq->recv_buf_list[droq->refill_idx].
448 buffer);
449 }
450 INCR_INDEX_BY1(refill_index, droq->max_count);
451 } /* while */
452 return desc_refilled;
453}
454
455/* octeon_droq_refill
456 * Parameters:
457 * droq - droq in which descriptors require new buffers.
458 * Description:
459 * Called during normal DROQ processing in interrupt mode or by the poll
460 * thread to refill the descriptors from which buffers were dispatched
461 * to upper layers. Attempts to allocate new buffers. If that fails, moves
462 * up buffers (that were not dispatched) to form a contiguous ring.
463 * Returns:
464 * No of descriptors refilled.
465 * Locks:
466 * This routine is called with droq->lock held.
467 */
468static u32
469octeon_droq_refill(struct octeon_device *octeon_dev, struct octeon_droq *droq)
470{
471 struct octeon_droq_desc *desc_ring;
472 void *buf = NULL;
473 u8 *data;
474 u32 desc_refilled = 0;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700475 struct octeon_skb_page_info *pg_info;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700476
477 desc_ring = droq->desc_ring;
478
479 while (droq->refill_count && (desc_refilled < droq->max_count)) {
480 /* If a valid buffer exists (happens if there is no dispatch),
481 * reuse
482 * the buffer, else allocate.
483 */
484 if (!droq->recv_buf_list[droq->refill_idx].buffer) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700485 pg_info =
486 &droq->recv_buf_list[droq->refill_idx].pg_info;
487 /* Either recycle the existing pages or go for
488 * new page alloc
489 */
490 if (pg_info->page)
491 buf = recv_buffer_reuse(octeon_dev, pg_info);
492 else
493 buf = recv_buffer_alloc(octeon_dev, pg_info);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700494 /* If a buffer could not be allocated, no point in
495 * continuing
496 */
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700497 if (!buf) {
498 droq->stats.rx_alloc_failure++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700499 break;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700500 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700501 droq->recv_buf_list[droq->refill_idx].buffer =
502 buf;
503 data = get_rbd(buf);
504 } else {
505 data = get_rbd(droq->recv_buf_list
506 [droq->refill_idx].buffer);
507 }
508
509 droq->recv_buf_list[droq->refill_idx].data = data;
510
511 desc_ring[droq->refill_idx].buffer_ptr =
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700512 lio_map_ring(droq->recv_buf_list[droq->
513 refill_idx].buffer);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700514 /* Reset any previous values in the length field. */
515 droq->info_list[droq->refill_idx].length = 0;
516
517 INCR_INDEX_BY1(droq->refill_idx, droq->max_count);
518 desc_refilled++;
519 droq->refill_count--;
520 }
521
522 if (droq->refill_count)
523 desc_refilled +=
524 octeon_droq_refill_pullup_descs(droq, desc_ring);
525
526 /* if droq->refill_count
527 * The refill count would not change in pass two. We only moved buffers
528 * to close the gap in the ring, but we would still have the same no. of
529 * buffers to refill.
530 */
531 return desc_refilled;
532}
533
534static inline u32
535octeon_droq_get_bufcount(u32 buf_size, u32 total_len)
536{
537 u32 buf_cnt = 0;
538
539 while (total_len > (buf_size * buf_cnt))
540 buf_cnt++;
541 return buf_cnt;
542}
543
544static int
545octeon_droq_dispatch_pkt(struct octeon_device *oct,
546 struct octeon_droq *droq,
547 union octeon_rh *rh,
548 struct octeon_droq_info *info)
549{
550 u32 cnt;
551 octeon_dispatch_fn_t disp_fn;
552 struct octeon_recv_info *rinfo;
553
554 cnt = octeon_droq_get_bufcount(droq->buffer_size, (u32)info->length);
555
556 disp_fn = octeon_get_dispatch(oct, (u16)rh->r.opcode,
557 (u16)rh->r.subcode);
558 if (disp_fn) {
559 rinfo = octeon_create_recv_info(oct, droq, cnt, droq->read_idx);
560 if (rinfo) {
561 struct __dispatch *rdisp = rinfo->rsvd;
562
563 rdisp->rinfo = rinfo;
564 rdisp->disp_fn = disp_fn;
565 rinfo->recv_pkt->rh = *rh;
566 list_add_tail(&rdisp->list,
567 &droq->dispatch_list);
568 } else {
569 droq->stats.dropped_nomem++;
570 }
571 } else {
572 dev_err(&oct->pci_dev->dev, "DROQ: No dispatch function\n");
573 droq->stats.dropped_nodispatch++;
574 } /* else (dispatch_fn ... */
575
576 return cnt;
577}
578
579static inline void octeon_droq_drop_packets(struct octeon_device *oct,
580 struct octeon_droq *droq,
581 u32 cnt)
582{
583 u32 i = 0, buf_cnt;
584 struct octeon_droq_info *info;
585
586 for (i = 0; i < cnt; i++) {
587 info = &droq->info_list[droq->read_idx];
588 octeon_swap_8B_data((u64 *)info, 2);
589
590 if (info->length) {
591 info->length -= OCT_RH_SIZE;
592 droq->stats.bytes_received += info->length;
593 buf_cnt = octeon_droq_get_bufcount(droq->buffer_size,
594 (u32)info->length);
595 } else {
596 dev_err(&oct->pci_dev->dev, "DROQ: In drop: pkt with len 0\n");
597 buf_cnt = 1;
598 }
599
600 INCR_INDEX(droq->read_idx, buf_cnt, droq->max_count);
601 droq->refill_count += buf_cnt;
602 }
603}
604
605static u32
606octeon_droq_fast_process_packets(struct octeon_device *oct,
607 struct octeon_droq *droq,
608 u32 pkts_to_process)
609{
610 struct octeon_droq_info *info;
611 union octeon_rh *rh;
612 u32 pkt, total_len = 0, pkt_count;
613
614 pkt_count = pkts_to_process;
615
616 for (pkt = 0; pkt < pkt_count; pkt++) {
617 u32 pkt_len = 0;
618 struct sk_buff *nicbuf = NULL;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700619 struct octeon_skb_page_info *pg_info;
620 void *buf;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700621
622 info = &droq->info_list[droq->read_idx];
623 octeon_swap_8B_data((u64 *)info, 2);
624
625 if (!info->length) {
626 dev_err(&oct->pci_dev->dev,
627 "DROQ[%d] idx: %d len:0, pkt_cnt: %d\n",
628 droq->q_no, droq->read_idx, pkt_count);
629 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS,
630 (u8 *)info,
631 OCT_DROQ_INFO_SIZE);
632 break;
633 }
634
635 /* Len of resp hdr in included in the received data len. */
636 info->length -= OCT_RH_SIZE;
637 rh = &info->rh;
638
639 total_len += (u32)info->length;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700640 if (OPCODE_SLOW_PATH(rh)) {
641 u32 buf_cnt;
642
643 buf_cnt = octeon_droq_dispatch_pkt(oct, droq, rh, info);
644 INCR_INDEX(droq->read_idx, buf_cnt, droq->max_count);
645 droq->refill_count += buf_cnt;
646 } else {
647 if (info->length <= droq->buffer_size) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700648 pkt_len = (u32)info->length;
649 nicbuf = droq->recv_buf_list[
650 droq->read_idx].buffer;
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700651 pg_info = &droq->recv_buf_list[
652 droq->read_idx].pg_info;
653 if (recv_buffer_recycle(oct, pg_info))
654 pg_info->page = NULL;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700655 droq->recv_buf_list[droq->read_idx].buffer =
656 NULL;
657 INCR_INDEX_BY1(droq->read_idx, droq->max_count);
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700658 droq->refill_count++;
659 } else {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700660 nicbuf = octeon_fast_packet_alloc((u32)
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700661 info->length);
662 pkt_len = 0;
663 /* nicbuf allocation can fail. We'll handle it
664 * inside the loop.
665 */
666 while (pkt_len < info->length) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700667 int cpy_len, idx = droq->read_idx;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700668
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700669 cpy_len = ((pkt_len + droq->buffer_size)
670 > info->length) ?
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700671 ((u32)info->length - pkt_len) :
672 droq->buffer_size;
673
674 if (nicbuf) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700675 octeon_fast_packet_next(droq,
676 nicbuf,
677 cpy_len,
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700678 idx);
679 buf = droq->recv_buf_list[idx].
680 buffer;
681 recv_buffer_fast_free(buf);
682 droq->recv_buf_list[idx].buffer
683 = NULL;
684 } else {
685 droq->stats.rx_alloc_failure++;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700686 }
687
688 pkt_len += cpy_len;
689 INCR_INDEX_BY1(droq->read_idx,
690 droq->max_count);
691 droq->refill_count++;
692 }
693 }
694
695 if (nicbuf) {
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700696 if (droq->ops.fptr) {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700697 droq->ops.fptr(oct->octeon_id,
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700698 nicbuf, pkt_len,
699 rh, &droq->napi);
700 } else {
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700701 recv_buffer_free(nicbuf);
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700702 }
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700703 }
704 }
705
706 if (droq->refill_count >= droq->refill_threshold) {
707 int desc_refilled = octeon_droq_refill(oct, droq);
708
709 /* Flush the droq descriptor data to memory to be sure
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700710 * that when we update the credits the data in memory
711 * is accurate.
712 */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700713 wmb();
714 writel((desc_refilled), droq->pkts_credit_reg);
715 /* make sure mmio write completes */
716 mmiowb();
717 }
718
Raghu Vatsavayicabeb132016-06-14 16:54:47 -0700719 } /* for (each packet)... */
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700720
721 /* Increment refill_count by the number of buffers processed. */
722 droq->stats.pkts_received += pkt;
723 droq->stats.bytes_received += total_len;
724
725 if ((droq->ops.drop_on_max) && (pkts_to_process - pkt)) {
726 octeon_droq_drop_packets(oct, droq, (pkts_to_process - pkt));
727
728 droq->stats.dropped_toomany += (pkts_to_process - pkt);
729 return pkts_to_process;
730 }
731
732 return pkt;
733}
734
735int
736octeon_droq_process_packets(struct octeon_device *oct,
737 struct octeon_droq *droq,
738 u32 budget)
739{
740 u32 pkt_count = 0, pkts_processed = 0;
741 struct list_head *tmp, *tmp2;
742
743 pkt_count = atomic_read(&droq->pkts_pending);
744 if (!pkt_count)
745 return 0;
746
747 if (pkt_count > budget)
748 pkt_count = budget;
749
750 /* Grab the lock */
751 spin_lock(&droq->lock);
752
753 pkts_processed = octeon_droq_fast_process_packets(oct, droq, pkt_count);
754
755 atomic_sub(pkts_processed, &droq->pkts_pending);
756
757 /* Release the spin lock */
758 spin_unlock(&droq->lock);
759
760 list_for_each_safe(tmp, tmp2, &droq->dispatch_list) {
761 struct __dispatch *rdisp = (struct __dispatch *)tmp;
762
763 list_del(tmp);
764 rdisp->disp_fn(rdisp->rinfo,
765 octeon_get_dispatch_arg
766 (oct,
767 (u16)rdisp->rinfo->recv_pkt->rh.r.opcode,
768 (u16)rdisp->rinfo->recv_pkt->rh.r.subcode));
769 }
770
771 /* If there are packets pending. schedule tasklet again */
772 if (atomic_read(&droq->pkts_pending))
773 return 1;
774
775 return 0;
776}
777
778/**
779 * Utility function to poll for packets. check_hw_for_packets must be
780 * called before calling this routine.
781 */
782
783static int
784octeon_droq_process_poll_pkts(struct octeon_device *oct,
785 struct octeon_droq *droq, u32 budget)
786{
787 struct list_head *tmp, *tmp2;
788 u32 pkts_available = 0, pkts_processed = 0;
789 u32 total_pkts_processed = 0;
790
791 if (budget > droq->max_count)
792 budget = droq->max_count;
793
794 spin_lock(&droq->lock);
795
796 while (total_pkts_processed < budget) {
797 pkts_available =
798 CVM_MIN((budget - total_pkts_processed),
799 (u32)(atomic_read(&droq->pkts_pending)));
800
801 if (pkts_available == 0)
802 break;
803
804 pkts_processed =
805 octeon_droq_fast_process_packets(oct, droq,
806 pkts_available);
807
808 atomic_sub(pkts_processed, &droq->pkts_pending);
809
810 total_pkts_processed += pkts_processed;
811
812 octeon_droq_check_hw_for_pkts(oct, droq);
813 }
814
815 spin_unlock(&droq->lock);
816
817 list_for_each_safe(tmp, tmp2, &droq->dispatch_list) {
818 struct __dispatch *rdisp = (struct __dispatch *)tmp;
819
820 list_del(tmp);
821 rdisp->disp_fn(rdisp->rinfo,
822 octeon_get_dispatch_arg
823 (oct,
824 (u16)rdisp->rinfo->recv_pkt->rh.r.opcode,
825 (u16)rdisp->rinfo->recv_pkt->rh.r.subcode));
826 }
827
828 return total_pkts_processed;
829}
830
831int
832octeon_process_droq_poll_cmd(struct octeon_device *oct, u32 q_no, int cmd,
833 u32 arg)
834{
835 struct octeon_droq *droq;
836 struct octeon_config *oct_cfg = NULL;
837
838 oct_cfg = octeon_get_conf(oct);
839
840 if (!oct_cfg)
841 return -EINVAL;
842
843 if (q_no >= CFG_GET_OQ_MAX_Q(oct_cfg)) {
844 dev_err(&oct->pci_dev->dev, "%s: droq id (%d) exceeds MAX (%d)\n",
845 __func__, q_no, (oct->num_oqs - 1));
846 return -EINVAL;
847 }
848
849 droq = oct->droq[q_no];
850
851 if (cmd == POLL_EVENT_PROCESS_PKTS)
852 return octeon_droq_process_poll_pkts(oct, droq, arg);
853
854 if (cmd == POLL_EVENT_PENDING_PKTS) {
855 u32 pkt_cnt = atomic_read(&droq->pkts_pending);
856
857 return octeon_droq_process_packets(oct, droq, pkt_cnt);
858 }
859
860 if (cmd == POLL_EVENT_ENABLE_INTR) {
861 u32 value;
862 unsigned long flags;
863
864 /* Enable Pkt Interrupt */
865 switch (oct->chip_id) {
866 case OCTEON_CN66XX:
867 case OCTEON_CN68XX: {
868 struct octeon_cn6xxx *cn6xxx =
869 (struct octeon_cn6xxx *)oct->chip;
870 spin_lock_irqsave
871 (&cn6xxx->lock_for_droq_int_enb_reg, flags);
872 value =
873 octeon_read_csr(oct,
874 CN6XXX_SLI_PKT_TIME_INT_ENB);
875 value |= (1 << q_no);
876 octeon_write_csr(oct,
877 CN6XXX_SLI_PKT_TIME_INT_ENB,
878 value);
879 value =
880 octeon_read_csr(oct,
881 CN6XXX_SLI_PKT_CNT_INT_ENB);
882 value |= (1 << q_no);
883 octeon_write_csr(oct,
884 CN6XXX_SLI_PKT_CNT_INT_ENB,
885 value);
886
887 /* don't bother flushing the enables */
888
889 spin_unlock_irqrestore
890 (&cn6xxx->lock_for_droq_int_enb_reg, flags);
891 return 0;
892 }
893 break;
894 }
895
896 return 0;
897 }
898
899 dev_err(&oct->pci_dev->dev, "%s Unknown command: %d\n", __func__, cmd);
900 return -EINVAL;
901}
902
903int octeon_register_droq_ops(struct octeon_device *oct, u32 q_no,
904 struct octeon_droq_ops *ops)
905{
906 struct octeon_droq *droq;
907 unsigned long flags;
908 struct octeon_config *oct_cfg = NULL;
909
910 oct_cfg = octeon_get_conf(oct);
911
912 if (!oct_cfg)
913 return -EINVAL;
914
915 if (!(ops)) {
916 dev_err(&oct->pci_dev->dev, "%s: droq_ops pointer is NULL\n",
917 __func__);
918 return -EINVAL;
919 }
920
921 if (q_no >= CFG_GET_OQ_MAX_Q(oct_cfg)) {
922 dev_err(&oct->pci_dev->dev, "%s: droq id (%d) exceeds MAX (%d)\n",
923 __func__, q_no, (oct->num_oqs - 1));
924 return -EINVAL;
925 }
926
927 droq = oct->droq[q_no];
928
929 spin_lock_irqsave(&droq->lock, flags);
930
931 memcpy(&droq->ops, ops, sizeof(struct octeon_droq_ops));
932
933 spin_unlock_irqrestore(&droq->lock, flags);
934
935 return 0;
936}
937
938int octeon_unregister_droq_ops(struct octeon_device *oct, u32 q_no)
939{
940 unsigned long flags;
941 struct octeon_droq *droq;
942 struct octeon_config *oct_cfg = NULL;
943
944 oct_cfg = octeon_get_conf(oct);
945
946 if (!oct_cfg)
947 return -EINVAL;
948
949 if (q_no >= CFG_GET_OQ_MAX_Q(oct_cfg)) {
950 dev_err(&oct->pci_dev->dev, "%s: droq id (%d) exceeds MAX (%d)\n",
951 __func__, q_no, oct->num_oqs - 1);
952 return -EINVAL;
953 }
954
955 droq = oct->droq[q_no];
956
957 if (!droq) {
958 dev_info(&oct->pci_dev->dev,
959 "Droq id (%d) not available.\n", q_no);
960 return 0;
961 }
962
963 spin_lock_irqsave(&droq->lock, flags);
964
965 droq->ops.fptr = NULL;
966 droq->ops.drop_on_max = 0;
967
968 spin_unlock_irqrestore(&droq->lock, flags);
969
970 return 0;
971}
972
973int octeon_create_droq(struct octeon_device *oct,
974 u32 q_no, u32 num_descs,
975 u32 desc_size, void *app_ctx)
976{
977 struct octeon_droq *droq;
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700978 int numa_node = cpu_to_node(q_no % num_online_cpus());
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700979
980 if (oct->droq[q_no]) {
981 dev_dbg(&oct->pci_dev->dev, "Droq already in use. Cannot create droq %d again\n",
982 q_no);
983 return 1;
984 }
985
986 /* Allocate the DS for the new droq. */
Raghu Vatsavayi96ae48b2016-06-14 16:54:46 -0700987 droq = vmalloc_node(sizeof(*droq), numa_node);
988 if (!droq)
989 droq = vmalloc(sizeof(*droq));
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -0700990 if (!droq)
991 goto create_droq_fail;
992 memset(droq, 0, sizeof(struct octeon_droq));
993
994 /*Disable the pkt o/p for this Q */
995 octeon_set_droq_pkt_op(oct, q_no, 0);
996 oct->droq[q_no] = droq;
997
998 /* Initialize the Droq */
999 octeon_init_droq(oct, q_no, num_descs, desc_size, app_ctx);
1000
1001 oct->num_oqs++;
1002
1003 dev_dbg(&oct->pci_dev->dev, "%s: Total number of OQ: %d\n", __func__,
1004 oct->num_oqs);
1005
1006 /* Global Droq register settings */
1007
1008 /* As of now not required, as setting are done for all 32 Droqs at
1009 * the same time.
1010 */
1011 return 0;
1012
1013create_droq_fail:
1014 octeon_delete_droq(oct, q_no);
Amitoj Kaur Chawla08a965e2016-02-04 19:25:13 +05301015 return -ENOMEM;
Raghu Vatsavayif21fb3e2015-06-09 18:15:23 -07001016}