blob: f916ddf63938a03444c2ad6e4d8a6b63c46212aa [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 Woodhouse01e2ffa2009-01-27 16:20:04 +110041#include <linux/ctype.h>
42#include <linux/swab.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
David Woodhouse9c540042008-12-23 04:09:02 +000044
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000045#define VERSION "0.07"
David Woodhouse9c540042008-12-23 04:09:02 +000046#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
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000053#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
David Woodhouse90937232009-01-28 16:46:56 +110059#define TX_DMA_ADDR(port) (0x40 + (4 * (port)))
60#define RX_DMA_ADDR(port) (0x30 + (4 * (port)))
David Woodhouse9c540042008-12-23 04:09:02 +000061
62#define DATA_RAM_SIZE 32768
Nathan Williams4dbedf42009-03-25 20:27:37 +110063#define BUF_SIZE 2048
64#define OLD_BUF_SIZE 4096 /* For FPGA versions <= 2*/
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000065#define FPGA_PAGE 528 /* FPGA flash page size*/
66#define SOLOS_PAGE 512 /* Solos flash page size*/
67#define FPGA_BLOCK (FPGA_PAGE * 8) /* FPGA flash block size*/
68#define SOLOS_BLOCK (SOLOS_PAGE * 8) /* Solos flash block size*/
David Woodhouse9c540042008-12-23 04:09:02 +000069
Nathan Williams4dbedf42009-03-25 20:27:37 +110070#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)
David Woodhouse9c540042008-12-23 04:09:02 +000073
David Woodhouseeaf83e32009-01-29 10:51:11 +110074#define RX_DMA_SIZE 2048
75
Nathan Williams4dbedf42009-03-25 20:27:37 +110076#define FPGA_VERSION(a,b) (((a) << 8) + (b))
77#define LEGACY_BUFFERS 2
78#define DMA_SUPPORTED 4
79
David Woodhousecc3657e2009-01-30 14:23:22 +110080static int reset = 0;
David Woodhouse9c540042008-12-23 04:09:02 +000081static int atmdebug = 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +000082static int firmware_upgrade = 0;
83static int fpga_upgrade = 0;
Nathan Williams4dbedf42009-03-25 20:27:37 +110084static int db_firmware_upgrade = 0;
85static int db_fpga_upgrade = 0;
David Woodhouse9c540042008-12-23 04:09:02 +000086
87struct pkt_hdr {
88 __le16 size;
89 __le16 vpi;
90 __le16 vci;
91 __le16 type;
92};
93
David Woodhouse90937232009-01-28 16:46:56 +110094struct 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
David Woodhouse9c540042008-12-23 04:09:02 +0000102#define PKT_DATA 0
103#define PKT_COMMAND 1
104#define PKT_POPEN 3
105#define PKT_PCLOSE 4
David Woodhouse87ebb182009-01-27 20:02:30 +1100106#define PKT_STATUS 5
David Woodhouse9c540042008-12-23 04:09:02 +0000107
108struct solos_card {
109 void __iomem *config_regs;
110 void __iomem *buffers;
111 int nr_ports;
David Woodhousef69e4172009-01-29 11:10:58 +1100112 int tx_mask;
David Woodhouse9c540042008-12-23 04:09:02 +0000113 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;
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100119 spinlock_t param_queue_lock;
120 struct list_head param_queue;
David Woodhouse9c540042008-12-23 04:09:02 +0000121 struct sk_buff_head tx_queue[4];
122 struct sk_buff_head cli_queue[4];
David Woodhouse90937232009-01-28 16:46:56 +1100123 struct sk_buff *tx_skb[4];
124 struct sk_buff *rx_skb[4];
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100125 wait_queue_head_t param_wq;
David Woodhousefa755b92009-01-27 14:16:12 +1100126 wait_queue_head_t fw_wq;
David Woodhouse90937232009-01-28 16:46:56 +1100127 int using_dma;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100128 int fpga_version;
129 int buffer_size;
David Woodhouse9c540042008-12-23 04:09:02 +0000130};
131
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100132
133struct solos_param {
134 struct list_head list;
135 pid_t pid;
136 int port;
137 struct sk_buff *response;
David Woodhouse9c540042008-12-23 04:09:02 +0000138};
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");
Ben Hutchings9fca79d2009-11-07 11:40:32 +0000146MODULE_FIRMWARE("solos-FPGA.bin");
147MODULE_FIRMWARE("solos-Firmware.bin");
148MODULE_FIRMWARE("solos-db-FPGA.bin");
David Woodhousecc3657e2009-01-30 14:23:22 +1100149MODULE_PARM_DESC(reset, "Reset Solos chips on startup");
David Woodhouse9c540042008-12-23 04:09:02 +0000150MODULE_PARM_DESC(atmdebug, "Print ATM data");
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000151MODULE_PARM_DESC(firmware_upgrade, "Initiate Solos firmware upgrade");
152MODULE_PARM_DESC(fpga_upgrade, "Initiate FPGA upgrade");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100153MODULE_PARM_DESC(db_firmware_upgrade, "Initiate daughter board Solos firmware upgrade");
154MODULE_PARM_DESC(db_fpga_upgrade, "Initiate daughter board FPGA upgrade");
David Woodhousecc3657e2009-01-30 14:23:22 +1100155module_param(reset, int, 0444);
Simon Farnsworth4306cad2009-01-19 21:19:29 +0000156module_param(atmdebug, int, 0644);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000157module_param(firmware_upgrade, int, 0444);
158module_param(fpga_upgrade, int, 0444);
Nathan Williams4dbedf42009-03-25 20:27:37 +1100159module_param(db_firmware_upgrade, int, 0444);
160module_param(db_fpga_upgrade, int, 0444);
David Woodhouse9c540042008-12-23 04:09:02 +0000161
162static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
163 struct atm_vcc *vcc);
David Woodhouse35c22212009-01-29 14:08:27 +1100164static uint32_t fpga_tx(struct solos_card *);
David Woodhouse9c540042008-12-23 04:09:02 +0000165static 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);
David Woodhouse1e615df2009-01-27 21:47:47 +1100168static void release_vccs(struct atm_dev *dev);
David Woodhouse9c540042008-12-23 04:09:02 +0000169static int atm_init(struct solos_card *);
170static void atm_remove(struct solos_card *);
171static int send_command(struct solos_card *card, int dev, const char *buf, size_t size);
172static void solos_bh(unsigned long);
173static int print_buffer(struct sk_buff *buf);
174
175static inline void solos_pop(struct atm_vcc *vcc, struct sk_buff *skb)
176{
177 if (vcc->pop)
178 vcc->pop(vcc, skb);
179 else
180 dev_kfree_skb_any(skb);
181}
182
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100183static ssize_t solos_param_show(struct device *dev, struct device_attribute *attr,
184 char *buf)
185{
186 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
187 struct solos_card *card = atmdev->dev_data;
188 struct solos_param prm;
189 struct sk_buff *skb;
190 struct pkt_hdr *header;
191 int buflen;
192
193 buflen = strlen(attr->attr.name) + 10;
194
David Woodhouse3456b222009-01-28 10:39:23 +1100195 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100196 if (!skb) {
197 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_show()\n");
198 return -ENOMEM;
199 }
200
201 header = (void *)skb_put(skb, sizeof(*header));
202
203 buflen = snprintf((void *)&header[1], buflen - 1,
204 "L%05d\n%s\n", current->pid, attr->attr.name);
205 skb_put(skb, buflen);
206
207 header->size = cpu_to_le16(buflen);
208 header->vpi = cpu_to_le16(0);
209 header->vci = cpu_to_le16(0);
210 header->type = cpu_to_le16(PKT_COMMAND);
211
212 prm.pid = current->pid;
213 prm.response = NULL;
214 prm.port = SOLOS_CHAN(atmdev);
215
216 spin_lock_irq(&card->param_queue_lock);
217 list_add(&prm.list, &card->param_queue);
218 spin_unlock_irq(&card->param_queue_lock);
219
220 fpga_queue(card, prm.port, skb, NULL);
221
222 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
223
224 spin_lock_irq(&card->param_queue_lock);
225 list_del(&prm.list);
226 spin_unlock_irq(&card->param_queue_lock);
227
228 if (!prm.response)
229 return -EIO;
230
231 buflen = prm.response->len;
232 memcpy(buf, prm.response->data, buflen);
233 kfree_skb(prm.response);
234
235 return buflen;
236}
237
238static ssize_t solos_param_store(struct device *dev, struct device_attribute *attr,
239 const char *buf, size_t count)
240{
241 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
242 struct solos_card *card = atmdev->dev_data;
243 struct solos_param prm;
244 struct sk_buff *skb;
245 struct pkt_hdr *header;
246 int buflen;
247 ssize_t ret;
248
249 buflen = strlen(attr->attr.name) + 11 + count;
250
David Woodhouse3456b222009-01-28 10:39:23 +1100251 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100252 if (!skb) {
253 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in solos_param_store()\n");
254 return -ENOMEM;
255 }
256
257 header = (void *)skb_put(skb, sizeof(*header));
258
259 buflen = snprintf((void *)&header[1], buflen - 1,
260 "L%05d\n%s\n%s\n", current->pid, attr->attr.name, buf);
261
262 skb_put(skb, buflen);
263 header->size = cpu_to_le16(buflen);
264 header->vpi = cpu_to_le16(0);
265 header->vci = cpu_to_le16(0);
266 header->type = cpu_to_le16(PKT_COMMAND);
267
268 prm.pid = current->pid;
269 prm.response = NULL;
270 prm.port = SOLOS_CHAN(atmdev);
271
272 spin_lock_irq(&card->param_queue_lock);
273 list_add(&prm.list, &card->param_queue);
274 spin_unlock_irq(&card->param_queue_lock);
275
276 fpga_queue(card, prm.port, skb, NULL);
277
278 wait_event_timeout(card->param_wq, prm.response, 5 * HZ);
279
280 spin_lock_irq(&card->param_queue_lock);
281 list_del(&prm.list);
282 spin_unlock_irq(&card->param_queue_lock);
283
284 skb = prm.response;
285
286 if (!skb)
287 return -EIO;
288
289 buflen = skb->len;
290
291 /* Sometimes it has a newline, sometimes it doesn't. */
292 if (skb->data[buflen - 1] == '\n')
293 buflen--;
294
295 if (buflen == 2 && !strncmp(skb->data, "OK", 2))
296 ret = count;
297 else if (buflen == 5 && !strncmp(skb->data, "ERROR", 5))
298 ret = -EIO;
299 else {
300 /* We know we have enough space allocated for this; we allocated
301 it ourselves */
302 skb->data[buflen] = 0;
303
304 dev_warn(&card->dev->dev, "Unexpected parameter response: '%s'\n",
305 skb->data);
306 ret = -EIO;
307 }
308 kfree_skb(skb);
309
310 return ret;
311}
312
David Woodhouse87ebb182009-01-27 20:02:30 +1100313static char *next_string(struct sk_buff *skb)
314{
315 int i = 0;
316 char *this = skb->data;
David Woodhousec6428e52009-01-29 12:17:09 +1100317
318 for (i = 0; i < skb->len; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +1100319 if (this[i] == '\n') {
320 this[i] = 0;
David Woodhousec6428e52009-01-29 12:17:09 +1100321 skb_pull(skb, i + 1);
David Woodhouse87ebb182009-01-27 20:02:30 +1100322 return this;
323 }
David Woodhousec6428e52009-01-29 12:17:09 +1100324 if (!isprint(this[i]))
325 return NULL;
David Woodhouse87ebb182009-01-27 20:02:30 +1100326 }
327 return NULL;
328}
329
330/*
331 * Status packet has fields separated by \n, starting with a version number
332 * for the information therein. Fields are....
333 *
334 * packet version
David Woodhouse87ebb182009-01-27 20:02:30 +1100335 * RxBitRate (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100336 * TxBitRate (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100337 * State (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100338 * LocalSNRMargin (version >= 1)
339 * LocalLineAttn (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100340 */
341static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
342{
David Woodhousef87b2ed2009-01-30 14:31:36 +1100343 char *str, *end, *state_str, *snr, *attn;
344 int ver, rate_up, rate_down;
David Woodhouse87ebb182009-01-27 20:02:30 +1100345
346 if (!card->atmdev[port])
347 return -ENODEV;
348
349 str = next_string(skb);
350 if (!str)
351 return -EIO;
352
353 ver = simple_strtol(str, NULL, 10);
354 if (ver < 1) {
355 dev_warn(&card->dev->dev, "Unexpected status interrupt version %d\n",
356 ver);
357 return -EIO;
358 }
359
360 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100361 if (!str)
362 return -EIO;
David Woodhouse95852f42009-01-30 14:23:52 +1100363 if (!strcmp(str, "ERROR")) {
364 dev_dbg(&card->dev->dev, "Status packet indicated Solos error on port %d (starting up?)\n",
365 port);
366 return 0;
367 }
368
David Woodhousef87b2ed2009-01-30 14:31:36 +1100369 rate_down = simple_strtol(str, &end, 10);
David Woodhouse87ebb182009-01-27 20:02:30 +1100370 if (*end)
371 return -EIO;
372
373 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100374 if (!str)
375 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100376 rate_up = simple_strtol(str, &end, 10);
David Woodhouse87ebb182009-01-27 20:02:30 +1100377 if (*end)
378 return -EIO;
379
David Woodhouseaf780652009-01-28 10:22:57 +1100380 state_str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100381 if (!state_str)
382 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100383
384 /* Anything but 'Showtime' is down */
385 if (strcmp(state_str, "Showtime")) {
Karl Hiramoto49d49102010-07-08 20:55:34 +0000386 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
David Woodhouse1e615df2009-01-27 21:47:47 +1100387 release_vccs(card->atmdev[port]);
David Woodhousef87b2ed2009-01-30 14:31:36 +1100388 dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
389 return 0;
David Woodhouse1e615df2009-01-27 21:47:47 +1100390 }
David Woodhouse87ebb182009-01-27 20:02:30 +1100391
David Woodhousef87b2ed2009-01-30 14:31:36 +1100392 snr = next_string(skb);
Julia Lawall6cf57672009-07-27 11:38:52 -0700393 if (!snr)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100394 return -EIO;
395 attn = next_string(skb);
396 if (!attn)
397 return -EIO;
David Woodhouseaf780652009-01-28 10:22:57 +1100398
David Woodhousef87b2ed2009-01-30 14:31:36 +1100399 dev_info(&card->dev->dev, "Port %d: %s @%d/%d kb/s%s%s%s%s\n",
400 port, state_str, rate_down/1000, rate_up/1000,
401 snr[0]?", SNR ":"", snr, attn[0]?", Attn ":"", attn);
402
David Woodhousec6428e52009-01-29 12:17:09 +1100403 card->atmdev[port]->link_rate = rate_down / 424;
Karl Hiramoto49d49102010-07-08 20:55:34 +0000404 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +1100405
David Woodhouse87ebb182009-01-27 20:02:30 +1100406 return 0;
407}
408
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100409static int process_command(struct solos_card *card, int port, struct sk_buff *skb)
410{
411 struct solos_param *prm;
412 unsigned long flags;
413 int cmdpid;
414 int found = 0;
415
416 if (skb->len < 7)
417 return 0;
418
419 if (skb->data[0] != 'L' || !isdigit(skb->data[1]) ||
420 !isdigit(skb->data[2]) || !isdigit(skb->data[3]) ||
421 !isdigit(skb->data[4]) || !isdigit(skb->data[5]) ||
422 skb->data[6] != '\n')
423 return 0;
424
425 cmdpid = simple_strtol(&skb->data[1], NULL, 10);
426
427 spin_lock_irqsave(&card->param_queue_lock, flags);
428 list_for_each_entry(prm, &card->param_queue, list) {
429 if (prm->port == port && prm->pid == cmdpid) {
430 prm->response = skb;
431 skb_pull(skb, 7);
432 wake_up(&card->param_wq);
433 found = 1;
434 break;
435 }
436 }
437 spin_unlock_irqrestore(&card->param_queue_lock, flags);
438 return found;
439}
440
David Woodhouse9c540042008-12-23 04:09:02 +0000441static ssize_t console_show(struct device *dev, struct device_attribute *attr,
442 char *buf)
443{
444 struct atm_dev *atmdev = container_of(dev, struct atm_dev, class_dev);
445 struct solos_card *card = atmdev->dev_data;
446 struct sk_buff *skb;
447
448 spin_lock(&card->cli_queue_lock);
449 skb = skb_dequeue(&card->cli_queue[SOLOS_CHAN(atmdev)]);
450 spin_unlock(&card->cli_queue_lock);
451 if(skb == NULL)
452 return sprintf(buf, "No data.\n");
453
454 memcpy(buf, skb->data, skb->len);
455 dev_dbg(&card->dev->dev, "len: %d\n", skb->len);
456
457 kfree_skb(skb);
458 return skb->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
David Woodhouse9c540042008-12-23 04:09:02 +0000466 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
David Woodhoused057f0a2009-01-27 16:52:07 +1100504
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};
David Woodhouse9c540042008-12-23 04:09:02 +0000525
David Woodhousefa755b92009-01-27 14:16:12 +1100526static int flash_upgrade(struct solos_card *card, int chip)
527{
528 const struct firmware *fw;
529 const char *fw_name;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000530 uint32_t data32 = 0;
531 int blocksize = 0;
532 int numblocks = 0;
David Woodhousefa755b92009-01-27 14:16:12 +1100533 int offset;
534
Andrew Morton7adcdb42009-11-17 12:46:44 +0000535 switch (chip) {
536 case 0:
David Woodhousefa755b92009-01-27 14:16:12 +1100537 fw_name = "solos-FPGA.bin";
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000538 blocksize = FPGA_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000539 break;
540 case 1:
David Woodhousefa755b92009-01-27 14:16:12 +1100541 fw_name = "solos-Firmware.bin";
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000542 blocksize = SOLOS_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000543 break;
544 case 2:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100545 if (card->fpga_version > LEGACY_BUFFERS){
546 fw_name = "solos-db-FPGA.bin";
547 blocksize = FPGA_BLOCK;
548 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000549 dev_info(&card->dev->dev, "FPGA version doesn't support"
550 " daughter board upgrades\n");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100551 return -EPERM;
552 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000553 break;
554 case 3:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100555 if (card->fpga_version > LEGACY_BUFFERS){
556 fw_name = "solos-Firmware.bin";
557 blocksize = SOLOS_BLOCK;
558 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000559 dev_info(&card->dev->dev, "FPGA version doesn't support"
560 " daughter board upgrades\n");
561 return -EPERM;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100562 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000563 break;
564 default:
565 return -ENODEV;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100566 }
David Woodhousefa755b92009-01-27 14:16:12 +1100567
568 if (request_firmware(&fw, fw_name, &card->dev->dev))
569 return -ENOENT;
570
571 dev_info(&card->dev->dev, "Flash upgrade starting\n");
572
573 numblocks = fw->size / blocksize;
574 dev_info(&card->dev->dev, "Firmware size: %zd\n", fw->size);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000575 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
576
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000577 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
578 iowrite32(1, card->config_regs + FPGA_MODE);
579 data32 = ioread32(card->config_regs + FPGA_MODE);
David Woodhousefa755b92009-01-27 14:16:12 +1100580
581 /* Set mode to Chip Erase */
Nathan Williams4dbedf42009-03-25 20:27:37 +1100582 if(chip == 0 || chip == 2)
583 dev_info(&card->dev->dev, "Set FPGA Flash mode to FPGA Chip Erase\n");
584 if(chip == 1 || chip == 3)
585 dev_info(&card->dev->dev, "Set FPGA Flash mode to Solos Chip Erase\n");
David Woodhousefa755b92009-01-27 14:16:12 +1100586 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
587
588
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000589 iowrite32(1, card->config_regs + WRITE_FLASH);
David Woodhousefa755b92009-01-27 14:16:12 +1100590 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000591
David Woodhousefa755b92009-01-27 14:16:12 +1100592 for (offset = 0; offset < fw->size; offset += blocksize) {
593 int i;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000594
David Woodhousefa755b92009-01-27 14:16:12 +1100595 /* Clear write flag */
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000596 iowrite32(0, card->config_regs + WRITE_FLASH);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000597
David Woodhousefa755b92009-01-27 14:16:12 +1100598 /* Set mode to Block Write */
599 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
600 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
601
602 /* Copy block to buffer, swapping each 16 bits */
603 for(i = 0; i < blocksize; i += 4) {
604 uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
Nathan Williams4dbedf42009-03-25 20:27:37 +1100605 if(card->fpga_version > LEGACY_BUFFERS)
606 iowrite32(word, FLASH_BUF + i);
607 else
608 iowrite32(word, RX_BUF(card, 3) + i);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000609 }
610
David Woodhousefa755b92009-01-27 14:16:12 +1100611 /* Specify block number and then trigger flash write */
612 iowrite32(offset / blocksize, card->config_regs + FLASH_BLOCK);
613 iowrite32(1, card->config_regs + WRITE_FLASH);
614 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000615 }
616
David Woodhousefa755b92009-01-27 14:16:12 +1100617 release_firmware(fw);
618 iowrite32(0, card->config_regs + WRITE_FLASH);
619 iowrite32(0, card->config_regs + FPGA_MODE);
620 iowrite32(0, card->config_regs + FLASH_MODE);
621 dev_info(&card->dev->dev, "Returning FPGA to Data mode\n");
622 return 0;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000623}
624
David Woodhouse9c540042008-12-23 04:09:02 +0000625static irqreturn_t solos_irq(int irq, void *dev_id)
626{
627 struct solos_card *card = dev_id;
628 int handled = 1;
629
David Woodhouse9c540042008-12-23 04:09:02 +0000630 iowrite32(0, card->config_regs + IRQ_CLEAR);
David Woodhouse9c540042008-12-23 04:09:02 +0000631
David Woodhouse35c22212009-01-29 14:08:27 +1100632 /* If we're up and running, just kick the tasklet to process TX/RX */
David Woodhousefa755b92009-01-27 14:16:12 +1100633 if (card->atmdev[0])
David Woodhouse9c540042008-12-23 04:09:02 +0000634 tasklet_schedule(&card->tlet);
David Woodhousefa755b92009-01-27 14:16:12 +1100635 else
636 wake_up(&card->fw_wq);
David Woodhouse9c540042008-12-23 04:09:02 +0000637
David Woodhouse9c540042008-12-23 04:09:02 +0000638 return IRQ_RETVAL(handled);
639}
640
641void solos_bh(unsigned long card_arg)
642{
643 struct solos_card *card = (void *)card_arg;
David Woodhouse9c540042008-12-23 04:09:02 +0000644 uint32_t card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000645 uint32_t rx_done = 0;
David Woodhouse35c22212009-01-29 14:08:27 +1100646 int port;
David Woodhouse9c540042008-12-23 04:09:02 +0000647
David Woodhouse35c22212009-01-29 14:08:27 +1100648 /*
649 * Since fpga_tx() is going to need to read the flags under its lock,
650 * it can return them to us so that we don't have to hit PCI MMIO
651 * again for the same information
652 */
653 card_flags = fpga_tx(card);
David Woodhouse9c540042008-12-23 04:09:02 +0000654
655 for (port = 0; port < card->nr_ports; port++) {
656 if (card_flags & (0x10 << port)) {
David Woodhouse90937232009-01-28 16:46:56 +1100657 struct pkt_hdr _hdr, *header;
David Woodhouse9c540042008-12-23 04:09:02 +0000658 struct sk_buff *skb;
659 struct atm_vcc *vcc;
660 int size;
661
David Woodhouse90937232009-01-28 16:46:56 +1100662 if (card->using_dma) {
663 skb = card->rx_skb[port];
David Woodhouseeaf83e32009-01-29 10:51:11 +1100664 card->rx_skb[port] = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000665
David Woodhouseeaf83e32009-01-29 10:51:11 +1100666 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
667 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
David Woodhouse9c540042008-12-23 04:09:02 +0000668
David Woodhouse90937232009-01-28 16:46:56 +1100669 header = (void *)skb->data;
670 size = le16_to_cpu(header->size);
671 skb_put(skb, size + sizeof(*header));
672 skb_pull(skb, sizeof(*header));
673 } else {
674 header = &_hdr;
David Woodhouse9c540042008-12-23 04:09:02 +0000675
David Woodhouse90937232009-01-28 16:46:56 +1100676 rx_done |= 0x10 << port;
David Woodhouse9c540042008-12-23 04:09:02 +0000677
David Woodhouse90937232009-01-28 16:46:56 +1100678 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
679
680 size = le16_to_cpu(header->size);
Nathan Williams78f857f2009-03-25 20:33:42 +1100681 if (size > (card->buffer_size - sizeof(*header))){
682 dev_warn(&card->dev->dev, "Invalid buffer size\n");
683 continue;
684 }
David Woodhouse90937232009-01-28 16:46:56 +1100685
686 skb = alloc_skb(size + 1, GFP_ATOMIC);
687 if (!skb) {
688 if (net_ratelimit())
689 dev_warn(&card->dev->dev, "Failed to allocate sk_buff for RX\n");
690 continue;
691 }
692
693 memcpy_fromio(skb_put(skb, size),
694 RX_BUF(card, port) + sizeof(*header),
695 size);
David Woodhouse9c540042008-12-23 04:09:02 +0000696 }
David Woodhouse9c540042008-12-23 04:09:02 +0000697 if (atmdebug) {
698 dev_info(&card->dev->dev, "Received: device %d\n", port);
699 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
David Woodhouse90937232009-01-28 16:46:56 +1100700 size, le16_to_cpu(header->vpi),
701 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000702 print_buffer(skb);
703 }
704
David Woodhouse90937232009-01-28 16:46:56 +1100705 switch (le16_to_cpu(header->type)) {
David Woodhouse9c540042008-12-23 04:09:02 +0000706 case PKT_DATA:
David Woodhouse90937232009-01-28 16:46:56 +1100707 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
708 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000709 if (!vcc) {
710 if (net_ratelimit())
711 dev_warn(&card->dev->dev, "Received packet for unknown VCI.VPI %d.%d on port %d\n",
David Woodhouse90937232009-01-28 16:46:56 +1100712 le16_to_cpu(header->vci), le16_to_cpu(header->vpi),
David Woodhouse9c540042008-12-23 04:09:02 +0000713 port);
714 continue;
715 }
716 atm_charge(vcc, skb->truesize);
717 vcc->push(vcc, skb);
718 atomic_inc(&vcc->stats->rx);
719 break;
720
David Woodhouse87ebb182009-01-27 20:02:30 +1100721 case PKT_STATUS:
David Woodhouse95852f42009-01-30 14:23:52 +1100722 if (process_status(card, port, skb) &&
723 net_ratelimit()) {
724 dev_warn(&card->dev->dev, "Bad status packet of %d bytes on port %d:\n", skb->len, port);
725 print_buffer(skb);
726 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100727 dev_kfree_skb_any(skb);
David Woodhouse87ebb182009-01-27 20:02:30 +1100728 break;
729
David Woodhouse9c540042008-12-23 04:09:02 +0000730 case PKT_COMMAND:
731 default: /* FIXME: Not really, surely? */
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100732 if (process_command(card, port, skb))
733 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000734 spin_lock(&card->cli_queue_lock);
735 if (skb_queue_len(&card->cli_queue[port]) > 10) {
736 if (net_ratelimit())
737 dev_warn(&card->dev->dev, "Dropping console response on port %d\n",
738 port);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100739 dev_kfree_skb_any(skb);
David Woodhouse9c540042008-12-23 04:09:02 +0000740 } else
741 skb_queue_tail(&card->cli_queue[port], skb);
742 spin_unlock(&card->cli_queue_lock);
743 break;
744 }
745 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100746 /* Allocate RX skbs for any ports which need them */
747 if (card->using_dma && card->atmdev[port] &&
748 !card->rx_skb[port]) {
749 struct sk_buff *skb = alloc_skb(RX_DMA_SIZE, GFP_ATOMIC);
750 if (skb) {
751 SKB_CB(skb)->dma_addr =
752 pci_map_single(card->dev, skb->data,
753 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
754 iowrite32(SKB_CB(skb)->dma_addr,
755 card->config_regs + RX_DMA_ADDR(port));
756 card->rx_skb[port] = skb;
757 } else {
758 if (net_ratelimit())
759 dev_warn(&card->dev->dev, "Failed to allocate RX skb");
760
761 /* We'll have to try again later */
762 tasklet_schedule(&card->tlet);
763 }
764 }
David Woodhouse9c540042008-12-23 04:09:02 +0000765 }
766 if (rx_done)
767 iowrite32(rx_done, card->config_regs + FLAGS_ADDR);
768
769 return;
770}
771
772static struct atm_vcc *find_vcc(struct atm_dev *dev, short vpi, int vci)
773{
774 struct hlist_head *head;
775 struct atm_vcc *vcc = NULL;
776 struct hlist_node *node;
777 struct sock *s;
778
779 read_lock(&vcc_sklist_lock);
780 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
781 sk_for_each(s, node, head) {
782 vcc = atm_sk(s);
783 if (vcc->dev == dev && vcc->vci == vci &&
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700784 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
785 test_bit(ATM_VF_READY, &vcc->flags))
David Woodhouse9c540042008-12-23 04:09:02 +0000786 goto out;
787 }
788 vcc = NULL;
789 out:
790 read_unlock(&vcc_sklist_lock);
791 return vcc;
792}
793
794static int list_vccs(int vci)
795{
796 struct hlist_head *head;
797 struct atm_vcc *vcc;
798 struct hlist_node *node;
799 struct sock *s;
800 int num_found = 0;
801 int i;
802
803 read_lock(&vcc_sklist_lock);
804 if (vci != 0){
805 head = &vcc_hash[vci & (VCC_HTABLE_SIZE -1)];
806 sk_for_each(s, node, head) {
807 num_found ++;
808 vcc = atm_sk(s);
809 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
810 vcc->dev->number,
811 vcc->vpi,
812 vcc->vci);
813 }
814 } else {
David Woodhouse1e615df2009-01-27 21:47:47 +1100815 for(i = 0; i < VCC_HTABLE_SIZE; i++){
David Woodhouse9c540042008-12-23 04:09:02 +0000816 head = &vcc_hash[i];
817 sk_for_each(s, node, head) {
818 num_found ++;
819 vcc = atm_sk(s);
820 printk(KERN_DEBUG "Device: %d Vpi: %d Vci: %d\n",
821 vcc->dev->number,
822 vcc->vpi,
823 vcc->vci);
824 }
825 }
826 }
827 read_unlock(&vcc_sklist_lock);
828 return num_found;
829}
830
David Woodhouse1e615df2009-01-27 21:47:47 +1100831static void release_vccs(struct atm_dev *dev)
832{
833 int i;
834
835 write_lock_irq(&vcc_sklist_lock);
836 for (i = 0; i < VCC_HTABLE_SIZE; i++) {
837 struct hlist_head *head = &vcc_hash[i];
838 struct hlist_node *node, *tmp;
839 struct sock *s;
840 struct atm_vcc *vcc;
841
842 sk_for_each_safe(s, node, tmp, head) {
843 vcc = atm_sk(s);
844 if (vcc->dev == dev) {
845 vcc_release_async(vcc, -EPIPE);
846 sk_del_node_init(s);
847 }
848 }
849 }
850 write_unlock_irq(&vcc_sklist_lock);
851}
852
David Woodhouse9c540042008-12-23 04:09:02 +0000853
854static int popen(struct atm_vcc *vcc)
855{
856 struct solos_card *card = vcc->dev->dev_data;
857 struct sk_buff *skb;
858 struct pkt_hdr *header;
859
David Woodhouseb28a4b92009-01-27 21:50:36 +1100860 if (vcc->qos.aal != ATM_AAL5) {
861 dev_warn(&card->dev->dev, "Unsupported ATM type %d\n",
862 vcc->qos.aal);
863 return -EINVAL;
864 }
865
David Woodhouse9c540042008-12-23 04:09:02 +0000866 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
867 if (!skb && net_ratelimit()) {
868 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
869 return -ENOMEM;
870 }
871 header = (void *)skb_put(skb, sizeof(*header));
872
David Woodhouseb76811a2009-01-27 10:18:51 +1100873 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000874 header->vpi = cpu_to_le16(vcc->vpi);
875 header->vci = cpu_to_le16(vcc->vci);
876 header->type = cpu_to_le16(PKT_POPEN);
877
878 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
879
David Woodhousebdc54622009-01-29 14:37:20 +1100880 set_bit(ATM_VF_ADDR, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000881 set_bit(ATM_VF_READY, &vcc->flags);
882 list_vccs(0);
883
David Woodhouse9c540042008-12-23 04:09:02 +0000884
885 return 0;
886}
887
888static void pclose(struct atm_vcc *vcc)
889{
890 struct solos_card *card = vcc->dev->dev_data;
891 struct sk_buff *skb;
892 struct pkt_hdr *header;
893
894 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
895 if (!skb) {
896 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
897 return;
898 }
899 header = (void *)skb_put(skb, sizeof(*header));
900
David Woodhouseb76811a2009-01-27 10:18:51 +1100901 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000902 header->vpi = cpu_to_le16(vcc->vpi);
903 header->vci = cpu_to_le16(vcc->vci);
904 header->type = cpu_to_le16(PKT_PCLOSE);
905
906 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, NULL);
907
David Woodhouse9c540042008-12-23 04:09:02 +0000908 clear_bit(ATM_VF_ADDR, &vcc->flags);
909 clear_bit(ATM_VF_READY, &vcc->flags);
910
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700911 /* Hold up vcc_destroy_socket() (our caller) until solos_bh() in the
912 tasklet has finished processing any incoming packets (and, more to
913 the point, using the vcc pointer). */
914 tasklet_unlock_wait(&card->tlet);
David Woodhouse9c540042008-12-23 04:09:02 +0000915 return;
916}
917
918static int print_buffer(struct sk_buff *buf)
919{
920 int len,i;
921 char msg[500];
922 char item[10];
923
924 len = buf->len;
925 for (i = 0; i < len; i++){
926 if(i % 8 == 0)
927 sprintf(msg, "%02X: ", i);
928
929 sprintf(item,"%02X ",*(buf->data + i));
930 strcat(msg, item);
931 if(i % 8 == 7) {
932 sprintf(item, "\n");
933 strcat(msg, item);
934 printk(KERN_DEBUG "%s", msg);
935 }
936 }
937 if (i % 8 != 0) {
938 sprintf(item, "\n");
939 strcat(msg, item);
940 printk(KERN_DEBUG "%s", msg);
941 }
942 printk(KERN_DEBUG "\n");
943
944 return 0;
945}
946
947static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
948 struct atm_vcc *vcc)
949{
950 int old_len;
David Woodhousef69e4172009-01-29 11:10:58 +1100951 unsigned long flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000952
David Woodhouse90937232009-01-28 16:46:56 +1100953 SKB_CB(skb)->vcc = vcc;
David Woodhouse9c540042008-12-23 04:09:02 +0000954
David Woodhousef69e4172009-01-29 11:10:58 +1100955 spin_lock_irqsave(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000956 old_len = skb_queue_len(&card->tx_queue[port]);
957 skb_queue_tail(&card->tx_queue[port], skb);
David Woodhouse35c22212009-01-29 14:08:27 +1100958 if (!old_len)
David Woodhousef69e4172009-01-29 11:10:58 +1100959 card->tx_mask |= (1 << port);
David Woodhousef69e4172009-01-29 11:10:58 +1100960 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000961
David Woodhousef69e4172009-01-29 11:10:58 +1100962 /* Theoretically we could just schedule the tasklet here, but
963 that introduces latency we don't want -- it's noticeable */
David Woodhouse9c540042008-12-23 04:09:02 +0000964 if (!old_len)
965 fpga_tx(card);
966}
967
David Woodhouse35c22212009-01-29 14:08:27 +1100968static uint32_t fpga_tx(struct solos_card *card)
David Woodhouse9c540042008-12-23 04:09:02 +0000969{
David Woodhouse35c22212009-01-29 14:08:27 +1100970 uint32_t tx_pending, card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000971 uint32_t tx_started = 0;
972 struct sk_buff *skb;
973 struct atm_vcc *vcc;
974 unsigned char port;
975 unsigned long flags;
976
977 spin_lock_irqsave(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +1100978
979 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
980 /*
981 * The queue lock is required for _writing_ to tx_mask, but we're
982 * OK to read it here without locking. The only potential update
983 * that we could race with is in fpga_queue() where it sets a bit
984 * for a new port... but it's going to call this function again if
985 * it's doing that, anyway.
986 */
987 tx_pending = card->tx_mask & ~card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000988
David Woodhouse35c22212009-01-29 14:08:27 +1100989 for (port = 0; tx_pending; tx_pending >>= 1, port++) {
990 if (tx_pending & 1) {
David Woodhouseeaf83e32009-01-29 10:51:11 +1100991 struct sk_buff *oldskb = card->tx_skb[port];
David Woodhouseeaf83e32009-01-29 10:51:11 +1100992 if (oldskb)
993 pci_unmap_single(card->dev, SKB_CB(oldskb)->dma_addr,
994 oldskb->len, PCI_DMA_TODEVICE);
David Woodhouse9c540042008-12-23 04:09:02 +0000995
996 spin_lock(&card->tx_queue_lock);
997 skb = skb_dequeue(&card->tx_queue[port]);
David Woodhousef69e4172009-01-29 11:10:58 +1100998 if (!skb)
999 card->tx_mask &= ~(1 << port);
David Woodhouse9c540042008-12-23 04:09:02 +00001000 spin_unlock(&card->tx_queue_lock);
1001
David Woodhouseeaf83e32009-01-29 10:51:11 +11001002 if (skb && !card->using_dma) {
1003 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
David Woodhousebdc54622009-01-29 14:37:20 +11001004 tx_started |= 1 << port;
David Woodhouseeaf83e32009-01-29 10:51:11 +11001005 oldskb = skb; /* We're done with this skb already */
1006 } else if (skb && card->using_dma) {
1007 SKB_CB(skb)->dma_addr = pci_map_single(card->dev, skb->data,
1008 skb->len, PCI_DMA_TODEVICE);
1009 iowrite32(SKB_CB(skb)->dma_addr,
1010 card->config_regs + TX_DMA_ADDR(port));
1011 }
1012
1013 if (!oldskb)
David Woodhouse9c540042008-12-23 04:09:02 +00001014 continue;
1015
David Woodhouseeaf83e32009-01-29 10:51:11 +11001016 /* Clean up and free oldskb now it's gone */
David Woodhouse9c540042008-12-23 04:09:02 +00001017 if (atmdebug) {
1018 dev_info(&card->dev->dev, "Transmitted: port %d\n",
1019 port);
David Woodhouseeaf83e32009-01-29 10:51:11 +11001020 print_buffer(oldskb);
David Woodhouse9c540042008-12-23 04:09:02 +00001021 }
David Woodhouse9c540042008-12-23 04:09:02 +00001022
David Woodhouseeaf83e32009-01-29 10:51:11 +11001023 vcc = SKB_CB(oldskb)->vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001024
1025 if (vcc) {
1026 atomic_inc(&vcc->stats->tx);
David Woodhouseeaf83e32009-01-29 10:51:11 +11001027 solos_pop(vcc, oldskb);
David Woodhouse9c540042008-12-23 04:09:02 +00001028 } else
David Woodhouseeaf83e32009-01-29 10:51:11 +11001029 dev_kfree_skb_irq(oldskb);
David Woodhouse9c540042008-12-23 04:09:02 +00001030
David Woodhouse9c540042008-12-23 04:09:02 +00001031 }
1032 }
David Woodhousebdc54622009-01-29 14:37:20 +11001033 /* For non-DMA TX, write the 'TX start' bit for all four ports simultaneously */
David Woodhouse9c540042008-12-23 04:09:02 +00001034 if (tx_started)
1035 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
1036
1037 spin_unlock_irqrestore(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001038 return card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001039}
1040
1041static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
1042{
1043 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse9c540042008-12-23 04:09:02 +00001044 struct pkt_hdr *header;
David Woodhouseb76811a2009-01-27 10:18:51 +11001045 int pktlen;
David Woodhouse9c540042008-12-23 04:09:02 +00001046
David Woodhouseb76811a2009-01-27 10:18:51 +11001047 pktlen = skb->len;
1048 if (pktlen > (BUF_SIZE - sizeof(*header))) {
David Woodhouse9c540042008-12-23 04:09:02 +00001049 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
1050 solos_pop(vcc, skb);
1051 return 0;
1052 }
1053
1054 if (!skb_clone_writable(skb, sizeof(*header))) {
1055 int expand_by = 0;
1056 int ret;
1057
1058 if (skb_headroom(skb) < sizeof(*header))
1059 expand_by = sizeof(*header) - skb_headroom(skb);
1060
1061 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
1062 if (ret) {
Simon Farnsworth4306cad2009-01-19 21:19:29 +00001063 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
David Woodhouse9c540042008-12-23 04:09:02 +00001064 solos_pop(vcc, skb);
1065 return ret;
1066 }
1067 }
1068
1069 header = (void *)skb_push(skb, sizeof(*header));
1070
David Woodhouseb76811a2009-01-27 10:18:51 +11001071 /* This does _not_ include the size of the header */
1072 header->size = cpu_to_le16(pktlen);
David Woodhouse9c540042008-12-23 04:09:02 +00001073 header->vpi = cpu_to_le16(vcc->vpi);
1074 header->vci = cpu_to_le16(vcc->vci);
1075 header->type = cpu_to_le16(PKT_DATA);
1076
1077 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1078
1079 return 0;
1080}
1081
1082static struct atmdev_ops fpga_ops = {
1083 .open = popen,
1084 .close = pclose,
1085 .ioctl = NULL,
1086 .getsockopt = NULL,
1087 .setsockopt = NULL,
1088 .send = psend,
1089 .send_oam = NULL,
1090 .phy_put = NULL,
1091 .phy_get = NULL,
1092 .change_qos = NULL,
1093 .proc_read = NULL,
1094 .owner = THIS_MODULE
1095};
1096
1097static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
1098{
David Woodhousecd5549e2009-01-30 14:26:37 +11001099 int err;
David Woodhouse9c540042008-12-23 04:09:02 +00001100 uint16_t fpga_ver;
1101 uint8_t major_ver, minor_ver;
1102 uint32_t data32;
1103 struct solos_card *card;
1104
David Woodhouse9c540042008-12-23 04:09:02 +00001105 card = kzalloc(sizeof(*card), GFP_KERNEL);
1106 if (!card)
1107 return -ENOMEM;
1108
1109 card->dev = dev;
David Woodhousefa755b92009-01-27 14:16:12 +11001110 init_waitqueue_head(&card->fw_wq);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001111 init_waitqueue_head(&card->param_wq);
David Woodhouse9c540042008-12-23 04:09:02 +00001112
1113 err = pci_enable_device(dev);
1114 if (err) {
1115 dev_warn(&dev->dev, "Failed to enable PCI device\n");
1116 goto out;
1117 }
1118
Yang Hongyange9304382009-04-13 14:40:14 -07001119 err = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
David Woodhouse90937232009-01-28 16:46:56 +11001120 if (err) {
1121 dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
1122 goto out;
1123 }
1124
David Woodhouse9c540042008-12-23 04:09:02 +00001125 err = pci_request_regions(dev, "solos");
1126 if (err) {
1127 dev_warn(&dev->dev, "Failed to request regions\n");
1128 goto out;
1129 }
1130
1131 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1132 if (!card->config_regs) {
1133 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
1134 goto out_release_regions;
1135 }
1136 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1137 if (!card->buffers) {
1138 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
1139 goto out_unmap_config;
1140 }
1141
David Woodhousecc3657e2009-01-30 14:23:22 +11001142 if (reset) {
1143 iowrite32(1, card->config_regs + FPGA_MODE);
1144 data32 = ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001145
David Woodhousecc3657e2009-01-30 14:23:22 +11001146 iowrite32(0, card->config_regs + FPGA_MODE);
1147 data32 = ioread32(card->config_regs + FPGA_MODE);
1148 }
David Woodhouse9c540042008-12-23 04:09:02 +00001149
1150 data32 = ioread32(card->config_regs + FPGA_VER);
1151 fpga_ver = (data32 & 0x0000FFFF);
1152 major_ver = ((data32 & 0xFF000000) >> 24);
1153 minor_ver = ((data32 & 0x00FF0000) >> 16);
Nathan Williams4dbedf42009-03-25 20:27:37 +11001154 card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
1155 if (card->fpga_version > LEGACY_BUFFERS)
1156 card->buffer_size = BUF_SIZE;
1157 else
1158 card->buffer_size = OLD_BUF_SIZE;
David Woodhouse9c540042008-12-23 04:09:02 +00001159 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1160 major_ver, minor_ver, fpga_ver);
1161
Nathan Williams4dbedf42009-03-25 20:27:37 +11001162 if (card->fpga_version >= DMA_SUPPORTED){
David Woodhouse90937232009-01-28 16:46:56 +11001163 card->using_dma = 1;
Nathan Williams4dbedf42009-03-25 20:27:37 +11001164 } else {
1165 card->using_dma = 0;
David Woodhouseeab50f72009-01-30 14:27:26 +11001166 /* Set RX empty flag for all ports */
1167 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1168 }
David Woodhouse9c540042008-12-23 04:09:02 +00001169
Nathan Williams0fc36aa2009-02-07 10:19:13 +11001170 data32 = ioread32(card->config_regs + PORTS);
1171 card->nr_ports = (data32 & 0x000000FF);
David Woodhouse9c540042008-12-23 04:09:02 +00001172
1173 pci_set_drvdata(dev, card);
David Woodhousefa755b92009-01-27 14:16:12 +11001174
David Woodhouse9c540042008-12-23 04:09:02 +00001175 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1176 spin_lock_init(&card->tx_lock);
1177 spin_lock_init(&card->tx_queue_lock);
1178 spin_lock_init(&card->cli_queue_lock);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001179 spin_lock_init(&card->param_queue_lock);
1180 INIT_LIST_HEAD(&card->param_queue);
David Woodhouse9c540042008-12-23 04:09:02 +00001181
David Woodhousefcd82662009-01-29 11:29:12 +11001182 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
David Woodhouse9c540042008-12-23 04:09:02 +00001183 "solos-pci", card);
David Woodhousefa755b92009-01-27 14:16:12 +11001184 if (err) {
David Woodhouse9c540042008-12-23 04:09:02 +00001185 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
David Woodhousefa755b92009-01-27 14:16:12 +11001186 goto out_unmap_both;
1187 }
David Woodhouse9c540042008-12-23 04:09:02 +00001188
David Woodhouse9c540042008-12-23 04:09:02 +00001189 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1190
David Woodhousefa755b92009-01-27 14:16:12 +11001191 if (fpga_upgrade)
1192 flash_upgrade(card, 0);
1193
1194 if (firmware_upgrade)
1195 flash_upgrade(card, 1);
1196
Nathan Williams4dbedf42009-03-25 20:27:37 +11001197 if (db_fpga_upgrade)
1198 flash_upgrade(card, 2);
1199
1200 if (db_firmware_upgrade)
1201 flash_upgrade(card, 3);
1202
David Woodhousefa755b92009-01-27 14:16:12 +11001203 err = atm_init(card);
1204 if (err)
1205 goto out_free_irq;
1206
David Woodhouse9c540042008-12-23 04:09:02 +00001207 return 0;
1208
David Woodhousefa755b92009-01-27 14:16:12 +11001209 out_free_irq:
1210 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1211 free_irq(dev->irq, card);
1212 tasklet_kill(&card->tlet);
1213
David Woodhouse9c540042008-12-23 04:09:02 +00001214 out_unmap_both:
David Woodhousefa755b92009-01-27 14:16:12 +11001215 pci_set_drvdata(dev, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001216 pci_iounmap(dev, card->config_regs);
1217 out_unmap_config:
1218 pci_iounmap(dev, card->buffers);
1219 out_release_regions:
1220 pci_release_regions(dev);
1221 out:
Julia Lawallbc111d52009-02-08 17:00:02 -08001222 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001223 return err;
1224}
1225
1226static int atm_init(struct solos_card *card)
1227{
1228 int i;
1229
David Woodhouse9c540042008-12-23 04:09:02 +00001230 for (i = 0; i < card->nr_ports; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +11001231 struct sk_buff *skb;
1232 struct pkt_hdr *header;
1233
David Woodhouse9c540042008-12-23 04:09:02 +00001234 skb_queue_head_init(&card->tx_queue[i]);
1235 skb_queue_head_init(&card->cli_queue[i]);
1236
1237 card->atmdev[i] = atm_dev_register("solos-pci", &fpga_ops, -1, NULL);
1238 if (!card->atmdev[i]) {
1239 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1240 atm_remove(card);
1241 return -ENODEV;
1242 }
1243 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1244 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
David Woodhoused057f0a2009-01-27 16:52:07 +11001245 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1246 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
David Woodhouse9c540042008-12-23 04:09:02 +00001247
1248 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1249
1250 card->atmdev[i]->ci_range.vpi_bits = 8;
1251 card->atmdev[i]->ci_range.vci_bits = 16;
1252 card->atmdev[i]->dev_data = card;
1253 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
Karl Hiramoto49d49102010-07-08 20:55:34 +00001254 atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_UNKNOWN);
David Woodhouse87ebb182009-01-27 20:02:30 +11001255
1256 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
1257 if (!skb) {
1258 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1259 continue;
1260 }
1261
1262 header = (void *)skb_put(skb, sizeof(*header));
1263
1264 header->size = cpu_to_le16(0);
1265 header->vpi = cpu_to_le16(0);
1266 header->vci = cpu_to_le16(0);
1267 header->type = cpu_to_le16(PKT_STATUS);
1268
1269 fpga_queue(card, i, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001270 }
1271 return 0;
1272}
1273
1274static void atm_remove(struct solos_card *card)
1275{
1276 int i;
1277
1278 for (i = 0; i < card->nr_ports; i++) {
1279 if (card->atmdev[i]) {
David Woodhouse97d759d2009-03-17 14:59:34 +00001280 struct sk_buff *skb;
1281
David Woodhouse9c540042008-12-23 04:09:02 +00001282 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
David Woodhousec0fe3022009-01-28 14:34:34 +11001283
1284 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
David Woodhouse9c540042008-12-23 04:09:02 +00001285 atm_dev_deregister(card->atmdev[i]);
David Woodhouse97d759d2009-03-17 14:59:34 +00001286
1287 skb = card->rx_skb[i];
1288 if (skb) {
1289 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1290 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
1291 dev_kfree_skb(skb);
1292 }
1293 skb = card->tx_skb[i];
1294 if (skb) {
1295 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1296 skb->len, PCI_DMA_TODEVICE);
1297 dev_kfree_skb(skb);
1298 }
1299 while ((skb = skb_dequeue(&card->tx_queue[i])))
1300 dev_kfree_skb(skb);
1301
David Woodhouse9c540042008-12-23 04:09:02 +00001302 }
1303 }
1304}
1305
1306static void fpga_remove(struct pci_dev *dev)
1307{
1308 struct solos_card *card = pci_get_drvdata(dev);
David Woodhouse97d759d2009-03-17 14:59:34 +00001309
1310 /* Disable IRQs */
1311 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
David Woodhouse9c540042008-12-23 04:09:02 +00001312
David Woodhouse97d759d2009-03-17 14:59:34 +00001313 /* Reset FPGA */
1314 iowrite32(1, card->config_regs + FPGA_MODE);
1315 (void)ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001316
1317 atm_remove(card);
1318
David Woodhouse9c540042008-12-23 04:09:02 +00001319 free_irq(dev->irq, card);
1320 tasklet_kill(&card->tlet);
1321
David Woodhouse97d759d2009-03-17 14:59:34 +00001322 /* Release device from reset */
1323 iowrite32(0, card->config_regs + FPGA_MODE);
1324 (void)ioread32(card->config_regs + FPGA_MODE);
1325
David Woodhouse9c540042008-12-23 04:09:02 +00001326 pci_iounmap(dev, card->buffers);
1327 pci_iounmap(dev, card->config_regs);
1328
David Woodhouse9c540042008-12-23 04:09:02 +00001329 pci_release_regions(dev);
1330 pci_disable_device(dev);
1331
1332 pci_set_drvdata(dev, NULL);
1333 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001334}
1335
1336static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1337 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1338 { 0, }
1339};
1340
1341MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1342
1343static struct pci_driver fpga_driver = {
1344 .name = "solos",
1345 .id_table = fpga_pci_tbl,
1346 .probe = fpga_probe,
1347 .remove = fpga_remove,
1348};
1349
1350
1351static int __init solos_pci_init(void)
1352{
1353 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1354 return pci_register_driver(&fpga_driver);
1355}
1356
1357static void __exit solos_pci_exit(void)
1358{
1359 pci_unregister_driver(&fpga_driver);
1360 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1361}
1362
1363module_init(solos_pci_init);
1364module_exit(solos_pci_exit);