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.

realtek.c 28KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * (EEPROM code originally implemented for rtl8139.c)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. */
  21. FILE_LICENCE ( GPL2_OR_LATER );
  22. #include <stdint.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <byteswap.h>
  27. #include <ipxe/netdevice.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/if_ether.h>
  30. #include <ipxe/iobuf.h>
  31. #include <ipxe/malloc.h>
  32. #include <ipxe/pci.h>
  33. #include <ipxe/nvs.h>
  34. #include <ipxe/threewire.h>
  35. #include <ipxe/bitbash.h>
  36. #include <ipxe/mii.h>
  37. #include "realtek.h"
  38. /** @file
  39. *
  40. * Realtek 10/100/1000 network card driver
  41. *
  42. * Based on the following datasheets:
  43. *
  44. * http://www.datasheetarchive.com/dl/Datasheets-8/DSA-153536.pdf
  45. * http://www.datasheetarchive.com/indexdl/Datasheet-028/DSA00494723.pdf
  46. */
  47. /******************************************************************************
  48. *
  49. * EEPROM interface
  50. *
  51. ******************************************************************************
  52. */
  53. /** Pin mapping for SPI bit-bashing interface */
  54. static const uint8_t realtek_eeprom_bits[] = {
  55. [SPI_BIT_SCLK] = RTL_9346CR_EESK,
  56. [SPI_BIT_MOSI] = RTL_9346CR_EEDI,
  57. [SPI_BIT_MISO] = RTL_9346CR_EEDO,
  58. [SPI_BIT_SS(0)] = ( RTL_9346CR_EECS | RTL_9346CR_EEM1 ),
  59. };
  60. /**
  61. * Read input bit
  62. *
  63. * @v basher Bit-bashing interface
  64. * @v bit_id Bit number
  65. * @ret zero Input is a logic 0
  66. * @ret non-zero Input is a logic 1
  67. */
  68. static int realtek_spi_read_bit ( struct bit_basher *basher,
  69. unsigned int bit_id ) {
  70. struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
  71. spibit.basher );
  72. uint8_t mask = realtek_eeprom_bits[bit_id];
  73. uint8_t reg;
  74. DBG_DISABLE ( DBGLVL_IO );
  75. reg = readb ( rtl->regs + RTL_9346CR );
  76. DBG_ENABLE ( DBGLVL_IO );
  77. return ( reg & mask );
  78. }
  79. /**
  80. * Set/clear output bit
  81. *
  82. * @v basher Bit-bashing interface
  83. * @v bit_id Bit number
  84. * @v data Value to write
  85. */
  86. static void realtek_spi_write_bit ( struct bit_basher *basher,
  87. unsigned int bit_id, unsigned long data ) {
  88. struct realtek_nic *rtl = container_of ( basher, struct realtek_nic,
  89. spibit.basher );
  90. uint8_t mask = realtek_eeprom_bits[bit_id];
  91. uint8_t reg;
  92. DBG_DISABLE ( DBGLVL_IO );
  93. reg = readb ( rtl->regs + RTL_9346CR );
  94. reg &= ~mask;
  95. reg |= ( data & mask );
  96. writeb ( reg, rtl->regs + RTL_9346CR );
  97. DBG_ENABLE ( DBGLVL_IO );
  98. }
  99. /** SPI bit-bashing interface */
  100. static struct bit_basher_operations realtek_basher_ops = {
  101. .read = realtek_spi_read_bit,
  102. .write = realtek_spi_write_bit,
  103. };
  104. /**
  105. * Initialise EEPROM
  106. *
  107. * @v netdev Network device
  108. */
  109. static void realtek_init_eeprom ( struct net_device *netdev ) {
  110. struct realtek_nic *rtl = netdev->priv;
  111. /* Initialise SPI bit-bashing interface */
  112. rtl->spibit.basher.op = &realtek_basher_ops;
  113. rtl->spibit.bus.mode = SPI_MODE_THREEWIRE;
  114. init_spi_bit_basher ( &rtl->spibit );
  115. /* Detect EEPROM type and initialise three-wire device */
  116. if ( readl ( rtl->regs + RTL_RCR ) & RTL_RCR_9356SEL ) {
  117. DBGC ( rtl, "REALTEK %p EEPROM is a 93C56\n", rtl );
  118. init_at93c56 ( &rtl->eeprom, 16 );
  119. } else {
  120. DBGC ( rtl, "REALTEK %p EEPROM is a 93C46\n", rtl );
  121. init_at93c46 ( &rtl->eeprom, 16 );
  122. }
  123. rtl->eeprom.bus = &rtl->spibit.bus;
  124. /* Initialise space for non-volatile options, if available
  125. *
  126. * We use offset 0x40 (i.e. address 0x20), length 0x40. This
  127. * block is marked as VPD in the Realtek datasheets, so we use
  128. * it only if we detect that the card is not supporting VPD.
  129. */
  130. if ( readb ( rtl->regs + RTL_CONFIG1 ) & RTL_CONFIG1_VPD ) {
  131. DBGC ( rtl, "REALTEK %p EEPROM in use for VPD; cannot use "
  132. "for options\n", rtl );
  133. } else {
  134. nvo_init ( &rtl->nvo, &rtl->eeprom.nvs, RTL_EEPROM_VPD,
  135. RTL_EEPROM_VPD_LEN, NULL, &netdev->refcnt );
  136. }
  137. }
  138. /******************************************************************************
  139. *
  140. * MII interface
  141. *
  142. ******************************************************************************
  143. */
  144. /**
  145. * Read from MII register
  146. *
  147. * @v mii MII interface
  148. * @v reg Register address
  149. * @ret value Data read, or negative error
  150. */
  151. static int realtek_mii_read ( struct mii_interface *mii, unsigned int reg ) {
  152. struct realtek_nic *rtl = container_of ( mii, struct realtek_nic, mii );
  153. unsigned int i;
  154. uint32_t value;
  155. /* Fail if PHYAR register is not present */
  156. if ( ! rtl->have_phy_regs )
  157. return -ENOTSUP;
  158. /* Initiate read */
  159. writel ( RTL_PHYAR_VALUE ( 0, reg, 0 ), rtl->regs + RTL_PHYAR );
  160. /* Wait for read to complete */
  161. for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
  162. /* If read is not complete, delay 1us and retry */
  163. value = readl ( rtl->regs + RTL_PHYAR );
  164. if ( ! ( value & RTL_PHYAR_FLAG ) ) {
  165. udelay ( 1 );
  166. continue;
  167. }
  168. /* Return register value */
  169. return ( RTL_PHYAR_DATA ( value ) );
  170. }
  171. DBGC ( rtl, "REALTEK %p timed out waiting for MII read\n", rtl );
  172. return -ETIMEDOUT;
  173. }
  174. /**
  175. * Write to MII register
  176. *
  177. * @v mii MII interface
  178. * @v reg Register address
  179. * @v data Data to write
  180. * @ret rc Return status code
  181. */
  182. static int realtek_mii_write ( struct mii_interface *mii, unsigned int reg,
  183. unsigned int data) {
  184. struct realtek_nic *rtl = container_of ( mii, struct realtek_nic, mii );
  185. unsigned int i;
  186. /* Fail if PHYAR register is not present */
  187. if ( ! rtl->have_phy_regs )
  188. return -ENOTSUP;
  189. /* Initiate write */
  190. writel ( RTL_PHYAR_VALUE ( RTL_PHYAR_FLAG, reg, data ),
  191. rtl->regs + RTL_PHYAR );
  192. /* Wait for write to complete */
  193. for ( i = 0 ; i < RTL_MII_MAX_WAIT_US ; i++ ) {
  194. /* If write is not complete, delay 1us and retry */
  195. if ( readl ( rtl->regs + RTL_PHYAR ) & RTL_PHYAR_FLAG ) {
  196. udelay ( 1 );
  197. continue;
  198. }
  199. return 0;
  200. }
  201. DBGC ( rtl, "REALTEK %p timed out waiting for MII write\n", rtl );
  202. return -ETIMEDOUT;
  203. }
  204. /** Realtek MII operations */
  205. static struct mii_operations realtek_mii_operations = {
  206. .read = realtek_mii_read,
  207. .write = realtek_mii_write,
  208. };
  209. /******************************************************************************
  210. *
  211. * Device reset
  212. *
  213. ******************************************************************************
  214. */
  215. /**
  216. * Reset hardware
  217. *
  218. * @v rtl Realtek device
  219. * @ret rc Return status code
  220. */
  221. static int realtek_reset ( struct realtek_nic *rtl ) {
  222. unsigned int i;
  223. /* Issue reset */
  224. writeb ( RTL_CR_RST, rtl->regs + RTL_CR );
  225. /* Wait for reset to complete */
  226. for ( i = 0 ; i < RTL_RESET_MAX_WAIT_MS ; i++ ) {
  227. /* If reset is not complete, delay 1ms and retry */
  228. if ( readb ( rtl->regs + RTL_CR ) & RTL_CR_RST ) {
  229. mdelay ( 1 );
  230. continue;
  231. }
  232. return 0;
  233. }
  234. DBGC ( rtl, "REALTEK %p timed out waiting for reset\n", rtl );
  235. return -ETIMEDOUT;
  236. }
  237. /******************************************************************************
  238. *
  239. * Link state
  240. *
  241. ******************************************************************************
  242. */
  243. /**
  244. * Check link state
  245. *
  246. * @v netdev Network device
  247. */
  248. static void realtek_check_link ( struct net_device *netdev ) {
  249. struct realtek_nic *rtl = netdev->priv;
  250. uint8_t phystatus;
  251. uint8_t msr;
  252. int link_up;
  253. /* Determine link state */
  254. if ( rtl->have_phy_regs ) {
  255. phystatus = readb ( rtl->regs + RTL_PHYSTATUS );
  256. link_up = ( phystatus & RTL_PHYSTATUS_LINKSTS );
  257. DBGC ( rtl, "REALTEK %p PHY status is %02x\n", rtl, phystatus );
  258. } else {
  259. msr = readb ( rtl->regs + RTL_MSR );
  260. link_up = ( ! ( msr & RTL_MSR_LINKB ) );
  261. DBGC ( rtl, "REALTEK %p media status is %02x\n", rtl, msr );
  262. }
  263. /* Report link state */
  264. if ( link_up ) {
  265. netdev_link_up ( netdev );
  266. } else {
  267. netdev_link_down ( netdev );
  268. }
  269. }
  270. /******************************************************************************
  271. *
  272. * Network device interface
  273. *
  274. ******************************************************************************
  275. */
  276. /**
  277. * Create receive buffer (legacy mode)
  278. *
  279. * @v rtl Realtek device
  280. * @ret rc Return status code
  281. */
  282. static int realtek_create_buffer ( struct realtek_nic *rtl ) {
  283. size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
  284. physaddr_t address;
  285. int rc;
  286. /* Do nothing unless in legacy mode */
  287. if ( ! rtl->legacy )
  288. return 0;
  289. /* Allocate buffer */
  290. rtl->rx_buffer = malloc_dma ( len, RTL_RXBUF_ALIGN );
  291. if ( ! rtl->rx_buffer ) {
  292. rc = -ENOMEM;
  293. goto err_alloc;
  294. }
  295. address = virt_to_bus ( rtl->rx_buffer );
  296. /* Check that card can support address */
  297. if ( address & ~0xffffffffULL ) {
  298. DBGC ( rtl, "REALTEK %p cannot support 64-bit RX buffer "
  299. "address\n", rtl );
  300. rc = -ENOTSUP;
  301. goto err_64bit;
  302. }
  303. /* Program buffer address */
  304. writel ( address, rtl->regs + RTL_RBSTART );
  305. DBGC ( rtl, "REALTEK %p receive buffer is at [%08llx,%08llx,%08llx)\n",
  306. rtl, ( ( unsigned long long ) address ),
  307. ( ( unsigned long long ) address + RTL_RXBUF_LEN ),
  308. ( ( unsigned long long ) address + len ) );
  309. return 0;
  310. err_64bit:
  311. free_dma ( rtl->rx_buffer, len );
  312. rtl->rx_buffer = NULL;
  313. err_alloc:
  314. return rc;
  315. }
  316. /**
  317. * Destroy receive buffer (legacy mode)
  318. *
  319. * @v rtl Realtek device
  320. */
  321. static void realtek_destroy_buffer ( struct realtek_nic *rtl ) {
  322. size_t len = ( RTL_RXBUF_LEN + RTL_RXBUF_PAD );
  323. /* Do nothing unless in legacy mode */
  324. if ( ! rtl->legacy )
  325. return;
  326. /* Clear buffer address */
  327. writel ( 0, rtl->regs + RTL_RBSTART );
  328. /* Free buffer */
  329. free_dma ( rtl->rx_buffer, len );
  330. rtl->rx_buffer = NULL;
  331. rtl->rx_offset = 0;
  332. }
  333. /**
  334. * Create descriptor ring
  335. *
  336. * @v rtl Realtek device
  337. * @v ring Descriptor ring
  338. * @ret rc Return status code
  339. */
  340. static int realtek_create_ring ( struct realtek_nic *rtl,
  341. struct realtek_ring *ring ) {
  342. physaddr_t address;
  343. /* Do nothing in legacy mode */
  344. if ( rtl->legacy )
  345. return 0;
  346. /* Allocate descriptor ring */
  347. ring->desc = malloc_dma ( ring->len, RTL_RING_ALIGN );
  348. if ( ! ring->desc )
  349. return -ENOMEM;
  350. /* Initialise descriptor ring */
  351. memset ( ring->desc, 0, ring->len );
  352. /* Program ring address */
  353. address = virt_to_bus ( ring->desc );
  354. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  355. rtl->regs + ring->reg + 4 );
  356. writel ( ( address & 0xffffffffUL ), rtl->regs + ring->reg );
  357. DBGC ( rtl, "REALTEK %p ring %02x is at [%08llx,%08llx)\n",
  358. rtl, ring->reg, ( ( unsigned long long ) address ),
  359. ( ( unsigned long long ) address + ring->len ) );
  360. return 0;
  361. }
  362. /**
  363. * Destroy descriptor ring
  364. *
  365. * @v rtl Realtek device
  366. * @v ring Descriptor ring
  367. */
  368. static void realtek_destroy_ring ( struct realtek_nic *rtl,
  369. struct realtek_ring *ring ) {
  370. /* Do nothing in legacy mode */
  371. if ( rtl->legacy )
  372. return;
  373. /* Clear ring address */
  374. writel ( 0, rtl->regs + ring->reg );
  375. writel ( 0, rtl->regs + ring->reg + 4 );
  376. /* Free descriptor ring */
  377. free_dma ( ring->desc, ring->len );
  378. ring->desc = NULL;
  379. ring->prod = 0;
  380. ring->cons = 0;
  381. }
  382. /**
  383. * Refill receive descriptor ring
  384. *
  385. * @v rtl Realtek device
  386. */
  387. static void realtek_refill_rx ( struct realtek_nic *rtl ) {
  388. struct realtek_descriptor *rx;
  389. struct io_buffer *iobuf;
  390. unsigned int rx_idx;
  391. physaddr_t address;
  392. int is_last;
  393. /* Do nothing in legacy mode */
  394. if ( rtl->legacy )
  395. return;
  396. while ( ( rtl->rx.prod - rtl->rx.cons ) < RTL_NUM_RX_DESC ) {
  397. /* Allocate I/O buffer */
  398. iobuf = alloc_iob ( RTL_RX_MAX_LEN );
  399. if ( ! iobuf ) {
  400. /* Wait for next refill */
  401. return;
  402. }
  403. /* Get next receive descriptor */
  404. rx_idx = ( rtl->rx.prod++ % RTL_NUM_RX_DESC );
  405. is_last = ( rx_idx == ( RTL_NUM_RX_DESC - 1 ) );
  406. rx = &rtl->rx.desc[rx_idx];
  407. /* Populate receive descriptor */
  408. address = virt_to_bus ( iobuf->data );
  409. rx->address = cpu_to_le64 ( address );
  410. rx->length = cpu_to_le16 ( RTL_RX_MAX_LEN );
  411. wmb();
  412. rx->flags = ( cpu_to_le16 ( RTL_DESC_OWN ) |
  413. ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
  414. wmb();
  415. /* Record I/O buffer */
  416. assert ( rtl->rx_iobuf[rx_idx] == NULL );
  417. rtl->rx_iobuf[rx_idx] = iobuf;
  418. DBGC2 ( rtl, "REALTEK %p RX %d is [%llx,%llx)\n", rtl, rx_idx,
  419. ( ( unsigned long long ) address ),
  420. ( ( unsigned long long ) address + RTL_RX_MAX_LEN ) );
  421. }
  422. }
  423. /**
  424. * Open network device
  425. *
  426. * @v netdev Network device
  427. * @ret rc Return status code
  428. */
  429. static int realtek_open ( struct net_device *netdev ) {
  430. struct realtek_nic *rtl = netdev->priv;
  431. uint32_t tcr;
  432. uint32_t rcr;
  433. int rc;
  434. /* Create transmit descriptor ring */
  435. if ( ( rc = realtek_create_ring ( rtl, &rtl->tx ) ) != 0 )
  436. goto err_create_tx;
  437. /* Create receive descriptor ring */
  438. if ( ( rc = realtek_create_ring ( rtl, &rtl->rx ) ) != 0 )
  439. goto err_create_rx;
  440. /* Create receive buffer */
  441. if ( ( rc = realtek_create_buffer ( rtl ) ) != 0 )
  442. goto err_create_buffer;
  443. /* Accept all packets */
  444. writel ( 0xffffffffUL, rtl->regs + RTL_MAR0 );
  445. writel ( 0xffffffffUL, rtl->regs + RTL_MAR4 );
  446. /* Enable transmitter and receiver. RTL8139 requires that
  447. * this happens before writing to RCR.
  448. */
  449. writeb ( ( RTL_CR_TE | RTL_CR_RE ), rtl->regs + RTL_CR );
  450. /* Configure transmitter */
  451. tcr = readl ( rtl->regs + RTL_TCR );
  452. tcr &= ~RTL_TCR_MXDMA_MASK;
  453. tcr |= RTL_TCR_MXDMA_DEFAULT;
  454. writel ( tcr, rtl->regs + RTL_TCR );
  455. /* Configure receiver */
  456. rcr = readl ( rtl->regs + RTL_RCR );
  457. rcr &= ~( RTL_RCR_RXFTH_MASK | RTL_RCR_RBLEN_MASK |
  458. RTL_RCR_MXDMA_MASK );
  459. rcr |= ( RTL_RCR_RXFTH_DEFAULT | RTL_RCR_RBLEN_DEFAULT |
  460. RTL_RCR_MXDMA_DEFAULT | RTL_RCR_WRAP | RTL_RCR_AB |
  461. RTL_RCR_AM | RTL_RCR_APM | RTL_RCR_AAP );
  462. writel ( rcr, rtl->regs + RTL_RCR );
  463. /* Fill receive ring */
  464. realtek_refill_rx ( rtl );
  465. /* Update link state */
  466. realtek_check_link ( netdev );
  467. return 0;
  468. realtek_destroy_buffer ( rtl );
  469. err_create_buffer:
  470. realtek_destroy_ring ( rtl, &rtl->rx );
  471. err_create_rx:
  472. realtek_destroy_ring ( rtl, &rtl->tx );
  473. err_create_tx:
  474. return rc;
  475. }
  476. /**
  477. * Close network device
  478. *
  479. * @v netdev Network device
  480. */
  481. static void realtek_close ( struct net_device *netdev ) {
  482. struct realtek_nic *rtl = netdev->priv;
  483. unsigned int i;
  484. /* Disable receiver and transmitter */
  485. writeb ( 0, rtl->regs + RTL_CR );
  486. /* Destroy receive buffer */
  487. realtek_destroy_buffer ( rtl );
  488. /* Destroy receive descriptor ring */
  489. realtek_destroy_ring ( rtl, &rtl->rx );
  490. /* Discard any unused receive buffers */
  491. for ( i = 0 ; i < RTL_NUM_RX_DESC ; i++ ) {
  492. if ( rtl->rx_iobuf[i] )
  493. free_iob ( rtl->rx_iobuf[i] );
  494. rtl->rx_iobuf[i] = NULL;
  495. }
  496. /* Destroy transmit descriptor ring */
  497. realtek_destroy_ring ( rtl, &rtl->tx );
  498. }
  499. /**
  500. * Transmit packet
  501. *
  502. * @v netdev Network device
  503. * @v iobuf I/O buffer
  504. * @ret rc Return status code
  505. */
  506. static int realtek_transmit ( struct net_device *netdev,
  507. struct io_buffer *iobuf ) {
  508. struct realtek_nic *rtl = netdev->priv;
  509. struct realtek_descriptor *tx;
  510. unsigned int tx_idx;
  511. physaddr_t address;
  512. int is_last;
  513. /* Get next transmit descriptor */
  514. if ( ( rtl->tx.prod - rtl->tx.cons ) >= RTL_NUM_TX_DESC ) {
  515. DBGC ( rtl, "REALTEK %p out of transmit descriptors\n", rtl );
  516. return -ENOBUFS;
  517. }
  518. tx_idx = ( rtl->tx.prod++ % RTL_NUM_TX_DESC );
  519. /* Transmit packet */
  520. if ( rtl->legacy ) {
  521. /* Pad and align packet */
  522. iob_pad ( iobuf, ETH_ZLEN );
  523. address = virt_to_bus ( iobuf->data );
  524. /* Check that card can support address */
  525. if ( address & ~0xffffffffULL ) {
  526. DBGC ( rtl, "REALTEK %p cannot support 64-bit TX "
  527. "buffer address\n", rtl );
  528. return -ENOTSUP;
  529. }
  530. /* Add to transmit ring */
  531. writel ( address, rtl->regs + RTL_TSAD ( tx_idx ) );
  532. writel ( ( RTL_TSD_ERTXTH_DEFAULT | iob_len ( iobuf ) ),
  533. rtl->regs + RTL_TSD ( tx_idx ) );
  534. } else {
  535. /* Populate transmit descriptor */
  536. address = virt_to_bus ( iobuf->data );
  537. is_last = ( tx_idx == ( RTL_NUM_TX_DESC - 1 ) );
  538. tx = &rtl->tx.desc[tx_idx];
  539. tx->address = cpu_to_le64 ( address );
  540. tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
  541. wmb();
  542. tx->flags = ( cpu_to_le16 ( RTL_DESC_OWN | RTL_DESC_FS |
  543. RTL_DESC_LS ) |
  544. ( is_last ? cpu_to_le16 ( RTL_DESC_EOR ) : 0 ) );
  545. wmb();
  546. /* Notify card that there are packets ready to transmit */
  547. writeb ( RTL_TPPOLL_NPQ, rtl->regs + rtl->tppoll );
  548. }
  549. DBGC2 ( rtl, "REALTEK %p TX %d is [%llx,%llx)\n", rtl, tx_idx,
  550. ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ),
  551. ( ( ( unsigned long long ) virt_to_bus ( iobuf->data ) ) +
  552. iob_len ( iobuf ) ) );
  553. return 0;
  554. }
  555. /**
  556. * Poll for completed packets
  557. *
  558. * @v netdev Network device
  559. */
  560. static void realtek_poll_tx ( struct net_device *netdev ) {
  561. struct realtek_nic *rtl = netdev->priv;
  562. struct realtek_descriptor *tx;
  563. unsigned int tx_idx;
  564. /* Check for completed packets */
  565. while ( rtl->tx.cons != rtl->tx.prod ) {
  566. /* Get next transmit descriptor */
  567. tx_idx = ( rtl->tx.cons % RTL_NUM_TX_DESC );
  568. /* Stop if descriptor is still in use */
  569. if ( rtl->legacy ) {
  570. /* Check ownership bit in transmit status register */
  571. if ( ! ( readl ( rtl->regs + RTL_TSD ( tx_idx ) ) &
  572. RTL_TSD_OWN ) )
  573. return;
  574. } else {
  575. /* Check ownership bit in descriptor */
  576. tx = &rtl->tx.desc[tx_idx];
  577. if ( tx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  578. return;
  579. }
  580. DBGC2 ( rtl, "REALTEK %p TX %d complete\n", rtl, tx_idx );
  581. /* Complete TX descriptor */
  582. netdev_tx_complete_next ( netdev );
  583. rtl->tx.cons++;
  584. }
  585. }
  586. /**
  587. * Poll for received packets (legacy mode)
  588. *
  589. * @v netdev Network device
  590. */
  591. static void realtek_legacy_poll_rx ( struct net_device *netdev ) {
  592. struct realtek_nic *rtl = netdev->priv;
  593. struct realtek_legacy_header *rx;
  594. struct io_buffer *iobuf;
  595. size_t len;
  596. /* Check for received packets */
  597. while ( ! ( readb ( rtl->regs + RTL_CR ) & RTL_CR_BUFE ) ) {
  598. /* Extract packet from receive buffer */
  599. rx = ( rtl->rx_buffer + rtl->rx_offset );
  600. len = le16_to_cpu ( rx->length );
  601. if ( rx->status & cpu_to_le16 ( RTL_STAT_ROK ) ) {
  602. DBGC2 ( rtl, "REALTEK %p RX offset %x+%zx\n",
  603. rtl, rtl->rx_offset, len );
  604. /* Allocate I/O buffer */
  605. iobuf = alloc_iob ( len );
  606. if ( ! iobuf ) {
  607. netdev_rx_err ( netdev, NULL, -ENOMEM );
  608. /* Leave packet for next poll */
  609. break;
  610. }
  611. /* Copy data to I/O buffer */
  612. memcpy ( iob_put ( iobuf, len ), rx->data, len );
  613. iob_unput ( iobuf, 4 /* strip CRC */ );
  614. /* Hand off to network stack */
  615. netdev_rx ( netdev, iobuf );
  616. } else {
  617. DBGC ( rtl, "REALTEK %p RX offset %x+%zx error %04x\n",
  618. rtl, rtl->rx_offset, len,
  619. le16_to_cpu ( rx->status ) );
  620. netdev_rx_err ( netdev, NULL, -EIO );
  621. }
  622. /* Update buffer offset */
  623. rtl->rx_offset = ( rtl->rx_offset + sizeof ( *rx ) + len );
  624. rtl->rx_offset = ( ( rtl->rx_offset + 3 ) & ~3 );
  625. rtl->rx_offset = ( rtl->rx_offset % RTL_RXBUF_LEN );
  626. writew ( ( rtl->rx_offset - 16 ), rtl->regs + RTL_CAPR );
  627. }
  628. }
  629. /**
  630. * Poll for received packets
  631. *
  632. * @v netdev Network device
  633. */
  634. static void realtek_poll_rx ( struct net_device *netdev ) {
  635. struct realtek_nic *rtl = netdev->priv;
  636. struct realtek_descriptor *rx;
  637. struct io_buffer *iobuf;
  638. unsigned int rx_idx;
  639. size_t len;
  640. /* Poll receive buffer if in legacy mode */
  641. if ( rtl->legacy ) {
  642. realtek_legacy_poll_rx ( netdev );
  643. return;
  644. }
  645. /* Check for received packets */
  646. while ( rtl->rx.cons != rtl->rx.prod ) {
  647. /* Get next receive descriptor */
  648. rx_idx = ( rtl->rx.cons % RTL_NUM_RX_DESC );
  649. rx = &rtl->rx.desc[rx_idx];
  650. /* Stop if descriptor is still in use */
  651. if ( rx->flags & cpu_to_le16 ( RTL_DESC_OWN ) )
  652. return;
  653. /* Populate I/O buffer */
  654. iobuf = rtl->rx_iobuf[rx_idx];
  655. rtl->rx_iobuf[rx_idx] = NULL;
  656. len = ( le16_to_cpu ( rx->length ) & RTL_DESC_SIZE_MASK );
  657. iob_put ( iobuf, ( len - 4 /* strip CRC */ ) );
  658. DBGC2 ( rtl, "REALTEK %p RX %d complete (length %zd)\n",
  659. rtl, rx_idx, len );
  660. /* Hand off to network stack */
  661. if ( rx->flags & cpu_to_le16 ( RTL_DESC_RES ) ) {
  662. netdev_rx_err ( netdev, iobuf, -EIO );
  663. } else {
  664. netdev_rx ( netdev, iobuf );
  665. }
  666. rtl->rx.cons++;
  667. }
  668. }
  669. /**
  670. * Poll for completed and received packets
  671. *
  672. * @v netdev Network device
  673. */
  674. static void realtek_poll ( struct net_device *netdev ) {
  675. struct realtek_nic *rtl = netdev->priv;
  676. uint16_t isr;
  677. /* Check for and acknowledge interrupts */
  678. isr = readw ( rtl->regs + RTL_ISR );
  679. if ( ! isr )
  680. return;
  681. writew ( isr, rtl->regs + RTL_ISR );
  682. /* Poll for TX completions, if applicable */
  683. if ( isr & ( RTL_IRQ_TER | RTL_IRQ_TOK ) )
  684. realtek_poll_tx ( netdev );
  685. /* Poll for RX completionsm, if applicable */
  686. if ( isr & ( RTL_IRQ_RER | RTL_IRQ_ROK ) )
  687. realtek_poll_rx ( netdev );
  688. /* Check link state, if applicable */
  689. if ( isr & RTL_IRQ_PUN_LINKCHG )
  690. realtek_check_link ( netdev );
  691. /* Refill RX ring */
  692. realtek_refill_rx ( rtl );
  693. }
  694. /**
  695. * Enable or disable interrupts
  696. *
  697. * @v netdev Network device
  698. * @v enable Interrupts should be enabled
  699. */
  700. static void realtek_irq ( struct net_device *netdev, int enable ) {
  701. struct realtek_nic *rtl = netdev->priv;
  702. uint16_t imr;
  703. /* Set interrupt mask */
  704. imr = ( enable ? ( RTL_IRQ_PUN_LINKCHG | RTL_IRQ_TER | RTL_IRQ_TOK |
  705. RTL_IRQ_RER | RTL_IRQ_ROK ) : 0 );
  706. writew ( imr, rtl->regs + RTL_IMR );
  707. }
  708. /** Realtek network device operations */
  709. static struct net_device_operations realtek_operations = {
  710. .open = realtek_open,
  711. .close = realtek_close,
  712. .transmit = realtek_transmit,
  713. .poll = realtek_poll,
  714. .irq = realtek_irq,
  715. };
  716. /******************************************************************************
  717. *
  718. * PCI interface
  719. *
  720. ******************************************************************************
  721. */
  722. /**
  723. * Detect device type
  724. *
  725. * @v rtl Realtek device
  726. */
  727. static void realtek_detect ( struct realtek_nic *rtl ) {
  728. uint16_t rms;
  729. uint16_t check_rms;
  730. uint16_t cpcr;
  731. uint16_t check_cpcr;
  732. /* The RX Packet Maximum Size register is present only on
  733. * 8169. Try to set to our intended MTU.
  734. */
  735. rms = RTL_RX_MAX_LEN;
  736. writew ( rms, rtl->regs + RTL_RMS );
  737. check_rms = readw ( rtl->regs + RTL_RMS );
  738. /* The C+ Command register is present only on 8169 and 8139C+.
  739. * Try to enable C+ mode and PCI Dual Address Cycle (for
  740. * 64-bit systems), if supported.
  741. */
  742. cpcr = ( RTL_CPCR_DAC | RTL_CPCR_MULRW | RTL_CPCR_CPRX |
  743. RTL_CPCR_CPTX );
  744. writew ( cpcr, rtl->regs + RTL_CPCR );
  745. check_cpcr = readw ( rtl->regs + RTL_CPCR );
  746. /* Detect device type */
  747. if ( check_rms == rms ) {
  748. DBGC ( rtl, "REALTEK %p appears to be an RTL8169\n", rtl );
  749. rtl->have_phy_regs = 1;
  750. rtl->tppoll = RTL_TPPOLL_8169;
  751. } else {
  752. if ( check_cpcr == cpcr ) {
  753. DBGC ( rtl, "REALTEK %p appears to be an RTL8139C+\n",
  754. rtl );
  755. rtl->tppoll = RTL_TPPOLL_8139CP;
  756. } else {
  757. DBGC ( rtl, "REALTEK %p appears to be an RTL8139\n",
  758. rtl );
  759. rtl->legacy = 1;
  760. }
  761. }
  762. }
  763. /**
  764. * Probe PCI device
  765. *
  766. * @v pci PCI device
  767. * @ret rc Return status code
  768. */
  769. static int realtek_probe ( struct pci_device *pci ) {
  770. struct net_device *netdev;
  771. struct realtek_nic *rtl;
  772. unsigned int i;
  773. int rc;
  774. /* Allocate and initialise net device */
  775. netdev = alloc_etherdev ( sizeof ( *rtl ) );
  776. if ( ! netdev ) {
  777. rc = -ENOMEM;
  778. goto err_alloc;
  779. }
  780. netdev_init ( netdev, &realtek_operations );
  781. rtl = netdev->priv;
  782. pci_set_drvdata ( pci, netdev );
  783. netdev->dev = &pci->dev;
  784. memset ( rtl, 0, sizeof ( *rtl ) );
  785. realtek_init_ring ( &rtl->tx, RTL_NUM_TX_DESC, RTL_TNPDS );
  786. realtek_init_ring ( &rtl->rx, RTL_NUM_RX_DESC, RTL_RDSAR );
  787. /* Fix up PCI device */
  788. adjust_pci_device ( pci );
  789. /* Map registers */
  790. rtl->regs = ioremap ( pci->membase, RTL_BAR_SIZE );
  791. /* Reset the NIC */
  792. if ( ( rc = realtek_reset ( rtl ) ) != 0 )
  793. goto err_reset;
  794. /* Detect device type */
  795. realtek_detect ( rtl );
  796. /* Initialise EEPROM */
  797. realtek_init_eeprom ( netdev );
  798. /* Read MAC address from EEPROM */
  799. if ( ( rc = nvs_read ( &rtl->eeprom.nvs, RTL_EEPROM_MAC,
  800. netdev->hw_addr, ETH_ALEN ) ) != 0 ) {
  801. DBGC ( rtl, "REALTEK %p could not read MAC address: %s\n",
  802. rtl, strerror ( rc ) );
  803. goto err_nvs_read;
  804. }
  805. /* The EEPROM may not be present for onboard NICs. Fall back
  806. * to reading the current ID register value, which will
  807. * hopefully have been programmed by the platform firmware.
  808. */
  809. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  810. DBGC ( rtl, "REALTEK %p seems to have no EEPROM\n", rtl );
  811. for ( i = 0 ; i < ETH_ALEN ; i++ )
  812. netdev->hw_addr[i] = readb ( rtl->regs + RTL_IDR0 + i );
  813. }
  814. /* Initialise and reset MII interface */
  815. mii_init ( &rtl->mii, &realtek_mii_operations );
  816. if ( rtl->have_phy_regs &&
  817. ( ( rc = mii_reset ( &rtl->mii ) ) != 0 ) ) {
  818. DBGC ( rtl, "REALTEK %p could not reset MII: %s\n",
  819. rtl, strerror ( rc ) );
  820. goto err_mii_reset;
  821. }
  822. /* Register network device */
  823. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  824. goto err_register_netdev;
  825. /* Set initial link state */
  826. realtek_check_link ( netdev );
  827. /* Register non-volatile options, if applicable */
  828. if ( rtl->nvo.nvs ) {
  829. if ( ( rc = register_nvo ( &rtl->nvo,
  830. netdev_settings ( netdev ) ) ) != 0)
  831. goto err_register_nvo;
  832. }
  833. return 0;
  834. err_register_nvo:
  835. unregister_netdev ( netdev );
  836. err_register_netdev:
  837. err_mii_reset:
  838. err_nvs_read:
  839. realtek_reset ( rtl );
  840. err_reset:
  841. iounmap ( rtl->regs );
  842. netdev_nullify ( netdev );
  843. netdev_put ( netdev );
  844. err_alloc:
  845. return rc;
  846. }
  847. /**
  848. * Remove PCI device
  849. *
  850. * @v pci PCI device
  851. */
  852. static void realtek_remove ( struct pci_device *pci ) {
  853. struct net_device *netdev = pci_get_drvdata ( pci );
  854. struct realtek_nic *rtl = netdev->priv;
  855. /* Unregister non-volatile options, if applicable */
  856. if ( rtl->nvo.nvs )
  857. unregister_nvo ( &rtl->nvo );
  858. /* Unregister network device */
  859. unregister_netdev ( netdev );
  860. /* Reset card */
  861. realtek_reset ( rtl );
  862. /* Free network device */
  863. iounmap ( rtl->regs );
  864. netdev_nullify ( netdev );
  865. netdev_put ( netdev );
  866. }
  867. /** Realtek PCI device IDs */
  868. static struct pci_device_id realtek_nics[] = {
  869. PCI_ROM ( 0x0001, 0x8168, "clone8169", "Cloned 8169", 0 ),
  870. PCI_ROM ( 0x018a, 0x0106, "fpc0106tx", "LevelOne FPC-0106TX", 0 ),
  871. PCI_ROM ( 0x021b, 0x8139, "hne300", "Compaq HNE-300", 0 ),
  872. PCI_ROM ( 0x02ac, 0x1012, "s1012", "SpeedStream 1012", 0 ),
  873. PCI_ROM ( 0x0357, 0x000a, "ttpmon", "TTTech TTP-Monitoring", 0 ),
  874. PCI_ROM ( 0x10ec, 0x8129, "rtl8129", "RTL-8129", 0 ),
  875. PCI_ROM ( 0x10ec, 0x8136, "rtl8136", "RTL8101E/RTL8102E", 0 ),
  876. PCI_ROM ( 0x10ec, 0x8138, "rtl8138", "RT8139 (B/C)", 0 ),
  877. PCI_ROM ( 0x10ec, 0x8139, "rtl8139", "RTL-8139/8139C/8139C+", 0 ),
  878. PCI_ROM ( 0x10ec, 0x8167, "rtl8167", "RTL-8110SC/8169SC", 0 ),
  879. PCI_ROM ( 0x10ec, 0x8168, "rtl8168", "RTL8111/8168B", 0 ),
  880. PCI_ROM ( 0x10ec, 0x8169, "rtl8169", "RTL-8169", 0 ),
  881. PCI_ROM ( 0x1113, 0x1211, "smc1211", "SMC2-1211TX", 0 ),
  882. PCI_ROM ( 0x1186, 0x1300, "dfe538", "DFE530TX+/DFE538TX", 0 ),
  883. PCI_ROM ( 0x1186, 0x1340, "dfe690", "DFE-690TXD", 0 ),
  884. PCI_ROM ( 0x1186, 0x4300, "dge528t", "DGE-528T", 0 ),
  885. PCI_ROM ( 0x11db, 0x1234, "sega8139", "Sega Enterprises 8139", 0 ),
  886. PCI_ROM ( 0x1259, 0xa117, "allied8139", "Allied Telesyn 8139", 0 ),
  887. PCI_ROM ( 0x1259, 0xa11e, "allied81xx", "Allied Telesyn 81xx", 0 ),
  888. PCI_ROM ( 0x1259, 0xc107, "allied8169", "Allied Telesyn 8169", 0 ),
  889. PCI_ROM ( 0x126c, 0x1211, "northen8139","Northern Telecom 8139", 0 ),
  890. PCI_ROM ( 0x13d1, 0xab06, "fe2000vx", "Abocom FE2000VX", 0 ),
  891. PCI_ROM ( 0x1432, 0x9130, "edi8139", "Edimax 8139", 0 ),
  892. PCI_ROM ( 0x14ea, 0xab06, "fnw3603tx", "Planex FNW-3603-TX", 0 ),
  893. PCI_ROM ( 0x14ea, 0xab07, "fnw3800tx", "Planex FNW-3800-TX", 0 ),
  894. PCI_ROM ( 0x1500, 0x1360, "delta8139", "Delta Electronics 8139", 0 ),
  895. PCI_ROM ( 0x16ec, 0x0116, "usr997902", "USR997902", 0 ),
  896. PCI_ROM ( 0x1737, 0x1032, "linksys8169","Linksys 8169", 0 ),
  897. PCI_ROM ( 0x1743, 0x8139, "rolf100", "Peppercorn ROL/F-100", 0 ),
  898. PCI_ROM ( 0x4033, 0x1360, "addron8139", "Addtron 8139", 0 ),
  899. PCI_ROM ( 0xffff, 0x8139, "clonse8139", "Cloned 8139", 0 ),
  900. };
  901. /** Realtek PCI driver */
  902. struct pci_driver realtek_driver __pci_driver = {
  903. .ids = realtek_nics,
  904. .id_count = ( sizeof ( realtek_nics ) / sizeof ( realtek_nics[0] ) ),
  905. .probe = realtek_probe,
  906. .remove = realtek_remove,
  907. };