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 33KB

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