blob: a83133388de919dc43b6c368cf797342568aeb82 [file] [log] [blame]
Thomas Gleixner25763b32019-05-28 10:10:09 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * wanXL serial card driver for Linux
4 * host part
5 *
6 * Copyright (C) 2003 Krzysztof Halasa <khc@pm.waw.pl>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Status:
9 * - Only DTE (external clock) support with NRZ and NRZI encodings
10 * - wanXL100 will require minor driver modifications, no access to hw
11 */
12
Joe Perches12a3bfe2011-06-26 19:01:28 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/ioport.h>
26#include <linux/netdevice.h>
27#include <linux/hdlc.h>
28#include <linux/pci.h>
Domen Puncer1e7f0bd2005-06-26 18:22:14 -040029#include <linux/dma-mapping.h>
Al Viro164006d2005-11-30 23:47:05 -050030#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include "wanxl.h"
34
35static const char* version = "wanXL serial card driver version: 0.48";
36
37#define PLX_CTL_RESET 0x40000000 /* adapter reset */
38
39#undef DEBUG_PKT
40#undef DEBUG_PCI
41
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 */
46#else
47#define MBX1_CMD_BSWAP 0x8C000000 /* big-endian Byte Swap Mode */
48#endif
49
50/* MAILBOX #2 - DRAM SIZE */
51#define MBX2_MEMSZ_MASK 0xFFFF0000 /* PUTS Memory Size Register mask */
52
53
Himangi Saraogidb569582014-08-10 13:06:47 +053054struct port {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 struct net_device *dev;
Himangi Saraogidb569582014-08-10 13:06:47 +053056 struct card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 spinlock_t lock; /* for wanxl_xmit */
58 int node; /* physical port #0 - 3 */
59 unsigned int clock_type;
60 int tx_in, tx_out;
61 struct sk_buff *tx_skbs[TX_BUFFERS];
Himangi Saraogidb569582014-08-10 13:06:47 +053062};
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64
Himangi Saraogidb569582014-08-10 13:06:47 +053065struct card_status {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 desc_t rx_descs[RX_QUEUE_LENGTH];
67 port_status_t port_status[4];
Himangi Saraogidb569582014-08-10 13:06:47 +053068};
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70
Himangi Saraogidb569582014-08-10 13:06:47 +053071struct card {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int n_ports; /* 1, 2 or 4 ports */
73 u8 irq;
74
75 u8 __iomem *plx; /* PLX PCI9060 virtual base address */
76 struct pci_dev *pdev; /* for pci_name(pdev) */
77 int rx_in;
78 struct sk_buff *rx_skbs[RX_QUEUE_LENGTH];
Himangi Saraogidb569582014-08-10 13:06:47 +053079 struct card_status *status; /* shared between host and card */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 dma_addr_t status_address;
Gustavo A. R. Silva37e12442020-02-26 15:23:17 -060081 struct port ports[]; /* 1 - 4 port structures follow */
Himangi Saraogidb569582014-08-10 13:06:47 +053082};
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84
85
Himangi Saraogidb569582014-08-10 13:06:47 +053086static inline struct port *dev_to_port(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Himangi Saraogidb569582014-08-10 13:06:47 +053088 return (struct port *)dev_to_hdlc(dev)->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91
Himangi Saraogidb569582014-08-10 13:06:47 +053092static inline port_status_t *get_status(struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 return &port->card->status->port_status[port->node];
95}
96
97
98#ifdef DEBUG_PCI
99static inline dma_addr_t pci_map_single_debug(struct pci_dev *pdev, void *ptr,
100 size_t size, int direction)
101{
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200102 dma_addr_t addr = dma_map_single(&pdev->dev, ptr, size, direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (addr + size > 0x100000000LL)
Joe Perches12a3bfe2011-06-26 19:01:28 +0000104 pr_crit("%s: pci_map_single() returned memory at 0x%llx!\n",
105 pci_name(pdev), (unsigned long long)addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return addr;
107}
108
109#undef pci_map_single
110#define pci_map_single pci_map_single_debug
111#endif
112
113
114/* Cable and/or personality module change interrupt service */
Himangi Saraogidb569582014-08-10 13:06:47 +0530115static inline void wanxl_cable_intr(struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 u32 value = get_status(port)->cable;
118 int valid = 1;
119 const char *cable, *pm, *dte = "", *dsr = "", *dcd = "";
120
121 switch(value & 0x7) {
122 case STATUS_CABLE_V35: cable = "V.35"; break;
123 case STATUS_CABLE_X21: cable = "X.21"; break;
124 case STATUS_CABLE_V24: cable = "V.24"; break;
125 case STATUS_CABLE_EIA530: cable = "EIA530"; break;
126 case STATUS_CABLE_NONE: cable = "no"; break;
127 default: cable = "invalid";
128 }
129
130 switch((value >> STATUS_CABLE_PM_SHIFT) & 0x7) {
131 case STATUS_CABLE_V35: pm = "V.35"; break;
132 case STATUS_CABLE_X21: pm = "X.21"; break;
133 case STATUS_CABLE_V24: pm = "V.24"; break;
134 case STATUS_CABLE_EIA530: pm = "EIA530"; break;
135 case STATUS_CABLE_NONE: pm = "no personality"; valid = 0; break;
136 default: pm = "invalid personality"; valid = 0;
137 }
138
139 if (valid) {
140 if ((value & 7) == ((value >> STATUS_CABLE_PM_SHIFT) & 7)) {
141 dsr = (value & STATUS_CABLE_DSR) ? ", DSR ON" :
142 ", DSR off";
143 dcd = (value & STATUS_CABLE_DCD) ? ", carrier ON" :
144 ", carrier off";
145 }
146 dte = (value & STATUS_CABLE_DCE) ? " DCE" : " DTE";
147 }
Joe Perches12a3bfe2011-06-26 19:01:28 +0000148 netdev_info(port->dev, "%s%s module, %s cable%s%s\n",
149 pm, dte, cable, dsr, dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Krzysztof Halasac2ce9202006-07-12 13:46:12 -0700151 if (value & STATUS_CABLE_DCD)
152 netif_carrier_on(port->dev);
153 else
154 netif_carrier_off(port->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157
158
159/* Transmit complete interrupt service */
Himangi Saraogidb569582014-08-10 13:06:47 +0530160static inline void wanxl_tx_intr(struct port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct net_device *dev = port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 while (1) {
164 desc_t *desc = &get_status(port)->tx_descs[port->tx_in];
165 struct sk_buff *skb = port->tx_skbs[port->tx_in];
166
167 switch (desc->stat) {
168 case PACKET_FULL:
169 case PACKET_EMPTY:
170 netif_wake_queue(dev);
171 return;
172
173 case PACKET_UNDERRUN:
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200174 dev->stats.tx_errors++;
175 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 break;
177
178 default:
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200179 dev->stats.tx_packets++;
180 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182 desc->stat = PACKET_EMPTY; /* Free descriptor */
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200183 dma_unmap_single(&port->card->pdev->dev, desc->address,
184 skb->len, DMA_TO_DEVICE);
Yang Wei73634c02019-02-25 23:01:50 +0800185 dev_consume_skb_irq(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 port->tx_in = (port->tx_in + 1) % TX_BUFFERS;
187 }
188}
189
190
191
192/* Receive complete interrupt service */
Himangi Saraogidb569582014-08-10 13:06:47 +0530193static inline void wanxl_rx_intr(struct card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 desc_t *desc;
196 while (desc = &card->status->rx_descs[card->rx_in],
197 desc->stat != PACKET_EMPTY) {
198 if ((desc->stat & PACKET_PORT_MASK) > card->n_ports)
Joe Perches12a3bfe2011-06-26 19:01:28 +0000199 pr_crit("%s: received packet for nonexistent port\n",
200 pci_name(card->pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 else {
202 struct sk_buff *skb = card->rx_skbs[card->rx_in];
Himangi Saraogidb569582014-08-10 13:06:47 +0530203 struct port *port = &card->ports[desc->stat &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 PACKET_PORT_MASK];
205 struct net_device *dev = port->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 if (!skb)
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200208 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 else {
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200210 dma_unmap_single(&card->pdev->dev,
211 desc->address, BUFFER_LENGTH,
212 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 skb_put(skb, desc->length);
214
215#ifdef DEBUG_PKT
216 printk(KERN_DEBUG "%s RX(%i):", dev->name,
217 skb->len);
218 debug_frame(skb);
219#endif
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200220 dev->stats.rx_packets++;
221 dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 skb->protocol = hdlc_type_trans(skb, dev);
223 netif_rx(skb);
224 skb = NULL;
225 }
226
227 if (!skb) {
228 skb = dev_alloc_skb(BUFFER_LENGTH);
229 desc->address = skb ?
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200230 dma_map_single(&card->pdev->dev,
231 skb->data,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 BUFFER_LENGTH,
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200233 DMA_FROM_DEVICE) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 card->rx_skbs[card->rx_in] = skb;
235 }
236 }
237 desc->stat = PACKET_EMPTY; /* Free descriptor */
238 card->rx_in = (card->rx_in + 1) % RX_QUEUE_LENGTH;
239 }
240}
241
242
243
David Howells7d12e782006-10-05 14:55:46 +0100244static irqreturn_t wanxl_intr(int irq, void* dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Himangi Saraogidb569582014-08-10 13:06:47 +0530246 struct card *card = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int i;
248 u32 stat;
249 int handled = 0;
250
251
252 while((stat = readl(card->plx + PLX_DOORBELL_FROM_CARD)) != 0) {
253 handled = 1;
254 writel(stat, card->plx + PLX_DOORBELL_FROM_CARD);
255
256 for (i = 0; i < card->n_ports; i++) {
257 if (stat & (1 << (DOORBELL_FROM_CARD_TX_0 + i)))
258 wanxl_tx_intr(&card->ports[i]);
259 if (stat & (1 << (DOORBELL_FROM_CARD_CABLE_0 + i)))
260 wanxl_cable_intr(&card->ports[i]);
261 }
262 if (stat & (1 << DOORBELL_FROM_CARD_RX))
263 wanxl_rx_intr(card);
264 }
265
266 return IRQ_RETVAL(handled);
267}
268
269
270
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000271static netdev_tx_t wanxl_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
Himangi Saraogidb569582014-08-10 13:06:47 +0530273 struct port *port = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 desc_t *desc;
275
276 spin_lock(&port->lock);
277
278 desc = &get_status(port)->tx_descs[port->tx_out];
279 if (desc->stat != PACKET_EMPTY) {
280 /* should never happen - previous xmit should stop queue */
281#ifdef DEBUG_PKT
282 printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name);
283#endif
284 netif_stop_queue(dev);
Dan Carpenter0311ee22011-06-28 20:29:51 +0000285 spin_unlock(&port->lock);
Patrick McHardy5b548142009-06-12 06:22:29 +0000286 return NETDEV_TX_BUSY; /* request packet to be queued */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
289#ifdef DEBUG_PKT
290 printk(KERN_DEBUG "%s TX(%i):", dev->name, skb->len);
291 debug_frame(skb);
292#endif
293
294 port->tx_skbs[port->tx_out] = skb;
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200295 desc->address = dma_map_single(&port->card->pdev->dev, skb->data,
296 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 desc->length = skb->len;
298 desc->stat = PACKET_FULL;
299 writel(1 << (DOORBELL_TO_CARD_TX_0 + port->node),
300 port->card->plx + PLX_DOORBELL_TO_CARD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 port->tx_out = (port->tx_out + 1) % TX_BUFFERS;
303
304 if (get_status(port)->tx_descs[port->tx_out].stat != PACKET_EMPTY) {
305 netif_stop_queue(dev);
306#ifdef DEBUG_PKT
307 printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name);
308#endif
309 }
310
311 spin_unlock(&port->lock);
Patrick McHardyec634fe2009-07-05 19:23:38 -0700312 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313}
314
315
316
317static int wanxl_attach(struct net_device *dev, unsigned short encoding,
318 unsigned short parity)
319{
Himangi Saraogidb569582014-08-10 13:06:47 +0530320 struct port *port = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 if (encoding != ENCODING_NRZ &&
323 encoding != ENCODING_NRZI)
324 return -EINVAL;
325
326 if (parity != PARITY_NONE &&
327 parity != PARITY_CRC32_PR1_CCITT &&
328 parity != PARITY_CRC16_PR1_CCITT &&
329 parity != PARITY_CRC32_PR0_CCITT &&
330 parity != PARITY_CRC16_PR0_CCITT)
331 return -EINVAL;
332
333 get_status(port)->encoding = encoding;
334 get_status(port)->parity = parity;
335 return 0;
336}
337
338
339
340static int wanxl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
341{
342 const size_t size = sizeof(sync_serial_settings);
343 sync_serial_settings line;
Himangi Saraogidb569582014-08-10 13:06:47 +0530344 struct port *port = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
346 if (cmd != SIOCWANDEV)
347 return hdlc_ioctl(dev, ifr, cmd);
348
349 switch (ifr->ifr_settings.type) {
350 case IF_GET_IFACE:
351 ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
352 if (ifr->ifr_settings.size < size) {
353 ifr->ifr_settings.size = size; /* data size wanted */
354 return -ENOBUFS;
355 }
Salva PeirĂł2b13d062013-10-16 12:46:50 +0200356 memset(&line, 0, sizeof(line));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 line.clock_type = get_status(port)->clocking;
358 line.clock_rate = 0;
359 line.loopback = 0;
360
361 if (copy_to_user(ifr->ifr_settings.ifs_ifsu.sync, &line, size))
362 return -EFAULT;
363 return 0;
364
365 case IF_IFACE_SYNC_SERIAL:
366 if (!capable(CAP_NET_ADMIN))
367 return -EPERM;
368 if (dev->flags & IFF_UP)
369 return -EBUSY;
370
371 if (copy_from_user(&line, ifr->ifr_settings.ifs_ifsu.sync,
372 size))
373 return -EFAULT;
374
375 if (line.clock_type != CLOCK_EXT &&
376 line.clock_type != CLOCK_TXFROMRX)
377 return -EINVAL; /* No such clock setting */
378
379 if (line.loopback != 0)
380 return -EINVAL;
381
382 get_status(port)->clocking = line.clock_type;
383 return 0;
384
385 default:
386 return hdlc_ioctl(dev, ifr, cmd);
387 }
388}
389
390
391
392static int wanxl_open(struct net_device *dev)
393{
Himangi Saraogidb569582014-08-10 13:06:47 +0530394 struct port *port = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 u8 __iomem *dbr = port->card->plx + PLX_DOORBELL_TO_CARD;
396 unsigned long timeout;
397 int i;
398
399 if (get_status(port)->open) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000400 netdev_err(dev, "port already open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return -EIO;
402 }
403 if ((i = hdlc_open(dev)) != 0)
404 return i;
405
406 port->tx_in = port->tx_out = 0;
407 for (i = 0; i < TX_BUFFERS; i++)
408 get_status(port)->tx_descs[i].stat = PACKET_EMPTY;
409 /* signal the card */
410 writel(1 << (DOORBELL_TO_CARD_OPEN_0 + port->node), dbr);
411
412 timeout = jiffies + HZ;
Hannes Ederd157e022008-12-22 09:17:55 +0000413 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (get_status(port)->open) {
415 netif_start_queue(dev);
416 return 0;
417 }
Hannes Ederd157e022008-12-22 09:17:55 +0000418 } while (time_after(timeout, jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Joe Perches12a3bfe2011-06-26 19:01:28 +0000420 netdev_err(dev, "unable to open port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* ask the card to close the port, should it be still alive */
422 writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node), dbr);
423 return -EFAULT;
424}
425
426
427
428static int wanxl_close(struct net_device *dev)
429{
Himangi Saraogidb569582014-08-10 13:06:47 +0530430 struct port *port = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned long timeout;
432 int i;
433
434 hdlc_close(dev);
435 /* signal the card */
436 writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node),
437 port->card->plx + PLX_DOORBELL_TO_CARD);
438
439 timeout = jiffies + HZ;
Hannes Ederd157e022008-12-22 09:17:55 +0000440 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (!get_status(port)->open)
442 break;
Hannes Ederd157e022008-12-22 09:17:55 +0000443 } while (time_after(timeout, jiffies));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 if (get_status(port)->open)
Joe Perches12a3bfe2011-06-26 19:01:28 +0000446 netdev_err(dev, "unable to close port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 netif_stop_queue(dev);
449
450 for (i = 0; i < TX_BUFFERS; i++) {
451 desc_t *desc = &get_status(port)->tx_descs[i];
452
453 if (desc->stat != PACKET_EMPTY) {
454 desc->stat = PACKET_EMPTY;
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200455 dma_unmap_single(&port->card->pdev->dev,
456 desc->address, port->tx_skbs[i]->len,
457 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dev_kfree_skb(port->tx_skbs[i]);
459 }
460 }
461 return 0;
462}
463
464
465
466static struct net_device_stats *wanxl_get_stats(struct net_device *dev)
467{
Himangi Saraogidb569582014-08-10 13:06:47 +0530468 struct port *port = dev_to_port(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Krzysztof Halasa198191c2008-06-30 23:26:53 +0200470 dev->stats.rx_over_errors = get_status(port)->rx_overruns;
471 dev->stats.rx_frame_errors = get_status(port)->rx_frame_errors;
472 dev->stats.rx_errors = dev->stats.rx_over_errors +
473 dev->stats.rx_frame_errors;
474 return &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
477
478
Himangi Saraogidb569582014-08-10 13:06:47 +0530479static int wanxl_puts_command(struct card *card, u32 cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 unsigned long timeout = jiffies + 5 * HZ;
482
483 writel(cmd, card->plx + PLX_MAILBOX_1);
484 do {
485 if (readl(card->plx + PLX_MAILBOX_1) == 0)
486 return 0;
487
488 schedule();
489 }while (time_after(timeout, jiffies));
490
491 return -1;
492}
493
494
495
Himangi Saraogidb569582014-08-10 13:06:47 +0530496static void wanxl_reset(struct card *card)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 u32 old_value = readl(card->plx + PLX_CONTROL) & ~PLX_CTL_RESET;
499
500 writel(0x80, card->plx + PLX_MAILBOX_0);
501 writel(old_value | PLX_CTL_RESET, card->plx + PLX_CONTROL);
502 readl(card->plx + PLX_CONTROL); /* wait for posted write */
503 udelay(1);
504 writel(old_value, card->plx + PLX_CONTROL);
505 readl(card->plx + PLX_CONTROL); /* wait for posted write */
506}
507
508
509
510static void wanxl_pci_remove_one(struct pci_dev *pdev)
511{
Himangi Saraogidb569582014-08-10 13:06:47 +0530512 struct card *card = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int i;
514
515 for (i = 0; i < card->n_ports; i++) {
516 unregister_hdlc_device(card->ports[i].dev);
517 free_netdev(card->ports[i].dev);
518 }
519
520 /* unregister and free all host resources */
521 if (card->irq)
522 free_irq(card->irq, card);
523
524 wanxl_reset(card);
525
526 for (i = 0; i < RX_QUEUE_LENGTH; i++)
527 if (card->rx_skbs[i]) {
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200528 dma_unmap_single(&card->pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 card->status->rx_descs[i].address,
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200530 BUFFER_LENGTH, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 dev_kfree_skb(card->rx_skbs[i]);
532 }
533
534 if (card->plx)
535 iounmap(card->plx);
536
537 if (card->status)
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200538 dma_free_coherent(&pdev->dev, sizeof(struct card_status),
539 card->status, card->status_address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 pci_release_regions(pdev);
542 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 kfree(card);
544}
545
546
547#include "wanxlfw.inc"
548
Krzysztof HaƂasa991990a2009-01-08 22:52:11 +0100549static const struct net_device_ops wanxl_ops = {
550 .ndo_open = wanxl_open,
551 .ndo_stop = wanxl_close,
Krzysztof HaƂasa991990a2009-01-08 22:52:11 +0100552 .ndo_start_xmit = hdlc_start_xmit,
553 .ndo_do_ioctl = wanxl_ioctl,
554 .ndo_get_stats = wanxl_get_stats,
555};
556
Bill Pembertonaeea6bb2012-12-03 09:24:19 -0500557static int wanxl_pci_init_one(struct pci_dev *pdev,
Greg Kroah-Hartman1dd06ae2012-12-06 14:30:56 +0000558 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Himangi Saraogidb569582014-08-10 13:06:47 +0530560 struct card *card;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 u32 ramsize, stat;
562 unsigned long timeout;
563 u32 plx_phy; /* PLX PCI base address */
564 u32 mem_phy; /* memory PCI base addr */
565 u8 __iomem *mem; /* memory virtual base addr */
Gustavo A. R. Silvaa3deec52019-02-07 21:16:48 -0600566 int i, ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568#ifndef MODULE
Joe Perches12a3bfe2011-06-26 19:01:28 +0000569 pr_info_once("%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#endif
571
572 i = pci_enable_device(pdev);
573 if (i)
574 return i;
575
576 /* QUICC can only access first 256 MB of host RAM directly,
577 but PLX9060 DMA does 32-bits for actual packet data transfers */
578
579 /* FIXME when PCI/DMA subsystems are fixed.
580 We set both dma_mask and consistent_dma_mask to 28 bits
581 and pray pci_alloc_consistent() will use this info. It should
582 work on most platforms */
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200583 if (dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(28)) ||
584 dma_set_mask(&pdev->dev, DMA_BIT_MASK(28))) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000585 pr_err("No usable DMA configuration\n");
Saurabh Sengar7e074af2015-12-12 00:58:19 +0530586 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return -EIO;
588 }
589
590 i = pci_request_regions(pdev, "wanXL");
591 if (i) {
592 pci_disable_device(pdev);
593 return i;
594 }
595
596 switch (pdev->device) {
597 case PCI_DEVICE_ID_SBE_WANXL100: ports = 1; break;
598 case PCI_DEVICE_ID_SBE_WANXL200: ports = 2; break;
599 default: ports = 4;
600 }
601
Gustavo A. R. Silvaa3deec52019-02-07 21:16:48 -0600602 card = kzalloc(struct_size(card, ports, ports), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 if (card == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 pci_release_regions(pdev);
605 pci_disable_device(pdev);
606 return -ENOBUFS;
607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 pci_set_drvdata(pdev, card);
610 card->pdev = pdev;
611
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200612 card->status = dma_alloc_coherent(&pdev->dev,
613 sizeof(struct card_status),
614 &card->status_address, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (card->status == NULL) {
616 wanxl_pci_remove_one(pdev);
617 return -ENOBUFS;
618 }
619
620#ifdef DEBUG_PCI
621 printk(KERN_DEBUG "wanXL %s: pci_alloc_consistent() returned memory"
622 " at 0x%LX\n", pci_name(pdev),
623 (unsigned long long)card->status_address);
624#endif
625
626 /* FIXME when PCI/DMA subsystems are fixed.
627 We set both dma_mask and consistent_dma_mask back to 32 bits
628 to indicate the card can do 32-bit DMA addressing */
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200629 if (dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32)) ||
630 dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000631 pr_err("No usable DMA configuration\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 wanxl_pci_remove_one(pdev);
633 return -EIO;
634 }
635
636 /* set up PLX mapping */
637 plx_phy = pci_resource_start(pdev, 0);
Krzysztof Halasa44460652006-06-22 22:29:28 +0200638
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100639 card->plx = ioremap(plx_phy, 0x70);
Krzysztof Halasa44460652006-06-22 22:29:28 +0200640 if (!card->plx) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000641 pr_err("ioremap() failed\n");
Krzysztof Halasa44460652006-06-22 22:29:28 +0200642 wanxl_pci_remove_one(pdev);
643 return -EFAULT;
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646#if RESET_WHILE_LOADING
647 wanxl_reset(card);
648#endif
649
650 timeout = jiffies + 20 * HZ;
651 while ((stat = readl(card->plx + PLX_MAILBOX_0)) != 0) {
652 if (time_before(timeout, jiffies)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000653 pr_warn("%s: timeout waiting for PUTS to complete\n",
654 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 wanxl_pci_remove_one(pdev);
656 return -ENODEV;
657 }
658
659 switch(stat & 0xC0) {
660 case 0x00: /* hmm - PUTS completed with non-zero code? */
661 case 0x80: /* PUTS still testing the hardware */
662 break;
663
664 default:
Joe Perches12a3bfe2011-06-26 19:01:28 +0000665 pr_warn("%s: PUTS test 0x%X failed\n",
666 pci_name(pdev), stat & 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 wanxl_pci_remove_one(pdev);
668 return -ENODEV;
669 }
670
671 schedule();
672 }
673
674 /* get on-board memory size (PUTS detects no more than 4 MB) */
675 ramsize = readl(card->plx + PLX_MAILBOX_2) & MBX2_MEMSZ_MASK;
676
677 /* set up on-board RAM mapping */
678 mem_phy = pci_resource_start(pdev, 2);
679
680
681 /* sanity check the board's reported memory size */
682 if (ramsize < BUFFERS_ADDR +
683 (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000684 pr_warn("%s: no enough on-board RAM (%u bytes detected, %u bytes required)\n",
685 pci_name(pdev), ramsize,
686 BUFFERS_ADDR +
687 (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 wanxl_pci_remove_one(pdev);
689 return -ENODEV;
690 }
691
692 if (wanxl_puts_command(card, MBX1_CMD_BSWAP)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000693 pr_warn("%s: unable to Set Byte Swap Mode\n", pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 wanxl_pci_remove_one(pdev);
695 return -ENODEV;
696 }
697
698 for (i = 0; i < RX_QUEUE_LENGTH; i++) {
699 struct sk_buff *skb = dev_alloc_skb(BUFFER_LENGTH);
700 card->rx_skbs[i] = skb;
701 if (skb)
702 card->status->rx_descs[i].address =
Christophe JAILLET24dd3772020-08-04 22:08:09 +0200703 dma_map_single(&card->pdev->dev, skb->data,
704 BUFFER_LENGTH, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100707 mem = ioremap(mem_phy, PDM_OFFSET + sizeof(firmware));
Krzysztof Halasa44460652006-06-22 22:29:28 +0200708 if (!mem) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000709 pr_err("ioremap() failed\n");
Krzysztof Halasa44460652006-06-22 22:29:28 +0200710 wanxl_pci_remove_one(pdev);
711 return -EFAULT;
712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 for (i = 0; i < sizeof(firmware); i += 4)
Al Viro90458402007-12-22 17:52:52 +0000715 writel(ntohl(*(__be32*)(firmware + i)), mem + PDM_OFFSET + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 for (i = 0; i < ports; i++)
718 writel(card->status_address +
719 (void *)&card->status->port_status[i] -
720 (void *)card->status, mem + PDM_OFFSET + 4 + i * 4);
721 writel(card->status_address, mem + PDM_OFFSET + 20);
722 writel(PDM_OFFSET, mem);
723 iounmap(mem);
724
725 writel(0, card->plx + PLX_MAILBOX_5);
726
727 if (wanxl_puts_command(card, MBX1_CMD_ABORTJ)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000728 pr_warn("%s: unable to Abort and Jump\n", pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 wanxl_pci_remove_one(pdev);
730 return -ENODEV;
731 }
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 timeout = jiffies + 5 * HZ;
734 do {
735 if ((stat = readl(card->plx + PLX_MAILBOX_5)) != 0)
736 break;
737 schedule();
738 }while (time_after(timeout, jiffies));
739
740 if (!stat) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000741 pr_warn("%s: timeout while initializing card firmware\n",
742 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 wanxl_pci_remove_one(pdev);
744 return -ENODEV;
745 }
746
747#if DETECT_RAM
748 ramsize = stat;
749#endif
750
Joe Perches12a3bfe2011-06-26 19:01:28 +0000751 pr_info("%s: at 0x%X, %u KB of RAM at 0x%X, irq %u\n",
752 pci_name(pdev), plx_phy, ramsize / 1024, mem_phy, pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Allocate IRQ */
Thomas Gleixner1fb9df52006-07-01 19:29:39 -0700755 if (request_irq(pdev->irq, wanxl_intr, IRQF_SHARED, "wanXL", card)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000756 pr_warn("%s: could not allocate IRQ%i\n",
757 pci_name(pdev), pdev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 wanxl_pci_remove_one(pdev);
759 return -EBUSY;
760 }
761 card->irq = pdev->irq;
762
763 for (i = 0; i < ports; i++) {
764 hdlc_device *hdlc;
Himangi Saraogidb569582014-08-10 13:06:47 +0530765 struct port *port = &card->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct net_device *dev = alloc_hdlcdev(port);
767 if (!dev) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000768 pr_err("%s: unable to allocate memory\n",
769 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 wanxl_pci_remove_one(pdev);
771 return -ENOMEM;
772 }
773
774 port->dev = dev;
775 hdlc = dev_to_hdlc(dev);
776 spin_lock_init(&port->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 dev->tx_queue_len = 50;
Krzysztof HaƂasa991990a2009-01-08 22:52:11 +0100778 dev->netdev_ops = &wanxl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 hdlc->attach = wanxl_attach;
780 hdlc->xmit = wanxl_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 port->card = card;
782 port->node = i;
783 get_status(port)->clocking = CLOCK_EXT;
784 if (register_hdlc_device(dev)) {
Joe Perches12a3bfe2011-06-26 19:01:28 +0000785 pr_err("%s: unable to register hdlc device\n",
786 pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 free_netdev(dev);
788 wanxl_pci_remove_one(pdev);
789 return -ENOBUFS;
790 }
791 card->n_ports++;
792 }
793
Joe Perches12a3bfe2011-06-26 19:01:28 +0000794 pr_info("%s: port", pci_name(pdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 for (i = 0; i < ports; i++)
Joe Perches12a3bfe2011-06-26 19:01:28 +0000796 pr_cont("%s #%i: %s",
797 i ? "," : "", i, card->ports[i].dev->name);
798 pr_cont("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 for (i = 0; i < ports; i++)
801 wanxl_cable_intr(&card->ports[i]); /* get carrier status etc.*/
802
803 return 0;
804}
805
Benoit Taine9baa3c32014-08-08 15:56:03 +0200806static const struct pci_device_id wanxl_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL100, PCI_ANY_ID,
808 PCI_ANY_ID, 0, 0, 0 },
809 { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL200, PCI_ANY_ID,
810 PCI_ANY_ID, 0, 0, 0 },
811 { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL400, PCI_ANY_ID,
812 PCI_ANY_ID, 0, 0, 0 },
813 { 0, }
814};
815
816
817static struct pci_driver wanxl_pci_driver = {
818 .name = "wanXL",
819 .id_table = wanxl_pci_tbl,
820 .probe = wanxl_pci_init_one,
821 .remove = wanxl_pci_remove_one,
822};
823
824
825static int __init wanxl_init_module(void)
826{
827#ifdef MODULE
Joe Perches12a3bfe2011-06-26 19:01:28 +0000828 pr_info("%s\n", version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829#endif
Jeff Garzik29917622006-08-19 17:48:59 -0400830 return pci_register_driver(&wanxl_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831}
832
833static void __exit wanxl_cleanup_module(void)
834{
835 pci_unregister_driver(&wanxl_pci_driver);
836}
837
838
839MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
840MODULE_DESCRIPTION("SBE Inc. wanXL serial port driver");
841MODULE_LICENSE("GPL v2");
842MODULE_DEVICE_TABLE(pci, wanxl_pci_tbl);
843
844module_init(wanxl_init_module);
845module_exit(wanxl_cleanup_module);