sge.c 81 KB

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