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

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