r6040.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  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. phy_stop(dev->phydev);
  411. }
  412. static int r6040_close(struct net_device *dev)
  413. {
  414. struct r6040_private *lp = netdev_priv(dev);
  415. struct pci_dev *pdev = lp->pdev;
  416. spin_lock_irq(&lp->lock);
  417. napi_disable(&lp->napi);
  418. netif_stop_queue(dev);
  419. r6040_down(dev);
  420. free_irq(dev->irq, dev);
  421. /* Free RX buffer */
  422. r6040_free_rxbufs(dev);
  423. /* Free TX buffer */
  424. r6040_free_txbufs(dev);
  425. spin_unlock_irq(&lp->lock);
  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_change_mtu = eth_change_mtu,
  821. .ndo_validate_addr = eth_validate_addr,
  822. .ndo_set_mac_address = eth_mac_addr,
  823. .ndo_do_ioctl = r6040_ioctl,
  824. .ndo_tx_timeout = r6040_tx_timeout,
  825. #ifdef CONFIG_NET_POLL_CONTROLLER
  826. .ndo_poll_controller = r6040_poll_controller,
  827. #endif
  828. };
  829. static void r6040_adjust_link(struct net_device *dev)
  830. {
  831. struct r6040_private *lp = netdev_priv(dev);
  832. struct phy_device *phydev = dev->phydev;
  833. int status_changed = 0;
  834. void __iomem *ioaddr = lp->base;
  835. BUG_ON(!phydev);
  836. if (lp->old_link != phydev->link) {
  837. status_changed = 1;
  838. lp->old_link = phydev->link;
  839. }
  840. /* reflect duplex change */
  841. if (phydev->link && (lp->old_duplex != phydev->duplex)) {
  842. lp->mcr0 |= (phydev->duplex == DUPLEX_FULL ? MCR0_FD : 0);
  843. iowrite16(lp->mcr0, ioaddr);
  844. status_changed = 1;
  845. lp->old_duplex = phydev->duplex;
  846. }
  847. if (status_changed)
  848. phy_print_status(phydev);
  849. }
  850. static int r6040_mii_probe(struct net_device *dev)
  851. {
  852. struct r6040_private *lp = netdev_priv(dev);
  853. struct phy_device *phydev = NULL;
  854. phydev = phy_find_first(lp->mii_bus);
  855. if (!phydev) {
  856. dev_err(&lp->pdev->dev, "no PHY found\n");
  857. return -ENODEV;
  858. }
  859. phydev = phy_connect(dev, phydev_name(phydev), &r6040_adjust_link,
  860. PHY_INTERFACE_MODE_MII);
  861. if (IS_ERR(phydev)) {
  862. dev_err(&lp->pdev->dev, "could not attach to PHY\n");
  863. return PTR_ERR(phydev);
  864. }
  865. /* mask with MAC supported features */
  866. phydev->supported &= (SUPPORTED_10baseT_Half
  867. | SUPPORTED_10baseT_Full
  868. | SUPPORTED_100baseT_Half
  869. | SUPPORTED_100baseT_Full
  870. | SUPPORTED_Autoneg
  871. | SUPPORTED_MII
  872. | SUPPORTED_TP);
  873. phydev->advertising = phydev->supported;
  874. lp->old_link = 0;
  875. lp->old_duplex = -1;
  876. phy_attached_info(phydev);
  877. return 0;
  878. }
  879. static int r6040_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  880. {
  881. struct net_device *dev;
  882. struct r6040_private *lp;
  883. void __iomem *ioaddr;
  884. int err, io_size = R6040_IO_SIZE;
  885. static int card_idx = -1;
  886. int bar = 0;
  887. u16 *adrp;
  888. pr_info("%s\n", version);
  889. err = pci_enable_device(pdev);
  890. if (err)
  891. goto err_out;
  892. /* this should always be supported */
  893. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  894. if (err) {
  895. dev_err(&pdev->dev, "32-bit PCI DMA addresses not supported by the card\n");
  896. goto err_out_disable_dev;
  897. }
  898. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  899. if (err) {
  900. dev_err(&pdev->dev, "32-bit PCI DMA addresses not supported by the card\n");
  901. goto err_out_disable_dev;
  902. }
  903. /* IO Size check */
  904. if (pci_resource_len(pdev, bar) < io_size) {
  905. dev_err(&pdev->dev, "Insufficient PCI resources, aborting\n");
  906. err = -EIO;
  907. goto err_out_disable_dev;
  908. }
  909. pci_set_master(pdev);
  910. dev = alloc_etherdev(sizeof(struct r6040_private));
  911. if (!dev) {
  912. err = -ENOMEM;
  913. goto err_out_disable_dev;
  914. }
  915. SET_NETDEV_DEV(dev, &pdev->dev);
  916. lp = netdev_priv(dev);
  917. err = pci_request_regions(pdev, DRV_NAME);
  918. if (err) {
  919. dev_err(&pdev->dev, "Failed to request PCI regions\n");
  920. goto err_out_free_dev;
  921. }
  922. ioaddr = pci_iomap(pdev, bar, io_size);
  923. if (!ioaddr) {
  924. dev_err(&pdev->dev, "ioremap failed for device\n");
  925. err = -EIO;
  926. goto err_out_free_res;
  927. }
  928. /* If PHY status change register is still set to zero it means the
  929. * bootloader didn't initialize it, so we set it to:
  930. * - enable phy status change
  931. * - enable all phy addresses
  932. * - set to lowest timer divider */
  933. if (ioread16(ioaddr + PHY_CC) == 0)
  934. iowrite16(SCEN | PHY_MAX_ADDR << PHYAD_SHIFT |
  935. 7 << TMRDIV_SHIFT, ioaddr + PHY_CC);
  936. /* Init system & device */
  937. lp->base = ioaddr;
  938. dev->irq = pdev->irq;
  939. spin_lock_init(&lp->lock);
  940. pci_set_drvdata(pdev, dev);
  941. /* Set MAC address */
  942. card_idx++;
  943. adrp = (u16 *)dev->dev_addr;
  944. adrp[0] = ioread16(ioaddr + MID_0L);
  945. adrp[1] = ioread16(ioaddr + MID_0M);
  946. adrp[2] = ioread16(ioaddr + MID_0H);
  947. /* Some bootloader/BIOSes do not initialize
  948. * MAC address, warn about that */
  949. if (!(adrp[0] || adrp[1] || adrp[2])) {
  950. netdev_warn(dev, "MAC address not initialized, "
  951. "generating random\n");
  952. eth_hw_addr_random(dev);
  953. }
  954. /* Link new device into r6040_root_dev */
  955. lp->pdev = pdev;
  956. lp->dev = dev;
  957. /* Init RDC private data */
  958. lp->mcr0 = MCR0_XMTEN | MCR0_RCVEN;
  959. /* The RDC-specific entries in the device structure. */
  960. dev->netdev_ops = &r6040_netdev_ops;
  961. dev->ethtool_ops = &netdev_ethtool_ops;
  962. dev->watchdog_timeo = TX_TIMEOUT;
  963. netif_napi_add(dev, &lp->napi, r6040_poll, 64);
  964. lp->mii_bus = mdiobus_alloc();
  965. if (!lp->mii_bus) {
  966. dev_err(&pdev->dev, "mdiobus_alloc() failed\n");
  967. err = -ENOMEM;
  968. goto err_out_unmap;
  969. }
  970. lp->mii_bus->priv = dev;
  971. lp->mii_bus->read = r6040_mdiobus_read;
  972. lp->mii_bus->write = r6040_mdiobus_write;
  973. lp->mii_bus->name = "r6040_eth_mii";
  974. snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
  975. dev_name(&pdev->dev), card_idx);
  976. err = mdiobus_register(lp->mii_bus);
  977. if (err) {
  978. dev_err(&pdev->dev, "failed to register MII bus\n");
  979. goto err_out_mdio;
  980. }
  981. err = r6040_mii_probe(dev);
  982. if (err) {
  983. dev_err(&pdev->dev, "failed to probe MII bus\n");
  984. goto err_out_mdio_unregister;
  985. }
  986. /* Register net device. After this dev->name assign */
  987. err = register_netdev(dev);
  988. if (err) {
  989. dev_err(&pdev->dev, "Failed to register net device\n");
  990. goto err_out_mdio_unregister;
  991. }
  992. return 0;
  993. err_out_mdio_unregister:
  994. mdiobus_unregister(lp->mii_bus);
  995. err_out_mdio:
  996. mdiobus_free(lp->mii_bus);
  997. err_out_unmap:
  998. netif_napi_del(&lp->napi);
  999. pci_iounmap(pdev, ioaddr);
  1000. err_out_free_res:
  1001. pci_release_regions(pdev);
  1002. err_out_free_dev:
  1003. free_netdev(dev);
  1004. err_out_disable_dev:
  1005. pci_disable_device(pdev);
  1006. err_out:
  1007. return err;
  1008. }
  1009. static void r6040_remove_one(struct pci_dev *pdev)
  1010. {
  1011. struct net_device *dev = pci_get_drvdata(pdev);
  1012. struct r6040_private *lp = netdev_priv(dev);
  1013. unregister_netdev(dev);
  1014. mdiobus_unregister(lp->mii_bus);
  1015. mdiobus_free(lp->mii_bus);
  1016. netif_napi_del(&lp->napi);
  1017. pci_iounmap(pdev, lp->base);
  1018. pci_release_regions(pdev);
  1019. free_netdev(dev);
  1020. pci_disable_device(pdev);
  1021. }
  1022. static const struct pci_device_id r6040_pci_tbl[] = {
  1023. { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) },
  1024. { 0 }
  1025. };
  1026. MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
  1027. static struct pci_driver r6040_driver = {
  1028. .name = DRV_NAME,
  1029. .id_table = r6040_pci_tbl,
  1030. .probe = r6040_init_one,
  1031. .remove = r6040_remove_one,
  1032. };
  1033. module_pci_driver(r6040_driver);