ambassador.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Madge Ambassador ATM Adapter driver.
  4. Copyright (C) 1995-1999 Madge Networks Ltd.
  5. */
  6. /* * dedicated to the memory of Graham Gordon 1971-1998 * */
  7. #include <linux/module.h>
  8. #include <linux/types.h>
  9. #include <linux/pci.h>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/ioport.h>
  13. #include <linux/atmdev.h>
  14. #include <linux/delay.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/poison.h>
  17. #include <linux/bitrev.h>
  18. #include <linux/mutex.h>
  19. #include <linux/firmware.h>
  20. #include <linux/ihex.h>
  21. #include <linux/slab.h>
  22. #include <linux/atomic.h>
  23. #include <asm/io.h>
  24. #include <asm/byteorder.h>
  25. #include "ambassador.h"
  26. #define maintainer_string "Giuliano Procida at Madge Networks <gprocida@madge.com>"
  27. #define description_string "Madge ATM Ambassador driver"
  28. #define version_string "1.2.4"
  29. static inline void __init show_version (void) {
  30. printk ("%s version %s\n", description_string, version_string);
  31. }
  32. /*
  33. Theory of Operation
  34. I Hardware, detection, initialisation and shutdown.
  35. 1. Supported Hardware
  36. This driver is for the PCI ATMizer-based Ambassador card (except
  37. very early versions). It is not suitable for the similar EISA "TR7"
  38. card. Commercially, both cards are known as Collage Server ATM
  39. adapters.
  40. The loader supports image transfer to the card, image start and few
  41. other miscellaneous commands.
  42. Only AAL5 is supported with vpi = 0 and vci in the range 0 to 1023.
  43. The cards are big-endian.
  44. 2. Detection
  45. Standard PCI stuff, the early cards are detected and rejected.
  46. 3. Initialisation
  47. The cards are reset and the self-test results are checked. The
  48. microcode image is then transferred and started. This waits for a
  49. pointer to a descriptor containing details of the host-based queues
  50. and buffers and various parameters etc. Once they are processed
  51. normal operations may begin. The BIA is read using a microcode
  52. command.
  53. 4. Shutdown
  54. This may be accomplished either by a card reset or via the microcode
  55. shutdown command. Further investigation required.
  56. 5. Persistent state
  57. The card reset does not affect PCI configuration (good) or the
  58. contents of several other "shared run-time registers" (bad) which
  59. include doorbell and interrupt control as well as EEPROM and PCI
  60. control. The driver must be careful when modifying these registers
  61. not to touch bits it does not use and to undo any changes at exit.
  62. II Driver software
  63. 0. Generalities
  64. The adapter is quite intelligent (fast) and has a simple interface
  65. (few features). VPI is always zero, 1024 VCIs are supported. There
  66. is limited cell rate support. UBR channels can be capped and ABR
  67. (explicit rate, but not EFCI) is supported. There is no CBR or VBR
  68. support.
  69. 1. Driver <-> Adapter Communication
  70. Apart from the basic loader commands, the driver communicates
  71. through three entities: the command queue (CQ), the transmit queue
  72. pair (TXQ) and the receive queue pairs (RXQ). These three entities
  73. are set up by the host and passed to the microcode just after it has
  74. been started.
  75. All queues are host-based circular queues. They are contiguous and
  76. (due to hardware limitations) have some restrictions as to their
  77. locations in (bus) memory. They are of the "full means the same as
  78. empty so don't do that" variety since the adapter uses pointers
  79. internally.
  80. The queue pairs work as follows: one queue is for supply to the
  81. adapter, items in it are pending and are owned by the adapter; the
  82. other is the queue for return from the adapter, items in it have
  83. been dealt with by the adapter. The host adds items to the supply
  84. (TX descriptors and free RX buffer descriptors) and removes items
  85. from the return (TX and RX completions). The adapter deals with out
  86. of order completions.
  87. Interrupts (card to host) and the doorbell (host to card) are used
  88. for signalling.
  89. 1. CQ
  90. This is to communicate "open VC", "close VC", "get stats" etc. to
  91. the adapter. At most one command is retired every millisecond by the
  92. card. There is no out of order completion or notification. The
  93. driver needs to check the return code of the command, waiting as
  94. appropriate.
  95. 2. TXQ
  96. TX supply items are of variable length (scatter gather support) and
  97. so the queue items are (more or less) pointers to the real thing.
  98. Each TX supply item contains a unique, host-supplied handle (the skb
  99. bus address seems most sensible as this works for Alphas as well,
  100. there is no need to do any endian conversions on the handles).
  101. TX return items consist of just the handles above.
  102. 3. RXQ (up to 4 of these with different lengths and buffer sizes)
  103. RX supply items consist of a unique, host-supplied handle (the skb
  104. bus address again) and a pointer to the buffer data area.
  105. RX return items consist of the handle above, the VC, length and a
  106. status word. This just screams "oh so easy" doesn't it?
  107. Note on RX pool sizes:
  108. Each pool should have enough buffers to handle a back-to-back stream
  109. of minimum sized frames on a single VC. For example:
  110. frame spacing = 3us (about right)
  111. delay = IRQ lat + RX handling + RX buffer replenish = 20 (us) (a guess)
  112. min number of buffers for one VC = 1 + delay/spacing (buffers)
  113. delay/spacing = latency = (20+2)/3 = 7 (buffers) (rounding up)
  114. The 20us delay assumes that there is no need to sleep; if we need to
  115. sleep to get buffers we are going to drop frames anyway.
  116. In fact, each pool should have enough buffers to support the
  117. simultaneous reassembly of a separate frame on each VC and cope with
  118. the case in which frames complete in round robin cell fashion on
  119. each VC.
  120. Only one frame can complete at each cell arrival, so if "n" VCs are
  121. open, the worst case is to have them all complete frames together
  122. followed by all starting new frames together.
  123. desired number of buffers = n + delay/spacing
  124. These are the extreme requirements, however, they are "n+k" for some
  125. "k" so we have only the constant to choose. This is the argument
  126. rx_lats which current defaults to 7.
  127. Actually, "n ? n+k : 0" is better and this is what is implemented,
  128. subject to the limit given by the pool size.
  129. 4. Driver locking
  130. Simple spinlocks are used around the TX and RX queue mechanisms.
  131. Anyone with a faster, working method is welcome to implement it.
  132. The adapter command queue is protected with a spinlock. We always
  133. wait for commands to complete.
  134. A more complex form of locking is used around parts of the VC open
  135. and close functions. There are three reasons for a lock: 1. we need
  136. to do atomic rate reservation and release (not used yet), 2. Opening
  137. sometimes involves two adapter commands which must not be separated
  138. by another command on the same VC, 3. the changes to RX pool size
  139. must be atomic. The lock needs to work over context switches, so we
  140. use a semaphore.
  141. III Hardware Features and Microcode Bugs
  142. 1. Byte Ordering
  143. *%^"$&%^$*&^"$(%^$#&^%$(&#%$*(&^#%!"!"!*!
  144. 2. Memory access
  145. All structures that are not accessed using DMA must be 4-byte
  146. aligned (not a problem) and must not cross 4MB boundaries.
  147. There is a DMA memory hole at E0000000-E00000FF (groan).
  148. TX fragments (DMA read) must not cross 4MB boundaries (would be 16MB
  149. but for a hardware bug).
  150. RX buffers (DMA write) must not cross 16MB boundaries and must
  151. include spare trailing bytes up to the next 4-byte boundary; they
  152. will be written with rubbish.
  153. The PLX likes to prefetch; if reading up to 4 u32 past the end of
  154. each TX fragment is not a problem, then TX can be made to go a
  155. little faster by passing a flag at init that disables a prefetch
  156. workaround. We do not pass this flag. (new microcode only)
  157. Now we:
  158. . Note that alloc_skb rounds up size to a 16byte boundary.
  159. . Ensure all areas do not traverse 4MB boundaries.
  160. . Ensure all areas do not start at a E00000xx bus address.
  161. (I cannot be certain, but this may always hold with Linux)
  162. . Make all failures cause a loud message.
  163. . Discard non-conforming SKBs (causes TX failure or RX fill delay).
  164. . Discard non-conforming TX fragment descriptors (the TX fails).
  165. In the future we could:
  166. . Allow RX areas that traverse 4MB (but not 16MB) boundaries.
  167. . Segment TX areas into some/more fragments, when necessary.
  168. . Relax checks for non-DMA items (ignore hole).
  169. . Give scatter-gather (iovec) requirements using ???. (?)
  170. 3. VC close is broken (only for new microcode)
  171. The VC close adapter microcode command fails to do anything if any
  172. frames have been received on the VC but none have been transmitted.
  173. Frames continue to be reassembled and passed (with IRQ) to the
  174. driver.
  175. IV To Do List
  176. . Fix bugs!
  177. . Timer code may be broken.
  178. . Deal with buggy VC close (somehow) in microcode 12.
  179. . Handle interrupted and/or non-blocking writes - is this a job for
  180. the protocol layer?
  181. . Add code to break up TX fragments when they span 4MB boundaries.
  182. . Add SUNI phy layer (need to know where SUNI lives on card).
  183. . Implement a tx_alloc fn to (a) satisfy TX alignment etc. and (b)
  184. leave extra headroom space for Ambassador TX descriptors.
  185. . Understand these elements of struct atm_vcc: recvq (proto?),
  186. sleep, callback, listenq, backlog_quota, reply and user_back.
  187. . Adjust TX/RX skb allocation to favour IP with LANE/CLIP (configurable).
  188. . Impose a TX-pending limit (2?) on each VC, help avoid TX q overflow.
  189. . Decide whether RX buffer recycling is or can be made completely safe;
  190. turn it back on. It looks like Werner is going to axe this.
  191. . Implement QoS changes on open VCs (involves extracting parts of VC open
  192. and close into separate functions and using them to make changes).
  193. . Hack on command queue so that someone can issue multiple commands and wait
  194. on the last one (OR only "no-op" or "wait" commands are waited for).
  195. . Eliminate need for while-schedule around do_command.
  196. */
  197. static void do_housekeeping (struct timer_list *t);
  198. /********** globals **********/
  199. static unsigned short debug = 0;
  200. static unsigned int cmds = 8;
  201. static unsigned int txs = 32;
  202. static unsigned int rxs[NUM_RX_POOLS] = { 64, 64, 64, 64 };
  203. static unsigned int rxs_bs[NUM_RX_POOLS] = { 4080, 12240, 36720, 65535 };
  204. static unsigned int rx_lats = 7;
  205. static unsigned char pci_lat = 0;
  206. static const unsigned long onegigmask = -1 << 30;
  207. /********** access to adapter **********/
  208. static inline void wr_plain (const amb_dev * dev, size_t addr, u32 data) {
  209. PRINTD (DBG_FLOW|DBG_REGS, "wr: %08zx <- %08x", addr, data);
  210. #ifdef AMB_MMIO
  211. dev->membase[addr / sizeof(u32)] = data;
  212. #else
  213. outl (data, dev->iobase + addr);
  214. #endif
  215. }
  216. static inline u32 rd_plain (const amb_dev * dev, size_t addr) {
  217. #ifdef AMB_MMIO
  218. u32 data = dev->membase[addr / sizeof(u32)];
  219. #else
  220. u32 data = inl (dev->iobase + addr);
  221. #endif
  222. PRINTD (DBG_FLOW|DBG_REGS, "rd: %08zx -> %08x", addr, data);
  223. return data;
  224. }
  225. static inline void wr_mem (const amb_dev * dev, size_t addr, u32 data) {
  226. __be32 be = cpu_to_be32 (data);
  227. PRINTD (DBG_FLOW|DBG_REGS, "wr: %08zx <- %08x b[%08x]", addr, data, be);
  228. #ifdef AMB_MMIO
  229. dev->membase[addr / sizeof(u32)] = be;
  230. #else
  231. outl (be, dev->iobase + addr);
  232. #endif
  233. }
  234. static inline u32 rd_mem (const amb_dev * dev, size_t addr) {
  235. #ifdef AMB_MMIO
  236. __be32 be = dev->membase[addr / sizeof(u32)];
  237. #else
  238. __be32 be = inl (dev->iobase + addr);
  239. #endif
  240. u32 data = be32_to_cpu (be);
  241. PRINTD (DBG_FLOW|DBG_REGS, "rd: %08zx -> %08x b[%08x]", addr, data, be);
  242. return data;
  243. }
  244. /********** dump routines **********/
  245. static inline void dump_registers (const amb_dev * dev) {
  246. #ifdef DEBUG_AMBASSADOR
  247. if (debug & DBG_REGS) {
  248. size_t i;
  249. PRINTD (DBG_REGS, "reading PLX control: ");
  250. for (i = 0x00; i < 0x30; i += sizeof(u32))
  251. rd_mem (dev, i);
  252. PRINTD (DBG_REGS, "reading mailboxes: ");
  253. for (i = 0x40; i < 0x60; i += sizeof(u32))
  254. rd_mem (dev, i);
  255. PRINTD (DBG_REGS, "reading doorb irqev irqen reset:");
  256. for (i = 0x60; i < 0x70; i += sizeof(u32))
  257. rd_mem (dev, i);
  258. }
  259. #else
  260. (void) dev;
  261. #endif
  262. return;
  263. }
  264. static inline void dump_loader_block (volatile loader_block * lb) {
  265. #ifdef DEBUG_AMBASSADOR
  266. unsigned int i;
  267. PRINTDB (DBG_LOAD, "lb @ %p; res: %d, cmd: %d, pay:",
  268. lb, be32_to_cpu (lb->result), be32_to_cpu (lb->command));
  269. for (i = 0; i < MAX_COMMAND_DATA; ++i)
  270. PRINTDM (DBG_LOAD, " %08x", be32_to_cpu (lb->payload.data[i]));
  271. PRINTDE (DBG_LOAD, ", vld: %08x", be32_to_cpu (lb->valid));
  272. #else
  273. (void) lb;
  274. #endif
  275. return;
  276. }
  277. static inline void dump_command (command * cmd) {
  278. #ifdef DEBUG_AMBASSADOR
  279. unsigned int i;
  280. PRINTDB (DBG_CMD, "cmd @ %p, req: %08x, pars:",
  281. cmd, /*be32_to_cpu*/ (cmd->request));
  282. for (i = 0; i < 3; ++i)
  283. PRINTDM (DBG_CMD, " %08x", /*be32_to_cpu*/ (cmd->args.par[i]));
  284. PRINTDE (DBG_CMD, "");
  285. #else
  286. (void) cmd;
  287. #endif
  288. return;
  289. }
  290. static inline void dump_skb (char * prefix, unsigned int vc, struct sk_buff * skb) {
  291. #ifdef DEBUG_AMBASSADOR
  292. unsigned int i;
  293. unsigned char * data = skb->data;
  294. PRINTDB (DBG_DATA, "%s(%u) ", prefix, vc);
  295. for (i=0; i<skb->len && i < 256;i++)
  296. PRINTDM (DBG_DATA, "%02x ", data[i]);
  297. PRINTDE (DBG_DATA,"");
  298. #else
  299. (void) prefix;
  300. (void) vc;
  301. (void) skb;
  302. #endif
  303. return;
  304. }
  305. /********** check memory areas for use by Ambassador **********/
  306. /* see limitations under Hardware Features */
  307. static int check_area (void * start, size_t length) {
  308. // assumes length > 0
  309. const u32 fourmegmask = -1 << 22;
  310. const u32 twofivesixmask = -1 << 8;
  311. const u32 starthole = 0xE0000000;
  312. u32 startaddress = virt_to_bus (start);
  313. u32 lastaddress = startaddress+length-1;
  314. if ((startaddress ^ lastaddress) & fourmegmask ||
  315. (startaddress & twofivesixmask) == starthole) {
  316. PRINTK (KERN_ERR, "check_area failure: [%x,%x] - mail maintainer!",
  317. startaddress, lastaddress);
  318. return -1;
  319. } else {
  320. return 0;
  321. }
  322. }
  323. /********** free an skb (as per ATM device driver documentation) **********/
  324. static void amb_kfree_skb (struct sk_buff * skb) {
  325. if (ATM_SKB(skb)->vcc->pop) {
  326. ATM_SKB(skb)->vcc->pop (ATM_SKB(skb)->vcc, skb);
  327. } else {
  328. dev_kfree_skb_any (skb);
  329. }
  330. }
  331. /********** TX completion **********/
  332. static void tx_complete (amb_dev * dev, tx_out * tx) {
  333. tx_simple * tx_descr = bus_to_virt (tx->handle);
  334. struct sk_buff * skb = tx_descr->skb;
  335. PRINTD (DBG_FLOW|DBG_TX, "tx_complete %p %p", dev, tx);
  336. // VC layer stats
  337. atomic_inc(&ATM_SKB(skb)->vcc->stats->tx);
  338. // free the descriptor
  339. kfree (tx_descr);
  340. // free the skb
  341. amb_kfree_skb (skb);
  342. dev->stats.tx_ok++;
  343. return;
  344. }
  345. /********** RX completion **********/
  346. static void rx_complete (amb_dev * dev, rx_out * rx) {
  347. struct sk_buff * skb = bus_to_virt (rx->handle);
  348. u16 vc = be16_to_cpu (rx->vc);
  349. // unused: u16 lec_id = be16_to_cpu (rx->lec_id);
  350. u16 status = be16_to_cpu (rx->status);
  351. u16 rx_len = be16_to_cpu (rx->length);
  352. PRINTD (DBG_FLOW|DBG_RX, "rx_complete %p %p (len=%hu)", dev, rx, rx_len);
  353. // XXX move this in and add to VC stats ???
  354. if (!status) {
  355. struct atm_vcc * atm_vcc = dev->rxer[vc];
  356. dev->stats.rx.ok++;
  357. if (atm_vcc) {
  358. if (rx_len <= atm_vcc->qos.rxtp.max_sdu) {
  359. if (atm_charge (atm_vcc, skb->truesize)) {
  360. // prepare socket buffer
  361. ATM_SKB(skb)->vcc = atm_vcc;
  362. skb_put (skb, rx_len);
  363. dump_skb ("<<<", vc, skb);
  364. // VC layer stats
  365. atomic_inc(&atm_vcc->stats->rx);
  366. __net_timestamp(skb);
  367. // end of our responsibility
  368. atm_vcc->push (atm_vcc, skb);
  369. return;
  370. } else {
  371. // someone fix this (message), please!
  372. PRINTD (DBG_INFO|DBG_RX, "dropped thanks to atm_charge (vc %hu, truesize %u)", vc, skb->truesize);
  373. // drop stats incremented in atm_charge
  374. }
  375. } else {
  376. PRINTK (KERN_INFO, "dropped over-size frame");
  377. // should we count this?
  378. atomic_inc(&atm_vcc->stats->rx_drop);
  379. }
  380. } else {
  381. PRINTD (DBG_WARN|DBG_RX, "got frame but RX closed for channel %hu", vc);
  382. // this is an adapter bug, only in new version of microcode
  383. }
  384. } else {
  385. dev->stats.rx.error++;
  386. if (status & CRC_ERR)
  387. dev->stats.rx.badcrc++;
  388. if (status & LEN_ERR)
  389. dev->stats.rx.toolong++;
  390. if (status & ABORT_ERR)
  391. dev->stats.rx.aborted++;
  392. if (status & UNUSED_ERR)
  393. dev->stats.rx.unused++;
  394. }
  395. dev_kfree_skb_any (skb);
  396. return;
  397. }
  398. /*
  399. Note on queue handling.
  400. Here "give" and "take" refer to queue entries and a queue (pair)
  401. rather than frames to or from the host or adapter. Empty frame
  402. buffers are given to the RX queue pair and returned unused or
  403. containing RX frames. TX frames (well, pointers to TX fragment
  404. lists) are given to the TX queue pair, completions are returned.
  405. */
  406. /********** command queue **********/
  407. // I really don't like this, but it's the best I can do at the moment
  408. // also, the callers are responsible for byte order as the microcode
  409. // sometimes does 16-bit accesses (yuk yuk yuk)
  410. static int command_do (amb_dev * dev, command * cmd) {
  411. amb_cq * cq = &dev->cq;
  412. volatile amb_cq_ptrs * ptrs = &cq->ptrs;
  413. command * my_slot;
  414. PRINTD (DBG_FLOW|DBG_CMD, "command_do %p", dev);
  415. if (test_bit (dead, &dev->flags))
  416. return 0;
  417. spin_lock (&cq->lock);
  418. // if not full...
  419. if (cq->pending < cq->maximum) {
  420. // remember my slot for later
  421. my_slot = ptrs->in;
  422. PRINTD (DBG_CMD, "command in slot %p", my_slot);
  423. dump_command (cmd);
  424. // copy command in
  425. *ptrs->in = *cmd;
  426. cq->pending++;
  427. ptrs->in = NEXTQ (ptrs->in, ptrs->start, ptrs->limit);
  428. // mail the command
  429. wr_mem (dev, offsetof(amb_mem, mb.adapter.cmd_address), virt_to_bus (ptrs->in));
  430. if (cq->pending > cq->high)
  431. cq->high = cq->pending;
  432. spin_unlock (&cq->lock);
  433. // these comments were in a while-loop before, msleep removes the loop
  434. // go to sleep
  435. // PRINTD (DBG_CMD, "wait: sleeping %lu for command", timeout);
  436. msleep(cq->pending);
  437. // wait for my slot to be reached (all waiters are here or above, until...)
  438. while (ptrs->out != my_slot) {
  439. PRINTD (DBG_CMD, "wait: command slot (now at %p)", ptrs->out);
  440. set_current_state(TASK_UNINTERRUPTIBLE);
  441. schedule();
  442. }
  443. // wait on my slot (... one gets to its slot, and... )
  444. while (ptrs->out->request != cpu_to_be32 (SRB_COMPLETE)) {
  445. PRINTD (DBG_CMD, "wait: command slot completion");
  446. set_current_state(TASK_UNINTERRUPTIBLE);
  447. schedule();
  448. }
  449. PRINTD (DBG_CMD, "command complete");
  450. // update queue (... moves the queue along to the next slot)
  451. spin_lock (&cq->lock);
  452. cq->pending--;
  453. // copy command out
  454. *cmd = *ptrs->out;
  455. ptrs->out = NEXTQ (ptrs->out, ptrs->start, ptrs->limit);
  456. spin_unlock (&cq->lock);
  457. return 0;
  458. } else {
  459. cq->filled++;
  460. spin_unlock (&cq->lock);
  461. return -EAGAIN;
  462. }
  463. }
  464. /********** TX queue pair **********/
  465. static int tx_give (amb_dev * dev, tx_in * tx) {
  466. amb_txq * txq = &dev->txq;
  467. unsigned long flags;
  468. PRINTD (DBG_FLOW|DBG_TX, "tx_give %p", dev);
  469. if (test_bit (dead, &dev->flags))
  470. return 0;
  471. spin_lock_irqsave (&txq->lock, flags);
  472. if (txq->pending < txq->maximum) {
  473. PRINTD (DBG_TX, "TX in slot %p", txq->in.ptr);
  474. *txq->in.ptr = *tx;
  475. txq->pending++;
  476. txq->in.ptr = NEXTQ (txq->in.ptr, txq->in.start, txq->in.limit);
  477. // hand over the TX and ring the bell
  478. wr_mem (dev, offsetof(amb_mem, mb.adapter.tx_address), virt_to_bus (txq->in.ptr));
  479. wr_mem (dev, offsetof(amb_mem, doorbell), TX_FRAME);
  480. if (txq->pending > txq->high)
  481. txq->high = txq->pending;
  482. spin_unlock_irqrestore (&txq->lock, flags);
  483. return 0;
  484. } else {
  485. txq->filled++;
  486. spin_unlock_irqrestore (&txq->lock, flags);
  487. return -EAGAIN;
  488. }
  489. }
  490. static int tx_take (amb_dev * dev) {
  491. amb_txq * txq = &dev->txq;
  492. unsigned long flags;
  493. PRINTD (DBG_FLOW|DBG_TX, "tx_take %p", dev);
  494. spin_lock_irqsave (&txq->lock, flags);
  495. if (txq->pending && txq->out.ptr->handle) {
  496. // deal with TX completion
  497. tx_complete (dev, txq->out.ptr);
  498. // mark unused again
  499. txq->out.ptr->handle = 0;
  500. // remove item
  501. txq->pending--;
  502. txq->out.ptr = NEXTQ (txq->out.ptr, txq->out.start, txq->out.limit);
  503. spin_unlock_irqrestore (&txq->lock, flags);
  504. return 0;
  505. } else {
  506. spin_unlock_irqrestore (&txq->lock, flags);
  507. return -1;
  508. }
  509. }
  510. /********** RX queue pairs **********/
  511. static int rx_give (amb_dev * dev, rx_in * rx, unsigned char pool) {
  512. amb_rxq * rxq = &dev->rxq[pool];
  513. unsigned long flags;
  514. PRINTD (DBG_FLOW|DBG_RX, "rx_give %p[%hu]", dev, pool);
  515. spin_lock_irqsave (&rxq->lock, flags);
  516. if (rxq->pending < rxq->maximum) {
  517. PRINTD (DBG_RX, "RX in slot %p", rxq->in.ptr);
  518. *rxq->in.ptr = *rx;
  519. rxq->pending++;
  520. rxq->in.ptr = NEXTQ (rxq->in.ptr, rxq->in.start, rxq->in.limit);
  521. // hand over the RX buffer
  522. wr_mem (dev, offsetof(amb_mem, mb.adapter.rx_address[pool]), virt_to_bus (rxq->in.ptr));
  523. spin_unlock_irqrestore (&rxq->lock, flags);
  524. return 0;
  525. } else {
  526. spin_unlock_irqrestore (&rxq->lock, flags);
  527. return -1;
  528. }
  529. }
  530. static int rx_take (amb_dev * dev, unsigned char pool) {
  531. amb_rxq * rxq = &dev->rxq[pool];
  532. unsigned long flags;
  533. PRINTD (DBG_FLOW|DBG_RX, "rx_take %p[%hu]", dev, pool);
  534. spin_lock_irqsave (&rxq->lock, flags);
  535. if (rxq->pending && (rxq->out.ptr->status || rxq->out.ptr->length)) {
  536. // deal with RX completion
  537. rx_complete (dev, rxq->out.ptr);
  538. // mark unused again
  539. rxq->out.ptr->status = 0;
  540. rxq->out.ptr->length = 0;
  541. // remove item
  542. rxq->pending--;
  543. rxq->out.ptr = NEXTQ (rxq->out.ptr, rxq->out.start, rxq->out.limit);
  544. if (rxq->pending < rxq->low)
  545. rxq->low = rxq->pending;
  546. spin_unlock_irqrestore (&rxq->lock, flags);
  547. return 0;
  548. } else {
  549. if (!rxq->pending && rxq->buffers_wanted)
  550. rxq->emptied++;
  551. spin_unlock_irqrestore (&rxq->lock, flags);
  552. return -1;
  553. }
  554. }
  555. /********** RX Pool handling **********/
  556. /* pre: buffers_wanted = 0, post: pending = 0 */
  557. static void drain_rx_pool (amb_dev * dev, unsigned char pool) {
  558. amb_rxq * rxq = &dev->rxq[pool];
  559. PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pool %p %hu", dev, pool);
  560. if (test_bit (dead, &dev->flags))
  561. return;
  562. /* we are not quite like the fill pool routines as we cannot just
  563. remove one buffer, we have to remove all of them, but we might as
  564. well pretend... */
  565. if (rxq->pending > rxq->buffers_wanted) {
  566. command cmd;
  567. cmd.request = cpu_to_be32 (SRB_FLUSH_BUFFER_Q);
  568. cmd.args.flush.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
  569. while (command_do (dev, &cmd))
  570. schedule();
  571. /* the pool may also be emptied via the interrupt handler */
  572. while (rxq->pending > rxq->buffers_wanted)
  573. if (rx_take (dev, pool))
  574. schedule();
  575. }
  576. return;
  577. }
  578. static void drain_rx_pools (amb_dev * dev) {
  579. unsigned char pool;
  580. PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pools %p", dev);
  581. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  582. drain_rx_pool (dev, pool);
  583. }
  584. static void fill_rx_pool (amb_dev * dev, unsigned char pool,
  585. gfp_t priority)
  586. {
  587. rx_in rx;
  588. amb_rxq * rxq;
  589. PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pool %p %hu %x", dev, pool, priority);
  590. if (test_bit (dead, &dev->flags))
  591. return;
  592. rxq = &dev->rxq[pool];
  593. while (rxq->pending < rxq->maximum && rxq->pending < rxq->buffers_wanted) {
  594. struct sk_buff * skb = alloc_skb (rxq->buffer_size, priority);
  595. if (!skb) {
  596. PRINTD (DBG_SKB|DBG_POOL, "failed to allocate skb for RX pool %hu", pool);
  597. return;
  598. }
  599. if (check_area (skb->data, skb->truesize)) {
  600. dev_kfree_skb_any (skb);
  601. return;
  602. }
  603. // cast needed as there is no %? for pointer differences
  604. PRINTD (DBG_SKB, "allocated skb at %p, head %p, area %li",
  605. skb, skb->head, (long) skb_end_offset(skb));
  606. rx.handle = virt_to_bus (skb);
  607. rx.host_address = cpu_to_be32 (virt_to_bus (skb->data));
  608. if (rx_give (dev, &rx, pool))
  609. dev_kfree_skb_any (skb);
  610. }
  611. return;
  612. }
  613. // top up all RX pools
  614. static void fill_rx_pools (amb_dev * dev) {
  615. unsigned char pool;
  616. PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pools %p", dev);
  617. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  618. fill_rx_pool (dev, pool, GFP_ATOMIC);
  619. return;
  620. }
  621. /********** enable host interrupts **********/
  622. static void interrupts_on (amb_dev * dev) {
  623. wr_plain (dev, offsetof(amb_mem, interrupt_control),
  624. rd_plain (dev, offsetof(amb_mem, interrupt_control))
  625. | AMB_INTERRUPT_BITS);
  626. }
  627. /********** disable host interrupts **********/
  628. static void interrupts_off (amb_dev * dev) {
  629. wr_plain (dev, offsetof(amb_mem, interrupt_control),
  630. rd_plain (dev, offsetof(amb_mem, interrupt_control))
  631. &~ AMB_INTERRUPT_BITS);
  632. }
  633. /********** interrupt handling **********/
  634. static irqreturn_t interrupt_handler(int irq, void *dev_id) {
  635. amb_dev * dev = dev_id;
  636. PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler: %p", dev_id);
  637. {
  638. u32 interrupt = rd_plain (dev, offsetof(amb_mem, interrupt));
  639. // for us or someone else sharing the same interrupt
  640. if (!interrupt) {
  641. PRINTD (DBG_IRQ, "irq not for me: %d", irq);
  642. return IRQ_NONE;
  643. }
  644. // definitely for us
  645. PRINTD (DBG_IRQ, "FYI: interrupt was %08x", interrupt);
  646. wr_plain (dev, offsetof(amb_mem, interrupt), -1);
  647. }
  648. {
  649. unsigned int irq_work = 0;
  650. unsigned char pool;
  651. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  652. while (!rx_take (dev, pool))
  653. ++irq_work;
  654. while (!tx_take (dev))
  655. ++irq_work;
  656. if (irq_work) {
  657. fill_rx_pools (dev);
  658. PRINTD (DBG_IRQ, "work done: %u", irq_work);
  659. } else {
  660. PRINTD (DBG_IRQ|DBG_WARN, "no work done");
  661. }
  662. }
  663. PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler done: %p", dev_id);
  664. return IRQ_HANDLED;
  665. }
  666. /********** make rate (not quite as much fun as Horizon) **********/
  667. static int make_rate (unsigned int rate, rounding r,
  668. u16 * bits, unsigned int * actual) {
  669. unsigned char exp = -1; // hush gcc
  670. unsigned int man = -1; // hush gcc
  671. PRINTD (DBG_FLOW|DBG_QOS, "make_rate %u", rate);
  672. // rates in cells per second, ITU format (nasty 16-bit floating-point)
  673. // given 5-bit e and 9-bit m:
  674. // rate = EITHER (1+m/2^9)*2^e OR 0
  675. // bits = EITHER 1<<14 | e<<9 | m OR 0
  676. // (bit 15 is "reserved", bit 14 "non-zero")
  677. // smallest rate is 0 (special representation)
  678. // largest rate is (1+511/512)*2^31 = 4290772992 (< 2^32-1)
  679. // smallest non-zero rate is (1+0/512)*2^0 = 1 (> 0)
  680. // simple algorithm:
  681. // find position of top bit, this gives e
  682. // remove top bit and shift (rounding if feeling clever) by 9-e
  683. // ucode bug: please don't set bit 14! so 0 rate not representable
  684. if (rate > 0xffc00000U) {
  685. // larger than largest representable rate
  686. if (r == round_up) {
  687. return -EINVAL;
  688. } else {
  689. exp = 31;
  690. man = 511;
  691. }
  692. } else if (rate) {
  693. // representable rate
  694. exp = 31;
  695. man = rate;
  696. // invariant: rate = man*2^(exp-31)
  697. while (!(man & (1<<31))) {
  698. exp = exp - 1;
  699. man = man<<1;
  700. }
  701. // man has top bit set
  702. // rate = (2^31+(man-2^31))*2^(exp-31)
  703. // rate = (1+(man-2^31)/2^31)*2^exp
  704. man = man<<1;
  705. man &= 0xffffffffU; // a nop on 32-bit systems
  706. // rate = (1+man/2^32)*2^exp
  707. // exp is in the range 0 to 31, man is in the range 0 to 2^32-1
  708. // time to lose significance... we want m in the range 0 to 2^9-1
  709. // rounding presents a minor problem... we first decide which way
  710. // we are rounding (based on given rounding direction and possibly
  711. // the bits of the mantissa that are to be discarded).
  712. switch (r) {
  713. case round_down: {
  714. // just truncate
  715. man = man>>(32-9);
  716. break;
  717. }
  718. case round_up: {
  719. // check all bits that we are discarding
  720. if (man & (~0U>>9)) {
  721. man = (man>>(32-9)) + 1;
  722. if (man == (1<<9)) {
  723. // no need to check for round up outside of range
  724. man = 0;
  725. exp += 1;
  726. }
  727. } else {
  728. man = (man>>(32-9));
  729. }
  730. break;
  731. }
  732. case round_nearest: {
  733. // check msb that we are discarding
  734. if (man & (1<<(32-9-1))) {
  735. man = (man>>(32-9)) + 1;
  736. if (man == (1<<9)) {
  737. // no need to check for round up outside of range
  738. man = 0;
  739. exp += 1;
  740. }
  741. } else {
  742. man = (man>>(32-9));
  743. }
  744. break;
  745. }
  746. }
  747. } else {
  748. // zero rate - not representable
  749. if (r == round_down) {
  750. return -EINVAL;
  751. } else {
  752. exp = 0;
  753. man = 0;
  754. }
  755. }
  756. PRINTD (DBG_QOS, "rate: man=%u, exp=%hu", man, exp);
  757. if (bits)
  758. *bits = /* (1<<14) | */ (exp<<9) | man;
  759. if (actual)
  760. *actual = (exp >= 9)
  761. ? (1 << exp) + (man << (exp-9))
  762. : (1 << exp) + ((man + (1<<(9-exp-1))) >> (9-exp));
  763. return 0;
  764. }
  765. /********** Linux ATM Operations **********/
  766. // some are not yet implemented while others do not make sense for
  767. // this device
  768. /********** Open a VC **********/
  769. static int amb_open (struct atm_vcc * atm_vcc)
  770. {
  771. int error;
  772. struct atm_qos * qos;
  773. struct atm_trafprm * txtp;
  774. struct atm_trafprm * rxtp;
  775. u16 tx_rate_bits = -1; // hush gcc
  776. u16 tx_vc_bits = -1; // hush gcc
  777. u16 tx_frame_bits = -1; // hush gcc
  778. amb_dev * dev = AMB_DEV(atm_vcc->dev);
  779. amb_vcc * vcc;
  780. unsigned char pool = -1; // hush gcc
  781. short vpi = atm_vcc->vpi;
  782. int vci = atm_vcc->vci;
  783. PRINTD (DBG_FLOW|DBG_VCC, "amb_open %x %x", vpi, vci);
  784. #ifdef ATM_VPI_UNSPEC
  785. // UNSPEC is deprecated, remove this code eventually
  786. if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) {
  787. PRINTK (KERN_WARNING, "rejecting open with unspecified VPI/VCI (deprecated)");
  788. return -EINVAL;
  789. }
  790. #endif
  791. if (!(0 <= vpi && vpi < (1<<NUM_VPI_BITS) &&
  792. 0 <= vci && vci < (1<<NUM_VCI_BITS))) {
  793. PRINTD (DBG_WARN|DBG_VCC, "VPI/VCI out of range: %hd/%d", vpi, vci);
  794. return -EINVAL;
  795. }
  796. qos = &atm_vcc->qos;
  797. if (qos->aal != ATM_AAL5) {
  798. PRINTD (DBG_QOS, "AAL not supported");
  799. return -EINVAL;
  800. }
  801. // traffic parameters
  802. PRINTD (DBG_QOS, "TX:");
  803. txtp = &qos->txtp;
  804. if (txtp->traffic_class != ATM_NONE) {
  805. switch (txtp->traffic_class) {
  806. case ATM_UBR: {
  807. // we take "the PCR" as a rate-cap
  808. int pcr = atm_pcr_goal (txtp);
  809. if (!pcr) {
  810. // no rate cap
  811. tx_rate_bits = 0;
  812. tx_vc_bits = TX_UBR;
  813. tx_frame_bits = TX_FRAME_NOTCAP;
  814. } else {
  815. rounding r;
  816. if (pcr < 0) {
  817. r = round_down;
  818. pcr = -pcr;
  819. } else {
  820. r = round_up;
  821. }
  822. error = make_rate (pcr, r, &tx_rate_bits, NULL);
  823. if (error)
  824. return error;
  825. tx_vc_bits = TX_UBR_CAPPED;
  826. tx_frame_bits = TX_FRAME_CAPPED;
  827. }
  828. break;
  829. }
  830. #if 0
  831. case ATM_ABR: {
  832. pcr = atm_pcr_goal (txtp);
  833. PRINTD (DBG_QOS, "pcr goal = %d", pcr);
  834. break;
  835. }
  836. #endif
  837. default: {
  838. // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
  839. PRINTD (DBG_QOS, "request for non-UBR denied");
  840. return -EINVAL;
  841. }
  842. }
  843. PRINTD (DBG_QOS, "tx_rate_bits=%hx, tx_vc_bits=%hx",
  844. tx_rate_bits, tx_vc_bits);
  845. }
  846. PRINTD (DBG_QOS, "RX:");
  847. rxtp = &qos->rxtp;
  848. if (rxtp->traffic_class == ATM_NONE) {
  849. // do nothing
  850. } else {
  851. // choose an RX pool (arranged in increasing size)
  852. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  853. if ((unsigned int) rxtp->max_sdu <= dev->rxq[pool].buffer_size) {
  854. PRINTD (DBG_VCC|DBG_QOS|DBG_POOL, "chose pool %hu (max_sdu %u <= %u)",
  855. pool, rxtp->max_sdu, dev->rxq[pool].buffer_size);
  856. break;
  857. }
  858. if (pool == NUM_RX_POOLS) {
  859. PRINTD (DBG_WARN|DBG_VCC|DBG_QOS|DBG_POOL,
  860. "no pool suitable for VC (RX max_sdu %d is too large)",
  861. rxtp->max_sdu);
  862. return -EINVAL;
  863. }
  864. switch (rxtp->traffic_class) {
  865. case ATM_UBR: {
  866. break;
  867. }
  868. #if 0
  869. case ATM_ABR: {
  870. pcr = atm_pcr_goal (rxtp);
  871. PRINTD (DBG_QOS, "pcr goal = %d", pcr);
  872. break;
  873. }
  874. #endif
  875. default: {
  876. // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
  877. PRINTD (DBG_QOS, "request for non-UBR denied");
  878. return -EINVAL;
  879. }
  880. }
  881. }
  882. // get space for our vcc stuff
  883. vcc = kmalloc (sizeof(amb_vcc), GFP_KERNEL);
  884. if (!vcc) {
  885. PRINTK (KERN_ERR, "out of memory!");
  886. return -ENOMEM;
  887. }
  888. atm_vcc->dev_data = (void *) vcc;
  889. // no failures beyond this point
  890. // we are not really "immediately before allocating the connection
  891. // identifier in hardware", but it will just have to do!
  892. set_bit(ATM_VF_ADDR,&atm_vcc->flags);
  893. if (txtp->traffic_class != ATM_NONE) {
  894. command cmd;
  895. vcc->tx_frame_bits = tx_frame_bits;
  896. mutex_lock(&dev->vcc_sf);
  897. if (dev->rxer[vci]) {
  898. // RXer on the channel already, just modify rate...
  899. cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
  900. cmd.args.modify_rate.vc = cpu_to_be32 (vci); // vpi 0
  901. cmd.args.modify_rate.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
  902. while (command_do (dev, &cmd))
  903. schedule();
  904. // ... and TX flags, preserving the RX pool
  905. cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
  906. cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
  907. cmd.args.modify_flags.flags = cpu_to_be32
  908. ( (AMB_VCC(dev->rxer[vci])->rx_info.pool << SRB_POOL_SHIFT)
  909. | (tx_vc_bits << SRB_FLAGS_SHIFT) );
  910. while (command_do (dev, &cmd))
  911. schedule();
  912. } else {
  913. // no RXer on the channel, just open (with pool zero)
  914. cmd.request = cpu_to_be32 (SRB_OPEN_VC);
  915. cmd.args.open.vc = cpu_to_be32 (vci); // vpi 0
  916. cmd.args.open.flags = cpu_to_be32 (tx_vc_bits << SRB_FLAGS_SHIFT);
  917. cmd.args.open.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
  918. while (command_do (dev, &cmd))
  919. schedule();
  920. }
  921. dev->txer[vci].tx_present = 1;
  922. mutex_unlock(&dev->vcc_sf);
  923. }
  924. if (rxtp->traffic_class != ATM_NONE) {
  925. command cmd;
  926. vcc->rx_info.pool = pool;
  927. mutex_lock(&dev->vcc_sf);
  928. /* grow RX buffer pool */
  929. if (!dev->rxq[pool].buffers_wanted)
  930. dev->rxq[pool].buffers_wanted = rx_lats;
  931. dev->rxq[pool].buffers_wanted += 1;
  932. fill_rx_pool (dev, pool, GFP_KERNEL);
  933. if (dev->txer[vci].tx_present) {
  934. // TXer on the channel already
  935. // switch (from pool zero) to this pool, preserving the TX bits
  936. cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
  937. cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
  938. cmd.args.modify_flags.flags = cpu_to_be32
  939. ( (pool << SRB_POOL_SHIFT)
  940. | (dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT) );
  941. } else {
  942. // no TXer on the channel, open the VC (with no rate info)
  943. cmd.request = cpu_to_be32 (SRB_OPEN_VC);
  944. cmd.args.open.vc = cpu_to_be32 (vci); // vpi 0
  945. cmd.args.open.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
  946. cmd.args.open.rate = cpu_to_be32 (0);
  947. }
  948. while (command_do (dev, &cmd))
  949. schedule();
  950. // this link allows RX frames through
  951. dev->rxer[vci] = atm_vcc;
  952. mutex_unlock(&dev->vcc_sf);
  953. }
  954. // indicate readiness
  955. set_bit(ATM_VF_READY,&atm_vcc->flags);
  956. return 0;
  957. }
  958. /********** Close a VC **********/
  959. static void amb_close (struct atm_vcc * atm_vcc) {
  960. amb_dev * dev = AMB_DEV (atm_vcc->dev);
  961. amb_vcc * vcc = AMB_VCC (atm_vcc);
  962. u16 vci = atm_vcc->vci;
  963. PRINTD (DBG_VCC|DBG_FLOW, "amb_close");
  964. // indicate unreadiness
  965. clear_bit(ATM_VF_READY,&atm_vcc->flags);
  966. // disable TXing
  967. if (atm_vcc->qos.txtp.traffic_class != ATM_NONE) {
  968. command cmd;
  969. mutex_lock(&dev->vcc_sf);
  970. if (dev->rxer[vci]) {
  971. // RXer still on the channel, just modify rate... XXX not really needed
  972. cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
  973. cmd.args.modify_rate.vc = cpu_to_be32 (vci); // vpi 0
  974. cmd.args.modify_rate.rate = cpu_to_be32 (0);
  975. // ... and clear TX rate flags (XXX to stop RM cell output?), preserving RX pool
  976. } else {
  977. // no RXer on the channel, close channel
  978. cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
  979. cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
  980. }
  981. dev->txer[vci].tx_present = 0;
  982. while (command_do (dev, &cmd))
  983. schedule();
  984. mutex_unlock(&dev->vcc_sf);
  985. }
  986. // disable RXing
  987. if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
  988. command cmd;
  989. // this is (the?) one reason why we need the amb_vcc struct
  990. unsigned char pool = vcc->rx_info.pool;
  991. mutex_lock(&dev->vcc_sf);
  992. if (dev->txer[vci].tx_present) {
  993. // TXer still on the channel, just go to pool zero XXX not really needed
  994. cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
  995. cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
  996. cmd.args.modify_flags.flags = cpu_to_be32
  997. (dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT);
  998. } else {
  999. // no TXer on the channel, close the VC
  1000. cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
  1001. cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
  1002. }
  1003. // forget the rxer - no more skbs will be pushed
  1004. if (atm_vcc != dev->rxer[vci])
  1005. PRINTK (KERN_ERR, "%s vcc=%p rxer[vci]=%p",
  1006. "arghhh! we're going to die!",
  1007. vcc, dev->rxer[vci]);
  1008. dev->rxer[vci] = NULL;
  1009. while (command_do (dev, &cmd))
  1010. schedule();
  1011. /* shrink RX buffer pool */
  1012. dev->rxq[pool].buffers_wanted -= 1;
  1013. if (dev->rxq[pool].buffers_wanted == rx_lats) {
  1014. dev->rxq[pool].buffers_wanted = 0;
  1015. drain_rx_pool (dev, pool);
  1016. }
  1017. mutex_unlock(&dev->vcc_sf);
  1018. }
  1019. // free our structure
  1020. kfree (vcc);
  1021. // say the VPI/VCI is free again
  1022. clear_bit(ATM_VF_ADDR,&atm_vcc->flags);
  1023. return;
  1024. }
  1025. /********** Send **********/
  1026. static int amb_send (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
  1027. amb_dev * dev = AMB_DEV(atm_vcc->dev);
  1028. amb_vcc * vcc = AMB_VCC(atm_vcc);
  1029. u16 vc = atm_vcc->vci;
  1030. unsigned int tx_len = skb->len;
  1031. unsigned char * tx_data = skb->data;
  1032. tx_simple * tx_descr;
  1033. tx_in tx;
  1034. if (test_bit (dead, &dev->flags))
  1035. return -EIO;
  1036. PRINTD (DBG_FLOW|DBG_TX, "amb_send vc %x data %p len %u",
  1037. vc, tx_data, tx_len);
  1038. dump_skb (">>>", vc, skb);
  1039. if (!dev->txer[vc].tx_present) {
  1040. PRINTK (KERN_ERR, "attempt to send on RX-only VC %x", vc);
  1041. return -EBADFD;
  1042. }
  1043. // this is a driver private field so we have to set it ourselves,
  1044. // despite the fact that we are _required_ to use it to check for a
  1045. // pop function
  1046. ATM_SKB(skb)->vcc = atm_vcc;
  1047. if (skb->len > (size_t) atm_vcc->qos.txtp.max_sdu) {
  1048. PRINTK (KERN_ERR, "sk_buff length greater than agreed max_sdu, dropping...");
  1049. return -EIO;
  1050. }
  1051. if (check_area (skb->data, skb->len)) {
  1052. atomic_inc(&atm_vcc->stats->tx_err);
  1053. return -ENOMEM; // ?
  1054. }
  1055. // allocate memory for fragments
  1056. tx_descr = kmalloc (sizeof(tx_simple), GFP_KERNEL);
  1057. if (!tx_descr) {
  1058. PRINTK (KERN_ERR, "could not allocate TX descriptor");
  1059. return -ENOMEM;
  1060. }
  1061. if (check_area (tx_descr, sizeof(tx_simple))) {
  1062. kfree (tx_descr);
  1063. return -ENOMEM;
  1064. }
  1065. PRINTD (DBG_TX, "fragment list allocated at %p", tx_descr);
  1066. tx_descr->skb = skb;
  1067. tx_descr->tx_frag.bytes = cpu_to_be32 (tx_len);
  1068. tx_descr->tx_frag.address = cpu_to_be32 (virt_to_bus (tx_data));
  1069. tx_descr->tx_frag_end.handle = virt_to_bus (tx_descr);
  1070. tx_descr->tx_frag_end.vc = 0;
  1071. tx_descr->tx_frag_end.next_descriptor_length = 0;
  1072. tx_descr->tx_frag_end.next_descriptor = 0;
  1073. #ifdef AMB_NEW_MICROCODE
  1074. tx_descr->tx_frag_end.cpcs_uu = 0;
  1075. tx_descr->tx_frag_end.cpi = 0;
  1076. tx_descr->tx_frag_end.pad = 0;
  1077. #endif
  1078. tx.vc = cpu_to_be16 (vcc->tx_frame_bits | vc);
  1079. tx.tx_descr_length = cpu_to_be16 (sizeof(tx_frag)+sizeof(tx_frag_end));
  1080. tx.tx_descr_addr = cpu_to_be32 (virt_to_bus (&tx_descr->tx_frag));
  1081. while (tx_give (dev, &tx))
  1082. schedule();
  1083. return 0;
  1084. }
  1085. /********** Change QoS on a VC **********/
  1086. // int amb_change_qos (struct atm_vcc * atm_vcc, struct atm_qos * qos, int flags);
  1087. /********** Free RX Socket Buffer **********/
  1088. #if 0
  1089. static void amb_free_rx_skb (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
  1090. amb_dev * dev = AMB_DEV (atm_vcc->dev);
  1091. amb_vcc * vcc = AMB_VCC (atm_vcc);
  1092. unsigned char pool = vcc->rx_info.pool;
  1093. rx_in rx;
  1094. // This may be unsafe for various reasons that I cannot really guess
  1095. // at. However, I note that the ATM layer calls kfree_skb rather
  1096. // than dev_kfree_skb at this point so we are least covered as far
  1097. // as buffer locking goes. There may be bugs if pcap clones RX skbs.
  1098. PRINTD (DBG_FLOW|DBG_SKB, "amb_rx_free skb %p (atm_vcc %p, vcc %p)",
  1099. skb, atm_vcc, vcc);
  1100. rx.handle = virt_to_bus (skb);
  1101. rx.host_address = cpu_to_be32 (virt_to_bus (skb->data));
  1102. skb->data = skb->head;
  1103. skb_reset_tail_pointer(skb);
  1104. skb->len = 0;
  1105. if (!rx_give (dev, &rx, pool)) {
  1106. // success
  1107. PRINTD (DBG_SKB|DBG_POOL, "recycled skb for pool %hu", pool);
  1108. return;
  1109. }
  1110. // just do what the ATM layer would have done
  1111. dev_kfree_skb_any (skb);
  1112. return;
  1113. }
  1114. #endif
  1115. /********** Proc File Output **********/
  1116. static int amb_proc_read (struct atm_dev * atm_dev, loff_t * pos, char * page) {
  1117. amb_dev * dev = AMB_DEV (atm_dev);
  1118. int left = *pos;
  1119. unsigned char pool;
  1120. PRINTD (DBG_FLOW, "amb_proc_read");
  1121. /* more diagnostics here? */
  1122. if (!left--) {
  1123. amb_stats * s = &dev->stats;
  1124. return sprintf (page,
  1125. "frames: TX OK %lu, RX OK %lu, RX bad %lu "
  1126. "(CRC %lu, long %lu, aborted %lu, unused %lu).\n",
  1127. s->tx_ok, s->rx.ok, s->rx.error,
  1128. s->rx.badcrc, s->rx.toolong,
  1129. s->rx.aborted, s->rx.unused);
  1130. }
  1131. if (!left--) {
  1132. amb_cq * c = &dev->cq;
  1133. return sprintf (page, "cmd queue [cur/hi/max]: %u/%u/%u. ",
  1134. c->pending, c->high, c->maximum);
  1135. }
  1136. if (!left--) {
  1137. amb_txq * t = &dev->txq;
  1138. return sprintf (page, "TX queue [cur/max high full]: %u/%u %u %u.\n",
  1139. t->pending, t->maximum, t->high, t->filled);
  1140. }
  1141. if (!left--) {
  1142. unsigned int count = sprintf (page, "RX queues [cur/max/req low empty]:");
  1143. for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
  1144. amb_rxq * r = &dev->rxq[pool];
  1145. count += sprintf (page+count, " %u/%u/%u %u %u",
  1146. r->pending, r->maximum, r->buffers_wanted, r->low, r->emptied);
  1147. }
  1148. count += sprintf (page+count, ".\n");
  1149. return count;
  1150. }
  1151. if (!left--) {
  1152. unsigned int count = sprintf (page, "RX buffer sizes:");
  1153. for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
  1154. amb_rxq * r = &dev->rxq[pool];
  1155. count += sprintf (page+count, " %u", r->buffer_size);
  1156. }
  1157. count += sprintf (page+count, ".\n");
  1158. return count;
  1159. }
  1160. #if 0
  1161. if (!left--) {
  1162. // suni block etc?
  1163. }
  1164. #endif
  1165. return 0;
  1166. }
  1167. /********** Operation Structure **********/
  1168. static const struct atmdev_ops amb_ops = {
  1169. .open = amb_open,
  1170. .close = amb_close,
  1171. .send = amb_send,
  1172. .proc_read = amb_proc_read,
  1173. .owner = THIS_MODULE,
  1174. };
  1175. /********** housekeeping **********/
  1176. static void do_housekeeping (struct timer_list *t) {
  1177. amb_dev * dev = from_timer(dev, t, housekeeping);
  1178. // could collect device-specific (not driver/atm-linux) stats here
  1179. // last resort refill once every ten seconds
  1180. fill_rx_pools (dev);
  1181. mod_timer(&dev->housekeeping, jiffies + 10*HZ);
  1182. return;
  1183. }
  1184. /********** creation of communication queues **********/
  1185. static int create_queues(amb_dev *dev, unsigned int cmds, unsigned int txs,
  1186. unsigned int *rxs, unsigned int *rx_buffer_sizes)
  1187. {
  1188. unsigned char pool;
  1189. size_t total = 0;
  1190. void * memory;
  1191. void * limit;
  1192. PRINTD (DBG_FLOW, "create_queues %p", dev);
  1193. total += cmds * sizeof(command);
  1194. total += txs * (sizeof(tx_in) + sizeof(tx_out));
  1195. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  1196. total += rxs[pool] * (sizeof(rx_in) + sizeof(rx_out));
  1197. memory = kmalloc (total, GFP_KERNEL);
  1198. if (!memory) {
  1199. PRINTK (KERN_ERR, "could not allocate queues");
  1200. return -ENOMEM;
  1201. }
  1202. if (check_area (memory, total)) {
  1203. PRINTK (KERN_ERR, "queues allocated in nasty area");
  1204. kfree (memory);
  1205. return -ENOMEM;
  1206. }
  1207. limit = memory + total;
  1208. PRINTD (DBG_INIT, "queues from %p to %p", memory, limit);
  1209. PRINTD (DBG_CMD, "command queue at %p", memory);
  1210. {
  1211. command * cmd = memory;
  1212. amb_cq * cq = &dev->cq;
  1213. cq->pending = 0;
  1214. cq->high = 0;
  1215. cq->maximum = cmds - 1;
  1216. cq->ptrs.start = cmd;
  1217. cq->ptrs.in = cmd;
  1218. cq->ptrs.out = cmd;
  1219. cq->ptrs.limit = cmd + cmds;
  1220. memory = cq->ptrs.limit;
  1221. }
  1222. PRINTD (DBG_TX, "TX queue pair at %p", memory);
  1223. {
  1224. tx_in * in = memory;
  1225. tx_out * out;
  1226. amb_txq * txq = &dev->txq;
  1227. txq->pending = 0;
  1228. txq->high = 0;
  1229. txq->filled = 0;
  1230. txq->maximum = txs - 1;
  1231. txq->in.start = in;
  1232. txq->in.ptr = in;
  1233. txq->in.limit = in + txs;
  1234. memory = txq->in.limit;
  1235. out = memory;
  1236. txq->out.start = out;
  1237. txq->out.ptr = out;
  1238. txq->out.limit = out + txs;
  1239. memory = txq->out.limit;
  1240. }
  1241. PRINTD (DBG_RX, "RX queue pairs at %p", memory);
  1242. for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
  1243. rx_in * in = memory;
  1244. rx_out * out;
  1245. amb_rxq * rxq = &dev->rxq[pool];
  1246. rxq->buffer_size = rx_buffer_sizes[pool];
  1247. rxq->buffers_wanted = 0;
  1248. rxq->pending = 0;
  1249. rxq->low = rxs[pool] - 1;
  1250. rxq->emptied = 0;
  1251. rxq->maximum = rxs[pool] - 1;
  1252. rxq->in.start = in;
  1253. rxq->in.ptr = in;
  1254. rxq->in.limit = in + rxs[pool];
  1255. memory = rxq->in.limit;
  1256. out = memory;
  1257. rxq->out.start = out;
  1258. rxq->out.ptr = out;
  1259. rxq->out.limit = out + rxs[pool];
  1260. memory = rxq->out.limit;
  1261. }
  1262. if (memory == limit) {
  1263. return 0;
  1264. } else {
  1265. PRINTK (KERN_ERR, "bad queue alloc %p != %p (tell maintainer)", memory, limit);
  1266. kfree (limit - total);
  1267. return -ENOMEM;
  1268. }
  1269. }
  1270. /********** destruction of communication queues **********/
  1271. static void destroy_queues (amb_dev * dev) {
  1272. // all queues assumed empty
  1273. void * memory = dev->cq.ptrs.start;
  1274. // includes txq.in, txq.out, rxq[].in and rxq[].out
  1275. PRINTD (DBG_FLOW, "destroy_queues %p", dev);
  1276. PRINTD (DBG_INIT, "freeing queues at %p", memory);
  1277. kfree (memory);
  1278. return;
  1279. }
  1280. /********** basic loader commands and error handling **********/
  1281. // centisecond timeouts - guessing away here
  1282. static unsigned int command_timeouts [] = {
  1283. [host_memory_test] = 15,
  1284. [read_adapter_memory] = 2,
  1285. [write_adapter_memory] = 2,
  1286. [adapter_start] = 50,
  1287. [get_version_number] = 10,
  1288. [interrupt_host] = 1,
  1289. [flash_erase_sector] = 1,
  1290. [adap_download_block] = 1,
  1291. [adap_erase_flash] = 1,
  1292. [adap_run_in_iram] = 1,
  1293. [adap_end_download] = 1
  1294. };
  1295. static unsigned int command_successes [] = {
  1296. [host_memory_test] = COMMAND_PASSED_TEST,
  1297. [read_adapter_memory] = COMMAND_READ_DATA_OK,
  1298. [write_adapter_memory] = COMMAND_WRITE_DATA_OK,
  1299. [adapter_start] = COMMAND_COMPLETE,
  1300. [get_version_number] = COMMAND_COMPLETE,
  1301. [interrupt_host] = COMMAND_COMPLETE,
  1302. [flash_erase_sector] = COMMAND_COMPLETE,
  1303. [adap_download_block] = COMMAND_COMPLETE,
  1304. [adap_erase_flash] = COMMAND_COMPLETE,
  1305. [adap_run_in_iram] = COMMAND_COMPLETE,
  1306. [adap_end_download] = COMMAND_COMPLETE
  1307. };
  1308. static int decode_loader_result (loader_command cmd, u32 result)
  1309. {
  1310. int res;
  1311. const char *msg;
  1312. if (result == command_successes[cmd])
  1313. return 0;
  1314. switch (result) {
  1315. case BAD_COMMAND:
  1316. res = -EINVAL;
  1317. msg = "bad command";
  1318. break;
  1319. case COMMAND_IN_PROGRESS:
  1320. res = -ETIMEDOUT;
  1321. msg = "command in progress";
  1322. break;
  1323. case COMMAND_PASSED_TEST:
  1324. res = 0;
  1325. msg = "command passed test";
  1326. break;
  1327. case COMMAND_FAILED_TEST:
  1328. res = -EIO;
  1329. msg = "command failed test";
  1330. break;
  1331. case COMMAND_READ_DATA_OK:
  1332. res = 0;
  1333. msg = "command read data ok";
  1334. break;
  1335. case COMMAND_READ_BAD_ADDRESS:
  1336. res = -EINVAL;
  1337. msg = "command read bad address";
  1338. break;
  1339. case COMMAND_WRITE_DATA_OK:
  1340. res = 0;
  1341. msg = "command write data ok";
  1342. break;
  1343. case COMMAND_WRITE_BAD_ADDRESS:
  1344. res = -EINVAL;
  1345. msg = "command write bad address";
  1346. break;
  1347. case COMMAND_WRITE_FLASH_FAILURE:
  1348. res = -EIO;
  1349. msg = "command write flash failure";
  1350. break;
  1351. case COMMAND_COMPLETE:
  1352. res = 0;
  1353. msg = "command complete";
  1354. break;
  1355. case COMMAND_FLASH_ERASE_FAILURE:
  1356. res = -EIO;
  1357. msg = "command flash erase failure";
  1358. break;
  1359. case COMMAND_WRITE_BAD_DATA:
  1360. res = -EINVAL;
  1361. msg = "command write bad data";
  1362. break;
  1363. default:
  1364. res = -EINVAL;
  1365. msg = "unknown error";
  1366. PRINTD (DBG_LOAD|DBG_ERR,
  1367. "decode_loader_result got %d=%x !",
  1368. result, result);
  1369. break;
  1370. }
  1371. PRINTK (KERN_ERR, "%s", msg);
  1372. return res;
  1373. }
  1374. static int do_loader_command(volatile loader_block *lb, const amb_dev *dev,
  1375. loader_command cmd)
  1376. {
  1377. unsigned long timeout;
  1378. PRINTD (DBG_FLOW|DBG_LOAD, "do_loader_command");
  1379. /* do a command
  1380. Set the return value to zero, set the command type and set the
  1381. valid entry to the right magic value. The payload is already
  1382. correctly byte-ordered so we leave it alone. Hit the doorbell
  1383. with the bus address of this structure.
  1384. */
  1385. lb->result = 0;
  1386. lb->command = cpu_to_be32 (cmd);
  1387. lb->valid = cpu_to_be32 (DMA_VALID);
  1388. // dump_registers (dev);
  1389. // dump_loader_block (lb);
  1390. wr_mem (dev, offsetof(amb_mem, doorbell), virt_to_bus (lb) & ~onegigmask);
  1391. timeout = command_timeouts[cmd] * 10;
  1392. while (!lb->result || lb->result == cpu_to_be32 (COMMAND_IN_PROGRESS))
  1393. if (timeout) {
  1394. timeout = msleep_interruptible(timeout);
  1395. } else {
  1396. PRINTD (DBG_LOAD|DBG_ERR, "command %d timed out", cmd);
  1397. dump_registers (dev);
  1398. dump_loader_block (lb);
  1399. return -ETIMEDOUT;
  1400. }
  1401. if (cmd == adapter_start) {
  1402. // wait for start command to acknowledge...
  1403. timeout = 100;
  1404. while (rd_plain (dev, offsetof(amb_mem, doorbell)))
  1405. if (timeout) {
  1406. timeout = msleep_interruptible(timeout);
  1407. } else {
  1408. PRINTD (DBG_LOAD|DBG_ERR, "start command did not clear doorbell, res=%08x",
  1409. be32_to_cpu (lb->result));
  1410. dump_registers (dev);
  1411. return -ETIMEDOUT;
  1412. }
  1413. return 0;
  1414. } else {
  1415. return decode_loader_result (cmd, be32_to_cpu (lb->result));
  1416. }
  1417. }
  1418. /* loader: determine loader version */
  1419. static int get_loader_version(loader_block *lb, const amb_dev *dev,
  1420. u32 *version)
  1421. {
  1422. int res;
  1423. PRINTD (DBG_FLOW|DBG_LOAD, "get_loader_version");
  1424. res = do_loader_command (lb, dev, get_version_number);
  1425. if (res)
  1426. return res;
  1427. if (version)
  1428. *version = be32_to_cpu (lb->payload.version);
  1429. return 0;
  1430. }
  1431. /* loader: write memory data blocks */
  1432. static int loader_write(loader_block *lb, const amb_dev *dev,
  1433. const struct ihex_binrec *rec)
  1434. {
  1435. transfer_block * tb = &lb->payload.transfer;
  1436. PRINTD (DBG_FLOW|DBG_LOAD, "loader_write");
  1437. tb->address = rec->addr;
  1438. tb->count = cpu_to_be32(be16_to_cpu(rec->len) / 4);
  1439. memcpy(tb->data, rec->data, be16_to_cpu(rec->len));
  1440. return do_loader_command (lb, dev, write_adapter_memory);
  1441. }
  1442. /* loader: verify memory data blocks */
  1443. static int loader_verify(loader_block *lb, const amb_dev *dev,
  1444. const struct ihex_binrec *rec)
  1445. {
  1446. transfer_block * tb = &lb->payload.transfer;
  1447. int res;
  1448. PRINTD (DBG_FLOW|DBG_LOAD, "loader_verify");
  1449. tb->address = rec->addr;
  1450. tb->count = cpu_to_be32(be16_to_cpu(rec->len) / 4);
  1451. res = do_loader_command (lb, dev, read_adapter_memory);
  1452. if (!res && memcmp(tb->data, rec->data, be16_to_cpu(rec->len)))
  1453. res = -EINVAL;
  1454. return res;
  1455. }
  1456. /* loader: start microcode */
  1457. static int loader_start(loader_block *lb, const amb_dev *dev, u32 address)
  1458. {
  1459. PRINTD (DBG_FLOW|DBG_LOAD, "loader_start");
  1460. lb->payload.start = cpu_to_be32 (address);
  1461. return do_loader_command (lb, dev, adapter_start);
  1462. }
  1463. /********** reset card **********/
  1464. static inline void sf (const char * msg)
  1465. {
  1466. PRINTK (KERN_ERR, "self-test failed: %s", msg);
  1467. }
  1468. static int amb_reset (amb_dev * dev, int diags) {
  1469. u32 word;
  1470. PRINTD (DBG_FLOW|DBG_LOAD, "amb_reset");
  1471. word = rd_plain (dev, offsetof(amb_mem, reset_control));
  1472. // put card into reset state
  1473. wr_plain (dev, offsetof(amb_mem, reset_control), word | AMB_RESET_BITS);
  1474. // wait a short while
  1475. udelay (10);
  1476. #if 1
  1477. // put card into known good state
  1478. wr_plain (dev, offsetof(amb_mem, interrupt_control), AMB_DOORBELL_BITS);
  1479. // clear all interrupts just in case
  1480. wr_plain (dev, offsetof(amb_mem, interrupt), -1);
  1481. #endif
  1482. // clear self-test done flag
  1483. wr_plain (dev, offsetof(amb_mem, mb.loader.ready), 0);
  1484. // take card out of reset state
  1485. wr_plain (dev, offsetof(amb_mem, reset_control), word &~ AMB_RESET_BITS);
  1486. if (diags) {
  1487. unsigned long timeout;
  1488. // 4.2 second wait
  1489. msleep(4200);
  1490. // half second time-out
  1491. timeout = 500;
  1492. while (!rd_plain (dev, offsetof(amb_mem, mb.loader.ready)))
  1493. if (timeout) {
  1494. timeout = msleep_interruptible(timeout);
  1495. } else {
  1496. PRINTD (DBG_LOAD|DBG_ERR, "reset timed out");
  1497. return -ETIMEDOUT;
  1498. }
  1499. // get results of self-test
  1500. // XXX double check byte-order
  1501. word = rd_mem (dev, offsetof(amb_mem, mb.loader.result));
  1502. if (word & SELF_TEST_FAILURE) {
  1503. if (word & GPINT_TST_FAILURE)
  1504. sf ("interrupt");
  1505. if (word & SUNI_DATA_PATTERN_FAILURE)
  1506. sf ("SUNI data pattern");
  1507. if (word & SUNI_DATA_BITS_FAILURE)
  1508. sf ("SUNI data bits");
  1509. if (word & SUNI_UTOPIA_FAILURE)
  1510. sf ("SUNI UTOPIA interface");
  1511. if (word & SUNI_FIFO_FAILURE)
  1512. sf ("SUNI cell buffer FIFO");
  1513. if (word & SRAM_FAILURE)
  1514. sf ("bad SRAM");
  1515. // better return value?
  1516. return -EIO;
  1517. }
  1518. }
  1519. return 0;
  1520. }
  1521. /********** transfer and start the microcode **********/
  1522. static int ucode_init(loader_block *lb, amb_dev *dev)
  1523. {
  1524. const struct firmware *fw;
  1525. unsigned long start_address;
  1526. const struct ihex_binrec *rec;
  1527. const char *errmsg = NULL;
  1528. int res;
  1529. res = request_ihex_firmware(&fw, "atmsar11.fw", &dev->pci_dev->dev);
  1530. if (res) {
  1531. PRINTK (KERN_ERR, "Cannot load microcode data");
  1532. return res;
  1533. }
  1534. /* First record contains just the start address */
  1535. rec = (const struct ihex_binrec *)fw->data;
  1536. if (be16_to_cpu(rec->len) != sizeof(__be32) || be32_to_cpu(rec->addr)) {
  1537. errmsg = "no start record";
  1538. goto fail;
  1539. }
  1540. start_address = be32_to_cpup((__be32 *)rec->data);
  1541. rec = ihex_next_binrec(rec);
  1542. PRINTD (DBG_FLOW|DBG_LOAD, "ucode_init");
  1543. while (rec) {
  1544. PRINTD (DBG_LOAD, "starting region (%x, %u)", be32_to_cpu(rec->addr),
  1545. be16_to_cpu(rec->len));
  1546. if (be16_to_cpu(rec->len) > 4 * MAX_TRANSFER_DATA) {
  1547. errmsg = "record too long";
  1548. goto fail;
  1549. }
  1550. if (be16_to_cpu(rec->len) & 3) {
  1551. errmsg = "odd number of bytes";
  1552. goto fail;
  1553. }
  1554. res = loader_write(lb, dev, rec);
  1555. if (res)
  1556. break;
  1557. res = loader_verify(lb, dev, rec);
  1558. if (res)
  1559. break;
  1560. rec = ihex_next_binrec(rec);
  1561. }
  1562. release_firmware(fw);
  1563. if (!res)
  1564. res = loader_start(lb, dev, start_address);
  1565. return res;
  1566. fail:
  1567. release_firmware(fw);
  1568. PRINTK(KERN_ERR, "Bad microcode data (%s)", errmsg);
  1569. return -EINVAL;
  1570. }
  1571. /********** give adapter parameters **********/
  1572. static inline __be32 bus_addr(void * addr) {
  1573. return cpu_to_be32 (virt_to_bus (addr));
  1574. }
  1575. static int amb_talk(amb_dev *dev)
  1576. {
  1577. adap_talk_block a;
  1578. unsigned char pool;
  1579. unsigned long timeout;
  1580. PRINTD (DBG_FLOW, "amb_talk %p", dev);
  1581. a.command_start = bus_addr (dev->cq.ptrs.start);
  1582. a.command_end = bus_addr (dev->cq.ptrs.limit);
  1583. a.tx_start = bus_addr (dev->txq.in.start);
  1584. a.tx_end = bus_addr (dev->txq.in.limit);
  1585. a.txcom_start = bus_addr (dev->txq.out.start);
  1586. a.txcom_end = bus_addr (dev->txq.out.limit);
  1587. for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
  1588. // the other "a" items are set up by the adapter
  1589. a.rec_struct[pool].buffer_start = bus_addr (dev->rxq[pool].in.start);
  1590. a.rec_struct[pool].buffer_end = bus_addr (dev->rxq[pool].in.limit);
  1591. a.rec_struct[pool].rx_start = bus_addr (dev->rxq[pool].out.start);
  1592. a.rec_struct[pool].rx_end = bus_addr (dev->rxq[pool].out.limit);
  1593. a.rec_struct[pool].buffer_size = cpu_to_be32 (dev->rxq[pool].buffer_size);
  1594. }
  1595. #ifdef AMB_NEW_MICROCODE
  1596. // disable fast PLX prefetching
  1597. a.init_flags = 0;
  1598. #endif
  1599. // pass the structure
  1600. wr_mem (dev, offsetof(amb_mem, doorbell), virt_to_bus (&a));
  1601. // 2.2 second wait (must not touch doorbell during 2 second DMA test)
  1602. msleep(2200);
  1603. // give the adapter another half second?
  1604. timeout = 500;
  1605. while (rd_plain (dev, offsetof(amb_mem, doorbell)))
  1606. if (timeout) {
  1607. timeout = msleep_interruptible(timeout);
  1608. } else {
  1609. PRINTD (DBG_INIT|DBG_ERR, "adapter init timed out");
  1610. return -ETIMEDOUT;
  1611. }
  1612. return 0;
  1613. }
  1614. // get microcode version
  1615. static void amb_ucode_version(amb_dev *dev)
  1616. {
  1617. u32 major;
  1618. u32 minor;
  1619. command cmd;
  1620. cmd.request = cpu_to_be32 (SRB_GET_VERSION);
  1621. while (command_do (dev, &cmd)) {
  1622. set_current_state(TASK_UNINTERRUPTIBLE);
  1623. schedule();
  1624. }
  1625. major = be32_to_cpu (cmd.args.version.major);
  1626. minor = be32_to_cpu (cmd.args.version.minor);
  1627. PRINTK (KERN_INFO, "microcode version is %u.%u", major, minor);
  1628. }
  1629. // get end station address
  1630. static void amb_esi(amb_dev *dev, u8 *esi)
  1631. {
  1632. u32 lower4;
  1633. u16 upper2;
  1634. command cmd;
  1635. cmd.request = cpu_to_be32 (SRB_GET_BIA);
  1636. while (command_do (dev, &cmd)) {
  1637. set_current_state(TASK_UNINTERRUPTIBLE);
  1638. schedule();
  1639. }
  1640. lower4 = be32_to_cpu (cmd.args.bia.lower4);
  1641. upper2 = be32_to_cpu (cmd.args.bia.upper2);
  1642. PRINTD (DBG_LOAD, "BIA: lower4: %08x, upper2 %04x", lower4, upper2);
  1643. if (esi) {
  1644. unsigned int i;
  1645. PRINTDB (DBG_INIT, "ESI:");
  1646. for (i = 0; i < ESI_LEN; ++i) {
  1647. if (i < 4)
  1648. esi[i] = bitrev8(lower4>>(8*i));
  1649. else
  1650. esi[i] = bitrev8(upper2>>(8*(i-4)));
  1651. PRINTDM (DBG_INIT, " %02x", esi[i]);
  1652. }
  1653. PRINTDE (DBG_INIT, "");
  1654. }
  1655. return;
  1656. }
  1657. static void fixup_plx_window (amb_dev *dev, loader_block *lb)
  1658. {
  1659. // fix up the PLX-mapped window base address to match the block
  1660. unsigned long blb;
  1661. u32 mapreg;
  1662. blb = virt_to_bus(lb);
  1663. // the kernel stack had better not ever cross a 1Gb boundary!
  1664. mapreg = rd_plain (dev, offsetof(amb_mem, stuff[10]));
  1665. mapreg &= ~onegigmask;
  1666. mapreg |= blb & onegigmask;
  1667. wr_plain (dev, offsetof(amb_mem, stuff[10]), mapreg);
  1668. return;
  1669. }
  1670. static int amb_init(amb_dev *dev)
  1671. {
  1672. loader_block lb;
  1673. u32 version;
  1674. if (amb_reset (dev, 1)) {
  1675. PRINTK (KERN_ERR, "card reset failed!");
  1676. } else {
  1677. fixup_plx_window (dev, &lb);
  1678. if (get_loader_version (&lb, dev, &version)) {
  1679. PRINTK (KERN_INFO, "failed to get loader version");
  1680. } else {
  1681. PRINTK (KERN_INFO, "loader version is %08x", version);
  1682. if (ucode_init (&lb, dev)) {
  1683. PRINTK (KERN_ERR, "microcode failure");
  1684. } else if (create_queues (dev, cmds, txs, rxs, rxs_bs)) {
  1685. PRINTK (KERN_ERR, "failed to get memory for queues");
  1686. } else {
  1687. if (amb_talk (dev)) {
  1688. PRINTK (KERN_ERR, "adapter did not accept queues");
  1689. } else {
  1690. amb_ucode_version (dev);
  1691. return 0;
  1692. } /* amb_talk */
  1693. destroy_queues (dev);
  1694. } /* create_queues, ucode_init */
  1695. amb_reset (dev, 0);
  1696. } /* get_loader_version */
  1697. } /* amb_reset */
  1698. return -EINVAL;
  1699. }
  1700. static void setup_dev(amb_dev *dev, struct pci_dev *pci_dev)
  1701. {
  1702. unsigned char pool;
  1703. // set up known dev items straight away
  1704. dev->pci_dev = pci_dev;
  1705. pci_set_drvdata(pci_dev, dev);
  1706. dev->iobase = pci_resource_start (pci_dev, 1);
  1707. dev->irq = pci_dev->irq;
  1708. dev->membase = bus_to_virt(pci_resource_start(pci_dev, 0));
  1709. // flags (currently only dead)
  1710. dev->flags = 0;
  1711. // Allocate cell rates (fibre)
  1712. // ATM_OC3_PCR = 1555200000/8/270*260/53 - 29/53
  1713. // to be really pedantic, this should be ATM_OC3c_PCR
  1714. dev->tx_avail = ATM_OC3_PCR;
  1715. dev->rx_avail = ATM_OC3_PCR;
  1716. // semaphore for txer/rxer modifications - we cannot use a
  1717. // spinlock as the critical region needs to switch processes
  1718. mutex_init(&dev->vcc_sf);
  1719. // queue manipulation spinlocks; we want atomic reads and
  1720. // writes to the queue descriptors (handles IRQ and SMP)
  1721. // consider replacing "int pending" -> "atomic_t available"
  1722. // => problem related to who gets to move queue pointers
  1723. spin_lock_init (&dev->cq.lock);
  1724. spin_lock_init (&dev->txq.lock);
  1725. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  1726. spin_lock_init (&dev->rxq[pool].lock);
  1727. }
  1728. static void setup_pci_dev(struct pci_dev *pci_dev)
  1729. {
  1730. unsigned char lat;
  1731. // enable bus master accesses
  1732. pci_set_master(pci_dev);
  1733. // frobnicate latency (upwards, usually)
  1734. pci_read_config_byte (pci_dev, PCI_LATENCY_TIMER, &lat);
  1735. if (!pci_lat)
  1736. pci_lat = (lat < MIN_PCI_LATENCY) ? MIN_PCI_LATENCY : lat;
  1737. if (lat != pci_lat) {
  1738. PRINTK (KERN_INFO, "Changing PCI latency timer from %hu to %hu",
  1739. lat, pci_lat);
  1740. pci_write_config_byte(pci_dev, PCI_LATENCY_TIMER, pci_lat);
  1741. }
  1742. }
  1743. static int amb_probe(struct pci_dev *pci_dev,
  1744. const struct pci_device_id *pci_ent)
  1745. {
  1746. amb_dev * dev;
  1747. int err;
  1748. unsigned int irq;
  1749. err = pci_enable_device(pci_dev);
  1750. if (err < 0) {
  1751. PRINTK (KERN_ERR, "skipped broken (PLX rev 2) card");
  1752. goto out;
  1753. }
  1754. // read resources from PCI configuration space
  1755. irq = pci_dev->irq;
  1756. if (pci_dev->device == PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD) {
  1757. PRINTK (KERN_ERR, "skipped broken (PLX rev 2) card");
  1758. err = -EINVAL;
  1759. goto out_disable;
  1760. }
  1761. PRINTD (DBG_INFO, "found Madge ATM adapter (amb) at"
  1762. " IO %llx, IRQ %u, MEM %p",
  1763. (unsigned long long)pci_resource_start(pci_dev, 1),
  1764. irq, bus_to_virt(pci_resource_start(pci_dev, 0)));
  1765. // check IO region
  1766. err = pci_request_region(pci_dev, 1, DEV_LABEL);
  1767. if (err < 0) {
  1768. PRINTK (KERN_ERR, "IO range already in use!");
  1769. goto out_disable;
  1770. }
  1771. dev = kzalloc(sizeof(amb_dev), GFP_KERNEL);
  1772. if (!dev) {
  1773. PRINTK (KERN_ERR, "out of memory!");
  1774. err = -ENOMEM;
  1775. goto out_release;
  1776. }
  1777. setup_dev(dev, pci_dev);
  1778. err = amb_init(dev);
  1779. if (err < 0) {
  1780. PRINTK (KERN_ERR, "adapter initialisation failure");
  1781. goto out_free;
  1782. }
  1783. setup_pci_dev(pci_dev);
  1784. // grab (but share) IRQ and install handler
  1785. err = request_irq(irq, interrupt_handler, IRQF_SHARED, DEV_LABEL, dev);
  1786. if (err < 0) {
  1787. PRINTK (KERN_ERR, "request IRQ failed!");
  1788. goto out_reset;
  1789. }
  1790. dev->atm_dev = atm_dev_register (DEV_LABEL, &pci_dev->dev, &amb_ops, -1,
  1791. NULL);
  1792. if (!dev->atm_dev) {
  1793. PRINTD (DBG_ERR, "failed to register Madge ATM adapter");
  1794. err = -EINVAL;
  1795. goto out_free_irq;
  1796. }
  1797. PRINTD (DBG_INFO, "registered Madge ATM adapter (no. %d) (%p) at %p",
  1798. dev->atm_dev->number, dev, dev->atm_dev);
  1799. dev->atm_dev->dev_data = (void *) dev;
  1800. // register our address
  1801. amb_esi (dev, dev->atm_dev->esi);
  1802. // 0 bits for vpi, 10 bits for vci
  1803. dev->atm_dev->ci_range.vpi_bits = NUM_VPI_BITS;
  1804. dev->atm_dev->ci_range.vci_bits = NUM_VCI_BITS;
  1805. timer_setup(&dev->housekeeping, do_housekeeping, 0);
  1806. mod_timer(&dev->housekeeping, jiffies);
  1807. // enable host interrupts
  1808. interrupts_on (dev);
  1809. out:
  1810. return err;
  1811. out_free_irq:
  1812. free_irq(irq, dev);
  1813. out_reset:
  1814. amb_reset(dev, 0);
  1815. out_free:
  1816. kfree(dev);
  1817. out_release:
  1818. pci_release_region(pci_dev, 1);
  1819. out_disable:
  1820. pci_disable_device(pci_dev);
  1821. goto out;
  1822. }
  1823. static void amb_remove_one(struct pci_dev *pci_dev)
  1824. {
  1825. struct amb_dev *dev;
  1826. dev = pci_get_drvdata(pci_dev);
  1827. PRINTD(DBG_INFO|DBG_INIT, "closing %p (atm_dev = %p)", dev, dev->atm_dev);
  1828. del_timer_sync(&dev->housekeeping);
  1829. // the drain should not be necessary
  1830. drain_rx_pools(dev);
  1831. interrupts_off(dev);
  1832. amb_reset(dev, 0);
  1833. free_irq(dev->irq, dev);
  1834. pci_disable_device(pci_dev);
  1835. destroy_queues(dev);
  1836. atm_dev_deregister(dev->atm_dev);
  1837. kfree(dev);
  1838. pci_release_region(pci_dev, 1);
  1839. }
  1840. static void __init amb_check_args (void) {
  1841. unsigned char pool;
  1842. unsigned int max_rx_size;
  1843. #ifdef DEBUG_AMBASSADOR
  1844. PRINTK (KERN_NOTICE, "debug bitmap is %hx", debug &= DBG_MASK);
  1845. #else
  1846. if (debug)
  1847. PRINTK (KERN_NOTICE, "no debugging support");
  1848. #endif
  1849. if (cmds < MIN_QUEUE_SIZE)
  1850. PRINTK (KERN_NOTICE, "cmds has been raised to %u",
  1851. cmds = MIN_QUEUE_SIZE);
  1852. if (txs < MIN_QUEUE_SIZE)
  1853. PRINTK (KERN_NOTICE, "txs has been raised to %u",
  1854. txs = MIN_QUEUE_SIZE);
  1855. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  1856. if (rxs[pool] < MIN_QUEUE_SIZE)
  1857. PRINTK (KERN_NOTICE, "rxs[%hu] has been raised to %u",
  1858. pool, rxs[pool] = MIN_QUEUE_SIZE);
  1859. // buffers sizes should be greater than zero and strictly increasing
  1860. max_rx_size = 0;
  1861. for (pool = 0; pool < NUM_RX_POOLS; ++pool)
  1862. if (rxs_bs[pool] <= max_rx_size)
  1863. PRINTK (KERN_NOTICE, "useless pool (rxs_bs[%hu] = %u)",
  1864. pool, rxs_bs[pool]);
  1865. else
  1866. max_rx_size = rxs_bs[pool];
  1867. if (rx_lats < MIN_RX_BUFFERS)
  1868. PRINTK (KERN_NOTICE, "rx_lats has been raised to %u",
  1869. rx_lats = MIN_RX_BUFFERS);
  1870. return;
  1871. }
  1872. /********** module stuff **********/
  1873. MODULE_AUTHOR(maintainer_string);
  1874. MODULE_DESCRIPTION(description_string);
  1875. MODULE_LICENSE("GPL");
  1876. MODULE_FIRMWARE("atmsar11.fw");
  1877. module_param(debug, ushort, 0644);
  1878. module_param(cmds, uint, 0);
  1879. module_param(txs, uint, 0);
  1880. module_param_array(rxs, uint, NULL, 0);
  1881. module_param_array(rxs_bs, uint, NULL, 0);
  1882. module_param(rx_lats, uint, 0);
  1883. module_param(pci_lat, byte, 0);
  1884. MODULE_PARM_DESC(debug, "debug bitmap, see .h file");
  1885. MODULE_PARM_DESC(cmds, "number of command queue entries");
  1886. MODULE_PARM_DESC(txs, "number of TX queue entries");
  1887. MODULE_PARM_DESC(rxs, "number of RX queue entries [" __MODULE_STRING(NUM_RX_POOLS) "]");
  1888. MODULE_PARM_DESC(rxs_bs, "size of RX buffers [" __MODULE_STRING(NUM_RX_POOLS) "]");
  1889. MODULE_PARM_DESC(rx_lats, "number of extra buffers to cope with RX latencies");
  1890. MODULE_PARM_DESC(pci_lat, "PCI latency in bus cycles");
  1891. /********** module entry **********/
  1892. static const struct pci_device_id amb_pci_tbl[] = {
  1893. { PCI_VDEVICE(MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR), 0 },
  1894. { PCI_VDEVICE(MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD), 0 },
  1895. { 0, }
  1896. };
  1897. MODULE_DEVICE_TABLE(pci, amb_pci_tbl);
  1898. static struct pci_driver amb_driver = {
  1899. .name = "amb",
  1900. .probe = amb_probe,
  1901. .remove = amb_remove_one,
  1902. .id_table = amb_pci_tbl,
  1903. };
  1904. static int __init amb_module_init (void)
  1905. {
  1906. PRINTD (DBG_FLOW|DBG_INIT, "init_module");
  1907. BUILD_BUG_ON(sizeof(amb_mem) != 4*16 + 4*12);
  1908. show_version();
  1909. amb_check_args();
  1910. // get the juice
  1911. return pci_register_driver(&amb_driver);
  1912. }
  1913. /********** module exit **********/
  1914. static void __exit amb_module_exit (void)
  1915. {
  1916. PRINTD (DBG_FLOW|DBG_INIT, "cleanup_module");
  1917. pci_unregister_driver(&amb_driver);
  1918. }
  1919. module_init(amb_module_init);
  1920. module_exit(amb_module_exit);