farsync.c 72 KB

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