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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. * 3c90x.c -- This file implements a gPXE 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 gPXE 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 <gpxe/ethernet.h>
  49. #include <gpxe/if_ether.h>
  50. #include <gpxe/io.h>
  51. #include <gpxe/iobuf.h>
  52. #include <gpxe/malloc.h>
  53. #include <gpxe/netdevice.h>
  54. #include <gpxe/pci.h>
  55. #include <gpxe/timer.h>
  56. #include <gpxe/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. DBG("3c90x: Issuing RESET\n");
  182. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdGlobalReset, 0);
  183. /* global reset command resets station mask, non-B revision cards
  184. * require explicit reset of values
  185. */
  186. a3c90x_internal_SetWindow(inf_3c90x, winAddressing2);
  187. outw(0, inf_3c90x->IOAddr + regStationMask_2_3w + 0);
  188. outw(0, inf_3c90x->IOAddr + regStationMask_2_3w + 2);
  189. outw(0, inf_3c90x->IOAddr + regStationMask_2_3w + 4);
  190. /* Issue transmit reset, wait for command completion */
  191. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdTxReset, 0);
  192. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdTxEnable, 0);
  193. /*
  194. * reset of the receiver on B-revision cards re-negotiates the link
  195. * takes several seconds (a computer eternity)
  196. */
  197. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdRxReset,
  198. inf_3c90x->isBrev ? 0x04 : 0x00);
  199. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdRxEnable, 0);
  200. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  201. cmdSetInterruptEnable, 0);
  202. /* enable rxComplete and txComplete */
  203. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  204. cmdSetIndicationEnable,
  205. INT_TXCOMPLETE | INT_UPCOMPLETE);
  206. /* acknowledge any pending status flags */
  207. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  208. cmdAcknowledgeInterrupt, 0x661);
  209. return;
  210. }
  211. /**
  212. * a3c90x_setup_tx_ring - Allocates TX ring, initialize tx_desc values
  213. *
  214. * @v p Private NIC data
  215. *
  216. * @ret Returns 0 on success, negative on failure
  217. */
  218. static int a3c90x_setup_tx_ring(struct INF_3C90X *p)
  219. {
  220. DBGP("a3c90x_setup_tx_ring\n");
  221. p->tx_ring =
  222. malloc_dma(TX_RING_SIZE * sizeof(struct TXD), TX_RING_ALIGN);
  223. if (!p->tx_ring) {
  224. DBG("Could not allocate TX-ring\n");
  225. return -ENOMEM;
  226. }
  227. memset(p->tx_ring, 0, TX_RING_SIZE * sizeof(struct TXD));
  228. p->tx_cur = 0;
  229. p->tx_cnt = 0;
  230. p->tx_tail = 0;
  231. return 0;
  232. }
  233. /**
  234. * a3c90x_process_tx_packets - Checks for successfully sent packets,
  235. * reports them to gPXE with netdev_tx_complete();
  236. *
  237. * @v netdev Network device info
  238. */
  239. static void a3c90x_process_tx_packets(struct net_device *netdev)
  240. {
  241. struct INF_3C90X *p = netdev_priv(netdev);
  242. unsigned int downlist_ptr;
  243. DBGP("a3c90x_process_tx_packets\n");
  244. DBG(" tx_cnt: %d\n", p->tx_cnt);
  245. while (p->tx_tail != p->tx_cur) {
  246. downlist_ptr = inl(p->IOAddr + regDnListPtr_l);
  247. DBG(" downlist_ptr: %#08x\n", downlist_ptr);
  248. DBG(" tx_tail: %d tx_cur: %d\n", p->tx_tail, p->tx_cur);
  249. /* NIC is currently working on this tx desc */
  250. if(downlist_ptr == virt_to_bus(p->tx_ring + p->tx_tail))
  251. return;
  252. netdev_tx_complete(netdev, p->tx_iobuf[p->tx_tail]);
  253. DBG("transmitted packet\n");
  254. DBG(" size: %zd\n", iob_len(p->tx_iobuf[p->tx_tail]));
  255. p->tx_tail = (p->tx_tail + 1) % TX_RING_SIZE;
  256. p->tx_cnt--;
  257. }
  258. }
  259. static void a3c90x_free_tx_ring(struct INF_3C90X *p)
  260. {
  261. DBGP("a3c90x_free_tx_ring\n");
  262. free_dma(p->tx_ring, TX_RING_SIZE * sizeof(struct TXD));
  263. p->tx_ring = NULL;
  264. /* io_buffers are free()ed by netdev_tx_complete[,_err]() */
  265. }
  266. /**
  267. * a3c90x_transmit - Transmits a packet.
  268. *
  269. * @v netdev Network device info
  270. * @v iob io_buffer containing the data to be send
  271. *
  272. * @ret Returns 0 on success, negative on failure
  273. */
  274. static int a3c90x_transmit(struct net_device *netdev,
  275. struct io_buffer *iob)
  276. {
  277. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  278. struct TXD *tx_cur_desc;
  279. struct TXD *tx_prev_desc;
  280. unsigned int len;
  281. unsigned int downlist_ptr;
  282. DBGP("a3c90x_transmit\n");
  283. if (inf_3c90x->tx_cnt == TX_RING_SIZE) {
  284. DBG("TX-Ring overflow\n");
  285. return -ENOBUFS;
  286. }
  287. inf_3c90x->tx_iobuf[inf_3c90x->tx_cur] = iob;
  288. tx_cur_desc = inf_3c90x->tx_ring + inf_3c90x->tx_cur;
  289. tx_prev_desc = inf_3c90x->tx_ring +
  290. (((inf_3c90x->tx_cur + TX_RING_SIZE) - 1) % TX_RING_SIZE);
  291. len = iob_len(iob);
  292. /* Setup the DPD (download descriptor) */
  293. tx_cur_desc->DnNextPtr = 0;
  294. /* FrameStartHeader differs in 90x and >= 90xB
  295. * It contains length in 90x and a round up boundary and packet ID for
  296. * 90xB and 90xC. We can leave this to 0 for 90xB and 90xC.
  297. */
  298. tx_cur_desc->FrameStartHeader =
  299. fshTxIndicate | (inf_3c90x->isBrev ? 0x00 : len);
  300. tx_cur_desc->DataAddr = virt_to_bus(iob->data);
  301. tx_cur_desc->DataLength = len | downLastFrag;
  302. /* We have to stall the download engine, so the NIC won't access the
  303. * tx descriptor while we modify it. There is a way around this
  304. * from revision B and upwards. To stay compatible with older revisions
  305. * we don't use it here.
  306. */
  307. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdStallCtl,
  308. dnStall);
  309. tx_prev_desc->DnNextPtr = virt_to_bus(tx_cur_desc);
  310. downlist_ptr = inl(inf_3c90x->IOAddr + regDnListPtr_l);
  311. if (downlist_ptr == 0) {
  312. /* currently no DownList, sending a new one */
  313. outl(virt_to_bus(tx_cur_desc),
  314. inf_3c90x->IOAddr + regDnListPtr_l);
  315. }
  316. /* End Stall */
  317. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdStallCtl,
  318. dnUnStall);
  319. inf_3c90x->tx_cur = (inf_3c90x->tx_cur + 1) % TX_RING_SIZE;
  320. inf_3c90x->tx_cnt++;
  321. return 0;
  322. }
  323. /**
  324. * a3c90x_prepare_rx_desc - fills the rx desc with initial data
  325. *
  326. * @v p NIC private data
  327. * @v index Index for rx_iobuf and rx_ring array
  328. */
  329. static void a3c90x_prepare_rx_desc(struct INF_3C90X *p, unsigned int index)
  330. {
  331. DBGP("a3c90x_prepare_rx_desc\n");
  332. DBG("Populating rx_desc %d\n", index);
  333. /* We have to stall the upload engine, so the NIC won't access the
  334. * rx descriptor while we modify it. There is a way around this
  335. * from revision B and upwards. To stay compatible with older revisions
  336. * we don't use it here.
  337. */
  338. a3c90x_internal_IssueCommand(p->IOAddr, cmdStallCtl, upStall);
  339. p->rx_ring[index].DataAddr = virt_to_bus(p->rx_iobuf[index]->data);
  340. p->rx_ring[index].DataLength = RX_BUF_SIZE | upLastFrag;
  341. p->rx_ring[index].UpPktStatus = 0;
  342. /* unstall upload engine */
  343. a3c90x_internal_IssueCommand(p->IOAddr, cmdStallCtl, upUnStall);
  344. }
  345. /**
  346. * a3c90x_refill_rx_ring -checks every entry in the rx ring and reallocates
  347. * them as necessary. Then it calls a3c90x_prepare_rx_desc to fill the rx desc
  348. * with initial data.
  349. *
  350. * @v p NIC private data
  351. */
  352. static void a3c90x_refill_rx_ring(struct INF_3C90X *p)
  353. {
  354. int i;
  355. unsigned int status;
  356. struct RXD *rx_cur_desc;
  357. DBGP("a3c90x_refill_rx_ring\n");
  358. for (i = 0; i < RX_RING_SIZE; i++) {
  359. rx_cur_desc = p->rx_ring + i;
  360. status = rx_cur_desc->UpPktStatus;
  361. /* only refill used descriptor */
  362. if (!(status & upComplete))
  363. continue;
  364. /* we still need to process this descriptor */
  365. if (p->rx_iobuf[i] != NULL)
  366. continue;
  367. p->rx_iobuf[i] = alloc_iob(RX_BUF_SIZE);
  368. if (p->rx_iobuf[i] == NULL) {
  369. DBG("alloc_iob() failed\n");
  370. break;
  371. }
  372. a3c90x_prepare_rx_desc(p, i);
  373. }
  374. }
  375. /**
  376. * a3c90x_setup_rx_ring - Allocates RX ring, initialize rx_desc values
  377. *
  378. * @v p Private NIC data
  379. *
  380. * @ret Returns 0 on success, negative on failure
  381. */
  382. static int a3c90x_setup_rx_ring(struct INF_3C90X *p)
  383. {
  384. int i;
  385. DBGP("a3c90x_setup_rx_ring\n");
  386. p->rx_ring =
  387. malloc_dma(RX_RING_SIZE * sizeof(struct RXD), RX_RING_ALIGN);
  388. if (!p->rx_ring) {
  389. DBG("Could not allocate RX-ring\n");
  390. return -ENOMEM;
  391. }
  392. p->rx_cur = 0;
  393. for (i = 0; i < RX_RING_SIZE; i++) {
  394. p->rx_ring[i].UpNextPtr =
  395. virt_to_bus(p->rx_ring + (i + 1));
  396. /* these are needed so refill_rx_ring initializes the ring */
  397. p->rx_ring[i].UpPktStatus = upComplete;
  398. p->rx_iobuf[i] = NULL;
  399. }
  400. /* Loop the ring */
  401. p->rx_ring[i - 1].UpNextPtr = virt_to_bus(p->rx_ring);
  402. a3c90x_refill_rx_ring(p);
  403. return 0;
  404. }
  405. static void a3c90x_free_rx_ring(struct INF_3C90X *p)
  406. {
  407. DBGP("a3c90x_free_rx_ring\n");
  408. free_dma(p->rx_ring, RX_RING_SIZE * sizeof(struct RXD));
  409. p->rx_ring = NULL;
  410. }
  411. static void a3c90x_free_rx_iobuf(struct INF_3C90X *p)
  412. {
  413. int i;
  414. DBGP("a3c90x_free_rx_iobuf\n");
  415. for (i = 0; i < RX_RING_SIZE; i++) {
  416. free_iob(p->rx_iobuf[i]);
  417. p->rx_iobuf[i] = NULL;
  418. }
  419. }
  420. /**
  421. * a3c90x_process_rx_packets - Checks for received packets,
  422. * reports them to gPXE with netdev_rx() or netdev_rx_err() if there was an
  423. * error while receiving the packet
  424. *
  425. * @v netdev Network device info
  426. */
  427. static void a3c90x_process_rx_packets(struct net_device *netdev)
  428. {
  429. int i;
  430. unsigned int rx_status;
  431. struct INF_3C90X *p = netdev_priv(netdev);
  432. struct RXD *rx_cur_desc;
  433. DBGP("a3c90x_process_rx_packets\n");
  434. for (i = 0; i < RX_RING_SIZE; i++) {
  435. rx_cur_desc = p->rx_ring + p->rx_cur;
  436. rx_status = rx_cur_desc->UpPktStatus;
  437. if (!(rx_status & upComplete) && !(rx_status & upError))
  438. break;
  439. if (p->rx_iobuf[p->rx_cur] == NULL)
  440. break;
  441. if (rx_status & upError) {
  442. DBG("Corrupted packet received\n");
  443. netdev_rx_err(netdev, p->rx_iobuf[p->rx_cur],
  444. -EINVAL);
  445. } else {
  446. /* if we're here, we've got good packet */
  447. int packet_len;
  448. packet_len = rx_status & 0x1FFF;
  449. iob_put(p->rx_iobuf[p->rx_cur], packet_len);
  450. DBG("received packet\n");
  451. DBG(" size: %d\n", packet_len);
  452. netdev_rx(netdev, p->rx_iobuf[p->rx_cur]);
  453. }
  454. p->rx_iobuf[p->rx_cur] = NULL; /* invalidate rx desc */
  455. p->rx_cur = (p->rx_cur + 1) % RX_RING_SIZE;
  456. }
  457. a3c90x_refill_rx_ring(p);
  458. }
  459. /**
  460. * a3c90x_poll - Routine that gets called periodically.
  461. * Here we hanle transmitted and received packets.
  462. * We could also check the link status from time to time, which we
  463. * currently don't do.
  464. *
  465. * @v netdev Network device info
  466. */
  467. static void a3c90x_poll(struct net_device *netdev)
  468. {
  469. struct INF_3C90X *p = netdev_priv(netdev);
  470. uint16_t raw_status, int_status;
  471. DBGP("a3c90x_poll\n");
  472. raw_status = inw(p->IOAddr + regCommandIntStatus_w);
  473. int_status = (raw_status & 0x0FFF);
  474. if ( int_status == 0 )
  475. return;
  476. a3c90x_internal_IssueCommand(p->IOAddr, cmdAcknowledgeInterrupt,
  477. int_status);
  478. if (int_status & INT_TXCOMPLETE)
  479. outb(0x00, p->IOAddr + regTxStatus_b);
  480. DBG("poll: status = %#04x\n", raw_status);
  481. a3c90x_process_tx_packets(netdev);
  482. a3c90x_process_rx_packets(netdev);
  483. }
  484. static void a3c90x_free_resources(struct INF_3C90X *p)
  485. {
  486. DBGP("a3c90x_free_resources\n");
  487. a3c90x_free_tx_ring(p);
  488. a3c90x_free_rx_ring(p);
  489. a3c90x_free_rx_iobuf(p);
  490. }
  491. /**
  492. * a3c90x_remove - Routine to remove the card. Unregisters
  493. * the NIC from gPXE, disables RX/TX and resets the card.
  494. *
  495. * @v pci PCI device info
  496. */
  497. static void a3c90x_remove(struct pci_device *pci)
  498. {
  499. struct net_device *netdev = pci_get_drvdata(pci);
  500. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  501. DBGP("a3c90x_remove\n");
  502. a3c90x_reset(inf_3c90x);
  503. /* Disable the receiver and transmitter. */
  504. outw(cmdRxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  505. outw(cmdTxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  506. unregister_netdev(netdev);
  507. netdev_nullify(netdev);
  508. netdev_put(netdev);
  509. }
  510. static void a3c90x_irq(struct net_device *netdev, int enable)
  511. {
  512. struct INF_3C90X *p = netdev_priv(netdev);
  513. DBGP("a3c90x_irq\n");
  514. if (enable == 0) {
  515. /* disable interrupts */
  516. a3c90x_internal_IssueCommand(p->IOAddr,
  517. cmdSetInterruptEnable, 0);
  518. } else {
  519. a3c90x_internal_IssueCommand(p->IOAddr,
  520. cmdSetInterruptEnable,
  521. INT_TXCOMPLETE |
  522. INT_UPCOMPLETE);
  523. a3c90x_internal_IssueCommand(p->IOAddr,
  524. cmdAcknowledgeInterrupt,
  525. 0x661);
  526. }
  527. }
  528. /**
  529. * a3c90x_hw_start - Initialize hardware, copy MAC address
  530. * to NIC registers, set default receiver
  531. */
  532. static void a3c90x_hw_start(struct net_device *netdev)
  533. {
  534. int i, c;
  535. unsigned int cfg;
  536. unsigned int mopt;
  537. unsigned short linktype;
  538. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  539. DBGP("a3c90x_hw_start\n");
  540. /* 3C556: Invert MII power */
  541. if (inf_3c90x->is3c556) {
  542. unsigned int tmp;
  543. a3c90x_internal_SetWindow(inf_3c90x, winAddressing2);
  544. tmp = inw(inf_3c90x->IOAddr + regResetOptions_2_w);
  545. tmp |= 0x4000;
  546. outw(tmp, inf_3c90x->IOAddr + regResetOptions_2_w);
  547. }
  548. /* Copy MAC address into the NIC registers */
  549. a3c90x_internal_SetWindow(inf_3c90x, winAddressing2);
  550. for (i = 0; i < ETH_ALEN; i++)
  551. outb(netdev->ll_addr[i],
  552. inf_3c90x->IOAddr + regStationAddress_2_3w + i);
  553. for (i = 0; i < ETH_ALEN; i++)
  554. outb(0, inf_3c90x->IOAddr + regStationMask_2_3w + i);
  555. /* Read the media options register, print a message and set default
  556. * xcvr.
  557. *
  558. * Uses Media Option command on B revision, Reset Option on non-B
  559. * revision cards -- same register address
  560. */
  561. a3c90x_internal_SetWindow(inf_3c90x, winTxRxOptions3);
  562. mopt = inw(inf_3c90x->IOAddr + regResetMediaOptions_3_w);
  563. /* mask out VCO bit that is defined as 10baseFL bit on B-rev cards */
  564. if (!inf_3c90x->isBrev) {
  565. mopt &= 0x7F;
  566. }
  567. DBG("Connectors present: ");
  568. c = 0;
  569. linktype = 0x0008;
  570. if (mopt & 0x01) {
  571. DBG("%s100Base-T4", (c++) ? ", " : "");
  572. linktype = linkMII;
  573. }
  574. if (mopt & 0x04) {
  575. DBG("%s100Base-FX", (c++) ? ", " : "");
  576. linktype = link100BaseFX;
  577. }
  578. if (mopt & 0x10) {
  579. DBG("%s10Base-2", (c++) ? ", " : "");
  580. linktype = link10Base2;
  581. }
  582. if (mopt & 0x20) {
  583. DBG("%sAUI", (c++) ? ", " : "");
  584. linktype = linkAUI;
  585. }
  586. if (mopt & 0x40) {
  587. DBG("%sMII", (c++) ? ", " : "");
  588. linktype = linkMII;
  589. }
  590. if ((mopt & 0xA) == 0xA) {
  591. DBG("%s10Base-T / 100Base-TX", (c++) ? ", " : "");
  592. linktype = linkAutoneg;
  593. } else if ((mopt & 0xA) == 0x2) {
  594. DBG("%s100Base-TX", (c++) ? ", " : "");
  595. linktype = linkAutoneg;
  596. } else if ((mopt & 0xA) == 0x8) {
  597. DBG("%s10Base-T", (c++) ? ", " : "");
  598. linktype = linkAutoneg;
  599. }
  600. DBG(".\n");
  601. /* Determine transceiver type to use, depending on value stored in
  602. * eeprom 0x16
  603. */
  604. if (inf_3c90x->isBrev) {
  605. if ((inf_3c90x->eeprom[0x16] & 0xFF00) == XCVR_MAGIC) {
  606. /* User-defined */
  607. linktype = inf_3c90x->eeprom[0x16] & 0x000F;
  608. }
  609. } else {
  610. /* I don't know what MII MAC only mode is!!! */
  611. if (linktype == linkExternalMII) {
  612. if (inf_3c90x->isBrev)
  613. DBG("WARNING: MII External MAC Mode only supported on B-revision " "cards!!!!\nFalling Back to MII Mode\n");
  614. linktype = linkMII;
  615. }
  616. }
  617. /* enable DC converter for 10-Base-T */
  618. if (linktype == link10Base2) {
  619. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  620. cmdEnableDcConverter, 0);
  621. }
  622. /* Set the link to the type we just determined. */
  623. a3c90x_internal_SetWindow(inf_3c90x, winTxRxOptions3);
  624. cfg = inl(inf_3c90x->IOAddr + regInternalConfig_3_l);
  625. cfg &= ~(0xF << 20);
  626. cfg |= (linktype << 20);
  627. DBG("Setting internal cfg register: 0x%08X (linktype: 0x%02X)\n",
  628. cfg, linktype);
  629. outl(cfg, inf_3c90x->IOAddr + regInternalConfig_3_l);
  630. /* Now that we set the xcvr type, reset the Tx and Rx */
  631. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdTxReset, 0x00);
  632. if (!inf_3c90x->isBrev)
  633. outb(0x01, inf_3c90x->IOAddr + regTxFreeThresh_b);
  634. /* Set the RX filter = receive only individual pkts & multicast & bcast. */
  635. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdSetRxFilter,
  636. 0x01 + 0x02 + 0x04);
  637. /*
  638. * set Indication and Interrupt flags , acknowledge any IRQ's
  639. */
  640. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  641. cmdSetInterruptEnable,
  642. INT_TXCOMPLETE | INT_UPCOMPLETE);
  643. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  644. cmdSetIndicationEnable,
  645. INT_TXCOMPLETE | INT_UPCOMPLETE);
  646. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr,
  647. cmdAcknowledgeInterrupt, 0x661);
  648. }
  649. /**
  650. * a3c90x_open - Routine to initialize the card. Initialize hardware,
  651. * allocate TX and RX ring, send RX ring address to the NIC.
  652. *
  653. * @v netdev Network device info
  654. *
  655. * @ret Returns 0 on success, negative on failure
  656. */
  657. static int a3c90x_open(struct net_device *netdev)
  658. {
  659. int rc;
  660. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  661. DBGP("a3c90x_open\n");
  662. a3c90x_hw_start(netdev);
  663. rc = a3c90x_setup_tx_ring(inf_3c90x);
  664. if (rc != 0) {
  665. DBG("Error setting up TX Ring\n");
  666. goto error;
  667. }
  668. rc = a3c90x_setup_rx_ring(inf_3c90x);
  669. if (rc != 0) {
  670. DBG("Error setting up RX Ring\n");
  671. goto error;
  672. }
  673. /* send rx_ring address to NIC */
  674. outl(virt_to_bus(inf_3c90x->rx_ring),
  675. inf_3c90x->IOAddr + regUpListPtr_l);
  676. /* enable packet transmission and reception */
  677. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdTxEnable, 0);
  678. a3c90x_internal_IssueCommand(inf_3c90x->IOAddr, cmdRxEnable, 0);
  679. return 0;
  680. error:
  681. a3c90x_free_resources(inf_3c90x);
  682. a3c90x_reset(inf_3c90x);
  683. return rc;
  684. }
  685. /**
  686. * a3c90x_close - free()s TX and RX ring, disablex RX/TX, resets NIC
  687. *
  688. * @v netdev Network device info
  689. */
  690. static void a3c90x_close(struct net_device *netdev)
  691. {
  692. struct INF_3C90X *inf_3c90x = netdev_priv(netdev);
  693. DBGP("a3c90x_close\n");
  694. a3c90x_reset(inf_3c90x);
  695. outw(cmdRxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  696. outw(cmdTxDisable, inf_3c90x->IOAddr + regCommandIntStatus_w);
  697. a3c90x_free_resources(inf_3c90x);
  698. }
  699. static struct net_device_operations a3c90x_operations = {
  700. .open = a3c90x_open,
  701. .close = a3c90x_close,
  702. .poll = a3c90x_poll,
  703. .transmit = a3c90x_transmit,
  704. .irq = a3c90x_irq,
  705. };
  706. /**
  707. * a3c90x_probe: exported routine to probe for the 3c905 card.
  708. * If this routine is called, the pci functions did find the
  709. * card. We read the eeprom here and get the MAC address.
  710. * Initialization is done in a3c90x_open().
  711. *
  712. * @v pci PCI device info
  713. * @ pci_id PCI device IDs
  714. *
  715. * @ret rc Returns 0 on success, negative on failure
  716. */
  717. static int a3c90x_probe(struct pci_device *pci,
  718. const struct pci_device_id *pci_id __unused)
  719. {
  720. struct net_device *netdev;
  721. struct INF_3C90X *inf_3c90x;
  722. unsigned char *HWAddr;
  723. int rc;
  724. DBGP("a3c90x_probe\n");
  725. if (pci->ioaddr == 0)
  726. return -EINVAL;
  727. netdev = alloc_etherdev(sizeof(*inf_3c90x));
  728. if (!netdev)
  729. return -ENOMEM;
  730. netdev_init(netdev, &a3c90x_operations);
  731. pci_set_drvdata(pci, netdev);
  732. netdev->dev = &pci->dev;
  733. inf_3c90x = netdev_priv(netdev);
  734. memset(inf_3c90x, 0, sizeof(*inf_3c90x));
  735. adjust_pci_device(pci);
  736. inf_3c90x->is3c556 = (pci->device == 0x6055);
  737. inf_3c90x->IOAddr = pci->ioaddr;
  738. inf_3c90x->CurrentWindow = winNone;
  739. inf_3c90x->isBrev = 1;
  740. switch (pci->device) {
  741. case 0x9000: /* 10 Base TPO */
  742. case 0x9001: /* 10/100 T4 */
  743. case 0x9050: /* 10/100 TPO */
  744. case 0x9051: /* 10 Base Combo */
  745. inf_3c90x->isBrev = 0;
  746. break;
  747. }
  748. DBG("[3c90x]: found NIC(0x%04X, 0x%04X), isBrev=%d, is3c556=%d\n",
  749. pci->vendor, pci->device, inf_3c90x->isBrev,
  750. inf_3c90x->is3c556);
  751. /* initialize nvs device */
  752. inf_3c90x->nvs.word_len_log2 = 1; /* word */
  753. inf_3c90x->nvs.size = (inf_3c90x->isBrev ? 0x20 : 0x17);
  754. inf_3c90x->nvs.block_size = 1;
  755. inf_3c90x->nvs.read = a3c90x_internal_ReadEeprom;
  756. inf_3c90x->nvs.write = a3c90x_internal_WriteEeprom;
  757. /* reset NIC before accessing any data from it */
  758. a3c90x_reset(inf_3c90x);
  759. /* load eeprom contents to inf_3c90x->eeprom */
  760. a3c90x_internal_ReadEepromContents(inf_3c90x);
  761. HWAddr = netdev->hw_addr;
  762. /* Retrieve the Hardware address */
  763. HWAddr[0] = inf_3c90x->eeprom[eepromHwAddrOffset + 0] >> 8;
  764. HWAddr[1] = inf_3c90x->eeprom[eepromHwAddrOffset + 0] & 0xFF;
  765. HWAddr[2] = inf_3c90x->eeprom[eepromHwAddrOffset + 1] >> 8;
  766. HWAddr[3] = inf_3c90x->eeprom[eepromHwAddrOffset + 1] & 0xFF;
  767. HWAddr[4] = inf_3c90x->eeprom[eepromHwAddrOffset + 2] >> 8;
  768. HWAddr[5] = inf_3c90x->eeprom[eepromHwAddrOffset + 2] & 0xFF;
  769. /* we don't handle linkstates yet, so we're always up */
  770. netdev_link_up(netdev);
  771. if ((rc = register_netdev(netdev)) != 0) {
  772. DBG("3c90x: register_netdev() failed\n");
  773. netdev_put(netdev);
  774. return rc;
  775. }
  776. return 0;
  777. }
  778. static struct pci_device_id a3c90x_nics[] = {
  779. /* Original 90x revisions: */
  780. PCI_ROM(0x10b7, 0x6055, "3c556", "3C556", 0), /* Huricane */
  781. PCI_ROM(0x10b7, 0x9000, "3c905-tpo", "3Com900-TPO", 0), /* 10 Base TPO */
  782. PCI_ROM(0x10b7, 0x9001, "3c905-t4", "3Com900-Combo", 0), /* 10/100 T4 */
  783. PCI_ROM(0x10b7, 0x9050, "3c905-tpo100", "3Com905-TX", 0), /* 100 Base TX / 10/100 TPO */
  784. PCI_ROM(0x10b7, 0x9051, "3c905-combo", "3Com905-T4", 0), /* 100 Base T4 / 10 Base Combo */
  785. /* Newer 90xB revisions: */
  786. PCI_ROM(0x10b7, 0x9004, "3c905b-tpo", "3Com900B-TPO", 0), /* 10 Base TPO */
  787. PCI_ROM(0x10b7, 0x9005, "3c905b-combo", "3Com900B-Combo", 0), /* 10 Base Combo */
  788. PCI_ROM(0x10b7, 0x9006, "3c905b-tpb2", "3Com900B-2/T", 0), /* 10 Base TP and Base2 */
  789. PCI_ROM(0x10b7, 0x900a, "3c905b-fl", "3Com900B-FL", 0), /* 10 Base FL */
  790. PCI_ROM(0x10b7, 0x9055, "3c905b-tpo100", "3Com905B-TX", 0), /* 10/100 TPO */
  791. PCI_ROM(0x10b7, 0x9056, "3c905b-t4", "3Com905B-T4", 0), /* 10/100 T4 */
  792. PCI_ROM(0x10b7, 0x9058, "3c905b-9058", "3Com905B-9058", 0), /* Cyclone 10/100/BNC */
  793. PCI_ROM(0x10b7, 0x905a, "3c905b-fx", "3Com905B-FL", 0), /* 100 Base FX / 10 Base FX */
  794. /* Newer 90xC revision: */
  795. PCI_ROM(0x10b7, 0x9200, "3c905c-tpo", "3Com905C-TXM", 0), /* 10/100 TPO (3C905C-TXM) */
  796. PCI_ROM(0x10b7, 0x9202, "3c920b-emb-ati", "3c920B-EMB-WNM (ATI Radeon 9100 IGP)", 0), /* 3c920B-EMB-WNM (ATI Radeon 9100 IGP) */
  797. PCI_ROM(0x10b7, 0x9210, "3c920b-emb-wnm", "3Com20B-EMB WNM", 0),
  798. PCI_ROM(0x10b7, 0x9800, "3c980", "3Com980-Cyclone", 0), /* Cyclone */
  799. PCI_ROM(0x10b7, 0x9805, "3c9805", "3Com9805", 0), /* Dual Port Server Cyclone */
  800. PCI_ROM(0x10b7, 0x7646, "3csoho100-tx", "3CSOHO100-TX", 0), /* Hurricane */
  801. PCI_ROM(0x10b7, 0x4500, "3c450", "3Com450 HomePNA Tornado", 0),
  802. PCI_ROM(0x10b7, 0x1201, "3c982a", "3Com982A", 0),
  803. PCI_ROM(0x10b7, 0x1202, "3c982b", "3Com982B", 0),
  804. };
  805. struct pci_driver a3c90x_driver __pci_driver = {
  806. .ids = a3c90x_nics,
  807. .id_count = (sizeof(a3c90x_nics) / sizeof(a3c90x_nics[0])),
  808. .probe = a3c90x_probe,
  809. .remove = a3c90x_remove,
  810. };
  811. /*
  812. * Local variables:
  813. * c-basic-offset: 8
  814. * c-indent-level: 8
  815. * tab-width: 8
  816. * End:
  817. */