18 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/module.h>
21 #include <linux/kernel.h>
22 #include <linux/version.h>
23 #include <linux/pci.h>
24 #include <linux/sched.h>
25 #include <linux/slab.h>
30 #include <linux/hdlc.h>
32 #include <asm/uaccess.h>
49 #define FST_MAX_PORTS 4
50 #define FST_MAX_CARDS 32
54 #define FST_TX_QUEUE_LEN 100
56 #define FST_TXQ_DEPTH 16
61 #define FST_HIGH_WATER_MARK 12
63 #define FST_LOW_WATER_MARK 8
65 #define FST_MAX_MTU 8000
66 #define FST_DEF_MTU 1500
68 #define FST_TX_TIMEOUT (2*HZ)
71 #define ARPHRD_MYTYPE ARPHRD_RAWHDLC
73 #define ARPHRD_MYTYPE ARPHRD_HDLC
81 static int fst_max_reads = 7;
82 static int fst_excluded_cards = 0;
106 #define SMC_VERSION 24
108 #define FST_MEMSIZE 0x100000
110 #define SMC_BASE 0x00002000L
112 #define BFM_BASE 0x00010000L
115 #define LEN_TX_BUFFER 8192
116 #define LEN_RX_BUFFER 8192
118 #define LEN_SMALL_TX_BUFFER 256
119 #define LEN_SMALL_RX_BUFFER 256
121 #define NUM_TX_BUFFER 2
122 #define NUM_RX_BUFFER 8
125 #define INT_RETRY_TIME 2
164 #define cnv_bcnt(len) (-(len))
181 #define MAX_CIRBUFF 32
192 #define CTLA_CHG 0x18
193 #define CTLB_CHG 0x19
194 #define CTLC_CHG 0x1A
195 #define CTLD_CHG 0x1B
197 #define INIT_CPLT 0x20
198 #define INIT_FAIL 0x21
200 #define ABTA_SENT 0x24
201 #define ABTB_SENT 0x25
202 #define ABTC_SENT 0x26
203 #define ABTD_SENT 0x27
205 #define TXA_UNDF 0x28
206 #define TXB_UNDF 0x29
207 #define TXC_UNDF 0x2A
208 #define TXD_UNDF 0x2B
213 #define TE1_ALMA 0x30
270 struct rxdesc rxDescrRing[FST_MAX_PORTS][NUM_RX_BUFFER];
271 struct txdesc txDescrRing[FST_MAX_PORTS][NUM_TX_BUFFER];
364 #define END_SIG 0x12345678
376 #define CNTRL_9052 0x50
377 #define CNTRL_9054 0x6c
379 #define INTCSR_9052 0x4c
380 #define INTCSR_9054 0x68
387 #define DMAMODE0 0x80
388 #define DMAPADR0 0x84
389 #define DMALADR0 0x88
392 #define DMAMODE1 0x94
393 #define DMAPADR1 0x98
394 #define DMALADR1 0x9c
403 #define DMAMARBR 0xac
405 #define FST_MIN_DMA_LEN 64
406 #define FST_RX_DMA_INT 0x01
407 #define FST_TX_DMA_INT 0x02
408 #define FST_CARD_INT 0x04
417 #define BUF_OFFSET(X) (BFM_BASE + offsetof(struct buf_window, X))
482 #define dev_to_port(D) (dev_to_hdlc(D)->priv)
483 #define port_to_dev(P) ((P)->dev)
495 #define WIN_OFFSET(X) ((long)&(((struct fst_shared *)SMC_BASE)->X))
497 #define FST_RDB(C,E) readb ((C)->mem + WIN_OFFSET(E))
498 #define FST_RDW(C,E) readw ((C)->mem + WIN_OFFSET(E))
499 #define FST_RDL(C,E) readl ((C)->mem + WIN_OFFSET(E))
501 #define FST_WRB(C,E,B) writeb ((B), (C)->mem + WIN_OFFSET(E))
502 #define FST_WRW(C,E,W) writew ((W), (C)->mem + WIN_OFFSET(E))
503 #define FST_WRL(C,E,L) writel ((L), (C)->mem + WIN_OFFSET(E))
510 static int fst_debug_mask = {
FST_DEBUG };
517 #define dbg(F, fmt, args...) \
519 if (fst_debug_mask & (F)) \
520 printk(KERN_DEBUG pr_fmt(fmt), ##args); \
523 #define dbg(F, fmt, args...) \
526 printk(KERN_DEBUG pr_fmt(fmt), ##args); \
572 static void fst_process_tx_work_q(
unsigned long work_q);
573 static void fst_process_int_work_q(
unsigned long work_q);
580 static u64 fst_work_txq;
581 static u64 fst_work_intq;
584 fst_q_work_item(
u64 *
queue,
int card_index)
600 mask = (
u64)1 << card_index;
602 spin_unlock_irqrestore(&fst_work_q_lock, flags);
606 fst_process_tx_work_q(
unsigned long work_q)
617 work_txq = fst_work_txq;
619 spin_unlock_irqrestore(&fst_work_q_lock, flags);
625 if (work_txq & 0x01) {
626 if (fst_card_array[i] !=
NULL) {
627 dbg(
DBG_TX,
"Calling tx bh for card %d\n", i);
628 do_bottom_half_tx(fst_card_array[i]);
631 work_txq = work_txq >> 1;
636 fst_process_int_work_q(
unsigned long work_q)
647 work_intq = fst_work_intq;
649 spin_unlock_irqrestore(&fst_work_q_lock, flags);
655 if (work_intq & 0x01) {
656 if (fst_card_array[i] !=
NULL) {
658 "Calling rx & tx bh for card %d\n", i);
659 do_bottom_half_rx(fst_card_array[i]);
660 do_bottom_half_tx(fst_card_array[i]);
663 work_intq = work_intq >> 1;
680 unsigned char interrupt_line_register;
685 if (pci_read_config_byte
688 "Error in reading interrupt line register\n");
711 if (pci_write_config_byte
714 "Error in writing interrupt line register\n");
800 dbg(DBG_ASS,
"%s: Received packet dropped\n", name);
836 dev->
stats.tx_packets++;
837 dev->
stats.tx_bytes += len;
847 skb_reset_mac_header(skb);
870 dev->
stats.rx_packets++;
871 dev->
stats.rx_bytes += len;
874 dbg(
DBG_RX,
"Pushing the frame up the stack\n");
876 skb->
protocol = farsync_type_trans(skb, dev);
878 skb->
protocol = hdlc_type_trans(skb, dev);
880 fst_process_rx_status(rx_status,
port_to_dev(port)->name);
882 dev->
stats.rx_dropped++;
896 dbg(
DBG_RX,
"In fst_rx_dma %lx %lx %d\n",
897 (
unsigned long) skb, (
unsigned long) mem, len);
899 dbg(DBG_ASS,
"In fst_rx_dma while dma in progress\n");
919 unsigned char *mem,
int len)
925 dbg(
DBG_TX,
"In fst_tx_dma %p %p %d\n", skb, mem, len);
927 dbg(DBG_ASS,
"In fst_tx_dma while dma in progress\n");
950 unsigned short mbval;
960 while (mbval >
NAK) {
961 spin_unlock_irqrestore(&card->
card_lock, flags);
965 if (++safety > 2000) {
966 pr_err(
"Mailbox safety timeout\n");
973 dbg(
DBG_CMD,
"Mailbox clear after %d jiffies\n", safety);
976 dbg(
DBG_CMD,
"issue_cmd: previous command was NAK'd\n");
987 spin_unlock_irqrestore(&card->
card_lock, flags);
993 fst_op_raise(
struct fst_port_info *port,
unsigned int outputs)
1003 fst_op_lower(
struct fst_port_info *port,
unsigned int outputs)
1021 unsigned long flags;
1030 FST_WRW(card, rxDescrRing[pi][i].ladr, (
u16) offset);
1031 FST_WRB(card, rxDescrRing[pi][i].hadr, (
u8) (offset >> 16));
1037 spin_unlock_irqrestore(&card->
card_lock, flags);
1049 unsigned long flags;
1058 FST_WRW(card, txDescrRing[pi][i].ladr, (
u16) offset);
1059 FST_WRB(card, txDescrRing[pi][i].hadr, (
u8) (offset >> 16));
1060 FST_WRW(card, txDescrRing[pi][i].bcnt, 0);
1066 spin_unlock_irqrestore(&card->
card_lock, flags);
1078 los =
FST_RDB(card, suStatus.lossOfSignal);
1079 rra =
FST_RDB(card, suStatus.receiveRemoteAlarm);
1080 ais =
FST_RDB(card, suStatus.alarmIndicationSignal);
1142 unsigned char dmabits,
int rxp,
unsigned short len)
1149 dev->
stats.rx_errors++;
1151 dev->
stats.rx_fifo_errors++;
1152 dbg(DBG_ASS,
"Rx fifo error on card %d port %d buffer %d\n",
1156 dev->
stats.rx_crc_errors++;
1157 dbg(DBG_ASS,
"Rx crc error on card %d port %d\n",
1161 dev->
stats.rx_frame_errors++;
1162 dbg(DBG_ASS,
"Rx frame error on card %d port %d\n",
1166 dev->
stats.rx_length_errors++;
1167 dbg(DBG_ASS,
"Rx length error (%d) on card %d port %d\n",
1176 unsigned char dmabits,
int rxp,
unsigned short len)
1190 rxp = (rxp+1) % NUM_RX_BUFFER;
1191 if (++i > NUM_RX_BUFFER) {
1192 dbg(DBG_ASS,
"intr_rx: Discarding more bufs"
1196 dmabits =
FST_RDB(card, rxDescrRing[pi][rxp].
bits);
1197 dbg(DBG_ASS,
"DMA Bits of next buffer was %x\n", dmabits);
1199 dbg(DBG_ASS,
"There were %d subsequent buffers in error\n", i);
1203 FST_WRB(card, rxDescrRing[pi][rxp].
bits, DMA_OWN);
1204 rxp = (rxp+1) % NUM_RX_BUFFER;
1216 unsigned char dmabits;
1227 dmabits =
FST_RDB(card, rxDescrRing[pi][rxp].
bits);
1238 len =
FST_RDW(card, rxDescrRing[pi][rxp].mcnt);
1246 pr_err(
"Frame received with 0 length. Card %d Port %d\n",
1249 FST_WRB(card, rxDescrRing[pi][rxp].
bits, DMA_OWN);
1251 rxp = (rxp+1) % NUM_RX_BUFFER;
1260 dbg(
DBG_RX,
"intr_rx: %d,%d: flags %x len %d\n", pi, rxp, dmabits, len);
1262 fst_log_rx_error(card, port, dmabits, rxp, len);
1263 fst_recover_rx_error(card, port, dmabits, rxp, len);
1268 if ((skb = dev_alloc_skb(len)) ==
NULL) {
1269 dbg(
DBG_RX,
"intr_rx: can't allocate buffer\n");
1271 dev->
stats.rx_dropped++;
1274 FST_WRB(card, rxDescrRing[pi][rxp].
bits, DMA_OWN);
1276 rxp = (rxp+1) % NUM_RX_BUFFER;
1293 FST_WRB(card, rxDescrRing[pi][rxp].
bits, DMA_OWN);
1296 dev->
stats.rx_packets++;
1297 dev->
stats.rx_bytes += len;
1300 dbg(
DBG_RX,
"Pushing frame up the stack\n");
1302 skb->
protocol = farsync_type_trans(skb, dev);
1304 skb->
protocol = hdlc_type_trans(skb, dev);
1306 fst_process_rx_status(rx_status,
port_to_dev(port)->name);
1308 dev->
stats.rx_dropped++;
1317 if (rxp != port->
rxpos) {
1318 dbg(DBG_ASS,
"About to increment rxpos by more than 1\n");
1319 dbg(DBG_ASS,
"rxp = %d rxpos = %d\n", rxp, port->
rxpos);
1321 rxp = (rxp+1) % NUM_RX_BUFFER;
1337 unsigned long flags;
1346 for (pi = 0, port = card->
ports; pi < card->nports; pi++, port++) {
1351 while (!(
FST_RDB(card, txDescrRing[pi][port->
txpos].bits) &
1360 if ((txq_length = port->
txqe - port->
txqs) < 0) {
1367 spin_unlock_irqrestore(&card->
card_lock, flags);
1368 if (txq_length > 0) {
1378 spin_unlock_irqrestore(&card->
card_lock, flags);
1394 txDescrRing[pi][port->
txpos].
1397 dev->
stats.tx_packets++;
1415 if (++port->
txpos >= NUM_TX_BUFFER)
1421 if (txq_length < fst_txq_low) {
1447 for (pi = 0, port = card->
ports; pi < card->nports; pi++, port++) {
1451 while (!(
FST_RDB(card, rxDescrRing[pi][port->
rxpos].bits)
1453 if (rx_count > fst_max_reads) {
1458 fst_q_work_item(&fst_work_intq, card->
card_no);
1459 tasklet_schedule(&fst_int_task);
1462 fst_intr_rx(card, port);
1480 unsigned int dma_intcsr = 0;
1481 unsigned int do_card_interrupt;
1482 unsigned int int_retry_count;
1491 pr_err(
"Interrupt received for card %d in a non running state (%d)\n",
1499 fst_clear_intr(card);
1504 fst_clear_intr(card);
1509 do_card_interrupt = 0;
1510 if (
FST_RDB(card, interruptHandshake) == 1) {
1513 FST_WRB(card, interruptHandshake, 0xEE);
1520 if (dma_intcsr & 0x00200000) {
1532 if (dma_intcsr & 0x00400000) {
1548 int_retry_count =
FST_RDL(card, interruptRetryCount);
1549 if (int_retry_count) {
1550 dbg(DBG_ASS,
"Card %d int_retry_count is %d\n",
1551 card->
card_no, int_retry_count);
1552 FST_WRL(card, interruptRetryCount, 0);
1555 if (!do_card_interrupt) {
1560 fst_q_work_item(&fst_work_intq, card->
card_no);
1561 tasklet_schedule(&fst_int_task);
1564 rdidx =
FST_RDB(card, interruptEvent.rdindex) & 0x1f;
1565 wridx =
FST_RDB(card, interruptEvent.wrindex) & 0x1f;
1566 while (rdidx != wridx) {
1567 event =
FST_RDB(card, interruptEvent.evntbuff[rdidx]);
1568 port = &card->
ports[
event & 0x03];
1570 dbg(
DBG_INTR,
"Processing Interrupt event: %x\n", event);
1576 fst_intr_te1_alarm(card, port);
1584 fst_intr_ctlchg(card, port);
1604 dbg(DBG_ASS,
"Tx underflow on card %d port %d\n",
1618 pr_err(
"intr: unknown card event %d. ignored\n", event);
1626 FST_WRB(card, interruptEvent.rdindex, rdidx);
1640 pr_err(
"Bad shared memory version %d expected %d\n",
1646 pr_err(
"Missing shared memory signature\n");
1651 if ((i =
FST_RDB(card, taskStatus)) == 0x01) {
1653 }
else if (i == 0xFF) {
1654 pr_err(
"Firmware initialisation failed. Card halted\n");
1657 }
else if (i != 0x00) {
1658 pr_err(
"Unknown firmware status 0x%x\n", i);
1668 pr_warn(
"Port count mismatch on card %d. Firmware thinks %d we say %d\n",
1679 unsigned char my_framing;
1714 FST_WRB(card, suConfig.framing, my_framing);
1728 FST_WRB(card, suConfig.enableIdleCode, 1);
1730 FST_WRB(card, suConfig.enableIdleCode, 0);
1734 printk(
"Setting TE1 data\n");
1738 printk(
"Framing = %d\n", my_framing);
1744 printk(
"Transparent mode = %d\n",
1757 fst_debug_mask = info->
debug;
1780 info->
debug = fst_debug_mask;
1809 if (port->
index == 0) {
1840 if (
FST_RDB(card, suConfig.enableIdleCode))
1845 FST_RDL(card, suStatus.receiveBufferDelay);
1847 FST_RDL(card, suStatus.framingErrorCount);
1849 FST_RDL(card, suStatus.codeViolationCount);
1854 FST_RDB(card, suStatus.receiveRemoteAlarm);
1856 FST_RDB(card, suStatus.alarmIndicationSignal);
1867 if (ifr->ifr_settings.size !=
sizeof (sync)) {
1872 (&sync, ifr->ifr_settings.ifs_ifsu.sync,
sizeof (sync))) {
1881 switch (ifr->ifr_settings.type) {
1883 FST_WRW(card, portConfig[i].lineInterface,
V35);
1888 FST_WRW(card, portConfig[i].lineInterface,
V24);
1893 FST_WRW(card, portConfig[i].lineInterface,
X21);
1903 FST_WRW(card, portConfig[i].lineInterface,
T1);
1908 FST_WRW(card, portConfig[i].lineInterface,
E1);
1946 switch (port->
hwif) {
1967 if (ifr->ifr_settings.size == 0) {
1970 if (ifr->ifr_settings.size <
sizeof (sync)) {
1985 ifr->ifr_settings.size =
sizeof (
sync);
1996 unsigned long flags;
2014 fst_cpurelease(card);
2023 if (ifr->ifr_data ==
NULL) {
2044 return PTR_ERR(buf);
2063 check_started_ok(card);
2068 fst_enable_intr(card);
2069 FST_WRB(card, interruptHandshake, 0xEE);
2070 spin_unlock_irqrestore(&card->
card_lock, flags);
2074 if (ifr->ifr_data ==
NULL) {
2078 gather_conf_info(card, port, &info);
2093 pr_err(
"Attempt to configure card %d in non-running state (%d)\n",
2101 return set_conf_from_info(card, port, &info);
2104 switch (ifr->ifr_settings.type) {
2106 return fst_get_iface(card, port, ifr);
2115 return fst_set_iface(card, port, ifr);
2131 ifr->ifr_settings.type);
2152 dbg(
DBG_OPEN,
"open: found port already running\n");
2158 fst_rx_config(port);
2159 fst_tx_config(port);
2172 txq_length = port->
txqe - port->
txqs;
2213 netif_wake_queue(dev);
2222 unsigned char tx_dma_done;
2223 unsigned char rx_dma_done;
2231 "Port Close: tx_dma_in_progress = %d (%x) rx_dma_in_progress = %d (%x)\n",
2235 netif_stop_queue(dev);
2245 fst_attach(
struct net_device *dev,
unsigned short encoding,
unsigned short parity)
2263 dev->
stats.tx_errors++;
2264 dev->
stats.tx_aborted_errors++;
2265 dbg(DBG_ASS,
"Tx timeout card %d port %d\n",
2270 netif_wake_queue(dev);
2279 unsigned long flags;
2284 dbg(
DBG_TX,
"fst_start_xmit: length = %d\n", skb->
len);
2287 if (!netif_carrier_ok(dev)) {
2289 dev->
stats.tx_errors++;
2290 dev->
stats.tx_carrier_errors++;
2292 "Tried to transmit but no carrier on card %d port %d\n",
2299 dbg(DBG_ASS,
"Packet too large %d vs %d\n", skb->
len,
2302 dev->
stats.tx_errors++;
2312 if ((txq_length = port->
txqe - port->
txqs) < 0) {
2319 spin_unlock_irqrestore(&card->
card_lock, flags);
2320 if (txq_length > fst_txq_high) {
2325 netif_stop_queue(dev);
2334 dev->
stats.tx_errors++;
2335 dbg(DBG_ASS,
"Tx queue overflow card %d port %d\n",
2348 spin_unlock_irqrestore(&card->
card_lock, flags);
2351 fst_q_work_item(&fst_work_txq, card->
card_no);
2352 tasklet_schedule(&fst_tx_task);
2384 for (i = 0; i < card->
nports; i++) {
2388 pr_err(
"Cannot register HDLC device for port %d (errno %d)\n",
2390 for (j = i; j < card->
nports; j++) {
2399 pr_info(
"%s-%s: %s IRQ%d, %d ports\n",
2406 .ndo_open = fst_open,
2407 .ndo_stop = fst_close,
2410 .ndo_do_ioctl = fst_ioctl,
2411 .ndo_tx_timeout = fst_tx_timeout,
2421 static int no_of_cards_added = 0;
2428 " (c) 2001-2004 FarSite Communications Ltd.\n"));
2430 dbg(DBG_ASS,
"The value of debug mask is %x\n", fst_debug_mask);
2436 if (fst_excluded_cards != 0) {
2441 for (i = 0; i < fst_excluded_cards; i++) {
2442 if ((pdev->
devfn) >> 3 == fst_excluded_list[i]) {
2443 pr_info(
"FarSync PCI device %d not assigned\n",
2444 (pdev->
devfn) >> 3);
2453 pr_err(
"FarSync card found but insufficient memory for driver storage\n");
2459 pr_err(
"Failed to enable card. Err %d\n", -err);
2465 pr_err(
"Failed to allocate regions. Err %d\n", -err);
2476 pr_err(
"Physical memory remap failed\n");
2483 pr_err(
"Control memory remap failed\n");
2494 pr_err(
"Unable to register interrupt %d\n", card->
irq);
2519 for ( i = 0 ; i < card->
nports ; i++ ) {
2525 pr_err(
"FarSync: out of memory\n");
2539 hdlc = dev_to_hdlc(dev);
2549 +
BUF_OFFSET ( txBuffer[i][NUM_TX_BUFFER][0]);
2556 hdlc->
attach = fst_attach;
2557 hdlc->
xmit = fst_start_xmit;
2564 dbg(
DBG_PCI,
"conf %04x mem %08x ctlmem %08x\n",
2575 pci_set_drvdata(pdev, card);
2578 fst_card_array[no_of_cards_added] =
card;
2579 card->
card_no = no_of_cards_added++;
2580 fst_init_card(card);
2589 pr_err(
"Could not allocate rx dma buffer\n");
2590 fst_disable_intr(card);
2602 pr_err(
"Could not allocate tx dma buffer\n");
2603 fst_disable_intr(card);
2619 fst_remove_one(
struct pci_dev *pdev)
2624 card = pci_get_drvdata(pdev);
2626 for (i = 0; i < card->
nports; i++) {
2631 fst_disable_intr(card);
2653 .id_table = fst_pci_dev_id,
2654 .probe = fst_add_one,
2666 fst_card_array[i] =
NULL;
2668 return pci_register_driver(&fst_driver);
2672 fst_cleanup_module(
void)
2674 pr_info(
"FarSync WAN driver unloading\n");