r6040.c 33 KB

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