sge.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221
  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. /*
  299. * t1_sched_max_avail_bytes() tells the scheduler the maximum amount of
  300. * data that can be pushed per port.
  301. */
  302. void t1_sched_set_max_avail_bytes(struct sge *sge, unsigned int val)
  303. {
  304. struct sched *s = sge->tx_sched;
  305. unsigned int i;
  306. s->max_avail = val;
  307. for (i = 0; i < MAX_NPORTS; i++)
  308. t1_sched_update_parms(sge, i, 0, 0);
  309. }
  310. /*
  311. * t1_sched_set_drain_bits_per_us() tells the scheduler at which rate a port
  312. * is draining.
  313. */
  314. void t1_sched_set_drain_bits_per_us(struct sge *sge, unsigned int port,
  315. unsigned int val)
  316. {
  317. struct sched *s = sge->tx_sched;
  318. struct sched_port *p = &s->p[port];
  319. p->drain_bits_per_1024ns = val * 1024 / 1000;
  320. t1_sched_update_parms(sge, port, 0, 0);
  321. }
  322. /*
  323. * get_clock() implements a ns clock (see ktime_get)
  324. */
  325. static inline ktime_t get_clock(void)
  326. {
  327. struct timespec ts;
  328. ktime_get_ts(&ts);
  329. return timespec_to_ktime(ts);
  330. }
  331. /*
  332. * tx_sched_init() allocates resources and does basic initialization.
  333. */
  334. static int tx_sched_init(struct sge *sge)
  335. {
  336. struct sched *s;
  337. int i;
  338. s = kzalloc(sizeof (struct sched), GFP_KERNEL);
  339. if (!s)
  340. return -ENOMEM;
  341. pr_debug("tx_sched_init\n");
  342. tasklet_init(&s->sched_tsk, restart_sched, (unsigned long) sge);
  343. sge->tx_sched = s;
  344. for (i = 0; i < MAX_NPORTS; i++) {
  345. skb_queue_head_init(&s->p[i].skbq);
  346. t1_sched_update_parms(sge, i, 1500, 1000);
  347. }
  348. return 0;
  349. }
  350. /*
  351. * sched_update_avail() computes the delta since the last time it was called
  352. * and updates the per port quota (number of bits that can be sent to the any
  353. * port).
  354. */
  355. static inline int sched_update_avail(struct sge *sge)
  356. {
  357. struct sched *s = sge->tx_sched;
  358. ktime_t now = get_clock();
  359. unsigned int i;
  360. long long delta_time_ns;
  361. delta_time_ns = ktime_to_ns(ktime_sub(now, s->last_updated));
  362. pr_debug("sched_update_avail delta=%lld\n", delta_time_ns);
  363. if (delta_time_ns < 15000)
  364. return 0;
  365. for (i = 0; i < MAX_NPORTS; i++) {
  366. struct sched_port *p = &s->p[i];
  367. unsigned int delta_avail;
  368. delta_avail = (p->drain_bits_per_1024ns * delta_time_ns) >> 13;
  369. p->avail = min(p->avail + delta_avail, s->max_avail);
  370. }
  371. s->last_updated = now;
  372. return 1;
  373. }
  374. /*
  375. * sched_skb() is called from two different places. In the tx path, any
  376. * packet generating load on an output port will call sched_skb()
  377. * (skb != NULL). In addition, sched_skb() is called from the irq/soft irq
  378. * context (skb == NULL).
  379. * The scheduler only returns a skb (which will then be sent) if the
  380. * length of the skb is <= the current quota of the output port.
  381. */
  382. static struct sk_buff *sched_skb(struct sge *sge, struct sk_buff *skb,
  383. unsigned int credits)
  384. {
  385. struct sched *s = sge->tx_sched;
  386. struct sk_buff_head *skbq;
  387. unsigned int i, len, update = 1;
  388. pr_debug("sched_skb %p\n", skb);
  389. if (!skb) {
  390. if (!s->num)
  391. return NULL;
  392. } else {
  393. skbq = &s->p[skb->dev->if_port].skbq;
  394. __skb_queue_tail(skbq, skb);
  395. s->num++;
  396. skb = NULL;
  397. }
  398. if (credits < MAX_SKB_FRAGS + 1)
  399. goto out;
  400. again:
  401. for (i = 0; i < MAX_NPORTS; i++) {
  402. s->port = ++s->port & (MAX_NPORTS - 1);
  403. skbq = &s->p[s->port].skbq;
  404. skb = skb_peek(skbq);
  405. if (!skb)
  406. continue;
  407. len = skb->len;
  408. if (len <= s->p[s->port].avail) {
  409. s->p[s->port].avail -= len;
  410. s->num--;
  411. __skb_unlink(skb, skbq);
  412. goto out;
  413. }
  414. skb = NULL;
  415. }
  416. if (update-- && sched_update_avail(sge))
  417. goto again;
  418. out:
  419. /* If there are more pending skbs, we use the hardware to schedule us
  420. * again.
  421. */
  422. if (s->num && !skb) {
  423. struct cmdQ *q = &sge->cmdQ[0];
  424. clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  425. if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
  426. set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  427. writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
  428. }
  429. }
  430. pr_debug("sched_skb ret %p\n", skb);
  431. return skb;
  432. }
  433. /*
  434. * PIO to indicate that memory mapped Q contains valid descriptor(s).
  435. */
  436. static inline void doorbell_pio(struct adapter *adapter, u32 val)
  437. {
  438. wmb();
  439. writel(val, adapter->regs + A_SG_DOORBELL);
  440. }
  441. /*
  442. * Frees all RX buffers on the freelist Q. The caller must make sure that
  443. * the SGE is turned off before calling this function.
  444. */
  445. static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *q)
  446. {
  447. unsigned int cidx = q->cidx;
  448. while (q->credits--) {
  449. struct freelQ_ce *ce = &q->centries[cidx];
  450. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  451. pci_unmap_len(ce, dma_len),
  452. PCI_DMA_FROMDEVICE);
  453. dev_kfree_skb(ce->skb);
  454. ce->skb = NULL;
  455. if (++cidx == q->size)
  456. cidx = 0;
  457. }
  458. }
  459. /*
  460. * Free RX free list and response queue resources.
  461. */
  462. static void free_rx_resources(struct sge *sge)
  463. {
  464. struct pci_dev *pdev = sge->adapter->pdev;
  465. unsigned int size, i;
  466. if (sge->respQ.entries) {
  467. size = sizeof(struct respQ_e) * sge->respQ.size;
  468. pci_free_consistent(pdev, size, sge->respQ.entries,
  469. sge->respQ.dma_addr);
  470. }
  471. for (i = 0; i < SGE_FREELQ_N; i++) {
  472. struct freelQ *q = &sge->freelQ[i];
  473. if (q->centries) {
  474. free_freelQ_buffers(pdev, q);
  475. kfree(q->centries);
  476. }
  477. if (q->entries) {
  478. size = sizeof(struct freelQ_e) * q->size;
  479. pci_free_consistent(pdev, size, q->entries,
  480. q->dma_addr);
  481. }
  482. }
  483. }
  484. /*
  485. * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
  486. * response queue.
  487. */
  488. static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
  489. {
  490. struct pci_dev *pdev = sge->adapter->pdev;
  491. unsigned int size, i;
  492. for (i = 0; i < SGE_FREELQ_N; i++) {
  493. struct freelQ *q = &sge->freelQ[i];
  494. q->genbit = 1;
  495. q->size = p->freelQ_size[i];
  496. q->dma_offset = sge->rx_pkt_pad ? 0 : NET_IP_ALIGN;
  497. size = sizeof(struct freelQ_e) * q->size;
  498. q->entries = pci_alloc_consistent(pdev, size, &q->dma_addr);
  499. if (!q->entries)
  500. goto err_no_mem;
  501. size = sizeof(struct freelQ_ce) * q->size;
  502. q->centries = kzalloc(size, GFP_KERNEL);
  503. if (!q->centries)
  504. goto err_no_mem;
  505. }
  506. /*
  507. * Calculate the buffer sizes for the two free lists. FL0 accommodates
  508. * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
  509. * including all the sk_buff overhead.
  510. *
  511. * Note: For T2 FL0 and FL1 are reversed.
  512. */
  513. sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
  514. sizeof(struct cpl_rx_data) +
  515. sge->freelQ[!sge->jumbo_fl].dma_offset;
  516. size = (16 * 1024) -
  517. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  518. sge->freelQ[sge->jumbo_fl].rx_buffer_size = size;
  519. /*
  520. * Setup which skb recycle Q should be used when recycling buffers from
  521. * each free list.
  522. */
  523. sge->freelQ[!sge->jumbo_fl].recycleq_idx = 0;
  524. sge->freelQ[sge->jumbo_fl].recycleq_idx = 1;
  525. sge->respQ.genbit = 1;
  526. sge->respQ.size = SGE_RESPQ_E_N;
  527. sge->respQ.credits = 0;
  528. size = sizeof(struct respQ_e) * sge->respQ.size;
  529. sge->respQ.entries =
  530. pci_alloc_consistent(pdev, size, &sge->respQ.dma_addr);
  531. if (!sge->respQ.entries)
  532. goto err_no_mem;
  533. return 0;
  534. err_no_mem:
  535. free_rx_resources(sge);
  536. return -ENOMEM;
  537. }
  538. /*
  539. * Reclaims n TX descriptors and frees the buffers associated with them.
  540. */
  541. static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *q, unsigned int n)
  542. {
  543. struct cmdQ_ce *ce;
  544. struct pci_dev *pdev = sge->adapter->pdev;
  545. unsigned int cidx = q->cidx;
  546. q->in_use -= n;
  547. ce = &q->centries[cidx];
  548. while (n--) {
  549. if (likely(pci_unmap_len(ce, dma_len))) {
  550. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  551. pci_unmap_len(ce, dma_len),
  552. PCI_DMA_TODEVICE);
  553. if (q->sop)
  554. q->sop = 0;
  555. }
  556. if (ce->skb) {
  557. dev_kfree_skb_any(ce->skb);
  558. q->sop = 1;
  559. }
  560. ce++;
  561. if (++cidx == q->size) {
  562. cidx = 0;
  563. ce = q->centries;
  564. }
  565. }
  566. q->cidx = cidx;
  567. }
  568. /*
  569. * Free TX resources.
  570. *
  571. * Assumes that SGE is stopped and all interrupts are disabled.
  572. */
  573. static void free_tx_resources(struct sge *sge)
  574. {
  575. struct pci_dev *pdev = sge->adapter->pdev;
  576. unsigned int size, i;
  577. for (i = 0; i < SGE_CMDQ_N; i++) {
  578. struct cmdQ *q = &sge->cmdQ[i];
  579. if (q->centries) {
  580. if (q->in_use)
  581. free_cmdQ_buffers(sge, q, q->in_use);
  582. kfree(q->centries);
  583. }
  584. if (q->entries) {
  585. size = sizeof(struct cmdQ_e) * q->size;
  586. pci_free_consistent(pdev, size, q->entries,
  587. q->dma_addr);
  588. }
  589. }
  590. }
  591. /*
  592. * Allocates basic TX resources, consisting of memory mapped command Qs.
  593. */
  594. static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
  595. {
  596. struct pci_dev *pdev = sge->adapter->pdev;
  597. unsigned int size, i;
  598. for (i = 0; i < SGE_CMDQ_N; i++) {
  599. struct cmdQ *q = &sge->cmdQ[i];
  600. q->genbit = 1;
  601. q->sop = 1;
  602. q->size = p->cmdQ_size[i];
  603. q->in_use = 0;
  604. q->status = 0;
  605. q->processed = q->cleaned = 0;
  606. q->stop_thres = 0;
  607. spin_lock_init(&q->lock);
  608. size = sizeof(struct cmdQ_e) * q->size;
  609. q->entries = pci_alloc_consistent(pdev, size, &q->dma_addr);
  610. if (!q->entries)
  611. goto err_no_mem;
  612. size = sizeof(struct cmdQ_ce) * q->size;
  613. q->centries = kzalloc(size, GFP_KERNEL);
  614. if (!q->centries)
  615. goto err_no_mem;
  616. }
  617. /*
  618. * CommandQ 0 handles Ethernet and TOE packets, while queue 1 is TOE
  619. * only. For queue 0 set the stop threshold so we can handle one more
  620. * packet from each port, plus reserve an additional 24 entries for
  621. * Ethernet packets only. Queue 1 never suspends nor do we reserve
  622. * space for Ethernet packets.
  623. */
  624. sge->cmdQ[0].stop_thres = sge->adapter->params.nports *
  625. (MAX_SKB_FRAGS + 1);
  626. return 0;
  627. err_no_mem:
  628. free_tx_resources(sge);
  629. return -ENOMEM;
  630. }
  631. static inline void setup_ring_params(struct adapter *adapter, u64 addr,
  632. u32 size, int base_reg_lo,
  633. int base_reg_hi, int size_reg)
  634. {
  635. writel((u32)addr, adapter->regs + base_reg_lo);
  636. writel(addr >> 32, adapter->regs + base_reg_hi);
  637. writel(size, adapter->regs + size_reg);
  638. }
  639. /*
  640. * Enable/disable VLAN acceleration.
  641. */
  642. void t1_set_vlan_accel(struct adapter *adapter, int on_off)
  643. {
  644. struct sge *sge = adapter->sge;
  645. sge->sge_control &= ~F_VLAN_XTRACT;
  646. if (on_off)
  647. sge->sge_control |= F_VLAN_XTRACT;
  648. if (adapter->open_device_map) {
  649. writel(sge->sge_control, adapter->regs + A_SG_CONTROL);
  650. readl(adapter->regs + A_SG_CONTROL); /* flush */
  651. }
  652. }
  653. /*
  654. * Programs the various SGE registers. However, the engine is not yet enabled,
  655. * but sge->sge_control is setup and ready to go.
  656. */
  657. static void configure_sge(struct sge *sge, struct sge_params *p)
  658. {
  659. struct adapter *ap = sge->adapter;
  660. writel(0, ap->regs + A_SG_CONTROL);
  661. setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].size,
  662. A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
  663. setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].size,
  664. A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
  665. setup_ring_params(ap, sge->freelQ[0].dma_addr,
  666. sge->freelQ[0].size, A_SG_FL0BASELWR,
  667. A_SG_FL0BASEUPR, A_SG_FL0SIZE);
  668. setup_ring_params(ap, sge->freelQ[1].dma_addr,
  669. sge->freelQ[1].size, A_SG_FL1BASELWR,
  670. A_SG_FL1BASEUPR, A_SG_FL1SIZE);
  671. /* The threshold comparison uses <. */
  672. writel(SGE_RX_SM_BUF_SIZE + 1, ap->regs + A_SG_FLTHRESHOLD);
  673. setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.size,
  674. A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
  675. writel((u32)sge->respQ.size - 1, ap->regs + A_SG_RSPQUEUECREDIT);
  676. sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
  677. F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
  678. V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
  679. V_RX_PKT_OFFSET(sge->rx_pkt_pad);
  680. #if defined(__BIG_ENDIAN_BITFIELD)
  681. sge->sge_control |= F_ENABLE_BIG_ENDIAN;
  682. #endif
  683. /* Initialize no-resource timer */
  684. sge->intrtimer_nres = SGE_INTRTIMER_NRES * core_ticks_per_usec(ap);
  685. t1_sge_set_coalesce_params(sge, p);
  686. }
  687. /*
  688. * Return the payload capacity of the jumbo free-list buffers.
  689. */
  690. static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
  691. {
  692. return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
  693. sge->freelQ[sge->jumbo_fl].dma_offset -
  694. sizeof(struct cpl_rx_data);
  695. }
  696. /*
  697. * Frees all SGE related resources and the sge structure itself
  698. */
  699. void t1_sge_destroy(struct sge *sge)
  700. {
  701. int i;
  702. for_each_port(sge->adapter, i)
  703. free_percpu(sge->port_stats[i]);
  704. kfree(sge->tx_sched);
  705. free_tx_resources(sge);
  706. free_rx_resources(sge);
  707. kfree(sge);
  708. }
  709. /*
  710. * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
  711. * context Q) until the Q is full or alloc_skb fails.
  712. *
  713. * It is possible that the generation bits already match, indicating that the
  714. * buffer is already valid and nothing needs to be done. This happens when we
  715. * copied a received buffer into a new sk_buff during the interrupt processing.
  716. *
  717. * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
  718. * we specify a RX_OFFSET in order to make sure that the IP header is 4B
  719. * aligned.
  720. */
  721. static void refill_free_list(struct sge *sge, struct freelQ *q)
  722. {
  723. struct pci_dev *pdev = sge->adapter->pdev;
  724. struct freelQ_ce *ce = &q->centries[q->pidx];
  725. struct freelQ_e *e = &q->entries[q->pidx];
  726. unsigned int dma_len = q->rx_buffer_size - q->dma_offset;
  727. while (q->credits < q->size) {
  728. struct sk_buff *skb;
  729. dma_addr_t mapping;
  730. skb = alloc_skb(q->rx_buffer_size, GFP_ATOMIC);
  731. if (!skb)
  732. break;
  733. skb_reserve(skb, q->dma_offset);
  734. mapping = pci_map_single(pdev, skb->data, dma_len,
  735. PCI_DMA_FROMDEVICE);
  736. skb_reserve(skb, sge->rx_pkt_pad);
  737. ce->skb = skb;
  738. pci_unmap_addr_set(ce, dma_addr, mapping);
  739. pci_unmap_len_set(ce, dma_len, dma_len);
  740. e->addr_lo = (u32)mapping;
  741. e->addr_hi = (u64)mapping >> 32;
  742. e->len_gen = V_CMD_LEN(dma_len) | V_CMD_GEN1(q->genbit);
  743. wmb();
  744. e->gen2 = V_CMD_GEN2(q->genbit);
  745. e++;
  746. ce++;
  747. if (++q->pidx == q->size) {
  748. q->pidx = 0;
  749. q->genbit ^= 1;
  750. ce = q->centries;
  751. e = q->entries;
  752. }
  753. q->credits++;
  754. }
  755. }
  756. /*
  757. * Calls refill_free_list for both free lists. If we cannot fill at least 1/4
  758. * of both rings, we go into 'few interrupt mode' in order to give the system
  759. * time to free up resources.
  760. */
  761. static void freelQs_empty(struct sge *sge)
  762. {
  763. struct adapter *adapter = sge->adapter;
  764. u32 irq_reg = readl(adapter->regs + A_SG_INT_ENABLE);
  765. u32 irqholdoff_reg;
  766. refill_free_list(sge, &sge->freelQ[0]);
  767. refill_free_list(sge, &sge->freelQ[1]);
  768. if (sge->freelQ[0].credits > (sge->freelQ[0].size >> 2) &&
  769. sge->freelQ[1].credits > (sge->freelQ[1].size >> 2)) {
  770. irq_reg |= F_FL_EXHAUSTED;
  771. irqholdoff_reg = sge->fixed_intrtimer;
  772. } else {
  773. /* Clear the F_FL_EXHAUSTED interrupts for now */
  774. irq_reg &= ~F_FL_EXHAUSTED;
  775. irqholdoff_reg = sge->intrtimer_nres;
  776. }
  777. writel(irqholdoff_reg, adapter->regs + A_SG_INTRTIMER);
  778. writel(irq_reg, adapter->regs + A_SG_INT_ENABLE);
  779. /* We reenable the Qs to force a freelist GTS interrupt later */
  780. doorbell_pio(adapter, F_FL0_ENABLE | F_FL1_ENABLE);
  781. }
  782. #define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
  783. #define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
  784. #define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
  785. F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
  786. /*
  787. * Disable SGE Interrupts
  788. */
  789. void t1_sge_intr_disable(struct sge *sge)
  790. {
  791. u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
  792. writel(val & ~SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
  793. writel(0, sge->adapter->regs + A_SG_INT_ENABLE);
  794. }
  795. /*
  796. * Enable SGE interrupts.
  797. */
  798. void t1_sge_intr_enable(struct sge *sge)
  799. {
  800. u32 en = SGE_INT_ENABLE;
  801. u32 val = readl(sge->adapter->regs + A_PL_ENABLE);
  802. if (sge->adapter->flags & TSO_CAPABLE)
  803. en &= ~F_PACKET_TOO_BIG;
  804. writel(en, sge->adapter->regs + A_SG_INT_ENABLE);
  805. writel(val | SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_ENABLE);
  806. }
  807. /*
  808. * Clear SGE interrupts.
  809. */
  810. void t1_sge_intr_clear(struct sge *sge)
  811. {
  812. writel(SGE_PL_INTR_MASK, sge->adapter->regs + A_PL_CAUSE);
  813. writel(0xffffffff, sge->adapter->regs + A_SG_INT_CAUSE);
  814. }
  815. /*
  816. * SGE 'Error' interrupt handler
  817. */
  818. int t1_sge_intr_error_handler(struct sge *sge)
  819. {
  820. struct adapter *adapter = sge->adapter;
  821. u32 cause = readl(adapter->regs + A_SG_INT_CAUSE);
  822. if (adapter->flags & TSO_CAPABLE)
  823. cause &= ~F_PACKET_TOO_BIG;
  824. if (cause & F_RESPQ_EXHAUSTED)
  825. sge->stats.respQ_empty++;
  826. if (cause & F_RESPQ_OVERFLOW) {
  827. sge->stats.respQ_overflow++;
  828. CH_ALERT("%s: SGE response queue overflow\n",
  829. adapter->name);
  830. }
  831. if (cause & F_FL_EXHAUSTED) {
  832. sge->stats.freelistQ_empty++;
  833. freelQs_empty(sge);
  834. }
  835. if (cause & F_PACKET_TOO_BIG) {
  836. sge->stats.pkt_too_big++;
  837. CH_ALERT("%s: SGE max packet size exceeded\n",
  838. adapter->name);
  839. }
  840. if (cause & F_PACKET_MISMATCH) {
  841. sge->stats.pkt_mismatch++;
  842. CH_ALERT("%s: SGE packet mismatch\n", adapter->name);
  843. }
  844. if (cause & SGE_INT_FATAL)
  845. t1_fatal_err(adapter);
  846. writel(cause, adapter->regs + A_SG_INT_CAUSE);
  847. return 0;
  848. }
  849. const struct sge_intr_counts *t1_sge_get_intr_counts(const struct sge *sge)
  850. {
  851. return &sge->stats;
  852. }
  853. void t1_sge_get_port_stats(const struct sge *sge, int port,
  854. struct sge_port_stats *ss)
  855. {
  856. int cpu;
  857. memset(ss, 0, sizeof(*ss));
  858. for_each_possible_cpu(cpu) {
  859. struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[port], cpu);
  860. ss->rx_packets += st->rx_packets;
  861. ss->rx_cso_good += st->rx_cso_good;
  862. ss->tx_packets += st->tx_packets;
  863. ss->tx_cso += st->tx_cso;
  864. ss->tx_tso += st->tx_tso;
  865. ss->vlan_xtract += st->vlan_xtract;
  866. ss->vlan_insert += st->vlan_insert;
  867. }
  868. }
  869. /**
  870. * recycle_fl_buf - recycle a free list buffer
  871. * @fl: the free list
  872. * @idx: index of buffer to recycle
  873. *
  874. * Recycles the specified buffer on the given free list by adding it at
  875. * the next available slot on the list.
  876. */
  877. static void recycle_fl_buf(struct freelQ *fl, int idx)
  878. {
  879. struct freelQ_e *from = &fl->entries[idx];
  880. struct freelQ_e *to = &fl->entries[fl->pidx];
  881. fl->centries[fl->pidx] = fl->centries[idx];
  882. to->addr_lo = from->addr_lo;
  883. to->addr_hi = from->addr_hi;
  884. to->len_gen = G_CMD_LEN(from->len_gen) | V_CMD_GEN1(fl->genbit);
  885. wmb();
  886. to->gen2 = V_CMD_GEN2(fl->genbit);
  887. fl->credits++;
  888. if (++fl->pidx == fl->size) {
  889. fl->pidx = 0;
  890. fl->genbit ^= 1;
  891. }
  892. }
  893. static int copybreak __read_mostly = 256;
  894. module_param(copybreak, int, 0);
  895. MODULE_PARM_DESC(copybreak, "Receive copy threshold");
  896. /**
  897. * get_packet - return the next ingress packet buffer
  898. * @pdev: the PCI device that received the packet
  899. * @fl: the SGE free list holding the packet
  900. * @len: the actual packet length, excluding any SGE padding
  901. * @dma_pad: padding at beginning of buffer left by SGE DMA
  902. * @skb_pad: padding to be used if the packet is copied
  903. * @copy_thres: length threshold under which a packet should be copied
  904. * @drop_thres: # of remaining buffers before we start dropping packets
  905. *
  906. * Get the next packet from a free list and complete setup of the
  907. * sk_buff. If the packet is small we make a copy and recycle the
  908. * original buffer, otherwise we use the original buffer itself. If a
  909. * positive drop threshold is supplied packets are dropped and their
  910. * buffers recycled if (a) the number of remaining buffers is under the
  911. * threshold and the packet is too big to copy, or (b) the packet should
  912. * be copied but there is no memory for the copy.
  913. */
  914. static inline struct sk_buff *get_packet(struct pci_dev *pdev,
  915. struct freelQ *fl, unsigned int len)
  916. {
  917. struct sk_buff *skb;
  918. const struct freelQ_ce *ce = &fl->centries[fl->cidx];
  919. if (len < copybreak) {
  920. skb = alloc_skb(len + 2, GFP_ATOMIC);
  921. if (!skb)
  922. goto use_orig_buf;
  923. skb_reserve(skb, 2); /* align IP header */
  924. skb_put(skb, len);
  925. pci_dma_sync_single_for_cpu(pdev,
  926. pci_unmap_addr(ce, dma_addr),
  927. pci_unmap_len(ce, dma_len),
  928. PCI_DMA_FROMDEVICE);
  929. skb_copy_from_linear_data(ce->skb, skb->data, len);
  930. pci_dma_sync_single_for_device(pdev,
  931. pci_unmap_addr(ce, dma_addr),
  932. pci_unmap_len(ce, dma_len),
  933. PCI_DMA_FROMDEVICE);
  934. recycle_fl_buf(fl, fl->cidx);
  935. return skb;
  936. }
  937. use_orig_buf:
  938. if (fl->credits < 2) {
  939. recycle_fl_buf(fl, fl->cidx);
  940. return NULL;
  941. }
  942. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  943. pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
  944. skb = ce->skb;
  945. prefetch(skb->data);
  946. skb_put(skb, len);
  947. return skb;
  948. }
  949. /**
  950. * unexpected_offload - handle an unexpected offload packet
  951. * @adapter: the adapter
  952. * @fl: the free list that received the packet
  953. *
  954. * Called when we receive an unexpected offload packet (e.g., the TOE
  955. * function is disabled or the card is a NIC). Prints a message and
  956. * recycles the buffer.
  957. */
  958. static void unexpected_offload(struct adapter *adapter, struct freelQ *fl)
  959. {
  960. struct freelQ_ce *ce = &fl->centries[fl->cidx];
  961. struct sk_buff *skb = ce->skb;
  962. pci_dma_sync_single_for_cpu(adapter->pdev, pci_unmap_addr(ce, dma_addr),
  963. pci_unmap_len(ce, dma_len), PCI_DMA_FROMDEVICE);
  964. CH_ERR("%s: unexpected offload packet, cmd %u\n",
  965. adapter->name, *skb->data);
  966. recycle_fl_buf(fl, fl->cidx);
  967. }
  968. /*
  969. * T1/T2 SGE limits the maximum DMA size per TX descriptor to
  970. * SGE_TX_DESC_MAX_PLEN (16KB). If the PAGE_SIZE is larger than 16KB, the
  971. * stack might send more than SGE_TX_DESC_MAX_PLEN in a contiguous manner.
  972. * Note that the *_large_page_tx_descs stuff will be optimized out when
  973. * PAGE_SIZE <= SGE_TX_DESC_MAX_PLEN.
  974. *
  975. * compute_large_page_descs() computes how many additional descriptors are
  976. * required to break down the stack's request.
  977. */
  978. static inline unsigned int compute_large_page_tx_descs(struct sk_buff *skb)
  979. {
  980. unsigned int count = 0;
  981. if (PAGE_SIZE > SGE_TX_DESC_MAX_PLEN) {
  982. unsigned int nfrags = skb_shinfo(skb)->nr_frags;
  983. unsigned int i, len = skb->len - skb->data_len;
  984. while (len > SGE_TX_DESC_MAX_PLEN) {
  985. count++;
  986. len -= SGE_TX_DESC_MAX_PLEN;
  987. }
  988. for (i = 0; nfrags--; i++) {
  989. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  990. len = frag->size;
  991. while (len > SGE_TX_DESC_MAX_PLEN) {
  992. count++;
  993. len -= SGE_TX_DESC_MAX_PLEN;
  994. }
  995. }
  996. }
  997. return count;
  998. }
  999. /*
  1000. * Write a cmdQ entry.
  1001. *
  1002. * Since this function writes the 'flags' field, it must not be used to
  1003. * write the first cmdQ entry.
  1004. */
  1005. static inline void write_tx_desc(struct cmdQ_e *e, dma_addr_t mapping,
  1006. unsigned int len, unsigned int gen,
  1007. unsigned int eop)
  1008. {
  1009. if (unlikely(len > SGE_TX_DESC_MAX_PLEN))
  1010. BUG();
  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. pci_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->len - skb->data_len, PCI_DMA_TODEVICE);
  1074. desc_mapping = mapping;
  1075. desc_len = skb->len - skb->data_len;
  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. pci_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. pci_unmap_addr_set(ce, dma_addr, mapping);
  1107. pci_unmap_len_set(ce, dma_len, skb->len - skb->data_len);
  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. pci_unmap_addr_set(ce, dma_addr, mapping);
  1131. pci_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. skb->dev->last_rx = jiffies;
  1216. st = per_cpu_ptr(sge->port_stats[p->iff], smp_processor_id());
  1217. st->rx_packets++;
  1218. skb->protocol = eth_type_trans(skb, adapter->port[p->iff].dev);
  1219. if ((adapter->flags & RX_CSUM_ENABLED) && p->csum == 0xffff &&
  1220. skb->protocol == htons(ETH_P_IP) &&
  1221. (skb->data[9] == IPPROTO_TCP || skb->data[9] == IPPROTO_UDP)) {
  1222. ++st->rx_cso_good;
  1223. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1224. } else
  1225. skb->ip_summed = CHECKSUM_NONE;
  1226. if (unlikely(adapter->vlan_grp && p->vlan_valid)) {
  1227. st->vlan_xtract++;
  1228. #ifdef CONFIG_CHELSIO_T1_NAPI
  1229. vlan_hwaccel_receive_skb(skb, adapter->vlan_grp,
  1230. ntohs(p->vlan));
  1231. #else
  1232. vlan_hwaccel_rx(skb, adapter->vlan_grp,
  1233. ntohs(p->vlan));
  1234. #endif
  1235. } else {
  1236. #ifdef CONFIG_CHELSIO_T1_NAPI
  1237. netif_receive_skb(skb);
  1238. #else
  1239. netif_rx(skb);
  1240. #endif
  1241. }
  1242. }
  1243. /*
  1244. * Returns true if a command queue has enough available descriptors that
  1245. * we can resume Tx operation after temporarily disabling its packet queue.
  1246. */
  1247. static inline int enough_free_Tx_descs(const struct cmdQ *q)
  1248. {
  1249. unsigned int r = q->processed - q->cleaned;
  1250. return q->in_use - r < (q->size >> 1);
  1251. }
  1252. /*
  1253. * Called when sufficient space has become available in the SGE command queues
  1254. * after the Tx packet schedulers have been suspended to restart the Tx path.
  1255. */
  1256. static void restart_tx_queues(struct sge *sge)
  1257. {
  1258. struct adapter *adap = sge->adapter;
  1259. int i;
  1260. if (!enough_free_Tx_descs(&sge->cmdQ[0]))
  1261. return;
  1262. for_each_port(adap, i) {
  1263. struct net_device *nd = adap->port[i].dev;
  1264. if (test_and_clear_bit(nd->if_port, &sge->stopped_tx_queues) &&
  1265. netif_running(nd)) {
  1266. sge->stats.cmdQ_restarted[2]++;
  1267. netif_wake_queue(nd);
  1268. }
  1269. }
  1270. }
  1271. /*
  1272. * update_tx_info is called from the interrupt handler/NAPI to return cmdQ0
  1273. * information.
  1274. */
  1275. static unsigned int update_tx_info(struct adapter *adapter,
  1276. unsigned int flags,
  1277. unsigned int pr0)
  1278. {
  1279. struct sge *sge = adapter->sge;
  1280. struct cmdQ *cmdq = &sge->cmdQ[0];
  1281. cmdq->processed += pr0;
  1282. if (flags & (F_FL0_ENABLE | F_FL1_ENABLE)) {
  1283. freelQs_empty(sge);
  1284. flags &= ~(F_FL0_ENABLE | F_FL1_ENABLE);
  1285. }
  1286. if (flags & F_CMDQ0_ENABLE) {
  1287. clear_bit(CMDQ_STAT_RUNNING, &cmdq->status);
  1288. if (cmdq->cleaned + cmdq->in_use != cmdq->processed &&
  1289. !test_and_set_bit(CMDQ_STAT_LAST_PKT_DB, &cmdq->status)) {
  1290. set_bit(CMDQ_STAT_RUNNING, &cmdq->status);
  1291. writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
  1292. }
  1293. if (sge->tx_sched)
  1294. tasklet_hi_schedule(&sge->tx_sched->sched_tsk);
  1295. flags &= ~F_CMDQ0_ENABLE;
  1296. }
  1297. if (unlikely(sge->stopped_tx_queues != 0))
  1298. restart_tx_queues(sge);
  1299. return flags;
  1300. }
  1301. /*
  1302. * Process SGE responses, up to the supplied budget. Returns the number of
  1303. * responses processed. A negative budget is effectively unlimited.
  1304. */
  1305. static int process_responses(struct adapter *adapter, int budget)
  1306. {
  1307. struct sge *sge = adapter->sge;
  1308. struct respQ *q = &sge->respQ;
  1309. struct respQ_e *e = &q->entries[q->cidx];
  1310. int done = 0;
  1311. unsigned int flags = 0;
  1312. unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
  1313. while (done < budget && e->GenerationBit == q->genbit) {
  1314. flags |= e->Qsleeping;
  1315. cmdq_processed[0] += e->Cmdq0CreditReturn;
  1316. cmdq_processed[1] += e->Cmdq1CreditReturn;
  1317. /* We batch updates to the TX side to avoid cacheline
  1318. * ping-pong of TX state information on MP where the sender
  1319. * might run on a different CPU than this function...
  1320. */
  1321. if (unlikely((flags & F_CMDQ0_ENABLE) || cmdq_processed[0] > 64)) {
  1322. flags = update_tx_info(adapter, flags, cmdq_processed[0]);
  1323. cmdq_processed[0] = 0;
  1324. }
  1325. if (unlikely(cmdq_processed[1] > 16)) {
  1326. sge->cmdQ[1].processed += cmdq_processed[1];
  1327. cmdq_processed[1] = 0;
  1328. }
  1329. if (likely(e->DataValid)) {
  1330. struct freelQ *fl = &sge->freelQ[e->FreelistQid];
  1331. BUG_ON(!e->Sop || !e->Eop);
  1332. if (unlikely(e->Offload))
  1333. unexpected_offload(adapter, fl);
  1334. else
  1335. sge_rx(sge, fl, e->BufferLength);
  1336. ++done;
  1337. /*
  1338. * Note: this depends on each packet consuming a
  1339. * single free-list buffer; cf. the BUG above.
  1340. */
  1341. if (++fl->cidx == fl->size)
  1342. fl->cidx = 0;
  1343. prefetch(fl->centries[fl->cidx].skb);
  1344. if (unlikely(--fl->credits <
  1345. fl->size - SGE_FREEL_REFILL_THRESH))
  1346. refill_free_list(sge, fl);
  1347. } else
  1348. sge->stats.pure_rsps++;
  1349. e++;
  1350. if (unlikely(++q->cidx == q->size)) {
  1351. q->cidx = 0;
  1352. q->genbit ^= 1;
  1353. e = q->entries;
  1354. }
  1355. prefetch(e);
  1356. if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
  1357. writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
  1358. q->credits = 0;
  1359. }
  1360. }
  1361. flags = update_tx_info(adapter, flags, cmdq_processed[0]);
  1362. sge->cmdQ[1].processed += cmdq_processed[1];
  1363. return done;
  1364. }
  1365. static inline int responses_pending(const struct adapter *adapter)
  1366. {
  1367. const struct respQ *Q = &adapter->sge->respQ;
  1368. const struct respQ_e *e = &Q->entries[Q->cidx];
  1369. return (e->GenerationBit == Q->genbit);
  1370. }
  1371. #ifdef CONFIG_CHELSIO_T1_NAPI
  1372. /*
  1373. * A simpler version of process_responses() that handles only pure (i.e.,
  1374. * non data-carrying) responses. Such respones are too light-weight to justify
  1375. * calling a softirq when using NAPI, so we handle them specially in hard
  1376. * interrupt context. The function is called with a pointer to a response,
  1377. * which the caller must ensure is a valid pure response. Returns 1 if it
  1378. * encounters a valid data-carrying response, 0 otherwise.
  1379. */
  1380. static int process_pure_responses(struct adapter *adapter)
  1381. {
  1382. struct sge *sge = adapter->sge;
  1383. struct respQ *q = &sge->respQ;
  1384. struct respQ_e *e = &q->entries[q->cidx];
  1385. const struct freelQ *fl = &sge->freelQ[e->FreelistQid];
  1386. unsigned int flags = 0;
  1387. unsigned int cmdq_processed[SGE_CMDQ_N] = {0, 0};
  1388. prefetch(fl->centries[fl->cidx].skb);
  1389. if (e->DataValid)
  1390. return 1;
  1391. do {
  1392. flags |= e->Qsleeping;
  1393. cmdq_processed[0] += e->Cmdq0CreditReturn;
  1394. cmdq_processed[1] += e->Cmdq1CreditReturn;
  1395. e++;
  1396. if (unlikely(++q->cidx == q->size)) {
  1397. q->cidx = 0;
  1398. q->genbit ^= 1;
  1399. e = q->entries;
  1400. }
  1401. prefetch(e);
  1402. if (++q->credits > SGE_RESPQ_REPLENISH_THRES) {
  1403. writel(q->credits, adapter->regs + A_SG_RSPQUEUECREDIT);
  1404. q->credits = 0;
  1405. }
  1406. sge->stats.pure_rsps++;
  1407. } while (e->GenerationBit == q->genbit && !e->DataValid);
  1408. flags = update_tx_info(adapter, flags, cmdq_processed[0]);
  1409. sge->cmdQ[1].processed += cmdq_processed[1];
  1410. return e->GenerationBit == q->genbit;
  1411. }
  1412. /*
  1413. * Handler for new data events when using NAPI. This does not need any locking
  1414. * or protection from interrupts as data interrupts are off at this point and
  1415. * other adapter interrupts do not interfere.
  1416. */
  1417. int t1_poll(struct net_device *dev, int *budget)
  1418. {
  1419. struct adapter *adapter = dev->priv;
  1420. int work_done;
  1421. work_done = process_responses(adapter, min(*budget, dev->quota));
  1422. *budget -= work_done;
  1423. dev->quota -= work_done;
  1424. if (unlikely(responses_pending(adapter)))
  1425. return 1;
  1426. netif_rx_complete(dev);
  1427. writel(adapter->sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
  1428. return 0;
  1429. }
  1430. /*
  1431. * NAPI version of the main interrupt handler.
  1432. */
  1433. irqreturn_t t1_interrupt(int irq, void *data)
  1434. {
  1435. struct adapter *adapter = data;
  1436. struct sge *sge = adapter->sge;
  1437. int handled;
  1438. if (likely(responses_pending(adapter))) {
  1439. struct net_device *dev = sge->netdev;
  1440. writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
  1441. if (__netif_rx_schedule_prep(dev)) {
  1442. if (process_pure_responses(adapter))
  1443. __netif_rx_schedule(dev);
  1444. else {
  1445. /* no data, no NAPI needed */
  1446. writel(sge->respQ.cidx, adapter->regs + A_SG_SLEEPING);
  1447. netif_poll_enable(dev); /* undo schedule_prep */
  1448. }
  1449. }
  1450. return IRQ_HANDLED;
  1451. }
  1452. spin_lock(&adapter->async_lock);
  1453. handled = t1_slow_intr_handler(adapter);
  1454. spin_unlock(&adapter->async_lock);
  1455. if (!handled)
  1456. sge->stats.unhandled_irqs++;
  1457. return IRQ_RETVAL(handled != 0);
  1458. }
  1459. #else
  1460. /*
  1461. * Main interrupt handler, optimized assuming that we took a 'DATA'
  1462. * interrupt.
  1463. *
  1464. * 1. Clear the interrupt
  1465. * 2. Loop while we find valid descriptors and process them; accumulate
  1466. * information that can be processed after the loop
  1467. * 3. Tell the SGE at which index we stopped processing descriptors
  1468. * 4. Bookkeeping; free TX buffers, ring doorbell if there are any
  1469. * outstanding TX buffers waiting, replenish RX buffers, potentially
  1470. * reenable upper layers if they were turned off due to lack of TX
  1471. * resources which are available again.
  1472. * 5. If we took an interrupt, but no valid respQ descriptors was found we
  1473. * let the slow_intr_handler run and do error handling.
  1474. */
  1475. irqreturn_t t1_interrupt(int irq, void *cookie)
  1476. {
  1477. int work_done;
  1478. struct adapter *adapter = cookie;
  1479. struct respQ *Q = &adapter->sge->respQ;
  1480. spin_lock(&adapter->async_lock);
  1481. writel(F_PL_INTR_SGE_DATA, adapter->regs + A_PL_CAUSE);
  1482. if (likely(responses_pending(adapter)))
  1483. work_done = process_responses(adapter, -1);
  1484. else
  1485. work_done = t1_slow_intr_handler(adapter);
  1486. /*
  1487. * The unconditional clearing of the PL_CAUSE above may have raced
  1488. * with DMA completion and the corresponding generation of a response
  1489. * to cause us to miss the resulting data interrupt. The next write
  1490. * is also unconditional to recover the missed interrupt and render
  1491. * this race harmless.
  1492. */
  1493. writel(Q->cidx, adapter->regs + A_SG_SLEEPING);
  1494. if (!work_done)
  1495. adapter->sge->stats.unhandled_irqs++;
  1496. spin_unlock(&adapter->async_lock);
  1497. return IRQ_RETVAL(work_done != 0);
  1498. }
  1499. #endif
  1500. /*
  1501. * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
  1502. *
  1503. * The code figures out how many entries the sk_buff will require in the
  1504. * cmdQ and updates the cmdQ data structure with the state once the enqueue
  1505. * has complete. Then, it doesn't access the global structure anymore, but
  1506. * uses the corresponding fields on the stack. In conjuction with a spinlock
  1507. * around that code, we can make the function reentrant without holding the
  1508. * lock when we actually enqueue (which might be expensive, especially on
  1509. * architectures with IO MMUs).
  1510. *
  1511. * This runs with softirqs disabled.
  1512. */
  1513. static int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
  1514. unsigned int qid, struct net_device *dev)
  1515. {
  1516. struct sge *sge = adapter->sge;
  1517. struct cmdQ *q = &sge->cmdQ[qid];
  1518. unsigned int credits, pidx, genbit, count, use_sched_skb = 0;
  1519. if (!spin_trylock(&q->lock))
  1520. return NETDEV_TX_LOCKED;
  1521. reclaim_completed_tx(sge, q);
  1522. pidx = q->pidx;
  1523. credits = q->size - q->in_use;
  1524. count = 1 + skb_shinfo(skb)->nr_frags;
  1525. count += compute_large_page_tx_descs(skb);
  1526. /* Ethernet packet */
  1527. if (unlikely(credits < count)) {
  1528. if (!netif_queue_stopped(dev)) {
  1529. netif_stop_queue(dev);
  1530. set_bit(dev->if_port, &sge->stopped_tx_queues);
  1531. sge->stats.cmdQ_full[2]++;
  1532. CH_ERR("%s: Tx ring full while queue awake!\n",
  1533. adapter->name);
  1534. }
  1535. spin_unlock(&q->lock);
  1536. return NETDEV_TX_BUSY;
  1537. }
  1538. if (unlikely(credits - count < q->stop_thres)) {
  1539. netif_stop_queue(dev);
  1540. set_bit(dev->if_port, &sge->stopped_tx_queues);
  1541. sge->stats.cmdQ_full[2]++;
  1542. }
  1543. /* T204 cmdQ0 skbs that are destined for a certain port have to go
  1544. * through the scheduler.
  1545. */
  1546. if (sge->tx_sched && !qid && skb->dev) {
  1547. use_sched:
  1548. use_sched_skb = 1;
  1549. /* Note that the scheduler might return a different skb than
  1550. * the one passed in.
  1551. */
  1552. skb = sched_skb(sge, skb, credits);
  1553. if (!skb) {
  1554. spin_unlock(&q->lock);
  1555. return NETDEV_TX_OK;
  1556. }
  1557. pidx = q->pidx;
  1558. count = 1 + skb_shinfo(skb)->nr_frags;
  1559. count += compute_large_page_tx_descs(skb);
  1560. }
  1561. q->in_use += count;
  1562. genbit = q->genbit;
  1563. pidx = q->pidx;
  1564. q->pidx += count;
  1565. if (q->pidx >= q->size) {
  1566. q->pidx -= q->size;
  1567. q->genbit ^= 1;
  1568. }
  1569. spin_unlock(&q->lock);
  1570. write_tx_descs(adapter, skb, pidx, genbit, q);
  1571. /*
  1572. * We always ring the doorbell for cmdQ1. For cmdQ0, we only ring
  1573. * the doorbell if the Q is asleep. There is a natural race, where
  1574. * the hardware is going to sleep just after we checked, however,
  1575. * then the interrupt handler will detect the outstanding TX packet
  1576. * and ring the doorbell for us.
  1577. */
  1578. if (qid)
  1579. doorbell_pio(adapter, F_CMDQ1_ENABLE);
  1580. else {
  1581. clear_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  1582. if (test_and_set_bit(CMDQ_STAT_RUNNING, &q->status) == 0) {
  1583. set_bit(CMDQ_STAT_LAST_PKT_DB, &q->status);
  1584. writel(F_CMDQ0_ENABLE, adapter->regs + A_SG_DOORBELL);
  1585. }
  1586. }
  1587. if (use_sched_skb) {
  1588. if (spin_trylock(&q->lock)) {
  1589. credits = q->size - q->in_use;
  1590. skb = NULL;
  1591. goto use_sched;
  1592. }
  1593. }
  1594. return NETDEV_TX_OK;
  1595. }
  1596. #define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
  1597. /*
  1598. * eth_hdr_len - return the length of an Ethernet header
  1599. * @data: pointer to the start of the Ethernet header
  1600. *
  1601. * Returns the length of an Ethernet header, including optional VLAN tag.
  1602. */
  1603. static inline int eth_hdr_len(const void *data)
  1604. {
  1605. const struct ethhdr *e = data;
  1606. return e->h_proto == htons(ETH_P_8021Q) ? VLAN_ETH_HLEN : ETH_HLEN;
  1607. }
  1608. /*
  1609. * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
  1610. */
  1611. int t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1612. {
  1613. struct adapter *adapter = dev->priv;
  1614. struct sge *sge = adapter->sge;
  1615. struct sge_port_stats *st = per_cpu_ptr(sge->port_stats[dev->if_port], smp_processor_id());
  1616. struct cpl_tx_pkt *cpl;
  1617. struct sk_buff *orig_skb = skb;
  1618. int ret;
  1619. if (skb->protocol == htons(ETH_P_CPL5))
  1620. goto send;
  1621. if (skb_shinfo(skb)->gso_size) {
  1622. int eth_type;
  1623. struct cpl_tx_pkt_lso *hdr;
  1624. ++st->tx_tso;
  1625. eth_type = skb_network_offset(skb) == ETH_HLEN ?
  1626. CPL_ETH_II : CPL_ETH_II_VLAN;
  1627. hdr = (struct cpl_tx_pkt_lso *)skb_push(skb, sizeof(*hdr));
  1628. hdr->opcode = CPL_TX_PKT_LSO;
  1629. hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
  1630. hdr->ip_hdr_words = ip_hdr(skb)->ihl;
  1631. hdr->tcp_hdr_words = tcp_hdr(skb)->doff;
  1632. hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
  1633. skb_shinfo(skb)->gso_size));
  1634. hdr->len = htonl(skb->len - sizeof(*hdr));
  1635. cpl = (struct cpl_tx_pkt *)hdr;
  1636. } else {
  1637. /*
  1638. * Packets shorter than ETH_HLEN can break the MAC, drop them
  1639. * early. Also, we may get oversized packets because some
  1640. * parts of the kernel don't handle our unusual hard_header_len
  1641. * right, drop those too.
  1642. */
  1643. if (unlikely(skb->len < ETH_HLEN ||
  1644. skb->len > dev->mtu + eth_hdr_len(skb->data))) {
  1645. pr_debug("%s: packet size %d hdr %d mtu%d\n", dev->name,
  1646. skb->len, eth_hdr_len(skb->data), dev->mtu);
  1647. dev_kfree_skb_any(skb);
  1648. return NETDEV_TX_OK;
  1649. }
  1650. /*
  1651. * We are using a non-standard hard_header_len and some kernel
  1652. * components, such as pktgen, do not handle it right.
  1653. * Complain when this happens but try to fix things up.
  1654. */
  1655. if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
  1656. pr_debug("%s: headroom %d header_len %d\n", dev->name,
  1657. skb_headroom(skb), dev->hard_header_len);
  1658. if (net_ratelimit())
  1659. printk(KERN_ERR "%s: inadequate headroom in "
  1660. "Tx packet\n", dev->name);
  1661. skb = skb_realloc_headroom(skb, sizeof(*cpl));
  1662. dev_kfree_skb_any(orig_skb);
  1663. if (!skb)
  1664. return NETDEV_TX_OK;
  1665. }
  1666. if (!(adapter->flags & UDP_CSUM_CAPABLE) &&
  1667. skb->ip_summed == CHECKSUM_PARTIAL &&
  1668. ip_hdr(skb)->protocol == IPPROTO_UDP) {
  1669. if (unlikely(skb_checksum_help(skb))) {
  1670. pr_debug("%s: unable to do udp checksum\n", dev->name);
  1671. dev_kfree_skb_any(skb);
  1672. return NETDEV_TX_OK;
  1673. }
  1674. }
  1675. /* Hmmm, assuming to catch the gratious arp... and we'll use
  1676. * it to flush out stuck espi packets...
  1677. */
  1678. if ((unlikely(!adapter->sge->espibug_skb[dev->if_port]))) {
  1679. if (skb->protocol == htons(ETH_P_ARP) &&
  1680. arp_hdr(skb)->ar_op == htons(ARPOP_REQUEST)) {
  1681. adapter->sge->espibug_skb[dev->if_port] = skb;
  1682. /* We want to re-use this skb later. We
  1683. * simply bump the reference count and it
  1684. * will not be freed...
  1685. */
  1686. skb = skb_get(skb);
  1687. }
  1688. }
  1689. cpl = (struct cpl_tx_pkt *)__skb_push(skb, sizeof(*cpl));
  1690. cpl->opcode = CPL_TX_PKT;
  1691. cpl->ip_csum_dis = 1; /* SW calculates IP csum */
  1692. cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_PARTIAL ? 0 : 1;
  1693. /* the length field isn't used so don't bother setting it */
  1694. st->tx_cso += (skb->ip_summed == CHECKSUM_PARTIAL);
  1695. }
  1696. cpl->iff = dev->if_port;
  1697. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  1698. if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
  1699. cpl->vlan_valid = 1;
  1700. cpl->vlan = htons(vlan_tx_tag_get(skb));
  1701. st->vlan_insert++;
  1702. } else
  1703. #endif
  1704. cpl->vlan_valid = 0;
  1705. send:
  1706. st->tx_packets++;
  1707. dev->trans_start = jiffies;
  1708. ret = t1_sge_tx(skb, adapter, 0, dev);
  1709. /* If transmit busy, and we reallocated skb's due to headroom limit,
  1710. * then silently discard to avoid leak.
  1711. */
  1712. if (unlikely(ret != NETDEV_TX_OK && skb != orig_skb)) {
  1713. dev_kfree_skb_any(skb);
  1714. ret = NETDEV_TX_OK;
  1715. }
  1716. return ret;
  1717. }
  1718. /*
  1719. * Callback for the Tx buffer reclaim timer. Runs with softirqs disabled.
  1720. */
  1721. static void sge_tx_reclaim_cb(unsigned long data)
  1722. {
  1723. int i;
  1724. struct sge *sge = (struct sge *)data;
  1725. for (i = 0; i < SGE_CMDQ_N; ++i) {
  1726. struct cmdQ *q = &sge->cmdQ[i];
  1727. if (!spin_trylock(&q->lock))
  1728. continue;
  1729. reclaim_completed_tx(sge, q);
  1730. if (i == 0 && q->in_use) { /* flush pending credits */
  1731. writel(F_CMDQ0_ENABLE, sge->adapter->regs + A_SG_DOORBELL);
  1732. }
  1733. spin_unlock(&q->lock);
  1734. }
  1735. mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
  1736. }
  1737. /*
  1738. * Propagate changes of the SGE coalescing parameters to the HW.
  1739. */
  1740. int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
  1741. {
  1742. sge->fixed_intrtimer = p->rx_coalesce_usecs *
  1743. core_ticks_per_usec(sge->adapter);
  1744. writel(sge->fixed_intrtimer, sge->adapter->regs + A_SG_INTRTIMER);
  1745. return 0;
  1746. }
  1747. /*
  1748. * Allocates both RX and TX resources and configures the SGE. However,
  1749. * the hardware is not enabled yet.
  1750. */
  1751. int t1_sge_configure(struct sge *sge, struct sge_params *p)
  1752. {
  1753. if (alloc_rx_resources(sge, p))
  1754. return -ENOMEM;
  1755. if (alloc_tx_resources(sge, p)) {
  1756. free_rx_resources(sge);
  1757. return -ENOMEM;
  1758. }
  1759. configure_sge(sge, p);
  1760. /*
  1761. * Now that we have sized the free lists calculate the payload
  1762. * capacity of the large buffers. Other parts of the driver use
  1763. * this to set the max offload coalescing size so that RX packets
  1764. * do not overflow our large buffers.
  1765. */
  1766. p->large_buf_capacity = jumbo_payload_capacity(sge);
  1767. return 0;
  1768. }
  1769. /*
  1770. * Disables the DMA engine.
  1771. */
  1772. void t1_sge_stop(struct sge *sge)
  1773. {
  1774. int i;
  1775. writel(0, sge->adapter->regs + A_SG_CONTROL);
  1776. readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
  1777. if (is_T2(sge->adapter))
  1778. del_timer_sync(&sge->espibug_timer);
  1779. del_timer_sync(&sge->tx_reclaim_timer);
  1780. if (sge->tx_sched)
  1781. tx_sched_stop(sge);
  1782. for (i = 0; i < MAX_NPORTS; i++)
  1783. if (sge->espibug_skb[i])
  1784. kfree_skb(sge->espibug_skb[i]);
  1785. }
  1786. /*
  1787. * Enables the DMA engine.
  1788. */
  1789. void t1_sge_start(struct sge *sge)
  1790. {
  1791. refill_free_list(sge, &sge->freelQ[0]);
  1792. refill_free_list(sge, &sge->freelQ[1]);
  1793. writel(sge->sge_control, sge->adapter->regs + A_SG_CONTROL);
  1794. doorbell_pio(sge->adapter, F_FL0_ENABLE | F_FL1_ENABLE);
  1795. readl(sge->adapter->regs + A_SG_CONTROL); /* flush */
  1796. mod_timer(&sge->tx_reclaim_timer, jiffies + TX_RECLAIM_PERIOD);
  1797. if (is_T2(sge->adapter))
  1798. mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
  1799. }
  1800. /*
  1801. * Callback for the T2 ESPI 'stuck packet feature' workaorund
  1802. */
  1803. static void espibug_workaround_t204(unsigned long data)
  1804. {
  1805. struct adapter *adapter = (struct adapter *)data;
  1806. struct sge *sge = adapter->sge;
  1807. unsigned int nports = adapter->params.nports;
  1808. u32 seop[MAX_NPORTS];
  1809. if (adapter->open_device_map & PORT_MASK) {
  1810. int i;
  1811. if (t1_espi_get_mon_t204(adapter, &(seop[0]), 0) < 0)
  1812. return;
  1813. for (i = 0; i < nports; i++) {
  1814. struct sk_buff *skb = sge->espibug_skb[i];
  1815. if (!netif_running(adapter->port[i].dev) ||
  1816. netif_queue_stopped(adapter->port[i].dev) ||
  1817. !seop[i] || ((seop[i] & 0xfff) != 0) || !skb)
  1818. continue;
  1819. if (!skb->cb[0]) {
  1820. u8 ch_mac_addr[ETH_ALEN] = {
  1821. 0x0, 0x7, 0x43, 0x0, 0x0, 0x0
  1822. };
  1823. skb_copy_to_linear_data_offset(skb,
  1824. sizeof(struct cpl_tx_pkt),
  1825. ch_mac_addr,
  1826. ETH_ALEN);
  1827. skb_copy_to_linear_data_offset(skb,
  1828. skb->len - 10,
  1829. ch_mac_addr,
  1830. ETH_ALEN);
  1831. skb->cb[0] = 0xff;
  1832. }
  1833. /* bump the reference count to avoid freeing of
  1834. * the skb once the DMA has completed.
  1835. */
  1836. skb = skb_get(skb);
  1837. t1_sge_tx(skb, adapter, 0, adapter->port[i].dev);
  1838. }
  1839. }
  1840. mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
  1841. }
  1842. static void espibug_workaround(unsigned long data)
  1843. {
  1844. struct adapter *adapter = (struct adapter *)data;
  1845. struct sge *sge = adapter->sge;
  1846. if (netif_running(adapter->port[0].dev)) {
  1847. struct sk_buff *skb = sge->espibug_skb[0];
  1848. u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
  1849. if ((seop & 0xfff0fff) == 0xfff && skb) {
  1850. if (!skb->cb[0]) {
  1851. u8 ch_mac_addr[ETH_ALEN] =
  1852. {0x0, 0x7, 0x43, 0x0, 0x0, 0x0};
  1853. skb_copy_to_linear_data_offset(skb,
  1854. sizeof(struct cpl_tx_pkt),
  1855. ch_mac_addr,
  1856. ETH_ALEN);
  1857. skb_copy_to_linear_data_offset(skb,
  1858. skb->len - 10,
  1859. ch_mac_addr,
  1860. ETH_ALEN);
  1861. skb->cb[0] = 0xff;
  1862. }
  1863. /* bump the reference count to avoid freeing of the
  1864. * skb once the DMA has completed.
  1865. */
  1866. skb = skb_get(skb);
  1867. t1_sge_tx(skb, adapter, 0, adapter->port[0].dev);
  1868. }
  1869. }
  1870. mod_timer(&sge->espibug_timer, jiffies + sge->espibug_timeout);
  1871. }
  1872. /*
  1873. * Creates a t1_sge structure and returns suggested resource parameters.
  1874. */
  1875. struct sge * __devinit t1_sge_create(struct adapter *adapter,
  1876. struct sge_params *p)
  1877. {
  1878. struct sge *sge = kzalloc(sizeof(*sge), GFP_KERNEL);
  1879. int i;
  1880. if (!sge)
  1881. return NULL;
  1882. sge->adapter = adapter;
  1883. sge->netdev = adapter->port[0].dev;
  1884. sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : 2;
  1885. sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
  1886. for_each_port(adapter, i) {
  1887. sge->port_stats[i] = alloc_percpu(struct sge_port_stats);
  1888. if (!sge->port_stats[i])
  1889. goto nomem_port;
  1890. }
  1891. init_timer(&sge->tx_reclaim_timer);
  1892. sge->tx_reclaim_timer.data = (unsigned long)sge;
  1893. sge->tx_reclaim_timer.function = sge_tx_reclaim_cb;
  1894. if (is_T2(sge->adapter)) {
  1895. init_timer(&sge->espibug_timer);
  1896. if (adapter->params.nports > 1) {
  1897. tx_sched_init(sge);
  1898. sge->espibug_timer.function = espibug_workaround_t204;
  1899. } else
  1900. sge->espibug_timer.function = espibug_workaround;
  1901. sge->espibug_timer.data = (unsigned long)sge->adapter;
  1902. sge->espibug_timeout = 1;
  1903. /* for T204, every 10ms */
  1904. if (adapter->params.nports > 1)
  1905. sge->espibug_timeout = HZ/100;
  1906. }
  1907. p->cmdQ_size[0] = SGE_CMDQ0_E_N;
  1908. p->cmdQ_size[1] = SGE_CMDQ1_E_N;
  1909. p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
  1910. p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
  1911. if (sge->tx_sched) {
  1912. if (board_info(sge->adapter)->board == CHBT_BOARD_CHT204)
  1913. p->rx_coalesce_usecs = 15;
  1914. else
  1915. p->rx_coalesce_usecs = 50;
  1916. } else
  1917. p->rx_coalesce_usecs = 50;
  1918. p->coalesce_enable = 0;
  1919. p->sample_interval_usecs = 0;
  1920. return sge;
  1921. nomem_port:
  1922. while (i >= 0) {
  1923. free_percpu(sge->port_stats[i]);
  1924. --i;
  1925. }
  1926. kfree(sge);
  1927. return NULL;
  1928. }