wanxl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. /*
  2. * wanXL serial card driver for Linux
  3. * host part
  4. *
  5. * Copyright (C) 2003 Krzysztof Halasa <khc@pm.waw.pl>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License
  9. * as published by the Free Software Foundation.
  10. *
  11. * Status:
  12. * - Only DTE (external clock) support with NRZ and NRZI encodings
  13. * - wanXL100 will require minor driver modifications, no access to hw
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched.h>
  20. #include <linux/types.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/string.h>
  23. #include <linux/errno.h>
  24. #include <linux/init.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/ioport.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/hdlc.h>
  29. #include <linux/pci.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/delay.h>
  32. #include <asm/io.h>
  33. #include "wanxl.h"
  34. static const char* version = "wanXL serial card driver version: 0.48";
  35. #define PLX_CTL_RESET 0x40000000 /* adapter reset */
  36. #undef DEBUG_PKT
  37. #undef DEBUG_PCI
  38. /* MAILBOX #1 - PUTS COMMANDS */
  39. #define MBX1_CMD_ABORTJ 0x85000000 /* Abort and Jump */
  40. #ifdef __LITTLE_ENDIAN
  41. #define MBX1_CMD_BSWAP 0x8C000001 /* little-endian Byte Swap Mode */
  42. #else
  43. #define MBX1_CMD_BSWAP 0x8C000000 /* big-endian Byte Swap Mode */
  44. #endif
  45. /* MAILBOX #2 - DRAM SIZE */
  46. #define MBX2_MEMSZ_MASK 0xFFFF0000 /* PUTS Memory Size Register mask */
  47. struct port {
  48. struct net_device *dev;
  49. struct card *card;
  50. spinlock_t lock; /* for wanxl_xmit */
  51. int node; /* physical port #0 - 3 */
  52. unsigned int clock_type;
  53. int tx_in, tx_out;
  54. struct sk_buff *tx_skbs[TX_BUFFERS];
  55. };
  56. struct card_status {
  57. desc_t rx_descs[RX_QUEUE_LENGTH];
  58. port_status_t port_status[4];
  59. };
  60. struct card {
  61. int n_ports; /* 1, 2 or 4 ports */
  62. u8 irq;
  63. u8 __iomem *plx; /* PLX PCI9060 virtual base address */
  64. struct pci_dev *pdev; /* for pci_name(pdev) */
  65. int rx_in;
  66. struct sk_buff *rx_skbs[RX_QUEUE_LENGTH];
  67. struct card_status *status; /* shared between host and card */
  68. dma_addr_t status_address;
  69. struct port ports[0]; /* 1 - 4 port structures follow */
  70. };
  71. static inline struct port *dev_to_port(struct net_device *dev)
  72. {
  73. return (struct port *)dev_to_hdlc(dev)->priv;
  74. }
  75. static inline port_status_t *get_status(struct port *port)
  76. {
  77. return &port->card->status->port_status[port->node];
  78. }
  79. #ifdef DEBUG_PCI
  80. static inline dma_addr_t pci_map_single_debug(struct pci_dev *pdev, void *ptr,
  81. size_t size, int direction)
  82. {
  83. dma_addr_t addr = pci_map_single(pdev, ptr, size, direction);
  84. if (addr + size > 0x100000000LL)
  85. pr_crit("%s: pci_map_single() returned memory at 0x%llx!\n",
  86. pci_name(pdev), (unsigned long long)addr);
  87. return addr;
  88. }
  89. #undef pci_map_single
  90. #define pci_map_single pci_map_single_debug
  91. #endif
  92. /* Cable and/or personality module change interrupt service */
  93. static inline void wanxl_cable_intr(struct port *port)
  94. {
  95. u32 value = get_status(port)->cable;
  96. int valid = 1;
  97. const char *cable, *pm, *dte = "", *dsr = "", *dcd = "";
  98. switch(value & 0x7) {
  99. case STATUS_CABLE_V35: cable = "V.35"; break;
  100. case STATUS_CABLE_X21: cable = "X.21"; break;
  101. case STATUS_CABLE_V24: cable = "V.24"; break;
  102. case STATUS_CABLE_EIA530: cable = "EIA530"; break;
  103. case STATUS_CABLE_NONE: cable = "no"; break;
  104. default: cable = "invalid";
  105. }
  106. switch((value >> STATUS_CABLE_PM_SHIFT) & 0x7) {
  107. case STATUS_CABLE_V35: pm = "V.35"; break;
  108. case STATUS_CABLE_X21: pm = "X.21"; break;
  109. case STATUS_CABLE_V24: pm = "V.24"; break;
  110. case STATUS_CABLE_EIA530: pm = "EIA530"; break;
  111. case STATUS_CABLE_NONE: pm = "no personality"; valid = 0; break;
  112. default: pm = "invalid personality"; valid = 0;
  113. }
  114. if (valid) {
  115. if ((value & 7) == ((value >> STATUS_CABLE_PM_SHIFT) & 7)) {
  116. dsr = (value & STATUS_CABLE_DSR) ? ", DSR ON" :
  117. ", DSR off";
  118. dcd = (value & STATUS_CABLE_DCD) ? ", carrier ON" :
  119. ", carrier off";
  120. }
  121. dte = (value & STATUS_CABLE_DCE) ? " DCE" : " DTE";
  122. }
  123. netdev_info(port->dev, "%s%s module, %s cable%s%s\n",
  124. pm, dte, cable, dsr, dcd);
  125. if (value & STATUS_CABLE_DCD)
  126. netif_carrier_on(port->dev);
  127. else
  128. netif_carrier_off(port->dev);
  129. }
  130. /* Transmit complete interrupt service */
  131. static inline void wanxl_tx_intr(struct port *port)
  132. {
  133. struct net_device *dev = port->dev;
  134. while (1) {
  135. desc_t *desc = &get_status(port)->tx_descs[port->tx_in];
  136. struct sk_buff *skb = port->tx_skbs[port->tx_in];
  137. switch (desc->stat) {
  138. case PACKET_FULL:
  139. case PACKET_EMPTY:
  140. netif_wake_queue(dev);
  141. return;
  142. case PACKET_UNDERRUN:
  143. dev->stats.tx_errors++;
  144. dev->stats.tx_fifo_errors++;
  145. break;
  146. default:
  147. dev->stats.tx_packets++;
  148. dev->stats.tx_bytes += skb->len;
  149. }
  150. desc->stat = PACKET_EMPTY; /* Free descriptor */
  151. pci_unmap_single(port->card->pdev, desc->address, skb->len,
  152. PCI_DMA_TODEVICE);
  153. dev_kfree_skb_irq(skb);
  154. port->tx_in = (port->tx_in + 1) % TX_BUFFERS;
  155. }
  156. }
  157. /* Receive complete interrupt service */
  158. static inline void wanxl_rx_intr(struct card *card)
  159. {
  160. desc_t *desc;
  161. while (desc = &card->status->rx_descs[card->rx_in],
  162. desc->stat != PACKET_EMPTY) {
  163. if ((desc->stat & PACKET_PORT_MASK) > card->n_ports)
  164. pr_crit("%s: received packet for nonexistent port\n",
  165. pci_name(card->pdev));
  166. else {
  167. struct sk_buff *skb = card->rx_skbs[card->rx_in];
  168. struct port *port = &card->ports[desc->stat &
  169. PACKET_PORT_MASK];
  170. struct net_device *dev = port->dev;
  171. if (!skb)
  172. dev->stats.rx_dropped++;
  173. else {
  174. pci_unmap_single(card->pdev, desc->address,
  175. BUFFER_LENGTH,
  176. PCI_DMA_FROMDEVICE);
  177. skb_put(skb, desc->length);
  178. #ifdef DEBUG_PKT
  179. printk(KERN_DEBUG "%s RX(%i):", dev->name,
  180. skb->len);
  181. debug_frame(skb);
  182. #endif
  183. dev->stats.rx_packets++;
  184. dev->stats.rx_bytes += skb->len;
  185. skb->protocol = hdlc_type_trans(skb, dev);
  186. netif_rx(skb);
  187. skb = NULL;
  188. }
  189. if (!skb) {
  190. skb = dev_alloc_skb(BUFFER_LENGTH);
  191. desc->address = skb ?
  192. pci_map_single(card->pdev, skb->data,
  193. BUFFER_LENGTH,
  194. PCI_DMA_FROMDEVICE) : 0;
  195. card->rx_skbs[card->rx_in] = skb;
  196. }
  197. }
  198. desc->stat = PACKET_EMPTY; /* Free descriptor */
  199. card->rx_in = (card->rx_in + 1) % RX_QUEUE_LENGTH;
  200. }
  201. }
  202. static irqreturn_t wanxl_intr(int irq, void* dev_id)
  203. {
  204. struct card *card = dev_id;
  205. int i;
  206. u32 stat;
  207. int handled = 0;
  208. while((stat = readl(card->plx + PLX_DOORBELL_FROM_CARD)) != 0) {
  209. handled = 1;
  210. writel(stat, card->plx + PLX_DOORBELL_FROM_CARD);
  211. for (i = 0; i < card->n_ports; i++) {
  212. if (stat & (1 << (DOORBELL_FROM_CARD_TX_0 + i)))
  213. wanxl_tx_intr(&card->ports[i]);
  214. if (stat & (1 << (DOORBELL_FROM_CARD_CABLE_0 + i)))
  215. wanxl_cable_intr(&card->ports[i]);
  216. }
  217. if (stat & (1 << DOORBELL_FROM_CARD_RX))
  218. wanxl_rx_intr(card);
  219. }
  220. return IRQ_RETVAL(handled);
  221. }
  222. static netdev_tx_t wanxl_xmit(struct sk_buff *skb, struct net_device *dev)
  223. {
  224. struct port *port = dev_to_port(dev);
  225. desc_t *desc;
  226. spin_lock(&port->lock);
  227. desc = &get_status(port)->tx_descs[port->tx_out];
  228. if (desc->stat != PACKET_EMPTY) {
  229. /* should never happen - previous xmit should stop queue */
  230. #ifdef DEBUG_PKT
  231. printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name);
  232. #endif
  233. netif_stop_queue(dev);
  234. spin_unlock(&port->lock);
  235. return NETDEV_TX_BUSY; /* request packet to be queued */
  236. }
  237. #ifdef DEBUG_PKT
  238. printk(KERN_DEBUG "%s TX(%i):", dev->name, skb->len);
  239. debug_frame(skb);
  240. #endif
  241. port->tx_skbs[port->tx_out] = skb;
  242. desc->address = pci_map_single(port->card->pdev, skb->data, skb->len,
  243. PCI_DMA_TODEVICE);
  244. desc->length = skb->len;
  245. desc->stat = PACKET_FULL;
  246. writel(1 << (DOORBELL_TO_CARD_TX_0 + port->node),
  247. port->card->plx + PLX_DOORBELL_TO_CARD);
  248. port->tx_out = (port->tx_out + 1) % TX_BUFFERS;
  249. if (get_status(port)->tx_descs[port->tx_out].stat != PACKET_EMPTY) {
  250. netif_stop_queue(dev);
  251. #ifdef DEBUG_PKT
  252. printk(KERN_DEBUG "%s: transmitter buffer full\n", dev->name);
  253. #endif
  254. }
  255. spin_unlock(&port->lock);
  256. return NETDEV_TX_OK;
  257. }
  258. static int wanxl_attach(struct net_device *dev, unsigned short encoding,
  259. unsigned short parity)
  260. {
  261. struct port *port = dev_to_port(dev);
  262. if (encoding != ENCODING_NRZ &&
  263. encoding != ENCODING_NRZI)
  264. return -EINVAL;
  265. if (parity != PARITY_NONE &&
  266. parity != PARITY_CRC32_PR1_CCITT &&
  267. parity != PARITY_CRC16_PR1_CCITT &&
  268. parity != PARITY_CRC32_PR0_CCITT &&
  269. parity != PARITY_CRC16_PR0_CCITT)
  270. return -EINVAL;
  271. get_status(port)->encoding = encoding;
  272. get_status(port)->parity = parity;
  273. return 0;
  274. }
  275. static int wanxl_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  276. {
  277. const size_t size = sizeof(sync_serial_settings);
  278. sync_serial_settings line;
  279. struct port *port = dev_to_port(dev);
  280. if (cmd != SIOCWANDEV)
  281. return hdlc_ioctl(dev, ifr, cmd);
  282. switch (ifr->ifr_settings.type) {
  283. case IF_GET_IFACE:
  284. ifr->ifr_settings.type = IF_IFACE_SYNC_SERIAL;
  285. if (ifr->ifr_settings.size < size) {
  286. ifr->ifr_settings.size = size; /* data size wanted */
  287. return -ENOBUFS;
  288. }
  289. memset(&line, 0, sizeof(line));
  290. line.clock_type = get_status(port)->clocking;
  291. line.clock_rate = 0;
  292. line.loopback = 0;
  293. if (copy_to_user(ifr->ifr_settings.ifs_ifsu.sync, &line, size))
  294. return -EFAULT;
  295. return 0;
  296. case IF_IFACE_SYNC_SERIAL:
  297. if (!capable(CAP_NET_ADMIN))
  298. return -EPERM;
  299. if (dev->flags & IFF_UP)
  300. return -EBUSY;
  301. if (copy_from_user(&line, ifr->ifr_settings.ifs_ifsu.sync,
  302. size))
  303. return -EFAULT;
  304. if (line.clock_type != CLOCK_EXT &&
  305. line.clock_type != CLOCK_TXFROMRX)
  306. return -EINVAL; /* No such clock setting */
  307. if (line.loopback != 0)
  308. return -EINVAL;
  309. get_status(port)->clocking = line.clock_type;
  310. return 0;
  311. default:
  312. return hdlc_ioctl(dev, ifr, cmd);
  313. }
  314. }
  315. static int wanxl_open(struct net_device *dev)
  316. {
  317. struct port *port = dev_to_port(dev);
  318. u8 __iomem *dbr = port->card->plx + PLX_DOORBELL_TO_CARD;
  319. unsigned long timeout;
  320. int i;
  321. if (get_status(port)->open) {
  322. netdev_err(dev, "port already open\n");
  323. return -EIO;
  324. }
  325. if ((i = hdlc_open(dev)) != 0)
  326. return i;
  327. port->tx_in = port->tx_out = 0;
  328. for (i = 0; i < TX_BUFFERS; i++)
  329. get_status(port)->tx_descs[i].stat = PACKET_EMPTY;
  330. /* signal the card */
  331. writel(1 << (DOORBELL_TO_CARD_OPEN_0 + port->node), dbr);
  332. timeout = jiffies + HZ;
  333. do {
  334. if (get_status(port)->open) {
  335. netif_start_queue(dev);
  336. return 0;
  337. }
  338. } while (time_after(timeout, jiffies));
  339. netdev_err(dev, "unable to open port\n");
  340. /* ask the card to close the port, should it be still alive */
  341. writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node), dbr);
  342. return -EFAULT;
  343. }
  344. static int wanxl_close(struct net_device *dev)
  345. {
  346. struct port *port = dev_to_port(dev);
  347. unsigned long timeout;
  348. int i;
  349. hdlc_close(dev);
  350. /* signal the card */
  351. writel(1 << (DOORBELL_TO_CARD_CLOSE_0 + port->node),
  352. port->card->plx + PLX_DOORBELL_TO_CARD);
  353. timeout = jiffies + HZ;
  354. do {
  355. if (!get_status(port)->open)
  356. break;
  357. } while (time_after(timeout, jiffies));
  358. if (get_status(port)->open)
  359. netdev_err(dev, "unable to close port\n");
  360. netif_stop_queue(dev);
  361. for (i = 0; i < TX_BUFFERS; i++) {
  362. desc_t *desc = &get_status(port)->tx_descs[i];
  363. if (desc->stat != PACKET_EMPTY) {
  364. desc->stat = PACKET_EMPTY;
  365. pci_unmap_single(port->card->pdev, desc->address,
  366. port->tx_skbs[i]->len,
  367. PCI_DMA_TODEVICE);
  368. dev_kfree_skb(port->tx_skbs[i]);
  369. }
  370. }
  371. return 0;
  372. }
  373. static struct net_device_stats *wanxl_get_stats(struct net_device *dev)
  374. {
  375. struct port *port = dev_to_port(dev);
  376. dev->stats.rx_over_errors = get_status(port)->rx_overruns;
  377. dev->stats.rx_frame_errors = get_status(port)->rx_frame_errors;
  378. dev->stats.rx_errors = dev->stats.rx_over_errors +
  379. dev->stats.rx_frame_errors;
  380. return &dev->stats;
  381. }
  382. static int wanxl_puts_command(struct card *card, u32 cmd)
  383. {
  384. unsigned long timeout = jiffies + 5 * HZ;
  385. writel(cmd, card->plx + PLX_MAILBOX_1);
  386. do {
  387. if (readl(card->plx + PLX_MAILBOX_1) == 0)
  388. return 0;
  389. schedule();
  390. }while (time_after(timeout, jiffies));
  391. return -1;
  392. }
  393. static void wanxl_reset(struct card *card)
  394. {
  395. u32 old_value = readl(card->plx + PLX_CONTROL) & ~PLX_CTL_RESET;
  396. writel(0x80, card->plx + PLX_MAILBOX_0);
  397. writel(old_value | PLX_CTL_RESET, card->plx + PLX_CONTROL);
  398. readl(card->plx + PLX_CONTROL); /* wait for posted write */
  399. udelay(1);
  400. writel(old_value, card->plx + PLX_CONTROL);
  401. readl(card->plx + PLX_CONTROL); /* wait for posted write */
  402. }
  403. static void wanxl_pci_remove_one(struct pci_dev *pdev)
  404. {
  405. struct card *card = pci_get_drvdata(pdev);
  406. int i;
  407. for (i = 0; i < card->n_ports; i++) {
  408. unregister_hdlc_device(card->ports[i].dev);
  409. free_netdev(card->ports[i].dev);
  410. }
  411. /* unregister and free all host resources */
  412. if (card->irq)
  413. free_irq(card->irq, card);
  414. wanxl_reset(card);
  415. for (i = 0; i < RX_QUEUE_LENGTH; i++)
  416. if (card->rx_skbs[i]) {
  417. pci_unmap_single(card->pdev,
  418. card->status->rx_descs[i].address,
  419. BUFFER_LENGTH, PCI_DMA_FROMDEVICE);
  420. dev_kfree_skb(card->rx_skbs[i]);
  421. }
  422. if (card->plx)
  423. iounmap(card->plx);
  424. if (card->status)
  425. pci_free_consistent(pdev, sizeof(struct card_status),
  426. card->status, card->status_address);
  427. pci_release_regions(pdev);
  428. pci_disable_device(pdev);
  429. kfree(card);
  430. }
  431. #include "wanxlfw.inc"
  432. static const struct net_device_ops wanxl_ops = {
  433. .ndo_open = wanxl_open,
  434. .ndo_stop = wanxl_close,
  435. .ndo_change_mtu = hdlc_change_mtu,
  436. .ndo_start_xmit = hdlc_start_xmit,
  437. .ndo_do_ioctl = wanxl_ioctl,
  438. .ndo_get_stats = wanxl_get_stats,
  439. };
  440. static int wanxl_pci_init_one(struct pci_dev *pdev,
  441. const struct pci_device_id *ent)
  442. {
  443. struct card *card;
  444. u32 ramsize, stat;
  445. unsigned long timeout;
  446. u32 plx_phy; /* PLX PCI base address */
  447. u32 mem_phy; /* memory PCI base addr */
  448. u8 __iomem *mem; /* memory virtual base addr */
  449. int i, ports, alloc_size;
  450. #ifndef MODULE
  451. pr_info_once("%s\n", version);
  452. #endif
  453. i = pci_enable_device(pdev);
  454. if (i)
  455. return i;
  456. /* QUICC can only access first 256 MB of host RAM directly,
  457. but PLX9060 DMA does 32-bits for actual packet data transfers */
  458. /* FIXME when PCI/DMA subsystems are fixed.
  459. We set both dma_mask and consistent_dma_mask to 28 bits
  460. and pray pci_alloc_consistent() will use this info. It should
  461. work on most platforms */
  462. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(28)) ||
  463. pci_set_dma_mask(pdev, DMA_BIT_MASK(28))) {
  464. pr_err("No usable DMA configuration\n");
  465. pci_disable_device(pdev);
  466. return -EIO;
  467. }
  468. i = pci_request_regions(pdev, "wanXL");
  469. if (i) {
  470. pci_disable_device(pdev);
  471. return i;
  472. }
  473. switch (pdev->device) {
  474. case PCI_DEVICE_ID_SBE_WANXL100: ports = 1; break;
  475. case PCI_DEVICE_ID_SBE_WANXL200: ports = 2; break;
  476. default: ports = 4;
  477. }
  478. alloc_size = sizeof(struct card) + ports * sizeof(struct port);
  479. card = kzalloc(alloc_size, GFP_KERNEL);
  480. if (card == NULL) {
  481. pci_release_regions(pdev);
  482. pci_disable_device(pdev);
  483. return -ENOBUFS;
  484. }
  485. pci_set_drvdata(pdev, card);
  486. card->pdev = pdev;
  487. card->status = pci_alloc_consistent(pdev,
  488. sizeof(struct card_status),
  489. &card->status_address);
  490. if (card->status == NULL) {
  491. wanxl_pci_remove_one(pdev);
  492. return -ENOBUFS;
  493. }
  494. #ifdef DEBUG_PCI
  495. printk(KERN_DEBUG "wanXL %s: pci_alloc_consistent() returned memory"
  496. " at 0x%LX\n", pci_name(pdev),
  497. (unsigned long long)card->status_address);
  498. #endif
  499. /* FIXME when PCI/DMA subsystems are fixed.
  500. We set both dma_mask and consistent_dma_mask back to 32 bits
  501. to indicate the card can do 32-bit DMA addressing */
  502. if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) ||
  503. pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
  504. pr_err("No usable DMA configuration\n");
  505. wanxl_pci_remove_one(pdev);
  506. return -EIO;
  507. }
  508. /* set up PLX mapping */
  509. plx_phy = pci_resource_start(pdev, 0);
  510. card->plx = ioremap_nocache(plx_phy, 0x70);
  511. if (!card->plx) {
  512. pr_err("ioremap() failed\n");
  513. wanxl_pci_remove_one(pdev);
  514. return -EFAULT;
  515. }
  516. #if RESET_WHILE_LOADING
  517. wanxl_reset(card);
  518. #endif
  519. timeout = jiffies + 20 * HZ;
  520. while ((stat = readl(card->plx + PLX_MAILBOX_0)) != 0) {
  521. if (time_before(timeout, jiffies)) {
  522. pr_warn("%s: timeout waiting for PUTS to complete\n",
  523. pci_name(pdev));
  524. wanxl_pci_remove_one(pdev);
  525. return -ENODEV;
  526. }
  527. switch(stat & 0xC0) {
  528. case 0x00: /* hmm - PUTS completed with non-zero code? */
  529. case 0x80: /* PUTS still testing the hardware */
  530. break;
  531. default:
  532. pr_warn("%s: PUTS test 0x%X failed\n",
  533. pci_name(pdev), stat & 0x30);
  534. wanxl_pci_remove_one(pdev);
  535. return -ENODEV;
  536. }
  537. schedule();
  538. }
  539. /* get on-board memory size (PUTS detects no more than 4 MB) */
  540. ramsize = readl(card->plx + PLX_MAILBOX_2) & MBX2_MEMSZ_MASK;
  541. /* set up on-board RAM mapping */
  542. mem_phy = pci_resource_start(pdev, 2);
  543. /* sanity check the board's reported memory size */
  544. if (ramsize < BUFFERS_ADDR +
  545. (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports) {
  546. pr_warn("%s: no enough on-board RAM (%u bytes detected, %u bytes required)\n",
  547. pci_name(pdev), ramsize,
  548. BUFFERS_ADDR +
  549. (TX_BUFFERS + RX_BUFFERS) * BUFFER_LENGTH * ports);
  550. wanxl_pci_remove_one(pdev);
  551. return -ENODEV;
  552. }
  553. if (wanxl_puts_command(card, MBX1_CMD_BSWAP)) {
  554. pr_warn("%s: unable to Set Byte Swap Mode\n", pci_name(pdev));
  555. wanxl_pci_remove_one(pdev);
  556. return -ENODEV;
  557. }
  558. for (i = 0; i < RX_QUEUE_LENGTH; i++) {
  559. struct sk_buff *skb = dev_alloc_skb(BUFFER_LENGTH);
  560. card->rx_skbs[i] = skb;
  561. if (skb)
  562. card->status->rx_descs[i].address =
  563. pci_map_single(card->pdev, skb->data,
  564. BUFFER_LENGTH,
  565. PCI_DMA_FROMDEVICE);
  566. }
  567. mem = ioremap_nocache(mem_phy, PDM_OFFSET + sizeof(firmware));
  568. if (!mem) {
  569. pr_err("ioremap() failed\n");
  570. wanxl_pci_remove_one(pdev);
  571. return -EFAULT;
  572. }
  573. for (i = 0; i < sizeof(firmware); i += 4)
  574. writel(ntohl(*(__be32*)(firmware + i)), mem + PDM_OFFSET + i);
  575. for (i = 0; i < ports; i++)
  576. writel(card->status_address +
  577. (void *)&card->status->port_status[i] -
  578. (void *)card->status, mem + PDM_OFFSET + 4 + i * 4);
  579. writel(card->status_address, mem + PDM_OFFSET + 20);
  580. writel(PDM_OFFSET, mem);
  581. iounmap(mem);
  582. writel(0, card->plx + PLX_MAILBOX_5);
  583. if (wanxl_puts_command(card, MBX1_CMD_ABORTJ)) {
  584. pr_warn("%s: unable to Abort and Jump\n", pci_name(pdev));
  585. wanxl_pci_remove_one(pdev);
  586. return -ENODEV;
  587. }
  588. stat = 0;
  589. timeout = jiffies + 5 * HZ;
  590. do {
  591. if ((stat = readl(card->plx + PLX_MAILBOX_5)) != 0)
  592. break;
  593. schedule();
  594. }while (time_after(timeout, jiffies));
  595. if (!stat) {
  596. pr_warn("%s: timeout while initializing card firmware\n",
  597. pci_name(pdev));
  598. wanxl_pci_remove_one(pdev);
  599. return -ENODEV;
  600. }
  601. #if DETECT_RAM
  602. ramsize = stat;
  603. #endif
  604. pr_info("%s: at 0x%X, %u KB of RAM at 0x%X, irq %u\n",
  605. pci_name(pdev), plx_phy, ramsize / 1024, mem_phy, pdev->irq);
  606. /* Allocate IRQ */
  607. if (request_irq(pdev->irq, wanxl_intr, IRQF_SHARED, "wanXL", card)) {
  608. pr_warn("%s: could not allocate IRQ%i\n",
  609. pci_name(pdev), pdev->irq);
  610. wanxl_pci_remove_one(pdev);
  611. return -EBUSY;
  612. }
  613. card->irq = pdev->irq;
  614. for (i = 0; i < ports; i++) {
  615. hdlc_device *hdlc;
  616. struct port *port = &card->ports[i];
  617. struct net_device *dev = alloc_hdlcdev(port);
  618. if (!dev) {
  619. pr_err("%s: unable to allocate memory\n",
  620. pci_name(pdev));
  621. wanxl_pci_remove_one(pdev);
  622. return -ENOMEM;
  623. }
  624. port->dev = dev;
  625. hdlc = dev_to_hdlc(dev);
  626. spin_lock_init(&port->lock);
  627. dev->tx_queue_len = 50;
  628. dev->netdev_ops = &wanxl_ops;
  629. hdlc->attach = wanxl_attach;
  630. hdlc->xmit = wanxl_xmit;
  631. port->card = card;
  632. port->node = i;
  633. get_status(port)->clocking = CLOCK_EXT;
  634. if (register_hdlc_device(dev)) {
  635. pr_err("%s: unable to register hdlc device\n",
  636. pci_name(pdev));
  637. free_netdev(dev);
  638. wanxl_pci_remove_one(pdev);
  639. return -ENOBUFS;
  640. }
  641. card->n_ports++;
  642. }
  643. pr_info("%s: port", pci_name(pdev));
  644. for (i = 0; i < ports; i++)
  645. pr_cont("%s #%i: %s",
  646. i ? "," : "", i, card->ports[i].dev->name);
  647. pr_cont("\n");
  648. for (i = 0; i < ports; i++)
  649. wanxl_cable_intr(&card->ports[i]); /* get carrier status etc.*/
  650. return 0;
  651. }
  652. static const struct pci_device_id wanxl_pci_tbl[] = {
  653. { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL100, PCI_ANY_ID,
  654. PCI_ANY_ID, 0, 0, 0 },
  655. { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL200, PCI_ANY_ID,
  656. PCI_ANY_ID, 0, 0, 0 },
  657. { PCI_VENDOR_ID_SBE, PCI_DEVICE_ID_SBE_WANXL400, PCI_ANY_ID,
  658. PCI_ANY_ID, 0, 0, 0 },
  659. { 0, }
  660. };
  661. static struct pci_driver wanxl_pci_driver = {
  662. .name = "wanXL",
  663. .id_table = wanxl_pci_tbl,
  664. .probe = wanxl_pci_init_one,
  665. .remove = wanxl_pci_remove_one,
  666. };
  667. static int __init wanxl_init_module(void)
  668. {
  669. #ifdef MODULE
  670. pr_info("%s\n", version);
  671. #endif
  672. return pci_register_driver(&wanxl_pci_driver);
  673. }
  674. static void __exit wanxl_cleanup_module(void)
  675. {
  676. pci_unregister_driver(&wanxl_pci_driver);
  677. }
  678. MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
  679. MODULE_DESCRIPTION("SBE Inc. wanXL serial port driver");
  680. MODULE_LICENSE("GPL v2");
  681. MODULE_DEVICE_TABLE(pci, wanxl_pci_tbl);
  682. module_init(wanxl_init_module);
  683. module_exit(wanxl_cleanup_module);