csio_wr.c 45 KB

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