sge.c 61 KB

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