farsync.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713
  1. /*
  2. * FarSync WAN driver for Linux (2.6.x kernel version)
  3. *
  4. * Actually sync driver for X.21, V.35 and V.24 on FarSync T-series cards
  5. *
  6. * Copyright (C) 2001-2004 FarSite Communications Ltd.
  7. * www.farsite.co.uk
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. *
  14. * Author: R.J.Dunlop <bob.dunlop@farsite.co.uk>
  15. * Maintainer: Kevin Curtis <kevin.curtis@farsite.co.uk>
  16. */
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/version.h>
  20. #include <linux/pci.h>
  21. #include <linux/ioport.h>
  22. #include <linux/init.h>
  23. #include <linux/if.h>
  24. #include <linux/hdlc.h>
  25. #include <asm/io.h>
  26. #include <asm/uaccess.h>
  27. #include "farsync.h"
  28. /*
  29. * Module info
  30. */
  31. MODULE_AUTHOR("R.J.Dunlop <bob.dunlop@farsite.co.uk>");
  32. MODULE_DESCRIPTION("FarSync T-Series WAN driver. FarSite Communications Ltd.");
  33. MODULE_LICENSE("GPL");
  34. /* Driver configuration and global parameters
  35. * ==========================================
  36. */
  37. /* Number of ports (per card) and cards supported
  38. */
  39. #define FST_MAX_PORTS 4
  40. #define FST_MAX_CARDS 32
  41. /* Default parameters for the link
  42. */
  43. #define FST_TX_QUEUE_LEN 100 /* At 8Mbps a longer queue length is
  44. * useful, the syncppp module forces
  45. * this down assuming a slower line I
  46. * guess.
  47. */
  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 varaibles
  67. */
  68. int fst_txq_low = FST_LOW_WATER_MARK;
  69. int fst_txq_high = FST_HIGH_WATER_MARK;
  70. int fst_max_reads = 7;
  71. int fst_excluded_cards = 0;
  72. 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,A...) if ( ! ( fst_debug_mask & (F))) \
  438. ; \
  439. else \
  440. printk ( KERN_DEBUG FST_NAME ": " fmt, ## A )
  441. #else
  442. #define dbg(X...) /* NOP */
  443. #endif
  444. /* Printing short cuts
  445. */
  446. #define printk_err(fmt,A...) printk ( KERN_ERR FST_NAME ": " fmt, ## A )
  447. #define printk_warn(fmt,A...) printk ( KERN_WARNING FST_NAME ": " fmt, ## A )
  448. #define printk_info(fmt,A...) printk ( KERN_INFO FST_NAME ": " fmt, ## A )
  449. /*
  450. * PCI ID lookup table
  451. */
  452. static struct pci_device_id fst_pci_dev_id[] __devinitdata = {
  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. DECLARE_TASKLET(fst_tx_task, fst_process_tx_work_q, 0);
  485. DECLARE_TASKLET(fst_int_task, fst_process_int_work_q, 0);
  486. struct fst_card_info *fst_card_array[FST_MAX_CARDS];
  487. spinlock_t fst_work_q_lock;
  488. u64 fst_work_txq;
  489. 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_CN_LOW:
  687. {
  688. dbg(DBG_ASS, "%s: Receive Low Congestion\n", name);
  689. break;
  690. }
  691. case NET_RX_CN_MOD:
  692. {
  693. dbg(DBG_ASS, "%s: Receive Moderate Congestion\n", name);
  694. break;
  695. }
  696. case NET_RX_CN_HIGH:
  697. {
  698. dbg(DBG_ASS, "%s: Receive High Congestion\n", name);
  699. break;
  700. }
  701. case NET_RX_DROP:
  702. {
  703. dbg(DBG_ASS, "%s: Received packet dropped\n", name);
  704. break;
  705. }
  706. }
  707. }
  708. /* Initilaise DMA for PLX 9054
  709. */
  710. static inline void
  711. fst_init_dma(struct fst_card_info *card)
  712. {
  713. /*
  714. * This is only required for the PLX 9054
  715. */
  716. if (card->family == FST_FAMILY_TXU) {
  717. pci_set_master(card->device);
  718. outl(0x00020441, card->pci_conf + DMAMODE0);
  719. outl(0x00020441, card->pci_conf + DMAMODE1);
  720. outl(0x0, card->pci_conf + DMATHR);
  721. }
  722. }
  723. /* Tx dma complete interrupt
  724. */
  725. static void
  726. fst_tx_dma_complete(struct fst_card_info *card, struct fst_port_info *port,
  727. int len, int txpos)
  728. {
  729. struct net_device *dev = port_to_dev(port);
  730. struct net_device_stats *stats = hdlc_stats(dev);
  731. /*
  732. * Everything is now set, just tell the card to go
  733. */
  734. dbg(DBG_TX, "fst_tx_dma_complete\n");
  735. FST_WRB(card, txDescrRing[port->index][txpos].bits,
  736. DMA_OWN | TX_STP | TX_ENP);
  737. stats->tx_packets++;
  738. stats->tx_bytes += len;
  739. dev->trans_start = jiffies;
  740. }
  741. /*
  742. * Mark it for our own raw sockets interface
  743. */
  744. static unsigned short farsync_type_trans(struct sk_buff *skb,
  745. struct net_device *dev)
  746. {
  747. skb->dev = dev;
  748. skb->mac.raw = skb->data;
  749. skb->pkt_type = PACKET_HOST;
  750. return htons(ETH_P_CUST);
  751. }
  752. /* Rx dma complete interrupt
  753. */
  754. static void
  755. fst_rx_dma_complete(struct fst_card_info *card, struct fst_port_info *port,
  756. int len, struct sk_buff *skb, int rxp)
  757. {
  758. struct net_device *dev = port_to_dev(port);
  759. struct net_device_stats *stats = hdlc_stats(dev);
  760. int pi;
  761. int rx_status;
  762. dbg(DBG_TX, "fst_rx_dma_complete\n");
  763. pi = port->index;
  764. memcpy(skb_put(skb, len), card->rx_dma_handle_host, len);
  765. /* Reset buffer descriptor */
  766. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  767. /* Update stats */
  768. stats->rx_packets++;
  769. stats->rx_bytes += len;
  770. /* Push upstream */
  771. dbg(DBG_RX, "Pushing the frame up the stack\n");
  772. if (port->mode == FST_RAW)
  773. skb->protocol = farsync_type_trans(skb, dev);
  774. else
  775. skb->protocol = hdlc_type_trans(skb, dev);
  776. rx_status = netif_rx(skb);
  777. fst_process_rx_status(rx_status, port_to_dev(port)->name);
  778. if (rx_status == NET_RX_DROP)
  779. stats->rx_dropped++;
  780. dev->last_rx = jiffies;
  781. }
  782. /*
  783. * Receive a frame through the DMA
  784. */
  785. static inline void
  786. fst_rx_dma(struct fst_card_info *card, unsigned char *skb,
  787. unsigned char *mem, int len)
  788. {
  789. /*
  790. * This routine will setup the DMA and start it
  791. */
  792. dbg(DBG_RX, "In fst_rx_dma %p %p %d\n", skb, mem, len);
  793. if (card->dmarx_in_progress) {
  794. dbg(DBG_ASS, "In fst_rx_dma while dma in progress\n");
  795. }
  796. outl((unsigned long) skb, card->pci_conf + DMAPADR0); /* Copy to here */
  797. outl((unsigned long) mem, card->pci_conf + DMALADR0); /* from here */
  798. outl(len, card->pci_conf + DMASIZ0); /* for this length */
  799. outl(0x00000000c, card->pci_conf + DMADPR0); /* In this direction */
  800. /*
  801. * We use the dmarx_in_progress flag to flag the channel as busy
  802. */
  803. card->dmarx_in_progress = 1;
  804. outb(0x03, card->pci_conf + DMACSR0); /* Start the transfer */
  805. }
  806. /*
  807. * Send a frame through the DMA
  808. */
  809. static inline void
  810. fst_tx_dma(struct fst_card_info *card, unsigned char *skb,
  811. unsigned char *mem, int len)
  812. {
  813. /*
  814. * This routine will setup the DMA and start it.
  815. */
  816. dbg(DBG_TX, "In fst_tx_dma %p %p %d\n", skb, mem, len);
  817. if (card->dmatx_in_progress) {
  818. dbg(DBG_ASS, "In fst_tx_dma while dma in progress\n");
  819. }
  820. outl((unsigned long) skb, card->pci_conf + DMAPADR1); /* Copy from here */
  821. outl((unsigned long) mem, card->pci_conf + DMALADR1); /* to here */
  822. outl(len, card->pci_conf + DMASIZ1); /* for this length */
  823. outl(0x000000004, card->pci_conf + DMADPR1); /* In this direction */
  824. /*
  825. * We use the dmatx_in_progress to flag the channel as busy
  826. */
  827. card->dmatx_in_progress = 1;
  828. outb(0x03, card->pci_conf + DMACSR1); /* Start the transfer */
  829. }
  830. /* Issue a Mailbox command for a port.
  831. * Note we issue them on a fire and forget basis, not expecting to see an
  832. * error and not waiting for completion.
  833. */
  834. static void
  835. fst_issue_cmd(struct fst_port_info *port, unsigned short cmd)
  836. {
  837. struct fst_card_info *card;
  838. unsigned short mbval;
  839. unsigned long flags;
  840. int safety;
  841. card = port->card;
  842. spin_lock_irqsave(&card->card_lock, flags);
  843. mbval = FST_RDW(card, portMailbox[port->index][0]);
  844. safety = 0;
  845. /* Wait for any previous command to complete */
  846. while (mbval > NAK) {
  847. spin_unlock_irqrestore(&card->card_lock, flags);
  848. set_current_state(TASK_UNINTERRUPTIBLE);
  849. schedule_timeout(1);
  850. spin_lock_irqsave(&card->card_lock, flags);
  851. if (++safety > 2000) {
  852. printk_err("Mailbox safety timeout\n");
  853. break;
  854. }
  855. mbval = FST_RDW(card, portMailbox[port->index][0]);
  856. }
  857. if (safety > 0) {
  858. dbg(DBG_CMD, "Mailbox clear after %d jiffies\n", safety);
  859. }
  860. if (mbval == NAK) {
  861. dbg(DBG_CMD, "issue_cmd: previous command was NAK'd\n");
  862. }
  863. FST_WRW(card, portMailbox[port->index][0], cmd);
  864. if (cmd == ABORTTX || cmd == STARTPORT) {
  865. port->txpos = 0;
  866. port->txipos = 0;
  867. port->start = 0;
  868. }
  869. spin_unlock_irqrestore(&card->card_lock, flags);
  870. }
  871. /* Port output signals control
  872. */
  873. static inline void
  874. fst_op_raise(struct fst_port_info *port, unsigned int outputs)
  875. {
  876. outputs |= FST_RDL(port->card, v24OpSts[port->index]);
  877. FST_WRL(port->card, v24OpSts[port->index], outputs);
  878. if (port->run)
  879. fst_issue_cmd(port, SETV24O);
  880. }
  881. static inline void
  882. fst_op_lower(struct fst_port_info *port, unsigned int outputs)
  883. {
  884. outputs = ~outputs & FST_RDL(port->card, v24OpSts[port->index]);
  885. FST_WRL(port->card, v24OpSts[port->index], outputs);
  886. if (port->run)
  887. fst_issue_cmd(port, SETV24O);
  888. }
  889. /*
  890. * Setup port Rx buffers
  891. */
  892. static void
  893. fst_rx_config(struct fst_port_info *port)
  894. {
  895. int i;
  896. int pi;
  897. unsigned int offset;
  898. unsigned long flags;
  899. struct fst_card_info *card;
  900. pi = port->index;
  901. card = port->card;
  902. spin_lock_irqsave(&card->card_lock, flags);
  903. for (i = 0; i < NUM_RX_BUFFER; i++) {
  904. offset = BUF_OFFSET(rxBuffer[pi][i][0]);
  905. FST_WRW(card, rxDescrRing[pi][i].ladr, (u16) offset);
  906. FST_WRB(card, rxDescrRing[pi][i].hadr, (u8) (offset >> 16));
  907. FST_WRW(card, rxDescrRing[pi][i].bcnt, cnv_bcnt(LEN_RX_BUFFER));
  908. FST_WRW(card, rxDescrRing[pi][i].mcnt, LEN_RX_BUFFER);
  909. FST_WRB(card, rxDescrRing[pi][i].bits, DMA_OWN);
  910. }
  911. port->rxpos = 0;
  912. spin_unlock_irqrestore(&card->card_lock, flags);
  913. }
  914. /*
  915. * Setup port Tx buffers
  916. */
  917. static void
  918. fst_tx_config(struct fst_port_info *port)
  919. {
  920. int i;
  921. int pi;
  922. unsigned int offset;
  923. unsigned long flags;
  924. struct fst_card_info *card;
  925. pi = port->index;
  926. card = port->card;
  927. spin_lock_irqsave(&card->card_lock, flags);
  928. for (i = 0; i < NUM_TX_BUFFER; i++) {
  929. offset = BUF_OFFSET(txBuffer[pi][i][0]);
  930. FST_WRW(card, txDescrRing[pi][i].ladr, (u16) offset);
  931. FST_WRB(card, txDescrRing[pi][i].hadr, (u8) (offset >> 16));
  932. FST_WRW(card, txDescrRing[pi][i].bcnt, 0);
  933. FST_WRB(card, txDescrRing[pi][i].bits, 0);
  934. }
  935. port->txpos = 0;
  936. port->txipos = 0;
  937. port->start = 0;
  938. spin_unlock_irqrestore(&card->card_lock, flags);
  939. }
  940. /* TE1 Alarm change interrupt event
  941. */
  942. static void
  943. fst_intr_te1_alarm(struct fst_card_info *card, struct fst_port_info *port)
  944. {
  945. u8 los;
  946. u8 rra;
  947. u8 ais;
  948. los = FST_RDB(card, suStatus.lossOfSignal);
  949. rra = FST_RDB(card, suStatus.receiveRemoteAlarm);
  950. ais = FST_RDB(card, suStatus.alarmIndicationSignal);
  951. if (los) {
  952. /*
  953. * Lost the link
  954. */
  955. if (netif_carrier_ok(port_to_dev(port))) {
  956. dbg(DBG_INTR, "Net carrier off\n");
  957. netif_carrier_off(port_to_dev(port));
  958. }
  959. } else {
  960. /*
  961. * Link available
  962. */
  963. if (!netif_carrier_ok(port_to_dev(port))) {
  964. dbg(DBG_INTR, "Net carrier on\n");
  965. netif_carrier_on(port_to_dev(port));
  966. }
  967. }
  968. if (los)
  969. dbg(DBG_INTR, "Assert LOS Alarm\n");
  970. else
  971. dbg(DBG_INTR, "De-assert LOS Alarm\n");
  972. if (rra)
  973. dbg(DBG_INTR, "Assert RRA Alarm\n");
  974. else
  975. dbg(DBG_INTR, "De-assert RRA Alarm\n");
  976. if (ais)
  977. dbg(DBG_INTR, "Assert AIS Alarm\n");
  978. else
  979. dbg(DBG_INTR, "De-assert AIS Alarm\n");
  980. }
  981. /* Control signal change interrupt event
  982. */
  983. static void
  984. fst_intr_ctlchg(struct fst_card_info *card, struct fst_port_info *port)
  985. {
  986. int signals;
  987. signals = FST_RDL(card, v24DebouncedSts[port->index]);
  988. if (signals & (((port->hwif == X21) || (port->hwif == X21D))
  989. ? IPSTS_INDICATE : IPSTS_DCD)) {
  990. if (!netif_carrier_ok(port_to_dev(port))) {
  991. dbg(DBG_INTR, "DCD active\n");
  992. netif_carrier_on(port_to_dev(port));
  993. }
  994. } else {
  995. if (netif_carrier_ok(port_to_dev(port))) {
  996. dbg(DBG_INTR, "DCD lost\n");
  997. netif_carrier_off(port_to_dev(port));
  998. }
  999. }
  1000. }
  1001. /* Log Rx Errors
  1002. */
  1003. static void
  1004. fst_log_rx_error(struct fst_card_info *card, struct fst_port_info *port,
  1005. unsigned char dmabits, int rxp, unsigned short len)
  1006. {
  1007. struct net_device *dev = port_to_dev(port);
  1008. struct net_device_stats *stats = hdlc_stats(dev);
  1009. /*
  1010. * Increment the appropriate error counter
  1011. */
  1012. stats->rx_errors++;
  1013. if (dmabits & RX_OFLO) {
  1014. stats->rx_fifo_errors++;
  1015. dbg(DBG_ASS, "Rx fifo error on card %d port %d buffer %d\n",
  1016. card->card_no, port->index, rxp);
  1017. }
  1018. if (dmabits & RX_CRC) {
  1019. stats->rx_crc_errors++;
  1020. dbg(DBG_ASS, "Rx crc error on card %d port %d\n",
  1021. card->card_no, port->index);
  1022. }
  1023. if (dmabits & RX_FRAM) {
  1024. stats->rx_frame_errors++;
  1025. dbg(DBG_ASS, "Rx frame error on card %d port %d\n",
  1026. card->card_no, port->index);
  1027. }
  1028. if (dmabits == (RX_STP | RX_ENP)) {
  1029. stats->rx_length_errors++;
  1030. dbg(DBG_ASS, "Rx length error (%d) on card %d port %d\n",
  1031. len, card->card_no, port->index);
  1032. }
  1033. }
  1034. /* Rx Error Recovery
  1035. */
  1036. static void
  1037. fst_recover_rx_error(struct fst_card_info *card, struct fst_port_info *port,
  1038. unsigned char dmabits, int rxp, unsigned short len)
  1039. {
  1040. int i;
  1041. int pi;
  1042. pi = port->index;
  1043. /*
  1044. * Discard buffer descriptors until we see the start of the
  1045. * next frame. Note that for long frames this could be in
  1046. * a subsequent interrupt.
  1047. */
  1048. i = 0;
  1049. while ((dmabits & (DMA_OWN | RX_STP)) == 0) {
  1050. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1051. rxp = (rxp+1) % NUM_RX_BUFFER;
  1052. if (++i > NUM_RX_BUFFER) {
  1053. dbg(DBG_ASS, "intr_rx: Discarding more bufs"
  1054. " than we have\n");
  1055. break;
  1056. }
  1057. dmabits = FST_RDB(card, rxDescrRing[pi][rxp].bits);
  1058. dbg(DBG_ASS, "DMA Bits of next buffer was %x\n", dmabits);
  1059. }
  1060. dbg(DBG_ASS, "There were %d subsequent buffers in error\n", i);
  1061. /* Discard the terminal buffer */
  1062. if (!(dmabits & DMA_OWN)) {
  1063. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1064. rxp = (rxp+1) % NUM_RX_BUFFER;
  1065. }
  1066. port->rxpos = rxp;
  1067. return;
  1068. }
  1069. /* Rx complete interrupt
  1070. */
  1071. static void
  1072. fst_intr_rx(struct fst_card_info *card, struct fst_port_info *port)
  1073. {
  1074. unsigned char dmabits;
  1075. int pi;
  1076. int rxp;
  1077. int rx_status;
  1078. unsigned short len;
  1079. struct sk_buff *skb;
  1080. struct net_device *dev = port_to_dev(port);
  1081. struct net_device_stats *stats = hdlc_stats(dev);
  1082. /* Check we have a buffer to process */
  1083. pi = port->index;
  1084. rxp = port->rxpos;
  1085. dmabits = FST_RDB(card, rxDescrRing[pi][rxp].bits);
  1086. if (dmabits & DMA_OWN) {
  1087. dbg(DBG_RX | DBG_INTR, "intr_rx: No buffer port %d pos %d\n",
  1088. pi, rxp);
  1089. return;
  1090. }
  1091. if (card->dmarx_in_progress) {
  1092. return;
  1093. }
  1094. /* Get buffer length */
  1095. len = FST_RDW(card, rxDescrRing[pi][rxp].mcnt);
  1096. /* Discard the CRC */
  1097. len -= 2;
  1098. if (len == 0) {
  1099. /*
  1100. * This seems to happen on the TE1 interface sometimes
  1101. * so throw the frame away and log the event.
  1102. */
  1103. printk_err("Frame received with 0 length. Card %d Port %d\n",
  1104. card->card_no, port->index);
  1105. /* Return descriptor to card */
  1106. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1107. rxp = (rxp+1) % NUM_RX_BUFFER;
  1108. port->rxpos = rxp;
  1109. return;
  1110. }
  1111. /* Check buffer length and for other errors. We insist on one packet
  1112. * in one buffer. This simplifies things greatly and since we've
  1113. * allocated 8K it shouldn't be a real world limitation
  1114. */
  1115. dbg(DBG_RX, "intr_rx: %d,%d: flags %x len %d\n", pi, rxp, dmabits, len);
  1116. if (dmabits != (RX_STP | RX_ENP) || len > LEN_RX_BUFFER - 2) {
  1117. fst_log_rx_error(card, port, dmabits, rxp, len);
  1118. fst_recover_rx_error(card, port, dmabits, rxp, len);
  1119. return;
  1120. }
  1121. /* Allocate SKB */
  1122. if ((skb = dev_alloc_skb(len)) == NULL) {
  1123. dbg(DBG_RX, "intr_rx: can't allocate buffer\n");
  1124. stats->rx_dropped++;
  1125. /* Return descriptor to card */
  1126. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1127. rxp = (rxp+1) % NUM_RX_BUFFER;
  1128. port->rxpos = rxp;
  1129. return;
  1130. }
  1131. /*
  1132. * We know the length we need to receive, len.
  1133. * It's not worth using the DMA for reads of less than
  1134. * FST_MIN_DMA_LEN
  1135. */
  1136. if ((len < FST_MIN_DMA_LEN) || (card->family == FST_FAMILY_TXP)) {
  1137. memcpy_fromio(skb_put(skb, len),
  1138. card->mem + BUF_OFFSET(rxBuffer[pi][rxp][0]),
  1139. len);
  1140. /* Reset buffer descriptor */
  1141. FST_WRB(card, rxDescrRing[pi][rxp].bits, DMA_OWN);
  1142. /* Update stats */
  1143. stats->rx_packets++;
  1144. stats->rx_bytes += len;
  1145. /* Push upstream */
  1146. dbg(DBG_RX, "Pushing frame up the stack\n");
  1147. if (port->mode == FST_RAW)
  1148. skb->protocol = farsync_type_trans(skb, dev);
  1149. else
  1150. skb->protocol = hdlc_type_trans(skb, dev);
  1151. rx_status = netif_rx(skb);
  1152. fst_process_rx_status(rx_status, port_to_dev(port)->name);
  1153. if (rx_status == NET_RX_DROP) {
  1154. stats->rx_dropped++;
  1155. }
  1156. dev->last_rx = jiffies;
  1157. } else {
  1158. card->dma_skb_rx = skb;
  1159. card->dma_port_rx = port;
  1160. card->dma_len_rx = len;
  1161. card->dma_rxpos = rxp;
  1162. fst_rx_dma(card, (char *) card->rx_dma_handle_card,
  1163. (char *) BUF_OFFSET(rxBuffer[pi][rxp][0]), len);
  1164. }
  1165. if (rxp != port->rxpos) {
  1166. dbg(DBG_ASS, "About to increment rxpos by more than 1\n");
  1167. dbg(DBG_ASS, "rxp = %d rxpos = %d\n", rxp, port->rxpos);
  1168. }
  1169. rxp = (rxp+1) % NUM_RX_BUFFER;
  1170. port->rxpos = rxp;
  1171. }
  1172. /*
  1173. * The bottom halfs to the ISR
  1174. *
  1175. */
  1176. static void
  1177. do_bottom_half_tx(struct fst_card_info *card)
  1178. {
  1179. struct fst_port_info *port;
  1180. int pi;
  1181. int txq_length;
  1182. struct sk_buff *skb;
  1183. unsigned long flags;
  1184. struct net_device *dev;
  1185. struct net_device_stats *stats;
  1186. /*
  1187. * Find a free buffer for the transmit
  1188. * Step through each port on this card
  1189. */
  1190. dbg(DBG_TX, "do_bottom_half_tx\n");
  1191. for (pi = 0, port = card->ports; pi < card->nports; pi++, port++) {
  1192. if (!port->run)
  1193. continue;
  1194. dev = port_to_dev(port);
  1195. stats = hdlc_stats(dev);
  1196. while (!
  1197. (FST_RDB(card, txDescrRing[pi][port->txpos].bits) &
  1198. DMA_OWN)
  1199. && !(card->dmatx_in_progress)) {
  1200. /*
  1201. * There doesn't seem to be a txdone event per-se
  1202. * We seem to have to deduce it, by checking the DMA_OWN
  1203. * bit on the next buffer we think we can use
  1204. */
  1205. spin_lock_irqsave(&card->card_lock, flags);
  1206. if ((txq_length = port->txqe - port->txqs) < 0) {
  1207. /*
  1208. * This is the case where one has wrapped and the
  1209. * maths gives us a negative number
  1210. */
  1211. txq_length = txq_length + FST_TXQ_DEPTH;
  1212. }
  1213. spin_unlock_irqrestore(&card->card_lock, flags);
  1214. if (txq_length > 0) {
  1215. /*
  1216. * There is something to send
  1217. */
  1218. spin_lock_irqsave(&card->card_lock, flags);
  1219. skb = port->txq[port->txqs];
  1220. port->txqs++;
  1221. if (port->txqs == FST_TXQ_DEPTH) {
  1222. port->txqs = 0;
  1223. }
  1224. spin_unlock_irqrestore(&card->card_lock, flags);
  1225. /*
  1226. * copy the data and set the required indicators on the
  1227. * card.
  1228. */
  1229. FST_WRW(card, txDescrRing[pi][port->txpos].bcnt,
  1230. cnv_bcnt(skb->len));
  1231. if ((skb->len < FST_MIN_DMA_LEN)
  1232. || (card->family == FST_FAMILY_TXP)) {
  1233. /* Enqueue the packet with normal io */
  1234. memcpy_toio(card->mem +
  1235. BUF_OFFSET(txBuffer[pi]
  1236. [port->
  1237. txpos][0]),
  1238. skb->data, skb->len);
  1239. FST_WRB(card,
  1240. txDescrRing[pi][port->txpos].
  1241. bits,
  1242. DMA_OWN | TX_STP | TX_ENP);
  1243. stats->tx_packets++;
  1244. stats->tx_bytes += skb->len;
  1245. dev->trans_start = jiffies;
  1246. } else {
  1247. /* Or do it through dma */
  1248. memcpy(card->tx_dma_handle_host,
  1249. skb->data, skb->len);
  1250. card->dma_port_tx = port;
  1251. card->dma_len_tx = skb->len;
  1252. card->dma_txpos = port->txpos;
  1253. fst_tx_dma(card,
  1254. (char *) card->
  1255. tx_dma_handle_card,
  1256. (char *)
  1257. BUF_OFFSET(txBuffer[pi]
  1258. [port->txpos][0]),
  1259. skb->len);
  1260. }
  1261. if (++port->txpos >= NUM_TX_BUFFER)
  1262. port->txpos = 0;
  1263. /*
  1264. * If we have flow control on, can we now release it?
  1265. */
  1266. if (port->start) {
  1267. if (txq_length < fst_txq_low) {
  1268. netif_wake_queue(port_to_dev
  1269. (port));
  1270. port->start = 0;
  1271. }
  1272. }
  1273. dev_kfree_skb(skb);
  1274. } else {
  1275. /*
  1276. * Nothing to send so break out of the while loop
  1277. */
  1278. break;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. static void
  1284. do_bottom_half_rx(struct fst_card_info *card)
  1285. {
  1286. struct fst_port_info *port;
  1287. int pi;
  1288. int rx_count = 0;
  1289. /* Check for rx completions on all ports on this card */
  1290. dbg(DBG_RX, "do_bottom_half_rx\n");
  1291. for (pi = 0, port = card->ports; pi < card->nports; pi++, port++) {
  1292. if (!port->run)
  1293. continue;
  1294. while (!(FST_RDB(card, rxDescrRing[pi][port->rxpos].bits)
  1295. & DMA_OWN) && !(card->dmarx_in_progress)) {
  1296. if (rx_count > fst_max_reads) {
  1297. /*
  1298. * Don't spend forever in receive processing
  1299. * Schedule another event
  1300. */
  1301. fst_q_work_item(&fst_work_intq, card->card_no);
  1302. tasklet_schedule(&fst_int_task);
  1303. break; /* Leave the loop */
  1304. }
  1305. fst_intr_rx(card, port);
  1306. rx_count++;
  1307. }
  1308. }
  1309. }
  1310. /*
  1311. * The interrupt service routine
  1312. * Dev_id is our fst_card_info pointer
  1313. */
  1314. irqreturn_t
  1315. fst_intr(int irq, void *dev_id, struct pt_regs *regs)
  1316. {
  1317. struct fst_card_info *card;
  1318. struct fst_port_info *port;
  1319. int rdidx; /* Event buffer indices */
  1320. int wridx;
  1321. int event; /* Actual event for processing */
  1322. unsigned int dma_intcsr = 0;
  1323. unsigned int do_card_interrupt;
  1324. unsigned int int_retry_count;
  1325. if ((card = dev_id) == NULL) {
  1326. dbg(DBG_INTR, "intr: spurious %d\n", irq);
  1327. return IRQ_NONE;
  1328. }
  1329. /*
  1330. * Check to see if the interrupt was for this card
  1331. * return if not
  1332. * Note that the call to clear the interrupt is important
  1333. */
  1334. dbg(DBG_INTR, "intr: %d %p\n", irq, card);
  1335. if (card->state != FST_RUNNING) {
  1336. printk_err
  1337. ("Interrupt received for card %d in a non running state (%d)\n",
  1338. card->card_no, card->state);
  1339. /*
  1340. * It is possible to really be running, i.e. we have re-loaded
  1341. * a running card
  1342. * Clear and reprime the interrupt source
  1343. */
  1344. fst_clear_intr(card);
  1345. return IRQ_HANDLED;
  1346. }
  1347. /* Clear and reprime the interrupt source */
  1348. fst_clear_intr(card);
  1349. /*
  1350. * Is the interrupt for this card (handshake == 1)
  1351. */
  1352. do_card_interrupt = 0;
  1353. if (FST_RDB(card, interruptHandshake) == 1) {
  1354. do_card_interrupt += FST_CARD_INT;
  1355. /* Set the software acknowledge */
  1356. FST_WRB(card, interruptHandshake, 0xEE);
  1357. }
  1358. if (card->family == FST_FAMILY_TXU) {
  1359. /*
  1360. * Is it a DMA Interrupt
  1361. */
  1362. dma_intcsr = inl(card->pci_conf + INTCSR_9054);
  1363. if (dma_intcsr & 0x00200000) {
  1364. /*
  1365. * DMA Channel 0 (Rx transfer complete)
  1366. */
  1367. dbg(DBG_RX, "DMA Rx xfer complete\n");
  1368. outb(0x8, card->pci_conf + DMACSR0);
  1369. fst_rx_dma_complete(card, card->dma_port_rx,
  1370. card->dma_len_rx, card->dma_skb_rx,
  1371. card->dma_rxpos);
  1372. card->dmarx_in_progress = 0;
  1373. do_card_interrupt += FST_RX_DMA_INT;
  1374. }
  1375. if (dma_intcsr & 0x00400000) {
  1376. /*
  1377. * DMA Channel 1 (Tx transfer complete)
  1378. */
  1379. dbg(DBG_TX, "DMA Tx xfer complete\n");
  1380. outb(0x8, card->pci_conf + DMACSR1);
  1381. fst_tx_dma_complete(card, card->dma_port_tx,
  1382. card->dma_len_tx, card->dma_txpos);
  1383. card->dmatx_in_progress = 0;
  1384. do_card_interrupt += FST_TX_DMA_INT;
  1385. }
  1386. }
  1387. /*
  1388. * Have we been missing Interrupts
  1389. */
  1390. int_retry_count = FST_RDL(card, interruptRetryCount);
  1391. if (int_retry_count) {
  1392. dbg(DBG_ASS, "Card %d int_retry_count is %d\n",
  1393. card->card_no, int_retry_count);
  1394. FST_WRL(card, interruptRetryCount, 0);
  1395. }
  1396. if (!do_card_interrupt) {
  1397. return IRQ_HANDLED;
  1398. }
  1399. /* Scehdule the bottom half of the ISR */
  1400. fst_q_work_item(&fst_work_intq, card->card_no);
  1401. tasklet_schedule(&fst_int_task);
  1402. /* Drain the event queue */
  1403. rdidx = FST_RDB(card, interruptEvent.rdindex) & 0x1f;
  1404. wridx = FST_RDB(card, interruptEvent.wrindex) & 0x1f;
  1405. while (rdidx != wridx) {
  1406. event = FST_RDB(card, interruptEvent.evntbuff[rdidx]);
  1407. port = &card->ports[event & 0x03];
  1408. dbg(DBG_INTR, "Processing Interrupt event: %x\n", event);
  1409. switch (event) {
  1410. case TE1_ALMA:
  1411. dbg(DBG_INTR, "TE1 Alarm intr\n");
  1412. if (port->run)
  1413. fst_intr_te1_alarm(card, port);
  1414. break;
  1415. case CTLA_CHG:
  1416. case CTLB_CHG:
  1417. case CTLC_CHG:
  1418. case CTLD_CHG:
  1419. if (port->run)
  1420. fst_intr_ctlchg(card, port);
  1421. break;
  1422. case ABTA_SENT:
  1423. case ABTB_SENT:
  1424. case ABTC_SENT:
  1425. case ABTD_SENT:
  1426. dbg(DBG_TX, "Abort complete port %d\n", port->index);
  1427. break;
  1428. case TXA_UNDF:
  1429. case TXB_UNDF:
  1430. case TXC_UNDF:
  1431. case TXD_UNDF:
  1432. /* Difficult to see how we'd get this given that we
  1433. * always load up the entire packet for DMA.
  1434. */
  1435. dbg(DBG_TX, "Tx underflow port %d\n", port->index);
  1436. hdlc_stats(port_to_dev(port))->tx_errors++;
  1437. hdlc_stats(port_to_dev(port))->tx_fifo_errors++;
  1438. dbg(DBG_ASS, "Tx underflow on card %d port %d\n",
  1439. card->card_no, port->index);
  1440. break;
  1441. case INIT_CPLT:
  1442. dbg(DBG_INIT, "Card init OK intr\n");
  1443. break;
  1444. case INIT_FAIL:
  1445. dbg(DBG_INIT, "Card init FAILED intr\n");
  1446. card->state = FST_IFAILED;
  1447. break;
  1448. default:
  1449. printk_err("intr: unknown card event %d. ignored\n",
  1450. event);
  1451. break;
  1452. }
  1453. /* Bump and wrap the index */
  1454. if (++rdidx >= MAX_CIRBUFF)
  1455. rdidx = 0;
  1456. }
  1457. FST_WRB(card, interruptEvent.rdindex, rdidx);
  1458. return IRQ_HANDLED;
  1459. }
  1460. /* Check that the shared memory configuration is one that we can handle
  1461. * and that some basic parameters are correct
  1462. */
  1463. static void
  1464. check_started_ok(struct fst_card_info *card)
  1465. {
  1466. int i;
  1467. /* Check structure version and end marker */
  1468. if (FST_RDW(card, smcVersion) != SMC_VERSION) {
  1469. printk_err("Bad shared memory version %d expected %d\n",
  1470. FST_RDW(card, smcVersion), SMC_VERSION);
  1471. card->state = FST_BADVERSION;
  1472. return;
  1473. }
  1474. if (FST_RDL(card, endOfSmcSignature) != END_SIG) {
  1475. printk_err("Missing shared memory signature\n");
  1476. card->state = FST_BADVERSION;
  1477. return;
  1478. }
  1479. /* Firmware status flag, 0x00 = initialising, 0x01 = OK, 0xFF = fail */
  1480. if ((i = FST_RDB(card, taskStatus)) == 0x01) {
  1481. card->state = FST_RUNNING;
  1482. } else if (i == 0xFF) {
  1483. printk_err("Firmware initialisation failed. Card halted\n");
  1484. card->state = FST_HALTED;
  1485. return;
  1486. } else if (i != 0x00) {
  1487. printk_err("Unknown firmware status 0x%x\n", i);
  1488. card->state = FST_HALTED;
  1489. return;
  1490. }
  1491. /* Finally check the number of ports reported by firmware against the
  1492. * number we assumed at card detection. Should never happen with
  1493. * existing firmware etc so we just report it for the moment.
  1494. */
  1495. if (FST_RDL(card, numberOfPorts) != card->nports) {
  1496. printk_warn("Port count mismatch on card %d."
  1497. " Firmware thinks %d we say %d\n", card->card_no,
  1498. FST_RDL(card, numberOfPorts), card->nports);
  1499. }
  1500. }
  1501. static int
  1502. set_conf_from_info(struct fst_card_info *card, struct fst_port_info *port,
  1503. struct fstioc_info *info)
  1504. {
  1505. int err;
  1506. unsigned char my_framing;
  1507. /* Set things according to the user set valid flags
  1508. * Several of the old options have been invalidated/replaced by the
  1509. * generic hdlc package.
  1510. */
  1511. err = 0;
  1512. if (info->valid & FSTVAL_PROTO) {
  1513. if (info->proto == FST_RAW)
  1514. port->mode = FST_RAW;
  1515. else
  1516. port->mode = FST_GEN_HDLC;
  1517. }
  1518. if (info->valid & FSTVAL_CABLE)
  1519. err = -EINVAL;
  1520. if (info->valid & FSTVAL_SPEED)
  1521. err = -EINVAL;
  1522. if (info->valid & FSTVAL_PHASE)
  1523. FST_WRB(card, portConfig[port->index].invertClock,
  1524. info->invertClock);
  1525. if (info->valid & FSTVAL_MODE)
  1526. FST_WRW(card, cardMode, info->cardMode);
  1527. if (info->valid & FSTVAL_TE1) {
  1528. FST_WRL(card, suConfig.dataRate, info->lineSpeed);
  1529. FST_WRB(card, suConfig.clocking, info->clockSource);
  1530. my_framing = FRAMING_E1;
  1531. if (info->framing == E1)
  1532. my_framing = FRAMING_E1;
  1533. if (info->framing == T1)
  1534. my_framing = FRAMING_T1;
  1535. if (info->framing == J1)
  1536. my_framing = FRAMING_J1;
  1537. FST_WRB(card, suConfig.framing, my_framing);
  1538. FST_WRB(card, suConfig.structure, info->structure);
  1539. FST_WRB(card, suConfig.interface, info->interface);
  1540. FST_WRB(card, suConfig.coding, info->coding);
  1541. FST_WRB(card, suConfig.lineBuildOut, info->lineBuildOut);
  1542. FST_WRB(card, suConfig.equalizer, info->equalizer);
  1543. FST_WRB(card, suConfig.transparentMode, info->transparentMode);
  1544. FST_WRB(card, suConfig.loopMode, info->loopMode);
  1545. FST_WRB(card, suConfig.range, info->range);
  1546. FST_WRB(card, suConfig.txBufferMode, info->txBufferMode);
  1547. FST_WRB(card, suConfig.rxBufferMode, info->rxBufferMode);
  1548. FST_WRB(card, suConfig.startingSlot, info->startingSlot);
  1549. FST_WRB(card, suConfig.losThreshold, info->losThreshold);
  1550. if (info->idleCode)
  1551. FST_WRB(card, suConfig.enableIdleCode, 1);
  1552. else
  1553. FST_WRB(card, suConfig.enableIdleCode, 0);
  1554. FST_WRB(card, suConfig.idleCode, info->idleCode);
  1555. #if FST_DEBUG
  1556. if (info->valid & FSTVAL_TE1) {
  1557. printk("Setting TE1 data\n");
  1558. printk("Line Speed = %d\n", info->lineSpeed);
  1559. printk("Start slot = %d\n", info->startingSlot);
  1560. printk("Clock source = %d\n", info->clockSource);
  1561. printk("Framing = %d\n", my_framing);
  1562. printk("Structure = %d\n", info->structure);
  1563. printk("interface = %d\n", info->interface);
  1564. printk("Coding = %d\n", info->coding);
  1565. printk("Line build out = %d\n", info->lineBuildOut);
  1566. printk("Equaliser = %d\n", info->equalizer);
  1567. printk("Transparent mode = %d\n",
  1568. info->transparentMode);
  1569. printk("Loop mode = %d\n", info->loopMode);
  1570. printk("Range = %d\n", info->range);
  1571. printk("Tx Buffer mode = %d\n", info->txBufferMode);
  1572. printk("Rx Buffer mode = %d\n", info->rxBufferMode);
  1573. printk("LOS Threshold = %d\n", info->losThreshold);
  1574. printk("Idle Code = %d\n", info->idleCode);
  1575. }
  1576. #endif
  1577. }
  1578. #if FST_DEBUG
  1579. if (info->valid & FSTVAL_DEBUG) {
  1580. fst_debug_mask = info->debug;
  1581. }
  1582. #endif
  1583. return err;
  1584. }
  1585. static void
  1586. gather_conf_info(struct fst_card_info *card, struct fst_port_info *port,
  1587. struct fstioc_info *info)
  1588. {
  1589. int i;
  1590. memset(info, 0, sizeof (struct fstioc_info));
  1591. i = port->index;
  1592. info->kernelVersion = LINUX_VERSION_CODE;
  1593. info->nports = card->nports;
  1594. info->type = card->type;
  1595. info->state = card->state;
  1596. info->proto = FST_GEN_HDLC;
  1597. info->index = i;
  1598. #if FST_DEBUG
  1599. info->debug = fst_debug_mask;
  1600. #endif
  1601. /* Only mark information as valid if card is running.
  1602. * Copy the data anyway in case it is useful for diagnostics
  1603. */
  1604. info->valid = ((card->state == FST_RUNNING) ? FSTVAL_ALL : FSTVAL_CARD)
  1605. #if FST_DEBUG
  1606. | FSTVAL_DEBUG
  1607. #endif
  1608. ;
  1609. info->lineInterface = FST_RDW(card, portConfig[i].lineInterface);
  1610. info->internalClock = FST_RDB(card, portConfig[i].internalClock);
  1611. info->lineSpeed = FST_RDL(card, portConfig[i].lineSpeed);
  1612. info->invertClock = FST_RDB(card, portConfig[i].invertClock);
  1613. info->v24IpSts = FST_RDL(card, v24IpSts[i]);
  1614. info->v24OpSts = FST_RDL(card, v24OpSts[i]);
  1615. info->clockStatus = FST_RDW(card, clockStatus[i]);
  1616. info->cableStatus = FST_RDW(card, cableStatus);
  1617. info->cardMode = FST_RDW(card, cardMode);
  1618. info->smcFirmwareVersion = FST_RDL(card, smcFirmwareVersion);
  1619. /*
  1620. * The T2U can report cable presence for both A or B
  1621. * in bits 0 and 1 of cableStatus. See which port we are and
  1622. * do the mapping.
  1623. */
  1624. if (card->family == FST_FAMILY_TXU) {
  1625. if (port->index == 0) {
  1626. /*
  1627. * Port A
  1628. */
  1629. info->cableStatus = info->cableStatus & 1;
  1630. } else {
  1631. /*
  1632. * Port B
  1633. */
  1634. info->cableStatus = info->cableStatus >> 1;
  1635. info->cableStatus = info->cableStatus & 1;
  1636. }
  1637. }
  1638. /*
  1639. * Some additional bits if we are TE1
  1640. */
  1641. if (card->type == FST_TYPE_TE1) {
  1642. info->lineSpeed = FST_RDL(card, suConfig.dataRate);
  1643. info->clockSource = FST_RDB(card, suConfig.clocking);
  1644. info->framing = FST_RDB(card, suConfig.framing);
  1645. info->structure = FST_RDB(card, suConfig.structure);
  1646. info->interface = FST_RDB(card, suConfig.interface);
  1647. info->coding = FST_RDB(card, suConfig.coding);
  1648. info->lineBuildOut = FST_RDB(card, suConfig.lineBuildOut);
  1649. info->equalizer = FST_RDB(card, suConfig.equalizer);
  1650. info->loopMode = FST_RDB(card, suConfig.loopMode);
  1651. info->range = FST_RDB(card, suConfig.range);
  1652. info->txBufferMode = FST_RDB(card, suConfig.txBufferMode);
  1653. info->rxBufferMode = FST_RDB(card, suConfig.rxBufferMode);
  1654. info->startingSlot = FST_RDB(card, suConfig.startingSlot);
  1655. info->losThreshold = FST_RDB(card, suConfig.losThreshold);
  1656. if (FST_RDB(card, suConfig.enableIdleCode))
  1657. info->idleCode = FST_RDB(card, suConfig.idleCode);
  1658. else
  1659. info->idleCode = 0;
  1660. info->receiveBufferDelay =
  1661. FST_RDL(card, suStatus.receiveBufferDelay);
  1662. info->framingErrorCount =
  1663. FST_RDL(card, suStatus.framingErrorCount);
  1664. info->codeViolationCount =
  1665. FST_RDL(card, suStatus.codeViolationCount);
  1666. info->crcErrorCount = FST_RDL(card, suStatus.crcErrorCount);
  1667. info->lineAttenuation = FST_RDL(card, suStatus.lineAttenuation);
  1668. info->lossOfSignal = FST_RDB(card, suStatus.lossOfSignal);
  1669. info->receiveRemoteAlarm =
  1670. FST_RDB(card, suStatus.receiveRemoteAlarm);
  1671. info->alarmIndicationSignal =
  1672. FST_RDB(card, suStatus.alarmIndicationSignal);
  1673. }
  1674. }
  1675. static int
  1676. fst_set_iface(struct fst_card_info *card, struct fst_port_info *port,
  1677. struct ifreq *ifr)
  1678. {
  1679. sync_serial_settings sync;
  1680. int i;
  1681. if (ifr->ifr_settings.size != sizeof (sync)) {
  1682. return -ENOMEM;
  1683. }
  1684. if (copy_from_user
  1685. (&sync, ifr->ifr_settings.ifs_ifsu.sync, sizeof (sync))) {
  1686. return -EFAULT;
  1687. }
  1688. if (sync.loopback)
  1689. return -EINVAL;
  1690. i = port->index;
  1691. switch (ifr->ifr_settings.type) {
  1692. case IF_IFACE_V35:
  1693. FST_WRW(card, portConfig[i].lineInterface, V35);
  1694. port->hwif = V35;
  1695. break;
  1696. case IF_IFACE_V24:
  1697. FST_WRW(card, portConfig[i].lineInterface, V24);
  1698. port->hwif = V24;
  1699. break;
  1700. case IF_IFACE_X21:
  1701. FST_WRW(card, portConfig[i].lineInterface, X21);
  1702. port->hwif = X21;
  1703. break;
  1704. case IF_IFACE_X21D:
  1705. FST_WRW(card, portConfig[i].lineInterface, X21D);
  1706. port->hwif = X21D;
  1707. break;
  1708. case IF_IFACE_T1:
  1709. FST_WRW(card, portConfig[i].lineInterface, T1);
  1710. port->hwif = T1;
  1711. break;
  1712. case IF_IFACE_E1:
  1713. FST_WRW(card, portConfig[i].lineInterface, E1);
  1714. port->hwif = E1;
  1715. break;
  1716. case IF_IFACE_SYNC_SERIAL:
  1717. break;
  1718. default:
  1719. return -EINVAL;
  1720. }
  1721. switch (sync.clock_type) {
  1722. case CLOCK_EXT:
  1723. FST_WRB(card, portConfig[i].internalClock, EXTCLK);
  1724. break;
  1725. case CLOCK_INT:
  1726. FST_WRB(card, portConfig[i].internalClock, INTCLK);
  1727. break;
  1728. default:
  1729. return -EINVAL;
  1730. }
  1731. FST_WRL(card, portConfig[i].lineSpeed, sync.clock_rate);
  1732. return 0;
  1733. }
  1734. static int
  1735. fst_get_iface(struct fst_card_info *card, struct fst_port_info *port,
  1736. struct ifreq *ifr)
  1737. {
  1738. sync_serial_settings sync;
  1739. int i;
  1740. /* First check what line type is set, we'll default to reporting X.21
  1741. * if nothing is set as IF_IFACE_SYNC_SERIAL implies it can't be
  1742. * changed
  1743. */
  1744. switch (port->hwif) {
  1745. case E1:
  1746. ifr->ifr_settings.type = IF_IFACE_E1;
  1747. break;
  1748. case T1:
  1749. ifr->ifr_settings.type = IF_IFACE_T1;
  1750. break;
  1751. case V35:
  1752. ifr->ifr_settings.type = IF_IFACE_V35;
  1753. break;
  1754. case V24:
  1755. ifr->ifr_settings.type = IF_IFACE_V24;
  1756. break;
  1757. case X21D:
  1758. ifr->ifr_settings.type = IF_IFACE_X21D;
  1759. break;
  1760. case X21:
  1761. default:
  1762. ifr->ifr_settings.type = IF_IFACE_X21;
  1763. break;
  1764. }
  1765. if (ifr->ifr_settings.size == 0) {
  1766. return 0; /* only type requested */
  1767. }
  1768. if (ifr->ifr_settings.size < sizeof (sync)) {
  1769. return -ENOMEM;
  1770. }
  1771. i = port->index;
  1772. sync.clock_rate = FST_RDL(card, portConfig[i].lineSpeed);
  1773. /* Lucky card and linux use same encoding here */
  1774. sync.clock_type = FST_RDB(card, portConfig[i].internalClock) ==
  1775. INTCLK ? CLOCK_INT : CLOCK_EXT;
  1776. sync.loopback = 0;
  1777. if (copy_to_user(ifr->ifr_settings.ifs_ifsu.sync, &sync, sizeof (sync))) {
  1778. return -EFAULT;
  1779. }
  1780. ifr->ifr_settings.size = sizeof (sync);
  1781. return 0;
  1782. }
  1783. static int
  1784. fst_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1785. {
  1786. struct fst_card_info *card;
  1787. struct fst_port_info *port;
  1788. struct fstioc_write wrthdr;
  1789. struct fstioc_info info;
  1790. unsigned long flags;
  1791. dbg(DBG_IOCTL, "ioctl: %x, %p\n", cmd, ifr->ifr_data);
  1792. port = dev_to_port(dev);
  1793. card = port->card;
  1794. if (!capable(CAP_NET_ADMIN))
  1795. return -EPERM;
  1796. switch (cmd) {
  1797. case FSTCPURESET:
  1798. fst_cpureset(card);
  1799. card->state = FST_RESET;
  1800. return 0;
  1801. case FSTCPURELEASE:
  1802. fst_cpurelease(card);
  1803. card->state = FST_STARTING;
  1804. return 0;
  1805. case FSTWRITE: /* Code write (download) */
  1806. /* First copy in the header with the length and offset of data
  1807. * to write
  1808. */
  1809. if (ifr->ifr_data == NULL) {
  1810. return -EINVAL;
  1811. }
  1812. if (copy_from_user(&wrthdr, ifr->ifr_data,
  1813. sizeof (struct fstioc_write))) {
  1814. return -EFAULT;
  1815. }
  1816. /* Sanity check the parameters. We don't support partial writes
  1817. * when going over the top
  1818. */
  1819. if (wrthdr.size > FST_MEMSIZE || wrthdr.offset > FST_MEMSIZE
  1820. || wrthdr.size + wrthdr.offset > FST_MEMSIZE) {
  1821. return -ENXIO;
  1822. }
  1823. /* Now copy the data to the card.
  1824. * This will probably break on some architectures.
  1825. * I'll fix it when I have something to test on.
  1826. */
  1827. if (copy_from_user(card->mem + wrthdr.offset,
  1828. ifr->ifr_data + sizeof (struct fstioc_write),
  1829. wrthdr.size)) {
  1830. return -EFAULT;
  1831. }
  1832. /* Writes to the memory of a card in the reset state constitute
  1833. * a download
  1834. */
  1835. if (card->state == FST_RESET) {
  1836. card->state = FST_DOWNLOAD;
  1837. }
  1838. return 0;
  1839. case FSTGETCONF:
  1840. /* If card has just been started check the shared memory config
  1841. * version and marker
  1842. */
  1843. if (card->state == FST_STARTING) {
  1844. check_started_ok(card);
  1845. /* If everything checked out enable card interrupts */
  1846. if (card->state == FST_RUNNING) {
  1847. spin_lock_irqsave(&card->card_lock, flags);
  1848. fst_enable_intr(card);
  1849. FST_WRB(card, interruptHandshake, 0xEE);
  1850. spin_unlock_irqrestore(&card->card_lock, flags);
  1851. }
  1852. }
  1853. if (ifr->ifr_data == NULL) {
  1854. return -EINVAL;
  1855. }
  1856. gather_conf_info(card, port, &info);
  1857. if (copy_to_user(ifr->ifr_data, &info, sizeof (info))) {
  1858. return -EFAULT;
  1859. }
  1860. return 0;
  1861. case FSTSETCONF:
  1862. /*
  1863. * Most of the settings have been moved to the generic ioctls
  1864. * this just covers debug and board ident now
  1865. */
  1866. if (card->state != FST_RUNNING) {
  1867. printk_err
  1868. ("Attempt to configure card %d in non-running state (%d)\n",
  1869. card->card_no, card->state);
  1870. return -EIO;
  1871. }
  1872. if (copy_from_user(&info, ifr->ifr_data, sizeof (info))) {
  1873. return -EFAULT;
  1874. }
  1875. return set_conf_from_info(card, port, &info);
  1876. case SIOCWANDEV:
  1877. switch (ifr->ifr_settings.type) {
  1878. case IF_GET_IFACE:
  1879. return fst_get_iface(card, port, ifr);
  1880. case IF_IFACE_SYNC_SERIAL:
  1881. case IF_IFACE_V35:
  1882. case IF_IFACE_V24:
  1883. case IF_IFACE_X21:
  1884. case IF_IFACE_X21D:
  1885. case IF_IFACE_T1:
  1886. case IF_IFACE_E1:
  1887. return fst_set_iface(card, port, ifr);
  1888. case IF_PROTO_RAW:
  1889. port->mode = FST_RAW;
  1890. return 0;
  1891. case IF_GET_PROTO:
  1892. if (port->mode == FST_RAW) {
  1893. ifr->ifr_settings.type = IF_PROTO_RAW;
  1894. return 0;
  1895. }
  1896. return hdlc_ioctl(dev, ifr, cmd);
  1897. default:
  1898. port->mode = FST_GEN_HDLC;
  1899. dbg(DBG_IOCTL, "Passing this type to hdlc %x\n",
  1900. ifr->ifr_settings.type);
  1901. return hdlc_ioctl(dev, ifr, cmd);
  1902. }
  1903. default:
  1904. /* Not one of ours. Pass through to HDLC package */
  1905. return hdlc_ioctl(dev, ifr, cmd);
  1906. }
  1907. }
  1908. static void
  1909. fst_openport(struct fst_port_info *port)
  1910. {
  1911. int signals;
  1912. int txq_length;
  1913. /* Only init things if card is actually running. This allows open to
  1914. * succeed for downloads etc.
  1915. */
  1916. if (port->card->state == FST_RUNNING) {
  1917. if (port->run) {
  1918. dbg(DBG_OPEN, "open: found port already running\n");
  1919. fst_issue_cmd(port, STOPPORT);
  1920. port->run = 0;
  1921. }
  1922. fst_rx_config(port);
  1923. fst_tx_config(port);
  1924. fst_op_raise(port, OPSTS_RTS | OPSTS_DTR);
  1925. fst_issue_cmd(port, STARTPORT);
  1926. port->run = 1;
  1927. signals = FST_RDL(port->card, v24DebouncedSts[port->index]);
  1928. if (signals & (((port->hwif == X21) || (port->hwif == X21D))
  1929. ? IPSTS_INDICATE : IPSTS_DCD))
  1930. netif_carrier_on(port_to_dev(port));
  1931. else
  1932. netif_carrier_off(port_to_dev(port));
  1933. txq_length = port->txqe - port->txqs;
  1934. port->txqe = 0;
  1935. port->txqs = 0;
  1936. }
  1937. }
  1938. static void
  1939. fst_closeport(struct fst_port_info *port)
  1940. {
  1941. if (port->card->state == FST_RUNNING) {
  1942. if (port->run) {
  1943. port->run = 0;
  1944. fst_op_lower(port, OPSTS_RTS | OPSTS_DTR);
  1945. fst_issue_cmd(port, STOPPORT);
  1946. } else {
  1947. dbg(DBG_OPEN, "close: port not running\n");
  1948. }
  1949. }
  1950. }
  1951. static int
  1952. fst_open(struct net_device *dev)
  1953. {
  1954. int err;
  1955. struct fst_port_info *port;
  1956. port = dev_to_port(dev);
  1957. if (!try_module_get(THIS_MODULE))
  1958. return -EBUSY;
  1959. if (port->mode != FST_RAW) {
  1960. err = hdlc_open(dev);
  1961. if (err)
  1962. return err;
  1963. }
  1964. fst_openport(port);
  1965. netif_wake_queue(dev);
  1966. return 0;
  1967. }
  1968. static int
  1969. fst_close(struct net_device *dev)
  1970. {
  1971. struct fst_port_info *port;
  1972. struct fst_card_info *card;
  1973. unsigned char tx_dma_done;
  1974. unsigned char rx_dma_done;
  1975. port = dev_to_port(dev);
  1976. card = port->card;
  1977. tx_dma_done = inb(card->pci_conf + DMACSR1);
  1978. rx_dma_done = inb(card->pci_conf + DMACSR0);
  1979. dbg(DBG_OPEN,
  1980. "Port Close: tx_dma_in_progress = %d (%x) rx_dma_in_progress = %d (%x)\n",
  1981. card->dmatx_in_progress, tx_dma_done, card->dmarx_in_progress,
  1982. rx_dma_done);
  1983. netif_stop_queue(dev);
  1984. fst_closeport(dev_to_port(dev));
  1985. if (port->mode != FST_RAW) {
  1986. hdlc_close(dev);
  1987. }
  1988. module_put(THIS_MODULE);
  1989. return 0;
  1990. }
  1991. static int
  1992. fst_attach(struct net_device *dev, unsigned short encoding, unsigned short parity)
  1993. {
  1994. /*
  1995. * Setting currently fixed in FarSync card so we check and forget
  1996. */
  1997. if (encoding != ENCODING_NRZ || parity != PARITY_CRC16_PR1_CCITT)
  1998. return -EINVAL;
  1999. return 0;
  2000. }
  2001. static void
  2002. fst_tx_timeout(struct net_device *dev)
  2003. {
  2004. struct fst_port_info *port;
  2005. struct fst_card_info *card;
  2006. struct net_device_stats *stats = hdlc_stats(dev);
  2007. port = dev_to_port(dev);
  2008. card = port->card;
  2009. stats->tx_errors++;
  2010. stats->tx_aborted_errors++;
  2011. dbg(DBG_ASS, "Tx timeout card %d port %d\n",
  2012. card->card_no, port->index);
  2013. fst_issue_cmd(port, ABORTTX);
  2014. dev->trans_start = jiffies;
  2015. netif_wake_queue(dev);
  2016. port->start = 0;
  2017. }
  2018. static int
  2019. fst_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2020. {
  2021. struct fst_card_info *card;
  2022. struct fst_port_info *port;
  2023. struct net_device_stats *stats = hdlc_stats(dev);
  2024. unsigned long flags;
  2025. int txq_length;
  2026. port = dev_to_port(dev);
  2027. card = port->card;
  2028. dbg(DBG_TX, "fst_start_xmit: length = %d\n", skb->len);
  2029. /* Drop packet with error if we don't have carrier */
  2030. if (!netif_carrier_ok(dev)) {
  2031. dev_kfree_skb(skb);
  2032. stats->tx_errors++;
  2033. stats->tx_carrier_errors++;
  2034. dbg(DBG_ASS,
  2035. "Tried to transmit but no carrier on card %d port %d\n",
  2036. card->card_no, port->index);
  2037. return 0;
  2038. }
  2039. /* Drop it if it's too big! MTU failure ? */
  2040. if (skb->len > LEN_TX_BUFFER) {
  2041. dbg(DBG_ASS, "Packet too large %d vs %d\n", skb->len,
  2042. LEN_TX_BUFFER);
  2043. dev_kfree_skb(skb);
  2044. stats->tx_errors++;
  2045. return 0;
  2046. }
  2047. /*
  2048. * We are always going to queue the packet
  2049. * so that the bottom half is the only place we tx from
  2050. * Check there is room in the port txq
  2051. */
  2052. spin_lock_irqsave(&card->card_lock, flags);
  2053. if ((txq_length = port->txqe - port->txqs) < 0) {
  2054. /*
  2055. * This is the case where the next free has wrapped but the
  2056. * last used hasn't
  2057. */
  2058. txq_length = txq_length + FST_TXQ_DEPTH;
  2059. }
  2060. spin_unlock_irqrestore(&card->card_lock, flags);
  2061. if (txq_length > fst_txq_high) {
  2062. /*
  2063. * We have got enough buffers in the pipeline. Ask the network
  2064. * layer to stop sending frames down
  2065. */
  2066. netif_stop_queue(dev);
  2067. port->start = 1; /* I'm using this to signal stop sent up */
  2068. }
  2069. if (txq_length == FST_TXQ_DEPTH - 1) {
  2070. /*
  2071. * This shouldn't have happened but such is life
  2072. */
  2073. dev_kfree_skb(skb);
  2074. stats->tx_errors++;
  2075. dbg(DBG_ASS, "Tx queue overflow card %d port %d\n",
  2076. card->card_no, port->index);
  2077. return 0;
  2078. }
  2079. /*
  2080. * queue the buffer
  2081. */
  2082. spin_lock_irqsave(&card->card_lock, flags);
  2083. port->txq[port->txqe] = skb;
  2084. port->txqe++;
  2085. if (port->txqe == FST_TXQ_DEPTH)
  2086. port->txqe = 0;
  2087. spin_unlock_irqrestore(&card->card_lock, flags);
  2088. /* Scehdule the bottom half which now does transmit processing */
  2089. fst_q_work_item(&fst_work_txq, card->card_no);
  2090. tasklet_schedule(&fst_tx_task);
  2091. return 0;
  2092. }
  2093. /*
  2094. * Card setup having checked hardware resources.
  2095. * Should be pretty bizarre if we get an error here (kernel memory
  2096. * exhaustion is one possibility). If we do see a problem we report it
  2097. * via a printk and leave the corresponding interface and all that follow
  2098. * disabled.
  2099. */
  2100. static char *type_strings[] __devinitdata = {
  2101. "no hardware", /* Should never be seen */
  2102. "FarSync T2P",
  2103. "FarSync T4P",
  2104. "FarSync T1U",
  2105. "FarSync T2U",
  2106. "FarSync T4U",
  2107. "FarSync TE1"
  2108. };
  2109. static void __devinit
  2110. fst_init_card(struct fst_card_info *card)
  2111. {
  2112. int i;
  2113. int err;
  2114. /* We're working on a number of ports based on the card ID. If the
  2115. * firmware detects something different later (should never happen)
  2116. * we'll have to revise it in some way then.
  2117. */
  2118. for (i = 0; i < card->nports; i++) {
  2119. err = register_hdlc_device(card->ports[i].dev);
  2120. if (err < 0) {
  2121. int j;
  2122. printk_err ("Cannot register HDLC device for port %d"
  2123. " (errno %d)\n", i, -err );
  2124. for (j = i; j < card->nports; j++) {
  2125. free_netdev(card->ports[j].dev);
  2126. card->ports[j].dev = NULL;
  2127. }
  2128. card->nports = i;
  2129. break;
  2130. }
  2131. }
  2132. printk_info("%s-%s: %s IRQ%d, %d ports\n",
  2133. port_to_dev(&card->ports[0])->name,
  2134. port_to_dev(&card->ports[card->nports - 1])->name,
  2135. type_strings[card->type], card->irq, card->nports);
  2136. }
  2137. /*
  2138. * Initialise card when detected.
  2139. * Returns 0 to indicate success, or errno otherwise.
  2140. */
  2141. static int __devinit
  2142. fst_add_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  2143. {
  2144. static int firsttime_done = 0;
  2145. static int no_of_cards_added = 0;
  2146. struct fst_card_info *card;
  2147. int err = 0;
  2148. int i;
  2149. if (!firsttime_done) {
  2150. printk_info("FarSync WAN driver " FST_USER_VERSION
  2151. " (c) 2001-2004 FarSite Communications Ltd.\n");
  2152. firsttime_done = 1;
  2153. dbg(DBG_ASS, "The value of debug mask is %x\n", fst_debug_mask);
  2154. }
  2155. /*
  2156. * We are going to be clever and allow certain cards not to be
  2157. * configured. An exclude list can be provided in /etc/modules.conf
  2158. */
  2159. if (fst_excluded_cards != 0) {
  2160. /*
  2161. * There are cards to exclude
  2162. *
  2163. */
  2164. for (i = 0; i < fst_excluded_cards; i++) {
  2165. if ((pdev->devfn) >> 3 == fst_excluded_list[i]) {
  2166. printk_info("FarSync PCI device %d not assigned\n",
  2167. (pdev->devfn) >> 3);
  2168. return -EBUSY;
  2169. }
  2170. }
  2171. }
  2172. /* Allocate driver private data */
  2173. card = kmalloc(sizeof (struct fst_card_info), GFP_KERNEL);
  2174. if (card == NULL) {
  2175. printk_err("FarSync card found but insufficient memory for"
  2176. " driver storage\n");
  2177. return -ENOMEM;
  2178. }
  2179. memset(card, 0, sizeof (struct fst_card_info));
  2180. /* Try to enable the device */
  2181. if ((err = pci_enable_device(pdev)) != 0) {
  2182. printk_err("Failed to enable card. Err %d\n", -err);
  2183. kfree(card);
  2184. return err;
  2185. }
  2186. if ((err = pci_request_regions(pdev, "FarSync")) !=0) {
  2187. printk_err("Failed to allocate regions. Err %d\n", -err);
  2188. pci_disable_device(pdev);
  2189. kfree(card);
  2190. return err;
  2191. }
  2192. /* Get virtual addresses of memory regions */
  2193. card->pci_conf = pci_resource_start(pdev, 1);
  2194. card->phys_mem = pci_resource_start(pdev, 2);
  2195. card->phys_ctlmem = pci_resource_start(pdev, 3);
  2196. if ((card->mem = ioremap(card->phys_mem, FST_MEMSIZE)) == NULL) {
  2197. printk_err("Physical memory remap failed\n");
  2198. pci_release_regions(pdev);
  2199. pci_disable_device(pdev);
  2200. kfree(card);
  2201. return -ENODEV;
  2202. }
  2203. if ((card->ctlmem = ioremap(card->phys_ctlmem, 0x10)) == NULL) {
  2204. printk_err("Control memory remap failed\n");
  2205. pci_release_regions(pdev);
  2206. pci_disable_device(pdev);
  2207. kfree(card);
  2208. return -ENODEV;
  2209. }
  2210. dbg(DBG_PCI, "kernel mem %p, ctlmem %p\n", card->mem, card->ctlmem);
  2211. /* Register the interrupt handler */
  2212. if (request_irq(pdev->irq, fst_intr, SA_SHIRQ, FST_DEV_NAME, card)) {
  2213. printk_err("Unable to register interrupt %d\n", card->irq);
  2214. pci_release_regions(pdev);
  2215. pci_disable_device(pdev);
  2216. iounmap(card->ctlmem);
  2217. iounmap(card->mem);
  2218. kfree(card);
  2219. return -ENODEV;
  2220. }
  2221. /* Record info we need */
  2222. card->irq = pdev->irq;
  2223. card->type = ent->driver_data;
  2224. card->family = ((ent->driver_data == FST_TYPE_T2P) ||
  2225. (ent->driver_data == FST_TYPE_T4P))
  2226. ? FST_FAMILY_TXP : FST_FAMILY_TXU;
  2227. if ((ent->driver_data == FST_TYPE_T1U) ||
  2228. (ent->driver_data == FST_TYPE_TE1))
  2229. card->nports = 1;
  2230. else
  2231. card->nports = ((ent->driver_data == FST_TYPE_T2P) ||
  2232. (ent->driver_data == FST_TYPE_T2U)) ? 2 : 4;
  2233. card->state = FST_UNINIT;
  2234. spin_lock_init ( &card->card_lock );
  2235. for ( i = 0 ; i < card->nports ; i++ ) {
  2236. struct net_device *dev = alloc_hdlcdev(&card->ports[i]);
  2237. hdlc_device *hdlc;
  2238. if (!dev) {
  2239. while (i--)
  2240. free_netdev(card->ports[i].dev);
  2241. printk_err ("FarSync: out of memory\n");
  2242. free_irq(card->irq, card);
  2243. pci_release_regions(pdev);
  2244. pci_disable_device(pdev);
  2245. iounmap(card->ctlmem);
  2246. iounmap(card->mem);
  2247. kfree(card);
  2248. return -ENODEV;
  2249. }
  2250. card->ports[i].dev = dev;
  2251. card->ports[i].card = card;
  2252. card->ports[i].index = i;
  2253. card->ports[i].run = 0;
  2254. hdlc = dev_to_hdlc(dev);
  2255. /* Fill in the net device info */
  2256. /* Since this is a PCI setup this is purely
  2257. * informational. Give them the buffer addresses
  2258. * and basic card I/O.
  2259. */
  2260. dev->mem_start = card->phys_mem
  2261. + BUF_OFFSET ( txBuffer[i][0][0]);
  2262. dev->mem_end = card->phys_mem
  2263. + BUF_OFFSET ( txBuffer[i][NUM_TX_BUFFER][0]);
  2264. dev->base_addr = card->pci_conf;
  2265. dev->irq = card->irq;
  2266. dev->tx_queue_len = FST_TX_QUEUE_LEN;
  2267. dev->open = fst_open;
  2268. dev->stop = fst_close;
  2269. dev->do_ioctl = fst_ioctl;
  2270. dev->watchdog_timeo = FST_TX_TIMEOUT;
  2271. dev->tx_timeout = fst_tx_timeout;
  2272. hdlc->attach = fst_attach;
  2273. hdlc->xmit = fst_start_xmit;
  2274. }
  2275. card->device = pdev;
  2276. dbg(DBG_PCI, "type %d nports %d irq %d\n", card->type,
  2277. card->nports, card->irq);
  2278. dbg(DBG_PCI, "conf %04x mem %08x ctlmem %08x\n",
  2279. card->pci_conf, card->phys_mem, card->phys_ctlmem);
  2280. /* Reset the card's processor */
  2281. fst_cpureset(card);
  2282. card->state = FST_RESET;
  2283. /* Initialise DMA (if required) */
  2284. fst_init_dma(card);
  2285. /* Record driver data for later use */
  2286. pci_set_drvdata(pdev, card);
  2287. /* Remainder of card setup */
  2288. fst_card_array[no_of_cards_added] = card;
  2289. card->card_no = no_of_cards_added++; /* Record instance and bump it */
  2290. fst_init_card(card);
  2291. if (card->family == FST_FAMILY_TXU) {
  2292. /*
  2293. * Allocate a dma buffer for transmit and receives
  2294. */
  2295. card->rx_dma_handle_host =
  2296. pci_alloc_consistent(card->device, FST_MAX_MTU,
  2297. &card->rx_dma_handle_card);
  2298. if (card->rx_dma_handle_host == NULL) {
  2299. printk_err("Could not allocate rx dma buffer\n");
  2300. fst_disable_intr(card);
  2301. pci_release_regions(pdev);
  2302. pci_disable_device(pdev);
  2303. iounmap(card->ctlmem);
  2304. iounmap(card->mem);
  2305. kfree(card);
  2306. return -ENOMEM;
  2307. }
  2308. card->tx_dma_handle_host =
  2309. pci_alloc_consistent(card->device, FST_MAX_MTU,
  2310. &card->tx_dma_handle_card);
  2311. if (card->tx_dma_handle_host == NULL) {
  2312. printk_err("Could not allocate tx dma buffer\n");
  2313. fst_disable_intr(card);
  2314. pci_release_regions(pdev);
  2315. pci_disable_device(pdev);
  2316. iounmap(card->ctlmem);
  2317. iounmap(card->mem);
  2318. kfree(card);
  2319. return -ENOMEM;
  2320. }
  2321. }
  2322. return 0; /* Success */
  2323. }
  2324. /*
  2325. * Cleanup and close down a card
  2326. */
  2327. static void __devexit
  2328. fst_remove_one(struct pci_dev *pdev)
  2329. {
  2330. struct fst_card_info *card;
  2331. int i;
  2332. card = pci_get_drvdata(pdev);
  2333. for (i = 0; i < card->nports; i++) {
  2334. struct net_device *dev = port_to_dev(&card->ports[i]);
  2335. unregister_hdlc_device(dev);
  2336. }
  2337. fst_disable_intr(card);
  2338. free_irq(card->irq, card);
  2339. iounmap(card->ctlmem);
  2340. iounmap(card->mem);
  2341. pci_release_regions(pdev);
  2342. if (card->family == FST_FAMILY_TXU) {
  2343. /*
  2344. * Free dma buffers
  2345. */
  2346. pci_free_consistent(card->device, FST_MAX_MTU,
  2347. card->rx_dma_handle_host,
  2348. card->rx_dma_handle_card);
  2349. pci_free_consistent(card->device, FST_MAX_MTU,
  2350. card->tx_dma_handle_host,
  2351. card->tx_dma_handle_card);
  2352. }
  2353. fst_card_array[card->card_no] = NULL;
  2354. }
  2355. static struct pci_driver fst_driver = {
  2356. .name = FST_NAME,
  2357. .id_table = fst_pci_dev_id,
  2358. .probe = fst_add_one,
  2359. .remove = __devexit_p(fst_remove_one),
  2360. .suspend = NULL,
  2361. .resume = NULL,
  2362. };
  2363. static int __init
  2364. fst_init(void)
  2365. {
  2366. int i;
  2367. for (i = 0; i < FST_MAX_CARDS; i++)
  2368. fst_card_array[i] = NULL;
  2369. spin_lock_init(&fst_work_q_lock);
  2370. return pci_module_init(&fst_driver);
  2371. }
  2372. static void __exit
  2373. fst_cleanup_module(void)
  2374. {
  2375. printk_info("FarSync WAN driver unloading\n");
  2376. pci_unregister_driver(&fst_driver);
  2377. }
  2378. module_init(fst_init);
  2379. module_exit(fst_cleanup_module);