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.

velocity.c 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * Copyright (C) 2012 Adrian Jamróz <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 "velocity.h"
  33. #define velocity_setbit(_reg, _mask) writeb ( readb ( _reg ) | _mask, _reg )
  34. #define virt_to_le32bus(x) ( cpu_to_le32 ( virt_to_bus ( x ) ) )
  35. /** @file
  36. *
  37. * VIA Velocity network driver
  38. *
  39. */
  40. /******************************************************************************
  41. *
  42. * MII interface
  43. *
  44. ******************************************************************************
  45. */
  46. /**
  47. * Stop MII auto-polling
  48. *
  49. * @v vlc Velocity device
  50. * @ret rc Return status code
  51. */
  52. static int velocity_autopoll_stop ( struct velocity_nic *vlc ) {
  53. int timeout = VELOCITY_TIMEOUT_US;
  54. /* Disable MII auto polling */
  55. writeb ( 0, vlc->regs + VELOCITY_MIICR );
  56. /* Wait for disabling to take effect */
  57. while ( timeout-- ) {
  58. udelay ( 1 );
  59. if ( readb ( vlc->regs + VELOCITY_MIISR ) &
  60. VELOCITY_MIISR_IDLE )
  61. return 0;
  62. }
  63. DBGC ( vlc, "MII autopoll stop timeout\n" );
  64. return -ETIMEDOUT;
  65. }
  66. /**
  67. * Start MII auto-polling
  68. *
  69. * @v vlc Velocity device
  70. * @ret rc Return status code
  71. */
  72. static int velocity_autopoll_start ( struct velocity_nic *vlc ) {
  73. int timeout = VELOCITY_TIMEOUT_US;
  74. /* Enable MII auto polling */
  75. writeb ( VELOCITY_MIICR_MAUTO, vlc->regs + VELOCITY_MIICR );
  76. /* Wait for enabling to take effect */
  77. while ( timeout-- ) {
  78. udelay ( 1 );
  79. if ( ( readb ( vlc->regs + VELOCITY_MIISR ) &
  80. VELOCITY_MIISR_IDLE ) == 0 )
  81. return 0;
  82. }
  83. DBGC ( vlc, "MII autopoll start timeout\n" );
  84. return -ETIMEDOUT;
  85. }
  86. /**
  87. * Read from MII register
  88. *
  89. * @v mdio MII interface
  90. * @v phy PHY address
  91. * @v reg Register address
  92. * @ret value Data read, or negative error
  93. */
  94. static int velocity_mii_read ( struct mii_interface *mdio,
  95. unsigned int phy __unused, unsigned int reg ) {
  96. struct velocity_nic *vlc =
  97. container_of ( mdio, struct velocity_nic, mdio );
  98. int timeout = VELOCITY_TIMEOUT_US;
  99. int result;
  100. DBGC2 ( vlc, "VELOCITY %p MII read reg %d\n", vlc, reg );
  101. /* Disable autopolling before we can access MII */
  102. velocity_autopoll_stop ( vlc );
  103. /* Send read command and address */
  104. writeb ( reg, vlc->regs + VELOCITY_MIIADDR );
  105. velocity_setbit ( vlc->regs + VELOCITY_MIICR, VELOCITY_MIICR_RCMD );
  106. /* Wait for read to complete */
  107. while ( timeout-- ) {
  108. udelay ( 1 );
  109. if ( ( readb ( vlc->regs + VELOCITY_MIICR ) &
  110. VELOCITY_MIICR_RCMD ) == 0 ) {
  111. result = readw ( vlc->regs + VELOCITY_MIIDATA );
  112. velocity_autopoll_start ( vlc );
  113. return result;
  114. }
  115. }
  116. /* Restart autopolling */
  117. velocity_autopoll_start ( vlc );
  118. DBGC ( vlc, "MII read timeout\n" );
  119. return -ETIMEDOUT;
  120. }
  121. /**
  122. * Write to MII register
  123. *
  124. * @v mdio MII interface
  125. * @v phy PHY address
  126. * @v reg Register address
  127. * @v data Data to write
  128. * @ret rc Return status code
  129. */
  130. static int velocity_mii_write ( struct mii_interface *mdio,
  131. unsigned int phy __unused, unsigned int reg,
  132. unsigned int data) {
  133. struct velocity_nic *vlc =
  134. container_of ( mdio, struct velocity_nic, mdio );
  135. int timeout = VELOCITY_TIMEOUT_US;
  136. DBGC2 ( vlc, "VELOCITY %p MII write reg %d data 0x%04x\n",
  137. vlc, reg, data );
  138. /* Disable autopolling before we can access MII */
  139. velocity_autopoll_stop ( vlc );
  140. /* Send write command, data and destination register */
  141. writeb ( reg, vlc->regs + VELOCITY_MIIADDR );
  142. writew ( data, vlc->regs + VELOCITY_MIIDATA );
  143. velocity_setbit ( vlc->regs + VELOCITY_MIICR, VELOCITY_MIICR_WCMD );
  144. /* Wait for write to complete */
  145. while ( timeout-- ) {
  146. udelay ( 1 );
  147. if ( ( readb ( vlc->regs + VELOCITY_MIICR ) &
  148. VELOCITY_MIICR_WCMD ) == 0 ) {
  149. velocity_autopoll_start ( vlc );
  150. return 0;
  151. }
  152. }
  153. /* Restart autopolling */
  154. velocity_autopoll_start ( vlc );
  155. DBGC ( vlc, "MII write timeout\n" );
  156. return -ETIMEDOUT;
  157. }
  158. /** Velocity MII operations */
  159. static struct mii_operations velocity_mii_operations = {
  160. .read = velocity_mii_read,
  161. .write = velocity_mii_write,
  162. };
  163. /**
  164. * Set Link speed
  165. *
  166. * @v vlc Velocity device
  167. */
  168. static void velocity_set_link ( struct velocity_nic *vlc ) {
  169. int tmp;
  170. /* Advertise 1000MBit */
  171. tmp = mii_read ( &vlc->mii, MII_CTRL1000 );
  172. tmp |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  173. mii_write ( &vlc->mii, MII_CTRL1000, tmp );
  174. /* Enable GBit operation in MII Control Register */
  175. tmp = mii_read ( &vlc->mii, MII_BMCR );
  176. tmp |= BMCR_SPEED1000;
  177. mii_write ( &vlc->mii, MII_BMCR, tmp );
  178. }
  179. /******************************************************************************
  180. *
  181. * Device reset
  182. *
  183. ******************************************************************************
  184. */
  185. /**
  186. * Reload eeprom contents
  187. *
  188. * @v vlc Velocity device
  189. */
  190. static int velocity_reload_eeprom ( struct velocity_nic *vlc ) {
  191. int timeout = VELOCITY_TIMEOUT_US;
  192. /* Initiate reload */
  193. velocity_setbit ( vlc->regs + VELOCITY_EECSR, VELOCITY_EECSR_RELOAD );
  194. /* Wait for reload to complete */
  195. while ( timeout-- ) {
  196. udelay ( 1 );
  197. if ( ( readb ( vlc->regs + VELOCITY_EECSR ) &
  198. VELOCITY_EECSR_RELOAD ) == 0 )
  199. return 0;
  200. }
  201. DBGC ( vlc, "VELOCITY %p EEPROM reload timeout\n", vlc );
  202. return -ETIMEDOUT;
  203. }
  204. /**
  205. * Reset hardware
  206. *
  207. * @v vlc Velocity device
  208. * @ret rc Return status code
  209. */
  210. static int velocity_reset ( struct velocity_nic *vlc ) {
  211. int timeout = VELOCITY_TIMEOUT_US;
  212. uint8_t tmp;
  213. DBGC ( vlc, "VELOCITY %p reset\n", vlc );
  214. /* clear sticky Power state bits */
  215. tmp = readb ( vlc->regs + VELOCITY_STICKY );
  216. tmp &= ~( VELOCITY_STICKY_DS0 | VELOCITY_STICKY_DS1 );
  217. writeb ( tmp, vlc->regs + VELOCITY_STICKY );
  218. /* clear PACPI, which might have been enabled by the EEPROM reload */
  219. tmp = readb ( vlc->regs + VELOCITY_CFGA );
  220. tmp &= ~VELOCITY_CFGA_PACPI;
  221. writeb ( tmp, vlc->regs + VELOCITY_CFGA );
  222. velocity_setbit ( vlc->regs + VELOCITY_CRS1, VELOCITY_CR1_SFRST );
  223. /* Wait for reset to complete */
  224. while ( timeout-- ) {
  225. udelay ( 1 );
  226. if ( ( readb ( vlc->regs + VELOCITY_CRS1 ) &
  227. VELOCITY_CR1_SFRST ) == 0 )
  228. return 0;
  229. }
  230. return -EINVAL;
  231. }
  232. /******************************************************************************
  233. *
  234. * Link state
  235. *
  236. ******************************************************************************
  237. */
  238. /**
  239. * Check link state
  240. *
  241. * @v netdev Network device
  242. */
  243. static void velocity_check_link ( struct net_device *netdev ) {
  244. struct velocity_nic *vlc = netdev->priv;
  245. if ( readb ( vlc->regs + VELOCITY_PHYSTS0 ) & VELOCITY_PHYSTS0_LINK ) {
  246. netdev_link_up ( netdev );
  247. DBGC ( vlc, "VELOCITY %p link up\n", vlc );
  248. } else {
  249. netdev_link_down ( netdev );
  250. DBGC ( vlc, "VELOCITY %p link down\n", vlc );
  251. }
  252. /* The card disables auto-poll after a link change */
  253. velocity_autopoll_start ( vlc );
  254. }
  255. /******************************************************************************
  256. *
  257. * Network device interface
  258. *
  259. ******************************************************************************
  260. */
  261. /**
  262. * Allocate descriptor rings
  263. *
  264. * @v vlc Velocity device
  265. * @ret rc Return status code
  266. */
  267. static int velocity_alloc_rings ( struct velocity_nic *vlc ) {
  268. int rc = 0;
  269. /* Allocate RX descriptor ring */
  270. vlc->rx_prod = 0;
  271. vlc->rx_cons = 0;
  272. vlc->rx_commit = 0;
  273. vlc->rx_ring = malloc_dma ( VELOCITY_RXDESC_SIZE, VELOCITY_RING_ALIGN );
  274. if ( ! vlc->rx_ring )
  275. return -ENOMEM;
  276. memset ( vlc->rx_ring, 0, VELOCITY_RXDESC_SIZE );
  277. DBGC2 ( vlc, "VELOCITY %p RX ring start address: %p(phys: %#08lx)\n",
  278. vlc, vlc->rx_ring, virt_to_bus ( vlc->rx_ring ) );
  279. /* Allocate TX descriptor ring */
  280. vlc->tx_prod = 0;
  281. vlc->tx_cons = 0;
  282. vlc->tx_ring = malloc_dma ( VELOCITY_TXDESC_SIZE, VELOCITY_RING_ALIGN );
  283. if ( ! vlc->tx_ring ) {
  284. rc = -ENOMEM;
  285. goto err_tx_alloc;
  286. }
  287. memset ( vlc->tx_ring, 0, VELOCITY_TXDESC_SIZE );
  288. /* Send RX ring to the card */
  289. writel ( virt_to_bus ( vlc->rx_ring ),
  290. vlc->regs + VELOCITY_RXDESC_ADDR_LO );
  291. writew ( VELOCITY_RXDESC_NUM - 1, vlc->regs + VELOCITY_RXDESCNUM );
  292. /* Send TX ring to the card */
  293. writel ( virt_to_bus ( vlc->tx_ring ),
  294. vlc->regs + VELOCITY_TXDESC_ADDR_LO0 );
  295. writew ( VELOCITY_TXDESC_NUM - 1, vlc->regs + VELOCITY_TXDESCNUM );
  296. DBGC2 ( vlc, "VELOCITY %p TX ring start address: %p(phys: %#08lx)\n",
  297. vlc, vlc->tx_ring, virt_to_bus ( vlc->tx_ring ) );
  298. return 0;
  299. err_tx_alloc:
  300. free_dma ( vlc->rx_ring, VELOCITY_RXDESC_SIZE );
  301. return rc;
  302. }
  303. /**
  304. * Refill receive descriptor ring
  305. *
  306. * @v vlc Velocity device
  307. */
  308. static void velocity_refill_rx ( struct velocity_nic *vlc ) {
  309. struct velocity_rx_descriptor *desc;
  310. struct io_buffer *iobuf;
  311. int rx_idx, i = 0;
  312. /* Check for new packets */
  313. while ( ( vlc->rx_prod - vlc->rx_cons ) < VELOCITY_RXDESC_NUM ) {
  314. iobuf = alloc_iob ( VELOCITY_RX_MAX_LEN );
  315. /* Memory pressure: try again next poll */
  316. if ( ! iobuf )
  317. break;
  318. rx_idx = ( vlc->rx_prod++ % VELOCITY_RXDESC_NUM );
  319. desc = &vlc->rx_ring[rx_idx];
  320. /* Set descrptor fields */
  321. desc->des1 = 0;
  322. desc->addr = virt_to_le32bus ( iobuf-> data );
  323. desc->des2 = cpu_to_le32 (
  324. VELOCITY_DES2_SIZE ( VELOCITY_RX_MAX_LEN - 1 ) |
  325. VELOCITY_DES2_IC );
  326. vlc->rx_buffs[rx_idx] = iobuf;
  327. i++;
  328. /* Return RX descriptors in blocks of 4 (hw requirement) */
  329. if ( rx_idx % 4 == 3 ) {
  330. int j;
  331. for (j = 0; j < 4; j++) {
  332. desc = &vlc->rx_ring[rx_idx - j];
  333. desc->des0 = cpu_to_le32 ( VELOCITY_DES0_OWN );
  334. }
  335. vlc->rx_commit += 4;
  336. }
  337. }
  338. wmb();
  339. if ( vlc->rx_commit ) {
  340. writew ( vlc->rx_commit,
  341. vlc->regs + VELOCITY_RXDESC_RESIDUECNT );
  342. vlc->rx_commit = 0;
  343. }
  344. if ( i > 0 )
  345. DBGC2 ( vlc, "VELOCITY %p refilled %d RX descriptors\n",
  346. vlc, i );
  347. }
  348. /**
  349. * Open network device
  350. *
  351. * @v netdev Network device
  352. * @ret rc Return status code
  353. */
  354. static int velocity_open ( struct net_device *netdev ) {
  355. struct velocity_nic *vlc = netdev->priv;
  356. int rc;
  357. DBGC ( vlc, "VELOCITY %p open\n", vlc );
  358. DBGC ( vlc, "VELOCITY %p regs at: %p\n", vlc, vlc->regs );
  359. /* Allocate descriptor rings */
  360. if ( ( rc = velocity_alloc_rings ( vlc ) ) != 0 )
  361. return rc;
  362. velocity_refill_rx ( vlc );
  363. /* Enable TX/RX queue */
  364. writew ( VELOCITY_TXQCSRS_RUN0, vlc->regs + VELOCITY_TXQCSRS );
  365. writew ( VELOCITY_RXQCSR_RUN | VELOCITY_RXQCSR_WAK,
  366. vlc->regs + VELOCITY_RXQCSRS );
  367. /* Enable interrupts */
  368. writeb ( 0xff, vlc->regs + VELOCITY_IMR0 );
  369. writeb ( 0xff, vlc->regs + VELOCITY_IMR1 );
  370. /* Start MAC */
  371. writeb ( VELOCITY_CR0_STOP, vlc->regs + VELOCITY_CRC0 );
  372. writeb ( VELOCITY_CR1_DPOLL, vlc->regs + VELOCITY_CRC0 );
  373. writeb ( VELOCITY_CR0_START | VELOCITY_CR0_TXON | VELOCITY_CR0_RXON,
  374. vlc->regs + VELOCITY_CRS0 );
  375. /* Receive all packets */
  376. writeb ( 0xff, vlc->regs + VELOCITY_RCR );
  377. /* Set initial link state */
  378. velocity_check_link ( netdev );
  379. velocity_autopoll_start ( vlc );
  380. DBGC2 ( vlc, "VELOCITY %p CR3 %02x\n",
  381. vlc, readb ( vlc->regs + 0x0B ) );
  382. return 0;
  383. }
  384. /**
  385. * Close network device
  386. *
  387. * @v netdev Network device
  388. */
  389. static void velocity_close ( struct net_device *netdev ) {
  390. struct velocity_nic *vlc = netdev->priv;
  391. int i;
  392. /* Stop NIC */
  393. writeb ( VELOCITY_CR0_TXON | VELOCITY_CR0_RXON,
  394. vlc->regs + VELOCITY_CRC0 );
  395. writeb ( VELOCITY_CR0_STOP, vlc->regs + VELOCITY_CRS0 );
  396. /* Clear RX ring information */
  397. writel ( 0, vlc->regs + VELOCITY_RXDESC_ADDR_LO );
  398. writew ( 0, vlc->regs + VELOCITY_RXDESCNUM );
  399. /* Destroy RX ring */
  400. free_dma ( vlc->rx_ring, VELOCITY_RXDESC_SIZE );
  401. vlc->rx_ring = NULL;
  402. vlc->rx_prod = 0;
  403. vlc->rx_cons = 0;
  404. /* Discard receive buffers */
  405. for ( i = 0 ; i < VELOCITY_RXDESC_NUM ; i++ ) {
  406. if ( vlc->rx_buffs[i] )
  407. free_iob ( vlc->rx_buffs[i] );
  408. vlc->rx_buffs[i] = NULL;
  409. }
  410. /* Clear TX ring information */
  411. writel ( 0, vlc->regs + VELOCITY_TXDESC_ADDR_LO0 );
  412. writew ( 0, vlc->regs + VELOCITY_TXDESCNUM );
  413. /* Destroy TX ring */
  414. free_dma ( vlc->tx_ring, VELOCITY_TXDESC_SIZE );
  415. vlc->tx_ring = NULL;
  416. vlc->tx_prod = 0;
  417. vlc->tx_cons = 0;
  418. }
  419. /**
  420. * Transmit packet
  421. *
  422. * @v netdev Network device
  423. * @v iobuf I/O buffer
  424. * @ret rc Return status code
  425. */
  426. static int velocity_transmit ( struct net_device *netdev,
  427. struct io_buffer *iobuf ) {
  428. struct velocity_nic *vlc = netdev->priv;
  429. struct velocity_tx_descriptor *desc;
  430. unsigned int tx_idx;
  431. /* Pad packet to minimum length */
  432. iob_pad ( iobuf, ETH_ZLEN );
  433. tx_idx = ( vlc->tx_prod++ % VELOCITY_TXDESC_NUM );
  434. desc = &vlc->tx_ring[tx_idx];
  435. /* Set packet size and transfer ownership to NIC */
  436. desc->des0 = cpu_to_le32 ( VELOCITY_DES0_OWN |
  437. VELOCITY_DES2_SIZE ( iob_len ( iobuf ) ) );
  438. /* Data in first desc fragment, only desc for packet, generate INT */
  439. desc->des1 = cpu_to_le32 ( VELOCITY_DES1_FRAG ( 1 ) |
  440. VELOCITY_DES1_TCPLS |
  441. VELOCITY_DES1_INTR );
  442. desc->frags[0].addr = virt_to_le32bus ( iobuf->data );
  443. desc->frags[0].des2 = cpu_to_le32 (
  444. VELOCITY_DES2_SIZE ( iob_len ( iobuf ) ) );
  445. wmb();
  446. /* Initiate TX */
  447. velocity_setbit ( vlc->regs + VELOCITY_TXQCSRS, VELOCITY_TXQCSRS_WAK0 );
  448. DBGC2 ( vlc, "VELOCITY %p tx_prod=%d desc=%p iobuf=%p len=%zd\n",
  449. vlc, tx_idx, desc, iobuf->data, iob_len ( iobuf ) );
  450. return 0;
  451. }
  452. /**
  453. * Poll for received packets.
  454. *
  455. * @v vlc Velocity device
  456. */
  457. static void velocity_poll_rx ( struct velocity_nic *vlc ) {
  458. struct velocity_rx_descriptor *desc;
  459. struct io_buffer *iobuf;
  460. int rx_idx;
  461. size_t len;
  462. uint32_t des0;
  463. /* Check for packets */
  464. while ( vlc->rx_cons != vlc->rx_prod ) {
  465. rx_idx = ( vlc->rx_cons % VELOCITY_RXDESC_NUM );
  466. desc = &vlc->rx_ring[rx_idx];
  467. des0 = cpu_to_le32 ( desc->des0 );
  468. /* Return if descriptor still in use */
  469. if ( des0 & VELOCITY_DES0_OWN )
  470. return;
  471. iobuf = vlc->rx_buffs[rx_idx];
  472. /* Get length, strip CRC */
  473. len = VELOCITY_DES0_RMBC ( des0 ) - 4;
  474. iob_put ( iobuf, len );
  475. DBGC2 ( vlc, "VELOCITY %p got packet on idx=%d (prod=%d), len %zd\n",
  476. vlc, rx_idx, vlc->rx_prod % VELOCITY_RXDESC_NUM, len );
  477. if ( des0 & VELOCITY_DES0_RX_ERR ) {
  478. /* Report receive error */
  479. netdev_rx_err ( vlc->netdev, iobuf, -EINVAL );
  480. DBGC ( vlc, "VELOCITY %p receive error, status: %02x\n",
  481. vlc, des0 );
  482. } else if ( des0 & VELOCITY_DES0_RXOK ) {
  483. /* Report receive success */
  484. netdev_rx( vlc->netdev, iobuf );
  485. } else {
  486. /* Card indicated neither success nor failure
  487. * Technically this shouldn't happen, but we saw it
  488. * in debugging once. */
  489. DBGC ( vlc, "VELOCITY %p RX neither ERR nor OK: %04x\n",
  490. vlc, des0 );
  491. DBGC ( vlc, "packet len: %zd\n", len );
  492. DBGC_HD ( vlc, iobuf->data, 64 );
  493. /* we don't know what it is, treat is as an error */
  494. netdev_rx_err ( vlc->netdev, iobuf, -EINVAL );
  495. }
  496. vlc->rx_cons++;
  497. }
  498. }
  499. /**
  500. * Poll for completed packets.
  501. *
  502. * @v vlc Velocity device
  503. */
  504. static void velocity_poll_tx ( struct velocity_nic *vlc ) {
  505. struct velocity_tx_descriptor *desc;
  506. int tx_idx;
  507. /* Check for packets */
  508. while ( vlc->tx_cons != vlc->tx_prod ) {
  509. tx_idx = ( vlc->tx_cons % VELOCITY_TXDESC_NUM );
  510. desc = &vlc->tx_ring[tx_idx];
  511. /* Return if descriptor still in use */
  512. if ( le32_to_cpu ( desc->des0 ) & VELOCITY_DES0_OWN )
  513. return;
  514. /* Report errors */
  515. if ( le32_to_cpu ( desc->des0 ) & VELOCITY_DES0_TERR ) {
  516. netdev_tx_complete_next_err ( vlc->netdev, -EINVAL );
  517. return;
  518. }
  519. netdev_tx_complete_next ( vlc->netdev );
  520. DBGC2 ( vlc, "VELOCITY %p poll_tx cons=%d prod=%d tsr=%04x\n",
  521. vlc, tx_idx, vlc->tx_prod % VELOCITY_TXDESC_NUM,
  522. ( desc->des0 & 0xffff ) );
  523. vlc->tx_cons++;
  524. }
  525. }
  526. /**
  527. * Poll for completed and received packets
  528. *
  529. * @v netdev Network device
  530. */
  531. static void velocity_poll ( struct net_device *netdev ) {
  532. struct velocity_nic *vlc = netdev->priv;
  533. uint8_t isr1;
  534. isr1 = readb ( vlc->regs + VELOCITY_ISR1 );
  535. /* ACK interrupts */
  536. writew ( 0xFFFF, vlc->regs + VELOCITY_ISR0 );
  537. /* Check for competed packets */
  538. velocity_poll_rx ( vlc );
  539. velocity_poll_tx ( vlc );
  540. if ( isr1 & VELOCITY_ISR1_SRCI ) {
  541. /* Update linkstate */
  542. DBGC2 ( vlc, "VELOCITY %p link status interrupt\n", vlc );
  543. velocity_check_link ( netdev );
  544. }
  545. velocity_refill_rx ( vlc );
  546. /* deal with potential RX stall caused by RX ring underrun */
  547. writew ( VELOCITY_RXQCSR_RUN | VELOCITY_RXQCSR_WAK,
  548. vlc->regs + VELOCITY_RXQCSRS );
  549. }
  550. /**
  551. * Enable or disable interrupts
  552. *
  553. * @v netdev Network device
  554. * @v enable Interrupts should be enabled
  555. */
  556. static void velocity_irq ( struct net_device *netdev, int enable ) {
  557. struct velocity_nic *vlc = netdev->priv;
  558. DBGC ( vlc, "VELOCITY %p interrupts %s\n", vlc,
  559. enable ? "enable" : "disable" );
  560. if (enable) {
  561. /* Enable interrupts */
  562. writeb ( VELOCITY_CR3_GINTMSK1, vlc->regs + VELOCITY_CRS3 );
  563. } else {
  564. /* Disable interrupts */
  565. writeb ( VELOCITY_CR3_GINTMSK1, vlc->regs + VELOCITY_CRC3 );
  566. }
  567. }
  568. /** Velocity network device operations */
  569. static struct net_device_operations velocity_operations = {
  570. .open = velocity_open,
  571. .close = velocity_close,
  572. .transmit = velocity_transmit,
  573. .poll = velocity_poll,
  574. .irq = velocity_irq,
  575. };
  576. /******************************************************************************
  577. *
  578. * PCI interface
  579. *
  580. ******************************************************************************
  581. */
  582. /**
  583. * Probe PCI device
  584. *
  585. * @v pci PCI device
  586. * @ret rc Return status code
  587. */
  588. static int velocity_probe ( struct pci_device *pci ) {
  589. struct net_device *netdev;
  590. struct velocity_nic *vlc;
  591. int rc;
  592. /* Allocate and initialise net device */
  593. netdev = alloc_etherdev ( sizeof ( *vlc ) );
  594. if ( ! netdev ) {
  595. rc = -ENOMEM;
  596. goto err_alloc;
  597. }
  598. netdev_init ( netdev, &velocity_operations );
  599. vlc = netdev->priv;
  600. pci_set_drvdata ( pci, netdev );
  601. netdev->dev = &pci->dev;
  602. /* Fix up PCI device */
  603. adjust_pci_device ( pci );
  604. /* Map registers */
  605. vlc->regs = ioremap ( pci->membase, VELOCITY_BAR_SIZE );
  606. vlc->netdev = netdev;
  607. /* Reset the NIC */
  608. if ( ( rc = velocity_reset ( vlc ) ) != 0 )
  609. goto err_reset;
  610. /* Reload EEPROM */
  611. if ( ( rc = velocity_reload_eeprom ( vlc ) ) != 0 )
  612. goto err_reset;
  613. /* Get MAC address */
  614. netdev->hw_addr[0] = readb ( vlc->regs + VELOCITY_MAC0 );
  615. netdev->hw_addr[1] = readb ( vlc->regs + VELOCITY_MAC1 );
  616. netdev->hw_addr[2] = readb ( vlc->regs + VELOCITY_MAC2 );
  617. netdev->hw_addr[3] = readb ( vlc->regs + VELOCITY_MAC3 );
  618. netdev->hw_addr[4] = readb ( vlc->regs + VELOCITY_MAC4 );
  619. netdev->hw_addr[5] = readb ( vlc->regs + VELOCITY_MAC5 );
  620. /* Initialise and reset MII interface */
  621. mdio_init ( &vlc->mdio, &velocity_mii_operations );
  622. mii_init ( &vlc->mii, &vlc->mdio, 0 );
  623. if ( ( rc = mii_reset ( &vlc->mii ) ) != 0 ) {
  624. DBGC ( vlc, "VELOCITY %p could not reset MII: %s\n",
  625. vlc, strerror ( rc ) );
  626. goto err_mii_reset;
  627. }
  628. /* Enable proper link advertising */
  629. velocity_set_link ( vlc );
  630. /* Register network device */
  631. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  632. goto err_register_netdev;
  633. return 0;
  634. err_register_netdev:
  635. err_mii_reset:
  636. velocity_reset ( vlc );
  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 velocity_remove ( struct pci_device *pci ) {
  649. struct net_device *netdev = pci_get_drvdata ( pci );
  650. struct velocity_nic *vlc = netdev->priv;
  651. /* Unregister network device */
  652. unregister_netdev ( netdev );
  653. /* Reset card */
  654. velocity_reset ( vlc );
  655. /* Free network device */
  656. netdev_nullify ( netdev );
  657. netdev_put ( netdev );
  658. }
  659. /** Velocity PCI device IDs */
  660. static struct pci_device_id velocity_nics[] = {
  661. PCI_ROM ( 0x1106, 0x3119, "vt6122", "VIA Velocity", 0 ),
  662. };
  663. /** Velocity PCI driver */
  664. struct pci_driver velocity_driver __pci_driver = {
  665. .ids = velocity_nics,
  666. .id_count = ( sizeof ( velocity_nics ) / sizeof ( velocity_nics[0] ) ),
  667. .probe = velocity_probe,
  668. .remove = velocity_remove,
  669. };