dl2k.c 48 KB

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