dl2k.c 48 KB

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