2 * Copyright (C) 2002 Intersil Americas Inc.
3 * Copyright 2004 Jens Maurer <Jens.Maurer@gmx.net>
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
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.
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/>.
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>
26 #include <linux/if_arp.h>
28 #include "prismcompat.h"
30 #include "islpci_mgt.h"
31 #include "isl_oid.h" /* additional types and defs for isl38xx fw */
32 #include "isl_ioctl.h"
34 #include <net/iw_handler.h>
36 /******************************************************************************
37 Global variable definition section
38 ******************************************************************************/
39 int pc_debug = VERBOSE;
40 module_param(pc_debug, int, 0);
42 /******************************************************************************
43 Driver general functions
44 ******************************************************************************/
45 #if VERBOSE > SHOW_ERROR_MESSAGES
47 display_buffer(char *buffer, int length)
49 if ((pc_debug & SHOW_BUFFER_CONTENTS) == 0)
53 printk("[%02x]", *buffer & 255);
62 /*****************************************************************************
63 Queue handling for management frames
64 ******************************************************************************/
67 * Helper function to create a PIMFOR management frame header.
70 pimfor_encode_header(int operation, u32 oid, u32 length, pimfor_header_t *h)
72 h->version = PIMFOR_VERSION;
73 h->operation = operation;
74 h->device_id = PIMFOR_DEV_ID_MHLI_MIB;
76 h->oid = cpu_to_be32(oid);
77 h->length = cpu_to_be32(length);
81 * Helper function to analyze a PIMFOR management frame header.
83 static pimfor_header_t *
84 pimfor_decode_header(void *data, int len)
86 pimfor_header_t *h = data;
88 while ((void *) h < data + len) {
89 if (h->flags & PIMFOR_FLAG_LITTLE_ENDIAN) {
90 le32_to_cpus(&h->oid);
91 le32_to_cpus(&h->length);
93 be32_to_cpus(&h->oid);
94 be32_to_cpus(&h->length);
96 if (h->oid != OID_INL_TUNNEL)
104 * Fill the receive queue for management frames with fresh buffers.
107 islpci_mgmt_rx_fill(struct net_device *ndev)
109 islpci_private *priv = netdev_priv(ndev);
110 isl38xx_control_block *cb = /* volatile not needed */
111 (isl38xx_control_block *) priv->control_block;
112 u32 curr = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_RX_MGMTQ]);
114 #if VERBOSE > SHOW_ERROR_MESSAGES
115 DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgmt_rx_fill\n");
118 while (curr - priv->index_mgmt_rx < ISL38XX_CB_MGMT_QSIZE) {
119 u32 index = curr % ISL38XX_CB_MGMT_QSIZE;
120 struct islpci_membuf *buf = &priv->mgmt_rx[index];
121 isl38xx_fragment *frag = &cb->rx_data_mgmt[index];
123 if (buf->mem == NULL) {
124 buf->mem = kmalloc(MGMT_FRAME_SIZE, GFP_ATOMIC);
127 buf->size = MGMT_FRAME_SIZE;
129 if (buf->pci_addr == 0) {
130 buf->pci_addr = pci_map_single(priv->pdev, buf->mem,
133 if (pci_dma_mapping_error(priv->pdev, buf->pci_addr)) {
135 "Failed to make memory DMA'able.\n");
140 /* be safe: always reset control block information */
141 frag->size = cpu_to_le16(MGMT_FRAME_SIZE);
143 frag->address = cpu_to_le32(buf->pci_addr);
146 /* The fragment address in the control block must have
147 * been written before announcing the frame buffer to
150 cb->driver_curr_frag[ISL38XX_CB_RX_MGMTQ] = cpu_to_le32(curr);
156 * Create and transmit a management frame using "operation" and "oid",
157 * with arguments data/length.
158 * We either return an error and free the frame, or we return 0 and
159 * islpci_mgt_cleanup_transmit() frees the frame in the tx-done
163 islpci_mgt_transmit(struct net_device *ndev, int operation, unsigned long oid,
164 void *data, int length)
166 islpci_private *priv = netdev_priv(ndev);
167 isl38xx_control_block *cb =
168 (isl38xx_control_block *) priv->control_block;
172 isl38xx_fragment *frag;
173 struct islpci_membuf buf;
176 int frag_len = length + PIMFOR_HEADER_SIZE;
178 #if VERBOSE > SHOW_ERROR_MESSAGES
179 DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgt_transmit\n");
182 if (frag_len > MGMT_FRAME_SIZE) {
183 printk(KERN_DEBUG "%s: mgmt frame too large %d\n",
184 ndev->name, frag_len);
189 p = buf.mem = kmalloc(frag_len, GFP_KERNEL);
195 /* create the header directly in the fragment data area */
196 pimfor_encode_header(operation, oid, length, (pimfor_header_t *) p);
197 p += PIMFOR_HEADER_SIZE;
200 memcpy(p, data, length);
202 memset(p, 0, length);
204 #if VERBOSE > SHOW_ERROR_MESSAGES
206 pimfor_header_t *h = buf.mem;
207 DEBUG(SHOW_PIMFOR_FRAMES,
208 "PIMFOR: op %i, oid 0x%08lx, device %i, flags 0x%x length 0x%x\n",
209 h->operation, oid, h->device_id, h->flags, length);
211 /* display the buffer contents for debugging */
212 display_buffer((char *) h, sizeof (pimfor_header_t));
213 display_buffer(p, length);
218 buf.pci_addr = pci_map_single(priv->pdev, buf.mem, frag_len,
220 if (pci_dma_mapping_error(priv->pdev, buf.pci_addr)) {
221 printk(KERN_WARNING "%s: cannot map PCI memory for mgmt\n",
226 /* Protect the control block modifications against interrupts. */
227 spin_lock_irqsave(&priv->slock, flags);
228 curr_frag = le32_to_cpu(cb->driver_curr_frag[ISL38XX_CB_TX_MGMTQ]);
229 if (curr_frag - priv->index_mgmt_tx >= ISL38XX_CB_MGMT_QSIZE) {
230 printk(KERN_WARNING "%s: mgmt tx queue is still full\n",
235 /* commit the frame to the tx device queue */
236 index = curr_frag % ISL38XX_CB_MGMT_QSIZE;
237 priv->mgmt_tx[index] = buf;
238 frag = &cb->tx_data_mgmt[index];
239 frag->size = cpu_to_le16(frag_len);
240 frag->flags = 0; /* for any other than the last fragment, set to 1 */
241 frag->address = cpu_to_le32(buf.pci_addr);
243 /* The fragment address in the control block must have
244 * been written before announcing the frame buffer to
247 cb->driver_curr_frag[ISL38XX_CB_TX_MGMTQ] = cpu_to_le32(curr_frag + 1);
248 spin_unlock_irqrestore(&priv->slock, flags);
250 /* trigger the device */
251 islpci_trigger(priv);
255 spin_unlock_irqrestore(&priv->slock, flags);
263 * Receive a management frame from the device.
264 * This can be an arbitrary number of traps, and at most one response
265 * frame for a previous request sent via islpci_mgt_transmit().
268 islpci_mgt_receive(struct net_device *ndev)
270 islpci_private *priv = netdev_priv(ndev);
271 isl38xx_control_block *cb =
272 (isl38xx_control_block *) priv->control_block;
275 #if VERBOSE > SHOW_ERROR_MESSAGES
276 DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgt_receive\n");
279 /* Only once per interrupt, determine fragment range to
280 * process. This avoids an endless loop (i.e. lockup) if
281 * frames come in faster than we can process them. */
282 curr_frag = le32_to_cpu(cb->device_curr_frag[ISL38XX_CB_RX_MGMTQ]);
285 for (; priv->index_mgmt_rx < curr_frag; priv->index_mgmt_rx++) {
286 pimfor_header_t *header;
287 u32 index = priv->index_mgmt_rx % ISL38XX_CB_MGMT_QSIZE;
288 struct islpci_membuf *buf = &priv->mgmt_rx[index];
291 struct islpci_mgmtframe *frame;
293 /* I have no idea (and no documentation) if flags != 0
294 * is possible. Drop the frame, reuse the buffer. */
295 if (le16_to_cpu(cb->rx_data_mgmt[index].flags) != 0) {
296 printk(KERN_WARNING "%s: unknown flags 0x%04x\n",
298 le16_to_cpu(cb->rx_data_mgmt[index].flags));
302 /* The device only returns the size of the header(s) here. */
303 frag_len = le16_to_cpu(cb->rx_data_mgmt[index].size);
306 * We appear to have no way to tell the device the
307 * size of a receive buffer. Thus, if this check
308 * triggers, we likely have kernel heap corruption. */
309 if (frag_len > MGMT_FRAME_SIZE) {
311 "%s: Bogus packet size of %d (%#x).\n",
312 ndev->name, frag_len, frag_len);
313 frag_len = MGMT_FRAME_SIZE;
316 /* Ensure the results of device DMA are visible to the CPU. */
317 pci_dma_sync_single_for_cpu(priv->pdev, buf->pci_addr,
318 buf->size, PCI_DMA_FROMDEVICE);
320 /* Perform endianess conversion for PIMFOR header in-place. */
321 header = pimfor_decode_header(buf->mem, frag_len);
323 printk(KERN_WARNING "%s: no PIMFOR header found\n",
328 /* The device ID from the PIMFOR packet received from
329 * the MVC is always 0. We forward a sensible device_id.
330 * Not that anyone upstream would care... */
331 header->device_id = priv->ndev->ifindex;
333 #if VERBOSE > SHOW_ERROR_MESSAGES
334 DEBUG(SHOW_PIMFOR_FRAMES,
335 "PIMFOR: op %i, oid 0x%08x, device %i, flags 0x%x length 0x%x\n",
336 header->operation, header->oid, header->device_id,
337 header->flags, header->length);
339 /* display the buffer contents for debugging */
340 display_buffer((char *) header, PIMFOR_HEADER_SIZE);
341 display_buffer((char *) header + PIMFOR_HEADER_SIZE,
345 /* nobody sends these */
346 if (header->flags & PIMFOR_FLAG_APPLIC_ORIGIN) {
348 "%s: errant PIMFOR application frame\n",
353 /* Determine frame size, skipping OID_INL_TUNNEL headers. */
354 size = PIMFOR_HEADER_SIZE + header->length;
355 frame = kmalloc(sizeof(struct islpci_mgmtframe) + size,
361 memcpy(&frame->buf, header, size);
362 frame->header = (pimfor_header_t *) frame->buf;
363 frame->data = frame->buf + PIMFOR_HEADER_SIZE;
365 #if VERBOSE > SHOW_ERROR_MESSAGES
366 DEBUG(SHOW_PIMFOR_FRAMES,
367 "frame: header: %p, data: %p, size: %d\n",
368 frame->header, frame->data, size);
371 if (header->operation == PIMFOR_OP_TRAP) {
372 #if VERBOSE > SHOW_ERROR_MESSAGES
374 "TRAP: oid 0x%x, device %i, flags 0x%x length %i\n",
375 header->oid, header->device_id, header->flags,
379 /* Create work to handle trap out of interrupt
381 INIT_WORK(&frame->ws, prism54_process_trap);
382 schedule_work(&frame->ws);
385 /* Signal the one waiting process that a response
386 * has been received. */
387 if ((frame = xchg(&priv->mgmt_received, frame)) != NULL) {
389 "%s: mgmt response not collected\n",
393 #if VERBOSE > SHOW_ERROR_MESSAGES
394 DEBUG(SHOW_TRACING, "Wake up Mgmt Queue\n");
396 wake_up(&priv->mgmt_wqueue);
405 * Cleanup the transmit queue by freeing all frames handled by the device.
408 islpci_mgt_cleanup_transmit(struct net_device *ndev)
410 islpci_private *priv = netdev_priv(ndev);
411 isl38xx_control_block *cb = /* volatile not needed */
412 (isl38xx_control_block *) priv->control_block;
415 #if VERBOSE > SHOW_ERROR_MESSAGES
416 DEBUG(SHOW_FUNCTION_CALLS, "islpci_mgt_cleanup_transmit\n");
419 /* Only once per cleanup, determine fragment range to
420 * process. This avoids an endless loop (i.e. lockup) if
421 * the device became confused, incrementing device_curr_frag
423 curr_frag = le32_to_cpu(cb->device_curr_frag[ISL38XX_CB_TX_MGMTQ]);
426 for (; priv->index_mgmt_tx < curr_frag; priv->index_mgmt_tx++) {
427 int index = priv->index_mgmt_tx % ISL38XX_CB_MGMT_QSIZE;
428 struct islpci_membuf *buf = &priv->mgmt_tx[index];
429 pci_unmap_single(priv->pdev, buf->pci_addr, buf->size,
439 * Perform one request-response transaction to the device.
442 islpci_mgt_transaction(struct net_device *ndev,
443 int operation, unsigned long oid,
444 void *senddata, int sendlen,
445 struct islpci_mgmtframe **recvframe)
447 islpci_private *priv = netdev_priv(ndev);
448 const long wait_cycle_jiffies = msecs_to_jiffies(ISL38XX_WAIT_CYCLE * 10);
449 long timeout_left = ISL38XX_MAX_WAIT_CYCLES * wait_cycle_jiffies;
455 if (mutex_lock_interruptible(&priv->mgmt_lock))
458 prepare_to_wait(&priv->mgmt_wqueue, &wait, TASK_UNINTERRUPTIBLE);
459 err = islpci_mgt_transmit(ndev, operation, oid, senddata, sendlen);
464 while (timeout_left > 0) {
466 struct islpci_mgmtframe *frame;
468 timeleft = schedule_timeout_uninterruptible(wait_cycle_jiffies);
469 frame = xchg(&priv->mgmt_received, NULL);
471 if (frame->header->oid == oid) {
477 "%s: expecting oid 0x%x, received 0x%x.\n",
478 ndev->name, (unsigned int) oid,
486 "%s: timeout waiting for mgmt response %lu, "
487 "triggering device\n",
488 ndev->name, timeout_left);
489 islpci_trigger(priv);
491 timeout_left += timeleft - wait_cycle_jiffies;
493 printk(KERN_WARNING "%s: timeout waiting for mgmt response\n",
496 /* TODO: we should reset the device here */
498 finish_wait(&priv->mgmt_wqueue, &wait);
499 mutex_unlock(&priv->mgmt_lock);