sge.c 89 KB

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