You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rhine.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * Copyright (C) 2012 Adrian Jamroz <adrian.jamroz@gmail.com>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <errno.h>
  24. #include <byteswap.h>
  25. #include <ipxe/netdevice.h>
  26. #include <ipxe/ethernet.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/malloc.h>
  30. #include <ipxe/pci.h>
  31. #include <ipxe/mii.h>
  32. #include "rhine.h"
  33. /** @file
  34. *
  35. * VIA Rhine network driver
  36. *
  37. */
  38. /******************************************************************************
  39. *
  40. * MII interface
  41. *
  42. ******************************************************************************
  43. */
  44. /**
  45. * Read from MII register
  46. *
  47. * @v mdio MII interface
  48. * @v phy PHY address
  49. * @v reg Register address
  50. * @ret value Data read, or negative error
  51. */
  52. static int rhine_mii_read ( struct mii_interface *mdio,
  53. unsigned int phy __unused, unsigned int reg ) {
  54. struct rhine_nic *rhn = container_of ( mdio, struct rhine_nic, mdio );
  55. unsigned int timeout = RHINE_TIMEOUT_US;
  56. uint8_t cr;
  57. DBGC2 ( rhn, "RHINE %p MII read reg %d\n", rhn, reg );
  58. /* Initiate read */
  59. writeb ( reg, rhn->regs + RHINE_MII_ADDR );
  60. cr = readb ( rhn->regs + RHINE_MII_CR );
  61. writeb ( ( cr | RHINE_MII_CR_RDEN ), rhn->regs + RHINE_MII_CR );
  62. /* Wait for read to complete */
  63. while ( timeout-- ) {
  64. udelay ( 1 );
  65. cr = readb ( rhn->regs + RHINE_MII_CR );
  66. if ( ! ( cr & RHINE_MII_CR_RDEN ) )
  67. return readw ( rhn->regs + RHINE_MII_RDWR );
  68. }
  69. DBGC ( rhn, "RHINE %p MII read timeout\n", rhn );
  70. return -ETIMEDOUT;
  71. }
  72. /**
  73. * Write to MII register
  74. *
  75. * @v mdio MII interface
  76. * @v phy PHY address
  77. * @v reg Register address
  78. * @v data Data to write
  79. * @ret rc Return status code
  80. */
  81. static int rhine_mii_write ( struct mii_interface *mdio,
  82. unsigned int phy __unused, unsigned int reg,
  83. unsigned int data ) {
  84. struct rhine_nic *rhn = container_of ( mdio, struct rhine_nic, mdio );
  85. unsigned int timeout = RHINE_TIMEOUT_US;
  86. uint8_t cr;
  87. DBGC2 ( rhn, "RHINE %p MII write reg %d data 0x%04x\n",
  88. rhn, reg, data );
  89. /* Initiate write */
  90. writeb ( reg, rhn->regs + RHINE_MII_ADDR );
  91. writew ( data, rhn->regs + RHINE_MII_RDWR );
  92. cr = readb ( rhn->regs + RHINE_MII_CR );
  93. writeb ( ( cr | RHINE_MII_CR_WREN ), rhn->regs + RHINE_MII_CR );
  94. /* Wait for write to complete */
  95. while ( timeout-- ) {
  96. udelay ( 1 );
  97. cr = readb ( rhn->regs + RHINE_MII_CR );
  98. if ( ! ( cr & RHINE_MII_CR_WREN ) )
  99. return 0;
  100. }
  101. DBGC ( rhn, "RHINE %p MII write timeout\n", rhn );
  102. return -ETIMEDOUT;
  103. }
  104. /** Rhine MII operations */
  105. static struct mii_operations rhine_mii_operations = {
  106. .read = rhine_mii_read,
  107. .write = rhine_mii_write,
  108. };
  109. /**
  110. * Enable auto-polling
  111. *
  112. * @v rhn Rhine device
  113. * @ret rc Return status code
  114. *
  115. * This is voodoo. There seems to be no documentation on exactly what
  116. * we are waiting for, or why we have to do anything other than simply
  117. * turn the feature on.
  118. */
  119. static int rhine_mii_autopoll ( struct rhine_nic *rhn ) {
  120. unsigned int timeout = RHINE_TIMEOUT_US;
  121. uint8_t addr;
  122. /* Initiate auto-polling */
  123. writeb ( MII_BMSR, rhn->regs + RHINE_MII_ADDR );
  124. writeb ( RHINE_MII_CR_AUTOPOLL, rhn->regs + RHINE_MII_CR );
  125. /* Wait for auto-polling to complete */
  126. while ( timeout-- ) {
  127. udelay ( 1 );
  128. addr = readb ( rhn->regs + RHINE_MII_ADDR );
  129. if ( ! ( addr & RHINE_MII_ADDR_MDONE ) ) {
  130. writeb ( ( MII_BMSR | RHINE_MII_ADDR_MSRCEN ),
  131. rhn->regs + RHINE_MII_ADDR );
  132. return 0;
  133. }
  134. }
  135. DBGC ( rhn, "RHINE %p MII auto-poll timeout\n", rhn );
  136. return -ETIMEDOUT;
  137. }
  138. /******************************************************************************
  139. *
  140. * Device reset
  141. *
  142. ******************************************************************************
  143. */
  144. /**
  145. * Reset hardware
  146. *
  147. * @v rhn Rhine device
  148. * @ret rc Return status code
  149. *
  150. * We're using PIO because this might reset the MMIO enable bit.
  151. */
  152. static int rhine_reset ( struct rhine_nic *rhn ) {
  153. unsigned int timeout = RHINE_TIMEOUT_US;
  154. uint8_t cr1;
  155. DBGC ( rhn, "RHINE %p reset\n", rhn );
  156. /* Initiate reset */
  157. outb ( RHINE_CR1_RESET, rhn->ioaddr + RHINE_CR1 );
  158. /* Wait for reset to complete */
  159. while ( timeout-- ) {
  160. udelay ( 1 );
  161. cr1 = inb ( rhn->ioaddr + RHINE_CR1 );
  162. if ( ! ( cr1 & RHINE_CR1_RESET ) )
  163. return 0;
  164. }
  165. DBGC ( rhn, "RHINE %p reset timeout\n", rhn );
  166. return -ETIMEDOUT;
  167. }
  168. /**
  169. * Enable MMIO register access
  170. *
  171. * @v rhn Rhine device
  172. * @v revision Card revision
  173. */
  174. static void rhine_enable_mmio ( struct rhine_nic *rhn, int revision ) {
  175. uint8_t conf;
  176. if ( revision < RHINE_REVISION_OLD ) {
  177. conf = inb ( rhn->ioaddr + RHINE_CHIPCFG_A );
  178. outb ( ( conf | RHINE_CHIPCFG_A_MMIO ),
  179. rhn->ioaddr + RHINE_CHIPCFG_A );
  180. } else {
  181. conf = inb ( rhn->ioaddr + RHINE_CHIPCFG_D );
  182. outb ( ( conf | RHINE_CHIPCFG_D_MMIO ),
  183. rhn->ioaddr + RHINE_CHIPCFG_D );
  184. }
  185. }
  186. /**
  187. * Reload EEPROM contents
  188. *
  189. * @v rhn Rhine device
  190. * @ret rc Return status code
  191. *
  192. * We're using PIO because this might reset the MMIO enable bit.
  193. */
  194. static int rhine_reload_eeprom ( struct rhine_nic *rhn ) {
  195. unsigned int timeout = RHINE_TIMEOUT_US;
  196. uint8_t eeprom;
  197. /* Initiate reload */
  198. eeprom = inb ( rhn->ioaddr + RHINE_EEPROM_CTRL );
  199. outb ( ( eeprom | RHINE_EEPROM_CTRL_RELOAD ),
  200. rhn->ioaddr + RHINE_EEPROM_CTRL );
  201. /* Wait for reload to complete */
  202. while ( timeout-- ) {
  203. udelay ( 1 );
  204. eeprom = inb ( rhn->ioaddr + RHINE_EEPROM_CTRL );
  205. if ( ! ( eeprom & RHINE_EEPROM_CTRL_RELOAD ) )
  206. return 0;
  207. }
  208. DBGC ( rhn, "RHINE %p EEPROM reload timeout\n", rhn );
  209. return -ETIMEDOUT;
  210. }
  211. /******************************************************************************
  212. *
  213. * Link state
  214. *
  215. ******************************************************************************
  216. */
  217. /**
  218. * Check link state
  219. *
  220. * @v netdev Network device
  221. */
  222. static void rhine_check_link ( struct net_device *netdev ) {
  223. struct rhine_nic *rhn = netdev->priv;
  224. uint8_t mii_sr;
  225. /* Read MII status register */
  226. mii_sr = readb ( rhn->regs + RHINE_MII_SR );
  227. DBGC ( rhn, "RHINE %p link status %02x\n", rhn, mii_sr );
  228. /* Report link state */
  229. if ( ! ( mii_sr & RHINE_MII_SR_LINKPOLL ) ) {
  230. netdev_link_up ( netdev );
  231. } else if ( mii_sr & RHINE_MII_SR_PHYERR ) {
  232. netdev_link_err ( netdev, -EIO );
  233. } else {
  234. netdev_link_down ( netdev );
  235. }
  236. }
  237. /******************************************************************************
  238. *
  239. * Network device interface
  240. *
  241. ******************************************************************************
  242. */
  243. /**
  244. * Create descriptor ring
  245. *
  246. * @v rhn Rhine device
  247. * @v ring Descriptor ring
  248. * @ret rc Return status code
  249. */
  250. static int rhine_create_ring ( struct rhine_nic *rhn,
  251. struct rhine_ring *ring ) {
  252. size_t len = ( ring->count * sizeof ( ring->desc[0] ) );
  253. struct rhine_descriptor *next;
  254. physaddr_t address;
  255. unsigned int i;
  256. /* Allocate descriptors */
  257. ring->desc = malloc_dma ( len, RHINE_RING_ALIGN );
  258. if ( ! ring->desc )
  259. return -ENOMEM;
  260. /* Initialise descriptor ring */
  261. memset ( ring->desc, 0, len );
  262. for ( i = 0 ; i < ring->count ; i++ ) {
  263. next = &ring->desc[ ( i + 1 ) % ring->count ];
  264. ring->desc[i].next = cpu_to_le32 ( virt_to_bus ( next ) );
  265. }
  266. /* Program ring address */
  267. address = virt_to_bus ( ring->desc );
  268. writel ( address, rhn->regs + ring->reg );
  269. DBGC ( rhn, "RHINE %p ring %02x is at [%08llx,%08llx)\n",
  270. rhn, ring->reg, ( ( unsigned long long ) address ),
  271. ( ( unsigned long long ) address + len ) );
  272. return 0;
  273. }
  274. /**
  275. * Destroy descriptor ring
  276. *
  277. * @v rhn Rhine device
  278. * @v ring Descriptor ring
  279. */
  280. static void rhine_destroy_ring ( struct rhine_nic *rhn,
  281. struct rhine_ring *ring ) {
  282. size_t len = ( ring->count * sizeof ( ring->desc[0] ) );
  283. /* Clear ring address */
  284. writel ( 0, rhn->regs + ring->reg );
  285. /* Free descriptor ring */
  286. free_dma ( ring->desc, len );
  287. ring->desc = NULL;
  288. ring->prod = 0;
  289. ring->cons = 0;
  290. }
  291. /**
  292. * Refill RX descriptor ring
  293. *
  294. * @v rhn Rhine device
  295. */
  296. static void rhine_refill_rx ( struct rhine_nic *rhn ) {
  297. struct rhine_descriptor *desc;
  298. struct io_buffer *iobuf;
  299. unsigned int rx_idx;
  300. physaddr_t address;
  301. while ( ( rhn->rx.prod - rhn->rx.cons ) < RHINE_RXDESC_NUM ) {
  302. /* Allocate I/O buffer */
  303. iobuf = alloc_iob ( RHINE_RX_MAX_LEN );
  304. if ( ! iobuf ) {
  305. /* Wait for next refill */
  306. return;
  307. }
  308. /* Populate next receive descriptor */
  309. rx_idx = ( rhn->rx.prod++ % RHINE_RXDESC_NUM );
  310. desc = &rhn->rx.desc[rx_idx];
  311. address = virt_to_bus ( iobuf->data );
  312. desc->buffer = cpu_to_le32 ( address );
  313. desc->des1 =
  314. cpu_to_le32 ( RHINE_DES1_SIZE ( RHINE_RX_MAX_LEN - 1) |
  315. RHINE_DES1_CHAIN | RHINE_DES1_IC );
  316. wmb();
  317. desc->des0 = cpu_to_le32 ( RHINE_DES0_OWN );
  318. /* Record I/O buffer */
  319. rhn->rx_iobuf[rx_idx] = iobuf;
  320. DBGC2 ( rhn, "RHINE %p RX %d is [%llx,%llx)\n", rhn, rx_idx,
  321. ( ( unsigned long long ) address ),
  322. ( ( unsigned long long ) address + RHINE_RX_MAX_LEN ) );
  323. }
  324. }
  325. /**
  326. * Open network device
  327. *
  328. * @v netdev Network device
  329. * @ret rc Return status code
  330. */
  331. static int rhine_open ( struct net_device *netdev ) {
  332. struct rhine_nic *rhn = netdev->priv;
  333. int rc;
  334. /* Create transmit ring */
  335. if ( ( rc = rhine_create_ring ( rhn, &rhn->tx ) ) != 0 )
  336. goto err_create_tx;
  337. /* Create receive ring */
  338. if ( ( rc = rhine_create_ring ( rhn, &rhn->rx ) ) != 0 )
  339. goto err_create_rx;
  340. /* Set receive configuration */
  341. writeb ( ( RHINE_RCR_PHYS_ACCEPT | RHINE_RCR_BCAST_ACCEPT |
  342. RHINE_RCR_RUNT_ACCEPT ), rhn->regs + RHINE_RCR );
  343. /* Enable link status monitoring */
  344. if ( ( rc = rhine_mii_autopoll ( rhn ) ) != 0 )
  345. goto err_mii_autopoll;
  346. /* Some cards need an extra delay(observed with VT6102) */
  347. mdelay ( 10 );
  348. /* Enable RX/TX of packets */
  349. writeb ( ( RHINE_CR0_STARTNIC | RHINE_CR0_RXEN | RHINE_CR0_TXEN ),
  350. rhn->regs + RHINE_CR0 );
  351. /* Enable auto polling and full duplex operation */
  352. rhn->cr1 = RHINE_CR1_FDX;
  353. writeb ( rhn->cr1, rhn->regs + RHINE_CR1 );
  354. /* Refill RX ring */
  355. rhine_refill_rx ( rhn );
  356. /* Update link state */
  357. rhine_check_link ( netdev );
  358. return 0;
  359. err_mii_autopoll:
  360. rhine_destroy_ring ( rhn, &rhn->rx );
  361. err_create_rx:
  362. rhine_destroy_ring ( rhn, &rhn->tx );
  363. err_create_tx:
  364. return rc;
  365. }
  366. /**
  367. * Close network device
  368. *
  369. * @v netdev Network device
  370. */
  371. static void rhine_close ( struct net_device *netdev ) {
  372. struct rhine_nic *rhn = netdev->priv;
  373. unsigned int i;
  374. /* Disable interrupts */
  375. writeb ( 0, RHINE_IMR0 );
  376. writeb ( 0, RHINE_IMR1 );
  377. /* Stop card, clear RXON and TXON bits */
  378. writeb ( RHINE_CR0_STOPNIC, rhn->regs + RHINE_CR0 );
  379. /* Destroy receive ring */
  380. rhine_destroy_ring ( rhn, &rhn->rx );
  381. /* Discard any unused receive buffers */
  382. for ( i = 0 ; i < RHINE_RXDESC_NUM ; i++ ) {
  383. if ( rhn->rx_iobuf[i] )
  384. free_iob ( rhn->rx_iobuf[i] );
  385. rhn->rx_iobuf[i] = NULL;
  386. }
  387. /* Destroy transmit ring */
  388. rhine_destroy_ring ( rhn, &rhn->tx );
  389. }
  390. /**
  391. * Transmit packet
  392. *
  393. * @v netdev Network device
  394. * @v iobuf I/O buffer
  395. * @ret rc Return status code
  396. */
  397. static int rhine_transmit ( struct net_device *netdev,
  398. struct io_buffer *iobuf ) {
  399. struct rhine_nic *rhn = netdev->priv;
  400. struct rhine_descriptor *desc;
  401. physaddr_t address;
  402. unsigned int tx_idx;
  403. /* Get next transmit descriptor */
  404. if ( ( rhn->tx.prod - rhn->tx.cons ) >= RHINE_TXDESC_NUM )
  405. return -ENOBUFS;
  406. tx_idx = ( rhn->tx.prod++ % RHINE_TXDESC_NUM );
  407. desc = &rhn->tx.desc[tx_idx];
  408. /* Pad and align packet */
  409. iob_pad ( iobuf, ETH_ZLEN );
  410. address = virt_to_bus ( iobuf->data );
  411. /* Populate transmit descriptor */
  412. desc->buffer = cpu_to_le32 ( address );
  413. desc->des1 = cpu_to_le32 ( RHINE_DES1_IC | RHINE_TDES1_STP |
  414. RHINE_TDES1_EDP | RHINE_DES1_CHAIN |
  415. RHINE_DES1_SIZE ( iob_len ( iobuf ) ) );
  416. wmb();
  417. desc->des0 = cpu_to_le32 ( RHINE_DES0_OWN );
  418. wmb();
  419. /* Notify card that there are packets ready to transmit */
  420. writeb ( ( rhn->cr1 | RHINE_CR1_TXPOLL ), rhn->regs + RHINE_CR1 );
  421. DBGC2 ( rhn, "RHINE %p TX %d is [%llx,%llx)\n", rhn, tx_idx,
  422. ( ( unsigned long long ) address ),
  423. ( ( unsigned long long ) address + iob_len ( iobuf ) ) );
  424. return 0;
  425. }
  426. /**
  427. * Poll for completed packets
  428. *
  429. * @v netdev Network device
  430. */
  431. static void rhine_poll_tx ( struct net_device *netdev ) {
  432. struct rhine_nic *rhn = netdev->priv;
  433. struct rhine_descriptor *desc;
  434. unsigned int tx_idx;
  435. uint32_t des0;
  436. /* Check for completed packets */
  437. while ( rhn->tx.cons != rhn->tx.prod ) {
  438. /* Get next transmit descriptor */
  439. tx_idx = ( rhn->tx.cons % RHINE_TXDESC_NUM );
  440. desc = &rhn->tx.desc[tx_idx];
  441. /* Stop if descriptor is still in use */
  442. if ( desc->des0 & cpu_to_le32 ( RHINE_DES0_OWN ) )
  443. return;
  444. /* Complete TX descriptor */
  445. des0 = le32_to_cpu ( desc->des0 );
  446. if ( des0 & RHINE_TDES0_TERR ) {
  447. DBGC ( rhn, "RHINE %p TX %d error (DES0 %08x)\n",
  448. rhn, tx_idx, des0 );
  449. netdev_tx_complete_next_err ( netdev, -EIO );
  450. } else {
  451. DBGC2 ( rhn, "RHINE %p TX %d complete\n", rhn, tx_idx );
  452. netdev_tx_complete_next ( netdev );
  453. }
  454. rhn->tx.cons++;
  455. }
  456. }
  457. /**
  458. * Poll for received packets
  459. *
  460. * @v netdev Network device
  461. */
  462. static void rhine_poll_rx ( struct net_device *netdev ) {
  463. struct rhine_nic *rhn = netdev->priv;
  464. struct rhine_descriptor *desc;
  465. struct io_buffer *iobuf;
  466. unsigned int rx_idx;
  467. uint32_t des0;
  468. size_t len;
  469. /* Check for received packets */
  470. while ( rhn->rx.cons != rhn->rx.prod ) {
  471. /* Get next receive descriptor */
  472. rx_idx = ( rhn->rx.cons % RHINE_RXDESC_NUM );
  473. desc = &rhn->rx.desc[rx_idx];
  474. /* Stop if descriptor is still in use */
  475. if ( desc->des0 & cpu_to_le32 ( RHINE_DES0_OWN ) )
  476. return;
  477. /* Populate I/O buffer */
  478. iobuf = rhn->rx_iobuf[rx_idx];
  479. rhn->rx_iobuf[rx_idx] = NULL;
  480. des0 = le32_to_cpu ( desc->des0 );
  481. len = ( RHINE_DES0_GETSIZE ( des0 ) - 4 /* strip CRC */ );
  482. iob_put ( iobuf, len );
  483. /* Hand off to network stack */
  484. if ( des0 & RHINE_RDES0_RXOK ) {
  485. DBGC2 ( rhn, "RHINE %p RX %d complete (length %zd)\n",
  486. rhn, rx_idx, len );
  487. netdev_rx ( netdev, iobuf );
  488. } else {
  489. DBGC ( rhn, "RHINE %p RX %d error (length %zd, DES0 "
  490. "%08x)\n", rhn, rx_idx, len, des0 );
  491. netdev_rx_err ( netdev, iobuf, -EIO );
  492. }
  493. rhn->rx.cons++;
  494. }
  495. }
  496. /**
  497. * Poll for completed and received packets
  498. *
  499. * @v netdev Network device
  500. */
  501. static void rhine_poll ( struct net_device *netdev ) {
  502. struct rhine_nic *rhn = netdev->priv;
  503. uint8_t isr0;
  504. uint8_t isr1;
  505. /* Read and acknowledge interrupts */
  506. isr0 = readb ( rhn->regs + RHINE_ISR0 );
  507. isr1 = readb ( rhn->regs + RHINE_ISR1 );
  508. if ( isr0 )
  509. writeb ( isr0, rhn->regs + RHINE_ISR0 );
  510. if ( isr1 )
  511. writeb ( isr1, rhn->regs + RHINE_ISR1 );
  512. /* Report unexpected errors */
  513. if ( ( isr0 & ( RHINE_ISR0_MIBOVFL | RHINE_ISR0_PCIERR |
  514. RHINE_ISR0_RXRINGERR | RHINE_ISR0_TXRINGERR ) ) ||
  515. ( isr1 & ( RHINE_ISR1_GPI | RHINE_ISR1_TXABORT |
  516. RHINE_ISR1_RXFIFOOVFL | RHINE_ISR1_RXFIFOUNFL |
  517. RHINE_ISR1_TXFIFOUNFL ) ) ) {
  518. DBGC ( rhn, "RHINE %p unexpected ISR0 %02x ISR1 %02x\n",
  519. rhn, isr0, isr1 );
  520. /* Report as a TX error */
  521. netdev_tx_err ( netdev, NULL, -EIO );
  522. }
  523. /* Poll for TX completions, if applicable */
  524. if ( isr0 & ( RHINE_ISR0_TXDONE | RHINE_ISR0_TXERR ) )
  525. rhine_poll_tx ( netdev );
  526. /* Poll for RX completions, if applicable */
  527. if ( isr0 & ( RHINE_ISR0_RXDONE | RHINE_ISR0_RXERR ) )
  528. rhine_poll_rx ( netdev );
  529. /* Handle RX buffer exhaustion */
  530. if ( isr1 & RHINE_ISR1_RXNOBUF ) {
  531. rhine_poll_rx ( netdev );
  532. netdev_rx_err ( netdev, NULL, -ENOBUFS );
  533. }
  534. /* Check link state, if applicable */
  535. if ( isr1 & RHINE_ISR1_PORTSTATE )
  536. rhine_check_link ( netdev );
  537. /* Refill RX ring */
  538. rhine_refill_rx ( rhn );
  539. }
  540. /**
  541. * Enable or disable interrupts
  542. *
  543. * @v netdev Network device
  544. * @v enable Interrupts should be enabled
  545. */
  546. static void rhine_irq ( struct net_device *netdev, int enable ) {
  547. struct rhine_nic *nic = netdev->priv;
  548. if ( enable ) {
  549. /* Enable interrupts */
  550. writeb ( 0xff, nic->regs + RHINE_IMR0 );
  551. writeb ( 0xff, nic->regs + RHINE_IMR1 );
  552. } else {
  553. /* Disable interrupts */
  554. writeb ( 0, nic->regs + RHINE_IMR0 );
  555. writeb ( 0, nic->regs + RHINE_IMR1 );
  556. }
  557. }
  558. /** Rhine network device operations */
  559. static struct net_device_operations rhine_operations = {
  560. .open = rhine_open,
  561. .close = rhine_close,
  562. .transmit = rhine_transmit,
  563. .poll = rhine_poll,
  564. .irq = rhine_irq,
  565. };
  566. /******************************************************************************
  567. *
  568. * PCI interface
  569. *
  570. ******************************************************************************
  571. */
  572. /**
  573. * Probe PCI device
  574. *
  575. * @v pci PCI device
  576. * @ret rc Return status code
  577. */
  578. static int rhine_probe ( struct pci_device *pci ) {
  579. struct net_device *netdev;
  580. struct rhine_nic *rhn;
  581. uint8_t revision;
  582. unsigned int i;
  583. int rc;
  584. /* Allocate and initialise net device */
  585. netdev = alloc_etherdev ( sizeof ( *rhn ) );
  586. if ( ! netdev ) {
  587. rc = -ENOMEM;
  588. goto err_alloc;
  589. }
  590. netdev_init ( netdev, &rhine_operations );
  591. rhn = netdev->priv;
  592. pci_set_drvdata ( pci, netdev );
  593. netdev->dev = &pci->dev;
  594. memset ( rhn, 0, sizeof ( *rhn ) );
  595. rhine_init_ring ( &rhn->tx, RHINE_TXDESC_NUM, RHINE_TXQUEUE_BASE );
  596. rhine_init_ring ( &rhn->rx, RHINE_RXDESC_NUM, RHINE_RXQUEUE_BASE );
  597. /* Fix up PCI device */
  598. adjust_pci_device ( pci );
  599. /* Map registers */
  600. rhn->regs = ioremap ( pci->membase, RHINE_BAR_SIZE );
  601. rhn->ioaddr = pci->ioaddr;
  602. DBGC ( rhn, "RHINE %p regs at %08lx, I/O at %04lx\n", rhn,
  603. pci->membase, pci->ioaddr );
  604. /* Reset the NIC */
  605. if ( ( rc = rhine_reset ( rhn ) ) != 0 )
  606. goto err_reset;
  607. /* Reload EEPROM */
  608. if ( ( rc = rhine_reload_eeprom ( rhn ) ) != 0 )
  609. goto err_reload_eeprom;
  610. /* Read card revision and enable MMIO */
  611. pci_read_config_byte ( pci, PCI_REVISION, &revision );
  612. DBGC ( rhn, "RHINE %p revision %#02x detected\n", rhn, revision );
  613. rhine_enable_mmio ( rhn, revision );
  614. /* Read MAC address */
  615. for ( i = 0 ; i < ETH_ALEN ; i++ )
  616. netdev->hw_addr[i] = readb ( rhn->regs + RHINE_MAC + i );
  617. /* Initialise and reset MII interface */
  618. mdio_init ( &rhn->mdio, &rhine_mii_operations );
  619. mii_init ( &rhn->mii, &rhn->mdio, 0 );
  620. if ( ( rc = mii_reset ( &rhn->mii ) ) != 0 ) {
  621. DBGC ( rhn, "RHINE %p could not reset MII: %s\n",
  622. rhn, strerror ( rc ) );
  623. goto err_mii_reset;
  624. }
  625. DBGC ( rhn, "RHINE PHY vendor %04x device %04x\n",
  626. mii_read ( &rhn->mii, 0x02 ), mii_read ( &rhn->mii, 0x03 ) );
  627. /* Register network device */
  628. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  629. goto err_register_netdev;
  630. /* Set initial link state */
  631. rhine_check_link ( netdev );
  632. return 0;
  633. err_register_netdev:
  634. err_mii_reset:
  635. err_reload_eeprom:
  636. rhine_reset ( rhn );
  637. err_reset:
  638. netdev_nullify ( netdev );
  639. netdev_put ( netdev );
  640. err_alloc:
  641. return rc;
  642. }
  643. /**
  644. * Remove PCI device
  645. *
  646. * @v pci PCI device
  647. */
  648. static void rhine_remove ( struct pci_device *pci ) {
  649. struct net_device *netdev = pci_get_drvdata ( pci );
  650. struct rhine_nic *nic = netdev->priv;
  651. /* Unregister network device */
  652. unregister_netdev ( netdev );
  653. /* Reset card */
  654. rhine_reset ( nic );
  655. /* Free network device */
  656. netdev_nullify ( netdev );
  657. netdev_put ( netdev );
  658. }
  659. /** Rhine PCI device IDs */
  660. static struct pci_device_id rhine_nics[] = {
  661. PCI_ROM ( 0x1106, 0x3065, "dlink-530tx", "VIA VT6102", 0 ),
  662. PCI_ROM ( 0x1106, 0x3106, "vt6105", "VIA VT6105", 0 ),
  663. PCI_ROM ( 0x1106, 0x3043, "dlink-530tx-old", "VIA VT3043", 0 ),
  664. PCI_ROM ( 0x1106, 0x3053, "vt6105m", "VIA VT6105M", 0 ),
  665. PCI_ROM ( 0x1106, 0x6100, "via-rhine-old", "VIA 86C100A", 0 )
  666. };
  667. /** Rhine PCI driver */
  668. struct pci_driver rhine_driver __pci_driver = {
  669. .ids = rhine_nics,
  670. .id_count = ( sizeof ( rhine_nics ) / sizeof ( rhine_nics[0] ) ),
  671. .probe = rhine_probe,
  672. .remove = rhine_remove,
  673. };