farsync.c 70 KB

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