sis190.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  1. /*
  2. sis190.c: Silicon Integrated Systems SiS190 ethernet driver
  3. Copyright (c) 2003 K.M. Liu <kmliu@sis.com>
  4. Copyright (c) 2003, 2004 Jeff Garzik <jgarzik@pobox.com>
  5. Copyright (c) 2003, 2004, 2005 Francois Romieu <romieu@fr.zoreil.com>
  6. Based on r8169.c, tg3.c, 8139cp.c, skge.c, epic100.c and SiS 190/191
  7. genuine driver.
  8. This software may be used and distributed according to the terms of
  9. the GNU General Public License (GPL), incorporated herein by reference.
  10. Drivers based on or derived from this code fall under the GPL and must
  11. retain the authorship, copyright and license notice. This file is not
  12. a complete program and may only be used when the entire operating
  13. system is licensed under the GPL.
  14. See the file COPYING in this distribution for more information.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/moduleparam.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/pci.h>
  25. #include <linux/mii.h>
  26. #include <linux/delay.h>
  27. #include <linux/crc32.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/slab.h>
  30. #include <asm/irq.h>
  31. #define PHY_MAX_ADDR 32
  32. #define PHY_ID_ANY 0x1f
  33. #define MII_REG_ANY 0x1f
  34. #define DRV_VERSION "1.4"
  35. #define DRV_NAME "sis190"
  36. #define SIS190_DRIVER_NAME DRV_NAME " Gigabit Ethernet driver " DRV_VERSION
  37. #define sis190_rx_skb netif_rx
  38. #define sis190_rx_quota(count, quota) count
  39. #define NUM_TX_DESC 64 /* [8..1024] */
  40. #define NUM_RX_DESC 64 /* [8..8192] */
  41. #define TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  42. #define RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  43. #define RX_BUF_SIZE 1536
  44. #define RX_BUF_MASK 0xfff8
  45. #define SIS190_REGS_SIZE 0x80
  46. #define SIS190_TX_TIMEOUT (6*HZ)
  47. #define SIS190_PHY_TIMEOUT (10*HZ)
  48. #define SIS190_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | \
  49. NETIF_MSG_LINK | NETIF_MSG_IFUP | \
  50. NETIF_MSG_IFDOWN)
  51. /* Enhanced PHY access register bit definitions */
  52. #define EhnMIIread 0x0000
  53. #define EhnMIIwrite 0x0020
  54. #define EhnMIIdataShift 16
  55. #define EhnMIIpmdShift 6 /* 7016 only */
  56. #define EhnMIIregShift 11
  57. #define EhnMIIreq 0x0010
  58. #define EhnMIInotDone 0x0010
  59. /* Write/read MMIO register */
  60. #define SIS_W8(reg, val) writeb ((val), ioaddr + (reg))
  61. #define SIS_W16(reg, val) writew ((val), ioaddr + (reg))
  62. #define SIS_W32(reg, val) writel ((val), ioaddr + (reg))
  63. #define SIS_R8(reg) readb (ioaddr + (reg))
  64. #define SIS_R16(reg) readw (ioaddr + (reg))
  65. #define SIS_R32(reg) readl (ioaddr + (reg))
  66. #define SIS_PCI_COMMIT() SIS_R32(IntrControl)
  67. enum sis190_registers {
  68. TxControl = 0x00,
  69. TxDescStartAddr = 0x04,
  70. rsv0 = 0x08, // reserved
  71. TxSts = 0x0c, // unused (Control/Status)
  72. RxControl = 0x10,
  73. RxDescStartAddr = 0x14,
  74. rsv1 = 0x18, // reserved
  75. RxSts = 0x1c, // unused
  76. IntrStatus = 0x20,
  77. IntrMask = 0x24,
  78. IntrControl = 0x28,
  79. IntrTimer = 0x2c, // unused (Interrupt Timer)
  80. PMControl = 0x30, // unused (Power Mgmt Control/Status)
  81. rsv2 = 0x34, // reserved
  82. ROMControl = 0x38,
  83. ROMInterface = 0x3c,
  84. StationControl = 0x40,
  85. GMIIControl = 0x44,
  86. GIoCR = 0x48, // unused (GMAC IO Compensation)
  87. GIoCtrl = 0x4c, // unused (GMAC IO Control)
  88. TxMacControl = 0x50,
  89. TxLimit = 0x54, // unused (Tx MAC Timer/TryLimit)
  90. RGDelay = 0x58, // unused (RGMII Tx Internal Delay)
  91. rsv3 = 0x5c, // reserved
  92. RxMacControl = 0x60,
  93. RxMacAddr = 0x62,
  94. RxHashTable = 0x68,
  95. // Undocumented = 0x6c,
  96. RxWolCtrl = 0x70,
  97. RxWolData = 0x74, // unused (Rx WOL Data Access)
  98. RxMPSControl = 0x78, // unused (Rx MPS Control)
  99. rsv4 = 0x7c, // reserved
  100. };
  101. enum sis190_register_content {
  102. /* IntrStatus */
  103. SoftInt = 0x40000000, // unused
  104. Timeup = 0x20000000, // unused
  105. PauseFrame = 0x00080000, // unused
  106. MagicPacket = 0x00040000, // unused
  107. WakeupFrame = 0x00020000, // unused
  108. LinkChange = 0x00010000,
  109. RxQEmpty = 0x00000080,
  110. RxQInt = 0x00000040,
  111. TxQ1Empty = 0x00000020, // unused
  112. TxQ1Int = 0x00000010,
  113. TxQ0Empty = 0x00000008, // unused
  114. TxQ0Int = 0x00000004,
  115. RxHalt = 0x00000002,
  116. TxHalt = 0x00000001,
  117. /* {Rx/Tx}CmdBits */
  118. CmdReset = 0x10,
  119. CmdRxEnb = 0x08, // unused
  120. CmdTxEnb = 0x01,
  121. RxBufEmpty = 0x01, // unused
  122. /* Cfg9346Bits */
  123. Cfg9346_Lock = 0x00, // unused
  124. Cfg9346_Unlock = 0xc0, // unused
  125. /* RxMacControl */
  126. AcceptErr = 0x20, // unused
  127. AcceptRunt = 0x10, // unused
  128. AcceptBroadcast = 0x0800,
  129. AcceptMulticast = 0x0400,
  130. AcceptMyPhys = 0x0200,
  131. AcceptAllPhys = 0x0100,
  132. /* RxConfigBits */
  133. RxCfgFIFOShift = 13,
  134. RxCfgDMAShift = 8, // 0x1a in RxControl ?
  135. /* TxConfigBits */
  136. TxInterFrameGapShift = 24,
  137. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  138. LinkStatus = 0x02, // unused
  139. FullDup = 0x01, // unused
  140. /* TBICSRBit */
  141. TBILinkOK = 0x02000000, // unused
  142. };
  143. struct TxDesc {
  144. __le32 PSize;
  145. __le32 status;
  146. __le32 addr;
  147. __le32 size;
  148. };
  149. struct RxDesc {
  150. __le32 PSize;
  151. __le32 status;
  152. __le32 addr;
  153. __le32 size;
  154. };
  155. enum _DescStatusBit {
  156. /* _Desc.status */
  157. OWNbit = 0x80000000, // RXOWN/TXOWN
  158. INTbit = 0x40000000, // RXINT/TXINT
  159. CRCbit = 0x00020000, // CRCOFF/CRCEN
  160. PADbit = 0x00010000, // PREADD/PADEN
  161. /* _Desc.size */
  162. RingEnd = 0x80000000,
  163. /* TxDesc.status */
  164. LSEN = 0x08000000, // TSO ? -- FR
  165. IPCS = 0x04000000,
  166. TCPCS = 0x02000000,
  167. UDPCS = 0x01000000,
  168. BSTEN = 0x00800000,
  169. EXTEN = 0x00400000,
  170. DEFEN = 0x00200000,
  171. BKFEN = 0x00100000,
  172. CRSEN = 0x00080000,
  173. COLEN = 0x00040000,
  174. THOL3 = 0x30000000,
  175. THOL2 = 0x20000000,
  176. THOL1 = 0x10000000,
  177. THOL0 = 0x00000000,
  178. WND = 0x00080000,
  179. TABRT = 0x00040000,
  180. FIFO = 0x00020000,
  181. LINK = 0x00010000,
  182. ColCountMask = 0x0000ffff,
  183. /* RxDesc.status */
  184. IPON = 0x20000000,
  185. TCPON = 0x10000000,
  186. UDPON = 0x08000000,
  187. Wakup = 0x00400000,
  188. Magic = 0x00200000,
  189. Pause = 0x00100000,
  190. DEFbit = 0x00200000,
  191. BCAST = 0x000c0000,
  192. MCAST = 0x00080000,
  193. UCAST = 0x00040000,
  194. /* RxDesc.PSize */
  195. TAGON = 0x80000000,
  196. RxDescCountMask = 0x7f000000, // multi-desc pkt when > 1 ? -- FR
  197. ABORT = 0x00800000,
  198. SHORT = 0x00400000,
  199. LIMIT = 0x00200000,
  200. MIIER = 0x00100000,
  201. OVRUN = 0x00080000,
  202. NIBON = 0x00040000,
  203. COLON = 0x00020000,
  204. CRCOK = 0x00010000,
  205. RxSizeMask = 0x0000ffff
  206. /*
  207. * The asic could apparently do vlan, TSO, jumbo (sis191 only) and
  208. * provide two (unused with Linux) Tx queues. No publicly
  209. * available documentation alas.
  210. */
  211. };
  212. enum sis190_eeprom_access_register_bits {
  213. EECS = 0x00000001, // unused
  214. EECLK = 0x00000002, // unused
  215. EEDO = 0x00000008, // unused
  216. EEDI = 0x00000004, // unused
  217. EEREQ = 0x00000080,
  218. EEROP = 0x00000200,
  219. EEWOP = 0x00000100 // unused
  220. };
  221. /* EEPROM Addresses */
  222. enum sis190_eeprom_address {
  223. EEPROMSignature = 0x00,
  224. EEPROMCLK = 0x01, // unused
  225. EEPROMInfo = 0x02,
  226. EEPROMMACAddr = 0x03
  227. };
  228. enum sis190_feature {
  229. F_HAS_RGMII = 1,
  230. F_PHY_88E1111 = 2,
  231. F_PHY_BCM5461 = 4
  232. };
  233. struct sis190_private {
  234. void __iomem *mmio_addr;
  235. struct pci_dev *pci_dev;
  236. struct net_device *dev;
  237. spinlock_t lock;
  238. u32 rx_buf_sz;
  239. u32 cur_rx;
  240. u32 cur_tx;
  241. u32 dirty_rx;
  242. u32 dirty_tx;
  243. dma_addr_t rx_dma;
  244. dma_addr_t tx_dma;
  245. struct RxDesc *RxDescRing;
  246. struct TxDesc *TxDescRing;
  247. struct sk_buff *Rx_skbuff[NUM_RX_DESC];
  248. struct sk_buff *Tx_skbuff[NUM_TX_DESC];
  249. struct work_struct phy_task;
  250. struct timer_list timer;
  251. u32 msg_enable;
  252. struct mii_if_info mii_if;
  253. struct list_head first_phy;
  254. u32 features;
  255. u32 negotiated_lpa;
  256. enum {
  257. LNK_OFF,
  258. LNK_ON,
  259. LNK_AUTONEG,
  260. } link_status;
  261. };
  262. struct sis190_phy {
  263. struct list_head list;
  264. int phy_id;
  265. u16 id[2];
  266. u16 status;
  267. u8 type;
  268. };
  269. enum sis190_phy_type {
  270. UNKNOWN = 0x00,
  271. HOME = 0x01,
  272. LAN = 0x02,
  273. MIX = 0x03
  274. };
  275. static struct mii_chip_info {
  276. const char *name;
  277. u16 id[2];
  278. unsigned int type;
  279. u32 feature;
  280. } mii_chip_table[] = {
  281. { "Atheros PHY", { 0x004d, 0xd010 }, LAN, 0 },
  282. { "Atheros PHY AR8012", { 0x004d, 0xd020 }, LAN, 0 },
  283. { "Broadcom PHY BCM5461", { 0x0020, 0x60c0 }, LAN, F_PHY_BCM5461 },
  284. { "Broadcom PHY AC131", { 0x0143, 0xbc70 }, LAN, 0 },
  285. { "Agere PHY ET1101B", { 0x0282, 0xf010 }, LAN, 0 },
  286. { "Marvell PHY 88E1111", { 0x0141, 0x0cc0 }, LAN, F_PHY_88E1111 },
  287. { "Realtek PHY RTL8201", { 0x0000, 0x8200 }, LAN, 0 },
  288. { NULL, }
  289. };
  290. static const struct {
  291. const char *name;
  292. } sis_chip_info[] = {
  293. { "SiS 190 PCI Fast Ethernet adapter" },
  294. { "SiS 191 PCI Gigabit Ethernet adapter" },
  295. };
  296. static const struct pci_device_id sis190_pci_tbl[] = {
  297. { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x0190), 0, 0, 0 },
  298. { PCI_DEVICE(PCI_VENDOR_ID_SI, 0x0191), 0, 0, 1 },
  299. { 0, },
  300. };
  301. MODULE_DEVICE_TABLE(pci, sis190_pci_tbl);
  302. static int rx_copybreak = 200;
  303. static struct {
  304. u32 msg_enable;
  305. } debug = { -1 };
  306. MODULE_DESCRIPTION("SiS sis190/191 Gigabit Ethernet driver");
  307. module_param(rx_copybreak, int, 0);
  308. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
  309. module_param_named(debug, debug.msg_enable, int, 0);
  310. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  311. MODULE_AUTHOR("K.M. Liu <kmliu@sis.com>, Ueimor <romieu@fr.zoreil.com>");
  312. MODULE_VERSION(DRV_VERSION);
  313. MODULE_LICENSE("GPL");
  314. static const u32 sis190_intr_mask =
  315. RxQEmpty | RxQInt | TxQ1Int | TxQ0Int | RxHalt | TxHalt | LinkChange;
  316. /*
  317. * Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  318. * The chips use a 64 element hash table based on the Ethernet CRC.
  319. */
  320. static const int multicast_filter_limit = 32;
  321. static void __mdio_cmd(void __iomem *ioaddr, u32 ctl)
  322. {
  323. unsigned int i;
  324. SIS_W32(GMIIControl, ctl);
  325. msleep(1);
  326. for (i = 0; i < 100; i++) {
  327. if (!(SIS_R32(GMIIControl) & EhnMIInotDone))
  328. break;
  329. msleep(1);
  330. }
  331. if (i > 99)
  332. pr_err("PHY command failed !\n");
  333. }
  334. static void mdio_write(void __iomem *ioaddr, int phy_id, int reg, int val)
  335. {
  336. __mdio_cmd(ioaddr, EhnMIIreq | EhnMIIwrite |
  337. (((u32) reg) << EhnMIIregShift) | (phy_id << EhnMIIpmdShift) |
  338. (((u32) val) << EhnMIIdataShift));
  339. }
  340. static int mdio_read(void __iomem *ioaddr, int phy_id, int reg)
  341. {
  342. __mdio_cmd(ioaddr, EhnMIIreq | EhnMIIread |
  343. (((u32) reg) << EhnMIIregShift) | (phy_id << EhnMIIpmdShift));
  344. return (u16) (SIS_R32(GMIIControl) >> EhnMIIdataShift);
  345. }
  346. static void __mdio_write(struct net_device *dev, int phy_id, int reg, int val)
  347. {
  348. struct sis190_private *tp = netdev_priv(dev);
  349. mdio_write(tp->mmio_addr, phy_id, reg, val);
  350. }
  351. static int __mdio_read(struct net_device *dev, int phy_id, int reg)
  352. {
  353. struct sis190_private *tp = netdev_priv(dev);
  354. return mdio_read(tp->mmio_addr, phy_id, reg);
  355. }
  356. static u16 mdio_read_latched(void __iomem *ioaddr, int phy_id, int reg)
  357. {
  358. mdio_read(ioaddr, phy_id, reg);
  359. return mdio_read(ioaddr, phy_id, reg);
  360. }
  361. static u16 sis190_read_eeprom(void __iomem *ioaddr, u32 reg)
  362. {
  363. u16 data = 0xffff;
  364. unsigned int i;
  365. if (!(SIS_R32(ROMControl) & 0x0002))
  366. return 0;
  367. SIS_W32(ROMInterface, EEREQ | EEROP | (reg << 10));
  368. for (i = 0; i < 200; i++) {
  369. if (!(SIS_R32(ROMInterface) & EEREQ)) {
  370. data = (SIS_R32(ROMInterface) & 0xffff0000) >> 16;
  371. break;
  372. }
  373. msleep(1);
  374. }
  375. return data;
  376. }
  377. static void sis190_irq_mask_and_ack(void __iomem *ioaddr)
  378. {
  379. SIS_W32(IntrMask, 0x00);
  380. SIS_W32(IntrStatus, 0xffffffff);
  381. SIS_PCI_COMMIT();
  382. }
  383. static void sis190_asic_down(void __iomem *ioaddr)
  384. {
  385. /* Stop the chip's Tx and Rx DMA processes. */
  386. SIS_W32(TxControl, 0x1a00);
  387. SIS_W32(RxControl, 0x1a00);
  388. sis190_irq_mask_and_ack(ioaddr);
  389. }
  390. static void sis190_mark_as_last_descriptor(struct RxDesc *desc)
  391. {
  392. desc->size |= cpu_to_le32(RingEnd);
  393. }
  394. static inline void sis190_give_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  395. {
  396. u32 eor = le32_to_cpu(desc->size) & RingEnd;
  397. desc->PSize = 0x0;
  398. desc->size = cpu_to_le32((rx_buf_sz & RX_BUF_MASK) | eor);
  399. wmb();
  400. desc->status = cpu_to_le32(OWNbit | INTbit);
  401. }
  402. static inline void sis190_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  403. u32 rx_buf_sz)
  404. {
  405. desc->addr = cpu_to_le32(mapping);
  406. sis190_give_to_asic(desc, rx_buf_sz);
  407. }
  408. static inline void sis190_make_unusable_by_asic(struct RxDesc *desc)
  409. {
  410. desc->PSize = 0x0;
  411. desc->addr = cpu_to_le32(0xdeadbeef);
  412. desc->size &= cpu_to_le32(RingEnd);
  413. wmb();
  414. desc->status = 0x0;
  415. }
  416. static struct sk_buff *sis190_alloc_rx_skb(struct sis190_private *tp,
  417. struct RxDesc *desc)
  418. {
  419. u32 rx_buf_sz = tp->rx_buf_sz;
  420. struct sk_buff *skb;
  421. dma_addr_t mapping;
  422. skb = netdev_alloc_skb(tp->dev, rx_buf_sz);
  423. if (unlikely(!skb))
  424. goto skb_alloc_failed;
  425. mapping = pci_map_single(tp->pci_dev, skb->data, tp->rx_buf_sz,
  426. PCI_DMA_FROMDEVICE);
  427. if (pci_dma_mapping_error(tp->pci_dev, mapping))
  428. goto out;
  429. sis190_map_to_asic(desc, mapping, rx_buf_sz);
  430. return skb;
  431. out:
  432. dev_kfree_skb_any(skb);
  433. skb_alloc_failed:
  434. sis190_make_unusable_by_asic(desc);
  435. return NULL;
  436. }
  437. static u32 sis190_rx_fill(struct sis190_private *tp, struct net_device *dev,
  438. u32 start, u32 end)
  439. {
  440. u32 cur;
  441. for (cur = start; cur < end; cur++) {
  442. unsigned int i = cur % NUM_RX_DESC;
  443. if (tp->Rx_skbuff[i])
  444. continue;
  445. tp->Rx_skbuff[i] = sis190_alloc_rx_skb(tp, tp->RxDescRing + i);
  446. if (!tp->Rx_skbuff[i])
  447. break;
  448. }
  449. return cur - start;
  450. }
  451. static bool sis190_try_rx_copy(struct sis190_private *tp,
  452. struct sk_buff **sk_buff, int pkt_size,
  453. dma_addr_t addr)
  454. {
  455. struct sk_buff *skb;
  456. bool done = false;
  457. if (pkt_size >= rx_copybreak)
  458. goto out;
  459. skb = netdev_alloc_skb_ip_align(tp->dev, pkt_size);
  460. if (!skb)
  461. goto out;
  462. pci_dma_sync_single_for_cpu(tp->pci_dev, addr, tp->rx_buf_sz,
  463. PCI_DMA_FROMDEVICE);
  464. skb_copy_to_linear_data(skb, sk_buff[0]->data, pkt_size);
  465. *sk_buff = skb;
  466. done = true;
  467. out:
  468. return done;
  469. }
  470. static inline int sis190_rx_pkt_err(u32 status, struct net_device_stats *stats)
  471. {
  472. #define ErrMask (OVRUN | SHORT | LIMIT | MIIER | NIBON | COLON | ABORT)
  473. if ((status & CRCOK) && !(status & ErrMask))
  474. return 0;
  475. if (!(status & CRCOK))
  476. stats->rx_crc_errors++;
  477. else if (status & OVRUN)
  478. stats->rx_over_errors++;
  479. else if (status & (SHORT | LIMIT))
  480. stats->rx_length_errors++;
  481. else if (status & (MIIER | NIBON | COLON))
  482. stats->rx_frame_errors++;
  483. stats->rx_errors++;
  484. return -1;
  485. }
  486. static int sis190_rx_interrupt(struct net_device *dev,
  487. struct sis190_private *tp, void __iomem *ioaddr)
  488. {
  489. struct net_device_stats *stats = &dev->stats;
  490. u32 rx_left, cur_rx = tp->cur_rx;
  491. u32 delta, count;
  492. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  493. rx_left = sis190_rx_quota(rx_left, (u32) dev->quota);
  494. for (; rx_left > 0; rx_left--, cur_rx++) {
  495. unsigned int entry = cur_rx % NUM_RX_DESC;
  496. struct RxDesc *desc = tp->RxDescRing + entry;
  497. u32 status;
  498. if (le32_to_cpu(desc->status) & OWNbit)
  499. break;
  500. status = le32_to_cpu(desc->PSize);
  501. //netif_info(tp, intr, dev, "Rx PSize = %08x\n", status);
  502. if (sis190_rx_pkt_err(status, stats) < 0)
  503. sis190_give_to_asic(desc, tp->rx_buf_sz);
  504. else {
  505. struct sk_buff *skb = tp->Rx_skbuff[entry];
  506. dma_addr_t addr = le32_to_cpu(desc->addr);
  507. int pkt_size = (status & RxSizeMask) - 4;
  508. struct pci_dev *pdev = tp->pci_dev;
  509. if (unlikely(pkt_size > tp->rx_buf_sz)) {
  510. netif_info(tp, intr, dev,
  511. "(frag) status = %08x\n", status);
  512. stats->rx_dropped++;
  513. stats->rx_length_errors++;
  514. sis190_give_to_asic(desc, tp->rx_buf_sz);
  515. continue;
  516. }
  517. if (sis190_try_rx_copy(tp, &skb, pkt_size, addr)) {
  518. pci_dma_sync_single_for_device(pdev, addr,
  519. tp->rx_buf_sz, PCI_DMA_FROMDEVICE);
  520. sis190_give_to_asic(desc, tp->rx_buf_sz);
  521. } else {
  522. pci_unmap_single(pdev, addr, tp->rx_buf_sz,
  523. PCI_DMA_FROMDEVICE);
  524. tp->Rx_skbuff[entry] = NULL;
  525. sis190_make_unusable_by_asic(desc);
  526. }
  527. skb_put(skb, pkt_size);
  528. skb->protocol = eth_type_trans(skb, dev);
  529. sis190_rx_skb(skb);
  530. stats->rx_packets++;
  531. stats->rx_bytes += pkt_size;
  532. if ((status & BCAST) == MCAST)
  533. stats->multicast++;
  534. }
  535. }
  536. count = cur_rx - tp->cur_rx;
  537. tp->cur_rx = cur_rx;
  538. delta = sis190_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
  539. if (!delta && count)
  540. netif_info(tp, intr, dev, "no Rx buffer allocated\n");
  541. tp->dirty_rx += delta;
  542. if ((tp->dirty_rx + NUM_RX_DESC) == tp->cur_rx)
  543. netif_emerg(tp, intr, dev, "Rx buffers exhausted\n");
  544. return count;
  545. }
  546. static void sis190_unmap_tx_skb(struct pci_dev *pdev, struct sk_buff *skb,
  547. struct TxDesc *desc)
  548. {
  549. unsigned int len;
  550. len = skb->len < ETH_ZLEN ? ETH_ZLEN : skb->len;
  551. pci_unmap_single(pdev, le32_to_cpu(desc->addr), len, PCI_DMA_TODEVICE);
  552. memset(desc, 0x00, sizeof(*desc));
  553. }
  554. static inline int sis190_tx_pkt_err(u32 status, struct net_device_stats *stats)
  555. {
  556. #define TxErrMask (WND | TABRT | FIFO | LINK)
  557. if (!unlikely(status & TxErrMask))
  558. return 0;
  559. if (status & WND)
  560. stats->tx_window_errors++;
  561. if (status & TABRT)
  562. stats->tx_aborted_errors++;
  563. if (status & FIFO)
  564. stats->tx_fifo_errors++;
  565. if (status & LINK)
  566. stats->tx_carrier_errors++;
  567. stats->tx_errors++;
  568. return -1;
  569. }
  570. static void sis190_tx_interrupt(struct net_device *dev,
  571. struct sis190_private *tp, void __iomem *ioaddr)
  572. {
  573. struct net_device_stats *stats = &dev->stats;
  574. u32 pending, dirty_tx = tp->dirty_tx;
  575. /*
  576. * It would not be needed if queueing was allowed to be enabled
  577. * again too early (hint: think preempt and unclocked smp systems).
  578. */
  579. unsigned int queue_stopped;
  580. smp_rmb();
  581. pending = tp->cur_tx - dirty_tx;
  582. queue_stopped = (pending == NUM_TX_DESC);
  583. for (; pending; pending--, dirty_tx++) {
  584. unsigned int entry = dirty_tx % NUM_TX_DESC;
  585. struct TxDesc *txd = tp->TxDescRing + entry;
  586. u32 status = le32_to_cpu(txd->status);
  587. struct sk_buff *skb;
  588. if (status & OWNbit)
  589. break;
  590. skb = tp->Tx_skbuff[entry];
  591. if (likely(sis190_tx_pkt_err(status, stats) == 0)) {
  592. stats->tx_packets++;
  593. stats->tx_bytes += skb->len;
  594. stats->collisions += ((status & ColCountMask) - 1);
  595. }
  596. sis190_unmap_tx_skb(tp->pci_dev, skb, txd);
  597. tp->Tx_skbuff[entry] = NULL;
  598. dev_consume_skb_irq(skb);
  599. }
  600. if (tp->dirty_tx != dirty_tx) {
  601. tp->dirty_tx = dirty_tx;
  602. smp_wmb();
  603. if (queue_stopped)
  604. netif_wake_queue(dev);
  605. }
  606. }
  607. /*
  608. * The interrupt handler does all of the Rx thread work and cleans up after
  609. * the Tx thread.
  610. */
  611. static irqreturn_t sis190_irq(int irq, void *__dev)
  612. {
  613. struct net_device *dev = __dev;
  614. struct sis190_private *tp = netdev_priv(dev);
  615. void __iomem *ioaddr = tp->mmio_addr;
  616. unsigned int handled = 0;
  617. u32 status;
  618. status = SIS_R32(IntrStatus);
  619. if ((status == 0xffffffff) || !status)
  620. goto out;
  621. handled = 1;
  622. if (unlikely(!netif_running(dev))) {
  623. sis190_asic_down(ioaddr);
  624. goto out;
  625. }
  626. SIS_W32(IntrStatus, status);
  627. // netif_info(tp, intr, dev, "status = %08x\n", status);
  628. if (status & LinkChange) {
  629. netif_info(tp, intr, dev, "link change\n");
  630. del_timer(&tp->timer);
  631. schedule_work(&tp->phy_task);
  632. }
  633. if (status & RxQInt)
  634. sis190_rx_interrupt(dev, tp, ioaddr);
  635. if (status & TxQ0Int)
  636. sis190_tx_interrupt(dev, tp, ioaddr);
  637. out:
  638. return IRQ_RETVAL(handled);
  639. }
  640. #ifdef CONFIG_NET_POLL_CONTROLLER
  641. static void sis190_netpoll(struct net_device *dev)
  642. {
  643. struct sis190_private *tp = netdev_priv(dev);
  644. const int irq = tp->pci_dev->irq;
  645. disable_irq(irq);
  646. sis190_irq(irq, dev);
  647. enable_irq(irq);
  648. }
  649. #endif
  650. static void sis190_free_rx_skb(struct sis190_private *tp,
  651. struct sk_buff **sk_buff, struct RxDesc *desc)
  652. {
  653. struct pci_dev *pdev = tp->pci_dev;
  654. pci_unmap_single(pdev, le32_to_cpu(desc->addr), tp->rx_buf_sz,
  655. PCI_DMA_FROMDEVICE);
  656. dev_kfree_skb(*sk_buff);
  657. *sk_buff = NULL;
  658. sis190_make_unusable_by_asic(desc);
  659. }
  660. static void sis190_rx_clear(struct sis190_private *tp)
  661. {
  662. unsigned int i;
  663. for (i = 0; i < NUM_RX_DESC; i++) {
  664. if (!tp->Rx_skbuff[i])
  665. continue;
  666. sis190_free_rx_skb(tp, tp->Rx_skbuff + i, tp->RxDescRing + i);
  667. }
  668. }
  669. static void sis190_init_ring_indexes(struct sis190_private *tp)
  670. {
  671. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  672. }
  673. static int sis190_init_ring(struct net_device *dev)
  674. {
  675. struct sis190_private *tp = netdev_priv(dev);
  676. sis190_init_ring_indexes(tp);
  677. memset(tp->Tx_skbuff, 0x0, NUM_TX_DESC * sizeof(struct sk_buff *));
  678. memset(tp->Rx_skbuff, 0x0, NUM_RX_DESC * sizeof(struct sk_buff *));
  679. if (sis190_rx_fill(tp, dev, 0, NUM_RX_DESC) != NUM_RX_DESC)
  680. goto err_rx_clear;
  681. sis190_mark_as_last_descriptor(tp->RxDescRing + NUM_RX_DESC - 1);
  682. return 0;
  683. err_rx_clear:
  684. sis190_rx_clear(tp);
  685. return -ENOMEM;
  686. }
  687. static void sis190_set_rx_mode(struct net_device *dev)
  688. {
  689. struct sis190_private *tp = netdev_priv(dev);
  690. void __iomem *ioaddr = tp->mmio_addr;
  691. unsigned long flags;
  692. u32 mc_filter[2]; /* Multicast hash filter */
  693. u16 rx_mode;
  694. if (dev->flags & IFF_PROMISC) {
  695. rx_mode =
  696. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  697. AcceptAllPhys;
  698. mc_filter[1] = mc_filter[0] = 0xffffffff;
  699. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  700. (dev->flags & IFF_ALLMULTI)) {
  701. /* Too many to filter perfectly -- accept all multicasts. */
  702. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  703. mc_filter[1] = mc_filter[0] = 0xffffffff;
  704. } else {
  705. struct netdev_hw_addr *ha;
  706. rx_mode = AcceptBroadcast | AcceptMyPhys;
  707. mc_filter[1] = mc_filter[0] = 0;
  708. netdev_for_each_mc_addr(ha, dev) {
  709. int bit_nr =
  710. ether_crc(ETH_ALEN, ha->addr) & 0x3f;
  711. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  712. rx_mode |= AcceptMulticast;
  713. }
  714. }
  715. spin_lock_irqsave(&tp->lock, flags);
  716. SIS_W16(RxMacControl, rx_mode | 0x2);
  717. SIS_W32(RxHashTable, mc_filter[0]);
  718. SIS_W32(RxHashTable + 4, mc_filter[1]);
  719. spin_unlock_irqrestore(&tp->lock, flags);
  720. }
  721. static void sis190_soft_reset(void __iomem *ioaddr)
  722. {
  723. SIS_W32(IntrControl, 0x8000);
  724. SIS_PCI_COMMIT();
  725. SIS_W32(IntrControl, 0x0);
  726. sis190_asic_down(ioaddr);
  727. }
  728. static void sis190_hw_start(struct net_device *dev)
  729. {
  730. struct sis190_private *tp = netdev_priv(dev);
  731. void __iomem *ioaddr = tp->mmio_addr;
  732. sis190_soft_reset(ioaddr);
  733. SIS_W32(TxDescStartAddr, tp->tx_dma);
  734. SIS_W32(RxDescStartAddr, tp->rx_dma);
  735. SIS_W32(IntrStatus, 0xffffffff);
  736. SIS_W32(IntrMask, 0x0);
  737. SIS_W32(GMIIControl, 0x0);
  738. SIS_W32(TxMacControl, 0x60);
  739. SIS_W16(RxMacControl, 0x02);
  740. SIS_W32(RxHashTable, 0x0);
  741. SIS_W32(0x6c, 0x0);
  742. SIS_W32(RxWolCtrl, 0x0);
  743. SIS_W32(RxWolData, 0x0);
  744. SIS_PCI_COMMIT();
  745. sis190_set_rx_mode(dev);
  746. /* Enable all known interrupts by setting the interrupt mask. */
  747. SIS_W32(IntrMask, sis190_intr_mask);
  748. SIS_W32(TxControl, 0x1a00 | CmdTxEnb);
  749. SIS_W32(RxControl, 0x1a1d);
  750. netif_start_queue(dev);
  751. }
  752. static void sis190_phy_task(struct work_struct *work)
  753. {
  754. struct sis190_private *tp =
  755. container_of(work, struct sis190_private, phy_task);
  756. struct net_device *dev = tp->dev;
  757. void __iomem *ioaddr = tp->mmio_addr;
  758. int phy_id = tp->mii_if.phy_id;
  759. u16 val;
  760. rtnl_lock();
  761. if (!netif_running(dev))
  762. goto out_unlock;
  763. val = mdio_read(ioaddr, phy_id, MII_BMCR);
  764. if (val & BMCR_RESET) {
  765. // FIXME: needlessly high ? -- FR 02/07/2005
  766. mod_timer(&tp->timer, jiffies + HZ/10);
  767. goto out_unlock;
  768. }
  769. val = mdio_read_latched(ioaddr, phy_id, MII_BMSR);
  770. if (!(val & BMSR_ANEGCOMPLETE) && tp->link_status != LNK_AUTONEG) {
  771. netif_carrier_off(dev);
  772. netif_warn(tp, link, dev, "auto-negotiating...\n");
  773. tp->link_status = LNK_AUTONEG;
  774. } else if ((val & BMSR_LSTATUS) && tp->link_status != LNK_ON) {
  775. /* Rejoice ! */
  776. struct {
  777. int val;
  778. u32 ctl;
  779. const char *msg;
  780. } reg31[] = {
  781. { LPA_1000FULL, 0x07000c00 | 0x00001000,
  782. "1000 Mbps Full Duplex" },
  783. { LPA_1000HALF, 0x07000c00,
  784. "1000 Mbps Half Duplex" },
  785. { LPA_100FULL, 0x04000800 | 0x00001000,
  786. "100 Mbps Full Duplex" },
  787. { LPA_100HALF, 0x04000800,
  788. "100 Mbps Half Duplex" },
  789. { LPA_10FULL, 0x04000400 | 0x00001000,
  790. "10 Mbps Full Duplex" },
  791. { LPA_10HALF, 0x04000400,
  792. "10 Mbps Half Duplex" },
  793. { 0, 0x04000400, "unknown" }
  794. }, *p = NULL;
  795. u16 adv, autoexp, gigadv, gigrec;
  796. val = mdio_read(ioaddr, phy_id, 0x1f);
  797. netif_info(tp, link, dev, "mii ext = %04x\n", val);
  798. val = mdio_read(ioaddr, phy_id, MII_LPA);
  799. adv = mdio_read(ioaddr, phy_id, MII_ADVERTISE);
  800. autoexp = mdio_read(ioaddr, phy_id, MII_EXPANSION);
  801. netif_info(tp, link, dev, "mii lpa=%04x adv=%04x exp=%04x\n",
  802. val, adv, autoexp);
  803. if (val & LPA_NPAGE && autoexp & EXPANSION_NWAY) {
  804. /* check for gigabit speed */
  805. gigadv = mdio_read(ioaddr, phy_id, MII_CTRL1000);
  806. gigrec = mdio_read(ioaddr, phy_id, MII_STAT1000);
  807. val = (gigadv & (gigrec >> 2));
  808. if (val & ADVERTISE_1000FULL)
  809. p = reg31;
  810. else if (val & ADVERTISE_1000HALF)
  811. p = reg31 + 1;
  812. }
  813. if (!p) {
  814. val &= adv;
  815. for (p = reg31; p->val; p++) {
  816. if ((val & p->val) == p->val)
  817. break;
  818. }
  819. }
  820. p->ctl |= SIS_R32(StationControl) & ~0x0f001c00;
  821. if ((tp->features & F_HAS_RGMII) &&
  822. (tp->features & F_PHY_BCM5461)) {
  823. // Set Tx Delay in RGMII mode.
  824. mdio_write(ioaddr, phy_id, 0x18, 0xf1c7);
  825. udelay(200);
  826. mdio_write(ioaddr, phy_id, 0x1c, 0x8c00);
  827. p->ctl |= 0x03000000;
  828. }
  829. SIS_W32(StationControl, p->ctl);
  830. if (tp->features & F_HAS_RGMII) {
  831. SIS_W32(RGDelay, 0x0441);
  832. SIS_W32(RGDelay, 0x0440);
  833. }
  834. tp->negotiated_lpa = p->val;
  835. netif_info(tp, link, dev, "link on %s mode\n", p->msg);
  836. netif_carrier_on(dev);
  837. tp->link_status = LNK_ON;
  838. } else if (!(val & BMSR_LSTATUS) && tp->link_status != LNK_AUTONEG)
  839. tp->link_status = LNK_OFF;
  840. mod_timer(&tp->timer, jiffies + SIS190_PHY_TIMEOUT);
  841. out_unlock:
  842. rtnl_unlock();
  843. }
  844. static void sis190_phy_timer(struct timer_list *t)
  845. {
  846. struct sis190_private *tp = from_timer(tp, t, timer);
  847. struct net_device *dev = tp->dev;
  848. if (likely(netif_running(dev)))
  849. schedule_work(&tp->phy_task);
  850. }
  851. static inline void sis190_delete_timer(struct net_device *dev)
  852. {
  853. struct sis190_private *tp = netdev_priv(dev);
  854. del_timer_sync(&tp->timer);
  855. }
  856. static inline void sis190_request_timer(struct net_device *dev)
  857. {
  858. struct sis190_private *tp = netdev_priv(dev);
  859. struct timer_list *timer = &tp->timer;
  860. timer_setup(timer, sis190_phy_timer, 0);
  861. timer->expires = jiffies + SIS190_PHY_TIMEOUT;
  862. add_timer(timer);
  863. }
  864. static void sis190_set_rxbufsize(struct sis190_private *tp,
  865. struct net_device *dev)
  866. {
  867. unsigned int mtu = dev->mtu;
  868. tp->rx_buf_sz = (mtu > RX_BUF_SIZE) ? mtu + ETH_HLEN + 8 : RX_BUF_SIZE;
  869. /* RxDesc->size has a licence to kill the lower bits */
  870. if (tp->rx_buf_sz & 0x07) {
  871. tp->rx_buf_sz += 8;
  872. tp->rx_buf_sz &= RX_BUF_MASK;
  873. }
  874. }
  875. static int sis190_open(struct net_device *dev)
  876. {
  877. struct sis190_private *tp = netdev_priv(dev);
  878. struct pci_dev *pdev = tp->pci_dev;
  879. int rc = -ENOMEM;
  880. sis190_set_rxbufsize(tp, dev);
  881. /*
  882. * Rx and Tx descriptors need 256 bytes alignment.
  883. * pci_alloc_consistent() guarantees a stronger alignment.
  884. */
  885. tp->TxDescRing = pci_alloc_consistent(pdev, TX_RING_BYTES, &tp->tx_dma);
  886. if (!tp->TxDescRing)
  887. goto out;
  888. tp->RxDescRing = pci_alloc_consistent(pdev, RX_RING_BYTES, &tp->rx_dma);
  889. if (!tp->RxDescRing)
  890. goto err_free_tx_0;
  891. rc = sis190_init_ring(dev);
  892. if (rc < 0)
  893. goto err_free_rx_1;
  894. sis190_request_timer(dev);
  895. rc = request_irq(pdev->irq, sis190_irq, IRQF_SHARED, dev->name, dev);
  896. if (rc < 0)
  897. goto err_release_timer_2;
  898. sis190_hw_start(dev);
  899. out:
  900. return rc;
  901. err_release_timer_2:
  902. sis190_delete_timer(dev);
  903. sis190_rx_clear(tp);
  904. err_free_rx_1:
  905. pci_free_consistent(pdev, RX_RING_BYTES, tp->RxDescRing, tp->rx_dma);
  906. err_free_tx_0:
  907. pci_free_consistent(pdev, TX_RING_BYTES, tp->TxDescRing, tp->tx_dma);
  908. goto out;
  909. }
  910. static void sis190_tx_clear(struct sis190_private *tp)
  911. {
  912. unsigned int i;
  913. for (i = 0; i < NUM_TX_DESC; i++) {
  914. struct sk_buff *skb = tp->Tx_skbuff[i];
  915. if (!skb)
  916. continue;
  917. sis190_unmap_tx_skb(tp->pci_dev, skb, tp->TxDescRing + i);
  918. tp->Tx_skbuff[i] = NULL;
  919. dev_kfree_skb(skb);
  920. tp->dev->stats.tx_dropped++;
  921. }
  922. tp->cur_tx = tp->dirty_tx = 0;
  923. }
  924. static void sis190_down(struct net_device *dev)
  925. {
  926. struct sis190_private *tp = netdev_priv(dev);
  927. void __iomem *ioaddr = tp->mmio_addr;
  928. unsigned int poll_locked = 0;
  929. sis190_delete_timer(dev);
  930. netif_stop_queue(dev);
  931. do {
  932. spin_lock_irq(&tp->lock);
  933. sis190_asic_down(ioaddr);
  934. spin_unlock_irq(&tp->lock);
  935. synchronize_irq(tp->pci_dev->irq);
  936. if (!poll_locked)
  937. poll_locked++;
  938. synchronize_rcu();
  939. } while (SIS_R32(IntrMask));
  940. sis190_tx_clear(tp);
  941. sis190_rx_clear(tp);
  942. }
  943. static int sis190_close(struct net_device *dev)
  944. {
  945. struct sis190_private *tp = netdev_priv(dev);
  946. struct pci_dev *pdev = tp->pci_dev;
  947. sis190_down(dev);
  948. free_irq(pdev->irq, dev);
  949. pci_free_consistent(pdev, TX_RING_BYTES, tp->TxDescRing, tp->tx_dma);
  950. pci_free_consistent(pdev, RX_RING_BYTES, tp->RxDescRing, tp->rx_dma);
  951. tp->TxDescRing = NULL;
  952. tp->RxDescRing = NULL;
  953. return 0;
  954. }
  955. static netdev_tx_t sis190_start_xmit(struct sk_buff *skb,
  956. struct net_device *dev)
  957. {
  958. struct sis190_private *tp = netdev_priv(dev);
  959. void __iomem *ioaddr = tp->mmio_addr;
  960. u32 len, entry, dirty_tx;
  961. struct TxDesc *desc;
  962. dma_addr_t mapping;
  963. if (unlikely(skb->len < ETH_ZLEN)) {
  964. if (skb_padto(skb, ETH_ZLEN)) {
  965. dev->stats.tx_dropped++;
  966. goto out;
  967. }
  968. len = ETH_ZLEN;
  969. } else {
  970. len = skb->len;
  971. }
  972. entry = tp->cur_tx % NUM_TX_DESC;
  973. desc = tp->TxDescRing + entry;
  974. if (unlikely(le32_to_cpu(desc->status) & OWNbit)) {
  975. netif_stop_queue(dev);
  976. netif_err(tp, tx_err, dev,
  977. "BUG! Tx Ring full when queue awake!\n");
  978. return NETDEV_TX_BUSY;
  979. }
  980. mapping = pci_map_single(tp->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  981. if (pci_dma_mapping_error(tp->pci_dev, mapping)) {
  982. netif_err(tp, tx_err, dev,
  983. "PCI mapping failed, dropping packet");
  984. return NETDEV_TX_BUSY;
  985. }
  986. tp->Tx_skbuff[entry] = skb;
  987. desc->PSize = cpu_to_le32(len);
  988. desc->addr = cpu_to_le32(mapping);
  989. desc->size = cpu_to_le32(len);
  990. if (entry == (NUM_TX_DESC - 1))
  991. desc->size |= cpu_to_le32(RingEnd);
  992. wmb();
  993. desc->status = cpu_to_le32(OWNbit | INTbit | DEFbit | CRCbit | PADbit);
  994. if (tp->negotiated_lpa & (LPA_1000HALF | LPA_100HALF | LPA_10HALF)) {
  995. /* Half Duplex */
  996. desc->status |= cpu_to_le32(COLEN | CRSEN | BKFEN);
  997. if (tp->negotiated_lpa & (LPA_1000HALF | LPA_1000FULL))
  998. desc->status |= cpu_to_le32(EXTEN | BSTEN); /* gigabit HD */
  999. }
  1000. tp->cur_tx++;
  1001. smp_wmb();
  1002. SIS_W32(TxControl, 0x1a00 | CmdReset | CmdTxEnb);
  1003. dirty_tx = tp->dirty_tx;
  1004. if ((tp->cur_tx - NUM_TX_DESC) == dirty_tx) {
  1005. netif_stop_queue(dev);
  1006. smp_rmb();
  1007. if (dirty_tx != tp->dirty_tx)
  1008. netif_wake_queue(dev);
  1009. }
  1010. out:
  1011. return NETDEV_TX_OK;
  1012. }
  1013. static void sis190_free_phy(struct list_head *first_phy)
  1014. {
  1015. struct sis190_phy *cur, *next;
  1016. list_for_each_entry_safe(cur, next, first_phy, list) {
  1017. kfree(cur);
  1018. }
  1019. }
  1020. /**
  1021. * sis190_default_phy - Select default PHY for sis190 mac.
  1022. * @dev: the net device to probe for
  1023. *
  1024. * Select first detected PHY with link as default.
  1025. * If no one is link on, select PHY whose types is HOME as default.
  1026. * If HOME doesn't exist, select LAN.
  1027. */
  1028. static u16 sis190_default_phy(struct net_device *dev)
  1029. {
  1030. struct sis190_phy *phy, *phy_home, *phy_default, *phy_lan;
  1031. struct sis190_private *tp = netdev_priv(dev);
  1032. struct mii_if_info *mii_if = &tp->mii_if;
  1033. void __iomem *ioaddr = tp->mmio_addr;
  1034. u16 status;
  1035. phy_home = phy_default = phy_lan = NULL;
  1036. list_for_each_entry(phy, &tp->first_phy, list) {
  1037. status = mdio_read_latched(ioaddr, phy->phy_id, MII_BMSR);
  1038. // Link ON & Not select default PHY & not ghost PHY.
  1039. if ((status & BMSR_LSTATUS) &&
  1040. !phy_default &&
  1041. (phy->type != UNKNOWN)) {
  1042. phy_default = phy;
  1043. } else {
  1044. status = mdio_read(ioaddr, phy->phy_id, MII_BMCR);
  1045. mdio_write(ioaddr, phy->phy_id, MII_BMCR,
  1046. status | BMCR_ANENABLE | BMCR_ISOLATE);
  1047. if (phy->type == HOME)
  1048. phy_home = phy;
  1049. else if (phy->type == LAN)
  1050. phy_lan = phy;
  1051. }
  1052. }
  1053. if (!phy_default) {
  1054. if (phy_home)
  1055. phy_default = phy_home;
  1056. else if (phy_lan)
  1057. phy_default = phy_lan;
  1058. else
  1059. phy_default = list_first_entry(&tp->first_phy,
  1060. struct sis190_phy, list);
  1061. }
  1062. if (mii_if->phy_id != phy_default->phy_id) {
  1063. mii_if->phy_id = phy_default->phy_id;
  1064. if (netif_msg_probe(tp))
  1065. pr_info("%s: Using transceiver at address %d as default\n",
  1066. pci_name(tp->pci_dev), mii_if->phy_id);
  1067. }
  1068. status = mdio_read(ioaddr, mii_if->phy_id, MII_BMCR);
  1069. status &= (~BMCR_ISOLATE);
  1070. mdio_write(ioaddr, mii_if->phy_id, MII_BMCR, status);
  1071. status = mdio_read_latched(ioaddr, mii_if->phy_id, MII_BMSR);
  1072. return status;
  1073. }
  1074. static void sis190_init_phy(struct net_device *dev, struct sis190_private *tp,
  1075. struct sis190_phy *phy, unsigned int phy_id,
  1076. u16 mii_status)
  1077. {
  1078. void __iomem *ioaddr = tp->mmio_addr;
  1079. struct mii_chip_info *p;
  1080. INIT_LIST_HEAD(&phy->list);
  1081. phy->status = mii_status;
  1082. phy->phy_id = phy_id;
  1083. phy->id[0] = mdio_read(ioaddr, phy_id, MII_PHYSID1);
  1084. phy->id[1] = mdio_read(ioaddr, phy_id, MII_PHYSID2);
  1085. for (p = mii_chip_table; p->type; p++) {
  1086. if ((p->id[0] == phy->id[0]) &&
  1087. (p->id[1] == (phy->id[1] & 0xfff0))) {
  1088. break;
  1089. }
  1090. }
  1091. if (p->id[1]) {
  1092. phy->type = (p->type == MIX) ?
  1093. ((mii_status & (BMSR_100FULL | BMSR_100HALF)) ?
  1094. LAN : HOME) : p->type;
  1095. tp->features |= p->feature;
  1096. if (netif_msg_probe(tp))
  1097. pr_info("%s: %s transceiver at address %d\n",
  1098. pci_name(tp->pci_dev), p->name, phy_id);
  1099. } else {
  1100. phy->type = UNKNOWN;
  1101. if (netif_msg_probe(tp))
  1102. pr_info("%s: unknown PHY 0x%x:0x%x transceiver at address %d\n",
  1103. pci_name(tp->pci_dev),
  1104. phy->id[0], (phy->id[1] & 0xfff0), phy_id);
  1105. }
  1106. }
  1107. static void sis190_mii_probe_88e1111_fixup(struct sis190_private *tp)
  1108. {
  1109. if (tp->features & F_PHY_88E1111) {
  1110. void __iomem *ioaddr = tp->mmio_addr;
  1111. int phy_id = tp->mii_if.phy_id;
  1112. u16 reg[2][2] = {
  1113. { 0x808b, 0x0ce1 },
  1114. { 0x808f, 0x0c60 }
  1115. }, *p;
  1116. p = (tp->features & F_HAS_RGMII) ? reg[0] : reg[1];
  1117. mdio_write(ioaddr, phy_id, 0x1b, p[0]);
  1118. udelay(200);
  1119. mdio_write(ioaddr, phy_id, 0x14, p[1]);
  1120. udelay(200);
  1121. }
  1122. }
  1123. /**
  1124. * sis190_mii_probe - Probe MII PHY for sis190
  1125. * @dev: the net device to probe for
  1126. *
  1127. * Search for total of 32 possible mii phy addresses.
  1128. * Identify and set current phy if found one,
  1129. * return error if it failed to found.
  1130. */
  1131. static int sis190_mii_probe(struct net_device *dev)
  1132. {
  1133. struct sis190_private *tp = netdev_priv(dev);
  1134. struct mii_if_info *mii_if = &tp->mii_if;
  1135. void __iomem *ioaddr = tp->mmio_addr;
  1136. int phy_id;
  1137. int rc = 0;
  1138. INIT_LIST_HEAD(&tp->first_phy);
  1139. for (phy_id = 0; phy_id < PHY_MAX_ADDR; phy_id++) {
  1140. struct sis190_phy *phy;
  1141. u16 status;
  1142. status = mdio_read_latched(ioaddr, phy_id, MII_BMSR);
  1143. // Try next mii if the current one is not accessible.
  1144. if (status == 0xffff || status == 0x0000)
  1145. continue;
  1146. phy = kmalloc(sizeof(*phy), GFP_KERNEL);
  1147. if (!phy) {
  1148. sis190_free_phy(&tp->first_phy);
  1149. rc = -ENOMEM;
  1150. goto out;
  1151. }
  1152. sis190_init_phy(dev, tp, phy, phy_id, status);
  1153. list_add(&tp->first_phy, &phy->list);
  1154. }
  1155. if (list_empty(&tp->first_phy)) {
  1156. if (netif_msg_probe(tp))
  1157. pr_info("%s: No MII transceivers found!\n",
  1158. pci_name(tp->pci_dev));
  1159. rc = -EIO;
  1160. goto out;
  1161. }
  1162. /* Select default PHY for mac */
  1163. sis190_default_phy(dev);
  1164. sis190_mii_probe_88e1111_fixup(tp);
  1165. mii_if->dev = dev;
  1166. mii_if->mdio_read = __mdio_read;
  1167. mii_if->mdio_write = __mdio_write;
  1168. mii_if->phy_id_mask = PHY_ID_ANY;
  1169. mii_if->reg_num_mask = MII_REG_ANY;
  1170. out:
  1171. return rc;
  1172. }
  1173. static void sis190_mii_remove(struct net_device *dev)
  1174. {
  1175. struct sis190_private *tp = netdev_priv(dev);
  1176. sis190_free_phy(&tp->first_phy);
  1177. }
  1178. static void sis190_release_board(struct pci_dev *pdev)
  1179. {
  1180. struct net_device *dev = pci_get_drvdata(pdev);
  1181. struct sis190_private *tp = netdev_priv(dev);
  1182. iounmap(tp->mmio_addr);
  1183. pci_release_regions(pdev);
  1184. pci_disable_device(pdev);
  1185. free_netdev(dev);
  1186. }
  1187. static struct net_device *sis190_init_board(struct pci_dev *pdev)
  1188. {
  1189. struct sis190_private *tp;
  1190. struct net_device *dev;
  1191. void __iomem *ioaddr;
  1192. int rc;
  1193. dev = alloc_etherdev(sizeof(*tp));
  1194. if (!dev) {
  1195. rc = -ENOMEM;
  1196. goto err_out_0;
  1197. }
  1198. SET_NETDEV_DEV(dev, &pdev->dev);
  1199. tp = netdev_priv(dev);
  1200. tp->dev = dev;
  1201. tp->msg_enable = netif_msg_init(debug.msg_enable, SIS190_MSG_DEFAULT);
  1202. rc = pci_enable_device(pdev);
  1203. if (rc < 0) {
  1204. if (netif_msg_probe(tp))
  1205. pr_err("%s: enable failure\n", pci_name(pdev));
  1206. goto err_free_dev_1;
  1207. }
  1208. rc = -ENODEV;
  1209. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  1210. if (netif_msg_probe(tp))
  1211. pr_err("%s: region #0 is no MMIO resource\n",
  1212. pci_name(pdev));
  1213. goto err_pci_disable_2;
  1214. }
  1215. if (pci_resource_len(pdev, 0) < SIS190_REGS_SIZE) {
  1216. if (netif_msg_probe(tp))
  1217. pr_err("%s: invalid PCI region size(s)\n",
  1218. pci_name(pdev));
  1219. goto err_pci_disable_2;
  1220. }
  1221. rc = pci_request_regions(pdev, DRV_NAME);
  1222. if (rc < 0) {
  1223. if (netif_msg_probe(tp))
  1224. pr_err("%s: could not request regions\n",
  1225. pci_name(pdev));
  1226. goto err_pci_disable_2;
  1227. }
  1228. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1229. if (rc < 0) {
  1230. if (netif_msg_probe(tp))
  1231. pr_err("%s: DMA configuration failed\n",
  1232. pci_name(pdev));
  1233. goto err_free_res_3;
  1234. }
  1235. pci_set_master(pdev);
  1236. ioaddr = ioremap(pci_resource_start(pdev, 0), SIS190_REGS_SIZE);
  1237. if (!ioaddr) {
  1238. if (netif_msg_probe(tp))
  1239. pr_err("%s: cannot remap MMIO, aborting\n",
  1240. pci_name(pdev));
  1241. rc = -EIO;
  1242. goto err_free_res_3;
  1243. }
  1244. tp->pci_dev = pdev;
  1245. tp->mmio_addr = ioaddr;
  1246. tp->link_status = LNK_OFF;
  1247. sis190_irq_mask_and_ack(ioaddr);
  1248. sis190_soft_reset(ioaddr);
  1249. out:
  1250. return dev;
  1251. err_free_res_3:
  1252. pci_release_regions(pdev);
  1253. err_pci_disable_2:
  1254. pci_disable_device(pdev);
  1255. err_free_dev_1:
  1256. free_netdev(dev);
  1257. err_out_0:
  1258. dev = ERR_PTR(rc);
  1259. goto out;
  1260. }
  1261. static void sis190_tx_timeout(struct net_device *dev)
  1262. {
  1263. struct sis190_private *tp = netdev_priv(dev);
  1264. void __iomem *ioaddr = tp->mmio_addr;
  1265. u8 tmp8;
  1266. /* Disable Tx, if not already */
  1267. tmp8 = SIS_R8(TxControl);
  1268. if (tmp8 & CmdTxEnb)
  1269. SIS_W8(TxControl, tmp8 & ~CmdTxEnb);
  1270. netif_info(tp, tx_err, dev, "Transmit timeout, status %08x %08x\n",
  1271. SIS_R32(TxControl), SIS_R32(TxSts));
  1272. /* Disable interrupts by clearing the interrupt mask. */
  1273. SIS_W32(IntrMask, 0x0000);
  1274. /* Stop a shared interrupt from scavenging while we are. */
  1275. spin_lock_irq(&tp->lock);
  1276. sis190_tx_clear(tp);
  1277. spin_unlock_irq(&tp->lock);
  1278. /* ...and finally, reset everything. */
  1279. sis190_hw_start(dev);
  1280. netif_wake_queue(dev);
  1281. }
  1282. static void sis190_set_rgmii(struct sis190_private *tp, u8 reg)
  1283. {
  1284. tp->features |= (reg & 0x80) ? F_HAS_RGMII : 0;
  1285. }
  1286. static int sis190_get_mac_addr_from_eeprom(struct pci_dev *pdev,
  1287. struct net_device *dev)
  1288. {
  1289. struct sis190_private *tp = netdev_priv(dev);
  1290. void __iomem *ioaddr = tp->mmio_addr;
  1291. u16 sig;
  1292. int i;
  1293. if (netif_msg_probe(tp))
  1294. pr_info("%s: Read MAC address from EEPROM\n", pci_name(pdev));
  1295. /* Check to see if there is a sane EEPROM */
  1296. sig = (u16) sis190_read_eeprom(ioaddr, EEPROMSignature);
  1297. if ((sig == 0xffff) || (sig == 0x0000)) {
  1298. if (netif_msg_probe(tp))
  1299. pr_info("%s: Error EEPROM read %x\n",
  1300. pci_name(pdev), sig);
  1301. return -EIO;
  1302. }
  1303. /* Get MAC address from EEPROM */
  1304. for (i = 0; i < ETH_ALEN / 2; i++) {
  1305. u16 w = sis190_read_eeprom(ioaddr, EEPROMMACAddr + i);
  1306. ((__le16 *)dev->dev_addr)[i] = cpu_to_le16(w);
  1307. }
  1308. sis190_set_rgmii(tp, sis190_read_eeprom(ioaddr, EEPROMInfo));
  1309. return 0;
  1310. }
  1311. /**
  1312. * sis190_get_mac_addr_from_apc - Get MAC address for SiS96x model
  1313. * @pdev: PCI device
  1314. * @dev: network device to get address for
  1315. *
  1316. * SiS96x model, use APC CMOS RAM to store MAC address.
  1317. * APC CMOS RAM is accessed through ISA bridge.
  1318. * MAC address is read into @net_dev->dev_addr.
  1319. */
  1320. static int sis190_get_mac_addr_from_apc(struct pci_dev *pdev,
  1321. struct net_device *dev)
  1322. {
  1323. static const u16 ids[] = { 0x0965, 0x0966, 0x0968 };
  1324. struct sis190_private *tp = netdev_priv(dev);
  1325. struct pci_dev *isa_bridge;
  1326. u8 reg, tmp8;
  1327. unsigned int i;
  1328. if (netif_msg_probe(tp))
  1329. pr_info("%s: Read MAC address from APC\n", pci_name(pdev));
  1330. for (i = 0; i < ARRAY_SIZE(ids); i++) {
  1331. isa_bridge = pci_get_device(PCI_VENDOR_ID_SI, ids[i], NULL);
  1332. if (isa_bridge)
  1333. break;
  1334. }
  1335. if (!isa_bridge) {
  1336. if (netif_msg_probe(tp))
  1337. pr_info("%s: Can not find ISA bridge\n",
  1338. pci_name(pdev));
  1339. return -EIO;
  1340. }
  1341. /* Enable port 78h & 79h to access APC Registers. */
  1342. pci_read_config_byte(isa_bridge, 0x48, &tmp8);
  1343. reg = (tmp8 & ~0x02);
  1344. pci_write_config_byte(isa_bridge, 0x48, reg);
  1345. udelay(50);
  1346. pci_read_config_byte(isa_bridge, 0x48, &reg);
  1347. for (i = 0; i < ETH_ALEN; i++) {
  1348. outb(0x9 + i, 0x78);
  1349. dev->dev_addr[i] = inb(0x79);
  1350. }
  1351. outb(0x12, 0x78);
  1352. reg = inb(0x79);
  1353. sis190_set_rgmii(tp, reg);
  1354. /* Restore the value to ISA Bridge */
  1355. pci_write_config_byte(isa_bridge, 0x48, tmp8);
  1356. pci_dev_put(isa_bridge);
  1357. return 0;
  1358. }
  1359. /**
  1360. * sis190_init_rxfilter - Initialize the Rx filter
  1361. * @dev: network device to initialize
  1362. *
  1363. * Set receive filter address to our MAC address
  1364. * and enable packet filtering.
  1365. */
  1366. static inline void sis190_init_rxfilter(struct net_device *dev)
  1367. {
  1368. struct sis190_private *tp = netdev_priv(dev);
  1369. void __iomem *ioaddr = tp->mmio_addr;
  1370. u16 ctl;
  1371. int i;
  1372. ctl = SIS_R16(RxMacControl);
  1373. /*
  1374. * Disable packet filtering before setting filter.
  1375. * Note: SiS's driver writes 32 bits but RxMacControl is 16 bits
  1376. * only and followed by RxMacAddr (6 bytes). Strange. -- FR
  1377. */
  1378. SIS_W16(RxMacControl, ctl & ~0x0f00);
  1379. for (i = 0; i < ETH_ALEN; i++)
  1380. SIS_W8(RxMacAddr + i, dev->dev_addr[i]);
  1381. SIS_W16(RxMacControl, ctl);
  1382. SIS_PCI_COMMIT();
  1383. }
  1384. static int sis190_get_mac_addr(struct pci_dev *pdev, struct net_device *dev)
  1385. {
  1386. int rc;
  1387. rc = sis190_get_mac_addr_from_eeprom(pdev, dev);
  1388. if (rc < 0) {
  1389. u8 reg;
  1390. pci_read_config_byte(pdev, 0x73, &reg);
  1391. if (reg & 0x00000001)
  1392. rc = sis190_get_mac_addr_from_apc(pdev, dev);
  1393. }
  1394. return rc;
  1395. }
  1396. static void sis190_set_speed_auto(struct net_device *dev)
  1397. {
  1398. struct sis190_private *tp = netdev_priv(dev);
  1399. void __iomem *ioaddr = tp->mmio_addr;
  1400. int phy_id = tp->mii_if.phy_id;
  1401. int val;
  1402. netif_info(tp, link, dev, "Enabling Auto-negotiation\n");
  1403. val = mdio_read(ioaddr, phy_id, MII_ADVERTISE);
  1404. // Enable 10/100 Full/Half Mode, leave MII_ADVERTISE bit4:0
  1405. // unchanged.
  1406. mdio_write(ioaddr, phy_id, MII_ADVERTISE, (val & ADVERTISE_SLCT) |
  1407. ADVERTISE_100FULL | ADVERTISE_10FULL |
  1408. ADVERTISE_100HALF | ADVERTISE_10HALF);
  1409. // Enable 1000 Full Mode.
  1410. mdio_write(ioaddr, phy_id, MII_CTRL1000, ADVERTISE_1000FULL);
  1411. // Enable auto-negotiation and restart auto-negotiation.
  1412. mdio_write(ioaddr, phy_id, MII_BMCR,
  1413. BMCR_ANENABLE | BMCR_ANRESTART | BMCR_RESET);
  1414. }
  1415. static int sis190_get_link_ksettings(struct net_device *dev,
  1416. struct ethtool_link_ksettings *cmd)
  1417. {
  1418. struct sis190_private *tp = netdev_priv(dev);
  1419. mii_ethtool_get_link_ksettings(&tp->mii_if, cmd);
  1420. return 0;
  1421. }
  1422. static int sis190_set_link_ksettings(struct net_device *dev,
  1423. const struct ethtool_link_ksettings *cmd)
  1424. {
  1425. struct sis190_private *tp = netdev_priv(dev);
  1426. return mii_ethtool_set_link_ksettings(&tp->mii_if, cmd);
  1427. }
  1428. static void sis190_get_drvinfo(struct net_device *dev,
  1429. struct ethtool_drvinfo *info)
  1430. {
  1431. struct sis190_private *tp = netdev_priv(dev);
  1432. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  1433. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  1434. strlcpy(info->bus_info, pci_name(tp->pci_dev),
  1435. sizeof(info->bus_info));
  1436. }
  1437. static int sis190_get_regs_len(struct net_device *dev)
  1438. {
  1439. return SIS190_REGS_SIZE;
  1440. }
  1441. static void sis190_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  1442. void *p)
  1443. {
  1444. struct sis190_private *tp = netdev_priv(dev);
  1445. unsigned long flags;
  1446. spin_lock_irqsave(&tp->lock, flags);
  1447. memcpy_fromio(p, tp->mmio_addr, regs->len);
  1448. spin_unlock_irqrestore(&tp->lock, flags);
  1449. }
  1450. static int sis190_nway_reset(struct net_device *dev)
  1451. {
  1452. struct sis190_private *tp = netdev_priv(dev);
  1453. return mii_nway_restart(&tp->mii_if);
  1454. }
  1455. static u32 sis190_get_msglevel(struct net_device *dev)
  1456. {
  1457. struct sis190_private *tp = netdev_priv(dev);
  1458. return tp->msg_enable;
  1459. }
  1460. static void sis190_set_msglevel(struct net_device *dev, u32 value)
  1461. {
  1462. struct sis190_private *tp = netdev_priv(dev);
  1463. tp->msg_enable = value;
  1464. }
  1465. static const struct ethtool_ops sis190_ethtool_ops = {
  1466. .get_drvinfo = sis190_get_drvinfo,
  1467. .get_regs_len = sis190_get_regs_len,
  1468. .get_regs = sis190_get_regs,
  1469. .get_link = ethtool_op_get_link,
  1470. .get_msglevel = sis190_get_msglevel,
  1471. .set_msglevel = sis190_set_msglevel,
  1472. .nway_reset = sis190_nway_reset,
  1473. .get_link_ksettings = sis190_get_link_ksettings,
  1474. .set_link_ksettings = sis190_set_link_ksettings,
  1475. };
  1476. static int sis190_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1477. {
  1478. struct sis190_private *tp = netdev_priv(dev);
  1479. return !netif_running(dev) ? -EINVAL :
  1480. generic_mii_ioctl(&tp->mii_if, if_mii(ifr), cmd, NULL);
  1481. }
  1482. static int sis190_mac_addr(struct net_device *dev, void *p)
  1483. {
  1484. int rc;
  1485. rc = eth_mac_addr(dev, p);
  1486. if (!rc)
  1487. sis190_init_rxfilter(dev);
  1488. return rc;
  1489. }
  1490. static const struct net_device_ops sis190_netdev_ops = {
  1491. .ndo_open = sis190_open,
  1492. .ndo_stop = sis190_close,
  1493. .ndo_do_ioctl = sis190_ioctl,
  1494. .ndo_start_xmit = sis190_start_xmit,
  1495. .ndo_tx_timeout = sis190_tx_timeout,
  1496. .ndo_set_rx_mode = sis190_set_rx_mode,
  1497. .ndo_set_mac_address = sis190_mac_addr,
  1498. .ndo_validate_addr = eth_validate_addr,
  1499. #ifdef CONFIG_NET_POLL_CONTROLLER
  1500. .ndo_poll_controller = sis190_netpoll,
  1501. #endif
  1502. };
  1503. static int sis190_init_one(struct pci_dev *pdev,
  1504. const struct pci_device_id *ent)
  1505. {
  1506. static int printed_version = 0;
  1507. struct sis190_private *tp;
  1508. struct net_device *dev;
  1509. void __iomem *ioaddr;
  1510. int rc;
  1511. if (!printed_version) {
  1512. if (netif_msg_drv(&debug))
  1513. pr_info(SIS190_DRIVER_NAME " loaded\n");
  1514. printed_version = 1;
  1515. }
  1516. dev = sis190_init_board(pdev);
  1517. if (IS_ERR(dev)) {
  1518. rc = PTR_ERR(dev);
  1519. goto out;
  1520. }
  1521. pci_set_drvdata(pdev, dev);
  1522. tp = netdev_priv(dev);
  1523. ioaddr = tp->mmio_addr;
  1524. rc = sis190_get_mac_addr(pdev, dev);
  1525. if (rc < 0)
  1526. goto err_release_board;
  1527. sis190_init_rxfilter(dev);
  1528. INIT_WORK(&tp->phy_task, sis190_phy_task);
  1529. dev->netdev_ops = &sis190_netdev_ops;
  1530. dev->ethtool_ops = &sis190_ethtool_ops;
  1531. dev->watchdog_timeo = SIS190_TX_TIMEOUT;
  1532. spin_lock_init(&tp->lock);
  1533. rc = sis190_mii_probe(dev);
  1534. if (rc < 0)
  1535. goto err_release_board;
  1536. rc = register_netdev(dev);
  1537. if (rc < 0)
  1538. goto err_remove_mii;
  1539. if (netif_msg_probe(tp)) {
  1540. netdev_info(dev, "%s: %s at %p (IRQ: %d), %pM\n",
  1541. pci_name(pdev),
  1542. sis_chip_info[ent->driver_data].name,
  1543. ioaddr, pdev->irq, dev->dev_addr);
  1544. netdev_info(dev, "%s mode.\n",
  1545. (tp->features & F_HAS_RGMII) ? "RGMII" : "GMII");
  1546. }
  1547. netif_carrier_off(dev);
  1548. sis190_set_speed_auto(dev);
  1549. out:
  1550. return rc;
  1551. err_remove_mii:
  1552. sis190_mii_remove(dev);
  1553. err_release_board:
  1554. sis190_release_board(pdev);
  1555. goto out;
  1556. }
  1557. static void sis190_remove_one(struct pci_dev *pdev)
  1558. {
  1559. struct net_device *dev = pci_get_drvdata(pdev);
  1560. struct sis190_private *tp = netdev_priv(dev);
  1561. sis190_mii_remove(dev);
  1562. cancel_work_sync(&tp->phy_task);
  1563. unregister_netdev(dev);
  1564. sis190_release_board(pdev);
  1565. }
  1566. static struct pci_driver sis190_pci_driver = {
  1567. .name = DRV_NAME,
  1568. .id_table = sis190_pci_tbl,
  1569. .probe = sis190_init_one,
  1570. .remove = sis190_remove_one,
  1571. };
  1572. module_pci_driver(sis190_pci_driver);