csio_wr.c 43 KB

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