typhoon.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575
  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[] __devinitdata = {
  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 DEFINE_PCI_DEVICE_TABLE(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 updateing */
  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(vlan_tx_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(vlan_tx_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. 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 = frag->size;
  712. frag_addr = (void *) page_address(frag->page) +
  713. frag->page_offset;
  714. skb_dma = pci_map_single(tp->tx_pdev, frag_addr, len,
  715. PCI_DMA_TODEVICE);
  716. txd->flags = TYPHOON_FRAG_DESC | TYPHOON_DESC_VALID;
  717. txd->len = cpu_to_le16(len);
  718. txd->frag.addr = cpu_to_le32(skb_dma);
  719. txd->frag.addrHi = 0;
  720. first_txd->numDesc++;
  721. }
  722. }
  723. /* Kick the 3XP
  724. */
  725. wmb();
  726. iowrite32(txRing->lastWrite, tp->tx_ioaddr + txRing->writeRegister);
  727. /* If we don't have room to put the worst case packet on the
  728. * queue, then we must stop the queue. We need 2 extra
  729. * descriptors -- one to prevent ring wrap, and one for the
  730. * Tx header.
  731. */
  732. numDesc = MAX_SKB_FRAGS + TSO_NUM_DESCRIPTORS + 1;
  733. if(typhoon_num_free_tx(txRing) < (numDesc + 2)) {
  734. netif_stop_queue(dev);
  735. /* A Tx complete IRQ could have gotten between, making
  736. * the ring free again. Only need to recheck here, since
  737. * Tx is serialized.
  738. */
  739. if(typhoon_num_free_tx(txRing) >= (numDesc + 2))
  740. netif_wake_queue(dev);
  741. }
  742. return NETDEV_TX_OK;
  743. }
  744. static void
  745. typhoon_set_rx_mode(struct net_device *dev)
  746. {
  747. struct typhoon *tp = netdev_priv(dev);
  748. struct cmd_desc xp_cmd;
  749. u32 mc_filter[2];
  750. __le16 filter;
  751. filter = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
  752. if(dev->flags & IFF_PROMISC) {
  753. filter |= TYPHOON_RX_FILTER_PROMISCOUS;
  754. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  755. (dev->flags & IFF_ALLMULTI)) {
  756. /* Too many to match, or accept all multicasts. */
  757. filter |= TYPHOON_RX_FILTER_ALL_MCAST;
  758. } else if (!netdev_mc_empty(dev)) {
  759. struct netdev_hw_addr *ha;
  760. memset(mc_filter, 0, sizeof(mc_filter));
  761. netdev_for_each_mc_addr(ha, dev) {
  762. int bit = ether_crc(ETH_ALEN, ha->addr) & 0x3f;
  763. mc_filter[bit >> 5] |= 1 << (bit & 0x1f);
  764. }
  765. INIT_COMMAND_NO_RESPONSE(&xp_cmd,
  766. TYPHOON_CMD_SET_MULTICAST_HASH);
  767. xp_cmd.parm1 = TYPHOON_MCAST_HASH_SET;
  768. xp_cmd.parm2 = cpu_to_le32(mc_filter[0]);
  769. xp_cmd.parm3 = cpu_to_le32(mc_filter[1]);
  770. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  771. filter |= TYPHOON_RX_FILTER_MCAST_HASH;
  772. }
  773. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
  774. xp_cmd.parm1 = filter;
  775. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  776. }
  777. static int
  778. typhoon_do_get_stats(struct typhoon *tp)
  779. {
  780. struct net_device_stats *stats = &tp->stats;
  781. struct net_device_stats *saved = &tp->stats_saved;
  782. struct cmd_desc xp_cmd;
  783. struct resp_desc xp_resp[7];
  784. struct stats_resp *s = (struct stats_resp *) xp_resp;
  785. int err;
  786. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_STATS);
  787. err = typhoon_issue_command(tp, 1, &xp_cmd, 7, xp_resp);
  788. if(err < 0)
  789. return err;
  790. /* 3Com's Linux driver uses txMultipleCollisions as it's
  791. * collisions value, but there is some other collision info as well...
  792. *
  793. * The extra status reported would be a good candidate for
  794. * ethtool_ops->get_{strings,stats}()
  795. */
  796. stats->tx_packets = le32_to_cpu(s->txPackets) +
  797. saved->tx_packets;
  798. stats->tx_bytes = le64_to_cpu(s->txBytes) +
  799. saved->tx_bytes;
  800. stats->tx_errors = le32_to_cpu(s->txCarrierLost) +
  801. saved->tx_errors;
  802. stats->tx_carrier_errors = le32_to_cpu(s->txCarrierLost) +
  803. saved->tx_carrier_errors;
  804. stats->collisions = le32_to_cpu(s->txMultipleCollisions) +
  805. saved->collisions;
  806. stats->rx_packets = le32_to_cpu(s->rxPacketsGood) +
  807. saved->rx_packets;
  808. stats->rx_bytes = le64_to_cpu(s->rxBytesGood) +
  809. saved->rx_bytes;
  810. stats->rx_fifo_errors = le32_to_cpu(s->rxFifoOverruns) +
  811. saved->rx_fifo_errors;
  812. stats->rx_errors = le32_to_cpu(s->rxFifoOverruns) +
  813. le32_to_cpu(s->BadSSD) + le32_to_cpu(s->rxCrcErrors) +
  814. saved->rx_errors;
  815. stats->rx_crc_errors = le32_to_cpu(s->rxCrcErrors) +
  816. saved->rx_crc_errors;
  817. stats->rx_length_errors = le32_to_cpu(s->rxOversized) +
  818. saved->rx_length_errors;
  819. tp->speed = (s->linkStatus & TYPHOON_LINK_100MBPS) ?
  820. SPEED_100 : SPEED_10;
  821. tp->duplex = (s->linkStatus & TYPHOON_LINK_FULL_DUPLEX) ?
  822. DUPLEX_FULL : DUPLEX_HALF;
  823. return 0;
  824. }
  825. static struct net_device_stats *
  826. typhoon_get_stats(struct net_device *dev)
  827. {
  828. struct typhoon *tp = netdev_priv(dev);
  829. struct net_device_stats *stats = &tp->stats;
  830. struct net_device_stats *saved = &tp->stats_saved;
  831. smp_rmb();
  832. if(tp->card_state == Sleeping)
  833. return saved;
  834. if(typhoon_do_get_stats(tp) < 0) {
  835. netdev_err(dev, "error getting stats\n");
  836. return saved;
  837. }
  838. return stats;
  839. }
  840. static int
  841. typhoon_set_mac_address(struct net_device *dev, void *addr)
  842. {
  843. struct sockaddr *saddr = (struct sockaddr *) addr;
  844. if(netif_running(dev))
  845. return -EBUSY;
  846. memcpy(dev->dev_addr, saddr->sa_data, dev->addr_len);
  847. return 0;
  848. }
  849. static void
  850. typhoon_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  851. {
  852. struct typhoon *tp = netdev_priv(dev);
  853. struct pci_dev *pci_dev = tp->pdev;
  854. struct cmd_desc xp_cmd;
  855. struct resp_desc xp_resp[3];
  856. smp_rmb();
  857. if(tp->card_state == Sleeping) {
  858. strcpy(info->fw_version, "Sleep image");
  859. } else {
  860. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
  861. if(typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp) < 0) {
  862. strcpy(info->fw_version, "Unknown runtime");
  863. } else {
  864. u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
  865. snprintf(info->fw_version, 32, "%02x.%03x.%03x",
  866. sleep_ver >> 24, (sleep_ver >> 12) & 0xfff,
  867. sleep_ver & 0xfff);
  868. }
  869. }
  870. strcpy(info->driver, KBUILD_MODNAME);
  871. strcpy(info->bus_info, pci_name(pci_dev));
  872. }
  873. static int
  874. typhoon_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  875. {
  876. struct typhoon *tp = netdev_priv(dev);
  877. cmd->supported = SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
  878. SUPPORTED_Autoneg;
  879. switch (tp->xcvr_select) {
  880. case TYPHOON_XCVR_10HALF:
  881. cmd->advertising = ADVERTISED_10baseT_Half;
  882. break;
  883. case TYPHOON_XCVR_10FULL:
  884. cmd->advertising = ADVERTISED_10baseT_Full;
  885. break;
  886. case TYPHOON_XCVR_100HALF:
  887. cmd->advertising = ADVERTISED_100baseT_Half;
  888. break;
  889. case TYPHOON_XCVR_100FULL:
  890. cmd->advertising = ADVERTISED_100baseT_Full;
  891. break;
  892. case TYPHOON_XCVR_AUTONEG:
  893. cmd->advertising = ADVERTISED_10baseT_Half |
  894. ADVERTISED_10baseT_Full |
  895. ADVERTISED_100baseT_Half |
  896. ADVERTISED_100baseT_Full |
  897. ADVERTISED_Autoneg;
  898. break;
  899. }
  900. if(tp->capabilities & TYPHOON_FIBER) {
  901. cmd->supported |= SUPPORTED_FIBRE;
  902. cmd->advertising |= ADVERTISED_FIBRE;
  903. cmd->port = PORT_FIBRE;
  904. } else {
  905. cmd->supported |= SUPPORTED_10baseT_Half |
  906. SUPPORTED_10baseT_Full |
  907. SUPPORTED_TP;
  908. cmd->advertising |= ADVERTISED_TP;
  909. cmd->port = PORT_TP;
  910. }
  911. /* need to get stats to make these link speed/duplex valid */
  912. typhoon_do_get_stats(tp);
  913. ethtool_cmd_speed_set(cmd, tp->speed);
  914. cmd->duplex = tp->duplex;
  915. cmd->phy_address = 0;
  916. cmd->transceiver = XCVR_INTERNAL;
  917. if(tp->xcvr_select == TYPHOON_XCVR_AUTONEG)
  918. cmd->autoneg = AUTONEG_ENABLE;
  919. else
  920. cmd->autoneg = AUTONEG_DISABLE;
  921. cmd->maxtxpkt = 1;
  922. cmd->maxrxpkt = 1;
  923. return 0;
  924. }
  925. static int
  926. typhoon_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  927. {
  928. struct typhoon *tp = netdev_priv(dev);
  929. u32 speed = ethtool_cmd_speed(cmd);
  930. struct cmd_desc xp_cmd;
  931. __le16 xcvr;
  932. int err;
  933. err = -EINVAL;
  934. if (cmd->autoneg == AUTONEG_ENABLE) {
  935. xcvr = TYPHOON_XCVR_AUTONEG;
  936. } else {
  937. if (cmd->duplex == DUPLEX_HALF) {
  938. if (speed == SPEED_10)
  939. xcvr = TYPHOON_XCVR_10HALF;
  940. else if (speed == SPEED_100)
  941. xcvr = TYPHOON_XCVR_100HALF;
  942. else
  943. goto out;
  944. } else if (cmd->duplex == DUPLEX_FULL) {
  945. if (speed == SPEED_10)
  946. xcvr = TYPHOON_XCVR_10FULL;
  947. else if (speed == SPEED_100)
  948. xcvr = TYPHOON_XCVR_100FULL;
  949. else
  950. goto out;
  951. } else
  952. goto out;
  953. }
  954. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
  955. xp_cmd.parm1 = xcvr;
  956. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  957. if(err < 0)
  958. goto out;
  959. tp->xcvr_select = xcvr;
  960. if(cmd->autoneg == AUTONEG_ENABLE) {
  961. tp->speed = 0xff; /* invalid */
  962. tp->duplex = 0xff; /* invalid */
  963. } else {
  964. tp->speed = speed;
  965. tp->duplex = cmd->duplex;
  966. }
  967. out:
  968. return err;
  969. }
  970. static void
  971. typhoon_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  972. {
  973. struct typhoon *tp = netdev_priv(dev);
  974. wol->supported = WAKE_PHY | WAKE_MAGIC;
  975. wol->wolopts = 0;
  976. if(tp->wol_events & TYPHOON_WAKE_LINK_EVENT)
  977. wol->wolopts |= WAKE_PHY;
  978. if(tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
  979. wol->wolopts |= WAKE_MAGIC;
  980. memset(&wol->sopass, 0, sizeof(wol->sopass));
  981. }
  982. static int
  983. typhoon_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  984. {
  985. struct typhoon *tp = netdev_priv(dev);
  986. if(wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC))
  987. return -EINVAL;
  988. tp->wol_events = 0;
  989. if(wol->wolopts & WAKE_PHY)
  990. tp->wol_events |= TYPHOON_WAKE_LINK_EVENT;
  991. if(wol->wolopts & WAKE_MAGIC)
  992. tp->wol_events |= TYPHOON_WAKE_MAGIC_PKT;
  993. return 0;
  994. }
  995. static void
  996. typhoon_get_ringparam(struct net_device *dev, struct ethtool_ringparam *ering)
  997. {
  998. ering->rx_max_pending = RXENT_ENTRIES;
  999. ering->rx_mini_max_pending = 0;
  1000. ering->rx_jumbo_max_pending = 0;
  1001. ering->tx_max_pending = TXLO_ENTRIES - 1;
  1002. ering->rx_pending = RXENT_ENTRIES;
  1003. ering->rx_mini_pending = 0;
  1004. ering->rx_jumbo_pending = 0;
  1005. ering->tx_pending = TXLO_ENTRIES - 1;
  1006. }
  1007. static const struct ethtool_ops typhoon_ethtool_ops = {
  1008. .get_settings = typhoon_get_settings,
  1009. .set_settings = typhoon_set_settings,
  1010. .get_drvinfo = typhoon_get_drvinfo,
  1011. .get_wol = typhoon_get_wol,
  1012. .set_wol = typhoon_set_wol,
  1013. .get_link = ethtool_op_get_link,
  1014. .get_ringparam = typhoon_get_ringparam,
  1015. };
  1016. static int
  1017. typhoon_wait_interrupt(void __iomem *ioaddr)
  1018. {
  1019. int i, err = 0;
  1020. for(i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
  1021. if(ioread32(ioaddr + TYPHOON_REG_INTR_STATUS) &
  1022. TYPHOON_INTR_BOOTCMD)
  1023. goto out;
  1024. udelay(TYPHOON_UDELAY);
  1025. }
  1026. err = -ETIMEDOUT;
  1027. out:
  1028. iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
  1029. return err;
  1030. }
  1031. #define shared_offset(x) offsetof(struct typhoon_shared, x)
  1032. static void
  1033. typhoon_init_interface(struct typhoon *tp)
  1034. {
  1035. struct typhoon_interface *iface = &tp->shared->iface;
  1036. dma_addr_t shared_dma;
  1037. memset(tp->shared, 0, sizeof(struct typhoon_shared));
  1038. /* The *Hi members of iface are all init'd to zero by the memset().
  1039. */
  1040. shared_dma = tp->shared_dma + shared_offset(indexes);
  1041. iface->ringIndex = cpu_to_le32(shared_dma);
  1042. shared_dma = tp->shared_dma + shared_offset(txLo);
  1043. iface->txLoAddr = cpu_to_le32(shared_dma);
  1044. iface->txLoSize = cpu_to_le32(TXLO_ENTRIES * sizeof(struct tx_desc));
  1045. shared_dma = tp->shared_dma + shared_offset(txHi);
  1046. iface->txHiAddr = cpu_to_le32(shared_dma);
  1047. iface->txHiSize = cpu_to_le32(TXHI_ENTRIES * sizeof(struct tx_desc));
  1048. shared_dma = tp->shared_dma + shared_offset(rxBuff);
  1049. iface->rxBuffAddr = cpu_to_le32(shared_dma);
  1050. iface->rxBuffSize = cpu_to_le32(RXFREE_ENTRIES *
  1051. sizeof(struct rx_free));
  1052. shared_dma = tp->shared_dma + shared_offset(rxLo);
  1053. iface->rxLoAddr = cpu_to_le32(shared_dma);
  1054. iface->rxLoSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
  1055. shared_dma = tp->shared_dma + shared_offset(rxHi);
  1056. iface->rxHiAddr = cpu_to_le32(shared_dma);
  1057. iface->rxHiSize = cpu_to_le32(RX_ENTRIES * sizeof(struct rx_desc));
  1058. shared_dma = tp->shared_dma + shared_offset(cmd);
  1059. iface->cmdAddr = cpu_to_le32(shared_dma);
  1060. iface->cmdSize = cpu_to_le32(COMMAND_RING_SIZE);
  1061. shared_dma = tp->shared_dma + shared_offset(resp);
  1062. iface->respAddr = cpu_to_le32(shared_dma);
  1063. iface->respSize = cpu_to_le32(RESPONSE_RING_SIZE);
  1064. shared_dma = tp->shared_dma + shared_offset(zeroWord);
  1065. iface->zeroAddr = cpu_to_le32(shared_dma);
  1066. tp->indexes = &tp->shared->indexes;
  1067. tp->txLoRing.ringBase = (u8 *) tp->shared->txLo;
  1068. tp->txHiRing.ringBase = (u8 *) tp->shared->txHi;
  1069. tp->rxLoRing.ringBase = (u8 *) tp->shared->rxLo;
  1070. tp->rxHiRing.ringBase = (u8 *) tp->shared->rxHi;
  1071. tp->rxBuffRing.ringBase = (u8 *) tp->shared->rxBuff;
  1072. tp->cmdRing.ringBase = (u8 *) tp->shared->cmd;
  1073. tp->respRing.ringBase = (u8 *) tp->shared->resp;
  1074. tp->txLoRing.writeRegister = TYPHOON_REG_TX_LO_READY;
  1075. tp->txHiRing.writeRegister = TYPHOON_REG_TX_HI_READY;
  1076. tp->txlo_dma_addr = le32_to_cpu(iface->txLoAddr);
  1077. tp->card_state = Sleeping;
  1078. tp->offload = TYPHOON_OFFLOAD_IP_CHKSUM | TYPHOON_OFFLOAD_TCP_CHKSUM;
  1079. tp->offload |= TYPHOON_OFFLOAD_UDP_CHKSUM | TSO_OFFLOAD_ON;
  1080. tp->offload |= TYPHOON_OFFLOAD_VLAN;
  1081. spin_lock_init(&tp->command_lock);
  1082. /* Force the writes to the shared memory area out before continuing. */
  1083. wmb();
  1084. }
  1085. static void
  1086. typhoon_init_rings(struct typhoon *tp)
  1087. {
  1088. memset(tp->indexes, 0, sizeof(struct typhoon_indexes));
  1089. tp->txLoRing.lastWrite = 0;
  1090. tp->txHiRing.lastWrite = 0;
  1091. tp->rxLoRing.lastWrite = 0;
  1092. tp->rxHiRing.lastWrite = 0;
  1093. tp->rxBuffRing.lastWrite = 0;
  1094. tp->cmdRing.lastWrite = 0;
  1095. tp->respRing.lastWrite = 0;
  1096. tp->txLoRing.lastRead = 0;
  1097. tp->txHiRing.lastRead = 0;
  1098. }
  1099. static const struct firmware *typhoon_fw;
  1100. static int
  1101. typhoon_request_firmware(struct typhoon *tp)
  1102. {
  1103. const struct typhoon_file_header *fHdr;
  1104. const struct typhoon_section_header *sHdr;
  1105. const u8 *image_data;
  1106. u32 numSections;
  1107. u32 section_len;
  1108. u32 remaining;
  1109. int err;
  1110. if (typhoon_fw)
  1111. return 0;
  1112. err = request_firmware(&typhoon_fw, FIRMWARE_NAME, &tp->pdev->dev);
  1113. if (err) {
  1114. netdev_err(tp->dev, "Failed to load firmware \"%s\"\n",
  1115. FIRMWARE_NAME);
  1116. return err;
  1117. }
  1118. image_data = (u8 *) typhoon_fw->data;
  1119. remaining = typhoon_fw->size;
  1120. if (remaining < sizeof(struct typhoon_file_header))
  1121. goto invalid_fw;
  1122. fHdr = (struct typhoon_file_header *) image_data;
  1123. if (memcmp(fHdr->tag, "TYPHOON", 8))
  1124. goto invalid_fw;
  1125. numSections = le32_to_cpu(fHdr->numSections);
  1126. image_data += sizeof(struct typhoon_file_header);
  1127. remaining -= sizeof(struct typhoon_file_header);
  1128. while (numSections--) {
  1129. if (remaining < sizeof(struct typhoon_section_header))
  1130. goto invalid_fw;
  1131. sHdr = (struct typhoon_section_header *) image_data;
  1132. image_data += sizeof(struct typhoon_section_header);
  1133. section_len = le32_to_cpu(sHdr->len);
  1134. if (remaining < section_len)
  1135. goto invalid_fw;
  1136. image_data += section_len;
  1137. remaining -= section_len;
  1138. }
  1139. return 0;
  1140. invalid_fw:
  1141. netdev_err(tp->dev, "Invalid firmware image\n");
  1142. release_firmware(typhoon_fw);
  1143. typhoon_fw = NULL;
  1144. return -EINVAL;
  1145. }
  1146. static int
  1147. typhoon_download_firmware(struct typhoon *tp)
  1148. {
  1149. void __iomem *ioaddr = tp->ioaddr;
  1150. struct pci_dev *pdev = tp->pdev;
  1151. const struct typhoon_file_header *fHdr;
  1152. const struct typhoon_section_header *sHdr;
  1153. const u8 *image_data;
  1154. void *dpage;
  1155. dma_addr_t dpage_dma;
  1156. __sum16 csum;
  1157. u32 irqEnabled;
  1158. u32 irqMasked;
  1159. u32 numSections;
  1160. u32 section_len;
  1161. u32 len;
  1162. u32 load_addr;
  1163. u32 hmac;
  1164. int i;
  1165. int err;
  1166. image_data = (u8 *) typhoon_fw->data;
  1167. fHdr = (struct typhoon_file_header *) image_data;
  1168. /* Cannot just map the firmware image using pci_map_single() as
  1169. * the firmware is vmalloc()'d and may not be physically contiguous,
  1170. * so we allocate some consistent memory to copy the sections into.
  1171. */
  1172. err = -ENOMEM;
  1173. dpage = pci_alloc_consistent(pdev, PAGE_SIZE, &dpage_dma);
  1174. if(!dpage) {
  1175. netdev_err(tp->dev, "no DMA mem for firmware\n");
  1176. goto err_out;
  1177. }
  1178. irqEnabled = ioread32(ioaddr + TYPHOON_REG_INTR_ENABLE);
  1179. iowrite32(irqEnabled | TYPHOON_INTR_BOOTCMD,
  1180. ioaddr + TYPHOON_REG_INTR_ENABLE);
  1181. irqMasked = ioread32(ioaddr + TYPHOON_REG_INTR_MASK);
  1182. iowrite32(irqMasked | TYPHOON_INTR_BOOTCMD,
  1183. ioaddr + TYPHOON_REG_INTR_MASK);
  1184. err = -ETIMEDOUT;
  1185. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
  1186. netdev_err(tp->dev, "card ready timeout\n");
  1187. goto err_out_irq;
  1188. }
  1189. numSections = le32_to_cpu(fHdr->numSections);
  1190. load_addr = le32_to_cpu(fHdr->startAddr);
  1191. iowrite32(TYPHOON_INTR_BOOTCMD, ioaddr + TYPHOON_REG_INTR_STATUS);
  1192. iowrite32(load_addr, ioaddr + TYPHOON_REG_DOWNLOAD_BOOT_ADDR);
  1193. hmac = le32_to_cpu(fHdr->hmacDigest[0]);
  1194. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_0);
  1195. hmac = le32_to_cpu(fHdr->hmacDigest[1]);
  1196. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_1);
  1197. hmac = le32_to_cpu(fHdr->hmacDigest[2]);
  1198. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_2);
  1199. hmac = le32_to_cpu(fHdr->hmacDigest[3]);
  1200. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_3);
  1201. hmac = le32_to_cpu(fHdr->hmacDigest[4]);
  1202. iowrite32(hmac, ioaddr + TYPHOON_REG_DOWNLOAD_HMAC_4);
  1203. typhoon_post_pci_writes(ioaddr);
  1204. iowrite32(TYPHOON_BOOTCMD_RUNTIME_IMAGE, ioaddr + TYPHOON_REG_COMMAND);
  1205. image_data += sizeof(struct typhoon_file_header);
  1206. /* The ioread32() in typhoon_wait_interrupt() will force the
  1207. * last write to the command register to post, so
  1208. * we don't need a typhoon_post_pci_writes() after it.
  1209. */
  1210. for(i = 0; i < numSections; i++) {
  1211. sHdr = (struct typhoon_section_header *) image_data;
  1212. image_data += sizeof(struct typhoon_section_header);
  1213. load_addr = le32_to_cpu(sHdr->startAddr);
  1214. section_len = le32_to_cpu(sHdr->len);
  1215. while(section_len) {
  1216. len = min_t(u32, section_len, PAGE_SIZE);
  1217. if(typhoon_wait_interrupt(ioaddr) < 0 ||
  1218. ioread32(ioaddr + TYPHOON_REG_STATUS) !=
  1219. TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
  1220. netdev_err(tp->dev, "segment ready timeout\n");
  1221. goto err_out_irq;
  1222. }
  1223. /* Do an pseudo IPv4 checksum on the data -- first
  1224. * need to convert each u16 to cpu order before
  1225. * summing. Fortunately, due to the properties of
  1226. * the checksum, we can do this once, at the end.
  1227. */
  1228. csum = csum_fold(csum_partial_copy_nocheck(image_data,
  1229. dpage, len,
  1230. 0));
  1231. iowrite32(len, ioaddr + TYPHOON_REG_BOOT_LENGTH);
  1232. iowrite32(le16_to_cpu((__force __le16)csum),
  1233. ioaddr + TYPHOON_REG_BOOT_CHECKSUM);
  1234. iowrite32(load_addr,
  1235. ioaddr + TYPHOON_REG_BOOT_DEST_ADDR);
  1236. iowrite32(0, ioaddr + TYPHOON_REG_BOOT_DATA_HI);
  1237. iowrite32(dpage_dma, ioaddr + TYPHOON_REG_BOOT_DATA_LO);
  1238. typhoon_post_pci_writes(ioaddr);
  1239. iowrite32(TYPHOON_BOOTCMD_SEG_AVAILABLE,
  1240. ioaddr + TYPHOON_REG_COMMAND);
  1241. image_data += len;
  1242. load_addr += len;
  1243. section_len -= len;
  1244. }
  1245. }
  1246. if(typhoon_wait_interrupt(ioaddr) < 0 ||
  1247. ioread32(ioaddr + TYPHOON_REG_STATUS) !=
  1248. TYPHOON_STATUS_WAITING_FOR_SEGMENT) {
  1249. netdev_err(tp->dev, "final segment ready timeout\n");
  1250. goto err_out_irq;
  1251. }
  1252. iowrite32(TYPHOON_BOOTCMD_DNLD_COMPLETE, ioaddr + TYPHOON_REG_COMMAND);
  1253. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
  1254. netdev_err(tp->dev, "boot ready timeout, status 0x%0x\n",
  1255. ioread32(ioaddr + TYPHOON_REG_STATUS));
  1256. goto err_out_irq;
  1257. }
  1258. err = 0;
  1259. err_out_irq:
  1260. iowrite32(irqMasked, ioaddr + TYPHOON_REG_INTR_MASK);
  1261. iowrite32(irqEnabled, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1262. pci_free_consistent(pdev, PAGE_SIZE, dpage, dpage_dma);
  1263. err_out:
  1264. return err;
  1265. }
  1266. static int
  1267. typhoon_boot_3XP(struct typhoon *tp, u32 initial_status)
  1268. {
  1269. void __iomem *ioaddr = tp->ioaddr;
  1270. if(typhoon_wait_status(ioaddr, initial_status) < 0) {
  1271. netdev_err(tp->dev, "boot ready timeout\n");
  1272. goto out_timeout;
  1273. }
  1274. iowrite32(0, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_HI);
  1275. iowrite32(tp->shared_dma, ioaddr + TYPHOON_REG_BOOT_RECORD_ADDR_LO);
  1276. typhoon_post_pci_writes(ioaddr);
  1277. iowrite32(TYPHOON_BOOTCMD_REG_BOOT_RECORD,
  1278. ioaddr + TYPHOON_REG_COMMAND);
  1279. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_RUNNING) < 0) {
  1280. netdev_err(tp->dev, "boot finish timeout (status 0x%x)\n",
  1281. ioread32(ioaddr + TYPHOON_REG_STATUS));
  1282. goto out_timeout;
  1283. }
  1284. /* Clear the Transmit and Command ready registers
  1285. */
  1286. iowrite32(0, ioaddr + TYPHOON_REG_TX_HI_READY);
  1287. iowrite32(0, ioaddr + TYPHOON_REG_CMD_READY);
  1288. iowrite32(0, ioaddr + TYPHOON_REG_TX_LO_READY);
  1289. typhoon_post_pci_writes(ioaddr);
  1290. iowrite32(TYPHOON_BOOTCMD_BOOT, ioaddr + TYPHOON_REG_COMMAND);
  1291. return 0;
  1292. out_timeout:
  1293. return -ETIMEDOUT;
  1294. }
  1295. static u32
  1296. typhoon_clean_tx(struct typhoon *tp, struct transmit_ring *txRing,
  1297. volatile __le32 * index)
  1298. {
  1299. u32 lastRead = txRing->lastRead;
  1300. struct tx_desc *tx;
  1301. dma_addr_t skb_dma;
  1302. int dma_len;
  1303. int type;
  1304. while(lastRead != le32_to_cpu(*index)) {
  1305. tx = (struct tx_desc *) (txRing->ringBase + lastRead);
  1306. type = tx->flags & TYPHOON_TYPE_MASK;
  1307. if(type == TYPHOON_TX_DESC) {
  1308. /* This tx_desc describes a packet.
  1309. */
  1310. unsigned long ptr = tx->tx_addr;
  1311. struct sk_buff *skb = (struct sk_buff *) ptr;
  1312. dev_kfree_skb_irq(skb);
  1313. } else if(type == TYPHOON_FRAG_DESC) {
  1314. /* This tx_desc describes a memory mapping. Free it.
  1315. */
  1316. skb_dma = (dma_addr_t) le32_to_cpu(tx->frag.addr);
  1317. dma_len = le16_to_cpu(tx->len);
  1318. pci_unmap_single(tp->pdev, skb_dma, dma_len,
  1319. PCI_DMA_TODEVICE);
  1320. }
  1321. tx->flags = 0;
  1322. typhoon_inc_tx_index(&lastRead, 1);
  1323. }
  1324. return lastRead;
  1325. }
  1326. static void
  1327. typhoon_tx_complete(struct typhoon *tp, struct transmit_ring *txRing,
  1328. volatile __le32 * index)
  1329. {
  1330. u32 lastRead;
  1331. int numDesc = MAX_SKB_FRAGS + 1;
  1332. /* This will need changing if we start to use the Hi Tx ring. */
  1333. lastRead = typhoon_clean_tx(tp, txRing, index);
  1334. if(netif_queue_stopped(tp->dev) && typhoon_num_free(txRing->lastWrite,
  1335. lastRead, TXLO_ENTRIES) > (numDesc + 2))
  1336. netif_wake_queue(tp->dev);
  1337. txRing->lastRead = lastRead;
  1338. smp_wmb();
  1339. }
  1340. static void
  1341. typhoon_recycle_rx_skb(struct typhoon *tp, u32 idx)
  1342. {
  1343. struct typhoon_indexes *indexes = tp->indexes;
  1344. struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
  1345. struct basic_ring *ring = &tp->rxBuffRing;
  1346. struct rx_free *r;
  1347. if((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
  1348. le32_to_cpu(indexes->rxBuffCleared)) {
  1349. /* no room in ring, just drop the skb
  1350. */
  1351. dev_kfree_skb_any(rxb->skb);
  1352. rxb->skb = NULL;
  1353. return;
  1354. }
  1355. r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
  1356. typhoon_inc_rxfree_index(&ring->lastWrite, 1);
  1357. r->virtAddr = idx;
  1358. r->physAddr = cpu_to_le32(rxb->dma_addr);
  1359. /* Tell the card about it */
  1360. wmb();
  1361. indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
  1362. }
  1363. static int
  1364. typhoon_alloc_rx_skb(struct typhoon *tp, u32 idx)
  1365. {
  1366. struct typhoon_indexes *indexes = tp->indexes;
  1367. struct rxbuff_ent *rxb = &tp->rxbuffers[idx];
  1368. struct basic_ring *ring = &tp->rxBuffRing;
  1369. struct rx_free *r;
  1370. struct sk_buff *skb;
  1371. dma_addr_t dma_addr;
  1372. rxb->skb = NULL;
  1373. if((ring->lastWrite + sizeof(*r)) % (RXFREE_ENTRIES * sizeof(*r)) ==
  1374. le32_to_cpu(indexes->rxBuffCleared))
  1375. return -ENOMEM;
  1376. skb = dev_alloc_skb(PKT_BUF_SZ);
  1377. if(!skb)
  1378. return -ENOMEM;
  1379. #if 0
  1380. /* Please, 3com, fix the firmware to allow DMA to a unaligned
  1381. * address! Pretty please?
  1382. */
  1383. skb_reserve(skb, 2);
  1384. #endif
  1385. skb->dev = tp->dev;
  1386. dma_addr = pci_map_single(tp->pdev, skb->data,
  1387. PKT_BUF_SZ, PCI_DMA_FROMDEVICE);
  1388. /* Since no card does 64 bit DAC, the high bits will never
  1389. * change from zero.
  1390. */
  1391. r = (struct rx_free *) (ring->ringBase + ring->lastWrite);
  1392. typhoon_inc_rxfree_index(&ring->lastWrite, 1);
  1393. r->virtAddr = idx;
  1394. r->physAddr = cpu_to_le32(dma_addr);
  1395. rxb->skb = skb;
  1396. rxb->dma_addr = dma_addr;
  1397. /* Tell the card about it */
  1398. wmb();
  1399. indexes->rxBuffReady = cpu_to_le32(ring->lastWrite);
  1400. return 0;
  1401. }
  1402. static int
  1403. typhoon_rx(struct typhoon *tp, struct basic_ring *rxRing, volatile __le32 * ready,
  1404. volatile __le32 * cleared, int budget)
  1405. {
  1406. struct rx_desc *rx;
  1407. struct sk_buff *skb, *new_skb;
  1408. struct rxbuff_ent *rxb;
  1409. dma_addr_t dma_addr;
  1410. u32 local_ready;
  1411. u32 rxaddr;
  1412. int pkt_len;
  1413. u32 idx;
  1414. __le32 csum_bits;
  1415. int received;
  1416. received = 0;
  1417. local_ready = le32_to_cpu(*ready);
  1418. rxaddr = le32_to_cpu(*cleared);
  1419. while(rxaddr != local_ready && budget > 0) {
  1420. rx = (struct rx_desc *) (rxRing->ringBase + rxaddr);
  1421. idx = rx->addr;
  1422. rxb = &tp->rxbuffers[idx];
  1423. skb = rxb->skb;
  1424. dma_addr = rxb->dma_addr;
  1425. typhoon_inc_rx_index(&rxaddr, 1);
  1426. if(rx->flags & TYPHOON_RX_ERROR) {
  1427. typhoon_recycle_rx_skb(tp, idx);
  1428. continue;
  1429. }
  1430. pkt_len = le16_to_cpu(rx->frameLen);
  1431. if(pkt_len < rx_copybreak &&
  1432. (new_skb = dev_alloc_skb(pkt_len + 2)) != NULL) {
  1433. skb_reserve(new_skb, 2);
  1434. pci_dma_sync_single_for_cpu(tp->pdev, dma_addr,
  1435. PKT_BUF_SZ,
  1436. PCI_DMA_FROMDEVICE);
  1437. skb_copy_to_linear_data(new_skb, skb->data, pkt_len);
  1438. pci_dma_sync_single_for_device(tp->pdev, dma_addr,
  1439. PKT_BUF_SZ,
  1440. PCI_DMA_FROMDEVICE);
  1441. skb_put(new_skb, pkt_len);
  1442. typhoon_recycle_rx_skb(tp, idx);
  1443. } else {
  1444. new_skb = skb;
  1445. skb_put(new_skb, pkt_len);
  1446. pci_unmap_single(tp->pdev, dma_addr, PKT_BUF_SZ,
  1447. PCI_DMA_FROMDEVICE);
  1448. typhoon_alloc_rx_skb(tp, idx);
  1449. }
  1450. new_skb->protocol = eth_type_trans(new_skb, tp->dev);
  1451. csum_bits = rx->rxStatus & (TYPHOON_RX_IP_CHK_GOOD |
  1452. TYPHOON_RX_UDP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD);
  1453. if(csum_bits ==
  1454. (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_TCP_CHK_GOOD) ||
  1455. csum_bits ==
  1456. (TYPHOON_RX_IP_CHK_GOOD | TYPHOON_RX_UDP_CHK_GOOD)) {
  1457. new_skb->ip_summed = CHECKSUM_UNNECESSARY;
  1458. } else
  1459. skb_checksum_none_assert(new_skb);
  1460. if (rx->rxStatus & TYPHOON_RX_VLAN)
  1461. __vlan_hwaccel_put_tag(new_skb,
  1462. ntohl(rx->vlanTag) & 0xffff);
  1463. netif_receive_skb(new_skb);
  1464. received++;
  1465. budget--;
  1466. }
  1467. *cleared = cpu_to_le32(rxaddr);
  1468. return received;
  1469. }
  1470. static void
  1471. typhoon_fill_free_ring(struct typhoon *tp)
  1472. {
  1473. u32 i;
  1474. for(i = 0; i < RXENT_ENTRIES; i++) {
  1475. struct rxbuff_ent *rxb = &tp->rxbuffers[i];
  1476. if(rxb->skb)
  1477. continue;
  1478. if(typhoon_alloc_rx_skb(tp, i) < 0)
  1479. break;
  1480. }
  1481. }
  1482. static int
  1483. typhoon_poll(struct napi_struct *napi, int budget)
  1484. {
  1485. struct typhoon *tp = container_of(napi, struct typhoon, napi);
  1486. struct typhoon_indexes *indexes = tp->indexes;
  1487. int work_done;
  1488. rmb();
  1489. if(!tp->awaiting_resp && indexes->respReady != indexes->respCleared)
  1490. typhoon_process_response(tp, 0, NULL);
  1491. if(le32_to_cpu(indexes->txLoCleared) != tp->txLoRing.lastRead)
  1492. typhoon_tx_complete(tp, &tp->txLoRing, &indexes->txLoCleared);
  1493. work_done = 0;
  1494. if(indexes->rxHiCleared != indexes->rxHiReady) {
  1495. work_done += typhoon_rx(tp, &tp->rxHiRing, &indexes->rxHiReady,
  1496. &indexes->rxHiCleared, budget);
  1497. }
  1498. if(indexes->rxLoCleared != indexes->rxLoReady) {
  1499. work_done += typhoon_rx(tp, &tp->rxLoRing, &indexes->rxLoReady,
  1500. &indexes->rxLoCleared, budget - work_done);
  1501. }
  1502. if(le32_to_cpu(indexes->rxBuffCleared) == tp->rxBuffRing.lastWrite) {
  1503. /* rxBuff ring is empty, try to fill it. */
  1504. typhoon_fill_free_ring(tp);
  1505. }
  1506. if (work_done < budget) {
  1507. napi_complete(napi);
  1508. iowrite32(TYPHOON_INTR_NONE,
  1509. tp->ioaddr + TYPHOON_REG_INTR_MASK);
  1510. typhoon_post_pci_writes(tp->ioaddr);
  1511. }
  1512. return work_done;
  1513. }
  1514. static irqreturn_t
  1515. typhoon_interrupt(int irq, void *dev_instance)
  1516. {
  1517. struct net_device *dev = dev_instance;
  1518. struct typhoon *tp = netdev_priv(dev);
  1519. void __iomem *ioaddr = tp->ioaddr;
  1520. u32 intr_status;
  1521. intr_status = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1522. if(!(intr_status & TYPHOON_INTR_HOST_INT))
  1523. return IRQ_NONE;
  1524. iowrite32(intr_status, ioaddr + TYPHOON_REG_INTR_STATUS);
  1525. if (napi_schedule_prep(&tp->napi)) {
  1526. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  1527. typhoon_post_pci_writes(ioaddr);
  1528. __napi_schedule(&tp->napi);
  1529. } else {
  1530. netdev_err(dev, "Error, poll already scheduled\n");
  1531. }
  1532. return IRQ_HANDLED;
  1533. }
  1534. static void
  1535. typhoon_free_rx_rings(struct typhoon *tp)
  1536. {
  1537. u32 i;
  1538. for(i = 0; i < RXENT_ENTRIES; i++) {
  1539. struct rxbuff_ent *rxb = &tp->rxbuffers[i];
  1540. if(rxb->skb) {
  1541. pci_unmap_single(tp->pdev, rxb->dma_addr, PKT_BUF_SZ,
  1542. PCI_DMA_FROMDEVICE);
  1543. dev_kfree_skb(rxb->skb);
  1544. rxb->skb = NULL;
  1545. }
  1546. }
  1547. }
  1548. static int
  1549. typhoon_sleep(struct typhoon *tp, pci_power_t state, __le16 events)
  1550. {
  1551. struct pci_dev *pdev = tp->pdev;
  1552. void __iomem *ioaddr = tp->ioaddr;
  1553. struct cmd_desc xp_cmd;
  1554. int err;
  1555. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_ENABLE_WAKE_EVENTS);
  1556. xp_cmd.parm1 = events;
  1557. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1558. if(err < 0) {
  1559. netdev_err(tp->dev, "typhoon_sleep(): wake events cmd err %d\n",
  1560. err);
  1561. return err;
  1562. }
  1563. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_GOTO_SLEEP);
  1564. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1565. if(err < 0) {
  1566. netdev_err(tp->dev, "typhoon_sleep(): sleep cmd err %d\n", err);
  1567. return err;
  1568. }
  1569. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_SLEEPING) < 0)
  1570. return -ETIMEDOUT;
  1571. /* Since we cannot monitor the status of the link while sleeping,
  1572. * tell the world it went away.
  1573. */
  1574. netif_carrier_off(tp->dev);
  1575. pci_enable_wake(tp->pdev, state, 1);
  1576. pci_disable_device(pdev);
  1577. return pci_set_power_state(pdev, state);
  1578. }
  1579. static int
  1580. typhoon_wakeup(struct typhoon *tp, int wait_type)
  1581. {
  1582. struct pci_dev *pdev = tp->pdev;
  1583. void __iomem *ioaddr = tp->ioaddr;
  1584. pci_set_power_state(pdev, PCI_D0);
  1585. pci_restore_state(pdev);
  1586. /* Post 2.x.x versions of the Sleep Image require a reset before
  1587. * we can download the Runtime Image. But let's not make users of
  1588. * the old firmware pay for the reset.
  1589. */
  1590. iowrite32(TYPHOON_BOOTCMD_WAKEUP, ioaddr + TYPHOON_REG_COMMAND);
  1591. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_WAITING_FOR_HOST) < 0 ||
  1592. (tp->capabilities & TYPHOON_WAKEUP_NEEDS_RESET))
  1593. return typhoon_reset(ioaddr, wait_type);
  1594. return 0;
  1595. }
  1596. static int
  1597. typhoon_start_runtime(struct typhoon *tp)
  1598. {
  1599. struct net_device *dev = tp->dev;
  1600. void __iomem *ioaddr = tp->ioaddr;
  1601. struct cmd_desc xp_cmd;
  1602. int err;
  1603. typhoon_init_rings(tp);
  1604. typhoon_fill_free_ring(tp);
  1605. err = typhoon_download_firmware(tp);
  1606. if(err < 0) {
  1607. netdev_err(tp->dev, "cannot load runtime on 3XP\n");
  1608. goto error_out;
  1609. }
  1610. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_BOOT) < 0) {
  1611. netdev_err(tp->dev, "cannot boot 3XP\n");
  1612. err = -EIO;
  1613. goto error_out;
  1614. }
  1615. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAX_PKT_SIZE);
  1616. xp_cmd.parm1 = cpu_to_le16(PKT_BUF_SZ);
  1617. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1618. if(err < 0)
  1619. goto error_out;
  1620. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
  1621. xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
  1622. xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
  1623. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1624. if(err < 0)
  1625. goto error_out;
  1626. /* Disable IRQ coalescing -- we can reenable it when 3Com gives
  1627. * us some more information on how to control it.
  1628. */
  1629. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_IRQ_COALESCE_CTRL);
  1630. xp_cmd.parm1 = 0;
  1631. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1632. if(err < 0)
  1633. goto error_out;
  1634. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_XCVR_SELECT);
  1635. xp_cmd.parm1 = tp->xcvr_select;
  1636. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1637. if(err < 0)
  1638. goto error_out;
  1639. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_VLAN_TYPE_WRITE);
  1640. xp_cmd.parm1 = cpu_to_le16(ETH_P_8021Q);
  1641. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1642. if(err < 0)
  1643. goto error_out;
  1644. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_OFFLOAD_TASKS);
  1645. xp_cmd.parm2 = tp->offload;
  1646. xp_cmd.parm3 = tp->offload;
  1647. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1648. if(err < 0)
  1649. goto error_out;
  1650. typhoon_set_rx_mode(dev);
  1651. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_ENABLE);
  1652. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1653. if(err < 0)
  1654. goto error_out;
  1655. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_ENABLE);
  1656. err = typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1657. if(err < 0)
  1658. goto error_out;
  1659. tp->card_state = Running;
  1660. smp_wmb();
  1661. iowrite32(TYPHOON_INTR_ENABLE_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1662. iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_MASK);
  1663. typhoon_post_pci_writes(ioaddr);
  1664. return 0;
  1665. error_out:
  1666. typhoon_reset(ioaddr, WaitNoSleep);
  1667. typhoon_free_rx_rings(tp);
  1668. typhoon_init_rings(tp);
  1669. return err;
  1670. }
  1671. static int
  1672. typhoon_stop_runtime(struct typhoon *tp, int wait_type)
  1673. {
  1674. struct typhoon_indexes *indexes = tp->indexes;
  1675. struct transmit_ring *txLo = &tp->txLoRing;
  1676. void __iomem *ioaddr = tp->ioaddr;
  1677. struct cmd_desc xp_cmd;
  1678. int i;
  1679. /* Disable interrupts early, since we can't schedule a poll
  1680. * when called with !netif_running(). This will be posted
  1681. * when we force the posting of the command.
  1682. */
  1683. iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1684. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_RX_DISABLE);
  1685. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1686. /* Wait 1/2 sec for any outstanding transmits to occur
  1687. * We'll cleanup after the reset if this times out.
  1688. */
  1689. for(i = 0; i < TYPHOON_WAIT_TIMEOUT; i++) {
  1690. if(indexes->txLoCleared == cpu_to_le32(txLo->lastWrite))
  1691. break;
  1692. udelay(TYPHOON_UDELAY);
  1693. }
  1694. if(i == TYPHOON_WAIT_TIMEOUT)
  1695. netdev_err(tp->dev, "halt timed out waiting for Tx to complete\n");
  1696. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_TX_DISABLE);
  1697. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1698. /* save the statistics so when we bring the interface up again,
  1699. * the values reported to userspace are correct.
  1700. */
  1701. tp->card_state = Sleeping;
  1702. smp_wmb();
  1703. typhoon_do_get_stats(tp);
  1704. memcpy(&tp->stats_saved, &tp->stats, sizeof(struct net_device_stats));
  1705. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_HALT);
  1706. typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL);
  1707. if(typhoon_wait_status(ioaddr, TYPHOON_STATUS_HALTED) < 0)
  1708. netdev_err(tp->dev, "timed out waiting for 3XP to halt\n");
  1709. if(typhoon_reset(ioaddr, wait_type) < 0) {
  1710. netdev_err(tp->dev, "unable to reset 3XP\n");
  1711. return -ETIMEDOUT;
  1712. }
  1713. /* cleanup any outstanding Tx packets */
  1714. if(indexes->txLoCleared != cpu_to_le32(txLo->lastWrite)) {
  1715. indexes->txLoCleared = cpu_to_le32(txLo->lastWrite);
  1716. typhoon_clean_tx(tp, &tp->txLoRing, &indexes->txLoCleared);
  1717. }
  1718. return 0;
  1719. }
  1720. static void
  1721. typhoon_tx_timeout(struct net_device *dev)
  1722. {
  1723. struct typhoon *tp = netdev_priv(dev);
  1724. if(typhoon_reset(tp->ioaddr, WaitNoSleep) < 0) {
  1725. netdev_warn(dev, "could not reset in tx timeout\n");
  1726. goto truly_dead;
  1727. }
  1728. /* If we ever start using the Hi ring, it will need cleaning too */
  1729. typhoon_clean_tx(tp, &tp->txLoRing, &tp->indexes->txLoCleared);
  1730. typhoon_free_rx_rings(tp);
  1731. if(typhoon_start_runtime(tp) < 0) {
  1732. netdev_err(dev, "could not start runtime in tx timeout\n");
  1733. goto truly_dead;
  1734. }
  1735. netif_wake_queue(dev);
  1736. return;
  1737. truly_dead:
  1738. /* Reset the hardware, and turn off carrier to avoid more timeouts */
  1739. typhoon_reset(tp->ioaddr, NoWait);
  1740. netif_carrier_off(dev);
  1741. }
  1742. static int
  1743. typhoon_open(struct net_device *dev)
  1744. {
  1745. struct typhoon *tp = netdev_priv(dev);
  1746. int err;
  1747. err = typhoon_request_firmware(tp);
  1748. if (err)
  1749. goto out;
  1750. err = typhoon_wakeup(tp, WaitSleep);
  1751. if(err < 0) {
  1752. netdev_err(dev, "unable to wakeup device\n");
  1753. goto out_sleep;
  1754. }
  1755. err = request_irq(dev->irq, typhoon_interrupt, IRQF_SHARED,
  1756. dev->name, dev);
  1757. if(err < 0)
  1758. goto out_sleep;
  1759. napi_enable(&tp->napi);
  1760. err = typhoon_start_runtime(tp);
  1761. if(err < 0) {
  1762. napi_disable(&tp->napi);
  1763. goto out_irq;
  1764. }
  1765. netif_start_queue(dev);
  1766. return 0;
  1767. out_irq:
  1768. free_irq(dev->irq, dev);
  1769. out_sleep:
  1770. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
  1771. netdev_err(dev, "unable to reboot into sleep img\n");
  1772. typhoon_reset(tp->ioaddr, NoWait);
  1773. goto out;
  1774. }
  1775. if(typhoon_sleep(tp, PCI_D3hot, 0) < 0)
  1776. netdev_err(dev, "unable to go back to sleep\n");
  1777. out:
  1778. return err;
  1779. }
  1780. static int
  1781. typhoon_close(struct net_device *dev)
  1782. {
  1783. struct typhoon *tp = netdev_priv(dev);
  1784. netif_stop_queue(dev);
  1785. napi_disable(&tp->napi);
  1786. if(typhoon_stop_runtime(tp, WaitSleep) < 0)
  1787. netdev_err(dev, "unable to stop runtime\n");
  1788. /* Make sure there is no irq handler running on a different CPU. */
  1789. free_irq(dev->irq, dev);
  1790. typhoon_free_rx_rings(tp);
  1791. typhoon_init_rings(tp);
  1792. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0)
  1793. netdev_err(dev, "unable to boot sleep image\n");
  1794. if(typhoon_sleep(tp, PCI_D3hot, 0) < 0)
  1795. netdev_err(dev, "unable to put card to sleep\n");
  1796. return 0;
  1797. }
  1798. #ifdef CONFIG_PM
  1799. static int
  1800. typhoon_resume(struct pci_dev *pdev)
  1801. {
  1802. struct net_device *dev = pci_get_drvdata(pdev);
  1803. struct typhoon *tp = netdev_priv(dev);
  1804. /* If we're down, resume when we are upped.
  1805. */
  1806. if(!netif_running(dev))
  1807. return 0;
  1808. if(typhoon_wakeup(tp, WaitNoSleep) < 0) {
  1809. netdev_err(dev, "critical: could not wake up in resume\n");
  1810. goto reset;
  1811. }
  1812. if(typhoon_start_runtime(tp) < 0) {
  1813. netdev_err(dev, "critical: could not start runtime in resume\n");
  1814. goto reset;
  1815. }
  1816. netif_device_attach(dev);
  1817. return 0;
  1818. reset:
  1819. typhoon_reset(tp->ioaddr, NoWait);
  1820. return -EBUSY;
  1821. }
  1822. static int
  1823. typhoon_suspend(struct pci_dev *pdev, pm_message_t state)
  1824. {
  1825. struct net_device *dev = pci_get_drvdata(pdev);
  1826. struct typhoon *tp = netdev_priv(dev);
  1827. struct cmd_desc xp_cmd;
  1828. /* If we're down, we're already suspended.
  1829. */
  1830. if(!netif_running(dev))
  1831. return 0;
  1832. /* TYPHOON_OFFLOAD_VLAN is always on now, so this doesn't work */
  1833. if(tp->wol_events & TYPHOON_WAKE_MAGIC_PKT)
  1834. netdev_warn(dev, "cannot do WAKE_MAGIC with VLAN offloading\n");
  1835. netif_device_detach(dev);
  1836. if(typhoon_stop_runtime(tp, WaitNoSleep) < 0) {
  1837. netdev_err(dev, "unable to stop runtime\n");
  1838. goto need_resume;
  1839. }
  1840. typhoon_free_rx_rings(tp);
  1841. typhoon_init_rings(tp);
  1842. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
  1843. netdev_err(dev, "unable to boot sleep image\n");
  1844. goto need_resume;
  1845. }
  1846. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_MAC_ADDRESS);
  1847. xp_cmd.parm1 = cpu_to_le16(ntohs(*(__be16 *)&dev->dev_addr[0]));
  1848. xp_cmd.parm2 = cpu_to_le32(ntohl(*(__be32 *)&dev->dev_addr[2]));
  1849. if(typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
  1850. netdev_err(dev, "unable to set mac address in suspend\n");
  1851. goto need_resume;
  1852. }
  1853. INIT_COMMAND_NO_RESPONSE(&xp_cmd, TYPHOON_CMD_SET_RX_FILTER);
  1854. xp_cmd.parm1 = TYPHOON_RX_FILTER_DIRECTED | TYPHOON_RX_FILTER_BROADCAST;
  1855. if(typhoon_issue_command(tp, 1, &xp_cmd, 0, NULL) < 0) {
  1856. netdev_err(dev, "unable to set rx filter in suspend\n");
  1857. goto need_resume;
  1858. }
  1859. if(typhoon_sleep(tp, pci_choose_state(pdev, state), tp->wol_events) < 0) {
  1860. netdev_err(dev, "unable to put card to sleep\n");
  1861. goto need_resume;
  1862. }
  1863. return 0;
  1864. need_resume:
  1865. typhoon_resume(pdev);
  1866. return -EBUSY;
  1867. }
  1868. #endif
  1869. static int __devinit
  1870. typhoon_test_mmio(struct pci_dev *pdev)
  1871. {
  1872. void __iomem *ioaddr = pci_iomap(pdev, 1, 128);
  1873. int mode = 0;
  1874. u32 val;
  1875. if(!ioaddr)
  1876. goto out;
  1877. if(ioread32(ioaddr + TYPHOON_REG_STATUS) !=
  1878. TYPHOON_STATUS_WAITING_FOR_HOST)
  1879. goto out_unmap;
  1880. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  1881. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
  1882. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1883. /* Ok, see if we can change our interrupt status register by
  1884. * sending ourselves an interrupt. If so, then MMIO works.
  1885. * The 50usec delay is arbitrary -- it could probably be smaller.
  1886. */
  1887. val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1888. if((val & TYPHOON_INTR_SELF) == 0) {
  1889. iowrite32(1, ioaddr + TYPHOON_REG_SELF_INTERRUPT);
  1890. ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1891. udelay(50);
  1892. val = ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1893. if(val & TYPHOON_INTR_SELF)
  1894. mode = 1;
  1895. }
  1896. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_MASK);
  1897. iowrite32(TYPHOON_INTR_ALL, ioaddr + TYPHOON_REG_INTR_STATUS);
  1898. iowrite32(TYPHOON_INTR_NONE, ioaddr + TYPHOON_REG_INTR_ENABLE);
  1899. ioread32(ioaddr + TYPHOON_REG_INTR_STATUS);
  1900. out_unmap:
  1901. pci_iounmap(pdev, ioaddr);
  1902. out:
  1903. if(!mode)
  1904. pr_info("%s: falling back to port IO\n", pci_name(pdev));
  1905. return mode;
  1906. }
  1907. static const struct net_device_ops typhoon_netdev_ops = {
  1908. .ndo_open = typhoon_open,
  1909. .ndo_stop = typhoon_close,
  1910. .ndo_start_xmit = typhoon_start_tx,
  1911. .ndo_set_multicast_list = typhoon_set_rx_mode,
  1912. .ndo_tx_timeout = typhoon_tx_timeout,
  1913. .ndo_get_stats = typhoon_get_stats,
  1914. .ndo_validate_addr = eth_validate_addr,
  1915. .ndo_set_mac_address = typhoon_set_mac_address,
  1916. .ndo_change_mtu = eth_change_mtu,
  1917. };
  1918. static int __devinit
  1919. typhoon_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1920. {
  1921. struct net_device *dev;
  1922. struct typhoon *tp;
  1923. int card_id = (int) ent->driver_data;
  1924. void __iomem *ioaddr;
  1925. void *shared;
  1926. dma_addr_t shared_dma;
  1927. struct cmd_desc xp_cmd;
  1928. struct resp_desc xp_resp[3];
  1929. int err = 0;
  1930. const char *err_msg;
  1931. dev = alloc_etherdev(sizeof(*tp));
  1932. if(dev == NULL) {
  1933. err_msg = "unable to alloc new net device";
  1934. err = -ENOMEM;
  1935. goto error_out;
  1936. }
  1937. SET_NETDEV_DEV(dev, &pdev->dev);
  1938. err = pci_enable_device(pdev);
  1939. if(err < 0) {
  1940. err_msg = "unable to enable device";
  1941. goto error_out_dev;
  1942. }
  1943. err = pci_set_mwi(pdev);
  1944. if(err < 0) {
  1945. err_msg = "unable to set MWI";
  1946. goto error_out_disable;
  1947. }
  1948. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1949. if(err < 0) {
  1950. err_msg = "No usable DMA configuration";
  1951. goto error_out_mwi;
  1952. }
  1953. /* sanity checks on IO and MMIO BARs
  1954. */
  1955. if(!(pci_resource_flags(pdev, 0) & IORESOURCE_IO)) {
  1956. err_msg = "region #1 not a PCI IO resource, aborting";
  1957. err = -ENODEV;
  1958. goto error_out_mwi;
  1959. }
  1960. if(pci_resource_len(pdev, 0) < 128) {
  1961. err_msg = "Invalid PCI IO region size, aborting";
  1962. err = -ENODEV;
  1963. goto error_out_mwi;
  1964. }
  1965. if(!(pci_resource_flags(pdev, 1) & IORESOURCE_MEM)) {
  1966. err_msg = "region #1 not a PCI MMIO resource, aborting";
  1967. err = -ENODEV;
  1968. goto error_out_mwi;
  1969. }
  1970. if(pci_resource_len(pdev, 1) < 128) {
  1971. err_msg = "Invalid PCI MMIO region size, aborting";
  1972. err = -ENODEV;
  1973. goto error_out_mwi;
  1974. }
  1975. err = pci_request_regions(pdev, KBUILD_MODNAME);
  1976. if(err < 0) {
  1977. err_msg = "could not request regions";
  1978. goto error_out_mwi;
  1979. }
  1980. /* map our registers
  1981. */
  1982. if(use_mmio != 0 && use_mmio != 1)
  1983. use_mmio = typhoon_test_mmio(pdev);
  1984. ioaddr = pci_iomap(pdev, use_mmio, 128);
  1985. if (!ioaddr) {
  1986. err_msg = "cannot remap registers, aborting";
  1987. err = -EIO;
  1988. goto error_out_regions;
  1989. }
  1990. /* allocate pci dma space for rx and tx descriptor rings
  1991. */
  1992. shared = pci_alloc_consistent(pdev, sizeof(struct typhoon_shared),
  1993. &shared_dma);
  1994. if(!shared) {
  1995. err_msg = "could not allocate DMA memory";
  1996. err = -ENOMEM;
  1997. goto error_out_remap;
  1998. }
  1999. dev->irq = pdev->irq;
  2000. tp = netdev_priv(dev);
  2001. tp->shared = (struct typhoon_shared *) shared;
  2002. tp->shared_dma = shared_dma;
  2003. tp->pdev = pdev;
  2004. tp->tx_pdev = pdev;
  2005. tp->ioaddr = ioaddr;
  2006. tp->tx_ioaddr = ioaddr;
  2007. tp->dev = dev;
  2008. /* Init sequence:
  2009. * 1) Reset the adapter to clear any bad juju
  2010. * 2) Reload the sleep image
  2011. * 3) Boot the sleep image
  2012. * 4) Get the hardware address.
  2013. * 5) Put the card to sleep.
  2014. */
  2015. if (typhoon_reset(ioaddr, WaitSleep) < 0) {
  2016. err_msg = "could not reset 3XP";
  2017. err = -EIO;
  2018. goto error_out_dma;
  2019. }
  2020. /* Now that we've reset the 3XP and are sure it's not going to
  2021. * write all over memory, enable bus mastering, and save our
  2022. * state for resuming after a suspend.
  2023. */
  2024. pci_set_master(pdev);
  2025. pci_save_state(pdev);
  2026. typhoon_init_interface(tp);
  2027. typhoon_init_rings(tp);
  2028. if(typhoon_boot_3XP(tp, TYPHOON_STATUS_WAITING_FOR_HOST) < 0) {
  2029. err_msg = "cannot boot 3XP sleep image";
  2030. err = -EIO;
  2031. goto error_out_reset;
  2032. }
  2033. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_MAC_ADDRESS);
  2034. if(typhoon_issue_command(tp, 1, &xp_cmd, 1, xp_resp) < 0) {
  2035. err_msg = "cannot read MAC address";
  2036. err = -EIO;
  2037. goto error_out_reset;
  2038. }
  2039. *(__be16 *)&dev->dev_addr[0] = htons(le16_to_cpu(xp_resp[0].parm1));
  2040. *(__be32 *)&dev->dev_addr[2] = htonl(le32_to_cpu(xp_resp[0].parm2));
  2041. if(!is_valid_ether_addr(dev->dev_addr)) {
  2042. err_msg = "Could not obtain valid ethernet address, aborting";
  2043. goto error_out_reset;
  2044. }
  2045. /* Read the Sleep Image version last, so the response is valid
  2046. * later when we print out the version reported.
  2047. */
  2048. INIT_COMMAND_WITH_RESPONSE(&xp_cmd, TYPHOON_CMD_READ_VERSIONS);
  2049. if(typhoon_issue_command(tp, 1, &xp_cmd, 3, xp_resp) < 0) {
  2050. err_msg = "Could not get Sleep Image version";
  2051. goto error_out_reset;
  2052. }
  2053. tp->capabilities = typhoon_card_info[card_id].capabilities;
  2054. tp->xcvr_select = TYPHOON_XCVR_AUTONEG;
  2055. /* Typhoon 1.0 Sleep Images return one response descriptor to the
  2056. * READ_VERSIONS command. Those versions are OK after waking up
  2057. * from sleep without needing a reset. Typhoon 1.1+ Sleep Images
  2058. * seem to need a little extra help to get started. Since we don't
  2059. * know how to nudge it along, just kick it.
  2060. */
  2061. if(xp_resp[0].numDesc != 0)
  2062. tp->capabilities |= TYPHOON_WAKEUP_NEEDS_RESET;
  2063. if(typhoon_sleep(tp, PCI_D3hot, 0) < 0) {
  2064. err_msg = "cannot put adapter to sleep";
  2065. err = -EIO;
  2066. goto error_out_reset;
  2067. }
  2068. /* The chip-specific entries in the device structure. */
  2069. dev->netdev_ops = &typhoon_netdev_ops;
  2070. netif_napi_add(dev, &tp->napi, typhoon_poll, 16);
  2071. dev->watchdog_timeo = TX_TIMEOUT;
  2072. SET_ETHTOOL_OPS(dev, &typhoon_ethtool_ops);
  2073. /* We can handle scatter gather, up to 16 entries, and
  2074. * we can do IP checksumming (only version 4, doh...)
  2075. *
  2076. * There's no way to turn off the RX VLAN offloading and stripping
  2077. * on the current 3XP firmware -- it does not respect the offload
  2078. * settings -- so we only allow the user to toggle the TX processing.
  2079. */
  2080. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO |
  2081. NETIF_F_HW_VLAN_TX;
  2082. dev->features = dev->hw_features |
  2083. NETIF_F_HW_VLAN_RX | NETIF_F_RXCSUM;
  2084. if(register_netdev(dev) < 0) {
  2085. err_msg = "unable to register netdev";
  2086. goto error_out_reset;
  2087. }
  2088. pci_set_drvdata(pdev, dev);
  2089. netdev_info(dev, "%s at %s 0x%llx, %pM\n",
  2090. typhoon_card_info[card_id].name,
  2091. use_mmio ? "MMIO" : "IO",
  2092. (unsigned long long)pci_resource_start(pdev, use_mmio),
  2093. dev->dev_addr);
  2094. /* xp_resp still contains the response to the READ_VERSIONS command.
  2095. * For debugging, let the user know what version he has.
  2096. */
  2097. if(xp_resp[0].numDesc == 0) {
  2098. /* This is the Typhoon 1.0 type Sleep Image, last 16 bits
  2099. * of version is Month/Day of build.
  2100. */
  2101. u16 monthday = le32_to_cpu(xp_resp[0].parm2) & 0xffff;
  2102. netdev_info(dev, "Typhoon 1.0 Sleep Image built %02u/%02u/2000\n",
  2103. monthday >> 8, monthday & 0xff);
  2104. } else if(xp_resp[0].numDesc == 2) {
  2105. /* This is the Typhoon 1.1+ type Sleep Image
  2106. */
  2107. u32 sleep_ver = le32_to_cpu(xp_resp[0].parm2);
  2108. u8 *ver_string = (u8 *) &xp_resp[1];
  2109. ver_string[25] = 0;
  2110. netdev_info(dev, "Typhoon 1.1+ Sleep Image version %02x.%03x.%03x %s\n",
  2111. sleep_ver >> 24, (sleep_ver >> 12) & 0xfff,
  2112. sleep_ver & 0xfff, ver_string);
  2113. } else {
  2114. netdev_warn(dev, "Unknown Sleep Image version (%u:%04x)\n",
  2115. xp_resp[0].numDesc, le32_to_cpu(xp_resp[0].parm2));
  2116. }
  2117. return 0;
  2118. error_out_reset:
  2119. typhoon_reset(ioaddr, NoWait);
  2120. error_out_dma:
  2121. pci_free_consistent(pdev, sizeof(struct typhoon_shared),
  2122. shared, shared_dma);
  2123. error_out_remap:
  2124. pci_iounmap(pdev, ioaddr);
  2125. error_out_regions:
  2126. pci_release_regions(pdev);
  2127. error_out_mwi:
  2128. pci_clear_mwi(pdev);
  2129. error_out_disable:
  2130. pci_disable_device(pdev);
  2131. error_out_dev:
  2132. free_netdev(dev);
  2133. error_out:
  2134. pr_err("%s: %s\n", pci_name(pdev), err_msg);
  2135. return err;
  2136. }
  2137. static void __devexit
  2138. typhoon_remove_one(struct pci_dev *pdev)
  2139. {
  2140. struct net_device *dev = pci_get_drvdata(pdev);
  2141. struct typhoon *tp = netdev_priv(dev);
  2142. unregister_netdev(dev);
  2143. pci_set_power_state(pdev, PCI_D0);
  2144. pci_restore_state(pdev);
  2145. typhoon_reset(tp->ioaddr, NoWait);
  2146. pci_iounmap(pdev, tp->ioaddr);
  2147. pci_free_consistent(pdev, sizeof(struct typhoon_shared),
  2148. tp->shared, tp->shared_dma);
  2149. pci_release_regions(pdev);
  2150. pci_clear_mwi(pdev);
  2151. pci_disable_device(pdev);
  2152. pci_set_drvdata(pdev, NULL);
  2153. free_netdev(dev);
  2154. }
  2155. static struct pci_driver typhoon_driver = {
  2156. .name = KBUILD_MODNAME,
  2157. .id_table = typhoon_pci_tbl,
  2158. .probe = typhoon_init_one,
  2159. .remove = __devexit_p(typhoon_remove_one),
  2160. #ifdef CONFIG_PM
  2161. .suspend = typhoon_suspend,
  2162. .resume = typhoon_resume,
  2163. #endif
  2164. };
  2165. static int __init
  2166. typhoon_init(void)
  2167. {
  2168. return pci_register_driver(&typhoon_driver);
  2169. }
  2170. static void __exit
  2171. typhoon_cleanup(void)
  2172. {
  2173. if (typhoon_fw)
  2174. release_firmware(typhoon_fw);
  2175. pci_unregister_driver(&typhoon_driver);
  2176. }
  2177. module_init(typhoon_init);
  2178. module_exit(typhoon_cleanup);