farsync.c 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684
  1. /*
  2. * FarSync WAN driver for Linux (2.6.x kernel version)
  3. *
  4. * Actually sync driver for X.21, V.35 and V.24 on FarSync T-series cards
  5. *
  6. * Copyright (C) 2001-2004 FarSite Communications Ltd.
  7. * www.farsite.co.uk
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Author: R.J.Dunlop <bob.dunlop@farsite.co.uk>
  15. * Maintainer: Kevin Curtis <kevin.curtis@farsite.co.uk>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/version.h>
  20. #include <linux/pci.h>
  21. #include <linux/ioport.h>
  22. #include <linux/init.h>
  23. #include <linux/if.h>
  24. #include <linux/hdlc.h>
  25. #include <asm/io.h>
  26. #include <asm/uaccess.h>
  27. #include "farsync.h"
  28. /*
  29. * Module info
  30. */
  31. MODULE_AUTHOR("R.J.Dunlop <bob.dunlop@farsite.co.uk>");
  32. MODULE_DESCRIPTION("FarSync T-Series WAN driver. FarSite Communications Ltd.");
  33. MODULE_LICENSE("GPL");
  34. /* Driver configuration and global parameters
  35. * ==========================================
  36. */
  37. /* Number of ports (per card) and cards supported
  38. */
  39. #define FST_MAX_PORTS 4
  40. #define FST_MAX_CARDS 32
  41. /* Default parameters for the link
  42. */
  43. #define FST_TX_QUEUE_LEN 100 /* At 8Mbps a longer queue length is
  44. * useful */
  45. #define FST_TXQ_DEPTH 16 /* This one is for the buffering
  46. * of frames on the way down to the card
  47. * so that we can keep the card busy
  48. * and maximise throughput
  49. */
  50. #define FST_HIGH_WATER_MARK 12 /* Point at which we flow control
  51. * network layer */
  52. #define FST_LOW_WATER_MARK 8 /* Point at which we remove flow
  53. * control from network layer */
  54. #define FST_MAX_MTU 8000 /* Huge but possible */
  55. #define FST_DEF_MTU 1500 /* Common sane value */
  56. #define FST_TX_TIMEOUT (2*HZ)
  57. #ifdef ARPHRD_RAWHDLC
  58. #define ARPHRD_MYTYPE ARPHRD_RAWHDLC /* Raw frames */
  59. #else
  60. #define ARPHRD_MYTYPE ARPHRD_HDLC /* Cisco-HDLC (keepalives etc) */
  61. #endif
  62. /*
  63. * Modules parameters and associated variables
  64. */
  65. static int fst_txq_low = FST_LOW_WATER_MARK;
  66. static int fst_txq_high = FST_HIGH_WATER_MARK;
  67. static int fst_max_reads = 7;
  68. static int fst_excluded_cards = 0;
  69. static int fst_excluded_list[FST_MAX_CARDS];
  70. module_param(fst_txq_low, int, 0);
  71. module_param(fst_txq_high, int, 0);
  72. module_param(fst_max_reads, int, 0);
  73. module_param(fst_excluded_cards, int, 0);
  74. module_param_array(fst_excluded_list, int, NULL, 0);
  75. /* Card shared memory layout
  76. * =========================
  77. */
  78. #pragma pack(1)
  79. /* This information is derived in part from the FarSite FarSync Smc.h
  80. * file. Unfortunately various name clashes and the non-portability of the
  81. * bit field declarations in that file have meant that I have chosen to
  82. * recreate the information here.
  83. *
  84. * The SMC (Shared Memory Configuration) has a version number that is
  85. * incremented every time there is a significant change. This number can
  86. * be used to check that we have not got out of step with the firmware
  87. * contained in the .CDE files.
  88. */
  89. #define SMC_VERSION 24
  90. #define FST_MEMSIZE 0x100000 /* Size of card memory (1Mb) */
  91. #define SMC_BASE 0x00002000L /* Base offset of the shared memory window main
  92. * configuration structure */
  93. #define BFM_BASE 0x00010000L /* Base offset of the shared memory window DMA
  94. * buffers */
  95. #define LEN_TX_BUFFER 8192 /* Size of packet buffers */
  96. #define LEN_RX_BUFFER 8192
  97. #define LEN_SMALL_TX_BUFFER 256 /* Size of obsolete buffs used for DOS diags */
  98. #define LEN_SMALL_RX_BUFFER 256
  99. #define NUM_TX_BUFFER 2 /* Must be power of 2. Fixed by firmware */
  100. #define NUM_RX_BUFFER 8
  101. /* Interrupt retry time in milliseconds */
  102. #define INT_RETRY_TIME 2
  103. /* The Am186CH/CC processors support a SmartDMA mode using circular pools
  104. * of buffer descriptors. The structure is almost identical to that used
  105. * in the LANCE Ethernet controllers. Details available as PDF from the
  106. * AMD web site: http://www.amd.com/products/epd/processors/\
  107. * 2.16bitcont/3.am186cxfa/a21914/21914.pdf
  108. */
  109. struct txdesc { /* Transmit descriptor */
  110. volatile u16 ladr; /* Low order address of packet. This is a
  111. * linear address in the Am186 memory space
  112. */
  113. volatile u8 hadr; /* High order address. Low 4 bits only, high 4
  114. * bits must be zero
  115. */
  116. volatile u8 bits; /* Status and config */
  117. volatile u16 bcnt; /* 2s complement of packet size in low 15 bits.
  118. * Transmit terminal count interrupt enable in
  119. * top bit.
  120. */
  121. u16 unused; /* Not used in Tx */
  122. };
  123. struct rxdesc { /* Receive descriptor */
  124. volatile u16 ladr; /* Low order address of packet */
  125. volatile u8 hadr; /* High order address */
  126. volatile u8 bits; /* Status and config */
  127. volatile u16 bcnt; /* 2s complement of buffer size in low 15 bits.
  128. * Receive terminal count interrupt enable in
  129. * top bit.
  130. */
  131. volatile u16 mcnt; /* Message byte count (15 bits) */
  132. };
  133. /* Convert a length into the 15 bit 2's complement */
  134. /* #define cnv_bcnt(len) (( ~(len) + 1 ) & 0x7FFF ) */
  135. /* Since we need to set the high bit to enable the completion interrupt this
  136. * can be made a lot simpler
  137. */
  138. #define cnv_bcnt(len) (-(len))
  139. /* Status and config bits for the above */
  140. #define DMA_OWN 0x80 /* SmartDMA owns the descriptor */
  141. #define TX_STP 0x02 /* Tx: start of packet */
  142. #define TX_ENP 0x01 /* Tx: end of packet */
  143. #define RX_ERR 0x40 /* Rx: error (OR of next 4 bits) */
  144. #define RX_FRAM 0x20 /* Rx: framing error */
  145. #define RX_OFLO 0x10 /* Rx: overflow error */
  146. #define RX_CRC 0x08 /* Rx: CRC error */
  147. #define RX_HBUF 0x04 /* Rx: buffer error */
  148. #define RX_STP 0x02 /* Rx: start of packet */
  149. #define RX_ENP 0x01 /* Rx: end of packet */
  150. /* Interrupts from the card are caused by various events which are presented
  151. * in a circular buffer as several events may be processed on one physical int
  152. */
  153. #define MAX_CIRBUFF 32
  154. struct cirbuff {
  155. u8 rdindex; /* read, then increment and wrap */
  156. u8 wrindex; /* write, then increment and wrap */
  157. u8 evntbuff[MAX_CIRBUFF];
  158. };
  159. /* Interrupt event codes.
  160. * Where appropriate the two low order bits indicate the port number
  161. */
  162. #define CTLA_CHG 0x18 /* Control signal changed */
  163. #define CTLB_CHG 0x19
  164. #define CTLC_CHG 0x1A
  165. #define CTLD_CHG 0x1B
  166. #define INIT_CPLT 0x20 /* Initialisation complete */
  167. #define INIT_FAIL 0x21 /* Initialisation failed */
  168. #define ABTA_SENT 0x24 /* Abort sent */
  169. #define ABTB_SENT 0x25
  170. #define ABTC_SENT 0x26
  171. #define ABTD_SENT 0x27
  172. #define TXA_UNDF 0x28 /* Transmission underflow */
  173. #define TXB_UNDF 0x29
  174. #define TXC_UNDF 0x2A
  175. #define TXD_UNDF 0x2B
  176. #define F56_INT 0x2C
  177. #define M32_INT 0x2D
  178. #define TE1_ALMA 0x30
  179. /* Port physical configuration. See farsync.h for field values */
  180. struct port_cfg {
  181. u16 lineInterface; /* Physical interface type */
  182. u8 x25op; /* Unused at present */
  183. u8 internalClock; /* 1 => internal clock, 0 => external */
  184. u8 transparentMode; /* 1 => on, 0 => off */
  185. u8 invertClock; /* 0 => normal, 1 => inverted */
  186. u8 padBytes[6]; /* Padding */
  187. u32 lineSpeed; /* Speed in bps */
  188. };
  189. /* TE1 port physical configuration */
  190. struct su_config {
  191. u32 dataRate;
  192. u8 clocking;
  193. u8 framing;
  194. u8 structure;
  195. u8 interface;
  196. u8 coding;
  197. u8 lineBuildOut;
  198. u8 equalizer;
  199. u8 transparentMode;
  200. u8 loopMode;
  201. u8 range;
  202. u8 txBufferMode;
  203. u8 rxBufferMode;
  204. u8 startingSlot;
  205. u8 losThreshold;
  206. u8 enableIdleCode;
  207. u8 idleCode;
  208. u8 spare[44];
  209. };
  210. /* TE1 Status */
  211. struct su_status {
  212. u32 receiveBufferDelay;
  213. u32 framingErrorCount;
  214. u32 codeViolationCount;
  215. u32 crcErrorCount;
  216. u32 lineAttenuation;
  217. u8 portStarted;
  218. u8 lossOfSignal;
  219. u8 receiveRemoteAlarm;
  220. u8 alarmIndicationSignal;
  221. u8 spare[40];
  222. };
  223. /* Finally sling all the above together into the shared memory structure.
  224. * Sorry it's a hodge podge of arrays, structures and unused bits, it's been
  225. * evolving under NT for some time so I guess we're stuck with it.
  226. * The structure starts at offset SMC_BASE.
  227. * See farsync.h for some field values.
  228. */
  229. struct fst_shared {
  230. /* DMA descriptor rings */
  231. struct rxdesc rxDescrRing[FST_MAX_PORTS][NUM_RX_BUFFER];
  232. struct txdesc txDescrRing[FST_MAX_PORTS][NUM_TX_BUFFER];
  233. /* Obsolete small buffers */
  234. u8 smallRxBuffer[FST_MAX_PORTS][NUM_RX_BUFFER][LEN_SMALL_RX_BUFFER];
  235. u8 smallTxBuffer[FST_MAX_PORTS][NUM_TX_BUFFER][LEN_SMALL_TX_BUFFER];
  236. u8 taskStatus; /* 0x00 => initialising, 0x01 => running,
  237. * 0xFF => halted
  238. */
  239. u8 interruptHandshake; /* Set to 0x01 by adapter to signal interrupt,
  240. * set to 0xEE by host to acknowledge interrupt
  241. */
  242. u16 smcVersion; /* Must match SMC_VERSION */
  243. u32 smcFirmwareVersion; /* 0xIIVVRRBB where II = product ID, VV = major
  244. * version, RR = revision and BB = build
  245. */
  246. u16 txa_done; /* Obsolete completion flags */
  247. u16 rxa_done;
  248. u16 txb_done;
  249. u16 rxb_done;
  250. u16 txc_done;
  251. u16 rxc_done;
  252. u16 txd_done;
  253. u16 rxd_done;
  254. u16 mailbox[4]; /* Diagnostics mailbox. Not used */
  255. struct cirbuff interruptEvent; /* interrupt causes */
  256. u32 v24IpSts[FST_MAX_PORTS]; /* V.24 control input status */
  257. u32 v24OpSts[FST_MAX_PORTS]; /* V.24 control output status */
  258. struct port_cfg portConfig[FST_MAX_PORTS];
  259. u16 clockStatus[FST_MAX_PORTS]; /* lsb: 0=> present, 1=> absent */
  260. u16 cableStatus; /* lsb: 0=> present, 1=> absent */
  261. u16 txDescrIndex[FST_MAX_PORTS]; /* transmit descriptor ring index */
  262. u16 rxDescrIndex[FST_MAX_PORTS]; /* receive descriptor ring index */
  263. u16 portMailbox[FST_MAX_PORTS][2]; /* command, modifier */
  264. u16 cardMailbox[4]; /* Not used */
  265. /* Number of times the card thinks the host has
  266. * missed an interrupt by not acknowledging
  267. * within 2mS (I guess NT has problems)
  268. */
  269. u32 interruptRetryCount;
  270. /* Driver private data used as an ID. We'll not
  271. * use this as I'd rather keep such things
  272. * in main memory rather than on the PCI bus
  273. */
  274. u32 portHandle[FST_MAX_PORTS];
  275. /* Count of Tx underflows for stats */
  276. u32 transmitBufferUnderflow[FST_MAX_PORTS];
  277. /* Debounced V.24 control input status */
  278. u32 v24DebouncedSts[FST_MAX_PORTS];
  279. /* Adapter debounce timers. Don't touch */
  280. u32 ctsTimer[FST_MAX_PORTS];
  281. u32 ctsTimerRun[FST_MAX_PORTS];
  282. u32 dcdTimer[FST_MAX_PORTS];
  283. u32 dcdTimerRun[FST_MAX_PORTS];
  284. u32 numberOfPorts; /* Number of ports detected at startup */
  285. u16 _reserved[64];
  286. u16 cardMode; /* Bit-mask to enable features:
  287. * Bit 0: 1 enables LED identify mode
  288. */
  289. u16 portScheduleOffset;
  290. struct su_config suConfig; /* TE1 Bits */
  291. struct su_status suStatus;
  292. u32 endOfSmcSignature; /* endOfSmcSignature MUST be the last member of
  293. * the structure and marks the end of shared
  294. * memory. Adapter code initializes it as
  295. * END_SIG.
  296. */
  297. };
  298. /* endOfSmcSignature value */
  299. #define END_SIG 0x12345678
  300. /* Mailbox values. (portMailbox) */
  301. #define NOP 0 /* No operation */
  302. #define ACK 1 /* Positive acknowledgement to PC driver */
  303. #define NAK 2 /* Negative acknowledgement to PC driver */
  304. #define STARTPORT 3 /* Start an HDLC port */
  305. #define STOPPORT 4 /* Stop an HDLC port */
  306. #define ABORTTX 5 /* Abort the transmitter for a port */
  307. #define SETV24O 6 /* Set V24 outputs */
  308. /* PLX Chip Register Offsets */
  309. #define CNTRL_9052 0x50 /* Control Register */
  310. #define CNTRL_9054 0x6c /* Control Register */
  311. #define INTCSR_9052 0x4c /* Interrupt control/status register */
  312. #define INTCSR_9054 0x68 /* Interrupt control/status register */
  313. /* 9054 DMA Registers */
  314. /*
  315. * Note that we will be using DMA Channel 0 for copying rx data
  316. * and Channel 1 for copying tx data
  317. */
  318. #define DMAMODE0 0x80
  319. #define DMAPADR0 0x84
  320. #define DMALADR0 0x88
  321. #define DMASIZ0 0x8c
  322. #define DMADPR0 0x90
  323. #define DMAMODE1 0x94
  324. #define DMAPADR1 0x98
  325. #define DMALADR1 0x9c
  326. #define DMASIZ1 0xa0
  327. #define DMADPR1 0xa4
  328. #define DMACSR0 0xa8
  329. #define DMACSR1 0xa9
  330. #define DMAARB 0xac
  331. #define DMATHR 0xb0
  332. #define DMADAC0 0xb4
  333. #define DMADAC1 0xb8
  334. #define DMAMARBR 0xac
  335. #define FST_MIN_DMA_LEN 64
  336. #define FST_RX_DMA_INT 0x01
  337. #define FST_TX_DMA_INT 0x02
  338. #define FST_CARD_INT 0x04
  339. /* Larger buffers are positioned in memory at offset BFM_BASE */
  340. struct buf_window {
  341. u8 txBuffer[FST_MAX_PORTS][NUM_TX_BUFFER][LEN_TX_BUFFER];
  342. u8 rxBuffer[FST_MAX_PORTS][NUM_RX_BUFFER][LEN_RX_BUFFER];
  343. };
  344. /* Calculate offset of a buffer object within the shared memory window */
  345. #define BUF_OFFSET(X) (BFM_BASE + offsetof(struct buf_window, X))
  346. #pragma pack()
  347. /* Device driver private information
  348. * =================================
  349. */
  350. /* Per port (line or channel) information
  351. */
  352. struct fst_port_info {
  353. struct net_device *dev; /* Device struct - must be first */
  354. struct fst_card_info *card; /* Card we're associated with */
  355. int index; /* Port index on the card */
  356. int hwif; /* Line hardware (lineInterface copy) */
  357. int run; /* Port is running */
  358. int mode; /* Normal or FarSync raw */
  359. int rxpos; /* Next Rx buffer to use */
  360. int txpos; /* Next Tx buffer to use */
  361. int txipos; /* Next Tx buffer to check for free */
  362. int start; /* Indication of start/stop to network */
  363. /*
  364. * A sixteen entry transmit queue
  365. */
  366. int txqs; /* index to get next buffer to tx */
  367. int txqe; /* index to queue next packet */
  368. struct sk_buff *txq[FST_TXQ_DEPTH]; /* The queue */
  369. int rxqdepth;
  370. };
  371. /* Per card information
  372. */
  373. struct fst_card_info {
  374. char __iomem *mem; /* Card memory mapped to kernel space */
  375. char __iomem *ctlmem; /* Control memory for PCI cards */
  376. unsigned int phys_mem; /* Physical memory window address */
  377. unsigned int phys_ctlmem; /* Physical control memory address */
  378. unsigned int irq; /* Interrupt request line number */
  379. unsigned int nports; /* Number of serial ports */
  380. unsigned int type; /* Type index of card */
  381. unsigned int state; /* State of card */
  382. spinlock_t card_lock; /* Lock for SMP access */
  383. unsigned short pci_conf; /* PCI card config in I/O space */
  384. /* Per port info */
  385. struct fst_port_info ports[FST_MAX_PORTS];
  386. struct pci_dev *device; /* Information about the pci device */
  387. int card_no; /* Inst of the card on the system */
  388. int family; /* TxP or TxU */
  389. int dmarx_in_progress;
  390. int dmatx_in_progress;
  391. unsigned long int_count;
  392. unsigned long int_time_ave;
  393. void *rx_dma_handle_host;
  394. dma_addr_t rx_dma_handle_card;
  395. void *tx_dma_handle_host;
  396. dma_addr_t tx_dma_handle_card;
  397. struct sk_buff *dma_skb_rx;
  398. struct fst_port_info *dma_port_rx;
  399. struct fst_port_info *dma_port_tx;
  400. int dma_len_rx;
  401. int dma_len_tx;
  402. int dma_txpos;
  403. int dma_rxpos;
  404. };
  405. /* Convert an HDLC device pointer into a port info pointer and similar */
  406. #define dev_to_port(D) (dev_to_hdlc(D)->priv)
  407. #define port_to_dev(P) ((P)->dev)
  408. /*
  409. * Shared memory window access macros
  410. *
  411. * We have a nice memory based structure above, which could be directly
  412. * mapped on i386 but might not work on other architectures unless we use
  413. * the readb,w,l and writeb,w,l macros. Unfortunately these macros take
  414. * physical offsets so we have to convert. The only saving grace is that
  415. * this should all collapse back to a simple indirection eventually.
  416. */
  417. #define WIN_OFFSET(X) ((long)&(((struct fst_shared *)SMC_BASE)->X))
  418. #define FST_RDB(C,E) readb ((C)->mem + WIN_OFFSET(E))
  419. #define FST_RDW(C,E) readw ((C)->mem + WIN_OFFSET(E))
  420. #define FST_RDL(C,E) readl ((C)->mem + WIN_OFFSET(E))
  421. #define FST_WRB(C,E,B) writeb ((B), (C)->mem + WIN_OFFSET(E))
  422. #define FST_WRW(C,E,W) writew ((W), (C)->mem + WIN_OFFSET(E))
  423. #define FST_WRL(C,E,L) writel ((L), (C)->mem + WIN_OFFSET(E))
  424. /*
  425. * Debug support
  426. */
  427. #if FST_DEBUG
  428. static int fst_debug_mask = { FST_DEBUG };
  429. /* Most common debug activity is to print something if the corresponding bit
  430. * is set in the debug mask. Note: this uses a non-ANSI extension in GCC to
  431. * support variable numbers of macro parameters. The inverted if prevents us
  432. * eating someone else's else clause.
  433. */
  434. #define dbg(F,fmt,A...) if ( ! ( fst_debug_mask & (F))) \
  435. ; \
  436. else \
  437. printk ( KERN_DEBUG FST_NAME ": " fmt, ## A )
  438. #else
  439. #define dbg(X...) /* NOP */
  440. #endif
  441. /* Printing short cuts
  442. */
  443. #define printk_err(fmt,A...) printk ( KERN_ERR FST_NAME ": " fmt, ## A )
  444. #define printk_warn(fmt,A...) printk ( KERN_WARNING FST_NAME ": " fmt, ## A )
  445. #define printk_info(fmt,A...) printk ( KERN_INFO FST_NAME ": " fmt, ## A )
  446. /*
  447. * PCI ID lookup table
  448. */
  449. static struct pci_device_id fst_pci_dev_id[] __devinitdata = {
  450. {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T2P, PCI_ANY_ID,
  451. PCI_ANY_ID, 0, 0, FST_TYPE_T2P},
  452. {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T4P, PCI_ANY_ID,
  453. PCI_ANY_ID, 0, 0, FST_TYPE_T4P},
  454. {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T1U, PCI_ANY_ID,
  455. PCI_ANY_ID, 0, 0, FST_TYPE_T1U},
  456. {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T2U, PCI_ANY_ID,
  457. PCI_ANY_ID, 0, 0, FST_TYPE_T2U},
  458. {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_T4U, PCI_ANY_ID,
  459. PCI_ANY_ID, 0, 0, FST_TYPE_T4U},
  460. {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_TE1, PCI_ANY_ID,
  461. PCI_ANY_ID, 0, 0, FST_TYPE_TE1},
  462. {PCI_VENDOR_ID_FARSITE, PCI_DEVICE_ID_FARSITE_TE1C, PCI_ANY_ID,
  463. PCI_ANY_ID, 0, 0, FST_TYPE_TE1},
  464. {0,} /* End */
  465. };
  466. MODULE_DEVICE_TABLE(pci, fst_pci_dev_id);
  467. /*
  468. * Device Driver Work Queues
  469. *
  470. * So that we don't spend too much time processing events in the
  471. * Interrupt Service routine, we will declare a work queue per Card
  472. * and make the ISR schedule a task in the queue for later execution.
  473. * In the 2.4 Kernel we used to use the immediate queue for BH's
  474. * Now that they are gone, tasklets seem to be much better than work
  475. * queues.
  476. */
  477. static void do_bottom_half_tx(struct fst_card_info *card);
  478. static void do_bottom_half_rx(struct fst_card_info *card);
  479. static void fst_process_tx_work_q(unsigned long work_q);
  480. static void fst_process_int_work_q(unsigned long work_q);
  481. static DECLARE_TASKLET(fst_tx_task, fst_process_tx_work_q, 0);
  482. static DECLARE_TASKLET(fst_int_task, fst_process_int_work_q, 0);
  483. static struct fst_card_info *fst_card_array[FST_MAX_CARDS];
  484. static spinlock_t fst_work_q_lock;
  485. static u64 fst_work_txq;
  486. static u64 fst_work_intq;
  487. static void
  488. fst_q_work_item(u64 * queue, int card_index)
  489. {
  490. unsigned long flags;
  491. u64 mask;
  492. /*
  493. * Grab the queue exclusively
  494. */
  495. spin_lock_irqsave(&fst_work_q_lock, flags);
  496. /*
  497. * Making an entry in the queue is simply a matter of setting
  498. * a bit for the card indicating that there is work to do in the
  499. * bottom half for the card. Note the limitation of 64 cards.
  500. * That ought to be enough
  501. */
  502. mask = 1 << card_index;
  503. *queue |= mask;
  504. spin_unlock_irqrestore(&fst_work_q_lock, flags);
  505. }
  506. static void
  507. fst_process_tx_work_q(unsigned long /*void **/work_q)
  508. {
  509. unsigned long flags;
  510. u64 work_txq;
  511. int i;
  512. /*
  513. * Grab the queue exclusively
  514. */
  515. dbg(DBG_TX, "fst_process_tx_work_q\n");
  516. spin_lock_irqsave(&fst_work_q_lock, flags);
  517. work_txq = fst_work_txq;
  518. fst_work_txq = 0;
  519. spin_unlock_irqrestore(&fst_work_q_lock, flags);
  520. /*
  521. * Call the bottom half for each card with work waiting
  522. */
  523. for (i = 0; i < FST_MAX_CARDS; i++) {
  524. if (work_txq & 0x01) {
  525. if (fst_card_array[i] != NULL) {
  526. dbg(DBG_TX, "Calling tx bh for card %d\n", i);
  527. do_bottom_half_tx(fst_card_array[i]);
  528. }
  529. }
  530. work_txq = work_txq >> 1;
  531. }
  532. }
  533. static void
  534. fst_process_int_work_q(unsigned long /*void **/work_q)
  535. {
  536. unsigned long flags;
  537. u64 work_intq;
  538. int i;
  539. /*
  540. * Grab the queue exclusively
  541. */
  542. dbg(DBG_INTR, "fst_process_int_work_q\n");
  543. spin_lock_irqsave(&fst_work_q_lock, flags);
  544. work_intq = fst_work_intq;
  545. fst_work_intq = 0;
  546. spin_unlock_irqrestore(&fst_work_q_lock, flags);
  547. /*
  548. * Call the bottom half for each card with work waiting
  549. */
  550. for (i = 0; i < FST_MAX_CARDS; i++) {
  551. if (work_intq & 0x01) {
  552. if (fst_card_array[i] != NULL) {
  553. dbg(DBG_INTR,
  554. "Calling rx & tx bh for card %d\n", i);
  555. do_bottom_half_rx(fst_card_array[i]);
  556. do_bottom_half_tx(fst_card_array[i]);
  557. }
  558. }
  559. work_intq = work_intq >> 1;
  560. }
  561. }
  562. /* Card control functions
  563. * ======================
  564. */
  565. /* Place the processor in reset state
  566. *
  567. * Used to be a simple write to card control space but a glitch in the latest
  568. * AMD Am186CH processor means that we now have to do it by asserting and de-
  569. * asserting the PLX chip PCI Adapter Software Reset. Bit 30 in CNTRL register
  570. * at offset 9052_CNTRL. Note the updates for the TXU.
  571. */
  572. static inline void
  573. fst_cpureset(struct fst_card_info *card)
  574. {
  575. unsigned char interrupt_line_register;
  576. unsigned long j = jiffies + 1;
  577. unsigned int regval;
  578. if (card->family == FST_FAMILY_TXU) {
  579. if (pci_read_config_byte
  580. (card->device, PCI_INTERRUPT_LINE, &interrupt_line_register)) {
  581. dbg(DBG_ASS,
  582. "Error in reading interrupt line register\n");
  583. }
  584. /*
  585. * Assert PLX software reset and Am186 hardware reset
  586. * and then deassert the PLX software reset but 186 still in reset
  587. */
  588. outw(0x440f, card->pci_conf + CNTRL_9054 + 2);
  589. outw(0x040f, card->pci_conf + CNTRL_9054 + 2);
  590. /*
  591. * We are delaying here to allow the 9054 to reset itself
  592. */
  593. j = jiffies + 1;
  594. while (jiffies < j)
  595. /* Do nothing */ ;
  596. outw(0x240f, card->pci_conf + CNTRL_9054 + 2);
  597. /*
  598. * We are delaying here to allow the 9054 to reload its eeprom
  599. */
  600. j = jiffies + 1;
  601. while (jiffies < j)
  602. /* Do nothing */ ;
  603. outw(0x040f, card->pci_conf + CNTRL_9054 + 2);
  604. if (pci_write_config_byte
  605. (card->device, PCI_INTERRUPT_LINE, interrupt_line_register)) {
  606. dbg(DBG_ASS,
  607. "Error in writing interrupt line register\n");
  608. }
  609. } else {
  610. regval = inl(card->pci_conf + CNTRL_9052);
  611. outl(regval | 0x40000000, card->pci_conf + CNTRL_9052);
  612. outl(regval & ~0x40000000, card->pci_conf + CNTRL_9052);
  613. }
  614. }
  615. /* Release the processor from reset
  616. */
  617. static inline void
  618. fst_cpurelease(struct fst_card_info *card)
  619. {
  620. if (card->family == FST_FAMILY_TXU) {
  621. /*
  622. * Force posted writes to complete
  623. */
  624. (void) readb(card->mem);
  625. /*
  626. * Release LRESET DO = 1
  627. * Then release Local Hold, DO = 1
  628. */
  629. outw(0x040e, card->pci_conf + CNTRL_9054 + 2);
  630. outw(0x040f, card->pci_conf + CNTRL_9054 + 2);
  631. } else {
  632. (void) readb(card->ctlmem);
  633. }
  634. }
  635. /* Clear the cards interrupt flag
  636. */
  637. static inline void
  638. fst_clear_intr(struct fst_card_info *card)
  639. {
  640. if (card->family == FST_FAMILY_TXU) {
  641. (void) readb(card->ctlmem);
  642. } else {
  643. /* Poke the appropriate PLX chip register (same as enabling interrupts)
  644. */
  645. outw(0x0543, card->pci_conf + INTCSR_9052);
  646. }
  647. }
  648. /* Enable card interrupts
  649. */
  650. static inline void
  651. fst_enable_intr(struct fst_card_info *card)
  652. {
  653. if (card->family == FST_FAMILY_TXU) {
  654. outl(0x0f0c0900, card->pci_conf + INTCSR_9054);
  655. } else {
  656. outw(0x0543, card->pci_conf + INTCSR_9052);
  657. }
  658. }
  659. /* Disable card interrupts
  660. */
  661. static inline void
  662. fst_disable_intr(struct fst_card_info *card)
  663. {
  664. if (card->family == FST_FAMILY_TXU) {
  665. outl(0x00000000, card->pci_conf + INTCSR_9054);
  666. } else {
  667. outw(0x0000, card->pci_conf + INTCSR_9052);
  668. }
  669. }
  670. /* Process the result of trying to pass a received frame up the stack
  671. */
  672. static void
  673. fst_process_rx_status(int rx_status, char *name)
  674. {
  675. switch (rx_status) {
  676. case NET_RX_SUCCESS:
  677. {
  678. /*
  679. * Nothing to do here
  680. */
  681. break;
  682. }
  683. case NET_RX_DROP:
  684. {
  685. dbg(DBG_ASS, "%s: Received packet dropped\n", name);
  686. break;
  687. }
  688. }
  689. }
  690. /* Initilaise DMA for PLX 9054
  691. */
  692. static inline void
  693. fst_init_dma(struct fst_card_info *card)
  694. {
  695. /*
  696. * This is only required for the PLX 9054
  697. */
  698. if (card->family == FST_FAMILY_TXU) {
  699. pci_set_master(card->device);
  700. outl(0x00020441, card->pci_conf + DMAMODE0);
  701. outl(0x00020441, card->pci_conf + DMAMODE1);
  702. outl(0x0, card->pci_conf + DMATHR);
  703. }
  704. }
  705. /* Tx dma complete interrupt
  706. */
  707. static void
  708. fst_tx_dma_complete(struct fst_card_info *card, struct fst_port_info *port,
  709. int len, int txpos)
  710. {
  711. struct net_device *dev = port_to_dev(port);
  712. /*
  713. * Everything is now set, just tell the card to go
  714. */
  715. dbg(DBG_TX, "fst_tx_dma_complete\n");
  716. FST_WRB(card, txDescrRing[port->index][txpos].bits,
  717. DMA_OWN | TX_STP | TX_ENP);
  718. dev->stats.tx_packets++;
  719. dev->stats.tx_bytes += len;
  720. dev->trans_start = jiffies;
  721. }
  722. /*
  723. * Mark it for our own raw sockets interface
  724. */
  725. static __be16 farsync_type_trans(struct sk_buff *skb, struct net_device *dev)
  726. {
  727. skb->dev = dev;
  728. skb_reset_mac_header(skb);
  729. skb->pkt_type = PACKET_HOST;
  730. return htons(ETH_P_CUST);
  731. }
  732. /* Rx dma complete interrupt
  733. */
  734. static void
  735. fst_rx_dma_complete(struct fst_card_info *card, struct fst_port_info *port,
  736. int len, struct sk_buff *skb, int rxp)
  737. {
  738. struct net_device *dev = port_to_dev(port);
  739. int pi;
  740. int rx_status;
  741. dbg(DBG_TX, "fst_rx_dma_complete\n");
  742. pi = port->index;
  743. memcpy(skb_put(skb, len), card->rx_dma_handle_host, len);
  744. /* Reset buffer descriptor */
  745. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  746. /* Update stats */
  747. dev->stats.rx_packets++;
  748. dev->stats.rx_bytes += len;
  749. /* Push upstream */
  750. dbg(DBG_RX, "Pushing the frame up the stack\n");
  751. if (port->mode == FST_RAW)
  752. skb->protocol = farsync_type_trans(skb, dev);
  753. else
  754. skb->protocol = hdlc_type_trans(skb, dev);
  755. rx_status = netif_rx(skb);
  756. fst_process_rx_status(rx_status, port_to_dev(port)->name);
  757. if (rx_status == NET_RX_DROP)
  758. dev->stats.rx_dropped++;
  759. }
  760. /*
  761. * Receive a frame through the DMA
  762. */
  763. static inline void
  764. fst_rx_dma(struct fst_card_info *card, unsigned char *skb,
  765. unsigned char *mem, int len)
  766. {
  767. /*
  768. * This routine will setup the DMA and start it
  769. */
  770. dbg(DBG_RX, "In fst_rx_dma %p %p %d\n", skb, mem, len);
  771. if (card->dmarx_in_progress) {
  772. dbg(DBG_ASS, "In fst_rx_dma while dma in progress\n");
  773. }
  774. outl((unsigned long) skb, card->pci_conf + DMAPADR0); /* Copy to here */
  775. outl((unsigned long) mem, card->pci_conf + DMALADR0); /* from here */
  776. outl(len, card->pci_conf + DMASIZ0); /* for this length */
  777. outl(0x00000000c, card->pci_conf + DMADPR0); /* In this direction */
  778. /*
  779. * We use the dmarx_in_progress flag to flag the channel as busy
  780. */
  781. card->dmarx_in_progress = 1;
  782. outb(0x03, card->pci_conf + DMACSR0); /* Start the transfer */
  783. }
  784. /*
  785. * Send a frame through the DMA
  786. */
  787. static inline void
  788. fst_tx_dma(struct fst_card_info *card, unsigned char *skb,
  789. unsigned char *mem, int len)
  790. {
  791. /*
  792. * This routine will setup the DMA and start it.
  793. */
  794. dbg(DBG_TX, "In fst_tx_dma %p %p %d\n", skb, mem, len);
  795. if (card->dmatx_in_progress) {
  796. dbg(DBG_ASS, "In fst_tx_dma while dma in progress\n");
  797. }
  798. outl((unsigned long) skb, card->pci_conf + DMAPADR1); /* Copy from here */
  799. outl((unsigned long) mem, card->pci_conf + DMALADR1); /* to here */
  800. outl(len, card->pci_conf + DMASIZ1); /* for this length */
  801. outl(0x000000004, card->pci_conf + DMADPR1); /* In this direction */
  802. /*
  803. * We use the dmatx_in_progress to flag the channel as busy
  804. */
  805. card->dmatx_in_progress = 1;
  806. outb(0x03, card->pci_conf + DMACSR1); /* Start the transfer */
  807. }
  808. /* Issue a Mailbox command for a port.
  809. * Note we issue them on a fire and forget basis, not expecting to see an
  810. * error and not waiting for completion.
  811. */
  812. static void
  813. fst_issue_cmd(struct fst_port_info *port, unsigned short cmd)
  814. {
  815. struct fst_card_info *card;
  816. unsigned short mbval;
  817. unsigned long flags;
  818. int safety;
  819. card = port->card;
  820. spin_lock_irqsave(&card->card_lock, flags);
  821. mbval = FST_RDW(card, portMailbox[port->index][0]);
  822. safety = 0;
  823. /* Wait for any previous command to complete */
  824. while (mbval > NAK) {
  825. spin_unlock_irqrestore(&card->card_lock, flags);
  826. schedule_timeout_uninterruptible(1);
  827. spin_lock_irqsave(&card->card_lock, flags);
  828. if (++safety > 2000) {
  829. printk_err("Mailbox safety timeout\n");
  830. break;
  831. }
  832. mbval = FST_RDW(card, portMailbox[port->index][0]);
  833. }
  834. if (safety > 0) {
  835. dbg(DBG_CMD, "Mailbox clear after %d jiffies\n", safety);
  836. }
  837. if (mbval == NAK) {
  838. dbg(DBG_CMD, "issue_cmd: previous command was NAK'd\n");
  839. }
  840. FST_WRW(card, portMailbox[port->index][0], cmd);
  841. if (cmd == ABORTTX || cmd == STARTPORT) {
  842. port->txpos = 0;
  843. port->txipos = 0;
  844. port->start = 0;
  845. }
  846. spin_unlock_irqrestore(&card->card_lock, flags);
  847. }
  848. /* Port output signals control
  849. */
  850. static inline void
  851. fst_op_raise(struct fst_port_info *port, unsigned int outputs)
  852. {
  853. outputs |= FST_RDL(port->card, v24OpSts[port->index]);
  854. FST_WRL(port->card, v24OpSts[port->index], outputs);
  855. if (port->run)
  856. fst_issue_cmd(port, SETV24O);
  857. }
  858. static inline void
  859. fst_op_lower(struct fst_port_info *port, unsigned int outputs)
  860. {
  861. outputs = ~outputs & FST_RDL(port->card, v24OpSts[port->index]);
  862. FST_WRL(port->card, v24OpSts[port->index], outputs);
  863. if (port->run)
  864. fst_issue_cmd(port, SETV24O);
  865. }
  866. /*
  867. * Setup port Rx buffers
  868. */
  869. static void
  870. fst_rx_config(struct fst_port_info *port)
  871. {
  872. int i;
  873. int pi;
  874. unsigned int offset;
  875. unsigned long flags;
  876. struct fst_card_info *card;
  877. pi = port->index;
  878. card = port->card;
  879. spin_lock_irqsave(&card->card_lock, flags);
  880. for (i = 0; i < NUM_RX_BUFFER; i++) {
  881. offset = BUF_OFFSET(rxBuffer[pi][i][0]);
  882. FST_WRW(card, rxDescrRing[pi][i].ladr, (u16) offset);
  883. FST_WRB(card, rxDescrRing[pi][i].hadr, (u8) (offset >> 16));
  884. FST_WRW(card, rxDescrRing[pi][i].bcnt, cnv_bcnt(LEN_RX_BUFFER));
  885. FST_WRW(card, rxDescrRing[pi][i].mcnt, LEN_RX_BUFFER);
  886. FST_WRB(card, rxDescrRing[pi][i].bits, DMA_OWN);
  887. }
  888. port->rxpos = 0;
  889. spin_unlock_irqrestore(&card->card_lock, flags);
  890. }
  891. /*
  892. * Setup port Tx buffers
  893. */
  894. static void
  895. fst_tx_config(struct fst_port_info *port)
  896. {
  897. int i;
  898. int pi;
  899. unsigned int offset;
  900. unsigned long flags;
  901. struct fst_card_info *card;
  902. pi = port->index;
  903. card = port->card;
  904. spin_lock_irqsave(&card->card_lock, flags);
  905. for (i = 0; i < NUM_TX_BUFFER; i++) {
  906. offset = BUF_OFFSET(txBuffer[pi][i][0]);
  907. FST_WRW(card, txDescrRing[pi][i].ladr, (u16) offset);
  908. FST_WRB(card, txDescrRing[pi][i].hadr, (u8) (offset >> 16));
  909. FST_WRW(card, txDescrRing[pi][i].bcnt, 0);
  910. FST_WRB(card, txDescrRing[pi][i].bits, 0);
  911. }
  912. port->txpos = 0;
  913. port->txipos = 0;
  914. port->start = 0;
  915. spin_unlock_irqrestore(&card->card_lock, flags);
  916. }
  917. /* TE1 Alarm change interrupt event
  918. */
  919. static void
  920. fst_intr_te1_alarm(struct fst_card_info *card, struct fst_port_info *port)
  921. {
  922. u8 los;
  923. u8 rra;
  924. u8 ais;
  925. los = FST_RDB(card, suStatus.lossOfSignal);
  926. rra = FST_RDB(card, suStatus.receiveRemoteAlarm);
  927. ais = FST_RDB(card, suStatus.alarmIndicationSignal);
  928. if (los) {
  929. /*
  930. * Lost the link
  931. */
  932. if (netif_carrier_ok(port_to_dev(port))) {
  933. dbg(DBG_INTR, "Net carrier off\n");
  934. netif_carrier_off(port_to_dev(port));
  935. }
  936. } else {
  937. /*
  938. * Link available
  939. */
  940. if (!netif_carrier_ok(port_to_dev(port))) {
  941. dbg(DBG_INTR, "Net carrier on\n");
  942. netif_carrier_on(port_to_dev(port));
  943. }
  944. }
  945. if (los)
  946. dbg(DBG_INTR, "Assert LOS Alarm\n");
  947. else
  948. dbg(DBG_INTR, "De-assert LOS Alarm\n");
  949. if (rra)
  950. dbg(DBG_INTR, "Assert RRA Alarm\n");
  951. else
  952. dbg(DBG_INTR, "De-assert RRA Alarm\n");
  953. if (ais)
  954. dbg(DBG_INTR, "Assert AIS Alarm\n");
  955. else
  956. dbg(DBG_INTR, "De-assert AIS Alarm\n");
  957. }
  958. /* Control signal change interrupt event
  959. */
  960. static void
  961. fst_intr_ctlchg(struct fst_card_info *card, struct fst_port_info *port)
  962. {
  963. int signals;
  964. signals = FST_RDL(card, v24DebouncedSts[port->index]);
  965. if (signals & (((port->hwif == X21) || (port->hwif == X21D))
  966. ? IPSTS_INDICATE : IPSTS_DCD)) {
  967. if (!netif_carrier_ok(port_to_dev(port))) {
  968. dbg(DBG_INTR, "DCD active\n");
  969. netif_carrier_on(port_to_dev(port));
  970. }
  971. } else {
  972. if (netif_carrier_ok(port_to_dev(port))) {
  973. dbg(DBG_INTR, "DCD lost\n");
  974. netif_carrier_off(port_to_dev(port));
  975. }
  976. }
  977. }
  978. /* Log Rx Errors
  979. */
  980. static void
  981. fst_log_rx_error(struct fst_card_info *card, struct fst_port_info *port,
  982. unsigned char dmabits, int rxp, unsigned short len)
  983. {
  984. struct net_device *dev = port_to_dev(port);
  985. /*
  986. * Increment the appropriate error counter
  987. */
  988. dev->stats.rx_errors++;
  989. if (dmabits & RX_OFLO) {
  990. dev->stats.rx_fifo_errors++;
  991. dbg(DBG_ASS, "Rx fifo error on card %d port %d buffer %d\n",
  992. card->card_no, port->index, rxp);
  993. }
  994. if (dmabits & RX_CRC) {
  995. dev->stats.rx_crc_errors++;
  996. dbg(DBG_ASS, "Rx crc error on card %d port %d\n",
  997. card->card_no, port->index);
  998. }
  999. if (dmabits & RX_FRAM) {
  1000. dev->stats.rx_frame_errors++;
  1001. dbg(DBG_ASS, "Rx frame error on card %d port %d\n",
  1002. card->card_no, port->index);
  1003. }
  1004. if (dmabits == (RX_STP | RX_ENP)) {
  1005. dev->stats.rx_length_errors++;
  1006. dbg(DBG_ASS, "Rx length error (%d) on card %d port %d\n",
  1007. len, card->card_no, port->index);
  1008. }
  1009. }
  1010. /* Rx Error Recovery
  1011. */
  1012. static void
  1013. fst_recover_rx_error(struct fst_card_info *card, struct fst_port_info *port,
  1014. unsigned char dmabits, int rxp, unsigned short len)
  1015. {
  1016. int i;
  1017. int pi;
  1018. pi = port->index;
  1019. /*
  1020. * Discard buffer descriptors until we see the start of the
  1021. * next frame. Note that for long frames this could be in
  1022. * a subsequent interrupt.
  1023. */
  1024. i = 0;
  1025. while ((dmabits & (DMA_OWN | RX_STP)) == 0) {
  1026. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1027. rxp = (rxp+1) % NUM_RX_BUFFER;
  1028. if (++i > NUM_RX_BUFFER) {
  1029. dbg(DBG_ASS, "intr_rx: Discarding more bufs"
  1030. " than we have\n");
  1031. break;
  1032. }
  1033. dmabits = FST_RDB(card, rxDescrRing[pi][rxp].bits);
  1034. dbg(DBG_ASS, "DMA Bits of next buffer was %x\n", dmabits);
  1035. }
  1036. dbg(DBG_ASS, "There were %d subsequent buffers in error\n", i);
  1037. /* Discard the terminal buffer */
  1038. if (!(dmabits & DMA_OWN)) {
  1039. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1040. rxp = (rxp+1) % NUM_RX_BUFFER;
  1041. }
  1042. port->rxpos = rxp;
  1043. return;
  1044. }
  1045. /* Rx complete interrupt
  1046. */
  1047. static void
  1048. fst_intr_rx(struct fst_card_info *card, struct fst_port_info *port)
  1049. {
  1050. unsigned char dmabits;
  1051. int pi;
  1052. int rxp;
  1053. int rx_status;
  1054. unsigned short len;
  1055. struct sk_buff *skb;
  1056. struct net_device *dev = port_to_dev(port);
  1057. /* Check we have a buffer to process */
  1058. pi = port->index;
  1059. rxp = port->rxpos;
  1060. dmabits = FST_RDB(card, rxDescrRing[pi][rxp].bits);
  1061. if (dmabits & DMA_OWN) {
  1062. dbg(DBG_RX | DBG_INTR, "intr_rx: No buffer port %d pos %d\n",
  1063. pi, rxp);
  1064. return;
  1065. }
  1066. if (card->dmarx_in_progress) {
  1067. return;
  1068. }
  1069. /* Get buffer length */
  1070. len = FST_RDW(card, rxDescrRing[pi][rxp].mcnt);
  1071. /* Discard the CRC */
  1072. len -= 2;
  1073. if (len == 0) {
  1074. /*
  1075. * This seems to happen on the TE1 interface sometimes
  1076. * so throw the frame away and log the event.
  1077. */
  1078. printk_err("Frame received with 0 length. Card %d Port %d\n",
  1079. card->card_no, port->index);
  1080. /* Return descriptor to card */
  1081. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1082. rxp = (rxp+1) % NUM_RX_BUFFER;
  1083. port->rxpos = rxp;
  1084. return;
  1085. }
  1086. /* Check buffer length and for other errors. We insist on one packet
  1087. * in one buffer. This simplifies things greatly and since we've
  1088. * allocated 8K it shouldn't be a real world limitation
  1089. */
  1090. dbg(DBG_RX, "intr_rx: %d,%d: flags %x len %d\n", pi, rxp, dmabits, len);
  1091. if (dmabits != (RX_STP | RX_ENP) || len > LEN_RX_BUFFER - 2) {
  1092. fst_log_rx_error(card, port, dmabits, rxp, len);
  1093. fst_recover_rx_error(card, port, dmabits, rxp, len);
  1094. return;
  1095. }
  1096. /* Allocate SKB */
  1097. if ((skb = dev_alloc_skb(len)) == NULL) {
  1098. dbg(DBG_RX, "intr_rx: can't allocate buffer\n");
  1099. dev->stats.rx_dropped++;
  1100. /* Return descriptor to card */
  1101. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1102. rxp = (rxp+1) % NUM_RX_BUFFER;
  1103. port->rxpos = rxp;
  1104. return;
  1105. }
  1106. /*
  1107. * We know the length we need to receive, len.
  1108. * It's not worth using the DMA for reads of less than
  1109. * FST_MIN_DMA_LEN
  1110. */
  1111. if ((len < FST_MIN_DMA_LEN) || (card->family == FST_FAMILY_TXP)) {
  1112. memcpy_fromio(skb_put(skb, len),
  1113. card->mem + BUF_OFFSET(rxBuffer[pi][rxp][0]),
  1114. len);
  1115. /* Reset buffer descriptor */
  1116. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1117. /* Update stats */
  1118. dev->stats.rx_packets++;
  1119. dev->stats.rx_bytes += len;
  1120. /* Push upstream */
  1121. dbg(DBG_RX, "Pushing frame up the stack\n");
  1122. if (port->mode == FST_RAW)
  1123. skb->protocol = farsync_type_trans(skb, dev);
  1124. else
  1125. skb->protocol = hdlc_type_trans(skb, dev);
  1126. rx_status = netif_rx(skb);
  1127. fst_process_rx_status(rx_status, port_to_dev(port)->name);
  1128. if (rx_status == NET_RX_DROP)
  1129. dev->stats.rx_dropped++;
  1130. } else {
  1131. card->dma_skb_rx = skb;
  1132. card->dma_port_rx = port;
  1133. card->dma_len_rx = len;
  1134. card->dma_rxpos = rxp;
  1135. fst_rx_dma(card, (char *) card->rx_dma_handle_card,
  1136. (char *) BUF_OFFSET(rxBuffer[pi][rxp][0]), len);
  1137. }
  1138. if (rxp != port->rxpos) {
  1139. dbg(DBG_ASS, "About to increment rxpos by more than 1\n");
  1140. dbg(DBG_ASS, "rxp = %d rxpos = %d\n", rxp, port->rxpos);
  1141. }
  1142. rxp = (rxp+1) % NUM_RX_BUFFER;
  1143. port->rxpos = rxp;
  1144. }
  1145. /*
  1146. * The bottom halfs to the ISR
  1147. *
  1148. */
  1149. static void
  1150. do_bottom_half_tx(struct fst_card_info *card)
  1151. {
  1152. struct fst_port_info *port;
  1153. int pi;
  1154. int txq_length;
  1155. struct sk_buff *skb;
  1156. unsigned long flags;
  1157. struct net_device *dev;
  1158. /*
  1159. * Find a free buffer for the transmit
  1160. * Step through each port on this card
  1161. */
  1162. dbg(DBG_TX, "do_bottom_half_tx\n");
  1163. for (pi = 0, port = card->ports; pi < card->nports; pi++, port++) {
  1164. if (!port->run)
  1165. continue;
  1166. dev = port_to_dev(port);
  1167. while (!(FST_RDB(card, txDescrRing[pi][port->txpos].bits) &
  1168. DMA_OWN)
  1169. && !(card->dmatx_in_progress)) {
  1170. /*
  1171. * There doesn't seem to be a txdone event per-se
  1172. * We seem to have to deduce it, by checking the DMA_OWN
  1173. * bit on the next buffer we think we can use
  1174. */
  1175. spin_lock_irqsave(&card->card_lock, flags);
  1176. if ((txq_length = port->txqe - port->txqs) < 0) {
  1177. /*
  1178. * This is the case where one has wrapped and the
  1179. * maths gives us a negative number
  1180. */
  1181. txq_length = txq_length + FST_TXQ_DEPTH;
  1182. }
  1183. spin_unlock_irqrestore(&card->card_lock, flags);
  1184. if (txq_length > 0) {
  1185. /*
  1186. * There is something to send
  1187. */
  1188. spin_lock_irqsave(&card->card_lock, flags);
  1189. skb = port->txq[port->txqs];
  1190. port->txqs++;
  1191. if (port->txqs == FST_TXQ_DEPTH) {
  1192. port->txqs = 0;
  1193. }
  1194. spin_unlock_irqrestore(&card->card_lock, flags);
  1195. /*
  1196. * copy the data and set the required indicators on the
  1197. * card.
  1198. */
  1199. FST_WRW(card, txDescrRing[pi][port->txpos].bcnt,
  1200. cnv_bcnt(skb->len));
  1201. if ((skb->len < FST_MIN_DMA_LEN)
  1202. || (card->family == FST_FAMILY_TXP)) {
  1203. /* Enqueue the packet with normal io */
  1204. memcpy_toio(card->mem +
  1205. BUF_OFFSET(txBuffer[pi]
  1206. [port->
  1207. txpos][0]),
  1208. skb->data, skb->len);
  1209. FST_WRB(card,
  1210. txDescrRing[pi][port->txpos].
  1211. bits,
  1212. DMA_OWN | TX_STP | TX_ENP);
  1213. dev->stats.tx_packets++;
  1214. dev->stats.tx_bytes += skb->len;
  1215. dev->trans_start = jiffies;
  1216. } else {
  1217. /* Or do it through dma */
  1218. memcpy(card->tx_dma_handle_host,
  1219. skb->data, skb->len);
  1220. card->dma_port_tx = port;
  1221. card->dma_len_tx = skb->len;
  1222. card->dma_txpos = port->txpos;
  1223. fst_tx_dma(card,
  1224. (char *) card->
  1225. tx_dma_handle_card,
  1226. (char *)
  1227. BUF_OFFSET(txBuffer[pi]
  1228. [port->txpos][0]),
  1229. skb->len);
  1230. }
  1231. if (++port->txpos >= NUM_TX_BUFFER)
  1232. port->txpos = 0;
  1233. /*
  1234. * If we have flow control on, can we now release it?
  1235. */
  1236. if (port->start) {
  1237. if (txq_length < fst_txq_low) {
  1238. netif_wake_queue(port_to_dev
  1239. (port));
  1240. port->start = 0;
  1241. }
  1242. }
  1243. dev_kfree_skb(skb);
  1244. } else {
  1245. /*
  1246. * Nothing to send so break out of the while loop
  1247. */
  1248. break;
  1249. }
  1250. }
  1251. }
  1252. }
  1253. static void
  1254. do_bottom_half_rx(struct fst_card_info *card)
  1255. {
  1256. struct fst_port_info *port;
  1257. int pi;
  1258. int rx_count = 0;
  1259. /* Check for rx completions on all ports on this card */
  1260. dbg(DBG_RX, "do_bottom_half_rx\n");
  1261. for (pi = 0, port = card->ports; pi < card->nports; pi++, port++) {
  1262. if (!port->run)
  1263. continue;
  1264. while (!(FST_RDB(card, rxDescrRing[pi][port->rxpos].bits)
  1265. & DMA_OWN) && !(card->dmarx_in_progress)) {
  1266. if (rx_count > fst_max_reads) {
  1267. /*
  1268. * Don't spend forever in receive processing
  1269. * Schedule another event
  1270. */
  1271. fst_q_work_item(&fst_work_intq, card->card_no);
  1272. tasklet_schedule(&fst_int_task);
  1273. break; /* Leave the loop */
  1274. }
  1275. fst_intr_rx(card, port);
  1276. rx_count++;
  1277. }
  1278. }
  1279. }
  1280. /*
  1281. * The interrupt service routine
  1282. * Dev_id is our fst_card_info pointer
  1283. */
  1284. static irqreturn_t
  1285. fst_intr(int dummy, void *dev_id)
  1286. {
  1287. struct fst_card_info *card = dev_id;
  1288. struct fst_port_info *port;
  1289. int rdidx; /* Event buffer indices */
  1290. int wridx;
  1291. int event; /* Actual event for processing */
  1292. unsigned int dma_intcsr = 0;
  1293. unsigned int do_card_interrupt;
  1294. unsigned int int_retry_count;
  1295. /*
  1296. * Check to see if the interrupt was for this card
  1297. * return if not
  1298. * Note that the call to clear the interrupt is important
  1299. */
  1300. dbg(DBG_INTR, "intr: %d %p\n", card->irq, card);
  1301. if (card->state != FST_RUNNING) {
  1302. printk_err
  1303. ("Interrupt received for card %d in a non running state (%d)\n",
  1304. card->card_no, card->state);
  1305. /*
  1306. * It is possible to really be running, i.e. we have re-loaded
  1307. * a running card
  1308. * Clear and reprime the interrupt source
  1309. */
  1310. fst_clear_intr(card);
  1311. return IRQ_HANDLED;
  1312. }
  1313. /* Clear and reprime the interrupt source */
  1314. fst_clear_intr(card);
  1315. /*
  1316. * Is the interrupt for this card (handshake == 1)
  1317. */
  1318. do_card_interrupt = 0;
  1319. if (FST_RDB(card, interruptHandshake) == 1) {
  1320. do_card_interrupt += FST_CARD_INT;
  1321. /* Set the software acknowledge */
  1322. FST_WRB(card, interruptHandshake, 0xEE);
  1323. }
  1324. if (card->family == FST_FAMILY_TXU) {
  1325. /*
  1326. * Is it a DMA Interrupt
  1327. */
  1328. dma_intcsr = inl(card->pci_conf + INTCSR_9054);
  1329. if (dma_intcsr & 0x00200000) {
  1330. /*
  1331. * DMA Channel 0 (Rx transfer complete)
  1332. */
  1333. dbg(DBG_RX, "DMA Rx xfer complete\n");
  1334. outb(0x8, card->pci_conf + DMACSR0);
  1335. fst_rx_dma_complete(card, card->dma_port_rx,
  1336. card->dma_len_rx, card->dma_skb_rx,
  1337. card->dma_rxpos);
  1338. card->dmarx_in_progress = 0;
  1339. do_card_interrupt += FST_RX_DMA_INT;
  1340. }
  1341. if (dma_intcsr & 0x00400000) {
  1342. /*
  1343. * DMA Channel 1 (Tx transfer complete)
  1344. */
  1345. dbg(DBG_TX, "DMA Tx xfer complete\n");
  1346. outb(0x8, card->pci_conf + DMACSR1);
  1347. fst_tx_dma_complete(card, card->dma_port_tx,
  1348. card->dma_len_tx, card->dma_txpos);
  1349. card->dmatx_in_progress = 0;
  1350. do_card_interrupt += FST_TX_DMA_INT;
  1351. }
  1352. }
  1353. /*
  1354. * Have we been missing Interrupts
  1355. */
  1356. int_retry_count = FST_RDL(card, interruptRetryCount);
  1357. if (int_retry_count) {
  1358. dbg(DBG_ASS, "Card %d int_retry_count is %d\n",
  1359. card->card_no, int_retry_count);
  1360. FST_WRL(card, interruptRetryCount, 0);
  1361. }
  1362. if (!do_card_interrupt) {
  1363. return IRQ_HANDLED;
  1364. }
  1365. /* Scehdule the bottom half of the ISR */
  1366. fst_q_work_item(&fst_work_intq, card->card_no);
  1367. tasklet_schedule(&fst_int_task);
  1368. /* Drain the event queue */
  1369. rdidx = FST_RDB(card, interruptEvent.rdindex) & 0x1f;
  1370. wridx = FST_RDB(card, interruptEvent.wrindex) & 0x1f;
  1371. while (rdidx != wridx) {
  1372. event = FST_RDB(card, interruptEvent.evntbuff[rdidx]);
  1373. port = &card->ports[event & 0x03];
  1374. dbg(DBG_INTR, "Processing Interrupt event: %x\n", event);
  1375. switch (event) {
  1376. case TE1_ALMA:
  1377. dbg(DBG_INTR, "TE1 Alarm intr\n");
  1378. if (port->run)
  1379. fst_intr_te1_alarm(card, port);
  1380. break;
  1381. case CTLA_CHG:
  1382. case CTLB_CHG:
  1383. case CTLC_CHG:
  1384. case CTLD_CHG:
  1385. if (port->run)
  1386. fst_intr_ctlchg(card, port);
  1387. break;
  1388. case ABTA_SENT:
  1389. case ABTB_SENT:
  1390. case ABTC_SENT:
  1391. case ABTD_SENT:
  1392. dbg(DBG_TX, "Abort complete port %d\n", port->index);
  1393. break;
  1394. case TXA_UNDF:
  1395. case TXB_UNDF:
  1396. case TXC_UNDF:
  1397. case TXD_UNDF:
  1398. /* Difficult to see how we'd get this given that we
  1399. * always load up the entire packet for DMA.
  1400. */
  1401. dbg(DBG_TX, "Tx underflow port %d\n", port->index);
  1402. port_to_dev(port)->stats.tx_errors++;
  1403. port_to_dev(port)->stats.tx_fifo_errors++;
  1404. dbg(DBG_ASS, "Tx underflow on card %d port %d\n",
  1405. card->card_no, port->index);
  1406. break;
  1407. case INIT_CPLT:
  1408. dbg(DBG_INIT, "Card init OK intr\n");
  1409. break;
  1410. case INIT_FAIL:
  1411. dbg(DBG_INIT, "Card init FAILED intr\n");
  1412. card->state = FST_IFAILED;
  1413. break;
  1414. default:
  1415. printk_err("intr: unknown card event %d. ignored\n",
  1416. event);
  1417. break;
  1418. }
  1419. /* Bump and wrap the index */
  1420. if (++rdidx >= MAX_CIRBUFF)
  1421. rdidx = 0;
  1422. }
  1423. FST_WRB(card, interruptEvent.rdindex, rdidx);
  1424. return IRQ_HANDLED;
  1425. }
  1426. /* Check that the shared memory configuration is one that we can handle
  1427. * and that some basic parameters are correct
  1428. */
  1429. static void
  1430. check_started_ok(struct fst_card_info *card)
  1431. {
  1432. int i;
  1433. /* Check structure version and end marker */
  1434. if (FST_RDW(card, smcVersion) != SMC_VERSION) {
  1435. printk_err("Bad shared memory version %d expected %d\n",
  1436. FST_RDW(card, smcVersion), SMC_VERSION);
  1437. card->state = FST_BADVERSION;
  1438. return;
  1439. }
  1440. if (FST_RDL(card, endOfSmcSignature) != END_SIG) {
  1441. printk_err("Missing shared memory signature\n");
  1442. card->state = FST_BADVERSION;
  1443. return;
  1444. }
  1445. /* Firmware status flag, 0x00 = initialising, 0x01 = OK, 0xFF = fail */
  1446. if ((i = FST_RDB(card, taskStatus)) == 0x01) {
  1447. card->state = FST_RUNNING;
  1448. } else if (i == 0xFF) {
  1449. printk_err("Firmware initialisation failed. Card halted\n");
  1450. card->state = FST_HALTED;
  1451. return;
  1452. } else if (i != 0x00) {
  1453. printk_err("Unknown firmware status 0x%x\n", i);
  1454. card->state = FST_HALTED;
  1455. return;
  1456. }
  1457. /* Finally check the number of ports reported by firmware against the
  1458. * number we assumed at card detection. Should never happen with
  1459. * existing firmware etc so we just report it for the moment.
  1460. */
  1461. if (FST_RDL(card, numberOfPorts) != card->nports) {
  1462. printk_warn("Port count mismatch on card %d."
  1463. " Firmware thinks %d we say %d\n", card->card_no,
  1464. FST_RDL(card, numberOfPorts), card->nports);
  1465. }
  1466. }
  1467. static int
  1468. set_conf_from_info(struct fst_card_info *card, struct fst_port_info *port,
  1469. struct fstioc_info *info)
  1470. {
  1471. int err;
  1472. unsigned char my_framing;
  1473. /* Set things according to the user set valid flags
  1474. * Several of the old options have been invalidated/replaced by the
  1475. * generic hdlc package.
  1476. */
  1477. err = 0;
  1478. if (info->valid & FSTVAL_PROTO) {
  1479. if (info->proto == FST_RAW)
  1480. port->mode = FST_RAW;
  1481. else
  1482. port->mode = FST_GEN_HDLC;
  1483. }
  1484. if (info->valid & FSTVAL_CABLE)
  1485. err = -EINVAL;
  1486. if (info->valid & FSTVAL_SPEED)
  1487. err = -EINVAL;
  1488. if (info->valid & FSTVAL_PHASE)
  1489. FST_WRB(card, portConfig[port->index].invertClock,
  1490. info->invertClock);
  1491. if (info->valid & FSTVAL_MODE)
  1492. FST_WRW(card, cardMode, info->cardMode);
  1493. if (info->valid & FSTVAL_TE1) {
  1494. FST_WRL(card, suConfig.dataRate, info->lineSpeed);
  1495. FST_WRB(card, suConfig.clocking, info->clockSource);
  1496. my_framing = FRAMING_E1;
  1497. if (info->framing == E1)
  1498. my_framing = FRAMING_E1;
  1499. if (info->framing == T1)
  1500. my_framing = FRAMING_T1;
  1501. if (info->framing == J1)
  1502. my_framing = FRAMING_J1;
  1503. FST_WRB(card, suConfig.framing, my_framing);
  1504. FST_WRB(card, suConfig.structure, info->structure);
  1505. FST_WRB(card, suConfig.interface, info->interface);
  1506. FST_WRB(card, suConfig.coding, info->coding);
  1507. FST_WRB(card, suConfig.lineBuildOut, info->lineBuildOut);
  1508. FST_WRB(card, suConfig.equalizer, info->equalizer);
  1509. FST_WRB(card, suConfig.transparentMode, info->transparentMode);
  1510. FST_WRB(card, suConfig.loopMode, info->loopMode);
  1511. FST_WRB(card, suConfig.range, info->range);
  1512. FST_WRB(card, suConfig.txBufferMode, info->txBufferMode);
  1513. FST_WRB(card, suConfig.rxBufferMode, info->rxBufferMode);
  1514. FST_WRB(card, suConfig.startingSlot, info->startingSlot);
  1515. FST_WRB(card, suConfig.losThreshold, info->losThreshold);
  1516. if (info->idleCode)
  1517. FST_WRB(card, suConfig.enableIdleCode, 1);
  1518. else
  1519. FST_WRB(card, suConfig.enableIdleCode, 0);
  1520. FST_WRB(card, suConfig.idleCode, info->idleCode);
  1521. #if FST_DEBUG
  1522. if (info->valid & FSTVAL_TE1) {
  1523. printk("Setting TE1 data\n");
  1524. printk("Line Speed = %d\n", info->lineSpeed);
  1525. printk("Start slot = %d\n", info->startingSlot);
  1526. printk("Clock source = %d\n", info->clockSource);
  1527. printk("Framing = %d\n", my_framing);
  1528. printk("Structure = %d\n", info->structure);
  1529. printk("interface = %d\n", info->interface);
  1530. printk("Coding = %d\n", info->coding);
  1531. printk("Line build out = %d\n", info->lineBuildOut);
  1532. printk("Equaliser = %d\n", info->equalizer);
  1533. printk("Transparent mode = %d\n",
  1534. info->transparentMode);
  1535. printk("Loop mode = %d\n", info->loopMode);
  1536. printk("Range = %d\n", info->range);
  1537. printk("Tx Buffer mode = %d\n", info->txBufferMode);
  1538. printk("Rx Buffer mode = %d\n", info->rxBufferMode);
  1539. printk("LOS Threshold = %d\n", info->losThreshold);
  1540. printk("Idle Code = %d\n", info->idleCode);
  1541. }
  1542. #endif
  1543. }
  1544. #if FST_DEBUG
  1545. if (info->valid & FSTVAL_DEBUG) {
  1546. fst_debug_mask = info->debug;
  1547. }
  1548. #endif
  1549. return err;
  1550. }
  1551. static void
  1552. gather_conf_info(struct fst_card_info *card, struct fst_port_info *port,
  1553. struct fstioc_info *info)
  1554. {
  1555. int i;
  1556. memset(info, 0, sizeof (struct fstioc_info));
  1557. i = port->index;
  1558. info->kernelVersion = LINUX_VERSION_CODE;
  1559. info->nports = card->nports;
  1560. info->type = card->type;
  1561. info->state = card->state;
  1562. info->proto = FST_GEN_HDLC;
  1563. info->index = i;
  1564. #if FST_DEBUG
  1565. info->debug = fst_debug_mask;
  1566. #endif
  1567. /* Only mark information as valid if card is running.
  1568. * Copy the data anyway in case it is useful for diagnostics
  1569. */
  1570. info->valid = ((card->state == FST_RUNNING) ? FSTVAL_ALL : FSTVAL_CARD)
  1571. #if FST_DEBUG
  1572. | FSTVAL_DEBUG
  1573. #endif
  1574. ;
  1575. info->lineInterface = FST_RDW(card, portConfig[i].lineInterface);
  1576. info->internalClock = FST_RDB(card, portConfig[i].internalClock);
  1577. info->lineSpeed = FST_RDL(card, portConfig[i].lineSpeed);
  1578. info->invertClock = FST_RDB(card, portConfig[i].invertClock);
  1579. info->v24IpSts = FST_RDL(card, v24IpSts[i]);
  1580. info->v24OpSts = FST_RDL(card, v24OpSts[i]);
  1581. info->clockStatus = FST_RDW(card, clockStatus[i]);
  1582. info->cableStatus = FST_RDW(card, cableStatus);
  1583. info->cardMode = FST_RDW(card, cardMode);
  1584. info->smcFirmwareVersion = FST_RDL(card, smcFirmwareVersion);
  1585. /*
  1586. * The T2U can report cable presence for both A or B
  1587. * in bits 0 and 1 of cableStatus. See which port we are and
  1588. * do the mapping.
  1589. */
  1590. if (card->family == FST_FAMILY_TXU) {
  1591. if (port->index == 0) {
  1592. /*
  1593. * Port A
  1594. */
  1595. info->cableStatus = info->cableStatus & 1;
  1596. } else {
  1597. /*
  1598. * Port B
  1599. */
  1600. info->cableStatus = info->cableStatus >> 1;
  1601. info->cableStatus = info->cableStatus & 1;
  1602. }
  1603. }
  1604. /*
  1605. * Some additional bits if we are TE1
  1606. */
  1607. if (card->type == FST_TYPE_TE1) {
  1608. info->lineSpeed = FST_RDL(card, suConfig.dataRate);
  1609. info->clockSource = FST_RDB(card, suConfig.clocking);
  1610. info->framing = FST_RDB(card, suConfig.framing);
  1611. info->structure = FST_RDB(card, suConfig.structure);
  1612. info->interface = FST_RDB(card, suConfig.interface);
  1613. info->coding = FST_RDB(card, suConfig.coding);
  1614. info->lineBuildOut = FST_RDB(card, suConfig.lineBuildOut);
  1615. info->equalizer = FST_RDB(card, suConfig.equalizer);
  1616. info->loopMode = FST_RDB(card, suConfig.loopMode);
  1617. info->range = FST_RDB(card, suConfig.range);
  1618. info->txBufferMode = FST_RDB(card, suConfig.txBufferMode);
  1619. info->rxBufferMode = FST_RDB(card, suConfig.rxBufferMode);
  1620. info->startingSlot = FST_RDB(card, suConfig.startingSlot);
  1621. info->losThreshold = FST_RDB(card, suConfig.losThreshold);
  1622. if (FST_RDB(card, suConfig.enableIdleCode))
  1623. info->idleCode = FST_RDB(card, suConfig.idleCode);
  1624. else
  1625. info->idleCode = 0;
  1626. info->receiveBufferDelay =
  1627. FST_RDL(card, suStatus.receiveBufferDelay);
  1628. info->framingErrorCount =
  1629. FST_RDL(card, suStatus.framingErrorCount);
  1630. info->codeViolationCount =
  1631. FST_RDL(card, suStatus.codeViolationCount);
  1632. info->crcErrorCount = FST_RDL(card, suStatus.crcErrorCount);
  1633. info->lineAttenuation = FST_RDL(card, suStatus.lineAttenuation);
  1634. info->lossOfSignal = FST_RDB(card, suStatus.lossOfSignal);
  1635. info->receiveRemoteAlarm =
  1636. FST_RDB(card, suStatus.receiveRemoteAlarm);
  1637. info->alarmIndicationSignal =
  1638. FST_RDB(card, suStatus.alarmIndicationSignal);
  1639. }
  1640. }
  1641. static int
  1642. fst_set_iface(struct fst_card_info *card, struct fst_port_info *port,
  1643. struct ifreq *ifr)
  1644. {
  1645. sync_serial_settings sync;
  1646. int i;
  1647. if (ifr->ifr_settings.size != sizeof (sync)) {
  1648. return -ENOMEM;
  1649. }
  1650. if (copy_from_user
  1651. (&sync, ifr->ifr_settings.ifs_ifsu.sync, sizeof (sync))) {
  1652. return -EFAULT;
  1653. }
  1654. if (sync.loopback)
  1655. return -EINVAL;
  1656. i = port->index;
  1657. switch (ifr->ifr_settings.type) {
  1658. case IF_IFACE_V35:
  1659. FST_WRW(card, portConfig[i].lineInterface, V35);
  1660. port->hwif = V35;
  1661. break;
  1662. case IF_IFACE_V24:
  1663. FST_WRW(card, portConfig[i].lineInterface, V24);
  1664. port->hwif = V24;
  1665. break;
  1666. case IF_IFACE_X21:
  1667. FST_WRW(card, portConfig[i].lineInterface, X21);
  1668. port->hwif = X21;
  1669. break;
  1670. case IF_IFACE_X21D:
  1671. FST_WRW(card, portConfig[i].lineInterface, X21D);
  1672. port->hwif = X21D;
  1673. break;
  1674. case IF_IFACE_T1:
  1675. FST_WRW(card, portConfig[i].lineInterface, T1);
  1676. port->hwif = T1;
  1677. break;
  1678. case IF_IFACE_E1:
  1679. FST_WRW(card, portConfig[i].lineInterface, E1);
  1680. port->hwif = E1;
  1681. break;
  1682. case IF_IFACE_SYNC_SERIAL:
  1683. break;
  1684. default:
  1685. return -EINVAL;
  1686. }
  1687. switch (sync.clock_type) {
  1688. case CLOCK_EXT:
  1689. FST_WRB(card, portConfig[i].internalClock, EXTCLK);
  1690. break;
  1691. case CLOCK_INT:
  1692. FST_WRB(card, portConfig[i].internalClock, INTCLK);
  1693. break;
  1694. default:
  1695. return -EINVAL;
  1696. }
  1697. FST_WRL(card, portConfig[i].lineSpeed, sync.clock_rate);
  1698. return 0;
  1699. }
  1700. static int
  1701. fst_get_iface(struct fst_card_info *card, struct fst_port_info *port,
  1702. struct ifreq *ifr)
  1703. {
  1704. sync_serial_settings sync;
  1705. int i;
  1706. /* First check what line type is set, we'll default to reporting X.21
  1707. * if nothing is set as IF_IFACE_SYNC_SERIAL implies it can't be
  1708. * changed
  1709. */
  1710. switch (port->hwif) {
  1711. case E1:
  1712. ifr->ifr_settings.type = IF_IFACE_E1;
  1713. break;
  1714. case T1:
  1715. ifr->ifr_settings.type = IF_IFACE_T1;
  1716. break;
  1717. case V35:
  1718. ifr->ifr_settings.type = IF_IFACE_V35;
  1719. break;
  1720. case V24:
  1721. ifr->ifr_settings.type = IF_IFACE_V24;
  1722. break;
  1723. case X21D:
  1724. ifr->ifr_settings.type = IF_IFACE_X21D;
  1725. break;
  1726. case X21:
  1727. default:
  1728. ifr->ifr_settings.type = IF_IFACE_X21;
  1729. break;
  1730. }
  1731. if (ifr->ifr_settings.size == 0) {
  1732. return 0; /* only type requested */
  1733. }
  1734. if (ifr->ifr_settings.size < sizeof (sync)) {
  1735. return -ENOMEM;
  1736. }
  1737. i = port->index;
  1738. sync.clock_rate = FST_RDL(card, portConfig[i].lineSpeed);
  1739. /* Lucky card and linux use same encoding here */
  1740. sync.clock_type = FST_RDB(card, portConfig[i].internalClock) ==
  1741. INTCLK ? CLOCK_INT : CLOCK_EXT;
  1742. sync.loopback = 0;
  1743. if (copy_to_user(ifr->ifr_settings.ifs_ifsu.sync, &sync, sizeof (sync))) {
  1744. return -EFAULT;
  1745. }
  1746. ifr->ifr_settings.size = sizeof (sync);
  1747. return 0;
  1748. }
  1749. static int
  1750. fst_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1751. {
  1752. struct fst_card_info *card;
  1753. struct fst_port_info *port;
  1754. struct fstioc_write wrthdr;
  1755. struct fstioc_info info;
  1756. unsigned long flags;
  1757. void *buf;
  1758. dbg(DBG_IOCTL, "ioctl: %x, %p\n", cmd, ifr->ifr_data);
  1759. port = dev_to_port(dev);
  1760. card = port->card;
  1761. if (!capable(CAP_NET_ADMIN))
  1762. return -EPERM;
  1763. switch (cmd) {
  1764. case FSTCPURESET:
  1765. fst_cpureset(card);
  1766. card->state = FST_RESET;
  1767. return 0;
  1768. case FSTCPURELEASE:
  1769. fst_cpurelease(card);
  1770. card->state = FST_STARTING;
  1771. return 0;
  1772. case FSTWRITE: /* Code write (download) */
  1773. /* First copy in the header with the length and offset of data
  1774. * to write
  1775. */
  1776. if (ifr->ifr_data == NULL) {
  1777. return -EINVAL;
  1778. }
  1779. if (copy_from_user(&wrthdr, ifr->ifr_data,
  1780. sizeof (struct fstioc_write))) {
  1781. return -EFAULT;
  1782. }
  1783. /* Sanity check the parameters. We don't support partial writes
  1784. * when going over the top
  1785. */
  1786. if (wrthdr.size > FST_MEMSIZE || wrthdr.offset > FST_MEMSIZE
  1787. || wrthdr.size + wrthdr.offset > FST_MEMSIZE) {
  1788. return -ENXIO;
  1789. }
  1790. /* Now copy the data to the card. */
  1791. buf = kmalloc(wrthdr.size, GFP_KERNEL);
  1792. if (!buf)
  1793. return -ENOMEM;
  1794. if (copy_from_user(buf,
  1795. ifr->ifr_data + sizeof (struct fstioc_write),
  1796. wrthdr.size)) {
  1797. kfree(buf);
  1798. return -EFAULT;
  1799. }
  1800. memcpy_toio(card->mem + wrthdr.offset, buf, wrthdr.size);
  1801. kfree(buf);
  1802. /* Writes to the memory of a card in the reset state constitute
  1803. * a download
  1804. */
  1805. if (card->state == FST_RESET) {
  1806. card->state = FST_DOWNLOAD;
  1807. }
  1808. return 0;
  1809. case FSTGETCONF:
  1810. /* If card has just been started check the shared memory config
  1811. * version and marker
  1812. */
  1813. if (card->state == FST_STARTING) {
  1814. check_started_ok(card);
  1815. /* If everything checked out enable card interrupts */
  1816. if (card->state == FST_RUNNING) {
  1817. spin_lock_irqsave(&card->card_lock, flags);
  1818. fst_enable_intr(card);
  1819. FST_WRB(card, interruptHandshake, 0xEE);
  1820. spin_unlock_irqrestore(&card->card_lock, flags);
  1821. }
  1822. }
  1823. if (ifr->ifr_data == NULL) {
  1824. return -EINVAL;
  1825. }
  1826. gather_conf_info(card, port, &info);
  1827. if (copy_to_user(ifr->ifr_data, &info, sizeof (info))) {
  1828. return -EFAULT;
  1829. }
  1830. return 0;
  1831. case FSTSETCONF:
  1832. /*
  1833. * Most of the settings have been moved to the generic ioctls
  1834. * this just covers debug and board ident now
  1835. */
  1836. if (card->state != FST_RUNNING) {
  1837. printk_err
  1838. ("Attempt to configure card %d in non-running state (%d)\n",
  1839. card->card_no, card->state);
  1840. return -EIO;
  1841. }
  1842. if (copy_from_user(&info, ifr->ifr_data, sizeof (info))) {
  1843. return -EFAULT;
  1844. }
  1845. return set_conf_from_info(card, port, &info);
  1846. case SIOCWANDEV:
  1847. switch (ifr->ifr_settings.type) {
  1848. case IF_GET_IFACE:
  1849. return fst_get_iface(card, port, ifr);
  1850. case IF_IFACE_SYNC_SERIAL:
  1851. case IF_IFACE_V35:
  1852. case IF_IFACE_V24:
  1853. case IF_IFACE_X21:
  1854. case IF_IFACE_X21D:
  1855. case IF_IFACE_T1:
  1856. case IF_IFACE_E1:
  1857. return fst_set_iface(card, port, ifr);
  1858. case IF_PROTO_RAW:
  1859. port->mode = FST_RAW;
  1860. return 0;
  1861. case IF_GET_PROTO:
  1862. if (port->mode == FST_RAW) {
  1863. ifr->ifr_settings.type = IF_PROTO_RAW;
  1864. return 0;
  1865. }
  1866. return hdlc_ioctl(dev, ifr, cmd);
  1867. default:
  1868. port->mode = FST_GEN_HDLC;
  1869. dbg(DBG_IOCTL, "Passing this type to hdlc %x\n",
  1870. ifr->ifr_settings.type);
  1871. return hdlc_ioctl(dev, ifr, cmd);
  1872. }
  1873. default:
  1874. /* Not one of ours. Pass through to HDLC package */
  1875. return hdlc_ioctl(dev, ifr, cmd);
  1876. }
  1877. }
  1878. static void
  1879. fst_openport(struct fst_port_info *port)
  1880. {
  1881. int signals;
  1882. int txq_length;
  1883. /* Only init things if card is actually running. This allows open to
  1884. * succeed for downloads etc.
  1885. */
  1886. if (port->card->state == FST_RUNNING) {
  1887. if (port->run) {
  1888. dbg(DBG_OPEN, "open: found port already running\n");
  1889. fst_issue_cmd(port, STOPPORT);
  1890. port->run = 0;
  1891. }
  1892. fst_rx_config(port);
  1893. fst_tx_config(port);
  1894. fst_op_raise(port, OPSTS_RTS | OPSTS_DTR);
  1895. fst_issue_cmd(port, STARTPORT);
  1896. port->run = 1;
  1897. signals = FST_RDL(port->card, v24DebouncedSts[port->index]);
  1898. if (signals & (((port->hwif == X21) || (port->hwif == X21D))
  1899. ? IPSTS_INDICATE : IPSTS_DCD))
  1900. netif_carrier_on(port_to_dev(port));
  1901. else
  1902. netif_carrier_off(port_to_dev(port));
  1903. txq_length = port->txqe - port->txqs;
  1904. port->txqe = 0;
  1905. port->txqs = 0;
  1906. }
  1907. }
  1908. static void
  1909. fst_closeport(struct fst_port_info *port)
  1910. {
  1911. if (port->card->state == FST_RUNNING) {
  1912. if (port->run) {
  1913. port->run = 0;
  1914. fst_op_lower(port, OPSTS_RTS | OPSTS_DTR);
  1915. fst_issue_cmd(port, STOPPORT);
  1916. } else {
  1917. dbg(DBG_OPEN, "close: port not running\n");
  1918. }
  1919. }
  1920. }
  1921. static int
  1922. fst_open(struct net_device *dev)
  1923. {
  1924. int err;
  1925. struct fst_port_info *port;
  1926. port = dev_to_port(dev);
  1927. if (!try_module_get(THIS_MODULE))
  1928. return -EBUSY;
  1929. if (port->mode != FST_RAW) {
  1930. err = hdlc_open(dev);
  1931. if (err)
  1932. return err;
  1933. }
  1934. fst_openport(port);
  1935. netif_wake_queue(dev);
  1936. return 0;
  1937. }
  1938. static int
  1939. fst_close(struct net_device *dev)
  1940. {
  1941. struct fst_port_info *port;
  1942. struct fst_card_info *card;
  1943. unsigned char tx_dma_done;
  1944. unsigned char rx_dma_done;
  1945. port = dev_to_port(dev);
  1946. card = port->card;
  1947. tx_dma_done = inb(card->pci_conf + DMACSR1);
  1948. rx_dma_done = inb(card->pci_conf + DMACSR0);
  1949. dbg(DBG_OPEN,
  1950. "Port Close: tx_dma_in_progress = %d (%x) rx_dma_in_progress = %d (%x)\n",
  1951. card->dmatx_in_progress, tx_dma_done, card->dmarx_in_progress,
  1952. rx_dma_done);
  1953. netif_stop_queue(dev);
  1954. fst_closeport(dev_to_port(dev));
  1955. if (port->mode != FST_RAW) {
  1956. hdlc_close(dev);
  1957. }
  1958. module_put(THIS_MODULE);
  1959. return 0;
  1960. }
  1961. static int
  1962. fst_attach(struct net_device *dev, unsigned short encoding, unsigned short parity)
  1963. {
  1964. /*
  1965. * Setting currently fixed in FarSync card so we check and forget
  1966. */
  1967. if (encoding != ENCODING_NRZ || parity != PARITY_CRC16_PR1_CCITT)
  1968. return -EINVAL;
  1969. return 0;
  1970. }
  1971. static void
  1972. fst_tx_timeout(struct net_device *dev)
  1973. {
  1974. struct fst_port_info *port;
  1975. struct fst_card_info *card;
  1976. port = dev_to_port(dev);
  1977. card = port->card;
  1978. dev->stats.tx_errors++;
  1979. dev->stats.tx_aborted_errors++;
  1980. dbg(DBG_ASS, "Tx timeout card %d port %d\n",
  1981. card->card_no, port->index);
  1982. fst_issue_cmd(port, ABORTTX);
  1983. dev->trans_start = jiffies;
  1984. netif_wake_queue(dev);
  1985. port->start = 0;
  1986. }
  1987. static netdev_tx_t
  1988. fst_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1989. {
  1990. struct fst_card_info *card;
  1991. struct fst_port_info *port;
  1992. unsigned long flags;
  1993. int txq_length;
  1994. port = dev_to_port(dev);
  1995. card = port->card;
  1996. dbg(DBG_TX, "fst_start_xmit: length = %d\n", skb->len);
  1997. /* Drop packet with error if we don't have carrier */
  1998. if (!netif_carrier_ok(dev)) {
  1999. dev_kfree_skb(skb);
  2000. dev->stats.tx_errors++;
  2001. dev->stats.tx_carrier_errors++;
  2002. dbg(DBG_ASS,
  2003. "Tried to transmit but no carrier on card %d port %d\n",
  2004. card->card_no, port->index);
  2005. return NETDEV_TX_OK;
  2006. }
  2007. /* Drop it if it's too big! MTU failure ? */
  2008. if (skb->len > LEN_TX_BUFFER) {
  2009. dbg(DBG_ASS, "Packet too large %d vs %d\n", skb->len,
  2010. LEN_TX_BUFFER);
  2011. dev_kfree_skb(skb);
  2012. dev->stats.tx_errors++;
  2013. return NETDEV_TX_OK;
  2014. }
  2015. /*
  2016. * We are always going to queue the packet
  2017. * so that the bottom half is the only place we tx from
  2018. * Check there is room in the port txq
  2019. */
  2020. spin_lock_irqsave(&card->card_lock, flags);
  2021. if ((txq_length = port->txqe - port->txqs) < 0) {
  2022. /*
  2023. * This is the case where the next free has wrapped but the
  2024. * last used hasn't
  2025. */
  2026. txq_length = txq_length + FST_TXQ_DEPTH;
  2027. }
  2028. spin_unlock_irqrestore(&card->card_lock, flags);
  2029. if (txq_length > fst_txq_high) {
  2030. /*
  2031. * We have got enough buffers in the pipeline. Ask the network
  2032. * layer to stop sending frames down
  2033. */
  2034. netif_stop_queue(dev);
  2035. port->start = 1; /* I'm using this to signal stop sent up */
  2036. }
  2037. if (txq_length == FST_TXQ_DEPTH - 1) {
  2038. /*
  2039. * This shouldn't have happened but such is life
  2040. */
  2041. dev_kfree_skb(skb);
  2042. dev->stats.tx_errors++;
  2043. dbg(DBG_ASS, "Tx queue overflow card %d port %d\n",
  2044. card->card_no, port->index);
  2045. return NETDEV_TX_OK;
  2046. }
  2047. /*
  2048. * queue the buffer
  2049. */
  2050. spin_lock_irqsave(&card->card_lock, flags);
  2051. port->txq[port->txqe] = skb;
  2052. port->txqe++;
  2053. if (port->txqe == FST_TXQ_DEPTH)
  2054. port->txqe = 0;
  2055. spin_unlock_irqrestore(&card->card_lock, flags);
  2056. /* Scehdule the bottom half which now does transmit processing */
  2057. fst_q_work_item(&fst_work_txq, card->card_no);
  2058. tasklet_schedule(&fst_tx_task);
  2059. return NETDEV_TX_OK;
  2060. }
  2061. /*
  2062. * Card setup having checked hardware resources.
  2063. * Should be pretty bizarre if we get an error here (kernel memory
  2064. * exhaustion is one possibility). If we do see a problem we report it
  2065. * via a printk and leave the corresponding interface and all that follow
  2066. * disabled.
  2067. */
  2068. static char *type_strings[] __devinitdata = {
  2069. "no hardware", /* Should never be seen */
  2070. "FarSync T2P",
  2071. "FarSync T4P",
  2072. "FarSync T1U",
  2073. "FarSync T2U",
  2074. "FarSync T4U",
  2075. "FarSync TE1"
  2076. };
  2077. static void __devinit
  2078. fst_init_card(struct fst_card_info *card)
  2079. {
  2080. int i;
  2081. int err;
  2082. /* We're working on a number of ports based on the card ID. If the
  2083. * firmware detects something different later (should never happen)
  2084. * we'll have to revise it in some way then.
  2085. */
  2086. for (i = 0; i < card->nports; i++) {
  2087. err = register_hdlc_device(card->ports[i].dev);
  2088. if (err < 0) {
  2089. int j;
  2090. printk_err ("Cannot register HDLC device for port %d"
  2091. " (errno %d)\n", i, -err );
  2092. for (j = i; j < card->nports; j++) {
  2093. free_netdev(card->ports[j].dev);
  2094. card->ports[j].dev = NULL;
  2095. }
  2096. card->nports = i;
  2097. break;
  2098. }
  2099. }
  2100. printk_info("%s-%s: %s IRQ%d, %d ports\n",
  2101. port_to_dev(&card->ports[0])->name,
  2102. port_to_dev(&card->ports[card->nports - 1])->name,
  2103. type_strings[card->type], card->irq, card->nports);
  2104. }
  2105. static const struct net_device_ops fst_ops = {
  2106. .ndo_open = fst_open,
  2107. .ndo_stop = fst_close,
  2108. .ndo_change_mtu = hdlc_change_mtu,
  2109. .ndo_start_xmit = hdlc_start_xmit,
  2110. .ndo_do_ioctl = fst_ioctl,
  2111. .ndo_tx_timeout = fst_tx_timeout,
  2112. };
  2113. /*
  2114. * Initialise card when detected.
  2115. * Returns 0 to indicate success, or errno otherwise.
  2116. */
  2117. static int __devinit
  2118. fst_add_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  2119. {
  2120. static int firsttime_done = 0;
  2121. static int no_of_cards_added = 0;
  2122. struct fst_card_info *card;
  2123. int err = 0;
  2124. int i;
  2125. if (!firsttime_done) {
  2126. printk_info("FarSync WAN driver " FST_USER_VERSION
  2127. " (c) 2001-2004 FarSite Communications Ltd.\n");
  2128. firsttime_done = 1;
  2129. dbg(DBG_ASS, "The value of debug mask is %x\n", fst_debug_mask);
  2130. }
  2131. /*
  2132. * We are going to be clever and allow certain cards not to be
  2133. * configured. An exclude list can be provided in /etc/modules.conf
  2134. */
  2135. if (fst_excluded_cards != 0) {
  2136. /*
  2137. * There are cards to exclude
  2138. *
  2139. */
  2140. for (i = 0; i < fst_excluded_cards; i++) {
  2141. if ((pdev->devfn) >> 3 == fst_excluded_list[i]) {
  2142. printk_info("FarSync PCI device %d not assigned\n",
  2143. (pdev->devfn) >> 3);
  2144. return -EBUSY;
  2145. }
  2146. }
  2147. }
  2148. /* Allocate driver private data */
  2149. card = kzalloc(sizeof (struct fst_card_info), GFP_KERNEL);
  2150. if (card == NULL) {
  2151. printk_err("FarSync card found but insufficient memory for"
  2152. " driver storage\n");
  2153. return -ENOMEM;
  2154. }
  2155. /* Try to enable the device */
  2156. if ((err = pci_enable_device(pdev)) != 0) {
  2157. printk_err("Failed to enable card. Err %d\n", -err);
  2158. kfree(card);
  2159. return err;
  2160. }
  2161. if ((err = pci_request_regions(pdev, "FarSync")) !=0) {
  2162. printk_err("Failed to allocate regions. Err %d\n", -err);
  2163. pci_disable_device(pdev);
  2164. kfree(card);
  2165. return err;
  2166. }
  2167. /* Get virtual addresses of memory regions */
  2168. card->pci_conf = pci_resource_start(pdev, 1);
  2169. card->phys_mem = pci_resource_start(pdev, 2);
  2170. card->phys_ctlmem = pci_resource_start(pdev, 3);
  2171. if ((card->mem = ioremap(card->phys_mem, FST_MEMSIZE)) == NULL) {
  2172. printk_err("Physical memory remap failed\n");
  2173. pci_release_regions(pdev);
  2174. pci_disable_device(pdev);
  2175. kfree(card);
  2176. return -ENODEV;
  2177. }
  2178. if ((card->ctlmem = ioremap(card->phys_ctlmem, 0x10)) == NULL) {
  2179. printk_err("Control memory remap failed\n");
  2180. pci_release_regions(pdev);
  2181. pci_disable_device(pdev);
  2182. kfree(card);
  2183. return -ENODEV;
  2184. }
  2185. dbg(DBG_PCI, "kernel mem %p, ctlmem %p\n", card->mem, card->ctlmem);
  2186. /* Register the interrupt handler */
  2187. if (request_irq(pdev->irq, fst_intr, IRQF_SHARED, FST_DEV_NAME, card)) {
  2188. printk_err("Unable to register interrupt %d\n", card->irq);
  2189. pci_release_regions(pdev);
  2190. pci_disable_device(pdev);
  2191. iounmap(card->ctlmem);
  2192. iounmap(card->mem);
  2193. kfree(card);
  2194. return -ENODEV;
  2195. }
  2196. /* Record info we need */
  2197. card->irq = pdev->irq;
  2198. card->type = ent->driver_data;
  2199. card->family = ((ent->driver_data == FST_TYPE_T2P) ||
  2200. (ent->driver_data == FST_TYPE_T4P))
  2201. ? FST_FAMILY_TXP : FST_FAMILY_TXU;
  2202. if ((ent->driver_data == FST_TYPE_T1U) ||
  2203. (ent->driver_data == FST_TYPE_TE1))
  2204. card->nports = 1;
  2205. else
  2206. card->nports = ((ent->driver_data == FST_TYPE_T2P) ||
  2207. (ent->driver_data == FST_TYPE_T2U)) ? 2 : 4;
  2208. card->state = FST_UNINIT;
  2209. spin_lock_init ( &card->card_lock );
  2210. for ( i = 0 ; i < card->nports ; i++ ) {
  2211. struct net_device *dev = alloc_hdlcdev(&card->ports[i]);
  2212. hdlc_device *hdlc;
  2213. if (!dev) {
  2214. while (i--)
  2215. free_netdev(card->ports[i].dev);
  2216. printk_err ("FarSync: out of memory\n");
  2217. free_irq(card->irq, card);
  2218. pci_release_regions(pdev);
  2219. pci_disable_device(pdev);
  2220. iounmap(card->ctlmem);
  2221. iounmap(card->mem);
  2222. kfree(card);
  2223. return -ENODEV;
  2224. }
  2225. card->ports[i].dev = dev;
  2226. card->ports[i].card = card;
  2227. card->ports[i].index = i;
  2228. card->ports[i].run = 0;
  2229. hdlc = dev_to_hdlc(dev);
  2230. /* Fill in the net device info */
  2231. /* Since this is a PCI setup this is purely
  2232. * informational. Give them the buffer addresses
  2233. * and basic card I/O.
  2234. */
  2235. dev->mem_start = card->phys_mem
  2236. + BUF_OFFSET ( txBuffer[i][0][0]);
  2237. dev->mem_end = card->phys_mem
  2238. + BUF_OFFSET ( txBuffer[i][NUM_TX_BUFFER][0]);
  2239. dev->base_addr = card->pci_conf;
  2240. dev->irq = card->irq;
  2241. dev->netdev_ops = &fst_ops;
  2242. dev->tx_queue_len = FST_TX_QUEUE_LEN;
  2243. dev->watchdog_timeo = FST_TX_TIMEOUT;
  2244. hdlc->attach = fst_attach;
  2245. hdlc->xmit = fst_start_xmit;
  2246. }
  2247. card->device = pdev;
  2248. dbg(DBG_PCI, "type %d nports %d irq %d\n", card->type,
  2249. card->nports, card->irq);
  2250. dbg(DBG_PCI, "conf %04x mem %08x ctlmem %08x\n",
  2251. card->pci_conf, card->phys_mem, card->phys_ctlmem);
  2252. /* Reset the card's processor */
  2253. fst_cpureset(card);
  2254. card->state = FST_RESET;
  2255. /* Initialise DMA (if required) */
  2256. fst_init_dma(card);
  2257. /* Record driver data for later use */
  2258. pci_set_drvdata(pdev, card);
  2259. /* Remainder of card setup */
  2260. fst_card_array[no_of_cards_added] = card;
  2261. card->card_no = no_of_cards_added++; /* Record instance and bump it */
  2262. fst_init_card(card);
  2263. if (card->family == FST_FAMILY_TXU) {
  2264. /*
  2265. * Allocate a dma buffer for transmit and receives
  2266. */
  2267. card->rx_dma_handle_host =
  2268. pci_alloc_consistent(card->device, FST_MAX_MTU,
  2269. &card->rx_dma_handle_card);
  2270. if (card->rx_dma_handle_host == NULL) {
  2271. printk_err("Could not allocate rx dma buffer\n");
  2272. fst_disable_intr(card);
  2273. pci_release_regions(pdev);
  2274. pci_disable_device(pdev);
  2275. iounmap(card->ctlmem);
  2276. iounmap(card->mem);
  2277. kfree(card);
  2278. return -ENOMEM;
  2279. }
  2280. card->tx_dma_handle_host =
  2281. pci_alloc_consistent(card->device, FST_MAX_MTU,
  2282. &card->tx_dma_handle_card);
  2283. if (card->tx_dma_handle_host == NULL) {
  2284. printk_err("Could not allocate tx dma buffer\n");
  2285. fst_disable_intr(card);
  2286. pci_release_regions(pdev);
  2287. pci_disable_device(pdev);
  2288. iounmap(card->ctlmem);
  2289. iounmap(card->mem);
  2290. kfree(card);
  2291. return -ENOMEM;
  2292. }
  2293. }
  2294. return 0; /* Success */
  2295. }
  2296. /*
  2297. * Cleanup and close down a card
  2298. */
  2299. static void __devexit
  2300. fst_remove_one(struct pci_dev *pdev)
  2301. {
  2302. struct fst_card_info *card;
  2303. int i;
  2304. card = pci_get_drvdata(pdev);
  2305. for (i = 0; i < card->nports; i++) {
  2306. struct net_device *dev = port_to_dev(&card->ports[i]);
  2307. unregister_hdlc_device(dev);
  2308. }
  2309. fst_disable_intr(card);
  2310. free_irq(card->irq, card);
  2311. iounmap(card->ctlmem);
  2312. iounmap(card->mem);
  2313. pci_release_regions(pdev);
  2314. if (card->family == FST_FAMILY_TXU) {
  2315. /*
  2316. * Free dma buffers
  2317. */
  2318. pci_free_consistent(card->device, FST_MAX_MTU,
  2319. card->rx_dma_handle_host,
  2320. card->rx_dma_handle_card);
  2321. pci_free_consistent(card->device, FST_MAX_MTU,
  2322. card->tx_dma_handle_host,
  2323. card->tx_dma_handle_card);
  2324. }
  2325. fst_card_array[card->card_no] = NULL;
  2326. }
  2327. static struct pci_driver fst_driver = {
  2328. .name = FST_NAME,
  2329. .id_table = fst_pci_dev_id,
  2330. .probe = fst_add_one,
  2331. .remove = __devexit_p(fst_remove_one),
  2332. .suspend = NULL,
  2333. .resume = NULL,
  2334. };
  2335. static int __init
  2336. fst_init(void)
  2337. {
  2338. int i;
  2339. for (i = 0; i < FST_MAX_CARDS; i++)
  2340. fst_card_array[i] = NULL;
  2341. spin_lock_init(&fst_work_q_lock);
  2342. return pci_register_driver(&fst_driver);
  2343. }
  2344. static void __exit
  2345. fst_cleanup_module(void)
  2346. {
  2347. printk_info("FarSync WAN driver unloading\n");
  2348. pci_unregister_driver(&fst_driver);
  2349. }
  2350. module_init(fst_init);
  2351. module_exit(fst_cleanup_module);