islpci_mgt.c 14 KB

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