1 // SPDX-License-Identifier: GPL-2.0-only
3 * wanXL serial card driver for Linux
9 * - Only DTE (external clock) support with NRZ and NRZI encodings
10 * - wanXL100 will require minor driver modifications, no access to hw
13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include <linux/types.h>
20 #include <linux/fcntl.h>
21 #include <linux/string.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/ioport.h>
26 #include <linux/netdevice.h>
27 #include <linux/hdlc.h>
28 #include <linux/pci.h>
29 #include <linux/dma-mapping.h>
30 #include <linux/delay.h>
35 static const char *version = "wanXL serial card driver version: 0.48";
37 #define PLX_CTL_RESET 0x40000000 /* adapter reset */
42 /* MAILBOX #1 - PUTS COMMANDS */
43 #define MBX1_CMD_ABORTJ 0x85000000 /* Abort and Jump */
44 #ifdef __LITTLE_ENDIAN
45 #define MBX1_CMD_BSWAP 0x8C000001 /* little-endian Byte Swap Mode */
47 #define MBX1_CMD_BSWAP 0x8C000000 /* big-endian Byte Swap Mode */
50 /* MAILBOX #2 - DRAM SIZE */
51 #define MBX2_MEMSZ_MASK 0xFFFF0000 /* PUTS Memory Size Register mask */
54 struct net_device *dev;
56 spinlock_t lock; /* for wanxl_xmit */
57 int node; /* physical port #0 - 3 */
58 unsigned int clock_type;
60 struct sk_buff *tx_skbs[TX_BUFFERS];
64 desc_t rx_descs[RX_QUEUE_LENGTH];
65 port_status_t port_status[4];
69 int n_ports; /* 1, 2 or 4 ports */
72 u8 __iomem *plx; /* PLX PCI9060 virtual base address */
73 struct pci_dev *pdev; /* for pci_name(pdev) */
75 struct sk_buff *rx_skbs[RX_QUEUE_LENGTH];
76 struct card_status *status; /* shared between host and card */
77 dma_addr_t status_address;
78 struct port ports[]; /* 1 - 4 port structures follow */
81 static inline struct port *dev_to_port(struct net_device *dev)
83 return (struct port *)dev_to_hdlc(dev)->priv;
86 static inline port_status_t *get_status(struct port *port)
88 return &port->card->status->port_status[port->node];
92 static inline dma_addr_t pci_map_single_debug(struct pci_dev *pdev, void *ptr,
93 size_t size, int direction)
95 dma_addr_t addr = dma_map_single(&pdev->dev, ptr, size, direction);
97 if (addr + size > 0x100000000LL)
98 pr_crit("%s: pci_map_single() returned memory at 0x%llx!\n",
99 pci_name(pdev), (unsigned long long)addr);
103 #undef pci_map_single
104 #define pci_map_single pci_map_single_debug
107 /* Cable and/or personality module change interrupt service */
108 static inline void wanxl_cable_intr(struct port *port)
110 u32 value = get_status(port)->cable;
112 const char *cable, *pm, *dte = "", *dsr = "", *dcd = "";
114 switch (value & 0x7) {
115 case STATUS_CABLE_V35:
118 case STATUS_CABLE_X21:
121 case STATUS_CABLE_V24:
124 case STATUS_CABLE_EIA530:
127 case STATUS_CABLE_NONE:
134 switch ((value >> STATUS_CABLE_PM_SHIFT) & 0x7) {
135 case STATUS_CABLE_V35:
138 case STATUS_CABLE_X21:
141 case STATUS_CABLE_V24:
144 case STATUS_CABLE_EIA530:
147 case STATUS_CABLE_NONE:
148 pm = "no personality";
152 pm = "invalid personality";
157 if ((value & 7) == ((value >> STATUS_CABLE_PM_SHIFT) & 7)) {
158 dsr = (value & STATUS_CABLE_DSR) ? ", DSR ON" :
160 dcd = (value & STATUS_CABLE_DCD) ? ", carrier ON" :
163 dte = (value & STATUS_CABLE_DCE) ? " DCE" : " DTE";
165 netdev_info(port->dev, "%s%s module, %s cable%s%s\n",
166 pm, dte, cable, dsr, dcd);
168 if (value & STATUS_CABLE_DCD)
169 netif_carrier_on(port->dev);
171 netif_carrier_off(port->dev);
174 /* Transmit complete interrupt service */
175 static inline void wanxl_tx_intr(struct port *port)
177 struct net_device *dev = port->dev;
180 desc_t *desc = &get_status(port)->tx_descs[port->tx_in];
181 struct sk_buff *skb = port->tx_skbs[port->tx_in];
183 switch (desc->stat) {
186 netif_wake_queue(dev);
189 case PACKET_UNDERRUN:
190 dev->stats.tx_errors++;
191 dev->stats.tx_fifo_errors++;
195 dev->stats.tx_packets++;
196 dev->stats.tx_bytes += skb->len;
198 desc->stat = PACKET_EMPTY; /* Free descriptor */
199 dma_unmap_single(&port->card->pdev->dev, desc->address,
200 skb->len, DMA_TO_DEVICE);
201 dev_consume_skb_irq(skb);
202 port->tx_in = (port->tx_in + 1) % TX_BUFFERS;
206 /* Receive complete interrupt service */
207 static inline void wanxl_rx_intr(struct card *card)
211 while (desc = &card->status->rx_descs[card->rx_in],
212 desc->stat != PACKET_EMPTY) {
213 if ((desc->stat & PACKET_PORT_MASK) > card->n_ports) {
214 pr_crit("%s: received packet for nonexistent port\n",
215 pci_name(card->pdev));
217 struct sk_buff *skb = card->rx_skbs[card->rx_in];
218 struct port *port = &card->ports[desc->stat &
220 struct net_device *dev = port->dev;
223 dev->stats.rx_dropped++;
225 dma_unmap_single(&card->pdev->dev,
226 desc->address, BUFFER_LENGTH,
228 skb_put(skb, desc->length);
231 printk(KERN_DEBUG "%s RX(%i):", dev->name,
235 dev->stats.rx_packets++;
236 dev->stats.rx_bytes += skb->len;
237 skb->protocol = hdlc_type_trans(skb, dev);
243 skb = dev_alloc_skb(BUFFER_LENGTH);
244 desc->address = skb ?
245 dma_map_single(&card->pdev->dev,
248 DMA_FROM_DEVICE) : 0;
249 card->rx_skbs[card->rx_in] = skb;
252 desc->stat = PACKET_EMPTY; /* Free descriptor */
253 card->rx_in = (card->rx_in + 1) % RX_QUEUE_LENGTH;
257 static irqreturn_t wanxl_intr(int irq, void *dev_id)
259 struct card *card = dev_id;
264 while ((stat = readl(card->plx + PLX_DOORBELL_FROM_CARD)) != 0) {
266 writel(stat, card->plx + PLX_DOORBELL_FROM_CARD);
268 for (i = 0; i < card->n_ports; i++) {
269 if (stat & (1 << (DOORBELL_FROM_CARD_TX_0 + i)))
270 wanxl_tx_intr(&card->ports[i]);
271 if (stat & (1 << (DOORBELL_FROM_CARD_CABLE_0 + i)))
272 wanxl_cable_intr(&card->ports[i]);
274 if (stat & (1 << DOORBELL_FROM_CARD_RX))
278 return IRQ_RETVAL(handled);
281 static netdev_tx_t wanxl_xmit(struct sk_buff *skb, struct net_device *dev)
283 struct port *port = dev_to_port(dev);
286 spin_lock(&port->lock);
288 desc = &get_status(port)->tx_descs[port->tx_out];
289 if (desc->stat != PACKET_EMPTY) {
290 /* should never happen - previous xmit should stop queue */
292 printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name);
294 netif_stop_queue(dev);
295 spin_unlock(&port->lock);
296 return NETDEV_TX_BUSY; /* request packet to be queued */
300 printk(KERN_DEBUG "%s TX(%i):", dev->name, skb->len);
304 port->tx_skbs[port->tx_out] = skb;
305 desc->address = dma_map_single(&port->card->pdev->dev, skb->data,
306 skb->len, DMA_TO_DEVICE);
307 desc->length = skb->len;
308 desc->stat = PACKET_FULL;
309 writel(1 << (DOORBELL_TO_CARD_TX_0 + port->node),
310 port->card->plx + PLX_DOORBELL_TO_CARD);
312 port->tx_out = (port->tx_out + 1) % TX_BUFFERS;
314 if (get_status(port)->tx_descs[port->tx_out].stat != PACKET_EMPTY) {
315 netif_stop_queue(dev);
317 printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name);
321 spin_unlock(&port->lock);
325 static int wanxl_attach(struct net_device *dev, unsigned short encoding,
326 unsigned short parity)
328 struct port *port = dev_to_port(dev);
330 if (encoding != ENCODING_NRZ &&
331 encoding != ENCODING_NRZI)
334 if (parity != PARITY_NONE &&
335 parity != PARITY_CRC32_PR1_CCITT &&
336 parity != PARITY_CRC16_PR1_CCITT &&
337 parity != PARITY_CRC32_PR0_CCITT &&
338 parity != PARITY_CRC16_PR0_CCITT)
341 get_status(port)->encoding = encoding;
342 get_status(port)->parity = parity;
346 static int wanxl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
348 const size_t size = sizeof(sync_serial_settings);
349 sync_serial_settings line;
350 struct port *port = dev_to_port(dev);
352 if (cmd != SIOCWANDEV)
353 return hdlc_ioctl(dev, ifr, cmd);
355 switch (ifr->ifr_settings.type) {
357 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
358 if (ifr->ifr_settings.size < size) {
359 ifr->ifr_settings.size = size; /* data size wanted */
362 memset(&line, 0, sizeof(line));
363 line.clock_type = get_status(port)->clocking;
367 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.sync, &line, size))
371 case IF_IFACE_SYNC_SERIAL:
372 if (!capable(CAP_NET_ADMIN))
374 if (dev->flags & IFF_UP)
377 if (copy_from_user(&line, ifr->ifr_settings.ifs_ifsu.sync,
381 if (line.clock_type != CLOCK_EXT &&
382 line.clock_type != CLOCK_TXFROMRX)
383 return -EINVAL; /* No such clock setting */
385 if (line.loopback != 0)
388 get_status(port)->clocking = line.clock_type;
392 return hdlc_ioctl(dev, ifr, cmd);
396 static int wanxl_open(struct net_device *dev)
398 struct port *port = dev_to_port(dev);
399 u8 __iomem *dbr = port->card->plx + PLX_DOORBELL_TO_CARD;
400 unsigned long timeout;
403 if (get_status(port)->open) {
404 netdev_err(dev, "port already open\n");
412 port->tx_in = port->tx_out = 0;
413 for (i = 0; i < TX_BUFFERS; i++)
414 get_status(port)->tx_descs[i].stat = PACKET_EMPTY;
415 /* signal the card */
416 writel(1 << (DOORBELL_TO_CARD_OPEN_0 + port->node), dbr);
418 timeout = jiffies + HZ;
420 if (get_status(port)->open) {
421 netif_start_queue(dev);
424 } while (time_after(timeout, jiffies));
426 netdev_err(dev, "unable to open port\n");
427 /* ask the card to close the port, should it be still alive */
428 writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node), dbr);
432 static int wanxl_close(struct net_device *dev)
434 struct port *port = dev_to_port(dev);
435 unsigned long timeout;
439 /* signal the card */
440 writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node),
441 port->card->plx + PLX_DOORBELL_TO_CARD);
443 timeout = jiffies + HZ;
445 if (!get_status(port)->open)
447 } while (time_after(timeout, jiffies));
449 if (get_status(port)->open)
450 netdev_err(dev, "unable to close port\n");
452 netif_stop_queue(dev);
454 for (i = 0; i < TX_BUFFERS; i++) {
455 desc_t *desc = &get_status(port)->tx_descs[i];
457 if (desc->stat != PACKET_EMPTY) {
458 desc->stat = PACKET_EMPTY;
459 dma_unmap_single(&port->card->pdev->dev,
460 desc->address, port->tx_skbs[i]->len,
462 dev_kfree_skb(port->tx_skbs[i]);
468 static struct net_device_stats *wanxl_get_stats(struct net_device *dev)
470 struct port *port = dev_to_port(dev);
472 dev->stats.rx_over_errors = get_status(port)->rx_overruns;
473 dev->stats.rx_frame_errors = get_status(port)->rx_frame_errors;
474 dev->stats.rx_errors = dev->stats.rx_over_errors +
475 dev->stats.rx_frame_errors;
479 static int wanxl_puts_command(struct card *card, u32 cmd)
481 unsigned long timeout = jiffies + 5 * HZ;
483 writel(cmd, card->plx + PLX_MAILBOX_1);
485 if (readl(card->plx + PLX_MAILBOX_1) == 0)
489 } while (time_after(timeout, jiffies));
494 static void wanxl_reset(struct card *card)
496 u32 old_value = readl(card->plx + PLX_CONTROL) & ~PLX_CTL_RESET;
498 writel(0x80, card->plx + PLX_MAILBOX_0);
499 writel(old_value | PLX_CTL_RESET, card->plx + PLX_CONTROL);
500 readl(card->plx + PLX_CONTROL); /* wait for posted write */
502 writel(old_value, card->plx + PLX_CONTROL);
503 readl(card->plx + PLX_CONTROL); /* wait for posted write */
506 static void wanxl_pci_remove_one(struct pci_dev *pdev)
508 struct card *card = pci_get_drvdata(pdev);
511 for (i = 0; i < card->n_ports; i++) {
512 unregister_hdlc_device(card->ports[i].dev);
513 free_netdev(card->ports[i].dev);
516 /* unregister and free all host resources */
518 free_irq(card->irq, card);
522 for (i = 0; i < RX_QUEUE_LENGTH; i++)
523 if (card->rx_skbs[i]) {
524 dma_unmap_single(&card->pdev->dev,
525 card->status->rx_descs[i].address,
526 BUFFER_LENGTH, DMA_FROM_DEVICE);
527 dev_kfree_skb(card->rx_skbs[i]);
534 dma_free_coherent(&pdev->dev, sizeof(struct card_status),
535 card->status, card->status_address);
537 pci_release_regions(pdev);
538 pci_disable_device(pdev);
542 #include "wanxlfw.inc"
544 static const struct net_device_ops wanxl_ops = {
545 .ndo_open = wanxl_open,
546 .ndo_stop = wanxl_close,
547 .ndo_start_xmit = hdlc_start_xmit,
548 .ndo_do_ioctl = wanxl_ioctl,
549 .ndo_get_stats = wanxl_get_stats,
552 static int wanxl_pci_init_one(struct pci_dev *pdev,
553 const struct pci_device_id *ent)
557 unsigned long timeout;
558 u32 plx_phy; /* PLX PCI base address */
559 u32 mem_phy; /* memory PCI base addr */
560 u8 __iomem *mem; /* memory virtual base addr */
564 pr_info_once("%s\n", version);
567 i = pci_enable_device(pdev);
571 /* QUICC can only access first 256 MB of host RAM directly,
572 * but PLX9060 DMA does 32-bits for actual packet data transfers
575 /* FIXME when PCI/DMA subsystems are fixed.
576 * We set both dma_mask and consistent_dma_mask to 28 bits
577 * and pray pci_alloc_consistent() will use this info. It should
578 * work on most platforms
580 if (dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(28)) ||
581 dma_set_mask(&pdev->dev, DMA_BIT_MASK(28))) {
582 pr_err("No usable DMA configuration\n");
583 pci_disable_device(pdev);
587 i = pci_request_regions(pdev, "wanXL");
589 pci_disable_device(pdev);
593 switch (pdev->device) {
594 case PCI_DEVICE_ID_SBE_WANXL100:
597 case PCI_DEVICE_ID_SBE_WANXL200:
604 card = kzalloc(struct_size(card, ports, ports), GFP_KERNEL);
606 pci_release_regions(pdev);
607 pci_disable_device(pdev);
611 pci_set_drvdata(pdev, card);
614 card->status = dma_alloc_coherent(&pdev->dev,
615 sizeof(struct card_status),
616 &card->status_address, GFP_KERNEL);
618 wanxl_pci_remove_one(pdev);
623 printk(KERN_DEBUG "wanXL %s: pci_alloc_consistent() returned memory"
624 " at 0x%LX\n", pci_name(pdev),
625 (unsigned long long)card->status_address);
628 /* FIXME when PCI/DMA subsystems are fixed.
629 * We set both dma_mask and consistent_dma_mask back to 32 bits
630 * to indicate the card can do 32-bit DMA addressing
632 if (dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)) ||
633 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
634 pr_err("No usable DMA configuration\n");
635 wanxl_pci_remove_one(pdev);
639 /* set up PLX mapping */
640 plx_phy = pci_resource_start(pdev, 0);
642 card->plx = ioremap(plx_phy, 0x70);
644 pr_err("ioremap() failed\n");
645 wanxl_pci_remove_one(pdev);
649 #if RESET_WHILE_LOADING
653 timeout = jiffies + 20 * HZ;
654 while ((stat = readl(card->plx + PLX_MAILBOX_0)) != 0) {
655 if (time_before(timeout, jiffies)) {
656 pr_warn("%s: timeout waiting for PUTS to complete\n",
658 wanxl_pci_remove_one(pdev);
662 switch (stat & 0xC0) {
663 case 0x00: /* hmm - PUTS completed with non-zero code? */
664 case 0x80: /* PUTS still testing the hardware */
668 pr_warn("%s: PUTS test 0x%X failed\n",
669 pci_name(pdev), stat & 0x30);
670 wanxl_pci_remove_one(pdev);
677 /* get on-board memory size (PUTS detects no more than 4 MB) */
678 ramsize = readl(card->plx + PLX_MAILBOX_2) & MBX2_MEMSZ_MASK;
680 /* set up on-board RAM mapping */
681 mem_phy = pci_resource_start(pdev, 2);
683 /* sanity check the board's reported memory size */
684 if (ramsize < BUFFERS_ADDR +
685 (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports) {
686 pr_warn("%s: no enough on-board RAM (%u bytes detected, %u bytes required)\n",
687 pci_name(pdev), ramsize,
689 (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports);
690 wanxl_pci_remove_one(pdev);
694 if (wanxl_puts_command(card, MBX1_CMD_BSWAP)) {
695 pr_warn("%s: unable to Set Byte Swap Mode\n", pci_name(pdev));
696 wanxl_pci_remove_one(pdev);
700 for (i = 0; i < RX_QUEUE_LENGTH; i++) {
701 struct sk_buff *skb = dev_alloc_skb(BUFFER_LENGTH);
703 card->rx_skbs[i] = skb;
705 card->status->rx_descs[i].address =
706 dma_map_single(&card->pdev->dev, skb->data,
707 BUFFER_LENGTH, DMA_FROM_DEVICE);
710 mem = ioremap(mem_phy, PDM_OFFSET + sizeof(firmware));
712 pr_err("ioremap() failed\n");
713 wanxl_pci_remove_one(pdev);
717 for (i = 0; i < sizeof(firmware); i += 4)
718 writel(ntohl(*(__be32 *)(firmware + i)), mem + PDM_OFFSET + i);
720 for (i = 0; i < ports; i++)
721 writel(card->status_address +
722 (void *)&card->status->port_status[i] -
723 (void *)card->status, mem + PDM_OFFSET + 4 + i * 4);
724 writel(card->status_address, mem + PDM_OFFSET + 20);
725 writel(PDM_OFFSET, mem);
728 writel(0, card->plx + PLX_MAILBOX_5);
730 if (wanxl_puts_command(card, MBX1_CMD_ABORTJ)) {
731 pr_warn("%s: unable to Abort and Jump\n", pci_name(pdev));
732 wanxl_pci_remove_one(pdev);
736 timeout = jiffies + 5 * HZ;
738 stat = readl(card->plx + PLX_MAILBOX_5);
742 } while (time_after(timeout, jiffies));
745 pr_warn("%s: timeout while initializing card firmware\n",
747 wanxl_pci_remove_one(pdev);
755 pr_info("%s: at 0x%X, %u KB of RAM at 0x%X, irq %u\n",
756 pci_name(pdev), plx_phy, ramsize / 1024, mem_phy, pdev->irq);
759 if (request_irq(pdev->irq, wanxl_intr, IRQF_SHARED, "wanXL", card)) {
760 pr_warn("%s: could not allocate IRQ%i\n",
761 pci_name(pdev), pdev->irq);
762 wanxl_pci_remove_one(pdev);
765 card->irq = pdev->irq;
767 for (i = 0; i < ports; i++) {
769 struct port *port = &card->ports[i];
770 struct net_device *dev = alloc_hdlcdev(port);
773 pr_err("%s: unable to allocate memory\n",
775 wanxl_pci_remove_one(pdev);
780 hdlc = dev_to_hdlc(dev);
781 spin_lock_init(&port->lock);
782 dev->tx_queue_len = 50;
783 dev->netdev_ops = &wanxl_ops;
784 hdlc->attach = wanxl_attach;
785 hdlc->xmit = wanxl_xmit;
788 get_status(port)->clocking = CLOCK_EXT;
789 if (register_hdlc_device(dev)) {
790 pr_err("%s: unable to register hdlc device\n",
793 wanxl_pci_remove_one(pdev);
799 pr_info("%s: port", pci_name(pdev));
800 for (i = 0; i < ports; i++)
801 pr_cont("%s #%i: %s",
802 i ? "," : "", i, card->ports[i].dev->name);
805 for (i = 0; i < ports; i++)
806 wanxl_cable_intr(&card->ports[i]); /* get carrier status etc.*/
811 static const struct pci_device_id wanxl_pci_tbl[] = {
812 { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL100, PCI_ANY_ID,
813 PCI_ANY_ID, 0, 0, 0 },
814 { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL200, PCI_ANY_ID,
815 PCI_ANY_ID, 0, 0, 0 },
816 { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL400, PCI_ANY_ID,
817 PCI_ANY_ID, 0, 0, 0 },
821 static struct pci_driver wanxl_pci_driver = {
823 .id_table = wanxl_pci_tbl,
824 .probe = wanxl_pci_init_one,
825 .remove = wanxl_pci_remove_one,
828 static int __init wanxl_init_module(void)
831 pr_info("%s\n", version);
833 return pci_register_driver(&wanxl_pci_driver);
836 static void __exit wanxl_cleanup_module(void)
838 pci_unregister_driver(&wanxl_pci_driver);
842 MODULE_DESCRIPTION("SBE Inc. wanXL serial port driver");
843 MODULE_LICENSE("GPL v2");
844 MODULE_DEVICE_TABLE(pci, wanxl_pci_tbl);
846 module_init(wanxl_init_module);
847 module_exit(wanxl_cleanup_module);