sge.c 58 KB

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