sge.c 66 KB

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