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 "D-Link DL2000-based linux driver"
  12. #define DRV_VERSION "v1.18"
  13. #define DRV_RELDATE "2006/06/27"
  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. if (!version_printed++)
  90. printk ("%s", version);
  91. err = pci_enable_device (pdev);
  92. if (err)
  93. return err;
  94. irq = pdev->irq;
  95. err = pci_request_regions (pdev, "dl2k");
  96. if (err)
  97. goto err_out_disable;
  98. pci_set_master (pdev);
  99. dev = alloc_etherdev (sizeof (*np));
  100. if (!dev) {
  101. err = -ENOMEM;
  102. goto err_out_res;
  103. }
  104. SET_MODULE_OWNER (dev);
  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. pci_read_config_byte(pdev, PCI_REVISION_ID, &np->pci_rev_id);
  231. err = register_netdev (dev);
  232. if (err)
  233. goto err_out_unmap_rx;
  234. card_idx++;
  235. printk (KERN_INFO "%s: %s, %02x:%02x:%02x:%02x:%02x:%02x, IRQ %d\n",
  236. dev->name, np->name,
  237. dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
  238. dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5], irq);
  239. if (tx_coalesce > 1)
  240. printk(KERN_INFO "tx_coalesce:\t%d packets\n",
  241. tx_coalesce);
  242. if (np->coalesce)
  243. printk(KERN_INFO "rx_coalesce:\t%d packets\n"
  244. KERN_INFO "rx_timeout: \t%d ns\n",
  245. np->rx_coalesce, np->rx_timeout*640);
  246. if (np->vlan)
  247. printk(KERN_INFO "vlan(id):\t%d\n", np->vlan);
  248. return 0;
  249. err_out_unmap_rx:
  250. pci_free_consistent (pdev, RX_TOTAL_SIZE, np->rx_ring, np->rx_ring_dma);
  251. err_out_unmap_tx:
  252. pci_free_consistent (pdev, TX_TOTAL_SIZE, np->tx_ring, np->tx_ring_dma);
  253. err_out_iounmap:
  254. #ifdef MEM_MAPPING
  255. iounmap ((void *) ioaddr);
  256. err_out_dev:
  257. #endif
  258. free_netdev (dev);
  259. err_out_res:
  260. pci_release_regions (pdev);
  261. err_out_disable:
  262. pci_disable_device (pdev);
  263. return err;
  264. }
  265. int
  266. find_miiphy (struct net_device *dev)
  267. {
  268. int i, phy_found = 0;
  269. struct netdev_private *np;
  270. long ioaddr;
  271. np = netdev_priv(dev);
  272. ioaddr = dev->base_addr;
  273. np->phy_addr = 1;
  274. for (i = 31; i >= 0; i--) {
  275. int mii_status = mii_read (dev, i, 1);
  276. if (mii_status != 0xffff && mii_status != 0x0000) {
  277. np->phy_addr = i;
  278. phy_found++;
  279. }
  280. }
  281. if (!phy_found) {
  282. printk (KERN_ERR "%s: No MII PHY found!\n", dev->name);
  283. return -ENODEV;
  284. }
  285. return 0;
  286. }
  287. int
  288. parse_eeprom (struct net_device *dev)
  289. {
  290. int i, j;
  291. long ioaddr = dev->base_addr;
  292. u8 sromdata[256];
  293. u8 *psib;
  294. u32 crc;
  295. PSROM_t psrom = (PSROM_t) sromdata;
  296. struct netdev_private *np = netdev_priv(dev);
  297. int cid, next;
  298. #ifdef MEM_MAPPING
  299. ioaddr = pci_resource_start (np->pdev, 0);
  300. #endif
  301. /* Read eeprom */
  302. for (i = 0; i < 128; i++) {
  303. ((u16 *) sromdata)[i] = le16_to_cpu (read_eeprom (ioaddr, i));
  304. }
  305. #ifdef MEM_MAPPING
  306. ioaddr = dev->base_addr;
  307. #endif
  308. /* Check CRC */
  309. crc = ~ether_crc_le (256 - 4, sromdata);
  310. if (psrom->crc != crc) {
  311. printk (KERN_ERR "%s: EEPROM data CRC error.\n", dev->name);
  312. return -1;
  313. }
  314. /* Set MAC address */
  315. for (i = 0; i < 6; i++)
  316. dev->dev_addr[i] = psrom->mac_addr[i];
  317. /* Parse Software Information Block */
  318. i = 0x30;
  319. psib = (u8 *) sromdata;
  320. do {
  321. cid = psib[i++];
  322. next = psib[i++];
  323. if ((cid == 0 && next == 0) || (cid == 0xff && next == 0xff)) {
  324. printk (KERN_ERR "Cell data error\n");
  325. return -1;
  326. }
  327. switch (cid) {
  328. case 0: /* Format version */
  329. break;
  330. case 1: /* End of cell */
  331. return 0;
  332. case 2: /* Duplex Polarity */
  333. np->duplex_polarity = psib[i];
  334. writeb (readb (ioaddr + PhyCtrl) | psib[i],
  335. ioaddr + PhyCtrl);
  336. break;
  337. case 3: /* Wake Polarity */
  338. np->wake_polarity = psib[i];
  339. break;
  340. case 9: /* Adapter description */
  341. j = (next - i > 255) ? 255 : next - i;
  342. memcpy (np->name, &(psib[i]), j);
  343. break;
  344. case 4:
  345. case 5:
  346. case 6:
  347. case 7:
  348. case 8: /* Reversed */
  349. break;
  350. default: /* Unknown cell */
  351. return -1;
  352. }
  353. i = next;
  354. } while (1);
  355. return 0;
  356. }
  357. static int
  358. rio_open (struct net_device *dev)
  359. {
  360. struct netdev_private *np = netdev_priv(dev);
  361. long ioaddr = dev->base_addr;
  362. int i;
  363. u16 macctrl;
  364. i = request_irq (dev->irq, &rio_interrupt, IRQF_SHARED, dev->name, dev);
  365. if (i)
  366. return i;
  367. /* Reset all logic functions */
  368. writew (GlobalReset | DMAReset | FIFOReset | NetworkReset | HostReset,
  369. ioaddr + ASICCtrl + 2);
  370. mdelay(10);
  371. /* DebugCtrl bit 4, 5, 9 must set */
  372. writel (readl (ioaddr + DebugCtrl) | 0x0230, ioaddr + DebugCtrl);
  373. /* Jumbo frame */
  374. if (np->jumbo != 0)
  375. writew (MAX_JUMBO+14, ioaddr + MaxFrameSize);
  376. alloc_list (dev);
  377. /* Get station address */
  378. for (i = 0; i < 6; i++)
  379. writeb (dev->dev_addr[i], ioaddr + StationAddr0 + i);
  380. set_multicast (dev);
  381. if (np->coalesce) {
  382. writel (np->rx_coalesce | np->rx_timeout << 16,
  383. ioaddr + RxDMAIntCtrl);
  384. }
  385. /* Set RIO to poll every N*320nsec. */
  386. writeb (0x20, ioaddr + RxDMAPollPeriod);
  387. writeb (0xff, ioaddr + TxDMAPollPeriod);
  388. writeb (0x30, ioaddr + RxDMABurstThresh);
  389. writeb (0x30, ioaddr + RxDMAUrgentThresh);
  390. writel (0x0007ffff, ioaddr + RmonStatMask);
  391. /* clear statistics */
  392. clear_stats (dev);
  393. /* VLAN supported */
  394. if (np->vlan) {
  395. /* priority field in RxDMAIntCtrl */
  396. writel (readl(ioaddr + RxDMAIntCtrl) | 0x7 << 10,
  397. ioaddr + RxDMAIntCtrl);
  398. /* VLANId */
  399. writew (np->vlan, ioaddr + VLANId);
  400. /* Length/Type should be 0x8100 */
  401. writel (0x8100 << 16 | np->vlan, ioaddr + VLANTag);
  402. /* Enable AutoVLANuntagging, but disable AutoVLANtagging.
  403. VLAN information tagged by TFC' VID, CFI fields. */
  404. writel (readl (ioaddr + MACCtrl) | AutoVLANuntagging,
  405. ioaddr + MACCtrl);
  406. }
  407. init_timer (&np->timer);
  408. np->timer.expires = jiffies + 1*HZ;
  409. np->timer.data = (unsigned long) dev;
  410. np->timer.function = &rio_timer;
  411. add_timer (&np->timer);
  412. /* Start Tx/Rx */
  413. writel (readl (ioaddr + MACCtrl) | StatsEnable | RxEnable | TxEnable,
  414. ioaddr + MACCtrl);
  415. macctrl = 0;
  416. macctrl |= (np->vlan) ? AutoVLANuntagging : 0;
  417. macctrl |= (np->full_duplex) ? DuplexSelect : 0;
  418. macctrl |= (np->tx_flow) ? TxFlowControlEnable : 0;
  419. macctrl |= (np->rx_flow) ? RxFlowControlEnable : 0;
  420. writew(macctrl, ioaddr + MACCtrl);
  421. netif_start_queue (dev);
  422. /* Enable default interrupts */
  423. EnableInt ();
  424. return 0;
  425. }
  426. static void
  427. rio_timer (unsigned long data)
  428. {
  429. struct net_device *dev = (struct net_device *)data;
  430. struct netdev_private *np = netdev_priv(dev);
  431. unsigned int entry;
  432. int next_tick = 1*HZ;
  433. unsigned long flags;
  434. spin_lock_irqsave(&np->rx_lock, flags);
  435. /* Recover rx ring exhausted error */
  436. if (np->cur_rx - np->old_rx >= RX_RING_SIZE) {
  437. printk(KERN_INFO "Try to recover rx ring exhausted...\n");
  438. /* Re-allocate skbuffs to fill the descriptor ring */
  439. for (; np->cur_rx - np->old_rx > 0; np->old_rx++) {
  440. struct sk_buff *skb;
  441. entry = np->old_rx % RX_RING_SIZE;
  442. /* Dropped packets don't need to re-allocate */
  443. if (np->rx_skbuff[entry] == NULL) {
  444. skb = dev_alloc_skb (np->rx_buf_sz);
  445. if (skb == NULL) {
  446. np->rx_ring[entry].fraginfo = 0;
  447. printk (KERN_INFO
  448. "%s: Still unable to re-allocate Rx skbuff.#%d\n",
  449. dev->name, entry);
  450. break;
  451. }
  452. np->rx_skbuff[entry] = skb;
  453. skb->dev = dev;
  454. /* 16 byte align the IP header */
  455. skb_reserve (skb, 2);
  456. np->rx_ring[entry].fraginfo =
  457. cpu_to_le64 (pci_map_single
  458. (np->pdev, skb->data, np->rx_buf_sz,
  459. PCI_DMA_FROMDEVICE));
  460. }
  461. np->rx_ring[entry].fraginfo |=
  462. cpu_to_le64 (np->rx_buf_sz) << 48;
  463. np->rx_ring[entry].status = 0;
  464. } /* end for */
  465. } /* end if */
  466. spin_unlock_irqrestore (&np->rx_lock, flags);
  467. np->timer.expires = jiffies + next_tick;
  468. add_timer(&np->timer);
  469. }
  470. static void
  471. rio_tx_timeout (struct net_device *dev)
  472. {
  473. long ioaddr = dev->base_addr;
  474. printk (KERN_INFO "%s: Tx timed out (%4.4x), is buffer full?\n",
  475. dev->name, readl (ioaddr + TxStatus));
  476. rio_free_tx(dev, 0);
  477. dev->if_port = 0;
  478. dev->trans_start = jiffies;
  479. }
  480. /* allocate and initialize Tx and Rx descriptors */
  481. static void
  482. alloc_list (struct net_device *dev)
  483. {
  484. struct netdev_private *np = netdev_priv(dev);
  485. int i;
  486. np->cur_rx = np->cur_tx = 0;
  487. np->old_rx = np->old_tx = 0;
  488. np->rx_buf_sz = (dev->mtu <= 1500 ? PACKET_SIZE : dev->mtu + 32);
  489. /* Initialize Tx descriptors, TFDListPtr leaves in start_xmit(). */
  490. for (i = 0; i < TX_RING_SIZE; i++) {
  491. np->tx_skbuff[i] = NULL;
  492. np->tx_ring[i].status = cpu_to_le64 (TFDDone);
  493. np->tx_ring[i].next_desc = cpu_to_le64 (np->tx_ring_dma +
  494. ((i+1)%TX_RING_SIZE) *
  495. sizeof (struct netdev_desc));
  496. }
  497. /* Initialize Rx descriptors */
  498. for (i = 0; i < RX_RING_SIZE; i++) {
  499. np->rx_ring[i].next_desc = cpu_to_le64 (np->rx_ring_dma +
  500. ((i + 1) % RX_RING_SIZE) *
  501. sizeof (struct netdev_desc));
  502. np->rx_ring[i].status = 0;
  503. np->rx_ring[i].fraginfo = 0;
  504. np->rx_skbuff[i] = NULL;
  505. }
  506. /* Allocate the rx buffers */
  507. for (i = 0; i < RX_RING_SIZE; i++) {
  508. /* Allocated fixed size of skbuff */
  509. struct sk_buff *skb = dev_alloc_skb (np->rx_buf_sz);
  510. np->rx_skbuff[i] = skb;
  511. if (skb == NULL) {
  512. printk (KERN_ERR
  513. "%s: alloc_list: allocate Rx buffer error! ",
  514. dev->name);
  515. break;
  516. }
  517. skb->dev = dev; /* Mark as being used by this device. */
  518. skb_reserve (skb, 2); /* 16 byte align the IP header. */
  519. /* Rubicon now supports 40 bits of addressing space. */
  520. np->rx_ring[i].fraginfo =
  521. cpu_to_le64 ( pci_map_single (
  522. np->pdev, skb->data, np->rx_buf_sz,
  523. PCI_DMA_FROMDEVICE));
  524. np->rx_ring[i].fraginfo |= cpu_to_le64 (np->rx_buf_sz) << 48;
  525. }
  526. /* Set RFDListPtr */
  527. writel (cpu_to_le32 (np->rx_ring_dma), dev->base_addr + RFDListPtr0);
  528. writel (0, dev->base_addr + RFDListPtr1);
  529. return;
  530. }
  531. static int
  532. start_xmit (struct sk_buff *skb, struct net_device *dev)
  533. {
  534. struct netdev_private *np = netdev_priv(dev);
  535. struct netdev_desc *txdesc;
  536. unsigned entry;
  537. u32 ioaddr;
  538. u64 tfc_vlan_tag = 0;
  539. if (np->link_status == 0) { /* Link Down */
  540. dev_kfree_skb(skb);
  541. return 0;
  542. }
  543. ioaddr = dev->base_addr;
  544. entry = np->cur_tx % TX_RING_SIZE;
  545. np->tx_skbuff[entry] = skb;
  546. txdesc = &np->tx_ring[entry];
  547. #if 0
  548. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  549. txdesc->status |=
  550. cpu_to_le64 (TCPChecksumEnable | UDPChecksumEnable |
  551. IPChecksumEnable);
  552. }
  553. #endif
  554. if (np->vlan) {
  555. tfc_vlan_tag =
  556. cpu_to_le64 (VLANTagInsert) |
  557. (cpu_to_le64 (np->vlan) << 32) |
  558. (cpu_to_le64 (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 (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 void
  636. rio_free_tx (struct net_device *dev, int irq)
  637. {
  638. struct netdev_private *np = netdev_priv(dev);
  639. int entry = np->old_tx % TX_RING_SIZE;
  640. int tx_use = 0;
  641. unsigned long flag = 0;
  642. if (irq)
  643. spin_lock(&np->tx_lock);
  644. else
  645. spin_lock_irqsave(&np->tx_lock, flag);
  646. /* Free used tx skbuffs */
  647. while (entry != np->cur_tx) {
  648. struct sk_buff *skb;
  649. if (!(np->tx_ring[entry].status & TFDDone))
  650. break;
  651. skb = np->tx_skbuff[entry];
  652. pci_unmap_single (np->pdev,
  653. np->tx_ring[entry].fraginfo & DMA_48BIT_MASK,
  654. skb->len, PCI_DMA_TODEVICE);
  655. if (irq)
  656. dev_kfree_skb_irq (skb);
  657. else
  658. dev_kfree_skb (skb);
  659. np->tx_skbuff[entry] = NULL;
  660. entry = (entry + 1) % TX_RING_SIZE;
  661. tx_use++;
  662. }
  663. if (irq)
  664. spin_unlock(&np->tx_lock);
  665. else
  666. spin_unlock_irqrestore(&np->tx_lock, flag);
  667. np->old_tx = entry;
  668. /* If the ring is no longer full, clear tx_full and
  669. call netif_wake_queue() */
  670. if (netif_queue_stopped(dev) &&
  671. ((np->cur_tx - np->old_tx + TX_RING_SIZE) % TX_RING_SIZE
  672. < TX_QUEUE_LEN - 1 || np->speed == 10)) {
  673. netif_wake_queue (dev);
  674. }
  675. }
  676. static void
  677. tx_error (struct net_device *dev, int tx_status)
  678. {
  679. struct netdev_private *np;
  680. long ioaddr = dev->base_addr;
  681. int frame_id;
  682. int i;
  683. np = netdev_priv(dev);
  684. frame_id = (tx_status & 0xffff0000);
  685. printk (KERN_ERR "%s: Transmit error, TxStatus %4.4x, FrameId %d.\n",
  686. dev->name, tx_status, frame_id);
  687. np->stats.tx_errors++;
  688. /* Ttransmit Underrun */
  689. if (tx_status & 0x10) {
  690. np->stats.tx_fifo_errors++;
  691. writew (readw (ioaddr + TxStartThresh) + 0x10,
  692. ioaddr + TxStartThresh);
  693. /* Transmit Underrun need to set TxReset, DMARest, FIFOReset */
  694. writew (TxReset | DMAReset | FIFOReset | NetworkReset,
  695. ioaddr + ASICCtrl + 2);
  696. /* Wait for ResetBusy bit clear */
  697. for (i = 50; i > 0; i--) {
  698. if ((readw (ioaddr + ASICCtrl + 2) & ResetBusy) == 0)
  699. break;
  700. mdelay (1);
  701. }
  702. rio_free_tx (dev, 1);
  703. /* Reset TFDListPtr */
  704. writel (np->tx_ring_dma +
  705. np->old_tx * sizeof (struct netdev_desc),
  706. dev->base_addr + TFDListPtr0);
  707. writel (0, dev->base_addr + TFDListPtr1);
  708. /* Let TxStartThresh stay default value */
  709. }
  710. /* Late Collision */
  711. if (tx_status & 0x04) {
  712. np->stats.tx_fifo_errors++;
  713. /* TxReset and clear FIFO */
  714. writew (TxReset | FIFOReset, ioaddr + ASICCtrl + 2);
  715. /* Wait reset done */
  716. for (i = 50; i > 0; i--) {
  717. if ((readw (ioaddr + ASICCtrl + 2) & ResetBusy) == 0)
  718. break;
  719. mdelay (1);
  720. }
  721. /* Let TxStartThresh stay default value */
  722. }
  723. /* Maximum Collisions */
  724. #ifdef ETHER_STATS
  725. if (tx_status & 0x08)
  726. np->stats.collisions16++;
  727. #else
  728. if (tx_status & 0x08)
  729. np->stats.collisions++;
  730. #endif
  731. /* Restart the Tx */
  732. writel (readw (dev->base_addr + MACCtrl) | TxEnable, ioaddr + MACCtrl);
  733. }
  734. static int
  735. receive_packet (struct net_device *dev)
  736. {
  737. struct netdev_private *np = netdev_priv(dev);
  738. int entry = np->cur_rx % RX_RING_SIZE;
  739. int cnt = 30;
  740. /* If RFDDone, FrameStart and FrameEnd set, there is a new packet in. */
  741. while (1) {
  742. struct netdev_desc *desc = &np->rx_ring[entry];
  743. int pkt_len;
  744. u64 frame_status;
  745. if (!(desc->status & RFDDone) ||
  746. !(desc->status & FrameStart) || !(desc->status & FrameEnd))
  747. break;
  748. /* Chip omits the CRC. */
  749. pkt_len = le64_to_cpu (desc->status & 0xffff);
  750. frame_status = le64_to_cpu (desc->status);
  751. if (--cnt < 0)
  752. break;
  753. /* Update rx error statistics, drop packet. */
  754. if (frame_status & RFS_Errors) {
  755. np->stats.rx_errors++;
  756. if (frame_status & (RxRuntFrame | RxLengthError))
  757. np->stats.rx_length_errors++;
  758. if (frame_status & RxFCSError)
  759. np->stats.rx_crc_errors++;
  760. if (frame_status & RxAlignmentError && np->speed != 1000)
  761. np->stats.rx_frame_errors++;
  762. if (frame_status & RxFIFOOverrun)
  763. np->stats.rx_fifo_errors++;
  764. } else {
  765. struct sk_buff *skb;
  766. /* Small skbuffs for short packets */
  767. if (pkt_len > copy_thresh) {
  768. pci_unmap_single (np->pdev,
  769. desc->fraginfo & DMA_48BIT_MASK,
  770. np->rx_buf_sz,
  771. PCI_DMA_FROMDEVICE);
  772. skb_put (skb = np->rx_skbuff[entry], pkt_len);
  773. np->rx_skbuff[entry] = NULL;
  774. } else if ((skb = dev_alloc_skb (pkt_len + 2)) != NULL) {
  775. pci_dma_sync_single_for_cpu(np->pdev,
  776. desc->fraginfo &
  777. DMA_48BIT_MASK,
  778. np->rx_buf_sz,
  779. PCI_DMA_FROMDEVICE);
  780. skb->dev = dev;
  781. /* 16 byte align the IP header */
  782. skb_reserve (skb, 2);
  783. eth_copy_and_sum (skb,
  784. np->rx_skbuff[entry]->data,
  785. pkt_len, 0);
  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->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. skb->dev = dev;
  825. /* 16 byte align the IP header */
  826. skb_reserve (skb, 2);
  827. np->rx_ring[entry].fraginfo =
  828. cpu_to_le64 (pci_map_single
  829. (np->pdev, skb->data, np->rx_buf_sz,
  830. PCI_DMA_FROMDEVICE));
  831. }
  832. np->rx_ring[entry].fraginfo |=
  833. cpu_to_le64 (np->rx_buf_sz) << 48;
  834. np->rx_ring[entry].status = 0;
  835. entry = (entry + 1) % RX_RING_SIZE;
  836. }
  837. np->old_rx = entry;
  838. spin_unlock(&np->rx_lock);
  839. return 0;
  840. }
  841. static void
  842. rio_error (struct net_device *dev, int int_status)
  843. {
  844. long ioaddr = dev->base_addr;
  845. struct netdev_private *np = netdev_priv(dev);
  846. u16 macctrl;
  847. /* Link change event */
  848. if (int_status & LinkEvent) {
  849. if (mii_wait_link (dev, 10) == 0) {
  850. printk (KERN_INFO "%s: Link up\n", dev->name);
  851. if (np->phy_media)
  852. mii_get_media_pcs (dev);
  853. else
  854. mii_get_media (dev);
  855. if (np->speed == 1000)
  856. np->tx_coalesce = tx_coalesce;
  857. else
  858. np->tx_coalesce = 1;
  859. macctrl = 0;
  860. macctrl |= (np->vlan) ? AutoVLANuntagging : 0;
  861. macctrl |= (np->full_duplex) ? DuplexSelect : 0;
  862. macctrl |= (np->tx_flow) ?
  863. TxFlowControlEnable : 0;
  864. macctrl |= (np->rx_flow) ?
  865. RxFlowControlEnable : 0;
  866. writew(macctrl, ioaddr + MACCtrl);
  867. np->link_status = 1;
  868. netif_carrier_on(dev);
  869. } else {
  870. printk (KERN_INFO "%s: Link off\n", dev->name);
  871. np->link_status = 0;
  872. netif_carrier_off(dev);
  873. }
  874. }
  875. /* UpdateStats statistics registers */
  876. if (int_status & UpdateStats) {
  877. get_stats (dev);
  878. }
  879. /* PCI Error, a catastronphic error related to the bus interface
  880. occurs, set GlobalReset and HostReset to reset. */
  881. if (int_status & HostError) {
  882. printk (KERN_ERR "%s: HostError! IntStatus %4.4x.\n",
  883. dev->name, int_status);
  884. writew (GlobalReset | HostReset, ioaddr + ASICCtrl + 2);
  885. mdelay (500);
  886. }
  887. }
  888. static struct net_device_stats *
  889. get_stats (struct net_device *dev)
  890. {
  891. long ioaddr = dev->base_addr;
  892. struct netdev_private *np = netdev_priv(dev);
  893. #ifdef MEM_MAPPING
  894. int i;
  895. #endif
  896. unsigned int stat_reg;
  897. /* All statistics registers need to be acknowledged,
  898. else statistic overflow could cause problems */
  899. np->stats.rx_packets += readl (ioaddr + FramesRcvOk);
  900. np->stats.tx_packets += readl (ioaddr + FramesXmtOk);
  901. np->stats.rx_bytes += readl (ioaddr + OctetRcvOk);
  902. np->stats.tx_bytes += readl (ioaddr + OctetXmtOk);
  903. np->stats.multicast = readl (ioaddr + McstFramesRcvdOk);
  904. np->stats.collisions += readl (ioaddr + SingleColFrames)
  905. + readl (ioaddr + MultiColFrames);
  906. /* detailed tx errors */
  907. stat_reg = readw (ioaddr + FramesAbortXSColls);
  908. np->stats.tx_aborted_errors += stat_reg;
  909. np->stats.tx_errors += stat_reg;
  910. stat_reg = readw (ioaddr + CarrierSenseErrors);
  911. np->stats.tx_carrier_errors += stat_reg;
  912. np->stats.tx_errors += stat_reg;
  913. /* Clear all other statistic register. */
  914. readl (ioaddr + McstOctetXmtOk);
  915. readw (ioaddr + BcstFramesXmtdOk);
  916. readl (ioaddr + McstFramesXmtdOk);
  917. readw (ioaddr + BcstFramesRcvdOk);
  918. readw (ioaddr + MacControlFramesRcvd);
  919. readw (ioaddr + FrameTooLongErrors);
  920. readw (ioaddr + InRangeLengthErrors);
  921. readw (ioaddr + FramesCheckSeqErrors);
  922. readw (ioaddr + FramesLostRxErrors);
  923. readl (ioaddr + McstOctetXmtOk);
  924. readl (ioaddr + BcstOctetXmtOk);
  925. readl (ioaddr + McstFramesXmtdOk);
  926. readl (ioaddr + FramesWDeferredXmt);
  927. readl (ioaddr + LateCollisions);
  928. readw (ioaddr + BcstFramesXmtdOk);
  929. readw (ioaddr + MacControlFramesXmtd);
  930. readw (ioaddr + FramesWEXDeferal);
  931. #ifdef MEM_MAPPING
  932. for (i = 0x100; i <= 0x150; i += 4)
  933. readl (ioaddr + i);
  934. #endif
  935. readw (ioaddr + TxJumboFrames);
  936. readw (ioaddr + RxJumboFrames);
  937. readw (ioaddr + TCPCheckSumErrors);
  938. readw (ioaddr + UDPCheckSumErrors);
  939. readw (ioaddr + IPCheckSumErrors);
  940. return &np->stats;
  941. }
  942. static int
  943. clear_stats (struct net_device *dev)
  944. {
  945. long ioaddr = dev->base_addr;
  946. #ifdef MEM_MAPPING
  947. int i;
  948. #endif
  949. /* All statistics registers need to be acknowledged,
  950. else statistic overflow could cause problems */
  951. readl (ioaddr + FramesRcvOk);
  952. readl (ioaddr + FramesXmtOk);
  953. readl (ioaddr + OctetRcvOk);
  954. readl (ioaddr + OctetXmtOk);
  955. readl (ioaddr + McstFramesRcvdOk);
  956. readl (ioaddr + SingleColFrames);
  957. readl (ioaddr + MultiColFrames);
  958. readl (ioaddr + LateCollisions);
  959. /* detailed rx errors */
  960. readw (ioaddr + FrameTooLongErrors);
  961. readw (ioaddr + InRangeLengthErrors);
  962. readw (ioaddr + FramesCheckSeqErrors);
  963. readw (ioaddr + FramesLostRxErrors);
  964. /* detailed tx errors */
  965. readw (ioaddr + FramesAbortXSColls);
  966. readw (ioaddr + CarrierSenseErrors);
  967. /* Clear all other statistic register. */
  968. readl (ioaddr + McstOctetXmtOk);
  969. readw (ioaddr + BcstFramesXmtdOk);
  970. readl (ioaddr + McstFramesXmtdOk);
  971. readw (ioaddr + BcstFramesRcvdOk);
  972. readw (ioaddr + MacControlFramesRcvd);
  973. readl (ioaddr + McstOctetXmtOk);
  974. readl (ioaddr + BcstOctetXmtOk);
  975. readl (ioaddr + McstFramesXmtdOk);
  976. readl (ioaddr + FramesWDeferredXmt);
  977. readw (ioaddr + BcstFramesXmtdOk);
  978. readw (ioaddr + MacControlFramesXmtd);
  979. readw (ioaddr + FramesWEXDeferal);
  980. #ifdef MEM_MAPPING
  981. for (i = 0x100; i <= 0x150; i += 4)
  982. readl (ioaddr + i);
  983. #endif
  984. readw (ioaddr + TxJumboFrames);
  985. readw (ioaddr + RxJumboFrames);
  986. readw (ioaddr + TCPCheckSumErrors);
  987. readw (ioaddr + UDPCheckSumErrors);
  988. readw (ioaddr + IPCheckSumErrors);
  989. return 0;
  990. }
  991. int
  992. change_mtu (struct net_device *dev, int new_mtu)
  993. {
  994. struct netdev_private *np = netdev_priv(dev);
  995. int max = (np->jumbo) ? MAX_JUMBO : 1536;
  996. if ((new_mtu < 68) || (new_mtu > max)) {
  997. return -EINVAL;
  998. }
  999. dev->mtu = new_mtu;
  1000. return 0;
  1001. }
  1002. static void
  1003. set_multicast (struct net_device *dev)
  1004. {
  1005. long ioaddr = dev->base_addr;
  1006. u32 hash_table[2];
  1007. u16 rx_mode = 0;
  1008. struct netdev_private *np = netdev_priv(dev);
  1009. hash_table[0] = hash_table[1] = 0;
  1010. /* RxFlowcontrol DA: 01-80-C2-00-00-01. Hash index=0x39 */
  1011. hash_table[1] |= cpu_to_le32(0x02000000);
  1012. if (dev->flags & IFF_PROMISC) {
  1013. /* Receive all frames promiscuously. */
  1014. rx_mode = ReceiveAllFrames;
  1015. } else if ((dev->flags & IFF_ALLMULTI) ||
  1016. (dev->mc_count > multicast_filter_limit)) {
  1017. /* Receive broadcast and multicast frames */
  1018. rx_mode = ReceiveBroadcast | ReceiveMulticast | ReceiveUnicast;
  1019. } else if (dev->mc_count > 0) {
  1020. int i;
  1021. struct dev_mc_list *mclist;
  1022. /* Receive broadcast frames and multicast frames filtering
  1023. by Hashtable */
  1024. rx_mode =
  1025. ReceiveBroadcast | ReceiveMulticastHash | ReceiveUnicast;
  1026. for (i=0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  1027. i++, mclist=mclist->next)
  1028. {
  1029. int bit, index = 0;
  1030. int crc = ether_crc_le (ETH_ALEN, mclist->dmi_addr);
  1031. /* The inverted high significant 6 bits of CRC are
  1032. used as an index to hashtable */
  1033. for (bit = 0; bit < 6; bit++)
  1034. if (crc & (1 << (31 - bit)))
  1035. index |= (1 << bit);
  1036. hash_table[index / 32] |= (1 << (index % 32));
  1037. }
  1038. } else {
  1039. rx_mode = ReceiveBroadcast | ReceiveUnicast;
  1040. }
  1041. if (np->vlan) {
  1042. /* ReceiveVLANMatch field in ReceiveMode */
  1043. rx_mode |= ReceiveVLANMatch;
  1044. }
  1045. writel (hash_table[0], ioaddr + HashTable0);
  1046. writel (hash_table[1], ioaddr + HashTable1);
  1047. writew (rx_mode, ioaddr + ReceiveMode);
  1048. }
  1049. static void rio_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1050. {
  1051. struct netdev_private *np = netdev_priv(dev);
  1052. strcpy(info->driver, "dl2k");
  1053. strcpy(info->version, DRV_VERSION);
  1054. strcpy(info->bus_info, pci_name(np->pdev));
  1055. }
  1056. static int rio_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1057. {
  1058. struct netdev_private *np = netdev_priv(dev);
  1059. if (np->phy_media) {
  1060. /* fiber device */
  1061. cmd->supported = SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  1062. cmd->advertising= ADVERTISED_Autoneg | ADVERTISED_FIBRE;
  1063. cmd->port = PORT_FIBRE;
  1064. cmd->transceiver = XCVR_INTERNAL;
  1065. } else {
  1066. /* copper device */
  1067. cmd->supported = SUPPORTED_10baseT_Half |
  1068. SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Half
  1069. | SUPPORTED_100baseT_Full | SUPPORTED_1000baseT_Full |
  1070. SUPPORTED_Autoneg | SUPPORTED_MII;
  1071. cmd->advertising = ADVERTISED_10baseT_Half |
  1072. ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Half |
  1073. ADVERTISED_100baseT_Full | ADVERTISED_1000baseT_Full|
  1074. ADVERTISED_Autoneg | ADVERTISED_MII;
  1075. cmd->port = PORT_MII;
  1076. cmd->transceiver = XCVR_INTERNAL;
  1077. }
  1078. if ( np->link_status ) {
  1079. cmd->speed = np->speed;
  1080. cmd->duplex = np->full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  1081. } else {
  1082. cmd->speed = -1;
  1083. cmd->duplex = -1;
  1084. }
  1085. if ( np->an_enable)
  1086. cmd->autoneg = AUTONEG_ENABLE;
  1087. else
  1088. cmd->autoneg = AUTONEG_DISABLE;
  1089. cmd->phy_address = np->phy_addr;
  1090. return 0;
  1091. }
  1092. static int rio_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1093. {
  1094. struct netdev_private *np = netdev_priv(dev);
  1095. netif_carrier_off(dev);
  1096. if (cmd->autoneg == AUTONEG_ENABLE) {
  1097. if (np->an_enable)
  1098. return 0;
  1099. else {
  1100. np->an_enable = 1;
  1101. mii_set_media(dev);
  1102. return 0;
  1103. }
  1104. } else {
  1105. np->an_enable = 0;
  1106. if (np->speed == 1000) {
  1107. cmd->speed = SPEED_100;
  1108. cmd->duplex = DUPLEX_FULL;
  1109. printk("Warning!! Can't disable Auto negotiation in 1000Mbps, change to Manual 100Mbps, Full duplex.\n");
  1110. }
  1111. switch(cmd->speed + cmd->duplex) {
  1112. case SPEED_10 + DUPLEX_HALF:
  1113. np->speed = 10;
  1114. np->full_duplex = 0;
  1115. break;
  1116. case SPEED_10 + DUPLEX_FULL:
  1117. np->speed = 10;
  1118. np->full_duplex = 1;
  1119. break;
  1120. case SPEED_100 + DUPLEX_HALF:
  1121. np->speed = 100;
  1122. np->full_duplex = 0;
  1123. break;
  1124. case SPEED_100 + DUPLEX_FULL:
  1125. np->speed = 100;
  1126. np->full_duplex = 1;
  1127. break;
  1128. case SPEED_1000 + DUPLEX_HALF:/* not supported */
  1129. case SPEED_1000 + DUPLEX_FULL:/* not supported */
  1130. default:
  1131. return -EINVAL;
  1132. }
  1133. mii_set_media(dev);
  1134. }
  1135. return 0;
  1136. }
  1137. static u32 rio_get_link(struct net_device *dev)
  1138. {
  1139. struct netdev_private *np = netdev_priv(dev);
  1140. return np->link_status;
  1141. }
  1142. static const struct ethtool_ops ethtool_ops = {
  1143. .get_drvinfo = rio_get_drvinfo,
  1144. .get_settings = rio_get_settings,
  1145. .set_settings = rio_set_settings,
  1146. .get_link = rio_get_link,
  1147. };
  1148. static int
  1149. rio_ioctl (struct net_device *dev, struct ifreq *rq, int cmd)
  1150. {
  1151. int phy_addr;
  1152. struct netdev_private *np = netdev_priv(dev);
  1153. struct mii_data *miidata = (struct mii_data *) &rq->ifr_ifru;
  1154. struct netdev_desc *desc;
  1155. int i;
  1156. phy_addr = np->phy_addr;
  1157. switch (cmd) {
  1158. case SIOCDEVPRIVATE:
  1159. break;
  1160. case SIOCDEVPRIVATE + 1:
  1161. miidata->out_value = mii_read (dev, phy_addr, miidata->reg_num);
  1162. break;
  1163. case SIOCDEVPRIVATE + 2:
  1164. mii_write (dev, phy_addr, miidata->reg_num, miidata->in_value);
  1165. break;
  1166. case SIOCDEVPRIVATE + 3:
  1167. break;
  1168. case SIOCDEVPRIVATE + 4:
  1169. break;
  1170. case SIOCDEVPRIVATE + 5:
  1171. netif_stop_queue (dev);
  1172. break;
  1173. case SIOCDEVPRIVATE + 6:
  1174. netif_wake_queue (dev);
  1175. break;
  1176. case SIOCDEVPRIVATE + 7:
  1177. printk
  1178. ("tx_full=%x cur_tx=%lx old_tx=%lx cur_rx=%lx old_rx=%lx\n",
  1179. netif_queue_stopped(dev), np->cur_tx, np->old_tx, np->cur_rx,
  1180. np->old_rx);
  1181. break;
  1182. case SIOCDEVPRIVATE + 8:
  1183. printk("TX ring:\n");
  1184. for (i = 0; i < TX_RING_SIZE; i++) {
  1185. desc = &np->tx_ring[i];
  1186. printk
  1187. ("%02x:cur:%08x next:%08x status:%08x frag1:%08x frag0:%08x",
  1188. i,
  1189. (u32) (np->tx_ring_dma + i * sizeof (*desc)),
  1190. (u32) desc->next_desc,
  1191. (u32) desc->status, (u32) (desc->fraginfo >> 32),
  1192. (u32) desc->fraginfo);
  1193. printk ("\n");
  1194. }
  1195. printk ("\n");
  1196. break;
  1197. default:
  1198. return -EOPNOTSUPP;
  1199. }
  1200. return 0;
  1201. }
  1202. #define EEP_READ 0x0200
  1203. #define EEP_BUSY 0x8000
  1204. /* Read the EEPROM word */
  1205. /* We use I/O instruction to read/write eeprom to avoid fail on some machines */
  1206. int
  1207. read_eeprom (long ioaddr, int eep_addr)
  1208. {
  1209. int i = 1000;
  1210. outw (EEP_READ | (eep_addr & 0xff), ioaddr + EepromCtrl);
  1211. while (i-- > 0) {
  1212. if (!(inw (ioaddr + EepromCtrl) & EEP_BUSY)) {
  1213. return inw (ioaddr + EepromData);
  1214. }
  1215. }
  1216. return 0;
  1217. }
  1218. enum phy_ctrl_bits {
  1219. MII_READ = 0x00, MII_CLK = 0x01, MII_DATA1 = 0x02, MII_WRITE = 0x04,
  1220. MII_DUPLEX = 0x08,
  1221. };
  1222. #define mii_delay() readb(ioaddr)
  1223. static void
  1224. mii_sendbit (struct net_device *dev, u32 data)
  1225. {
  1226. long ioaddr = dev->base_addr + PhyCtrl;
  1227. data = (data) ? MII_DATA1 : 0;
  1228. data |= MII_WRITE;
  1229. data |= (readb (ioaddr) & 0xf8) | MII_WRITE;
  1230. writeb (data, ioaddr);
  1231. mii_delay ();
  1232. writeb (data | MII_CLK, ioaddr);
  1233. mii_delay ();
  1234. }
  1235. static int
  1236. mii_getbit (struct net_device *dev)
  1237. {
  1238. long ioaddr = dev->base_addr + PhyCtrl;
  1239. u8 data;
  1240. data = (readb (ioaddr) & 0xf8) | MII_READ;
  1241. writeb (data, ioaddr);
  1242. mii_delay ();
  1243. writeb (data | MII_CLK, ioaddr);
  1244. mii_delay ();
  1245. return ((readb (ioaddr) >> 1) & 1);
  1246. }
  1247. static void
  1248. mii_send_bits (struct net_device *dev, u32 data, int len)
  1249. {
  1250. int i;
  1251. for (i = len - 1; i >= 0; i--) {
  1252. mii_sendbit (dev, data & (1 << i));
  1253. }
  1254. }
  1255. static int
  1256. mii_read (struct net_device *dev, int phy_addr, int reg_num)
  1257. {
  1258. u32 cmd;
  1259. int i;
  1260. u32 retval = 0;
  1261. /* Preamble */
  1262. mii_send_bits (dev, 0xffffffff, 32);
  1263. /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
  1264. /* ST,OP = 0110'b for read operation */
  1265. cmd = (0x06 << 10 | phy_addr << 5 | reg_num);
  1266. mii_send_bits (dev, cmd, 14);
  1267. /* Turnaround */
  1268. if (mii_getbit (dev))
  1269. goto err_out;
  1270. /* Read data */
  1271. for (i = 0; i < 16; i++) {
  1272. retval |= mii_getbit (dev);
  1273. retval <<= 1;
  1274. }
  1275. /* End cycle */
  1276. mii_getbit (dev);
  1277. return (retval >> 1) & 0xffff;
  1278. err_out:
  1279. return 0;
  1280. }
  1281. static int
  1282. mii_write (struct net_device *dev, int phy_addr, int reg_num, u16 data)
  1283. {
  1284. u32 cmd;
  1285. /* Preamble */
  1286. mii_send_bits (dev, 0xffffffff, 32);
  1287. /* ST(2), OP(2), ADDR(5), REG#(5), TA(2), Data(16) total 32 bits */
  1288. /* ST,OP,AAAAA,RRRRR,TA = 0101xxxxxxxxxx10'b = 0x5002 for write */
  1289. cmd = (0x5002 << 16) | (phy_addr << 23) | (reg_num << 18) | data;
  1290. mii_send_bits (dev, cmd, 32);
  1291. /* End cycle */
  1292. mii_getbit (dev);
  1293. return 0;
  1294. }
  1295. static int
  1296. mii_wait_link (struct net_device *dev, int wait)
  1297. {
  1298. BMSR_t bmsr;
  1299. int phy_addr;
  1300. struct netdev_private *np;
  1301. np = netdev_priv(dev);
  1302. phy_addr = np->phy_addr;
  1303. do {
  1304. bmsr.image = mii_read (dev, phy_addr, MII_BMSR);
  1305. if (bmsr.bits.link_status)
  1306. return 0;
  1307. mdelay (1);
  1308. } while (--wait > 0);
  1309. return -1;
  1310. }
  1311. static int
  1312. mii_get_media (struct net_device *dev)
  1313. {
  1314. ANAR_t negotiate;
  1315. BMSR_t bmsr;
  1316. BMCR_t bmcr;
  1317. MSCR_t mscr;
  1318. MSSR_t mssr;
  1319. int phy_addr;
  1320. struct netdev_private *np;
  1321. np = netdev_priv(dev);
  1322. phy_addr = np->phy_addr;
  1323. bmsr.image = mii_read (dev, phy_addr, MII_BMSR);
  1324. if (np->an_enable) {
  1325. if (!bmsr.bits.an_complete) {
  1326. /* Auto-Negotiation not completed */
  1327. return -1;
  1328. }
  1329. negotiate.image = mii_read (dev, phy_addr, MII_ANAR) &
  1330. mii_read (dev, phy_addr, MII_ANLPAR);
  1331. mscr.image = mii_read (dev, phy_addr, MII_MSCR);
  1332. mssr.image = mii_read (dev, phy_addr, MII_MSSR);
  1333. if (mscr.bits.media_1000BT_FD & mssr.bits.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.bits.media_1000BT_HD & mssr.bits.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.bits.media_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.bits.media_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.bits.media_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.bits.media_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.bits.pause) {
  1359. np->tx_flow &= 1;
  1360. np->rx_flow &= 1;
  1361. } else if (negotiate.bits.asymmetric) {
  1362. np->tx_flow = 0;
  1363. np->rx_flow &= 1;
  1364. }
  1365. /* else tx_flow, rx_flow = user select */
  1366. } else {
  1367. bmcr.image = mii_read (dev, phy_addr, MII_BMCR);
  1368. if (bmcr.bits.speed100 == 1 && bmcr.bits.speed1000 == 0) {
  1369. printk (KERN_INFO "Operating at 100 Mbps, ");
  1370. } else if (bmcr.bits.speed100 == 0 && bmcr.bits.speed1000 == 0) {
  1371. printk (KERN_INFO "Operating at 10 Mbps, ");
  1372. } else if (bmcr.bits.speed100 == 0 && bmcr.bits.speed1000 == 1) {
  1373. printk (KERN_INFO "Operating at 1000 Mbps, ");
  1374. }
  1375. if (bmcr.bits.duplex_mode) {
  1376. printk ("Full duplex\n");
  1377. } else {
  1378. printk ("Half duplex\n");
  1379. }
  1380. }
  1381. if (np->tx_flow)
  1382. printk(KERN_INFO "Enable Tx Flow Control\n");
  1383. else
  1384. printk(KERN_INFO "Disable Tx Flow Control\n");
  1385. if (np->rx_flow)
  1386. printk(KERN_INFO "Enable Rx Flow Control\n");
  1387. else
  1388. printk(KERN_INFO "Disable Rx Flow Control\n");
  1389. return 0;
  1390. }
  1391. static int
  1392. mii_set_media (struct net_device *dev)
  1393. {
  1394. PHY_SCR_t pscr;
  1395. BMCR_t bmcr;
  1396. BMSR_t bmsr;
  1397. ANAR_t anar;
  1398. int phy_addr;
  1399. struct netdev_private *np;
  1400. np = netdev_priv(dev);
  1401. phy_addr = np->phy_addr;
  1402. /* Does user set speed? */
  1403. if (np->an_enable) {
  1404. /* Advertise capabilities */
  1405. bmsr.image = mii_read (dev, phy_addr, MII_BMSR);
  1406. anar.image = mii_read (dev, phy_addr, MII_ANAR);
  1407. anar.bits.media_100BX_FD = bmsr.bits.media_100BX_FD;
  1408. anar.bits.media_100BX_HD = bmsr.bits.media_100BX_HD;
  1409. anar.bits.media_100BT4 = bmsr.bits.media_100BT4;
  1410. anar.bits.media_10BT_FD = bmsr.bits.media_10BT_FD;
  1411. anar.bits.media_10BT_HD = bmsr.bits.media_10BT_HD;
  1412. anar.bits.pause = 1;
  1413. anar.bits.asymmetric = 1;
  1414. mii_write (dev, phy_addr, MII_ANAR, anar.image);
  1415. /* Enable Auto crossover */
  1416. pscr.image = mii_read (dev, phy_addr, MII_PHY_SCR);
  1417. pscr.bits.mdi_crossover_mode = 3; /* 11'b */
  1418. mii_write (dev, phy_addr, MII_PHY_SCR, pscr.image);
  1419. /* Soft reset PHY */
  1420. mii_write (dev, phy_addr, MII_BMCR, MII_BMCR_RESET);
  1421. bmcr.image = 0;
  1422. bmcr.bits.an_enable = 1;
  1423. bmcr.bits.restart_an = 1;
  1424. bmcr.bits.reset = 1;
  1425. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1426. mdelay(1);
  1427. } else {
  1428. /* Force speed setting */
  1429. /* 1) Disable Auto crossover */
  1430. pscr.image = mii_read (dev, phy_addr, MII_PHY_SCR);
  1431. pscr.bits.mdi_crossover_mode = 0;
  1432. mii_write (dev, phy_addr, MII_PHY_SCR, pscr.image);
  1433. /* 2) PHY Reset */
  1434. bmcr.image = mii_read (dev, phy_addr, MII_BMCR);
  1435. bmcr.bits.reset = 1;
  1436. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1437. /* 3) Power Down */
  1438. bmcr.image = 0x1940; /* must be 0x1940 */
  1439. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1440. mdelay (100); /* wait a certain time */
  1441. /* 4) Advertise nothing */
  1442. mii_write (dev, phy_addr, MII_ANAR, 0);
  1443. /* 5) Set media and Power Up */
  1444. bmcr.image = 0;
  1445. bmcr.bits.power_down = 1;
  1446. if (np->speed == 100) {
  1447. bmcr.bits.speed100 = 1;
  1448. bmcr.bits.speed1000 = 0;
  1449. printk (KERN_INFO "Manual 100 Mbps, ");
  1450. } else if (np->speed == 10) {
  1451. bmcr.bits.speed100 = 0;
  1452. bmcr.bits.speed1000 = 0;
  1453. printk (KERN_INFO "Manual 10 Mbps, ");
  1454. }
  1455. if (np->full_duplex) {
  1456. bmcr.bits.duplex_mode = 1;
  1457. printk ("Full duplex\n");
  1458. } else {
  1459. bmcr.bits.duplex_mode = 0;
  1460. printk ("Half duplex\n");
  1461. }
  1462. #if 0
  1463. /* Set 1000BaseT Master/Slave setting */
  1464. mscr.image = mii_read (dev, phy_addr, MII_MSCR);
  1465. mscr.bits.cfg_enable = 1;
  1466. mscr.bits.cfg_value = 0;
  1467. #endif
  1468. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1469. mdelay(10);
  1470. }
  1471. return 0;
  1472. }
  1473. static int
  1474. mii_get_media_pcs (struct net_device *dev)
  1475. {
  1476. ANAR_PCS_t negotiate;
  1477. BMSR_t bmsr;
  1478. BMCR_t bmcr;
  1479. int phy_addr;
  1480. struct netdev_private *np;
  1481. np = netdev_priv(dev);
  1482. phy_addr = np->phy_addr;
  1483. bmsr.image = mii_read (dev, phy_addr, PCS_BMSR);
  1484. if (np->an_enable) {
  1485. if (!bmsr.bits.an_complete) {
  1486. /* Auto-Negotiation not completed */
  1487. return -1;
  1488. }
  1489. negotiate.image = mii_read (dev, phy_addr, PCS_ANAR) &
  1490. mii_read (dev, phy_addr, PCS_ANLPAR);
  1491. np->speed = 1000;
  1492. if (negotiate.bits.full_duplex) {
  1493. printk (KERN_INFO "Auto 1000 Mbps, Full duplex\n");
  1494. np->full_duplex = 1;
  1495. } else {
  1496. printk (KERN_INFO "Auto 1000 Mbps, half duplex\n");
  1497. np->full_duplex = 0;
  1498. }
  1499. if (negotiate.bits.pause) {
  1500. np->tx_flow &= 1;
  1501. np->rx_flow &= 1;
  1502. } else if (negotiate.bits.asymmetric) {
  1503. np->tx_flow = 0;
  1504. np->rx_flow &= 1;
  1505. }
  1506. /* else tx_flow, rx_flow = user select */
  1507. } else {
  1508. bmcr.image = mii_read (dev, phy_addr, PCS_BMCR);
  1509. printk (KERN_INFO "Operating at 1000 Mbps, ");
  1510. if (bmcr.bits.duplex_mode) {
  1511. printk ("Full duplex\n");
  1512. } else {
  1513. printk ("Half duplex\n");
  1514. }
  1515. }
  1516. if (np->tx_flow)
  1517. printk(KERN_INFO "Enable Tx Flow Control\n");
  1518. else
  1519. printk(KERN_INFO "Disable Tx Flow Control\n");
  1520. if (np->rx_flow)
  1521. printk(KERN_INFO "Enable Rx Flow Control\n");
  1522. else
  1523. printk(KERN_INFO "Disable Rx Flow Control\n");
  1524. return 0;
  1525. }
  1526. static int
  1527. mii_set_media_pcs (struct net_device *dev)
  1528. {
  1529. BMCR_t bmcr;
  1530. ESR_t esr;
  1531. ANAR_PCS_t anar;
  1532. int phy_addr;
  1533. struct netdev_private *np;
  1534. np = netdev_priv(dev);
  1535. phy_addr = np->phy_addr;
  1536. /* Auto-Negotiation? */
  1537. if (np->an_enable) {
  1538. /* Advertise capabilities */
  1539. esr.image = mii_read (dev, phy_addr, PCS_ESR);
  1540. anar.image = mii_read (dev, phy_addr, MII_ANAR);
  1541. anar.bits.half_duplex =
  1542. esr.bits.media_1000BT_HD | esr.bits.media_1000BX_HD;
  1543. anar.bits.full_duplex =
  1544. esr.bits.media_1000BT_FD | esr.bits.media_1000BX_FD;
  1545. anar.bits.pause = 1;
  1546. anar.bits.asymmetric = 1;
  1547. mii_write (dev, phy_addr, MII_ANAR, anar.image);
  1548. /* Soft reset PHY */
  1549. mii_write (dev, phy_addr, MII_BMCR, MII_BMCR_RESET);
  1550. bmcr.image = 0;
  1551. bmcr.bits.an_enable = 1;
  1552. bmcr.bits.restart_an = 1;
  1553. bmcr.bits.reset = 1;
  1554. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1555. mdelay(1);
  1556. } else {
  1557. /* Force speed setting */
  1558. /* PHY Reset */
  1559. bmcr.image = 0;
  1560. bmcr.bits.reset = 1;
  1561. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1562. mdelay(10);
  1563. bmcr.image = 0;
  1564. bmcr.bits.an_enable = 0;
  1565. if (np->full_duplex) {
  1566. bmcr.bits.duplex_mode = 1;
  1567. printk (KERN_INFO "Manual full duplex\n");
  1568. } else {
  1569. bmcr.bits.duplex_mode = 0;
  1570. printk (KERN_INFO "Manual half duplex\n");
  1571. }
  1572. mii_write (dev, phy_addr, MII_BMCR, bmcr.image);
  1573. mdelay(10);
  1574. /* Advertise nothing */
  1575. mii_write (dev, phy_addr, MII_ANAR, 0);
  1576. }
  1577. return 0;
  1578. }
  1579. static int
  1580. rio_close (struct net_device *dev)
  1581. {
  1582. long ioaddr = dev->base_addr;
  1583. struct netdev_private *np = netdev_priv(dev);
  1584. struct sk_buff *skb;
  1585. int i;
  1586. netif_stop_queue (dev);
  1587. /* Disable interrupts */
  1588. writew (0, ioaddr + IntEnable);
  1589. /* Stop Tx and Rx logics */
  1590. writel (TxDisable | RxDisable | StatsDisable, ioaddr + MACCtrl);
  1591. synchronize_irq (dev->irq);
  1592. free_irq (dev->irq, dev);
  1593. del_timer_sync (&np->timer);
  1594. /* Free all the skbuffs in the queue. */
  1595. for (i = 0; i < RX_RING_SIZE; i++) {
  1596. np->rx_ring[i].status = 0;
  1597. np->rx_ring[i].fraginfo = 0;
  1598. skb = np->rx_skbuff[i];
  1599. if (skb) {
  1600. pci_unmap_single(np->pdev,
  1601. np->rx_ring[i].fraginfo & DMA_48BIT_MASK,
  1602. skb->len, PCI_DMA_FROMDEVICE);
  1603. dev_kfree_skb (skb);
  1604. np->rx_skbuff[i] = NULL;
  1605. }
  1606. }
  1607. for (i = 0; i < TX_RING_SIZE; i++) {
  1608. skb = np->tx_skbuff[i];
  1609. if (skb) {
  1610. pci_unmap_single(np->pdev,
  1611. np->tx_ring[i].fraginfo & DMA_48BIT_MASK,
  1612. skb->len, PCI_DMA_TODEVICE);
  1613. dev_kfree_skb (skb);
  1614. np->tx_skbuff[i] = NULL;
  1615. }
  1616. }
  1617. return 0;
  1618. }
  1619. static void __devexit
  1620. rio_remove1 (struct pci_dev *pdev)
  1621. {
  1622. struct net_device *dev = pci_get_drvdata (pdev);
  1623. if (dev) {
  1624. struct netdev_private *np = netdev_priv(dev);
  1625. unregister_netdev (dev);
  1626. pci_free_consistent (pdev, RX_TOTAL_SIZE, np->rx_ring,
  1627. np->rx_ring_dma);
  1628. pci_free_consistent (pdev, TX_TOTAL_SIZE, np->tx_ring,
  1629. np->tx_ring_dma);
  1630. #ifdef MEM_MAPPING
  1631. iounmap ((char *) (dev->base_addr));
  1632. #endif
  1633. free_netdev (dev);
  1634. pci_release_regions (pdev);
  1635. pci_disable_device (pdev);
  1636. }
  1637. pci_set_drvdata (pdev, NULL);
  1638. }
  1639. static struct pci_driver rio_driver = {
  1640. .name = "dl2k",
  1641. .id_table = rio_pci_tbl,
  1642. .probe = rio_probe1,
  1643. .remove = __devexit_p(rio_remove1),
  1644. };
  1645. static int __init
  1646. rio_init (void)
  1647. {
  1648. return pci_register_driver(&rio_driver);
  1649. }
  1650. static void __exit
  1651. rio_exit (void)
  1652. {
  1653. pci_unregister_driver (&rio_driver);
  1654. }
  1655. module_init (rio_init);
  1656. module_exit (rio_exit);
  1657. /*
  1658. Compile command:
  1659. gcc -D__KERNEL__ -DMODULE -I/usr/src/linux/include -Wall -Wstrict-prototypes -O2 -c dl2k.c
  1660. Read Documentation/networking/dl2k.txt for details.
  1661. */