sge.c 81 KB

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