horizon.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  1. /*
  2. Madge Horizon ATM Adapter driver.
  3. Copyright (C) 1995-1999 Madge Networks Ltd.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. The GNU GPL is contained in /usr/doc/copyright/GPL on a Debian
  16. system and in the file COPYING in the Linux kernel source.
  17. */
  18. /*
  19. IMPORTANT NOTE: Madge Networks no longer makes the adapters
  20. supported by this driver and makes no commitment to maintain it.
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/mm.h>
  25. #include <linux/pci.h>
  26. #include <linux/errno.h>
  27. #include <linux/atm.h>
  28. #include <linux/atmdev.h>
  29. #include <linux/sonet.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/time.h>
  32. #include <linux/delay.h>
  33. #include <linux/uio.h>
  34. #include <linux/init.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/ioport.h>
  37. #include <linux/wait.h>
  38. #include <linux/slab.h>
  39. #include <asm/io.h>
  40. #include <linux/atomic.h>
  41. #include <asm/uaccess.h>
  42. #include <asm/string.h>
  43. #include <asm/byteorder.h>
  44. #include "horizon.h"
  45. #define maintainer_string "Giuliano Procida at Madge Networks <gprocida@madge.com>"
  46. #define description_string "Madge ATM Horizon [Ultra] driver"
  47. #define version_string "1.2.1"
  48. static inline void __init show_version (void) {
  49. printk ("%s version %s\n", description_string, version_string);
  50. }
  51. /*
  52. CREDITS
  53. Driver and documentation by:
  54. Chris Aston Madge Networks
  55. Giuliano Procida Madge Networks
  56. Simon Benham Madge Networks
  57. Simon Johnson Madge Networks
  58. Various Others Madge Networks
  59. Some inspiration taken from other drivers by:
  60. Alexandru Cucos UTBv
  61. Kari Mettinen University of Helsinki
  62. Werner Almesberger EPFL LRC
  63. Theory of Operation
  64. I Hardware, detection, initialisation and shutdown.
  65. 1. Supported Hardware
  66. This driver should handle all variants of the PCI Madge ATM adapters
  67. with the Horizon chipset. These are all PCI cards supporting PIO, BM
  68. DMA and a form of MMIO (registers only, not internal RAM).
  69. The driver is only known to work with SONET and UTP Horizon Ultra
  70. cards at 155Mb/s. However, code is in place to deal with both the
  71. original Horizon and 25Mb/s operation.
  72. There are two revisions of the Horizon ASIC: the original and the
  73. Ultra. Details of hardware bugs are in section III.
  74. The ASIC version can be distinguished by chip markings but is NOT
  75. indicated by the PCI revision (all adapters seem to have PCI rev 1).
  76. I believe that:
  77. Horizon => Collage 25 PCI Adapter (UTP and STP)
  78. Horizon Ultra => Collage 155 PCI Client (UTP or SONET)
  79. Ambassador x => Collage 155 PCI Server (completely different)
  80. Horizon (25Mb/s) is fitted with UTP and STP connectors. It seems to
  81. have a Madge B154 plus glue logic serializer. I have also found a
  82. really ancient version of this with slightly different glue. It
  83. comes with the revision 0 (140-025-01) ASIC.
  84. Horizon Ultra (155Mb/s) is fitted with either a Pulse Medialink
  85. output (UTP) or an HP HFBR 5205 output (SONET). It has either
  86. Madge's SAMBA framer or a SUNI-lite device (early versions). It
  87. comes with the revision 1 (140-027-01) ASIC.
  88. 2. Detection
  89. All Horizon-based cards present with the same PCI Vendor and Device
  90. IDs. The standard Linux 2.2 PCI API is used to locate any cards and
  91. to enable bus-mastering (with appropriate latency).
  92. ATM_LAYER_STATUS in the control register distinguishes between the
  93. two possible physical layers (25 and 155). It is not clear whether
  94. the 155 cards can also operate at 25Mbps. We rely on the fact that a
  95. card operates at 155 if and only if it has the newer Horizon Ultra
  96. ASIC.
  97. For 155 cards the two possible framers are probed for and then set
  98. up for loop-timing.
  99. 3. Initialisation
  100. The card is reset and then put into a known state. The physical
  101. layer is configured for normal operation at the appropriate speed;
  102. in the case of the 155 cards, the framer is initialised with
  103. line-based timing; the internal RAM is zeroed and the allocation of
  104. buffers for RX and TX is made; the Burnt In Address is read and
  105. copied to the ATM ESI; various policy settings for RX (VPI bits,
  106. unknown VCs, oam cells) are made. Ideally all policy items should be
  107. configurable at module load (if not actually on-demand), however,
  108. only the vpi vs vci bit allocation can be specified at insmod.
  109. 4. Shutdown
  110. This is in response to module_cleaup. No VCs are in use and the card
  111. should be idle; it is reset.
  112. II Driver software (as it should be)
  113. 0. Traffic Parameters
  114. The traffic classes (not an enumeration) are currently: ATM_NONE (no
  115. traffic), ATM_UBR, ATM_CBR, ATM_VBR and ATM_ABR, ATM_ANYCLASS
  116. (compatible with everything). Together with (perhaps only some of)
  117. the following items they make up the traffic specification.
  118. struct atm_trafprm {
  119. unsigned char traffic_class; traffic class (ATM_UBR, ...)
  120. int max_pcr; maximum PCR in cells per second
  121. int pcr; desired PCR in cells per second
  122. int min_pcr; minimum PCR in cells per second
  123. int max_cdv; maximum CDV in microseconds
  124. int max_sdu; maximum SDU in bytes
  125. };
  126. Note that these denote bandwidth available not bandwidth used; the
  127. possibilities according to ATMF are:
  128. Real Time (cdv and max CDT given)
  129. CBR(pcr) pcr bandwidth always available
  130. rtVBR(pcr,scr,mbs) scr bandwidth always available, up to pcr at mbs too
  131. Non Real Time
  132. nrtVBR(pcr,scr,mbs) scr bandwidth always available, up to pcr at mbs too
  133. UBR()
  134. ABR(mcr,pcr) mcr bandwidth always available, up to pcr (depending) too
  135. mbs is max burst size (bucket)
  136. pcr and scr have associated cdvt values
  137. mcr is like scr but has no cdtv
  138. cdtv may differ at each hop
  139. Some of the above items are qos items (as opposed to traffic
  140. parameters). We have nothing to do with qos. All except ABR can have
  141. their traffic parameters converted to GCRA parameters. The GCRA may
  142. be implemented as a (real-number) leaky bucket. The GCRA can be used
  143. in complicated ways by switches and in simpler ways by end-stations.
  144. It can be used both to filter incoming cells and shape out-going
  145. cells.
  146. ATM Linux actually supports:
  147. ATM_NONE() (no traffic in this direction)
  148. ATM_UBR(max_frame_size)
  149. ATM_CBR(max/min_pcr, max_cdv, max_frame_size)
  150. 0 or ATM_MAX_PCR are used to indicate maximum available PCR
  151. A traffic specification consists of the AAL type and separate
  152. traffic specifications for either direction. In ATM Linux it is:
  153. struct atm_qos {
  154. struct atm_trafprm txtp;
  155. struct atm_trafprm rxtp;
  156. unsigned char aal;
  157. };
  158. AAL types are:
  159. ATM_NO_AAL AAL not specified
  160. ATM_AAL0 "raw" ATM cells
  161. ATM_AAL1 AAL1 (CBR)
  162. ATM_AAL2 AAL2 (VBR)
  163. ATM_AAL34 AAL3/4 (data)
  164. ATM_AAL5 AAL5 (data)
  165. ATM_SAAL signaling AAL
  166. The Horizon has support for AAL frame types: 0, 3/4 and 5. However,
  167. it does not implement AAL 3/4 SAR and it has a different notion of
  168. "raw cell" to ATM Linux's (48 bytes vs. 52 bytes) so neither are
  169. supported by this driver.
  170. The Horizon has limited support for ABR (including UBR), VBR and
  171. CBR. Each TX channel has a bucket (containing up to 31 cell units)
  172. and two timers (PCR and SCR) associated with it that can be used to
  173. govern cell emissions and host notification (in the case of ABR this
  174. is presumably so that RM cells may be emitted at appropriate times).
  175. The timers may either be disabled or may be set to any of 240 values
  176. (determined by the clock crystal, a fixed (?) per-device divider, a
  177. configurable divider and a configurable timer preload value).
  178. At the moment only UBR and CBR are supported by the driver. VBR will
  179. be supported as soon as ATM for Linux supports it. ABR support is
  180. very unlikely as RM cell handling is completely up to the driver.
  181. 1. TX (TX channel setup and TX transfer)
  182. The TX half of the driver owns the TX Horizon registers. The TX
  183. component in the IRQ handler is the BM completion handler. This can
  184. only be entered when tx_busy is true (enforced by hardware). The
  185. other TX component can only be entered when tx_busy is false
  186. (enforced by driver). So TX is single-threaded.
  187. Apart from a minor optimisation to not re-select the last channel,
  188. the TX send component works as follows:
  189. Atomic test and set tx_busy until we succeed; we should implement
  190. some sort of timeout so that tx_busy will never be stuck at true.
  191. If no TX channel is set up for this VC we wait for an idle one (if
  192. necessary) and set it up.
  193. At this point we have a TX channel ready for use. We wait for enough
  194. buffers to become available then start a TX transmit (set the TX
  195. descriptor, schedule transfer, exit).
  196. The IRQ component handles TX completion (stats, free buffer, tx_busy
  197. unset, exit). We also re-schedule further transfers for the same
  198. frame if needed.
  199. TX setup in more detail:
  200. TX open is a nop, the relevant information is held in the hrz_vcc
  201. (vcc->dev_data) structure and is "cached" on the card.
  202. TX close gets the TX lock and clears the channel from the "cache".
  203. 2. RX (Data Available and RX transfer)
  204. The RX half of the driver owns the RX registers. There are two RX
  205. components in the IRQ handler: the data available handler deals with
  206. fresh data that has arrived on the card, the BM completion handler
  207. is very similar to the TX completion handler. The data available
  208. handler grabs the rx_lock and it is only released once the data has
  209. been discarded or completely transferred to the host. The BM
  210. completion handler only runs when the lock is held; the data
  211. available handler is locked out over the same period.
  212. Data available on the card triggers an interrupt. If the data is not
  213. suitable for our existing RX channels or we cannot allocate a buffer
  214. it is flushed. Otherwise an RX receive is scheduled. Multiple RX
  215. transfers may be scheduled for the same frame.
  216. RX setup in more detail:
  217. RX open...
  218. RX close...
  219. III Hardware Bugs
  220. 0. Byte vs Word addressing of adapter RAM.
  221. A design feature; see the .h file (especially the memory map).
  222. 1. Bus Master Data Transfers (original Horizon only, fixed in Ultra)
  223. The host must not start a transmit direction transfer at a
  224. non-four-byte boundary in host memory. Instead the host should
  225. perform a byte, or a two byte, or one byte followed by two byte
  226. transfer in order to start the rest of the transfer on a four byte
  227. boundary. RX is OK.
  228. Simultaneous transmit and receive direction bus master transfers are
  229. not allowed.
  230. The simplest solution to these two is to always do PIO (never DMA)
  231. in the TX direction on the original Horizon. More complicated
  232. solutions are likely to hurt my brain.
  233. 2. Loss of buffer on close VC
  234. When a VC is being closed, the buffer associated with it is not
  235. returned to the pool. The host must store the reference to this
  236. buffer and when opening a new VC then give it to that new VC.
  237. The host intervention currently consists of stacking such a buffer
  238. pointer at VC close and checking the stack at VC open.
  239. 3. Failure to close a VC
  240. If a VC is currently receiving a frame then closing the VC may fail
  241. and the frame continues to be received.
  242. The solution is to make sure any received frames are flushed when
  243. ready. This is currently done just before the solution to 2.
  244. 4. PCI bus (original Horizon only, fixed in Ultra)
  245. Reading from the data port prior to initialisation will hang the PCI
  246. bus. Just don't do that then! We don't.
  247. IV To Do List
  248. . Timer code may be broken.
  249. . Allow users to specify buffer allocation split for TX and RX.
  250. . Deal once and for all with buggy VC close.
  251. . Handle interrupted and/or non-blocking operations.
  252. . Change some macros to functions and move from .h to .c.
  253. . Try to limit the number of TX frames each VC may have queued, in
  254. order to reduce the chances of TX buffer exhaustion.
  255. . Implement VBR (bucket and timers not understood) and ABR (need to
  256. do RM cells manually); also no Linux support for either.
  257. . Implement QoS changes on open VCs (involves extracting parts of VC open
  258. and close into separate functions and using them to make changes).
  259. */
  260. /********** globals **********/
  261. static void do_housekeeping (unsigned long arg);
  262. static unsigned short debug = 0;
  263. static unsigned short vpi_bits = 0;
  264. static int max_tx_size = 9000;
  265. static int max_rx_size = 9000;
  266. static unsigned char pci_lat = 0;
  267. /********** access functions **********/
  268. /* Read / Write Horizon registers */
  269. static inline void wr_regl (const hrz_dev * dev, unsigned char reg, u32 data) {
  270. outl (cpu_to_le32 (data), dev->iobase + reg);
  271. }
  272. static inline u32 rd_regl (const hrz_dev * dev, unsigned char reg) {
  273. return le32_to_cpu (inl (dev->iobase + reg));
  274. }
  275. static inline void wr_regw (const hrz_dev * dev, unsigned char reg, u16 data) {
  276. outw (cpu_to_le16 (data), dev->iobase + reg);
  277. }
  278. static inline u16 rd_regw (const hrz_dev * dev, unsigned char reg) {
  279. return le16_to_cpu (inw (dev->iobase + reg));
  280. }
  281. static inline void wrs_regb (const hrz_dev * dev, unsigned char reg, void * addr, u32 len) {
  282. outsb (dev->iobase + reg, addr, len);
  283. }
  284. static inline void rds_regb (const hrz_dev * dev, unsigned char reg, void * addr, u32 len) {
  285. insb (dev->iobase + reg, addr, len);
  286. }
  287. /* Read / Write to a given address in Horizon buffer memory.
  288. Interrupts must be disabled between the address register and data
  289. port accesses as these must form an atomic operation. */
  290. static inline void wr_mem (const hrz_dev * dev, HDW * addr, u32 data) {
  291. // wr_regl (dev, MEM_WR_ADDR_REG_OFF, (u32) addr);
  292. wr_regl (dev, MEM_WR_ADDR_REG_OFF, (addr - (HDW *) 0) * sizeof(HDW));
  293. wr_regl (dev, MEMORY_PORT_OFF, data);
  294. }
  295. static inline u32 rd_mem (const hrz_dev * dev, HDW * addr) {
  296. // wr_regl (dev, MEM_RD_ADDR_REG_OFF, (u32) addr);
  297. wr_regl (dev, MEM_RD_ADDR_REG_OFF, (addr - (HDW *) 0) * sizeof(HDW));
  298. return rd_regl (dev, MEMORY_PORT_OFF);
  299. }
  300. static inline void wr_framer (const hrz_dev * dev, u32 addr, u32 data) {
  301. wr_regl (dev, MEM_WR_ADDR_REG_OFF, (u32) addr | 0x80000000);
  302. wr_regl (dev, MEMORY_PORT_OFF, data);
  303. }
  304. static inline u32 rd_framer (const hrz_dev * dev, u32 addr) {
  305. wr_regl (dev, MEM_RD_ADDR_REG_OFF, (u32) addr | 0x80000000);
  306. return rd_regl (dev, MEMORY_PORT_OFF);
  307. }
  308. /********** specialised access functions **********/
  309. /* RX */
  310. static inline void FLUSH_RX_CHANNEL (hrz_dev * dev, u16 channel) {
  311. wr_regw (dev, RX_CHANNEL_PORT_OFF, FLUSH_CHANNEL | channel);
  312. return;
  313. }
  314. static void WAIT_FLUSH_RX_COMPLETE (hrz_dev * dev) {
  315. while (rd_regw (dev, RX_CHANNEL_PORT_OFF) & FLUSH_CHANNEL)
  316. ;
  317. return;
  318. }
  319. static inline void SELECT_RX_CHANNEL (hrz_dev * dev, u16 channel) {
  320. wr_regw (dev, RX_CHANNEL_PORT_OFF, channel);
  321. return;
  322. }
  323. static void WAIT_UPDATE_COMPLETE (hrz_dev * dev) {
  324. while (rd_regw (dev, RX_CHANNEL_PORT_OFF) & RX_CHANNEL_UPDATE_IN_PROGRESS)
  325. ;
  326. return;
  327. }
  328. /* TX */
  329. static inline void SELECT_TX_CHANNEL (hrz_dev * dev, u16 tx_channel) {
  330. wr_regl (dev, TX_CHANNEL_PORT_OFF, tx_channel);
  331. return;
  332. }
  333. /* Update or query one configuration parameter of a particular channel. */
  334. static inline void update_tx_channel_config (hrz_dev * dev, short chan, u8 mode, u16 value) {
  335. wr_regw (dev, TX_CHANNEL_CONFIG_COMMAND_OFF,
  336. chan * TX_CHANNEL_CONFIG_MULT | mode);
  337. wr_regw (dev, TX_CHANNEL_CONFIG_DATA_OFF, value);
  338. return;
  339. }
  340. /********** dump functions **********/
  341. static inline void dump_skb (char * prefix, unsigned int vc, struct sk_buff * skb) {
  342. #ifdef DEBUG_HORIZON
  343. unsigned int i;
  344. unsigned char * data = skb->data;
  345. PRINTDB (DBG_DATA, "%s(%u) ", prefix, vc);
  346. for (i=0; i<skb->len && i < 256;i++)
  347. PRINTDM (DBG_DATA, "%02x ", data[i]);
  348. PRINTDE (DBG_DATA,"");
  349. #else
  350. (void) prefix;
  351. (void) vc;
  352. (void) skb;
  353. #endif
  354. return;
  355. }
  356. static inline void dump_regs (hrz_dev * dev) {
  357. #ifdef DEBUG_HORIZON
  358. PRINTD (DBG_REGS, "CONTROL 0: %#x", rd_regl (dev, CONTROL_0_REG));
  359. PRINTD (DBG_REGS, "RX CONFIG: %#x", rd_regw (dev, RX_CONFIG_OFF));
  360. PRINTD (DBG_REGS, "TX CONFIG: %#x", rd_regw (dev, TX_CONFIG_OFF));
  361. PRINTD (DBG_REGS, "TX STATUS: %#x", rd_regw (dev, TX_STATUS_OFF));
  362. PRINTD (DBG_REGS, "IRQ ENBLE: %#x", rd_regl (dev, INT_ENABLE_REG_OFF));
  363. PRINTD (DBG_REGS, "IRQ SORCE: %#x", rd_regl (dev, INT_SOURCE_REG_OFF));
  364. #else
  365. (void) dev;
  366. #endif
  367. return;
  368. }
  369. static inline void dump_framer (hrz_dev * dev) {
  370. #ifdef DEBUG_HORIZON
  371. unsigned int i;
  372. PRINTDB (DBG_REGS, "framer registers:");
  373. for (i = 0; i < 0x10; ++i)
  374. PRINTDM (DBG_REGS, " %02x", rd_framer (dev, i));
  375. PRINTDE (DBG_REGS,"");
  376. #else
  377. (void) dev;
  378. #endif
  379. return;
  380. }
  381. /********** VPI/VCI <-> (RX) channel conversions **********/
  382. /* RX channels are 10 bit integers, these fns are quite paranoid */
  383. static inline int vpivci_to_channel (u16 * channel, const short vpi, const int vci) {
  384. unsigned short vci_bits = 10 - vpi_bits;
  385. if (0 <= vpi && vpi < 1<<vpi_bits && 0 <= vci && vci < 1<<vci_bits) {
  386. *channel = vpi<<vci_bits | vci;
  387. return *channel ? 0 : -EINVAL;
  388. }
  389. return -EINVAL;
  390. }
  391. /********** decode RX queue entries **********/
  392. static inline u16 rx_q_entry_to_length (u32 x) {
  393. return x & RX_Q_ENTRY_LENGTH_MASK;
  394. }
  395. static inline u16 rx_q_entry_to_rx_channel (u32 x) {
  396. return (x>>RX_Q_ENTRY_CHANNEL_SHIFT) & RX_CHANNEL_MASK;
  397. }
  398. /* Cell Transmit Rate Values
  399. *
  400. * the cell transmit rate (cells per sec) can be set to a variety of
  401. * different values by specifying two parameters: a timer preload from
  402. * 1 to 16 (stored as 0 to 15) and a clock divider (2 to the power of
  403. * an exponent from 0 to 14; the special value 15 disables the timer).
  404. *
  405. * cellrate = baserate / (preload * 2^divider)
  406. *
  407. * The maximum cell rate that can be specified is therefore just the
  408. * base rate. Halving the preload is equivalent to adding 1 to the
  409. * divider and so values 1 to 8 of the preload are redundant except
  410. * in the case of a maximal divider (14).
  411. *
  412. * Given a desired cell rate, an algorithm to determine the preload
  413. * and divider is:
  414. *
  415. * a) x = baserate / cellrate, want p * 2^d = x (as far as possible)
  416. * b) if x > 16 * 2^14 then set p = 16, d = 14 (min rate), done
  417. * if x <= 16 then set p = x, d = 0 (high rates), done
  418. * c) now have 16 < x <= 2^18, or 1 < x/16 <= 2^14 and we want to
  419. * know n such that 2^(n-1) < x/16 <= 2^n, so slide a bit until
  420. * we find the range (n will be between 1 and 14), set d = n
  421. * d) Also have 8 < x/2^n <= 16, so set p nearest x/2^n
  422. *
  423. * The algorithm used below is a minor variant of the above.
  424. *
  425. * The base rate is derived from the oscillator frequency (Hz) using a
  426. * fixed divider:
  427. *
  428. * baserate = freq / 32 in the case of some Unknown Card
  429. * baserate = freq / 8 in the case of the Horizon 25
  430. * baserate = freq / 8 in the case of the Horizon Ultra 155
  431. *
  432. * The Horizon cards have oscillators and base rates as follows:
  433. *
  434. * Card Oscillator Base Rate
  435. * Unknown Card 33 MHz 1.03125 MHz (33 MHz = PCI freq)
  436. * Horizon 25 32 MHz 4 MHz
  437. * Horizon Ultra 155 40 MHz 5 MHz
  438. *
  439. * The following defines give the base rates in Hz. These were
  440. * previously a factor of 100 larger, no doubt someone was using
  441. * cps*100.
  442. */
  443. #define BR_UKN 1031250l
  444. #define BR_HRZ 4000000l
  445. #define BR_ULT 5000000l
  446. // d is an exponent
  447. #define CR_MIND 0
  448. #define CR_MAXD 14
  449. // p ranges from 1 to a power of 2
  450. #define CR_MAXPEXP 4
  451. static int make_rate (const hrz_dev * dev, u32 c, rounding r,
  452. u16 * bits, unsigned int * actual)
  453. {
  454. // note: rounding the rate down means rounding 'p' up
  455. const unsigned long br = test_bit(ultra, &dev->flags) ? BR_ULT : BR_HRZ;
  456. u32 div = CR_MIND;
  457. u32 pre;
  458. // br_exp and br_man are used to avoid overflowing (c*maxp*2^d) in
  459. // the tests below. We could think harder about exact possibilities
  460. // of failure...
  461. unsigned long br_man = br;
  462. unsigned int br_exp = 0;
  463. PRINTD (DBG_QOS|DBG_FLOW, "make_rate b=%lu, c=%u, %s", br, c,
  464. r == round_up ? "up" : r == round_down ? "down" : "nearest");
  465. // avoid div by zero
  466. if (!c) {
  467. PRINTD (DBG_QOS|DBG_ERR, "zero rate is not allowed!");
  468. return -EINVAL;
  469. }
  470. while (br_exp < CR_MAXPEXP + CR_MIND && (br_man % 2 == 0)) {
  471. br_man = br_man >> 1;
  472. ++br_exp;
  473. }
  474. // (br >>br_exp) <<br_exp == br and
  475. // br_exp <= CR_MAXPEXP+CR_MIND
  476. if (br_man <= (c << (CR_MAXPEXP+CR_MIND-br_exp))) {
  477. // Equivalent to: B <= (c << (MAXPEXP+MIND))
  478. // take care of rounding
  479. switch (r) {
  480. case round_down:
  481. pre = DIV_ROUND_UP(br, c<<div);
  482. // but p must be non-zero
  483. if (!pre)
  484. pre = 1;
  485. break;
  486. case round_nearest:
  487. pre = DIV_ROUND_CLOSEST(br, c<<div);
  488. // but p must be non-zero
  489. if (!pre)
  490. pre = 1;
  491. break;
  492. default: /* round_up */
  493. pre = br/(c<<div);
  494. // but p must be non-zero
  495. if (!pre)
  496. return -EINVAL;
  497. }
  498. PRINTD (DBG_QOS, "A: p=%u, d=%u", pre, div);
  499. goto got_it;
  500. }
  501. // at this point we have
  502. // d == MIND and (c << (MAXPEXP+MIND)) < B
  503. while (div < CR_MAXD) {
  504. div++;
  505. if (br_man <= (c << (CR_MAXPEXP+div-br_exp))) {
  506. // Equivalent to: B <= (c << (MAXPEXP+d))
  507. // c << (MAXPEXP+d-1) < B <= c << (MAXPEXP+d)
  508. // 1 << (MAXPEXP-1) < B/2^d/c <= 1 << MAXPEXP
  509. // MAXP/2 < B/c2^d <= MAXP
  510. // take care of rounding
  511. switch (r) {
  512. case round_down:
  513. pre = DIV_ROUND_UP(br, c<<div);
  514. break;
  515. case round_nearest:
  516. pre = DIV_ROUND_CLOSEST(br, c<<div);
  517. break;
  518. default: /* round_up */
  519. pre = br/(c<<div);
  520. }
  521. PRINTD (DBG_QOS, "B: p=%u, d=%u", pre, div);
  522. goto got_it;
  523. }
  524. }
  525. // at this point we have
  526. // d == MAXD and (c << (MAXPEXP+MAXD)) < B
  527. // but we cannot go any higher
  528. // take care of rounding
  529. if (r == round_down)
  530. return -EINVAL;
  531. pre = 1 << CR_MAXPEXP;
  532. PRINTD (DBG_QOS, "C: p=%u, d=%u", pre, div);
  533. got_it:
  534. // paranoia
  535. if (div > CR_MAXD || (!pre) || pre > 1<<CR_MAXPEXP) {
  536. PRINTD (DBG_QOS, "set_cr internal failure: d=%u p=%u",
  537. div, pre);
  538. return -EINVAL;
  539. } else {
  540. if (bits)
  541. *bits = (div<<CLOCK_SELECT_SHIFT) | (pre-1);
  542. if (actual) {
  543. *actual = DIV_ROUND_UP(br, pre<<div);
  544. PRINTD (DBG_QOS, "actual rate: %u", *actual);
  545. }
  546. return 0;
  547. }
  548. }
  549. static int make_rate_with_tolerance (const hrz_dev * dev, u32 c, rounding r, unsigned int tol,
  550. u16 * bit_pattern, unsigned int * actual) {
  551. unsigned int my_actual;
  552. PRINTD (DBG_QOS|DBG_FLOW, "make_rate_with_tolerance c=%u, %s, tol=%u",
  553. c, (r == round_up) ? "up" : (r == round_down) ? "down" : "nearest", tol);
  554. if (!actual)
  555. // actual rate is not returned
  556. actual = &my_actual;
  557. if (make_rate (dev, c, round_nearest, bit_pattern, actual))
  558. // should never happen as round_nearest always succeeds
  559. return -1;
  560. if (c - tol <= *actual && *actual <= c + tol)
  561. // within tolerance
  562. return 0;
  563. else
  564. // intolerant, try rounding instead
  565. return make_rate (dev, c, r, bit_pattern, actual);
  566. }
  567. /********** Listen on a VC **********/
  568. static int hrz_open_rx (hrz_dev * dev, u16 channel) {
  569. // is there any guarantee that we don't get two simulataneous
  570. // identical calls of this function from different processes? yes
  571. // rate_lock
  572. unsigned long flags;
  573. u32 channel_type; // u16?
  574. u16 buf_ptr = RX_CHANNEL_IDLE;
  575. rx_ch_desc * rx_desc = &memmap->rx_descs[channel];
  576. PRINTD (DBG_FLOW, "hrz_open_rx %x", channel);
  577. spin_lock_irqsave (&dev->mem_lock, flags);
  578. channel_type = rd_mem (dev, &rx_desc->wr_buf_type) & BUFFER_PTR_MASK;
  579. spin_unlock_irqrestore (&dev->mem_lock, flags);
  580. // very serious error, should never occur
  581. if (channel_type != RX_CHANNEL_DISABLED) {
  582. PRINTD (DBG_ERR|DBG_VCC, "RX channel for VC already open");
  583. return -EBUSY; // clean up?
  584. }
  585. // Give back spare buffer
  586. if (dev->noof_spare_buffers) {
  587. buf_ptr = dev->spare_buffers[--dev->noof_spare_buffers];
  588. PRINTD (DBG_VCC, "using a spare buffer: %u", buf_ptr);
  589. // should never occur
  590. if (buf_ptr == RX_CHANNEL_DISABLED || buf_ptr == RX_CHANNEL_IDLE) {
  591. // but easy to recover from
  592. PRINTD (DBG_ERR|DBG_VCC, "bad spare buffer pointer, using IDLE");
  593. buf_ptr = RX_CHANNEL_IDLE;
  594. }
  595. } else {
  596. PRINTD (DBG_VCC, "using IDLE buffer pointer");
  597. }
  598. // Channel is currently disabled so change its status to idle
  599. // do we really need to save the flags again?
  600. spin_lock_irqsave (&dev->mem_lock, flags);
  601. wr_mem (dev, &rx_desc->wr_buf_type,
  602. buf_ptr | CHANNEL_TYPE_AAL5 | FIRST_CELL_OF_AAL5_FRAME);
  603. if (buf_ptr != RX_CHANNEL_IDLE)
  604. wr_mem (dev, &rx_desc->rd_buf_type, buf_ptr);
  605. spin_unlock_irqrestore (&dev->mem_lock, flags);
  606. // rxer->rate = make_rate (qos->peak_cells);
  607. PRINTD (DBG_FLOW, "hrz_open_rx ok");
  608. return 0;
  609. }
  610. #if 0
  611. /********** change vc rate for a given vc **********/
  612. static void hrz_change_vc_qos (ATM_RXER * rxer, MAAL_QOS * qos) {
  613. rxer->rate = make_rate (qos->peak_cells);
  614. }
  615. #endif
  616. /********** free an skb (as per ATM device driver documentation) **********/
  617. static void hrz_kfree_skb (struct sk_buff * skb) {
  618. if (ATM_SKB(skb)->vcc->pop) {
  619. ATM_SKB(skb)->vcc->pop (ATM_SKB(skb)->vcc, skb);
  620. } else {
  621. dev_kfree_skb_any (skb);
  622. }
  623. }
  624. /********** cancel listen on a VC **********/
  625. static void hrz_close_rx (hrz_dev * dev, u16 vc) {
  626. unsigned long flags;
  627. u32 value;
  628. u32 r1, r2;
  629. rx_ch_desc * rx_desc = &memmap->rx_descs[vc];
  630. int was_idle = 0;
  631. spin_lock_irqsave (&dev->mem_lock, flags);
  632. value = rd_mem (dev, &rx_desc->wr_buf_type) & BUFFER_PTR_MASK;
  633. spin_unlock_irqrestore (&dev->mem_lock, flags);
  634. if (value == RX_CHANNEL_DISABLED) {
  635. // I suppose this could happen once we deal with _NONE traffic properly
  636. PRINTD (DBG_VCC, "closing VC: RX channel %u already disabled", vc);
  637. return;
  638. }
  639. if (value == RX_CHANNEL_IDLE)
  640. was_idle = 1;
  641. spin_lock_irqsave (&dev->mem_lock, flags);
  642. for (;;) {
  643. wr_mem (dev, &rx_desc->wr_buf_type, RX_CHANNEL_DISABLED);
  644. if ((rd_mem (dev, &rx_desc->wr_buf_type) & BUFFER_PTR_MASK) == RX_CHANNEL_DISABLED)
  645. break;
  646. was_idle = 0;
  647. }
  648. if (was_idle) {
  649. spin_unlock_irqrestore (&dev->mem_lock, flags);
  650. return;
  651. }
  652. WAIT_FLUSH_RX_COMPLETE(dev);
  653. // XXX Is this all really necessary? We can rely on the rx_data_av
  654. // handler to discard frames that remain queued for delivery. If the
  655. // worry is that immediately reopening the channel (perhaps by a
  656. // different process) may cause some data to be mis-delivered then
  657. // there may still be a simpler solution (such as busy-waiting on
  658. // rx_busy once the channel is disabled or before a new one is
  659. // opened - does this leave any holes?). Arguably setting up and
  660. // tearing down the TX and RX halves of each virtual circuit could
  661. // most safely be done within ?x_busy protected regions.
  662. // OK, current changes are that Simon's marker is disabled and we DO
  663. // look for NULL rxer elsewhere. The code here seems flush frames
  664. // and then remember the last dead cell belonging to the channel
  665. // just disabled - the cell gets relinked at the next vc_open.
  666. // However, when all VCs are closed or only a few opened there are a
  667. // handful of buffers that are unusable.
  668. // Does anyone feel like documenting spare_buffers properly?
  669. // Does anyone feel like fixing this in a nicer way?
  670. // Flush any data which is left in the channel
  671. for (;;) {
  672. // Change the rx channel port to something different to the RX
  673. // channel we are trying to close to force Horizon to flush the rx
  674. // channel read and write pointers.
  675. u16 other = vc^(RX_CHANS/2);
  676. SELECT_RX_CHANNEL (dev, other);
  677. WAIT_UPDATE_COMPLETE (dev);
  678. r1 = rd_mem (dev, &rx_desc->rd_buf_type);
  679. // Select this RX channel. Flush doesn't seem to work unless we
  680. // select an RX channel before hand
  681. SELECT_RX_CHANNEL (dev, vc);
  682. WAIT_UPDATE_COMPLETE (dev);
  683. // Attempt to flush a frame on this RX channel
  684. FLUSH_RX_CHANNEL (dev, vc);
  685. WAIT_FLUSH_RX_COMPLETE (dev);
  686. // Force Horizon to flush rx channel read and write pointers as before
  687. SELECT_RX_CHANNEL (dev, other);
  688. WAIT_UPDATE_COMPLETE (dev);
  689. r2 = rd_mem (dev, &rx_desc->rd_buf_type);
  690. PRINTD (DBG_VCC|DBG_RX, "r1 = %u, r2 = %u", r1, r2);
  691. if (r1 == r2) {
  692. dev->spare_buffers[dev->noof_spare_buffers++] = (u16)r1;
  693. break;
  694. }
  695. }
  696. #if 0
  697. {
  698. rx_q_entry * wr_ptr = &memmap->rx_q_entries[rd_regw (dev, RX_QUEUE_WR_PTR_OFF)];
  699. rx_q_entry * rd_ptr = dev->rx_q_entry;
  700. PRINTD (DBG_VCC|DBG_RX, "rd_ptr = %u, wr_ptr = %u", rd_ptr, wr_ptr);
  701. while (rd_ptr != wr_ptr) {
  702. u32 x = rd_mem (dev, (HDW *) rd_ptr);
  703. if (vc == rx_q_entry_to_rx_channel (x)) {
  704. x |= SIMONS_DODGEY_MARKER;
  705. PRINTD (DBG_RX|DBG_VCC|DBG_WARN, "marking a frame as dodgey");
  706. wr_mem (dev, (HDW *) rd_ptr, x);
  707. }
  708. if (rd_ptr == dev->rx_q_wrap)
  709. rd_ptr = dev->rx_q_reset;
  710. else
  711. rd_ptr++;
  712. }
  713. }
  714. #endif
  715. spin_unlock_irqrestore (&dev->mem_lock, flags);
  716. return;
  717. }
  718. /********** schedule RX transfers **********/
  719. // Note on tail recursion: a GCC developer said that it is not likely
  720. // to be fixed soon, so do not define TAILRECUSRIONWORKS unless you
  721. // are sure it does as you may otherwise overflow the kernel stack.
  722. // giving this fn a return value would help GCC, allegedly
  723. static void rx_schedule (hrz_dev * dev, int irq) {
  724. unsigned int rx_bytes;
  725. int pio_instead = 0;
  726. #ifndef TAILRECURSIONWORKS
  727. pio_instead = 1;
  728. while (pio_instead) {
  729. #endif
  730. // bytes waiting for RX transfer
  731. rx_bytes = dev->rx_bytes;
  732. #if 0
  733. spin_count = 0;
  734. while (rd_regl (dev, MASTER_RX_COUNT_REG_OFF)) {
  735. PRINTD (DBG_RX|DBG_WARN, "RX error: other PCI Bus Master RX still in progress!");
  736. if (++spin_count > 10) {
  737. PRINTD (DBG_RX|DBG_ERR, "spun out waiting PCI Bus Master RX completion");
  738. wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
  739. clear_bit (rx_busy, &dev->flags);
  740. hrz_kfree_skb (dev->rx_skb);
  741. return;
  742. }
  743. }
  744. #endif
  745. // this code follows the TX code but (at the moment) there is only
  746. // one region - the skb itself. I don't know if this will change,
  747. // but it doesn't hurt to have the code here, disabled.
  748. if (rx_bytes) {
  749. // start next transfer within same region
  750. if (rx_bytes <= MAX_PIO_COUNT) {
  751. PRINTD (DBG_RX|DBG_BUS, "(pio)");
  752. pio_instead = 1;
  753. }
  754. if (rx_bytes <= MAX_TRANSFER_COUNT) {
  755. PRINTD (DBG_RX|DBG_BUS, "(simple or last multi)");
  756. dev->rx_bytes = 0;
  757. } else {
  758. PRINTD (DBG_RX|DBG_BUS, "(continuing multi)");
  759. dev->rx_bytes = rx_bytes - MAX_TRANSFER_COUNT;
  760. rx_bytes = MAX_TRANSFER_COUNT;
  761. }
  762. } else {
  763. // rx_bytes == 0 -- we're between regions
  764. // regions remaining to transfer
  765. #if 0
  766. unsigned int rx_regions = dev->rx_regions;
  767. #else
  768. unsigned int rx_regions = 0;
  769. #endif
  770. if (rx_regions) {
  771. #if 0
  772. // start a new region
  773. dev->rx_addr = dev->rx_iovec->iov_base;
  774. rx_bytes = dev->rx_iovec->iov_len;
  775. ++dev->rx_iovec;
  776. dev->rx_regions = rx_regions - 1;
  777. if (rx_bytes <= MAX_PIO_COUNT) {
  778. PRINTD (DBG_RX|DBG_BUS, "(pio)");
  779. pio_instead = 1;
  780. }
  781. if (rx_bytes <= MAX_TRANSFER_COUNT) {
  782. PRINTD (DBG_RX|DBG_BUS, "(full region)");
  783. dev->rx_bytes = 0;
  784. } else {
  785. PRINTD (DBG_RX|DBG_BUS, "(start multi region)");
  786. dev->rx_bytes = rx_bytes - MAX_TRANSFER_COUNT;
  787. rx_bytes = MAX_TRANSFER_COUNT;
  788. }
  789. #endif
  790. } else {
  791. // rx_regions == 0
  792. // that's all folks - end of frame
  793. struct sk_buff * skb = dev->rx_skb;
  794. // dev->rx_iovec = 0;
  795. FLUSH_RX_CHANNEL (dev, dev->rx_channel);
  796. dump_skb ("<<<", dev->rx_channel, skb);
  797. PRINTD (DBG_RX|DBG_SKB, "push %p %u", skb->data, skb->len);
  798. {
  799. struct atm_vcc * vcc = ATM_SKB(skb)->vcc;
  800. // VC layer stats
  801. atomic_inc(&vcc->stats->rx);
  802. __net_timestamp(skb);
  803. // end of our responsibility
  804. vcc->push (vcc, skb);
  805. }
  806. }
  807. }
  808. // note: writing RX_COUNT clears any interrupt condition
  809. if (rx_bytes) {
  810. if (pio_instead) {
  811. if (irq)
  812. wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
  813. rds_regb (dev, DATA_PORT_OFF, dev->rx_addr, rx_bytes);
  814. } else {
  815. wr_regl (dev, MASTER_RX_ADDR_REG_OFF, virt_to_bus (dev->rx_addr));
  816. wr_regl (dev, MASTER_RX_COUNT_REG_OFF, rx_bytes);
  817. }
  818. dev->rx_addr += rx_bytes;
  819. } else {
  820. if (irq)
  821. wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
  822. // allow another RX thread to start
  823. YELLOW_LED_ON(dev);
  824. clear_bit (rx_busy, &dev->flags);
  825. PRINTD (DBG_RX, "cleared rx_busy for dev %p", dev);
  826. }
  827. #ifdef TAILRECURSIONWORKS
  828. // and we all bless optimised tail calls
  829. if (pio_instead)
  830. return rx_schedule (dev, 0);
  831. return;
  832. #else
  833. // grrrrrrr!
  834. irq = 0;
  835. }
  836. return;
  837. #endif
  838. }
  839. /********** handle RX bus master complete events **********/
  840. static void rx_bus_master_complete_handler (hrz_dev * dev) {
  841. if (test_bit (rx_busy, &dev->flags)) {
  842. rx_schedule (dev, 1);
  843. } else {
  844. PRINTD (DBG_RX|DBG_ERR, "unexpected RX bus master completion");
  845. // clear interrupt condition on adapter
  846. wr_regl (dev, MASTER_RX_COUNT_REG_OFF, 0);
  847. }
  848. return;
  849. }
  850. /********** (queue to) become the next TX thread **********/
  851. static int tx_hold (hrz_dev * dev) {
  852. PRINTD (DBG_TX, "sleeping at tx lock %p %lu", dev, dev->flags);
  853. wait_event_interruptible(dev->tx_queue, (!test_and_set_bit(tx_busy, &dev->flags)));
  854. PRINTD (DBG_TX, "woken at tx lock %p %lu", dev, dev->flags);
  855. if (signal_pending (current))
  856. return -1;
  857. PRINTD (DBG_TX, "set tx_busy for dev %p", dev);
  858. return 0;
  859. }
  860. /********** allow another TX thread to start **********/
  861. static inline void tx_release (hrz_dev * dev) {
  862. clear_bit (tx_busy, &dev->flags);
  863. PRINTD (DBG_TX, "cleared tx_busy for dev %p", dev);
  864. wake_up_interruptible (&dev->tx_queue);
  865. }
  866. /********** schedule TX transfers **********/
  867. static void tx_schedule (hrz_dev * const dev, int irq) {
  868. unsigned int tx_bytes;
  869. int append_desc = 0;
  870. int pio_instead = 0;
  871. #ifndef TAILRECURSIONWORKS
  872. pio_instead = 1;
  873. while (pio_instead) {
  874. #endif
  875. // bytes in current region waiting for TX transfer
  876. tx_bytes = dev->tx_bytes;
  877. #if 0
  878. spin_count = 0;
  879. while (rd_regl (dev, MASTER_TX_COUNT_REG_OFF)) {
  880. PRINTD (DBG_TX|DBG_WARN, "TX error: other PCI Bus Master TX still in progress!");
  881. if (++spin_count > 10) {
  882. PRINTD (DBG_TX|DBG_ERR, "spun out waiting PCI Bus Master TX completion");
  883. wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
  884. tx_release (dev);
  885. hrz_kfree_skb (dev->tx_skb);
  886. return;
  887. }
  888. }
  889. #endif
  890. if (tx_bytes) {
  891. // start next transfer within same region
  892. if (!test_bit (ultra, &dev->flags) || tx_bytes <= MAX_PIO_COUNT) {
  893. PRINTD (DBG_TX|DBG_BUS, "(pio)");
  894. pio_instead = 1;
  895. }
  896. if (tx_bytes <= MAX_TRANSFER_COUNT) {
  897. PRINTD (DBG_TX|DBG_BUS, "(simple or last multi)");
  898. if (!dev->tx_iovec) {
  899. // end of last region
  900. append_desc = 1;
  901. }
  902. dev->tx_bytes = 0;
  903. } else {
  904. PRINTD (DBG_TX|DBG_BUS, "(continuing multi)");
  905. dev->tx_bytes = tx_bytes - MAX_TRANSFER_COUNT;
  906. tx_bytes = MAX_TRANSFER_COUNT;
  907. }
  908. } else {
  909. // tx_bytes == 0 -- we're between regions
  910. // regions remaining to transfer
  911. unsigned int tx_regions = dev->tx_regions;
  912. if (tx_regions) {
  913. // start a new region
  914. dev->tx_addr = dev->tx_iovec->iov_base;
  915. tx_bytes = dev->tx_iovec->iov_len;
  916. ++dev->tx_iovec;
  917. dev->tx_regions = tx_regions - 1;
  918. if (!test_bit (ultra, &dev->flags) || tx_bytes <= MAX_PIO_COUNT) {
  919. PRINTD (DBG_TX|DBG_BUS, "(pio)");
  920. pio_instead = 1;
  921. }
  922. if (tx_bytes <= MAX_TRANSFER_COUNT) {
  923. PRINTD (DBG_TX|DBG_BUS, "(full region)");
  924. dev->tx_bytes = 0;
  925. } else {
  926. PRINTD (DBG_TX|DBG_BUS, "(start multi region)");
  927. dev->tx_bytes = tx_bytes - MAX_TRANSFER_COUNT;
  928. tx_bytes = MAX_TRANSFER_COUNT;
  929. }
  930. } else {
  931. // tx_regions == 0
  932. // that's all folks - end of frame
  933. struct sk_buff * skb = dev->tx_skb;
  934. dev->tx_iovec = NULL;
  935. // VC layer stats
  936. atomic_inc(&ATM_SKB(skb)->vcc->stats->tx);
  937. // free the skb
  938. hrz_kfree_skb (skb);
  939. }
  940. }
  941. // note: writing TX_COUNT clears any interrupt condition
  942. if (tx_bytes) {
  943. if (pio_instead) {
  944. if (irq)
  945. wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
  946. wrs_regb (dev, DATA_PORT_OFF, dev->tx_addr, tx_bytes);
  947. if (append_desc)
  948. wr_regl (dev, TX_DESCRIPTOR_PORT_OFF, cpu_to_be32 (dev->tx_skb->len));
  949. } else {
  950. wr_regl (dev, MASTER_TX_ADDR_REG_OFF, virt_to_bus (dev->tx_addr));
  951. if (append_desc)
  952. wr_regl (dev, TX_DESCRIPTOR_REG_OFF, cpu_to_be32 (dev->tx_skb->len));
  953. wr_regl (dev, MASTER_TX_COUNT_REG_OFF,
  954. append_desc
  955. ? tx_bytes | MASTER_TX_AUTO_APPEND_DESC
  956. : tx_bytes);
  957. }
  958. dev->tx_addr += tx_bytes;
  959. } else {
  960. if (irq)
  961. wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
  962. YELLOW_LED_ON(dev);
  963. tx_release (dev);
  964. }
  965. #ifdef TAILRECURSIONWORKS
  966. // and we all bless optimised tail calls
  967. if (pio_instead)
  968. return tx_schedule (dev, 0);
  969. return;
  970. #else
  971. // grrrrrrr!
  972. irq = 0;
  973. }
  974. return;
  975. #endif
  976. }
  977. /********** handle TX bus master complete events **********/
  978. static void tx_bus_master_complete_handler (hrz_dev * dev) {
  979. if (test_bit (tx_busy, &dev->flags)) {
  980. tx_schedule (dev, 1);
  981. } else {
  982. PRINTD (DBG_TX|DBG_ERR, "unexpected TX bus master completion");
  983. // clear interrupt condition on adapter
  984. wr_regl (dev, MASTER_TX_COUNT_REG_OFF, 0);
  985. }
  986. return;
  987. }
  988. /********** move RX Q pointer to next item in circular buffer **********/
  989. // called only from IRQ sub-handler
  990. static u32 rx_queue_entry_next (hrz_dev * dev) {
  991. u32 rx_queue_entry;
  992. spin_lock (&dev->mem_lock);
  993. rx_queue_entry = rd_mem (dev, &dev->rx_q_entry->entry);
  994. if (dev->rx_q_entry == dev->rx_q_wrap)
  995. dev->rx_q_entry = dev->rx_q_reset;
  996. else
  997. dev->rx_q_entry++;
  998. wr_regw (dev, RX_QUEUE_RD_PTR_OFF, dev->rx_q_entry - dev->rx_q_reset);
  999. spin_unlock (&dev->mem_lock);
  1000. return rx_queue_entry;
  1001. }
  1002. /********** handle RX data received by device **********/
  1003. // called from IRQ handler
  1004. static void rx_data_av_handler (hrz_dev * dev) {
  1005. u32 rx_queue_entry;
  1006. u32 rx_queue_entry_flags;
  1007. u16 rx_len;
  1008. u16 rx_channel;
  1009. PRINTD (DBG_FLOW, "hrz_data_av_handler");
  1010. // try to grab rx lock (not possible during RX bus mastering)
  1011. if (test_and_set_bit (rx_busy, &dev->flags)) {
  1012. PRINTD (DBG_RX, "locked out of rx lock");
  1013. return;
  1014. }
  1015. PRINTD (DBG_RX, "set rx_busy for dev %p", dev);
  1016. // lock is cleared if we fail now, o/w after bus master completion
  1017. YELLOW_LED_OFF(dev);
  1018. rx_queue_entry = rx_queue_entry_next (dev);
  1019. rx_len = rx_q_entry_to_length (rx_queue_entry);
  1020. rx_channel = rx_q_entry_to_rx_channel (rx_queue_entry);
  1021. WAIT_FLUSH_RX_COMPLETE (dev);
  1022. SELECT_RX_CHANNEL (dev, rx_channel);
  1023. PRINTD (DBG_RX, "rx_queue_entry is: %#x", rx_queue_entry);
  1024. rx_queue_entry_flags = rx_queue_entry & (RX_CRC_32_OK|RX_COMPLETE_FRAME|SIMONS_DODGEY_MARKER);
  1025. if (!rx_len) {
  1026. // (at least) bus-mastering breaks if we try to handle a
  1027. // zero-length frame, besides AAL5 does not support them
  1028. PRINTK (KERN_ERR, "zero-length frame!");
  1029. rx_queue_entry_flags &= ~RX_COMPLETE_FRAME;
  1030. }
  1031. if (rx_queue_entry_flags & SIMONS_DODGEY_MARKER) {
  1032. PRINTD (DBG_RX|DBG_ERR, "Simon's marker detected!");
  1033. }
  1034. if (rx_queue_entry_flags == (RX_CRC_32_OK | RX_COMPLETE_FRAME)) {
  1035. struct atm_vcc * atm_vcc;
  1036. PRINTD (DBG_RX, "got a frame on rx_channel %x len %u", rx_channel, rx_len);
  1037. atm_vcc = dev->rxer[rx_channel];
  1038. // if no vcc is assigned to this channel, we should drop the frame
  1039. // (is this what SIMONS etc. was trying to achieve?)
  1040. if (atm_vcc) {
  1041. if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
  1042. if (rx_len <= atm_vcc->qos.rxtp.max_sdu) {
  1043. struct sk_buff * skb = atm_alloc_charge (atm_vcc, rx_len, GFP_ATOMIC);
  1044. if (skb) {
  1045. // remember this so we can push it later
  1046. dev->rx_skb = skb;
  1047. // remember this so we can flush it later
  1048. dev->rx_channel = rx_channel;
  1049. // prepare socket buffer
  1050. skb_put (skb, rx_len);
  1051. ATM_SKB(skb)->vcc = atm_vcc;
  1052. // simple transfer
  1053. // dev->rx_regions = 0;
  1054. // dev->rx_iovec = 0;
  1055. dev->rx_bytes = rx_len;
  1056. dev->rx_addr = skb->data;
  1057. PRINTD (DBG_RX, "RX start simple transfer (addr %p, len %d)",
  1058. skb->data, rx_len);
  1059. // do the business
  1060. rx_schedule (dev, 0);
  1061. return;
  1062. } else {
  1063. PRINTD (DBG_SKB|DBG_WARN, "failed to get skb");
  1064. }
  1065. } else {
  1066. PRINTK (KERN_INFO, "frame received on TX-only VC %x", rx_channel);
  1067. // do we count this?
  1068. }
  1069. } else {
  1070. PRINTK (KERN_WARNING, "dropped over-size frame");
  1071. // do we count this?
  1072. }
  1073. } else {
  1074. PRINTD (DBG_WARN|DBG_VCC|DBG_RX, "no VCC for this frame (VC closed)");
  1075. // do we count this?
  1076. }
  1077. } else {
  1078. // Wait update complete ? SPONG
  1079. }
  1080. // RX was aborted
  1081. YELLOW_LED_ON(dev);
  1082. FLUSH_RX_CHANNEL (dev,rx_channel);
  1083. clear_bit (rx_busy, &dev->flags);
  1084. return;
  1085. }
  1086. /********** interrupt handler **********/
  1087. static irqreturn_t interrupt_handler(int irq, void *dev_id)
  1088. {
  1089. hrz_dev *dev = dev_id;
  1090. u32 int_source;
  1091. unsigned int irq_ok;
  1092. PRINTD (DBG_FLOW, "interrupt_handler: %p", dev_id);
  1093. // definitely for us
  1094. irq_ok = 0;
  1095. while ((int_source = rd_regl (dev, INT_SOURCE_REG_OFF)
  1096. & INTERESTING_INTERRUPTS)) {
  1097. // In the interests of fairness, the handlers below are
  1098. // called in sequence and without immediate return to the head of
  1099. // the while loop. This is only of issue for slow hosts (or when
  1100. // debugging messages are on). Really slow hosts may find a fast
  1101. // sender keeps them permanently in the IRQ handler. :(
  1102. // (only an issue for slow hosts) RX completion goes before
  1103. // rx_data_av as the former implies rx_busy and so the latter
  1104. // would just abort. If it reschedules another transfer
  1105. // (continuing the same frame) then it will not clear rx_busy.
  1106. // (only an issue for slow hosts) TX completion goes before RX
  1107. // data available as it is a much shorter routine - there is the
  1108. // chance that any further transfers it schedules will be complete
  1109. // by the time of the return to the head of the while loop
  1110. if (int_source & RX_BUS_MASTER_COMPLETE) {
  1111. ++irq_ok;
  1112. PRINTD (DBG_IRQ|DBG_BUS|DBG_RX, "rx_bus_master_complete asserted");
  1113. rx_bus_master_complete_handler (dev);
  1114. }
  1115. if (int_source & TX_BUS_MASTER_COMPLETE) {
  1116. ++irq_ok;
  1117. PRINTD (DBG_IRQ|DBG_BUS|DBG_TX, "tx_bus_master_complete asserted");
  1118. tx_bus_master_complete_handler (dev);
  1119. }
  1120. if (int_source & RX_DATA_AV) {
  1121. ++irq_ok;
  1122. PRINTD (DBG_IRQ|DBG_RX, "rx_data_av asserted");
  1123. rx_data_av_handler (dev);
  1124. }
  1125. }
  1126. if (irq_ok) {
  1127. PRINTD (DBG_IRQ, "work done: %u", irq_ok);
  1128. } else {
  1129. PRINTD (DBG_IRQ|DBG_WARN, "spurious interrupt source: %#x", int_source);
  1130. }
  1131. PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler done: %p", dev_id);
  1132. if (irq_ok)
  1133. return IRQ_HANDLED;
  1134. return IRQ_NONE;
  1135. }
  1136. /********** housekeeping **********/
  1137. static void do_housekeeping (unsigned long arg) {
  1138. // just stats at the moment
  1139. hrz_dev * dev = (hrz_dev *) arg;
  1140. // collect device-specific (not driver/atm-linux) stats here
  1141. dev->tx_cell_count += rd_regw (dev, TX_CELL_COUNT_OFF);
  1142. dev->rx_cell_count += rd_regw (dev, RX_CELL_COUNT_OFF);
  1143. dev->hec_error_count += rd_regw (dev, HEC_ERROR_COUNT_OFF);
  1144. dev->unassigned_cell_count += rd_regw (dev, UNASSIGNED_CELL_COUNT_OFF);
  1145. mod_timer (&dev->housekeeping, jiffies + HZ/10);
  1146. return;
  1147. }
  1148. /********** find an idle channel for TX and set it up **********/
  1149. // called with tx_busy set
  1150. static short setup_idle_tx_channel (hrz_dev * dev, hrz_vcc * vcc) {
  1151. unsigned short idle_channels;
  1152. short tx_channel = -1;
  1153. unsigned int spin_count;
  1154. PRINTD (DBG_FLOW|DBG_TX, "setup_idle_tx_channel %p", dev);
  1155. // better would be to fail immediately, the caller can then decide whether
  1156. // to wait or drop (depending on whether this is UBR etc.)
  1157. spin_count = 0;
  1158. while (!(idle_channels = rd_regw (dev, TX_STATUS_OFF) & IDLE_CHANNELS_MASK)) {
  1159. PRINTD (DBG_TX|DBG_WARN, "waiting for idle TX channel");
  1160. // delay a bit here
  1161. if (++spin_count > 100) {
  1162. PRINTD (DBG_TX|DBG_ERR, "spun out waiting for idle TX channel");
  1163. return -EBUSY;
  1164. }
  1165. }
  1166. // got an idle channel
  1167. {
  1168. // tx_idle ensures we look for idle channels in RR order
  1169. int chan = dev->tx_idle;
  1170. int keep_going = 1;
  1171. while (keep_going) {
  1172. if (idle_channels & (1<<chan)) {
  1173. tx_channel = chan;
  1174. keep_going = 0;
  1175. }
  1176. ++chan;
  1177. if (chan == TX_CHANS)
  1178. chan = 0;
  1179. }
  1180. dev->tx_idle = chan;
  1181. }
  1182. // set up the channel we found
  1183. {
  1184. // Initialise the cell header in the transmit channel descriptor
  1185. // a.k.a. prepare the channel and remember that we have done so.
  1186. tx_ch_desc * tx_desc = &memmap->tx_descs[tx_channel];
  1187. u32 rd_ptr;
  1188. u32 wr_ptr;
  1189. u16 channel = vcc->channel;
  1190. unsigned long flags;
  1191. spin_lock_irqsave (&dev->mem_lock, flags);
  1192. // Update the transmit channel record.
  1193. dev->tx_channel_record[tx_channel] = channel;
  1194. // xBR channel
  1195. update_tx_channel_config (dev, tx_channel, RATE_TYPE_ACCESS,
  1196. vcc->tx_xbr_bits);
  1197. // Update the PCR counter preload value etc.
  1198. update_tx_channel_config (dev, tx_channel, PCR_TIMER_ACCESS,
  1199. vcc->tx_pcr_bits);
  1200. #if 0
  1201. if (vcc->tx_xbr_bits == VBR_RATE_TYPE) {
  1202. // SCR timer
  1203. update_tx_channel_config (dev, tx_channel, SCR_TIMER_ACCESS,
  1204. vcc->tx_scr_bits);
  1205. // Bucket size...
  1206. update_tx_channel_config (dev, tx_channel, BUCKET_CAPACITY_ACCESS,
  1207. vcc->tx_bucket_bits);
  1208. // ... and fullness
  1209. update_tx_channel_config (dev, tx_channel, BUCKET_FULLNESS_ACCESS,
  1210. vcc->tx_bucket_bits);
  1211. }
  1212. #endif
  1213. // Initialise the read and write buffer pointers
  1214. rd_ptr = rd_mem (dev, &tx_desc->rd_buf_type) & BUFFER_PTR_MASK;
  1215. wr_ptr = rd_mem (dev, &tx_desc->wr_buf_type) & BUFFER_PTR_MASK;
  1216. // idle TX channels should have identical pointers
  1217. if (rd_ptr != wr_ptr) {
  1218. PRINTD (DBG_TX|DBG_ERR, "TX buffer pointers are broken!");
  1219. // spin_unlock... return -E...
  1220. // I wonder if gcc would get rid of one of the pointer aliases
  1221. }
  1222. PRINTD (DBG_TX, "TX buffer pointers are: rd %x, wr %x.",
  1223. rd_ptr, wr_ptr);
  1224. switch (vcc->aal) {
  1225. case aal0:
  1226. PRINTD (DBG_QOS|DBG_TX, "tx_channel: aal0");
  1227. rd_ptr |= CHANNEL_TYPE_RAW_CELLS;
  1228. wr_ptr |= CHANNEL_TYPE_RAW_CELLS;
  1229. break;
  1230. case aal34:
  1231. PRINTD (DBG_QOS|DBG_TX, "tx_channel: aal34");
  1232. rd_ptr |= CHANNEL_TYPE_AAL3_4;
  1233. wr_ptr |= CHANNEL_TYPE_AAL3_4;
  1234. break;
  1235. case aal5:
  1236. rd_ptr |= CHANNEL_TYPE_AAL5;
  1237. wr_ptr |= CHANNEL_TYPE_AAL5;
  1238. // Initialise the CRC
  1239. wr_mem (dev, &tx_desc->partial_crc, INITIAL_CRC);
  1240. break;
  1241. }
  1242. wr_mem (dev, &tx_desc->rd_buf_type, rd_ptr);
  1243. wr_mem (dev, &tx_desc->wr_buf_type, wr_ptr);
  1244. // Write the Cell Header
  1245. // Payload Type, CLP and GFC would go here if non-zero
  1246. wr_mem (dev, &tx_desc->cell_header, channel);
  1247. spin_unlock_irqrestore (&dev->mem_lock, flags);
  1248. }
  1249. return tx_channel;
  1250. }
  1251. /********** send a frame **********/
  1252. static int hrz_send (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
  1253. unsigned int spin_count;
  1254. int free_buffers;
  1255. hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
  1256. hrz_vcc * vcc = HRZ_VCC(atm_vcc);
  1257. u16 channel = vcc->channel;
  1258. u32 buffers_required;
  1259. /* signed for error return */
  1260. short tx_channel;
  1261. PRINTD (DBG_FLOW|DBG_TX, "hrz_send vc %x data %p len %u",
  1262. channel, skb->data, skb->len);
  1263. dump_skb (">>>", channel, skb);
  1264. if (atm_vcc->qos.txtp.traffic_class == ATM_NONE) {
  1265. PRINTK (KERN_ERR, "attempt to send on RX-only VC %x", channel);
  1266. hrz_kfree_skb (skb);
  1267. return -EIO;
  1268. }
  1269. // don't understand this
  1270. ATM_SKB(skb)->vcc = atm_vcc;
  1271. if (skb->len > atm_vcc->qos.txtp.max_sdu) {
  1272. PRINTK (KERN_ERR, "sk_buff length greater than agreed max_sdu, dropping...");
  1273. hrz_kfree_skb (skb);
  1274. return -EIO;
  1275. }
  1276. if (!channel) {
  1277. PRINTD (DBG_ERR|DBG_TX, "attempt to transmit on zero (rx_)channel");
  1278. hrz_kfree_skb (skb);
  1279. return -EIO;
  1280. }
  1281. #if 0
  1282. {
  1283. // where would be a better place for this? housekeeping?
  1284. u16 status;
  1285. pci_read_config_word (dev->pci_dev, PCI_STATUS, &status);
  1286. if (status & PCI_STATUS_REC_MASTER_ABORT) {
  1287. PRINTD (DBG_BUS|DBG_ERR, "Clearing PCI Master Abort (and cleaning up)");
  1288. status &= ~PCI_STATUS_REC_MASTER_ABORT;
  1289. pci_write_config_word (dev->pci_dev, PCI_STATUS, status);
  1290. if (test_bit (tx_busy, &dev->flags)) {
  1291. hrz_kfree_skb (dev->tx_skb);
  1292. tx_release (dev);
  1293. }
  1294. }
  1295. }
  1296. #endif
  1297. #ifdef DEBUG_HORIZON
  1298. /* wey-hey! */
  1299. if (channel == 1023) {
  1300. unsigned int i;
  1301. unsigned short d = 0;
  1302. char * s = skb->data;
  1303. if (*s++ == 'D') {
  1304. for (i = 0; i < 4; ++i)
  1305. d = (d << 4) | hex_to_bin(*s++);
  1306. PRINTK (KERN_INFO, "debug bitmap is now %hx", debug = d);
  1307. }
  1308. }
  1309. #endif
  1310. // wait until TX is free and grab lock
  1311. if (tx_hold (dev)) {
  1312. hrz_kfree_skb (skb);
  1313. return -ERESTARTSYS;
  1314. }
  1315. // Wait for enough space to be available in transmit buffer memory.
  1316. // should be number of cells needed + 2 (according to hardware docs)
  1317. // = ((framelen+8)+47) / 48 + 2
  1318. // = (framelen+7) / 48 + 3, hmm... faster to put addition inside XXX
  1319. buffers_required = (skb->len+(ATM_AAL5_TRAILER-1)) / ATM_CELL_PAYLOAD + 3;
  1320. // replace with timer and sleep, add dev->tx_buffers_queue (max 1 entry)
  1321. spin_count = 0;
  1322. while ((free_buffers = rd_regw (dev, TX_FREE_BUFFER_COUNT_OFF)) < buffers_required) {
  1323. PRINTD (DBG_TX, "waiting for free TX buffers, got %d of %d",
  1324. free_buffers, buffers_required);
  1325. // what is the appropriate delay? implement a timeout? (depending on line speed?)
  1326. // mdelay (1);
  1327. // what happens if we kill (current_pid, SIGKILL) ?
  1328. schedule();
  1329. if (++spin_count > 1000) {
  1330. PRINTD (DBG_TX|DBG_ERR, "spun out waiting for tx buffers, got %d of %d",
  1331. free_buffers, buffers_required);
  1332. tx_release (dev);
  1333. hrz_kfree_skb (skb);
  1334. return -ERESTARTSYS;
  1335. }
  1336. }
  1337. // Select a channel to transmit the frame on.
  1338. if (channel == dev->last_vc) {
  1339. PRINTD (DBG_TX, "last vc hack: hit");
  1340. tx_channel = dev->tx_last;
  1341. } else {
  1342. PRINTD (DBG_TX, "last vc hack: miss");
  1343. // Are we currently transmitting this VC on one of the channels?
  1344. for (tx_channel = 0; tx_channel < TX_CHANS; ++tx_channel)
  1345. if (dev->tx_channel_record[tx_channel] == channel) {
  1346. PRINTD (DBG_TX, "vc already on channel: hit");
  1347. break;
  1348. }
  1349. if (tx_channel == TX_CHANS) {
  1350. PRINTD (DBG_TX, "vc already on channel: miss");
  1351. // Find and set up an idle channel.
  1352. tx_channel = setup_idle_tx_channel (dev, vcc);
  1353. if (tx_channel < 0) {
  1354. PRINTD (DBG_TX|DBG_ERR, "failed to get channel");
  1355. tx_release (dev);
  1356. return tx_channel;
  1357. }
  1358. }
  1359. PRINTD (DBG_TX, "got channel");
  1360. SELECT_TX_CHANNEL(dev, tx_channel);
  1361. dev->last_vc = channel;
  1362. dev->tx_last = tx_channel;
  1363. }
  1364. PRINTD (DBG_TX, "using channel %u", tx_channel);
  1365. YELLOW_LED_OFF(dev);
  1366. // TX start transfer
  1367. {
  1368. unsigned int tx_len = skb->len;
  1369. unsigned int tx_iovcnt = skb_shinfo(skb)->nr_frags;
  1370. // remember this so we can free it later
  1371. dev->tx_skb = skb;
  1372. if (tx_iovcnt) {
  1373. // scatter gather transfer
  1374. dev->tx_regions = tx_iovcnt;
  1375. dev->tx_iovec = NULL; /* @@@ needs rewritten */
  1376. dev->tx_bytes = 0;
  1377. PRINTD (DBG_TX|DBG_BUS, "TX start scatter-gather transfer (iovec %p, len %d)",
  1378. skb->data, tx_len);
  1379. tx_release (dev);
  1380. hrz_kfree_skb (skb);
  1381. return -EIO;
  1382. } else {
  1383. // simple transfer
  1384. dev->tx_regions = 0;
  1385. dev->tx_iovec = NULL;
  1386. dev->tx_bytes = tx_len;
  1387. dev->tx_addr = skb->data;
  1388. PRINTD (DBG_TX|DBG_BUS, "TX start simple transfer (addr %p, len %d)",
  1389. skb->data, tx_len);
  1390. }
  1391. // and do the business
  1392. tx_schedule (dev, 0);
  1393. }
  1394. return 0;
  1395. }
  1396. /********** reset a card **********/
  1397. static void hrz_reset (const hrz_dev * dev) {
  1398. u32 control_0_reg = rd_regl (dev, CONTROL_0_REG);
  1399. // why not set RESET_HORIZON to one and wait for the card to
  1400. // reassert that bit as zero? Like so:
  1401. control_0_reg = control_0_reg & RESET_HORIZON;
  1402. wr_regl (dev, CONTROL_0_REG, control_0_reg);
  1403. while (control_0_reg & RESET_HORIZON)
  1404. control_0_reg = rd_regl (dev, CONTROL_0_REG);
  1405. // old reset code retained:
  1406. wr_regl (dev, CONTROL_0_REG, control_0_reg |
  1407. RESET_ATM | RESET_RX | RESET_TX | RESET_HOST);
  1408. // just guessing here
  1409. udelay (1000);
  1410. wr_regl (dev, CONTROL_0_REG, control_0_reg);
  1411. }
  1412. /********** read the burnt in address **********/
  1413. static void WRITE_IT_WAIT (const hrz_dev *dev, u32 ctrl)
  1414. {
  1415. wr_regl (dev, CONTROL_0_REG, ctrl);
  1416. udelay (5);
  1417. }
  1418. static void CLOCK_IT (const hrz_dev *dev, u32 ctrl)
  1419. {
  1420. // DI must be valid around rising SK edge
  1421. WRITE_IT_WAIT(dev, ctrl & ~SEEPROM_SK);
  1422. WRITE_IT_WAIT(dev, ctrl | SEEPROM_SK);
  1423. }
  1424. static u16 read_bia(const hrz_dev *dev, u16 addr)
  1425. {
  1426. u32 ctrl = rd_regl (dev, CONTROL_0_REG);
  1427. const unsigned int addr_bits = 6;
  1428. const unsigned int data_bits = 16;
  1429. unsigned int i;
  1430. u16 res;
  1431. ctrl &= ~(SEEPROM_CS | SEEPROM_SK | SEEPROM_DI);
  1432. WRITE_IT_WAIT(dev, ctrl);
  1433. // wake Serial EEPROM and send 110 (READ) command
  1434. ctrl |= (SEEPROM_CS | SEEPROM_DI);
  1435. CLOCK_IT(dev, ctrl);
  1436. ctrl |= SEEPROM_DI;
  1437. CLOCK_IT(dev, ctrl);
  1438. ctrl &= ~SEEPROM_DI;
  1439. CLOCK_IT(dev, ctrl);
  1440. for (i=0; i<addr_bits; i++) {
  1441. if (addr & (1 << (addr_bits-1)))
  1442. ctrl |= SEEPROM_DI;
  1443. else
  1444. ctrl &= ~SEEPROM_DI;
  1445. CLOCK_IT(dev, ctrl);
  1446. addr = addr << 1;
  1447. }
  1448. // we could check that we have DO = 0 here
  1449. ctrl &= ~SEEPROM_DI;
  1450. res = 0;
  1451. for (i=0;i<data_bits;i++) {
  1452. res = res >> 1;
  1453. CLOCK_IT(dev, ctrl);
  1454. if (rd_regl (dev, CONTROL_0_REG) & SEEPROM_DO)
  1455. res |= (1 << (data_bits-1));
  1456. }
  1457. ctrl &= ~(SEEPROM_SK | SEEPROM_CS);
  1458. WRITE_IT_WAIT(dev, ctrl);
  1459. return res;
  1460. }
  1461. /********** initialise a card **********/
  1462. static int hrz_init(hrz_dev *dev)
  1463. {
  1464. int onefivefive;
  1465. u16 chan;
  1466. int buff_count;
  1467. HDW * mem;
  1468. cell_buf * tx_desc;
  1469. cell_buf * rx_desc;
  1470. u32 ctrl;
  1471. ctrl = rd_regl (dev, CONTROL_0_REG);
  1472. PRINTD (DBG_INFO, "ctrl0reg is %#x", ctrl);
  1473. onefivefive = ctrl & ATM_LAYER_STATUS;
  1474. if (onefivefive)
  1475. printk (DEV_LABEL ": Horizon Ultra (at 155.52 MBps)");
  1476. else
  1477. printk (DEV_LABEL ": Horizon (at 25 MBps)");
  1478. printk (":");
  1479. // Reset the card to get everything in a known state
  1480. printk (" reset");
  1481. hrz_reset (dev);
  1482. // Clear all the buffer memory
  1483. printk (" clearing memory");
  1484. for (mem = (HDW *) memmap; mem < (HDW *) (memmap + 1); ++mem)
  1485. wr_mem (dev, mem, 0);
  1486. printk (" tx channels");
  1487. // All transmit eight channels are set up as AAL5 ABR channels with
  1488. // a 16us cell spacing. Why?
  1489. // Channel 0 gets the free buffer at 100h, channel 1 gets the free
  1490. // buffer at 110h etc.
  1491. for (chan = 0; chan < TX_CHANS; ++chan) {
  1492. tx_ch_desc * tx_desc = &memmap->tx_descs[chan];
  1493. cell_buf * buf = &memmap->inittxbufs[chan];
  1494. // initialise the read and write buffer pointers
  1495. wr_mem (dev, &tx_desc->rd_buf_type, BUF_PTR(buf));
  1496. wr_mem (dev, &tx_desc->wr_buf_type, BUF_PTR(buf));
  1497. // set the status of the initial buffers to empty
  1498. wr_mem (dev, &buf->next, BUFF_STATUS_EMPTY);
  1499. }
  1500. // Use space bufn3 at the moment for tx buffers
  1501. printk (" tx buffers");
  1502. tx_desc = memmap->bufn3;
  1503. wr_mem (dev, &memmap->txfreebufstart.next, BUF_PTR(tx_desc) | BUFF_STATUS_EMPTY);
  1504. for (buff_count = 0; buff_count < BUFN3_SIZE-1; buff_count++) {
  1505. wr_mem (dev, &tx_desc->next, BUF_PTR(tx_desc+1) | BUFF_STATUS_EMPTY);
  1506. tx_desc++;
  1507. }
  1508. wr_mem (dev, &tx_desc->next, BUF_PTR(&memmap->txfreebufend) | BUFF_STATUS_EMPTY);
  1509. // Initialise the transmit free buffer count
  1510. wr_regw (dev, TX_FREE_BUFFER_COUNT_OFF, BUFN3_SIZE);
  1511. printk (" rx channels");
  1512. // Initialise all of the receive channels to be AAL5 disabled with
  1513. // an interrupt threshold of 0
  1514. for (chan = 0; chan < RX_CHANS; ++chan) {
  1515. rx_ch_desc * rx_desc = &memmap->rx_descs[chan];
  1516. wr_mem (dev, &rx_desc->wr_buf_type, CHANNEL_TYPE_AAL5 | RX_CHANNEL_DISABLED);
  1517. }
  1518. printk (" rx buffers");
  1519. // Use space bufn4 at the moment for rx buffers
  1520. rx_desc = memmap->bufn4;
  1521. wr_mem (dev, &memmap->rxfreebufstart.next, BUF_PTR(rx_desc) | BUFF_STATUS_EMPTY);
  1522. for (buff_count = 0; buff_count < BUFN4_SIZE-1; buff_count++) {
  1523. wr_mem (dev, &rx_desc->next, BUF_PTR(rx_desc+1) | BUFF_STATUS_EMPTY);
  1524. rx_desc++;
  1525. }
  1526. wr_mem (dev, &rx_desc->next, BUF_PTR(&memmap->rxfreebufend) | BUFF_STATUS_EMPTY);
  1527. // Initialise the receive free buffer count
  1528. wr_regw (dev, RX_FREE_BUFFER_COUNT_OFF, BUFN4_SIZE);
  1529. // Initialize Horizons registers
  1530. // TX config
  1531. wr_regw (dev, TX_CONFIG_OFF,
  1532. ABR_ROUND_ROBIN | TX_NORMAL_OPERATION | DRVR_DRVRBAR_ENABLE);
  1533. // RX config. Use 10-x VC bits, x VP bits, non user cells in channel 0.
  1534. wr_regw (dev, RX_CONFIG_OFF,
  1535. DISCARD_UNUSED_VPI_VCI_BITS_SET | NON_USER_CELLS_IN_ONE_CHANNEL | vpi_bits);
  1536. // RX line config
  1537. wr_regw (dev, RX_LINE_CONFIG_OFF,
  1538. LOCK_DETECT_ENABLE | FREQUENCY_DETECT_ENABLE | GXTALOUT_SELECT_DIV4);
  1539. // Set the max AAL5 cell count to be just enough to contain the
  1540. // largest AAL5 frame that the user wants to receive
  1541. wr_regw (dev, MAX_AAL5_CELL_COUNT_OFF,
  1542. DIV_ROUND_UP(max_rx_size + ATM_AAL5_TRAILER, ATM_CELL_PAYLOAD));
  1543. // Enable receive
  1544. wr_regw (dev, RX_CONFIG_OFF, rd_regw (dev, RX_CONFIG_OFF) | RX_ENABLE);
  1545. printk (" control");
  1546. // Drive the OE of the LEDs then turn the green LED on
  1547. ctrl |= GREEN_LED_OE | YELLOW_LED_OE | GREEN_LED | YELLOW_LED;
  1548. wr_regl (dev, CONTROL_0_REG, ctrl);
  1549. // Test for a 155-capable card
  1550. if (onefivefive) {
  1551. // Select 155 mode... make this a choice (or: how do we detect
  1552. // external line speed and switch?)
  1553. ctrl |= ATM_LAYER_SELECT;
  1554. wr_regl (dev, CONTROL_0_REG, ctrl);
  1555. // test SUNI-lite vs SAMBA
  1556. // Register 0x00 in the SUNI will have some of bits 3-7 set, and
  1557. // they will always be zero for the SAMBA. Ha! Bloody hardware
  1558. // engineers. It'll never work.
  1559. if (rd_framer (dev, 0) & 0x00f0) {
  1560. // SUNI
  1561. printk (" SUNI");
  1562. // Reset, just in case
  1563. wr_framer (dev, 0x00, 0x0080);
  1564. wr_framer (dev, 0x00, 0x0000);
  1565. // Configure transmit FIFO
  1566. wr_framer (dev, 0x63, rd_framer (dev, 0x63) | 0x0002);
  1567. // Set line timed mode
  1568. wr_framer (dev, 0x05, rd_framer (dev, 0x05) | 0x0001);
  1569. } else {
  1570. // SAMBA
  1571. printk (" SAMBA");
  1572. // Reset, just in case
  1573. wr_framer (dev, 0, rd_framer (dev, 0) | 0x0001);
  1574. wr_framer (dev, 0, rd_framer (dev, 0) &~ 0x0001);
  1575. // Turn off diagnostic loopback and enable line-timed mode
  1576. wr_framer (dev, 0, 0x0002);
  1577. // Turn on transmit outputs
  1578. wr_framer (dev, 2, 0x0B80);
  1579. }
  1580. } else {
  1581. // Select 25 mode
  1582. ctrl &= ~ATM_LAYER_SELECT;
  1583. // Madge B154 setup
  1584. // none required?
  1585. }
  1586. printk (" LEDs");
  1587. GREEN_LED_ON(dev);
  1588. YELLOW_LED_ON(dev);
  1589. printk (" ESI=");
  1590. {
  1591. u16 b = 0;
  1592. int i;
  1593. u8 * esi = dev->atm_dev->esi;
  1594. // in the card I have, EEPROM
  1595. // addresses 0, 1, 2 contain 0
  1596. // addresess 5, 6 etc. contain ffff
  1597. // NB: Madge prefix is 00 00 f6 (which is 00 00 6f in Ethernet bit order)
  1598. // the read_bia routine gets the BIA in Ethernet bit order
  1599. for (i=0; i < ESI_LEN; ++i) {
  1600. if (i % 2 == 0)
  1601. b = read_bia (dev, i/2 + 2);
  1602. else
  1603. b = b >> 8;
  1604. esi[i] = b & 0xFF;
  1605. printk ("%02x", esi[i]);
  1606. }
  1607. }
  1608. // Enable RX_Q and ?X_COMPLETE interrupts only
  1609. wr_regl (dev, INT_ENABLE_REG_OFF, INTERESTING_INTERRUPTS);
  1610. printk (" IRQ on");
  1611. printk (".\n");
  1612. return onefivefive;
  1613. }
  1614. /********** check max_sdu **********/
  1615. static int check_max_sdu (hrz_aal aal, struct atm_trafprm * tp, unsigned int max_frame_size) {
  1616. PRINTD (DBG_FLOW|DBG_QOS, "check_max_sdu");
  1617. switch (aal) {
  1618. case aal0:
  1619. if (!(tp->max_sdu)) {
  1620. PRINTD (DBG_QOS, "defaulting max_sdu");
  1621. tp->max_sdu = ATM_AAL0_SDU;
  1622. } else if (tp->max_sdu != ATM_AAL0_SDU) {
  1623. PRINTD (DBG_QOS|DBG_ERR, "rejecting max_sdu");
  1624. return -EINVAL;
  1625. }
  1626. break;
  1627. case aal34:
  1628. if (tp->max_sdu == 0 || tp->max_sdu > ATM_MAX_AAL34_PDU) {
  1629. PRINTD (DBG_QOS, "%sing max_sdu", tp->max_sdu ? "capp" : "default");
  1630. tp->max_sdu = ATM_MAX_AAL34_PDU;
  1631. }
  1632. break;
  1633. case aal5:
  1634. if (tp->max_sdu == 0 || tp->max_sdu > max_frame_size) {
  1635. PRINTD (DBG_QOS, "%sing max_sdu", tp->max_sdu ? "capp" : "default");
  1636. tp->max_sdu = max_frame_size;
  1637. }
  1638. break;
  1639. }
  1640. return 0;
  1641. }
  1642. /********** check pcr **********/
  1643. // something like this should be part of ATM Linux
  1644. static int atm_pcr_check (struct atm_trafprm * tp, unsigned int pcr) {
  1645. // we are assuming non-UBR, and non-special values of pcr
  1646. if (tp->min_pcr == ATM_MAX_PCR)
  1647. PRINTD (DBG_QOS, "luser gave min_pcr = ATM_MAX_PCR");
  1648. else if (tp->min_pcr < 0)
  1649. PRINTD (DBG_QOS, "luser gave negative min_pcr");
  1650. else if (tp->min_pcr && tp->min_pcr > pcr)
  1651. PRINTD (DBG_QOS, "pcr less than min_pcr");
  1652. else
  1653. // !! max_pcr = UNSPEC (0) is equivalent to max_pcr = MAX (-1)
  1654. // easier to #define ATM_MAX_PCR 0 and have all rates unsigned?
  1655. // [this would get rid of next two conditionals]
  1656. if ((0) && tp->max_pcr == ATM_MAX_PCR)
  1657. PRINTD (DBG_QOS, "luser gave max_pcr = ATM_MAX_PCR");
  1658. else if ((tp->max_pcr != ATM_MAX_PCR) && tp->max_pcr < 0)
  1659. PRINTD (DBG_QOS, "luser gave negative max_pcr");
  1660. else if (tp->max_pcr && tp->max_pcr != ATM_MAX_PCR && tp->max_pcr < pcr)
  1661. PRINTD (DBG_QOS, "pcr greater than max_pcr");
  1662. else {
  1663. // each limit unspecified or not violated
  1664. PRINTD (DBG_QOS, "xBR(pcr) OK");
  1665. return 0;
  1666. }
  1667. PRINTD (DBG_QOS, "pcr=%u, tp: min_pcr=%d, pcr=%d, max_pcr=%d",
  1668. pcr, tp->min_pcr, tp->pcr, tp->max_pcr);
  1669. return -EINVAL;
  1670. }
  1671. /********** open VC **********/
  1672. static int hrz_open (struct atm_vcc *atm_vcc)
  1673. {
  1674. int error;
  1675. u16 channel;
  1676. struct atm_qos * qos;
  1677. struct atm_trafprm * txtp;
  1678. struct atm_trafprm * rxtp;
  1679. hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
  1680. hrz_vcc vcc;
  1681. hrz_vcc * vccp; // allocated late
  1682. short vpi = atm_vcc->vpi;
  1683. int vci = atm_vcc->vci;
  1684. PRINTD (DBG_FLOW|DBG_VCC, "hrz_open %x %x", vpi, vci);
  1685. #ifdef ATM_VPI_UNSPEC
  1686. // UNSPEC is deprecated, remove this code eventually
  1687. if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) {
  1688. PRINTK (KERN_WARNING, "rejecting open with unspecified VPI/VCI (deprecated)");
  1689. return -EINVAL;
  1690. }
  1691. #endif
  1692. error = vpivci_to_channel (&channel, vpi, vci);
  1693. if (error) {
  1694. PRINTD (DBG_WARN|DBG_VCC, "VPI/VCI out of range: %hd/%d", vpi, vci);
  1695. return error;
  1696. }
  1697. vcc.channel = channel;
  1698. // max speed for the moment
  1699. vcc.tx_rate = 0x0;
  1700. qos = &atm_vcc->qos;
  1701. // check AAL and remember it
  1702. switch (qos->aal) {
  1703. case ATM_AAL0:
  1704. // we would if it were 48 bytes and not 52!
  1705. PRINTD (DBG_QOS|DBG_VCC, "AAL0");
  1706. vcc.aal = aal0;
  1707. break;
  1708. case ATM_AAL34:
  1709. // we would if I knew how do the SAR!
  1710. PRINTD (DBG_QOS|DBG_VCC, "AAL3/4");
  1711. vcc.aal = aal34;
  1712. break;
  1713. case ATM_AAL5:
  1714. PRINTD (DBG_QOS|DBG_VCC, "AAL5");
  1715. vcc.aal = aal5;
  1716. break;
  1717. default:
  1718. PRINTD (DBG_QOS|DBG_VCC, "Bad AAL!");
  1719. return -EINVAL;
  1720. }
  1721. // TX traffic parameters
  1722. // there are two, interrelated problems here: 1. the reservation of
  1723. // PCR is not a binary choice, we are given bounds and/or a
  1724. // desirable value; 2. the device is only capable of certain values,
  1725. // most of which are not integers. It is almost certainly acceptable
  1726. // to be off by a maximum of 1 to 10 cps.
  1727. // Pragmatic choice: always store an integral PCR as that which has
  1728. // been allocated, even if we allocate a little (or a lot) less,
  1729. // after rounding. The actual allocation depends on what we can
  1730. // manage with our rate selection algorithm. The rate selection
  1731. // algorithm is given an integral PCR and a tolerance and told
  1732. // whether it should round the value up or down if the tolerance is
  1733. // exceeded; it returns: a) the actual rate selected (rounded up to
  1734. // the nearest integer), b) a bit pattern to feed to the timer
  1735. // register, and c) a failure value if no applicable rate exists.
  1736. // Part of the job is done by atm_pcr_goal which gives us a PCR
  1737. // specification which says: EITHER grab the maximum available PCR
  1738. // (and perhaps a lower bound which we musn't pass), OR grab this
  1739. // amount, rounding down if you have to (and perhaps a lower bound
  1740. // which we musn't pass) OR grab this amount, rounding up if you
  1741. // have to (and perhaps an upper bound which we musn't pass). If any
  1742. // bounds ARE passed we fail. Note that rounding is only rounding to
  1743. // match device limitations, we do not round down to satisfy
  1744. // bandwidth availability even if this would not violate any given
  1745. // lower bound.
  1746. // Note: telephony = 64kb/s = 48 byte cell payload @ 500/3 cells/s
  1747. // (say) so this is not even a binary fixpoint cell rate (but this
  1748. // device can do it). To avoid this sort of hassle we use a
  1749. // tolerance parameter (currently fixed at 10 cps).
  1750. PRINTD (DBG_QOS, "TX:");
  1751. txtp = &qos->txtp;
  1752. // set up defaults for no traffic
  1753. vcc.tx_rate = 0;
  1754. // who knows what would actually happen if you try and send on this?
  1755. vcc.tx_xbr_bits = IDLE_RATE_TYPE;
  1756. vcc.tx_pcr_bits = CLOCK_DISABLE;
  1757. #if 0
  1758. vcc.tx_scr_bits = CLOCK_DISABLE;
  1759. vcc.tx_bucket_bits = 0;
  1760. #endif
  1761. if (txtp->traffic_class != ATM_NONE) {
  1762. error = check_max_sdu (vcc.aal, txtp, max_tx_size);
  1763. if (error) {
  1764. PRINTD (DBG_QOS, "TX max_sdu check failed");
  1765. return error;
  1766. }
  1767. switch (txtp->traffic_class) {
  1768. case ATM_UBR: {
  1769. // we take "the PCR" as a rate-cap
  1770. // not reserved
  1771. vcc.tx_rate = 0;
  1772. make_rate (dev, 1<<30, round_nearest, &vcc.tx_pcr_bits, NULL);
  1773. vcc.tx_xbr_bits = ABR_RATE_TYPE;
  1774. break;
  1775. }
  1776. #if 0
  1777. case ATM_ABR: {
  1778. // reserve min, allow up to max
  1779. vcc.tx_rate = 0; // ?
  1780. make_rate (dev, 1<<30, round_nearest, &vcc.tx_pcr_bits, 0);
  1781. vcc.tx_xbr_bits = ABR_RATE_TYPE;
  1782. break;
  1783. }
  1784. #endif
  1785. case ATM_CBR: {
  1786. int pcr = atm_pcr_goal (txtp);
  1787. rounding r;
  1788. if (!pcr) {
  1789. // down vs. up, remaining bandwidth vs. unlimited bandwidth!!
  1790. // should really have: once someone gets unlimited bandwidth
  1791. // that no more non-UBR channels can be opened until the
  1792. // unlimited one closes?? For the moment, round_down means
  1793. // greedy people actually get something and not nothing
  1794. r = round_down;
  1795. // slight race (no locking) here so we may get -EAGAIN
  1796. // later; the greedy bastards would deserve it :)
  1797. PRINTD (DBG_QOS, "snatching all remaining TX bandwidth");
  1798. pcr = dev->tx_avail;
  1799. } else if (pcr < 0) {
  1800. r = round_down;
  1801. pcr = -pcr;
  1802. } else {
  1803. r = round_up;
  1804. }
  1805. error = make_rate_with_tolerance (dev, pcr, r, 10,
  1806. &vcc.tx_pcr_bits, &vcc.tx_rate);
  1807. if (error) {
  1808. PRINTD (DBG_QOS, "could not make rate from TX PCR");
  1809. return error;
  1810. }
  1811. // not really clear what further checking is needed
  1812. error = atm_pcr_check (txtp, vcc.tx_rate);
  1813. if (error) {
  1814. PRINTD (DBG_QOS, "TX PCR failed consistency check");
  1815. return error;
  1816. }
  1817. vcc.tx_xbr_bits = CBR_RATE_TYPE;
  1818. break;
  1819. }
  1820. #if 0
  1821. case ATM_VBR: {
  1822. int pcr = atm_pcr_goal (txtp);
  1823. // int scr = atm_scr_goal (txtp);
  1824. int scr = pcr/2; // just for fun
  1825. unsigned int mbs = 60; // just for fun
  1826. rounding pr;
  1827. rounding sr;
  1828. unsigned int bucket;
  1829. if (!pcr) {
  1830. pr = round_nearest;
  1831. pcr = 1<<30;
  1832. } else if (pcr < 0) {
  1833. pr = round_down;
  1834. pcr = -pcr;
  1835. } else {
  1836. pr = round_up;
  1837. }
  1838. error = make_rate_with_tolerance (dev, pcr, pr, 10,
  1839. &vcc.tx_pcr_bits, 0);
  1840. if (!scr) {
  1841. // see comments for PCR with CBR above
  1842. sr = round_down;
  1843. // slight race (no locking) here so we may get -EAGAIN
  1844. // later; the greedy bastards would deserve it :)
  1845. PRINTD (DBG_QOS, "snatching all remaining TX bandwidth");
  1846. scr = dev->tx_avail;
  1847. } else if (scr < 0) {
  1848. sr = round_down;
  1849. scr = -scr;
  1850. } else {
  1851. sr = round_up;
  1852. }
  1853. error = make_rate_with_tolerance (dev, scr, sr, 10,
  1854. &vcc.tx_scr_bits, &vcc.tx_rate);
  1855. if (error) {
  1856. PRINTD (DBG_QOS, "could not make rate from TX SCR");
  1857. return error;
  1858. }
  1859. // not really clear what further checking is needed
  1860. // error = atm_scr_check (txtp, vcc.tx_rate);
  1861. if (error) {
  1862. PRINTD (DBG_QOS, "TX SCR failed consistency check");
  1863. return error;
  1864. }
  1865. // bucket calculations (from a piece of paper...) cell bucket
  1866. // capacity must be largest integer smaller than m(p-s)/p + 1
  1867. // where m = max burst size, p = pcr, s = scr
  1868. bucket = mbs*(pcr-scr)/pcr;
  1869. if (bucket*pcr != mbs*(pcr-scr))
  1870. bucket += 1;
  1871. if (bucket > BUCKET_MAX_SIZE) {
  1872. PRINTD (DBG_QOS, "shrinking bucket from %u to %u",
  1873. bucket, BUCKET_MAX_SIZE);
  1874. bucket = BUCKET_MAX_SIZE;
  1875. }
  1876. vcc.tx_xbr_bits = VBR_RATE_TYPE;
  1877. vcc.tx_bucket_bits = bucket;
  1878. break;
  1879. }
  1880. #endif
  1881. default: {
  1882. PRINTD (DBG_QOS, "unsupported TX traffic class");
  1883. return -EINVAL;
  1884. }
  1885. }
  1886. }
  1887. // RX traffic parameters
  1888. PRINTD (DBG_QOS, "RX:");
  1889. rxtp = &qos->rxtp;
  1890. // set up defaults for no traffic
  1891. vcc.rx_rate = 0;
  1892. if (rxtp->traffic_class != ATM_NONE) {
  1893. error = check_max_sdu (vcc.aal, rxtp, max_rx_size);
  1894. if (error) {
  1895. PRINTD (DBG_QOS, "RX max_sdu check failed");
  1896. return error;
  1897. }
  1898. switch (rxtp->traffic_class) {
  1899. case ATM_UBR: {
  1900. // not reserved
  1901. break;
  1902. }
  1903. #if 0
  1904. case ATM_ABR: {
  1905. // reserve min
  1906. vcc.rx_rate = 0; // ?
  1907. break;
  1908. }
  1909. #endif
  1910. case ATM_CBR: {
  1911. int pcr = atm_pcr_goal (rxtp);
  1912. if (!pcr) {
  1913. // slight race (no locking) here so we may get -EAGAIN
  1914. // later; the greedy bastards would deserve it :)
  1915. PRINTD (DBG_QOS, "snatching all remaining RX bandwidth");
  1916. pcr = dev->rx_avail;
  1917. } else if (pcr < 0) {
  1918. pcr = -pcr;
  1919. }
  1920. vcc.rx_rate = pcr;
  1921. // not really clear what further checking is needed
  1922. error = atm_pcr_check (rxtp, vcc.rx_rate);
  1923. if (error) {
  1924. PRINTD (DBG_QOS, "RX PCR failed consistency check");
  1925. return error;
  1926. }
  1927. break;
  1928. }
  1929. #if 0
  1930. case ATM_VBR: {
  1931. // int scr = atm_scr_goal (rxtp);
  1932. int scr = 1<<16; // just for fun
  1933. if (!scr) {
  1934. // slight race (no locking) here so we may get -EAGAIN
  1935. // later; the greedy bastards would deserve it :)
  1936. PRINTD (DBG_QOS, "snatching all remaining RX bandwidth");
  1937. scr = dev->rx_avail;
  1938. } else if (scr < 0) {
  1939. scr = -scr;
  1940. }
  1941. vcc.rx_rate = scr;
  1942. // not really clear what further checking is needed
  1943. // error = atm_scr_check (rxtp, vcc.rx_rate);
  1944. if (error) {
  1945. PRINTD (DBG_QOS, "RX SCR failed consistency check");
  1946. return error;
  1947. }
  1948. break;
  1949. }
  1950. #endif
  1951. default: {
  1952. PRINTD (DBG_QOS, "unsupported RX traffic class");
  1953. return -EINVAL;
  1954. }
  1955. }
  1956. }
  1957. // late abort useful for diagnostics
  1958. if (vcc.aal != aal5) {
  1959. PRINTD (DBG_QOS, "AAL not supported");
  1960. return -EINVAL;
  1961. }
  1962. // get space for our vcc stuff and copy parameters into it
  1963. vccp = kmalloc (sizeof(hrz_vcc), GFP_KERNEL);
  1964. if (!vccp) {
  1965. PRINTK (KERN_ERR, "out of memory!");
  1966. return -ENOMEM;
  1967. }
  1968. *vccp = vcc;
  1969. // clear error and grab cell rate resource lock
  1970. error = 0;
  1971. spin_lock (&dev->rate_lock);
  1972. if (vcc.tx_rate > dev->tx_avail) {
  1973. PRINTD (DBG_QOS, "not enough TX PCR left");
  1974. error = -EAGAIN;
  1975. }
  1976. if (vcc.rx_rate > dev->rx_avail) {
  1977. PRINTD (DBG_QOS, "not enough RX PCR left");
  1978. error = -EAGAIN;
  1979. }
  1980. if (!error) {
  1981. // really consume cell rates
  1982. dev->tx_avail -= vcc.tx_rate;
  1983. dev->rx_avail -= vcc.rx_rate;
  1984. PRINTD (DBG_QOS|DBG_VCC, "reserving %u TX PCR and %u RX PCR",
  1985. vcc.tx_rate, vcc.rx_rate);
  1986. }
  1987. // release lock and exit on error
  1988. spin_unlock (&dev->rate_lock);
  1989. if (error) {
  1990. PRINTD (DBG_QOS|DBG_VCC, "insufficient cell rate resources");
  1991. kfree (vccp);
  1992. return error;
  1993. }
  1994. // this is "immediately before allocating the connection identifier
  1995. // in hardware" - so long as the next call does not fail :)
  1996. set_bit(ATM_VF_ADDR,&atm_vcc->flags);
  1997. // any errors here are very serious and should never occur
  1998. if (rxtp->traffic_class != ATM_NONE) {
  1999. if (dev->rxer[channel]) {
  2000. PRINTD (DBG_ERR|DBG_VCC, "VC already open for RX");
  2001. error = -EBUSY;
  2002. }
  2003. if (!error)
  2004. error = hrz_open_rx (dev, channel);
  2005. if (error) {
  2006. kfree (vccp);
  2007. return error;
  2008. }
  2009. // this link allows RX frames through
  2010. dev->rxer[channel] = atm_vcc;
  2011. }
  2012. // success, set elements of atm_vcc
  2013. atm_vcc->dev_data = (void *) vccp;
  2014. // indicate readiness
  2015. set_bit(ATM_VF_READY,&atm_vcc->flags);
  2016. return 0;
  2017. }
  2018. /********** close VC **********/
  2019. static void hrz_close (struct atm_vcc * atm_vcc) {
  2020. hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
  2021. hrz_vcc * vcc = HRZ_VCC(atm_vcc);
  2022. u16 channel = vcc->channel;
  2023. PRINTD (DBG_VCC|DBG_FLOW, "hrz_close");
  2024. // indicate unreadiness
  2025. clear_bit(ATM_VF_READY,&atm_vcc->flags);
  2026. if (atm_vcc->qos.txtp.traffic_class != ATM_NONE) {
  2027. unsigned int i;
  2028. // let any TX on this channel that has started complete
  2029. // no restart, just keep trying
  2030. while (tx_hold (dev))
  2031. ;
  2032. // remove record of any tx_channel having been setup for this channel
  2033. for (i = 0; i < TX_CHANS; ++i)
  2034. if (dev->tx_channel_record[i] == channel) {
  2035. dev->tx_channel_record[i] = -1;
  2036. break;
  2037. }
  2038. if (dev->last_vc == channel)
  2039. dev->tx_last = -1;
  2040. tx_release (dev);
  2041. }
  2042. if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
  2043. // disable RXing - it tries quite hard
  2044. hrz_close_rx (dev, channel);
  2045. // forget the vcc - no more skbs will be pushed
  2046. if (atm_vcc != dev->rxer[channel])
  2047. PRINTK (KERN_ERR, "%s atm_vcc=%p rxer[channel]=%p",
  2048. "arghhh! we're going to die!",
  2049. atm_vcc, dev->rxer[channel]);
  2050. dev->rxer[channel] = NULL;
  2051. }
  2052. // atomically release our rate reservation
  2053. spin_lock (&dev->rate_lock);
  2054. PRINTD (DBG_QOS|DBG_VCC, "releasing %u TX PCR and %u RX PCR",
  2055. vcc->tx_rate, vcc->rx_rate);
  2056. dev->tx_avail += vcc->tx_rate;
  2057. dev->rx_avail += vcc->rx_rate;
  2058. spin_unlock (&dev->rate_lock);
  2059. // free our structure
  2060. kfree (vcc);
  2061. // say the VPI/VCI is free again
  2062. clear_bit(ATM_VF_ADDR,&atm_vcc->flags);
  2063. }
  2064. #if 0
  2065. static int hrz_getsockopt (struct atm_vcc * atm_vcc, int level, int optname,
  2066. void *optval, int optlen) {
  2067. hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
  2068. PRINTD (DBG_FLOW|DBG_VCC, "hrz_getsockopt");
  2069. switch (level) {
  2070. case SOL_SOCKET:
  2071. switch (optname) {
  2072. // case SO_BCTXOPT:
  2073. // break;
  2074. // case SO_BCRXOPT:
  2075. // break;
  2076. default:
  2077. return -ENOPROTOOPT;
  2078. };
  2079. break;
  2080. }
  2081. return -EINVAL;
  2082. }
  2083. static int hrz_setsockopt (struct atm_vcc * atm_vcc, int level, int optname,
  2084. void *optval, unsigned int optlen) {
  2085. hrz_dev * dev = HRZ_DEV(atm_vcc->dev);
  2086. PRINTD (DBG_FLOW|DBG_VCC, "hrz_setsockopt");
  2087. switch (level) {
  2088. case SOL_SOCKET:
  2089. switch (optname) {
  2090. // case SO_BCTXOPT:
  2091. // break;
  2092. // case SO_BCRXOPT:
  2093. // break;
  2094. default:
  2095. return -ENOPROTOOPT;
  2096. };
  2097. break;
  2098. }
  2099. return -EINVAL;
  2100. }
  2101. #endif
  2102. #if 0
  2103. static int hrz_ioctl (struct atm_dev * atm_dev, unsigned int cmd, void *arg) {
  2104. hrz_dev * dev = HRZ_DEV(atm_dev);
  2105. PRINTD (DBG_FLOW, "hrz_ioctl");
  2106. return -1;
  2107. }
  2108. unsigned char hrz_phy_get (struct atm_dev * atm_dev, unsigned long addr) {
  2109. hrz_dev * dev = HRZ_DEV(atm_dev);
  2110. PRINTD (DBG_FLOW, "hrz_phy_get");
  2111. return 0;
  2112. }
  2113. static void hrz_phy_put (struct atm_dev * atm_dev, unsigned char value,
  2114. unsigned long addr) {
  2115. hrz_dev * dev = HRZ_DEV(atm_dev);
  2116. PRINTD (DBG_FLOW, "hrz_phy_put");
  2117. }
  2118. static int hrz_change_qos (struct atm_vcc * atm_vcc, struct atm_qos *qos, int flgs) {
  2119. hrz_dev * dev = HRZ_DEV(vcc->dev);
  2120. PRINTD (DBG_FLOW, "hrz_change_qos");
  2121. return -1;
  2122. }
  2123. #endif
  2124. /********** proc file contents **********/
  2125. static int hrz_proc_read (struct atm_dev * atm_dev, loff_t * pos, char * page) {
  2126. hrz_dev * dev = HRZ_DEV(atm_dev);
  2127. int left = *pos;
  2128. PRINTD (DBG_FLOW, "hrz_proc_read");
  2129. /* more diagnostics here? */
  2130. #if 0
  2131. if (!left--) {
  2132. unsigned int count = sprintf (page, "vbr buckets:");
  2133. unsigned int i;
  2134. for (i = 0; i < TX_CHANS; ++i)
  2135. count += sprintf (page, " %u/%u",
  2136. query_tx_channel_config (dev, i, BUCKET_FULLNESS_ACCESS),
  2137. query_tx_channel_config (dev, i, BUCKET_CAPACITY_ACCESS));
  2138. count += sprintf (page+count, ".\n");
  2139. return count;
  2140. }
  2141. #endif
  2142. if (!left--)
  2143. return sprintf (page,
  2144. "cells: TX %lu, RX %lu, HEC errors %lu, unassigned %lu.\n",
  2145. dev->tx_cell_count, dev->rx_cell_count,
  2146. dev->hec_error_count, dev->unassigned_cell_count);
  2147. if (!left--)
  2148. return sprintf (page,
  2149. "free cell buffers: TX %hu, RX %hu+%hu.\n",
  2150. rd_regw (dev, TX_FREE_BUFFER_COUNT_OFF),
  2151. rd_regw (dev, RX_FREE_BUFFER_COUNT_OFF),
  2152. dev->noof_spare_buffers);
  2153. if (!left--)
  2154. return sprintf (page,
  2155. "cps remaining: TX %u, RX %u\n",
  2156. dev->tx_avail, dev->rx_avail);
  2157. return 0;
  2158. }
  2159. static const struct atmdev_ops hrz_ops = {
  2160. .open = hrz_open,
  2161. .close = hrz_close,
  2162. .send = hrz_send,
  2163. .proc_read = hrz_proc_read,
  2164. .owner = THIS_MODULE,
  2165. };
  2166. static int hrz_probe(struct pci_dev *pci_dev,
  2167. const struct pci_device_id *pci_ent)
  2168. {
  2169. hrz_dev * dev;
  2170. int err = 0;
  2171. // adapter slot free, read resources from PCI configuration space
  2172. u32 iobase = pci_resource_start (pci_dev, 0);
  2173. u32 * membase = bus_to_virt (pci_resource_start (pci_dev, 1));
  2174. unsigned int irq;
  2175. unsigned char lat;
  2176. PRINTD (DBG_FLOW, "hrz_probe");
  2177. if (pci_enable_device(pci_dev))
  2178. return -EINVAL;
  2179. /* XXX DEV_LABEL is a guess */
  2180. if (!request_region(iobase, HRZ_IO_EXTENT, DEV_LABEL)) {
  2181. err = -EINVAL;
  2182. goto out_disable;
  2183. }
  2184. dev = kzalloc(sizeof(hrz_dev), GFP_KERNEL);
  2185. if (!dev) {
  2186. // perhaps we should be nice: deregister all adapters and abort?
  2187. PRINTD(DBG_ERR, "out of memory");
  2188. err = -ENOMEM;
  2189. goto out_release;
  2190. }
  2191. pci_set_drvdata(pci_dev, dev);
  2192. // grab IRQ and install handler - move this someplace more sensible
  2193. irq = pci_dev->irq;
  2194. if (request_irq(irq,
  2195. interrupt_handler,
  2196. IRQF_SHARED, /* irqflags guess */
  2197. DEV_LABEL, /* name guess */
  2198. dev)) {
  2199. PRINTD(DBG_WARN, "request IRQ failed!");
  2200. err = -EINVAL;
  2201. goto out_free;
  2202. }
  2203. PRINTD(DBG_INFO, "found Madge ATM adapter (hrz) at: IO %x, IRQ %u, MEM %p",
  2204. iobase, irq, membase);
  2205. dev->atm_dev = atm_dev_register(DEV_LABEL, &pci_dev->dev, &hrz_ops, -1,
  2206. NULL);
  2207. if (!(dev->atm_dev)) {
  2208. PRINTD(DBG_ERR, "failed to register Madge ATM adapter");
  2209. err = -EINVAL;
  2210. goto out_free_irq;
  2211. }
  2212. PRINTD(DBG_INFO, "registered Madge ATM adapter (no. %d) (%p) at %p",
  2213. dev->atm_dev->number, dev, dev->atm_dev);
  2214. dev->atm_dev->dev_data = (void *) dev;
  2215. dev->pci_dev = pci_dev;
  2216. // enable bus master accesses
  2217. pci_set_master(pci_dev);
  2218. // frobnicate latency (upwards, usually)
  2219. pci_read_config_byte(pci_dev, PCI_LATENCY_TIMER, &lat);
  2220. if (pci_lat) {
  2221. PRINTD(DBG_INFO, "%s PCI latency timer from %hu to %hu",
  2222. "changing", lat, pci_lat);
  2223. pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, pci_lat);
  2224. } else if (lat < MIN_PCI_LATENCY) {
  2225. PRINTK(KERN_INFO, "%s PCI latency timer from %hu to %hu",
  2226. "increasing", lat, MIN_PCI_LATENCY);
  2227. pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, MIN_PCI_LATENCY);
  2228. }
  2229. dev->iobase = iobase;
  2230. dev->irq = irq;
  2231. dev->membase = membase;
  2232. dev->rx_q_entry = dev->rx_q_reset = &memmap->rx_q_entries[0];
  2233. dev->rx_q_wrap = &memmap->rx_q_entries[RX_CHANS-1];
  2234. // these next three are performance hacks
  2235. dev->last_vc = -1;
  2236. dev->tx_last = -1;
  2237. dev->tx_idle = 0;
  2238. dev->tx_regions = 0;
  2239. dev->tx_bytes = 0;
  2240. dev->tx_skb = NULL;
  2241. dev->tx_iovec = NULL;
  2242. dev->tx_cell_count = 0;
  2243. dev->rx_cell_count = 0;
  2244. dev->hec_error_count = 0;
  2245. dev->unassigned_cell_count = 0;
  2246. dev->noof_spare_buffers = 0;
  2247. {
  2248. unsigned int i;
  2249. for (i = 0; i < TX_CHANS; ++i)
  2250. dev->tx_channel_record[i] = -1;
  2251. }
  2252. dev->flags = 0;
  2253. // Allocate cell rates and remember ASIC version
  2254. // Fibre: ATM_OC3_PCR = 1555200000/8/270*260/53 - 29/53
  2255. // Copper: (WRONG) we want 6 into the above, close to 25Mb/s
  2256. // Copper: (plagarise!) 25600000/8/270*260/53 - n/53
  2257. if (hrz_init(dev)) {
  2258. // to be really pedantic, this should be ATM_OC3c_PCR
  2259. dev->tx_avail = ATM_OC3_PCR;
  2260. dev->rx_avail = ATM_OC3_PCR;
  2261. set_bit(ultra, &dev->flags); // NOT "|= ultra" !
  2262. } else {
  2263. dev->tx_avail = ((25600000/8)*26)/(27*53);
  2264. dev->rx_avail = ((25600000/8)*26)/(27*53);
  2265. PRINTD(DBG_WARN, "Buggy ASIC: no TX bus-mastering.");
  2266. }
  2267. // rate changes spinlock
  2268. spin_lock_init(&dev->rate_lock);
  2269. // on-board memory access spinlock; we want atomic reads and
  2270. // writes to adapter memory (handles IRQ and SMP)
  2271. spin_lock_init(&dev->mem_lock);
  2272. init_waitqueue_head(&dev->tx_queue);
  2273. // vpi in 0..4, vci in 6..10
  2274. dev->atm_dev->ci_range.vpi_bits = vpi_bits;
  2275. dev->atm_dev->ci_range.vci_bits = 10-vpi_bits;
  2276. setup_timer(&dev->housekeeping, do_housekeeping, (unsigned long) dev);
  2277. mod_timer(&dev->housekeeping, jiffies);
  2278. out:
  2279. return err;
  2280. out_free_irq:
  2281. free_irq(irq, dev);
  2282. out_free:
  2283. kfree(dev);
  2284. out_release:
  2285. release_region(iobase, HRZ_IO_EXTENT);
  2286. out_disable:
  2287. pci_disable_device(pci_dev);
  2288. goto out;
  2289. }
  2290. static void hrz_remove_one(struct pci_dev *pci_dev)
  2291. {
  2292. hrz_dev *dev;
  2293. dev = pci_get_drvdata(pci_dev);
  2294. PRINTD(DBG_INFO, "closing %p (atm_dev = %p)", dev, dev->atm_dev);
  2295. del_timer_sync(&dev->housekeeping);
  2296. hrz_reset(dev);
  2297. atm_dev_deregister(dev->atm_dev);
  2298. free_irq(dev->irq, dev);
  2299. release_region(dev->iobase, HRZ_IO_EXTENT);
  2300. kfree(dev);
  2301. pci_disable_device(pci_dev);
  2302. }
  2303. static void __init hrz_check_args (void) {
  2304. #ifdef DEBUG_HORIZON
  2305. PRINTK (KERN_NOTICE, "debug bitmap is %hx", debug &= DBG_MASK);
  2306. #else
  2307. if (debug)
  2308. PRINTK (KERN_NOTICE, "no debug support in this image");
  2309. #endif
  2310. if (vpi_bits > HRZ_MAX_VPI)
  2311. PRINTK (KERN_ERR, "vpi_bits has been limited to %hu",
  2312. vpi_bits = HRZ_MAX_VPI);
  2313. if (max_tx_size < 0 || max_tx_size > TX_AAL5_LIMIT)
  2314. PRINTK (KERN_NOTICE, "max_tx_size has been limited to %hu",
  2315. max_tx_size = TX_AAL5_LIMIT);
  2316. if (max_rx_size < 0 || max_rx_size > RX_AAL5_LIMIT)
  2317. PRINTK (KERN_NOTICE, "max_rx_size has been limited to %hu",
  2318. max_rx_size = RX_AAL5_LIMIT);
  2319. return;
  2320. }
  2321. MODULE_AUTHOR(maintainer_string);
  2322. MODULE_DESCRIPTION(description_string);
  2323. MODULE_LICENSE("GPL");
  2324. module_param(debug, ushort, 0644);
  2325. module_param(vpi_bits, ushort, 0);
  2326. module_param(max_tx_size, int, 0);
  2327. module_param(max_rx_size, int, 0);
  2328. module_param(pci_lat, byte, 0);
  2329. MODULE_PARM_DESC(debug, "debug bitmap, see .h file");
  2330. MODULE_PARM_DESC(vpi_bits, "number of bits (0..4) to allocate to VPIs");
  2331. MODULE_PARM_DESC(max_tx_size, "maximum size of TX AAL5 frames");
  2332. MODULE_PARM_DESC(max_rx_size, "maximum size of RX AAL5 frames");
  2333. MODULE_PARM_DESC(pci_lat, "PCI latency in bus cycles");
  2334. static struct pci_device_id hrz_pci_tbl[] = {
  2335. { PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_HORIZON, PCI_ANY_ID, PCI_ANY_ID,
  2336. 0, 0, 0 },
  2337. { 0, }
  2338. };
  2339. MODULE_DEVICE_TABLE(pci, hrz_pci_tbl);
  2340. static struct pci_driver hrz_driver = {
  2341. .name = "horizon",
  2342. .probe = hrz_probe,
  2343. .remove = hrz_remove_one,
  2344. .id_table = hrz_pci_tbl,
  2345. };
  2346. /********** module entry **********/
  2347. static int __init hrz_module_init (void) {
  2348. // sanity check - cast is needed since printk does not support %Zu
  2349. if (sizeof(struct MEMMAP) != 128*1024/4) {
  2350. PRINTK (KERN_ERR, "Fix struct MEMMAP (is %lu fakewords).",
  2351. (unsigned long) sizeof(struct MEMMAP));
  2352. return -ENOMEM;
  2353. }
  2354. show_version();
  2355. // check arguments
  2356. hrz_check_args();
  2357. // get the juice
  2358. return pci_register_driver(&hrz_driver);
  2359. }
  2360. /********** module exit **********/
  2361. static void __exit hrz_module_exit (void) {
  2362. PRINTD (DBG_FLOW, "cleanup_module");
  2363. pci_unregister_driver(&hrz_driver);
  2364. }
  2365. module_init(hrz_module_init);
  2366. module_exit(hrz_module_exit);