lmc_main.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145
  1. /*
  2. * Copyright (c) 1997-2000 LAN Media Corporation (LMC)
  3. * All rights reserved. www.lanmedia.com
  4. * Generic HDLC port Copyright (C) 2008 Krzysztof Halasa <khc@pm.waw.pl>
  5. *
  6. * This code is written by:
  7. * Andrew Stanley-Jones (asj@cban.com)
  8. * Rob Braun (bbraun@vix.com),
  9. * Michael Graff (explorer@vix.com) and
  10. * Matt Thomas (matt@3am-software.com).
  11. *
  12. * With Help By:
  13. * David Boggs
  14. * Ron Crane
  15. * Alan Cox
  16. *
  17. * This software may be used and distributed according to the terms
  18. * of the GNU General Public License version 2, incorporated herein by reference.
  19. *
  20. * Driver for the LanMedia LMC5200, LMC5245, LMC1000, LMC1200 cards.
  21. *
  22. * To control link specific options lmcctl is required.
  23. * It can be obtained from ftp.lanmedia.com.
  24. *
  25. * Linux driver notes:
  26. * Linux uses the device struct lmc_private to pass private information
  27. * arround.
  28. *
  29. * The initialization portion of this driver (the lmc_reset() and the
  30. * lmc_dec_reset() functions, as well as the led controls and the
  31. * lmc_initcsrs() functions.
  32. *
  33. * The watchdog function runs every second and checks to see if
  34. * we still have link, and that the timing source is what we expected
  35. * it to be. If link is lost, the interface is marked down, and
  36. * we no longer can transmit.
  37. *
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/string.h>
  42. #include <linux/timer.h>
  43. #include <linux/ptrace.h>
  44. #include <linux/errno.h>
  45. #include <linux/ioport.h>
  46. #include <linux/slab.h>
  47. #include <linux/interrupt.h>
  48. #include <linux/pci.h>
  49. #include <linux/delay.h>
  50. #include <linux/hdlc.h>
  51. #include <linux/init.h>
  52. #include <linux/in.h>
  53. #include <linux/if_arp.h>
  54. #include <linux/netdevice.h>
  55. #include <linux/etherdevice.h>
  56. #include <linux/skbuff.h>
  57. #include <linux/inet.h>
  58. #include <linux/bitops.h>
  59. #include <asm/processor.h> /* Processor type for cache alignment. */
  60. #include <asm/io.h>
  61. #include <asm/dma.h>
  62. #include <asm/uaccess.h>
  63. //#include <asm/spinlock.h>
  64. #define DRIVER_MAJOR_VERSION 1
  65. #define DRIVER_MINOR_VERSION 34
  66. #define DRIVER_SUB_VERSION 0
  67. #define DRIVER_VERSION ((DRIVER_MAJOR_VERSION << 8) + DRIVER_MINOR_VERSION)
  68. #include "lmc.h"
  69. #include "lmc_var.h"
  70. #include "lmc_ioctl.h"
  71. #include "lmc_debug.h"
  72. #include "lmc_proto.h"
  73. static int LMC_PKT_BUF_SZ = 1542;
  74. static struct pci_device_id lmc_pci_tbl[] = {
  75. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST,
  76. PCI_VENDOR_ID_LMC, PCI_ANY_ID },
  77. { PCI_VENDOR_ID_DEC, PCI_DEVICE_ID_DEC_TULIP_FAST,
  78. PCI_ANY_ID, PCI_VENDOR_ID_LMC },
  79. { 0 }
  80. };
  81. MODULE_DEVICE_TABLE(pci, lmc_pci_tbl);
  82. MODULE_LICENSE("GPL v2");
  83. static int lmc_start_xmit(struct sk_buff *skb, struct net_device *dev);
  84. static int lmc_rx (struct net_device *dev);
  85. static int lmc_open(struct net_device *dev);
  86. static int lmc_close(struct net_device *dev);
  87. static struct net_device_stats *lmc_get_stats(struct net_device *dev);
  88. static irqreturn_t lmc_interrupt(int irq, void *dev_instance);
  89. static void lmc_initcsrs(lmc_softc_t * const sc, lmc_csrptr_t csr_base, size_t csr_size);
  90. static void lmc_softreset(lmc_softc_t * const);
  91. static void lmc_running_reset(struct net_device *dev);
  92. static int lmc_ifdown(struct net_device * const);
  93. static void lmc_watchdog(unsigned long data);
  94. static void lmc_reset(lmc_softc_t * const sc);
  95. static void lmc_dec_reset(lmc_softc_t * const sc);
  96. static void lmc_driver_timeout(struct net_device *dev);
  97. /*
  98. * linux reserves 16 device specific IOCTLs. We call them
  99. * LMCIOC* to control various bits of our world.
  100. */
  101. int lmc_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) /*fold00*/
  102. {
  103. lmc_softc_t *sc = dev_to_sc(dev);
  104. lmc_ctl_t ctl;
  105. int ret = -EOPNOTSUPP;
  106. u16 regVal;
  107. unsigned long flags;
  108. lmc_trace(dev, "lmc_ioctl in");
  109. /*
  110. * Most functions mess with the structure
  111. * Disable interrupts while we do the polling
  112. */
  113. switch (cmd) {
  114. /*
  115. * Return current driver state. Since we keep this up
  116. * To date internally, just copy this out to the user.
  117. */
  118. case LMCIOCGINFO: /*fold01*/
  119. if (copy_to_user(ifr->ifr_data, &sc->ictl, sizeof(lmc_ctl_t)))
  120. ret = -EFAULT;
  121. else
  122. ret = 0;
  123. break;
  124. case LMCIOCSINFO: /*fold01*/
  125. if (!capable(CAP_NET_ADMIN)) {
  126. ret = -EPERM;
  127. break;
  128. }
  129. if(dev->flags & IFF_UP){
  130. ret = -EBUSY;
  131. break;
  132. }
  133. if (copy_from_user(&ctl, ifr->ifr_data, sizeof(lmc_ctl_t))) {
  134. ret = -EFAULT;
  135. break;
  136. }
  137. spin_lock_irqsave(&sc->lmc_lock, flags);
  138. sc->lmc_media->set_status (sc, &ctl);
  139. if(ctl.crc_length != sc->ictl.crc_length) {
  140. sc->lmc_media->set_crc_length(sc, ctl.crc_length);
  141. if (sc->ictl.crc_length == LMC_CTL_CRC_LENGTH_16)
  142. sc->TxDescriptControlInit |= LMC_TDES_ADD_CRC_DISABLE;
  143. else
  144. sc->TxDescriptControlInit &= ~LMC_TDES_ADD_CRC_DISABLE;
  145. }
  146. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  147. ret = 0;
  148. break;
  149. case LMCIOCIFTYPE: /*fold01*/
  150. {
  151. u16 old_type = sc->if_type;
  152. u16 new_type;
  153. if (!capable(CAP_NET_ADMIN)) {
  154. ret = -EPERM;
  155. break;
  156. }
  157. if (copy_from_user(&new_type, ifr->ifr_data, sizeof(u16))) {
  158. ret = -EFAULT;
  159. break;
  160. }
  161. if (new_type == old_type)
  162. {
  163. ret = 0 ;
  164. break; /* no change */
  165. }
  166. spin_lock_irqsave(&sc->lmc_lock, flags);
  167. lmc_proto_close(sc);
  168. sc->if_type = new_type;
  169. lmc_proto_attach(sc);
  170. ret = lmc_proto_open(sc);
  171. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  172. break;
  173. }
  174. case LMCIOCGETXINFO: /*fold01*/
  175. spin_lock_irqsave(&sc->lmc_lock, flags);
  176. sc->lmc_xinfo.Magic0 = 0xBEEFCAFE;
  177. sc->lmc_xinfo.PciCardType = sc->lmc_cardtype;
  178. sc->lmc_xinfo.PciSlotNumber = 0;
  179. sc->lmc_xinfo.DriverMajorVersion = DRIVER_MAJOR_VERSION;
  180. sc->lmc_xinfo.DriverMinorVersion = DRIVER_MINOR_VERSION;
  181. sc->lmc_xinfo.DriverSubVersion = DRIVER_SUB_VERSION;
  182. sc->lmc_xinfo.XilinxRevisionNumber =
  183. lmc_mii_readreg (sc, 0, 3) & 0xf;
  184. sc->lmc_xinfo.MaxFrameSize = LMC_PKT_BUF_SZ;
  185. sc->lmc_xinfo.link_status = sc->lmc_media->get_link_status (sc);
  186. sc->lmc_xinfo.mii_reg16 = lmc_mii_readreg (sc, 0, 16);
  187. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  188. sc->lmc_xinfo.Magic1 = 0xDEADBEEF;
  189. if (copy_to_user(ifr->ifr_data, &sc->lmc_xinfo,
  190. sizeof(struct lmc_xinfo)))
  191. ret = -EFAULT;
  192. else
  193. ret = 0;
  194. break;
  195. case LMCIOCGETLMCSTATS:
  196. spin_lock_irqsave(&sc->lmc_lock, flags);
  197. if (sc->lmc_cardtype == LMC_CARDTYPE_T1) {
  198. lmc_mii_writereg(sc, 0, 17, T1FRAMER_FERR_LSB);
  199. sc->extra_stats.framingBitErrorCount +=
  200. lmc_mii_readreg(sc, 0, 18) & 0xff;
  201. lmc_mii_writereg(sc, 0, 17, T1FRAMER_FERR_MSB);
  202. sc->extra_stats.framingBitErrorCount +=
  203. (lmc_mii_readreg(sc, 0, 18) & 0xff) << 8;
  204. lmc_mii_writereg(sc, 0, 17, T1FRAMER_LCV_LSB);
  205. sc->extra_stats.lineCodeViolationCount +=
  206. lmc_mii_readreg(sc, 0, 18) & 0xff;
  207. lmc_mii_writereg(sc, 0, 17, T1FRAMER_LCV_MSB);
  208. sc->extra_stats.lineCodeViolationCount +=
  209. (lmc_mii_readreg(sc, 0, 18) & 0xff) << 8;
  210. lmc_mii_writereg(sc, 0, 17, T1FRAMER_AERR);
  211. regVal = lmc_mii_readreg(sc, 0, 18) & 0xff;
  212. sc->extra_stats.lossOfFrameCount +=
  213. (regVal & T1FRAMER_LOF_MASK) >> 4;
  214. sc->extra_stats.changeOfFrameAlignmentCount +=
  215. (regVal & T1FRAMER_COFA_MASK) >> 2;
  216. sc->extra_stats.severelyErroredFrameCount +=
  217. regVal & T1FRAMER_SEF_MASK;
  218. }
  219. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  220. if (copy_to_user(ifr->ifr_data, &sc->lmc_device->stats,
  221. sizeof(sc->lmc_device->stats)) ||
  222. copy_to_user(ifr->ifr_data + sizeof(sc->lmc_device->stats),
  223. &sc->extra_stats, sizeof(sc->extra_stats)))
  224. ret = -EFAULT;
  225. else
  226. ret = 0;
  227. break;
  228. case LMCIOCCLEARLMCSTATS:
  229. if (!capable(CAP_NET_ADMIN)) {
  230. ret = -EPERM;
  231. break;
  232. }
  233. spin_lock_irqsave(&sc->lmc_lock, flags);
  234. memset(&sc->lmc_device->stats, 0, sizeof(sc->lmc_device->stats));
  235. memset(&sc->extra_stats, 0, sizeof(sc->extra_stats));
  236. sc->extra_stats.check = STATCHECK;
  237. sc->extra_stats.version_size = (DRIVER_VERSION << 16) +
  238. sizeof(sc->lmc_device->stats) + sizeof(sc->extra_stats);
  239. sc->extra_stats.lmc_cardtype = sc->lmc_cardtype;
  240. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  241. ret = 0;
  242. break;
  243. case LMCIOCSETCIRCUIT: /*fold01*/
  244. if (!capable(CAP_NET_ADMIN)){
  245. ret = -EPERM;
  246. break;
  247. }
  248. if(dev->flags & IFF_UP){
  249. ret = -EBUSY;
  250. break;
  251. }
  252. if (copy_from_user(&ctl, ifr->ifr_data, sizeof(lmc_ctl_t))) {
  253. ret = -EFAULT;
  254. break;
  255. }
  256. spin_lock_irqsave(&sc->lmc_lock, flags);
  257. sc->lmc_media->set_circuit_type(sc, ctl.circuit_type);
  258. sc->ictl.circuit_type = ctl.circuit_type;
  259. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  260. ret = 0;
  261. break;
  262. case LMCIOCRESET: /*fold01*/
  263. if (!capable(CAP_NET_ADMIN)){
  264. ret = -EPERM;
  265. break;
  266. }
  267. spin_lock_irqsave(&sc->lmc_lock, flags);
  268. /* Reset driver and bring back to current state */
  269. printk (" REG16 before reset +%04x\n", lmc_mii_readreg (sc, 0, 16));
  270. lmc_running_reset (dev);
  271. printk (" REG16 after reset +%04x\n", lmc_mii_readreg (sc, 0, 16));
  272. LMC_EVENT_LOG(LMC_EVENT_FORCEDRESET, LMC_CSR_READ (sc, csr_status), lmc_mii_readreg (sc, 0, 16));
  273. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  274. ret = 0;
  275. break;
  276. #ifdef DEBUG
  277. case LMCIOCDUMPEVENTLOG:
  278. if (copy_to_user(ifr->ifr_data, &lmcEventLogIndex, sizeof(u32))) {
  279. ret = -EFAULT;
  280. break;
  281. }
  282. if (copy_to_user(ifr->ifr_data + sizeof(u32), lmcEventLogBuf,
  283. sizeof(lmcEventLogBuf)))
  284. ret = -EFAULT;
  285. else
  286. ret = 0;
  287. break;
  288. #endif /* end ifdef _DBG_EVENTLOG */
  289. case LMCIOCT1CONTROL: /*fold01*/
  290. if (sc->lmc_cardtype != LMC_CARDTYPE_T1){
  291. ret = -EOPNOTSUPP;
  292. break;
  293. }
  294. break;
  295. case LMCIOCXILINX: /*fold01*/
  296. {
  297. struct lmc_xilinx_control xc; /*fold02*/
  298. if (!capable(CAP_NET_ADMIN)){
  299. ret = -EPERM;
  300. break;
  301. }
  302. /*
  303. * Stop the xwitter whlie we restart the hardware
  304. */
  305. netif_stop_queue(dev);
  306. if (copy_from_user(&xc, ifr->ifr_data, sizeof(struct lmc_xilinx_control))) {
  307. ret = -EFAULT;
  308. break;
  309. }
  310. switch(xc.command){
  311. case lmc_xilinx_reset: /*fold02*/
  312. {
  313. u16 mii;
  314. spin_lock_irqsave(&sc->lmc_lock, flags);
  315. mii = lmc_mii_readreg (sc, 0, 16);
  316. /*
  317. * Make all of them 0 and make input
  318. */
  319. lmc_gpio_mkinput(sc, 0xff);
  320. /*
  321. * make the reset output
  322. */
  323. lmc_gpio_mkoutput(sc, LMC_GEP_RESET);
  324. /*
  325. * RESET low to force configuration. This also forces
  326. * the transmitter clock to be internal, but we expect to reset
  327. * that later anyway.
  328. */
  329. sc->lmc_gpio &= ~LMC_GEP_RESET;
  330. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  331. /*
  332. * hold for more than 10 microseconds
  333. */
  334. udelay(50);
  335. sc->lmc_gpio |= LMC_GEP_RESET;
  336. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  337. /*
  338. * stop driving Xilinx-related signals
  339. */
  340. lmc_gpio_mkinput(sc, 0xff);
  341. /* Reset the frammer hardware */
  342. sc->lmc_media->set_link_status (sc, 1);
  343. sc->lmc_media->set_status (sc, NULL);
  344. // lmc_softreset(sc);
  345. {
  346. int i;
  347. for(i = 0; i < 5; i++){
  348. lmc_led_on(sc, LMC_DS3_LED0);
  349. mdelay(100);
  350. lmc_led_off(sc, LMC_DS3_LED0);
  351. lmc_led_on(sc, LMC_DS3_LED1);
  352. mdelay(100);
  353. lmc_led_off(sc, LMC_DS3_LED1);
  354. lmc_led_on(sc, LMC_DS3_LED3);
  355. mdelay(100);
  356. lmc_led_off(sc, LMC_DS3_LED3);
  357. lmc_led_on(sc, LMC_DS3_LED2);
  358. mdelay(100);
  359. lmc_led_off(sc, LMC_DS3_LED2);
  360. }
  361. }
  362. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  363. ret = 0x0;
  364. }
  365. break;
  366. case lmc_xilinx_load_prom: /*fold02*/
  367. {
  368. u16 mii;
  369. int timeout = 500000;
  370. spin_lock_irqsave(&sc->lmc_lock, flags);
  371. mii = lmc_mii_readreg (sc, 0, 16);
  372. /*
  373. * Make all of them 0 and make input
  374. */
  375. lmc_gpio_mkinput(sc, 0xff);
  376. /*
  377. * make the reset output
  378. */
  379. lmc_gpio_mkoutput(sc, LMC_GEP_DP | LMC_GEP_RESET);
  380. /*
  381. * RESET low to force configuration. This also forces
  382. * the transmitter clock to be internal, but we expect to reset
  383. * that later anyway.
  384. */
  385. sc->lmc_gpio &= ~(LMC_GEP_RESET | LMC_GEP_DP);
  386. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  387. /*
  388. * hold for more than 10 microseconds
  389. */
  390. udelay(50);
  391. sc->lmc_gpio |= LMC_GEP_DP | LMC_GEP_RESET;
  392. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  393. /*
  394. * busy wait for the chip to reset
  395. */
  396. while( (LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0 &&
  397. (timeout-- > 0))
  398. cpu_relax();
  399. /*
  400. * stop driving Xilinx-related signals
  401. */
  402. lmc_gpio_mkinput(sc, 0xff);
  403. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  404. ret = 0x0;
  405. break;
  406. }
  407. case lmc_xilinx_load: /*fold02*/
  408. {
  409. char *data;
  410. int pos;
  411. int timeout = 500000;
  412. if (!xc.data) {
  413. ret = -EINVAL;
  414. break;
  415. }
  416. data = kmalloc(xc.len, GFP_KERNEL);
  417. if (!data) {
  418. printk(KERN_WARNING "%s: Failed to allocate memory for copy\n", dev->name);
  419. ret = -ENOMEM;
  420. break;
  421. }
  422. if(copy_from_user(data, xc.data, xc.len))
  423. {
  424. kfree(data);
  425. ret = -ENOMEM;
  426. break;
  427. }
  428. printk("%s: Starting load of data Len: %d at 0x%p == 0x%p\n", dev->name, xc.len, xc.data, data);
  429. spin_lock_irqsave(&sc->lmc_lock, flags);
  430. lmc_gpio_mkinput(sc, 0xff);
  431. /*
  432. * Clear the Xilinx and start prgramming from the DEC
  433. */
  434. /*
  435. * Set ouput as:
  436. * Reset: 0 (active)
  437. * DP: 0 (active)
  438. * Mode: 1
  439. *
  440. */
  441. sc->lmc_gpio = 0x00;
  442. sc->lmc_gpio &= ~LMC_GEP_DP;
  443. sc->lmc_gpio &= ~LMC_GEP_RESET;
  444. sc->lmc_gpio |= LMC_GEP_MODE;
  445. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  446. lmc_gpio_mkoutput(sc, LMC_GEP_MODE | LMC_GEP_DP | LMC_GEP_RESET);
  447. /*
  448. * Wait at least 10 us 20 to be safe
  449. */
  450. udelay(50);
  451. /*
  452. * Clear reset and activate programming lines
  453. * Reset: Input
  454. * DP: Input
  455. * Clock: Output
  456. * Data: Output
  457. * Mode: Output
  458. */
  459. lmc_gpio_mkinput(sc, LMC_GEP_DP | LMC_GEP_RESET);
  460. /*
  461. * Set LOAD, DATA, Clock to 1
  462. */
  463. sc->lmc_gpio = 0x00;
  464. sc->lmc_gpio |= LMC_GEP_MODE;
  465. sc->lmc_gpio |= LMC_GEP_DATA;
  466. sc->lmc_gpio |= LMC_GEP_CLK;
  467. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  468. lmc_gpio_mkoutput(sc, LMC_GEP_DATA | LMC_GEP_CLK | LMC_GEP_MODE );
  469. /*
  470. * busy wait for the chip to reset
  471. */
  472. while( (LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0 &&
  473. (timeout-- > 0))
  474. cpu_relax();
  475. printk(KERN_DEBUG "%s: Waited %d for the Xilinx to clear it's memory\n", dev->name, 500000-timeout);
  476. for(pos = 0; pos < xc.len; pos++){
  477. switch(data[pos]){
  478. case 0:
  479. sc->lmc_gpio &= ~LMC_GEP_DATA; /* Data is 0 */
  480. break;
  481. case 1:
  482. sc->lmc_gpio |= LMC_GEP_DATA; /* Data is 1 */
  483. break;
  484. default:
  485. printk(KERN_WARNING "%s Bad data in xilinx programming data at %d, got %d wanted 0 or 1\n", dev->name, pos, data[pos]);
  486. sc->lmc_gpio |= LMC_GEP_DATA; /* Assume it's 1 */
  487. }
  488. sc->lmc_gpio &= ~LMC_GEP_CLK; /* Clock to zero */
  489. sc->lmc_gpio |= LMC_GEP_MODE;
  490. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  491. udelay(1);
  492. sc->lmc_gpio |= LMC_GEP_CLK; /* Put the clack back to one */
  493. sc->lmc_gpio |= LMC_GEP_MODE;
  494. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  495. udelay(1);
  496. }
  497. if((LMC_CSR_READ(sc, csr_gp) & LMC_GEP_INIT) == 0){
  498. printk(KERN_WARNING "%s: Reprogramming FAILED. Needs to be reprogrammed. (corrupted data)\n", dev->name);
  499. }
  500. else if((LMC_CSR_READ(sc, csr_gp) & LMC_GEP_DP) == 0){
  501. printk(KERN_WARNING "%s: Reprogramming FAILED. Needs to be reprogrammed. (done)\n", dev->name);
  502. }
  503. else {
  504. printk(KERN_DEBUG "%s: Done reprogramming Xilinx, %d bits, good luck!\n", dev->name, pos);
  505. }
  506. lmc_gpio_mkinput(sc, 0xff);
  507. sc->lmc_miireg16 |= LMC_MII16_FIFO_RESET;
  508. lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
  509. sc->lmc_miireg16 &= ~LMC_MII16_FIFO_RESET;
  510. lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
  511. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  512. kfree(data);
  513. ret = 0;
  514. break;
  515. }
  516. default: /*fold02*/
  517. ret = -EBADE;
  518. break;
  519. }
  520. netif_wake_queue(dev);
  521. sc->lmc_txfull = 0;
  522. }
  523. break;
  524. default: /*fold01*/
  525. /* If we don't know what to do, give the protocol a shot. */
  526. ret = lmc_proto_ioctl (sc, ifr, cmd);
  527. break;
  528. }
  529. lmc_trace(dev, "lmc_ioctl out");
  530. return ret;
  531. }
  532. /* the watchdog process that cruises around */
  533. static void lmc_watchdog (unsigned long data) /*fold00*/
  534. {
  535. struct net_device *dev = (struct net_device *)data;
  536. lmc_softc_t *sc = dev_to_sc(dev);
  537. int link_status;
  538. u32 ticks;
  539. unsigned long flags;
  540. lmc_trace(dev, "lmc_watchdog in");
  541. spin_lock_irqsave(&sc->lmc_lock, flags);
  542. if(sc->check != 0xBEAFCAFE){
  543. printk("LMC: Corrupt net_device struct, breaking out\n");
  544. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  545. return;
  546. }
  547. /* Make sure the tx jabber and rx watchdog are off,
  548. * and the transmit and receive processes are running.
  549. */
  550. LMC_CSR_WRITE (sc, csr_15, 0x00000011);
  551. sc->lmc_cmdmode |= TULIP_CMD_TXRUN | TULIP_CMD_RXRUN;
  552. LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
  553. if (sc->lmc_ok == 0)
  554. goto kick_timer;
  555. LMC_EVENT_LOG(LMC_EVENT_WATCHDOG, LMC_CSR_READ (sc, csr_status), lmc_mii_readreg (sc, 0, 16));
  556. /* --- begin time out check -----------------------------------
  557. * check for a transmit interrupt timeout
  558. * Has the packet xmt vs xmt serviced threshold been exceeded */
  559. if (sc->lmc_taint_tx == sc->lastlmc_taint_tx &&
  560. sc->lmc_device->stats.tx_packets > sc->lasttx_packets &&
  561. sc->tx_TimeoutInd == 0)
  562. {
  563. /* wait for the watchdog to come around again */
  564. sc->tx_TimeoutInd = 1;
  565. }
  566. else if (sc->lmc_taint_tx == sc->lastlmc_taint_tx &&
  567. sc->lmc_device->stats.tx_packets > sc->lasttx_packets &&
  568. sc->tx_TimeoutInd)
  569. {
  570. LMC_EVENT_LOG(LMC_EVENT_XMTINTTMO, LMC_CSR_READ (sc, csr_status), 0);
  571. sc->tx_TimeoutDisplay = 1;
  572. sc->extra_stats.tx_TimeoutCnt++;
  573. /* DEC chip is stuck, hit it with a RESET!!!! */
  574. lmc_running_reset (dev);
  575. /* look at receive & transmit process state to make sure they are running */
  576. LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
  577. /* look at: DSR - 02 for Reg 16
  578. * CTS - 08
  579. * DCD - 10
  580. * RI - 20
  581. * for Reg 17
  582. */
  583. LMC_EVENT_LOG(LMC_EVENT_RESET2, lmc_mii_readreg (sc, 0, 16), lmc_mii_readreg (sc, 0, 17));
  584. /* reset the transmit timeout detection flag */
  585. sc->tx_TimeoutInd = 0;
  586. sc->lastlmc_taint_tx = sc->lmc_taint_tx;
  587. sc->lasttx_packets = sc->lmc_device->stats.tx_packets;
  588. } else {
  589. sc->tx_TimeoutInd = 0;
  590. sc->lastlmc_taint_tx = sc->lmc_taint_tx;
  591. sc->lasttx_packets = sc->lmc_device->stats.tx_packets;
  592. }
  593. /* --- end time out check ----------------------------------- */
  594. link_status = sc->lmc_media->get_link_status (sc);
  595. /*
  596. * hardware level link lost, but the interface is marked as up.
  597. * Mark it as down.
  598. */
  599. if ((link_status == 0) && (sc->last_link_status != 0)) {
  600. printk(KERN_WARNING "%s: hardware/physical link down\n", dev->name);
  601. sc->last_link_status = 0;
  602. /* lmc_reset (sc); Why reset??? The link can go down ok */
  603. /* Inform the world that link has been lost */
  604. netif_carrier_off(dev);
  605. }
  606. /*
  607. * hardware link is up, but the interface is marked as down.
  608. * Bring it back up again.
  609. */
  610. if (link_status != 0 && sc->last_link_status == 0) {
  611. printk(KERN_WARNING "%s: hardware/physical link up\n", dev->name);
  612. sc->last_link_status = 1;
  613. /* lmc_reset (sc); Again why reset??? */
  614. netif_carrier_on(dev);
  615. }
  616. /* Call media specific watchdog functions */
  617. sc->lmc_media->watchdog(sc);
  618. /*
  619. * Poke the transmitter to make sure it
  620. * never stops, even if we run out of mem
  621. */
  622. LMC_CSR_WRITE(sc, csr_rxpoll, 0);
  623. /*
  624. * Check for code that failed
  625. * and try and fix it as appropriate
  626. */
  627. if(sc->failed_ring == 1){
  628. /*
  629. * Failed to setup the recv/xmit rin
  630. * Try again
  631. */
  632. sc->failed_ring = 0;
  633. lmc_softreset(sc);
  634. }
  635. if(sc->failed_recv_alloc == 1){
  636. /*
  637. * We failed to alloc mem in the
  638. * interrupt handler, go through the rings
  639. * and rebuild them
  640. */
  641. sc->failed_recv_alloc = 0;
  642. lmc_softreset(sc);
  643. }
  644. /*
  645. * remember the timer value
  646. */
  647. kick_timer:
  648. ticks = LMC_CSR_READ (sc, csr_gp_timer);
  649. LMC_CSR_WRITE (sc, csr_gp_timer, 0xffffffffUL);
  650. sc->ictl.ticks = 0x0000ffff - (ticks & 0x0000ffff);
  651. /*
  652. * restart this timer.
  653. */
  654. sc->timer.expires = jiffies + (HZ);
  655. add_timer (&sc->timer);
  656. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  657. lmc_trace(dev, "lmc_watchdog out");
  658. }
  659. static int lmc_attach(struct net_device *dev, unsigned short encoding,
  660. unsigned short parity)
  661. {
  662. if (encoding == ENCODING_NRZ && parity == PARITY_CRC16_PR1_CCITT)
  663. return 0;
  664. return -EINVAL;
  665. }
  666. static const struct net_device_ops lmc_ops = {
  667. .ndo_open = lmc_open,
  668. .ndo_stop = lmc_close,
  669. .ndo_change_mtu = hdlc_change_mtu,
  670. .ndo_start_xmit = hdlc_start_xmit,
  671. .ndo_do_ioctl = lmc_ioctl,
  672. .ndo_tx_timeout = lmc_driver_timeout,
  673. .ndo_get_stats = lmc_get_stats,
  674. };
  675. static int __devinit lmc_init_one(struct pci_dev *pdev,
  676. const struct pci_device_id *ent)
  677. {
  678. lmc_softc_t *sc;
  679. struct net_device *dev;
  680. u16 subdevice;
  681. u16 AdapModelNum;
  682. int err;
  683. static int cards_found;
  684. /* lmc_trace(dev, "lmc_init_one in"); */
  685. err = pci_enable_device(pdev);
  686. if (err) {
  687. printk(KERN_ERR "lmc: pci enable failed: %d\n", err);
  688. return err;
  689. }
  690. err = pci_request_regions(pdev, "lmc");
  691. if (err) {
  692. printk(KERN_ERR "lmc: pci_request_region failed\n");
  693. goto err_req_io;
  694. }
  695. /*
  696. * Allocate our own device structure
  697. */
  698. sc = kzalloc(sizeof(lmc_softc_t), GFP_KERNEL);
  699. if (!sc) {
  700. err = -ENOMEM;
  701. goto err_kzalloc;
  702. }
  703. dev = alloc_hdlcdev(sc);
  704. if (!dev) {
  705. printk(KERN_ERR "lmc:alloc_netdev for device failed\n");
  706. goto err_hdlcdev;
  707. }
  708. dev->type = ARPHRD_HDLC;
  709. dev_to_hdlc(dev)->xmit = lmc_start_xmit;
  710. dev_to_hdlc(dev)->attach = lmc_attach;
  711. dev->netdev_ops = &lmc_ops;
  712. dev->watchdog_timeo = HZ; /* 1 second */
  713. dev->tx_queue_len = 100;
  714. sc->lmc_device = dev;
  715. sc->name = dev->name;
  716. sc->if_type = LMC_PPP;
  717. sc->check = 0xBEAFCAFE;
  718. dev->base_addr = pci_resource_start(pdev, 0);
  719. dev->irq = pdev->irq;
  720. pci_set_drvdata(pdev, dev);
  721. SET_NETDEV_DEV(dev, &pdev->dev);
  722. /*
  723. * This will get the protocol layer ready and do any 1 time init's
  724. * Must have a valid sc and dev structure
  725. */
  726. lmc_proto_attach(sc);
  727. /* Init the spin lock so can call it latter */
  728. spin_lock_init(&sc->lmc_lock);
  729. pci_set_master(pdev);
  730. printk(KERN_INFO "%s: detected at %lx, irq %d\n", dev->name,
  731. dev->base_addr, dev->irq);
  732. err = register_hdlc_device(dev);
  733. if (err) {
  734. printk(KERN_ERR "%s: register_netdev failed.\n", dev->name);
  735. free_netdev(dev);
  736. goto err_hdlcdev;
  737. }
  738. sc->lmc_cardtype = LMC_CARDTYPE_UNKNOWN;
  739. sc->lmc_timing = LMC_CTL_CLOCK_SOURCE_EXT;
  740. /*
  741. *
  742. * Check either the subvendor or the subdevice, some systems reverse
  743. * the setting in the bois, seems to be version and arch dependent?
  744. * Fix the error, exchange the two values
  745. */
  746. if ((subdevice = pdev->subsystem_device) == PCI_VENDOR_ID_LMC)
  747. subdevice = pdev->subsystem_vendor;
  748. switch (subdevice) {
  749. case PCI_DEVICE_ID_LMC_HSSI:
  750. printk(KERN_INFO "%s: LMC HSSI\n", dev->name);
  751. sc->lmc_cardtype = LMC_CARDTYPE_HSSI;
  752. sc->lmc_media = &lmc_hssi_media;
  753. break;
  754. case PCI_DEVICE_ID_LMC_DS3:
  755. printk(KERN_INFO "%s: LMC DS3\n", dev->name);
  756. sc->lmc_cardtype = LMC_CARDTYPE_DS3;
  757. sc->lmc_media = &lmc_ds3_media;
  758. break;
  759. case PCI_DEVICE_ID_LMC_SSI:
  760. printk(KERN_INFO "%s: LMC SSI\n", dev->name);
  761. sc->lmc_cardtype = LMC_CARDTYPE_SSI;
  762. sc->lmc_media = &lmc_ssi_media;
  763. break;
  764. case PCI_DEVICE_ID_LMC_T1:
  765. printk(KERN_INFO "%s: LMC T1\n", dev->name);
  766. sc->lmc_cardtype = LMC_CARDTYPE_T1;
  767. sc->lmc_media = &lmc_t1_media;
  768. break;
  769. default:
  770. printk(KERN_WARNING "%s: LMC UNKOWN CARD!\n", dev->name);
  771. break;
  772. }
  773. lmc_initcsrs (sc, dev->base_addr, 8);
  774. lmc_gpio_mkinput (sc, 0xff);
  775. sc->lmc_gpio = 0; /* drive no signals yet */
  776. sc->lmc_media->defaults (sc);
  777. sc->lmc_media->set_link_status (sc, LMC_LINK_UP);
  778. /* verify that the PCI Sub System ID matches the Adapter Model number
  779. * from the MII register
  780. */
  781. AdapModelNum = (lmc_mii_readreg (sc, 0, 3) & 0x3f0) >> 4;
  782. if ((AdapModelNum != LMC_ADAP_T1 || /* detect LMC1200 */
  783. subdevice != PCI_DEVICE_ID_LMC_T1) &&
  784. (AdapModelNum != LMC_ADAP_SSI || /* detect LMC1000 */
  785. subdevice != PCI_DEVICE_ID_LMC_SSI) &&
  786. (AdapModelNum != LMC_ADAP_DS3 || /* detect LMC5245 */
  787. subdevice != PCI_DEVICE_ID_LMC_DS3) &&
  788. (AdapModelNum != LMC_ADAP_HSSI || /* detect LMC5200 */
  789. subdevice != PCI_DEVICE_ID_LMC_HSSI))
  790. printk(KERN_WARNING "%s: Model number (%d) miscompare for PCI"
  791. " Subsystem ID = 0x%04x\n",
  792. dev->name, AdapModelNum, subdevice);
  793. /*
  794. * reset clock
  795. */
  796. LMC_CSR_WRITE (sc, csr_gp_timer, 0xFFFFFFFFUL);
  797. sc->board_idx = cards_found++;
  798. sc->extra_stats.check = STATCHECK;
  799. sc->extra_stats.version_size = (DRIVER_VERSION << 16) +
  800. sizeof(sc->lmc_device->stats) + sizeof(sc->extra_stats);
  801. sc->extra_stats.lmc_cardtype = sc->lmc_cardtype;
  802. sc->lmc_ok = 0;
  803. sc->last_link_status = 0;
  804. lmc_trace(dev, "lmc_init_one out");
  805. return 0;
  806. err_hdlcdev:
  807. pci_set_drvdata(pdev, NULL);
  808. kfree(sc);
  809. err_kzalloc:
  810. pci_release_regions(pdev);
  811. err_req_io:
  812. pci_disable_device(pdev);
  813. return err;
  814. }
  815. /*
  816. * Called from pci when removing module.
  817. */
  818. static void __devexit lmc_remove_one(struct pci_dev *pdev)
  819. {
  820. struct net_device *dev = pci_get_drvdata(pdev);
  821. if (dev) {
  822. printk(KERN_DEBUG "%s: removing...\n", dev->name);
  823. unregister_hdlc_device(dev);
  824. free_netdev(dev);
  825. pci_release_regions(pdev);
  826. pci_disable_device(pdev);
  827. pci_set_drvdata(pdev, NULL);
  828. }
  829. }
  830. /* After this is called, packets can be sent.
  831. * Does not initialize the addresses
  832. */
  833. static int lmc_open(struct net_device *dev)
  834. {
  835. lmc_softc_t *sc = dev_to_sc(dev);
  836. int err;
  837. lmc_trace(dev, "lmc_open in");
  838. lmc_led_on(sc, LMC_DS3_LED0);
  839. lmc_dec_reset(sc);
  840. lmc_reset(sc);
  841. LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ(sc, csr_status), 0);
  842. LMC_EVENT_LOG(LMC_EVENT_RESET2, lmc_mii_readreg(sc, 0, 16),
  843. lmc_mii_readreg(sc, 0, 17));
  844. if (sc->lmc_ok){
  845. lmc_trace(dev, "lmc_open lmc_ok out");
  846. return (0);
  847. }
  848. lmc_softreset (sc);
  849. /* Since we have to use PCI bus, this should work on x86,alpha,ppc */
  850. if (request_irq (dev->irq, &lmc_interrupt, IRQF_SHARED, dev->name, dev)){
  851. printk(KERN_WARNING "%s: could not get irq: %d\n", dev->name, dev->irq);
  852. lmc_trace(dev, "lmc_open irq failed out");
  853. return -EAGAIN;
  854. }
  855. sc->got_irq = 1;
  856. /* Assert Terminal Active */
  857. sc->lmc_miireg16 |= LMC_MII16_LED_ALL;
  858. sc->lmc_media->set_link_status (sc, LMC_LINK_UP);
  859. /*
  860. * reset to last state.
  861. */
  862. sc->lmc_media->set_status (sc, NULL);
  863. /* setup default bits to be used in tulip_desc_t transmit descriptor
  864. * -baz */
  865. sc->TxDescriptControlInit = (
  866. LMC_TDES_INTERRUPT_ON_COMPLETION
  867. | LMC_TDES_FIRST_SEGMENT
  868. | LMC_TDES_LAST_SEGMENT
  869. | LMC_TDES_SECOND_ADDR_CHAINED
  870. | LMC_TDES_DISABLE_PADDING
  871. );
  872. if (sc->ictl.crc_length == LMC_CTL_CRC_LENGTH_16) {
  873. /* disable 32 bit CRC generated by ASIC */
  874. sc->TxDescriptControlInit |= LMC_TDES_ADD_CRC_DISABLE;
  875. }
  876. sc->lmc_media->set_crc_length(sc, sc->ictl.crc_length);
  877. /* Acknoledge the Terminal Active and light LEDs */
  878. /* dev->flags |= IFF_UP; */
  879. if ((err = lmc_proto_open(sc)) != 0)
  880. return err;
  881. netif_start_queue(dev);
  882. sc->extra_stats.tx_tbusy0++;
  883. /*
  884. * select what interrupts we want to get
  885. */
  886. sc->lmc_intrmask = 0;
  887. /* Should be using the default interrupt mask defined in the .h file. */
  888. sc->lmc_intrmask |= (TULIP_STS_NORMALINTR
  889. | TULIP_STS_RXINTR
  890. | TULIP_STS_TXINTR
  891. | TULIP_STS_ABNRMLINTR
  892. | TULIP_STS_SYSERROR
  893. | TULIP_STS_TXSTOPPED
  894. | TULIP_STS_TXUNDERFLOW
  895. | TULIP_STS_RXSTOPPED
  896. | TULIP_STS_RXNOBUF
  897. );
  898. LMC_CSR_WRITE (sc, csr_intr, sc->lmc_intrmask);
  899. sc->lmc_cmdmode |= TULIP_CMD_TXRUN;
  900. sc->lmc_cmdmode |= TULIP_CMD_RXRUN;
  901. LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
  902. sc->lmc_ok = 1; /* Run watchdog */
  903. /*
  904. * Set the if up now - pfb
  905. */
  906. sc->last_link_status = 1;
  907. /*
  908. * Setup a timer for the watchdog on probe, and start it running.
  909. * Since lmc_ok == 0, it will be a NOP for now.
  910. */
  911. init_timer (&sc->timer);
  912. sc->timer.expires = jiffies + HZ;
  913. sc->timer.data = (unsigned long) dev;
  914. sc->timer.function = &lmc_watchdog;
  915. add_timer (&sc->timer);
  916. lmc_trace(dev, "lmc_open out");
  917. return (0);
  918. }
  919. /* Total reset to compensate for the AdTran DSU doing bad things
  920. * under heavy load
  921. */
  922. static void lmc_running_reset (struct net_device *dev) /*fold00*/
  923. {
  924. lmc_softc_t *sc = dev_to_sc(dev);
  925. lmc_trace(dev, "lmc_runnig_reset in");
  926. /* stop interrupts */
  927. /* Clear the interrupt mask */
  928. LMC_CSR_WRITE (sc, csr_intr, 0x00000000);
  929. lmc_dec_reset (sc);
  930. lmc_reset (sc);
  931. lmc_softreset (sc);
  932. /* sc->lmc_miireg16 |= LMC_MII16_LED_ALL; */
  933. sc->lmc_media->set_link_status (sc, 1);
  934. sc->lmc_media->set_status (sc, NULL);
  935. netif_wake_queue(dev);
  936. sc->lmc_txfull = 0;
  937. sc->extra_stats.tx_tbusy0++;
  938. sc->lmc_intrmask = TULIP_DEFAULT_INTR_MASK;
  939. LMC_CSR_WRITE (sc, csr_intr, sc->lmc_intrmask);
  940. sc->lmc_cmdmode |= (TULIP_CMD_TXRUN | TULIP_CMD_RXRUN);
  941. LMC_CSR_WRITE (sc, csr_command, sc->lmc_cmdmode);
  942. lmc_trace(dev, "lmc_runnin_reset_out");
  943. }
  944. /* This is what is called when you ifconfig down a device.
  945. * This disables the timer for the watchdog and keepalives,
  946. * and disables the irq for dev.
  947. */
  948. static int lmc_close(struct net_device *dev)
  949. {
  950. /* not calling release_region() as we should */
  951. lmc_softc_t *sc = dev_to_sc(dev);
  952. lmc_trace(dev, "lmc_close in");
  953. sc->lmc_ok = 0;
  954. sc->lmc_media->set_link_status (sc, 0);
  955. del_timer (&sc->timer);
  956. lmc_proto_close(sc);
  957. lmc_ifdown (dev);
  958. lmc_trace(dev, "lmc_close out");
  959. return 0;
  960. }
  961. /* Ends the transfer of packets */
  962. /* When the interface goes down, this is called */
  963. static int lmc_ifdown (struct net_device *dev) /*fold00*/
  964. {
  965. lmc_softc_t *sc = dev_to_sc(dev);
  966. u32 csr6;
  967. int i;
  968. lmc_trace(dev, "lmc_ifdown in");
  969. /* Don't let anything else go on right now */
  970. // dev->start = 0;
  971. netif_stop_queue(dev);
  972. sc->extra_stats.tx_tbusy1++;
  973. /* stop interrupts */
  974. /* Clear the interrupt mask */
  975. LMC_CSR_WRITE (sc, csr_intr, 0x00000000);
  976. /* Stop Tx and Rx on the chip */
  977. csr6 = LMC_CSR_READ (sc, csr_command);
  978. csr6 &= ~LMC_DEC_ST; /* Turn off the Transmission bit */
  979. csr6 &= ~LMC_DEC_SR; /* Turn off the Receive bit */
  980. LMC_CSR_WRITE (sc, csr_command, csr6);
  981. sc->lmc_device->stats.rx_missed_errors +=
  982. LMC_CSR_READ(sc, csr_missed_frames) & 0xffff;
  983. /* release the interrupt */
  984. if(sc->got_irq == 1){
  985. free_irq (dev->irq, dev);
  986. sc->got_irq = 0;
  987. }
  988. /* free skbuffs in the Rx queue */
  989. for (i = 0; i < LMC_RXDESCS; i++)
  990. {
  991. struct sk_buff *skb = sc->lmc_rxq[i];
  992. sc->lmc_rxq[i] = NULL;
  993. sc->lmc_rxring[i].status = 0;
  994. sc->lmc_rxring[i].length = 0;
  995. sc->lmc_rxring[i].buffer1 = 0xDEADBEEF;
  996. if (skb != NULL)
  997. dev_kfree_skb(skb);
  998. sc->lmc_rxq[i] = NULL;
  999. }
  1000. for (i = 0; i < LMC_TXDESCS; i++)
  1001. {
  1002. if (sc->lmc_txq[i] != NULL)
  1003. dev_kfree_skb(sc->lmc_txq[i]);
  1004. sc->lmc_txq[i] = NULL;
  1005. }
  1006. lmc_led_off (sc, LMC_MII16_LED_ALL);
  1007. netif_wake_queue(dev);
  1008. sc->extra_stats.tx_tbusy0++;
  1009. lmc_trace(dev, "lmc_ifdown out");
  1010. return 0;
  1011. }
  1012. /* Interrupt handling routine. This will take an incoming packet, or clean
  1013. * up after a trasmit.
  1014. */
  1015. static irqreturn_t lmc_interrupt (int irq, void *dev_instance) /*fold00*/
  1016. {
  1017. struct net_device *dev = (struct net_device *) dev_instance;
  1018. lmc_softc_t *sc = dev_to_sc(dev);
  1019. u32 csr;
  1020. int i;
  1021. s32 stat;
  1022. unsigned int badtx;
  1023. u32 firstcsr;
  1024. int max_work = LMC_RXDESCS;
  1025. int handled = 0;
  1026. lmc_trace(dev, "lmc_interrupt in");
  1027. spin_lock(&sc->lmc_lock);
  1028. /*
  1029. * Read the csr to find what interrupts we have (if any)
  1030. */
  1031. csr = LMC_CSR_READ (sc, csr_status);
  1032. /*
  1033. * Make sure this is our interrupt
  1034. */
  1035. if ( ! (csr & sc->lmc_intrmask)) {
  1036. goto lmc_int_fail_out;
  1037. }
  1038. firstcsr = csr;
  1039. /* always go through this loop at least once */
  1040. while (csr & sc->lmc_intrmask) {
  1041. handled = 1;
  1042. /*
  1043. * Clear interrupt bits, we handle all case below
  1044. */
  1045. LMC_CSR_WRITE (sc, csr_status, csr);
  1046. /*
  1047. * One of
  1048. * - Transmit process timed out CSR5<1>
  1049. * - Transmit jabber timeout CSR5<3>
  1050. * - Transmit underflow CSR5<5>
  1051. * - Transmit Receiver buffer unavailable CSR5<7>
  1052. * - Receive process stopped CSR5<8>
  1053. * - Receive watchdog timeout CSR5<9>
  1054. * - Early transmit interrupt CSR5<10>
  1055. *
  1056. * Is this really right? Should we do a running reset for jabber?
  1057. * (being a WAN card and all)
  1058. */
  1059. if (csr & TULIP_STS_ABNRMLINTR){
  1060. lmc_running_reset (dev);
  1061. break;
  1062. }
  1063. if (csr & TULIP_STS_RXINTR){
  1064. lmc_trace(dev, "rx interrupt");
  1065. lmc_rx (dev);
  1066. }
  1067. if (csr & (TULIP_STS_TXINTR | TULIP_STS_TXNOBUF | TULIP_STS_TXSTOPPED)) {
  1068. int n_compl = 0 ;
  1069. /* reset the transmit timeout detection flag -baz */
  1070. sc->extra_stats.tx_NoCompleteCnt = 0;
  1071. badtx = sc->lmc_taint_tx;
  1072. i = badtx % LMC_TXDESCS;
  1073. while ((badtx < sc->lmc_next_tx)) {
  1074. stat = sc->lmc_txring[i].status;
  1075. LMC_EVENT_LOG (LMC_EVENT_XMTINT, stat,
  1076. sc->lmc_txring[i].length);
  1077. /*
  1078. * If bit 31 is 1 the tulip owns it break out of the loop
  1079. */
  1080. if (stat & 0x80000000)
  1081. break;
  1082. n_compl++ ; /* i.e., have an empty slot in ring */
  1083. /*
  1084. * If we have no skbuff or have cleared it
  1085. * Already continue to the next buffer
  1086. */
  1087. if (sc->lmc_txq[i] == NULL)
  1088. continue;
  1089. /*
  1090. * Check the total error summary to look for any errors
  1091. */
  1092. if (stat & 0x8000) {
  1093. sc->lmc_device->stats.tx_errors++;
  1094. if (stat & 0x4104)
  1095. sc->lmc_device->stats.tx_aborted_errors++;
  1096. if (stat & 0x0C00)
  1097. sc->lmc_device->stats.tx_carrier_errors++;
  1098. if (stat & 0x0200)
  1099. sc->lmc_device->stats.tx_window_errors++;
  1100. if (stat & 0x0002)
  1101. sc->lmc_device->stats.tx_fifo_errors++;
  1102. } else {
  1103. sc->lmc_device->stats.tx_bytes += sc->lmc_txring[i].length & 0x7ff;
  1104. sc->lmc_device->stats.tx_packets++;
  1105. }
  1106. // dev_kfree_skb(sc->lmc_txq[i]);
  1107. dev_kfree_skb_irq(sc->lmc_txq[i]);
  1108. sc->lmc_txq[i] = NULL;
  1109. badtx++;
  1110. i = badtx % LMC_TXDESCS;
  1111. }
  1112. if (sc->lmc_next_tx - badtx > LMC_TXDESCS)
  1113. {
  1114. printk ("%s: out of sync pointer\n", dev->name);
  1115. badtx += LMC_TXDESCS;
  1116. }
  1117. LMC_EVENT_LOG(LMC_EVENT_TBUSY0, n_compl, 0);
  1118. sc->lmc_txfull = 0;
  1119. netif_wake_queue(dev);
  1120. sc->extra_stats.tx_tbusy0++;
  1121. #ifdef DEBUG
  1122. sc->extra_stats.dirtyTx = badtx;
  1123. sc->extra_stats.lmc_next_tx = sc->lmc_next_tx;
  1124. sc->extra_stats.lmc_txfull = sc->lmc_txfull;
  1125. #endif
  1126. sc->lmc_taint_tx = badtx;
  1127. /*
  1128. * Why was there a break here???
  1129. */
  1130. } /* end handle transmit interrupt */
  1131. if (csr & TULIP_STS_SYSERROR) {
  1132. u32 error;
  1133. printk (KERN_WARNING "%s: system bus error csr: %#8.8x\n", dev->name, csr);
  1134. error = csr>>23 & 0x7;
  1135. switch(error){
  1136. case 0x000:
  1137. printk(KERN_WARNING "%s: Parity Fault (bad)\n", dev->name);
  1138. break;
  1139. case 0x001:
  1140. printk(KERN_WARNING "%s: Master Abort (naughty)\n", dev->name);
  1141. break;
  1142. case 0x010:
  1143. printk(KERN_WARNING "%s: Target Abort (not so naughty)\n", dev->name);
  1144. break;
  1145. default:
  1146. printk(KERN_WARNING "%s: This bus error code was supposed to be reserved!\n", dev->name);
  1147. }
  1148. lmc_dec_reset (sc);
  1149. lmc_reset (sc);
  1150. LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
  1151. LMC_EVENT_LOG(LMC_EVENT_RESET2,
  1152. lmc_mii_readreg (sc, 0, 16),
  1153. lmc_mii_readreg (sc, 0, 17));
  1154. }
  1155. if(max_work-- <= 0)
  1156. break;
  1157. /*
  1158. * Get current csr status to make sure
  1159. * we've cleared all interrupts
  1160. */
  1161. csr = LMC_CSR_READ (sc, csr_status);
  1162. } /* end interrupt loop */
  1163. LMC_EVENT_LOG(LMC_EVENT_INT, firstcsr, csr);
  1164. lmc_int_fail_out:
  1165. spin_unlock(&sc->lmc_lock);
  1166. lmc_trace(dev, "lmc_interrupt out");
  1167. return IRQ_RETVAL(handled);
  1168. }
  1169. static int lmc_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1170. {
  1171. lmc_softc_t *sc = dev_to_sc(dev);
  1172. u32 flag;
  1173. int entry;
  1174. int ret = 0;
  1175. unsigned long flags;
  1176. lmc_trace(dev, "lmc_start_xmit in");
  1177. spin_lock_irqsave(&sc->lmc_lock, flags);
  1178. /* normal path, tbusy known to be zero */
  1179. entry = sc->lmc_next_tx % LMC_TXDESCS;
  1180. sc->lmc_txq[entry] = skb;
  1181. sc->lmc_txring[entry].buffer1 = virt_to_bus (skb->data);
  1182. LMC_CONSOLE_LOG("xmit", skb->data, skb->len);
  1183. #ifndef GCOM
  1184. /* If the queue is less than half full, don't interrupt */
  1185. if (sc->lmc_next_tx - sc->lmc_taint_tx < LMC_TXDESCS / 2)
  1186. {
  1187. /* Do not interrupt on completion of this packet */
  1188. flag = 0x60000000;
  1189. netif_wake_queue(dev);
  1190. }
  1191. else if (sc->lmc_next_tx - sc->lmc_taint_tx == LMC_TXDESCS / 2)
  1192. {
  1193. /* This generates an interrupt on completion of this packet */
  1194. flag = 0xe0000000;
  1195. netif_wake_queue(dev);
  1196. }
  1197. else if (sc->lmc_next_tx - sc->lmc_taint_tx < LMC_TXDESCS - 1)
  1198. {
  1199. /* Do not interrupt on completion of this packet */
  1200. flag = 0x60000000;
  1201. netif_wake_queue(dev);
  1202. }
  1203. else
  1204. {
  1205. /* This generates an interrupt on completion of this packet */
  1206. flag = 0xe0000000;
  1207. sc->lmc_txfull = 1;
  1208. netif_stop_queue(dev);
  1209. }
  1210. #else
  1211. flag = LMC_TDES_INTERRUPT_ON_COMPLETION;
  1212. if (sc->lmc_next_tx - sc->lmc_taint_tx >= LMC_TXDESCS - 1)
  1213. { /* ring full, go busy */
  1214. sc->lmc_txfull = 1;
  1215. netif_stop_queue(dev);
  1216. sc->extra_stats.tx_tbusy1++;
  1217. LMC_EVENT_LOG(LMC_EVENT_TBUSY1, entry, 0);
  1218. }
  1219. #endif
  1220. if (entry == LMC_TXDESCS - 1) /* last descriptor in ring */
  1221. flag |= LMC_TDES_END_OF_RING; /* flag as such for Tulip */
  1222. /* don't pad small packets either */
  1223. flag = sc->lmc_txring[entry].length = (skb->len) | flag |
  1224. sc->TxDescriptControlInit;
  1225. /* set the transmit timeout flag to be checked in
  1226. * the watchdog timer handler. -baz
  1227. */
  1228. sc->extra_stats.tx_NoCompleteCnt++;
  1229. sc->lmc_next_tx++;
  1230. /* give ownership to the chip */
  1231. LMC_EVENT_LOG(LMC_EVENT_XMT, flag, entry);
  1232. sc->lmc_txring[entry].status = 0x80000000;
  1233. /* send now! */
  1234. LMC_CSR_WRITE (sc, csr_txpoll, 0);
  1235. dev->trans_start = jiffies;
  1236. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  1237. lmc_trace(dev, "lmc_start_xmit_out");
  1238. return ret;
  1239. }
  1240. static int lmc_rx(struct net_device *dev)
  1241. {
  1242. lmc_softc_t *sc = dev_to_sc(dev);
  1243. int i;
  1244. int rx_work_limit = LMC_RXDESCS;
  1245. unsigned int next_rx;
  1246. int rxIntLoopCnt; /* debug -baz */
  1247. int localLengthErrCnt = 0;
  1248. long stat;
  1249. struct sk_buff *skb, *nsb;
  1250. u16 len;
  1251. lmc_trace(dev, "lmc_rx in");
  1252. lmc_led_on(sc, LMC_DS3_LED3);
  1253. rxIntLoopCnt = 0; /* debug -baz */
  1254. i = sc->lmc_next_rx % LMC_RXDESCS;
  1255. next_rx = sc->lmc_next_rx;
  1256. while (((stat = sc->lmc_rxring[i].status) & LMC_RDES_OWN_BIT) != DESC_OWNED_BY_DC21X4)
  1257. {
  1258. rxIntLoopCnt++; /* debug -baz */
  1259. len = ((stat & LMC_RDES_FRAME_LENGTH) >> RDES_FRAME_LENGTH_BIT_NUMBER);
  1260. if ((stat & 0x0300) != 0x0300) { /* Check first segment and last segment */
  1261. if ((stat & 0x0000ffff) != 0x7fff) {
  1262. /* Oversized frame */
  1263. sc->lmc_device->stats.rx_length_errors++;
  1264. goto skip_packet;
  1265. }
  1266. }
  1267. if (stat & 0x00000008) { /* Catch a dribbling bit error */
  1268. sc->lmc_device->stats.rx_errors++;
  1269. sc->lmc_device->stats.rx_frame_errors++;
  1270. goto skip_packet;
  1271. }
  1272. if (stat & 0x00000004) { /* Catch a CRC error by the Xilinx */
  1273. sc->lmc_device->stats.rx_errors++;
  1274. sc->lmc_device->stats.rx_crc_errors++;
  1275. goto skip_packet;
  1276. }
  1277. if (len > LMC_PKT_BUF_SZ) {
  1278. sc->lmc_device->stats.rx_length_errors++;
  1279. localLengthErrCnt++;
  1280. goto skip_packet;
  1281. }
  1282. if (len < sc->lmc_crcSize + 2) {
  1283. sc->lmc_device->stats.rx_length_errors++;
  1284. sc->extra_stats.rx_SmallPktCnt++;
  1285. localLengthErrCnt++;
  1286. goto skip_packet;
  1287. }
  1288. if(stat & 0x00004000){
  1289. printk(KERN_WARNING "%s: Receiver descriptor error, receiver out of sync?\n", dev->name);
  1290. }
  1291. len -= sc->lmc_crcSize;
  1292. skb = sc->lmc_rxq[i];
  1293. /*
  1294. * We ran out of memory at some point
  1295. * just allocate an skb buff and continue.
  1296. */
  1297. if (!skb) {
  1298. nsb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
  1299. if (nsb) {
  1300. sc->lmc_rxq[i] = nsb;
  1301. nsb->dev = dev;
  1302. sc->lmc_rxring[i].buffer1 = virt_to_bus(skb_tail_pointer(nsb));
  1303. }
  1304. sc->failed_recv_alloc = 1;
  1305. goto skip_packet;
  1306. }
  1307. sc->lmc_device->stats.rx_packets++;
  1308. sc->lmc_device->stats.rx_bytes += len;
  1309. LMC_CONSOLE_LOG("recv", skb->data, len);
  1310. /*
  1311. * I'm not sure of the sanity of this
  1312. * Packets could be arriving at a constant
  1313. * 44.210mbits/sec and we're going to copy
  1314. * them into a new buffer??
  1315. */
  1316. if(len > (LMC_MTU - (LMC_MTU>>2))){ /* len > LMC_MTU * 0.75 */
  1317. /*
  1318. * If it's a large packet don't copy it just hand it up
  1319. */
  1320. give_it_anyways:
  1321. sc->lmc_rxq[i] = NULL;
  1322. sc->lmc_rxring[i].buffer1 = 0x0;
  1323. skb_put (skb, len);
  1324. skb->protocol = lmc_proto_type(sc, skb);
  1325. skb_reset_mac_header(skb);
  1326. /* skb_reset_network_header(skb); */
  1327. skb->dev = dev;
  1328. lmc_proto_netif(sc, skb);
  1329. /*
  1330. * This skb will be destroyed by the upper layers, make a new one
  1331. */
  1332. nsb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
  1333. if (nsb) {
  1334. sc->lmc_rxq[i] = nsb;
  1335. nsb->dev = dev;
  1336. sc->lmc_rxring[i].buffer1 = virt_to_bus(skb_tail_pointer(nsb));
  1337. /* Transferred to 21140 below */
  1338. }
  1339. else {
  1340. /*
  1341. * We've run out of memory, stop trying to allocate
  1342. * memory and exit the interrupt handler
  1343. *
  1344. * The chip may run out of receivers and stop
  1345. * in which care we'll try to allocate the buffer
  1346. * again. (once a second)
  1347. */
  1348. sc->extra_stats.rx_BuffAllocErr++;
  1349. LMC_EVENT_LOG(LMC_EVENT_RCVINT, stat, len);
  1350. sc->failed_recv_alloc = 1;
  1351. goto skip_out_of_mem;
  1352. }
  1353. }
  1354. else {
  1355. nsb = dev_alloc_skb(len);
  1356. if(!nsb) {
  1357. goto give_it_anyways;
  1358. }
  1359. skb_copy_from_linear_data(skb, skb_put(nsb, len), len);
  1360. nsb->protocol = lmc_proto_type(sc, skb);
  1361. skb_reset_mac_header(nsb);
  1362. /* skb_reset_network_header(nsb); */
  1363. nsb->dev = dev;
  1364. lmc_proto_netif(sc, nsb);
  1365. }
  1366. skip_packet:
  1367. LMC_EVENT_LOG(LMC_EVENT_RCVINT, stat, len);
  1368. sc->lmc_rxring[i].status = DESC_OWNED_BY_DC21X4;
  1369. sc->lmc_next_rx++;
  1370. i = sc->lmc_next_rx % LMC_RXDESCS;
  1371. rx_work_limit--;
  1372. if (rx_work_limit < 0)
  1373. break;
  1374. }
  1375. /* detect condition for LMC1000 where DSU cable attaches and fills
  1376. * descriptors with bogus packets
  1377. *
  1378. if (localLengthErrCnt > LMC_RXDESCS - 3) {
  1379. sc->extra_stats.rx_BadPktSurgeCnt++;
  1380. LMC_EVENT_LOG(LMC_EVENT_BADPKTSURGE, localLengthErrCnt,
  1381. sc->extra_stats.rx_BadPktSurgeCnt);
  1382. } */
  1383. /* save max count of receive descriptors serviced */
  1384. if (rxIntLoopCnt > sc->extra_stats.rxIntLoopCnt)
  1385. sc->extra_stats.rxIntLoopCnt = rxIntLoopCnt; /* debug -baz */
  1386. #ifdef DEBUG
  1387. if (rxIntLoopCnt == 0)
  1388. {
  1389. for (i = 0; i < LMC_RXDESCS; i++)
  1390. {
  1391. if ((sc->lmc_rxring[i].status & LMC_RDES_OWN_BIT)
  1392. != DESC_OWNED_BY_DC21X4)
  1393. {
  1394. rxIntLoopCnt++;
  1395. }
  1396. }
  1397. LMC_EVENT_LOG(LMC_EVENT_RCVEND, rxIntLoopCnt, 0);
  1398. }
  1399. #endif
  1400. lmc_led_off(sc, LMC_DS3_LED3);
  1401. skip_out_of_mem:
  1402. lmc_trace(dev, "lmc_rx out");
  1403. return 0;
  1404. }
  1405. static struct net_device_stats *lmc_get_stats(struct net_device *dev)
  1406. {
  1407. lmc_softc_t *sc = dev_to_sc(dev);
  1408. unsigned long flags;
  1409. lmc_trace(dev, "lmc_get_stats in");
  1410. spin_lock_irqsave(&sc->lmc_lock, flags);
  1411. sc->lmc_device->stats.rx_missed_errors += LMC_CSR_READ(sc, csr_missed_frames) & 0xffff;
  1412. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  1413. lmc_trace(dev, "lmc_get_stats out");
  1414. return &sc->lmc_device->stats;
  1415. }
  1416. static struct pci_driver lmc_driver = {
  1417. .name = "lmc",
  1418. .id_table = lmc_pci_tbl,
  1419. .probe = lmc_init_one,
  1420. .remove = __devexit_p(lmc_remove_one),
  1421. };
  1422. static int __init init_lmc(void)
  1423. {
  1424. return pci_register_driver(&lmc_driver);
  1425. }
  1426. static void __exit exit_lmc(void)
  1427. {
  1428. pci_unregister_driver(&lmc_driver);
  1429. }
  1430. module_init(init_lmc);
  1431. module_exit(exit_lmc);
  1432. unsigned lmc_mii_readreg (lmc_softc_t * const sc, unsigned devaddr, unsigned regno) /*fold00*/
  1433. {
  1434. int i;
  1435. int command = (0xf6 << 10) | (devaddr << 5) | regno;
  1436. int retval = 0;
  1437. lmc_trace(sc->lmc_device, "lmc_mii_readreg in");
  1438. LMC_MII_SYNC (sc);
  1439. lmc_trace(sc->lmc_device, "lmc_mii_readreg: done sync");
  1440. for (i = 15; i >= 0; i--)
  1441. {
  1442. int dataval = (command & (1 << i)) ? 0x20000 : 0;
  1443. LMC_CSR_WRITE (sc, csr_9, dataval);
  1444. lmc_delay ();
  1445. /* __SLOW_DOWN_IO; */
  1446. LMC_CSR_WRITE (sc, csr_9, dataval | 0x10000);
  1447. lmc_delay ();
  1448. /* __SLOW_DOWN_IO; */
  1449. }
  1450. lmc_trace(sc->lmc_device, "lmc_mii_readreg: done1");
  1451. for (i = 19; i > 0; i--)
  1452. {
  1453. LMC_CSR_WRITE (sc, csr_9, 0x40000);
  1454. lmc_delay ();
  1455. /* __SLOW_DOWN_IO; */
  1456. retval = (retval << 1) | ((LMC_CSR_READ (sc, csr_9) & 0x80000) ? 1 : 0);
  1457. LMC_CSR_WRITE (sc, csr_9, 0x40000 | 0x10000);
  1458. lmc_delay ();
  1459. /* __SLOW_DOWN_IO; */
  1460. }
  1461. lmc_trace(sc->lmc_device, "lmc_mii_readreg out");
  1462. return (retval >> 1) & 0xffff;
  1463. }
  1464. void lmc_mii_writereg (lmc_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data) /*fold00*/
  1465. {
  1466. int i = 32;
  1467. int command = (0x5002 << 16) | (devaddr << 23) | (regno << 18) | data;
  1468. lmc_trace(sc->lmc_device, "lmc_mii_writereg in");
  1469. LMC_MII_SYNC (sc);
  1470. i = 31;
  1471. while (i >= 0)
  1472. {
  1473. int datav;
  1474. if (command & (1 << i))
  1475. datav = 0x20000;
  1476. else
  1477. datav = 0x00000;
  1478. LMC_CSR_WRITE (sc, csr_9, datav);
  1479. lmc_delay ();
  1480. /* __SLOW_DOWN_IO; */
  1481. LMC_CSR_WRITE (sc, csr_9, (datav | 0x10000));
  1482. lmc_delay ();
  1483. /* __SLOW_DOWN_IO; */
  1484. i--;
  1485. }
  1486. i = 2;
  1487. while (i > 0)
  1488. {
  1489. LMC_CSR_WRITE (sc, csr_9, 0x40000);
  1490. lmc_delay ();
  1491. /* __SLOW_DOWN_IO; */
  1492. LMC_CSR_WRITE (sc, csr_9, 0x50000);
  1493. lmc_delay ();
  1494. /* __SLOW_DOWN_IO; */
  1495. i--;
  1496. }
  1497. lmc_trace(sc->lmc_device, "lmc_mii_writereg out");
  1498. }
  1499. static void lmc_softreset (lmc_softc_t * const sc) /*fold00*/
  1500. {
  1501. int i;
  1502. lmc_trace(sc->lmc_device, "lmc_softreset in");
  1503. /* Initialize the receive rings and buffers. */
  1504. sc->lmc_txfull = 0;
  1505. sc->lmc_next_rx = 0;
  1506. sc->lmc_next_tx = 0;
  1507. sc->lmc_taint_rx = 0;
  1508. sc->lmc_taint_tx = 0;
  1509. /*
  1510. * Setup each one of the receiver buffers
  1511. * allocate an skbuff for each one, setup the descriptor table
  1512. * and point each buffer at the next one
  1513. */
  1514. for (i = 0; i < LMC_RXDESCS; i++)
  1515. {
  1516. struct sk_buff *skb;
  1517. if (sc->lmc_rxq[i] == NULL)
  1518. {
  1519. skb = dev_alloc_skb (LMC_PKT_BUF_SZ + 2);
  1520. if(skb == NULL){
  1521. printk(KERN_WARNING "%s: Failed to allocate receiver ring, will try again\n", sc->name);
  1522. sc->failed_ring = 1;
  1523. break;
  1524. }
  1525. else{
  1526. sc->lmc_rxq[i] = skb;
  1527. }
  1528. }
  1529. else
  1530. {
  1531. skb = sc->lmc_rxq[i];
  1532. }
  1533. skb->dev = sc->lmc_device;
  1534. /* owned by 21140 */
  1535. sc->lmc_rxring[i].status = 0x80000000;
  1536. /* used to be PKT_BUF_SZ now uses skb since we lose some to head room */
  1537. sc->lmc_rxring[i].length = skb_tailroom(skb);
  1538. /* use to be tail which is dumb since you're thinking why write
  1539. * to the end of the packj,et but since there's nothing there tail == data
  1540. */
  1541. sc->lmc_rxring[i].buffer1 = virt_to_bus (skb->data);
  1542. /* This is fair since the structure is static and we have the next address */
  1543. sc->lmc_rxring[i].buffer2 = virt_to_bus (&sc->lmc_rxring[i + 1]);
  1544. }
  1545. /*
  1546. * Sets end of ring
  1547. */
  1548. sc->lmc_rxring[i - 1].length |= 0x02000000; /* Set end of buffers flag */
  1549. sc->lmc_rxring[i - 1].buffer2 = virt_to_bus (&sc->lmc_rxring[0]); /* Point back to the start */
  1550. LMC_CSR_WRITE (sc, csr_rxlist, virt_to_bus (sc->lmc_rxring)); /* write base address */
  1551. /* Initialize the transmit rings and buffers */
  1552. for (i = 0; i < LMC_TXDESCS; i++)
  1553. {
  1554. if (sc->lmc_txq[i] != NULL){ /* have buffer */
  1555. dev_kfree_skb(sc->lmc_txq[i]); /* free it */
  1556. sc->lmc_device->stats.tx_dropped++; /* We just dropped a packet */
  1557. }
  1558. sc->lmc_txq[i] = NULL;
  1559. sc->lmc_txring[i].status = 0x00000000;
  1560. sc->lmc_txring[i].buffer2 = virt_to_bus (&sc->lmc_txring[i + 1]);
  1561. }
  1562. sc->lmc_txring[i - 1].buffer2 = virt_to_bus (&sc->lmc_txring[0]);
  1563. LMC_CSR_WRITE (sc, csr_txlist, virt_to_bus (sc->lmc_txring));
  1564. lmc_trace(sc->lmc_device, "lmc_softreset out");
  1565. }
  1566. void lmc_gpio_mkinput(lmc_softc_t * const sc, u32 bits) /*fold00*/
  1567. {
  1568. lmc_trace(sc->lmc_device, "lmc_gpio_mkinput in");
  1569. sc->lmc_gpio_io &= ~bits;
  1570. LMC_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET | (sc->lmc_gpio_io));
  1571. lmc_trace(sc->lmc_device, "lmc_gpio_mkinput out");
  1572. }
  1573. void lmc_gpio_mkoutput(lmc_softc_t * const sc, u32 bits) /*fold00*/
  1574. {
  1575. lmc_trace(sc->lmc_device, "lmc_gpio_mkoutput in");
  1576. sc->lmc_gpio_io |= bits;
  1577. LMC_CSR_WRITE(sc, csr_gp, TULIP_GP_PINSET | (sc->lmc_gpio_io));
  1578. lmc_trace(sc->lmc_device, "lmc_gpio_mkoutput out");
  1579. }
  1580. void lmc_led_on(lmc_softc_t * const sc, u32 led) /*fold00*/
  1581. {
  1582. lmc_trace(sc->lmc_device, "lmc_led_on in");
  1583. if((~sc->lmc_miireg16) & led){ /* Already on! */
  1584. lmc_trace(sc->lmc_device, "lmc_led_on aon out");
  1585. return;
  1586. }
  1587. sc->lmc_miireg16 &= ~led;
  1588. lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
  1589. lmc_trace(sc->lmc_device, "lmc_led_on out");
  1590. }
  1591. void lmc_led_off(lmc_softc_t * const sc, u32 led) /*fold00*/
  1592. {
  1593. lmc_trace(sc->lmc_device, "lmc_led_off in");
  1594. if(sc->lmc_miireg16 & led){ /* Already set don't do anything */
  1595. lmc_trace(sc->lmc_device, "lmc_led_off aoff out");
  1596. return;
  1597. }
  1598. sc->lmc_miireg16 |= led;
  1599. lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
  1600. lmc_trace(sc->lmc_device, "lmc_led_off out");
  1601. }
  1602. static void lmc_reset(lmc_softc_t * const sc) /*fold00*/
  1603. {
  1604. lmc_trace(sc->lmc_device, "lmc_reset in");
  1605. sc->lmc_miireg16 |= LMC_MII16_FIFO_RESET;
  1606. lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
  1607. sc->lmc_miireg16 &= ~LMC_MII16_FIFO_RESET;
  1608. lmc_mii_writereg(sc, 0, 16, sc->lmc_miireg16);
  1609. /*
  1610. * make some of the GPIO pins be outputs
  1611. */
  1612. lmc_gpio_mkoutput(sc, LMC_GEP_RESET);
  1613. /*
  1614. * RESET low to force state reset. This also forces
  1615. * the transmitter clock to be internal, but we expect to reset
  1616. * that later anyway.
  1617. */
  1618. sc->lmc_gpio &= ~(LMC_GEP_RESET);
  1619. LMC_CSR_WRITE(sc, csr_gp, sc->lmc_gpio);
  1620. /*
  1621. * hold for more than 10 microseconds
  1622. */
  1623. udelay(50);
  1624. /*
  1625. * stop driving Xilinx-related signals
  1626. */
  1627. lmc_gpio_mkinput(sc, LMC_GEP_RESET);
  1628. /*
  1629. * Call media specific init routine
  1630. */
  1631. sc->lmc_media->init(sc);
  1632. sc->extra_stats.resetCount++;
  1633. lmc_trace(sc->lmc_device, "lmc_reset out");
  1634. }
  1635. static void lmc_dec_reset(lmc_softc_t * const sc) /*fold00*/
  1636. {
  1637. u32 val;
  1638. lmc_trace(sc->lmc_device, "lmc_dec_reset in");
  1639. /*
  1640. * disable all interrupts
  1641. */
  1642. sc->lmc_intrmask = 0;
  1643. LMC_CSR_WRITE(sc, csr_intr, sc->lmc_intrmask);
  1644. /*
  1645. * Reset the chip with a software reset command.
  1646. * Wait 10 microseconds (actually 50 PCI cycles but at
  1647. * 33MHz that comes to two microseconds but wait a
  1648. * bit longer anyways)
  1649. */
  1650. LMC_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
  1651. udelay(25);
  1652. #ifdef __sparc__
  1653. sc->lmc_busmode = LMC_CSR_READ(sc, csr_busmode);
  1654. sc->lmc_busmode = 0x00100000;
  1655. sc->lmc_busmode &= ~TULIP_BUSMODE_SWRESET;
  1656. LMC_CSR_WRITE(sc, csr_busmode, sc->lmc_busmode);
  1657. #endif
  1658. sc->lmc_cmdmode = LMC_CSR_READ(sc, csr_command);
  1659. /*
  1660. * We want:
  1661. * no ethernet address in frames we write
  1662. * disable padding (txdesc, padding disable)
  1663. * ignore runt frames (rdes0 bit 15)
  1664. * no receiver watchdog or transmitter jabber timer
  1665. * (csr15 bit 0,14 == 1)
  1666. * if using 16-bit CRC, turn off CRC (trans desc, crc disable)
  1667. */
  1668. sc->lmc_cmdmode |= ( TULIP_CMD_PROMISCUOUS
  1669. | TULIP_CMD_FULLDUPLEX
  1670. | TULIP_CMD_PASSBADPKT
  1671. | TULIP_CMD_NOHEARTBEAT
  1672. | TULIP_CMD_PORTSELECT
  1673. | TULIP_CMD_RECEIVEALL
  1674. | TULIP_CMD_MUSTBEONE
  1675. );
  1676. sc->lmc_cmdmode &= ~( TULIP_CMD_OPERMODE
  1677. | TULIP_CMD_THRESHOLDCTL
  1678. | TULIP_CMD_STOREFWD
  1679. | TULIP_CMD_TXTHRSHLDCTL
  1680. );
  1681. LMC_CSR_WRITE(sc, csr_command, sc->lmc_cmdmode);
  1682. /*
  1683. * disable receiver watchdog and transmit jabber
  1684. */
  1685. val = LMC_CSR_READ(sc, csr_sia_general);
  1686. val |= (TULIP_WATCHDOG_TXDISABLE | TULIP_WATCHDOG_RXDISABLE);
  1687. LMC_CSR_WRITE(sc, csr_sia_general, val);
  1688. lmc_trace(sc->lmc_device, "lmc_dec_reset out");
  1689. }
  1690. static void lmc_initcsrs(lmc_softc_t * const sc, lmc_csrptr_t csr_base, /*fold00*/
  1691. size_t csr_size)
  1692. {
  1693. lmc_trace(sc->lmc_device, "lmc_initcsrs in");
  1694. sc->lmc_csrs.csr_busmode = csr_base + 0 * csr_size;
  1695. sc->lmc_csrs.csr_txpoll = csr_base + 1 * csr_size;
  1696. sc->lmc_csrs.csr_rxpoll = csr_base + 2 * csr_size;
  1697. sc->lmc_csrs.csr_rxlist = csr_base + 3 * csr_size;
  1698. sc->lmc_csrs.csr_txlist = csr_base + 4 * csr_size;
  1699. sc->lmc_csrs.csr_status = csr_base + 5 * csr_size;
  1700. sc->lmc_csrs.csr_command = csr_base + 6 * csr_size;
  1701. sc->lmc_csrs.csr_intr = csr_base + 7 * csr_size;
  1702. sc->lmc_csrs.csr_missed_frames = csr_base + 8 * csr_size;
  1703. sc->lmc_csrs.csr_9 = csr_base + 9 * csr_size;
  1704. sc->lmc_csrs.csr_10 = csr_base + 10 * csr_size;
  1705. sc->lmc_csrs.csr_11 = csr_base + 11 * csr_size;
  1706. sc->lmc_csrs.csr_12 = csr_base + 12 * csr_size;
  1707. sc->lmc_csrs.csr_13 = csr_base + 13 * csr_size;
  1708. sc->lmc_csrs.csr_14 = csr_base + 14 * csr_size;
  1709. sc->lmc_csrs.csr_15 = csr_base + 15 * csr_size;
  1710. lmc_trace(sc->lmc_device, "lmc_initcsrs out");
  1711. }
  1712. static void lmc_driver_timeout(struct net_device *dev)
  1713. {
  1714. lmc_softc_t *sc = dev_to_sc(dev);
  1715. u32 csr6;
  1716. unsigned long flags;
  1717. lmc_trace(dev, "lmc_driver_timeout in");
  1718. spin_lock_irqsave(&sc->lmc_lock, flags);
  1719. printk("%s: Xmitter busy|\n", dev->name);
  1720. sc->extra_stats.tx_tbusy_calls++;
  1721. if (jiffies - dev->trans_start < TX_TIMEOUT)
  1722. goto bug_out;
  1723. /*
  1724. * Chip seems to have locked up
  1725. * Reset it
  1726. * This whips out all our decriptor
  1727. * table and starts from scartch
  1728. */
  1729. LMC_EVENT_LOG(LMC_EVENT_XMTPRCTMO,
  1730. LMC_CSR_READ (sc, csr_status),
  1731. sc->extra_stats.tx_ProcTimeout);
  1732. lmc_running_reset (dev);
  1733. LMC_EVENT_LOG(LMC_EVENT_RESET1, LMC_CSR_READ (sc, csr_status), 0);
  1734. LMC_EVENT_LOG(LMC_EVENT_RESET2,
  1735. lmc_mii_readreg (sc, 0, 16),
  1736. lmc_mii_readreg (sc, 0, 17));
  1737. /* restart the tx processes */
  1738. csr6 = LMC_CSR_READ (sc, csr_command);
  1739. LMC_CSR_WRITE (sc, csr_command, csr6 | 0x0002);
  1740. LMC_CSR_WRITE (sc, csr_command, csr6 | 0x2002);
  1741. /* immediate transmit */
  1742. LMC_CSR_WRITE (sc, csr_txpoll, 0);
  1743. sc->lmc_device->stats.tx_errors++;
  1744. sc->extra_stats.tx_ProcTimeout++; /* -baz */
  1745. dev->trans_start = jiffies;
  1746. bug_out:
  1747. spin_unlock_irqrestore(&sc->lmc_lock, flags);
  1748. lmc_trace(dev, "lmc_driver_timout out");
  1749. }