sc92031.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /* Silan SC92031 PCI Fast Ethernet Adapter driver
  2. *
  3. * Based on vendor drivers:
  4. * Silan Fast Ethernet Netcard Driver:
  5. * MODULE_AUTHOR ("gaoyonghong");
  6. * MODULE_DESCRIPTION ("SILAN Fast Ethernet driver");
  7. * MODULE_LICENSE("GPL");
  8. * 8139D Fast Ethernet driver:
  9. * (C) 2002 by gaoyonghong
  10. * MODULE_AUTHOR ("gaoyonghong");
  11. * MODULE_DESCRIPTION ("Rsltek 8139D PCI Fast Ethernet Adapter driver");
  12. * MODULE_LICENSE("GPL");
  13. * Both are almost identical and seem to be based on pci-skeleton.c
  14. *
  15. * Rewritten for 2.6 by Cesar Eduardo Barros
  16. *
  17. * A datasheet for this chip can be found at
  18. * http://www.silan.com.cn/english/product/pdf/SC92031AY.pdf
  19. */
  20. /* Note about set_mac_address: I don't know how to change the hardware
  21. * matching, so you need to enable IFF_PROMISC when using it.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/delay.h>
  26. #include <linux/pci.h>
  27. #include <linux/dma-mapping.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/etherdevice.h>
  30. #include <linux/ethtool.h>
  31. #include <linux/crc32.h>
  32. #include <asm/irq.h>
  33. #define SC92031_NAME "sc92031"
  34. /* BAR 0 is MMIO, BAR 1 is PIO */
  35. #ifndef SC92031_USE_BAR
  36. #define SC92031_USE_BAR 0
  37. #endif
  38. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast). */
  39. static int multicast_filter_limit = 64;
  40. module_param(multicast_filter_limit, int, 0);
  41. MODULE_PARM_DESC(multicast_filter_limit,
  42. "Maximum number of filtered multicast addresses");
  43. static int media;
  44. module_param(media, int, 0);
  45. MODULE_PARM_DESC(media, "Media type (0x00 = autodetect,"
  46. " 0x01 = 10M half, 0x02 = 10M full,"
  47. " 0x04 = 100M half, 0x08 = 100M full)");
  48. /* Size of the in-memory receive ring. */
  49. #define RX_BUF_LEN_IDX 3 /* 0==8K, 1==16K, 2==32K, 3==64K ,4==128K*/
  50. #define RX_BUF_LEN (8192 << RX_BUF_LEN_IDX)
  51. /* Number of Tx descriptor registers. */
  52. #define NUM_TX_DESC 4
  53. /* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/
  54. #define MAX_ETH_FRAME_SIZE 1536
  55. /* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */
  56. #define TX_BUF_SIZE MAX_ETH_FRAME_SIZE
  57. #define TX_BUF_TOT_LEN (TX_BUF_SIZE * NUM_TX_DESC)
  58. /* The following settings are log_2(bytes)-4: 0 == 16 bytes .. 6==1024, 7==end of packet. */
  59. #define RX_FIFO_THRESH 7 /* Rx buffer level before first PCI xfer. */
  60. /* Time in jiffies before concluding the transmitter is hung. */
  61. #define TX_TIMEOUT (4*HZ)
  62. #define SILAN_STATS_NUM 2 /* number of ETHTOOL_GSTATS */
  63. /* media options */
  64. #define AUTOSELECT 0x00
  65. #define M10_HALF 0x01
  66. #define M10_FULL 0x02
  67. #define M100_HALF 0x04
  68. #define M100_FULL 0x08
  69. /* Symbolic offsets to registers. */
  70. enum silan_registers {
  71. Config0 = 0x00, // Config0
  72. Config1 = 0x04, // Config1
  73. RxBufWPtr = 0x08, // Rx buffer writer poiter
  74. IntrStatus = 0x0C, // Interrupt status
  75. IntrMask = 0x10, // Interrupt mask
  76. RxbufAddr = 0x14, // Rx buffer start address
  77. RxBufRPtr = 0x18, // Rx buffer read pointer
  78. Txstatusall = 0x1C, // Transmit status of all descriptors
  79. TxStatus0 = 0x20, // Transmit status (Four 32bit registers).
  80. TxAddr0 = 0x30, // Tx descriptors (also four 32bit).
  81. RxConfig = 0x40, // Rx configuration
  82. MAC0 = 0x44, // Ethernet hardware address.
  83. MAR0 = 0x4C, // Multicast filter.
  84. RxStatus0 = 0x54, // Rx status
  85. TxConfig = 0x5C, // Tx configuration
  86. PhyCtrl = 0x60, // physical control
  87. FlowCtrlConfig = 0x64, // flow control
  88. Miicmd0 = 0x68, // Mii command0 register
  89. Miicmd1 = 0x6C, // Mii command1 register
  90. Miistatus = 0x70, // Mii status register
  91. Timercnt = 0x74, // Timer counter register
  92. TimerIntr = 0x78, // Timer interrupt register
  93. PMConfig = 0x7C, // Power Manager configuration
  94. CRC0 = 0x80, // Power Manager CRC ( Two 32bit regisers)
  95. Wakeup0 = 0x88, // power Manager wakeup( Eight 64bit regiser)
  96. LSBCRC0 = 0xC8, // power Manager LSBCRC(Two 32bit regiser)
  97. TestD0 = 0xD0,
  98. TestD4 = 0xD4,
  99. TestD8 = 0xD8,
  100. };
  101. #define MII_BMCR 0 // Basic mode control register
  102. #define MII_BMSR 1 // Basic mode status register
  103. #define MII_JAB 16
  104. #define MII_OutputStatus 24
  105. #define BMCR_FULLDPLX 0x0100 // Full duplex
  106. #define BMCR_ANRESTART 0x0200 // Auto negotiation restart
  107. #define BMCR_ANENABLE 0x1000 // Enable auto negotiation
  108. #define BMCR_SPEED100 0x2000 // Select 100Mbps
  109. #define BMSR_LSTATUS 0x0004 // Link status
  110. #define PHY_16_JAB_ENB 0x1000
  111. #define PHY_16_PORT_ENB 0x1
  112. enum IntrStatusBits {
  113. LinkFail = 0x80000000,
  114. LinkOK = 0x40000000,
  115. TimeOut = 0x20000000,
  116. RxOverflow = 0x0040,
  117. RxOK = 0x0020,
  118. TxOK = 0x0001,
  119. IntrBits = LinkFail|LinkOK|TimeOut|RxOverflow|RxOK|TxOK,
  120. };
  121. enum TxStatusBits {
  122. TxCarrierLost = 0x20000000,
  123. TxAborted = 0x10000000,
  124. TxOutOfWindow = 0x08000000,
  125. TxNccShift = 22,
  126. EarlyTxThresShift = 16,
  127. TxStatOK = 0x8000,
  128. TxUnderrun = 0x4000,
  129. TxOwn = 0x2000,
  130. };
  131. enum RxStatusBits {
  132. RxStatesOK = 0x80000,
  133. RxBadAlign = 0x40000,
  134. RxHugeFrame = 0x20000,
  135. RxSmallFrame = 0x10000,
  136. RxCRCOK = 0x8000,
  137. RxCrlFrame = 0x4000,
  138. Rx_Broadcast = 0x2000,
  139. Rx_Multicast = 0x1000,
  140. RxAddrMatch = 0x0800,
  141. MiiErr = 0x0400,
  142. };
  143. enum RxConfigBits {
  144. RxFullDx = 0x80000000,
  145. RxEnb = 0x40000000,
  146. RxSmall = 0x20000000,
  147. RxHuge = 0x10000000,
  148. RxErr = 0x08000000,
  149. RxAllphys = 0x04000000,
  150. RxMulticast = 0x02000000,
  151. RxBroadcast = 0x01000000,
  152. RxLoopBack = (1 << 23) | (1 << 22),
  153. LowThresholdShift = 12,
  154. HighThresholdShift = 2,
  155. };
  156. enum TxConfigBits {
  157. TxFullDx = 0x80000000,
  158. TxEnb = 0x40000000,
  159. TxEnbPad = 0x20000000,
  160. TxEnbHuge = 0x10000000,
  161. TxEnbFCS = 0x08000000,
  162. TxNoBackOff = 0x04000000,
  163. TxEnbPrem = 0x02000000,
  164. TxCareLostCrs = 0x1000000,
  165. TxExdCollNum = 0xf00000,
  166. TxDataRate = 0x80000,
  167. };
  168. enum PhyCtrlconfigbits {
  169. PhyCtrlAne = 0x80000000,
  170. PhyCtrlSpd100 = 0x40000000,
  171. PhyCtrlSpd10 = 0x20000000,
  172. PhyCtrlPhyBaseAddr = 0x1f000000,
  173. PhyCtrlDux = 0x800000,
  174. PhyCtrlReset = 0x400000,
  175. };
  176. enum FlowCtrlConfigBits {
  177. FlowCtrlFullDX = 0x80000000,
  178. FlowCtrlEnb = 0x40000000,
  179. };
  180. enum Config0Bits {
  181. Cfg0_Reset = 0x80000000,
  182. Cfg0_Anaoff = 0x40000000,
  183. Cfg0_LDPS = 0x20000000,
  184. };
  185. enum Config1Bits {
  186. Cfg1_EarlyRx = 1 << 31,
  187. Cfg1_EarlyTx = 1 << 30,
  188. //rx buffer size
  189. Cfg1_Rcv8K = 0x0,
  190. Cfg1_Rcv16K = 0x1,
  191. Cfg1_Rcv32K = 0x3,
  192. Cfg1_Rcv64K = 0x7,
  193. Cfg1_Rcv128K = 0xf,
  194. };
  195. enum MiiCmd0Bits {
  196. Mii_Divider = 0x20000000,
  197. Mii_WRITE = 0x400000,
  198. Mii_READ = 0x200000,
  199. Mii_SCAN = 0x100000,
  200. Mii_Tamod = 0x80000,
  201. Mii_Drvmod = 0x40000,
  202. Mii_mdc = 0x20000,
  203. Mii_mdoen = 0x10000,
  204. Mii_mdo = 0x8000,
  205. Mii_mdi = 0x4000,
  206. };
  207. enum MiiStatusBits {
  208. Mii_StatusBusy = 0x80000000,
  209. };
  210. enum PMConfigBits {
  211. PM_Enable = 1 << 31,
  212. PM_LongWF = 1 << 30,
  213. PM_Magic = 1 << 29,
  214. PM_LANWake = 1 << 28,
  215. PM_LWPTN = (1 << 27 | 1<< 26),
  216. PM_LinkUp = 1 << 25,
  217. PM_WakeUp = 1 << 24,
  218. };
  219. /* Locking rules:
  220. * priv->lock protects most of the fields of priv and most of the
  221. * hardware registers. It does not have to protect against softirqs
  222. * between sc92031_disable_interrupts and sc92031_enable_interrupts;
  223. * it also does not need to be used in ->open and ->stop while the
  224. * device interrupts are off.
  225. * Not having to protect against softirqs is very useful due to heavy
  226. * use of mdelay() at _sc92031_reset.
  227. * Functions prefixed with _sc92031_ must be called with the lock held;
  228. * functions prefixed with sc92031_ must be called without the lock held.
  229. * Use mmiowb() before unlocking if the hardware was written to.
  230. */
  231. /* Locking rules for the interrupt:
  232. * - the interrupt and the tasklet never run at the same time
  233. * - neither run between sc92031_disable_interrupts and
  234. * sc92031_enable_interrupt
  235. */
  236. struct sc92031_priv {
  237. spinlock_t lock;
  238. /* iomap.h cookie */
  239. void __iomem *port_base;
  240. /* pci device structure */
  241. struct pci_dev *pdev;
  242. /* tasklet */
  243. struct tasklet_struct tasklet;
  244. /* CPU address of rx ring */
  245. void *rx_ring;
  246. /* PCI address of rx ring */
  247. dma_addr_t rx_ring_dma_addr;
  248. /* PCI address of rx ring read pointer */
  249. dma_addr_t rx_ring_tail;
  250. /* tx ring write index */
  251. unsigned tx_head;
  252. /* tx ring read index */
  253. unsigned tx_tail;
  254. /* CPU address of tx bounce buffer */
  255. void *tx_bufs;
  256. /* PCI address of tx bounce buffer */
  257. dma_addr_t tx_bufs_dma_addr;
  258. /* copies of some hardware registers */
  259. u32 intr_status;
  260. atomic_t intr_mask;
  261. u32 rx_config;
  262. u32 tx_config;
  263. u32 pm_config;
  264. /* copy of some flags from dev->flags */
  265. unsigned int mc_flags;
  266. /* for ETHTOOL_GSTATS */
  267. u64 tx_timeouts;
  268. u64 rx_loss;
  269. /* for dev->get_stats */
  270. long rx_value;
  271. };
  272. /* I don't know which registers can be safely read; however, I can guess
  273. * MAC0 is one of them. */
  274. static inline void _sc92031_dummy_read(void __iomem *port_base)
  275. {
  276. ioread32(port_base + MAC0);
  277. }
  278. static u32 _sc92031_mii_wait(void __iomem *port_base)
  279. {
  280. u32 mii_status;
  281. do {
  282. udelay(10);
  283. mii_status = ioread32(port_base + Miistatus);
  284. } while (mii_status & Mii_StatusBusy);
  285. return mii_status;
  286. }
  287. static u32 _sc92031_mii_cmd(void __iomem *port_base, u32 cmd0, u32 cmd1)
  288. {
  289. iowrite32(Mii_Divider, port_base + Miicmd0);
  290. _sc92031_mii_wait(port_base);
  291. iowrite32(cmd1, port_base + Miicmd1);
  292. iowrite32(Mii_Divider | cmd0, port_base + Miicmd0);
  293. return _sc92031_mii_wait(port_base);
  294. }
  295. static void _sc92031_mii_scan(void __iomem *port_base)
  296. {
  297. _sc92031_mii_cmd(port_base, Mii_SCAN, 0x1 << 6);
  298. }
  299. static u16 _sc92031_mii_read(void __iomem *port_base, unsigned reg)
  300. {
  301. return _sc92031_mii_cmd(port_base, Mii_READ, reg << 6) >> 13;
  302. }
  303. static void _sc92031_mii_write(void __iomem *port_base, unsigned reg, u16 val)
  304. {
  305. _sc92031_mii_cmd(port_base, Mii_WRITE, (reg << 6) | ((u32)val << 11));
  306. }
  307. static void sc92031_disable_interrupts(struct net_device *dev)
  308. {
  309. struct sc92031_priv *priv = netdev_priv(dev);
  310. void __iomem *port_base = priv->port_base;
  311. /* tell the tasklet/interrupt not to enable interrupts */
  312. atomic_set(&priv->intr_mask, 0);
  313. wmb();
  314. /* stop interrupts */
  315. iowrite32(0, port_base + IntrMask);
  316. _sc92031_dummy_read(port_base);
  317. mmiowb();
  318. /* wait for any concurrent interrupt/tasklet to finish */
  319. synchronize_irq(dev->irq);
  320. tasklet_disable(&priv->tasklet);
  321. }
  322. static void sc92031_enable_interrupts(struct net_device *dev)
  323. {
  324. struct sc92031_priv *priv = netdev_priv(dev);
  325. void __iomem *port_base = priv->port_base;
  326. tasklet_enable(&priv->tasklet);
  327. atomic_set(&priv->intr_mask, IntrBits);
  328. wmb();
  329. iowrite32(IntrBits, port_base + IntrMask);
  330. mmiowb();
  331. }
  332. static void _sc92031_disable_tx_rx(struct net_device *dev)
  333. {
  334. struct sc92031_priv *priv = netdev_priv(dev);
  335. void __iomem *port_base = priv->port_base;
  336. priv->rx_config &= ~RxEnb;
  337. priv->tx_config &= ~TxEnb;
  338. iowrite32(priv->rx_config, port_base + RxConfig);
  339. iowrite32(priv->tx_config, port_base + TxConfig);
  340. }
  341. static void _sc92031_enable_tx_rx(struct net_device *dev)
  342. {
  343. struct sc92031_priv *priv = netdev_priv(dev);
  344. void __iomem *port_base = priv->port_base;
  345. priv->rx_config |= RxEnb;
  346. priv->tx_config |= TxEnb;
  347. iowrite32(priv->rx_config, port_base + RxConfig);
  348. iowrite32(priv->tx_config, port_base + TxConfig);
  349. }
  350. static void _sc92031_tx_clear(struct net_device *dev)
  351. {
  352. struct sc92031_priv *priv = netdev_priv(dev);
  353. while (priv->tx_head - priv->tx_tail > 0) {
  354. priv->tx_tail++;
  355. dev->stats.tx_dropped++;
  356. }
  357. priv->tx_head = priv->tx_tail = 0;
  358. }
  359. static void _sc92031_set_mar(struct net_device *dev)
  360. {
  361. struct sc92031_priv *priv = netdev_priv(dev);
  362. void __iomem *port_base = priv->port_base;
  363. u32 mar0 = 0, mar1 = 0;
  364. if ((dev->flags & IFF_PROMISC) ||
  365. netdev_mc_count(dev) > multicast_filter_limit ||
  366. (dev->flags & IFF_ALLMULTI))
  367. mar0 = mar1 = 0xffffffff;
  368. else if (dev->flags & IFF_MULTICAST) {
  369. struct netdev_hw_addr *ha;
  370. netdev_for_each_mc_addr(ha, dev) {
  371. u32 crc;
  372. unsigned bit = 0;
  373. crc = ~ether_crc(ETH_ALEN, ha->addr);
  374. crc >>= 24;
  375. if (crc & 0x01) bit |= 0x02;
  376. if (crc & 0x02) bit |= 0x01;
  377. if (crc & 0x10) bit |= 0x20;
  378. if (crc & 0x20) bit |= 0x10;
  379. if (crc & 0x40) bit |= 0x08;
  380. if (crc & 0x80) bit |= 0x04;
  381. if (bit > 31)
  382. mar0 |= 0x1 << (bit - 32);
  383. else
  384. mar1 |= 0x1 << bit;
  385. }
  386. }
  387. iowrite32(mar0, port_base + MAR0);
  388. iowrite32(mar1, port_base + MAR0 + 4);
  389. }
  390. static void _sc92031_set_rx_config(struct net_device *dev)
  391. {
  392. struct sc92031_priv *priv = netdev_priv(dev);
  393. void __iomem *port_base = priv->port_base;
  394. unsigned int old_mc_flags;
  395. u32 rx_config_bits = 0;
  396. old_mc_flags = priv->mc_flags;
  397. if (dev->flags & IFF_PROMISC)
  398. rx_config_bits |= RxSmall | RxHuge | RxErr | RxBroadcast
  399. | RxMulticast | RxAllphys;
  400. if (dev->flags & (IFF_ALLMULTI | IFF_MULTICAST))
  401. rx_config_bits |= RxMulticast;
  402. if (dev->flags & IFF_BROADCAST)
  403. rx_config_bits |= RxBroadcast;
  404. priv->rx_config &= ~(RxSmall | RxHuge | RxErr | RxBroadcast
  405. | RxMulticast | RxAllphys);
  406. priv->rx_config |= rx_config_bits;
  407. priv->mc_flags = dev->flags & (IFF_PROMISC | IFF_ALLMULTI
  408. | IFF_MULTICAST | IFF_BROADCAST);
  409. if (netif_carrier_ok(dev) && priv->mc_flags != old_mc_flags)
  410. iowrite32(priv->rx_config, port_base + RxConfig);
  411. }
  412. static bool _sc92031_check_media(struct net_device *dev)
  413. {
  414. struct sc92031_priv *priv = netdev_priv(dev);
  415. void __iomem *port_base = priv->port_base;
  416. u16 bmsr;
  417. bmsr = _sc92031_mii_read(port_base, MII_BMSR);
  418. rmb();
  419. if (bmsr & BMSR_LSTATUS) {
  420. bool speed_100, duplex_full;
  421. u32 flow_ctrl_config = 0;
  422. u16 output_status = _sc92031_mii_read(port_base,
  423. MII_OutputStatus);
  424. _sc92031_mii_scan(port_base);
  425. speed_100 = output_status & 0x2;
  426. duplex_full = output_status & 0x4;
  427. /* Initial Tx/Rx configuration */
  428. priv->rx_config = (0x40 << LowThresholdShift) | (0x1c0 << HighThresholdShift);
  429. priv->tx_config = 0x48800000;
  430. /* NOTE: vendor driver had dead code here to enable tx padding */
  431. if (!speed_100)
  432. priv->tx_config |= 0x80000;
  433. // configure rx mode
  434. _sc92031_set_rx_config(dev);
  435. if (duplex_full) {
  436. priv->rx_config |= RxFullDx;
  437. priv->tx_config |= TxFullDx;
  438. flow_ctrl_config = FlowCtrlFullDX | FlowCtrlEnb;
  439. } else {
  440. priv->rx_config &= ~RxFullDx;
  441. priv->tx_config &= ~TxFullDx;
  442. }
  443. _sc92031_set_mar(dev);
  444. _sc92031_set_rx_config(dev);
  445. _sc92031_enable_tx_rx(dev);
  446. iowrite32(flow_ctrl_config, port_base + FlowCtrlConfig);
  447. netif_carrier_on(dev);
  448. if (printk_ratelimit())
  449. printk(KERN_INFO "%s: link up, %sMbps, %s-duplex\n",
  450. dev->name,
  451. speed_100 ? "100" : "10",
  452. duplex_full ? "full" : "half");
  453. return true;
  454. } else {
  455. _sc92031_mii_scan(port_base);
  456. netif_carrier_off(dev);
  457. _sc92031_disable_tx_rx(dev);
  458. if (printk_ratelimit())
  459. printk(KERN_INFO "%s: link down\n", dev->name);
  460. return false;
  461. }
  462. }
  463. static void _sc92031_phy_reset(struct net_device *dev)
  464. {
  465. struct sc92031_priv *priv = netdev_priv(dev);
  466. void __iomem *port_base = priv->port_base;
  467. u32 phy_ctrl;
  468. phy_ctrl = ioread32(port_base + PhyCtrl);
  469. phy_ctrl &= ~(PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10);
  470. phy_ctrl |= PhyCtrlAne | PhyCtrlReset;
  471. switch (media) {
  472. default:
  473. case AUTOSELECT:
  474. phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10;
  475. break;
  476. case M10_HALF:
  477. phy_ctrl |= PhyCtrlSpd10;
  478. break;
  479. case M10_FULL:
  480. phy_ctrl |= PhyCtrlDux | PhyCtrlSpd10;
  481. break;
  482. case M100_HALF:
  483. phy_ctrl |= PhyCtrlSpd100;
  484. break;
  485. case M100_FULL:
  486. phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100;
  487. break;
  488. }
  489. iowrite32(phy_ctrl, port_base + PhyCtrl);
  490. mdelay(10);
  491. phy_ctrl &= ~PhyCtrlReset;
  492. iowrite32(phy_ctrl, port_base + PhyCtrl);
  493. mdelay(1);
  494. _sc92031_mii_write(port_base, MII_JAB,
  495. PHY_16_JAB_ENB | PHY_16_PORT_ENB);
  496. _sc92031_mii_scan(port_base);
  497. netif_carrier_off(dev);
  498. netif_stop_queue(dev);
  499. }
  500. static void _sc92031_reset(struct net_device *dev)
  501. {
  502. struct sc92031_priv *priv = netdev_priv(dev);
  503. void __iomem *port_base = priv->port_base;
  504. /* disable PM */
  505. iowrite32(0, port_base + PMConfig);
  506. /* soft reset the chip */
  507. iowrite32(Cfg0_Reset, port_base + Config0);
  508. mdelay(200);
  509. iowrite32(0, port_base + Config0);
  510. mdelay(10);
  511. /* disable interrupts */
  512. iowrite32(0, port_base + IntrMask);
  513. /* clear multicast address */
  514. iowrite32(0, port_base + MAR0);
  515. iowrite32(0, port_base + MAR0 + 4);
  516. /* init rx ring */
  517. iowrite32(priv->rx_ring_dma_addr, port_base + RxbufAddr);
  518. priv->rx_ring_tail = priv->rx_ring_dma_addr;
  519. /* init tx ring */
  520. _sc92031_tx_clear(dev);
  521. /* clear old register values */
  522. priv->intr_status = 0;
  523. atomic_set(&priv->intr_mask, 0);
  524. priv->rx_config = 0;
  525. priv->tx_config = 0;
  526. priv->mc_flags = 0;
  527. /* configure rx buffer size */
  528. /* NOTE: vendor driver had dead code here to enable early tx/rx */
  529. iowrite32(Cfg1_Rcv64K, port_base + Config1);
  530. _sc92031_phy_reset(dev);
  531. _sc92031_check_media(dev);
  532. /* calculate rx fifo overflow */
  533. priv->rx_value = 0;
  534. /* enable PM */
  535. iowrite32(priv->pm_config, port_base + PMConfig);
  536. /* clear intr register */
  537. ioread32(port_base + IntrStatus);
  538. }
  539. static void _sc92031_tx_tasklet(struct net_device *dev)
  540. {
  541. struct sc92031_priv *priv = netdev_priv(dev);
  542. void __iomem *port_base = priv->port_base;
  543. unsigned old_tx_tail;
  544. unsigned entry;
  545. u32 tx_status;
  546. old_tx_tail = priv->tx_tail;
  547. while (priv->tx_head - priv->tx_tail > 0) {
  548. entry = priv->tx_tail % NUM_TX_DESC;
  549. tx_status = ioread32(port_base + TxStatus0 + entry * 4);
  550. if (!(tx_status & (TxStatOK | TxUnderrun | TxAborted)))
  551. break;
  552. priv->tx_tail++;
  553. if (tx_status & TxStatOK) {
  554. dev->stats.tx_bytes += tx_status & 0x1fff;
  555. dev->stats.tx_packets++;
  556. /* Note: TxCarrierLost is always asserted at 100mbps. */
  557. dev->stats.collisions += (tx_status >> 22) & 0xf;
  558. }
  559. if (tx_status & (TxOutOfWindow | TxAborted)) {
  560. dev->stats.tx_errors++;
  561. if (tx_status & TxAborted)
  562. dev->stats.tx_aborted_errors++;
  563. if (tx_status & TxCarrierLost)
  564. dev->stats.tx_carrier_errors++;
  565. if (tx_status & TxOutOfWindow)
  566. dev->stats.tx_window_errors++;
  567. }
  568. if (tx_status & TxUnderrun)
  569. dev->stats.tx_fifo_errors++;
  570. }
  571. if (priv->tx_tail != old_tx_tail)
  572. if (netif_queue_stopped(dev))
  573. netif_wake_queue(dev);
  574. }
  575. static void _sc92031_rx_tasklet_error(struct net_device *dev,
  576. u32 rx_status, unsigned rx_size)
  577. {
  578. if(rx_size > (MAX_ETH_FRAME_SIZE + 4) || rx_size < 16) {
  579. dev->stats.rx_errors++;
  580. dev->stats.rx_length_errors++;
  581. }
  582. if (!(rx_status & RxStatesOK)) {
  583. dev->stats.rx_errors++;
  584. if (rx_status & (RxHugeFrame | RxSmallFrame))
  585. dev->stats.rx_length_errors++;
  586. if (rx_status & RxBadAlign)
  587. dev->stats.rx_frame_errors++;
  588. if (!(rx_status & RxCRCOK))
  589. dev->stats.rx_crc_errors++;
  590. } else {
  591. struct sc92031_priv *priv = netdev_priv(dev);
  592. priv->rx_loss++;
  593. }
  594. }
  595. static void _sc92031_rx_tasklet(struct net_device *dev)
  596. {
  597. struct sc92031_priv *priv = netdev_priv(dev);
  598. void __iomem *port_base = priv->port_base;
  599. dma_addr_t rx_ring_head;
  600. unsigned rx_len;
  601. unsigned rx_ring_offset;
  602. void *rx_ring = priv->rx_ring;
  603. rx_ring_head = ioread32(port_base + RxBufWPtr);
  604. rmb();
  605. /* rx_ring_head is only 17 bits in the RxBufWPtr register.
  606. * we need to change it to 32 bits physical address
  607. */
  608. rx_ring_head &= (dma_addr_t)(RX_BUF_LEN - 1);
  609. rx_ring_head |= priv->rx_ring_dma_addr & ~(dma_addr_t)(RX_BUF_LEN - 1);
  610. if (rx_ring_head < priv->rx_ring_dma_addr)
  611. rx_ring_head += RX_BUF_LEN;
  612. if (rx_ring_head >= priv->rx_ring_tail)
  613. rx_len = rx_ring_head - priv->rx_ring_tail;
  614. else
  615. rx_len = RX_BUF_LEN - (priv->rx_ring_tail - rx_ring_head);
  616. if (!rx_len)
  617. return;
  618. if (unlikely(rx_len > RX_BUF_LEN)) {
  619. if (printk_ratelimit())
  620. printk(KERN_ERR "%s: rx packets length > rx buffer\n",
  621. dev->name);
  622. return;
  623. }
  624. rx_ring_offset = (priv->rx_ring_tail - priv->rx_ring_dma_addr) % RX_BUF_LEN;
  625. while (rx_len) {
  626. u32 rx_status;
  627. unsigned rx_size, rx_size_align, pkt_size;
  628. struct sk_buff *skb;
  629. rx_status = le32_to_cpup((__le32 *)(rx_ring + rx_ring_offset));
  630. rmb();
  631. rx_size = rx_status >> 20;
  632. rx_size_align = (rx_size + 3) & ~3; // for 4 bytes aligned
  633. pkt_size = rx_size - 4; // Omit the four octet CRC from the length.
  634. rx_ring_offset = (rx_ring_offset + 4) % RX_BUF_LEN;
  635. if (unlikely(rx_status == 0 ||
  636. rx_size > (MAX_ETH_FRAME_SIZE + 4) ||
  637. rx_size < 16 ||
  638. !(rx_status & RxStatesOK))) {
  639. _sc92031_rx_tasklet_error(dev, rx_status, rx_size);
  640. break;
  641. }
  642. if (unlikely(rx_size_align + 4 > rx_len)) {
  643. if (printk_ratelimit())
  644. printk(KERN_ERR "%s: rx_len is too small\n", dev->name);
  645. break;
  646. }
  647. rx_len -= rx_size_align + 4;
  648. skb = netdev_alloc_skb_ip_align(dev, pkt_size);
  649. if (unlikely(!skb)) {
  650. if (printk_ratelimit())
  651. printk(KERN_ERR "%s: Couldn't allocate a skb_buff for a packet of size %u\n",
  652. dev->name, pkt_size);
  653. goto next;
  654. }
  655. if ((rx_ring_offset + pkt_size) > RX_BUF_LEN) {
  656. memcpy(skb_put(skb, RX_BUF_LEN - rx_ring_offset),
  657. rx_ring + rx_ring_offset, RX_BUF_LEN - rx_ring_offset);
  658. memcpy(skb_put(skb, pkt_size - (RX_BUF_LEN - rx_ring_offset)),
  659. rx_ring, pkt_size - (RX_BUF_LEN - rx_ring_offset));
  660. } else {
  661. memcpy(skb_put(skb, pkt_size), rx_ring + rx_ring_offset, pkt_size);
  662. }
  663. skb->protocol = eth_type_trans(skb, dev);
  664. netif_rx(skb);
  665. dev->stats.rx_bytes += pkt_size;
  666. dev->stats.rx_packets++;
  667. if (rx_status & Rx_Multicast)
  668. dev->stats.multicast++;
  669. next:
  670. rx_ring_offset = (rx_ring_offset + rx_size_align) % RX_BUF_LEN;
  671. }
  672. mb();
  673. priv->rx_ring_tail = rx_ring_head;
  674. iowrite32(priv->rx_ring_tail, port_base + RxBufRPtr);
  675. }
  676. static void _sc92031_link_tasklet(struct net_device *dev)
  677. {
  678. if (_sc92031_check_media(dev))
  679. netif_wake_queue(dev);
  680. else {
  681. netif_stop_queue(dev);
  682. dev->stats.tx_carrier_errors++;
  683. }
  684. }
  685. static void sc92031_tasklet(unsigned long data)
  686. {
  687. struct net_device *dev = (struct net_device *)data;
  688. struct sc92031_priv *priv = netdev_priv(dev);
  689. void __iomem *port_base = priv->port_base;
  690. u32 intr_status, intr_mask;
  691. intr_status = priv->intr_status;
  692. spin_lock(&priv->lock);
  693. if (unlikely(!netif_running(dev)))
  694. goto out;
  695. if (intr_status & TxOK)
  696. _sc92031_tx_tasklet(dev);
  697. if (intr_status & RxOK)
  698. _sc92031_rx_tasklet(dev);
  699. if (intr_status & RxOverflow)
  700. dev->stats.rx_errors++;
  701. if (intr_status & TimeOut) {
  702. dev->stats.rx_errors++;
  703. dev->stats.rx_length_errors++;
  704. }
  705. if (intr_status & (LinkFail | LinkOK))
  706. _sc92031_link_tasklet(dev);
  707. out:
  708. intr_mask = atomic_read(&priv->intr_mask);
  709. rmb();
  710. iowrite32(intr_mask, port_base + IntrMask);
  711. mmiowb();
  712. spin_unlock(&priv->lock);
  713. }
  714. static irqreturn_t sc92031_interrupt(int irq, void *dev_id)
  715. {
  716. struct net_device *dev = dev_id;
  717. struct sc92031_priv *priv = netdev_priv(dev);
  718. void __iomem *port_base = priv->port_base;
  719. u32 intr_status, intr_mask;
  720. /* mask interrupts before clearing IntrStatus */
  721. iowrite32(0, port_base + IntrMask);
  722. _sc92031_dummy_read(port_base);
  723. intr_status = ioread32(port_base + IntrStatus);
  724. if (unlikely(intr_status == 0xffffffff))
  725. return IRQ_NONE; // hardware has gone missing
  726. intr_status &= IntrBits;
  727. if (!intr_status)
  728. goto out_none;
  729. priv->intr_status = intr_status;
  730. tasklet_schedule(&priv->tasklet);
  731. return IRQ_HANDLED;
  732. out_none:
  733. intr_mask = atomic_read(&priv->intr_mask);
  734. rmb();
  735. iowrite32(intr_mask, port_base + IntrMask);
  736. mmiowb();
  737. return IRQ_NONE;
  738. }
  739. static struct net_device_stats *sc92031_get_stats(struct net_device *dev)
  740. {
  741. struct sc92031_priv *priv = netdev_priv(dev);
  742. void __iomem *port_base = priv->port_base;
  743. // FIXME I do not understand what is this trying to do.
  744. if (netif_running(dev)) {
  745. int temp;
  746. spin_lock_bh(&priv->lock);
  747. /* Update the error count. */
  748. temp = (ioread32(port_base + RxStatus0) >> 16) & 0xffff;
  749. if (temp == 0xffff) {
  750. priv->rx_value += temp;
  751. dev->stats.rx_fifo_errors = priv->rx_value;
  752. } else
  753. dev->stats.rx_fifo_errors = temp + priv->rx_value;
  754. spin_unlock_bh(&priv->lock);
  755. }
  756. return &dev->stats;
  757. }
  758. static netdev_tx_t sc92031_start_xmit(struct sk_buff *skb,
  759. struct net_device *dev)
  760. {
  761. struct sc92031_priv *priv = netdev_priv(dev);
  762. void __iomem *port_base = priv->port_base;
  763. unsigned len;
  764. unsigned entry;
  765. u32 tx_status;
  766. if (unlikely(skb->len > TX_BUF_SIZE)) {
  767. dev->stats.tx_dropped++;
  768. goto out;
  769. }
  770. spin_lock(&priv->lock);
  771. if (unlikely(!netif_carrier_ok(dev))) {
  772. dev->stats.tx_dropped++;
  773. goto out_unlock;
  774. }
  775. BUG_ON(priv->tx_head - priv->tx_tail >= NUM_TX_DESC);
  776. entry = priv->tx_head++ % NUM_TX_DESC;
  777. skb_copy_and_csum_dev(skb, priv->tx_bufs + entry * TX_BUF_SIZE);
  778. len = skb->len;
  779. if (len < ETH_ZLEN) {
  780. memset(priv->tx_bufs + entry * TX_BUF_SIZE + len,
  781. 0, ETH_ZLEN - len);
  782. len = ETH_ZLEN;
  783. }
  784. wmb();
  785. if (len < 100)
  786. tx_status = len;
  787. else if (len < 300)
  788. tx_status = 0x30000 | len;
  789. else
  790. tx_status = 0x50000 | len;
  791. iowrite32(priv->tx_bufs_dma_addr + entry * TX_BUF_SIZE,
  792. port_base + TxAddr0 + entry * 4);
  793. iowrite32(tx_status, port_base + TxStatus0 + entry * 4);
  794. mmiowb();
  795. if (priv->tx_head - priv->tx_tail >= NUM_TX_DESC)
  796. netif_stop_queue(dev);
  797. out_unlock:
  798. spin_unlock(&priv->lock);
  799. out:
  800. dev_kfree_skb(skb);
  801. return NETDEV_TX_OK;
  802. }
  803. static int sc92031_open(struct net_device *dev)
  804. {
  805. int err;
  806. struct sc92031_priv *priv = netdev_priv(dev);
  807. struct pci_dev *pdev = priv->pdev;
  808. priv->rx_ring = pci_alloc_consistent(pdev, RX_BUF_LEN,
  809. &priv->rx_ring_dma_addr);
  810. if (unlikely(!priv->rx_ring)) {
  811. err = -ENOMEM;
  812. goto out_alloc_rx_ring;
  813. }
  814. priv->tx_bufs = pci_alloc_consistent(pdev, TX_BUF_TOT_LEN,
  815. &priv->tx_bufs_dma_addr);
  816. if (unlikely(!priv->tx_bufs)) {
  817. err = -ENOMEM;
  818. goto out_alloc_tx_bufs;
  819. }
  820. priv->tx_head = priv->tx_tail = 0;
  821. err = request_irq(pdev->irq, sc92031_interrupt,
  822. IRQF_SHARED, dev->name, dev);
  823. if (unlikely(err < 0))
  824. goto out_request_irq;
  825. priv->pm_config = 0;
  826. /* Interrupts already disabled by sc92031_stop or sc92031_probe */
  827. spin_lock_bh(&priv->lock);
  828. _sc92031_reset(dev);
  829. mmiowb();
  830. spin_unlock_bh(&priv->lock);
  831. sc92031_enable_interrupts(dev);
  832. if (netif_carrier_ok(dev))
  833. netif_start_queue(dev);
  834. else
  835. netif_tx_disable(dev);
  836. return 0;
  837. out_request_irq:
  838. pci_free_consistent(pdev, TX_BUF_TOT_LEN, priv->tx_bufs,
  839. priv->tx_bufs_dma_addr);
  840. out_alloc_tx_bufs:
  841. pci_free_consistent(pdev, RX_BUF_LEN, priv->rx_ring,
  842. priv->rx_ring_dma_addr);
  843. out_alloc_rx_ring:
  844. return err;
  845. }
  846. static int sc92031_stop(struct net_device *dev)
  847. {
  848. struct sc92031_priv *priv = netdev_priv(dev);
  849. struct pci_dev *pdev = priv->pdev;
  850. netif_tx_disable(dev);
  851. /* Disable interrupts, stop Tx and Rx. */
  852. sc92031_disable_interrupts(dev);
  853. spin_lock_bh(&priv->lock);
  854. _sc92031_disable_tx_rx(dev);
  855. _sc92031_tx_clear(dev);
  856. mmiowb();
  857. spin_unlock_bh(&priv->lock);
  858. free_irq(pdev->irq, dev);
  859. pci_free_consistent(pdev, TX_BUF_TOT_LEN, priv->tx_bufs,
  860. priv->tx_bufs_dma_addr);
  861. pci_free_consistent(pdev, RX_BUF_LEN, priv->rx_ring,
  862. priv->rx_ring_dma_addr);
  863. return 0;
  864. }
  865. static void sc92031_set_multicast_list(struct net_device *dev)
  866. {
  867. struct sc92031_priv *priv = netdev_priv(dev);
  868. spin_lock_bh(&priv->lock);
  869. _sc92031_set_mar(dev);
  870. _sc92031_set_rx_config(dev);
  871. mmiowb();
  872. spin_unlock_bh(&priv->lock);
  873. }
  874. static void sc92031_tx_timeout(struct net_device *dev)
  875. {
  876. struct sc92031_priv *priv = netdev_priv(dev);
  877. /* Disable interrupts by clearing the interrupt mask.*/
  878. sc92031_disable_interrupts(dev);
  879. spin_lock(&priv->lock);
  880. priv->tx_timeouts++;
  881. _sc92031_reset(dev);
  882. mmiowb();
  883. spin_unlock(&priv->lock);
  884. /* enable interrupts */
  885. sc92031_enable_interrupts(dev);
  886. if (netif_carrier_ok(dev))
  887. netif_wake_queue(dev);
  888. }
  889. #ifdef CONFIG_NET_POLL_CONTROLLER
  890. static void sc92031_poll_controller(struct net_device *dev)
  891. {
  892. disable_irq(dev->irq);
  893. if (sc92031_interrupt(dev->irq, dev) != IRQ_NONE)
  894. sc92031_tasklet((unsigned long)dev);
  895. enable_irq(dev->irq);
  896. }
  897. #endif
  898. static int sc92031_ethtool_get_settings(struct net_device *dev,
  899. struct ethtool_cmd *cmd)
  900. {
  901. struct sc92031_priv *priv = netdev_priv(dev);
  902. void __iomem *port_base = priv->port_base;
  903. u8 phy_address;
  904. u32 phy_ctrl;
  905. u16 output_status;
  906. spin_lock_bh(&priv->lock);
  907. phy_address = ioread32(port_base + Miicmd1) >> 27;
  908. phy_ctrl = ioread32(port_base + PhyCtrl);
  909. output_status = _sc92031_mii_read(port_base, MII_OutputStatus);
  910. _sc92031_mii_scan(port_base);
  911. mmiowb();
  912. spin_unlock_bh(&priv->lock);
  913. cmd->supported = SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full
  914. | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full
  915. | SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII;
  916. cmd->advertising = ADVERTISED_TP | ADVERTISED_MII;
  917. if ((phy_ctrl & (PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10))
  918. == (PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10))
  919. cmd->advertising |= ADVERTISED_Autoneg;
  920. if ((phy_ctrl & PhyCtrlSpd10) == PhyCtrlSpd10)
  921. cmd->advertising |= ADVERTISED_10baseT_Half;
  922. if ((phy_ctrl & (PhyCtrlSpd10 | PhyCtrlDux))
  923. == (PhyCtrlSpd10 | PhyCtrlDux))
  924. cmd->advertising |= ADVERTISED_10baseT_Full;
  925. if ((phy_ctrl & PhyCtrlSpd100) == PhyCtrlSpd100)
  926. cmd->advertising |= ADVERTISED_100baseT_Half;
  927. if ((phy_ctrl & (PhyCtrlSpd100 | PhyCtrlDux))
  928. == (PhyCtrlSpd100 | PhyCtrlDux))
  929. cmd->advertising |= ADVERTISED_100baseT_Full;
  930. if (phy_ctrl & PhyCtrlAne)
  931. cmd->advertising |= ADVERTISED_Autoneg;
  932. ethtool_cmd_speed_set(cmd,
  933. (output_status & 0x2) ? SPEED_100 : SPEED_10);
  934. cmd->duplex = (output_status & 0x4) ? DUPLEX_FULL : DUPLEX_HALF;
  935. cmd->port = PORT_MII;
  936. cmd->phy_address = phy_address;
  937. cmd->transceiver = XCVR_INTERNAL;
  938. cmd->autoneg = (phy_ctrl & PhyCtrlAne) ? AUTONEG_ENABLE : AUTONEG_DISABLE;
  939. return 0;
  940. }
  941. static int sc92031_ethtool_set_settings(struct net_device *dev,
  942. struct ethtool_cmd *cmd)
  943. {
  944. struct sc92031_priv *priv = netdev_priv(dev);
  945. void __iomem *port_base = priv->port_base;
  946. u32 speed = ethtool_cmd_speed(cmd);
  947. u32 phy_ctrl;
  948. u32 old_phy_ctrl;
  949. if (!(speed == SPEED_10 || speed == SPEED_100))
  950. return -EINVAL;
  951. if (!(cmd->duplex == DUPLEX_HALF || cmd->duplex == DUPLEX_FULL))
  952. return -EINVAL;
  953. if (!(cmd->port == PORT_MII))
  954. return -EINVAL;
  955. if (!(cmd->phy_address == 0x1f))
  956. return -EINVAL;
  957. if (!(cmd->transceiver == XCVR_INTERNAL))
  958. return -EINVAL;
  959. if (!(cmd->autoneg == AUTONEG_DISABLE || cmd->autoneg == AUTONEG_ENABLE))
  960. return -EINVAL;
  961. if (cmd->autoneg == AUTONEG_ENABLE) {
  962. if (!(cmd->advertising & (ADVERTISED_Autoneg
  963. | ADVERTISED_100baseT_Full
  964. | ADVERTISED_100baseT_Half
  965. | ADVERTISED_10baseT_Full
  966. | ADVERTISED_10baseT_Half)))
  967. return -EINVAL;
  968. phy_ctrl = PhyCtrlAne;
  969. // FIXME: I'm not sure what the original code was trying to do
  970. if (cmd->advertising & ADVERTISED_Autoneg)
  971. phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100 | PhyCtrlSpd10;
  972. if (cmd->advertising & ADVERTISED_100baseT_Full)
  973. phy_ctrl |= PhyCtrlDux | PhyCtrlSpd100;
  974. if (cmd->advertising & ADVERTISED_100baseT_Half)
  975. phy_ctrl |= PhyCtrlSpd100;
  976. if (cmd->advertising & ADVERTISED_10baseT_Full)
  977. phy_ctrl |= PhyCtrlSpd10 | PhyCtrlDux;
  978. if (cmd->advertising & ADVERTISED_10baseT_Half)
  979. phy_ctrl |= PhyCtrlSpd10;
  980. } else {
  981. // FIXME: Whole branch guessed
  982. phy_ctrl = 0;
  983. if (speed == SPEED_10)
  984. phy_ctrl |= PhyCtrlSpd10;
  985. else /* cmd->speed == SPEED_100 */
  986. phy_ctrl |= PhyCtrlSpd100;
  987. if (cmd->duplex == DUPLEX_FULL)
  988. phy_ctrl |= PhyCtrlDux;
  989. }
  990. spin_lock_bh(&priv->lock);
  991. old_phy_ctrl = ioread32(port_base + PhyCtrl);
  992. phy_ctrl |= old_phy_ctrl & ~(PhyCtrlAne | PhyCtrlDux
  993. | PhyCtrlSpd100 | PhyCtrlSpd10);
  994. if (phy_ctrl != old_phy_ctrl)
  995. iowrite32(phy_ctrl, port_base + PhyCtrl);
  996. spin_unlock_bh(&priv->lock);
  997. return 0;
  998. }
  999. static void sc92031_ethtool_get_wol(struct net_device *dev,
  1000. struct ethtool_wolinfo *wolinfo)
  1001. {
  1002. struct sc92031_priv *priv = netdev_priv(dev);
  1003. void __iomem *port_base = priv->port_base;
  1004. u32 pm_config;
  1005. spin_lock_bh(&priv->lock);
  1006. pm_config = ioread32(port_base + PMConfig);
  1007. spin_unlock_bh(&priv->lock);
  1008. // FIXME: Guessed
  1009. wolinfo->supported = WAKE_PHY | WAKE_MAGIC
  1010. | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;
  1011. wolinfo->wolopts = 0;
  1012. if (pm_config & PM_LinkUp)
  1013. wolinfo->wolopts |= WAKE_PHY;
  1014. if (pm_config & PM_Magic)
  1015. wolinfo->wolopts |= WAKE_MAGIC;
  1016. if (pm_config & PM_WakeUp)
  1017. // FIXME: Guessed
  1018. wolinfo->wolopts |= WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;
  1019. }
  1020. static int sc92031_ethtool_set_wol(struct net_device *dev,
  1021. struct ethtool_wolinfo *wolinfo)
  1022. {
  1023. struct sc92031_priv *priv = netdev_priv(dev);
  1024. void __iomem *port_base = priv->port_base;
  1025. u32 pm_config;
  1026. spin_lock_bh(&priv->lock);
  1027. pm_config = ioread32(port_base + PMConfig)
  1028. & ~(PM_LinkUp | PM_Magic | PM_WakeUp);
  1029. if (wolinfo->wolopts & WAKE_PHY)
  1030. pm_config |= PM_LinkUp;
  1031. if (wolinfo->wolopts & WAKE_MAGIC)
  1032. pm_config |= PM_Magic;
  1033. // FIXME: Guessed
  1034. if (wolinfo->wolopts & (WAKE_UCAST | WAKE_MCAST | WAKE_BCAST))
  1035. pm_config |= PM_WakeUp;
  1036. priv->pm_config = pm_config;
  1037. iowrite32(pm_config, port_base + PMConfig);
  1038. mmiowb();
  1039. spin_unlock_bh(&priv->lock);
  1040. return 0;
  1041. }
  1042. static int sc92031_ethtool_nway_reset(struct net_device *dev)
  1043. {
  1044. int err = 0;
  1045. struct sc92031_priv *priv = netdev_priv(dev);
  1046. void __iomem *port_base = priv->port_base;
  1047. u16 bmcr;
  1048. spin_lock_bh(&priv->lock);
  1049. bmcr = _sc92031_mii_read(port_base, MII_BMCR);
  1050. if (!(bmcr & BMCR_ANENABLE)) {
  1051. err = -EINVAL;
  1052. goto out;
  1053. }
  1054. _sc92031_mii_write(port_base, MII_BMCR, bmcr | BMCR_ANRESTART);
  1055. out:
  1056. _sc92031_mii_scan(port_base);
  1057. mmiowb();
  1058. spin_unlock_bh(&priv->lock);
  1059. return err;
  1060. }
  1061. static const char sc92031_ethtool_stats_strings[SILAN_STATS_NUM][ETH_GSTRING_LEN] = {
  1062. "tx_timeout",
  1063. "rx_loss",
  1064. };
  1065. static void sc92031_ethtool_get_strings(struct net_device *dev,
  1066. u32 stringset, u8 *data)
  1067. {
  1068. if (stringset == ETH_SS_STATS)
  1069. memcpy(data, sc92031_ethtool_stats_strings,
  1070. SILAN_STATS_NUM * ETH_GSTRING_LEN);
  1071. }
  1072. static int sc92031_ethtool_get_sset_count(struct net_device *dev, int sset)
  1073. {
  1074. switch (sset) {
  1075. case ETH_SS_STATS:
  1076. return SILAN_STATS_NUM;
  1077. default:
  1078. return -EOPNOTSUPP;
  1079. }
  1080. }
  1081. static void sc92031_ethtool_get_ethtool_stats(struct net_device *dev,
  1082. struct ethtool_stats *stats, u64 *data)
  1083. {
  1084. struct sc92031_priv *priv = netdev_priv(dev);
  1085. spin_lock_bh(&priv->lock);
  1086. data[0] = priv->tx_timeouts;
  1087. data[1] = priv->rx_loss;
  1088. spin_unlock_bh(&priv->lock);
  1089. }
  1090. static const struct ethtool_ops sc92031_ethtool_ops = {
  1091. .get_settings = sc92031_ethtool_get_settings,
  1092. .set_settings = sc92031_ethtool_set_settings,
  1093. .get_wol = sc92031_ethtool_get_wol,
  1094. .set_wol = sc92031_ethtool_set_wol,
  1095. .nway_reset = sc92031_ethtool_nway_reset,
  1096. .get_link = ethtool_op_get_link,
  1097. .get_strings = sc92031_ethtool_get_strings,
  1098. .get_sset_count = sc92031_ethtool_get_sset_count,
  1099. .get_ethtool_stats = sc92031_ethtool_get_ethtool_stats,
  1100. };
  1101. static const struct net_device_ops sc92031_netdev_ops = {
  1102. .ndo_get_stats = sc92031_get_stats,
  1103. .ndo_start_xmit = sc92031_start_xmit,
  1104. .ndo_open = sc92031_open,
  1105. .ndo_stop = sc92031_stop,
  1106. .ndo_set_multicast_list = sc92031_set_multicast_list,
  1107. .ndo_change_mtu = eth_change_mtu,
  1108. .ndo_validate_addr = eth_validate_addr,
  1109. .ndo_set_mac_address = eth_mac_addr,
  1110. .ndo_tx_timeout = sc92031_tx_timeout,
  1111. #ifdef CONFIG_NET_POLL_CONTROLLER
  1112. .ndo_poll_controller = sc92031_poll_controller,
  1113. #endif
  1114. };
  1115. static int __devinit sc92031_probe(struct pci_dev *pdev,
  1116. const struct pci_device_id *id)
  1117. {
  1118. int err;
  1119. void __iomem* port_base;
  1120. struct net_device *dev;
  1121. struct sc92031_priv *priv;
  1122. u32 mac0, mac1;
  1123. unsigned long base_addr;
  1124. err = pci_enable_device(pdev);
  1125. if (unlikely(err < 0))
  1126. goto out_enable_device;
  1127. pci_set_master(pdev);
  1128. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  1129. if (unlikely(err < 0))
  1130. goto out_set_dma_mask;
  1131. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  1132. if (unlikely(err < 0))
  1133. goto out_set_dma_mask;
  1134. err = pci_request_regions(pdev, SC92031_NAME);
  1135. if (unlikely(err < 0))
  1136. goto out_request_regions;
  1137. port_base = pci_iomap(pdev, SC92031_USE_BAR, 0);
  1138. if (unlikely(!port_base)) {
  1139. err = -EIO;
  1140. goto out_iomap;
  1141. }
  1142. dev = alloc_etherdev(sizeof(struct sc92031_priv));
  1143. if (unlikely(!dev)) {
  1144. err = -ENOMEM;
  1145. goto out_alloc_etherdev;
  1146. }
  1147. pci_set_drvdata(pdev, dev);
  1148. SET_NETDEV_DEV(dev, &pdev->dev);
  1149. #if SC92031_USE_BAR == 0
  1150. dev->mem_start = pci_resource_start(pdev, SC92031_USE_BAR);
  1151. dev->mem_end = pci_resource_end(pdev, SC92031_USE_BAR);
  1152. #elif SC92031_USE_BAR == 1
  1153. dev->base_addr = pci_resource_start(pdev, SC92031_USE_BAR);
  1154. #endif
  1155. dev->irq = pdev->irq;
  1156. /* faked with skb_copy_and_csum_dev */
  1157. dev->features = NETIF_F_SG | NETIF_F_HIGHDMA |
  1158. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1159. dev->netdev_ops = &sc92031_netdev_ops;
  1160. dev->watchdog_timeo = TX_TIMEOUT;
  1161. dev->ethtool_ops = &sc92031_ethtool_ops;
  1162. priv = netdev_priv(dev);
  1163. spin_lock_init(&priv->lock);
  1164. priv->port_base = port_base;
  1165. priv->pdev = pdev;
  1166. tasklet_init(&priv->tasklet, sc92031_tasklet, (unsigned long)dev);
  1167. /* Fudge tasklet count so the call to sc92031_enable_interrupts at
  1168. * sc92031_open will work correctly */
  1169. tasklet_disable_nosync(&priv->tasklet);
  1170. /* PCI PM Wakeup */
  1171. iowrite32((~PM_LongWF & ~PM_LWPTN) | PM_Enable, port_base + PMConfig);
  1172. mac0 = ioread32(port_base + MAC0);
  1173. mac1 = ioread32(port_base + MAC0 + 4);
  1174. dev->dev_addr[0] = dev->perm_addr[0] = mac0 >> 24;
  1175. dev->dev_addr[1] = dev->perm_addr[1] = mac0 >> 16;
  1176. dev->dev_addr[2] = dev->perm_addr[2] = mac0 >> 8;
  1177. dev->dev_addr[3] = dev->perm_addr[3] = mac0;
  1178. dev->dev_addr[4] = dev->perm_addr[4] = mac1 >> 8;
  1179. dev->dev_addr[5] = dev->perm_addr[5] = mac1;
  1180. err = register_netdev(dev);
  1181. if (err < 0)
  1182. goto out_register_netdev;
  1183. #if SC92031_USE_BAR == 0
  1184. base_addr = dev->mem_start;
  1185. #elif SC92031_USE_BAR == 1
  1186. base_addr = dev->base_addr;
  1187. #endif
  1188. printk(KERN_INFO "%s: SC92031 at 0x%lx, %pM, IRQ %d\n", dev->name,
  1189. base_addr, dev->dev_addr, dev->irq);
  1190. return 0;
  1191. out_register_netdev:
  1192. free_netdev(dev);
  1193. out_alloc_etherdev:
  1194. pci_iounmap(pdev, port_base);
  1195. out_iomap:
  1196. pci_release_regions(pdev);
  1197. out_request_regions:
  1198. out_set_dma_mask:
  1199. pci_disable_device(pdev);
  1200. out_enable_device:
  1201. return err;
  1202. }
  1203. static void __devexit sc92031_remove(struct pci_dev *pdev)
  1204. {
  1205. struct net_device *dev = pci_get_drvdata(pdev);
  1206. struct sc92031_priv *priv = netdev_priv(dev);
  1207. void __iomem* port_base = priv->port_base;
  1208. unregister_netdev(dev);
  1209. free_netdev(dev);
  1210. pci_iounmap(pdev, port_base);
  1211. pci_release_regions(pdev);
  1212. pci_disable_device(pdev);
  1213. }
  1214. static int sc92031_suspend(struct pci_dev *pdev, pm_message_t state)
  1215. {
  1216. struct net_device *dev = pci_get_drvdata(pdev);
  1217. struct sc92031_priv *priv = netdev_priv(dev);
  1218. pci_save_state(pdev);
  1219. if (!netif_running(dev))
  1220. goto out;
  1221. netif_device_detach(dev);
  1222. /* Disable interrupts, stop Tx and Rx. */
  1223. sc92031_disable_interrupts(dev);
  1224. spin_lock_bh(&priv->lock);
  1225. _sc92031_disable_tx_rx(dev);
  1226. _sc92031_tx_clear(dev);
  1227. mmiowb();
  1228. spin_unlock_bh(&priv->lock);
  1229. out:
  1230. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  1231. return 0;
  1232. }
  1233. static int sc92031_resume(struct pci_dev *pdev)
  1234. {
  1235. struct net_device *dev = pci_get_drvdata(pdev);
  1236. struct sc92031_priv *priv = netdev_priv(dev);
  1237. pci_restore_state(pdev);
  1238. pci_set_power_state(pdev, PCI_D0);
  1239. if (!netif_running(dev))
  1240. goto out;
  1241. /* Interrupts already disabled by sc92031_suspend */
  1242. spin_lock_bh(&priv->lock);
  1243. _sc92031_reset(dev);
  1244. mmiowb();
  1245. spin_unlock_bh(&priv->lock);
  1246. sc92031_enable_interrupts(dev);
  1247. netif_device_attach(dev);
  1248. if (netif_carrier_ok(dev))
  1249. netif_wake_queue(dev);
  1250. else
  1251. netif_tx_disable(dev);
  1252. out:
  1253. return 0;
  1254. }
  1255. static DEFINE_PCI_DEVICE_TABLE(sc92031_pci_device_id_table) = {
  1256. { PCI_DEVICE(PCI_VENDOR_ID_SILAN, 0x2031) },
  1257. { PCI_DEVICE(PCI_VENDOR_ID_SILAN, 0x8139) },
  1258. { PCI_DEVICE(0x1088, 0x2031) },
  1259. { 0, }
  1260. };
  1261. MODULE_DEVICE_TABLE(pci, sc92031_pci_device_id_table);
  1262. static struct pci_driver sc92031_pci_driver = {
  1263. .name = SC92031_NAME,
  1264. .id_table = sc92031_pci_device_id_table,
  1265. .probe = sc92031_probe,
  1266. .remove = __devexit_p(sc92031_remove),
  1267. .suspend = sc92031_suspend,
  1268. .resume = sc92031_resume,
  1269. };
  1270. static int __init sc92031_init(void)
  1271. {
  1272. return pci_register_driver(&sc92031_pci_driver);
  1273. }
  1274. static void __exit sc92031_exit(void)
  1275. {
  1276. pci_unregister_driver(&sc92031_pci_driver);
  1277. }
  1278. module_init(sc92031_init);
  1279. module_exit(sc92031_exit);
  1280. MODULE_LICENSE("GPL");
  1281. MODULE_AUTHOR("Cesar Eduardo Barros <cesarb@cesarb.net>");
  1282. MODULE_DESCRIPTION("Silan SC92031 PCI Fast Ethernet Adapter driver");