dl2k.c 48 KB

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