sge.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448
  1. /*
  2. * This file is part of the Chelsio T4 Ethernet driver for Linux.
  3. *
  4. * Copyright (c) 2003-2010 Chelsio Communications, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/etherdevice.h>
  37. #include <linux/if_vlan.h>
  38. #include <linux/ip.h>
  39. #include <linux/dma-mapping.h>
  40. #include <linux/jiffies.h>
  41. #include <net/ipv6.h>
  42. #include <net/tcp.h>
  43. #include "cxgb4.h"
  44. #include "t4_regs.h"
  45. #include "t4_msg.h"
  46. #include "t4fw_api.h"
  47. /*
  48. * Rx buffer size. We use largish buffers if possible but settle for single
  49. * pages under memory shortage.
  50. */
  51. #if PAGE_SHIFT >= 16
  52. # define FL_PG_ORDER 0
  53. #else
  54. # define FL_PG_ORDER (16 - PAGE_SHIFT)
  55. #endif
  56. /* RX_PULL_LEN should be <= RX_COPY_THRES */
  57. #define RX_COPY_THRES 256
  58. #define RX_PULL_LEN 128
  59. /*
  60. * Main body length for sk_buffs used for Rx Ethernet packets with fragments.
  61. * Should be >= RX_PULL_LEN but possibly bigger to give pskb_may_pull some room.
  62. */
  63. #define RX_PKT_SKB_LEN 512
  64. /* Ethernet header padding prepended to RX_PKTs */
  65. #define RX_PKT_PAD 2
  66. /*
  67. * Max number of Tx descriptors we clean up at a time. Should be modest as
  68. * freeing skbs isn't cheap and it happens while holding locks. We just need
  69. * to free packets faster than they arrive, we eventually catch up and keep
  70. * the amortized cost reasonable. Must be >= 2 * TXQ_STOP_THRES.
  71. */
  72. #define MAX_TX_RECLAIM 16
  73. /*
  74. * Max number of Rx buffers we replenish at a time. Again keep this modest,
  75. * allocating buffers isn't cheap either.
  76. */
  77. #define MAX_RX_REFILL 16U
  78. /*
  79. * Period of the Rx queue check timer. This timer is infrequent as it has
  80. * something to do only when the system experiences severe memory shortage.
  81. */
  82. #define RX_QCHECK_PERIOD (HZ / 2)
  83. /*
  84. * Period of the Tx queue check timer.
  85. */
  86. #define TX_QCHECK_PERIOD (HZ / 2)
  87. /*
  88. * Max number of Tx descriptors to be reclaimed by the Tx timer.
  89. */
  90. #define MAX_TIMER_TX_RECLAIM 100
  91. /*
  92. * Timer index used when backing off due to memory shortage.
  93. */
  94. #define NOMEM_TMR_IDX (SGE_NTIMERS - 1)
  95. /*
  96. * An FL with <= FL_STARVE_THRES buffers is starving and a periodic timer will
  97. * attempt to refill it.
  98. */
  99. #define FL_STARVE_THRES 4
  100. /*
  101. * Suspend an Ethernet Tx queue with fewer available descriptors than this.
  102. * This is the same as calc_tx_descs() for a TSO packet with
  103. * nr_frags == MAX_SKB_FRAGS.
  104. */
  105. #define ETHTXQ_STOP_THRES \
  106. (1 + DIV_ROUND_UP((3 * MAX_SKB_FRAGS) / 2 + (MAX_SKB_FRAGS & 1), 8))
  107. /*
  108. * Suspension threshold for non-Ethernet Tx queues. We require enough room
  109. * for a full sized WR.
  110. */
  111. #define TXQ_STOP_THRES (SGE_MAX_WR_LEN / sizeof(struct tx_desc))
  112. /*
  113. * Max Tx descriptor space we allow for an Ethernet packet to be inlined
  114. * into a WR.
  115. */
  116. #define MAX_IMM_TX_PKT_LEN 128
  117. /*
  118. * Max size of a WR sent through a control Tx queue.
  119. */
  120. #define MAX_CTRL_WR_LEN SGE_MAX_WR_LEN
  121. enum {
  122. /* packet alignment in FL buffers */
  123. FL_ALIGN = L1_CACHE_BYTES < 32 ? 32 : L1_CACHE_BYTES,
  124. /* egress status entry size */
  125. STAT_LEN = L1_CACHE_BYTES > 64 ? 128 : 64
  126. };
  127. struct tx_sw_desc { /* SW state per Tx descriptor */
  128. struct sk_buff *skb;
  129. struct ulptx_sgl *sgl;
  130. };
  131. struct rx_sw_desc { /* SW state per Rx descriptor */
  132. struct page *page;
  133. dma_addr_t dma_addr;
  134. };
  135. /*
  136. * The low bits of rx_sw_desc.dma_addr have special meaning.
  137. */
  138. enum {
  139. RX_LARGE_BUF = 1 << 0, /* buffer is larger than PAGE_SIZE */
  140. RX_UNMAPPED_BUF = 1 << 1, /* buffer is not mapped */
  141. };
  142. static inline dma_addr_t get_buf_addr(const struct rx_sw_desc *d)
  143. {
  144. return d->dma_addr & ~(dma_addr_t)(RX_LARGE_BUF | RX_UNMAPPED_BUF);
  145. }
  146. static inline bool is_buf_mapped(const struct rx_sw_desc *d)
  147. {
  148. return !(d->dma_addr & RX_UNMAPPED_BUF);
  149. }
  150. /**
  151. * txq_avail - return the number of available slots in a Tx queue
  152. * @q: the Tx queue
  153. *
  154. * Returns the number of descriptors in a Tx queue available to write new
  155. * packets.
  156. */
  157. static inline unsigned int txq_avail(const struct sge_txq *q)
  158. {
  159. return q->size - 1 - q->in_use;
  160. }
  161. /**
  162. * fl_cap - return the capacity of a free-buffer list
  163. * @fl: the FL
  164. *
  165. * Returns the capacity of a free-buffer list. The capacity is less than
  166. * the size because one descriptor needs to be left unpopulated, otherwise
  167. * HW will think the FL is empty.
  168. */
  169. static inline unsigned int fl_cap(const struct sge_fl *fl)
  170. {
  171. return fl->size - 8; /* 1 descriptor = 8 buffers */
  172. }
  173. static inline bool fl_starving(const struct sge_fl *fl)
  174. {
  175. return fl->avail - fl->pend_cred <= FL_STARVE_THRES;
  176. }
  177. static int map_skb(struct device *dev, const struct sk_buff *skb,
  178. dma_addr_t *addr)
  179. {
  180. const skb_frag_t *fp, *end;
  181. const struct skb_shared_info *si;
  182. *addr = dma_map_single(dev, skb->data, skb_headlen(skb), DMA_TO_DEVICE);
  183. if (dma_mapping_error(dev, *addr))
  184. goto out_err;
  185. si = skb_shinfo(skb);
  186. end = &si->frags[si->nr_frags];
  187. for (fp = si->frags; fp < end; fp++) {
  188. *++addr = dma_map_page(dev, fp->page, fp->page_offset, fp->size,
  189. DMA_TO_DEVICE);
  190. if (dma_mapping_error(dev, *addr))
  191. goto unwind;
  192. }
  193. return 0;
  194. unwind:
  195. while (fp-- > si->frags)
  196. dma_unmap_page(dev, *--addr, fp->size, DMA_TO_DEVICE);
  197. dma_unmap_single(dev, addr[-1], skb_headlen(skb), DMA_TO_DEVICE);
  198. out_err:
  199. return -ENOMEM;
  200. }
  201. #ifdef CONFIG_NEED_DMA_MAP_STATE
  202. static void unmap_skb(struct device *dev, const struct sk_buff *skb,
  203. const dma_addr_t *addr)
  204. {
  205. const skb_frag_t *fp, *end;
  206. const struct skb_shared_info *si;
  207. dma_unmap_single(dev, *addr++, skb_headlen(skb), DMA_TO_DEVICE);
  208. si = skb_shinfo(skb);
  209. end = &si->frags[si->nr_frags];
  210. for (fp = si->frags; fp < end; fp++)
  211. dma_unmap_page(dev, *addr++, fp->size, DMA_TO_DEVICE);
  212. }
  213. /**
  214. * deferred_unmap_destructor - unmap a packet when it is freed
  215. * @skb: the packet
  216. *
  217. * This is the packet destructor used for Tx packets that need to remain
  218. * mapped until they are freed rather than until their Tx descriptors are
  219. * freed.
  220. */
  221. static void deferred_unmap_destructor(struct sk_buff *skb)
  222. {
  223. unmap_skb(skb->dev->dev.parent, skb, (dma_addr_t *)skb->head);
  224. }
  225. #endif
  226. static void unmap_sgl(struct device *dev, const struct sk_buff *skb,
  227. const struct ulptx_sgl *sgl, const struct sge_txq *q)
  228. {
  229. const struct ulptx_sge_pair *p;
  230. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  231. if (likely(skb_headlen(skb)))
  232. dma_unmap_single(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  233. DMA_TO_DEVICE);
  234. else {
  235. dma_unmap_page(dev, be64_to_cpu(sgl->addr0), ntohl(sgl->len0),
  236. DMA_TO_DEVICE);
  237. nfrags--;
  238. }
  239. /*
  240. * the complexity below is because of the possibility of a wrap-around
  241. * in the middle of an SGL
  242. */
  243. for (p = sgl->sge; nfrags >= 2; nfrags -= 2) {
  244. if (likely((u8 *)(p + 1) <= (u8 *)q->stat)) {
  245. unmap: dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  246. ntohl(p->len[0]), DMA_TO_DEVICE);
  247. dma_unmap_page(dev, be64_to_cpu(p->addr[1]),
  248. ntohl(p->len[1]), DMA_TO_DEVICE);
  249. p++;
  250. } else if ((u8 *)p == (u8 *)q->stat) {
  251. p = (const struct ulptx_sge_pair *)q->desc;
  252. goto unmap;
  253. } else if ((u8 *)p + 8 == (u8 *)q->stat) {
  254. const __be64 *addr = (const __be64 *)q->desc;
  255. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  256. ntohl(p->len[0]), DMA_TO_DEVICE);
  257. dma_unmap_page(dev, be64_to_cpu(addr[1]),
  258. ntohl(p->len[1]), DMA_TO_DEVICE);
  259. p = (const struct ulptx_sge_pair *)&addr[2];
  260. } else {
  261. const __be64 *addr = (const __be64 *)q->desc;
  262. dma_unmap_page(dev, be64_to_cpu(p->addr[0]),
  263. ntohl(p->len[0]), DMA_TO_DEVICE);
  264. dma_unmap_page(dev, be64_to_cpu(addr[0]),
  265. ntohl(p->len[1]), DMA_TO_DEVICE);
  266. p = (const struct ulptx_sge_pair *)&addr[1];
  267. }
  268. }
  269. if (nfrags) {
  270. __be64 addr;
  271. if ((u8 *)p == (u8 *)q->stat)
  272. p = (const struct ulptx_sge_pair *)q->desc;
  273. addr = (u8 *)p + 16 <= (u8 *)q->stat ? p->addr[0] :
  274. *(const __be64 *)q->desc;
  275. dma_unmap_page(dev, be64_to_cpu(addr), ntohl(p->len[0]),
  276. DMA_TO_DEVICE);
  277. }
  278. }
  279. /**
  280. * free_tx_desc - reclaims Tx descriptors and their buffers
  281. * @adapter: the adapter
  282. * @q: the Tx queue to reclaim descriptors from
  283. * @n: the number of descriptors to reclaim
  284. * @unmap: whether the buffers should be unmapped for DMA
  285. *
  286. * Reclaims Tx descriptors from an SGE Tx queue and frees the associated
  287. * Tx buffers. Called with the Tx queue lock held.
  288. */
  289. static void free_tx_desc(struct adapter *adap, struct sge_txq *q,
  290. unsigned int n, bool unmap)
  291. {
  292. struct tx_sw_desc *d;
  293. unsigned int cidx = q->cidx;
  294. struct device *dev = adap->pdev_dev;
  295. d = &q->sdesc[cidx];
  296. while (n--) {
  297. if (d->skb) { /* an SGL is present */
  298. if (unmap)
  299. unmap_sgl(dev, d->skb, d->sgl, q);
  300. kfree_skb(d->skb);
  301. d->skb = NULL;
  302. }
  303. ++d;
  304. if (++cidx == q->size) {
  305. cidx = 0;
  306. d = q->sdesc;
  307. }
  308. }
  309. q->cidx = cidx;
  310. }
  311. /*
  312. * Return the number of reclaimable descriptors in a Tx queue.
  313. */
  314. static inline int reclaimable(const struct sge_txq *q)
  315. {
  316. int hw_cidx = ntohs(q->stat->cidx);
  317. hw_cidx -= q->cidx;
  318. return hw_cidx < 0 ? hw_cidx + q->size : hw_cidx;
  319. }
  320. /**
  321. * reclaim_completed_tx - reclaims completed Tx descriptors
  322. * @adap: the adapter
  323. * @q: the Tx queue to reclaim completed descriptors from
  324. * @unmap: whether the buffers should be unmapped for DMA
  325. *
  326. * Reclaims Tx descriptors that the SGE has indicated it has processed,
  327. * and frees the associated buffers if possible. Called with the Tx
  328. * queue locked.
  329. */
  330. static inline void reclaim_completed_tx(struct adapter *adap, struct sge_txq *q,
  331. bool unmap)
  332. {
  333. int avail = reclaimable(q);
  334. if (avail) {
  335. /*
  336. * Limit the amount of clean up work we do at a time to keep
  337. * the Tx lock hold time O(1).
  338. */
  339. if (avail > MAX_TX_RECLAIM)
  340. avail = MAX_TX_RECLAIM;
  341. free_tx_desc(adap, q, avail, unmap);
  342. q->in_use -= avail;
  343. }
  344. }
  345. static inline int get_buf_size(const struct rx_sw_desc *d)
  346. {
  347. #if FL_PG_ORDER > 0
  348. return (d->dma_addr & RX_LARGE_BUF) ? (PAGE_SIZE << FL_PG_ORDER) :
  349. PAGE_SIZE;
  350. #else
  351. return PAGE_SIZE;
  352. #endif
  353. }
  354. /**
  355. * free_rx_bufs - free the Rx buffers on an SGE free list
  356. * @adap: the adapter
  357. * @q: the SGE free list to free buffers from
  358. * @n: how many buffers to free
  359. *
  360. * Release the next @n buffers on an SGE free-buffer Rx queue. The
  361. * buffers must be made inaccessible to HW before calling this function.
  362. */
  363. static void free_rx_bufs(struct adapter *adap, struct sge_fl *q, int n)
  364. {
  365. while (n--) {
  366. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  367. if (is_buf_mapped(d))
  368. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  369. get_buf_size(d), PCI_DMA_FROMDEVICE);
  370. put_page(d->page);
  371. d->page = NULL;
  372. if (++q->cidx == q->size)
  373. q->cidx = 0;
  374. q->avail--;
  375. }
  376. }
  377. /**
  378. * unmap_rx_buf - unmap the current Rx buffer on an SGE free list
  379. * @adap: the adapter
  380. * @q: the SGE free list
  381. *
  382. * Unmap the current buffer on an SGE free-buffer Rx queue. The
  383. * buffer must be made inaccessible to HW before calling this function.
  384. *
  385. * This is similar to @free_rx_bufs above but does not free the buffer.
  386. * Do note that the FL still loses any further access to the buffer.
  387. */
  388. static void unmap_rx_buf(struct adapter *adap, struct sge_fl *q)
  389. {
  390. struct rx_sw_desc *d = &q->sdesc[q->cidx];
  391. if (is_buf_mapped(d))
  392. dma_unmap_page(adap->pdev_dev, get_buf_addr(d),
  393. get_buf_size(d), PCI_DMA_FROMDEVICE);
  394. d->page = NULL;
  395. if (++q->cidx == q->size)
  396. q->cidx = 0;
  397. q->avail--;
  398. }
  399. static inline void ring_fl_db(struct adapter *adap, struct sge_fl *q)
  400. {
  401. if (q->pend_cred >= 8) {
  402. wmb();
  403. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL), DBPRIO |
  404. QID(q->cntxt_id) | PIDX(q->pend_cred / 8));
  405. q->pend_cred &= 7;
  406. }
  407. }
  408. static inline void set_rx_sw_desc(struct rx_sw_desc *sd, struct page *pg,
  409. dma_addr_t mapping)
  410. {
  411. sd->page = pg;
  412. sd->dma_addr = mapping; /* includes size low bits */
  413. }
  414. /**
  415. * refill_fl - refill an SGE Rx buffer ring
  416. * @adap: the adapter
  417. * @q: the ring to refill
  418. * @n: the number of new buffers to allocate
  419. * @gfp: the gfp flags for the allocations
  420. *
  421. * (Re)populate an SGE free-buffer queue with up to @n new packet buffers,
  422. * allocated with the supplied gfp flags. The caller must assure that
  423. * @n does not exceed the queue's capacity. If afterwards the queue is
  424. * found critically low mark it as starving in the bitmap of starving FLs.
  425. *
  426. * Returns the number of buffers allocated.
  427. */
  428. static unsigned int refill_fl(struct adapter *adap, struct sge_fl *q, int n,
  429. gfp_t gfp)
  430. {
  431. struct page *pg;
  432. dma_addr_t mapping;
  433. unsigned int cred = q->avail;
  434. __be64 *d = &q->desc[q->pidx];
  435. struct rx_sw_desc *sd = &q->sdesc[q->pidx];
  436. gfp |= __GFP_NOWARN; /* failures are expected */
  437. #if FL_PG_ORDER > 0
  438. /*
  439. * Prefer large buffers
  440. */
  441. while (n) {
  442. pg = alloc_pages(gfp | __GFP_COMP, FL_PG_ORDER);
  443. if (unlikely(!pg)) {
  444. q->large_alloc_failed++;
  445. break; /* fall back to single pages */
  446. }
  447. mapping = dma_map_page(adap->pdev_dev, pg, 0,
  448. PAGE_SIZE << FL_PG_ORDER,
  449. PCI_DMA_FROMDEVICE);
  450. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  451. __free_pages(pg, FL_PG_ORDER);
  452. goto out; /* do not try small pages for this error */
  453. }
  454. mapping |= RX_LARGE_BUF;
  455. *d++ = cpu_to_be64(mapping);
  456. set_rx_sw_desc(sd, pg, mapping);
  457. sd++;
  458. q->avail++;
  459. if (++q->pidx == q->size) {
  460. q->pidx = 0;
  461. sd = q->sdesc;
  462. d = q->desc;
  463. }
  464. n--;
  465. }
  466. #endif
  467. while (n--) {
  468. pg = __netdev_alloc_page(adap->port[0], gfp);
  469. if (unlikely(!pg)) {
  470. q->alloc_failed++;
  471. break;
  472. }
  473. mapping = dma_map_page(adap->pdev_dev, pg, 0, PAGE_SIZE,
  474. PCI_DMA_FROMDEVICE);
  475. if (unlikely(dma_mapping_error(adap->pdev_dev, mapping))) {
  476. netdev_free_page(adap->port[0], pg);
  477. goto out;
  478. }
  479. *d++ = cpu_to_be64(mapping);
  480. set_rx_sw_desc(sd, pg, mapping);
  481. sd++;
  482. q->avail++;
  483. if (++q->pidx == q->size) {
  484. q->pidx = 0;
  485. sd = q->sdesc;
  486. d = q->desc;
  487. }
  488. }
  489. out: cred = q->avail - cred;
  490. q->pend_cred += cred;
  491. ring_fl_db(adap, q);
  492. if (unlikely(fl_starving(q))) {
  493. smp_wmb();
  494. set_bit(q->cntxt_id - adap->sge.egr_start,
  495. adap->sge.starving_fl);
  496. }
  497. return cred;
  498. }
  499. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  500. {
  501. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  502. GFP_ATOMIC);
  503. }
  504. /**
  505. * alloc_ring - allocate resources for an SGE descriptor ring
  506. * @dev: the PCI device's core device
  507. * @nelem: the number of descriptors
  508. * @elem_size: the size of each descriptor
  509. * @sw_size: the size of the SW state associated with each ring element
  510. * @phys: the physical address of the allocated ring
  511. * @metadata: address of the array holding the SW state for the ring
  512. * @stat_size: extra space in HW ring for status information
  513. *
  514. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  515. * free buffer lists, or response queues. Each SGE ring requires
  516. * space for its HW descriptors plus, optionally, space for the SW state
  517. * associated with each HW entry (the metadata). The function returns
  518. * three values: the virtual address for the HW ring (the return value
  519. * of the function), the bus address of the HW ring, and the address
  520. * of the SW ring.
  521. */
  522. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  523. size_t sw_size, dma_addr_t *phys, void *metadata,
  524. size_t stat_size)
  525. {
  526. size_t len = nelem * elem_size + stat_size;
  527. void *s = NULL;
  528. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  529. if (!p)
  530. return NULL;
  531. if (sw_size) {
  532. s = kcalloc(nelem, sw_size, GFP_KERNEL);
  533. if (!s) {
  534. dma_free_coherent(dev, len, p, *phys);
  535. return NULL;
  536. }
  537. }
  538. if (metadata)
  539. *(void **)metadata = s;
  540. memset(p, 0, len);
  541. return p;
  542. }
  543. /**
  544. * sgl_len - calculates the size of an SGL of the given capacity
  545. * @n: the number of SGL entries
  546. *
  547. * Calculates the number of flits needed for a scatter/gather list that
  548. * can hold the given number of entries.
  549. */
  550. static inline unsigned int sgl_len(unsigned int n)
  551. {
  552. n--;
  553. return (3 * n) / 2 + (n & 1) + 2;
  554. }
  555. /**
  556. * flits_to_desc - returns the num of Tx descriptors for the given flits
  557. * @n: the number of flits
  558. *
  559. * Returns the number of Tx descriptors needed for the supplied number
  560. * of flits.
  561. */
  562. static inline unsigned int flits_to_desc(unsigned int n)
  563. {
  564. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  565. return DIV_ROUND_UP(n, 8);
  566. }
  567. /**
  568. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  569. * @skb: the packet
  570. *
  571. * Returns whether an Ethernet packet is small enough to fit as
  572. * immediate data.
  573. */
  574. static inline int is_eth_imm(const struct sk_buff *skb)
  575. {
  576. return skb->len <= MAX_IMM_TX_PKT_LEN - sizeof(struct cpl_tx_pkt);
  577. }
  578. /**
  579. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  580. * @skb: the packet
  581. *
  582. * Returns the number of flits needed for a Tx WR for the given Ethernet
  583. * packet, including the needed WR and CPL headers.
  584. */
  585. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  586. {
  587. unsigned int flits;
  588. if (is_eth_imm(skb))
  589. return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt), 8);
  590. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
  591. if (skb_shinfo(skb)->gso_size)
  592. flits += 2;
  593. return flits;
  594. }
  595. /**
  596. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  597. * @skb: the packet
  598. *
  599. * Returns the number of Tx descriptors needed for the given Ethernet
  600. * packet, including the needed WR and CPL headers.
  601. */
  602. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  603. {
  604. return flits_to_desc(calc_tx_flits(skb));
  605. }
  606. /**
  607. * write_sgl - populate a scatter/gather list for a packet
  608. * @skb: the packet
  609. * @q: the Tx queue we are writing into
  610. * @sgl: starting location for writing the SGL
  611. * @end: points right after the end of the SGL
  612. * @start: start offset into skb main-body data to include in the SGL
  613. * @addr: the list of bus addresses for the SGL elements
  614. *
  615. * Generates a gather list for the buffers that make up a packet.
  616. * The caller must provide adequate space for the SGL that will be written.
  617. * The SGL includes all of the packet's page fragments and the data in its
  618. * main body except for the first @start bytes. @sgl must be 16-byte
  619. * aligned and within a Tx descriptor with available space. @end points
  620. * right after the end of the SGL but does not account for any potential
  621. * wrap around, i.e., @end > @sgl.
  622. */
  623. static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  624. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  625. const dma_addr_t *addr)
  626. {
  627. unsigned int i, len;
  628. struct ulptx_sge_pair *to;
  629. const struct skb_shared_info *si = skb_shinfo(skb);
  630. unsigned int nfrags = si->nr_frags;
  631. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  632. len = skb_headlen(skb) - start;
  633. if (likely(len)) {
  634. sgl->len0 = htonl(len);
  635. sgl->addr0 = cpu_to_be64(addr[0] + start);
  636. nfrags++;
  637. } else {
  638. sgl->len0 = htonl(si->frags[0].size);
  639. sgl->addr0 = cpu_to_be64(addr[1]);
  640. }
  641. sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
  642. if (likely(--nfrags == 0))
  643. return;
  644. /*
  645. * Most of the complexity below deals with the possibility we hit the
  646. * end of the queue in the middle of writing the SGL. For this case
  647. * only we create the SGL in a temporary buffer and then copy it.
  648. */
  649. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  650. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  651. to->len[0] = cpu_to_be32(si->frags[i].size);
  652. to->len[1] = cpu_to_be32(si->frags[++i].size);
  653. to->addr[0] = cpu_to_be64(addr[i]);
  654. to->addr[1] = cpu_to_be64(addr[++i]);
  655. }
  656. if (nfrags) {
  657. to->len[0] = cpu_to_be32(si->frags[i].size);
  658. to->len[1] = cpu_to_be32(0);
  659. to->addr[0] = cpu_to_be64(addr[i + 1]);
  660. }
  661. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  662. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  663. if (likely(part0))
  664. memcpy(sgl->sge, buf, part0);
  665. part1 = (u8 *)end - (u8 *)q->stat;
  666. memcpy(q->desc, (u8 *)buf + part0, part1);
  667. end = (void *)q->desc + part1;
  668. }
  669. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  670. *(u64 *)end = 0;
  671. }
  672. /**
  673. * ring_tx_db - check and potentially ring a Tx queue's doorbell
  674. * @adap: the adapter
  675. * @q: the Tx queue
  676. * @n: number of new descriptors to give to HW
  677. *
  678. * Ring the doorbel for a Tx queue.
  679. */
  680. static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  681. {
  682. wmb(); /* write descriptors before telling HW */
  683. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
  684. QID(q->cntxt_id) | PIDX(n));
  685. }
  686. /**
  687. * inline_tx_skb - inline a packet's data into Tx descriptors
  688. * @skb: the packet
  689. * @q: the Tx queue where the packet will be inlined
  690. * @pos: starting position in the Tx queue where to inline the packet
  691. *
  692. * Inline a packet's contents directly into Tx descriptors, starting at
  693. * the given position within the Tx DMA ring.
  694. * Most of the complexity of this operation is dealing with wrap arounds
  695. * in the middle of the packet we want to inline.
  696. */
  697. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
  698. void *pos)
  699. {
  700. u64 *p;
  701. int left = (void *)q->stat - pos;
  702. if (likely(skb->len <= left)) {
  703. if (likely(!skb->data_len))
  704. skb_copy_from_linear_data(skb, pos, skb->len);
  705. else
  706. skb_copy_bits(skb, 0, pos, skb->len);
  707. pos += skb->len;
  708. } else {
  709. skb_copy_bits(skb, 0, pos, left);
  710. skb_copy_bits(skb, left, q->desc, skb->len - left);
  711. pos = (void *)q->desc + (skb->len - left);
  712. }
  713. /* 0-pad to multiple of 16 */
  714. p = PTR_ALIGN(pos, 8);
  715. if ((uintptr_t)p & 8)
  716. *p = 0;
  717. }
  718. /*
  719. * Figure out what HW csum a packet wants and return the appropriate control
  720. * bits.
  721. */
  722. static u64 hwcsum(const struct sk_buff *skb)
  723. {
  724. int csum_type;
  725. const struct iphdr *iph = ip_hdr(skb);
  726. if (iph->version == 4) {
  727. if (iph->protocol == IPPROTO_TCP)
  728. csum_type = TX_CSUM_TCPIP;
  729. else if (iph->protocol == IPPROTO_UDP)
  730. csum_type = TX_CSUM_UDPIP;
  731. else {
  732. nocsum: /*
  733. * unknown protocol, disable HW csum
  734. * and hope a bad packet is detected
  735. */
  736. return TXPKT_L4CSUM_DIS;
  737. }
  738. } else {
  739. /*
  740. * this doesn't work with extension headers
  741. */
  742. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  743. if (ip6h->nexthdr == IPPROTO_TCP)
  744. csum_type = TX_CSUM_TCPIP6;
  745. else if (ip6h->nexthdr == IPPROTO_UDP)
  746. csum_type = TX_CSUM_UDPIP6;
  747. else
  748. goto nocsum;
  749. }
  750. if (likely(csum_type >= TX_CSUM_TCPIP))
  751. return TXPKT_CSUM_TYPE(csum_type) |
  752. TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
  753. TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
  754. else {
  755. int start = skb_transport_offset(skb);
  756. return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
  757. TXPKT_CSUM_LOC(start + skb->csum_offset);
  758. }
  759. }
  760. static void eth_txq_stop(struct sge_eth_txq *q)
  761. {
  762. netif_tx_stop_queue(q->txq);
  763. q->q.stops++;
  764. }
  765. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  766. {
  767. q->in_use += n;
  768. q->pidx += n;
  769. if (q->pidx >= q->size)
  770. q->pidx -= q->size;
  771. }
  772. /**
  773. * t4_eth_xmit - add a packet to an Ethernet Tx queue
  774. * @skb: the packet
  775. * @dev: the egress net device
  776. *
  777. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  778. */
  779. netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  780. {
  781. u32 wr_mid;
  782. u64 cntrl, *end;
  783. int qidx, credits;
  784. unsigned int flits, ndesc;
  785. struct adapter *adap;
  786. struct sge_eth_txq *q;
  787. const struct port_info *pi;
  788. struct fw_eth_tx_pkt_wr *wr;
  789. struct cpl_tx_pkt_core *cpl;
  790. const struct skb_shared_info *ssi;
  791. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  792. /*
  793. * The chip min packet length is 10 octets but play safe and reject
  794. * anything shorter than an Ethernet header.
  795. */
  796. if (unlikely(skb->len < ETH_HLEN)) {
  797. out_free: dev_kfree_skb(skb);
  798. return NETDEV_TX_OK;
  799. }
  800. pi = netdev_priv(dev);
  801. adap = pi->adapter;
  802. qidx = skb_get_queue_mapping(skb);
  803. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  804. reclaim_completed_tx(adap, &q->q, true);
  805. flits = calc_tx_flits(skb);
  806. ndesc = flits_to_desc(flits);
  807. credits = txq_avail(&q->q) - ndesc;
  808. if (unlikely(credits < 0)) {
  809. eth_txq_stop(q);
  810. dev_err(adap->pdev_dev,
  811. "%s: Tx ring %u full while queue awake!\n",
  812. dev->name, qidx);
  813. return NETDEV_TX_BUSY;
  814. }
  815. if (!is_eth_imm(skb) &&
  816. unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
  817. q->mapping_err++;
  818. goto out_free;
  819. }
  820. wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
  821. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  822. eth_txq_stop(q);
  823. wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
  824. }
  825. wr = (void *)&q->q.desc[q->q.pidx];
  826. wr->equiq_to_len16 = htonl(wr_mid);
  827. wr->r3 = cpu_to_be64(0);
  828. end = (u64 *)wr + flits;
  829. ssi = skb_shinfo(skb);
  830. if (ssi->gso_size) {
  831. struct cpl_tx_pkt_lso *lso = (void *)wr;
  832. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  833. int l3hdr_len = skb_network_header_len(skb);
  834. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  835. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  836. FW_WR_IMMDLEN(sizeof(*lso)));
  837. lso->c.lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
  838. LSO_FIRST_SLICE | LSO_LAST_SLICE |
  839. LSO_IPV6(v6) |
  840. LSO_ETHHDR_LEN(eth_xtra_len / 4) |
  841. LSO_IPHDR_LEN(l3hdr_len / 4) |
  842. LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
  843. lso->c.ipid_ofst = htons(0);
  844. lso->c.mss = htons(ssi->gso_size);
  845. lso->c.seqno_offset = htonl(0);
  846. lso->c.len = htonl(skb->len);
  847. cpl = (void *)(lso + 1);
  848. cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  849. TXPKT_IPHDR_LEN(l3hdr_len) |
  850. TXPKT_ETHHDR_LEN(eth_xtra_len);
  851. q->tso++;
  852. q->tx_cso += ssi->gso_segs;
  853. } else {
  854. int len;
  855. len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
  856. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  857. FW_WR_IMMDLEN(len));
  858. cpl = (void *)(wr + 1);
  859. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  860. cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
  861. q->tx_cso++;
  862. } else
  863. cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
  864. }
  865. if (vlan_tx_tag_present(skb)) {
  866. q->vlan_ins++;
  867. cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
  868. }
  869. cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
  870. TXPKT_INTF(pi->tx_chan) | TXPKT_PF(adap->fn));
  871. cpl->pack = htons(0);
  872. cpl->len = htons(skb->len);
  873. cpl->ctrl1 = cpu_to_be64(cntrl);
  874. if (is_eth_imm(skb)) {
  875. inline_tx_skb(skb, &q->q, cpl + 1);
  876. dev_kfree_skb(skb);
  877. } else {
  878. int last_desc;
  879. write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
  880. addr);
  881. skb_orphan(skb);
  882. last_desc = q->q.pidx + ndesc - 1;
  883. if (last_desc >= q->q.size)
  884. last_desc -= q->q.size;
  885. q->q.sdesc[last_desc].skb = skb;
  886. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
  887. }
  888. txq_advance(&q->q, ndesc);
  889. ring_tx_db(adap, &q->q, ndesc);
  890. return NETDEV_TX_OK;
  891. }
  892. /**
  893. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  894. * @q: the SGE control Tx queue
  895. *
  896. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  897. * that send only immediate data (presently just the control queues) and
  898. * thus do not have any sk_buffs to release.
  899. */
  900. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  901. {
  902. int hw_cidx = ntohs(q->stat->cidx);
  903. int reclaim = hw_cidx - q->cidx;
  904. if (reclaim < 0)
  905. reclaim += q->size;
  906. q->in_use -= reclaim;
  907. q->cidx = hw_cidx;
  908. }
  909. /**
  910. * is_imm - check whether a packet can be sent as immediate data
  911. * @skb: the packet
  912. *
  913. * Returns true if a packet can be sent as a WR with immediate data.
  914. */
  915. static inline int is_imm(const struct sk_buff *skb)
  916. {
  917. return skb->len <= MAX_CTRL_WR_LEN;
  918. }
  919. /**
  920. * ctrlq_check_stop - check if a control queue is full and should stop
  921. * @q: the queue
  922. * @wr: most recent WR written to the queue
  923. *
  924. * Check if a control queue has become full and should be stopped.
  925. * We clean up control queue descriptors very lazily, only when we are out.
  926. * If the queue is still full after reclaiming any completed descriptors
  927. * we suspend it and have the last WR wake it up.
  928. */
  929. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  930. {
  931. reclaim_completed_tx_imm(&q->q);
  932. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  933. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  934. q->q.stops++;
  935. q->full = 1;
  936. }
  937. }
  938. /**
  939. * ctrl_xmit - send a packet through an SGE control Tx queue
  940. * @q: the control queue
  941. * @skb: the packet
  942. *
  943. * Send a packet through an SGE control Tx queue. Packets sent through
  944. * a control queue must fit entirely as immediate data.
  945. */
  946. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  947. {
  948. unsigned int ndesc;
  949. struct fw_wr_hdr *wr;
  950. if (unlikely(!is_imm(skb))) {
  951. WARN_ON(1);
  952. dev_kfree_skb(skb);
  953. return NET_XMIT_DROP;
  954. }
  955. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  956. spin_lock(&q->sendq.lock);
  957. if (unlikely(q->full)) {
  958. skb->priority = ndesc; /* save for restart */
  959. __skb_queue_tail(&q->sendq, skb);
  960. spin_unlock(&q->sendq.lock);
  961. return NET_XMIT_CN;
  962. }
  963. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  964. inline_tx_skb(skb, &q->q, wr);
  965. txq_advance(&q->q, ndesc);
  966. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  967. ctrlq_check_stop(q, wr);
  968. ring_tx_db(q->adap, &q->q, ndesc);
  969. spin_unlock(&q->sendq.lock);
  970. kfree_skb(skb);
  971. return NET_XMIT_SUCCESS;
  972. }
  973. /**
  974. * restart_ctrlq - restart a suspended control queue
  975. * @data: the control queue to restart
  976. *
  977. * Resumes transmission on a suspended Tx control queue.
  978. */
  979. static void restart_ctrlq(unsigned long data)
  980. {
  981. struct sk_buff *skb;
  982. unsigned int written = 0;
  983. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  984. spin_lock(&q->sendq.lock);
  985. reclaim_completed_tx_imm(&q->q);
  986. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  987. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  988. struct fw_wr_hdr *wr;
  989. unsigned int ndesc = skb->priority; /* previously saved */
  990. /*
  991. * Write descriptors and free skbs outside the lock to limit
  992. * wait times. q->full is still set so new skbs will be queued.
  993. */
  994. spin_unlock(&q->sendq.lock);
  995. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  996. inline_tx_skb(skb, &q->q, wr);
  997. kfree_skb(skb);
  998. written += ndesc;
  999. txq_advance(&q->q, ndesc);
  1000. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1001. unsigned long old = q->q.stops;
  1002. ctrlq_check_stop(q, wr);
  1003. if (q->q.stops != old) { /* suspended anew */
  1004. spin_lock(&q->sendq.lock);
  1005. goto ringdb;
  1006. }
  1007. }
  1008. if (written > 16) {
  1009. ring_tx_db(q->adap, &q->q, written);
  1010. written = 0;
  1011. }
  1012. spin_lock(&q->sendq.lock);
  1013. }
  1014. q->full = 0;
  1015. ringdb: if (written)
  1016. ring_tx_db(q->adap, &q->q, written);
  1017. spin_unlock(&q->sendq.lock);
  1018. }
  1019. /**
  1020. * t4_mgmt_tx - send a management message
  1021. * @adap: the adapter
  1022. * @skb: the packet containing the management message
  1023. *
  1024. * Send a management message through control queue 0.
  1025. */
  1026. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1027. {
  1028. int ret;
  1029. local_bh_disable();
  1030. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1031. local_bh_enable();
  1032. return ret;
  1033. }
  1034. /**
  1035. * is_ofld_imm - check whether a packet can be sent as immediate data
  1036. * @skb: the packet
  1037. *
  1038. * Returns true if a packet can be sent as an offload WR with immediate
  1039. * data. We currently use the same limit as for Ethernet packets.
  1040. */
  1041. static inline int is_ofld_imm(const struct sk_buff *skb)
  1042. {
  1043. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1044. }
  1045. /**
  1046. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1047. * @skb: the packet
  1048. *
  1049. * Returns the number of flits needed for the given offload packet.
  1050. * These packets are already fully constructed and no additional headers
  1051. * will be added.
  1052. */
  1053. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1054. {
  1055. unsigned int flits, cnt;
  1056. if (is_ofld_imm(skb))
  1057. return DIV_ROUND_UP(skb->len, 8);
  1058. flits = skb_transport_offset(skb) / 8U; /* headers */
  1059. cnt = skb_shinfo(skb)->nr_frags;
  1060. if (skb->tail != skb->transport_header)
  1061. cnt++;
  1062. return flits + sgl_len(cnt);
  1063. }
  1064. /**
  1065. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1066. * @adap: the adapter
  1067. * @q: the queue to stop
  1068. *
  1069. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1070. * inability to map packets. A periodic timer attempts to restart
  1071. * queues so marked.
  1072. */
  1073. static void txq_stop_maperr(struct sge_ofld_txq *q)
  1074. {
  1075. q->mapping_err++;
  1076. q->q.stops++;
  1077. set_bit(q->q.cntxt_id - q->adap->sge.egr_start,
  1078. q->adap->sge.txq_maperr);
  1079. }
  1080. /**
  1081. * ofldtxq_stop - stop an offload Tx queue that has become full
  1082. * @q: the queue to stop
  1083. * @skb: the packet causing the queue to become full
  1084. *
  1085. * Stops an offload Tx queue that has become full and modifies the packet
  1086. * being written to request a wakeup.
  1087. */
  1088. static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
  1089. {
  1090. struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
  1091. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1092. q->q.stops++;
  1093. q->full = 1;
  1094. }
  1095. /**
  1096. * service_ofldq - restart a suspended offload queue
  1097. * @q: the offload queue
  1098. *
  1099. * Services an offload Tx queue by moving packets from its packet queue
  1100. * to the HW Tx ring. The function starts and ends with the queue locked.
  1101. */
  1102. static void service_ofldq(struct sge_ofld_txq *q)
  1103. {
  1104. u64 *pos;
  1105. int credits;
  1106. struct sk_buff *skb;
  1107. unsigned int written = 0;
  1108. unsigned int flits, ndesc;
  1109. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1110. /*
  1111. * We drop the lock but leave skb on sendq, thus retaining
  1112. * exclusive access to the state of the queue.
  1113. */
  1114. spin_unlock(&q->sendq.lock);
  1115. reclaim_completed_tx(q->adap, &q->q, false);
  1116. flits = skb->priority; /* previously saved */
  1117. ndesc = flits_to_desc(flits);
  1118. credits = txq_avail(&q->q) - ndesc;
  1119. BUG_ON(credits < 0);
  1120. if (unlikely(credits < TXQ_STOP_THRES))
  1121. ofldtxq_stop(q, skb);
  1122. pos = (u64 *)&q->q.desc[q->q.pidx];
  1123. if (is_ofld_imm(skb))
  1124. inline_tx_skb(skb, &q->q, pos);
  1125. else if (map_skb(q->adap->pdev_dev, skb,
  1126. (dma_addr_t *)skb->head)) {
  1127. txq_stop_maperr(q);
  1128. spin_lock(&q->sendq.lock);
  1129. break;
  1130. } else {
  1131. int last_desc, hdr_len = skb_transport_offset(skb);
  1132. memcpy(pos, skb->data, hdr_len);
  1133. write_sgl(skb, &q->q, (void *)pos + hdr_len,
  1134. pos + flits, hdr_len,
  1135. (dma_addr_t *)skb->head);
  1136. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1137. skb->dev = q->adap->port[0];
  1138. skb->destructor = deferred_unmap_destructor;
  1139. #endif
  1140. last_desc = q->q.pidx + ndesc - 1;
  1141. if (last_desc >= q->q.size)
  1142. last_desc -= q->q.size;
  1143. q->q.sdesc[last_desc].skb = skb;
  1144. }
  1145. txq_advance(&q->q, ndesc);
  1146. written += ndesc;
  1147. if (unlikely(written > 32)) {
  1148. ring_tx_db(q->adap, &q->q, written);
  1149. written = 0;
  1150. }
  1151. spin_lock(&q->sendq.lock);
  1152. __skb_unlink(skb, &q->sendq);
  1153. if (is_ofld_imm(skb))
  1154. kfree_skb(skb);
  1155. }
  1156. if (likely(written))
  1157. ring_tx_db(q->adap, &q->q, written);
  1158. }
  1159. /**
  1160. * ofld_xmit - send a packet through an offload queue
  1161. * @q: the Tx offload queue
  1162. * @skb: the packet
  1163. *
  1164. * Send an offload packet through an SGE offload queue.
  1165. */
  1166. static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
  1167. {
  1168. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1169. spin_lock(&q->sendq.lock);
  1170. __skb_queue_tail(&q->sendq, skb);
  1171. if (q->sendq.qlen == 1)
  1172. service_ofldq(q);
  1173. spin_unlock(&q->sendq.lock);
  1174. return NET_XMIT_SUCCESS;
  1175. }
  1176. /**
  1177. * restart_ofldq - restart a suspended offload queue
  1178. * @data: the offload queue to restart
  1179. *
  1180. * Resumes transmission on a suspended Tx offload queue.
  1181. */
  1182. static void restart_ofldq(unsigned long data)
  1183. {
  1184. struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
  1185. spin_lock(&q->sendq.lock);
  1186. q->full = 0; /* the queue actually is completely empty now */
  1187. service_ofldq(q);
  1188. spin_unlock(&q->sendq.lock);
  1189. }
  1190. /**
  1191. * skb_txq - return the Tx queue an offload packet should use
  1192. * @skb: the packet
  1193. *
  1194. * Returns the Tx queue an offload packet should use as indicated by bits
  1195. * 1-15 in the packet's queue_mapping.
  1196. */
  1197. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1198. {
  1199. return skb->queue_mapping >> 1;
  1200. }
  1201. /**
  1202. * is_ctrl_pkt - return whether an offload packet is a control packet
  1203. * @skb: the packet
  1204. *
  1205. * Returns whether an offload packet should use an OFLD or a CTRL
  1206. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1207. */
  1208. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1209. {
  1210. return skb->queue_mapping & 1;
  1211. }
  1212. static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
  1213. {
  1214. unsigned int idx = skb_txq(skb);
  1215. if (unlikely(is_ctrl_pkt(skb)))
  1216. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1217. return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
  1218. }
  1219. /**
  1220. * t4_ofld_send - send an offload packet
  1221. * @adap: the adapter
  1222. * @skb: the packet
  1223. *
  1224. * Sends an offload packet. We use the packet queue_mapping to select the
  1225. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1226. * should be sent as regular or control, bits 1-15 select the queue.
  1227. */
  1228. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1229. {
  1230. int ret;
  1231. local_bh_disable();
  1232. ret = ofld_send(adap, skb);
  1233. local_bh_enable();
  1234. return ret;
  1235. }
  1236. /**
  1237. * cxgb4_ofld_send - send an offload packet
  1238. * @dev: the net device
  1239. * @skb: the packet
  1240. *
  1241. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1242. * intended for ULDs.
  1243. */
  1244. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1245. {
  1246. return t4_ofld_send(netdev2adap(dev), skb);
  1247. }
  1248. EXPORT_SYMBOL(cxgb4_ofld_send);
  1249. static inline void copy_frags(struct skb_shared_info *ssi,
  1250. const struct pkt_gl *gl, unsigned int offset)
  1251. {
  1252. unsigned int n;
  1253. /* usually there's just one frag */
  1254. ssi->frags[0].page = gl->frags[0].page;
  1255. ssi->frags[0].page_offset = gl->frags[0].page_offset + offset;
  1256. ssi->frags[0].size = gl->frags[0].size - offset;
  1257. ssi->nr_frags = gl->nfrags;
  1258. n = gl->nfrags - 1;
  1259. if (n)
  1260. memcpy(&ssi->frags[1], &gl->frags[1], n * sizeof(skb_frag_t));
  1261. /* get a reference to the last page, we don't own it */
  1262. get_page(gl->frags[n].page);
  1263. }
  1264. /**
  1265. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1266. * @gl: the gather list
  1267. * @skb_len: size of sk_buff main body if it carries fragments
  1268. * @pull_len: amount of data to move to the sk_buff's main body
  1269. *
  1270. * Builds an sk_buff from the given packet gather list. Returns the
  1271. * sk_buff or %NULL if sk_buff allocation failed.
  1272. */
  1273. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1274. unsigned int skb_len, unsigned int pull_len)
  1275. {
  1276. struct sk_buff *skb;
  1277. /*
  1278. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1279. * size, which is expected since buffers are at least PAGE_SIZEd.
  1280. * In this case packets up to RX_COPY_THRES have only one fragment.
  1281. */
  1282. if (gl->tot_len <= RX_COPY_THRES) {
  1283. skb = dev_alloc_skb(gl->tot_len);
  1284. if (unlikely(!skb))
  1285. goto out;
  1286. __skb_put(skb, gl->tot_len);
  1287. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1288. } else {
  1289. skb = dev_alloc_skb(skb_len);
  1290. if (unlikely(!skb))
  1291. goto out;
  1292. __skb_put(skb, pull_len);
  1293. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1294. copy_frags(skb_shinfo(skb), gl, pull_len);
  1295. skb->len = gl->tot_len;
  1296. skb->data_len = skb->len - pull_len;
  1297. skb->truesize += skb->data_len;
  1298. }
  1299. out: return skb;
  1300. }
  1301. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1302. /**
  1303. * t4_pktgl_free - free a packet gather list
  1304. * @gl: the gather list
  1305. *
  1306. * Releases the pages of a packet gather list. We do not own the last
  1307. * page on the list and do not free it.
  1308. */
  1309. static void t4_pktgl_free(const struct pkt_gl *gl)
  1310. {
  1311. int n;
  1312. const skb_frag_t *p;
  1313. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1314. put_page(p->page);
  1315. }
  1316. /*
  1317. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1318. * be delivered to anyone and send it to the stack for capture.
  1319. */
  1320. static noinline int handle_trace_pkt(struct adapter *adap,
  1321. const struct pkt_gl *gl)
  1322. {
  1323. struct sk_buff *skb;
  1324. struct cpl_trace_pkt *p;
  1325. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  1326. if (unlikely(!skb)) {
  1327. t4_pktgl_free(gl);
  1328. return 0;
  1329. }
  1330. p = (struct cpl_trace_pkt *)skb->data;
  1331. __skb_pull(skb, sizeof(*p));
  1332. skb_reset_mac_header(skb);
  1333. skb->protocol = htons(0xffff);
  1334. skb->dev = adap->port[0];
  1335. netif_receive_skb(skb);
  1336. return 0;
  1337. }
  1338. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1339. const struct cpl_rx_pkt *pkt)
  1340. {
  1341. int ret;
  1342. struct sk_buff *skb;
  1343. skb = napi_get_frags(&rxq->rspq.napi);
  1344. if (unlikely(!skb)) {
  1345. t4_pktgl_free(gl);
  1346. rxq->stats.rx_drops++;
  1347. return;
  1348. }
  1349. copy_frags(skb_shinfo(skb), gl, RX_PKT_PAD);
  1350. skb->len = gl->tot_len - RX_PKT_PAD;
  1351. skb->data_len = skb->len;
  1352. skb->truesize += skb->data_len;
  1353. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1354. skb_record_rx_queue(skb, rxq->rspq.idx);
  1355. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  1356. skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
  1357. if (unlikely(pkt->vlan_ex)) {
  1358. struct port_info *pi = netdev_priv(rxq->rspq.netdev);
  1359. struct vlan_group *grp = pi->vlan_grp;
  1360. rxq->stats.vlan_ex++;
  1361. if (likely(grp)) {
  1362. ret = vlan_gro_frags(&rxq->rspq.napi, grp,
  1363. ntohs(pkt->vlan));
  1364. goto stats;
  1365. }
  1366. }
  1367. ret = napi_gro_frags(&rxq->rspq.napi);
  1368. stats: if (ret == GRO_HELD)
  1369. rxq->stats.lro_pkts++;
  1370. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1371. rxq->stats.lro_merged++;
  1372. rxq->stats.pkts++;
  1373. rxq->stats.rx_cso++;
  1374. }
  1375. /**
  1376. * t4_ethrx_handler - process an ingress ethernet packet
  1377. * @q: the response queue that received the packet
  1378. * @rsp: the response queue descriptor holding the RX_PKT message
  1379. * @si: the gather list of packet fragments
  1380. *
  1381. * Process an ingress ethernet packet and deliver it to the stack.
  1382. */
  1383. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  1384. const struct pkt_gl *si)
  1385. {
  1386. bool csum_ok;
  1387. struct sk_buff *skb;
  1388. struct port_info *pi;
  1389. const struct cpl_rx_pkt *pkt;
  1390. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1391. if (unlikely(*(u8 *)rsp == CPL_TRACE_PKT))
  1392. return handle_trace_pkt(q->adap, si);
  1393. pkt = (const struct cpl_rx_pkt *)rsp;
  1394. csum_ok = pkt->csum_calc && !pkt->err_vec;
  1395. if ((pkt->l2info & htonl(RXF_TCP)) &&
  1396. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  1397. do_gro(rxq, si, pkt);
  1398. return 0;
  1399. }
  1400. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  1401. if (unlikely(!skb)) {
  1402. t4_pktgl_free(si);
  1403. rxq->stats.rx_drops++;
  1404. return 0;
  1405. }
  1406. __skb_pull(skb, RX_PKT_PAD); /* remove ethernet header padding */
  1407. skb->protocol = eth_type_trans(skb, q->netdev);
  1408. skb_record_rx_queue(skb, q->idx);
  1409. if (skb->dev->features & NETIF_F_RXHASH)
  1410. skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
  1411. pi = netdev_priv(skb->dev);
  1412. rxq->stats.pkts++;
  1413. if (csum_ok && (pi->rx_offload & RX_CSO) &&
  1414. (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
  1415. if (!pkt->ip_frag) {
  1416. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1417. rxq->stats.rx_cso++;
  1418. } else if (pkt->l2info & htonl(RXF_IP)) {
  1419. __sum16 c = (__force __sum16)pkt->csum;
  1420. skb->csum = csum_unfold(c);
  1421. skb->ip_summed = CHECKSUM_COMPLETE;
  1422. rxq->stats.rx_cso++;
  1423. }
  1424. } else
  1425. skb_checksum_none_assert(skb);
  1426. if (unlikely(pkt->vlan_ex)) {
  1427. struct vlan_group *grp = pi->vlan_grp;
  1428. rxq->stats.vlan_ex++;
  1429. if (likely(grp))
  1430. vlan_hwaccel_receive_skb(skb, grp, ntohs(pkt->vlan));
  1431. else
  1432. dev_kfree_skb_any(skb);
  1433. } else
  1434. netif_receive_skb(skb);
  1435. return 0;
  1436. }
  1437. /**
  1438. * restore_rx_bufs - put back a packet's Rx buffers
  1439. * @si: the packet gather list
  1440. * @q: the SGE free list
  1441. * @frags: number of FL buffers to restore
  1442. *
  1443. * Puts back on an FL the Rx buffers associated with @si. The buffers
  1444. * have already been unmapped and are left unmapped, we mark them so to
  1445. * prevent further unmapping attempts.
  1446. *
  1447. * This function undoes a series of @unmap_rx_buf calls when we find out
  1448. * that the current packet can't be processed right away afterall and we
  1449. * need to come back to it later. This is a very rare event and there's
  1450. * no effort to make this particularly efficient.
  1451. */
  1452. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  1453. int frags)
  1454. {
  1455. struct rx_sw_desc *d;
  1456. while (frags--) {
  1457. if (q->cidx == 0)
  1458. q->cidx = q->size - 1;
  1459. else
  1460. q->cidx--;
  1461. d = &q->sdesc[q->cidx];
  1462. d->page = si->frags[frags].page;
  1463. d->dma_addr |= RX_UNMAPPED_BUF;
  1464. q->avail++;
  1465. }
  1466. }
  1467. /**
  1468. * is_new_response - check if a response is newly written
  1469. * @r: the response descriptor
  1470. * @q: the response queue
  1471. *
  1472. * Returns true if a response descriptor contains a yet unprocessed
  1473. * response.
  1474. */
  1475. static inline bool is_new_response(const struct rsp_ctrl *r,
  1476. const struct sge_rspq *q)
  1477. {
  1478. return RSPD_GEN(r->type_gen) == q->gen;
  1479. }
  1480. /**
  1481. * rspq_next - advance to the next entry in a response queue
  1482. * @q: the queue
  1483. *
  1484. * Updates the state of a response queue to advance it to the next entry.
  1485. */
  1486. static inline void rspq_next(struct sge_rspq *q)
  1487. {
  1488. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  1489. if (unlikely(++q->cidx == q->size)) {
  1490. q->cidx = 0;
  1491. q->gen ^= 1;
  1492. q->cur_desc = q->desc;
  1493. }
  1494. }
  1495. /**
  1496. * process_responses - process responses from an SGE response queue
  1497. * @q: the ingress queue to process
  1498. * @budget: how many responses can be processed in this round
  1499. *
  1500. * Process responses from an SGE response queue up to the supplied budget.
  1501. * Responses include received packets as well as control messages from FW
  1502. * or HW.
  1503. *
  1504. * Additionally choose the interrupt holdoff time for the next interrupt
  1505. * on this queue. If the system is under memory shortage use a fairly
  1506. * long delay to help recovery.
  1507. */
  1508. static int process_responses(struct sge_rspq *q, int budget)
  1509. {
  1510. int ret, rsp_type;
  1511. int budget_left = budget;
  1512. const struct rsp_ctrl *rc;
  1513. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1514. while (likely(budget_left)) {
  1515. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1516. if (!is_new_response(rc, q))
  1517. break;
  1518. rmb();
  1519. rsp_type = RSPD_TYPE(rc->type_gen);
  1520. if (likely(rsp_type == RSP_TYPE_FLBUF)) {
  1521. skb_frag_t *fp;
  1522. struct pkt_gl si;
  1523. const struct rx_sw_desc *rsd;
  1524. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  1525. if (len & RSPD_NEWBUF) {
  1526. if (likely(q->offset > 0)) {
  1527. free_rx_bufs(q->adap, &rxq->fl, 1);
  1528. q->offset = 0;
  1529. }
  1530. len = RSPD_LEN(len);
  1531. }
  1532. si.tot_len = len;
  1533. /* gather packet fragments */
  1534. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  1535. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  1536. bufsz = get_buf_size(rsd);
  1537. fp->page = rsd->page;
  1538. fp->page_offset = q->offset;
  1539. fp->size = min(bufsz, len);
  1540. len -= fp->size;
  1541. if (!len)
  1542. break;
  1543. unmap_rx_buf(q->adap, &rxq->fl);
  1544. }
  1545. /*
  1546. * Last buffer remains mapped so explicitly make it
  1547. * coherent for CPU access.
  1548. */
  1549. dma_sync_single_for_cpu(q->adap->pdev_dev,
  1550. get_buf_addr(rsd),
  1551. fp->size, DMA_FROM_DEVICE);
  1552. si.va = page_address(si.frags[0].page) +
  1553. si.frags[0].page_offset;
  1554. prefetch(si.va);
  1555. si.nfrags = frags + 1;
  1556. ret = q->handler(q, q->cur_desc, &si);
  1557. if (likely(ret == 0))
  1558. q->offset += ALIGN(fp->size, FL_ALIGN);
  1559. else
  1560. restore_rx_bufs(&si, &rxq->fl, frags);
  1561. } else if (likely(rsp_type == RSP_TYPE_CPL)) {
  1562. ret = q->handler(q, q->cur_desc, NULL);
  1563. } else {
  1564. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  1565. }
  1566. if (unlikely(ret)) {
  1567. /* couldn't process descriptor, back off for recovery */
  1568. q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
  1569. break;
  1570. }
  1571. rspq_next(q);
  1572. budget_left--;
  1573. }
  1574. if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
  1575. __refill_fl(q->adap, &rxq->fl);
  1576. return budget - budget_left;
  1577. }
  1578. /**
  1579. * napi_rx_handler - the NAPI handler for Rx processing
  1580. * @napi: the napi instance
  1581. * @budget: how many packets we can process in this round
  1582. *
  1583. * Handler for new data events when using NAPI. This does not need any
  1584. * locking or protection from interrupts as data interrupts are off at
  1585. * this point and other adapter interrupts do not interfere (the latter
  1586. * in not a concern at all with MSI-X as non-data interrupts then have
  1587. * a separate handler).
  1588. */
  1589. static int napi_rx_handler(struct napi_struct *napi, int budget)
  1590. {
  1591. unsigned int params;
  1592. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  1593. int work_done = process_responses(q, budget);
  1594. if (likely(work_done < budget)) {
  1595. napi_complete(napi);
  1596. params = q->next_intr_params;
  1597. q->next_intr_params = q->intr_params;
  1598. } else
  1599. params = QINTR_TIMER_IDX(7);
  1600. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS), CIDXINC(work_done) |
  1601. INGRESSQID((u32)q->cntxt_id) | SEINTARM(params));
  1602. return work_done;
  1603. }
  1604. /*
  1605. * The MSI-X interrupt handler for an SGE response queue.
  1606. */
  1607. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  1608. {
  1609. struct sge_rspq *q = cookie;
  1610. napi_schedule(&q->napi);
  1611. return IRQ_HANDLED;
  1612. }
  1613. /*
  1614. * Process the indirect interrupt entries in the interrupt queue and kick off
  1615. * NAPI for each queue that has generated an entry.
  1616. */
  1617. static unsigned int process_intrq(struct adapter *adap)
  1618. {
  1619. unsigned int credits;
  1620. const struct rsp_ctrl *rc;
  1621. struct sge_rspq *q = &adap->sge.intrq;
  1622. spin_lock(&adap->sge.intrq_lock);
  1623. for (credits = 0; ; credits++) {
  1624. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1625. if (!is_new_response(rc, q))
  1626. break;
  1627. rmb();
  1628. if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
  1629. unsigned int qid = ntohl(rc->pldbuflen_qid);
  1630. qid -= adap->sge.ingr_start;
  1631. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  1632. }
  1633. rspq_next(q);
  1634. }
  1635. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS), CIDXINC(credits) |
  1636. INGRESSQID(q->cntxt_id) | SEINTARM(q->intr_params));
  1637. spin_unlock(&adap->sge.intrq_lock);
  1638. return credits;
  1639. }
  1640. /*
  1641. * The MSI interrupt handler, which handles data events from SGE response queues
  1642. * as well as error and other async events as they all use the same MSI vector.
  1643. */
  1644. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  1645. {
  1646. struct adapter *adap = cookie;
  1647. t4_slow_intr_handler(adap);
  1648. process_intrq(adap);
  1649. return IRQ_HANDLED;
  1650. }
  1651. /*
  1652. * Interrupt handler for legacy INTx interrupts.
  1653. * Handles data events from SGE response queues as well as error and other
  1654. * async events as they all use the same interrupt line.
  1655. */
  1656. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  1657. {
  1658. struct adapter *adap = cookie;
  1659. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
  1660. if (t4_slow_intr_handler(adap) | process_intrq(adap))
  1661. return IRQ_HANDLED;
  1662. return IRQ_NONE; /* probably shared interrupt */
  1663. }
  1664. /**
  1665. * t4_intr_handler - select the top-level interrupt handler
  1666. * @adap: the adapter
  1667. *
  1668. * Selects the top-level interrupt handler based on the type of interrupts
  1669. * (MSI-X, MSI, or INTx).
  1670. */
  1671. irq_handler_t t4_intr_handler(struct adapter *adap)
  1672. {
  1673. if (adap->flags & USING_MSIX)
  1674. return t4_sge_intr_msix;
  1675. if (adap->flags & USING_MSI)
  1676. return t4_intr_msi;
  1677. return t4_intr_intx;
  1678. }
  1679. static void sge_rx_timer_cb(unsigned long data)
  1680. {
  1681. unsigned long m;
  1682. unsigned int i, cnt[2];
  1683. struct adapter *adap = (struct adapter *)data;
  1684. struct sge *s = &adap->sge;
  1685. for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
  1686. for (m = s->starving_fl[i]; m; m &= m - 1) {
  1687. struct sge_eth_rxq *rxq;
  1688. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  1689. struct sge_fl *fl = s->egr_map[id];
  1690. clear_bit(id, s->starving_fl);
  1691. smp_mb__after_clear_bit();
  1692. if (fl_starving(fl)) {
  1693. rxq = container_of(fl, struct sge_eth_rxq, fl);
  1694. if (napi_reschedule(&rxq->rspq.napi))
  1695. fl->starving++;
  1696. else
  1697. set_bit(id, s->starving_fl);
  1698. }
  1699. }
  1700. t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
  1701. cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
  1702. cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1703. for (i = 0; i < 2; i++)
  1704. if (cnt[i] >= s->starve_thres) {
  1705. if (s->idma_state[i] || cnt[i] == 0xffffffff)
  1706. continue;
  1707. s->idma_state[i] = 1;
  1708. t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
  1709. m = t4_read_reg(adap, SGE_DEBUG_DATA_LOW) >> (i * 16);
  1710. dev_warn(adap->pdev_dev,
  1711. "SGE idma%u starvation detected for "
  1712. "queue %lu\n", i, m & 0xffff);
  1713. } else if (s->idma_state[i])
  1714. s->idma_state[i] = 0;
  1715. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  1716. }
  1717. static void sge_tx_timer_cb(unsigned long data)
  1718. {
  1719. unsigned long m;
  1720. unsigned int i, budget;
  1721. struct adapter *adap = (struct adapter *)data;
  1722. struct sge *s = &adap->sge;
  1723. for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
  1724. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  1725. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  1726. struct sge_ofld_txq *txq = s->egr_map[id];
  1727. clear_bit(id, s->txq_maperr);
  1728. tasklet_schedule(&txq->qresume_tsk);
  1729. }
  1730. budget = MAX_TIMER_TX_RECLAIM;
  1731. i = s->ethtxq_rover;
  1732. do {
  1733. struct sge_eth_txq *q = &s->ethtxq[i];
  1734. if (q->q.in_use &&
  1735. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  1736. __netif_tx_trylock(q->txq)) {
  1737. int avail = reclaimable(&q->q);
  1738. if (avail) {
  1739. if (avail > budget)
  1740. avail = budget;
  1741. free_tx_desc(adap, &q->q, avail, true);
  1742. q->q.in_use -= avail;
  1743. budget -= avail;
  1744. }
  1745. __netif_tx_unlock(q->txq);
  1746. }
  1747. if (++i >= s->ethqsets)
  1748. i = 0;
  1749. } while (budget && i != s->ethtxq_rover);
  1750. s->ethtxq_rover = i;
  1751. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  1752. }
  1753. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  1754. struct net_device *dev, int intr_idx,
  1755. struct sge_fl *fl, rspq_handler_t hnd)
  1756. {
  1757. int ret, flsz = 0;
  1758. struct fw_iq_cmd c;
  1759. struct port_info *pi = netdev_priv(dev);
  1760. /* Size needs to be multiple of 16, including status entry. */
  1761. iq->size = roundup(iq->size, 16);
  1762. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  1763. &iq->phys_addr, NULL, 0);
  1764. if (!iq->desc)
  1765. return -ENOMEM;
  1766. memset(&c, 0, sizeof(c));
  1767. c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
  1768. FW_CMD_WRITE | FW_CMD_EXEC |
  1769. FW_IQ_CMD_PFN(adap->fn) | FW_IQ_CMD_VFN(0));
  1770. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
  1771. FW_LEN16(c));
  1772. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
  1773. FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
  1774. FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
  1775. FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
  1776. -intr_idx - 1));
  1777. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
  1778. FW_IQ_CMD_IQGTSMODE |
  1779. FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
  1780. FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
  1781. c.iqsize = htons(iq->size);
  1782. c.iqaddr = cpu_to_be64(iq->phys_addr);
  1783. if (fl) {
  1784. fl->size = roundup(fl->size, 8);
  1785. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  1786. sizeof(struct rx_sw_desc), &fl->addr,
  1787. &fl->sdesc, STAT_LEN);
  1788. if (!fl->desc)
  1789. goto fl_nomem;
  1790. flsz = fl->size / 8 + STAT_LEN / sizeof(struct tx_desc);
  1791. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN |
  1792. FW_IQ_CMD_FL0PADEN);
  1793. c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
  1794. FW_IQ_CMD_FL0FBMAX(3));
  1795. c.fl0size = htons(flsz);
  1796. c.fl0addr = cpu_to_be64(fl->addr);
  1797. }
  1798. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  1799. if (ret)
  1800. goto err;
  1801. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  1802. iq->cur_desc = iq->desc;
  1803. iq->cidx = 0;
  1804. iq->gen = 1;
  1805. iq->next_intr_params = iq->intr_params;
  1806. iq->cntxt_id = ntohs(c.iqid);
  1807. iq->abs_id = ntohs(c.physiqid);
  1808. iq->size--; /* subtract status entry */
  1809. iq->adap = adap;
  1810. iq->netdev = dev;
  1811. iq->handler = hnd;
  1812. /* set offset to -1 to distinguish ingress queues without FL */
  1813. iq->offset = fl ? 0 : -1;
  1814. adap->sge.ingr_map[iq->cntxt_id - adap->sge.ingr_start] = iq;
  1815. if (fl) {
  1816. fl->cntxt_id = ntohs(c.fl0id);
  1817. fl->avail = fl->pend_cred = 0;
  1818. fl->pidx = fl->cidx = 0;
  1819. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  1820. adap->sge.egr_map[fl->cntxt_id - adap->sge.egr_start] = fl;
  1821. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  1822. }
  1823. return 0;
  1824. fl_nomem:
  1825. ret = -ENOMEM;
  1826. err:
  1827. if (iq->desc) {
  1828. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  1829. iq->desc, iq->phys_addr);
  1830. iq->desc = NULL;
  1831. }
  1832. if (fl && fl->desc) {
  1833. kfree(fl->sdesc);
  1834. fl->sdesc = NULL;
  1835. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  1836. fl->desc, fl->addr);
  1837. fl->desc = NULL;
  1838. }
  1839. return ret;
  1840. }
  1841. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  1842. {
  1843. q->in_use = 0;
  1844. q->cidx = q->pidx = 0;
  1845. q->stops = q->restarts = 0;
  1846. q->stat = (void *)&q->desc[q->size];
  1847. q->cntxt_id = id;
  1848. adap->sge.egr_map[id - adap->sge.egr_start] = q;
  1849. }
  1850. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  1851. struct net_device *dev, struct netdev_queue *netdevq,
  1852. unsigned int iqid)
  1853. {
  1854. int ret, nentries;
  1855. struct fw_eq_eth_cmd c;
  1856. struct port_info *pi = netdev_priv(dev);
  1857. /* Add status entries */
  1858. nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
  1859. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  1860. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  1861. &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN);
  1862. if (!txq->q.desc)
  1863. return -ENOMEM;
  1864. memset(&c, 0, sizeof(c));
  1865. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
  1866. FW_CMD_WRITE | FW_CMD_EXEC |
  1867. FW_EQ_ETH_CMD_PFN(adap->fn) | FW_EQ_ETH_CMD_VFN(0));
  1868. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
  1869. FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
  1870. c.viid_pkd = htonl(FW_EQ_ETH_CMD_VIID(pi->viid));
  1871. c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
  1872. FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
  1873. FW_EQ_ETH_CMD_IQID(iqid));
  1874. c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
  1875. FW_EQ_ETH_CMD_FBMAX(3) |
  1876. FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
  1877. FW_EQ_ETH_CMD_EQSIZE(nentries));
  1878. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  1879. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  1880. if (ret) {
  1881. kfree(txq->q.sdesc);
  1882. txq->q.sdesc = NULL;
  1883. dma_free_coherent(adap->pdev_dev,
  1884. nentries * sizeof(struct tx_desc),
  1885. txq->q.desc, txq->q.phys_addr);
  1886. txq->q.desc = NULL;
  1887. return ret;
  1888. }
  1889. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  1890. txq->txq = netdevq;
  1891. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  1892. txq->mapping_err = 0;
  1893. return 0;
  1894. }
  1895. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  1896. struct net_device *dev, unsigned int iqid,
  1897. unsigned int cmplqid)
  1898. {
  1899. int ret, nentries;
  1900. struct fw_eq_ctrl_cmd c;
  1901. struct port_info *pi = netdev_priv(dev);
  1902. /* Add status entries */
  1903. nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
  1904. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  1905. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  1906. NULL, 0);
  1907. if (!txq->q.desc)
  1908. return -ENOMEM;
  1909. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
  1910. FW_CMD_WRITE | FW_CMD_EXEC |
  1911. FW_EQ_CTRL_CMD_PFN(adap->fn) |
  1912. FW_EQ_CTRL_CMD_VFN(0));
  1913. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
  1914. FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
  1915. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
  1916. c.physeqid_pkd = htonl(0);
  1917. c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
  1918. FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
  1919. FW_EQ_CTRL_CMD_IQID(iqid));
  1920. c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
  1921. FW_EQ_CTRL_CMD_FBMAX(3) |
  1922. FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
  1923. FW_EQ_CTRL_CMD_EQSIZE(nentries));
  1924. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  1925. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  1926. if (ret) {
  1927. dma_free_coherent(adap->pdev_dev,
  1928. nentries * sizeof(struct tx_desc),
  1929. txq->q.desc, txq->q.phys_addr);
  1930. txq->q.desc = NULL;
  1931. return ret;
  1932. }
  1933. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
  1934. txq->adap = adap;
  1935. skb_queue_head_init(&txq->sendq);
  1936. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  1937. txq->full = 0;
  1938. return 0;
  1939. }
  1940. int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
  1941. struct net_device *dev, unsigned int iqid)
  1942. {
  1943. int ret, nentries;
  1944. struct fw_eq_ofld_cmd c;
  1945. struct port_info *pi = netdev_priv(dev);
  1946. /* Add status entries */
  1947. nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
  1948. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  1949. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  1950. &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN);
  1951. if (!txq->q.desc)
  1952. return -ENOMEM;
  1953. memset(&c, 0, sizeof(c));
  1954. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
  1955. FW_CMD_WRITE | FW_CMD_EXEC |
  1956. FW_EQ_OFLD_CMD_PFN(adap->fn) |
  1957. FW_EQ_OFLD_CMD_VFN(0));
  1958. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
  1959. FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
  1960. c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
  1961. FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
  1962. FW_EQ_OFLD_CMD_IQID(iqid));
  1963. c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
  1964. FW_EQ_OFLD_CMD_FBMAX(3) |
  1965. FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
  1966. FW_EQ_OFLD_CMD_EQSIZE(nentries));
  1967. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  1968. ret = t4_wr_mbox(adap, adap->fn, &c, sizeof(c), &c);
  1969. if (ret) {
  1970. kfree(txq->q.sdesc);
  1971. txq->q.sdesc = NULL;
  1972. dma_free_coherent(adap->pdev_dev,
  1973. nentries * sizeof(struct tx_desc),
  1974. txq->q.desc, txq->q.phys_addr);
  1975. txq->q.desc = NULL;
  1976. return ret;
  1977. }
  1978. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  1979. txq->adap = adap;
  1980. skb_queue_head_init(&txq->sendq);
  1981. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  1982. txq->full = 0;
  1983. txq->mapping_err = 0;
  1984. return 0;
  1985. }
  1986. static void free_txq(struct adapter *adap, struct sge_txq *q)
  1987. {
  1988. dma_free_coherent(adap->pdev_dev,
  1989. q->size * sizeof(struct tx_desc) + STAT_LEN,
  1990. q->desc, q->phys_addr);
  1991. q->cntxt_id = 0;
  1992. q->sdesc = NULL;
  1993. q->desc = NULL;
  1994. }
  1995. static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  1996. struct sge_fl *fl)
  1997. {
  1998. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  1999. adap->sge.ingr_map[rq->cntxt_id - adap->sge.ingr_start] = NULL;
  2000. t4_iq_free(adap, adap->fn, adap->fn, 0, FW_IQ_TYPE_FL_INT_CAP,
  2001. rq->cntxt_id, fl_id, 0xffff);
  2002. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  2003. rq->desc, rq->phys_addr);
  2004. netif_napi_del(&rq->napi);
  2005. rq->netdev = NULL;
  2006. rq->cntxt_id = rq->abs_id = 0;
  2007. rq->desc = NULL;
  2008. if (fl) {
  2009. free_rx_bufs(adap, fl, fl->avail);
  2010. dma_free_coherent(adap->pdev_dev, fl->size * 8 + STAT_LEN,
  2011. fl->desc, fl->addr);
  2012. kfree(fl->sdesc);
  2013. fl->sdesc = NULL;
  2014. fl->cntxt_id = 0;
  2015. fl->desc = NULL;
  2016. }
  2017. }
  2018. /**
  2019. * t4_free_sge_resources - free SGE resources
  2020. * @adap: the adapter
  2021. *
  2022. * Frees resources used by the SGE queue sets.
  2023. */
  2024. void t4_free_sge_resources(struct adapter *adap)
  2025. {
  2026. int i;
  2027. struct sge_eth_rxq *eq = adap->sge.ethrxq;
  2028. struct sge_eth_txq *etq = adap->sge.ethtxq;
  2029. struct sge_ofld_rxq *oq = adap->sge.ofldrxq;
  2030. /* clean up Ethernet Tx/Rx queues */
  2031. for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
  2032. if (eq->rspq.desc)
  2033. free_rspq_fl(adap, &eq->rspq, &eq->fl);
  2034. if (etq->q.desc) {
  2035. t4_eth_eq_free(adap, adap->fn, adap->fn, 0,
  2036. etq->q.cntxt_id);
  2037. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  2038. kfree(etq->q.sdesc);
  2039. free_txq(adap, &etq->q);
  2040. }
  2041. }
  2042. /* clean up RDMA and iSCSI Rx queues */
  2043. for (i = 0; i < adap->sge.ofldqsets; i++, oq++) {
  2044. if (oq->rspq.desc)
  2045. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2046. }
  2047. for (i = 0, oq = adap->sge.rdmarxq; i < adap->sge.rdmaqs; i++, oq++) {
  2048. if (oq->rspq.desc)
  2049. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2050. }
  2051. /* clean up offload Tx queues */
  2052. for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
  2053. struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
  2054. if (q->q.desc) {
  2055. tasklet_kill(&q->qresume_tsk);
  2056. t4_ofld_eq_free(adap, adap->fn, adap->fn, 0,
  2057. q->q.cntxt_id);
  2058. free_tx_desc(adap, &q->q, q->q.in_use, false);
  2059. kfree(q->q.sdesc);
  2060. __skb_queue_purge(&q->sendq);
  2061. free_txq(adap, &q->q);
  2062. }
  2063. }
  2064. /* clean up control Tx queues */
  2065. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  2066. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  2067. if (cq->q.desc) {
  2068. tasklet_kill(&cq->qresume_tsk);
  2069. t4_ctrl_eq_free(adap, adap->fn, adap->fn, 0,
  2070. cq->q.cntxt_id);
  2071. __skb_queue_purge(&cq->sendq);
  2072. free_txq(adap, &cq->q);
  2073. }
  2074. }
  2075. if (adap->sge.fw_evtq.desc)
  2076. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  2077. if (adap->sge.intrq.desc)
  2078. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  2079. /* clear the reverse egress queue map */
  2080. memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
  2081. }
  2082. void t4_sge_start(struct adapter *adap)
  2083. {
  2084. adap->sge.ethtxq_rover = 0;
  2085. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2086. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2087. }
  2088. /**
  2089. * t4_sge_stop - disable SGE operation
  2090. * @adap: the adapter
  2091. *
  2092. * Stop tasklets and timers associated with the DMA engine. Note that
  2093. * this is effective only if measures have been taken to disable any HW
  2094. * events that may restart them.
  2095. */
  2096. void t4_sge_stop(struct adapter *adap)
  2097. {
  2098. int i;
  2099. struct sge *s = &adap->sge;
  2100. if (in_interrupt()) /* actions below require waiting */
  2101. return;
  2102. if (s->rx_timer.function)
  2103. del_timer_sync(&s->rx_timer);
  2104. if (s->tx_timer.function)
  2105. del_timer_sync(&s->tx_timer);
  2106. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
  2107. struct sge_ofld_txq *q = &s->ofldtxq[i];
  2108. if (q->q.desc)
  2109. tasklet_kill(&q->qresume_tsk);
  2110. }
  2111. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  2112. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  2113. if (cq->q.desc)
  2114. tasklet_kill(&cq->qresume_tsk);
  2115. }
  2116. }
  2117. /**
  2118. * t4_sge_init - initialize SGE
  2119. * @adap: the adapter
  2120. *
  2121. * Performs SGE initialization needed every time after a chip reset.
  2122. * We do not initialize any of the queues here, instead the driver
  2123. * top-level must request them individually.
  2124. */
  2125. void t4_sge_init(struct adapter *adap)
  2126. {
  2127. unsigned int i, v;
  2128. struct sge *s = &adap->sge;
  2129. unsigned int fl_align_log = ilog2(FL_ALIGN);
  2130. t4_set_reg_field(adap, SGE_CONTROL, PKTSHIFT_MASK |
  2131. INGPADBOUNDARY_MASK | EGRSTATUSPAGESIZE,
  2132. INGPADBOUNDARY(fl_align_log - 5) | PKTSHIFT(2) |
  2133. RXPKTCPLMODE |
  2134. (STAT_LEN == 128 ? EGRSTATUSPAGESIZE : 0));
  2135. for (i = v = 0; i < 32; i += 4)
  2136. v |= (PAGE_SHIFT - 10) << i;
  2137. t4_write_reg(adap, SGE_HOST_PAGE_SIZE, v);
  2138. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0, PAGE_SIZE);
  2139. #if FL_PG_ORDER > 0
  2140. t4_write_reg(adap, SGE_FL_BUFFER_SIZE1, PAGE_SIZE << FL_PG_ORDER);
  2141. #endif
  2142. t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
  2143. THRESHOLD_0(s->counter_val[0]) |
  2144. THRESHOLD_1(s->counter_val[1]) |
  2145. THRESHOLD_2(s->counter_val[2]) |
  2146. THRESHOLD_3(s->counter_val[3]));
  2147. t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
  2148. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
  2149. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
  2150. t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
  2151. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[2])) |
  2152. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[3])));
  2153. t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
  2154. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[4])) |
  2155. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[5])));
  2156. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
  2157. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
  2158. s->starve_thres = core_ticks_per_usec(adap) * 1000000; /* 1 s */
  2159. s->idma_state[0] = s->idma_state[1] = 0;
  2160. spin_lock_init(&s->intrq_lock);
  2161. }