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.

intel.c 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
  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 "intel.h"
  32. /** @file
  33. *
  34. * Intel 10/100/1000 network card driver
  35. *
  36. */
  37. /******************************************************************************
  38. *
  39. * EEPROM interface
  40. *
  41. ******************************************************************************
  42. */
  43. /**
  44. * Read data from EEPROM
  45. *
  46. * @v nvs NVS device
  47. * @v address Address from which to read
  48. * @v data Data buffer
  49. * @v len Length of data buffer
  50. * @ret rc Return status code
  51. */
  52. static int intel_read_eeprom ( struct nvs_device *nvs, unsigned int address,
  53. void *data, size_t len ) {
  54. struct intel_nic *intel =
  55. container_of ( nvs, struct intel_nic, eeprom );
  56. unsigned int i;
  57. uint32_t value;
  58. uint16_t *data_word = data;
  59. /* Sanity check. We advertise a blocksize of one word, so
  60. * should only ever receive single-word requests.
  61. */
  62. assert ( len == sizeof ( *data_word ) );
  63. /* Initiate read */
  64. writel ( ( INTEL_EERD_START | ( address << intel->eerd_addr_shift ) ),
  65. intel->regs + INTEL_EERD );
  66. /* Wait for read to complete */
  67. for ( i = 0 ; i < INTEL_EEPROM_MAX_WAIT_MS ; i++ ) {
  68. /* If read is not complete, delay 1ms and retry */
  69. value = readl ( intel->regs + INTEL_EERD );
  70. if ( ! ( value & intel->eerd_done ) ) {
  71. mdelay ( 1 );
  72. continue;
  73. }
  74. /* Extract data */
  75. *data_word = cpu_to_le16 ( INTEL_EERD_DATA ( value ) );
  76. return 0;
  77. }
  78. DBGC ( intel, "INTEL %p timed out waiting for EEPROM read\n", intel );
  79. return -ETIMEDOUT;
  80. }
  81. /**
  82. * Write data to EEPROM
  83. *
  84. * @v nvs NVS device
  85. * @v address Address to which to write
  86. * @v data Data buffer
  87. * @v len Length of data buffer
  88. * @ret rc Return status code
  89. */
  90. static int intel_write_eeprom ( struct nvs_device *nvs,
  91. unsigned int address __unused,
  92. const void *data __unused,
  93. size_t len __unused ) {
  94. struct intel_nic *intel =
  95. container_of ( nvs, struct intel_nic, eeprom );
  96. DBGC ( intel, "INTEL %p EEPROM write not supported\n", intel );
  97. return -ENOTSUP;
  98. }
  99. /**
  100. * Initialise EEPROM
  101. *
  102. * @v intel Intel device
  103. * @ret rc Return status code
  104. */
  105. static int intel_init_eeprom ( struct intel_nic *intel ) {
  106. unsigned int i;
  107. uint32_t value;
  108. /* The NIC automatically detects the type of attached EEPROM.
  109. * The EERD register provides access to only a single word at
  110. * a time, so we pretend to have a single-word block size.
  111. *
  112. * The EEPROM size may be larger than the minimum size, but
  113. * this doesn't matter to us since we access only the first
  114. * few words.
  115. */
  116. intel->eeprom.word_len_log2 = INTEL_EEPROM_WORD_LEN_LOG2;
  117. intel->eeprom.size = INTEL_EEPROM_MIN_SIZE_WORDS;
  118. intel->eeprom.block_size = 1;
  119. intel->eeprom.read = intel_read_eeprom;
  120. intel->eeprom.write = intel_write_eeprom;
  121. /* The layout of the EERD register was changed at some point
  122. * to accommodate larger EEPROMs. Read from address zero (for
  123. * which the request layouts are compatible) to determine
  124. * which type of register we have.
  125. */
  126. writel ( INTEL_EERD_START, intel->regs + INTEL_EERD );
  127. for ( i = 0 ; i < INTEL_EEPROM_MAX_WAIT_MS ; i++ ) {
  128. value = readl ( intel->regs + INTEL_EERD );
  129. if ( value & INTEL_EERD_DONE_LARGE ) {
  130. DBGC ( intel, "INTEL %p has large-format EERD\n",
  131. intel );
  132. intel->eerd_done = INTEL_EERD_DONE_LARGE;
  133. intel->eerd_addr_shift = INTEL_EERD_ADDR_SHIFT_LARGE;
  134. return 0;
  135. }
  136. if ( value & INTEL_EERD_DONE_SMALL ) {
  137. DBGC ( intel, "INTEL %p has small-format EERD\n",
  138. intel );
  139. intel->eerd_done = INTEL_EERD_DONE_SMALL;
  140. intel->eerd_addr_shift = INTEL_EERD_ADDR_SHIFT_SMALL;
  141. return 0;
  142. }
  143. mdelay ( 1 );
  144. }
  145. DBGC ( intel, "INTEL %p timed out waiting for initial EEPROM read "
  146. "(value %08x)\n", intel, value );
  147. return -ETIMEDOUT;
  148. }
  149. /******************************************************************************
  150. *
  151. * MAC address
  152. *
  153. ******************************************************************************
  154. */
  155. /**
  156. * Fetch initial MAC address from EEPROM
  157. *
  158. * @v intel Intel device
  159. * @v hw_addr Hardware address to fill in
  160. * @ret rc Return status code
  161. */
  162. static int intel_fetch_mac_eeprom ( struct intel_nic *intel,
  163. uint8_t *hw_addr ) {
  164. int rc;
  165. /* Initialise EEPROM */
  166. if ( ( rc = intel_init_eeprom ( intel ) ) != 0 )
  167. return rc;
  168. /* Read base MAC address from EEPROM */
  169. if ( ( rc = nvs_read ( &intel->eeprom, INTEL_EEPROM_MAC,
  170. hw_addr, ETH_ALEN ) ) != 0 ) {
  171. DBGC ( intel, "INTEL %p could not read EEPROM base MAC "
  172. "address: %s\n", intel, strerror ( rc ) );
  173. return rc;
  174. }
  175. /* Adjust MAC address for multi-port devices */
  176. hw_addr[ETH_ALEN-1] ^= intel->port;
  177. DBGC ( intel, "INTEL %p has EEPROM MAC address %s (port %d)\n",
  178. intel, eth_ntoa ( hw_addr ), intel->port );
  179. return 0;
  180. }
  181. /**
  182. * Fetch initial MAC address
  183. *
  184. * @v intel Intel device
  185. * @v hw_addr Hardware address to fill in
  186. * @ret rc Return status code
  187. */
  188. static int intel_fetch_mac ( struct intel_nic *intel, uint8_t *hw_addr ) {
  189. union intel_receive_address mac;
  190. int rc;
  191. /* Read current address from RAL0/RAH0 */
  192. mac.reg.low = cpu_to_le32 ( readl ( intel->regs + INTEL_RAL0 ) );
  193. mac.reg.high = cpu_to_le32 ( readl ( intel->regs + INTEL_RAH0 ) );
  194. DBGC ( intel, "INTEL %p has autoloaded MAC address %s\n",
  195. intel, eth_ntoa ( mac.raw ) );
  196. /* Try to read address from EEPROM */
  197. if ( ( rc = intel_fetch_mac_eeprom ( intel, hw_addr ) ) == 0 )
  198. return 0;
  199. /* Use current address if valid */
  200. if ( is_valid_ether_addr ( mac.raw ) ) {
  201. memcpy ( hw_addr, mac.raw, ETH_ALEN );
  202. return 0;
  203. }
  204. DBGC ( intel, "INTEL %p has no MAC address to use\n", intel );
  205. return -ENOENT;
  206. }
  207. /******************************************************************************
  208. *
  209. * Diagnostics
  210. *
  211. ******************************************************************************
  212. */
  213. /**
  214. * Dump diagnostic information
  215. *
  216. * @v intel Intel device
  217. */
  218. static void __attribute__ (( unused )) intel_diag ( struct intel_nic *intel ) {
  219. DBGC ( intel, "INTEL %p TDH=%04x TDT=%04x RDH=%04x RDT=%04x\n", intel,
  220. readl ( intel->regs + INTEL_TDH ),
  221. readl ( intel->regs + INTEL_TDT ),
  222. readl ( intel->regs + INTEL_RDH ),
  223. readl ( intel->regs + INTEL_RDT ) );
  224. }
  225. /******************************************************************************
  226. *
  227. * Device reset
  228. *
  229. ******************************************************************************
  230. */
  231. /**
  232. * Reset hardware
  233. *
  234. * @v intel Intel device
  235. * @ret rc Return status code
  236. */
  237. static int intel_reset ( struct intel_nic *intel ) {
  238. uint32_t pbs;
  239. uint32_t ctrl;
  240. uint32_t status;
  241. /* Force RX and TX packet buffer allocation, to work around an
  242. * errata in ICH devices.
  243. */
  244. pbs = readl ( intel->regs + INTEL_PBS );
  245. if ( ( pbs == 0x14 ) || ( pbs == 0x18 ) ) {
  246. DBGC ( intel, "INTEL %p WARNING: applying ICH PBS/PBA errata\n",
  247. intel );
  248. writel ( 0x08, intel->regs + INTEL_PBA );
  249. writel ( 0x10, intel->regs + INTEL_PBS );
  250. }
  251. /* Always reset MAC. Required to reset the TX and RX rings. */
  252. ctrl = readl ( intel->regs + INTEL_CTRL );
  253. writel ( ( ctrl | INTEL_CTRL_RST ), intel->regs + INTEL_CTRL );
  254. mdelay ( INTEL_RESET_DELAY_MS );
  255. /* Set a sensible default configuration */
  256. ctrl |= ( INTEL_CTRL_SLU | INTEL_CTRL_ASDE );
  257. ctrl &= ~( INTEL_CTRL_LRST | INTEL_CTRL_FRCSPD | INTEL_CTRL_FRCDPLX );
  258. writel ( ctrl, intel->regs + INTEL_CTRL );
  259. mdelay ( INTEL_RESET_DELAY_MS );
  260. /* If link is already up, do not attempt to reset the PHY. On
  261. * some models (notably ICH), performing a PHY reset seems to
  262. * drop the link speed to 10Mbps.
  263. */
  264. status = readl ( intel->regs + INTEL_STATUS );
  265. if ( status & INTEL_STATUS_LU ) {
  266. DBGC ( intel, "INTEL %p MAC reset (ctrl %08x)\n",
  267. intel, ctrl );
  268. return 0;
  269. }
  270. /* Reset PHY and MAC simultaneously */
  271. writel ( ( ctrl | INTEL_CTRL_RST | INTEL_CTRL_PHY_RST ),
  272. intel->regs + INTEL_CTRL );
  273. mdelay ( INTEL_RESET_DELAY_MS );
  274. /* PHY reset is not self-clearing on all models */
  275. writel ( ctrl, intel->regs + INTEL_CTRL );
  276. mdelay ( INTEL_RESET_DELAY_MS );
  277. DBGC ( intel, "INTEL %p MAC+PHY reset (ctrl %08x)\n", intel, ctrl );
  278. return 0;
  279. }
  280. /******************************************************************************
  281. *
  282. * Link state
  283. *
  284. ******************************************************************************
  285. */
  286. /**
  287. * Check link state
  288. *
  289. * @v netdev Network device
  290. */
  291. static void intel_check_link ( struct net_device *netdev ) {
  292. struct intel_nic *intel = netdev->priv;
  293. uint32_t status;
  294. /* Read link status */
  295. status = readl ( intel->regs + INTEL_STATUS );
  296. DBGC ( intel, "INTEL %p link status is %08x\n", intel, status );
  297. /* Update network device */
  298. if ( status & INTEL_STATUS_LU ) {
  299. netdev_link_up ( netdev );
  300. } else {
  301. netdev_link_down ( netdev );
  302. }
  303. }
  304. /******************************************************************************
  305. *
  306. * Network device interface
  307. *
  308. ******************************************************************************
  309. */
  310. /**
  311. * Create descriptor ring
  312. *
  313. * @v intel Intel device
  314. * @v ring Descriptor ring
  315. * @ret rc Return status code
  316. */
  317. static int intel_create_ring ( struct intel_nic *intel,
  318. struct intel_ring *ring ) {
  319. physaddr_t address;
  320. uint32_t dctl;
  321. /* Allocate descriptor ring. Align ring on its own size to
  322. * prevent any possible page-crossing errors due to hardware
  323. * errata.
  324. */
  325. ring->desc = malloc_dma ( ring->len, ring->len );
  326. if ( ! ring->desc )
  327. return -ENOMEM;
  328. /* Initialise descriptor ring */
  329. memset ( ring->desc, 0, ring->len );
  330. /* Program ring address */
  331. address = virt_to_bus ( ring->desc );
  332. writel ( ( address & 0xffffffffUL ),
  333. ( intel->regs + ring->reg + INTEL_xDBAL ) );
  334. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
  335. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  336. ( intel->regs + ring->reg + INTEL_xDBAH ) );
  337. } else {
  338. writel ( 0, intel->regs + ring->reg + INTEL_xDBAH );
  339. }
  340. /* Program ring length */
  341. writel ( ring->len, ( intel->regs + ring->reg + INTEL_xDLEN ) );
  342. /* Reset head and tail pointers */
  343. writel ( 0, ( intel->regs + ring->reg + INTEL_xDH ) );
  344. writel ( 0, ( intel->regs + ring->reg + INTEL_xDT ) );
  345. /* Enable ring */
  346. dctl = readl ( intel->regs + ring->reg + INTEL_xDCTL );
  347. dctl |= INTEL_xDCTL_ENABLE;
  348. writel ( dctl, intel->regs + ring->reg + INTEL_xDCTL );
  349. DBGC ( intel, "INTEL %p ring %05x is at [%08llx,%08llx)\n",
  350. intel, ring->reg, ( ( unsigned long long ) address ),
  351. ( ( unsigned long long ) address + ring->len ) );
  352. return 0;
  353. }
  354. /**
  355. * Destroy descriptor ring
  356. *
  357. * @v intel Intel device
  358. * @v ring Descriptor ring
  359. */
  360. static void intel_destroy_ring ( struct intel_nic *intel,
  361. struct intel_ring *ring ) {
  362. /* Clear ring length */
  363. writel ( 0, ( intel->regs + ring->reg + INTEL_xDLEN ) );
  364. /* Clear ring address */
  365. writel ( 0, ( intel->regs + ring->reg + INTEL_xDBAL ) );
  366. writel ( 0, ( intel->regs + ring->reg + INTEL_xDBAH ) );
  367. /* Free descriptor ring */
  368. free_dma ( ring->desc, ring->len );
  369. ring->desc = NULL;
  370. ring->prod = 0;
  371. ring->cons = 0;
  372. }
  373. /**
  374. * Refill receive descriptor ring
  375. *
  376. * @v intel Intel device
  377. */
  378. static void intel_refill_rx ( struct intel_nic *intel ) {
  379. struct intel_descriptor *rx;
  380. struct io_buffer *iobuf;
  381. unsigned int rx_idx;
  382. unsigned int rx_tail;
  383. physaddr_t address;
  384. while ( ( intel->rx.prod - intel->rx.cons ) < INTEL_RX_FILL ) {
  385. /* Allocate I/O buffer */
  386. iobuf = alloc_iob ( INTEL_RX_MAX_LEN );
  387. if ( ! iobuf ) {
  388. /* Wait for next refill */
  389. return;
  390. }
  391. /* Get next receive descriptor */
  392. rx_idx = ( intel->rx.prod++ % INTEL_NUM_RX_DESC );
  393. rx_tail = ( intel->rx.prod % INTEL_NUM_RX_DESC );
  394. rx = &intel->rx.desc[rx_idx];
  395. /* Populate receive descriptor */
  396. address = virt_to_bus ( iobuf->data );
  397. rx->address = cpu_to_le64 ( address );
  398. rx->length = 0;
  399. rx->status = 0;
  400. rx->errors = 0;
  401. wmb();
  402. /* Record I/O buffer */
  403. assert ( intel->rx_iobuf[rx_idx] == NULL );
  404. intel->rx_iobuf[rx_idx] = iobuf;
  405. /* Push descriptor to card */
  406. writel ( rx_tail, intel->regs + INTEL_RDT );
  407. DBGC2 ( intel, "INTEL %p RX %d is [%llx,%llx)\n", intel, rx_idx,
  408. ( ( unsigned long long ) address ),
  409. ( ( unsigned long long ) address + INTEL_RX_MAX_LEN ) );
  410. }
  411. }
  412. /**
  413. * Open network device
  414. *
  415. * @v netdev Network device
  416. * @ret rc Return status code
  417. */
  418. static int intel_open ( struct net_device *netdev ) {
  419. struct intel_nic *intel = netdev->priv;
  420. union intel_receive_address mac;
  421. uint32_t tctl;
  422. uint32_t rctl;
  423. int rc;
  424. /* Create transmit descriptor ring */
  425. if ( ( rc = intel_create_ring ( intel, &intel->tx ) ) != 0 )
  426. goto err_create_tx;
  427. /* Create receive descriptor ring */
  428. if ( ( rc = intel_create_ring ( intel, &intel->rx ) ) != 0 )
  429. goto err_create_rx;
  430. /* Program MAC address */
  431. memset ( &mac, 0, sizeof ( mac ) );
  432. memcpy ( mac.raw, netdev->ll_addr, sizeof ( mac.raw ) );
  433. writel ( le32_to_cpu ( mac.reg.low ), intel->regs + INTEL_RAL0 );
  434. writel ( ( le32_to_cpu ( mac.reg.high ) | INTEL_RAH0_AV ),
  435. intel->regs + INTEL_RAH0 );
  436. /* Enable transmitter */
  437. tctl = readl ( intel->regs + INTEL_TCTL );
  438. tctl &= ~( INTEL_TCTL_CT_MASK | INTEL_TCTL_COLD_MASK );
  439. tctl |= ( INTEL_TCTL_EN | INTEL_TCTL_PSP | INTEL_TCTL_CT_DEFAULT |
  440. INTEL_TCTL_COLD_DEFAULT );
  441. writel ( tctl, intel->regs + INTEL_TCTL );
  442. /* Enable receiver */
  443. rctl = readl ( intel->regs + INTEL_RCTL );
  444. rctl &= ~( INTEL_RCTL_BSIZE_BSEX_MASK );
  445. rctl |= ( INTEL_RCTL_EN | INTEL_RCTL_UPE | INTEL_RCTL_MPE |
  446. INTEL_RCTL_BAM | INTEL_RCTL_BSIZE_2048 | INTEL_RCTL_SECRC );
  447. writel ( rctl, intel->regs + INTEL_RCTL );
  448. /* Fill receive ring */
  449. intel_refill_rx ( intel );
  450. /* Update link state */
  451. intel_check_link ( netdev );
  452. return 0;
  453. intel_destroy_ring ( intel, &intel->rx );
  454. err_create_rx:
  455. intel_destroy_ring ( intel, &intel->tx );
  456. err_create_tx:
  457. return rc;
  458. }
  459. /**
  460. * Close network device
  461. *
  462. * @v netdev Network device
  463. */
  464. static void intel_close ( struct net_device *netdev ) {
  465. struct intel_nic *intel = netdev->priv;
  466. unsigned int i;
  467. /* Disable receiver */
  468. writel ( 0, intel->regs + INTEL_RCTL );
  469. /* Disable transmitter */
  470. writel ( 0, intel->regs + INTEL_TCTL );
  471. /* Destroy receive descriptor ring */
  472. intel_destroy_ring ( intel, &intel->rx );
  473. /* Discard any unused receive buffers */
  474. for ( i = 0 ; i < INTEL_NUM_RX_DESC ; i++ ) {
  475. if ( intel->rx_iobuf[i] )
  476. free_iob ( intel->rx_iobuf[i] );
  477. intel->rx_iobuf[i] = NULL;
  478. }
  479. /* Destroy transmit descriptor ring */
  480. intel_destroy_ring ( intel, &intel->tx );
  481. /* Reset the NIC, to flush the transmit and receive FIFOs */
  482. intel_reset ( intel );
  483. }
  484. /**
  485. * Transmit packet
  486. *
  487. * @v netdev Network device
  488. * @v iobuf I/O buffer
  489. * @ret rc Return status code
  490. */
  491. static int intel_transmit ( struct net_device *netdev,
  492. struct io_buffer *iobuf ) {
  493. struct intel_nic *intel = netdev->priv;
  494. struct intel_descriptor *tx;
  495. unsigned int tx_idx;
  496. unsigned int tx_tail;
  497. physaddr_t address;
  498. /* Get next transmit descriptor */
  499. if ( ( intel->tx.prod - intel->tx.cons ) >= INTEL_NUM_TX_DESC ) {
  500. DBGC ( intel, "INTEL %p out of transmit descriptors\n", intel );
  501. return -ENOBUFS;
  502. }
  503. tx_idx = ( intel->tx.prod++ % INTEL_NUM_TX_DESC );
  504. tx_tail = ( intel->tx.prod % INTEL_NUM_TX_DESC );
  505. tx = &intel->tx.desc[tx_idx];
  506. /* Populate transmit descriptor */
  507. address = virt_to_bus ( iobuf->data );
  508. tx->address = cpu_to_le64 ( address );
  509. tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
  510. tx->command = ( INTEL_DESC_CMD_RS | INTEL_DESC_CMD_IFCS |
  511. INTEL_DESC_CMD_EOP );
  512. tx->status = 0;
  513. wmb();
  514. /* Notify card that there are packets ready to transmit */
  515. writel ( tx_tail, intel->regs + INTEL_TDT );
  516. DBGC2 ( intel, "INTEL %p TX %d is [%llx,%llx)\n", intel, tx_idx,
  517. ( ( unsigned long long ) address ),
  518. ( ( unsigned long long ) address + iob_len ( iobuf ) ) );
  519. return 0;
  520. }
  521. /**
  522. * Poll for completed packets
  523. *
  524. * @v netdev Network device
  525. */
  526. static void intel_poll_tx ( struct net_device *netdev ) {
  527. struct intel_nic *intel = netdev->priv;
  528. struct intel_descriptor *tx;
  529. unsigned int tx_idx;
  530. /* Check for completed packets */
  531. while ( intel->tx.cons != intel->tx.prod ) {
  532. /* Get next transmit descriptor */
  533. tx_idx = ( intel->tx.cons % INTEL_NUM_TX_DESC );
  534. tx = &intel->tx.desc[tx_idx];
  535. /* Stop if descriptor is still in use */
  536. if ( ! ( tx->status & INTEL_DESC_STATUS_DD ) )
  537. return;
  538. DBGC2 ( intel, "INTEL %p TX %d complete\n", intel, tx_idx );
  539. /* Complete TX descriptor */
  540. netdev_tx_complete_next ( netdev );
  541. intel->tx.cons++;
  542. }
  543. }
  544. /**
  545. * Poll for received packets
  546. *
  547. * @v netdev Network device
  548. */
  549. static void intel_poll_rx ( struct net_device *netdev ) {
  550. struct intel_nic *intel = netdev->priv;
  551. struct intel_descriptor *rx;
  552. struct io_buffer *iobuf;
  553. unsigned int rx_idx;
  554. size_t len;
  555. /* Check for received packets */
  556. while ( intel->rx.cons != intel->rx.prod ) {
  557. /* Get next receive descriptor */
  558. rx_idx = ( intel->rx.cons % INTEL_NUM_RX_DESC );
  559. rx = &intel->rx.desc[rx_idx];
  560. /* Stop if descriptor is still in use */
  561. if ( ! ( rx->status & INTEL_DESC_STATUS_DD ) )
  562. return;
  563. /* Populate I/O buffer */
  564. iobuf = intel->rx_iobuf[rx_idx];
  565. intel->rx_iobuf[rx_idx] = NULL;
  566. len = le16_to_cpu ( rx->length );
  567. iob_put ( iobuf, len );
  568. /* Hand off to network stack */
  569. if ( rx->errors ) {
  570. DBGC ( intel, "INTEL %p RX %d error (length %zd, "
  571. "errors %02x)\n",
  572. intel, rx_idx, len, rx->errors );
  573. netdev_rx_err ( netdev, iobuf, -EIO );
  574. } else {
  575. DBGC2 ( intel, "INTEL %p RX %d complete (length %zd)\n",
  576. intel, rx_idx, len );
  577. netdev_rx ( netdev, iobuf );
  578. }
  579. intel->rx.cons++;
  580. }
  581. }
  582. /**
  583. * Poll for completed and received packets
  584. *
  585. * @v netdev Network device
  586. */
  587. static void intel_poll ( struct net_device *netdev ) {
  588. struct intel_nic *intel = netdev->priv;
  589. uint32_t icr;
  590. /* Check for and acknowledge interrupts */
  591. icr = readl ( intel->regs + INTEL_ICR );
  592. if ( ! icr )
  593. return;
  594. /* Poll for TX completions, if applicable */
  595. if ( icr & INTEL_IRQ_TXDW )
  596. intel_poll_tx ( netdev );
  597. /* Poll for RX completionsm, if applicable */
  598. if ( icr & INTEL_IRQ_RXT0 )
  599. intel_poll_rx ( netdev );
  600. /* Check link state, if applicable */
  601. if ( icr & INTEL_IRQ_LSC )
  602. intel_check_link ( netdev );
  603. /* Refill RX ring */
  604. intel_refill_rx ( intel );
  605. }
  606. /**
  607. * Enable or disable interrupts
  608. *
  609. * @v netdev Network device
  610. * @v enable Interrupts should be enabled
  611. */
  612. static void intel_irq ( struct net_device *netdev, int enable ) {
  613. struct intel_nic *intel = netdev->priv;
  614. uint32_t mask;
  615. mask = ( INTEL_IRQ_TXDW | INTEL_IRQ_LSC | INTEL_IRQ_RXT0 );
  616. if ( enable ) {
  617. writel ( mask, intel->regs + INTEL_IMS );
  618. } else {
  619. writel ( mask, intel->regs + INTEL_IMC );
  620. }
  621. }
  622. /** Intel network device operations */
  623. static struct net_device_operations intel_operations = {
  624. .open = intel_open,
  625. .close = intel_close,
  626. .transmit = intel_transmit,
  627. .poll = intel_poll,
  628. .irq = intel_irq,
  629. };
  630. /******************************************************************************
  631. *
  632. * PCI interface
  633. *
  634. ******************************************************************************
  635. */
  636. /**
  637. * Probe PCI device
  638. *
  639. * @v pci PCI device
  640. * @ret rc Return status code
  641. */
  642. static int intel_probe ( struct pci_device *pci ) {
  643. struct net_device *netdev;
  644. struct intel_nic *intel;
  645. int rc;
  646. /* Allocate and initialise net device */
  647. netdev = alloc_etherdev ( sizeof ( *intel ) );
  648. if ( ! netdev ) {
  649. rc = -ENOMEM;
  650. goto err_alloc;
  651. }
  652. netdev_init ( netdev, &intel_operations );
  653. intel = netdev->priv;
  654. pci_set_drvdata ( pci, netdev );
  655. netdev->dev = &pci->dev;
  656. memset ( intel, 0, sizeof ( *intel ) );
  657. intel->port = PCI_FUNC ( pci->busdevfn );
  658. intel_init_ring ( &intel->tx, INTEL_NUM_TX_DESC, INTEL_TD );
  659. intel_init_ring ( &intel->rx, INTEL_NUM_RX_DESC, INTEL_RD );
  660. /* Fix up PCI device */
  661. adjust_pci_device ( pci );
  662. /* Map registers */
  663. intel->regs = ioremap ( pci->membase, INTEL_BAR_SIZE );
  664. /* Reset the NIC */
  665. if ( ( rc = intel_reset ( intel ) ) != 0 )
  666. goto err_reset;
  667. /* Fetch MAC address */
  668. if ( ( rc = intel_fetch_mac ( intel, netdev->hw_addr ) ) != 0 )
  669. goto err_fetch_mac;
  670. /* Register network device */
  671. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  672. goto err_register_netdev;
  673. /* Set initial link state */
  674. intel_check_link ( netdev );
  675. return 0;
  676. unregister_netdev ( netdev );
  677. err_register_netdev:
  678. err_fetch_mac:
  679. intel_reset ( intel );
  680. err_reset:
  681. netdev_nullify ( netdev );
  682. netdev_put ( netdev );
  683. err_alloc:
  684. return rc;
  685. }
  686. /**
  687. * Remove PCI device
  688. *
  689. * @v pci PCI device
  690. */
  691. static void intel_remove ( struct pci_device *pci ) {
  692. struct net_device *netdev = pci_get_drvdata ( pci );
  693. struct intel_nic *intel = netdev->priv;
  694. /* Unregister network device */
  695. unregister_netdev ( netdev );
  696. /* Reset the NIC */
  697. intel_reset ( intel );
  698. /* Free network device */
  699. netdev_nullify ( netdev );
  700. netdev_put ( netdev );
  701. }
  702. /** Intel PCI device IDs */
  703. static struct pci_device_id intel_nics[] = {
  704. PCI_ROM ( 0x8086, 0x0438, "dh8900cc", "DH8900CC", 0 ),
  705. PCI_ROM ( 0x8086, 0x043a, "dh8900cc-f", "DH8900CC Fiber", 0 ),
  706. PCI_ROM ( 0x8086, 0x043c, "dh8900cc-b", "DH8900CC Backplane", 0 ),
  707. PCI_ROM ( 0x8086, 0x0440, "dh8900cc-s", "DH8900CC SFP", 0 ),
  708. PCI_ROM ( 0x8086, 0x1000, "82542-f", "82542 (Fiber)", 0 ),
  709. PCI_ROM ( 0x8086, 0x1001, "82543gc-f", "82543GC (Fiber)", 0 ),
  710. PCI_ROM ( 0x8086, 0x1004, "82543gc", "82543GC (Copper)", 0 ),
  711. PCI_ROM ( 0x8086, 0x1008, "82544ei", "82544EI (Copper)", 0 ),
  712. PCI_ROM ( 0x8086, 0x1009, "82544ei-f", "82544EI (Fiber)", 0 ),
  713. PCI_ROM ( 0x8086, 0x100c, "82544gc", "82544GC (Copper)", 0 ),
  714. PCI_ROM ( 0x8086, 0x100d, "82544gc-l", "82544GC (LOM)", 0 ),
  715. PCI_ROM ( 0x8086, 0x100e, "82540em", "82540EM", 0 ),
  716. PCI_ROM ( 0x8086, 0x100f, "82545em", "82545EM (Copper)", 0 ),
  717. PCI_ROM ( 0x8086, 0x1010, "82546eb", "82546EB (Copper)", 0 ),
  718. PCI_ROM ( 0x8086, 0x1011, "82545em-f", "82545EM (Fiber)", 0 ),
  719. PCI_ROM ( 0x8086, 0x1012, "82546eb-f", "82546EB (Fiber)", 0 ),
  720. PCI_ROM ( 0x8086, 0x1013, "82541ei", "82541EI", 0 ),
  721. PCI_ROM ( 0x8086, 0x1014, "82541er", "82541ER", 0 ),
  722. PCI_ROM ( 0x8086, 0x1015, "82540em-l", "82540EM (LOM)", 0 ),
  723. PCI_ROM ( 0x8086, 0x1016, "82540ep-m", "82540EP (Mobile)", 0 ),
  724. PCI_ROM ( 0x8086, 0x1017, "82540ep", "82540EP", 0 ),
  725. PCI_ROM ( 0x8086, 0x1018, "82541ei", "82541EI", 0 ),
  726. PCI_ROM ( 0x8086, 0x1019, "82547ei", "82547EI", 0 ),
  727. PCI_ROM ( 0x8086, 0x101a, "82547ei-m", "82547EI (Mobile)", 0 ),
  728. PCI_ROM ( 0x8086, 0x101d, "82546eb", "82546EB", 0 ),
  729. PCI_ROM ( 0x8086, 0x101e, "82540ep-m", "82540EP (Mobile)", 0 ),
  730. PCI_ROM ( 0x8086, 0x1026, "82545gm", "82545GM", 0 ),
  731. PCI_ROM ( 0x8086, 0x1027, "82545gm-1", "82545GM", 0 ),
  732. PCI_ROM ( 0x8086, 0x1028, "82545gm-2", "82545GM", 0 ),
  733. PCI_ROM ( 0x8086, 0x1049, "82566mm", "82566MM", 0 ),
  734. PCI_ROM ( 0x8086, 0x104a, "82566dm", "82566DM", 0 ),
  735. PCI_ROM ( 0x8086, 0x104b, "82566dc", "82566DC", 0 ),
  736. PCI_ROM ( 0x8086, 0x104c, "82562v", "82562V 10/100", 0 ),
  737. PCI_ROM ( 0x8086, 0x104d, "82566mc", "82566MC", 0 ),
  738. PCI_ROM ( 0x8086, 0x105e, "82571eb", "82571EB", 0 ),
  739. PCI_ROM ( 0x8086, 0x105f, "82571eb-1", "82571EB", 0 ),
  740. PCI_ROM ( 0x8086, 0x1060, "82571eb-2", "82571EB", 0 ),
  741. PCI_ROM ( 0x8086, 0x1075, "82547gi", "82547GI", 0 ),
  742. PCI_ROM ( 0x8086, 0x1076, "82541gi", "82541GI", 0 ),
  743. PCI_ROM ( 0x8086, 0x1077, "82541gi-1", "82541GI", 0 ),
  744. PCI_ROM ( 0x8086, 0x1078, "82541er", "82541ER", 0 ),
  745. PCI_ROM ( 0x8086, 0x1079, "82546gb", "82546GB", 0 ),
  746. PCI_ROM ( 0x8086, 0x107a, "82546gb-1", "82546GB", 0 ),
  747. PCI_ROM ( 0x8086, 0x107b, "82546gb-2", "82546GB", 0 ),
  748. PCI_ROM ( 0x8086, 0x107c, "82541pi", "82541PI", 0 ),
  749. PCI_ROM ( 0x8086, 0x107d, "82572ei", "82572EI (Copper)", 0 ),
  750. PCI_ROM ( 0x8086, 0x107e, "82572ei-f", "82572EI (Fiber)", 0 ),
  751. PCI_ROM ( 0x8086, 0x107f, "82572ei", "82572EI", 0 ),
  752. PCI_ROM ( 0x8086, 0x108a, "82546gb-3", "82546GB", 0 ),
  753. PCI_ROM ( 0x8086, 0x108b, "82573v", "82573V (Copper)", 0 ),
  754. PCI_ROM ( 0x8086, 0x108c, "82573e", "82573E (Copper)", 0 ),
  755. PCI_ROM ( 0x8086, 0x1096, "80003es2lan", "80003ES2LAN (Copper)", 0 ),
  756. PCI_ROM ( 0x8086, 0x1098, "80003es2lan-s", "80003ES2LAN (Serdes)", 0 ),
  757. PCI_ROM ( 0x8086, 0x1099, "82546gb-4", "82546GB (Copper)", 0 ),
  758. PCI_ROM ( 0x8086, 0x109a, "82573l", "82573L", 0 ),
  759. PCI_ROM ( 0x8086, 0x10a4, "82571eb", "82571EB", 0 ),
  760. PCI_ROM ( 0x8086, 0x10a5, "82571eb", "82571EB (Fiber)", 0 ),
  761. PCI_ROM ( 0x8086, 0x10a7, "82575eb", "82575EB", 0 ),
  762. PCI_ROM ( 0x8086, 0x10a9, "82575eb", "82575EB Backplane", 0 ),
  763. PCI_ROM ( 0x8086, 0x10b5, "82546gb", "82546GB (Copper)", 0 ),
  764. PCI_ROM ( 0x8086, 0x10b9, "82572ei", "82572EI (Copper)", 0 ),
  765. PCI_ROM ( 0x8086, 0x10ba, "80003es2lan", "80003ES2LAN (Copper)", 0 ),
  766. PCI_ROM ( 0x8086, 0x10bb, "80003es2lan", "80003ES2LAN (Serdes)", 0 ),
  767. PCI_ROM ( 0x8086, 0x10bc, "82571eb", "82571EB (Copper)", 0 ),
  768. PCI_ROM ( 0x8086, 0x10bd, "82566dm-2", "82566DM-2", 0 ),
  769. PCI_ROM ( 0x8086, 0x10bf, "82567lf", "82567LF", 0 ),
  770. PCI_ROM ( 0x8086, 0x10c0, "82562v-2", "82562V-2 10/100", 0 ),
  771. PCI_ROM ( 0x8086, 0x10c2, "82562g-2", "82562G-2 10/100", 0 ),
  772. PCI_ROM ( 0x8086, 0x10c3, "82562gt-2", "82562GT-2 10/100", 0 ),
  773. PCI_ROM ( 0x8086, 0x10c4, "82562gt", "82562GT 10/100", 0 ),
  774. PCI_ROM ( 0x8086, 0x10c5, "82562g", "82562G 10/100", 0 ),
  775. PCI_ROM ( 0x8086, 0x10c9, "82576", "82576", 0 ),
  776. PCI_ROM ( 0x8086, 0x10cb, "82567v", "82567V", 0 ),
  777. PCI_ROM ( 0x8086, 0x10cc, "82567lm-2", "82567LM-2", 0 ),
  778. PCI_ROM ( 0x8086, 0x10cd, "82567lf-2", "82567LF-2", 0 ),
  779. PCI_ROM ( 0x8086, 0x10ce, "82567v-2", "82567V-2", 0 ),
  780. PCI_ROM ( 0x8086, 0x10d3, "82574l", "82574L", 0 ),
  781. PCI_ROM ( 0x8086, 0x10d5, "82571pt", "82571PT PT Quad", 0 ),
  782. PCI_ROM ( 0x8086, 0x10d6, "82575gb", "82575GB", 0 ),
  783. PCI_ROM ( 0x8086, 0x10d9, "82571eb-d", "82571EB Dual Mezzanine", 0 ),
  784. PCI_ROM ( 0x8086, 0x10da, "82571eb-q", "82571EB Quad Mezzanine", 0 ),
  785. PCI_ROM ( 0x8086, 0x10de, "82567lm-3", "82567LM-3", 0 ),
  786. PCI_ROM ( 0x8086, 0x10df, "82567lf-3", "82567LF-3", 0 ),
  787. PCI_ROM ( 0x8086, 0x10e5, "82567lm-4", "82567LM-4", 0 ),
  788. PCI_ROM ( 0x8086, 0x10e6, "82576", "82576", 0 ),
  789. PCI_ROM ( 0x8086, 0x10e7, "82576-2", "82576", 0 ),
  790. PCI_ROM ( 0x8086, 0x10e8, "82576-3", "82576", 0 ),
  791. PCI_ROM ( 0x8086, 0x10ea, "82577lm", "82577LM", 0 ),
  792. PCI_ROM ( 0x8086, 0x10eb, "82577lc", "82577LC", 0 ),
  793. PCI_ROM ( 0x8086, 0x10ef, "82578dm", "82578DM", 0 ),
  794. PCI_ROM ( 0x8086, 0x10f0, "82578dc", "82578DC", 0 ),
  795. PCI_ROM ( 0x8086, 0x10f5, "82567lm", "82567LM", 0 ),
  796. PCI_ROM ( 0x8086, 0x10f6, "82574l", "82574L", 0 ),
  797. PCI_ROM ( 0x8086, 0x1501, "82567v-3", "82567V-3", 0 ),
  798. PCI_ROM ( 0x8086, 0x1502, "82579lm", "82579LM", 0 ),
  799. PCI_ROM ( 0x8086, 0x1503, "82579v", "82579V", 0 ),
  800. PCI_ROM ( 0x8086, 0x150a, "82576ns", "82576NS", 0 ),
  801. PCI_ROM ( 0x8086, 0x150c, "82583v", "82583V", 0 ),
  802. PCI_ROM ( 0x8086, 0x150d, "82576-4", "82576 Backplane", 0 ),
  803. PCI_ROM ( 0x8086, 0x150e, "82580", "82580", 0 ),
  804. PCI_ROM ( 0x8086, 0x150f, "82580-f", "82580 Fiber", 0 ),
  805. PCI_ROM ( 0x8086, 0x1510, "82580-b", "82580 Backplane", 0 ),
  806. PCI_ROM ( 0x8086, 0x1511, "82580-s", "82580 SFP", 0 ),
  807. PCI_ROM ( 0x8086, 0x1516, "82580-2", "82580", 0 ),
  808. PCI_ROM ( 0x8086, 0x1518, "82576ns", "82576NS SerDes", 0 ),
  809. PCI_ROM ( 0x8086, 0x1521, "i350", "I350", 0 ),
  810. PCI_ROM ( 0x8086, 0x1522, "i350-f", "I350 Fiber", 0 ),
  811. PCI_ROM ( 0x8086, 0x1523, "i350-b", "I350 Backplane", 0 ),
  812. PCI_ROM ( 0x8086, 0x1524, "i350-2", "I350", 0 ),
  813. PCI_ROM ( 0x8086, 0x1525, "82567v-4", "82567V-4", 0 ),
  814. PCI_ROM ( 0x8086, 0x1526, "82576-5", "82576", 0 ),
  815. PCI_ROM ( 0x8086, 0x1527, "82580-f2", "82580 Fiber", 0 ),
  816. PCI_ROM ( 0x8086, 0x294c, "82566dc-2", "82566DC-2", 0 ),
  817. PCI_ROM ( 0x8086, 0x2e6e, "cemedia", "CE Media Processor", 0 ),
  818. };
  819. /** Intel PCI driver */
  820. struct pci_driver intel_driver __pci_driver = {
  821. .ids = intel_nics,
  822. .id_count = ( sizeof ( intel_nics ) / sizeof ( intel_nics[0] ) ),
  823. .probe = intel_probe,
  824. .remove = intel_remove,
  825. };