solos-pci.c 24 KB

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