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