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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. /* Try to read address from EEPROM */
  207. if ( ( rc = intel_fetch_mac_eeprom ( intel, hw_addr ) ) == 0 )
  208. return 0;
  209. /* Use current address if valid */
  210. if ( is_valid_ether_addr ( mac.raw ) ) {
  211. memcpy ( hw_addr, mac.raw, ETH_ALEN );
  212. return 0;
  213. }
  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. pbs = readl ( intel->regs + INTEL_PBS );
  261. if ( ( pbs == 0x14 ) || ( pbs == 0x18 ) ) {
  262. DBGC ( intel, "INTEL %p WARNING: applying ICH PBS/PBA errata\n",
  263. intel );
  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_init_ring ( &intel->tx, INTEL_NUM_TX_DESC, INTEL_TD );
  701. intel_init_ring ( &intel->rx, INTEL_NUM_RX_DESC, INTEL_RD );
  702. /* Fix up PCI device */
  703. adjust_pci_device ( pci );
  704. /* Map registers */
  705. intel->regs = ioremap ( pci->membase, INTEL_BAR_SIZE );
  706. if ( ! intel->regs ) {
  707. rc = -ENODEV;
  708. goto err_ioremap;
  709. }
  710. /* Reset the NIC */
  711. if ( ( rc = intel_reset ( intel ) ) != 0 )
  712. goto err_reset;
  713. /* Fetch MAC address */
  714. if ( ( rc = intel_fetch_mac ( intel, netdev->hw_addr ) ) != 0 )
  715. goto err_fetch_mac;
  716. /* Register network device */
  717. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  718. goto err_register_netdev;
  719. /* Set initial link state */
  720. intel_check_link ( netdev );
  721. return 0;
  722. unregister_netdev ( netdev );
  723. err_register_netdev:
  724. err_fetch_mac:
  725. intel_reset ( intel );
  726. err_reset:
  727. iounmap ( intel->regs );
  728. err_ioremap:
  729. netdev_nullify ( netdev );
  730. netdev_put ( netdev );
  731. err_alloc:
  732. return rc;
  733. }
  734. /**
  735. * Remove PCI device
  736. *
  737. * @v pci PCI device
  738. */
  739. static void intel_remove ( struct pci_device *pci ) {
  740. struct net_device *netdev = pci_get_drvdata ( pci );
  741. struct intel_nic *intel = netdev->priv;
  742. /* Unregister network device */
  743. unregister_netdev ( netdev );
  744. /* Reset the NIC */
  745. intel_reset ( intel );
  746. /* Free network device */
  747. iounmap ( intel->regs );
  748. netdev_nullify ( netdev );
  749. netdev_put ( netdev );
  750. }
  751. /** Intel PCI device IDs */
  752. static struct pci_device_id intel_nics[] = {
  753. PCI_ROM ( 0x8086, 0x0438, "dh8900cc", "DH8900CC", 0 ),
  754. PCI_ROM ( 0x8086, 0x043a, "dh8900cc-f", "DH8900CC Fiber", 0 ),
  755. PCI_ROM ( 0x8086, 0x043c, "dh8900cc-b", "DH8900CC Backplane", 0 ),
  756. PCI_ROM ( 0x8086, 0x0440, "dh8900cc-s", "DH8900CC SFP", 0 ),
  757. PCI_ROM ( 0x8086, 0x1000, "82542-f", "82542 (Fiber)", 0 ),
  758. PCI_ROM ( 0x8086, 0x1001, "82543gc-f", "82543GC (Fiber)", 0 ),
  759. PCI_ROM ( 0x8086, 0x1004, "82543gc", "82543GC (Copper)", 0 ),
  760. PCI_ROM ( 0x8086, 0x1008, "82544ei", "82544EI (Copper)", 0 ),
  761. PCI_ROM ( 0x8086, 0x1009, "82544ei-f", "82544EI (Fiber)", 0 ),
  762. PCI_ROM ( 0x8086, 0x100c, "82544gc", "82544GC (Copper)", 0 ),
  763. PCI_ROM ( 0x8086, 0x100d, "82544gc-l", "82544GC (LOM)", 0 ),
  764. PCI_ROM ( 0x8086, 0x100e, "82540em", "82540EM", 0 ),
  765. PCI_ROM ( 0x8086, 0x100f, "82545em", "82545EM (Copper)", 0 ),
  766. PCI_ROM ( 0x8086, 0x1010, "82546eb", "82546EB (Copper)", 0 ),
  767. PCI_ROM ( 0x8086, 0x1011, "82545em-f", "82545EM (Fiber)", 0 ),
  768. PCI_ROM ( 0x8086, 0x1012, "82546eb-f", "82546EB (Fiber)", 0 ),
  769. PCI_ROM ( 0x8086, 0x1013, "82541ei", "82541EI", 0 ),
  770. PCI_ROM ( 0x8086, 0x1014, "82541er", "82541ER", 0 ),
  771. PCI_ROM ( 0x8086, 0x1015, "82540em-l", "82540EM (LOM)", 0 ),
  772. PCI_ROM ( 0x8086, 0x1016, "82540ep-m", "82540EP (Mobile)", 0 ),
  773. PCI_ROM ( 0x8086, 0x1017, "82540ep", "82540EP", 0 ),
  774. PCI_ROM ( 0x8086, 0x1018, "82541ei", "82541EI", 0 ),
  775. PCI_ROM ( 0x8086, 0x1019, "82547ei", "82547EI", 0 ),
  776. PCI_ROM ( 0x8086, 0x101a, "82547ei-m", "82547EI (Mobile)", 0 ),
  777. PCI_ROM ( 0x8086, 0x101d, "82546eb", "82546EB", 0 ),
  778. PCI_ROM ( 0x8086, 0x101e, "82540ep-m", "82540EP (Mobile)", 0 ),
  779. PCI_ROM ( 0x8086, 0x1026, "82545gm", "82545GM", 0 ),
  780. PCI_ROM ( 0x8086, 0x1027, "82545gm-1", "82545GM", 0 ),
  781. PCI_ROM ( 0x8086, 0x1028, "82545gm-2", "82545GM", 0 ),
  782. PCI_ROM ( 0x8086, 0x1049, "82566mm", "82566MM", 0 ),
  783. PCI_ROM ( 0x8086, 0x104a, "82566dm", "82566DM", 0 ),
  784. PCI_ROM ( 0x8086, 0x104b, "82566dc", "82566DC", 0 ),
  785. PCI_ROM ( 0x8086, 0x104c, "82562v", "82562V 10/100", 0 ),
  786. PCI_ROM ( 0x8086, 0x104d, "82566mc", "82566MC", 0 ),
  787. PCI_ROM ( 0x8086, 0x105e, "82571eb", "82571EB", 0 ),
  788. PCI_ROM ( 0x8086, 0x105f, "82571eb-1", "82571EB", 0 ),
  789. PCI_ROM ( 0x8086, 0x1060, "82571eb-2", "82571EB", 0 ),
  790. PCI_ROM ( 0x8086, 0x1075, "82547gi", "82547GI", 0 ),
  791. PCI_ROM ( 0x8086, 0x1076, "82541gi", "82541GI", 0 ),
  792. PCI_ROM ( 0x8086, 0x1077, "82541gi-1", "82541GI", 0 ),
  793. PCI_ROM ( 0x8086, 0x1078, "82541er", "82541ER", 0 ),
  794. PCI_ROM ( 0x8086, 0x1079, "82546gb", "82546GB", 0 ),
  795. PCI_ROM ( 0x8086, 0x107a, "82546gb-1", "82546GB", 0 ),
  796. PCI_ROM ( 0x8086, 0x107b, "82546gb-2", "82546GB", 0 ),
  797. PCI_ROM ( 0x8086, 0x107c, "82541pi", "82541PI", 0 ),
  798. PCI_ROM ( 0x8086, 0x107d, "82572ei", "82572EI (Copper)", 0 ),
  799. PCI_ROM ( 0x8086, 0x107e, "82572ei-f", "82572EI (Fiber)", 0 ),
  800. PCI_ROM ( 0x8086, 0x107f, "82572ei", "82572EI", 0 ),
  801. PCI_ROM ( 0x8086, 0x108a, "82546gb-3", "82546GB", 0 ),
  802. PCI_ROM ( 0x8086, 0x108b, "82573v", "82573V (Copper)", 0 ),
  803. PCI_ROM ( 0x8086, 0x108c, "82573e", "82573E (Copper)", 0 ),
  804. PCI_ROM ( 0x8086, 0x1096, "80003es2lan", "80003ES2LAN (Copper)", 0 ),
  805. PCI_ROM ( 0x8086, 0x1098, "80003es2lan-s", "80003ES2LAN (Serdes)", 0 ),
  806. PCI_ROM ( 0x8086, 0x1099, "82546gb-4", "82546GB (Copper)", 0 ),
  807. PCI_ROM ( 0x8086, 0x109a, "82573l", "82573L", 0 ),
  808. PCI_ROM ( 0x8086, 0x10a4, "82571eb", "82571EB", 0 ),
  809. PCI_ROM ( 0x8086, 0x10a5, "82571eb", "82571EB (Fiber)", 0 ),
  810. PCI_ROM ( 0x8086, 0x10a7, "82575eb", "82575EB", 0 ),
  811. PCI_ROM ( 0x8086, 0x10a9, "82575eb", "82575EB Backplane", 0 ),
  812. PCI_ROM ( 0x8086, 0x10b5, "82546gb", "82546GB (Copper)", 0 ),
  813. PCI_ROM ( 0x8086, 0x10b9, "82572ei", "82572EI (Copper)", 0 ),
  814. PCI_ROM ( 0x8086, 0x10ba, "80003es2lan", "80003ES2LAN (Copper)", 0 ),
  815. PCI_ROM ( 0x8086, 0x10bb, "80003es2lan", "80003ES2LAN (Serdes)", 0 ),
  816. PCI_ROM ( 0x8086, 0x10bc, "82571eb", "82571EB (Copper)", 0 ),
  817. PCI_ROM ( 0x8086, 0x10bd, "82566dm-2", "82566DM-2", 0 ),
  818. PCI_ROM ( 0x8086, 0x10bf, "82567lf", "82567LF", 0 ),
  819. PCI_ROM ( 0x8086, 0x10c0, "82562v-2", "82562V-2 10/100", 0 ),
  820. PCI_ROM ( 0x8086, 0x10c2, "82562g-2", "82562G-2 10/100", 0 ),
  821. PCI_ROM ( 0x8086, 0x10c3, "82562gt-2", "82562GT-2 10/100", 0 ),
  822. PCI_ROM ( 0x8086, 0x10c4, "82562gt", "82562GT 10/100", 0 ),
  823. PCI_ROM ( 0x8086, 0x10c5, "82562g", "82562G 10/100", 0 ),
  824. PCI_ROM ( 0x8086, 0x10c9, "82576", "82576", 0 ),
  825. PCI_ROM ( 0x8086, 0x10cb, "82567v", "82567V", 0 ),
  826. PCI_ROM ( 0x8086, 0x10cc, "82567lm-2", "82567LM-2", 0 ),
  827. PCI_ROM ( 0x8086, 0x10cd, "82567lf-2", "82567LF-2", 0 ),
  828. PCI_ROM ( 0x8086, 0x10ce, "82567v-2", "82567V-2", 0 ),
  829. PCI_ROM ( 0x8086, 0x10d3, "82574l", "82574L", 0 ),
  830. PCI_ROM ( 0x8086, 0x10d5, "82571pt", "82571PT PT Quad", 0 ),
  831. PCI_ROM ( 0x8086, 0x10d6, "82575gb", "82575GB", 0 ),
  832. PCI_ROM ( 0x8086, 0x10d9, "82571eb-d", "82571EB Dual Mezzanine", 0 ),
  833. PCI_ROM ( 0x8086, 0x10da, "82571eb-q", "82571EB Quad Mezzanine", 0 ),
  834. PCI_ROM ( 0x8086, 0x10de, "82567lm-3", "82567LM-3", 0 ),
  835. PCI_ROM ( 0x8086, 0x10df, "82567lf-3", "82567LF-3", 0 ),
  836. PCI_ROM ( 0x8086, 0x10e5, "82567lm-4", "82567LM-4", 0 ),
  837. PCI_ROM ( 0x8086, 0x10e6, "82576", "82576", 0 ),
  838. PCI_ROM ( 0x8086, 0x10e7, "82576-2", "82576", 0 ),
  839. PCI_ROM ( 0x8086, 0x10e8, "82576-3", "82576", 0 ),
  840. PCI_ROM ( 0x8086, 0x10ea, "82577lm", "82577LM", 0 ),
  841. PCI_ROM ( 0x8086, 0x10eb, "82577lc", "82577LC", 0 ),
  842. PCI_ROM ( 0x8086, 0x10ef, "82578dm", "82578DM", 0 ),
  843. PCI_ROM ( 0x8086, 0x10f0, "82578dc", "82578DC", 0 ),
  844. PCI_ROM ( 0x8086, 0x10f5, "82567lm", "82567LM", 0 ),
  845. PCI_ROM ( 0x8086, 0x10f6, "82574l", "82574L", 0 ),
  846. PCI_ROM ( 0x8086, 0x1501, "82567v-3", "82567V-3", 0 ),
  847. PCI_ROM ( 0x8086, 0x1502, "82579lm", "82579LM", 0 ),
  848. PCI_ROM ( 0x8086, 0x1503, "82579v", "82579V", 0 ),
  849. PCI_ROM ( 0x8086, 0x150a, "82576ns", "82576NS", 0 ),
  850. PCI_ROM ( 0x8086, 0x150c, "82583v", "82583V", 0 ),
  851. PCI_ROM ( 0x8086, 0x150d, "82576-4", "82576 Backplane", 0 ),
  852. PCI_ROM ( 0x8086, 0x150e, "82580", "82580", 0 ),
  853. PCI_ROM ( 0x8086, 0x150f, "82580-f", "82580 Fiber", 0 ),
  854. PCI_ROM ( 0x8086, 0x1510, "82580-b", "82580 Backplane", 0 ),
  855. PCI_ROM ( 0x8086, 0x1511, "82580-s", "82580 SFP", 0 ),
  856. PCI_ROM ( 0x8086, 0x1516, "82580-2", "82580", 0 ),
  857. PCI_ROM ( 0x8086, 0x1518, "82576ns", "82576NS SerDes", 0 ),
  858. PCI_ROM ( 0x8086, 0x1521, "i350", "I350", 0 ),
  859. PCI_ROM ( 0x8086, 0x1522, "i350-f", "I350 Fiber", 0 ),
  860. PCI_ROM ( 0x8086, 0x1523, "i350-b", "I350 Backplane", 0 ),
  861. PCI_ROM ( 0x8086, 0x1524, "i350-2", "I350", 0 ),
  862. PCI_ROM ( 0x8086, 0x1525, "82567v-4", "82567V-4", 0 ),
  863. PCI_ROM ( 0x8086, 0x1526, "82576-5", "82576", 0 ),
  864. PCI_ROM ( 0x8086, 0x1527, "82580-f2", "82580 Fiber", 0 ),
  865. PCI_ROM ( 0x8086, 0x1533, "i210", "I210", 0 ),
  866. PCI_ROM ( 0x8086, 0x153b, "i217", "I217", 0 ),
  867. PCI_ROM ( 0x8086, 0x294c, "82566dc-2", "82566DC-2", 0 ),
  868. PCI_ROM ( 0x8086, 0x2e6e, "cemedia", "CE Media Processor", 0 ),
  869. };
  870. /** Intel PCI driver */
  871. struct pci_driver intel_driver __pci_driver = {
  872. .ids = intel_nics,
  873. .id_count = ( sizeof ( intel_nics ) / sizeof ( intel_nics[0] ) ),
  874. .probe = intel_probe,
  875. .remove = intel_remove,
  876. };