solos-pci.c 39 KB

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