dl2k.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /* D-Link DL2000-based Gigabit Ethernet Adapter Linux driver */
  2. /*
  3. Copyright (c) 2001, 2002 by D-Link Corporation
  4. Written by Edward Peng.<edward_peng@dlink.com.tw>
  5. Created 03-May-2001, base on Linux' sundance.c.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. */
  11. #define DRV_NAME "DL2000/TC902x-based linux driver"
  12. #define DRV_VERSION "v1.19"
  13. #define DRV_RELDATE "2007/08/12"
  14. #include "dl2k.h"
  15. #include <linux/dma-mapping.h>
  16. static char version[] __devinitdata =
  17. KERN_INFO DRV_NAME " " DRV_VERSION " " DRV_RELDATE "\n";
  18. #define MAX_UNITS 8
  19. static int mtu[MAX_UNITS];
  20. static int vlan[MAX_UNITS];
  21. static int jumbo[MAX_UNITS];
  22. static char *media[MAX_UNITS];
  23. static int tx_flow=-1;
  24. static int rx_flow=-1;
  25. static int copy_thresh;
  26. static int rx_coalesce=10; /* Rx frame count each interrupt */
  27. static int rx_timeout=200; /* Rx DMA wait time in 640ns increments */
  28. static int tx_coalesce=16; /* HW xmit count each TxDMAComplete */
  29. MODULE_AUTHOR ("Edward Peng");
  30. MODULE_DESCRIPTION ("D-Link DL2000-based Gigabit Ethernet Adapter");
  31. MODULE_LICENSE("GPL");
  32. module_param_array(mtu, int, NULL, 0);
  33. module_param_array(media, charp, NULL, 0);
  34. module_param_array(vlan, int, NULL, 0);
  35. module_param_array(jumbo, int, NULL, 0);
  36. module_param(tx_flow, int, 0);
  37. module_param(rx_flow, int, 0);
  38. module_param(copy_thresh, int, 0);
  39. module_param(rx_coalesce, int, 0); /* Rx frame count each interrupt */
  40. module_param(rx_timeout, int, 0); /* Rx DMA wait time in 64ns increments */
  41. module_param(tx_coalesce, int, 0); /* HW xmit count each TxDMAComplete */
  42. /* Enable the default interrupts */
  43. #define DEFAULT_INTR (RxDMAComplete | HostError | IntRequested | TxDMAComplete| \
  44. UpdateStats | LinkEvent)
  45. #define EnableInt() \
  46. writew(DEFAULT_INTR, ioaddr + IntEnable)
  47. static const int max_intrloop = 50;
  48. static const int multicast_filter_limit = 0x40;
  49. static int rio_open (struct net_device *dev);
  50. static void rio_timer (unsigned long data);
  51. static void rio_tx_timeout (struct net_device *dev);
  52. static void alloc_list (struct net_device *dev);
  53. static int start_xmit (struct sk_buff *skb, struct net_device *dev);
  54. static irqreturn_t rio_interrupt (int irq, void *dev_instance);
  55. static void rio_free_tx (struct net_device *dev, int irq);
  56. static void tx_error (struct net_device *dev, int tx_status);
  57. static int receive_packet (struct net_device *dev);
  58. static void rio_error (struct net_device *dev, int int_status);
  59. static int change_mtu (struct net_device *dev, int new_mtu);
  60. static void set_multicast (struct net_device *dev);
  61. static struct net_device_stats *get_stats (struct net_device *dev);
  62. static int clear_stats (struct net_device *dev);
  63. static int rio_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
  64. static int rio_close (struct net_device *dev);
  65. static int find_miiphy (struct net_device *dev);
  66. static int parse_eeprom (struct net_device *dev);
  67. static int read_eeprom (long ioaddr, int eep_addr);
  68. static int mii_wait_link (struct net_device *dev, int wait);
  69. static int mii_set_media (struct net_device *dev);
  70. static int mii_get_media (struct net_device *dev);
  71. static int mii_set_media_pcs (struct net_device *dev);
  72. static int mii_get_media_pcs (struct net_device *dev);
  73. static int mii_read (struct net_device *dev, int phy_addr, int reg_num);
  74. static int mii_write (struct net_device *dev, int phy_addr, int reg_num,
  75. u16 data);
  76. static const struct ethtool_ops ethtool_ops;
  77. static int __devinit
  78. rio_probe1 (struct pci_dev *pdev, const struct pci_device_id *ent)
  79. {
  80. struct net_device *dev;
  81. struct netdev_private *np;
  82. static int card_idx;
  83. int chip_idx = ent->driver_data;
  84. int err, irq;
  85. long ioaddr;
  86. static int version_printed;
  87. void *ring_space;
  88. dma_addr_t ring_dma;
  89. DECLARE_MAC_BUF(mac);
  90. if (!version_printed++)
  91. printk ("%s", version);
  92. err = pci_enable_device (pdev);
  93. if (err)
  94. return err;
  95. irq = pdev->irq;
  96. err = pci_request_regions (pdev, "dl2k");
  97. if (err)
  98. goto err_out_disable;
  99. pci_set_master (pdev);
  100. dev = alloc_etherdev (sizeof (*np));
  101. if (!dev) {
  102. err = -ENOMEM;
  103. goto err_out_res;
  104. }
  105. SET_NETDEV_DEV(dev, &pdev->dev);
  106. #ifdef MEM_MAPPING
  107. ioaddr = pci_resource_start (pdev, 1);
  108. ioaddr = (long) ioremap (ioaddr, RIO_IO_SIZE);
  109. if (!ioaddr) {
  110. err = -ENOMEM;
  111. goto err_out_dev;
  112. }
  113. #else
  114. ioaddr = pci_resource_start (pdev, 0);
  115. #endif
  116. dev->base_addr = ioaddr;
  117. dev->irq = irq;
  118. np = netdev_priv(dev);
  119. np->chip_id = chip_idx;
  120. np->pdev = pdev;
  121. spin_lock_init (&np->tx_lock);
  122. spin_lock_init (&np->rx_lock);
  123. /* Parse manual configuration */
  124. np->an_enable = 1;
  125. np->tx_coalesce = 1;
  126. if (card_idx < MAX_UNITS) {
  127. if (media[card_idx] != NULL) {
  128. np->an_enable = 0;
  129. if (strcmp (media[card_idx], "auto") == 0 ||
  130. strcmp (media[card_idx], "autosense") == 0 ||
  131. strcmp (media[card_idx], "0") == 0 ) {
  132. np->an_enable = 2;
  133. } else if (strcmp (media[card_idx], "100mbps_fd") == 0 ||
  134. strcmp (media[card_idx], "4") == 0) {
  135. np->speed = 100;
  136. np->full_duplex = 1;
  137. } else if (strcmp (media[card_idx], "100mbps_hd") == 0
  138. || strcmp (media[card_idx], "3") == 0) {
  139. np->speed = 100;
  140. np->full_duplex = 0;
  141. } else if (strcmp (media[card_idx], "10mbps_fd") == 0 ||
  142. strcmp (media[card_idx], "2") == 0) {
  143. np->speed = 10;
  144. np->full_duplex = 1;
  145. } else if (strcmp (media[card_idx], "10mbps_hd") == 0 ||
  146. strcmp (media[card_idx], "1") == 0) {
  147. np->speed = 10;
  148. np->full_duplex = 0;
  149. } else if (strcmp (media[card_idx], "1000mbps_fd") == 0 ||
  150. strcmp (media[card_idx], "6") == 0) {
  151. np->speed=1000;
  152. np->full_duplex=1;
  153. } else if (strcmp (media[card_idx], "1000mbps_hd") == 0 ||
  154. strcmp (media[card_idx], "5") == 0) {
  155. np->speed = 1000;
  156. np->full_duplex = 0;
  157. } else {
  158. np->an_enable = 1;
  159. }
  160. }
  161. if (jumbo[card_idx] != 0) {
  162. np->jumbo = 1;
  163. dev->mtu = MAX_JUMBO;
  164. } else {
  165. np->jumbo = 0;
  166. if (mtu[card_idx] > 0 && mtu[card_idx] < PACKET_SIZE)
  167. dev->mtu = mtu[card_idx];
  168. }
  169. np->vlan = (vlan[card_idx] > 0 && vlan[card_idx] < 4096) ?
  170. vlan[card_idx] : 0;
  171. if (rx_coalesce > 0 && rx_timeout > 0) {
  172. np->rx_coalesce = rx_coalesce;
  173. np->rx_timeout = rx_timeout;
  174. np->coalesce = 1;
  175. }
  176. np->tx_flow = (tx_flow == 0) ? 0 : 1;
  177. np->rx_flow = (rx_flow == 0) ? 0 : 1;
  178. if (tx_coalesce < 1)
  179. tx_coalesce = 1;
  180. else if (tx_coalesce > TX_RING_SIZE-1)
  181. tx_coalesce = TX_RING_SIZE - 1;
  182. }
  183. dev->open = &rio_open;
  184. dev->hard_start_xmit = &start_xmit;
  185. dev->stop = &rio_close;
  186. dev->get_stats = &get_stats;
  187. dev->set_multicast_list = &set_multicast;
  188. dev->do_ioctl = &rio_ioctl;
  189. dev->tx_timeout = &rio_tx_timeout;
  190. dev->watchdog_timeo = TX_TIMEOUT;
  191. dev->change_mtu = &change_mtu;
  192. SET_ETHTOOL_OPS(dev, &ethtool_ops);
  193. #if 0
  194. dev->features = NETIF_F_IP_CSUM;
  195. #endif
  196. pci_set_drvdata (pdev, dev);
  197. ring_space = pci_alloc_consistent (pdev, TX_TOTAL_SIZE, &ring_dma);
  198. if (!ring_space)
  199. goto err_out_iounmap;
  200. np->tx_ring = (struct netdev_desc *) ring_space;
  201. np->tx_ring_dma = ring_dma;
  202. ring_space = pci_alloc_consistent (pdev, RX_TOTAL_SIZE, &ring_dma);
  203. if (!ring_space)
  204. goto err_out_unmap_tx;
  205. np->rx_ring = (struct netdev_desc *) ring_space;
  206. np->rx_ring_dma = ring_dma;
  207. /* Parse eeprom data */
  208. parse_eeprom (dev);
  209. /* Find PHY address */
  210. err = find_miiphy (dev);
  211. if (err)
  212. goto err_out_unmap_rx;
  213. /* Fiber device? */
  214. np->phy_media = (readw(ioaddr + ASICCtrl) & PhyMedia) ? 1 : 0;
  215. np->link_status = 0;
  216. /* Set media and reset PHY */
  217. if (np->phy_media) {
  218. /* default Auto-Negotiation for fiber deivices */
  219. if (np->an_enable == 2) {
  220. np->an_enable = 1;
  221. }
  222. mii_set_media_pcs (dev);
  223. } else {
  224. /* Auto-Negotiation is mandatory for 1000BASE-T,
  225. IEEE 802.3ab Annex 28D page 14 */
  226. if (np->speed == 1000)
  227. np->an_enable = 1;
  228. mii_set_media (dev);
  229. }
  230. err = register_netdev (dev);
  231. if (err)
  232. goto err_out_unmap_rx;
  233. card_idx++;
  234. printk (KERN_INFO "%s: %s, %s, IRQ %d\n",
  235. dev->name, np->name, print_mac(mac, dev->dev_addr), irq);
  236. if (tx_coalesce > 1)
  237. printk(KERN_INFO "tx_coalesce:\t%d packets\n",
  238. tx_coalesce);
  239. if (np->coalesce)
  240. printk(KERN_INFO "rx_coalesce:\t%d packets\n"
  241. KERN_INFO "rx_timeout: \t%d ns\n",
  242. np->rx_coalesce, np->rx_timeout*640);
  243. if (np->vlan)
  244. printk(KERN_INFO "vlan(id):\t%d\n", np->vlan);
  245. return 0;
  246. err_out_unmap_rx:
  247. pci_free_consistent (pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma);
  248. err_out_unmap_tx:
  249. pci_free_consistent (pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma);
  250. err_out_iounmap:
  251. #ifdef MEM_MAPPING
  252. iounmap ((void *) ioaddr);
  253. err_out_dev:
  254. #endif
  255. free_netdev (dev);
  256. err_out_res:
  257. pci_release_regions (pdev);
  258. err_out_disable:
  259. pci_disable_device (pdev);
  260. return err;
  261. }
  262. static int
  263. find_miiphy (struct net_device *dev)
  264. {
  265. int i, phy_found = 0;
  266. struct netdev_private *np;
  267. long ioaddr;
  268. np = netdev_priv(dev);
  269. ioaddr = dev->base_addr;
  270. np->phy_addr = 1;
  271. for (i = 31; i >= 0; i--) {
  272. int mii_status = mii_read (dev, i, 1);
  273. if (mii_status != 0xffff && mii_status != 0x0000) {
  274. np->phy_addr = i;
  275. phy_found++;
  276. }
  277. }
  278. if (!phy_found) {
  279. printk (KERN_ERR "%s: No MII PHY found!\n", dev->name);
  280. return -ENODEV;
  281. }
  282. return 0;
  283. }
  284. static int
  285. parse_eeprom (struct net_device *dev)
  286. {
  287. int i, j;
  288. long ioaddr = dev->base_addr;
  289. u8 sromdata[256];
  290. u8 *psib;
  291. u32 crc;
  292. PSROM_t psrom = (PSROM_t) sromdata;
  293. struct netdev_private *np = netdev_priv(dev);
  294. int cid, next;
  295. #ifdef MEM_MAPPING
  296. ioaddr = pci_resource_start (np->pdev, 0);
  297. #endif
  298. /* Read eeprom */
  299. for (i = 0; i < 128; i++) {
  300. ((__le16 *) sromdata)[i] = cpu_to_le16(read_eeprom (ioaddr, i));
  301. }
  302. #ifdef MEM_MAPPING
  303. ioaddr = dev->base_addr;
  304. #endif
  305. if (np->pdev->vendor == PCI_VENDOR_ID_DLINK) { /* D-Link Only */
  306. /* Check CRC */
  307. crc = ~ether_crc_le (256 - 4, sromdata);
  308. if (psrom->crc != crc) {
  309. printk (KERN_ERR "%s: EEPROM data CRC error.\n",
  310. dev->name);
  311. return -1;
  312. }
  313. }
  314. /* Set MAC address */
  315. for (i = 0; i < 6; i++)
  316. dev->dev_addr[i] = psrom->mac_addr[i];
  317. if (np->pdev->vendor != PCI_VENDOR_ID_DLINK) {
  318. return 0;
  319. }
  320. /* Parse Software Information Block */
  321. i = 0x30;
  322. psib = (u8 *) sromdata;
  323. do {
  324. cid = psib[i++];
  325. next = psib[i++];
  326. if ((cid == 0 && next == 0) || (cid == 0xff && next == 0xff)) {
  327. printk (KERN_ERR "Cell data error\n");
  328. return -1;
  329. }
  330. switch (cid) {
  331. case 0: /* Format version */
  332. break;
  333. case 1: /* End of cell */
  334. return 0;
  335. case 2: /* Duplex Polarity */
  336. np->duplex_polarity = psib[i];
  337. writeb (readb (ioaddr + PhyCtrl) | psib[i],
  338. ioaddr + PhyCtrl);
  339. break;
  340. case 3: /* Wake Polarity */
  341. np->wake_polarity = psib[i];
  342. break;
  343. case 9: /* Adapter description */
  344. j = (next - i > 255) ? 255 : next - i;
  345. memcpy (np->name, &(psib[i]), j);
  346. break;
  347. case 4:
  348. case 5:
  349. case 6:
  350. case 7:
  351. case 8: /* Reversed */
  352. break;
  353. default: /* Unknown cell */
  354. return -1;
  355. }
  356. i = next;
  357. } while (1);
  358. return 0;
  359. }
  360. static int
  361. rio_open (struct net_device *dev)
  362. {
  363. struct netdev_private *np = netdev_priv(dev);
  364. long ioaddr = dev->base_addr;
  365. int i;
  366. u16 macctrl;
  367. i = request_irq (dev->irq, &rio_interrupt, IRQF_SHARED, dev->name, dev);
  368. if (i)
  369. return i;
  370. /* Reset all logic functions */
  371. writew (GlobalReset | DMAReset | FIFOReset | NetworkReset | HostReset,
  372. ioaddr + ASICCtrl + 2);
  373. mdelay(10);
  374. /* DebugCtrl bit 4, 5, 9 must set */
  375. writel (readl (ioaddr + DebugCtrl) | 0x0230, ioaddr + DebugCtrl);
  376. /* Jumbo frame */
  377. if (np->jumbo != 0)
  378. writew (MAX_JUMBO+14, ioaddr + MaxFrameSize);
  379. alloc_list (dev);
  380. /* Get station address */
  381. for (i = 0; i < 6; i++)
  382. writeb (dev->dev_addr[i], ioaddr + StationAddr0 + i);
  383. set_multicast (dev);
  384. if (np->coalesce) {
  385. writel (np->rx_coalesce | np->rx_timeout << 16,
  386. ioaddr + RxDMAIntCtrl);
  387. }
  388. /* Set RIO to poll every N*320nsec. */
  389. writeb (0x20, ioaddr + RxDMAPollPeriod);
  390. writeb (0xff, ioaddr + TxDMAPollPeriod);
  391. writeb (0x30, ioaddr + RxDMABurstThresh);
  392. writeb (0x30, ioaddr + RxDMAUrgentThresh);
  393. writel (0x0007ffff, ioaddr + RmonStatMask);
  394. /* clear statistics */
  395. clear_stats (dev);
  396. /* VLAN supported */
  397. if (np->vlan) {
  398. /* priority field in RxDMAIntCtrl */
  399. writel (readl(ioaddr + RxDMAIntCtrl) | 0x7 << 10,
  400. ioaddr + RxDMAIntCtrl);
  401. /* VLANId */
  402. writew (np->vlan, ioaddr + VLANId);
  403. /* Length/Type should be 0x8100 */
  404. writel (0x8100 << 16 | np->vlan, ioaddr + VLANTag);
  405. /* Enable AutoVLANuntagging, but disable AutoVLANtagging.
  406. VLAN information tagged by TFC' VID, CFI fields. */
  407. writel (readl (ioaddr + MACCtrl) | AutoVLANuntagging,
  408. ioaddr + MACCtrl);
  409. }
  410. init_timer (&np->timer);
  411. np->timer.expires = jiffies + 1*HZ;
  412. np->timer.data = (unsigned long) dev;
  413. np->timer.function = &rio_timer;
  414. add_timer (&np->timer);
  415. /* Start Tx/Rx */
  416. writel (readl (ioaddr + MACCtrl) | StatsEnable | RxEnable | TxEnable,
  417. ioaddr + MACCtrl);
  418. macctrl = 0;
  419. macctrl |= (np->vlan) ? AutoVLANuntagging : 0;
  420. macctrl |= (np->full_duplex) ? DuplexSelect : 0;
  421. macctrl |= (np->tx_flow) ? TxFlowControlEnable : 0;
  422. macctrl |= (np->rx_flow) ? RxFlowControlEnable : 0;
  423. writew(macctrl, ioaddr + MACCtrl);
  424. netif_start_queue (dev);
  425. /* Enable default interrupts */
  426. EnableInt ();
  427. return 0;
  428. }
  429. static void
  430. rio_timer (unsigned long data)
  431. {
  432. struct net_device *dev = (struct net_device *)data;
  433. struct netdev_private *np = netdev_priv(dev);
  434. unsigned int entry;
  435. int next_tick = 1*HZ;
  436. unsigned long flags;
  437. spin_lock_irqsave(&np->rx_lock, flags);
  438. /* Recover rx ring exhausted error */
  439. if (np->cur_rx - np->old_rx >= RX_RING_SIZE) {
  440. printk(KERN_INFO "Try to recover rx ring exhausted...\n");
  441. /* Re-allocate skbuffs to fill the descriptor ring */
  442. for (; np->cur_rx - np->old_rx > 0; np->old_rx++) {
  443. struct sk_buff *skb;
  444. entry = np->old_rx % RX_RING_SIZE;
  445. /* Dropped packets don't need to re-allocate */
  446. if (np->rx_skbuff[entry] == NULL) {
  447. skb = dev_alloc_skb (np->rx_buf_sz);
  448. if (skb == NULL) {
  449. np->rx_ring[entry].fraginfo = 0;
  450. printk (KERN_INFO
  451. "%s: Still unable to re-allocate Rx skbuff.#%d\n",
  452. dev->name, entry);
  453. break;
  454. }
  455. np->rx_skbuff[entry] = skb;
  456. /* 16 byte align the IP header */
  457. skb_reserve (skb, 2);
  458. np->rx_ring[entry].fraginfo =
  459. cpu_to_le64 (pci_map_single
  460. (np->pdev, skb->data, np->rx_buf_sz,
  461. PCI_DMA_FROMDEVICE));
  462. }
  463. np->rx_ring[entry].fraginfo |=
  464. cpu_to_le64((u64)np->rx_buf_sz << 48);
  465. np->rx_ring[entry].status = 0;
  466. } /* end for */
  467. } /* end if */
  468. spin_unlock_irqrestore (&np->rx_lock, flags);
  469. np->timer.expires = jiffies + next_tick;
  470. add_timer(&np->timer);
  471. }
  472. static void
  473. rio_tx_timeout (struct net_device *dev)
  474. {
  475. long ioaddr = dev->base_addr;
  476. printk (KERN_INFO "%s: Tx timed out (%4.4x), is buffer full?\n",
  477. dev->name, readl (ioaddr + TxStatus));
  478. rio_free_tx(dev, 0);
  479. dev->if_port = 0;
  480. dev->trans_start = jiffies;
  481. }
  482. /* allocate and initialize Tx and Rx descriptors */
  483. static void
  484. alloc_list (struct net_device *dev)
  485. {
  486. struct netdev_private *np = netdev_priv(dev);
  487. int i;
  488. np->cur_rx = np->cur_tx = 0;
  489. np->old_rx = np->old_tx = 0;
  490. np->rx_buf_sz = (dev->mtu <= 1500 ? PACKET_SIZE : dev->mtu + 32);
  491. /* Initialize Tx descriptors, TFDListPtr leaves in start_xmit(). */
  492. for (i = 0; i < TX_RING_SIZE; i++) {
  493. np->tx_skbuff[i] = NULL;
  494. np->tx_ring[i].status = cpu_to_le64 (TFDDone);
  495. np->tx_ring[i].next_desc = cpu_to_le64 (np->tx_ring_dma +
  496. ((i+1)%TX_RING_SIZE) *
  497. sizeof (struct netdev_desc));
  498. }
  499. /* Initialize Rx descriptors */
  500. for (i = 0; i < RX_RING_SIZE; i++) {
  501. np->rx_ring[i].next_desc = cpu_to_le64 (np->rx_ring_dma +
  502. ((i + 1) % RX_RING_SIZE) *
  503. sizeof (struct netdev_desc));
  504. np->rx_ring[i].status = 0;
  505. np->rx_ring[i].fraginfo = 0;
  506. np->rx_skbuff[i] = NULL;
  507. }
  508. /* Allocate the rx buffers */
  509. for (i = 0; i < RX_RING_SIZE; i++) {
  510. /* Allocated fixed size of skbuff */
  511. struct sk_buff *skb = dev_alloc_skb (np->rx_buf_sz);
  512. np->rx_skbuff[i] = skb;
  513. if (skb == NULL) {
  514. printk (KERN_ERR
  515. "%s: alloc_list: allocate Rx buffer error! ",
  516. dev->name);
  517. break;
  518. }
  519. skb_reserve (skb, 2); /* 16 byte align the IP header. */
  520. /* Rubicon now supports 40 bits of addressing space. */
  521. np->rx_ring[i].fraginfo =
  522. cpu_to_le64 ( pci_map_single (
  523. np->pdev, skb->data, np->rx_buf_sz,
  524. PCI_DMA_FROMDEVICE));
  525. np->rx_ring[i].fraginfo |= cpu_to_le64((u64)np->rx_buf_sz << 48);
  526. }
  527. /* Set RFDListPtr */
  528. writel (np->rx_ring_dma, dev->base_addr + RFDListPtr0);
  529. writel (0, dev->base_addr + RFDListPtr1);
  530. return;
  531. }
  532. static int
  533. start_xmit (struct sk_buff *skb, struct net_device *dev)
  534. {
  535. struct netdev_private *np = netdev_priv(dev);
  536. struct netdev_desc *txdesc;
  537. unsigned entry;
  538. u32 ioaddr;
  539. u64 tfc_vlan_tag = 0;
  540. if (np->link_status == 0) { /* Link Down */
  541. dev_kfree_skb(skb);
  542. return 0;
  543. }
  544. ioaddr = dev->base_addr;
  545. entry = np->cur_tx % TX_RING_SIZE;
  546. np->tx_skbuff[entry] = skb;
  547. txdesc = &np->tx_ring[entry];
  548. #if 0
  549. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  550. txdesc->status |=
  551. cpu_to_le64 (TCPChecksumEnable | UDPChecksumEnable |
  552. IPChecksumEnable);
  553. }
  554. #endif
  555. if (np->vlan) {
  556. tfc_vlan_tag = VLANTagInsert |
  557. ((u64)np->vlan << 32) |
  558. ((u64)skb->priority << 45);
  559. }
  560. txdesc->fraginfo = cpu_to_le64 (pci_map_single (np->pdev, skb->data,
  561. skb->len,
  562. PCI_DMA_TODEVICE));
  563. txdesc->fraginfo |= cpu_to_le64((u64)skb->len << 48);
  564. /* DL2K bug: DMA fails to get next descriptor ptr in 10Mbps mode
  565. * Work around: Always use 1 descriptor in 10Mbps mode */
  566. if (entry % np->tx_coalesce == 0 || np->speed == 10)
  567. txdesc->status = cpu_to_le64 (entry | tfc_vlan_tag |
  568. WordAlignDisable |
  569. TxDMAIndicate |
  570. (1 << FragCountShift));
  571. else
  572. txdesc->status = cpu_to_le64 (entry | tfc_vlan_tag |
  573. WordAlignDisable |
  574. (1 << FragCountShift));
  575. /* TxDMAPollNow */
  576. writel (readl (ioaddr + DMACtrl) | 0x00001000, ioaddr + DMACtrl);
  577. /* Schedule ISR */
  578. writel(10000, ioaddr + CountDown);
  579. np->cur_tx = (np->cur_tx + 1) % TX_RING_SIZE;
  580. if ((np->cur_tx - np->old_tx + TX_RING_SIZE) % TX_RING_SIZE
  581. < TX_QUEUE_LEN - 1 && np->speed != 10) {
  582. /* do nothing */
  583. } else if (!netif_queue_stopped(dev)) {
  584. netif_stop_queue (dev);
  585. }
  586. /* The first TFDListPtr */
  587. if (readl (dev->base_addr + TFDListPtr0) == 0) {
  588. writel (np->tx_ring_dma + entry * sizeof (struct netdev_desc),
  589. dev->base_addr + TFDListPtr0);
  590. writel (0, dev->base_addr + TFDListPtr1);
  591. }
  592. /* NETDEV WATCHDOG timer */
  593. dev->trans_start = jiffies;
  594. return 0;
  595. }
  596. static irqreturn_t
  597. rio_interrupt (int irq, void *dev_instance)
  598. {
  599. struct net_device *dev = dev_instance;
  600. struct netdev_private *np;
  601. unsigned int_status;
  602. long ioaddr;
  603. int cnt = max_intrloop;
  604. int handled = 0;
  605. ioaddr = dev->base_addr;
  606. np = netdev_priv(dev);
  607. while (1) {
  608. int_status = readw (ioaddr + IntStatus);
  609. writew (int_status, ioaddr + IntStatus);
  610. int_status &= DEFAULT_INTR;
  611. if (int_status == 0 || --cnt < 0)
  612. break;
  613. handled = 1;
  614. /* Processing received packets */
  615. if (int_status & RxDMAComplete)
  616. receive_packet (dev);
  617. /* TxDMAComplete interrupt */
  618. if ((int_status & (TxDMAComplete|IntRequested))) {
  619. int tx_status;
  620. tx_status = readl (ioaddr + TxStatus);
  621. if (tx_status & 0x01)
  622. tx_error (dev, tx_status);
  623. /* Free used tx skbuffs */
  624. rio_free_tx (dev, 1);
  625. }
  626. /* Handle uncommon events */
  627. if (int_status &
  628. (HostError | LinkEvent | UpdateStats))
  629. rio_error (dev, int_status);
  630. }
  631. if (np->cur_tx != np->old_tx)
  632. writel (100, ioaddr + CountDown);
  633. return IRQ_RETVAL(handled);
  634. }
  635. static inline dma_addr_t desc_to_dma(struct netdev_desc *desc)
  636. {
  637. return le64_to_cpu(desc->fraginfo) & DMA_48BIT_MASK;
  638. }
  639. static void
  640. rio_free_tx (struct net_device *dev, int irq)
  641. {
  642. struct netdev_private *np = netdev_priv(dev);
  643. int entry = np->old_tx % TX_RING_SIZE;
  644. int tx_use = 0;
  645. unsigned long flag = 0;
  646. if (irq)
  647. spin_lock(&np->tx_lock);
  648. else
  649. spin_lock_irqsave(&np->tx_lock, flag);
  650. /* Free used tx skbuffs */
  651. while (entry != np->cur_tx) {
  652. struct sk_buff *skb;
  653. if (!(np->tx_ring[entry].status & cpu_to_le64(TFDDone)))
  654. break;
  655. skb = np->tx_skbuff[entry];
  656. pci_unmap_single (np->pdev,
  657. desc_to_dma(&np->tx_ring[entry]),
  658. skb->len, PCI_DMA_TODEVICE);
  659. if (irq)
  660. dev_kfree_skb_irq (skb);
  661. else
  662. dev_kfree_skb (skb);
  663. np->tx_skbuff[entry] = NULL;
  664. entry = (entry + 1) % TX_RING_SIZE;
  665. tx_use++;
  666. }
  667. if (irq)
  668. spin_unlock(&np->tx_lock);
  669. else
  670. spin_unlock_irqrestore(&np->tx_lock, flag);
  671. np->old_tx = entry;
  672. /* If the ring is no longer full, clear tx_full and
  673. call netif_wake_queue() */
  674. if (netif_queue_stopped(dev) &&
  675. ((np->cur_tx - np->old_tx + TX_RING_SIZE) % TX_RING_SIZE
  676. < TX_QUEUE_LEN - 1 || np->speed == 10)) {
  677. netif_wake_queue (dev);
  678. }
  679. }
  680. static void
  681. tx_error (struct net_device *dev, int tx_status)
  682. {
  683. struct netdev_private *np;
  684. long ioaddr = dev->base_addr;
  685. int frame_id;
  686. int i;
  687. np = netdev_priv(dev);
  688. frame_id = (tx_status & 0xffff0000);
  689. printk (KERN_ERR "%s: Transmit error, TxStatus %4.4x, FrameId %d.\n",
  690. dev->name, tx_status, frame_id);
  691. np->stats.tx_errors++;
  692. /* Ttransmit Underrun */
  693. if (tx_status & 0x10) {
  694. np->stats.tx_fifo_errors++;
  695. writew (readw (ioaddr + TxStartThresh) + 0x10,
  696. ioaddr + TxStartThresh);
  697. /* Transmit Underrun need to set TxReset, DMARest, FIFOReset */
  698. writew (TxReset | DMAReset | FIFOReset | NetworkReset,
  699. ioaddr + ASICCtrl + 2);
  700. /* Wait for ResetBusy bit clear */
  701. for (i = 50; i > 0; i--) {
  702. if ((readw (ioaddr + ASICCtrl + 2) & ResetBusy) == 0)
  703. break;
  704. mdelay (1);
  705. }
  706. rio_free_tx (dev, 1);
  707. /* Reset TFDListPtr */
  708. writel (np->tx_ring_dma +
  709. np->old_tx * sizeof (struct netdev_desc),
  710. dev->base_addr + TFDListPtr0);
  711. writel (0, dev->base_addr + TFDListPtr1);
  712. /* Let TxStartThresh stay default value */
  713. }
  714. /* Late Collision */
  715. if (tx_status & 0x04) {
  716. np->stats.tx_fifo_errors++;
  717. /* TxReset and clear FIFO */
  718. writew (TxReset | FIFOReset, ioaddr + ASICCtrl + 2);
  719. /* Wait reset done */
  720. for (i = 50; i > 0; i--) {
  721. if ((readw (ioaddr + ASICCtrl + 2) & ResetBusy) == 0)
  722. break;
  723. mdelay (1);
  724. }
  725. /* Let TxStartThresh stay default value */
  726. }
  727. /* Maximum Collisions */
  728. #ifdef ETHER_STATS
  729. if (tx_status & 0x08)
  730. np->stats.collisions16++;
  731. #else
  732. if (tx_status & 0x08)
  733. np->stats.collisions++;
  734. #endif
  735. /* Restart the Tx */
  736. writel (readw (dev->base_addr + MACCtrl) | TxEnable, ioaddr + MACCtrl);
  737. }
  738. static int
  739. receive_packet (struct net_device *dev)
  740. {
  741. struct netdev_private *np = netdev_priv(dev);
  742. int entry = np->cur_rx % RX_RING_SIZE;
  743. int cnt = 30;
  744. /* If RFDDone, FrameStart and FrameEnd set, there is a new packet in. */
  745. while (1) {
  746. struct netdev_desc *desc = &np->rx_ring[entry];
  747. int pkt_len;
  748. u64 frame_status;
  749. if (!(desc->status & cpu_to_le64(RFDDone)) ||
  750. !(desc->status & cpu_to_le64(FrameStart)) ||
  751. !(desc->status & cpu_to_le64(FrameEnd)))
  752. break;
  753. /* Chip omits the CRC. */
  754. frame_status = le64_to_cpu(desc->status);
  755. pkt_len = frame_status & 0xffff;
  756. if (--cnt < 0)
  757. break;
  758. /* Update rx error statistics, drop packet. */
  759. if (frame_status & RFS_Errors) {
  760. np->stats.rx_errors++;
  761. if (frame_status & (RxRuntFrame | RxLengthError))
  762. np->stats.rx_length_errors++;
  763. if (frame_status & RxFCSError)
  764. np->stats.rx_crc_errors++;
  765. if (frame_status & RxAlignmentError && np->speed != 1000)
  766. np->stats.rx_frame_errors++;
  767. if (frame_status & RxFIFOOverrun)
  768. np->stats.rx_fifo_errors++;
  769. } else {
  770. struct sk_buff *skb;
  771. /* Small skbuffs for short packets */
  772. if (pkt_len > copy_thresh) {
  773. pci_unmap_single (np->pdev,
  774. desc_to_dma(desc),
  775. np->rx_buf_sz,
  776. PCI_DMA_FROMDEVICE);
  777. skb_put (skb = np->rx_skbuff[entry], pkt_len);
  778. np->rx_skbuff[entry] = NULL;
  779. } else if ((skb = dev_alloc_skb (pkt_len + 2)) != NULL) {
  780. pci_dma_sync_single_for_cpu(np->pdev,
  781. desc_to_dma(desc),
  782. np->rx_buf_sz,
  783. PCI_DMA_FROMDEVICE);
  784. /* 16 byte align the IP header */
  785. skb_reserve (skb, 2);
  786. skb_copy_to_linear_data (skb,
  787. np->rx_skbuff[entry]->data,
  788. pkt_len);
  789. skb_put (skb, pkt_len);
  790. pci_dma_sync_single_for_device(np->pdev,
  791. desc_to_dma(desc),
  792. np->rx_buf_sz,
  793. PCI_DMA_FROMDEVICE);
  794. }
  795. skb->protocol = eth_type_trans (skb, dev);
  796. #if 0
  797. /* Checksum done by hw, but csum value unavailable. */
  798. if (np->pdev->pci_rev_id >= 0x0c &&
  799. !(frame_status & (TCPError | UDPError | IPError))) {
  800. skb->ip_summed = CHECKSUM_UNNECESSARY;
  801. }
  802. #endif
  803. netif_rx (skb);
  804. dev->last_rx = jiffies;
  805. }
  806. entry = (entry + 1) % RX_RING_SIZE;
  807. }
  808. spin_lock(&np->rx_lock);
  809. np->cur_rx = entry;
  810. /* Re-allocate skbuffs to fill the descriptor ring */
  811. entry = np->old_rx;
  812. while (entry != np->cur_rx) {
  813. struct sk_buff *skb;
  814. /* Dropped packets don't need to re-allocate */
  815. if (np->rx_skbuff[entry] == NULL) {
  816. skb = dev_alloc_skb (np->rx_buf_sz);
  817. if (skb == NULL) {
  818. np->rx_ring[entry].fraginfo = 0;
  819. printk (KERN_INFO
  820. "%s: receive_packet: "
  821. "Unable to re-allocate Rx skbuff.#%d\n",
  822. dev->name, entry);
  823. break;
  824. }
  825. np->rx_skbuff[entry] = skb;
  826. /* 16 byte align the IP header */
  827. skb_reserve (skb, 2);
  828. np->rx_ring[entry].fraginfo =
  829. cpu_to_le64 (pci_map_single
  830. (np->pdev, skb->data, np->rx_buf_sz,
  831. PCI_DMA_FROMDEVICE));
  832. }
  833. np->rx_ring[entry].fraginfo |=
  834. cpu_to_le64((u64)np->rx_buf_sz << 48);
  835. np->rx_ring[entry].status = 0;
  836. entry = (entry + 1) % RX_RING_SIZE;
  837. }
  838. np->old_rx = entry;
  839. spin_unlock(&np->rx_lock);
  840. return 0;
  841. }
  842. static void
  843. rio_error (struct net_device *dev, int int_status)
  844. {
  845. long ioaddr = dev->base_addr;
  846. struct netdev_private *np = netdev_priv(dev);
  847. u16 macctrl;
  848. /* Link change event */
  849. if (int_status & LinkEvent) {
  850. if (mii_wait_link (dev, 10) == 0) {
  851. printk (KERN_INFO "%s: Link up\n", dev->name);
  852. if (np->phy_media)
  853. mii_get_media_pcs (dev);
  854. else
  855. mii_get_media (dev);
  856. if (np->speed == 1000)
  857. np->tx_coalesce = tx_coalesce;
  858. else
  859. np->tx_coalesce = 1;
  860. macctrl = 0;
  861. macctrl |= (np->vlan) ? AutoVLANuntagging : 0;
  862. macctrl |= (np->full_duplex) ? DuplexSelect : 0;
  863. macctrl |= (np->tx_flow) ?
  864. TxFlowControlEnable : 0;
  865. macctrl |= (np->rx_flow) ?
  866. RxFlowControlEnable : 0;
  867. writew(macctrl, ioaddr + MACCtrl);
  868. np->link_status = 1;
  869. netif_carrier_on(dev);
  870. } else {
  871. printk (KERN_INFO "%s: Link off\n", dev->name);
  872. np->link_status = 0;
  873. netif_carrier_off(dev);
  874. }
  875. }
  876. /* UpdateStats statistics registers */
  877. if (int_status & UpdateStats) {
  878. get_stats (dev);
  879. }
  880. /* PCI Error, a catastronphic error related to the bus interface
  881. occurs, set GlobalReset and HostReset to reset. */
  882. if (int_status & HostError) {
  883. printk (KERN_ERR "%s: HostError! IntStatus %4.4x.\n",
  884. dev->name, int_status);
  885. writew (GlobalReset | HostReset, ioaddr + ASICCtrl + 2);
  886. mdelay (500);
  887. }
  888. }
  889. static struct net_device_stats *
  890. get_stats (struct net_device *dev)
  891. {
  892. long ioaddr = dev->base_addr;
  893. struct netdev_private *np = netdev_priv(dev);
  894. #ifdef MEM_MAPPING
  895. int i;
  896. #endif
  897. unsigned int stat_reg;
  898. /* All statistics registers need to be acknowledged,
  899. else statistic overflow could cause problems */
  900. np->stats.rx_packets += readl (ioaddr + FramesRcvOk);
  901. np->stats.tx_packets += readl (ioaddr + FramesXmtOk);
  902. np->stats.rx_bytes += readl (ioaddr + OctetRcvOk);
  903. np->stats.tx_bytes += readl (ioaddr + OctetXmtOk);
  904. np->stats.multicast = readl (ioaddr + McstFramesRcvdOk);
  905. np->stats.collisions += readl (ioaddr + SingleColFrames)
  906. + readl (ioaddr + MultiColFrames);
  907. /* detailed tx errors */
  908. stat_reg = readw (ioaddr + FramesAbortXSColls);
  909. np->stats.tx_aborted_errors += stat_reg;
  910. np->stats.tx_errors += stat_reg;
  911. stat_reg = readw (ioaddr + CarrierSenseErrors);
  912. np->stats.tx_carrier_errors += stat_reg;
  913. np->stats.tx_errors += stat_reg;
  914. /* Clear all other statistic register. */
  915. readl (ioaddr + McstOctetXmtOk);
  916. readw (ioaddr + BcstFramesXmtdOk);
  917. readl (ioaddr + McstFramesXmtdOk);
  918. readw (ioaddr + BcstFramesRcvdOk);
  919. readw (ioaddr + MacControlFramesRcvd);
  920. readw (ioaddr + FrameTooLongErrors);
  921. readw (ioaddr + InRangeLengthErrors);
  922. readw (ioaddr + FramesCheckSeqErrors);
  923. readw (ioaddr + FramesLostRxErrors);
  924. readl (ioaddr + McstOctetXmtOk);
  925. readl (ioaddr + BcstOctetXmtOk);
  926. readl (ioaddr + McstFramesXmtdOk);
  927. readl (ioaddr + FramesWDeferredXmt);
  928. readl (ioaddr + LateCollisions);
  929. readw (ioaddr + BcstFramesXmtdOk);
  930. readw (ioaddr + MacControlFramesXmtd);
  931. readw (ioaddr + FramesWEXDeferal);
  932. #ifdef MEM_MAPPING
  933. for (i = 0x100; i <= 0x150; i += 4)
  934. readl (ioaddr + i);
  935. #endif
  936. readw (ioaddr + TxJumboFrames);
  937. readw (ioaddr + RxJumboFrames);
  938. readw (ioaddr + TCPCheckSumErrors);
  939. readw (ioaddr + UDPCheckSumErrors);
  940. readw (ioaddr + IPCheckSumErrors);
  941. return &np->stats;
  942. }
  943. static int
  944. clear_stats (struct net_device *dev)
  945. {
  946. long ioaddr = dev->base_addr;
  947. #ifdef MEM_MAPPING
  948. int i;
  949. #endif
  950. /* All statistics registers need to be acknowledged,
  951. else statistic overflow could cause problems */
  952. readl (ioaddr + FramesRcvOk);
  953. readl (ioaddr + FramesXmtOk);
  954. readl (ioaddr + OctetRcvOk);
  955. readl (ioaddr + OctetXmtOk);
  956. readl (ioaddr + McstFramesRcvdOk);
  957. readl (ioaddr + SingleColFrames);
  958. readl (ioaddr + MultiColFrames);
  959. readl (ioaddr + LateCollisions);
  960. /* detailed rx errors */
  961. readw (ioaddr + FrameTooLongErrors);
  962. readw (ioaddr + InRangeLengthErrors);
  963. readw (ioaddr + FramesCheckSeqErrors);
  964. readw (ioaddr + FramesLostRxErrors);
  965. /* detailed tx errors */
  966. readw (ioaddr + FramesAbortXSColls);
  967. readw (ioaddr + CarrierSenseErrors);
  968. /* Clear all other statistic register. */
  969. readl (ioaddr + McstOctetXmtOk);
  970. readw (ioaddr + BcstFramesXmtdOk);
  971. readl (ioaddr + McstFramesXmtdOk);
  972. readw (ioaddr + BcstFramesRcvdOk);
  973. readw (ioaddr + MacControlFramesRcvd);
  974. readl (ioaddr + McstOctetXmtOk);
  975. readl (ioaddr + BcstOctetXmtOk);
  976. readl (ioaddr + McstFramesXmtdOk);
  977. readl (ioaddr + FramesWDeferredXmt);
  978. readw (ioaddr + BcstFramesXmtdOk);
  979. readw (ioaddr + MacControlFramesXmtd);
  980. readw (ioaddr + FramesWEXDeferal);
  981. #ifdef MEM_MAPPING
  982. for (i = 0x100; i <= 0x150; i += 4)
  983. readl (ioaddr + i);
  984. #endif
  985. readw (ioaddr + TxJumboFrames);
  986. readw (ioaddr + RxJumboFrames);
  987. readw (ioaddr + TCPCheckSumErrors);
  988. readw (ioaddr + UDPCheckSumErrors);
  989. readw (ioaddr + IPCheckSumErrors);
  990. return 0;
  991. }
  992. static int
  993. change_mtu (struct net_device *dev, int new_mtu)
  994. {
  995. struct netdev_private *np = netdev_priv(dev);
  996. int max = (np->jumbo) ? MAX_JUMBO : 1536;
  997. if ((new_mtu < 68) || (new_mtu > max)) {
  998. return -EINVAL;
  999. }
  1000. dev->mtu = new_mtu;
  1001. return 0;
  1002. }
  1003. static void
  1004. set_multicast (struct net_device *dev)
  1005. {
  1006. long ioaddr = dev->base_addr;
  1007. u32 hash_table[2];
  1008. u16 rx_mode = 0;
  1009. struct netdev_private *np = netdev_priv(dev);
  1010. hash_table[0] = hash_table[1] = 0;
  1011. /* RxFlowcontrol DA: 01-80-C2-00-00-01. Hash index=0x39 */
  1012. hash_table[1] |= 0x02000000;
  1013. if (dev->flags & IFF_PROMISC) {
  1014. /* Receive all frames promiscuously. */
  1015. rx_mode = ReceiveAllFrames;
  1016. } else if ((dev->flags & IFF_ALLMULTI) ||
  1017. (dev->mc_count > multicast_filter_limit)) {
  1018. /* Receive broadcast and multicast frames */
  1019. rx_mode = ReceiveBroadcast | ReceiveMulticast | ReceiveUnicast;
  1020. } else if (dev->mc_count > 0) {
  1021. int i;
  1022. struct dev_mc_list *mclist;
  1023. /* Receive broadcast frames and multicast frames filtering
  1024. by Hashtable */
  1025. rx_mode =
  1026. ReceiveBroadcast | ReceiveMulticastHash | ReceiveUnicast;
  1027. for (i=0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  1028. i++, mclist=mclist->next)
  1029. {
  1030. int bit, index = 0;
  1031. int crc = ether_crc_le (ETH_ALEN, mclist->dmi_addr);
  1032. /* The inverted high significant 6 bits of CRC are
  1033. used as an index to hashtable */
  1034. for (bit = 0; bit < 6; bit++)
  1035. if (crc & (1 << (31 - bit)))
  1036. index |= (1 << bit);
  1037. hash_table[index / 32] |= (1 << (index % 32));
  1038. }
  1039. } else {
  1040. rx_mode = ReceiveBroadcast | ReceiveUnicast;
  1041. }
  1042. if (np->vlan) {
  1043. /* ReceiveVLANMatch field in ReceiveMode */
  1044. rx_mode |= ReceiveVLANMatch;
  1045. }
  1046. writel (hash_table[0], ioaddr + HashTable0);
  1047. writel (hash_table[1], ioaddr + HashTable1);
  1048. writew (rx_mode, ioaddr + ReceiveMode);
  1049. }
  1050. static void rio_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1051. {
  1052. struct netdev_private *np = netdev_priv(dev);
  1053. strcpy(info->driver, "dl2k");
  1054. strcpy(info->version, DRV_VERSION);
  1055. strcpy(info->bus_info, pci_name(np->pdev));
  1056. }
  1057. static int rio_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1058. {
  1059. struct netdev_private *np = netdev_priv(dev);
  1060. if (np->phy_media) {
  1061. /* fiber device */
  1062. cmd->supported = SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1063. cmd->advertising= ADVERTISED_Autoneg | ADVERTISED_FIBRE;
  1064. cmd->port = PORT_FIBRE;
  1065. cmd->transceiver = XCVR_INTERNAL;
  1066. } else {
  1067. /* copper device */
  1068. cmd->supported = SUPPORTED_10baseT_Half |
  1069. SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half
  1070. | SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full |
  1071. SUPPORTED_Autoneg | SUPPORTED_MII;
  1072. cmd->advertising = ADVERTISED_10baseT_Half |
  1073. ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Half |
  1074. ADVERTISED_100baseT_Full | ADVERTISED_1000baseT_Full|
  1075. ADVERTISED_Autoneg | ADVERTISED_MII;
  1076. cmd->port = PORT_MII;
  1077. cmd->transceiver = XCVR_INTERNAL;
  1078. }
  1079. if ( np->link_status ) {
  1080. cmd->speed = np->speed;
  1081. cmd->duplex = np->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1082. } else {
  1083. cmd->speed = -1;
  1084. cmd->duplex = -1;
  1085. }
  1086. if ( np->an_enable)
  1087. cmd->autoneg = AUTONEG_ENABLE;
  1088. else
  1089. cmd->autoneg = AUTONEG_DISABLE;
  1090. cmd->phy_address = np->phy_addr;
  1091. return 0;
  1092. }
  1093. static int rio_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1094. {
  1095. struct netdev_private *np = netdev_priv(dev);
  1096. netif_carrier_off(dev);
  1097. if (cmd->autoneg == AUTONEG_ENABLE) {
  1098. if (np->an_enable)
  1099. return 0;
  1100. else {
  1101. np->an_enable = 1;
  1102. mii_set_media(dev);
  1103. return 0;
  1104. }
  1105. } else {
  1106. np->an_enable = 0;
  1107. if (np->speed == 1000) {
  1108. cmd->speed = SPEED_100;
  1109. cmd->duplex = DUPLEX_FULL;
  1110. printk("Warning!! Can't disable Auto negotiation in 1000Mbps, change to Manual 100Mbps, Full duplex.\n");
  1111. }
  1112. switch(cmd->speed + cmd->duplex) {
  1113. case SPEED_10 + DUPLEX_HALF:
  1114. np->speed = 10;
  1115. np->full_duplex = 0;
  1116. break;
  1117. case SPEED_10 + DUPLEX_FULL:
  1118. np->speed = 10;
  1119. np->full_duplex = 1;
  1120. break;
  1121. case SPEED_100 + DUPLEX_HALF:
  1122. np->speed = 100;
  1123. np->full_duplex = 0;
  1124. break;
  1125. case SPEED_100 + DUPLEX_FULL:
  1126. np->speed = 100;
  1127. np->full_duplex = 1;
  1128. break;
  1129. case SPEED_1000 + DUPLEX_HALF:/* not supported */
  1130. case SPEED_1000 + DUPLEX_FULL:/* not supported */
  1131. default:
  1132. return -EINVAL;
  1133. }
  1134. mii_set_media(dev);
  1135. }
  1136. return 0;
  1137. }
  1138. static u32 rio_get_link(struct net_device *dev)
  1139. {
  1140. struct netdev_private *np = netdev_priv(dev);
  1141. return np->link_status;
  1142. }
  1143. static const struct ethtool_ops ethtool_ops = {
  1144. .get_drvinfo = rio_get_drvinfo,
  1145. .get_settings = rio_get_settings,
  1146. .set_settings = rio_set_settings,
  1147. .get_link = rio_get_link,
  1148. };
  1149. static int
  1150. rio_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
  1151. {
  1152. int phy_addr;
  1153. struct netdev_private *np = netdev_priv(dev);
  1154. struct mii_data *miidata = (struct mii_data *) &rq->ifr_ifru;
  1155. struct netdev_desc *desc;
  1156. int i;
  1157. phy_addr = np->phy_addr;
  1158. switch (cmd) {
  1159. case SIOCDEVPRIVATE:
  1160. break;
  1161. case SIOCDEVPRIVATE + 1:
  1162. miidata->out_value = mii_read (dev, phy_addr, miidata->reg_num);
  1163. break;
  1164. case SIOCDEVPRIVATE + 2:
  1165. mii_write (dev, phy_addr, miidata->reg_num, miidata->in_value);
  1166. break;
  1167. case SIOCDEVPRIVATE + 3:
  1168. break;
  1169. case SIOCDEVPRIVATE + 4:
  1170. break;
  1171. case SIOCDEVPRIVATE + 5:
  1172. netif_stop_queue (dev);
  1173. break;
  1174. case SIOCDEVPRIVATE + 6:
  1175. netif_wake_queue (dev);
  1176. break;
  1177. case SIOCDEVPRIVATE + 7:
  1178. printk
  1179. ("tx_full=%x cur_tx=%lx old_tx=%lx cur_rx=%lx old_rx=%lx\n",
  1180. netif_queue_stopped(dev), np->cur_tx, np->old_tx, np->cur_rx,
  1181. np->old_rx);
  1182. break;
  1183. case SIOCDEVPRIVATE + 8:
  1184. printk("TX ring:\n");
  1185. for (i = 0; i < TX_RING_SIZE; i++) {
  1186. desc = &np->tx_ring[i];
  1187. printk
  1188. ("%02x:cur:%08x next:%08x status:%08x frag1:%08x frag0:%08x",
  1189. i,
  1190. (u32) (np->tx_ring_dma + i * sizeof (*desc)),
  1191. (u32) desc->next_desc,
  1192. (u32) desc->status, (u32) (desc->fraginfo >> 32),
  1193. (u32) desc->fraginfo);
  1194. printk ("\n");
  1195. }
  1196. printk ("\n");
  1197. break;
  1198. default:
  1199. return -EOPNOTSUPP;
  1200. }
  1201. return 0;
  1202. }
  1203. #define EEP_READ 0x0200
  1204. #define EEP_BUSY 0x8000
  1205. /* Read the EEPROM word */
  1206. /* We use I/O instruction to read/write eeprom to avoid fail on some machines */
  1207. static int
  1208. read_eeprom (long ioaddr, int eep_addr)
  1209. {
  1210. int i = 1000;
  1211. outw (EEP_READ | (eep_addr & 0xff), ioaddr + EepromCtrl);
  1212. while (i-- > 0) {
  1213. if (!(inw (ioaddr + EepromCtrl) & EEP_BUSY)) {
  1214. return inw (ioaddr + EepromData);
  1215. }
  1216. }
  1217. return 0;
  1218. }
  1219. enum phy_ctrl_bits {
  1220. MII_READ = 0x00, MII_CLK = 0x01, MII_DATA1 = 0x02, MII_WRITE = 0x04,
  1221. MII_DUPLEX = 0x08,
  1222. };
  1223. #define mii_delay() readb(ioaddr)
  1224. static void
  1225. mii_sendbit (struct net_device *dev, u32 data)
  1226. {
  1227. long ioaddr = dev->base_addr + PhyCtrl;
  1228. data = (data) ? MII_DATA1 : 0;
  1229. data |= MII_WRITE;
  1230. data |= (readb (ioaddr) & 0xf8) | MII_WRITE;
  1231. writeb (data, ioaddr);
  1232. mii_delay ();
  1233. writeb (data | MII_CLK, ioaddr);
  1234. mii_delay ();
  1235. }
  1236. static int
  1237. mii_getbit (struct net_device *dev)
  1238. {
  1239. long ioaddr = dev->base_addr + PhyCtrl;
  1240. u8 data;
  1241. data = (readb (ioaddr) & 0xf8) | MII_READ;
  1242. writeb (data, ioaddr);
  1243. mii_delay ();
  1244. writeb (data | MII_CLK, ioaddr);
  1245. mii_delay ();
  1246. return ((readb (ioaddr) >> 1) & 1);
  1247. }
  1248. static void
  1249. mii_send_bits (struct net_device *dev, u32 data, int len)
  1250. {
  1251. int i;
  1252. for (i = len - 1; i >= 0; i--) {
  1253. mii_sendbit (dev, data & (1 << i));
  1254. }
  1255. }
  1256. static int
  1257. mii_read (struct net_device *dev, int phy_addr, int reg_num)
  1258. {
  1259. u32 cmd;
  1260. int i;
  1261. u32 retval = 0;
  1262. /* Preamble */
  1263. mii_send_bits (dev, 0xffffffff, 32);
  1264. /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
  1265. /* ST,OP = 0110'b for read operation */
  1266. cmd = (0x06 << 10 | phy_addr << 5 | reg_num);
  1267. mii_send_bits (dev, cmd, 14);
  1268. /* Turnaround */
  1269. if (mii_getbit (dev))
  1270. goto err_out;
  1271. /* Read data */
  1272. for (i = 0; i < 16; i++) {
  1273. retval |= mii_getbit (dev);
  1274. retval <<= 1;
  1275. }
  1276. /* End cycle */
  1277. mii_getbit (dev);
  1278. return (retval >> 1) & 0xffff;
  1279. err_out:
  1280. return 0;
  1281. }
  1282. static int
  1283. mii_write (struct net_device *dev, int phy_addr, int reg_num, u16 data)
  1284. {
  1285. u32 cmd;
  1286. /* Preamble */
  1287. mii_send_bits (dev, 0xffffffff, 32);
  1288. /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
  1289. /* ST,OP,AAAAA,RRRRR,TA = 0101xxxxxxxxxx10'b = 0x5002 for write */
  1290. cmd = (0x5002 << 16) | (phy_addr << 23) | (reg_num << 18) | data;
  1291. mii_send_bits (dev, cmd, 32);
  1292. /* End cycle */
  1293. mii_getbit (dev);
  1294. return 0;
  1295. }
  1296. static int
  1297. mii_wait_link (struct net_device *dev, int wait)
  1298. {
  1299. __u16 bmsr;
  1300. int phy_addr;
  1301. struct netdev_private *np;
  1302. np = netdev_priv(dev);
  1303. phy_addr = np->phy_addr;
  1304. do {
  1305. bmsr = mii_read (dev, phy_addr, MII_BMSR);
  1306. if (bmsr & MII_BMSR_LINK_STATUS)
  1307. return 0;
  1308. mdelay (1);
  1309. } while (--wait > 0);
  1310. return -1;
  1311. }
  1312. static int
  1313. mii_get_media (struct net_device *dev)
  1314. {
  1315. __u16 negotiate;
  1316. __u16 bmsr;
  1317. __u16 mscr;
  1318. __u16 mssr;
  1319. int phy_addr;
  1320. struct netdev_private *np;
  1321. np = netdev_priv(dev);
  1322. phy_addr = np->phy_addr;
  1323. bmsr = mii_read (dev, phy_addr, MII_BMSR);
  1324. if (np->an_enable) {
  1325. if (!(bmsr & MII_BMSR_AN_COMPLETE)) {
  1326. /* Auto-Negotiation not completed */
  1327. return -1;
  1328. }
  1329. negotiate = mii_read (dev, phy_addr, MII_ANAR) &
  1330. mii_read (dev, phy_addr, MII_ANLPAR);
  1331. mscr = mii_read (dev, phy_addr, MII_MSCR);
  1332. mssr = mii_read (dev, phy_addr, MII_MSSR);
  1333. if (mscr & MII_MSCR_1000BT_FD && mssr & MII_MSSR_LP_1000BT_FD) {
  1334. np->speed = 1000;
  1335. np->full_duplex = 1;
  1336. printk (KERN_INFO "Auto 1000 Mbps, Full duplex\n");
  1337. } else if (mscr & MII_MSCR_1000BT_HD && mssr & MII_MSSR_LP_1000BT_HD) {
  1338. np->speed = 1000;
  1339. np->full_duplex = 0;
  1340. printk (KERN_INFO "Auto 1000 Mbps, Half duplex\n");
  1341. } else if (negotiate & MII_ANAR_100BX_FD) {
  1342. np->speed = 100;
  1343. np->full_duplex = 1;
  1344. printk (KERN_INFO "Auto 100 Mbps, Full duplex\n");
  1345. } else if (negotiate & MII_ANAR_100BX_HD) {
  1346. np->speed = 100;
  1347. np->full_duplex = 0;
  1348. printk (KERN_INFO "Auto 100 Mbps, Half duplex\n");
  1349. } else if (negotiate & MII_ANAR_10BT_FD) {
  1350. np->speed = 10;
  1351. np->full_duplex = 1;
  1352. printk (KERN_INFO "Auto 10 Mbps, Full duplex\n");
  1353. } else if (negotiate & MII_ANAR_10BT_HD) {
  1354. np->speed = 10;
  1355. np->full_duplex = 0;
  1356. printk (KERN_INFO "Auto 10 Mbps, Half duplex\n");
  1357. }
  1358. if (negotiate & MII_ANAR_PAUSE) {
  1359. np->tx_flow &= 1;
  1360. np->rx_flow &= 1;
  1361. } else if (negotiate & MII_ANAR_ASYMMETRIC) {
  1362. np->tx_flow = 0;
  1363. np->rx_flow &= 1;
  1364. }
  1365. /* else tx_flow, rx_flow = user select */
  1366. } else {
  1367. __u16 bmcr = mii_read (dev, phy_addr, MII_BMCR);
  1368. switch (bmcr & (MII_BMCR_SPEED_100 | MII_BMCR_SPEED_1000)) {
  1369. case MII_BMCR_SPEED_1000:
  1370. printk (KERN_INFO "Operating at 1000 Mbps, ");
  1371. break;
  1372. case MII_BMCR_SPEED_100:
  1373. printk (KERN_INFO "Operating at 100 Mbps, ");
  1374. break;
  1375. case 0:
  1376. printk (KERN_INFO "Operating at 10 Mbps, ");
  1377. }
  1378. if (bmcr & MII_BMCR_DUPLEX_MODE) {
  1379. printk ("Full duplex\n");
  1380. } else {
  1381. printk ("Half duplex\n");
  1382. }
  1383. }
  1384. if (np->tx_flow)
  1385. printk(KERN_INFO "Enable Tx Flow Control\n");
  1386. else
  1387. printk(KERN_INFO "Disable Tx Flow Control\n");
  1388. if (np->rx_flow)
  1389. printk(KERN_INFO "Enable Rx Flow Control\n");
  1390. else
  1391. printk(KERN_INFO "Disable Rx Flow Control\n");
  1392. return 0;
  1393. }
  1394. static int
  1395. mii_set_media (struct net_device *dev)
  1396. {
  1397. __u16 pscr;
  1398. __u16 bmcr;
  1399. __u16 bmsr;
  1400. __u16 anar;
  1401. int phy_addr;
  1402. struct netdev_private *np;
  1403. np = netdev_priv(dev);
  1404. phy_addr = np->phy_addr;
  1405. /* Does user set speed? */
  1406. if (np->an_enable) {
  1407. /* Advertise capabilities */
  1408. bmsr = mii_read (dev, phy_addr, MII_BMSR);
  1409. anar = mii_read (dev, phy_addr, MII_ANAR) &
  1410. ~MII_ANAR_100BX_FD &
  1411. ~MII_ANAR_100BX_HD &
  1412. ~MII_ANAR_100BT4 &
  1413. ~MII_ANAR_10BT_FD &
  1414. ~MII_ANAR_10BT_HD;
  1415. if (bmsr & MII_BMSR_100BX_FD)
  1416. anar |= MII_ANAR_100BX_FD;
  1417. if (bmsr & MII_BMSR_100BX_HD)
  1418. anar |= MII_ANAR_100BX_HD;
  1419. if (bmsr & MII_BMSR_100BT4)
  1420. anar |= MII_ANAR_100BT4;
  1421. if (bmsr & MII_BMSR_10BT_FD)
  1422. anar |= MII_ANAR_10BT_FD;
  1423. if (bmsr & MII_BMSR_10BT_HD)
  1424. anar |= MII_ANAR_10BT_HD;
  1425. anar |= MII_ANAR_PAUSE | MII_ANAR_ASYMMETRIC;
  1426. mii_write (dev, phy_addr, MII_ANAR, anar);
  1427. /* Enable Auto crossover */
  1428. pscr = mii_read (dev, phy_addr, MII_PHY_SCR);
  1429. pscr |= 3 << 5; /* 11'b */
  1430. mii_write (dev, phy_addr, MII_PHY_SCR, pscr);
  1431. /* Soft reset PHY */
  1432. mii_write (dev, phy_addr, MII_BMCR, MII_BMCR_RESET);
  1433. bmcr = MII_BMCR_AN_ENABLE | MII_BMCR_RESTART_AN | MII_BMCR_RESET;
  1434. mii_write (dev, phy_addr, MII_BMCR, bmcr);
  1435. mdelay(1);
  1436. } else {
  1437. /* Force speed setting */
  1438. /* 1) Disable Auto crossover */
  1439. pscr = mii_read (dev, phy_addr, MII_PHY_SCR);
  1440. pscr &= ~(3 << 5);
  1441. mii_write (dev, phy_addr, MII_PHY_SCR, pscr);
  1442. /* 2) PHY Reset */
  1443. bmcr = mii_read (dev, phy_addr, MII_BMCR);
  1444. bmcr |= MII_BMCR_RESET;
  1445. mii_write (dev, phy_addr, MII_BMCR, bmcr);
  1446. /* 3) Power Down */
  1447. bmcr = 0x1940; /* must be 0x1940 */
  1448. mii_write (dev, phy_addr, MII_BMCR, bmcr);
  1449. mdelay (100); /* wait a certain time */
  1450. /* 4) Advertise nothing */
  1451. mii_write (dev, phy_addr, MII_ANAR, 0);
  1452. /* 5) Set media and Power Up */
  1453. bmcr = MII_BMCR_POWER_DOWN;
  1454. if (np->speed == 100) {
  1455. bmcr |= MII_BMCR_SPEED_100;
  1456. printk (KERN_INFO "Manual 100 Mbps, ");
  1457. } else if (np->speed == 10) {
  1458. printk (KERN_INFO "Manual 10 Mbps, ");
  1459. }
  1460. if (np->full_duplex) {
  1461. bmcr |= MII_BMCR_DUPLEX_MODE;
  1462. printk ("Full duplex\n");
  1463. } else {
  1464. printk ("Half duplex\n");
  1465. }
  1466. #if 0
  1467. /* Set 1000BaseT Master/Slave setting */
  1468. mscr = mii_read (dev, phy_addr, MII_MSCR);
  1469. mscr |= MII_MSCR_CFG_ENABLE;
  1470. mscr &= ~MII_MSCR_CFG_VALUE = 0;
  1471. #endif
  1472. mii_write (dev, phy_addr, MII_BMCR, bmcr);
  1473. mdelay(10);
  1474. }
  1475. return 0;
  1476. }
  1477. static int
  1478. mii_get_media_pcs (struct net_device *dev)
  1479. {
  1480. __u16 negotiate;
  1481. __u16 bmsr;
  1482. int phy_addr;
  1483. struct netdev_private *np;
  1484. np = netdev_priv(dev);
  1485. phy_addr = np->phy_addr;
  1486. bmsr = mii_read (dev, phy_addr, PCS_BMSR);
  1487. if (np->an_enable) {
  1488. if (!(bmsr & MII_BMSR_AN_COMPLETE)) {
  1489. /* Auto-Negotiation not completed */
  1490. return -1;
  1491. }
  1492. negotiate = mii_read (dev, phy_addr, PCS_ANAR) &
  1493. mii_read (dev, phy_addr, PCS_ANLPAR);
  1494. np->speed = 1000;
  1495. if (negotiate & PCS_ANAR_FULL_DUPLEX) {
  1496. printk (KERN_INFO "Auto 1000 Mbps, Full duplex\n");
  1497. np->full_duplex = 1;
  1498. } else {
  1499. printk (KERN_INFO "Auto 1000 Mbps, half duplex\n");
  1500. np->full_duplex = 0;
  1501. }
  1502. if (negotiate & PCS_ANAR_PAUSE) {
  1503. np->tx_flow &= 1;
  1504. np->rx_flow &= 1;
  1505. } else if (negotiate & PCS_ANAR_ASYMMETRIC) {
  1506. np->tx_flow = 0;
  1507. np->rx_flow &= 1;
  1508. }
  1509. /* else tx_flow, rx_flow = user select */
  1510. } else {
  1511. __u16 bmcr = mii_read (dev, phy_addr, PCS_BMCR);
  1512. printk (KERN_INFO "Operating at 1000 Mbps, ");
  1513. if (bmcr & MII_BMCR_DUPLEX_MODE) {
  1514. printk ("Full duplex\n");
  1515. } else {
  1516. printk ("Half duplex\n");
  1517. }
  1518. }
  1519. if (np->tx_flow)
  1520. printk(KERN_INFO "Enable Tx Flow Control\n");
  1521. else
  1522. printk(KERN_INFO "Disable Tx Flow Control\n");
  1523. if (np->rx_flow)
  1524. printk(KERN_INFO "Enable Rx Flow Control\n");
  1525. else
  1526. printk(KERN_INFO "Disable Rx Flow Control\n");
  1527. return 0;
  1528. }
  1529. static int
  1530. mii_set_media_pcs (struct net_device *dev)
  1531. {
  1532. __u16 bmcr;
  1533. __u16 esr;
  1534. __u16 anar;
  1535. int phy_addr;
  1536. struct netdev_private *np;
  1537. np = netdev_priv(dev);
  1538. phy_addr = np->phy_addr;
  1539. /* Auto-Negotiation? */
  1540. if (np->an_enable) {
  1541. /* Advertise capabilities */
  1542. esr = mii_read (dev, phy_addr, PCS_ESR);
  1543. anar = mii_read (dev, phy_addr, MII_ANAR) &
  1544. ~PCS_ANAR_HALF_DUPLEX &
  1545. ~PCS_ANAR_FULL_DUPLEX;
  1546. if (esr & (MII_ESR_1000BT_HD | MII_ESR_1000BX_HD))
  1547. anar |= PCS_ANAR_HALF_DUPLEX;
  1548. if (esr & (MII_ESR_1000BT_FD | MII_ESR_1000BX_FD))
  1549. anar |= PCS_ANAR_FULL_DUPLEX;
  1550. anar |= PCS_ANAR_PAUSE | PCS_ANAR_ASYMMETRIC;
  1551. mii_write (dev, phy_addr, MII_ANAR, anar);
  1552. /* Soft reset PHY */
  1553. mii_write (dev, phy_addr, MII_BMCR, MII_BMCR_RESET);
  1554. bmcr = MII_BMCR_AN_ENABLE | MII_BMCR_RESTART_AN |
  1555. MII_BMCR_RESET;
  1556. mii_write (dev, phy_addr, MII_BMCR, bmcr);
  1557. mdelay(1);
  1558. } else {
  1559. /* Force speed setting */
  1560. /* PHY Reset */
  1561. bmcr = MII_BMCR_RESET;
  1562. mii_write (dev, phy_addr, MII_BMCR, bmcr);
  1563. mdelay(10);
  1564. if (np->full_duplex) {
  1565. bmcr = MII_BMCR_DUPLEX_MODE;
  1566. printk (KERN_INFO "Manual full duplex\n");
  1567. } else {
  1568. bmcr = 0;
  1569. printk (KERN_INFO "Manual half duplex\n");
  1570. }
  1571. mii_write (dev, phy_addr, MII_BMCR, bmcr);
  1572. mdelay(10);
  1573. /* Advertise nothing */
  1574. mii_write (dev, phy_addr, MII_ANAR, 0);
  1575. }
  1576. return 0;
  1577. }
  1578. static int
  1579. rio_close (struct net_device *dev)
  1580. {
  1581. long ioaddr = dev->base_addr;
  1582. struct netdev_private *np = netdev_priv(dev);
  1583. struct sk_buff *skb;
  1584. int i;
  1585. netif_stop_queue (dev);
  1586. /* Disable interrupts */
  1587. writew (0, ioaddr + IntEnable);
  1588. /* Stop Tx and Rx logics */
  1589. writel (TxDisable | RxDisable | StatsDisable, ioaddr + MACCtrl);
  1590. synchronize_irq (dev->irq);
  1591. free_irq (dev->irq, dev);
  1592. del_timer_sync (&np->timer);
  1593. /* Free all the skbuffs in the queue. */
  1594. for (i = 0; i < RX_RING_SIZE; i++) {
  1595. np->rx_ring[i].status = 0;
  1596. np->rx_ring[i].fraginfo = 0;
  1597. skb = np->rx_skbuff[i];
  1598. if (skb) {
  1599. pci_unmap_single(np->pdev,
  1600. desc_to_dma(&np->rx_ring[i]),
  1601. skb->len, PCI_DMA_FROMDEVICE);
  1602. dev_kfree_skb (skb);
  1603. np->rx_skbuff[i] = NULL;
  1604. }
  1605. }
  1606. for (i = 0; i < TX_RING_SIZE; i++) {
  1607. skb = np->tx_skbuff[i];
  1608. if (skb) {
  1609. pci_unmap_single(np->pdev,
  1610. desc_to_dma(&np->tx_ring[i]),
  1611. skb->len, PCI_DMA_TODEVICE);
  1612. dev_kfree_skb (skb);
  1613. np->tx_skbuff[i] = NULL;
  1614. }
  1615. }
  1616. return 0;
  1617. }
  1618. static void __devexit
  1619. rio_remove1 (struct pci_dev *pdev)
  1620. {
  1621. struct net_device *dev = pci_get_drvdata (pdev);
  1622. if (dev) {
  1623. struct netdev_private *np = netdev_priv(dev);
  1624. unregister_netdev (dev);
  1625. pci_free_consistent (pdev, RX_TOTAL_SIZE, np->rx_ring,
  1626. np->rx_ring_dma);
  1627. pci_free_consistent (pdev, TX_TOTAL_SIZE, np->tx_ring,
  1628. np->tx_ring_dma);
  1629. #ifdef MEM_MAPPING
  1630. iounmap ((char *) (dev->base_addr));
  1631. #endif
  1632. free_netdev (dev);
  1633. pci_release_regions (pdev);
  1634. pci_disable_device (pdev);
  1635. }
  1636. pci_set_drvdata (pdev, NULL);
  1637. }
  1638. static struct pci_driver rio_driver = {
  1639. .name = "dl2k",
  1640. .id_table = rio_pci_tbl,
  1641. .probe = rio_probe1,
  1642. .remove = __devexit_p(rio_remove1),
  1643. };
  1644. static int __init
  1645. rio_init (void)
  1646. {
  1647. return pci_register_driver(&rio_driver);
  1648. }
  1649. static void __exit
  1650. rio_exit (void)
  1651. {
  1652. pci_unregister_driver (&rio_driver);
  1653. }
  1654. module_init (rio_init);
  1655. module_exit (rio_exit);
  1656. /*
  1657. Compile command:
  1658. gcc -D__KERNEL__ -DMODULE -I/usr/src/linux/include -Wall -Wstrict-prototypes -O2 -c dl2k.c
  1659. Read Documentation/networking/dl2k.txt for details.
  1660. */