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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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 TX %04x(%02x)/%04x(%02x) "
  220. "RX %04x(%02x)/%04x(%02x)\n", intel,
  221. ( intel->tx.cons & 0xffff ),
  222. readl ( intel->regs + intel->tx.reg + INTEL_xDH ),
  223. ( intel->tx.prod & 0xffff ),
  224. readl ( intel->regs + intel->tx.reg + INTEL_xDT ),
  225. ( intel->rx.cons & 0xffff ),
  226. readl ( intel->regs + intel->rx.reg + INTEL_xDH ),
  227. ( intel->rx.prod & 0xffff ),
  228. readl ( intel->regs + intel->rx.reg + INTEL_xDT ) );
  229. }
  230. /******************************************************************************
  231. *
  232. * Device reset
  233. *
  234. ******************************************************************************
  235. */
  236. /**
  237. * Reset hardware
  238. *
  239. * @v intel Intel device
  240. * @ret rc Return status code
  241. */
  242. static int intel_reset ( struct intel_nic *intel ) {
  243. uint32_t pbs;
  244. uint32_t ctrl;
  245. uint32_t status;
  246. /* Force RX and TX packet buffer allocation, to work around an
  247. * errata in ICH devices.
  248. */
  249. pbs = readl ( intel->regs + INTEL_PBS );
  250. if ( ( pbs == 0x14 ) || ( pbs == 0x18 ) ) {
  251. DBGC ( intel, "INTEL %p WARNING: applying ICH PBS/PBA errata\n",
  252. intel );
  253. writel ( 0x08, intel->regs + INTEL_PBA );
  254. writel ( 0x10, intel->regs + INTEL_PBS );
  255. }
  256. /* Always reset MAC. Required to reset the TX and RX rings. */
  257. ctrl = readl ( intel->regs + INTEL_CTRL );
  258. writel ( ( ctrl | INTEL_CTRL_RST ), intel->regs + INTEL_CTRL );
  259. mdelay ( INTEL_RESET_DELAY_MS );
  260. /* Set a sensible default configuration */
  261. ctrl |= ( INTEL_CTRL_SLU | INTEL_CTRL_ASDE );
  262. ctrl &= ~( INTEL_CTRL_LRST | INTEL_CTRL_FRCSPD | INTEL_CTRL_FRCDPLX );
  263. writel ( ctrl, intel->regs + INTEL_CTRL );
  264. mdelay ( INTEL_RESET_DELAY_MS );
  265. /* If link is already up, do not attempt to reset the PHY. On
  266. * some models (notably ICH), performing a PHY reset seems to
  267. * drop the link speed to 10Mbps.
  268. */
  269. status = readl ( intel->regs + INTEL_STATUS );
  270. if ( status & INTEL_STATUS_LU ) {
  271. DBGC ( intel, "INTEL %p MAC reset (ctrl %08x)\n",
  272. intel, ctrl );
  273. return 0;
  274. }
  275. /* Reset PHY and MAC simultaneously */
  276. writel ( ( ctrl | INTEL_CTRL_RST | INTEL_CTRL_PHY_RST ),
  277. intel->regs + INTEL_CTRL );
  278. mdelay ( INTEL_RESET_DELAY_MS );
  279. /* PHY reset is not self-clearing on all models */
  280. writel ( ctrl, intel->regs + INTEL_CTRL );
  281. mdelay ( INTEL_RESET_DELAY_MS );
  282. DBGC ( intel, "INTEL %p MAC+PHY reset (ctrl %08x)\n", intel, ctrl );
  283. return 0;
  284. }
  285. /******************************************************************************
  286. *
  287. * Link state
  288. *
  289. ******************************************************************************
  290. */
  291. /**
  292. * Check link state
  293. *
  294. * @v netdev Network device
  295. */
  296. static void intel_check_link ( struct net_device *netdev ) {
  297. struct intel_nic *intel = netdev->priv;
  298. uint32_t status;
  299. /* Read link status */
  300. status = readl ( intel->regs + INTEL_STATUS );
  301. DBGC ( intel, "INTEL %p link status is %08x\n", intel, status );
  302. /* Update network device */
  303. if ( status & INTEL_STATUS_LU ) {
  304. netdev_link_up ( netdev );
  305. } else {
  306. netdev_link_down ( netdev );
  307. }
  308. }
  309. /******************************************************************************
  310. *
  311. * Network device interface
  312. *
  313. ******************************************************************************
  314. */
  315. /**
  316. * Create descriptor ring
  317. *
  318. * @v intel Intel device
  319. * @v ring Descriptor ring
  320. * @ret rc Return status code
  321. */
  322. int intel_create_ring ( struct intel_nic *intel, struct intel_ring *ring ) {
  323. physaddr_t address;
  324. uint32_t dctl;
  325. /* Allocate descriptor ring. Align ring on its own size to
  326. * prevent any possible page-crossing errors due to hardware
  327. * errata.
  328. */
  329. ring->desc = malloc_dma ( ring->len, ring->len );
  330. if ( ! ring->desc )
  331. return -ENOMEM;
  332. /* Initialise descriptor ring */
  333. memset ( ring->desc, 0, ring->len );
  334. /* Program ring address */
  335. address = virt_to_bus ( ring->desc );
  336. writel ( ( address & 0xffffffffUL ),
  337. ( intel->regs + ring->reg + INTEL_xDBAL ) );
  338. if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
  339. writel ( ( ( ( uint64_t ) address ) >> 32 ),
  340. ( intel->regs + ring->reg + INTEL_xDBAH ) );
  341. } else {
  342. writel ( 0, intel->regs + ring->reg + INTEL_xDBAH );
  343. }
  344. /* Program ring length */
  345. writel ( ring->len, ( intel->regs + ring->reg + INTEL_xDLEN ) );
  346. /* Reset head and tail pointers */
  347. writel ( 0, ( intel->regs + ring->reg + INTEL_xDH ) );
  348. writel ( 0, ( intel->regs + ring->reg + INTEL_xDT ) );
  349. /* Enable ring */
  350. dctl = readl ( intel->regs + ring->reg + INTEL_xDCTL );
  351. dctl |= INTEL_xDCTL_ENABLE;
  352. writel ( dctl, intel->regs + ring->reg + INTEL_xDCTL );
  353. DBGC ( intel, "INTEL %p ring %05x is at [%08llx,%08llx)\n",
  354. intel, ring->reg, ( ( unsigned long long ) address ),
  355. ( ( unsigned long long ) address + ring->len ) );
  356. return 0;
  357. }
  358. /**
  359. * Destroy descriptor ring
  360. *
  361. * @v intel Intel device
  362. * @v ring Descriptor ring
  363. */
  364. void intel_destroy_ring ( struct intel_nic *intel, struct intel_ring *ring ) {
  365. /* Clear ring length */
  366. writel ( 0, ( intel->regs + ring->reg + INTEL_xDLEN ) );
  367. /* Clear ring address */
  368. writel ( 0, ( intel->regs + ring->reg + INTEL_xDBAL ) );
  369. writel ( 0, ( intel->regs + ring->reg + INTEL_xDBAH ) );
  370. /* Free descriptor ring */
  371. free_dma ( ring->desc, ring->len );
  372. ring->desc = NULL;
  373. ring->prod = 0;
  374. ring->cons = 0;
  375. }
  376. /**
  377. * Refill receive descriptor ring
  378. *
  379. * @v intel Intel device
  380. */
  381. void intel_refill_rx ( struct intel_nic *intel ) {
  382. struct intel_descriptor *rx;
  383. struct io_buffer *iobuf;
  384. unsigned int rx_idx;
  385. unsigned int rx_tail;
  386. physaddr_t address;
  387. while ( ( intel->rx.prod - intel->rx.cons ) < INTEL_RX_FILL ) {
  388. /* Allocate I/O buffer */
  389. iobuf = alloc_iob ( INTEL_RX_MAX_LEN );
  390. if ( ! iobuf ) {
  391. /* Wait for next refill */
  392. return;
  393. }
  394. /* Get next receive descriptor */
  395. rx_idx = ( intel->rx.prod++ % INTEL_NUM_RX_DESC );
  396. rx_tail = ( intel->rx.prod % INTEL_NUM_RX_DESC );
  397. rx = &intel->rx.desc[rx_idx];
  398. /* Populate receive descriptor */
  399. address = virt_to_bus ( iobuf->data );
  400. rx->address = cpu_to_le64 ( address );
  401. rx->length = 0;
  402. rx->status = 0;
  403. rx->errors = 0;
  404. wmb();
  405. /* Record I/O buffer */
  406. assert ( intel->rx_iobuf[rx_idx] == NULL );
  407. intel->rx_iobuf[rx_idx] = iobuf;
  408. /* Push descriptor to card */
  409. writel ( rx_tail, intel->regs + intel->rx.reg + INTEL_xDT );
  410. DBGC2 ( intel, "INTEL %p RX %d is [%llx,%llx)\n", intel, rx_idx,
  411. ( ( unsigned long long ) address ),
  412. ( ( unsigned long long ) address + INTEL_RX_MAX_LEN ) );
  413. }
  414. }
  415. /**
  416. * Discard unused receive I/O buffers
  417. *
  418. * @v intel Intel device
  419. */
  420. void intel_empty_rx ( struct intel_nic *intel ) {
  421. unsigned int i;
  422. for ( i = 0 ; i < INTEL_NUM_RX_DESC ; i++ ) {
  423. if ( intel->rx_iobuf[i] )
  424. free_iob ( intel->rx_iobuf[i] );
  425. intel->rx_iobuf[i] = NULL;
  426. }
  427. }
  428. /**
  429. * Open network device
  430. *
  431. * @v netdev Network device
  432. * @ret rc Return status code
  433. */
  434. static int intel_open ( struct net_device *netdev ) {
  435. struct intel_nic *intel = netdev->priv;
  436. union intel_receive_address mac;
  437. uint32_t tctl;
  438. uint32_t rctl;
  439. int rc;
  440. /* Create transmit descriptor ring */
  441. if ( ( rc = intel_create_ring ( intel, &intel->tx ) ) != 0 )
  442. goto err_create_tx;
  443. /* Create receive descriptor ring */
  444. if ( ( rc = intel_create_ring ( intel, &intel->rx ) ) != 0 )
  445. goto err_create_rx;
  446. /* Program MAC address */
  447. memset ( &mac, 0, sizeof ( mac ) );
  448. memcpy ( mac.raw, netdev->ll_addr, sizeof ( mac.raw ) );
  449. writel ( le32_to_cpu ( mac.reg.low ), intel->regs + INTEL_RAL0 );
  450. writel ( ( le32_to_cpu ( mac.reg.high ) | INTEL_RAH0_AV ),
  451. intel->regs + INTEL_RAH0 );
  452. /* Enable transmitter */
  453. tctl = readl ( intel->regs + INTEL_TCTL );
  454. tctl &= ~( INTEL_TCTL_CT_MASK | INTEL_TCTL_COLD_MASK );
  455. tctl |= ( INTEL_TCTL_EN | INTEL_TCTL_PSP | INTEL_TCTL_CT_DEFAULT |
  456. INTEL_TCTL_COLD_DEFAULT );
  457. writel ( tctl, intel->regs + INTEL_TCTL );
  458. /* Enable receiver */
  459. rctl = readl ( intel->regs + INTEL_RCTL );
  460. rctl &= ~( INTEL_RCTL_BSIZE_BSEX_MASK );
  461. rctl |= ( INTEL_RCTL_EN | INTEL_RCTL_UPE | INTEL_RCTL_MPE |
  462. INTEL_RCTL_BAM | INTEL_RCTL_BSIZE_2048 | INTEL_RCTL_SECRC );
  463. writel ( rctl, intel->regs + INTEL_RCTL );
  464. /* Fill receive ring */
  465. intel_refill_rx ( intel );
  466. /* Update link state */
  467. intel_check_link ( netdev );
  468. return 0;
  469. intel_destroy_ring ( intel, &intel->rx );
  470. err_create_rx:
  471. intel_destroy_ring ( intel, &intel->tx );
  472. err_create_tx:
  473. return rc;
  474. }
  475. /**
  476. * Close network device
  477. *
  478. * @v netdev Network device
  479. */
  480. static void intel_close ( struct net_device *netdev ) {
  481. struct intel_nic *intel = netdev->priv;
  482. /* Disable receiver */
  483. writel ( 0, intel->regs + INTEL_RCTL );
  484. /* Disable transmitter */
  485. writel ( 0, intel->regs + INTEL_TCTL );
  486. /* Destroy receive descriptor ring */
  487. intel_destroy_ring ( intel, &intel->rx );
  488. /* Discard any unused receive buffers */
  489. intel_empty_rx ( intel );
  490. /* Destroy transmit descriptor ring */
  491. intel_destroy_ring ( intel, &intel->tx );
  492. /* Reset the NIC, to flush the transmit and receive FIFOs */
  493. intel_reset ( intel );
  494. }
  495. /**
  496. * Transmit packet
  497. *
  498. * @v netdev Network device
  499. * @v iobuf I/O buffer
  500. * @ret rc Return status code
  501. */
  502. int intel_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
  503. struct intel_nic *intel = netdev->priv;
  504. struct intel_descriptor *tx;
  505. unsigned int tx_idx;
  506. unsigned int tx_tail;
  507. physaddr_t address;
  508. /* Get next transmit descriptor */
  509. if ( ( intel->tx.prod - intel->tx.cons ) >= INTEL_NUM_TX_DESC ) {
  510. DBGC ( intel, "INTEL %p out of transmit descriptors\n", intel );
  511. return -ENOBUFS;
  512. }
  513. tx_idx = ( intel->tx.prod++ % INTEL_NUM_TX_DESC );
  514. tx_tail = ( intel->tx.prod % INTEL_NUM_TX_DESC );
  515. tx = &intel->tx.desc[tx_idx];
  516. /* Populate transmit descriptor */
  517. address = virt_to_bus ( iobuf->data );
  518. tx->address = cpu_to_le64 ( address );
  519. tx->length = cpu_to_le16 ( iob_len ( iobuf ) );
  520. tx->command = ( INTEL_DESC_CMD_RS | INTEL_DESC_CMD_IFCS |
  521. INTEL_DESC_CMD_EOP );
  522. tx->status = 0;
  523. wmb();
  524. /* Notify card that there are packets ready to transmit */
  525. writel ( tx_tail, intel->regs + intel->tx.reg + INTEL_xDT );
  526. DBGC2 ( intel, "INTEL %p TX %d is [%llx,%llx)\n", intel, tx_idx,
  527. ( ( unsigned long long ) address ),
  528. ( ( unsigned long long ) address + iob_len ( iobuf ) ) );
  529. return 0;
  530. }
  531. /**
  532. * Poll for completed packets
  533. *
  534. * @v netdev Network device
  535. */
  536. void intel_poll_tx ( struct net_device *netdev ) {
  537. struct intel_nic *intel = netdev->priv;
  538. struct intel_descriptor *tx;
  539. unsigned int tx_idx;
  540. /* Check for completed packets */
  541. while ( intel->tx.cons != intel->tx.prod ) {
  542. /* Get next transmit descriptor */
  543. tx_idx = ( intel->tx.cons % INTEL_NUM_TX_DESC );
  544. tx = &intel->tx.desc[tx_idx];
  545. /* Stop if descriptor is still in use */
  546. if ( ! ( tx->status & INTEL_DESC_STATUS_DD ) )
  547. return;
  548. DBGC2 ( intel, "INTEL %p TX %d complete\n", intel, tx_idx );
  549. /* Complete TX descriptor */
  550. netdev_tx_complete_next ( netdev );
  551. intel->tx.cons++;
  552. }
  553. }
  554. /**
  555. * Poll for received packets
  556. *
  557. * @v netdev Network device
  558. */
  559. void intel_poll_rx ( struct net_device *netdev ) {
  560. struct intel_nic *intel = netdev->priv;
  561. struct intel_descriptor *rx;
  562. struct io_buffer *iobuf;
  563. unsigned int rx_idx;
  564. size_t len;
  565. /* Check for received packets */
  566. while ( intel->rx.cons != intel->rx.prod ) {
  567. /* Get next receive descriptor */
  568. rx_idx = ( intel->rx.cons % INTEL_NUM_RX_DESC );
  569. rx = &intel->rx.desc[rx_idx];
  570. /* Stop if descriptor is still in use */
  571. if ( ! ( rx->status & INTEL_DESC_STATUS_DD ) )
  572. return;
  573. /* Populate I/O buffer */
  574. iobuf = intel->rx_iobuf[rx_idx];
  575. intel->rx_iobuf[rx_idx] = NULL;
  576. len = le16_to_cpu ( rx->length );
  577. iob_put ( iobuf, len );
  578. /* Hand off to network stack */
  579. if ( rx->errors ) {
  580. DBGC ( intel, "INTEL %p RX %d error (length %zd, "
  581. "errors %02x)\n",
  582. intel, rx_idx, len, rx->errors );
  583. netdev_rx_err ( netdev, iobuf, -EIO );
  584. } else {
  585. DBGC2 ( intel, "INTEL %p RX %d complete (length %zd)\n",
  586. intel, rx_idx, len );
  587. netdev_rx ( netdev, iobuf );
  588. }
  589. intel->rx.cons++;
  590. }
  591. }
  592. /**
  593. * Poll for completed and received packets
  594. *
  595. * @v netdev Network device
  596. */
  597. static void intel_poll ( struct net_device *netdev ) {
  598. struct intel_nic *intel = netdev->priv;
  599. uint32_t icr;
  600. /* Check for and acknowledge interrupts */
  601. icr = readl ( intel->regs + INTEL_ICR );
  602. if ( ! icr )
  603. return;
  604. /* Poll for TX completions, if applicable */
  605. if ( icr & INTEL_IRQ_TXDW )
  606. intel_poll_tx ( netdev );
  607. /* Poll for RX completions, if applicable */
  608. if ( icr & ( INTEL_IRQ_RXT0 | INTEL_IRQ_RXO ) )
  609. intel_poll_rx ( netdev );
  610. /* Report receive overruns */
  611. if ( icr & INTEL_IRQ_RXO )
  612. netdev_rx_err ( netdev, NULL, -ENOBUFS );
  613. /* Check link state, if applicable */
  614. if ( icr & INTEL_IRQ_LSC )
  615. intel_check_link ( netdev );
  616. /* Refill RX ring */
  617. intel_refill_rx ( intel );
  618. }
  619. /**
  620. * Enable or disable interrupts
  621. *
  622. * @v netdev Network device
  623. * @v enable Interrupts should be enabled
  624. */
  625. static void intel_irq ( struct net_device *netdev, int enable ) {
  626. struct intel_nic *intel = netdev->priv;
  627. uint32_t mask;
  628. mask = ( INTEL_IRQ_TXDW | INTEL_IRQ_LSC | INTEL_IRQ_RXT0 );
  629. if ( enable ) {
  630. writel ( mask, intel->regs + INTEL_IMS );
  631. } else {
  632. writel ( mask, intel->regs + INTEL_IMC );
  633. }
  634. }
  635. /** Intel network device operations */
  636. static struct net_device_operations intel_operations = {
  637. .open = intel_open,
  638. .close = intel_close,
  639. .transmit = intel_transmit,
  640. .poll = intel_poll,
  641. .irq = intel_irq,
  642. };
  643. /******************************************************************************
  644. *
  645. * PCI interface
  646. *
  647. ******************************************************************************
  648. */
  649. /**
  650. * Probe PCI device
  651. *
  652. * @v pci PCI device
  653. * @ret rc Return status code
  654. */
  655. static int intel_probe ( struct pci_device *pci ) {
  656. struct net_device *netdev;
  657. struct intel_nic *intel;
  658. int rc;
  659. /* Allocate and initialise net device */
  660. netdev = alloc_etherdev ( sizeof ( *intel ) );
  661. if ( ! netdev ) {
  662. rc = -ENOMEM;
  663. goto err_alloc;
  664. }
  665. netdev_init ( netdev, &intel_operations );
  666. intel = netdev->priv;
  667. pci_set_drvdata ( pci, netdev );
  668. netdev->dev = &pci->dev;
  669. memset ( intel, 0, sizeof ( *intel ) );
  670. intel->port = PCI_FUNC ( pci->busdevfn );
  671. intel_init_ring ( &intel->tx, INTEL_NUM_TX_DESC, INTEL_TD );
  672. intel_init_ring ( &intel->rx, INTEL_NUM_RX_DESC, INTEL_RD );
  673. /* Fix up PCI device */
  674. adjust_pci_device ( pci );
  675. /* Map registers */
  676. intel->regs = ioremap ( pci->membase, INTEL_BAR_SIZE );
  677. /* Reset the NIC */
  678. if ( ( rc = intel_reset ( intel ) ) != 0 )
  679. goto err_reset;
  680. /* Fetch MAC address */
  681. if ( ( rc = intel_fetch_mac ( intel, netdev->hw_addr ) ) != 0 )
  682. goto err_fetch_mac;
  683. /* Register network device */
  684. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  685. goto err_register_netdev;
  686. /* Set initial link state */
  687. intel_check_link ( netdev );
  688. return 0;
  689. unregister_netdev ( netdev );
  690. err_register_netdev:
  691. err_fetch_mac:
  692. intel_reset ( intel );
  693. err_reset:
  694. iounmap ( intel->regs );
  695. netdev_nullify ( netdev );
  696. netdev_put ( netdev );
  697. err_alloc:
  698. return rc;
  699. }
  700. /**
  701. * Remove PCI device
  702. *
  703. * @v pci PCI device
  704. */
  705. static void intel_remove ( struct pci_device *pci ) {
  706. struct net_device *netdev = pci_get_drvdata ( pci );
  707. struct intel_nic *intel = netdev->priv;
  708. /* Unregister network device */
  709. unregister_netdev ( netdev );
  710. /* Reset the NIC */
  711. intel_reset ( intel );
  712. /* Free network device */
  713. iounmap ( intel->regs );
  714. netdev_nullify ( netdev );
  715. netdev_put ( netdev );
  716. }
  717. /** Intel PCI device IDs */
  718. static struct pci_device_id intel_nics[] = {
  719. PCI_ROM ( 0x8086, 0x0438, "dh8900cc", "DH8900CC", 0 ),
  720. PCI_ROM ( 0x8086, 0x043a, "dh8900cc-f", "DH8900CC Fiber", 0 ),
  721. PCI_ROM ( 0x8086, 0x043c, "dh8900cc-b", "DH8900CC Backplane", 0 ),
  722. PCI_ROM ( 0x8086, 0x0440, "dh8900cc-s", "DH8900CC SFP", 0 ),
  723. PCI_ROM ( 0x8086, 0x1000, "82542-f", "82542 (Fiber)", 0 ),
  724. PCI_ROM ( 0x8086, 0x1001, "82543gc-f", "82543GC (Fiber)", 0 ),
  725. PCI_ROM ( 0x8086, 0x1004, "82543gc", "82543GC (Copper)", 0 ),
  726. PCI_ROM ( 0x8086, 0x1008, "82544ei", "82544EI (Copper)", 0 ),
  727. PCI_ROM ( 0x8086, 0x1009, "82544ei-f", "82544EI (Fiber)", 0 ),
  728. PCI_ROM ( 0x8086, 0x100c, "82544gc", "82544GC (Copper)", 0 ),
  729. PCI_ROM ( 0x8086, 0x100d, "82544gc-l", "82544GC (LOM)", 0 ),
  730. PCI_ROM ( 0x8086, 0x100e, "82540em", "82540EM", 0 ),
  731. PCI_ROM ( 0x8086, 0x100f, "82545em", "82545EM (Copper)", 0 ),
  732. PCI_ROM ( 0x8086, 0x1010, "82546eb", "82546EB (Copper)", 0 ),
  733. PCI_ROM ( 0x8086, 0x1011, "82545em-f", "82545EM (Fiber)", 0 ),
  734. PCI_ROM ( 0x8086, 0x1012, "82546eb-f", "82546EB (Fiber)", 0 ),
  735. PCI_ROM ( 0x8086, 0x1013, "82541ei", "82541EI", 0 ),
  736. PCI_ROM ( 0x8086, 0x1014, "82541er", "82541ER", 0 ),
  737. PCI_ROM ( 0x8086, 0x1015, "82540em-l", "82540EM (LOM)", 0 ),
  738. PCI_ROM ( 0x8086, 0x1016, "82540ep-m", "82540EP (Mobile)", 0 ),
  739. PCI_ROM ( 0x8086, 0x1017, "82540ep", "82540EP", 0 ),
  740. PCI_ROM ( 0x8086, 0x1018, "82541ei", "82541EI", 0 ),
  741. PCI_ROM ( 0x8086, 0x1019, "82547ei", "82547EI", 0 ),
  742. PCI_ROM ( 0x8086, 0x101a, "82547ei-m", "82547EI (Mobile)", 0 ),
  743. PCI_ROM ( 0x8086, 0x101d, "82546eb", "82546EB", 0 ),
  744. PCI_ROM ( 0x8086, 0x101e, "82540ep-m", "82540EP (Mobile)", 0 ),
  745. PCI_ROM ( 0x8086, 0x1026, "82545gm", "82545GM", 0 ),
  746. PCI_ROM ( 0x8086, 0x1027, "82545gm-1", "82545GM", 0 ),
  747. PCI_ROM ( 0x8086, 0x1028, "82545gm-2", "82545GM", 0 ),
  748. PCI_ROM ( 0x8086, 0x1049, "82566mm", "82566MM", 0 ),
  749. PCI_ROM ( 0x8086, 0x104a, "82566dm", "82566DM", 0 ),
  750. PCI_ROM ( 0x8086, 0x104b, "82566dc", "82566DC", 0 ),
  751. PCI_ROM ( 0x8086, 0x104c, "82562v", "82562V 10/100", 0 ),
  752. PCI_ROM ( 0x8086, 0x104d, "82566mc", "82566MC", 0 ),
  753. PCI_ROM ( 0x8086, 0x105e, "82571eb", "82571EB", 0 ),
  754. PCI_ROM ( 0x8086, 0x105f, "82571eb-1", "82571EB", 0 ),
  755. PCI_ROM ( 0x8086, 0x1060, "82571eb-2", "82571EB", 0 ),
  756. PCI_ROM ( 0x8086, 0x1075, "82547gi", "82547GI", 0 ),
  757. PCI_ROM ( 0x8086, 0x1076, "82541gi", "82541GI", 0 ),
  758. PCI_ROM ( 0x8086, 0x1077, "82541gi-1", "82541GI", 0 ),
  759. PCI_ROM ( 0x8086, 0x1078, "82541er", "82541ER", 0 ),
  760. PCI_ROM ( 0x8086, 0x1079, "82546gb", "82546GB", 0 ),
  761. PCI_ROM ( 0x8086, 0x107a, "82546gb-1", "82546GB", 0 ),
  762. PCI_ROM ( 0x8086, 0x107b, "82546gb-2", "82546GB", 0 ),
  763. PCI_ROM ( 0x8086, 0x107c, "82541pi", "82541PI", 0 ),
  764. PCI_ROM ( 0x8086, 0x107d, "82572ei", "82572EI (Copper)", 0 ),
  765. PCI_ROM ( 0x8086, 0x107e, "82572ei-f", "82572EI (Fiber)", 0 ),
  766. PCI_ROM ( 0x8086, 0x107f, "82572ei", "82572EI", 0 ),
  767. PCI_ROM ( 0x8086, 0x108a, "82546gb-3", "82546GB", 0 ),
  768. PCI_ROM ( 0x8086, 0x108b, "82573v", "82573V (Copper)", 0 ),
  769. PCI_ROM ( 0x8086, 0x108c, "82573e", "82573E (Copper)", 0 ),
  770. PCI_ROM ( 0x8086, 0x1096, "80003es2lan", "80003ES2LAN (Copper)", 0 ),
  771. PCI_ROM ( 0x8086, 0x1098, "80003es2lan-s", "80003ES2LAN (Serdes)", 0 ),
  772. PCI_ROM ( 0x8086, 0x1099, "82546gb-4", "82546GB (Copper)", 0 ),
  773. PCI_ROM ( 0x8086, 0x109a, "82573l", "82573L", 0 ),
  774. PCI_ROM ( 0x8086, 0x10a4, "82571eb", "82571EB", 0 ),
  775. PCI_ROM ( 0x8086, 0x10a5, "82571eb", "82571EB (Fiber)", 0 ),
  776. PCI_ROM ( 0x8086, 0x10a7, "82575eb", "82575EB", 0 ),
  777. PCI_ROM ( 0x8086, 0x10a9, "82575eb", "82575EB Backplane", 0 ),
  778. PCI_ROM ( 0x8086, 0x10b5, "82546gb", "82546GB (Copper)", 0 ),
  779. PCI_ROM ( 0x8086, 0x10b9, "82572ei", "82572EI (Copper)", 0 ),
  780. PCI_ROM ( 0x8086, 0x10ba, "80003es2lan", "80003ES2LAN (Copper)", 0 ),
  781. PCI_ROM ( 0x8086, 0x10bb, "80003es2lan", "80003ES2LAN (Serdes)", 0 ),
  782. PCI_ROM ( 0x8086, 0x10bc, "82571eb", "82571EB (Copper)", 0 ),
  783. PCI_ROM ( 0x8086, 0x10bd, "82566dm-2", "82566DM-2", 0 ),
  784. PCI_ROM ( 0x8086, 0x10bf, "82567lf", "82567LF", 0 ),
  785. PCI_ROM ( 0x8086, 0x10c0, "82562v-2", "82562V-2 10/100", 0 ),
  786. PCI_ROM ( 0x8086, 0x10c2, "82562g-2", "82562G-2 10/100", 0 ),
  787. PCI_ROM ( 0x8086, 0x10c3, "82562gt-2", "82562GT-2 10/100", 0 ),
  788. PCI_ROM ( 0x8086, 0x10c4, "82562gt", "82562GT 10/100", 0 ),
  789. PCI_ROM ( 0x8086, 0x10c5, "82562g", "82562G 10/100", 0 ),
  790. PCI_ROM ( 0x8086, 0x10c9, "82576", "82576", 0 ),
  791. PCI_ROM ( 0x8086, 0x10cb, "82567v", "82567V", 0 ),
  792. PCI_ROM ( 0x8086, 0x10cc, "82567lm-2", "82567LM-2", 0 ),
  793. PCI_ROM ( 0x8086, 0x10cd, "82567lf-2", "82567LF-2", 0 ),
  794. PCI_ROM ( 0x8086, 0x10ce, "82567v-2", "82567V-2", 0 ),
  795. PCI_ROM ( 0x8086, 0x10d3, "82574l", "82574L", 0 ),
  796. PCI_ROM ( 0x8086, 0x10d5, "82571pt", "82571PT PT Quad", 0 ),
  797. PCI_ROM ( 0x8086, 0x10d6, "82575gb", "82575GB", 0 ),
  798. PCI_ROM ( 0x8086, 0x10d9, "82571eb-d", "82571EB Dual Mezzanine", 0 ),
  799. PCI_ROM ( 0x8086, 0x10da, "82571eb-q", "82571EB Quad Mezzanine", 0 ),
  800. PCI_ROM ( 0x8086, 0x10de, "82567lm-3", "82567LM-3", 0 ),
  801. PCI_ROM ( 0x8086, 0x10df, "82567lf-3", "82567LF-3", 0 ),
  802. PCI_ROM ( 0x8086, 0x10e5, "82567lm-4", "82567LM-4", 0 ),
  803. PCI_ROM ( 0x8086, 0x10e6, "82576", "82576", 0 ),
  804. PCI_ROM ( 0x8086, 0x10e7, "82576-2", "82576", 0 ),
  805. PCI_ROM ( 0x8086, 0x10e8, "82576-3", "82576", 0 ),
  806. PCI_ROM ( 0x8086, 0x10ea, "82577lm", "82577LM", 0 ),
  807. PCI_ROM ( 0x8086, 0x10eb, "82577lc", "82577LC", 0 ),
  808. PCI_ROM ( 0x8086, 0x10ef, "82578dm", "82578DM", 0 ),
  809. PCI_ROM ( 0x8086, 0x10f0, "82578dc", "82578DC", 0 ),
  810. PCI_ROM ( 0x8086, 0x10f5, "82567lm", "82567LM", 0 ),
  811. PCI_ROM ( 0x8086, 0x10f6, "82574l", "82574L", 0 ),
  812. PCI_ROM ( 0x8086, 0x1501, "82567v-3", "82567V-3", 0 ),
  813. PCI_ROM ( 0x8086, 0x1502, "82579lm", "82579LM", 0 ),
  814. PCI_ROM ( 0x8086, 0x1503, "82579v", "82579V", 0 ),
  815. PCI_ROM ( 0x8086, 0x150a, "82576ns", "82576NS", 0 ),
  816. PCI_ROM ( 0x8086, 0x150c, "82583v", "82583V", 0 ),
  817. PCI_ROM ( 0x8086, 0x150d, "82576-4", "82576 Backplane", 0 ),
  818. PCI_ROM ( 0x8086, 0x150e, "82580", "82580", 0 ),
  819. PCI_ROM ( 0x8086, 0x150f, "82580-f", "82580 Fiber", 0 ),
  820. PCI_ROM ( 0x8086, 0x1510, "82580-b", "82580 Backplane", 0 ),
  821. PCI_ROM ( 0x8086, 0x1511, "82580-s", "82580 SFP", 0 ),
  822. PCI_ROM ( 0x8086, 0x1516, "82580-2", "82580", 0 ),
  823. PCI_ROM ( 0x8086, 0x1518, "82576ns", "82576NS SerDes", 0 ),
  824. PCI_ROM ( 0x8086, 0x1521, "i350", "I350", 0 ),
  825. PCI_ROM ( 0x8086, 0x1522, "i350-f", "I350 Fiber", 0 ),
  826. PCI_ROM ( 0x8086, 0x1523, "i350-b", "I350 Backplane", 0 ),
  827. PCI_ROM ( 0x8086, 0x1524, "i350-2", "I350", 0 ),
  828. PCI_ROM ( 0x8086, 0x1525, "82567v-4", "82567V-4", 0 ),
  829. PCI_ROM ( 0x8086, 0x1526, "82576-5", "82576", 0 ),
  830. PCI_ROM ( 0x8086, 0x1527, "82580-f2", "82580 Fiber", 0 ),
  831. PCI_ROM ( 0x8086, 0x1533, "i210", "I210", 0 ),
  832. PCI_ROM ( 0x8086, 0x294c, "82566dc-2", "82566DC-2", 0 ),
  833. PCI_ROM ( 0x8086, 0x2e6e, "cemedia", "CE Media Processor", 0 ),
  834. };
  835. /** Intel PCI driver */
  836. struct pci_driver intel_driver __pci_driver = {
  837. .ids = intel_nics,
  838. .id_count = ( sizeof ( intel_nics ) / sizeof ( intel_nics[0] ) ),
  839. .probe = intel_probe,
  840. .remove = intel_remove,
  841. };