blob: 89bdf733af90201cc18d77187ef6e220263e1744 [file] [log] [blame]
David Woodhouse9c540042008-12-23 04:09:02 +00001/*
2 * Driver for the Solos PCI ADSL2+ card, designed to support Linux by
3 * Traverse Technologies -- http://www.traverse.com.au/
4 * Xrio Limited -- http://www.xrio.com/
5 *
6 *
7 * Copyright © 2008 Traverse Technologies
8 * Copyright © 2008 Intel Corporation
9 *
10 * Authors: Nathan Williams <nathan@traverse.com.au>
11 * David Woodhouse <dwmw2@infradead.org>
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000012 * Treker Chen <treker@xrio.com>
David Woodhouse9c540042008-12-23 04:09:02 +000013 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * version 2, as published by the Free Software Foundation.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#define DEBUG
25#define VERBOSE_DEBUG
26
27#include <linux/interrupt.h>
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/errno.h>
31#include <linux/ioport.h>
32#include <linux/types.h>
33#include <linux/pci.h>
34#include <linux/atm.h>
35#include <linux/atmdev.h>
36#include <linux/skbuff.h>
37#include <linux/sysfs.h>
38#include <linux/device.h>
39#include <linux/kobject.h>
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000040#include <linux/firmware.h>
David Woodhouse9c540042008-12-23 04:09:02 +000041
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000042#define VERSION "0.07"
David Woodhouse9c540042008-12-23 04:09:02 +000043#define PTAG "solos-pci"
44
45#define CONFIG_RAM_SIZE 128
46#define FLAGS_ADDR 0x7C
47#define IRQ_EN_ADDR 0x78
48#define FPGA_VER 0x74
49#define IRQ_CLEAR 0x70
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000050#define WRITE_FLASH 0x6C
51#define PORTS 0x68
52#define FLASH_BLOCK 0x64
53#define FLASH_BUSY 0x60
54#define FPGA_MODE 0x5C
55#define FLASH_MODE 0x58
David Woodhouse9c540042008-12-23 04:09:02 +000056
57#define DATA_RAM_SIZE 32768
58#define BUF_SIZE 4096
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000059#define FPGA_PAGE 528 /* FPGA flash page size*/
60#define SOLOS_PAGE 512 /* Solos flash page size*/
61#define FPGA_BLOCK (FPGA_PAGE * 8) /* FPGA flash block size*/
62#define SOLOS_BLOCK (SOLOS_PAGE * 8) /* Solos flash block size*/
David Woodhouse9c540042008-12-23 04:09:02 +000063
64#define RX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2)
65#define TX_BUF(card, nr) ((card->buffers) + (nr)*BUF_SIZE*2 + BUF_SIZE)
66
67static int debug = 0;
68static int atmdebug = 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000069static int firmware_upgrade = 0;
70static int fpga_upgrade = 0;
David Woodhouse9c540042008-12-23 04:09:02 +000071
72struct pkt_hdr {
73 __le16 size;
74 __le16 vpi;
75 __le16 vci;
76 __le16 type;
77};
78
79#define PKT_DATA 0
80#define PKT_COMMAND 1
81#define PKT_POPEN 3
82#define PKT_PCLOSE 4
83
84struct solos_card {
85 void __iomem *config_regs;
86 void __iomem *buffers;
87 int nr_ports;
88 struct pci_dev *dev;
89 struct atm_dev *atmdev[4];
90 struct tasklet_struct tlet;
91 spinlock_t tx_lock;
92 spinlock_t tx_queue_lock;
93 spinlock_t cli_queue_lock;
94 struct sk_buff_head tx_queue[4];
95 struct sk_buff_head cli_queue[4];
David Woodhousefa755b92009-01-27 14:16:12 +110096 wait_queue_head_t fw_wq;
David Woodhouse9c540042008-12-23 04:09:02 +000097};
98
99#define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
100
101MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
102MODULE_DESCRIPTION("Solos PCI driver");
103MODULE_VERSION(VERSION);
104MODULE_LICENSE("GPL");
105MODULE_PARM_DESC(debug, "Enable Loopback");
106MODULE_PARM_DESC(atmdebug, "Print ATM data");
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000107MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
108MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
David Woodhouse9c540042008-12-23 04:09:02 +0000109module_param(debug, int, 0444);
Simon Farnsworth4306cad2009-01-19 21:19:29 +0000110module_param(atmdebug, int, 0644);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000111module_param(firmware_upgrade, int, 0444);
112module_param(fpga_upgrade, int, 0444);
David Woodhouse9c540042008-12-23 04:09:02 +0000113
114static int opens;
115
116static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
117 struct atm_vcc *vcc);
118static int fpga_tx(struct solos_card *);
119static irqreturn_t solos_irq(int irq, void *dev_id);
120static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
121static int list_vccs(int vci);
122static int atm_init(struct solos_card *);
123static void atm_remove(struct solos_card *);
124static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
125static void solos_bh(unsigned long);
126static int print_buffer(struct sk_buff *buf);
127
128static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
129{
130 if (vcc->pop)
131 vcc->pop(vcc, skb);
132 else
133 dev_kfree_skb_any(skb);
134}
135
136static ssize_t console_show(struct device *dev, struct device_attribute *attr,
137 char *buf)
138{
139 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
140 struct solos_card *card = atmdev->dev_data;
141 struct sk_buff *skb;
142
143 spin_lock(&card->cli_queue_lock);
144 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
145 spin_unlock(&card->cli_queue_lock);
146 if(skb == NULL)
147 return sprintf(buf, "No data.\n");
148
149 memcpy(buf, skb->data, skb->len);
150 dev_dbg(&card->dev->dev, "len: %d\n", skb->len);
151
152 kfree_skb(skb);
153 return skb->len;
154}
155
156static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
157{
158 struct sk_buff *skb;
159 struct pkt_hdr *header;
160
161// dev_dbg(&card->dev->dev, "size: %d\n", size);
162
163 if (size > (BUF_SIZE - sizeof(*header))) {
164 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
165 return 0;
166 }
167 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
168 if (!skb) {
169 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
170 return 0;
171 }
172
173 header = (void *)skb_put(skb, sizeof(*header));
174
175 header->size = cpu_to_le16(size);
176 header->vpi = cpu_to_le16(0);
177 header->vci = cpu_to_le16(0);
178 header->type = cpu_to_le16(PKT_COMMAND);
179
180 memcpy(skb_put(skb, size), buf, size);
181
182 fpga_queue(card, dev, skb, NULL);
183
184 return 0;
185}
186
187static ssize_t console_store(struct device *dev, struct device_attribute *attr,
188 const char *buf, size_t count)
189{
190 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
191 struct solos_card *card = atmdev->dev_data;
192 int err;
193
194 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
195
196 return err?:count;
197}
198
199static DEVICE_ATTR(console, 0644, console_show, console_store);
200
David Woodhousefa755b92009-01-27 14:16:12 +1100201static int flash_upgrade(struct solos_card *card, int chip)
202{
203 const struct firmware *fw;
204 const char *fw_name;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000205 uint32_t data32 = 0;
206 int blocksize = 0;
207 int numblocks = 0;
David Woodhousefa755b92009-01-27 14:16:12 +1100208 int offset;
209
210 if (chip == 0) {
211 fw_name = "solos-FPGA.bin";
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000212 blocksize = FPGA_BLOCK;
213 } else {
David Woodhousefa755b92009-01-27 14:16:12 +1100214 fw_name = "solos-Firmware.bin";
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000215 blocksize = SOLOS_BLOCK;
216 }
David Woodhousefa755b92009-01-27 14:16:12 +1100217
218 if (request_firmware(&fw, fw_name, &card->dev->dev))
219 return -ENOENT;
220
221 dev_info(&card->dev->dev, "Flash upgrade starting\n");
222
223 numblocks = fw->size / blocksize;
224 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000225 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
226
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000227 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
228 iowrite32(1, card->config_regs + FPGA_MODE);
229 data32 = ioread32(card->config_regs + FPGA_MODE);
David Woodhousefa755b92009-01-27 14:16:12 +1100230
231 /* Set mode to Chip Erase */
232 dev_info(&card->dev->dev, "Set FPGA Flash mode to %s Chip Erase\n",
233 chip?"Solos":"FPGA");
234 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
235
236
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000237 iowrite32(1, card->config_regs + WRITE_FLASH);
David Woodhousefa755b92009-01-27 14:16:12 +1100238 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000239
David Woodhousefa755b92009-01-27 14:16:12 +1100240 for (offset = 0; offset < fw->size; offset += blocksize) {
241 int i;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000242
David Woodhousefa755b92009-01-27 14:16:12 +1100243 /* Clear write flag */
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000244 iowrite32(0, card->config_regs + WRITE_FLASH);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000245
David Woodhousefa755b92009-01-27 14:16:12 +1100246 /* Set mode to Block Write */
247 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
248 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
249
250 /* Copy block to buffer, swapping each 16 bits */
251 for(i = 0; i < blocksize; i += 4) {
252 uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
253 iowrite32(word, RX_BUF(card, 3) + i);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000254 }
255
David Woodhousefa755b92009-01-27 14:16:12 +1100256 /* Specify block number and then trigger flash write */
257 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
258 iowrite32(1, card->config_regs + WRITE_FLASH);
259 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000260 }
261
David Woodhousefa755b92009-01-27 14:16:12 +1100262 release_firmware(fw);
263 iowrite32(0, card->config_regs + WRITE_FLASH);
264 iowrite32(0, card->config_regs + FPGA_MODE);
265 iowrite32(0, card->config_regs + FLASH_MODE);
266 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
267 return 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000268}
269
David Woodhouse9c540042008-12-23 04:09:02 +0000270static irqreturn_t solos_irq(int irq, void *dev_id)
271{
272 struct solos_card *card = dev_id;
273 int handled = 1;
274
275 //ACK IRQ
276 iowrite32(0, card->config_regs + IRQ_CLEAR);
277 //Disable IRQs from FPGA
278 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
279
David Woodhousefa755b92009-01-27 14:16:12 +1100280 if (card->atmdev[0])
David Woodhouse9c540042008-12-23 04:09:02 +0000281 tasklet_schedule(&card->tlet);
David Woodhousefa755b92009-01-27 14:16:12 +1100282 else
283 wake_up(&card->fw_wq);
David Woodhouse9c540042008-12-23 04:09:02 +0000284
285 //Enable IRQs from FPGA
286 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
287 return IRQ_RETVAL(handled);
288}
289
290void solos_bh(unsigned long card_arg)
291{
292 struct solos_card *card = (void *)card_arg;
293 int port;
294 uint32_t card_flags;
295 uint32_t tx_mask;
296 uint32_t rx_done = 0;
297
298 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
299
300 /* The TX bits are set if the channel is busy; clear if not. We want to
301 invoke fpga_tx() unless _all_ the bits for active channels are set */
302 tx_mask = (1 << card->nr_ports) - 1;
303 if ((card_flags & tx_mask) != tx_mask)
304 fpga_tx(card);
305
306 for (port = 0; port < card->nr_ports; port++) {
307 if (card_flags & (0x10 << port)) {
308 struct pkt_hdr header;
309 struct sk_buff *skb;
310 struct atm_vcc *vcc;
311 int size;
312
313 rx_done |= 0x10 << port;
314
315 memcpy_fromio(&header, RX_BUF(card, port), sizeof(header));
316
317 size = le16_to_cpu(header.size);
318
319 skb = alloc_skb(size, GFP_ATOMIC);
320 if (!skb) {
321 if (net_ratelimit())
322 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
323 continue;
324 }
325
326 memcpy_fromio(skb_put(skb, size),
327 RX_BUF(card, port) + sizeof(header),
328 size);
329
330 if (atmdebug) {
331 dev_info(&card->dev->dev, "Received: device %d\n", port);
332 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
333 size, le16_to_cpu(header.vpi),
334 le16_to_cpu(header.vci));
335 print_buffer(skb);
336 }
337
338 switch (le16_to_cpu(header.type)) {
339 case PKT_DATA:
340 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header.vpi),
341 le16_to_cpu(header.vci));
342 if (!vcc) {
343 if (net_ratelimit())
344 dev_warn(&card->dev->dev, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
345 le16_to_cpu(header.vci), le16_to_cpu(header.vpi),
346 port);
347 continue;
348 }
349 atm_charge(vcc, skb->truesize);
350 vcc->push(vcc, skb);
351 atomic_inc(&vcc->stats->rx);
352 break;
353
354 case PKT_COMMAND:
355 default: /* FIXME: Not really, surely? */
356 spin_lock(&card->cli_queue_lock);
357 if (skb_queue_len(&card->cli_queue[port]) > 10) {
358 if (net_ratelimit())
359 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
360 port);
361 } else
362 skb_queue_tail(&card->cli_queue[port], skb);
363 spin_unlock(&card->cli_queue_lock);
364 break;
365 }
366 }
367 }
368 if (rx_done)
369 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
370
371 return;
372}
373
374static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
375{
376 struct hlist_head *head;
377 struct atm_vcc *vcc = NULL;
378 struct hlist_node *node;
379 struct sock *s;
380
381 read_lock(&vcc_sklist_lock);
382 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
383 sk_for_each(s, node, head) {
384 vcc = atm_sk(s);
385 if (vcc->dev == dev && vcc->vci == vci &&
386 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE)
387 goto out;
388 }
389 vcc = NULL;
390 out:
391 read_unlock(&vcc_sklist_lock);
392 return vcc;
393}
394
395static int list_vccs(int vci)
396{
397 struct hlist_head *head;
398 struct atm_vcc *vcc;
399 struct hlist_node *node;
400 struct sock *s;
401 int num_found = 0;
402 int i;
403
404 read_lock(&vcc_sklist_lock);
405 if (vci != 0){
406 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
407 sk_for_each(s, node, head) {
408 num_found ++;
409 vcc = atm_sk(s);
410 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
411 vcc->dev->number,
412 vcc->vpi,
413 vcc->vci);
414 }
415 } else {
416 for(i=0; i<32; i++){
417 head = &vcc_hash[i];
418 sk_for_each(s, node, head) {
419 num_found ++;
420 vcc = atm_sk(s);
421 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
422 vcc->dev->number,
423 vcc->vpi,
424 vcc->vci);
425 }
426 }
427 }
428 read_unlock(&vcc_sklist_lock);
429 return num_found;
430}
431
432
433static int popen(struct atm_vcc *vcc)
434{
435 struct solos_card *card = vcc->dev->dev_data;
436 struct sk_buff *skb;
437 struct pkt_hdr *header;
438
439 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
440 if (!skb && net_ratelimit()) {
441 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
442 return -ENOMEM;
443 }
444 header = (void *)skb_put(skb, sizeof(*header));
445
David Woodhouseb76811a2009-01-27 10:18:51 +1100446 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000447 header->vpi = cpu_to_le16(vcc->vpi);
448 header->vci = cpu_to_le16(vcc->vci);
449 header->type = cpu_to_le16(PKT_POPEN);
450
451 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
452
453// dev_dbg(&card->dev->dev, "Open for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
454 set_bit(ATM_VF_ADDR, &vcc->flags); // accept the vpi / vci
455 set_bit(ATM_VF_READY, &vcc->flags);
456 list_vccs(0);
457
458 if (!opens)
459 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
460
461 opens++; //count open PVCs
462
463 return 0;
464}
465
466static void pclose(struct atm_vcc *vcc)
467{
468 struct solos_card *card = vcc->dev->dev_data;
469 struct sk_buff *skb;
470 struct pkt_hdr *header;
471
472 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
473 if (!skb) {
474 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
475 return;
476 }
477 header = (void *)skb_put(skb, sizeof(*header));
478
David Woodhouseb76811a2009-01-27 10:18:51 +1100479 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000480 header->vpi = cpu_to_le16(vcc->vpi);
481 header->vci = cpu_to_le16(vcc->vci);
482 header->type = cpu_to_le16(PKT_PCLOSE);
483
484 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
485
486// dev_dbg(&card->dev->dev, "Close for vpi %d and vci %d on interface %d\n", vcc->vpi, vcc->vci, SOLOS_CHAN(vcc->dev));
487 if (!--opens)
488 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
489
490 clear_bit(ATM_VF_ADDR, &vcc->flags);
491 clear_bit(ATM_VF_READY, &vcc->flags);
492
493 return;
494}
495
496static int print_buffer(struct sk_buff *buf)
497{
498 int len,i;
499 char msg[500];
500 char item[10];
501
502 len = buf->len;
503 for (i = 0; i < len; i++){
504 if(i % 8 == 0)
505 sprintf(msg, "%02X: ", i);
506
507 sprintf(item,"%02X ",*(buf->data + i));
508 strcat(msg, item);
509 if(i % 8 == 7) {
510 sprintf(item, "\n");
511 strcat(msg, item);
512 printk(KERN_DEBUG "%s", msg);
513 }
514 }
515 if (i % 8 != 0) {
516 sprintf(item, "\n");
517 strcat(msg, item);
518 printk(KERN_DEBUG "%s", msg);
519 }
520 printk(KERN_DEBUG "\n");
521
522 return 0;
523}
524
525static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
526 struct atm_vcc *vcc)
527{
528 int old_len;
529
530 *(void **)skb->cb = vcc;
531
532 spin_lock(&card->tx_queue_lock);
533 old_len = skb_queue_len(&card->tx_queue[port]);
534 skb_queue_tail(&card->tx_queue[port], skb);
535 spin_unlock(&card->tx_queue_lock);
536
537 /* If TX might need to be started, do so */
538 if (!old_len)
539 fpga_tx(card);
540}
541
542static int fpga_tx(struct solos_card *card)
543{
544 uint32_t tx_pending;
545 uint32_t tx_started = 0;
546 struct sk_buff *skb;
547 struct atm_vcc *vcc;
548 unsigned char port;
549 unsigned long flags;
550
551 spin_lock_irqsave(&card->tx_lock, flags);
552
553 tx_pending = ioread32(card->config_regs + FLAGS_ADDR);
554
555 dev_vdbg(&card->dev->dev, "TX Flags are %X\n", tx_pending);
556
557 for (port = 0; port < card->nr_ports; port++) {
558 if (!(tx_pending & (1 << port))) {
559
560 spin_lock(&card->tx_queue_lock);
561 skb = skb_dequeue(&card->tx_queue[port]);
562 spin_unlock(&card->tx_queue_lock);
563
564 if (!skb)
565 continue;
566
567 if (atmdebug) {
568 dev_info(&card->dev->dev, "Transmitted: port %d\n",
569 port);
570 print_buffer(skb);
571 }
572 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
573
574 vcc = *(void **)skb->cb;
575
576 if (vcc) {
577 atomic_inc(&vcc->stats->tx);
578 solos_pop(vcc, skb);
579 } else
580 dev_kfree_skb_irq(skb);
581
582 tx_started |= 1 << port; //Set TX full flag
583 }
584 }
585 if (tx_started)
586 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
587
588 spin_unlock_irqrestore(&card->tx_lock, flags);
589 return 0;
590}
591
592static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
593{
594 struct solos_card *card = vcc->dev->dev_data;
595 struct sk_buff *skb2 = NULL;
596 struct pkt_hdr *header;
David Woodhouseb76811a2009-01-27 10:18:51 +1100597 int pktlen;
David Woodhouse9c540042008-12-23 04:09:02 +0000598
599 //dev_dbg(&card->dev->dev, "psend called.\n");
600 //dev_dbg(&card->dev->dev, "dev,vpi,vci = %d,%d,%d\n",SOLOS_CHAN(vcc->dev),vcc->vpi,vcc->vci);
601
602 if (debug) {
603 skb2 = atm_alloc_charge(vcc, skb->len, GFP_ATOMIC);
604 if (skb2) {
605 memcpy(skb2->data, skb->data, skb->len);
606 skb_put(skb2, skb->len);
607 vcc->push(vcc, skb2);
608 atomic_inc(&vcc->stats->rx);
609 }
610 atomic_inc(&vcc->stats->tx);
611 solos_pop(vcc, skb);
612 return 0;
613 }
614
David Woodhouseb76811a2009-01-27 10:18:51 +1100615 pktlen = skb->len;
616 if (pktlen > (BUF_SIZE - sizeof(*header))) {
David Woodhouse9c540042008-12-23 04:09:02 +0000617 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
618 solos_pop(vcc, skb);
619 return 0;
620 }
621
622 if (!skb_clone_writable(skb, sizeof(*header))) {
623 int expand_by = 0;
624 int ret;
625
626 if (skb_headroom(skb) < sizeof(*header))
627 expand_by = sizeof(*header) - skb_headroom(skb);
628
629 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
630 if (ret) {
Simon Farnsworth4306cad2009-01-19 21:19:29 +0000631 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
David Woodhouse9c540042008-12-23 04:09:02 +0000632 solos_pop(vcc, skb);
633 return ret;
634 }
635 }
636
637 header = (void *)skb_push(skb, sizeof(*header));
638
David Woodhouseb76811a2009-01-27 10:18:51 +1100639 /* This does _not_ include the size of the header */
640 header->size = cpu_to_le16(pktlen);
David Woodhouse9c540042008-12-23 04:09:02 +0000641 header->vpi = cpu_to_le16(vcc->vpi);
642 header->vci = cpu_to_le16(vcc->vci);
643 header->type = cpu_to_le16(PKT_DATA);
644
645 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
646
647 return 0;
648}
649
650static struct atmdev_ops fpga_ops = {
651 .open = popen,
652 .close = pclose,
653 .ioctl = NULL,
654 .getsockopt = NULL,
655 .setsockopt = NULL,
656 .send = psend,
657 .send_oam = NULL,
658 .phy_put = NULL,
659 .phy_get = NULL,
660 .change_qos = NULL,
661 .proc_read = NULL,
662 .owner = THIS_MODULE
663};
664
665static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
666{
667 int err, i;
668 uint16_t fpga_ver;
669 uint8_t major_ver, minor_ver;
670 uint32_t data32;
671 struct solos_card *card;
672
673 if (debug)
674 return 0;
675
676 card = kzalloc(sizeof(*card), GFP_KERNEL);
677 if (!card)
678 return -ENOMEM;
679
680 card->dev = dev;
David Woodhousefa755b92009-01-27 14:16:12 +1100681 init_waitqueue_head(&card->fw_wq);
David Woodhouse9c540042008-12-23 04:09:02 +0000682
683 err = pci_enable_device(dev);
684 if (err) {
685 dev_warn(&dev->dev, "Failed to enable PCI device\n");
686 goto out;
687 }
688
689 err = pci_request_regions(dev, "solos");
690 if (err) {
691 dev_warn(&dev->dev, "Failed to request regions\n");
692 goto out;
693 }
694
695 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
696 if (!card->config_regs) {
697 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
698 goto out_release_regions;
699 }
700 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
701 if (!card->buffers) {
702 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
703 goto out_unmap_config;
704 }
705
706// for(i=0;i<64 ;i+=4){
707// data32=ioread32(card->buffers + i);
708// dev_dbg(&card->dev->dev, "%08lX\n",(unsigned long)data32);
709// }
710
711 //Fill Config Mem with zeros
712 for(i = 0; i < 128; i += 4)
713 iowrite32(0, card->config_regs + i);
714
715 //Set RX empty flags
716 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
717
718 data32 = ioread32(card->config_regs + FPGA_VER);
719 fpga_ver = (data32 & 0x0000FFFF);
720 major_ver = ((data32 & 0xFF000000) >> 24);
721 minor_ver = ((data32 & 0x00FF0000) >> 16);
722 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
723 major_ver, minor_ver, fpga_ver);
724
725 card->nr_ports = 2; /* FIXME: Detect daughterboard */
726
David Woodhouse9c540042008-12-23 04:09:02 +0000727 pci_set_drvdata(dev, card);
David Woodhousefa755b92009-01-27 14:16:12 +1100728
David Woodhouse9c540042008-12-23 04:09:02 +0000729 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
730 spin_lock_init(&card->tx_lock);
731 spin_lock_init(&card->tx_queue_lock);
732 spin_lock_init(&card->cli_queue_lock);
David Woodhousefa755b92009-01-27 14:16:12 +1100733
David Woodhouse9c540042008-12-23 04:09:02 +0000734/*
735 // Set Loopback mode
736 data32 = 0x00010000;
737 iowrite32(data32,card->config_regs + FLAGS_ADDR);
738*/
739/*
740 // Fill Buffers with zeros
741 for (i = 0; i < BUF_SIZE * 8; i += 4)
742 iowrite32(0, card->buffers + i);
743*/
744/*
745 for(i = 0; i < (BUF_SIZE * 1); i += 4)
746 iowrite32(0x12345678, card->buffers + i + (0*BUF_SIZE));
747 for(i = 0; i < (BUF_SIZE * 1); i += 4)
748 iowrite32(0xabcdef98, card->buffers + i + (1*BUF_SIZE));
749
750 // Read Config Memory
751 printk(KERN_DEBUG "Reading Config MEM\n");
752 i = 0;
753 for(i = 0; i < 16; i++) {
754 data32=ioread32(card->buffers + i*(BUF_SIZE/2));
755 printk(KERN_ALERT "Addr: %lX Data: %08lX\n",
756 (unsigned long)(addr_start + i*(BUF_SIZE/2)),
757 (unsigned long)data32);
758 }
759*/
760 //dev_dbg(&card->dev->dev, "Requesting IRQ: %d\n",dev->irq);
761 err = request_irq(dev->irq, solos_irq, IRQF_DISABLED|IRQF_SHARED,
762 "solos-pci", card);
David Woodhousefa755b92009-01-27 14:16:12 +1100763 if (err) {
David Woodhouse9c540042008-12-23 04:09:02 +0000764 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
David Woodhousefa755b92009-01-27 14:16:12 +1100765 goto out_unmap_both;
766 }
David Woodhouse9c540042008-12-23 04:09:02 +0000767
768 // Enable IRQs
769 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
770
David Woodhousefa755b92009-01-27 14:16:12 +1100771 if (fpga_upgrade)
772 flash_upgrade(card, 0);
773
774 if (firmware_upgrade)
775 flash_upgrade(card, 1);
776
777 err = atm_init(card);
778 if (err)
779 goto out_free_irq;
780
David Woodhouse9c540042008-12-23 04:09:02 +0000781 return 0;
782
David Woodhousefa755b92009-01-27 14:16:12 +1100783 out_free_irq:
784 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
785 free_irq(dev->irq, card);
786 tasklet_kill(&card->tlet);
787
David Woodhouse9c540042008-12-23 04:09:02 +0000788 out_unmap_both:
David Woodhousefa755b92009-01-27 14:16:12 +1100789 pci_set_drvdata(dev, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +0000790 pci_iounmap(dev, card->config_regs);
791 out_unmap_config:
792 pci_iounmap(dev, card->buffers);
793 out_release_regions:
794 pci_release_regions(dev);
795 out:
796 return err;
797}
798
799static int atm_init(struct solos_card *card)
800{
801 int i;
802
803 opens = 0;
804
805 for (i = 0; i < card->nr_ports; i++) {
806 skb_queue_head_init(&card->tx_queue[i]);
807 skb_queue_head_init(&card->cli_queue[i]);
808
809 card->atmdev[i] = atm_dev_register("solos-pci", &fpga_ops, -1, NULL);
810 if (!card->atmdev[i]) {
811 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
812 atm_remove(card);
813 return -ENODEV;
814 }
815 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
816 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
817
818 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
819
820 card->atmdev[i]->ci_range.vpi_bits = 8;
821 card->atmdev[i]->ci_range.vci_bits = 16;
822 card->atmdev[i]->dev_data = card;
823 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
824 }
825 return 0;
826}
827
828static void atm_remove(struct solos_card *card)
829{
830 int i;
831
832 for (i = 0; i < card->nr_ports; i++) {
833 if (card->atmdev[i]) {
834 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
835 atm_dev_deregister(card->atmdev[i]);
836 }
837 }
838}
839
840static void fpga_remove(struct pci_dev *dev)
841{
842 struct solos_card *card = pci_get_drvdata(dev);
843
844 if (debug)
845 return;
846
847 atm_remove(card);
848
849 dev_vdbg(&dev->dev, "Freeing IRQ\n");
850 // Disable IRQs from FPGA
851 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
852 free_irq(dev->irq, card);
853 tasklet_kill(&card->tlet);
854
855 // iowrite32(0x01,pciregs);
856 dev_vdbg(&dev->dev, "Unmapping PCI resource\n");
857 pci_iounmap(dev, card->buffers);
858 pci_iounmap(dev, card->config_regs);
859
860 dev_vdbg(&dev->dev, "Releasing PCI Region\n");
861 pci_release_regions(dev);
862 pci_disable_device(dev);
863
864 pci_set_drvdata(dev, NULL);
865 kfree(card);
866// dev_dbg(&card->dev->dev, "fpga_remove\n");
867 return;
868}
869
870static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
871 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
872 { 0, }
873};
874
875MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
876
877static struct pci_driver fpga_driver = {
878 .name = "solos",
879 .id_table = fpga_pci_tbl,
880 .probe = fpga_probe,
881 .remove = fpga_remove,
882};
883
884
885static int __init solos_pci_init(void)
886{
887 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
888 return pci_register_driver(&fpga_driver);
889}
890
891static void __exit solos_pci_exit(void)
892{
893 pci_unregister_driver(&fpga_driver);
894 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
895}
896
897module_init(solos_pci_init);
898module_exit(solos_pci_exit);