farsync.c 71 KB

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