lmc_main.c 63 KB

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