octeon_droq.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /**********************************************************************
  2. * 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. ***********************************************************************/
  18. #include <linux/pci.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/vmalloc.h>
  21. #include "liquidio_common.h"
  22. #include "octeon_droq.h"
  23. #include "octeon_iq.h"
  24. #include "response_manager.h"
  25. #include "octeon_device.h"
  26. #include "octeon_main.h"
  27. #include "octeon_network.h"
  28. #include "cn66xx_regs.h"
  29. #include "cn66xx_device.h"
  30. #include "cn23xx_pf_device.h"
  31. #include "cn23xx_vf_device.h"
  32. struct niclist {
  33. struct list_head list;
  34. void *ptr;
  35. };
  36. struct __dispatch {
  37. struct list_head list;
  38. struct octeon_recv_info *rinfo;
  39. octeon_dispatch_fn_t disp_fn;
  40. };
  41. /** Get the argument that the user set when registering dispatch
  42. * function for a given opcode/subcode.
  43. * @param octeon_dev - the octeon device pointer.
  44. * @param opcode - the opcode for which the dispatch argument
  45. * is to be checked.
  46. * @param subcode - the subcode for which the dispatch argument
  47. * is to be checked.
  48. * @return Success: void * (argument to the dispatch function)
  49. * @return Failure: NULL
  50. *
  51. */
  52. static inline void *octeon_get_dispatch_arg(struct octeon_device *octeon_dev,
  53. u16 opcode, u16 subcode)
  54. {
  55. int idx;
  56. struct list_head *dispatch;
  57. void *fn_arg = NULL;
  58. u16 combined_opcode = OPCODE_SUBCODE(opcode, subcode);
  59. idx = combined_opcode & OCTEON_OPCODE_MASK;
  60. spin_lock_bh(&octeon_dev->dispatch.lock);
  61. if (octeon_dev->dispatch.count == 0) {
  62. spin_unlock_bh(&octeon_dev->dispatch.lock);
  63. return NULL;
  64. }
  65. if (octeon_dev->dispatch.dlist[idx].opcode == combined_opcode) {
  66. fn_arg = octeon_dev->dispatch.dlist[idx].arg;
  67. } else {
  68. list_for_each(dispatch,
  69. &octeon_dev->dispatch.dlist[idx].list) {
  70. if (((struct octeon_dispatch *)dispatch)->opcode ==
  71. combined_opcode) {
  72. fn_arg = ((struct octeon_dispatch *)
  73. dispatch)->arg;
  74. break;
  75. }
  76. }
  77. }
  78. spin_unlock_bh(&octeon_dev->dispatch.lock);
  79. return fn_arg;
  80. }
  81. /** Check for packets on Droq. This function should be called with lock held.
  82. * @param droq - Droq on which count is checked.
  83. * @return Returns packet count.
  84. */
  85. u32 octeon_droq_check_hw_for_pkts(struct octeon_droq *droq)
  86. {
  87. u32 pkt_count = 0;
  88. u32 last_count;
  89. pkt_count = readl(droq->pkts_sent_reg);
  90. last_count = pkt_count - droq->pkt_count;
  91. droq->pkt_count = pkt_count;
  92. /* we shall write to cnts at napi irq enable or end of droq tasklet */
  93. if (last_count)
  94. atomic_add(last_count, &droq->pkts_pending);
  95. return last_count;
  96. }
  97. static void octeon_droq_compute_max_packet_bufs(struct octeon_droq *droq)
  98. {
  99. u32 count = 0;
  100. /* max_empty_descs is the max. no. of descs that can have no buffers.
  101. * If the empty desc count goes beyond this value, we cannot safely
  102. * read in a 64K packet sent by Octeon
  103. * (64K is max pkt size from Octeon)
  104. */
  105. droq->max_empty_descs = 0;
  106. do {
  107. droq->max_empty_descs++;
  108. count += droq->buffer_size;
  109. } while (count < (64 * 1024));
  110. droq->max_empty_descs = droq->max_count - droq->max_empty_descs;
  111. }
  112. static void octeon_droq_reset_indices(struct octeon_droq *droq)
  113. {
  114. droq->read_idx = 0;
  115. droq->write_idx = 0;
  116. droq->refill_idx = 0;
  117. droq->refill_count = 0;
  118. atomic_set(&droq->pkts_pending, 0);
  119. }
  120. static void
  121. octeon_droq_destroy_ring_buffers(struct octeon_device *oct,
  122. struct octeon_droq *droq)
  123. {
  124. u32 i;
  125. struct octeon_skb_page_info *pg_info;
  126. for (i = 0; i < droq->max_count; i++) {
  127. pg_info = &droq->recv_buf_list[i].pg_info;
  128. if (pg_info->dma)
  129. lio_unmap_ring(oct->pci_dev,
  130. (u64)pg_info->dma);
  131. pg_info->dma = 0;
  132. if (pg_info->page)
  133. recv_buffer_destroy(droq->recv_buf_list[i].buffer,
  134. pg_info);
  135. droq->recv_buf_list[i].buffer = NULL;
  136. }
  137. octeon_droq_reset_indices(droq);
  138. }
  139. static int
  140. octeon_droq_setup_ring_buffers(struct octeon_device *oct,
  141. struct octeon_droq *droq)
  142. {
  143. u32 i;
  144. void *buf;
  145. struct octeon_droq_desc *desc_ring = droq->desc_ring;
  146. for (i = 0; i < droq->max_count; i++) {
  147. buf = recv_buffer_alloc(oct, &droq->recv_buf_list[i].pg_info);
  148. if (!buf) {
  149. dev_err(&oct->pci_dev->dev, "%s buffer alloc failed\n",
  150. __func__);
  151. droq->stats.rx_alloc_failure++;
  152. return -ENOMEM;
  153. }
  154. droq->recv_buf_list[i].buffer = buf;
  155. droq->recv_buf_list[i].data = get_rbd(buf);
  156. droq->info_list[i].length = 0;
  157. /* map ring buffers into memory */
  158. desc_ring[i].info_ptr = lio_map_ring_info(droq, i);
  159. desc_ring[i].buffer_ptr =
  160. lio_map_ring(droq->recv_buf_list[i].buffer);
  161. }
  162. octeon_droq_reset_indices(droq);
  163. octeon_droq_compute_max_packet_bufs(droq);
  164. return 0;
  165. }
  166. int octeon_delete_droq(struct octeon_device *oct, u32 q_no)
  167. {
  168. struct octeon_droq *droq = oct->droq[q_no];
  169. dev_dbg(&oct->pci_dev->dev, "%s[%d]\n", __func__, q_no);
  170. octeon_droq_destroy_ring_buffers(oct, droq);
  171. vfree(droq->recv_buf_list);
  172. if (droq->info_base_addr)
  173. lio_free_info_buffer(oct, droq);
  174. if (droq->desc_ring)
  175. lio_dma_free(oct, (droq->max_count * OCT_DROQ_DESC_SIZE),
  176. droq->desc_ring, droq->desc_ring_dma);
  177. memset(droq, 0, OCT_DROQ_SIZE);
  178. return 0;
  179. }
  180. int octeon_init_droq(struct octeon_device *oct,
  181. u32 q_no,
  182. u32 num_descs,
  183. u32 desc_size,
  184. void *app_ctx)
  185. {
  186. struct octeon_droq *droq;
  187. u32 desc_ring_size = 0, c_num_descs = 0, c_buf_size = 0;
  188. u32 c_pkts_per_intr = 0, c_refill_threshold = 0;
  189. int numa_node = dev_to_node(&oct->pci_dev->dev);
  190. dev_dbg(&oct->pci_dev->dev, "%s[%d]\n", __func__, q_no);
  191. droq = oct->droq[q_no];
  192. memset(droq, 0, OCT_DROQ_SIZE);
  193. droq->oct_dev = oct;
  194. droq->q_no = q_no;
  195. if (app_ctx)
  196. droq->app_ctx = app_ctx;
  197. else
  198. droq->app_ctx = (void *)(size_t)q_no;
  199. c_num_descs = num_descs;
  200. c_buf_size = desc_size;
  201. if (OCTEON_CN6XXX(oct)) {
  202. struct octeon_config *conf6x = CHIP_CONF(oct, cn6xxx);
  203. c_pkts_per_intr = (u32)CFG_GET_OQ_PKTS_PER_INTR(conf6x);
  204. c_refill_threshold =
  205. (u32)CFG_GET_OQ_REFILL_THRESHOLD(conf6x);
  206. } else if (OCTEON_CN23XX_PF(oct)) {
  207. struct octeon_config *conf23 = CHIP_CONF(oct, cn23xx_pf);
  208. c_pkts_per_intr = (u32)CFG_GET_OQ_PKTS_PER_INTR(conf23);
  209. c_refill_threshold = (u32)CFG_GET_OQ_REFILL_THRESHOLD(conf23);
  210. } else if (OCTEON_CN23XX_VF(oct)) {
  211. struct octeon_config *conf23 = CHIP_CONF(oct, cn23xx_vf);
  212. c_pkts_per_intr = (u32)CFG_GET_OQ_PKTS_PER_INTR(conf23);
  213. c_refill_threshold = (u32)CFG_GET_OQ_REFILL_THRESHOLD(conf23);
  214. } else {
  215. return 1;
  216. }
  217. droq->max_count = c_num_descs;
  218. droq->buffer_size = c_buf_size;
  219. desc_ring_size = droq->max_count * OCT_DROQ_DESC_SIZE;
  220. droq->desc_ring = lio_dma_alloc(oct, desc_ring_size,
  221. (dma_addr_t *)&droq->desc_ring_dma);
  222. if (!droq->desc_ring) {
  223. dev_err(&oct->pci_dev->dev,
  224. "Output queue %d ring alloc failed\n", q_no);
  225. return 1;
  226. }
  227. dev_dbg(&oct->pci_dev->dev, "droq[%d]: desc_ring: virt: 0x%p, dma: %lx\n",
  228. q_no, droq->desc_ring, droq->desc_ring_dma);
  229. dev_dbg(&oct->pci_dev->dev, "droq[%d]: num_desc: %d\n", q_no,
  230. droq->max_count);
  231. droq->info_list = lio_alloc_info_buffer(oct, droq);
  232. if (!droq->info_list) {
  233. dev_err(&oct->pci_dev->dev, "Cannot allocate memory for info list.\n");
  234. lio_dma_free(oct, (droq->max_count * OCT_DROQ_DESC_SIZE),
  235. droq->desc_ring, droq->desc_ring_dma);
  236. return 1;
  237. }
  238. droq->recv_buf_list = (struct octeon_recv_buffer *)
  239. vmalloc_node(droq->max_count *
  240. OCT_DROQ_RECVBUF_SIZE,
  241. numa_node);
  242. if (!droq->recv_buf_list)
  243. droq->recv_buf_list = (struct octeon_recv_buffer *)
  244. vmalloc(droq->max_count *
  245. OCT_DROQ_RECVBUF_SIZE);
  246. if (!droq->recv_buf_list) {
  247. dev_err(&oct->pci_dev->dev, "Output queue recv buf list alloc failed\n");
  248. goto init_droq_fail;
  249. }
  250. if (octeon_droq_setup_ring_buffers(oct, droq))
  251. goto init_droq_fail;
  252. droq->pkts_per_intr = c_pkts_per_intr;
  253. droq->refill_threshold = c_refill_threshold;
  254. dev_dbg(&oct->pci_dev->dev, "DROQ INIT: max_empty_descs: %d\n",
  255. droq->max_empty_descs);
  256. spin_lock_init(&droq->lock);
  257. INIT_LIST_HEAD(&droq->dispatch_list);
  258. /* For 56xx Pass1, this function won't be called, so no checks. */
  259. oct->fn_list.setup_oq_regs(oct, q_no);
  260. oct->io_qmask.oq |= BIT_ULL(q_no);
  261. return 0;
  262. init_droq_fail:
  263. octeon_delete_droq(oct, q_no);
  264. return 1;
  265. }
  266. /* octeon_create_recv_info
  267. * Parameters:
  268. * octeon_dev - pointer to the octeon device structure
  269. * droq - droq in which the packet arrived.
  270. * buf_cnt - no. of buffers used by the packet.
  271. * idx - index in the descriptor for the first buffer in the packet.
  272. * Description:
  273. * Allocates a recv_info_t and copies the buffer addresses for packet data
  274. * into the recv_pkt space which starts at an 8B offset from recv_info_t.
  275. * Flags the descriptors for refill later. If available descriptors go
  276. * below the threshold to receive a 64K pkt, new buffers are first allocated
  277. * before the recv_pkt_t is created.
  278. * This routine will be called in interrupt context.
  279. * Returns:
  280. * Success: Pointer to recv_info_t
  281. * Failure: NULL.
  282. * Locks:
  283. * The droq->lock is held when this routine is called.
  284. */
  285. static inline struct octeon_recv_info *octeon_create_recv_info(
  286. struct octeon_device *octeon_dev,
  287. struct octeon_droq *droq,
  288. u32 buf_cnt,
  289. u32 idx)
  290. {
  291. struct octeon_droq_info *info;
  292. struct octeon_recv_pkt *recv_pkt;
  293. struct octeon_recv_info *recv_info;
  294. u32 i, bytes_left;
  295. struct octeon_skb_page_info *pg_info;
  296. info = &droq->info_list[idx];
  297. recv_info = octeon_alloc_recv_info(sizeof(struct __dispatch));
  298. if (!recv_info)
  299. return NULL;
  300. recv_pkt = recv_info->recv_pkt;
  301. recv_pkt->rh = info->rh;
  302. recv_pkt->length = (u32)info->length;
  303. recv_pkt->buffer_count = (u16)buf_cnt;
  304. recv_pkt->octeon_id = (u16)octeon_dev->octeon_id;
  305. i = 0;
  306. bytes_left = (u32)info->length;
  307. while (buf_cnt) {
  308. {
  309. pg_info = &droq->recv_buf_list[idx].pg_info;
  310. lio_unmap_ring(octeon_dev->pci_dev,
  311. (u64)pg_info->dma);
  312. pg_info->page = NULL;
  313. pg_info->dma = 0;
  314. }
  315. recv_pkt->buffer_size[i] =
  316. (bytes_left >=
  317. droq->buffer_size) ? droq->buffer_size : bytes_left;
  318. recv_pkt->buffer_ptr[i] = droq->recv_buf_list[idx].buffer;
  319. droq->recv_buf_list[idx].buffer = NULL;
  320. idx = incr_index(idx, 1, droq->max_count);
  321. bytes_left -= droq->buffer_size;
  322. i++;
  323. buf_cnt--;
  324. }
  325. return recv_info;
  326. }
  327. /* If we were not able to refill all buffers, try to move around
  328. * the buffers that were not dispatched.
  329. */
  330. static inline u32
  331. octeon_droq_refill_pullup_descs(struct octeon_droq *droq,
  332. struct octeon_droq_desc *desc_ring)
  333. {
  334. u32 desc_refilled = 0;
  335. u32 refill_index = droq->refill_idx;
  336. while (refill_index != droq->read_idx) {
  337. if (droq->recv_buf_list[refill_index].buffer) {
  338. droq->recv_buf_list[droq->refill_idx].buffer =
  339. droq->recv_buf_list[refill_index].buffer;
  340. droq->recv_buf_list[droq->refill_idx].data =
  341. droq->recv_buf_list[refill_index].data;
  342. desc_ring[droq->refill_idx].buffer_ptr =
  343. desc_ring[refill_index].buffer_ptr;
  344. droq->recv_buf_list[refill_index].buffer = NULL;
  345. desc_ring[refill_index].buffer_ptr = 0;
  346. do {
  347. droq->refill_idx = incr_index(droq->refill_idx,
  348. 1,
  349. droq->max_count);
  350. desc_refilled++;
  351. droq->refill_count--;
  352. } while (droq->recv_buf_list[droq->refill_idx].
  353. buffer);
  354. }
  355. refill_index = incr_index(refill_index, 1, droq->max_count);
  356. } /* while */
  357. return desc_refilled;
  358. }
  359. /* octeon_droq_refill
  360. * Parameters:
  361. * droq - droq in which descriptors require new buffers.
  362. * Description:
  363. * Called during normal DROQ processing in interrupt mode or by the poll
  364. * thread to refill the descriptors from which buffers were dispatched
  365. * to upper layers. Attempts to allocate new buffers. If that fails, moves
  366. * up buffers (that were not dispatched) to form a contiguous ring.
  367. * Returns:
  368. * No of descriptors refilled.
  369. * Locks:
  370. * This routine is called with droq->lock held.
  371. */
  372. static u32
  373. octeon_droq_refill(struct octeon_device *octeon_dev, struct octeon_droq *droq)
  374. {
  375. struct octeon_droq_desc *desc_ring;
  376. void *buf = NULL;
  377. u8 *data;
  378. u32 desc_refilled = 0;
  379. struct octeon_skb_page_info *pg_info;
  380. desc_ring = droq->desc_ring;
  381. while (droq->refill_count && (desc_refilled < droq->max_count)) {
  382. /* If a valid buffer exists (happens if there is no dispatch),
  383. * reuse
  384. * the buffer, else allocate.
  385. */
  386. if (!droq->recv_buf_list[droq->refill_idx].buffer) {
  387. pg_info =
  388. &droq->recv_buf_list[droq->refill_idx].pg_info;
  389. /* Either recycle the existing pages or go for
  390. * new page alloc
  391. */
  392. if (pg_info->page)
  393. buf = recv_buffer_reuse(octeon_dev, pg_info);
  394. else
  395. buf = recv_buffer_alloc(octeon_dev, pg_info);
  396. /* If a buffer could not be allocated, no point in
  397. * continuing
  398. */
  399. if (!buf) {
  400. droq->stats.rx_alloc_failure++;
  401. break;
  402. }
  403. droq->recv_buf_list[droq->refill_idx].buffer =
  404. buf;
  405. data = get_rbd(buf);
  406. } else {
  407. data = get_rbd(droq->recv_buf_list
  408. [droq->refill_idx].buffer);
  409. }
  410. droq->recv_buf_list[droq->refill_idx].data = data;
  411. desc_ring[droq->refill_idx].buffer_ptr =
  412. lio_map_ring(droq->recv_buf_list[droq->
  413. refill_idx].buffer);
  414. /* Reset any previous values in the length field. */
  415. droq->info_list[droq->refill_idx].length = 0;
  416. droq->refill_idx = incr_index(droq->refill_idx, 1,
  417. droq->max_count);
  418. desc_refilled++;
  419. droq->refill_count--;
  420. }
  421. if (droq->refill_count)
  422. desc_refilled +=
  423. octeon_droq_refill_pullup_descs(droq, desc_ring);
  424. /* if droq->refill_count
  425. * The refill count would not change in pass two. We only moved buffers
  426. * to close the gap in the ring, but we would still have the same no. of
  427. * buffers to refill.
  428. */
  429. return desc_refilled;
  430. }
  431. /** check if we can allocate packets to get out of oom.
  432. * @param droq - Droq being checked.
  433. * @return does not return anything
  434. */
  435. void octeon_droq_check_oom(struct octeon_droq *droq)
  436. {
  437. int desc_refilled;
  438. struct octeon_device *oct = droq->oct_dev;
  439. if (readl(droq->pkts_credit_reg) <= CN23XX_SLI_DEF_BP) {
  440. spin_lock_bh(&droq->lock);
  441. desc_refilled = octeon_droq_refill(oct, droq);
  442. if (desc_refilled) {
  443. /* Flush the droq descriptor data to memory to be sure
  444. * that when we update the credits the data in memory
  445. * is accurate.
  446. */
  447. wmb();
  448. writel(desc_refilled, droq->pkts_credit_reg);
  449. /* make sure mmio write completes */
  450. mmiowb();
  451. }
  452. spin_unlock_bh(&droq->lock);
  453. }
  454. }
  455. static inline u32
  456. octeon_droq_get_bufcount(u32 buf_size, u32 total_len)
  457. {
  458. u32 buf_cnt = 0;
  459. while (total_len > (buf_size * buf_cnt))
  460. buf_cnt++;
  461. return buf_cnt;
  462. }
  463. static int
  464. octeon_droq_dispatch_pkt(struct octeon_device *oct,
  465. struct octeon_droq *droq,
  466. union octeon_rh *rh,
  467. struct octeon_droq_info *info)
  468. {
  469. u32 cnt;
  470. octeon_dispatch_fn_t disp_fn;
  471. struct octeon_recv_info *rinfo;
  472. cnt = octeon_droq_get_bufcount(droq->buffer_size, (u32)info->length);
  473. disp_fn = octeon_get_dispatch(oct, (u16)rh->r.opcode,
  474. (u16)rh->r.subcode);
  475. if (disp_fn) {
  476. rinfo = octeon_create_recv_info(oct, droq, cnt, droq->read_idx);
  477. if (rinfo) {
  478. struct __dispatch *rdisp = rinfo->rsvd;
  479. rdisp->rinfo = rinfo;
  480. rdisp->disp_fn = disp_fn;
  481. rinfo->recv_pkt->rh = *rh;
  482. list_add_tail(&rdisp->list,
  483. &droq->dispatch_list);
  484. } else {
  485. droq->stats.dropped_nomem++;
  486. }
  487. } else {
  488. dev_err(&oct->pci_dev->dev, "DROQ: No dispatch function (opcode %u/%u)\n",
  489. (unsigned int)rh->r.opcode,
  490. (unsigned int)rh->r.subcode);
  491. droq->stats.dropped_nodispatch++;
  492. }
  493. return cnt;
  494. }
  495. static inline void octeon_droq_drop_packets(struct octeon_device *oct,
  496. struct octeon_droq *droq,
  497. u32 cnt)
  498. {
  499. u32 i = 0, buf_cnt;
  500. struct octeon_droq_info *info;
  501. for (i = 0; i < cnt; i++) {
  502. info = &droq->info_list[droq->read_idx];
  503. octeon_swap_8B_data((u64 *)info, 2);
  504. if (info->length) {
  505. info->length -= OCT_RH_SIZE;
  506. droq->stats.bytes_received += info->length;
  507. buf_cnt = octeon_droq_get_bufcount(droq->buffer_size,
  508. (u32)info->length);
  509. } else {
  510. dev_err(&oct->pci_dev->dev, "DROQ: In drop: pkt with len 0\n");
  511. buf_cnt = 1;
  512. }
  513. droq->read_idx = incr_index(droq->read_idx, buf_cnt,
  514. droq->max_count);
  515. droq->refill_count += buf_cnt;
  516. }
  517. }
  518. static u32
  519. octeon_droq_fast_process_packets(struct octeon_device *oct,
  520. struct octeon_droq *droq,
  521. u32 pkts_to_process)
  522. {
  523. struct octeon_droq_info *info;
  524. union octeon_rh *rh;
  525. u32 pkt, total_len = 0, pkt_count;
  526. pkt_count = pkts_to_process;
  527. for (pkt = 0; pkt < pkt_count; pkt++) {
  528. u32 pkt_len = 0;
  529. struct sk_buff *nicbuf = NULL;
  530. struct octeon_skb_page_info *pg_info;
  531. void *buf;
  532. info = &droq->info_list[droq->read_idx];
  533. octeon_swap_8B_data((u64 *)info, 2);
  534. if (!info->length) {
  535. dev_err(&oct->pci_dev->dev,
  536. "DROQ[%d] idx: %d len:0, pkt_cnt: %d\n",
  537. droq->q_no, droq->read_idx, pkt_count);
  538. print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS,
  539. (u8 *)info,
  540. OCT_DROQ_INFO_SIZE);
  541. break;
  542. }
  543. /* Len of resp hdr in included in the received data len. */
  544. info->length -= OCT_RH_SIZE;
  545. rh = &info->rh;
  546. total_len += (u32)info->length;
  547. if (opcode_slow_path(rh)) {
  548. u32 buf_cnt;
  549. buf_cnt = octeon_droq_dispatch_pkt(oct, droq, rh, info);
  550. droq->read_idx = incr_index(droq->read_idx,
  551. buf_cnt, droq->max_count);
  552. droq->refill_count += buf_cnt;
  553. } else {
  554. if (info->length <= droq->buffer_size) {
  555. pkt_len = (u32)info->length;
  556. nicbuf = droq->recv_buf_list[
  557. droq->read_idx].buffer;
  558. pg_info = &droq->recv_buf_list[
  559. droq->read_idx].pg_info;
  560. if (recv_buffer_recycle(oct, pg_info))
  561. pg_info->page = NULL;
  562. droq->recv_buf_list[droq->read_idx].buffer =
  563. NULL;
  564. droq->read_idx = incr_index(droq->read_idx, 1,
  565. droq->max_count);
  566. droq->refill_count++;
  567. } else {
  568. nicbuf = octeon_fast_packet_alloc((u32)
  569. info->length);
  570. pkt_len = 0;
  571. /* nicbuf allocation can fail. We'll handle it
  572. * inside the loop.
  573. */
  574. while (pkt_len < info->length) {
  575. int cpy_len, idx = droq->read_idx;
  576. cpy_len = ((pkt_len + droq->buffer_size)
  577. > info->length) ?
  578. ((u32)info->length - pkt_len) :
  579. droq->buffer_size;
  580. if (nicbuf) {
  581. octeon_fast_packet_next(droq,
  582. nicbuf,
  583. cpy_len,
  584. idx);
  585. buf = droq->recv_buf_list[idx].
  586. buffer;
  587. recv_buffer_fast_free(buf);
  588. droq->recv_buf_list[idx].buffer
  589. = NULL;
  590. } else {
  591. droq->stats.rx_alloc_failure++;
  592. }
  593. pkt_len += cpy_len;
  594. droq->read_idx =
  595. incr_index(droq->read_idx, 1,
  596. droq->max_count);
  597. droq->refill_count++;
  598. }
  599. }
  600. if (nicbuf) {
  601. if (droq->ops.fptr) {
  602. droq->ops.fptr(oct->octeon_id,
  603. nicbuf, pkt_len,
  604. rh, &droq->napi,
  605. droq->ops.farg);
  606. } else {
  607. recv_buffer_free(nicbuf);
  608. }
  609. }
  610. }
  611. if (droq->refill_count >= droq->refill_threshold) {
  612. int desc_refilled = octeon_droq_refill(oct, droq);
  613. /* Flush the droq descriptor data to memory to be sure
  614. * that when we update the credits the data in memory
  615. * is accurate.
  616. */
  617. wmb();
  618. writel((desc_refilled), droq->pkts_credit_reg);
  619. /* make sure mmio write completes */
  620. mmiowb();
  621. }
  622. } /* for (each packet)... */
  623. /* Increment refill_count by the number of buffers processed. */
  624. droq->stats.pkts_received += pkt;
  625. droq->stats.bytes_received += total_len;
  626. if ((droq->ops.drop_on_max) && (pkts_to_process - pkt)) {
  627. octeon_droq_drop_packets(oct, droq, (pkts_to_process - pkt));
  628. droq->stats.dropped_toomany += (pkts_to_process - pkt);
  629. return pkts_to_process;
  630. }
  631. return pkt;
  632. }
  633. int
  634. octeon_droq_process_packets(struct octeon_device *oct,
  635. struct octeon_droq *droq,
  636. u32 budget)
  637. {
  638. u32 pkt_count = 0, pkts_processed = 0;
  639. struct list_head *tmp, *tmp2;
  640. /* Grab the droq lock */
  641. spin_lock(&droq->lock);
  642. octeon_droq_check_hw_for_pkts(droq);
  643. pkt_count = atomic_read(&droq->pkts_pending);
  644. if (!pkt_count) {
  645. spin_unlock(&droq->lock);
  646. return 0;
  647. }
  648. if (pkt_count > budget)
  649. pkt_count = budget;
  650. pkts_processed = octeon_droq_fast_process_packets(oct, droq, pkt_count);
  651. atomic_sub(pkts_processed, &droq->pkts_pending);
  652. /* Release the spin lock */
  653. spin_unlock(&droq->lock);
  654. list_for_each_safe(tmp, tmp2, &droq->dispatch_list) {
  655. struct __dispatch *rdisp = (struct __dispatch *)tmp;
  656. list_del(tmp);
  657. rdisp->disp_fn(rdisp->rinfo,
  658. octeon_get_dispatch_arg
  659. (oct,
  660. (u16)rdisp->rinfo->recv_pkt->rh.r.opcode,
  661. (u16)rdisp->rinfo->recv_pkt->rh.r.subcode));
  662. }
  663. /* If there are packets pending. schedule tasklet again */
  664. if (atomic_read(&droq->pkts_pending))
  665. return 1;
  666. return 0;
  667. }
  668. /**
  669. * Utility function to poll for packets. check_hw_for_packets must be
  670. * called before calling this routine.
  671. */
  672. static int
  673. octeon_droq_process_poll_pkts(struct octeon_device *oct,
  674. struct octeon_droq *droq, u32 budget)
  675. {
  676. struct list_head *tmp, *tmp2;
  677. u32 pkts_available = 0, pkts_processed = 0;
  678. u32 total_pkts_processed = 0;
  679. if (budget > droq->max_count)
  680. budget = droq->max_count;
  681. spin_lock(&droq->lock);
  682. while (total_pkts_processed < budget) {
  683. octeon_droq_check_hw_for_pkts(droq);
  684. pkts_available = min((budget - total_pkts_processed),
  685. (u32)(atomic_read(&droq->pkts_pending)));
  686. if (pkts_available == 0)
  687. break;
  688. pkts_processed =
  689. octeon_droq_fast_process_packets(oct, droq,
  690. pkts_available);
  691. atomic_sub(pkts_processed, &droq->pkts_pending);
  692. total_pkts_processed += pkts_processed;
  693. }
  694. spin_unlock(&droq->lock);
  695. list_for_each_safe(tmp, tmp2, &droq->dispatch_list) {
  696. struct __dispatch *rdisp = (struct __dispatch *)tmp;
  697. list_del(tmp);
  698. rdisp->disp_fn(rdisp->rinfo,
  699. octeon_get_dispatch_arg
  700. (oct,
  701. (u16)rdisp->rinfo->recv_pkt->rh.r.opcode,
  702. (u16)rdisp->rinfo->recv_pkt->rh.r.subcode));
  703. }
  704. return total_pkts_processed;
  705. }
  706. int
  707. octeon_process_droq_poll_cmd(struct octeon_device *oct, u32 q_no, int cmd,
  708. u32 arg)
  709. {
  710. struct octeon_droq *droq;
  711. droq = oct->droq[q_no];
  712. if (cmd == POLL_EVENT_PROCESS_PKTS)
  713. return octeon_droq_process_poll_pkts(oct, droq, arg);
  714. if (cmd == POLL_EVENT_PENDING_PKTS) {
  715. u32 pkt_cnt = atomic_read(&droq->pkts_pending);
  716. return octeon_droq_process_packets(oct, droq, pkt_cnt);
  717. }
  718. if (cmd == POLL_EVENT_ENABLE_INTR) {
  719. u32 value;
  720. unsigned long flags;
  721. /* Enable Pkt Interrupt */
  722. switch (oct->chip_id) {
  723. case OCTEON_CN66XX:
  724. case OCTEON_CN68XX: {
  725. struct octeon_cn6xxx *cn6xxx =
  726. (struct octeon_cn6xxx *)oct->chip;
  727. spin_lock_irqsave
  728. (&cn6xxx->lock_for_droq_int_enb_reg, flags);
  729. value =
  730. octeon_read_csr(oct,
  731. CN6XXX_SLI_PKT_TIME_INT_ENB);
  732. value |= (1 << q_no);
  733. octeon_write_csr(oct,
  734. CN6XXX_SLI_PKT_TIME_INT_ENB,
  735. value);
  736. value =
  737. octeon_read_csr(oct,
  738. CN6XXX_SLI_PKT_CNT_INT_ENB);
  739. value |= (1 << q_no);
  740. octeon_write_csr(oct,
  741. CN6XXX_SLI_PKT_CNT_INT_ENB,
  742. value);
  743. /* don't bother flushing the enables */
  744. spin_unlock_irqrestore
  745. (&cn6xxx->lock_for_droq_int_enb_reg, flags);
  746. return 0;
  747. }
  748. break;
  749. case OCTEON_CN23XX_PF_VID: {
  750. lio_enable_irq(oct->droq[q_no], oct->instr_queue[q_no]);
  751. }
  752. break;
  753. case OCTEON_CN23XX_VF_VID:
  754. lio_enable_irq(oct->droq[q_no], oct->instr_queue[q_no]);
  755. break;
  756. }
  757. return 0;
  758. }
  759. dev_err(&oct->pci_dev->dev, "%s Unknown command: %d\n", __func__, cmd);
  760. return -EINVAL;
  761. }
  762. int octeon_register_droq_ops(struct octeon_device *oct, u32 q_no,
  763. struct octeon_droq_ops *ops)
  764. {
  765. struct octeon_droq *droq;
  766. unsigned long flags;
  767. struct octeon_config *oct_cfg = NULL;
  768. oct_cfg = octeon_get_conf(oct);
  769. if (!oct_cfg)
  770. return -EINVAL;
  771. if (!(ops)) {
  772. dev_err(&oct->pci_dev->dev, "%s: droq_ops pointer is NULL\n",
  773. __func__);
  774. return -EINVAL;
  775. }
  776. if (q_no >= CFG_GET_OQ_MAX_Q(oct_cfg)) {
  777. dev_err(&oct->pci_dev->dev, "%s: droq id (%d) exceeds MAX (%d)\n",
  778. __func__, q_no, (oct->num_oqs - 1));
  779. return -EINVAL;
  780. }
  781. droq = oct->droq[q_no];
  782. spin_lock_irqsave(&droq->lock, flags);
  783. memcpy(&droq->ops, ops, sizeof(struct octeon_droq_ops));
  784. spin_unlock_irqrestore(&droq->lock, flags);
  785. return 0;
  786. }
  787. int octeon_unregister_droq_ops(struct octeon_device *oct, u32 q_no)
  788. {
  789. unsigned long flags;
  790. struct octeon_droq *droq;
  791. struct octeon_config *oct_cfg = NULL;
  792. oct_cfg = octeon_get_conf(oct);
  793. if (!oct_cfg)
  794. return -EINVAL;
  795. if (q_no >= CFG_GET_OQ_MAX_Q(oct_cfg)) {
  796. dev_err(&oct->pci_dev->dev, "%s: droq id (%d) exceeds MAX (%d)\n",
  797. __func__, q_no, oct->num_oqs - 1);
  798. return -EINVAL;
  799. }
  800. droq = oct->droq[q_no];
  801. if (!droq) {
  802. dev_info(&oct->pci_dev->dev,
  803. "Droq id (%d) not available.\n", q_no);
  804. return 0;
  805. }
  806. spin_lock_irqsave(&droq->lock, flags);
  807. droq->ops.fptr = NULL;
  808. droq->ops.farg = NULL;
  809. droq->ops.drop_on_max = 0;
  810. spin_unlock_irqrestore(&droq->lock, flags);
  811. return 0;
  812. }
  813. int octeon_create_droq(struct octeon_device *oct,
  814. u32 q_no, u32 num_descs,
  815. u32 desc_size, void *app_ctx)
  816. {
  817. struct octeon_droq *droq;
  818. int numa_node = dev_to_node(&oct->pci_dev->dev);
  819. if (oct->droq[q_no]) {
  820. dev_dbg(&oct->pci_dev->dev, "Droq already in use. Cannot create droq %d again\n",
  821. q_no);
  822. return 1;
  823. }
  824. /* Allocate the DS for the new droq. */
  825. droq = vmalloc_node(sizeof(*droq), numa_node);
  826. if (!droq)
  827. droq = vmalloc(sizeof(*droq));
  828. if (!droq)
  829. return -1;
  830. memset(droq, 0, sizeof(struct octeon_droq));
  831. /*Disable the pkt o/p for this Q */
  832. octeon_set_droq_pkt_op(oct, q_no, 0);
  833. oct->droq[q_no] = droq;
  834. /* Initialize the Droq */
  835. if (octeon_init_droq(oct, q_no, num_descs, desc_size, app_ctx)) {
  836. vfree(oct->droq[q_no]);
  837. oct->droq[q_no] = NULL;
  838. return -1;
  839. }
  840. oct->num_oqs++;
  841. dev_dbg(&oct->pci_dev->dev, "%s: Total number of OQ: %d\n", __func__,
  842. oct->num_oqs);
  843. /* Global Droq register settings */
  844. /* As of now not required, as setting are done for all 32 Droqs at
  845. * the same time.
  846. */
  847. return 0;
  848. }