Import 2.3.18pre1
[davej-history.git] / drivers / atm / ambassador.c
blob5664f0ea21ee2737af47a719b9537738a6278ebb
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/io.h>
36 #include <asm/byteorder.h>
38 #include "ambassador.h"
40 #define maintainer_string "Giuliano Procida at Madge Networks <gprocida@madge.com>"
41 #define description_string "Madge ATM Ambassador driver"
42 #define version_string "1.2"
44 static inline void __init show_version (void) {
45 printk ("%s version %s\n", description_string, version_string);
50 Theory of Operation
52 I Hardware, detection, initialisation and shutdown.
54 1. Supported Hardware
56 This driver is for the PCI ATMizer-based Ambassador card (except
57 very early versions). It is not suitable for the similar EISA "TR7"
58 card. Commercially, both cards are known as Collage Server ATM
59 adapters.
61 The loader supports image transfer to the card, image start and few
62 other miscellaneous commands.
64 Only AAL5 is supported with vpi = 0 and vci in the range 0 to 1023.
66 The cards are big-endian.
68 2. Detection
70 Standard PCI stuff, the early cards are detected and rejected.
72 3. Initialisation
74 The cards are reset and the self-test results are checked. The
75 microcode image is then transferred and started. This waits for a
76 pointer to a descriptor containing details of the host-based queues
77 and buffers and various parameters etc. Once they are processed
78 normal operations may begin. The BIA is read using a microcode
79 command.
81 4. Shutdown
83 This may be accomplished either by a card reset or via the microcode
84 shutdown command. Further investigation required.
86 5. Persistent state
88 The card reset does not affect PCI configuration (good) or the
89 contents of several other "shared run-time registers" (bad) which
90 include doorbell and interrupt control as well as EEPROM and PCI
91 control. The driver must be careful when modifying these registers
92 not to touch bits it does not use and to undo any changes at exit.
94 II Driver software
96 0. Generalities
98 The adapter is quite intelligent (fast) and has a simple interface
99 (few features). VPI is always zero, 1024 VCIs are supported. There
100 is limited cell rate support. UBR channels can be kept and ABR
101 (explicit rate, bu not EFCI) is supported. There is no CBR or VBR
102 support.
104 1. Driver <-> Adapter Communication
106 Apart from the basic loader commands, the driver communicates
107 through three entities: the command queue (CQ), the transmit queue
108 pair (TXQ) and the receive queue pairs (RXQ). These three entities
109 are set up by the host and passed to the microcode just after it has
110 been started.
112 All queues are host-based circular queues. They are contiguous and
113 (due to hardware limitations) have some restrictions as to their
114 locations in (bus) memory. They are of the "full means the same as
115 empty so don't do that" variety since the adapter uses pointers
116 internally.
118 The queue pairs work as follows: one queue is for supply to the
119 adapter, items in it are pending and are owned by the adapter; the
120 other is the queue for return from the adapter, items in it have
121 been dealt with by the adapter. The host adds items to the supply
122 (TX descriptors and free RX buffer descriptors) and removes items
123 from the return (TX and RX completions). The adapter deals with out
124 of order completions.
126 Interrupts (card to host) and the doorbell (host to card) are used
127 for signalling.
129 1. CQ
131 This is to communicate "open VC", "close VC", "get stats" etc. to
132 the adapter. At most one command is retired every millisecond by the
133 card. There is no out of order completion or notification. The
134 driver needs to check the return code of the command, waiting as
135 appropriate.
137 2. TXQ
139 TX supply items are of variable length (scatter gather support) and
140 so the queue items are (more or less) pointers to the real thing.
141 Each TX supply item contains a unique, host-supplied handle (the skb
142 bus address seems most sensible as this works for Alphas as well,
143 there is no need to do any endian conversions on the handles).
145 TX return items consist of just the handles above.
147 3. RXQ (up to 4 of these with different lengths and buffer sizes)
149 RX supply items consist of a unique, host-supplied handle (the skb
150 bus address again) and a pointer to the buffer data area.
152 RX return items consist of the handle above, the VC, length and a
153 status word. This just screams "oh so easy" doesn't it?
155 Note on RX pool sizes:
157 Each pool should have enough buffers to handle a back-to-back stream
158 of minimum sized frames on a single VC. For example:
160 frame spacing = 3us (about right)
162 delay = IRQ lat + RX handling + RX buffer replenish = 20 (us) (a guess)
164 min number of buffers for one VC = 1 + delay/spacing (buffers)
166 delay/spacing = latency = (20+2)/3 = 7 (buffers) (rounding up)
168 The 20us delay assumes that there is no need to sleep; if we need to
169 sleep to get buffers we are going to drop frames anyway.
171 In fact, each pool should have enough buffers to support the
172 simultaneous reassembly of a separate frame on each VC and cope with
173 the case in which frames complete in round robin cell fashion on
174 each VC.
176 Only one frame can complete at each cell arrival, so if "n" VCs are
177 open, the worst case is to have them all complete frames together
178 followed by all starting new frames together.
180 desired number of buffers = n + delay/spacing
182 These are the extreme requirements, however, they are "n+k" for some
183 "k" so we have only the constant to choose. This is the argument
184 rx_lats which current defaults to 7.
186 Actually, "n ? n+k : 0" is better and this is what is implemented,
187 subject to the limit given by the pool size.
189 4. Driver locking
191 Simple spinlocks are used around the TX and RX queue mechanisms.
192 Anyone with a faster, working method is welcome to implement it.
194 The adapter command queue is protected with a spinlock. We always
195 wait for commands to complete.
197 A more complex form of locking is used around parts of the VC open
198 and close functions. There are three reasons for a lock: 1. we need
199 to do atomic rate reservation and release (not used yet), 2. Opening
200 sometimes involves two adapter commands which must not be separated
201 by another command on the same VC, 3. the changes to RX pool size
202 must be atomic. The lock needs to work over context switches, so we
203 use a semaphore.
205 III Hardware Features and Microcode Bugs
207 1. Byte Ordering
209 *%^"$&%^$*&^"$(%^$#&^%$(&#%$*(&^#%!"!"!*!
211 2. Memory access
213 All structures that are not accessed using DMA must be 4-byte
214 aligned (not a problem) and must not cross 4MB boundaries.
216 There is a DMA memory hole at E0000000-E00000FF (groan).
218 TX fragments (DMA read) must not cross 4MB boundaries (would be 16MB
219 but for a hardware bug).
221 RX buffers (DMA write) must not cross 16MB boundaries and must
222 include spare trailing bytes up to the next 4-byte boundary; they
223 will be written with rubbish.
225 The PLX likes to prefetch; if reading up to 4 u32 past the end of
226 each TX fragment is not a problem, then TX can be made to go a
227 little faster by passing a flag at init that disables a prefetch
228 workaround. We do not pass this flag. (new microcode only)
230 Now we:
231 . Note that alloc_skb rounds up size to a 16byte boundary.
232 . Ensure all areas do not traverse 4MB boundaries.
233 . Ensure all areas do not start at a E00000xx bus address.
234 (I cannot be certain, but this may always hold with Linux)
235 . Make all failures cause a loud message.
236 . Discard non-conforming SKBs (causes TX failure or RX fill delay).
237 . Discard non-conforming TX fragment descriptors (the TX fails).
238 In the future we could:
239 . Allow RX areas that traverse 4MB (but not 16MB) boundaries.
240 . Segment TX areas into some/more fragments, when necessary.
241 . Relax checks for non-DMA items (ignore hole).
242 . Give scatter-gather (iovec) requirements using ???. (?)
244 3. VC close is broken (only for new microcode)
246 The VC close adapter microcode command fails to do anything if any
247 frames have been received on the VC but none have been transmitted.
248 Frames continue to be reassembled and passed (with IRQ) to the
249 driver.
251 IV To Do List
253 . Fix bugs!
255 . Timer code may be broken.
257 . Deal with buggy VC close (somehow) in microcode 12.
259 . Handle interrupted and/or non-blocking writes - is this a job for
260 the protocol layer?
262 . Add code to break up TX fragments when they span 4MB boundaries.
264 . Add SUNI phy layer (need to know where SUNI lives on card).
266 . Implement a tx_alloc fn to (a) satisfy TX alignment etc. and (b)
267 leave extra headroom space for Ambassador TX descriptors.
269 . Understand these elements of struct atm_vcc: recvq (proto?),
270 sleep, callback, listenq, backlog_quota, reply and user_back.
272 . Adjust TX/RX skb allocation to favour IP with LANE/CLIP (configurable).
274 . Impose a TX-pending limit (2?) on each VC, help avoid TX q overflow.
276 . Decide whether RX buffer recycling is or can be made completely safe;
277 turn it back on. It looks like Werner is going to axe this.
279 . Implement QoS changes on open VCs (involves extracting parts of VC open
280 and close into separate functions and using them to make changes).
282 . Hack on command queue so that someone can issue multiple commands and wait
283 on the last one (OR only "no-op" or "wait" commands are waited for).
285 . Eliminate need for while-schedule around do_command.
289 /********** microcode **********/
291 #ifdef AMB_NEW_MICROCODE
292 #define UCODE(x) "atmsar12" "." #x
293 #else
294 #define UCODE(x) "atmsar11" "." #x
295 #endif
297 static const u32 __initdata ucode_start =
298 #include UCODE(start)
301 static const region __initdata ucode_regions[] = {
302 #include UCODE(regions)
303 { 0, 0 }
306 static const u32 __initdata ucode_data[] = {
307 #include UCODE(data)
308 0xdeadbeef
311 /********** globals **********/
313 static amb_dev * amb_devs = NULL;
314 static struct timer_list housekeeping;
316 static unsigned short debug = 0;
317 static unsigned int cmds = 8;
318 static unsigned int txs = 32;
319 static unsigned int rxs[NUM_RX_POOLS] = { 64, 64, 64, 64 };
320 static unsigned int rxs_bs[NUM_RX_POOLS] = { 4080, 12240, 36720, 65535 };
321 static unsigned int rx_lats = 7;
322 static unsigned char pci_lat = 0;
324 /********** access to adapter **********/
326 static const amb_mem * const mem = 0;
328 static inline void wr_mem (const amb_dev * dev, const u32 * addr, u32 data) {
329 u32 be = cpu_to_be32 (data);
330 PRINTD (DBG_FLOW|DBG_REGS, "wr: %p <- %08x b[%08x]", addr, data, be);
331 #ifdef AMB_MMIO
332 dev->membase[addr - (u32 *) 0] = be;
333 #else
334 outl (be, dev->iobase + (addr - (u32 *) 0) * sizeof(u32));
335 #endif
338 static inline u32 rd_mem (const amb_dev * dev, const u32 * addr) {
339 #ifdef AMB_MMIO
340 u32 be = dev->membase[addr - (u32 *) 0];
341 #else
342 u32 be = inl (dev->iobase + (addr - (u32 *) 0) * sizeof(u32));
343 #endif
344 u32 data = be32_to_cpu (be);
345 PRINTD (DBG_FLOW|DBG_REGS, "rd: %p -> %08x b[%08x]", addr, data, be);
346 return data;
349 /********** dump routines **********/
351 static inline void dump_registers (const amb_dev * dev) {
352 #ifdef DEBUG_AMBASSADOR
353 // u32 * i;
354 // PRINTD (DBG_REGS, "mailboxes: ");
355 // for (i = (u32 *) 0x40; i < (u32 *) 0x60; ++i)
356 // PRINTD (DBG_REGS, "%08x ", rd_mem (dev, i));
357 PRINTD (DBG_REGS, "doorb %08x", rd_mem (dev, (u32 *) 0x60));
358 PRINTD (DBG_REGS, "irqev %08x", rd_mem (dev, (u32 *) 0x64));
359 PRINTD (DBG_REGS, "irqen %08x", rd_mem (dev, (u32 *) 0x68));
360 PRINTD (DBG_REGS, "reset %08x", rd_mem (dev, (u32 *) 0x6c));
361 #else
362 (void) dev;
363 #endif
364 return;
367 static inline void dump_loader_block (volatile loader_block * lb) {
368 #ifdef DEBUG_AMBASSADOR
369 unsigned int i;
370 PRINTDB (DBG_LOAD, "lb @ %p; res: %d, cmd: %d, pay:",
371 lb, be32_to_cpu (lb->result), be32_to_cpu (lb->command));
372 for (i = 0; i < MAX_COMMAND_DATA; ++i)
373 PRINTDM (DBG_LOAD, " %08x", be32_to_cpu (lb->payload.data[i]));
374 PRINTDE (DBG_LOAD, ", vld: %08x", be32_to_cpu (lb->valid));
375 #else
376 (void) lb;
377 #endif
378 return;
381 static inline void dump_command (command * cmd) {
382 #ifdef DEBUG_AMBASSADOR
383 unsigned int i;
384 PRINTDB (DBG_CMD, "cmd @ %p, req: %08x, pars:",
385 cmd, /*be32_to_cpu*/ (cmd->request));
386 for (i = 0; i < 3; ++i)
387 PRINTDM (DBG_CMD, " %08x", /*be32_to_cpu*/ (cmd->args.par[i]));
388 PRINTDE (DBG_CMD, "");
389 #else
390 (void) cmd;
391 #endif
392 return;
395 static inline void dump_skb (char * prefix, unsigned int vc, struct sk_buff * skb) {
396 #ifdef DEBUG_AMBASSADOR
397 unsigned int i;
398 unsigned char * data = skb->data;
399 PRINTDB (DBG_DATA, "%s(%u) ", prefix, vc);
400 for (i=0; i<skb->len && i < 256;i++)
401 PRINTDM (DBG_DATA, "%02x ", data[i]);
402 PRINTDE (DBG_DATA,"");
403 #else
404 (void) prefix;
405 (void) vc;
406 (void) skb;
407 #endif
408 return;
411 /********** check memory areas for use by Ambassador **********/
413 /* see limitations under Hardware Features */
415 static inline int check_area (void * start, size_t length) {
416 // assumes length > 0
417 const u32 fourmegmask = (-1)<<22;
418 const u32 twofivesixmask = (-1)<<8;
419 const u32 starthole = 0xE0000000;
420 u32 startaddress = virt_to_bus (start);
421 u32 lastaddress = startaddress+length-1;
422 if ((startaddress ^ lastaddress) & fourmegmask ||
423 (startaddress & twofivesixmask) == starthole) {
424 PRINTK (KERN_ERR, "check_area failure: [%x,%x] - mail maintainer!",
425 startaddress, lastaddress);
426 return -1;
427 } else {
428 return 0;
432 /********** free an skb (as per ATM device driver documentation) **********/
434 static inline void amb_kfree_skb (struct sk_buff * skb) {
435 if (ATM_SKB(skb)->vcc->pop) {
436 ATM_SKB(skb)->vcc->pop (ATM_SKB(skb)->vcc, skb);
437 } else {
438 dev_kfree_skb (skb);
442 /********** TX completion **********/
444 static inline void tx_complete (amb_dev * dev, tx_out * tx) {
445 tx_simple * tx_descr = bus_to_virt (tx->handle);
446 struct sk_buff * skb = tx_descr->skb;
448 PRINTD (DBG_FLOW|DBG_TX, "tx_complete %p %p", dev, tx);
450 // VC layer stats
451 ATM_SKB(skb)->vcc->stats->tx++;
453 // free the descriptor
454 kfree (tx_descr);
456 // free the skb
457 amb_kfree_skb (skb);
459 dev->stats.tx_ok++;
460 return;
463 /********** RX completion **********/
465 static void rx_complete (amb_dev * dev, rx_out * rx) {
466 struct sk_buff * skb = bus_to_virt (rx->handle);
467 u16 vc = be16_to_cpu (rx->vc);
468 // unused: u16 lec_id = be16_to_cpu (rx->lec_id);
469 u16 status = be16_to_cpu (rx->status);
470 u16 rx_len = be16_to_cpu (rx->length);
472 PRINTD (DBG_FLOW|DBG_RX, "rx_complete %p %p (len=%hu)", dev, rx, rx_len);
474 // XXX move this in and add to VC stats ???
475 if (!status) {
476 struct atm_vcc * atm_vcc = dev->rxer[vc];
477 dev->stats.rx.ok++;
479 if (atm_vcc) {
481 if (rx_len <= atm_vcc->qos.rxtp.max_sdu) {
483 if (atm_charge (atm_vcc, skb->truesize)) {
485 // prepare socket buffer
486 ATM_SKB(skb)->vcc = atm_vcc;
487 skb_put (skb, rx_len);
489 dump_skb ("<<<", vc, skb);
491 // VC layer stats
492 atm_vcc->stats->rx++;
493 skb->stamp = xtime;
494 // end of our responsability
495 atm_vcc->push (atm_vcc, skb);
496 return;
498 } else {
499 // someone fix this (message), please!
500 PRINTD (DBG_INFO|DBG_RX, "dropped thanks to atm_charge (vc %hu, truesize %u)", vc, skb->truesize);
501 // drop stats incremented in atm_charge
504 } else {
505 PRINTK (KERN_INFO, "dropped over-size frame");
506 // should we count this?
507 atm_vcc->stats->rx_drop++;
510 } else {
511 PRINTD (DBG_WARN|DBG_RX, "got frame but RX closed for channel %hu", vc);
512 // this is an adapter bug, only in new version of microcode
515 } else {
516 dev->stats.rx.error++;
517 if (status & CRC_ERR)
518 dev->stats.rx.badcrc++;
519 if (status & LEN_ERR)
520 dev->stats.rx.toolong++;
521 if (status & ABORT_ERR)
522 dev->stats.rx.aborted++;
523 if (status & UNUSED_ERR)
524 dev->stats.rx.unused++;
527 dev_kfree_skb (skb);
528 return;
533 Note on queue handling.
535 Here "give" and "take" refer to queue entries and a queue (pair)
536 rather than frames to or from the host or adapter. Empty frame
537 buffers are given to the RX queue pair and returned unused or
538 containing RX frames. TX frames (well, pointers to TX fragment
539 lists) are given to the TX queue pair, completions are returned.
543 /********** command queue **********/
545 // I really don't like this, but it's the best I can do at the moment
547 // also, the callers are responsible for byte order as the microcode
548 // sometimes does 16-bit accesses (yuk yuk yuk)
550 static int command_do (amb_dev * dev, command * cmd) {
551 volatile amb_cq * cq = &dev->cq;
552 command * my_slot;
553 unsigned long timeout;
555 PRINTD (DBG_FLOW|DBG_CMD, "command_do %p", dev);
557 if (test_bit (dead, &dev->flags))
558 return 0;
560 spin_lock (&cq->lock);
562 // if not full...
563 if (cq->pending < cq->maximum) {
564 // remember my slot for later
565 my_slot = cq->in;
566 PRINTD (DBG_CMD, "command in slot %p", my_slot);
568 dump_command (cmd);
570 // copy command in
571 *cq->in = *cmd;
572 cq->pending++;
573 cq->in = NEXTQ (cq->in, cq->start, cq->limit);
575 // mail the command
576 wr_mem (dev, &mem->mb.adapter.cmd_address, virt_to_bus (cq->in));
578 // prepare to wait for cq->pending milliseconds
579 // effectively one centisecond on i386
580 timeout = (cq->pending*HZ+999)/1000;
582 if (cq->pending > cq->high)
583 cq->high = cq->pending;
584 spin_unlock (&cq->lock);
586 while (timeout) {
587 // go to sleep
588 // PRINTD (DBG_CMD, "wait: sleeping %lu for command", timeout);
589 timeout = schedule_timeout (timeout);
590 // woken up by timeout or signal
593 // wait for my slot to be reached (all waiters are here or above, until...)
594 while (cq->out != my_slot) {
595 PRINTD (DBG_CMD, "wait: command slot (now at %p)", cq->out);
596 schedule();
599 // wait on my slot (... one gets to its slot, and... )
600 while (cq->out->request != cpu_to_be32 (SRB_COMPLETE)) {
601 PRINTD (DBG_CMD, "wait: command slot completion");
602 schedule();
605 PRINTD (DBG_CMD, "command complete");
606 // update queue (... moves the queue along to the next slot)
607 spin_lock (&cq->lock);
608 cq->pending--;
609 // copy command out
610 *cmd = *cq->out;
611 cq->out = NEXTQ (cq->out, cq->start, cq->limit);
612 spin_unlock (&cq->lock);
614 return 0;
615 } else {
616 spin_unlock (&cq->lock);
617 return -EAGAIN;
622 /********** TX queue pair **********/
624 static inline int tx_give (amb_dev * dev, tx_in * tx) {
625 amb_txq * txq = &dev->txq;
626 unsigned long flags;
628 PRINTD (DBG_FLOW|DBG_TX, "tx_give %p", dev);
630 if (test_bit (dead, &dev->flags))
631 return 0;
633 spin_lock_irqsave (&txq->lock, flags);
635 if (txq->pending < txq->maximum) {
636 PRINTD (DBG_TX, "TX in slot %p", txq->in.ptr);
638 *txq->in.ptr = *tx;
639 txq->pending++;
640 txq->in.ptr = NEXTQ (txq->in.ptr, txq->in.start, txq->in.limit);
641 // hand over the TX and ring the bell
642 wr_mem (dev, &mem->mb.adapter.tx_address, virt_to_bus (txq->in.ptr));
643 wr_mem (dev, &mem->doorbell, TX_FRAME);
645 if (txq->pending > txq->high)
646 txq->high = txq->pending;
647 spin_unlock_irqrestore (&txq->lock, flags);
648 return 0;
649 } else {
650 txq->filled++;
651 spin_unlock_irqrestore (&txq->lock, flags);
652 return -EAGAIN;
656 static inline int tx_take (amb_dev * dev) {
657 amb_txq * txq = &dev->txq;
658 unsigned long flags;
660 PRINTD (DBG_FLOW|DBG_TX, "tx_take %p", dev);
662 spin_lock_irqsave (&txq->lock, flags);
664 if (txq->pending && txq->out.ptr->handle) {
665 // deal with TX completion
666 tx_complete (dev, txq->out.ptr);
667 // mark unused again
668 txq->out.ptr->handle = 0;
669 // remove item
670 txq->pending--;
671 txq->out.ptr = NEXTQ (txq->out.ptr, txq->out.start, txq->out.limit);
673 spin_unlock_irqrestore (&txq->lock, flags);
674 return 0;
675 } else {
677 spin_unlock_irqrestore (&dev->lock, flags);
678 return -1;
682 /********** RX queue pairs **********/
684 static inline int rx_give (amb_dev * dev, rx_in * rx, unsigned char pool) {
685 amb_rxq * rxq = &dev->rxq[pool];
686 unsigned long flags;
688 PRINTD (DBG_FLOW|DBG_RX, "rx_give %p[%hu]", dev, pool);
690 spin_lock_irqsave (&rxq->lock, flags);
692 if (rxq->pending < rxq->maximum) {
693 PRINTD (DBG_RX, "RX in slot %p", rxq->in.ptr);
695 *rxq->in.ptr = *rx;
696 rxq->pending++;
697 rxq->in.ptr = NEXTQ (rxq->in.ptr, rxq->in.start, rxq->in.limit);
698 // hand over the RX buffer
699 wr_mem (dev, &mem->mb.adapter.rx_address[pool], virt_to_bus (rxq->in.ptr));
701 spin_unlock_irqrestore (&rxq->lock, flags);
702 return 0;
703 } else {
704 spin_unlock_irqrestore (&rxq->lock, flags);
705 return -1;
709 static inline int rx_take (amb_dev * dev, unsigned char pool) {
710 amb_rxq * rxq = &dev->rxq[pool];
711 unsigned long flags;
713 PRINTD (DBG_FLOW|DBG_RX, "rx_take %p[%hu]", dev, pool);
715 spin_lock_irqsave (&rxq->lock, flags);
717 if (rxq->pending && (rxq->out.ptr->status || rxq->out.ptr->length)) {
718 // deal with RX completion
719 rx_complete (dev, rxq->out.ptr);
720 // mark unused again
721 rxq->out.ptr->status = 0;
722 rxq->out.ptr->length = 0;
723 // remove item
724 rxq->pending--;
725 rxq->out.ptr = NEXTQ (rxq->out.ptr, rxq->out.start, rxq->out.limit);
727 if (rxq->pending < rxq->low)
728 rxq->low = rxq->pending;
729 spin_unlock_irqrestore (&rxq->lock, flags);
730 return 0;
731 } else {
732 if (!rxq->pending && rxq->buffers_wanted)
733 rxq->emptied++;
734 spin_unlock_irqrestore (&rxq->lock, flags);
735 return -1;
739 /********** RX Pool handling **********/
741 /* pre: buffers_wanted = 0, post: pending = 0 */
742 static inline void drain_rx_pool (amb_dev * dev, unsigned char pool) {
743 amb_rxq * rxq = &dev->rxq[pool];
745 PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pool %p %hu", dev, pool);
747 if (test_bit (dead, &dev->flags))
748 return;
750 /* we are not quite like the fill pool routines as we cannot just
751 remove one buffer, we have to remove all of them, but we might as
752 well pretend... */
753 if (rxq->pending > rxq->buffers_wanted) {
754 command cmd;
755 cmd.request = cpu_to_be32 (SRB_FLUSH_BUFFER_Q);
756 cmd.args.flush.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
757 while (command_do (dev, &cmd))
758 schedule();
759 /* the pool may also be emptied via the interrupt handler */
760 while (rxq->pending > rxq->buffers_wanted)
761 if (rx_take (dev, pool))
762 schedule();
765 return;
768 #ifdef MODULE
769 static void drain_rx_pools (amb_dev * dev) {
770 unsigned char pool;
772 PRINTD (DBG_FLOW|DBG_POOL, "drain_rx_pools %p", dev);
774 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
775 drain_rx_pool (dev, pool);
777 return;
779 #endif
781 static inline void fill_rx_pool (amb_dev * dev, unsigned char pool, int priority) {
782 rx_in rx;
783 amb_rxq * rxq;
785 PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pool %p %hu %x", dev, pool, priority);
787 if (test_bit (dead, &dev->flags))
788 return;
790 rxq = &dev->rxq[pool];
791 while (rxq->pending < rxq->maximum && rxq->pending < rxq->buffers_wanted) {
793 struct sk_buff * skb = alloc_skb (rxq->buffer_size, priority);
794 if (!skb) {
795 PRINTD (DBG_SKB|DBG_POOL, "failed to allocate skb for RX pool %hu", pool);
796 return;
798 if (check_area (skb->data, skb->truesize)) {
799 dev_kfree_skb (skb);
800 return;
802 // cast needed as there is no %? for pointer differences
803 PRINTD (DBG_SKB, "allocated skb at %p, head %p, area %li",
804 skb, skb->head, (long) (skb->end - skb->head));
805 rx.handle = virt_to_bus (skb);
806 rx.host_address = cpu_to_be32 (virt_to_bus (skb->data));
807 if (rx_give (dev, &rx, pool))
808 dev_kfree_skb (skb);
812 return;
815 // top up all RX pools (also called as a bottom half)
816 static void fill_rx_pools (amb_dev * dev) {
817 unsigned char pool;
819 PRINTD (DBG_FLOW|DBG_POOL, "fill_rx_pools %p", dev);
821 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
822 fill_rx_pool (dev, pool, GFP_ATOMIC);
824 return;
827 /********** enable host interrupts **********/
829 static inline void interrupts_on (amb_dev * dev) {
830 wr_mem (dev, &mem->interrupt_control,
831 rd_mem (dev, &mem->interrupt_control)
832 | AMB_INTERRUPT_BITS);
835 /********** disable host interrupts **********/
837 static inline void interrupts_off (amb_dev * dev) {
838 wr_mem (dev, &mem->interrupt_control,
839 rd_mem (dev, &mem->interrupt_control)
840 &~ AMB_INTERRUPT_BITS);
843 /********** interrupt handling **********/
845 static void interrupt_handler (int irq, void * dev_id, struct pt_regs * pt_regs) {
846 amb_dev * dev = amb_devs;
847 unsigned int irq_ok;
848 unsigned int irq_ok_old;
849 (void) pt_regs;
851 PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler: %p", dev_id);
853 if (!dev_id) {
854 PRINTD (DBG_IRQ|DBG_ERR, "irq with NULL dev_id: %d", irq);
855 return;
857 // Did one of our cards generate the interrupt?
858 while (dev) {
859 if (dev == dev_id)
860 break;
861 dev = dev->prev;
863 if (!dev) {
864 PRINTD (DBG_IRQ, "irq not for me: %d", irq);
865 return;
867 if (irq != dev->irq) {
868 PRINTD (DBG_IRQ|DBG_ERR, "irq mismatch: %d", irq);
869 return;
872 // definitely for us
873 irq_ok = 0;
874 irq_ok_old = -1;
876 // perhaps disable interrupts? (disabled at PIC by Linux)
877 // interrupts_off (dev);
879 while (irq_ok_old != irq_ok && irq_ok < 100) {
880 unsigned char pool;
881 PRINTD (DBG_IRQ, "FYI: interrupt was %08x, work %u",
882 rd_mem (dev, &mem->interrupt), irq_ok);
883 wr_mem (dev, &mem->interrupt, -1);
884 irq_ok_old = irq_ok;
885 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
886 while (!rx_take (dev, pool))
887 ++irq_ok;
888 while (!tx_take (dev))
889 ++irq_ok;
892 if (irq_ok) {
893 #if 0
894 queue_task (&dev->bh, &tq_immediate);
895 mark_bh (IMMEDIATE_BH);
896 #else
897 fill_rx_pools (dev);
898 #endif
900 PRINTD (DBG_IRQ, "work done: %u", irq_ok);
901 } else {
902 PRINTD (DBG_IRQ|DBG_WARN, "no work done");
905 // perhaps re-enable interrupts? (re-enabled at PIC by Linux)
906 // interrupts_on (dev);
907 PRINTD (DBG_IRQ|DBG_FLOW, "interrupt_handler done: %p", dev_id);
908 return;
911 /********** don't panic... yeah, right **********/
913 #ifdef DEBUG_AMBASSADOR
914 static void dont_panic (amb_dev * dev) {
915 amb_cq * cq = &dev->cq;
916 amb_txq * txq;
917 amb_rxq * rxq;
918 command * cmd;
919 tx_in * tx;
920 tx_simple * tx_descr;
921 unsigned char pool;
922 rx_in * rx;
924 unsigned long flags;
925 save_flags (flags);
926 cli();
928 PRINTK (KERN_INFO, "don't panic - putting adapter into reset");
929 wr_mem (dev, &mem->reset_control, rd_mem (dev, &mem->reset_control) | AMB_RESET);
931 PRINTK (KERN_INFO, "marking all commands complete");
932 for (cmd = cq->start; cmd < cq->limit; ++cmd)
933 cmd->request = cpu_to_be32 (SRB_COMPLETE);
935 PRINTK (KERN_INFO, "completing all TXs");
936 txq = &dev->txq;
937 tx = txq->in.ptr;
938 while (txq->pending--) {
939 if (tx == txq->in.start)
940 tx = txq->in.limit;
941 --tx;
942 tx_descr = bus_to_virt (be32_to_cpu (tx->tx_descr_addr));
943 amb_kfree_skb (tx_descr->skb);
944 kfree (tx_descr);
947 PRINTK (KERN_INFO, "freeing all RX buffers");
948 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
949 rxq = &dev->rxq[pool];
950 rx = rxq->in.ptr;
951 while (rxq->pending--) {
952 if (rx == rxq->in.start)
953 rx = rxq->in.limit;
954 --rx;
955 dev_kfree_skb (bus_to_virt (rx->handle));
959 PRINTK (KERN_INFO, "don't panic over - close all VCs and rmmod");
960 set_bit (dead, &dev->flags);
961 restore_flags (flags);
962 return;
964 #endif
966 /********** make rate (not quite as much fun as Horizon) **********/
968 static unsigned int make_rate (unsigned int rate, rounding r,
969 u16 * bits, unsigned int * actual) {
970 unsigned char exp = -1; // hush gcc
971 unsigned int man = -1; // hush gcc
973 PRINTD (DBG_FLOW|DBG_QOS, "make_rate %u", rate);
975 // rates in cells per second, ITU format (nasty 16bit fp)
976 // given 5-bit e and 9-bit m:
977 // rate = EITHER (1+m/2^9)*2^e OR 0
978 // bits = EITHER 1<<14 | e<<9 | m OR 0
979 // (bit 15 is "reserved", bit 14 "non-zero")
980 // smallest rate is 0 (special representation)
981 // largest rate is (1+511/512)*2^31 = 4290772992 (< 2^32-1)
982 // simple algorithm:
983 // find position of top bit, this gives e
984 // remove top bit and shift (rounding if feeling clever) by 9-e
986 // ucode bug: please don't set bit 14! 0 not representable
988 if (rate > 0xffc00000U) {
989 // larger than largest representable rate
991 if (r == round_up) {
992 return -EINVAL;
993 } else {
994 exp = 31;
995 man = 511;
998 } else if (rate) {
999 // representable rate
1001 exp = 31;
1002 man = rate;
1004 // invariant: rate = man*2^(exp-31)
1005 while (!(man & (1<<31))) {
1006 exp = exp - 1;
1007 man = man<<1;
1010 // man has top bit set
1011 // rate = (2^31+(man-2^31))*2^(exp-31)
1012 // rate = (1+(man-2^31)/2^31)*2^exp
1013 man = man<<1;
1014 man &= 0xffffffffU; // a nop on 32-bit systems
1015 // rate = (1+man/2^32)*2^exp
1017 // exp is in the range 0 to 31, man is in the range 0 to 2^32-1
1018 // time to lose significance... we want m in the range 0 to 2^9-1
1019 // rounding presents a minor problem... we first decide which way
1020 // we are rounding (based on given rounding direction and possibly
1021 // the bits of the mantissa that are to be discarded).
1023 switch (r) {
1024 case round_down: {
1025 // just truncate
1026 man = man>>(32-9);
1027 break;
1029 case round_up: {
1030 // check all bits that we are discarding
1031 if (man & (-1>>9)) {
1032 man = (man>>(32-9)) + 1;
1033 if (man == (1<<9)) {
1034 // no need to check for round up outside of range
1035 man = 0;
1036 exp += 1;
1038 } else {
1039 man = (man>>(32-9));
1041 break;
1043 case round_nearest: {
1044 // check msb that we are discarding
1045 if (man & (1<<(32-9-1))) {
1046 man = (man>>(32-9)) + 1;
1047 if (man == (1<<9)) {
1048 // no need to check for round up outside of range
1049 man = 0;
1050 exp += 1;
1052 } else {
1053 man = (man>>(32-9));
1055 break;
1059 } else {
1060 // zero rate - not representable
1062 if (r == round_down) {
1063 return -EINVAL;
1064 } else {
1065 exp = 0;
1066 man = 0;
1071 PRINTD (DBG_QOS, "rate: man=%u, exp=%hu", man, exp);
1073 if (bits)
1074 *bits = /* (1<<14) | */ (exp<<9) | man;
1076 if (actual)
1077 *actual = (exp >= 9)
1078 ? (1 << exp) + (man << (exp-9))
1079 : (1 << exp) + ((man + (1<<(9-exp-1))) >> (9-exp));
1081 return 0;
1084 /********** Linux ATM Operations **********/
1086 // some are not yet implemented while others do not make sense for
1087 // this device
1089 /********** Open a VC **********/
1091 static int amb_open (struct atm_vcc * atm_vcc, short vpi, int vci) {
1092 int error;
1094 struct atm_qos * qos;
1095 struct atm_trafprm * txtp;
1096 struct atm_trafprm * rxtp;
1097 u16 tx_rate_bits;
1098 u16 tx_vc_bits = -1; // hush gcc
1099 u16 tx_frame_bits = -1; // hush gcc
1101 amb_dev * dev = AMB_DEV(atm_vcc->dev);
1102 amb_vcc * vcc;
1103 unsigned char pool = -1; // hush gcc
1105 PRINTD (DBG_FLOW|DBG_VCC, "amb_open %x %x", vpi, vci);
1107 #ifdef ATM_VPI_UNSPEC
1108 // UNSPEC is deprecated, remove this code eventually
1109 if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) {
1110 PRINTK (KERN_WARNING, "rejecting open with unspecified VPI/VCI (deprecated)");
1111 return -EINVAL;
1113 #endif
1115 // deal with possibly wildcarded VCs
1116 error = atm_find_ci (atm_vcc, &vpi, &vci);
1117 if (error) {
1118 PRINTD (DBG_WARN|DBG_VCC, "atm_find_ci failed!");
1119 return error;
1121 PRINTD (DBG_VCC, "atm_find_ci gives %x %x", vpi, vci);
1123 if (!(0 <= vpi && vpi < (1<<NUM_VPI_BITS) &&
1124 0 <= vci && vci < (1<<NUM_VCI_BITS))) {
1125 PRINTD (DBG_WARN|DBG_VCC, "VPI/VCI out of range: %hd/%d", vpi, vci);
1126 return -EINVAL;
1129 qos = &atm_vcc->qos;
1131 if (qos->aal != ATM_AAL5) {
1132 PRINTD (DBG_QOS, "AAL not supported");
1133 return -EINVAL;
1136 // traffic parameters
1138 PRINTD (DBG_QOS, "TX:");
1139 txtp = &qos->txtp;
1140 if (txtp->traffic_class != ATM_NONE) {
1141 switch (txtp->traffic_class) {
1142 case ATM_UBR: {
1143 // we take "the PCR" as a rate-cap
1144 int pcr = atm_pcr_goal (txtp);
1145 if (!pcr) {
1146 // no rate cap
1147 tx_rate_bits = 0;
1148 tx_vc_bits = TX_UBR;
1149 tx_frame_bits = TX_FRAME_NOTCAP;
1150 } else {
1151 rounding r;
1152 if (pcr < 0) {
1153 r = round_down;
1154 pcr = -pcr;
1155 } else {
1156 r = round_up;
1158 error = make_rate (pcr, r, &tx_rate_bits, 0);
1159 tx_vc_bits = TX_UBR_CAPPED;
1160 tx_frame_bits = TX_FRAME_CAPPED;
1162 break;
1164 #if 0
1165 case ATM_ABR: {
1166 pcr = atm_pcr_goal (txtp);
1167 PRINTD (DBG_QOS, "pcr goal = %d", pcr);
1168 break;
1170 #endif
1171 default: {
1172 // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
1173 PRINTD (DBG_QOS, "request for non-UBR denied");
1174 return -EINVAL;
1177 PRINTD (DBG_QOS, "tx_rate_bits=%hx, tx_vc_bits=%hx",
1178 tx_rate_bits, tx_vc_bits);
1181 PRINTD (DBG_QOS, "RX:");
1182 rxtp = &qos->rxtp;
1183 if (rxtp->traffic_class == ATM_NONE) {
1184 // do nothing
1185 } else {
1186 // choose an RX pool (arranged in increasing size)
1187 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
1188 if ((unsigned int) rxtp->max_sdu <= dev->rxq[pool].buffer_size) {
1189 PRINTD (DBG_VCC|DBG_QOS|DBG_POOL, "chose pool %hu (max_sdu %u <= %u)",
1190 pool, rxtp->max_sdu, dev->rxq[pool].buffer_size);
1191 break;
1193 if (pool == NUM_RX_POOLS) {
1194 PRINTD (DBG_WARN|DBG_VCC|DBG_QOS|DBG_POOL,
1195 "no pool suitable for VC (RX max_sdu %d is too large)",
1196 rxtp->max_sdu);
1197 return -EINVAL;
1200 switch (rxtp->traffic_class) {
1201 case ATM_UBR: {
1202 break;
1204 #if 0
1205 case ATM_ABR: {
1206 pcr = atm_pcr_goal (rxtp);
1207 PRINTD (DBG_QOS, "pcr goal = %d", pcr);
1208 break;
1210 #endif
1211 default: {
1212 // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
1213 PRINTD (DBG_QOS, "request for non-UBR denied");
1214 return -EINVAL;
1219 // get space for our vcc stuff
1220 vcc = kmalloc (sizeof(amb_vcc), GFP_KERNEL);
1221 if (!vcc) {
1222 PRINTK (KERN_ERR, "out of memory!");
1223 return -ENOMEM;
1225 atm_vcc->dev_data = (void *) vcc;
1227 // no failures beyond this point
1229 // we are not really "immediately before allocating the connection
1230 // identifier in hardware", but it will just have to do!
1231 atm_vcc->flags |= ATM_VF_ADDR;
1233 if (txtp->traffic_class != ATM_NONE) {
1234 command cmd;
1236 vcc->tx_frame_bits = tx_frame_bits;
1238 down (&dev->vcc_sf);
1239 if (dev->rxer[vci]) {
1240 // RXer on the channel already, just modify rate...
1241 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
1242 cmd.args.modify_rate.vc = cpu_to_be32 (vci); // vpi 0
1243 cmd.args.modify_rate.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
1244 while (command_do (dev, &cmd))
1245 schedule();
1246 // ... and TX flags, preserving the RX pool
1247 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
1248 cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
1249 cmd.args.modify_flags.flags = cpu_to_be32
1250 ( (AMB_VCC(dev->rxer[vci])->rx_info.pool << SRB_POOL_SHIFT)
1251 | (tx_vc_bits << SRB_FLAGS_SHIFT) );
1252 while (command_do (dev, &cmd))
1253 schedule();
1254 } else {
1255 // no RXer on the channel, just open (with pool zero)
1256 cmd.request = cpu_to_be32 (SRB_OPEN_VC);
1257 cmd.args.open.vc = cpu_to_be32 (vci); // vpi 0
1258 cmd.args.open.flags = cpu_to_be32 (tx_vc_bits << SRB_FLAGS_SHIFT);
1259 cmd.args.open.rate = cpu_to_be32 (tx_rate_bits << SRB_RATE_SHIFT);
1260 while (command_do (dev, &cmd))
1261 schedule();
1263 dev->txer[vci].tx_present = 1;
1264 up (&dev->vcc_sf);
1267 if (rxtp->traffic_class != ATM_NONE) {
1268 command cmd;
1270 vcc->rx_info.pool = pool;
1272 down (&dev->vcc_sf);
1273 /* grow RX buffer pool */
1274 if (!dev->rxq[pool].buffers_wanted)
1275 dev->rxq[pool].buffers_wanted = rx_lats;
1276 dev->rxq[pool].buffers_wanted += 1;
1277 fill_rx_pool (dev, pool, GFP_KERNEL);
1279 if (dev->txer[vci].tx_present) {
1280 // TXer on the channel already
1281 // switch (from pool zero) to this pool, preserving the TX bits
1282 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
1283 cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
1284 cmd.args.modify_flags.flags = cpu_to_be32
1285 ( (pool << SRB_POOL_SHIFT)
1286 | (dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT) );
1287 } else {
1288 // no TXer on the channel, open the VC (with no rate info)
1289 cmd.request = cpu_to_be32 (SRB_OPEN_VC);
1290 cmd.args.open.vc = cpu_to_be32 (vci); // vpi 0
1291 cmd.args.open.flags = cpu_to_be32 (pool << SRB_POOL_SHIFT);
1292 cmd.args.open.rate = cpu_to_be32 (0);
1294 while (command_do (dev, &cmd))
1295 schedule();
1296 // this link allows RX frames through
1297 dev->rxer[vci] = atm_vcc;
1298 up (&dev->vcc_sf);
1301 // set elements of vcc
1302 atm_vcc->vpi = vpi; // 0
1303 atm_vcc->vci = vci;
1305 // indicate readiness
1306 atm_vcc->flags |= ATM_VF_READY;
1308 MOD_INC_USE_COUNT;
1309 return 0;
1312 /********** Close a VC **********/
1314 static void amb_close (struct atm_vcc * atm_vcc) {
1315 amb_dev * dev = AMB_DEV (atm_vcc->dev);
1316 amb_vcc * vcc = AMB_VCC (atm_vcc);
1317 u16 vci = atm_vcc->vci;
1319 PRINTD (DBG_VCC|DBG_FLOW, "amb_close");
1321 // indicate unreadiness
1322 atm_vcc->flags &= ~ATM_VF_READY;
1324 // disable TXing
1325 if (atm_vcc->qos.txtp.traffic_class != ATM_NONE) {
1326 command cmd;
1328 down (&dev->vcc_sf);
1329 if (dev->rxer[vci]) {
1330 // RXer still on the channel, just modify rate... XXX not really needed
1331 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_RATE);
1332 cmd.args.modify_rate.vc = cpu_to_be32 (vci); // vpi 0
1333 cmd.args.modify_rate.rate = cpu_to_be32 (0);
1334 // ... and clear TX rate flags (XXX to stop RM cell output?), preserving RX pool
1335 } else {
1336 // no RXer on the channel, close channel
1337 cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
1338 cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
1340 dev->txer[vci].tx_present = 0;
1341 while (command_do (dev, &cmd))
1342 schedule();
1343 up (&dev->vcc_sf);
1346 // disable RXing
1347 if (atm_vcc->qos.rxtp.traffic_class != ATM_NONE) {
1348 command cmd;
1350 // this is (the?) one reason why we need the amb_vcc struct
1351 unsigned char pool = vcc->rx_info.pool;
1353 down (&dev->vcc_sf);
1354 if (dev->txer[vci].tx_present) {
1355 // TXer still on the channel, just go to pool zero XXX not really needed
1356 cmd.request = cpu_to_be32 (SRB_MODIFY_VC_FLAGS);
1357 cmd.args.modify_flags.vc = cpu_to_be32 (vci); // vpi 0
1358 cmd.args.modify_flags.flags = cpu_to_be32
1359 (dev->txer[vci].tx_vc_bits << SRB_FLAGS_SHIFT);
1360 } else {
1361 // no TXer on the channel, close the VC
1362 cmd.request = cpu_to_be32 (SRB_CLOSE_VC);
1363 cmd.args.close.vc = cpu_to_be32 (vci); // vpi 0
1365 // forget the rxer - no more skbs will be pushed
1366 if (atm_vcc != dev->rxer[vci])
1367 PRINTK (KERN_ERR, "%s vcc=%p rxer[vci]=%p",
1368 "arghhh! we're going to die!",
1369 vcc, dev->rxer[vci]);
1370 dev->rxer[vci] = 0;
1371 while (command_do (dev, &cmd))
1372 schedule();
1374 /* shrink RX buffer pool */
1375 dev->rxq[pool].buffers_wanted -= 1;
1376 if (dev->rxq[pool].buffers_wanted == rx_lats) {
1377 dev->rxq[pool].buffers_wanted = 0;
1378 drain_rx_pool (dev, pool);
1380 up (&dev->vcc_sf);
1383 // free our structure
1384 kfree (vcc);
1386 // say the VPI/VCI is free again
1387 atm_vcc->flags &= ~ATM_VF_ADDR;
1388 MOD_DEC_USE_COUNT;
1391 /********** Debug\x17Ioctl **********/
1393 #if 0
1394 static int amb_ioctl (struct atm_dev * dev, unsigned int cmd, void * arg) {
1395 unsigned short newdebug;
1396 if (cmd == AMB_SETDEBUG) {
1397 if (copy_from_user (&newdebug, arg, sizeof(newdebug))) {
1398 // moan
1399 return -EFAULT;
1400 } else {
1401 debug = newdebug;
1402 return 0;
1404 } else if (cmd == AMB_DONTPANIC) {
1405 dont_panic (dev);
1406 } else {
1407 // moan
1408 return -EINVAL;
1411 #endif
1413 /********** Set socket options for a VC **********/
1415 // int amb_getsockopt (struct atm_vcc * atm_vcc, int level, int optname, void * optval, int optlen);
1417 /********** Set socket options for a VC **********/
1419 // int amb_setsockopt (struct atm_vcc * atm_vcc, int level, int optname, void * optval, int optlen);
1421 /********** Send **********/
1423 static int amb_send (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
1424 amb_dev * dev = AMB_DEV(atm_vcc->dev);
1425 amb_vcc * vcc = AMB_VCC(atm_vcc);
1426 u16 vc = atm_vcc->vci;
1427 unsigned int tx_len = skb->len;
1428 unsigned char * tx_data = skb->data;
1429 tx_simple * tx_descr;
1430 tx_in tx;
1432 if (test_bit (dead, &dev->flags))
1433 return -EIO;
1435 PRINTD (DBG_FLOW|DBG_TX, "amb_send vc %x data %p len %u",
1436 vc, tx_data, tx_len);
1438 dump_skb (">>>", vc, skb);
1440 if (!dev->txer[vc].tx_present) {
1441 PRINTK (KERN_ERR, "attempt to send on RX-only VC %x", vc);
1442 return -EBADFD;
1445 // this is a driver private field so we have to set it ourselves,
1446 // despite the fact that we are _required_ to use it to check for a
1447 // pop function
1448 ATM_SKB(skb)->vcc = atm_vcc;
1450 if (skb->len > (size_t) atm_vcc->qos.txtp.max_sdu) {
1451 PRINTK (KERN_ERR, "sk_buff length greater than agreed max_sdu, dropping...");
1452 return -EIO;
1455 if (check_area (skb->data, skb->len)) {
1456 atm_vcc->stats->tx_err++;
1457 return -ENOMEM; // ?
1460 // allocate memory for fragments
1461 tx_descr = kmalloc (sizeof(tx_simple), GFP_KERNEL);
1462 if (!tx_descr) {
1463 PRINTK (KERN_ERR, "could not allocate TX descriptor");
1464 return -ENOMEM;
1466 if (check_area (tx_descr, sizeof(tx_simple))) {
1467 kfree (tx_descr);
1468 return -ENOMEM;
1470 PRINTD (DBG_TX, "fragment list allocated at %p", tx_descr);
1472 tx_descr->skb = skb;
1474 tx_descr->tx_frag.bytes = cpu_to_be32 (tx_len);
1475 tx_descr->tx_frag.address = cpu_to_be32 (virt_to_bus (tx_data));
1477 tx_descr->tx_frag_end.handle = virt_to_bus (tx_descr);
1478 tx_descr->tx_frag_end.vc = 0;
1479 tx_descr->tx_frag_end.next_descriptor_length = 0;
1480 tx_descr->tx_frag_end.next_descriptor = 0;
1481 #ifdef AMB_NEW_MICROCODE
1482 tx_descr->tx_frag_end.cpcs_uu = 0;
1483 tx_descr->tx_frag_end.cpi = 0;
1484 tx_descr->tx_frag_end.pad = 0;
1485 #endif
1487 tx.vc = cpu_to_be16 (vcc->tx_frame_bits | vc);
1488 tx.tx_descr_length = cpu_to_be16 (sizeof(tx_frag)+sizeof(tx_frag_end));
1489 tx.tx_descr_addr = cpu_to_be32 (virt_to_bus (&tx_descr->tx_frag));
1491 #ifdef DEBUG_AMBASSADOR
1492 /* wey-hey! */
1493 if (vc == 1023) {
1494 unsigned int i;
1495 unsigned short d = 0;
1496 char * s = skb->data;
1497 switch (*s++) {
1498 case 'D': {
1499 for (i = 0; i < 4; ++i) {
1500 d = (d<<4) | ((*s <= '9') ? (*s - '0') : (*s - 'a' + 10));
1501 ++s;
1503 PRINTK (KERN_INFO, "debug bitmap is now %hx", debug = d);
1504 break;
1506 case 'R': {
1507 if (*s++ == 'e' && *s++ == 's' && *s++ == 'e' && *s++ == 't')
1508 dont_panic (dev);
1509 break;
1511 default: {
1512 break;
1516 #endif
1518 while (tx_give (dev, &tx))
1519 schedule();
1520 return 0;
1523 /********** Scatter Gather Send Capability **********/
1525 static int amb_sg_send (struct atm_vcc * atm_vcc,
1526 unsigned long start,
1527 unsigned long size) {
1528 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: never");
1529 return 0;
1530 if (atm_vcc->qos.aal == ATM_AAL5) {
1531 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: yes");
1532 return 1;
1533 } else {
1534 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: no");
1535 return 0;
1537 PRINTD (DBG_FLOW|DBG_VCC, "amb_sg_send: always");
1538 return 1;
1541 /********** Send OAM **********/
1543 // static int amb_send_oam (struct atm_vcc * atm_vcc, void * cell, int flags);
1545 /********** Feedback to Driver **********/
1547 // void amb_feedback (struct atm_vcc * atm_vcc, struct sk_buff * skb,
1548 // unsigned long start, unsigned long dest, int len);
1550 /********** Change QoS on a VC **********/
1552 // int amb_change_qos (struct atm_vcc * atm_vcc, struct atm_qos * qos, int flags);
1554 /********** Free RX Socket Buffer **********/
1556 #if 0
1557 static void amb_free_rx_skb (struct atm_vcc * atm_vcc, struct sk_buff * skb) {
1558 amb_dev * dev = AMB_DEV (atm_vcc->dev);
1559 amb_vcc * vcc = AMB_VCC (atm_vcc);
1560 unsigned char pool = vcc->rx_info.pool;
1561 rx_in rx;
1563 // This may be unsafe for various reasons that I cannot really guess
1564 // at. However, I note that the ATM layer calls kfree_skb rather
1565 // than dev_kfree_skb at this point so we are least covered as far
1566 // as buffer locking goes. There may be bugs if pcap clones RX skbs.
1568 PRINTD (DBG_FLOW|DBG_SKB, "amb_rx_free skb %p (atm_vcc %p, vcc %p)",
1569 skb, atm_vcc, vcc);
1571 rx.handle = virt_to_bus (skb);
1572 rx.host_address = cpu_to_be32 (virt_to_bus (skb->data));
1574 skb->data = skb->head;
1575 skb->tail = skb->head;
1576 skb->len = 0;
1578 if (!rx_give (dev, &rx, pool)) {
1579 // success
1580 PRINTD (DBG_SKB|DBG_POOL, "recycled skb for pool %hu", pool);
1581 return;
1584 // just do what the ATM layer would have done
1585 kfree_skb (skb);
1587 return;
1589 #endif
1591 /********** Proc File Output **********/
1593 static int amb_proc_read (struct atm_dev * atm_dev, loff_t * pos, char * page) {
1594 amb_dev * dev = AMB_DEV (atm_dev);
1595 int left = *pos;
1596 unsigned char pool;
1598 PRINTD (DBG_FLOW, "amb_proc_read");
1600 /* more diagnostics here? */
1602 if (!left--) {
1603 amb_stats * s = &dev->stats;
1604 return sprintf (page,
1605 "frames: TX OK %lu, RX OK %lu, RX bad %lu "
1606 "(CRC %lu, long %lu, aborted %lu, unused %lu).\n",
1607 s->tx_ok, s->rx.ok, s->rx.error,
1608 s->rx.badcrc, s->rx.toolong,
1609 s->rx.aborted, s->rx.unused);
1612 if (!left--) {
1613 amb_cq * c = &dev->cq;
1614 return sprintf (page, "cmd queue [cur/hi/max]: %u/%u/%u. ",
1615 c->pending, c->high, c->maximum);
1618 if (!left--) {
1619 amb_txq * t = &dev->txq;
1620 return sprintf (page, "TX queue [cur/max high full]: %u/%u %u %u.\n",
1621 t->pending, t->maximum, t->high, t->filled);
1624 if (!left--) {
1625 unsigned int count = sprintf (page, "RX queues [cur/max/req low empty]:");
1626 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1627 amb_rxq * r = &dev->rxq[pool];
1628 count += sprintf (page+count, " %u/%u/%u %u %u",
1629 r->pending, r->maximum, r->buffers_wanted, r->low, r->emptied);
1631 count += sprintf (page+count, ".\n");
1632 return count;
1635 if (!left--) {
1636 unsigned int count = sprintf (page, "RX buffer sizes:");
1637 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1638 amb_rxq * r = &dev->rxq[pool];
1639 count += sprintf (page+count, " %u", r->buffer_size);
1641 count += sprintf (page+count, ".\n");
1642 return count;
1645 #if 0
1646 if (!left--) {
1647 // suni block etc?
1649 #endif
1651 return 0;
1654 /********** Operation Structure **********/
1656 static const struct atmdev_ops amb_ops = {
1657 NULL, // no amb_dev_close
1658 amb_open,
1659 amb_close,
1660 NULL, // no amb_ioctl,
1661 NULL, // no amb_getsockopt,
1662 NULL, // no amb_setsockopt,
1663 amb_send,
1664 amb_sg_send,
1665 NULL, // no send_oam - not in fact used yet
1666 NULL, // no amb_phy_put - not needed in this driver
1667 NULL, // no amb_phy_get - not needed in this driver
1668 NULL, // no feedback - feedback to the driver!
1669 NULL, // no amb_change_qos
1670 NULL, // amb_free_rx_skb not used until checked by someone else
1671 amb_proc_read
1674 /********** housekeeping **********/
1676 static inline void set_timer (struct timer_list * timer, unsigned long delay) {
1677 timer->expires = jiffies + delay;
1678 add_timer (timer);
1679 return;
1682 static void do_housekeeping (unsigned long arg) {
1683 amb_dev * dev = amb_devs;
1684 // data is set to zero at module unload
1685 (void) arg;
1687 if (housekeeping.data) {
1688 while (dev) {
1690 // could collect device-specific (not driver/atm-linux) stats here
1692 // last resort refill once every ten seconds
1693 fill_rx_pools (dev);
1695 dev = dev->prev;
1697 set_timer (&housekeeping, 10*HZ);
1700 return;
1703 /********** creation of communication queues **********/
1705 static int __init create_queues (amb_dev * dev, unsigned int cmds,
1706 unsigned int txs, unsigned int * rxs,
1707 unsigned int * rx_buffer_sizes) {
1708 unsigned char pool;
1709 size_t total = 0;
1710 void * memory;
1711 void * limit;
1713 PRINTD (DBG_FLOW, "create_queues %p", dev);
1715 total += cmds * sizeof(command);
1717 total += txs * (sizeof(tx_in) + sizeof(tx_out));
1719 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
1720 total += rxs[pool] * (sizeof(rx_in) + sizeof(rx_out));
1722 memory = kmalloc (total, GFP_KERNEL);
1723 if (!memory) {
1724 PRINTK (KERN_ERR, "could not allocate queues");
1725 return -ENOMEM;
1727 if (check_area (memory, total)) {
1728 PRINTK (KERN_ERR, "queues allocated in nasty area");
1729 kfree (memory);
1730 return -ENOMEM;
1733 limit = memory + total;
1734 PRINTD (DBG_INIT, "queues from %p to %p", memory, limit);
1736 PRINTD (DBG_CMD, "command queue at %p", memory);
1739 command * cmd = memory;
1740 amb_cq * cq = &dev->cq;
1742 cq->pending = 0;
1743 cq->high = 0;
1744 cq->maximum = cmds - 1;
1746 cq->start = cmd;
1747 cq->in = cmd;
1748 cq->out = cmd;
1749 cq->limit = cmd + cmds;
1751 memory = cq->limit;
1754 PRINTD (DBG_TX, "TX queue pair at %p", memory);
1757 tx_in * in = memory;
1758 tx_out * out;
1759 amb_txq * txq = &dev->txq;
1761 txq->pending = 0;
1762 txq->high = 0;
1763 txq->filled = 0;
1764 txq->maximum = txs - 1;
1766 txq->in.start = in;
1767 txq->in.ptr = in;
1768 txq->in.limit = in + txs;
1770 memory = txq->in.limit;
1771 out = memory;
1773 txq->out.start = out;
1774 txq->out.ptr = out;
1775 txq->out.limit = out + txs;
1777 memory = txq->out.limit;
1780 PRINTD (DBG_RX, "RX queue pairs at %p", memory);
1782 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
1783 rx_in * in = memory;
1784 rx_out * out;
1785 amb_rxq * rxq = &dev->rxq[pool];
1787 rxq->buffer_size = rx_buffer_sizes[pool];
1788 rxq->buffers_wanted = 0;
1790 rxq->pending = 0;
1791 rxq->low = rxs[pool] - 1;
1792 rxq->emptied = 0;
1793 rxq->maximum = rxs[pool] - 1;
1795 rxq->in.start = in;
1796 rxq->in.ptr = in;
1797 rxq->in.limit = in + rxs[pool];
1799 memory = rxq->in.limit;
1800 out = memory;
1802 rxq->out.start = out;
1803 rxq->out.ptr = out;
1804 rxq->out.limit = out + rxs[pool];
1806 memory = rxq->out.limit;
1809 if (memory == limit) {
1810 return 0;
1811 } else {
1812 PRINTK (KERN_ERR, "bad queue alloc %p != %p (tell maintainer)", memory, limit);
1813 kfree (limit - total);
1814 return -ENOMEM;
1819 /********** destruction of communication queues **********/
1821 static void destroy_queues (amb_dev * dev) {
1822 // all queues assumed empty
1823 void * memory = dev->cq.start;
1824 // includes txq.in, txq.out, rxq[].in and rxq[].out
1826 PRINTD (DBG_FLOW, "destroy_queues %p", dev);
1828 PRINTD (DBG_INIT, "freeing queues at %p", memory);
1829 kfree (memory);
1831 return;
1834 /********** basic loader commands and error handling **********/
1836 static int __init do_loader_command (const amb_dev * dev, loader_command cmd,
1837 volatile loader_block * lb) {
1839 // centisecond timeouts - guessing away here
1840 unsigned int command_timeouts [] = {
1841 [host_memory_test] = 15,
1842 [read_adapter_memory] = 2,
1843 [write_adapter_memory] = 2,
1844 [adapter_start] = 50,
1845 [get_version_number] = 10,
1846 [interrupt_host] = 1,
1847 [flash_erase_sector] = 1,
1848 [adap_download_block] = 1,
1849 [adap_erase_flash] = 1,
1850 [adap_run_in_iram] = 1,
1851 [adap_end_download] = 1
1854 unsigned int command_successes [] = {
1855 [host_memory_test] = COMMAND_PASSED_TEST,
1856 [read_adapter_memory] = COMMAND_READ_DATA_OK,
1857 [write_adapter_memory] = COMMAND_WRITE_DATA_OK,
1858 [adapter_start] = COMMAND_COMPLETE,
1859 [get_version_number] = COMMAND_COMPLETE,
1860 [interrupt_host] = COMMAND_COMPLETE,
1861 [flash_erase_sector] = COMMAND_COMPLETE,
1862 [adap_download_block] = COMMAND_COMPLETE,
1863 [adap_erase_flash] = COMMAND_COMPLETE,
1864 [adap_run_in_iram] = COMMAND_COMPLETE,
1865 [adap_end_download] = COMMAND_COMPLETE
1868 int decode_loader_result (loader_command cmd, u32 result) {
1869 int res;
1870 const char * msg;
1872 if (result == command_successes[cmd])
1873 return 0;
1875 switch (result) {
1876 case BAD_COMMAND:
1877 res = -EINVAL;
1878 msg = "bad command";
1879 break;
1880 case COMMAND_IN_PROGRESS:
1881 res = -ETIMEDOUT;
1882 msg = "command in progress";
1883 break;
1884 case COMMAND_PASSED_TEST:
1885 res = 0;
1886 msg = "command passed test";
1887 break;
1888 case COMMAND_FAILED_TEST:
1889 res = -EIO;
1890 msg = "command failed test";
1891 break;
1892 case COMMAND_READ_DATA_OK:
1893 res = 0;
1894 msg = "command read data ok";
1895 break;
1896 case COMMAND_READ_BAD_ADDRESS:
1897 res = -EINVAL;
1898 msg = "command read bad address";
1899 break;
1900 case COMMAND_WRITE_DATA_OK:
1901 res = 0;
1902 msg = "command write data ok";
1903 break;
1904 case COMMAND_WRITE_BAD_ADDRESS:
1905 res = -EINVAL;
1906 msg = "command write bad address";
1907 break;
1908 case COMMAND_WRITE_FLASH_FAILURE:
1909 res = -EIO;
1910 msg = "command write flash failure";
1911 break;
1912 case COMMAND_COMPLETE:
1913 res = 0;
1914 msg = "command complete";
1915 break;
1916 case COMMAND_FLASH_ERASE_FAILURE:
1917 res = -EIO;
1918 msg = "command flash erase failure";
1919 break;
1920 case COMMAND_WRITE_BAD_DATA:
1921 res = -EINVAL;
1922 msg = "command write bad data";
1923 break;
1924 default:
1925 res = -EINVAL;
1926 msg = "unknown error";
1927 PRINTD (DBG_LOAD|DBG_ERR, "decode_loader_result got %d=%x !",
1928 result, result);
1929 break;
1932 PRINTK (KERN_ERR, "%s", msg);
1933 return res;
1936 unsigned long timeout;
1938 PRINTD (DBG_FLOW|DBG_LOAD, "do_loader_command");
1940 /* do a command
1942 Set the return value to zero, set the command type and set the
1943 valid entry to the right magic value. The payload is already
1944 correctly byte-ordered so we leave it alone. Hit the doorbell
1945 with the bus address of this structure.
1949 lb->result = 0;
1950 lb->command = cpu_to_be32 (cmd);
1951 lb->valid = cpu_to_be32 (DMA_VALID);
1952 // dump_loader_block (lb);
1953 wr_mem (dev, &mem->doorbell, virt_to_bus (lb));
1955 timeout = command_timeouts[cmd] * HZ/100;
1957 while (!lb->result || lb->result == cpu_to_be32 (COMMAND_IN_PROGRESS))
1958 if (timeout) {
1959 timeout = schedule_timeout (timeout);
1960 } else {
1961 PRINTD (DBG_LOAD|DBG_ERR, "command %d timed out", cmd);
1962 dump_registers (dev);
1963 dump_loader_block (lb);
1964 return -ETIMEDOUT;
1967 if (cmd == adapter_start) {
1968 // wait for start command to acknowledge...
1969 timeout = HZ/10;
1970 while (rd_mem (dev, &mem->doorbell))
1971 if (timeout) {
1972 timeout = schedule_timeout (timeout);
1973 } else {
1974 PRINTD (DBG_LOAD|DBG_ERR, "start command did not clear doorbell, res=%08x",
1975 be32_to_cpu (lb->result));
1976 dump_registers (dev);
1977 return -ETIMEDOUT;
1979 return 0;
1980 } else {
1981 return decode_loader_result (cmd, be32_to_cpu (lb->result));
1986 /* loader: determine loader version */
1988 static int __init get_loader_version (const amb_dev * dev, u32 * version) {
1989 loader_block lb;
1990 int res;
1992 PRINTD (DBG_FLOW|DBG_LOAD, "get_loader_version");
1994 res = do_loader_command (dev, get_version_number, &lb);
1995 if (res)
1996 return res;
1997 if (version)
1998 *version = be32_to_cpu (lb.payload.version);
1999 return 0;
2002 /* loader: read or verify memory data blocks */
2004 static int __init loader_write (const amb_dev * dev, const u32 * data,
2005 u32 address, unsigned int count) {
2006 unsigned int i;
2007 loader_block lb;
2008 transfer_block * tb = &lb.payload.transfer;
2010 PRINTD (DBG_FLOW|DBG_LOAD, "loader_write");
2012 if (count > MAX_TRANSFER_DATA)
2013 return -EINVAL;
2014 tb->address = cpu_to_be32 (address);
2015 tb->count = cpu_to_be32 (count);
2016 for (i = 0; i < count; ++i)
2017 tb->data[i] = cpu_to_be32 (data[i]);
2018 return do_loader_command (dev, write_adapter_memory, &lb);
2021 static int __init loader_verify (const amb_dev * dev, const u32 * data,
2022 u32 address, unsigned int count) {
2023 unsigned int i;
2024 loader_block lb;
2025 transfer_block * tb = &lb.payload.transfer;
2026 int res;
2028 PRINTD (DBG_FLOW|DBG_LOAD, "loader_verify");
2030 if (count > MAX_TRANSFER_DATA)
2031 return -EINVAL;
2032 tb->address = cpu_to_be32 (address);
2033 tb->count = cpu_to_be32 (count);
2034 res = do_loader_command (dev, read_adapter_memory, &lb);
2035 if (!res)
2036 for (i = 0; i < count; ++i)
2037 if (tb->data[i] != cpu_to_be32 (data[i])) {
2038 res = -EINVAL;
2039 break;
2041 return res;
2044 static int __init loader_start (const amb_dev * dev, u32 address) {
2045 loader_block lb;
2047 PRINTD (DBG_FLOW|DBG_LOAD, "loader_start");
2049 lb.payload.start = cpu_to_be32 (address);
2050 return do_loader_command (dev, adapter_start, &lb);
2053 /********** reset card **********/
2055 static int amb_reset (amb_dev * dev, int diags) {
2056 u32 word;
2058 PRINTD (DBG_FLOW|DBG_LOAD, "amb_reset");
2060 word = rd_mem (dev, &mem->reset_control);
2061 #if 0
2062 // clear all interrupts just in case
2063 wr_mem (dev, &mem->interrupt, -1);
2064 #endif
2065 // put card into reset state
2066 wr_mem (dev, &mem->reset_control, word | AMB_RESET);
2067 // wait a short while
2068 udelay (10);
2069 // clear self-test done flag
2070 wr_mem (dev, &mem->mb.loader.ready, 0);
2071 // take card out of reset state
2072 wr_mem (dev, &mem->reset_control, word &~ AMB_RESET);
2074 if (diags) {
2075 unsigned long timeout;
2076 // 4.2 second wait
2077 timeout = HZ*42/10;
2078 while (timeout)
2079 timeout = schedule_timeout (timeout);
2080 // half second time-out
2081 timeout = HZ/2;
2082 while (!rd_mem (dev, &mem->mb.loader.ready))
2083 if (timeout) {
2084 timeout = schedule_timeout (timeout);
2085 } else {
2086 PRINTD (DBG_LOAD|DBG_ERR, "reset timed out");
2087 return -ETIMEDOUT;
2090 // get results of self-test
2091 word = rd_mem (dev, &mem->mb.loader.result);
2092 if (word & SELF_TEST_FAILURE) {
2093 void sf (const char * msg) {
2094 PRINTK (KERN_ERR, "self-test failed: %s", msg);
2096 if (word & GPINT_TST_FAILURE)
2097 sf ("interrupt");
2098 if (word & SUNI_DATA_PATTERN_FAILURE)
2099 sf ("SUNI data pattern");
2100 if (word & SUNI_DATA_BITS_FAILURE)
2101 sf ("SUNI data bits");
2102 if (word & SUNI_UTOPIA_FAILURE)
2103 sf ("SUNI UTOPIA interface");
2104 if (word & SUNI_FIFO_FAILURE)
2105 sf ("SUNI cell buffer FIFO");
2106 if (word & SRAM_FAILURE)
2107 sf ("bad SRAM");
2108 // better return value?
2109 return -EIO;
2113 return 0;
2116 /********** transfer and start the microcode **********/
2118 static int __init ucode_init (amb_dev * dev) {
2119 unsigned int i = 0;
2120 unsigned int total = 0;
2121 const u32 * pointer = ucode_data;
2122 u32 address;
2123 unsigned int count;
2124 int res;
2126 PRINTD (DBG_FLOW|DBG_LOAD, "ucode_init");
2128 while (address = ucode_regions[i].start,
2129 count = ucode_regions[i].count) {
2130 PRINTD (DBG_LOAD, "starting region (%x, %u)", address, count);
2131 while (count) {
2132 unsigned int words;
2133 if (count <= MAX_TRANSFER_DATA)
2134 words = count;
2135 else
2136 words = MAX_TRANSFER_DATA;
2137 total += words;
2138 res = loader_write (dev, pointer, address, words);
2139 if (res)
2140 return res;
2141 res = loader_verify (dev, pointer, address, words);
2142 if (res)
2143 return res;
2144 count -= words;
2145 address += sizeof(u32) * words;
2146 pointer += words;
2148 i += 1;
2150 if (*pointer == 0xdeadbeef) {
2151 return loader_start (dev, ucode_start);
2152 } else {
2153 // cast needed as there is no %? for pointer differnces
2154 PRINTD (DBG_LOAD|DBG_ERR,
2155 "offset=%li, *pointer=%x, address=%x, total=%u",
2156 (long) (pointer - ucode_data), *pointer, address, total);
2157 PRINTK (KERN_ERR, "incorrect microcode data");
2158 return -ENOMEM;
2162 /********** give adapter parameters **********/
2164 static int __init amb_talk (amb_dev * dev) {
2165 adap_talk_block a;
2166 unsigned char pool;
2167 unsigned long timeout;
2169 static inline u32 x (void * addr) {
2170 return cpu_to_be32 (virt_to_bus (addr));
2173 PRINTD (DBG_FLOW, "amb_talk %p", dev);
2175 a.command_start = x (dev->cq.start);
2176 a.command_end = x (dev->cq.limit);
2177 a.tx_start = x (dev->txq.in.start);
2178 a.tx_end = x (dev->txq.in.limit);
2179 a.txcom_start = x (dev->txq.out.start);
2180 a.txcom_end = x (dev->txq.out.limit);
2182 for (pool = 0; pool < NUM_RX_POOLS; ++pool) {
2183 // the other "a" items are set up by the adapter
2184 a.rec_struct[pool].buffer_start = x (dev->rxq[pool].in.start);
2185 a.rec_struct[pool].buffer_end = x (dev->rxq[pool].in.limit);
2186 a.rec_struct[pool].rx_start = x (dev->rxq[pool].out.start);
2187 a.rec_struct[pool].rx_end = x (dev->rxq[pool].out.limit);
2188 a.rec_struct[pool].buffer_size = cpu_to_be32 (dev->rxq[pool].buffer_size);
2191 #ifdef AMB_NEW_MICROCODE
2192 // disable fast PLX prefetching
2193 a.init_flags = 0;
2194 #endif
2196 // pass the structure
2197 wr_mem (dev, &mem->doorbell, virt_to_bus (&a));
2199 // 2.2 second wait (must not touch doorbell during 2 second DMA test)
2200 timeout = HZ*22/10;
2201 while (timeout)
2202 timeout = schedule_timeout (timeout);
2203 // give the adapter another half second?
2204 timeout = HZ/2;
2205 while (rd_mem (dev, &mem->doorbell))
2206 if (timeout) {
2207 timeout = schedule_timeout (timeout);
2208 } else {
2209 PRINTD (DBG_INIT|DBG_ERR, "adapter init timed out");
2210 return -ETIMEDOUT;
2213 return 0;
2216 // get microcode version
2217 static void __init amb_ucode_version (amb_dev * dev) {
2218 u32 major;
2219 u32 minor;
2220 command cmd;
2221 cmd.request = cpu_to_be32 (SRB_GET_VERSION);
2222 while (command_do (dev, &cmd))
2223 schedule();
2224 major = be32_to_cpu (cmd.args.version.major);
2225 minor = be32_to_cpu (cmd.args.version.minor);
2226 PRINTK (KERN_INFO, "microcode version is %u.%u", major, minor);
2229 // get end station address
2230 static void __init amb_esi (amb_dev * dev, u8 * esi) {
2231 u32 lower4;
2232 u16 upper2;
2233 command cmd;
2235 // swap bits within byte to get Ethernet ordering
2236 u8 bit_swap (u8 byte) {
2237 const u8 swap[] = {
2238 0x0, 0x8, 0x4, 0xc,
2239 0x2, 0xa, 0x6, 0xe,
2240 0x1, 0x9, 0x5, 0xd,
2241 0x3, 0xb, 0x7, 0xf
2243 return ((swap[byte & 0xf]<<4) | swap[byte>>4]);
2246 cmd.request = cpu_to_be32 (SRB_GET_BIA);
2247 while (command_do (dev, &cmd))
2248 schedule();
2249 lower4 = be32_to_cpu (cmd.args.bia.lower4);
2250 upper2 = be32_to_cpu (cmd.args.bia.upper2);
2251 PRINTD (DBG_LOAD, "BIA: lower4: %08x, upper2 %04x", lower4, upper2);
2253 if (esi) {
2254 unsigned int i;
2256 PRINTDB (DBG_INIT, "ESI:");
2257 for (i = 0; i < ESI_LEN; ++i) {
2258 if (i < 4)
2259 esi[i] = bit_swap (lower4>>(8*i));
2260 else
2261 esi[i] = bit_swap (upper2>>(8*(i-4)));
2262 PRINTDM (DBG_INIT, " %02x", esi[i]);
2265 PRINTDE (DBG_INIT, "");
2268 return;
2271 static int __init amb_init (amb_dev * dev) {
2272 u32 version;
2274 /* enable adapter doorbell */
2275 wr_mem (dev, &mem->interrupt_control,
2276 rd_mem (dev, &mem->interrupt_control)
2277 | AMB_DOORBELL_BITS);
2279 if (amb_reset (dev, 1)) {
2280 PRINTK (KERN_ERR, "card reset failed!");
2281 } else if (get_loader_version (dev, &version)) {
2282 PRINTK (KERN_INFO, "failed to get loader version");
2283 } else {
2284 PRINTK (KERN_INFO, "loader version is %08x", version);
2286 if (ucode_init (dev)) {
2287 PRINTK (KERN_ERR, "microcode failure");
2288 } else if (create_queues (dev, cmds, txs, rxs, rxs_bs)) {
2289 PRINTK (KERN_ERR, "failed to get memory for queues");
2290 } else {
2292 if (amb_talk (dev)) {
2293 PRINTK (KERN_ERR, "adapter did not accept queues");
2294 } else {
2296 amb_ucode_version (dev);
2297 return 0;
2298 } /* amb_talk */
2300 destroy_queues (dev);
2301 } /* create_queues, ucode_init */
2303 } /* get_loader_version, amb_reset */
2305 return -1;
2308 static int __init amb_probe (void) {
2309 struct pci_dev * pci_dev;
2310 int devs;
2312 void do_pci_device (void) {
2313 amb_dev * dev;
2315 // read resources from PCI configuration space
2316 u32 * membase = bus_to_virt
2317 (pci_dev->resource[0].start);
2318 u32 iobase = pci_dev->resource[1].start;
2319 u8 irq = pci_dev->irq;
2321 // check IO region
2322 if (check_region (iobase, AMB_EXTENT)) {
2323 PRINTK (KERN_ERR, "IO range already in use!");
2324 return;
2327 dev = kmalloc (sizeof(amb_dev), GFP_KERNEL);
2328 if (!dev) {
2329 // perhaps we should be nice: deregister all adapters and abort?
2330 PRINTK (KERN_ERR, "out of memory!");
2331 return;
2333 memset (dev, 0, sizeof(amb_dev));
2335 // set up known dev items straight away
2336 dev->pci_dev = pci_dev;
2338 dev->iobase = iobase;
2339 dev->irq = irq;
2340 dev->membase = membase;
2342 // flags (currently only dead)
2343 dev->flags = 0;
2345 // Allocate cell rates (fibre)
2346 // ATM_OC3_PCR = 1555200000/8/270*260/53 - 29/53
2347 // to be really pedantic, this should be ATM_OC3c_PCR
2348 dev->tx_avail = ATM_OC3_PCR;
2349 dev->rx_avail = ATM_OC3_PCR;
2351 #if 0
2352 // initialise bottom half
2353 dev->bh.next = 0;
2354 dev->bh.sync = 0;
2355 dev->bh.routine = (void (*)(void *)) fill_rx_pools;
2356 dev->bh.data = dev;
2357 #endif
2359 // semaphore for txer/rxer modifications - we cannot use a
2360 // spinlock as the critical region needs to switch processes
2361 init_MUTEX (&dev->vcc_sf);
2362 // queue manipulation spinlocks; we want atomic reads and
2363 // writes to the queue descriptors (handles IRQ and SMP)
2364 // consider replacing "int pending" -> "atomic_t available"
2365 // => problem related to who gets to move queue pointers
2366 spin_lock_init (&dev->cq.lock);
2367 spin_lock_init (&dev->txq.lock);
2369 unsigned char pool;
2370 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2371 spin_lock_init (&dev->rxq[pool].lock);
2374 // grab (but share) IRQ and install handler
2375 if (request_irq (irq, interrupt_handler, SA_SHIRQ, DEV_LABEL, dev)) {
2376 PRINTK (KERN_ERR, "request IRQ failed!");
2377 // free_irq is at "endif"
2378 } else {
2380 unsigned char lat;
2382 // reserve IO region
2383 request_region (iobase, AMB_EXTENT, DEV_LABEL);
2385 PRINTD (DBG_INFO, "found Madge ATM adapter (amb) at IO %x, IRQ %u, MEM %p",
2386 iobase, irq, membase);
2388 // enable bus master accesses
2389 pci_set_master (pci_dev);
2391 // frobnicate latency (upwards, usually)
2392 pci_read_config_byte (pci_dev, PCI_LATENCY_TIMER, &lat);
2393 if (pci_lat) {
2394 PRINTD (DBG_INIT, "%s PCI latency timer from %hu to %hu",
2395 "changing", lat, pci_lat);
2396 pci_write_config_byte (pci_dev, PCI_LATENCY_TIMER, pci_lat);
2397 } else if (lat < MIN_PCI_LATENCY) {
2398 PRINTK (KERN_INFO, "%s PCI latency timer from %hu to %hu",
2399 "increasing", lat, MIN_PCI_LATENCY);
2400 pci_write_config_byte (pci_dev, PCI_LATENCY_TIMER, MIN_PCI_LATENCY);
2403 if (amb_init (dev)) {
2404 PRINTK (KERN_ERR, "adapter initialisation failure");
2405 } else if (!(dev->atm_dev = atm_dev_register (DEV_LABEL, &amb_ops, -1, 0))) {
2406 PRINTD (DBG_ERR, "failed to register Madge ATM adapter");
2407 } else {
2409 PRINTD (DBG_INFO, "registered Madge ATM adapter (no. %d) (%p) at %p",
2410 dev->atm_dev->number, dev, dev->atm_dev);
2411 dev->atm_dev->dev_data = (void *) dev;
2413 // register our address
2414 amb_esi (dev, dev->atm_dev->esi);
2416 // 0 bits for vpi, 10 bits for vci
2417 dev->atm_dev->ci_range.vpi_bits = NUM_VPI_BITS;
2418 dev->atm_dev->ci_range.vci_bits = NUM_VCI_BITS;
2420 fill_rx_pools (dev);
2422 /* enable host interrupts */
2423 interrupts_on (dev);
2425 // update count and linked list
2426 ++devs;
2427 dev->prev = amb_devs;
2428 amb_devs = dev;
2429 // success
2430 return;
2432 // not currently reached
2433 atm_dev_deregister (dev->atm_dev);
2434 } /* atm_dev_register, amb_init */
2436 release_region (iobase, AMB_EXTENT);
2437 free_irq (irq, dev);
2438 } /* request_region, request_irq */
2440 kfree (dev);
2441 } /* kmalloc, end-of-fn */
2443 PRINTD (DBG_FLOW, "amb_probe");
2445 if (!pci_present())
2446 return 0;
2448 devs = 0;
2449 pci_dev = NULL;
2450 while ((pci_dev = pci_find_device
2451 (PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR, pci_dev)
2453 do_pci_device();
2455 pci_dev = NULL;
2456 while ((pci_dev = pci_find_device
2457 (PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD, pci_dev)
2459 PRINTK (KERN_ERR, "skipped broken (PLX rev 2) card");
2461 return devs;
2464 static void __init amb_check_args (void) {
2465 unsigned char pool;
2466 unsigned int max_rx_size;
2468 #ifdef DEBUG_AMBASSADOR
2469 PRINTK (KERN_NOTICE, "debug bitmap is %hx", debug &= DBG_MASK);
2470 #else
2471 if (debug)
2472 PRINTK (KERN_NOTICE, "no debugging support");
2473 #endif
2475 if (cmds < MIN_QUEUE_SIZE)
2476 PRINTK (KERN_NOTICE, "cmds has been raised to %u",
2477 cmds = MIN_QUEUE_SIZE);
2479 if (txs < MIN_QUEUE_SIZE)
2480 PRINTK (KERN_NOTICE, "txs has been raised to %u",
2481 txs = MIN_QUEUE_SIZE);
2483 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2484 if (rxs[pool] < MIN_QUEUE_SIZE)
2485 PRINTK (KERN_NOTICE, "rxs[%hu] has been raised to %u",
2486 pool, rxs[pool] = MIN_QUEUE_SIZE);
2488 // buffers sizes should be greater than zero and strictly increasing
2489 max_rx_size = 0;
2490 for (pool = 0; pool < NUM_RX_POOLS; ++pool)
2491 if (rxs_bs[pool] <= max_rx_size)
2492 PRINTK (KERN_NOTICE, "useless pool (rxs_bs[%hu] = %u)",
2493 pool, rxs_bs[pool]);
2494 else
2495 max_rx_size = rxs_bs[pool];
2497 if (rx_lats < MIN_RX_BUFFERS)
2498 PRINTK (KERN_NOTICE, "rx_lats has been raised to %u",
2499 rx_lats = MIN_RX_BUFFERS);
2501 return;
2504 /********** module stuff **********/
2506 #ifdef MODULE
2507 EXPORT_NO_SYMBOLS;
2509 MODULE_AUTHOR(maintainer_string);
2510 MODULE_DESCRIPTION(description_string);
2511 MODULE_PARM(debug, "h");
2512 MODULE_PARM(cmds, "i");
2513 MODULE_PARM(txs, "i");
2514 MODULE_PARM(rxs, __MODULE_STRING(NUM_RX_POOLS) "i");
2515 MODULE_PARM(rxs_bs, __MODULE_STRING(NUM_RX_POOLS) "i");
2516 MODULE_PARM(rx_lats, "i");
2517 MODULE_PARM(pci_lat, "b");
2518 MODULE_PARM_DESC(debug, "debug bitmap, see .h file");
2519 MODULE_PARM_DESC(cmds, "number of command queue entries");
2520 MODULE_PARM_DESC(txs, "number of TX queue entries");
2521 MODULE_PARM_DESC(rxs, "number of RX queue entries [" __MODULE_STRING(NUM_RX_POOLS) "]");
2522 MODULE_PARM_DESC(rxs_bs, "size of RX buffers [" __MODULE_STRING(NUM_RX_POOLS) "]");
2523 MODULE_PARM_DESC(rx_lats, "number of extra buffers to cope with RX latencies");
2524 MODULE_PARM_DESC(pci_lat, "PCI latency in bus cycles");
2526 /********** module entry **********/
2528 int init_module (void) {
2529 int devs;
2531 PRINTD (DBG_FLOW|DBG_INIT, "init_module");
2533 // sanity check - cast needed as printk does not support %Zu
2534 if (sizeof(amb_mem) != 4*16 + 4*12) {
2535 PRINTK (KERN_ERR, "Fix amb_mem (is %lu words).",
2536 (unsigned long) sizeof(amb_mem));
2537 return -ENOMEM;
2540 show_version();
2542 // check arguments
2543 amb_check_args();
2545 // get the juice
2546 devs = amb_probe();
2548 if (devs) {
2549 init_timer (&housekeeping);
2550 housekeeping.function = do_housekeeping;
2551 // paranoia
2552 housekeeping.data = 1;
2553 set_timer (&housekeeping, 0);
2554 } else {
2555 PRINTK (KERN_INFO, "no (usable) adapters found");
2558 return devs ? 0 : -ENODEV;
2561 /********** module exit **********/
2563 void cleanup_module (void) {
2564 amb_dev * dev;
2566 PRINTD (DBG_FLOW|DBG_INIT, "cleanup_module");
2568 // paranoia
2569 housekeeping.data = 0;
2570 del_timer (&housekeeping);
2572 while (amb_devs) {
2573 dev = amb_devs;
2574 amb_devs = dev->prev;
2576 PRINTD (DBG_INFO|DBG_INIT, "closing %p (atm_dev = %p)", dev, dev->atm_dev);
2577 // the drain should not be necessary
2578 drain_rx_pools (dev);
2579 amb_reset (dev, 0);
2580 interrupts_off (dev);
2581 destroy_queues (dev);
2582 atm_dev_deregister (dev->atm_dev);
2583 free_irq (dev->irq, dev);
2584 release_region (dev->iobase, AMB_EXTENT);
2585 kfree (dev);
2588 return;
2591 #else
2593 /********** monolithic entry **********/
2595 int __init amb_detect (void) {
2596 int devs;
2598 // sanity check - cast needed as printk does not support %Zu
2599 if (sizeof(amb_mem) != 4*16 + 4*12) {
2600 PRINTK (KERN_ERR, "Fix amb_mem (is %lu words).",
2601 (unsigned long) sizeof(amb_mem));
2602 return 0;
2605 show_version();
2607 // check arguments
2608 amb_check_args();
2610 // get the juice
2611 devs = amb_probe();
2613 if (devs) {
2614 init_timer (&housekeeping);
2615 housekeeping.function = do_housekeeping;
2616 // paranoia
2617 housekeeping.data = 1;
2618 set_timer (&housekeeping, 0);
2619 } else {
2620 PRINTK (KERN_INFO, "no (usable) adapters found");
2623 return devs;
2626 #endif