28 #include <linux/module.h>
29 #include <linux/kernel.h>
31 #include <linux/pci.h>
32 #include <linux/errno.h>
33 #include <linux/atm.h>
34 #include <linux/atmdev.h>
35 #include <linux/sonet.h>
37 #include <linux/time.h>
39 #include <linux/uio.h>
43 #include <linux/wait.h>
44 #include <linux/slab.h>
48 #include <asm/uaccess.h>
49 #include <asm/string.h>
50 #include <asm/byteorder.h>
55 #define description_string "Madge ATM Horizon [Ultra] driver"
56 #define version_string "1.2.1"
58 static inline void __init show_version (
void) {
359 static void do_housekeeping (
unsigned long arg);
361 static unsigned short debug = 0;
362 static unsigned short vpi_bits = 0;
363 static int max_tx_size = 9000;
364 static int max_rx_size = 9000;
365 static unsigned char pci_lat = 0;
378 static inline void wr_regw (
const hrz_dev * dev,
unsigned char reg,
u16 data) {
382 static inline u16 rd_regw (
const hrz_dev * dev,
unsigned char reg) {
386 static inline void wrs_regb (
const hrz_dev * dev,
unsigned char reg,
void *
addr,
u32 len) {
390 static inline void rds_regb (
const hrz_dev * dev,
unsigned char reg,
void *
addr,
u32 len) {
409 static inline void wr_framer (
const hrz_dev * dev,
u32 addr,
u32 data) {
414 static inline u32 rd_framer (
const hrz_dev * dev,
u32 addr) {
428 static void WAIT_FLUSH_RX_COMPLETE (
hrz_dev * dev) {
439 static void WAIT_UPDATE_COMPLETE (
hrz_dev * dev) {
447 static inline void SELECT_TX_CHANNEL (
hrz_dev * dev,
u16 tx_channel) {
472 unsigned char * data = skb->
data;
473 PRINTDB (DBG_DATA,
"%s(%u) ", prefix, vc);
474 for (i=0; i<skb->
len && i < 256;i++)
475 PRINTDM (DBG_DATA,
"%02x ", data[i]);
485 static inline void dump_regs (
hrz_dev * dev) {
499 static inline void dump_framer (
hrz_dev * dev) {
502 PRINTDB (DBG_REGS,
"framer registers:");
503 for (i = 0; i < 0x10; ++
i)
504 PRINTDM (DBG_REGS,
" %02x", rd_framer (dev, i));
516 static inline int channel_to_vpivci (
const u16 channel,
short * vpi,
int * vci) {
517 unsigned short vci_bits = 10 - vpi_bits;
519 *vci = channel & ((~0)<<vci_bits);
520 *vpi = channel >> vci_bits;
521 return channel ? 0 : -
EINVAL;
526 static inline int vpivci_to_channel (
u16 * channel,
const short vpi,
const int vci) {
527 unsigned short vci_bits = 10 - vpi_bits;
528 if (0 <= vpi && vpi < 1<<vpi_bits && 0 <= vci && vci < 1<<vci_bits) {
529 *channel = vpi<<vci_bits | vci;
530 return *channel ? 0 : -
EINVAL;
537 static inline u16 rx_q_entry_to_length (
u32 x) {
541 static inline u16 rx_q_entry_to_rx_channel (
u32 x) {
591 #define BR_UKN 1031250l
592 #define BR_HRZ 4000000l
593 #define BR_ULT 5000000l
603 u16 *
bits,
unsigned int * actual)
615 unsigned long br_man = br;
616 unsigned int br_exp = 0;
618 PRINTD (DBG_QOS|DBG_FLOW,
"make_rate b=%lu, c=%u, %s", br, c,
628 br_man = br_man >> 1;
656 PRINTD (DBG_QOS,
"A: p=%u, d=%u", pre, div);
664 if (br_man <= (c << (
CR_MAXPEXP+div-br_exp))) {
680 PRINTD (DBG_QOS,
"B: p=%u, d=%u", pre, div);
691 PRINTD (DBG_QOS,
"C: p=%u, d=%u", pre, div);
695 PRINTD (DBG_QOS,
"set_cr internal failure: d=%u p=%u",
703 PRINTD (DBG_QOS,
"actual rate: %u", *actual);
710 u16 * bit_pattern,
unsigned int * actual) {
711 unsigned int my_actual;
713 PRINTD (DBG_QOS|DBG_FLOW,
"make_rate_with_tolerance c=%u, %s, tol=%u",
724 if (c - tol <= *actual && *actual <= c + tol)
729 return make_rate (dev, c, r, bit_pattern, actual);
734 static int hrz_open_rx (
hrz_dev * dev,
u16 channel) {
745 PRINTD (DBG_FLOW,
"hrz_open_rx %x", channel);
749 spin_unlock_irqrestore (&dev->
mem_lock, flags);
760 PRINTD (DBG_VCC,
"using a spare buffer: %u", buf_ptr);
764 PRINTD (
DBG_ERR|DBG_VCC,
"bad spare buffer pointer, using IDLE");
768 PRINTD (DBG_VCC,
"using IDLE buffer pointer");
781 spin_unlock_irqrestore (&dev->
mem_lock, flags);
785 PRINTD (DBG_FLOW,
"hrz_open_rx ok");
793 static void hrz_change_vc_qos (ATM_RXER * rxer, MAAL_QOS * qos) {
794 rxer->rate = make_rate (qos->peak_cells);
800 static void hrz_kfree_skb (
struct sk_buff * skb) {
810 static void hrz_close_rx (
hrz_dev * dev,
u16 vc) {
823 spin_unlock_irqrestore (&dev->
mem_lock, flags);
827 PRINTD (DBG_VCC,
"closing VC: RX channel %u already disabled", vc);
845 spin_unlock_irqrestore (&dev->
mem_lock, flags);
849 WAIT_FLUSH_RX_COMPLETE(dev);
879 SELECT_RX_CHANNEL (dev, other);
880 WAIT_UPDATE_COMPLETE (dev);
887 SELECT_RX_CHANNEL (dev, vc);
888 WAIT_UPDATE_COMPLETE (dev);
892 FLUSH_RX_CHANNEL (dev, vc);
893 WAIT_FLUSH_RX_COMPLETE (dev);
897 SELECT_RX_CHANNEL (dev, other);
898 WAIT_UPDATE_COMPLETE (dev);
915 PRINTD (DBG_VCC|
DBG_RX,
"rd_ptr = %u, wr_ptr = %u", rd_ptr, wr_ptr);
917 while (rd_ptr != wr_ptr) {
918 u32 x = rd_mem (dev, (
HDW *) rd_ptr);
920 if (vc == rx_q_entry_to_rx_channel (x)) {
925 wr_mem (dev, (
HDW *) rd_ptr, x);
936 spin_unlock_irqrestore (&dev->
mem_lock, flags);
949 static void rx_schedule (
hrz_dev * dev,
int irq) {
953 #ifndef TAILRECURSIONWORKS
955 while (pio_instead) {
964 if (++spin_count > 10) {
968 hrz_kfree_skb (dev->
rx_skb);
996 unsigned int rx_regions = dev->rx_regions;
998 unsigned int rx_regions = 0;
1004 dev->
rx_addr = dev->rx_iovec->iov_base;
1005 rx_bytes = dev->rx_iovec->iov_len;
1007 dev->rx_regions = rx_regions - 1;
1038 __net_timestamp(skb);
1040 vcc->
push (vcc, skb);
1065 #ifdef TAILRECURSIONWORKS
1068 return rx_schedule (dev, 0);
1080 static void rx_bus_master_complete_handler (
hrz_dev * dev) {
1082 rx_schedule (dev, 1);
1093 static int tx_hold (
hrz_dev * dev) {
1105 static inline void tx_release (
hrz_dev * dev) {
1113 static void tx_schedule (
hrz_dev *
const dev,
int irq) {
1116 int append_desc = 0;
1118 int pio_instead = 0;
1119 #ifndef TAILRECURSIONWORKS
1121 while (pio_instead) {
1130 if (++spin_count > 10) {
1134 hrz_kfree_skb (dev->
tx_skb);
1192 hrz_kfree_skb (skb);
1221 #ifdef TAILRECURSIONWORKS
1224 return tx_schedule (dev, 0);
1236 static void tx_bus_master_complete_handler (
hrz_dev * dev) {
1238 tx_schedule (dev, 1);
1250 static u32 rx_queue_entry_next (
hrz_dev * dev) {
1260 return rx_queue_entry;
1265 static inline void rx_disabled_handler (
hrz_dev * dev) {
1274 static void rx_data_av_handler (
hrz_dev * dev) {
1276 u32 rx_queue_entry_flags;
1280 PRINTD (DBG_FLOW,
"hrz_data_av_handler");
1292 rx_queue_entry = rx_queue_entry_next (dev);
1294 rx_len = rx_q_entry_to_length (rx_queue_entry);
1295 rx_channel = rx_q_entry_to_rx_channel (rx_queue_entry);
1297 WAIT_FLUSH_RX_COMPLETE (dev);
1299 SELECT_RX_CHANNEL (dev, rx_channel);
1301 PRINTD (
DBG_RX,
"rx_queue_entry is: %#x", rx_queue_entry);
1317 PRINTD (
DBG_RX,
"got a frame on rx_channel %x len %u", rx_channel, rx_len);
1319 atm_vcc = dev->
rxer[rx_channel];
1325 if (atm_vcc->
qos.rxtp.traffic_class !=
ATM_NONE) {
1327 if (rx_len <= atm_vcc->
qos.rxtp.max_sdu) {
1345 PRINTD (
DBG_RX,
"RX start simple transfer (addr %p, len %d)",
1349 rx_schedule (dev, 0);
1378 FLUSH_RX_CHANNEL (dev,rx_channel);
1390 unsigned int irq_ok;
1392 PRINTD (DBG_FLOW,
"interrupt_handler: %p", dev_id);
1417 rx_bus_master_complete_handler (dev);
1422 tx_bus_master_complete_handler (dev);
1427 rx_data_av_handler (dev);
1436 PRINTD (
DBG_IRQ|DBG_FLOW,
"interrupt_handler done: %p", dev_id);
1444 static void do_housekeeping (
unsigned long arg) {
1463 unsigned short idle_channels;
1464 short tx_channel = -1;
1465 unsigned int spin_count;
1466 PRINTD (DBG_FLOW|
DBG_TX,
"setup_idle_tx_channel %p", dev);
1474 if (++spin_count > 100) {
1486 while (keep_going) {
1487 if (idle_channels & (1<<chan)) {
1509 unsigned long flags;
1531 vcc->tx_bucket_bits);
1535 vcc->tx_bucket_bits);
1544 if (rd_ptr != wr_ptr) {
1549 PRINTD (
DBG_TX,
"TX buffer pointers are: rd %x, wr %x.",
1578 spin_unlock_irqrestore (&dev->
mem_lock, flags);
1586 static int hrz_send (
struct atm_vcc * atm_vcc,
struct sk_buff * skb) {
1587 unsigned int spin_count;
1593 u32 buffers_required;
1598 PRINTD (DBG_FLOW|
DBG_TX,
"hrz_send vc %x data %p len %u",
1599 channel, skb->
data, skb->
len);
1601 dump_skb (
">>>", channel, skb);
1603 if (atm_vcc->
qos.txtp.traffic_class ==
ATM_NONE) {
1605 hrz_kfree_skb (skb);
1612 if (skb->
len > atm_vcc->
qos.txtp.max_sdu) {
1613 PRINTK (
KERN_ERR,
"sk_buff length greater than agreed max_sdu, dropping...");
1614 hrz_kfree_skb (skb);
1620 hrz_kfree_skb (skb);
1631 status &= ~PCI_STATUS_REC_MASTER_ABORT;
1634 hrz_kfree_skb (dev->
tx_skb);
1641 #ifdef DEBUG_HORIZON
1643 if (channel == 1023) {
1645 unsigned short d = 0;
1646 char *
s = skb->
data;
1648 for (i = 0; i < 4; ++
i)
1656 if (tx_hold (dev)) {
1657 hrz_kfree_skb (skb);
1671 PRINTD (
DBG_TX,
"waiting for free TX buffers, got %d of %d",
1672 free_buffers, buffers_required);
1677 if (++spin_count > 1000) {
1679 free_buffers, buffers_required);
1681 hrz_kfree_skb (skb);
1687 if (channel == dev->
last_vc) {
1693 for (tx_channel = 0; tx_channel <
TX_CHANS; ++tx_channel)
1698 if (tx_channel == TX_CHANS) {
1701 tx_channel = setup_idle_tx_channel (dev, vcc);
1702 if (tx_channel < 0) {
1710 SELECT_TX_CHANNEL(dev, tx_channel);
1723 unsigned int tx_len = skb->
len;
1724 unsigned int tx_iovcnt = skb_shinfo(skb)->nr_frags;
1736 hrz_kfree_skb (skb);
1749 tx_schedule (dev, 0);
1758 static void hrz_reset (
const hrz_dev * dev) {
1765 while (control_0_reg & RESET_HORIZON)
1796 const unsigned int addr_bits = 6;
1797 const unsigned int data_bits = 16;
1804 WRITE_IT_WAIT(dev, ctrl);
1808 CLOCK_IT(dev, ctrl);
1811 CLOCK_IT(dev, ctrl);
1814 CLOCK_IT(dev, ctrl);
1816 for (i=0; i<addr_bits; i++) {
1817 if (addr & (1 << (addr_bits-1)))
1822 CLOCK_IT(dev, ctrl);
1831 for (i=0;i<data_bits;i++) {
1834 CLOCK_IT(dev, ctrl);
1837 res |= (1 << (data_bits-1));
1841 WRITE_IT_WAIT(dev, ctrl);
1879 printk (
" clearing memory");
1882 wr_mem (dev, mem, 0);
1892 for (chan = 0; chan <
TX_CHANS; ++chan) {
1912 for (buff_count = 0; buff_count <
BUFN3_SIZE-1; buff_count++) {
1927 for (chan = 0; chan <
RX_CHANS; ++chan) {
1941 for (buff_count = 0; buff_count <
BUFN4_SIZE-1; buff_count++) {
1994 if (rd_framer (dev, 0) & 0x00f0) {
1999 wr_framer (dev, 0x00, 0x0080);
2000 wr_framer (dev, 0x00, 0x0000);
2003 wr_framer (dev, 0x63, rd_framer (dev, 0x63) | 0x0002);
2006 wr_framer (dev, 0x05, rd_framer (dev, 0x05) | 0x0001);
2012 wr_framer (dev, 0, rd_framer (dev, 0) | 0x0001);
2013 wr_framer (dev, 0, rd_framer (dev, 0) &~ 0x0001);
2016 wr_framer (dev, 0, 0x0002);
2019 wr_framer (dev, 2, 0x0B80);
2049 b = read_bia (dev, i/2 + 2);
2068 static int check_max_sdu (
hrz_aal aal,
struct atm_trafprm * tp,
unsigned int max_frame_size) {
2069 PRINTD (DBG_FLOW|DBG_QOS,
"check_max_sdu");
2074 PRINTD (DBG_QOS,
"defaulting max_sdu");
2083 PRINTD (DBG_QOS,
"%sing max_sdu", tp->
max_sdu ?
"capp" :
"default");
2089 PRINTD (DBG_QOS,
"%sing max_sdu", tp->
max_sdu ?
"capp" :
"default");
2100 static int atm_pcr_check (
struct atm_trafprm * tp,
unsigned int pcr) {
2103 PRINTD (DBG_QOS,
"luser gave min_pcr = ATM_MAX_PCR");
2105 PRINTD (DBG_QOS,
"luser gave negative min_pcr");
2107 PRINTD (DBG_QOS,
"pcr less than min_pcr");
2113 PRINTD (DBG_QOS,
"luser gave max_pcr = ATM_MAX_PCR");
2115 PRINTD (DBG_QOS,
"luser gave negative max_pcr");
2117 PRINTD (DBG_QOS,
"pcr greater than max_pcr");
2120 PRINTD (DBG_QOS,
"xBR(pcr) OK");
2123 PRINTD (DBG_QOS,
"pcr=%u, tp: min_pcr=%d, pcr=%d, max_pcr=%d",
2130 static int hrz_open (
struct atm_vcc *atm_vcc)
2142 short vpi = atm_vcc->
vpi;
2143 int vci = atm_vcc->
vci;
2144 PRINTD (DBG_FLOW|DBG_VCC,
"hrz_open %x %x", vpi, vci);
2146 #ifdef ATM_VPI_UNSPEC
2154 error = vpivci_to_channel (&channel, vpi, vci);
2156 PRINTD (
DBG_WARN|DBG_VCC,
"VPI/VCI out of range: %hd/%d", vpi, vci);
2164 qos = &atm_vcc->
qos;
2170 PRINTD (DBG_QOS|DBG_VCC,
"AAL0");
2175 PRINTD (DBG_QOS|DBG_VCC,
"AAL3/4");
2179 PRINTD (DBG_QOS|DBG_VCC,
"AAL5");
2183 PRINTD (DBG_QOS|DBG_VCC,
"Bad AAL!");
2232 vcc.tx_bucket_bits = 0;
2236 error = check_max_sdu (vcc.
aal, txtp, max_tx_size);
2238 PRINTD (DBG_QOS,
"TX max_sdu check failed");
2272 PRINTD (DBG_QOS,
"snatching all remaining TX bandwidth");
2274 }
else if (pcr < 0) {
2280 error = make_rate_with_tolerance (dev, pcr, r, 10,
2283 PRINTD (DBG_QOS,
"could not make rate from TX PCR");
2287 error = atm_pcr_check (txtp, vcc.
tx_rate);
2289 PRINTD (DBG_QOS,
"TX PCR failed consistency check");
2300 unsigned int mbs = 60;
2303 unsigned int bucket;
2307 }
else if (pcr < 0) {
2313 error = make_rate_with_tolerance (dev, pcr, pr, 10,
2320 PRINTD (DBG_QOS,
"snatching all remaining TX bandwidth");
2322 }
else if (scr < 0) {
2328 error = make_rate_with_tolerance (dev, scr, sr, 10,
2329 &vcc.tx_scr_bits, &vcc.
tx_rate);
2331 PRINTD (DBG_QOS,
"could not make rate from TX SCR");
2337 PRINTD (DBG_QOS,
"TX SCR failed consistency check");
2343 bucket = mbs*(pcr-
scr)/pcr;
2344 if (bucket*pcr != mbs*(pcr-scr))
2347 PRINTD (DBG_QOS,
"shrinking bucket from %u to %u",
2352 vcc.tx_bucket_bits = bucket;
2357 PRINTD (DBG_QOS,
"unsupported TX traffic class");
2373 error = check_max_sdu (vcc.
aal, rxtp, max_rx_size);
2375 PRINTD (DBG_QOS,
"RX max_sdu check failed");
2395 PRINTD (DBG_QOS,
"snatching all remaining RX bandwidth");
2397 }
else if (pcr < 0) {
2402 error = atm_pcr_check (rxtp, vcc.
rx_rate);
2404 PRINTD (DBG_QOS,
"RX PCR failed consistency check");
2416 PRINTD (DBG_QOS,
"snatching all remaining RX bandwidth");
2418 }
else if (scr < 0) {
2425 PRINTD (DBG_QOS,
"RX SCR failed consistency check");
2432 PRINTD (DBG_QOS,
"unsupported RX traffic class");
2441 PRINTD (DBG_QOS,
"AAL not supported");
2458 PRINTD (DBG_QOS,
"not enough TX PCR left");
2463 PRINTD (DBG_QOS,
"not enough RX PCR left");
2471 PRINTD (DBG_QOS|DBG_VCC,
"reserving %u TX PCR and %u RX PCR",
2478 PRINTD (DBG_QOS|DBG_VCC,
"insufficient cell rate resources");
2490 if (dev->
rxer[channel]) {
2495 error = hrz_open_rx (dev, channel);
2515 static void hrz_close (
struct atm_vcc * atm_vcc) {
2519 PRINTD (DBG_VCC|DBG_FLOW,
"hrz_close");
2524 if (atm_vcc->
qos.txtp.traffic_class !=
ATM_NONE) {
2529 while (tx_hold (dev))
2542 if (atm_vcc->
qos.rxtp.traffic_class !=
ATM_NONE) {
2544 hrz_close_rx (dev, channel);
2546 if (atm_vcc != dev->
rxer[channel])
2548 "arghhh! we're going to die!",
2549 atm_vcc, dev->
rxer[channel]);
2555 PRINTD (DBG_QOS|DBG_VCC,
"releasing %u TX PCR and %u RX PCR",
2568 static int hrz_getsockopt (
struct atm_vcc * atm_vcc,
int level,
int optname,
2569 void *optval,
int optlen) {
2571 PRINTD (DBG_FLOW|DBG_VCC,
"hrz_getsockopt");
2587 static int hrz_setsockopt (
struct atm_vcc * atm_vcc,
int level,
int optname,
2588 void *optval,
unsigned int optlen) {
2590 PRINTD (DBG_FLOW|DBG_VCC,
"hrz_setsockopt");
2610 PRINTD (DBG_FLOW,
"hrz_ioctl");
2614 unsigned char hrz_phy_get (
struct atm_dev *
atm_dev,
unsigned long addr) {
2616 PRINTD (DBG_FLOW,
"hrz_phy_get");
2620 static void hrz_phy_put (
struct atm_dev *
atm_dev,
unsigned char value,
2621 unsigned long addr) {
2623 PRINTD (DBG_FLOW,
"hrz_phy_put");
2626 static int hrz_change_qos (
struct atm_vcc * atm_vcc,
struct atm_qos *qos,
int flgs) {
2628 PRINTD (DBG_FLOW,
"hrz_change_qos");
2638 PRINTD (DBG_FLOW,
"hrz_proc_read");
2647 count +=
sprintf (page,
" %u/%u",
2650 count +=
sprintf (page+count,
".\n");
2657 "cells: TX %lu, RX %lu, HEC errors %lu, unassigned %lu.\n",
2663 "free cell buffers: TX %hu, RX %hu+%hu.\n",
2670 "cps remaining: TX %u, RX %u\n",
2680 .proc_read = hrz_proc_read,
2695 PRINTD (DBG_FLOW,
"hrz_probe");
2714 pci_set_drvdata(pci_dev, dev);
2728 PRINTD(
DBG_INFO,
"found Madge ATM adapter (hrz) at: IO %x, IRQ %u, MEM %p",
2729 iobase, irq, membase);
2739 PRINTD(
DBG_INFO,
"registered Madge ATM adapter (no. %d) (%p) at %p",
2741 dev->
atm_dev->dev_data = (
void *) dev;
2751 "changing", lat, pci_lat);
2796 if (hrz_init(dev)) {
2802 dev->
tx_avail = ((25600000/8)*26)/(27*53);
2803 dev->
rx_avail = ((25600000/8)*26)/(27*53);
2817 dev->
atm_dev->ci_range.vpi_bits = vpi_bits;
2818 dev->
atm_dev->ci_range.vci_bits = 10-vpi_bits;
2839 static void __devexit hrz_remove_one(
struct pci_dev *pci_dev)
2843 dev = pci_get_drvdata(pci_dev);
2856 static void __init hrz_check_args (
void) {
2857 #ifdef DEBUG_HORIZON
2870 max_tx_size = TX_AAL5_LIMIT);
2874 max_rx_size = RX_AAL5_LIMIT);
2905 .id_table = hrz_pci_tbl,
2910 static int __init hrz_module_init (
void) {
2912 if (
sizeof(
struct MEMMAP) != 128*1024/4) {
2914 (
unsigned long)
sizeof(
struct MEMMAP));
2924 return pci_register_driver(&hrz_driver);
2929 static void __exit hrz_module_exit (
void) {
2930 PRINTD (DBG_FLOW,
"cleanup_module");