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

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