e100.c 90 KB

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