sge.c 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681
  1. /*
  2. * Copyright (c) 2005-2007 Chelsio, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/skbuff.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/ip.h>
  37. #include <linux/tcp.h>
  38. #include <linux/dma-mapping.h>
  39. #include "common.h"
  40. #include "regs.h"
  41. #include "sge_defs.h"
  42. #include "t3_cpl.h"
  43. #include "firmware_exports.h"
  44. #define USE_GTS 0
  45. #define SGE_RX_SM_BUF_SIZE 1536
  46. #define SGE_RX_COPY_THRES 256
  47. # define SGE_RX_DROP_THRES 16
  48. /*
  49. * Period of the Tx buffer reclaim timer. This timer does not need to run
  50. * frequently as Tx buffers are usually reclaimed by new Tx packets.
  51. */
  52. #define TX_RECLAIM_PERIOD (HZ / 4)
  53. /* WR size in bytes */
  54. #define WR_LEN (WR_FLITS * 8)
  55. /*
  56. * Types of Tx queues in each queue set. Order here matters, do not change.
  57. */
  58. enum { TXQ_ETH, TXQ_OFLD, TXQ_CTRL };
  59. /* Values for sge_txq.flags */
  60. enum {
  61. TXQ_RUNNING = 1 << 0, /* fetch engine is running */
  62. TXQ_LAST_PKT_DB = 1 << 1, /* last packet rang the doorbell */
  63. };
  64. struct tx_desc {
  65. u64 flit[TX_DESC_FLITS];
  66. };
  67. struct rx_desc {
  68. __be32 addr_lo;
  69. __be32 len_gen;
  70. __be32 gen2;
  71. __be32 addr_hi;
  72. };
  73. struct tx_sw_desc { /* SW state per Tx descriptor */
  74. struct sk_buff *skb;
  75. };
  76. struct rx_sw_desc { /* SW state per Rx descriptor */
  77. struct sk_buff *skb;
  78. DECLARE_PCI_UNMAP_ADDR(dma_addr);
  79. };
  80. struct rsp_desc { /* response queue descriptor */
  81. struct rss_header rss_hdr;
  82. __be32 flags;
  83. __be32 len_cq;
  84. u8 imm_data[47];
  85. u8 intr_gen;
  86. };
  87. struct unmap_info { /* packet unmapping info, overlays skb->cb */
  88. int sflit; /* start flit of first SGL entry in Tx descriptor */
  89. u16 fragidx; /* first page fragment in current Tx descriptor */
  90. u16 addr_idx; /* buffer index of first SGL entry in descriptor */
  91. u32 len; /* mapped length of skb main body */
  92. };
  93. /*
  94. * Maps a number of flits to the number of Tx descriptors that can hold them.
  95. * The formula is
  96. *
  97. * desc = 1 + (flits - 2) / (WR_FLITS - 1).
  98. *
  99. * HW allows up to 4 descriptors to be combined into a WR.
  100. */
  101. static u8 flit_desc_map[] = {
  102. 0,
  103. #if SGE_NUM_GENBITS == 1
  104. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  105. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  106. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  107. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
  108. #elif SGE_NUM_GENBITS == 2
  109. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  110. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  111. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  112. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  113. #else
  114. # error "SGE_NUM_GENBITS must be 1 or 2"
  115. #endif
  116. };
  117. static inline struct sge_qset *fl_to_qset(const struct sge_fl *q, int qidx)
  118. {
  119. return container_of(q, struct sge_qset, fl[qidx]);
  120. }
  121. static inline struct sge_qset *rspq_to_qset(const struct sge_rspq *q)
  122. {
  123. return container_of(q, struct sge_qset, rspq);
  124. }
  125. static inline struct sge_qset *txq_to_qset(const struct sge_txq *q, int qidx)
  126. {
  127. return container_of(q, struct sge_qset, txq[qidx]);
  128. }
  129. /**
  130. * refill_rspq - replenish an SGE response queue
  131. * @adapter: the adapter
  132. * @q: the response queue to replenish
  133. * @credits: how many new responses to make available
  134. *
  135. * Replenishes a response queue by making the supplied number of responses
  136. * available to HW.
  137. */
  138. static inline void refill_rspq(struct adapter *adapter,
  139. const struct sge_rspq *q, unsigned int credits)
  140. {
  141. t3_write_reg(adapter, A_SG_RSPQ_CREDIT_RETURN,
  142. V_RSPQ(q->cntxt_id) | V_CREDITS(credits));
  143. }
  144. /**
  145. * need_skb_unmap - does the platform need unmapping of sk_buffs?
  146. *
  147. * Returns true if the platfrom needs sk_buff unmapping. The compiler
  148. * optimizes away unecessary code if this returns true.
  149. */
  150. static inline int need_skb_unmap(void)
  151. {
  152. /*
  153. * This structure is used to tell if the platfrom needs buffer
  154. * unmapping by checking if DECLARE_PCI_UNMAP_ADDR defines anything.
  155. */
  156. struct dummy {
  157. DECLARE_PCI_UNMAP_ADDR(addr);
  158. };
  159. return sizeof(struct dummy) != 0;
  160. }
  161. /**
  162. * unmap_skb - unmap a packet main body and its page fragments
  163. * @skb: the packet
  164. * @q: the Tx queue containing Tx descriptors for the packet
  165. * @cidx: index of Tx descriptor
  166. * @pdev: the PCI device
  167. *
  168. * Unmap the main body of an sk_buff and its page fragments, if any.
  169. * Because of the fairly complicated structure of our SGLs and the desire
  170. * to conserve space for metadata, we keep the information necessary to
  171. * unmap an sk_buff partly in the sk_buff itself (in its cb), and partly
  172. * in the Tx descriptors (the physical addresses of the various data
  173. * buffers). The send functions initialize the state in skb->cb so we
  174. * can unmap the buffers held in the first Tx descriptor here, and we
  175. * have enough information at this point to update the state for the next
  176. * Tx descriptor.
  177. */
  178. static inline void unmap_skb(struct sk_buff *skb, struct sge_txq *q,
  179. unsigned int cidx, struct pci_dev *pdev)
  180. {
  181. const struct sg_ent *sgp;
  182. struct unmap_info *ui = (struct unmap_info *)skb->cb;
  183. int nfrags, frag_idx, curflit, j = ui->addr_idx;
  184. sgp = (struct sg_ent *)&q->desc[cidx].flit[ui->sflit];
  185. if (ui->len) {
  186. pci_unmap_single(pdev, be64_to_cpu(sgp->addr[0]), ui->len,
  187. PCI_DMA_TODEVICE);
  188. ui->len = 0; /* so we know for next descriptor for this skb */
  189. j = 1;
  190. }
  191. frag_idx = ui->fragidx;
  192. curflit = ui->sflit + 1 + j;
  193. nfrags = skb_shinfo(skb)->nr_frags;
  194. while (frag_idx < nfrags && curflit < WR_FLITS) {
  195. pci_unmap_page(pdev, be64_to_cpu(sgp->addr[j]),
  196. skb_shinfo(skb)->frags[frag_idx].size,
  197. PCI_DMA_TODEVICE);
  198. j ^= 1;
  199. if (j == 0) {
  200. sgp++;
  201. curflit++;
  202. }
  203. curflit++;
  204. frag_idx++;
  205. }
  206. if (frag_idx < nfrags) { /* SGL continues into next Tx descriptor */
  207. ui->fragidx = frag_idx;
  208. ui->addr_idx = j;
  209. ui->sflit = curflit - WR_FLITS - j; /* sflit can be -1 */
  210. }
  211. }
  212. /**
  213. * free_tx_desc - reclaims Tx descriptors and their buffers
  214. * @adapter: the adapter
  215. * @q: the Tx queue to reclaim descriptors from
  216. * @n: the number of descriptors to reclaim
  217. *
  218. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  219. * Tx buffers. Called with the Tx queue lock held.
  220. */
  221. static void free_tx_desc(struct adapter *adapter, struct sge_txq *q,
  222. unsigned int n)
  223. {
  224. struct tx_sw_desc *d;
  225. struct pci_dev *pdev = adapter->pdev;
  226. unsigned int cidx = q->cidx;
  227. d = &q->sdesc[cidx];
  228. while (n--) {
  229. if (d->skb) { /* an SGL is present */
  230. if (need_skb_unmap())
  231. unmap_skb(d->skb, q, cidx, pdev);
  232. if (d->skb->priority == cidx)
  233. kfree_skb(d->skb);
  234. }
  235. ++d;
  236. if (++cidx == q->size) {
  237. cidx = 0;
  238. d = q->sdesc;
  239. }
  240. }
  241. q->cidx = cidx;
  242. }
  243. /**
  244. * reclaim_completed_tx - reclaims completed Tx descriptors
  245. * @adapter: the adapter
  246. * @q: the Tx queue to reclaim completed descriptors from
  247. *
  248. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  249. * and frees the associated buffers if possible. Called with the Tx
  250. * queue's lock held.
  251. */
  252. static inline void reclaim_completed_tx(struct adapter *adapter,
  253. struct sge_txq *q)
  254. {
  255. unsigned int reclaim = q->processed - q->cleaned;
  256. if (reclaim) {
  257. free_tx_desc(adapter, q, reclaim);
  258. q->cleaned += reclaim;
  259. q->in_use -= reclaim;
  260. }
  261. }
  262. /**
  263. * should_restart_tx - are there enough resources to restart a Tx queue?
  264. * @q: the Tx queue
  265. *
  266. * Checks if there are enough descriptors to restart a suspended Tx queue.
  267. */
  268. static inline int should_restart_tx(const struct sge_txq *q)
  269. {
  270. unsigned int r = q->processed - q->cleaned;
  271. return q->in_use - r < (q->size >> 1);
  272. }
  273. /**
  274. * free_rx_bufs - free the Rx buffers on an SGE free list
  275. * @pdev: the PCI device associated with the adapter
  276. * @rxq: the SGE free list to clean up
  277. *
  278. * Release the buffers on an SGE free-buffer Rx queue. HW fetching from
  279. * this queue should be stopped before calling this function.
  280. */
  281. static void free_rx_bufs(struct pci_dev *pdev, struct sge_fl *q)
  282. {
  283. unsigned int cidx = q->cidx;
  284. while (q->credits--) {
  285. struct rx_sw_desc *d = &q->sdesc[cidx];
  286. pci_unmap_single(pdev, pci_unmap_addr(d, dma_addr),
  287. q->buf_size, PCI_DMA_FROMDEVICE);
  288. kfree_skb(d->skb);
  289. d->skb = NULL;
  290. if (++cidx == q->size)
  291. cidx = 0;
  292. }
  293. }
  294. /**
  295. * add_one_rx_buf - add a packet buffer to a free-buffer list
  296. * @skb: the buffer to add
  297. * @len: the buffer length
  298. * @d: the HW Rx descriptor to write
  299. * @sd: the SW Rx descriptor to write
  300. * @gen: the generation bit value
  301. * @pdev: the PCI device associated with the adapter
  302. *
  303. * Add a buffer of the given length to the supplied HW and SW Rx
  304. * descriptors.
  305. */
  306. static inline void add_one_rx_buf(struct sk_buff *skb, unsigned int len,
  307. struct rx_desc *d, struct rx_sw_desc *sd,
  308. unsigned int gen, struct pci_dev *pdev)
  309. {
  310. dma_addr_t mapping;
  311. sd->skb = skb;
  312. mapping = pci_map_single(pdev, skb->data, len, PCI_DMA_FROMDEVICE);
  313. pci_unmap_addr_set(sd, dma_addr, mapping);
  314. d->addr_lo = cpu_to_be32(mapping);
  315. d->addr_hi = cpu_to_be32((u64) mapping >> 32);
  316. wmb();
  317. d->len_gen = cpu_to_be32(V_FLD_GEN1(gen));
  318. d->gen2 = cpu_to_be32(V_FLD_GEN2(gen));
  319. }
  320. /**
  321. * refill_fl - refill an SGE free-buffer list
  322. * @adapter: the adapter
  323. * @q: the free-list to refill
  324. * @n: the number of new buffers to allocate
  325. * @gfp: the gfp flags for allocating new buffers
  326. *
  327. * (Re)populate an SGE free-buffer list with up to @n new packet buffers,
  328. * allocated with the supplied gfp flags. The caller must assure that
  329. * @n does not exceed the queue's capacity.
  330. */
  331. static void refill_fl(struct adapter *adap, struct sge_fl *q, int n, gfp_t gfp)
  332. {
  333. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  334. struct rx_desc *d = &q->desc[q->pidx];
  335. while (n--) {
  336. struct sk_buff *skb = alloc_skb(q->buf_size, gfp);
  337. if (!skb)
  338. break;
  339. add_one_rx_buf(skb, q->buf_size, d, sd, q->gen, adap->pdev);
  340. d++;
  341. sd++;
  342. if (++q->pidx == q->size) {
  343. q->pidx = 0;
  344. q->gen ^= 1;
  345. sd = q->sdesc;
  346. d = q->desc;
  347. }
  348. q->credits++;
  349. }
  350. t3_write_reg(adap, A_SG_KDOORBELL, V_EGRCNTX(q->cntxt_id));
  351. }
  352. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  353. {
  354. refill_fl(adap, fl, min(16U, fl->size - fl->credits), GFP_ATOMIC);
  355. }
  356. /**
  357. * recycle_rx_buf - recycle a receive buffer
  358. * @adapter: the adapter
  359. * @q: the SGE free list
  360. * @idx: index of buffer to recycle
  361. *
  362. * Recycles the specified buffer on the given free list by adding it at
  363. * the next available slot on the list.
  364. */
  365. static void recycle_rx_buf(struct adapter *adap, struct sge_fl *q,
  366. unsigned int idx)
  367. {
  368. struct rx_desc *from = &q->desc[idx];
  369. struct rx_desc *to = &q->desc[q->pidx];
  370. q->sdesc[q->pidx] = q->sdesc[idx];
  371. to->addr_lo = from->addr_lo; /* already big endian */
  372. to->addr_hi = from->addr_hi; /* likewise */
  373. wmb();
  374. to->len_gen = cpu_to_be32(V_FLD_GEN1(q->gen));
  375. to->gen2 = cpu_to_be32(V_FLD_GEN2(q->gen));
  376. q->credits++;
  377. if (++q->pidx == q->size) {
  378. q->pidx = 0;
  379. q->gen ^= 1;
  380. }
  381. t3_write_reg(adap, A_SG_KDOORBELL, V_EGRCNTX(q->cntxt_id));
  382. }
  383. /**
  384. * alloc_ring - allocate resources for an SGE descriptor ring
  385. * @pdev: the PCI device
  386. * @nelem: the number of descriptors
  387. * @elem_size: the size of each descriptor
  388. * @sw_size: the size of the SW state associated with each ring element
  389. * @phys: the physical address of the allocated ring
  390. * @metadata: address of the array holding the SW state for the ring
  391. *
  392. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  393. * free buffer lists, or response queues. Each SGE ring requires
  394. * space for its HW descriptors plus, optionally, space for the SW state
  395. * associated with each HW entry (the metadata). The function returns
  396. * three values: the virtual address for the HW ring (the return value
  397. * of the function), the physical address of the HW ring, and the address
  398. * of the SW ring.
  399. */
  400. static void *alloc_ring(struct pci_dev *pdev, size_t nelem, size_t elem_size,
  401. size_t sw_size, dma_addr_t *phys, void *metadata)
  402. {
  403. size_t len = nelem * elem_size;
  404. void *s = NULL;
  405. void *p = dma_alloc_coherent(&pdev->dev, len, phys, GFP_KERNEL);
  406. if (!p)
  407. return NULL;
  408. if (sw_size) {
  409. s = kcalloc(nelem, sw_size, GFP_KERNEL);
  410. if (!s) {
  411. dma_free_coherent(&pdev->dev, len, p, *phys);
  412. return NULL;
  413. }
  414. }
  415. if (metadata)
  416. *(void **)metadata = s;
  417. memset(p, 0, len);
  418. return p;
  419. }
  420. /**
  421. * free_qset - free the resources of an SGE queue set
  422. * @adapter: the adapter owning the queue set
  423. * @q: the queue set
  424. *
  425. * Release the HW and SW resources associated with an SGE queue set, such
  426. * as HW contexts, packet buffers, and descriptor rings. Traffic to the
  427. * queue set must be quiesced prior to calling this.
  428. */
  429. void t3_free_qset(struct adapter *adapter, struct sge_qset *q)
  430. {
  431. int i;
  432. struct pci_dev *pdev = adapter->pdev;
  433. if (q->tx_reclaim_timer.function)
  434. del_timer_sync(&q->tx_reclaim_timer);
  435. for (i = 0; i < SGE_RXQ_PER_SET; ++i)
  436. if (q->fl[i].desc) {
  437. spin_lock(&adapter->sge.reg_lock);
  438. t3_sge_disable_fl(adapter, q->fl[i].cntxt_id);
  439. spin_unlock(&adapter->sge.reg_lock);
  440. free_rx_bufs(pdev, &q->fl[i]);
  441. kfree(q->fl[i].sdesc);
  442. dma_free_coherent(&pdev->dev,
  443. q->fl[i].size *
  444. sizeof(struct rx_desc), q->fl[i].desc,
  445. q->fl[i].phys_addr);
  446. }
  447. for (i = 0; i < SGE_TXQ_PER_SET; ++i)
  448. if (q->txq[i].desc) {
  449. spin_lock(&adapter->sge.reg_lock);
  450. t3_sge_enable_ecntxt(adapter, q->txq[i].cntxt_id, 0);
  451. spin_unlock(&adapter->sge.reg_lock);
  452. if (q->txq[i].sdesc) {
  453. free_tx_desc(adapter, &q->txq[i],
  454. q->txq[i].in_use);
  455. kfree(q->txq[i].sdesc);
  456. }
  457. dma_free_coherent(&pdev->dev,
  458. q->txq[i].size *
  459. sizeof(struct tx_desc),
  460. q->txq[i].desc, q->txq[i].phys_addr);
  461. __skb_queue_purge(&q->txq[i].sendq);
  462. }
  463. if (q->rspq.desc) {
  464. spin_lock(&adapter->sge.reg_lock);
  465. t3_sge_disable_rspcntxt(adapter, q->rspq.cntxt_id);
  466. spin_unlock(&adapter->sge.reg_lock);
  467. dma_free_coherent(&pdev->dev,
  468. q->rspq.size * sizeof(struct rsp_desc),
  469. q->rspq.desc, q->rspq.phys_addr);
  470. }
  471. if (q->netdev)
  472. q->netdev->atalk_ptr = NULL;
  473. memset(q, 0, sizeof(*q));
  474. }
  475. /**
  476. * init_qset_cntxt - initialize an SGE queue set context info
  477. * @qs: the queue set
  478. * @id: the queue set id
  479. *
  480. * Initializes the TIDs and context ids for the queues of a queue set.
  481. */
  482. static void init_qset_cntxt(struct sge_qset *qs, unsigned int id)
  483. {
  484. qs->rspq.cntxt_id = id;
  485. qs->fl[0].cntxt_id = 2 * id;
  486. qs->fl[1].cntxt_id = 2 * id + 1;
  487. qs->txq[TXQ_ETH].cntxt_id = FW_TUNNEL_SGEEC_START + id;
  488. qs->txq[TXQ_ETH].token = FW_TUNNEL_TID_START + id;
  489. qs->txq[TXQ_OFLD].cntxt_id = FW_OFLD_SGEEC_START + id;
  490. qs->txq[TXQ_CTRL].cntxt_id = FW_CTRL_SGEEC_START + id;
  491. qs->txq[TXQ_CTRL].token = FW_CTRL_TID_START + id;
  492. }
  493. /**
  494. * sgl_len - calculates the size of an SGL of the given capacity
  495. * @n: the number of SGL entries
  496. *
  497. * Calculates the number of flits needed for a scatter/gather list that
  498. * can hold the given number of entries.
  499. */
  500. static inline unsigned int sgl_len(unsigned int n)
  501. {
  502. /* alternatively: 3 * (n / 2) + 2 * (n & 1) */
  503. return (3 * n) / 2 + (n & 1);
  504. }
  505. /**
  506. * flits_to_desc - returns the num of Tx descriptors for the given flits
  507. * @n: the number of flits
  508. *
  509. * Calculates the number of Tx descriptors needed for the supplied number
  510. * of flits.
  511. */
  512. static inline unsigned int flits_to_desc(unsigned int n)
  513. {
  514. BUG_ON(n >= ARRAY_SIZE(flit_desc_map));
  515. return flit_desc_map[n];
  516. }
  517. /**
  518. * get_packet - return the next ingress packet buffer from a free list
  519. * @adap: the adapter that received the packet
  520. * @fl: the SGE free list holding the packet
  521. * @len: the packet length including any SGE padding
  522. * @drop_thres: # of remaining buffers before we start dropping packets
  523. *
  524. * Get the next packet from a free list and complete setup of the
  525. * sk_buff. If the packet is small we make a copy and recycle the
  526. * original buffer, otherwise we use the original buffer itself. If a
  527. * positive drop threshold is supplied packets are dropped and their
  528. * buffers recycled if (a) the number of remaining buffers is under the
  529. * threshold and the packet is too big to copy, or (b) the packet should
  530. * be copied but there is no memory for the copy.
  531. */
  532. static struct sk_buff *get_packet(struct adapter *adap, struct sge_fl *fl,
  533. unsigned int len, unsigned int drop_thres)
  534. {
  535. struct sk_buff *skb = NULL;
  536. struct rx_sw_desc *sd = &fl->sdesc[fl->cidx];
  537. prefetch(sd->skb->data);
  538. if (len <= SGE_RX_COPY_THRES) {
  539. skb = alloc_skb(len, GFP_ATOMIC);
  540. if (likely(skb != NULL)) {
  541. __skb_put(skb, len);
  542. pci_dma_sync_single_for_cpu(adap->pdev,
  543. pci_unmap_addr(sd,
  544. dma_addr),
  545. len, PCI_DMA_FROMDEVICE);
  546. memcpy(skb->data, sd->skb->data, len);
  547. pci_dma_sync_single_for_device(adap->pdev,
  548. pci_unmap_addr(sd,
  549. dma_addr),
  550. len, PCI_DMA_FROMDEVICE);
  551. } else if (!drop_thres)
  552. goto use_orig_buf;
  553. recycle:
  554. recycle_rx_buf(adap, fl, fl->cidx);
  555. return skb;
  556. }
  557. if (unlikely(fl->credits < drop_thres))
  558. goto recycle;
  559. use_orig_buf:
  560. pci_unmap_single(adap->pdev, pci_unmap_addr(sd, dma_addr),
  561. fl->buf_size, PCI_DMA_FROMDEVICE);
  562. skb = sd->skb;
  563. skb_put(skb, len);
  564. __refill_fl(adap, fl);
  565. return skb;
  566. }
  567. /**
  568. * get_imm_packet - return the next ingress packet buffer from a response
  569. * @resp: the response descriptor containing the packet data
  570. *
  571. * Return a packet containing the immediate data of the given response.
  572. */
  573. static inline struct sk_buff *get_imm_packet(const struct rsp_desc *resp)
  574. {
  575. struct sk_buff *skb = alloc_skb(IMMED_PKT_SIZE, GFP_ATOMIC);
  576. if (skb) {
  577. __skb_put(skb, IMMED_PKT_SIZE);
  578. memcpy(skb->data, resp->imm_data, IMMED_PKT_SIZE);
  579. }
  580. return skb;
  581. }
  582. /**
  583. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  584. * @skb: the packet
  585. *
  586. * Returns the number of Tx descriptors needed for the given Ethernet
  587. * packet. Ethernet packets require addition of WR and CPL headers.
  588. */
  589. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  590. {
  591. unsigned int flits;
  592. if (skb->len <= WR_LEN - sizeof(struct cpl_tx_pkt))
  593. return 1;
  594. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 2;
  595. if (skb_shinfo(skb)->gso_size)
  596. flits++;
  597. return flits_to_desc(flits);
  598. }
  599. /**
  600. * make_sgl - populate a scatter/gather list for a packet
  601. * @skb: the packet
  602. * @sgp: the SGL to populate
  603. * @start: start address of skb main body data to include in the SGL
  604. * @len: length of skb main body data to include in the SGL
  605. * @pdev: the PCI device
  606. *
  607. * Generates a scatter/gather list for the buffers that make up a packet
  608. * and returns the SGL size in 8-byte words. The caller must size the SGL
  609. * appropriately.
  610. */
  611. static inline unsigned int make_sgl(const struct sk_buff *skb,
  612. struct sg_ent *sgp, unsigned char *start,
  613. unsigned int len, struct pci_dev *pdev)
  614. {
  615. dma_addr_t mapping;
  616. unsigned int i, j = 0, nfrags;
  617. if (len) {
  618. mapping = pci_map_single(pdev, start, len, PCI_DMA_TODEVICE);
  619. sgp->len[0] = cpu_to_be32(len);
  620. sgp->addr[0] = cpu_to_be64(mapping);
  621. j = 1;
  622. }
  623. nfrags = skb_shinfo(skb)->nr_frags;
  624. for (i = 0; i < nfrags; i++) {
  625. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  626. mapping = pci_map_page(pdev, frag->page, frag->page_offset,
  627. frag->size, PCI_DMA_TODEVICE);
  628. sgp->len[j] = cpu_to_be32(frag->size);
  629. sgp->addr[j] = cpu_to_be64(mapping);
  630. j ^= 1;
  631. if (j == 0)
  632. ++sgp;
  633. }
  634. if (j)
  635. sgp->len[j] = 0;
  636. return ((nfrags + (len != 0)) * 3) / 2 + j;
  637. }
  638. /**
  639. * check_ring_tx_db - check and potentially ring a Tx queue's doorbell
  640. * @adap: the adapter
  641. * @q: the Tx queue
  642. *
  643. * Ring the doorbel if a Tx queue is asleep. There is a natural race,
  644. * where the HW is going to sleep just after we checked, however,
  645. * then the interrupt handler will detect the outstanding TX packet
  646. * and ring the doorbell for us.
  647. *
  648. * When GTS is disabled we unconditionally ring the doorbell.
  649. */
  650. static inline void check_ring_tx_db(struct adapter *adap, struct sge_txq *q)
  651. {
  652. #if USE_GTS
  653. clear_bit(TXQ_LAST_PKT_DB, &q->flags);
  654. if (test_and_set_bit(TXQ_RUNNING, &q->flags) == 0) {
  655. set_bit(TXQ_LAST_PKT_DB, &q->flags);
  656. t3_write_reg(adap, A_SG_KDOORBELL,
  657. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  658. }
  659. #else
  660. wmb(); /* write descriptors before telling HW */
  661. t3_write_reg(adap, A_SG_KDOORBELL,
  662. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  663. #endif
  664. }
  665. static inline void wr_gen2(struct tx_desc *d, unsigned int gen)
  666. {
  667. #if SGE_NUM_GENBITS == 2
  668. d->flit[TX_DESC_FLITS - 1] = cpu_to_be64(gen);
  669. #endif
  670. }
  671. /**
  672. * write_wr_hdr_sgl - write a WR header and, optionally, SGL
  673. * @ndesc: number of Tx descriptors spanned by the SGL
  674. * @skb: the packet corresponding to the WR
  675. * @d: first Tx descriptor to be written
  676. * @pidx: index of above descriptors
  677. * @q: the SGE Tx queue
  678. * @sgl: the SGL
  679. * @flits: number of flits to the start of the SGL in the first descriptor
  680. * @sgl_flits: the SGL size in flits
  681. * @gen: the Tx descriptor generation
  682. * @wr_hi: top 32 bits of WR header based on WR type (big endian)
  683. * @wr_lo: low 32 bits of WR header based on WR type (big endian)
  684. *
  685. * Write a work request header and an associated SGL. If the SGL is
  686. * small enough to fit into one Tx descriptor it has already been written
  687. * and we just need to write the WR header. Otherwise we distribute the
  688. * SGL across the number of descriptors it spans.
  689. */
  690. static void write_wr_hdr_sgl(unsigned int ndesc, struct sk_buff *skb,
  691. struct tx_desc *d, unsigned int pidx,
  692. const struct sge_txq *q,
  693. const struct sg_ent *sgl,
  694. unsigned int flits, unsigned int sgl_flits,
  695. unsigned int gen, unsigned int wr_hi,
  696. unsigned int wr_lo)
  697. {
  698. struct work_request_hdr *wrp = (struct work_request_hdr *)d;
  699. struct tx_sw_desc *sd = &q->sdesc[pidx];
  700. sd->skb = skb;
  701. if (need_skb_unmap()) {
  702. struct unmap_info *ui = (struct unmap_info *)skb->cb;
  703. ui->fragidx = 0;
  704. ui->addr_idx = 0;
  705. ui->sflit = flits;
  706. }
  707. if (likely(ndesc == 1)) {
  708. skb->priority = pidx;
  709. wrp->wr_hi = htonl(F_WR_SOP | F_WR_EOP | V_WR_DATATYPE(1) |
  710. V_WR_SGLSFLT(flits)) | wr_hi;
  711. wmb();
  712. wrp->wr_lo = htonl(V_WR_LEN(flits + sgl_flits) |
  713. V_WR_GEN(gen)) | wr_lo;
  714. wr_gen2(d, gen);
  715. } else {
  716. unsigned int ogen = gen;
  717. const u64 *fp = (const u64 *)sgl;
  718. struct work_request_hdr *wp = wrp;
  719. wrp->wr_hi = htonl(F_WR_SOP | V_WR_DATATYPE(1) |
  720. V_WR_SGLSFLT(flits)) | wr_hi;
  721. while (sgl_flits) {
  722. unsigned int avail = WR_FLITS - flits;
  723. if (avail > sgl_flits)
  724. avail = sgl_flits;
  725. memcpy(&d->flit[flits], fp, avail * sizeof(*fp));
  726. sgl_flits -= avail;
  727. ndesc--;
  728. if (!sgl_flits)
  729. break;
  730. fp += avail;
  731. d++;
  732. sd++;
  733. if (++pidx == q->size) {
  734. pidx = 0;
  735. gen ^= 1;
  736. d = q->desc;
  737. sd = q->sdesc;
  738. }
  739. sd->skb = skb;
  740. wrp = (struct work_request_hdr *)d;
  741. wrp->wr_hi = htonl(V_WR_DATATYPE(1) |
  742. V_WR_SGLSFLT(1)) | wr_hi;
  743. wrp->wr_lo = htonl(V_WR_LEN(min(WR_FLITS,
  744. sgl_flits + 1)) |
  745. V_WR_GEN(gen)) | wr_lo;
  746. wr_gen2(d, gen);
  747. flits = 1;
  748. }
  749. skb->priority = pidx;
  750. wrp->wr_hi |= htonl(F_WR_EOP);
  751. wmb();
  752. wp->wr_lo = htonl(V_WR_LEN(WR_FLITS) | V_WR_GEN(ogen)) | wr_lo;
  753. wr_gen2((struct tx_desc *)wp, ogen);
  754. WARN_ON(ndesc != 0);
  755. }
  756. }
  757. /**
  758. * write_tx_pkt_wr - write a TX_PKT work request
  759. * @adap: the adapter
  760. * @skb: the packet to send
  761. * @pi: the egress interface
  762. * @pidx: index of the first Tx descriptor to write
  763. * @gen: the generation value to use
  764. * @q: the Tx queue
  765. * @ndesc: number of descriptors the packet will occupy
  766. * @compl: the value of the COMPL bit to use
  767. *
  768. * Generate a TX_PKT work request to send the supplied packet.
  769. */
  770. static void write_tx_pkt_wr(struct adapter *adap, struct sk_buff *skb,
  771. const struct port_info *pi,
  772. unsigned int pidx, unsigned int gen,
  773. struct sge_txq *q, unsigned int ndesc,
  774. unsigned int compl)
  775. {
  776. unsigned int flits, sgl_flits, cntrl, tso_info;
  777. struct sg_ent *sgp, sgl[MAX_SKB_FRAGS / 2 + 1];
  778. struct tx_desc *d = &q->desc[pidx];
  779. struct cpl_tx_pkt *cpl = (struct cpl_tx_pkt *)d;
  780. cpl->len = htonl(skb->len | 0x80000000);
  781. cntrl = V_TXPKT_INTF(pi->port_id);
  782. if (vlan_tx_tag_present(skb) && pi->vlan_grp)
  783. cntrl |= F_TXPKT_VLAN_VLD | V_TXPKT_VLAN(vlan_tx_tag_get(skb));
  784. tso_info = V_LSO_MSS(skb_shinfo(skb)->gso_size);
  785. if (tso_info) {
  786. int eth_type;
  787. struct cpl_tx_pkt_lso *hdr = (struct cpl_tx_pkt_lso *)cpl;
  788. d->flit[2] = 0;
  789. cntrl |= V_TXPKT_OPCODE(CPL_TX_PKT_LSO);
  790. hdr->cntrl = htonl(cntrl);
  791. eth_type = skb->nh.raw - skb->data == ETH_HLEN ?
  792. CPL_ETH_II : CPL_ETH_II_VLAN;
  793. tso_info |= V_LSO_ETH_TYPE(eth_type) |
  794. V_LSO_IPHDR_WORDS(skb->nh.iph->ihl) |
  795. V_LSO_TCPHDR_WORDS(skb->h.th->doff);
  796. hdr->lso_info = htonl(tso_info);
  797. flits = 3;
  798. } else {
  799. cntrl |= V_TXPKT_OPCODE(CPL_TX_PKT);
  800. cntrl |= F_TXPKT_IPCSUM_DIS; /* SW calculates IP csum */
  801. cntrl |= V_TXPKT_L4CSUM_DIS(skb->ip_summed != CHECKSUM_PARTIAL);
  802. cpl->cntrl = htonl(cntrl);
  803. if (skb->len <= WR_LEN - sizeof(*cpl)) {
  804. q->sdesc[pidx].skb = NULL;
  805. if (!skb->data_len)
  806. memcpy(&d->flit[2], skb->data, skb->len);
  807. else
  808. skb_copy_bits(skb, 0, &d->flit[2], skb->len);
  809. flits = (skb->len + 7) / 8 + 2;
  810. cpl->wr.wr_hi = htonl(V_WR_BCNTLFLT(skb->len & 7) |
  811. V_WR_OP(FW_WROPCODE_TUNNEL_TX_PKT)
  812. | F_WR_SOP | F_WR_EOP | compl);
  813. wmb();
  814. cpl->wr.wr_lo = htonl(V_WR_LEN(flits) | V_WR_GEN(gen) |
  815. V_WR_TID(q->token));
  816. wr_gen2(d, gen);
  817. kfree_skb(skb);
  818. return;
  819. }
  820. flits = 2;
  821. }
  822. sgp = ndesc == 1 ? (struct sg_ent *)&d->flit[flits] : sgl;
  823. sgl_flits = make_sgl(skb, sgp, skb->data, skb_headlen(skb), adap->pdev);
  824. if (need_skb_unmap())
  825. ((struct unmap_info *)skb->cb)->len = skb_headlen(skb);
  826. write_wr_hdr_sgl(ndesc, skb, d, pidx, q, sgl, flits, sgl_flits, gen,
  827. htonl(V_WR_OP(FW_WROPCODE_TUNNEL_TX_PKT) | compl),
  828. htonl(V_WR_TID(q->token)));
  829. }
  830. /**
  831. * eth_xmit - add a packet to the Ethernet Tx queue
  832. * @skb: the packet
  833. * @dev: the egress net device
  834. *
  835. * Add a packet to an SGE Tx queue. Runs with softirqs disabled.
  836. */
  837. int t3_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  838. {
  839. unsigned int ndesc, pidx, credits, gen, compl;
  840. const struct port_info *pi = netdev_priv(dev);
  841. struct adapter *adap = dev->priv;
  842. struct sge_qset *qs = dev2qset(dev);
  843. struct sge_txq *q = &qs->txq[TXQ_ETH];
  844. /*
  845. * The chip min packet length is 9 octets but play safe and reject
  846. * anything shorter than an Ethernet header.
  847. */
  848. if (unlikely(skb->len < ETH_HLEN)) {
  849. dev_kfree_skb(skb);
  850. return NETDEV_TX_OK;
  851. }
  852. spin_lock(&q->lock);
  853. reclaim_completed_tx(adap, q);
  854. credits = q->size - q->in_use;
  855. ndesc = calc_tx_descs(skb);
  856. if (unlikely(credits < ndesc)) {
  857. if (!netif_queue_stopped(dev)) {
  858. netif_stop_queue(dev);
  859. set_bit(TXQ_ETH, &qs->txq_stopped);
  860. q->stops++;
  861. dev_err(&adap->pdev->dev,
  862. "%s: Tx ring %u full while queue awake!\n",
  863. dev->name, q->cntxt_id & 7);
  864. }
  865. spin_unlock(&q->lock);
  866. return NETDEV_TX_BUSY;
  867. }
  868. q->in_use += ndesc;
  869. if (unlikely(credits - ndesc < q->stop_thres)) {
  870. q->stops++;
  871. netif_stop_queue(dev);
  872. set_bit(TXQ_ETH, &qs->txq_stopped);
  873. #if !USE_GTS
  874. if (should_restart_tx(q) &&
  875. test_and_clear_bit(TXQ_ETH, &qs->txq_stopped)) {
  876. q->restarts++;
  877. netif_wake_queue(dev);
  878. }
  879. #endif
  880. }
  881. gen = q->gen;
  882. q->unacked += ndesc;
  883. compl = (q->unacked & 8) << (S_WR_COMPL - 3);
  884. q->unacked &= 7;
  885. pidx = q->pidx;
  886. q->pidx += ndesc;
  887. if (q->pidx >= q->size) {
  888. q->pidx -= q->size;
  889. q->gen ^= 1;
  890. }
  891. /* update port statistics */
  892. if (skb->ip_summed == CHECKSUM_COMPLETE)
  893. qs->port_stats[SGE_PSTAT_TX_CSUM]++;
  894. if (skb_shinfo(skb)->gso_size)
  895. qs->port_stats[SGE_PSTAT_TSO]++;
  896. if (vlan_tx_tag_present(skb) && pi->vlan_grp)
  897. qs->port_stats[SGE_PSTAT_VLANINS]++;
  898. dev->trans_start = jiffies;
  899. spin_unlock(&q->lock);
  900. /*
  901. * We do not use Tx completion interrupts to free DMAd Tx packets.
  902. * This is good for performamce but means that we rely on new Tx
  903. * packets arriving to run the destructors of completed packets,
  904. * which open up space in their sockets' send queues. Sometimes
  905. * we do not get such new packets causing Tx to stall. A single
  906. * UDP transmitter is a good example of this situation. We have
  907. * a clean up timer that periodically reclaims completed packets
  908. * but it doesn't run often enough (nor do we want it to) to prevent
  909. * lengthy stalls. A solution to this problem is to run the
  910. * destructor early, after the packet is queued but before it's DMAd.
  911. * A cons is that we lie to socket memory accounting, but the amount
  912. * of extra memory is reasonable (limited by the number of Tx
  913. * descriptors), the packets do actually get freed quickly by new
  914. * packets almost always, and for protocols like TCP that wait for
  915. * acks to really free up the data the extra memory is even less.
  916. * On the positive side we run the destructors on the sending CPU
  917. * rather than on a potentially different completing CPU, usually a
  918. * good thing. We also run them without holding our Tx queue lock,
  919. * unlike what reclaim_completed_tx() would otherwise do.
  920. *
  921. * Run the destructor before telling the DMA engine about the packet
  922. * to make sure it doesn't complete and get freed prematurely.
  923. */
  924. if (likely(!skb_shared(skb)))
  925. skb_orphan(skb);
  926. write_tx_pkt_wr(adap, skb, pi, pidx, gen, q, ndesc, compl);
  927. check_ring_tx_db(adap, q);
  928. return NETDEV_TX_OK;
  929. }
  930. /**
  931. * write_imm - write a packet into a Tx descriptor as immediate data
  932. * @d: the Tx descriptor to write
  933. * @skb: the packet
  934. * @len: the length of packet data to write as immediate data
  935. * @gen: the generation bit value to write
  936. *
  937. * Writes a packet as immediate data into a Tx descriptor. The packet
  938. * contains a work request at its beginning. We must write the packet
  939. * carefully so the SGE doesn't read accidentally before it's written in
  940. * its entirety.
  941. */
  942. static inline void write_imm(struct tx_desc *d, struct sk_buff *skb,
  943. unsigned int len, unsigned int gen)
  944. {
  945. struct work_request_hdr *from = (struct work_request_hdr *)skb->data;
  946. struct work_request_hdr *to = (struct work_request_hdr *)d;
  947. memcpy(&to[1], &from[1], len - sizeof(*from));
  948. to->wr_hi = from->wr_hi | htonl(F_WR_SOP | F_WR_EOP |
  949. V_WR_BCNTLFLT(len & 7));
  950. wmb();
  951. to->wr_lo = from->wr_lo | htonl(V_WR_GEN(gen) |
  952. V_WR_LEN((len + 7) / 8));
  953. wr_gen2(d, gen);
  954. kfree_skb(skb);
  955. }
  956. /**
  957. * check_desc_avail - check descriptor availability on a send queue
  958. * @adap: the adapter
  959. * @q: the send queue
  960. * @skb: the packet needing the descriptors
  961. * @ndesc: the number of Tx descriptors needed
  962. * @qid: the Tx queue number in its queue set (TXQ_OFLD or TXQ_CTRL)
  963. *
  964. * Checks if the requested number of Tx descriptors is available on an
  965. * SGE send queue. If the queue is already suspended or not enough
  966. * descriptors are available the packet is queued for later transmission.
  967. * Must be called with the Tx queue locked.
  968. *
  969. * Returns 0 if enough descriptors are available, 1 if there aren't
  970. * enough descriptors and the packet has been queued, and 2 if the caller
  971. * needs to retry because there weren't enough descriptors at the
  972. * beginning of the call but some freed up in the mean time.
  973. */
  974. static inline int check_desc_avail(struct adapter *adap, struct sge_txq *q,
  975. struct sk_buff *skb, unsigned int ndesc,
  976. unsigned int qid)
  977. {
  978. if (unlikely(!skb_queue_empty(&q->sendq))) {
  979. addq_exit:__skb_queue_tail(&q->sendq, skb);
  980. return 1;
  981. }
  982. if (unlikely(q->size - q->in_use < ndesc)) {
  983. struct sge_qset *qs = txq_to_qset(q, qid);
  984. set_bit(qid, &qs->txq_stopped);
  985. smp_mb__after_clear_bit();
  986. if (should_restart_tx(q) &&
  987. test_and_clear_bit(qid, &qs->txq_stopped))
  988. return 2;
  989. q->stops++;
  990. goto addq_exit;
  991. }
  992. return 0;
  993. }
  994. /**
  995. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  996. * @q: the SGE control Tx queue
  997. *
  998. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  999. * that send only immediate data (presently just the control queues) and
  1000. * thus do not have any sk_buffs to release.
  1001. */
  1002. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  1003. {
  1004. unsigned int reclaim = q->processed - q->cleaned;
  1005. q->in_use -= reclaim;
  1006. q->cleaned += reclaim;
  1007. }
  1008. static inline int immediate(const struct sk_buff *skb)
  1009. {
  1010. return skb->len <= WR_LEN && !skb->data_len;
  1011. }
  1012. /**
  1013. * ctrl_xmit - send a packet through an SGE control Tx queue
  1014. * @adap: the adapter
  1015. * @q: the control queue
  1016. * @skb: the packet
  1017. *
  1018. * Send a packet through an SGE control Tx queue. Packets sent through
  1019. * a control queue must fit entirely as immediate data in a single Tx
  1020. * descriptor and have no page fragments.
  1021. */
  1022. static int ctrl_xmit(struct adapter *adap, struct sge_txq *q,
  1023. struct sk_buff *skb)
  1024. {
  1025. int ret;
  1026. struct work_request_hdr *wrp = (struct work_request_hdr *)skb->data;
  1027. if (unlikely(!immediate(skb))) {
  1028. WARN_ON(1);
  1029. dev_kfree_skb(skb);
  1030. return NET_XMIT_SUCCESS;
  1031. }
  1032. wrp->wr_hi |= htonl(F_WR_SOP | F_WR_EOP);
  1033. wrp->wr_lo = htonl(V_WR_TID(q->token));
  1034. spin_lock(&q->lock);
  1035. again:reclaim_completed_tx_imm(q);
  1036. ret = check_desc_avail(adap, q, skb, 1, TXQ_CTRL);
  1037. if (unlikely(ret)) {
  1038. if (ret == 1) {
  1039. spin_unlock(&q->lock);
  1040. return NET_XMIT_CN;
  1041. }
  1042. goto again;
  1043. }
  1044. write_imm(&q->desc[q->pidx], skb, skb->len, q->gen);
  1045. q->in_use++;
  1046. if (++q->pidx >= q->size) {
  1047. q->pidx = 0;
  1048. q->gen ^= 1;
  1049. }
  1050. spin_unlock(&q->lock);
  1051. wmb();
  1052. t3_write_reg(adap, A_SG_KDOORBELL,
  1053. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  1054. return NET_XMIT_SUCCESS;
  1055. }
  1056. /**
  1057. * restart_ctrlq - restart a suspended control queue
  1058. * @qs: the queue set cotaining the control queue
  1059. *
  1060. * Resumes transmission on a suspended Tx control queue.
  1061. */
  1062. static void restart_ctrlq(unsigned long data)
  1063. {
  1064. struct sk_buff *skb;
  1065. struct sge_qset *qs = (struct sge_qset *)data;
  1066. struct sge_txq *q = &qs->txq[TXQ_CTRL];
  1067. struct adapter *adap = qs->netdev->priv;
  1068. spin_lock(&q->lock);
  1069. again:reclaim_completed_tx_imm(q);
  1070. while (q->in_use < q->size && (skb = __skb_dequeue(&q->sendq)) != NULL) {
  1071. write_imm(&q->desc[q->pidx], skb, skb->len, q->gen);
  1072. if (++q->pidx >= q->size) {
  1073. q->pidx = 0;
  1074. q->gen ^= 1;
  1075. }
  1076. q->in_use++;
  1077. }
  1078. if (!skb_queue_empty(&q->sendq)) {
  1079. set_bit(TXQ_CTRL, &qs->txq_stopped);
  1080. smp_mb__after_clear_bit();
  1081. if (should_restart_tx(q) &&
  1082. test_and_clear_bit(TXQ_CTRL, &qs->txq_stopped))
  1083. goto again;
  1084. q->stops++;
  1085. }
  1086. spin_unlock(&q->lock);
  1087. t3_write_reg(adap, A_SG_KDOORBELL,
  1088. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  1089. }
  1090. /*
  1091. * Send a management message through control queue 0
  1092. */
  1093. int t3_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1094. {
  1095. return ctrl_xmit(adap, &adap->sge.qs[0].txq[TXQ_CTRL], skb);
  1096. }
  1097. /**
  1098. * write_ofld_wr - write an offload work request
  1099. * @adap: the adapter
  1100. * @skb: the packet to send
  1101. * @q: the Tx queue
  1102. * @pidx: index of the first Tx descriptor to write
  1103. * @gen: the generation value to use
  1104. * @ndesc: number of descriptors the packet will occupy
  1105. *
  1106. * Write an offload work request to send the supplied packet. The packet
  1107. * data already carry the work request with most fields populated.
  1108. */
  1109. static void write_ofld_wr(struct adapter *adap, struct sk_buff *skb,
  1110. struct sge_txq *q, unsigned int pidx,
  1111. unsigned int gen, unsigned int ndesc)
  1112. {
  1113. unsigned int sgl_flits, flits;
  1114. struct work_request_hdr *from;
  1115. struct sg_ent *sgp, sgl[MAX_SKB_FRAGS / 2 + 1];
  1116. struct tx_desc *d = &q->desc[pidx];
  1117. if (immediate(skb)) {
  1118. q->sdesc[pidx].skb = NULL;
  1119. write_imm(d, skb, skb->len, gen);
  1120. return;
  1121. }
  1122. /* Only TX_DATA builds SGLs */
  1123. from = (struct work_request_hdr *)skb->data;
  1124. memcpy(&d->flit[1], &from[1], skb->h.raw - skb->data - sizeof(*from));
  1125. flits = (skb->h.raw - skb->data) / 8;
  1126. sgp = ndesc == 1 ? (struct sg_ent *)&d->flit[flits] : sgl;
  1127. sgl_flits = make_sgl(skb, sgp, skb->h.raw, skb->tail - skb->h.raw,
  1128. adap->pdev);
  1129. if (need_skb_unmap())
  1130. ((struct unmap_info *)skb->cb)->len = skb->tail - skb->h.raw;
  1131. write_wr_hdr_sgl(ndesc, skb, d, pidx, q, sgl, flits, sgl_flits,
  1132. gen, from->wr_hi, from->wr_lo);
  1133. }
  1134. /**
  1135. * calc_tx_descs_ofld - calculate # of Tx descriptors for an offload packet
  1136. * @skb: the packet
  1137. *
  1138. * Returns the number of Tx descriptors needed for the given offload
  1139. * packet. These packets are already fully constructed.
  1140. */
  1141. static inline unsigned int calc_tx_descs_ofld(const struct sk_buff *skb)
  1142. {
  1143. unsigned int flits, cnt = skb_shinfo(skb)->nr_frags;
  1144. if (skb->len <= WR_LEN && cnt == 0)
  1145. return 1; /* packet fits as immediate data */
  1146. flits = (skb->h.raw - skb->data) / 8; /* headers */
  1147. if (skb->tail != skb->h.raw)
  1148. cnt++;
  1149. return flits_to_desc(flits + sgl_len(cnt));
  1150. }
  1151. /**
  1152. * ofld_xmit - send a packet through an offload queue
  1153. * @adap: the adapter
  1154. * @q: the Tx offload queue
  1155. * @skb: the packet
  1156. *
  1157. * Send an offload packet through an SGE offload queue.
  1158. */
  1159. static int ofld_xmit(struct adapter *adap, struct sge_txq *q,
  1160. struct sk_buff *skb)
  1161. {
  1162. int ret;
  1163. unsigned int ndesc = calc_tx_descs_ofld(skb), pidx, gen;
  1164. spin_lock(&q->lock);
  1165. again:reclaim_completed_tx(adap, q);
  1166. ret = check_desc_avail(adap, q, skb, ndesc, TXQ_OFLD);
  1167. if (unlikely(ret)) {
  1168. if (ret == 1) {
  1169. skb->priority = ndesc; /* save for restart */
  1170. spin_unlock(&q->lock);
  1171. return NET_XMIT_CN;
  1172. }
  1173. goto again;
  1174. }
  1175. gen = q->gen;
  1176. q->in_use += ndesc;
  1177. pidx = q->pidx;
  1178. q->pidx += ndesc;
  1179. if (q->pidx >= q->size) {
  1180. q->pidx -= q->size;
  1181. q->gen ^= 1;
  1182. }
  1183. spin_unlock(&q->lock);
  1184. write_ofld_wr(adap, skb, q, pidx, gen, ndesc);
  1185. check_ring_tx_db(adap, q);
  1186. return NET_XMIT_SUCCESS;
  1187. }
  1188. /**
  1189. * restart_offloadq - restart a suspended offload queue
  1190. * @qs: the queue set cotaining the offload queue
  1191. *
  1192. * Resumes transmission on a suspended Tx offload queue.
  1193. */
  1194. static void restart_offloadq(unsigned long data)
  1195. {
  1196. struct sk_buff *skb;
  1197. struct sge_qset *qs = (struct sge_qset *)data;
  1198. struct sge_txq *q = &qs->txq[TXQ_OFLD];
  1199. struct adapter *adap = qs->netdev->priv;
  1200. spin_lock(&q->lock);
  1201. again:reclaim_completed_tx(adap, q);
  1202. while ((skb = skb_peek(&q->sendq)) != NULL) {
  1203. unsigned int gen, pidx;
  1204. unsigned int ndesc = skb->priority;
  1205. if (unlikely(q->size - q->in_use < ndesc)) {
  1206. set_bit(TXQ_OFLD, &qs->txq_stopped);
  1207. smp_mb__after_clear_bit();
  1208. if (should_restart_tx(q) &&
  1209. test_and_clear_bit(TXQ_OFLD, &qs->txq_stopped))
  1210. goto again;
  1211. q->stops++;
  1212. break;
  1213. }
  1214. gen = q->gen;
  1215. q->in_use += ndesc;
  1216. pidx = q->pidx;
  1217. q->pidx += ndesc;
  1218. if (q->pidx >= q->size) {
  1219. q->pidx -= q->size;
  1220. q->gen ^= 1;
  1221. }
  1222. __skb_unlink(skb, &q->sendq);
  1223. spin_unlock(&q->lock);
  1224. write_ofld_wr(adap, skb, q, pidx, gen, ndesc);
  1225. spin_lock(&q->lock);
  1226. }
  1227. spin_unlock(&q->lock);
  1228. #if USE_GTS
  1229. set_bit(TXQ_RUNNING, &q->flags);
  1230. set_bit(TXQ_LAST_PKT_DB, &q->flags);
  1231. #endif
  1232. t3_write_reg(adap, A_SG_KDOORBELL,
  1233. F_SELEGRCNTX | V_EGRCNTX(q->cntxt_id));
  1234. }
  1235. /**
  1236. * queue_set - return the queue set a packet should use
  1237. * @skb: the packet
  1238. *
  1239. * Maps a packet to the SGE queue set it should use. The desired queue
  1240. * set is carried in bits 1-3 in the packet's priority.
  1241. */
  1242. static inline int queue_set(const struct sk_buff *skb)
  1243. {
  1244. return skb->priority >> 1;
  1245. }
  1246. /**
  1247. * is_ctrl_pkt - return whether an offload packet is a control packet
  1248. * @skb: the packet
  1249. *
  1250. * Determines whether an offload packet should use an OFLD or a CTRL
  1251. * Tx queue. This is indicated by bit 0 in the packet's priority.
  1252. */
  1253. static inline int is_ctrl_pkt(const struct sk_buff *skb)
  1254. {
  1255. return skb->priority & 1;
  1256. }
  1257. /**
  1258. * t3_offload_tx - send an offload packet
  1259. * @tdev: the offload device to send to
  1260. * @skb: the packet
  1261. *
  1262. * Sends an offload packet. We use the packet priority to select the
  1263. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1264. * should be sent as regular or control, bits 1-3 select the queue set.
  1265. */
  1266. int t3_offload_tx(struct t3cdev *tdev, struct sk_buff *skb)
  1267. {
  1268. struct adapter *adap = tdev2adap(tdev);
  1269. struct sge_qset *qs = &adap->sge.qs[queue_set(skb)];
  1270. if (unlikely(is_ctrl_pkt(skb)))
  1271. return ctrl_xmit(adap, &qs->txq[TXQ_CTRL], skb);
  1272. return ofld_xmit(adap, &qs->txq[TXQ_OFLD], skb);
  1273. }
  1274. /**
  1275. * offload_enqueue - add an offload packet to an SGE offload receive queue
  1276. * @q: the SGE response queue
  1277. * @skb: the packet
  1278. *
  1279. * Add a new offload packet to an SGE response queue's offload packet
  1280. * queue. If the packet is the first on the queue it schedules the RX
  1281. * softirq to process the queue.
  1282. */
  1283. static inline void offload_enqueue(struct sge_rspq *q, struct sk_buff *skb)
  1284. {
  1285. skb->next = skb->prev = NULL;
  1286. if (q->rx_tail)
  1287. q->rx_tail->next = skb;
  1288. else {
  1289. struct sge_qset *qs = rspq_to_qset(q);
  1290. if (__netif_rx_schedule_prep(qs->netdev))
  1291. __netif_rx_schedule(qs->netdev);
  1292. q->rx_head = skb;
  1293. }
  1294. q->rx_tail = skb;
  1295. }
  1296. /**
  1297. * deliver_partial_bundle - deliver a (partial) bundle of Rx offload pkts
  1298. * @tdev: the offload device that will be receiving the packets
  1299. * @q: the SGE response queue that assembled the bundle
  1300. * @skbs: the partial bundle
  1301. * @n: the number of packets in the bundle
  1302. *
  1303. * Delivers a (partial) bundle of Rx offload packets to an offload device.
  1304. */
  1305. static inline void deliver_partial_bundle(struct t3cdev *tdev,
  1306. struct sge_rspq *q,
  1307. struct sk_buff *skbs[], int n)
  1308. {
  1309. if (n) {
  1310. q->offload_bundles++;
  1311. tdev->recv(tdev, skbs, n);
  1312. }
  1313. }
  1314. /**
  1315. * ofld_poll - NAPI handler for offload packets in interrupt mode
  1316. * @dev: the network device doing the polling
  1317. * @budget: polling budget
  1318. *
  1319. * The NAPI handler for offload packets when a response queue is serviced
  1320. * by the hard interrupt handler, i.e., when it's operating in non-polling
  1321. * mode. Creates small packet batches and sends them through the offload
  1322. * receive handler. Batches need to be of modest size as we do prefetches
  1323. * on the packets in each.
  1324. */
  1325. static int ofld_poll(struct net_device *dev, int *budget)
  1326. {
  1327. struct adapter *adapter = dev->priv;
  1328. struct sge_qset *qs = dev2qset(dev);
  1329. struct sge_rspq *q = &qs->rspq;
  1330. int work_done, limit = min(*budget, dev->quota), avail = limit;
  1331. while (avail) {
  1332. struct sk_buff *head, *tail, *skbs[RX_BUNDLE_SIZE];
  1333. int ngathered;
  1334. spin_lock_irq(&q->lock);
  1335. head = q->rx_head;
  1336. if (!head) {
  1337. work_done = limit - avail;
  1338. *budget -= work_done;
  1339. dev->quota -= work_done;
  1340. __netif_rx_complete(dev);
  1341. spin_unlock_irq(&q->lock);
  1342. return 0;
  1343. }
  1344. tail = q->rx_tail;
  1345. q->rx_head = q->rx_tail = NULL;
  1346. spin_unlock_irq(&q->lock);
  1347. for (ngathered = 0; avail && head; avail--) {
  1348. prefetch(head->data);
  1349. skbs[ngathered] = head;
  1350. head = head->next;
  1351. skbs[ngathered]->next = NULL;
  1352. if (++ngathered == RX_BUNDLE_SIZE) {
  1353. q->offload_bundles++;
  1354. adapter->tdev.recv(&adapter->tdev, skbs,
  1355. ngathered);
  1356. ngathered = 0;
  1357. }
  1358. }
  1359. if (head) { /* splice remaining packets back onto Rx queue */
  1360. spin_lock_irq(&q->lock);
  1361. tail->next = q->rx_head;
  1362. if (!q->rx_head)
  1363. q->rx_tail = tail;
  1364. q->rx_head = head;
  1365. spin_unlock_irq(&q->lock);
  1366. }
  1367. deliver_partial_bundle(&adapter->tdev, q, skbs, ngathered);
  1368. }
  1369. work_done = limit - avail;
  1370. *budget -= work_done;
  1371. dev->quota -= work_done;
  1372. return 1;
  1373. }
  1374. /**
  1375. * rx_offload - process a received offload packet
  1376. * @tdev: the offload device receiving the packet
  1377. * @rq: the response queue that received the packet
  1378. * @skb: the packet
  1379. * @rx_gather: a gather list of packets if we are building a bundle
  1380. * @gather_idx: index of the next available slot in the bundle
  1381. *
  1382. * Process an ingress offload pakcet and add it to the offload ingress
  1383. * queue. Returns the index of the next available slot in the bundle.
  1384. */
  1385. static inline int rx_offload(struct t3cdev *tdev, struct sge_rspq *rq,
  1386. struct sk_buff *skb, struct sk_buff *rx_gather[],
  1387. unsigned int gather_idx)
  1388. {
  1389. rq->offload_pkts++;
  1390. skb->mac.raw = skb->nh.raw = skb->h.raw = skb->data;
  1391. if (rq->polling) {
  1392. rx_gather[gather_idx++] = skb;
  1393. if (gather_idx == RX_BUNDLE_SIZE) {
  1394. tdev->recv(tdev, rx_gather, RX_BUNDLE_SIZE);
  1395. gather_idx = 0;
  1396. rq->offload_bundles++;
  1397. }
  1398. } else
  1399. offload_enqueue(rq, skb);
  1400. return gather_idx;
  1401. }
  1402. /**
  1403. * restart_tx - check whether to restart suspended Tx queues
  1404. * @qs: the queue set to resume
  1405. *
  1406. * Restarts suspended Tx queues of an SGE queue set if they have enough
  1407. * free resources to resume operation.
  1408. */
  1409. static void restart_tx(struct sge_qset *qs)
  1410. {
  1411. if (test_bit(TXQ_ETH, &qs->txq_stopped) &&
  1412. should_restart_tx(&qs->txq[TXQ_ETH]) &&
  1413. test_and_clear_bit(TXQ_ETH, &qs->txq_stopped)) {
  1414. qs->txq[TXQ_ETH].restarts++;
  1415. if (netif_running(qs->netdev))
  1416. netif_wake_queue(qs->netdev);
  1417. }
  1418. if (test_bit(TXQ_OFLD, &qs->txq_stopped) &&
  1419. should_restart_tx(&qs->txq[TXQ_OFLD]) &&
  1420. test_and_clear_bit(TXQ_OFLD, &qs->txq_stopped)) {
  1421. qs->txq[TXQ_OFLD].restarts++;
  1422. tasklet_schedule(&qs->txq[TXQ_OFLD].qresume_tsk);
  1423. }
  1424. if (test_bit(TXQ_CTRL, &qs->txq_stopped) &&
  1425. should_restart_tx(&qs->txq[TXQ_CTRL]) &&
  1426. test_and_clear_bit(TXQ_CTRL, &qs->txq_stopped)) {
  1427. qs->txq[TXQ_CTRL].restarts++;
  1428. tasklet_schedule(&qs->txq[TXQ_CTRL].qresume_tsk);
  1429. }
  1430. }
  1431. /**
  1432. * rx_eth - process an ingress ethernet packet
  1433. * @adap: the adapter
  1434. * @rq: the response queue that received the packet
  1435. * @skb: the packet
  1436. * @pad: amount of padding at the start of the buffer
  1437. *
  1438. * Process an ingress ethernet pakcet and deliver it to the stack.
  1439. * The padding is 2 if the packet was delivered in an Rx buffer and 0
  1440. * if it was immediate data in a response.
  1441. */
  1442. static void rx_eth(struct adapter *adap, struct sge_rspq *rq,
  1443. struct sk_buff *skb, int pad)
  1444. {
  1445. struct cpl_rx_pkt *p = (struct cpl_rx_pkt *)(skb->data + pad);
  1446. struct port_info *pi;
  1447. rq->eth_pkts++;
  1448. skb_pull(skb, sizeof(*p) + pad);
  1449. skb->dev = adap->port[p->iff];
  1450. skb->dev->last_rx = jiffies;
  1451. skb->protocol = eth_type_trans(skb, skb->dev);
  1452. pi = netdev_priv(skb->dev);
  1453. if (pi->rx_csum_offload && p->csum_valid && p->csum == 0xffff &&
  1454. !p->fragment) {
  1455. rspq_to_qset(rq)->port_stats[SGE_PSTAT_RX_CSUM_GOOD]++;
  1456. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1457. } else
  1458. skb->ip_summed = CHECKSUM_NONE;
  1459. if (unlikely(p->vlan_valid)) {
  1460. struct vlan_group *grp = pi->vlan_grp;
  1461. rspq_to_qset(rq)->port_stats[SGE_PSTAT_VLANEX]++;
  1462. if (likely(grp))
  1463. __vlan_hwaccel_rx(skb, grp, ntohs(p->vlan),
  1464. rq->polling);
  1465. else
  1466. dev_kfree_skb_any(skb);
  1467. } else if (rq->polling)
  1468. netif_receive_skb(skb);
  1469. else
  1470. netif_rx(skb);
  1471. }
  1472. /**
  1473. * handle_rsp_cntrl_info - handles control information in a response
  1474. * @qs: the queue set corresponding to the response
  1475. * @flags: the response control flags
  1476. *
  1477. * Handles the control information of an SGE response, such as GTS
  1478. * indications and completion credits for the queue set's Tx queues.
  1479. * HW coalesces credits, we don't do any extra SW coalescing.
  1480. */
  1481. static inline void handle_rsp_cntrl_info(struct sge_qset *qs, u32 flags)
  1482. {
  1483. unsigned int credits;
  1484. #if USE_GTS
  1485. if (flags & F_RSPD_TXQ0_GTS)
  1486. clear_bit(TXQ_RUNNING, &qs->txq[TXQ_ETH].flags);
  1487. #endif
  1488. credits = G_RSPD_TXQ0_CR(flags);
  1489. if (credits)
  1490. qs->txq[TXQ_ETH].processed += credits;
  1491. credits = G_RSPD_TXQ2_CR(flags);
  1492. if (credits)
  1493. qs->txq[TXQ_CTRL].processed += credits;
  1494. # if USE_GTS
  1495. if (flags & F_RSPD_TXQ1_GTS)
  1496. clear_bit(TXQ_RUNNING, &qs->txq[TXQ_OFLD].flags);
  1497. # endif
  1498. credits = G_RSPD_TXQ1_CR(flags);
  1499. if (credits)
  1500. qs->txq[TXQ_OFLD].processed += credits;
  1501. }
  1502. /**
  1503. * check_ring_db - check if we need to ring any doorbells
  1504. * @adapter: the adapter
  1505. * @qs: the queue set whose Tx queues are to be examined
  1506. * @sleeping: indicates which Tx queue sent GTS
  1507. *
  1508. * Checks if some of a queue set's Tx queues need to ring their doorbells
  1509. * to resume transmission after idling while they still have unprocessed
  1510. * descriptors.
  1511. */
  1512. static void check_ring_db(struct adapter *adap, struct sge_qset *qs,
  1513. unsigned int sleeping)
  1514. {
  1515. if (sleeping & F_RSPD_TXQ0_GTS) {
  1516. struct sge_txq *txq = &qs->txq[TXQ_ETH];
  1517. if (txq->cleaned + txq->in_use != txq->processed &&
  1518. !test_and_set_bit(TXQ_LAST_PKT_DB, &txq->flags)) {
  1519. set_bit(TXQ_RUNNING, &txq->flags);
  1520. t3_write_reg(adap, A_SG_KDOORBELL, F_SELEGRCNTX |
  1521. V_EGRCNTX(txq->cntxt_id));
  1522. }
  1523. }
  1524. if (sleeping & F_RSPD_TXQ1_GTS) {
  1525. struct sge_txq *txq = &qs->txq[TXQ_OFLD];
  1526. if (txq->cleaned + txq->in_use != txq->processed &&
  1527. !test_and_set_bit(TXQ_LAST_PKT_DB, &txq->flags)) {
  1528. set_bit(TXQ_RUNNING, &txq->flags);
  1529. t3_write_reg(adap, A_SG_KDOORBELL, F_SELEGRCNTX |
  1530. V_EGRCNTX(txq->cntxt_id));
  1531. }
  1532. }
  1533. }
  1534. /**
  1535. * is_new_response - check if a response is newly written
  1536. * @r: the response descriptor
  1537. * @q: the response queue
  1538. *
  1539. * Returns true if a response descriptor contains a yet unprocessed
  1540. * response.
  1541. */
  1542. static inline int is_new_response(const struct rsp_desc *r,
  1543. const struct sge_rspq *q)
  1544. {
  1545. return (r->intr_gen & F_RSPD_GEN2) == q->gen;
  1546. }
  1547. #define RSPD_GTS_MASK (F_RSPD_TXQ0_GTS | F_RSPD_TXQ1_GTS)
  1548. #define RSPD_CTRL_MASK (RSPD_GTS_MASK | \
  1549. V_RSPD_TXQ0_CR(M_RSPD_TXQ0_CR) | \
  1550. V_RSPD_TXQ1_CR(M_RSPD_TXQ1_CR) | \
  1551. V_RSPD_TXQ2_CR(M_RSPD_TXQ2_CR))
  1552. /* How long to delay the next interrupt in case of memory shortage, in 0.1us. */
  1553. #define NOMEM_INTR_DELAY 2500
  1554. /**
  1555. * process_responses - process responses from an SGE response queue
  1556. * @adap: the adapter
  1557. * @qs: the queue set to which the response queue belongs
  1558. * @budget: how many responses can be processed in this round
  1559. *
  1560. * Process responses from an SGE response queue up to the supplied budget.
  1561. * Responses include received packets as well as credits and other events
  1562. * for the queues that belong to the response queue's queue set.
  1563. * A negative budget is effectively unlimited.
  1564. *
  1565. * Additionally choose the interrupt holdoff time for the next interrupt
  1566. * on this queue. If the system is under memory shortage use a fairly
  1567. * long delay to help recovery.
  1568. */
  1569. static int process_responses(struct adapter *adap, struct sge_qset *qs,
  1570. int budget)
  1571. {
  1572. struct sge_rspq *q = &qs->rspq;
  1573. struct rsp_desc *r = &q->desc[q->cidx];
  1574. int budget_left = budget;
  1575. unsigned int sleeping = 0;
  1576. struct sk_buff *offload_skbs[RX_BUNDLE_SIZE];
  1577. int ngathered = 0;
  1578. q->next_holdoff = q->holdoff_tmr;
  1579. while (likely(budget_left && is_new_response(r, q))) {
  1580. int eth, ethpad = 0;
  1581. struct sk_buff *skb = NULL;
  1582. u32 len, flags = ntohl(r->flags);
  1583. u32 rss_hi = *(const u32 *)r, rss_lo = r->rss_hdr.rss_hash_val;
  1584. eth = r->rss_hdr.opcode == CPL_RX_PKT;
  1585. if (unlikely(flags & F_RSPD_ASYNC_NOTIF)) {
  1586. skb = alloc_skb(AN_PKT_SIZE, GFP_ATOMIC);
  1587. if (!skb)
  1588. goto no_mem;
  1589. memcpy(__skb_put(skb, AN_PKT_SIZE), r, AN_PKT_SIZE);
  1590. skb->data[0] = CPL_ASYNC_NOTIF;
  1591. rss_hi = htonl(CPL_ASYNC_NOTIF << 24);
  1592. q->async_notif++;
  1593. } else if (flags & F_RSPD_IMM_DATA_VALID) {
  1594. skb = get_imm_packet(r);
  1595. if (unlikely(!skb)) {
  1596. no_mem:
  1597. q->next_holdoff = NOMEM_INTR_DELAY;
  1598. q->nomem++;
  1599. /* consume one credit since we tried */
  1600. budget_left--;
  1601. break;
  1602. }
  1603. q->imm_data++;
  1604. } else if ((len = ntohl(r->len_cq)) != 0) {
  1605. struct sge_fl *fl;
  1606. fl = (len & F_RSPD_FLQ) ? &qs->fl[1] : &qs->fl[0];
  1607. fl->credits--;
  1608. skb = get_packet(adap, fl, G_RSPD_LEN(len),
  1609. eth ? SGE_RX_DROP_THRES : 0);
  1610. if (!skb)
  1611. q->rx_drops++;
  1612. else if (r->rss_hdr.opcode == CPL_TRACE_PKT)
  1613. __skb_pull(skb, 2);
  1614. ethpad = 2;
  1615. if (++fl->cidx == fl->size)
  1616. fl->cidx = 0;
  1617. } else
  1618. q->pure_rsps++;
  1619. if (flags & RSPD_CTRL_MASK) {
  1620. sleeping |= flags & RSPD_GTS_MASK;
  1621. handle_rsp_cntrl_info(qs, flags);
  1622. }
  1623. r++;
  1624. if (unlikely(++q->cidx == q->size)) {
  1625. q->cidx = 0;
  1626. q->gen ^= 1;
  1627. r = q->desc;
  1628. }
  1629. prefetch(r);
  1630. if (++q->credits >= (q->size / 4)) {
  1631. refill_rspq(adap, q, q->credits);
  1632. q->credits = 0;
  1633. }
  1634. if (likely(skb != NULL)) {
  1635. if (eth)
  1636. rx_eth(adap, q, skb, ethpad);
  1637. else {
  1638. /* Preserve the RSS info in csum & priority */
  1639. skb->csum = rss_hi;
  1640. skb->priority = rss_lo;
  1641. ngathered = rx_offload(&adap->tdev, q, skb,
  1642. offload_skbs, ngathered);
  1643. }
  1644. }
  1645. --budget_left;
  1646. }
  1647. deliver_partial_bundle(&adap->tdev, q, offload_skbs, ngathered);
  1648. if (sleeping)
  1649. check_ring_db(adap, qs, sleeping);
  1650. smp_mb(); /* commit Tx queue .processed updates */
  1651. if (unlikely(qs->txq_stopped != 0))
  1652. restart_tx(qs);
  1653. budget -= budget_left;
  1654. return budget;
  1655. }
  1656. static inline int is_pure_response(const struct rsp_desc *r)
  1657. {
  1658. u32 n = ntohl(r->flags) & (F_RSPD_ASYNC_NOTIF | F_RSPD_IMM_DATA_VALID);
  1659. return (n | r->len_cq) == 0;
  1660. }
  1661. /**
  1662. * napi_rx_handler - the NAPI handler for Rx processing
  1663. * @dev: the net device
  1664. * @budget: how many packets we can process in this round
  1665. *
  1666. * Handler for new data events when using NAPI.
  1667. */
  1668. static int napi_rx_handler(struct net_device *dev, int *budget)
  1669. {
  1670. struct adapter *adap = dev->priv;
  1671. struct sge_qset *qs = dev2qset(dev);
  1672. int effective_budget = min(*budget, dev->quota);
  1673. int work_done = process_responses(adap, qs, effective_budget);
  1674. *budget -= work_done;
  1675. dev->quota -= work_done;
  1676. if (work_done >= effective_budget)
  1677. return 1;
  1678. netif_rx_complete(dev);
  1679. /*
  1680. * Because we don't atomically flush the following write it is
  1681. * possible that in very rare cases it can reach the device in a way
  1682. * that races with a new response being written plus an error interrupt
  1683. * causing the NAPI interrupt handler below to return unhandled status
  1684. * to the OS. To protect against this would require flushing the write
  1685. * and doing both the write and the flush with interrupts off. Way too
  1686. * expensive and unjustifiable given the rarity of the race.
  1687. *
  1688. * The race cannot happen at all with MSI-X.
  1689. */
  1690. t3_write_reg(adap, A_SG_GTS, V_RSPQ(qs->rspq.cntxt_id) |
  1691. V_NEWTIMER(qs->rspq.next_holdoff) |
  1692. V_NEWINDEX(qs->rspq.cidx));
  1693. return 0;
  1694. }
  1695. /*
  1696. * Returns true if the device is already scheduled for polling.
  1697. */
  1698. static inline int napi_is_scheduled(struct net_device *dev)
  1699. {
  1700. return test_bit(__LINK_STATE_RX_SCHED, &dev->state);
  1701. }
  1702. /**
  1703. * process_pure_responses - process pure responses from a response queue
  1704. * @adap: the adapter
  1705. * @qs: the queue set owning the response queue
  1706. * @r: the first pure response to process
  1707. *
  1708. * A simpler version of process_responses() that handles only pure (i.e.,
  1709. * non data-carrying) responses. Such respones are too light-weight to
  1710. * justify calling a softirq under NAPI, so we handle them specially in
  1711. * the interrupt handler. The function is called with a pointer to a
  1712. * response, which the caller must ensure is a valid pure response.
  1713. *
  1714. * Returns 1 if it encounters a valid data-carrying response, 0 otherwise.
  1715. */
  1716. static int process_pure_responses(struct adapter *adap, struct sge_qset *qs,
  1717. struct rsp_desc *r)
  1718. {
  1719. struct sge_rspq *q = &qs->rspq;
  1720. unsigned int sleeping = 0;
  1721. do {
  1722. u32 flags = ntohl(r->flags);
  1723. r++;
  1724. if (unlikely(++q->cidx == q->size)) {
  1725. q->cidx = 0;
  1726. q->gen ^= 1;
  1727. r = q->desc;
  1728. }
  1729. prefetch(r);
  1730. if (flags & RSPD_CTRL_MASK) {
  1731. sleeping |= flags & RSPD_GTS_MASK;
  1732. handle_rsp_cntrl_info(qs, flags);
  1733. }
  1734. q->pure_rsps++;
  1735. if (++q->credits >= (q->size / 4)) {
  1736. refill_rspq(adap, q, q->credits);
  1737. q->credits = 0;
  1738. }
  1739. } while (is_new_response(r, q) && is_pure_response(r));
  1740. if (sleeping)
  1741. check_ring_db(adap, qs, sleeping);
  1742. smp_mb(); /* commit Tx queue .processed updates */
  1743. if (unlikely(qs->txq_stopped != 0))
  1744. restart_tx(qs);
  1745. return is_new_response(r, q);
  1746. }
  1747. /**
  1748. * handle_responses - decide what to do with new responses in NAPI mode
  1749. * @adap: the adapter
  1750. * @q: the response queue
  1751. *
  1752. * This is used by the NAPI interrupt handlers to decide what to do with
  1753. * new SGE responses. If there are no new responses it returns -1. If
  1754. * there are new responses and they are pure (i.e., non-data carrying)
  1755. * it handles them straight in hard interrupt context as they are very
  1756. * cheap and don't deliver any packets. Finally, if there are any data
  1757. * signaling responses it schedules the NAPI handler. Returns 1 if it
  1758. * schedules NAPI, 0 if all new responses were pure.
  1759. *
  1760. * The caller must ascertain NAPI is not already running.
  1761. */
  1762. static inline int handle_responses(struct adapter *adap, struct sge_rspq *q)
  1763. {
  1764. struct sge_qset *qs = rspq_to_qset(q);
  1765. struct rsp_desc *r = &q->desc[q->cidx];
  1766. if (!is_new_response(r, q))
  1767. return -1;
  1768. if (is_pure_response(r) && process_pure_responses(adap, qs, r) == 0) {
  1769. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q->cntxt_id) |
  1770. V_NEWTIMER(q->holdoff_tmr) | V_NEWINDEX(q->cidx));
  1771. return 0;
  1772. }
  1773. if (likely(__netif_rx_schedule_prep(qs->netdev)))
  1774. __netif_rx_schedule(qs->netdev);
  1775. return 1;
  1776. }
  1777. /*
  1778. * The MSI-X interrupt handler for an SGE response queue for the non-NAPI case
  1779. * (i.e., response queue serviced in hard interrupt).
  1780. */
  1781. irqreturn_t t3_sge_intr_msix(int irq, void *cookie)
  1782. {
  1783. struct sge_qset *qs = cookie;
  1784. struct adapter *adap = qs->netdev->priv;
  1785. struct sge_rspq *q = &qs->rspq;
  1786. spin_lock(&q->lock);
  1787. if (process_responses(adap, qs, -1) == 0)
  1788. q->unhandled_irqs++;
  1789. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q->cntxt_id) |
  1790. V_NEWTIMER(q->next_holdoff) | V_NEWINDEX(q->cidx));
  1791. spin_unlock(&q->lock);
  1792. return IRQ_HANDLED;
  1793. }
  1794. /*
  1795. * The MSI-X interrupt handler for an SGE response queue for the NAPI case
  1796. * (i.e., response queue serviced by NAPI polling).
  1797. */
  1798. irqreturn_t t3_sge_intr_msix_napi(int irq, void *cookie)
  1799. {
  1800. struct sge_qset *qs = cookie;
  1801. struct adapter *adap = qs->netdev->priv;
  1802. struct sge_rspq *q = &qs->rspq;
  1803. spin_lock(&q->lock);
  1804. BUG_ON(napi_is_scheduled(qs->netdev));
  1805. if (handle_responses(adap, q) < 0)
  1806. q->unhandled_irqs++;
  1807. spin_unlock(&q->lock);
  1808. return IRQ_HANDLED;
  1809. }
  1810. /*
  1811. * The non-NAPI MSI interrupt handler. This needs to handle data events from
  1812. * SGE response queues as well as error and other async events as they all use
  1813. * the same MSI vector. We use one SGE response queue per port in this mode
  1814. * and protect all response queues with queue 0's lock.
  1815. */
  1816. static irqreturn_t t3_intr_msi(int irq, void *cookie)
  1817. {
  1818. int new_packets = 0;
  1819. struct adapter *adap = cookie;
  1820. struct sge_rspq *q = &adap->sge.qs[0].rspq;
  1821. spin_lock(&q->lock);
  1822. if (process_responses(adap, &adap->sge.qs[0], -1)) {
  1823. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q->cntxt_id) |
  1824. V_NEWTIMER(q->next_holdoff) | V_NEWINDEX(q->cidx));
  1825. new_packets = 1;
  1826. }
  1827. if (adap->params.nports == 2 &&
  1828. process_responses(adap, &adap->sge.qs[1], -1)) {
  1829. struct sge_rspq *q1 = &adap->sge.qs[1].rspq;
  1830. t3_write_reg(adap, A_SG_GTS, V_RSPQ(q1->cntxt_id) |
  1831. V_NEWTIMER(q1->next_holdoff) |
  1832. V_NEWINDEX(q1->cidx));
  1833. new_packets = 1;
  1834. }
  1835. if (!new_packets && t3_slow_intr_handler(adap) == 0)
  1836. q->unhandled_irqs++;
  1837. spin_unlock(&q->lock);
  1838. return IRQ_HANDLED;
  1839. }
  1840. static int rspq_check_napi(struct net_device *dev, struct sge_rspq *q)
  1841. {
  1842. if (!napi_is_scheduled(dev) && is_new_response(&q->desc[q->cidx], q)) {
  1843. if (likely(__netif_rx_schedule_prep(dev)))
  1844. __netif_rx_schedule(dev);
  1845. return 1;
  1846. }
  1847. return 0;
  1848. }
  1849. /*
  1850. * The MSI interrupt handler for the NAPI case (i.e., response queues serviced
  1851. * by NAPI polling). Handles data events from SGE response queues as well as
  1852. * error and other async events as they all use the same MSI vector. We use
  1853. * one SGE response queue per port in this mode and protect all response
  1854. * queues with queue 0's lock.
  1855. */
  1856. irqreturn_t t3_intr_msi_napi(int irq, void *cookie)
  1857. {
  1858. int new_packets;
  1859. struct adapter *adap = cookie;
  1860. struct sge_rspq *q = &adap->sge.qs[0].rspq;
  1861. spin_lock(&q->lock);
  1862. new_packets = rspq_check_napi(adap->sge.qs[0].netdev, q);
  1863. if (adap->params.nports == 2)
  1864. new_packets += rspq_check_napi(adap->sge.qs[1].netdev,
  1865. &adap->sge.qs[1].rspq);
  1866. if (!new_packets && t3_slow_intr_handler(adap) == 0)
  1867. q->unhandled_irqs++;
  1868. spin_unlock(&q->lock);
  1869. return IRQ_HANDLED;
  1870. }
  1871. /*
  1872. * A helper function that processes responses and issues GTS.
  1873. */
  1874. static inline int process_responses_gts(struct adapter *adap,
  1875. struct sge_rspq *rq)
  1876. {
  1877. int work;
  1878. work = process_responses(adap, rspq_to_qset(rq), -1);
  1879. t3_write_reg(adap, A_SG_GTS, V_RSPQ(rq->cntxt_id) |
  1880. V_NEWTIMER(rq->next_holdoff) | V_NEWINDEX(rq->cidx));
  1881. return work;
  1882. }
  1883. /*
  1884. * The legacy INTx interrupt handler. This needs to handle data events from
  1885. * SGE response queues as well as error and other async events as they all use
  1886. * the same interrupt pin. We use one SGE response queue per port in this mode
  1887. * and protect all response queues with queue 0's lock.
  1888. */
  1889. static irqreturn_t t3_intr(int irq, void *cookie)
  1890. {
  1891. int work_done, w0, w1;
  1892. struct adapter *adap = cookie;
  1893. struct sge_rspq *q0 = &adap->sge.qs[0].rspq;
  1894. struct sge_rspq *q1 = &adap->sge.qs[1].rspq;
  1895. spin_lock(&q0->lock);
  1896. w0 = is_new_response(&q0->desc[q0->cidx], q0);
  1897. w1 = adap->params.nports == 2 &&
  1898. is_new_response(&q1->desc[q1->cidx], q1);
  1899. if (likely(w0 | w1)) {
  1900. t3_write_reg(adap, A_PL_CLI, 0);
  1901. t3_read_reg(adap, A_PL_CLI); /* flush */
  1902. if (likely(w0))
  1903. process_responses_gts(adap, q0);
  1904. if (w1)
  1905. process_responses_gts(adap, q1);
  1906. work_done = w0 | w1;
  1907. } else
  1908. work_done = t3_slow_intr_handler(adap);
  1909. spin_unlock(&q0->lock);
  1910. return IRQ_RETVAL(work_done != 0);
  1911. }
  1912. /*
  1913. * Interrupt handler for legacy INTx interrupts for T3B-based cards.
  1914. * Handles data events from SGE response queues as well as error and other
  1915. * async events as they all use the same interrupt pin. We use one SGE
  1916. * response queue per port in this mode and protect all response queues with
  1917. * queue 0's lock.
  1918. */
  1919. static irqreturn_t t3b_intr(int irq, void *cookie)
  1920. {
  1921. u32 map;
  1922. struct adapter *adap = cookie;
  1923. struct sge_rspq *q0 = &adap->sge.qs[0].rspq;
  1924. t3_write_reg(adap, A_PL_CLI, 0);
  1925. map = t3_read_reg(adap, A_SG_DATA_INTR);
  1926. if (unlikely(!map)) /* shared interrupt, most likely */
  1927. return IRQ_NONE;
  1928. spin_lock(&q0->lock);
  1929. if (unlikely(map & F_ERRINTR))
  1930. t3_slow_intr_handler(adap);
  1931. if (likely(map & 1))
  1932. process_responses_gts(adap, q0);
  1933. if (map & 2)
  1934. process_responses_gts(adap, &adap->sge.qs[1].rspq);
  1935. spin_unlock(&q0->lock);
  1936. return IRQ_HANDLED;
  1937. }
  1938. /*
  1939. * NAPI interrupt handler for legacy INTx interrupts for T3B-based cards.
  1940. * Handles data events from SGE response queues as well as error and other
  1941. * async events as they all use the same interrupt pin. We use one SGE
  1942. * response queue per port in this mode and protect all response queues with
  1943. * queue 0's lock.
  1944. */
  1945. static irqreturn_t t3b_intr_napi(int irq, void *cookie)
  1946. {
  1947. u32 map;
  1948. struct net_device *dev;
  1949. struct adapter *adap = cookie;
  1950. struct sge_rspq *q0 = &adap->sge.qs[0].rspq;
  1951. t3_write_reg(adap, A_PL_CLI, 0);
  1952. map = t3_read_reg(adap, A_SG_DATA_INTR);
  1953. if (unlikely(!map)) /* shared interrupt, most likely */
  1954. return IRQ_NONE;
  1955. spin_lock(&q0->lock);
  1956. if (unlikely(map & F_ERRINTR))
  1957. t3_slow_intr_handler(adap);
  1958. if (likely(map & 1)) {
  1959. dev = adap->sge.qs[0].netdev;
  1960. if (likely(__netif_rx_schedule_prep(dev)))
  1961. __netif_rx_schedule(dev);
  1962. }
  1963. if (map & 2) {
  1964. dev = adap->sge.qs[1].netdev;
  1965. if (likely(__netif_rx_schedule_prep(dev)))
  1966. __netif_rx_schedule(dev);
  1967. }
  1968. spin_unlock(&q0->lock);
  1969. return IRQ_HANDLED;
  1970. }
  1971. /**
  1972. * t3_intr_handler - select the top-level interrupt handler
  1973. * @adap: the adapter
  1974. * @polling: whether using NAPI to service response queues
  1975. *
  1976. * Selects the top-level interrupt handler based on the type of interrupts
  1977. * (MSI-X, MSI, or legacy) and whether NAPI will be used to service the
  1978. * response queues.
  1979. */
  1980. intr_handler_t t3_intr_handler(struct adapter *adap, int polling)
  1981. {
  1982. if (adap->flags & USING_MSIX)
  1983. return polling ? t3_sge_intr_msix_napi : t3_sge_intr_msix;
  1984. if (adap->flags & USING_MSI)
  1985. return polling ? t3_intr_msi_napi : t3_intr_msi;
  1986. if (adap->params.rev > 0)
  1987. return polling ? t3b_intr_napi : t3b_intr;
  1988. return t3_intr;
  1989. }
  1990. /**
  1991. * t3_sge_err_intr_handler - SGE async event interrupt handler
  1992. * @adapter: the adapter
  1993. *
  1994. * Interrupt handler for SGE asynchronous (non-data) events.
  1995. */
  1996. void t3_sge_err_intr_handler(struct adapter *adapter)
  1997. {
  1998. unsigned int v, status = t3_read_reg(adapter, A_SG_INT_CAUSE);
  1999. if (status & F_RSPQCREDITOVERFOW)
  2000. CH_ALERT(adapter, "SGE response queue credit overflow\n");
  2001. if (status & F_RSPQDISABLED) {
  2002. v = t3_read_reg(adapter, A_SG_RSPQ_FL_STATUS);
  2003. CH_ALERT(adapter,
  2004. "packet delivered to disabled response queue "
  2005. "(0x%x)\n", (v >> S_RSPQ0DISABLED) & 0xff);
  2006. }
  2007. t3_write_reg(adapter, A_SG_INT_CAUSE, status);
  2008. if (status & (F_RSPQCREDITOVERFOW | F_RSPQDISABLED))
  2009. t3_fatal_err(adapter);
  2010. }
  2011. /**
  2012. * sge_timer_cb - perform periodic maintenance of an SGE qset
  2013. * @data: the SGE queue set to maintain
  2014. *
  2015. * Runs periodically from a timer to perform maintenance of an SGE queue
  2016. * set. It performs two tasks:
  2017. *
  2018. * a) Cleans up any completed Tx descriptors that may still be pending.
  2019. * Normal descriptor cleanup happens when new packets are added to a Tx
  2020. * queue so this timer is relatively infrequent and does any cleanup only
  2021. * if the Tx queue has not seen any new packets in a while. We make a
  2022. * best effort attempt to reclaim descriptors, in that we don't wait
  2023. * around if we cannot get a queue's lock (which most likely is because
  2024. * someone else is queueing new packets and so will also handle the clean
  2025. * up). Since control queues use immediate data exclusively we don't
  2026. * bother cleaning them up here.
  2027. *
  2028. * b) Replenishes Rx queues that have run out due to memory shortage.
  2029. * Normally new Rx buffers are added when existing ones are consumed but
  2030. * when out of memory a queue can become empty. We try to add only a few
  2031. * buffers here, the queue will be replenished fully as these new buffers
  2032. * are used up if memory shortage has subsided.
  2033. */
  2034. static void sge_timer_cb(unsigned long data)
  2035. {
  2036. spinlock_t *lock;
  2037. struct sge_qset *qs = (struct sge_qset *)data;
  2038. struct adapter *adap = qs->netdev->priv;
  2039. if (spin_trylock(&qs->txq[TXQ_ETH].lock)) {
  2040. reclaim_completed_tx(adap, &qs->txq[TXQ_ETH]);
  2041. spin_unlock(&qs->txq[TXQ_ETH].lock);
  2042. }
  2043. if (spin_trylock(&qs->txq[TXQ_OFLD].lock)) {
  2044. reclaim_completed_tx(adap, &qs->txq[TXQ_OFLD]);
  2045. spin_unlock(&qs->txq[TXQ_OFLD].lock);
  2046. }
  2047. lock = (adap->flags & USING_MSIX) ? &qs->rspq.lock :
  2048. &adap->sge.qs[0].rspq.lock;
  2049. if (spin_trylock_irq(lock)) {
  2050. if (!napi_is_scheduled(qs->netdev)) {
  2051. if (qs->fl[0].credits < qs->fl[0].size)
  2052. __refill_fl(adap, &qs->fl[0]);
  2053. if (qs->fl[1].credits < qs->fl[1].size)
  2054. __refill_fl(adap, &qs->fl[1]);
  2055. }
  2056. spin_unlock_irq(lock);
  2057. }
  2058. mod_timer(&qs->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
  2059. }
  2060. /**
  2061. * t3_update_qset_coalesce - update coalescing settings for a queue set
  2062. * @qs: the SGE queue set
  2063. * @p: new queue set parameters
  2064. *
  2065. * Update the coalescing settings for an SGE queue set. Nothing is done
  2066. * if the queue set is not initialized yet.
  2067. */
  2068. void t3_update_qset_coalesce(struct sge_qset *qs, const struct qset_params *p)
  2069. {
  2070. if (!qs->netdev)
  2071. return;
  2072. qs->rspq.holdoff_tmr = max(p->coalesce_usecs * 10, 1U);/* can't be 0 */
  2073. qs->rspq.polling = p->polling;
  2074. qs->netdev->poll = p->polling ? napi_rx_handler : ofld_poll;
  2075. }
  2076. /**
  2077. * t3_sge_alloc_qset - initialize an SGE queue set
  2078. * @adapter: the adapter
  2079. * @id: the queue set id
  2080. * @nports: how many Ethernet ports will be using this queue set
  2081. * @irq_vec_idx: the IRQ vector index for response queue interrupts
  2082. * @p: configuration parameters for this queue set
  2083. * @ntxq: number of Tx queues for the queue set
  2084. * @netdev: net device associated with this queue set
  2085. *
  2086. * Allocate resources and initialize an SGE queue set. A queue set
  2087. * comprises a response queue, two Rx free-buffer queues, and up to 3
  2088. * Tx queues. The Tx queues are assigned roles in the order Ethernet
  2089. * queue, offload queue, and control queue.
  2090. */
  2091. int t3_sge_alloc_qset(struct adapter *adapter, unsigned int id, int nports,
  2092. int irq_vec_idx, const struct qset_params *p,
  2093. int ntxq, struct net_device *netdev)
  2094. {
  2095. int i, ret = -ENOMEM;
  2096. struct sge_qset *q = &adapter->sge.qs[id];
  2097. init_qset_cntxt(q, id);
  2098. init_timer(&q->tx_reclaim_timer);
  2099. q->tx_reclaim_timer.data = (unsigned long)q;
  2100. q->tx_reclaim_timer.function = sge_timer_cb;
  2101. q->fl[0].desc = alloc_ring(adapter->pdev, p->fl_size,
  2102. sizeof(struct rx_desc),
  2103. sizeof(struct rx_sw_desc),
  2104. &q->fl[0].phys_addr, &q->fl[0].sdesc);
  2105. if (!q->fl[0].desc)
  2106. goto err;
  2107. q->fl[1].desc = alloc_ring(adapter->pdev, p->jumbo_size,
  2108. sizeof(struct rx_desc),
  2109. sizeof(struct rx_sw_desc),
  2110. &q->fl[1].phys_addr, &q->fl[1].sdesc);
  2111. if (!q->fl[1].desc)
  2112. goto err;
  2113. q->rspq.desc = alloc_ring(adapter->pdev, p->rspq_size,
  2114. sizeof(struct rsp_desc), 0,
  2115. &q->rspq.phys_addr, NULL);
  2116. if (!q->rspq.desc)
  2117. goto err;
  2118. for (i = 0; i < ntxq; ++i) {
  2119. /*
  2120. * The control queue always uses immediate data so does not
  2121. * need to keep track of any sk_buffs.
  2122. */
  2123. size_t sz = i == TXQ_CTRL ? 0 : sizeof(struct tx_sw_desc);
  2124. q->txq[i].desc = alloc_ring(adapter->pdev, p->txq_size[i],
  2125. sizeof(struct tx_desc), sz,
  2126. &q->txq[i].phys_addr,
  2127. &q->txq[i].sdesc);
  2128. if (!q->txq[i].desc)
  2129. goto err;
  2130. q->txq[i].gen = 1;
  2131. q->txq[i].size = p->txq_size[i];
  2132. spin_lock_init(&q->txq[i].lock);
  2133. skb_queue_head_init(&q->txq[i].sendq);
  2134. }
  2135. tasklet_init(&q->txq[TXQ_OFLD].qresume_tsk, restart_offloadq,
  2136. (unsigned long)q);
  2137. tasklet_init(&q->txq[TXQ_CTRL].qresume_tsk, restart_ctrlq,
  2138. (unsigned long)q);
  2139. q->fl[0].gen = q->fl[1].gen = 1;
  2140. q->fl[0].size = p->fl_size;
  2141. q->fl[1].size = p->jumbo_size;
  2142. q->rspq.gen = 1;
  2143. q->rspq.size = p->rspq_size;
  2144. spin_lock_init(&q->rspq.lock);
  2145. q->txq[TXQ_ETH].stop_thres = nports *
  2146. flits_to_desc(sgl_len(MAX_SKB_FRAGS + 1) + 3);
  2147. if (ntxq == 1) {
  2148. q->fl[0].buf_size = SGE_RX_SM_BUF_SIZE + 2 +
  2149. sizeof(struct cpl_rx_pkt);
  2150. q->fl[1].buf_size = MAX_FRAME_SIZE + 2 +
  2151. sizeof(struct cpl_rx_pkt);
  2152. } else {
  2153. q->fl[0].buf_size = SGE_RX_SM_BUF_SIZE +
  2154. sizeof(struct cpl_rx_data);
  2155. q->fl[1].buf_size = (16 * 1024) -
  2156. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2157. }
  2158. spin_lock(&adapter->sge.reg_lock);
  2159. /* FL threshold comparison uses < */
  2160. ret = t3_sge_init_rspcntxt(adapter, q->rspq.cntxt_id, irq_vec_idx,
  2161. q->rspq.phys_addr, q->rspq.size,
  2162. q->fl[0].buf_size, 1, 0);
  2163. if (ret)
  2164. goto err_unlock;
  2165. for (i = 0; i < SGE_RXQ_PER_SET; ++i) {
  2166. ret = t3_sge_init_flcntxt(adapter, q->fl[i].cntxt_id, 0,
  2167. q->fl[i].phys_addr, q->fl[i].size,
  2168. q->fl[i].buf_size, p->cong_thres, 1,
  2169. 0);
  2170. if (ret)
  2171. goto err_unlock;
  2172. }
  2173. ret = t3_sge_init_ecntxt(adapter, q->txq[TXQ_ETH].cntxt_id, USE_GTS,
  2174. SGE_CNTXT_ETH, id, q->txq[TXQ_ETH].phys_addr,
  2175. q->txq[TXQ_ETH].size, q->txq[TXQ_ETH].token,
  2176. 1, 0);
  2177. if (ret)
  2178. goto err_unlock;
  2179. if (ntxq > 1) {
  2180. ret = t3_sge_init_ecntxt(adapter, q->txq[TXQ_OFLD].cntxt_id,
  2181. USE_GTS, SGE_CNTXT_OFLD, id,
  2182. q->txq[TXQ_OFLD].phys_addr,
  2183. q->txq[TXQ_OFLD].size, 0, 1, 0);
  2184. if (ret)
  2185. goto err_unlock;
  2186. }
  2187. if (ntxq > 2) {
  2188. ret = t3_sge_init_ecntxt(adapter, q->txq[TXQ_CTRL].cntxt_id, 0,
  2189. SGE_CNTXT_CTRL, id,
  2190. q->txq[TXQ_CTRL].phys_addr,
  2191. q->txq[TXQ_CTRL].size,
  2192. q->txq[TXQ_CTRL].token, 1, 0);
  2193. if (ret)
  2194. goto err_unlock;
  2195. }
  2196. spin_unlock(&adapter->sge.reg_lock);
  2197. q->netdev = netdev;
  2198. t3_update_qset_coalesce(q, p);
  2199. /*
  2200. * We use atalk_ptr as a backpointer to a qset. In case a device is
  2201. * associated with multiple queue sets only the first one sets
  2202. * atalk_ptr.
  2203. */
  2204. if (netdev->atalk_ptr == NULL)
  2205. netdev->atalk_ptr = q;
  2206. refill_fl(adapter, &q->fl[0], q->fl[0].size, GFP_KERNEL);
  2207. refill_fl(adapter, &q->fl[1], q->fl[1].size, GFP_KERNEL);
  2208. refill_rspq(adapter, &q->rspq, q->rspq.size - 1);
  2209. t3_write_reg(adapter, A_SG_GTS, V_RSPQ(q->rspq.cntxt_id) |
  2210. V_NEWTIMER(q->rspq.holdoff_tmr));
  2211. mod_timer(&q->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
  2212. return 0;
  2213. err_unlock:
  2214. spin_unlock(&adapter->sge.reg_lock);
  2215. err:
  2216. t3_free_qset(adapter, q);
  2217. return ret;
  2218. }
  2219. /**
  2220. * t3_free_sge_resources - free SGE resources
  2221. * @adap: the adapter
  2222. *
  2223. * Frees resources used by the SGE queue sets.
  2224. */
  2225. void t3_free_sge_resources(struct adapter *adap)
  2226. {
  2227. int i;
  2228. for (i = 0; i < SGE_QSETS; ++i)
  2229. t3_free_qset(adap, &adap->sge.qs[i]);
  2230. }
  2231. /**
  2232. * t3_sge_start - enable SGE
  2233. * @adap: the adapter
  2234. *
  2235. * Enables the SGE for DMAs. This is the last step in starting packet
  2236. * transfers.
  2237. */
  2238. void t3_sge_start(struct adapter *adap)
  2239. {
  2240. t3_set_reg_field(adap, A_SG_CONTROL, F_GLOBALENABLE, F_GLOBALENABLE);
  2241. }
  2242. /**
  2243. * t3_sge_stop - disable SGE operation
  2244. * @adap: the adapter
  2245. *
  2246. * Disables the DMA engine. This can be called in emeregencies (e.g.,
  2247. * from error interrupts) or from normal process context. In the latter
  2248. * case it also disables any pending queue restart tasklets. Note that
  2249. * if it is called in interrupt context it cannot disable the restart
  2250. * tasklets as it cannot wait, however the tasklets will have no effect
  2251. * since the doorbells are disabled and the driver will call this again
  2252. * later from process context, at which time the tasklets will be stopped
  2253. * if they are still running.
  2254. */
  2255. void t3_sge_stop(struct adapter *adap)
  2256. {
  2257. t3_set_reg_field(adap, A_SG_CONTROL, F_GLOBALENABLE, 0);
  2258. if (!in_interrupt()) {
  2259. int i;
  2260. for (i = 0; i < SGE_QSETS; ++i) {
  2261. struct sge_qset *qs = &adap->sge.qs[i];
  2262. tasklet_kill(&qs->txq[TXQ_OFLD].qresume_tsk);
  2263. tasklet_kill(&qs->txq[TXQ_CTRL].qresume_tsk);
  2264. }
  2265. }
  2266. }
  2267. /**
  2268. * t3_sge_init - initialize SGE
  2269. * @adap: the adapter
  2270. * @p: the SGE parameters
  2271. *
  2272. * Performs SGE initialization needed every time after a chip reset.
  2273. * We do not initialize any of the queue sets here, instead the driver
  2274. * top-level must request those individually. We also do not enable DMA
  2275. * here, that should be done after the queues have been set up.
  2276. */
  2277. void t3_sge_init(struct adapter *adap, struct sge_params *p)
  2278. {
  2279. unsigned int ctrl, ups = ffs(pci_resource_len(adap->pdev, 2) >> 12);
  2280. ctrl = F_DROPPKT | V_PKTSHIFT(2) | F_FLMODE | F_AVOIDCQOVFL |
  2281. F_CQCRDTCTRL |
  2282. V_HOSTPAGESIZE(PAGE_SHIFT - 11) | F_BIGENDIANINGRESS |
  2283. V_USERSPACESIZE(ups ? ups - 1 : 0) | F_ISCSICOALESCING;
  2284. #if SGE_NUM_GENBITS == 1
  2285. ctrl |= F_EGRGENCTRL;
  2286. #endif
  2287. if (adap->params.rev > 0) {
  2288. if (!(adap->flags & (USING_MSIX | USING_MSI)))
  2289. ctrl |= F_ONEINTMULTQ | F_OPTONEINTMULTQ;
  2290. ctrl |= F_CQCRDTCTRL | F_AVOIDCQOVFL;
  2291. }
  2292. t3_write_reg(adap, A_SG_CONTROL, ctrl);
  2293. t3_write_reg(adap, A_SG_EGR_RCQ_DRB_THRSH, V_HIRCQDRBTHRSH(512) |
  2294. V_LORCQDRBTHRSH(512));
  2295. t3_write_reg(adap, A_SG_TIMER_TICK, core_ticks_per_usec(adap) / 10);
  2296. t3_write_reg(adap, A_SG_CMDQ_CREDIT_TH, V_THRESHOLD(32) |
  2297. V_TIMEOUT(200 * core_ticks_per_usec(adap)));
  2298. t3_write_reg(adap, A_SG_HI_DRB_HI_THRSH, 1000);
  2299. t3_write_reg(adap, A_SG_HI_DRB_LO_THRSH, 256);
  2300. t3_write_reg(adap, A_SG_LO_DRB_HI_THRSH, 1000);
  2301. t3_write_reg(adap, A_SG_LO_DRB_LO_THRSH, 256);
  2302. t3_write_reg(adap, A_SG_OCO_BASE, V_BASE1(0xfff));
  2303. t3_write_reg(adap, A_SG_DRB_PRI_THRESH, 63 * 1024);
  2304. }
  2305. /**
  2306. * t3_sge_prep - one-time SGE initialization
  2307. * @adap: the associated adapter
  2308. * @p: SGE parameters
  2309. *
  2310. * Performs one-time initialization of SGE SW state. Includes determining
  2311. * defaults for the assorted SGE parameters, which admins can change until
  2312. * they are used to initialize the SGE.
  2313. */
  2314. void __devinit t3_sge_prep(struct adapter *adap, struct sge_params *p)
  2315. {
  2316. int i;
  2317. p->max_pkt_size = (16 * 1024) - sizeof(struct cpl_rx_data) -
  2318. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2319. for (i = 0; i < SGE_QSETS; ++i) {
  2320. struct qset_params *q = p->qset + i;
  2321. q->polling = adap->params.rev > 0;
  2322. q->coalesce_usecs = 5;
  2323. q->rspq_size = 1024;
  2324. q->fl_size = 4096;
  2325. q->jumbo_size = 512;
  2326. q->txq_size[TXQ_ETH] = 1024;
  2327. q->txq_size[TXQ_OFLD] = 1024;
  2328. q->txq_size[TXQ_CTRL] = 256;
  2329. q->cong_thres = 0;
  2330. }
  2331. spin_lock_init(&adap->sge.reg_lock);
  2332. }
  2333. /**
  2334. * t3_get_desc - dump an SGE descriptor for debugging purposes
  2335. * @qs: the queue set
  2336. * @qnum: identifies the specific queue (0..2: Tx, 3:response, 4..5: Rx)
  2337. * @idx: the descriptor index in the queue
  2338. * @data: where to dump the descriptor contents
  2339. *
  2340. * Dumps the contents of a HW descriptor of an SGE queue. Returns the
  2341. * size of the descriptor.
  2342. */
  2343. int t3_get_desc(const struct sge_qset *qs, unsigned int qnum, unsigned int idx,
  2344. unsigned char *data)
  2345. {
  2346. if (qnum >= 6)
  2347. return -EINVAL;
  2348. if (qnum < 3) {
  2349. if (!qs->txq[qnum].desc || idx >= qs->txq[qnum].size)
  2350. return -EINVAL;
  2351. memcpy(data, &qs->txq[qnum].desc[idx], sizeof(struct tx_desc));
  2352. return sizeof(struct tx_desc);
  2353. }
  2354. if (qnum == 3) {
  2355. if (!qs->rspq.desc || idx >= qs->rspq.size)
  2356. return -EINVAL;
  2357. memcpy(data, &qs->rspq.desc[idx], sizeof(struct rsp_desc));
  2358. return sizeof(struct rsp_desc);
  2359. }
  2360. qnum -= 4;
  2361. if (!qs->fl[qnum].desc || idx >= qs->fl[qnum].size)
  2362. return -EINVAL;
  2363. memcpy(data, &qs->fl[qnum].desc[idx], sizeof(struct rx_desc));
  2364. return sizeof(struct rx_desc);
  2365. }