sge.c 66 KB

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