sge.c 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436
  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.starving_fl);
  495. }
  496. return cred;
  497. }
  498. static inline void __refill_fl(struct adapter *adap, struct sge_fl *fl)
  499. {
  500. refill_fl(adap, fl, min(MAX_RX_REFILL, fl_cap(fl) - fl->avail),
  501. GFP_ATOMIC);
  502. }
  503. /**
  504. * alloc_ring - allocate resources for an SGE descriptor ring
  505. * @dev: the PCI device's core device
  506. * @nelem: the number of descriptors
  507. * @elem_size: the size of each descriptor
  508. * @sw_size: the size of the SW state associated with each ring element
  509. * @phys: the physical address of the allocated ring
  510. * @metadata: address of the array holding the SW state for the ring
  511. * @stat_size: extra space in HW ring for status information
  512. *
  513. * Allocates resources for an SGE descriptor ring, such as Tx queues,
  514. * free buffer lists, or response queues. Each SGE ring requires
  515. * space for its HW descriptors plus, optionally, space for the SW state
  516. * associated with each HW entry (the metadata). The function returns
  517. * three values: the virtual address for the HW ring (the return value
  518. * of the function), the bus address of the HW ring, and the address
  519. * of the SW ring.
  520. */
  521. static void *alloc_ring(struct device *dev, size_t nelem, size_t elem_size,
  522. size_t sw_size, dma_addr_t *phys, void *metadata,
  523. size_t stat_size)
  524. {
  525. size_t len = nelem * elem_size + stat_size;
  526. void *s = NULL;
  527. void *p = dma_alloc_coherent(dev, len, phys, GFP_KERNEL);
  528. if (!p)
  529. return NULL;
  530. if (sw_size) {
  531. s = kcalloc(nelem, sw_size, GFP_KERNEL);
  532. if (!s) {
  533. dma_free_coherent(dev, len, p, *phys);
  534. return NULL;
  535. }
  536. }
  537. if (metadata)
  538. *(void **)metadata = s;
  539. memset(p, 0, len);
  540. return p;
  541. }
  542. /**
  543. * sgl_len - calculates the size of an SGL of the given capacity
  544. * @n: the number of SGL entries
  545. *
  546. * Calculates the number of flits needed for a scatter/gather list that
  547. * can hold the given number of entries.
  548. */
  549. static inline unsigned int sgl_len(unsigned int n)
  550. {
  551. n--;
  552. return (3 * n) / 2 + (n & 1) + 2;
  553. }
  554. /**
  555. * flits_to_desc - returns the num of Tx descriptors for the given flits
  556. * @n: the number of flits
  557. *
  558. * Returns the number of Tx descriptors needed for the supplied number
  559. * of flits.
  560. */
  561. static inline unsigned int flits_to_desc(unsigned int n)
  562. {
  563. BUG_ON(n > SGE_MAX_WR_LEN / 8);
  564. return DIV_ROUND_UP(n, 8);
  565. }
  566. /**
  567. * is_eth_imm - can an Ethernet packet be sent as immediate data?
  568. * @skb: the packet
  569. *
  570. * Returns whether an Ethernet packet is small enough to fit as
  571. * immediate data.
  572. */
  573. static inline int is_eth_imm(const struct sk_buff *skb)
  574. {
  575. return skb->len <= MAX_IMM_TX_PKT_LEN - sizeof(struct cpl_tx_pkt);
  576. }
  577. /**
  578. * calc_tx_flits - calculate the number of flits for a packet Tx WR
  579. * @skb: the packet
  580. *
  581. * Returns the number of flits needed for a Tx WR for the given Ethernet
  582. * packet, including the needed WR and CPL headers.
  583. */
  584. static inline unsigned int calc_tx_flits(const struct sk_buff *skb)
  585. {
  586. unsigned int flits;
  587. if (is_eth_imm(skb))
  588. return DIV_ROUND_UP(skb->len + sizeof(struct cpl_tx_pkt), 8);
  589. flits = sgl_len(skb_shinfo(skb)->nr_frags + 1) + 4;
  590. if (skb_shinfo(skb)->gso_size)
  591. flits += 2;
  592. return flits;
  593. }
  594. /**
  595. * calc_tx_descs - calculate the number of Tx descriptors for a packet
  596. * @skb: the packet
  597. *
  598. * Returns the number of Tx descriptors needed for the given Ethernet
  599. * packet, including the needed WR and CPL headers.
  600. */
  601. static inline unsigned int calc_tx_descs(const struct sk_buff *skb)
  602. {
  603. return flits_to_desc(calc_tx_flits(skb));
  604. }
  605. /**
  606. * write_sgl - populate a scatter/gather list for a packet
  607. * @skb: the packet
  608. * @q: the Tx queue we are writing into
  609. * @sgl: starting location for writing the SGL
  610. * @end: points right after the end of the SGL
  611. * @start: start offset into skb main-body data to include in the SGL
  612. * @addr: the list of bus addresses for the SGL elements
  613. *
  614. * Generates a gather list for the buffers that make up a packet.
  615. * The caller must provide adequate space for the SGL that will be written.
  616. * The SGL includes all of the packet's page fragments and the data in its
  617. * main body except for the first @start bytes. @sgl must be 16-byte
  618. * aligned and within a Tx descriptor with available space. @end points
  619. * right after the end of the SGL but does not account for any potential
  620. * wrap around, i.e., @end > @sgl.
  621. */
  622. static void write_sgl(const struct sk_buff *skb, struct sge_txq *q,
  623. struct ulptx_sgl *sgl, u64 *end, unsigned int start,
  624. const dma_addr_t *addr)
  625. {
  626. unsigned int i, len;
  627. struct ulptx_sge_pair *to;
  628. const struct skb_shared_info *si = skb_shinfo(skb);
  629. unsigned int nfrags = si->nr_frags;
  630. struct ulptx_sge_pair buf[MAX_SKB_FRAGS / 2 + 1];
  631. len = skb_headlen(skb) - start;
  632. if (likely(len)) {
  633. sgl->len0 = htonl(len);
  634. sgl->addr0 = cpu_to_be64(addr[0] + start);
  635. nfrags++;
  636. } else {
  637. sgl->len0 = htonl(si->frags[0].size);
  638. sgl->addr0 = cpu_to_be64(addr[1]);
  639. }
  640. sgl->cmd_nsge = htonl(ULPTX_CMD(ULP_TX_SC_DSGL) | ULPTX_NSGE(nfrags));
  641. if (likely(--nfrags == 0))
  642. return;
  643. /*
  644. * Most of the complexity below deals with the possibility we hit the
  645. * end of the queue in the middle of writing the SGL. For this case
  646. * only we create the SGL in a temporary buffer and then copy it.
  647. */
  648. to = (u8 *)end > (u8 *)q->stat ? buf : sgl->sge;
  649. for (i = (nfrags != si->nr_frags); nfrags >= 2; nfrags -= 2, to++) {
  650. to->len[0] = cpu_to_be32(si->frags[i].size);
  651. to->len[1] = cpu_to_be32(si->frags[++i].size);
  652. to->addr[0] = cpu_to_be64(addr[i]);
  653. to->addr[1] = cpu_to_be64(addr[++i]);
  654. }
  655. if (nfrags) {
  656. to->len[0] = cpu_to_be32(si->frags[i].size);
  657. to->len[1] = cpu_to_be32(0);
  658. to->addr[0] = cpu_to_be64(addr[i + 1]);
  659. }
  660. if (unlikely((u8 *)end > (u8 *)q->stat)) {
  661. unsigned int part0 = (u8 *)q->stat - (u8 *)sgl->sge, part1;
  662. if (likely(part0))
  663. memcpy(sgl->sge, buf, part0);
  664. part1 = (u8 *)end - (u8 *)q->stat;
  665. memcpy(q->desc, (u8 *)buf + part0, part1);
  666. end = (void *)q->desc + part1;
  667. }
  668. if ((uintptr_t)end & 8) /* 0-pad to multiple of 16 */
  669. *(u64 *)end = 0;
  670. }
  671. /**
  672. * ring_tx_db - check and potentially ring a Tx queue's doorbell
  673. * @adap: the adapter
  674. * @q: the Tx queue
  675. * @n: number of new descriptors to give to HW
  676. *
  677. * Ring the doorbel for a Tx queue.
  678. */
  679. static inline void ring_tx_db(struct adapter *adap, struct sge_txq *q, int n)
  680. {
  681. wmb(); /* write descriptors before telling HW */
  682. t4_write_reg(adap, MYPF_REG(SGE_PF_KDOORBELL),
  683. QID(q->cntxt_id) | PIDX(n));
  684. }
  685. /**
  686. * inline_tx_skb - inline a packet's data into Tx descriptors
  687. * @skb: the packet
  688. * @q: the Tx queue where the packet will be inlined
  689. * @pos: starting position in the Tx queue where to inline the packet
  690. *
  691. * Inline a packet's contents directly into Tx descriptors, starting at
  692. * the given position within the Tx DMA ring.
  693. * Most of the complexity of this operation is dealing with wrap arounds
  694. * in the middle of the packet we want to inline.
  695. */
  696. static void inline_tx_skb(const struct sk_buff *skb, const struct sge_txq *q,
  697. void *pos)
  698. {
  699. u64 *p;
  700. int left = (void *)q->stat - pos;
  701. if (likely(skb->len <= left)) {
  702. if (likely(!skb->data_len))
  703. skb_copy_from_linear_data(skb, pos, skb->len);
  704. else
  705. skb_copy_bits(skb, 0, pos, skb->len);
  706. pos += skb->len;
  707. } else {
  708. skb_copy_bits(skb, 0, pos, left);
  709. skb_copy_bits(skb, left, q->desc, skb->len - left);
  710. pos = (void *)q->desc + (skb->len - left);
  711. }
  712. /* 0-pad to multiple of 16 */
  713. p = PTR_ALIGN(pos, 8);
  714. if ((uintptr_t)p & 8)
  715. *p = 0;
  716. }
  717. /*
  718. * Figure out what HW csum a packet wants and return the appropriate control
  719. * bits.
  720. */
  721. static u64 hwcsum(const struct sk_buff *skb)
  722. {
  723. int csum_type;
  724. const struct iphdr *iph = ip_hdr(skb);
  725. if (iph->version == 4) {
  726. if (iph->protocol == IPPROTO_TCP)
  727. csum_type = TX_CSUM_TCPIP;
  728. else if (iph->protocol == IPPROTO_UDP)
  729. csum_type = TX_CSUM_UDPIP;
  730. else {
  731. nocsum: /*
  732. * unknown protocol, disable HW csum
  733. * and hope a bad packet is detected
  734. */
  735. return TXPKT_L4CSUM_DIS;
  736. }
  737. } else {
  738. /*
  739. * this doesn't work with extension headers
  740. */
  741. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)iph;
  742. if (ip6h->nexthdr == IPPROTO_TCP)
  743. csum_type = TX_CSUM_TCPIP6;
  744. else if (ip6h->nexthdr == IPPROTO_UDP)
  745. csum_type = TX_CSUM_UDPIP6;
  746. else
  747. goto nocsum;
  748. }
  749. if (likely(csum_type >= TX_CSUM_TCPIP))
  750. return TXPKT_CSUM_TYPE(csum_type) |
  751. TXPKT_IPHDR_LEN(skb_network_header_len(skb)) |
  752. TXPKT_ETHHDR_LEN(skb_network_offset(skb) - ETH_HLEN);
  753. else {
  754. int start = skb_transport_offset(skb);
  755. return TXPKT_CSUM_TYPE(csum_type) | TXPKT_CSUM_START(start) |
  756. TXPKT_CSUM_LOC(start + skb->csum_offset);
  757. }
  758. }
  759. static void eth_txq_stop(struct sge_eth_txq *q)
  760. {
  761. netif_tx_stop_queue(q->txq);
  762. q->q.stops++;
  763. }
  764. static inline void txq_advance(struct sge_txq *q, unsigned int n)
  765. {
  766. q->in_use += n;
  767. q->pidx += n;
  768. if (q->pidx >= q->size)
  769. q->pidx -= q->size;
  770. }
  771. /**
  772. * t4_eth_xmit - add a packet to an Ethernet Tx queue
  773. * @skb: the packet
  774. * @dev: the egress net device
  775. *
  776. * Add a packet to an SGE Ethernet Tx queue. Runs with softirqs disabled.
  777. */
  778. netdev_tx_t t4_eth_xmit(struct sk_buff *skb, struct net_device *dev)
  779. {
  780. u32 wr_mid;
  781. u64 cntrl, *end;
  782. int qidx, credits;
  783. unsigned int flits, ndesc;
  784. struct adapter *adap;
  785. struct sge_eth_txq *q;
  786. const struct port_info *pi;
  787. struct fw_eth_tx_pkt_wr *wr;
  788. struct cpl_tx_pkt_core *cpl;
  789. const struct skb_shared_info *ssi;
  790. dma_addr_t addr[MAX_SKB_FRAGS + 1];
  791. /*
  792. * The chip min packet length is 10 octets but play safe and reject
  793. * anything shorter than an Ethernet header.
  794. */
  795. if (unlikely(skb->len < ETH_HLEN)) {
  796. out_free: dev_kfree_skb(skb);
  797. return NETDEV_TX_OK;
  798. }
  799. pi = netdev_priv(dev);
  800. adap = pi->adapter;
  801. qidx = skb_get_queue_mapping(skb);
  802. q = &adap->sge.ethtxq[qidx + pi->first_qset];
  803. reclaim_completed_tx(adap, &q->q, true);
  804. flits = calc_tx_flits(skb);
  805. ndesc = flits_to_desc(flits);
  806. credits = txq_avail(&q->q) - ndesc;
  807. if (unlikely(credits < 0)) {
  808. eth_txq_stop(q);
  809. dev_err(adap->pdev_dev,
  810. "%s: Tx ring %u full while queue awake!\n",
  811. dev->name, qidx);
  812. return NETDEV_TX_BUSY;
  813. }
  814. if (!is_eth_imm(skb) &&
  815. unlikely(map_skb(adap->pdev_dev, skb, addr) < 0)) {
  816. q->mapping_err++;
  817. goto out_free;
  818. }
  819. wr_mid = FW_WR_LEN16(DIV_ROUND_UP(flits, 2));
  820. if (unlikely(credits < ETHTXQ_STOP_THRES)) {
  821. eth_txq_stop(q);
  822. wr_mid |= FW_WR_EQUEQ | FW_WR_EQUIQ;
  823. }
  824. wr = (void *)&q->q.desc[q->q.pidx];
  825. wr->equiq_to_len16 = htonl(wr_mid);
  826. wr->r3 = cpu_to_be64(0);
  827. end = (u64 *)wr + flits;
  828. ssi = skb_shinfo(skb);
  829. if (ssi->gso_size) {
  830. struct cpl_tx_pkt_lso_core *lso = (void *)(wr + 1);
  831. bool v6 = (ssi->gso_type & SKB_GSO_TCPV6) != 0;
  832. int l3hdr_len = skb_network_header_len(skb);
  833. int eth_xtra_len = skb_network_offset(skb) - ETH_HLEN;
  834. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  835. FW_WR_IMMDLEN(sizeof(*lso)));
  836. lso->lso_ctrl = htonl(LSO_OPCODE(CPL_TX_PKT_LSO) |
  837. LSO_FIRST_SLICE | LSO_LAST_SLICE |
  838. LSO_IPV6(v6) |
  839. LSO_ETHHDR_LEN(eth_xtra_len / 4) |
  840. LSO_IPHDR_LEN(l3hdr_len / 4) |
  841. LSO_TCPHDR_LEN(tcp_hdr(skb)->doff));
  842. lso->ipid_ofst = htons(0);
  843. lso->mss = htons(ssi->gso_size);
  844. lso->seqno_offset = htonl(0);
  845. lso->len = htonl(skb->len);
  846. cpl = (void *)(lso + 1);
  847. cntrl = TXPKT_CSUM_TYPE(v6 ? TX_CSUM_TCPIP6 : TX_CSUM_TCPIP) |
  848. TXPKT_IPHDR_LEN(l3hdr_len) |
  849. TXPKT_ETHHDR_LEN(eth_xtra_len);
  850. q->tso++;
  851. q->tx_cso += ssi->gso_segs;
  852. } else {
  853. int len;
  854. len = is_eth_imm(skb) ? skb->len + sizeof(*cpl) : sizeof(*cpl);
  855. wr->op_immdlen = htonl(FW_WR_OP(FW_ETH_TX_PKT_WR) |
  856. FW_WR_IMMDLEN(len));
  857. cpl = (void *)(wr + 1);
  858. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  859. cntrl = hwcsum(skb) | TXPKT_IPCSUM_DIS;
  860. q->tx_cso++;
  861. } else
  862. cntrl = TXPKT_L4CSUM_DIS | TXPKT_IPCSUM_DIS;
  863. }
  864. if (vlan_tx_tag_present(skb)) {
  865. q->vlan_ins++;
  866. cntrl |= TXPKT_VLAN_VLD | TXPKT_VLAN(vlan_tx_tag_get(skb));
  867. }
  868. cpl->ctrl0 = htonl(TXPKT_OPCODE(CPL_TX_PKT_XT) |
  869. TXPKT_INTF(pi->tx_chan) | TXPKT_PF(0));
  870. cpl->pack = htons(0);
  871. cpl->len = htons(skb->len);
  872. cpl->ctrl1 = cpu_to_be64(cntrl);
  873. if (is_eth_imm(skb)) {
  874. inline_tx_skb(skb, &q->q, cpl + 1);
  875. dev_kfree_skb(skb);
  876. } else {
  877. int last_desc;
  878. write_sgl(skb, &q->q, (struct ulptx_sgl *)(cpl + 1), end, 0,
  879. addr);
  880. skb_orphan(skb);
  881. last_desc = q->q.pidx + ndesc - 1;
  882. if (last_desc >= q->q.size)
  883. last_desc -= q->q.size;
  884. q->q.sdesc[last_desc].skb = skb;
  885. q->q.sdesc[last_desc].sgl = (struct ulptx_sgl *)(cpl + 1);
  886. }
  887. txq_advance(&q->q, ndesc);
  888. ring_tx_db(adap, &q->q, ndesc);
  889. return NETDEV_TX_OK;
  890. }
  891. /**
  892. * reclaim_completed_tx_imm - reclaim completed control-queue Tx descs
  893. * @q: the SGE control Tx queue
  894. *
  895. * This is a variant of reclaim_completed_tx() that is used for Tx queues
  896. * that send only immediate data (presently just the control queues) and
  897. * thus do not have any sk_buffs to release.
  898. */
  899. static inline void reclaim_completed_tx_imm(struct sge_txq *q)
  900. {
  901. int hw_cidx = ntohs(q->stat->cidx);
  902. int reclaim = hw_cidx - q->cidx;
  903. if (reclaim < 0)
  904. reclaim += q->size;
  905. q->in_use -= reclaim;
  906. q->cidx = hw_cidx;
  907. }
  908. /**
  909. * is_imm - check whether a packet can be sent as immediate data
  910. * @skb: the packet
  911. *
  912. * Returns true if a packet can be sent as a WR with immediate data.
  913. */
  914. static inline int is_imm(const struct sk_buff *skb)
  915. {
  916. return skb->len <= MAX_CTRL_WR_LEN;
  917. }
  918. /**
  919. * ctrlq_check_stop - check if a control queue is full and should stop
  920. * @q: the queue
  921. * @wr: most recent WR written to the queue
  922. *
  923. * Check if a control queue has become full and should be stopped.
  924. * We clean up control queue descriptors very lazily, only when we are out.
  925. * If the queue is still full after reclaiming any completed descriptors
  926. * we suspend it and have the last WR wake it up.
  927. */
  928. static void ctrlq_check_stop(struct sge_ctrl_txq *q, struct fw_wr_hdr *wr)
  929. {
  930. reclaim_completed_tx_imm(&q->q);
  931. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  932. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  933. q->q.stops++;
  934. q->full = 1;
  935. }
  936. }
  937. /**
  938. * ctrl_xmit - send a packet through an SGE control Tx queue
  939. * @q: the control queue
  940. * @skb: the packet
  941. *
  942. * Send a packet through an SGE control Tx queue. Packets sent through
  943. * a control queue must fit entirely as immediate data.
  944. */
  945. static int ctrl_xmit(struct sge_ctrl_txq *q, struct sk_buff *skb)
  946. {
  947. unsigned int ndesc;
  948. struct fw_wr_hdr *wr;
  949. if (unlikely(!is_imm(skb))) {
  950. WARN_ON(1);
  951. dev_kfree_skb(skb);
  952. return NET_XMIT_DROP;
  953. }
  954. ndesc = DIV_ROUND_UP(skb->len, sizeof(struct tx_desc));
  955. spin_lock(&q->sendq.lock);
  956. if (unlikely(q->full)) {
  957. skb->priority = ndesc; /* save for restart */
  958. __skb_queue_tail(&q->sendq, skb);
  959. spin_unlock(&q->sendq.lock);
  960. return NET_XMIT_CN;
  961. }
  962. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  963. inline_tx_skb(skb, &q->q, wr);
  964. txq_advance(&q->q, ndesc);
  965. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES))
  966. ctrlq_check_stop(q, wr);
  967. ring_tx_db(q->adap, &q->q, ndesc);
  968. spin_unlock(&q->sendq.lock);
  969. kfree_skb(skb);
  970. return NET_XMIT_SUCCESS;
  971. }
  972. /**
  973. * restart_ctrlq - restart a suspended control queue
  974. * @data: the control queue to restart
  975. *
  976. * Resumes transmission on a suspended Tx control queue.
  977. */
  978. static void restart_ctrlq(unsigned long data)
  979. {
  980. struct sk_buff *skb;
  981. unsigned int written = 0;
  982. struct sge_ctrl_txq *q = (struct sge_ctrl_txq *)data;
  983. spin_lock(&q->sendq.lock);
  984. reclaim_completed_tx_imm(&q->q);
  985. BUG_ON(txq_avail(&q->q) < TXQ_STOP_THRES); /* q should be empty */
  986. while ((skb = __skb_dequeue(&q->sendq)) != NULL) {
  987. struct fw_wr_hdr *wr;
  988. unsigned int ndesc = skb->priority; /* previously saved */
  989. /*
  990. * Write descriptors and free skbs outside the lock to limit
  991. * wait times. q->full is still set so new skbs will be queued.
  992. */
  993. spin_unlock(&q->sendq.lock);
  994. wr = (struct fw_wr_hdr *)&q->q.desc[q->q.pidx];
  995. inline_tx_skb(skb, &q->q, wr);
  996. kfree_skb(skb);
  997. written += ndesc;
  998. txq_advance(&q->q, ndesc);
  999. if (unlikely(txq_avail(&q->q) < TXQ_STOP_THRES)) {
  1000. unsigned long old = q->q.stops;
  1001. ctrlq_check_stop(q, wr);
  1002. if (q->q.stops != old) { /* suspended anew */
  1003. spin_lock(&q->sendq.lock);
  1004. goto ringdb;
  1005. }
  1006. }
  1007. if (written > 16) {
  1008. ring_tx_db(q->adap, &q->q, written);
  1009. written = 0;
  1010. }
  1011. spin_lock(&q->sendq.lock);
  1012. }
  1013. q->full = 0;
  1014. ringdb: if (written)
  1015. ring_tx_db(q->adap, &q->q, written);
  1016. spin_unlock(&q->sendq.lock);
  1017. }
  1018. /**
  1019. * t4_mgmt_tx - send a management message
  1020. * @adap: the adapter
  1021. * @skb: the packet containing the management message
  1022. *
  1023. * Send a management message through control queue 0.
  1024. */
  1025. int t4_mgmt_tx(struct adapter *adap, struct sk_buff *skb)
  1026. {
  1027. int ret;
  1028. local_bh_disable();
  1029. ret = ctrl_xmit(&adap->sge.ctrlq[0], skb);
  1030. local_bh_enable();
  1031. return ret;
  1032. }
  1033. /**
  1034. * is_ofld_imm - check whether a packet can be sent as immediate data
  1035. * @skb: the packet
  1036. *
  1037. * Returns true if a packet can be sent as an offload WR with immediate
  1038. * data. We currently use the same limit as for Ethernet packets.
  1039. */
  1040. static inline int is_ofld_imm(const struct sk_buff *skb)
  1041. {
  1042. return skb->len <= MAX_IMM_TX_PKT_LEN;
  1043. }
  1044. /**
  1045. * calc_tx_flits_ofld - calculate # of flits for an offload packet
  1046. * @skb: the packet
  1047. *
  1048. * Returns the number of flits needed for the given offload packet.
  1049. * These packets are already fully constructed and no additional headers
  1050. * will be added.
  1051. */
  1052. static inline unsigned int calc_tx_flits_ofld(const struct sk_buff *skb)
  1053. {
  1054. unsigned int flits, cnt;
  1055. if (is_ofld_imm(skb))
  1056. return DIV_ROUND_UP(skb->len, 8);
  1057. flits = skb_transport_offset(skb) / 8U; /* headers */
  1058. cnt = skb_shinfo(skb)->nr_frags;
  1059. if (skb->tail != skb->transport_header)
  1060. cnt++;
  1061. return flits + sgl_len(cnt);
  1062. }
  1063. /**
  1064. * txq_stop_maperr - stop a Tx queue due to I/O MMU exhaustion
  1065. * @adap: the adapter
  1066. * @q: the queue to stop
  1067. *
  1068. * Mark a Tx queue stopped due to I/O MMU exhaustion and resulting
  1069. * inability to map packets. A periodic timer attempts to restart
  1070. * queues so marked.
  1071. */
  1072. static void txq_stop_maperr(struct sge_ofld_txq *q)
  1073. {
  1074. q->mapping_err++;
  1075. q->q.stops++;
  1076. set_bit(q->q.cntxt_id, q->adap->sge.txq_maperr);
  1077. }
  1078. /**
  1079. * ofldtxq_stop - stop an offload Tx queue that has become full
  1080. * @q: the queue to stop
  1081. * @skb: the packet causing the queue to become full
  1082. *
  1083. * Stops an offload Tx queue that has become full and modifies the packet
  1084. * being written to request a wakeup.
  1085. */
  1086. static void ofldtxq_stop(struct sge_ofld_txq *q, struct sk_buff *skb)
  1087. {
  1088. struct fw_wr_hdr *wr = (struct fw_wr_hdr *)skb->data;
  1089. wr->lo |= htonl(FW_WR_EQUEQ | FW_WR_EQUIQ);
  1090. q->q.stops++;
  1091. q->full = 1;
  1092. }
  1093. /**
  1094. * service_ofldq - restart a suspended offload queue
  1095. * @q: the offload queue
  1096. *
  1097. * Services an offload Tx queue by moving packets from its packet queue
  1098. * to the HW Tx ring. The function starts and ends with the queue locked.
  1099. */
  1100. static void service_ofldq(struct sge_ofld_txq *q)
  1101. {
  1102. u64 *pos;
  1103. int credits;
  1104. struct sk_buff *skb;
  1105. unsigned int written = 0;
  1106. unsigned int flits, ndesc;
  1107. while ((skb = skb_peek(&q->sendq)) != NULL && !q->full) {
  1108. /*
  1109. * We drop the lock but leave skb on sendq, thus retaining
  1110. * exclusive access to the state of the queue.
  1111. */
  1112. spin_unlock(&q->sendq.lock);
  1113. reclaim_completed_tx(q->adap, &q->q, false);
  1114. flits = skb->priority; /* previously saved */
  1115. ndesc = flits_to_desc(flits);
  1116. credits = txq_avail(&q->q) - ndesc;
  1117. BUG_ON(credits < 0);
  1118. if (unlikely(credits < TXQ_STOP_THRES))
  1119. ofldtxq_stop(q, skb);
  1120. pos = (u64 *)&q->q.desc[q->q.pidx];
  1121. if (is_ofld_imm(skb))
  1122. inline_tx_skb(skb, &q->q, pos);
  1123. else if (map_skb(q->adap->pdev_dev, skb,
  1124. (dma_addr_t *)skb->head)) {
  1125. txq_stop_maperr(q);
  1126. spin_lock(&q->sendq.lock);
  1127. break;
  1128. } else {
  1129. int last_desc, hdr_len = skb_transport_offset(skb);
  1130. memcpy(pos, skb->data, hdr_len);
  1131. write_sgl(skb, &q->q, (void *)pos + hdr_len,
  1132. pos + flits, hdr_len,
  1133. (dma_addr_t *)skb->head);
  1134. #ifdef CONFIG_NEED_DMA_MAP_STATE
  1135. skb->dev = q->adap->port[0];
  1136. skb->destructor = deferred_unmap_destructor;
  1137. #endif
  1138. last_desc = q->q.pidx + ndesc - 1;
  1139. if (last_desc >= q->q.size)
  1140. last_desc -= q->q.size;
  1141. q->q.sdesc[last_desc].skb = skb;
  1142. }
  1143. txq_advance(&q->q, ndesc);
  1144. written += ndesc;
  1145. if (unlikely(written > 32)) {
  1146. ring_tx_db(q->adap, &q->q, written);
  1147. written = 0;
  1148. }
  1149. spin_lock(&q->sendq.lock);
  1150. __skb_unlink(skb, &q->sendq);
  1151. if (is_ofld_imm(skb))
  1152. kfree_skb(skb);
  1153. }
  1154. if (likely(written))
  1155. ring_tx_db(q->adap, &q->q, written);
  1156. }
  1157. /**
  1158. * ofld_xmit - send a packet through an offload queue
  1159. * @q: the Tx offload queue
  1160. * @skb: the packet
  1161. *
  1162. * Send an offload packet through an SGE offload queue.
  1163. */
  1164. static int ofld_xmit(struct sge_ofld_txq *q, struct sk_buff *skb)
  1165. {
  1166. skb->priority = calc_tx_flits_ofld(skb); /* save for restart */
  1167. spin_lock(&q->sendq.lock);
  1168. __skb_queue_tail(&q->sendq, skb);
  1169. if (q->sendq.qlen == 1)
  1170. service_ofldq(q);
  1171. spin_unlock(&q->sendq.lock);
  1172. return NET_XMIT_SUCCESS;
  1173. }
  1174. /**
  1175. * restart_ofldq - restart a suspended offload queue
  1176. * @data: the offload queue to restart
  1177. *
  1178. * Resumes transmission on a suspended Tx offload queue.
  1179. */
  1180. static void restart_ofldq(unsigned long data)
  1181. {
  1182. struct sge_ofld_txq *q = (struct sge_ofld_txq *)data;
  1183. spin_lock(&q->sendq.lock);
  1184. q->full = 0; /* the queue actually is completely empty now */
  1185. service_ofldq(q);
  1186. spin_unlock(&q->sendq.lock);
  1187. }
  1188. /**
  1189. * skb_txq - return the Tx queue an offload packet should use
  1190. * @skb: the packet
  1191. *
  1192. * Returns the Tx queue an offload packet should use as indicated by bits
  1193. * 1-15 in the packet's queue_mapping.
  1194. */
  1195. static inline unsigned int skb_txq(const struct sk_buff *skb)
  1196. {
  1197. return skb->queue_mapping >> 1;
  1198. }
  1199. /**
  1200. * is_ctrl_pkt - return whether an offload packet is a control packet
  1201. * @skb: the packet
  1202. *
  1203. * Returns whether an offload packet should use an OFLD or a CTRL
  1204. * Tx queue as indicated by bit 0 in the packet's queue_mapping.
  1205. */
  1206. static inline unsigned int is_ctrl_pkt(const struct sk_buff *skb)
  1207. {
  1208. return skb->queue_mapping & 1;
  1209. }
  1210. static inline int ofld_send(struct adapter *adap, struct sk_buff *skb)
  1211. {
  1212. unsigned int idx = skb_txq(skb);
  1213. if (unlikely(is_ctrl_pkt(skb)))
  1214. return ctrl_xmit(&adap->sge.ctrlq[idx], skb);
  1215. return ofld_xmit(&adap->sge.ofldtxq[idx], skb);
  1216. }
  1217. /**
  1218. * t4_ofld_send - send an offload packet
  1219. * @adap: the adapter
  1220. * @skb: the packet
  1221. *
  1222. * Sends an offload packet. We use the packet queue_mapping to select the
  1223. * appropriate Tx queue as follows: bit 0 indicates whether the packet
  1224. * should be sent as regular or control, bits 1-15 select the queue.
  1225. */
  1226. int t4_ofld_send(struct adapter *adap, struct sk_buff *skb)
  1227. {
  1228. int ret;
  1229. local_bh_disable();
  1230. ret = ofld_send(adap, skb);
  1231. local_bh_enable();
  1232. return ret;
  1233. }
  1234. /**
  1235. * cxgb4_ofld_send - send an offload packet
  1236. * @dev: the net device
  1237. * @skb: the packet
  1238. *
  1239. * Sends an offload packet. This is an exported version of @t4_ofld_send,
  1240. * intended for ULDs.
  1241. */
  1242. int cxgb4_ofld_send(struct net_device *dev, struct sk_buff *skb)
  1243. {
  1244. return t4_ofld_send(netdev2adap(dev), skb);
  1245. }
  1246. EXPORT_SYMBOL(cxgb4_ofld_send);
  1247. static inline void copy_frags(struct skb_shared_info *ssi,
  1248. const struct pkt_gl *gl, unsigned int offset)
  1249. {
  1250. unsigned int n;
  1251. /* usually there's just one frag */
  1252. ssi->frags[0].page = gl->frags[0].page;
  1253. ssi->frags[0].page_offset = gl->frags[0].page_offset + offset;
  1254. ssi->frags[0].size = gl->frags[0].size - offset;
  1255. ssi->nr_frags = gl->nfrags;
  1256. n = gl->nfrags - 1;
  1257. if (n)
  1258. memcpy(&ssi->frags[1], &gl->frags[1], n * sizeof(skb_frag_t));
  1259. /* get a reference to the last page, we don't own it */
  1260. get_page(gl->frags[n].page);
  1261. }
  1262. /**
  1263. * cxgb4_pktgl_to_skb - build an sk_buff from a packet gather list
  1264. * @gl: the gather list
  1265. * @skb_len: size of sk_buff main body if it carries fragments
  1266. * @pull_len: amount of data to move to the sk_buff's main body
  1267. *
  1268. * Builds an sk_buff from the given packet gather list. Returns the
  1269. * sk_buff or %NULL if sk_buff allocation failed.
  1270. */
  1271. struct sk_buff *cxgb4_pktgl_to_skb(const struct pkt_gl *gl,
  1272. unsigned int skb_len, unsigned int pull_len)
  1273. {
  1274. struct sk_buff *skb;
  1275. /*
  1276. * Below we rely on RX_COPY_THRES being less than the smallest Rx buffer
  1277. * size, which is expected since buffers are at least PAGE_SIZEd.
  1278. * In this case packets up to RX_COPY_THRES have only one fragment.
  1279. */
  1280. if (gl->tot_len <= RX_COPY_THRES) {
  1281. skb = dev_alloc_skb(gl->tot_len);
  1282. if (unlikely(!skb))
  1283. goto out;
  1284. __skb_put(skb, gl->tot_len);
  1285. skb_copy_to_linear_data(skb, gl->va, gl->tot_len);
  1286. } else {
  1287. skb = dev_alloc_skb(skb_len);
  1288. if (unlikely(!skb))
  1289. goto out;
  1290. __skb_put(skb, pull_len);
  1291. skb_copy_to_linear_data(skb, gl->va, pull_len);
  1292. copy_frags(skb_shinfo(skb), gl, pull_len);
  1293. skb->len = gl->tot_len;
  1294. skb->data_len = skb->len - pull_len;
  1295. skb->truesize += skb->data_len;
  1296. }
  1297. out: return skb;
  1298. }
  1299. EXPORT_SYMBOL(cxgb4_pktgl_to_skb);
  1300. /**
  1301. * t4_pktgl_free - free a packet gather list
  1302. * @gl: the gather list
  1303. *
  1304. * Releases the pages of a packet gather list. We do not own the last
  1305. * page on the list and do not free it.
  1306. */
  1307. static void t4_pktgl_free(const struct pkt_gl *gl)
  1308. {
  1309. int n;
  1310. const skb_frag_t *p;
  1311. for (p = gl->frags, n = gl->nfrags - 1; n--; p++)
  1312. put_page(p->page);
  1313. }
  1314. /*
  1315. * Process an MPS trace packet. Give it an unused protocol number so it won't
  1316. * be delivered to anyone and send it to the stack for capture.
  1317. */
  1318. static noinline int handle_trace_pkt(struct adapter *adap,
  1319. const struct pkt_gl *gl)
  1320. {
  1321. struct sk_buff *skb;
  1322. struct cpl_trace_pkt *p;
  1323. skb = cxgb4_pktgl_to_skb(gl, RX_PULL_LEN, RX_PULL_LEN);
  1324. if (unlikely(!skb)) {
  1325. t4_pktgl_free(gl);
  1326. return 0;
  1327. }
  1328. p = (struct cpl_trace_pkt *)skb->data;
  1329. __skb_pull(skb, sizeof(*p));
  1330. skb_reset_mac_header(skb);
  1331. skb->protocol = htons(0xffff);
  1332. skb->dev = adap->port[0];
  1333. netif_receive_skb(skb);
  1334. return 0;
  1335. }
  1336. static void do_gro(struct sge_eth_rxq *rxq, const struct pkt_gl *gl,
  1337. const struct cpl_rx_pkt *pkt)
  1338. {
  1339. int ret;
  1340. struct sk_buff *skb;
  1341. skb = napi_get_frags(&rxq->rspq.napi);
  1342. if (unlikely(!skb)) {
  1343. t4_pktgl_free(gl);
  1344. rxq->stats.rx_drops++;
  1345. return;
  1346. }
  1347. copy_frags(skb_shinfo(skb), gl, RX_PKT_PAD);
  1348. skb->len = gl->tot_len - RX_PKT_PAD;
  1349. skb->data_len = skb->len;
  1350. skb->truesize += skb->data_len;
  1351. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1352. skb_record_rx_queue(skb, rxq->rspq.idx);
  1353. if (rxq->rspq.netdev->features & NETIF_F_RXHASH)
  1354. skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
  1355. if (unlikely(pkt->vlan_ex)) {
  1356. struct port_info *pi = netdev_priv(rxq->rspq.netdev);
  1357. struct vlan_group *grp = pi->vlan_grp;
  1358. rxq->stats.vlan_ex++;
  1359. if (likely(grp)) {
  1360. ret = vlan_gro_frags(&rxq->rspq.napi, grp,
  1361. ntohs(pkt->vlan));
  1362. goto stats;
  1363. }
  1364. }
  1365. ret = napi_gro_frags(&rxq->rspq.napi);
  1366. stats: if (ret == GRO_HELD)
  1367. rxq->stats.lro_pkts++;
  1368. else if (ret == GRO_MERGED || ret == GRO_MERGED_FREE)
  1369. rxq->stats.lro_merged++;
  1370. rxq->stats.pkts++;
  1371. rxq->stats.rx_cso++;
  1372. }
  1373. /**
  1374. * t4_ethrx_handler - process an ingress ethernet packet
  1375. * @q: the response queue that received the packet
  1376. * @rsp: the response queue descriptor holding the RX_PKT message
  1377. * @si: the gather list of packet fragments
  1378. *
  1379. * Process an ingress ethernet packet and deliver it to the stack.
  1380. */
  1381. int t4_ethrx_handler(struct sge_rspq *q, const __be64 *rsp,
  1382. const struct pkt_gl *si)
  1383. {
  1384. bool csum_ok;
  1385. struct sk_buff *skb;
  1386. struct port_info *pi;
  1387. const struct cpl_rx_pkt *pkt;
  1388. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1389. if (unlikely(*(u8 *)rsp == CPL_TRACE_PKT))
  1390. return handle_trace_pkt(q->adap, si);
  1391. pkt = (const struct cpl_rx_pkt *)rsp;
  1392. csum_ok = pkt->csum_calc && !pkt->err_vec;
  1393. if ((pkt->l2info & htonl(RXF_TCP)) &&
  1394. (q->netdev->features & NETIF_F_GRO) && csum_ok && !pkt->ip_frag) {
  1395. do_gro(rxq, si, pkt);
  1396. return 0;
  1397. }
  1398. skb = cxgb4_pktgl_to_skb(si, RX_PKT_SKB_LEN, RX_PULL_LEN);
  1399. if (unlikely(!skb)) {
  1400. t4_pktgl_free(si);
  1401. rxq->stats.rx_drops++;
  1402. return 0;
  1403. }
  1404. __skb_pull(skb, RX_PKT_PAD); /* remove ethernet header padding */
  1405. skb->protocol = eth_type_trans(skb, q->netdev);
  1406. skb_record_rx_queue(skb, q->idx);
  1407. if (skb->dev->features & NETIF_F_RXHASH)
  1408. skb->rxhash = (__force u32)pkt->rsshdr.hash_val;
  1409. pi = netdev_priv(skb->dev);
  1410. rxq->stats.pkts++;
  1411. if (csum_ok && (pi->rx_offload & RX_CSO) &&
  1412. (pkt->l2info & htonl(RXF_UDP | RXF_TCP))) {
  1413. if (!pkt->ip_frag)
  1414. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1415. else {
  1416. __sum16 c = (__force __sum16)pkt->csum;
  1417. skb->csum = csum_unfold(c);
  1418. skb->ip_summed = CHECKSUM_COMPLETE;
  1419. }
  1420. rxq->stats.rx_cso++;
  1421. } else
  1422. skb->ip_summed = CHECKSUM_NONE;
  1423. if (unlikely(pkt->vlan_ex)) {
  1424. struct vlan_group *grp = pi->vlan_grp;
  1425. rxq->stats.vlan_ex++;
  1426. if (likely(grp))
  1427. vlan_hwaccel_receive_skb(skb, grp, ntohs(pkt->vlan));
  1428. else
  1429. dev_kfree_skb_any(skb);
  1430. } else
  1431. netif_receive_skb(skb);
  1432. return 0;
  1433. }
  1434. /**
  1435. * restore_rx_bufs - put back a packet's Rx buffers
  1436. * @si: the packet gather list
  1437. * @q: the SGE free list
  1438. * @frags: number of FL buffers to restore
  1439. *
  1440. * Puts back on an FL the Rx buffers associated with @si. The buffers
  1441. * have already been unmapped and are left unmapped, we mark them so to
  1442. * prevent further unmapping attempts.
  1443. *
  1444. * This function undoes a series of @unmap_rx_buf calls when we find out
  1445. * that the current packet can't be processed right away afterall and we
  1446. * need to come back to it later. This is a very rare event and there's
  1447. * no effort to make this particularly efficient.
  1448. */
  1449. static void restore_rx_bufs(const struct pkt_gl *si, struct sge_fl *q,
  1450. int frags)
  1451. {
  1452. struct rx_sw_desc *d;
  1453. while (frags--) {
  1454. if (q->cidx == 0)
  1455. q->cidx = q->size - 1;
  1456. else
  1457. q->cidx--;
  1458. d = &q->sdesc[q->cidx];
  1459. d->page = si->frags[frags].page;
  1460. d->dma_addr |= RX_UNMAPPED_BUF;
  1461. q->avail++;
  1462. }
  1463. }
  1464. /**
  1465. * is_new_response - check if a response is newly written
  1466. * @r: the response descriptor
  1467. * @q: the response queue
  1468. *
  1469. * Returns true if a response descriptor contains a yet unprocessed
  1470. * response.
  1471. */
  1472. static inline bool is_new_response(const struct rsp_ctrl *r,
  1473. const struct sge_rspq *q)
  1474. {
  1475. return RSPD_GEN(r->type_gen) == q->gen;
  1476. }
  1477. /**
  1478. * rspq_next - advance to the next entry in a response queue
  1479. * @q: the queue
  1480. *
  1481. * Updates the state of a response queue to advance it to the next entry.
  1482. */
  1483. static inline void rspq_next(struct sge_rspq *q)
  1484. {
  1485. q->cur_desc = (void *)q->cur_desc + q->iqe_len;
  1486. if (unlikely(++q->cidx == q->size)) {
  1487. q->cidx = 0;
  1488. q->gen ^= 1;
  1489. q->cur_desc = q->desc;
  1490. }
  1491. }
  1492. /**
  1493. * process_responses - process responses from an SGE response queue
  1494. * @q: the ingress queue to process
  1495. * @budget: how many responses can be processed in this round
  1496. *
  1497. * Process responses from an SGE response queue up to the supplied budget.
  1498. * Responses include received packets as well as control messages from FW
  1499. * or HW.
  1500. *
  1501. * Additionally choose the interrupt holdoff time for the next interrupt
  1502. * on this queue. If the system is under memory shortage use a fairly
  1503. * long delay to help recovery.
  1504. */
  1505. static int process_responses(struct sge_rspq *q, int budget)
  1506. {
  1507. int ret, rsp_type;
  1508. int budget_left = budget;
  1509. const struct rsp_ctrl *rc;
  1510. struct sge_eth_rxq *rxq = container_of(q, struct sge_eth_rxq, rspq);
  1511. while (likely(budget_left)) {
  1512. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1513. if (!is_new_response(rc, q))
  1514. break;
  1515. rmb();
  1516. rsp_type = RSPD_TYPE(rc->type_gen);
  1517. if (likely(rsp_type == RSP_TYPE_FLBUF)) {
  1518. skb_frag_t *fp;
  1519. struct pkt_gl si;
  1520. const struct rx_sw_desc *rsd;
  1521. u32 len = ntohl(rc->pldbuflen_qid), bufsz, frags;
  1522. if (len & RSPD_NEWBUF) {
  1523. if (likely(q->offset > 0)) {
  1524. free_rx_bufs(q->adap, &rxq->fl, 1);
  1525. q->offset = 0;
  1526. }
  1527. len = RSPD_LEN(len);
  1528. }
  1529. si.tot_len = len;
  1530. /* gather packet fragments */
  1531. for (frags = 0, fp = si.frags; ; frags++, fp++) {
  1532. rsd = &rxq->fl.sdesc[rxq->fl.cidx];
  1533. bufsz = get_buf_size(rsd);
  1534. fp->page = rsd->page;
  1535. fp->page_offset = q->offset;
  1536. fp->size = min(bufsz, len);
  1537. len -= fp->size;
  1538. if (!len)
  1539. break;
  1540. unmap_rx_buf(q->adap, &rxq->fl);
  1541. }
  1542. /*
  1543. * Last buffer remains mapped so explicitly make it
  1544. * coherent for CPU access.
  1545. */
  1546. dma_sync_single_for_cpu(q->adap->pdev_dev,
  1547. get_buf_addr(rsd),
  1548. fp->size, DMA_FROM_DEVICE);
  1549. si.va = page_address(si.frags[0].page) +
  1550. si.frags[0].page_offset;
  1551. prefetch(si.va);
  1552. si.nfrags = frags + 1;
  1553. ret = q->handler(q, q->cur_desc, &si);
  1554. if (likely(ret == 0))
  1555. q->offset += ALIGN(fp->size, FL_ALIGN);
  1556. else
  1557. restore_rx_bufs(&si, &rxq->fl, frags);
  1558. } else if (likely(rsp_type == RSP_TYPE_CPL)) {
  1559. ret = q->handler(q, q->cur_desc, NULL);
  1560. } else {
  1561. ret = q->handler(q, (const __be64 *)rc, CXGB4_MSG_AN);
  1562. }
  1563. if (unlikely(ret)) {
  1564. /* couldn't process descriptor, back off for recovery */
  1565. q->next_intr_params = QINTR_TIMER_IDX(NOMEM_TMR_IDX);
  1566. break;
  1567. }
  1568. rspq_next(q);
  1569. budget_left--;
  1570. }
  1571. if (q->offset >= 0 && rxq->fl.size - rxq->fl.avail >= 16)
  1572. __refill_fl(q->adap, &rxq->fl);
  1573. return budget - budget_left;
  1574. }
  1575. /**
  1576. * napi_rx_handler - the NAPI handler for Rx processing
  1577. * @napi: the napi instance
  1578. * @budget: how many packets we can process in this round
  1579. *
  1580. * Handler for new data events when using NAPI. This does not need any
  1581. * locking or protection from interrupts as data interrupts are off at
  1582. * this point and other adapter interrupts do not interfere (the latter
  1583. * in not a concern at all with MSI-X as non-data interrupts then have
  1584. * a separate handler).
  1585. */
  1586. static int napi_rx_handler(struct napi_struct *napi, int budget)
  1587. {
  1588. unsigned int params;
  1589. struct sge_rspq *q = container_of(napi, struct sge_rspq, napi);
  1590. int work_done = process_responses(q, budget);
  1591. if (likely(work_done < budget)) {
  1592. napi_complete(napi);
  1593. params = q->next_intr_params;
  1594. q->next_intr_params = q->intr_params;
  1595. } else
  1596. params = QINTR_TIMER_IDX(7);
  1597. t4_write_reg(q->adap, MYPF_REG(SGE_PF_GTS), CIDXINC(work_done) |
  1598. INGRESSQID((u32)q->cntxt_id) | SEINTARM(params));
  1599. return work_done;
  1600. }
  1601. /*
  1602. * The MSI-X interrupt handler for an SGE response queue.
  1603. */
  1604. irqreturn_t t4_sge_intr_msix(int irq, void *cookie)
  1605. {
  1606. struct sge_rspq *q = cookie;
  1607. napi_schedule(&q->napi);
  1608. return IRQ_HANDLED;
  1609. }
  1610. /*
  1611. * Process the indirect interrupt entries in the interrupt queue and kick off
  1612. * NAPI for each queue that has generated an entry.
  1613. */
  1614. static unsigned int process_intrq(struct adapter *adap)
  1615. {
  1616. unsigned int credits;
  1617. const struct rsp_ctrl *rc;
  1618. struct sge_rspq *q = &adap->sge.intrq;
  1619. spin_lock(&adap->sge.intrq_lock);
  1620. for (credits = 0; ; credits++) {
  1621. rc = (void *)q->cur_desc + (q->iqe_len - sizeof(*rc));
  1622. if (!is_new_response(rc, q))
  1623. break;
  1624. rmb();
  1625. if (RSPD_TYPE(rc->type_gen) == RSP_TYPE_INTR) {
  1626. unsigned int qid = ntohl(rc->pldbuflen_qid);
  1627. napi_schedule(&adap->sge.ingr_map[qid]->napi);
  1628. }
  1629. rspq_next(q);
  1630. }
  1631. t4_write_reg(adap, MYPF_REG(SGE_PF_GTS), CIDXINC(credits) |
  1632. INGRESSQID(q->cntxt_id) | SEINTARM(q->intr_params));
  1633. spin_unlock(&adap->sge.intrq_lock);
  1634. return credits;
  1635. }
  1636. /*
  1637. * The MSI interrupt handler, which handles data events from SGE response queues
  1638. * as well as error and other async events as they all use the same MSI vector.
  1639. */
  1640. static irqreturn_t t4_intr_msi(int irq, void *cookie)
  1641. {
  1642. struct adapter *adap = cookie;
  1643. t4_slow_intr_handler(adap);
  1644. process_intrq(adap);
  1645. return IRQ_HANDLED;
  1646. }
  1647. /*
  1648. * Interrupt handler for legacy INTx interrupts.
  1649. * Handles data events from SGE response queues as well as error and other
  1650. * async events as they all use the same interrupt line.
  1651. */
  1652. static irqreturn_t t4_intr_intx(int irq, void *cookie)
  1653. {
  1654. struct adapter *adap = cookie;
  1655. t4_write_reg(adap, MYPF_REG(PCIE_PF_CLI), 0);
  1656. if (t4_slow_intr_handler(adap) | process_intrq(adap))
  1657. return IRQ_HANDLED;
  1658. return IRQ_NONE; /* probably shared interrupt */
  1659. }
  1660. /**
  1661. * t4_intr_handler - select the top-level interrupt handler
  1662. * @adap: the adapter
  1663. *
  1664. * Selects the top-level interrupt handler based on the type of interrupts
  1665. * (MSI-X, MSI, or INTx).
  1666. */
  1667. irq_handler_t t4_intr_handler(struct adapter *adap)
  1668. {
  1669. if (adap->flags & USING_MSIX)
  1670. return t4_sge_intr_msix;
  1671. if (adap->flags & USING_MSI)
  1672. return t4_intr_msi;
  1673. return t4_intr_intx;
  1674. }
  1675. static void sge_rx_timer_cb(unsigned long data)
  1676. {
  1677. unsigned long m;
  1678. unsigned int i, cnt[2];
  1679. struct adapter *adap = (struct adapter *)data;
  1680. struct sge *s = &adap->sge;
  1681. for (i = 0; i < ARRAY_SIZE(s->starving_fl); i++)
  1682. for (m = s->starving_fl[i]; m; m &= m - 1) {
  1683. struct sge_eth_rxq *rxq;
  1684. unsigned int id = __ffs(m) + i * BITS_PER_LONG;
  1685. struct sge_fl *fl = s->egr_map[id];
  1686. clear_bit(id, s->starving_fl);
  1687. smp_mb__after_clear_bit();
  1688. if (fl_starving(fl)) {
  1689. rxq = container_of(fl, struct sge_eth_rxq, fl);
  1690. if (napi_reschedule(&rxq->rspq.napi))
  1691. fl->starving++;
  1692. else
  1693. set_bit(id, s->starving_fl);
  1694. }
  1695. }
  1696. t4_write_reg(adap, SGE_DEBUG_INDEX, 13);
  1697. cnt[0] = t4_read_reg(adap, SGE_DEBUG_DATA_HIGH);
  1698. cnt[1] = t4_read_reg(adap, SGE_DEBUG_DATA_LOW);
  1699. for (i = 0; i < 2; i++)
  1700. if (cnt[i] >= s->starve_thres) {
  1701. if (s->idma_state[i] || cnt[i] == 0xffffffff)
  1702. continue;
  1703. s->idma_state[i] = 1;
  1704. t4_write_reg(adap, SGE_DEBUG_INDEX, 11);
  1705. m = t4_read_reg(adap, SGE_DEBUG_DATA_LOW) >> (i * 16);
  1706. dev_warn(adap->pdev_dev,
  1707. "SGE idma%u starvation detected for "
  1708. "queue %lu\n", i, m & 0xffff);
  1709. } else if (s->idma_state[i])
  1710. s->idma_state[i] = 0;
  1711. mod_timer(&s->rx_timer, jiffies + RX_QCHECK_PERIOD);
  1712. }
  1713. static void sge_tx_timer_cb(unsigned long data)
  1714. {
  1715. unsigned long m;
  1716. unsigned int i, budget;
  1717. struct adapter *adap = (struct adapter *)data;
  1718. struct sge *s = &adap->sge;
  1719. for (i = 0; i < ARRAY_SIZE(s->txq_maperr); i++)
  1720. for (m = s->txq_maperr[i]; m; m &= m - 1) {
  1721. unsigned long id = __ffs(m) + i * BITS_PER_LONG;
  1722. struct sge_ofld_txq *txq = s->egr_map[id];
  1723. clear_bit(id, s->txq_maperr);
  1724. tasklet_schedule(&txq->qresume_tsk);
  1725. }
  1726. budget = MAX_TIMER_TX_RECLAIM;
  1727. i = s->ethtxq_rover;
  1728. do {
  1729. struct sge_eth_txq *q = &s->ethtxq[i];
  1730. if (q->q.in_use &&
  1731. time_after_eq(jiffies, q->txq->trans_start + HZ / 100) &&
  1732. __netif_tx_trylock(q->txq)) {
  1733. int avail = reclaimable(&q->q);
  1734. if (avail) {
  1735. if (avail > budget)
  1736. avail = budget;
  1737. free_tx_desc(adap, &q->q, avail, true);
  1738. q->q.in_use -= avail;
  1739. budget -= avail;
  1740. }
  1741. __netif_tx_unlock(q->txq);
  1742. }
  1743. if (++i >= s->ethqsets)
  1744. i = 0;
  1745. } while (budget && i != s->ethtxq_rover);
  1746. s->ethtxq_rover = i;
  1747. mod_timer(&s->tx_timer, jiffies + (budget ? TX_QCHECK_PERIOD : 2));
  1748. }
  1749. int t4_sge_alloc_rxq(struct adapter *adap, struct sge_rspq *iq, bool fwevtq,
  1750. struct net_device *dev, int intr_idx,
  1751. struct sge_fl *fl, rspq_handler_t hnd)
  1752. {
  1753. int ret, flsz = 0;
  1754. struct fw_iq_cmd c;
  1755. struct port_info *pi = netdev_priv(dev);
  1756. /* Size needs to be multiple of 16, including status entry. */
  1757. iq->size = roundup(iq->size, 16);
  1758. iq->desc = alloc_ring(adap->pdev_dev, iq->size, iq->iqe_len, 0,
  1759. &iq->phys_addr, NULL, 0);
  1760. if (!iq->desc)
  1761. return -ENOMEM;
  1762. memset(&c, 0, sizeof(c));
  1763. c.op_to_vfn = htonl(FW_CMD_OP(FW_IQ_CMD) | FW_CMD_REQUEST |
  1764. FW_CMD_WRITE | FW_CMD_EXEC |
  1765. FW_IQ_CMD_PFN(0) | FW_IQ_CMD_VFN(0));
  1766. c.alloc_to_len16 = htonl(FW_IQ_CMD_ALLOC | FW_IQ_CMD_IQSTART(1) |
  1767. FW_LEN16(c));
  1768. c.type_to_iqandstindex = htonl(FW_IQ_CMD_TYPE(FW_IQ_TYPE_FL_INT_CAP) |
  1769. FW_IQ_CMD_IQASYNCH(fwevtq) | FW_IQ_CMD_VIID(pi->viid) |
  1770. FW_IQ_CMD_IQANDST(intr_idx < 0) | FW_IQ_CMD_IQANUD(1) |
  1771. FW_IQ_CMD_IQANDSTINDEX(intr_idx >= 0 ? intr_idx :
  1772. -intr_idx - 1));
  1773. c.iqdroprss_to_iqesize = htons(FW_IQ_CMD_IQPCIECH(pi->tx_chan) |
  1774. FW_IQ_CMD_IQGTSMODE |
  1775. FW_IQ_CMD_IQINTCNTTHRESH(iq->pktcnt_idx) |
  1776. FW_IQ_CMD_IQESIZE(ilog2(iq->iqe_len) - 4));
  1777. c.iqsize = htons(iq->size);
  1778. c.iqaddr = cpu_to_be64(iq->phys_addr);
  1779. if (fl) {
  1780. fl->size = roundup(fl->size, 8);
  1781. fl->desc = alloc_ring(adap->pdev_dev, fl->size, sizeof(__be64),
  1782. sizeof(struct rx_sw_desc), &fl->addr,
  1783. &fl->sdesc, STAT_LEN);
  1784. if (!fl->desc)
  1785. goto fl_nomem;
  1786. flsz = fl->size / 8 + STAT_LEN / sizeof(struct tx_desc);
  1787. c.iqns_to_fl0congen = htonl(FW_IQ_CMD_FL0PACKEN |
  1788. FW_IQ_CMD_FL0PADEN);
  1789. c.fl0dcaen_to_fl0cidxfthresh = htons(FW_IQ_CMD_FL0FBMIN(2) |
  1790. FW_IQ_CMD_FL0FBMAX(3));
  1791. c.fl0size = htons(flsz);
  1792. c.fl0addr = cpu_to_be64(fl->addr);
  1793. }
  1794. ret = t4_wr_mbox(adap, 0, &c, sizeof(c), &c);
  1795. if (ret)
  1796. goto err;
  1797. netif_napi_add(dev, &iq->napi, napi_rx_handler, 64);
  1798. iq->cur_desc = iq->desc;
  1799. iq->cidx = 0;
  1800. iq->gen = 1;
  1801. iq->next_intr_params = iq->intr_params;
  1802. iq->cntxt_id = ntohs(c.iqid);
  1803. iq->abs_id = ntohs(c.physiqid);
  1804. iq->size--; /* subtract status entry */
  1805. iq->adap = adap;
  1806. iq->netdev = dev;
  1807. iq->handler = hnd;
  1808. /* set offset to -1 to distinguish ingress queues without FL */
  1809. iq->offset = fl ? 0 : -1;
  1810. adap->sge.ingr_map[iq->cntxt_id] = iq;
  1811. if (fl) {
  1812. fl->cntxt_id = ntohs(c.fl0id);
  1813. fl->avail = fl->pend_cred = 0;
  1814. fl->pidx = fl->cidx = 0;
  1815. fl->alloc_failed = fl->large_alloc_failed = fl->starving = 0;
  1816. adap->sge.egr_map[fl->cntxt_id] = fl;
  1817. refill_fl(adap, fl, fl_cap(fl), GFP_KERNEL);
  1818. }
  1819. return 0;
  1820. fl_nomem:
  1821. ret = -ENOMEM;
  1822. err:
  1823. if (iq->desc) {
  1824. dma_free_coherent(adap->pdev_dev, iq->size * iq->iqe_len,
  1825. iq->desc, iq->phys_addr);
  1826. iq->desc = NULL;
  1827. }
  1828. if (fl && fl->desc) {
  1829. kfree(fl->sdesc);
  1830. fl->sdesc = NULL;
  1831. dma_free_coherent(adap->pdev_dev, flsz * sizeof(struct tx_desc),
  1832. fl->desc, fl->addr);
  1833. fl->desc = NULL;
  1834. }
  1835. return ret;
  1836. }
  1837. static void init_txq(struct adapter *adap, struct sge_txq *q, unsigned int id)
  1838. {
  1839. q->in_use = 0;
  1840. q->cidx = q->pidx = 0;
  1841. q->stops = q->restarts = 0;
  1842. q->stat = (void *)&q->desc[q->size];
  1843. q->cntxt_id = id;
  1844. adap->sge.egr_map[id] = q;
  1845. }
  1846. int t4_sge_alloc_eth_txq(struct adapter *adap, struct sge_eth_txq *txq,
  1847. struct net_device *dev, struct netdev_queue *netdevq,
  1848. unsigned int iqid)
  1849. {
  1850. int ret, nentries;
  1851. struct fw_eq_eth_cmd c;
  1852. struct port_info *pi = netdev_priv(dev);
  1853. /* Add status entries */
  1854. nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
  1855. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  1856. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  1857. &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN);
  1858. if (!txq->q.desc)
  1859. return -ENOMEM;
  1860. memset(&c, 0, sizeof(c));
  1861. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_ETH_CMD) | FW_CMD_REQUEST |
  1862. FW_CMD_WRITE | FW_CMD_EXEC |
  1863. FW_EQ_ETH_CMD_PFN(0) | FW_EQ_ETH_CMD_VFN(0));
  1864. c.alloc_to_len16 = htonl(FW_EQ_ETH_CMD_ALLOC |
  1865. FW_EQ_ETH_CMD_EQSTART | FW_LEN16(c));
  1866. c.viid_pkd = htonl(FW_EQ_ETH_CMD_VIID(pi->viid));
  1867. c.fetchszm_to_iqid = htonl(FW_EQ_ETH_CMD_HOSTFCMODE(2) |
  1868. FW_EQ_ETH_CMD_PCIECHN(pi->tx_chan) |
  1869. FW_EQ_ETH_CMD_IQID(iqid));
  1870. c.dcaen_to_eqsize = htonl(FW_EQ_ETH_CMD_FBMIN(2) |
  1871. FW_EQ_ETH_CMD_FBMAX(3) |
  1872. FW_EQ_ETH_CMD_CIDXFTHRESH(5) |
  1873. FW_EQ_ETH_CMD_EQSIZE(nentries));
  1874. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  1875. ret = t4_wr_mbox(adap, 0, &c, sizeof(c), &c);
  1876. if (ret) {
  1877. kfree(txq->q.sdesc);
  1878. txq->q.sdesc = NULL;
  1879. dma_free_coherent(adap->pdev_dev,
  1880. nentries * sizeof(struct tx_desc),
  1881. txq->q.desc, txq->q.phys_addr);
  1882. txq->q.desc = NULL;
  1883. return ret;
  1884. }
  1885. init_txq(adap, &txq->q, FW_EQ_ETH_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  1886. txq->txq = netdevq;
  1887. txq->tso = txq->tx_cso = txq->vlan_ins = 0;
  1888. txq->mapping_err = 0;
  1889. return 0;
  1890. }
  1891. int t4_sge_alloc_ctrl_txq(struct adapter *adap, struct sge_ctrl_txq *txq,
  1892. struct net_device *dev, unsigned int iqid,
  1893. unsigned int cmplqid)
  1894. {
  1895. int ret, nentries;
  1896. struct fw_eq_ctrl_cmd c;
  1897. struct port_info *pi = netdev_priv(dev);
  1898. /* Add status entries */
  1899. nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
  1900. txq->q.desc = alloc_ring(adap->pdev_dev, nentries,
  1901. sizeof(struct tx_desc), 0, &txq->q.phys_addr,
  1902. NULL, 0);
  1903. if (!txq->q.desc)
  1904. return -ENOMEM;
  1905. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_CTRL_CMD) | FW_CMD_REQUEST |
  1906. FW_CMD_WRITE | FW_CMD_EXEC |
  1907. FW_EQ_CTRL_CMD_PFN(0) | FW_EQ_CTRL_CMD_VFN(0));
  1908. c.alloc_to_len16 = htonl(FW_EQ_CTRL_CMD_ALLOC |
  1909. FW_EQ_CTRL_CMD_EQSTART | FW_LEN16(c));
  1910. c.cmpliqid_eqid = htonl(FW_EQ_CTRL_CMD_CMPLIQID(cmplqid));
  1911. c.physeqid_pkd = htonl(0);
  1912. c.fetchszm_to_iqid = htonl(FW_EQ_CTRL_CMD_HOSTFCMODE(2) |
  1913. FW_EQ_CTRL_CMD_PCIECHN(pi->tx_chan) |
  1914. FW_EQ_CTRL_CMD_IQID(iqid));
  1915. c.dcaen_to_eqsize = htonl(FW_EQ_CTRL_CMD_FBMIN(2) |
  1916. FW_EQ_CTRL_CMD_FBMAX(3) |
  1917. FW_EQ_CTRL_CMD_CIDXFTHRESH(5) |
  1918. FW_EQ_CTRL_CMD_EQSIZE(nentries));
  1919. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  1920. ret = t4_wr_mbox(adap, 0, &c, sizeof(c), &c);
  1921. if (ret) {
  1922. dma_free_coherent(adap->pdev_dev,
  1923. nentries * sizeof(struct tx_desc),
  1924. txq->q.desc, txq->q.phys_addr);
  1925. txq->q.desc = NULL;
  1926. return ret;
  1927. }
  1928. init_txq(adap, &txq->q, FW_EQ_CTRL_CMD_EQID_GET(ntohl(c.cmpliqid_eqid)));
  1929. txq->adap = adap;
  1930. skb_queue_head_init(&txq->sendq);
  1931. tasklet_init(&txq->qresume_tsk, restart_ctrlq, (unsigned long)txq);
  1932. txq->full = 0;
  1933. return 0;
  1934. }
  1935. int t4_sge_alloc_ofld_txq(struct adapter *adap, struct sge_ofld_txq *txq,
  1936. struct net_device *dev, unsigned int iqid)
  1937. {
  1938. int ret, nentries;
  1939. struct fw_eq_ofld_cmd c;
  1940. struct port_info *pi = netdev_priv(dev);
  1941. /* Add status entries */
  1942. nentries = txq->q.size + STAT_LEN / sizeof(struct tx_desc);
  1943. txq->q.desc = alloc_ring(adap->pdev_dev, txq->q.size,
  1944. sizeof(struct tx_desc), sizeof(struct tx_sw_desc),
  1945. &txq->q.phys_addr, &txq->q.sdesc, STAT_LEN);
  1946. if (!txq->q.desc)
  1947. return -ENOMEM;
  1948. memset(&c, 0, sizeof(c));
  1949. c.op_to_vfn = htonl(FW_CMD_OP(FW_EQ_OFLD_CMD) | FW_CMD_REQUEST |
  1950. FW_CMD_WRITE | FW_CMD_EXEC |
  1951. FW_EQ_OFLD_CMD_PFN(0) | FW_EQ_OFLD_CMD_VFN(0));
  1952. c.alloc_to_len16 = htonl(FW_EQ_OFLD_CMD_ALLOC |
  1953. FW_EQ_OFLD_CMD_EQSTART | FW_LEN16(c));
  1954. c.fetchszm_to_iqid = htonl(FW_EQ_OFLD_CMD_HOSTFCMODE(2) |
  1955. FW_EQ_OFLD_CMD_PCIECHN(pi->tx_chan) |
  1956. FW_EQ_OFLD_CMD_IQID(iqid));
  1957. c.dcaen_to_eqsize = htonl(FW_EQ_OFLD_CMD_FBMIN(2) |
  1958. FW_EQ_OFLD_CMD_FBMAX(3) |
  1959. FW_EQ_OFLD_CMD_CIDXFTHRESH(5) |
  1960. FW_EQ_OFLD_CMD_EQSIZE(nentries));
  1961. c.eqaddr = cpu_to_be64(txq->q.phys_addr);
  1962. ret = t4_wr_mbox(adap, 0, &c, sizeof(c), &c);
  1963. if (ret) {
  1964. kfree(txq->q.sdesc);
  1965. txq->q.sdesc = NULL;
  1966. dma_free_coherent(adap->pdev_dev,
  1967. nentries * sizeof(struct tx_desc),
  1968. txq->q.desc, txq->q.phys_addr);
  1969. txq->q.desc = NULL;
  1970. return ret;
  1971. }
  1972. init_txq(adap, &txq->q, FW_EQ_OFLD_CMD_EQID_GET(ntohl(c.eqid_pkd)));
  1973. txq->adap = adap;
  1974. skb_queue_head_init(&txq->sendq);
  1975. tasklet_init(&txq->qresume_tsk, restart_ofldq, (unsigned long)txq);
  1976. txq->full = 0;
  1977. txq->mapping_err = 0;
  1978. return 0;
  1979. }
  1980. static void free_txq(struct adapter *adap, struct sge_txq *q)
  1981. {
  1982. dma_free_coherent(adap->pdev_dev,
  1983. q->size * sizeof(struct tx_desc) + STAT_LEN,
  1984. q->desc, q->phys_addr);
  1985. q->cntxt_id = 0;
  1986. q->sdesc = NULL;
  1987. q->desc = NULL;
  1988. }
  1989. static void free_rspq_fl(struct adapter *adap, struct sge_rspq *rq,
  1990. struct sge_fl *fl)
  1991. {
  1992. unsigned int fl_id = fl ? fl->cntxt_id : 0xffff;
  1993. adap->sge.ingr_map[rq->cntxt_id] = NULL;
  1994. t4_iq_free(adap, 0, 0, 0, FW_IQ_TYPE_FL_INT_CAP, rq->cntxt_id, fl_id,
  1995. 0xffff);
  1996. dma_free_coherent(adap->pdev_dev, (rq->size + 1) * rq->iqe_len,
  1997. rq->desc, rq->phys_addr);
  1998. netif_napi_del(&rq->napi);
  1999. rq->netdev = NULL;
  2000. rq->cntxt_id = rq->abs_id = 0;
  2001. rq->desc = NULL;
  2002. if (fl) {
  2003. free_rx_bufs(adap, fl, fl->avail);
  2004. dma_free_coherent(adap->pdev_dev, fl->size * 8 + STAT_LEN,
  2005. fl->desc, fl->addr);
  2006. kfree(fl->sdesc);
  2007. fl->sdesc = NULL;
  2008. fl->cntxt_id = 0;
  2009. fl->desc = NULL;
  2010. }
  2011. }
  2012. /**
  2013. * t4_free_sge_resources - free SGE resources
  2014. * @adap: the adapter
  2015. *
  2016. * Frees resources used by the SGE queue sets.
  2017. */
  2018. void t4_free_sge_resources(struct adapter *adap)
  2019. {
  2020. int i;
  2021. struct sge_eth_rxq *eq = adap->sge.ethrxq;
  2022. struct sge_eth_txq *etq = adap->sge.ethtxq;
  2023. struct sge_ofld_rxq *oq = adap->sge.ofldrxq;
  2024. /* clean up Ethernet Tx/Rx queues */
  2025. for (i = 0; i < adap->sge.ethqsets; i++, eq++, etq++) {
  2026. if (eq->rspq.desc)
  2027. free_rspq_fl(adap, &eq->rspq, &eq->fl);
  2028. if (etq->q.desc) {
  2029. t4_eth_eq_free(adap, 0, 0, 0, etq->q.cntxt_id);
  2030. free_tx_desc(adap, &etq->q, etq->q.in_use, true);
  2031. kfree(etq->q.sdesc);
  2032. free_txq(adap, &etq->q);
  2033. }
  2034. }
  2035. /* clean up RDMA and iSCSI Rx queues */
  2036. for (i = 0; i < adap->sge.ofldqsets; i++, oq++) {
  2037. if (oq->rspq.desc)
  2038. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2039. }
  2040. for (i = 0, oq = adap->sge.rdmarxq; i < adap->sge.rdmaqs; i++, oq++) {
  2041. if (oq->rspq.desc)
  2042. free_rspq_fl(adap, &oq->rspq, &oq->fl);
  2043. }
  2044. /* clean up offload Tx queues */
  2045. for (i = 0; i < ARRAY_SIZE(adap->sge.ofldtxq); i++) {
  2046. struct sge_ofld_txq *q = &adap->sge.ofldtxq[i];
  2047. if (q->q.desc) {
  2048. tasklet_kill(&q->qresume_tsk);
  2049. t4_ofld_eq_free(adap, 0, 0, 0, q->q.cntxt_id);
  2050. free_tx_desc(adap, &q->q, q->q.in_use, false);
  2051. kfree(q->q.sdesc);
  2052. __skb_queue_purge(&q->sendq);
  2053. free_txq(adap, &q->q);
  2054. }
  2055. }
  2056. /* clean up control Tx queues */
  2057. for (i = 0; i < ARRAY_SIZE(adap->sge.ctrlq); i++) {
  2058. struct sge_ctrl_txq *cq = &adap->sge.ctrlq[i];
  2059. if (cq->q.desc) {
  2060. tasklet_kill(&cq->qresume_tsk);
  2061. t4_ctrl_eq_free(adap, 0, 0, 0, cq->q.cntxt_id);
  2062. __skb_queue_purge(&cq->sendq);
  2063. free_txq(adap, &cq->q);
  2064. }
  2065. }
  2066. if (adap->sge.fw_evtq.desc)
  2067. free_rspq_fl(adap, &adap->sge.fw_evtq, NULL);
  2068. if (adap->sge.intrq.desc)
  2069. free_rspq_fl(adap, &adap->sge.intrq, NULL);
  2070. /* clear the reverse egress queue map */
  2071. memset(adap->sge.egr_map, 0, sizeof(adap->sge.egr_map));
  2072. }
  2073. void t4_sge_start(struct adapter *adap)
  2074. {
  2075. adap->sge.ethtxq_rover = 0;
  2076. mod_timer(&adap->sge.rx_timer, jiffies + RX_QCHECK_PERIOD);
  2077. mod_timer(&adap->sge.tx_timer, jiffies + TX_QCHECK_PERIOD);
  2078. }
  2079. /**
  2080. * t4_sge_stop - disable SGE operation
  2081. * @adap: the adapter
  2082. *
  2083. * Stop tasklets and timers associated with the DMA engine. Note that
  2084. * this is effective only if measures have been taken to disable any HW
  2085. * events that may restart them.
  2086. */
  2087. void t4_sge_stop(struct adapter *adap)
  2088. {
  2089. int i;
  2090. struct sge *s = &adap->sge;
  2091. if (in_interrupt()) /* actions below require waiting */
  2092. return;
  2093. if (s->rx_timer.function)
  2094. del_timer_sync(&s->rx_timer);
  2095. if (s->tx_timer.function)
  2096. del_timer_sync(&s->tx_timer);
  2097. for (i = 0; i < ARRAY_SIZE(s->ofldtxq); i++) {
  2098. struct sge_ofld_txq *q = &s->ofldtxq[i];
  2099. if (q->q.desc)
  2100. tasklet_kill(&q->qresume_tsk);
  2101. }
  2102. for (i = 0; i < ARRAY_SIZE(s->ctrlq); i++) {
  2103. struct sge_ctrl_txq *cq = &s->ctrlq[i];
  2104. if (cq->q.desc)
  2105. tasklet_kill(&cq->qresume_tsk);
  2106. }
  2107. }
  2108. /**
  2109. * t4_sge_init - initialize SGE
  2110. * @adap: the adapter
  2111. *
  2112. * Performs SGE initialization needed every time after a chip reset.
  2113. * We do not initialize any of the queues here, instead the driver
  2114. * top-level must request them individually.
  2115. */
  2116. void t4_sge_init(struct adapter *adap)
  2117. {
  2118. struct sge *s = &adap->sge;
  2119. unsigned int fl_align_log = ilog2(FL_ALIGN);
  2120. t4_set_reg_field(adap, SGE_CONTROL, PKTSHIFT_MASK |
  2121. INGPADBOUNDARY_MASK | EGRSTATUSPAGESIZE,
  2122. INGPADBOUNDARY(fl_align_log - 5) | PKTSHIFT(2) |
  2123. RXPKTCPLMODE |
  2124. (STAT_LEN == 128 ? EGRSTATUSPAGESIZE : 0));
  2125. t4_set_reg_field(adap, SGE_HOST_PAGE_SIZE, HOSTPAGESIZEPF0_MASK,
  2126. HOSTPAGESIZEPF0(PAGE_SHIFT - 10));
  2127. t4_write_reg(adap, SGE_FL_BUFFER_SIZE0, PAGE_SIZE);
  2128. #if FL_PG_ORDER > 0
  2129. t4_write_reg(adap, SGE_FL_BUFFER_SIZE1, PAGE_SIZE << FL_PG_ORDER);
  2130. #endif
  2131. t4_write_reg(adap, SGE_INGRESS_RX_THRESHOLD,
  2132. THRESHOLD_0(s->counter_val[0]) |
  2133. THRESHOLD_1(s->counter_val[1]) |
  2134. THRESHOLD_2(s->counter_val[2]) |
  2135. THRESHOLD_3(s->counter_val[3]));
  2136. t4_write_reg(adap, SGE_TIMER_VALUE_0_AND_1,
  2137. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[0])) |
  2138. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[1])));
  2139. t4_write_reg(adap, SGE_TIMER_VALUE_2_AND_3,
  2140. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[2])) |
  2141. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[3])));
  2142. t4_write_reg(adap, SGE_TIMER_VALUE_4_AND_5,
  2143. TIMERVALUE0(us_to_core_ticks(adap, s->timer_val[4])) |
  2144. TIMERVALUE1(us_to_core_ticks(adap, s->timer_val[5])));
  2145. setup_timer(&s->rx_timer, sge_rx_timer_cb, (unsigned long)adap);
  2146. setup_timer(&s->tx_timer, sge_tx_timer_cb, (unsigned long)adap);
  2147. s->starve_thres = core_ticks_per_usec(adap) * 1000000; /* 1 s */
  2148. s->idma_state[0] = s->idma_state[1] = 0;
  2149. spin_lock_init(&s->intrq_lock);
  2150. }