solos-pci.c 27 KB

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