csio_wr.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648
  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. CSIO_HW_PIDX(hw, 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. CSIO_HW_PIDX(hw, q->inc_idx),
  856. MYPF_REG(SGE_PF_KDOORBELL));
  857. q->inc_idx = 0;
  858. return 0;
  859. }
  860. static inline uint32_t
  861. csio_wr_avail_qcredits(struct csio_q *q)
  862. {
  863. if (q->pidx > q->cidx)
  864. return q->pidx - q->cidx;
  865. else if (q->cidx > q->pidx)
  866. return q->credits - (q->cidx - q->pidx);
  867. else
  868. return 0; /* cidx == pidx, empty queue */
  869. }
  870. /*
  871. * csio_wr_inval_flq_buf - Invalidate a free list buffer entry.
  872. * @hw: HW module.
  873. * @flq: The freelist queue.
  874. *
  875. * Invalidate the driver's version of a freelist buffer entry,
  876. * without freeing the associated the DMA memory. The entry
  877. * to be invalidated is picked up from the current Free list
  878. * queue cidx.
  879. *
  880. */
  881. static inline void
  882. csio_wr_inval_flq_buf(struct csio_hw *hw, struct csio_q *flq)
  883. {
  884. flq->cidx++;
  885. if (flq->cidx == flq->credits) {
  886. flq->cidx = 0;
  887. CSIO_INC_STATS(flq, n_qwrap);
  888. }
  889. }
  890. /*
  891. * csio_wr_process_fl - Process a freelist completion.
  892. * @hw: HW module.
  893. * @q: The ingress queue attached to the Freelist.
  894. * @wr: The freelist completion WR in the ingress queue.
  895. * @len_to_qid: The lower 32-bits of the first flit of the RSP footer
  896. * @iq_handler: Caller's handler for this completion.
  897. * @priv: Private pointer of caller
  898. *
  899. */
  900. static inline void
  901. csio_wr_process_fl(struct csio_hw *hw, struct csio_q *q,
  902. void *wr, uint32_t len_to_qid,
  903. void (*iq_handler)(struct csio_hw *, void *,
  904. uint32_t, struct csio_fl_dma_buf *,
  905. void *),
  906. void *priv)
  907. {
  908. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  909. struct csio_sge *sge = &wrm->sge;
  910. struct csio_fl_dma_buf flb;
  911. struct csio_dma_buf *buf, *fbuf;
  912. uint32_t bufsz, len, lastlen = 0;
  913. int n;
  914. struct csio_q *flq = hw->wrm.q_arr[q->un.iq.flq_idx];
  915. CSIO_DB_ASSERT(flq != NULL);
  916. len = len_to_qid;
  917. if (len & IQWRF_NEWBUF) {
  918. if (flq->un.fl.offset > 0) {
  919. csio_wr_inval_flq_buf(hw, flq);
  920. flq->un.fl.offset = 0;
  921. }
  922. len = IQWRF_LEN_GET(len);
  923. }
  924. CSIO_DB_ASSERT(len != 0);
  925. flb.totlen = len;
  926. /* Consume all freelist buffers used for len bytes */
  927. for (n = 0, fbuf = flb.flbufs; ; n++, fbuf++) {
  928. buf = &flq->un.fl.bufs[flq->cidx];
  929. bufsz = csio_wr_fl_bufsz(sge, buf);
  930. fbuf->paddr = buf->paddr;
  931. fbuf->vaddr = buf->vaddr;
  932. flb.offset = flq->un.fl.offset;
  933. lastlen = min(bufsz, len);
  934. fbuf->len = lastlen;
  935. len -= lastlen;
  936. if (!len)
  937. break;
  938. csio_wr_inval_flq_buf(hw, flq);
  939. }
  940. flb.defer_free = flq->un.fl.packen ? 0 : 1;
  941. iq_handler(hw, wr, q->wr_sz - sizeof(struct csio_iqwr_footer),
  942. &flb, priv);
  943. if (flq->un.fl.packen)
  944. flq->un.fl.offset += ALIGN(lastlen, sge->csio_fl_align);
  945. else
  946. csio_wr_inval_flq_buf(hw, flq);
  947. }
  948. /*
  949. * csio_is_new_iqwr - Is this a new Ingress queue entry ?
  950. * @q: Ingress quueue.
  951. * @ftr: Ingress queue WR SGE footer.
  952. *
  953. * The entry is new if our generation bit matches the corresponding
  954. * bit in the footer of the current WR.
  955. */
  956. static inline bool
  957. csio_is_new_iqwr(struct csio_q *q, struct csio_iqwr_footer *ftr)
  958. {
  959. return (q->un.iq.genbit == (ftr->u.type_gen >> IQWRF_GEN_SHIFT));
  960. }
  961. /*
  962. * csio_wr_process_iq - Process elements in Ingress queue.
  963. * @hw: HW pointer
  964. * @qidx: Index of queue
  965. * @iq_handler: Handler for this queue
  966. * @priv: Caller's private pointer
  967. *
  968. * This routine walks through every entry of the ingress queue, calling
  969. * the provided iq_handler with the entry, until the generation bit
  970. * flips.
  971. */
  972. int
  973. csio_wr_process_iq(struct csio_hw *hw, struct csio_q *q,
  974. void (*iq_handler)(struct csio_hw *, void *,
  975. uint32_t, struct csio_fl_dma_buf *,
  976. void *),
  977. void *priv)
  978. {
  979. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  980. void *wr = (void *)((uintptr_t)q->vstart + (q->cidx * q->wr_sz));
  981. struct csio_iqwr_footer *ftr;
  982. uint32_t wr_type, fw_qid, qid;
  983. struct csio_q *q_completed;
  984. struct csio_q *flq = csio_iq_has_fl(q) ?
  985. wrm->q_arr[q->un.iq.flq_idx] : NULL;
  986. int rv = 0;
  987. /* Get the footer */
  988. ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
  989. (q->wr_sz - sizeof(*ftr)));
  990. /*
  991. * When q wrapped around last time, driver should have inverted
  992. * ic.genbit as well.
  993. */
  994. while (csio_is_new_iqwr(q, ftr)) {
  995. CSIO_DB_ASSERT(((uintptr_t)wr + q->wr_sz) <=
  996. (uintptr_t)q->vwrap);
  997. rmb();
  998. wr_type = IQWRF_TYPE_GET(ftr->u.type_gen);
  999. switch (wr_type) {
  1000. case X_RSPD_TYPE_CPL:
  1001. /* Subtract footer from WR len */
  1002. iq_handler(hw, wr, q->wr_sz - sizeof(*ftr), NULL, priv);
  1003. break;
  1004. case X_RSPD_TYPE_FLBUF:
  1005. csio_wr_process_fl(hw, q, wr,
  1006. ntohl(ftr->pldbuflen_qid),
  1007. iq_handler, priv);
  1008. break;
  1009. case X_RSPD_TYPE_INTR:
  1010. fw_qid = ntohl(ftr->pldbuflen_qid);
  1011. qid = fw_qid - wrm->fw_iq_start;
  1012. q_completed = hw->wrm.intr_map[qid];
  1013. if (unlikely(qid ==
  1014. csio_q_physiqid(hw, hw->intr_iq_idx))) {
  1015. /*
  1016. * We are already in the Forward Interrupt
  1017. * Interrupt Queue Service! Do-not service
  1018. * again!
  1019. *
  1020. */
  1021. } else {
  1022. CSIO_DB_ASSERT(q_completed);
  1023. CSIO_DB_ASSERT(
  1024. q_completed->un.iq.iq_intx_handler);
  1025. /* Call the queue handler. */
  1026. q_completed->un.iq.iq_intx_handler(hw, NULL,
  1027. 0, NULL, (void *)q_completed);
  1028. }
  1029. break;
  1030. default:
  1031. csio_warn(hw, "Unknown resp type 0x%x received\n",
  1032. wr_type);
  1033. CSIO_INC_STATS(q, n_rsp_unknown);
  1034. break;
  1035. }
  1036. /*
  1037. * Ingress *always* has fixed size WR entries. Therefore,
  1038. * there should always be complete WRs towards the end of
  1039. * queue.
  1040. */
  1041. if (((uintptr_t)wr + q->wr_sz) == (uintptr_t)q->vwrap) {
  1042. /* Roll over to start of queue */
  1043. q->cidx = 0;
  1044. wr = q->vstart;
  1045. /* Toggle genbit */
  1046. q->un.iq.genbit ^= 0x1;
  1047. CSIO_INC_STATS(q, n_qwrap);
  1048. } else {
  1049. q->cidx++;
  1050. wr = (void *)((uintptr_t)(q->vstart) +
  1051. (q->cidx * q->wr_sz));
  1052. }
  1053. ftr = (struct csio_iqwr_footer *)((uintptr_t)wr +
  1054. (q->wr_sz - sizeof(*ftr)));
  1055. q->inc_idx++;
  1056. } /* while (q->un.iq.genbit == hdr->genbit) */
  1057. /*
  1058. * We need to re-arm SGE interrupts in case we got a stray interrupt,
  1059. * especially in msix mode. With INTx, this may be a common occurence.
  1060. */
  1061. if (unlikely(!q->inc_idx)) {
  1062. CSIO_INC_STATS(q, n_stray_comp);
  1063. rv = -EINVAL;
  1064. goto restart;
  1065. }
  1066. /* Replenish free list buffers if pending falls below low water mark */
  1067. if (flq) {
  1068. uint32_t avail = csio_wr_avail_qcredits(flq);
  1069. if (avail <= 16) {
  1070. /* Make sure in FLQ, atleast 1 credit (8 FL buffers)
  1071. * remains unpopulated otherwise HW thinks
  1072. * FLQ is empty.
  1073. */
  1074. csio_wr_update_fl(hw, flq, (flq->credits - 8) - avail);
  1075. csio_wr_ring_fldb(hw, flq);
  1076. }
  1077. }
  1078. restart:
  1079. /* Now inform SGE about our incremental index value */
  1080. csio_wr_reg32(hw, CIDXINC(q->inc_idx) |
  1081. INGRESSQID(q->un.iq.physiqid) |
  1082. TIMERREG(csio_sge_timer_reg),
  1083. MYPF_REG(SGE_PF_GTS));
  1084. q->stats.n_tot_rsps += q->inc_idx;
  1085. q->inc_idx = 0;
  1086. return rv;
  1087. }
  1088. int
  1089. csio_wr_process_iq_idx(struct csio_hw *hw, int qidx,
  1090. void (*iq_handler)(struct csio_hw *, void *,
  1091. uint32_t, struct csio_fl_dma_buf *,
  1092. void *),
  1093. void *priv)
  1094. {
  1095. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1096. struct csio_q *iq = wrm->q_arr[qidx];
  1097. return csio_wr_process_iq(hw, iq, iq_handler, priv);
  1098. }
  1099. static int
  1100. csio_closest_timer(struct csio_sge *s, int time)
  1101. {
  1102. int i, delta, match = 0, min_delta = INT_MAX;
  1103. for (i = 0; i < ARRAY_SIZE(s->timer_val); i++) {
  1104. delta = time - s->timer_val[i];
  1105. if (delta < 0)
  1106. delta = -delta;
  1107. if (delta < min_delta) {
  1108. min_delta = delta;
  1109. match = i;
  1110. }
  1111. }
  1112. return match;
  1113. }
  1114. static int
  1115. csio_closest_thresh(struct csio_sge *s, int cnt)
  1116. {
  1117. int i, delta, match = 0, min_delta = INT_MAX;
  1118. for (i = 0; i < ARRAY_SIZE(s->counter_val); i++) {
  1119. delta = cnt - s->counter_val[i];
  1120. if (delta < 0)
  1121. delta = -delta;
  1122. if (delta < min_delta) {
  1123. min_delta = delta;
  1124. match = i;
  1125. }
  1126. }
  1127. return match;
  1128. }
  1129. static void
  1130. csio_wr_fixup_host_params(struct csio_hw *hw)
  1131. {
  1132. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1133. struct csio_sge *sge = &wrm->sge;
  1134. uint32_t clsz = L1_CACHE_BYTES;
  1135. uint32_t s_hps = PAGE_SHIFT - 10;
  1136. uint32_t ingpad = 0;
  1137. uint32_t stat_len = clsz > 64 ? 128 : 64;
  1138. csio_wr_reg32(hw, HOSTPAGESIZEPF0(s_hps) | HOSTPAGESIZEPF1(s_hps) |
  1139. HOSTPAGESIZEPF2(s_hps) | HOSTPAGESIZEPF3(s_hps) |
  1140. HOSTPAGESIZEPF4(s_hps) | HOSTPAGESIZEPF5(s_hps) |
  1141. HOSTPAGESIZEPF6(s_hps) | HOSTPAGESIZEPF7(s_hps),
  1142. SGE_HOST_PAGE_SIZE);
  1143. sge->csio_fl_align = clsz < 32 ? 32 : clsz;
  1144. ingpad = ilog2(sge->csio_fl_align) - 5;
  1145. csio_set_reg_field(hw, SGE_CONTROL, INGPADBOUNDARY_MASK |
  1146. EGRSTATUSPAGESIZE(1),
  1147. INGPADBOUNDARY(ingpad) |
  1148. EGRSTATUSPAGESIZE(stat_len != 64));
  1149. /* FL BUFFER SIZE#0 is Page size i,e already aligned to cache line */
  1150. csio_wr_reg32(hw, PAGE_SIZE, SGE_FL_BUFFER_SIZE0);
  1151. /*
  1152. * If using hard params, the following will get set correctly
  1153. * in csio_wr_set_sge().
  1154. */
  1155. if (hw->flags & CSIO_HWF_USING_SOFT_PARAMS) {
  1156. csio_wr_reg32(hw,
  1157. (csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE2) +
  1158. sge->csio_fl_align - 1) & ~(sge->csio_fl_align - 1),
  1159. SGE_FL_BUFFER_SIZE2);
  1160. csio_wr_reg32(hw,
  1161. (csio_rd_reg32(hw, SGE_FL_BUFFER_SIZE3) +
  1162. sge->csio_fl_align - 1) & ~(sge->csio_fl_align - 1),
  1163. SGE_FL_BUFFER_SIZE3);
  1164. }
  1165. csio_wr_reg32(hw, HPZ0(PAGE_SHIFT - 12), ULP_RX_TDDP_PSZ);
  1166. /* default value of rx_dma_offset of the NIC driver */
  1167. csio_set_reg_field(hw, SGE_CONTROL, PKTSHIFT_MASK,
  1168. PKTSHIFT(CSIO_SGE_RX_DMA_OFFSET));
  1169. csio_hw_tp_wr_bits_indirect(hw, TP_INGRESS_CONFIG,
  1170. CSUM_HAS_PSEUDO_HDR, 0);
  1171. }
  1172. static void
  1173. csio_init_intr_coalesce_parms(struct csio_hw *hw)
  1174. {
  1175. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1176. struct csio_sge *sge = &wrm->sge;
  1177. csio_sge_thresh_reg = csio_closest_thresh(sge, csio_intr_coalesce_cnt);
  1178. if (csio_intr_coalesce_cnt) {
  1179. csio_sge_thresh_reg = 0;
  1180. csio_sge_timer_reg = X_TIMERREG_RESTART_COUNTER;
  1181. return;
  1182. }
  1183. csio_sge_timer_reg = csio_closest_timer(sge, csio_intr_coalesce_time);
  1184. }
  1185. /*
  1186. * csio_wr_get_sge - Get SGE register values.
  1187. * @hw: HW module.
  1188. *
  1189. * Used by non-master functions and by master-functions relying on config file.
  1190. */
  1191. static void
  1192. csio_wr_get_sge(struct csio_hw *hw)
  1193. {
  1194. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1195. struct csio_sge *sge = &wrm->sge;
  1196. uint32_t ingpad;
  1197. int i;
  1198. u32 timer_value_0_and_1, timer_value_2_and_3, timer_value_4_and_5;
  1199. u32 ingress_rx_threshold;
  1200. sge->sge_control = csio_rd_reg32(hw, SGE_CONTROL);
  1201. ingpad = INGPADBOUNDARY_GET(sge->sge_control);
  1202. switch (ingpad) {
  1203. case X_INGPCIEBOUNDARY_32B:
  1204. sge->csio_fl_align = 32; break;
  1205. case X_INGPCIEBOUNDARY_64B:
  1206. sge->csio_fl_align = 64; break;
  1207. case X_INGPCIEBOUNDARY_128B:
  1208. sge->csio_fl_align = 128; break;
  1209. case X_INGPCIEBOUNDARY_256B:
  1210. sge->csio_fl_align = 256; break;
  1211. case X_INGPCIEBOUNDARY_512B:
  1212. sge->csio_fl_align = 512; break;
  1213. case X_INGPCIEBOUNDARY_1024B:
  1214. sge->csio_fl_align = 1024; break;
  1215. case X_INGPCIEBOUNDARY_2048B:
  1216. sge->csio_fl_align = 2048; break;
  1217. case X_INGPCIEBOUNDARY_4096B:
  1218. sge->csio_fl_align = 4096; break;
  1219. }
  1220. for (i = 0; i < CSIO_SGE_FL_SIZE_REGS; i++)
  1221. csio_get_flbuf_size(hw, sge, i);
  1222. timer_value_0_and_1 = csio_rd_reg32(hw, SGE_TIMER_VALUE_0_AND_1);
  1223. timer_value_2_and_3 = csio_rd_reg32(hw, SGE_TIMER_VALUE_2_AND_3);
  1224. timer_value_4_and_5 = csio_rd_reg32(hw, SGE_TIMER_VALUE_4_AND_5);
  1225. sge->timer_val[0] = (uint16_t)csio_core_ticks_to_us(hw,
  1226. TIMERVALUE0_GET(timer_value_0_and_1));
  1227. sge->timer_val[1] = (uint16_t)csio_core_ticks_to_us(hw,
  1228. TIMERVALUE1_GET(timer_value_0_and_1));
  1229. sge->timer_val[2] = (uint16_t)csio_core_ticks_to_us(hw,
  1230. TIMERVALUE2_GET(timer_value_2_and_3));
  1231. sge->timer_val[3] = (uint16_t)csio_core_ticks_to_us(hw,
  1232. TIMERVALUE3_GET(timer_value_2_and_3));
  1233. sge->timer_val[4] = (uint16_t)csio_core_ticks_to_us(hw,
  1234. TIMERVALUE4_GET(timer_value_4_and_5));
  1235. sge->timer_val[5] = (uint16_t)csio_core_ticks_to_us(hw,
  1236. TIMERVALUE5_GET(timer_value_4_and_5));
  1237. ingress_rx_threshold = csio_rd_reg32(hw, SGE_INGRESS_RX_THRESHOLD);
  1238. sge->counter_val[0] = THRESHOLD_0_GET(ingress_rx_threshold);
  1239. sge->counter_val[1] = THRESHOLD_1_GET(ingress_rx_threshold);
  1240. sge->counter_val[2] = THRESHOLD_2_GET(ingress_rx_threshold);
  1241. sge->counter_val[3] = THRESHOLD_3_GET(ingress_rx_threshold);
  1242. csio_init_intr_coalesce_parms(hw);
  1243. }
  1244. /*
  1245. * csio_wr_set_sge - Initialize SGE registers
  1246. * @hw: HW module.
  1247. *
  1248. * Used by Master function to initialize SGE registers in the absence
  1249. * of a config file.
  1250. */
  1251. static void
  1252. csio_wr_set_sge(struct csio_hw *hw)
  1253. {
  1254. struct csio_wrm *wrm = csio_hw_to_wrm(hw);
  1255. struct csio_sge *sge = &wrm->sge;
  1256. int i;
  1257. /*
  1258. * Set up our basic SGE mode to deliver CPL messages to our Ingress
  1259. * Queue and Packet Date to the Free List.
  1260. */
  1261. csio_set_reg_field(hw, SGE_CONTROL, RXPKTCPLMODE(1), RXPKTCPLMODE(1));
  1262. sge->sge_control = csio_rd_reg32(hw, SGE_CONTROL);
  1263. /* sge->csio_fl_align is set up by csio_wr_fixup_host_params(). */
  1264. /*
  1265. * Set up to drop DOORBELL writes when the DOORBELL FIFO overflows
  1266. * and generate an interrupt when this occurs so we can recover.
  1267. */
  1268. csio_set_reg_field(hw, SGE_DBFIFO_STATUS,
  1269. HP_INT_THRESH(HP_INT_THRESH_MASK) |
  1270. CSIO_HW_LP_INT_THRESH(hw, CSIO_HW_M_LP_INT_THRESH(hw)),
  1271. HP_INT_THRESH(CSIO_SGE_DBFIFO_INT_THRESH) |
  1272. CSIO_HW_LP_INT_THRESH(hw, CSIO_SGE_DBFIFO_INT_THRESH));
  1273. csio_set_reg_field(hw, SGE_DOORBELL_CONTROL, ENABLE_DROP,
  1274. ENABLE_DROP);
  1275. /* SGE_FL_BUFFER_SIZE0 is set up by csio_wr_fixup_host_params(). */
  1276. CSIO_SET_FLBUF_SIZE(hw, 1, CSIO_SGE_FLBUF_SIZE1);
  1277. csio_wr_reg32(hw, (CSIO_SGE_FLBUF_SIZE2 + sge->csio_fl_align - 1)
  1278. & ~(sge->csio_fl_align - 1), SGE_FL_BUFFER_SIZE2);
  1279. csio_wr_reg32(hw, (CSIO_SGE_FLBUF_SIZE3 + sge->csio_fl_align - 1)
  1280. & ~(sge->csio_fl_align - 1), SGE_FL_BUFFER_SIZE3);
  1281. CSIO_SET_FLBUF_SIZE(hw, 4, CSIO_SGE_FLBUF_SIZE4);
  1282. CSIO_SET_FLBUF_SIZE(hw, 5, CSIO_SGE_FLBUF_SIZE5);
  1283. CSIO_SET_FLBUF_SIZE(hw, 6, CSIO_SGE_FLBUF_SIZE6);
  1284. CSIO_SET_FLBUF_SIZE(hw, 7, CSIO_SGE_FLBUF_SIZE7);
  1285. CSIO_SET_FLBUF_SIZE(hw, 8, CSIO_SGE_FLBUF_SIZE8);
  1286. for (i = 0; i < CSIO_SGE_FL_SIZE_REGS; i++)
  1287. csio_get_flbuf_size(hw, sge, i);
  1288. /* Initialize interrupt coalescing attributes */
  1289. sge->timer_val[0] = CSIO_SGE_TIMER_VAL_0;
  1290. sge->timer_val[1] = CSIO_SGE_TIMER_VAL_1;
  1291. sge->timer_val[2] = CSIO_SGE_TIMER_VAL_2;
  1292. sge->timer_val[3] = CSIO_SGE_TIMER_VAL_3;
  1293. sge->timer_val[4] = CSIO_SGE_TIMER_VAL_4;
  1294. sge->timer_val[5] = CSIO_SGE_TIMER_VAL_5;
  1295. sge->counter_val[0] = CSIO_SGE_INT_CNT_VAL_0;
  1296. sge->counter_val[1] = CSIO_SGE_INT_CNT_VAL_1;
  1297. sge->counter_val[2] = CSIO_SGE_INT_CNT_VAL_2;
  1298. sge->counter_val[3] = CSIO_SGE_INT_CNT_VAL_3;
  1299. csio_wr_reg32(hw, THRESHOLD_0(sge->counter_val[0]) |
  1300. THRESHOLD_1(sge->counter_val[1]) |
  1301. THRESHOLD_2(sge->counter_val[2]) |
  1302. THRESHOLD_3(sge->counter_val[3]),
  1303. SGE_INGRESS_RX_THRESHOLD);
  1304. csio_wr_reg32(hw,
  1305. TIMERVALUE0(csio_us_to_core_ticks(hw, sge->timer_val[0])) |
  1306. TIMERVALUE1(csio_us_to_core_ticks(hw, sge->timer_val[1])),
  1307. SGE_TIMER_VALUE_0_AND_1);
  1308. csio_wr_reg32(hw,
  1309. TIMERVALUE2(csio_us_to_core_ticks(hw, sge->timer_val[2])) |
  1310. TIMERVALUE3(csio_us_to_core_ticks(hw, sge->timer_val[3])),
  1311. SGE_TIMER_VALUE_2_AND_3);
  1312. csio_wr_reg32(hw,
  1313. TIMERVALUE4(csio_us_to_core_ticks(hw, sge->timer_val[4])) |
  1314. TIMERVALUE5(csio_us_to_core_ticks(hw, sge->timer_val[5])),
  1315. SGE_TIMER_VALUE_4_AND_5);
  1316. csio_init_intr_coalesce_parms(hw);
  1317. }
  1318. void
  1319. csio_wr_sge_init(struct csio_hw *hw)
  1320. {
  1321. /*
  1322. * If we are master and chip is not initialized:
  1323. * - If we plan to use the config file, we need to fixup some
  1324. * host specific registers, and read the rest of the SGE
  1325. * configuration.
  1326. * - If we dont plan to use the config file, we need to initialize
  1327. * SGE entirely, including fixing the host specific registers.
  1328. * If we are master and chip is initialized, just read and work off of
  1329. * the already initialized SGE values.
  1330. * If we arent the master, we are only allowed to read and work off of
  1331. * the already initialized SGE values.
  1332. *
  1333. * Therefore, before calling this function, we assume that the master-
  1334. * ship of the card, state and whether to use config file or not, have
  1335. * already been decided.
  1336. */
  1337. if (csio_is_hw_master(hw)) {
  1338. if (hw->fw_state != CSIO_DEV_STATE_INIT)
  1339. csio_wr_fixup_host_params(hw);
  1340. if (hw->flags & CSIO_HWF_USING_SOFT_PARAMS)
  1341. csio_wr_get_sge(hw);
  1342. else
  1343. csio_wr_set_sge(hw);
  1344. } else
  1345. csio_wr_get_sge(hw);
  1346. }
  1347. /*
  1348. * csio_wrm_init - Initialize Work request module.
  1349. * @wrm: WR module
  1350. * @hw: HW pointer
  1351. *
  1352. * Allocates memory for an array of queue pointers starting at q_arr.
  1353. */
  1354. int
  1355. csio_wrm_init(struct csio_wrm *wrm, struct csio_hw *hw)
  1356. {
  1357. int i;
  1358. if (!wrm->num_q) {
  1359. csio_err(hw, "Num queues is not set\n");
  1360. return -EINVAL;
  1361. }
  1362. wrm->q_arr = kzalloc(sizeof(struct csio_q *) * wrm->num_q, GFP_KERNEL);
  1363. if (!wrm->q_arr)
  1364. goto err;
  1365. for (i = 0; i < wrm->num_q; i++) {
  1366. wrm->q_arr[i] = kzalloc(sizeof(struct csio_q), GFP_KERNEL);
  1367. if (!wrm->q_arr[i]) {
  1368. while (--i >= 0)
  1369. kfree(wrm->q_arr[i]);
  1370. goto err_free_arr;
  1371. }
  1372. }
  1373. wrm->free_qidx = 0;
  1374. return 0;
  1375. err_free_arr:
  1376. kfree(wrm->q_arr);
  1377. err:
  1378. return -ENOMEM;
  1379. }
  1380. /*
  1381. * csio_wrm_exit - Initialize Work request module.
  1382. * @wrm: WR module
  1383. * @hw: HW module
  1384. *
  1385. * Uninitialize WR module. Free q_arr and pointers in it.
  1386. * We have the additional job of freeing the DMA memory associated
  1387. * with the queues.
  1388. */
  1389. void
  1390. csio_wrm_exit(struct csio_wrm *wrm, struct csio_hw *hw)
  1391. {
  1392. int i;
  1393. uint32_t j;
  1394. struct csio_q *q;
  1395. struct csio_dma_buf *buf;
  1396. for (i = 0; i < wrm->num_q; i++) {
  1397. q = wrm->q_arr[i];
  1398. if (wrm->free_qidx && (i < wrm->free_qidx)) {
  1399. if (q->type == CSIO_FREELIST) {
  1400. if (!q->un.fl.bufs)
  1401. continue;
  1402. for (j = 0; j < q->credits; j++) {
  1403. buf = &q->un.fl.bufs[j];
  1404. if (!buf->vaddr)
  1405. continue;
  1406. pci_free_consistent(hw->pdev, buf->len,
  1407. buf->vaddr,
  1408. buf->paddr);
  1409. }
  1410. kfree(q->un.fl.bufs);
  1411. }
  1412. pci_free_consistent(hw->pdev, q->size,
  1413. q->vstart, q->pstart);
  1414. }
  1415. kfree(q);
  1416. }
  1417. hw->flags &= ~CSIO_HWF_Q_MEM_ALLOCED;
  1418. kfree(wrm->q_arr);
  1419. }