sc92031.c 39 KB

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