csio_wr.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. /*
  2. * This file is part of the Chelsio FCoE driver for Linux.
  3. *
  4. * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/string.h>
  36. #include <linux/compiler.h>
  37. #include <linux/slab.h>
  38. #include <asm/page.h>
  39. #include <linux/cache.h>
  40. #include "csio_hw.h"
  41. #include "csio_wr.h"
  42. #include "csio_mb.h"
  43. #include "csio_defs.h"
  44. int csio_intr_coalesce_cnt; /* value:SGE_INGRESS_RX_THRESHOLD[0] */
  45. static int csio_sge_thresh_reg; /* SGE_INGRESS_RX_THRESHOLD[0] */
  46. int csio_intr_coalesce_time = 10; /* value:SGE_TIMER_VALUE_1 */
  47. static int csio_sge_timer_reg = 1;
  48. #define CSIO_SET_FLBUF_SIZE(_hw, _reg, _val) \
  49. csio_wr_reg32((_hw), (_val), SGE_FL_BUFFER_SIZE##_reg)
  50. static void
  51. csio_get_flbuf_size(struct csio_hw *hw, struct csio_sge *sge, uint32_t reg)
  52. {
  53. sge->sge_fl_buf_size[reg] = csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE0 +
  54. reg * sizeof(uint32_t));
  55. }
  56. /* Free list buffer size */
  57. static inline uint32_t
  58. csio_wr_fl_bufsz(struct csio_sge *sge, struct csio_dma_buf *buf)
  59. {
  60. return sge->sge_fl_buf_size[buf->paddr & 0xF];
  61. }
  62. /* Size of the egress queue status page */
  63. static inline uint32_t
  64. csio_wr_qstat_pgsz(struct csio_hw *hw)
  65. {
  66. return (hw->wrm.sge.sge_control & EGRSTATUSPAGESIZE(1)) ? 128 : 64;
  67. }
  68. /* Ring freelist doorbell */
  69. static inline void
  70. csio_wr_ring_fldb(struct csio_hw *hw, struct csio_q *flq)
  71. {
  72. /*
  73. * Ring the doorbell only when we have atleast CSIO_QCREDIT_SZ
  74. * number of bytes in the freelist queue. This translates to atleast
  75. * 8 freelist buffer pointers (since each pointer is 8 bytes).
  76. */
  77. if (flq->inc_idx >= 8) {
  78. csio_wr_reg32(hw, DBPRIO(1) | QID(flq->un.fl.flid) |
  79. PIDX(flq->inc_idx / 8),
  80. MYPF_REG(SGE_PF_KDOORBELL));
  81. flq->inc_idx &= 7;
  82. }
  83. }
  84. /* Write a 0 cidx increment value to enable SGE interrupts for this queue */
  85. static void
  86. csio_wr_sge_intr_enable(struct csio_hw *hw, uint16_t iqid)
  87. {
  88. csio_wr_reg32(hw, CIDXINC(0) |
  89. INGRESSQID(iqid) |
  90. TIMERREG(X_TIMERREG_RESTART_COUNTER),
  91. MYPF_REG(SGE_PF_GTS));
  92. }
  93. /*
  94. * csio_wr_fill_fl - Populate the FL buffers of a FL queue.
  95. * @hw: HW module.
  96. * @flq: Freelist queue.
  97. *
  98. * Fill up freelist buffer entries with buffers of size specified
  99. * in the size register.
  100. *
  101. */
  102. static int
  103. csio_wr_fill_fl(struct csio_hw *hw, struct csio_q *flq)
  104. {
  105. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  106. struct csio_sge *sge = &wrm->sge;
  107. __be64 *d = (__be64 *)(flq->vstart);
  108. struct csio_dma_buf *buf = &flq->un.fl.bufs[0];
  109. uint64_t paddr;
  110. int sreg = flq->un.fl.sreg;
  111. int n = flq->credits;
  112. while (n--) {
  113. buf->len = sge->sge_fl_buf_size[sreg];
  114. buf->vaddr = pci_alloc_consistent(hw->pdev, buf->len,
  115. &buf->paddr);
  116. if (!buf->vaddr) {
  117. csio_err(hw, "Could only fill %d buffers!\n", n + 1);
  118. return -ENOMEM;
  119. }
  120. paddr = buf->paddr | (sreg & 0xF);
  121. *d++ = cpu_to_be64(paddr);
  122. buf++;
  123. }
  124. return 0;
  125. }
  126. /*
  127. * csio_wr_update_fl -
  128. * @hw: HW module.
  129. * @flq: Freelist queue.
  130. *
  131. *
  132. */
  133. static inline void
  134. csio_wr_update_fl(struct csio_hw *hw, struct csio_q *flq, uint16_t n)
  135. {
  136. flq->inc_idx += n;
  137. flq->pidx += n;
  138. if (unlikely(flq->pidx >= flq->credits))
  139. flq->pidx -= (uint16_t)flq->credits;
  140. CSIO_INC_STATS(flq, n_flq_refill);
  141. }
  142. /*
  143. * csio_wr_alloc_q - Allocate a WR queue and initialize it.
  144. * @hw: HW module
  145. * @qsize: Size of the queue in bytes
  146. * @wrsize: Since of WR in this queue, if fixed.
  147. * @type: Type of queue (Ingress/Egress/Freelist)
  148. * @owner: Module that owns this queue.
  149. * @nflb: Number of freelist buffers for FL.
  150. * @sreg: What is the FL buffer size register?
  151. * @iq_int_handler: Ingress queue handler in INTx mode.
  152. *
  153. * This function allocates and sets up a queue for the caller
  154. * of size qsize, aligned at the required boundary. This is subject to
  155. * be free entries being available in the queue array. If one is found,
  156. * it is initialized with the allocated queue, marked as being used (owner),
  157. * and a handle returned to the caller in form of the queue's index
  158. * into the q_arr array.
  159. * If user has indicated a freelist (by specifying nflb > 0), create
  160. * another queue (with its own index into q_arr) for the freelist. Allocate
  161. * memory for DMA buffer metadata (vaddr, len etc). Save off the freelist
  162. * idx in the ingress queue's flq.idx. This is how a Freelist is associated
  163. * with its owning ingress queue.
  164. */
  165. int
  166. csio_wr_alloc_q(struct csio_hw *hw, uint32_t qsize, uint32_t wrsize,
  167. uint16_t type, void *owner, uint32_t nflb, int sreg,
  168. iq_handler_t iq_intx_handler)
  169. {
  170. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  171. struct csio_q *q, *flq;
  172. int free_idx = wrm->free_qidx;
  173. int ret_idx = free_idx;
  174. uint32_t qsz;
  175. int flq_idx;
  176. if (free_idx >= wrm->num_q) {
  177. csio_err(hw, "No more free queues.\n");
  178. return -1;
  179. }
  180. switch (type) {
  181. case CSIO_EGRESS:
  182. qsz = ALIGN(qsize, CSIO_QCREDIT_SZ) + csio_wr_qstat_pgsz(hw);
  183. break;
  184. case CSIO_INGRESS:
  185. switch (wrsize) {
  186. case 16:
  187. case 32:
  188. case 64:
  189. case 128:
  190. break;
  191. default:
  192. csio_err(hw, "Invalid Ingress queue WR size:%d\n",
  193. wrsize);
  194. return -1;
  195. }
  196. /*
  197. * Number of elements must be a multiple of 16
  198. * So this includes status page size
  199. */
  200. qsz = ALIGN(qsize/wrsize, 16) * wrsize;
  201. break;
  202. case CSIO_FREELIST:
  203. qsz = ALIGN(qsize/wrsize, 8) * wrsize + csio_wr_qstat_pgsz(hw);
  204. break;
  205. default:
  206. csio_err(hw, "Invalid queue type: 0x%x\n", type);
  207. return -1;
  208. }
  209. q = wrm->q_arr[free_idx];
  210. q->vstart = pci_alloc_consistent(hw->pdev, qsz, &q->pstart);
  211. if (!q->vstart) {
  212. csio_err(hw,
  213. "Failed to allocate DMA memory for "
  214. "queue at id: %d size: %d\n", free_idx, qsize);
  215. return -1;
  216. }
  217. /*
  218. * We need to zero out the contents, importantly for ingress,
  219. * since we start with a generatiom bit of 1 for ingress.
  220. */
  221. memset(q->vstart, 0, qsz);
  222. q->type = type;
  223. q->owner = owner;
  224. q->pidx = q->cidx = q->inc_idx = 0;
  225. q->size = qsz;
  226. q->wr_sz = wrsize; /* If using fixed size WRs */
  227. wrm->free_qidx++;
  228. if (type == CSIO_INGRESS) {
  229. /* Since queue area is set to zero */
  230. q->un.iq.genbit = 1;
  231. /*
  232. * Ingress queue status page size is always the size of
  233. * the ingress queue entry.
  234. */
  235. q->credits = (qsz - q->wr_sz) / q->wr_sz;
  236. q->vwrap = (void *)((uintptr_t)(q->vstart) + qsz
  237. - q->wr_sz);
  238. /* Allocate memory for FL if requested */
  239. if (nflb > 0) {
  240. flq_idx = csio_wr_alloc_q(hw, nflb * sizeof(__be64),
  241. sizeof(__be64), CSIO_FREELIST,
  242. owner, 0, sreg, NULL);
  243. if (flq_idx == -1) {
  244. csio_err(hw,
  245. "Failed to allocate FL queue"
  246. " for IQ idx:%d\n", free_idx);
  247. return -1;
  248. }
  249. /* Associate the new FL with the Ingress quue */
  250. q->un.iq.flq_idx = flq_idx;
  251. flq = wrm->q_arr[q->un.iq.flq_idx];
  252. flq->un.fl.bufs = kzalloc(flq->credits *
  253. sizeof(struct csio_dma_buf),
  254. GFP_KERNEL);
  255. if (!flq->un.fl.bufs) {
  256. csio_err(hw,
  257. "Failed to allocate FL queue bufs"
  258. " for IQ idx:%d\n", free_idx);
  259. return -1;
  260. }
  261. flq->un.fl.packen = 0;
  262. flq->un.fl.offset = 0;
  263. flq->un.fl.sreg = sreg;
  264. /* Fill up the free list buffers */
  265. if (csio_wr_fill_fl(hw, flq))
  266. return -1;
  267. /*
  268. * Make sure in a FLQ, atleast 1 credit (8 FL buffers)
  269. * remains unpopulated,otherwise HW thinks
  270. * FLQ is empty.
  271. */
  272. flq->pidx = flq->inc_idx = flq->credits - 8;
  273. } else {
  274. q->un.iq.flq_idx = -1;
  275. }
  276. /* Associate the IQ INTx handler. */
  277. q->un.iq.iq_intx_handler = iq_intx_handler;
  278. csio_q_iqid(hw, ret_idx) = CSIO_MAX_QID;
  279. } else if (type == CSIO_EGRESS) {
  280. q->credits = (qsz - csio_wr_qstat_pgsz(hw)) / CSIO_QCREDIT_SZ;
  281. q->vwrap = (void *)((uintptr_t)(q->vstart) + qsz
  282. - csio_wr_qstat_pgsz(hw));
  283. csio_q_eqid(hw, ret_idx) = CSIO_MAX_QID;
  284. } else { /* Freelist */
  285. q->credits = (qsz - csio_wr_qstat_pgsz(hw)) / sizeof(__be64);
  286. q->vwrap = (void *)((uintptr_t)(q->vstart) + qsz
  287. - csio_wr_qstat_pgsz(hw));
  288. csio_q_flid(hw, ret_idx) = CSIO_MAX_QID;
  289. }
  290. return ret_idx;
  291. }
  292. /*
  293. * csio_wr_iq_create_rsp - Response handler for IQ creation.
  294. * @hw: The HW module.
  295. * @mbp: Mailbox.
  296. * @iq_idx: Ingress queue that got created.
  297. *
  298. * Handle FW_IQ_CMD mailbox completion. Save off the assigned IQ/FL ids.
  299. */
  300. static int
  301. csio_wr_iq_create_rsp(struct csio_hw *hw, struct csio_mb *mbp, int iq_idx)
  302. {
  303. struct csio_iq_params iqp;
  304. enum fw_retval retval;
  305. uint32_t iq_id;
  306. int flq_idx;
  307. memset(&iqp, 0, sizeof(struct csio_iq_params));
  308. csio_mb_iq_alloc_write_rsp(hw, mbp, &retval, &iqp);
  309. if (retval != FW_SUCCESS) {
  310. csio_err(hw, "IQ cmd returned 0x%x!\n", retval);
  311. mempool_free(mbp, hw->mb_mempool);
  312. return -EINVAL;
  313. }
  314. csio_q_iqid(hw, iq_idx) = iqp.iqid;
  315. csio_q_physiqid(hw, iq_idx) = iqp.physiqid;
  316. csio_q_pidx(hw, iq_idx) = csio_q_cidx(hw, iq_idx) = 0;
  317. csio_q_inc_idx(hw, iq_idx) = 0;
  318. /* Actual iq-id. */
  319. iq_id = iqp.iqid - hw->wrm.fw_iq_start;
  320. /* Set the iq-id to iq map table. */
  321. if (iq_id >= CSIO_MAX_IQ) {
  322. csio_err(hw,
  323. "Exceeding MAX_IQ(%d) supported!"
  324. " iqid:%d rel_iqid:%d FW iq_start:%d\n",
  325. CSIO_MAX_IQ, iq_id, iqp.iqid, hw->wrm.fw_iq_start);
  326. mempool_free(mbp, hw->mb_mempool);
  327. return -EINVAL;
  328. }
  329. csio_q_set_intr_map(hw, iq_idx, iq_id);
  330. /*
  331. * During FW_IQ_CMD, FW sets interrupt_sent bit to 1 in the SGE
  332. * ingress context of this queue. This will block interrupts to
  333. * this queue until the next GTS write. Therefore, we do a
  334. * 0-cidx increment GTS write for this queue just to clear the
  335. * interrupt_sent bit. This will re-enable interrupts to this
  336. * queue.
  337. */
  338. csio_wr_sge_intr_enable(hw, iqp.physiqid);
  339. flq_idx = csio_q_iq_flq_idx(hw, iq_idx);
  340. if (flq_idx != -1) {
  341. struct csio_q *flq = hw->wrm.q_arr[flq_idx];
  342. csio_q_flid(hw, flq_idx) = iqp.fl0id;
  343. csio_q_cidx(hw, flq_idx) = 0;
  344. csio_q_pidx(hw, flq_idx) = csio_q_credits(hw, flq_idx) - 8;
  345. csio_q_inc_idx(hw, flq_idx) = csio_q_credits(hw, flq_idx) - 8;
  346. /* Now update SGE about the buffers allocated during init */
  347. csio_wr_ring_fldb(hw, flq);
  348. }
  349. mempool_free(mbp, hw->mb_mempool);
  350. return 0;
  351. }
  352. /*
  353. * csio_wr_iq_create - Configure an Ingress queue with FW.
  354. * @hw: The HW module.
  355. * @priv: Private data object.
  356. * @iq_idx: Ingress queue index in the WR module.
  357. * @vec: MSIX vector.
  358. * @portid: PCIE Channel to be associated with this queue.
  359. * @async: Is this a FW asynchronous message handling queue?
  360. * @cbfn: Completion callback.
  361. *
  362. * This API configures an ingress queue with FW by issuing a FW_IQ_CMD mailbox
  363. * with alloc/write bits set.
  364. */
  365. int
  366. csio_wr_iq_create(struct csio_hw *hw, void *priv, int iq_idx,
  367. uint32_t vec, uint8_t portid, bool async,
  368. void (*cbfn) (struct csio_hw *, struct csio_mb *))
  369. {
  370. struct csio_mb *mbp;
  371. struct csio_iq_params iqp;
  372. int flq_idx;
  373. memset(&iqp, 0, sizeof(struct csio_iq_params));
  374. csio_q_portid(hw, iq_idx) = portid;
  375. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  376. if (!mbp) {
  377. csio_err(hw, "IQ command out of memory!\n");
  378. return -ENOMEM;
  379. }
  380. switch (hw->intr_mode) {
  381. case CSIO_IM_INTX:
  382. case CSIO_IM_MSI:
  383. /* For interrupt forwarding queue only */
  384. if (hw->intr_iq_idx == iq_idx)
  385. iqp.iqandst = X_INTERRUPTDESTINATION_PCIE;
  386. else
  387. iqp.iqandst = X_INTERRUPTDESTINATION_IQ;
  388. iqp.iqandstindex =
  389. csio_q_physiqid(hw, hw->intr_iq_idx);
  390. break;
  391. case CSIO_IM_MSIX:
  392. iqp.iqandst = X_INTERRUPTDESTINATION_PCIE;
  393. iqp.iqandstindex = (uint16_t)vec;
  394. break;
  395. case CSIO_IM_NONE:
  396. mempool_free(mbp, hw->mb_mempool);
  397. return -EINVAL;
  398. }
  399. /* Pass in the ingress queue cmd parameters */
  400. iqp.pfn = hw->pfn;
  401. iqp.vfn = 0;
  402. iqp.iq_start = 1;
  403. iqp.viid = 0;
  404. iqp.type = FW_IQ_TYPE_FL_INT_CAP;
  405. iqp.iqasynch = async;
  406. if (csio_intr_coalesce_cnt)
  407. iqp.iqanus = X_UPDATESCHEDULING_COUNTER_OPTTIMER;
  408. else
  409. iqp.iqanus = X_UPDATESCHEDULING_TIMER;
  410. iqp.iqanud = X_UPDATEDELIVERY_INTERRUPT;
  411. iqp.iqpciech = portid;
  412. iqp.iqintcntthresh = (uint8_t)csio_sge_thresh_reg;
  413. switch (csio_q_wr_sz(hw, iq_idx)) {
  414. case 16:
  415. iqp.iqesize = 0; break;
  416. case 32:
  417. iqp.iqesize = 1; break;
  418. case 64:
  419. iqp.iqesize = 2; break;
  420. case 128:
  421. iqp.iqesize = 3; break;
  422. }
  423. iqp.iqsize = csio_q_size(hw, iq_idx) /
  424. csio_q_wr_sz(hw, iq_idx);
  425. iqp.iqaddr = csio_q_pstart(hw, iq_idx);
  426. flq_idx = csio_q_iq_flq_idx(hw, iq_idx);
  427. if (flq_idx != -1) {
  428. struct csio_q *flq = hw->wrm.q_arr[flq_idx];
  429. iqp.fl0paden = 1;
  430. iqp.fl0packen = flq->un.fl.packen ? 1 : 0;
  431. iqp.fl0fbmin = X_FETCHBURSTMIN_64B;
  432. iqp.fl0fbmax = X_FETCHBURSTMAX_512B;
  433. iqp.fl0size = csio_q_size(hw, flq_idx) / CSIO_QCREDIT_SZ;
  434. iqp.fl0addr = csio_q_pstart(hw, flq_idx);
  435. }
  436. csio_mb_iq_alloc_write(hw, mbp, priv, CSIO_MB_DEFAULT_TMO, &iqp, cbfn);
  437. if (csio_mb_issue(hw, mbp)) {
  438. csio_err(hw, "Issue of IQ cmd failed!\n");
  439. mempool_free(mbp, hw->mb_mempool);
  440. return -EINVAL;
  441. }
  442. if (cbfn != NULL)
  443. return 0;
  444. return csio_wr_iq_create_rsp(hw, mbp, iq_idx);
  445. }
  446. /*
  447. * csio_wr_eq_create_rsp - Response handler for EQ creation.
  448. * @hw: The HW module.
  449. * @mbp: Mailbox.
  450. * @eq_idx: Egress queue that got created.
  451. *
  452. * Handle FW_EQ_OFLD_CMD mailbox completion. Save off the assigned EQ ids.
  453. */
  454. static int
  455. csio_wr_eq_cfg_rsp(struct csio_hw *hw, struct csio_mb *mbp, int eq_idx)
  456. {
  457. struct csio_eq_params eqp;
  458. enum fw_retval retval;
  459. memset(&eqp, 0, sizeof(struct csio_eq_params));
  460. csio_mb_eq_ofld_alloc_write_rsp(hw, mbp, &retval, &eqp);
  461. if (retval != FW_SUCCESS) {
  462. csio_err(hw, "EQ OFLD cmd returned 0x%x!\n", retval);
  463. mempool_free(mbp, hw->mb_mempool);
  464. return -EINVAL;
  465. }
  466. csio_q_eqid(hw, eq_idx) = (uint16_t)eqp.eqid;
  467. csio_q_physeqid(hw, eq_idx) = (uint16_t)eqp.physeqid;
  468. csio_q_pidx(hw, eq_idx) = csio_q_cidx(hw, eq_idx) = 0;
  469. csio_q_inc_idx(hw, eq_idx) = 0;
  470. mempool_free(mbp, hw->mb_mempool);
  471. return 0;
  472. }
  473. /*
  474. * csio_wr_eq_create - Configure an Egress queue with FW.
  475. * @hw: HW module.
  476. * @priv: Private data.
  477. * @eq_idx: Egress queue index in the WR module.
  478. * @iq_idx: Associated ingress queue index.
  479. * @cbfn: Completion callback.
  480. *
  481. * This API configures a offload egress queue with FW by issuing a
  482. * FW_EQ_OFLD_CMD (with alloc + write ) mailbox.
  483. */
  484. int
  485. csio_wr_eq_create(struct csio_hw *hw, void *priv, int eq_idx,
  486. int iq_idx, uint8_t portid,
  487. void (*cbfn) (struct csio_hw *, struct csio_mb *))
  488. {
  489. struct csio_mb *mbp;
  490. struct csio_eq_params eqp;
  491. memset(&eqp, 0, sizeof(struct csio_eq_params));
  492. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  493. if (!mbp) {
  494. csio_err(hw, "EQ command out of memory!\n");
  495. return -ENOMEM;
  496. }
  497. eqp.pfn = hw->pfn;
  498. eqp.vfn = 0;
  499. eqp.eqstart = 1;
  500. eqp.hostfcmode = X_HOSTFCMODE_STATUS_PAGE;
  501. eqp.iqid = csio_q_iqid(hw, iq_idx);
  502. eqp.fbmin = X_FETCHBURSTMIN_64B;
  503. eqp.fbmax = X_FETCHBURSTMAX_512B;
  504. eqp.cidxfthresh = 0;
  505. eqp.pciechn = portid;
  506. eqp.eqsize = csio_q_size(hw, eq_idx) / CSIO_QCREDIT_SZ;
  507. eqp.eqaddr = csio_q_pstart(hw, eq_idx);
  508. csio_mb_eq_ofld_alloc_write(hw, mbp, priv, CSIO_MB_DEFAULT_TMO,
  509. &eqp, cbfn);
  510. if (csio_mb_issue(hw, mbp)) {
  511. csio_err(hw, "Issue of EQ OFLD cmd failed!\n");
  512. mempool_free(mbp, hw->mb_mempool);
  513. return -EINVAL;
  514. }
  515. if (cbfn != NULL)
  516. return 0;
  517. return csio_wr_eq_cfg_rsp(hw, mbp, eq_idx);
  518. }
  519. /*
  520. * csio_wr_iq_destroy_rsp - Response handler for IQ removal.
  521. * @hw: The HW module.
  522. * @mbp: Mailbox.
  523. * @iq_idx: Ingress queue that was freed.
  524. *
  525. * Handle FW_IQ_CMD (free) mailbox completion.
  526. */
  527. static int
  528. csio_wr_iq_destroy_rsp(struct csio_hw *hw, struct csio_mb *mbp, int iq_idx)
  529. {
  530. enum fw_retval retval = csio_mb_fw_retval(mbp);
  531. int rv = 0;
  532. if (retval != FW_SUCCESS)
  533. rv = -EINVAL;
  534. mempool_free(mbp, hw->mb_mempool);
  535. return rv;
  536. }
  537. /*
  538. * csio_wr_iq_destroy - Free an ingress queue.
  539. * @hw: The HW module.
  540. * @priv: Private data object.
  541. * @iq_idx: Ingress queue index to destroy
  542. * @cbfn: Completion callback.
  543. *
  544. * This API frees an ingress queue by issuing the FW_IQ_CMD
  545. * with the free bit set.
  546. */
  547. static int
  548. csio_wr_iq_destroy(struct csio_hw *hw, void *priv, int iq_idx,
  549. void (*cbfn)(struct csio_hw *, struct csio_mb *))
  550. {
  551. int rv = 0;
  552. struct csio_mb *mbp;
  553. struct csio_iq_params iqp;
  554. int flq_idx;
  555. memset(&iqp, 0, sizeof(struct csio_iq_params));
  556. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  557. if (!mbp)
  558. return -ENOMEM;
  559. iqp.pfn = hw->pfn;
  560. iqp.vfn = 0;
  561. iqp.iqid = csio_q_iqid(hw, iq_idx);
  562. iqp.type = FW_IQ_TYPE_FL_INT_CAP;
  563. flq_idx = csio_q_iq_flq_idx(hw, iq_idx);
  564. if (flq_idx != -1)
  565. iqp.fl0id = csio_q_flid(hw, flq_idx);
  566. else
  567. iqp.fl0id = 0xFFFF;
  568. iqp.fl1id = 0xFFFF;
  569. csio_mb_iq_free(hw, mbp, priv, CSIO_MB_DEFAULT_TMO, &iqp, cbfn);
  570. rv = csio_mb_issue(hw, mbp);
  571. if (rv != 0) {
  572. mempool_free(mbp, hw->mb_mempool);
  573. return rv;
  574. }
  575. if (cbfn != NULL)
  576. return 0;
  577. return csio_wr_iq_destroy_rsp(hw, mbp, iq_idx);
  578. }
  579. /*
  580. * csio_wr_eq_destroy_rsp - Response handler for OFLD EQ creation.
  581. * @hw: The HW module.
  582. * @mbp: Mailbox.
  583. * @eq_idx: Egress queue that was freed.
  584. *
  585. * Handle FW_OFLD_EQ_CMD (free) mailbox completion.
  586. */
  587. static int
  588. csio_wr_eq_destroy_rsp(struct csio_hw *hw, struct csio_mb *mbp, int eq_idx)
  589. {
  590. enum fw_retval retval = csio_mb_fw_retval(mbp);
  591. int rv = 0;
  592. if (retval != FW_SUCCESS)
  593. rv = -EINVAL;
  594. mempool_free(mbp, hw->mb_mempool);
  595. return rv;
  596. }
  597. /*
  598. * csio_wr_eq_destroy - Free an Egress queue.
  599. * @hw: The HW module.
  600. * @priv: Private data object.
  601. * @eq_idx: Egress queue index to destroy
  602. * @cbfn: Completion callback.
  603. *
  604. * This API frees an Egress queue by issuing the FW_EQ_OFLD_CMD
  605. * with the free bit set.
  606. */
  607. static int
  608. csio_wr_eq_destroy(struct csio_hw *hw, void *priv, int eq_idx,
  609. void (*cbfn) (struct csio_hw *, struct csio_mb *))
  610. {
  611. int rv = 0;
  612. struct csio_mb *mbp;
  613. struct csio_eq_params eqp;
  614. memset(&eqp, 0, sizeof(struct csio_eq_params));
  615. mbp = mempool_alloc(hw->mb_mempool, GFP_ATOMIC);
  616. if (!mbp)
  617. return -ENOMEM;
  618. eqp.pfn = hw->pfn;
  619. eqp.vfn = 0;
  620. eqp.eqid = csio_q_eqid(hw, eq_idx);
  621. csio_mb_eq_ofld_free(hw, mbp, priv, CSIO_MB_DEFAULT_TMO, &eqp, cbfn);
  622. rv = csio_mb_issue(hw, mbp);
  623. if (rv != 0) {
  624. mempool_free(mbp, hw->mb_mempool);
  625. return rv;
  626. }
  627. if (cbfn != NULL)
  628. return 0;
  629. return csio_wr_eq_destroy_rsp(hw, mbp, eq_idx);
  630. }
  631. /*
  632. * csio_wr_cleanup_eq_stpg - Cleanup Egress queue status page
  633. * @hw: HW module
  634. * @qidx: Egress queue index
  635. *
  636. * Cleanup the Egress queue status page.
  637. */
  638. static void
  639. csio_wr_cleanup_eq_stpg(struct csio_hw *hw, int qidx)
  640. {
  641. struct csio_q *q = csio_hw_to_wrm(hw)->q_arr[qidx];
  642. struct csio_qstatus_page *stp = (struct csio_qstatus_page *)q->vwrap;
  643. memset(stp, 0, sizeof(*stp));
  644. }
  645. /*
  646. * csio_wr_cleanup_iq_ftr - Cleanup Footer entries in IQ
  647. * @hw: HW module
  648. * @qidx: Ingress queue index
  649. *
  650. * Cleanup the footer entries in the given ingress queue,
  651. * set to 1 the internal copy of genbit.
  652. */
  653. static void
  654. csio_wr_cleanup_iq_ftr(struct csio_hw *hw, int qidx)
  655. {
  656. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  657. struct csio_q *q = wrm->q_arr[qidx];
  658. void *wr;
  659. struct csio_iqwr_footer *ftr;
  660. uint32_t i = 0;
  661. /* set to 1 since we are just about zero out genbit */
  662. q->un.iq.genbit = 1;
  663. for (i = 0; i < q->credits; i++) {
  664. /* Get the WR */
  665. wr = (void *)((uintptr_t)q->vstart +
  666. (i * q->wr_sz));
  667. /* Get the footer */
  668. ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
  669. (q->wr_sz - sizeof(*ftr)));
  670. /* Zero out footer */
  671. memset(ftr, 0, sizeof(*ftr));
  672. }
  673. }
  674. int
  675. csio_wr_destroy_queues(struct csio_hw *hw, bool cmd)
  676. {
  677. int i, flq_idx;
  678. struct csio_q *q;
  679. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  680. int rv;
  681. for (i = 0; i < wrm->free_qidx; i++) {
  682. q = wrm->q_arr[i];
  683. switch (q->type) {
  684. case CSIO_EGRESS:
  685. if (csio_q_eqid(hw, i) != CSIO_MAX_QID) {
  686. csio_wr_cleanup_eq_stpg(hw, i);
  687. if (!cmd) {
  688. csio_q_eqid(hw, i) = CSIO_MAX_QID;
  689. continue;
  690. }
  691. rv = csio_wr_eq_destroy(hw, NULL, i, NULL);
  692. if ((rv == -EBUSY) || (rv == -ETIMEDOUT))
  693. cmd = false;
  694. csio_q_eqid(hw, i) = CSIO_MAX_QID;
  695. }
  696. case CSIO_INGRESS:
  697. if (csio_q_iqid(hw, i) != CSIO_MAX_QID) {
  698. csio_wr_cleanup_iq_ftr(hw, i);
  699. if (!cmd) {
  700. csio_q_iqid(hw, i) = CSIO_MAX_QID;
  701. flq_idx = csio_q_iq_flq_idx(hw, i);
  702. if (flq_idx != -1)
  703. csio_q_flid(hw, flq_idx) =
  704. CSIO_MAX_QID;
  705. continue;
  706. }
  707. rv = csio_wr_iq_destroy(hw, NULL, i, NULL);
  708. if ((rv == -EBUSY) || (rv == -ETIMEDOUT))
  709. cmd = false;
  710. csio_q_iqid(hw, i) = CSIO_MAX_QID;
  711. flq_idx = csio_q_iq_flq_idx(hw, i);
  712. if (flq_idx != -1)
  713. csio_q_flid(hw, flq_idx) = CSIO_MAX_QID;
  714. }
  715. default:
  716. break;
  717. }
  718. }
  719. hw->flags &= ~CSIO_HWF_Q_FW_ALLOCED;
  720. return 0;
  721. }
  722. /*
  723. * csio_wr_get - Get requested size of WR entry/entries from queue.
  724. * @hw: HW module.
  725. * @qidx: Index of queue.
  726. * @size: Cumulative size of Work request(s).
  727. * @wrp: Work request pair.
  728. *
  729. * If requested credits are available, return the start address of the
  730. * work request in the work request pair. Set pidx accordingly and
  731. * return.
  732. *
  733. * NOTE about WR pair:
  734. * ==================
  735. * A WR can start towards the end of a queue, and then continue at the
  736. * beginning, since the queue is considered to be circular. This will
  737. * require a pair of address/size to be passed back to the caller -
  738. * hence Work request pair format.
  739. */
  740. int
  741. csio_wr_get(struct csio_hw *hw, int qidx, uint32_t size,
  742. struct csio_wr_pair *wrp)
  743. {
  744. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  745. struct csio_q *q = wrm->q_arr[qidx];
  746. void *cwr = (void *)((uintptr_t)(q->vstart) +
  747. (q->pidx * CSIO_QCREDIT_SZ));
  748. struct csio_qstatus_page *stp = (struct csio_qstatus_page *)q->vwrap;
  749. uint16_t cidx = q->cidx = ntohs(stp->cidx);
  750. uint16_t pidx = q->pidx;
  751. uint32_t req_sz = ALIGN(size, CSIO_QCREDIT_SZ);
  752. int req_credits = req_sz / CSIO_QCREDIT_SZ;
  753. int credits;
  754. CSIO_DB_ASSERT(q->owner != NULL);
  755. CSIO_DB_ASSERT((qidx >= 0) && (qidx < wrm->free_qidx));
  756. CSIO_DB_ASSERT(cidx <= q->credits);
  757. /* Calculate credits */
  758. if (pidx > cidx) {
  759. credits = q->credits - (pidx - cidx) - 1;
  760. } else if (cidx > pidx) {
  761. credits = cidx - pidx - 1;
  762. } else {
  763. /* cidx == pidx, empty queue */
  764. credits = q->credits;
  765. CSIO_INC_STATS(q, n_qempty);
  766. }
  767. /*
  768. * Check if we have enough credits.
  769. * credits = 1 implies queue is full.
  770. */
  771. if (!credits || (req_credits > credits)) {
  772. CSIO_INC_STATS(q, n_qfull);
  773. return -EBUSY;
  774. }
  775. /*
  776. * If we are here, we have enough credits to satisfy the
  777. * request. Check if we are near the end of q, and if WR spills over.
  778. * If it does, use the first addr/size to cover the queue until
  779. * the end. Fit the remainder portion of the request at the top
  780. * of queue and return it in the second addr/len. Set pidx
  781. * accordingly.
  782. */
  783. if (unlikely(((uintptr_t)cwr + req_sz) > (uintptr_t)(q->vwrap))) {
  784. wrp->addr1 = cwr;
  785. wrp->size1 = (uint32_t)((uintptr_t)q->vwrap - (uintptr_t)cwr);
  786. wrp->addr2 = q->vstart;
  787. wrp->size2 = req_sz - wrp->size1;
  788. q->pidx = (uint16_t)(ALIGN(wrp->size2, CSIO_QCREDIT_SZ) /
  789. CSIO_QCREDIT_SZ);
  790. CSIO_INC_STATS(q, n_qwrap);
  791. CSIO_INC_STATS(q, n_eq_wr_split);
  792. } else {
  793. wrp->addr1 = cwr;
  794. wrp->size1 = req_sz;
  795. wrp->addr2 = NULL;
  796. wrp->size2 = 0;
  797. q->pidx += (uint16_t)req_credits;
  798. /* We are the end of queue, roll back pidx to top of queue */
  799. if (unlikely(q->pidx == q->credits)) {
  800. q->pidx = 0;
  801. CSIO_INC_STATS(q, n_qwrap);
  802. }
  803. }
  804. q->inc_idx = (uint16_t)req_credits;
  805. CSIO_INC_STATS(q, n_tot_reqs);
  806. return 0;
  807. }
  808. /*
  809. * csio_wr_copy_to_wrp - Copies given data into WR.
  810. * @data_buf - Data buffer
  811. * @wrp - Work request pair.
  812. * @wr_off - Work request offset.
  813. * @data_len - Data length.
  814. *
  815. * Copies the given data in Work Request. Work request pair(wrp) specifies
  816. * address information of Work request.
  817. * Returns: none
  818. */
  819. void
  820. csio_wr_copy_to_wrp(void *data_buf, struct csio_wr_pair *wrp,
  821. uint32_t wr_off, uint32_t data_len)
  822. {
  823. uint32_t nbytes;
  824. /* Number of space available in buffer addr1 of WRP */
  825. nbytes = ((wrp->size1 - wr_off) >= data_len) ?
  826. data_len : (wrp->size1 - wr_off);
  827. memcpy((uint8_t *) wrp->addr1 + wr_off, data_buf, nbytes);
  828. data_len -= nbytes;
  829. /* Write the remaining data from the begining of circular buffer */
  830. if (data_len) {
  831. CSIO_DB_ASSERT(data_len <= wrp->size2);
  832. CSIO_DB_ASSERT(wrp->addr2 != NULL);
  833. memcpy(wrp->addr2, (uint8_t *) data_buf + nbytes, data_len);
  834. }
  835. }
  836. /*
  837. * csio_wr_issue - Notify chip of Work request.
  838. * @hw: HW module.
  839. * @qidx: Index of queue.
  840. * @prio: 0: Low priority, 1: High priority
  841. *
  842. * Rings the SGE Doorbell by writing the current producer index of the passed
  843. * in queue into the register.
  844. *
  845. */
  846. int
  847. csio_wr_issue(struct csio_hw *hw, int qidx, bool prio)
  848. {
  849. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  850. struct csio_q *q = wrm->q_arr[qidx];
  851. CSIO_DB_ASSERT((qidx >= 0) && (qidx < wrm->free_qidx));
  852. wmb();
  853. /* Ring SGE Doorbell writing q->pidx into it */
  854. csio_wr_reg32(hw, DBPRIO(prio) | QID(q->un.eq.physeqid) |
  855. PIDX(q->inc_idx), MYPF_REG(SGE_PF_KDOORBELL));
  856. q->inc_idx = 0;
  857. return 0;
  858. }
  859. static inline uint32_t
  860. csio_wr_avail_qcredits(struct csio_q *q)
  861. {
  862. if (q->pidx > q->cidx)
  863. return q->pidx - q->cidx;
  864. else if (q->cidx > q->pidx)
  865. return q->credits - (q->cidx - q->pidx);
  866. else
  867. return 0; /* cidx == pidx, empty queue */
  868. }
  869. /*
  870. * csio_wr_inval_flq_buf - Invalidate a free list buffer entry.
  871. * @hw: HW module.
  872. * @flq: The freelist queue.
  873. *
  874. * Invalidate the driver's version of a freelist buffer entry,
  875. * without freeing the associated the DMA memory. The entry
  876. * to be invalidated is picked up from the current Free list
  877. * queue cidx.
  878. *
  879. */
  880. static inline void
  881. csio_wr_inval_flq_buf(struct csio_hw *hw, struct csio_q *flq)
  882. {
  883. flq->cidx++;
  884. if (flq->cidx == flq->credits) {
  885. flq->cidx = 0;
  886. CSIO_INC_STATS(flq, n_qwrap);
  887. }
  888. }
  889. /*
  890. * csio_wr_process_fl - Process a freelist completion.
  891. * @hw: HW module.
  892. * @q: The ingress queue attached to the Freelist.
  893. * @wr: The freelist completion WR in the ingress queue.
  894. * @len_to_qid: The lower 32-bits of the first flit of the RSP footer
  895. * @iq_handler: Caller's handler for this completion.
  896. * @priv: Private pointer of caller
  897. *
  898. */
  899. static inline void
  900. csio_wr_process_fl(struct csio_hw *hw, struct csio_q *q,
  901. void *wr, uint32_t len_to_qid,
  902. void (*iq_handler)(struct csio_hw *, void *,
  903. uint32_t, struct csio_fl_dma_buf *,
  904. void *),
  905. void *priv)
  906. {
  907. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  908. struct csio_sge *sge = &wrm->sge;
  909. struct csio_fl_dma_buf flb;
  910. struct csio_dma_buf *buf, *fbuf;
  911. uint32_t bufsz, len, lastlen = 0;
  912. int n;
  913. struct csio_q *flq = hw->wrm.q_arr[q->un.iq.flq_idx];
  914. CSIO_DB_ASSERT(flq != NULL);
  915. len = len_to_qid;
  916. if (len & IQWRF_NEWBUF) {
  917. if (flq->un.fl.offset > 0) {
  918. csio_wr_inval_flq_buf(hw, flq);
  919. flq->un.fl.offset = 0;
  920. }
  921. len = IQWRF_LEN_GET(len);
  922. }
  923. CSIO_DB_ASSERT(len != 0);
  924. flb.totlen = len;
  925. /* Consume all freelist buffers used for len bytes */
  926. for (n = 0, fbuf = flb.flbufs; ; n++, fbuf++) {
  927. buf = &flq->un.fl.bufs[flq->cidx];
  928. bufsz = csio_wr_fl_bufsz(sge, buf);
  929. fbuf->paddr = buf->paddr;
  930. fbuf->vaddr = buf->vaddr;
  931. flb.offset = flq->un.fl.offset;
  932. lastlen = min(bufsz, len);
  933. fbuf->len = lastlen;
  934. len -= lastlen;
  935. if (!len)
  936. break;
  937. csio_wr_inval_flq_buf(hw, flq);
  938. }
  939. flb.defer_free = flq->un.fl.packen ? 0 : 1;
  940. iq_handler(hw, wr, q->wr_sz - sizeof(struct csio_iqwr_footer),
  941. &flb, priv);
  942. if (flq->un.fl.packen)
  943. flq->un.fl.offset += ALIGN(lastlen, sge->csio_fl_align);
  944. else
  945. csio_wr_inval_flq_buf(hw, flq);
  946. }
  947. /*
  948. * csio_is_new_iqwr - Is this a new Ingress queue entry ?
  949. * @q: Ingress quueue.
  950. * @ftr: Ingress queue WR SGE footer.
  951. *
  952. * The entry is new if our generation bit matches the corresponding
  953. * bit in the footer of the current WR.
  954. */
  955. static inline bool
  956. csio_is_new_iqwr(struct csio_q *q, struct csio_iqwr_footer *ftr)
  957. {
  958. return (q->un.iq.genbit == (ftr->u.type_gen >> IQWRF_GEN_SHIFT));
  959. }
  960. /*
  961. * csio_wr_process_iq - Process elements in Ingress queue.
  962. * @hw: HW pointer
  963. * @qidx: Index of queue
  964. * @iq_handler: Handler for this queue
  965. * @priv: Caller's private pointer
  966. *
  967. * This routine walks through every entry of the ingress queue, calling
  968. * the provided iq_handler with the entry, until the generation bit
  969. * flips.
  970. */
  971. int
  972. csio_wr_process_iq(struct csio_hw *hw, struct csio_q *q,
  973. void (*iq_handler)(struct csio_hw *, void *,
  974. uint32_t, struct csio_fl_dma_buf *,
  975. void *),
  976. void *priv)
  977. {
  978. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  979. void *wr = (void *)((uintptr_t)q->vstart + (q->cidx * q->wr_sz));
  980. struct csio_iqwr_footer *ftr;
  981. uint32_t wr_type, fw_qid, qid;
  982. struct csio_q *q_completed;
  983. struct csio_q *flq = csio_iq_has_fl(q) ?
  984. wrm->q_arr[q->un.iq.flq_idx] : NULL;
  985. int rv = 0;
  986. /* Get the footer */
  987. ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
  988. (q->wr_sz - sizeof(*ftr)));
  989. /*
  990. * When q wrapped around last time, driver should have inverted
  991. * ic.genbit as well.
  992. */
  993. while (csio_is_new_iqwr(q, ftr)) {
  994. CSIO_DB_ASSERT(((uintptr_t)wr + q->wr_sz) <=
  995. (uintptr_t)q->vwrap);
  996. rmb();
  997. wr_type = IQWRF_TYPE_GET(ftr->u.type_gen);
  998. switch (wr_type) {
  999. case X_RSPD_TYPE_CPL:
  1000. /* Subtract footer from WR len */
  1001. iq_handler(hw, wr, q->wr_sz - sizeof(*ftr), NULL, priv);
  1002. break;
  1003. case X_RSPD_TYPE_FLBUF:
  1004. csio_wr_process_fl(hw, q, wr,
  1005. ntohl(ftr->pldbuflen_qid),
  1006. iq_handler, priv);
  1007. break;
  1008. case X_RSPD_TYPE_INTR:
  1009. fw_qid = ntohl(ftr->pldbuflen_qid);
  1010. qid = fw_qid - wrm->fw_iq_start;
  1011. q_completed = hw->wrm.intr_map[qid];
  1012. if (unlikely(qid ==
  1013. csio_q_physiqid(hw, hw->intr_iq_idx))) {
  1014. /*
  1015. * We are already in the Forward Interrupt
  1016. * Interrupt Queue Service! Do-not service
  1017. * again!
  1018. *
  1019. */
  1020. } else {
  1021. CSIO_DB_ASSERT(q_completed);
  1022. CSIO_DB_ASSERT(
  1023. q_completed->un.iq.iq_intx_handler);
  1024. /* Call the queue handler. */
  1025. q_completed->un.iq.iq_intx_handler(hw, NULL,
  1026. 0, NULL, (void *)q_completed);
  1027. }
  1028. break;
  1029. default:
  1030. csio_warn(hw, "Unknown resp type 0x%x received\n",
  1031. wr_type);
  1032. CSIO_INC_STATS(q, n_rsp_unknown);
  1033. break;
  1034. }
  1035. /*
  1036. * Ingress *always* has fixed size WR entries. Therefore,
  1037. * there should always be complete WRs towards the end of
  1038. * queue.
  1039. */
  1040. if (((uintptr_t)wr + q->wr_sz) == (uintptr_t)q->vwrap) {
  1041. /* Roll over to start of queue */
  1042. q->cidx = 0;
  1043. wr = q->vstart;
  1044. /* Toggle genbit */
  1045. q->un.iq.genbit ^= 0x1;
  1046. CSIO_INC_STATS(q, n_qwrap);
  1047. } else {
  1048. q->cidx++;
  1049. wr = (void *)((uintptr_t)(q->vstart) +
  1050. (q->cidx * q->wr_sz));
  1051. }
  1052. ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
  1053. (q->wr_sz - sizeof(*ftr)));
  1054. q->inc_idx++;
  1055. } /* while (q->un.iq.genbit == hdr->genbit) */
  1056. /*
  1057. * We need to re-arm SGE interrupts in case we got a stray interrupt,
  1058. * especially in msix mode. With INTx, this may be a common occurence.
  1059. */
  1060. if (unlikely(!q->inc_idx)) {
  1061. CSIO_INC_STATS(q, n_stray_comp);
  1062. rv = -EINVAL;
  1063. goto restart;
  1064. }
  1065. /* Replenish free list buffers if pending falls below low water mark */
  1066. if (flq) {
  1067. uint32_t avail = csio_wr_avail_qcredits(flq);
  1068. if (avail <= 16) {
  1069. /* Make sure in FLQ, atleast 1 credit (8 FL buffers)
  1070. * remains unpopulated otherwise HW thinks
  1071. * FLQ is empty.
  1072. */
  1073. csio_wr_update_fl(hw, flq, (flq->credits - 8) - avail);
  1074. csio_wr_ring_fldb(hw, flq);
  1075. }
  1076. }
  1077. restart:
  1078. /* Now inform SGE about our incremental index value */
  1079. csio_wr_reg32(hw, CIDXINC(q->inc_idx) |
  1080. INGRESSQID(q->un.iq.physiqid) |
  1081. TIMERREG(csio_sge_timer_reg),
  1082. MYPF_REG(SGE_PF_GTS));
  1083. q->stats.n_tot_rsps += q->inc_idx;
  1084. q->inc_idx = 0;
  1085. return rv;
  1086. }
  1087. int
  1088. csio_wr_process_iq_idx(struct csio_hw *hw, int qidx,
  1089. void (*iq_handler)(struct csio_hw *, void *,
  1090. uint32_t, struct csio_fl_dma_buf *,
  1091. void *),
  1092. void *priv)
  1093. {
  1094. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1095. struct csio_q *iq = wrm->q_arr[qidx];
  1096. return csio_wr_process_iq(hw, iq, iq_handler, priv);
  1097. }
  1098. static int
  1099. csio_closest_timer(struct csio_sge *s, int time)
  1100. {
  1101. int i, delta, match = 0, min_delta = INT_MAX;
  1102. for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
  1103. delta = time - s->timer_val[i];
  1104. if (delta < 0)
  1105. delta = -delta;
  1106. if (delta < min_delta) {
  1107. min_delta = delta;
  1108. match = i;
  1109. }
  1110. }
  1111. return match;
  1112. }
  1113. static int
  1114. csio_closest_thresh(struct csio_sge *s, int cnt)
  1115. {
  1116. int i, delta, match = 0, min_delta = INT_MAX;
  1117. for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
  1118. delta = cnt - s->counter_val[i];
  1119. if (delta < 0)
  1120. delta = -delta;
  1121. if (delta < min_delta) {
  1122. min_delta = delta;
  1123. match = i;
  1124. }
  1125. }
  1126. return match;
  1127. }
  1128. static void
  1129. csio_wr_fixup_host_params(struct csio_hw *hw)
  1130. {
  1131. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1132. struct csio_sge *sge = &wrm->sge;
  1133. uint32_t clsz = L1_CACHE_BYTES;
  1134. uint32_t s_hps = PAGE_SHIFT - 10;
  1135. uint32_t ingpad = 0;
  1136. uint32_t stat_len = clsz > 64 ? 128 : 64;
  1137. csio_wr_reg32(hw, HOSTPAGESIZEPF0(s_hps) | HOSTPAGESIZEPF1(s_hps) |
  1138. HOSTPAGESIZEPF2(s_hps) | HOSTPAGESIZEPF3(s_hps) |
  1139. HOSTPAGESIZEPF4(s_hps) | HOSTPAGESIZEPF5(s_hps) |
  1140. HOSTPAGESIZEPF6(s_hps) | HOSTPAGESIZEPF7(s_hps),
  1141. SGE_HOST_PAGE_SIZE);
  1142. sge->csio_fl_align = clsz < 32 ? 32 : clsz;
  1143. ingpad = ilog2(sge->csio_fl_align) - 5;
  1144. csio_set_reg_field(hw, SGE_CONTROL, INGPADBOUNDARY_MASK |
  1145. EGRSTATUSPAGESIZE(1),
  1146. INGPADBOUNDARY(ingpad) |
  1147. EGRSTATUSPAGESIZE(stat_len != 64));
  1148. /* FL BUFFER SIZE#0 is Page size i,e already aligned to cache line */
  1149. csio_wr_reg32(hw, PAGE_SIZE, SGE_FL_BUFFER_SIZE0);
  1150. csio_wr_reg32(hw,
  1151. (csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE2) +
  1152. sge->csio_fl_align - 1) & ~(sge->csio_fl_align - 1),
  1153. SGE_FL_BUFFER_SIZE2);
  1154. csio_wr_reg32(hw,
  1155. (csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE3) +
  1156. sge->csio_fl_align - 1) & ~(sge->csio_fl_align - 1),
  1157. SGE_FL_BUFFER_SIZE3);
  1158. csio_wr_reg32(hw, HPZ0(PAGE_SHIFT - 12), ULP_RX_TDDP_PSZ);
  1159. /* default value of rx_dma_offset of the NIC driver */
  1160. csio_set_reg_field(hw, SGE_CONTROL, PKTSHIFT_MASK,
  1161. PKTSHIFT(CSIO_SGE_RX_DMA_OFFSET));
  1162. }
  1163. static void
  1164. csio_init_intr_coalesce_parms(struct csio_hw *hw)
  1165. {
  1166. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1167. struct csio_sge *sge = &wrm->sge;
  1168. csio_sge_thresh_reg = csio_closest_thresh(sge, csio_intr_coalesce_cnt);
  1169. if (csio_intr_coalesce_cnt) {
  1170. csio_sge_thresh_reg = 0;
  1171. csio_sge_timer_reg = X_TIMERREG_RESTART_COUNTER;
  1172. return;
  1173. }
  1174. csio_sge_timer_reg = csio_closest_timer(sge, csio_intr_coalesce_time);
  1175. }
  1176. /*
  1177. * csio_wr_get_sge - Get SGE register values.
  1178. * @hw: HW module.
  1179. *
  1180. * Used by non-master functions and by master-functions relying on config file.
  1181. */
  1182. static void
  1183. csio_wr_get_sge(struct csio_hw *hw)
  1184. {
  1185. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1186. struct csio_sge *sge = &wrm->sge;
  1187. uint32_t ingpad;
  1188. int i;
  1189. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  1190. u32 ingress_rx_threshold;
  1191. sge->sge_control = csio_rd_reg32(hw, SGE_CONTROL);
  1192. ingpad = INGPADBOUNDARY_GET(sge->sge_control);
  1193. switch (ingpad) {
  1194. case X_INGPCIEBOUNDARY_32B:
  1195. sge->csio_fl_align = 32; break;
  1196. case X_INGPCIEBOUNDARY_64B:
  1197. sge->csio_fl_align = 64; break;
  1198. case X_INGPCIEBOUNDARY_128B:
  1199. sge->csio_fl_align = 128; break;
  1200. case X_INGPCIEBOUNDARY_256B:
  1201. sge->csio_fl_align = 256; break;
  1202. case X_INGPCIEBOUNDARY_512B:
  1203. sge->csio_fl_align = 512; break;
  1204. case X_INGPCIEBOUNDARY_1024B:
  1205. sge->csio_fl_align = 1024; break;
  1206. case X_INGPCIEBOUNDARY_2048B:
  1207. sge->csio_fl_align = 2048; break;
  1208. case X_INGPCIEBOUNDARY_4096B:
  1209. sge->csio_fl_align = 4096; break;
  1210. }
  1211. for (i = 0; i < CSIO_SGE_FL_SIZE_REGS; i++)
  1212. csio_get_flbuf_size(hw, sge, i);
  1213. timer_value_0_and_1 = csio_rd_reg32(hw, SGE_TIMER_VALUE_0_AND_1);
  1214. timer_value_2_and_3 = csio_rd_reg32(hw, SGE_TIMER_VALUE_2_AND_3);
  1215. timer_value_4_and_5 = csio_rd_reg32(hw, SGE_TIMER_VALUE_4_AND_5);
  1216. sge->timer_val[0] = (uint16_t)csio_core_ticks_to_us(hw,
  1217. TIMERVALUE0_GET(timer_value_0_and_1));
  1218. sge->timer_val[1] = (uint16_t)csio_core_ticks_to_us(hw,
  1219. TIMERVALUE1_GET(timer_value_0_and_1));
  1220. sge->timer_val[2] = (uint16_t)csio_core_ticks_to_us(hw,
  1221. TIMERVALUE2_GET(timer_value_2_and_3));
  1222. sge->timer_val[3] = (uint16_t)csio_core_ticks_to_us(hw,
  1223. TIMERVALUE3_GET(timer_value_2_and_3));
  1224. sge->timer_val[4] = (uint16_t)csio_core_ticks_to_us(hw,
  1225. TIMERVALUE4_GET(timer_value_4_and_5));
  1226. sge->timer_val[5] = (uint16_t)csio_core_ticks_to_us(hw,
  1227. TIMERVALUE5_GET(timer_value_4_and_5));
  1228. ingress_rx_threshold = csio_rd_reg32(hw, SGE_INGRESS_RX_THRESHOLD);
  1229. sge->counter_val[0] = THRESHOLD_0_GET(ingress_rx_threshold);
  1230. sge->counter_val[1] = THRESHOLD_1_GET(ingress_rx_threshold);
  1231. sge->counter_val[2] = THRESHOLD_2_GET(ingress_rx_threshold);
  1232. sge->counter_val[3] = THRESHOLD_3_GET(ingress_rx_threshold);
  1233. csio_init_intr_coalesce_parms(hw);
  1234. }
  1235. /*
  1236. * csio_wr_set_sge - Initialize SGE registers
  1237. * @hw: HW module.
  1238. *
  1239. * Used by Master function to initialize SGE registers in the absence
  1240. * of a config file.
  1241. */
  1242. static void
  1243. csio_wr_set_sge(struct csio_hw *hw)
  1244. {
  1245. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1246. struct csio_sge *sge = &wrm->sge;
  1247. int i;
  1248. /*
  1249. * Set up our basic SGE mode to deliver CPL messages to our Ingress
  1250. * Queue and Packet Date to the Free List.
  1251. */
  1252. csio_set_reg_field(hw, SGE_CONTROL, RXPKTCPLMODE(1), RXPKTCPLMODE(1));
  1253. sge->sge_control = csio_rd_reg32(hw, SGE_CONTROL);
  1254. /* sge->csio_fl_align is set up by csio_wr_fixup_host_params(). */
  1255. /*
  1256. * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
  1257. * and generate an interrupt when this occurs so we can recover.
  1258. */
  1259. csio_set_reg_field(hw, SGE_DBFIFO_STATUS,
  1260. HP_INT_THRESH(HP_INT_THRESH_MASK) |
  1261. LP_INT_THRESH(LP_INT_THRESH_MASK),
  1262. HP_INT_THRESH(CSIO_SGE_DBFIFO_INT_THRESH) |
  1263. LP_INT_THRESH(CSIO_SGE_DBFIFO_INT_THRESH));
  1264. csio_set_reg_field(hw, SGE_DOORBELL_CONTROL, ENABLE_DROP,
  1265. ENABLE_DROP);
  1266. /* SGE_FL_BUFFER_SIZE0 is set up by csio_wr_fixup_host_params(). */
  1267. CSIO_SET_FLBUF_SIZE(hw, 1, CSIO_SGE_FLBUF_SIZE1);
  1268. CSIO_SET_FLBUF_SIZE(hw, 2, CSIO_SGE_FLBUF_SIZE2);
  1269. CSIO_SET_FLBUF_SIZE(hw, 3, CSIO_SGE_FLBUF_SIZE3);
  1270. CSIO_SET_FLBUF_SIZE(hw, 4, CSIO_SGE_FLBUF_SIZE4);
  1271. CSIO_SET_FLBUF_SIZE(hw, 5, CSIO_SGE_FLBUF_SIZE5);
  1272. CSIO_SET_FLBUF_SIZE(hw, 6, CSIO_SGE_FLBUF_SIZE6);
  1273. CSIO_SET_FLBUF_SIZE(hw, 7, CSIO_SGE_FLBUF_SIZE7);
  1274. CSIO_SET_FLBUF_SIZE(hw, 8, CSIO_SGE_FLBUF_SIZE8);
  1275. for (i = 0; i < CSIO_SGE_FL_SIZE_REGS; i++)
  1276. csio_get_flbuf_size(hw, sge, i);
  1277. /* Initialize interrupt coalescing attributes */
  1278. sge->timer_val[0] = CSIO_SGE_TIMER_VAL_0;
  1279. sge->timer_val[1] = CSIO_SGE_TIMER_VAL_1;
  1280. sge->timer_val[2] = CSIO_SGE_TIMER_VAL_2;
  1281. sge->timer_val[3] = CSIO_SGE_TIMER_VAL_3;
  1282. sge->timer_val[4] = CSIO_SGE_TIMER_VAL_4;
  1283. sge->timer_val[5] = CSIO_SGE_TIMER_VAL_5;
  1284. sge->counter_val[0] = CSIO_SGE_INT_CNT_VAL_0;
  1285. sge->counter_val[1] = CSIO_SGE_INT_CNT_VAL_1;
  1286. sge->counter_val[2] = CSIO_SGE_INT_CNT_VAL_2;
  1287. sge->counter_val[3] = CSIO_SGE_INT_CNT_VAL_3;
  1288. csio_wr_reg32(hw, THRESHOLD_0(sge->counter_val[0]) |
  1289. THRESHOLD_1(sge->counter_val[1]) |
  1290. THRESHOLD_2(sge->counter_val[2]) |
  1291. THRESHOLD_3(sge->counter_val[3]),
  1292. SGE_INGRESS_RX_THRESHOLD);
  1293. csio_wr_reg32(hw,
  1294. TIMERVALUE0(csio_us_to_core_ticks(hw, sge->timer_val[0])) |
  1295. TIMERVALUE1(csio_us_to_core_ticks(hw, sge->timer_val[1])),
  1296. SGE_TIMER_VALUE_0_AND_1);
  1297. csio_wr_reg32(hw,
  1298. TIMERVALUE2(csio_us_to_core_ticks(hw, sge->timer_val[2])) |
  1299. TIMERVALUE3(csio_us_to_core_ticks(hw, sge->timer_val[3])),
  1300. SGE_TIMER_VALUE_2_AND_3);
  1301. csio_wr_reg32(hw,
  1302. TIMERVALUE4(csio_us_to_core_ticks(hw, sge->timer_val[4])) |
  1303. TIMERVALUE5(csio_us_to_core_ticks(hw, sge->timer_val[5])),
  1304. SGE_TIMER_VALUE_4_AND_5);
  1305. csio_init_intr_coalesce_parms(hw);
  1306. }
  1307. void
  1308. csio_wr_sge_init(struct csio_hw *hw)
  1309. {
  1310. /*
  1311. * If we are master:
  1312. * - If we plan to use the config file, we need to fixup some
  1313. * host specific registers, and read the rest of the SGE
  1314. * configuration.
  1315. * - If we dont plan to use the config file, we need to initialize
  1316. * SGE entirely, including fixing the host specific registers.
  1317. * If we arent the master, we are only allowed to read and work off of
  1318. * the already initialized SGE values.
  1319. *
  1320. * Therefore, before calling this function, we assume that the master-
  1321. * ship of the card, and whether to use config file or not, have
  1322. * already been decided. In other words, CSIO_HWF_USING_SOFT_PARAMS and
  1323. * CSIO_HWF_MASTER should be set/unset.
  1324. */
  1325. if (csio_is_hw_master(hw)) {
  1326. csio_wr_fixup_host_params(hw);
  1327. if (hw->flags & CSIO_HWF_USING_SOFT_PARAMS)
  1328. csio_wr_get_sge(hw);
  1329. else
  1330. csio_wr_set_sge(hw);
  1331. } else
  1332. csio_wr_get_sge(hw);
  1333. }
  1334. /*
  1335. * csio_wrm_init - Initialize Work request module.
  1336. * @wrm: WR module
  1337. * @hw: HW pointer
  1338. *
  1339. * Allocates memory for an array of queue pointers starting at q_arr.
  1340. */
  1341. int
  1342. csio_wrm_init(struct csio_wrm *wrm, struct csio_hw *hw)
  1343. {
  1344. int i;
  1345. if (!wrm->num_q) {
  1346. csio_err(hw, "Num queues is not set\n");
  1347. return -EINVAL;
  1348. }
  1349. wrm->q_arr = kzalloc(sizeof(struct csio_q *) * wrm->num_q, GFP_KERNEL);
  1350. if (!wrm->q_arr)
  1351. goto err;
  1352. for (i = 0; i < wrm->num_q; i++) {
  1353. wrm->q_arr[i] = kzalloc(sizeof(struct csio_q), GFP_KERNEL);
  1354. if (!wrm->q_arr[i]) {
  1355. while (--i >= 0)
  1356. kfree(wrm->q_arr[i]);
  1357. goto err_free_arr;
  1358. }
  1359. }
  1360. wrm->free_qidx = 0;
  1361. return 0;
  1362. err_free_arr:
  1363. kfree(wrm->q_arr);
  1364. err:
  1365. return -ENOMEM;
  1366. }
  1367. /*
  1368. * csio_wrm_exit - Initialize Work request module.
  1369. * @wrm: WR module
  1370. * @hw: HW module
  1371. *
  1372. * Uninitialize WR module. Free q_arr and pointers in it.
  1373. * We have the additional job of freeing the DMA memory associated
  1374. * with the queues.
  1375. */
  1376. void
  1377. csio_wrm_exit(struct csio_wrm *wrm, struct csio_hw *hw)
  1378. {
  1379. int i;
  1380. uint32_t j;
  1381. struct csio_q *q;
  1382. struct csio_dma_buf *buf;
  1383. for (i = 0; i < wrm->num_q; i++) {
  1384. q = wrm->q_arr[i];
  1385. if (wrm->free_qidx && (i < wrm->free_qidx)) {
  1386. if (q->type == CSIO_FREELIST) {
  1387. if (!q->un.fl.bufs)
  1388. continue;
  1389. for (j = 0; j < q->credits; j++) {
  1390. buf = &q->un.fl.bufs[j];
  1391. if (!buf->vaddr)
  1392. continue;
  1393. pci_free_consistent(hw->pdev, buf->len,
  1394. buf->vaddr,
  1395. buf->paddr);
  1396. }
  1397. kfree(q->un.fl.bufs);
  1398. }
  1399. pci_free_consistent(hw->pdev, q->size,
  1400. q->vstart, q->pstart);
  1401. }
  1402. kfree(q);
  1403. }
  1404. hw->flags &= ~CSIO_HWF_Q_MEM_ALLOCED;
  1405. kfree(wrm->q_arr);
  1406. }