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>
34 #include <linux/poison.h>
35 #include <linux/bitrev.h>
36 #include <linux/mutex.h>
37 #include <linux/firmware.h>
38 #include <linux/ihex.h>
39 #include <linux/slab.h>
41 #include <asm/atomic.h>
43 #include <asm/byteorder.h>
45 #include "ambassador.h"
47 #define maintainer_string "Giuliano Procida at Madge Networks <gprocida@madge.com>"
48 #define description_string "Madge ATM Ambassador driver"
49 #define version_string "1.2.4"
51 static inline void __init
show_version (void) {
52 printk ("%s version %s\n", description_string
, version_string
);
56 static void do_housekeeping (unsigned long arg
);
57 /********** globals **********/
59 static unsigned short debug
= 0;
60 static unsigned int cmds
= 8;
61 static unsigned int txs
= 32;
62 static unsigned int rxs
[NUM_RX_POOLS
] = { 64, 64, 64, 64 };
63 static unsigned int rxs_bs
[NUM_RX_POOLS
] = { 4080, 12240, 36720, 65535 };
64 static unsigned int rx_lats
= 7;
65 static unsigned char pci_lat
= 0;
67 static const unsigned long onegigmask
= -1 << 30;
69 /********** access to adapter **********/
71 static inline void wr_plain (const amb_dev
* dev
, size_t addr
, u32 data
) {
72 PRINTD (DBG_FLOW
|DBG_REGS
, "wr: %08zx <- %08x", addr
, data
);
74 dev
->membase
[addr
/ sizeof(u32
)] = data
;
76 outl (data
, dev
->iobase
+ addr
);
80 static inline u32
rd_plain (const amb_dev
* dev
, size_t addr
) {
82 u32 data
= dev
->membase
[addr
/ sizeof(u32
)];
84 u32 data
= inl (dev
->iobase
+ addr
);
86 PRINTD (DBG_FLOW
|DBG_REGS
, "rd: %08zx -> %08x", addr
, data
);
90 static inline void wr_mem (const amb_dev
* dev
, size_t addr
, u32 data
) {
91 __be32 be
= cpu_to_be32 (data
);
92 PRINTD (DBG_FLOW
|DBG_REGS
, "wr: %08zx <- %08x b[%08x]", addr
, data
, be
);
94 dev
->membase
[addr
/ sizeof(u32
)] = be
;
96 outl (be
, dev
->iobase
+ addr
);
100 static inline u32
rd_mem (const amb_dev
* dev
, size_t addr
) {
102 __be32 be
= dev
->membase
[addr
/ sizeof(u32
)];
104 __be32 be
= inl (dev
->iobase
+ addr
);
106 u32 data
= be32_to_cpu (be
);
107 PRINTD (DBG_FLOW
|DBG_REGS
, "rd: %08zx -> %08x b[%08x]", addr
, data
, be
);
111 /********** dump routines **********/
113 static inline void dump_registers (const amb_dev
* dev
) {
114 #ifdef DEBUG_AMBASSADOR
115 if (debug
& DBG_REGS
) {
117 PRINTD (DBG_REGS
, "reading PLX control: ");
118 for (i
= 0x00; i
< 0x30; i
+= sizeof(u32
))
120 PRINTD (DBG_REGS
, "reading mailboxes: ");
121 for (i
= 0x40; i
< 0x60; i
+= sizeof(u32
))
123 PRINTD (DBG_REGS
, "reading doorb irqev irqen reset:");
124 for (i
= 0x60; i
< 0x70; i
+= sizeof(u32
))
133 static inline void dump_loader_block (volatile loader_block
* lb
) {
134 #ifdef DEBUG_AMBASSADOR
136 PRINTDB (DBG_LOAD
, "lb @ %p; res: %d, cmd: %d, pay:",
137 lb
, be32_to_cpu (lb
->result
), be32_to_cpu (lb
->command
));
138 for (i
= 0; i
< MAX_COMMAND_DATA
; ++i
)
139 PRINTDM (DBG_LOAD
, " %08x", be32_to_cpu (lb
->payload
.data
[i
]));
140 PRINTDE (DBG_LOAD
, ", vld: %08x", be32_to_cpu (lb
->valid
));
147 static inline void dump_command (command
* cmd
) {
148 #ifdef DEBUG_AMBASSADOR
150 PRINTDB (DBG_CMD
, "cmd @ %p, req: %08x, pars:",
151 cmd
, /*be32_to_cpu*/ (cmd
->request
));
152 for (i
= 0; i
< 3; ++i
)
153 PRINTDM (DBG_CMD
, " %08x", /*be32_to_cpu*/ (cmd
->args
.par
[i
]));
154 PRINTDE (DBG_CMD
, "");
161 static inline void dump_skb (char * prefix
, unsigned int vc
, struct sk_buff
* skb
) {
162 #ifdef DEBUG_AMBASSADOR
164 unsigned char * data
= skb
->data
;
165 PRINTDB (DBG_DATA
, "%s(%u) ", prefix
, vc
);
166 for (i
=0; i
<skb
->len
&& i
< 256;i
++)
167 PRINTDM (DBG_DATA
, "%02x ", data
[i
]);
168 PRINTDE (DBG_DATA
,"");
177 /********** check memory areas for use by Ambassador **********/
179 /* see limitations under Hardware Features */
181 static int check_area (void * start
, size_t length
) {
182 // assumes length > 0
183 const u32 fourmegmask
= -1 << 22;
184 const u32 twofivesixmask
= -1 << 8;
185 const u32 starthole
= 0xE0000000;
186 u32 startaddress
= virt_to_bus (start
);
187 u32 lastaddress
= startaddress
+length
-1;
188 if ((startaddress
^ lastaddress
) & fourmegmask
||
189 (startaddress
& twofivesixmask
) == starthole
) {
190 PRINTK (KERN_ERR
, "check_area failure: [%x,%x] - mail maintainer!",
191 startaddress
, lastaddress
);
198 /********** free an skb (as per ATM device driver documentation) **********/
200 static void amb_kfree_skb (struct sk_buff
* skb
) {
201 if (ATM_SKB(skb
)->vcc
->pop
) {
202 ATM_SKB(skb
)->vcc
->pop (ATM_SKB(skb
)->vcc
, skb
);
204 dev_kfree_skb_any (skb
);
208 /********** TX completion **********/
210 static void tx_complete (amb_dev
* dev
, tx_out
* tx
) {
211 tx_simple
* tx_descr
= bus_to_virt (tx
->handle
);
212 struct sk_buff
* skb
= tx_descr
->skb
;
214 PRINTD (DBG_FLOW
|DBG_TX
, "tx_complete %p %p", dev
, tx
);
217 atomic_inc(&ATM_SKB(skb
)->vcc
->stats
->tx
);
219 // free the descriptor
229 /********** RX completion **********/
231 static void rx_complete (amb_dev
* dev
, rx_out
* rx
) {
232 struct sk_buff
* skb
= bus_to_virt (rx
->handle
);
233 u16 vc
= be16_to_cpu (rx
->vc
);
234 // unused: u16 lec_id = be16_to_cpu (rx->lec_id);
235 u16 status
= be16_to_cpu (rx
->status
);
236 u16 rx_len
= be16_to_cpu (rx
->length
);
238 PRINTD (DBG_FLOW
|DBG_RX
, "rx_complete %p %p (len=%hu)", dev
, rx
, rx_len
);
241 struct atm_vcc
* atm_vcc
= dev
->rxer
[vc
];
246 if (rx_len
<= atm_vcc
->qos
.rxtp
.max_sdu
) {
248 if (atm_charge (atm_vcc
, skb
->truesize
)) {
250 // prepare socket buffer
251 ATM_SKB(skb
)->vcc
= atm_vcc
;
252 skb_put (skb
, rx_len
);
254 dump_skb ("<<<", vc
, skb
);
257 atomic_inc(&atm_vcc
->stats
->rx
);
258 __net_timestamp(skb
);
259 // end of our responsability
260 atm_vcc
->push (atm_vcc
, skb
);
264 // someone fix this (message), please!
265 PRINTD (DBG_INFO
|DBG_RX
, "dropped thanks to atm_charge (vc %hu, truesize %u)", vc
, skb
->truesize
);
266 // drop stats incremented in atm_charge
270 PRINTK (KERN_INFO
, "dropped over-size frame");
271 // should we count this?
272 atomic_inc(&atm_vcc
->stats
->rx_drop
);
276 PRINTD (DBG_WARN
|DBG_RX
, "got frame but RX closed for channel %hu", vc
);
277 // this is an adapter bug, only in new version of microcode
281 dev
->stats
.rx
.error
++;
282 if (status
& CRC_ERR
)
283 dev
->stats
.rx
.badcrc
++;
284 if (status
& LEN_ERR
)
285 dev
->stats
.rx
.toolong
++;
286 if (status
& ABORT_ERR
)
287 dev
->stats
.rx
.aborted
++;
288 if (status
& UNUSED_ERR
)
289 dev
->stats
.rx
.unused
++;
292 dev_kfree_skb_any (skb
);
298 Note on queue handling.
300 Here "give" and "take" refer to queue entries and a queue (pair)
301 rather than frames to or from the host or adapter. Empty frame
302 buffers are given to the RX queue pair and returned unused or
303 containing RX frames. TX frames (well, pointers to TX fragment
304 lists) are given to the TX queue pair, completions are returned.
308 /********** command queue **********/
310 // I really don't like this, but it's the best I can do at the moment
312 // also, the callers are responsible for byte order as the microcode
313 // sometimes does 16-bit accesses (yuk yuk yuk)
315 static int command_do (amb_dev
* dev
, command
* cmd
) {
316 amb_cq
* cq
= &dev
->cq
;
317 volatile amb_cq_ptrs
* ptrs
= &cq
->ptrs
;
320 PRINTD (DBG_FLOW
|DBG_CMD
, "command_do %p", dev
);
322 if (test_bit (dead
, &dev
->flags
))
325 spin_lock (&cq
->lock
);
328 if (cq
->pending
< cq
->maximum
) {
329 // remember my slot for later
331 PRINTD (DBG_CMD
, "command in slot %p", my_slot
);
338 ptrs
->in
= NEXTQ (ptrs
->in
, ptrs
->start
, ptrs
->limit
);
341 wr_mem (dev
, offsetof(amb_mem
, mb
.adapter
.cmd_address
), virt_to_bus (ptrs
->in
));
343 if (cq
->pending
> cq
->high
)
344 cq
->high
= cq
->pending
;
345 spin_unlock (&cq
->lock
);
347 // these comments were in a while-loop before, msleep removes the loop
349 // PRINTD (DBG_CMD, "wait: sleeping %lu for command", timeout);
352 // wait for my slot to be reached (all waiters are here or above, until...)
353 while (ptrs
->out
!= my_slot
) {
354 PRINTD (DBG_CMD
, "wait: command slot (now at %p)", ptrs
->out
);
355 set_current_state(TASK_UNINTERRUPTIBLE
);
359 // wait on my slot (... one gets to its slot, and... )
360 while (ptrs
->out
->request
!= cpu_to_be32 (SRB_COMPLETE
)) {
361 PRINTD (DBG_CMD
, "wait: command slot completion");
362 set_current_state(TASK_UNINTERRUPTIBLE
);
366 PRINTD (DBG_CMD
, "command complete");
367 // update queue (... moves the queue along to the next slot)
368 spin_lock (&cq
->lock
);
372 ptrs
->out
= NEXTQ (ptrs
->out
, ptrs
->start
, ptrs
->limit
);
373 spin_unlock (&cq
->lock
);
378 spin_unlock (&cq
->lock
);
384 /********** TX queue pair **********/
386 static int tx_give (amb_dev
* dev
, tx_in
* tx
) {
387 amb_txq
* txq
= &dev
->txq
;
390 PRINTD (DBG_FLOW
|DBG_TX
, "tx_give %p", dev
);
392 if (test_bit (dead
, &dev
->flags
))
395 spin_lock_irqsave (&txq
->lock
, flags
);
397 if (txq
->pending
< txq
->maximum
) {
398 PRINTD (DBG_TX
, "TX in slot %p", txq
->in
.ptr
);
402 txq
->in
.ptr
= NEXTQ (txq
->in
.ptr
, txq
->in
.start
, txq
->in
.limit
);
403 // hand over the TX and ring the bell
404 wr_mem (dev
, offsetof(amb_mem
, mb
.adapter
.tx_address
), virt_to_bus (txq
->in
.ptr
));
405 wr_mem (dev
, offsetof(amb_mem
, doorbell
), TX_FRAME
);
407 if (txq
->pending
> txq
->high
)
408 txq
->high
= txq
->pending
;
409 spin_unlock_irqrestore (&txq
->lock
, flags
);
413 spin_unlock_irqrestore (&txq
->lock
, flags
);
418 static int tx_take (amb_dev
* dev
) {
419 amb_txq
* txq
= &dev
->txq
;
422 PRINTD (DBG_FLOW
|DBG_TX
, "tx_take %p", dev
);
424 spin_lock_irqsave (&txq
->lock
, flags
);
426 if (txq
->pending
&& txq
->out
.ptr
->handle
) {
427 // deal with TX completion
428 tx_complete (dev
, txq
->out
.ptr
);
430 txq
->out
.ptr
->handle
= 0;
433 txq
->out
.ptr
= NEXTQ (txq
->out
.ptr
, txq
->out
.start
, txq
->out
.limit
);
435 spin_unlock_irqrestore (&txq
->lock
, flags
);
439 spin_unlock_irqrestore (&txq
->lock
, flags
);
444 /********** RX queue pairs **********/
446 static int rx_give (amb_dev
* dev
, rx_in
* rx
, unsigned char pool
) {
447 amb_rxq
* rxq
= &dev
->rxq
[pool
];
450 PRINTD (DBG_FLOW
|DBG_RX
, "rx_give %p[%hu]", dev
, pool
);
452 spin_lock_irqsave (&rxq
->lock
, flags
);
454 if (rxq
->pending
< rxq
->maximum
) {
455 PRINTD (DBG_RX
, "RX in slot %p", rxq
->in
.ptr
);
459 rxq
->in
.ptr
= NEXTQ (rxq
->in
.ptr
, rxq
->in
.start
, rxq
->in
.limit
);
460 // hand over the RX buffer
461 wr_mem (dev
, offsetof(amb_mem
, mb
.adapter
.rx_address
[pool
]), virt_to_bus (rxq
->in
.ptr
));
463 spin_unlock_irqrestore (&rxq
->lock
, flags
);
466 spin_unlock_irqrestore (&rxq
->lock
, flags
);
471 static int rx_take (amb_dev
* dev
, unsigned char pool
) {
472 amb_rxq
* rxq
= &dev
->rxq
[pool
];
475 PRINTD (DBG_FLOW
|DBG_RX
, "rx_take %p[%hu]", dev
, pool
);
477 spin_lock_irqsave (&rxq
->lock
, flags
);
479 if (rxq
->pending
&& (rxq
->out
.ptr
->status
|| rxq
->out
.ptr
->length
)) {
480 // deal with RX completion
481 rx_complete (dev
, rxq
->out
.ptr
);
483 rxq
->out
.ptr
->status
= 0;
484 rxq
->out
.ptr
->length
= 0;
487 rxq
->out
.ptr
= NEXTQ (rxq
->out
.ptr
, rxq
->out
.start
, rxq
->out
.limit
);
489 if (rxq
->pending
< rxq
->low
)
490 rxq
->low
= rxq
->pending
;
491 spin_unlock_irqrestore (&rxq
->lock
, flags
);
494 if (!rxq
->pending
&& rxq
->buffers_wanted
)
496 spin_unlock_irqrestore (&rxq
->lock
, flags
);
501 /********** RX Pool handling **********/
503 /* pre: buffers_wanted = 0, post: pending = 0 */
504 static void drain_rx_pool (amb_dev
* dev
, unsigned char pool
) {
505 amb_rxq
* rxq
= &dev
->rxq
[pool
];
507 PRINTD (DBG_FLOW
|DBG_POOL
, "drain_rx_pool %p %hu", dev
, pool
);
509 if (test_bit (dead
, &dev
->flags
))
512 /* we are not quite like the fill pool routines as we cannot just
513 remove one buffer, we have to remove all of them, but we might as
515 if (rxq
->pending
> rxq
->buffers_wanted
) {
517 cmd
.request
= cpu_to_be32 (SRB_FLUSH_BUFFER_Q
);
518 cmd
.args
.flush
.flags
= cpu_to_be32 (pool
<< SRB_POOL_SHIFT
);
519 while (command_do (dev
, &cmd
))
521 /* the pool may also be emptied via the interrupt handler */
522 while (rxq
->pending
> rxq
->buffers_wanted
)
523 if (rx_take (dev
, pool
))
530 static void drain_rx_pools (amb_dev
* dev
) {
533 PRINTD (DBG_FLOW
|DBG_POOL
, "drain_rx_pools %p", dev
);
535 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
536 drain_rx_pool (dev
, pool
);
539 static void fill_rx_pool (amb_dev
* dev
, unsigned char pool
,
545 PRINTD (DBG_FLOW
|DBG_POOL
, "fill_rx_pool %p %hu %x", dev
, pool
, priority
);
547 if (test_bit (dead
, &dev
->flags
))
550 rxq
= &dev
->rxq
[pool
];
551 while (rxq
->pending
< rxq
->maximum
&& rxq
->pending
< rxq
->buffers_wanted
) {
553 struct sk_buff
* skb
= alloc_skb (rxq
->buffer_size
, priority
);
555 PRINTD (DBG_SKB
|DBG_POOL
, "failed to allocate skb for RX pool %hu", pool
);
558 if (check_area (skb
->data
, skb
->truesize
)) {
559 dev_kfree_skb_any (skb
);
562 // cast needed as there is no %? for pointer differences
563 PRINTD (DBG_SKB
, "allocated skb at %p, head %p, area %li",
564 skb
, skb
->head
, (long) (skb_end_pointer(skb
) - skb
->head
));
565 rx
.handle
= virt_to_bus (skb
);
566 rx
.host_address
= cpu_to_be32 (virt_to_bus (skb
->data
));
567 if (rx_give (dev
, &rx
, pool
))
568 dev_kfree_skb_any (skb
);
575 // top up all RX pools (can also be called as a bottom half)
576 static void fill_rx_pools (amb_dev
* dev
) {
579 PRINTD (DBG_FLOW
|DBG_POOL
, "fill_rx_pools %p", dev
);
581 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
582 fill_rx_pool (dev
, pool
, GFP_ATOMIC
);
587 /********** enable host interrupts **********/
589 static void interrupts_on (amb_dev
* dev
) {
590 wr_plain (dev
, offsetof(amb_mem
, interrupt_control
),
591 rd_plain (dev
, offsetof(amb_mem
, interrupt_control
))
592 | AMB_INTERRUPT_BITS
);
595 /********** disable host interrupts **********/
597 static void interrupts_off (amb_dev
* dev
) {
598 wr_plain (dev
, offsetof(amb_mem
, interrupt_control
),
599 rd_plain (dev
, offsetof(amb_mem
, interrupt_control
))
600 &~ AMB_INTERRUPT_BITS
);
603 /********** interrupt handling **********/
605 static irqreturn_t
interrupt_handler(int irq
, void *dev_id
) {
606 amb_dev
* dev
= dev_id
;
608 PRINTD (DBG_IRQ
|DBG_FLOW
, "interrupt_handler: %p", dev_id
);
611 u32 interrupt
= rd_plain (dev
, offsetof(amb_mem
, interrupt
));
613 // for us or someone else sharing the same interrupt
615 PRINTD (DBG_IRQ
, "irq not for me: %d", irq
);
620 PRINTD (DBG_IRQ
, "FYI: interrupt was %08x", interrupt
);
621 wr_plain (dev
, offsetof(amb_mem
, interrupt
), -1);
625 unsigned int irq_work
= 0;
627 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
628 while (!rx_take (dev
, pool
))
630 while (!tx_take (dev
))
634 #ifdef FILL_RX_POOLS_IN_BH
635 schedule_work (&dev
->bh
);
640 PRINTD (DBG_IRQ
, "work done: %u", irq_work
);
642 PRINTD (DBG_IRQ
|DBG_WARN
, "no work done");
646 PRINTD (DBG_IRQ
|DBG_FLOW
, "interrupt_handler done: %p", dev_id
);
650 /********** make rate (not quite as much fun as Horizon) **********/
652 static int make_rate (unsigned int rate
, rounding r
,
653 u16
* bits
, unsigned int * actual
) {
654 unsigned char exp
= -1; // hush gcc
655 unsigned int man
= -1; // hush gcc
657 PRINTD (DBG_FLOW
|DBG_QOS
, "make_rate %u", rate
);
659 // rates in cells per second, ITU format (nasty 16-bit floating-point)
660 // given 5-bit e and 9-bit m:
661 // rate = EITHER (1+m/2^9)*2^e OR 0
662 // bits = EITHER 1<<14 | e<<9 | m OR 0
663 // (bit 15 is "reserved", bit 14 "non-zero")
664 // smallest rate is 0 (special representation)
665 // largest rate is (1+511/512)*2^31 = 4290772992 (< 2^32-1)
666 // smallest non-zero rate is (1+0/512)*2^0 = 1 (> 0)
668 // find position of top bit, this gives e
669 // remove top bit and shift (rounding if feeling clever) by 9-e
671 // ucode bug: please don't set bit 14! so 0 rate not representable
673 if (rate
> 0xffc00000U
) {
674 // larger than largest representable rate
684 // representable rate
689 // invariant: rate = man*2^(exp-31)
690 while (!(man
& (1<<31))) {
695 // man has top bit set
696 // rate = (2^31+(man-2^31))*2^(exp-31)
697 // rate = (1+(man-2^31)/2^31)*2^exp
699 man
&= 0xffffffffU
; // a nop on 32-bit systems
700 // rate = (1+man/2^32)*2^exp
702 // exp is in the range 0 to 31, man is in the range 0 to 2^32-1
703 // time to lose significance... we want m in the range 0 to 2^9-1
704 // rounding presents a minor problem... we first decide which way
705 // we are rounding (based on given rounding direction and possibly
706 // the bits of the mantissa that are to be discarded).
715 // check all bits that we are discarding
716 if (man
& (~0U>>9)) {
717 man
= (man
>>(32-9)) + 1;
719 // no need to check for round up outside of range
728 case round_nearest
: {
729 // check msb that we are discarding
730 if (man
& (1<<(32-9-1))) {
731 man
= (man
>>(32-9)) + 1;
733 // no need to check for round up outside of range
745 // zero rate - not representable
747 if (r
== round_down
) {
756 PRINTD (DBG_QOS
, "rate: man=%u, exp=%hu", man
, exp
);
759 *bits
= /* (1<<14) | */ (exp
<<9) | man
;
763 ? (1 << exp
) + (man
<< (exp
-9))
764 : (1 << exp
) + ((man
+ (1<<(9-exp
-1))) >> (9-exp
));
769 /********** Linux ATM Operations **********/
771 // some are not yet implemented while others do not make sense for
774 /********** Open a VC **********/
776 static int amb_open (struct atm_vcc
* atm_vcc
)
780 struct atm_qos
* qos
;
781 struct atm_trafprm
* txtp
;
782 struct atm_trafprm
* rxtp
;
783 u16 tx_rate_bits
= -1; // hush gcc
784 u16 tx_vc_bits
= -1; // hush gcc
785 u16 tx_frame_bits
= -1; // hush gcc
787 amb_dev
* dev
= AMB_DEV(atm_vcc
->dev
);
789 unsigned char pool
= -1; // hush gcc
790 short vpi
= atm_vcc
->vpi
;
791 int vci
= atm_vcc
->vci
;
793 PRINTD (DBG_FLOW
|DBG_VCC
, "amb_open %x %x", vpi
, vci
);
795 #ifdef ATM_VPI_UNSPEC
796 // UNSPEC is deprecated, remove this code eventually
797 if (vpi
== ATM_VPI_UNSPEC
|| vci
== ATM_VCI_UNSPEC
) {
798 PRINTK (KERN_WARNING
, "rejecting open with unspecified VPI/VCI (deprecated)");
803 if (!(0 <= vpi
&& vpi
< (1<<NUM_VPI_BITS
) &&
804 0 <= vci
&& vci
< (1<<NUM_VCI_BITS
))) {
805 PRINTD (DBG_WARN
|DBG_VCC
, "VPI/VCI out of range: %hd/%d", vpi
, vci
);
811 if (qos
->aal
!= ATM_AAL5
) {
812 PRINTD (DBG_QOS
, "AAL not supported");
816 // traffic parameters
818 PRINTD (DBG_QOS
, "TX:");
820 if (txtp
->traffic_class
!= ATM_NONE
) {
821 switch (txtp
->traffic_class
) {
823 // we take "the PCR" as a rate-cap
824 int pcr
= atm_pcr_goal (txtp
);
829 tx_frame_bits
= TX_FRAME_NOTCAP
;
838 error
= make_rate (pcr
, r
, &tx_rate_bits
, NULL
);
841 tx_vc_bits
= TX_UBR_CAPPED
;
842 tx_frame_bits
= TX_FRAME_CAPPED
;
847 // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
848 PRINTD (DBG_QOS
, "request for non-UBR denied");
852 PRINTD (DBG_QOS
, "tx_rate_bits=%hx, tx_vc_bits=%hx",
853 tx_rate_bits
, tx_vc_bits
);
856 PRINTD (DBG_QOS
, "RX:");
858 if (rxtp
->traffic_class
== ATM_NONE
) {
861 // choose an RX pool (arranged in increasing size)
862 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
863 if ((unsigned int) rxtp
->max_sdu
<= dev
->rxq
[pool
].buffer_size
) {
864 PRINTD (DBG_VCC
|DBG_QOS
|DBG_POOL
, "chose pool %hu (max_sdu %u <= %u)",
865 pool
, rxtp
->max_sdu
, dev
->rxq
[pool
].buffer_size
);
868 if (pool
== NUM_RX_POOLS
) {
869 PRINTD (DBG_WARN
|DBG_VCC
|DBG_QOS
|DBG_POOL
,
870 "no pool suitable for VC (RX max_sdu %d is too large)",
875 switch (rxtp
->traffic_class
) {
880 // PRINTD (DBG_QOS, "request for non-UBR/ABR denied");
881 PRINTD (DBG_QOS
, "request for non-UBR denied");
887 // get space for our vcc stuff
888 vcc
= kmalloc (sizeof(amb_vcc
), GFP_KERNEL
);
890 PRINTK (KERN_ERR
, "out of memory!");
893 atm_vcc
->dev_data
= (void *) vcc
;
895 // no failures beyond this point
897 // we are not really "immediately before allocating the connection
898 // identifier in hardware", but it will just have to do!
899 set_bit(ATM_VF_ADDR
,&atm_vcc
->flags
);
901 if (txtp
->traffic_class
!= ATM_NONE
) {
904 vcc
->tx_frame_bits
= tx_frame_bits
;
906 mutex_lock(&dev
->vcc_sf
);
907 if (dev
->rxer
[vci
]) {
908 // RXer on the channel already, just modify rate...
909 cmd
.request
= cpu_to_be32 (SRB_MODIFY_VC_RATE
);
910 cmd
.args
.modify_rate
.vc
= cpu_to_be32 (vci
); // vpi 0
911 cmd
.args
.modify_rate
.rate
= cpu_to_be32 (tx_rate_bits
<< SRB_RATE_SHIFT
);
912 while (command_do (dev
, &cmd
))
914 // ... and TX flags, preserving the RX pool
915 cmd
.request
= cpu_to_be32 (SRB_MODIFY_VC_FLAGS
);
916 cmd
.args
.modify_flags
.vc
= cpu_to_be32 (vci
); // vpi 0
917 cmd
.args
.modify_flags
.flags
= cpu_to_be32
918 ( (AMB_VCC(dev
->rxer
[vci
])->rx_info
.pool
<< SRB_POOL_SHIFT
)
919 | (tx_vc_bits
<< SRB_FLAGS_SHIFT
) );
920 while (command_do (dev
, &cmd
))
923 // no RXer on the channel, just open (with pool zero)
924 cmd
.request
= cpu_to_be32 (SRB_OPEN_VC
);
925 cmd
.args
.open
.vc
= cpu_to_be32 (vci
); // vpi 0
926 cmd
.args
.open
.flags
= cpu_to_be32 (tx_vc_bits
<< SRB_FLAGS_SHIFT
);
927 cmd
.args
.open
.rate
= cpu_to_be32 (tx_rate_bits
<< SRB_RATE_SHIFT
);
928 while (command_do (dev
, &cmd
))
931 dev
->txer
[vci
].tx_present
= 1;
932 mutex_unlock(&dev
->vcc_sf
);
935 if (rxtp
->traffic_class
!= ATM_NONE
) {
938 vcc
->rx_info
.pool
= pool
;
940 mutex_lock(&dev
->vcc_sf
);
941 /* grow RX buffer pool */
942 if (!dev
->rxq
[pool
].buffers_wanted
)
943 dev
->rxq
[pool
].buffers_wanted
= rx_lats
;
944 dev
->rxq
[pool
].buffers_wanted
+= 1;
945 fill_rx_pool (dev
, pool
, GFP_KERNEL
);
947 if (dev
->txer
[vci
].tx_present
) {
948 // TXer on the channel already
949 // switch (from pool zero) to this pool, preserving the TX bits
950 cmd
.request
= cpu_to_be32 (SRB_MODIFY_VC_FLAGS
);
951 cmd
.args
.modify_flags
.vc
= cpu_to_be32 (vci
); // vpi 0
952 cmd
.args
.modify_flags
.flags
= cpu_to_be32
953 ( (pool
<< SRB_POOL_SHIFT
)
954 | (dev
->txer
[vci
].tx_vc_bits
<< SRB_FLAGS_SHIFT
) );
956 // no TXer on the channel, open the VC (with no rate info)
957 cmd
.request
= cpu_to_be32 (SRB_OPEN_VC
);
958 cmd
.args
.open
.vc
= cpu_to_be32 (vci
); // vpi 0
959 cmd
.args
.open
.flags
= cpu_to_be32 (pool
<< SRB_POOL_SHIFT
);
960 cmd
.args
.open
.rate
= cpu_to_be32 (0);
962 while (command_do (dev
, &cmd
))
964 // this link allows RX frames through
965 dev
->rxer
[vci
] = atm_vcc
;
966 mutex_unlock(&dev
->vcc_sf
);
969 // indicate readiness
970 set_bit(ATM_VF_READY
,&atm_vcc
->flags
);
975 /********** Close a VC **********/
977 static void amb_close (struct atm_vcc
* atm_vcc
) {
978 amb_dev
* dev
= AMB_DEV (atm_vcc
->dev
);
979 amb_vcc
* vcc
= AMB_VCC (atm_vcc
);
980 u16 vci
= atm_vcc
->vci
;
982 PRINTD (DBG_VCC
|DBG_FLOW
, "amb_close");
984 // indicate unreadiness
985 clear_bit(ATM_VF_READY
,&atm_vcc
->flags
);
988 if (atm_vcc
->qos
.txtp
.traffic_class
!= ATM_NONE
) {
991 mutex_lock(&dev
->vcc_sf
);
992 if (dev
->rxer
[vci
]) {
993 cmd
.request
= cpu_to_be32 (SRB_MODIFY_VC_RATE
);
994 cmd
.args
.modify_rate
.vc
= cpu_to_be32 (vci
); // vpi 0
995 cmd
.args
.modify_rate
.rate
= cpu_to_be32 (0);
997 // no RXer on the channel, close channel
998 cmd
.request
= cpu_to_be32 (SRB_CLOSE_VC
);
999 cmd
.args
.close
.vc
= cpu_to_be32 (vci
); // vpi 0
1001 dev
->txer
[vci
].tx_present
= 0;
1002 while (command_do (dev
, &cmd
))
1004 mutex_unlock(&dev
->vcc_sf
);
1008 if (atm_vcc
->qos
.rxtp
.traffic_class
!= ATM_NONE
) {
1011 // this is (the?) one reason why we need the amb_vcc struct
1012 unsigned char pool
= vcc
->rx_info
.pool
;
1014 mutex_lock(&dev
->vcc_sf
);
1015 if (dev
->txer
[vci
].tx_present
) {
1016 cmd
.request
= cpu_to_be32 (SRB_MODIFY_VC_FLAGS
);
1017 cmd
.args
.modify_flags
.vc
= cpu_to_be32 (vci
); // vpi 0
1018 cmd
.args
.modify_flags
.flags
= cpu_to_be32
1019 (dev
->txer
[vci
].tx_vc_bits
<< SRB_FLAGS_SHIFT
);
1021 // no TXer on the channel, close the VC
1022 cmd
.request
= cpu_to_be32 (SRB_CLOSE_VC
);
1023 cmd
.args
.close
.vc
= cpu_to_be32 (vci
); // vpi 0
1025 // forget the rxer - no more skbs will be pushed
1026 if (atm_vcc
!= dev
->rxer
[vci
])
1027 PRINTK (KERN_ERR
, "%s vcc=%p rxer[vci]=%p",
1028 "arghhh! we're going to die!",
1029 vcc
, dev
->rxer
[vci
]);
1030 dev
->rxer
[vci
] = NULL
;
1031 while (command_do (dev
, &cmd
))
1034 /* shrink RX buffer pool */
1035 dev
->rxq
[pool
].buffers_wanted
-= 1;
1036 if (dev
->rxq
[pool
].buffers_wanted
== rx_lats
) {
1037 dev
->rxq
[pool
].buffers_wanted
= 0;
1038 drain_rx_pool (dev
, pool
);
1040 mutex_unlock(&dev
->vcc_sf
);
1043 // free our structure
1046 // say the VPI/VCI is free again
1047 clear_bit(ATM_VF_ADDR
,&atm_vcc
->flags
);
1052 /********** Send **********/
1054 static int amb_send (struct atm_vcc
* atm_vcc
, struct sk_buff
* skb
) {
1055 amb_dev
* dev
= AMB_DEV(atm_vcc
->dev
);
1056 amb_vcc
* vcc
= AMB_VCC(atm_vcc
);
1057 u16 vc
= atm_vcc
->vci
;
1058 unsigned int tx_len
= skb
->len
;
1059 unsigned char * tx_data
= skb
->data
;
1060 tx_simple
* tx_descr
;
1063 if (test_bit (dead
, &dev
->flags
))
1066 PRINTD (DBG_FLOW
|DBG_TX
, "amb_send vc %x data %p len %u",
1067 vc
, tx_data
, tx_len
);
1069 dump_skb (">>>", vc
, skb
);
1071 if (!dev
->txer
[vc
].tx_present
) {
1072 PRINTK (KERN_ERR
, "attempt to send on RX-only VC %x", vc
);
1076 // this is a driver private field so we have to set it ourselves,
1077 // despite the fact that we are _required_ to use it to check for a
1079 ATM_SKB(skb
)->vcc
= atm_vcc
;
1081 if (skb
->len
> (size_t) atm_vcc
->qos
.txtp
.max_sdu
) {
1082 PRINTK (KERN_ERR
, "sk_buff length greater than agreed max_sdu, dropping...");
1086 if (check_area (skb
->data
, skb
->len
)) {
1087 atomic_inc(&atm_vcc
->stats
->tx_err
);
1088 return -ENOMEM
; // ?
1091 // allocate memory for fragments
1092 tx_descr
= kmalloc (sizeof(tx_simple
), GFP_KERNEL
);
1094 PRINTK (KERN_ERR
, "could not allocate TX descriptor");
1097 if (check_area (tx_descr
, sizeof(tx_simple
))) {
1101 PRINTD (DBG_TX
, "fragment list allocated at %p", tx_descr
);
1103 tx_descr
->skb
= skb
;
1105 tx_descr
->tx_frag
.bytes
= cpu_to_be32 (tx_len
);
1106 tx_descr
->tx_frag
.address
= cpu_to_be32 (virt_to_bus (tx_data
));
1108 tx_descr
->tx_frag_end
.handle
= virt_to_bus (tx_descr
);
1109 tx_descr
->tx_frag_end
.vc
= 0;
1110 tx_descr
->tx_frag_end
.next_descriptor_length
= 0;
1111 tx_descr
->tx_frag_end
.next_descriptor
= 0;
1112 #ifdef AMB_NEW_MICROCODE
1113 tx_descr
->tx_frag_end
.cpcs_uu
= 0;
1114 tx_descr
->tx_frag_end
.cpi
= 0;
1115 tx_descr
->tx_frag_end
.pad
= 0;
1118 tx
.vc
= cpu_to_be16 (vcc
->tx_frame_bits
| vc
);
1119 tx
.tx_descr_length
= cpu_to_be16 (sizeof(tx_frag
)+sizeof(tx_frag_end
));
1120 tx
.tx_descr_addr
= cpu_to_be32 (virt_to_bus (&tx_descr
->tx_frag
));
1122 while (tx_give (dev
, &tx
))
1127 /********** Change QoS on a VC **********/
1129 // int amb_change_qos (struct atm_vcc * atm_vcc, struct atm_qos * qos, int flags);
1131 /********** Free RX Socket Buffer **********/
1134 /********** Proc File Output **********/
1136 static int amb_proc_read (struct atm_dev
* atm_dev
, loff_t
* pos
, char * page
) {
1137 amb_dev
* dev
= AMB_DEV (atm_dev
);
1141 PRINTD (DBG_FLOW
, "amb_proc_read");
1143 /* more diagnostics here? */
1146 amb_stats
* s
= &dev
->stats
;
1147 return sprintf (page
,
1148 "frames: TX OK %lu, RX OK %lu, RX bad %lu "
1149 "(CRC %lu, long %lu, aborted %lu, unused %lu).\n",
1150 s
->tx_ok
, s
->rx
.ok
, s
->rx
.error
,
1151 s
->rx
.badcrc
, s
->rx
.toolong
,
1152 s
->rx
.aborted
, s
->rx
.unused
);
1156 amb_cq
* c
= &dev
->cq
;
1157 return sprintf (page
, "cmd queue [cur/hi/max]: %u/%u/%u. ",
1158 c
->pending
, c
->high
, c
->maximum
);
1162 amb_txq
* t
= &dev
->txq
;
1163 return sprintf (page
, "TX queue [cur/max high full]: %u/%u %u %u.\n",
1164 t
->pending
, t
->maximum
, t
->high
, t
->filled
);
1168 unsigned int count
= sprintf (page
, "RX queues [cur/max/req low empty]:");
1169 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
) {
1170 amb_rxq
* r
= &dev
->rxq
[pool
];
1171 count
+= sprintf (page
+count
, " %u/%u/%u %u %u",
1172 r
->pending
, r
->maximum
, r
->buffers_wanted
, r
->low
, r
->emptied
);
1174 count
+= sprintf (page
+count
, ".\n");
1179 unsigned int count
= sprintf (page
, "RX buffer sizes:");
1180 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
) {
1181 amb_rxq
* r
= &dev
->rxq
[pool
];
1182 count
+= sprintf (page
+count
, " %u", r
->buffer_size
);
1184 count
+= sprintf (page
+count
, ".\n");
1192 /********** Operation Structure **********/
1194 static const struct atmdev_ops amb_ops
= {
1198 .proc_read
= amb_proc_read
,
1199 .owner
= THIS_MODULE
,
1202 /********** housekeeping **********/
1203 static void do_housekeeping (unsigned long arg
) {
1204 amb_dev
* dev
= (amb_dev
*) arg
;
1206 // could collect device-specific (not driver/atm-linux) stats here
1208 // last resort refill once every ten seconds
1209 fill_rx_pools (dev
);
1210 mod_timer(&dev
->housekeeping
, jiffies
+ 10*HZ
);
1215 /********** creation of communication queues **********/
1217 static int __devinit
create_queues (amb_dev
* dev
, unsigned int cmds
,
1218 unsigned int txs
, unsigned int * rxs
,
1219 unsigned int * rx_buffer_sizes
) {
1225 PRINTD (DBG_FLOW
, "create_queues %p", dev
);
1227 total
+= cmds
* sizeof(command
);
1229 total
+= txs
* (sizeof(tx_in
) + sizeof(tx_out
));
1231 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
1232 total
+= rxs
[pool
] * (sizeof(rx_in
) + sizeof(rx_out
));
1234 memory
= kmalloc (total
, GFP_KERNEL
);
1236 PRINTK (KERN_ERR
, "could not allocate queues");
1239 if (check_area (memory
, total
)) {
1240 PRINTK (KERN_ERR
, "queues allocated in nasty area");
1245 limit
= memory
+ total
;
1246 PRINTD (DBG_INIT
, "queues from %p to %p", memory
, limit
);
1248 PRINTD (DBG_CMD
, "command queue at %p", memory
);
1251 command
* cmd
= memory
;
1252 amb_cq
* cq
= &dev
->cq
;
1256 cq
->maximum
= cmds
- 1;
1258 cq
->ptrs
.start
= cmd
;
1261 cq
->ptrs
.limit
= cmd
+ cmds
;
1263 memory
= cq
->ptrs
.limit
;
1266 PRINTD (DBG_TX
, "TX queue pair at %p", memory
);
1269 tx_in
* in
= memory
;
1271 amb_txq
* txq
= &dev
->txq
;
1276 txq
->maximum
= txs
- 1;
1280 txq
->in
.limit
= in
+ txs
;
1282 memory
= txq
->in
.limit
;
1285 txq
->out
.start
= out
;
1287 txq
->out
.limit
= out
+ txs
;
1289 memory
= txq
->out
.limit
;
1292 PRINTD (DBG_RX
, "RX queue pairs at %p", memory
);
1294 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
) {
1295 rx_in
* in
= memory
;
1297 amb_rxq
* rxq
= &dev
->rxq
[pool
];
1299 rxq
->buffer_size
= rx_buffer_sizes
[pool
];
1300 rxq
->buffers_wanted
= 0;
1303 rxq
->low
= rxs
[pool
] - 1;
1305 rxq
->maximum
= rxs
[pool
] - 1;
1309 rxq
->in
.limit
= in
+ rxs
[pool
];
1311 memory
= rxq
->in
.limit
;
1314 rxq
->out
.start
= out
;
1316 rxq
->out
.limit
= out
+ rxs
[pool
];
1318 memory
= rxq
->out
.limit
;
1321 if (memory
== limit
) {
1324 PRINTK (KERN_ERR
, "bad queue alloc %p != %p (tell maintainer)", memory
, limit
);
1325 kfree (limit
- total
);
1331 /********** destruction of communication queues **********/
1333 static void destroy_queues (amb_dev
* dev
) {
1334 // all queues assumed empty
1335 void * memory
= dev
->cq
.ptrs
.start
;
1336 // includes txq.in, txq.out, rxq[].in and rxq[].out
1338 PRINTD (DBG_FLOW
, "destroy_queues %p", dev
);
1340 PRINTD (DBG_INIT
, "freeing queues at %p", memory
);
1346 /********** basic loader commands and error handling **********/
1347 // centisecond timeouts - guessing away here
1348 static unsigned int command_timeouts
[] = {
1349 [host_memory_test
] = 15,
1350 [read_adapter_memory
] = 2,
1351 [write_adapter_memory
] = 2,
1352 [adapter_start
] = 50,
1353 [get_version_number
] = 10,
1354 [interrupt_host
] = 1,
1355 [flash_erase_sector
] = 1,
1356 [adap_download_block
] = 1,
1357 [adap_erase_flash
] = 1,
1358 [adap_run_in_iram
] = 1,
1359 [adap_end_download
] = 1
1363 static unsigned int command_successes
[] = {
1364 [host_memory_test
] = COMMAND_PASSED_TEST
,
1365 [read_adapter_memory
] = COMMAND_READ_DATA_OK
,
1366 [write_adapter_memory
] = COMMAND_WRITE_DATA_OK
,
1367 [adapter_start
] = COMMAND_COMPLETE
,
1368 [get_version_number
] = COMMAND_COMPLETE
,
1369 [interrupt_host
] = COMMAND_COMPLETE
,
1370 [flash_erase_sector
] = COMMAND_COMPLETE
,
1371 [adap_download_block
] = COMMAND_COMPLETE
,
1372 [adap_erase_flash
] = COMMAND_COMPLETE
,
1373 [adap_run_in_iram
] = COMMAND_COMPLETE
,
1374 [adap_end_download
] = COMMAND_COMPLETE
1377 static int decode_loader_result (loader_command cmd
, u32 result
)
1382 if (result
== command_successes
[cmd
])
1388 msg
= "bad command";
1390 case COMMAND_IN_PROGRESS
:
1392 msg
= "command in progress";
1394 case COMMAND_PASSED_TEST
:
1396 msg
= "command passed test";
1398 case COMMAND_FAILED_TEST
:
1400 msg
= "command failed test";
1402 case COMMAND_READ_DATA_OK
:
1404 msg
= "command read data ok";
1406 case COMMAND_READ_BAD_ADDRESS
:
1408 msg
= "command read bad address";
1410 case COMMAND_WRITE_DATA_OK
:
1412 msg
= "command write data ok";
1414 case COMMAND_WRITE_BAD_ADDRESS
:
1416 msg
= "command write bad address";
1418 case COMMAND_WRITE_FLASH_FAILURE
:
1420 msg
= "command write flash failure";
1422 case COMMAND_COMPLETE
:
1424 msg
= "command complete";
1426 case COMMAND_FLASH_ERASE_FAILURE
:
1428 msg
= "command flash erase failure";
1430 case COMMAND_WRITE_BAD_DATA
:
1432 msg
= "command write bad data";
1436 msg
= "unknown error";
1437 PRINTD (DBG_LOAD
|DBG_ERR
,
1438 "decode_loader_result got %d=%x !",
1443 PRINTK (KERN_ERR
, "%s", msg
);
1447 static int __devinit
do_loader_command (volatile loader_block
* lb
,
1448 const amb_dev
* dev
, loader_command cmd
) {
1450 unsigned long timeout
;
1452 PRINTD (DBG_FLOW
|DBG_LOAD
, "do_loader_command");
1456 Set the return value to zero, set the command type and set the
1457 valid entry to the right magic value. The payload is already
1458 correctly byte-ordered so we leave it alone. Hit the doorbell
1459 with the bus address of this structure.
1464 lb
->command
= cpu_to_be32 (cmd
);
1465 lb
->valid
= cpu_to_be32 (DMA_VALID
);
1466 // dump_registers (dev);
1467 // dump_loader_block (lb);
1468 wr_mem (dev
, offsetof(amb_mem
, doorbell
), virt_to_bus (lb
) & ~onegigmask
);
1470 timeout
= command_timeouts
[cmd
] * 10;
1472 while (!lb
->result
|| lb
->result
== cpu_to_be32 (COMMAND_IN_PROGRESS
))
1474 timeout
= msleep_interruptible(timeout
);
1476 PRINTD (DBG_LOAD
|DBG_ERR
, "command %d timed out", cmd
);
1477 dump_registers (dev
);
1478 dump_loader_block (lb
);
1482 if (cmd
== adapter_start
) {
1483 // wait for start command to acknowledge...
1485 while (rd_plain (dev
, offsetof(amb_mem
, doorbell
)))
1487 timeout
= msleep_interruptible(timeout
);
1489 PRINTD (DBG_LOAD
|DBG_ERR
, "start command did not clear doorbell, res=%08x",
1490 be32_to_cpu (lb
->result
));
1491 dump_registers (dev
);
1496 return decode_loader_result (cmd
, be32_to_cpu (lb
->result
));
1501 /* loader: determine loader version */
1503 static int __devinit
get_loader_version (loader_block
* lb
,
1504 const amb_dev
* dev
, u32
* version
) {
1507 PRINTD (DBG_FLOW
|DBG_LOAD
, "get_loader_version");
1509 res
= do_loader_command (lb
, dev
, get_version_number
);
1513 *version
= be32_to_cpu (lb
->payload
.version
);
1517 /* loader: write memory data blocks */
1519 static int __devinit
loader_write (loader_block
* lb
,
1521 const struct ihex_binrec
*rec
) {
1522 transfer_block
* tb
= &lb
->payload
.transfer
;
1524 PRINTD (DBG_FLOW
|DBG_LOAD
, "loader_write");
1526 tb
->address
= rec
->addr
;
1527 tb
->count
= cpu_to_be32(be16_to_cpu(rec
->len
) / 4);
1528 memcpy(tb
->data
, rec
->data
, be16_to_cpu(rec
->len
));
1529 return do_loader_command (lb
, dev
, write_adapter_memory
);
1532 /* loader: verify memory data blocks */
1534 static int __devinit
loader_verify (loader_block
* lb
,
1536 const struct ihex_binrec
*rec
) {
1537 transfer_block
* tb
= &lb
->payload
.transfer
;
1540 PRINTD (DBG_FLOW
|DBG_LOAD
, "loader_verify");
1542 tb
->address
= rec
->addr
;
1543 tb
->count
= cpu_to_be32(be16_to_cpu(rec
->len
) / 4);
1544 res
= do_loader_command (lb
, dev
, read_adapter_memory
);
1545 if (!res
&& memcmp(tb
->data
, rec
->data
, be16_to_cpu(rec
->len
)))
1550 /* loader: start microcode */
1552 static int __devinit
loader_start (loader_block
* lb
,
1553 const amb_dev
* dev
, u32 address
) {
1554 PRINTD (DBG_FLOW
|DBG_LOAD
, "loader_start");
1556 lb
->payload
.start
= cpu_to_be32 (address
);
1557 return do_loader_command (lb
, dev
, adapter_start
);
1560 /********** reset card **********/
1562 static inline void sf (const char * msg
)
1564 PRINTK (KERN_ERR
, "self-test failed: %s", msg
);
1567 static int amb_reset (amb_dev
* dev
, int diags
) {
1570 PRINTD (DBG_FLOW
|DBG_LOAD
, "amb_reset");
1572 word
= rd_plain (dev
, offsetof(amb_mem
, reset_control
));
1573 // put card into reset state
1574 wr_plain (dev
, offsetof(amb_mem
, reset_control
), word
| AMB_RESET_BITS
);
1575 // wait a short while
1577 // put card into known good state
1578 wr_plain (dev
, offsetof(amb_mem
, interrupt_control
), AMB_DOORBELL_BITS
);
1579 // clear all interrupts just in case
1580 wr_plain (dev
, offsetof(amb_mem
, interrupt
), -1);
1581 // clear self-test done flag
1582 wr_plain (dev
, offsetof(amb_mem
, mb
.loader
.ready
), 0);
1583 // take card out of reset state
1584 wr_plain (dev
, offsetof(amb_mem
, reset_control
), word
&~ AMB_RESET_BITS
);
1587 unsigned long timeout
;
1590 // half second time-out
1592 while (!rd_plain (dev
, offsetof(amb_mem
, mb
.loader
.ready
)))
1594 timeout
= msleep_interruptible(timeout
);
1596 PRINTD (DBG_LOAD
|DBG_ERR
, "reset timed out");
1600 // get results of self-test
1601 word
= rd_mem (dev
, offsetof(amb_mem
, mb
.loader
.result
));
1602 if (word
& SELF_TEST_FAILURE
) {
1603 if (word
& GPINT_TST_FAILURE
)
1605 if (word
& SUNI_DATA_PATTERN_FAILURE
)
1606 sf ("SUNI data pattern");
1607 if (word
& SUNI_DATA_BITS_FAILURE
)
1608 sf ("SUNI data bits");
1609 if (word
& SUNI_UTOPIA_FAILURE
)
1610 sf ("SUNI UTOPIA interface");
1611 if (word
& SUNI_FIFO_FAILURE
)
1612 sf ("SUNI cell buffer FIFO");
1613 if (word
& SRAM_FAILURE
)
1615 // better return value?
1623 /********** transfer and start the microcode **********/
1625 static int __devinit
ucode_init (loader_block
* lb
, amb_dev
* dev
) {
1626 const struct firmware
*fw
;
1627 unsigned long start_address
;
1628 const struct ihex_binrec
*rec
;
1631 res
= request_ihex_firmware(&fw
, "atmsar11.fw", &dev
->pci_dev
->dev
);
1633 PRINTK (KERN_ERR
, "Cannot load microcode data");
1637 /* First record contains just the start address */
1638 rec
= (const struct ihex_binrec
*)fw
->data
;
1639 if (be16_to_cpu(rec
->len
) != sizeof(__be32
) || be32_to_cpu(rec
->addr
)) {
1640 PRINTK (KERN_ERR
, "Bad microcode data (no start record)");
1643 start_address
= be32_to_cpup((__be32
*)rec
->data
);
1645 rec
= ihex_next_binrec(rec
);
1647 PRINTD (DBG_FLOW
|DBG_LOAD
, "ucode_init");
1650 PRINTD (DBG_LOAD
, "starting region (%x, %u)", be32_to_cpu(rec
->addr
),
1651 be16_to_cpu(rec
->len
));
1652 if (be16_to_cpu(rec
->len
) > 4 * MAX_TRANSFER_DATA
) {
1653 PRINTK (KERN_ERR
, "Bad microcode data (record too long)");
1656 if (be16_to_cpu(rec
->len
) & 3) {
1657 PRINTK (KERN_ERR
, "Bad microcode data (odd number of bytes)");
1660 res
= loader_write(lb
, dev
, rec
);
1664 res
= loader_verify(lb
, dev
, rec
);
1668 release_firmware(fw
);
1670 res
= loader_start(lb
, dev
, start_address
);
1675 /********** give adapter parameters **********/
1677 static inline __be32
bus_addr(void * addr
) {
1678 return cpu_to_be32 (virt_to_bus (addr
));
1681 static int __devinit
amb_talk (amb_dev
* dev
) {
1684 unsigned long timeout
;
1686 PRINTD (DBG_FLOW
, "amb_talk %p", dev
);
1688 a
.command_start
= bus_addr (dev
->cq
.ptrs
.start
);
1689 a
.command_end
= bus_addr (dev
->cq
.ptrs
.limit
);
1690 a
.tx_start
= bus_addr (dev
->txq
.in
.start
);
1691 a
.tx_end
= bus_addr (dev
->txq
.in
.limit
);
1692 a
.txcom_start
= bus_addr (dev
->txq
.out
.start
);
1693 a
.txcom_end
= bus_addr (dev
->txq
.out
.limit
);
1695 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
) {
1696 // the other "a" items are set up by the adapter
1697 a
.rec_struct
[pool
].buffer_start
= bus_addr (dev
->rxq
[pool
].in
.start
);
1698 a
.rec_struct
[pool
].buffer_end
= bus_addr (dev
->rxq
[pool
].in
.limit
);
1699 a
.rec_struct
[pool
].rx_start
= bus_addr (dev
->rxq
[pool
].out
.start
);
1700 a
.rec_struct
[pool
].rx_end
= bus_addr (dev
->rxq
[pool
].out
.limit
);
1701 a
.rec_struct
[pool
].buffer_size
= cpu_to_be32 (dev
->rxq
[pool
].buffer_size
);
1704 #ifdef AMB_NEW_MICROCODE
1705 // disable fast PLX prefetching
1709 // pass the structure
1710 wr_mem (dev
, offsetof(amb_mem
, doorbell
), virt_to_bus (&a
));
1712 // 2.2 second wait (must not touch doorbell during 2 second DMA test)
1714 // give the adapter another half second?
1716 while (rd_plain (dev
, offsetof(amb_mem
, doorbell
)))
1718 timeout
= msleep_interruptible(timeout
);
1720 PRINTD (DBG_INIT
|DBG_ERR
, "adapter init timed out");
1727 // get microcode version
1728 static void __devinit
amb_ucode_version (amb_dev
* dev
) {
1732 cmd
.request
= cpu_to_be32 (SRB_GET_VERSION
);
1733 while (command_do (dev
, &cmd
)) {
1734 set_current_state(TASK_UNINTERRUPTIBLE
);
1737 major
= be32_to_cpu (cmd
.args
.version
.major
);
1738 minor
= be32_to_cpu (cmd
.args
.version
.minor
);
1739 PRINTK (KERN_INFO
, "microcode version is %u.%u", major
, minor
);
1742 // get end station address
1743 static void __devinit
amb_esi (amb_dev
* dev
, u8
* esi
) {
1748 cmd
.request
= cpu_to_be32 (SRB_GET_BIA
);
1749 while (command_do (dev
, &cmd
)) {
1750 set_current_state(TASK_UNINTERRUPTIBLE
);
1753 lower4
= be32_to_cpu (cmd
.args
.bia
.lower4
);
1754 upper2
= be32_to_cpu (cmd
.args
.bia
.upper2
);
1755 PRINTD (DBG_LOAD
, "BIA: lower4: %08x, upper2 %04x", lower4
, upper2
);
1760 PRINTDB (DBG_INIT
, "ESI:");
1761 for (i
= 0; i
< ESI_LEN
; ++i
) {
1763 esi
[i
] = bitrev8(lower4
>>(8*i
));
1765 esi
[i
] = bitrev8(upper2
>>(8*(i
-4)));
1766 PRINTDM (DBG_INIT
, " %02x", esi
[i
]);
1769 PRINTDE (DBG_INIT
, "");
1775 static void fixup_plx_window (amb_dev
*dev
, loader_block
*lb
)
1777 // fix up the PLX-mapped window base address to match the block
1780 blb
= virt_to_bus(lb
);
1781 // the kernel stack had better not ever cross a 1Gb boundary!
1782 mapreg
= rd_plain (dev
, offsetof(amb_mem
, stuff
[10]));
1783 mapreg
&= ~onegigmask
;
1784 mapreg
|= blb
& onegigmask
;
1785 wr_plain (dev
, offsetof(amb_mem
, stuff
[10]), mapreg
);
1789 static int __devinit
amb_init (amb_dev
* dev
)
1795 if (amb_reset (dev
, 1)) {
1796 PRINTK (KERN_ERR
, "card reset failed!");
1798 fixup_plx_window (dev
, &lb
);
1800 if (get_loader_version (&lb
, dev
, &version
)) {
1801 PRINTK (KERN_INFO
, "failed to get loader version");
1803 PRINTK (KERN_INFO
, "loader version is %08x", version
);
1805 if (ucode_init (&lb
, dev
)) {
1806 PRINTK (KERN_ERR
, "microcode failure");
1807 } else if (create_queues (dev
, cmds
, txs
, rxs
, rxs_bs
)) {
1808 PRINTK (KERN_ERR
, "failed to get memory for queues");
1811 if (amb_talk (dev
)) {
1812 PRINTK (KERN_ERR
, "adapter did not accept queues");
1815 amb_ucode_version (dev
);
1820 destroy_queues (dev
);
1821 } /* create_queues, ucode_init */
1824 } /* get_loader_version */
1831 static void setup_dev(amb_dev
*dev
, struct pci_dev
*pci_dev
)
1835 // set up known dev items straight away
1836 dev
->pci_dev
= pci_dev
;
1837 pci_set_drvdata(pci_dev
, dev
);
1839 dev
->iobase
= pci_resource_start (pci_dev
, 1);
1840 dev
->irq
= pci_dev
->irq
;
1841 dev
->membase
= bus_to_virt(pci_resource_start(pci_dev
, 0));
1843 // flags (currently only dead)
1846 // Allocate cell rates (fibre)
1847 // ATM_OC3_PCR = 1555200000/8/270*260/53 - 29/53
1848 // to be really pedantic, this should be ATM_OC3c_PCR
1849 dev
->tx_avail
= ATM_OC3_PCR
;
1850 dev
->rx_avail
= ATM_OC3_PCR
;
1852 #ifdef FILL_RX_POOLS_IN_BH
1853 // initialise bottom half
1854 INIT_WORK(&dev
->bh
, (void (*)(void *)) fill_rx_pools
, dev
);
1857 // semaphore for txer/rxer modifications - we cannot use a
1858 // spinlock as the critical region needs to switch processes
1859 mutex_init(&dev
->vcc_sf
);
1860 // queue manipulation spinlocks; we want atomic reads and
1861 // writes to the queue descriptors (handles IRQ and SMP)
1862 // consider replacing "int pending" -> "atomic_t available"
1863 // => problem related to who gets to move queue pointers
1864 spin_lock_init (&dev
->cq
.lock
);
1865 spin_lock_init (&dev
->txq
.lock
);
1866 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
1867 spin_lock_init (&dev
->rxq
[pool
].lock
);
1870 static void setup_pci_dev(struct pci_dev
*pci_dev
)
1874 // enable bus master accesses
1875 pci_set_master(pci_dev
);
1877 // frobnicate latency (upwards, usually)
1878 pci_read_config_byte (pci_dev
, PCI_LATENCY_TIMER
, &lat
);
1881 pci_lat
= (lat
< MIN_PCI_LATENCY
) ? MIN_PCI_LATENCY
: lat
;
1883 if (lat
!= pci_lat
) {
1884 PRINTK (KERN_INFO
, "Changing PCI latency timer from %hu to %hu",
1886 pci_write_config_byte(pci_dev
, PCI_LATENCY_TIMER
, pci_lat
);
1890 static int __devinit
amb_probe(struct pci_dev
*pci_dev
, const struct pci_device_id
*pci_ent
)
1896 err
= pci_enable_device(pci_dev
);
1898 PRINTK (KERN_ERR
, "skipped broken (PLX rev 2) card");
1902 // read resources from PCI configuration space
1905 if (pci_dev
->device
== PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD
) {
1906 PRINTK (KERN_ERR
, "skipped broken (PLX rev 2) card");
1911 PRINTD (DBG_INFO
, "found Madge ATM adapter (amb) at"
1912 " IO %llx, IRQ %u, MEM %p",
1913 (unsigned long long)pci_resource_start(pci_dev
, 1),
1914 irq
, bus_to_virt(pci_resource_start(pci_dev
, 0)));
1917 err
= pci_request_region(pci_dev
, 1, DEV_LABEL
);
1919 PRINTK (KERN_ERR
, "IO range already in use!");
1923 dev
= kzalloc(sizeof(amb_dev
), GFP_KERNEL
);
1925 PRINTK (KERN_ERR
, "out of memory!");
1930 setup_dev(dev
, pci_dev
);
1932 err
= amb_init(dev
);
1934 PRINTK (KERN_ERR
, "adapter initialisation failure");
1938 setup_pci_dev(pci_dev
);
1940 // grab (but share) IRQ and install handler
1941 err
= request_irq(irq
, interrupt_handler
, IRQF_SHARED
, DEV_LABEL
, dev
);
1943 PRINTK (KERN_ERR
, "request IRQ failed!");
1947 dev
->atm_dev
= atm_dev_register (DEV_LABEL
, &amb_ops
, -1, NULL
);
1948 if (!dev
->atm_dev
) {
1949 PRINTD (DBG_ERR
, "failed to register Madge ATM adapter");
1954 PRINTD (DBG_INFO
, "registered Madge ATM adapter (no. %d) (%p) at %p",
1955 dev
->atm_dev
->number
, dev
, dev
->atm_dev
);
1956 dev
->atm_dev
->dev_data
= (void *) dev
;
1958 // register our address
1959 amb_esi (dev
, dev
->atm_dev
->esi
);
1961 // 0 bits for vpi, 10 bits for vci
1962 dev
->atm_dev
->ci_range
.vpi_bits
= NUM_VPI_BITS
;
1963 dev
->atm_dev
->ci_range
.vci_bits
= NUM_VCI_BITS
;
1965 init_timer(&dev
->housekeeping
);
1966 dev
->housekeeping
.function
= do_housekeeping
;
1967 dev
->housekeeping
.data
= (unsigned long) dev
;
1968 mod_timer(&dev
->housekeeping
, jiffies
);
1970 // enable host interrupts
1971 interrupts_on (dev
);
1983 pci_release_region(pci_dev
, 1);
1985 pci_disable_device(pci_dev
);
1990 static void __devexit
amb_remove_one(struct pci_dev
*pci_dev
)
1992 struct amb_dev
*dev
;
1994 dev
= pci_get_drvdata(pci_dev
);
1996 PRINTD(DBG_INFO
|DBG_INIT
, "closing %p (atm_dev = %p)", dev
, dev
->atm_dev
);
1997 del_timer_sync(&dev
->housekeeping
);
1998 // the drain should not be necessary
1999 drain_rx_pools(dev
);
2000 interrupts_off(dev
);
2002 free_irq(dev
->irq
, dev
);
2003 pci_disable_device(pci_dev
);
2004 destroy_queues(dev
);
2005 atm_dev_deregister(dev
->atm_dev
);
2007 pci_release_region(pci_dev
, 1);
2010 static void __init
amb_check_args (void) {
2012 unsigned int max_rx_size
;
2014 #ifdef DEBUG_AMBASSADOR
2015 PRINTK (KERN_NOTICE
, "debug bitmap is %hx", debug
&= DBG_MASK
);
2018 PRINTK (KERN_NOTICE
, "no debugging support");
2021 if (cmds
< MIN_QUEUE_SIZE
)
2022 PRINTK (KERN_NOTICE
, "cmds has been raised to %u",
2023 cmds
= MIN_QUEUE_SIZE
);
2025 if (txs
< MIN_QUEUE_SIZE
)
2026 PRINTK (KERN_NOTICE
, "txs has been raised to %u",
2027 txs
= MIN_QUEUE_SIZE
);
2029 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
2030 if (rxs
[pool
] < MIN_QUEUE_SIZE
)
2031 PRINTK (KERN_NOTICE
, "rxs[%hu] has been raised to %u",
2032 pool
, rxs
[pool
] = MIN_QUEUE_SIZE
);
2034 // buffers sizes should be greater than zero and strictly increasing
2036 for (pool
= 0; pool
< NUM_RX_POOLS
; ++pool
)
2037 if (rxs_bs
[pool
] <= max_rx_size
)
2038 PRINTK (KERN_NOTICE
, "useless pool (rxs_bs[%hu] = %u)",
2039 pool
, rxs_bs
[pool
]);
2041 max_rx_size
= rxs_bs
[pool
];
2043 if (rx_lats
< MIN_RX_BUFFERS
)
2044 PRINTK (KERN_NOTICE
, "rx_lats has been raised to %u",
2045 rx_lats
= MIN_RX_BUFFERS
);
2050 /********** module stuff **********/
2052 MODULE_AUTHOR(maintainer_string
);
2053 MODULE_DESCRIPTION(description_string
);
2054 MODULE_LICENSE("GPL");
2055 MODULE_FIRMWARE("atmsar11.fw");
2056 module_param(debug
, ushort
, 0644);
2057 module_param(cmds
, uint
, 0);
2058 module_param(txs
, uint
, 0);
2059 module_param_array(rxs
, uint
, NULL
, 0);
2060 module_param_array(rxs_bs
, uint
, NULL
, 0);
2061 module_param(rx_lats
, uint
, 0);
2062 module_param(pci_lat
, byte
, 0);
2063 MODULE_PARM_DESC(debug
, "debug bitmap, see .h file");
2064 MODULE_PARM_DESC(cmds
, "number of command queue entries");
2065 MODULE_PARM_DESC(txs
, "number of TX queue entries");
2066 MODULE_PARM_DESC(rxs
, "number of RX queue entries [" __MODULE_STRING(NUM_RX_POOLS
) "]");
2067 MODULE_PARM_DESC(rxs_bs
, "size of RX buffers [" __MODULE_STRING(NUM_RX_POOLS
) "]");
2068 MODULE_PARM_DESC(rx_lats
, "number of extra buffers to cope with RX latencies");
2069 MODULE_PARM_DESC(pci_lat
, "PCI latency in bus cycles");
2071 /********** module entry **********/
2073 static struct pci_device_id amb_pci_tbl
[] = {
2074 { PCI_VDEVICE(MADGE
, PCI_DEVICE_ID_MADGE_AMBASSADOR
), 0 },
2075 { PCI_VDEVICE(MADGE
, PCI_DEVICE_ID_MADGE_AMBASSADOR_BAD
), 0 },
2079 MODULE_DEVICE_TABLE(pci
, amb_pci_tbl
);
2081 static struct pci_driver amb_driver
= {
2084 .remove
= __devexit_p(amb_remove_one
),
2085 .id_table
= amb_pci_tbl
,
2088 static int __init
amb_module_init (void)
2090 PRINTD (DBG_FLOW
|DBG_INIT
, "init_module");
2092 // sanity check - cast needed as printk does not support %Zu
2093 if (sizeof(amb_mem
) != 4*16 + 4*12) {
2094 PRINTK (KERN_ERR
, "Fix amb_mem (is %lu words).",
2095 (unsigned long) sizeof(amb_mem
));
2104 return pci_register_driver(&amb_driver
);
2107 /********** module exit **********/
2109 static void __exit
amb_module_exit (void)
2111 PRINTD (DBG_FLOW
|DBG_INIT
, "cleanup_module");
2113 pci_unregister_driver(&amb_driver
);
2116 module_init(amb_module_init
);
2117 module_exit(amb_module_exit
);