blob: 026bdc1dfcc616ad253f243ff5b94f66944e8c6f [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);
Dan Williamsd9ca6762010-12-08 19:40:47 +0000168static int atm_init(struct solos_card *, struct device *);
David Woodhouse9c540042008-12-23 04:09:02 +0000169static 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
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100182static 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
David Woodhouse3456b222009-01-28 10:39:23 +1100194 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100195 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
David Woodhouse3456b222009-01-28 10:39:23 +1100250 skb = alloc_skb(sizeof(*header) + buflen, GFP_KERNEL);
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100251 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 /* Sometimes it has a newline, sometimes it doesn't. */
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 /* We know we have enough space allocated for this; we allocated
300 it ourselves */
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
David Woodhouse87ebb182009-01-27 20:02:30 +1100312static char *next_string(struct sk_buff *skb)
313{
314 int i = 0;
315 char *this = skb->data;
David Woodhousec6428e52009-01-29 12:17:09 +1100316
317 for (i = 0; i < skb->len; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +1100318 if (this[i] == '\n') {
319 this[i] = 0;
David Woodhousec6428e52009-01-29 12:17:09 +1100320 skb_pull(skb, i + 1);
David Woodhouse87ebb182009-01-27 20:02:30 +1100321 return this;
322 }
David Woodhousec6428e52009-01-29 12:17:09 +1100323 if (!isprint(this[i]))
324 return NULL;
David Woodhouse87ebb182009-01-27 20:02:30 +1100325 }
326 return NULL;
327}
328
329/*
330 * Status packet has fields separated by \n, starting with a version number
331 * for the information therein. Fields are....
332 *
333 * packet version
David Woodhouse87ebb182009-01-27 20:02:30 +1100334 * RxBitRate (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100335 * TxBitRate (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100336 * State (version >= 1)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100337 * LocalSNRMargin (version >= 1)
338 * LocalLineAttn (version >= 1)
David Woodhouse87ebb182009-01-27 20:02:30 +1100339 */
340static int process_status(struct solos_card *card, int port, struct sk_buff *skb)
341{
David Woodhousef87b2ed2009-01-30 14:31:36 +1100342 char *str, *end, *state_str, *snr, *attn;
343 int ver, rate_up, rate_down;
David Woodhouse87ebb182009-01-27 20:02:30 +1100344
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);
David Woodhousec6428e52009-01-29 12:17:09 +1100360 if (!str)
361 return -EIO;
David Woodhouse95852f42009-01-30 14:23:52 +1100362 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
David Woodhousef87b2ed2009-01-30 14:31:36 +1100368 rate_down = simple_strtol(str, &end, 10);
David Woodhouse87ebb182009-01-27 20:02:30 +1100369 if (*end)
370 return -EIO;
371
372 str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100373 if (!str)
374 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100375 rate_up = simple_strtol(str, &end, 10);
David Woodhouse87ebb182009-01-27 20:02:30 +1100376 if (*end)
377 return -EIO;
378
David Woodhouseaf780652009-01-28 10:22:57 +1100379 state_str = next_string(skb);
David Woodhousec6428e52009-01-29 12:17:09 +1100380 if (!state_str)
381 return -EIO;
David Woodhousef87b2ed2009-01-30 14:31:36 +1100382
383 /* Anything but 'Showtime' is down */
384 if (strcmp(state_str, "Showtime")) {
Karl Hiramoto49d49102010-07-08 20:55:34 +0000385 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_LOST);
David Woodhousef87b2ed2009-01-30 14:31:36 +1100386 dev_info(&card->dev->dev, "Port %d: %s\n", port, state_str);
387 return 0;
David Woodhouse1e615df2009-01-27 21:47:47 +1100388 }
David Woodhouse87ebb182009-01-27 20:02:30 +1100389
David Woodhousef87b2ed2009-01-30 14:31:36 +1100390 snr = next_string(skb);
Julia Lawall6cf57672009-07-27 11:38:52 -0700391 if (!snr)
David Woodhousef87b2ed2009-01-30 14:31:36 +1100392 return -EIO;
393 attn = next_string(skb);
394 if (!attn)
395 return -EIO;
David Woodhouseaf780652009-01-28 10:22:57 +1100396
David Woodhousef87b2ed2009-01-30 14:31:36 +1100397 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
David Woodhousec6428e52009-01-29 12:17:09 +1100401 card->atmdev[port]->link_rate = rate_down / 424;
Karl Hiramoto49d49102010-07-08 20:55:34 +0000402 atm_dev_signal_change(card->atmdev[port], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +1100403
David Woodhouse87ebb182009-01-27 20:02:30 +1100404 return 0;
405}
406
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100407static 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
David Woodhouse9c540042008-12-23 04:09:02 +0000439static 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;
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000445 unsigned int len;
David Woodhouse9c540042008-12-23 04:09:02 +0000446
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
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000453 len = skb->len;
454 memcpy(buf, skb->data, len);
455 dev_dbg(&card->dev->dev, "len: %d\n", len);
David Woodhouse9c540042008-12-23 04:09:02 +0000456
457 kfree_skb(skb);
Jiri Slabyf1ee89d2010-10-10 21:50:44 +0000458 return len;
David Woodhouse9c540042008-12-23 04:09:02 +0000459}
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 int blocksize = 0;
531 int numblocks = 0;
David Woodhousefa755b92009-01-27 14:16:12 +1100532 int offset;
533
Andrew Morton7adcdb42009-11-17 12:46:44 +0000534 switch (chip) {
535 case 0:
David Woodhousefa755b92009-01-27 14:16:12 +1100536 fw_name = "solos-FPGA.bin";
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000537 blocksize = FPGA_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000538 break;
539 case 1:
David Woodhousefa755b92009-01-27 14:16:12 +1100540 fw_name = "solos-Firmware.bin";
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000541 blocksize = SOLOS_BLOCK;
Andrew Morton7adcdb42009-11-17 12:46:44 +0000542 break;
543 case 2:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100544 if (card->fpga_version > LEGACY_BUFFERS){
545 fw_name = "solos-db-FPGA.bin";
546 blocksize = FPGA_BLOCK;
547 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000548 dev_info(&card->dev->dev, "FPGA version doesn't support"
549 " daughter board upgrades\n");
Nathan Williams4dbedf42009-03-25 20:27:37 +1100550 return -EPERM;
551 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000552 break;
553 case 3:
Nathan Williams4dbedf42009-03-25 20:27:37 +1100554 if (card->fpga_version > LEGACY_BUFFERS){
555 fw_name = "solos-Firmware.bin";
556 blocksize = SOLOS_BLOCK;
557 } else {
Andrew Morton7adcdb42009-11-17 12:46:44 +0000558 dev_info(&card->dev->dev, "FPGA version doesn't support"
559 " daughter board upgrades\n");
560 return -EPERM;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100561 }
Andrew Morton7adcdb42009-11-17 12:46:44 +0000562 break;
563 default:
564 return -ENODEV;
Nathan Williams4dbedf42009-03-25 20:27:37 +1100565 }
David Woodhousefa755b92009-01-27 14:16:12 +1100566
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);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000574 dev_info(&card->dev->dev, "Number of blocks: %d\n", numblocks);
575
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000576 dev_info(&card->dev->dev, "Changing FPGA to Update mode\n");
577 iowrite32(1, card->config_regs + FPGA_MODE);
David S. Miller06091ed2011-04-17 00:11:25 -0700578 (void) ioread32(card->config_regs + FPGA_MODE);
David Woodhousefa755b92009-01-27 14:16:12 +1100579
580 /* Set mode to Chip Erase */
Nathan Williams4dbedf42009-03-25 20:27:37 +1100581 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");
David Woodhousefa755b92009-01-27 14:16:12 +1100585 iowrite32((chip * 2), card->config_regs + FLASH_MODE);
586
587
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000588 iowrite32(1, card->config_regs + WRITE_FLASH);
David Woodhousefa755b92009-01-27 14:16:12 +1100589 wait_event(card->fw_wq, !ioread32(card->config_regs + FLASH_BUSY));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000590
David Woodhousefa755b92009-01-27 14:16:12 +1100591 for (offset = 0; offset < fw->size; offset += blocksize) {
592 int i;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000593
David Woodhousefa755b92009-01-27 14:16:12 +1100594 /* Clear write flag */
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000595 iowrite32(0, card->config_regs + WRITE_FLASH);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000596
David Woodhousefa755b92009-01-27 14:16:12 +1100597 /* Set mode to Block Write */
598 /* dev_info(&card->dev->dev, "Set FPGA Flash mode to Block Write\n"); */
599 iowrite32(((chip * 2) + 1), card->config_regs + FLASH_MODE);
600
601 /* Copy block to buffer, swapping each 16 bits */
602 for(i = 0; i < blocksize; i += 4) {
603 uint32_t word = swahb32p((uint32_t *)(fw->data + offset + i));
Nathan Williams4dbedf42009-03-25 20:27:37 +1100604 if(card->fpga_version > LEGACY_BUFFERS)
605 iowrite32(word, FLASH_BUF + i);
606 else
607 iowrite32(word, RX_BUF(card, 3) + i);
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000608 }
609
David Woodhousefa755b92009-01-27 14:16:12 +1100610 /* Specify block number and then trigger flash write */
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));
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000614 }
615
David Woodhousefa755b92009-01-27 14:16:12 +1100616 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;
Simon Farnsworth7c4015b2009-01-21 20:45:49 +0000622}
623
David Woodhouse9c540042008-12-23 04:09:02 +0000624static irqreturn_t solos_irq(int irq, void *dev_id)
625{
626 struct solos_card *card = dev_id;
627 int handled = 1;
628
David Woodhouse9c540042008-12-23 04:09:02 +0000629 iowrite32(0, card->config_regs + IRQ_CLEAR);
David Woodhouse9c540042008-12-23 04:09:02 +0000630
David Woodhouse35c22212009-01-29 14:08:27 +1100631 /* If we're up and running, just kick the tasklet to process TX/RX */
David Woodhousefa755b92009-01-27 14:16:12 +1100632 if (card->atmdev[0])
David Woodhouse9c540042008-12-23 04:09:02 +0000633 tasklet_schedule(&card->tlet);
David Woodhousefa755b92009-01-27 14:16:12 +1100634 else
635 wake_up(&card->fw_wq);
David Woodhouse9c540042008-12-23 04:09:02 +0000636
David Woodhouse9c540042008-12-23 04:09:02 +0000637 return IRQ_RETVAL(handled);
638}
639
640void solos_bh(unsigned long card_arg)
641{
642 struct solos_card *card = (void *)card_arg;
David Woodhouse9c540042008-12-23 04:09:02 +0000643 uint32_t card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000644 uint32_t rx_done = 0;
David Woodhouse35c22212009-01-29 14:08:27 +1100645 int port;
David Woodhouse9c540042008-12-23 04:09:02 +0000646
David Woodhouse35c22212009-01-29 14:08:27 +1100647 /*
648 * Since fpga_tx() is going to need to read the flags under its lock,
649 * it can return them to us so that we don't have to hit PCI MMIO
650 * again for the same information
651 */
652 card_flags = fpga_tx(card);
David Woodhouse9c540042008-12-23 04:09:02 +0000653
654 for (port = 0; port < card->nr_ports; port++) {
655 if (card_flags & (0x10 << port)) {
David Woodhouse90937232009-01-28 16:46:56 +1100656 struct pkt_hdr _hdr, *header;
David Woodhouse9c540042008-12-23 04:09:02 +0000657 struct sk_buff *skb;
658 struct atm_vcc *vcc;
659 int size;
660
David Woodhouse90937232009-01-28 16:46:56 +1100661 if (card->using_dma) {
662 skb = card->rx_skb[port];
David Woodhouseeaf83e32009-01-29 10:51:11 +1100663 card->rx_skb[port] = NULL;
David Woodhouse9c540042008-12-23 04:09:02 +0000664
David Woodhouseeaf83e32009-01-29 10:51:11 +1100665 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
666 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
David Woodhouse9c540042008-12-23 04:09:02 +0000667
David Woodhouse90937232009-01-28 16:46:56 +1100668 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;
David Woodhouse9c540042008-12-23 04:09:02 +0000674
David Woodhouse90937232009-01-28 16:46:56 +1100675 rx_done |= 0x10 << port;
David Woodhouse9c540042008-12-23 04:09:02 +0000676
David Woodhouse90937232009-01-28 16:46:56 +1100677 memcpy_fromio(header, RX_BUF(card, port), sizeof(*header));
678
679 size = le16_to_cpu(header->size);
Nathan Williams78f857f2009-03-25 20:33:42 +1100680 if (size > (card->buffer_size - sizeof(*header))){
681 dev_warn(&card->dev->dev, "Invalid buffer size\n");
682 continue;
683 }
David Woodhouse90937232009-01-28 16:46:56 +1100684
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);
David Woodhouse9c540042008-12-23 04:09:02 +0000695 }
David Woodhouse9c540042008-12-23 04:09:02 +0000696 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +0000697 dev_info(&card->dev->dev, "Received: port %d\n", port);
David Woodhouse9c540042008-12-23 04:09:02 +0000698 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
David Woodhouse90937232009-01-28 16:46:56 +1100699 size, le16_to_cpu(header->vpi),
700 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000701 print_buffer(skb);
702 }
703
David Woodhouse90937232009-01-28 16:46:56 +1100704 switch (le16_to_cpu(header->type)) {
David Woodhouse9c540042008-12-23 04:09:02 +0000705 case PKT_DATA:
David Woodhouse90937232009-01-28 16:46:56 +1100706 vcc = find_vcc(card->atmdev[port], le16_to_cpu(header->vpi),
707 le16_to_cpu(header->vci));
David Woodhouse9c540042008-12-23 04:09:02 +0000708 if (!vcc) {
709 if (net_ratelimit())
Philip A. Prindeville1e19e652011-03-30 13:22:45 +0000710 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),
David Woodhouse9c540042008-12-23 04:09:02 +0000712 port);
713 continue;
714 }
715 atm_charge(vcc, skb->truesize);
716 vcc->push(vcc, skb);
717 atomic_inc(&vcc->stats->rx);
718 break;
719
David Woodhouse87ebb182009-01-27 20:02:30 +1100720 case PKT_STATUS:
David Woodhouse95852f42009-01-30 14:23:52 +1100721 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 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100726 dev_kfree_skb_any(skb);
David Woodhouse87ebb182009-01-27 20:02:30 +1100727 break;
728
David Woodhouse9c540042008-12-23 04:09:02 +0000729 case PKT_COMMAND:
730 default: /* FIXME: Not really, surely? */
David Woodhouse01e2ffa2009-01-27 16:20:04 +1100731 if (process_command(card, port, skb))
732 break;
David Woodhouse9c540042008-12-23 04:09:02 +0000733 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);
David Woodhouseeaf83e32009-01-29 10:51:11 +1100738 dev_kfree_skb_any(skb);
David Woodhouse9c540042008-12-23 04:09:02 +0000739 } else
740 skb_queue_tail(&card->cli_queue[port], skb);
741 spin_unlock(&card->cli_queue_lock);
742 break;
743 }
744 }
David Woodhouseeaf83e32009-01-29 10:51:11 +1100745 /* Allocate RX skbs for any ports which need them */
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 /* We'll have to try again later */
761 tasklet_schedule(&card->tlet);
762 }
763 }
David Woodhouse9c540042008-12-23 04:09:02 +0000764 }
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 &&
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700783 vcc->vpi == vpi && vcc->qos.rxtp.traffic_class != ATM_NONE &&
784 test_bit(ATM_VF_READY, &vcc->flags))
David Woodhouse9c540042008-12-23 04:09:02 +0000785 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 {
David Woodhouse1e615df2009-01-27 21:47:47 +1100814 for(i = 0; i < VCC_HTABLE_SIZE; i++){
David Woodhouse9c540042008-12-23 04:09:02 +0000815 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
David Woodhouseb28a4b92009-01-27 21:50:36 +1100837 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
David Woodhouse9c540042008-12-23 04:09:02 +0000843 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
Jesper Juhlda1ab3e2011-02-13 10:49:32 +0000844 if (!skb) {
845 if (net_ratelimit())
846 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in popen()\n");
David Woodhouse9c540042008-12-23 04:09:02 +0000847 return -ENOMEM;
848 }
849 header = (void *)skb_put(skb, sizeof(*header));
850
David Woodhouseb76811a2009-01-27 10:18:51 +1100851 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000852 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
David Woodhousebdc54622009-01-29 14:37:20 +1100858 set_bit(ATM_VF_ADDR, &vcc->flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000859 set_bit(ATM_VF_READY, &vcc->flags);
860 list_vccs(0);
861
David Woodhouse9c540042008-12-23 04:09:02 +0000862
863 return 0;
864}
865
866static void pclose(struct atm_vcc *vcc)
867{
868 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000869 unsigned char port = SOLOS_CHAN(vcc->dev);
David Woodhouse9c540042008-12-23 04:09:02 +0000870 struct sk_buff *skb;
871 struct pkt_hdr *header;
872
873 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
874 if (!skb) {
875 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in pclose()\n");
876 return;
877 }
878 header = (void *)skb_put(skb, sizeof(*header));
879
David Woodhouseb76811a2009-01-27 10:18:51 +1100880 header->size = cpu_to_le16(0);
David Woodhouse9c540042008-12-23 04:09:02 +0000881 header->vpi = cpu_to_le16(vcc->vpi);
882 header->vci = cpu_to_le16(vcc->vci);
883 header->type = cpu_to_le16(PKT_PCLOSE);
884
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000885 skb_get(skb);
886 fpga_queue(card, port, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +0000887
David Woodhouse9c540042008-12-23 04:09:02 +0000888 clear_bit(ATM_VF_ADDR, &vcc->flags);
889 clear_bit(ATM_VF_READY, &vcc->flags);
890
David Woodhouse7ad3ead2012-11-27 23:49:24 +0000891 if (!wait_event_timeout(card->param_wq, !skb_shared(skb), 5 * HZ))
892 dev_warn(&card->dev->dev,
893 "Timeout waiting for VCC close on port %d\n", port);
894
895 dev_kfree_skb(skb);
896
David Woodhouse1f6ea6e2010-08-07 23:02:59 -0700897 /* Hold up vcc_destroy_socket() (our caller) until solos_bh() in the
898 tasklet has finished processing any incoming packets (and, more to
899 the point, using the vcc pointer). */
900 tasklet_unlock_wait(&card->tlet);
David Woodhouse9c540042008-12-23 04:09:02 +0000901 return;
902}
903
904static int print_buffer(struct sk_buff *buf)
905{
906 int len,i;
907 char msg[500];
908 char item[10];
909
910 len = buf->len;
911 for (i = 0; i < len; i++){
912 if(i % 8 == 0)
913 sprintf(msg, "%02X: ", i);
914
915 sprintf(item,"%02X ",*(buf->data + i));
916 strcat(msg, item);
917 if(i % 8 == 7) {
918 sprintf(item, "\n");
919 strcat(msg, item);
920 printk(KERN_DEBUG "%s", msg);
921 }
922 }
923 if (i % 8 != 0) {
924 sprintf(item, "\n");
925 strcat(msg, item);
926 printk(KERN_DEBUG "%s", msg);
927 }
928 printk(KERN_DEBUG "\n");
929
930 return 0;
931}
932
933static void fpga_queue(struct solos_card *card, int port, struct sk_buff *skb,
934 struct atm_vcc *vcc)
935{
936 int old_len;
David Woodhousef69e4172009-01-29 11:10:58 +1100937 unsigned long flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000938
David Woodhouse90937232009-01-28 16:46:56 +1100939 SKB_CB(skb)->vcc = vcc;
David Woodhouse9c540042008-12-23 04:09:02 +0000940
David Woodhousef69e4172009-01-29 11:10:58 +1100941 spin_lock_irqsave(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000942 old_len = skb_queue_len(&card->tx_queue[port]);
943 skb_queue_tail(&card->tx_queue[port], skb);
David Woodhouse35c22212009-01-29 14:08:27 +1100944 if (!old_len)
David Woodhousef69e4172009-01-29 11:10:58 +1100945 card->tx_mask |= (1 << port);
David Woodhousef69e4172009-01-29 11:10:58 +1100946 spin_unlock_irqrestore(&card->tx_queue_lock, flags);
David Woodhouse9c540042008-12-23 04:09:02 +0000947
David Woodhousef69e4172009-01-29 11:10:58 +1100948 /* Theoretically we could just schedule the tasklet here, but
949 that introduces latency we don't want -- it's noticeable */
David Woodhouse9c540042008-12-23 04:09:02 +0000950 if (!old_len)
951 fpga_tx(card);
952}
953
David Woodhouse35c22212009-01-29 14:08:27 +1100954static uint32_t fpga_tx(struct solos_card *card)
David Woodhouse9c540042008-12-23 04:09:02 +0000955{
David Woodhouse35c22212009-01-29 14:08:27 +1100956 uint32_t tx_pending, card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000957 uint32_t tx_started = 0;
958 struct sk_buff *skb;
959 struct atm_vcc *vcc;
960 unsigned char port;
961 unsigned long flags;
962
963 spin_lock_irqsave(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +1100964
965 card_flags = ioread32(card->config_regs + FLAGS_ADDR);
966 /*
967 * The queue lock is required for _writing_ to tx_mask, but we're
968 * OK to read it here without locking. The only potential update
969 * that we could race with is in fpga_queue() where it sets a bit
970 * for a new port... but it's going to call this function again if
971 * it's doing that, anyway.
972 */
973 tx_pending = card->tx_mask & ~card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +0000974
David Woodhouse35c22212009-01-29 14:08:27 +1100975 for (port = 0; tx_pending; tx_pending >>= 1, port++) {
976 if (tx_pending & 1) {
David Woodhouseeaf83e32009-01-29 10:51:11 +1100977 struct sk_buff *oldskb = card->tx_skb[port];
David Woodhouseeaf83e32009-01-29 10:51:11 +1100978 if (oldskb)
979 pci_unmap_single(card->dev, SKB_CB(oldskb)->dma_addr,
980 oldskb->len, PCI_DMA_TODEVICE);
David Woodhouse9c540042008-12-23 04:09:02 +0000981
982 spin_lock(&card->tx_queue_lock);
983 skb = skb_dequeue(&card->tx_queue[port]);
David Woodhousef69e4172009-01-29 11:10:58 +1100984 if (!skb)
985 card->tx_mask &= ~(1 << port);
David Woodhouse9c540042008-12-23 04:09:02 +0000986 spin_unlock(&card->tx_queue_lock);
987
David Woodhouseeaf83e32009-01-29 10:51:11 +1100988 if (skb && !card->using_dma) {
989 memcpy_toio(TX_BUF(card, port), skb->data, skb->len);
David Woodhousebdc54622009-01-29 14:37:20 +1100990 tx_started |= 1 << port;
David Woodhouseeaf83e32009-01-29 10:51:11 +1100991 oldskb = skb; /* We're done with this skb already */
992 } else if (skb && card->using_dma) {
993 SKB_CB(skb)->dma_addr = pci_map_single(card->dev, skb->data,
994 skb->len, PCI_DMA_TODEVICE);
David Woodhouseb4bd8ad2012-05-24 04:58:27 +0000995 card->tx_skb[port] = skb;
David Woodhouseeaf83e32009-01-29 10:51:11 +1100996 iowrite32(SKB_CB(skb)->dma_addr,
997 card->config_regs + TX_DMA_ADDR(port));
998 }
999
1000 if (!oldskb)
David Woodhouse9c540042008-12-23 04:09:02 +00001001 continue;
1002
David Woodhouseeaf83e32009-01-29 10:51:11 +11001003 /* Clean up and free oldskb now it's gone */
David Woodhouse9c540042008-12-23 04:09:02 +00001004 if (atmdebug) {
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001005 struct pkt_hdr *header = (void *)oldskb->data;
1006 int size = le16_to_cpu(header->size);
1007
1008 skb_pull(oldskb, sizeof(*header));
David Woodhouse9c540042008-12-23 04:09:02 +00001009 dev_info(&card->dev->dev, "Transmitted: port %d\n",
1010 port);
Philip A. Prindeville18b429e2011-03-30 12:59:26 +00001011 dev_info(&card->dev->dev, "size: %d VPI: %d VCI: %d\n",
1012 size, le16_to_cpu(header->vpi),
1013 le16_to_cpu(header->vci));
David Woodhouseeaf83e32009-01-29 10:51:11 +11001014 print_buffer(oldskb);
David Woodhouse9c540042008-12-23 04:09:02 +00001015 }
David Woodhouse9c540042008-12-23 04:09:02 +00001016
David Woodhouseeaf83e32009-01-29 10:51:11 +11001017 vcc = SKB_CB(oldskb)->vcc;
David Woodhouse9c540042008-12-23 04:09:02 +00001018
1019 if (vcc) {
1020 atomic_inc(&vcc->stats->tx);
David Woodhouseeaf83e32009-01-29 10:51:11 +11001021 solos_pop(vcc, oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001022 } else {
David Woodhouseeaf83e32009-01-29 10:51:11 +11001023 dev_kfree_skb_irq(oldskb);
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001024 wake_up(&card->param_wq);
1025 }
David Woodhouse9c540042008-12-23 04:09:02 +00001026 }
1027 }
David Woodhousebdc54622009-01-29 14:37:20 +11001028 /* For non-DMA TX, write the 'TX start' bit for all four ports simultaneously */
David Woodhouse9c540042008-12-23 04:09:02 +00001029 if (tx_started)
1030 iowrite32(tx_started, card->config_regs + FLAGS_ADDR);
1031
1032 spin_unlock_irqrestore(&card->tx_lock, flags);
David Woodhouse35c22212009-01-29 14:08:27 +11001033 return card_flags;
David Woodhouse9c540042008-12-23 04:09:02 +00001034}
1035
1036static int psend(struct atm_vcc *vcc, struct sk_buff *skb)
1037{
1038 struct solos_card *card = vcc->dev->dev_data;
David Woodhouse9c540042008-12-23 04:09:02 +00001039 struct pkt_hdr *header;
David Woodhouseb76811a2009-01-27 10:18:51 +11001040 int pktlen;
David Woodhouse9c540042008-12-23 04:09:02 +00001041
David Woodhouseb76811a2009-01-27 10:18:51 +11001042 pktlen = skb->len;
1043 if (pktlen > (BUF_SIZE - sizeof(*header))) {
David Woodhouse9c540042008-12-23 04:09:02 +00001044 dev_warn(&card->dev->dev, "Length of PDU is too large. Dropping PDU.\n");
1045 solos_pop(vcc, skb);
1046 return 0;
1047 }
1048
1049 if (!skb_clone_writable(skb, sizeof(*header))) {
1050 int expand_by = 0;
1051 int ret;
1052
1053 if (skb_headroom(skb) < sizeof(*header))
1054 expand_by = sizeof(*header) - skb_headroom(skb);
1055
1056 ret = pskb_expand_head(skb, expand_by, 0, GFP_ATOMIC);
1057 if (ret) {
Simon Farnsworth4306cad2009-01-19 21:19:29 +00001058 dev_warn(&card->dev->dev, "pskb_expand_head failed.\n");
David Woodhouse9c540042008-12-23 04:09:02 +00001059 solos_pop(vcc, skb);
1060 return ret;
1061 }
1062 }
1063
1064 header = (void *)skb_push(skb, sizeof(*header));
1065
David Woodhouseb76811a2009-01-27 10:18:51 +11001066 /* This does _not_ include the size of the header */
1067 header->size = cpu_to_le16(pktlen);
David Woodhouse9c540042008-12-23 04:09:02 +00001068 header->vpi = cpu_to_le16(vcc->vpi);
1069 header->vci = cpu_to_le16(vcc->vci);
1070 header->type = cpu_to_le16(PKT_DATA);
1071
1072 fpga_queue(card, SOLOS_CHAN(vcc->dev), skb, vcc);
1073
1074 return 0;
1075}
1076
1077static struct atmdev_ops fpga_ops = {
1078 .open = popen,
1079 .close = pclose,
1080 .ioctl = NULL,
1081 .getsockopt = NULL,
1082 .setsockopt = NULL,
1083 .send = psend,
1084 .send_oam = NULL,
1085 .phy_put = NULL,
1086 .phy_get = NULL,
1087 .change_qos = NULL,
1088 .proc_read = NULL,
1089 .owner = THIS_MODULE
1090};
1091
1092static int fpga_probe(struct pci_dev *dev, const struct pci_device_id *id)
1093{
David Woodhousecd5549e2009-01-30 14:26:37 +11001094 int err;
David Woodhouse9c540042008-12-23 04:09:02 +00001095 uint16_t fpga_ver;
1096 uint8_t major_ver, minor_ver;
1097 uint32_t data32;
1098 struct solos_card *card;
1099
David Woodhouse9c540042008-12-23 04:09:02 +00001100 card = kzalloc(sizeof(*card), GFP_KERNEL);
1101 if (!card)
1102 return -ENOMEM;
1103
1104 card->dev = dev;
David Woodhousefa755b92009-01-27 14:16:12 +11001105 init_waitqueue_head(&card->fw_wq);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001106 init_waitqueue_head(&card->param_wq);
David Woodhouse9c540042008-12-23 04:09:02 +00001107
1108 err = pci_enable_device(dev);
1109 if (err) {
1110 dev_warn(&dev->dev, "Failed to enable PCI device\n");
1111 goto out;
1112 }
1113
Yang Hongyange9304382009-04-13 14:40:14 -07001114 err = pci_set_dma_mask(dev, DMA_BIT_MASK(32));
David Woodhouse90937232009-01-28 16:46:56 +11001115 if (err) {
1116 dev_warn(&dev->dev, "Failed to set 32-bit DMA mask\n");
1117 goto out;
1118 }
1119
David Woodhouse9c540042008-12-23 04:09:02 +00001120 err = pci_request_regions(dev, "solos");
1121 if (err) {
1122 dev_warn(&dev->dev, "Failed to request regions\n");
1123 goto out;
1124 }
1125
1126 card->config_regs = pci_iomap(dev, 0, CONFIG_RAM_SIZE);
1127 if (!card->config_regs) {
1128 dev_warn(&dev->dev, "Failed to ioremap config registers\n");
1129 goto out_release_regions;
1130 }
1131 card->buffers = pci_iomap(dev, 1, DATA_RAM_SIZE);
1132 if (!card->buffers) {
1133 dev_warn(&dev->dev, "Failed to ioremap data buffers\n");
1134 goto out_unmap_config;
1135 }
1136
David Woodhousecc3657e2009-01-30 14:23:22 +11001137 if (reset) {
1138 iowrite32(1, card->config_regs + FPGA_MODE);
1139 data32 = ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001140
David Woodhousecc3657e2009-01-30 14:23:22 +11001141 iowrite32(0, card->config_regs + FPGA_MODE);
1142 data32 = ioread32(card->config_regs + FPGA_MODE);
1143 }
David Woodhouse9c540042008-12-23 04:09:02 +00001144
1145 data32 = ioread32(card->config_regs + FPGA_VER);
1146 fpga_ver = (data32 & 0x0000FFFF);
1147 major_ver = ((data32 & 0xFF000000) >> 24);
1148 minor_ver = ((data32 & 0x00FF0000) >> 16);
Nathan Williams4dbedf42009-03-25 20:27:37 +11001149 card->fpga_version = FPGA_VERSION(major_ver,minor_ver);
1150 if (card->fpga_version > LEGACY_BUFFERS)
1151 card->buffer_size = BUF_SIZE;
1152 else
1153 card->buffer_size = OLD_BUF_SIZE;
David Woodhouse9c540042008-12-23 04:09:02 +00001154 dev_info(&dev->dev, "Solos FPGA Version %d.%02d svn-%d\n",
1155 major_ver, minor_ver, fpga_ver);
1156
David Woodhouse3ce12272010-11-01 10:35:28 +00001157 if (fpga_ver < 37 && (fpga_upgrade || firmware_upgrade ||
1158 db_fpga_upgrade || db_firmware_upgrade)) {
1159 dev_warn(&dev->dev,
1160 "FPGA too old; cannot upgrade flash. Use JTAG.\n");
1161 fpga_upgrade = firmware_upgrade = 0;
1162 db_fpga_upgrade = db_firmware_upgrade = 0;
1163 }
1164
David Woodhouseb4bd8ad2012-05-24 04:58:27 +00001165 if (card->fpga_version >= DMA_SUPPORTED) {
1166 pci_set_master(dev);
David Woodhouse90937232009-01-28 16:46:56 +11001167 card->using_dma = 1;
Nathan Williams4dbedf42009-03-25 20:27:37 +11001168 } else {
1169 card->using_dma = 0;
David Woodhouseeab50f72009-01-30 14:27:26 +11001170 /* Set RX empty flag for all ports */
1171 iowrite32(0xF0, card->config_regs + FLAGS_ADDR);
1172 }
David Woodhouse9c540042008-12-23 04:09:02 +00001173
Nathan Williams0fc36aa2009-02-07 10:19:13 +11001174 data32 = ioread32(card->config_regs + PORTS);
1175 card->nr_ports = (data32 & 0x000000FF);
David Woodhouse9c540042008-12-23 04:09:02 +00001176
1177 pci_set_drvdata(dev, card);
David Woodhousefa755b92009-01-27 14:16:12 +11001178
David Woodhouse9c540042008-12-23 04:09:02 +00001179 tasklet_init(&card->tlet, solos_bh, (unsigned long)card);
1180 spin_lock_init(&card->tx_lock);
1181 spin_lock_init(&card->tx_queue_lock);
1182 spin_lock_init(&card->cli_queue_lock);
David Woodhouse01e2ffa2009-01-27 16:20:04 +11001183 spin_lock_init(&card->param_queue_lock);
1184 INIT_LIST_HEAD(&card->param_queue);
David Woodhouse9c540042008-12-23 04:09:02 +00001185
David Woodhousefcd82662009-01-29 11:29:12 +11001186 err = request_irq(dev->irq, solos_irq, IRQF_SHARED,
David Woodhouse9c540042008-12-23 04:09:02 +00001187 "solos-pci", card);
David Woodhousefa755b92009-01-27 14:16:12 +11001188 if (err) {
David Woodhouse9c540042008-12-23 04:09:02 +00001189 dev_dbg(&card->dev->dev, "Failed to request interrupt IRQ: %d\n", dev->irq);
David Woodhousefa755b92009-01-27 14:16:12 +11001190 goto out_unmap_both;
1191 }
David Woodhouse9c540042008-12-23 04:09:02 +00001192
David Woodhouse9c540042008-12-23 04:09:02 +00001193 iowrite32(1, card->config_regs + IRQ_EN_ADDR);
1194
David Woodhousefa755b92009-01-27 14:16:12 +11001195 if (fpga_upgrade)
1196 flash_upgrade(card, 0);
1197
1198 if (firmware_upgrade)
1199 flash_upgrade(card, 1);
1200
Nathan Williams4dbedf42009-03-25 20:27:37 +11001201 if (db_fpga_upgrade)
1202 flash_upgrade(card, 2);
1203
1204 if (db_firmware_upgrade)
1205 flash_upgrade(card, 3);
1206
Dan Williamsd9ca6762010-12-08 19:40:47 +00001207 err = atm_init(card, &dev->dev);
David Woodhousefa755b92009-01-27 14:16:12 +11001208 if (err)
1209 goto out_free_irq;
1210
David Woodhouse9c540042008-12-23 04:09:02 +00001211 return 0;
1212
David Woodhousefa755b92009-01-27 14:16:12 +11001213 out_free_irq:
1214 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
1215 free_irq(dev->irq, card);
1216 tasklet_kill(&card->tlet);
1217
David Woodhouse9c540042008-12-23 04:09:02 +00001218 out_unmap_both:
David Woodhousefa755b92009-01-27 14:16:12 +11001219 pci_set_drvdata(dev, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001220 pci_iounmap(dev, card->buffers);
Julia Lawall8ae0cfe2012-02-19 09:43:32 +00001221 out_unmap_config:
1222 pci_iounmap(dev, card->config_regs);
David Woodhouse9c540042008-12-23 04:09:02 +00001223 out_release_regions:
1224 pci_release_regions(dev);
1225 out:
Julia Lawallbc111d52009-02-08 17:00:02 -08001226 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001227 return err;
1228}
1229
Dan Williamsd9ca6762010-12-08 19:40:47 +00001230static int atm_init(struct solos_card *card, struct device *parent)
David Woodhouse9c540042008-12-23 04:09:02 +00001231{
1232 int i;
1233
David Woodhouse9c540042008-12-23 04:09:02 +00001234 for (i = 0; i < card->nr_ports; i++) {
David Woodhouse87ebb182009-01-27 20:02:30 +11001235 struct sk_buff *skb;
1236 struct pkt_hdr *header;
1237
David Woodhouse9c540042008-12-23 04:09:02 +00001238 skb_queue_head_init(&card->tx_queue[i]);
1239 skb_queue_head_init(&card->cli_queue[i]);
1240
Dan Williamsd9ca6762010-12-08 19:40:47 +00001241 card->atmdev[i] = atm_dev_register("solos-pci", parent, &fpga_ops, -1, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001242 if (!card->atmdev[i]) {
1243 dev_err(&card->dev->dev, "Could not register ATM device %d\n", i);
1244 atm_remove(card);
1245 return -ENODEV;
1246 }
1247 if (device_create_file(&card->atmdev[i]->class_dev, &dev_attr_console))
1248 dev_err(&card->dev->dev, "Could not register console for ATM device %d\n", i);
David Woodhoused057f0a2009-01-27 16:52:07 +11001249 if (sysfs_create_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group))
1250 dev_err(&card->dev->dev, "Could not register parameter group for ATM device %d\n", i);
David Woodhouse9c540042008-12-23 04:09:02 +00001251
1252 dev_info(&card->dev->dev, "Registered ATM device %d\n", card->atmdev[i]->number);
1253
1254 card->atmdev[i]->ci_range.vpi_bits = 8;
1255 card->atmdev[i]->ci_range.vci_bits = 16;
1256 card->atmdev[i]->dev_data = card;
1257 card->atmdev[i]->phy_data = (void *)(unsigned long)i;
Philip A. Prindevillec0312352011-03-30 13:17:04 +00001258 atm_dev_signal_change(card->atmdev[i], ATM_PHY_SIG_FOUND);
David Woodhouse87ebb182009-01-27 20:02:30 +11001259
1260 skb = alloc_skb(sizeof(*header), GFP_ATOMIC);
1261 if (!skb) {
1262 dev_warn(&card->dev->dev, "Failed to allocate sk_buff in atm_init()\n");
1263 continue;
1264 }
1265
1266 header = (void *)skb_put(skb, sizeof(*header));
1267
1268 header->size = cpu_to_le16(0);
1269 header->vpi = cpu_to_le16(0);
1270 header->vci = cpu_to_le16(0);
1271 header->type = cpu_to_le16(PKT_STATUS);
1272
1273 fpga_queue(card, i, skb, NULL);
David Woodhouse9c540042008-12-23 04:09:02 +00001274 }
1275 return 0;
1276}
1277
1278static void atm_remove(struct solos_card *card)
1279{
1280 int i;
1281
1282 for (i = 0; i < card->nr_ports; i++) {
1283 if (card->atmdev[i]) {
David Woodhouse97d759d2009-03-17 14:59:34 +00001284 struct sk_buff *skb;
1285
David Woodhouse9c540042008-12-23 04:09:02 +00001286 dev_info(&card->dev->dev, "Unregistering ATM device %d\n", card->atmdev[i]->number);
David Woodhousec0fe3022009-01-28 14:34:34 +11001287
1288 sysfs_remove_group(&card->atmdev[i]->class_dev.kobj, &solos_attr_group);
David Woodhouse9c540042008-12-23 04:09:02 +00001289 atm_dev_deregister(card->atmdev[i]);
David Woodhouse97d759d2009-03-17 14:59:34 +00001290
1291 skb = card->rx_skb[i];
1292 if (skb) {
1293 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1294 RX_DMA_SIZE, PCI_DMA_FROMDEVICE);
1295 dev_kfree_skb(skb);
1296 }
1297 skb = card->tx_skb[i];
1298 if (skb) {
1299 pci_unmap_single(card->dev, SKB_CB(skb)->dma_addr,
1300 skb->len, PCI_DMA_TODEVICE);
1301 dev_kfree_skb(skb);
1302 }
1303 while ((skb = skb_dequeue(&card->tx_queue[i])))
1304 dev_kfree_skb(skb);
1305
David Woodhouse9c540042008-12-23 04:09:02 +00001306 }
1307 }
1308}
1309
1310static void fpga_remove(struct pci_dev *dev)
1311{
1312 struct solos_card *card = pci_get_drvdata(dev);
David Woodhouse97d759d2009-03-17 14:59:34 +00001313
1314 /* Disable IRQs */
1315 iowrite32(0, card->config_regs + IRQ_EN_ADDR);
David Woodhouse9c540042008-12-23 04:09:02 +00001316
David Woodhouse97d759d2009-03-17 14:59:34 +00001317 /* Reset FPGA */
1318 iowrite32(1, card->config_regs + FPGA_MODE);
1319 (void)ioread32(card->config_regs + FPGA_MODE);
David Woodhouse9c540042008-12-23 04:09:02 +00001320
1321 atm_remove(card);
1322
David Woodhouse9c540042008-12-23 04:09:02 +00001323 free_irq(dev->irq, card);
1324 tasklet_kill(&card->tlet);
1325
David Woodhouse97d759d2009-03-17 14:59:34 +00001326 /* Release device from reset */
1327 iowrite32(0, card->config_regs + FPGA_MODE);
1328 (void)ioread32(card->config_regs + FPGA_MODE);
1329
David Woodhouse9c540042008-12-23 04:09:02 +00001330 pci_iounmap(dev, card->buffers);
1331 pci_iounmap(dev, card->config_regs);
1332
David Woodhouse9c540042008-12-23 04:09:02 +00001333 pci_release_regions(dev);
1334 pci_disable_device(dev);
1335
1336 pci_set_drvdata(dev, NULL);
1337 kfree(card);
David Woodhouse9c540042008-12-23 04:09:02 +00001338}
1339
1340static struct pci_device_id fpga_pci_tbl[] __devinitdata = {
1341 { 0x10ee, 0x0300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1342 { 0, }
1343};
1344
1345MODULE_DEVICE_TABLE(pci,fpga_pci_tbl);
1346
1347static struct pci_driver fpga_driver = {
1348 .name = "solos",
1349 .id_table = fpga_pci_tbl,
1350 .probe = fpga_probe,
1351 .remove = fpga_remove,
1352};
1353
1354
1355static int __init solos_pci_init(void)
1356{
David Woodhouse7ad3ead2012-11-27 23:49:24 +00001357 BUILD_BUG_ON(sizeof(struct solos_skb_cb) > sizeof(((struct sk_buff *)0)->cb));
1358
David Woodhouse9c540042008-12-23 04:09:02 +00001359 printk(KERN_INFO "Solos PCI Driver Version %s\n", VERSION);
1360 return pci_register_driver(&fpga_driver);
1361}
1362
1363static void __exit solos_pci_exit(void)
1364{
1365 pci_unregister_driver(&fpga_driver);
1366 printk(KERN_INFO "Solos PCI Driver %s Unloaded\n", VERSION);
1367}
1368
1369module_init(solos_pci_init);
1370module_exit(solos_pci_exit);