gt96100eth.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567
  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->io_size = GT96100_ETH_IO_SIZE;
  588. gp->port_offset = port_num * GT96100_ETH_IO_SIZE;
  589. gp->phy_addr = phy_addr;
  590. gp->chip_rev = chip_rev;
  591. info("%s found at 0x%x, irq %d\n",
  592. chip_name(gp->chip_rev), gtif->iobase, gtif->irq);
  593. dump_hw_addr(0, dev, "%s: HW Address ", __FUNCTION__, dev->dev_addr);
  594. info("%s chip revision=%d\n", chip_name(gp->chip_rev), gp->chip_rev);
  595. info("%s ethernet port %d\n", chip_name(gp->chip_rev), gp->port_num);
  596. info("external PHY ID1=0x%04x, ID2=0x%04x\n", phy_id1, phy_id2);
  597. // Allocate Rx and Tx descriptor rings
  598. if (gp->rx_ring == NULL) {
  599. // All descriptors in ring must be 16-byte aligned
  600. gp->rx_ring = dmaalloc(sizeof(gt96100_rd_t) * RX_RING_SIZE
  601. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  602. &gp->rx_ring_dma);
  603. if (gp->rx_ring == NULL) {
  604. retval = -ENOMEM;
  605. goto out1;
  606. }
  607. gp->tx_ring = (gt96100_td_t *)(gp->rx_ring + RX_RING_SIZE);
  608. gp->tx_ring_dma =
  609. gp->rx_ring_dma + sizeof(gt96100_rd_t) * RX_RING_SIZE;
  610. }
  611. // Allocate the Rx Data Buffers
  612. if (gp->rx_buff == NULL) {
  613. gp->rx_buff = dmaalloc(PKT_BUF_SZ*RX_RING_SIZE,
  614. &gp->rx_buff_dma);
  615. if (gp->rx_buff == NULL) {
  616. retval = -ENOMEM;
  617. goto out2;
  618. }
  619. }
  620. dbg(3, "%s: rx_ring=%p, tx_ring=%p\n", __FUNCTION__,
  621. gp->rx_ring, gp->tx_ring);
  622. // Allocate Rx Hash Table
  623. if (gp->hash_table == NULL) {
  624. gp->hash_table = (char*)dmaalloc(RX_HASH_TABLE_SIZE,
  625. &gp->hash_table_dma);
  626. if (gp->hash_table == NULL) {
  627. retval = -ENOMEM;
  628. goto out3;
  629. }
  630. }
  631. dbg(3, "%s: hash=%p\n", __FUNCTION__, gp->hash_table);
  632. spin_lock_init(&gp->lock);
  633. dev->open = gt96100_open;
  634. dev->hard_start_xmit = gt96100_tx;
  635. dev->stop = gt96100_close;
  636. dev->get_stats = gt96100_get_stats;
  637. //dev->do_ioctl = gt96100_ioctl;
  638. dev->set_multicast_list = gt96100_set_rx_mode;
  639. dev->tx_timeout = gt96100_tx_timeout;
  640. dev->watchdog_timeo = GT96100ETH_TX_TIMEOUT;
  641. retval = register_netdev(dev);
  642. if (retval)
  643. goto out4;
  644. return 0;
  645. out4:
  646. dmafree(RX_HASH_TABLE_SIZE, gp->hash_table_dma);
  647. out3:
  648. dmafree(PKT_BUF_SZ*RX_RING_SIZE, gp->rx_buff);
  649. out2:
  650. dmafree(sizeof(gt96100_rd_t) * RX_RING_SIZE
  651. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  652. gp->rx_ring);
  653. out1:
  654. free_netdev (dev);
  655. out:
  656. release_region(gtif->iobase, GT96100_ETH_IO_SIZE);
  657. err("%s failed. Returns %d\n", __FUNCTION__, retval);
  658. return retval;
  659. }
  660. static void
  661. reset_tx(struct net_device *dev)
  662. {
  663. struct gt96100_private *gp = netdev_priv(dev);
  664. int i;
  665. abort(dev, sdcmrAT);
  666. for (i=0; i<TX_RING_SIZE; i++) {
  667. if (gp->tx_skbuff[i]) {
  668. if (in_interrupt())
  669. dev_kfree_skb_irq(gp->tx_skbuff[i]);
  670. else
  671. dev_kfree_skb(gp->tx_skbuff[i]);
  672. gp->tx_skbuff[i] = NULL;
  673. }
  674. gp->tx_ring[i].cmdstat = 0; // CPU owns
  675. gp->tx_ring[i].byte_cnt = 0;
  676. gp->tx_ring[i].buff_ptr = 0;
  677. gp->tx_ring[i].next =
  678. cpu_to_dma32(gp->tx_ring_dma +
  679. sizeof(gt96100_td_t) * (i+1));
  680. dump_tx_desc(4, dev, i);
  681. }
  682. /* Wrap the ring. */
  683. gp->tx_ring[i-1].next = cpu_to_dma32(gp->tx_ring_dma);
  684. // setup only the lowest priority TxCDP reg
  685. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_TX_DESC_PTR0, gp->tx_ring_dma);
  686. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_TX_DESC_PTR1, 0);
  687. // init Tx indeces and pkt counter
  688. gp->tx_next_in = gp->tx_next_out = 0;
  689. gp->tx_count = 0;
  690. }
  691. static void
  692. reset_rx(struct net_device *dev)
  693. {
  694. struct gt96100_private *gp = netdev_priv(dev);
  695. int i;
  696. abort(dev, sdcmrAR);
  697. for (i=0; i<RX_RING_SIZE; i++) {
  698. gp->rx_ring[i].next =
  699. cpu_to_dma32(gp->rx_ring_dma +
  700. sizeof(gt96100_rd_t) * (i+1));
  701. gp->rx_ring[i].buff_ptr =
  702. cpu_to_dma32(gp->rx_buff_dma + i*PKT_BUF_SZ);
  703. gp->rx_ring[i].buff_sz = cpu_to_dma16(PKT_BUF_SZ);
  704. // Give ownership to device, set first and last, enable intr
  705. gp->rx_ring[i].cmdstat =
  706. cpu_to_dma32((u32)(rxFirst | rxLast | rxOwn | rxEI));
  707. dump_rx_desc(4, dev, i);
  708. }
  709. /* Wrap the ring. */
  710. gp->rx_ring[i-1].next = cpu_to_dma32(gp->rx_ring_dma);
  711. // Setup only the lowest priority RxFDP and RxCDP regs
  712. for (i=0; i<4; i++) {
  713. if (i == 0) {
  714. GT96100ETH_WRITE(gp, GT96100_ETH_1ST_RX_DESC_PTR0,
  715. gp->rx_ring_dma);
  716. GT96100ETH_WRITE(gp, GT96100_ETH_CURR_RX_DESC_PTR0,
  717. gp->rx_ring_dma);
  718. } else {
  719. GT96100ETH_WRITE(gp,
  720. GT96100_ETH_1ST_RX_DESC_PTR0 + i*4,
  721. 0);
  722. GT96100ETH_WRITE(gp,
  723. GT96100_ETH_CURR_RX_DESC_PTR0 + i*4,
  724. 0);
  725. }
  726. }
  727. // init Rx NextOut index
  728. gp->rx_next_out = 0;
  729. }
  730. // Returns 1 if the Tx counter and indeces don't gel
  731. static int
  732. gt96100_check_tx_consistent(struct gt96100_private *gp)
  733. {
  734. int diff = gp->tx_next_in - gp->tx_next_out;
  735. diff = diff<0 ? TX_RING_SIZE + diff : diff;
  736. diff = gp->tx_count == TX_RING_SIZE ? diff + TX_RING_SIZE : diff;
  737. return (diff != gp->tx_count);
  738. }
  739. static int
  740. gt96100_init(struct net_device *dev)
  741. {
  742. struct gt96100_private *gp = netdev_priv(dev);
  743. u32 tmp;
  744. u16 mii_reg;
  745. dbg(3, "%s: dev=%p\n", __FUNCTION__, dev);
  746. dbg(3, "%s: scs10_lo=%4x, scs10_hi=%4x\n", __FUNCTION__,
  747. GT96100_READ(0x8), GT96100_READ(0x10));
  748. dbg(3, "%s: scs32_lo=%4x, scs32_hi=%4x\n", __FUNCTION__,
  749. GT96100_READ(0x18), GT96100_READ(0x20));
  750. // Stop and disable Port
  751. hard_stop(dev);
  752. // Setup CIU Arbiter
  753. tmp = GT96100_READ(GT96100_CIU_ARBITER_CONFIG);
  754. tmp |= (0x0c << (gp->port_num*2)); // set Ether DMA req priority to hi
  755. #ifndef DESC_BE
  756. tmp &= ~(1<<31); // set desc endianess to little
  757. #else
  758. tmp |= (1<<31);
  759. #endif
  760. GT96100_WRITE(GT96100_CIU_ARBITER_CONFIG, tmp);
  761. dbg(3, "%s: CIU Config=%x/%x\n", __FUNCTION__,
  762. tmp, GT96100_READ(GT96100_CIU_ARBITER_CONFIG));
  763. // Set routing.
  764. tmp = GT96100_READ(GT96100_ROUTE_MAIN) & (0x3f << 18);
  765. tmp |= (0x07 << (18 + gp->port_num*3));
  766. GT96100_WRITE(GT96100_ROUTE_MAIN, tmp);
  767. /* set MII as peripheral func */
  768. tmp = GT96100_READ(GT96100_GPP_CONFIG2);
  769. tmp |= 0x7fff << (gp->port_num*16);
  770. GT96100_WRITE(GT96100_GPP_CONFIG2, tmp);
  771. /* Set up MII port pin directions */
  772. tmp = GT96100_READ(GT96100_GPP_IO2);
  773. tmp |= 0x003d << (gp->port_num*16);
  774. GT96100_WRITE(GT96100_GPP_IO2, tmp);
  775. // Set-up hash table
  776. memset(gp->hash_table, 0, RX_HASH_TABLE_SIZE); // clear it
  777. gp->hash_mode = 0;
  778. // Add a single entry to hash table - our ethernet address
  779. gt96100_add_hash_entry(dev, dev->dev_addr);
  780. // Set-up DMA ptr to hash table
  781. GT96100ETH_WRITE(gp, GT96100_ETH_HASH_TBL_PTR, gp->hash_table_dma);
  782. dbg(3, "%s: Hash Tbl Ptr=%x\n", __FUNCTION__,
  783. GT96100ETH_READ(gp, GT96100_ETH_HASH_TBL_PTR));
  784. // Setup Tx
  785. reset_tx(dev);
  786. dbg(3, "%s: Curr Tx Desc Ptr0=%x\n", __FUNCTION__,
  787. GT96100ETH_READ(gp, GT96100_ETH_CURR_TX_DESC_PTR0));
  788. // Setup Rx
  789. reset_rx(dev);
  790. dbg(3, "%s: 1st/Curr Rx Desc Ptr0=%x/%x\n", __FUNCTION__,
  791. GT96100ETH_READ(gp, GT96100_ETH_1ST_RX_DESC_PTR0),
  792. GT96100ETH_READ(gp, GT96100_ETH_CURR_RX_DESC_PTR0));
  793. // eth port config register
  794. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  795. pcxrFCTL | pcxrFCTLen | pcxrFLP | pcxrDPLXen);
  796. mii_reg = read_MII(gp->phy_addr, 0x11); /* int enable register */
  797. mii_reg |= 2; /* enable mii interrupt */
  798. write_MII(gp->phy_addr, 0x11, mii_reg);
  799. dbg(3, "%s: PhyAD=%x\n", __FUNCTION__,
  800. GT96100_READ(GT96100_ETH_PHY_ADDR_REG));
  801. // setup DMA
  802. // We want the Rx/Tx DMA to write/read data to/from memory in
  803. // Big Endian mode. Also set DMA Burst Size to 8 64Bit words.
  804. #ifdef DESC_DATA_BE
  805. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_CONFIG,
  806. (0xf<<sdcrRCBit) | sdcrRIFB | (3<<sdcrBSZBit));
  807. #else
  808. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_CONFIG,
  809. sdcrBLMR | sdcrBLMT |
  810. (0xf<<sdcrRCBit) | sdcrRIFB | (3<<sdcrBSZBit));
  811. #endif
  812. dbg(3, "%s: SDMA Config=%x\n", __FUNCTION__,
  813. GT96100ETH_READ(gp, GT96100_ETH_SDMA_CONFIG));
  814. // start Rx DMA
  815. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, sdcmrERD);
  816. dbg(3, "%s: SDMA Comm=%x\n", __FUNCTION__,
  817. GT96100ETH_READ(gp, GT96100_ETH_SDMA_COMM));
  818. // enable this port (set hash size to 1/2K)
  819. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG, pcrEN | pcrHS);
  820. dbg(3, "%s: Port Config=%x\n", __FUNCTION__,
  821. GT96100ETH_READ(gp, GT96100_ETH_PORT_CONFIG));
  822. /*
  823. * Disable all Type-of-Service queueing. All Rx packets will be
  824. * treated normally and will be sent to the lowest priority
  825. * queue.
  826. *
  827. * Disable flow-control for now. FIXME: support flow control?
  828. */
  829. // clear all the MIB ctr regs
  830. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  831. pcxrFCTL | pcxrFCTLen | pcxrFLP |
  832. pcxrPRIOrxOverride);
  833. read_mib_counters(gp);
  834. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG_EXT,
  835. pcxrFCTL | pcxrFCTLen | pcxrFLP |
  836. pcxrPRIOrxOverride | pcxrMIBclrMode);
  837. dbg(3, "%s: Port Config Ext=%x\n", __FUNCTION__,
  838. GT96100ETH_READ(gp, GT96100_ETH_PORT_CONFIG_EXT));
  839. netif_start_queue(dev);
  840. dump_MII(4, dev);
  841. // enable interrupts
  842. enable_ether_irq(dev);
  843. // we should now be receiving frames
  844. return 0;
  845. }
  846. static int
  847. gt96100_open(struct net_device *dev)
  848. {
  849. int retval;
  850. dbg(2, "%s: dev=%p\n", __FUNCTION__, dev);
  851. // Initialize and startup the GT-96100 ethernet port
  852. if ((retval = gt96100_init(dev))) {
  853. err("error in gt96100_init\n");
  854. free_irq(dev->irq, dev);
  855. return retval;
  856. }
  857. if ((retval = request_irq(dev->irq, &gt96100_interrupt,
  858. SA_SHIRQ, dev->name, dev))) {
  859. err("unable to get IRQ %d\n", dev->irq);
  860. return retval;
  861. }
  862. dbg(2, "%s: Initialization done.\n", __FUNCTION__);
  863. return 0;
  864. }
  865. static int
  866. gt96100_close(struct net_device *dev)
  867. {
  868. dbg(3, "%s: dev=%p\n", __FUNCTION__, dev);
  869. // stop the device
  870. if (netif_device_present(dev)) {
  871. netif_stop_queue(dev);
  872. hard_stop(dev);
  873. }
  874. free_irq(dev->irq, dev);
  875. return 0;
  876. }
  877. static int
  878. gt96100_tx(struct sk_buff *skb, struct net_device *dev)
  879. {
  880. struct gt96100_private *gp = netdev_priv(dev);
  881. unsigned long flags;
  882. int nextIn;
  883. spin_lock_irqsave(&gp->lock, flags);
  884. nextIn = gp->tx_next_in;
  885. dbg(3, "%s: nextIn=%d\n", __FUNCTION__, nextIn);
  886. if (gp->tx_count >= TX_RING_SIZE) {
  887. warn("Tx Ring full, pkt dropped.\n");
  888. gp->stats.tx_dropped++;
  889. spin_unlock_irqrestore(&gp->lock, flags);
  890. return 1;
  891. }
  892. if (!(gp->last_psr & psrLink)) {
  893. err("%s: Link down, pkt dropped.\n", __FUNCTION__);
  894. gp->stats.tx_dropped++;
  895. spin_unlock_irqrestore(&gp->lock, flags);
  896. return 1;
  897. }
  898. if (dma32_to_cpu(gp->tx_ring[nextIn].cmdstat) & txOwn) {
  899. err("%s: device owns descriptor, pkt dropped.\n", __FUNCTION__);
  900. gp->stats.tx_dropped++;
  901. // stop the queue, so Tx timeout can fix it
  902. netif_stop_queue(dev);
  903. spin_unlock_irqrestore(&gp->lock, flags);
  904. return 1;
  905. }
  906. // Prepare the Descriptor at tx_next_in
  907. gp->tx_skbuff[nextIn] = skb;
  908. gp->tx_ring[nextIn].byte_cnt = cpu_to_dma16(skb->len);
  909. gp->tx_ring[nextIn].buff_ptr = cpu_to_dma32(virt_to_phys(skb->data));
  910. // make sure packet gets written back to memory
  911. dma_cache_wback_inv((unsigned long)(skb->data), skb->len);
  912. // Give ownership to device, set first and last desc, enable interrupt
  913. // Setting of ownership bit must be *last*!
  914. gp->tx_ring[nextIn].cmdstat =
  915. cpu_to_dma32((u32)(txOwn | txGenCRC | txEI |
  916. txPad | txFirst | txLast));
  917. dump_tx_desc(4, dev, nextIn);
  918. dump_skb(4, dev, skb);
  919. // increment tx_next_in with wrap
  920. gp->tx_next_in = (nextIn + 1) % TX_RING_SIZE;
  921. // If DMA is stopped, restart
  922. if (!(GT96100ETH_READ(gp, GT96100_ETH_PORT_STATUS) & psrTxLow))
  923. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM,
  924. sdcmrERD | sdcmrTXDL);
  925. // increment count and stop queue if full
  926. if (++gp->tx_count == TX_RING_SIZE) {
  927. gp->tx_full = 1;
  928. netif_stop_queue(dev);
  929. dbg(2, "Tx Ring now full, queue stopped.\n");
  930. }
  931. dev->trans_start = jiffies;
  932. spin_unlock_irqrestore(&gp->lock, flags);
  933. return 0;
  934. }
  935. static int
  936. gt96100_rx(struct net_device *dev, u32 status)
  937. {
  938. struct gt96100_private *gp = netdev_priv(dev);
  939. struct sk_buff *skb;
  940. int pkt_len, nextOut, cdp;
  941. gt96100_rd_t *rd;
  942. u32 cmdstat;
  943. dbg(3, "%s: dev=%p, status=%x\n", __FUNCTION__, dev, status);
  944. cdp = (GT96100ETH_READ(gp, GT96100_ETH_1ST_RX_DESC_PTR0)
  945. - gp->rx_ring_dma) / sizeof(gt96100_rd_t);
  946. // Continue until we reach 1st descriptor pointer
  947. for (nextOut = gp->rx_next_out; nextOut != cdp;
  948. nextOut = (nextOut + 1) % RX_RING_SIZE) {
  949. if (--gp->intr_work_done == 0)
  950. break;
  951. rd = &gp->rx_ring[nextOut];
  952. cmdstat = dma32_to_cpu(rd->cmdstat);
  953. dbg(4, "%s: Rx desc cmdstat=%x, nextOut=%d\n", __FUNCTION__,
  954. cmdstat, nextOut);
  955. if (cmdstat & (u32)rxOwn) {
  956. //err("%s: device owns descriptor!\n", __FUNCTION__);
  957. // DMA is not finished updating descriptor???
  958. // Leave and come back later to pick-up where
  959. // we left off.
  960. break;
  961. }
  962. // Drop this received pkt if there were any errors
  963. if (((cmdstat & (u32)(rxErrorSummary)) &&
  964. (cmdstat & (u32)(rxFirst))) || (status & icrRxError)) {
  965. // update the detailed rx error counters that
  966. // are not covered by the MIB counters.
  967. if (cmdstat & (u32)rxOverrun)
  968. gp->stats.rx_fifo_errors++;
  969. cmdstat |= (u32)rxOwn;
  970. rd->cmdstat = cpu_to_dma32(cmdstat);
  971. continue;
  972. }
  973. /*
  974. * Must be first and last (ie only) descriptor of packet. We
  975. * ignore (drop) any packets that do not fit in one descriptor.
  976. * Every descriptor's receive buffer is large enough to hold
  977. * the maximum 802.3 frame size, so a multi-descriptor packet
  978. * indicates an error. Most if not all corrupted packets will
  979. * have already been dropped by the above check for the
  980. * rxErrorSummary status bit.
  981. */
  982. if (!(cmdstat & (u32)rxFirst) || !(cmdstat & (u32)rxLast)) {
  983. if (cmdstat & (u32)rxFirst) {
  984. /*
  985. * This is the first descriptor of a
  986. * multi-descriptor packet. It isn't corrupted
  987. * because the above check for rxErrorSummary
  988. * would have dropped it already, so what's
  989. * the deal with this packet? Good question,
  990. * let's dump it out.
  991. */
  992. err("%s: desc not first and last!\n", __FUNCTION__);
  993. dump_rx_desc(0, dev, nextOut);
  994. }
  995. cmdstat |= (u32)rxOwn;
  996. rd->cmdstat = cpu_to_dma32(cmdstat);
  997. // continue to drop every descriptor of this packet
  998. continue;
  999. }
  1000. pkt_len = dma16_to_cpu(rd->byte_cnt);
  1001. /* Create new skb. */
  1002. skb = dev_alloc_skb(pkt_len+2);
  1003. if (skb == NULL) {
  1004. err("%s: Memory squeeze, dropping packet.\n", __FUNCTION__);
  1005. gp->stats.rx_dropped++;
  1006. cmdstat |= (u32)rxOwn;
  1007. rd->cmdstat = cpu_to_dma32(cmdstat);
  1008. continue;
  1009. }
  1010. skb->dev = dev;
  1011. skb_reserve(skb, 2); /* 16 byte IP header align */
  1012. memcpy(skb_put(skb, pkt_len),
  1013. &gp->rx_buff[nextOut*PKT_BUF_SZ], pkt_len);
  1014. skb->protocol = eth_type_trans(skb, dev);
  1015. dump_skb(4, dev, skb);
  1016. netif_rx(skb); /* pass the packet to upper layers */
  1017. dev->last_rx = jiffies;
  1018. // now we can release ownership of this desc back to device
  1019. cmdstat |= (u32)rxOwn;
  1020. rd->cmdstat = cpu_to_dma32(cmdstat);
  1021. }
  1022. if (nextOut == gp->rx_next_out)
  1023. dbg(3, "%s: RxCDP did not increment?\n", __FUNCTION__);
  1024. gp->rx_next_out = nextOut;
  1025. return 0;
  1026. }
  1027. static void
  1028. gt96100_tx_complete(struct net_device *dev, u32 status)
  1029. {
  1030. struct gt96100_private *gp = netdev_priv(dev);
  1031. int nextOut, cdp;
  1032. gt96100_td_t *td;
  1033. u32 cmdstat;
  1034. cdp = (GT96100ETH_READ(gp, GT96100_ETH_CURR_TX_DESC_PTR0)
  1035. - gp->tx_ring_dma) / sizeof(gt96100_td_t);
  1036. // Continue until we reach the current descriptor pointer
  1037. for (nextOut = gp->tx_next_out; nextOut != cdp;
  1038. nextOut = (nextOut + 1) % TX_RING_SIZE) {
  1039. if (--gp->intr_work_done == 0)
  1040. break;
  1041. td = &gp->tx_ring[nextOut];
  1042. cmdstat = dma32_to_cpu(td->cmdstat);
  1043. dbg(3, "%s: Tx desc cmdstat=%x, nextOut=%d\n", __FUNCTION__,
  1044. cmdstat, nextOut);
  1045. if (cmdstat & (u32)txOwn) {
  1046. /*
  1047. * DMA is not finished writing descriptor???
  1048. * Leave and come back later to pick-up where
  1049. * we left off.
  1050. */
  1051. break;
  1052. }
  1053. // increment Tx error stats
  1054. if (cmdstat & (u32)txErrorSummary) {
  1055. dbg(2, "%s: Tx error, cmdstat = %x\n", __FUNCTION__,
  1056. cmdstat);
  1057. gp->stats.tx_errors++;
  1058. if (cmdstat & (u32)txReTxLimit)
  1059. gp->stats.tx_aborted_errors++;
  1060. if (cmdstat & (u32)txUnderrun)
  1061. gp->stats.tx_fifo_errors++;
  1062. if (cmdstat & (u32)txLateCollision)
  1063. gp->stats.tx_window_errors++;
  1064. }
  1065. if (cmdstat & (u32)txCollision)
  1066. gp->stats.collisions +=
  1067. (u32)((cmdstat & txReTxCntMask) >>
  1068. txReTxCntBit);
  1069. // Wake the queue if the ring was full
  1070. if (gp->tx_full) {
  1071. gp->tx_full = 0;
  1072. if (gp->last_psr & psrLink) {
  1073. netif_wake_queue(dev);
  1074. dbg(2, "%s: Tx Ring was full, queue waked\n",
  1075. __FUNCTION__);
  1076. }
  1077. }
  1078. // decrement tx ring buffer count
  1079. if (gp->tx_count) gp->tx_count--;
  1080. // free the skb
  1081. if (gp->tx_skbuff[nextOut]) {
  1082. dbg(3, "%s: good Tx, skb=%p\n", __FUNCTION__,
  1083. gp->tx_skbuff[nextOut]);
  1084. dev_kfree_skb_irq(gp->tx_skbuff[nextOut]);
  1085. gp->tx_skbuff[nextOut] = NULL;
  1086. } else {
  1087. err("%s: no skb!\n", __FUNCTION__);
  1088. }
  1089. }
  1090. gp->tx_next_out = nextOut;
  1091. if (gt96100_check_tx_consistent(gp)) {
  1092. err("%s: Tx queue inconsistent!\n", __FUNCTION__);
  1093. }
  1094. if ((status & icrTxEndLow) && gp->tx_count != 0) {
  1095. // we must restart the DMA
  1096. dbg(3, "%s: Restarting Tx DMA\n", __FUNCTION__);
  1097. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM,
  1098. sdcmrERD | sdcmrTXDL);
  1099. }
  1100. }
  1101. static irqreturn_t
  1102. gt96100_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1103. {
  1104. struct net_device *dev = (struct net_device *)dev_id;
  1105. struct gt96100_private *gp = netdev_priv(dev);
  1106. u32 status;
  1107. int handled = 0;
  1108. if (dev == NULL) {
  1109. err("%s: null dev ptr\n", __FUNCTION__);
  1110. return IRQ_NONE;
  1111. }
  1112. dbg(3, "%s: entry, icr=%x\n", __FUNCTION__,
  1113. GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE));
  1114. spin_lock(&gp->lock);
  1115. gp->intr_work_done = max_interrupt_work;
  1116. while (gp->intr_work_done > 0) {
  1117. status = GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE);
  1118. // ACK interrupts
  1119. GT96100ETH_WRITE(gp, GT96100_ETH_INT_CAUSE, ~status);
  1120. if ((status & icrEtherIntSum) == 0 &&
  1121. !(status & (icrTxBufferLow|icrTxBufferHigh|icrRxBuffer)))
  1122. break;
  1123. handled = 1;
  1124. if (status & icrMIIPhySTC) {
  1125. u32 psr = GT96100ETH_READ(gp, GT96100_ETH_PORT_STATUS);
  1126. if (gp->last_psr != psr) {
  1127. dbg(0, "port status:\n");
  1128. dbg(0, " %s MBit/s, %s-duplex, "
  1129. "flow-control %s, link is %s,\n",
  1130. psr & psrSpeed ? "100":"10",
  1131. psr & psrDuplex ? "full":"half",
  1132. psr & psrFctl ? "disabled":"enabled",
  1133. psr & psrLink ? "up":"down");
  1134. dbg(0, " TxLowQ is %s, TxHighQ is %s, "
  1135. "Transmitter is %s\n",
  1136. psr & psrTxLow ? "running":"stopped",
  1137. psr & psrTxHigh ? "running":"stopped",
  1138. psr & psrTxInProg ? "on":"off");
  1139. if ((psr & psrLink) && !gp->tx_full &&
  1140. netif_queue_stopped(dev)) {
  1141. dbg(0, "%s: Link up, waking queue.\n",
  1142. __FUNCTION__);
  1143. netif_wake_queue(dev);
  1144. } else if (!(psr & psrLink) &&
  1145. !netif_queue_stopped(dev)) {
  1146. dbg(0, "%s: Link down, stopping queue.\n",
  1147. __FUNCTION__);
  1148. netif_stop_queue(dev);
  1149. }
  1150. gp->last_psr = psr;
  1151. }
  1152. if (--gp->intr_work_done == 0)
  1153. break;
  1154. }
  1155. if (status & (icrTxBufferLow | icrTxEndLow))
  1156. gt96100_tx_complete(dev, status);
  1157. if (status & (icrRxBuffer | icrRxError)) {
  1158. gt96100_rx(dev, status);
  1159. }
  1160. // Now check TX errors (RX errors were handled in gt96100_rx)
  1161. if (status & icrTxErrorLow) {
  1162. err("%s: Tx resource error\n", __FUNCTION__);
  1163. if (--gp->intr_work_done == 0)
  1164. break;
  1165. }
  1166. if (status & icrTxUdr) {
  1167. err("%s: Tx underrun error\n", __FUNCTION__);
  1168. if (--gp->intr_work_done == 0)
  1169. break;
  1170. }
  1171. }
  1172. if (gp->intr_work_done == 0) {
  1173. // ACK any remaining pending interrupts
  1174. GT96100ETH_WRITE(gp, GT96100_ETH_INT_CAUSE, 0);
  1175. dbg(3, "%s: hit max work\n", __FUNCTION__);
  1176. }
  1177. dbg(3, "%s: exit, icr=%x\n", __FUNCTION__,
  1178. GT96100ETH_READ(gp, GT96100_ETH_INT_CAUSE));
  1179. spin_unlock(&gp->lock);
  1180. return IRQ_RETVAL(handled);
  1181. }
  1182. static void
  1183. gt96100_tx_timeout(struct net_device *dev)
  1184. {
  1185. struct gt96100_private *gp = netdev_priv(dev);
  1186. unsigned long flags;
  1187. spin_lock_irqsave(&gp->lock, flags);
  1188. if (!(gp->last_psr & psrLink)) {
  1189. err("tx_timeout: link down.\n");
  1190. spin_unlock_irqrestore(&gp->lock, flags);
  1191. } else {
  1192. if (gt96100_check_tx_consistent(gp))
  1193. err("tx_timeout: Tx ring error.\n");
  1194. disable_ether_irq(dev);
  1195. spin_unlock_irqrestore(&gp->lock, flags);
  1196. reset_tx(dev);
  1197. enable_ether_irq(dev);
  1198. netif_wake_queue(dev);
  1199. }
  1200. }
  1201. static void
  1202. gt96100_set_rx_mode(struct net_device *dev)
  1203. {
  1204. struct gt96100_private *gp = netdev_priv(dev);
  1205. unsigned long flags;
  1206. //struct dev_mc_list *mcptr;
  1207. dbg(3, "%s: dev=%p, flags=%x\n", __FUNCTION__, dev, dev->flags);
  1208. // stop the Receiver DMA
  1209. abort(dev, sdcmrAR);
  1210. spin_lock_irqsave(&gp->lock, flags);
  1211. if (dev->flags & IFF_PROMISC) {
  1212. GT96100ETH_WRITE(gp, GT96100_ETH_PORT_CONFIG,
  1213. pcrEN | pcrHS | pcrPM);
  1214. }
  1215. #if 0
  1216. /*
  1217. FIXME: currently multicast doesn't work - need to get hash table
  1218. working first.
  1219. */
  1220. if (dev->mc_count) {
  1221. // clear hash table
  1222. memset(gp->hash_table, 0, RX_HASH_TABLE_SIZE);
  1223. // Add our ethernet address
  1224. gt96100_add_hash_entry(dev, dev->dev_addr);
  1225. for (mcptr = dev->mc_list; mcptr; mcptr = mcptr->next) {
  1226. dump_hw_addr(2, dev, "%s: addr=", __FUNCTION__,
  1227. mcptr->dmi_addr);
  1228. gt96100_add_hash_entry(dev, mcptr->dmi_addr);
  1229. }
  1230. }
  1231. #endif
  1232. // restart Rx DMA
  1233. GT96100ETH_WRITE(gp, GT96100_ETH_SDMA_COMM, sdcmrERD);
  1234. spin_unlock_irqrestore(&gp->lock, flags);
  1235. }
  1236. static struct net_device_stats *
  1237. gt96100_get_stats(struct net_device *dev)
  1238. {
  1239. struct gt96100_private *gp = netdev_priv(dev);
  1240. unsigned long flags;
  1241. dbg(3, "%s: dev=%p\n", __FUNCTION__, dev);
  1242. if (netif_device_present(dev)) {
  1243. spin_lock_irqsave (&gp->lock, flags);
  1244. update_stats(gp);
  1245. spin_unlock_irqrestore (&gp->lock, flags);
  1246. }
  1247. return &gp->stats;
  1248. }
  1249. static void gt96100_cleanup_module(void)
  1250. {
  1251. int i;
  1252. for (i=0; i<NUM_INTERFACES; i++) {
  1253. struct gt96100_if_t *gtif = &gt96100_iflist[i];
  1254. if (gtif->dev != NULL) {
  1255. struct gt96100_private *gp = (struct gt96100_private *)
  1256. netdev_priv(gtif->dev);
  1257. unregister_netdev(gtif->dev);
  1258. dmafree(RX_HASH_TABLE_SIZE, gp->hash_table_dma);
  1259. dmafree(PKT_BUF_SZ*RX_RING_SIZE, gp->rx_buff);
  1260. dmafree(sizeof(gt96100_rd_t) * RX_RING_SIZE
  1261. + sizeof(gt96100_td_t) * TX_RING_SIZE,
  1262. gp->rx_ring);
  1263. free_netdev(gtif->dev);
  1264. release_region(gtif->iobase, gp->io_size);
  1265. }
  1266. }
  1267. }
  1268. static int __init gt96100_setup(char *options)
  1269. {
  1270. char *this_opt;
  1271. if (!options || !*options)
  1272. return 0;
  1273. while ((this_opt = strsep (&options, ",")) != NULL) {
  1274. if (!*this_opt)
  1275. continue;
  1276. if (!strncmp(this_opt, "mac0:", 5)) {
  1277. memcpy(mac0, this_opt+5, 17);
  1278. mac0[17]= '\0';
  1279. } else if (!strncmp(this_opt, "mac1:", 5)) {
  1280. memcpy(mac1, this_opt+5, 17);
  1281. mac1[17]= '\0';
  1282. }
  1283. }
  1284. return 1;
  1285. }
  1286. __setup("gt96100eth=", gt96100_setup);
  1287. module_init(gt96100_init_module);
  1288. module_exit(gt96100_cleanup_module);
  1289. MODULE_AUTHOR("Steve Longerbeam <stevel@mvista.com>");
  1290. MODULE_DESCRIPTION("GT96100 Ethernet driver");