horizon.c 83 KB

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