typhoon.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559
  1. /* typhoon.c: A Linux Ethernet device driver for 3Com 3CR990 family of NICs */
  2. /*
  3. Written 2002-2004 by David Dillow <dave@thedillows.org>
  4. Based on code written 1998-2000 by Donald Becker <becker@scyld.com> and
  5. Linux 2.2.x driver by David P. McLean <davidpmclean@yahoo.com>.
  6. This software may be used and distributed according to the terms of
  7. the GNU General Public License (GPL), incorporated herein by reference.
  8. Drivers based on or derived from this code fall under the GPL and must
  9. retain the authorship, copyright and license notice. This file is not
  10. a complete program and may only be used when the entire operating
  11. system is licensed under the GPL.
  12. This software is available on a public web site. It may enable
  13. cryptographic capabilities of the 3Com hardware, and may be
  14. exported from the United States under License Exception "TSU"
  15. pursuant to 15 C.F.R. Section 740.13(e).
  16. This work was funded by the National Library of Medicine under
  17. the Department of Energy project number 0274DD06D1 and NLM project
  18. number Y1-LM-2015-01.
  19. This driver is designed for the 3Com 3CR990 Family of cards with the
  20. 3XP Processor. It has been tested on x86 and sparc64.
  21. KNOWN ISSUES:
  22. *) Cannot DMA Rx packets to a 2 byte aligned address. Also firmware
  23. issue. Hopefully 3Com will fix it.
  24. *) Waiting for a command response takes 8ms due to non-preemptable
  25. polling. Only significant for getting stats and creating
  26. SAs, but an ugly wart never the less.
  27. TODO:
  28. *) Doesn't do IPSEC offloading. Yet. Keep yer pants on, it's coming.
  29. *) Add more support for ethtool (especially for NIC stats)
  30. *) Allow disabling of RX checksum offloading
  31. *) Fix MAC changing to work while the interface is up
  32. (Need to put commands on the TX ring, which changes
  33. the locking)
  34. *) Add in FCS to {rx,tx}_bytes, since the hardware doesn't. See
  35. http://oss.sgi.com/cgi-bin/mesg.cgi?a=netdev&i=20031215152211.7003fe8e.rddunlap%40osdl.org
  36. */
  37. /* Set the copy breakpoint for the copy-only-tiny-frames scheme.
  38. * Setting to > 1518 effectively disables this feature.
  39. */
  40. static int rx_copybreak = 200;
  41. /* Should we use MMIO or Port IO?
  42. * 0: Port IO
  43. * 1: MMIO
  44. * 2: Try MMIO, fallback to Port IO
  45. */
  46. static unsigned int use_mmio = 2;
  47. /* end user-configurable values */
  48. /* Maximum number of multicast addresses to filter (vs. rx-all-multicast).
  49. */
  50. static const int multicast_filter_limit = 32;
  51. /* Operational parameters that are set at compile time. */
  52. /* Keep the ring sizes a power of two for compile efficiency.
  53. * The compiler will convert <unsigned>'%'<2^N> into a bit mask.
  54. * Making the Tx ring too large decreases the effectiveness of channel
  55. * bonding and packet priority.
  56. * There are no ill effects from too-large receive rings.
  57. *
  58. * We don't currently use the Hi Tx ring so, don't make it very big.
  59. *
  60. * Beware that if we start using the Hi Tx ring, we will need to change
  61. * typhoon_num_free_tx() and typhoon_tx_complete() to account for that.
  62. */
  63. #define TXHI_ENTRIES 2
  64. #define TXLO_ENTRIES 128
  65. #define RX_ENTRIES 32
  66. #define COMMAND_ENTRIES 16
  67. #define RESPONSE_ENTRIES 32
  68. #define COMMAND_RING_SIZE (COMMAND_ENTRIES * sizeof(struct cmd_desc))
  69. #define RESPONSE_RING_SIZE (RESPONSE_ENTRIES * sizeof(struct resp_desc))
  70. /* The 3XP will preload and remove 64 entries from the free buffer
  71. * list, and we need one entry to keep the ring from wrapping, so
  72. * to keep this a power of two, we use 128 entries.
  73. */
  74. #define RXFREE_ENTRIES 128
  75. #define RXENT_ENTRIES (RXFREE_ENTRIES - 1)
  76. /* Operational parameters that usually are not changed. */
  77. /* Time in jiffies before concluding the transmitter is hung. */
  78. #define TX_TIMEOUT (2*HZ)
  79. #define PKT_BUF_SZ 1536
  80. #define FIRMWARE_NAME "3com/typhoon.bin"
  81. #define pr_fmt(fmt) KBUILD_MODNAME " " fmt
  82. #include <linux/module.h>
  83. #include <linux/kernel.h>
  84. #include <linux/sched.h>
  85. #include <linux/string.h>
  86. #include <linux/timer.h>
  87. #include <linux/errno.h>
  88. #include <linux/ioport.h>
  89. #include <linux/interrupt.h>
  90. #include <linux/pci.h>
  91. #include <linux/netdevice.h>
  92. #include <linux/etherdevice.h>
  93. #include <linux/skbuff.h>
  94. #include <linux/mm.h>
  95. #include <linux/init.h>
  96. #include <linux/delay.h>
  97. #include <linux/ethtool.h>
  98. #include <linux/if_vlan.h>
  99. #include <linux/crc32.h>
  100. #include <linux/bitops.h>
  101. #include <asm/processor.h>
  102. #include <asm/io.h>
  103. #include <asm/uaccess.h>
  104. #include <linux/in6.h>
  105. #include <linux/dma-mapping.h>
  106. #include <linux/firmware.h>
  107. #include "typhoon.h"
  108. MODULE_AUTHOR("David Dillow <dave@thedillows.org>");
  109. MODULE_VERSION("1.0");
  110. MODULE_LICENSE("GPL");
  111. MODULE_FIRMWARE(FIRMWARE_NAME);
  112. MODULE_DESCRIPTION("3Com Typhoon Family (3C990, 3CR990, and variants)");
  113. MODULE_PARM_DESC(rx_copybreak, "Packets smaller than this are copied and "
  114. "the buffer given back to the NIC. Default "
  115. "is 200.");
  116. MODULE_PARM_DESC(use_mmio, "Use MMIO (1) or PIO(0) to access the NIC. "
  117. "Default is to try MMIO and fallback to PIO.");
  118. module_param(rx_copybreak, int, 0);
  119. module_param(use_mmio, int, 0);
  120. #if defined(NETIF_F_TSO) && MAX_SKB_FRAGS > 32
  121. #warning Typhoon only supports 32 entries in its SG list for TSO, disabling TSO
  122. #undef NETIF_F_TSO
  123. #endif
  124. #if TXLO_ENTRIES <= (2 * MAX_SKB_FRAGS)
  125. #error TX ring too small!
  126. #endif
  127. struct typhoon_card_info {
  128. const char *name;
  129. const int capabilities;
  130. };
  131. #define TYPHOON_CRYPTO_NONE 0x00
  132. #define TYPHOON_CRYPTO_DES 0x01
  133. #define TYPHOON_CRYPTO_3DES 0x02
  134. #define TYPHOON_CRYPTO_VARIABLE 0x04
  135. #define TYPHOON_FIBER 0x08
  136. #define TYPHOON_WAKEUP_NEEDS_RESET 0x10
  137. enum typhoon_cards {
  138. TYPHOON_TX = 0, TYPHOON_TX95, TYPHOON_TX97, TYPHOON_SVR,
  139. TYPHOON_SVR95, TYPHOON_SVR97, TYPHOON_TXM, TYPHOON_BSVR,
  140. TYPHOON_FX95, TYPHOON_FX97, TYPHOON_FX95SVR, TYPHOON_FX97SVR,
  141. TYPHOON_FXM,
  142. };
  143. /* directly indexed by enum typhoon_cards, above */
  144. static struct typhoon_card_info typhoon_card_info[] = {
  145. { "3Com Typhoon (3C990-TX)",
  146. TYPHOON_CRYPTO_NONE},
  147. { "3Com Typhoon (3CR990-TX-95)",
  148. TYPHOON_CRYPTO_DES},
  149. { "3Com Typhoon (3CR990-TX-97)",
  150. TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES},
  151. { "3Com Typhoon (3C990SVR)",
  152. TYPHOON_CRYPTO_NONE},
  153. { "3Com Typhoon (3CR990SVR95)",
  154. TYPHOON_CRYPTO_DES},
  155. { "3Com Typhoon (3CR990SVR97)",
  156. TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES},
  157. { "3Com Typhoon2 (3C990B-TX-M)",
  158. TYPHOON_CRYPTO_VARIABLE},
  159. { "3Com Typhoon2 (3C990BSVR)",
  160. TYPHOON_CRYPTO_VARIABLE},
  161. { "3Com Typhoon (3CR990-FX-95)",
  162. TYPHOON_CRYPTO_DES | TYPHOON_FIBER},
  163. { "3Com Typhoon (3CR990-FX-97)",
  164. TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES | TYPHOON_FIBER},
  165. { "3Com Typhoon (3CR990-FX-95 Server)",
  166. TYPHOON_CRYPTO_DES | TYPHOON_FIBER},
  167. { "3Com Typhoon (3CR990-FX-97 Server)",
  168. TYPHOON_CRYPTO_DES | TYPHOON_CRYPTO_3DES | TYPHOON_FIBER},
  169. { "3Com Typhoon2 (3C990B-FX-97)",
  170. TYPHOON_CRYPTO_VARIABLE | TYPHOON_FIBER},
  171. };
  172. /* Notes on the new subsystem numbering scheme:
  173. * bits 0-1 indicate crypto capabilities: (0) variable, (1) DES, or (2) 3DES
  174. * bit 4 indicates if this card has secured firmware (we don't support it)
  175. * bit 8 indicates if this is a (0) copper or (1) fiber card
  176. * bits 12-16 indicate card type: (0) client and (1) server
  177. */
  178. static const struct pci_device_id typhoon_pci_tbl[] = {
  179. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990,
  180. PCI_ANY_ID, PCI_ANY_ID, 0, 0,TYPHOON_TX },
  181. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_TX_95,
  182. PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_TX95 },
  183. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_TX_97,
  184. PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_TX97 },
  185. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990B,
  186. PCI_ANY_ID, 0x1000, 0, 0, TYPHOON_TXM },
  187. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990B,
  188. PCI_ANY_ID, 0x1102, 0, 0, TYPHOON_FXM },
  189. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990B,
  190. PCI_ANY_ID, 0x2000, 0, 0, TYPHOON_BSVR },
  191. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
  192. PCI_ANY_ID, 0x1101, 0, 0, TYPHOON_FX95 },
  193. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
  194. PCI_ANY_ID, 0x1102, 0, 0, TYPHOON_FX97 },
  195. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
  196. PCI_ANY_ID, 0x2101, 0, 0, TYPHOON_FX95SVR },
  197. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990_FX,
  198. PCI_ANY_ID, 0x2102, 0, 0, TYPHOON_FX97SVR },
  199. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990SVR95,
  200. PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_SVR95 },
  201. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990SVR97,
  202. PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_SVR97 },
  203. { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3CR990SVR,
  204. PCI_ANY_ID, PCI_ANY_ID, 0, 0, TYPHOON_SVR },
  205. { 0, }
  206. };
  207. MODULE_DEVICE_TABLE(pci, typhoon_pci_tbl);
  208. /* Define the shared memory area
  209. * Align everything the 3XP will normally be using.
  210. * We'll need to move/align txHi if we start using that ring.
  211. */
  212. #define __3xp_aligned ____cacheline_aligned
  213. struct typhoon_shared {
  214. struct typhoon_interface iface;
  215. struct typhoon_indexes indexes __3xp_aligned;
  216. struct tx_desc txLo[TXLO_ENTRIES] __3xp_aligned;
  217. struct rx_desc rxLo[RX_ENTRIES] __3xp_aligned;
  218. struct rx_desc rxHi[RX_ENTRIES] __3xp_aligned;
  219. struct cmd_desc cmd[COMMAND_ENTRIES] __3xp_aligned;
  220. struct resp_desc resp[RESPONSE_ENTRIES] __3xp_aligned;
  221. struct rx_free rxBuff[RXFREE_ENTRIES] __3xp_aligned;
  222. u32 zeroWord;
  223. struct tx_desc txHi[TXHI_ENTRIES];
  224. } __packed;
  225. struct rxbuff_ent {
  226. struct sk_buff *skb;
  227. dma_addr_t dma_addr;
  228. };
  229. struct typhoon {
  230. /* Tx cache line section */
  231. struct transmit_ring txLoRing ____cacheline_aligned;
  232. struct pci_dev * tx_pdev;
  233. void __iomem *tx_ioaddr;
  234. u32 txlo_dma_addr;
  235. /* Irq/Rx cache line section */
  236. void __iomem *ioaddr ____cacheline_aligned;
  237. struct typhoon_indexes *indexes;
  238. u8 awaiting_resp;
  239. u8 duplex;
  240. u8 speed;
  241. u8 card_state;
  242. struct basic_ring rxLoRing;
  243. struct pci_dev * pdev;
  244. struct net_device * dev;
  245. struct napi_struct napi;
  246. struct basic_ring rxHiRing;
  247. struct basic_ring rxBuffRing;
  248. struct rxbuff_ent rxbuffers[RXENT_ENTRIES];
  249. /* general section */
  250. spinlock_t command_lock ____cacheline_aligned;
  251. struct basic_ring cmdRing;
  252. struct basic_ring respRing;
  253. struct net_device_stats stats;
  254. struct net_device_stats stats_saved;
  255. struct typhoon_shared * shared;
  256. dma_addr_t shared_dma;
  257. __le16 xcvr_select;
  258. __le16 wol_events;
  259. __le32 offload;
  260. /* unused stuff (future use) */
  261. int capabilities;
  262. struct transmit_ring txHiRing;
  263. };
  264. enum completion_wait_values {
  265. NoWait = 0, WaitNoSleep, WaitSleep,
  266. };
  267. /* These are the values for the typhoon.card_state variable.
  268. * These determine where the statistics will come from in get_stats().
  269. * The sleep image does not support the statistics we need.
  270. */
  271. enum state_values {
  272. Sleeping = 0, Running,
  273. };
  274. /* PCI writes are not guaranteed to be posted in order, but outstanding writes
  275. * cannot pass a read, so this forces current writes to post.
  276. */
  277. #define typhoon_post_pci_writes(x) \
  278. do { if(likely(use_mmio)) ioread32(x+TYPHOON_REG_HEARTBEAT); } while(0)
  279. /* We'll wait up to six seconds for a reset, and half a second normally.
  280. */
  281. #define TYPHOON_UDELAY 50
  282. #define TYPHOON_RESET_TIMEOUT_SLEEP (6 * HZ)
  283. #define TYPHOON_RESET_TIMEOUT_NOSLEEP ((6 * 1000000) / TYPHOON_UDELAY)
  284. #define TYPHOON_WAIT_TIMEOUT ((1000000 / 2) / TYPHOON_UDELAY)
  285. #if defined(NETIF_F_TSO)
  286. #define skb_tso_size(x) (skb_shinfo(x)->gso_size)
  287. #define TSO_NUM_DESCRIPTORS 2
  288. #define TSO_OFFLOAD_ON TYPHOON_OFFLOAD_TCP_SEGMENT
  289. #else
  290. #define NETIF_F_TSO 0
  291. #define skb_tso_size(x) 0
  292. #define TSO_NUM_DESCRIPTORS 0
  293. #define TSO_OFFLOAD_ON 0
  294. #endif
  295. static inline void
  296. typhoon_inc_index(u32 *index, const int count, const int num_entries)
  297. {
  298. /* Increment a ring index -- we can use this for all rings execept
  299. * the Rx rings, as they use different size descriptors
  300. * otherwise, everything is the same size as a cmd_desc
  301. */
  302. *index += count * sizeof(struct cmd_desc);
  303. *index %= num_entries * sizeof(struct cmd_desc);
  304. }
  305. static inline void
  306. typhoon_inc_cmd_index(u32 *index, const int count)
  307. {
  308. typhoon_inc_index(index, count, COMMAND_ENTRIES);
  309. }
  310. static inline void
  311. typhoon_inc_resp_index(u32 *index, const int count)
  312. {
  313. typhoon_inc_index(index, count, RESPONSE_ENTRIES);
  314. }
  315. static inline void
  316. typhoon_inc_rxfree_index(u32 *index, const int count)
  317. {
  318. typhoon_inc_index(index, count, RXFREE_ENTRIES);
  319. }
  320. static inline void
  321. typhoon_inc_tx_index(u32 *index, const int count)
  322. {
  323. /* if we start using the Hi Tx ring, this needs updating */
  324. typhoon_inc_index(index, count, TXLO_ENTRIES);
  325. }
  326. static inline void
  327. typhoon_inc_rx_index(u32 *index, const int count)
  328. {
  329. /* sizeof(struct rx_desc) != sizeof(struct cmd_desc) */
  330. *index += count * sizeof(struct rx_desc);
  331. *index %= RX_ENTRIES * sizeof(struct rx_desc);
  332. }
  333. static int
  334. typhoon_reset(void __iomem *ioaddr, int wait_type)
  335. {
  336. int i, err = 0;
  337. int timeout;
  338. if(wait_type == WaitNoSleep)
  339. timeout = TYPHOON_RESET_TIMEOUT_NOSLEEP;
  340. else
  341. timeout = TYPHOON_RESET_TIMEOUT_SLEEP;
  342. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  343. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
  344. iowrite32(TYPHOON_RESET_ALL, ioaddr + TYPHOON_REG_SOFT_RESET);
  345. typhoon_post_pci_writes(ioaddr);
  346. udelay(1);
  347. iowrite32(TYPHOON_RESET_NONE, ioaddr + TYPHOON_REG_SOFT_RESET);
  348. if(wait_type != NoWait) {
  349. for(i = 0; i < timeout; i++) {
  350. if(ioread32(ioaddr + TYPHOON_REG_STATUS) ==
  351. TYPHOON_STATUS_WAITING_FOR_HOST)
  352. goto out;
  353. if(wait_type == WaitSleep)
  354. schedule_timeout_uninterruptible(1);
  355. else
  356. udelay(TYPHOON_UDELAY);
  357. }
  358. err = -ETIMEDOUT;
  359. }
  360. out:
  361. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  362. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
  363. /* The 3XP seems to need a little extra time to complete the load
  364. * of the sleep image before we can reliably boot it. Failure to
  365. * do this occasionally results in a hung adapter after boot in
  366. * typhoon_init_one() while trying to read the MAC address or
  367. * putting the card to sleep. 3Com's driver waits 5ms, but
  368. * that seems to be overkill. However, if we can sleep, we might
  369. * as well give it that much time. Otherwise, we'll give it 500us,
  370. * which should be enough (I've see it work well at 100us, but still
  371. * saw occasional problems.)
  372. */
  373. if(wait_type == WaitSleep)
  374. msleep(5);
  375. else
  376. udelay(500);
  377. return err;
  378. }
  379. static int
  380. typhoon_wait_status(void __iomem *ioaddr, u32 wait_value)
  381. {
  382. int i, err = 0;
  383. for(i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
  384. if(ioread32(ioaddr + TYPHOON_REG_STATUS) == wait_value)
  385. goto out;
  386. udelay(TYPHOON_UDELAY);
  387. }
  388. err = -ETIMEDOUT;
  389. out:
  390. return err;
  391. }
  392. static inline void
  393. typhoon_media_status(struct net_device *dev, struct resp_desc *resp)
  394. {
  395. if(resp->parm1 & TYPHOON_MEDIA_STAT_NO_LINK)
  396. netif_carrier_off(dev);
  397. else
  398. netif_carrier_on(dev);
  399. }
  400. static inline void
  401. typhoon_hello(struct typhoon *tp)
  402. {
  403. struct basic_ring *ring = &tp->cmdRing;
  404. struct cmd_desc *cmd;
  405. /* We only get a hello request if we've not sent anything to the
  406. * card in a long while. If the lock is held, then we're in the
  407. * process of issuing a command, so we don't need to respond.
  408. */
  409. if(spin_trylock(&tp->command_lock)) {
  410. cmd = (struct cmd_desc *)(ring->ringBase + ring->lastWrite);
  411. typhoon_inc_cmd_index(&ring->lastWrite, 1);
  412. INIT_COMMAND_NO_RESPONSE(cmd, TYPHOON_CMD_HELLO_RESP);
  413. wmb();
  414. iowrite32(ring->lastWrite, tp->ioaddr + TYPHOON_REG_CMD_READY);
  415. spin_unlock(&tp->command_lock);
  416. }
  417. }
  418. static int
  419. typhoon_process_response(struct typhoon *tp, int resp_size,
  420. struct resp_desc *resp_save)
  421. {
  422. struct typhoon_indexes *indexes = tp->indexes;
  423. struct resp_desc *resp;
  424. u8 *base = tp->respRing.ringBase;
  425. int count, len, wrap_len;
  426. u32 cleared;
  427. u32 ready;
  428. cleared = le32_to_cpu(indexes->respCleared);
  429. ready = le32_to_cpu(indexes->respReady);
  430. while(cleared != ready) {
  431. resp = (struct resp_desc *)(base + cleared);
  432. count = resp->numDesc + 1;
  433. if(resp_save && resp->seqNo) {
  434. if(count > resp_size) {
  435. resp_save->flags = TYPHOON_RESP_ERROR;
  436. goto cleanup;
  437. }
  438. wrap_len = 0;
  439. len = count * sizeof(*resp);
  440. if(unlikely(cleared + len > RESPONSE_RING_SIZE)) {
  441. wrap_len = cleared + len - RESPONSE_RING_SIZE;
  442. len = RESPONSE_RING_SIZE - cleared;
  443. }
  444. memcpy(resp_save, resp, len);
  445. if(unlikely(wrap_len)) {
  446. resp_save += len / sizeof(*resp);
  447. memcpy(resp_save, base, wrap_len);
  448. }
  449. resp_save = NULL;
  450. } else if(resp->cmd == TYPHOON_CMD_READ_MEDIA_STATUS) {
  451. typhoon_media_status(tp->dev, resp);
  452. } else if(resp->cmd == TYPHOON_CMD_HELLO_RESP) {
  453. typhoon_hello(tp);
  454. } else {
  455. netdev_err(tp->dev,
  456. "dumping unexpected response 0x%04x:%d:0x%02x:0x%04x:%08x:%08x\n",
  457. le16_to_cpu(resp->cmd),
  458. resp->numDesc, resp->flags,
  459. le16_to_cpu(resp->parm1),
  460. le32_to_cpu(resp->parm2),
  461. le32_to_cpu(resp->parm3));
  462. }
  463. cleanup:
  464. typhoon_inc_resp_index(&cleared, count);
  465. }
  466. indexes->respCleared = cpu_to_le32(cleared);
  467. wmb();
  468. return resp_save == NULL;
  469. }
  470. static inline int
  471. typhoon_num_free(int lastWrite, int lastRead, int ringSize)
  472. {
  473. /* this works for all descriptors but rx_desc, as they are a
  474. * different size than the cmd_desc -- everyone else is the same
  475. */
  476. lastWrite /= sizeof(struct cmd_desc);
  477. lastRead /= sizeof(struct cmd_desc);
  478. return (ringSize + lastRead - lastWrite - 1) % ringSize;
  479. }
  480. static inline int
  481. typhoon_num_free_cmd(struct typhoon *tp)
  482. {
  483. int lastWrite = tp->cmdRing.lastWrite;
  484. int cmdCleared = le32_to_cpu(tp->indexes->cmdCleared);
  485. return typhoon_num_free(lastWrite, cmdCleared, COMMAND_ENTRIES);
  486. }
  487. static inline int
  488. typhoon_num_free_resp(struct typhoon *tp)
  489. {
  490. int respReady = le32_to_cpu(tp->indexes->respReady);
  491. int respCleared = le32_to_cpu(tp->indexes->respCleared);
  492. return typhoon_num_free(respReady, respCleared, RESPONSE_ENTRIES);
  493. }
  494. static inline int
  495. typhoon_num_free_tx(struct transmit_ring *ring)
  496. {
  497. /* if we start using the Hi Tx ring, this needs updating */
  498. return typhoon_num_free(ring->lastWrite, ring->lastRead, TXLO_ENTRIES);
  499. }
  500. static int
  501. typhoon_issue_command(struct typhoon *tp, int num_cmd, struct cmd_desc *cmd,
  502. int num_resp, struct resp_desc *resp)
  503. {
  504. struct typhoon_indexes *indexes = tp->indexes;
  505. struct basic_ring *ring = &tp->cmdRing;
  506. struct resp_desc local_resp;
  507. int i, err = 0;
  508. int got_resp;
  509. int freeCmd, freeResp;
  510. int len, wrap_len;
  511. spin_lock(&tp->command_lock);
  512. freeCmd = typhoon_num_free_cmd(tp);
  513. freeResp = typhoon_num_free_resp(tp);
  514. if(freeCmd < num_cmd || freeResp < num_resp) {
  515. netdev_err(tp->dev, "no descs for cmd, had (needed) %d (%d) cmd, %d (%d) resp\n",
  516. freeCmd, num_cmd, freeResp, num_resp);
  517. err = -ENOMEM;
  518. goto out;
  519. }
  520. if(cmd->flags & TYPHOON_CMD_RESPOND) {
  521. /* If we're expecting a response, but the caller hasn't given
  522. * us a place to put it, we'll provide one.
  523. */
  524. tp->awaiting_resp = 1;
  525. if(resp == NULL) {
  526. resp = &local_resp;
  527. num_resp = 1;
  528. }
  529. }
  530. wrap_len = 0;
  531. len = num_cmd * sizeof(*cmd);
  532. if(unlikely(ring->lastWrite + len > COMMAND_RING_SIZE)) {
  533. wrap_len = ring->lastWrite + len - COMMAND_RING_SIZE;
  534. len = COMMAND_RING_SIZE - ring->lastWrite;
  535. }
  536. memcpy(ring->ringBase + ring->lastWrite, cmd, len);
  537. if(unlikely(wrap_len)) {
  538. struct cmd_desc *wrap_ptr = cmd;
  539. wrap_ptr += len / sizeof(*cmd);
  540. memcpy(ring->ringBase, wrap_ptr, wrap_len);
  541. }
  542. typhoon_inc_cmd_index(&ring->lastWrite, num_cmd);
  543. /* "I feel a presence... another warrior is on the mesa."
  544. */
  545. wmb();
  546. iowrite32(ring->lastWrite, tp->ioaddr + TYPHOON_REG_CMD_READY);
  547. typhoon_post_pci_writes(tp->ioaddr);
  548. if((cmd->flags & TYPHOON_CMD_RESPOND) == 0)
  549. goto out;
  550. /* Ugh. We'll be here about 8ms, spinning our thumbs, unable to
  551. * preempt or do anything other than take interrupts. So, don't
  552. * wait for a response unless you have to.
  553. *
  554. * I've thought about trying to sleep here, but we're called
  555. * from many contexts that don't allow that. Also, given the way
  556. * 3Com has implemented irq coalescing, we would likely timeout --
  557. * this has been observed in real life!
  558. *
  559. * The big killer is we have to wait to get stats from the card,
  560. * though we could go to a periodic refresh of those if we don't
  561. * mind them getting somewhat stale. The rest of the waiting
  562. * commands occur during open/close/suspend/resume, so they aren't
  563. * time critical. Creating SAs in the future will also have to
  564. * wait here.
  565. */
  566. got_resp = 0;
  567. for(i = 0; i < TYPHOON_WAIT_TIMEOUT && !got_resp; i++) {
  568. if(indexes->respCleared != indexes->respReady)
  569. got_resp = typhoon_process_response(tp, num_resp,
  570. resp);
  571. udelay(TYPHOON_UDELAY);
  572. }
  573. if(!got_resp) {
  574. err = -ETIMEDOUT;
  575. goto out;
  576. }
  577. /* Collect the error response even if we don't care about the
  578. * rest of the response
  579. */
  580. if(resp->flags & TYPHOON_RESP_ERROR)
  581. err = -EIO;
  582. out:
  583. if(tp->awaiting_resp) {
  584. tp->awaiting_resp = 0;
  585. smp_wmb();
  586. /* Ugh. If a response was added to the ring between
  587. * the call to typhoon_process_response() and the clearing
  588. * of tp->awaiting_resp, we could have missed the interrupt
  589. * and it could hang in the ring an indeterminate amount of
  590. * time. So, check for it, and interrupt ourselves if this
  591. * is the case.
  592. */
  593. if(indexes->respCleared != indexes->respReady)
  594. iowrite32(1, tp->ioaddr + TYPHOON_REG_SELF_INTERRUPT);
  595. }
  596. spin_unlock(&tp->command_lock);
  597. return err;
  598. }
  599. static inline void
  600. typhoon_tso_fill(struct sk_buff *skb, struct transmit_ring *txRing,
  601. u32 ring_dma)
  602. {
  603. struct tcpopt_desc *tcpd;
  604. u32 tcpd_offset = ring_dma;
  605. tcpd = (struct tcpopt_desc *) (txRing->ringBase + txRing->lastWrite);
  606. tcpd_offset += txRing->lastWrite;
  607. tcpd_offset += offsetof(struct tcpopt_desc, bytesTx);
  608. typhoon_inc_tx_index(&txRing->lastWrite, 1);
  609. tcpd->flags = TYPHOON_OPT_DESC | TYPHOON_OPT_TCP_SEG;
  610. tcpd->numDesc = 1;
  611. tcpd->mss_flags = cpu_to_le16(skb_tso_size(skb));
  612. tcpd->mss_flags |= TYPHOON_TSO_FIRST | TYPHOON_TSO_LAST;
  613. tcpd->respAddrLo = cpu_to_le32(tcpd_offset);
  614. tcpd->bytesTx = cpu_to_le32(skb->len);
  615. tcpd->status = 0;
  616. }
  617. static netdev_tx_t
  618. typhoon_start_tx(struct sk_buff *skb, struct net_device *dev)
  619. {
  620. struct typhoon *tp = netdev_priv(dev);
  621. struct transmit_ring *txRing;
  622. struct tx_desc *txd, *first_txd;
  623. dma_addr_t skb_dma;
  624. int numDesc;
  625. /* we have two rings to choose from, but we only use txLo for now
  626. * If we start using the Hi ring as well, we'll need to update
  627. * typhoon_stop_runtime(), typhoon_interrupt(), typhoon_num_free_tx(),
  628. * and TXHI_ENTRIES to match, as well as update the TSO code below
  629. * to get the right DMA address
  630. */
  631. txRing = &tp->txLoRing;
  632. /* We need one descriptor for each fragment of the sk_buff, plus the
  633. * one for the ->data area of it.
  634. *
  635. * The docs say a maximum of 16 fragment descriptors per TCP option
  636. * descriptor, then make a new packet descriptor and option descriptor
  637. * for the next 16 fragments. The engineers say just an option
  638. * descriptor is needed. I've tested up to 26 fragments with a single
  639. * packet descriptor/option descriptor combo, so I use that for now.
  640. *
  641. * If problems develop with TSO, check this first.
  642. */
  643. numDesc = skb_shinfo(skb)->nr_frags + 1;
  644. if (skb_is_gso(skb))
  645. numDesc++;
  646. /* When checking for free space in the ring, we need to also
  647. * account for the initial Tx descriptor, and we always must leave
  648. * at least one descriptor unused in the ring so that it doesn't
  649. * wrap and look empty.
  650. *
  651. * The only time we should loop here is when we hit the race
  652. * between marking the queue awake and updating the cleared index.
  653. * Just loop and it will appear. This comes from the acenic driver.
  654. */
  655. while(unlikely(typhoon_num_free_tx(txRing) < (numDesc + 2)))
  656. smp_rmb();
  657. first_txd = (struct tx_desc *) (txRing->ringBase + txRing->lastWrite);
  658. typhoon_inc_tx_index(&txRing->lastWrite, 1);
  659. first_txd->flags = TYPHOON_TX_DESC | TYPHOON_DESC_VALID;
  660. first_txd->numDesc = 0;
  661. first_txd->len = 0;
  662. first_txd->tx_addr = (u64)((unsigned long) skb);
  663. first_txd->processFlags = 0;
  664. if(skb->ip_summed == CHECKSUM_PARTIAL) {
  665. /* The 3XP will figure out if this is UDP/TCP */
  666. first_txd->processFlags |= TYPHOON_TX_PF_TCP_CHKSUM;
  667. first_txd->processFlags |= TYPHOON_TX_PF_UDP_CHKSUM;
  668. first_txd->processFlags |= TYPHOON_TX_PF_IP_CHKSUM;
  669. }
  670. if (skb_vlan_tag_present(skb)) {
  671. first_txd->processFlags |=
  672. TYPHOON_TX_PF_INSERT_VLAN | TYPHOON_TX_PF_VLAN_PRIORITY;
  673. first_txd->processFlags |=
  674. cpu_to_le32(htons(skb_vlan_tag_get(skb)) <<
  675. TYPHOON_TX_PF_VLAN_TAG_SHIFT);
  676. }
  677. if (skb_is_gso(skb)) {
  678. first_txd->processFlags |= TYPHOON_TX_PF_TCP_SEGMENT;
  679. first_txd->numDesc++;
  680. typhoon_tso_fill(skb, txRing, tp->txlo_dma_addr);
  681. }
  682. txd = (struct tx_desc *) (txRing->ringBase + txRing->lastWrite);
  683. typhoon_inc_tx_index(&txRing->lastWrite, 1);
  684. /* No need to worry about padding packet -- the firmware pads
  685. * it with zeros to ETH_ZLEN for us.
  686. */
  687. if(skb_shinfo(skb)->nr_frags == 0) {
  688. skb_dma = pci_map_single(tp->tx_pdev, skb->data, skb->len,
  689. PCI_DMA_TODEVICE);
  690. txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
  691. txd->len = cpu_to_le16(skb->len);
  692. txd->frag.addr = cpu_to_le32(skb_dma);
  693. txd->frag.addrHi = 0;
  694. first_txd->numDesc++;
  695. } else {
  696. int i, len;
  697. len = skb_headlen(skb);
  698. skb_dma = pci_map_single(tp->tx_pdev, skb->data, len,
  699. PCI_DMA_TODEVICE);
  700. txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
  701. txd->len = cpu_to_le16(len);
  702. txd->frag.addr = cpu_to_le32(skb_dma);
  703. txd->frag.addrHi = 0;
  704. first_txd->numDesc++;
  705. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  706. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  707. void *frag_addr;
  708. txd = (struct tx_desc *) (txRing->ringBase +
  709. txRing->lastWrite);
  710. typhoon_inc_tx_index(&txRing->lastWrite, 1);
  711. len = skb_frag_size(frag);
  712. frag_addr = skb_frag_address(frag);
  713. skb_dma = pci_map_single(tp->tx_pdev, frag_addr, len,
  714. PCI_DMA_TODEVICE);
  715. txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
  716. txd->len = cpu_to_le16(len);
  717. txd->frag.addr = cpu_to_le32(skb_dma);
  718. txd->frag.addrHi = 0;
  719. first_txd->numDesc++;
  720. }
  721. }
  722. /* Kick the 3XP
  723. */
  724. wmb();
  725. iowrite32(txRing->lastWrite, tp->tx_ioaddr + txRing->writeRegister);
  726. /* If we don't have room to put the worst case packet on the
  727. * queue, then we must stop the queue. We need 2 extra
  728. * descriptors -- one to prevent ring wrap, and one for the
  729. * Tx header.
  730. */
  731. numDesc = MAX_SKB_FRAGS + TSO_NUM_DESCRIPTORS + 1;
  732. if(typhoon_num_free_tx(txRing) < (numDesc + 2)) {
  733. netif_stop_queue(dev);
  734. /* A Tx complete IRQ could have gotten between, making
  735. * the ring free again. Only need to recheck here, since
  736. * Tx is serialized.
  737. */
  738. if(typhoon_num_free_tx(txRing) >= (numDesc + 2))
  739. netif_wake_queue(dev);
  740. }
  741. return NETDEV_TX_OK;
  742. }
  743. static void
  744. typhoon_set_rx_mode(struct net_device *dev)
  745. {
  746. struct typhoon *tp = netdev_priv(dev);
  747. struct cmd_desc xp_cmd;
  748. u32 mc_filter[2];
  749. __le16 filter;
  750. filter = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
  751. if(dev->flags & IFF_PROMISC) {
  752. filter |= TYPHOON_RX_FILTER_PROMISCOUS;
  753. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  754. (dev->flags & IFF_ALLMULTI)) {
  755. /* Too many to match, or accept all multicasts. */
  756. filter |= TYPHOON_RX_FILTER_ALL_MCAST;
  757. } else if (!netdev_mc_empty(dev)) {
  758. struct netdev_hw_addr *ha;
  759. memset(mc_filter, 0, sizeof(mc_filter));
  760. netdev_for_each_mc_addr(ha, dev) {
  761. int bit = ether_crc(ETH_ALEN, ha->addr) & 0x3f;
  762. mc_filter[bit >> 5] |= 1 << (bit & 0x1f);
  763. }
  764. INIT_COMMAND_NO_RESPONSE(&xp_cmd,
  765. TYPHOON_CMD_SET_MULTICAST_HASH);
  766. xp_cmd.parm1 = TYPHOON_MCAST_HASH_SET;
  767. xp_cmd.parm2 = cpu_to_le32(mc_filter[0]);
  768. xp_cmd.parm3 = cpu_to_le32(mc_filter[1]);
  769. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  770. filter |= TYPHOON_RX_FILTER_MCAST_HASH;
  771. }
  772. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
  773. xp_cmd.parm1 = filter;
  774. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  775. }
  776. static int
  777. typhoon_do_get_stats(struct typhoon *tp)
  778. {
  779. struct net_device_stats *stats = &tp->stats;
  780. struct net_device_stats *saved = &tp->stats_saved;
  781. struct cmd_desc xp_cmd;
  782. struct resp_desc xp_resp[7];
  783. struct stats_resp *s = (struct stats_resp *) xp_resp;
  784. int err;
  785. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_STATS);
  786. err = typhoon_issue_command(tp, 1, &xp_cmd, 7, xp_resp);
  787. if(err < 0)
  788. return err;
  789. /* 3Com's Linux driver uses txMultipleCollisions as it's
  790. * collisions value, but there is some other collision info as well...
  791. *
  792. * The extra status reported would be a good candidate for
  793. * ethtool_ops->get_{strings,stats}()
  794. */
  795. stats->tx_packets = le32_to_cpu(s->txPackets) +
  796. saved->tx_packets;
  797. stats->tx_bytes = le64_to_cpu(s->txBytes) +
  798. saved->tx_bytes;
  799. stats->tx_errors = le32_to_cpu(s->txCarrierLost) +
  800. saved->tx_errors;
  801. stats->tx_carrier_errors = le32_to_cpu(s->txCarrierLost) +
  802. saved->tx_carrier_errors;
  803. stats->collisions = le32_to_cpu(s->txMultipleCollisions) +
  804. saved->collisions;
  805. stats->rx_packets = le32_to_cpu(s->rxPacketsGood) +
  806. saved->rx_packets;
  807. stats->rx_bytes = le64_to_cpu(s->rxBytesGood) +
  808. saved->rx_bytes;
  809. stats->rx_fifo_errors = le32_to_cpu(s->rxFifoOverruns) +
  810. saved->rx_fifo_errors;
  811. stats->rx_errors = le32_to_cpu(s->rxFifoOverruns) +
  812. le32_to_cpu(s->BadSSD) + le32_to_cpu(s->rxCrcErrors) +
  813. saved->rx_errors;
  814. stats->rx_crc_errors = le32_to_cpu(s->rxCrcErrors) +
  815. saved->rx_crc_errors;
  816. stats->rx_length_errors = le32_to_cpu(s->rxOversized) +
  817. saved->rx_length_errors;
  818. tp->speed = (s->linkStatus & TYPHOON_LINK_100MBPS) ?
  819. SPEED_100 : SPEED_10;
  820. tp->duplex = (s->linkStatus & TYPHOON_LINK_FULL_DUPLEX) ?
  821. DUPLEX_FULL : DUPLEX_HALF;
  822. return 0;
  823. }
  824. static struct net_device_stats *
  825. typhoon_get_stats(struct net_device *dev)
  826. {
  827. struct typhoon *tp = netdev_priv(dev);
  828. struct net_device_stats *stats = &tp->stats;
  829. struct net_device_stats *saved = &tp->stats_saved;
  830. smp_rmb();
  831. if(tp->card_state == Sleeping)
  832. return saved;
  833. if(typhoon_do_get_stats(tp) < 0) {
  834. netdev_err(dev, "error getting stats\n");
  835. return saved;
  836. }
  837. return stats;
  838. }
  839. static void
  840. typhoon_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  841. {
  842. struct typhoon *tp = netdev_priv(dev);
  843. struct pci_dev *pci_dev = tp->pdev;
  844. struct cmd_desc xp_cmd;
  845. struct resp_desc xp_resp[3];
  846. smp_rmb();
  847. if(tp->card_state == Sleeping) {
  848. strlcpy(info->fw_version, "Sleep image",
  849. sizeof(info->fw_version));
  850. } else {
  851. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
  852. if(typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp) < 0) {
  853. strlcpy(info->fw_version, "Unknown runtime",
  854. sizeof(info->fw_version));
  855. } else {
  856. u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
  857. snprintf(info->fw_version, sizeof(info->fw_version),
  858. "%02x.%03x.%03x", sleep_ver >> 24,
  859. (sleep_ver >> 12) & 0xfff, sleep_ver & 0xfff);
  860. }
  861. }
  862. strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
  863. strlcpy(info->bus_info, pci_name(pci_dev), sizeof(info->bus_info));
  864. }
  865. static int
  866. typhoon_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  867. {
  868. struct typhoon *tp = netdev_priv(dev);
  869. cmd->supported = SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  870. SUPPORTED_Autoneg;
  871. switch (tp->xcvr_select) {
  872. case TYPHOON_XCVR_10HALF:
  873. cmd->advertising = ADVERTISED_10baseT_Half;
  874. break;
  875. case TYPHOON_XCVR_10FULL:
  876. cmd->advertising = ADVERTISED_10baseT_Full;
  877. break;
  878. case TYPHOON_XCVR_100HALF:
  879. cmd->advertising = ADVERTISED_100baseT_Half;
  880. break;
  881. case TYPHOON_XCVR_100FULL:
  882. cmd->advertising = ADVERTISED_100baseT_Full;
  883. break;
  884. case TYPHOON_XCVR_AUTONEG:
  885. cmd->advertising = ADVERTISED_10baseT_Half |
  886. ADVERTISED_10baseT_Full |
  887. ADVERTISED_100baseT_Half |
  888. ADVERTISED_100baseT_Full |
  889. ADVERTISED_Autoneg;
  890. break;
  891. }
  892. if(tp->capabilities & TYPHOON_FIBER) {
  893. cmd->supported |= SUPPORTED_FIBRE;
  894. cmd->advertising |= ADVERTISED_FIBRE;
  895. cmd->port = PORT_FIBRE;
  896. } else {
  897. cmd->supported |= SUPPORTED_10baseT_Half |
  898. SUPPORTED_10baseT_Full |
  899. SUPPORTED_TP;
  900. cmd->advertising |= ADVERTISED_TP;
  901. cmd->port = PORT_TP;
  902. }
  903. /* need to get stats to make these link speed/duplex valid */
  904. typhoon_do_get_stats(tp);
  905. ethtool_cmd_speed_set(cmd, tp->speed);
  906. cmd->duplex = tp->duplex;
  907. cmd->phy_address = 0;
  908. cmd->transceiver = XCVR_INTERNAL;
  909. if(tp->xcvr_select == TYPHOON_XCVR_AUTONEG)
  910. cmd->autoneg = AUTONEG_ENABLE;
  911. else
  912. cmd->autoneg = AUTONEG_DISABLE;
  913. cmd->maxtxpkt = 1;
  914. cmd->maxrxpkt = 1;
  915. return 0;
  916. }
  917. static int
  918. typhoon_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  919. {
  920. struct typhoon *tp = netdev_priv(dev);
  921. u32 speed = ethtool_cmd_speed(cmd);
  922. struct cmd_desc xp_cmd;
  923. __le16 xcvr;
  924. int err;
  925. err = -EINVAL;
  926. if (cmd->autoneg == AUTONEG_ENABLE) {
  927. xcvr = TYPHOON_XCVR_AUTONEG;
  928. } else {
  929. if (cmd->duplex == DUPLEX_HALF) {
  930. if (speed == SPEED_10)
  931. xcvr = TYPHOON_XCVR_10HALF;
  932. else if (speed == SPEED_100)
  933. xcvr = TYPHOON_XCVR_100HALF;
  934. else
  935. goto out;
  936. } else if (cmd->duplex == DUPLEX_FULL) {
  937. if (speed == SPEED_10)
  938. xcvr = TYPHOON_XCVR_10FULL;
  939. else if (speed == SPEED_100)
  940. xcvr = TYPHOON_XCVR_100FULL;
  941. else
  942. goto out;
  943. } else
  944. goto out;
  945. }
  946. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
  947. xp_cmd.parm1 = xcvr;
  948. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  949. if(err < 0)
  950. goto out;
  951. tp->xcvr_select = xcvr;
  952. if(cmd->autoneg == AUTONEG_ENABLE) {
  953. tp->speed = 0xff; /* invalid */
  954. tp->duplex = 0xff; /* invalid */
  955. } else {
  956. tp->speed = speed;
  957. tp->duplex = cmd->duplex;
  958. }
  959. out:
  960. return err;
  961. }
  962. static void
  963. typhoon_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  964. {
  965. struct typhoon *tp = netdev_priv(dev);
  966. wol->supported = WAKE_PHY | WAKE_MAGIC;
  967. wol->wolopts = 0;
  968. if(tp->wol_events & TYPHOON_WAKE_LINK_EVENT)
  969. wol->wolopts |= WAKE_PHY;
  970. if(tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
  971. wol->wolopts |= WAKE_MAGIC;
  972. memset(&wol->sopass, 0, sizeof(wol->sopass));
  973. }
  974. static int
  975. typhoon_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  976. {
  977. struct typhoon *tp = netdev_priv(dev);
  978. if(wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
  979. return -EINVAL;
  980. tp->wol_events = 0;
  981. if(wol->wolopts & WAKE_PHY)
  982. tp->wol_events |= TYPHOON_WAKE_LINK_EVENT;
  983. if(wol->wolopts & WAKE_MAGIC)
  984. tp->wol_events |= TYPHOON_WAKE_MAGIC_PKT;
  985. return 0;
  986. }
  987. static void
  988. typhoon_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  989. {
  990. ering->rx_max_pending = RXENT_ENTRIES;
  991. ering->tx_max_pending = TXLO_ENTRIES - 1;
  992. ering->rx_pending = RXENT_ENTRIES;
  993. ering->tx_pending = TXLO_ENTRIES - 1;
  994. }
  995. static const struct ethtool_ops typhoon_ethtool_ops = {
  996. .get_settings = typhoon_get_settings,
  997. .set_settings = typhoon_set_settings,
  998. .get_drvinfo = typhoon_get_drvinfo,
  999. .get_wol = typhoon_get_wol,
  1000. .set_wol = typhoon_set_wol,
  1001. .get_link = ethtool_op_get_link,
  1002. .get_ringparam = typhoon_get_ringparam,
  1003. };
  1004. static int
  1005. typhoon_wait_interrupt(void __iomem *ioaddr)
  1006. {
  1007. int i, err = 0;
  1008. for(i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
  1009. if(ioread32(ioaddr + TYPHOON_REG_INTR_STATUS) &
  1010. TYPHOON_INTR_BOOTCMD)
  1011. goto out;
  1012. udelay(TYPHOON_UDELAY);
  1013. }
  1014. err = -ETIMEDOUT;
  1015. out:
  1016. iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
  1017. return err;
  1018. }
  1019. #define shared_offset(x) offsetof(struct typhoon_shared, x)
  1020. static void
  1021. typhoon_init_interface(struct typhoon *tp)
  1022. {
  1023. struct typhoon_interface *iface = &tp->shared->iface;
  1024. dma_addr_t shared_dma;
  1025. memset(tp->shared, 0, sizeof(struct typhoon_shared));
  1026. /* The *Hi members of iface are all init'd to zero by the memset().
  1027. */
  1028. shared_dma = tp->shared_dma + shared_offset(indexes);
  1029. iface->ringIndex = cpu_to_le32(shared_dma);
  1030. shared_dma = tp->shared_dma + shared_offset(txLo);
  1031. iface->txLoAddr = cpu_to_le32(shared_dma);
  1032. iface->txLoSize = cpu_to_le32(TXLO_ENTRIES * sizeof(struct tx_desc));
  1033. shared_dma = tp->shared_dma + shared_offset(txHi);
  1034. iface->txHiAddr = cpu_to_le32(shared_dma);
  1035. iface->txHiSize = cpu_to_le32(TXHI_ENTRIES * sizeof(struct tx_desc));
  1036. shared_dma = tp->shared_dma + shared_offset(rxBuff);
  1037. iface->rxBuffAddr = cpu_to_le32(shared_dma);
  1038. iface->rxBuffSize = cpu_to_le32(RXFREE_ENTRIES *
  1039. sizeof(struct rx_free));
  1040. shared_dma = tp->shared_dma + shared_offset(rxLo);
  1041. iface->rxLoAddr = cpu_to_le32(shared_dma);
  1042. iface->rxLoSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
  1043. shared_dma = tp->shared_dma + shared_offset(rxHi);
  1044. iface->rxHiAddr = cpu_to_le32(shared_dma);
  1045. iface->rxHiSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
  1046. shared_dma = tp->shared_dma + shared_offset(cmd);
  1047. iface->cmdAddr = cpu_to_le32(shared_dma);
  1048. iface->cmdSize = cpu_to_le32(COMMAND_RING_SIZE);
  1049. shared_dma = tp->shared_dma + shared_offset(resp);
  1050. iface->respAddr = cpu_to_le32(shared_dma);
  1051. iface->respSize = cpu_to_le32(RESPONSE_RING_SIZE);
  1052. shared_dma = tp->shared_dma + shared_offset(zeroWord);
  1053. iface->zeroAddr = cpu_to_le32(shared_dma);
  1054. tp->indexes = &tp->shared->indexes;
  1055. tp->txLoRing.ringBase = (u8 *) tp->shared->txLo;
  1056. tp->txHiRing.ringBase = (u8 *) tp->shared->txHi;
  1057. tp->rxLoRing.ringBase = (u8 *) tp->shared->rxLo;
  1058. tp->rxHiRing.ringBase = (u8 *) tp->shared->rxHi;
  1059. tp->rxBuffRing.ringBase = (u8 *) tp->shared->rxBuff;
  1060. tp->cmdRing.ringBase = (u8 *) tp->shared->cmd;
  1061. tp->respRing.ringBase = (u8 *) tp->shared->resp;
  1062. tp->txLoRing.writeRegister = TYPHOON_REG_TX_LO_READY;
  1063. tp->txHiRing.writeRegister = TYPHOON_REG_TX_HI_READY;
  1064. tp->txlo_dma_addr = le32_to_cpu(iface->txLoAddr);
  1065. tp->card_state = Sleeping;
  1066. tp->offload = TYPHOON_OFFLOAD_IP_CHKSUM | TYPHOON_OFFLOAD_TCP_CHKSUM;
  1067. tp->offload |= TYPHOON_OFFLOAD_UDP_CHKSUM | TSO_OFFLOAD_ON;
  1068. tp->offload |= TYPHOON_OFFLOAD_VLAN;
  1069. spin_lock_init(&tp->command_lock);
  1070. /* Force the writes to the shared memory area out before continuing. */
  1071. wmb();
  1072. }
  1073. static void
  1074. typhoon_init_rings(struct typhoon *tp)
  1075. {
  1076. memset(tp->indexes, 0, sizeof(struct typhoon_indexes));
  1077. tp->txLoRing.lastWrite = 0;
  1078. tp->txHiRing.lastWrite = 0;
  1079. tp->rxLoRing.lastWrite = 0;
  1080. tp->rxHiRing.lastWrite = 0;
  1081. tp->rxBuffRing.lastWrite = 0;
  1082. tp->cmdRing.lastWrite = 0;
  1083. tp->respRing.lastWrite = 0;
  1084. tp->txLoRing.lastRead = 0;
  1085. tp->txHiRing.lastRead = 0;
  1086. }
  1087. static const struct firmware *typhoon_fw;
  1088. static int
  1089. typhoon_request_firmware(struct typhoon *tp)
  1090. {
  1091. const struct typhoon_file_header *fHdr;
  1092. const struct typhoon_section_header *sHdr;
  1093. const u8 *image_data;
  1094. u32 numSections;
  1095. u32 section_len;
  1096. u32 remaining;
  1097. int err;
  1098. if (typhoon_fw)
  1099. return 0;
  1100. err = request_firmware(&typhoon_fw, FIRMWARE_NAME, &tp->pdev->dev);
  1101. if (err) {
  1102. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  1103. FIRMWARE_NAME);
  1104. return err;
  1105. }
  1106. image_data = typhoon_fw->data;
  1107. remaining = typhoon_fw->size;
  1108. if (remaining < sizeof(struct typhoon_file_header))
  1109. goto invalid_fw;
  1110. fHdr = (struct typhoon_file_header *) image_data;
  1111. if (memcmp(fHdr->tag, "TYPHOON", 8))
  1112. goto invalid_fw;
  1113. numSections = le32_to_cpu(fHdr->numSections);
  1114. image_data += sizeof(struct typhoon_file_header);
  1115. remaining -= sizeof(struct typhoon_file_header);
  1116. while (numSections--) {
  1117. if (remaining < sizeof(struct typhoon_section_header))
  1118. goto invalid_fw;
  1119. sHdr = (struct typhoon_section_header *) image_data;
  1120. image_data += sizeof(struct typhoon_section_header);
  1121. section_len = le32_to_cpu(sHdr->len);
  1122. if (remaining < section_len)
  1123. goto invalid_fw;
  1124. image_data += section_len;
  1125. remaining -= section_len;
  1126. }
  1127. return 0;
  1128. invalid_fw:
  1129. netdev_err(tp->dev, "Invalid firmware image\n");
  1130. release_firmware(typhoon_fw);
  1131. typhoon_fw = NULL;
  1132. return -EINVAL;
  1133. }
  1134. static int
  1135. typhoon_download_firmware(struct typhoon *tp)
  1136. {
  1137. void __iomem *ioaddr = tp->ioaddr;
  1138. struct pci_dev *pdev = tp->pdev;
  1139. const struct typhoon_file_header *fHdr;
  1140. const struct typhoon_section_header *sHdr;
  1141. const u8 *image_data;
  1142. void *dpage;
  1143. dma_addr_t dpage_dma;
  1144. __sum16 csum;
  1145. u32 irqEnabled;
  1146. u32 irqMasked;
  1147. u32 numSections;
  1148. u32 section_len;
  1149. u32 len;
  1150. u32 load_addr;
  1151. u32 hmac;
  1152. int i;
  1153. int err;
  1154. image_data = typhoon_fw->data;
  1155. fHdr = (struct typhoon_file_header *) image_data;
  1156. /* Cannot just map the firmware image using pci_map_single() as
  1157. * the firmware is vmalloc()'d and may not be physically contiguous,
  1158. * so we allocate some consistent memory to copy the sections into.
  1159. */
  1160. err = -ENOMEM;
  1161. dpage = pci_alloc_consistent(pdev, PAGE_SIZE, &dpage_dma);
  1162. if(!dpage) {
  1163. netdev_err(tp->dev, "no DMA mem for firmware\n");
  1164. goto err_out;
  1165. }
  1166. irqEnabled = ioread32(ioaddr + TYPHOON_REG_INTR_ENABLE);
  1167. iowrite32(irqEnabled | TYPHOON_INTR_BOOTCMD,
  1168. ioaddr + TYPHOON_REG_INTR_ENABLE);
  1169. irqMasked = ioread32(ioaddr + TYPHOON_REG_INTR_MASK);
  1170. iowrite32(irqMasked | TYPHOON_INTR_BOOTCMD,
  1171. ioaddr + TYPHOON_REG_INTR_MASK);
  1172. err = -ETIMEDOUT;
  1173. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
  1174. netdev_err(tp->dev, "card ready timeout\n");
  1175. goto err_out_irq;
  1176. }
  1177. numSections = le32_to_cpu(fHdr->numSections);
  1178. load_addr = le32_to_cpu(fHdr->startAddr);
  1179. iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
  1180. iowrite32(load_addr, ioaddr + TYPHOON_REG_DOWNLOAD_BOOT_ADDR);
  1181. hmac = le32_to_cpu(fHdr->hmacDigest[0]);
  1182. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_0);
  1183. hmac = le32_to_cpu(fHdr->hmacDigest[1]);
  1184. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_1);
  1185. hmac = le32_to_cpu(fHdr->hmacDigest[2]);
  1186. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_2);
  1187. hmac = le32_to_cpu(fHdr->hmacDigest[3]);
  1188. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_3);
  1189. hmac = le32_to_cpu(fHdr->hmacDigest[4]);
  1190. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_4);
  1191. typhoon_post_pci_writes(ioaddr);
  1192. iowrite32(TYPHOON_BOOTCMD_RUNTIME_IMAGE, ioaddr + TYPHOON_REG_COMMAND);
  1193. image_data += sizeof(struct typhoon_file_header);
  1194. /* The ioread32() in typhoon_wait_interrupt() will force the
  1195. * last write to the command register to post, so
  1196. * we don't need a typhoon_post_pci_writes() after it.
  1197. */
  1198. for(i = 0; i < numSections; i++) {
  1199. sHdr = (struct typhoon_section_header *) image_data;
  1200. image_data += sizeof(struct typhoon_section_header);
  1201. load_addr = le32_to_cpu(sHdr->startAddr);
  1202. section_len = le32_to_cpu(sHdr->len);
  1203. while(section_len) {
  1204. len = min_t(u32, section_len, PAGE_SIZE);
  1205. if(typhoon_wait_interrupt(ioaddr) < 0 ||
  1206. ioread32(ioaddr + TYPHOON_REG_STATUS) !=
  1207. TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
  1208. netdev_err(tp->dev, "segment ready timeout\n");
  1209. goto err_out_irq;
  1210. }
  1211. /* Do an pseudo IPv4 checksum on the data -- first
  1212. * need to convert each u16 to cpu order before
  1213. * summing. Fortunately, due to the properties of
  1214. * the checksum, we can do this once, at the end.
  1215. */
  1216. csum = csum_fold(csum_partial_copy_nocheck(image_data,
  1217. dpage, len,
  1218. 0));
  1219. iowrite32(len, ioaddr + TYPHOON_REG_BOOT_LENGTH);
  1220. iowrite32(le16_to_cpu((__force __le16)csum),
  1221. ioaddr + TYPHOON_REG_BOOT_CHECKSUM);
  1222. iowrite32(load_addr,
  1223. ioaddr + TYPHOON_REG_BOOT_DEST_ADDR);
  1224. iowrite32(0, ioaddr + TYPHOON_REG_BOOT_DATA_HI);
  1225. iowrite32(dpage_dma, ioaddr + TYPHOON_REG_BOOT_DATA_LO);
  1226. typhoon_post_pci_writes(ioaddr);
  1227. iowrite32(TYPHOON_BOOTCMD_SEG_AVAILABLE,
  1228. ioaddr + TYPHOON_REG_COMMAND);
  1229. image_data += len;
  1230. load_addr += len;
  1231. section_len -= len;
  1232. }
  1233. }
  1234. if(typhoon_wait_interrupt(ioaddr) < 0 ||
  1235. ioread32(ioaddr + TYPHOON_REG_STATUS) !=
  1236. TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
  1237. netdev_err(tp->dev, "final segment ready timeout\n");
  1238. goto err_out_irq;
  1239. }
  1240. iowrite32(TYPHOON_BOOTCMD_DNLD_COMPLETE, ioaddr + TYPHOON_REG_COMMAND);
  1241. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
  1242. netdev_err(tp->dev, "boot ready timeout, status 0x%0x\n",
  1243. ioread32(ioaddr + TYPHOON_REG_STATUS));
  1244. goto err_out_irq;
  1245. }
  1246. err = 0;
  1247. err_out_irq:
  1248. iowrite32(irqMasked, ioaddr + TYPHOON_REG_INTR_MASK);
  1249. iowrite32(irqEnabled, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1250. pci_free_consistent(pdev, PAGE_SIZE, dpage, dpage_dma);
  1251. err_out:
  1252. return err;
  1253. }
  1254. static int
  1255. typhoon_boot_3XP(struct typhoon *tp, u32 initial_status)
  1256. {
  1257. void __iomem *ioaddr = tp->ioaddr;
  1258. if(typhoon_wait_status(ioaddr, initial_status) < 0) {
  1259. netdev_err(tp->dev, "boot ready timeout\n");
  1260. goto out_timeout;
  1261. }
  1262. iowrite32(0, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_HI);
  1263. iowrite32(tp->shared_dma, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_LO);
  1264. typhoon_post_pci_writes(ioaddr);
  1265. iowrite32(TYPHOON_BOOTCMD_REG_BOOT_RECORD,
  1266. ioaddr + TYPHOON_REG_COMMAND);
  1267. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_RUNNING) < 0) {
  1268. netdev_err(tp->dev, "boot finish timeout (status 0x%x)\n",
  1269. ioread32(ioaddr + TYPHOON_REG_STATUS));
  1270. goto out_timeout;
  1271. }
  1272. /* Clear the Transmit and Command ready registers
  1273. */
  1274. iowrite32(0, ioaddr + TYPHOON_REG_TX_HI_READY);
  1275. iowrite32(0, ioaddr + TYPHOON_REG_CMD_READY);
  1276. iowrite32(0, ioaddr + TYPHOON_REG_TX_LO_READY);
  1277. typhoon_post_pci_writes(ioaddr);
  1278. iowrite32(TYPHOON_BOOTCMD_BOOT, ioaddr + TYPHOON_REG_COMMAND);
  1279. return 0;
  1280. out_timeout:
  1281. return -ETIMEDOUT;
  1282. }
  1283. static u32
  1284. typhoon_clean_tx(struct typhoon *tp, struct transmit_ring *txRing,
  1285. volatile __le32 * index)
  1286. {
  1287. u32 lastRead = txRing->lastRead;
  1288. struct tx_desc *tx;
  1289. dma_addr_t skb_dma;
  1290. int dma_len;
  1291. int type;
  1292. while(lastRead != le32_to_cpu(*index)) {
  1293. tx = (struct tx_desc *) (txRing->ringBase + lastRead);
  1294. type = tx->flags & TYPHOON_TYPE_MASK;
  1295. if(type == TYPHOON_TX_DESC) {
  1296. /* This tx_desc describes a packet.
  1297. */
  1298. unsigned long ptr = tx->tx_addr;
  1299. struct sk_buff *skb = (struct sk_buff *) ptr;
  1300. dev_kfree_skb_irq(skb);
  1301. } else if(type == TYPHOON_FRAG_DESC) {
  1302. /* This tx_desc describes a memory mapping. Free it.
  1303. */
  1304. skb_dma = (dma_addr_t) le32_to_cpu(tx->frag.addr);
  1305. dma_len = le16_to_cpu(tx->len);
  1306. pci_unmap_single(tp->pdev, skb_dma, dma_len,
  1307. PCI_DMA_TODEVICE);
  1308. }
  1309. tx->flags = 0;
  1310. typhoon_inc_tx_index(&lastRead, 1);
  1311. }
  1312. return lastRead;
  1313. }
  1314. static void
  1315. typhoon_tx_complete(struct typhoon *tp, struct transmit_ring *txRing,
  1316. volatile __le32 * index)
  1317. {
  1318. u32 lastRead;
  1319. int numDesc = MAX_SKB_FRAGS + 1;
  1320. /* This will need changing if we start to use the Hi Tx ring. */
  1321. lastRead = typhoon_clean_tx(tp, txRing, index);
  1322. if(netif_queue_stopped(tp->dev) && typhoon_num_free(txRing->lastWrite,
  1323. lastRead, TXLO_ENTRIES) > (numDesc + 2))
  1324. netif_wake_queue(tp->dev);
  1325. txRing->lastRead = lastRead;
  1326. smp_wmb();
  1327. }
  1328. static void
  1329. typhoon_recycle_rx_skb(struct typhoon *tp, u32 idx)
  1330. {
  1331. struct typhoon_indexes *indexes = tp->indexes;
  1332. struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
  1333. struct basic_ring *ring = &tp->rxBuffRing;
  1334. struct rx_free *r;
  1335. if((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
  1336. le32_to_cpu(indexes->rxBuffCleared)) {
  1337. /* no room in ring, just drop the skb
  1338. */
  1339. dev_kfree_skb_any(rxb->skb);
  1340. rxb->skb = NULL;
  1341. return;
  1342. }
  1343. r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
  1344. typhoon_inc_rxfree_index(&ring->lastWrite, 1);
  1345. r->virtAddr = idx;
  1346. r->physAddr = cpu_to_le32(rxb->dma_addr);
  1347. /* Tell the card about it */
  1348. wmb();
  1349. indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
  1350. }
  1351. static int
  1352. typhoon_alloc_rx_skb(struct typhoon *tp, u32 idx)
  1353. {
  1354. struct typhoon_indexes *indexes = tp->indexes;
  1355. struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
  1356. struct basic_ring *ring = &tp->rxBuffRing;
  1357. struct rx_free *r;
  1358. struct sk_buff *skb;
  1359. dma_addr_t dma_addr;
  1360. rxb->skb = NULL;
  1361. if((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
  1362. le32_to_cpu(indexes->rxBuffCleared))
  1363. return -ENOMEM;
  1364. skb = netdev_alloc_skb(tp->dev, PKT_BUF_SZ);
  1365. if(!skb)
  1366. return -ENOMEM;
  1367. #if 0
  1368. /* Please, 3com, fix the firmware to allow DMA to a unaligned
  1369. * address! Pretty please?
  1370. */
  1371. skb_reserve(skb, 2);
  1372. #endif
  1373. dma_addr = pci_map_single(tp->pdev, skb->data,
  1374. PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
  1375. /* Since no card does 64 bit DAC, the high bits will never
  1376. * change from zero.
  1377. */
  1378. r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
  1379. typhoon_inc_rxfree_index(&ring->lastWrite, 1);
  1380. r->virtAddr = idx;
  1381. r->physAddr = cpu_to_le32(dma_addr);
  1382. rxb->skb = skb;
  1383. rxb->dma_addr = dma_addr;
  1384. /* Tell the card about it */
  1385. wmb();
  1386. indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
  1387. return 0;
  1388. }
  1389. static int
  1390. typhoon_rx(struct typhoon *tp, struct basic_ring *rxRing, volatile __le32 * ready,
  1391. volatile __le32 * cleared, int budget)
  1392. {
  1393. struct rx_desc *rx;
  1394. struct sk_buff *skb, *new_skb;
  1395. struct rxbuff_ent *rxb;
  1396. dma_addr_t dma_addr;
  1397. u32 local_ready;
  1398. u32 rxaddr;
  1399. int pkt_len;
  1400. u32 idx;
  1401. __le32 csum_bits;
  1402. int received;
  1403. received = 0;
  1404. local_ready = le32_to_cpu(*ready);
  1405. rxaddr = le32_to_cpu(*cleared);
  1406. while(rxaddr != local_ready && budget > 0) {
  1407. rx = (struct rx_desc *) (rxRing->ringBase + rxaddr);
  1408. idx = rx->addr;
  1409. rxb = &tp->rxbuffers[idx];
  1410. skb = rxb->skb;
  1411. dma_addr = rxb->dma_addr;
  1412. typhoon_inc_rx_index(&rxaddr, 1);
  1413. if(rx->flags & TYPHOON_RX_ERROR) {
  1414. typhoon_recycle_rx_skb(tp, idx);
  1415. continue;
  1416. }
  1417. pkt_len = le16_to_cpu(rx->frameLen);
  1418. if(pkt_len < rx_copybreak &&
  1419. (new_skb = netdev_alloc_skb(tp->dev, pkt_len + 2)) != NULL) {
  1420. skb_reserve(new_skb, 2);
  1421. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr,
  1422. PKT_BUF_SZ,
  1423. PCI_DMA_FROMDEVICE);
  1424. skb_copy_to_linear_data(new_skb, skb->data, pkt_len);
  1425. pci_dma_sync_single_for_device(tp->pdev, dma_addr,
  1426. PKT_BUF_SZ,
  1427. PCI_DMA_FROMDEVICE);
  1428. skb_put(new_skb, pkt_len);
  1429. typhoon_recycle_rx_skb(tp, idx);
  1430. } else {
  1431. new_skb = skb;
  1432. skb_put(new_skb, pkt_len);
  1433. pci_unmap_single(tp->pdev, dma_addr, PKT_BUF_SZ,
  1434. PCI_DMA_FROMDEVICE);
  1435. typhoon_alloc_rx_skb(tp, idx);
  1436. }
  1437. new_skb->protocol = eth_type_trans(new_skb, tp->dev);
  1438. csum_bits = rx->rxStatus & (TYPHOON_RX_IP_CHK_GOOD |
  1439. TYPHOON_RX_UDP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD);
  1440. if(csum_bits ==
  1441. (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD) ||
  1442. csum_bits ==
  1443. (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_UDP_CHK_GOOD)) {
  1444. new_skb->ip_summed = CHECKSUM_UNNECESSARY;
  1445. } else
  1446. skb_checksum_none_assert(new_skb);
  1447. if (rx->rxStatus & TYPHOON_RX_VLAN)
  1448. __vlan_hwaccel_put_tag(new_skb, htons(ETH_P_8021Q),
  1449. ntohl(rx->vlanTag) & 0xffff);
  1450. netif_receive_skb(new_skb);
  1451. received++;
  1452. budget--;
  1453. }
  1454. *cleared = cpu_to_le32(rxaddr);
  1455. return received;
  1456. }
  1457. static void
  1458. typhoon_fill_free_ring(struct typhoon *tp)
  1459. {
  1460. u32 i;
  1461. for(i = 0; i < RXENT_ENTRIES; i++) {
  1462. struct rxbuff_ent *rxb = &tp->rxbuffers[i];
  1463. if(rxb->skb)
  1464. continue;
  1465. if(typhoon_alloc_rx_skb(tp, i) < 0)
  1466. break;
  1467. }
  1468. }
  1469. static int
  1470. typhoon_poll(struct napi_struct *napi, int budget)
  1471. {
  1472. struct typhoon *tp = container_of(napi, struct typhoon, napi);
  1473. struct typhoon_indexes *indexes = tp->indexes;
  1474. int work_done;
  1475. rmb();
  1476. if(!tp->awaiting_resp && indexes->respReady != indexes->respCleared)
  1477. typhoon_process_response(tp, 0, NULL);
  1478. if(le32_to_cpu(indexes->txLoCleared) != tp->txLoRing.lastRead)
  1479. typhoon_tx_complete(tp, &tp->txLoRing, &indexes->txLoCleared);
  1480. work_done = 0;
  1481. if(indexes->rxHiCleared != indexes->rxHiReady) {
  1482. work_done += typhoon_rx(tp, &tp->rxHiRing, &indexes->rxHiReady,
  1483. &indexes->rxHiCleared, budget);
  1484. }
  1485. if(indexes->rxLoCleared != indexes->rxLoReady) {
  1486. work_done += typhoon_rx(tp, &tp->rxLoRing, &indexes->rxLoReady,
  1487. &indexes->rxLoCleared, budget - work_done);
  1488. }
  1489. if(le32_to_cpu(indexes->rxBuffCleared) == tp->rxBuffRing.lastWrite) {
  1490. /* rxBuff ring is empty, try to fill it. */
  1491. typhoon_fill_free_ring(tp);
  1492. }
  1493. if (work_done < budget) {
  1494. napi_complete(napi);
  1495. iowrite32(TYPHOON_INTR_NONE,
  1496. tp->ioaddr + TYPHOON_REG_INTR_MASK);
  1497. typhoon_post_pci_writes(tp->ioaddr);
  1498. }
  1499. return work_done;
  1500. }
  1501. static irqreturn_t
  1502. typhoon_interrupt(int irq, void *dev_instance)
  1503. {
  1504. struct net_device *dev = dev_instance;
  1505. struct typhoon *tp = netdev_priv(dev);
  1506. void __iomem *ioaddr = tp->ioaddr;
  1507. u32 intr_status;
  1508. intr_status = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1509. if(!(intr_status & TYPHOON_INTR_HOST_INT))
  1510. return IRQ_NONE;
  1511. iowrite32(intr_status, ioaddr + TYPHOON_REG_INTR_STATUS);
  1512. if (napi_schedule_prep(&tp->napi)) {
  1513. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  1514. typhoon_post_pci_writes(ioaddr);
  1515. __napi_schedule(&tp->napi);
  1516. } else {
  1517. netdev_err(dev, "Error, poll already scheduled\n");
  1518. }
  1519. return IRQ_HANDLED;
  1520. }
  1521. static void
  1522. typhoon_free_rx_rings(struct typhoon *tp)
  1523. {
  1524. u32 i;
  1525. for(i = 0; i < RXENT_ENTRIES; i++) {
  1526. struct rxbuff_ent *rxb = &tp->rxbuffers[i];
  1527. if(rxb->skb) {
  1528. pci_unmap_single(tp->pdev, rxb->dma_addr, PKT_BUF_SZ,
  1529. PCI_DMA_FROMDEVICE);
  1530. dev_kfree_skb(rxb->skb);
  1531. rxb->skb = NULL;
  1532. }
  1533. }
  1534. }
  1535. static int
  1536. typhoon_sleep(struct typhoon *tp, pci_power_t state, __le16 events)
  1537. {
  1538. struct pci_dev *pdev = tp->pdev;
  1539. void __iomem *ioaddr = tp->ioaddr;
  1540. struct cmd_desc xp_cmd;
  1541. int err;
  1542. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_ENABLE_WAKE_EVENTS);
  1543. xp_cmd.parm1 = events;
  1544. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1545. if(err < 0) {
  1546. netdev_err(tp->dev, "typhoon_sleep(): wake events cmd err %d\n",
  1547. err);
  1548. return err;
  1549. }
  1550. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_GOTO_SLEEP);
  1551. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1552. if(err < 0) {
  1553. netdev_err(tp->dev, "typhoon_sleep(): sleep cmd err %d\n", err);
  1554. return err;
  1555. }
  1556. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_SLEEPING) < 0)
  1557. return -ETIMEDOUT;
  1558. /* Since we cannot monitor the status of the link while sleeping,
  1559. * tell the world it went away.
  1560. */
  1561. netif_carrier_off(tp->dev);
  1562. pci_enable_wake(tp->pdev, state, 1);
  1563. pci_disable_device(pdev);
  1564. return pci_set_power_state(pdev, state);
  1565. }
  1566. static int
  1567. typhoon_wakeup(struct typhoon *tp, int wait_type)
  1568. {
  1569. struct pci_dev *pdev = tp->pdev;
  1570. void __iomem *ioaddr = tp->ioaddr;
  1571. pci_set_power_state(pdev, PCI_D0);
  1572. pci_restore_state(pdev);
  1573. /* Post 2.x.x versions of the Sleep Image require a reset before
  1574. * we can download the Runtime Image. But let's not make users of
  1575. * the old firmware pay for the reset.
  1576. */
  1577. iowrite32(TYPHOON_BOOTCMD_WAKEUP, ioaddr + TYPHOON_REG_COMMAND);
  1578. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0 ||
  1579. (tp->capabilities & TYPHOON_WAKEUP_NEEDS_RESET))
  1580. return typhoon_reset(ioaddr, wait_type);
  1581. return 0;
  1582. }
  1583. static int
  1584. typhoon_start_runtime(struct typhoon *tp)
  1585. {
  1586. struct net_device *dev = tp->dev;
  1587. void __iomem *ioaddr = tp->ioaddr;
  1588. struct cmd_desc xp_cmd;
  1589. int err;
  1590. typhoon_init_rings(tp);
  1591. typhoon_fill_free_ring(tp);
  1592. err = typhoon_download_firmware(tp);
  1593. if(err < 0) {
  1594. netdev_err(tp->dev, "cannot load runtime on 3XP\n");
  1595. goto error_out;
  1596. }
  1597. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
  1598. netdev_err(tp->dev, "cannot boot 3XP\n");
  1599. err = -EIO;
  1600. goto error_out;
  1601. }
  1602. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAX_PKT_SIZE);
  1603. xp_cmd.parm1 = cpu_to_le16(PKT_BUF_SZ);
  1604. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1605. if(err < 0)
  1606. goto error_out;
  1607. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
  1608. xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
  1609. xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
  1610. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1611. if(err < 0)
  1612. goto error_out;
  1613. /* Disable IRQ coalescing -- we can reenable it when 3Com gives
  1614. * us some more information on how to control it.
  1615. */
  1616. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_IRQ_COALESCE_CTRL);
  1617. xp_cmd.parm1 = 0;
  1618. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1619. if(err < 0)
  1620. goto error_out;
  1621. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
  1622. xp_cmd.parm1 = tp->xcvr_select;
  1623. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1624. if(err < 0)
  1625. goto error_out;
  1626. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_VLAN_TYPE_WRITE);
  1627. xp_cmd.parm1 = cpu_to_le16(ETH_P_8021Q);
  1628. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1629. if(err < 0)
  1630. goto error_out;
  1631. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_OFFLOAD_TASKS);
  1632. xp_cmd.parm2 = tp->offload;
  1633. xp_cmd.parm3 = tp->offload;
  1634. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1635. if(err < 0)
  1636. goto error_out;
  1637. typhoon_set_rx_mode(dev);
  1638. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_ENABLE);
  1639. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1640. if(err < 0)
  1641. goto error_out;
  1642. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_ENABLE);
  1643. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1644. if(err < 0)
  1645. goto error_out;
  1646. tp->card_state = Running;
  1647. smp_wmb();
  1648. iowrite32(TYPHOON_INTR_ENABLE_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1649. iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_MASK);
  1650. typhoon_post_pci_writes(ioaddr);
  1651. return 0;
  1652. error_out:
  1653. typhoon_reset(ioaddr, WaitNoSleep);
  1654. typhoon_free_rx_rings(tp);
  1655. typhoon_init_rings(tp);
  1656. return err;
  1657. }
  1658. static int
  1659. typhoon_stop_runtime(struct typhoon *tp, int wait_type)
  1660. {
  1661. struct typhoon_indexes *indexes = tp->indexes;
  1662. struct transmit_ring *txLo = &tp->txLoRing;
  1663. void __iomem *ioaddr = tp->ioaddr;
  1664. struct cmd_desc xp_cmd;
  1665. int i;
  1666. /* Disable interrupts early, since we can't schedule a poll
  1667. * when called with !netif_running(). This will be posted
  1668. * when we force the posting of the command.
  1669. */
  1670. iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1671. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_DISABLE);
  1672. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1673. /* Wait 1/2 sec for any outstanding transmits to occur
  1674. * We'll cleanup after the reset if this times out.
  1675. */
  1676. for(i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
  1677. if(indexes->txLoCleared == cpu_to_le32(txLo->lastWrite))
  1678. break;
  1679. udelay(TYPHOON_UDELAY);
  1680. }
  1681. if(i == TYPHOON_WAIT_TIMEOUT)
  1682. netdev_err(tp->dev, "halt timed out waiting for Tx to complete\n");
  1683. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_DISABLE);
  1684. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1685. /* save the statistics so when we bring the interface up again,
  1686. * the values reported to userspace are correct.
  1687. */
  1688. tp->card_state = Sleeping;
  1689. smp_wmb();
  1690. typhoon_do_get_stats(tp);
  1691. memcpy(&tp->stats_saved, &tp->stats, sizeof(struct net_device_stats));
  1692. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_HALT);
  1693. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1694. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_HALTED) < 0)
  1695. netdev_err(tp->dev, "timed out waiting for 3XP to halt\n");
  1696. if(typhoon_reset(ioaddr, wait_type) < 0) {
  1697. netdev_err(tp->dev, "unable to reset 3XP\n");
  1698. return -ETIMEDOUT;
  1699. }
  1700. /* cleanup any outstanding Tx packets */
  1701. if(indexes->txLoCleared != cpu_to_le32(txLo->lastWrite)) {
  1702. indexes->txLoCleared = cpu_to_le32(txLo->lastWrite);
  1703. typhoon_clean_tx(tp, &tp->txLoRing, &indexes->txLoCleared);
  1704. }
  1705. return 0;
  1706. }
  1707. static void
  1708. typhoon_tx_timeout(struct net_device *dev)
  1709. {
  1710. struct typhoon *tp = netdev_priv(dev);
  1711. if(typhoon_reset(tp->ioaddr, WaitNoSleep) < 0) {
  1712. netdev_warn(dev, "could not reset in tx timeout\n");
  1713. goto truly_dead;
  1714. }
  1715. /* If we ever start using the Hi ring, it will need cleaning too */
  1716. typhoon_clean_tx(tp, &tp->txLoRing, &tp->indexes->txLoCleared);
  1717. typhoon_free_rx_rings(tp);
  1718. if(typhoon_start_runtime(tp) < 0) {
  1719. netdev_err(dev, "could not start runtime in tx timeout\n");
  1720. goto truly_dead;
  1721. }
  1722. netif_wake_queue(dev);
  1723. return;
  1724. truly_dead:
  1725. /* Reset the hardware, and turn off carrier to avoid more timeouts */
  1726. typhoon_reset(tp->ioaddr, NoWait);
  1727. netif_carrier_off(dev);
  1728. }
  1729. static int
  1730. typhoon_open(struct net_device *dev)
  1731. {
  1732. struct typhoon *tp = netdev_priv(dev);
  1733. int err;
  1734. err = typhoon_request_firmware(tp);
  1735. if (err)
  1736. goto out;
  1737. err = typhoon_wakeup(tp, WaitSleep);
  1738. if(err < 0) {
  1739. netdev_err(dev, "unable to wakeup device\n");
  1740. goto out_sleep;
  1741. }
  1742. err = request_irq(dev->irq, typhoon_interrupt, IRQF_SHARED,
  1743. dev->name, dev);
  1744. if(err < 0)
  1745. goto out_sleep;
  1746. napi_enable(&tp->napi);
  1747. err = typhoon_start_runtime(tp);
  1748. if(err < 0) {
  1749. napi_disable(&tp->napi);
  1750. goto out_irq;
  1751. }
  1752. netif_start_queue(dev);
  1753. return 0;
  1754. out_irq:
  1755. free_irq(dev->irq, dev);
  1756. out_sleep:
  1757. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
  1758. netdev_err(dev, "unable to reboot into sleep img\n");
  1759. typhoon_reset(tp->ioaddr, NoWait);
  1760. goto out;
  1761. }
  1762. if(typhoon_sleep(tp, PCI_D3hot, 0) < 0)
  1763. netdev_err(dev, "unable to go back to sleep\n");
  1764. out:
  1765. return err;
  1766. }
  1767. static int
  1768. typhoon_close(struct net_device *dev)
  1769. {
  1770. struct typhoon *tp = netdev_priv(dev);
  1771. netif_stop_queue(dev);
  1772. napi_disable(&tp->napi);
  1773. if(typhoon_stop_runtime(tp, WaitSleep) < 0)
  1774. netdev_err(dev, "unable to stop runtime\n");
  1775. /* Make sure there is no irq handler running on a different CPU. */
  1776. free_irq(dev->irq, dev);
  1777. typhoon_free_rx_rings(tp);
  1778. typhoon_init_rings(tp);
  1779. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0)
  1780. netdev_err(dev, "unable to boot sleep image\n");
  1781. if(typhoon_sleep(tp, PCI_D3hot, 0) < 0)
  1782. netdev_err(dev, "unable to put card to sleep\n");
  1783. return 0;
  1784. }
  1785. #ifdef CONFIG_PM
  1786. static int
  1787. typhoon_resume(struct pci_dev *pdev)
  1788. {
  1789. struct net_device *dev = pci_get_drvdata(pdev);
  1790. struct typhoon *tp = netdev_priv(dev);
  1791. /* If we're down, resume when we are upped.
  1792. */
  1793. if(!netif_running(dev))
  1794. return 0;
  1795. if(typhoon_wakeup(tp, WaitNoSleep) < 0) {
  1796. netdev_err(dev, "critical: could not wake up in resume\n");
  1797. goto reset;
  1798. }
  1799. if(typhoon_start_runtime(tp) < 0) {
  1800. netdev_err(dev, "critical: could not start runtime in resume\n");
  1801. goto reset;
  1802. }
  1803. netif_device_attach(dev);
  1804. return 0;
  1805. reset:
  1806. typhoon_reset(tp->ioaddr, NoWait);
  1807. return -EBUSY;
  1808. }
  1809. static int
  1810. typhoon_suspend(struct pci_dev *pdev, pm_message_t state)
  1811. {
  1812. struct net_device *dev = pci_get_drvdata(pdev);
  1813. struct typhoon *tp = netdev_priv(dev);
  1814. struct cmd_desc xp_cmd;
  1815. /* If we're down, we're already suspended.
  1816. */
  1817. if(!netif_running(dev))
  1818. return 0;
  1819. /* TYPHOON_OFFLOAD_VLAN is always on now, so this doesn't work */
  1820. if(tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
  1821. netdev_warn(dev, "cannot do WAKE_MAGIC with VLAN offloading\n");
  1822. netif_device_detach(dev);
  1823. if(typhoon_stop_runtime(tp, WaitNoSleep) < 0) {
  1824. netdev_err(dev, "unable to stop runtime\n");
  1825. goto need_resume;
  1826. }
  1827. typhoon_free_rx_rings(tp);
  1828. typhoon_init_rings(tp);
  1829. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
  1830. netdev_err(dev, "unable to boot sleep image\n");
  1831. goto need_resume;
  1832. }
  1833. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
  1834. xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
  1835. xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
  1836. if(typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
  1837. netdev_err(dev, "unable to set mac address in suspend\n");
  1838. goto need_resume;
  1839. }
  1840. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
  1841. xp_cmd.parm1 = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
  1842. if(typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
  1843. netdev_err(dev, "unable to set rx filter in suspend\n");
  1844. goto need_resume;
  1845. }
  1846. if(typhoon_sleep(tp, pci_choose_state(pdev, state), tp->wol_events) < 0) {
  1847. netdev_err(dev, "unable to put card to sleep\n");
  1848. goto need_resume;
  1849. }
  1850. return 0;
  1851. need_resume:
  1852. typhoon_resume(pdev);
  1853. return -EBUSY;
  1854. }
  1855. #endif
  1856. static int
  1857. typhoon_test_mmio(struct pci_dev *pdev)
  1858. {
  1859. void __iomem *ioaddr = pci_iomap(pdev, 1, 128);
  1860. int mode = 0;
  1861. u32 val;
  1862. if(!ioaddr)
  1863. goto out;
  1864. if(ioread32(ioaddr + TYPHOON_REG_STATUS) !=
  1865. TYPHOON_STATUS_WAITING_FOR_HOST)
  1866. goto out_unmap;
  1867. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  1868. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
  1869. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1870. /* Ok, see if we can change our interrupt status register by
  1871. * sending ourselves an interrupt. If so, then MMIO works.
  1872. * The 50usec delay is arbitrary -- it could probably be smaller.
  1873. */
  1874. val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1875. if((val & TYPHOON_INTR_SELF) == 0) {
  1876. iowrite32(1, ioaddr + TYPHOON_REG_SELF_INTERRUPT);
  1877. ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1878. udelay(50);
  1879. val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1880. if(val & TYPHOON_INTR_SELF)
  1881. mode = 1;
  1882. }
  1883. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  1884. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
  1885. iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1886. ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1887. out_unmap:
  1888. pci_iounmap(pdev, ioaddr);
  1889. out:
  1890. if(!mode)
  1891. pr_info("%s: falling back to port IO\n", pci_name(pdev));
  1892. return mode;
  1893. }
  1894. static const struct net_device_ops typhoon_netdev_ops = {
  1895. .ndo_open = typhoon_open,
  1896. .ndo_stop = typhoon_close,
  1897. .ndo_start_xmit = typhoon_start_tx,
  1898. .ndo_set_rx_mode = typhoon_set_rx_mode,
  1899. .ndo_tx_timeout = typhoon_tx_timeout,
  1900. .ndo_get_stats = typhoon_get_stats,
  1901. .ndo_validate_addr = eth_validate_addr,
  1902. .ndo_set_mac_address = eth_mac_addr,
  1903. .ndo_change_mtu = eth_change_mtu,
  1904. };
  1905. static int
  1906. typhoon_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1907. {
  1908. struct net_device *dev;
  1909. struct typhoon *tp;
  1910. int card_id = (int) ent->driver_data;
  1911. void __iomem *ioaddr;
  1912. void *shared;
  1913. dma_addr_t shared_dma;
  1914. struct cmd_desc xp_cmd;
  1915. struct resp_desc xp_resp[3];
  1916. int err = 0;
  1917. const char *err_msg;
  1918. dev = alloc_etherdev(sizeof(*tp));
  1919. if(dev == NULL) {
  1920. err_msg = "unable to alloc new net device";
  1921. err = -ENOMEM;
  1922. goto error_out;
  1923. }
  1924. SET_NETDEV_DEV(dev, &pdev->dev);
  1925. err = pci_enable_device(pdev);
  1926. if(err < 0) {
  1927. err_msg = "unable to enable device";
  1928. goto error_out_dev;
  1929. }
  1930. err = pci_set_mwi(pdev);
  1931. if(err < 0) {
  1932. err_msg = "unable to set MWI";
  1933. goto error_out_disable;
  1934. }
  1935. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1936. if(err < 0) {
  1937. err_msg = "No usable DMA configuration";
  1938. goto error_out_mwi;
  1939. }
  1940. /* sanity checks on IO and MMIO BARs
  1941. */
  1942. if(!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
  1943. err_msg = "region #1 not a PCI IO resource, aborting";
  1944. err = -ENODEV;
  1945. goto error_out_mwi;
  1946. }
  1947. if(pci_resource_len(pdev, 0) < 128) {
  1948. err_msg = "Invalid PCI IO region size, aborting";
  1949. err = -ENODEV;
  1950. goto error_out_mwi;
  1951. }
  1952. if(!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
  1953. err_msg = "region #1 not a PCI MMIO resource, aborting";
  1954. err = -ENODEV;
  1955. goto error_out_mwi;
  1956. }
  1957. if(pci_resource_len(pdev, 1) < 128) {
  1958. err_msg = "Invalid PCI MMIO region size, aborting";
  1959. err = -ENODEV;
  1960. goto error_out_mwi;
  1961. }
  1962. err = pci_request_regions(pdev, KBUILD_MODNAME);
  1963. if(err < 0) {
  1964. err_msg = "could not request regions";
  1965. goto error_out_mwi;
  1966. }
  1967. /* map our registers
  1968. */
  1969. if(use_mmio != 0 && use_mmio != 1)
  1970. use_mmio = typhoon_test_mmio(pdev);
  1971. ioaddr = pci_iomap(pdev, use_mmio, 128);
  1972. if (!ioaddr) {
  1973. err_msg = "cannot remap registers, aborting";
  1974. err = -EIO;
  1975. goto error_out_regions;
  1976. }
  1977. /* allocate pci dma space for rx and tx descriptor rings
  1978. */
  1979. shared = pci_alloc_consistent(pdev, sizeof(struct typhoon_shared),
  1980. &shared_dma);
  1981. if(!shared) {
  1982. err_msg = "could not allocate DMA memory";
  1983. err = -ENOMEM;
  1984. goto error_out_remap;
  1985. }
  1986. dev->irq = pdev->irq;
  1987. tp = netdev_priv(dev);
  1988. tp->shared = shared;
  1989. tp->shared_dma = shared_dma;
  1990. tp->pdev = pdev;
  1991. tp->tx_pdev = pdev;
  1992. tp->ioaddr = ioaddr;
  1993. tp->tx_ioaddr = ioaddr;
  1994. tp->dev = dev;
  1995. /* Init sequence:
  1996. * 1) Reset the adapter to clear any bad juju
  1997. * 2) Reload the sleep image
  1998. * 3) Boot the sleep image
  1999. * 4) Get the hardware address.
  2000. * 5) Put the card to sleep.
  2001. */
  2002. err = typhoon_reset(ioaddr, WaitSleep);
  2003. if (err < 0) {
  2004. err_msg = "could not reset 3XP";
  2005. goto error_out_dma;
  2006. }
  2007. /* Now that we've reset the 3XP and are sure it's not going to
  2008. * write all over memory, enable bus mastering, and save our
  2009. * state for resuming after a suspend.
  2010. */
  2011. pci_set_master(pdev);
  2012. pci_save_state(pdev);
  2013. typhoon_init_interface(tp);
  2014. typhoon_init_rings(tp);
  2015. err = typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST);
  2016. if (err < 0) {
  2017. err_msg = "cannot boot 3XP sleep image";
  2018. goto error_out_reset;
  2019. }
  2020. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_MAC_ADDRESS);
  2021. err = typhoon_issue_command(tp, 1, &xp_cmd, 1, xp_resp);
  2022. if (err < 0) {
  2023. err_msg = "cannot read MAC address";
  2024. goto error_out_reset;
  2025. }
  2026. *(__be16 *)&dev->dev_addr[0] = htons(le16_to_cpu(xp_resp[0].parm1));
  2027. *(__be32 *)&dev->dev_addr[2] = htonl(le32_to_cpu(xp_resp[0].parm2));
  2028. if (!is_valid_ether_addr(dev->dev_addr)) {
  2029. err_msg = "Could not obtain valid ethernet address, aborting";
  2030. err = -EIO;
  2031. goto error_out_reset;
  2032. }
  2033. /* Read the Sleep Image version last, so the response is valid
  2034. * later when we print out the version reported.
  2035. */
  2036. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
  2037. err = typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp);
  2038. if (err < 0) {
  2039. err_msg = "Could not get Sleep Image version";
  2040. goto error_out_reset;
  2041. }
  2042. tp->capabilities = typhoon_card_info[card_id].capabilities;
  2043. tp->xcvr_select = TYPHOON_XCVR_AUTONEG;
  2044. /* Typhoon 1.0 Sleep Images return one response descriptor to the
  2045. * READ_VERSIONS command. Those versions are OK after waking up
  2046. * from sleep without needing a reset. Typhoon 1.1+ Sleep Images
  2047. * seem to need a little extra help to get started. Since we don't
  2048. * know how to nudge it along, just kick it.
  2049. */
  2050. if(xp_resp[0].numDesc != 0)
  2051. tp->capabilities |= TYPHOON_WAKEUP_NEEDS_RESET;
  2052. err = typhoon_sleep(tp, PCI_D3hot, 0);
  2053. if (err < 0) {
  2054. err_msg = "cannot put adapter to sleep";
  2055. goto error_out_reset;
  2056. }
  2057. /* The chip-specific entries in the device structure. */
  2058. dev->netdev_ops = &typhoon_netdev_ops;
  2059. netif_napi_add(dev, &tp->napi, typhoon_poll, 16);
  2060. dev->watchdog_timeo = TX_TIMEOUT;
  2061. dev->ethtool_ops = &typhoon_ethtool_ops;
  2062. /* We can handle scatter gather, up to 16 entries, and
  2063. * we can do IP checksumming (only version 4, doh...)
  2064. *
  2065. * There's no way to turn off the RX VLAN offloading and stripping
  2066. * on the current 3XP firmware -- it does not respect the offload
  2067. * settings -- so we only allow the user to toggle the TX processing.
  2068. */
  2069. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  2070. NETIF_F_HW_VLAN_CTAG_TX;
  2071. dev->features = dev->hw_features |
  2072. NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_RXCSUM;
  2073. err = register_netdev(dev);
  2074. if (err < 0) {
  2075. err_msg = "unable to register netdev";
  2076. goto error_out_reset;
  2077. }
  2078. pci_set_drvdata(pdev, dev);
  2079. netdev_info(dev, "%s at %s 0x%llx, %pM\n",
  2080. typhoon_card_info[card_id].name,
  2081. use_mmio ? "MMIO" : "IO",
  2082. (unsigned long long)pci_resource_start(pdev, use_mmio),
  2083. dev->dev_addr);
  2084. /* xp_resp still contains the response to the READ_VERSIONS command.
  2085. * For debugging, let the user know what version he has.
  2086. */
  2087. if(xp_resp[0].numDesc == 0) {
  2088. /* This is the Typhoon 1.0 type Sleep Image, last 16 bits
  2089. * of version is Month/Day of build.
  2090. */
  2091. u16 monthday = le32_to_cpu(xp_resp[0].parm2) & 0xffff;
  2092. netdev_info(dev, "Typhoon 1.0 Sleep Image built %02u/%02u/2000\n",
  2093. monthday >> 8, monthday & 0xff);
  2094. } else if(xp_resp[0].numDesc == 2) {
  2095. /* This is the Typhoon 1.1+ type Sleep Image
  2096. */
  2097. u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
  2098. u8 *ver_string = (u8 *) &xp_resp[1];
  2099. ver_string[25] = 0;
  2100. netdev_info(dev, "Typhoon 1.1+ Sleep Image version %02x.%03x.%03x %s\n",
  2101. sleep_ver >> 24, (sleep_ver >> 12) & 0xfff,
  2102. sleep_ver & 0xfff, ver_string);
  2103. } else {
  2104. netdev_warn(dev, "Unknown Sleep Image version (%u:%04x)\n",
  2105. xp_resp[0].numDesc, le32_to_cpu(xp_resp[0].parm2));
  2106. }
  2107. return 0;
  2108. error_out_reset:
  2109. typhoon_reset(ioaddr, NoWait);
  2110. error_out_dma:
  2111. pci_free_consistent(pdev, sizeof(struct typhoon_shared),
  2112. shared, shared_dma);
  2113. error_out_remap:
  2114. pci_iounmap(pdev, ioaddr);
  2115. error_out_regions:
  2116. pci_release_regions(pdev);
  2117. error_out_mwi:
  2118. pci_clear_mwi(pdev);
  2119. error_out_disable:
  2120. pci_disable_device(pdev);
  2121. error_out_dev:
  2122. free_netdev(dev);
  2123. error_out:
  2124. pr_err("%s: %s\n", pci_name(pdev), err_msg);
  2125. return err;
  2126. }
  2127. static void
  2128. typhoon_remove_one(struct pci_dev *pdev)
  2129. {
  2130. struct net_device *dev = pci_get_drvdata(pdev);
  2131. struct typhoon *tp = netdev_priv(dev);
  2132. unregister_netdev(dev);
  2133. pci_set_power_state(pdev, PCI_D0);
  2134. pci_restore_state(pdev);
  2135. typhoon_reset(tp->ioaddr, NoWait);
  2136. pci_iounmap(pdev, tp->ioaddr);
  2137. pci_free_consistent(pdev, sizeof(struct typhoon_shared),
  2138. tp->shared, tp->shared_dma);
  2139. pci_release_regions(pdev);
  2140. pci_clear_mwi(pdev);
  2141. pci_disable_device(pdev);
  2142. free_netdev(dev);
  2143. }
  2144. static struct pci_driver typhoon_driver = {
  2145. .name = KBUILD_MODNAME,
  2146. .id_table = typhoon_pci_tbl,
  2147. .probe = typhoon_init_one,
  2148. .remove = typhoon_remove_one,
  2149. #ifdef CONFIG_PM
  2150. .suspend = typhoon_suspend,
  2151. .resume = typhoon_resume,
  2152. #endif
  2153. };
  2154. static int __init
  2155. typhoon_init(void)
  2156. {
  2157. return pci_register_driver(&typhoon_driver);
  2158. }
  2159. static void __exit
  2160. typhoon_cleanup(void)
  2161. {
  2162. release_firmware(typhoon_fw);
  2163. pci_unregister_driver(&typhoon_driver);
  2164. }
  2165. module_init(typhoon_init);
  2166. module_exit(typhoon_cleanup);