Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * wanXL serial card driver for Linux |
| 3 | * host part |
| 4 | * |
| 5 | * Copyright (C) 2003 Krzysztof Halasa <khc@pm.waw.pl> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of version 2 of the GNU General Public License |
| 9 | * as published by the Free Software Foundation. |
| 10 | * |
| 11 | * Status: |
| 12 | * - Only DTE (external clock) support with NRZ and NRZI encodings |
| 13 | * - wanXL100 will require minor driver modifications, no access to hw |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/sched.h> |
| 20 | #include <linux/types.h> |
| 21 | #include <linux/fcntl.h> |
| 22 | #include <linux/string.h> |
| 23 | #include <linux/errno.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/ioport.h> |
| 26 | #include <linux/netdevice.h> |
| 27 | #include <linux/hdlc.h> |
| 28 | #include <linux/pci.h> |
Domen Puncer | 1e7f0bd | 2005-06-26 18:22:14 -0400 | [diff] [blame] | 29 | #include <linux/dma-mapping.h> |
Al Viro | 164006d | 2005-11-30 23:47:05 -0500 | [diff] [blame] | 30 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include "wanxl.h" |
| 34 | |
| 35 | static 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 | |
| 54 | typedef struct { |
| 55 | struct net_device *dev; |
| 56 | struct card_t *card; |
| 57 | 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]; |
| 62 | }port_t; |
| 63 | |
| 64 | |
| 65 | typedef struct { |
| 66 | desc_t rx_descs[RX_QUEUE_LENGTH]; |
| 67 | port_status_t port_status[4]; |
| 68 | }card_status_t; |
| 69 | |
| 70 | |
| 71 | typedef struct card_t { |
| 72 | 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]; |
| 79 | card_status_t *status; /* shared between host and card */ |
| 80 | dma_addr_t status_address; |
| 81 | port_t ports[0]; /* 1 - 4 port_t structures follow */ |
| 82 | }card_t; |
| 83 | |
| 84 | |
| 85 | |
| 86 | static inline port_t* dev_to_port(struct net_device *dev) |
| 87 | { |
| 88 | return (port_t *)dev_to_hdlc(dev)->priv; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | static inline port_status_t* get_status(port_t *port) |
| 93 | { |
| 94 | return &port->card->status->port_status[port->node]; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | #ifdef DEBUG_PCI |
| 99 | static inline dma_addr_t pci_map_single_debug(struct pci_dev *pdev, void *ptr, |
| 100 | size_t size, int direction) |
| 101 | { |
| 102 | dma_addr_t addr = pci_map_single(pdev, ptr, size, direction); |
| 103 | if (addr + size > 0x100000000LL) |
| 104 | printk(KERN_CRIT "wanXL %s: pci_map_single() returned memory" |
| 105 | " at 0x%LX!\n", pci_name(pdev), |
| 106 | (unsigned long long)addr); |
| 107 | return addr; |
| 108 | } |
| 109 | |
| 110 | #undef pci_map_single |
| 111 | #define pci_map_single pci_map_single_debug |
| 112 | #endif |
| 113 | |
| 114 | |
| 115 | /* Cable and/or personality module change interrupt service */ |
| 116 | static inline void wanxl_cable_intr(port_t *port) |
| 117 | { |
| 118 | u32 value = get_status(port)->cable; |
| 119 | int valid = 1; |
| 120 | const char *cable, *pm, *dte = "", *dsr = "", *dcd = ""; |
| 121 | |
| 122 | switch(value & 0x7) { |
| 123 | case STATUS_CABLE_V35: cable = "V.35"; break; |
| 124 | case STATUS_CABLE_X21: cable = "X.21"; break; |
| 125 | case STATUS_CABLE_V24: cable = "V.24"; break; |
| 126 | case STATUS_CABLE_EIA530: cable = "EIA530"; break; |
| 127 | case STATUS_CABLE_NONE: cable = "no"; break; |
| 128 | default: cable = "invalid"; |
| 129 | } |
| 130 | |
| 131 | switch((value >> STATUS_CABLE_PM_SHIFT) & 0x7) { |
| 132 | case STATUS_CABLE_V35: pm = "V.35"; break; |
| 133 | case STATUS_CABLE_X21: pm = "X.21"; break; |
| 134 | case STATUS_CABLE_V24: pm = "V.24"; break; |
| 135 | case STATUS_CABLE_EIA530: pm = "EIA530"; break; |
| 136 | case STATUS_CABLE_NONE: pm = "no personality"; valid = 0; break; |
| 137 | default: pm = "invalid personality"; valid = 0; |
| 138 | } |
| 139 | |
| 140 | if (valid) { |
| 141 | if ((value & 7) == ((value >> STATUS_CABLE_PM_SHIFT) & 7)) { |
| 142 | dsr = (value & STATUS_CABLE_DSR) ? ", DSR ON" : |
| 143 | ", DSR off"; |
| 144 | dcd = (value & STATUS_CABLE_DCD) ? ", carrier ON" : |
| 145 | ", carrier off"; |
| 146 | } |
| 147 | dte = (value & STATUS_CABLE_DCE) ? " DCE" : " DTE"; |
| 148 | } |
| 149 | printk(KERN_INFO "%s: %s%s module, %s cable%s%s\n", |
| 150 | port->dev->name, pm, dte, cable, dsr, dcd); |
| 151 | |
Krzysztof Halasa | c2ce920 | 2006-07-12 13:46:12 -0700 | [diff] [blame] | 152 | if (value & STATUS_CABLE_DCD) |
| 153 | netif_carrier_on(port->dev); |
| 154 | else |
| 155 | netif_carrier_off(port->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
| 159 | |
| 160 | /* Transmit complete interrupt service */ |
| 161 | static inline void wanxl_tx_intr(port_t *port) |
| 162 | { |
| 163 | struct net_device *dev = port->dev; |
| 164 | struct net_device_stats *stats = hdlc_stats(dev); |
| 165 | while (1) { |
| 166 | desc_t *desc = &get_status(port)->tx_descs[port->tx_in]; |
| 167 | struct sk_buff *skb = port->tx_skbs[port->tx_in]; |
| 168 | |
| 169 | switch (desc->stat) { |
| 170 | case PACKET_FULL: |
| 171 | case PACKET_EMPTY: |
| 172 | netif_wake_queue(dev); |
| 173 | return; |
| 174 | |
| 175 | case PACKET_UNDERRUN: |
| 176 | stats->tx_errors++; |
| 177 | stats->tx_fifo_errors++; |
| 178 | break; |
| 179 | |
| 180 | default: |
| 181 | stats->tx_packets++; |
| 182 | stats->tx_bytes += skb->len; |
| 183 | } |
| 184 | desc->stat = PACKET_EMPTY; /* Free descriptor */ |
| 185 | pci_unmap_single(port->card->pdev, desc->address, skb->len, |
| 186 | PCI_DMA_TODEVICE); |
| 187 | dev_kfree_skb_irq(skb); |
| 188 | port->tx_in = (port->tx_in + 1) % TX_BUFFERS; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | |
| 193 | |
| 194 | /* Receive complete interrupt service */ |
| 195 | static inline void wanxl_rx_intr(card_t *card) |
| 196 | { |
| 197 | desc_t *desc; |
| 198 | while (desc = &card->status->rx_descs[card->rx_in], |
| 199 | desc->stat != PACKET_EMPTY) { |
| 200 | if ((desc->stat & PACKET_PORT_MASK) > card->n_ports) |
| 201 | printk(KERN_CRIT "wanXL %s: received packet for" |
| 202 | " nonexistent port\n", pci_name(card->pdev)); |
| 203 | else { |
| 204 | struct sk_buff *skb = card->rx_skbs[card->rx_in]; |
| 205 | port_t *port = &card->ports[desc->stat & |
| 206 | PACKET_PORT_MASK]; |
| 207 | struct net_device *dev = port->dev; |
| 208 | struct net_device_stats *stats = hdlc_stats(dev); |
| 209 | |
| 210 | if (!skb) |
| 211 | stats->rx_dropped++; |
| 212 | else { |
| 213 | pci_unmap_single(card->pdev, desc->address, |
| 214 | BUFFER_LENGTH, |
| 215 | PCI_DMA_FROMDEVICE); |
| 216 | skb_put(skb, desc->length); |
| 217 | |
| 218 | #ifdef DEBUG_PKT |
| 219 | printk(KERN_DEBUG "%s RX(%i):", dev->name, |
| 220 | skb->len); |
| 221 | debug_frame(skb); |
| 222 | #endif |
| 223 | stats->rx_packets++; |
| 224 | stats->rx_bytes += skb->len; |
| 225 | dev->last_rx = jiffies; |
| 226 | skb->protocol = hdlc_type_trans(skb, dev); |
| 227 | netif_rx(skb); |
| 228 | skb = NULL; |
| 229 | } |
| 230 | |
| 231 | if (!skb) { |
| 232 | skb = dev_alloc_skb(BUFFER_LENGTH); |
| 233 | desc->address = skb ? |
| 234 | pci_map_single(card->pdev, skb->data, |
| 235 | BUFFER_LENGTH, |
| 236 | PCI_DMA_FROMDEVICE) : 0; |
| 237 | card->rx_skbs[card->rx_in] = skb; |
| 238 | } |
| 239 | } |
| 240 | desc->stat = PACKET_EMPTY; /* Free descriptor */ |
| 241 | card->rx_in = (card->rx_in + 1) % RX_QUEUE_LENGTH; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | |
| 246 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 247 | static irqreturn_t wanxl_intr(int irq, void* dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | card_t *card = dev_id; |
| 250 | int i; |
| 251 | u32 stat; |
| 252 | int handled = 0; |
| 253 | |
| 254 | |
| 255 | while((stat = readl(card->plx + PLX_DOORBELL_FROM_CARD)) != 0) { |
| 256 | handled = 1; |
| 257 | writel(stat, card->plx + PLX_DOORBELL_FROM_CARD); |
| 258 | |
| 259 | for (i = 0; i < card->n_ports; i++) { |
| 260 | if (stat & (1 << (DOORBELL_FROM_CARD_TX_0 + i))) |
| 261 | wanxl_tx_intr(&card->ports[i]); |
| 262 | if (stat & (1 << (DOORBELL_FROM_CARD_CABLE_0 + i))) |
| 263 | wanxl_cable_intr(&card->ports[i]); |
| 264 | } |
| 265 | if (stat & (1 << DOORBELL_FROM_CARD_RX)) |
| 266 | wanxl_rx_intr(card); |
| 267 | } |
| 268 | |
| 269 | return IRQ_RETVAL(handled); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | |
| 274 | static int wanxl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 275 | { |
| 276 | port_t *port = dev_to_port(dev); |
| 277 | desc_t *desc; |
| 278 | |
| 279 | spin_lock(&port->lock); |
| 280 | |
| 281 | desc = &get_status(port)->tx_descs[port->tx_out]; |
| 282 | if (desc->stat != PACKET_EMPTY) { |
| 283 | /* should never happen - previous xmit should stop queue */ |
| 284 | #ifdef DEBUG_PKT |
| 285 | printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name); |
| 286 | #endif |
| 287 | netif_stop_queue(dev); |
| 288 | spin_unlock_irq(&port->lock); |
| 289 | return 1; /* request packet to be queued */ |
| 290 | } |
| 291 | |
| 292 | #ifdef DEBUG_PKT |
| 293 | printk(KERN_DEBUG "%s TX(%i):", dev->name, skb->len); |
| 294 | debug_frame(skb); |
| 295 | #endif |
| 296 | |
| 297 | port->tx_skbs[port->tx_out] = skb; |
| 298 | desc->address = pci_map_single(port->card->pdev, skb->data, skb->len, |
| 299 | PCI_DMA_TODEVICE); |
| 300 | desc->length = skb->len; |
| 301 | desc->stat = PACKET_FULL; |
| 302 | writel(1 << (DOORBELL_TO_CARD_TX_0 + port->node), |
| 303 | port->card->plx + PLX_DOORBELL_TO_CARD); |
| 304 | dev->trans_start = jiffies; |
| 305 | |
| 306 | port->tx_out = (port->tx_out + 1) % TX_BUFFERS; |
| 307 | |
| 308 | if (get_status(port)->tx_descs[port->tx_out].stat != PACKET_EMPTY) { |
| 309 | netif_stop_queue(dev); |
| 310 | #ifdef DEBUG_PKT |
| 311 | printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name); |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | spin_unlock(&port->lock); |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | static int wanxl_attach(struct net_device *dev, unsigned short encoding, |
| 322 | unsigned short parity) |
| 323 | { |
| 324 | port_t *port = dev_to_port(dev); |
| 325 | |
| 326 | if (encoding != ENCODING_NRZ && |
| 327 | encoding != ENCODING_NRZI) |
| 328 | return -EINVAL; |
| 329 | |
| 330 | if (parity != PARITY_NONE && |
| 331 | parity != PARITY_CRC32_PR1_CCITT && |
| 332 | parity != PARITY_CRC16_PR1_CCITT && |
| 333 | parity != PARITY_CRC32_PR0_CCITT && |
| 334 | parity != PARITY_CRC16_PR0_CCITT) |
| 335 | return -EINVAL; |
| 336 | |
| 337 | get_status(port)->encoding = encoding; |
| 338 | get_status(port)->parity = parity; |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | |
| 343 | |
| 344 | static int wanxl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 345 | { |
| 346 | const size_t size = sizeof(sync_serial_settings); |
| 347 | sync_serial_settings line; |
| 348 | port_t *port = dev_to_port(dev); |
| 349 | |
| 350 | if (cmd != SIOCWANDEV) |
| 351 | return hdlc_ioctl(dev, ifr, cmd); |
| 352 | |
| 353 | switch (ifr->ifr_settings.type) { |
| 354 | case IF_GET_IFACE: |
| 355 | ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL; |
| 356 | if (ifr->ifr_settings.size < size) { |
| 357 | ifr->ifr_settings.size = size; /* data size wanted */ |
| 358 | return -ENOBUFS; |
| 359 | } |
| 360 | line.clock_type = get_status(port)->clocking; |
| 361 | line.clock_rate = 0; |
| 362 | line.loopback = 0; |
| 363 | |
| 364 | if (copy_to_user(ifr->ifr_settings.ifs_ifsu.sync, &line, size)) |
| 365 | return -EFAULT; |
| 366 | return 0; |
| 367 | |
| 368 | case IF_IFACE_SYNC_SERIAL: |
| 369 | if (!capable(CAP_NET_ADMIN)) |
| 370 | return -EPERM; |
| 371 | if (dev->flags & IFF_UP) |
| 372 | return -EBUSY; |
| 373 | |
| 374 | if (copy_from_user(&line, ifr->ifr_settings.ifs_ifsu.sync, |
| 375 | size)) |
| 376 | return -EFAULT; |
| 377 | |
| 378 | if (line.clock_type != CLOCK_EXT && |
| 379 | line.clock_type != CLOCK_TXFROMRX) |
| 380 | return -EINVAL; /* No such clock setting */ |
| 381 | |
| 382 | if (line.loopback != 0) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | get_status(port)->clocking = line.clock_type; |
| 386 | return 0; |
| 387 | |
| 388 | default: |
| 389 | return hdlc_ioctl(dev, ifr, cmd); |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | |
| 394 | |
| 395 | static int wanxl_open(struct net_device *dev) |
| 396 | { |
| 397 | port_t *port = dev_to_port(dev); |
| 398 | u8 __iomem *dbr = port->card->plx + PLX_DOORBELL_TO_CARD; |
| 399 | unsigned long timeout; |
| 400 | int i; |
| 401 | |
| 402 | if (get_status(port)->open) { |
| 403 | printk(KERN_ERR "%s: port already open\n", dev->name); |
| 404 | return -EIO; |
| 405 | } |
| 406 | if ((i = hdlc_open(dev)) != 0) |
| 407 | return i; |
| 408 | |
| 409 | port->tx_in = port->tx_out = 0; |
| 410 | for (i = 0; i < TX_BUFFERS; i++) |
| 411 | get_status(port)->tx_descs[i].stat = PACKET_EMPTY; |
| 412 | /* signal the card */ |
| 413 | writel(1 << (DOORBELL_TO_CARD_OPEN_0 + port->node), dbr); |
| 414 | |
| 415 | timeout = jiffies + HZ; |
| 416 | do |
| 417 | if (get_status(port)->open) { |
| 418 | netif_start_queue(dev); |
| 419 | return 0; |
| 420 | } |
| 421 | while (time_after(timeout, jiffies)); |
| 422 | |
| 423 | printk(KERN_ERR "%s: unable to open port\n", dev->name); |
| 424 | /* ask the card to close the port, should it be still alive */ |
| 425 | writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node), dbr); |
| 426 | return -EFAULT; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | |
| 431 | static int wanxl_close(struct net_device *dev) |
| 432 | { |
| 433 | port_t *port = dev_to_port(dev); |
| 434 | unsigned long timeout; |
| 435 | int i; |
| 436 | |
| 437 | hdlc_close(dev); |
| 438 | /* signal the card */ |
| 439 | writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node), |
| 440 | port->card->plx + PLX_DOORBELL_TO_CARD); |
| 441 | |
| 442 | timeout = jiffies + HZ; |
| 443 | do |
| 444 | if (!get_status(port)->open) |
| 445 | break; |
| 446 | while (time_after(timeout, jiffies)); |
| 447 | |
| 448 | if (get_status(port)->open) |
| 449 | printk(KERN_ERR "%s: unable to close port\n", dev->name); |
| 450 | |
| 451 | netif_stop_queue(dev); |
| 452 | |
| 453 | for (i = 0; i < TX_BUFFERS; i++) { |
| 454 | desc_t *desc = &get_status(port)->tx_descs[i]; |
| 455 | |
| 456 | if (desc->stat != PACKET_EMPTY) { |
| 457 | desc->stat = PACKET_EMPTY; |
| 458 | pci_unmap_single(port->card->pdev, desc->address, |
| 459 | port->tx_skbs[i]->len, |
| 460 | PCI_DMA_TODEVICE); |
| 461 | dev_kfree_skb(port->tx_skbs[i]); |
| 462 | } |
| 463 | } |
| 464 | return 0; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | |
| 469 | static struct net_device_stats *wanxl_get_stats(struct net_device *dev) |
| 470 | { |
| 471 | struct net_device_stats *stats = hdlc_stats(dev); |
| 472 | port_t *port = dev_to_port(dev); |
| 473 | |
| 474 | stats->rx_over_errors = get_status(port)->rx_overruns; |
| 475 | stats->rx_frame_errors = get_status(port)->rx_frame_errors; |
| 476 | stats->rx_errors = stats->rx_over_errors + stats->rx_frame_errors; |
| 477 | return stats; |
| 478 | } |
| 479 | |
| 480 | |
| 481 | |
| 482 | static int wanxl_puts_command(card_t *card, u32 cmd) |
| 483 | { |
| 484 | unsigned long timeout = jiffies + 5 * HZ; |
| 485 | |
| 486 | writel(cmd, card->plx + PLX_MAILBOX_1); |
| 487 | do { |
| 488 | if (readl(card->plx + PLX_MAILBOX_1) == 0) |
| 489 | return 0; |
| 490 | |
| 491 | schedule(); |
| 492 | }while (time_after(timeout, jiffies)); |
| 493 | |
| 494 | return -1; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | |
| 499 | static void wanxl_reset(card_t *card) |
| 500 | { |
| 501 | u32 old_value = readl(card->plx + PLX_CONTROL) & ~PLX_CTL_RESET; |
| 502 | |
| 503 | writel(0x80, card->plx + PLX_MAILBOX_0); |
| 504 | writel(old_value | PLX_CTL_RESET, card->plx + PLX_CONTROL); |
| 505 | readl(card->plx + PLX_CONTROL); /* wait for posted write */ |
| 506 | udelay(1); |
| 507 | writel(old_value, card->plx + PLX_CONTROL); |
| 508 | readl(card->plx + PLX_CONTROL); /* wait for posted write */ |
| 509 | } |
| 510 | |
| 511 | |
| 512 | |
| 513 | static void wanxl_pci_remove_one(struct pci_dev *pdev) |
| 514 | { |
| 515 | card_t *card = pci_get_drvdata(pdev); |
| 516 | int i; |
| 517 | |
| 518 | for (i = 0; i < card->n_ports; i++) { |
| 519 | unregister_hdlc_device(card->ports[i].dev); |
| 520 | free_netdev(card->ports[i].dev); |
| 521 | } |
| 522 | |
| 523 | /* unregister and free all host resources */ |
| 524 | if (card->irq) |
| 525 | free_irq(card->irq, card); |
| 526 | |
| 527 | wanxl_reset(card); |
| 528 | |
| 529 | for (i = 0; i < RX_QUEUE_LENGTH; i++) |
| 530 | if (card->rx_skbs[i]) { |
| 531 | pci_unmap_single(card->pdev, |
| 532 | card->status->rx_descs[i].address, |
| 533 | BUFFER_LENGTH, PCI_DMA_FROMDEVICE); |
| 534 | dev_kfree_skb(card->rx_skbs[i]); |
| 535 | } |
| 536 | |
| 537 | if (card->plx) |
| 538 | iounmap(card->plx); |
| 539 | |
| 540 | if (card->status) |
| 541 | pci_free_consistent(pdev, sizeof(card_status_t), |
| 542 | card->status, card->status_address); |
| 543 | |
| 544 | pci_release_regions(pdev); |
| 545 | pci_disable_device(pdev); |
| 546 | pci_set_drvdata(pdev, NULL); |
| 547 | kfree(card); |
| 548 | } |
| 549 | |
| 550 | |
| 551 | #include "wanxlfw.inc" |
| 552 | |
| 553 | static int __devinit wanxl_pci_init_one(struct pci_dev *pdev, |
| 554 | const struct pci_device_id *ent) |
| 555 | { |
| 556 | card_t *card; |
| 557 | u32 ramsize, stat; |
| 558 | unsigned long timeout; |
| 559 | u32 plx_phy; /* PLX PCI base address */ |
| 560 | u32 mem_phy; /* memory PCI base addr */ |
| 561 | u8 __iomem *mem; /* memory virtual base addr */ |
| 562 | int i, ports, alloc_size; |
| 563 | |
| 564 | #ifndef MODULE |
| 565 | static int printed_version; |
| 566 | if (!printed_version) { |
| 567 | printed_version++; |
| 568 | printk(KERN_INFO "%s\n", version); |
| 569 | } |
| 570 | #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 */ |
Matthias Gehre | 910638a | 2006-03-28 01:56:48 -0800 | [diff] [blame] | 583 | if (pci_set_consistent_dma_mask(pdev, DMA_28BIT_MASK) || |
| 584 | pci_set_dma_mask(pdev, DMA_28BIT_MASK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | printk(KERN_ERR "wanXL: No usable DMA configuration\n"); |
| 586 | return -EIO; |
| 587 | } |
| 588 | |
| 589 | i = pci_request_regions(pdev, "wanXL"); |
| 590 | if (i) { |
| 591 | pci_disable_device(pdev); |
| 592 | return i; |
| 593 | } |
| 594 | |
| 595 | switch (pdev->device) { |
| 596 | case PCI_DEVICE_ID_SBE_WANXL100: ports = 1; break; |
| 597 | case PCI_DEVICE_ID_SBE_WANXL200: ports = 2; break; |
| 598 | default: ports = 4; |
| 599 | } |
| 600 | |
| 601 | alloc_size = sizeof(card_t) + ports * sizeof(port_t); |
| 602 | card = kmalloc(alloc_size, GFP_KERNEL); |
| 603 | if (card == NULL) { |
| 604 | printk(KERN_ERR "wanXL %s: unable to allocate memory\n", |
| 605 | pci_name(pdev)); |
| 606 | pci_release_regions(pdev); |
| 607 | pci_disable_device(pdev); |
| 608 | return -ENOBUFS; |
| 609 | } |
| 610 | memset(card, 0, alloc_size); |
| 611 | |
| 612 | pci_set_drvdata(pdev, card); |
| 613 | card->pdev = pdev; |
| 614 | |
| 615 | card->status = pci_alloc_consistent(pdev, sizeof(card_status_t), |
| 616 | &card->status_address); |
| 617 | if (card->status == NULL) { |
| 618 | wanxl_pci_remove_one(pdev); |
| 619 | return -ENOBUFS; |
| 620 | } |
| 621 | |
| 622 | #ifdef DEBUG_PCI |
| 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); |
| 626 | #endif |
| 627 | |
| 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 */ |
Domen Puncer | 1e7f0bd | 2005-06-26 18:22:14 -0400 | [diff] [blame] | 631 | if (pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) || |
| 632 | pci_set_dma_mask(pdev, DMA_32BIT_MASK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | printk(KERN_ERR "wanXL: No usable DMA configuration\n"); |
| 634 | wanxl_pci_remove_one(pdev); |
| 635 | return -EIO; |
| 636 | } |
| 637 | |
| 638 | /* set up PLX mapping */ |
| 639 | plx_phy = pci_resource_start(pdev, 0); |
Krzysztof Halasa | 4446065 | 2006-06-22 22:29:28 +0200 | [diff] [blame] | 640 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | card->plx = ioremap_nocache(plx_phy, 0x70); |
Krzysztof Halasa | 4446065 | 2006-06-22 22:29:28 +0200 | [diff] [blame] | 642 | if (!card->plx) { |
| 643 | printk(KERN_ERR "wanxl: ioremap() failed\n"); |
| 644 | wanxl_pci_remove_one(pdev); |
| 645 | return -EFAULT; |
| 646 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | #if RESET_WHILE_LOADING |
| 649 | wanxl_reset(card); |
| 650 | #endif |
| 651 | |
| 652 | timeout = jiffies + 20 * HZ; |
| 653 | while ((stat = readl(card->plx + PLX_MAILBOX_0)) != 0) { |
| 654 | if (time_before(timeout, jiffies)) { |
| 655 | printk(KERN_WARNING "wanXL %s: timeout waiting for" |
| 656 | " PUTS to complete\n", pci_name(pdev)); |
| 657 | wanxl_pci_remove_one(pdev); |
| 658 | return -ENODEV; |
| 659 | } |
| 660 | |
| 661 | switch(stat & 0xC0) { |
| 662 | case 0x00: /* hmm - PUTS completed with non-zero code? */ |
| 663 | case 0x80: /* PUTS still testing the hardware */ |
| 664 | break; |
| 665 | |
| 666 | default: |
| 667 | printk(KERN_WARNING "wanXL %s: PUTS test 0x%X" |
| 668 | " failed\n", pci_name(pdev), stat & 0x30); |
| 669 | wanxl_pci_remove_one(pdev); |
| 670 | return -ENODEV; |
| 671 | } |
| 672 | |
| 673 | schedule(); |
| 674 | } |
| 675 | |
| 676 | /* get on-board memory size (PUTS detects no more than 4 MB) */ |
| 677 | ramsize = readl(card->plx + PLX_MAILBOX_2) & MBX2_MEMSZ_MASK; |
| 678 | |
| 679 | /* set up on-board RAM mapping */ |
| 680 | mem_phy = pci_resource_start(pdev, 2); |
| 681 | |
| 682 | |
| 683 | /* sanity check the board's reported memory size */ |
| 684 | if (ramsize < BUFFERS_ADDR + |
| 685 | (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports) { |
| 686 | printk(KERN_WARNING "wanXL %s: no enough on-board RAM" |
| 687 | " (%u bytes detected, %u bytes required)\n", |
| 688 | pci_name(pdev), ramsize, BUFFERS_ADDR + |
| 689 | (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports); |
| 690 | wanxl_pci_remove_one(pdev); |
| 691 | return -ENODEV; |
| 692 | } |
| 693 | |
| 694 | if (wanxl_puts_command(card, MBX1_CMD_BSWAP)) { |
| 695 | printk(KERN_WARNING "wanXL %s: unable to Set Byte Swap" |
| 696 | " Mode\n", pci_name(pdev)); |
| 697 | wanxl_pci_remove_one(pdev); |
| 698 | return -ENODEV; |
| 699 | } |
| 700 | |
| 701 | for (i = 0; i < RX_QUEUE_LENGTH; i++) { |
| 702 | struct sk_buff *skb = dev_alloc_skb(BUFFER_LENGTH); |
| 703 | card->rx_skbs[i] = skb; |
| 704 | if (skb) |
| 705 | card->status->rx_descs[i].address = |
| 706 | pci_map_single(card->pdev, skb->data, |
| 707 | BUFFER_LENGTH, |
| 708 | PCI_DMA_FROMDEVICE); |
| 709 | } |
| 710 | |
| 711 | mem = ioremap_nocache(mem_phy, PDM_OFFSET + sizeof(firmware)); |
Krzysztof Halasa | 4446065 | 2006-06-22 22:29:28 +0200 | [diff] [blame] | 712 | if (!mem) { |
| 713 | printk(KERN_ERR "wanxl: ioremap() failed\n"); |
| 714 | wanxl_pci_remove_one(pdev); |
| 715 | return -EFAULT; |
| 716 | } |
| 717 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | for (i = 0; i < sizeof(firmware); i += 4) |
| 719 | writel(htonl(*(u32*)(firmware + i)), mem + PDM_OFFSET + i); |
| 720 | |
| 721 | for (i = 0; i < ports; i++) |
| 722 | writel(card->status_address + |
| 723 | (void *)&card->status->port_status[i] - |
| 724 | (void *)card->status, mem + PDM_OFFSET + 4 + i * 4); |
| 725 | writel(card->status_address, mem + PDM_OFFSET + 20); |
| 726 | writel(PDM_OFFSET, mem); |
| 727 | iounmap(mem); |
| 728 | |
| 729 | writel(0, card->plx + PLX_MAILBOX_5); |
| 730 | |
| 731 | if (wanxl_puts_command(card, MBX1_CMD_ABORTJ)) { |
| 732 | printk(KERN_WARNING "wanXL %s: unable to Abort and Jump\n", |
| 733 | pci_name(pdev)); |
| 734 | wanxl_pci_remove_one(pdev); |
| 735 | return -ENODEV; |
| 736 | } |
| 737 | |
| 738 | stat = 0; |
| 739 | timeout = jiffies + 5 * HZ; |
| 740 | do { |
| 741 | if ((stat = readl(card->plx + PLX_MAILBOX_5)) != 0) |
| 742 | break; |
| 743 | schedule(); |
| 744 | }while (time_after(timeout, jiffies)); |
| 745 | |
| 746 | if (!stat) { |
| 747 | printk(KERN_WARNING "wanXL %s: timeout while initializing card" |
| 748 | "firmware\n", pci_name(pdev)); |
| 749 | wanxl_pci_remove_one(pdev); |
| 750 | return -ENODEV; |
| 751 | } |
| 752 | |
| 753 | #if DETECT_RAM |
| 754 | ramsize = stat; |
| 755 | #endif |
| 756 | |
| 757 | printk(KERN_INFO "wanXL %s: at 0x%X, %u KB of RAM at 0x%X, irq %u\n", |
| 758 | pci_name(pdev), plx_phy, ramsize / 1024, mem_phy, pdev->irq); |
| 759 | |
| 760 | /* Allocate IRQ */ |
Thomas Gleixner | 1fb9df5 | 2006-07-01 19:29:39 -0700 | [diff] [blame] | 761 | if (request_irq(pdev->irq, wanxl_intr, IRQF_SHARED, "wanXL", card)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | printk(KERN_WARNING "wanXL %s: could not allocate IRQ%i.\n", |
| 763 | pci_name(pdev), pdev->irq); |
| 764 | wanxl_pci_remove_one(pdev); |
| 765 | return -EBUSY; |
| 766 | } |
| 767 | card->irq = pdev->irq; |
| 768 | |
| 769 | for (i = 0; i < ports; i++) { |
| 770 | hdlc_device *hdlc; |
| 771 | port_t *port = &card->ports[i]; |
| 772 | struct net_device *dev = alloc_hdlcdev(port); |
| 773 | if (!dev) { |
| 774 | printk(KERN_ERR "wanXL %s: unable to allocate" |
| 775 | " memory\n", pci_name(pdev)); |
| 776 | wanxl_pci_remove_one(pdev); |
| 777 | return -ENOMEM; |
| 778 | } |
| 779 | |
| 780 | port->dev = dev; |
| 781 | hdlc = dev_to_hdlc(dev); |
| 782 | spin_lock_init(&port->lock); |
| 783 | SET_MODULE_OWNER(dev); |
| 784 | dev->tx_queue_len = 50; |
| 785 | dev->do_ioctl = wanxl_ioctl; |
| 786 | dev->open = wanxl_open; |
| 787 | dev->stop = wanxl_close; |
| 788 | hdlc->attach = wanxl_attach; |
| 789 | hdlc->xmit = wanxl_xmit; |
| 790 | dev->get_stats = wanxl_get_stats; |
| 791 | port->card = card; |
| 792 | port->node = i; |
| 793 | get_status(port)->clocking = CLOCK_EXT; |
| 794 | if (register_hdlc_device(dev)) { |
| 795 | printk(KERN_ERR "wanXL %s: unable to register hdlc" |
| 796 | " device\n", pci_name(pdev)); |
| 797 | free_netdev(dev); |
| 798 | wanxl_pci_remove_one(pdev); |
| 799 | return -ENOBUFS; |
| 800 | } |
| 801 | card->n_ports++; |
| 802 | } |
| 803 | |
| 804 | printk(KERN_INFO "wanXL %s: port", pci_name(pdev)); |
| 805 | for (i = 0; i < ports; i++) |
| 806 | printk("%s #%i: %s", i ? "," : "", i, |
| 807 | card->ports[i].dev->name); |
| 808 | printk("\n"); |
| 809 | |
| 810 | for (i = 0; i < ports; i++) |
| 811 | wanxl_cable_intr(&card->ports[i]); /* get carrier status etc.*/ |
| 812 | |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static struct pci_device_id wanxl_pci_tbl[] __devinitdata = { |
| 817 | { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL100, PCI_ANY_ID, |
| 818 | PCI_ANY_ID, 0, 0, 0 }, |
| 819 | { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL200, PCI_ANY_ID, |
| 820 | PCI_ANY_ID, 0, 0, 0 }, |
| 821 | { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL400, PCI_ANY_ID, |
| 822 | PCI_ANY_ID, 0, 0, 0 }, |
| 823 | { 0, } |
| 824 | }; |
| 825 | |
| 826 | |
| 827 | static struct pci_driver wanxl_pci_driver = { |
| 828 | .name = "wanXL", |
| 829 | .id_table = wanxl_pci_tbl, |
| 830 | .probe = wanxl_pci_init_one, |
| 831 | .remove = wanxl_pci_remove_one, |
| 832 | }; |
| 833 | |
| 834 | |
| 835 | static int __init wanxl_init_module(void) |
| 836 | { |
| 837 | #ifdef MODULE |
| 838 | printk(KERN_INFO "%s\n", version); |
| 839 | #endif |
Jeff Garzik | 2991762 | 2006-08-19 17:48:59 -0400 | [diff] [blame] | 840 | return pci_register_driver(&wanxl_pci_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | static void __exit wanxl_cleanup_module(void) |
| 844 | { |
| 845 | pci_unregister_driver(&wanxl_pci_driver); |
| 846 | } |
| 847 | |
| 848 | |
| 849 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 850 | MODULE_DESCRIPTION("SBE Inc. wanXL serial port driver"); |
| 851 | MODULE_LICENSE("GPL v2"); |
| 852 | MODULE_DEVICE_TABLE(pci, wanxl_pci_tbl); |
| 853 | |
| 854 | module_init(wanxl_init_module); |
| 855 | module_exit(wanxl_cleanup_module); |