sge.c 66 KB

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