sge.c 81 KB

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