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.

3c90x.c 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * 3c90x.c -- This file implements a iPXE API 3c90x driver
  3. *
  4. * Originally written for etherboot by:
  5. * Greg Beeley, Greg.Beeley@LightSys.org
  6. * Modified by Steve Smith,
  7. * Steve.Smith@Juno.Com. Alignment bug fix Neil Newell (nn@icenoir.net).
  8. * Almost totally Rewritten to use iPXE API, implementation of tx/rx ring support
  9. * by Thomas Miletich, thomas.miletich@gmail.com
  10. * Thanks to Marty Connor and Stefan Hajnoczi for their help and feedback,
  11. * and to Daniel Verkamp for his help with testing.
  12. *
  13. * Copyright (c) 2009 Thomas Miletich
  14. *
  15. * Copyright (c) 1999 LightSys Technology Services, Inc.
  16. * Portions Copyright (c) 1999 Steve Smith
  17. *
  18. * This program may be re-distributed in source or binary form, modified,
  19. * sold, or copied for any purpose, provided that the above copyright message
  20. * and this text are included with all source copies or derivative works, and
  21. * provided that the above copyright message and this text are included in the
  22. * documentation of any binary-only distributions. This program is distributed
  23. * WITHOUT ANY WARRANTY, without even the warranty of FITNESS FOR A PARTICULAR
  24. * PURPOSE or MERCHANTABILITY. Please read the associated documentation
  25. * "3c90x.txt" before compiling and using this driver.
  26. *
  27. * [ --mdc 20090313 The 3c90x.txt file is now at:
  28. * http://etherboot.org/wiki/appnotes/3c90x_issues ]
  29. *
  30. * This program was written with the assistance of the 3com documentation for
  31. * the 3c905B-TX card, as well as with some assistance from the 3c59x
  32. * driver Donald Becker wrote for the Linux kernel, and with some assistance
  33. * from the remainder of the Etherboot distribution.
  34. *
  35. * Indented with unix 'indent' command:
  36. * $ indent -kr -i8 3c90x.c
  37. */
  38. FILE_LICENCE ( BSD2 );
  39. #include <stdint.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <stddef.h>
  43. #include <string.h>
  44. #include <unistd.h>
  45. #include <assert.h>
  46. #include <byteswap.h>
  47. #include <errno.h>
  48. #include <ipxe/ethernet.h>
  49. #include <ipxe/if_ether.h>
  50. #include <ipxe/io.h>
  51. #include <ipxe/iobuf.h>
  52. #include <ipxe/malloc.h>
  53. #include <ipxe/netdevice.h>
  54. #include <ipxe/pci.h>
  55. #include <ipxe/timer.h>
  56. #include <ipxe/nvs.h>
  57. #include "3c90x.h"
  58. /**
  59. * a3c90x_internal_IssueCommand: sends a command to the 3c90x card
  60. * and waits for it's completion
  61. *
  62. * @v ioaddr IOAddress of the NIC
  63. * @v cmd Command to be issued
  64. * @v param Command parameter
  65. */
  66. static void a3c90x_internal_IssueCommand(int ioaddr, int cmd, int param)
  67. {
  68. unsigned int val = (cmd << 11) | param;
  69. int cnt = 0;
  70. DBGP("a3c90x_internal_IssueCommand\n");
  71. /* Send the cmd to the cmd register */
  72. outw(val, ioaddr + regCommandIntStatus_w);
  73. /* Wait for the cmd to complete */
  74. for (cnt = 0; cnt < 100000; cnt++) {
  75. if (inw(ioaddr + regCommandIntStatus_w) & INT_CMDINPROGRESS) {
  76. continue;
  77. } else {
  78. DBG2("Command 0x%04X finished in time. cnt = %d.\n", cmd, cnt);
  79. return;
  80. }
  81. }
  82. DBG("Command 0x%04X DID NOT finish in time. cnt = %d.\n", cmd, cnt);
  83. }
  84. /**
  85. * a3c90x_internal_SetWindow: selects a register window set.
  86. *
  87. * @v inf_3c90x private NIC data
  88. * @v window window to be selected
  89. */
  90. static void a3c90x_internal_SetWindow(struct INF_3C90X *inf_3c90x, int window)
  91. {
  92. DBGP("a3c90x_internal_SetWindow\n");
  93. /* Window already as set? */
  94. if (inf_3c90x->CurrentWindow == window)
  95. return;
  96. /* Issue the window command. */
  97. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  98. cmdSelectRegisterWindow, window);
  99. inf_3c90x->CurrentWindow = window;
  100. return;
  101. }
  102. static void a3c90x_internal_WaitForEeprom(struct INF_3C90X *inf_3c90x)
  103. {
  104. int cnt = 0;
  105. DBGP("a3c90x_internal_WaitForEeprom\n");
  106. while (eepromBusy & inw(inf_3c90x->IOAddr + regEepromCommand_0_w)) {
  107. if (cnt == EEPROM_TIMEOUT) {
  108. DBG("Read from eeprom failed: timeout\n");
  109. return;
  110. }
  111. udelay(1);
  112. cnt++;
  113. }
  114. }
  115. /**
  116. * a3c90x_internal_ReadEeprom - nvs routine to read eeprom data
  117. * We only support reading one word(2 byte). The nvs subsystem will make sure
  118. * that the routine will never be called with len != 2.
  119. *
  120. * @v nvs nvs data.
  121. * @v address eeprom address to read data from.
  122. * @v data data is put here.
  123. * @v len number of bytes to read.
  124. */
  125. static int
  126. a3c90x_internal_ReadEeprom(struct nvs_device *nvs, unsigned int address, void *data, size_t len)
  127. {
  128. unsigned short *dest = (unsigned short *) data;
  129. struct INF_3C90X *inf_3c90x =
  130. container_of(nvs, struct INF_3C90X, nvs);
  131. DBGP("a3c90x_internal_ReadEeprom\n");
  132. /* we support reading 2 bytes only */
  133. assert(len == 2);
  134. /* Select correct window */
  135. a3c90x_internal_SetWindow(inf_3c90x, winEepromBios0);
  136. /* set eepromRead bits in command sent to NIC */
  137. address += (inf_3c90x->is3c556 ? eepromRead_556 : eepromRead);
  138. a3c90x_internal_WaitForEeprom(inf_3c90x);
  139. /* send address to NIC */
  140. outw(address, inf_3c90x->IOAddr + regEepromCommand_0_w);
  141. a3c90x_internal_WaitForEeprom(inf_3c90x);
  142. /* read value */
  143. *dest = inw(inf_3c90x->IOAddr + regEepromData_0_w);
  144. return 0;
  145. }
  146. /**
  147. * a3c90x_internal_WriteEeprom - nvs routine to write eeprom data
  148. * currently not implemented
  149. *
  150. * @v nvs nvs data.
  151. * @v address eeprom address to read data from.
  152. * @v data data is put here.
  153. * @v len number of bytes to read.
  154. */
  155. static int
  156. a3c90x_internal_WriteEeprom(struct nvs_device *nvs __unused,
  157. unsigned int address __unused,
  158. const void *data __unused, size_t len __unused)
  159. {
  160. return -ENOTSUP;
  161. }
  162. static void a3c90x_internal_ReadEepromContents(struct INF_3C90X *inf_3c90x)
  163. {
  164. int eeprom_size = (inf_3c90x->isBrev ? 0x20 : 0x17) * 2;
  165. DBGP("a3c90x_internal_ReadEepromContents\n");
  166. nvs_read(&inf_3c90x->nvs, 0, inf_3c90x->eeprom, eeprom_size);
  167. }
  168. /**
  169. * a3c90x_reset: exported function that resets the card to its default
  170. * state. This is so the Linux driver can re-set the card up the way
  171. * it wants to. If CFG_3C90X_PRESERVE_XCVR is defined, then the reset will
  172. * not alter the selected transceiver that we used to download the boot
  173. * image.
  174. *
  175. * @v inf_3c90x Private NIC data
  176. */
  177. static void a3c90x_reset(struct INF_3C90X *inf_3c90x)
  178. {
  179. DBGP("a3c90x_reset\n");
  180. /* Send the reset command to the card */
  181. DBG2("3c90x: Issuing RESET\n");
  182. /* reset of the receiver on B-revision cards re-negotiates the link
  183. * takes several seconds (a computer eternity), so we don't reset
  184. * it here.
  185. */
  186. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  187. cmdGlobalReset,
  188. globalResetMaskNetwork);
  189. /* global reset command resets station mask, non-B revision cards
  190. * require explicit reset of values
  191. */
  192. a3c90x_internal_SetWindow(inf_3c90x, winAddressing2);
  193. outw(0, inf_3c90x->IOAddr + regStationMask_2_3w + 0);
  194. outw(0, inf_3c90x->IOAddr + regStationMask_2_3w + 2);
  195. outw(0, inf_3c90x->IOAddr + regStationMask_2_3w + 4);
  196. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdTxEnable, 0);
  197. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdRxEnable, 0);
  198. /* enable rxComplete and txComplete indications */
  199. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  200. cmdSetIndicationEnable,
  201. INT_TXCOMPLETE | INT_UPCOMPLETE);
  202. /* acknowledge any pending status flags */
  203. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  204. cmdAcknowledgeInterrupt, 0x661);
  205. return;
  206. }
  207. /**
  208. * a3c90x_setup_tx_ring - Allocates TX ring, initialize tx_desc values
  209. *
  210. * @v p Private NIC data
  211. *
  212. * @ret Returns 0 on success, negative on failure
  213. */
  214. static int a3c90x_setup_tx_ring(struct INF_3C90X *p)
  215. {
  216. DBGP("a3c90x_setup_tx_ring\n");
  217. p->tx_ring =
  218. malloc_dma(TX_RING_SIZE * sizeof(struct TXD), TX_RING_ALIGN);
  219. if (!p->tx_ring) {
  220. DBG("Could not allocate TX-ring\n");
  221. return -ENOMEM;
  222. }
  223. memset(p->tx_ring, 0, TX_RING_SIZE * sizeof(struct TXD));
  224. p->tx_cur = 0;
  225. p->tx_cnt = 0;
  226. p->tx_tail = 0;
  227. return 0;
  228. }
  229. /**
  230. * a3c90x_process_tx_packets - Checks for successfully sent packets,
  231. * reports them to iPXE with netdev_tx_complete();
  232. *
  233. * @v netdev Network device info
  234. */
  235. static void a3c90x_process_tx_packets(struct net_device *netdev)
  236. {
  237. struct INF_3C90X *p = netdev_priv(netdev);
  238. unsigned int downlist_ptr;
  239. DBGP("a3c90x_process_tx_packets\n");
  240. DBG2(" tx_cnt: %d\n", p->tx_cnt);
  241. while (p->tx_tail != p->tx_cur) {
  242. downlist_ptr = inl(p->IOAddr + regDnListPtr_l);
  243. DBG2(" downlist_ptr: %#08x\n", downlist_ptr);
  244. DBG2(" tx_tail: %d tx_cur: %d\n", p->tx_tail, p->tx_cur);
  245. /* NIC is currently working on this tx desc */
  246. if(downlist_ptr == virt_to_bus(p->tx_ring + p->tx_tail))
  247. return;
  248. netdev_tx_complete(netdev, p->tx_iobuf[p->tx_tail]);
  249. DBG2("transmitted packet\n");
  250. DBG2(" size: %zd\n", iob_len(p->tx_iobuf[p->tx_tail]));
  251. p->tx_tail = (p->tx_tail + 1) % TX_RING_SIZE;
  252. p->tx_cnt--;
  253. }
  254. }
  255. static void a3c90x_free_tx_ring(struct INF_3C90X *p)
  256. {
  257. DBGP("a3c90x_free_tx_ring\n");
  258. free_dma(p->tx_ring, TX_RING_SIZE * sizeof(struct TXD));
  259. p->tx_ring = NULL;
  260. /* io_buffers are free()ed by netdev_tx_complete[,_err]() */
  261. }
  262. /**
  263. * a3c90x_transmit - Transmits a packet.
  264. *
  265. * @v netdev Network device info
  266. * @v iob io_buffer containing the data to be send
  267. *
  268. * @ret Returns 0 on success, negative on failure
  269. */
  270. static int a3c90x_transmit(struct net_device *netdev,
  271. struct io_buffer *iob)
  272. {
  273. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  274. struct TXD *tx_cur_desc;
  275. struct TXD *tx_prev_desc;
  276. unsigned int len;
  277. unsigned int downlist_ptr;
  278. DBGP("a3c90x_transmit\n");
  279. if (inf_3c90x->tx_cnt == TX_RING_SIZE) {
  280. DBG("TX-Ring overflow\n");
  281. return -ENOBUFS;
  282. }
  283. inf_3c90x->tx_iobuf[inf_3c90x->tx_cur] = iob;
  284. tx_cur_desc = inf_3c90x->tx_ring + inf_3c90x->tx_cur;
  285. tx_prev_desc = inf_3c90x->tx_ring +
  286. (((inf_3c90x->tx_cur + TX_RING_SIZE) - 1) % TX_RING_SIZE);
  287. len = iob_len(iob);
  288. /* Setup the DPD (download descriptor) */
  289. tx_cur_desc->DnNextPtr = 0;
  290. /* FrameStartHeader differs in 90x and >= 90xB
  291. * It contains the packet length in 90x and a round up boundary and
  292. * packet ID for 90xB and 90xC. Disable packet length round-up on the
  293. * later revisions.
  294. */
  295. tx_cur_desc->FrameStartHeader =
  296. fshTxIndicate | (inf_3c90x->isBrev ? fshRndupDefeat : len);
  297. tx_cur_desc->DataAddr = virt_to_bus(iob->data);
  298. tx_cur_desc->DataLength = len | downLastFrag;
  299. /* We have to stall the download engine, so the NIC won't access the
  300. * tx descriptor while we modify it. There is a way around this
  301. * from revision B and upwards. To stay compatible with older revisions
  302. * we don't use it here.
  303. */
  304. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdStallCtl,
  305. dnStall);
  306. tx_prev_desc->DnNextPtr = virt_to_bus(tx_cur_desc);
  307. downlist_ptr = inl(inf_3c90x->IOAddr + regDnListPtr_l);
  308. if (downlist_ptr == 0) {
  309. /* currently no DownList, sending a new one */
  310. outl(virt_to_bus(tx_cur_desc),
  311. inf_3c90x->IOAddr + regDnListPtr_l);
  312. }
  313. /* End Stall */
  314. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdStallCtl,
  315. dnUnStall);
  316. inf_3c90x->tx_cur = (inf_3c90x->tx_cur + 1) % TX_RING_SIZE;
  317. inf_3c90x->tx_cnt++;
  318. return 0;
  319. }
  320. /**
  321. * a3c90x_prepare_rx_desc - fills the rx desc with initial data
  322. *
  323. * @v p NIC private data
  324. * @v index Index for rx_iobuf and rx_ring array
  325. */
  326. static void a3c90x_prepare_rx_desc(struct INF_3C90X *p, unsigned int index)
  327. {
  328. DBGP("a3c90x_prepare_rx_desc\n");
  329. DBG2("Populating rx_desc %d\n", index);
  330. /* We have to stall the upload engine, so the NIC won't access the
  331. * rx descriptor while we modify it. There is a way around this
  332. * from revision B and upwards. To stay compatible with older revisions
  333. * we don't use it here.
  334. */
  335. a3c90x_internal_IssueCommand(p->IOAddr, cmdStallCtl, upStall);
  336. p->rx_ring[index].DataAddr = virt_to_bus(p->rx_iobuf[index]->data);
  337. p->rx_ring[index].DataLength = RX_BUF_SIZE | upLastFrag;
  338. p->rx_ring[index].UpPktStatus = 0;
  339. /* unstall upload engine */
  340. a3c90x_internal_IssueCommand(p->IOAddr, cmdStallCtl, upUnStall);
  341. }
  342. /**
  343. * a3c90x_refill_rx_ring -checks every entry in the rx ring and reallocates
  344. * them as necessary. Then it calls a3c90x_prepare_rx_desc to fill the rx desc
  345. * with initial data.
  346. *
  347. * @v p NIC private data
  348. */
  349. static void a3c90x_refill_rx_ring(struct INF_3C90X *p)
  350. {
  351. int i;
  352. unsigned int status;
  353. struct RXD *rx_cur_desc;
  354. DBGP("a3c90x_refill_rx_ring\n");
  355. for (i = 0; i < RX_RING_SIZE; i++) {
  356. rx_cur_desc = p->rx_ring + i;
  357. status = rx_cur_desc->UpPktStatus;
  358. /* only refill used descriptor */
  359. if (!(status & upComplete))
  360. continue;
  361. /* we still need to process this descriptor */
  362. if (p->rx_iobuf[i] != NULL)
  363. continue;
  364. p->rx_iobuf[i] = alloc_iob(RX_BUF_SIZE);
  365. if (p->rx_iobuf[i] == NULL) {
  366. DBG("alloc_iob() failed\n");
  367. break;
  368. }
  369. a3c90x_prepare_rx_desc(p, i);
  370. }
  371. }
  372. /**
  373. * a3c90x_setup_rx_ring - Allocates RX ring, initialize rx_desc values
  374. *
  375. * @v p Private NIC data
  376. *
  377. * @ret Returns 0 on success, negative on failure
  378. */
  379. static int a3c90x_setup_rx_ring(struct INF_3C90X *p)
  380. {
  381. int i;
  382. DBGP("a3c90x_setup_rx_ring\n");
  383. p->rx_ring =
  384. malloc_dma(RX_RING_SIZE * sizeof(struct RXD), RX_RING_ALIGN);
  385. if (!p->rx_ring) {
  386. DBG("Could not allocate RX-ring\n");
  387. return -ENOMEM;
  388. }
  389. p->rx_cur = 0;
  390. for (i = 0; i < RX_RING_SIZE; i++) {
  391. p->rx_ring[i].UpNextPtr =
  392. virt_to_bus(p->rx_ring + (i + 1));
  393. /* these are needed so refill_rx_ring initializes the ring */
  394. p->rx_ring[i].UpPktStatus = upComplete;
  395. p->rx_iobuf[i] = NULL;
  396. }
  397. /* Loop the ring */
  398. p->rx_ring[i - 1].UpNextPtr = virt_to_bus(p->rx_ring);
  399. a3c90x_refill_rx_ring(p);
  400. return 0;
  401. }
  402. static void a3c90x_free_rx_ring(struct INF_3C90X *p)
  403. {
  404. DBGP("a3c90x_free_rx_ring\n");
  405. free_dma(p->rx_ring, RX_RING_SIZE * sizeof(struct RXD));
  406. p->rx_ring = NULL;
  407. }
  408. static void a3c90x_free_rx_iobuf(struct INF_3C90X *p)
  409. {
  410. int i;
  411. DBGP("a3c90x_free_rx_iobuf\n");
  412. for (i = 0; i < RX_RING_SIZE; i++) {
  413. free_iob(p->rx_iobuf[i]);
  414. p->rx_iobuf[i] = NULL;
  415. }
  416. }
  417. /**
  418. * a3c90x_process_rx_packets - Checks for received packets,
  419. * reports them to iPXE with netdev_rx() or netdev_rx_err() if there was an
  420. * error while receiving the packet
  421. *
  422. * @v netdev Network device info
  423. */
  424. static void a3c90x_process_rx_packets(struct net_device *netdev)
  425. {
  426. int i;
  427. unsigned int rx_status;
  428. struct INF_3C90X *p = netdev_priv(netdev);
  429. struct RXD *rx_cur_desc;
  430. DBGP("a3c90x_process_rx_packets\n");
  431. for (i = 0; i < RX_RING_SIZE; i++) {
  432. rx_cur_desc = p->rx_ring + p->rx_cur;
  433. rx_status = rx_cur_desc->UpPktStatus;
  434. if (!(rx_status & upComplete) && !(rx_status & upError))
  435. break;
  436. if (p->rx_iobuf[p->rx_cur] == NULL)
  437. break;
  438. if (rx_status & upError) {
  439. DBG("Corrupted packet received: %#x\n", rx_status);
  440. netdev_rx_err(netdev, p->rx_iobuf[p->rx_cur],
  441. -EINVAL);
  442. } else {
  443. /* if we're here, we've got good packet */
  444. int packet_len;
  445. packet_len = rx_status & 0x1FFF;
  446. iob_put(p->rx_iobuf[p->rx_cur], packet_len);
  447. DBG2("received packet\n");
  448. DBG2(" size: %d\n", packet_len);
  449. netdev_rx(netdev, p->rx_iobuf[p->rx_cur]);
  450. }
  451. p->rx_iobuf[p->rx_cur] = NULL; /* invalidate rx desc */
  452. p->rx_cur = (p->rx_cur + 1) % RX_RING_SIZE;
  453. }
  454. a3c90x_refill_rx_ring(p);
  455. }
  456. /**
  457. * a3c90x_poll - Routine that gets called periodically.
  458. * Here we hanle transmitted and received packets.
  459. * We could also check the link status from time to time, which we
  460. * currently don't do.
  461. *
  462. * @v netdev Network device info
  463. */
  464. static void a3c90x_poll(struct net_device *netdev)
  465. {
  466. struct INF_3C90X *p = netdev_priv(netdev);
  467. uint16_t raw_status, int_status;
  468. DBGP("a3c90x_poll\n");
  469. raw_status = inw(p->IOAddr + regCommandIntStatus_w);
  470. int_status = (raw_status & 0x0FFF);
  471. if ( int_status == 0 )
  472. return;
  473. a3c90x_internal_IssueCommand(p->IOAddr, cmdAcknowledgeInterrupt,
  474. int_status);
  475. if (int_status & INT_TXCOMPLETE)
  476. outb(0x00, p->IOAddr + regTxStatus_b);
  477. DBG2("poll: status = %#04x\n", raw_status);
  478. a3c90x_process_tx_packets(netdev);
  479. a3c90x_process_rx_packets(netdev);
  480. }
  481. static void a3c90x_free_resources(struct INF_3C90X *p)
  482. {
  483. DBGP("a3c90x_free_resources\n");
  484. a3c90x_free_tx_ring(p);
  485. a3c90x_free_rx_ring(p);
  486. a3c90x_free_rx_iobuf(p);
  487. }
  488. /**
  489. * a3c90x_remove - Routine to remove the card. Unregisters
  490. * the NIC from iPXE, disables RX/TX and resets the card.
  491. *
  492. * @v pci PCI device info
  493. */
  494. static void a3c90x_remove(struct pci_device *pci)
  495. {
  496. struct net_device *netdev = pci_get_drvdata(pci);
  497. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  498. DBGP("a3c90x_remove\n");
  499. a3c90x_reset(inf_3c90x);
  500. /* Disable the receiver and transmitter. */
  501. outw(cmdRxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  502. outw(cmdTxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  503. unregister_netdev(netdev);
  504. netdev_nullify(netdev);
  505. netdev_put(netdev);
  506. }
  507. static void a3c90x_irq(struct net_device *netdev, int enable)
  508. {
  509. struct INF_3C90X *p = netdev_priv(netdev);
  510. DBGP("a3c90x_irq\n");
  511. if (enable == 0) {
  512. /* disable interrupts */
  513. a3c90x_internal_IssueCommand(p->IOAddr,
  514. cmdSetInterruptEnable, 0);
  515. } else {
  516. a3c90x_internal_IssueCommand(p->IOAddr,
  517. cmdSetInterruptEnable,
  518. INT_TXCOMPLETE |
  519. INT_UPCOMPLETE);
  520. a3c90x_internal_IssueCommand(p->IOAddr,
  521. cmdAcknowledgeInterrupt,
  522. 0x661);
  523. }
  524. }
  525. /**
  526. * a3c90x_hw_start - Initialize hardware, copy MAC address
  527. * to NIC registers, set default receiver
  528. */
  529. static void a3c90x_hw_start(struct net_device *netdev)
  530. {
  531. int i, c;
  532. unsigned int cfg;
  533. unsigned int mopt;
  534. unsigned short linktype;
  535. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  536. DBGP("a3c90x_hw_start\n");
  537. /* 3C556: Invert MII power */
  538. if (inf_3c90x->is3c556) {
  539. unsigned int tmp;
  540. a3c90x_internal_SetWindow(inf_3c90x, winAddressing2);
  541. tmp = inw(inf_3c90x->IOAddr + regResetOptions_2_w);
  542. tmp |= 0x4000;
  543. outw(tmp, inf_3c90x->IOAddr + regResetOptions_2_w);
  544. }
  545. /* Copy MAC address into the NIC registers */
  546. a3c90x_internal_SetWindow(inf_3c90x, winAddressing2);
  547. for (i = 0; i < ETH_ALEN; i++)
  548. outb(netdev->ll_addr[i],
  549. inf_3c90x->IOAddr + regStationAddress_2_3w + i);
  550. for (i = 0; i < ETH_ALEN; i++)
  551. outb(0, inf_3c90x->IOAddr + regStationMask_2_3w + i);
  552. /* Read the media options register, print a message and set default
  553. * xcvr.
  554. *
  555. * Uses Media Option command on B revision, Reset Option on non-B
  556. * revision cards -- same register address
  557. */
  558. a3c90x_internal_SetWindow(inf_3c90x, winTxRxOptions3);
  559. mopt = inw(inf_3c90x->IOAddr + regResetMediaOptions_3_w);
  560. /* mask out VCO bit that is defined as 10baseFL bit on B-rev cards */
  561. if (!inf_3c90x->isBrev) {
  562. mopt &= 0x7F;
  563. }
  564. DBG2("Connectors present: ");
  565. c = 0;
  566. linktype = 0x0008;
  567. if (mopt & 0x01) {
  568. DBG2("%s100Base-T4", (c++) ? ", " : "");
  569. linktype = linkMII;
  570. }
  571. if (mopt & 0x04) {
  572. DBG2("%s100Base-FX", (c++) ? ", " : "");
  573. linktype = link100BaseFX;
  574. }
  575. if (mopt & 0x10) {
  576. DBG2("%s10Base-2", (c++) ? ", " : "");
  577. linktype = link10Base2;
  578. }
  579. if (mopt & 0x20) {
  580. DBG2("%sAUI", (c++) ? ", " : "");
  581. linktype = linkAUI;
  582. }
  583. if (mopt & 0x40) {
  584. DBG2("%sMII", (c++) ? ", " : "");
  585. linktype = linkMII;
  586. }
  587. if ((mopt & 0xA) == 0xA) {
  588. DBG2("%s10Base-T / 100Base-TX", (c++) ? ", " : "");
  589. linktype = linkAutoneg;
  590. } else if ((mopt & 0xA) == 0x2) {
  591. DBG2("%s100Base-TX", (c++) ? ", " : "");
  592. linktype = linkAutoneg;
  593. } else if ((mopt & 0xA) == 0x8) {
  594. DBG2("%s10Base-T", (c++) ? ", " : "");
  595. linktype = linkAutoneg;
  596. }
  597. DBG2(".\n");
  598. /* Determine transceiver type to use, depending on value stored in
  599. * eeprom 0x16
  600. */
  601. if (inf_3c90x->isBrev) {
  602. if ((inf_3c90x->eeprom[0x16] & 0xFF00) == XCVR_MAGIC) {
  603. /* User-defined */
  604. linktype = inf_3c90x->eeprom[0x16] & 0x000F;
  605. }
  606. } else {
  607. /* I don't know what MII MAC only mode is!!! */
  608. if (linktype == linkExternalMII) {
  609. if (inf_3c90x->isBrev)
  610. DBG("WARNING: MII External MAC Mode only supported on B-revision " "cards!!!!\nFalling Back to MII Mode\n");
  611. linktype = linkMII;
  612. }
  613. }
  614. /* enable DC converter for 10-Base-T */
  615. if (linktype == link10Base2) {
  616. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  617. cmdEnableDcConverter, 0);
  618. }
  619. /* Set the link to the type we just determined. */
  620. a3c90x_internal_SetWindow(inf_3c90x, winTxRxOptions3);
  621. cfg = inl(inf_3c90x->IOAddr + regInternalConfig_3_l);
  622. cfg &= ~(0xF << 20);
  623. cfg |= (linktype << 20);
  624. DBG2("Setting internal cfg register: 0x%08X (linktype: 0x%02X)\n",
  625. cfg, linktype);
  626. outl(cfg, inf_3c90x->IOAddr + regInternalConfig_3_l);
  627. /* Now that we set the xcvr type, reset the Tx and Rx */
  628. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdTxReset, 0x00);
  629. if (!inf_3c90x->isBrev)
  630. outb(0x01, inf_3c90x->IOAddr + regTxFreeThresh_b);
  631. /* Set the RX filter = receive only individual pkts & multicast & bcast. */
  632. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdSetRxFilter,
  633. 0x01 + 0x02 + 0x04);
  634. /*
  635. * set Indication and Interrupt flags , acknowledge any IRQ's
  636. */
  637. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  638. cmdSetInterruptEnable,
  639. INT_TXCOMPLETE | INT_UPCOMPLETE);
  640. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  641. cmdSetIndicationEnable,
  642. INT_TXCOMPLETE | INT_UPCOMPLETE);
  643. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  644. cmdAcknowledgeInterrupt, 0x661);
  645. }
  646. /**
  647. * a3c90x_open - Routine to initialize the card. Initialize hardware,
  648. * allocate TX and RX ring, send RX ring address to the NIC.
  649. *
  650. * @v netdev Network device info
  651. *
  652. * @ret Returns 0 on success, negative on failure
  653. */
  654. static int a3c90x_open(struct net_device *netdev)
  655. {
  656. int rc;
  657. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  658. DBGP("a3c90x_open\n");
  659. a3c90x_hw_start(netdev);
  660. rc = a3c90x_setup_tx_ring(inf_3c90x);
  661. if (rc != 0) {
  662. DBG("Error setting up TX Ring\n");
  663. goto error;
  664. }
  665. rc = a3c90x_setup_rx_ring(inf_3c90x);
  666. if (rc != 0) {
  667. DBG("Error setting up RX Ring\n");
  668. goto error;
  669. }
  670. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdStallCtl, upStall);
  671. /* send rx_ring address to NIC */
  672. outl(virt_to_bus(inf_3c90x->rx_ring),
  673. inf_3c90x->IOAddr + regUpListPtr_l);
  674. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdStallCtl, upUnStall);
  675. /* set maximum allowed receive packet length */
  676. a3c90x_internal_SetWindow(inf_3c90x, winTxRxOptions3);
  677. outl(RX_BUF_SIZE, inf_3c90x->IOAddr + regMaxPktSize_3_w);
  678. /* enable packet transmission and reception */
  679. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdTxEnable, 0);
  680. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdRxEnable, 0);
  681. return 0;
  682. error:
  683. a3c90x_free_resources(inf_3c90x);
  684. a3c90x_reset(inf_3c90x);
  685. return rc;
  686. }
  687. /**
  688. * a3c90x_close - free()s TX and RX ring, disablex RX/TX, resets NIC
  689. *
  690. * @v netdev Network device info
  691. */
  692. static void a3c90x_close(struct net_device *netdev)
  693. {
  694. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  695. DBGP("a3c90x_close\n");
  696. a3c90x_reset(inf_3c90x);
  697. outw(cmdRxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  698. outw(cmdTxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  699. a3c90x_free_resources(inf_3c90x);
  700. }
  701. static struct net_device_operations a3c90x_operations = {
  702. .open = a3c90x_open,
  703. .close = a3c90x_close,
  704. .poll = a3c90x_poll,
  705. .transmit = a3c90x_transmit,
  706. .irq = a3c90x_irq,
  707. };
  708. /**
  709. * a3c90x_probe: exported routine to probe for the 3c905 card.
  710. * If this routine is called, the pci functions did find the
  711. * card. We read the eeprom here and get the MAC address.
  712. * Initialization is done in a3c90x_open().
  713. *
  714. * @v pci PCI device info
  715. * @ pci_id PCI device IDs
  716. *
  717. * @ret rc Returns 0 on success, negative on failure
  718. */
  719. static int a3c90x_probe(struct pci_device *pci)
  720. {
  721. struct net_device *netdev;
  722. struct INF_3C90X *inf_3c90x;
  723. unsigned char *HWAddr;
  724. int rc;
  725. DBGP("a3c90x_probe\n");
  726. if (pci->ioaddr == 0)
  727. return -EINVAL;
  728. netdev = alloc_etherdev(sizeof(*inf_3c90x));
  729. if (!netdev)
  730. return -ENOMEM;
  731. netdev_init(netdev, &a3c90x_operations);
  732. pci_set_drvdata(pci, netdev);
  733. netdev->dev = &pci->dev;
  734. inf_3c90x = netdev_priv(netdev);
  735. memset(inf_3c90x, 0, sizeof(*inf_3c90x));
  736. adjust_pci_device(pci);
  737. inf_3c90x->is3c556 = (pci->device == 0x6055);
  738. inf_3c90x->IOAddr = pci->ioaddr;
  739. inf_3c90x->CurrentWindow = winNone;
  740. inf_3c90x->isBrev = 1;
  741. switch (pci->device) {
  742. case 0x9000: /* 10 Base TPO */
  743. case 0x9001: /* 10/100 T4 */
  744. case 0x9050: /* 10/100 TPO */
  745. case 0x9051: /* 10 Base Combo */
  746. inf_3c90x->isBrev = 0;
  747. break;
  748. }
  749. DBG2("[3c90x]: found NIC(0x%04X, 0x%04X), isBrev=%d, is3c556=%d\n",
  750. pci->vendor, pci->device, inf_3c90x->isBrev,
  751. inf_3c90x->is3c556);
  752. /* initialize nvs device */
  753. inf_3c90x->nvs.word_len_log2 = 1; /* word */
  754. inf_3c90x->nvs.size = (inf_3c90x->isBrev ? 0x20 : 0x17);
  755. inf_3c90x->nvs.block_size = 1;
  756. inf_3c90x->nvs.read = a3c90x_internal_ReadEeprom;
  757. inf_3c90x->nvs.write = a3c90x_internal_WriteEeprom;
  758. /* reset NIC before accessing any data from it */
  759. a3c90x_reset(inf_3c90x);
  760. /* load eeprom contents to inf_3c90x->eeprom */
  761. a3c90x_internal_ReadEepromContents(inf_3c90x);
  762. HWAddr = netdev->hw_addr;
  763. /* Retrieve the Hardware address */
  764. HWAddr[0] = inf_3c90x->eeprom[eepromHwAddrOffset + 0] >> 8;
  765. HWAddr[1] = inf_3c90x->eeprom[eepromHwAddrOffset + 0] & 0xFF;
  766. HWAddr[2] = inf_3c90x->eeprom[eepromHwAddrOffset + 1] >> 8;
  767. HWAddr[3] = inf_3c90x->eeprom[eepromHwAddrOffset + 1] & 0xFF;
  768. HWAddr[4] = inf_3c90x->eeprom[eepromHwAddrOffset + 2] >> 8;
  769. HWAddr[5] = inf_3c90x->eeprom[eepromHwAddrOffset + 2] & 0xFF;
  770. if ((rc = register_netdev(netdev)) != 0) {
  771. DBG("3c90x: register_netdev() failed\n");
  772. netdev_put(netdev);
  773. return rc;
  774. }
  775. /* we don't handle linkstates yet, so we're always up */
  776. netdev_link_up(netdev);
  777. return 0;
  778. }
  779. static struct pci_device_id a3c90x_nics[] = {
  780. /* Original 90x revisions: */
  781. PCI_ROM(0x10b7, 0x6055, "3c556", "3C556", 0), /* Huricane */
  782. PCI_ROM(0x10b7, 0x9000, "3c905-tpo", "3Com900-TPO", 0), /* 10 Base TPO */
  783. PCI_ROM(0x10b7, 0x9001, "3c905-t4", "3Com900-Combo", 0), /* 10/100 T4 */
  784. PCI_ROM(0x10b7, 0x9050, "3c905-tpo100", "3Com905-TX", 0), /* 100 Base TX / 10/100 TPO */
  785. PCI_ROM(0x10b7, 0x9051, "3c905-combo", "3Com905-T4", 0), /* 100 Base T4 / 10 Base Combo */
  786. /* Newer 90xB revisions: */
  787. PCI_ROM(0x10b7, 0x9004, "3c905b-tpo", "3Com900B-TPO", 0), /* 10 Base TPO */
  788. PCI_ROM(0x10b7, 0x9005, "3c905b-combo", "3Com900B-Combo", 0), /* 10 Base Combo */
  789. PCI_ROM(0x10b7, 0x9006, "3c905b-tpb2", "3Com900B-2/T", 0), /* 10 Base TP and Base2 */
  790. PCI_ROM(0x10b7, 0x900a, "3c905b-fl", "3Com900B-FL", 0), /* 10 Base FL */
  791. PCI_ROM(0x10b7, 0x9055, "3c905b-tpo100", "3Com905B-TX", 0), /* 10/100 TPO */
  792. PCI_ROM(0x10b7, 0x9056, "3c905b-t4", "3Com905B-T4", 0), /* 10/100 T4 */
  793. PCI_ROM(0x10b7, 0x9058, "3c905b-9058", "3Com905B-9058", 0), /* Cyclone 10/100/BNC */
  794. PCI_ROM(0x10b7, 0x905a, "3c905b-fx", "3Com905B-FL", 0), /* 100 Base FX / 10 Base FX */
  795. /* Newer 90xC revision: */
  796. PCI_ROM(0x10b7, 0x9200, "3c905c-tpo", "3Com905C-TXM", 0), /* 10/100 TPO (3C905C-TXM) */
  797. PCI_ROM(0x10b7, 0x9202, "3c920b-emb-ati", "3c920B-EMB-WNM (ATI Radeon 9100 IGP)", 0), /* 3c920B-EMB-WNM (ATI Radeon 9100 IGP) */
  798. PCI_ROM(0x10b7, 0x9210, "3c920b-emb-wnm", "3Com20B-EMB WNM", 0),
  799. PCI_ROM(0x10b7, 0x9800, "3c980", "3Com980-Cyclone", 0), /* Cyclone */
  800. PCI_ROM(0x10b7, 0x9805, "3c9805", "3Com9805", 0), /* Dual Port Server Cyclone */
  801. PCI_ROM(0x10b7, 0x7646, "3csoho100-tx", "3CSOHO100-TX", 0), /* Hurricane */
  802. PCI_ROM(0x10b7, 0x4500, "3c450", "3Com450 HomePNA Tornado", 0),
  803. PCI_ROM(0x10b7, 0x1201, "3c982a", "3Com982A", 0),
  804. PCI_ROM(0x10b7, 0x1202, "3c982b", "3Com982B", 0),
  805. };
  806. struct pci_driver a3c90x_driver __pci_driver = {
  807. .ids = a3c90x_nics,
  808. .id_count = (sizeof(a3c90x_nics) / sizeof(a3c90x_nics[0])),
  809. .probe = a3c90x_probe,
  810. .remove = a3c90x_remove,
  811. };
  812. /*
  813. * Local variables:
  814. * c-basic-offset: 8
  815. * c-indent-level: 8
  816. * tab-width: 8
  817. * End:
  818. */