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

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