sge.c 76 KB

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