Import 2.4.0-test3pre6
[davej-history.git] / drivers / atm / ambassador.c
blobb860253d3ac1fc56cb1d9005a5ba3d3e2d079faf
1 /*
2 Madge Ambassador ATM Adapter driver.
3 Copyright (C) 1995-1999 Madge Networks Ltd.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 The GNU GPL is contained in /usr/doc/copyright/GPL on a Debian
20 system and in the file COPYING in the Linux kernel source.
23 /* * dedicated to the memory of Graham Gordon 1971-1998 * */
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/ioport.h>
31 #include <linux/atmdev.h>
32 #include <linux/delay.h>
33 #include <linux/interrupt.h>
35 #include <asm/atomic.h>
36 #include <asm/io.h>
37 #include <asm/byteorder.h>
39 #include "ambassador.h"
41 #define maintainer_string "Giuliano Procida at Madge Networks <gprocida@madge.com>"
42 #define description_string "Madge ATM Ambassador driver"
43 #define version_string "1.2.4"
45 static inline void __init show_version (void) {
46 printk ("%s version %s\n", description_string, version_string);
51 Theory of Operation
53 I Hardware, detection, initialisation and shutdown.
55 1. Supported Hardware
57 This driver is for the PCI ATMizer-based Ambassador card (except
58 very early versions). It is not suitable for the similar EISA "TR7"
59 card. Commercially, both cards are known as Collage Server ATM
60 adapters.
62 The loader supports image transfer to the card, image start and few
63 other miscellaneous commands.
65 Only AAL5 is supported with vpi = 0 and vci in the range 0 to 1023.
67 The cards are big-endian.
69 2. Detection
71 Standard PCI stuff, the early cards are detected and rejected.
73 3. Initialisation
75 The cards are reset and the self-test results are checked. The
76 microcode image is then transferred and started. This waits for a
77 pointer to a descriptor containing details of the host-based queues
78 and buffers and various parameters etc. Once they are processed
79 normal operations may begin. The BIA is read using a microcode
80 command.
82 4. Shutdown
84 This may be accomplished either by a card reset or via the microcode
85 shutdown command. Further investigation required.
87 5. Persistent state
89 The card reset does not affect PCI configuration (good) or the
90 contents of several other "shared run-time registers" (bad) which
91 include doorbell and interrupt control as well as EEPROM and PCI
92 control. The driver must be careful when modifying these registers
93 not to touch bits it does not use and to undo any changes at exit.
95 II Driver software
97 0. Generalities
99 The adapter is quite intelligent (fast) and has a simple interface
100 (few features). VPI is always zero, 1024 VCIs are supported. There
101 is limited cell rate support. UBR channels can be capped and ABR
102 (explicit rate, but not EFCI) is supported. There is no CBR or VBR
103 support.
105 1. Driver <-> Adapter Communication
107 Apart from the basic loader commands, the driver communicates
108 through three entities: the command queue (CQ), the transmit queue
109 pair (TXQ) and the receive queue pairs (RXQ). These three entities
110 are set up by the host and passed to the microcode just after it has
111 been started.
113 All queues are host-based circular queues. They are contiguous and
114 (due to hardware limitations) have some restrictions as to their
115 locations in (bus) memory. They are of the "full means the same as
116 empty so don't do that" variety since the adapter uses pointers
117 internally.
119 The queue pairs work as follows: one queue is for supply to the
120 adapter, items in it are pending and are owned by the adapter; the
121 other is the queue for return from the adapter, items in it have
122 been dealt with by the adapter. The host adds items to the supply
123 (TX descriptors and free RX buffer descriptors) and removes items
124 from the return (TX and RX completions). The adapter deals with out
125 of order completions.
127 Interrupts (card to host) and the doorbell (host to card) are used
128 for signalling.
130 1. CQ
132 This is to communicate "open VC", "close VC", "get stats" etc. to
133 the adapter. At most one command is retired every millisecond by the
134 card. There is no out of order completion or notification. The
135 driver needs to check the return code of the command, waiting as
136 appropriate.
138 2. TXQ
140 TX supply items are of variable length (scatter gather support) and
141 so the queue items are (more or less) pointers to the real thing.
142 Each TX supply item contains a unique, host-supplied handle (the skb
143 bus address seems most sensible as this works for Alphas as well,
144 there is no need to do any endian conversions on the handles).
146 TX return items consist of just the handles above.
148 3. RXQ (up to 4 of these with different lengths and buffer sizes)
150 RX supply items consist of a unique, host-supplied handle (the skb
151 bus address again) and a pointer to the buffer data area.
153 RX return items consist of the handle above, the VC, length and a
154 status word. This just screams "oh so easy" doesn't it?
156 Note on RX pool sizes:
158 Each pool should have enough buffers to handle a back-to-back stream
159 of minimum sized frames on a single VC. For example:
161 frame spacing = 3us (about right)
163 delay = IRQ lat + RX handling + RX buffer replenish = 20 (us) (a guess)
165 min number of buffers for one VC = 1 + delay/spacing (buffers)
167 delay/spacing = latency = (20+2)/3 = 7 (buffers) (rounding up)
169 The 20us delay assumes that there is no need to sleep; if we need to
170 sleep to get buffers we are going to drop frames anyway.
172 In fact, each pool should have enough buffers to support the
173 simultaneous reassembly of a separate frame on each VC and cope with
174 the case in which frames complete in round robin cell fashion on
175 each VC.
177 Only one frame can complete at each cell arrival, so if "n" VCs are
178 open, the worst case is to have them all complete frames together
179 followed by all starting new frames together.
181 desired number of buffers = n + delay/spacing
183 These are the extreme requirements, however, they are "n+k" for some
184 "k" so we have only the constant to choose. This is the argument
185 rx_lats which current defaults to 7.
187 Actually, "n ? n+k : 0" is better and this is what is implemented,
188 subject to the limit given by the pool size.
190 4. Driver locking
192 Simple spinlocks are used around the TX and RX queue mechanisms.
193 Anyone with a faster, working method is welcome to implement it.
195 The adapter command queue is protected with a spinlock. We always
196 wait for commands to complete.
198 A more complex form of locking is used around parts of the VC open
199 and close functions. There are three reasons for a lock: 1. we need
200 to do atomic rate reservation and release (not used yet), 2. Opening
201 sometimes involves two adapter commands which must not be separated
202 by another command on the same VC, 3. the changes to RX pool size
203 must be atomic. The lock needs to work over context switches, so we
204 use a semaphore.
206 III Hardware Features and Microcode Bugs
208 1. Byte Ordering
210 *%^"$&%^$*&^"$(%^$#&^%$(&#%$*(&^#%!"!"!*!
212 2. Memory access
214 All structures that are not accessed using DMA must be 4-byte
215 aligned (not a problem) and must not cross 4MB boundaries.
217 There is a DMA memory hole at E0000000-E00000FF (groan).
219 TX fragments (DMA read) must not cross 4MB boundaries (would be 16MB
220 but for a hardware bug).
222 RX buffers (DMA write) must not cross 16MB boundaries and must
223 include spare trailing bytes up to the next 4-byte boundary; they
224 will be written with rubbish.
226 The PLX likes to prefetch; if reading up to 4 u32 past the end of
227 each TX fragment is not a problem, then TX can be made to go a
228 little faster by passing a flag at init that disables a prefetch
229 workaround. We do not pass this flag. (new microcode only)
231 Now we:
232 . Note that alloc_skb rounds up size to a 16byte boundary.
233 . Ensure all areas do not traverse 4MB boundaries.
234 . Ensure all areas do not start at a E00000xx bus address.
235 (I cannot be certain, but this may always hold with Linux)
236 . Make all failures cause a loud message.
237 . Discard non-conforming SKBs (causes TX failure or RX fill delay).
238 . Discard non-conforming TX fragment descriptors (the TX fails).
239 In the future we could:
240 . Allow RX areas that traverse 4MB (but not 16MB) boundaries.
241 . Segment TX areas into some/more fragments, when necessary.
242 . Relax checks for non-DMA items (ignore hole).
243 . Give scatter-gather (iovec) requirements using ???. (?)
245 3. VC close is broken (only for new microcode)
247 The VC close adapter microcode command fails to do anything if any
248 frames have been received on the VC but none have been transmitted.
249 Frames continue to be reassembled and passed (with IRQ) to the
250 driver.
252 IV To Do List
254 . Fix bugs!
256 . Timer code may be broken.
258 . Deal with buggy VC close (somehow) in microcode 12.
260 . Handle interrupted and/or non-blocking writes - is this a job for
261 the protocol layer?
263 . Add code to break up TX fragments when they span 4MB boundaries.
265 . Add SUNI phy layer (need to know where SUNI lives on card).
267 . Implement a tx_alloc fn to (a) satisfy TX alignment etc. and (b)
268 leave extra headroom space for Ambassador TX descriptors.
270 . Understand these elements of struct atm_vcc: recvq (proto?),
271 sleep, callback, listenq, backlog_quota, reply and user_back.
273 . Adjust TX/RX skb allocation to favour IP with LANE/CLIP (configurable).
275 . Impose a TX-pending limit (2?) on each VC, help avoid TX q overflow.
277 . Decide whether RX buffer recycling is or can be made completely safe;
278 turn it back on. It looks like Werner is going to axe this.
280 . Implement QoS changes on open VCs (involves extracting parts of VC open
281 and close into separate functions and using them to make changes).
283 . Hack on command queue so that someone can issue multiple commands and wait
284 on the last one (OR only "no-op" or "wait" commands are waited for).
286 . Eliminate need for while-schedule around do_command.
290 /********** microcode **********/
292 #ifdef AMB_NEW_MICROCODE
293 #define UCODE(x) UCODE1(atmsar12.,x)
294 #else
295 #define UCODE(x) UCODE1(atmsar11.,x)
296 #endif
297 #define UCODE2(x) #x
298 #define UCODE1(x,y) UCODE2(x ## y)
300 static const u32 __initdata ucode_start =
301 #include UCODE(start)
304 static const region __initdata ucode_regions[] = {
305 #include UCODE(regions)
306 { 0, 0 }
309 static const u32 __initdata ucode_data[] = {
310 #include UCODE(data)
311 0xdeadbeef
314 /********** globals **********/
316 static amb_dev * amb_devs = NULL;
317 static struct timer_list housekeeping;
319 static unsigned short debug = 0;
320 static unsigned int cmds = 8;
321 static unsigned int txs = 32;
322 static unsigned int rxs[NUM_RX_POOLS] = { 64, 64, 64, 64 };
323 static unsigned int rxs_bs[NUM_RX_POOLS] = { 4080, 12240, 36720, 65535 };
324 static unsigned int rx_lats = 7;
325 static unsigned char pci_lat = 0;
327 static const unsigned long onegigmask = -1 << 30;
329 /********** access to adapter **********/
331 static inline void wr_plain (const amb_dev * dev, size_t addr, u32 data) {
332 PRINTD (DBG_FLOW|DBG_REGS, "wr: %08x <- %08x", addr, data);
333 #ifdef AMB_MMIO
334 dev->membase[addr / sizeof(u32)] = data;
335 #else
336 outl (data, dev->iobase + addr);
337 #endif
340 static inline u32 rd_plain (const amb_dev * dev, size_t addr) {
341 #ifdef AMB_MMIO
342 u32 data = dev->membase[addr / sizeof(u32)];
343 #else
344 u32 data = inl (dev->iobase + addr);
345 #endif
346 PRINTD (DBG_FLOW|DBG_REGS, "rd: %08x -> %08x", addr, data);
347 return data;
350 static inline void wr_mem (const amb_dev * dev, size_t addr, u32 data) {
351 u32 be = cpu_to_be32 (data);
352 PRINTD (DBG_FLOW|DBG_REGS, "wr: %08x <- %08x b[%08x]", addr, data, be);
353 #ifdef AMB_MMIO
354 dev->membase[addr / sizeof(u32)] = be;
355 #else
356 outl (be, dev->iobase + addr);
357 #endif
360 static inline u32 rd_mem (const amb_dev * dev, size_t addr) {
361 #ifdef AMB_MMIO
362 u32 be = dev->membase[addr / sizeof(u32)];
363 #else
364 u32 be = inl (dev->iobase + addr);
365 #endif
366 u32 data = be32_to_cpu (be);
367 PRINTD (DBG_FLOW|DBG_REGS, "rd: %08x -> %08x b[%08x]", addr, data, be);
368 return data;
371 /********** dump routines **********/
373 static inline void dump_registers (const amb_dev * dev) {
374 #ifdef DEBUG_AMBASSADOR
375 if (debug & DBG_REGS) {
376 size_t i;
377 PRINTD (DBG_REGS, "reading PLX control: ");
378 for (i = 0x00; i < 0x30; i += sizeof(u32))
379 rd_mem (dev, i);
380 PRINTD (DBG_REGS, "reading mailboxes: ");
381 for (i = 0x40; i < 0x60; i += sizeof(u32))
382 rd_mem (dev, i);
383 PRINTD (DBG_REGS, "reading doorb irqev irqen reset:");
384 for (i = 0x60; i < 0x70; i += sizeof(u32))
385 rd_mem (dev, i);
387 #else
388 (void) dev;
389 #endif
390 return;
393 static inline void dump_loader_block (volatile loader_block * lb) {
394 #ifdef DEBUG_AMBASSADOR
395 unsigned int i;
396 PRINTDB (DBG_LOAD, "lb @ %p; res: %d, cmd: %d, pay:",
397 lb, be32_to_cpu (lb->result), be32_to_cpu (lb->command));
398 for (i = 0; i < MAX_COMMAND_DATA; ++i)
399 PRINTDM (DBG_LOAD, " %08x", be32_to_cpu (lb->payload.data[i]));
400 PRINTDE (DBG_LOAD, ", vld: %08x", be32_to_cpu (lb->valid));
401 #else
402 (void) lb;
403 #endif
404 return;
407 static inline void dump_command (command * cmd) {
408 #ifdef DEBUG_AMBASSADOR
409 unsigned int i;
410 PRINTDB (DBG_CMD, "cmd @ %p, req: %08x, pars:",
411 cmd, /*be32_to_cpu*/ (cmd->request));
412 for (i = 0; i < 3; ++i)
413 PRINTDM (DBG_CMD, " %08x", /*be32_to_cpu*/ (cmd->args.par[i]));
414 PRINTDE (DBG_CMD, "");
415 #else
416 (void) cmd;
417 #endif
418 return;
421 static inline void dump_skb (char * prefix, unsigned int vc, struct sk_buff * skb) {
422 #ifdef DEBUG_AMBASSADOR
423 unsigned int i;
424 unsigned char * data = skb->data;
425 PRINTDB (DBG_DATA, "%s(%u) ", prefix, vc);
426 for (i=0; i<skb->len && i < 256;i++)
427 PRINTDM (DBG_DATA, "%02x ", data[i]);
428 PRINTDE (DBG_DATA,"");
429 #else
430 (void) prefix;
431 (void) vc;
432 (void) skb;
433 #endif
434 return;
437 /********** check memory areas for use by Ambassador **********/
439 /* see limitations under Hardware Features */
441 static inline int check_area (void * start, size_t length) {
442 // assumes length > 0
443 const u32 fourmegmask = -1 << 22;
444 const u32 twofivesixmask = -1 << 8;
445 const u32 starthole = 0xE0000000;
446 u32 startaddress = virt_to_bus (start);
447 u32 lastaddress = startaddress+length-1;
448 if ((startaddress ^ lastaddress) & fourmegmask ||
449 (startaddress & twofivesixmask) == starthole) {
450 PRINTK (KERN_ERR, "check_area failure: [%x,%x] - mail maintainer!",
451 startaddress, lastaddress);
452 return -1;
453 } else {
454 return 0;
458 /********** free an skb (as per ATM device driver documentation) **********/
460 static inline void amb_kfree_skb (struct sk_buff * skb) {
461 if (ATM_SKB(skb)->vcc->pop) {
462 ATM_SKB(skb)->vcc->pop (ATM_SKB(skb)->vcc, skb);
463 } else {
464 dev_kfree_skb_any (skb);
468 /********** TX completion **********/
470 static inline void tx_complete (amb_dev * dev, tx_out * tx) {
471 tx_simple * tx_descr = bus_to_virt (tx->handle);
472 struct sk_buff * skb = tx_descr->skb;
474 PRINTD (DBG_FLOW|DBG_TX, "tx_complete %p %p", dev, tx);
476 // VC layer stats
477 atomic_inc(&ATM_SKB(skb)->vcc->stats->tx);
479 // free the descriptor
480 kfree (tx_descr);
482 // free the skb
483 amb_kfree_skb (skb);
485 dev->stats.tx_ok++;
486 return;
489 /********** RX completion **********/
491 static void rx_complete (amb_dev * dev, rx_out * rx) {
492 struct sk_buff * skb = bus_to_virt (rx->handle);
493 u16 vc = be16_to_cpu (rx->vc);
494 // unused: u16 lec_id = be16_to_cpu (rx->lec_id);
495 u16 status = be16_to_cpu (rx->status);
496 u16 rx_len = be16_to_cpu (rx->length);
498 PRINTD (DBG_FLOW|DBG_RX, "rx_complete %p %p (len=%hu)", dev, rx, rx_len);
500 // XXX move this in and add to VC stats ???
501 if (!status) {
502 struct atm_vcc * atm_vcc = dev->rxer[vc];
503 dev->stats.rx.ok++;
505 if (atm_vcc) {
507 if (rx_len <= atm_vcc->qos.rxtp.max_sdu) {
509 if (atm_charge (atm_vcc, skb->truesize)) {
511 // prepare socket buffer
512 ATM_SKB(skb)->vcc = atm_vcc;
513 skb_put (skb, rx_len);
515 dump_skb ("<<<", vc, skb);
517 // VC layer stats
518 atomic_inc(&atm_vcc->stats->rx);
519 skb->stamp = xtime;
520 // end of our responsability
521 atm_vcc->push (atm_vcc, skb);
522 return;
524 } else {
525 // someone fix this (message), please!
526 PRINTD (DBG_INFO|DBG_RX, "dropped thanks to atm_charge (vc %hu, truesize %u)", vc, skb->truesize);
527 // drop stats incremented in atm_charge
530 } else {
531 PRINTK (KERN_INFO, "dropped over-size frame");
532 // should we count this?
533 atomic_inc(&atm_vcc->stats->rx_drop);
536 } else {
537 PRINTD (DBG_WARN|DBG_RX, "got frame but RX closed for channel %hu", vc);
538 // this is an adapter bug, only in new version of microcode
541 } else {
542 dev->stats.rx.error++;
543 if (status & CRC_ERR)
544 dev->stats.rx.badcrc++;
545 if (status & LEN_ERR)
546 dev->stats.rx.toolong++;
547 if (status & ABORT_ERR)
548 dev->stats.rx.aborted++;
549 if (status & UNUSED_ERR)
550 dev->stats.rx.unused++;
553 dev_kfree_skb_any (skb);
554 return;
559 Note on queue handling.
561 Here "give" and "take" refer to queue entries and a queue (pair)
562 rather than frames to or from the host or adapter. Empty frame
563 buffers are given to the RX queue pair and returned unused or
564 containing RX frames. TX frames (well, pointers to TX fragment
565 lists) are given to the TX queue pair, completions are returned.
569 /********** command queue **********/
571 // I really don't like this, but it's the best I can do at the moment
573 // also, the callers are responsible for byte order as the microcode
574 // sometimes does 16-bit accesses (yuk yuk yuk)
576 static int command_do (amb_dev * dev, command * cmd) {
577 amb_cq * cq = &dev->cq;
578 volatile amb_cq_ptrs * ptrs = &cq->ptrs;
579 command * my_slot;
580 unsigned long timeout;
582 PRINTD (DBG_FLOW|DBG_CMD, "command_do %p", dev);
584 if (test_bit (dead, &dev->flags))
585 return 0;
587 spin_lock (&cq->lock);
589 // if not full...
590 if (cq->pending < cq->maximum) {
591 // remember my slot for later
592 my_slot = ptrs->in;
593 PRINTD (DBG_CMD, "command in slot %p", my_slot);
595 dump_command (cmd);
597 // copy command in
598 *ptrs->in = *cmd;
599 cq->pending++;
600 ptrs->in = NEXTQ (ptrs->in, ptrs->start, ptrs->limit);
602 // mail the command
603 wr_mem (dev, offsetof(amb_mem, mb.adapter.cmd_address), virt_to_bus (ptrs->in));
605 // prepare to wait for cq->pending milliseconds
606 // effectively one centisecond on i386
607 timeout = (cq->pending*HZ+999)/1000;
609 if (cq->pending > cq->high)
610 cq->high = cq->pending;
611 spin_unlock (&cq->lock);
613 while (timeout) {
614 // go to sleep
615 // PRINTD (DBG_CMD, "wait: sleeping %lu for command", timeout);
616 timeout = schedule_timeout (timeout);
617 // woken up by timeout or signal
620 // wait for my slot to be reached (all waiters are here or above, until...)
621 while (ptrs->out != my_slot) {
622 PRINTD (DBG_CMD, "wait: command slot (now at %p)", ptrs->out);
623 schedule();
626 // wait on my slot (... one gets to its slot, and... )
627 while (ptrs->out->request != cpu_to_be32 (SRB_COMPLETE)) {
628 PRINTD (DBG_CMD, "wait: command slot completion");
629 schedule();
632 PRINTD (DBG_CMD, "command complete");
633 // update queue (... moves the queue along to the next slot)
634 spin_lock (&cq->lock);
635 cq->pending--;
636 // copy command out
637 *cmd = *ptrs->out;
638 ptrs->out = NEXTQ (ptrs->out, ptrs->start, ptrs->limit);
639 spin_unlock (&cq->lock);
641 return 0;
642 } else {
643 cq->filled++;
644 spin_unlock (&cq->lock);
645 return -EAGAIN;
650 /********** TX queue pair **********/
652 static inline int tx_give (amb_dev * dev, tx_in * tx) {
653 amb_txq * txq = &dev->txq;
654 unsigned long flags;
656 PRINTD (DBG_FLOW|DBG_TX, "tx_give %p", dev);
658 if (test_bit (dead, &dev->flags))
659 return 0;
661 spin_lock_irqsave (&txq->lock, flags);
663 if (txq->pending < txq->maximum) {
664 PRINTD (DBG_TX, "TX in slot %p", txq->in.ptr);
666 *txq->in.ptr = *tx;
667 txq->pending++;
668 txq->in.ptr = NEXTQ (txq->in.ptr, txq->in.start, txq->in.limit);
669 // hand over the TX and ring the bell
670 wr_mem (dev, offsetof(amb_mem, mb.adapter.tx_address), virt_to_bus (txq->in.ptr));
671 wr_mem (dev, offsetof(amb_mem, doorbell), TX_FRAME);
673 if (txq->pending > txq->high)
674 txq->high = txq->pending;
675 spin_unlock_irqrestore (&txq->lock, flags);
676 return 0;
677 } else {
678 txq->filled++;
679 spin_unlock_irqrestore (&txq->lock, flags);
680 return -EAGAIN;
684 static inline int tx_take (amb_dev * dev) {
685 amb_txq * txq = &dev->txq;
686 unsigned long flags;
688 PRINTD (DBG_FLOW|DBG_TX, "tx_take %p", dev);
690 spin_lock_irqsave (&txq->lock, flags);
692 if (txq->pending && txq->out.ptr->handle) {
693 // deal with TX completion
694 tx_complete (dev, txq->out.ptr);
695 // mark unused again
696 txq->out.ptr->handle = 0;
697 // remove item
698 txq->pending--;
699 txq->out.ptr = NEXTQ (txq->out.ptr, txq->out.start, txq->out.limit);
701 spin_unlock_irqrestore (&txq->lock, flags);
702 return 0;
703 } else {
705 spin_unlock_irqrestore (&txq->lock, flags);
706 return -1;
710 /********** RX queue pairs **********/
712 static inline int rx_give (amb_dev * dev, rx_in * rx, unsigned char pool) {
713 amb_rxq * rxq = &dev->rxq[pool];
714 unsigned long flags;
716 PRINTD (DBG_FLOW|DBG_RX, "rx_give %p[%hu]", dev, pool);
718 spin_lock_irqsave (&rxq->lock, flags);
720 if (rxq->pending < rxq->maximum) {
721 PRINTD (DBG_RX, "RX in slot %p", rxq->in.ptr);
723 *rxq->in.ptr = *rx;
724 rxq->pending++;
725 rxq->in.ptr = NEXTQ (rxq->in.ptr, rxq->in.start, rxq->in.limit);
726 // hand over the RX buffer
727 wr_mem (dev, offsetof(amb_mem, mb.adapter.rx_address[pool]), virt_to_bus (rxq->in.ptr));
729 spin_unlock_irqrestore (&rxq->lock, flags);
730 return 0;
731 } else {
732 spin_unlock_irqrestore (&rxq->lock, flags);
733 return -1;
737 static inline int rx_take (amb_dev * dev, unsigned char pool) {
738 amb_rxq * rxq = &dev->rxq[pool];
739 unsigned long flags;
741 PRINTD (DBG_FLOW|DBG_RX, "rx_take %p[%hu]", dev, pool);
743 spin_lock_irqsave (&rxq->lock, flags);
745 if (rxq->pending && (rxq->out.ptr->status || rxq->out.ptr->length)) {
746 // deal with RX completion
747 rx_complete (dev, rxq->out.ptr);
748 // mark unused again
749 rxq->out.ptr->status = 0;
750 rxq->out.ptr->length = 0;
751 // remove item
752 rxq->pending--;
753 rxq->out.ptr = NEXTQ (rxq->out.ptr, rxq->out.start, rxq->out.limit);
755 if (rxq->pending < rxq->low)
756 rxq->low = rxq->pending;
757 spin_unlock_irqrestore (&rxq->lock, flags);
758 return 0;
759 } else {
760 if (!rxq->pending && rxq->buffers_wanted)
761 rxq->emptied++;
762 spin_unlock_irqrestore (&rxq->lock, flags);
763 return -1;
767 /********** RX Pool handling **********/
769 /* pre: buffers_wanted = 0, post: pending = 0 */
770 static inline void drain_rx_pool (amb_dev * dev, unsigned char pool) {
771 amb_rxq * rxq = &dev->rxq[pool];
773 PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pool %p %hu", dev, pool);
775 if (test_bit (dead, &dev->flags))
776 return;
778 /* we are not quite like the fill pool routines as we cannot just
779 remove one buffer, we have to remove all of them, but we might as
780 well pretend... */
781 if (rxq->pending > rxq->buffers_wanted) {
782 command cmd;
783 cmd.request = cpu_to_be32 (SRB_FLUSH_BUFFER_Q);
784 cmd.args.flush.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
785 while (command_do (dev, &cmd))
786 schedule();
787 /* the pool may also be emptied via the interrupt handler */
788 while (rxq->pending > rxq->buffers_wanted)
789 if (rx_take (dev, pool))
790 schedule();
793 return;
796 #ifdef MODULE
797 static void drain_rx_pools (amb_dev * dev) {
798 unsigned char pool;
800 PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pools %p", dev);
802 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
803 drain_rx_pool (dev, pool);
805 return;
807 #endif
809 static inline void fill_rx_pool (amb_dev * dev, unsigned char pool, int priority) {
810 rx_in rx;
811 amb_rxq * rxq;
813 PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pool %p %hu %x", dev, pool, priority);
815 if (test_bit (dead, &dev->flags))
816 return;
818 rxq = &dev->rxq[pool];
819 while (rxq->pending < rxq->maximum && rxq->pending < rxq->buffers_wanted) {
821 struct sk_buff * skb = alloc_skb (rxq->buffer_size, priority);
822 if (!skb) {
823 PRINTD (DBG_SKB|DBG_POOL, "failed to allocate skb for RX pool %hu", pool);
824 return;
826 if (check_area (skb->data, skb->truesize)) {
827 dev_kfree_skb_any (skb);
828 return;
830 // cast needed as there is no %? for pointer differences
831 PRINTD (DBG_SKB, "allocated skb at %p, head %p, area %li",
832 skb, skb->head, (long) (skb->end - skb->head));
833 rx.handle = virt_to_bus (skb);
834 rx.host_address = cpu_to_be32 (virt_to_bus (skb->data));
835 if (rx_give (dev, &rx, pool))
836 dev_kfree_skb_any (skb);
840 return;
843 // top up all RX pools (can also be called as a bottom half)
844 static void fill_rx_pools (amb_dev * dev) {
845 unsigned char pool;
847 PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pools %p", dev);
849 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
850 fill_rx_pool (dev, pool, GFP_ATOMIC);
852 return;
855 /********** enable host interrupts **********/
857 static inline void interrupts_on (amb_dev * dev) {
858 wr_plain (dev, offsetof(amb_mem, interrupt_control),
859 rd_plain (dev, offsetof(amb_mem, interrupt_control))
860 | AMB_INTERRUPT_BITS);
863 /********** disable host interrupts **********/
865 static inline void interrupts_off (amb_dev * dev) {
866 wr_plain (dev, offsetof(amb_mem, interrupt_control),
867 rd_plain (dev, offsetof(amb_mem, interrupt_control))
868 &~ AMB_INTERRUPT_BITS);
871 /********** interrupt handling **********/
873 static void interrupt_handler (int irq, void * dev_id, struct pt_regs * pt_regs) {
874 amb_dev * dev = amb_devs;
875 (void) pt_regs;
877 PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler: %p", dev_id);
879 if (!dev_id) {
880 PRINTD (DBG_IRQ|DBG_ERR, "irq with NULL dev_id: %d", irq);
881 return;
883 // Did one of our cards generate the interrupt?
884 while (dev) {
885 if (dev == dev_id)
886 break;
887 dev = dev->prev;
889 // impossible - unless we add the device to our list after both
890 // registering the IRQ handler for it and enabling interrupts, AND
891 // the card generates an IRQ at startup - should not happen again
892 if (!dev) {
893 PRINTD (DBG_IRQ, "irq for unknown device: %d", irq);
894 return;
896 // impossible - unless we have memory corruption of dev or kernel
897 if (irq != dev->irq) {
898 PRINTD (DBG_IRQ|DBG_ERR, "irq mismatch: %d", irq);
899 return;
903 u32 interrupt = rd_plain (dev, offsetof(amb_mem, interrupt));
905 // for us or someone else sharing the same interrupt
906 if (!interrupt) {
907 PRINTD (DBG_IRQ, "irq not for me: %d", irq);
908 return;
911 // definitely for us
912 PRINTD (DBG_IRQ, "FYI: interrupt was %08x", interrupt);
913 wr_plain (dev, offsetof(amb_mem, interrupt), -1);
917 unsigned int irq_work = 0;
918 unsigned char pool;
919 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
920 while (!rx_take (dev, pool))
921 ++irq_work;
922 while (!tx_take (dev))
923 ++irq_work;
925 if (irq_work) {
926 #ifdef FILL_RX_POOLS_IN_BH
927 queue_task (&dev->bh, &tq_immediate);
928 mark_bh (IMMEDIATE_BH);
929 #else
930 fill_rx_pools (dev);
931 #endif
933 PRINTD (DBG_IRQ, "work done: %u", irq_work);
934 } else {
935 PRINTD (DBG_IRQ|DBG_WARN, "no work done");
939 PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler done: %p", dev_id);
940 return;
943 /********** don't panic... yeah, right **********/
945 #ifdef DEBUG_AMBASSADOR
946 static void dont_panic (amb_dev * dev) {
947 amb_cq * cq = &dev->cq;
948 volatile amb_cq_ptrs * ptrs = &cq->ptrs;
949 amb_txq * txq;
950 amb_rxq * rxq;
951 command * cmd;
952 tx_in * tx;
953 tx_simple * tx_descr;
954 unsigned char pool;
955 rx_in * rx;
957 unsigned long flags;
958 save_flags (flags);
959 cli();
961 PRINTK (KERN_INFO, "don't panic - putting adapter into reset");
962 wr_plain (dev, offsetof(amb_mem, reset_control),
963 rd_plain (dev, offsetof(amb_mem, reset_control)) | AMB_RESET_BITS);
965 PRINTK (KERN_INFO, "marking all commands complete");
966 for (cmd = ptrs->start; cmd < ptrs->limit; ++cmd)
967 cmd->request = cpu_to_be32 (SRB_COMPLETE);
969 PRINTK (KERN_INFO, "completing all TXs");
970 txq = &dev->txq;
971 tx = txq->in.ptr;
972 while (txq->pending--) {
973 if (tx == txq->in.start)
974 tx = txq->in.limit;
975 --tx;
976 tx_descr = bus_to_virt (be32_to_cpu (tx->tx_descr_addr));
977 amb_kfree_skb (tx_descr->skb);
978 kfree (tx_descr);
981 PRINTK (KERN_INFO, "freeing all RX buffers");
982 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
983 rxq = &dev->rxq[pool];
984 rx = rxq->in.ptr;
985 while (rxq->pending--) {
986 if (rx == rxq->in.start)
987 rx = rxq->in.limit;
988 --rx;
989 dev_kfree_skb_any (bus_to_virt (rx->handle));
993 PRINTK (KERN_INFO, "don't panic over - close all VCs and rmmod");
994 set_bit (dead, &dev->flags);
995 restore_flags (flags);
996 return;
998 #endif
1000 /********** make rate (not quite as much fun as Horizon) **********/
1002 static unsigned int make_rate (unsigned int rate, rounding r,
1003 u16 * bits, unsigned int * actual) {
1004 unsigned char exp = -1; // hush gcc
1005 unsigned int man = -1; // hush gcc
1007 PRINTD (DBG_FLOW|DBG_QOS, "make_rate %u", rate);
1009 // rates in cells per second, ITU format (nasty 16-bit floating-point)
1010 // given 5-bit e and 9-bit m:
1011 // rate = EITHER (1+m/2^9)*2^e OR 0
1012 // bits = EITHER 1<<14 | e<<9 | m OR 0
1013 // (bit 15 is "reserved", bit 14 "non-zero")
1014 // smallest rate is 0 (special representation)
1015 // largest rate is (1+511/512)*2^31 = 4290772992 (< 2^32-1)
1016 // smallest non-zero rate is (1+0/512)*2^0 = 1 (> 0)
1017 // simple algorithm:
1018 // find position of top bit, this gives e
1019 // remove top bit and shift (rounding if feeling clever) by 9-e
1021 // ucode bug: please don't set bit 14! so 0 rate not representable
1023 if (rate > 0xffc00000U) {
1024 // larger than largest representable rate
1026 if (r == round_up) {
1027 return -EINVAL;
1028 } else {
1029 exp = 31;
1030 man = 511;
1033 } else if (rate) {
1034 // representable rate
1036 exp = 31;
1037 man = rate;
1039 // invariant: rate = man*2^(exp-31)
1040 while (!(man & (1<<31))) {
1041 exp = exp - 1;
1042 man = man<<1;
1045 // man has top bit set
1046 // rate = (2^31+(man-2^31))*2^(exp-31)
1047 // rate = (1+(man-2^31)/2^31)*2^exp
1048 man = man<<1;
1049 man &= 0xffffffffU; // a nop on 32-bit systems
1050 // rate = (1+man/2^32)*2^exp
1052 // exp is in the range 0 to 31, man is in the range 0 to 2^32-1
1053 // time to lose significance... we want m in the range 0 to 2^9-1
1054 // rounding presents a minor problem... we first decide which way
1055 // we are rounding (based on given rounding direction and possibly
1056 // the bits of the mantissa that are to be discarded).
1058 switch (r) {
1059 case round_down: {
1060 // just truncate
1061 man = man>>(32-9);
1062 break;
1064 case round_up: {
1065 // check all bits that we are discarding
1066 if (man & (-1>>9)) {
1067 man = (man>>(32-9)) + 1;
1068 if (man == (1<<9)) {
1069 // no need to check for round up outside of range
1070 man = 0;
1071 exp += 1;
1073 } else {
1074 man = (man>>(32-9));
1076 break;
1078 case round_nearest: {
1079 // check msb that we are discarding
1080 if (man & (1<<(32-9-1))) {
1081 man = (man>>(32-9)) + 1;
1082 if (man == (1<<9)) {
1083 // no need to check for round up outside of range
1084 man = 0;
1085 exp += 1;
1087 } else {
1088 man = (man>>(32-9));
1090 break;
1094 } else {
1095 // zero rate - not representable
1097 if (r == round_down) {
1098 return -EINVAL;
1099 } else {
1100 exp = 0;
1101 man = 0;
1106 PRINTD (DBG_QOS, "rate: man=%u, exp=%hu", man, exp);
1108 if (bits)
1109 *bits = /* (1<<14) | */ (exp<<9) | man;
1111 if (actual)
1112 *actual = (exp >= 9)
1113 ? (1 << exp) + (man << (exp-9))
1114 : (1 << exp) + ((man + (1<<(9-exp-1))) >> (9-exp));
1116 return 0;
1119 /********** Linux ATM Operations **********/
1121 // some are not yet implemented while others do not make sense for
1122 // this device
1124 /********** Open a VC **********/
1126 static int amb_open (struct atm_vcc * atm_vcc, short vpi, int vci) {
1127 int error;
1129 struct atm_qos * qos;
1130 struct atm_trafprm * txtp;
1131 struct atm_trafprm * rxtp;
1132 u16 tx_rate_bits;
1133 u16 tx_vc_bits = -1; // hush gcc
1134 u16 tx_frame_bits = -1; // hush gcc
1136 amb_dev * dev = AMB_DEV(atm_vcc->dev);
1137 amb_vcc * vcc;
1138 unsigned char pool = -1; // hush gcc
1140 PRINTD (DBG_FLOW|DBG_VCC, "amb_open %x %x", vpi, vci);
1142 #ifdef ATM_VPI_UNSPEC
1143 // UNSPEC is deprecated, remove this code eventually
1144 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) {
1145 PRINTK (KERN_WARNING, "rejecting open with unspecified VPI/VCI (deprecated)");
1146 return -EINVAL;
1148 #endif
1150 // deal with possibly wildcarded VCs
1151 error = atm_find_ci (atm_vcc, &vpi, &vci);
1152 if (error) {
1153 PRINTD (DBG_WARN|DBG_VCC, "atm_find_ci failed!");
1154 return error;
1156 PRINTD (DBG_VCC, "atm_find_ci gives %x %x", vpi, vci);
1158 if (!(0 <= vpi && vpi < (1<<NUM_VPI_BITS) &&
1159 0 <= vci && vci < (1<<NUM_VCI_BITS))) {
1160 PRINTD (DBG_WARN|DBG_VCC, "VPI/VCI out of range: %hd/%d", vpi, vci);
1161 return -EINVAL;
1164 qos = &atm_vcc->qos;
1166 if (qos->aal != ATM_AAL5) {
1167 PRINTD (DBG_QOS, "AAL not supported");
1168 return -EINVAL;
1171 // traffic parameters
1173 PRINTD (DBG_QOS, "TX:");
1174 txtp = &qos->txtp;
1175 if (txtp->traffic_class != ATM_NONE) {
1176 switch (txtp->traffic_class) {
1177 case ATM_UBR: {
1178 // we take "the PCR" as a rate-cap
1179 int pcr = atm_pcr_goal (txtp);
1180 if (!pcr) {
1181 // no rate cap
1182 tx_rate_bits = 0;
1183 tx_vc_bits = TX_UBR;
1184 tx_frame_bits = TX_FRAME_NOTCAP;
1185 } else {
1186 rounding r;
1187 if (pcr < 0) {
1188 r = round_down;
1189 pcr = -pcr;
1190 } else {
1191 r = round_up;
1193 error = make_rate (pcr, r, &tx_rate_bits, 0);
1194 tx_vc_bits = TX_UBR_CAPPED;
1195 tx_frame_bits = TX_FRAME_CAPPED;
1197 break;
1199 #if 0
1200 case ATM_ABR: {
1201 pcr = atm_pcr_goal (txtp);
1202 PRINTD (DBG_QOS, "pcr goal = %d", pcr);
1203 break;
1205 #endif
1206 default: {
1207 // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
1208 PRINTD (DBG_QOS, "request for non-UBR denied");
1209 return -EINVAL;
1212 PRINTD (DBG_QOS, "tx_rate_bits=%hx, tx_vc_bits=%hx",
1213 tx_rate_bits, tx_vc_bits);
1216 PRINTD (DBG_QOS, "RX:");
1217 rxtp = &qos->rxtp;
1218 if (rxtp->traffic_class == ATM_NONE) {
1219 // do nothing
1220 } else {
1221 // choose an RX pool (arranged in increasing size)
1222 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
1223 if ((unsigned int) rxtp->max_sdu <= dev->rxq[pool].buffer_size) {
1224 PRINTD (DBG_VCC|DBG_QOS|DBG_POOL, "chose pool %hu (max_sdu %u <= %u)",
1225 pool, rxtp->max_sdu, dev->rxq[pool].buffer_size);
1226 break;
1228 if (pool == NUM_RX_POOLS) {
1229 PRINTD (DBG_WARN|DBG_VCC|DBG_QOS|DBG_POOL,
1230 "no pool suitable for VC (RX max_sdu %d is too large)",
1231 rxtp->max_sdu);
1232 return -EINVAL;
1235 switch (rxtp->traffic_class) {
1236 case ATM_UBR: {
1237 break;
1239 #if 0
1240 case ATM_ABR: {
1241 pcr = atm_pcr_goal (rxtp);
1242 PRINTD (DBG_QOS, "pcr goal = %d", pcr);
1243 break;
1245 #endif
1246 default: {
1247 // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
1248 PRINTD (DBG_QOS, "request for non-UBR denied");
1249 return -EINVAL;
1254 // prevent module unload while sleeping (kmalloc/down)
1255 // doing this any earlier would complicate more error return paths
1256 MOD_INC_USE_COUNT;
1258 // get space for our vcc stuff
1259 vcc = kmalloc (sizeof(amb_vcc), GFP_KERNEL);
1260 if (!vcc) {
1261 PRINTK (KERN_ERR, "out of memory!");
1262 MOD_DEC_USE_COUNT;
1263 return -ENOMEM;
1265 atm_vcc->dev_data = (void *) vcc;
1267 // no failures beyond this point
1269 // we are not really "immediately before allocating the connection
1270 // identifier in hardware", but it will just have to do!
1271 set_bit(ATM_VF_ADDR,&atm_vcc->flags);
1273 if (txtp->traffic_class != ATM_NONE) {
1274 command cmd;
1276 vcc->tx_frame_bits = tx_frame_bits;
1278 down (&dev->vcc_sf);
1279 if (dev->rxer[vci]) {
1280 // RXer on the channel already, just modify rate...
1281 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
1282 cmd.args.modify_rate.vc = cpu_to_be32 (vci); // vpi 0
1283 cmd.args.modify_rate.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
1284 while (command_do (dev, &cmd))
1285 schedule();
1286 // ... and TX flags, preserving the RX pool
1287 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
1288 cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
1289 cmd.args.modify_flags.flags = cpu_to_be32
1290 ( (AMB_VCC(dev->rxer[vci])->rx_info.pool << SRB_POOL_SHIFT)
1291 | (tx_vc_bits << SRB_FLAGS_SHIFT) );
1292 while (command_do (dev, &cmd))
1293 schedule();
1294 } else {
1295 // no RXer on the channel, just open (with pool zero)
1296 cmd.request = cpu_to_be32 (SRB_OPEN_VC);
1297 cmd.args.open.vc = cpu_to_be32 (vci); // vpi 0
1298 cmd.args.open.flags = cpu_to_be32 (tx_vc_bits << SRB_FLAGS_SHIFT);
1299 cmd.args.open.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
1300 while (command_do (dev, &cmd))
1301 schedule();
1303 dev->txer[vci].tx_present = 1;
1304 up (&dev->vcc_sf);
1307 if (rxtp->traffic_class != ATM_NONE) {
1308 command cmd;
1310 vcc->rx_info.pool = pool;
1312 down (&dev->vcc_sf);
1313 /* grow RX buffer pool */
1314 if (!dev->rxq[pool].buffers_wanted)
1315 dev->rxq[pool].buffers_wanted = rx_lats;
1316 dev->rxq[pool].buffers_wanted += 1;
1317 fill_rx_pool (dev, pool, GFP_KERNEL);
1319 if (dev->txer[vci].tx_present) {
1320 // TXer on the channel already
1321 // switch (from pool zero) to this pool, preserving the TX bits
1322 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
1323 cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
1324 cmd.args.modify_flags.flags = cpu_to_be32
1325 ( (pool << SRB_POOL_SHIFT)
1326 | (dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT) );
1327 } else {
1328 // no TXer on the channel, open the VC (with no rate info)
1329 cmd.request = cpu_to_be32 (SRB_OPEN_VC);
1330 cmd.args.open.vc = cpu_to_be32 (vci); // vpi 0
1331 cmd.args.open.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
1332 cmd.args.open.rate = cpu_to_be32 (0);
1334 while (command_do (dev, &cmd))
1335 schedule();
1336 // this link allows RX frames through
1337 dev->rxer[vci] = atm_vcc;
1338 up (&dev->vcc_sf);
1341 // set elements of vcc
1342 atm_vcc->vpi = vpi; // 0
1343 atm_vcc->vci = vci;
1345 // indicate readiness
1346 set_bit(ATM_VF_READY,&atm_vcc->flags);
1348 return 0;
1351 /********** Close a VC **********/
1353 static void amb_close (struct atm_vcc * atm_vcc) {
1354 amb_dev * dev = AMB_DEV (atm_vcc->dev);
1355 amb_vcc * vcc = AMB_VCC (atm_vcc);
1356 u16 vci = atm_vcc->vci;
1358 PRINTD (DBG_VCC|DBG_FLOW, "amb_close");
1360 // indicate unreadiness
1361 clear_bit(ATM_VF_READY,&atm_vcc->flags);
1363 // disable TXing
1364 if (atm_vcc->qos.txtp.traffic_class != ATM_NONE) {
1365 command cmd;
1367 down (&dev->vcc_sf);
1368 if (dev->rxer[vci]) {
1369 // RXer still on the channel, just modify rate... XXX not really needed
1370 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
1371 cmd.args.modify_rate.vc = cpu_to_be32 (vci); // vpi 0
1372 cmd.args.modify_rate.rate = cpu_to_be32 (0);
1373 // ... and clear TX rate flags (XXX to stop RM cell output?), preserving RX pool
1374 } else {
1375 // no RXer on the channel, close channel
1376 cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
1377 cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
1379 dev->txer[vci].tx_present = 0;
1380 while (command_do (dev, &cmd))
1381 schedule();
1382 up (&dev->vcc_sf);
1385 // disable RXing
1386 if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
1387 command cmd;
1389 // this is (the?) one reason why we need the amb_vcc struct
1390 unsigned char pool = vcc->rx_info.pool;
1392 down (&dev->vcc_sf);
1393 if (dev->txer[vci].tx_present) {
1394 // TXer still on the channel, just go to pool zero XXX not really needed
1395 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
1396 cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
1397 cmd.args.modify_flags.flags = cpu_to_be32
1398 (dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT);
1399 } else {
1400 // no TXer on the channel, close the VC
1401 cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
1402 cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
1404 // forget the rxer - no more skbs will be pushed
1405 if (atm_vcc != dev->rxer[vci])
1406 PRINTK (KERN_ERR, "%s vcc=%p rxer[vci]=%p",
1407 "arghhh! we're going to die!",
1408 vcc, dev->rxer[vci]);
1409 dev->rxer[vci] = 0;
1410 while (command_do (dev, &cmd))
1411 schedule();
1413 /* shrink RX buffer pool */
1414 dev->rxq[pool].buffers_wanted -= 1;
1415 if (dev->rxq[pool].buffers_wanted == rx_lats) {
1416 dev->rxq[pool].buffers_wanted = 0;
1417 drain_rx_pool (dev, pool);
1419 up (&dev->vcc_sf);
1422 // free our structure
1423 kfree (vcc);
1425 // say the VPI/VCI is free again
1426 clear_bit(ATM_VF_ADDR,&atm_vcc->flags);
1428 MOD_DEC_USE_COUNT;
1429 return;
1432 /********** Debug\x17Ioctl **********/
1434 #if 0
1435 static int amb_ioctl (struct atm_dev * dev, unsigned int cmd, void * arg) {
1436 unsigned short newdebug;
1437 if (cmd == AMB_SETDEBUG) {
1438 if (!capable(CAP_NET_ADMIN))
1439 return -EPERM;
1440 if (copy_from_user (&newdebug, arg, sizeof(newdebug))) {
1441 // moan
1442 return -EFAULT;
1443 } else {
1444 debug = newdebug;
1445 return 0;
1447 } else if (cmd == AMB_DONTPANIC) {
1448 if (!capable(CAP_NET_ADMIN))
1449 return -EPERM;
1450 dont_panic (dev);
1451 } else {
1452 // moan
1453 return -ENOIOCTLCMD;
1456 #endif
1458 /********** Set socket options for a VC **********/
1460 // int amb_getsockopt (struct atm_vcc * atm_vcc, int level, int optname, void * optval, int optlen);
1462 /********** Set socket options for a VC **********/
1464 // int amb_setsockopt (struct atm_vcc * atm_vcc, int level, int optname, void * optval, int optlen);
1466 /********** Send **********/
1468 static int amb_send (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
1469 amb_dev * dev = AMB_DEV(atm_vcc->dev);
1470 amb_vcc * vcc = AMB_VCC(atm_vcc);
1471 u16 vc = atm_vcc->vci;
1472 unsigned int tx_len = skb->len;
1473 unsigned char * tx_data = skb->data;
1474 tx_simple * tx_descr;
1475 tx_in tx;
1477 if (test_bit (dead, &dev->flags))
1478 return -EIO;
1480 PRINTD (DBG_FLOW|DBG_TX, "amb_send vc %x data %p len %u",
1481 vc, tx_data, tx_len);
1483 dump_skb (">>>", vc, skb);
1485 if (!dev->txer[vc].tx_present) {
1486 PRINTK (KERN_ERR, "attempt to send on RX-only VC %x", vc);
1487 return -EBADFD;
1490 // this is a driver private field so we have to set it ourselves,
1491 // despite the fact that we are _required_ to use it to check for a
1492 // pop function
1493 ATM_SKB(skb)->vcc = atm_vcc;
1495 if (skb->len > (size_t) atm_vcc->qos.txtp.max_sdu) {
1496 PRINTK (KERN_ERR, "sk_buff length greater than agreed max_sdu, dropping...");
1497 return -EIO;
1500 if (check_area (skb->data, skb->len)) {
1501 atomic_inc(&atm_vcc->stats->tx_err);
1502 return -ENOMEM; // ?
1505 // allocate memory for fragments
1506 tx_descr = kmalloc (sizeof(tx_simple), GFP_KERNEL);
1507 if (!tx_descr) {
1508 PRINTK (KERN_ERR, "could not allocate TX descriptor");
1509 return -ENOMEM;
1511 if (check_area (tx_descr, sizeof(tx_simple))) {
1512 kfree (tx_descr);
1513 return -ENOMEM;
1515 PRINTD (DBG_TX, "fragment list allocated at %p", tx_descr);
1517 tx_descr->skb = skb;
1519 tx_descr->tx_frag.bytes = cpu_to_be32 (tx_len);
1520 tx_descr->tx_frag.address = cpu_to_be32 (virt_to_bus (tx_data));
1522 tx_descr->tx_frag_end.handle = virt_to_bus (tx_descr);
1523 tx_descr->tx_frag_end.vc = 0;
1524 tx_descr->tx_frag_end.next_descriptor_length = 0;
1525 tx_descr->tx_frag_end.next_descriptor = 0;
1526 #ifdef AMB_NEW_MICROCODE
1527 tx_descr->tx_frag_end.cpcs_uu = 0;
1528 tx_descr->tx_frag_end.cpi = 0;
1529 tx_descr->tx_frag_end.pad = 0;
1530 #endif
1532 tx.vc = cpu_to_be16 (vcc->tx_frame_bits | vc);
1533 tx.tx_descr_length = cpu_to_be16 (sizeof(tx_frag)+sizeof(tx_frag_end));
1534 tx.tx_descr_addr = cpu_to_be32 (virt_to_bus (&tx_descr->tx_frag));
1536 #ifdef DEBUG_AMBASSADOR
1537 /* wey-hey! */
1538 if (vc == 1023) {
1539 unsigned int i;
1540 unsigned short d = 0;
1541 char * s = skb->data;
1542 switch (*s++) {
1543 case 'D': {
1544 for (i = 0; i < 4; ++i) {
1545 d = (d<<4) | ((*s <= '9') ? (*s - '0') : (*s - 'a' + 10));
1546 ++s;
1548 PRINTK (KERN_INFO, "debug bitmap is now %hx", debug = d);
1549 break;
1551 case 'R': {
1552 if (*s++ == 'e' && *s++ == 's' && *s++ == 'e' && *s++ == 't')
1553 dont_panic (dev);
1554 break;
1556 default: {
1557 break;
1561 #endif
1563 while (tx_give (dev, &tx))
1564 schedule();
1565 return 0;
1568 /********** Scatter Gather Send Capability **********/
1570 static int amb_sg_send (struct atm_vcc * atm_vcc,
1571 unsigned long start,
1572 unsigned long size) {
1573 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: never");
1574 return 0;
1575 if (atm_vcc->qos.aal == ATM_AAL5) {
1576 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: yes");
1577 return 1;
1578 } else {
1579 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: no");
1580 return 0;
1582 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: always");
1583 return 1;
1586 /********** Send OAM **********/
1588 // static int amb_send_oam (struct atm_vcc * atm_vcc, void * cell, int flags);
1590 /********** Feedback to Driver **********/
1592 // void amb_feedback (struct atm_vcc * atm_vcc, struct sk_buff * skb,
1593 // unsigned long start, unsigned long dest, int len);
1595 /********** Change QoS on a VC **********/
1597 // int amb_change_qos (struct atm_vcc * atm_vcc, struct atm_qos * qos, int flags);
1599 /********** Free RX Socket Buffer **********/
1601 #if 0
1602 static void amb_free_rx_skb (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
1603 amb_dev * dev = AMB_DEV (atm_vcc->dev);
1604 amb_vcc * vcc = AMB_VCC (atm_vcc);
1605 unsigned char pool = vcc->rx_info.pool;
1606 rx_in rx;
1608 // This may be unsafe for various reasons that I cannot really guess
1609 // at. However, I note that the ATM layer calls kfree_skb rather
1610 // than dev_kfree_skb at this point so we are least covered as far
1611 // as buffer locking goes. There may be bugs if pcap clones RX skbs.
1613 PRINTD (DBG_FLOW|DBG_SKB, "amb_rx_free skb %p (atm_vcc %p, vcc %p)",
1614 skb, atm_vcc, vcc);
1616 rx.handle = virt_to_bus (skb);
1617 rx.host_address = cpu_to_be32 (virt_to_bus (skb->data));
1619 skb->data = skb->head;
1620 skb->tail = skb->head;
1621 skb->len = 0;
1623 if (!rx_give (dev, &rx, pool)) {
1624 // success
1625 PRINTD (DBG_SKB|DBG_POOL, "recycled skb for pool %hu", pool);
1626 return;
1629 // just do what the ATM layer would have done
1630 dev_kfree_skb_any (skb);
1632 return;
1634 #endif
1636 /********** Proc File Output **********/
1638 static int amb_proc_read (struct atm_dev * atm_dev, loff_t * pos, char * page) {
1639 amb_dev * dev = AMB_DEV (atm_dev);
1640 int left = *pos;
1641 unsigned char pool;
1643 PRINTD (DBG_FLOW, "amb_proc_read");
1645 /* more diagnostics here? */
1647 if (!left--) {
1648 amb_stats * s = &dev->stats;
1649 return sprintf (page,
1650 "frames: TX OK %lu, RX OK %lu, RX bad %lu "
1651 "(CRC %lu, long %lu, aborted %lu, unused %lu).\n",
1652 s->tx_ok, s->rx.ok, s->rx.error,
1653 s->rx.badcrc, s->rx.toolong,
1654 s->rx.aborted, s->rx.unused);
1657 if (!left--) {
1658 amb_cq * c = &dev->cq;
1659 return sprintf (page, "cmd queue [cur/hi/max]: %u/%u/%u. ",
1660 c->pending, c->high, c->maximum);
1663 if (!left--) {
1664 amb_txq * t = &dev->txq;
1665 return sprintf (page, "TX queue [cur/max high full]: %u/%u %u %u.\n",
1666 t->pending, t->maximum, t->high, t->filled);
1669 if (!left--) {
1670 unsigned int count = sprintf (page, "RX queues [cur/max/req low empty]:");
1671 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1672 amb_rxq * r = &dev->rxq[pool];
1673 count += sprintf (page+count, " %u/%u/%u %u %u",
1674 r->pending, r->maximum, r->buffers_wanted, r->low, r->emptied);
1676 count += sprintf (page+count, ".\n");
1677 return count;
1680 if (!left--) {
1681 unsigned int count = sprintf (page, "RX buffer sizes:");
1682 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1683 amb_rxq * r = &dev->rxq[pool];
1684 count += sprintf (page+count, " %u", r->buffer_size);
1686 count += sprintf (page+count, ".\n");
1687 return count;
1690 #if 0
1691 if (!left--) {
1692 // suni block etc?
1694 #endif
1696 return 0;
1699 /********** Operation Structure **********/
1701 static const struct atmdev_ops amb_ops = {
1702 open: amb_open,
1703 close: amb_close,
1704 send: amb_send,
1705 sg_send: amb_sg_send,
1706 proc_read: amb_proc_read
1709 /********** housekeeping **********/
1711 static inline void set_timer (struct timer_list * timer, unsigned long delay) {
1712 timer->expires = jiffies + delay;
1713 add_timer (timer);
1714 return;
1717 static void do_housekeeping (unsigned long arg) {
1718 amb_dev * dev = amb_devs;
1719 // data is set to zero at module unload
1720 (void) arg;
1722 if (housekeeping.data) {
1723 while (dev) {
1725 // could collect device-specific (not driver/atm-linux) stats here
1727 // last resort refill once every ten seconds
1728 fill_rx_pools (dev);
1730 dev = dev->prev;
1732 set_timer (&housekeeping, 10*HZ);
1735 return;
1738 /********** creation of communication queues **********/
1740 static int __init create_queues (amb_dev * dev, unsigned int cmds,
1741 unsigned int txs, unsigned int * rxs,
1742 unsigned int * rx_buffer_sizes) {
1743 unsigned char pool;
1744 size_t total = 0;
1745 void * memory;
1746 void * limit;
1748 PRINTD (DBG_FLOW, "create_queues %p", dev);
1750 total += cmds * sizeof(command);
1752 total += txs * (sizeof(tx_in) + sizeof(tx_out));
1754 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
1755 total += rxs[pool] * (sizeof(rx_in) + sizeof(rx_out));
1757 memory = kmalloc (total, GFP_KERNEL);
1758 if (!memory) {
1759 PRINTK (KERN_ERR, "could not allocate queues");
1760 return -ENOMEM;
1762 if (check_area (memory, total)) {
1763 PRINTK (KERN_ERR, "queues allocated in nasty area");
1764 kfree (memory);
1765 return -ENOMEM;
1768 limit = memory + total;
1769 PRINTD (DBG_INIT, "queues from %p to %p", memory, limit);
1771 PRINTD (DBG_CMD, "command queue at %p", memory);
1774 command * cmd = memory;
1775 amb_cq * cq = &dev->cq;
1777 cq->pending = 0;
1778 cq->high = 0;
1779 cq->maximum = cmds - 1;
1781 cq->ptrs.start = cmd;
1782 cq->ptrs.in = cmd;
1783 cq->ptrs.out = cmd;
1784 cq->ptrs.limit = cmd + cmds;
1786 memory = cq->ptrs.limit;
1789 PRINTD (DBG_TX, "TX queue pair at %p", memory);
1792 tx_in * in = memory;
1793 tx_out * out;
1794 amb_txq * txq = &dev->txq;
1796 txq->pending = 0;
1797 txq->high = 0;
1798 txq->filled = 0;
1799 txq->maximum = txs - 1;
1801 txq->in.start = in;
1802 txq->in.ptr = in;
1803 txq->in.limit = in + txs;
1805 memory = txq->in.limit;
1806 out = memory;
1808 txq->out.start = out;
1809 txq->out.ptr = out;
1810 txq->out.limit = out + txs;
1812 memory = txq->out.limit;
1815 PRINTD (DBG_RX, "RX queue pairs at %p", memory);
1817 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1818 rx_in * in = memory;
1819 rx_out * out;
1820 amb_rxq * rxq = &dev->rxq[pool];
1822 rxq->buffer_size = rx_buffer_sizes[pool];
1823 rxq->buffers_wanted = 0;
1825 rxq->pending = 0;
1826 rxq->low = rxs[pool] - 1;
1827 rxq->emptied = 0;
1828 rxq->maximum = rxs[pool] - 1;
1830 rxq->in.start = in;
1831 rxq->in.ptr = in;
1832 rxq->in.limit = in + rxs[pool];
1834 memory = rxq->in.limit;
1835 out = memory;
1837 rxq->out.start = out;
1838 rxq->out.ptr = out;
1839 rxq->out.limit = out + rxs[pool];
1841 memory = rxq->out.limit;
1844 if (memory == limit) {
1845 return 0;
1846 } else {
1847 PRINTK (KERN_ERR, "bad queue alloc %p != %p (tell maintainer)", memory, limit);
1848 kfree (limit - total);
1849 return -ENOMEM;
1854 /********** destruction of communication queues **********/
1856 static void destroy_queues (amb_dev * dev) {
1857 // all queues assumed empty
1858 void * memory = dev->cq.ptrs.start;
1859 // includes txq.in, txq.out, rxq[].in and rxq[].out
1861 PRINTD (DBG_FLOW, "destroy_queues %p", dev);
1863 PRINTD (DBG_INIT, "freeing queues at %p", memory);
1864 kfree (memory);
1866 return;
1869 /********** basic loader commands and error handling **********/
1871 static int __init do_loader_command (volatile loader_block * lb,
1872 const amb_dev * dev, loader_command cmd) {
1873 // centisecond timeouts - guessing away here
1874 unsigned int command_timeouts [] = {
1875 [host_memory_test] = 15,
1876 [read_adapter_memory] = 2,
1877 [write_adapter_memory] = 2,
1878 [adapter_start] = 50,
1879 [get_version_number] = 10,
1880 [interrupt_host] = 1,
1881 [flash_erase_sector] = 1,
1882 [adap_download_block] = 1,
1883 [adap_erase_flash] = 1,
1884 [adap_run_in_iram] = 1,
1885 [adap_end_download] = 1
1888 unsigned int command_successes [] = {
1889 [host_memory_test] = COMMAND_PASSED_TEST,
1890 [read_adapter_memory] = COMMAND_READ_DATA_OK,
1891 [write_adapter_memory] = COMMAND_WRITE_DATA_OK,
1892 [adapter_start] = COMMAND_COMPLETE,
1893 [get_version_number] = COMMAND_COMPLETE,
1894 [interrupt_host] = COMMAND_COMPLETE,
1895 [flash_erase_sector] = COMMAND_COMPLETE,
1896 [adap_download_block] = COMMAND_COMPLETE,
1897 [adap_erase_flash] = COMMAND_COMPLETE,
1898 [adap_run_in_iram] = COMMAND_COMPLETE,
1899 [adap_end_download] = COMMAND_COMPLETE
1902 int decode_loader_result (loader_command cmd, u32 result) {
1903 int res;
1904 const char * msg;
1906 if (result == command_successes[cmd])
1907 return 0;
1909 switch (result) {
1910 case BAD_COMMAND:
1911 res = -EINVAL;
1912 msg = "bad command";
1913 break;
1914 case COMMAND_IN_PROGRESS:
1915 res = -ETIMEDOUT;
1916 msg = "command in progress";
1917 break;
1918 case COMMAND_PASSED_TEST:
1919 res = 0;
1920 msg = "command passed test";
1921 break;
1922 case COMMAND_FAILED_TEST:
1923 res = -EIO;
1924 msg = "command failed test";
1925 break;
1926 case COMMAND_READ_DATA_OK:
1927 res = 0;
1928 msg = "command read data ok";
1929 break;
1930 case COMMAND_READ_BAD_ADDRESS:
1931 res = -EINVAL;
1932 msg = "command read bad address";
1933 break;
1934 case COMMAND_WRITE_DATA_OK:
1935 res = 0;
1936 msg = "command write data ok";
1937 break;
1938 case COMMAND_WRITE_BAD_ADDRESS:
1939 res = -EINVAL;
1940 msg = "command write bad address";
1941 break;
1942 case COMMAND_WRITE_FLASH_FAILURE:
1943 res = -EIO;
1944 msg = "command write flash failure";
1945 break;
1946 case COMMAND_COMPLETE:
1947 res = 0;
1948 msg = "command complete";
1949 break;
1950 case COMMAND_FLASH_ERASE_FAILURE:
1951 res = -EIO;
1952 msg = "command flash erase failure";
1953 break;
1954 case COMMAND_WRITE_BAD_DATA:
1955 res = -EINVAL;
1956 msg = "command write bad data";
1957 break;
1958 default:
1959 res = -EINVAL;
1960 msg = "unknown error";
1961 PRINTD (DBG_LOAD|DBG_ERR, "decode_loader_result got %d=%x !",
1962 result, result);
1963 break;
1966 PRINTK (KERN_ERR, "%s", msg);
1967 return res;
1970 unsigned long timeout;
1972 PRINTD (DBG_FLOW|DBG_LOAD, "do_loader_command");
1974 /* do a command
1976 Set the return value to zero, set the command type and set the
1977 valid entry to the right magic value. The payload is already
1978 correctly byte-ordered so we leave it alone. Hit the doorbell
1979 with the bus address of this structure.
1983 lb->result = 0;
1984 lb->command = cpu_to_be32 (cmd);
1985 lb->valid = cpu_to_be32 (DMA_VALID);
1986 // dump_registers (dev);
1987 // dump_loader_block (lb);
1988 wr_mem (dev, offsetof(amb_mem, doorbell), virt_to_bus (lb) & ~onegigmask);
1990 timeout = command_timeouts[cmd] * HZ/100;
1992 while (!lb->result || lb->result == cpu_to_be32 (COMMAND_IN_PROGRESS))
1993 if (timeout) {
1994 timeout = schedule_timeout (timeout);
1995 } else {
1996 PRINTD (DBG_LOAD|DBG_ERR, "command %d timed out", cmd);
1997 dump_registers (dev);
1998 dump_loader_block (lb);
1999 return -ETIMEDOUT;
2002 if (cmd == adapter_start) {
2003 // wait for start command to acknowledge...
2004 timeout = HZ/10;
2005 while (rd_plain (dev, offsetof(amb_mem, doorbell)))
2006 if (timeout) {
2007 timeout = schedule_timeout (timeout);
2008 } else {
2009 PRINTD (DBG_LOAD|DBG_ERR, "start command did not clear doorbell, res=%08x",
2010 be32_to_cpu (lb->result));
2011 dump_registers (dev);
2012 return -ETIMEDOUT;
2014 return 0;
2015 } else {
2016 return decode_loader_result (cmd, be32_to_cpu (lb->result));
2021 /* loader: determine loader version */
2023 static int __init get_loader_version (loader_block * lb,
2024 const amb_dev * dev, u32 * version) {
2025 int res;
2027 PRINTD (DBG_FLOW|DBG_LOAD, "get_loader_version");
2029 res = do_loader_command (lb, dev, get_version_number);
2030 if (res)
2031 return res;
2032 if (version)
2033 *version = be32_to_cpu (lb->payload.version);
2034 return 0;
2037 /* loader: write memory data blocks */
2039 static int __init loader_write (loader_block * lb,
2040 const amb_dev * dev, const u32 * data,
2041 u32 address, unsigned int count) {
2042 unsigned int i;
2043 transfer_block * tb = &lb->payload.transfer;
2045 PRINTD (DBG_FLOW|DBG_LOAD, "loader_write");
2047 if (count > MAX_TRANSFER_DATA)
2048 return -EINVAL;
2049 tb->address = cpu_to_be32 (address);
2050 tb->count = cpu_to_be32 (count);
2051 for (i = 0; i < count; ++i)
2052 tb->data[i] = cpu_to_be32 (data[i]);
2053 return do_loader_command (lb, dev, write_adapter_memory);
2056 /* loader: verify memory data blocks */
2058 static int __init loader_verify (loader_block * lb,
2059 const amb_dev * dev, const u32 * data,
2060 u32 address, unsigned int count) {
2061 unsigned int i;
2062 transfer_block * tb = &lb->payload.transfer;
2063 int res;
2065 PRINTD (DBG_FLOW|DBG_LOAD, "loader_verify");
2067 if (count > MAX_TRANSFER_DATA)
2068 return -EINVAL;
2069 tb->address = cpu_to_be32 (address);
2070 tb->count = cpu_to_be32 (count);
2071 res = do_loader_command (lb, dev, read_adapter_memory);
2072 if (!res)
2073 for (i = 0; i < count; ++i)
2074 if (tb->data[i] != cpu_to_be32 (data[i])) {
2075 res = -EINVAL;
2076 break;
2078 return res;
2081 /* loader: start microcode */
2083 static int __init loader_start (loader_block * lb,
2084 const amb_dev * dev, u32 address) {
2085 PRINTD (DBG_FLOW|DBG_LOAD, "loader_start");
2087 lb->payload.start = cpu_to_be32 (address);
2088 return do_loader_command (lb, dev, adapter_start);
2091 /********** reset card **********/
2093 static int amb_reset (amb_dev * dev, int diags) {
2094 u32 word;
2096 PRINTD (DBG_FLOW|DBG_LOAD, "amb_reset");
2098 word = rd_plain (dev, offsetof(amb_mem, reset_control));
2099 // put card into reset state
2100 wr_plain (dev, offsetof(amb_mem, reset_control), word | AMB_RESET_BITS);
2101 // wait a short while
2102 udelay (10);
2103 #if 1
2104 // put card into known good state
2105 wr_plain (dev, offsetof(amb_mem, interrupt_control), AMB_DOORBELL_BITS);
2106 // clear all interrupts just in case
2107 wr_plain (dev, offsetof(amb_mem, interrupt), -1);
2108 #endif
2109 // clear self-test done flag
2110 wr_plain (dev, offsetof(amb_mem, mb.loader.ready), 0);
2111 // take card out of reset state
2112 wr_plain (dev, offsetof(amb_mem, reset_control), word &~ AMB_RESET_BITS);
2114 if (diags) {
2115 unsigned long timeout;
2116 // 4.2 second wait
2117 timeout = HZ*42/10;
2118 while (timeout)
2119 timeout = schedule_timeout (timeout);
2120 // half second time-out
2121 timeout = HZ/2;
2122 while (!rd_plain (dev, offsetof(amb_mem, mb.loader.ready)))
2123 if (timeout) {
2124 timeout = schedule_timeout (timeout);
2125 } else {
2126 PRINTD (DBG_LOAD|DBG_ERR, "reset timed out");
2127 return -ETIMEDOUT;
2130 // get results of self-test
2131 // XXX double check byte-order
2132 word = rd_mem (dev, offsetof(amb_mem, mb.loader.result));
2133 if (word & SELF_TEST_FAILURE) {
2134 void sf (const char * msg) {
2135 PRINTK (KERN_ERR, "self-test failed: %s", msg);
2137 if (word & GPINT_TST_FAILURE)
2138 sf ("interrupt");
2139 if (word & SUNI_DATA_PATTERN_FAILURE)
2140 sf ("SUNI data pattern");
2141 if (word & SUNI_DATA_BITS_FAILURE)
2142 sf ("SUNI data bits");
2143 if (word & SUNI_UTOPIA_FAILURE)
2144 sf ("SUNI UTOPIA interface");
2145 if (word & SUNI_FIFO_FAILURE)
2146 sf ("SUNI cell buffer FIFO");
2147 if (word & SRAM_FAILURE)
2148 sf ("bad SRAM");
2149 // better return value?
2150 return -EIO;
2154 return 0;
2157 /********** transfer and start the microcode **********/
2159 static int __init ucode_init (loader_block * lb, amb_dev * dev) {
2160 unsigned int i = 0;
2161 unsigned int total = 0;
2162 const u32 * pointer = ucode_data;
2163 u32 address;
2164 unsigned int count;
2165 int res;
2167 PRINTD (DBG_FLOW|DBG_LOAD, "ucode_init");
2169 while (address = ucode_regions[i].start,
2170 count = ucode_regions[i].count) {
2171 PRINTD (DBG_LOAD, "starting region (%x, %u)", address, count);
2172 while (count) {
2173 unsigned int words;
2174 if (count <= MAX_TRANSFER_DATA)
2175 words = count;
2176 else
2177 words = MAX_TRANSFER_DATA;
2178 total += words;
2179 res = loader_write (lb, dev, pointer, address, words);
2180 if (res)
2181 return res;
2182 res = loader_verify (lb, dev, pointer, address, words);
2183 if (res)
2184 return res;
2185 count -= words;
2186 address += sizeof(u32) * words;
2187 pointer += words;
2189 i += 1;
2191 if (*pointer == 0xdeadbeef) {
2192 return loader_start (lb, dev, ucode_start);
2193 } else {
2194 // cast needed as there is no %? for pointer differnces
2195 PRINTD (DBG_LOAD|DBG_ERR,
2196 "offset=%li, *pointer=%x, address=%x, total=%u",
2197 (long) (pointer - ucode_data), *pointer, address, total);
2198 PRINTK (KERN_ERR, "incorrect microcode data");
2199 return -ENOMEM;
2203 /********** give adapter parameters **********/
2205 static int __init amb_talk (amb_dev * dev) {
2206 adap_talk_block a;
2207 unsigned char pool;
2208 unsigned long timeout;
2210 u32 x (void * addr) {
2211 return cpu_to_be32 (virt_to_bus (addr));
2214 PRINTD (DBG_FLOW, "amb_talk %p", dev);
2216 a.command_start = x (dev->cq.ptrs.start);
2217 a.command_end = x (dev->cq.ptrs.limit);
2218 a.tx_start = x (dev->txq.in.start);
2219 a.tx_end = x (dev->txq.in.limit);
2220 a.txcom_start = x (dev->txq.out.start);
2221 a.txcom_end = x (dev->txq.out.limit);
2223 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
2224 // the other "a" items are set up by the adapter
2225 a.rec_struct[pool].buffer_start = x (dev->rxq[pool].in.start);
2226 a.rec_struct[pool].buffer_end = x (dev->rxq[pool].in.limit);
2227 a.rec_struct[pool].rx_start = x (dev->rxq[pool].out.start);
2228 a.rec_struct[pool].rx_end = x (dev->rxq[pool].out.limit);
2229 a.rec_struct[pool].buffer_size = cpu_to_be32 (dev->rxq[pool].buffer_size);
2232 #ifdef AMB_NEW_MICROCODE
2233 // disable fast PLX prefetching
2234 a.init_flags = 0;
2235 #endif
2237 // pass the structure
2238 wr_mem (dev, offsetof(amb_mem, doorbell), virt_to_bus (&a));
2240 // 2.2 second wait (must not touch doorbell during 2 second DMA test)
2241 timeout = HZ*22/10;
2242 while (timeout)
2243 timeout = schedule_timeout (timeout);
2244 // give the adapter another half second?
2245 timeout = HZ/2;
2246 while (rd_plain (dev, offsetof(amb_mem, doorbell)))
2247 if (timeout) {
2248 timeout = schedule_timeout (timeout);
2249 } else {
2250 PRINTD (DBG_INIT|DBG_ERR, "adapter init timed out");
2251 return -ETIMEDOUT;
2254 return 0;
2257 // get microcode version
2258 static void __init amb_ucode_version (amb_dev * dev) {
2259 u32 major;
2260 u32 minor;
2261 command cmd;
2262 cmd.request = cpu_to_be32 (SRB_GET_VERSION);
2263 while (command_do (dev, &cmd))
2264 schedule();
2265 major = be32_to_cpu (cmd.args.version.major);
2266 minor = be32_to_cpu (cmd.args.version.minor);
2267 PRINTK (KERN_INFO, "microcode version is %u.%u", major, minor);
2270 // get end station address
2271 static void __init amb_esi (amb_dev * dev, u8 * esi) {
2272 u32 lower4;
2273 u16 upper2;
2274 command cmd;
2276 // swap bits within byte to get Ethernet ordering
2277 u8 bit_swap (u8 byte) {
2278 const u8 swap[] = {
2279 0x0, 0x8, 0x4, 0xc,
2280 0x2, 0xa, 0x6, 0xe,
2281 0x1, 0x9, 0x5, 0xd,
2282 0x3, 0xb, 0x7, 0xf
2284 return ((swap[byte & 0xf]<<4) | swap[byte>>4]);
2287 cmd.request = cpu_to_be32 (SRB_GET_BIA);
2288 while (command_do (dev, &cmd))
2289 schedule();
2290 lower4 = be32_to_cpu (cmd.args.bia.lower4);
2291 upper2 = be32_to_cpu (cmd.args.bia.upper2);
2292 PRINTD (DBG_LOAD, "BIA: lower4: %08x, upper2 %04x", lower4, upper2);
2294 if (esi) {
2295 unsigned int i;
2297 PRINTDB (DBG_INIT, "ESI:");
2298 for (i = 0; i < ESI_LEN; ++i) {
2299 if (i < 4)
2300 esi[i] = bit_swap (lower4>>(8*i));
2301 else
2302 esi[i] = bit_swap (upper2>>(8*(i-4)));
2303 PRINTDM (DBG_INIT, " %02x", esi[i]);
2306 PRINTDE (DBG_INIT, "");
2309 return;
2312 static int __init amb_init (amb_dev * dev) {
2313 loader_block lb;
2315 void fixup_plx_window (void) {
2316 // fix up the PLX-mapped window base address to match the block
2317 unsigned long blb;
2318 u32 mapreg;
2319 blb = virt_to_bus (&lb);
2320 // the kernel stack had better not ever cross a 1Gb boundary!
2321 mapreg = rd_plain (dev, offsetof(amb_mem, stuff[10]));
2322 mapreg &= ~onegigmask;
2323 mapreg |= blb & onegigmask;
2324 wr_plain (dev, offsetof(amb_mem, stuff[10]), mapreg);
2325 return;
2328 u32 version;
2330 if (amb_reset (dev, 1)) {
2331 PRINTK (KERN_ERR, "card reset failed!");
2332 } else {
2333 fixup_plx_window ();
2335 if (get_loader_version (&lb, dev, &version)) {
2336 PRINTK (KERN_INFO, "failed to get loader version");
2337 } else {
2338 PRINTK (KERN_INFO, "loader version is %08x", version);
2340 if (ucode_init (&lb, dev)) {
2341 PRINTK (KERN_ERR, "microcode failure");
2342 } else if (create_queues (dev, cmds, txs, rxs, rxs_bs)) {
2343 PRINTK (KERN_ERR, "failed to get memory for queues");
2344 } else {
2346 if (amb_talk (dev)) {
2347 PRINTK (KERN_ERR, "adapter did not accept queues");
2348 } else {
2350 amb_ucode_version (dev);
2351 return 0;
2353 } /* amb_talk */
2355 destroy_queues (dev);
2356 } /* create_queues, ucode_init */
2358 amb_reset (dev, 0);
2359 } /* get_loader_version */
2361 } /* amb_reset */
2363 return -1;
2366 static int __init amb_probe (void) {
2367 struct pci_dev * pci_dev;
2368 int devs;
2370 void do_pci_device (void) {
2371 amb_dev * dev;
2373 // read resources from PCI configuration space
2374 u8 irq = pci_dev->irq;
2375 u32 * membase = bus_to_virt (pci_resource_start (pci_dev, 0));
2376 u32 iobase = pci_resource_start (pci_dev, 1);
2378 void setup_dev (void) {
2379 unsigned char pool;
2380 memset (dev, 0, sizeof(amb_dev));
2382 // set up known dev items straight away
2383 dev->pci_dev = pci_dev;
2385 dev->iobase = iobase;
2386 dev->irq = irq;
2387 dev->membase = membase;
2389 // flags (currently only dead)
2390 dev->flags = 0;
2392 // Allocate cell rates (fibre)
2393 // ATM_OC3_PCR = 1555200000/8/270*260/53 - 29/53
2394 // to be really pedantic, this should be ATM_OC3c_PCR
2395 dev->tx_avail = ATM_OC3_PCR;
2396 dev->rx_avail = ATM_OC3_PCR;
2398 #ifdef FILL_RX_POOLS_IN_BH
2399 // initialise bottom half
2400 dev->bh.next = 0;
2401 dev->bh.sync = 0;
2402 dev->bh.routine = (void (*)(void *)) fill_rx_pools;
2403 dev->bh.data = dev;
2404 #endif
2406 // semaphore for txer/rxer modifications - we cannot use a
2407 // spinlock as the critical region needs to switch processes
2408 init_MUTEX (&dev->vcc_sf);
2409 // queue manipulation spinlocks; we want atomic reads and
2410 // writes to the queue descriptors (handles IRQ and SMP)
2411 // consider replacing "int pending" -> "atomic_t available"
2412 // => problem related to who gets to move queue pointers
2413 spin_lock_init (&dev->cq.lock);
2414 spin_lock_init (&dev->txq.lock);
2415 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2416 spin_lock_init (&dev->rxq[pool].lock);
2419 void setup_pci_dev (void) {
2420 unsigned char lat;
2422 /* XXX check return value */
2423 pci_enable_device (pci_dev);
2425 // enable bus master accesses
2426 pci_set_master (pci_dev);
2428 // frobnicate latency (upwards, usually)
2429 pci_read_config_byte (pci_dev, PCI_LATENCY_TIMER, &lat);
2430 if (pci_lat) {
2431 PRINTD (DBG_INIT, "%s PCI latency timer from %hu to %hu",
2432 "changing", lat, pci_lat);
2433 pci_write_config_byte (pci_dev, PCI_LATENCY_TIMER, pci_lat);
2434 } else if (lat < MIN_PCI_LATENCY) {
2435 PRINTK (KERN_INFO, "%s PCI latency timer from %hu to %hu",
2436 "increasing", lat, MIN_PCI_LATENCY);
2437 pci_write_config_byte (pci_dev, PCI_LATENCY_TIMER, MIN_PCI_LATENCY);
2441 PRINTD (DBG_INFO, "found Madge ATM adapter (amb) at"
2442 " IO %x, IRQ %u, MEM %p", iobase, irq, membase);
2444 // check IO region
2445 if (check_region (iobase, AMB_EXTENT)) {
2446 PRINTK (KERN_ERR, "IO range already in use!");
2447 return;
2450 dev = kmalloc (sizeof(amb_dev), GFP_KERNEL);
2451 if (!dev) {
2452 // perhaps we should be nice: deregister all adapters and abort?
2453 PRINTK (KERN_ERR, "out of memory!");
2454 return;
2457 setup_dev();
2459 if (amb_init (dev)) {
2460 PRINTK (KERN_ERR, "adapter initialisation failure");
2461 } else {
2463 setup_pci_dev();
2465 // grab (but share) IRQ and install handler
2466 if (request_irq (irq, interrupt_handler, SA_SHIRQ, DEV_LABEL, dev)) {
2467 PRINTK (KERN_ERR, "request IRQ failed!");
2468 // free_irq is at "endif"
2469 } else {
2471 // reserve IO region
2472 request_region (iobase, AMB_EXTENT, DEV_LABEL);
2474 dev->atm_dev = atm_dev_register (DEV_LABEL, &amb_ops, -1, NULL);
2475 if (!dev->atm_dev) {
2476 PRINTD (DBG_ERR, "failed to register Madge ATM adapter");
2477 } else {
2479 PRINTD (DBG_INFO, "registered Madge ATM adapter (no. %d) (%p) at %p",
2480 dev->atm_dev->number, dev, dev->atm_dev);
2481 dev->atm_dev->dev_data = (void *) dev;
2483 // register our address
2484 amb_esi (dev, dev->atm_dev->esi);
2486 // 0 bits for vpi, 10 bits for vci
2487 dev->atm_dev->ci_range.vpi_bits = NUM_VPI_BITS;
2488 dev->atm_dev->ci_range.vci_bits = NUM_VCI_BITS;
2490 // update count and linked list
2491 ++devs;
2492 dev->prev = amb_devs;
2493 amb_devs = dev;
2495 // enable host interrupts
2496 interrupts_on (dev);
2498 // success
2499 return;
2501 // not currently reached
2502 atm_dev_deregister (dev->atm_dev);
2503 } /* atm_dev_register */
2505 release_region (iobase, AMB_EXTENT);
2506 free_irq (irq, dev);
2507 } /* request_region, request_irq */
2509 amb_reset (dev, 0);
2510 } /* amb_init */
2512 kfree (dev);
2513 } /* kmalloc, end-of-fn */
2515 PRINTD (DBG_FLOW, "amb_probe");
2517 if (!pci_present())
2518 return 0;
2520 devs = 0;
2521 pci_dev = NULL;
2522 while ((pci_dev = pci_find_device
2523 (PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR, pci_dev)
2525 do_pci_device();
2527 pci_dev = NULL;
2528 while ((pci_dev = pci_find_device
2529 (PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD, pci_dev)
2531 PRINTK (KERN_ERR, "skipped broken (PLX rev 2) card");
2533 return devs;
2536 static void __init amb_check_args (void) {
2537 unsigned char pool;
2538 unsigned int max_rx_size;
2540 #ifdef DEBUG_AMBASSADOR
2541 PRINTK (KERN_NOTICE, "debug bitmap is %hx", debug &= DBG_MASK);
2542 #else
2543 if (debug)
2544 PRINTK (KERN_NOTICE, "no debugging support");
2545 #endif
2547 if (cmds < MIN_QUEUE_SIZE)
2548 PRINTK (KERN_NOTICE, "cmds has been raised to %u",
2549 cmds = MIN_QUEUE_SIZE);
2551 if (txs < MIN_QUEUE_SIZE)
2552 PRINTK (KERN_NOTICE, "txs has been raised to %u",
2553 txs = MIN_QUEUE_SIZE);
2555 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2556 if (rxs[pool] < MIN_QUEUE_SIZE)
2557 PRINTK (KERN_NOTICE, "rxs[%hu] has been raised to %u",
2558 pool, rxs[pool] = MIN_QUEUE_SIZE);
2560 // buffers sizes should be greater than zero and strictly increasing
2561 max_rx_size = 0;
2562 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2563 if (rxs_bs[pool] <= max_rx_size)
2564 PRINTK (KERN_NOTICE, "useless pool (rxs_bs[%hu] = %u)",
2565 pool, rxs_bs[pool]);
2566 else
2567 max_rx_size = rxs_bs[pool];
2569 if (rx_lats < MIN_RX_BUFFERS)
2570 PRINTK (KERN_NOTICE, "rx_lats has been raised to %u",
2571 rx_lats = MIN_RX_BUFFERS);
2573 return;
2576 /********** module stuff **********/
2578 #ifdef MODULE
2579 EXPORT_NO_SYMBOLS;
2581 MODULE_AUTHOR(maintainer_string);
2582 MODULE_DESCRIPTION(description_string);
2583 MODULE_PARM(debug, "h");
2584 MODULE_PARM(cmds, "i");
2585 MODULE_PARM(txs, "i");
2586 MODULE_PARM(rxs, __MODULE_STRING(NUM_RX_POOLS) "i");
2587 MODULE_PARM(rxs_bs, __MODULE_STRING(NUM_RX_POOLS) "i");
2588 MODULE_PARM(rx_lats, "i");
2589 MODULE_PARM(pci_lat, "b");
2590 MODULE_PARM_DESC(debug, "debug bitmap, see .h file");
2591 MODULE_PARM_DESC(cmds, "number of command queue entries");
2592 MODULE_PARM_DESC(txs, "number of TX queue entries");
2593 MODULE_PARM_DESC(rxs, "number of RX queue entries [" __MODULE_STRING(NUM_RX_POOLS) "]");
2594 MODULE_PARM_DESC(rxs_bs, "size of RX buffers [" __MODULE_STRING(NUM_RX_POOLS) "]");
2595 MODULE_PARM_DESC(rx_lats, "number of extra buffers to cope with RX latencies");
2596 MODULE_PARM_DESC(pci_lat, "PCI latency in bus cycles");
2598 /********** module entry **********/
2600 int init_module (void) {
2601 int devs;
2603 PRINTD (DBG_FLOW|DBG_INIT, "init_module");
2605 // sanity check - cast needed as printk does not support %Zu
2606 if (sizeof(amb_mem) != 4*16 + 4*12) {
2607 PRINTK (KERN_ERR, "Fix amb_mem (is %lu words).",
2608 (unsigned long) sizeof(amb_mem));
2609 return -ENOMEM;
2612 show_version();
2614 amb_check_args();
2616 // get the juice
2617 devs = amb_probe();
2619 if (devs) {
2620 init_timer (&housekeeping);
2621 housekeeping.function = do_housekeeping;
2622 // paranoia
2623 housekeeping.data = 1;
2624 set_timer (&housekeeping, 0);
2625 } else {
2626 PRINTK (KERN_INFO, "no (usable) adapters found");
2629 return devs ? 0 : -ENODEV;
2632 /********** module exit **********/
2634 void cleanup_module (void) {
2635 amb_dev * dev;
2637 PRINTD (DBG_FLOW|DBG_INIT, "cleanup_module");
2639 // paranoia
2640 housekeeping.data = 0;
2641 del_timer (&housekeeping);
2643 while (amb_devs) {
2644 dev = amb_devs;
2645 amb_devs = dev->prev;
2647 PRINTD (DBG_INFO|DBG_INIT, "closing %p (atm_dev = %p)", dev, dev->atm_dev);
2648 // the drain should not be necessary
2649 drain_rx_pools (dev);
2650 interrupts_off (dev);
2651 amb_reset (dev, 0);
2652 destroy_queues (dev);
2653 atm_dev_deregister (dev->atm_dev);
2654 free_irq (dev->irq, dev);
2655 release_region (dev->iobase, AMB_EXTENT);
2656 kfree (dev);
2659 return;
2662 #else
2664 /********** monolithic entry **********/
2666 int __init amb_detect (void) {
2667 int devs;
2669 // sanity check - cast needed as printk does not support %Zu
2670 if (sizeof(amb_mem) != 4*16 + 4*12) {
2671 PRINTK (KERN_ERR, "Fix amb_mem (is %lu words).",
2672 (unsigned long) sizeof(amb_mem));
2673 return 0;
2676 show_version();
2678 amb_check_args();
2680 // get the juice
2681 devs = amb_probe();
2683 if (devs) {
2684 init_timer (&housekeeping);
2685 housekeeping.function = do_housekeeping;
2686 // paranoia
2687 housekeeping.data = 1;
2688 set_timer (&housekeeping, 0);
2689 } else {
2690 PRINTK (KERN_INFO, "no (usable) adapters found");
2693 return devs;
2696 #endif