islpci_mgt.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (C) 2002 Intersil Americas Inc.
  3. * Copyright 2004 Jens Maurer <Jens.Maurer@gmx.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. */
  19. #include <linux/netdevice.h>
  20. #include <linux/module.h>
  21. #include <linux/pci.h>
  22. #include <linux/sched.h>
  23. #include <linux/slab.h>
  24. #include <asm/io.h>
  25. #include <linux/if_arp.h>
  26. #include "prismcompat.h"
  27. #include "isl_38xx.h"
  28. #include "islpci_mgt.h"
  29. #include "isl_oid.h" /* additional types and defs for isl38xx fw */
  30. #include "isl_ioctl.h"
  31. #include <net/iw_handler.h>
  32. /******************************************************************************
  33. Global variable definition section
  34. ******************************************************************************/
  35. int pc_debug = VERBOSE;
  36. module_param(pc_debug, int, 0);
  37. /******************************************************************************
  38. Driver general functions
  39. ******************************************************************************/
  40. #if VERBOSE > SHOW_ERROR_MESSAGES
  41. void
  42. display_buffer(char *buffer, int length)
  43. {
  44. if ((pc_debug & SHOW_BUFFER_CONTENTS) == 0)
  45. return;
  46. while (length > 0) {
  47. printk("[%02x]", *buffer & 255);
  48. length--;
  49. buffer++;
  50. }
  51. printk("\n");
  52. }
  53. #endif
  54. /*****************************************************************************
  55. Queue handling for management frames
  56. ******************************************************************************/
  57. /*
  58. * Helper function to create a PIMFOR management frame header.
  59. */
  60. static void
  61. pimfor_encode_header(int operation, u32 oid, u32 length, pimfor_header_t *h)
  62. {
  63. h->version = PIMFOR_VERSION;
  64. h->operation = operation;
  65. h->device_id = PIMFOR_DEV_ID_MHLI_MIB;
  66. h->flags = 0;
  67. h->oid = cpu_to_be32(oid);
  68. h->length = cpu_to_be32(length);
  69. }
  70. /*
  71. * Helper function to analyze a PIMFOR management frame header.
  72. */
  73. static pimfor_header_t *
  74. pimfor_decode_header(void *data, int len)
  75. {
  76. pimfor_header_t *h = data;
  77. while ((void *) h < data + len) {
  78. if (h->flags & PIMFOR_FLAG_LITTLE_ENDIAN) {
  79. le32_to_cpus(&h->oid);
  80. le32_to_cpus(&h->length);
  81. } else {
  82. be32_to_cpus(&h->oid);
  83. be32_to_cpus(&h->length);
  84. }
  85. if (h->oid != OID_INL_TUNNEL)
  86. return h;
  87. h++;
  88. }
  89. return NULL;
  90. }
  91. /*
  92. * Fill the receive queue for management frames with fresh buffers.
  93. */
  94. int
  95. islpci_mgmt_rx_fill(struct net_device *ndev)
  96. {
  97. islpci_private *priv = netdev_priv(ndev);
  98. isl38xx_control_block *cb = /* volatile not needed */
  99. (isl38xx_control_block *) priv->control_block;
  100. u32 curr = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_RX_MGMTQ]);
  101. #if VERBOSE > SHOW_ERROR_MESSAGES
  102. DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgmt_rx_fill\n");
  103. #endif
  104. while (curr - priv->index_mgmt_rx < ISL38XX_CB_MGMT_QSIZE) {
  105. u32 index = curr % ISL38XX_CB_MGMT_QSIZE;
  106. struct islpci_membuf *buf = &priv->mgmt_rx[index];
  107. isl38xx_fragment *frag = &cb->rx_data_mgmt[index];
  108. if (buf->mem == NULL) {
  109. buf->mem = kmalloc(MGMT_FRAME_SIZE, GFP_ATOMIC);
  110. if (!buf->mem) {
  111. printk(KERN_WARNING
  112. "Error allocating management frame.\n");
  113. return -ENOMEM;
  114. }
  115. buf->size = MGMT_FRAME_SIZE;
  116. }
  117. if (buf->pci_addr == 0) {
  118. buf->pci_addr = pci_map_single(priv->pdev, buf->mem,
  119. MGMT_FRAME_SIZE,
  120. PCI_DMA_FROMDEVICE);
  121. if (!buf->pci_addr) {
  122. printk(KERN_WARNING
  123. "Failed to make memory DMA'able.\n");
  124. return -ENOMEM;
  125. }
  126. }
  127. /* be safe: always reset control block information */
  128. frag->size = cpu_to_le16(MGMT_FRAME_SIZE);
  129. frag->flags = 0;
  130. frag->address = cpu_to_le32(buf->pci_addr);
  131. curr++;
  132. /* The fragment address in the control block must have
  133. * been written before announcing the frame buffer to
  134. * device */
  135. wmb();
  136. cb->driver_curr_frag[ISL38XX_CB_RX_MGMTQ] = cpu_to_le32(curr);
  137. }
  138. return 0;
  139. }
  140. /*
  141. * Create and transmit a management frame using "operation" and "oid",
  142. * with arguments data/length.
  143. * We either return an error and free the frame, or we return 0 and
  144. * islpci_mgt_cleanup_transmit() frees the frame in the tx-done
  145. * interrupt.
  146. */
  147. static int
  148. islpci_mgt_transmit(struct net_device *ndev, int operation, unsigned long oid,
  149. void *data, int length)
  150. {
  151. islpci_private *priv = netdev_priv(ndev);
  152. isl38xx_control_block *cb =
  153. (isl38xx_control_block *) priv->control_block;
  154. void *p;
  155. int err = -EINVAL;
  156. unsigned long flags;
  157. isl38xx_fragment *frag;
  158. struct islpci_membuf buf;
  159. u32 curr_frag;
  160. int index;
  161. int frag_len = length + PIMFOR_HEADER_SIZE;
  162. #if VERBOSE > SHOW_ERROR_MESSAGES
  163. DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgt_transmit\n");
  164. #endif
  165. if (frag_len > MGMT_FRAME_SIZE) {
  166. printk(KERN_DEBUG "%s: mgmt frame too large %d\n",
  167. ndev->name, frag_len);
  168. goto error;
  169. }
  170. err = -ENOMEM;
  171. p = buf.mem = kmalloc(frag_len, GFP_KERNEL);
  172. if (!buf.mem)
  173. goto error;
  174. buf.size = frag_len;
  175. /* create the header directly in the fragment data area */
  176. pimfor_encode_header(operation, oid, length, (pimfor_header_t *) p);
  177. p += PIMFOR_HEADER_SIZE;
  178. if (data)
  179. memcpy(p, data, length);
  180. else
  181. memset(p, 0, length);
  182. #if VERBOSE > SHOW_ERROR_MESSAGES
  183. {
  184. pimfor_header_t *h = buf.mem;
  185. DEBUG(SHOW_PIMFOR_FRAMES,
  186. "PIMFOR: op %i, oid 0x%08lx, device %i, flags 0x%x length 0x%x\n",
  187. h->operation, oid, h->device_id, h->flags, length);
  188. /* display the buffer contents for debugging */
  189. display_buffer((char *) h, sizeof (pimfor_header_t));
  190. display_buffer(p, length);
  191. }
  192. #endif
  193. err = -ENOMEM;
  194. buf.pci_addr = pci_map_single(priv->pdev, buf.mem, frag_len,
  195. PCI_DMA_TODEVICE);
  196. if (!buf.pci_addr) {
  197. printk(KERN_WARNING "%s: cannot map PCI memory for mgmt\n",
  198. ndev->name);
  199. goto error_free;
  200. }
  201. /* Protect the control block modifications against interrupts. */
  202. spin_lock_irqsave(&priv->slock, flags);
  203. curr_frag = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_TX_MGMTQ]);
  204. if (curr_frag - priv->index_mgmt_tx >= ISL38XX_CB_MGMT_QSIZE) {
  205. printk(KERN_WARNING "%s: mgmt tx queue is still full\n",
  206. ndev->name);
  207. goto error_unlock;
  208. }
  209. /* commit the frame to the tx device queue */
  210. index = curr_frag % ISL38XX_CB_MGMT_QSIZE;
  211. priv->mgmt_tx[index] = buf;
  212. frag = &cb->tx_data_mgmt[index];
  213. frag->size = cpu_to_le16(frag_len);
  214. frag->flags = 0; /* for any other than the last fragment, set to 1 */
  215. frag->address = cpu_to_le32(buf.pci_addr);
  216. /* The fragment address in the control block must have
  217. * been written before announcing the frame buffer to
  218. * device */
  219. wmb();
  220. cb->driver_curr_frag[ISL38XX_CB_TX_MGMTQ] = cpu_to_le32(curr_frag + 1);
  221. spin_unlock_irqrestore(&priv->slock, flags);
  222. /* trigger the device */
  223. islpci_trigger(priv);
  224. return 0;
  225. error_unlock:
  226. spin_unlock_irqrestore(&priv->slock, flags);
  227. error_free:
  228. kfree(buf.mem);
  229. error:
  230. return err;
  231. }
  232. /*
  233. * Receive a management frame from the device.
  234. * This can be an arbitrary number of traps, and at most one response
  235. * frame for a previous request sent via islpci_mgt_transmit().
  236. */
  237. int
  238. islpci_mgt_receive(struct net_device *ndev)
  239. {
  240. islpci_private *priv = netdev_priv(ndev);
  241. isl38xx_control_block *cb =
  242. (isl38xx_control_block *) priv->control_block;
  243. u32 curr_frag;
  244. #if VERBOSE > SHOW_ERROR_MESSAGES
  245. DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgt_receive\n");
  246. #endif
  247. /* Only once per interrupt, determine fragment range to
  248. * process. This avoids an endless loop (i.e. lockup) if
  249. * frames come in faster than we can process them. */
  250. curr_frag = le32_to_cpu(cb->device_curr_frag[ISL38XX_CB_RX_MGMTQ]);
  251. barrier();
  252. for (; priv->index_mgmt_rx < curr_frag; priv->index_mgmt_rx++) {
  253. pimfor_header_t *header;
  254. u32 index = priv->index_mgmt_rx % ISL38XX_CB_MGMT_QSIZE;
  255. struct islpci_membuf *buf = &priv->mgmt_rx[index];
  256. u16 frag_len;
  257. int size;
  258. struct islpci_mgmtframe *frame;
  259. /* I have no idea (and no documentation) if flags != 0
  260. * is possible. Drop the frame, reuse the buffer. */
  261. if (le16_to_cpu(cb->rx_data_mgmt[index].flags) != 0) {
  262. printk(KERN_WARNING "%s: unknown flags 0x%04x\n",
  263. ndev->name,
  264. le16_to_cpu(cb->rx_data_mgmt[index].flags));
  265. continue;
  266. }
  267. /* The device only returns the size of the header(s) here. */
  268. frag_len = le16_to_cpu(cb->rx_data_mgmt[index].size);
  269. /*
  270. * We appear to have no way to tell the device the
  271. * size of a receive buffer. Thus, if this check
  272. * triggers, we likely have kernel heap corruption. */
  273. if (frag_len > MGMT_FRAME_SIZE) {
  274. printk(KERN_WARNING
  275. "%s: Bogus packet size of %d (%#x).\n",
  276. ndev->name, frag_len, frag_len);
  277. frag_len = MGMT_FRAME_SIZE;
  278. }
  279. /* Ensure the results of device DMA are visible to the CPU. */
  280. pci_dma_sync_single_for_cpu(priv->pdev, buf->pci_addr,
  281. buf->size, PCI_DMA_FROMDEVICE);
  282. /* Perform endianess conversion for PIMFOR header in-place. */
  283. header = pimfor_decode_header(buf->mem, frag_len);
  284. if (!header) {
  285. printk(KERN_WARNING "%s: no PIMFOR header found\n",
  286. ndev->name);
  287. continue;
  288. }
  289. /* The device ID from the PIMFOR packet received from
  290. * the MVC is always 0. We forward a sensible device_id.
  291. * Not that anyone upstream would care... */
  292. header->device_id = priv->ndev->ifindex;
  293. #if VERBOSE > SHOW_ERROR_MESSAGES
  294. DEBUG(SHOW_PIMFOR_FRAMES,
  295. "PIMFOR: op %i, oid 0x%08x, device %i, flags 0x%x length 0x%x\n",
  296. header->operation, header->oid, header->device_id,
  297. header->flags, header->length);
  298. /* display the buffer contents for debugging */
  299. display_buffer((char *) header, PIMFOR_HEADER_SIZE);
  300. display_buffer((char *) header + PIMFOR_HEADER_SIZE,
  301. header->length);
  302. #endif
  303. /* nobody sends these */
  304. if (header->flags & PIMFOR_FLAG_APPLIC_ORIGIN) {
  305. printk(KERN_DEBUG
  306. "%s: errant PIMFOR application frame\n",
  307. ndev->name);
  308. continue;
  309. }
  310. /* Determine frame size, skipping OID_INL_TUNNEL headers. */
  311. size = PIMFOR_HEADER_SIZE + header->length;
  312. frame = kmalloc(sizeof (struct islpci_mgmtframe) + size,
  313. GFP_ATOMIC);
  314. if (!frame) {
  315. printk(KERN_WARNING
  316. "%s: Out of memory, cannot handle oid 0x%08x\n",
  317. ndev->name, header->oid);
  318. continue;
  319. }
  320. frame->ndev = ndev;
  321. memcpy(&frame->buf, header, size);
  322. frame->header = (pimfor_header_t *) frame->buf;
  323. frame->data = frame->buf + PIMFOR_HEADER_SIZE;
  324. #if VERBOSE > SHOW_ERROR_MESSAGES
  325. DEBUG(SHOW_PIMFOR_FRAMES,
  326. "frame: header: %p, data: %p, size: %d\n",
  327. frame->header, frame->data, size);
  328. #endif
  329. if (header->operation == PIMFOR_OP_TRAP) {
  330. #if VERBOSE > SHOW_ERROR_MESSAGES
  331. printk(KERN_DEBUG
  332. "TRAP: oid 0x%x, device %i, flags 0x%x length %i\n",
  333. header->oid, header->device_id, header->flags,
  334. header->length);
  335. #endif
  336. /* Create work to handle trap out of interrupt
  337. * context. */
  338. INIT_WORK(&frame->ws, prism54_process_trap);
  339. schedule_work(&frame->ws);
  340. } else {
  341. /* Signal the one waiting process that a response
  342. * has been received. */
  343. if ((frame = xchg(&priv->mgmt_received, frame)) != NULL) {
  344. printk(KERN_WARNING
  345. "%s: mgmt response not collected\n",
  346. ndev->name);
  347. kfree(frame);
  348. }
  349. #if VERBOSE > SHOW_ERROR_MESSAGES
  350. DEBUG(SHOW_TRACING, "Wake up Mgmt Queue\n");
  351. #endif
  352. wake_up(&priv->mgmt_wqueue);
  353. }
  354. }
  355. return 0;
  356. }
  357. /*
  358. * Cleanup the transmit queue by freeing all frames handled by the device.
  359. */
  360. void
  361. islpci_mgt_cleanup_transmit(struct net_device *ndev)
  362. {
  363. islpci_private *priv = netdev_priv(ndev);
  364. isl38xx_control_block *cb = /* volatile not needed */
  365. (isl38xx_control_block *) priv->control_block;
  366. u32 curr_frag;
  367. #if VERBOSE > SHOW_ERROR_MESSAGES
  368. DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgt_cleanup_transmit\n");
  369. #endif
  370. /* Only once per cleanup, determine fragment range to
  371. * process. This avoids an endless loop (i.e. lockup) if
  372. * the device became confused, incrementing device_curr_frag
  373. * rapidly. */
  374. curr_frag = le32_to_cpu(cb->device_curr_frag[ISL38XX_CB_TX_MGMTQ]);
  375. barrier();
  376. for (; priv->index_mgmt_tx < curr_frag; priv->index_mgmt_tx++) {
  377. int index = priv->index_mgmt_tx % ISL38XX_CB_MGMT_QSIZE;
  378. struct islpci_membuf *buf = &priv->mgmt_tx[index];
  379. pci_unmap_single(priv->pdev, buf->pci_addr, buf->size,
  380. PCI_DMA_TODEVICE);
  381. buf->pci_addr = 0;
  382. kfree(buf->mem);
  383. buf->mem = NULL;
  384. buf->size = 0;
  385. }
  386. }
  387. /*
  388. * Perform one request-response transaction to the device.
  389. */
  390. int
  391. islpci_mgt_transaction(struct net_device *ndev,
  392. int operation, unsigned long oid,
  393. void *senddata, int sendlen,
  394. struct islpci_mgmtframe **recvframe)
  395. {
  396. islpci_private *priv = netdev_priv(ndev);
  397. const long wait_cycle_jiffies = msecs_to_jiffies(ISL38XX_WAIT_CYCLE * 10);
  398. long timeout_left = ISL38XX_MAX_WAIT_CYCLES * wait_cycle_jiffies;
  399. int err;
  400. DEFINE_WAIT(wait);
  401. *recvframe = NULL;
  402. if (mutex_lock_interruptible(&priv->mgmt_lock))
  403. return -ERESTARTSYS;
  404. prepare_to_wait(&priv->mgmt_wqueue, &wait, TASK_UNINTERRUPTIBLE);
  405. err = islpci_mgt_transmit(ndev, operation, oid, senddata, sendlen);
  406. if (err)
  407. goto out;
  408. err = -ETIMEDOUT;
  409. while (timeout_left > 0) {
  410. int timeleft;
  411. struct islpci_mgmtframe *frame;
  412. timeleft = schedule_timeout_uninterruptible(wait_cycle_jiffies);
  413. frame = xchg(&priv->mgmt_received, NULL);
  414. if (frame) {
  415. if (frame->header->oid == oid) {
  416. *recvframe = frame;
  417. err = 0;
  418. goto out;
  419. } else {
  420. printk(KERN_DEBUG
  421. "%s: expecting oid 0x%x, received 0x%x.\n",
  422. ndev->name, (unsigned int) oid,
  423. frame->header->oid);
  424. kfree(frame);
  425. frame = NULL;
  426. }
  427. }
  428. if (timeleft == 0) {
  429. printk(KERN_DEBUG
  430. "%s: timeout waiting for mgmt response %lu, "
  431. "triggering device\n",
  432. ndev->name, timeout_left);
  433. islpci_trigger(priv);
  434. }
  435. timeout_left += timeleft - wait_cycle_jiffies;
  436. }
  437. printk(KERN_WARNING "%s: timeout waiting for mgmt response\n",
  438. ndev->name);
  439. /* TODO: we should reset the device here */
  440. out:
  441. finish_wait(&priv->mgmt_wqueue, &wait);
  442. mutex_unlock(&priv->mgmt_lock);
  443. return err;
  444. }