Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

velocity.c 20KB

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