farsync.c 70 KB

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