csio_wr.c 45 KB

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