1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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>
40#include <linux/firmware.h>
41#include <linux/ctype.h>
42#include <linux/swab.h>
43#include <linux/slab.h>
44
45#define VERSION "0.07"
46#define PTAG "solos-pci"
47
48#define CONFIG_RAM_SIZE 128
49#define FLAGS_ADDR 0x7C
50#define IRQ_EN_ADDR 0x78
51#define FPGA_VER 0x74
52#define IRQ_CLEAR 0x70
53#define WRITE_FLASH 0x6C
54#define PORTS 0x68
55#define FLASH_BLOCK 0x64
56#define FLASH_BUSY 0x60
57#define FPGA_MODE 0x5C
58#define FLASH_MODE 0x58
59#define TX_DMA_ADDR(port) (0x40 + (4 * (port)))
60#define RX_DMA_ADDR(port) (0x30 + (4 * (port)))
61
62#define DATA_RAM_SIZE 32768
63#define BUF_SIZE 2048
64#define OLD_BUF_SIZE 4096
65#define FPGA_PAGE 528
66#define SOLOS_PAGE 512
67#define FPGA_BLOCK (FPGA_PAGE * 8)
68#define SOLOS_BLOCK (SOLOS_PAGE * 8)
69
70#define RX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2)
71#define TX_BUF(card, nr) ((card->buffers) + (nr)*(card->buffer_size)*2 + (card->buffer_size))
72#define FLASH_BUF ((card->buffers) + 4*(card->buffer_size)*2)
73
74#define RX_DMA_SIZE 2048
75
76#define FPGA_VERSION(a,b) (((a) << 8) + (b))
77#define LEGACY_BUFFERS 2
78#define DMA_SUPPORTED 4
79
80static int reset = 0;
81static int atmdebug = 0;
82static int firmware_upgrade = 0;
83static int fpga_upgrade = 0;
84static int db_firmware_upgrade = 0;
85static int db_fpga_upgrade = 0;
86
87struct pkt_hdr {
88 __le16 size;
89 __le16 vpi;
90 __le16 vci;
91 __le16 type;
92};
93
94struct solos_skb_cb {
95 struct atm_vcc *vcc;
96 uint32_t dma_addr;
97};
98
99
100#define SKB_CB(skb) ((struct solos_skb_cb *)skb->cb)
101
102#define PKT_DATA 0
103#define PKT_COMMAND 1
104#define PKT_POPEN 3
105#define PKT_PCLOSE 4
106#define PKT_STATUS 5
107
108struct solos_card {
109 void __iomem *config_regs;
110 void __iomem *buffers;
111 int nr_ports;
112 int tx_mask;
113 struct pci_dev *dev;
114 struct atm_dev *atmdev[4];
115 struct tasklet_struct tlet;
116 spinlock_t tx_lock;
117 spinlock_t tx_queue_lock;
118 spinlock_t cli_queue_lock;
119 spinlock_t param_queue_lock;
120 struct list_head param_queue;
121 struct sk_buff_head tx_queue[4];
122 struct sk_buff_head cli_queue[4];
123 struct sk_buff *tx_skb[4];
124 struct sk_buff *rx_skb[4];
125 wait_queue_head_t param_wq;
126 wait_queue_head_t fw_wq;
127 int using_dma;
128 int fpga_version;
129 int buffer_size;
130};
131
132
133struct solos_param {
134 struct list_head list;
135 pid_t pid;
136 int port;
137 struct sk_buff *response;
138};
139
140#define SOLOS_CHAN(atmdev) ((int)(unsigned long)(atmdev)->phy_data)
141
142MODULE_AUTHOR("Traverse Technologies <support@traverse.com.au>");
143MODULE_DESCRIPTION("Solos PCI driver");
144MODULE_VERSION(VERSION);
145MODULE_LICENSE("GPL");
146MODULE_FIRMWARE("solos-FPGA.bin");
147MODULE_FIRMWARE("solos-Firmware.bin");
148MODULE_FIRMWARE("solos-db-FPGA.bin");
149MODULE_PARM_DESC(reset, "Reset Solos chips on startup");
150MODULE_PARM_DESC(atmdebug, "Print ATM data");
151MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
152MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
153MODULE_PARM_DESC(db_firmware_upgrade, "Initiate daughter board Solos firmware upgrade");
154MODULE_PARM_DESC(db_fpga_upgrade, "Initiate daughter board FPGA upgrade");
155module_param(reset, int, 0444);
156module_param(atmdebug, int, 0644);
157module_param(firmware_upgrade, int, 0444);
158module_param(fpga_upgrade, int, 0444);
159module_param(db_firmware_upgrade, int, 0444);
160module_param(db_fpga_upgrade, int, 0444);
161
162static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
163 struct atm_vcc *vcc);
164static uint32_t fpga_tx(struct solos_card *);
165static irqreturn_t solos_irq(int irq, void *dev_id);
166static struct atm_vcc* find_vcc(struct atm_dev *dev, short vpi, int vci);
167static int list_vccs(int vci);
168static int atm_init(struct solos_card *, struct device *);
169static void atm_remove(struct solos_card *);
170static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
171static void solos_bh(unsigned long);
172static int print_buffer(struct sk_buff *buf);
173
174static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
175{
176 if (vcc->pop)
177 vcc->pop(vcc, skb);
178 else
179 dev_kfree_skb_any(skb);
180}
181
182static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
183 char *buf)
184{
185 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
186 struct solos_card *card = atmdev->dev_data;
187 struct solos_param prm;
188 struct sk_buff *skb;
189 struct pkt_hdr *header;
190 int buflen;
191
192 buflen = strlen(attr->attr.name) + 10;
193
194 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
195 if (!skb) {
196 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
197 return -ENOMEM;
198 }
199
200 header = (void *)skb_put(skb, sizeof(*header));
201
202 buflen = snprintf((void *)&header[1], buflen - 1,
203 "L%05d\n%s\n", current->pid, attr->attr.name);
204 skb_put(skb, buflen);
205
206 header->size = cpu_to_le16(buflen);
207 header->vpi = cpu_to_le16(0);
208 header->vci = cpu_to_le16(0);
209 header->type = cpu_to_le16(PKT_COMMAND);
210
211 prm.pid = current->pid;
212 prm.response = NULL;
213 prm.port = SOLOS_CHAN(atmdev);
214
215 spin_lock_irq(&card->param_queue_lock);
216 list_add(&prm.list, &card->param_queue);
217 spin_unlock_irq(&card->param_queue_lock);
218
219 fpga_queue(card, prm.port, skb, NULL);
220
221 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
222
223 spin_lock_irq(&card->param_queue_lock);
224 list_del(&prm.list);
225 spin_unlock_irq(&card->param_queue_lock);
226
227 if (!prm.response)
228 return -EIO;
229
230 buflen = prm.response->len;
231 memcpy(buf, prm.response->data, buflen);
232 kfree_skb(prm.response);
233
234 return buflen;
235}
236
237static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
238 const char *buf, size_t count)
239{
240 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
241 struct solos_card *card = atmdev->dev_data;
242 struct solos_param prm;
243 struct sk_buff *skb;
244 struct pkt_hdr *header;
245 int buflen;
246 ssize_t ret;
247
248 buflen = strlen(attr->attr.name) + 11 + count;
249
250 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
251 if (!skb) {
252 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
253 return -ENOMEM;
254 }
255
256 header = (void *)skb_put(skb, sizeof(*header));
257
258 buflen = snprintf((void *)&header[1], buflen - 1,
259 "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
260
261 skb_put(skb, buflen);
262 header->size = cpu_to_le16(buflen);
263 header->vpi = cpu_to_le16(0);
264 header->vci = cpu_to_le16(0);
265 header->type = cpu_to_le16(PKT_COMMAND);
266
267 prm.pid = current->pid;
268 prm.response = NULL;
269 prm.port = SOLOS_CHAN(atmdev);
270
271 spin_lock_irq(&card->param_queue_lock);
272 list_add(&prm.list, &card->param_queue);
273 spin_unlock_irq(&card->param_queue_lock);
274
275 fpga_queue(card, prm.port, skb, NULL);
276
277 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
278
279 spin_lock_irq(&card->param_queue_lock);
280 list_del(&prm.list);
281 spin_unlock_irq(&card->param_queue_lock);
282
283 skb = prm.response;
284
285 if (!skb)
286 return -EIO;
287
288 buflen = skb->len;
289
290
291 if (skb->data[buflen - 1] == '\n')
292 buflen--;
293
294 if (buflen == 2 && !strncmp(skb->data, "OK", 2))
295 ret = count;
296 else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
297 ret = -EIO;
298 else {
299
300
301 skb->data[buflen] = 0;
302
303 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
304 skb->data);
305 ret = -EIO;
306 }
307 kfree_skb(skb);
308
309 return ret;
310}
311
312static char *next_string(struct sk_buff *skb)
313{
314 int i = 0;
315 char *this = skb->data;
316
317 for (i = 0; i < skb->len; i++) {
318 if (this[i] == '\n') {
319 this[i] = 0;
320 skb_pull(skb, i + 1);
321 return this;
322 }
323 if (!isprint(this[i]))
324 return NULL;
325 }
326 return NULL;
327}
328
329
330
331
332
333
334
335
336
337
338
339
340static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
341{
342 char *str, *end, *state_str, *snr, *attn;
343 int ver, rate_up, rate_down;
344
345 if (!card->atmdev[port])
346 return -ENODEV;
347
348 str = next_string(skb);
349 if (!str)
350 return -EIO;
351
352 ver = simple_strtol(str, NULL, 10);
353 if (ver < 1) {
354 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
355 ver);
356 return -EIO;
357 }
358
359 str = next_string(skb);
360 if (!str)
361 return -EIO;
362 if (!strcmp(str, "ERROR")) {
363 dev_dbg(&card->dev->dev, "Status packet indicated Solos error on port %d (starting up?)\n",
364 port);
365 return 0;
366 }
367
368 rate_down = simple_strtol(str, &end, 10);
369 if (*end)
370 return -EIO;
371
372 str = next_string(skb);
373 if (!str)
374 return -EIO;
375 rate_up = simple_strtol(str, &end, 10);
376 if (*end)
377 return -EIO;
378
379 state_str = next_string(skb);
380 if (!state_str)
381 return -EIO;
382
383
384 if (strcmp(state_str, "Showtime")) {
385 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
386 dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
387 return 0;
388 }
389
390 snr = next_string(skb);
391 if (!snr)
392 return -EIO;
393 attn = next_string(skb);
394 if (!attn)
395 return -EIO;
396
397 dev_info(&card->dev->dev, "Port %d: %s @%d/%d kb/s%s%s%s%s\n",
398 port, state_str, rate_down/1000, rate_up/1000,
399 snr[0]?", SNR ":"", snr, attn[0]?", Attn ":"", attn);
400
401 card->atmdev[port]->link_rate = rate_down / 424;
402 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
403
404 return 0;
405}
406
407static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
408{
409 struct solos_param *prm;
410 unsigned long flags;
411 int cmdpid;
412 int found = 0;
413
414 if (skb->len < 7)
415 return 0;
416
417 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
418 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
419 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
420 skb->data[6] != '\n')
421 return 0;
422
423 cmdpid = simple_strtol(&skb->data[1], NULL, 10);
424
425 spin_lock_irqsave(&card->param_queue_lock, flags);
426 list_for_each_entry(prm, &card->param_queue, list) {
427 if (prm->port == port && prm->pid == cmdpid) {
428 prm->response = skb;
429 skb_pull(skb, 7);
430 wake_up(&card->param_wq);
431 found = 1;
432 break;
433 }
434 }
435 spin_unlock_irqrestore(&card->param_queue_lock, flags);
436 return found;
437}
438
439static ssize_t console_show(struct device *dev, struct device_attribute *attr,
440 char *buf)
441{
442 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
443 struct solos_card *card = atmdev->dev_data;
444 struct sk_buff *skb;
445 unsigned int len;
446
447 spin_lock(&card->cli_queue_lock);
448 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
449 spin_unlock(&card->cli_queue_lock);
450 if(skb == NULL)
451 return sprintf(buf, "No data.\n");
452
453 len = skb->len;
454 memcpy(buf, skb->data, len);
455 dev_dbg(&card->dev->dev, "len: %d\n", len);
456
457 kfree_skb(skb);
458 return len;
459}
460
461static int send_command(struct solos_card *card, int dev, const char *buf, size_t size)
462{
463 struct sk_buff *skb;
464 struct pkt_hdr *header;
465
466 if (size > (BUF_SIZE - sizeof(*header))) {
467 dev_dbg(&card->dev->dev, "Command is too big. Dropping request\n");
468 return 0;
469 }
470 skb = alloc_skb(size + sizeof(*header), GFP_ATOMIC);
471 if (!skb) {
472 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in send_command()\n");
473 return 0;
474 }
475
476 header = (void *)skb_put(skb, sizeof(*header));
477
478 header->size = cpu_to_le16(size);
479 header->vpi = cpu_to_le16(0);
480 header->vci = cpu_to_le16(0);
481 header->type = cpu_to_le16(PKT_COMMAND);
482
483 memcpy(skb_put(skb, size), buf, size);
484
485 fpga_queue(card, dev, skb, NULL);
486
487 return 0;
488}
489
490static ssize_t console_store(struct device *dev, struct device_attribute *attr,
491 const char *buf, size_t count)
492{
493 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
494 struct solos_card *card = atmdev->dev_data;
495 int err;
496
497 err = send_command(card, SOLOS_CHAN(atmdev), buf, count);
498
499 return err?:count;
500}
501
502static DEVICE_ATTR(console, 0644, console_show, console_store);
503
504
505#define SOLOS_ATTR_RO(x) static DEVICE_ATTR(x, 0444, solos_param_show, NULL);
506#define SOLOS_ATTR_RW(x) static DEVICE_ATTR(x, 0644, solos_param_show, solos_param_store);
507
508#include "solos-attrlist.c"
509
510#undef SOLOS_ATTR_RO
511#undef SOLOS_ATTR_RW
512
513#define SOLOS_ATTR_RO(x) &dev_attr_##x.attr,
514#define SOLOS_ATTR_RW(x) &dev_attr_##x.attr,
515
516static struct attribute *solos_attrs[] = {
517#include "solos-attrlist.c"
518 NULL
519};
520
521static struct attribute_group solos_attr_group = {
522 .attrs = solos_attrs,
523 .name = "parameters",
524};
525
526static int flash_upgrade(struct solos_card *card, int chip)
527{
528 const struct firmware *fw;
529 const char *fw_name;
530 int blocksize = 0;
531 int numblocks = 0;
532 int offset;
533
534 switch (chip) {
535 case 0:
536 fw_name = "solos-FPGA.bin";
537 blocksize = FPGA_BLOCK;
538 break;
539 case 1:
540 fw_name = "solos-Firmware.bin";
541 blocksize = SOLOS_BLOCK;
542 break;
543 case 2:
544 if (card->fpga_version > LEGACY_BUFFERS){
545 fw_name = "solos-db-FPGA.bin";
546 blocksize = FPGA_BLOCK;
547 } else {
548 dev_info(&card->dev->dev, "FPGA version doesn't support"
549 " daughter board upgrades\n");
550 return -EPERM;
551 }
552 break;
553 case 3:
554 if (card->fpga_version > LEGACY_BUFFERS){
555 fw_name = "solos-Firmware.bin";
556 blocksize = SOLOS_BLOCK;
557 } else {
558 dev_info(&card->dev->dev, "FPGA version doesn't support"
559 " daughter board upgrades\n");
560 return -EPERM;
561 }
562 break;
563 default:
564 return -ENODEV;
565 }
566
567 if (request_firmware(&fw, fw_name, &card->dev->dev))
568 return -ENOENT;
569
570 dev_info(&card->dev->dev, "Flash upgrade starting\n");
571
572 numblocks = fw->size / blocksize;
573 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
574 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
575
576 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
577 iowrite32(1, card->config_regs + FPGA_MODE);
578 (void) ioread32(card->config_regs + FPGA_MODE);
579
580
581 if(chip == 0 || chip == 2)
582 dev_info(&card->dev->dev, "Set FPGA Flash mode to FPGA Chip Erase\n");
583 if(chip == 1 || chip == 3)
584 dev_info(&card->dev->dev, "Set FPGA Flash mode to Solos Chip Erase\n");
585 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
586
587
588 iowrite32(1, card->config_regs + WRITE_FLASH);
589 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
590
591 for (offset = 0; offset < fw->size; offset += blocksize) {
592 int i;
593
594
595 iowrite32(0, card->config_regs + WRITE_FLASH);
596
597
598
599 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
600
601
602 for(i = 0; i < blocksize; i += 4) {
603 uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
604 if(card->fpga_version > LEGACY_BUFFERS)
605 iowrite32(word, FLASH_BUF + i);
606 else
607 iowrite32(word, RX_BUF(card, 3) + i);
608 }
609
610
611 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
612 iowrite32(1, card->config_regs + WRITE_FLASH);
613 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
614 }
615
616 release_firmware(fw);
617 iowrite32(0, card->config_regs + WRITE_FLASH);
618 iowrite32(0, card->config_regs + FPGA_MODE);
619 iowrite32(0, card->config_regs + FLASH_MODE);
620 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
621 return 0;
622}
623
624static irqreturn_t solos_irq(int irq, void *dev_id)
625{
626 struct solos_card *card = dev_id;
627 int handled = 1;
628
629 iowrite32(0, card->config_regs + IRQ_CLEAR);
630
631
632 if (card->atmdev[0])
633 tasklet_schedule(&card->tlet);
634 else
635 wake_up(&card->fw_wq);
636
637 return IRQ_RETVAL(handled);
638}
639
640void solos_bh(unsigned long card_arg)
641{
642 struct solos_card *card = (void *)card_arg;
643 uint32_t card_flags;
644 uint32_t rx_done = 0;
645 int port;
646
647
648
649
650
651
652 card_flags = fpga_tx(card);
653
654 for (port = 0; port < card->nr_ports; port++) {
655 if (card_flags & (0x10 << port)) {
656 struct pkt_hdr _hdr, *header;
657 struct sk_buff *skb;
658 struct atm_vcc *vcc;
659 int size;
660
661 if (card->using_dma) {
662 skb = card->rx_skb[port];
663 card->rx_skb[port] = NULL;
664
665 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
666 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
667
668 header = (void *)skb->data;
669 size = le16_to_cpu(header->size);
670 skb_put(skb, size + sizeof(*header));
671 skb_pull(skb, sizeof(*header));
672 } else {
673 header = &_hdr;
674
675 rx_done |= 0x10 << port;
676
677 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
678
679 size = le16_to_cpu(header->size);
680 if (size > (card->buffer_size - sizeof(*header))){
681 dev_warn(&card->dev->dev, "Invalid buffer size\n");
682 continue;
683 }
684
685 skb = alloc_skb(size + 1, GFP_ATOMIC);
686 if (!skb) {
687 if (net_ratelimit())
688 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
689 continue;
690 }
691
692 memcpy_fromio(skb_put(skb, size),
693 RX_BUF(card, port) + sizeof(*header),
694 size);
695 }
696 if (atmdebug) {
697 dev_info(&card->dev->dev, "Received: port %d\n", port);
698 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
699 size, le16_to_cpu(header->vpi),
700 le16_to_cpu(header->vci));
701 print_buffer(skb);
702 }
703
704 switch (le16_to_cpu(header->type)) {
705 case PKT_DATA:
706 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
707 le16_to_cpu(header->vci));
708 if (!vcc) {
709 if (net_ratelimit())
710 dev_warn(&card->dev->dev, "Received packet for unknown VPI.VCI %d.%d on port %d\n",
711 le16_to_cpu(header->vpi), le16_to_cpu(header->vci),
712 port);
713 continue;
714 }
715 atm_charge(vcc, skb->truesize);
716 vcc->push(vcc, skb);
717 atomic_inc(&vcc->stats->rx);
718 break;
719
720 case PKT_STATUS:
721 if (process_status(card, port, skb) &&
722 net_ratelimit()) {
723 dev_warn(&card->dev->dev, "Bad status packet of %d bytes on port %d:\n", skb->len, port);
724 print_buffer(skb);
725 }
726 dev_kfree_skb_any(skb);
727 break;
728
729 case PKT_COMMAND:
730 default:
731 if (process_command(card, port, skb))
732 break;
733 spin_lock(&card->cli_queue_lock);
734 if (skb_queue_len(&card->cli_queue[port]) > 10) {
735 if (net_ratelimit())
736 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
737 port);
738 dev_kfree_skb_any(skb);
739 } else
740 skb_queue_tail(&card->cli_queue[port], skb);
741 spin_unlock(&card->cli_queue_lock);
742 break;
743 }
744 }
745
746 if (card->using_dma && card->atmdev[port] &&
747 !card->rx_skb[port]) {
748 struct sk_buff *skb = alloc_skb(RX_DMA_SIZE, GFP_ATOMIC);
749 if (skb) {
750 SKB_CB(skb)->dma_addr =
751 pci_map_single(card->dev, skb->data,
752 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
753 iowrite32(SKB_CB(skb)->dma_addr,
754 card->config_regs + RX_DMA_ADDR(port));
755 card->rx_skb[port] = skb;
756 } else {
757 if (net_ratelimit())
758 dev_warn(&card->dev->dev, "Failed to allocate RX skb");
759
760
761 tasklet_schedule(&card->tlet);
762 }
763 }
764 }
765 if (rx_done)
766 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
767
768 return;
769}
770
771static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
772{
773 struct hlist_head *head;
774 struct atm_vcc *vcc = NULL;
775 struct hlist_node *node;
776 struct sock *s;
777
778 read_lock(&vcc_sklist_lock);
779 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
780 sk_for_each(s, node, head) {
781 vcc = atm_sk(s);
782 if (vcc->dev == dev && vcc->vci == vci &&
783 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
784 test_bit(ATM_VF_READY, &vcc->flags))
785 goto out;
786 }
787 vcc = NULL;
788 out:
789 read_unlock(&vcc_sklist_lock);
790 return vcc;
791}
792
793static int list_vccs(int vci)
794{
795 struct hlist_head *head;
796 struct atm_vcc *vcc;
797 struct hlist_node *node;
798 struct sock *s;
799 int num_found = 0;
800 int i;
801
802 read_lock(&vcc_sklist_lock);
803 if (vci != 0){
804 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
805 sk_for_each(s, node, head) {
806 num_found ++;
807 vcc = atm_sk(s);
808 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
809 vcc->dev->number,
810 vcc->vpi,
811 vcc->vci);
812 }
813 } else {
814 for(i = 0; i < VCC_HTABLE_SIZE; i++){
815 head = &vcc_hash[i];
816 sk_for_each(s, node, head) {
817 num_found ++;
818 vcc = atm_sk(s);
819 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
820 vcc->dev->number,
821 vcc->vpi,
822 vcc->vci);
823 }
824 }
825 }
826 read_unlock(&vcc_sklist_lock);
827 return num_found;
828}
829
830
831static int popen(struct atm_vcc *vcc)
832{
833 struct solos_card *card = vcc->dev->dev_data;
834 struct sk_buff *skb;
835 struct pkt_hdr *header;
836
837 if (vcc->qos.aal != ATM_AAL5) {
838 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
839 vcc->qos.aal);
840 return -EINVAL;
841 }
842
843 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
844 if (!skb) {
845 if (net_ratelimit())
846 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
847 return -ENOMEM;
848 }
849 header = (void *)skb_put(skb, sizeof(*header));
850
851 header->size = cpu_to_le16(0);
852 header->vpi = cpu_to_le16(vcc->vpi);
853 header->vci = cpu_to_le16(vcc->vci);
854 header->type = cpu_to_le16(PKT_POPEN);
855
856 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
857
858 set_bit(ATM_VF_ADDR, &vcc->flags);
859 set_bit(ATM_VF_READY, &vcc->flags);
860 list_vccs(0);
861
862
863 return 0;
864}
865
866static void pclose(struct atm_vcc *vcc)
867{
868 struct solos_card *card = vcc->dev->dev_data;
869 struct sk_buff *skb;
870 struct pkt_hdr *header;
871
872 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
873 if (!skb) {
874 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
875 return;
876 }
877 header = (void *)skb_put(skb, sizeof(*header));
878
879 header->size = cpu_to_le16(0);
880 header->vpi = cpu_to_le16(vcc->vpi);
881 header->vci = cpu_to_le16(vcc->vci);
882 header->type = cpu_to_le16(PKT_PCLOSE);
883
884 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
885
886 clear_bit(ATM_VF_ADDR, &vcc->flags);
887 clear_bit(ATM_VF_READY, &vcc->flags);
888
889
890
891
892 tasklet_unlock_wait(&card->tlet);
893 return;
894}
895
896static int print_buffer(struct sk_buff *buf)
897{
898 int len,i;
899 char msg[500];
900 char item[10];
901
902 len = buf->len;
903 for (i = 0; i < len; i++){
904 if(i % 8 == 0)
905 sprintf(msg, "%02X: ", i);
906
907 sprintf(item,"%02X ",*(buf->data + i));
908 strcat(msg, item);
909 if(i % 8 == 7) {
910 sprintf(item, "\n");
911 strcat(msg, item);
912 printk(KERN_DEBUG "%s", msg);
913 }
914 }
915 if (i % 8 != 0) {
916 sprintf(item, "\n");
917 strcat(msg, item);
918 printk(KERN_DEBUG "%s", msg);
919 }
920 printk(KERN_DEBUG "\n");
921
922 return 0;
923}
924
925static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
926 struct atm_vcc *vcc)
927{
928 int old_len;
929 unsigned long flags;
930
931 SKB_CB(skb)->vcc = vcc;
932
933 spin_lock_irqsave(&card->tx_queue_lock, flags);
934 old_len = skb_queue_len(&card->tx_queue[port]);
935 skb_queue_tail(&card->tx_queue[port], skb);
936 if (!old_len)
937 card->tx_mask |= (1 << port);
938 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
939
940
941
942 if (!old_len)
943 fpga_tx(card);
944}
945
946static uint32_t fpga_tx(struct solos_card *card)
947{
948 uint32_t tx_pending, card_flags;
949 uint32_t tx_started = 0;
950 struct sk_buff *skb;
951 struct atm_vcc *vcc;
952 unsigned char port;
953 unsigned long flags;
954
955 spin_lock_irqsave(&card->tx_lock, flags);
956
957 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
958
959
960
961
962
963
964
965 tx_pending = card->tx_mask & ~card_flags;
966
967 for (port = 0; tx_pending; tx_pending >>= 1, port++) {
968 if (tx_pending & 1) {
969 struct sk_buff *oldskb = card->tx_skb[port];
970 if (oldskb)
971 pci_unmap_single(card->dev, SKB_CB(oldskb)->dma_addr,
972 oldskb->len, PCI_DMA_TODEVICE);
973
974 spin_lock(&card->tx_queue_lock);
975 skb = skb_dequeue(&card->tx_queue[port]);
976 if (!skb)
977 card->tx_mask &= ~(1 << port);
978 spin_unlock(&card->tx_queue_lock);
979
980 if (skb && !card->using_dma) {
981 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
982 tx_started |= 1 << port;
983 oldskb = skb;
984 } else if (skb && card->using_dma) {
985 SKB_CB(skb)->dma_addr = pci_map_single(card->dev, skb->data,
986 skb->len, PCI_DMA_TODEVICE);
987 iowrite32(SKB_CB(skb)->dma_addr,
988 card->config_regs + TX_DMA_ADDR(port));
989 }
990
991 if (!oldskb)
992 continue;
993
994
995 if (atmdebug) {
996 struct pkt_hdr *header = (void *)oldskb->data;
997 int size = le16_to_cpu(header->size);
998
999 skb_pull(oldskb, sizeof(*header));
1000 dev_info(&card->dev->dev, "Transmitted: port %d\n",
1001 port);
1002 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
1003 size, le16_to_cpu(header->vpi),
1004 le16_to_cpu(header->vci));
1005 print_buffer(oldskb);
1006 }
1007
1008 vcc = SKB_CB(oldskb)->vcc;
1009
1010 if (vcc) {
1011 atomic_inc(&vcc->stats->tx);
1012 solos_pop(vcc, oldskb);
1013 } else
1014 dev_kfree_skb_irq(oldskb);
1015
1016 }
1017 }
1018
1019 if (tx_started)
1020 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
1021
1022 spin_unlock_irqrestore(&card->tx_lock, flags);
1023 return card_flags;
1024}
1025
1026static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
1027{
1028 struct solos_card *card = vcc->dev->dev_data;
1029 struct pkt_hdr *header;
1030 int pktlen;
1031
1032 pktlen = skb->len;
1033 if (pktlen > (BUF_SIZE - sizeof(*header))) {
1034 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
1035 solos_pop(vcc, skb);
1036 return 0;
1037 }
1038
1039 if (!skb_clone_writable(skb, sizeof(*header))) {
1040 int expand_by = 0;
1041 int ret;
1042
1043 if (skb_headroom(skb) < sizeof(*header))
1044 expand_by = sizeof(*header) - skb_headroom(skb);
1045
1046 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
1047 if (ret) {
1048 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
1049 solos_pop(vcc, skb);
1050 return ret;
1051 }
1052 }
1053
1054 header = (void *)skb_push(skb, sizeof(*header));
1055
1056
1057 header->size = cpu_to_le16(pktlen);
1058 header->vpi = cpu_to_le16(vcc->vpi);
1059 header->vci = cpu_to_le16(vcc->vci);
1060 header->type = cpu_to_le16(PKT_DATA);
1061
1062 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1063
1064 return 0;
1065}
1066
1067static struct atmdev_ops fpga_ops = {
1068 .open = popen,
1069 .close = pclose,
1070 .ioctl = NULL,
1071 .getsockopt = NULL,
1072 .setsockopt = NULL,
1073 .send = psend,
1074 .send_oam = NULL,
1075 .phy_put = NULL,
1076 .phy_get = NULL,
1077 .change_qos = NULL,
1078 .proc_read = NULL,
1079 .owner = THIS_MODULE
1080};
1081
1082static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
1083{
1084 int err;
1085 uint16_t fpga_ver;
1086 uint8_t major_ver, minor_ver;
1087 uint32_t data32;
1088 struct solos_card *card;
1089
1090 card = kzalloc(sizeof(*card), GFP_KERNEL);
1091 if (!card)
1092 return -ENOMEM;
1093
1094 card->dev = dev;
1095 init_waitqueue_head(&card->fw_wq);
1096 init_waitqueue_head(&card->param_wq);
1097
1098 err = pci_enable_device(dev);
1099 if (err) {
1100 dev_warn(&dev->dev, "Failed to enable PCI device\n");
1101 goto out;
1102 }
1103
1104 err = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
1105 if (err) {
1106 dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
1107 goto out;
1108 }
1109
1110 err = pci_request_regions(dev, "solos");
1111 if (err) {
1112 dev_warn(&dev->dev, "Failed to request regions\n");
1113 goto out;
1114 }
1115
1116 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1117 if (!card->config_regs) {
1118 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
1119 goto out_release_regions;
1120 }
1121 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1122 if (!card->buffers) {
1123 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
1124 goto out_unmap_config;
1125 }
1126
1127 if (reset) {
1128 iowrite32(1, card->config_regs + FPGA_MODE);
1129 data32 = ioread32(card->config_regs + FPGA_MODE);
1130
1131 iowrite32(0, card->config_regs + FPGA_MODE);
1132 data32 = ioread32(card->config_regs + FPGA_MODE);
1133 }
1134
1135 data32 = ioread32(card->config_regs + FPGA_VER);
1136 fpga_ver = (data32 & 0x0000FFFF);
1137 major_ver = ((data32 & 0xFF000000) >> 24);
1138 minor_ver = ((data32 & 0x00FF0000) >> 16);
1139 card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
1140 if (card->fpga_version > LEGACY_BUFFERS)
1141 card->buffer_size = BUF_SIZE;
1142 else
1143 card->buffer_size = OLD_BUF_SIZE;
1144 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1145 major_ver, minor_ver, fpga_ver);
1146
1147 if (fpga_ver < 37 && (fpga_upgrade || firmware_upgrade ||
1148 db_fpga_upgrade || db_firmware_upgrade)) {
1149 dev_warn(&dev->dev,
1150 "FPGA too old; cannot upgrade flash. Use JTAG.\n");
1151 fpga_upgrade = firmware_upgrade = 0;
1152 db_fpga_upgrade = db_firmware_upgrade = 0;
1153 }
1154
1155 if (card->fpga_version >= DMA_SUPPORTED){
1156 card->using_dma = 1;
1157 } else {
1158 card->using_dma = 0;
1159
1160 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1161 }
1162
1163 data32 = ioread32(card->config_regs + PORTS);
1164 card->nr_ports = (data32 & 0x000000FF);
1165
1166 pci_set_drvdata(dev, card);
1167
1168 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1169 spin_lock_init(&card->tx_lock);
1170 spin_lock_init(&card->tx_queue_lock);
1171 spin_lock_init(&card->cli_queue_lock);
1172 spin_lock_init(&card->param_queue_lock);
1173 INIT_LIST_HEAD(&card->param_queue);
1174
1175 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
1176 "solos-pci", card);
1177 if (err) {
1178 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
1179 goto out_unmap_both;
1180 }
1181
1182 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1183
1184 if (fpga_upgrade)
1185 flash_upgrade(card, 0);
1186
1187 if (firmware_upgrade)
1188 flash_upgrade(card, 1);
1189
1190 if (db_fpga_upgrade)
1191 flash_upgrade(card, 2);
1192
1193 if (db_firmware_upgrade)
1194 flash_upgrade(card, 3);
1195
1196 err = atm_init(card, &dev->dev);
1197 if (err)
1198 goto out_free_irq;
1199
1200 return 0;
1201
1202 out_free_irq:
1203 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1204 free_irq(dev->irq, card);
1205 tasklet_kill(&card->tlet);
1206
1207 out_unmap_both:
1208 pci_set_drvdata(dev, NULL);
1209 pci_iounmap(dev, card->config_regs);
1210 out_unmap_config:
1211 pci_iounmap(dev, card->buffers);
1212 out_release_regions:
1213 pci_release_regions(dev);
1214 out:
1215 kfree(card);
1216 return err;
1217}
1218
1219static int atm_init(struct solos_card *card, struct device *parent)
1220{
1221 int i;
1222
1223 for (i = 0; i < card->nr_ports; i++) {
1224 struct sk_buff *skb;
1225 struct pkt_hdr *header;
1226
1227 skb_queue_head_init(&card->tx_queue[i]);
1228 skb_queue_head_init(&card->cli_queue[i]);
1229
1230 card->atmdev[i] = atm_dev_register("solos-pci", parent, &fpga_ops, -1, NULL);
1231 if (!card->atmdev[i]) {
1232 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1233 atm_remove(card);
1234 return -ENODEV;
1235 }
1236 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1237 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
1238 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1239 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
1240
1241 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1242
1243 card->atmdev[i]->ci_range.vpi_bits = 8;
1244 card->atmdev[i]->ci_range.vci_bits = 16;
1245 card->atmdev[i]->dev_data = card;
1246 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
1247 atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_FOUND);
1248
1249 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
1250 if (!skb) {
1251 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1252 continue;
1253 }
1254
1255 header = (void *)skb_put(skb, sizeof(*header));
1256
1257 header->size = cpu_to_le16(0);
1258 header->vpi = cpu_to_le16(0);
1259 header->vci = cpu_to_le16(0);
1260 header->type = cpu_to_le16(PKT_STATUS);
1261
1262 fpga_queue(card, i, skb, NULL);
1263 }
1264 return 0;
1265}
1266
1267static void atm_remove(struct solos_card *card)
1268{
1269 int i;
1270
1271 for (i = 0; i < card->nr_ports; i++) {
1272 if (card->atmdev[i]) {
1273 struct sk_buff *skb;
1274
1275 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
1276
1277 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
1278 atm_dev_deregister(card->atmdev[i]);
1279
1280 skb = card->rx_skb[i];
1281 if (skb) {
1282 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1283 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
1284 dev_kfree_skb(skb);
1285 }
1286 skb = card->tx_skb[i];
1287 if (skb) {
1288 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1289 skb->len, PCI_DMA_TODEVICE);
1290 dev_kfree_skb(skb);
1291 }
1292 while ((skb = skb_dequeue(&card->tx_queue[i])))
1293 dev_kfree_skb(skb);
1294
1295 }
1296 }
1297}
1298
1299static void fpga_remove(struct pci_dev *dev)
1300{
1301 struct solos_card *card = pci_get_drvdata(dev);
1302
1303
1304 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1305
1306
1307 iowrite32(1, card->config_regs + FPGA_MODE);
1308 (void)ioread32(card->config_regs + FPGA_MODE);
1309
1310 atm_remove(card);
1311
1312 free_irq(dev->irq, card);
1313 tasklet_kill(&card->tlet);
1314
1315
1316 iowrite32(0, card->config_regs + FPGA_MODE);
1317 (void)ioread32(card->config_regs + FPGA_MODE);
1318
1319 pci_iounmap(dev, card->buffers);
1320 pci_iounmap(dev, card->config_regs);
1321
1322 pci_release_regions(dev);
1323 pci_disable_device(dev);
1324
1325 pci_set_drvdata(dev, NULL);
1326 kfree(card);
1327}
1328
1329static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1330 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1331 { 0, }
1332};
1333
1334MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1335
1336static struct pci_driver fpga_driver = {
1337 .name = "solos",
1338 .id_table = fpga_pci_tbl,
1339 .probe = fpga_probe,
1340 .remove = fpga_remove,
1341};
1342
1343
1344static int __init solos_pci_init(void)
1345{
1346 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1347 return pci_register_driver(&fpga_driver);
1348}
1349
1350static void __exit solos_pci_exit(void)
1351{
1352 pci_unregister_driver(&fpga_driver);
1353 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1354}
1355
1356module_init(solos_pci_init);
1357module_exit(solos_pci_exit);
1358