farsync.c 69 KB

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