sge.c 58 KB

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