sge.c 82 KB

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