sge.c 74 KB

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