]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/cw1200/wsm.c
cw1200: handle allocation failure in wsm_event_indication()
[karo-tx-linux.git] / drivers / net / wireless / cw1200 / wsm.c
1 /*
2  * WSM host interface (HI) implementation for
3  * ST-Ericsson CW1200 mac80211 drivers.
4  *
5  * Copyright (c) 2010, ST-Ericsson
6  * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/skbuff.h>
14 #include <linux/wait.h>
15 #include <linux/delay.h>
16 #include <linux/sched.h>
17 #include <linux/random.h>
18
19 #include "cw1200.h"
20 #include "wsm.h"
21 #include "bh.h"
22 #include "sta.h"
23 #include "debug.h"
24 #include "itp.h"
25
26 #define WSM_CMD_TIMEOUT         (2 * HZ) /* With respect to interrupt loss */
27 #define WSM_CMD_START_TIMEOUT   (7 * HZ)
28 #define WSM_CMD_RESET_TIMEOUT   (3 * HZ) /* 2 sec. timeout was observed.   */
29 #define WSM_CMD_MAX_TIMEOUT     (3 * HZ)
30
31 #define WSM_SKIP(buf, size)                                             \
32         do {                                                            \
33                 if ((buf)->data + size > (buf)->end)                    \
34                         goto underflow;                                 \
35                 (buf)->data += size;                                    \
36         } while (0)
37
38 #define WSM_GET(buf, ptr, size)                                         \
39         do {                                                            \
40                 if ((buf)->data + size > (buf)->end)                    \
41                         goto underflow;                                 \
42                 memcpy(ptr, (buf)->data, size);                         \
43                 (buf)->data += size;                                    \
44         } while (0)
45
46 #define __WSM_GET(buf, type, cvt)                                       \
47         ({                                                              \
48                 type val;                                               \
49                 if ((buf)->data + sizeof(type) > (buf)->end)            \
50                         goto underflow;                                 \
51                 val = cvt(*(type *)(buf)->data);                        \
52                 (buf)->data += sizeof(type);                            \
53                 val;                                                    \
54         })
55
56 #define WSM_GET8(buf)  __WSM_GET(buf, u8, (u8))
57 #define WSM_GET16(buf) __WSM_GET(buf, u16, __le16_to_cpu)
58 #define WSM_GET32(buf) __WSM_GET(buf, u32, __le32_to_cpu)
59
60 #define WSM_PUT(buf, ptr, size)                                         \
61         do {                                                            \
62                 if ((buf)->data + size > (buf)->end)            \
63                         if (wsm_buf_reserve((buf), size))       \
64                                 goto nomem;                             \
65                 memcpy((buf)->data, ptr, size);                         \
66                 (buf)->data += size;                                    \
67         } while (0)
68
69 #define __WSM_PUT(buf, val, type, cvt)                                  \
70         do {                                                            \
71                 if ((buf)->data + sizeof(type) > (buf)->end)            \
72                         if (wsm_buf_reserve((buf), sizeof(type))) \
73                                 goto nomem;                             \
74                 *(type *)(buf)->data = cvt(val);                        \
75                 (buf)->data += sizeof(type);                            \
76         } while (0)
77
78 #define WSM_PUT8(buf, val)  __WSM_PUT(buf, val, u8, (u8))
79 #define WSM_PUT16(buf, val) __WSM_PUT(buf, val, u16, __cpu_to_le16)
80 #define WSM_PUT32(buf, val) __WSM_PUT(buf, val, u32, __cpu_to_le32)
81
82 static void wsm_buf_reset(struct wsm_buf *buf);
83 static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size);
84
85 static int wsm_cmd_send(struct cw1200_common *priv,
86                         struct wsm_buf *buf,
87                         void *arg, u16 cmd, long tmo);
88
89 #define wsm_cmd_lock(__priv) mutex_lock(&((__priv)->wsm_cmd_mux))
90 #define wsm_cmd_unlock(__priv) mutex_unlock(&((__priv)->wsm_cmd_mux))
91
92 /* ******************************************************************** */
93 /* WSM API implementation                                               */
94
95 static int wsm_generic_confirm(struct cw1200_common *priv,
96                              void *arg,
97                              struct wsm_buf *buf)
98 {
99         u32 status = WSM_GET32(buf);
100         if (status != WSM_STATUS_SUCCESS)
101                 return -EINVAL;
102         return 0;
103
104 underflow:
105         WARN_ON(1);
106         return -EINVAL;
107 }
108
109 int wsm_configuration(struct cw1200_common *priv, struct wsm_configuration *arg)
110 {
111         int ret;
112         struct wsm_buf *buf = &priv->wsm_cmd_buf;
113
114         wsm_cmd_lock(priv);
115
116         WSM_PUT32(buf, arg->dot11MaxTransmitMsduLifeTime);
117         WSM_PUT32(buf, arg->dot11MaxReceiveLifeTime);
118         WSM_PUT32(buf, arg->dot11RtsThreshold);
119
120         /* DPD block. */
121         WSM_PUT16(buf, arg->dpdData_size + 12);
122         WSM_PUT16(buf, 1); /* DPD version */
123         WSM_PUT(buf, arg->dot11StationId, ETH_ALEN);
124         WSM_PUT16(buf, 5); /* DPD flags */
125         WSM_PUT(buf, arg->dpdData, arg->dpdData_size);
126
127         ret = wsm_cmd_send(priv, buf, arg,
128                            WSM_CONFIGURATION_REQ_ID, WSM_CMD_TIMEOUT);
129
130         wsm_cmd_unlock(priv);
131         return ret;
132
133 nomem:
134         wsm_cmd_unlock(priv);
135         return -ENOMEM;
136 }
137
138 static int wsm_configuration_confirm(struct cw1200_common *priv,
139                                      struct wsm_configuration *arg,
140                                      struct wsm_buf *buf)
141 {
142         int i;
143         int status;
144
145         status = WSM_GET32(buf);
146         if (WARN_ON(status != WSM_STATUS_SUCCESS))
147                 return -EINVAL;
148
149         WSM_GET(buf, arg->dot11StationId, ETH_ALEN);
150         arg->dot11FrequencyBandsSupported = WSM_GET8(buf);
151         WSM_SKIP(buf, 1);
152         arg->supportedRateMask = WSM_GET32(buf);
153         for (i = 0; i < 2; ++i) {
154                 arg->txPowerRange[i].min_power_level = WSM_GET32(buf);
155                 arg->txPowerRange[i].max_power_level = WSM_GET32(buf);
156                 arg->txPowerRange[i].stepping = WSM_GET32(buf);
157         }
158         return 0;
159
160 underflow:
161         WARN_ON(1);
162         return -EINVAL;
163 }
164
165 /* ******************************************************************** */
166
167 int wsm_reset(struct cw1200_common *priv, const struct wsm_reset *arg)
168 {
169         int ret;
170         struct wsm_buf *buf = &priv->wsm_cmd_buf;
171         u16 cmd = WSM_RESET_REQ_ID | WSM_TX_LINK_ID(arg->link_id);
172
173         wsm_cmd_lock(priv);
174
175         WSM_PUT32(buf, arg->reset_statistics ? 0 : 1);
176         ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_RESET_TIMEOUT);
177         wsm_cmd_unlock(priv);
178         return ret;
179
180 nomem:
181         wsm_cmd_unlock(priv);
182         return -ENOMEM;
183 }
184
185 /* ******************************************************************** */
186
187 struct wsm_mib {
188         u16 mib_id;
189         void *buf;
190         size_t buf_size;
191 };
192
193 int wsm_read_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
194                         size_t buf_size)
195 {
196         int ret;
197         struct wsm_buf *buf = &priv->wsm_cmd_buf;
198         struct wsm_mib mib_buf = {
199                 .mib_id = mib_id,
200                 .buf = _buf,
201                 .buf_size = buf_size,
202         };
203         wsm_cmd_lock(priv);
204
205         WSM_PUT16(buf, mib_id);
206         WSM_PUT16(buf, 0);
207
208         ret = wsm_cmd_send(priv, buf, &mib_buf,
209                            WSM_READ_MIB_REQ_ID, WSM_CMD_TIMEOUT);
210         wsm_cmd_unlock(priv);
211         return ret;
212
213 nomem:
214         wsm_cmd_unlock(priv);
215         return -ENOMEM;
216 }
217
218 static int wsm_read_mib_confirm(struct cw1200_common *priv,
219                                 struct wsm_mib *arg,
220                                 struct wsm_buf *buf)
221 {
222         u16 size;
223         if (WARN_ON(WSM_GET32(buf) != WSM_STATUS_SUCCESS))
224                 return -EINVAL;
225
226         if (WARN_ON(WSM_GET16(buf) != arg->mib_id))
227                 return -EINVAL;
228
229         size = WSM_GET16(buf);
230         if (size > arg->buf_size)
231                 size = arg->buf_size;
232
233         WSM_GET(buf, arg->buf, size);
234         arg->buf_size = size;
235         return 0;
236
237 underflow:
238         WARN_ON(1);
239         return -EINVAL;
240 }
241
242 /* ******************************************************************** */
243
244 int wsm_write_mib(struct cw1200_common *priv, u16 mib_id, void *_buf,
245                         size_t buf_size)
246 {
247         int ret;
248         struct wsm_buf *buf = &priv->wsm_cmd_buf;
249         struct wsm_mib mib_buf = {
250                 .mib_id = mib_id,
251                 .buf = _buf,
252                 .buf_size = buf_size,
253         };
254
255         wsm_cmd_lock(priv);
256
257         WSM_PUT16(buf, mib_id);
258         WSM_PUT16(buf, buf_size);
259         WSM_PUT(buf, _buf, buf_size);
260
261         ret = wsm_cmd_send(priv, buf, &mib_buf,
262                            WSM_WRITE_MIB_REQ_ID, WSM_CMD_TIMEOUT);
263         wsm_cmd_unlock(priv);
264         return ret;
265
266 nomem:
267         wsm_cmd_unlock(priv);
268         return -ENOMEM;
269 }
270
271 static int wsm_write_mib_confirm(struct cw1200_common *priv,
272                                 struct wsm_mib *arg,
273                                 struct wsm_buf *buf)
274 {
275         int ret;
276
277         ret = wsm_generic_confirm(priv, arg, buf);
278         if (ret)
279                 return ret;
280
281         if (arg->mib_id == WSM_MIB_ID_OPERATIONAL_POWER_MODE) {
282                 /* OperationalMode: update PM status. */
283                 const char *p = arg->buf;
284                 cw1200_enable_powersave(priv, (p[0] & 0x0F) ? true : false);
285         }
286         return 0;
287 }
288
289 /* ******************************************************************** */
290
291 int wsm_scan(struct cw1200_common *priv, const struct wsm_scan *arg)
292 {
293         int i;
294         int ret;
295         struct wsm_buf *buf = &priv->wsm_cmd_buf;
296
297         if (arg->num_channels > 48)
298                 return -EINVAL;
299
300         if (arg->num_ssids > 2)
301                 return -EINVAL;
302
303         if (arg->band > 1)
304                 return -EINVAL;
305
306         wsm_cmd_lock(priv);
307
308         WSM_PUT8(buf, arg->band);
309         WSM_PUT8(buf, arg->type);
310         WSM_PUT8(buf, arg->flags);
311         WSM_PUT8(buf, arg->max_tx_rate);
312         WSM_PUT32(buf, arg->auto_scan_interval);
313         WSM_PUT8(buf, arg->num_probes);
314         WSM_PUT8(buf, arg->num_channels);
315         WSM_PUT8(buf, arg->num_ssids);
316         WSM_PUT8(buf, arg->probe_delay);
317
318         for (i = 0; i < arg->num_channels; ++i) {
319                 WSM_PUT16(buf, arg->ch[i].number);
320                 WSM_PUT16(buf, 0);
321                 WSM_PUT32(buf, arg->ch[i].min_chan_time);
322                 WSM_PUT32(buf, arg->ch[i].max_chan_time);
323                 WSM_PUT32(buf, 0);
324         }
325
326         for (i = 0; i < arg->num_ssids; ++i) {
327                 WSM_PUT32(buf, arg->ssids[i].length);
328                 WSM_PUT(buf, &arg->ssids[i].ssid[0],
329                         sizeof(arg->ssids[i].ssid));
330         }
331
332         ret = wsm_cmd_send(priv, buf, NULL,
333                            WSM_START_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
334         wsm_cmd_unlock(priv);
335         return ret;
336
337 nomem:
338         wsm_cmd_unlock(priv);
339         return -ENOMEM;
340 }
341
342 /* ******************************************************************** */
343
344 int wsm_stop_scan(struct cw1200_common *priv)
345 {
346         int ret;
347         struct wsm_buf *buf = &priv->wsm_cmd_buf;
348         wsm_cmd_lock(priv);
349         ret = wsm_cmd_send(priv, buf, NULL,
350                            WSM_STOP_SCAN_REQ_ID, WSM_CMD_TIMEOUT);
351         wsm_cmd_unlock(priv);
352         return ret;
353 }
354
355
356 static int wsm_tx_confirm(struct cw1200_common *priv,
357                           struct wsm_buf *buf,
358                           int link_id)
359 {
360         struct wsm_tx_confirm tx_confirm;
361
362         tx_confirm.packet_id = WSM_GET32(buf);
363         tx_confirm.status = WSM_GET32(buf);
364         tx_confirm.tx_rate = WSM_GET8(buf);
365         tx_confirm.ack_failures = WSM_GET8(buf);
366         tx_confirm.flags = WSM_GET16(buf);
367         tx_confirm.media_delay = WSM_GET32(buf);
368         tx_confirm.tx_queue_delay = WSM_GET32(buf);
369
370         cw1200_tx_confirm_cb(priv, link_id, &tx_confirm);
371         return 0;
372
373 underflow:
374         WARN_ON(1);
375         return -EINVAL;
376 }
377
378 static int wsm_multi_tx_confirm(struct cw1200_common *priv,
379                                 struct wsm_buf *buf, int link_id)
380 {
381         int ret;
382         int count;
383         int i;
384
385         count = WSM_GET32(buf);
386         if (WARN_ON(count <= 0))
387                 return -EINVAL;
388
389         if (count > 1) {
390                 /* We already released one buffer, now for the rest */
391                 ret = wsm_release_tx_buffer(priv, count - 1);
392                 if (ret < 0)
393                         return ret;
394                 else if (ret > 0)
395                         cw1200_bh_wakeup(priv);
396         }
397
398         cw1200_debug_txed_multi(priv, count);
399         for (i = 0; i < count; ++i) {
400                 ret = wsm_tx_confirm(priv, buf, link_id);
401                 if (ret)
402                         return ret;
403         }
404         return ret;
405
406 underflow:
407         WARN_ON(1);
408         return -EINVAL;
409 }
410
411 /* ******************************************************************** */
412
413 static int wsm_join_confirm(struct cw1200_common *priv,
414                             struct wsm_join_cnf *arg,
415                             struct wsm_buf *buf)
416 {
417         arg->status = WSM_GET32(buf);
418         if (WARN_ON(arg->status) != WSM_STATUS_SUCCESS)
419                 return -EINVAL;
420
421         arg->min_power_level = WSM_GET32(buf);
422         arg->max_power_level = WSM_GET32(buf);
423
424         return 0;
425
426 underflow:
427         WARN_ON(1);
428         return -EINVAL;
429 }
430
431 int wsm_join(struct cw1200_common *priv, struct wsm_join *arg)
432 {
433         int ret;
434         struct wsm_buf *buf = &priv->wsm_cmd_buf;
435         struct wsm_join_cnf resp;
436         wsm_cmd_lock(priv);
437
438         WSM_PUT8(buf, arg->mode);
439         WSM_PUT8(buf, arg->band);
440         WSM_PUT16(buf, arg->channel_number);
441         WSM_PUT(buf, &arg->bssid[0], sizeof(arg->bssid));
442         WSM_PUT16(buf, arg->atim_window);
443         WSM_PUT8(buf, arg->preamble_type);
444         WSM_PUT8(buf, arg->probe_for_join);
445         WSM_PUT8(buf, arg->dtim_period);
446         WSM_PUT8(buf, arg->flags);
447         WSM_PUT32(buf, arg->ssid_len);
448         WSM_PUT(buf, &arg->ssid[0], sizeof(arg->ssid));
449         WSM_PUT32(buf, arg->beacon_interval);
450         WSM_PUT32(buf, arg->basic_rate_set);
451
452         priv->tx_burst_idx = -1;
453         ret = wsm_cmd_send(priv, buf, &resp,
454                            WSM_JOIN_REQ_ID, WSM_CMD_TIMEOUT);
455         /* TODO:  Update state based on resp.min|max_power_level */
456
457         priv->join_complete_status = resp.status;
458
459         wsm_cmd_unlock(priv);
460         return ret;
461
462 nomem:
463         wsm_cmd_unlock(priv);
464         return -ENOMEM;
465 }
466
467 /* ******************************************************************** */
468
469 int wsm_set_bss_params(struct cw1200_common *priv,
470                        const struct wsm_set_bss_params *arg)
471 {
472         int ret;
473         struct wsm_buf *buf = &priv->wsm_cmd_buf;
474
475         wsm_cmd_lock(priv);
476
477         WSM_PUT8(buf, (arg->reset_beacon_loss ?  0x1 : 0));
478         WSM_PUT8(buf, arg->beacon_lost_count);
479         WSM_PUT16(buf, arg->aid);
480         WSM_PUT32(buf, arg->operational_rate_set);
481
482         ret = wsm_cmd_send(priv, buf, NULL,
483                            WSM_SET_BSS_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
484
485         wsm_cmd_unlock(priv);
486         return ret;
487
488 nomem:
489         wsm_cmd_unlock(priv);
490         return -ENOMEM;
491 }
492
493 /* ******************************************************************** */
494
495 int wsm_add_key(struct cw1200_common *priv, const struct wsm_add_key *arg)
496 {
497         int ret;
498         struct wsm_buf *buf = &priv->wsm_cmd_buf;
499
500         wsm_cmd_lock(priv);
501
502         WSM_PUT(buf, arg, sizeof(*arg));
503
504         ret = wsm_cmd_send(priv, buf, NULL,
505                            WSM_ADD_KEY_REQ_ID, WSM_CMD_TIMEOUT);
506
507         wsm_cmd_unlock(priv);
508         return ret;
509
510 nomem:
511         wsm_cmd_unlock(priv);
512         return -ENOMEM;
513 }
514
515 /* ******************************************************************** */
516
517 int wsm_remove_key(struct cw1200_common *priv, const struct wsm_remove_key *arg)
518 {
519         int ret;
520         struct wsm_buf *buf = &priv->wsm_cmd_buf;
521
522         wsm_cmd_lock(priv);
523
524         WSM_PUT8(buf, arg->index);
525         WSM_PUT8(buf, 0);
526         WSM_PUT16(buf, 0);
527
528         ret = wsm_cmd_send(priv, buf, NULL,
529                            WSM_REMOVE_KEY_REQ_ID, WSM_CMD_TIMEOUT);
530
531         wsm_cmd_unlock(priv);
532         return ret;
533
534 nomem:
535         wsm_cmd_unlock(priv);
536         return -ENOMEM;
537 }
538
539 /* ******************************************************************** */
540
541 int wsm_set_tx_queue_params(struct cw1200_common *priv,
542                 const struct wsm_set_tx_queue_params *arg, u8 id)
543 {
544         int ret;
545         struct wsm_buf *buf = &priv->wsm_cmd_buf;
546         u8 queue_id_to_wmm_aci[] = {3, 2, 0, 1};
547
548         wsm_cmd_lock(priv);
549
550         WSM_PUT8(buf, queue_id_to_wmm_aci[id]);
551         WSM_PUT8(buf, 0);
552         WSM_PUT8(buf, arg->ackPolicy);
553         WSM_PUT8(buf, 0);
554         WSM_PUT32(buf, arg->maxTransmitLifetime);
555         WSM_PUT16(buf, arg->allowedMediumTime);
556         WSM_PUT16(buf, 0);
557
558         ret = wsm_cmd_send(priv, buf, NULL, 0x0012, WSM_CMD_TIMEOUT);
559
560         wsm_cmd_unlock(priv);
561         return ret;
562
563 nomem:
564         wsm_cmd_unlock(priv);
565         return -ENOMEM;
566 }
567
568 /* ******************************************************************** */
569
570 int wsm_set_edca_params(struct cw1200_common *priv,
571                                 const struct wsm_edca_params *arg)
572 {
573         int ret;
574         struct wsm_buf *buf = &priv->wsm_cmd_buf;
575
576         wsm_cmd_lock(priv);
577
578         /* Implemented according to specification. */
579
580         WSM_PUT16(buf, arg->params[3].cwmin);
581         WSM_PUT16(buf, arg->params[2].cwmin);
582         WSM_PUT16(buf, arg->params[1].cwmin);
583         WSM_PUT16(buf, arg->params[0].cwmin);
584
585         WSM_PUT16(buf, arg->params[3].cwmax);
586         WSM_PUT16(buf, arg->params[2].cwmax);
587         WSM_PUT16(buf, arg->params[1].cwmax);
588         WSM_PUT16(buf, arg->params[0].cwmax);
589
590         WSM_PUT8(buf, arg->params[3].aifns);
591         WSM_PUT8(buf, arg->params[2].aifns);
592         WSM_PUT8(buf, arg->params[1].aifns);
593         WSM_PUT8(buf, arg->params[0].aifns);
594
595         WSM_PUT16(buf, arg->params[3].txop_limit);
596         WSM_PUT16(buf, arg->params[2].txop_limit);
597         WSM_PUT16(buf, arg->params[1].txop_limit);
598         WSM_PUT16(buf, arg->params[0].txop_limit);
599
600         WSM_PUT32(buf, arg->params[3].max_rx_lifetime);
601         WSM_PUT32(buf, arg->params[2].max_rx_lifetime);
602         WSM_PUT32(buf, arg->params[1].max_rx_lifetime);
603         WSM_PUT32(buf, arg->params[0].max_rx_lifetime);
604
605         ret = wsm_cmd_send(priv, buf, NULL,
606                            WSM_EDCA_PARAMS_REQ_ID, WSM_CMD_TIMEOUT);
607         wsm_cmd_unlock(priv);
608         return ret;
609
610 nomem:
611         wsm_cmd_unlock(priv);
612         return -ENOMEM;
613 }
614
615 /* ******************************************************************** */
616
617 int wsm_switch_channel(struct cw1200_common *priv,
618                         const struct wsm_switch_channel *arg)
619 {
620         int ret;
621         struct wsm_buf *buf = &priv->wsm_cmd_buf;
622
623         wsm_cmd_lock(priv);
624
625         WSM_PUT8(buf, arg->mode);
626         WSM_PUT8(buf, arg->switch_count);
627         WSM_PUT16(buf, arg->channel_number);
628
629         priv->channel_switch_in_progress = 1;
630
631         ret = wsm_cmd_send(priv, buf, NULL,
632                            WSM_SWITCH_CHANNEL_REQ_ID, WSM_CMD_TIMEOUT);
633         if (ret)
634                 priv->channel_switch_in_progress = 0;
635
636         wsm_cmd_unlock(priv);
637         return ret;
638
639 nomem:
640         wsm_cmd_unlock(priv);
641         return -ENOMEM;
642 }
643
644 /* ******************************************************************** */
645
646 int wsm_set_pm(struct cw1200_common *priv, const struct wsm_set_pm *arg)
647 {
648         int ret;
649         struct wsm_buf *buf = &priv->wsm_cmd_buf;
650         priv->ps_mode_switch_in_progress = 1;
651
652         wsm_cmd_lock(priv);
653
654         WSM_PUT8(buf, arg->mode);
655         WSM_PUT8(buf, arg->fast_psm_idle_period);
656         WSM_PUT8(buf, arg->ap_psm_change_period);
657         WSM_PUT8(buf, arg->min_auto_pspoll_period);
658
659         ret = wsm_cmd_send(priv, buf, NULL,
660                            WSM_SET_PM_REQ_ID, WSM_CMD_TIMEOUT);
661
662         wsm_cmd_unlock(priv);
663         return ret;
664
665 nomem:
666         wsm_cmd_unlock(priv);
667         return -ENOMEM;
668 }
669
670 /* ******************************************************************** */
671
672 int wsm_start(struct cw1200_common *priv, const struct wsm_start *arg)
673 {
674         int ret;
675         struct wsm_buf *buf = &priv->wsm_cmd_buf;
676
677         wsm_cmd_lock(priv);
678
679         WSM_PUT8(buf, arg->mode);
680         WSM_PUT8(buf, arg->band);
681         WSM_PUT16(buf, arg->channel_number);
682         WSM_PUT32(buf, arg->ct_window);
683         WSM_PUT32(buf, arg->beacon_interval);
684         WSM_PUT8(buf, arg->dtim_period);
685         WSM_PUT8(buf, arg->preamble);
686         WSM_PUT8(buf, arg->probe_delay);
687         WSM_PUT8(buf, arg->ssid_len);
688         WSM_PUT(buf, arg->ssid, sizeof(arg->ssid));
689         WSM_PUT32(buf, arg->basic_rate_set);
690
691         priv->tx_burst_idx = -1;
692         ret = wsm_cmd_send(priv, buf, NULL,
693                            WSM_START_REQ_ID, WSM_CMD_START_TIMEOUT);
694
695         wsm_cmd_unlock(priv);
696         return ret;
697
698 nomem:
699         wsm_cmd_unlock(priv);
700         return -ENOMEM;
701 }
702
703 /* ******************************************************************** */
704
705 int wsm_beacon_transmit(struct cw1200_common *priv,
706                         const struct wsm_beacon_transmit *arg)
707 {
708         int ret;
709         struct wsm_buf *buf = &priv->wsm_cmd_buf;
710
711         wsm_cmd_lock(priv);
712
713         WSM_PUT32(buf, arg->enable_beaconing ? 1 : 0);
714
715         ret = wsm_cmd_send(priv, buf, NULL,
716                            WSM_BEACON_TRANSMIT_REQ_ID, WSM_CMD_TIMEOUT);
717
718         wsm_cmd_unlock(priv);
719         return ret;
720
721 nomem:
722         wsm_cmd_unlock(priv);
723         return -ENOMEM;
724 }
725
726 /* ******************************************************************** */
727
728 int wsm_start_find(struct cw1200_common *priv)
729 {
730         int ret;
731         struct wsm_buf *buf = &priv->wsm_cmd_buf;
732
733         wsm_cmd_lock(priv);
734         ret = wsm_cmd_send(priv, buf, NULL, 0x0019, WSM_CMD_TIMEOUT);
735         wsm_cmd_unlock(priv);
736         return ret;
737 }
738
739 /* ******************************************************************** */
740
741 int wsm_stop_find(struct cw1200_common *priv)
742 {
743         int ret;
744         struct wsm_buf *buf = &priv->wsm_cmd_buf;
745
746         wsm_cmd_lock(priv);
747         ret = wsm_cmd_send(priv, buf, NULL, 0x001A, WSM_CMD_TIMEOUT);
748         wsm_cmd_unlock(priv);
749         return ret;
750 }
751
752 /* ******************************************************************** */
753
754 int wsm_map_link(struct cw1200_common *priv, const struct wsm_map_link *arg)
755 {
756         int ret;
757         struct wsm_buf *buf = &priv->wsm_cmd_buf;
758         u16 cmd = 0x001C | WSM_TX_LINK_ID(arg->link_id);
759
760         wsm_cmd_lock(priv);
761
762         WSM_PUT(buf, &arg->mac_addr[0], sizeof(arg->mac_addr));
763         WSM_PUT16(buf, 0);
764
765         ret = wsm_cmd_send(priv, buf, NULL, cmd, WSM_CMD_TIMEOUT);
766
767         wsm_cmd_unlock(priv);
768         return ret;
769
770 nomem:
771         wsm_cmd_unlock(priv);
772         return -ENOMEM;
773 }
774
775 /* ******************************************************************** */
776
777 int wsm_update_ie(struct cw1200_common *priv,
778                   const struct wsm_update_ie *arg)
779 {
780         int ret;
781         struct wsm_buf *buf = &priv->wsm_cmd_buf;
782
783         wsm_cmd_lock(priv);
784
785         WSM_PUT16(buf, arg->what);
786         WSM_PUT16(buf, arg->count);
787         WSM_PUT(buf, arg->ies, arg->length);
788
789         ret = wsm_cmd_send(priv, buf, NULL, 0x001B, WSM_CMD_TIMEOUT);
790
791         wsm_cmd_unlock(priv);
792         return ret;
793
794 nomem:
795         wsm_cmd_unlock(priv);
796         return -ENOMEM;
797 }
798
799 /* ******************************************************************** */
800 int wsm_set_probe_responder(struct cw1200_common *priv, bool enable)
801 {
802         priv->rx_filter.probeResponder = enable;
803         return wsm_set_rx_filter(priv, &priv->rx_filter);
804 }
805
806 /* ******************************************************************** */
807 /* WSM indication events implementation                                 */
808 const char * const cw1200_fw_types[] = {
809         "ETF",
810         "WFM",
811         "WSM",
812         "HI test",
813         "Platform test"
814 };
815
816 static int wsm_startup_indication(struct cw1200_common *priv,
817                                         struct wsm_buf *buf)
818 {
819         priv->wsm_caps.input_buffers     = WSM_GET16(buf);
820         priv->wsm_caps.input_buffer_size = WSM_GET16(buf);
821         priv->wsm_caps.hw_id      = WSM_GET16(buf);
822         priv->wsm_caps.hw_subid   = WSM_GET16(buf);
823         priv->wsm_caps.status     = WSM_GET16(buf);
824         priv->wsm_caps.fw_cap     = WSM_GET16(buf);
825         priv->wsm_caps.fw_type    = WSM_GET16(buf);
826         priv->wsm_caps.fw_api     = WSM_GET16(buf);
827         priv->wsm_caps.fw_build   = WSM_GET16(buf);
828         priv->wsm_caps.fw_ver     = WSM_GET16(buf);
829         WSM_GET(buf, priv->wsm_caps.fw_label, sizeof(priv->wsm_caps.fw_label));
830         priv->wsm_caps.fw_label[sizeof(priv->wsm_caps.fw_label) - 1] = 0; /* Do not trust FW too much... */
831
832         if (WARN_ON(priv->wsm_caps.status))
833                 return -EINVAL;
834
835         if (WARN_ON(priv->wsm_caps.fw_type > 4))
836                 return -EINVAL;
837
838         pr_info("CW1200 WSM init done.\n"
839                 "   Input buffers: %d x %d bytes\n"
840                 "   Hardware: %d.%d\n"
841                 "   %s firmware [%s], ver: %d, build: %d,"
842                 "   api: %d, cap: 0x%.4X\n",
843                 priv->wsm_caps.input_buffers,
844                 priv->wsm_caps.input_buffer_size,
845                 priv->wsm_caps.hw_id, priv->wsm_caps.hw_subid,
846                 cw1200_fw_types[priv->wsm_caps.fw_type],
847                 priv->wsm_caps.fw_label, priv->wsm_caps.fw_ver,
848                 priv->wsm_caps.fw_build,
849                 priv->wsm_caps.fw_api, priv->wsm_caps.fw_cap);
850
851         /* Disable unsupported frequency bands */
852         if (!(priv->wsm_caps.fw_cap & 0x1))
853                 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = NULL;
854         if (!(priv->wsm_caps.fw_cap & 0x2))
855                 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = NULL;
856
857         priv->firmware_ready = 1;
858         wake_up(&priv->wsm_startup_done);
859         return 0;
860
861 underflow:
862         WARN_ON(1);
863         return -EINVAL;
864 }
865
866 static int wsm_receive_indication(struct cw1200_common *priv,
867                                   int link_id,
868                                   struct wsm_buf *buf,
869                                   struct sk_buff **skb_p)
870 {
871         struct wsm_rx rx;
872         struct ieee80211_hdr *hdr;
873         size_t hdr_len;
874         __le16 fctl;
875
876         rx.status = WSM_GET32(buf);
877         rx.channel_number = WSM_GET16(buf);
878         rx.rx_rate = WSM_GET8(buf);
879         rx.rcpi_rssi = WSM_GET8(buf);
880         rx.flags = WSM_GET32(buf);
881
882         /* FW Workaround: Drop probe resp or
883            beacon when RSSI is 0
884         */
885         hdr = (struct ieee80211_hdr *)(*skb_p)->data;
886
887         if (!rx.rcpi_rssi &&
888             (ieee80211_is_probe_resp(hdr->frame_control) ||
889              ieee80211_is_beacon(hdr->frame_control)))
890                 return 0;
891
892         /* If no RSSI subscription has been made,
893          * convert RCPI to RSSI here
894          */
895         if (!priv->cqm_use_rssi)
896                 rx.rcpi_rssi = rx.rcpi_rssi / 2 - 110;
897
898         fctl = *(__le16 *)buf->data;
899         hdr_len = buf->data - buf->begin;
900         skb_pull(*skb_p, hdr_len);
901         if (!rx.status && ieee80211_is_deauth(fctl)) {
902                 if (priv->join_status == CW1200_JOIN_STATUS_STA) {
903                         /* Shedule unjoin work */
904                         pr_debug("[WSM] Issue unjoin command (RX).\n");
905                         wsm_lock_tx_async(priv);
906                         if (queue_work(priv->workqueue,
907                                        &priv->unjoin_work) <= 0)
908                                 wsm_unlock_tx(priv);
909                 }
910         }
911         cw1200_rx_cb(priv, &rx, link_id, skb_p);
912         if (*skb_p)
913                 skb_push(*skb_p, hdr_len);
914
915         return 0;
916
917 underflow:
918         return -EINVAL;
919 }
920
921 static int wsm_event_indication(struct cw1200_common *priv, struct wsm_buf *buf)
922 {
923         int first;
924         struct cw1200_wsm_event *event;
925
926         if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
927                 /* STA is stopped. */
928                 return 0;
929         }
930
931         event = kzalloc(sizeof(struct cw1200_wsm_event), GFP_KERNEL);
932         if (!event)
933                 return -ENOMEM;
934
935         event->evt.id = __le32_to_cpu(WSM_GET32(buf));
936         event->evt.data = __le32_to_cpu(WSM_GET32(buf));
937
938         pr_debug("[WSM] Event: %d(%d)\n",
939                  event->evt.id, event->evt.data);
940
941         spin_lock(&priv->event_queue_lock);
942         first = list_empty(&priv->event_queue);
943         list_add_tail(&event->link, &priv->event_queue);
944         spin_unlock(&priv->event_queue_lock);
945
946         if (first)
947                 queue_work(priv->workqueue, &priv->event_handler);
948
949         return 0;
950
951 underflow:
952         kfree(event);
953         return -EINVAL;
954 }
955
956 static int wsm_channel_switch_indication(struct cw1200_common *priv,
957                                          struct wsm_buf *buf)
958 {
959         WARN_ON(WSM_GET32(buf));
960
961         priv->channel_switch_in_progress = 0;
962         wake_up(&priv->channel_switch_done);
963
964         wsm_unlock_tx(priv);
965
966         return 0;
967
968 underflow:
969         return -EINVAL;
970 }
971
972 static int wsm_set_pm_indication(struct cw1200_common *priv,
973                                  struct wsm_buf *buf)
974 {
975         /* TODO:  Check buf (struct wsm_set_pm_complete) for validity */
976         if (priv->ps_mode_switch_in_progress) {
977                 priv->ps_mode_switch_in_progress = 0;
978                 wake_up(&priv->ps_mode_switch_done);
979         }
980         return 0;
981 }
982
983 static int wsm_scan_started(struct cw1200_common *priv, void *arg,
984                             struct wsm_buf *buf)
985 {
986         u32 status = WSM_GET32(buf);
987         if (status != WSM_STATUS_SUCCESS) {
988                 cw1200_scan_failed_cb(priv);
989                 return -EINVAL;
990         }
991         return 0;
992
993 underflow:
994         WARN_ON(1);
995         return -EINVAL;
996 }
997
998 static int wsm_scan_complete_indication(struct cw1200_common *priv,
999                                         struct wsm_buf *buf)
1000 {
1001         struct wsm_scan_complete arg;
1002         arg.status = WSM_GET32(buf);
1003         arg.psm = WSM_GET8(buf);
1004         arg.num_channels = WSM_GET8(buf);
1005         cw1200_scan_complete_cb(priv, &arg);
1006
1007         return 0;
1008
1009 underflow:
1010         return -EINVAL;
1011 }
1012
1013 static int wsm_join_complete_indication(struct cw1200_common *priv,
1014                                         struct wsm_buf *buf)
1015 {
1016         struct wsm_join_complete arg;
1017         arg.status = WSM_GET32(buf);
1018         pr_debug("[WSM] Join complete indication, status: %d\n", arg.status);
1019         cw1200_join_complete_cb(priv, &arg);
1020
1021         return 0;
1022
1023 underflow:
1024         return -EINVAL;
1025 }
1026
1027 static int wsm_find_complete_indication(struct cw1200_common *priv,
1028                                         struct wsm_buf *buf)
1029 {
1030         pr_warn("Implement find_complete_indication\n");
1031         return 0;
1032 }
1033
1034 static int wsm_ba_timeout_indication(struct cw1200_common *priv,
1035                                      struct wsm_buf *buf)
1036 {
1037         u32 dummy;
1038         u8 tid;
1039         u8 dummy2;
1040         u8 addr[ETH_ALEN];
1041
1042         dummy = WSM_GET32(buf);
1043         tid = WSM_GET8(buf);
1044         dummy2 = WSM_GET8(buf);
1045         WSM_GET(buf, addr, ETH_ALEN);
1046
1047         pr_info("BlockACK timeout, tid %d, addr %pM\n",
1048                 tid, addr);
1049
1050         return 0;
1051
1052 underflow:
1053         return -EINVAL;
1054 }
1055
1056 static int wsm_suspend_resume_indication(struct cw1200_common *priv,
1057                                          int link_id, struct wsm_buf *buf)
1058 {
1059         u32 flags;
1060         struct wsm_suspend_resume arg;
1061
1062         flags = WSM_GET32(buf);
1063         arg.link_id = link_id;
1064         arg.stop = !(flags & 1);
1065         arg.multicast = !!(flags & 8);
1066         arg.queue = (flags >> 1) & 3;
1067
1068         cw1200_suspend_resume(priv, &arg);
1069
1070         return 0;
1071
1072 underflow:
1073         return -EINVAL;
1074 }
1075
1076
1077 /* ******************************************************************** */
1078 /* WSM TX                                                               */
1079
1080 static int wsm_cmd_send(struct cw1200_common *priv,
1081                         struct wsm_buf *buf,
1082                         void *arg, u16 cmd, long tmo)
1083 {
1084         size_t buf_len = buf->data - buf->begin;
1085         int ret;
1086
1087         /* Don't bother if we're dead. */
1088         if (priv->bh_error) {
1089                 ret = 0;
1090                 goto done;
1091         }
1092
1093         /* Block until the cmd buffer is completed.  Tortuous. */
1094         spin_lock(&priv->wsm_cmd.lock);
1095         while (!priv->wsm_cmd.done) {
1096                 spin_unlock(&priv->wsm_cmd.lock);
1097                 spin_lock(&priv->wsm_cmd.lock);
1098         }
1099         priv->wsm_cmd.done = 0;
1100         spin_unlock(&priv->wsm_cmd.lock);
1101
1102         if (cmd == WSM_WRITE_MIB_REQ_ID ||
1103             cmd == WSM_READ_MIB_REQ_ID)
1104                 pr_debug("[WSM] >>> 0x%.4X [MIB: 0x%.4X] (%zu)\n",
1105                          cmd, __le16_to_cpu(((__le16 *)buf->begin)[2]),
1106                          buf_len);
1107         else
1108                 pr_debug("[WSM] >>> 0x%.4X (%zu)\n", cmd, buf_len);
1109
1110         /*
1111          * Due to buggy SPI on CW1200, we need to
1112          * pad the message by a few bytes to ensure
1113          * that it's completely received.
1114          */
1115 #ifdef CONFIG_CW1200_ETF
1116         if (!etf_mode)
1117 #endif
1118                 buf_len += 4;
1119
1120         /* Fill HI message header */
1121         /* BH will add sequence number */
1122         ((__le16 *)buf->begin)[0] = __cpu_to_le16(buf_len);
1123         ((__le16 *)buf->begin)[1] = __cpu_to_le16(cmd);
1124
1125         spin_lock(&priv->wsm_cmd.lock);
1126         BUG_ON(priv->wsm_cmd.ptr);
1127         priv->wsm_cmd.ptr = buf->begin;
1128         priv->wsm_cmd.len = buf_len;
1129         priv->wsm_cmd.arg = arg;
1130         priv->wsm_cmd.cmd = cmd;
1131         spin_unlock(&priv->wsm_cmd.lock);
1132
1133         cw1200_bh_wakeup(priv);
1134
1135         /* Wait for command completion */
1136         ret = wait_event_timeout(priv->wsm_cmd_wq,
1137                                  priv->wsm_cmd.done, tmo);
1138
1139         if (!ret && !priv->wsm_cmd.done) {
1140                 spin_lock(&priv->wsm_cmd.lock);
1141                 priv->wsm_cmd.done = 1;
1142                 priv->wsm_cmd.ptr = NULL;
1143                 spin_unlock(&priv->wsm_cmd.lock);
1144                 if (priv->bh_error) {
1145                         /* Return ok to help system cleanup */
1146                         ret = 0;
1147                 } else {
1148                         pr_err("CMD req (0x%04x) stuck in firmware, killing BH\n", priv->wsm_cmd.cmd);
1149                         print_hex_dump_bytes("REQDUMP: ", DUMP_PREFIX_NONE,
1150                                              buf->begin, buf_len);
1151                         pr_err("Outstanding outgoing frames:  %d\n", priv->hw_bufs_used);
1152
1153                         /* Kill BH thread to report the error to the top layer. */
1154                         atomic_add(1, &priv->bh_term);
1155                         wake_up(&priv->bh_wq);
1156                         ret = -ETIMEDOUT;
1157                 }
1158         } else {
1159                 spin_lock(&priv->wsm_cmd.lock);
1160                 BUG_ON(!priv->wsm_cmd.done);
1161                 ret = priv->wsm_cmd.ret;
1162                 spin_unlock(&priv->wsm_cmd.lock);
1163         }
1164 done:
1165         wsm_buf_reset(buf);
1166         return ret;
1167 }
1168
1169 #ifdef CONFIG_CW1200_ETF
1170 int wsm_raw_cmd(struct cw1200_common *priv, u8 *data, size_t len)
1171 {
1172         struct wsm_buf *buf = &priv->wsm_cmd_buf;
1173         int ret;
1174
1175         u16 *cmd = (u16 *)(data + 2);
1176
1177         wsm_cmd_lock(priv);
1178
1179         WSM_PUT(buf, data + 4, len - 4);  /* Skip over header (u16+u16) */
1180
1181         ret = wsm_cmd_send(priv, buf, NULL, __le16_to_cpu(*cmd), WSM_CMD_TIMEOUT);
1182
1183         wsm_cmd_unlock(priv);
1184         return ret;
1185
1186 nomem:
1187         wsm_cmd_unlock(priv);
1188         return -ENOMEM;
1189 }
1190 #endif /* CONFIG_CW1200_ETF */
1191
1192 /* ******************************************************************** */
1193 /* WSM TX port control                                                  */
1194
1195 void wsm_lock_tx(struct cw1200_common *priv)
1196 {
1197         wsm_cmd_lock(priv);
1198         if (atomic_add_return(1, &priv->tx_lock) == 1) {
1199                 if (wsm_flush_tx(priv))
1200                         pr_debug("[WSM] TX is locked.\n");
1201         }
1202         wsm_cmd_unlock(priv);
1203 }
1204
1205 void wsm_lock_tx_async(struct cw1200_common *priv)
1206 {
1207         if (atomic_add_return(1, &priv->tx_lock) == 1)
1208                 pr_debug("[WSM] TX is locked (async).\n");
1209 }
1210
1211 bool wsm_flush_tx(struct cw1200_common *priv)
1212 {
1213         unsigned long timestamp = jiffies;
1214         bool pending = false;
1215         long timeout;
1216         int i;
1217
1218         /* Flush must be called with TX lock held. */
1219         BUG_ON(!atomic_read(&priv->tx_lock));
1220
1221         /* First check if we really need to do something.
1222          * It is safe to use unprotected access, as hw_bufs_used
1223          * can only decrements.
1224          */
1225         if (!priv->hw_bufs_used)
1226                 return true;
1227
1228         if (priv->bh_error) {
1229                 /* In case of failure do not wait for magic. */
1230                 pr_err("[WSM] Fatal error occured, will not flush TX.\n");
1231                 return false;
1232         } else {
1233                 /* Get a timestamp of "oldest" frame */
1234                 for (i = 0; i < 4; ++i)
1235                         pending |= cw1200_queue_get_xmit_timestamp(
1236                                         &priv->tx_queue[i],
1237                                         &timestamp, 0xffffffff);
1238                 /* If there's nothing pending, we're good */
1239                 if (!pending)
1240                         return true;
1241
1242                 timeout = timestamp + WSM_CMD_LAST_CHANCE_TIMEOUT - jiffies;
1243                 if (timeout < 0 || wait_event_timeout(priv->bh_evt_wq,
1244                                                       !priv->hw_bufs_used,
1245                                                       timeout) <= 0) {
1246                         /* Hmmm... Not good. Frame had stuck in firmware. */
1247                         priv->bh_error = 1;
1248                         wiphy_err(priv->hw->wiphy, "[WSM] TX Frames (%d) stuck in firmware, killing BH\n", priv->hw_bufs_used);
1249                         wake_up(&priv->bh_wq);
1250                         return false;
1251                 }
1252
1253                 /* Ok, everything is flushed. */
1254                 return true;
1255         }
1256 }
1257
1258 void wsm_unlock_tx(struct cw1200_common *priv)
1259 {
1260         int tx_lock;
1261         tx_lock = atomic_sub_return(1, &priv->tx_lock);
1262         BUG_ON(tx_lock < 0);
1263
1264         if (tx_lock == 0) {
1265                 if (!priv->bh_error)
1266                         cw1200_bh_wakeup(priv);
1267                 pr_debug("[WSM] TX is unlocked.\n");
1268         }
1269 }
1270
1271 /* ******************************************************************** */
1272 /* WSM RX                                                               */
1273
1274 int wsm_handle_exception(struct cw1200_common *priv, u8 *data, size_t len)
1275 {
1276         struct wsm_buf buf;
1277         u32 reason;
1278         u32 reg[18];
1279         char fname[48];
1280         unsigned int i;
1281
1282         static const char * const reason_str[] = {
1283                 "undefined instruction",
1284                 "prefetch abort",
1285                 "data abort",
1286                 "unknown error",
1287         };
1288
1289         buf.begin = buf.data = data;
1290         buf.end = &buf.begin[len];
1291
1292         reason = WSM_GET32(&buf);
1293         for (i = 0; i < ARRAY_SIZE(reg); ++i)
1294                 reg[i] = WSM_GET32(&buf);
1295         WSM_GET(&buf, fname, sizeof(fname));
1296
1297         if (reason < 4)
1298                 wiphy_err(priv->hw->wiphy,
1299                           "Firmware exception: %s.\n",
1300                           reason_str[reason]);
1301         else
1302                 wiphy_err(priv->hw->wiphy,
1303                           "Firmware assert at %.*s, line %d\n",
1304                           (int) sizeof(fname), fname, reg[1]);
1305
1306         for (i = 0; i < 12; i += 4)
1307                 wiphy_err(priv->hw->wiphy,
1308                           "R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X, R%d: 0x%.8X,\n",
1309                           i + 0, reg[i + 0], i + 1, reg[i + 1],
1310                           i + 2, reg[i + 2], i + 3, reg[i + 3]);
1311         wiphy_err(priv->hw->wiphy,
1312                   "R12: 0x%.8X, SP: 0x%.8X, LR: 0x%.8X, PC: 0x%.8X,\n",
1313                   reg[i + 0], reg[i + 1], reg[i + 2], reg[i + 3]);
1314         i += 4;
1315         wiphy_err(priv->hw->wiphy,
1316                   "CPSR: 0x%.8X, SPSR: 0x%.8X\n",
1317                   reg[i + 0], reg[i + 1]);
1318
1319         print_hex_dump_bytes("R1: ", DUMP_PREFIX_NONE,
1320                              fname, sizeof(fname));
1321         return 0;
1322
1323 underflow:
1324         wiphy_err(priv->hw->wiphy, "Firmware exception.\n");
1325         print_hex_dump_bytes("Exception: ", DUMP_PREFIX_NONE,
1326                              data, len);
1327         return -EINVAL;
1328 }
1329
1330 int wsm_handle_rx(struct cw1200_common *priv, u16 id,
1331                   struct wsm_hdr *wsm, struct sk_buff **skb_p)
1332 {
1333         int ret = 0;
1334         struct wsm_buf wsm_buf;
1335         int link_id = (id >> 6) & 0x0F;
1336
1337         /* Strip link id. */
1338         id &= ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
1339
1340         wsm_buf.begin = (u8 *)&wsm[0];
1341         wsm_buf.data = (u8 *)&wsm[1];
1342         wsm_buf.end = &wsm_buf.begin[__le32_to_cpu(wsm->len)];
1343
1344         pr_debug("[WSM] <<< 0x%.4X (%td)\n", id,
1345                  wsm_buf.end - wsm_buf.begin);
1346
1347 #ifdef CONFIG_CW1200_ETF
1348         if (etf_mode) {
1349                 struct sk_buff *skb = alloc_skb(wsm_buf.end - wsm_buf.begin, GFP_KERNEL);
1350
1351                 /* Strip out Sequence num before passing up */
1352                 wsm->id = __le16_to_cpu(wsm->id);
1353                 wsm->id &= 0x0FFF;
1354                 wsm->id = __cpu_to_le16(wsm->id);
1355
1356                 memcpy(skb_put(skb, wsm_buf.end - wsm_buf.begin),
1357                        wsm_buf.begin,
1358                        wsm_buf.end - wsm_buf.begin);
1359                 skb_queue_tail(&priv->etf_q, skb);
1360
1361                 /* Special case for startup */
1362                 if (id == WSM_STARTUP_IND_ID) {
1363                         wsm_startup_indication(priv, &wsm_buf);
1364                 } else if (id & 0x0400) {
1365                         spin_lock(&priv->wsm_cmd.lock);
1366                         priv->wsm_cmd.done = 1;
1367                         spin_unlock(&priv->wsm_cmd.lock);
1368                         wake_up(&priv->wsm_cmd_wq);
1369                 }
1370
1371                 goto out;
1372         }
1373 #endif
1374
1375         if (id == WSM_TX_CONFIRM_IND_ID) {
1376                 ret = wsm_tx_confirm(priv, &wsm_buf, link_id);
1377         } else if (id == WSM_MULTI_TX_CONFIRM_ID) {
1378                 ret = wsm_multi_tx_confirm(priv, &wsm_buf, link_id);
1379         } else if (id & 0x0400) {
1380                 void *wsm_arg;
1381                 u16 wsm_cmd;
1382
1383                 /* Do not trust FW too much. Protection against repeated
1384                  * response and race condition removal (see above).
1385                  */
1386                 spin_lock(&priv->wsm_cmd.lock);
1387                 wsm_arg = priv->wsm_cmd.arg;
1388                 wsm_cmd = priv->wsm_cmd.cmd &
1389                                 ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX);
1390                 priv->wsm_cmd.cmd = 0xFFFF;
1391                 spin_unlock(&priv->wsm_cmd.lock);
1392
1393                 if (WARN_ON((id & ~0x0400) != wsm_cmd)) {
1394                         /* Note that any non-zero is a fatal retcode. */
1395                         ret = -EINVAL;
1396                         goto out;
1397                 }
1398
1399                 /* Note that wsm_arg can be NULL in case of timeout in
1400                  * wsm_cmd_send().
1401                  */
1402
1403                 switch (id) {
1404                 case WSM_READ_MIB_RESP_ID:
1405                         if (wsm_arg)
1406                                 ret = wsm_read_mib_confirm(priv, wsm_arg,
1407                                                                 &wsm_buf);
1408                         break;
1409                 case WSM_WRITE_MIB_RESP_ID:
1410                         if (wsm_arg)
1411                                 ret = wsm_write_mib_confirm(priv, wsm_arg,
1412                                                             &wsm_buf);
1413                         break;
1414                 case WSM_START_SCAN_RESP_ID:
1415                         if (wsm_arg)
1416                                 ret = wsm_scan_started(priv, wsm_arg, &wsm_buf);
1417                         break;
1418                 case WSM_CONFIGURATION_RESP_ID:
1419                         if (wsm_arg)
1420                                 ret = wsm_configuration_confirm(priv, wsm_arg,
1421                                                                 &wsm_buf);
1422                         break;
1423                 case WSM_JOIN_RESP_ID:
1424                         if (wsm_arg)
1425                                 ret = wsm_join_confirm(priv, wsm_arg, &wsm_buf);
1426                         break;
1427                 case WSM_STOP_SCAN_RESP_ID:
1428                 case WSM_RESET_RESP_ID:
1429                 case WSM_ADD_KEY_RESP_ID:
1430                 case WSM_REMOVE_KEY_RESP_ID:
1431                 case WSM_SET_PM_RESP_ID:
1432                 case WSM_SET_BSS_PARAMS_RESP_ID:
1433                 case 0x0412: /* set_tx_queue_params */
1434                 case WSM_EDCA_PARAMS_RESP_ID:
1435                 case WSM_SWITCH_CHANNEL_RESP_ID:
1436                 case WSM_START_RESP_ID:
1437                 case WSM_BEACON_TRANSMIT_RESP_ID:
1438                 case 0x0419: /* start_find */
1439                 case 0x041A: /* stop_find */
1440                 case 0x041B: /* update_ie */
1441                 case 0x041C: /* map_link */
1442                         WARN_ON(wsm_arg != NULL);
1443                         ret = wsm_generic_confirm(priv, wsm_arg, &wsm_buf);
1444                         if (ret) {
1445                                 wiphy_warn(priv->hw->wiphy,
1446                                            "wsm_generic_confirm failed for request 0x%04x.\n",
1447                                            id & ~0x0400);
1448
1449                                 /* often 0x407 and 0x410 occur, this means we're dead.. */
1450                                 if (priv->join_status >= CW1200_JOIN_STATUS_JOINING) {
1451                                         wsm_lock_tx(priv);
1452                                         if (queue_work(priv->workqueue, &priv->unjoin_work) <= 0)
1453                                                 wsm_unlock_tx(priv);
1454                                 }
1455                         }
1456                         break;
1457                 default:
1458                         wiphy_warn(priv->hw->wiphy,
1459                                    "Unrecognized confirmation 0x%04x\n",
1460                                    id & ~0x0400);
1461                 }
1462
1463                 spin_lock(&priv->wsm_cmd.lock);
1464                 priv->wsm_cmd.ret = ret;
1465                 priv->wsm_cmd.done = 1;
1466                 spin_unlock(&priv->wsm_cmd.lock);
1467
1468                 ret = 0; /* Error response from device should ne stop BH. */
1469
1470                 wake_up(&priv->wsm_cmd_wq);
1471         } else if (id & 0x0800) {
1472                 switch (id) {
1473                 case WSM_STARTUP_IND_ID:
1474                         ret = wsm_startup_indication(priv, &wsm_buf);
1475                         break;
1476                 case WSM_RECEIVE_IND_ID:
1477                         ret = wsm_receive_indication(priv, link_id,
1478                                                      &wsm_buf, skb_p);
1479                         break;
1480                 case 0x0805:
1481                         ret = wsm_event_indication(priv, &wsm_buf);
1482                         break;
1483                 case WSM_SCAN_COMPLETE_IND_ID:
1484                         ret = wsm_scan_complete_indication(priv, &wsm_buf);
1485                         break;
1486                 case 0x0808:
1487                         ret = wsm_ba_timeout_indication(priv, &wsm_buf);
1488                         break;
1489                 case 0x0809:
1490                         ret = wsm_set_pm_indication(priv, &wsm_buf);
1491                         break;
1492                 case 0x080A:
1493                         ret = wsm_channel_switch_indication(priv, &wsm_buf);
1494                         break;
1495                 case 0x080B:
1496                         ret = wsm_find_complete_indication(priv, &wsm_buf);
1497                         break;
1498                 case 0x080C:
1499                         ret = wsm_suspend_resume_indication(priv,
1500                                         link_id, &wsm_buf);
1501                         break;
1502                 case 0x080F:
1503                         ret = wsm_join_complete_indication(priv, &wsm_buf);
1504                         break;
1505                 default:
1506                         pr_warn("Unrecognised WSM ID %04x\n", id);
1507                 }
1508         } else {
1509                 WARN_ON(1);
1510                 ret = -EINVAL;
1511         }
1512 out:
1513         return ret;
1514 }
1515
1516 static bool wsm_handle_tx_data(struct cw1200_common *priv,
1517                                struct wsm_tx *wsm,
1518                                const struct ieee80211_tx_info *tx_info,
1519                                const struct cw1200_txpriv *txpriv,
1520                                struct cw1200_queue *queue)
1521 {
1522         bool handled = false;
1523         const struct ieee80211_hdr *frame =
1524                 (struct ieee80211_hdr *)&((u8 *)wsm)[txpriv->offset];
1525         __le16 fctl = frame->frame_control;
1526         enum {
1527                 do_probe,
1528                 do_drop,
1529                 do_wep,
1530                 do_tx,
1531         } action = do_tx;
1532
1533         switch (priv->mode) {
1534         case NL80211_IFTYPE_STATION:
1535                 if (priv->join_status == CW1200_JOIN_STATUS_MONITOR)
1536                         action = do_tx;
1537                 else if (priv->join_status < CW1200_JOIN_STATUS_PRE_STA)
1538                         action = do_drop;
1539                 break;
1540         case NL80211_IFTYPE_AP:
1541                 if (!priv->join_status) {
1542                         action = do_drop;
1543                 } else if (!(BIT(txpriv->raw_link_id) &
1544                              (BIT(0) | priv->link_id_map))) {
1545                         wiphy_warn(priv->hw->wiphy,
1546                                    "A frame with expired link id is dropped.\n");
1547                         action = do_drop;
1548                 }
1549                 if (cw1200_queue_get_generation(wsm->packet_id) >
1550                                 CW1200_MAX_REQUEUE_ATTEMPTS) {
1551                         /* HACK!!! WSM324 firmware has tendency to requeue
1552                          * multicast frames in a loop, causing performance
1553                          * drop and high power consumption of the driver.
1554                          * In this situation it is better just to drop
1555                          * the problematic frame.
1556                          */
1557                         wiphy_warn(priv->hw->wiphy,
1558                                    "Too many attempts to requeue a frame; dropped.\n");
1559                         action = do_drop;
1560                 }
1561                 break;
1562         case NL80211_IFTYPE_ADHOC:
1563                 if (priv->join_status != CW1200_JOIN_STATUS_IBSS)
1564                         action = do_drop;
1565                 break;
1566         case NL80211_IFTYPE_MESH_POINT:
1567                 action = do_tx; /* TODO:  Test me! */
1568                 break;
1569         case NL80211_IFTYPE_MONITOR:
1570         default:
1571                 action = do_drop;
1572                 break;
1573         }
1574
1575         if (action == do_tx) {
1576                 if (ieee80211_is_nullfunc(fctl)) {
1577                         spin_lock(&priv->bss_loss_lock);
1578                         if (priv->bss_loss_state) {
1579                                 priv->bss_loss_confirm_id = wsm->packet_id;
1580                                 wsm->queue_id = WSM_QUEUE_VOICE;
1581                         }
1582                         spin_unlock(&priv->bss_loss_lock);
1583                 } else if (ieee80211_is_probe_req(fctl)) {
1584                         action = do_probe;
1585                 } else if (ieee80211_is_deauth(fctl) &&
1586                            priv->mode != NL80211_IFTYPE_AP) {
1587                         pr_debug("[WSM] Issue unjoin command due to tx deauth.\n");
1588                         wsm_lock_tx_async(priv);
1589                         if (queue_work(priv->workqueue,
1590                                        &priv->unjoin_work) <= 0)
1591                                 wsm_unlock_tx(priv);
1592                 } else if (ieee80211_has_protected(fctl) &&
1593                            tx_info->control.hw_key &&
1594                            tx_info->control.hw_key->keyidx != priv->wep_default_key_id &&
1595                            (tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
1596                             tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_WEP104)) {
1597                         action = do_wep;
1598                 }
1599         }
1600
1601         switch (action) {
1602         case do_probe:
1603                 /* An interesting FW "feature". Device filters probe responses.
1604                  * The easiest way to get it back is to convert
1605                  * probe request into WSM start_scan command.
1606                  */
1607                 pr_debug("[WSM] Convert probe request to scan.\n");
1608                 wsm_lock_tx_async(priv);
1609                 priv->pending_frame_id = __le32_to_cpu(wsm->packet_id);
1610                 if (queue_delayed_work(priv->workqueue,
1611                                        &priv->scan.probe_work, 0) <= 0)
1612                         wsm_unlock_tx(priv);
1613                 handled = true;
1614                 break;
1615         case do_drop:
1616                 pr_debug("[WSM] Drop frame (0x%.4X).\n", fctl);
1617                 BUG_ON(cw1200_queue_remove(queue,
1618                                            __le32_to_cpu(wsm->packet_id)));
1619                 handled = true;
1620                 break;
1621         case do_wep:
1622                 pr_debug("[WSM] Issue set_default_wep_key.\n");
1623                 wsm_lock_tx_async(priv);
1624                 priv->wep_default_key_id = tx_info->control.hw_key->keyidx;
1625                 priv->pending_frame_id = __le32_to_cpu(wsm->packet_id);
1626                 if (queue_work(priv->workqueue, &priv->wep_key_work) <= 0)
1627                         wsm_unlock_tx(priv);
1628                 handled = true;
1629                 break;
1630         case do_tx:
1631                 pr_debug("[WSM] Transmit frame.\n");
1632                 break;
1633         default:
1634                 /* Do nothing */
1635                 break;
1636         }
1637         return handled;
1638 }
1639
1640 static int cw1200_get_prio_queue(struct cw1200_common *priv,
1641                                  u32 link_id_map, int *total)
1642 {
1643         static const int urgent = BIT(CW1200_LINK_ID_AFTER_DTIM) |
1644                 BIT(CW1200_LINK_ID_UAPSD);
1645         struct wsm_edca_queue_params *edca;
1646         unsigned score, best = -1;
1647         int winner = -1;
1648         int queued;
1649         int i;
1650
1651         /* search for a winner using edca params */
1652         for (i = 0; i < 4; ++i) {
1653                 queued = cw1200_queue_get_num_queued(&priv->tx_queue[i],
1654                                 link_id_map);
1655                 if (!queued)
1656                         continue;
1657                 *total += queued;
1658                 edca = &priv->edca.params[i];
1659                 score = ((edca->aifns + edca->cwmin) << 16) +
1660                         ((edca->cwmax - edca->cwmin) *
1661                          (get_random_int() & 0xFFFF));
1662                 if (score < best && (winner < 0 || i != 3)) {
1663                         best = score;
1664                         winner = i;
1665                 }
1666         }
1667
1668         /* override winner if bursting */
1669         if (winner >= 0 && priv->tx_burst_idx >= 0 &&
1670             winner != priv->tx_burst_idx &&
1671             !cw1200_queue_get_num_queued(
1672                     &priv->tx_queue[winner],
1673                     link_id_map & urgent) &&
1674             cw1200_queue_get_num_queued(
1675                     &priv->tx_queue[priv->tx_burst_idx],
1676                     link_id_map))
1677                 winner = priv->tx_burst_idx;
1678
1679         return winner;
1680 }
1681
1682 static int wsm_get_tx_queue_and_mask(struct cw1200_common *priv,
1683                                      struct cw1200_queue **queue_p,
1684                                      u32 *tx_allowed_mask_p,
1685                                      bool *more)
1686 {
1687         int idx;
1688         u32 tx_allowed_mask;
1689         int total = 0;
1690
1691         /* Search for a queue with multicast frames buffered */
1692         if (priv->tx_multicast) {
1693                 tx_allowed_mask = BIT(CW1200_LINK_ID_AFTER_DTIM);
1694                 idx = cw1200_get_prio_queue(priv,
1695                                 tx_allowed_mask, &total);
1696                 if (idx >= 0) {
1697                         *more = total > 1;
1698                         goto found;
1699                 }
1700         }
1701
1702         /* Search for unicast traffic */
1703         tx_allowed_mask = ~priv->sta_asleep_mask;
1704         tx_allowed_mask |= BIT(CW1200_LINK_ID_UAPSD);
1705         if (priv->sta_asleep_mask) {
1706                 tx_allowed_mask |= priv->pspoll_mask;
1707                 tx_allowed_mask &= ~BIT(CW1200_LINK_ID_AFTER_DTIM);
1708         } else {
1709                 tx_allowed_mask |= BIT(CW1200_LINK_ID_AFTER_DTIM);
1710         }
1711         idx = cw1200_get_prio_queue(priv,
1712                         tx_allowed_mask, &total);
1713         if (idx < 0)
1714                 return -ENOENT;
1715
1716 found:
1717         *queue_p = &priv->tx_queue[idx];
1718         *tx_allowed_mask_p = tx_allowed_mask;
1719         return 0;
1720 }
1721
1722 int wsm_get_tx(struct cw1200_common *priv, u8 **data,
1723                size_t *tx_len, int *burst)
1724 {
1725         struct wsm_tx *wsm = NULL;
1726         struct ieee80211_tx_info *tx_info;
1727         struct cw1200_queue *queue = NULL;
1728         int queue_num;
1729         u32 tx_allowed_mask = 0;
1730         const struct cw1200_txpriv *txpriv = NULL;
1731         int count = 0;
1732
1733         /* More is used only for broadcasts. */
1734         bool more = false;
1735
1736 #ifdef CONFIG_CW1200_ITP
1737         count = cw1200_itp_get_tx(priv, data, tx_len, burst);
1738         if (count)
1739                 return count;
1740 #endif
1741
1742         if (priv->wsm_cmd.ptr) { /* CMD request */
1743                 ++count;
1744                 spin_lock(&priv->wsm_cmd.lock);
1745                 BUG_ON(!priv->wsm_cmd.ptr);
1746                 *data = priv->wsm_cmd.ptr;
1747                 *tx_len = priv->wsm_cmd.len;
1748                 *burst = 1;
1749                 spin_unlock(&priv->wsm_cmd.lock);
1750         } else {
1751                 for (;;) {
1752                         int ret;
1753
1754                         if (atomic_add_return(0, &priv->tx_lock))
1755                                 break;
1756
1757                         spin_lock_bh(&priv->ps_state_lock);
1758
1759                         ret = wsm_get_tx_queue_and_mask(priv, &queue,
1760                                                         &tx_allowed_mask, &more);
1761                         queue_num = queue - priv->tx_queue;
1762
1763                         if (priv->buffered_multicasts &&
1764                             (ret || !more) &&
1765                             (priv->tx_multicast || !priv->sta_asleep_mask)) {
1766                                 priv->buffered_multicasts = false;
1767                                 if (priv->tx_multicast) {
1768                                         priv->tx_multicast = false;
1769                                         queue_work(priv->workqueue,
1770                                                    &priv->multicast_stop_work);
1771                                 }
1772                         }
1773
1774                         spin_unlock_bh(&priv->ps_state_lock);
1775
1776                         if (ret)
1777                                 break;
1778
1779                         if (cw1200_queue_get(queue,
1780                                              tx_allowed_mask,
1781                                              &wsm, &tx_info, &txpriv))
1782                                 continue;
1783
1784                         if (wsm_handle_tx_data(priv, wsm,
1785                                                tx_info, txpriv, queue))
1786                                 continue;  /* Handled by WSM */
1787
1788                         wsm->hdr.id &= __cpu_to_le16(
1789                                 ~WSM_TX_LINK_ID(WSM_TX_LINK_ID_MAX));
1790                         wsm->hdr.id |= cpu_to_le16(
1791                                 WSM_TX_LINK_ID(txpriv->raw_link_id));
1792                         priv->pspoll_mask &= ~BIT(txpriv->raw_link_id);
1793
1794                         *data = (u8 *)wsm;
1795                         *tx_len = __le16_to_cpu(wsm->hdr.len);
1796
1797                         /* allow bursting if txop is set */
1798                         if (priv->edca.params[queue_num].txop_limit)
1799                                 *burst = min(*burst,
1800                                              (int)cw1200_queue_get_num_queued(queue, tx_allowed_mask) + 1);
1801                         else
1802                                 *burst = 1;
1803
1804                         /* store index of bursting queue */
1805                         if (*burst > 1)
1806                                 priv->tx_burst_idx = queue_num;
1807                         else
1808                                 priv->tx_burst_idx = -1;
1809
1810                         if (more) {
1811                                 struct ieee80211_hdr *hdr =
1812                                         (struct ieee80211_hdr *)
1813                                         &((u8 *)wsm)[txpriv->offset];
1814                                 /* more buffered multicast/broadcast frames
1815                                  *  ==> set MoreData flag in IEEE 802.11 header
1816                                  *  to inform PS STAs
1817                                  */
1818                                 hdr->frame_control |=
1819                                         cpu_to_le16(IEEE80211_FCTL_MOREDATA);
1820                         }
1821
1822                         pr_debug("[WSM] >>> 0x%.4X (%zu) %p %c\n",
1823                                  0x0004, *tx_len, *data,
1824                                  wsm->more ? 'M' : ' ');
1825                         ++count;
1826                         break;
1827                 }
1828         }
1829
1830         return count;
1831 }
1832
1833 void wsm_txed(struct cw1200_common *priv, u8 *data)
1834 {
1835         if (data == priv->wsm_cmd.ptr) {
1836                 spin_lock(&priv->wsm_cmd.lock);
1837                 priv->wsm_cmd.ptr = NULL;
1838                 spin_unlock(&priv->wsm_cmd.lock);
1839         }
1840 }
1841
1842 /* ******************************************************************** */
1843 /* WSM buffer                                                           */
1844
1845 void wsm_buf_init(struct wsm_buf *buf)
1846 {
1847         BUG_ON(buf->begin);
1848         buf->begin = kmalloc(FWLOAD_BLOCK_SIZE, GFP_KERNEL | GFP_DMA);
1849         buf->end = buf->begin ? &buf->begin[FWLOAD_BLOCK_SIZE] : buf->begin;
1850         wsm_buf_reset(buf);
1851 }
1852
1853 void wsm_buf_deinit(struct wsm_buf *buf)
1854 {
1855         kfree(buf->begin);
1856         buf->begin = buf->data = buf->end = NULL;
1857 }
1858
1859 static void wsm_buf_reset(struct wsm_buf *buf)
1860 {
1861         if (buf->begin) {
1862                 buf->data = &buf->begin[4];
1863                 *(u32 *)buf->begin = 0;
1864         } else {
1865                 buf->data = buf->begin;
1866         }
1867 }
1868
1869 static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size)
1870 {
1871         size_t pos = buf->data - buf->begin;
1872         size_t size = pos + extra_size;
1873
1874         size = round_up(size, FWLOAD_BLOCK_SIZE);
1875
1876         buf->begin = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
1877         if (buf->begin) {
1878                 buf->data = &buf->begin[pos];
1879                 buf->end = &buf->begin[size];
1880                 return 0;
1881         } else {
1882                 buf->end = buf->data = buf->begin;
1883                 return -ENOMEM;
1884         }
1885 }