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 | |
| 152 | hdlc_set_carrier(value & STATUS_CABLE_DCD, port->dev); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | /* Transmit complete interrupt service */ |
| 158 | static inline void wanxl_tx_intr(port_t *port) |
| 159 | { |
| 160 | struct net_device *dev = port->dev; |
| 161 | struct net_device_stats *stats = hdlc_stats(dev); |
| 162 | while (1) { |
| 163 | desc_t *desc = &get_status(port)->tx_descs[port->tx_in]; |
| 164 | struct sk_buff *skb = port->tx_skbs[port->tx_in]; |
| 165 | |
| 166 | switch (desc->stat) { |
| 167 | case PACKET_FULL: |
| 168 | case PACKET_EMPTY: |
| 169 | netif_wake_queue(dev); |
| 170 | return; |
| 171 | |
| 172 | case PACKET_UNDERRUN: |
| 173 | stats->tx_errors++; |
| 174 | stats->tx_fifo_errors++; |
| 175 | break; |
| 176 | |
| 177 | default: |
| 178 | stats->tx_packets++; |
| 179 | stats->tx_bytes += skb->len; |
| 180 | } |
| 181 | desc->stat = PACKET_EMPTY; /* Free descriptor */ |
| 182 | pci_unmap_single(port->card->pdev, desc->address, skb->len, |
| 183 | PCI_DMA_TODEVICE); |
| 184 | dev_kfree_skb_irq(skb); |
| 185 | port->tx_in = (port->tx_in + 1) % TX_BUFFERS; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | /* Receive complete interrupt service */ |
| 192 | static inline void wanxl_rx_intr(card_t *card) |
| 193 | { |
| 194 | desc_t *desc; |
| 195 | while (desc = &card->status->rx_descs[card->rx_in], |
| 196 | desc->stat != PACKET_EMPTY) { |
| 197 | if ((desc->stat & PACKET_PORT_MASK) > card->n_ports) |
| 198 | printk(KERN_CRIT "wanXL %s: received packet for" |
| 199 | " nonexistent port\n", pci_name(card->pdev)); |
| 200 | else { |
| 201 | struct sk_buff *skb = card->rx_skbs[card->rx_in]; |
| 202 | port_t *port = &card->ports[desc->stat & |
| 203 | PACKET_PORT_MASK]; |
| 204 | struct net_device *dev = port->dev; |
| 205 | struct net_device_stats *stats = hdlc_stats(dev); |
| 206 | |
| 207 | if (!skb) |
| 208 | stats->rx_dropped++; |
| 209 | else { |
| 210 | pci_unmap_single(card->pdev, desc->address, |
| 211 | BUFFER_LENGTH, |
| 212 | PCI_DMA_FROMDEVICE); |
| 213 | 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 |
| 220 | stats->rx_packets++; |
| 221 | stats->rx_bytes += skb->len; |
| 222 | dev->last_rx = jiffies; |
| 223 | skb->protocol = hdlc_type_trans(skb, dev); |
| 224 | netif_rx(skb); |
| 225 | skb = NULL; |
| 226 | } |
| 227 | |
| 228 | if (!skb) { |
| 229 | skb = dev_alloc_skb(BUFFER_LENGTH); |
| 230 | desc->address = skb ? |
| 231 | pci_map_single(card->pdev, skb->data, |
| 232 | BUFFER_LENGTH, |
| 233 | PCI_DMA_FROMDEVICE) : 0; |
| 234 | 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 | |
| 244 | static irqreturn_t wanxl_intr(int irq, void* dev_id, struct pt_regs *regs) |
| 245 | { |
| 246 | card_t *card = dev_id; |
| 247 | 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 | |
| 271 | static int wanxl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 272 | { |
| 273 | port_t *port = dev_to_port(dev); |
| 274 | 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); |
| 285 | spin_unlock_irq(&port->lock); |
| 286 | return 1; /* request packet to be queued */ |
| 287 | } |
| 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; |
| 295 | desc->address = pci_map_single(port->card->pdev, skb->data, skb->len, |
| 296 | PCI_DMA_TODEVICE); |
| 297 | 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); |
| 301 | dev->trans_start = jiffies; |
| 302 | |
| 303 | port->tx_out = (port->tx_out + 1) % TX_BUFFERS; |
| 304 | |
| 305 | if (get_status(port)->tx_descs[port->tx_out].stat != PACKET_EMPTY) { |
| 306 | netif_stop_queue(dev); |
| 307 | #ifdef DEBUG_PKT |
| 308 | printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name); |
| 309 | #endif |
| 310 | } |
| 311 | |
| 312 | spin_unlock(&port->lock); |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | static int wanxl_attach(struct net_device *dev, unsigned short encoding, |
| 319 | unsigned short parity) |
| 320 | { |
| 321 | port_t *port = dev_to_port(dev); |
| 322 | |
| 323 | if (encoding != ENCODING_NRZ && |
| 324 | encoding != ENCODING_NRZI) |
| 325 | return -EINVAL; |
| 326 | |
| 327 | if (parity != PARITY_NONE && |
| 328 | parity != PARITY_CRC32_PR1_CCITT && |
| 329 | parity != PARITY_CRC16_PR1_CCITT && |
| 330 | parity != PARITY_CRC32_PR0_CCITT && |
| 331 | parity != PARITY_CRC16_PR0_CCITT) |
| 332 | return -EINVAL; |
| 333 | |
| 334 | get_status(port)->encoding = encoding; |
| 335 | get_status(port)->parity = parity; |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | |
| 340 | |
| 341 | static int wanxl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 342 | { |
| 343 | const size_t size = sizeof(sync_serial_settings); |
| 344 | sync_serial_settings line; |
| 345 | port_t *port = dev_to_port(dev); |
| 346 | |
| 347 | if (cmd != SIOCWANDEV) |
| 348 | return hdlc_ioctl(dev, ifr, cmd); |
| 349 | |
| 350 | switch (ifr->ifr_settings.type) { |
| 351 | case IF_GET_IFACE: |
| 352 | ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL; |
| 353 | if (ifr->ifr_settings.size < size) { |
| 354 | ifr->ifr_settings.size = size; /* data size wanted */ |
| 355 | return -ENOBUFS; |
| 356 | } |
| 357 | 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 | |
| 392 | static int wanxl_open(struct net_device *dev) |
| 393 | { |
| 394 | port_t *port = dev_to_port(dev); |
| 395 | u8 __iomem *dbr = port->card->plx + PLX_DOORBELL_TO_CARD; |
| 396 | unsigned long timeout; |
| 397 | int i; |
| 398 | |
| 399 | if (get_status(port)->open) { |
| 400 | printk(KERN_ERR "%s: port already open\n", dev->name); |
| 401 | 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; |
| 413 | do |
| 414 | if (get_status(port)->open) { |
| 415 | netif_start_queue(dev); |
| 416 | return 0; |
| 417 | } |
| 418 | while (time_after(timeout, jiffies)); |
| 419 | |
| 420 | printk(KERN_ERR "%s: unable to open port\n", dev->name); |
| 421 | /* 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 | |
| 428 | static int wanxl_close(struct net_device *dev) |
| 429 | { |
| 430 | port_t *port = dev_to_port(dev); |
| 431 | 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; |
| 440 | do |
| 441 | if (!get_status(port)->open) |
| 442 | break; |
| 443 | while (time_after(timeout, jiffies)); |
| 444 | |
| 445 | if (get_status(port)->open) |
| 446 | printk(KERN_ERR "%s: unable to close port\n", dev->name); |
| 447 | |
| 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; |
| 455 | pci_unmap_single(port->card->pdev, desc->address, |
| 456 | port->tx_skbs[i]->len, |
| 457 | PCI_DMA_TODEVICE); |
| 458 | dev_kfree_skb(port->tx_skbs[i]); |
| 459 | } |
| 460 | } |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | |
| 465 | |
| 466 | static struct net_device_stats *wanxl_get_stats(struct net_device *dev) |
| 467 | { |
| 468 | struct net_device_stats *stats = hdlc_stats(dev); |
| 469 | port_t *port = dev_to_port(dev); |
| 470 | |
| 471 | stats->rx_over_errors = get_status(port)->rx_overruns; |
| 472 | stats->rx_frame_errors = get_status(port)->rx_frame_errors; |
| 473 | stats->rx_errors = stats->rx_over_errors + stats->rx_frame_errors; |
| 474 | return stats; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | |
| 479 | static int wanxl_puts_command(card_t *card, u32 cmd) |
| 480 | { |
| 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 | |
| 496 | static void wanxl_reset(card_t *card) |
| 497 | { |
| 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 | |
| 510 | static void wanxl_pci_remove_one(struct pci_dev *pdev) |
| 511 | { |
| 512 | card_t *card = pci_get_drvdata(pdev); |
| 513 | 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]) { |
| 528 | pci_unmap_single(card->pdev, |
| 529 | card->status->rx_descs[i].address, |
| 530 | BUFFER_LENGTH, PCI_DMA_FROMDEVICE); |
| 531 | dev_kfree_skb(card->rx_skbs[i]); |
| 532 | } |
| 533 | |
| 534 | if (card->plx) |
| 535 | iounmap(card->plx); |
| 536 | |
| 537 | if (card->status) |
| 538 | pci_free_consistent(pdev, sizeof(card_status_t), |
| 539 | card->status, card->status_address); |
| 540 | |
| 541 | pci_release_regions(pdev); |
| 542 | pci_disable_device(pdev); |
| 543 | pci_set_drvdata(pdev, NULL); |
| 544 | kfree(card); |
| 545 | } |
| 546 | |
| 547 | |
| 548 | #include "wanxlfw.inc" |
| 549 | |
| 550 | static int __devinit wanxl_pci_init_one(struct pci_dev *pdev, |
| 551 | const struct pci_device_id *ent) |
| 552 | { |
| 553 | card_t *card; |
| 554 | u32 ramsize, stat; |
| 555 | unsigned long timeout; |
| 556 | u32 plx_phy; /* PLX PCI base address */ |
| 557 | u32 mem_phy; /* memory PCI base addr */ |
| 558 | u8 __iomem *mem; /* memory virtual base addr */ |
| 559 | int i, ports, alloc_size; |
| 560 | |
| 561 | #ifndef MODULE |
| 562 | static int printed_version; |
| 563 | if (!printed_version) { |
| 564 | printed_version++; |
| 565 | printk(KERN_INFO "%s\n", version); |
| 566 | } |
| 567 | #endif |
| 568 | |
| 569 | i = pci_enable_device(pdev); |
| 570 | if (i) |
| 571 | return i; |
| 572 | |
| 573 | /* QUICC can only access first 256 MB of host RAM directly, |
| 574 | but PLX9060 DMA does 32-bits for actual packet data transfers */ |
| 575 | |
| 576 | /* FIXME when PCI/DMA subsystems are fixed. |
| 577 | We set both dma_mask and consistent_dma_mask to 28 bits |
| 578 | and pray pci_alloc_consistent() will use this info. It should |
| 579 | work on most platforms */ |
| 580 | if (pci_set_consistent_dma_mask(pdev, 0x0FFFFFFF) || |
| 581 | pci_set_dma_mask(pdev, 0x0FFFFFFF)) { |
| 582 | printk(KERN_ERR "wanXL: No usable DMA configuration\n"); |
| 583 | return -EIO; |
| 584 | } |
| 585 | |
| 586 | i = pci_request_regions(pdev, "wanXL"); |
| 587 | if (i) { |
| 588 | pci_disable_device(pdev); |
| 589 | return i; |
| 590 | } |
| 591 | |
| 592 | switch (pdev->device) { |
| 593 | case PCI_DEVICE_ID_SBE_WANXL100: ports = 1; break; |
| 594 | case PCI_DEVICE_ID_SBE_WANXL200: ports = 2; break; |
| 595 | default: ports = 4; |
| 596 | } |
| 597 | |
| 598 | alloc_size = sizeof(card_t) + ports * sizeof(port_t); |
| 599 | card = kmalloc(alloc_size, GFP_KERNEL); |
| 600 | if (card == NULL) { |
| 601 | printk(KERN_ERR "wanXL %s: unable to allocate memory\n", |
| 602 | pci_name(pdev)); |
| 603 | pci_release_regions(pdev); |
| 604 | pci_disable_device(pdev); |
| 605 | return -ENOBUFS; |
| 606 | } |
| 607 | memset(card, 0, alloc_size); |
| 608 | |
| 609 | pci_set_drvdata(pdev, card); |
| 610 | card->pdev = pdev; |
| 611 | |
| 612 | card->status = pci_alloc_consistent(pdev, sizeof(card_status_t), |
| 613 | &card->status_address); |
| 614 | if (card->status == NULL) { |
| 615 | wanxl_pci_remove_one(pdev); |
| 616 | return -ENOBUFS; |
| 617 | } |
| 618 | |
| 619 | #ifdef DEBUG_PCI |
| 620 | printk(KERN_DEBUG "wanXL %s: pci_alloc_consistent() returned memory" |
| 621 | " at 0x%LX\n", pci_name(pdev), |
| 622 | (unsigned long long)card->status_address); |
| 623 | #endif |
| 624 | |
| 625 | /* FIXME when PCI/DMA subsystems are fixed. |
| 626 | We set both dma_mask and consistent_dma_mask back to 32 bits |
| 627 | to indicate the card can do 32-bit DMA addressing */ |
Domen Puncer | 1e7f0bd | 2005-06-26 18:22:14 -0400 | [diff] [blame] | 628 | if (pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK) || |
| 629 | pci_set_dma_mask(pdev, DMA_32BIT_MASK)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | printk(KERN_ERR "wanXL: No usable DMA configuration\n"); |
| 631 | wanxl_pci_remove_one(pdev); |
| 632 | return -EIO; |
| 633 | } |
| 634 | |
| 635 | /* set up PLX mapping */ |
| 636 | plx_phy = pci_resource_start(pdev, 0); |
| 637 | card->plx = ioremap_nocache(plx_phy, 0x70); |
| 638 | |
| 639 | #if RESET_WHILE_LOADING |
| 640 | wanxl_reset(card); |
| 641 | #endif |
| 642 | |
| 643 | timeout = jiffies + 20 * HZ; |
| 644 | while ((stat = readl(card->plx + PLX_MAILBOX_0)) != 0) { |
| 645 | if (time_before(timeout, jiffies)) { |
| 646 | printk(KERN_WARNING "wanXL %s: timeout waiting for" |
| 647 | " PUTS to complete\n", pci_name(pdev)); |
| 648 | wanxl_pci_remove_one(pdev); |
| 649 | return -ENODEV; |
| 650 | } |
| 651 | |
| 652 | switch(stat & 0xC0) { |
| 653 | case 0x00: /* hmm - PUTS completed with non-zero code? */ |
| 654 | case 0x80: /* PUTS still testing the hardware */ |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | printk(KERN_WARNING "wanXL %s: PUTS test 0x%X" |
| 659 | " failed\n", pci_name(pdev), stat & 0x30); |
| 660 | wanxl_pci_remove_one(pdev); |
| 661 | return -ENODEV; |
| 662 | } |
| 663 | |
| 664 | schedule(); |
| 665 | } |
| 666 | |
| 667 | /* get on-board memory size (PUTS detects no more than 4 MB) */ |
| 668 | ramsize = readl(card->plx + PLX_MAILBOX_2) & MBX2_MEMSZ_MASK; |
| 669 | |
| 670 | /* set up on-board RAM mapping */ |
| 671 | mem_phy = pci_resource_start(pdev, 2); |
| 672 | |
| 673 | |
| 674 | /* sanity check the board's reported memory size */ |
| 675 | if (ramsize < BUFFERS_ADDR + |
| 676 | (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports) { |
| 677 | printk(KERN_WARNING "wanXL %s: no enough on-board RAM" |
| 678 | " (%u bytes detected, %u bytes required)\n", |
| 679 | pci_name(pdev), ramsize, BUFFERS_ADDR + |
| 680 | (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports); |
| 681 | wanxl_pci_remove_one(pdev); |
| 682 | return -ENODEV; |
| 683 | } |
| 684 | |
| 685 | if (wanxl_puts_command(card, MBX1_CMD_BSWAP)) { |
| 686 | printk(KERN_WARNING "wanXL %s: unable to Set Byte Swap" |
| 687 | " Mode\n", pci_name(pdev)); |
| 688 | wanxl_pci_remove_one(pdev); |
| 689 | return -ENODEV; |
| 690 | } |
| 691 | |
| 692 | for (i = 0; i < RX_QUEUE_LENGTH; i++) { |
| 693 | struct sk_buff *skb = dev_alloc_skb(BUFFER_LENGTH); |
| 694 | card->rx_skbs[i] = skb; |
| 695 | if (skb) |
| 696 | card->status->rx_descs[i].address = |
| 697 | pci_map_single(card->pdev, skb->data, |
| 698 | BUFFER_LENGTH, |
| 699 | PCI_DMA_FROMDEVICE); |
| 700 | } |
| 701 | |
| 702 | mem = ioremap_nocache(mem_phy, PDM_OFFSET + sizeof(firmware)); |
| 703 | for (i = 0; i < sizeof(firmware); i += 4) |
| 704 | writel(htonl(*(u32*)(firmware + i)), mem + PDM_OFFSET + i); |
| 705 | |
| 706 | for (i = 0; i < ports; i++) |
| 707 | writel(card->status_address + |
| 708 | (void *)&card->status->port_status[i] - |
| 709 | (void *)card->status, mem + PDM_OFFSET + 4 + i * 4); |
| 710 | writel(card->status_address, mem + PDM_OFFSET + 20); |
| 711 | writel(PDM_OFFSET, mem); |
| 712 | iounmap(mem); |
| 713 | |
| 714 | writel(0, card->plx + PLX_MAILBOX_5); |
| 715 | |
| 716 | if (wanxl_puts_command(card, MBX1_CMD_ABORTJ)) { |
| 717 | printk(KERN_WARNING "wanXL %s: unable to Abort and Jump\n", |
| 718 | pci_name(pdev)); |
| 719 | wanxl_pci_remove_one(pdev); |
| 720 | return -ENODEV; |
| 721 | } |
| 722 | |
| 723 | stat = 0; |
| 724 | timeout = jiffies + 5 * HZ; |
| 725 | do { |
| 726 | if ((stat = readl(card->plx + PLX_MAILBOX_5)) != 0) |
| 727 | break; |
| 728 | schedule(); |
| 729 | }while (time_after(timeout, jiffies)); |
| 730 | |
| 731 | if (!stat) { |
| 732 | printk(KERN_WARNING "wanXL %s: timeout while initializing card" |
| 733 | "firmware\n", pci_name(pdev)); |
| 734 | wanxl_pci_remove_one(pdev); |
| 735 | return -ENODEV; |
| 736 | } |
| 737 | |
| 738 | #if DETECT_RAM |
| 739 | ramsize = stat; |
| 740 | #endif |
| 741 | |
| 742 | printk(KERN_INFO "wanXL %s: at 0x%X, %u KB of RAM at 0x%X, irq %u\n", |
| 743 | pci_name(pdev), plx_phy, ramsize / 1024, mem_phy, pdev->irq); |
| 744 | |
| 745 | /* Allocate IRQ */ |
| 746 | if (request_irq(pdev->irq, wanxl_intr, SA_SHIRQ, "wanXL", card)) { |
| 747 | printk(KERN_WARNING "wanXL %s: could not allocate IRQ%i.\n", |
| 748 | pci_name(pdev), pdev->irq); |
| 749 | wanxl_pci_remove_one(pdev); |
| 750 | return -EBUSY; |
| 751 | } |
| 752 | card->irq = pdev->irq; |
| 753 | |
| 754 | for (i = 0; i < ports; i++) { |
| 755 | hdlc_device *hdlc; |
| 756 | port_t *port = &card->ports[i]; |
| 757 | struct net_device *dev = alloc_hdlcdev(port); |
| 758 | if (!dev) { |
| 759 | printk(KERN_ERR "wanXL %s: unable to allocate" |
| 760 | " memory\n", pci_name(pdev)); |
| 761 | wanxl_pci_remove_one(pdev); |
| 762 | return -ENOMEM; |
| 763 | } |
| 764 | |
| 765 | port->dev = dev; |
| 766 | hdlc = dev_to_hdlc(dev); |
| 767 | spin_lock_init(&port->lock); |
| 768 | SET_MODULE_OWNER(dev); |
| 769 | dev->tx_queue_len = 50; |
| 770 | dev->do_ioctl = wanxl_ioctl; |
| 771 | dev->open = wanxl_open; |
| 772 | dev->stop = wanxl_close; |
| 773 | hdlc->attach = wanxl_attach; |
| 774 | hdlc->xmit = wanxl_xmit; |
| 775 | dev->get_stats = wanxl_get_stats; |
| 776 | port->card = card; |
| 777 | port->node = i; |
| 778 | get_status(port)->clocking = CLOCK_EXT; |
| 779 | if (register_hdlc_device(dev)) { |
| 780 | printk(KERN_ERR "wanXL %s: unable to register hdlc" |
| 781 | " device\n", pci_name(pdev)); |
| 782 | free_netdev(dev); |
| 783 | wanxl_pci_remove_one(pdev); |
| 784 | return -ENOBUFS; |
| 785 | } |
| 786 | card->n_ports++; |
| 787 | } |
| 788 | |
| 789 | printk(KERN_INFO "wanXL %s: port", pci_name(pdev)); |
| 790 | for (i = 0; i < ports; i++) |
| 791 | printk("%s #%i: %s", i ? "," : "", i, |
| 792 | card->ports[i].dev->name); |
| 793 | printk("\n"); |
| 794 | |
| 795 | for (i = 0; i < ports; i++) |
| 796 | wanxl_cable_intr(&card->ports[i]); /* get carrier status etc.*/ |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static struct pci_device_id wanxl_pci_tbl[] __devinitdata = { |
| 802 | { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL100, PCI_ANY_ID, |
| 803 | PCI_ANY_ID, 0, 0, 0 }, |
| 804 | { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL200, PCI_ANY_ID, |
| 805 | PCI_ANY_ID, 0, 0, 0 }, |
| 806 | { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL400, PCI_ANY_ID, |
| 807 | PCI_ANY_ID, 0, 0, 0 }, |
| 808 | { 0, } |
| 809 | }; |
| 810 | |
| 811 | |
| 812 | static struct pci_driver wanxl_pci_driver = { |
| 813 | .name = "wanXL", |
| 814 | .id_table = wanxl_pci_tbl, |
| 815 | .probe = wanxl_pci_init_one, |
| 816 | .remove = wanxl_pci_remove_one, |
| 817 | }; |
| 818 | |
| 819 | |
| 820 | static int __init wanxl_init_module(void) |
| 821 | { |
| 822 | #ifdef MODULE |
| 823 | printk(KERN_INFO "%s\n", version); |
| 824 | #endif |
| 825 | return pci_module_init(&wanxl_pci_driver); |
| 826 | } |
| 827 | |
| 828 | static void __exit wanxl_cleanup_module(void) |
| 829 | { |
| 830 | pci_unregister_driver(&wanxl_pci_driver); |
| 831 | } |
| 832 | |
| 833 | |
| 834 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 835 | MODULE_DESCRIPTION("SBE Inc. wanXL serial port driver"); |
| 836 | MODULE_LICENSE("GPL v2"); |
| 837 | MODULE_DEVICE_TABLE(pci, wanxl_pci_tbl); |
| 838 | |
| 839 | module_init(wanxl_init_module); |
| 840 | module_exit(wanxl_cleanup_module); |