sge.c 84 KB

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