farsync.c 71 KB

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