sc92031.c 39 KB

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