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