25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/pci.h>
28 #include <linux/kernel.h>
31 #include <linux/atmdev.h>
34 #include <linux/poison.h>
39 #include <linux/slab.h>
43 #include <asm/byteorder.h>
48 #define description_string "Madge ATM Ambassador driver"
49 #define version_string "1.2.4"
51 static inline void __init show_version (
void) {
296 static void do_housekeeping (
unsigned long arg);
299 static unsigned short debug = 0;
300 static unsigned int cmds = 8;
301 static unsigned int txs = 32;
302 static unsigned int rxs[
NUM_RX_POOLS] = { 64, 64, 64, 64 };
303 static unsigned int rxs_bs[
NUM_RX_POOLS] = { 4080, 12240, 36720, 65535 };
304 static unsigned int rx_lats = 7;
305 static unsigned char pci_lat = 0;
307 static const unsigned long onegigmask = -1 << 30;
312 PRINTD (DBG_FLOW|DBG_REGS,
"wr: %08zx <- %08x", addr, data);
326 PRINTD (DBG_FLOW|DBG_REGS,
"rd: %08zx -> %08x", addr, data);
332 PRINTD (DBG_FLOW|DBG_REGS,
"wr: %08zx <- %08x b[%08x]", addr, data, be);
347 PRINTD (DBG_FLOW|DBG_REGS,
"rd: %08zx -> %08x b[%08x]", addr, data, be);
353 static inline void dump_registers (
const amb_dev *
dev) {
354 #ifdef DEBUG_AMBASSADOR
355 if (
debug & DBG_REGS) {
357 PRINTD (DBG_REGS,
"reading PLX control: ");
358 for (i = 0x00; i < 0x30; i +=
sizeof(
u32))
360 PRINTD (DBG_REGS,
"reading mailboxes: ");
361 for (i = 0x40; i < 0x60; i +=
sizeof(
u32))
363 PRINTD (DBG_REGS,
"reading doorb irqev irqen reset:");
364 for (i = 0x60; i < 0x70; i +=
sizeof(
u32))
373 static inline void dump_loader_block (
volatile loader_block * lb) {
374 #ifdef DEBUG_AMBASSADOR
376 PRINTDB (DBG_LOAD,
"lb @ %p; res: %d, cmd: %d, pay:",
387 static inline void dump_command (
command *
cmd) {
388 #ifdef DEBUG_AMBASSADOR
392 for (i = 0; i < 3; ++
i)
402 #ifdef DEBUG_AMBASSADOR
405 PRINTDB (DBG_DATA,
"%s(%u) ", prefix, vc);
406 for (i=0; i<skb->
len && i < 256;i++)
407 PRINTDM (DBG_DATA,
"%02x ", data[i]);
421 static int check_area (
void *
start,
size_t length) {
423 const u32 fourmegmask = -1 << 22;
424 const u32 twofivesixmask = -1 << 8;
425 const u32 starthole = 0xE0000000;
427 u32 lastaddress = startaddress+length-1;
428 if ((startaddress ^ lastaddress) & fourmegmask ||
429 (startaddress & twofivesixmask) == starthole) {
430 PRINTK (
KERN_ERR,
"check_area failure: [%x,%x] - mail maintainer!",
431 startaddress, lastaddress);
440 static void amb_kfree_skb (
struct sk_buff * skb) {
478 PRINTD (DBG_FLOW|
DBG_RX,
"rx_complete %p %p (len=%hu)", dev, rx, rx_len);
487 if (rx_len <= atm_vcc->
qos.rxtp.max_sdu) {
495 dump_skb (
"<<<", vc, skb);
499 __net_timestamp(skb);
501 atm_vcc->
push (atm_vcc, skb);
566 spin_lock (&cq->
lock);
586 spin_unlock (&cq->
lock);
594 while (ptrs->
out != my_slot) {
609 spin_lock (&cq->
lock);
614 spin_unlock (&cq->
lock);
619 spin_unlock (&cq->
lock);
643 txq->
in.ptr =
NEXTQ (txq->
in.ptr, txq->
in.start, txq->
in.limit);
650 spin_unlock_irqrestore (&txq->
lock, flags);
654 spin_unlock_irqrestore (&txq->
lock, flags);
659 static int tx_take (
amb_dev * dev) {
669 tx_complete (dev, txq->
out.ptr);
671 txq->
out.ptr->handle = 0;
676 spin_unlock_irqrestore (&txq->
lock, flags);
680 spin_unlock_irqrestore (&txq->
lock, flags);
700 rxq->
in.ptr =
NEXTQ (rxq->
in.ptr, rxq->
in.start, rxq->
in.limit);
704 spin_unlock_irqrestore (&rxq->
lock, flags);
707 spin_unlock_irqrestore (&rxq->
lock, flags);
712 static int rx_take (
amb_dev * dev,
unsigned char pool) {
720 if (rxq->
pending && (rxq->
out.ptr->status || rxq->
out.ptr->length)) {
722 rx_complete (dev, rxq->
out.ptr);
724 rxq->
out.ptr->status = 0;
725 rxq->
out.ptr->length = 0;
732 spin_unlock_irqrestore (&rxq->
lock, flags);
737 spin_unlock_irqrestore (&rxq->
lock, flags);
745 static void drain_rx_pool (
amb_dev * dev,
unsigned char pool) {
748 PRINTD (DBG_FLOW|DBG_POOL,
"drain_rx_pool %p %hu", dev, pool);
760 while (command_do (dev, &cmd))
764 if (rx_take (dev, pool))
771 static void drain_rx_pools (
amb_dev * dev) {
774 PRINTD (DBG_FLOW|DBG_POOL,
"drain_rx_pools %p", dev);
777 drain_rx_pool (dev, pool);
780 static void fill_rx_pool (
amb_dev * dev,
unsigned char pool,
786 PRINTD (DBG_FLOW|DBG_POOL,
"fill_rx_pool %p %hu %x", dev, pool, priority);
791 rxq = &dev->
rxq[pool];
796 PRINTD (
DBG_SKB|DBG_POOL,
"failed to allocate skb for RX pool %hu", pool);
805 skb, skb->
head, (
long) skb_end_offset(skb));
808 if (rx_give (dev, &rx, pool))
817 static void fill_rx_pools (
amb_dev * dev) {
820 PRINTD (DBG_FLOW|DBG_POOL,
"fill_rx_pools %p", dev);
830 static void interrupts_on (
amb_dev * dev) {
838 static void interrupts_off (
amb_dev * dev) {
869 while (!rx_take (dev, pool))
871 while (!tx_take (dev))
883 PRINTD (
DBG_IRQ|DBG_FLOW,
"interrupt_handler done: %p", dev_id);
890 u16 *
bits,
unsigned int * actual) {
891 unsigned char exp = -1;
892 unsigned int man = -1;
894 PRINTD (DBG_FLOW|DBG_QOS,
"make_rate %u", rate);
910 if (rate > 0xffc00000U) {
927 while (!(man & (1<<31))) {
953 if (man & (~0
U>>9)) {
954 man = (man>>(32-9)) + 1;
967 if (man & (1<<(32-9-1))) {
968 man = (man>>(32-9)) + 1;
993 PRINTD (DBG_QOS,
"rate: man=%u, exp=%hu", man, exp);
996 *bits = (exp<<9) | man;
1000 ? (1 << exp) + (man << (exp-9))
1001 : (1 <<
exp) + ((man + (1<<(9-exp-1))) >> (9-
exp));
1013 static int amb_open (
struct atm_vcc * atm_vcc)
1020 u16 tx_rate_bits = -1;
1021 u16 tx_vc_bits = -1;
1022 u16 tx_frame_bits = -1;
1026 unsigned char pool = -1;
1027 short vpi = atm_vcc->
vpi;
1028 int vci = atm_vcc->
vci;
1030 PRINTD (DBG_FLOW|DBG_VCC,
"amb_open %x %x", vpi, vci);
1032 #ifdef ATM_VPI_UNSPEC
1042 PRINTD (
DBG_WARN|DBG_VCC,
"VPI/VCI out of range: %hd/%d", vpi, vci);
1046 qos = &atm_vcc->
qos;
1049 PRINTD (DBG_QOS,
"AAL not supported");
1075 error = make_rate (pcr, r, &tx_rate_bits,
NULL);
1086 PRINTD (DBG_QOS,
"pcr goal = %d", pcr);
1092 PRINTD (DBG_QOS,
"request for non-UBR denied");
1096 PRINTD (DBG_QOS,
"tx_rate_bits=%hx, tx_vc_bits=%hx",
1097 tx_rate_bits, tx_vc_bits);
1108 PRINTD (DBG_VCC|DBG_QOS|DBG_POOL,
"chose pool %hu (max_sdu %u <= %u)",
1112 if (pool == NUM_RX_POOLS) {
1114 "no pool suitable for VC (RX max_sdu %d is too large)",
1126 PRINTD (DBG_QOS,
"pcr goal = %d", pcr);
1132 PRINTD (DBG_QOS,
"request for non-UBR denied");
1158 if (dev->
rxer[vci]) {
1163 while (command_do (dev, &cmd))
1171 while (command_do (dev, &cmd))
1179 while (command_do (dev, &cmd))
1213 while (command_do (dev, &cmd))
1216 dev->
rxer[vci] = atm_vcc;
1228 static void amb_close (
struct atm_vcc * atm_vcc) {
1233 PRINTD (DBG_VCC|DBG_FLOW,
"amb_close");
1239 if (atm_vcc->
qos.txtp.traffic_class !=
ATM_NONE) {
1243 if (dev->
rxer[vci]) {
1255 while (command_do (dev, &cmd))
1261 if (atm_vcc->
qos.rxtp.traffic_class !=
ATM_NONE) {
1280 if (atm_vcc != dev->
rxer[vci])
1282 "arghhh! we're going to die!",
1283 vcc, dev->
rxer[vci]);
1285 while (command_do (dev, &cmd))
1292 drain_rx_pool (dev, pool);
1308 static int amb_send (
struct atm_vcc * atm_vcc,
struct sk_buff * skb) {
1312 unsigned int tx_len = skb->
len;
1320 PRINTD (DBG_FLOW|
DBG_TX,
"amb_send vc %x data %p len %u",
1321 vc, tx_data, tx_len);
1323 dump_skb (
">>>", vc, skb);
1335 if (skb->
len > (
size_t) atm_vcc->
qos.txtp.max_sdu) {
1336 PRINTK (
KERN_ERR,
"sk_buff length greater than agreed max_sdu, dropping...");
1340 if (check_area (skb->
data, skb->
len)) {
1351 if (check_area (tx_descr,
sizeof(
tx_simple))) {
1355 PRINTD (
DBG_TX,
"fragment list allocated at %p", tx_descr);
1366 #ifdef AMB_NEW_MICROCODE
1376 while (tx_give (dev, &tx))
1388 static void amb_free_rx_skb (
struct atm_vcc * atm_vcc,
struct sk_buff * skb) {
1399 PRINTD (DBG_FLOW|
DBG_SKB,
"amb_rx_free skb %p (atm_vcc %p, vcc %p)",
1409 if (!rx_give (dev, &rx, pool)) {
1411 PRINTD (
DBG_SKB|DBG_POOL,
"recycled skb for pool %hu", pool);
1429 PRINTD (DBG_FLOW,
"amb_proc_read");
1436 "frames: TX OK %lu, RX OK %lu, RX bad %lu "
1437 "(CRC %lu, long %lu, aborted %lu, unused %lu).\n",
1439 s->
rx.badcrc, s->
rx.toolong,
1440 s->
rx.aborted, s->
rx.unused);
1445 return sprintf (page,
"cmd queue [cur/hi/max]: %u/%u/%u. ",
1451 return sprintf (page,
"TX queue [cur/max high full]: %u/%u %u %u.\n",
1456 unsigned int count =
sprintf (page,
"RX queues [cur/max/req low empty]:");
1459 count +=
sprintf (page+count,
" %u/%u/%u %u %u",
1462 count +=
sprintf (page+count,
".\n");
1467 unsigned int count =
sprintf (page,
"RX buffer sizes:");
1472 count +=
sprintf (page+count,
".\n");
1491 .proc_read = amb_proc_read,
1496 static void do_housekeeping (
unsigned long arg) {
1502 fill_rx_pools (dev);
1511 unsigned int txs,
unsigned int * rxs,
1512 unsigned int * rx_buffer_sizes) {
1518 PRINTD (DBG_FLOW,
"create_queues %p", dev);
1520 total += cmds *
sizeof(
command);
1525 total += rxs[pool] * (
sizeof(
rx_in) +
sizeof(
rx_out));
1532 if (check_area (memory, total)) {
1538 limit = memory + total;
1573 txq->
in.limit = in + txs;
1575 memory = txq->
in.limit;
1580 txq->
out.limit = out + txs;
1582 memory = txq->
out.limit;
1596 rxq->
low = rxs[pool] - 1;
1602 rxq->
in.limit = in + rxs[pool];
1604 memory = rxq->
in.limit;
1609 rxq->
out.limit = out + rxs[pool];
1611 memory = rxq->
out.limit;
1614 if (memory == limit) {
1617 PRINTK (
KERN_ERR,
"bad queue alloc %p != %p (tell maintainer)", memory, limit);
1618 kfree (limit - total);
1626 static void destroy_queues (
amb_dev * dev) {
1631 PRINTD (DBG_FLOW,
"destroy_queues %p", dev);
1641 static unsigned int command_timeouts [] = {
1656 static unsigned int command_successes [] = {
1675 if (result == command_successes[cmd])
1681 msg =
"bad command";
1685 msg =
"command in progress";
1689 msg =
"command passed test";
1693 msg =
"command failed test";
1697 msg =
"command read data ok";
1701 msg =
"command read bad address";
1705 msg =
"command write data ok";
1709 msg =
"command write bad address";
1713 msg =
"command write flash failure";
1717 msg =
"command complete";
1721 msg =
"command flash erase failure";
1725 msg =
"command write bad data";
1729 msg =
"unknown error";
1731 "decode_loader_result got %d=%x !",
1745 PRINTD (DBG_FLOW|DBG_LOAD,
"do_loader_command");
1763 timeout = command_timeouts[
cmd] * 10;
1770 dump_registers (dev);
1771 dump_loader_block (lb);
1782 PRINTD (DBG_LOAD|
DBG_ERR,
"start command did not clear doorbell, res=%08x",
1784 dump_registers (dev);
1800 PRINTD (DBG_FLOW|DBG_LOAD,
"get_loader_version");
1817 PRINTD (DBG_FLOW|DBG_LOAD,
"loader_write");
1833 PRINTD (DBG_FLOW|DBG_LOAD,
"loader_verify");
1847 PRINTD (DBG_FLOW|DBG_LOAD,
"loader_start");
1855 static inline void sf (
const char * msg)
1860 static int amb_reset (
amb_dev * dev,
int diags) {
1863 PRINTD (DBG_FLOW|DBG_LOAD,
"amb_reset");
1882 unsigned long timeout;
1902 sf (
"SUNI data pattern");
1904 sf (
"SUNI data bits");
1906 sf (
"SUNI UTOPIA interface");
1908 sf (
"SUNI cell buffer FIFO");
1923 unsigned long start_address;
1925 const char *errmsg = 0;
1928 res = request_ihex_firmware(&fw,
"atmsar11.fw", &dev->
pci_dev->dev);
1937 errmsg =
"no start record";
1942 rec = ihex_next_binrec(rec);
1944 PRINTD (DBG_FLOW|DBG_LOAD,
"ucode_init");
1950 errmsg =
"record too long";
1954 errmsg =
"odd number of bytes";
1957 res = loader_write(lb, dev, rec);
1961 res = loader_verify(lb, dev, rec);
1964 rec = ihex_next_binrec(rec);
1968 res = loader_start(lb, dev, start_address);
1986 unsigned long timeout;
1988 PRINTD (DBG_FLOW,
"amb_talk %p", dev);
2006 #ifdef AMB_NEW_MICROCODE
2035 while (command_do (dev, &cmd)) {
2051 while (command_do (dev, &cmd)) {
2057 PRINTD (DBG_LOAD,
"BIA: lower4: %08x, upper2 %04x", lower4, upper2);
2065 esi[
i] = bitrev8(lower4>>(8*i));
2067 esi[
i] = bitrev8(upper2>>(8*(i-4)));
2085 mapreg &= ~onegigmask;
2086 mapreg |= blb & onegigmask;
2097 if (amb_reset (dev, 1)) {
2100 fixup_plx_window (dev, &lb);
2102 if (get_loader_version (&lb, dev, &version)) {
2107 if (ucode_init (&lb, dev)) {
2109 }
else if (create_queues (dev, cmds, txs, rxs, rxs_bs)) {
2113 if (amb_talk (dev)) {
2117 amb_ucode_version (dev);
2122 destroy_queues (dev);
2139 pci_set_drvdata(pci_dev, dev);
2167 static void setup_pci_dev(
struct pci_dev *pci_dev)
2180 if (lat != pci_lat) {
2209 " IO %llx, IRQ %u, MEM %p",
2227 setup_dev(dev, pci_dev);
2229 err = amb_init(dev);
2235 setup_pci_dev(pci_dev);
2252 PRINTD (
DBG_INFO,
"registered Madge ATM adapter (no. %d) (%p) at %p",
2254 dev->
atm_dev->dev_data = (
void *) dev;
2257 amb_esi (dev, dev->
atm_dev->esi);
2269 interrupts_on (dev);
2288 static void __devexit amb_remove_one(
struct pci_dev *pci_dev)
2292 dev = pci_get_drvdata(pci_dev);
2297 drain_rx_pools(dev);
2298 interrupts_off(dev);
2302 destroy_queues(dev);
2308 static void __init amb_check_args (
void) {
2310 unsigned int max_rx_size;
2312 #ifdef DEBUG_AMBASSADOR
2335 if (rxs_bs[pool] <= max_rx_size)
2337 pool, rxs_bs[pool]);
2339 max_rx_size = rxs_bs[pool];
2366 MODULE_PARM_DESC(rx_lats,
"number of extra buffers to cope with RX latencies");
2383 .id_table = amb_pci_tbl,
2386 static int __init amb_module_init (
void)
2391 if (
sizeof(
amb_mem) != 4*16 + 4*12) {
2393 (
unsigned long)
sizeof(
amb_mem));
2402 return pci_register_driver(&amb_driver);
2407 static void __exit amb_module_exit (
void)