r6040.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. * RDC R6040 Fast Ethernet MAC support
  3. *
  4. * Copyright (C) 2004 Sten Wang <sten.wang@rdc.com.tw>
  5. * Copyright (C) 2007
  6. * Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
  7. * Copyright (C) 2007-2012 Florian Fainelli <f.fainelli@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301, USA.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/string.h>
  28. #include <linux/timer.h>
  29. #include <linux/errno.h>
  30. #include <linux/ioport.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/pci.h>
  33. #include <linux/netdevice.h>
  34. #include <linux/etherdevice.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/delay.h>
  37. #include <linux/mii.h>
  38. #include <linux/ethtool.h>
  39. #include <linux/crc32.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/bitops.h>
  42. #include <linux/io.h>
  43. #include <linux/irq.h>
  44. #include <linux/uaccess.h>
  45. #include <linux/phy.h>
  46. #include <asm/processor.h>
  47. #define DRV_NAME "r6040"
  48. #define DRV_VERSION "0.29"
  49. #define DRV_RELDATE "04Jul2016"
  50. /* Time in jiffies before concluding the transmitter is hung. */
  51. #define TX_TIMEOUT (6000 * HZ / 1000)
  52. /* RDC MAC I/O Size */
  53. #define R6040_IO_SIZE 256
  54. /* MAX RDC MAC */
  55. #define MAX_MAC 2
  56. /* MAC registers */
  57. #define MCR0 0x00 /* Control register 0 */
  58. #define MCR0_RCVEN 0x0002 /* Receive enable */
  59. #define MCR0_PROMISC 0x0020 /* Promiscuous mode */
  60. #define MCR0_HASH_EN 0x0100 /* Enable multicast hash table function */
  61. #define MCR0_XMTEN 0x1000 /* Transmission enable */
  62. #define MCR0_FD 0x8000 /* Full/Half duplex */
  63. #define MCR1 0x04 /* Control register 1 */
  64. #define MAC_RST 0x0001 /* Reset the MAC */
  65. #define MBCR 0x08 /* Bus control */
  66. #define MT_ICR 0x0C /* TX interrupt control */
  67. #define MR_ICR 0x10 /* RX interrupt control */
  68. #define MTPR 0x14 /* TX poll command register */
  69. #define TM2TX 0x0001 /* Trigger MAC to transmit */
  70. #define MR_BSR 0x18 /* RX buffer size */
  71. #define MR_DCR 0x1A /* RX descriptor control */
  72. #define MLSR 0x1C /* Last status */
  73. #define TX_FIFO_UNDR 0x0200 /* TX FIFO under-run */
  74. #define TX_EXCEEDC 0x2000 /* Transmit exceed collision */
  75. #define TX_LATEC 0x4000 /* Transmit late collision */
  76. #define MMDIO 0x20 /* MDIO control register */
  77. #define MDIO_WRITE 0x4000 /* MDIO write */
  78. #define MDIO_READ 0x2000 /* MDIO read */
  79. #define MMRD 0x24 /* MDIO read data register */
  80. #define MMWD 0x28 /* MDIO write data register */
  81. #define MTD_SA0 0x2C /* TX descriptor start address 0 */
  82. #define MTD_SA1 0x30 /* TX descriptor start address 1 */
  83. #define MRD_SA0 0x34 /* RX descriptor start address 0 */
  84. #define MRD_SA1 0x38 /* RX descriptor start address 1 */
  85. #define MISR 0x3C /* Status register */
  86. #define MIER 0x40 /* INT enable register */
  87. #define MSK_INT 0x0000 /* Mask off interrupts */
  88. #define RX_FINISH 0x0001 /* RX finished */
  89. #define RX_NO_DESC 0x0002 /* No RX descriptor available */
  90. #define RX_FIFO_FULL 0x0004 /* RX FIFO full */
  91. #define RX_EARLY 0x0008 /* RX early */
  92. #define TX_FINISH 0x0010 /* TX finished */
  93. #define TX_EARLY 0x0080 /* TX early */
  94. #define EVENT_OVRFL 0x0100 /* Event counter overflow */
  95. #define LINK_CHANGED 0x0200 /* PHY link changed */
  96. #define ME_CISR 0x44 /* Event counter INT status */
  97. #define ME_CIER 0x48 /* Event counter INT enable */
  98. #define MR_CNT 0x50 /* Successfully received packet counter */
  99. #define ME_CNT0 0x52 /* Event counter 0 */
  100. #define ME_CNT1 0x54 /* Event counter 1 */
  101. #define ME_CNT2 0x56 /* Event counter 2 */
  102. #define ME_CNT3 0x58 /* Event counter 3 */
  103. #define MT_CNT 0x5A /* Successfully transmit packet counter */
  104. #define ME_CNT4 0x5C /* Event counter 4 */
  105. #define MP_CNT 0x5E /* Pause frame counter register */
  106. #define MAR0 0x60 /* Hash table 0 */
  107. #define MAR1 0x62 /* Hash table 1 */
  108. #define MAR2 0x64 /* Hash table 2 */
  109. #define MAR3 0x66 /* Hash table 3 */
  110. #define MID_0L 0x68 /* Multicast address MID0 Low */
  111. #define MID_0M 0x6A /* Multicast address MID0 Medium */
  112. #define MID_0H 0x6C /* Multicast address MID0 High */
  113. #define MID_1L 0x70 /* MID1 Low */
  114. #define MID_1M 0x72 /* MID1 Medium */
  115. #define MID_1H 0x74 /* MID1 High */
  116. #define MID_2L 0x78 /* MID2 Low */
  117. #define MID_2M 0x7A /* MID2 Medium */
  118. #define MID_2H 0x7C /* MID2 High */
  119. #define MID_3L 0x80 /* MID3 Low */
  120. #define MID_3M 0x82 /* MID3 Medium */
  121. #define MID_3H 0x84 /* MID3 High */
  122. #define PHY_CC 0x88 /* PHY status change configuration register */
  123. #define SCEN 0x8000 /* PHY status change enable */
  124. #define PHYAD_SHIFT 8 /* PHY address shift */
  125. #define TMRDIV_SHIFT 0 /* Timer divider shift */
  126. #define PHY_ST 0x8A /* PHY status register */
  127. #define MAC_SM 0xAC /* MAC status machine */
  128. #define MAC_SM_RST 0x0002 /* MAC status machine reset */
  129. #define MAC_ID 0xBE /* Identifier register */
  130. #define TX_DCNT 0x80 /* TX descriptor count */
  131. #define RX_DCNT 0x80 /* RX descriptor count */
  132. #define MAX_BUF_SIZE 0x600
  133. #define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor))
  134. #define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor))
  135. #define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
  136. #define MCAST_MAX 3 /* Max number multicast addresses to filter */
  137. #define MAC_DEF_TIMEOUT 2048 /* Default MAC read/write operation timeout */
  138. /* Descriptor status */
  139. #define DSC_OWNER_MAC 0x8000 /* MAC is the owner of this descriptor */
  140. #define DSC_RX_OK 0x4000 /* RX was successful */
  141. #define DSC_RX_ERR 0x0800 /* RX PHY error */
  142. #define DSC_RX_ERR_DRI 0x0400 /* RX dribble packet */
  143. #define DSC_RX_ERR_BUF 0x0200 /* RX length exceeds buffer size */
  144. #define DSC_RX_ERR_LONG 0x0100 /* RX length > maximum packet length */
  145. #define DSC_RX_ERR_RUNT 0x0080 /* RX packet length < 64 byte */
  146. #define DSC_RX_ERR_CRC 0x0040 /* RX CRC error */
  147. #define DSC_RX_BCAST 0x0020 /* RX broadcast (no error) */
  148. #define DSC_RX_MCAST 0x0010 /* RX multicast (no error) */
  149. #define DSC_RX_MCH_HIT 0x0008 /* RX multicast hit in hash table (no error) */
  150. #define DSC_RX_MIDH_HIT 0x0004 /* RX MID table hit (no error) */
  151. #define DSC_RX_IDX_MID_MASK 3 /* RX mask for the index of matched MIDx */
  152. MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>,"
  153. "Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>,"
  154. "Florian Fainelli <f.fainelli@gmail.com>");
  155. MODULE_LICENSE("GPL");
  156. MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet driver");
  157. MODULE_VERSION(DRV_VERSION " " DRV_RELDATE);
  158. /* RX and TX interrupts that we handle */
  159. #define RX_INTS (RX_FIFO_FULL | RX_NO_DESC | RX_FINISH)
  160. #define TX_INTS (TX_FINISH)
  161. #define INT_MASK (RX_INTS | TX_INTS)
  162. struct r6040_descriptor {
  163. u16 status, len; /* 0-3 */
  164. __le32 buf; /* 4-7 */
  165. __le32 ndesc; /* 8-B */
  166. u32 rev1; /* C-F */
  167. char *vbufp; /* 10-13 */
  168. struct r6040_descriptor *vndescp; /* 14-17 */
  169. struct sk_buff *skb_ptr; /* 18-1B */
  170. u32 rev2; /* 1C-1F */
  171. } __aligned(32);
  172. struct r6040_private {
  173. spinlock_t lock; /* driver lock */
  174. struct pci_dev *pdev;
  175. struct r6040_descriptor *rx_insert_ptr;
  176. struct r6040_descriptor *rx_remove_ptr;
  177. struct r6040_descriptor *tx_insert_ptr;
  178. struct r6040_descriptor *tx_remove_ptr;
  179. struct r6040_descriptor *rx_ring;
  180. struct r6040_descriptor *tx_ring;
  181. dma_addr_t rx_ring_dma;
  182. dma_addr_t tx_ring_dma;
  183. u16 tx_free_desc;
  184. u16 mcr0;
  185. struct net_device *dev;
  186. struct mii_bus *mii_bus;
  187. struct napi_struct napi;
  188. void __iomem *base;
  189. int old_link;
  190. int old_duplex;
  191. };
  192. static char version[] = DRV_NAME
  193. ": RDC R6040 NAPI net driver,"
  194. "version "DRV_VERSION " (" DRV_RELDATE ")";
  195. /* Read a word data from PHY Chip */
  196. static int r6040_phy_read(void __iomem *ioaddr, int phy_addr, int reg)
  197. {
  198. int limit = MAC_DEF_TIMEOUT;
  199. u16 cmd;
  200. iowrite16(MDIO_READ + reg + (phy_addr << 8), ioaddr + MMDIO);
  201. /* Wait for the read bit to be cleared */
  202. while (limit--) {
  203. cmd = ioread16(ioaddr + MMDIO);
  204. if (!(cmd & MDIO_READ))
  205. break;
  206. udelay(1);
  207. }
  208. if (limit < 0)
  209. return -ETIMEDOUT;
  210. return ioread16(ioaddr + MMRD);
  211. }
  212. /* Write a word data from PHY Chip */
  213. static int r6040_phy_write(void __iomem *ioaddr,
  214. int phy_addr, int reg, u16 val)
  215. {
  216. int limit = MAC_DEF_TIMEOUT;
  217. u16 cmd;
  218. iowrite16(val, ioaddr + MMWD);
  219. /* Write the command to the MDIO bus */
  220. iowrite16(MDIO_WRITE + reg + (phy_addr << 8), ioaddr + MMDIO);
  221. /* Wait for the write bit to be cleared */
  222. while (limit--) {
  223. cmd = ioread16(ioaddr + MMDIO);
  224. if (!(cmd & MDIO_WRITE))
  225. break;
  226. udelay(1);
  227. }
  228. return (limit < 0) ? -ETIMEDOUT : 0;
  229. }
  230. static int r6040_mdiobus_read(struct mii_bus *bus, int phy_addr, int reg)
  231. {
  232. struct net_device *dev = bus->priv;
  233. struct r6040_private *lp = netdev_priv(dev);
  234. void __iomem *ioaddr = lp->base;
  235. return r6040_phy_read(ioaddr, phy_addr, reg);
  236. }
  237. static int r6040_mdiobus_write(struct mii_bus *bus, int phy_addr,
  238. int reg, u16 value)
  239. {
  240. struct net_device *dev = bus->priv;
  241. struct r6040_private *lp = netdev_priv(dev);
  242. void __iomem *ioaddr = lp->base;
  243. return r6040_phy_write(ioaddr, phy_addr, reg, value);
  244. }
  245. static void r6040_free_txbufs(struct net_device *dev)
  246. {
  247. struct r6040_private *lp = netdev_priv(dev);
  248. int i;
  249. for (i = 0; i < TX_DCNT; i++) {
  250. if (lp->tx_insert_ptr->skb_ptr) {
  251. pci_unmap_single(lp->pdev,
  252. le32_to_cpu(lp->tx_insert_ptr->buf),
  253. MAX_BUF_SIZE, PCI_DMA_TODEVICE);
  254. dev_kfree_skb(lp->tx_insert_ptr->skb_ptr);
  255. lp->tx_insert_ptr->skb_ptr = NULL;
  256. }
  257. lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
  258. }
  259. }
  260. static void r6040_free_rxbufs(struct net_device *dev)
  261. {
  262. struct r6040_private *lp = netdev_priv(dev);
  263. int i;
  264. for (i = 0; i < RX_DCNT; i++) {
  265. if (lp->rx_insert_ptr->skb_ptr) {
  266. pci_unmap_single(lp->pdev,
  267. le32_to_cpu(lp->rx_insert_ptr->buf),
  268. MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  269. dev_kfree_skb(lp->rx_insert_ptr->skb_ptr);
  270. lp->rx_insert_ptr->skb_ptr = NULL;
  271. }
  272. lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
  273. }
  274. }
  275. static void r6040_init_ring_desc(struct r6040_descriptor *desc_ring,
  276. dma_addr_t desc_dma, int size)
  277. {
  278. struct r6040_descriptor *desc = desc_ring;
  279. dma_addr_t mapping = desc_dma;
  280. while (size-- > 0) {
  281. mapping += sizeof(*desc);
  282. desc->ndesc = cpu_to_le32(mapping);
  283. desc->vndescp = desc + 1;
  284. desc++;
  285. }
  286. desc--;
  287. desc->ndesc = cpu_to_le32(desc_dma);
  288. desc->vndescp = desc_ring;
  289. }
  290. static void r6040_init_txbufs(struct net_device *dev)
  291. {
  292. struct r6040_private *lp = netdev_priv(dev);
  293. lp->tx_free_desc = TX_DCNT;
  294. lp->tx_remove_ptr = lp->tx_insert_ptr = lp->tx_ring;
  295. r6040_init_ring_desc(lp->tx_ring, lp->tx_ring_dma, TX_DCNT);
  296. }
  297. static int r6040_alloc_rxbufs(struct net_device *dev)
  298. {
  299. struct r6040_private *lp = netdev_priv(dev);
  300. struct r6040_descriptor *desc;
  301. struct sk_buff *skb;
  302. int rc;
  303. lp->rx_remove_ptr = lp->rx_insert_ptr = lp->rx_ring;
  304. r6040_init_ring_desc(lp->rx_ring, lp->rx_ring_dma, RX_DCNT);
  305. /* Allocate skbs for the rx descriptors */
  306. desc = lp->rx_ring;
  307. do {
  308. skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
  309. if (!skb) {
  310. rc = -ENOMEM;
  311. goto err_exit;
  312. }
  313. desc->skb_ptr = skb;
  314. desc->buf = cpu_to_le32(pci_map_single(lp->pdev,
  315. desc->skb_ptr->data,
  316. MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
  317. desc->status = DSC_OWNER_MAC;
  318. desc = desc->vndescp;
  319. } while (desc != lp->rx_ring);
  320. return 0;
  321. err_exit:
  322. /* Deallocate all previously allocated skbs */
  323. r6040_free_rxbufs(dev);
  324. return rc;
  325. }
  326. static void r6040_reset_mac(struct r6040_private *lp)
  327. {
  328. void __iomem *ioaddr = lp->base;
  329. int limit = MAC_DEF_TIMEOUT;
  330. u16 cmd;
  331. iowrite16(MAC_RST, ioaddr + MCR1);
  332. while (limit--) {
  333. cmd = ioread16(ioaddr + MCR1);
  334. if (cmd & MAC_RST)
  335. break;
  336. }
  337. /* Reset internal state machine */
  338. iowrite16(MAC_SM_RST, ioaddr + MAC_SM);
  339. iowrite16(0, ioaddr + MAC_SM);
  340. mdelay(5);
  341. }
  342. static void r6040_init_mac_regs(struct net_device *dev)
  343. {
  344. struct r6040_private *lp = netdev_priv(dev);
  345. void __iomem *ioaddr = lp->base;
  346. /* Mask Off Interrupt */
  347. iowrite16(MSK_INT, ioaddr + MIER);
  348. /* Reset RDC MAC */
  349. r6040_reset_mac(lp);
  350. /* MAC Bus Control Register */
  351. iowrite16(MBCR_DEFAULT, ioaddr + MBCR);
  352. /* Buffer Size Register */
  353. iowrite16(MAX_BUF_SIZE, ioaddr + MR_BSR);
  354. /* Write TX ring start address */
  355. iowrite16(lp->tx_ring_dma, ioaddr + MTD_SA0);
  356. iowrite16(lp->tx_ring_dma >> 16, ioaddr + MTD_SA1);
  357. /* Write RX ring start address */
  358. iowrite16(lp->rx_ring_dma, ioaddr + MRD_SA0);
  359. iowrite16(lp->rx_ring_dma >> 16, ioaddr + MRD_SA1);
  360. /* Set interrupt waiting time and packet numbers */
  361. iowrite16(0, ioaddr + MT_ICR);
  362. iowrite16(0, ioaddr + MR_ICR);
  363. /* Enable interrupts */
  364. iowrite16(INT_MASK, ioaddr + MIER);
  365. /* Enable TX and RX */
  366. iowrite16(lp->mcr0 | MCR0_RCVEN, ioaddr);
  367. /* Let TX poll the descriptors
  368. * we may got called by r6040_tx_timeout which has left
  369. * some unsent tx buffers */
  370. iowrite16(TM2TX, ioaddr + MTPR);
  371. }
  372. static void r6040_tx_timeout(struct net_device *dev)
  373. {
  374. struct r6040_private *priv = netdev_priv(dev);
  375. void __iomem *ioaddr = priv->base;
  376. netdev_warn(dev, "transmit timed out, int enable %4.4x "
  377. "status %4.4x\n",
  378. ioread16(ioaddr + MIER),
  379. ioread16(ioaddr + MISR));
  380. dev->stats.tx_errors++;
  381. /* Reset MAC and re-init all registers */
  382. r6040_init_mac_regs(dev);
  383. }
  384. static struct net_device_stats *r6040_get_stats(struct net_device *dev)
  385. {
  386. struct r6040_private *priv = netdev_priv(dev);
  387. void __iomem *ioaddr = priv->base;
  388. unsigned long flags;
  389. spin_lock_irqsave(&priv->lock, flags);
  390. dev->stats.rx_crc_errors += ioread8(ioaddr + ME_CNT1);
  391. dev->stats.multicast += ioread8(ioaddr + ME_CNT0);
  392. spin_unlock_irqrestore(&priv->lock, flags);
  393. return &dev->stats;
  394. }
  395. /* Stop RDC MAC and Free the allocated resource */
  396. static void r6040_down(struct net_device *dev)
  397. {
  398. struct r6040_private *lp = netdev_priv(dev);
  399. void __iomem *ioaddr = lp->base;
  400. u16 *adrp;
  401. /* Stop MAC */
  402. iowrite16(MSK_INT, ioaddr + MIER); /* Mask Off Interrupt */
  403. /* Reset RDC MAC */
  404. r6040_reset_mac(lp);
  405. /* Restore MAC Address to MIDx */
  406. adrp = (u16 *) dev->dev_addr;
  407. iowrite16(adrp[0], ioaddr + MID_0L);
  408. iowrite16(adrp[1], ioaddr + MID_0M);
  409. iowrite16(adrp[2], ioaddr + MID_0H);
  410. }
  411. static int r6040_close(struct net_device *dev)
  412. {
  413. struct r6040_private *lp = netdev_priv(dev);
  414. struct pci_dev *pdev = lp->pdev;
  415. phy_stop(dev->phydev);
  416. napi_disable(&lp->napi);
  417. netif_stop_queue(dev);
  418. spin_lock_irq(&lp->lock);
  419. r6040_down(dev);
  420. /* Free RX buffer */
  421. r6040_free_rxbufs(dev);
  422. /* Free TX buffer */
  423. r6040_free_txbufs(dev);
  424. spin_unlock_irq(&lp->lock);
  425. free_irq(dev->irq, dev);
  426. /* Free Descriptor memory */
  427. if (lp->rx_ring) {
  428. pci_free_consistent(pdev,
  429. RX_DESC_SIZE, lp->rx_ring, lp->rx_ring_dma);
  430. lp->rx_ring = NULL;
  431. }
  432. if (lp->tx_ring) {
  433. pci_free_consistent(pdev,
  434. TX_DESC_SIZE, lp->tx_ring, lp->tx_ring_dma);
  435. lp->tx_ring = NULL;
  436. }
  437. return 0;
  438. }
  439. static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  440. {
  441. if (!dev->phydev)
  442. return -EINVAL;
  443. return phy_mii_ioctl(dev->phydev, rq, cmd);
  444. }
  445. static int r6040_rx(struct net_device *dev, int limit)
  446. {
  447. struct r6040_private *priv = netdev_priv(dev);
  448. struct r6040_descriptor *descptr = priv->rx_remove_ptr;
  449. struct sk_buff *skb_ptr, *new_skb;
  450. int count = 0;
  451. u16 err;
  452. /* Limit not reached and the descriptor belongs to the CPU */
  453. while (count < limit && !(descptr->status & DSC_OWNER_MAC)) {
  454. /* Read the descriptor status */
  455. err = descptr->status;
  456. /* Global error status set */
  457. if (err & DSC_RX_ERR) {
  458. /* RX dribble */
  459. if (err & DSC_RX_ERR_DRI)
  460. dev->stats.rx_frame_errors++;
  461. /* Buffer length exceeded */
  462. if (err & DSC_RX_ERR_BUF)
  463. dev->stats.rx_length_errors++;
  464. /* Packet too long */
  465. if (err & DSC_RX_ERR_LONG)
  466. dev->stats.rx_length_errors++;
  467. /* Packet < 64 bytes */
  468. if (err & DSC_RX_ERR_RUNT)
  469. dev->stats.rx_length_errors++;
  470. /* CRC error */
  471. if (err & DSC_RX_ERR_CRC) {
  472. spin_lock(&priv->lock);
  473. dev->stats.rx_crc_errors++;
  474. spin_unlock(&priv->lock);
  475. }
  476. goto next_descr;
  477. }
  478. /* Packet successfully received */
  479. new_skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
  480. if (!new_skb) {
  481. dev->stats.rx_dropped++;
  482. goto next_descr;
  483. }
  484. skb_ptr = descptr->skb_ptr;
  485. skb_ptr->dev = priv->dev;
  486. /* Do not count the CRC */
  487. skb_put(skb_ptr, descptr->len - 4);
  488. pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
  489. MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  490. skb_ptr->protocol = eth_type_trans(skb_ptr, priv->dev);
  491. /* Send to upper layer */
  492. netif_receive_skb(skb_ptr);
  493. dev->stats.rx_packets++;
  494. dev->stats.rx_bytes += descptr->len - 4;
  495. /* put new skb into descriptor */
  496. descptr->skb_ptr = new_skb;
  497. descptr->buf = cpu_to_le32(pci_map_single(priv->pdev,
  498. descptr->skb_ptr->data,
  499. MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
  500. next_descr:
  501. /* put the descriptor back to the MAC */
  502. descptr->status = DSC_OWNER_MAC;
  503. descptr = descptr->vndescp;
  504. count++;
  505. }
  506. priv->rx_remove_ptr = descptr;
  507. return count;
  508. }
  509. static void r6040_tx(struct net_device *dev)
  510. {
  511. struct r6040_private *priv = netdev_priv(dev);
  512. struct r6040_descriptor *descptr;
  513. void __iomem *ioaddr = priv->base;
  514. struct sk_buff *skb_ptr;
  515. u16 err;
  516. spin_lock(&priv->lock);
  517. descptr = priv->tx_remove_ptr;
  518. while (priv->tx_free_desc < TX_DCNT) {
  519. /* Check for errors */
  520. err = ioread16(ioaddr + MLSR);
  521. if (err & TX_FIFO_UNDR)
  522. dev->stats.tx_fifo_errors++;
  523. if (err & (TX_EXCEEDC | TX_LATEC))
  524. dev->stats.tx_carrier_errors++;
  525. if (descptr->status & DSC_OWNER_MAC)
  526. break; /* Not complete */
  527. skb_ptr = descptr->skb_ptr;
  528. /* Statistic Counter */
  529. dev->stats.tx_packets++;
  530. dev->stats.tx_bytes += skb_ptr->len;
  531. pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
  532. skb_ptr->len, PCI_DMA_TODEVICE);
  533. /* Free buffer */
  534. dev_kfree_skb(skb_ptr);
  535. descptr->skb_ptr = NULL;
  536. /* To next descriptor */
  537. descptr = descptr->vndescp;
  538. priv->tx_free_desc++;
  539. }
  540. priv->tx_remove_ptr = descptr;
  541. if (priv->tx_free_desc)
  542. netif_wake_queue(dev);
  543. spin_unlock(&priv->lock);
  544. }
  545. static int r6040_poll(struct napi_struct *napi, int budget)
  546. {
  547. struct r6040_private *priv =
  548. container_of(napi, struct r6040_private, napi);
  549. struct net_device *dev = priv->dev;
  550. void __iomem *ioaddr = priv->base;
  551. int work_done;
  552. r6040_tx(dev);
  553. work_done = r6040_rx(dev, budget);
  554. if (work_done < budget) {
  555. napi_complete_done(napi, work_done);
  556. /* Enable RX/TX interrupt */
  557. iowrite16(ioread16(ioaddr + MIER) | RX_INTS | TX_INTS,
  558. ioaddr + MIER);
  559. }
  560. return work_done;
  561. }
  562. /* The RDC interrupt handler. */
  563. static irqreturn_t r6040_interrupt(int irq, void *dev_id)
  564. {
  565. struct net_device *dev = dev_id;
  566. struct r6040_private *lp = netdev_priv(dev);
  567. void __iomem *ioaddr = lp->base;
  568. u16 misr, status;
  569. /* Save MIER */
  570. misr = ioread16(ioaddr + MIER);
  571. /* Mask off RDC MAC interrupt */
  572. iowrite16(MSK_INT, ioaddr + MIER);
  573. /* Read MISR status and clear */
  574. status = ioread16(ioaddr + MISR);
  575. if (status == 0x0000 || status == 0xffff) {
  576. /* Restore RDC MAC interrupt */
  577. iowrite16(misr, ioaddr + MIER);
  578. return IRQ_NONE;
  579. }
  580. /* RX interrupt request */
  581. if (status & (RX_INTS | TX_INTS)) {
  582. if (status & RX_NO_DESC) {
  583. /* RX descriptor unavailable */
  584. dev->stats.rx_dropped++;
  585. dev->stats.rx_missed_errors++;
  586. }
  587. if (status & RX_FIFO_FULL)
  588. dev->stats.rx_fifo_errors++;
  589. if (likely(napi_schedule_prep(&lp->napi))) {
  590. /* Mask off RX interrupt */
  591. misr &= ~(RX_INTS | TX_INTS);
  592. __napi_schedule_irqoff(&lp->napi);
  593. }
  594. }
  595. /* Restore RDC MAC interrupt */
  596. iowrite16(misr, ioaddr + MIER);
  597. return IRQ_HANDLED;
  598. }
  599. #ifdef CONFIG_NET_POLL_CONTROLLER
  600. static void r6040_poll_controller(struct net_device *dev)
  601. {
  602. disable_irq(dev->irq);
  603. r6040_interrupt(dev->irq, dev);
  604. enable_irq(dev->irq);
  605. }
  606. #endif
  607. /* Init RDC MAC */
  608. static int r6040_up(struct net_device *dev)
  609. {
  610. struct r6040_private *lp = netdev_priv(dev);
  611. void __iomem *ioaddr = lp->base;
  612. int ret;
  613. /* Initialise and alloc RX/TX buffers */
  614. r6040_init_txbufs(dev);
  615. ret = r6040_alloc_rxbufs(dev);
  616. if (ret)
  617. return ret;
  618. /* improve performance (by RDC guys) */
  619. r6040_phy_write(ioaddr, 30, 17,
  620. (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
  621. r6040_phy_write(ioaddr, 30, 17,
  622. ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
  623. r6040_phy_write(ioaddr, 0, 19, 0x0000);
  624. r6040_phy_write(ioaddr, 0, 30, 0x01F0);
  625. /* Initialize all MAC registers */
  626. r6040_init_mac_regs(dev);
  627. phy_start(dev->phydev);
  628. return 0;
  629. }
  630. /* Read/set MAC address routines */
  631. static void r6040_mac_address(struct net_device *dev)
  632. {
  633. struct r6040_private *lp = netdev_priv(dev);
  634. void __iomem *ioaddr = lp->base;
  635. u16 *adrp;
  636. /* Reset MAC */
  637. r6040_reset_mac(lp);
  638. /* Restore MAC Address */
  639. adrp = (u16 *) dev->dev_addr;
  640. iowrite16(adrp[0], ioaddr + MID_0L);
  641. iowrite16(adrp[1], ioaddr + MID_0M);
  642. iowrite16(adrp[2], ioaddr + MID_0H);
  643. }
  644. static int r6040_open(struct net_device *dev)
  645. {
  646. struct r6040_private *lp = netdev_priv(dev);
  647. int ret;
  648. /* Request IRQ and Register interrupt handler */
  649. ret = request_irq(dev->irq, r6040_interrupt,
  650. IRQF_SHARED, dev->name, dev);
  651. if (ret)
  652. goto out;
  653. /* Set MAC address */
  654. r6040_mac_address(dev);
  655. /* Allocate Descriptor memory */
  656. lp->rx_ring =
  657. pci_alloc_consistent(lp->pdev, RX_DESC_SIZE, &lp->rx_ring_dma);
  658. if (!lp->rx_ring) {
  659. ret = -ENOMEM;
  660. goto err_free_irq;
  661. }
  662. lp->tx_ring =
  663. pci_alloc_consistent(lp->pdev, TX_DESC_SIZE, &lp->tx_ring_dma);
  664. if (!lp->tx_ring) {
  665. ret = -ENOMEM;
  666. goto err_free_rx_ring;
  667. }
  668. ret = r6040_up(dev);
  669. if (ret)
  670. goto err_free_tx_ring;
  671. napi_enable(&lp->napi);
  672. netif_start_queue(dev);
  673. return 0;
  674. err_free_tx_ring:
  675. pci_free_consistent(lp->pdev, TX_DESC_SIZE, lp->tx_ring,
  676. lp->tx_ring_dma);
  677. err_free_rx_ring:
  678. pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring,
  679. lp->rx_ring_dma);
  680. err_free_irq:
  681. free_irq(dev->irq, dev);
  682. out:
  683. return ret;
  684. }
  685. static netdev_tx_t r6040_start_xmit(struct sk_buff *skb,
  686. struct net_device *dev)
  687. {
  688. struct r6040_private *lp = netdev_priv(dev);
  689. struct r6040_descriptor *descptr;
  690. void __iomem *ioaddr = lp->base;
  691. unsigned long flags;
  692. if (skb_put_padto(skb, ETH_ZLEN) < 0)
  693. return NETDEV_TX_OK;
  694. /* Critical Section */
  695. spin_lock_irqsave(&lp->lock, flags);
  696. /* TX resource check */
  697. if (!lp->tx_free_desc) {
  698. spin_unlock_irqrestore(&lp->lock, flags);
  699. netif_stop_queue(dev);
  700. netdev_err(dev, ": no tx descriptor\n");
  701. return NETDEV_TX_BUSY;
  702. }
  703. /* Set TX descriptor & Transmit it */
  704. lp->tx_free_desc--;
  705. descptr = lp->tx_insert_ptr;
  706. descptr->len = skb->len;
  707. descptr->skb_ptr = skb;
  708. descptr->buf = cpu_to_le32(pci_map_single(lp->pdev,
  709. skb->data, skb->len, PCI_DMA_TODEVICE));
  710. descptr->status = DSC_OWNER_MAC;
  711. skb_tx_timestamp(skb);
  712. /* Trigger the MAC to check the TX descriptor */
  713. if (!skb->xmit_more || netif_queue_stopped(dev))
  714. iowrite16(TM2TX, ioaddr + MTPR);
  715. lp->tx_insert_ptr = descptr->vndescp;
  716. /* If no tx resource, stop */
  717. if (!lp->tx_free_desc)
  718. netif_stop_queue(dev);
  719. spin_unlock_irqrestore(&lp->lock, flags);
  720. return NETDEV_TX_OK;
  721. }
  722. static void r6040_multicast_list(struct net_device *dev)
  723. {
  724. struct r6040_private *lp = netdev_priv(dev);
  725. void __iomem *ioaddr = lp->base;
  726. unsigned long flags;
  727. struct netdev_hw_addr *ha;
  728. int i;
  729. u16 *adrp;
  730. u16 hash_table[4] = { 0 };
  731. spin_lock_irqsave(&lp->lock, flags);
  732. /* Keep our MAC Address */
  733. adrp = (u16 *)dev->dev_addr;
  734. iowrite16(adrp[0], ioaddr + MID_0L);
  735. iowrite16(adrp[1], ioaddr + MID_0M);
  736. iowrite16(adrp[2], ioaddr + MID_0H);
  737. /* Clear AMCP & PROM bits */
  738. lp->mcr0 = ioread16(ioaddr + MCR0) & ~(MCR0_PROMISC | MCR0_HASH_EN);
  739. /* Promiscuous mode */
  740. if (dev->flags & IFF_PROMISC)
  741. lp->mcr0 |= MCR0_PROMISC;
  742. /* Enable multicast hash table function to
  743. * receive all multicast packets. */
  744. else if (dev->flags & IFF_ALLMULTI) {
  745. lp->mcr0 |= MCR0_HASH_EN;
  746. for (i = 0; i < MCAST_MAX ; i++) {
  747. iowrite16(0, ioaddr + MID_1L + 8 * i);
  748. iowrite16(0, ioaddr + MID_1M + 8 * i);
  749. iowrite16(0, ioaddr + MID_1H + 8 * i);
  750. }
  751. for (i = 0; i < 4; i++)
  752. hash_table[i] = 0xffff;
  753. }
  754. /* Use internal multicast address registers if the number of
  755. * multicast addresses is not greater than MCAST_MAX. */
  756. else if (netdev_mc_count(dev) <= MCAST_MAX) {
  757. i = 0;
  758. netdev_for_each_mc_addr(ha, dev) {
  759. u16 *adrp = (u16 *) ha->addr;
  760. iowrite16(adrp[0], ioaddr + MID_1L + 8 * i);
  761. iowrite16(adrp[1], ioaddr + MID_1M + 8 * i);
  762. iowrite16(adrp[2], ioaddr + MID_1H + 8 * i);
  763. i++;
  764. }
  765. while (i < MCAST_MAX) {
  766. iowrite16(0, ioaddr + MID_1L + 8 * i);
  767. iowrite16(0, ioaddr + MID_1M + 8 * i);
  768. iowrite16(0, ioaddr + MID_1H + 8 * i);
  769. i++;
  770. }
  771. }
  772. /* Otherwise, Enable multicast hash table function. */
  773. else {
  774. u32 crc;
  775. lp->mcr0 |= MCR0_HASH_EN;
  776. for (i = 0; i < MCAST_MAX ; i++) {
  777. iowrite16(0, ioaddr + MID_1L + 8 * i);
  778. iowrite16(0, ioaddr + MID_1M + 8 * i);
  779. iowrite16(0, ioaddr + MID_1H + 8 * i);
  780. }
  781. /* Build multicast hash table */
  782. netdev_for_each_mc_addr(ha, dev) {
  783. u8 *addrs = ha->addr;
  784. crc = ether_crc(ETH_ALEN, addrs);
  785. crc >>= 26;
  786. hash_table[crc >> 4] |= 1 << (crc & 0xf);
  787. }
  788. }
  789. iowrite16(lp->mcr0, ioaddr + MCR0);
  790. /* Fill the MAC hash tables with their values */
  791. if (lp->mcr0 & MCR0_HASH_EN) {
  792. iowrite16(hash_table[0], ioaddr + MAR0);
  793. iowrite16(hash_table[1], ioaddr + MAR1);
  794. iowrite16(hash_table[2], ioaddr + MAR2);
  795. iowrite16(hash_table[3], ioaddr + MAR3);
  796. }
  797. spin_unlock_irqrestore(&lp->lock, flags);
  798. }
  799. static void netdev_get_drvinfo(struct net_device *dev,
  800. struct ethtool_drvinfo *info)
  801. {
  802. struct r6040_private *rp = netdev_priv(dev);
  803. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  804. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  805. strlcpy(info->bus_info, pci_name(rp->pdev), sizeof(info->bus_info));
  806. }
  807. static const struct ethtool_ops netdev_ethtool_ops = {
  808. .get_drvinfo = netdev_get_drvinfo,
  809. .get_link = ethtool_op_get_link,
  810. .get_ts_info = ethtool_op_get_ts_info,
  811. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  812. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  813. };
  814. static const struct net_device_ops r6040_netdev_ops = {
  815. .ndo_open = r6040_open,
  816. .ndo_stop = r6040_close,
  817. .ndo_start_xmit = r6040_start_xmit,
  818. .ndo_get_stats = r6040_get_stats,
  819. .ndo_set_rx_mode = r6040_multicast_list,
  820. .ndo_validate_addr = eth_validate_addr,
  821. .ndo_set_mac_address = eth_mac_addr,
  822. .ndo_do_ioctl = r6040_ioctl,
  823. .ndo_tx_timeout = r6040_tx_timeout,
  824. #ifdef CONFIG_NET_POLL_CONTROLLER
  825. .ndo_poll_controller = r6040_poll_controller,
  826. #endif
  827. };
  828. static void r6040_adjust_link(struct net_device *dev)
  829. {
  830. struct r6040_private *lp = netdev_priv(dev);
  831. struct phy_device *phydev = dev->phydev;
  832. int status_changed = 0;
  833. void __iomem *ioaddr = lp->base;
  834. BUG_ON(!phydev);
  835. if (lp->old_link != phydev->link) {
  836. status_changed = 1;
  837. lp->old_link = phydev->link;
  838. }
  839. /* reflect duplex change */
  840. if (phydev->link && (lp->old_duplex != phydev->duplex)) {
  841. lp->mcr0 |= (phydev->duplex == DUPLEX_FULL ? MCR0_FD : 0);
  842. iowrite16(lp->mcr0, ioaddr);
  843. status_changed = 1;
  844. lp->old_duplex = phydev->duplex;
  845. }
  846. if (status_changed)
  847. phy_print_status(phydev);
  848. }
  849. static int r6040_mii_probe(struct net_device *dev)
  850. {
  851. struct r6040_private *lp = netdev_priv(dev);
  852. struct phy_device *phydev = NULL;
  853. phydev = phy_find_first(lp->mii_bus);
  854. if (!phydev) {
  855. dev_err(&lp->pdev->dev, "no PHY found\n");
  856. return -ENODEV;
  857. }
  858. phydev = phy_connect(dev, phydev_name(phydev), &r6040_adjust_link,
  859. PHY_INTERFACE_MODE_MII);
  860. if (IS_ERR(phydev)) {
  861. dev_err(&lp->pdev->dev, "could not attach to PHY\n");
  862. return PTR_ERR(phydev);
  863. }
  864. /* mask with MAC supported features */
  865. phydev->supported &= (SUPPORTED_10baseT_Half
  866. | SUPPORTED_10baseT_Full
  867. | SUPPORTED_100baseT_Half
  868. | SUPPORTED_100baseT_Full
  869. | SUPPORTED_Autoneg
  870. | SUPPORTED_MII
  871. | SUPPORTED_TP);
  872. phydev->advertising = phydev->supported;
  873. lp->old_link = 0;
  874. lp->old_duplex = -1;
  875. phy_attached_info(phydev);
  876. return 0;
  877. }
  878. static int r6040_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  879. {
  880. struct net_device *dev;
  881. struct r6040_private *lp;
  882. void __iomem *ioaddr;
  883. int err, io_size = R6040_IO_SIZE;
  884. static int card_idx = -1;
  885. int bar = 0;
  886. u16 *adrp;
  887. pr_info("%s\n", version);
  888. err = pci_enable_device(pdev);
  889. if (err)
  890. goto err_out;
  891. /* this should always be supported */
  892. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  893. if (err) {
  894. dev_err(&pdev->dev, "32-bit PCI DMA addresses not supported by the card\n");
  895. goto err_out_disable_dev;
  896. }
  897. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  898. if (err) {
  899. dev_err(&pdev->dev, "32-bit PCI DMA addresses not supported by the card\n");
  900. goto err_out_disable_dev;
  901. }
  902. /* IO Size check */
  903. if (pci_resource_len(pdev, bar) < io_size) {
  904. dev_err(&pdev->dev, "Insufficient PCI resources, aborting\n");
  905. err = -EIO;
  906. goto err_out_disable_dev;
  907. }
  908. pci_set_master(pdev);
  909. dev = alloc_etherdev(sizeof(struct r6040_private));
  910. if (!dev) {
  911. err = -ENOMEM;
  912. goto err_out_disable_dev;
  913. }
  914. SET_NETDEV_DEV(dev, &pdev->dev);
  915. lp = netdev_priv(dev);
  916. err = pci_request_regions(pdev, DRV_NAME);
  917. if (err) {
  918. dev_err(&pdev->dev, "Failed to request PCI regions\n");
  919. goto err_out_free_dev;
  920. }
  921. ioaddr = pci_iomap(pdev, bar, io_size);
  922. if (!ioaddr) {
  923. dev_err(&pdev->dev, "ioremap failed for device\n");
  924. err = -EIO;
  925. goto err_out_free_res;
  926. }
  927. /* If PHY status change register is still set to zero it means the
  928. * bootloader didn't initialize it, so we set it to:
  929. * - enable phy status change
  930. * - enable all phy addresses
  931. * - set to lowest timer divider */
  932. if (ioread16(ioaddr + PHY_CC) == 0)
  933. iowrite16(SCEN | PHY_MAX_ADDR << PHYAD_SHIFT |
  934. 7 << TMRDIV_SHIFT, ioaddr + PHY_CC);
  935. /* Init system & device */
  936. lp->base = ioaddr;
  937. dev->irq = pdev->irq;
  938. spin_lock_init(&lp->lock);
  939. pci_set_drvdata(pdev, dev);
  940. /* Set MAC address */
  941. card_idx++;
  942. adrp = (u16 *)dev->dev_addr;
  943. adrp[0] = ioread16(ioaddr + MID_0L);
  944. adrp[1] = ioread16(ioaddr + MID_0M);
  945. adrp[2] = ioread16(ioaddr + MID_0H);
  946. /* Some bootloader/BIOSes do not initialize
  947. * MAC address, warn about that */
  948. if (!(adrp[0] || adrp[1] || adrp[2])) {
  949. netdev_warn(dev, "MAC address not initialized, "
  950. "generating random\n");
  951. eth_hw_addr_random(dev);
  952. }
  953. /* Link new device into r6040_root_dev */
  954. lp->pdev = pdev;
  955. lp->dev = dev;
  956. /* Init RDC private data */
  957. lp->mcr0 = MCR0_XMTEN | MCR0_RCVEN;
  958. /* The RDC-specific entries in the device structure. */
  959. dev->netdev_ops = &r6040_netdev_ops;
  960. dev->ethtool_ops = &netdev_ethtool_ops;
  961. dev->watchdog_timeo = TX_TIMEOUT;
  962. netif_napi_add(dev, &lp->napi, r6040_poll, 64);
  963. lp->mii_bus = mdiobus_alloc();
  964. if (!lp->mii_bus) {
  965. dev_err(&pdev->dev, "mdiobus_alloc() failed\n");
  966. err = -ENOMEM;
  967. goto err_out_unmap;
  968. }
  969. lp->mii_bus->priv = dev;
  970. lp->mii_bus->read = r6040_mdiobus_read;
  971. lp->mii_bus->write = r6040_mdiobus_write;
  972. lp->mii_bus->name = "r6040_eth_mii";
  973. snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  974. dev_name(&pdev->dev), card_idx);
  975. err = mdiobus_register(lp->mii_bus);
  976. if (err) {
  977. dev_err(&pdev->dev, "failed to register MII bus\n");
  978. goto err_out_mdio;
  979. }
  980. err = r6040_mii_probe(dev);
  981. if (err) {
  982. dev_err(&pdev->dev, "failed to probe MII bus\n");
  983. goto err_out_mdio_unregister;
  984. }
  985. /* Register net device. After this dev->name assign */
  986. err = register_netdev(dev);
  987. if (err) {
  988. dev_err(&pdev->dev, "Failed to register net device\n");
  989. goto err_out_mdio_unregister;
  990. }
  991. return 0;
  992. err_out_mdio_unregister:
  993. mdiobus_unregister(lp->mii_bus);
  994. err_out_mdio:
  995. mdiobus_free(lp->mii_bus);
  996. err_out_unmap:
  997. netif_napi_del(&lp->napi);
  998. pci_iounmap(pdev, ioaddr);
  999. err_out_free_res:
  1000. pci_release_regions(pdev);
  1001. err_out_free_dev:
  1002. free_netdev(dev);
  1003. err_out_disable_dev:
  1004. pci_disable_device(pdev);
  1005. err_out:
  1006. return err;
  1007. }
  1008. static void r6040_remove_one(struct pci_dev *pdev)
  1009. {
  1010. struct net_device *dev = pci_get_drvdata(pdev);
  1011. struct r6040_private *lp = netdev_priv(dev);
  1012. unregister_netdev(dev);
  1013. mdiobus_unregister(lp->mii_bus);
  1014. mdiobus_free(lp->mii_bus);
  1015. netif_napi_del(&lp->napi);
  1016. pci_iounmap(pdev, lp->base);
  1017. pci_release_regions(pdev);
  1018. free_netdev(dev);
  1019. pci_disable_device(pdev);
  1020. }
  1021. static const struct pci_device_id r6040_pci_tbl[] = {
  1022. { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) },
  1023. { 0 }
  1024. };
  1025. MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
  1026. static struct pci_driver r6040_driver = {
  1027. .name = DRV_NAME,
  1028. .id_table = r6040_pci_tbl,
  1029. .probe = r6040_init_one,
  1030. .remove = r6040_remove_one,
  1031. };
  1032. module_pci_driver(r6040_driver);