gt96100eth.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  1. /*
  2. * Copyright 2000, 2001 MontaVista Software Inc.
  3. * Author: MontaVista Software, Inc.
  4. * stevel@mvista.com or source@mvista.com
  5. *
  6. * This program is free software; you can distribute it and/or modify it
  7. * under the terms of the GNU General Public License (Version 2) as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
  18. *
  19. * Ethernet driver for the MIPS GT96100 Advanced Communication Controller.
  20. *
  21. * Revision history
  22. *
  23. * 11.11.2001 Moved to 2.4.14, ppopov@mvista.com. Modified driver to add
  24. * proper gt96100A support.
  25. * 12.05.2001 Moved eth port 0 to irq 3 (mapped to GT_SERINT0 on EV96100A)
  26. * in order for both ports to work. Also cleaned up boot
  27. * option support (mac address string parsing), fleshed out
  28. * gt96100_cleanup_module(), and other general code cleanups
  29. * <stevel@mvista.com>.
  30. */
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. #include <linux/string.h>
  34. #include <linux/timer.h>
  35. #include <linux/errno.h>
  36. #include <linux/in.h>
  37. #include <linux/ioport.h>
  38. #include <linux/slab.h>
  39. #include <linux/interrupt.h>
  40. #include <linux/pci.h>
  41. #include <linux/init.h>
  42. #include <linux/netdevice.h>
  43. #include <linux/etherdevice.h>
  44. #include <linux/skbuff.h>
  45. #include <linux/delay.h>
  46. #include <linux/ctype.h>
  47. #include <linux/bitops.h>
  48. #include <asm/irq.h>
  49. #include <asm/io.h>
  50. #define DESC_BE 1
  51. #define DESC_DATA_BE 1
  52. #define GT96100_DEBUG 2
  53. #include "gt96100eth.h"
  54. // prototypes
  55. static void* dmaalloc(size_t size, dma_addr_t *dma_handle);
  56. static void dmafree(size_t size, void *vaddr);
  57. static void gt96100_delay(int msec);
  58. static int gt96100_add_hash_entry(struct net_device *dev,
  59. unsigned char* addr);
  60. static void read_mib_counters(struct gt96100_private *gp);
  61. static int read_MII(int phy_addr, u32 reg);
  62. static int write_MII(int phy_addr, u32 reg, u16 data);
  63. static int gt96100_init_module(void);
  64. static void gt96100_cleanup_module(void);
  65. static void dump_MII(int dbg_lvl, struct net_device *dev);
  66. static void dump_tx_desc(int dbg_lvl, struct net_device *dev, int i);
  67. static void dump_rx_desc(int dbg_lvl, struct net_device *dev, int i);
  68. static void dump_skb(int dbg_lvl, struct net_device *dev,
  69. struct sk_buff *skb);
  70. static void dump_hw_addr(int dbg_lvl, struct net_device *dev,
  71. const char* pfx, unsigned char* addr_str);
  72. static void update_stats(struct gt96100_private *gp);
  73. static void abort(struct net_device *dev, u32 abort_bits);
  74. static void hard_stop(struct net_device *dev);
  75. static void enable_ether_irq(struct net_device *dev);
  76. static void disable_ether_irq(struct net_device *dev);
  77. static int gt96100_probe1(struct pci_dev *pci, int port_num);
  78. static void reset_tx(struct net_device *dev);
  79. static void reset_rx(struct net_device *dev);
  80. static int gt96100_check_tx_consistent(struct gt96100_private *gp);
  81. static int gt96100_init(struct net_device *dev);
  82. static int gt96100_open(struct net_device *dev);
  83. static int gt96100_close(struct net_device *dev);
  84. static int gt96100_tx(struct sk_buff *skb, struct net_device *dev);
  85. static int gt96100_rx(struct net_device *dev, u32 status);
  86. static irqreturn_t gt96100_interrupt(int irq, void *dev_id, struct pt_regs *regs);
  87. static void gt96100_tx_timeout(struct net_device *dev);
  88. static void gt96100_set_rx_mode(struct net_device *dev);
  89. static struct net_device_stats* gt96100_get_stats(struct net_device *dev);
  90. extern char * __init prom_getcmdline(void);
  91. static int max_interrupt_work = 32;
  92. #define nibswap(x) ((((x) >> 4) & 0x0f) | (((x) << 4) & 0xf0))
  93. #define RUN_AT(x) (jiffies + (x))
  94. // For reading/writing 32-bit words and half-words from/to DMA memory
  95. #ifdef DESC_BE
  96. #define cpu_to_dma32 cpu_to_be32
  97. #define dma32_to_cpu be32_to_cpu
  98. #define cpu_to_dma16 cpu_to_be16
  99. #define dma16_to_cpu be16_to_cpu
  100. #else
  101. #define cpu_to_dma32 cpu_to_le32
  102. #define dma32_to_cpu le32_to_cpu
  103. #define cpu_to_dma16 cpu_to_le16
  104. #define dma16_to_cpu le16_to_cpu
  105. #endif
  106. static char mac0[18] = "00.02.03.04.05.06";
  107. static char mac1[18] = "00.01.02.03.04.05";
  108. MODULE_PARM(mac0, "c18");
  109. MODULE_PARM(mac1, "c18");
  110. MODULE_PARM_DESC(mac0, "MAC address for GT96100 ethernet port 0");
  111. MODULE_PARM_DESC(mac1, "MAC address for GT96100 ethernet port 1");
  112. /*
  113. * Info for the GT96100 ethernet controller's ports.
  114. */
  115. static struct gt96100_if_t {
  116. struct net_device *dev;
  117. unsigned int iobase; // IO Base address of this port
  118. int irq; // IRQ number of this port
  119. char *mac_str;
  120. } gt96100_iflist[NUM_INTERFACES] = {
  121. {
  122. NULL,
  123. GT96100_ETH0_BASE, GT96100_ETHER0_IRQ,
  124. mac0
  125. },
  126. {
  127. NULL,
  128. GT96100_ETH1_BASE, GT96100_ETHER1_IRQ,
  129. mac1
  130. }
  131. };
  132. static inline const char*
  133. chip_name(int chip_rev)
  134. {
  135. switch (chip_rev) {
  136. case REV_GT96100:
  137. return "GT96100";
  138. case REV_GT96100A_1:
  139. case REV_GT96100A:
  140. return "GT96100A";
  141. default:
  142. return "Unknown GT96100";
  143. }
  144. }
  145. /*
  146. DMA memory allocation, derived from pci_alloc_consistent.
  147. */
  148. static void * dmaalloc(size_t size, dma_addr_t *dma_handle)
  149. {
  150. void *ret;
  151. ret = (void *)__get_free_pages(GFP_ATOMIC | GFP_DMA, get_order(size));
  152. if (ret != NULL) {
  153. dma_cache_inv((unsigned long)ret, size);
  154. if (dma_handle != NULL)
  155. *dma_handle = virt_to_phys(ret);
  156. /* bump virtual address up to non-cached area */
  157. ret = (void*)KSEG1ADDR(ret);
  158. }
  159. return ret;
  160. }
  161. static void dmafree(size_t size, void *vaddr)
  162. {
  163. vaddr = (void*)KSEG0ADDR(vaddr);
  164. free_pages((unsigned long)vaddr, get_order(size));
  165. }
  166. static void gt96100_delay(int ms)
  167. {
  168. if (in_interrupt())
  169. return;
  170. else
  171. msleep_interruptible(ms);
  172. }
  173. static int
  174. parse_mac_addr(struct net_device *dev, char* macstr)
  175. {
  176. int i, j;
  177. unsigned char result, value;
  178. for (i=0; i<6; i++) {
  179. result = 0;
  180. if (i != 5 && *(macstr+2) != '.') {
  181. err(__FILE__ "invalid mac address format: %d %c\n",
  182. i, *(macstr+2));
  183. return -EINVAL;
  184. }
  185. for (j=0; j<2; j++) {
  186. if (isxdigit(*macstr) &&
  187. (value = isdigit(*macstr) ? *macstr-'0' :
  188. toupper(*macstr)-'A'+10) < 16) {
  189. result = result*16 + value;
  190. macstr++;
  191. } else {
  192. err(__FILE__ "invalid mac address "
  193. "character: %c\n", *macstr);
  194. return -EINVAL;
  195. }
  196. }
  197. macstr++; // step over '.'
  198. dev->dev_addr[i] = result;
  199. }
  200. return 0;
  201. }
  202. static int
  203. read_MII(int phy_addr, u32 reg)
  204. {
  205. int timedout = 20;
  206. u32 smir = smirOpCode | (phy_addr << smirPhyAdBit) |
  207. (reg << smirRegAdBit);
  208. // wait for last operation to complete
  209. while (GT96100_READ(GT96100_ETH_SMI_REG) & smirBusy) {
  210. // snooze for 1 msec and check again
  211. gt96100_delay(1);
  212. if (--timedout == 0) {
  213. printk(KERN_ERR "%s: busy timeout!!\n", __FUNCTION__);
  214. return -ENODEV;
  215. }
  216. }
  217. GT96100_WRITE(GT96100_ETH_SMI_REG, smir);
  218. timedout = 20;
  219. // wait for read to complete
  220. while (!((smir = GT96100_READ(GT96100_ETH_SMI_REG)) & smirReadValid)) {
  221. // snooze for 1 msec and check again
  222. gt96100_delay(1);
  223. if (--timedout == 0) {
  224. printk(KERN_ERR "%s: timeout!!\n", __FUNCTION__);
  225. return -ENODEV;
  226. }
  227. }
  228. return (int)(smir & smirDataMask);
  229. }
  230. static void
  231. dump_tx_desc(int dbg_lvl, struct net_device *dev, int i)
  232. {
  233. struct gt96100_private *gp = netdev_priv(dev);
  234. gt96100_td_t *td = &gp->tx_ring[i];
  235. dbg(dbg_lvl, "Tx descriptor at 0x%08lx:\n", virt_to_phys(td));
  236. dbg(dbg_lvl,
  237. " cmdstat=%04x, byte_cnt=%04x, buff_ptr=%04x, next=%04x\n",
  238. dma32_to_cpu(td->cmdstat),
  239. dma16_to_cpu(td->byte_cnt),
  240. dma32_to_cpu(td->buff_ptr),
  241. dma32_to_cpu(td->next));
  242. }
  243. static void
  244. dump_rx_desc(int dbg_lvl, struct net_device *dev, int i)
  245. {
  246. struct gt96100_private *gp = netdev_priv(dev);
  247. gt96100_rd_t *rd = &gp->rx_ring[i];
  248. dbg(dbg_lvl, "Rx descriptor at 0x%08lx:\n", virt_to_phys(rd));
  249. dbg(dbg_lvl, " cmdstat=%04x, buff_sz=%04x, byte_cnt=%04x, "
  250. "buff_ptr=%04x, next=%04x\n",
  251. dma32_to_cpu(rd->cmdstat),
  252. dma16_to_cpu(rd->buff_sz),
  253. dma16_to_cpu(rd->byte_cnt),
  254. dma32_to_cpu(rd->buff_ptr),
  255. dma32_to_cpu(rd->next));
  256. }
  257. static int
  258. write_MII(int phy_addr, u32 reg, u16 data)
  259. {
  260. int timedout = 20;
  261. u32 smir = (phy_addr << smirPhyAdBit) |
  262. (reg << smirRegAdBit) | data;
  263. // wait for last operation to complete
  264. while (GT96100_READ(GT96100_ETH_SMI_REG) & smirBusy) {
  265. // snooze for 1 msec and check again
  266. gt96100_delay(1);
  267. if (--timedout == 0) {
  268. printk(KERN_ERR "%s: busy timeout!!\n", __FUNCTION__);
  269. return -1;
  270. }
  271. }
  272. GT96100_WRITE(GT96100_ETH_SMI_REG, smir);
  273. return 0;
  274. }
  275. static void
  276. dump_MII(int dbg_lvl, struct net_device *dev)
  277. {
  278. int i, val;
  279. struct gt96100_private *gp = netdev_priv(dev);
  280. if (dbg_lvl <= GT96100_DEBUG) {
  281. for (i=0; i<7; i++) {
  282. if ((val = read_MII(gp->phy_addr, i)) >= 0)
  283. printk("MII Reg %d=%x\n", i, val);
  284. }
  285. for (i=16; i<21; i++) {
  286. if ((val = read_MII(gp->phy_addr, i)) >= 0)
  287. printk("MII Reg %d=%x\n", i, val);
  288. }
  289. }
  290. }
  291. static void
  292. dump_hw_addr(int dbg_lvl, struct net_device *dev, const char* pfx,
  293. unsigned char* addr_str)
  294. {
  295. int i;
  296. char buf[100], octet[5];
  297. if (dbg_lvl <= GT96100_DEBUG) {
  298. strcpy(buf, pfx);
  299. for (i = 0; i < 6; i++) {
  300. sprintf(octet, "%2.2x%s",
  301. addr_str[i], i<5 ? ":" : "\n");
  302. strcat(buf, octet);
  303. }
  304. info("%s", buf);
  305. }
  306. }
  307. static void
  308. dump_skb(int dbg_lvl, struct net_device *dev, struct sk_buff *skb)
  309. {
  310. int i;
  311. unsigned char* skbdata;
  312. if (dbg_lvl <= GT96100_DEBUG) {
  313. dbg(dbg_lvl, "%s: skb=%p, skb->data=%p, skb->len=%d\n",
  314. __FUNCTION__, skb, skb->data, skb->len);
  315. skbdata = (unsigned char*)KSEG1ADDR(skb->data);
  316. for (i=0; i<skb->len; i++) {
  317. if (!(i % 16))
  318. printk(KERN_DEBUG "\n %3.3x: %2.2x,",
  319. i, skbdata[i]);
  320. else
  321. printk(KERN_DEBUG "%2.2x,", skbdata[i]);
  322. }
  323. printk(KERN_DEBUG "\n");
  324. }
  325. }
  326. static int
  327. gt96100_add_hash_entry(struct net_device *dev, unsigned char* addr)
  328. {
  329. struct gt96100_private *gp = netdev_priv(dev);
  330. //u16 hashResult, stmp;
  331. //unsigned char ctmp, hash_ea[6];
  332. u32 tblEntry1, tblEntry0, *tblEntryAddr;
  333. int i;
  334. tblEntry1 = hteValid | hteRD;
  335. tblEntry1 |= (u32)addr[5] << 3;
  336. tblEntry1 |= (u32)addr[4] << 11;
  337. tblEntry1 |= (u32)addr[3] << 19;
  338. tblEntry1 |= ((u32)addr[2] & 0x1f) << 27;
  339. dbg(3, "%s: tblEntry1=%x\n", __FUNCTION__, tblEntry1);
  340. tblEntry0 = ((u32)addr[2] >> 5) & 0x07;
  341. tblEntry0 |= (u32)addr[1] << 3;
  342. tblEntry0 |= (u32)addr[0] << 11;
  343. dbg(3, "%s: tblEntry0=%x\n", __FUNCTION__, tblEntry0);
  344. #if 0
  345. for (i=0; i<6; i++) {
  346. // nibble swap
  347. ctmp = nibswap(addr[i]);
  348. // invert every nibble
  349. hash_ea[i] = ((ctmp&1)<<3) | ((ctmp&8)>>3) |
  350. ((ctmp&2)<<1) | ((ctmp&4)>>1);
  351. hash_ea[i] |= ((ctmp&0x10)<<3) | ((ctmp&0x80)>>3) |
  352. ((ctmp&0x20)<<1) | ((ctmp&0x40)>>1);
  353. }
  354. dump_hw_addr(3, dev, "%s: nib swap/invt addr=", __FUNCTION__, hash_ea);
  355. if (gp->hash_mode == 0) {
  356. hashResult = ((u16)hash_ea[0] & 0xfc) << 7;
  357. stmp = ((u16)hash_ea[0] & 0x03) |
  358. (((u16)hash_ea[1] & 0x7f) << 2);
  359. stmp ^= (((u16)hash_ea[1] >> 7) & 0x01) |
  360. ((u16)hash_ea[2] << 1);
  361. stmp ^= (u16)hash_ea[3] | (((u16)hash_ea[4] & 1) << 8);
  362. hashResult |= stmp;
  363. } else {
  364. return -1; // don't support hash mode 1
  365. }
  366. dbg(3, "%s: hashResult=%x\n", __FUNCTION__, hashResult);
  367. tblEntryAddr =
  368. (u32 *)(&gp->hash_table[((u32)hashResult & 0x7ff) << 3]);
  369. dbg(3, "%s: tblEntryAddr=%p\n", tblEntryAddr, __FUNCTION__);
  370. for (i=0; i<HASH_HOP_NUMBER; i++) {
  371. if ((*tblEntryAddr & hteValid) &&
  372. !(*tblEntryAddr & hteSkip)) {
  373. // This entry is already occupied, go to next entry
  374. tblEntryAddr += 2;
  375. dbg(3, "%s: skipping to %p\n", __FUNCTION__,
  376. tblEntryAddr);
  377. } else {
  378. memset(tblEntryAddr, 0, 8);
  379. tblEntryAddr[1] = cpu_to_dma32(tblEntry1);
  380. tblEntryAddr[0] = cpu_to_dma32(tblEntry0);
  381. break;
  382. }
  383. }
  384. if (i >= HASH_HOP_NUMBER) {
  385. err("%s: expired!\n", __FUNCTION__);
  386. return -1; // Couldn't find an unused entry
  387. }
  388. #else
  389. tblEntryAddr = (u32 *)gp->hash_table;
  390. for (i=0; i<RX_HASH_TABLE_SIZE/4; i+=2) {
  391. tblEntryAddr[i+1] = cpu_to_dma32(tblEntry1);
  392. tblEntryAddr[i] = cpu_to_dma32(tblEntry0);
  393. }
  394. #endif
  395. return 0;
  396. }
  397. static void
  398. read_mib_counters(struct gt96100_private *gp)
  399. {
  400. u32* mib_regs = (u32*)&gp->mib;
  401. int i;
  402. for (i=0; i<sizeof(mib_counters_t)/sizeof(u32); i++)
  403. mib_regs[i] = GT96100ETH_READ(gp, GT96100_ETH_MIB_COUNT_BASE +
  404. i*sizeof(u32));
  405. }
  406. static void
  407. update_stats(struct gt96100_private *gp)
  408. {
  409. mib_counters_t *mib = &gp->mib;
  410. struct net_device_stats *stats = &gp->stats;
  411. read_mib_counters(gp);
  412. stats->rx_packets = mib->totalFramesReceived;
  413. stats->tx_packets = mib->framesSent;
  414. stats->rx_bytes = mib->totalByteReceived;
  415. stats->tx_bytes = mib->byteSent;
  416. stats->rx_errors = mib->totalFramesReceived - mib->framesReceived;
  417. //the tx error counters are incremented by the ISR
  418. //rx_dropped incremented by gt96100_rx
  419. //tx_dropped incremented by gt96100_tx
  420. stats->multicast = mib->multicastFramesReceived;
  421. // collisions incremented by gt96100_tx_complete
  422. stats->rx_length_errors = mib->oversizeFrames + mib->fragments;
  423. // The RxError condition means the Rx DMA encountered a
  424. // CPU owned descriptor, which, if things are working as
  425. // they should, means the Rx ring has overflowed.
  426. stats->rx_over_errors = mib->macRxError;
  427. stats->rx_crc_errors = mib->cRCError;
  428. }
  429. static void
  430. abort(struct net_device *dev, u32 abort_bits)
  431. {
  432. struct gt96100_private *gp = netdev_priv(dev);
  433. int timedout = 100; // wait up to 100 msec for hard stop to complete
  434. dbg(3, "%s\n", __FUNCTION__);
  435. // Return if neither Rx or Tx abort bits are set
  436. if (!(abort_bits & (sdcmrAR | sdcmrAT)))
  437. return;
  438. // make sure only the Rx/Tx abort bits are set
  439. abort_bits &= (sdcmrAR | sdcmrAT);
  440. spin_lock(&gp->lock);
  441. // abort any Rx/Tx DMA immediately
  442. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, abort_bits);
  443. dbg(3, "%s: SDMA comm = %x\n", __FUNCTION__,
  444. GT96100ETH_READ(gp, GT96100_ETH_SDMA_COMM));
  445. // wait for abort to complete
  446. while (GT96100ETH_READ(gp, GT96100_ETH_SDMA_COMM) & abort_bits) {
  447. // snooze for 1 msec and check again
  448. gt96100_delay(1);
  449. if (--timedout == 0) {
  450. err("%s: timeout!!\n", __FUNCTION__);
  451. break;
  452. }
  453. }
  454. spin_unlock(&gp->lock);
  455. }
  456. static void
  457. hard_stop(struct net_device *dev)
  458. {
  459. struct gt96100_private *gp = netdev_priv(dev);
  460. dbg(3, "%s\n", __FUNCTION__);
  461. disable_ether_irq(dev);
  462. abort(dev, sdcmrAR | sdcmrAT);
  463. // disable port
  464. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG, 0);
  465. }
  466. static void
  467. enable_ether_irq(struct net_device *dev)
  468. {
  469. struct gt96100_private *gp = netdev_priv(dev);
  470. u32 intMask;
  471. /*
  472. * route ethernet interrupt to GT_SERINT0 for port 0,
  473. * GT_INT0 for port 1.
  474. */
  475. int intr_mask_reg = (gp->port_num == 0) ?
  476. GT96100_SERINT0_MASK : GT96100_INT0_HIGH_MASK;
  477. if (gp->chip_rev >= REV_GT96100A_1) {
  478. intMask = icrTxBufferLow | icrTxEndLow |
  479. icrTxErrorLow | icrRxOVR | icrTxUdr |
  480. icrRxBufferQ0 | icrRxErrorQ0 |
  481. icrMIIPhySTC | icrEtherIntSum;
  482. }
  483. else {
  484. intMask = icrTxBufferLow | icrTxEndLow |
  485. icrTxErrorLow | icrRxOVR | icrTxUdr |
  486. icrRxBuffer | icrRxError |
  487. icrMIIPhySTC | icrEtherIntSum;
  488. }
  489. // unmask interrupts
  490. GT96100ETH_WRITE(gp, GT96100_ETH_INT_MASK, intMask);
  491. intMask = GT96100_READ(intr_mask_reg);
  492. intMask |= 1<<gp->port_num;
  493. GT96100_WRITE(intr_mask_reg, intMask);
  494. }
  495. static void
  496. disable_ether_irq(struct net_device *dev)
  497. {
  498. struct gt96100_private *gp = netdev_priv(dev);
  499. u32 intMask;
  500. int intr_mask_reg = (gp->port_num == 0) ?
  501. GT96100_SERINT0_MASK : GT96100_INT0_HIGH_MASK;
  502. intMask = GT96100_READ(intr_mask_reg);
  503. intMask &= ~(1<<gp->port_num);
  504. GT96100_WRITE(intr_mask_reg, intMask);
  505. GT96100ETH_WRITE(gp, GT96100_ETH_INT_MASK, 0);
  506. }
  507. /*
  508. * Init GT96100 ethernet controller driver
  509. */
  510. static int gt96100_init_module(void)
  511. {
  512. struct pci_dev *pci;
  513. int i, retval=0;
  514. u32 cpuConfig;
  515. /*
  516. * Stupid probe because this really isn't a PCI device
  517. */
  518. if (!(pci = pci_find_device(PCI_VENDOR_ID_MARVELL,
  519. PCI_DEVICE_ID_MARVELL_GT96100, NULL)) &&
  520. !(pci = pci_find_device(PCI_VENDOR_ID_MARVELL,
  521. PCI_DEVICE_ID_MARVELL_GT96100A, NULL))) {
  522. printk(KERN_ERR __FILE__ ": GT96100 not found!\n");
  523. return -ENODEV;
  524. }
  525. cpuConfig = GT96100_READ(GT96100_CPU_INTERF_CONFIG);
  526. if (cpuConfig & (1<<12)) {
  527. printk(KERN_ERR __FILE__
  528. ": must be in Big Endian mode!\n");
  529. return -ENODEV;
  530. }
  531. for (i=0; i < NUM_INTERFACES; i++)
  532. retval |= gt96100_probe1(pci, i);
  533. return retval;
  534. }
  535. static int __init gt96100_probe1(struct pci_dev *pci, int port_num)
  536. {
  537. struct gt96100_private *gp = NULL;
  538. struct gt96100_if_t *gtif = &gt96100_iflist[port_num];
  539. int phy_addr, phy_id1, phy_id2;
  540. u32 phyAD;
  541. int retval;
  542. unsigned char chip_rev;
  543. struct net_device *dev = NULL;
  544. if (gtif->irq < 0) {
  545. printk(KERN_ERR "%s: irq unknown - probing not supported\n",
  546. __FUNCTION__);
  547. return -ENODEV;
  548. }
  549. pci_read_config_byte(pci, PCI_REVISION_ID, &chip_rev);
  550. if (chip_rev >= REV_GT96100A_1) {
  551. phyAD = GT96100_READ(GT96100_ETH_PHY_ADDR_REG);
  552. phy_addr = (phyAD >> (5*port_num)) & 0x1f;
  553. } else {
  554. /*
  555. * not sure what's this about -- probably a gt bug
  556. */
  557. phy_addr = port_num;
  558. phyAD = GT96100_READ(GT96100_ETH_PHY_ADDR_REG);
  559. phyAD &= ~(0x1f << (port_num*5));
  560. phyAD |= phy_addr << (port_num*5);
  561. GT96100_WRITE(GT96100_ETH_PHY_ADDR_REG, phyAD);
  562. }
  563. // probe for the external PHY
  564. if ((phy_id1 = read_MII(phy_addr, 2)) <= 0 ||
  565. (phy_id2 = read_MII(phy_addr, 3)) <= 0) {
  566. printk(KERN_ERR "%s: no PHY found on MII%d\n", __FUNCTION__, port_num);
  567. return -ENODEV;
  568. }
  569. if (!request_region(gtif->iobase, GT96100_ETH_IO_SIZE, "GT96100ETH")) {
  570. printk(KERN_ERR "%s: request_region failed\n", __FUNCTION__);
  571. return -EBUSY;
  572. }
  573. dev = alloc_etherdev(sizeof(struct gt96100_private));
  574. if (!dev)
  575. goto out;
  576. gtif->dev = dev;
  577. /* private struct aligned and zeroed by alloc_etherdev */
  578. /* Fill in the 'dev' fields. */
  579. dev->base_addr = gtif->iobase;
  580. dev->irq = gtif->irq;
  581. if ((retval = parse_mac_addr(dev, gtif->mac_str))) {
  582. err("%s: MAC address parse failed\n", __FUNCTION__);
  583. retval = -EINVAL;
  584. goto out1;
  585. }
  586. gp = netdev_priv(dev);
  587. memset(gp, 0, sizeof(*gp)); // clear it
  588. gp->port_num = port_num;
  589. gp->io_size = GT96100_ETH_IO_SIZE;
  590. gp->port_offset = port_num * GT96100_ETH_IO_SIZE;
  591. gp->phy_addr = phy_addr;
  592. gp->chip_rev = chip_rev;
  593. info("%s found at 0x%x, irq %d\n",
  594. chip_name(gp->chip_rev), gtif->iobase, gtif->irq);
  595. dump_hw_addr(0, dev, "HW Address ", dev->dev_addr);
  596. info("%s chip revision=%d\n", chip_name(gp->chip_rev), gp->chip_rev);
  597. info("%s ethernet port %d\n", chip_name(gp->chip_rev), gp->port_num);
  598. info("external PHY ID1=0x%04x, ID2=0x%04x\n", phy_id1, phy_id2);
  599. // Allocate Rx and Tx descriptor rings
  600. if (gp->rx_ring == NULL) {
  601. // All descriptors in ring must be 16-byte aligned
  602. gp->rx_ring = dmaalloc(sizeof(gt96100_rd_t) * RX_RING_SIZE
  603. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  604. &gp->rx_ring_dma);
  605. if (gp->rx_ring == NULL) {
  606. retval = -ENOMEM;
  607. goto out1;
  608. }
  609. gp->tx_ring = (gt96100_td_t *)(gp->rx_ring + RX_RING_SIZE);
  610. gp->tx_ring_dma =
  611. gp->rx_ring_dma + sizeof(gt96100_rd_t) * RX_RING_SIZE;
  612. }
  613. // Allocate the Rx Data Buffers
  614. if (gp->rx_buff == NULL) {
  615. gp->rx_buff = dmaalloc(PKT_BUF_SZ*RX_RING_SIZE,
  616. &gp->rx_buff_dma);
  617. if (gp->rx_buff == NULL) {
  618. retval = -ENOMEM;
  619. goto out2;
  620. }
  621. }
  622. dbg(3, "%s: rx_ring=%p, tx_ring=%p\n", __FUNCTION__,
  623. gp->rx_ring, gp->tx_ring);
  624. // Allocate Rx Hash Table
  625. if (gp->hash_table == NULL) {
  626. gp->hash_table = (char*)dmaalloc(RX_HASH_TABLE_SIZE,
  627. &gp->hash_table_dma);
  628. if (gp->hash_table == NULL) {
  629. retval = -ENOMEM;
  630. goto out3;
  631. }
  632. }
  633. dbg(3, "%s: hash=%p\n", __FUNCTION__, gp->hash_table);
  634. spin_lock_init(&gp->lock);
  635. dev->open = gt96100_open;
  636. dev->hard_start_xmit = gt96100_tx;
  637. dev->stop = gt96100_close;
  638. dev->get_stats = gt96100_get_stats;
  639. //dev->do_ioctl = gt96100_ioctl;
  640. dev->set_multicast_list = gt96100_set_rx_mode;
  641. dev->tx_timeout = gt96100_tx_timeout;
  642. dev->watchdog_timeo = GT96100ETH_TX_TIMEOUT;
  643. retval = register_netdev(dev);
  644. if (retval)
  645. goto out4;
  646. return 0;
  647. out4:
  648. dmafree(RX_HASH_TABLE_SIZE, gp->hash_table_dma);
  649. out3:
  650. dmafree(PKT_BUF_SZ*RX_RING_SIZE, gp->rx_buff);
  651. out2:
  652. dmafree(sizeof(gt96100_rd_t) * RX_RING_SIZE
  653. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  654. gp->rx_ring);
  655. out1:
  656. free_netdev (dev);
  657. out:
  658. release_region(gtif->iobase, GT96100_ETH_IO_SIZE);
  659. err("%s failed. Returns %d\n", __FUNCTION__, retval);
  660. return retval;
  661. }
  662. static void
  663. reset_tx(struct net_device *dev)
  664. {
  665. struct gt96100_private *gp = netdev_priv(dev);
  666. int i;
  667. abort(dev, sdcmrAT);
  668. for (i=0; i<TX_RING_SIZE; i++) {
  669. if (gp->tx_skbuff[i]) {
  670. if (in_interrupt())
  671. dev_kfree_skb_irq(gp->tx_skbuff[i]);
  672. else
  673. dev_kfree_skb(gp->tx_skbuff[i]);
  674. gp->tx_skbuff[i] = NULL;
  675. }
  676. gp->tx_ring[i].cmdstat = 0; // CPU owns
  677. gp->tx_ring[i].byte_cnt = 0;
  678. gp->tx_ring[i].buff_ptr = 0;
  679. gp->tx_ring[i].next =
  680. cpu_to_dma32(gp->tx_ring_dma +
  681. sizeof(gt96100_td_t) * (i+1));
  682. dump_tx_desc(4, dev, i);
  683. }
  684. /* Wrap the ring. */
  685. gp->tx_ring[i-1].next = cpu_to_dma32(gp->tx_ring_dma);
  686. // setup only the lowest priority TxCDP reg
  687. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_TX_DESC_PTR0, gp->tx_ring_dma);
  688. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_TX_DESC_PTR1, 0);
  689. // init Tx indeces and pkt counter
  690. gp->tx_next_in = gp->tx_next_out = 0;
  691. gp->tx_count = 0;
  692. }
  693. static void
  694. reset_rx(struct net_device *dev)
  695. {
  696. struct gt96100_private *gp = netdev_priv(dev);
  697. int i;
  698. abort(dev, sdcmrAR);
  699. for (i=0; i<RX_RING_SIZE; i++) {
  700. gp->rx_ring[i].next =
  701. cpu_to_dma32(gp->rx_ring_dma +
  702. sizeof(gt96100_rd_t) * (i+1));
  703. gp->rx_ring[i].buff_ptr =
  704. cpu_to_dma32(gp->rx_buff_dma + i*PKT_BUF_SZ);
  705. gp->rx_ring[i].buff_sz = cpu_to_dma16(PKT_BUF_SZ);
  706. // Give ownership to device, set first and last, enable intr
  707. gp->rx_ring[i].cmdstat =
  708. cpu_to_dma32((u32)(rxFirst | rxLast | rxOwn | rxEI));
  709. dump_rx_desc(4, dev, i);
  710. }
  711. /* Wrap the ring. */
  712. gp->rx_ring[i-1].next = cpu_to_dma32(gp->rx_ring_dma);
  713. // Setup only the lowest priority RxFDP and RxCDP regs
  714. for (i=0; i<4; i++) {
  715. if (i == 0) {
  716. GT96100ETH_WRITE(gp, GT96100_ETH_1ST_RX_DESC_PTR0,
  717. gp->rx_ring_dma);
  718. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_RX_DESC_PTR0,
  719. gp->rx_ring_dma);
  720. } else {
  721. GT96100ETH_WRITE(gp,
  722. GT96100_ETH_1ST_RX_DESC_PTR0 + i*4,
  723. 0);
  724. GT96100ETH_WRITE(gp,
  725. GT96100_ETH_CURR_RX_DESC_PTR0 + i*4,
  726. 0);
  727. }
  728. }
  729. // init Rx NextOut index
  730. gp->rx_next_out = 0;
  731. }
  732. // Returns 1 if the Tx counter and indeces don't gel
  733. static int
  734. gt96100_check_tx_consistent(struct gt96100_private *gp)
  735. {
  736. int diff = gp->tx_next_in - gp->tx_next_out;
  737. diff = diff<0 ? TX_RING_SIZE + diff : diff;
  738. diff = gp->tx_count == TX_RING_SIZE ? diff + TX_RING_SIZE : diff;
  739. return (diff != gp->tx_count);
  740. }
  741. static int
  742. gt96100_init(struct net_device *dev)
  743. {
  744. struct gt96100_private *gp = netdev_priv(dev);
  745. u32 tmp;
  746. u16 mii_reg;
  747. dbg(3, "%s: dev=%p\n", __FUNCTION__, dev);
  748. dbg(3, "%s: scs10_lo=%4x, scs10_hi=%4x\n", __FUNCTION__,
  749. GT96100_READ(0x8), GT96100_READ(0x10));
  750. dbg(3, "%s: scs32_lo=%4x, scs32_hi=%4x\n", __FUNCTION__,
  751. GT96100_READ(0x18), GT96100_READ(0x20));
  752. // Stop and disable Port
  753. hard_stop(dev);
  754. // Setup CIU Arbiter
  755. tmp = GT96100_READ(GT96100_CIU_ARBITER_CONFIG);
  756. tmp |= (0x0c << (gp->port_num*2)); // set Ether DMA req priority to hi
  757. #ifndef DESC_BE
  758. tmp &= ~(1<<31); // set desc endianess to little
  759. #else
  760. tmp |= (1<<31);
  761. #endif
  762. GT96100_WRITE(GT96100_CIU_ARBITER_CONFIG, tmp);
  763. dbg(3, "%s: CIU Config=%x/%x\n", __FUNCTION__,
  764. tmp, GT96100_READ(GT96100_CIU_ARBITER_CONFIG));
  765. // Set routing.
  766. tmp = GT96100_READ(GT96100_ROUTE_MAIN) & (0x3f << 18);
  767. tmp |= (0x07 << (18 + gp->port_num*3));
  768. GT96100_WRITE(GT96100_ROUTE_MAIN, tmp);
  769. /* set MII as peripheral func */
  770. tmp = GT96100_READ(GT96100_GPP_CONFIG2);
  771. tmp |= 0x7fff << (gp->port_num*16);
  772. GT96100_WRITE(GT96100_GPP_CONFIG2, tmp);
  773. /* Set up MII port pin directions */
  774. tmp = GT96100_READ(GT96100_GPP_IO2);
  775. tmp |= 0x003d << (gp->port_num*16);
  776. GT96100_WRITE(GT96100_GPP_IO2, tmp);
  777. // Set-up hash table
  778. memset(gp->hash_table, 0, RX_HASH_TABLE_SIZE); // clear it
  779. gp->hash_mode = 0;
  780. // Add a single entry to hash table - our ethernet address
  781. gt96100_add_hash_entry(dev, dev->dev_addr);
  782. // Set-up DMA ptr to hash table
  783. GT96100ETH_WRITE(gp, GT96100_ETH_HASH_TBL_PTR, gp->hash_table_dma);
  784. dbg(3, "%s: Hash Tbl Ptr=%x\n", __FUNCTION__,
  785. GT96100ETH_READ(gp, GT96100_ETH_HASH_TBL_PTR));
  786. // Setup Tx
  787. reset_tx(dev);
  788. dbg(3, "%s: Curr Tx Desc Ptr0=%x\n", __FUNCTION__,
  789. GT96100ETH_READ(gp, GT96100_ETH_CURR_TX_DESC_PTR0));
  790. // Setup Rx
  791. reset_rx(dev);
  792. dbg(3, "%s: 1st/Curr Rx Desc Ptr0=%x/%x\n", __FUNCTION__,
  793. GT96100ETH_READ(gp, GT96100_ETH_1ST_RX_DESC_PTR0),
  794. GT96100ETH_READ(gp, GT96100_ETH_CURR_RX_DESC_PTR0));
  795. // eth port config register
  796. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  797. pcxrFCTL | pcxrFCTLen | pcxrFLP | pcxrDPLXen);
  798. mii_reg = read_MII(gp->phy_addr, 0x11); /* int enable register */
  799. mii_reg |= 2; /* enable mii interrupt */
  800. write_MII(gp->phy_addr, 0x11, mii_reg);
  801. dbg(3, "%s: PhyAD=%x\n", __FUNCTION__,
  802. GT96100_READ(GT96100_ETH_PHY_ADDR_REG));
  803. // setup DMA
  804. // We want the Rx/Tx DMA to write/read data to/from memory in
  805. // Big Endian mode. Also set DMA Burst Size to 8 64Bit words.
  806. #ifdef DESC_DATA_BE
  807. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_CONFIG,
  808. (0xf<<sdcrRCBit) | sdcrRIFB | (3<<sdcrBSZBit));
  809. #else
  810. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_CONFIG,
  811. sdcrBLMR | sdcrBLMT |
  812. (0xf<<sdcrRCBit) | sdcrRIFB | (3<<sdcrBSZBit));
  813. #endif
  814. dbg(3, "%s: SDMA Config=%x\n", __FUNCTION__,
  815. GT96100ETH_READ(gp, GT96100_ETH_SDMA_CONFIG));
  816. // start Rx DMA
  817. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, sdcmrERD);
  818. dbg(3, "%s: SDMA Comm=%x\n", __FUNCTION__,
  819. GT96100ETH_READ(gp, GT96100_ETH_SDMA_COMM));
  820. // enable this port (set hash size to 1/2K)
  821. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG, pcrEN | pcrHS);
  822. dbg(3, "%s: Port Config=%x\n", __FUNCTION__,
  823. GT96100ETH_READ(gp, GT96100_ETH_PORT_CONFIG));
  824. /*
  825. * Disable all Type-of-Service queueing. All Rx packets will be
  826. * treated normally and will be sent to the lowest priority
  827. * queue.
  828. *
  829. * Disable flow-control for now. FIXME: support flow control?
  830. */
  831. // clear all the MIB ctr regs
  832. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  833. pcxrFCTL | pcxrFCTLen | pcxrFLP |
  834. pcxrPRIOrxOverride);
  835. read_mib_counters(gp);
  836. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  837. pcxrFCTL | pcxrFCTLen | pcxrFLP |
  838. pcxrPRIOrxOverride | pcxrMIBclrMode);
  839. dbg(3, "%s: Port Config Ext=%x\n", __FUNCTION__,
  840. GT96100ETH_READ(gp, GT96100_ETH_PORT_CONFIG_EXT));
  841. netif_start_queue(dev);
  842. dump_MII(4, dev);
  843. // enable interrupts
  844. enable_ether_irq(dev);
  845. // we should now be receiving frames
  846. return 0;
  847. }
  848. static int
  849. gt96100_open(struct net_device *dev)
  850. {
  851. int retval;
  852. dbg(2, "%s: dev=%p\n", __FUNCTION__, dev);
  853. // Initialize and startup the GT-96100 ethernet port
  854. if ((retval = gt96100_init(dev))) {
  855. err("error in gt96100_init\n");
  856. free_irq(dev->irq, dev);
  857. return retval;
  858. }
  859. if ((retval = request_irq(dev->irq, &gt96100_interrupt,
  860. SA_SHIRQ, dev->name, dev))) {
  861. err("unable to get IRQ %d\n", dev->irq);
  862. return retval;
  863. }
  864. dbg(2, "%s: Initialization done.\n", __FUNCTION__);
  865. return 0;
  866. }
  867. static int
  868. gt96100_close(struct net_device *dev)
  869. {
  870. dbg(3, "%s: dev=%p\n", __FUNCTION__, dev);
  871. // stop the device
  872. if (netif_device_present(dev)) {
  873. netif_stop_queue(dev);
  874. hard_stop(dev);
  875. }
  876. free_irq(dev->irq, dev);
  877. return 0;
  878. }
  879. static int
  880. gt96100_tx(struct sk_buff *skb, struct net_device *dev)
  881. {
  882. struct gt96100_private *gp = netdev_priv(dev);
  883. unsigned long flags;
  884. int nextIn;
  885. spin_lock_irqsave(&gp->lock, flags);
  886. nextIn = gp->tx_next_in;
  887. dbg(3, "%s: nextIn=%d\n", __FUNCTION__, nextIn);
  888. if (gp->tx_count >= TX_RING_SIZE) {
  889. warn("Tx Ring full, pkt dropped.\n");
  890. gp->stats.tx_dropped++;
  891. spin_unlock_irqrestore(&gp->lock, flags);
  892. return 1;
  893. }
  894. if (!(gp->last_psr & psrLink)) {
  895. err("%s: Link down, pkt dropped.\n", __FUNCTION__);
  896. gp->stats.tx_dropped++;
  897. spin_unlock_irqrestore(&gp->lock, flags);
  898. return 1;
  899. }
  900. if (dma32_to_cpu(gp->tx_ring[nextIn].cmdstat) & txOwn) {
  901. err("%s: device owns descriptor, pkt dropped.\n", __FUNCTION__);
  902. gp->stats.tx_dropped++;
  903. // stop the queue, so Tx timeout can fix it
  904. netif_stop_queue(dev);
  905. spin_unlock_irqrestore(&gp->lock, flags);
  906. return 1;
  907. }
  908. // Prepare the Descriptor at tx_next_in
  909. gp->tx_skbuff[nextIn] = skb;
  910. gp->tx_ring[nextIn].byte_cnt = cpu_to_dma16(skb->len);
  911. gp->tx_ring[nextIn].buff_ptr = cpu_to_dma32(virt_to_phys(skb->data));
  912. // make sure packet gets written back to memory
  913. dma_cache_wback_inv((unsigned long)(skb->data), skb->len);
  914. // Give ownership to device, set first and last desc, enable interrupt
  915. // Setting of ownership bit must be *last*!
  916. gp->tx_ring[nextIn].cmdstat =
  917. cpu_to_dma32((u32)(txOwn | txGenCRC | txEI |
  918. txPad | txFirst | txLast));
  919. dump_tx_desc(4, dev, nextIn);
  920. dump_skb(4, dev, skb);
  921. // increment tx_next_in with wrap
  922. gp->tx_next_in = (nextIn + 1) % TX_RING_SIZE;
  923. // If DMA is stopped, restart
  924. if (!(GT96100ETH_READ(gp, GT96100_ETH_PORT_STATUS) & psrTxLow))
  925. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM,
  926. sdcmrERD | sdcmrTXDL);
  927. // increment count and stop queue if full
  928. if (++gp->tx_count == TX_RING_SIZE) {
  929. gp->tx_full = 1;
  930. netif_stop_queue(dev);
  931. dbg(2, "Tx Ring now full, queue stopped.\n");
  932. }
  933. dev->trans_start = jiffies;
  934. spin_unlock_irqrestore(&gp->lock, flags);
  935. return 0;
  936. }
  937. static int
  938. gt96100_rx(struct net_device *dev, u32 status)
  939. {
  940. struct gt96100_private *gp = netdev_priv(dev);
  941. struct sk_buff *skb;
  942. int pkt_len, nextOut, cdp;
  943. gt96100_rd_t *rd;
  944. u32 cmdstat;
  945. dbg(3, "%s: dev=%p, status=%x\n", __FUNCTION__, dev, status);
  946. cdp = (GT96100ETH_READ(gp, GT96100_ETH_1ST_RX_DESC_PTR0)
  947. - gp->rx_ring_dma) / sizeof(gt96100_rd_t);
  948. // Continue until we reach 1st descriptor pointer
  949. for (nextOut = gp->rx_next_out; nextOut != cdp;
  950. nextOut = (nextOut + 1) % RX_RING_SIZE) {
  951. if (--gp->intr_work_done == 0)
  952. break;
  953. rd = &gp->rx_ring[nextOut];
  954. cmdstat = dma32_to_cpu(rd->cmdstat);
  955. dbg(4, "%s: Rx desc cmdstat=%x, nextOut=%d\n", __FUNCTION__,
  956. cmdstat, nextOut);
  957. if (cmdstat & (u32)rxOwn) {
  958. //err("%s: device owns descriptor!\n", __FUNCTION__);
  959. // DMA is not finished updating descriptor???
  960. // Leave and come back later to pick-up where
  961. // we left off.
  962. break;
  963. }
  964. // Drop this received pkt if there were any errors
  965. if (((cmdstat & (u32)(rxErrorSummary)) &&
  966. (cmdstat & (u32)(rxFirst))) || (status & icrRxError)) {
  967. // update the detailed rx error counters that
  968. // are not covered by the MIB counters.
  969. if (cmdstat & (u32)rxOverrun)
  970. gp->stats.rx_fifo_errors++;
  971. cmdstat |= (u32)rxOwn;
  972. rd->cmdstat = cpu_to_dma32(cmdstat);
  973. continue;
  974. }
  975. /*
  976. * Must be first and last (ie only) descriptor of packet. We
  977. * ignore (drop) any packets that do not fit in one descriptor.
  978. * Every descriptor's receive buffer is large enough to hold
  979. * the maximum 802.3 frame size, so a multi-descriptor packet
  980. * indicates an error. Most if not all corrupted packets will
  981. * have already been dropped by the above check for the
  982. * rxErrorSummary status bit.
  983. */
  984. if (!(cmdstat & (u32)rxFirst) || !(cmdstat & (u32)rxLast)) {
  985. if (cmdstat & (u32)rxFirst) {
  986. /*
  987. * This is the first descriptor of a
  988. * multi-descriptor packet. It isn't corrupted
  989. * because the above check for rxErrorSummary
  990. * would have dropped it already, so what's
  991. * the deal with this packet? Good question,
  992. * let's dump it out.
  993. */
  994. err("%s: desc not first and last!\n", __FUNCTION__);
  995. dump_rx_desc(0, dev, nextOut);
  996. }
  997. cmdstat |= (u32)rxOwn;
  998. rd->cmdstat = cpu_to_dma32(cmdstat);
  999. // continue to drop every descriptor of this packet
  1000. continue;
  1001. }
  1002. pkt_len = dma16_to_cpu(rd->byte_cnt);
  1003. /* Create new skb. */
  1004. skb = dev_alloc_skb(pkt_len+2);
  1005. if (skb == NULL) {
  1006. err("%s: Memory squeeze, dropping packet.\n", __FUNCTION__);
  1007. gp->stats.rx_dropped++;
  1008. cmdstat |= (u32)rxOwn;
  1009. rd->cmdstat = cpu_to_dma32(cmdstat);
  1010. continue;
  1011. }
  1012. skb->dev = dev;
  1013. skb_reserve(skb, 2); /* 16 byte IP header align */
  1014. memcpy(skb_put(skb, pkt_len),
  1015. &gp->rx_buff[nextOut*PKT_BUF_SZ], pkt_len);
  1016. skb->protocol = eth_type_trans(skb, dev);
  1017. dump_skb(4, dev, skb);
  1018. netif_rx(skb); /* pass the packet to upper layers */
  1019. dev->last_rx = jiffies;
  1020. // now we can release ownership of this desc back to device
  1021. cmdstat |= (u32)rxOwn;
  1022. rd->cmdstat = cpu_to_dma32(cmdstat);
  1023. }
  1024. if (nextOut == gp->rx_next_out)
  1025. dbg(3, "%s: RxCDP did not increment?\n", __FUNCTION__);
  1026. gp->rx_next_out = nextOut;
  1027. return 0;
  1028. }
  1029. static void
  1030. gt96100_tx_complete(struct net_device *dev, u32 status)
  1031. {
  1032. struct gt96100_private *gp = netdev_priv(dev);
  1033. int nextOut, cdp;
  1034. gt96100_td_t *td;
  1035. u32 cmdstat;
  1036. cdp = (GT96100ETH_READ(gp, GT96100_ETH_CURR_TX_DESC_PTR0)
  1037. - gp->tx_ring_dma) / sizeof(gt96100_td_t);
  1038. // Continue until we reach the current descriptor pointer
  1039. for (nextOut = gp->tx_next_out; nextOut != cdp;
  1040. nextOut = (nextOut + 1) % TX_RING_SIZE) {
  1041. if (--gp->intr_work_done == 0)
  1042. break;
  1043. td = &gp->tx_ring[nextOut];
  1044. cmdstat = dma32_to_cpu(td->cmdstat);
  1045. dbg(3, "%s: Tx desc cmdstat=%x, nextOut=%d\n", __FUNCTION__,
  1046. cmdstat, nextOut);
  1047. if (cmdstat & (u32)txOwn) {
  1048. /*
  1049. * DMA is not finished writing descriptor???
  1050. * Leave and come back later to pick-up where
  1051. * we left off.
  1052. */
  1053. break;
  1054. }
  1055. // increment Tx error stats
  1056. if (cmdstat & (u32)txErrorSummary) {
  1057. dbg(2, "%s: Tx error, cmdstat = %x\n", __FUNCTION__,
  1058. cmdstat);
  1059. gp->stats.tx_errors++;
  1060. if (cmdstat & (u32)txReTxLimit)
  1061. gp->stats.tx_aborted_errors++;
  1062. if (cmdstat & (u32)txUnderrun)
  1063. gp->stats.tx_fifo_errors++;
  1064. if (cmdstat & (u32)txLateCollision)
  1065. gp->stats.tx_window_errors++;
  1066. }
  1067. if (cmdstat & (u32)txCollision)
  1068. gp->stats.collisions +=
  1069. (u32)((cmdstat & txReTxCntMask) >>
  1070. txReTxCntBit);
  1071. // Wake the queue if the ring was full
  1072. if (gp->tx_full) {
  1073. gp->tx_full = 0;
  1074. if (gp->last_psr & psrLink) {
  1075. netif_wake_queue(dev);
  1076. dbg(2, "%s: Tx Ring was full, queue waked\n",
  1077. __FUNCTION__);
  1078. }
  1079. }
  1080. // decrement tx ring buffer count
  1081. if (gp->tx_count) gp->tx_count--;
  1082. // free the skb
  1083. if (gp->tx_skbuff[nextOut]) {
  1084. dbg(3, "%s: good Tx, skb=%p\n", __FUNCTION__,
  1085. gp->tx_skbuff[nextOut]);
  1086. dev_kfree_skb_irq(gp->tx_skbuff[nextOut]);
  1087. gp->tx_skbuff[nextOut] = NULL;
  1088. } else {
  1089. err("%s: no skb!\n", __FUNCTION__);
  1090. }
  1091. }
  1092. gp->tx_next_out = nextOut;
  1093. if (gt96100_check_tx_consistent(gp)) {
  1094. err("%s: Tx queue inconsistent!\n", __FUNCTION__);
  1095. }
  1096. if ((status & icrTxEndLow) && gp->tx_count != 0) {
  1097. // we must restart the DMA
  1098. dbg(3, "%s: Restarting Tx DMA\n", __FUNCTION__);
  1099. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM,
  1100. sdcmrERD | sdcmrTXDL);
  1101. }
  1102. }
  1103. static irqreturn_t
  1104. gt96100_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1105. {
  1106. struct net_device *dev = (struct net_device *)dev_id;
  1107. struct gt96100_private *gp = netdev_priv(dev);
  1108. u32 status;
  1109. int handled = 0;
  1110. if (dev == NULL) {
  1111. err("%s: null dev ptr\n", __FUNCTION__);
  1112. return IRQ_NONE;
  1113. }
  1114. dbg(3, "%s: entry, icr=%x\n", __FUNCTION__,
  1115. GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE));
  1116. spin_lock(&gp->lock);
  1117. gp->intr_work_done = max_interrupt_work;
  1118. while (gp->intr_work_done > 0) {
  1119. status = GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE);
  1120. // ACK interrupts
  1121. GT96100ETH_WRITE(gp, GT96100_ETH_INT_CAUSE, ~status);
  1122. if ((status & icrEtherIntSum) == 0 &&
  1123. !(status & (icrTxBufferLow|icrTxBufferHigh|icrRxBuffer)))
  1124. break;
  1125. handled = 1;
  1126. if (status & icrMIIPhySTC) {
  1127. u32 psr = GT96100ETH_READ(gp, GT96100_ETH_PORT_STATUS);
  1128. if (gp->last_psr != psr) {
  1129. dbg(0, "port status:\n");
  1130. dbg(0, " %s MBit/s, %s-duplex, "
  1131. "flow-control %s, link is %s,\n",
  1132. psr & psrSpeed ? "100":"10",
  1133. psr & psrDuplex ? "full":"half",
  1134. psr & psrFctl ? "disabled":"enabled",
  1135. psr & psrLink ? "up":"down");
  1136. dbg(0, " TxLowQ is %s, TxHighQ is %s, "
  1137. "Transmitter is %s\n",
  1138. psr & psrTxLow ? "running":"stopped",
  1139. psr & psrTxHigh ? "running":"stopped",
  1140. psr & psrTxInProg ? "on":"off");
  1141. if ((psr & psrLink) && !gp->tx_full &&
  1142. netif_queue_stopped(dev)) {
  1143. dbg(0, "%s: Link up, waking queue.\n",
  1144. __FUNCTION__);
  1145. netif_wake_queue(dev);
  1146. } else if (!(psr & psrLink) &&
  1147. !netif_queue_stopped(dev)) {
  1148. dbg(0, "%s: Link down, stopping queue.\n",
  1149. __FUNCTION__);
  1150. netif_stop_queue(dev);
  1151. }
  1152. gp->last_psr = psr;
  1153. }
  1154. if (--gp->intr_work_done == 0)
  1155. break;
  1156. }
  1157. if (status & (icrTxBufferLow | icrTxEndLow))
  1158. gt96100_tx_complete(dev, status);
  1159. if (status & (icrRxBuffer | icrRxError)) {
  1160. gt96100_rx(dev, status);
  1161. }
  1162. // Now check TX errors (RX errors were handled in gt96100_rx)
  1163. if (status & icrTxErrorLow) {
  1164. err("%s: Tx resource error\n", __FUNCTION__);
  1165. if (--gp->intr_work_done == 0)
  1166. break;
  1167. }
  1168. if (status & icrTxUdr) {
  1169. err("%s: Tx underrun error\n", __FUNCTION__);
  1170. if (--gp->intr_work_done == 0)
  1171. break;
  1172. }
  1173. }
  1174. if (gp->intr_work_done == 0) {
  1175. // ACK any remaining pending interrupts
  1176. GT96100ETH_WRITE(gp, GT96100_ETH_INT_CAUSE, 0);
  1177. dbg(3, "%s: hit max work\n", __FUNCTION__);
  1178. }
  1179. dbg(3, "%s: exit, icr=%x\n", __FUNCTION__,
  1180. GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE));
  1181. spin_unlock(&gp->lock);
  1182. return IRQ_RETVAL(handled);
  1183. }
  1184. static void
  1185. gt96100_tx_timeout(struct net_device *dev)
  1186. {
  1187. struct gt96100_private *gp = netdev_priv(dev);
  1188. unsigned long flags;
  1189. spin_lock_irqsave(&gp->lock, flags);
  1190. if (!(gp->last_psr & psrLink)) {
  1191. err("tx_timeout: link down.\n");
  1192. spin_unlock_irqrestore(&gp->lock, flags);
  1193. } else {
  1194. if (gt96100_check_tx_consistent(gp))
  1195. err("tx_timeout: Tx ring error.\n");
  1196. disable_ether_irq(dev);
  1197. spin_unlock_irqrestore(&gp->lock, flags);
  1198. reset_tx(dev);
  1199. enable_ether_irq(dev);
  1200. netif_wake_queue(dev);
  1201. }
  1202. }
  1203. static void
  1204. gt96100_set_rx_mode(struct net_device *dev)
  1205. {
  1206. struct gt96100_private *gp = netdev_priv(dev);
  1207. unsigned long flags;
  1208. //struct dev_mc_list *mcptr;
  1209. dbg(3, "%s: dev=%p, flags=%x\n", __FUNCTION__, dev, dev->flags);
  1210. // stop the Receiver DMA
  1211. abort(dev, sdcmrAR);
  1212. spin_lock_irqsave(&gp->lock, flags);
  1213. if (dev->flags & IFF_PROMISC) {
  1214. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG,
  1215. pcrEN | pcrHS | pcrPM);
  1216. }
  1217. #if 0
  1218. /*
  1219. FIXME: currently multicast doesn't work - need to get hash table
  1220. working first.
  1221. */
  1222. if (dev->mc_count) {
  1223. // clear hash table
  1224. memset(gp->hash_table, 0, RX_HASH_TABLE_SIZE);
  1225. // Add our ethernet address
  1226. gt96100_add_hash_entry(dev, dev->dev_addr);
  1227. for (mcptr = dev->mc_list; mcptr; mcptr = mcptr->next) {
  1228. dump_hw_addr(2, dev, __FUNCTION__ ": addr=",
  1229. mcptr->dmi_addr);
  1230. gt96100_add_hash_entry(dev, mcptr->dmi_addr);
  1231. }
  1232. }
  1233. #endif
  1234. // restart Rx DMA
  1235. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, sdcmrERD);
  1236. spin_unlock_irqrestore(&gp->lock, flags);
  1237. }
  1238. static struct net_device_stats *
  1239. gt96100_get_stats(struct net_device *dev)
  1240. {
  1241. struct gt96100_private *gp = netdev_priv(dev);
  1242. unsigned long flags;
  1243. dbg(3, "%s: dev=%p\n", __FUNCTION__, dev);
  1244. if (netif_device_present(dev)) {
  1245. spin_lock_irqsave (&gp->lock, flags);
  1246. update_stats(gp);
  1247. spin_unlock_irqrestore (&gp->lock, flags);
  1248. }
  1249. return &gp->stats;
  1250. }
  1251. static void gt96100_cleanup_module(void)
  1252. {
  1253. int i;
  1254. for (i=0; i<NUM_INTERFACES; i++) {
  1255. struct gt96100_if_t *gtif = &gt96100_iflist[i];
  1256. if (gtif->dev != NULL) {
  1257. struct gt96100_private *gp = (struct gt96100_private *)
  1258. netdev_priv(gtif->dev);
  1259. unregister_netdev(gtif->dev);
  1260. dmafree(RX_HASH_TABLE_SIZE, gp->hash_table_dma);
  1261. dmafree(PKT_BUF_SZ*RX_RING_SIZE, gp->rx_buff);
  1262. dmafree(sizeof(gt96100_rd_t) * RX_RING_SIZE
  1263. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  1264. gp->rx_ring);
  1265. free_netdev(gtif->dev);
  1266. release_region(gtif->iobase, gp->io_size);
  1267. }
  1268. }
  1269. }
  1270. static int __init gt96100_setup(char *options)
  1271. {
  1272. char *this_opt;
  1273. if (!options || !*options)
  1274. return 0;
  1275. while ((this_opt = strsep (&options, ",")) != NULL) {
  1276. if (!*this_opt)
  1277. continue;
  1278. if (!strncmp(this_opt, "mac0:", 5)) {
  1279. memcpy(mac0, this_opt+5, 17);
  1280. mac0[17]= '\0';
  1281. } else if (!strncmp(this_opt, "mac1:", 5)) {
  1282. memcpy(mac1, this_opt+5, 17);
  1283. mac1[17]= '\0';
  1284. }
  1285. }
  1286. return 1;
  1287. }
  1288. __setup("gt96100eth=", gt96100_setup);
  1289. module_init(gt96100_init_module);
  1290. module_exit(gt96100_cleanup_module);
  1291. MODULE_AUTHOR("Steve Longerbeam <stevel@mvista.com>");
  1292. MODULE_DESCRIPTION("GT96100 Ethernet driver");