horizon.c 84 KB

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