e100.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2006 Intel Corporation. */
  3. /*
  4. * e100.c: Intel(R) PRO/100 ethernet driver
  5. *
  6. * (Re)written 2003 by [email protected]. Based loosely on
  7. * original e100 driver, but better described as a munging of
  8. * e100, e1000, eepro100, tg3, 8139cp, and other drivers.
  9. *
  10. * References:
  11. * Intel 8255x 10/100 Mbps Ethernet Controller Family,
  12. * Open Source Software Developers Manual,
  13. * http://sourceforge.net/projects/e1000
  14. *
  15. *
  16. * Theory of Operation
  17. *
  18. * I. General
  19. *
  20. * The driver supports Intel(R) 10/100 Mbps PCI Fast Ethernet
  21. * controller family, which includes the 82557, 82558, 82559, 82550,
  22. * 82551, and 82562 devices. 82558 and greater controllers
  23. * integrate the Intel 82555 PHY. The controllers are used in
  24. * server and client network interface cards, as well as in
  25. * LAN-On-Motherboard (LOM), CardBus, MiniPCI, and ICHx
  26. * configurations. 8255x supports a 32-bit linear addressing
  27. * mode and operates at 33Mhz PCI clock rate.
  28. *
  29. * II. Driver Operation
  30. *
  31. * Memory-mapped mode is used exclusively to access the device's
  32. * shared-memory structure, the Control/Status Registers (CSR). All
  33. * setup, configuration, and control of the device, including queuing
  34. * of Tx, Rx, and configuration commands is through the CSR.
  35. * cmd_lock serializes accesses to the CSR command register. cb_lock
  36. * protects the shared Command Block List (CBL).
  37. *
  38. * 8255x is highly MII-compliant and all access to the PHY go
  39. * through the Management Data Interface (MDI). Consequently, the
  40. * driver leverages the mii.c library shared with other MII-compliant
  41. * devices.
  42. *
  43. * Big- and Little-Endian byte order as well as 32- and 64-bit
  44. * archs are supported. Weak-ordered memory and non-cache-coherent
  45. * archs are supported.
  46. *
  47. * III. Transmit
  48. *
  49. * A Tx skb is mapped and hangs off of a TCB. TCBs are linked
  50. * together in a fixed-size ring (CBL) thus forming the flexible mode
  51. * memory structure. A TCB marked with the suspend-bit indicates
  52. * the end of the ring. The last TCB processed suspends the
  53. * controller, and the controller can be restarted by issue a CU
  54. * resume command to continue from the suspend point, or a CU start
  55. * command to start at a given position in the ring.
  56. *
  57. * Non-Tx commands (config, multicast setup, etc) are linked
  58. * into the CBL ring along with Tx commands. The common structure
  59. * used for both Tx and non-Tx commands is the Command Block (CB).
  60. *
  61. * cb_to_use is the next CB to use for queuing a command; cb_to_clean
  62. * is the next CB to check for completion; cb_to_send is the first
  63. * CB to start on in case of a previous failure to resume. CB clean
  64. * up happens in interrupt context in response to a CU interrupt.
  65. * cbs_avail keeps track of number of free CB resources available.
  66. *
  67. * Hardware padding of short packets to minimum packet size is
  68. * enabled. 82557 pads with 7Eh, while the later controllers pad
  69. * with 00h.
  70. *
  71. * IV. Receive
  72. *
  73. * The Receive Frame Area (RFA) comprises a ring of Receive Frame
  74. * Descriptors (RFD) + data buffer, thus forming the simplified mode
  75. * memory structure. Rx skbs are allocated to contain both the RFD
  76. * and the data buffer, but the RFD is pulled off before the skb is
  77. * indicated. The data buffer is aligned such that encapsulated
  78. * protocol headers are u32-aligned. Since the RFD is part of the
  79. * mapped shared memory, and completion status is contained within
  80. * the RFD, the RFD must be dma_sync'ed to maintain a consistent
  81. * view from software and hardware.
  82. *
  83. * In order to keep updates to the RFD link field from colliding with
  84. * hardware writes to mark packets complete, we use the feature that
  85. * hardware will not write to a size 0 descriptor and mark the previous
  86. * packet as end-of-list (EL). After updating the link, we remove EL
  87. * and only then restore the size such that hardware may use the
  88. * previous-to-end RFD.
  89. *
  90. * Under typical operation, the receive unit (RU) is start once,
  91. * and the controller happily fills RFDs as frames arrive. If
  92. * replacement RFDs cannot be allocated, or the RU goes non-active,
  93. * the RU must be restarted. Frame arrival generates an interrupt,
  94. * and Rx indication and re-allocation happen in the same context,
  95. * therefore no locking is required. A software-generated interrupt
  96. * is generated from the watchdog to recover from a failed allocation
  97. * scenario where all Rx resources have been indicated and none re-
  98. * placed.
  99. *
  100. * V. Miscellaneous
  101. *
  102. * VLAN offloading of tagging, stripping and filtering is not
  103. * supported, but driver will accommodate the extra 4-byte VLAN tag
  104. * for processing by upper layers. Tx/Rx Checksum offloading is not
  105. * supported. Tx Scatter/Gather is not supported. Jumbo Frames is
  106. * not supported (hardware limitation).
  107. *
  108. * MagicPacket(tm) WoL support is enabled/disabled via ethtool.
  109. *
  110. * Thanks to JC ([email protected]) for helping with
  111. * testing/troubleshooting the development driver.
  112. *
  113. * TODO:
  114. * o several entry points race with dev->close
  115. * o check for tx-no-resources/stop Q races with tx clean/wake Q
  116. *
  117. * FIXES:
  118. * 2005/12/02 - Michael O'Donnell <Michael.ODonnell at stratus dot com>
  119. * - Stratus87247: protect MDI control register manipulations
  120. * 2009/06/01 - Andreas Mohr <andi at lisas dot de>
  121. * - add clean lowlevel I/O emulation for cards with MII-lacking PHYs
  122. */
  123. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  124. #include <linux/hardirq.h>
  125. #include <linux/interrupt.h>
  126. #include <linux/module.h>
  127. #include <linux/moduleparam.h>
  128. #include <linux/kernel.h>
  129. #include <linux/types.h>
  130. #include <linux/sched.h>
  131. #include <linux/slab.h>
  132. #include <linux/delay.h>
  133. #include <linux/init.h>
  134. #include <linux/pci.h>
  135. #include <linux/dma-mapping.h>
  136. #include <linux/dmapool.h>
  137. #include <linux/netdevice.h>
  138. #include <linux/etherdevice.h>
  139. #include <linux/mii.h>
  140. #include <linux/if_vlan.h>
  141. #include <linux/skbuff.h>
  142. #include <linux/ethtool.h>
  143. #include <linux/string.h>
  144. #include <linux/firmware.h>
  145. #include <linux/rtnetlink.h>
  146. #include <asm/unaligned.h>
  147. #define DRV_NAME "e100"
  148. #define DRV_DESCRIPTION "Intel(R) PRO/100 Network Driver"
  149. #define DRV_COPYRIGHT "Copyright(c) 1999-2006 Intel Corporation"
  150. #define E100_WATCHDOG_PERIOD (2 * HZ)
  151. #define E100_NAPI_WEIGHT 16
  152. #define FIRMWARE_D101M "e100/d101m_ucode.bin"
  153. #define FIRMWARE_D101S "e100/d101s_ucode.bin"
  154. #define FIRMWARE_D102E "e100/d102e_ucode.bin"
  155. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  156. MODULE_AUTHOR(DRV_COPYRIGHT);
  157. MODULE_LICENSE("GPL v2");
  158. MODULE_FIRMWARE(FIRMWARE_D101M);
  159. MODULE_FIRMWARE(FIRMWARE_D101S);
  160. MODULE_FIRMWARE(FIRMWARE_D102E);
  161. static int debug = 3;
  162. static int eeprom_bad_csum_allow = 0;
  163. static int use_io = 0;
  164. module_param(debug, int, 0);
  165. module_param(eeprom_bad_csum_allow, int, 0);
  166. module_param(use_io, int, 0);
  167. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  168. MODULE_PARM_DESC(eeprom_bad_csum_allow, "Allow bad eeprom checksums");
  169. MODULE_PARM_DESC(use_io, "Force use of i/o access mode");
  170. #define INTEL_8255X_ETHERNET_DEVICE(device_id, ich) {\
  171. PCI_VENDOR_ID_INTEL, device_id, PCI_ANY_ID, PCI_ANY_ID, \
  172. PCI_CLASS_NETWORK_ETHERNET << 8, 0xFFFF00, ich }
  173. static const struct pci_device_id e100_id_table[] = {
  174. INTEL_8255X_ETHERNET_DEVICE(0x1029, 0),
  175. INTEL_8255X_ETHERNET_DEVICE(0x1030, 0),
  176. INTEL_8255X_ETHERNET_DEVICE(0x1031, 3),
  177. INTEL_8255X_ETHERNET_DEVICE(0x1032, 3),
  178. INTEL_8255X_ETHERNET_DEVICE(0x1033, 3),
  179. INTEL_8255X_ETHERNET_DEVICE(0x1034, 3),
  180. INTEL_8255X_ETHERNET_DEVICE(0x1038, 3),
  181. INTEL_8255X_ETHERNET_DEVICE(0x1039, 4),
  182. INTEL_8255X_ETHERNET_DEVICE(0x103A, 4),
  183. INTEL_8255X_ETHERNET_DEVICE(0x103B, 4),
  184. INTEL_8255X_ETHERNET_DEVICE(0x103C, 4),
  185. INTEL_8255X_ETHERNET_DEVICE(0x103D, 4),
  186. INTEL_8255X_ETHERNET_DEVICE(0x103E, 4),
  187. INTEL_8255X_ETHERNET_DEVICE(0x1050, 5),
  188. INTEL_8255X_ETHERNET_DEVICE(0x1051, 5),
  189. INTEL_8255X_ETHERNET_DEVICE(0x1052, 5),
  190. INTEL_8255X_ETHERNET_DEVICE(0x1053, 5),
  191. INTEL_8255X_ETHERNET_DEVICE(0x1054, 5),
  192. INTEL_8255X_ETHERNET_DEVICE(0x1055, 5),
  193. INTEL_8255X_ETHERNET_DEVICE(0x1056, 5),
  194. INTEL_8255X_ETHERNET_DEVICE(0x1057, 5),
  195. INTEL_8255X_ETHERNET_DEVICE(0x1059, 0),
  196. INTEL_8255X_ETHERNET_DEVICE(0x1064, 6),
  197. INTEL_8255X_ETHERNET_DEVICE(0x1065, 6),
  198. INTEL_8255X_ETHERNET_DEVICE(0x1066, 6),
  199. INTEL_8255X_ETHERNET_DEVICE(0x1067, 6),
  200. INTEL_8255X_ETHERNET_DEVICE(0x1068, 6),
  201. INTEL_8255X_ETHERNET_DEVICE(0x1069, 6),
  202. INTEL_8255X_ETHERNET_DEVICE(0x106A, 6),
  203. INTEL_8255X_ETHERNET_DEVICE(0x106B, 6),
  204. INTEL_8255X_ETHERNET_DEVICE(0x1091, 7),
  205. INTEL_8255X_ETHERNET_DEVICE(0x1092, 7),
  206. INTEL_8255X_ETHERNET_DEVICE(0x1093, 7),
  207. INTEL_8255X_ETHERNET_DEVICE(0x1094, 7),
  208. INTEL_8255X_ETHERNET_DEVICE(0x1095, 7),
  209. INTEL_8255X_ETHERNET_DEVICE(0x10fe, 7),
  210. INTEL_8255X_ETHERNET_DEVICE(0x1209, 0),
  211. INTEL_8255X_ETHERNET_DEVICE(0x1229, 0),
  212. INTEL_8255X_ETHERNET_DEVICE(0x2449, 2),
  213. INTEL_8255X_ETHERNET_DEVICE(0x2459, 2),
  214. INTEL_8255X_ETHERNET_DEVICE(0x245D, 2),
  215. INTEL_8255X_ETHERNET_DEVICE(0x27DC, 7),
  216. { 0, }
  217. };
  218. MODULE_DEVICE_TABLE(pci, e100_id_table);
  219. enum mac {
  220. mac_82557_D100_A = 0,
  221. mac_82557_D100_B = 1,
  222. mac_82557_D100_C = 2,
  223. mac_82558_D101_A4 = 4,
  224. mac_82558_D101_B0 = 5,
  225. mac_82559_D101M = 8,
  226. mac_82559_D101S = 9,
  227. mac_82550_D102 = 12,
  228. mac_82550_D102_C = 13,
  229. mac_82551_E = 14,
  230. mac_82551_F = 15,
  231. mac_82551_10 = 16,
  232. mac_unknown = 0xFF,
  233. };
  234. enum phy {
  235. phy_100a = 0x000003E0,
  236. phy_100c = 0x035002A8,
  237. phy_82555_tx = 0x015002A8,
  238. phy_nsc_tx = 0x5C002000,
  239. phy_82562_et = 0x033002A8,
  240. phy_82562_em = 0x032002A8,
  241. phy_82562_ek = 0x031002A8,
  242. phy_82562_eh = 0x017002A8,
  243. phy_82552_v = 0xd061004d,
  244. phy_unknown = 0xFFFFFFFF,
  245. };
  246. /* CSR (Control/Status Registers) */
  247. struct csr {
  248. struct {
  249. u8 status;
  250. u8 stat_ack;
  251. u8 cmd_lo;
  252. u8 cmd_hi;
  253. u32 gen_ptr;
  254. } scb;
  255. u32 port;
  256. u16 flash_ctrl;
  257. u8 eeprom_ctrl_lo;
  258. u8 eeprom_ctrl_hi;
  259. u32 mdi_ctrl;
  260. u32 rx_dma_count;
  261. };
  262. enum scb_status {
  263. rus_no_res = 0x08,
  264. rus_ready = 0x10,
  265. rus_mask = 0x3C,
  266. };
  267. enum ru_state {
  268. RU_SUSPENDED = 0,
  269. RU_RUNNING = 1,
  270. RU_UNINITIALIZED = -1,
  271. };
  272. enum scb_stat_ack {
  273. stat_ack_not_ours = 0x00,
  274. stat_ack_sw_gen = 0x04,
  275. stat_ack_rnr = 0x10,
  276. stat_ack_cu_idle = 0x20,
  277. stat_ack_frame_rx = 0x40,
  278. stat_ack_cu_cmd_done = 0x80,
  279. stat_ack_not_present = 0xFF,
  280. stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
  281. stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
  282. };
  283. enum scb_cmd_hi {
  284. irq_mask_none = 0x00,
  285. irq_mask_all = 0x01,
  286. irq_sw_gen = 0x02,
  287. };
  288. enum scb_cmd_lo {
  289. cuc_nop = 0x00,
  290. ruc_start = 0x01,
  291. ruc_load_base = 0x06,
  292. cuc_start = 0x10,
  293. cuc_resume = 0x20,
  294. cuc_dump_addr = 0x40,
  295. cuc_dump_stats = 0x50,
  296. cuc_load_base = 0x60,
  297. cuc_dump_reset = 0x70,
  298. };
  299. enum cuc_dump {
  300. cuc_dump_complete = 0x0000A005,
  301. cuc_dump_reset_complete = 0x0000A007,
  302. };
  303. enum port {
  304. software_reset = 0x0000,
  305. selftest = 0x0001,
  306. selective_reset = 0x0002,
  307. };
  308. enum eeprom_ctrl_lo {
  309. eesk = 0x01,
  310. eecs = 0x02,
  311. eedi = 0x04,
  312. eedo = 0x08,
  313. };
  314. enum mdi_ctrl {
  315. mdi_write = 0x04000000,
  316. mdi_read = 0x08000000,
  317. mdi_ready = 0x10000000,
  318. };
  319. enum eeprom_op {
  320. op_write = 0x05,
  321. op_read = 0x06,
  322. op_ewds = 0x10,
  323. op_ewen = 0x13,
  324. };
  325. enum eeprom_offsets {
  326. eeprom_cnfg_mdix = 0x03,
  327. eeprom_phy_iface = 0x06,
  328. eeprom_id = 0x0A,
  329. eeprom_config_asf = 0x0D,
  330. eeprom_smbus_addr = 0x90,
  331. };
  332. enum eeprom_cnfg_mdix {
  333. eeprom_mdix_enabled = 0x0080,
  334. };
  335. enum eeprom_phy_iface {
  336. NoSuchPhy = 0,
  337. I82553AB,
  338. I82553C,
  339. I82503,
  340. DP83840,
  341. S80C240,
  342. S80C24,
  343. I82555,
  344. DP83840A = 10,
  345. };
  346. enum eeprom_id {
  347. eeprom_id_wol = 0x0020,
  348. };
  349. enum eeprom_config_asf {
  350. eeprom_asf = 0x8000,
  351. eeprom_gcl = 0x4000,
  352. };
  353. enum cb_status {
  354. cb_complete = 0x8000,
  355. cb_ok = 0x2000,
  356. };
  357. /*
  358. * cb_command - Command Block flags
  359. * @cb_tx_nc: 0: controller does CRC (normal), 1: CRC from skb memory
  360. */
  361. enum cb_command {
  362. cb_nop = 0x0000,
  363. cb_iaaddr = 0x0001,
  364. cb_config = 0x0002,
  365. cb_multi = 0x0003,
  366. cb_tx = 0x0004,
  367. cb_ucode = 0x0005,
  368. cb_dump = 0x0006,
  369. cb_tx_sf = 0x0008,
  370. cb_tx_nc = 0x0010,
  371. cb_cid = 0x1f00,
  372. cb_i = 0x2000,
  373. cb_s = 0x4000,
  374. cb_el = 0x8000,
  375. };
  376. struct rfd {
  377. __le16 status;
  378. __le16 command;
  379. __le32 link;
  380. __le32 rbd;
  381. __le16 actual_size;
  382. __le16 size;
  383. };
  384. struct rx {
  385. struct rx *next, *prev;
  386. struct sk_buff *skb;
  387. dma_addr_t dma_addr;
  388. };
  389. #if defined(__BIG_ENDIAN_BITFIELD)
  390. #define X(a,b) b,a
  391. #else
  392. #define X(a,b) a,b
  393. #endif
  394. struct config {
  395. /*0*/ u8 X(byte_count:6, pad0:2);
  396. /*1*/ u8 X(X(rx_fifo_limit:4, tx_fifo_limit:3), pad1:1);
  397. /*2*/ u8 adaptive_ifs;
  398. /*3*/ u8 X(X(X(X(mwi_enable:1, type_enable:1), read_align_enable:1),
  399. term_write_cache_line:1), pad3:4);
  400. /*4*/ u8 X(rx_dma_max_count:7, pad4:1);
  401. /*5*/ u8 X(tx_dma_max_count:7, dma_max_count_enable:1);
  402. /*6*/ u8 X(X(X(X(X(X(X(late_scb_update:1, direct_rx_dma:1),
  403. tno_intr:1), cna_intr:1), standard_tcb:1), standard_stat_counter:1),
  404. rx_save_overruns : 1), rx_save_bad_frames : 1);
  405. /*7*/ u8 X(X(X(X(X(rx_discard_short_frames:1, tx_underrun_retry:2),
  406. pad7:2), rx_extended_rfd:1), tx_two_frames_in_fifo:1),
  407. tx_dynamic_tbd:1);
  408. /*8*/ u8 X(X(mii_mode:1, pad8:6), csma_disabled:1);
  409. /*9*/ u8 X(X(X(X(X(rx_tcpudp_checksum:1, pad9:3), vlan_arp_tco:1),
  410. link_status_wake:1), arp_wake:1), mcmatch_wake:1);
  411. /*10*/ u8 X(X(X(pad10:3, no_source_addr_insertion:1), preamble_length:2),
  412. loopback:2);
  413. /*11*/ u8 X(linear_priority:3, pad11:5);
  414. /*12*/ u8 X(X(linear_priority_mode:1, pad12:3), ifs:4);
  415. /*13*/ u8 ip_addr_lo;
  416. /*14*/ u8 ip_addr_hi;
  417. /*15*/ u8 X(X(X(X(X(X(X(promiscuous_mode:1, broadcast_disabled:1),
  418. wait_after_win:1), pad15_1:1), ignore_ul_bit:1), crc_16_bit:1),
  419. pad15_2:1), crs_or_cdt:1);
  420. /*16*/ u8 fc_delay_lo;
  421. /*17*/ u8 fc_delay_hi;
  422. /*18*/ u8 X(X(X(X(X(rx_stripping:1, tx_padding:1), rx_crc_transfer:1),
  423. rx_long_ok:1), fc_priority_threshold:3), pad18:1);
  424. /*19*/ u8 X(X(X(X(X(X(X(addr_wake:1, magic_packet_disable:1),
  425. fc_disable:1), fc_restop:1), fc_restart:1), fc_reject:1),
  426. full_duplex_force:1), full_duplex_pin:1);
  427. /*20*/ u8 X(X(X(pad20_1:5, fc_priority_location:1), multi_ia:1), pad20_2:1);
  428. /*21*/ u8 X(X(pad21_1:3, multicast_all:1), pad21_2:4);
  429. /*22*/ u8 X(X(rx_d102_mode:1, rx_vlan_drop:1), pad22:6);
  430. u8 pad_d102[9];
  431. };
  432. #define E100_MAX_MULTICAST_ADDRS 64
  433. struct multi {
  434. __le16 count;
  435. u8 addr[E100_MAX_MULTICAST_ADDRS * ETH_ALEN + 2/*pad*/];
  436. };
  437. /* Important: keep total struct u32-aligned */
  438. #define UCODE_SIZE 134
  439. struct cb {
  440. __le16 status;
  441. __le16 command;
  442. __le32 link;
  443. union {
  444. u8 iaaddr[ETH_ALEN];
  445. __le32 ucode[UCODE_SIZE];
  446. struct config config;
  447. struct multi multi;
  448. struct {
  449. u32 tbd_array;
  450. u16 tcb_byte_count;
  451. u8 threshold;
  452. u8 tbd_count;
  453. struct {
  454. __le32 buf_addr;
  455. __le16 size;
  456. u16 eol;
  457. } tbd;
  458. } tcb;
  459. __le32 dump_buffer_addr;
  460. } u;
  461. struct cb *next, *prev;
  462. dma_addr_t dma_addr;
  463. struct sk_buff *skb;
  464. };
  465. enum loopback {
  466. lb_none = 0, lb_mac = 1, lb_phy = 3,
  467. };
  468. struct stats {
  469. __le32 tx_good_frames, tx_max_collisions, tx_late_collisions,
  470. tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
  471. tx_multiple_collisions, tx_total_collisions;
  472. __le32 rx_good_frames, rx_crc_errors, rx_alignment_errors,
  473. rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
  474. rx_short_frame_errors;
  475. __le32 fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
  476. __le16 xmt_tco_frames, rcv_tco_frames;
  477. __le32 complete;
  478. };
  479. struct mem {
  480. struct {
  481. u32 signature;
  482. u32 result;
  483. } selftest;
  484. struct stats stats;
  485. u8 dump_buf[596];
  486. };
  487. struct param_range {
  488. u32 min;
  489. u32 max;
  490. u32 count;
  491. };
  492. struct params {
  493. struct param_range rfds;
  494. struct param_range cbs;
  495. };
  496. struct nic {
  497. /* Begin: frequently used values: keep adjacent for cache effect */
  498. u32 msg_enable ____cacheline_aligned;
  499. struct net_device *netdev;
  500. struct pci_dev *pdev;
  501. u16 (*mdio_ctrl)(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data);
  502. struct rx *rxs ____cacheline_aligned;
  503. struct rx *rx_to_use;
  504. struct rx *rx_to_clean;
  505. struct rfd blank_rfd;
  506. enum ru_state ru_running;
  507. spinlock_t cb_lock ____cacheline_aligned;
  508. spinlock_t cmd_lock;
  509. struct csr __iomem *csr;
  510. enum scb_cmd_lo cuc_cmd;
  511. unsigned int cbs_avail;
  512. struct napi_struct napi;
  513. struct cb *cbs;
  514. struct cb *cb_to_use;
  515. struct cb *cb_to_send;
  516. struct cb *cb_to_clean;
  517. __le16 tx_command;
  518. /* End: frequently used values: keep adjacent for cache effect */
  519. enum {
  520. ich = (1 << 0),
  521. promiscuous = (1 << 1),
  522. multicast_all = (1 << 2),
  523. wol_magic = (1 << 3),
  524. ich_10h_workaround = (1 << 4),
  525. } flags ____cacheline_aligned;
  526. enum mac mac;
  527. enum phy phy;
  528. struct params params;
  529. struct timer_list watchdog;
  530. struct mii_if_info mii;
  531. struct work_struct tx_timeout_task;
  532. enum loopback loopback;
  533. struct mem *mem;
  534. dma_addr_t dma_addr;
  535. struct dma_pool *cbs_pool;
  536. dma_addr_t cbs_dma_addr;
  537. u8 adaptive_ifs;
  538. u8 tx_threshold;
  539. u32 tx_frames;
  540. u32 tx_collisions;
  541. u32 tx_deferred;
  542. u32 tx_single_collisions;
  543. u32 tx_multiple_collisions;
  544. u32 tx_fc_pause;
  545. u32 tx_tco_frames;
  546. u32 rx_fc_pause;
  547. u32 rx_fc_unsupported;
  548. u32 rx_tco_frames;
  549. u32 rx_short_frame_errors;
  550. u32 rx_over_length_errors;
  551. u16 eeprom_wc;
  552. __le16 eeprom[256];
  553. spinlock_t mdio_lock;
  554. const struct firmware *fw;
  555. };
  556. static inline void e100_write_flush(struct nic *nic)
  557. {
  558. /* Flush previous PCI writes through intermediate bridges
  559. * by doing a benign read */
  560. (void)ioread8(&nic->csr->scb.status);
  561. }
  562. static void e100_enable_irq(struct nic *nic)
  563. {
  564. unsigned long flags;
  565. spin_lock_irqsave(&nic->cmd_lock, flags);
  566. iowrite8(irq_mask_none, &nic->csr->scb.cmd_hi);
  567. e100_write_flush(nic);
  568. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  569. }
  570. static void e100_disable_irq(struct nic *nic)
  571. {
  572. unsigned long flags;
  573. spin_lock_irqsave(&nic->cmd_lock, flags);
  574. iowrite8(irq_mask_all, &nic->csr->scb.cmd_hi);
  575. e100_write_flush(nic);
  576. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  577. }
  578. static void e100_hw_reset(struct nic *nic)
  579. {
  580. /* Put CU and RU into idle with a selective reset to get
  581. * device off of PCI bus */
  582. iowrite32(selective_reset, &nic->csr->port);
  583. e100_write_flush(nic); udelay(20);
  584. /* Now fully reset device */
  585. iowrite32(software_reset, &nic->csr->port);
  586. e100_write_flush(nic); udelay(20);
  587. /* Mask off our interrupt line - it's unmasked after reset */
  588. e100_disable_irq(nic);
  589. }
  590. static int e100_self_test(struct nic *nic)
  591. {
  592. u32 dma_addr = nic->dma_addr + offsetof(struct mem, selftest);
  593. /* Passing the self-test is a pretty good indication
  594. * that the device can DMA to/from host memory */
  595. nic->mem->selftest.signature = 0;
  596. nic->mem->selftest.result = 0xFFFFFFFF;
  597. iowrite32(selftest | dma_addr, &nic->csr->port);
  598. e100_write_flush(nic);
  599. /* Wait 10 msec for self-test to complete */
  600. msleep(10);
  601. /* Interrupts are enabled after self-test */
  602. e100_disable_irq(nic);
  603. /* Check results of self-test */
  604. if (nic->mem->selftest.result != 0) {
  605. netif_err(nic, hw, nic->netdev,
  606. "Self-test failed: result=0x%08X\n",
  607. nic->mem->selftest.result);
  608. return -ETIMEDOUT;
  609. }
  610. if (nic->mem->selftest.signature == 0) {
  611. netif_err(nic, hw, nic->netdev, "Self-test failed: timed out\n");
  612. return -ETIMEDOUT;
  613. }
  614. return 0;
  615. }
  616. static void e100_eeprom_write(struct nic *nic, u16 addr_len, u16 addr, __le16 data)
  617. {
  618. u32 cmd_addr_data[3];
  619. u8 ctrl;
  620. int i, j;
  621. /* Three cmds: write/erase enable, write data, write/erase disable */
  622. cmd_addr_data[0] = op_ewen << (addr_len - 2);
  623. cmd_addr_data[1] = (((op_write << addr_len) | addr) << 16) |
  624. le16_to_cpu(data);
  625. cmd_addr_data[2] = op_ewds << (addr_len - 2);
  626. /* Bit-bang cmds to write word to eeprom */
  627. for (j = 0; j < 3; j++) {
  628. /* Chip select */
  629. iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  630. e100_write_flush(nic); udelay(4);
  631. for (i = 31; i >= 0; i--) {
  632. ctrl = (cmd_addr_data[j] & (1 << i)) ?
  633. eecs | eedi : eecs;
  634. iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
  635. e100_write_flush(nic); udelay(4);
  636. iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  637. e100_write_flush(nic); udelay(4);
  638. }
  639. /* Wait 10 msec for cmd to complete */
  640. msleep(10);
  641. /* Chip deselect */
  642. iowrite8(0, &nic->csr->eeprom_ctrl_lo);
  643. e100_write_flush(nic); udelay(4);
  644. }
  645. };
  646. /* General technique stolen from the eepro100 driver - very clever */
  647. static __le16 e100_eeprom_read(struct nic *nic, u16 *addr_len, u16 addr)
  648. {
  649. u32 cmd_addr_data;
  650. u16 data = 0;
  651. u8 ctrl;
  652. int i;
  653. cmd_addr_data = ((op_read << *addr_len) | addr) << 16;
  654. /* Chip select */
  655. iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  656. e100_write_flush(nic); udelay(4);
  657. /* Bit-bang to read word from eeprom */
  658. for (i = 31; i >= 0; i--) {
  659. ctrl = (cmd_addr_data & (1 << i)) ? eecs | eedi : eecs;
  660. iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
  661. e100_write_flush(nic); udelay(4);
  662. iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  663. e100_write_flush(nic); udelay(4);
  664. /* Eeprom drives a dummy zero to EEDO after receiving
  665. * complete address. Use this to adjust addr_len. */
  666. ctrl = ioread8(&nic->csr->eeprom_ctrl_lo);
  667. if (!(ctrl & eedo) && i > 16) {
  668. *addr_len -= (i - 16);
  669. i = 17;
  670. }
  671. data = (data << 1) | (ctrl & eedo ? 1 : 0);
  672. }
  673. /* Chip deselect */
  674. iowrite8(0, &nic->csr->eeprom_ctrl_lo);
  675. e100_write_flush(nic); udelay(4);
  676. return cpu_to_le16(data);
  677. };
  678. /* Load entire EEPROM image into driver cache and validate checksum */
  679. static int e100_eeprom_load(struct nic *nic)
  680. {
  681. u16 addr, addr_len = 8, checksum = 0;
  682. /* Try reading with an 8-bit addr len to discover actual addr len */
  683. e100_eeprom_read(nic, &addr_len, 0);
  684. nic->eeprom_wc = 1 << addr_len;
  685. for (addr = 0; addr < nic->eeprom_wc; addr++) {
  686. nic->eeprom[addr] = e100_eeprom_read(nic, &addr_len, addr);
  687. if (addr < nic->eeprom_wc - 1)
  688. checksum += le16_to_cpu(nic->eeprom[addr]);
  689. }
  690. /* The checksum, stored in the last word, is calculated such that
  691. * the sum of words should be 0xBABA */
  692. if (cpu_to_le16(0xBABA - checksum) != nic->eeprom[nic->eeprom_wc - 1]) {
  693. netif_err(nic, probe, nic->netdev, "EEPROM corrupted\n");
  694. if (!eeprom_bad_csum_allow)
  695. return -EAGAIN;
  696. }
  697. return 0;
  698. }
  699. /* Save (portion of) driver EEPROM cache to device and update checksum */
  700. static int e100_eeprom_save(struct nic *nic, u16 start, u16 count)
  701. {
  702. u16 addr, addr_len = 8, checksum = 0;
  703. /* Try reading with an 8-bit addr len to discover actual addr len */
  704. e100_eeprom_read(nic, &addr_len, 0);
  705. nic->eeprom_wc = 1 << addr_len;
  706. if (start + count >= nic->eeprom_wc)
  707. return -EINVAL;
  708. for (addr = start; addr < start + count; addr++)
  709. e100_eeprom_write(nic, addr_len, addr, nic->eeprom[addr]);
  710. /* The checksum, stored in the last word, is calculated such that
  711. * the sum of words should be 0xBABA */
  712. for (addr = 0; addr < nic->eeprom_wc - 1; addr++)
  713. checksum += le16_to_cpu(nic->eeprom[addr]);
  714. nic->eeprom[nic->eeprom_wc - 1] = cpu_to_le16(0xBABA - checksum);
  715. e100_eeprom_write(nic, addr_len, nic->eeprom_wc - 1,
  716. nic->eeprom[nic->eeprom_wc - 1]);
  717. return 0;
  718. }
  719. #define E100_WAIT_SCB_TIMEOUT 20000 /* we might have to wait 100ms!!! */
  720. #define E100_WAIT_SCB_FAST 20 /* delay like the old code */
  721. static int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr)
  722. {
  723. unsigned long flags;
  724. unsigned int i;
  725. int err = 0;
  726. spin_lock_irqsave(&nic->cmd_lock, flags);
  727. /* Previous command is accepted when SCB clears */
  728. for (i = 0; i < E100_WAIT_SCB_TIMEOUT; i++) {
  729. if (likely(!ioread8(&nic->csr->scb.cmd_lo)))
  730. break;
  731. cpu_relax();
  732. if (unlikely(i > E100_WAIT_SCB_FAST))
  733. udelay(5);
  734. }
  735. if (unlikely(i == E100_WAIT_SCB_TIMEOUT)) {
  736. err = -EAGAIN;
  737. goto err_unlock;
  738. }
  739. if (unlikely(cmd != cuc_resume))
  740. iowrite32(dma_addr, &nic->csr->scb.gen_ptr);
  741. iowrite8(cmd, &nic->csr->scb.cmd_lo);
  742. err_unlock:
  743. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  744. return err;
  745. }
  746. static int e100_exec_cb(struct nic *nic, struct sk_buff *skb,
  747. int (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
  748. {
  749. struct cb *cb;
  750. unsigned long flags;
  751. int err;
  752. spin_lock_irqsave(&nic->cb_lock, flags);
  753. if (unlikely(!nic->cbs_avail)) {
  754. err = -ENOMEM;
  755. goto err_unlock;
  756. }
  757. cb = nic->cb_to_use;
  758. nic->cb_to_use = cb->next;
  759. nic->cbs_avail--;
  760. cb->skb = skb;
  761. err = cb_prepare(nic, cb, skb);
  762. if (err)
  763. goto err_unlock;
  764. if (unlikely(!nic->cbs_avail))
  765. err = -ENOSPC;
  766. /* Order is important otherwise we'll be in a race with h/w:
  767. * set S-bit in current first, then clear S-bit in previous. */
  768. cb->command |= cpu_to_le16(cb_s);
  769. dma_wmb();
  770. cb->prev->command &= cpu_to_le16(~cb_s);
  771. while (nic->cb_to_send != nic->cb_to_use) {
  772. if (unlikely(e100_exec_cmd(nic, nic->cuc_cmd,
  773. nic->cb_to_send->dma_addr))) {
  774. /* Ok, here's where things get sticky. It's
  775. * possible that we can't schedule the command
  776. * because the controller is too busy, so
  777. * let's just queue the command and try again
  778. * when another command is scheduled. */
  779. if (err == -ENOSPC) {
  780. //request a reset
  781. schedule_work(&nic->tx_timeout_task);
  782. }
  783. break;
  784. } else {
  785. nic->cuc_cmd = cuc_resume;
  786. nic->cb_to_send = nic->cb_to_send->next;
  787. }
  788. }
  789. err_unlock:
  790. spin_unlock_irqrestore(&nic->cb_lock, flags);
  791. return err;
  792. }
  793. static int mdio_read(struct net_device *netdev, int addr, int reg)
  794. {
  795. struct nic *nic = netdev_priv(netdev);
  796. return nic->mdio_ctrl(nic, addr, mdi_read, reg, 0);
  797. }
  798. static void mdio_write(struct net_device *netdev, int addr, int reg, int data)
  799. {
  800. struct nic *nic = netdev_priv(netdev);
  801. nic->mdio_ctrl(nic, addr, mdi_write, reg, data);
  802. }
  803. /* the standard mdio_ctrl() function for usual MII-compliant hardware */
  804. static u16 mdio_ctrl_hw(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data)
  805. {
  806. u32 data_out = 0;
  807. unsigned int i;
  808. unsigned long flags;
  809. /*
  810. * Stratus87247: we shouldn't be writing the MDI control
  811. * register until the Ready bit shows True. Also, since
  812. * manipulation of the MDI control registers is a multi-step
  813. * procedure it should be done under lock.
  814. */
  815. spin_lock_irqsave(&nic->mdio_lock, flags);
  816. for (i = 100; i; --i) {
  817. if (ioread32(&nic->csr->mdi_ctrl) & mdi_ready)
  818. break;
  819. udelay(20);
  820. }
  821. if (unlikely(!i)) {
  822. netdev_err(nic->netdev, "e100.mdio_ctrl won't go Ready\n");
  823. spin_unlock_irqrestore(&nic->mdio_lock, flags);
  824. return 0; /* No way to indicate timeout error */
  825. }
  826. iowrite32((reg << 16) | (addr << 21) | dir | data, &nic->csr->mdi_ctrl);
  827. for (i = 0; i < 100; i++) {
  828. udelay(20);
  829. if ((data_out = ioread32(&nic->csr->mdi_ctrl)) & mdi_ready)
  830. break;
  831. }
  832. spin_unlock_irqrestore(&nic->mdio_lock, flags);
  833. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  834. "%s:addr=%d, reg=%d, data_in=0x%04X, data_out=0x%04X\n",
  835. dir == mdi_read ? "READ" : "WRITE",
  836. addr, reg, data, data_out);
  837. return (u16)data_out;
  838. }
  839. /* slightly tweaked mdio_ctrl() function for phy_82552_v specifics */
  840. static u16 mdio_ctrl_phy_82552_v(struct nic *nic,
  841. u32 addr,
  842. u32 dir,
  843. u32 reg,
  844. u16 data)
  845. {
  846. if ((reg == MII_BMCR) && (dir == mdi_write)) {
  847. if (data & (BMCR_ANRESTART | BMCR_ANENABLE)) {
  848. u16 advert = mdio_read(nic->netdev, nic->mii.phy_id,
  849. MII_ADVERTISE);
  850. /*
  851. * Workaround Si issue where sometimes the part will not
  852. * autoneg to 100Mbps even when advertised.
  853. */
  854. if (advert & ADVERTISE_100FULL)
  855. data |= BMCR_SPEED100 | BMCR_FULLDPLX;
  856. else if (advert & ADVERTISE_100HALF)
  857. data |= BMCR_SPEED100;
  858. }
  859. }
  860. return mdio_ctrl_hw(nic, addr, dir, reg, data);
  861. }
  862. /* Fully software-emulated mdio_ctrl() function for cards without
  863. * MII-compliant PHYs.
  864. * For now, this is mainly geared towards 80c24 support; in case of further
  865. * requirements for other types (i82503, ...?) either extend this mechanism
  866. * or split it, whichever is cleaner.
  867. */
  868. static u16 mdio_ctrl_phy_mii_emulated(struct nic *nic,
  869. u32 addr,
  870. u32 dir,
  871. u32 reg,
  872. u16 data)
  873. {
  874. /* might need to allocate a netdev_priv'ed register array eventually
  875. * to be able to record state changes, but for now
  876. * some fully hardcoded register handling ought to be ok I guess. */
  877. if (dir == mdi_read) {
  878. switch (reg) {
  879. case MII_BMCR:
  880. /* Auto-negotiation, right? */
  881. return BMCR_ANENABLE |
  882. BMCR_FULLDPLX;
  883. case MII_BMSR:
  884. return BMSR_LSTATUS /* for mii_link_ok() */ |
  885. BMSR_ANEGCAPABLE |
  886. BMSR_10FULL;
  887. case MII_ADVERTISE:
  888. /* 80c24 is a "combo card" PHY, right? */
  889. return ADVERTISE_10HALF |
  890. ADVERTISE_10FULL;
  891. default:
  892. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  893. "%s:addr=%d, reg=%d, data=0x%04X: unimplemented emulation!\n",
  894. dir == mdi_read ? "READ" : "WRITE",
  895. addr, reg, data);
  896. return 0xFFFF;
  897. }
  898. } else {
  899. switch (reg) {
  900. default:
  901. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  902. "%s:addr=%d, reg=%d, data=0x%04X: unimplemented emulation!\n",
  903. dir == mdi_read ? "READ" : "WRITE",
  904. addr, reg, data);
  905. return 0xFFFF;
  906. }
  907. }
  908. }
  909. static inline int e100_phy_supports_mii(struct nic *nic)
  910. {
  911. /* for now, just check it by comparing whether we
  912. are using MII software emulation.
  913. */
  914. return (nic->mdio_ctrl != mdio_ctrl_phy_mii_emulated);
  915. }
  916. static void e100_get_defaults(struct nic *nic)
  917. {
  918. struct param_range rfds = { .min = 16, .max = 256, .count = 256 };
  919. struct param_range cbs = { .min = 64, .max = 256, .count = 128 };
  920. /* MAC type is encoded as rev ID; exception: ICH is treated as 82559 */
  921. nic->mac = (nic->flags & ich) ? mac_82559_D101M : nic->pdev->revision;
  922. if (nic->mac == mac_unknown)
  923. nic->mac = mac_82557_D100_A;
  924. nic->params.rfds = rfds;
  925. nic->params.cbs = cbs;
  926. /* Quadwords to DMA into FIFO before starting frame transmit */
  927. nic->tx_threshold = 0xE0;
  928. /* no interrupt for every tx completion, delay = 256us if not 557 */
  929. nic->tx_command = cpu_to_le16(cb_tx | cb_tx_sf |
  930. ((nic->mac >= mac_82558_D101_A4) ? cb_cid : cb_i));
  931. /* Template for a freshly allocated RFD */
  932. nic->blank_rfd.command = 0;
  933. nic->blank_rfd.rbd = cpu_to_le32(0xFFFFFFFF);
  934. nic->blank_rfd.size = cpu_to_le16(VLAN_ETH_FRAME_LEN + ETH_FCS_LEN);
  935. /* MII setup */
  936. nic->mii.phy_id_mask = 0x1F;
  937. nic->mii.reg_num_mask = 0x1F;
  938. nic->mii.dev = nic->netdev;
  939. nic->mii.mdio_read = mdio_read;
  940. nic->mii.mdio_write = mdio_write;
  941. }
  942. static int e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  943. {
  944. struct config *config = &cb->u.config;
  945. u8 *c = (u8 *)config;
  946. struct net_device *netdev = nic->netdev;
  947. cb->command = cpu_to_le16(cb_config);
  948. memset(config, 0, sizeof(struct config));
  949. config->byte_count = 0x16; /* bytes in this struct */
  950. config->rx_fifo_limit = 0x8; /* bytes in FIFO before DMA */
  951. config->direct_rx_dma = 0x1; /* reserved */
  952. config->standard_tcb = 0x1; /* 1=standard, 0=extended */
  953. config->standard_stat_counter = 0x1; /* 1=standard, 0=extended */
  954. config->rx_discard_short_frames = 0x1; /* 1=discard, 0=pass */
  955. config->tx_underrun_retry = 0x3; /* # of underrun retries */
  956. if (e100_phy_supports_mii(nic))
  957. config->mii_mode = 1; /* 1=MII mode, 0=i82503 mode */
  958. config->pad10 = 0x6;
  959. config->no_source_addr_insertion = 0x1; /* 1=no, 0=yes */
  960. config->preamble_length = 0x2; /* 0=1, 1=3, 2=7, 3=15 bytes */
  961. config->ifs = 0x6; /* x16 = inter frame spacing */
  962. config->ip_addr_hi = 0xF2; /* ARP IP filter - not used */
  963. config->pad15_1 = 0x1;
  964. config->pad15_2 = 0x1;
  965. config->crs_or_cdt = 0x0; /* 0=CRS only, 1=CRS or CDT */
  966. config->fc_delay_hi = 0x40; /* time delay for fc frame */
  967. config->tx_padding = 0x1; /* 1=pad short frames */
  968. config->fc_priority_threshold = 0x7; /* 7=priority fc disabled */
  969. config->pad18 = 0x1;
  970. config->full_duplex_pin = 0x1; /* 1=examine FDX# pin */
  971. config->pad20_1 = 0x1F;
  972. config->fc_priority_location = 0x1; /* 1=byte#31, 0=byte#19 */
  973. config->pad21_1 = 0x5;
  974. config->adaptive_ifs = nic->adaptive_ifs;
  975. config->loopback = nic->loopback;
  976. if (nic->mii.force_media && nic->mii.full_duplex)
  977. config->full_duplex_force = 0x1; /* 1=force, 0=auto */
  978. if (nic->flags & promiscuous || nic->loopback) {
  979. config->rx_save_bad_frames = 0x1; /* 1=save, 0=discard */
  980. config->rx_discard_short_frames = 0x0; /* 1=discard, 0=save */
  981. config->promiscuous_mode = 0x1; /* 1=on, 0=off */
  982. }
  983. if (unlikely(netdev->features & NETIF_F_RXFCS))
  984. config->rx_crc_transfer = 0x1; /* 1=save, 0=discard */
  985. if (nic->flags & multicast_all)
  986. config->multicast_all = 0x1; /* 1=accept, 0=no */
  987. /* disable WoL when up */
  988. if (netif_running(nic->netdev) || !(nic->flags & wol_magic))
  989. config->magic_packet_disable = 0x1; /* 1=off, 0=on */
  990. if (nic->mac >= mac_82558_D101_A4) {
  991. config->fc_disable = 0x1; /* 1=Tx fc off, 0=Tx fc on */
  992. config->mwi_enable = 0x1; /* 1=enable, 0=disable */
  993. config->standard_tcb = 0x0; /* 1=standard, 0=extended */
  994. config->rx_long_ok = 0x1; /* 1=VLANs ok, 0=standard */
  995. if (nic->mac >= mac_82559_D101M) {
  996. config->tno_intr = 0x1; /* TCO stats enable */
  997. /* Enable TCO in extended config */
  998. if (nic->mac >= mac_82551_10) {
  999. config->byte_count = 0x20; /* extended bytes */
  1000. config->rx_d102_mode = 0x1; /* GMRC for TCO */
  1001. }
  1002. } else {
  1003. config->standard_stat_counter = 0x0;
  1004. }
  1005. }
  1006. if (netdev->features & NETIF_F_RXALL) {
  1007. config->rx_save_overruns = 0x1; /* 1=save, 0=discard */
  1008. config->rx_save_bad_frames = 0x1; /* 1=save, 0=discard */
  1009. config->rx_discard_short_frames = 0x0; /* 1=discard, 0=save */
  1010. }
  1011. netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[00-07]=%8ph\n",
  1012. c + 0);
  1013. netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[08-15]=%8ph\n",
  1014. c + 8);
  1015. netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[16-23]=%8ph\n",
  1016. c + 16);
  1017. return 0;
  1018. }
  1019. /*************************************************************************
  1020. * CPUSaver parameters
  1021. *
  1022. * All CPUSaver parameters are 16-bit literals that are part of a
  1023. * "move immediate value" instruction. By changing the value of
  1024. * the literal in the instruction before the code is loaded, the
  1025. * driver can change the algorithm.
  1026. *
  1027. * INTDELAY - This loads the dead-man timer with its initial value.
  1028. * When this timer expires the interrupt is asserted, and the
  1029. * timer is reset each time a new packet is received. (see
  1030. * BUNDLEMAX below to set the limit on number of chained packets)
  1031. * The current default is 0x600 or 1536. Experiments show that
  1032. * the value should probably stay within the 0x200 - 0x1000.
  1033. *
  1034. * BUNDLEMAX -
  1035. * This sets the maximum number of frames that will be bundled. In
  1036. * some situations, such as the TCP windowing algorithm, it may be
  1037. * better to limit the growth of the bundle size than let it go as
  1038. * high as it can, because that could cause too much added latency.
  1039. * The default is six, because this is the number of packets in the
  1040. * default TCP window size. A value of 1 would make CPUSaver indicate
  1041. * an interrupt for every frame received. If you do not want to put
  1042. * a limit on the bundle size, set this value to xFFFF.
  1043. *
  1044. * BUNDLESMALL -
  1045. * This contains a bit-mask describing the minimum size frame that
  1046. * will be bundled. The default masks the lower 7 bits, which means
  1047. * that any frame less than 128 bytes in length will not be bundled,
  1048. * but will instead immediately generate an interrupt. This does
  1049. * not affect the current bundle in any way. Any frame that is 128
  1050. * bytes or large will be bundled normally. This feature is meant
  1051. * to provide immediate indication of ACK frames in a TCP environment.
  1052. * Customers were seeing poor performance when a machine with CPUSaver
  1053. * enabled was sending but not receiving. The delay introduced when
  1054. * the ACKs were received was enough to reduce total throughput, because
  1055. * the sender would sit idle until the ACK was finally seen.
  1056. *
  1057. * The current default is 0xFF80, which masks out the lower 7 bits.
  1058. * This means that any frame which is x7F (127) bytes or smaller
  1059. * will cause an immediate interrupt. Because this value must be a
  1060. * bit mask, there are only a few valid values that can be used. To
  1061. * turn this feature off, the driver can write the value xFFFF to the
  1062. * lower word of this instruction (in the same way that the other
  1063. * parameters are used). Likewise, a value of 0xF800 (2047) would
  1064. * cause an interrupt to be generated for every frame, because all
  1065. * standard Ethernet frames are <= 2047 bytes in length.
  1066. *************************************************************************/
  1067. /* if you wish to disable the ucode functionality, while maintaining the
  1068. * workarounds it provides, set the following defines to:
  1069. * BUNDLESMALL 0
  1070. * BUNDLEMAX 1
  1071. * INTDELAY 1
  1072. */
  1073. #define BUNDLESMALL 1
  1074. #define BUNDLEMAX (u16)6
  1075. #define INTDELAY (u16)1536 /* 0x600 */
  1076. /* Initialize firmware */
  1077. static const struct firmware *e100_request_firmware(struct nic *nic)
  1078. {
  1079. const char *fw_name;
  1080. const struct firmware *fw = nic->fw;
  1081. u8 timer, bundle, min_size;
  1082. int err = 0;
  1083. bool required = false;
  1084. /* do not load u-code for ICH devices */
  1085. if (nic->flags & ich)
  1086. return NULL;
  1087. /* Search for ucode match against h/w revision
  1088. *
  1089. * Based on comments in the source code for the FreeBSD fxp
  1090. * driver, the FIRMWARE_D102E ucode includes both CPUSaver and
  1091. *
  1092. * "fixes for bugs in the B-step hardware (specifically, bugs
  1093. * with Inline Receive)."
  1094. *
  1095. * So we must fail if it cannot be loaded.
  1096. *
  1097. * The other microcode files are only required for the optional
  1098. * CPUSaver feature. Nice to have, but no reason to fail.
  1099. */
  1100. if (nic->mac == mac_82559_D101M) {
  1101. fw_name = FIRMWARE_D101M;
  1102. } else if (nic->mac == mac_82559_D101S) {
  1103. fw_name = FIRMWARE_D101S;
  1104. } else if (nic->mac == mac_82551_F || nic->mac == mac_82551_10) {
  1105. fw_name = FIRMWARE_D102E;
  1106. required = true;
  1107. } else { /* No ucode on other devices */
  1108. return NULL;
  1109. }
  1110. /* If the firmware has not previously been loaded, request a pointer
  1111. * to it. If it was previously loaded, we are reinitializing the
  1112. * adapter, possibly in a resume from hibernate, in which case
  1113. * request_firmware() cannot be used.
  1114. */
  1115. if (!fw)
  1116. err = request_firmware(&fw, fw_name, &nic->pdev->dev);
  1117. if (err) {
  1118. if (required) {
  1119. netif_err(nic, probe, nic->netdev,
  1120. "Failed to load firmware \"%s\": %d\n",
  1121. fw_name, err);
  1122. return ERR_PTR(err);
  1123. } else {
  1124. netif_info(nic, probe, nic->netdev,
  1125. "CPUSaver disabled. Needs \"%s\": %d\n",
  1126. fw_name, err);
  1127. return NULL;
  1128. }
  1129. }
  1130. /* Firmware should be precisely UCODE_SIZE (words) plus three bytes
  1131. indicating the offsets for BUNDLESMALL, BUNDLEMAX, INTDELAY */
  1132. if (fw->size != UCODE_SIZE * 4 + 3) {
  1133. netif_err(nic, probe, nic->netdev,
  1134. "Firmware \"%s\" has wrong size %zu\n",
  1135. fw_name, fw->size);
  1136. release_firmware(fw);
  1137. return ERR_PTR(-EINVAL);
  1138. }
  1139. /* Read timer, bundle and min_size from end of firmware blob */
  1140. timer = fw->data[UCODE_SIZE * 4];
  1141. bundle = fw->data[UCODE_SIZE * 4 + 1];
  1142. min_size = fw->data[UCODE_SIZE * 4 + 2];
  1143. if (timer >= UCODE_SIZE || bundle >= UCODE_SIZE ||
  1144. min_size >= UCODE_SIZE) {
  1145. netif_err(nic, probe, nic->netdev,
  1146. "\"%s\" has bogus offset values (0x%x,0x%x,0x%x)\n",
  1147. fw_name, timer, bundle, min_size);
  1148. release_firmware(fw);
  1149. return ERR_PTR(-EINVAL);
  1150. }
  1151. /* OK, firmware is validated and ready to use. Save a pointer
  1152. * to it in the nic */
  1153. nic->fw = fw;
  1154. return fw;
  1155. }
  1156. static int e100_setup_ucode(struct nic *nic, struct cb *cb,
  1157. struct sk_buff *skb)
  1158. {
  1159. const struct firmware *fw = (void *)skb;
  1160. u8 timer, bundle, min_size;
  1161. /* It's not a real skb; we just abused the fact that e100_exec_cb
  1162. will pass it through to here... */
  1163. cb->skb = NULL;
  1164. /* firmware is stored as little endian already */
  1165. memcpy(cb->u.ucode, fw->data, UCODE_SIZE * 4);
  1166. /* Read timer, bundle and min_size from end of firmware blob */
  1167. timer = fw->data[UCODE_SIZE * 4];
  1168. bundle = fw->data[UCODE_SIZE * 4 + 1];
  1169. min_size = fw->data[UCODE_SIZE * 4 + 2];
  1170. /* Insert user-tunable settings in cb->u.ucode */
  1171. cb->u.ucode[timer] &= cpu_to_le32(0xFFFF0000);
  1172. cb->u.ucode[timer] |= cpu_to_le32(INTDELAY);
  1173. cb->u.ucode[bundle] &= cpu_to_le32(0xFFFF0000);
  1174. cb->u.ucode[bundle] |= cpu_to_le32(BUNDLEMAX);
  1175. cb->u.ucode[min_size] &= cpu_to_le32(0xFFFF0000);
  1176. cb->u.ucode[min_size] |= cpu_to_le32((BUNDLESMALL) ? 0xFFFF : 0xFF80);
  1177. cb->command = cpu_to_le16(cb_ucode | cb_el);
  1178. return 0;
  1179. }
  1180. static inline int e100_load_ucode_wait(struct nic *nic)
  1181. {
  1182. const struct firmware *fw;
  1183. int err = 0, counter = 50;
  1184. struct cb *cb = nic->cb_to_clean;
  1185. fw = e100_request_firmware(nic);
  1186. /* If it's NULL, then no ucode is required */
  1187. if (IS_ERR_OR_NULL(fw))
  1188. return PTR_ERR_OR_ZERO(fw);
  1189. if ((err = e100_exec_cb(nic, (void *)fw, e100_setup_ucode)))
  1190. netif_err(nic, probe, nic->netdev,
  1191. "ucode cmd failed with error %d\n", err);
  1192. /* must restart cuc */
  1193. nic->cuc_cmd = cuc_start;
  1194. /* wait for completion */
  1195. e100_write_flush(nic);
  1196. udelay(10);
  1197. /* wait for possibly (ouch) 500ms */
  1198. while (!(cb->status & cpu_to_le16(cb_complete))) {
  1199. msleep(10);
  1200. if (!--counter) break;
  1201. }
  1202. /* ack any interrupts, something could have been set */
  1203. iowrite8(~0, &nic->csr->scb.stat_ack);
  1204. /* if the command failed, or is not OK, notify and return */
  1205. if (!counter || !(cb->status & cpu_to_le16(cb_ok))) {
  1206. netif_err(nic, probe, nic->netdev, "ucode load failed\n");
  1207. err = -EPERM;
  1208. }
  1209. return err;
  1210. }
  1211. static int e100_setup_iaaddr(struct nic *nic, struct cb *cb,
  1212. struct sk_buff *skb)
  1213. {
  1214. cb->command = cpu_to_le16(cb_iaaddr);
  1215. memcpy(cb->u.iaaddr, nic->netdev->dev_addr, ETH_ALEN);
  1216. return 0;
  1217. }
  1218. static int e100_dump(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  1219. {
  1220. cb->command = cpu_to_le16(cb_dump);
  1221. cb->u.dump_buffer_addr = cpu_to_le32(nic->dma_addr +
  1222. offsetof(struct mem, dump_buf));
  1223. return 0;
  1224. }
  1225. static int e100_phy_check_without_mii(struct nic *nic)
  1226. {
  1227. u8 phy_type;
  1228. int without_mii;
  1229. phy_type = (le16_to_cpu(nic->eeprom[eeprom_phy_iface]) >> 8) & 0x0f;
  1230. switch (phy_type) {
  1231. case NoSuchPhy: /* Non-MII PHY; UNTESTED! */
  1232. case I82503: /* Non-MII PHY; UNTESTED! */
  1233. case S80C24: /* Non-MII PHY; tested and working */
  1234. /* paragraph from the FreeBSD driver, "FXP_PHY_80C24":
  1235. * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
  1236. * doesn't have a programming interface of any sort. The
  1237. * media is sensed automatically based on how the link partner
  1238. * is configured. This is, in essence, manual configuration.
  1239. */
  1240. netif_info(nic, probe, nic->netdev,
  1241. "found MII-less i82503 or 80c24 or other PHY\n");
  1242. nic->mdio_ctrl = mdio_ctrl_phy_mii_emulated;
  1243. nic->mii.phy_id = 0; /* is this ok for an MII-less PHY? */
  1244. /* these might be needed for certain MII-less cards...
  1245. * nic->flags |= ich;
  1246. * nic->flags |= ich_10h_workaround; */
  1247. without_mii = 1;
  1248. break;
  1249. default:
  1250. without_mii = 0;
  1251. break;
  1252. }
  1253. return without_mii;
  1254. }
  1255. #define NCONFIG_AUTO_SWITCH 0x0080
  1256. #define MII_NSC_CONG MII_RESV1
  1257. #define NSC_CONG_ENABLE 0x0100
  1258. #define NSC_CONG_TXREADY 0x0400
  1259. static int e100_phy_init(struct nic *nic)
  1260. {
  1261. struct net_device *netdev = nic->netdev;
  1262. u32 addr;
  1263. u16 bmcr, stat, id_lo, id_hi, cong;
  1264. /* Discover phy addr by searching addrs in order {1,0,2,..., 31} */
  1265. for (addr = 0; addr < 32; addr++) {
  1266. nic->mii.phy_id = (addr == 0) ? 1 : (addr == 1) ? 0 : addr;
  1267. bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
  1268. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  1269. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  1270. if (!((bmcr == 0xFFFF) || ((stat == 0) && (bmcr == 0))))
  1271. break;
  1272. }
  1273. if (addr == 32) {
  1274. /* uhoh, no PHY detected: check whether we seem to be some
  1275. * weird, rare variant which is *known* to not have any MII.
  1276. * But do this AFTER MII checking only, since this does
  1277. * lookup of EEPROM values which may easily be unreliable. */
  1278. if (e100_phy_check_without_mii(nic))
  1279. return 0; /* simply return and hope for the best */
  1280. else {
  1281. /* for unknown cases log a fatal error */
  1282. netif_err(nic, hw, nic->netdev,
  1283. "Failed to locate any known PHY, aborting\n");
  1284. return -EAGAIN;
  1285. }
  1286. } else
  1287. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  1288. "phy_addr = %d\n", nic->mii.phy_id);
  1289. /* Get phy ID */
  1290. id_lo = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID1);
  1291. id_hi = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID2);
  1292. nic->phy = (u32)id_hi << 16 | (u32)id_lo;
  1293. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  1294. "phy ID = 0x%08X\n", nic->phy);
  1295. /* Select the phy and isolate the rest */
  1296. for (addr = 0; addr < 32; addr++) {
  1297. if (addr != nic->mii.phy_id) {
  1298. mdio_write(netdev, addr, MII_BMCR, BMCR_ISOLATE);
  1299. } else if (nic->phy != phy_82552_v) {
  1300. bmcr = mdio_read(netdev, addr, MII_BMCR);
  1301. mdio_write(netdev, addr, MII_BMCR,
  1302. bmcr & ~BMCR_ISOLATE);
  1303. }
  1304. }
  1305. /*
  1306. * Workaround for 82552:
  1307. * Clear the ISOLATE bit on selected phy_id last (mirrored on all
  1308. * other phy_id's) using bmcr value from addr discovery loop above.
  1309. */
  1310. if (nic->phy == phy_82552_v)
  1311. mdio_write(netdev, nic->mii.phy_id, MII_BMCR,
  1312. bmcr & ~BMCR_ISOLATE);
  1313. /* Handle National tx phys */
  1314. #define NCS_PHY_MODEL_MASK 0xFFF0FFFF
  1315. if ((nic->phy & NCS_PHY_MODEL_MASK) == phy_nsc_tx) {
  1316. /* Disable congestion control */
  1317. cong = mdio_read(netdev, nic->mii.phy_id, MII_NSC_CONG);
  1318. cong |= NSC_CONG_TXREADY;
  1319. cong &= ~NSC_CONG_ENABLE;
  1320. mdio_write(netdev, nic->mii.phy_id, MII_NSC_CONG, cong);
  1321. }
  1322. if (nic->phy == phy_82552_v) {
  1323. u16 advert = mdio_read(netdev, nic->mii.phy_id, MII_ADVERTISE);
  1324. /* assign special tweaked mdio_ctrl() function */
  1325. nic->mdio_ctrl = mdio_ctrl_phy_82552_v;
  1326. /* Workaround Si not advertising flow-control during autoneg */
  1327. advert |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1328. mdio_write(netdev, nic->mii.phy_id, MII_ADVERTISE, advert);
  1329. /* Reset for the above changes to take effect */
  1330. bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
  1331. bmcr |= BMCR_RESET;
  1332. mdio_write(netdev, nic->mii.phy_id, MII_BMCR, bmcr);
  1333. } else if ((nic->mac >= mac_82550_D102) || ((nic->flags & ich) &&
  1334. (mdio_read(netdev, nic->mii.phy_id, MII_TPISTATUS) & 0x8000) &&
  1335. (le16_to_cpu(nic->eeprom[eeprom_cnfg_mdix]) & eeprom_mdix_enabled))) {
  1336. /* enable/disable MDI/MDI-X auto-switching. */
  1337. mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG,
  1338. nic->mii.force_media ? 0 : NCONFIG_AUTO_SWITCH);
  1339. }
  1340. return 0;
  1341. }
  1342. static int e100_hw_init(struct nic *nic)
  1343. {
  1344. int err = 0;
  1345. e100_hw_reset(nic);
  1346. netif_err(nic, hw, nic->netdev, "e100_hw_init\n");
  1347. if ((err = e100_self_test(nic)))
  1348. return err;
  1349. if ((err = e100_phy_init(nic)))
  1350. return err;
  1351. if ((err = e100_exec_cmd(nic, cuc_load_base, 0)))
  1352. return err;
  1353. if ((err = e100_exec_cmd(nic, ruc_load_base, 0)))
  1354. return err;
  1355. if ((err = e100_load_ucode_wait(nic)))
  1356. return err;
  1357. if ((err = e100_exec_cb(nic, NULL, e100_configure)))
  1358. return err;
  1359. if ((err = e100_exec_cb(nic, NULL, e100_setup_iaaddr)))
  1360. return err;
  1361. if ((err = e100_exec_cmd(nic, cuc_dump_addr,
  1362. nic->dma_addr + offsetof(struct mem, stats))))
  1363. return err;
  1364. if ((err = e100_exec_cmd(nic, cuc_dump_reset, 0)))
  1365. return err;
  1366. e100_disable_irq(nic);
  1367. return 0;
  1368. }
  1369. static int e100_multi(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  1370. {
  1371. struct net_device *netdev = nic->netdev;
  1372. struct netdev_hw_addr *ha;
  1373. u16 i, count = min(netdev_mc_count(netdev), E100_MAX_MULTICAST_ADDRS);
  1374. cb->command = cpu_to_le16(cb_multi);
  1375. cb->u.multi.count = cpu_to_le16(count * ETH_ALEN);
  1376. i = 0;
  1377. netdev_for_each_mc_addr(ha, netdev) {
  1378. if (i == count)
  1379. break;
  1380. memcpy(&cb->u.multi.addr[i++ * ETH_ALEN], &ha->addr,
  1381. ETH_ALEN);
  1382. }
  1383. return 0;
  1384. }
  1385. static void e100_set_multicast_list(struct net_device *netdev)
  1386. {
  1387. struct nic *nic = netdev_priv(netdev);
  1388. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  1389. "mc_count=%d, flags=0x%04X\n",
  1390. netdev_mc_count(netdev), netdev->flags);
  1391. if (netdev->flags & IFF_PROMISC)
  1392. nic->flags |= promiscuous;
  1393. else
  1394. nic->flags &= ~promiscuous;
  1395. if (netdev->flags & IFF_ALLMULTI ||
  1396. netdev_mc_count(netdev) > E100_MAX_MULTICAST_ADDRS)
  1397. nic->flags |= multicast_all;
  1398. else
  1399. nic->flags &= ~multicast_all;
  1400. e100_exec_cb(nic, NULL, e100_configure);
  1401. e100_exec_cb(nic, NULL, e100_multi);
  1402. }
  1403. static void e100_update_stats(struct nic *nic)
  1404. {
  1405. struct net_device *dev = nic->netdev;
  1406. struct net_device_stats *ns = &dev->stats;
  1407. struct stats *s = &nic->mem->stats;
  1408. __le32 *complete = (nic->mac < mac_82558_D101_A4) ? &s->fc_xmt_pause :
  1409. (nic->mac < mac_82559_D101M) ? (__le32 *)&s->xmt_tco_frames :
  1410. &s->complete;
  1411. /* Device's stats reporting may take several microseconds to
  1412. * complete, so we're always waiting for results of the
  1413. * previous command. */
  1414. if (*complete == cpu_to_le32(cuc_dump_reset_complete)) {
  1415. *complete = 0;
  1416. nic->tx_frames = le32_to_cpu(s->tx_good_frames);
  1417. nic->tx_collisions = le32_to_cpu(s->tx_total_collisions);
  1418. ns->tx_aborted_errors += le32_to_cpu(s->tx_max_collisions);
  1419. ns->tx_window_errors += le32_to_cpu(s->tx_late_collisions);
  1420. ns->tx_carrier_errors += le32_to_cpu(s->tx_lost_crs);
  1421. ns->tx_fifo_errors += le32_to_cpu(s->tx_underruns);
  1422. ns->collisions += nic->tx_collisions;
  1423. ns->tx_errors += le32_to_cpu(s->tx_max_collisions) +
  1424. le32_to_cpu(s->tx_lost_crs);
  1425. nic->rx_short_frame_errors +=
  1426. le32_to_cpu(s->rx_short_frame_errors);
  1427. ns->rx_length_errors = nic->rx_short_frame_errors +
  1428. nic->rx_over_length_errors;
  1429. ns->rx_crc_errors += le32_to_cpu(s->rx_crc_errors);
  1430. ns->rx_frame_errors += le32_to_cpu(s->rx_alignment_errors);
  1431. ns->rx_over_errors += le32_to_cpu(s->rx_overrun_errors);
  1432. ns->rx_fifo_errors += le32_to_cpu(s->rx_overrun_errors);
  1433. ns->rx_missed_errors += le32_to_cpu(s->rx_resource_errors);
  1434. ns->rx_errors += le32_to_cpu(s->rx_crc_errors) +
  1435. le32_to_cpu(s->rx_alignment_errors) +
  1436. le32_to_cpu(s->rx_short_frame_errors) +
  1437. le32_to_cpu(s->rx_cdt_errors);
  1438. nic->tx_deferred += le32_to_cpu(s->tx_deferred);
  1439. nic->tx_single_collisions +=
  1440. le32_to_cpu(s->tx_single_collisions);
  1441. nic->tx_multiple_collisions +=
  1442. le32_to_cpu(s->tx_multiple_collisions);
  1443. if (nic->mac >= mac_82558_D101_A4) {
  1444. nic->tx_fc_pause += le32_to_cpu(s->fc_xmt_pause);
  1445. nic->rx_fc_pause += le32_to_cpu(s->fc_rcv_pause);
  1446. nic->rx_fc_unsupported +=
  1447. le32_to_cpu(s->fc_rcv_unsupported);
  1448. if (nic->mac >= mac_82559_D101M) {
  1449. nic->tx_tco_frames +=
  1450. le16_to_cpu(s->xmt_tco_frames);
  1451. nic->rx_tco_frames +=
  1452. le16_to_cpu(s->rcv_tco_frames);
  1453. }
  1454. }
  1455. }
  1456. if (e100_exec_cmd(nic, cuc_dump_reset, 0))
  1457. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1458. "exec cuc_dump_reset failed\n");
  1459. }
  1460. static void e100_adjust_adaptive_ifs(struct nic *nic, int speed, int duplex)
  1461. {
  1462. /* Adjust inter-frame-spacing (IFS) between two transmits if
  1463. * we're getting collisions on a half-duplex connection. */
  1464. if (duplex == DUPLEX_HALF) {
  1465. u32 prev = nic->adaptive_ifs;
  1466. u32 min_frames = (speed == SPEED_100) ? 1000 : 100;
  1467. if ((nic->tx_frames / 32 < nic->tx_collisions) &&
  1468. (nic->tx_frames > min_frames)) {
  1469. if (nic->adaptive_ifs < 60)
  1470. nic->adaptive_ifs += 5;
  1471. } else if (nic->tx_frames < min_frames) {
  1472. if (nic->adaptive_ifs >= 5)
  1473. nic->adaptive_ifs -= 5;
  1474. }
  1475. if (nic->adaptive_ifs != prev)
  1476. e100_exec_cb(nic, NULL, e100_configure);
  1477. }
  1478. }
  1479. static void e100_watchdog(struct timer_list *t)
  1480. {
  1481. struct nic *nic = from_timer(nic, t, watchdog);
  1482. struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
  1483. u32 speed;
  1484. netif_printk(nic, timer, KERN_DEBUG, nic->netdev,
  1485. "right now = %ld\n", jiffies);
  1486. /* mii library handles link maintenance tasks */
  1487. mii_ethtool_gset(&nic->mii, &cmd);
  1488. speed = ethtool_cmd_speed(&cmd);
  1489. if (mii_link_ok(&nic->mii) && !netif_carrier_ok(nic->netdev)) {
  1490. netdev_info(nic->netdev, "NIC Link is Up %u Mbps %s Duplex\n",
  1491. speed == SPEED_100 ? 100 : 10,
  1492. cmd.duplex == DUPLEX_FULL ? "Full" : "Half");
  1493. } else if (!mii_link_ok(&nic->mii) && netif_carrier_ok(nic->netdev)) {
  1494. netdev_info(nic->netdev, "NIC Link is Down\n");
  1495. }
  1496. mii_check_link(&nic->mii);
  1497. /* Software generated interrupt to recover from (rare) Rx
  1498. * allocation failure.
  1499. * Unfortunately have to use a spinlock to not re-enable interrupts
  1500. * accidentally, due to hardware that shares a register between the
  1501. * interrupt mask bit and the SW Interrupt generation bit */
  1502. spin_lock_irq(&nic->cmd_lock);
  1503. iowrite8(ioread8(&nic->csr->scb.cmd_hi) | irq_sw_gen,&nic->csr->scb.cmd_hi);
  1504. e100_write_flush(nic);
  1505. spin_unlock_irq(&nic->cmd_lock);
  1506. e100_update_stats(nic);
  1507. e100_adjust_adaptive_ifs(nic, speed, cmd.duplex);
  1508. if (nic->mac <= mac_82557_D100_C)
  1509. /* Issue a multicast command to workaround a 557 lock up */
  1510. e100_set_multicast_list(nic->netdev);
  1511. if (nic->flags & ich && speed == SPEED_10 && cmd.duplex == DUPLEX_HALF)
  1512. /* Need SW workaround for ICH[x] 10Mbps/half duplex Tx hang. */
  1513. nic->flags |= ich_10h_workaround;
  1514. else
  1515. nic->flags &= ~ich_10h_workaround;
  1516. mod_timer(&nic->watchdog,
  1517. round_jiffies(jiffies + E100_WATCHDOG_PERIOD));
  1518. }
  1519. static int e100_xmit_prepare(struct nic *nic, struct cb *cb,
  1520. struct sk_buff *skb)
  1521. {
  1522. dma_addr_t dma_addr;
  1523. cb->command = nic->tx_command;
  1524. dma_addr = dma_map_single(&nic->pdev->dev, skb->data, skb->len,
  1525. DMA_TO_DEVICE);
  1526. /* If we can't map the skb, have the upper layer try later */
  1527. if (dma_mapping_error(&nic->pdev->dev, dma_addr))
  1528. return -ENOMEM;
  1529. /*
  1530. * Use the last 4 bytes of the SKB payload packet as the CRC, used for
  1531. * testing, ie sending frames with bad CRC.
  1532. */
  1533. if (unlikely(skb->no_fcs))
  1534. cb->command |= cpu_to_le16(cb_tx_nc);
  1535. else
  1536. cb->command &= ~cpu_to_le16(cb_tx_nc);
  1537. /* interrupt every 16 packets regardless of delay */
  1538. if ((nic->cbs_avail & ~15) == nic->cbs_avail)
  1539. cb->command |= cpu_to_le16(cb_i);
  1540. cb->u.tcb.tbd_array = cb->dma_addr + offsetof(struct cb, u.tcb.tbd);
  1541. cb->u.tcb.tcb_byte_count = 0;
  1542. cb->u.tcb.threshold = nic->tx_threshold;
  1543. cb->u.tcb.tbd_count = 1;
  1544. cb->u.tcb.tbd.buf_addr = cpu_to_le32(dma_addr);
  1545. cb->u.tcb.tbd.size = cpu_to_le16(skb->len);
  1546. skb_tx_timestamp(skb);
  1547. return 0;
  1548. }
  1549. static netdev_tx_t e100_xmit_frame(struct sk_buff *skb,
  1550. struct net_device *netdev)
  1551. {
  1552. struct nic *nic = netdev_priv(netdev);
  1553. int err;
  1554. if (nic->flags & ich_10h_workaround) {
  1555. /* SW workaround for ICH[x] 10Mbps/half duplex Tx hang.
  1556. Issue a NOP command followed by a 1us delay before
  1557. issuing the Tx command. */
  1558. if (e100_exec_cmd(nic, cuc_nop, 0))
  1559. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1560. "exec cuc_nop failed\n");
  1561. udelay(1);
  1562. }
  1563. err = e100_exec_cb(nic, skb, e100_xmit_prepare);
  1564. switch (err) {
  1565. case -ENOSPC:
  1566. /* We queued the skb, but now we're out of space. */
  1567. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1568. "No space for CB\n");
  1569. netif_stop_queue(netdev);
  1570. break;
  1571. case -ENOMEM:
  1572. /* This is a hard error - log it. */
  1573. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1574. "Out of Tx resources, returning skb\n");
  1575. netif_stop_queue(netdev);
  1576. return NETDEV_TX_BUSY;
  1577. }
  1578. return NETDEV_TX_OK;
  1579. }
  1580. static int e100_tx_clean(struct nic *nic)
  1581. {
  1582. struct net_device *dev = nic->netdev;
  1583. struct cb *cb;
  1584. int tx_cleaned = 0;
  1585. spin_lock(&nic->cb_lock);
  1586. /* Clean CBs marked complete */
  1587. for (cb = nic->cb_to_clean;
  1588. cb->status & cpu_to_le16(cb_complete);
  1589. cb = nic->cb_to_clean = cb->next) {
  1590. dma_rmb(); /* read skb after status */
  1591. netif_printk(nic, tx_done, KERN_DEBUG, nic->netdev,
  1592. "cb[%d]->status = 0x%04X\n",
  1593. (int)(((void*)cb - (void*)nic->cbs)/sizeof(struct cb)),
  1594. cb->status);
  1595. if (likely(cb->skb != NULL)) {
  1596. dev->stats.tx_packets++;
  1597. dev->stats.tx_bytes += cb->skb->len;
  1598. dma_unmap_single(&nic->pdev->dev,
  1599. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1600. le16_to_cpu(cb->u.tcb.tbd.size),
  1601. DMA_TO_DEVICE);
  1602. dev_kfree_skb_any(cb->skb);
  1603. cb->skb = NULL;
  1604. tx_cleaned = 1;
  1605. }
  1606. cb->status = 0;
  1607. nic->cbs_avail++;
  1608. }
  1609. spin_unlock(&nic->cb_lock);
  1610. /* Recover from running out of Tx resources in xmit_frame */
  1611. if (unlikely(tx_cleaned && netif_queue_stopped(nic->netdev)))
  1612. netif_wake_queue(nic->netdev);
  1613. return tx_cleaned;
  1614. }
  1615. static void e100_clean_cbs(struct nic *nic)
  1616. {
  1617. if (nic->cbs) {
  1618. while (nic->cbs_avail != nic->params.cbs.count) {
  1619. struct cb *cb = nic->cb_to_clean;
  1620. if (cb->skb) {
  1621. dma_unmap_single(&nic->pdev->dev,
  1622. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1623. le16_to_cpu(cb->u.tcb.tbd.size),
  1624. DMA_TO_DEVICE);
  1625. dev_kfree_skb(cb->skb);
  1626. }
  1627. nic->cb_to_clean = nic->cb_to_clean->next;
  1628. nic->cbs_avail++;
  1629. }
  1630. dma_pool_free(nic->cbs_pool, nic->cbs, nic->cbs_dma_addr);
  1631. nic->cbs = NULL;
  1632. nic->cbs_avail = 0;
  1633. }
  1634. nic->cuc_cmd = cuc_start;
  1635. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean =
  1636. nic->cbs;
  1637. }
  1638. static int e100_alloc_cbs(struct nic *nic)
  1639. {
  1640. struct cb *cb;
  1641. unsigned int i, count = nic->params.cbs.count;
  1642. nic->cuc_cmd = cuc_start;
  1643. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = NULL;
  1644. nic->cbs_avail = 0;
  1645. nic->cbs = dma_pool_zalloc(nic->cbs_pool, GFP_KERNEL,
  1646. &nic->cbs_dma_addr);
  1647. if (!nic->cbs)
  1648. return -ENOMEM;
  1649. for (cb = nic->cbs, i = 0; i < count; cb++, i++) {
  1650. cb->next = (i + 1 < count) ? cb + 1 : nic->cbs;
  1651. cb->prev = (i == 0) ? nic->cbs + count - 1 : cb - 1;
  1652. cb->dma_addr = nic->cbs_dma_addr + i * sizeof(struct cb);
  1653. cb->link = cpu_to_le32(nic->cbs_dma_addr +
  1654. ((i+1) % count) * sizeof(struct cb));
  1655. }
  1656. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = nic->cbs;
  1657. nic->cbs_avail = count;
  1658. return 0;
  1659. }
  1660. static inline void e100_start_receiver(struct nic *nic, struct rx *rx)
  1661. {
  1662. if (!nic->rxs) return;
  1663. if (RU_SUSPENDED != nic->ru_running) return;
  1664. /* handle init time starts */
  1665. if (!rx) rx = nic->rxs;
  1666. /* (Re)start RU if suspended or idle and RFA is non-NULL */
  1667. if (rx->skb) {
  1668. e100_exec_cmd(nic, ruc_start, rx->dma_addr);
  1669. nic->ru_running = RU_RUNNING;
  1670. }
  1671. }
  1672. #define RFD_BUF_LEN (sizeof(struct rfd) + VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
  1673. static int e100_rx_alloc_skb(struct nic *nic, struct rx *rx)
  1674. {
  1675. if (!(rx->skb = netdev_alloc_skb_ip_align(nic->netdev, RFD_BUF_LEN)))
  1676. return -ENOMEM;
  1677. /* Init, and map the RFD. */
  1678. skb_copy_to_linear_data(rx->skb, &nic->blank_rfd, sizeof(struct rfd));
  1679. rx->dma_addr = dma_map_single(&nic->pdev->dev, rx->skb->data,
  1680. RFD_BUF_LEN, DMA_BIDIRECTIONAL);
  1681. if (dma_mapping_error(&nic->pdev->dev, rx->dma_addr)) {
  1682. dev_kfree_skb_any(rx->skb);
  1683. rx->skb = NULL;
  1684. rx->dma_addr = 0;
  1685. return -ENOMEM;
  1686. }
  1687. /* Link the RFD to end of RFA by linking previous RFD to
  1688. * this one. We are safe to touch the previous RFD because
  1689. * it is protected by the before last buffer's el bit being set */
  1690. if (rx->prev->skb) {
  1691. struct rfd *prev_rfd = (struct rfd *)rx->prev->skb->data;
  1692. put_unaligned_le32(rx->dma_addr, &prev_rfd->link);
  1693. dma_sync_single_for_device(&nic->pdev->dev,
  1694. rx->prev->dma_addr,
  1695. sizeof(struct rfd),
  1696. DMA_BIDIRECTIONAL);
  1697. }
  1698. return 0;
  1699. }
  1700. static int e100_rx_indicate(struct nic *nic, struct rx *rx,
  1701. unsigned int *work_done, unsigned int work_to_do)
  1702. {
  1703. struct net_device *dev = nic->netdev;
  1704. struct sk_buff *skb = rx->skb;
  1705. struct rfd *rfd = (struct rfd *)skb->data;
  1706. u16 rfd_status, actual_size;
  1707. u16 fcs_pad = 0;
  1708. if (unlikely(work_done && *work_done >= work_to_do))
  1709. return -EAGAIN;
  1710. /* Need to sync before taking a peek at cb_complete bit */
  1711. dma_sync_single_for_cpu(&nic->pdev->dev, rx->dma_addr,
  1712. sizeof(struct rfd), DMA_BIDIRECTIONAL);
  1713. rfd_status = le16_to_cpu(rfd->status);
  1714. netif_printk(nic, rx_status, KERN_DEBUG, nic->netdev,
  1715. "status=0x%04X\n", rfd_status);
  1716. dma_rmb(); /* read size after status bit */
  1717. /* If data isn't ready, nothing to indicate */
  1718. if (unlikely(!(rfd_status & cb_complete))) {
  1719. /* If the next buffer has the el bit, but we think the receiver
  1720. * is still running, check to see if it really stopped while
  1721. * we had interrupts off.
  1722. * This allows for a fast restart without re-enabling
  1723. * interrupts */
  1724. if ((le16_to_cpu(rfd->command) & cb_el) &&
  1725. (RU_RUNNING == nic->ru_running))
  1726. if (ioread8(&nic->csr->scb.status) & rus_no_res)
  1727. nic->ru_running = RU_SUSPENDED;
  1728. dma_sync_single_for_device(&nic->pdev->dev, rx->dma_addr,
  1729. sizeof(struct rfd),
  1730. DMA_FROM_DEVICE);
  1731. return -ENODATA;
  1732. }
  1733. /* Get actual data size */
  1734. if (unlikely(dev->features & NETIF_F_RXFCS))
  1735. fcs_pad = 4;
  1736. actual_size = le16_to_cpu(rfd->actual_size) & 0x3FFF;
  1737. if (unlikely(actual_size > RFD_BUF_LEN - sizeof(struct rfd)))
  1738. actual_size = RFD_BUF_LEN - sizeof(struct rfd);
  1739. /* Get data */
  1740. dma_unmap_single(&nic->pdev->dev, rx->dma_addr, RFD_BUF_LEN,
  1741. DMA_BIDIRECTIONAL);
  1742. /* If this buffer has the el bit, but we think the receiver
  1743. * is still running, check to see if it really stopped while
  1744. * we had interrupts off.
  1745. * This allows for a fast restart without re-enabling interrupts.
  1746. * This can happen when the RU sees the size change but also sees
  1747. * the el bit set. */
  1748. if ((le16_to_cpu(rfd->command) & cb_el) &&
  1749. (RU_RUNNING == nic->ru_running)) {
  1750. if (ioread8(&nic->csr->scb.status) & rus_no_res)
  1751. nic->ru_running = RU_SUSPENDED;
  1752. }
  1753. /* Pull off the RFD and put the actual data (minus eth hdr) */
  1754. skb_reserve(skb, sizeof(struct rfd));
  1755. skb_put(skb, actual_size);
  1756. skb->protocol = eth_type_trans(skb, nic->netdev);
  1757. /* If we are receiving all frames, then don't bother
  1758. * checking for errors.
  1759. */
  1760. if (unlikely(dev->features & NETIF_F_RXALL)) {
  1761. if (actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN + fcs_pad)
  1762. /* Received oversized frame, but keep it. */
  1763. nic->rx_over_length_errors++;
  1764. goto process_skb;
  1765. }
  1766. if (unlikely(!(rfd_status & cb_ok))) {
  1767. /* Don't indicate if hardware indicates errors */
  1768. dev_kfree_skb_any(skb);
  1769. } else if (actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN + fcs_pad) {
  1770. /* Don't indicate oversized frames */
  1771. nic->rx_over_length_errors++;
  1772. dev_kfree_skb_any(skb);
  1773. } else {
  1774. process_skb:
  1775. dev->stats.rx_packets++;
  1776. dev->stats.rx_bytes += (actual_size - fcs_pad);
  1777. netif_receive_skb(skb);
  1778. if (work_done)
  1779. (*work_done)++;
  1780. }
  1781. rx->skb = NULL;
  1782. return 0;
  1783. }
  1784. static void e100_rx_clean(struct nic *nic, unsigned int *work_done,
  1785. unsigned int work_to_do)
  1786. {
  1787. struct rx *rx;
  1788. int restart_required = 0, err = 0;
  1789. struct rx *old_before_last_rx, *new_before_last_rx;
  1790. struct rfd *old_before_last_rfd, *new_before_last_rfd;
  1791. /* Indicate newly arrived packets */
  1792. for (rx = nic->rx_to_clean; rx->skb; rx = nic->rx_to_clean = rx->next) {
  1793. err = e100_rx_indicate(nic, rx, work_done, work_to_do);
  1794. /* Hit quota or no more to clean */
  1795. if (-EAGAIN == err || -ENODATA == err)
  1796. break;
  1797. }
  1798. /* On EAGAIN, hit quota so have more work to do, restart once
  1799. * cleanup is complete.
  1800. * Else, are we already rnr? then pay attention!!! this ensures that
  1801. * the state machine progression never allows a start with a
  1802. * partially cleaned list, avoiding a race between hardware
  1803. * and rx_to_clean when in NAPI mode */
  1804. if (-EAGAIN != err && RU_SUSPENDED == nic->ru_running)
  1805. restart_required = 1;
  1806. old_before_last_rx = nic->rx_to_use->prev->prev;
  1807. old_before_last_rfd = (struct rfd *)old_before_last_rx->skb->data;
  1808. /* Alloc new skbs to refill list */
  1809. for (rx = nic->rx_to_use; !rx->skb; rx = nic->rx_to_use = rx->next) {
  1810. if (unlikely(e100_rx_alloc_skb(nic, rx)))
  1811. break; /* Better luck next time (see watchdog) */
  1812. }
  1813. new_before_last_rx = nic->rx_to_use->prev->prev;
  1814. if (new_before_last_rx != old_before_last_rx) {
  1815. /* Set the el-bit on the buffer that is before the last buffer.
  1816. * This lets us update the next pointer on the last buffer
  1817. * without worrying about hardware touching it.
  1818. * We set the size to 0 to prevent hardware from touching this
  1819. * buffer.
  1820. * When the hardware hits the before last buffer with el-bit
  1821. * and size of 0, it will RNR interrupt, the RUS will go into
  1822. * the No Resources state. It will not complete nor write to
  1823. * this buffer. */
  1824. new_before_last_rfd =
  1825. (struct rfd *)new_before_last_rx->skb->data;
  1826. new_before_last_rfd->size = 0;
  1827. new_before_last_rfd->command |= cpu_to_le16(cb_el);
  1828. dma_sync_single_for_device(&nic->pdev->dev,
  1829. new_before_last_rx->dma_addr,
  1830. sizeof(struct rfd),
  1831. DMA_BIDIRECTIONAL);
  1832. /* Now that we have a new stopping point, we can clear the old
  1833. * stopping point. We must sync twice to get the proper
  1834. * ordering on the hardware side of things. */
  1835. old_before_last_rfd->command &= ~cpu_to_le16(cb_el);
  1836. dma_sync_single_for_device(&nic->pdev->dev,
  1837. old_before_last_rx->dma_addr,
  1838. sizeof(struct rfd),
  1839. DMA_BIDIRECTIONAL);
  1840. old_before_last_rfd->size = cpu_to_le16(VLAN_ETH_FRAME_LEN
  1841. + ETH_FCS_LEN);
  1842. dma_sync_single_for_device(&nic->pdev->dev,
  1843. old_before_last_rx->dma_addr,
  1844. sizeof(struct rfd),
  1845. DMA_BIDIRECTIONAL);
  1846. }
  1847. if (restart_required) {
  1848. // ack the rnr?
  1849. iowrite8(stat_ack_rnr, &nic->csr->scb.stat_ack);
  1850. e100_start_receiver(nic, nic->rx_to_clean);
  1851. if (work_done)
  1852. (*work_done)++;
  1853. }
  1854. }
  1855. static void e100_rx_clean_list(struct nic *nic)
  1856. {
  1857. struct rx *rx;
  1858. unsigned int i, count = nic->params.rfds.count;
  1859. nic->ru_running = RU_UNINITIALIZED;
  1860. if (nic->rxs) {
  1861. for (rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1862. if (rx->skb) {
  1863. dma_unmap_single(&nic->pdev->dev,
  1864. rx->dma_addr, RFD_BUF_LEN,
  1865. DMA_BIDIRECTIONAL);
  1866. dev_kfree_skb(rx->skb);
  1867. }
  1868. }
  1869. kfree(nic->rxs);
  1870. nic->rxs = NULL;
  1871. }
  1872. nic->rx_to_use = nic->rx_to_clean = NULL;
  1873. }
  1874. static int e100_rx_alloc_list(struct nic *nic)
  1875. {
  1876. struct rx *rx;
  1877. unsigned int i, count = nic->params.rfds.count;
  1878. struct rfd *before_last;
  1879. nic->rx_to_use = nic->rx_to_clean = NULL;
  1880. nic->ru_running = RU_UNINITIALIZED;
  1881. if (!(nic->rxs = kcalloc(count, sizeof(struct rx), GFP_KERNEL)))
  1882. return -ENOMEM;
  1883. for (rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1884. rx->next = (i + 1 < count) ? rx + 1 : nic->rxs;
  1885. rx->prev = (i == 0) ? nic->rxs + count - 1 : rx - 1;
  1886. if (e100_rx_alloc_skb(nic, rx)) {
  1887. e100_rx_clean_list(nic);
  1888. return -ENOMEM;
  1889. }
  1890. }
  1891. /* Set the el-bit on the buffer that is before the last buffer.
  1892. * This lets us update the next pointer on the last buffer without
  1893. * worrying about hardware touching it.
  1894. * We set the size to 0 to prevent hardware from touching this buffer.
  1895. * When the hardware hits the before last buffer with el-bit and size
  1896. * of 0, it will RNR interrupt, the RU will go into the No Resources
  1897. * state. It will not complete nor write to this buffer. */
  1898. rx = nic->rxs->prev->prev;
  1899. before_last = (struct rfd *)rx->skb->data;
  1900. before_last->command |= cpu_to_le16(cb_el);
  1901. before_last->size = 0;
  1902. dma_sync_single_for_device(&nic->pdev->dev, rx->dma_addr,
  1903. sizeof(struct rfd), DMA_BIDIRECTIONAL);
  1904. nic->rx_to_use = nic->rx_to_clean = nic->rxs;
  1905. nic->ru_running = RU_SUSPENDED;
  1906. return 0;
  1907. }
  1908. static irqreturn_t e100_intr(int irq, void *dev_id)
  1909. {
  1910. struct net_device *netdev = dev_id;
  1911. struct nic *nic = netdev_priv(netdev);
  1912. u8 stat_ack = ioread8(&nic->csr->scb.stat_ack);
  1913. netif_printk(nic, intr, KERN_DEBUG, nic->netdev,
  1914. "stat_ack = 0x%02X\n", stat_ack);
  1915. if (stat_ack == stat_ack_not_ours || /* Not our interrupt */
  1916. stat_ack == stat_ack_not_present) /* Hardware is ejected */
  1917. return IRQ_NONE;
  1918. /* Ack interrupt(s) */
  1919. iowrite8(stat_ack, &nic->csr->scb.stat_ack);
  1920. /* We hit Receive No Resource (RNR); restart RU after cleaning */
  1921. if (stat_ack & stat_ack_rnr)
  1922. nic->ru_running = RU_SUSPENDED;
  1923. if (likely(napi_schedule_prep(&nic->napi))) {
  1924. e100_disable_irq(nic);
  1925. __napi_schedule(&nic->napi);
  1926. }
  1927. return IRQ_HANDLED;
  1928. }
  1929. static int e100_poll(struct napi_struct *napi, int budget)
  1930. {
  1931. struct nic *nic = container_of(napi, struct nic, napi);
  1932. unsigned int work_done = 0;
  1933. e100_rx_clean(nic, &work_done, budget);
  1934. e100_tx_clean(nic);
  1935. /* If budget fully consumed, continue polling */
  1936. if (work_done == budget)
  1937. return budget;
  1938. /* only re-enable interrupt if stack agrees polling is really done */
  1939. if (likely(napi_complete_done(napi, work_done)))
  1940. e100_enable_irq(nic);
  1941. return work_done;
  1942. }
  1943. #ifdef CONFIG_NET_POLL_CONTROLLER
  1944. static void e100_netpoll(struct net_device *netdev)
  1945. {
  1946. struct nic *nic = netdev_priv(netdev);
  1947. e100_disable_irq(nic);
  1948. e100_intr(nic->pdev->irq, netdev);
  1949. e100_tx_clean(nic);
  1950. e100_enable_irq(nic);
  1951. }
  1952. #endif
  1953. static int e100_set_mac_address(struct net_device *netdev, void *p)
  1954. {
  1955. struct nic *nic = netdev_priv(netdev);
  1956. struct sockaddr *addr = p;
  1957. if (!is_valid_ether_addr(addr->sa_data))
  1958. return -EADDRNOTAVAIL;
  1959. eth_hw_addr_set(netdev, addr->sa_data);
  1960. e100_exec_cb(nic, NULL, e100_setup_iaaddr);
  1961. return 0;
  1962. }
  1963. static int e100_asf(struct nic *nic)
  1964. {
  1965. /* ASF can be enabled from eeprom */
  1966. return (nic->pdev->device >= 0x1050) && (nic->pdev->device <= 0x1057) &&
  1967. (le16_to_cpu(nic->eeprom[eeprom_config_asf]) & eeprom_asf) &&
  1968. !(le16_to_cpu(nic->eeprom[eeprom_config_asf]) & eeprom_gcl) &&
  1969. ((le16_to_cpu(nic->eeprom[eeprom_smbus_addr]) & 0xFF) != 0xFE);
  1970. }
  1971. static int e100_up(struct nic *nic)
  1972. {
  1973. int err;
  1974. if ((err = e100_rx_alloc_list(nic)))
  1975. return err;
  1976. if ((err = e100_alloc_cbs(nic)))
  1977. goto err_rx_clean_list;
  1978. if ((err = e100_hw_init(nic)))
  1979. goto err_clean_cbs;
  1980. e100_set_multicast_list(nic->netdev);
  1981. e100_start_receiver(nic, NULL);
  1982. mod_timer(&nic->watchdog, jiffies);
  1983. if ((err = request_irq(nic->pdev->irq, e100_intr, IRQF_SHARED,
  1984. nic->netdev->name, nic->netdev)))
  1985. goto err_no_irq;
  1986. netif_wake_queue(nic->netdev);
  1987. napi_enable(&nic->napi);
  1988. /* enable ints _after_ enabling poll, preventing a race between
  1989. * disable ints+schedule */
  1990. e100_enable_irq(nic);
  1991. return 0;
  1992. err_no_irq:
  1993. del_timer_sync(&nic->watchdog);
  1994. err_clean_cbs:
  1995. e100_clean_cbs(nic);
  1996. err_rx_clean_list:
  1997. e100_rx_clean_list(nic);
  1998. return err;
  1999. }
  2000. static void e100_down(struct nic *nic)
  2001. {
  2002. /* wait here for poll to complete */
  2003. napi_disable(&nic->napi);
  2004. netif_stop_queue(nic->netdev);
  2005. e100_hw_reset(nic);
  2006. free_irq(nic->pdev->irq, nic->netdev);
  2007. del_timer_sync(&nic->watchdog);
  2008. netif_carrier_off(nic->netdev);
  2009. e100_clean_cbs(nic);
  2010. e100_rx_clean_list(nic);
  2011. }
  2012. static void e100_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  2013. {
  2014. struct nic *nic = netdev_priv(netdev);
  2015. /* Reset outside of interrupt context, to avoid request_irq
  2016. * in interrupt context */
  2017. schedule_work(&nic->tx_timeout_task);
  2018. }
  2019. static void e100_tx_timeout_task(struct work_struct *work)
  2020. {
  2021. struct nic *nic = container_of(work, struct nic, tx_timeout_task);
  2022. struct net_device *netdev = nic->netdev;
  2023. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  2024. "scb.status=0x%02X\n", ioread8(&nic->csr->scb.status));
  2025. rtnl_lock();
  2026. if (netif_running(netdev)) {
  2027. e100_down(netdev_priv(netdev));
  2028. e100_up(netdev_priv(netdev));
  2029. }
  2030. rtnl_unlock();
  2031. }
  2032. static int e100_loopback_test(struct nic *nic, enum loopback loopback_mode)
  2033. {
  2034. int err;
  2035. struct sk_buff *skb;
  2036. /* Use driver resources to perform internal MAC or PHY
  2037. * loopback test. A single packet is prepared and transmitted
  2038. * in loopback mode, and the test passes if the received
  2039. * packet compares byte-for-byte to the transmitted packet. */
  2040. if ((err = e100_rx_alloc_list(nic)))
  2041. return err;
  2042. if ((err = e100_alloc_cbs(nic)))
  2043. goto err_clean_rx;
  2044. /* ICH PHY loopback is broken so do MAC loopback instead */
  2045. if (nic->flags & ich && loopback_mode == lb_phy)
  2046. loopback_mode = lb_mac;
  2047. nic->loopback = loopback_mode;
  2048. if ((err = e100_hw_init(nic)))
  2049. goto err_loopback_none;
  2050. if (loopback_mode == lb_phy)
  2051. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR,
  2052. BMCR_LOOPBACK);
  2053. e100_start_receiver(nic, NULL);
  2054. if (!(skb = netdev_alloc_skb(nic->netdev, ETH_DATA_LEN))) {
  2055. err = -ENOMEM;
  2056. goto err_loopback_none;
  2057. }
  2058. skb_put(skb, ETH_DATA_LEN);
  2059. memset(skb->data, 0xFF, ETH_DATA_LEN);
  2060. e100_xmit_frame(skb, nic->netdev);
  2061. msleep(10);
  2062. dma_sync_single_for_cpu(&nic->pdev->dev, nic->rx_to_clean->dma_addr,
  2063. RFD_BUF_LEN, DMA_BIDIRECTIONAL);
  2064. if (memcmp(nic->rx_to_clean->skb->data + sizeof(struct rfd),
  2065. skb->data, ETH_DATA_LEN))
  2066. err = -EAGAIN;
  2067. err_loopback_none:
  2068. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR, 0);
  2069. nic->loopback = lb_none;
  2070. e100_clean_cbs(nic);
  2071. e100_hw_reset(nic);
  2072. err_clean_rx:
  2073. e100_rx_clean_list(nic);
  2074. return err;
  2075. }
  2076. #define MII_LED_CONTROL 0x1B
  2077. #define E100_82552_LED_OVERRIDE 0x19
  2078. #define E100_82552_LED_ON 0x000F /* LEDTX and LED_RX both on */
  2079. #define E100_82552_LED_OFF 0x000A /* LEDTX and LED_RX both off */
  2080. static int e100_get_link_ksettings(struct net_device *netdev,
  2081. struct ethtool_link_ksettings *cmd)
  2082. {
  2083. struct nic *nic = netdev_priv(netdev);
  2084. mii_ethtool_get_link_ksettings(&nic->mii, cmd);
  2085. return 0;
  2086. }
  2087. static int e100_set_link_ksettings(struct net_device *netdev,
  2088. const struct ethtool_link_ksettings *cmd)
  2089. {
  2090. struct nic *nic = netdev_priv(netdev);
  2091. int err;
  2092. mdio_write(netdev, nic->mii.phy_id, MII_BMCR, BMCR_RESET);
  2093. err = mii_ethtool_set_link_ksettings(&nic->mii, cmd);
  2094. e100_exec_cb(nic, NULL, e100_configure);
  2095. return err;
  2096. }
  2097. static void e100_get_drvinfo(struct net_device *netdev,
  2098. struct ethtool_drvinfo *info)
  2099. {
  2100. struct nic *nic = netdev_priv(netdev);
  2101. strscpy(info->driver, DRV_NAME, sizeof(info->driver));
  2102. strscpy(info->bus_info, pci_name(nic->pdev),
  2103. sizeof(info->bus_info));
  2104. }
  2105. #define E100_PHY_REGS 0x1D
  2106. static int e100_get_regs_len(struct net_device *netdev)
  2107. {
  2108. struct nic *nic = netdev_priv(netdev);
  2109. /* We know the number of registers, and the size of the dump buffer.
  2110. * Calculate the total size in bytes.
  2111. */
  2112. return (1 + E100_PHY_REGS) * sizeof(u32) + sizeof(nic->mem->dump_buf);
  2113. }
  2114. static void e100_get_regs(struct net_device *netdev,
  2115. struct ethtool_regs *regs, void *p)
  2116. {
  2117. struct nic *nic = netdev_priv(netdev);
  2118. u32 *buff = p;
  2119. int i;
  2120. regs->version = (1 << 24) | nic->pdev->revision;
  2121. buff[0] = ioread8(&nic->csr->scb.cmd_hi) << 24 |
  2122. ioread8(&nic->csr->scb.cmd_lo) << 16 |
  2123. ioread16(&nic->csr->scb.status);
  2124. for (i = 0; i < E100_PHY_REGS; i++)
  2125. /* Note that we read the registers in reverse order. This
  2126. * ordering is the ABI apparently used by ethtool and other
  2127. * applications.
  2128. */
  2129. buff[1 + i] = mdio_read(netdev, nic->mii.phy_id,
  2130. E100_PHY_REGS - 1 - i);
  2131. memset(nic->mem->dump_buf, 0, sizeof(nic->mem->dump_buf));
  2132. e100_exec_cb(nic, NULL, e100_dump);
  2133. msleep(10);
  2134. memcpy(&buff[1 + E100_PHY_REGS], nic->mem->dump_buf,
  2135. sizeof(nic->mem->dump_buf));
  2136. }
  2137. static void e100_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2138. {
  2139. struct nic *nic = netdev_priv(netdev);
  2140. wol->supported = (nic->mac >= mac_82558_D101_A4) ? WAKE_MAGIC : 0;
  2141. wol->wolopts = (nic->flags & wol_magic) ? WAKE_MAGIC : 0;
  2142. }
  2143. static int e100_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2144. {
  2145. struct nic *nic = netdev_priv(netdev);
  2146. if ((wol->wolopts && wol->wolopts != WAKE_MAGIC) ||
  2147. !device_can_wakeup(&nic->pdev->dev))
  2148. return -EOPNOTSUPP;
  2149. if (wol->wolopts)
  2150. nic->flags |= wol_magic;
  2151. else
  2152. nic->flags &= ~wol_magic;
  2153. device_set_wakeup_enable(&nic->pdev->dev, wol->wolopts);
  2154. e100_exec_cb(nic, NULL, e100_configure);
  2155. return 0;
  2156. }
  2157. static u32 e100_get_msglevel(struct net_device *netdev)
  2158. {
  2159. struct nic *nic = netdev_priv(netdev);
  2160. return nic->msg_enable;
  2161. }
  2162. static void e100_set_msglevel(struct net_device *netdev, u32 value)
  2163. {
  2164. struct nic *nic = netdev_priv(netdev);
  2165. nic->msg_enable = value;
  2166. }
  2167. static int e100_nway_reset(struct net_device *netdev)
  2168. {
  2169. struct nic *nic = netdev_priv(netdev);
  2170. return mii_nway_restart(&nic->mii);
  2171. }
  2172. static u32 e100_get_link(struct net_device *netdev)
  2173. {
  2174. struct nic *nic = netdev_priv(netdev);
  2175. return mii_link_ok(&nic->mii);
  2176. }
  2177. static int e100_get_eeprom_len(struct net_device *netdev)
  2178. {
  2179. struct nic *nic = netdev_priv(netdev);
  2180. return nic->eeprom_wc << 1;
  2181. }
  2182. #define E100_EEPROM_MAGIC 0x1234
  2183. static int e100_get_eeprom(struct net_device *netdev,
  2184. struct ethtool_eeprom *eeprom, u8 *bytes)
  2185. {
  2186. struct nic *nic = netdev_priv(netdev);
  2187. eeprom->magic = E100_EEPROM_MAGIC;
  2188. memcpy(bytes, &((u8 *)nic->eeprom)[eeprom->offset], eeprom->len);
  2189. return 0;
  2190. }
  2191. static int e100_set_eeprom(struct net_device *netdev,
  2192. struct ethtool_eeprom *eeprom, u8 *bytes)
  2193. {
  2194. struct nic *nic = netdev_priv(netdev);
  2195. if (eeprom->magic != E100_EEPROM_MAGIC)
  2196. return -EINVAL;
  2197. memcpy(&((u8 *)nic->eeprom)[eeprom->offset], bytes, eeprom->len);
  2198. return e100_eeprom_save(nic, eeprom->offset >> 1,
  2199. (eeprom->len >> 1) + 1);
  2200. }
  2201. static void e100_get_ringparam(struct net_device *netdev,
  2202. struct ethtool_ringparam *ring,
  2203. struct kernel_ethtool_ringparam *kernel_ring,
  2204. struct netlink_ext_ack *extack)
  2205. {
  2206. struct nic *nic = netdev_priv(netdev);
  2207. struct param_range *rfds = &nic->params.rfds;
  2208. struct param_range *cbs = &nic->params.cbs;
  2209. ring->rx_max_pending = rfds->max;
  2210. ring->tx_max_pending = cbs->max;
  2211. ring->rx_pending = rfds->count;
  2212. ring->tx_pending = cbs->count;
  2213. }
  2214. static int e100_set_ringparam(struct net_device *netdev,
  2215. struct ethtool_ringparam *ring,
  2216. struct kernel_ethtool_ringparam *kernel_ring,
  2217. struct netlink_ext_ack *extack)
  2218. {
  2219. struct nic *nic = netdev_priv(netdev);
  2220. struct param_range *rfds = &nic->params.rfds;
  2221. struct param_range *cbs = &nic->params.cbs;
  2222. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2223. return -EINVAL;
  2224. if (netif_running(netdev))
  2225. e100_down(nic);
  2226. rfds->count = max(ring->rx_pending, rfds->min);
  2227. rfds->count = min(rfds->count, rfds->max);
  2228. cbs->count = max(ring->tx_pending, cbs->min);
  2229. cbs->count = min(cbs->count, cbs->max);
  2230. netif_info(nic, drv, nic->netdev, "Ring Param settings: rx: %d, tx %d\n",
  2231. rfds->count, cbs->count);
  2232. if (netif_running(netdev))
  2233. e100_up(nic);
  2234. return 0;
  2235. }
  2236. static const char e100_gstrings_test[][ETH_GSTRING_LEN] = {
  2237. "Link test (on/offline)",
  2238. "Eeprom test (on/offline)",
  2239. "Self test (offline)",
  2240. "Mac loopback (offline)",
  2241. "Phy loopback (offline)",
  2242. };
  2243. #define E100_TEST_LEN ARRAY_SIZE(e100_gstrings_test)
  2244. static void e100_diag_test(struct net_device *netdev,
  2245. struct ethtool_test *test, u64 *data)
  2246. {
  2247. struct ethtool_cmd cmd;
  2248. struct nic *nic = netdev_priv(netdev);
  2249. int i;
  2250. memset(data, 0, E100_TEST_LEN * sizeof(u64));
  2251. data[0] = !mii_link_ok(&nic->mii);
  2252. data[1] = e100_eeprom_load(nic);
  2253. if (test->flags & ETH_TEST_FL_OFFLINE) {
  2254. /* save speed, duplex & autoneg settings */
  2255. mii_ethtool_gset(&nic->mii, &cmd);
  2256. if (netif_running(netdev))
  2257. e100_down(nic);
  2258. data[2] = e100_self_test(nic);
  2259. data[3] = e100_loopback_test(nic, lb_mac);
  2260. data[4] = e100_loopback_test(nic, lb_phy);
  2261. /* restore speed, duplex & autoneg settings */
  2262. mii_ethtool_sset(&nic->mii, &cmd);
  2263. if (netif_running(netdev))
  2264. e100_up(nic);
  2265. }
  2266. for (i = 0; i < E100_TEST_LEN; i++)
  2267. test->flags |= data[i] ? ETH_TEST_FL_FAILED : 0;
  2268. msleep_interruptible(4 * 1000);
  2269. }
  2270. static int e100_set_phys_id(struct net_device *netdev,
  2271. enum ethtool_phys_id_state state)
  2272. {
  2273. struct nic *nic = netdev_priv(netdev);
  2274. enum led_state {
  2275. led_on = 0x01,
  2276. led_off = 0x04,
  2277. led_on_559 = 0x05,
  2278. led_on_557 = 0x07,
  2279. };
  2280. u16 led_reg = (nic->phy == phy_82552_v) ? E100_82552_LED_OVERRIDE :
  2281. MII_LED_CONTROL;
  2282. u16 leds = 0;
  2283. switch (state) {
  2284. case ETHTOOL_ID_ACTIVE:
  2285. return 2;
  2286. case ETHTOOL_ID_ON:
  2287. leds = (nic->phy == phy_82552_v) ? E100_82552_LED_ON :
  2288. (nic->mac < mac_82559_D101M) ? led_on_557 : led_on_559;
  2289. break;
  2290. case ETHTOOL_ID_OFF:
  2291. leds = (nic->phy == phy_82552_v) ? E100_82552_LED_OFF : led_off;
  2292. break;
  2293. case ETHTOOL_ID_INACTIVE:
  2294. break;
  2295. }
  2296. mdio_write(netdev, nic->mii.phy_id, led_reg, leds);
  2297. return 0;
  2298. }
  2299. static const char e100_gstrings_stats[][ETH_GSTRING_LEN] = {
  2300. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  2301. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  2302. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  2303. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  2304. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  2305. "tx_heartbeat_errors", "tx_window_errors",
  2306. /* device-specific stats */
  2307. "tx_deferred", "tx_single_collisions", "tx_multi_collisions",
  2308. "tx_flow_control_pause", "rx_flow_control_pause",
  2309. "rx_flow_control_unsupported", "tx_tco_packets", "rx_tco_packets",
  2310. "rx_short_frame_errors", "rx_over_length_errors",
  2311. };
  2312. #define E100_NET_STATS_LEN 21
  2313. #define E100_STATS_LEN ARRAY_SIZE(e100_gstrings_stats)
  2314. static int e100_get_sset_count(struct net_device *netdev, int sset)
  2315. {
  2316. switch (sset) {
  2317. case ETH_SS_TEST:
  2318. return E100_TEST_LEN;
  2319. case ETH_SS_STATS:
  2320. return E100_STATS_LEN;
  2321. default:
  2322. return -EOPNOTSUPP;
  2323. }
  2324. }
  2325. static void e100_get_ethtool_stats(struct net_device *netdev,
  2326. struct ethtool_stats *stats, u64 *data)
  2327. {
  2328. struct nic *nic = netdev_priv(netdev);
  2329. int i;
  2330. for (i = 0; i < E100_NET_STATS_LEN; i++)
  2331. data[i] = ((unsigned long *)&netdev->stats)[i];
  2332. data[i++] = nic->tx_deferred;
  2333. data[i++] = nic->tx_single_collisions;
  2334. data[i++] = nic->tx_multiple_collisions;
  2335. data[i++] = nic->tx_fc_pause;
  2336. data[i++] = nic->rx_fc_pause;
  2337. data[i++] = nic->rx_fc_unsupported;
  2338. data[i++] = nic->tx_tco_frames;
  2339. data[i++] = nic->rx_tco_frames;
  2340. data[i++] = nic->rx_short_frame_errors;
  2341. data[i++] = nic->rx_over_length_errors;
  2342. }
  2343. static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  2344. {
  2345. switch (stringset) {
  2346. case ETH_SS_TEST:
  2347. memcpy(data, e100_gstrings_test, sizeof(e100_gstrings_test));
  2348. break;
  2349. case ETH_SS_STATS:
  2350. memcpy(data, e100_gstrings_stats, sizeof(e100_gstrings_stats));
  2351. break;
  2352. }
  2353. }
  2354. static const struct ethtool_ops e100_ethtool_ops = {
  2355. .get_drvinfo = e100_get_drvinfo,
  2356. .get_regs_len = e100_get_regs_len,
  2357. .get_regs = e100_get_regs,
  2358. .get_wol = e100_get_wol,
  2359. .set_wol = e100_set_wol,
  2360. .get_msglevel = e100_get_msglevel,
  2361. .set_msglevel = e100_set_msglevel,
  2362. .nway_reset = e100_nway_reset,
  2363. .get_link = e100_get_link,
  2364. .get_eeprom_len = e100_get_eeprom_len,
  2365. .get_eeprom = e100_get_eeprom,
  2366. .set_eeprom = e100_set_eeprom,
  2367. .get_ringparam = e100_get_ringparam,
  2368. .set_ringparam = e100_set_ringparam,
  2369. .self_test = e100_diag_test,
  2370. .get_strings = e100_get_strings,
  2371. .set_phys_id = e100_set_phys_id,
  2372. .get_ethtool_stats = e100_get_ethtool_stats,
  2373. .get_sset_count = e100_get_sset_count,
  2374. .get_ts_info = ethtool_op_get_ts_info,
  2375. .get_link_ksettings = e100_get_link_ksettings,
  2376. .set_link_ksettings = e100_set_link_ksettings,
  2377. };
  2378. static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2379. {
  2380. struct nic *nic = netdev_priv(netdev);
  2381. return generic_mii_ioctl(&nic->mii, if_mii(ifr), cmd, NULL);
  2382. }
  2383. static int e100_alloc(struct nic *nic)
  2384. {
  2385. nic->mem = dma_alloc_coherent(&nic->pdev->dev, sizeof(struct mem),
  2386. &nic->dma_addr, GFP_KERNEL);
  2387. return nic->mem ? 0 : -ENOMEM;
  2388. }
  2389. static void e100_free(struct nic *nic)
  2390. {
  2391. if (nic->mem) {
  2392. dma_free_coherent(&nic->pdev->dev, sizeof(struct mem),
  2393. nic->mem, nic->dma_addr);
  2394. nic->mem = NULL;
  2395. }
  2396. }
  2397. static int e100_open(struct net_device *netdev)
  2398. {
  2399. struct nic *nic = netdev_priv(netdev);
  2400. int err = 0;
  2401. netif_carrier_off(netdev);
  2402. if ((err = e100_up(nic)))
  2403. netif_err(nic, ifup, nic->netdev, "Cannot open interface, aborting\n");
  2404. return err;
  2405. }
  2406. static int e100_close(struct net_device *netdev)
  2407. {
  2408. e100_down(netdev_priv(netdev));
  2409. return 0;
  2410. }
  2411. static int e100_set_features(struct net_device *netdev,
  2412. netdev_features_t features)
  2413. {
  2414. struct nic *nic = netdev_priv(netdev);
  2415. netdev_features_t changed = features ^ netdev->features;
  2416. if (!(changed & (NETIF_F_RXFCS | NETIF_F_RXALL)))
  2417. return 0;
  2418. netdev->features = features;
  2419. e100_exec_cb(nic, NULL, e100_configure);
  2420. return 1;
  2421. }
  2422. static const struct net_device_ops e100_netdev_ops = {
  2423. .ndo_open = e100_open,
  2424. .ndo_stop = e100_close,
  2425. .ndo_start_xmit = e100_xmit_frame,
  2426. .ndo_validate_addr = eth_validate_addr,
  2427. .ndo_set_rx_mode = e100_set_multicast_list,
  2428. .ndo_set_mac_address = e100_set_mac_address,
  2429. .ndo_eth_ioctl = e100_do_ioctl,
  2430. .ndo_tx_timeout = e100_tx_timeout,
  2431. #ifdef CONFIG_NET_POLL_CONTROLLER
  2432. .ndo_poll_controller = e100_netpoll,
  2433. #endif
  2434. .ndo_set_features = e100_set_features,
  2435. };
  2436. static int e100_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2437. {
  2438. struct net_device *netdev;
  2439. struct nic *nic;
  2440. int err;
  2441. if (!(netdev = alloc_etherdev(sizeof(struct nic))))
  2442. return -ENOMEM;
  2443. netdev->hw_features |= NETIF_F_RXFCS;
  2444. netdev->priv_flags |= IFF_SUPP_NOFCS;
  2445. netdev->hw_features |= NETIF_F_RXALL;
  2446. netdev->netdev_ops = &e100_netdev_ops;
  2447. netdev->ethtool_ops = &e100_ethtool_ops;
  2448. netdev->watchdog_timeo = E100_WATCHDOG_PERIOD;
  2449. strncpy(netdev->name, pci_name(pdev), sizeof(netdev->name) - 1);
  2450. nic = netdev_priv(netdev);
  2451. netif_napi_add_weight(netdev, &nic->napi, e100_poll, E100_NAPI_WEIGHT);
  2452. nic->netdev = netdev;
  2453. nic->pdev = pdev;
  2454. nic->msg_enable = (1 << debug) - 1;
  2455. nic->mdio_ctrl = mdio_ctrl_hw;
  2456. pci_set_drvdata(pdev, netdev);
  2457. if ((err = pci_enable_device(pdev))) {
  2458. netif_err(nic, probe, nic->netdev, "Cannot enable PCI device, aborting\n");
  2459. goto err_out_free_dev;
  2460. }
  2461. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  2462. netif_err(nic, probe, nic->netdev, "Cannot find proper PCI device base address, aborting\n");
  2463. err = -ENODEV;
  2464. goto err_out_disable_pdev;
  2465. }
  2466. if ((err = pci_request_regions(pdev, DRV_NAME))) {
  2467. netif_err(nic, probe, nic->netdev, "Cannot obtain PCI resources, aborting\n");
  2468. goto err_out_disable_pdev;
  2469. }
  2470. if ((err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)))) {
  2471. netif_err(nic, probe, nic->netdev, "No usable DMA configuration, aborting\n");
  2472. goto err_out_free_res;
  2473. }
  2474. SET_NETDEV_DEV(netdev, &pdev->dev);
  2475. if (use_io)
  2476. netif_info(nic, probe, nic->netdev, "using i/o access mode\n");
  2477. nic->csr = pci_iomap(pdev, (use_io ? 1 : 0), sizeof(struct csr));
  2478. if (!nic->csr) {
  2479. netif_err(nic, probe, nic->netdev, "Cannot map device registers, aborting\n");
  2480. err = -ENOMEM;
  2481. goto err_out_free_res;
  2482. }
  2483. if (ent->driver_data)
  2484. nic->flags |= ich;
  2485. else
  2486. nic->flags &= ~ich;
  2487. e100_get_defaults(nic);
  2488. /* D100 MAC doesn't allow rx of vlan packets with normal MTU */
  2489. if (nic->mac < mac_82558_D101_A4)
  2490. netdev->features |= NETIF_F_VLAN_CHALLENGED;
  2491. /* locks must be initialized before calling hw_reset */
  2492. spin_lock_init(&nic->cb_lock);
  2493. spin_lock_init(&nic->cmd_lock);
  2494. spin_lock_init(&nic->mdio_lock);
  2495. /* Reset the device before pci_set_master() in case device is in some
  2496. * funky state and has an interrupt pending - hint: we don't have the
  2497. * interrupt handler registered yet. */
  2498. e100_hw_reset(nic);
  2499. pci_set_master(pdev);
  2500. timer_setup(&nic->watchdog, e100_watchdog, 0);
  2501. INIT_WORK(&nic->tx_timeout_task, e100_tx_timeout_task);
  2502. if ((err = e100_alloc(nic))) {
  2503. netif_err(nic, probe, nic->netdev, "Cannot alloc driver memory, aborting\n");
  2504. goto err_out_iounmap;
  2505. }
  2506. if ((err = e100_eeprom_load(nic)))
  2507. goto err_out_free;
  2508. e100_phy_init(nic);
  2509. eth_hw_addr_set(netdev, (u8 *)nic->eeprom);
  2510. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2511. if (!eeprom_bad_csum_allow) {
  2512. netif_err(nic, probe, nic->netdev, "Invalid MAC address from EEPROM, aborting\n");
  2513. err = -EAGAIN;
  2514. goto err_out_free;
  2515. } else {
  2516. netif_err(nic, probe, nic->netdev, "Invalid MAC address from EEPROM, you MUST configure one.\n");
  2517. }
  2518. }
  2519. /* Wol magic packet can be enabled from eeprom */
  2520. if ((nic->mac >= mac_82558_D101_A4) &&
  2521. (le16_to_cpu(nic->eeprom[eeprom_id]) & eeprom_id_wol)) {
  2522. nic->flags |= wol_magic;
  2523. device_set_wakeup_enable(&pdev->dev, true);
  2524. }
  2525. /* ack any pending wake events, disable PME */
  2526. pci_pme_active(pdev, false);
  2527. strcpy(netdev->name, "eth%d");
  2528. if ((err = register_netdev(netdev))) {
  2529. netif_err(nic, probe, nic->netdev, "Cannot register net device, aborting\n");
  2530. goto err_out_free;
  2531. }
  2532. nic->cbs_pool = dma_pool_create(netdev->name,
  2533. &nic->pdev->dev,
  2534. nic->params.cbs.max * sizeof(struct cb),
  2535. sizeof(u32),
  2536. 0);
  2537. if (!nic->cbs_pool) {
  2538. netif_err(nic, probe, nic->netdev, "Cannot create DMA pool, aborting\n");
  2539. err = -ENOMEM;
  2540. goto err_out_pool;
  2541. }
  2542. netif_info(nic, probe, nic->netdev,
  2543. "addr 0x%llx, irq %d, MAC addr %pM\n",
  2544. (unsigned long long)pci_resource_start(pdev, use_io ? 1 : 0),
  2545. pdev->irq, netdev->dev_addr);
  2546. return 0;
  2547. err_out_pool:
  2548. unregister_netdev(netdev);
  2549. err_out_free:
  2550. e100_free(nic);
  2551. err_out_iounmap:
  2552. pci_iounmap(pdev, nic->csr);
  2553. err_out_free_res:
  2554. pci_release_regions(pdev);
  2555. err_out_disable_pdev:
  2556. pci_disable_device(pdev);
  2557. err_out_free_dev:
  2558. free_netdev(netdev);
  2559. return err;
  2560. }
  2561. static void e100_remove(struct pci_dev *pdev)
  2562. {
  2563. struct net_device *netdev = pci_get_drvdata(pdev);
  2564. if (netdev) {
  2565. struct nic *nic = netdev_priv(netdev);
  2566. unregister_netdev(netdev);
  2567. e100_free(nic);
  2568. pci_iounmap(pdev, nic->csr);
  2569. dma_pool_destroy(nic->cbs_pool);
  2570. free_netdev(netdev);
  2571. pci_release_regions(pdev);
  2572. pci_disable_device(pdev);
  2573. }
  2574. }
  2575. #define E100_82552_SMARTSPEED 0x14 /* SmartSpeed Ctrl register */
  2576. #define E100_82552_REV_ANEG 0x0200 /* Reverse auto-negotiation */
  2577. #define E100_82552_ANEG_NOW 0x0400 /* Auto-negotiate now */
  2578. static void __e100_shutdown(struct pci_dev *pdev, bool *enable_wake)
  2579. {
  2580. struct net_device *netdev = pci_get_drvdata(pdev);
  2581. struct nic *nic = netdev_priv(netdev);
  2582. netif_device_detach(netdev);
  2583. if (netif_running(netdev))
  2584. e100_down(nic);
  2585. if ((nic->flags & wol_magic) | e100_asf(nic)) {
  2586. /* enable reverse auto-negotiation */
  2587. if (nic->phy == phy_82552_v) {
  2588. u16 smartspeed = mdio_read(netdev, nic->mii.phy_id,
  2589. E100_82552_SMARTSPEED);
  2590. mdio_write(netdev, nic->mii.phy_id,
  2591. E100_82552_SMARTSPEED, smartspeed |
  2592. E100_82552_REV_ANEG | E100_82552_ANEG_NOW);
  2593. }
  2594. *enable_wake = true;
  2595. } else {
  2596. *enable_wake = false;
  2597. }
  2598. pci_disable_device(pdev);
  2599. }
  2600. static int __e100_power_off(struct pci_dev *pdev, bool wake)
  2601. {
  2602. if (wake)
  2603. return pci_prepare_to_sleep(pdev);
  2604. pci_wake_from_d3(pdev, false);
  2605. pci_set_power_state(pdev, PCI_D3hot);
  2606. return 0;
  2607. }
  2608. static int __maybe_unused e100_suspend(struct device *dev_d)
  2609. {
  2610. bool wake;
  2611. __e100_shutdown(to_pci_dev(dev_d), &wake);
  2612. return 0;
  2613. }
  2614. static int __maybe_unused e100_resume(struct device *dev_d)
  2615. {
  2616. struct net_device *netdev = dev_get_drvdata(dev_d);
  2617. struct nic *nic = netdev_priv(netdev);
  2618. int err;
  2619. err = pci_enable_device(to_pci_dev(dev_d));
  2620. if (err) {
  2621. netdev_err(netdev, "Resume cannot enable PCI device, aborting\n");
  2622. return err;
  2623. }
  2624. pci_set_master(to_pci_dev(dev_d));
  2625. /* disable reverse auto-negotiation */
  2626. if (nic->phy == phy_82552_v) {
  2627. u16 smartspeed = mdio_read(netdev, nic->mii.phy_id,
  2628. E100_82552_SMARTSPEED);
  2629. mdio_write(netdev, nic->mii.phy_id,
  2630. E100_82552_SMARTSPEED,
  2631. smartspeed & ~(E100_82552_REV_ANEG));
  2632. }
  2633. if (netif_running(netdev))
  2634. e100_up(nic);
  2635. netif_device_attach(netdev);
  2636. return 0;
  2637. }
  2638. static void e100_shutdown(struct pci_dev *pdev)
  2639. {
  2640. bool wake;
  2641. __e100_shutdown(pdev, &wake);
  2642. if (system_state == SYSTEM_POWER_OFF)
  2643. __e100_power_off(pdev, wake);
  2644. }
  2645. /* ------------------ PCI Error Recovery infrastructure -------------- */
  2646. /**
  2647. * e100_io_error_detected - called when PCI error is detected.
  2648. * @pdev: Pointer to PCI device
  2649. * @state: The current pci connection state
  2650. */
  2651. static pci_ers_result_t e100_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  2652. {
  2653. struct net_device *netdev = pci_get_drvdata(pdev);
  2654. struct nic *nic = netdev_priv(netdev);
  2655. netif_device_detach(netdev);
  2656. if (state == pci_channel_io_perm_failure)
  2657. return PCI_ERS_RESULT_DISCONNECT;
  2658. if (netif_running(netdev))
  2659. e100_down(nic);
  2660. pci_disable_device(pdev);
  2661. /* Request a slot reset. */
  2662. return PCI_ERS_RESULT_NEED_RESET;
  2663. }
  2664. /**
  2665. * e100_io_slot_reset - called after the pci bus has been reset.
  2666. * @pdev: Pointer to PCI device
  2667. *
  2668. * Restart the card from scratch.
  2669. */
  2670. static pci_ers_result_t e100_io_slot_reset(struct pci_dev *pdev)
  2671. {
  2672. struct net_device *netdev = pci_get_drvdata(pdev);
  2673. struct nic *nic = netdev_priv(netdev);
  2674. if (pci_enable_device(pdev)) {
  2675. pr_err("Cannot re-enable PCI device after reset\n");
  2676. return PCI_ERS_RESULT_DISCONNECT;
  2677. }
  2678. pci_set_master(pdev);
  2679. /* Only one device per card can do a reset */
  2680. if (0 != PCI_FUNC(pdev->devfn))
  2681. return PCI_ERS_RESULT_RECOVERED;
  2682. e100_hw_reset(nic);
  2683. e100_phy_init(nic);
  2684. return PCI_ERS_RESULT_RECOVERED;
  2685. }
  2686. /**
  2687. * e100_io_resume - resume normal operations
  2688. * @pdev: Pointer to PCI device
  2689. *
  2690. * Resume normal operations after an error recovery
  2691. * sequence has been completed.
  2692. */
  2693. static void e100_io_resume(struct pci_dev *pdev)
  2694. {
  2695. struct net_device *netdev = pci_get_drvdata(pdev);
  2696. struct nic *nic = netdev_priv(netdev);
  2697. /* ack any pending wake events, disable PME */
  2698. pci_enable_wake(pdev, PCI_D0, 0);
  2699. netif_device_attach(netdev);
  2700. if (netif_running(netdev)) {
  2701. e100_open(netdev);
  2702. mod_timer(&nic->watchdog, jiffies);
  2703. }
  2704. }
  2705. static const struct pci_error_handlers e100_err_handler = {
  2706. .error_detected = e100_io_error_detected,
  2707. .slot_reset = e100_io_slot_reset,
  2708. .resume = e100_io_resume,
  2709. };
  2710. static SIMPLE_DEV_PM_OPS(e100_pm_ops, e100_suspend, e100_resume);
  2711. static struct pci_driver e100_driver = {
  2712. .name = DRV_NAME,
  2713. .id_table = e100_id_table,
  2714. .probe = e100_probe,
  2715. .remove = e100_remove,
  2716. /* Power Management hooks */
  2717. .driver.pm = &e100_pm_ops,
  2718. .shutdown = e100_shutdown,
  2719. .err_handler = &e100_err_handler,
  2720. };
  2721. static int __init e100_init_module(void)
  2722. {
  2723. if (((1 << debug) - 1) & NETIF_MSG_DRV) {
  2724. pr_info("%s\n", DRV_DESCRIPTION);
  2725. pr_info("%s\n", DRV_COPYRIGHT);
  2726. }
  2727. return pci_register_driver(&e100_driver);
  2728. }
  2729. static void __exit e100_cleanup_module(void)
  2730. {
  2731. pci_unregister_driver(&e100_driver);
  2732. }
  2733. module_init(e100_init_module);
  2734. module_exit(e100_cleanup_module);