solos-pci.c 39 KB

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