solos-pci.c 39.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Driver for the Solos PCI ADSL2+ card, designed to support Linux by
 *  Traverse Technologies -- http://www.traverse.com.au/
 *  Xrio Limited          -- http://www.xrio.com/
 *
 *
 * Copyright © 2008 Traverse Technologies
 * Copyright © 2008 Intel Corporation
 *
 * Authors: Nathan Williams <nathan@traverse.com.au>
 *          David Woodhouse <dwmw2@infradead.org>
12
 *          Treker Chen <treker@xrio.com>
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#define DEBUG
#define VERBOSE_DEBUG

#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/atm.h>
#include <linux/atmdev.h>
#include <linux/skbuff.h>
#include <linux/sysfs.h>
#include <linux/device.h>
#include <linux/kobject.h>
40
#include <linux/firmware.h>
41 42
#include <linux/ctype.h>
#include <linux/swab.h>
43
#include <linux/slab.h>
44

45 46
#define VERSION "1.04"
#define DRIVER_VERSION 0x01
47 48 49 50 51 52 53
#define PTAG "solos-pci"

#define CONFIG_RAM_SIZE	128
#define FLAGS_ADDR	0x7C
#define IRQ_EN_ADDR	0x78
#define FPGA_VER	0x74
#define IRQ_CLEAR	0x70
54 55 56 57 58 59
#define WRITE_FLASH	0x6C
#define PORTS		0x68
#define FLASH_BLOCK	0x64
#define FLASH_BUSY	0x60
#define FPGA_MODE	0x5C
#define FLASH_MODE	0x58
60
#define GPIO_STATUS	0x54
61
#define DRIVER_VER	0x50
62 63
#define TX_DMA_ADDR(port)	(0x40 + (4 * (port)))
#define RX_DMA_ADDR(port)	(0x30 + (4 * (port)))
64 65

#define DATA_RAM_SIZE	32768
66 67
#define BUF_SIZE	2048
#define OLD_BUF_SIZE	4096 /* For FPGA versions <= 2*/
68 69 70 71 72 73 74
/* Old boards use ATMEL AD45DB161D flash */
#define ATMEL_FPGA_PAGE	528 /* FPGA flash page size*/
#define ATMEL_SOLOS_PAGE	512 /* Solos flash page size*/
#define ATMEL_FPGA_BLOCK	(ATMEL_FPGA_PAGE * 8) /* FPGA block size*/
#define ATMEL_SOLOS_BLOCK	(ATMEL_SOLOS_PAGE * 8) /* Solos block size*/
/* Current boards use M25P/M25PE SPI flash */
#define SPI_FLASH_BLOCK	(256 * 64)
75

76 77 78
#define RX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2)
#define TX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2 + (card->buffer_size))
#define FLASH_BUF ((card->buffers) + 4*(card->buffer_size)*2)
79

80 81
#define RX_DMA_SIZE	2048

82 83 84 85
#define FPGA_VERSION(a,b) (((a) << 8) + (b))
#define LEGACY_BUFFERS	2
#define DMA_SUPPORTED	4

86
static int reset = 0;
87
static int atmdebug = 0;
88 89
static int firmware_upgrade = 0;
static int fpga_upgrade = 0;
90 91
static int db_firmware_upgrade = 0;
static int db_fpga_upgrade = 0;
92 93 94 95 96 97 98 99

struct pkt_hdr {
	__le16 size;
	__le16 vpi;
	__le16 vci;
	__le16 type;
};

100 101 102 103 104 105 106 107
struct solos_skb_cb {
	struct atm_vcc *vcc;
	uint32_t dma_addr;
};


#define SKB_CB(skb)		((struct solos_skb_cb *)skb->cb)

108 109 110 111
#define PKT_DATA	0
#define PKT_COMMAND	1
#define PKT_POPEN	3
#define PKT_PCLOSE	4
112
#define PKT_STATUS	5
113 114 115 116 117

struct solos_card {
	void __iomem *config_regs;
	void __iomem *buffers;
	int nr_ports;
118
	int tx_mask;
119 120 121 122 123 124
	struct pci_dev *dev;
	struct atm_dev *atmdev[4];
	struct tasklet_struct tlet;
	spinlock_t tx_lock;
	spinlock_t tx_queue_lock;
	spinlock_t cli_queue_lock;
125 126
	spinlock_t param_queue_lock;
	struct list_head param_queue;
127 128
	struct sk_buff_head tx_queue[4];
	struct sk_buff_head cli_queue[4];
129 130
	struct sk_buff *tx_skb[4];
	struct sk_buff *rx_skb[4];
131
	unsigned char *dma_bounce;
132
	wait_queue_head_t param_wq;
133
	wait_queue_head_t fw_wq;
134
	int using_dma;
135
	int dma_alignment;
136 137
	int fpga_version;
	int buffer_size;
138
	int atmel_flash;
139 140
};

141 142 143 144 145 146

struct solos_param {
	struct list_head list;
	pid_t pid;
	int port;
	struct sk_buff *response;
147 148 149 150 151 152 153 154
};

#define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)

MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
MODULE_DESCRIPTION("Solos PCI driver");
MODULE_VERSION(VERSION);
MODULE_LICENSE("GPL");
B
Ben Hutchings 已提交
155 156 157
MODULE_FIRMWARE("solos-FPGA.bin");
MODULE_FIRMWARE("solos-Firmware.bin");
MODULE_FIRMWARE("solos-db-FPGA.bin");
158
MODULE_PARM_DESC(reset, "Reset Solos chips on startup");
159
MODULE_PARM_DESC(atmdebug, "Print ATM data");
160 161
MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
162 163
MODULE_PARM_DESC(db_firmware_upgrade, "Initiate daughter board Solos firmware upgrade");
MODULE_PARM_DESC(db_fpga_upgrade, "Initiate daughter board FPGA upgrade");
164
module_param(reset, int, 0444);
165
module_param(atmdebug, int, 0644);
166 167
module_param(firmware_upgrade, int, 0444);
module_param(fpga_upgrade, int, 0444);
168 169
module_param(db_firmware_upgrade, int, 0444);
module_param(db_fpga_upgrade, int, 0444);
170 171 172

static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
		       struct atm_vcc *vcc);
173
static uint32_t fpga_tx(struct solos_card *);
174 175
static irqreturn_t solos_irq(int irq, void *dev_id);
static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
176
static int atm_init(struct solos_card *, struct device *);
177 178 179 180 181 182 183 184 185 186 187 188 189
static void atm_remove(struct solos_card *);
static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
static void solos_bh(unsigned long);
static int print_buffer(struct sk_buff *buf);

static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
{
        if (vcc->pop)
                vcc->pop(vcc, skb);
        else
                dev_kfree_skb_any(skb);
}

190 191 192 193 194 195 196 197 198 199 200 201
static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
				char *buf)
{
	struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
	struct solos_card *card = atmdev->dev_data;
	struct solos_param prm;
	struct sk_buff *skb;
	struct pkt_hdr *header;
	int buflen;

	buflen = strlen(attr->attr.name) + 10;

202
	skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
203 204 205 206 207
	if (!skb) {
		dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
		return -ENOMEM;
	}

208
	header = skb_put(skb, sizeof(*header));
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257

	buflen = snprintf((void *)&header[1], buflen - 1,
			  "L%05d\n%s\n", current->pid, attr->attr.name);
	skb_put(skb, buflen);

	header->size = cpu_to_le16(buflen);
	header->vpi = cpu_to_le16(0);
	header->vci = cpu_to_le16(0);
	header->type = cpu_to_le16(PKT_COMMAND);

	prm.pid = current->pid;
	prm.response = NULL;
	prm.port = SOLOS_CHAN(atmdev);

	spin_lock_irq(&card->param_queue_lock);
	list_add(&prm.list, &card->param_queue);
	spin_unlock_irq(&card->param_queue_lock);

	fpga_queue(card, prm.port, skb, NULL);

	wait_event_timeout(card->param_wq, prm.response, 5 * HZ);

	spin_lock_irq(&card->param_queue_lock);
	list_del(&prm.list);
	spin_unlock_irq(&card->param_queue_lock);

	if (!prm.response)
		return -EIO;

	buflen = prm.response->len;
	memcpy(buf, prm.response->data, buflen);
	kfree_skb(prm.response);

	return buflen;
}

static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
				 const char *buf, size_t count)
{
	struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
	struct solos_card *card = atmdev->dev_data;
	struct solos_param prm;
	struct sk_buff *skb;
	struct pkt_hdr *header;
	int buflen;
	ssize_t ret;

	buflen = strlen(attr->attr.name) + 11 + count;

258
	skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
259 260 261 262 263
	if (!skb) {
		dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
		return -ENOMEM;
	}

264
	header = skb_put(skb, sizeof(*header));
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319

	buflen = snprintf((void *)&header[1], buflen - 1,
			  "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);

	skb_put(skb, buflen);
	header->size = cpu_to_le16(buflen);
	header->vpi = cpu_to_le16(0);
	header->vci = cpu_to_le16(0);
	header->type = cpu_to_le16(PKT_COMMAND);

	prm.pid = current->pid;
	prm.response = NULL;
	prm.port = SOLOS_CHAN(atmdev);

	spin_lock_irq(&card->param_queue_lock);
	list_add(&prm.list, &card->param_queue);
	spin_unlock_irq(&card->param_queue_lock);

	fpga_queue(card, prm.port, skb, NULL);

	wait_event_timeout(card->param_wq, prm.response, 5 * HZ);

	spin_lock_irq(&card->param_queue_lock);
	list_del(&prm.list);
	spin_unlock_irq(&card->param_queue_lock);

	skb = prm.response;

	if (!skb)
		return -EIO;

	buflen = skb->len;

	/* Sometimes it has a newline, sometimes it doesn't. */
	if (skb->data[buflen - 1] == '\n')
		buflen--;

	if (buflen == 2 && !strncmp(skb->data, "OK", 2))
		ret = count;
	else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
		ret = -EIO;
	else {
		/* We know we have enough space allocated for this; we allocated 
		   it ourselves */
		skb->data[buflen] = 0;
	
		dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
			 skb->data);
		ret = -EIO;
	}
	kfree_skb(skb);

	return ret;
}

320 321 322 323
static char *next_string(struct sk_buff *skb)
{
	int i = 0;
	char *this = skb->data;
324 325
	
	for (i = 0; i < skb->len; i++) {
326 327
		if (this[i] == '\n') {
			this[i] = 0;
328
			skb_pull(skb, i + 1);
329 330
			return this;
		}
331 332
		if (!isprint(this[i]))
			return NULL;
333 334 335 336 337 338 339 340 341 342
	}
	return NULL;
}

/*
 * Status packet has fields separated by \n, starting with a version number
 * for the information therein. Fields are....
 *
 *     packet version
 *     RxBitRate	(version >= 1)
343
 *     TxBitRate	(version >= 1)
344
 *     State		(version >= 1)
345 346
 *     LocalSNRMargin	(version >= 1)
 *     LocalLineAttn	(version >= 1)
347 348 349
 */       
static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
{
350 351
	char *str, *state_str, *snr, *attn;
	int ver, rate_up, rate_down, err;
352 353 354 355 356 357 358 359

	if (!card->atmdev[port])
		return -ENODEV;

	str = next_string(skb);
	if (!str)
		return -EIO;

360 361 362 363 364
	err = kstrtoint(str, 10, &ver);
	if (err) {
		dev_warn(&card->dev->dev, "Unexpected status interrupt version\n");
		return err;
	}
365 366 367 368 369 370 371
	if (ver < 1) {
		dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
			 ver);
		return -EIO;
	}

	str = next_string(skb);
372 373
	if (!str)
		return -EIO;
374 375 376 377 378 379
	if (!strcmp(str, "ERROR")) {
		dev_dbg(&card->dev->dev, "Status packet indicated Solos error on port %d (starting up?)\n",
			 port);
		return 0;
	}

380 381 382
	err = kstrtoint(str, 10, &rate_down);
	if (err)
		return err;
383 384

	str = next_string(skb);
385 386
	if (!str)
		return -EIO;
387 388 389
	err = kstrtoint(str, 10, &rate_up);
	if (err)
		return err;
390

391
	state_str = next_string(skb);
392 393
	if (!state_str)
		return -EIO;
394 395 396

	/* Anything but 'Showtime' is down */
	if (strcmp(state_str, "Showtime")) {
397
		atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
398 399
		dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
		return 0;
400
	}
401

402
	snr = next_string(skb);
403
	if (!snr)
404 405 406 407 408 409 410 411 412
		return -EIO;
	attn = next_string(skb);
	if (!attn)
		return -EIO;

	dev_info(&card->dev->dev, "Port %d: %s @%d/%d kb/s%s%s%s%s\n",
		 port, state_str, rate_down/1000, rate_up/1000,
		 snr[0]?", SNR ":"", snr, attn[0]?", Attn ":"", attn);
	
413
	card->atmdev[port]->link_rate = rate_down / 424;
414
	atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
415 416 417 418

	return 0;
}

419 420 421 422 423
static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
{
	struct solos_param *prm;
	unsigned long flags;
	int cmdpid;
424
	int found = 0, err;
425 426 427 428 429 430 431 432 433 434

	if (skb->len < 7)
		return 0;

	if (skb->data[0] != 'L'    || !isdigit(skb->data[1]) ||
	    !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
	    !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
	    skb->data[6] != '\n')
		return 0;

435 436 437
	err = kstrtoint(&skb->data[1], 10, &cmdpid);
	if (err)
		return err;
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

	spin_lock_irqsave(&card->param_queue_lock, flags);
	list_for_each_entry(prm, &card->param_queue, list) {
		if (prm->port == port && prm->pid == cmdpid) {
			prm->response = skb;
			skb_pull(skb, 7);
			wake_up(&card->param_wq);
			found = 1;
			break;
		}
	}
	spin_unlock_irqrestore(&card->param_queue_lock, flags);
	return found;
}

453 454 455 456 457 458
static ssize_t console_show(struct device *dev, struct device_attribute *attr,
			    char *buf)
{
	struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
	struct solos_card *card = atmdev->dev_data;
	struct sk_buff *skb;
J
Jiri Slaby 已提交
459
	unsigned int len;
460 461 462 463 464 465 466

	spin_lock(&card->cli_queue_lock);
	skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
	spin_unlock(&card->cli_queue_lock);
	if(skb == NULL)
		return sprintf(buf, "No data.\n");

J
Jiri Slaby 已提交
467 468
	len = skb->len;
	memcpy(buf, skb->data, len);
469 470

	kfree_skb(skb);
J
Jiri Slaby 已提交
471
	return len;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
}

static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
{
	struct sk_buff *skb;
	struct pkt_hdr *header;

	if (size > (BUF_SIZE - sizeof(*header))) {
		dev_dbg(&card->dev->dev, "Command is too big.  Dropping request\n");
		return 0;
	}
	skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
	if (!skb) {
		dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
		return 0;
	}

489
	header = skb_put(skb, sizeof(*header));
490 491 492 493 494 495

	header->size = cpu_to_le16(size);
	header->vpi = cpu_to_le16(0);
	header->vci = cpu_to_le16(0);
	header->type = cpu_to_le16(PKT_COMMAND);

496
	skb_put_data(skb, buf, size);
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

	fpga_queue(card, dev, skb, NULL);

	return 0;
}

static ssize_t console_store(struct device *dev, struct device_attribute *attr,
			     const char *buf, size_t count)
{
	struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
	struct solos_card *card = atmdev->dev_data;
	int err;

	err = send_command(card, SOLOS_CHAN(atmdev), buf, count);

	return err?:count;
}

515 516 517 518 519 520 521 522 523 524 525 526 527
struct geos_gpio_attr {
	struct device_attribute attr;
	int offset;
};

#define SOLOS_GPIO_ATTR(_name, _mode, _show, _store, _offset)	\
	struct geos_gpio_attr gpio_attr_##_name = {		\
		.attr = __ATTR(_name, _mode, _show, _store),	\
		.offset = _offset }

static ssize_t geos_gpio_store(struct device *dev, struct device_attribute *attr,
			       const char *buf, size_t count)
{
G
Geliang Tang 已提交
528
	struct pci_dev *pdev = to_pci_dev(dev);
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
	struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
	struct solos_card *card = pci_get_drvdata(pdev);
	uint32_t data32;

	if (count != 1 && (count != 2 || buf[1] != '\n'))
		return -EINVAL;

	spin_lock_irq(&card->param_queue_lock);
	data32 = ioread32(card->config_regs + GPIO_STATUS);
	if (buf[0] == '1') {
		data32 |= 1 << gattr->offset;
		iowrite32(data32, card->config_regs + GPIO_STATUS);
	} else if (buf[0] == '0') {
		data32 &= ~(1 << gattr->offset);
		iowrite32(data32, card->config_regs + GPIO_STATUS);
	} else {
		count = -EINVAL;
	}
547
	spin_unlock_irq(&card->param_queue_lock);
548 549 550 551 552 553
	return count;
}

static ssize_t geos_gpio_show(struct device *dev, struct device_attribute *attr,
			      char *buf)
{
G
Geliang Tang 已提交
554
	struct pci_dev *pdev = to_pci_dev(dev);
555 556 557 558 559 560 561 562 563 564 565 566 567
	struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
	struct solos_card *card = pci_get_drvdata(pdev);
	uint32_t data32;

	data32 = ioread32(card->config_regs + GPIO_STATUS);
	data32 = (data32 >> gattr->offset) & 1;

	return sprintf(buf, "%d\n", data32);
}

static ssize_t hardware_show(struct device *dev, struct device_attribute *attr,
			     char *buf)
{
G
Geliang Tang 已提交
568
	struct pci_dev *pdev = to_pci_dev(dev);
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	struct geos_gpio_attr *gattr = container_of(attr, struct geos_gpio_attr, attr);
	struct solos_card *card = pci_get_drvdata(pdev);
	uint32_t data32;

	data32 = ioread32(card->config_regs + GPIO_STATUS);
	switch (gattr->offset) {
	case 0:
		/* HardwareVersion */
		data32 = data32 & 0x1F;
		break;
	case 1:
		/* HardwareVariant */
		data32 = (data32 >> 5) & 0x0F;
		break;
	}
	return sprintf(buf, "%d\n", data32);
}

587
static DEVICE_ATTR_RW(console);
588

589 590 591 592 593 594

#define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
#define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);

#include "solos-attrlist.c"

595 596 597 598 599 600 601 602
static SOLOS_GPIO_ATTR(GPIO1, 0644, geos_gpio_show, geos_gpio_store, 9);
static SOLOS_GPIO_ATTR(GPIO2, 0644, geos_gpio_show, geos_gpio_store, 10);
static SOLOS_GPIO_ATTR(GPIO3, 0644, geos_gpio_show, geos_gpio_store, 11);
static SOLOS_GPIO_ATTR(GPIO4, 0644, geos_gpio_show, geos_gpio_store, 12);
static SOLOS_GPIO_ATTR(GPIO5, 0644, geos_gpio_show, geos_gpio_store, 13);
static SOLOS_GPIO_ATTR(PushButton, 0444, geos_gpio_show, NULL, 14);
static SOLOS_GPIO_ATTR(HardwareVersion, 0444, hardware_show, NULL, 0);
static SOLOS_GPIO_ATTR(HardwareVariant, 0444, hardware_show, NULL, 1);
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
#undef SOLOS_ATTR_RO
#undef SOLOS_ATTR_RW

#define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
#define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,

static struct attribute *solos_attrs[] = {
#include "solos-attrlist.c"
	NULL
};

static struct attribute_group solos_attr_group = {
	.attrs = solos_attrs,
	.name = "parameters",
};
618

619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
static struct attribute *gpio_attrs[] = {
	&gpio_attr_GPIO1.attr.attr,
	&gpio_attr_GPIO2.attr.attr,
	&gpio_attr_GPIO3.attr.attr,
	&gpio_attr_GPIO4.attr.attr,
	&gpio_attr_GPIO5.attr.attr,
	&gpio_attr_PushButton.attr.attr,
	&gpio_attr_HardwareVersion.attr.attr,
	&gpio_attr_HardwareVariant.attr.attr,
	NULL
};

static struct attribute_group gpio_attr_group = {
	.attrs = gpio_attrs,
	.name = "gpio",
};

636 637 638 639
static int flash_upgrade(struct solos_card *card, int chip)
{
	const struct firmware *fw;
	const char *fw_name;
640 641
	int blocksize = 0;
	int numblocks = 0;
642 643
	int offset;

644 645
	switch (chip) {
	case 0:
646
		fw_name = "solos-FPGA.bin";
647 648 649 650
		if (card->atmel_flash)
			blocksize = ATMEL_FPGA_BLOCK;
		else
			blocksize = SPI_FLASH_BLOCK;
651 652
		break;
	case 1:
653
		fw_name = "solos-Firmware.bin";
654 655 656 657
		if (card->atmel_flash)
			blocksize = ATMEL_SOLOS_BLOCK;
		else
			blocksize = SPI_FLASH_BLOCK;
658 659
		break;
	case 2:
660 661
		if (card->fpga_version > LEGACY_BUFFERS){
			fw_name = "solos-db-FPGA.bin";
662 663 664 665
			if (card->atmel_flash)
				blocksize = ATMEL_FPGA_BLOCK;
			else
				blocksize = SPI_FLASH_BLOCK;
666
		} else {
667 668
			dev_info(&card->dev->dev, "FPGA version doesn't support"
					" daughter board upgrades\n");
669 670
			return -EPERM;
		}
671 672
		break;
	case 3:
673 674
		if (card->fpga_version > LEGACY_BUFFERS){
			fw_name = "solos-Firmware.bin";
675 676 677 678
			if (card->atmel_flash)
				blocksize = ATMEL_SOLOS_BLOCK;
			else
				blocksize = SPI_FLASH_BLOCK;
679
		} else {
680 681 682
			dev_info(&card->dev->dev, "FPGA version doesn't support"
					" daughter board upgrades\n");
			return -EPERM;
683
		}
684 685 686
		break;
	default:
		return -ENODEV;
687
	}
688 689 690 691 692 693

	if (request_firmware(&fw, fw_name, &card->dev->dev))
		return -ENOENT;

	dev_info(&card->dev->dev, "Flash upgrade starting\n");

694 695 696
	/* New FPGAs require driver version before permitting flash upgrades */
	iowrite32(DRIVER_VERSION, card->config_regs + DRIVER_VER);

697 698
	numblocks = fw->size / blocksize;
	dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
699 700 701 702
	dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
	
	dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
	iowrite32(1, card->config_regs + FPGA_MODE);
703
	(void) ioread32(card->config_regs + FPGA_MODE); 
704

705
	/* Set mode to Chip Erase */
706 707 708 709
	if(chip == 0 || chip == 2)
		dev_info(&card->dev->dev, "Set FPGA Flash mode to FPGA Chip Erase\n");
	if(chip == 1 || chip == 3)
		dev_info(&card->dev->dev, "Set FPGA Flash mode to Solos Chip Erase\n");
710
	iowrite32((chip * 2), card->config_regs + FLASH_MODE);
711 712


713 714 715 716 717 718 719
	iowrite32(1, card->config_regs + WRITE_FLASH);
	wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));

	for (offset = 0; offset < fw->size; offset += blocksize) {
		int i;

		/* Clear write flag */
720 721
		iowrite32(0, card->config_regs + WRITE_FLASH);

722 723 724 725
		/* Set mode to Block Write */
		/* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
		iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);

726
		/* Copy block to buffer, swapping each 16 bits for Atmel flash */
727
		for(i = 0; i < blocksize; i += 4) {
728 729 730 731 732
			uint32_t word;
			if (card->atmel_flash)
				word = swahb32p((uint32_t *)(fw->data + offset + i));
			else
				word = *(uint32_t *)(fw->data + offset + i);
733 734 735 736
			if(card->fpga_version > LEGACY_BUFFERS)
				iowrite32(word, FLASH_BUF + i);
			else
				iowrite32(word, RX_BUF(card, 3) + i);
737
		}
738 739 740 741 742

		/* Specify block number and then trigger flash write */
		iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
		iowrite32(1, card->config_regs + WRITE_FLASH);
		wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
743 744
	}

745 746 747 748 749 750
	release_firmware(fw);
	iowrite32(0, card->config_regs + WRITE_FLASH);
	iowrite32(0, card->config_regs + FPGA_MODE);
	iowrite32(0, card->config_regs + FLASH_MODE);
	dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
	return 0;
751 752
}

753 754 755 756 757 758 759
static irqreturn_t solos_irq(int irq, void *dev_id)
{
	struct solos_card *card = dev_id;
	int handled = 1;

	iowrite32(0, card->config_regs + IRQ_CLEAR);

760
	/* If we're up and running, just kick the tasklet to process TX/RX */
761
	if (card->atmdev[0])
762
		tasklet_schedule(&card->tlet);
763 764
	else
		wake_up(&card->fw_wq);
765 766 767 768

	return IRQ_RETVAL(handled);
}

769
static void solos_bh(unsigned long card_arg)
770 771 772 773
{
	struct solos_card *card = (void *)card_arg;
	uint32_t card_flags;
	uint32_t rx_done = 0;
774
	int port;
775

776 777 778 779 780 781
	/*
	 * Since fpga_tx() is going to need to read the flags under its lock,
	 * it can return them to us so that we don't have to hit PCI MMIO
	 * again for the same information
	 */
	card_flags = fpga_tx(card);
782 783 784

	for (port = 0; port < card->nr_ports; port++) {
		if (card_flags & (0x10 << port)) {
785
			struct pkt_hdr _hdr, *header;
786 787 788 789
			struct sk_buff *skb;
			struct atm_vcc *vcc;
			int size;

790 791
			if (card->using_dma) {
				skb = card->rx_skb[port];
792
				card->rx_skb[port] = NULL;
793

794 795
				dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
						 RX_DMA_SIZE, DMA_FROM_DEVICE);
796

797 798 799 800 801 802
				header = (void *)skb->data;
				size = le16_to_cpu(header->size);
				skb_put(skb, size + sizeof(*header));
				skb_pull(skb, sizeof(*header));
			} else {
				header = &_hdr;
803

804
				rx_done |= 0x10 << port;
805

806
				memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
807

808
				size = le16_to_cpu(header->size);
809 810 811 812
				if (size > (card->buffer_size - sizeof(*header))){
					dev_warn(&card->dev->dev, "Invalid buffer size\n");
					continue;
				}
813

814 815 816 817 818 819
				/* Use netdev_alloc_skb() because it adds NET_SKB_PAD of
				 * headroom, and ensures we can route packets back out an
				 * Ethernet interface (for example) without having to
				 * reallocate. Adding NET_IP_ALIGN also ensures that both
				 * PPPoATM and PPPoEoBR2684 packets end up aligned. */
				skb = netdev_alloc_skb_ip_align(NULL, size + 1);
820 821 822 823 824
				if (!skb) {
					if (net_ratelimit())
						dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
					continue;
				}
825

826 827 828 829
				memcpy_fromio(skb_put(skb, size),
					      RX_BUF(card, port) + sizeof(*header),
					      size);
			}
830
			if (atmdebug) {
831
				dev_info(&card->dev->dev, "Received: port %d\n", port);
832
				dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
833 834
					 size, le16_to_cpu(header->vpi),
					 le16_to_cpu(header->vci));
835 836 837
				print_buffer(skb);
			}

838
			switch (le16_to_cpu(header->type)) {
839
			case PKT_DATA:
840 841
				vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
					       le16_to_cpu(header->vci));
842 843
				if (!vcc) {
					if (net_ratelimit())
844 845
						dev_warn(&card->dev->dev, "Received packet for unknown VPI.VCI %d.%d on port %d\n",
							 le16_to_cpu(header->vpi), le16_to_cpu(header->vci),
846
							 port);
847 848
					dev_kfree_skb_any(skb);
					break;
849 850 851 852 853 854
				}
				atm_charge(vcc, skb->truesize);
				vcc->push(vcc, skb);
				atomic_inc(&vcc->stats->rx);
				break;

855
			case PKT_STATUS:
856 857 858 859 860
				if (process_status(card, port, skb) &&
				    net_ratelimit()) {
					dev_warn(&card->dev->dev, "Bad status packet of %d bytes on port %d:\n", skb->len, port);
					print_buffer(skb);
				}
861
				dev_kfree_skb_any(skb);
862 863
				break;

864 865
			case PKT_COMMAND:
			default: /* FIXME: Not really, surely? */
866 867
				if (process_command(card, port, skb))
					break;
868 869 870 871 872
				spin_lock(&card->cli_queue_lock);
				if (skb_queue_len(&card->cli_queue[port]) > 10) {
					if (net_ratelimit())
						dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
							 port);
873
					dev_kfree_skb_any(skb);
874 875 876 877 878 879
				} else
					skb_queue_tail(&card->cli_queue[port], skb);
				spin_unlock(&card->cli_queue_lock);
				break;
			}
		}
880 881 882
		/* Allocate RX skbs for any ports which need them */
		if (card->using_dma && card->atmdev[port] &&
		    !card->rx_skb[port]) {
883 884 885 886
			/* Unlike the MMIO case (qv) we can't add NET_IP_ALIGN
			 * here; the FPGA can only DMA to addresses which are
			 * aligned to 4 bytes. */
			struct sk_buff *skb = dev_alloc_skb(RX_DMA_SIZE);
887 888
			if (skb) {
				SKB_CB(skb)->dma_addr =
889 890
					dma_map_single(&card->dev->dev, skb->data,
						       RX_DMA_SIZE, DMA_FROM_DEVICE);
891 892 893 894 895 896 897 898 899 900 901
				iowrite32(SKB_CB(skb)->dma_addr,
					  card->config_regs + RX_DMA_ADDR(port));
				card->rx_skb[port] = skb;
			} else {
				if (net_ratelimit())
					dev_warn(&card->dev->dev, "Failed to allocate RX skb");

				/* We'll have to try again later */
				tasklet_schedule(&card->tlet);
			}
		}
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
	}
	if (rx_done)
		iowrite32(rx_done, card->config_regs + FLAGS_ADDR);

	return;
}

static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
{
	struct hlist_head *head;
	struct atm_vcc *vcc = NULL;
	struct sock *s;

	read_lock(&vcc_sklist_lock);
	head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
917
	sk_for_each(s, head) {
918 919
		vcc = atm_sk(s);
		if (vcc->dev == dev && vcc->vci == vci &&
920 921
		    vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
		    test_bit(ATM_VF_READY, &vcc->flags))
922 923 924 925 926 927 928 929 930 931 932 933 934 935
			goto out;
	}
	vcc = NULL;
 out:
	read_unlock(&vcc_sklist_lock);
	return vcc;
}

static int popen(struct atm_vcc *vcc)
{
	struct solos_card *card = vcc->dev->dev_data;
	struct sk_buff *skb;
	struct pkt_hdr *header;

936 937 938 939 940 941
	if (vcc->qos.aal != ATM_AAL5) {
		dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
			 vcc->qos.aal);
		return -EINVAL;
	}

942
	skb = alloc_skb(sizeof(*header), GFP_KERNEL);
943 944 945
	if (!skb) {
		if (net_ratelimit())
			dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
946 947
		return -ENOMEM;
	}
948
	header = skb_put(skb, sizeof(*header));
949

950
	header->size = cpu_to_le16(0);
951 952 953 954 955 956
	header->vpi = cpu_to_le16(vcc->vpi);
	header->vci = cpu_to_le16(vcc->vci);
	header->type = cpu_to_le16(PKT_POPEN);

	fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);

957
	set_bit(ATM_VF_ADDR, &vcc->flags);
958 959 960 961 962 963 964 965
	set_bit(ATM_VF_READY, &vcc->flags);

	return 0;
}

static void pclose(struct atm_vcc *vcc)
{
	struct solos_card *card = vcc->dev->dev_data;
966
	unsigned char port = SOLOS_CHAN(vcc->dev);
967
	struct sk_buff *skb, *tmpskb;
968 969
	struct pkt_hdr *header;

970 971 972 973 974 975 976 977 978 979
	/* Remove any yet-to-be-transmitted packets from the pending queue */
	spin_lock(&card->tx_queue_lock);
	skb_queue_walk_safe(&card->tx_queue[port], skb, tmpskb) {
		if (SKB_CB(skb)->vcc == vcc) {
			skb_unlink(skb, &card->tx_queue[port]);
			solos_pop(vcc, skb);
		}
	}
	spin_unlock(&card->tx_queue_lock);

980
	skb = alloc_skb(sizeof(*header), GFP_KERNEL);
981 982 983 984
	if (!skb) {
		dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
		return;
	}
985
	header = skb_put(skb, sizeof(*header));
986

987
	header->size = cpu_to_le16(0);
988 989 990 991
	header->vpi = cpu_to_le16(vcc->vpi);
	header->vci = cpu_to_le16(vcc->vci);
	header->type = cpu_to_le16(PKT_PCLOSE);

992 993
	skb_get(skb);
	fpga_queue(card, port, skb, NULL);
994

995 996 997 998 999 1000
	if (!wait_event_timeout(card->param_wq, !skb_shared(skb), 5 * HZ))
		dev_warn(&card->dev->dev,
			 "Timeout waiting for VCC close on port %d\n", port);

	dev_kfree_skb(skb);

1001 1002 1003 1004
	/* Hold up vcc_destroy_socket() (our caller) until solos_bh() in the
	   tasklet has finished processing any incoming packets (and, more to
	   the point, using the vcc pointer). */
	tasklet_unlock_wait(&card->tlet);
1005 1006 1007

	clear_bit(ATM_VF_ADDR, &vcc->flags);

1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
	return;
}

static int print_buffer(struct sk_buff *buf)
{
	int len,i;
	char msg[500];
	char item[10];

	len = buf->len;
	for (i = 0; i < len; i++){
		if(i % 8 == 0)
			sprintf(msg, "%02X: ", i);

		sprintf(item,"%02X ",*(buf->data + i));
		strcat(msg, item);
		if(i % 8 == 7) {
			sprintf(item, "\n");
			strcat(msg, item);
			printk(KERN_DEBUG "%s", msg);
		}
	}
	if (i % 8 != 0) {
		sprintf(item, "\n");
		strcat(msg, item);
		printk(KERN_DEBUG "%s", msg);
	}
	printk(KERN_DEBUG "\n");

	return 0;
}

static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
		       struct atm_vcc *vcc)
{
	int old_len;
1044
	unsigned long flags;
1045

1046
	SKB_CB(skb)->vcc = vcc;
1047

1048
	spin_lock_irqsave(&card->tx_queue_lock, flags);
1049 1050
	old_len = skb_queue_len(&card->tx_queue[port]);
	skb_queue_tail(&card->tx_queue[port], skb);
1051
	if (!old_len)
1052 1053
		card->tx_mask |= (1 << port);
	spin_unlock_irqrestore(&card->tx_queue_lock, flags);
1054

1055 1056
	/* Theoretically we could just schedule the tasklet here, but
	   that introduces latency we don't want -- it's noticeable */
1057 1058 1059 1060
	if (!old_len)
		fpga_tx(card);
}

1061
static uint32_t fpga_tx(struct solos_card *card)
1062
{
1063
	uint32_t tx_pending, card_flags;
1064 1065 1066 1067 1068 1069 1070
	uint32_t tx_started = 0;
	struct sk_buff *skb;
	struct atm_vcc *vcc;
	unsigned char port;
	unsigned long flags;

	spin_lock_irqsave(&card->tx_lock, flags);
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
	
	card_flags = ioread32(card->config_regs + FLAGS_ADDR);
	/*
	 * The queue lock is required for _writing_ to tx_mask, but we're
	 * OK to read it here without locking. The only potential update
	 * that we could race with is in fpga_queue() where it sets a bit
	 * for a new port... but it's going to call this function again if
	 * it's doing that, anyway.
	 */
	tx_pending = card->tx_mask & ~card_flags;

	for (port = 0; tx_pending; tx_pending >>= 1, port++) {
		if (tx_pending & 1) {
1084
			struct sk_buff *oldskb = card->tx_skb[port];
1085
			if (oldskb) {
1086 1087
				dma_unmap_single(&card->dev->dev, SKB_CB(oldskb)->dma_addr,
						 oldskb->len, DMA_TO_DEVICE);
1088 1089
				card->tx_skb[port] = NULL;
			}
1090 1091
			spin_lock(&card->tx_queue_lock);
			skb = skb_dequeue(&card->tx_queue[port]);
1092 1093
			if (!skb)
				card->tx_mask &= ~(1 << port);
1094 1095
			spin_unlock(&card->tx_queue_lock);

1096 1097
			if (skb && !card->using_dma) {
				memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
1098
				tx_started |= 1 << port;
1099 1100
				oldskb = skb; /* We're done with this skb already */
			} else if (skb && card->using_dma) {
1101 1102 1103 1104 1105
				unsigned char *data = skb->data;
				if ((unsigned long)data & card->dma_alignment) {
					data = card->dma_bounce + (BUF_SIZE * port);
					memcpy(data, skb->data, skb->len);
				}
1106 1107
				SKB_CB(skb)->dma_addr = dma_map_single(&card->dev->dev, data,
								       skb->len, DMA_TO_DEVICE);
D
David Woodhouse 已提交
1108
				card->tx_skb[port] = skb;
1109 1110 1111 1112 1113
				iowrite32(SKB_CB(skb)->dma_addr,
					  card->config_regs + TX_DMA_ADDR(port));
			}

			if (!oldskb)
1114 1115
				continue;

1116
			/* Clean up and free oldskb now it's gone */
1117
			if (atmdebug) {
1118 1119 1120 1121
				struct pkt_hdr *header = (void *)oldskb->data;
				int size = le16_to_cpu(header->size);

				skb_pull(oldskb, sizeof(*header));
1122 1123
				dev_info(&card->dev->dev, "Transmitted: port %d\n",
					 port);
1124 1125 1126
				dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
					 size, le16_to_cpu(header->vpi),
					 le16_to_cpu(header->vci));
1127
				print_buffer(oldskb);
1128 1129
			}

1130
			vcc = SKB_CB(oldskb)->vcc;
1131 1132 1133

			if (vcc) {
				atomic_inc(&vcc->stats->tx);
1134
				solos_pop(vcc, oldskb);
1135
			} else {
1136
				dev_kfree_skb_irq(oldskb);
1137 1138
				wake_up(&card->param_wq);
			}
1139 1140
		}
	}
1141
	/* For non-DMA TX, write the 'TX start' bit for all four ports simultaneously */
1142 1143 1144 1145
	if (tx_started)
		iowrite32(tx_started, card->config_regs + FLAGS_ADDR);

	spin_unlock_irqrestore(&card->tx_lock, flags);
1146
	return card_flags;
1147 1148 1149 1150 1151 1152
}

static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
{
	struct solos_card *card = vcc->dev->dev_data;
	struct pkt_hdr *header;
1153
	int pktlen;
1154

1155 1156
	pktlen = skb->len;
	if (pktlen > (BUF_SIZE - sizeof(*header))) {
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
		dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
		solos_pop(vcc, skb);
		return 0;
	}

	if (!skb_clone_writable(skb, sizeof(*header))) {
		int expand_by = 0;
		int ret;

		if (skb_headroom(skb) < sizeof(*header))
			expand_by = sizeof(*header) - skb_headroom(skb);

		ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
		if (ret) {
1171
			dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
1172 1173 1174 1175 1176
			solos_pop(vcc, skb);
			return ret;
		}
	}

1177
	header = skb_push(skb, sizeof(*header));
1178

1179 1180
	/* This does _not_ include the size of the header */
	header->size = cpu_to_le16(pktlen);
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	header->vpi = cpu_to_le16(vcc->vpi);
	header->vci = cpu_to_le16(vcc->vci);
	header->type = cpu_to_le16(PKT_DATA);

	fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);

	return 0;
}

static struct atmdev_ops fpga_ops = {
	.open =		popen,
	.close =	pclose,
	.ioctl =	NULL,
	.getsockopt =	NULL,
	.setsockopt =	NULL,
	.send =		psend,
	.send_oam =	NULL,
	.phy_put =	NULL,
	.phy_get =	NULL,
	.change_qos =	NULL,
	.proc_read =	NULL,
	.owner =	THIS_MODULE
};

static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
1207
	int err;
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
	uint16_t fpga_ver;
	uint8_t major_ver, minor_ver;
	uint32_t data32;
	struct solos_card *card;

	card = kzalloc(sizeof(*card), GFP_KERNEL);
	if (!card)
		return -ENOMEM;

	card->dev = dev;
1218
	init_waitqueue_head(&card->fw_wq);
1219
	init_waitqueue_head(&card->param_wq);
1220 1221 1222 1223 1224 1225 1226

	err = pci_enable_device(dev);
	if (err) {
		dev_warn(&dev->dev,  "Failed to enable PCI device\n");
		goto out;
	}

1227
	err = dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(32));
1228 1229 1230 1231 1232
	if (err) {
		dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
		goto out;
	}

1233 1234 1235 1236 1237 1238 1239 1240 1241
	err = pci_request_regions(dev, "solos");
	if (err) {
		dev_warn(&dev->dev, "Failed to request regions\n");
		goto out;
	}

	card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
	if (!card->config_regs) {
		dev_warn(&dev->dev, "Failed to ioremap config registers\n");
J
Julia Lawall 已提交
1242
		err = -ENOMEM;
1243 1244 1245 1246 1247
		goto out_release_regions;
	}
	card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
	if (!card->buffers) {
		dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
J
Julia Lawall 已提交
1248
		err = -ENOMEM;
1249 1250 1251
		goto out_unmap_config;
	}

1252 1253
	if (reset) {
		iowrite32(1, card->config_regs + FPGA_MODE);
1254
		ioread32(card->config_regs + FPGA_MODE);
1255

1256
		iowrite32(0, card->config_regs + FPGA_MODE);
1257
		ioread32(card->config_regs + FPGA_MODE);
1258
	}
1259 1260 1261 1262 1263

	data32 = ioread32(card->config_regs + FPGA_VER);
	fpga_ver = (data32 & 0x0000FFFF);
	major_ver = ((data32 & 0xFF000000) >> 24);
	minor_ver = ((data32 & 0x00FF0000) >> 16);
1264 1265 1266 1267 1268
	card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
	if (card->fpga_version > LEGACY_BUFFERS)
		card->buffer_size = BUF_SIZE;
	else
		card->buffer_size = OLD_BUF_SIZE;
1269 1270 1271
	dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
		 major_ver, minor_ver, fpga_ver);

1272 1273 1274 1275 1276 1277 1278 1279
	if (fpga_ver < 37 && (fpga_upgrade || firmware_upgrade ||
			      db_fpga_upgrade || db_firmware_upgrade)) {
		dev_warn(&dev->dev,
			 "FPGA too old; cannot upgrade flash. Use JTAG.\n");
		fpga_upgrade = firmware_upgrade = 0;
		db_fpga_upgrade = db_firmware_upgrade = 0;
	}

1280 1281 1282 1283 1284 1285
	/* Stopped using Atmel flash after 0.03-38 */
	if (fpga_ver < 39)
		card->atmel_flash = 1;
	else
		card->atmel_flash = 0;

1286 1287 1288
	data32 = ioread32(card->config_regs + PORTS);
	card->nr_ports = (data32 & 0x000000FF);

D
David Woodhouse 已提交
1289 1290
	if (card->fpga_version >= DMA_SUPPORTED) {
		pci_set_master(dev);
1291
		card->using_dma = 1;
1292 1293 1294 1295 1296
		if (1) { /* All known FPGA versions so far */
			card->dma_alignment = 3;
			card->dma_bounce = kmalloc(card->nr_ports * BUF_SIZE, GFP_KERNEL);
			if (!card->dma_bounce) {
				dev_warn(&card->dev->dev, "Failed to allocate DMA bounce buffers\n");
J
Julia Lawall 已提交
1297
				err = -ENOMEM;
1298 1299 1300 1301
				/* Fallback to MMIO doesn't work */
				goto out_unmap_both;
			}
		}
1302 1303
	} else {
		card->using_dma = 0;
1304 1305 1306
		/* Set RX empty flag for all ports */
		iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
	}
1307 1308

	pci_set_drvdata(dev, card);
1309

1310 1311 1312 1313
	tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
	spin_lock_init(&card->tx_lock);
	spin_lock_init(&card->tx_queue_lock);
	spin_lock_init(&card->cli_queue_lock);
1314 1315
	spin_lock_init(&card->param_queue_lock);
	INIT_LIST_HEAD(&card->param_queue);
1316

1317
	err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
1318
			  "solos-pci", card);
1319
	if (err) {
1320
		dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
1321 1322
		goto out_unmap_both;
	}
1323 1324 1325

	iowrite32(1, card->config_regs + IRQ_EN_ADDR);

1326 1327 1328 1329 1330 1331
	if (fpga_upgrade)
		flash_upgrade(card, 0);

	if (firmware_upgrade)
		flash_upgrade(card, 1);

1332 1333 1334 1335 1336 1337
	if (db_fpga_upgrade)
		flash_upgrade(card, 2);

	if (db_firmware_upgrade)
		flash_upgrade(card, 3);

1338
	err = atm_init(card, &dev->dev);
1339 1340 1341
	if (err)
		goto out_free_irq;

1342 1343 1344 1345
	if (card->fpga_version >= DMA_SUPPORTED &&
	    sysfs_create_group(&card->dev->dev.kobj, &gpio_attr_group))
		dev_err(&card->dev->dev, "Could not register parameter group for GPIOs\n");

1346 1347
	return 0;

1348 1349 1350 1351 1352
 out_free_irq:
	iowrite32(0, card->config_regs + IRQ_EN_ADDR);
	free_irq(dev->irq, card);
	tasklet_kill(&card->tlet);
	
1353
 out_unmap_both:
1354
	kfree(card->dma_bounce);
1355
	pci_iounmap(dev, card->buffers);
1356 1357
 out_unmap_config:
	pci_iounmap(dev, card->config_regs);
1358 1359 1360
 out_release_regions:
	pci_release_regions(dev);
 out:
1361
	kfree(card);
1362 1363 1364
	return err;
}

1365
static int atm_init(struct solos_card *card, struct device *parent)
1366 1367 1368 1369
{
	int i;

	for (i = 0; i < card->nr_ports; i++) {
1370 1371 1372
		struct sk_buff *skb;
		struct pkt_hdr *header;

1373 1374 1375
		skb_queue_head_init(&card->tx_queue[i]);
		skb_queue_head_init(&card->cli_queue[i]);

1376
		card->atmdev[i] = atm_dev_register("solos-pci", parent, &fpga_ops, -1, NULL);
1377 1378 1379 1380 1381 1382 1383
		if (!card->atmdev[i]) {
			dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
			atm_remove(card);
			return -ENODEV;
		}
		if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
			dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
1384 1385
		if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
			dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
1386 1387 1388 1389 1390 1391 1392

		dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);

		card->atmdev[i]->ci_range.vpi_bits = 8;
		card->atmdev[i]->ci_range.vci_bits = 16;
		card->atmdev[i]->dev_data = card;
		card->atmdev[i]->phy_data = (void *)(unsigned long)i;
1393
		atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_FOUND);
1394

1395
		skb = alloc_skb(sizeof(*header), GFP_KERNEL);
1396 1397 1398 1399 1400
		if (!skb) {
			dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
			continue;
		}

1401
		header = skb_put(skb, sizeof(*header));
1402 1403 1404 1405 1406 1407 1408

		header->size = cpu_to_le16(0);
		header->vpi = cpu_to_le16(0);
		header->vci = cpu_to_le16(0);
		header->type = cpu_to_le16(PKT_STATUS);

		fpga_queue(card, i, skb, NULL);
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
	}
	return 0;
}

static void atm_remove(struct solos_card *card)
{
	int i;

	for (i = 0; i < card->nr_ports; i++) {
		if (card->atmdev[i]) {
1419 1420
			struct sk_buff *skb;

1421
			dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
1422 1423

			sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
1424
			atm_dev_deregister(card->atmdev[i]);
1425 1426 1427

			skb = card->rx_skb[i];
			if (skb) {
1428 1429
				dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
						 RX_DMA_SIZE, DMA_FROM_DEVICE);
1430 1431 1432 1433
				dev_kfree_skb(skb);
			}
			skb = card->tx_skb[i];
			if (skb) {
1434 1435
				dma_unmap_single(&card->dev->dev, SKB_CB(skb)->dma_addr,
						 skb->len, DMA_TO_DEVICE);
1436 1437 1438 1439 1440
				dev_kfree_skb(skb);
			}
			while ((skb = skb_dequeue(&card->tx_queue[i])))
				dev_kfree_skb(skb);
 
1441 1442 1443 1444 1445 1446 1447
		}
	}
}

static void fpga_remove(struct pci_dev *dev)
{
	struct solos_card *card = pci_get_drvdata(dev);
1448 1449 1450
	
	/* Disable IRQs */
	iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1451

1452 1453 1454
	/* Reset FPGA */
	iowrite32(1, card->config_regs + FPGA_MODE);
	(void)ioread32(card->config_regs + FPGA_MODE); 
1455

1456 1457 1458
	if (card->fpga_version >= DMA_SUPPORTED)
		sysfs_remove_group(&card->dev->dev.kobj, &gpio_attr_group);

1459 1460 1461 1462 1463
	atm_remove(card);

	free_irq(dev->irq, card);
	tasklet_kill(&card->tlet);

1464 1465
	kfree(card->dma_bounce);

1466 1467 1468 1469
	/* Release device from reset */
	iowrite32(0, card->config_regs + FPGA_MODE);
	(void)ioread32(card->config_regs + FPGA_MODE); 

1470 1471 1472 1473 1474 1475 1476 1477 1478
	pci_iounmap(dev, card->buffers);
	pci_iounmap(dev, card->config_regs);

	pci_release_regions(dev);
	pci_disable_device(dev);

	kfree(card);
}

1479
static const struct pci_device_id fpga_pci_tbl[] = {
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
	{ 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
	{ 0, }
};

MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);

static struct pci_driver fpga_driver = {
	.name =		"solos",
	.id_table =	fpga_pci_tbl,
	.probe =	fpga_probe,
	.remove =	fpga_remove,
};


static int __init solos_pci_init(void)
{
1496 1497
	BUILD_BUG_ON(sizeof(struct solos_skb_cb) > sizeof(((struct sk_buff *)0)->cb));

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
	printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
	return pci_register_driver(&fpga_driver);
}

static void __exit solos_pci_exit(void)
{
	pci_unregister_driver(&fpga_driver);
	printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
}

module_init(solos_pci_init);
module_exit(solos_pci_exit);