Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /* natsemi.c - gPXE driver for the NatSemi DP8381x series. */
  2. /*
  3. natsemi.c: An Etherboot driver for the NatSemi DP8381x series.
  4. Copyright (C) 2001 Entity Cyber, Inc.
  5. This development of this Etherboot driver was funded by
  6. Sicom Systems: http://www.sicompos.com/
  7. Author: Marty Connor (mdc@thinguin.org)
  8. Adapted from a Linux driver which was written by Donald Becker
  9. This software may be used and distributed according to the terms
  10. of the GNU Public License (GPL), incorporated herein by reference.
  11. Original Copyright Notice:
  12. Written/copyright 1999-2001 by Donald Becker.
  13. This software may be used and distributed according to the terms of
  14. the GNU General Public License (GPL), incorporated herein by reference.
  15. Drivers based on or derived from this code fall under the GPL and must
  16. retain the authorship, copyright and license notice. This file is not
  17. a complete program and may only be used when the entire operating
  18. system is licensed under the GPL. License for under other terms may be
  19. available. Contact the original author for details.
  20. The original author may be reached as becker@scyld.com, or at
  21. Scyld Computing Corporation
  22. 410 Severn Ave., Suite 210
  23. Annapolis MD 21403
  24. Support information and updates available at
  25. http://www.scyld.com/network/netsemi.html
  26. References:
  27. http://www.scyld.com/expert/100mbps.html
  28. http://www.scyld.com/expert/NWay.html
  29. Datasheet is available from:
  30. http://www.national.com/pf/DP/DP83815.html
  31. */
  32. /* Revision History */
  33. /*
  34. 02 JUL 2007 Udayan Kumar 1.2 ported the driver from etherboot to gPXE API.
  35. Fully rewritten,adapting the old driver.
  36. Added a circular buffer for transmit and receive.
  37. transmit routine will not wait for transmission to finish.
  38. poll routine deals with it.
  39. 13 Dec 2003 timlegge 1.1 Enabled Multicast Support
  40. 29 May 2001 mdc 1.0
  41. Initial Release. Tested with Netgear FA311 and FA312 boards
  42. */
  43. #include <stdint.h>
  44. #include <pic8259.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <io.h>
  48. #include <errno.h>
  49. #include <timer.h>
  50. #include <byteswap.h>
  51. #include <gpxe/pci.h>
  52. #include <gpxe/if_ether.h>
  53. #include <gpxe/ethernet.h>
  54. #include <gpxe/iobuf.h>
  55. #include <gpxe/netdevice.h>
  56. #include <gpxe/spi_bit.h>
  57. #include <gpxe/threewire.h>
  58. #include <gpxe/nvo.h>
  59. #define TX_RING_SIZE 4
  60. #define NUM_RX_DESC 4
  61. #define RX_BUF_SIZE 1536
  62. #define OWN 0x80000000
  63. #define DSIZE 0x00000FFF
  64. #define CRC_SIZE 4
  65. struct natsemi_tx {
  66. uint32_t link;
  67. uint32_t cmdsts;
  68. uint32_t bufptr;
  69. };
  70. struct natsemi_rx {
  71. uint32_t link;
  72. uint32_t cmdsts;
  73. uint32_t bufptr;
  74. };
  75. struct natsemi_nic {
  76. unsigned short ioaddr;
  77. unsigned short tx_cur;
  78. unsigned short tx_dirty;
  79. unsigned short rx_cur;
  80. struct natsemi_tx tx[TX_RING_SIZE];
  81. struct natsemi_rx rx[NUM_RX_DESC];
  82. /* need to add iobuf as we cannot free iobuf->data in close without this
  83. * alternatively substracting sizeof(head) and sizeof(list_head) can also
  84. * give the same.
  85. */
  86. struct io_buffer *iobuf[NUM_RX_DESC];
  87. /* netdev_tx_complete needs pointer to the iobuf of the data so as to free
  88. * it from the memory.
  89. */
  90. struct io_buffer *tx_iobuf[TX_RING_SIZE];
  91. struct spi_bit_basher spibit;
  92. struct spi_device eeprom;
  93. struct nvo_block nvo;
  94. };
  95. /* NATSEMI: Offsets to the device registers.
  96. * Unlike software-only systems, device drivers interact with complex hardware.
  97. * It's not useful to define symbolic names for every register bit in the
  98. * device.
  99. */
  100. enum register_offsets {
  101. ChipCmd = 0x00,
  102. ChipConfig = 0x04,
  103. EECtrl = 0x08,
  104. PCIBusCfg = 0x0C,
  105. IntrStatus = 0x10,
  106. IntrMask = 0x14,
  107. IntrEnable = 0x18,
  108. TxRingPtr = 0x20,
  109. TxConfig = 0x24,
  110. RxRingPtr = 0x30,
  111. RxConfig = 0x34,
  112. ClkRun = 0x3C,
  113. WOLCmd = 0x40,
  114. PauseCmd = 0x44,
  115. RxFilterAddr = 0x48,
  116. RxFilterData = 0x4C,
  117. BootRomAddr = 0x50,
  118. BootRomData = 0x54,
  119. SiliconRev = 0x58,
  120. StatsCtrl = 0x5C,
  121. StatsData = 0x60,
  122. RxPktErrs = 0x60,
  123. RxMissed = 0x68,
  124. RxCRCErrs = 0x64,
  125. PCIPM = 0x44,
  126. PhyStatus = 0xC0,
  127. MIntrCtrl = 0xC4,
  128. MIntrStatus = 0xC8,
  129. /* These are from the spec, around page 78... on a separate table.
  130. */
  131. PGSEL = 0xCC,
  132. PMDCSR = 0xE4,
  133. TSTDAT = 0xFC,
  134. DSPCFG = 0xF4,
  135. SDCFG = 0x8C,
  136. BasicControl = 0x80,
  137. BasicStatus = 0x84
  138. };
  139. /* the values for the 'magic' registers above (PGSEL=1) */
  140. #define PMDCSR_VAL 0x189c /* enable preferred adaptation circuitry */
  141. #define TSTDAT_VAL 0x0
  142. #define DSPCFG_VAL 0x5040
  143. #define SDCFG_VAL 0x008c /* set voltage thresholds for Signal Detect */
  144. #define DSPCFG_LOCK 0x20 /* coefficient lock bit in DSPCFG */
  145. #define DSPCFG_COEF 0x1000 /* see coefficient (in TSTDAT) bit in DSPCFG */
  146. #define TSTDAT_FIXED 0xe8 /* magic number for bad coefficients */
  147. /* Bit in ChipCmd.
  148. */
  149. enum ChipCmdBits {
  150. ChipReset = 0x100,
  151. RxReset = 0x20,
  152. TxReset = 0x10,
  153. RxOff = 0x08,
  154. RxOn = 0x04,
  155. TxOff = 0x02,
  156. TxOn = 0x01
  157. };
  158. enum ChipConfig_bits {
  159. CfgPhyDis = 0x200,
  160. CfgPhyRst = 0x400,
  161. CfgExtPhy = 0x1000,
  162. CfgAnegEnable = 0x2000,
  163. CfgAneg100 = 0x4000,
  164. CfgAnegFull = 0x8000,
  165. CfgAnegDone = 0x8000000,
  166. CfgFullDuplex = 0x20000000,
  167. CfgSpeed100 = 0x40000000,
  168. CfgLink = 0x80000000,
  169. };
  170. /* Bits in the RxMode register.
  171. */
  172. enum rx_mode_bits {
  173. AcceptErr = 0x20,
  174. AcceptRunt = 0x10,
  175. AcceptBroadcast = 0xC0000000,
  176. AcceptMulticast = 0x00200000,
  177. AcceptAllMulticast = 0x20000000,
  178. AcceptAllPhys = 0x10000000,
  179. AcceptMyPhys = 0x08000000,
  180. RxFilterEnable = 0x80000000
  181. };
  182. /* Bits in network_desc.status
  183. */
  184. enum desc_status_bits {
  185. DescOwn = 0x80000000,
  186. DescMore = 0x40000000,
  187. DescIntr = 0x20000000,
  188. DescNoCRC = 0x10000000,
  189. DescPktOK = 0x08000000,
  190. RxTooLong = 0x00400000
  191. };
  192. /*Bits in Interrupt Mask register
  193. */
  194. enum Intr_mask_register_bits {
  195. RxOk = 0x001,
  196. RxErr = 0x004,
  197. TxOk = 0x040,
  198. TxErr = 0x100
  199. };
  200. /* EEPROM access , values are devices specific
  201. */
  202. #define EE_CS 0x08 /* EEPROM chip select */
  203. #define EE_SK 0x04 /* EEPROM shift clock */
  204. #define EE_DI 0x01 /* Data in */
  205. #define EE_DO 0x02 /* Data out */
  206. /* Offsets within EEPROM (these are word offsets)
  207. */
  208. #define EE_MAC 7
  209. #define EE_REG EECtrl
  210. static uint32_t SavedClkRun;
  211. static const uint8_t nat_ee_bits[] = {
  212. [SPI_BIT_SCLK] = EE_SK,
  213. [SPI_BIT_MOSI] = EE_DI,
  214. [SPI_BIT_MISO] = EE_DO,
  215. [SPI_BIT_SS(0)] = EE_CS,
  216. };
  217. static int nat_spi_read_bit ( struct bit_basher *basher,
  218. unsigned int bit_id ) {
  219. struct natsemi_nic *nat = container_of ( basher, struct natsemi_nic,
  220. spibit.basher );
  221. uint8_t mask = nat_ee_bits[bit_id];
  222. uint8_t eereg;
  223. eereg = inb ( nat->ioaddr + EE_REG );
  224. return ( eereg & mask );
  225. }
  226. static void nat_spi_write_bit ( struct bit_basher *basher,
  227. unsigned int bit_id, unsigned long data ) {
  228. struct natsemi_nic *nat = container_of ( basher, struct natsemi_nic,
  229. spibit.basher );
  230. uint8_t mask = nat_ee_bits[bit_id];
  231. uint8_t eereg;
  232. eereg = inb ( nat->ioaddr + EE_REG );
  233. eereg &= ~mask;
  234. eereg |= ( data & mask );
  235. outb ( eereg, nat->ioaddr + EE_REG );
  236. }
  237. static struct bit_basher_operations nat_basher_ops = {
  238. .read = nat_spi_read_bit,
  239. .write = nat_spi_write_bit,
  240. };
  241. /* It looks that this portion of EEPROM can be used for
  242. * non-volatile stored options. Data sheet does not talk about this region.
  243. * Currently it is not working. But with some efforts it can.
  244. */
  245. static struct nvo_fragment nat_nvo_fragments[] = {
  246. { 0x0c, 0x68 },
  247. { 0, 0 }
  248. };
  249. /*
  250. * Set up for EEPROM access
  251. *
  252. * @v NAT NATSEMI NIC
  253. */
  254. void nat_init_eeprom ( struct natsemi_nic *nat ) {
  255. /* Initialise three-wire bus
  256. */
  257. nat->spibit.basher.op = &nat_basher_ops;
  258. nat->spibit.bus.mode = SPI_MODE_THREEWIRE;
  259. nat->spibit.endianness = SPI_BIT_LITTLE_ENDIAN;
  260. init_spi_bit_basher ( &nat->spibit );
  261. /*natsemi DP 83815 only supports at93c46
  262. */
  263. init_at93c46 ( &nat->eeprom, 16 );
  264. nat->eeprom.bus = &nat->spibit.bus;
  265. nat->nvo.nvs = &nat->eeprom.nvs;
  266. nat->nvo.fragments = nat_nvo_fragments;
  267. }
  268. /*
  269. * Reset NIC
  270. *
  271. * @v NATSEMI NIC
  272. *
  273. * Issues a hardware reset and waits for the reset to complete.
  274. */
  275. static void nat_reset ( struct natsemi_nic *nat ) {
  276. int i;
  277. /* Reset chip
  278. */
  279. outl ( ChipReset, nat->ioaddr + ChipCmd );
  280. mdelay ( 10 );
  281. nat->tx_dirty = 0;
  282. nat->tx_cur = 0;
  283. for ( i = 0 ; i < TX_RING_SIZE ; i++ ) {
  284. nat->tx[i].link = 0;
  285. nat->tx[i].cmdsts = 0;
  286. nat->tx[i].bufptr = 0;
  287. }
  288. nat->rx_cur = 0;
  289. outl ( virt_to_bus( &nat->tx[0] ),nat->ioaddr + TxRingPtr );
  290. outl ( virt_to_bus( &nat->rx[0] ),nat->ioaddr + RxRingPtr );
  291. outl ( TxOff|RxOff, nat->ioaddr + ChipCmd );
  292. /* Restore PME enable bit
  293. */
  294. outl ( SavedClkRun, nat->ioaddr + ClkRun );
  295. }
  296. static void do_cable_magic ( struct net_device *netdev ) {
  297. struct natsemi_nic *nat = netdev->priv;
  298. uint16_t data;
  299. /*
  300. * 100 MBit links with short cables can trip an issue with the chip.
  301. * The problem manifests as lots of CRC errors and/or flickering
  302. * activity LED while idle. This process is based on instructions
  303. * from engineers at National.
  304. */
  305. if (inl(nat->ioaddr + ChipConfig) & CfgSpeed100) {
  306. outw(1, nat->ioaddr + PGSEL);
  307. /*
  308. * coefficient visibility should already be enabled via
  309. * DSPCFG | 0x1000
  310. */
  311. data = inw(nat->ioaddr + TSTDAT) & 0xff;
  312. /*
  313. * the value must be negative, and within certain values
  314. * (these values all come from National)
  315. */
  316. if (!(data & 0x80) || ((data >= 0xd8) && (data <= 0xff))) {
  317. /* the bug has been triggered - fix the coefficient */
  318. outw(TSTDAT_FIXED, nat->ioaddr + TSTDAT);
  319. /* lock the value */
  320. data = inw(nat->ioaddr + DSPCFG);
  321. //np->dspcfg = data | DSPCFG_LOCK;
  322. outw(data | DSPCFG_LOCK , nat->ioaddr + DSPCFG);
  323. }
  324. outw(0, nat->ioaddr + PGSEL);
  325. }
  326. }
  327. /*
  328. * Open NIC
  329. *
  330. * @v netdev Net device
  331. * @ret rc Return status code
  332. */
  333. static int nat_open ( struct net_device *netdev ) {
  334. struct natsemi_nic *nat = netdev->priv;
  335. int i;
  336. uint32_t tx_config,rx_config;
  337. /* Disable PME:
  338. * The PME bit is initialized from the EEPROM contents.
  339. * PCI cards probably have PME disabled, but motherboard
  340. * implementations may have PME set to enable WakeOnLan.
  341. * With PME set the chip will scan incoming packets but
  342. * nothing will be written to memory.
  343. */
  344. SavedClkRun = inl ( nat->ioaddr + ClkRun );
  345. outl ( SavedClkRun & ~0x100, nat->ioaddr + ClkRun );
  346. /* Setting up Mac address in the NIC
  347. */
  348. for ( i = 0 ; i < ETH_ALEN ; i+=2 ) {
  349. outl ( i,nat->ioaddr + RxFilterAddr );
  350. outw ( netdev->ll_addr[i] + ( netdev->ll_addr[i + 1] << 8 ),
  351. nat->ioaddr + RxFilterData );
  352. }
  353. /*Set up the Tx Ring
  354. */
  355. nat->tx_cur = 0;
  356. nat->tx_dirty = 0;
  357. for ( i = 0 ; i < TX_RING_SIZE ; i++ ) {
  358. nat->tx[i].link = virt_to_bus ( ( i + 1 < TX_RING_SIZE ) ? &nat->tx[i + 1] : &nat->tx[0] );
  359. nat->tx[i].cmdsts = 0;
  360. nat->tx[i].bufptr = 0;
  361. }
  362. /* Set up RX ring
  363. */
  364. nat->rx_cur = 0;
  365. for ( i = 0 ; i < NUM_RX_DESC ; i++ ) {
  366. nat->iobuf[i] = alloc_iob ( RX_BUF_SIZE );
  367. if ( !nat->iobuf[i] )
  368. goto memory_alloc_err;
  369. nat->rx[i].link = virt_to_bus ( ( i + 1 < NUM_RX_DESC ) ? &nat->rx[i + 1] : &nat->rx[0] );
  370. nat->rx[i].cmdsts = RX_BUF_SIZE;
  371. nat->rx[i].bufptr = virt_to_bus ( nat->iobuf[i]->data );
  372. }
  373. /* load Receive Descriptor Register
  374. */
  375. outl ( virt_to_bus ( &nat->rx[0] ), nat->ioaddr + RxRingPtr );
  376. DBG ( "Natsemi Rx descriptor loaded with: %X\n",
  377. (unsigned int) inl ( nat->ioaddr + RxRingPtr ) );
  378. /* setup Tx ring
  379. */
  380. outl ( virt_to_bus ( &nat->tx[0] ),nat->ioaddr + TxRingPtr );
  381. DBG ( "Natsemi Tx descriptor loaded with: %X\n",
  382. (unsigned int)inl ( nat->ioaddr + TxRingPtr ) );
  383. /* Enables RX
  384. */
  385. outl ( RxFilterEnable|AcceptBroadcast|AcceptAllMulticast|AcceptMyPhys,
  386. nat->ioaddr + RxFilterAddr );
  387. /* Initialize other registers.
  388. * Configure the PCI bus bursts and FIFO thresholds.
  389. * Configure for standard, in-spec Ethernet.
  390. */
  391. if ( inl ( nat->ioaddr + ChipConfig ) & 0x20000000 ) { /* Full duplex */
  392. tx_config = 0xD0801002 | 0xC0000000;
  393. DBG ( "Full duplex\n" );
  394. rx_config = 0x10000020 | 0x10000000;
  395. } else {
  396. tx_config = 0x10801002 & ~0xC0000000;
  397. DBG ( "Half duplex\n" );
  398. rx_config = 0x0020 & ~0x10000000;
  399. }
  400. outl ( tx_config, nat->ioaddr + TxConfig );
  401. outl ( rx_config, nat->ioaddr + RxConfig );
  402. /*start the receiver
  403. */
  404. outl ( RxOn, nat->ioaddr + ChipCmd );
  405. /* lines 1586 linux-natsemi.c uses cable magic
  406. * testing this feature is required or not
  407. */
  408. do_cable_magic ( netdev );
  409. /* mask the interrupts. note interrupt is not enabled here
  410. */
  411. return 0;
  412. memory_alloc_err:
  413. /* this block frees the previously allocated buffers
  414. * if memory for all the buffers is not available
  415. */
  416. i = 0;
  417. while ( nat->rx[i].cmdsts == RX_BUF_SIZE ) {
  418. free_iob ( nat->iobuf[i] );
  419. i++;
  420. }
  421. return -ENOMEM;
  422. }
  423. /**
  424. * Close NIC
  425. *
  426. * @v netdev Net device
  427. */
  428. static void nat_close ( struct net_device *netdev ) {
  429. struct natsemi_nic *nat = netdev->priv;
  430. int i;
  431. /* Reset the hardware to disable everything in one go
  432. */
  433. nat_reset ( nat );
  434. /* Free RX ring
  435. */
  436. for ( i = 0; i < NUM_RX_DESC ; i++ ) {
  437. free_iob ( nat->iobuf[i] );
  438. }
  439. }
  440. /**
  441. * Transmit packet
  442. *
  443. * @v netdev Network device
  444. * @v iobuf I/O buffer
  445. * @ret rc Return status code
  446. */
  447. static int nat_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
  448. struct natsemi_nic *nat = netdev->priv;
  449. /* check for space in TX ring
  450. */
  451. if ( nat->tx[nat->tx_cur].cmdsts != 0 ) {
  452. DBG ( "TX overflow\n" );
  453. return -ENOBUFS;
  454. }
  455. /* to be used in netdev_tx_complete
  456. */
  457. nat->tx_iobuf[nat->tx_cur] = iobuf;
  458. /* Pad and align packet has not been used because its not required here
  459. * iob_pad ( iobuf, ETH_ZLEN ); can be used to achieve it
  460. */
  461. /* Add to TX ring
  462. */
  463. DBG ( "TX id %d at %lx + %x\n", nat->tx_cur,
  464. virt_to_bus ( &iobuf->data ), iob_len ( iobuf ) );
  465. nat->tx[nat->tx_cur].bufptr = virt_to_bus ( iobuf->data );
  466. nat->tx[nat->tx_cur].cmdsts = iob_len ( iobuf ) | OWN;
  467. /* increment the circular buffer pointer to the next buffer location
  468. */
  469. nat->tx_cur = ( nat->tx_cur + 1 ) % TX_RING_SIZE;
  470. /*start the transmitter
  471. */
  472. outl ( TxOn, nat->ioaddr + ChipCmd );
  473. return 0;
  474. }
  475. /**
  476. * Poll for received packets
  477. *
  478. * @v netdev Network device
  479. * @v rx_quota Maximum number of packets to receive
  480. */
  481. static void nat_poll ( struct net_device *netdev) {
  482. struct natsemi_nic *nat = netdev->priv;
  483. unsigned int status;
  484. unsigned int rx_status;
  485. unsigned int intr_status;
  486. unsigned int rx_len;
  487. struct io_buffer *rx_iob;
  488. int i;
  489. /* read the interrupt register
  490. */
  491. intr_status = inl ( nat->ioaddr + IntrStatus );
  492. if ( !intr_status )
  493. goto end;
  494. /* check the status of packets given to card for transmission
  495. */
  496. DBG ( "Intr status %X\n",intr_status );
  497. i = nat->tx_dirty;
  498. while ( i!= nat->tx_cur ) {
  499. status = nat->tx[nat->tx_dirty].cmdsts;
  500. DBG ( "value of tx_dirty = %d tx_cur=%d status=%X\n",
  501. nat->tx_dirty,nat->tx_cur,status );
  502. /* check if current packet has been transmitted or not
  503. */
  504. if ( status & OWN )
  505. break;
  506. /* Check if any errors in transmission
  507. */
  508. if (! ( status & DescPktOK ) ) {
  509. DBG ( "Error in sending Packet status:%X\n",
  510. (unsigned int) status );
  511. netdev_tx_complete_err ( netdev,nat->tx_iobuf[nat->tx_dirty],-EINVAL );
  512. } else {
  513. DBG ( "Success in transmitting Packet\n" );
  514. netdev_tx_complete ( netdev,nat->tx_iobuf[nat->tx_dirty] );
  515. }
  516. /* setting cmdsts zero, indicating that it can be reused
  517. */
  518. nat->tx[nat->tx_dirty].cmdsts = 0;
  519. nat->tx_dirty = ( nat->tx_dirty + 1 ) % TX_RING_SIZE;
  520. i = ( i + 1 ) % TX_RING_SIZE;
  521. }
  522. /* Handle received packets
  523. */
  524. rx_status = (unsigned int) nat->rx[nat->rx_cur].cmdsts;
  525. while ( ( rx_status & OWN ) ) {
  526. rx_len = ( rx_status & DSIZE ) - CRC_SIZE;
  527. /*check for the corrupt packet
  528. */
  529. if ( ( rx_status & ( DescMore|DescPktOK|RxTooLong ) ) != DescPktOK) {
  530. DBG ( "natsemi_poll: Corrupted packet received, "
  531. "buffer status = %X ^ %X \n",rx_status,
  532. (unsigned int) nat->rx[nat->rx_cur].cmdsts );
  533. netdev_rx_err ( netdev,NULL,-EINVAL );
  534. } else {
  535. rx_iob = alloc_iob ( rx_len );
  536. if ( !rx_iob )
  537. /* leave packet for next call to poll
  538. */
  539. goto end;
  540. memcpy ( iob_put ( rx_iob,rx_len ),
  541. nat->iobuf[nat->rx_cur]->data,rx_len );
  542. DBG ( "received packet\n" );
  543. /* add to the receive queue.
  544. */
  545. netdev_rx ( netdev,rx_iob );
  546. }
  547. nat->rx[nat->rx_cur].cmdsts = RX_BUF_SIZE;
  548. nat->rx_cur = ( nat->rx_cur + 1 ) % NUM_RX_DESC;
  549. rx_status = nat->rx[nat->rx_cur].cmdsts;
  550. }
  551. end:
  552. /* re-enable the potentially idle receive state machine
  553. */
  554. outl ( RxOn, nat->ioaddr + ChipCmd );
  555. }
  556. /**
  557. * Enable/disable interrupts
  558. *
  559. * @v netdev Network device
  560. * @v enable Interrupts should be enabled
  561. */
  562. static void nat_irq ( struct net_device *netdev, int enable ) {
  563. struct natsemi_nic *nat = netdev->priv;
  564. outl ( ( enable ? ( RxOk|RxErr|TxOk|TxErr ) :0 ),
  565. nat->ioaddr + IntrMask);
  566. outl ( ( enable ? 1:0 ),nat->ioaddr + IntrEnable );
  567. }
  568. /** natsemi net device operations */
  569. static struct net_device_operations nat_operations = {
  570. .open = nat_open,
  571. .close = nat_close,
  572. .transmit = nat_transmit,
  573. .poll = nat_poll,
  574. .irq = nat_irq,
  575. };
  576. /*
  577. * Probe PCI device
  578. *
  579. * @v pci PCI device
  580. * @v id PCI ID
  581. * @ret rc Return status code
  582. */
  583. static int nat_probe ( struct pci_device *pci,
  584. const struct pci_device_id *id __unused ) {
  585. struct net_device *netdev;
  586. struct natsemi_nic *nat = NULL;
  587. int rc;
  588. int i;
  589. uint8_t ll_addr_encoded[MAX_LL_ADDR_LEN];
  590. uint8_t last = 0;
  591. uint8_t last1 = 0;
  592. uint8_t prev_bytes[2];
  593. /* Allocate net device
  594. */
  595. netdev = alloc_etherdev ( sizeof ( *nat ) );
  596. if ( ! netdev )
  597. return -ENOMEM;
  598. netdev_init ( netdev,&nat_operations );
  599. nat = netdev->priv;
  600. pci_set_drvdata ( pci, netdev );
  601. netdev->dev = &pci->dev;
  602. memset ( nat, 0, sizeof ( *nat ) );
  603. nat->ioaddr = pci->ioaddr;
  604. /* Fix up PCI device
  605. */
  606. adjust_pci_device ( pci );
  607. /* Reset the NIC, set up EEPROM access and read MAC address
  608. */
  609. nat_reset ( nat );
  610. nat_init_eeprom ( nat );
  611. nvs_read ( &nat->eeprom.nvs, EE_MAC-1, prev_bytes, 1 );
  612. nvs_read ( &nat->eeprom.nvs, EE_MAC, ll_addr_encoded, ETH_ALEN );
  613. /* decoding the MAC address read from NVS
  614. * and save it in netdev->ll_addr
  615. */
  616. last = prev_bytes[1] >> 7;
  617. for ( i = 0 ; i < ETH_ALEN ; i++ ) {
  618. last1 = ll_addr_encoded[i] >> 7;
  619. netdev->ll_addr[i] = ll_addr_encoded[i] << 1 | last;
  620. last = last1;
  621. }
  622. /* Register network device
  623. */
  624. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  625. goto err_register_netdev;
  626. return 0;
  627. err_register_netdev:
  628. /* Disable NIC
  629. */
  630. nat_reset ( nat );
  631. /* Free net device
  632. */
  633. netdev_put ( netdev );
  634. return rc;
  635. }
  636. /**
  637. * Remove PCI device
  638. *
  639. * @v pci PCI device
  640. */
  641. static void nat_remove ( struct pci_device *pci ) {
  642. struct net_device *netdev = pci_get_drvdata ( pci );
  643. struct natsemi_nic *nat = netdev->priv;
  644. if ( nat->nvo.nvs )
  645. nvo_unregister ( &nat->nvo );
  646. unregister_netdev ( netdev );
  647. nat_reset ( nat );
  648. netdev_put ( netdev );
  649. }
  650. static struct pci_device_id natsemi_nics[] = {
  651. PCI_ROM(0x100b, 0x0020, "dp83815", "DP83815"),
  652. };
  653. struct pci_driver natsemi_driver __pci_driver = {
  654. .ids = natsemi_nics,
  655. .id_count = ( sizeof ( natsemi_nics ) / sizeof ( natsemi_nics[0] ) ),
  656. .probe = nat_probe,
  657. .remove = nat_remove,
  658. };