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