sge.c 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210
  1. /*****************************************************************************
  2. * *
  3. * File: sge.c *
  4. * $Revision: 1.26 $ *
  5. * $Date: 2005/06/21 18:29:48 $ *
  6. * Description: *
  7. * DMA engine. *
  8. * part of the Chelsio 10Gb Ethernet Driver. *
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License, version 2, as *
  12. * published by the Free Software Foundation. *
  13. * *
  14. * You should have received a copy of the GNU General Public License along *
  15. * with this program; if not, write to the Free Software Foundation, Inc., *
  16. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  17. * *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED *
  19. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF *
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *
  21. * *
  22. * http://www.chelsio.com *
  23. * *
  24. * Copyright (c) 2003 - 2005 Chelsio Communications, Inc. *
  25. * All rights reserved. *
  26. * *
  27. * Maintainers: maintainers@chelsio.com *
  28. * *
  29. * Authors: Dimitrios Michailidis <dm@chelsio.com> *
  30. * Tina Yang <tainay@chelsio.com> *
  31. * Felix Marti <felix@chelsio.com> *
  32. * Scott Bardone <sbardone@chelsio.com> *
  33. * Kurt Ottaway <kottaway@chelsio.com> *
  34. * Frank DiMambro <frank@chelsio.com> *
  35. * *
  36. * History: *
  37. * *
  38. ****************************************************************************/
  39. #include "common.h"
  40. #include <linux/types.h>
  41. #include <linux/errno.h>
  42. #include <linux/pci.h>
  43. #include <linux/ktime.h>
  44. #include <linux/netdevice.h>
  45. #include <linux/etherdevice.h>
  46. #include <linux/if_vlan.h>
  47. #include <linux/skbuff.h>
  48. #include <linux/init.h>
  49. #include <linux/mm.h>
  50. #include <linux/tcp.h>
  51. #include <linux/ip.h>
  52. #include <linux/in.h>
  53. #include <linux/if_arp.h>
  54. #include "cpl5_cmd.h"
  55. #include "sge.h"
  56. #include "regs.h"
  57. #include "espi.h"
  58. /* This belongs in if_ether.h */
  59. #define ETH_P_CPL5 0xf
  60. #define SGE_CMDQ_N 2
  61. #define SGE_FREELQ_N 2
  62. #define SGE_CMDQ0_E_N 1024
  63. #define SGE_CMDQ1_E_N 128
  64. #define SGE_FREEL_SIZE 4096
  65. #define SGE_JUMBO_FREEL_SIZE 512
  66. #define SGE_FREEL_REFILL_THRESH 16
  67. #define SGE_RESPQ_E_N 1024
  68. #define SGE_INTRTIMER_NRES 1000
  69. #define SGE_RX_COPY_THRES 256
  70. #define SGE_RX_SM_BUF_SIZE 1536
  71. #define SGE_TX_DESC_MAX_PLEN 16384
  72. # define SGE_RX_DROP_THRES 2
  73. #define SGE_RESPQ_REPLENISH_THRES (SGE_RESPQ_E_N / 4)
  74. /*
  75. * Period of the TX buffer reclaim timer. This timer does not need to run
  76. * frequently as TX buffers are usually reclaimed by new TX packets.
  77. */
  78. #define TX_RECLAIM_PERIOD (HZ / 4)
  79. #define M_CMD_LEN 0x7fffffff
  80. #define V_CMD_LEN(v) (v)
  81. #define G_CMD_LEN(v) ((v) & M_CMD_LEN)
  82. #define V_CMD_GEN1(v) ((v) << 31)
  83. #define V_CMD_GEN2(v) (v)
  84. #define F_CMD_DATAVALID (1 << 1)
  85. #define F_CMD_SOP (1 << 2)
  86. #define V_CMD_EOP(v) ((v) << 3)
  87. /*
  88. * Command queue, receive buffer list, and response queue descriptors.
  89. */
  90. #if defined(__BIG_ENDIAN_BITFIELD)
  91. struct cmdQ_e {
  92. u32 addr_lo;
  93. u32 len_gen;
  94. u32 flags;
  95. u32 addr_hi;
  96. };
  97. struct freelQ_e {
  98. u32 addr_lo;
  99. u32 len_gen;
  100. u32 gen2;
  101. u32 addr_hi;
  102. };
  103. struct respQ_e {
  104. u32 Qsleeping : 4;
  105. u32 Cmdq1CreditReturn : 5;
  106. u32 Cmdq1DmaComplete : 5;
  107. u32 Cmdq0CreditReturn : 5;
  108. u32 Cmdq0DmaComplete : 5;
  109. u32 FreelistQid : 2;
  110. u32 CreditValid : 1;
  111. u32 DataValid : 1;
  112. u32 Offload : 1;
  113. u32 Eop : 1;
  114. u32 Sop : 1;
  115. u32 GenerationBit : 1;
  116. u32 BufferLength;
  117. };
  118. #elif defined(__LITTLE_ENDIAN_BITFIELD)
  119. struct cmdQ_e {
  120. u32 len_gen;
  121. u32 addr_lo;
  122. u32 addr_hi;
  123. u32 flags;
  124. };
  125. struct freelQ_e {
  126. u32 len_gen;
  127. u32 addr_lo;
  128. u32 addr_hi;
  129. u32 gen2;
  130. };
  131. struct respQ_e {
  132. u32 BufferLength;
  133. u32 GenerationBit : 1;
  134. u32 Sop : 1;
  135. u32 Eop : 1;
  136. u32 Offload : 1;
  137. u32 DataValid : 1;
  138. u32 CreditValid : 1;
  139. u32 FreelistQid : 2;
  140. u32 Cmdq0DmaComplete : 5;
  141. u32 Cmdq0CreditReturn : 5;
  142. u32 Cmdq1DmaComplete : 5;
  143. u32 Cmdq1CreditReturn : 5;
  144. u32 Qsleeping : 4;
  145. } ;
  146. #endif
  147. /*
  148. * SW Context Command and Freelist Queue Descriptors
  149. */
  150. struct cmdQ_ce {
  151. struct sk_buff *skb;
  152. DECLARE_PCI_UNMAP_ADDR(dma_addr);
  153. DECLARE_PCI_UNMAP_LEN(dma_len);
  154. };
  155. struct freelQ_ce {
  156. struct sk_buff *skb;
  157. DECLARE_PCI_UNMAP_ADDR(dma_addr);
  158. DECLARE_PCI_UNMAP_LEN(dma_len);
  159. };
  160. /*
  161. * SW command, freelist and response rings
  162. */
  163. struct cmdQ {
  164. unsigned long status; /* HW DMA fetch status */
  165. unsigned int in_use; /* # of in-use command descriptors */
  166. unsigned int size; /* # of descriptors */
  167. unsigned int processed; /* total # of descs HW has processed */
  168. unsigned int cleaned; /* total # of descs SW has reclaimed */
  169. unsigned int stop_thres; /* SW TX queue suspend threshold */
  170. u16 pidx; /* producer index (SW) */
  171. u16 cidx; /* consumer index (HW) */
  172. u8 genbit; /* current generation (=valid) bit */
  173. u8 sop; /* is next entry start of packet? */
  174. struct cmdQ_e *entries; /* HW command descriptor Q */
  175. struct cmdQ_ce *centries; /* SW command context descriptor Q */
  176. dma_addr_t dma_addr; /* DMA addr HW command descriptor Q */
  177. spinlock_t lock; /* Lock to protect cmdQ enqueuing */
  178. };
  179. struct freelQ {
  180. unsigned int credits; /* # of available RX buffers */
  181. unsigned int size; /* free list capacity */
  182. u16 pidx; /* producer index (SW) */
  183. u16 cidx; /* consumer index (HW) */
  184. u16 rx_buffer_size; /* Buffer size on this free list */
  185. u16 dma_offset; /* DMA offset to align IP headers */
  186. u16 recycleq_idx; /* skb recycle q to use */
  187. u8 genbit; /* current generation (=valid) bit */
  188. struct freelQ_e *entries; /* HW freelist descriptor Q */
  189. struct freelQ_ce *centries; /* SW freelist context descriptor Q */
  190. dma_addr_t dma_addr; /* DMA addr HW freelist descriptor Q */
  191. };
  192. struct respQ {
  193. unsigned int credits; /* credits to be returned to SGE */
  194. unsigned int size; /* # of response Q descriptors */
  195. u16 cidx; /* consumer index (SW) */
  196. u8 genbit; /* current generation(=valid) bit */
  197. struct respQ_e *entries; /* HW response descriptor Q */
  198. dma_addr_t dma_addr; /* DMA addr HW response descriptor Q */
  199. };
  200. /* Bit flags for cmdQ.status */
  201. enum {
  202. CMDQ_STAT_RUNNING = 1, /* fetch engine is running */
  203. CMDQ_STAT_LAST_PKT_DB = 2 /* last packet rung the doorbell */
  204. };
  205. /* T204 TX SW scheduler */
  206. /* Per T204 TX port */
  207. struct sched_port {
  208. unsigned int avail; /* available bits - quota */
  209. unsigned int drain_bits_per_1024ns; /* drain rate */
  210. unsigned int speed; /* drain rate, mbps */
  211. unsigned int mtu; /* mtu size */
  212. struct sk_buff_head skbq; /* pending skbs */
  213. };
  214. /* Per T204 device */
  215. struct sched {
  216. ktime_t last_updated; /* last time quotas were computed */
  217. unsigned int max_avail; /* max bits to be sent to any port */
  218. unsigned int port; /* port index (round robin ports) */
  219. unsigned int num; /* num skbs in per port queues */
  220. struct sched_port p[MAX_NPORTS];
  221. struct tasklet_struct sched_tsk;/* tasklet used to run scheduler */
  222. };
  223. static void restart_sched(unsigned long);
  224. /*
  225. * Main SGE data structure
  226. *
  227. * Interrupts are handled by a single CPU and it is likely that on a MP system
  228. * the application is migrated to another CPU. In that scenario, we try to
  229. * seperate the RX(in irq context) and TX state in order to decrease memory
  230. * contention.
  231. */
  232. struct sge {
  233. struct adapter *adapter; /* adapter backpointer */
  234. struct net_device *netdev; /* netdevice backpointer */
  235. struct freelQ freelQ[SGE_FREELQ_N]; /* buffer free lists */
  236. struct respQ respQ; /* response Q */
  237. unsigned long stopped_tx_queues; /* bitmap of suspended Tx queues */
  238. unsigned int rx_pkt_pad; /* RX padding for L2 packets */
  239. unsigned int jumbo_fl; /* jumbo freelist Q index */
  240. unsigned int intrtimer_nres; /* no-resource interrupt timer */
  241. unsigned int fixed_intrtimer;/* non-adaptive interrupt timer */
  242. struct timer_list tx_reclaim_timer; /* reclaims TX buffers */
  243. struct timer_list espibug_timer;
  244. unsigned long espibug_timeout;
  245. struct sk_buff *espibug_skb[MAX_NPORTS];
  246. u32 sge_control; /* shadow value of sge control reg */
  247. struct sge_intr_counts stats;
  248. struct sge_port_stats *port_stats[MAX_NPORTS];
  249. struct sched *tx_sched;
  250. struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned_in_smp;
  251. };
  252. /*
  253. * stop tasklet and free all pending skb's
  254. */
  255. static void tx_sched_stop(struct sge *sge)
  256. {
  257. struct sched *s = sge->tx_sched;
  258. int i;
  259. tasklet_kill(&s->sched_tsk);
  260. for (i = 0; i < MAX_NPORTS; i++)
  261. __skb_queue_purge(&s->p[s->port].skbq);
  262. }
  263. /*
  264. * t1_sched_update_parms() is called when the MTU or link speed changes. It
  265. * re-computes scheduler parameters to scope with the change.
  266. */
  267. unsigned int t1_sched_update_parms(struct sge *sge, unsigned int port,
  268. unsigned int mtu, unsigned int speed)
  269. {
  270. struct sched *s = sge->tx_sched;
  271. struct sched_port *p = &s->p[port];
  272. unsigned int max_avail_segs;
  273. pr_debug("t1_sched_update_params mtu=%d speed=%d\n", mtu, speed);
  274. if (speed)
  275. p->speed = speed;
  276. if (mtu)
  277. p->mtu = mtu;
  278. if (speed || mtu) {
  279. unsigned long long drain = 1024ULL * p->speed * (p->mtu - 40);
  280. do_div(drain, (p->mtu + 50) * 1000);
  281. p->drain_bits_per_1024ns = (unsigned int) drain;
  282. if (p->speed < 1000)
  283. p->drain_bits_per_1024ns =
  284. 90 * p->drain_bits_per_1024ns / 100;
  285. }
  286. if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204) {
  287. p->drain_bits_per_1024ns -= 16;
  288. s->max_avail = max(4096U, p->mtu + 16 + 14 + 4);
  289. max_avail_segs = max(1U, 4096 / (p->mtu - 40));
  290. } else {
  291. s->max_avail = 16384;
  292. max_avail_segs = max(1U, 9000 / (p->mtu - 40));
  293. }
  294. pr_debug("t1_sched_update_parms: mtu %u speed %u max_avail %u "
  295. "max_avail_segs %u drain_bits_per_1024ns %u\n", p->mtu,
  296. p->speed, s->max_avail, max_avail_segs,
  297. p->drain_bits_per_1024ns);
  298. return max_avail_segs * (p->mtu - 40);
  299. }
  300. /*
  301. * t1_sched_max_avail_bytes() tells the scheduler the maximum amount of
  302. * data that can be pushed per port.
  303. */
  304. void t1_sched_set_max_avail_bytes(struct sge *sge, unsigned int val)
  305. {
  306. struct sched *s = sge->tx_sched;
  307. unsigned int i;
  308. s->max_avail = val;
  309. for (i = 0; i < MAX_NPORTS; i++)
  310. t1_sched_update_parms(sge, i, 0, 0);
  311. }
  312. /*
  313. * t1_sched_set_drain_bits_per_us() tells the scheduler at which rate a port
  314. * is draining.
  315. */
  316. void t1_sched_set_drain_bits_per_us(struct sge *sge, unsigned int port,
  317. unsigned int val)
  318. {
  319. struct sched *s = sge->tx_sched;
  320. struct sched_port *p = &s->p[port];
  321. p->drain_bits_per_1024ns = val * 1024 / 1000;
  322. t1_sched_update_parms(sge, port, 0, 0);
  323. }
  324. /*
  325. * get_clock() implements a ns clock (see ktime_get)
  326. */
  327. static inline ktime_t get_clock(void)
  328. {
  329. struct timespec ts;
  330. ktime_get_ts(&ts);
  331. return timespec_to_ktime(ts);
  332. }
  333. /*
  334. * tx_sched_init() allocates resources and does basic initialization.
  335. */
  336. static int tx_sched_init(struct sge *sge)
  337. {
  338. struct sched *s;
  339. int i;
  340. s = kzalloc(sizeof (struct sched), GFP_KERNEL);
  341. if (!s)
  342. return -ENOMEM;
  343. pr_debug("tx_sched_init\n");
  344. tasklet_init(&s->sched_tsk, restart_sched, (unsigned long) sge);
  345. sge->tx_sched = s;
  346. for (i = 0; i < MAX_NPORTS; i++) {
  347. skb_queue_head_init(&s->p[i].skbq);
  348. t1_sched_update_parms(sge, i, 1500, 1000);
  349. }
  350. return 0;
  351. }
  352. /*
  353. * sched_update_avail() computes the delta since the last time it was called
  354. * and updates the per port quota (number of bits that can be sent to the any
  355. * port).
  356. */
  357. static inline int sched_update_avail(struct sge *sge)
  358. {
  359. struct sched *s = sge->tx_sched;
  360. ktime_t now = get_clock();
  361. unsigned int i;
  362. long long delta_time_ns;
  363. delta_time_ns = ktime_to_ns(ktime_sub(now, s->last_updated));
  364. pr_debug("sched_update_avail delta=%lld\n", delta_time_ns);
  365. if (delta_time_ns < 15000)
  366. return 0;
  367. for (i = 0; i < MAX_NPORTS; i++) {
  368. struct sched_port *p = &s->p[i];
  369. unsigned int delta_avail;
  370. delta_avail = (p->drain_bits_per_1024ns * delta_time_ns) >> 13;
  371. p->avail = min(p->avail + delta_avail, s->max_avail);
  372. }
  373. s->last_updated = now;
  374. return 1;
  375. }
  376. /*
  377. * sched_skb() is called from two different places. In the tx path, any
  378. * packet generating load on an output port will call sched_skb()
  379. * (skb != NULL). In addition, sched_skb() is called from the irq/soft irq
  380. * context (skb == NULL).
  381. * The scheduler only returns a skb (which will then be sent) if the
  382. * length of the skb is <= the current quota of the output port.
  383. */
  384. static struct sk_buff *sched_skb(struct sge *sge, struct sk_buff *skb,
  385. unsigned int credits)
  386. {
  387. struct sched *s = sge->tx_sched;
  388. struct sk_buff_head *skbq;
  389. unsigned int i, len, update = 1;
  390. pr_debug("sched_skb %p\n", skb);
  391. if (!skb) {
  392. if (!s->num)
  393. return NULL;
  394. } else {
  395. skbq = &s->p[skb->dev->if_port].skbq;
  396. __skb_queue_tail(skbq, skb);
  397. s->num++;
  398. skb = NULL;
  399. }
  400. if (credits < MAX_SKB_FRAGS + 1)
  401. goto out;
  402. again:
  403. for (i = 0; i < MAX_NPORTS; i++) {
  404. s->port = ++s->port & (MAX_NPORTS - 1);
  405. skbq = &s->p[s->port].skbq;
  406. skb = skb_peek(skbq);
  407. if (!skb)
  408. continue;
  409. len = skb->len;
  410. if (len <= s->p[s->port].avail) {
  411. s->p[s->port].avail -= len;
  412. s->num--;
  413. __skb_unlink(skb, skbq);
  414. goto out;
  415. }
  416. skb = NULL;
  417. }
  418. if (update-- && sched_update_avail(sge))
  419. goto again;
  420. out:
  421. /* If there are more pending skbs, we use the hardware to schedule us
  422. * again.
  423. */
  424. if (s->num && !skb) {
  425. struct cmdQ *q = &sge->cmdQ[0];
  426. clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  427. if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
  428. set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  429. writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
  430. }
  431. }
  432. pr_debug("sched_skb ret %p\n", skb);
  433. return skb;
  434. }
  435. /*
  436. * PIO to indicate that memory mapped Q contains valid descriptor(s).
  437. */
  438. static inline void doorbell_pio(struct adapter *adapter, u32 val)
  439. {
  440. wmb();
  441. writel(val, adapter->regs + A_SG_DOORBELL);
  442. }
  443. /*
  444. * Frees all RX buffers on the freelist Q. The caller must make sure that
  445. * the SGE is turned off before calling this function.
  446. */
  447. static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
  448. {
  449. unsigned int cidx = q->cidx;
  450. while (q->credits--) {
  451. struct freelQ_ce *ce = &q->centries[cidx];
  452. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  453. pci_unmap_len(ce, dma_len),
  454. PCI_DMA_FROMDEVICE);
  455. dev_kfree_skb(ce->skb);
  456. ce->skb = NULL;
  457. if (++cidx == q->size)
  458. cidx = 0;
  459. }
  460. }
  461. /*
  462. * Free RX free list and response queue resources.
  463. */
  464. static void free_rx_resources(struct sge *sge)
  465. {
  466. struct pci_dev *pdev = sge->adapter->pdev;
  467. unsigned int size, i;
  468. if (sge->respQ.entries) {
  469. size = sizeof(struct respQ_e) * sge->respQ.size;
  470. pci_free_consistent(pdev, size, sge->respQ.entries,
  471. sge->respQ.dma_addr);
  472. }
  473. for (i = 0; i < SGE_FREELQ_N; i++) {
  474. struct freelQ *q = &sge->freelQ[i];
  475. if (q->centries) {
  476. free_freelQ_buffers(pdev, q);
  477. kfree(q->centries);
  478. }
  479. if (q->entries) {
  480. size = sizeof(struct freelQ_e) * q->size;
  481. pci_free_consistent(pdev, size, q->entries,
  482. q->dma_addr);
  483. }
  484. }
  485. }
  486. /*
  487. * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
  488. * response queue.
  489. */
  490. static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
  491. {
  492. struct pci_dev *pdev = sge->adapter->pdev;
  493. unsigned int size, i;
  494. for (i = 0; i < SGE_FREELQ_N; i++) {
  495. struct freelQ *q = &sge->freelQ[i];
  496. q->genbit = 1;
  497. q->size = p->freelQ_size[i];
  498. q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
  499. size = sizeof(struct freelQ_e) * q->size;
  500. q->entries = pci_alloc_consistent(pdev, size, &q->dma_addr);
  501. if (!q->entries)
  502. goto err_no_mem;
  503. size = sizeof(struct freelQ_ce) * q->size;
  504. q->centries = kzalloc(size, GFP_KERNEL);
  505. if (!q->centries)
  506. goto err_no_mem;
  507. }
  508. /*
  509. * Calculate the buffer sizes for the two free lists. FL0 accommodates
  510. * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
  511. * including all the sk_buff overhead.
  512. *
  513. * Note: For T2 FL0 and FL1 are reversed.
  514. */
  515. sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
  516. sizeof(struct cpl_rx_data) +
  517. sge->freelQ[!sge->jumbo_fl].dma_offset;
  518. size = (16 * 1024) -
  519. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  520. sge->freelQ[sge->jumbo_fl].rx_buffer_size = size;
  521. /*
  522. * Setup which skb recycle Q should be used when recycling buffers from
  523. * each free list.
  524. */
  525. sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
  526. sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;
  527. sge->respQ.genbit = 1;
  528. sge->respQ.size = SGE_RESPQ_E_N;
  529. sge->respQ.credits = 0;
  530. size = sizeof(struct respQ_e) * sge->respQ.size;
  531. sge->respQ.entries =
  532. pci_alloc_consistent(pdev, size, &sge->respQ.dma_addr);
  533. if (!sge->respQ.entries)
  534. goto err_no_mem;
  535. return 0;
  536. err_no_mem:
  537. free_rx_resources(sge);
  538. return -ENOMEM;
  539. }
  540. /*
  541. * Reclaims n TX descriptors and frees the buffers associated with them.
  542. */
  543. static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
  544. {
  545. struct cmdQ_ce *ce;
  546. struct pci_dev *pdev = sge->adapter->pdev;
  547. unsigned int cidx = q->cidx;
  548. q->in_use -= n;
  549. ce = &q->centries[cidx];
  550. while (n--) {
  551. if (likely(pci_unmap_len(ce, dma_len))) {
  552. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  553. pci_unmap_len(ce, dma_len),
  554. PCI_DMA_TODEVICE);
  555. if (q->sop)
  556. q->sop = 0;
  557. }
  558. if (ce->skb) {
  559. dev_kfree_skb_any(ce->skb);
  560. q->sop = 1;
  561. }
  562. ce++;
  563. if (++cidx == q->size) {
  564. cidx = 0;
  565. ce = q->centries;
  566. }
  567. }
  568. q->cidx = cidx;
  569. }
  570. /*
  571. * Free TX resources.
  572. *
  573. * Assumes that SGE is stopped and all interrupts are disabled.
  574. */
  575. static void free_tx_resources(struct sge *sge)
  576. {
  577. struct pci_dev *pdev = sge->adapter->pdev;
  578. unsigned int size, i;
  579. for (i = 0; i < SGE_CMDQ_N; i++) {
  580. struct cmdQ *q = &sge->cmdQ[i];
  581. if (q->centries) {
  582. if (q->in_use)
  583. free_cmdQ_buffers(sge, q, q->in_use);
  584. kfree(q->centries);
  585. }
  586. if (q->entries) {
  587. size = sizeof(struct cmdQ_e) * q->size;
  588. pci_free_consistent(pdev, size, q->entries,
  589. q->dma_addr);
  590. }
  591. }
  592. }
  593. /*
  594. * Allocates basic TX resources, consisting of memory mapped command Qs.
  595. */
  596. static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
  597. {
  598. struct pci_dev *pdev = sge->adapter->pdev;
  599. unsigned int size, i;
  600. for (i = 0; i < SGE_CMDQ_N; i++) {
  601. struct cmdQ *q = &sge->cmdQ[i];
  602. q->genbit = 1;
  603. q->sop = 1;
  604. q->size = p->cmdQ_size[i];
  605. q->in_use = 0;
  606. q->status = 0;
  607. q->processed = q->cleaned = 0;
  608. q->stop_thres = 0;
  609. spin_lock_init(&q->lock);
  610. size = sizeof(struct cmdQ_e) * q->size;
  611. q->entries = pci_alloc_consistent(pdev, size, &q->dma_addr);
  612. if (!q->entries)
  613. goto err_no_mem;
  614. size = sizeof(struct cmdQ_ce) * q->size;
  615. q->centries = kzalloc(size, GFP_KERNEL);
  616. if (!q->centries)
  617. goto err_no_mem;
  618. }
  619. /*
  620. * CommandQ 0 handles Ethernet and TOE packets, while queue 1 is TOE
  621. * only. For queue 0 set the stop threshold so we can handle one more
  622. * packet from each port, plus reserve an additional 24 entries for
  623. * Ethernet packets only. Queue 1 never suspends nor do we reserve
  624. * space for Ethernet packets.
  625. */
  626. sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
  627. (MAX_SKB_FRAGS + 1);
  628. return 0;
  629. err_no_mem:
  630. free_tx_resources(sge);
  631. return -ENOMEM;
  632. }
  633. static inline void setup_ring_params(struct adapter *adapter, u64 addr,
  634. u32 size, int base_reg_lo,
  635. int base_reg_hi, int size_reg)
  636. {
  637. writel((u32)addr, adapter->regs + base_reg_lo);
  638. writel(addr >> 32, adapter->regs + base_reg_hi);
  639. writel(size, adapter->regs + size_reg);
  640. }
  641. /*
  642. * Enable/disable VLAN acceleration.
  643. */
  644. void t1_set_vlan_accel(struct adapter *adapter, int on_off)
  645. {
  646. struct sge *sge = adapter->sge;
  647. sge->sge_control &= ~F_VLAN_XTRACT;
  648. if (on_off)
  649. sge->sge_control |= F_VLAN_XTRACT;
  650. if (adapter->open_device_map) {
  651. writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
  652. readl(adapter->regs + A_SG_CONTROL); /* flush */
  653. }
  654. }
  655. /*
  656. * Programs the various SGE registers. However, the engine is not yet enabled,
  657. * but sge->sge_control is setup and ready to go.
  658. */
  659. static void configure_sge(struct sge *sge, struct sge_params *p)
  660. {
  661. struct adapter *ap = sge->adapter;
  662. writel(0, ap->regs + A_SG_CONTROL);
  663. setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
  664. A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
  665. setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
  666. A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
  667. setup_ring_params(ap, sge->freelQ[0].dma_addr,
  668. sge->freelQ[0].size, A_SG_FL0BASELWR,
  669. A_SG_FL0BASEUPR, A_SG_FL0SIZE);
  670. setup_ring_params(ap, sge->freelQ[1].dma_addr,
  671. sge->freelQ[1].size, A_SG_FL1BASELWR,
  672. A_SG_FL1BASEUPR, A_SG_FL1SIZE);
  673. /* The threshold comparison uses <. */
  674. writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
  675. setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
  676. A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
  677. writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
  678. sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
  679. F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
  680. V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
  681. V_RX_PKT_OFFSET(sge->rx_pkt_pad);
  682. #if defined(__BIG_ENDIAN_BITFIELD)
  683. sge->sge_control |= F_ENABLE_BIG_ENDIAN;
  684. #endif
  685. /* Initialize no-resource timer */
  686. sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);
  687. t1_sge_set_coalesce_params(sge, p);
  688. }
  689. /*
  690. * Return the payload capacity of the jumbo free-list buffers.
  691. */
  692. static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
  693. {
  694. return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
  695. sge->freelQ[sge->jumbo_fl].dma_offset -
  696. sizeof(struct cpl_rx_data);
  697. }
  698. /*
  699. * Frees all SGE related resources and the sge structure itself
  700. */
  701. void t1_sge_destroy(struct sge *sge)
  702. {
  703. int i;
  704. for_each_port(sge->adapter, i)
  705. free_percpu(sge->port_stats[i]);
  706. kfree(sge->tx_sched);
  707. free_tx_resources(sge);
  708. free_rx_resources(sge);
  709. kfree(sge);
  710. }
  711. /*
  712. * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
  713. * context Q) until the Q is full or alloc_skb fails.
  714. *
  715. * It is possible that the generation bits already match, indicating that the
  716. * buffer is already valid and nothing needs to be done. This happens when we
  717. * copied a received buffer into a new sk_buff during the interrupt processing.
  718. *
  719. * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
  720. * we specify a RX_OFFSET in order to make sure that the IP header is 4B
  721. * aligned.
  722. */
  723. static void refill_free_list(struct sge *sge, struct freelQ *q)
  724. {
  725. struct pci_dev *pdev = sge->adapter->pdev;
  726. struct freelQ_ce *ce = &q->centries[q->pidx];
  727. struct freelQ_e *e = &q->entries[q->pidx];
  728. unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
  729. while (q->credits < q->size) {
  730. struct sk_buff *skb;
  731. dma_addr_t mapping;
  732. skb = alloc_skb(q->rx_buffer_size, GFP_ATOMIC);
  733. if (!skb)
  734. break;
  735. skb_reserve(skb, q->dma_offset);
  736. mapping = pci_map_single(pdev, skb->data, dma_len,
  737. PCI_DMA_FROMDEVICE);
  738. ce->skb = skb;
  739. pci_unmap_addr_set(ce, dma_addr, mapping);
  740. pci_unmap_len_set(ce, dma_len, dma_len);
  741. e->addr_lo = (u32)mapping;
  742. e->addr_hi = (u64)mapping >> 32;
  743. e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
  744. wmb();
  745. e->gen2 = V_CMD_GEN2(q->genbit);
  746. e++;
  747. ce++;
  748. if (++q->pidx == q->size) {
  749. q->pidx = 0;
  750. q->genbit ^= 1;
  751. ce = q->centries;
  752. e = q->entries;
  753. }
  754. q->credits++;
  755. }
  756. }
  757. /*
  758. * Calls refill_free_list for both free lists. If we cannot fill at least 1/4
  759. * of both rings, we go into 'few interrupt mode' in order to give the system
  760. * time to free up resources.
  761. */
  762. static void freelQs_empty(struct sge *sge)
  763. {
  764. struct adapter *adapter = sge->adapter;
  765. u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
  766. u32 irqholdoff_reg;
  767. refill_free_list(sge, &sge->freelQ[0]);
  768. refill_free_list(sge, &sge->freelQ[1]);
  769. if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
  770. sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
  771. irq_reg |= F_FL_EXHAUSTED;
  772. irqholdoff_reg = sge->fixed_intrtimer;
  773. } else {
  774. /* Clear the F_FL_EXHAUSTED interrupts for now */
  775. irq_reg &= ~F_FL_EXHAUSTED;
  776. irqholdoff_reg = sge->intrtimer_nres;
  777. }
  778. writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
  779. writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
  780. /* We reenable the Qs to force a freelist GTS interrupt later */
  781. doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
  782. }
  783. #define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
  784. #define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
  785. #define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
  786. F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
  787. /*
  788. * Disable SGE Interrupts
  789. */
  790. void t1_sge_intr_disable(struct sge *sge)
  791. {
  792. u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
  793. writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
  794. writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
  795. }
  796. /*
  797. * Enable SGE interrupts.
  798. */
  799. void t1_sge_intr_enable(struct sge *sge)
  800. {
  801. u32 en = SGE_INT_ENABLE;
  802. u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
  803. if (sge->adapter->flags & TSO_CAPABLE)
  804. en &= ~F_PACKET_TOO_BIG;
  805. writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
  806. writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
  807. }
  808. /*
  809. * Clear SGE interrupts.
  810. */
  811. void t1_sge_intr_clear(struct sge *sge)
  812. {
  813. writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
  814. writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
  815. }
  816. /*
  817. * SGE 'Error' interrupt handler
  818. */
  819. int t1_sge_intr_error_handler(struct sge *sge)
  820. {
  821. struct adapter *adapter = sge->adapter;
  822. u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
  823. if (adapter->flags & TSO_CAPABLE)
  824. cause &= ~F_PACKET_TOO_BIG;
  825. if (cause & F_RESPQ_EXHAUSTED)
  826. sge->stats.respQ_empty++;
  827. if (cause & F_RESPQ_OVERFLOW) {
  828. sge->stats.respQ_overflow++;
  829. CH_ALERT("%s: SGE response queue overflow\n",
  830. adapter->name);
  831. }
  832. if (cause & F_FL_EXHAUSTED) {
  833. sge->stats.freelistQ_empty++;
  834. freelQs_empty(sge);
  835. }
  836. if (cause & F_PACKET_TOO_BIG) {
  837. sge->stats.pkt_too_big++;
  838. CH_ALERT("%s: SGE max packet size exceeded\n",
  839. adapter->name);
  840. }
  841. if (cause & F_PACKET_MISMATCH) {
  842. sge->stats.pkt_mismatch++;
  843. CH_ALERT("%s: SGE packet mismatch\n", adapter->name);
  844. }
  845. if (cause & SGE_INT_FATAL)
  846. t1_fatal_err(adapter);
  847. writel(cause, adapter->regs + A_SG_INT_CAUSE);
  848. return 0;
  849. }
  850. const struct sge_intr_counts *t1_sge_get_intr_counts(const struct sge *sge)
  851. {
  852. return &sge->stats;
  853. }
  854. void t1_sge_get_port_stats(const struct sge *sge, int port,
  855. struct sge_port_stats *ss)
  856. {
  857. int cpu;
  858. memset(ss, 0, sizeof(*ss));
  859. for_each_possible_cpu(cpu) {
  860. struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[port], cpu);
  861. ss->rx_packets += st->rx_packets;
  862. ss->rx_cso_good += st->rx_cso_good;
  863. ss->tx_packets += st->tx_packets;
  864. ss->tx_cso += st->tx_cso;
  865. ss->tx_tso += st->tx_tso;
  866. ss->vlan_xtract += st->vlan_xtract;
  867. ss->vlan_insert += st->vlan_insert;
  868. }
  869. }
  870. /**
  871. * recycle_fl_buf - recycle a free list buffer
  872. * @fl: the free list
  873. * @idx: index of buffer to recycle
  874. *
  875. * Recycles the specified buffer on the given free list by adding it at
  876. * the next available slot on the list.
  877. */
  878. static void recycle_fl_buf(struct freelQ *fl, int idx)
  879. {
  880. struct freelQ_e *from = &fl->entries[idx];
  881. struct freelQ_e *to = &fl->entries[fl->pidx];
  882. fl->centries[fl->pidx] = fl->centries[idx];
  883. to->addr_lo = from->addr_lo;
  884. to->addr_hi = from->addr_hi;
  885. to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
  886. wmb();
  887. to->gen2 = V_CMD_GEN2(fl->genbit);
  888. fl->credits++;
  889. if (++fl->pidx == fl->size) {
  890. fl->pidx = 0;
  891. fl->genbit ^= 1;
  892. }
  893. }
  894. /**
  895. * get_packet - return the next ingress packet buffer
  896. * @pdev: the PCI device that received the packet
  897. * @fl: the SGE free list holding the packet
  898. * @len: the actual packet length, excluding any SGE padding
  899. * @dma_pad: padding at beginning of buffer left by SGE DMA
  900. * @skb_pad: padding to be used if the packet is copied
  901. * @copy_thres: length threshold under which a packet should be copied
  902. * @drop_thres: # of remaining buffers before we start dropping packets
  903. *
  904. * Get the next packet from a free list and complete setup of the
  905. * sk_buff. If the packet is small we make a copy and recycle the
  906. * original buffer, otherwise we use the original buffer itself. If a
  907. * positive drop threshold is supplied packets are dropped and their
  908. * buffers recycled if (a) the number of remaining buffers is under the
  909. * threshold and the packet is too big to copy, or (b) the packet should
  910. * be copied but there is no memory for the copy.
  911. */
  912. static inline struct sk_buff *get_packet(struct pci_dev *pdev,
  913. struct freelQ *fl, unsigned int len,
  914. int dma_pad, int skb_pad,
  915. unsigned int copy_thres,
  916. unsigned int drop_thres)
  917. {
  918. struct sk_buff *skb;
  919. struct freelQ_ce *ce = &fl->centries[fl->cidx];
  920. if (len < copy_thres) {
  921. skb = alloc_skb(len + skb_pad, GFP_ATOMIC);
  922. if (likely(skb != NULL)) {
  923. skb_reserve(skb, skb_pad);
  924. skb_put(skb, len);
  925. pci_dma_sync_single_for_cpu(pdev,
  926. pci_unmap_addr(ce, dma_addr),
  927. pci_unmap_len(ce, dma_len),
  928. PCI_DMA_FROMDEVICE);
  929. memcpy(skb->data, ce->skb->data + dma_pad, len);
  930. pci_dma_sync_single_for_device(pdev,
  931. pci_unmap_addr(ce, dma_addr),
  932. pci_unmap_len(ce, dma_len),
  933. PCI_DMA_FROMDEVICE);
  934. } else if (!drop_thres)
  935. goto use_orig_buf;
  936. recycle_fl_buf(fl, fl->cidx);
  937. return skb;
  938. }
  939. if (fl->credits < drop_thres) {
  940. recycle_fl_buf(fl, fl->cidx);
  941. return NULL;
  942. }
  943. use_orig_buf:
  944. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  945. pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
  946. skb = ce->skb;
  947. skb_reserve(skb, dma_pad);
  948. skb_put(skb, len);
  949. return skb;
  950. }
  951. /**
  952. * unexpected_offload - handle an unexpected offload packet
  953. * @adapter: the adapter
  954. * @fl: the free list that received the packet
  955. *
  956. * Called when we receive an unexpected offload packet (e.g., the TOE
  957. * function is disabled or the card is a NIC). Prints a message and
  958. * recycles the buffer.
  959. */
  960. static void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
  961. {
  962. struct freelQ_ce *ce = &fl->centries[fl->cidx];
  963. struct sk_buff *skb = ce->skb;
  964. pci_dma_sync_single_for_cpu(adapter->pdev, pci_unmap_addr(ce, dma_addr),
  965. pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
  966. CH_ERR("%s: unexpected offload packet, cmd %u\n",
  967. adapter->name, *skb->data);
  968. recycle_fl_buf(fl, fl->cidx);
  969. }
  970. /*
  971. * T1/T2 SGE limits the maximum DMA size per TX descriptor to
  972. * SGE_TX_DESC_MAX_PLEN (16KB). If the PAGE_SIZE is larger than 16KB, the
  973. * stack might send more than SGE_TX_DESC_MAX_PLEN in a contiguous manner.
  974. * Note that the *_large_page_tx_descs stuff will be optimized out when
  975. * PAGE_SIZE <= SGE_TX_DESC_MAX_PLEN.
  976. *
  977. * compute_large_page_descs() computes how many additional descriptors are
  978. * required to break down the stack's request.
  979. */
  980. static inline unsigned int compute_large_page_tx_descs(struct sk_buff *skb)
  981. {
  982. unsigned int count = 0;
  983. if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
  984. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  985. unsigned int i, len = skb->len - skb->data_len;
  986. while (len > SGE_TX_DESC_MAX_PLEN) {
  987. count++;
  988. len -= SGE_TX_DESC_MAX_PLEN;
  989. }
  990. for (i = 0; nfrags--; i++) {
  991. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  992. len = frag->size;
  993. while (len > SGE_TX_DESC_MAX_PLEN) {
  994. count++;
  995. len -= SGE_TX_DESC_MAX_PLEN;
  996. }
  997. }
  998. }
  999. return count;
  1000. }
  1001. /*
  1002. * Write a cmdQ entry.
  1003. *
  1004. * Since this function writes the 'flags' field, it must not be used to
  1005. * write the first cmdQ entry.
  1006. */
  1007. static inline void write_tx_desc(struct cmdQ_e *e, dma_addr_t mapping,
  1008. unsigned int len, unsigned int gen,
  1009. unsigned int eop)
  1010. {
  1011. if (unlikely(len > SGE_TX_DESC_MAX_PLEN))
  1012. BUG();
  1013. e->addr_lo = (u32)mapping;
  1014. e->addr_hi = (u64)mapping >> 32;
  1015. e->len_gen = V_CMD_LEN(len) | V_CMD_GEN1(gen);
  1016. e->flags = F_CMD_DATAVALID | V_CMD_EOP(eop) | V_CMD_GEN2(gen);
  1017. }
  1018. /*
  1019. * See comment for previous function.
  1020. *
  1021. * write_tx_descs_large_page() writes additional SGE tx descriptors if
  1022. * *desc_len exceeds HW's capability.
  1023. */
  1024. static inline unsigned int write_large_page_tx_descs(unsigned int pidx,
  1025. struct cmdQ_e **e,
  1026. struct cmdQ_ce **ce,
  1027. unsigned int *gen,
  1028. dma_addr_t *desc_mapping,
  1029. unsigned int *desc_len,
  1030. unsigned int nfrags,
  1031. struct cmdQ *q)
  1032. {
  1033. if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
  1034. struct cmdQ_e *e1 = *e;
  1035. struct cmdQ_ce *ce1 = *ce;
  1036. while (*desc_len > SGE_TX_DESC_MAX_PLEN) {
  1037. *desc_len -= SGE_TX_DESC_MAX_PLEN;
  1038. write_tx_desc(e1, *desc_mapping, SGE_TX_DESC_MAX_PLEN,
  1039. *gen, nfrags == 0 && *desc_len == 0);
  1040. ce1->skb = NULL;
  1041. pci_unmap_len_set(ce1, dma_len, 0);
  1042. *desc_mapping += SGE_TX_DESC_MAX_PLEN;
  1043. if (*desc_len) {
  1044. ce1++;
  1045. e1++;
  1046. if (++pidx == q->size) {
  1047. pidx = 0;
  1048. *gen ^= 1;
  1049. ce1 = q->centries;
  1050. e1 = q->entries;
  1051. }
  1052. }
  1053. }
  1054. *e = e1;
  1055. *ce = ce1;
  1056. }
  1057. return pidx;
  1058. }
  1059. /*
  1060. * Write the command descriptors to transmit the given skb starting at
  1061. * descriptor pidx with the given generation.
  1062. */
  1063. static inline void write_tx_descs(struct adapter *adapter, struct sk_buff *skb,
  1064. unsigned int pidx, unsigned int gen,
  1065. struct cmdQ *q)
  1066. {
  1067. dma_addr_t mapping, desc_mapping;
  1068. struct cmdQ_e *e, *e1;
  1069. struct cmdQ_ce *ce;
  1070. unsigned int i, flags, first_desc_len, desc_len,
  1071. nfrags = skb_shinfo(skb)->nr_frags;
  1072. e = e1 = &q->entries[pidx];
  1073. ce = &q->centries[pidx];
  1074. mapping = pci_map_single(adapter->pdev, skb->data,
  1075. skb->len - skb->data_len, PCI_DMA_TODEVICE);
  1076. desc_mapping = mapping;
  1077. desc_len = skb->len - skb->data_len;
  1078. flags = F_CMD_DATAVALID | F_CMD_SOP |
  1079. V_CMD_EOP(nfrags == 0 && desc_len <= SGE_TX_DESC_MAX_PLEN) |
  1080. V_CMD_GEN2(gen);
  1081. first_desc_len = (desc_len <= SGE_TX_DESC_MAX_PLEN) ?
  1082. desc_len : SGE_TX_DESC_MAX_PLEN;
  1083. e->addr_lo = (u32)desc_mapping;
  1084. e->addr_hi = (u64)desc_mapping >> 32;
  1085. e->len_gen = V_CMD_LEN(first_desc_len) | V_CMD_GEN1(gen);
  1086. ce->skb = NULL;
  1087. pci_unmap_len_set(ce, dma_len, 0);
  1088. if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN &&
  1089. desc_len > SGE_TX_DESC_MAX_PLEN) {
  1090. desc_mapping += first_desc_len;
  1091. desc_len -= first_desc_len;
  1092. e1++;
  1093. ce++;
  1094. if (++pidx == q->size) {
  1095. pidx = 0;
  1096. gen ^= 1;
  1097. e1 = q->entries;
  1098. ce = q->centries;
  1099. }
  1100. pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
  1101. &desc_mapping, &desc_len,
  1102. nfrags, q);
  1103. if (likely(desc_len))
  1104. write_tx_desc(e1, desc_mapping, desc_len, gen,
  1105. nfrags == 0);
  1106. }
  1107. ce->skb = NULL;
  1108. pci_unmap_addr_set(ce, dma_addr, mapping);
  1109. pci_unmap_len_set(ce, dma_len, skb->len - skb->data_len);
  1110. for (i = 0; nfrags--; i++) {
  1111. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1112. e1++;
  1113. ce++;
  1114. if (++pidx == q->size) {
  1115. pidx = 0;
  1116. gen ^= 1;
  1117. e1 = q->entries;
  1118. ce = q->centries;
  1119. }
  1120. mapping = pci_map_page(adapter->pdev, frag->page,
  1121. frag->page_offset, frag->size,
  1122. PCI_DMA_TODEVICE);
  1123. desc_mapping = mapping;
  1124. desc_len = frag->size;
  1125. pidx = write_large_page_tx_descs(pidx, &e1, &ce, &gen,
  1126. &desc_mapping, &desc_len,
  1127. nfrags, q);
  1128. if (likely(desc_len))
  1129. write_tx_desc(e1, desc_mapping, desc_len, gen,
  1130. nfrags == 0);
  1131. ce->skb = NULL;
  1132. pci_unmap_addr_set(ce, dma_addr, mapping);
  1133. pci_unmap_len_set(ce, dma_len, frag->size);
  1134. }
  1135. ce->skb = skb;
  1136. wmb();
  1137. e->flags = flags;
  1138. }
  1139. /*
  1140. * Clean up completed Tx buffers.
  1141. */
  1142. static inline void reclaim_completed_tx(struct sge *sge, struct cmdQ *q)
  1143. {
  1144. unsigned int reclaim = q->processed - q->cleaned;
  1145. if (reclaim) {
  1146. pr_debug("reclaim_completed_tx processed:%d cleaned:%d\n",
  1147. q->processed, q->cleaned);
  1148. free_cmdQ_buffers(sge, q, reclaim);
  1149. q->cleaned += reclaim;
  1150. }
  1151. }
  1152. /*
  1153. * Called from tasklet. Checks the scheduler for any
  1154. * pending skbs that can be sent.
  1155. */
  1156. static void restart_sched(unsigned long arg)
  1157. {
  1158. struct sge *sge = (struct sge *) arg;
  1159. struct adapter *adapter = sge->adapter;
  1160. struct cmdQ *q = &sge->cmdQ[0];
  1161. struct sk_buff *skb;
  1162. unsigned int credits, queued_skb = 0;
  1163. spin_lock(&q->lock);
  1164. reclaim_completed_tx(sge, q);
  1165. credits = q->size - q->in_use;
  1166. pr_debug("restart_sched credits=%d\n", credits);
  1167. while ((skb = sched_skb(sge, NULL, credits)) != NULL) {
  1168. unsigned int genbit, pidx, count;
  1169. count = 1 + skb_shinfo(skb)->nr_frags;
  1170. count += compute_large_page_tx_descs(skb);
  1171. q->in_use += count;
  1172. genbit = q->genbit;
  1173. pidx = q->pidx;
  1174. q->pidx += count;
  1175. if (q->pidx >= q->size) {
  1176. q->pidx -= q->size;
  1177. q->genbit ^= 1;
  1178. }
  1179. write_tx_descs(adapter, skb, pidx, genbit, q);
  1180. credits = q->size - q->in_use;
  1181. queued_skb = 1;
  1182. }
  1183. if (queued_skb) {
  1184. clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  1185. if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
  1186. set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  1187. writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
  1188. }
  1189. }
  1190. spin_unlock(&q->lock);
  1191. }
  1192. /**
  1193. * sge_rx - process an ingress ethernet packet
  1194. * @sge: the sge structure
  1195. * @fl: the free list that contains the packet buffer
  1196. * @len: the packet length
  1197. *
  1198. * Process an ingress ethernet pakcet and deliver it to the stack.
  1199. */
  1200. static int sge_rx(struct sge *sge, struct freelQ *fl, unsigned int len)
  1201. {
  1202. struct sk_buff *skb;
  1203. struct cpl_rx_pkt *p;
  1204. struct adapter *adapter = sge->adapter;
  1205. struct sge_port_stats *st;
  1206. skb = get_packet(adapter->pdev, fl, len - sge->rx_pkt_pad,
  1207. sge->rx_pkt_pad, 2, SGE_RX_COPY_THRES,
  1208. SGE_RX_DROP_THRES);
  1209. if (unlikely(!skb)) {
  1210. sge->stats.rx_drops++;
  1211. return 0;
  1212. }
  1213. p = (struct cpl_rx_pkt *)skb->data;
  1214. skb_pull(skb, sizeof(*p));
  1215. if (p->iff >= adapter->params.nports) {
  1216. kfree_skb(skb);
  1217. return 0;
  1218. }
  1219. skb->dev = adapter->port[p->iff].dev;
  1220. skb->dev->last_rx = jiffies;
  1221. st = per_cpu_ptr(sge->port_stats[p->iff], smp_processor_id());
  1222. st->rx_packets++;
  1223. skb->protocol = eth_type_trans(skb, skb->dev);
  1224. if ((adapter->flags & RX_CSUM_ENABLED) && p->csum == 0xffff &&
  1225. skb->protocol == htons(ETH_P_IP) &&
  1226. (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
  1227. ++st->rx_cso_good;
  1228. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1229. } else
  1230. skb->ip_summed = CHECKSUM_NONE;
  1231. if (unlikely(adapter->vlan_grp && p->vlan_valid)) {
  1232. st->vlan_xtract++;
  1233. #ifdef CONFIG_CHELSIO_T1_NAPI
  1234. vlan_hwaccel_receive_skb(skb, adapter->vlan_grp,
  1235. ntohs(p->vlan));
  1236. #else
  1237. vlan_hwaccel_rx(skb, adapter->vlan_grp,
  1238. ntohs(p->vlan));
  1239. #endif
  1240. } else {
  1241. #ifdef CONFIG_CHELSIO_T1_NAPI
  1242. netif_receive_skb(skb);
  1243. #else
  1244. netif_rx(skb);
  1245. #endif
  1246. }
  1247. return 0;
  1248. }
  1249. /*
  1250. * Returns true if a command queue has enough available descriptors that
  1251. * we can resume Tx operation after temporarily disabling its packet queue.
  1252. */
  1253. static inline int enough_free_Tx_descs(const struct cmdQ *q)
  1254. {
  1255. unsigned int r = q->processed - q->cleaned;
  1256. return q->in_use - r < (q->size >> 1);
  1257. }
  1258. /*
  1259. * Called when sufficient space has become available in the SGE command queues
  1260. * after the Tx packet schedulers have been suspended to restart the Tx path.
  1261. */
  1262. static void restart_tx_queues(struct sge *sge)
  1263. {
  1264. struct adapter *adap = sge->adapter;
  1265. int i;
  1266. if (!enough_free_Tx_descs(&sge->cmdQ[0]))
  1267. return;
  1268. for_each_port(adap, i) {
  1269. struct net_device *nd = adap->port[i].dev;
  1270. if (test_and_clear_bit(nd->if_port, &sge->stopped_tx_queues) &&
  1271. netif_running(nd)) {
  1272. sge->stats.cmdQ_restarted[2]++;
  1273. netif_wake_queue(nd);
  1274. }
  1275. }
  1276. }
  1277. /*
  1278. * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0
  1279. * information.
  1280. */
  1281. static unsigned int update_tx_info(struct adapter *adapter,
  1282. unsigned int flags,
  1283. unsigned int pr0)
  1284. {
  1285. struct sge *sge = adapter->sge;
  1286. struct cmdQ *cmdq = &sge->cmdQ[0];
  1287. cmdq->processed += pr0;
  1288. if (flags & (F_FL0_ENABLE | F_FL1_ENABLE)) {
  1289. freelQs_empty(sge);
  1290. flags &= ~(F_FL0_ENABLE | F_FL1_ENABLE);
  1291. }
  1292. if (flags & F_CMDQ0_ENABLE) {
  1293. clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
  1294. if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
  1295. !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
  1296. set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
  1297. writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
  1298. }
  1299. if (sge->tx_sched)
  1300. tasklet_hi_schedule(&sge->tx_sched->sched_tsk);
  1301. flags &= ~F_CMDQ0_ENABLE;
  1302. }
  1303. if (unlikely(sge->stopped_tx_queues != 0))
  1304. restart_tx_queues(sge);
  1305. return flags;
  1306. }
  1307. /*
  1308. * Process SGE responses, up to the supplied budget. Returns the number of
  1309. * responses processed. A negative budget is effectively unlimited.
  1310. */
  1311. static int process_responses(struct adapter *adapter, int budget)
  1312. {
  1313. struct sge *sge = adapter->sge;
  1314. struct respQ *q = &sge->respQ;
  1315. struct respQ_e *e = &q->entries[q->cidx];
  1316. int budget_left = budget;
  1317. unsigned int flags = 0;
  1318. unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
  1319. while (likely(budget_left && e->GenerationBit == q->genbit)) {
  1320. flags |= e->Qsleeping;
  1321. cmdq_processed[0] += e->Cmdq0CreditReturn;
  1322. cmdq_processed[1] += e->Cmdq1CreditReturn;
  1323. /* We batch updates to the TX side to avoid cacheline
  1324. * ping-pong of TX state information on MP where the sender
  1325. * might run on a different CPU than this function...
  1326. */
  1327. if (unlikely(flags & F_CMDQ0_ENABLE || cmdq_processed[0] > 64)) {
  1328. flags = update_tx_info(adapter, flags, cmdq_processed[0]);
  1329. cmdq_processed[0] = 0;
  1330. }
  1331. if (unlikely(cmdq_processed[1] > 16)) {
  1332. sge->cmdQ[1].processed += cmdq_processed[1];
  1333. cmdq_processed[1] = 0;
  1334. }
  1335. if (likely(e->DataValid)) {
  1336. struct freelQ *fl = &sge->freelQ[e->FreelistQid];
  1337. BUG_ON(!e->Sop || !e->Eop);
  1338. if (unlikely(e->Offload))
  1339. unexpected_offload(adapter, fl);
  1340. else
  1341. sge_rx(sge, fl, e->BufferLength);
  1342. /*
  1343. * Note: this depends on each packet consuming a
  1344. * single free-list buffer; cf. the BUG above.
  1345. */
  1346. if (++fl->cidx == fl->size)
  1347. fl->cidx = 0;
  1348. if (unlikely(--fl->credits <
  1349. fl->size - SGE_FREEL_REFILL_THRESH))
  1350. refill_free_list(sge, fl);
  1351. } else
  1352. sge->stats.pure_rsps++;
  1353. e++;
  1354. if (unlikely(++q->cidx == q->size)) {
  1355. q->cidx = 0;
  1356. q->genbit ^= 1;
  1357. e = q->entries;
  1358. }
  1359. prefetch(e);
  1360. if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
  1361. writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
  1362. q->credits = 0;
  1363. }
  1364. --budget_left;
  1365. }
  1366. flags = update_tx_info(adapter, flags, cmdq_processed[0]);
  1367. sge->cmdQ[1].processed += cmdq_processed[1];
  1368. budget -= budget_left;
  1369. return budget;
  1370. }
  1371. static inline int responses_pending(const struct adapter *adapter)
  1372. {
  1373. const struct respQ *Q = &adapter->sge->respQ;
  1374. const struct respQ_e *e = &Q->entries[Q->cidx];
  1375. return (e->GenerationBit == Q->genbit);
  1376. }
  1377. #ifdef CONFIG_CHELSIO_T1_NAPI
  1378. /*
  1379. * A simpler version of process_responses() that handles only pure (i.e.,
  1380. * non data-carrying) responses. Such respones are too light-weight to justify
  1381. * calling a softirq when using NAPI, so we handle them specially in hard
  1382. * interrupt context. The function is called with a pointer to a response,
  1383. * which the caller must ensure is a valid pure response. Returns 1 if it
  1384. * encounters a valid data-carrying response, 0 otherwise.
  1385. */
  1386. static int process_pure_responses(struct adapter *adapter)
  1387. {
  1388. struct sge *sge = adapter->sge;
  1389. struct respQ *q = &sge->respQ;
  1390. struct respQ_e *e = &q->entries[q->cidx];
  1391. unsigned int flags = 0;
  1392. unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
  1393. if (e->DataValid)
  1394. return 1;
  1395. do {
  1396. flags |= e->Qsleeping;
  1397. cmdq_processed[0] += e->Cmdq0CreditReturn;
  1398. cmdq_processed[1] += e->Cmdq1CreditReturn;
  1399. e++;
  1400. if (unlikely(++q->cidx == q->size)) {
  1401. q->cidx = 0;
  1402. q->genbit ^= 1;
  1403. e = q->entries;
  1404. }
  1405. prefetch(e);
  1406. if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
  1407. writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
  1408. q->credits = 0;
  1409. }
  1410. sge->stats.pure_rsps++;
  1411. } while (e->GenerationBit == q->genbit && !e->DataValid);
  1412. flags = update_tx_info(adapter, flags, cmdq_processed[0]);
  1413. sge->cmdQ[1].processed += cmdq_processed[1];
  1414. return e->GenerationBit == q->genbit;
  1415. }
  1416. /*
  1417. * Handler for new data events when using NAPI. This does not need any locking
  1418. * or protection from interrupts as data interrupts are off at this point and
  1419. * other adapter interrupts do not interfere.
  1420. */
  1421. int t1_poll(struct net_device *dev, int *budget)
  1422. {
  1423. struct adapter *adapter = dev->priv;
  1424. int work_done;
  1425. work_done = process_responses(adapter, min(*budget, dev->quota));
  1426. *budget -= work_done;
  1427. dev->quota -= work_done;
  1428. if (unlikely(responses_pending(adapter)))
  1429. return 1;
  1430. netif_rx_complete(dev);
  1431. writel(adapter->sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
  1432. return 0;
  1433. }
  1434. /*
  1435. * NAPI version of the main interrupt handler.
  1436. */
  1437. irqreturn_t t1_interrupt(int irq, void *data)
  1438. {
  1439. struct adapter *adapter = data;
  1440. struct sge *sge = adapter->sge;
  1441. int handled;
  1442. if (likely(responses_pending(adapter))) {
  1443. struct net_device *dev = sge->netdev;
  1444. writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
  1445. if (__netif_rx_schedule_prep(dev)) {
  1446. if (process_pure_responses(adapter))
  1447. __netif_rx_schedule(dev);
  1448. else {
  1449. /* no data, no NAPI needed */
  1450. writel(sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
  1451. netif_poll_enable(dev); /* undo schedule_prep */
  1452. }
  1453. }
  1454. return IRQ_HANDLED;
  1455. }
  1456. spin_lock(&adapter->async_lock);
  1457. handled = t1_slow_intr_handler(adapter);
  1458. spin_unlock(&adapter->async_lock);
  1459. if (!handled)
  1460. sge->stats.unhandled_irqs++;
  1461. return IRQ_RETVAL(handled != 0);
  1462. }
  1463. #else
  1464. /*
  1465. * Main interrupt handler, optimized assuming that we took a 'DATA'
  1466. * interrupt.
  1467. *
  1468. * 1. Clear the interrupt
  1469. * 2. Loop while we find valid descriptors and process them; accumulate
  1470. * information that can be processed after the loop
  1471. * 3. Tell the SGE at which index we stopped processing descriptors
  1472. * 4. Bookkeeping; free TX buffers, ring doorbell if there are any
  1473. * outstanding TX buffers waiting, replenish RX buffers, potentially
  1474. * reenable upper layers if they were turned off due to lack of TX
  1475. * resources which are available again.
  1476. * 5. If we took an interrupt, but no valid respQ descriptors was found we
  1477. * let the slow_intr_handler run and do error handling.
  1478. */
  1479. irqreturn_t t1_interrupt(int irq, void *cookie)
  1480. {
  1481. int work_done;
  1482. struct adapter *adapter = cookie;
  1483. spin_lock(&adapter->async_lock);
  1484. writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
  1485. if (likely(responses_pending(adapter))
  1486. work_done = process_responses(adapter, -1);
  1487. else
  1488. work_done = t1_slow_intr_handler(adapter);
  1489. /*
  1490. * The unconditional clearing of the PL_CAUSE above may have raced
  1491. * with DMA completion and the corresponding generation of a response
  1492. * to cause us to miss the resulting data interrupt. The next write
  1493. * is also unconditional to recover the missed interrupt and render
  1494. * this race harmless.
  1495. */
  1496. writel(Q->cidx, adapter->regs + A_SG_SLEEPING);
  1497. if (!work_done)
  1498. adapter->sge->stats.unhandled_irqs++;
  1499. spin_unlock(&adapter->async_lock);
  1500. return IRQ_RETVAL(work_done != 0);
  1501. }
  1502. #endif
  1503. /*
  1504. * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
  1505. *
  1506. * The code figures out how many entries the sk_buff will require in the
  1507. * cmdQ and updates the cmdQ data structure with the state once the enqueue
  1508. * has complete. Then, it doesn't access the global structure anymore, but
  1509. * uses the corresponding fields on the stack. In conjuction with a spinlock
  1510. * around that code, we can make the function reentrant without holding the
  1511. * lock when we actually enqueue (which might be expensive, especially on
  1512. * architectures with IO MMUs).
  1513. *
  1514. * This runs with softirqs disabled.
  1515. */
  1516. static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
  1517. unsigned int qid, struct net_device *dev)
  1518. {
  1519. struct sge *sge = adapter->sge;
  1520. struct cmdQ *q = &sge->cmdQ[qid];
  1521. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  1522. if (!spin_trylock(&q->lock))
  1523. return NETDEV_TX_LOCKED;
  1524. reclaim_completed_tx(sge, q);
  1525. pidx = q->pidx;
  1526. credits = q->size - q->in_use;
  1527. count = 1 + skb_shinfo(skb)->nr_frags;
  1528. count += compute_large_page_tx_descs(skb);
  1529. /* Ethernet packet */
  1530. if (unlikely(credits < count)) {
  1531. if (!netif_queue_stopped(dev)) {
  1532. netif_stop_queue(dev);
  1533. set_bit(dev->if_port, &sge->stopped_tx_queues);
  1534. sge->stats.cmdQ_full[2]++;
  1535. CH_ERR("%s: Tx ring full while queue awake!\n",
  1536. adapter->name);
  1537. }
  1538. spin_unlock(&q->lock);
  1539. return NETDEV_TX_BUSY;
  1540. }
  1541. if (unlikely(credits - count < q->stop_thres)) {
  1542. netif_stop_queue(dev);
  1543. set_bit(dev->if_port, &sge->stopped_tx_queues);
  1544. sge->stats.cmdQ_full[2]++;
  1545. }
  1546. /* T204 cmdQ0 skbs that are destined for a certain port have to go
  1547. * through the scheduler.
  1548. */
  1549. if (sge->tx_sched && !qid && skb->dev) {
  1550. use_sched:
  1551. use_sched_skb = 1;
  1552. /* Note that the scheduler might return a different skb than
  1553. * the one passed in.
  1554. */
  1555. skb = sched_skb(sge, skb, credits);
  1556. if (!skb) {
  1557. spin_unlock(&q->lock);
  1558. return NETDEV_TX_OK;
  1559. }
  1560. pidx = q->pidx;
  1561. count = 1 + skb_shinfo(skb)->nr_frags;
  1562. count += compute_large_page_tx_descs(skb);
  1563. }
  1564. q->in_use += count;
  1565. genbit = q->genbit;
  1566. pidx = q->pidx;
  1567. q->pidx += count;
  1568. if (q->pidx >= q->size) {
  1569. q->pidx -= q->size;
  1570. q->genbit ^= 1;
  1571. }
  1572. spin_unlock(&q->lock);
  1573. write_tx_descs(adapter, skb, pidx, genbit, q);
  1574. /*
  1575. * We always ring the doorbell for cmdQ1. For cmdQ0, we only ring
  1576. * the doorbell if the Q is asleep. There is a natural race, where
  1577. * the hardware is going to sleep just after we checked, however,
  1578. * then the interrupt handler will detect the outstanding TX packet
  1579. * and ring the doorbell for us.
  1580. */
  1581. if (qid)
  1582. doorbell_pio(adapter, F_CMDQ1_ENABLE);
  1583. else {
  1584. clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  1585. if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
  1586. set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  1587. writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
  1588. }
  1589. }
  1590. if (use_sched_skb) {
  1591. if (spin_trylock(&q->lock)) {
  1592. credits = q->size - q->in_use;
  1593. skb = NULL;
  1594. goto use_sched;
  1595. }
  1596. }
  1597. return NETDEV_TX_OK;
  1598. }
  1599. #define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
  1600. /*
  1601. * eth_hdr_len - return the length of an Ethernet header
  1602. * @data: pointer to the start of the Ethernet header
  1603. *
  1604. * Returns the length of an Ethernet header, including optional VLAN tag.
  1605. */
  1606. static inline int eth_hdr_len(const void *data)
  1607. {
  1608. const struct ethhdr *e = data;
  1609. return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
  1610. }
  1611. /*
  1612. * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
  1613. */
  1614. int t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1615. {
  1616. struct adapter *adapter = dev->priv;
  1617. struct sge *sge = adapter->sge;
  1618. struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[dev->if_port], smp_processor_id());
  1619. struct cpl_tx_pkt *cpl;
  1620. struct sk_buff *orig_skb = skb;
  1621. int ret;
  1622. if (skb->protocol == htons(ETH_P_CPL5))
  1623. goto send;
  1624. if (skb_shinfo(skb)->gso_size) {
  1625. int eth_type;
  1626. struct cpl_tx_pkt_lso *hdr;
  1627. ++st->tx_tso;
  1628. eth_type = skb->nh.raw - skb->data == ETH_HLEN ?
  1629. CPL_ETH_II : CPL_ETH_II_VLAN;
  1630. hdr = (struct cpl_tx_pkt_lso *)skb_push(skb, sizeof(*hdr));
  1631. hdr->opcode = CPL_TX_PKT_LSO;
  1632. hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
  1633. hdr->ip_hdr_words = skb->nh.iph->ihl;
  1634. hdr->tcp_hdr_words = skb->h.th->doff;
  1635. hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
  1636. skb_shinfo(skb)->gso_size));
  1637. hdr->len = htonl(skb->len - sizeof(*hdr));
  1638. cpl = (struct cpl_tx_pkt *)hdr;
  1639. } else {
  1640. /*
  1641. * Packets shorter than ETH_HLEN can break the MAC, drop them
  1642. * early. Also, we may get oversized packets because some
  1643. * parts of the kernel don't handle our unusual hard_header_len
  1644. * right, drop those too.
  1645. */
  1646. if (unlikely(skb->len < ETH_HLEN ||
  1647. skb->len > dev->mtu + eth_hdr_len(skb->data))) {
  1648. pr_debug("%s: packet size %d hdr %d mtu%d\n", dev->name,
  1649. skb->len, eth_hdr_len(skb->data), dev->mtu);
  1650. dev_kfree_skb_any(skb);
  1651. return NETDEV_TX_OK;
  1652. }
  1653. /*
  1654. * We are using a non-standard hard_header_len and some kernel
  1655. * components, such as pktgen, do not handle it right.
  1656. * Complain when this happens but try to fix things up.
  1657. */
  1658. if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
  1659. pr_debug("%s: headroom %d header_len %d\n", dev->name,
  1660. skb_headroom(skb), dev->hard_header_len);
  1661. if (net_ratelimit())
  1662. printk(KERN_ERR "%s: inadequate headroom in "
  1663. "Tx packet\n", dev->name);
  1664. skb = skb_realloc_headroom(skb, sizeof(*cpl));
  1665. dev_kfree_skb_any(orig_skb);
  1666. if (!skb)
  1667. return NETDEV_TX_OK;
  1668. }
  1669. if (!(adapter->flags & UDP_CSUM_CAPABLE) &&
  1670. skb->ip_summed == CHECKSUM_PARTIAL &&
  1671. skb->nh.iph->protocol == IPPROTO_UDP) {
  1672. if (unlikely(skb_checksum_help(skb))) {
  1673. pr_debug("%s: unable to do udp checksum\n", dev->name);
  1674. dev_kfree_skb_any(skb);
  1675. return NETDEV_TX_OK;
  1676. }
  1677. }
  1678. /* Hmmm, assuming to catch the gratious arp... and we'll use
  1679. * it to flush out stuck espi packets...
  1680. */
  1681. if ((unlikely(!adapter->sge->espibug_skb[dev->if_port]))) {
  1682. if (skb->protocol == htons(ETH_P_ARP) &&
  1683. skb->nh.arph->ar_op == htons(ARPOP_REQUEST)) {
  1684. adapter->sge->espibug_skb[dev->if_port] = skb;
  1685. /* We want to re-use this skb later. We
  1686. * simply bump the reference count and it
  1687. * will not be freed...
  1688. */
  1689. skb = skb_get(skb);
  1690. }
  1691. }
  1692. cpl = (struct cpl_tx_pkt *)__skb_push(skb, sizeof(*cpl));
  1693. cpl->opcode = CPL_TX_PKT;
  1694. cpl->ip_csum_dis = 1; /* SW calculates IP csum */
  1695. cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
  1696. /* the length field isn't used so don't bother setting it */
  1697. st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
  1698. }
  1699. cpl->iff = dev->if_port;
  1700. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  1701. if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
  1702. cpl->vlan_valid = 1;
  1703. cpl->vlan = htons(vlan_tx_tag_get(skb));
  1704. st->vlan_insert++;
  1705. } else
  1706. #endif
  1707. cpl->vlan_valid = 0;
  1708. send:
  1709. st->tx_packets++;
  1710. dev->trans_start = jiffies;
  1711. ret = t1_sge_tx(skb, adapter, 0, dev);
  1712. /* If transmit busy, and we reallocated skb's due to headroom limit,
  1713. * then silently discard to avoid leak.
  1714. */
  1715. if (unlikely(ret != NETDEV_TX_OK && skb != orig_skb)) {
  1716. dev_kfree_skb_any(skb);
  1717. ret = NETDEV_TX_OK;
  1718. }
  1719. return ret;
  1720. }
  1721. /*
  1722. * Callback for the Tx buffer reclaim timer. Runs with softirqs disabled.
  1723. */
  1724. static void sge_tx_reclaim_cb(unsigned long data)
  1725. {
  1726. int i;
  1727. struct sge *sge = (struct sge *)data;
  1728. for (i = 0; i < SGE_CMDQ_N; ++i) {
  1729. struct cmdQ *q = &sge->cmdQ[i];
  1730. if (!spin_trylock(&q->lock))
  1731. continue;
  1732. reclaim_completed_tx(sge, q);
  1733. if (i == 0 && q->in_use) { /* flush pending credits */
  1734. writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
  1735. }
  1736. spin_unlock(&q->lock);
  1737. }
  1738. mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
  1739. }
  1740. /*
  1741. * Propagate changes of the SGE coalescing parameters to the HW.
  1742. */
  1743. int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
  1744. {
  1745. sge->fixed_intrtimer = p->rx_coalesce_usecs *
  1746. core_ticks_per_usec(sge->adapter);
  1747. writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
  1748. return 0;
  1749. }
  1750. /*
  1751. * Allocates both RX and TX resources and configures the SGE. However,
  1752. * the hardware is not enabled yet.
  1753. */
  1754. int t1_sge_configure(struct sge *sge, struct sge_params *p)
  1755. {
  1756. if (alloc_rx_resources(sge, p))
  1757. return -ENOMEM;
  1758. if (alloc_tx_resources(sge, p)) {
  1759. free_rx_resources(sge);
  1760. return -ENOMEM;
  1761. }
  1762. configure_sge(sge, p);
  1763. /*
  1764. * Now that we have sized the free lists calculate the payload
  1765. * capacity of the large buffers. Other parts of the driver use
  1766. * this to set the max offload coalescing size so that RX packets
  1767. * do not overflow our large buffers.
  1768. */
  1769. p->large_buf_capacity = jumbo_payload_capacity(sge);
  1770. return 0;
  1771. }
  1772. /*
  1773. * Disables the DMA engine.
  1774. */
  1775. void t1_sge_stop(struct sge *sge)
  1776. {
  1777. int i;
  1778. writel(0, sge->adapter->regs + A_SG_CONTROL);
  1779. readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
  1780. if (is_T2(sge->adapter))
  1781. del_timer_sync(&sge->espibug_timer);
  1782. del_timer_sync(&sge->tx_reclaim_timer);
  1783. if (sge->tx_sched)
  1784. tx_sched_stop(sge);
  1785. for (i = 0; i < MAX_NPORTS; i++)
  1786. if (sge->espibug_skb[i])
  1787. kfree_skb(sge->espibug_skb[i]);
  1788. }
  1789. /*
  1790. * Enables the DMA engine.
  1791. */
  1792. void t1_sge_start(struct sge *sge)
  1793. {
  1794. refill_free_list(sge, &sge->freelQ[0]);
  1795. refill_free_list(sge, &sge->freelQ[1]);
  1796. writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
  1797. doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
  1798. readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
  1799. mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
  1800. if (is_T2(sge->adapter))
  1801. mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
  1802. }
  1803. /*
  1804. * Callback for the T2 ESPI 'stuck packet feature' workaorund
  1805. */
  1806. static void espibug_workaround_t204(unsigned long data)
  1807. {
  1808. struct adapter *adapter = (struct adapter *)data;
  1809. struct sge *sge = adapter->sge;
  1810. unsigned int nports = adapter->params.nports;
  1811. u32 seop[MAX_NPORTS];
  1812. if (adapter->open_device_map & PORT_MASK) {
  1813. int i;
  1814. if (t1_espi_get_mon_t204(adapter, &(seop[0]), 0) < 0)
  1815. return;
  1816. for (i = 0; i < nports; i++) {
  1817. struct sk_buff *skb = sge->espibug_skb[i];
  1818. if (!netif_running(adapter->port[i].dev) ||
  1819. netif_queue_stopped(adapter->port[i].dev) ||
  1820. !seop[i] || ((seop[i] & 0xfff) != 0) || !skb)
  1821. continue;
  1822. if (!skb->cb[0]) {
  1823. u8 ch_mac_addr[ETH_ALEN] = {
  1824. 0x0, 0x7, 0x43, 0x0, 0x0, 0x0
  1825. };
  1826. memcpy(skb->data + sizeof(struct cpl_tx_pkt),
  1827. ch_mac_addr, ETH_ALEN);
  1828. memcpy(skb->data + skb->len - 10,
  1829. ch_mac_addr, ETH_ALEN);
  1830. skb->cb[0] = 0xff;
  1831. }
  1832. /* bump the reference count to avoid freeing of
  1833. * the skb once the DMA has completed.
  1834. */
  1835. skb = skb_get(skb);
  1836. t1_sge_tx(skb, adapter, 0, adapter->port[i].dev);
  1837. }
  1838. }
  1839. mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
  1840. }
  1841. static void espibug_workaround(unsigned long data)
  1842. {
  1843. struct adapter *adapter = (struct adapter *)data;
  1844. struct sge *sge = adapter->sge;
  1845. if (netif_running(adapter->port[0].dev)) {
  1846. struct sk_buff *skb = sge->espibug_skb[0];
  1847. u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
  1848. if ((seop & 0xfff0fff) == 0xfff && skb) {
  1849. if (!skb->cb[0]) {
  1850. u8 ch_mac_addr[ETH_ALEN] =
  1851. {0x0, 0x7, 0x43, 0x0, 0x0, 0x0};
  1852. memcpy(skb->data + sizeof(struct cpl_tx_pkt),
  1853. ch_mac_addr, ETH_ALEN);
  1854. memcpy(skb->data + skb->len - 10, ch_mac_addr,
  1855. ETH_ALEN);
  1856. skb->cb[0] = 0xff;
  1857. }
  1858. /* bump the reference count to avoid freeing of the
  1859. * skb once the DMA has completed.
  1860. */
  1861. skb = skb_get(skb);
  1862. t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
  1863. }
  1864. }
  1865. mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
  1866. }
  1867. /*
  1868. * Creates a t1_sge structure and returns suggested resource parameters.
  1869. */
  1870. struct sge * __devinit t1_sge_create(struct adapter *adapter,
  1871. struct sge_params *p)
  1872. {
  1873. struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
  1874. int i;
  1875. if (!sge)
  1876. return NULL;
  1877. sge->adapter = adapter;
  1878. sge->netdev = adapter->port[0].dev;
  1879. sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
  1880. sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
  1881. for_each_port(adapter, i) {
  1882. sge->port_stats[i] = alloc_percpu(struct sge_port_stats);
  1883. if (!sge->port_stats[i])
  1884. goto nomem_port;
  1885. }
  1886. init_timer(&sge->tx_reclaim_timer);
  1887. sge->tx_reclaim_timer.data = (unsigned long)sge;
  1888. sge->tx_reclaim_timer.function = sge_tx_reclaim_cb;
  1889. if (is_T2(sge->adapter)) {
  1890. init_timer(&sge->espibug_timer);
  1891. if (adapter->params.nports > 1) {
  1892. tx_sched_init(sge);
  1893. sge->espibug_timer.function = espibug_workaround_t204;
  1894. } else
  1895. sge->espibug_timer.function = espibug_workaround;
  1896. sge->espibug_timer.data = (unsigned long)sge->adapter;
  1897. sge->espibug_timeout = 1;
  1898. /* for T204, every 10ms */
  1899. if (adapter->params.nports > 1)
  1900. sge->espibug_timeout = HZ/100;
  1901. }
  1902. p->cmdQ_size[0] = SGE_CMDQ0_E_N;
  1903. p->cmdQ_size[1] = SGE_CMDQ1_E_N;
  1904. p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
  1905. p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
  1906. if (sge->tx_sched) {
  1907. if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204)
  1908. p->rx_coalesce_usecs = 15;
  1909. else
  1910. p->rx_coalesce_usecs = 50;
  1911. } else
  1912. p->rx_coalesce_usecs = 50;
  1913. p->coalesce_enable = 0;
  1914. p->sample_interval_usecs = 0;
  1915. return sge;
  1916. nomem_port:
  1917. while (i >= 0) {
  1918. free_percpu(sge->port_stats[i]);
  1919. --i;
  1920. }
  1921. kfree(sge);
  1922. return NULL;
  1923. }