typhoon.c 71 KB

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