farsync.c 71 KB

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