typhoon.c 71 KB

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