octeon_droq.c 26 KB

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