farsync.c 71 KB

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