sge.c 66 KB

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