sge.c 58 KB

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