dl2k.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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. ((u16 *) sromdata)[i] = le16_to_cpu (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 (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 (np->rx_buf_sz) << 48;
  526. }
  527. /* Set RFDListPtr */
  528. writel (cpu_to_le32 (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 =
  557. cpu_to_le64 (VLANTagInsert) |
  558. (cpu_to_le64 (np->vlan) << 32) |
  559. (cpu_to_le64 (skb->priority) << 45);
  560. }
  561. txdesc->fraginfo = cpu_to_le64 (pci_map_single (np->pdev, skb->data,
  562. skb->len,
  563. PCI_DMA_TODEVICE));
  564. txdesc->fraginfo |= cpu_to_le64 (skb->len) << 48;
  565. /* DL2K bug: DMA fails to get next descriptor ptr in 10Mbps mode
  566. * Work around: Always use 1 descriptor in 10Mbps mode */
  567. if (entry % np->tx_coalesce == 0 || np->speed == 10)
  568. txdesc->status = cpu_to_le64 (entry | tfc_vlan_tag |
  569. WordAlignDisable |
  570. TxDMAIndicate |
  571. (1 << FragCountShift));
  572. else
  573. txdesc->status = cpu_to_le64 (entry | tfc_vlan_tag |
  574. WordAlignDisable |
  575. (1 << FragCountShift));
  576. /* TxDMAPollNow */
  577. writel (readl (ioaddr + DMACtrl) | 0x00001000, ioaddr + DMACtrl);
  578. /* Schedule ISR */
  579. writel(10000, ioaddr + CountDown);
  580. np->cur_tx = (np->cur_tx + 1) % TX_RING_SIZE;
  581. if ((np->cur_tx - np->old_tx + TX_RING_SIZE) % TX_RING_SIZE
  582. < TX_QUEUE_LEN - 1 && np->speed != 10) {
  583. /* do nothing */
  584. } else if (!netif_queue_stopped(dev)) {
  585. netif_stop_queue (dev);
  586. }
  587. /* The first TFDListPtr */
  588. if (readl (dev->base_addr + TFDListPtr0) == 0) {
  589. writel (np->tx_ring_dma + entry * sizeof (struct netdev_desc),
  590. dev->base_addr + TFDListPtr0);
  591. writel (0, dev->base_addr + TFDListPtr1);
  592. }
  593. /* NETDEV WATCHDOG timer */
  594. dev->trans_start = jiffies;
  595. return 0;
  596. }
  597. static irqreturn_t
  598. rio_interrupt (int irq, void *dev_instance)
  599. {
  600. struct net_device *dev = dev_instance;
  601. struct netdev_private *np;
  602. unsigned int_status;
  603. long ioaddr;
  604. int cnt = max_intrloop;
  605. int handled = 0;
  606. ioaddr = dev->base_addr;
  607. np = netdev_priv(dev);
  608. while (1) {
  609. int_status = readw (ioaddr + IntStatus);
  610. writew (int_status, ioaddr + IntStatus);
  611. int_status &= DEFAULT_INTR;
  612. if (int_status == 0 || --cnt < 0)
  613. break;
  614. handled = 1;
  615. /* Processing received packets */
  616. if (int_status & RxDMAComplete)
  617. receive_packet (dev);
  618. /* TxDMAComplete interrupt */
  619. if ((int_status & (TxDMAComplete|IntRequested))) {
  620. int tx_status;
  621. tx_status = readl (ioaddr + TxStatus);
  622. if (tx_status & 0x01)
  623. tx_error (dev, tx_status);
  624. /* Free used tx skbuffs */
  625. rio_free_tx (dev, 1);
  626. }
  627. /* Handle uncommon events */
  628. if (int_status &
  629. (HostError | LinkEvent | UpdateStats))
  630. rio_error (dev, int_status);
  631. }
  632. if (np->cur_tx != np->old_tx)
  633. writel (100, ioaddr + CountDown);
  634. return IRQ_RETVAL(handled);
  635. }
  636. static void
  637. rio_free_tx (struct net_device *dev, int irq)
  638. {
  639. struct netdev_private *np = netdev_priv(dev);
  640. int entry = np->old_tx % TX_RING_SIZE;
  641. int tx_use = 0;
  642. unsigned long flag = 0;
  643. if (irq)
  644. spin_lock(&np->tx_lock);
  645. else
  646. spin_lock_irqsave(&np->tx_lock, flag);
  647. /* Free used tx skbuffs */
  648. while (entry != np->cur_tx) {
  649. struct sk_buff *skb;
  650. if (!(np->tx_ring[entry].status & TFDDone))
  651. break;
  652. skb = np->tx_skbuff[entry];
  653. pci_unmap_single (np->pdev,
  654. np->tx_ring[entry].fraginfo & DMA_48BIT_MASK,
  655. skb->len, PCI_DMA_TODEVICE);
  656. if (irq)
  657. dev_kfree_skb_irq (skb);
  658. else
  659. dev_kfree_skb (skb);
  660. np->tx_skbuff[entry] = NULL;
  661. entry = (entry + 1) % TX_RING_SIZE;
  662. tx_use++;
  663. }
  664. if (irq)
  665. spin_unlock(&np->tx_lock);
  666. else
  667. spin_unlock_irqrestore(&np->tx_lock, flag);
  668. np->old_tx = entry;
  669. /* If the ring is no longer full, clear tx_full and
  670. call netif_wake_queue() */
  671. if (netif_queue_stopped(dev) &&
  672. ((np->cur_tx - np->old_tx + TX_RING_SIZE) % TX_RING_SIZE
  673. < TX_QUEUE_LEN - 1 || np->speed == 10)) {
  674. netif_wake_queue (dev);
  675. }
  676. }
  677. static void
  678. tx_error (struct net_device *dev, int tx_status)
  679. {
  680. struct netdev_private *np;
  681. long ioaddr = dev->base_addr;
  682. int frame_id;
  683. int i;
  684. np = netdev_priv(dev);
  685. frame_id = (tx_status & 0xffff0000);
  686. printk (KERN_ERR "%s: Transmit error, TxStatus %4.4x, FrameId %d.\n",
  687. dev->name, tx_status, frame_id);
  688. np->stats.tx_errors++;
  689. /* Ttransmit Underrun */
  690. if (tx_status & 0x10) {
  691. np->stats.tx_fifo_errors++;
  692. writew (readw (ioaddr + TxStartThresh) + 0x10,
  693. ioaddr + TxStartThresh);
  694. /* Transmit Underrun need to set TxReset, DMARest, FIFOReset */
  695. writew (TxReset | DMAReset | FIFOReset | NetworkReset,
  696. ioaddr + ASICCtrl + 2);
  697. /* Wait for ResetBusy bit clear */
  698. for (i = 50; i > 0; i--) {
  699. if ((readw (ioaddr + ASICCtrl + 2) & ResetBusy) == 0)
  700. break;
  701. mdelay (1);
  702. }
  703. rio_free_tx (dev, 1);
  704. /* Reset TFDListPtr */
  705. writel (np->tx_ring_dma +
  706. np->old_tx * sizeof (struct netdev_desc),
  707. dev->base_addr + TFDListPtr0);
  708. writel (0, dev->base_addr + TFDListPtr1);
  709. /* Let TxStartThresh stay default value */
  710. }
  711. /* Late Collision */
  712. if (tx_status & 0x04) {
  713. np->stats.tx_fifo_errors++;
  714. /* TxReset and clear FIFO */
  715. writew (TxReset | FIFOReset, ioaddr + ASICCtrl + 2);
  716. /* Wait reset done */
  717. for (i = 50; i > 0; i--) {
  718. if ((readw (ioaddr + ASICCtrl + 2) & ResetBusy) == 0)
  719. break;
  720. mdelay (1);
  721. }
  722. /* Let TxStartThresh stay default value */
  723. }
  724. /* Maximum Collisions */
  725. #ifdef ETHER_STATS
  726. if (tx_status & 0x08)
  727. np->stats.collisions16++;
  728. #else
  729. if (tx_status & 0x08)
  730. np->stats.collisions++;
  731. #endif
  732. /* Restart the Tx */
  733. writel (readw (dev->base_addr + MACCtrl) | TxEnable, ioaddr + MACCtrl);
  734. }
  735. static int
  736. receive_packet (struct net_device *dev)
  737. {
  738. struct netdev_private *np = netdev_priv(dev);
  739. int entry = np->cur_rx % RX_RING_SIZE;
  740. int cnt = 30;
  741. /* If RFDDone, FrameStart and FrameEnd set, there is a new packet in. */
  742. while (1) {
  743. struct netdev_desc *desc = &np->rx_ring[entry];
  744. int pkt_len;
  745. u64 frame_status;
  746. if (!(desc->status & RFDDone) ||
  747. !(desc->status & FrameStart) || !(desc->status & FrameEnd))
  748. break;
  749. /* Chip omits the CRC. */
  750. pkt_len = le64_to_cpu (desc->status & 0xffff);
  751. frame_status = le64_to_cpu (desc->status);
  752. if (--cnt < 0)
  753. break;
  754. /* Update rx error statistics, drop packet. */
  755. if (frame_status & RFS_Errors) {
  756. np->stats.rx_errors++;
  757. if (frame_status & (RxRuntFrame | RxLengthError))
  758. np->stats.rx_length_errors++;
  759. if (frame_status & RxFCSError)
  760. np->stats.rx_crc_errors++;
  761. if (frame_status & RxAlignmentError && np->speed != 1000)
  762. np->stats.rx_frame_errors++;
  763. if (frame_status & RxFIFOOverrun)
  764. np->stats.rx_fifo_errors++;
  765. } else {
  766. struct sk_buff *skb;
  767. /* Small skbuffs for short packets */
  768. if (pkt_len > copy_thresh) {
  769. pci_unmap_single (np->pdev,
  770. desc->fraginfo & DMA_48BIT_MASK,
  771. np->rx_buf_sz,
  772. PCI_DMA_FROMDEVICE);
  773. skb_put (skb = np->rx_skbuff[entry], pkt_len);
  774. np->rx_skbuff[entry] = NULL;
  775. } else if ((skb = dev_alloc_skb (pkt_len + 2)) != NULL) {
  776. pci_dma_sync_single_for_cpu(np->pdev,
  777. desc->fraginfo &
  778. DMA_48BIT_MASK,
  779. np->rx_buf_sz,
  780. PCI_DMA_FROMDEVICE);
  781. /* 16 byte align the IP header */
  782. skb_reserve (skb, 2);
  783. skb_copy_to_linear_data (skb,
  784. np->rx_skbuff[entry]->data,
  785. pkt_len);
  786. skb_put (skb, pkt_len);
  787. pci_dma_sync_single_for_device(np->pdev,
  788. desc->fraginfo &
  789. DMA_48BIT_MASK,
  790. np->rx_buf_sz,
  791. PCI_DMA_FROMDEVICE);
  792. }
  793. skb->protocol = eth_type_trans (skb, dev);
  794. #if 0
  795. /* Checksum done by hw, but csum value unavailable. */
  796. if (np->pdev->pci_rev_id >= 0x0c &&
  797. !(frame_status & (TCPError | UDPError | IPError))) {
  798. skb->ip_summed = CHECKSUM_UNNECESSARY;
  799. }
  800. #endif
  801. netif_rx (skb);
  802. dev->last_rx = jiffies;
  803. }
  804. entry = (entry + 1) % RX_RING_SIZE;
  805. }
  806. spin_lock(&np->rx_lock);
  807. np->cur_rx = entry;
  808. /* Re-allocate skbuffs to fill the descriptor ring */
  809. entry = np->old_rx;
  810. while (entry != np->cur_rx) {
  811. struct sk_buff *skb;
  812. /* Dropped packets don't need to re-allocate */
  813. if (np->rx_skbuff[entry] == NULL) {
  814. skb = dev_alloc_skb (np->rx_buf_sz);
  815. if (skb == NULL) {
  816. np->rx_ring[entry].fraginfo = 0;
  817. printk (KERN_INFO
  818. "%s: receive_packet: "
  819. "Unable to re-allocate Rx skbuff.#%d\n",
  820. dev->name, entry);
  821. break;
  822. }
  823. np->rx_skbuff[entry] = skb;
  824. /* 16 byte align the IP header */
  825. skb_reserve (skb, 2);
  826. np->rx_ring[entry].fraginfo =
  827. cpu_to_le64 (pci_map_single
  828. (np->pdev, skb->data, np->rx_buf_sz,
  829. PCI_DMA_FROMDEVICE));
  830. }
  831. np->rx_ring[entry].fraginfo |=
  832. cpu_to_le64 (np->rx_buf_sz) << 48;
  833. np->rx_ring[entry].status = 0;
  834. entry = (entry + 1) % RX_RING_SIZE;
  835. }
  836. np->old_rx = entry;
  837. spin_unlock(&np->rx_lock);
  838. return 0;
  839. }
  840. static void
  841. rio_error (struct net_device *dev, int int_status)
  842. {
  843. long ioaddr = dev->base_addr;
  844. struct netdev_private *np = netdev_priv(dev);
  845. u16 macctrl;
  846. /* Link change event */
  847. if (int_status & LinkEvent) {
  848. if (mii_wait_link (dev, 10) == 0) {
  849. printk (KERN_INFO "%s: Link up\n", dev->name);
  850. if (np->phy_media)
  851. mii_get_media_pcs (dev);
  852. else
  853. mii_get_media (dev);
  854. if (np->speed == 1000)
  855. np->tx_coalesce = tx_coalesce;
  856. else
  857. np->tx_coalesce = 1;
  858. macctrl = 0;
  859. macctrl |= (np->vlan) ? AutoVLANuntagging : 0;
  860. macctrl |= (np->full_duplex) ? DuplexSelect : 0;
  861. macctrl |= (np->tx_flow) ?
  862. TxFlowControlEnable : 0;
  863. macctrl |= (np->rx_flow) ?
  864. RxFlowControlEnable : 0;
  865. writew(macctrl, ioaddr + MACCtrl);
  866. np->link_status = 1;
  867. netif_carrier_on(dev);
  868. } else {
  869. printk (KERN_INFO "%s: Link off\n", dev->name);
  870. np->link_status = 0;
  871. netif_carrier_off(dev);
  872. }
  873. }
  874. /* UpdateStats statistics registers */
  875. if (int_status & UpdateStats) {
  876. get_stats (dev);
  877. }
  878. /* PCI Error, a catastronphic error related to the bus interface
  879. occurs, set GlobalReset and HostReset to reset. */
  880. if (int_status & HostError) {
  881. printk (KERN_ERR "%s: HostError! IntStatus %4.4x.\n",
  882. dev->name, int_status);
  883. writew (GlobalReset | HostReset, ioaddr + ASICCtrl + 2);
  884. mdelay (500);
  885. }
  886. }
  887. static struct net_device_stats *
  888. get_stats (struct net_device *dev)
  889. {
  890. long ioaddr = dev->base_addr;
  891. struct netdev_private *np = netdev_priv(dev);
  892. #ifdef MEM_MAPPING
  893. int i;
  894. #endif
  895. unsigned int stat_reg;
  896. /* All statistics registers need to be acknowledged,
  897. else statistic overflow could cause problems */
  898. np->stats.rx_packets += readl (ioaddr + FramesRcvOk);
  899. np->stats.tx_packets += readl (ioaddr + FramesXmtOk);
  900. np->stats.rx_bytes += readl (ioaddr + OctetRcvOk);
  901. np->stats.tx_bytes += readl (ioaddr + OctetXmtOk);
  902. np->stats.multicast = readl (ioaddr + McstFramesRcvdOk);
  903. np->stats.collisions += readl (ioaddr + SingleColFrames)
  904. + readl (ioaddr + MultiColFrames);
  905. /* detailed tx errors */
  906. stat_reg = readw (ioaddr + FramesAbortXSColls);
  907. np->stats.tx_aborted_errors += stat_reg;
  908. np->stats.tx_errors += stat_reg;
  909. stat_reg = readw (ioaddr + CarrierSenseErrors);
  910. np->stats.tx_carrier_errors += stat_reg;
  911. np->stats.tx_errors += stat_reg;
  912. /* Clear all other statistic register. */
  913. readl (ioaddr + McstOctetXmtOk);
  914. readw (ioaddr + BcstFramesXmtdOk);
  915. readl (ioaddr + McstFramesXmtdOk);
  916. readw (ioaddr + BcstFramesRcvdOk);
  917. readw (ioaddr + MacControlFramesRcvd);
  918. readw (ioaddr + FrameTooLongErrors);
  919. readw (ioaddr + InRangeLengthErrors);
  920. readw (ioaddr + FramesCheckSeqErrors);
  921. readw (ioaddr + FramesLostRxErrors);
  922. readl (ioaddr + McstOctetXmtOk);
  923. readl (ioaddr + BcstOctetXmtOk);
  924. readl (ioaddr + McstFramesXmtdOk);
  925. readl (ioaddr + FramesWDeferredXmt);
  926. readl (ioaddr + LateCollisions);
  927. readw (ioaddr + BcstFramesXmtdOk);
  928. readw (ioaddr + MacControlFramesXmtd);
  929. readw (ioaddr + FramesWEXDeferal);
  930. #ifdef MEM_MAPPING
  931. for (i = 0x100; i <= 0x150; i += 4)
  932. readl (ioaddr + i);
  933. #endif
  934. readw (ioaddr + TxJumboFrames);
  935. readw (ioaddr + RxJumboFrames);
  936. readw (ioaddr + TCPCheckSumErrors);
  937. readw (ioaddr + UDPCheckSumErrors);
  938. readw (ioaddr + IPCheckSumErrors);
  939. return &np->stats;
  940. }
  941. static int
  942. clear_stats (struct net_device *dev)
  943. {
  944. long ioaddr = dev->base_addr;
  945. #ifdef MEM_MAPPING
  946. int i;
  947. #endif
  948. /* All statistics registers need to be acknowledged,
  949. else statistic overflow could cause problems */
  950. readl (ioaddr + FramesRcvOk);
  951. readl (ioaddr + FramesXmtOk);
  952. readl (ioaddr + OctetRcvOk);
  953. readl (ioaddr + OctetXmtOk);
  954. readl (ioaddr + McstFramesRcvdOk);
  955. readl (ioaddr + SingleColFrames);
  956. readl (ioaddr + MultiColFrames);
  957. readl (ioaddr + LateCollisions);
  958. /* detailed rx errors */
  959. readw (ioaddr + FrameTooLongErrors);
  960. readw (ioaddr + InRangeLengthErrors);
  961. readw (ioaddr + FramesCheckSeqErrors);
  962. readw (ioaddr + FramesLostRxErrors);
  963. /* detailed tx errors */
  964. readw (ioaddr + FramesAbortXSColls);
  965. readw (ioaddr + CarrierSenseErrors);
  966. /* Clear all other statistic register. */
  967. readl (ioaddr + McstOctetXmtOk);
  968. readw (ioaddr + BcstFramesXmtdOk);
  969. readl (ioaddr + McstFramesXmtdOk);
  970. readw (ioaddr + BcstFramesRcvdOk);
  971. readw (ioaddr + MacControlFramesRcvd);
  972. readl (ioaddr + McstOctetXmtOk);
  973. readl (ioaddr + BcstOctetXmtOk);
  974. readl (ioaddr + McstFramesXmtdOk);
  975. readl (ioaddr + FramesWDeferredXmt);
  976. readw (ioaddr + BcstFramesXmtdOk);
  977. readw (ioaddr + MacControlFramesXmtd);
  978. readw (ioaddr + FramesWEXDeferal);
  979. #ifdef MEM_MAPPING
  980. for (i = 0x100; i <= 0x150; i += 4)
  981. readl (ioaddr + i);
  982. #endif
  983. readw (ioaddr + TxJumboFrames);
  984. readw (ioaddr + RxJumboFrames);
  985. readw (ioaddr + TCPCheckSumErrors);
  986. readw (ioaddr + UDPCheckSumErrors);
  987. readw (ioaddr + IPCheckSumErrors);
  988. return 0;
  989. }
  990. static int
  991. change_mtu (struct net_device *dev, int new_mtu)
  992. {
  993. struct netdev_private *np = netdev_priv(dev);
  994. int max = (np->jumbo) ? MAX_JUMBO : 1536;
  995. if ((new_mtu < 68) || (new_mtu > max)) {
  996. return -EINVAL;
  997. }
  998. dev->mtu = new_mtu;
  999. return 0;
  1000. }
  1001. static void
  1002. set_multicast (struct net_device *dev)
  1003. {
  1004. long ioaddr = dev->base_addr;
  1005. u32 hash_table[2];
  1006. u16 rx_mode = 0;
  1007. struct netdev_private *np = netdev_priv(dev);
  1008. hash_table[0] = hash_table[1] = 0;
  1009. /* RxFlowcontrol DA: 01-80-C2-00-00-01. Hash index=0x39 */
  1010. hash_table[1] |= cpu_to_le32(0x02000000);
  1011. if (dev->flags & IFF_PROMISC) {
  1012. /* Receive all frames promiscuously. */
  1013. rx_mode = ReceiveAllFrames;
  1014. } else if ((dev->flags & IFF_ALLMULTI) ||
  1015. (dev->mc_count > multicast_filter_limit)) {
  1016. /* Receive broadcast and multicast frames */
  1017. rx_mode = ReceiveBroadcast | ReceiveMulticast | ReceiveUnicast;
  1018. } else if (dev->mc_count > 0) {
  1019. int i;
  1020. struct dev_mc_list *mclist;
  1021. /* Receive broadcast frames and multicast frames filtering
  1022. by Hashtable */
  1023. rx_mode =
  1024. ReceiveBroadcast | ReceiveMulticastHash | ReceiveUnicast;
  1025. for (i=0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  1026. i++, mclist=mclist->next)
  1027. {
  1028. int bit, index = 0;
  1029. int crc = ether_crc_le (ETH_ALEN, mclist->dmi_addr);
  1030. /* The inverted high significant 6 bits of CRC are
  1031. used as an index to hashtable */
  1032. for (bit = 0; bit < 6; bit++)
  1033. if (crc & (1 << (31 - bit)))
  1034. index |= (1 << bit);
  1035. hash_table[index / 32] |= (1 << (index % 32));
  1036. }
  1037. } else {
  1038. rx_mode = ReceiveBroadcast | ReceiveUnicast;
  1039. }
  1040. if (np->vlan) {
  1041. /* ReceiveVLANMatch field in ReceiveMode */
  1042. rx_mode |= ReceiveVLANMatch;
  1043. }
  1044. writel (hash_table[0], ioaddr + HashTable0);
  1045. writel (hash_table[1], ioaddr + HashTable1);
  1046. writew (rx_mode, ioaddr + ReceiveMode);
  1047. }
  1048. static void rio_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1049. {
  1050. struct netdev_private *np = netdev_priv(dev);
  1051. strcpy(info->driver, "dl2k");
  1052. strcpy(info->version, DRV_VERSION);
  1053. strcpy(info->bus_info, pci_name(np->pdev));
  1054. }
  1055. static int rio_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1056. {
  1057. struct netdev_private *np = netdev_priv(dev);
  1058. if (np->phy_media) {
  1059. /* fiber device */
  1060. cmd->supported = SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1061. cmd->advertising= ADVERTISED_Autoneg | ADVERTISED_FIBRE;
  1062. cmd->port = PORT_FIBRE;
  1063. cmd->transceiver = XCVR_INTERNAL;
  1064. } else {
  1065. /* copper device */
  1066. cmd->supported = SUPPORTED_10baseT_Half |
  1067. SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half
  1068. | SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full |
  1069. SUPPORTED_Autoneg | SUPPORTED_MII;
  1070. cmd->advertising = ADVERTISED_10baseT_Half |
  1071. ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Half |
  1072. ADVERTISED_100baseT_Full | ADVERTISED_1000baseT_Full|
  1073. ADVERTISED_Autoneg | ADVERTISED_MII;
  1074. cmd->port = PORT_MII;
  1075. cmd->transceiver = XCVR_INTERNAL;
  1076. }
  1077. if ( np->link_status ) {
  1078. cmd->speed = np->speed;
  1079. cmd->duplex = np->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1080. } else {
  1081. cmd->speed = -1;
  1082. cmd->duplex = -1;
  1083. }
  1084. if ( np->an_enable)
  1085. cmd->autoneg = AUTONEG_ENABLE;
  1086. else
  1087. cmd->autoneg = AUTONEG_DISABLE;
  1088. cmd->phy_address = np->phy_addr;
  1089. return 0;
  1090. }
  1091. static int rio_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1092. {
  1093. struct netdev_private *np = netdev_priv(dev);
  1094. netif_carrier_off(dev);
  1095. if (cmd->autoneg == AUTONEG_ENABLE) {
  1096. if (np->an_enable)
  1097. return 0;
  1098. else {
  1099. np->an_enable = 1;
  1100. mii_set_media(dev);
  1101. return 0;
  1102. }
  1103. } else {
  1104. np->an_enable = 0;
  1105. if (np->speed == 1000) {
  1106. cmd->speed = SPEED_100;
  1107. cmd->duplex = DUPLEX_FULL;
  1108. printk("Warning!! Can't disable Auto negotiation in 1000Mbps, change to Manual 100Mbps, Full duplex.\n");
  1109. }
  1110. switch(cmd->speed + cmd->duplex) {
  1111. case SPEED_10 + DUPLEX_HALF:
  1112. np->speed = 10;
  1113. np->full_duplex = 0;
  1114. break;
  1115. case SPEED_10 + DUPLEX_FULL:
  1116. np->speed = 10;
  1117. np->full_duplex = 1;
  1118. break;
  1119. case SPEED_100 + DUPLEX_HALF:
  1120. np->speed = 100;
  1121. np->full_duplex = 0;
  1122. break;
  1123. case SPEED_100 + DUPLEX_FULL:
  1124. np->speed = 100;
  1125. np->full_duplex = 1;
  1126. break;
  1127. case SPEED_1000 + DUPLEX_HALF:/* not supported */
  1128. case SPEED_1000 + DUPLEX_FULL:/* not supported */
  1129. default:
  1130. return -EINVAL;
  1131. }
  1132. mii_set_media(dev);
  1133. }
  1134. return 0;
  1135. }
  1136. static u32 rio_get_link(struct net_device *dev)
  1137. {
  1138. struct netdev_private *np = netdev_priv(dev);
  1139. return np->link_status;
  1140. }
  1141. static const struct ethtool_ops ethtool_ops = {
  1142. .get_drvinfo = rio_get_drvinfo,
  1143. .get_settings = rio_get_settings,
  1144. .set_settings = rio_set_settings,
  1145. .get_link = rio_get_link,
  1146. };
  1147. static int
  1148. rio_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
  1149. {
  1150. int phy_addr;
  1151. struct netdev_private *np = netdev_priv(dev);
  1152. struct mii_data *miidata = (struct mii_data *) &rq->ifr_ifru;
  1153. struct netdev_desc *desc;
  1154. int i;
  1155. phy_addr = np->phy_addr;
  1156. switch (cmd) {
  1157. case SIOCDEVPRIVATE:
  1158. break;
  1159. case SIOCDEVPRIVATE + 1:
  1160. miidata->out_value = mii_read (dev, phy_addr, miidata->reg_num);
  1161. break;
  1162. case SIOCDEVPRIVATE + 2:
  1163. mii_write (dev, phy_addr, miidata->reg_num, miidata->in_value);
  1164. break;
  1165. case SIOCDEVPRIVATE + 3:
  1166. break;
  1167. case SIOCDEVPRIVATE + 4:
  1168. break;
  1169. case SIOCDEVPRIVATE + 5:
  1170. netif_stop_queue (dev);
  1171. break;
  1172. case SIOCDEVPRIVATE + 6:
  1173. netif_wake_queue (dev);
  1174. break;
  1175. case SIOCDEVPRIVATE + 7:
  1176. printk
  1177. ("tx_full=%x cur_tx=%lx old_tx=%lx cur_rx=%lx old_rx=%lx\n",
  1178. netif_queue_stopped(dev), np->cur_tx, np->old_tx, np->cur_rx,
  1179. np->old_rx);
  1180. break;
  1181. case SIOCDEVPRIVATE + 8:
  1182. printk("TX ring:\n");
  1183. for (i = 0; i < TX_RING_SIZE; i++) {
  1184. desc = &np->tx_ring[i];
  1185. printk
  1186. ("%02x:cur:%08x next:%08x status:%08x frag1:%08x frag0:%08x",
  1187. i,
  1188. (u32) (np->tx_ring_dma + i * sizeof (*desc)),
  1189. (u32) desc->next_desc,
  1190. (u32) desc->status, (u32) (desc->fraginfo >> 32),
  1191. (u32) desc->fraginfo);
  1192. printk ("\n");
  1193. }
  1194. printk ("\n");
  1195. break;
  1196. default:
  1197. return -EOPNOTSUPP;
  1198. }
  1199. return 0;
  1200. }
  1201. #define EEP_READ 0x0200
  1202. #define EEP_BUSY 0x8000
  1203. /* Read the EEPROM word */
  1204. /* We use I/O instruction to read/write eeprom to avoid fail on some machines */
  1205. static int
  1206. read_eeprom (long ioaddr, int eep_addr)
  1207. {
  1208. int i = 1000;
  1209. outw (EEP_READ | (eep_addr & 0xff), ioaddr + EepromCtrl);
  1210. while (i-- > 0) {
  1211. if (!(inw (ioaddr + EepromCtrl) & EEP_BUSY)) {
  1212. return inw (ioaddr + EepromData);
  1213. }
  1214. }
  1215. return 0;
  1216. }
  1217. enum phy_ctrl_bits {
  1218. MII_READ = 0x00, MII_CLK = 0x01, MII_DATA1 = 0x02, MII_WRITE = 0x04,
  1219. MII_DUPLEX = 0x08,
  1220. };
  1221. #define mii_delay() readb(ioaddr)
  1222. static void
  1223. mii_sendbit (struct net_device *dev, u32 data)
  1224. {
  1225. long ioaddr = dev->base_addr + PhyCtrl;
  1226. data = (data) ? MII_DATA1 : 0;
  1227. data |= MII_WRITE;
  1228. data |= (readb (ioaddr) & 0xf8) | MII_WRITE;
  1229. writeb (data, ioaddr);
  1230. mii_delay ();
  1231. writeb (data | MII_CLK, ioaddr);
  1232. mii_delay ();
  1233. }
  1234. static int
  1235. mii_getbit (struct net_device *dev)
  1236. {
  1237. long ioaddr = dev->base_addr + PhyCtrl;
  1238. u8 data;
  1239. data = (readb (ioaddr) & 0xf8) | MII_READ;
  1240. writeb (data, ioaddr);
  1241. mii_delay ();
  1242. writeb (data | MII_CLK, ioaddr);
  1243. mii_delay ();
  1244. return ((readb (ioaddr) >> 1) & 1);
  1245. }
  1246. static void
  1247. mii_send_bits (struct net_device *dev, u32 data, int len)
  1248. {
  1249. int i;
  1250. for (i = len - 1; i >= 0; i--) {
  1251. mii_sendbit (dev, data & (1 << i));
  1252. }
  1253. }
  1254. static int
  1255. mii_read (struct net_device *dev, int phy_addr, int reg_num)
  1256. {
  1257. u32 cmd;
  1258. int i;
  1259. u32 retval = 0;
  1260. /* Preamble */
  1261. mii_send_bits (dev, 0xffffffff, 32);
  1262. /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
  1263. /* ST,OP = 0110'b for read operation */
  1264. cmd = (0x06 << 10 | phy_addr << 5 | reg_num);
  1265. mii_send_bits (dev, cmd, 14);
  1266. /* Turnaround */
  1267. if (mii_getbit (dev))
  1268. goto err_out;
  1269. /* Read data */
  1270. for (i = 0; i < 16; i++) {
  1271. retval |= mii_getbit (dev);
  1272. retval <<= 1;
  1273. }
  1274. /* End cycle */
  1275. mii_getbit (dev);
  1276. return (retval >> 1) & 0xffff;
  1277. err_out:
  1278. return 0;
  1279. }
  1280. static int
  1281. mii_write (struct net_device *dev, int phy_addr, int reg_num, u16 data)
  1282. {
  1283. u32 cmd;
  1284. /* Preamble */
  1285. mii_send_bits (dev, 0xffffffff, 32);
  1286. /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
  1287. /* ST,OP,AAAAA,RRRRR,TA = 0101xxxxxxxxxx10'b = 0x5002 for write */
  1288. cmd = (0x5002 << 16) | (phy_addr << 23) | (reg_num << 18) | data;
  1289. mii_send_bits (dev, cmd, 32);
  1290. /* End cycle */
  1291. mii_getbit (dev);
  1292. return 0;
  1293. }
  1294. static int
  1295. mii_wait_link (struct net_device *dev, int wait)
  1296. {
  1297. BMSR_t bmsr;
  1298. int phy_addr;
  1299. struct netdev_private *np;
  1300. np = netdev_priv(dev);
  1301. phy_addr = np->phy_addr;
  1302. do {
  1303. bmsr.image = mii_read (dev, phy_addr, MII_BMSR);
  1304. if (bmsr.bits.link_status)
  1305. return 0;
  1306. mdelay (1);
  1307. } while (--wait > 0);
  1308. return -1;
  1309. }
  1310. static int
  1311. mii_get_media (struct net_device *dev)
  1312. {
  1313. ANAR_t negotiate;
  1314. BMSR_t bmsr;
  1315. BMCR_t bmcr;
  1316. MSCR_t mscr;
  1317. MSSR_t mssr;
  1318. int phy_addr;
  1319. struct netdev_private *np;
  1320. np = netdev_priv(dev);
  1321. phy_addr = np->phy_addr;
  1322. bmsr.image = mii_read (dev, phy_addr, MII_BMSR);
  1323. if (np->an_enable) {
  1324. if (!bmsr.bits.an_complete) {
  1325. /* Auto-Negotiation not completed */
  1326. return -1;
  1327. }
  1328. negotiate.image = mii_read (dev, phy_addr, MII_ANAR) &
  1329. mii_read (dev, phy_addr, MII_ANLPAR);
  1330. mscr.image = mii_read (dev, phy_addr, MII_MSCR);
  1331. mssr.image = mii_read (dev, phy_addr, MII_MSSR);
  1332. if (mscr.bits.media_1000BT_FD & mssr.bits.lp_1000BT_FD) {
  1333. np->speed = 1000;
  1334. np->full_duplex = 1;
  1335. printk (KERN_INFO "Auto 1000 Mbps, Full duplex\n");
  1336. } else if (mscr.bits.media_1000BT_HD & mssr.bits.lp_1000BT_HD) {
  1337. np->speed = 1000;
  1338. np->full_duplex = 0;
  1339. printk (KERN_INFO "Auto 1000 Mbps, Half duplex\n");
  1340. } else if (negotiate.bits.media_100BX_FD) {
  1341. np->speed = 100;
  1342. np->full_duplex = 1;
  1343. printk (KERN_INFO "Auto 100 Mbps, Full duplex\n");
  1344. } else if (negotiate.bits.media_100BX_HD) {
  1345. np->speed = 100;
  1346. np->full_duplex = 0;
  1347. printk (KERN_INFO "Auto 100 Mbps, Half duplex\n");
  1348. } else if (negotiate.bits.media_10BT_FD) {
  1349. np->speed = 10;
  1350. np->full_duplex = 1;
  1351. printk (KERN_INFO "Auto 10 Mbps, Full duplex\n");
  1352. } else if (negotiate.bits.media_10BT_HD) {
  1353. np->speed = 10;
  1354. np->full_duplex = 0;
  1355. printk (KERN_INFO "Auto 10 Mbps, Half duplex\n");
  1356. }
  1357. if (negotiate.bits.pause) {
  1358. np->tx_flow &= 1;
  1359. np->rx_flow &= 1;
  1360. } else if (negotiate.bits.asymmetric) {
  1361. np->tx_flow = 0;
  1362. np->rx_flow &= 1;
  1363. }
  1364. /* else tx_flow, rx_flow = user select */
  1365. } else {
  1366. bmcr.image = mii_read (dev, phy_addr, MII_BMCR);
  1367. if (bmcr.bits.speed100 == 1 && bmcr.bits.speed1000 == 0) {
  1368. printk (KERN_INFO "Operating at 100 Mbps, ");
  1369. } else if (bmcr.bits.speed100 == 0 && bmcr.bits.speed1000 == 0) {
  1370. printk (KERN_INFO "Operating at 10 Mbps, ");
  1371. } else if (bmcr.bits.speed100 == 0 && bmcr.bits.speed1000 == 1) {
  1372. printk (KERN_INFO "Operating at 1000 Mbps, ");
  1373. }
  1374. if (bmcr.bits.duplex_mode) {
  1375. printk ("Full duplex\n");
  1376. } else {
  1377. printk ("Half duplex\n");
  1378. }
  1379. }
  1380. if (np->tx_flow)
  1381. printk(KERN_INFO "Enable Tx Flow Control\n");
  1382. else
  1383. printk(KERN_INFO "Disable Tx Flow Control\n");
  1384. if (np->rx_flow)
  1385. printk(KERN_INFO "Enable Rx Flow Control\n");
  1386. else
  1387. printk(KERN_INFO "Disable Rx Flow Control\n");
  1388. return 0;
  1389. }
  1390. static int
  1391. mii_set_media (struct net_device *dev)
  1392. {
  1393. PHY_SCR_t pscr;
  1394. BMCR_t bmcr;
  1395. BMSR_t bmsr;
  1396. ANAR_t anar;
  1397. int phy_addr;
  1398. struct netdev_private *np;
  1399. np = netdev_priv(dev);
  1400. phy_addr = np->phy_addr;
  1401. /* Does user set speed? */
  1402. if (np->an_enable) {
  1403. /* Advertise capabilities */
  1404. bmsr.image = mii_read (dev, phy_addr, MII_BMSR);
  1405. anar.image = mii_read (dev, phy_addr, MII_ANAR);
  1406. anar.bits.media_100BX_FD = bmsr.bits.media_100BX_FD;
  1407. anar.bits.media_100BX_HD = bmsr.bits.media_100BX_HD;
  1408. anar.bits.media_100BT4 = bmsr.bits.media_100BT4;
  1409. anar.bits.media_10BT_FD = bmsr.bits.media_10BT_FD;
  1410. anar.bits.media_10BT_HD = bmsr.bits.media_10BT_HD;
  1411. anar.bits.pause = 1;
  1412. anar.bits.asymmetric = 1;
  1413. mii_write (dev, phy_addr, MII_ANAR, anar.image);
  1414. /* Enable Auto crossover */
  1415. pscr.image = mii_read (dev, phy_addr, MII_PHY_SCR);
  1416. pscr.bits.mdi_crossover_mode = 3; /* 11'b */
  1417. mii_write (dev, phy_addr, MII_PHY_SCR, pscr.image);
  1418. /* Soft reset PHY */
  1419. mii_write (dev, phy_addr, MII_BMCR, MII_BMCR_RESET);
  1420. bmcr.image = 0;
  1421. bmcr.bits.an_enable = 1;
  1422. bmcr.bits.restart_an = 1;
  1423. bmcr.bits.reset = 1;
  1424. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1425. mdelay(1);
  1426. } else {
  1427. /* Force speed setting */
  1428. /* 1) Disable Auto crossover */
  1429. pscr.image = mii_read (dev, phy_addr, MII_PHY_SCR);
  1430. pscr.bits.mdi_crossover_mode = 0;
  1431. mii_write (dev, phy_addr, MII_PHY_SCR, pscr.image);
  1432. /* 2) PHY Reset */
  1433. bmcr.image = mii_read (dev, phy_addr, MII_BMCR);
  1434. bmcr.bits.reset = 1;
  1435. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1436. /* 3) Power Down */
  1437. bmcr.image = 0x1940; /* must be 0x1940 */
  1438. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1439. mdelay (100); /* wait a certain time */
  1440. /* 4) Advertise nothing */
  1441. mii_write (dev, phy_addr, MII_ANAR, 0);
  1442. /* 5) Set media and Power Up */
  1443. bmcr.image = 0;
  1444. bmcr.bits.power_down = 1;
  1445. if (np->speed == 100) {
  1446. bmcr.bits.speed100 = 1;
  1447. bmcr.bits.speed1000 = 0;
  1448. printk (KERN_INFO "Manual 100 Mbps, ");
  1449. } else if (np->speed == 10) {
  1450. bmcr.bits.speed100 = 0;
  1451. bmcr.bits.speed1000 = 0;
  1452. printk (KERN_INFO "Manual 10 Mbps, ");
  1453. }
  1454. if (np->full_duplex) {
  1455. bmcr.bits.duplex_mode = 1;
  1456. printk ("Full duplex\n");
  1457. } else {
  1458. bmcr.bits.duplex_mode = 0;
  1459. printk ("Half duplex\n");
  1460. }
  1461. #if 0
  1462. /* Set 1000BaseT Master/Slave setting */
  1463. mscr.image = mii_read (dev, phy_addr, MII_MSCR);
  1464. mscr.bits.cfg_enable = 1;
  1465. mscr.bits.cfg_value = 0;
  1466. #endif
  1467. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1468. mdelay(10);
  1469. }
  1470. return 0;
  1471. }
  1472. static int
  1473. mii_get_media_pcs (struct net_device *dev)
  1474. {
  1475. ANAR_PCS_t negotiate;
  1476. BMSR_t bmsr;
  1477. BMCR_t bmcr;
  1478. int phy_addr;
  1479. struct netdev_private *np;
  1480. np = netdev_priv(dev);
  1481. phy_addr = np->phy_addr;
  1482. bmsr.image = mii_read (dev, phy_addr, PCS_BMSR);
  1483. if (np->an_enable) {
  1484. if (!bmsr.bits.an_complete) {
  1485. /* Auto-Negotiation not completed */
  1486. return -1;
  1487. }
  1488. negotiate.image = mii_read (dev, phy_addr, PCS_ANAR) &
  1489. mii_read (dev, phy_addr, PCS_ANLPAR);
  1490. np->speed = 1000;
  1491. if (negotiate.bits.full_duplex) {
  1492. printk (KERN_INFO "Auto 1000 Mbps, Full duplex\n");
  1493. np->full_duplex = 1;
  1494. } else {
  1495. printk (KERN_INFO "Auto 1000 Mbps, half duplex\n");
  1496. np->full_duplex = 0;
  1497. }
  1498. if (negotiate.bits.pause) {
  1499. np->tx_flow &= 1;
  1500. np->rx_flow &= 1;
  1501. } else if (negotiate.bits.asymmetric) {
  1502. np->tx_flow = 0;
  1503. np->rx_flow &= 1;
  1504. }
  1505. /* else tx_flow, rx_flow = user select */
  1506. } else {
  1507. bmcr.image = mii_read (dev, phy_addr, PCS_BMCR);
  1508. printk (KERN_INFO "Operating at 1000 Mbps, ");
  1509. if (bmcr.bits.duplex_mode) {
  1510. printk ("Full duplex\n");
  1511. } else {
  1512. printk ("Half duplex\n");
  1513. }
  1514. }
  1515. if (np->tx_flow)
  1516. printk(KERN_INFO "Enable Tx Flow Control\n");
  1517. else
  1518. printk(KERN_INFO "Disable Tx Flow Control\n");
  1519. if (np->rx_flow)
  1520. printk(KERN_INFO "Enable Rx Flow Control\n");
  1521. else
  1522. printk(KERN_INFO "Disable Rx Flow Control\n");
  1523. return 0;
  1524. }
  1525. static int
  1526. mii_set_media_pcs (struct net_device *dev)
  1527. {
  1528. BMCR_t bmcr;
  1529. ESR_t esr;
  1530. ANAR_PCS_t anar;
  1531. int phy_addr;
  1532. struct netdev_private *np;
  1533. np = netdev_priv(dev);
  1534. phy_addr = np->phy_addr;
  1535. /* Auto-Negotiation? */
  1536. if (np->an_enable) {
  1537. /* Advertise capabilities */
  1538. esr.image = mii_read (dev, phy_addr, PCS_ESR);
  1539. anar.image = mii_read (dev, phy_addr, MII_ANAR);
  1540. anar.bits.half_duplex =
  1541. esr.bits.media_1000BT_HD | esr.bits.media_1000BX_HD;
  1542. anar.bits.full_duplex =
  1543. esr.bits.media_1000BT_FD | esr.bits.media_1000BX_FD;
  1544. anar.bits.pause = 1;
  1545. anar.bits.asymmetric = 1;
  1546. mii_write (dev, phy_addr, MII_ANAR, anar.image);
  1547. /* Soft reset PHY */
  1548. mii_write (dev, phy_addr, MII_BMCR, MII_BMCR_RESET);
  1549. bmcr.image = 0;
  1550. bmcr.bits.an_enable = 1;
  1551. bmcr.bits.restart_an = 1;
  1552. bmcr.bits.reset = 1;
  1553. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1554. mdelay(1);
  1555. } else {
  1556. /* Force speed setting */
  1557. /* PHY Reset */
  1558. bmcr.image = 0;
  1559. bmcr.bits.reset = 1;
  1560. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1561. mdelay(10);
  1562. bmcr.image = 0;
  1563. bmcr.bits.an_enable = 0;
  1564. if (np->full_duplex) {
  1565. bmcr.bits.duplex_mode = 1;
  1566. printk (KERN_INFO "Manual full duplex\n");
  1567. } else {
  1568. bmcr.bits.duplex_mode = 0;
  1569. printk (KERN_INFO "Manual half duplex\n");
  1570. }
  1571. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  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. np->rx_ring[i].fraginfo & DMA_48BIT_MASK,
  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. np->tx_ring[i].fraginfo & DMA_48BIT_MASK,
  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. */