farsync.c 71 KB

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