gt96100eth.c 40 KB

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