horizon.c 84 KB

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