sge.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  1. /*****************************************************************************
  2. * *
  3. * File: sge.c *
  4. * $Revision: 1.13 $ *
  5. * $Date: 2005/03/23 07:41:27 $ *
  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/config.h>
  41. #include <linux/types.h>
  42. #include <linux/errno.h>
  43. #include <linux/pci.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/ip.h>
  51. #include <linux/in.h>
  52. #include <linux/if_arp.h>
  53. #include "cpl5_cmd.h"
  54. #include "sge.h"
  55. #include "regs.h"
  56. #include "espi.h"
  57. #include <linux/tcp.h>
  58. #define SGE_CMDQ_N 2
  59. #define SGE_FREELQ_N 2
  60. #define SGE_CMDQ0_E_N 512
  61. #define SGE_CMDQ1_E_N 128
  62. #define SGE_FREEL_SIZE 4096
  63. #define SGE_JUMBO_FREEL_SIZE 512
  64. #define SGE_FREEL_REFILL_THRESH 16
  65. #define SGE_RESPQ_E_N 1024
  66. #define SGE_INTR_BUCKETSIZE 100
  67. #define SGE_INTR_LATBUCKETS 5
  68. #define SGE_INTR_MAXBUCKETS 11
  69. #define SGE_INTRTIMER0 1
  70. #define SGE_INTRTIMER1 50
  71. #define SGE_INTRTIMER_NRES 10000
  72. #define SGE_RX_COPY_THRESHOLD 256
  73. #define SGE_RX_SM_BUF_SIZE 1536
  74. #define SGE_RESPQ_REPLENISH_THRES ((3 * SGE_RESPQ_E_N) / 4)
  75. #define SGE_RX_OFFSET 2
  76. #ifndef NET_IP_ALIGN
  77. # define NET_IP_ALIGN SGE_RX_OFFSET
  78. #endif
  79. /*
  80. * Memory Mapped HW Command, Freelist and Response Queue Descriptors
  81. */
  82. #if defined(__BIG_ENDIAN_BITFIELD)
  83. struct cmdQ_e {
  84. u32 AddrLow;
  85. u32 GenerationBit : 1;
  86. u32 BufferLength : 31;
  87. u32 RespQueueSelector : 4;
  88. u32 ResponseTokens : 12;
  89. u32 CmdId : 8;
  90. u32 Reserved : 3;
  91. u32 TokenValid : 1;
  92. u32 Eop : 1;
  93. u32 Sop : 1;
  94. u32 DataValid : 1;
  95. u32 GenerationBit2 : 1;
  96. u32 AddrHigh;
  97. };
  98. struct freelQ_e {
  99. u32 AddrLow;
  100. u32 GenerationBit : 1;
  101. u32 BufferLength : 31;
  102. u32 Reserved : 31;
  103. u32 GenerationBit2 : 1;
  104. u32 AddrHigh;
  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 BufferLength : 31;
  124. u32 GenerationBit : 1;
  125. u32 AddrLow;
  126. u32 AddrHigh;
  127. u32 GenerationBit2 : 1;
  128. u32 DataValid : 1;
  129. u32 Sop : 1;
  130. u32 Eop : 1;
  131. u32 TokenValid : 1;
  132. u32 Reserved : 3;
  133. u32 CmdId : 8;
  134. u32 ResponseTokens : 12;
  135. u32 RespQueueSelector : 4;
  136. };
  137. struct freelQ_e {
  138. u32 BufferLength : 31;
  139. u32 GenerationBit : 1;
  140. u32 AddrLow;
  141. u32 AddrHigh;
  142. u32 GenerationBit2 : 1;
  143. u32 Reserved : 31;
  144. };
  145. struct respQ_e {
  146. u32 BufferLength;
  147. u32 GenerationBit : 1;
  148. u32 Sop : 1;
  149. u32 Eop : 1;
  150. u32 Offload : 1;
  151. u32 DataValid : 1;
  152. u32 CreditValid : 1;
  153. u32 FreelistQid : 2;
  154. u32 Cmdq0DmaComplete : 5;
  155. u32 Cmdq0CreditReturn : 5;
  156. u32 Cmdq1DmaComplete : 5;
  157. u32 Cmdq1CreditReturn : 5;
  158. u32 Qsleeping : 4;
  159. } ;
  160. #endif
  161. /*
  162. * SW Context Command and Freelist Queue Descriptors
  163. */
  164. struct cmdQ_ce {
  165. struct sk_buff *skb;
  166. DECLARE_PCI_UNMAP_ADDR(dma_addr);
  167. DECLARE_PCI_UNMAP_LEN(dma_len);
  168. unsigned int single;
  169. };
  170. struct freelQ_ce {
  171. struct sk_buff *skb;
  172. DECLARE_PCI_UNMAP_ADDR(dma_addr);
  173. DECLARE_PCI_UNMAP_LEN(dma_len);
  174. };
  175. /*
  176. * SW Command, Freelist and Response Queue
  177. */
  178. struct cmdQ {
  179. atomic_t asleep; /* HW DMA Fetch status */
  180. atomic_t credits; /* # available descriptors for TX */
  181. atomic_t pio_pidx; /* Variable updated on Doorbell */
  182. u16 entries_n; /* # descriptors for TX */
  183. u16 pidx; /* producer index (SW) */
  184. u16 cidx; /* consumer index (HW) */
  185. u8 genbit; /* current generation (=valid) bit */
  186. struct cmdQ_e *entries; /* HW command descriptor Q */
  187. struct cmdQ_ce *centries; /* SW command context descriptor Q */
  188. spinlock_t Qlock; /* Lock to protect cmdQ enqueuing */
  189. dma_addr_t dma_addr; /* DMA addr HW command descriptor Q */
  190. };
  191. struct freelQ {
  192. unsigned int credits; /* # of available RX buffers */
  193. unsigned int entries_n; /* free list capacity */
  194. u16 pidx; /* producer index (SW) */
  195. u16 cidx; /* consumer index (HW) */
  196. u16 rx_buffer_size; /* Buffer size on this free list */
  197. u16 dma_offset; /* DMA offset to align IP headers */
  198. u8 genbit; /* current generation (=valid) bit */
  199. struct freelQ_e *entries; /* HW freelist descriptor Q */
  200. struct freelQ_ce *centries; /* SW freelist conext descriptor Q */
  201. dma_addr_t dma_addr; /* DMA addr HW freelist descriptor Q */
  202. };
  203. struct respQ {
  204. u16 credits; /* # of available respQ descriptors */
  205. u16 credits_pend; /* # of not yet returned descriptors */
  206. u16 entries_n; /* # of response Q descriptors */
  207. u16 pidx; /* producer index (HW) */
  208. u16 cidx; /* consumer index (SW) */
  209. u8 genbit; /* current generation(=valid) bit */
  210. struct respQ_e *entries; /* HW response descriptor Q */
  211. dma_addr_t dma_addr; /* DMA addr HW response descriptor Q */
  212. };
  213. /*
  214. * Main SGE data structure
  215. *
  216. * Interrupts are handled by a single CPU and it is likely that on a MP system
  217. * the application is migrated to another CPU. In that scenario, we try to
  218. * seperate the RX(in irq context) and TX state in order to decrease memory
  219. * contention.
  220. */
  221. struct sge {
  222. struct adapter *adapter; /* adapter backpointer */
  223. struct freelQ freelQ[SGE_FREELQ_N]; /* freelist Q(s) */
  224. struct respQ respQ; /* response Q instatiation */
  225. unsigned int rx_pkt_pad; /* RX padding for L2 packets */
  226. unsigned int jumbo_fl; /* jumbo freelist Q index */
  227. u32 intrtimer[SGE_INTR_MAXBUCKETS]; /* ! */
  228. u32 currIndex; /* current index into intrtimer[] */
  229. u32 intrtimer_nres; /* no resource interrupt timer value */
  230. u32 sge_control; /* shadow content of sge control reg */
  231. struct sge_intr_counts intr_cnt;
  232. struct timer_list ptimer;
  233. struct sk_buff *pskb;
  234. u32 ptimeout;
  235. struct cmdQ cmdQ[SGE_CMDQ_N] ____cacheline_aligned; /* command Q(s)*/
  236. };
  237. static unsigned int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
  238. unsigned int qid);
  239. /*
  240. * PIO to indicate that memory mapped Q contains valid descriptor(s).
  241. */
  242. static inline void doorbell_pio(struct sge *sge, u32 val)
  243. {
  244. wmb();
  245. t1_write_reg_4(sge->adapter, A_SG_DOORBELL, val);
  246. }
  247. /*
  248. * Disables the DMA engine.
  249. */
  250. void t1_sge_stop(struct sge *sge)
  251. {
  252. t1_write_reg_4(sge->adapter, A_SG_CONTROL, 0);
  253. t1_read_reg_4(sge->adapter, A_SG_CONTROL); /* flush write */
  254. if (is_T2(sge->adapter))
  255. del_timer_sync(&sge->ptimer);
  256. }
  257. static u8 ch_mac_addr[ETH_ALEN] = {0x0, 0x7, 0x43, 0x0, 0x0, 0x0};
  258. static void t1_espi_workaround(void *data)
  259. {
  260. struct adapter *adapter = (struct adapter *)data;
  261. struct sge *sge = adapter->sge;
  262. if (netif_running(adapter->port[0].dev) &&
  263. atomic_read(&sge->cmdQ[0].asleep)) {
  264. u32 seop = t1_espi_get_mon(adapter, 0x930, 0);
  265. if ((seop & 0xfff0fff) == 0xfff && sge->pskb) {
  266. struct sk_buff *skb = sge->pskb;
  267. if (!skb->cb[0]) {
  268. memcpy(skb->data+sizeof(struct cpl_tx_pkt), ch_mac_addr, ETH_ALEN);
  269. memcpy(skb->data+skb->len-10, ch_mac_addr, ETH_ALEN);
  270. skb->cb[0] = 0xff;
  271. }
  272. t1_sge_tx(skb, adapter,0);
  273. }
  274. }
  275. mod_timer(&adapter->sge->ptimer, jiffies + sge->ptimeout);
  276. }
  277. /*
  278. * Enables the DMA engine.
  279. */
  280. void t1_sge_start(struct sge *sge)
  281. {
  282. t1_write_reg_4(sge->adapter, A_SG_CONTROL, sge->sge_control);
  283. t1_read_reg_4(sge->adapter, A_SG_CONTROL); /* flush write */
  284. if (is_T2(sge->adapter)) {
  285. init_timer(&sge->ptimer);
  286. sge->ptimer.function = (void *)&t1_espi_workaround;
  287. sge->ptimer.data = (unsigned long)sge->adapter;
  288. sge->ptimer.expires = jiffies + sge->ptimeout;
  289. add_timer(&sge->ptimer);
  290. }
  291. }
  292. /*
  293. * Creates a t1_sge structure and returns suggested resource parameters.
  294. */
  295. struct sge * __devinit t1_sge_create(struct adapter *adapter,
  296. struct sge_params *p)
  297. {
  298. struct sge *sge = kmalloc(sizeof(*sge), GFP_KERNEL);
  299. if (!sge)
  300. return NULL;
  301. memset(sge, 0, sizeof(*sge));
  302. if (is_T2(adapter))
  303. sge->ptimeout = 1; /* finest allowed */
  304. sge->adapter = adapter;
  305. sge->rx_pkt_pad = t1_is_T1B(adapter) ? 0 : SGE_RX_OFFSET;
  306. sge->jumbo_fl = t1_is_T1B(adapter) ? 1 : 0;
  307. p->cmdQ_size[0] = SGE_CMDQ0_E_N;
  308. p->cmdQ_size[1] = SGE_CMDQ1_E_N;
  309. p->freelQ_size[!sge->jumbo_fl] = SGE_FREEL_SIZE;
  310. p->freelQ_size[sge->jumbo_fl] = SGE_JUMBO_FREEL_SIZE;
  311. p->rx_coalesce_usecs = SGE_INTRTIMER1;
  312. p->last_rx_coalesce_raw = SGE_INTRTIMER1 *
  313. (board_info(sge->adapter)->clock_core / 1000000);
  314. p->default_rx_coalesce_usecs = SGE_INTRTIMER1;
  315. p->coalesce_enable = 0; /* Turn off adaptive algorithm by default */
  316. p->sample_interval_usecs = 0;
  317. return sge;
  318. }
  319. /*
  320. * Frees all RX buffers on the freelist Q. The caller must make sure that
  321. * the SGE is turned off before calling this function.
  322. */
  323. static void free_freelQ_buffers(struct pci_dev *pdev, struct freelQ *Q)
  324. {
  325. unsigned int cidx = Q->cidx, credits = Q->credits;
  326. while (credits--) {
  327. struct freelQ_ce *ce = &Q->centries[cidx];
  328. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  329. pci_unmap_len(ce, dma_len),
  330. PCI_DMA_FROMDEVICE);
  331. dev_kfree_skb(ce->skb);
  332. ce->skb = NULL;
  333. if (++cidx == Q->entries_n)
  334. cidx = 0;
  335. }
  336. }
  337. /*
  338. * Free RX free list and response queue resources.
  339. */
  340. static void free_rx_resources(struct sge *sge)
  341. {
  342. struct pci_dev *pdev = sge->adapter->pdev;
  343. unsigned int size, i;
  344. if (sge->respQ.entries) {
  345. size = sizeof(struct respQ_e) * sge->respQ.entries_n;
  346. pci_free_consistent(pdev, size, sge->respQ.entries,
  347. sge->respQ.dma_addr);
  348. }
  349. for (i = 0; i < SGE_FREELQ_N; i++) {
  350. struct freelQ *Q = &sge->freelQ[i];
  351. if (Q->centries) {
  352. free_freelQ_buffers(pdev, Q);
  353. kfree(Q->centries);
  354. }
  355. if (Q->entries) {
  356. size = sizeof(struct freelQ_e) * Q->entries_n;
  357. pci_free_consistent(pdev, size, Q->entries,
  358. Q->dma_addr);
  359. }
  360. }
  361. }
  362. /*
  363. * Allocates basic RX resources, consisting of memory mapped freelist Qs and a
  364. * response Q.
  365. */
  366. static int alloc_rx_resources(struct sge *sge, struct sge_params *p)
  367. {
  368. struct pci_dev *pdev = sge->adapter->pdev;
  369. unsigned int size, i;
  370. for (i = 0; i < SGE_FREELQ_N; i++) {
  371. struct freelQ *Q = &sge->freelQ[i];
  372. Q->genbit = 1;
  373. Q->entries_n = p->freelQ_size[i];
  374. Q->dma_offset = SGE_RX_OFFSET - sge->rx_pkt_pad;
  375. size = sizeof(struct freelQ_e) * Q->entries_n;
  376. Q->entries = (struct freelQ_e *)
  377. pci_alloc_consistent(pdev, size, &Q->dma_addr);
  378. if (!Q->entries)
  379. goto err_no_mem;
  380. memset(Q->entries, 0, size);
  381. Q->centries = kcalloc(Q->entries_n, sizeof(struct freelQ_ce),
  382. GFP_KERNEL);
  383. if (!Q->centries)
  384. goto err_no_mem;
  385. }
  386. /*
  387. * Calculate the buffer sizes for the two free lists. FL0 accommodates
  388. * regular sized Ethernet frames, FL1 is sized not to exceed 16K,
  389. * including all the sk_buff overhead.
  390. *
  391. * Note: For T2 FL0 and FL1 are reversed.
  392. */
  393. sge->freelQ[!sge->jumbo_fl].rx_buffer_size = SGE_RX_SM_BUF_SIZE +
  394. sizeof(struct cpl_rx_data) +
  395. sge->freelQ[!sge->jumbo_fl].dma_offset;
  396. sge->freelQ[sge->jumbo_fl].rx_buffer_size = (16 * 1024) -
  397. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  398. sge->respQ.genbit = 1;
  399. sge->respQ.entries_n = SGE_RESPQ_E_N;
  400. sge->respQ.credits = SGE_RESPQ_E_N;
  401. size = sizeof(struct respQ_e) * sge->respQ.entries_n;
  402. sge->respQ.entries = (struct respQ_e *)
  403. pci_alloc_consistent(pdev, size, &sge->respQ.dma_addr);
  404. if (!sge->respQ.entries)
  405. goto err_no_mem;
  406. memset(sge->respQ.entries, 0, size);
  407. return 0;
  408. err_no_mem:
  409. free_rx_resources(sge);
  410. return -ENOMEM;
  411. }
  412. /*
  413. * Frees 'credits_pend' TX buffers and returns the credits to Q->credits.
  414. *
  415. * The adaptive algorithm receives the total size of the buffers freed
  416. * accumulated in @*totpayload. No initialization of this argument here.
  417. *
  418. */
  419. static void free_cmdQ_buffers(struct sge *sge, struct cmdQ *Q,
  420. unsigned int credits_pend, unsigned int *totpayload)
  421. {
  422. struct pci_dev *pdev = sge->adapter->pdev;
  423. struct sk_buff *skb;
  424. struct cmdQ_ce *ce, *cq = Q->centries;
  425. unsigned int entries_n = Q->entries_n, cidx = Q->cidx,
  426. i = credits_pend;
  427. ce = &cq[cidx];
  428. while (i--) {
  429. if (ce->single)
  430. pci_unmap_single(pdev, pci_unmap_addr(ce, dma_addr),
  431. pci_unmap_len(ce, dma_len),
  432. PCI_DMA_TODEVICE);
  433. else
  434. pci_unmap_page(pdev, pci_unmap_addr(ce, dma_addr),
  435. pci_unmap_len(ce, dma_len),
  436. PCI_DMA_TODEVICE);
  437. if (totpayload)
  438. *totpayload += pci_unmap_len(ce, dma_len);
  439. skb = ce->skb;
  440. if (skb)
  441. dev_kfree_skb_irq(skb);
  442. ce++;
  443. if (++cidx == entries_n) {
  444. cidx = 0;
  445. ce = cq;
  446. }
  447. }
  448. Q->cidx = cidx;
  449. atomic_add(credits_pend, &Q->credits);
  450. }
  451. /*
  452. * Free TX resources.
  453. *
  454. * Assumes that SGE is stopped and all interrupts are disabled.
  455. */
  456. static void free_tx_resources(struct sge *sge)
  457. {
  458. struct pci_dev *pdev = sge->adapter->pdev;
  459. unsigned int size, i;
  460. for (i = 0; i < SGE_CMDQ_N; i++) {
  461. struct cmdQ *Q = &sge->cmdQ[i];
  462. if (Q->centries) {
  463. unsigned int pending = Q->entries_n -
  464. atomic_read(&Q->credits);
  465. if (pending)
  466. free_cmdQ_buffers(sge, Q, pending, NULL);
  467. kfree(Q->centries);
  468. }
  469. if (Q->entries) {
  470. size = sizeof(struct cmdQ_e) * Q->entries_n;
  471. pci_free_consistent(pdev, size, Q->entries,
  472. Q->dma_addr);
  473. }
  474. }
  475. }
  476. /*
  477. * Allocates basic TX resources, consisting of memory mapped command Qs.
  478. */
  479. static int alloc_tx_resources(struct sge *sge, struct sge_params *p)
  480. {
  481. struct pci_dev *pdev = sge->adapter->pdev;
  482. unsigned int size, i;
  483. for (i = 0; i < SGE_CMDQ_N; i++) {
  484. struct cmdQ *Q = &sge->cmdQ[i];
  485. Q->genbit = 1;
  486. Q->entries_n = p->cmdQ_size[i];
  487. atomic_set(&Q->credits, Q->entries_n);
  488. atomic_set(&Q->asleep, 1);
  489. spin_lock_init(&Q->Qlock);
  490. size = sizeof(struct cmdQ_e) * Q->entries_n;
  491. Q->entries = (struct cmdQ_e *)
  492. pci_alloc_consistent(pdev, size, &Q->dma_addr);
  493. if (!Q->entries)
  494. goto err_no_mem;
  495. memset(Q->entries, 0, size);
  496. Q->centries = kcalloc(Q->entries_n, sizeof(struct cmdQ_ce),
  497. GFP_KERNEL);
  498. if (!Q->centries)
  499. goto err_no_mem;
  500. }
  501. return 0;
  502. err_no_mem:
  503. free_tx_resources(sge);
  504. return -ENOMEM;
  505. }
  506. static inline void setup_ring_params(struct adapter *adapter, u64 addr,
  507. u32 size, int base_reg_lo,
  508. int base_reg_hi, int size_reg)
  509. {
  510. t1_write_reg_4(adapter, base_reg_lo, (u32)addr);
  511. t1_write_reg_4(adapter, base_reg_hi, addr >> 32);
  512. t1_write_reg_4(adapter, size_reg, size);
  513. }
  514. /*
  515. * Enable/disable VLAN acceleration.
  516. */
  517. void t1_set_vlan_accel(struct adapter *adapter, int on_off)
  518. {
  519. struct sge *sge = adapter->sge;
  520. sge->sge_control &= ~F_VLAN_XTRACT;
  521. if (on_off)
  522. sge->sge_control |= F_VLAN_XTRACT;
  523. if (adapter->open_device_map) {
  524. t1_write_reg_4(adapter, A_SG_CONTROL, sge->sge_control);
  525. t1_read_reg_4(adapter, A_SG_CONTROL); /* flush */
  526. }
  527. }
  528. /*
  529. * Sets the interrupt latency timer when the adaptive Rx coalescing
  530. * is turned off. Do nothing when it is turned on again.
  531. *
  532. * This routine relies on the fact that the caller has already set
  533. * the adaptive policy in adapter->sge_params before calling it.
  534. */
  535. int t1_sge_set_coalesce_params(struct sge *sge, struct sge_params *p)
  536. {
  537. if (!p->coalesce_enable) {
  538. u32 newTimer = p->rx_coalesce_usecs *
  539. (board_info(sge->adapter)->clock_core / 1000000);
  540. t1_write_reg_4(sge->adapter, A_SG_INTRTIMER, newTimer);
  541. }
  542. return 0;
  543. }
  544. /*
  545. * Programs the various SGE registers. However, the engine is not yet enabled,
  546. * but sge->sge_control is setup and ready to go.
  547. */
  548. static void configure_sge(struct sge *sge, struct sge_params *p)
  549. {
  550. struct adapter *ap = sge->adapter;
  551. int i;
  552. t1_write_reg_4(ap, A_SG_CONTROL, 0);
  553. setup_ring_params(ap, sge->cmdQ[0].dma_addr, sge->cmdQ[0].entries_n,
  554. A_SG_CMD0BASELWR, A_SG_CMD0BASEUPR, A_SG_CMD0SIZE);
  555. setup_ring_params(ap, sge->cmdQ[1].dma_addr, sge->cmdQ[1].entries_n,
  556. A_SG_CMD1BASELWR, A_SG_CMD1BASEUPR, A_SG_CMD1SIZE);
  557. setup_ring_params(ap, sge->freelQ[0].dma_addr,
  558. sge->freelQ[0].entries_n, A_SG_FL0BASELWR,
  559. A_SG_FL0BASEUPR, A_SG_FL0SIZE);
  560. setup_ring_params(ap, sge->freelQ[1].dma_addr,
  561. sge->freelQ[1].entries_n, A_SG_FL1BASELWR,
  562. A_SG_FL1BASEUPR, A_SG_FL1SIZE);
  563. /* The threshold comparison uses <. */
  564. t1_write_reg_4(ap, A_SG_FLTHRESHOLD, SGE_RX_SM_BUF_SIZE + 1);
  565. setup_ring_params(ap, sge->respQ.dma_addr, sge->respQ.entries_n,
  566. A_SG_RSPBASELWR, A_SG_RSPBASEUPR, A_SG_RSPSIZE);
  567. t1_write_reg_4(ap, A_SG_RSPQUEUECREDIT, (u32)sge->respQ.entries_n);
  568. sge->sge_control = F_CMDQ0_ENABLE | F_CMDQ1_ENABLE | F_FL0_ENABLE |
  569. F_FL1_ENABLE | F_CPL_ENABLE | F_RESPONSE_QUEUE_ENABLE |
  570. V_CMDQ_PRIORITY(2) | F_DISABLE_CMDQ1_GTS | F_ISCSI_COALESCE |
  571. V_RX_PKT_OFFSET(sge->rx_pkt_pad);
  572. #if defined(__BIG_ENDIAN_BITFIELD)
  573. sge->sge_control |= F_ENABLE_BIG_ENDIAN;
  574. #endif
  575. /*
  576. * Initialize the SGE Interrupt Timer arrray:
  577. * intrtimer[0] = (SGE_INTRTIMER0) usec
  578. * intrtimer[0<i<5] = (SGE_INTRTIMER0 + i*2) usec
  579. * intrtimer[4<i<10] = ((i - 3) * 6) usec
  580. * intrtimer[10] = (SGE_INTRTIMER1) usec
  581. *
  582. */
  583. sge->intrtimer[0] = board_info(sge->adapter)->clock_core / 1000000;
  584. for (i = 1; i < SGE_INTR_LATBUCKETS; ++i) {
  585. sge->intrtimer[i] = SGE_INTRTIMER0 + (2 * i);
  586. sge->intrtimer[i] *= sge->intrtimer[0];
  587. }
  588. for (i = SGE_INTR_LATBUCKETS; i < SGE_INTR_MAXBUCKETS - 1; ++i) {
  589. sge->intrtimer[i] = (i - 3) * 6;
  590. sge->intrtimer[i] *= sge->intrtimer[0];
  591. }
  592. sge->intrtimer[SGE_INTR_MAXBUCKETS - 1] =
  593. sge->intrtimer[0] * SGE_INTRTIMER1;
  594. /* Initialize resource timer */
  595. sge->intrtimer_nres = sge->intrtimer[0] * SGE_INTRTIMER_NRES;
  596. /* Finally finish initialization of intrtimer[0] */
  597. sge->intrtimer[0] *= SGE_INTRTIMER0;
  598. /* Initialize for a throughput oriented workload */
  599. sge->currIndex = SGE_INTR_MAXBUCKETS - 1;
  600. if (p->coalesce_enable)
  601. t1_write_reg_4(ap, A_SG_INTRTIMER,
  602. sge->intrtimer[sge->currIndex]);
  603. else
  604. t1_sge_set_coalesce_params(sge, p);
  605. }
  606. /*
  607. * Return the payload capacity of the jumbo free-list buffers.
  608. */
  609. static inline unsigned int jumbo_payload_capacity(const struct sge *sge)
  610. {
  611. return sge->freelQ[sge->jumbo_fl].rx_buffer_size -
  612. sizeof(struct cpl_rx_data) - SGE_RX_OFFSET + sge->rx_pkt_pad;
  613. }
  614. /*
  615. * Allocates both RX and TX resources and configures the SGE. However,
  616. * the hardware is not enabled yet.
  617. */
  618. int t1_sge_configure(struct sge *sge, struct sge_params *p)
  619. {
  620. if (alloc_rx_resources(sge, p))
  621. return -ENOMEM;
  622. if (alloc_tx_resources(sge, p)) {
  623. free_rx_resources(sge);
  624. return -ENOMEM;
  625. }
  626. configure_sge(sge, p);
  627. /*
  628. * Now that we have sized the free lists calculate the payload
  629. * capacity of the large buffers. Other parts of the driver use
  630. * this to set the max offload coalescing size so that RX packets
  631. * do not overflow our large buffers.
  632. */
  633. p->large_buf_capacity = jumbo_payload_capacity(sge);
  634. return 0;
  635. }
  636. /*
  637. * Frees all SGE related resources and the sge structure itself
  638. */
  639. void t1_sge_destroy(struct sge *sge)
  640. {
  641. if (sge->pskb)
  642. dev_kfree_skb(sge->pskb);
  643. free_tx_resources(sge);
  644. free_rx_resources(sge);
  645. kfree(sge);
  646. }
  647. /*
  648. * Allocates new RX buffers on the freelist Q (and tracks them on the freelist
  649. * context Q) until the Q is full or alloc_skb fails.
  650. *
  651. * It is possible that the generation bits already match, indicating that the
  652. * buffer is already valid and nothing needs to be done. This happens when we
  653. * copied a received buffer into a new sk_buff during the interrupt processing.
  654. *
  655. * If the SGE doesn't automatically align packets properly (!sge->rx_pkt_pad),
  656. * we specify a RX_OFFSET in order to make sure that the IP header is 4B
  657. * aligned.
  658. */
  659. static void refill_free_list(struct sge *sge, struct freelQ *Q)
  660. {
  661. struct pci_dev *pdev = sge->adapter->pdev;
  662. struct freelQ_ce *ce = &Q->centries[Q->pidx];
  663. struct freelQ_e *e = &Q->entries[Q->pidx];
  664. unsigned int dma_len = Q->rx_buffer_size - Q->dma_offset;
  665. while (Q->credits < Q->entries_n) {
  666. if (e->GenerationBit != Q->genbit) {
  667. struct sk_buff *skb;
  668. dma_addr_t mapping;
  669. skb = alloc_skb(Q->rx_buffer_size, GFP_ATOMIC);
  670. if (!skb)
  671. break;
  672. if (Q->dma_offset)
  673. skb_reserve(skb, Q->dma_offset);
  674. mapping = pci_map_single(pdev, skb->data, dma_len,
  675. PCI_DMA_FROMDEVICE);
  676. ce->skb = skb;
  677. pci_unmap_addr_set(ce, dma_addr, mapping);
  678. pci_unmap_len_set(ce, dma_len, dma_len);
  679. e->AddrLow = (u32)mapping;
  680. e->AddrHigh = (u64)mapping >> 32;
  681. e->BufferLength = dma_len;
  682. e->GenerationBit = e->GenerationBit2 = Q->genbit;
  683. }
  684. e++;
  685. ce++;
  686. if (++Q->pidx == Q->entries_n) {
  687. Q->pidx = 0;
  688. Q->genbit ^= 1;
  689. ce = Q->centries;
  690. e = Q->entries;
  691. }
  692. Q->credits++;
  693. }
  694. }
  695. /*
  696. * Calls refill_free_list for both freelist Qs. If we cannot
  697. * fill at least 1/4 of both Qs, we go into 'few interrupt mode' in order
  698. * to give the system time to free up resources.
  699. */
  700. static void freelQs_empty(struct sge *sge)
  701. {
  702. u32 irq_reg = t1_read_reg_4(sge->adapter, A_SG_INT_ENABLE);
  703. u32 irqholdoff_reg;
  704. refill_free_list(sge, &sge->freelQ[0]);
  705. refill_free_list(sge, &sge->freelQ[1]);
  706. if (sge->freelQ[0].credits > (sge->freelQ[0].entries_n >> 2) &&
  707. sge->freelQ[1].credits > (sge->freelQ[1].entries_n >> 2)) {
  708. irq_reg |= F_FL_EXHAUSTED;
  709. irqholdoff_reg = sge->intrtimer[sge->currIndex];
  710. } else {
  711. /* Clear the F_FL_EXHAUSTED interrupts for now */
  712. irq_reg &= ~F_FL_EXHAUSTED;
  713. irqholdoff_reg = sge->intrtimer_nres;
  714. }
  715. t1_write_reg_4(sge->adapter, A_SG_INTRTIMER, irqholdoff_reg);
  716. t1_write_reg_4(sge->adapter, A_SG_INT_ENABLE, irq_reg);
  717. /* We reenable the Qs to force a freelist GTS interrupt later */
  718. doorbell_pio(sge, F_FL0_ENABLE | F_FL1_ENABLE);
  719. }
  720. #define SGE_PL_INTR_MASK (F_PL_INTR_SGE_ERR | F_PL_INTR_SGE_DATA)
  721. #define SGE_INT_FATAL (F_RESPQ_OVERFLOW | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
  722. #define SGE_INT_ENABLE (F_RESPQ_EXHAUSTED | F_RESPQ_OVERFLOW | \
  723. F_FL_EXHAUSTED | F_PACKET_TOO_BIG | F_PACKET_MISMATCH)
  724. /*
  725. * Disable SGE Interrupts
  726. */
  727. void t1_sge_intr_disable(struct sge *sge)
  728. {
  729. u32 val = t1_read_reg_4(sge->adapter, A_PL_ENABLE);
  730. t1_write_reg_4(sge->adapter, A_PL_ENABLE, val & ~SGE_PL_INTR_MASK);
  731. t1_write_reg_4(sge->adapter, A_SG_INT_ENABLE, 0);
  732. }
  733. /*
  734. * Enable SGE interrupts.
  735. */
  736. void t1_sge_intr_enable(struct sge *sge)
  737. {
  738. u32 en = SGE_INT_ENABLE;
  739. u32 val = t1_read_reg_4(sge->adapter, A_PL_ENABLE);
  740. if (sge->adapter->flags & TSO_CAPABLE)
  741. en &= ~F_PACKET_TOO_BIG;
  742. t1_write_reg_4(sge->adapter, A_SG_INT_ENABLE, en);
  743. t1_write_reg_4(sge->adapter, A_PL_ENABLE, val | SGE_PL_INTR_MASK);
  744. }
  745. /*
  746. * Clear SGE interrupts.
  747. */
  748. void t1_sge_intr_clear(struct sge *sge)
  749. {
  750. t1_write_reg_4(sge->adapter, A_PL_CAUSE, SGE_PL_INTR_MASK);
  751. t1_write_reg_4(sge->adapter, A_SG_INT_CAUSE, 0xffffffff);
  752. }
  753. /*
  754. * SGE 'Error' interrupt handler
  755. */
  756. int t1_sge_intr_error_handler(struct sge *sge)
  757. {
  758. struct adapter *adapter = sge->adapter;
  759. u32 cause = t1_read_reg_4(adapter, A_SG_INT_CAUSE);
  760. if (adapter->flags & TSO_CAPABLE)
  761. cause &= ~F_PACKET_TOO_BIG;
  762. if (cause & F_RESPQ_EXHAUSTED)
  763. sge->intr_cnt.respQ_empty++;
  764. if (cause & F_RESPQ_OVERFLOW) {
  765. sge->intr_cnt.respQ_overflow++;
  766. CH_ALERT("%s: SGE response queue overflow\n",
  767. adapter->name);
  768. }
  769. if (cause & F_FL_EXHAUSTED) {
  770. sge->intr_cnt.freelistQ_empty++;
  771. freelQs_empty(sge);
  772. }
  773. if (cause & F_PACKET_TOO_BIG) {
  774. sge->intr_cnt.pkt_too_big++;
  775. CH_ALERT("%s: SGE max packet size exceeded\n",
  776. adapter->name);
  777. }
  778. if (cause & F_PACKET_MISMATCH) {
  779. sge->intr_cnt.pkt_mismatch++;
  780. CH_ALERT("%s: SGE packet mismatch\n", adapter->name);
  781. }
  782. if (cause & SGE_INT_FATAL)
  783. t1_fatal_err(adapter);
  784. t1_write_reg_4(adapter, A_SG_INT_CAUSE, cause);
  785. return 0;
  786. }
  787. /*
  788. * The following code is copied from 2.6, where the skb_pull is doing the
  789. * right thing and only pulls ETH_HLEN.
  790. *
  791. * Determine the packet's protocol ID. The rule here is that we
  792. * assume 802.3 if the type field is short enough to be a length.
  793. * This is normal practice and works for any 'now in use' protocol.
  794. */
  795. static unsigned short sge_eth_type_trans(struct sk_buff *skb,
  796. struct net_device *dev)
  797. {
  798. struct ethhdr *eth;
  799. unsigned char *rawp;
  800. skb->mac.raw = skb->data;
  801. skb_pull(skb, ETH_HLEN);
  802. eth = (struct ethhdr *)skb->mac.raw;
  803. if (*eth->h_dest&1) {
  804. if(memcmp(eth->h_dest, dev->broadcast, ETH_ALEN) == 0)
  805. skb->pkt_type = PACKET_BROADCAST;
  806. else
  807. skb->pkt_type = PACKET_MULTICAST;
  808. }
  809. /*
  810. * This ALLMULTI check should be redundant by 1.4
  811. * so don't forget to remove it.
  812. *
  813. * Seems, you forgot to remove it. All silly devices
  814. * seems to set IFF_PROMISC.
  815. */
  816. else if (1 /*dev->flags&IFF_PROMISC*/)
  817. {
  818. if(memcmp(eth->h_dest,dev->dev_addr, ETH_ALEN))
  819. skb->pkt_type=PACKET_OTHERHOST;
  820. }
  821. if (ntohs(eth->h_proto) >= 1536)
  822. return eth->h_proto;
  823. rawp = skb->data;
  824. /*
  825. * This is a magic hack to spot IPX packets. Older Novell breaks
  826. * the protocol design and runs IPX over 802.3 without an 802.2 LLC
  827. * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
  828. * won't work for fault tolerant netware but does for the rest.
  829. */
  830. if (*(unsigned short *)rawp == 0xFFFF)
  831. return htons(ETH_P_802_3);
  832. /*
  833. * Real 802.2 LLC
  834. */
  835. return htons(ETH_P_802_2);
  836. }
  837. /*
  838. * Prepare the received buffer and pass it up the stack. If it is small enough
  839. * and allocation doesn't fail, we use a new sk_buff and copy the content.
  840. */
  841. static unsigned int t1_sge_rx(struct sge *sge, struct freelQ *Q,
  842. unsigned int len, unsigned int offload)
  843. {
  844. struct sk_buff *skb;
  845. struct adapter *adapter = sge->adapter;
  846. struct freelQ_ce *ce = &Q->centries[Q->cidx];
  847. if (len <= SGE_RX_COPY_THRESHOLD &&
  848. (skb = alloc_skb(len + NET_IP_ALIGN, GFP_ATOMIC))) {
  849. struct freelQ_e *e;
  850. char *src = ce->skb->data;
  851. pci_dma_sync_single_for_cpu(adapter->pdev,
  852. pci_unmap_addr(ce, dma_addr),
  853. pci_unmap_len(ce, dma_len),
  854. PCI_DMA_FROMDEVICE);
  855. if (!offload) {
  856. skb_reserve(skb, NET_IP_ALIGN);
  857. src += sge->rx_pkt_pad;
  858. }
  859. memcpy(skb->data, src, len);
  860. /* Reuse the entry. */
  861. e = &Q->entries[Q->cidx];
  862. e->GenerationBit ^= 1;
  863. e->GenerationBit2 ^= 1;
  864. } else {
  865. pci_unmap_single(adapter->pdev, pci_unmap_addr(ce, dma_addr),
  866. pci_unmap_len(ce, dma_len),
  867. PCI_DMA_FROMDEVICE);
  868. skb = ce->skb;
  869. if (!offload && sge->rx_pkt_pad)
  870. __skb_pull(skb, sge->rx_pkt_pad);
  871. }
  872. skb_put(skb, len);
  873. if (unlikely(offload)) {
  874. {
  875. printk(KERN_ERR
  876. "%s: unexpected offloaded packet, cmd %u\n",
  877. adapter->name, *skb->data);
  878. dev_kfree_skb_any(skb);
  879. }
  880. } else {
  881. struct cpl_rx_pkt *p = (struct cpl_rx_pkt *)skb->data;
  882. skb_pull(skb, sizeof(*p));
  883. skb->dev = adapter->port[p->iff].dev;
  884. skb->dev->last_rx = jiffies;
  885. skb->protocol = sge_eth_type_trans(skb, skb->dev);
  886. if ((adapter->flags & RX_CSUM_ENABLED) && p->csum == 0xffff &&
  887. skb->protocol == htons(ETH_P_IP) &&
  888. (skb->data[9] == IPPROTO_TCP ||
  889. skb->data[9] == IPPROTO_UDP))
  890. skb->ip_summed = CHECKSUM_UNNECESSARY;
  891. else
  892. skb->ip_summed = CHECKSUM_NONE;
  893. if (adapter->vlan_grp && p->vlan_valid)
  894. vlan_hwaccel_rx(skb, adapter->vlan_grp,
  895. ntohs(p->vlan));
  896. else
  897. netif_rx(skb);
  898. }
  899. if (++Q->cidx == Q->entries_n)
  900. Q->cidx = 0;
  901. if (unlikely(--Q->credits < Q->entries_n - SGE_FREEL_REFILL_THRESH))
  902. refill_free_list(sge, Q);
  903. return 1;
  904. }
  905. /*
  906. * Adaptive interrupt timer logic to keep the CPU utilization to
  907. * manageable levels. Basically, as the Average Packet Size (APS)
  908. * gets higher, the interrupt latency setting gets longer. Every
  909. * SGE_INTR_BUCKETSIZE (of 100B) causes a bump of 2usec to the
  910. * base value of SGE_INTRTIMER0. At large values of payload the
  911. * latency hits the ceiling value of SGE_INTRTIMER1 stored at
  912. * index SGE_INTR_MAXBUCKETS-1 in sge->intrtimer[].
  913. *
  914. * sge->currIndex caches the last index to save unneeded PIOs.
  915. */
  916. static inline void update_intr_timer(struct sge *sge, unsigned int avg_payload)
  917. {
  918. unsigned int newIndex;
  919. newIndex = avg_payload / SGE_INTR_BUCKETSIZE;
  920. if (newIndex > SGE_INTR_MAXBUCKETS - 1) {
  921. newIndex = SGE_INTR_MAXBUCKETS - 1;
  922. }
  923. /* Save a PIO with this check....maybe */
  924. if (newIndex != sge->currIndex) {
  925. t1_write_reg_4(sge->adapter, A_SG_INTRTIMER,
  926. sge->intrtimer[newIndex]);
  927. sge->currIndex = newIndex;
  928. sge->adapter->params.sge.last_rx_coalesce_raw =
  929. sge->intrtimer[newIndex];
  930. }
  931. }
  932. /*
  933. * Returns true if command queue q_num has enough available descriptors that
  934. * we can resume Tx operation after temporarily disabling its packet queue.
  935. */
  936. static inline int enough_free_Tx_descs(struct sge *sge, int q_num)
  937. {
  938. return atomic_read(&sge->cmdQ[q_num].credits) >
  939. (sge->cmdQ[q_num].entries_n >> 2);
  940. }
  941. /*
  942. * Main interrupt handler, optimized assuming that we took a 'DATA'
  943. * interrupt.
  944. *
  945. * 1. Clear the interrupt
  946. * 2. Loop while we find valid descriptors and process them; accumulate
  947. * information that can be processed after the loop
  948. * 3. Tell the SGE at which index we stopped processing descriptors
  949. * 4. Bookkeeping; free TX buffers, ring doorbell if there are any
  950. * outstanding TX buffers waiting, replenish RX buffers, potentially
  951. * reenable upper layers if they were turned off due to lack of TX
  952. * resources which are available again.
  953. * 5. If we took an interrupt, but no valid respQ descriptors was found we
  954. * let the slow_intr_handler run and do error handling.
  955. */
  956. irqreturn_t t1_interrupt(int irq, void *cookie, struct pt_regs *regs)
  957. {
  958. struct net_device *netdev;
  959. struct adapter *adapter = cookie;
  960. struct sge *sge = adapter->sge;
  961. struct respQ *Q = &sge->respQ;
  962. unsigned int credits = Q->credits, flags = 0, ret = 0;
  963. unsigned int tot_rxpayload = 0, tot_txpayload = 0, n_rx = 0, n_tx = 0;
  964. unsigned int credits_pend[SGE_CMDQ_N] = { 0, 0 };
  965. struct respQ_e *e = &Q->entries[Q->cidx];
  966. prefetch(e);
  967. t1_write_reg_4(adapter, A_PL_CAUSE, F_PL_INTR_SGE_DATA);
  968. while (e->GenerationBit == Q->genbit) {
  969. if (--credits < SGE_RESPQ_REPLENISH_THRES) {
  970. u32 n = Q->entries_n - credits - 1;
  971. t1_write_reg_4(adapter, A_SG_RSPQUEUECREDIT, n);
  972. credits += n;
  973. }
  974. if (likely(e->DataValid)) {
  975. if (!e->Sop || !e->Eop)
  976. BUG();
  977. t1_sge_rx(sge, &sge->freelQ[e->FreelistQid],
  978. e->BufferLength, e->Offload);
  979. tot_rxpayload += e->BufferLength;
  980. ++n_rx;
  981. }
  982. flags |= e->Qsleeping;
  983. credits_pend[0] += e->Cmdq0CreditReturn;
  984. credits_pend[1] += e->Cmdq1CreditReturn;
  985. #ifdef CONFIG_SMP
  986. /*
  987. * If enough cmdQ0 buffers have finished DMAing free them so
  988. * anyone that may be waiting for their release can continue.
  989. * We do this only on MP systems to allow other CPUs to proceed
  990. * promptly. UP systems can wait for the free_cmdQ_buffers()
  991. * calls after this loop as the sole CPU is currently busy in
  992. * this loop.
  993. */
  994. if (unlikely(credits_pend[0] > SGE_FREEL_REFILL_THRESH)) {
  995. free_cmdQ_buffers(sge, &sge->cmdQ[0], credits_pend[0],
  996. &tot_txpayload);
  997. n_tx += credits_pend[0];
  998. credits_pend[0] = 0;
  999. }
  1000. #endif
  1001. ret++;
  1002. e++;
  1003. if (unlikely(++Q->cidx == Q->entries_n)) {
  1004. Q->cidx = 0;
  1005. Q->genbit ^= 1;
  1006. e = Q->entries;
  1007. }
  1008. }
  1009. Q->credits = credits;
  1010. t1_write_reg_4(adapter, A_SG_SLEEPING, Q->cidx);
  1011. if (credits_pend[0])
  1012. free_cmdQ_buffers(sge, &sge->cmdQ[0], credits_pend[0], &tot_txpayload);
  1013. if (credits_pend[1])
  1014. free_cmdQ_buffers(sge, &sge->cmdQ[1], credits_pend[1], &tot_txpayload);
  1015. /* Do any coalescing and interrupt latency timer adjustments */
  1016. if (adapter->params.sge.coalesce_enable) {
  1017. unsigned int avg_txpayload = 0, avg_rxpayload = 0;
  1018. n_tx += credits_pend[0] + credits_pend[1];
  1019. /*
  1020. * Choose larger avg. payload size to increase
  1021. * throughput and reduce [CPU util., intr/s.]
  1022. *
  1023. * Throughput behavior favored in mixed-mode.
  1024. */
  1025. if (n_tx)
  1026. avg_txpayload = tot_txpayload/n_tx;
  1027. if (n_rx)
  1028. avg_rxpayload = tot_rxpayload/n_rx;
  1029. if (n_tx && avg_txpayload > avg_rxpayload){
  1030. update_intr_timer(sge, avg_txpayload);
  1031. } else if (n_rx) {
  1032. update_intr_timer(sge, avg_rxpayload);
  1033. }
  1034. }
  1035. if (flags & F_CMDQ0_ENABLE) {
  1036. struct cmdQ *cmdQ = &sge->cmdQ[0];
  1037. atomic_set(&cmdQ->asleep, 1);
  1038. if (atomic_read(&cmdQ->pio_pidx) != cmdQ->pidx) {
  1039. doorbell_pio(sge, F_CMDQ0_ENABLE);
  1040. atomic_set(&cmdQ->pio_pidx, cmdQ->pidx);
  1041. }
  1042. }
  1043. if (unlikely(flags & (F_FL0_ENABLE | F_FL1_ENABLE)))
  1044. freelQs_empty(sge);
  1045. netdev = adapter->port[0].dev;
  1046. if (unlikely(netif_queue_stopped(netdev) && netif_carrier_ok(netdev) &&
  1047. enough_free_Tx_descs(sge, 0) &&
  1048. enough_free_Tx_descs(sge, 1))) {
  1049. netif_wake_queue(netdev);
  1050. }
  1051. if (unlikely(!ret))
  1052. ret = t1_slow_intr_handler(adapter);
  1053. return IRQ_RETVAL(ret != 0);
  1054. }
  1055. /*
  1056. * Enqueues the sk_buff onto the cmdQ[qid] and has hardware fetch it.
  1057. *
  1058. * The code figures out how many entries the sk_buff will require in the
  1059. * cmdQ and updates the cmdQ data structure with the state once the enqueue
  1060. * has complete. Then, it doesn't access the global structure anymore, but
  1061. * uses the corresponding fields on the stack. In conjuction with a spinlock
  1062. * around that code, we can make the function reentrant without holding the
  1063. * lock when we actually enqueue (which might be expensive, especially on
  1064. * architectures with IO MMUs).
  1065. */
  1066. static unsigned int t1_sge_tx(struct sk_buff *skb, struct adapter *adapter,
  1067. unsigned int qid)
  1068. {
  1069. struct sge *sge = adapter->sge;
  1070. struct cmdQ *Q = &sge->cmdQ[qid];
  1071. struct cmdQ_e *e;
  1072. struct cmdQ_ce *ce;
  1073. dma_addr_t mapping;
  1074. unsigned int credits, pidx, genbit;
  1075. unsigned int count = 1 + skb_shinfo(skb)->nr_frags;
  1076. /*
  1077. * Coming from the timer
  1078. */
  1079. if ((skb == sge->pskb)) {
  1080. /*
  1081. * Quit if any cmdQ activities
  1082. */
  1083. if (!spin_trylock(&Q->Qlock))
  1084. return 0;
  1085. if (atomic_read(&Q->credits) != Q->entries_n) {
  1086. spin_unlock(&Q->Qlock);
  1087. return 0;
  1088. }
  1089. }
  1090. else
  1091. spin_lock(&Q->Qlock);
  1092. genbit = Q->genbit;
  1093. pidx = Q->pidx;
  1094. credits = atomic_read(&Q->credits);
  1095. credits -= count;
  1096. atomic_sub(count, &Q->credits);
  1097. Q->pidx += count;
  1098. if (Q->pidx >= Q->entries_n) {
  1099. Q->pidx -= Q->entries_n;
  1100. Q->genbit ^= 1;
  1101. }
  1102. if (unlikely(credits < (MAX_SKB_FRAGS + 1))) {
  1103. sge->intr_cnt.cmdQ_full[qid]++;
  1104. netif_stop_queue(adapter->port[0].dev);
  1105. }
  1106. spin_unlock(&Q->Qlock);
  1107. mapping = pci_map_single(adapter->pdev, skb->data,
  1108. skb->len - skb->data_len, PCI_DMA_TODEVICE);
  1109. ce = &Q->centries[pidx];
  1110. ce->skb = NULL;
  1111. pci_unmap_addr_set(ce, dma_addr, mapping);
  1112. pci_unmap_len_set(ce, dma_len, skb->len - skb->data_len);
  1113. ce->single = 1;
  1114. e = &Q->entries[pidx];
  1115. e->Sop = 1;
  1116. e->DataValid = 1;
  1117. e->BufferLength = skb->len - skb->data_len;
  1118. e->AddrHigh = (u64)mapping >> 32;
  1119. e->AddrLow = (u32)mapping;
  1120. if (--count > 0) {
  1121. unsigned int i;
  1122. e->Eop = 0;
  1123. wmb();
  1124. e->GenerationBit = e->GenerationBit2 = genbit;
  1125. for (i = 0; i < count; i++) {
  1126. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1127. ce++; e++;
  1128. if (++pidx == Q->entries_n) {
  1129. pidx = 0;
  1130. genbit ^= 1;
  1131. ce = Q->centries;
  1132. e = Q->entries;
  1133. }
  1134. mapping = pci_map_page(adapter->pdev, frag->page,
  1135. frag->page_offset,
  1136. frag->size,
  1137. PCI_DMA_TODEVICE);
  1138. ce->skb = NULL;
  1139. pci_unmap_addr_set(ce, dma_addr, mapping);
  1140. pci_unmap_len_set(ce, dma_len, frag->size);
  1141. ce->single = 0;
  1142. e->Sop = 0;
  1143. e->DataValid = 1;
  1144. e->BufferLength = frag->size;
  1145. e->AddrHigh = (u64)mapping >> 32;
  1146. e->AddrLow = (u32)mapping;
  1147. if (i < count - 1) {
  1148. e->Eop = 0;
  1149. wmb();
  1150. e->GenerationBit = e->GenerationBit2 = genbit;
  1151. }
  1152. }
  1153. }
  1154. if (skb != sge->pskb)
  1155. ce->skb = skb;
  1156. e->Eop = 1;
  1157. wmb();
  1158. e->GenerationBit = e->GenerationBit2 = genbit;
  1159. /*
  1160. * We always ring the doorbell for cmdQ1. For cmdQ0, we only ring
  1161. * the doorbell if the Q is asleep. There is a natural race, where
  1162. * the hardware is going to sleep just after we checked, however,
  1163. * then the interrupt handler will detect the outstanding TX packet
  1164. * and ring the doorbell for us.
  1165. */
  1166. if (qid) {
  1167. doorbell_pio(sge, F_CMDQ1_ENABLE);
  1168. } else if (atomic_read(&Q->asleep)) {
  1169. atomic_set(&Q->asleep, 0);
  1170. doorbell_pio(sge, F_CMDQ0_ENABLE);
  1171. atomic_set(&Q->pio_pidx, Q->pidx);
  1172. }
  1173. return 0;
  1174. }
  1175. #define MK_ETH_TYPE_MSS(type, mss) (((mss) & 0x3FFF) | ((type) << 14))
  1176. /*
  1177. * Adds the CPL header to the sk_buff and passes it to t1_sge_tx.
  1178. */
  1179. int t1_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1180. {
  1181. struct adapter *adapter = dev->priv;
  1182. struct cpl_tx_pkt *cpl;
  1183. struct ethhdr *eth;
  1184. size_t max_len;
  1185. /*
  1186. * We are using a non-standard hard_header_len and some kernel
  1187. * components, such as pktgen, do not handle it right. Complain
  1188. * when this happens but try to fix things up.
  1189. */
  1190. if (unlikely(skb_headroom(skb) < dev->hard_header_len - ETH_HLEN)) {
  1191. struct sk_buff *orig_skb = skb;
  1192. if (net_ratelimit())
  1193. printk(KERN_ERR
  1194. "%s: Tx packet has inadequate headroom\n",
  1195. dev->name);
  1196. skb = skb_realloc_headroom(skb, sizeof(struct cpl_tx_pkt_lso));
  1197. dev_kfree_skb_any(orig_skb);
  1198. if (!skb)
  1199. return -ENOMEM;
  1200. }
  1201. if (skb_shinfo(skb)->tso_size) {
  1202. int eth_type;
  1203. struct cpl_tx_pkt_lso *hdr;
  1204. eth_type = skb->nh.raw - skb->data == ETH_HLEN ?
  1205. CPL_ETH_II : CPL_ETH_II_VLAN;
  1206. hdr = (struct cpl_tx_pkt_lso *)skb_push(skb, sizeof(*hdr));
  1207. hdr->opcode = CPL_TX_PKT_LSO;
  1208. hdr->ip_csum_dis = hdr->l4_csum_dis = 0;
  1209. hdr->ip_hdr_words = skb->nh.iph->ihl;
  1210. hdr->tcp_hdr_words = skb->h.th->doff;
  1211. hdr->eth_type_mss = htons(MK_ETH_TYPE_MSS(eth_type,
  1212. skb_shinfo(skb)->tso_size));
  1213. hdr->len = htonl(skb->len - sizeof(*hdr));
  1214. cpl = (struct cpl_tx_pkt *)hdr;
  1215. } else
  1216. {
  1217. /*
  1218. * An Ethernet packet must have at least space for
  1219. * the DIX Ethernet header and be no greater than
  1220. * the device set MTU. Otherwise trash the packet.
  1221. */
  1222. if (skb->len < ETH_HLEN)
  1223. goto t1_start_xmit_fail2;
  1224. eth = (struct ethhdr *)skb->data;
  1225. if (eth->h_proto == htons(ETH_P_8021Q))
  1226. max_len = dev->mtu + VLAN_ETH_HLEN;
  1227. else
  1228. max_len = dev->mtu + ETH_HLEN;
  1229. if (skb->len > max_len)
  1230. goto t1_start_xmit_fail2;
  1231. if (!(adapter->flags & UDP_CSUM_CAPABLE) &&
  1232. skb->ip_summed == CHECKSUM_HW &&
  1233. skb->nh.iph->protocol == IPPROTO_UDP &&
  1234. skb_checksum_help(skb, 0))
  1235. goto t1_start_xmit_fail3;
  1236. if (!adapter->sge->pskb) {
  1237. if (skb->protocol == htons(ETH_P_ARP) &&
  1238. skb->nh.arph->ar_op == htons(ARPOP_REQUEST))
  1239. adapter->sge->pskb = skb;
  1240. }
  1241. cpl = (struct cpl_tx_pkt *)skb_push(skb, sizeof(*cpl));
  1242. cpl->opcode = CPL_TX_PKT;
  1243. cpl->ip_csum_dis = 1; /* SW calculates IP csum */
  1244. cpl->l4_csum_dis = skb->ip_summed == CHECKSUM_HW ? 0 : 1;
  1245. /* the length field isn't used so don't bother setting it */
  1246. }
  1247. cpl->iff = dev->if_port;
  1248. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  1249. if (adapter->vlan_grp && vlan_tx_tag_present(skb)) {
  1250. cpl->vlan_valid = 1;
  1251. cpl->vlan = htons(vlan_tx_tag_get(skb));
  1252. } else
  1253. #endif
  1254. cpl->vlan_valid = 0;
  1255. dev->trans_start = jiffies;
  1256. return t1_sge_tx(skb, adapter, 0);
  1257. t1_start_xmit_fail3:
  1258. printk(KERN_INFO "%s: Unable to complete checksum\n", dev->name);
  1259. goto t1_start_xmit_fail1;
  1260. t1_start_xmit_fail2:
  1261. printk(KERN_INFO "%s: Invalid packet length %d, dropping\n",
  1262. dev->name, skb->len);
  1263. t1_start_xmit_fail1:
  1264. dev_kfree_skb_any(skb);
  1265. return 0;
  1266. }
  1267. void t1_sge_set_ptimeout(adapter_t *adapter, u32 val)
  1268. {
  1269. struct sge *sge = adapter->sge;
  1270. if (is_T2(adapter))
  1271. sge->ptimeout = max((u32)((HZ * val) / 1000), (u32)1);
  1272. }
  1273. u32 t1_sge_get_ptimeout(adapter_t *adapter)
  1274. {
  1275. struct sge *sge = adapter->sge;
  1276. return (is_T2(adapter) ? ((sge->ptimeout * 1000) / HZ) : 0);
  1277. }