sge.c 92 KB

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