farsync.c 71 KB

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