bd193ae2178b636c0cbfdbdeeebf1a6868260c5a
[firefly-linux-kernel-4.4.55.git] / drivers / net / wimax / i2400m / control.c
1 /*
2  * Intel Wireless WiMAX Connection 2400m
3  * Miscellaneous control functions for managing the device
4  *
5  *
6  * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in
16  *     the documentation and/or other materials provided with the
17  *     distribution.
18  *   * Neither the name of Intel Corporation nor the names of its
19  *     contributors may be used to endorse or promote products derived
20  *     from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * Intel Corporation <linux-wimax@intel.com>
36  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
37  *  - Initial implementation
38  *
39  * This is a collection of functions used to control the device (plus
40  * a few helpers).
41  *
42  * There are utilities for handling TLV buffers, hooks on the device's
43  * reports to act on device changes of state [i2400m_report_hook()],
44  * on acks to commands [i2400m_msg_ack_hook()], a helper for sending
45  * commands to the device and blocking until a reply arrives
46  * [i2400m_msg_to_dev()], a few high level commands for manipulating
47  * the device state, powersving mode and configuration plus the
48  * routines to setup the device once communication is stablished with
49  * it [i2400m_dev_initialize()].
50  *
51  * ROADMAP
52  *
53  * i2400m_dev_initalize()       Called by i2400m_dev_start()
54  *   i2400m_set_init_config()
55  *   i2400m_cmd_get_state()
56  * i2400m_dev_shutdown()        Called by i2400m_dev_stop()
57  *   i2400m->bus_reset()
58  *
59  * i2400m_{cmd,get,set}_*()
60  *   i2400m_msg_to_dev()
61  *   i2400m_msg_check_status()
62  *
63  * i2400m_report_hook()         Called on reception of an event
64  *   i2400m_report_state_hook()
65  *     i2400m_tlv_buffer_walk()
66  *     i2400m_tlv_match()
67  *     i2400m_report_tlv_system_state()
68  *     i2400m_report_tlv_rf_switches_status()
69  *     i2400m_report_tlv_media_status()
70  *   i2400m_cmd_enter_powersave()
71  *
72  * i2400m_msg_ack_hook()        Called on reception of a reply to a
73  *                              command, get or set
74  */
75
76 #include <stdarg.h>
77 #include "i2400m.h"
78 #include <linux/kernel.h>
79 #include <linux/wimax/i2400m.h>
80
81
82 #define D_SUBMODULE control
83 #include "debug-levels.h"
84
85
86 /*
87  * Return if a TLV is of a give type and size
88  *
89  * @tlv_hdr: pointer to the TLV
90  * @tlv_type: type of the TLV we are looking for
91  * @tlv_size: expected size of the TLV we are looking for (if -1,
92  *            don't check the size). This includes the header
93  * Returns: 0 if the TLV matches
94  *          < 0 if it doesn't match at all
95  *          > 0 total TLV + payload size, if the type matches, but not
96  *              the size
97  */
98 static
99 ssize_t i2400m_tlv_match(const struct i2400m_tlv_hdr *tlv,
100                      enum i2400m_tlv tlv_type, ssize_t tlv_size)
101 {
102         if (le16_to_cpu(tlv->type) != tlv_type) /* Not our type? skip */
103                 return -1;
104         if (tlv_size != -1
105             && le16_to_cpu(tlv->length) + sizeof(*tlv) != tlv_size) {
106                 size_t size = le16_to_cpu(tlv->length) + sizeof(*tlv);
107                 printk(KERN_WARNING "W: tlv type 0x%x mismatched because of "
108                        "size (got %zu vs %zu expected)\n",
109                        tlv_type, size, tlv_size);
110                 return size;
111         }
112         return 0;
113 }
114
115
116 /*
117  * Given a buffer of TLVs, iterate over them
118  *
119  * @i2400m: device instance
120  * @tlv_buf: pointer to the beginning of the TLV buffer
121  * @buf_size: buffer size in bytes
122  * @tlv_pos: seek position; this is assumed to be a pointer returned
123  *           by i2400m_tlv_buffer_walk() [and thus, validated]. The
124  *           TLV returned will be the one following this one.
125  *
126  * Usage:
127  *
128  * tlv_itr = NULL;
129  * while (tlv_itr = i2400m_tlv_buffer_walk(i2400m, buf, size, tlv_itr))  {
130  *         ...
131  *         // Do stuff with tlv_itr, DON'T MODIFY IT
132  *         ...
133  * }
134  */
135 static
136 const struct i2400m_tlv_hdr *i2400m_tlv_buffer_walk(
137         struct i2400m *i2400m,
138         const void *tlv_buf, size_t buf_size,
139         const struct i2400m_tlv_hdr *tlv_pos)
140 {
141         struct device *dev = i2400m_dev(i2400m);
142         const struct i2400m_tlv_hdr *tlv_top = tlv_buf + buf_size;
143         size_t offset, length, avail_size;
144         unsigned type;
145
146         if (tlv_pos == NULL)    /* Take the first one? */
147                 tlv_pos = tlv_buf;
148         else                    /* Nope, the next one */
149                 tlv_pos = (void *) tlv_pos
150                         + le16_to_cpu(tlv_pos->length) + sizeof(*tlv_pos);
151         if (tlv_pos == tlv_top) {       /* buffer done */
152                 tlv_pos = NULL;
153                 goto error_beyond_end;
154         }
155         if (tlv_pos > tlv_top) {
156                 tlv_pos = NULL;
157                 WARN_ON(1);
158                 goto error_beyond_end;
159         }
160         offset = (void *) tlv_pos - (void *) tlv_buf;
161         avail_size = buf_size - offset;
162         if (avail_size < sizeof(*tlv_pos)) {
163                 dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], tlv @%zu: "
164                         "short header\n", tlv_buf, buf_size, offset);
165                 goto error_short_header;
166         }
167         type = le16_to_cpu(tlv_pos->type);
168         length = le16_to_cpu(tlv_pos->length);
169         if (avail_size < sizeof(*tlv_pos) + length) {
170                 dev_err(dev, "HW BUG? tlv_buf %p [%zu bytes], "
171                         "tlv type 0x%04x @%zu: "
172                         "short data (%zu bytes vs %zu needed)\n",
173                         tlv_buf, buf_size, type, offset, avail_size,
174                         sizeof(*tlv_pos) + length);
175                 goto error_short_header;
176         }
177 error_short_header:
178 error_beyond_end:
179         return tlv_pos;
180 }
181
182
183 /*
184  * Find a TLV in a buffer of sequential TLVs
185  *
186  * @i2400m: device descriptor
187  * @tlv_hdr: pointer to the first TLV in the sequence
188  * @size: size of the buffer in bytes; all TLVs are assumed to fit
189  *        fully in the buffer (otherwise we'll complain).
190  * @tlv_type: type of the TLV we are looking for
191  * @tlv_size: expected size of the TLV we are looking for (if -1,
192  *            don't check the size). This includes the header
193  *
194  * Returns: NULL if the TLV is not found, otherwise a pointer to
195  *          it. If the sizes don't match, an error is printed and NULL
196  *          returned.
197  */
198 static
199 const struct i2400m_tlv_hdr *i2400m_tlv_find(
200         struct i2400m *i2400m,
201         const struct i2400m_tlv_hdr *tlv_hdr, size_t size,
202         enum i2400m_tlv tlv_type, ssize_t tlv_size)
203 {
204         ssize_t match;
205         struct device *dev = i2400m_dev(i2400m);
206         const struct i2400m_tlv_hdr *tlv = NULL;
207         while ((tlv = i2400m_tlv_buffer_walk(i2400m, tlv_hdr, size, tlv))) {
208                 match = i2400m_tlv_match(tlv, tlv_type, tlv_size);
209                 if (match == 0)         /* found it :) */
210                         break;
211                 if (match > 0)
212                         dev_warn(dev, "TLV type 0x%04x found with size "
213                                  "mismatch (%zu vs %zu needed)\n",
214                                  tlv_type, match, tlv_size);
215         }
216         return tlv;
217 }
218
219
220 static const struct
221 {
222         char *msg;
223         int errno;
224 } ms_to_errno[I2400M_MS_MAX] = {
225         [I2400M_MS_DONE_OK] = { "", 0 },
226         [I2400M_MS_DONE_IN_PROGRESS] = { "", 0 },
227         [I2400M_MS_INVALID_OP] = { "invalid opcode", -ENOSYS },
228         [I2400M_MS_BAD_STATE] = { "invalid state", -EILSEQ },
229         [I2400M_MS_ILLEGAL_VALUE] = { "illegal value", -EINVAL },
230         [I2400M_MS_MISSING_PARAMS] = { "missing parameters", -ENOMSG },
231         [I2400M_MS_VERSION_ERROR] = { "bad version", -EIO },
232         [I2400M_MS_ACCESSIBILITY_ERROR] = { "accesibility error", -EIO },
233         [I2400M_MS_BUSY] = { "busy", -EBUSY },
234         [I2400M_MS_CORRUPTED_TLV] = { "corrupted TLV", -EILSEQ },
235         [I2400M_MS_UNINITIALIZED] = { "not unitialized", -EILSEQ },
236         [I2400M_MS_UNKNOWN_ERROR] = { "unknown error", -EIO },
237         [I2400M_MS_PRODUCTION_ERROR] = { "production error", -EIO },
238         [I2400M_MS_NO_RF] = { "no RF", -EIO },
239         [I2400M_MS_NOT_READY_FOR_POWERSAVE] =
240                 { "not ready for powersave", -EACCES },
241         [I2400M_MS_THERMAL_CRITICAL] = { "thermal critical", -EL3HLT },
242 };
243
244
245 /*
246  * i2400m_msg_check_status - translate a message's status code
247  *
248  * @i2400m: device descriptor
249  * @l3l4_hdr: message header
250  * @strbuf: buffer to place a formatted error message (unless NULL).
251  * @strbuf_size: max amount of available space; larger messages will
252  * be truncated.
253  *
254  * Returns: errno code corresponding to the status code in @l3l4_hdr
255  *          and a message in @strbuf describing the error.
256  */
257 int i2400m_msg_check_status(const struct i2400m_l3l4_hdr *l3l4_hdr,
258                             char *strbuf, size_t strbuf_size)
259 {
260         int result;
261         enum i2400m_ms status = le16_to_cpu(l3l4_hdr->status);
262         const char *str;
263
264         if (status == 0)
265                 return 0;
266         if (status > ARRAY_SIZE(ms_to_errno)) {
267                 str = "unknown status code";
268                 result = -EBADR;
269         } else {
270                 str = ms_to_errno[status].msg;
271                 result = ms_to_errno[status].errno;
272         }
273         if (strbuf)
274                 snprintf(strbuf, strbuf_size, "%s (%d)", str, status);
275         return result;
276 }
277
278
279 /*
280  * Act on a TLV System State reported by the device
281  *
282  * @i2400m: device descriptor
283  * @ss: validated System State TLV
284  */
285 static
286 void i2400m_report_tlv_system_state(struct i2400m *i2400m,
287                                     const struct i2400m_tlv_system_state *ss)
288 {
289         struct device *dev = i2400m_dev(i2400m);
290         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
291         enum i2400m_system_state i2400m_state = le32_to_cpu(ss->state);
292
293         d_fnstart(3, dev, "(i2400m %p ss %p [%u])\n", i2400m, ss, i2400m_state);
294
295         if (i2400m->state != i2400m_state) {
296                 i2400m->state = i2400m_state;
297                 wake_up_all(&i2400m->state_wq);
298         }
299         switch (i2400m_state) {
300         case I2400M_SS_UNINITIALIZED:
301         case I2400M_SS_INIT:
302         case I2400M_SS_CONFIG:
303         case I2400M_SS_PRODUCTION:
304                 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
305                 break;
306
307         case I2400M_SS_RF_OFF:
308         case I2400M_SS_RF_SHUTDOWN:
309                 wimax_state_change(wimax_dev, WIMAX_ST_RADIO_OFF);
310                 break;
311
312         case I2400M_SS_READY:
313         case I2400M_SS_STANDBY:
314         case I2400M_SS_SLEEPACTIVE:
315                 wimax_state_change(wimax_dev, WIMAX_ST_READY);
316                 break;
317
318         case I2400M_SS_CONNECTING:
319         case I2400M_SS_WIMAX_CONNECTED:
320                 wimax_state_change(wimax_dev, WIMAX_ST_READY);
321                 break;
322
323         case I2400M_SS_SCAN:
324         case I2400M_SS_OUT_OF_ZONE:
325                 wimax_state_change(wimax_dev, WIMAX_ST_SCANNING);
326                 break;
327
328         case I2400M_SS_IDLE:
329                 d_printf(1, dev, "entering BS-negotiated idle mode\n");
330         case I2400M_SS_DISCONNECTING:
331         case I2400M_SS_DATA_PATH_CONNECTED:
332                 wimax_state_change(wimax_dev, WIMAX_ST_CONNECTED);
333                 break;
334
335         default:
336                 /* Huh? just in case, shut it down */
337                 dev_err(dev, "HW BUG? unknown state %u: shutting down\n",
338                         i2400m_state);
339                 i2400m->bus_reset(i2400m, I2400M_RT_WARM);
340                 break;
341         };
342         d_fnend(3, dev, "(i2400m %p ss %p [%u]) = void\n",
343                 i2400m, ss, i2400m_state);
344 }
345
346
347 /*
348  * Parse and act on a TLV Media Status sent by the device
349  *
350  * @i2400m: device descriptor
351  * @ms: validated Media Status TLV
352  *
353  * This will set the carrier up on down based on the device's link
354  * report. This is done asides of what the WiMAX stack does based on
355  * the device's state as sometimes we need to do a link-renew (the BS
356  * wants us to renew a DHCP lease, for example).
357  *
358  * In fact, doc says that everytime we get a link-up, we should do a
359  * DHCP negotiation...
360  */
361 static
362 void i2400m_report_tlv_media_status(struct i2400m *i2400m,
363                                     const struct i2400m_tlv_media_status *ms)
364 {
365         struct device *dev = i2400m_dev(i2400m);
366         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
367         struct net_device *net_dev = wimax_dev->net_dev;
368         enum i2400m_media_status status = le32_to_cpu(ms->media_status);
369
370         d_fnstart(3, dev, "(i2400m %p ms %p [%u])\n", i2400m, ms, status);
371
372         switch (status) {
373         case I2400M_MEDIA_STATUS_LINK_UP:
374                 netif_carrier_on(net_dev);
375                 break;
376         case I2400M_MEDIA_STATUS_LINK_DOWN:
377                 netif_carrier_off(net_dev);
378                 break;
379         /*
380          * This is the network telling us we need to retrain the DHCP
381          * lease -- so far, we are trusting the WiMAX Network Service
382          * in user space to pick this up and poke the DHCP client.
383          */
384         case I2400M_MEDIA_STATUS_LINK_RENEW:
385                 netif_carrier_on(net_dev);
386                 break;
387         default:
388                 dev_err(dev, "HW BUG? unknown media status %u\n",
389                         status);
390         };
391         d_fnend(3, dev, "(i2400m %p ms %p [%u]) = void\n",
392                 i2400m, ms, status);
393 }
394
395
396 /*
397  * Process a TLV from a 'state report'
398  *
399  * @i2400m: device descriptor
400  * @tlv: pointer to the TLV header; it has been already validated for
401  *     consistent size.
402  * @tag: for error messages
403  *
404  * Act on the TLVs from a 'state report'.
405  */
406 static
407 void i2400m_report_state_parse_tlv(struct i2400m *i2400m,
408                                    const struct i2400m_tlv_hdr *tlv,
409                                    const char *tag)
410 {
411         struct device *dev = i2400m_dev(i2400m);
412         const struct i2400m_tlv_media_status *ms;
413         const struct i2400m_tlv_system_state *ss;
414         const struct i2400m_tlv_rf_switches_status *rfss;
415
416         if (0 == i2400m_tlv_match(tlv, I2400M_TLV_SYSTEM_STATE, sizeof(*ss))) {
417                 ss = container_of(tlv, typeof(*ss), hdr);
418                 d_printf(2, dev, "%s: system state TLV "
419                          "found (0x%04x), state 0x%08x\n",
420                          tag, I2400M_TLV_SYSTEM_STATE,
421                          le32_to_cpu(ss->state));
422                 i2400m_report_tlv_system_state(i2400m, ss);
423         }
424         if (0 == i2400m_tlv_match(tlv, I2400M_TLV_RF_STATUS, sizeof(*rfss))) {
425                 rfss = container_of(tlv, typeof(*rfss), hdr);
426                 d_printf(2, dev, "%s: RF status TLV "
427                          "found (0x%04x), sw 0x%02x hw 0x%02x\n",
428                          tag, I2400M_TLV_RF_STATUS,
429                          le32_to_cpu(rfss->sw_rf_switch),
430                          le32_to_cpu(rfss->hw_rf_switch));
431                 i2400m_report_tlv_rf_switches_status(i2400m, rfss);
432         }
433         if (0 == i2400m_tlv_match(tlv, I2400M_TLV_MEDIA_STATUS, sizeof(*ms))) {
434                 ms = container_of(tlv, typeof(*ms), hdr);
435                 d_printf(2, dev, "%s: Media Status TLV: %u\n",
436                          tag, le32_to_cpu(ms->media_status));
437                 i2400m_report_tlv_media_status(i2400m, ms);
438         }
439 }
440
441
442 /*
443  * Parse a 'state report' and extract information
444  *
445  * @i2400m: device descriptor
446  * @l3l4_hdr: pointer to message; it has been already validated for
447  *            consistent size.
448  * @size: size of the message (header + payload). The header length
449  *        declaration is assumed to be congruent with @size (as in
450  *        sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
451  *
452  * Walk over the TLVs in a report state and act on them.
453  */
454 static
455 void i2400m_report_state_hook(struct i2400m *i2400m,
456                               const struct i2400m_l3l4_hdr *l3l4_hdr,
457                               size_t size, const char *tag)
458 {
459         struct device *dev = i2400m_dev(i2400m);
460         const struct i2400m_tlv_hdr *tlv;
461         size_t tlv_size = le16_to_cpu(l3l4_hdr->length);
462
463         d_fnstart(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
464                   i2400m, l3l4_hdr, size, tag);
465         tlv = NULL;
466
467         while ((tlv = i2400m_tlv_buffer_walk(i2400m, &l3l4_hdr->pl,
468                                              tlv_size, tlv)))
469                 i2400m_report_state_parse_tlv(i2400m, tlv, tag);
470         d_fnend(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
471                 i2400m, l3l4_hdr, size, tag);
472 }
473
474
475 /*
476  * i2400m_report_hook - (maybe) act on a report
477  *
478  * @i2400m: device descriptor
479  * @l3l4_hdr: pointer to message; it has been already validated for
480  *            consistent size.
481  * @size: size of the message (header + payload). The header length
482  *        declaration is assumed to be congruent with @size (as in
483  *        sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
484  *
485  * Extract information we might need (like carrien on/off) from a
486  * device report.
487  */
488 void i2400m_report_hook(struct i2400m *i2400m,
489                         const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
490 {
491         struct device *dev = i2400m_dev(i2400m);
492         unsigned msg_type;
493
494         d_fnstart(3, dev, "(i2400m %p l3l4_hdr %p size %zu)\n",
495                   i2400m, l3l4_hdr, size);
496         /* Chew on the message, we might need some information from
497          * here */
498         msg_type = le16_to_cpu(l3l4_hdr->type);
499         switch (msg_type) {
500         case I2400M_MT_REPORT_STATE:    /* carrier detection... */
501                 i2400m_report_state_hook(i2400m,
502                                          l3l4_hdr, size, "REPORT STATE");
503                 break;
504         /* If the device is ready for power save, then ask it to do
505          * it. */
506         case I2400M_MT_REPORT_POWERSAVE_READY:  /* zzzzz */
507                 if (l3l4_hdr->status == cpu_to_le16(I2400M_MS_DONE_OK)) {
508                         d_printf(1, dev, "ready for powersave, requesting\n");
509                         i2400m_cmd_enter_powersave(i2400m);
510                 }
511                 break;
512         };
513         d_fnend(3, dev, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
514                 i2400m, l3l4_hdr, size);
515 }
516
517
518 /*
519  * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
520  *
521  * @i2400m: device descriptor
522  * @l3l4_hdr: pointer to message; it has been already validated for
523  *            consistent size.
524  * @size: size of the message
525  *
526  * Extract information we might need from acks to commands and act on
527  * it. This is akin to i2400m_report_hook(). Note most of this
528  * processing should be done in the function that calls the
529  * command. This is here for some cases where it can't happen...
530  */
531 void i2400m_msg_ack_hook(struct i2400m *i2400m,
532                          const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
533 {
534         int result;
535         struct device *dev = i2400m_dev(i2400m);
536         unsigned ack_type, ack_status;
537         char strerr[32];
538
539         /* Chew on the message, we might need some information from
540          * here */
541         ack_type = le16_to_cpu(l3l4_hdr->type);
542         ack_status = le16_to_cpu(l3l4_hdr->status);
543         switch (ack_type) {
544         case I2400M_MT_CMD_ENTER_POWERSAVE:
545                 /* This is just left here for the sake of example, as
546                  * the processing is done somewhere else. */
547                 if (0) {
548                         result = i2400m_msg_check_status(
549                                 l3l4_hdr, strerr, sizeof(strerr));
550                         if (result >= 0)
551                                 d_printf(1, dev, "ready for power save: %zd\n",
552                                          size);
553                 }
554                 break;
555         };
556         return;
557 }
558
559
560 /*
561  * i2400m_msg_size_check() - verify message size and header are congruent
562  *
563  * It is ok if the total message size is larger than the expected
564  * size, as there can be padding.
565  */
566 int i2400m_msg_size_check(struct i2400m *i2400m,
567                           const struct i2400m_l3l4_hdr *l3l4_hdr,
568                           size_t msg_size)
569 {
570         int result;
571         struct device *dev = i2400m_dev(i2400m);
572         size_t expected_size;
573         d_fnstart(4, dev, "(i2400m %p l3l4_hdr %p msg_size %zu)\n",
574                   i2400m, l3l4_hdr, msg_size);
575         if (msg_size < sizeof(*l3l4_hdr)) {
576                 dev_err(dev, "bad size for message header "
577                         "(expected at least %zu, got %zu)\n",
578                         (size_t) sizeof(*l3l4_hdr), msg_size);
579                 result = -EIO;
580                 goto error_hdr_size;
581         }
582         expected_size = le16_to_cpu(l3l4_hdr->length) + sizeof(*l3l4_hdr);
583         if (msg_size < expected_size) {
584                 dev_err(dev, "bad size for message code 0x%04x (expected %zu, "
585                         "got %zu)\n", le16_to_cpu(l3l4_hdr->type),
586                         expected_size, msg_size);
587                 result = -EIO;
588         } else
589                 result = 0;
590 error_hdr_size:
591         d_fnend(4, dev,
592                 "(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
593                 i2400m, l3l4_hdr, msg_size, result);
594         return result;
595 }
596
597
598
599 /*
600  * Cancel a wait for a command ACK
601  *
602  * @i2400m: device descriptor
603  * @code: [negative] errno code to cancel with (don't use
604  *     -EINPROGRESS)
605  *
606  * If there is an ack already filled out, free it.
607  */
608 void i2400m_msg_to_dev_cancel_wait(struct i2400m *i2400m, int code)
609 {
610         struct sk_buff *ack_skb;
611         unsigned long flags;
612
613         spin_lock_irqsave(&i2400m->rx_lock, flags);
614         ack_skb = i2400m->ack_skb;
615         if (ack_skb && !IS_ERR(ack_skb))
616                 kfree_skb(ack_skb);
617         i2400m->ack_skb = ERR_PTR(code);
618         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
619 }
620
621
622 /**
623  * i2400m_msg_to_dev - Send a control message to the device and get a response
624  *
625  * @i2400m: device descriptor
626  *
627  * @msg_skb: an skb  *
628  *
629  * @buf: pointer to the buffer containing the message to be sent; it
630  *           has to start with a &struct i2400M_l3l4_hdr and then
631  *           followed by the payload. Once this function returns, the
632  *           buffer can be reused.
633  *
634  * @buf_len: buffer size
635  *
636  * Returns:
637  *
638  * Pointer to skb containing the ack message. You need to check the
639  * pointer with IS_ERR(), as it might be an error code. Error codes
640  * could happen because:
641  *
642  *  - the message wasn't formatted correctly
643  *  - couldn't send the message
644  *  - failed waiting for a response
645  *  - the ack message wasn't formatted correctly
646  *
647  * The returned skb has been allocated with wimax_msg_to_user_alloc(),
648  * it contains the reponse in a netlink attribute and is ready to be
649  * passed up to user space with wimax_msg_to_user_send(). To access
650  * the payload and its length, use wimax_msg_{data,len}() on the skb.
651  *
652  * The skb has to be freed with kfree_skb() once done.
653  *
654  * Description:
655  *
656  * This function delivers a message/command to the device and waits
657  * for an ack to be received. The format is described in
658  * linux/wimax/i2400m.h. In summary, a command/get/set is followed by an
659  * ack.
660  *
661  * This function will not check the ack status, that's left up to the
662  * caller.  Once done with the ack skb, it has to be kfree_skb()ed.
663  *
664  * The i2400m handles only one message at the same time, thus we need
665  * the mutex to exclude other players.
666  *
667  * We write the message and then wait for an answer to come back. The
668  * RX path intercepts control messages and handles them in
669  * i2400m_rx_ctl(). Reports (notifications) are (maybe) processed
670  * locally and then forwarded (as needed) to user space on the WiMAX
671  * stack message pipe. Acks are saved and passed back to us through an
672  * skb in i2400m->ack_skb which is ready to be given to generic
673  * netlink if need be.
674  */
675 struct sk_buff *i2400m_msg_to_dev(struct i2400m *i2400m,
676                                   const void *buf, size_t buf_len)
677 {
678         int result;
679         struct device *dev = i2400m_dev(i2400m);
680         const struct i2400m_l3l4_hdr *msg_l3l4_hdr;
681         struct sk_buff *ack_skb;
682         const struct i2400m_l3l4_hdr *ack_l3l4_hdr;
683         size_t ack_len;
684         int ack_timeout;
685         unsigned msg_type;
686         unsigned long flags;
687
688         d_fnstart(3, dev, "(i2400m %p buf %p len %zu)\n",
689                   i2400m, buf, buf_len);
690
691         if (i2400m->boot_mode)
692                 return ERR_PTR(-ENODEV);
693
694         msg_l3l4_hdr = buf;
695         /* Check msg & payload consistency */
696         result = i2400m_msg_size_check(i2400m, msg_l3l4_hdr, buf_len);
697         if (result < 0)
698                 goto error_bad_msg;
699         msg_type = le16_to_cpu(msg_l3l4_hdr->type);
700         d_printf(1, dev, "CMD/GET/SET 0x%04x %zu bytes\n",
701                  msg_type, buf_len);
702         d_dump(2, dev, buf, buf_len);
703
704         /* Setup the completion, ack_skb ("we are waiting") and send
705          * the message to the device */
706         mutex_lock(&i2400m->msg_mutex);
707         spin_lock_irqsave(&i2400m->rx_lock, flags);
708         i2400m->ack_skb = ERR_PTR(-EINPROGRESS);
709         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
710         init_completion(&i2400m->msg_completion);
711         result = i2400m_tx(i2400m, buf, buf_len, I2400M_PT_CTRL);
712         if (result < 0) {
713                 dev_err(dev, "can't send message 0x%04x: %d\n",
714                         le16_to_cpu(msg_l3l4_hdr->type), result);
715                 goto error_tx;
716         }
717
718         /* Some commands take longer to execute because of crypto ops,
719          * so we give them some more leeway on timeout */
720         switch (msg_type) {
721         case I2400M_MT_GET_TLS_OPERATION_RESULT:
722         case I2400M_MT_CMD_SEND_EAP_RESPONSE:
723                 ack_timeout = 5 * HZ;
724                 break;
725         default:
726                 ack_timeout = HZ;
727         };
728
729         if (unlikely(i2400m->trace_msg_from_user))
730                 wimax_msg(&i2400m->wimax_dev, "echo", buf, buf_len, GFP_KERNEL);
731         /* The RX path in rx.c will put any response for this message
732          * in i2400m->ack_skb and wake us up. If we cancel the wait,
733          * we need to change the value of i2400m->ack_skb to something
734          * not -EINPROGRESS so RX knows there is no one waiting. */
735         result = wait_for_completion_interruptible_timeout(
736                 &i2400m->msg_completion, ack_timeout);
737         if (result == 0) {
738                 dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
739                         msg_type);
740                 result = -ETIMEDOUT;
741                 i2400m_msg_to_dev_cancel_wait(i2400m, result);
742                 goto error_wait_for_completion;
743         } else if (result < 0) {
744                 dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
745                         msg_type, result);
746                 i2400m_msg_to_dev_cancel_wait(i2400m, result);
747                 goto error_wait_for_completion;
748         }
749
750         /* Pull out the ack data from i2400m->ack_skb -- see if it is
751          * an error and act accordingly */
752         spin_lock_irqsave(&i2400m->rx_lock, flags);
753         ack_skb = i2400m->ack_skb;
754         if (IS_ERR(ack_skb))
755                 result = PTR_ERR(ack_skb);
756         else
757                 result = 0;
758         i2400m->ack_skb = NULL;
759         spin_unlock_irqrestore(&i2400m->rx_lock, flags);
760         if (result < 0)
761                 goto error_ack_status;
762         ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
763
764         /* Check the ack and deliver it if it is ok */
765         if (unlikely(i2400m->trace_msg_from_user))
766                 wimax_msg(&i2400m->wimax_dev, "echo",
767                           ack_l3l4_hdr, ack_len, GFP_KERNEL);
768         result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
769         if (result < 0) {
770                 dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
771                         msg_type, result);
772                 goto error_bad_ack_len;
773         }
774         if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
775                 dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
776                         le16_to_cpu(ack_l3l4_hdr->type), msg_type);
777                 result = -EIO;
778                 goto error_bad_ack_type;
779         }
780         i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
781         mutex_unlock(&i2400m->msg_mutex);
782         d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
783                 i2400m, buf, buf_len, ack_skb);
784         return ack_skb;
785
786 error_bad_ack_type:
787 error_bad_ack_len:
788         kfree_skb(ack_skb);
789 error_ack_status:
790 error_wait_for_completion:
791 error_tx:
792         mutex_unlock(&i2400m->msg_mutex);
793 error_bad_msg:
794         d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
795                 i2400m, buf, buf_len, result);
796         return ERR_PTR(result);
797 }
798
799
800 /*
801  * Definitions for the Enter Power Save command
802  *
803  * The Enter Power Save command requests the device to go into power
804  * saving mode. The device will ack or nak the command depending on it
805  * being ready for it. If it acks, we tell the USB subsystem to
806  *
807  * As well, the device might request to go into power saving mode by
808  * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
809  * this command. The hookups in the RX coder allow
810  */
811 enum {
812         I2400M_WAKEUP_ENABLED  = 0x01,
813         I2400M_WAKEUP_DISABLED = 0x02,
814         I2400M_TLV_TYPE_WAKEUP_MODE = 144,
815 };
816
817 struct i2400m_cmd_enter_power_save {
818         struct i2400m_l3l4_hdr hdr;
819         struct i2400m_tlv_hdr tlv;
820         __le32 val;
821 } __attribute__((packed));
822
823
824 /*
825  * Request entering power save
826  *
827  * This command is (mainly) executed when the device indicates that it
828  * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
829  */
830 int i2400m_cmd_enter_powersave(struct i2400m *i2400m)
831 {
832         int result;
833         struct device *dev = i2400m_dev(i2400m);
834         struct sk_buff *ack_skb;
835         struct i2400m_cmd_enter_power_save *cmd;
836         char strerr[32];
837
838         result = -ENOMEM;
839         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
840         if (cmd == NULL)
841                 goto error_alloc;
842         cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
843         cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
844         cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
845         cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
846         cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
847         cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
848
849         ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
850         result = PTR_ERR(ack_skb);
851         if (IS_ERR(ack_skb)) {
852                 dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
853                         result);
854                 goto error_msg_to_dev;
855         }
856         result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
857                                          strerr, sizeof(strerr));
858         if (result == -EACCES)
859                 d_printf(1, dev, "Cannot enter power save mode\n");
860         else if (result < 0)
861                 dev_err(dev, "'Enter power save' (0x%04x) command failed: "
862                         "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
863                         result, strerr);
864         else
865                 d_printf(1, dev, "device ready to power save\n");
866         kfree_skb(ack_skb);
867 error_msg_to_dev:
868         kfree(cmd);
869 error_alloc:
870         return result;
871 }
872 EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
873
874
875 /*
876  * Definitions for getting device information
877  */
878 enum {
879         I2400M_TLV_DETAILED_DEVICE_INFO = 140
880 };
881
882 /**
883  * i2400m_get_device_info - Query the device for detailed device information
884  *
885  * @i2400m: device descriptor
886  *
887  * Returns: an skb whose skb->data points to a 'struct
888  *    i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
889  *    skb is *guaranteed* to contain the whole TLV data structure.
890  *
891  *    On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
892  *    code.
893  */
894 struct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
895 {
896         int result;
897         struct device *dev = i2400m_dev(i2400m);
898         struct sk_buff *ack_skb;
899         struct i2400m_l3l4_hdr *cmd;
900         const struct i2400m_l3l4_hdr *ack;
901         size_t ack_len;
902         const struct i2400m_tlv_hdr *tlv;
903         const struct i2400m_tlv_detailed_device_info *ddi;
904         char strerr[32];
905
906         ack_skb = ERR_PTR(-ENOMEM);
907         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
908         if (cmd == NULL)
909                 goto error_alloc;
910         cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
911         cmd->length = 0;
912         cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
913
914         ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
915         if (IS_ERR(ack_skb)) {
916                 dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
917                         PTR_ERR(ack_skb));
918                 goto error_msg_to_dev;
919         }
920         ack = wimax_msg_data_len(ack_skb, &ack_len);
921         result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
922         if (result < 0) {
923                 dev_err(dev, "'get device info' (0x%04x) command failed: "
924                         "%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
925                         strerr);
926                 goto error_cmd_failed;
927         }
928         tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
929                               I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
930         if (tlv == NULL) {
931                 dev_err(dev, "GET DEVICE INFO: "
932                         "detailed device info TLV not found (0x%04x)\n",
933                         I2400M_TLV_DETAILED_DEVICE_INFO);
934                 result = -EIO;
935                 goto error_no_tlv;
936         }
937         skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
938 error_msg_to_dev:
939         kfree(cmd);
940 error_alloc:
941         return ack_skb;
942
943 error_no_tlv:
944 error_cmd_failed:
945         kfree_skb(ack_skb);
946         kfree(cmd);
947         return ERR_PTR(result);
948 }
949
950
951 /* Firmware interface versions we support */
952 enum {
953         I2400M_HDIv_MAJOR = 9,
954         I2400M_HDIv_MINOR = 1,
955         I2400M_HDIv_MINOR_2 = 2,
956 };
957
958
959 /**
960  * i2400m_firmware_check - check firmware versions are compatible with
961  * the driver
962  *
963  * @i2400m: device descriptor
964  *
965  * Returns: 0 if ok, < 0 errno code an error and a message in the
966  *    kernel log.
967  *
968  * Long function, but quite simple; first chunk launches the command
969  * and double checks the reply for the right TLV. Then we process the
970  * TLV (where the meat is).
971  *
972  * Once we process the TLV that gives us the firmware's interface
973  * version, we encode it and save it in i2400m->fw_version for future
974  * reference.
975  */
976 int i2400m_firmware_check(struct i2400m *i2400m)
977 {
978         int result;
979         struct device *dev = i2400m_dev(i2400m);
980         struct sk_buff *ack_skb;
981         struct i2400m_l3l4_hdr *cmd;
982         const struct i2400m_l3l4_hdr *ack;
983         size_t ack_len;
984         const struct i2400m_tlv_hdr *tlv;
985         const struct i2400m_tlv_l4_message_versions *l4mv;
986         char strerr[32];
987         unsigned major, minor, branch;
988
989         result = -ENOMEM;
990         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
991         if (cmd == NULL)
992                 goto error_alloc;
993         cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
994         cmd->length = 0;
995         cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
996
997         ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
998         if (IS_ERR(ack_skb)) {
999                 result = PTR_ERR(ack_skb);
1000                 dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
1001                         result);
1002                 goto error_msg_to_dev;
1003         }
1004         ack = wimax_msg_data_len(ack_skb, &ack_len);
1005         result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1006         if (result < 0) {
1007                 dev_err(dev, "'get lm version' (0x%04x) command failed: "
1008                         "%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
1009                         strerr);
1010                 goto error_cmd_failed;
1011         }
1012         tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
1013                               I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
1014         if (tlv == NULL) {
1015                 dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
1016                         I2400M_TLV_L4_MESSAGE_VERSIONS);
1017                 result = -EIO;
1018                 goto error_no_tlv;
1019         }
1020         l4mv = container_of(tlv, typeof(*l4mv), hdr);
1021         major = le16_to_cpu(l4mv->major);
1022         minor = le16_to_cpu(l4mv->minor);
1023         branch = le16_to_cpu(l4mv->branch);
1024         result = -EINVAL;
1025         if (major != I2400M_HDIv_MAJOR) {
1026                 dev_err(dev, "unsupported major fw version "
1027                         "%u.%u.%u\n", major, minor, branch);
1028                 goto error_bad_major;
1029         }
1030         result = 0;
1031         if (minor < I2400M_HDIv_MINOR_2 && minor > I2400M_HDIv_MINOR)
1032                 dev_warn(dev, "untested minor fw version %u.%u.%u\n",
1033                          major, minor, branch);
1034         /* Yes, we ignore the branch -- we don't have to track it */
1035         i2400m->fw_version = major << 16 | minor;
1036         dev_info(dev, "firmware interface version %u.%u.%u\n",
1037                  major, minor, branch);
1038 error_bad_major:
1039 error_no_tlv:
1040 error_cmd_failed:
1041         kfree_skb(ack_skb);
1042 error_msg_to_dev:
1043         kfree(cmd);
1044 error_alloc:
1045         return result;
1046 }
1047
1048
1049 /*
1050  * Send an DoExitIdle command to the device to ask it to go out of
1051  * basestation-idle mode.
1052  *
1053  * @i2400m: device descriptor
1054  *
1055  * This starts a renegotiation with the basestation that might involve
1056  * another crypto handshake with user space.
1057  *
1058  * Returns: 0 if ok, < 0 errno code on error.
1059  */
1060 int i2400m_cmd_exit_idle(struct i2400m *i2400m)
1061 {
1062         int result;
1063         struct device *dev = i2400m_dev(i2400m);
1064         struct sk_buff *ack_skb;
1065         struct i2400m_l3l4_hdr *cmd;
1066         char strerr[32];
1067
1068         result = -ENOMEM;
1069         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1070         if (cmd == NULL)
1071                 goto error_alloc;
1072         cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
1073         cmd->length = 0;
1074         cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1075
1076         ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1077         result = PTR_ERR(ack_skb);
1078         if (IS_ERR(ack_skb)) {
1079                 dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
1080                         result);
1081                 goto error_msg_to_dev;
1082         }
1083         result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1084                                          strerr, sizeof(strerr));
1085         kfree_skb(ack_skb);
1086 error_msg_to_dev:
1087         kfree(cmd);
1088 error_alloc:
1089         return result;
1090
1091 }
1092
1093
1094 /*
1095  * Query the device for its state, update the WiMAX stack's idea of it
1096  *
1097  * @i2400m: device descriptor
1098  *
1099  * Returns: 0 if ok, < 0 errno code on error.
1100  *
1101  * Executes a 'Get State' command and parses the returned
1102  * TLVs.
1103  *
1104  * Because this is almost identical to a 'Report State', we use
1105  * i2400m_report_state_hook() to parse the answer. This will set the
1106  * carrier state, as well as the RF Kill switches state.
1107  */
1108 int i2400m_cmd_get_state(struct i2400m *i2400m)
1109 {
1110         int result;
1111         struct device *dev = i2400m_dev(i2400m);
1112         struct sk_buff *ack_skb;
1113         struct i2400m_l3l4_hdr *cmd;
1114         const struct i2400m_l3l4_hdr *ack;
1115         size_t ack_len;
1116         char strerr[32];
1117
1118         result = -ENOMEM;
1119         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1120         if (cmd == NULL)
1121                 goto error_alloc;
1122         cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
1123         cmd->length = 0;
1124         cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1125
1126         ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1127         if (IS_ERR(ack_skb)) {
1128                 dev_err(dev, "Failed to issue 'get state' command: %ld\n",
1129                         PTR_ERR(ack_skb));
1130                 result = PTR_ERR(ack_skb);
1131                 goto error_msg_to_dev;
1132         }
1133         ack = wimax_msg_data_len(ack_skb, &ack_len);
1134         result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1135         if (result < 0) {
1136                 dev_err(dev, "'get state' (0x%04x) command failed: "
1137                         "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1138                 goto error_cmd_failed;
1139         }
1140         i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
1141                                  "GET STATE");
1142         result = 0;
1143         kfree_skb(ack_skb);
1144 error_cmd_failed:
1145 error_msg_to_dev:
1146         kfree(cmd);
1147 error_alloc:
1148         return result;
1149 }
1150 EXPORT_SYMBOL_GPL(i2400m_cmd_get_state);
1151
1152
1153 /**
1154  * Set basic configuration settings
1155  *
1156  * @i2400m: device descriptor
1157  * @args: array of pointers to the TLV headers to send for
1158  *     configuration (each followed by its payload).
1159  *     TLV headers and payloads must be properly initialized, with the
1160  *     right endianess (LE).
1161  * @arg_size: number of pointers in the @args array
1162  */
1163 int i2400m_set_init_config(struct i2400m *i2400m,
1164                            const struct i2400m_tlv_hdr **arg, size_t args)
1165 {
1166         int result;
1167         struct device *dev = i2400m_dev(i2400m);
1168         struct sk_buff *ack_skb;
1169         struct i2400m_l3l4_hdr *cmd;
1170         char strerr[32];
1171         unsigned argc, argsize, tlv_size;
1172         const struct i2400m_tlv_hdr *tlv_hdr;
1173         void *buf, *itr;
1174
1175         d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
1176         result = 0;
1177         if (args == 0)
1178                 goto none;
1179         /* Compute the size of all the TLVs, so we can alloc a
1180          * contiguous command block to copy them. */
1181         argsize = 0;
1182         for (argc = 0; argc < args; argc++) {
1183                 tlv_hdr = arg[argc];
1184                 argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1185         }
1186         WARN_ON(argc >= 9);     /* As per hw spec */
1187
1188         /* Alloc the space for the command and TLVs*/
1189         result = -ENOMEM;
1190         buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
1191         if (buf == NULL)
1192                 goto error_alloc;
1193         cmd = buf;
1194         cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
1195         cmd->length = cpu_to_le16(argsize);
1196         cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
1197
1198         /* Copy the TLVs */
1199         itr = buf + sizeof(*cmd);
1200         for (argc = 0; argc < args; argc++) {
1201                 tlv_hdr = arg[argc];
1202                 tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
1203                 memcpy(itr, tlv_hdr, tlv_size);
1204                 itr += tlv_size;
1205         }
1206
1207         /* Send the message! */
1208         ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
1209         result = PTR_ERR(ack_skb);
1210         if (IS_ERR(ack_skb)) {
1211                 dev_err(dev, "Failed to issue 'init config' command: %d\n",
1212                         result);
1213
1214                 goto error_msg_to_dev;
1215         }
1216         result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
1217                                          strerr, sizeof(strerr));
1218         if (result < 0)
1219                 dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
1220                         I2400M_MT_SET_INIT_CONFIG, result, strerr);
1221         kfree_skb(ack_skb);
1222 error_msg_to_dev:
1223         kfree(buf);
1224 error_alloc:
1225 none:
1226         d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
1227                 i2400m, arg, args, result);
1228         return result;
1229
1230 }
1231 EXPORT_SYMBOL_GPL(i2400m_set_init_config);
1232
1233
1234 /**
1235  * i2400m_set_idle_timeout - Set the device's idle mode timeout
1236  *
1237  * @i2400m: i2400m device descriptor
1238  *
1239  * @msecs: milliseconds for the timeout to enter idle mode. Between
1240  *     100 to 300000 (5m); 0 to disable. In increments of 100.
1241  *
1242  * After this @msecs of the link being idle (no data being sent or
1243  * received), the device will negotiate with the basestation entering
1244  * idle mode for saving power. The connection is maintained, but
1245  * getting out of it (done in tx.c) will require some negotiation,
1246  * possible crypto re-handshake and a possible DHCP re-lease.
1247  *
1248  * Only available if fw_version >= 0x00090002.
1249  *
1250  * Returns: 0 if ok, < 0 errno code on error.
1251  */
1252 int i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
1253 {
1254         int result;
1255         struct device *dev = i2400m_dev(i2400m);
1256         struct sk_buff *ack_skb;
1257         struct {
1258                 struct i2400m_l3l4_hdr hdr;
1259                 struct i2400m_tlv_config_idle_timeout cit;
1260         } *cmd;
1261         const struct i2400m_l3l4_hdr *ack;
1262         size_t ack_len;
1263         char strerr[32];
1264
1265         result = -ENOSYS;
1266         if (i2400m_le_v1_3(i2400m))
1267                 goto error_alloc;
1268         result = -ENOMEM;
1269         cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1270         if (cmd == NULL)
1271                 goto error_alloc;
1272         cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
1273         cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
1274         cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
1275
1276         cmd->cit.hdr.type =
1277                 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1278         cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
1279         cmd->cit.timeout = cpu_to_le32(msecs);
1280
1281         ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
1282         if (IS_ERR(ack_skb)) {
1283                 dev_err(dev, "Failed to issue 'set idle timeout' command: "
1284                         "%ld\n", PTR_ERR(ack_skb));
1285                 result = PTR_ERR(ack_skb);
1286                 goto error_msg_to_dev;
1287         }
1288         ack = wimax_msg_data_len(ack_skb, &ack_len);
1289         result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
1290         if (result < 0) {
1291                 dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
1292                         "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
1293                 goto error_cmd_failed;
1294         }
1295         result = 0;
1296         kfree_skb(ack_skb);
1297 error_cmd_failed:
1298 error_msg_to_dev:
1299         kfree(cmd);
1300 error_alloc:
1301         return result;
1302 }
1303
1304
1305 /**
1306  * i2400m_dev_initialize - Initialize the device once communications are ready
1307  *
1308  * @i2400m: device descriptor
1309  *
1310  * Returns: 0 if ok, < 0 errno code on error.
1311  *
1312  * Configures the device to work the way we like it.
1313  *
1314  * At the point of this call, the device is registered with the WiMAX
1315  * and netdev stacks, firmware is uploaded and we can talk to the
1316  * device normally.
1317  */
1318 int i2400m_dev_initialize(struct i2400m *i2400m)
1319 {
1320         int result;
1321         struct device *dev = i2400m_dev(i2400m);
1322         struct i2400m_tlv_config_idle_parameters idle_params;
1323         struct i2400m_tlv_config_idle_timeout idle_timeout;
1324         struct i2400m_tlv_config_d2h_data_format df;
1325         struct i2400m_tlv_config_dl_host_reorder dlhr;
1326         const struct i2400m_tlv_hdr *args[9];
1327         unsigned argc = 0;
1328
1329         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
1330         /* Disable idle mode? (enabled by default) */
1331         if (i2400m_idle_mode_disabled) {
1332                 if (i2400m_le_v1_3(i2400m)) {
1333                         idle_params.hdr.type =
1334                                 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
1335                         idle_params.hdr.length = cpu_to_le16(
1336                                 sizeof(idle_params) - sizeof(idle_params.hdr));
1337                         idle_params.idle_timeout = 0;
1338                         idle_params.idle_paging_interval = 0;
1339                         args[argc++] = &idle_params.hdr;
1340                 } else {
1341                         idle_timeout.hdr.type =
1342                                 cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
1343                         idle_timeout.hdr.length = cpu_to_le16(
1344                                 sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
1345                         idle_timeout.timeout = 0;
1346                         args[argc++] = &idle_timeout.hdr;
1347                 }
1348         }
1349         if (i2400m_ge_v1_4(i2400m)) {
1350                 /* Enable extended RX data format? */
1351                 df.hdr.type =
1352                         cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
1353                 df.hdr.length = cpu_to_le16(
1354                         sizeof(df) - sizeof(df.hdr));
1355                 df.format = 1;
1356                 args[argc++] = &df.hdr;
1357
1358                 /* Enable RX data reordering?
1359                  * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
1360                 if (i2400m->rx_reorder) {
1361                         dlhr.hdr.type =
1362                                 cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
1363                         dlhr.hdr.length = cpu_to_le16(
1364                                 sizeof(dlhr) - sizeof(dlhr.hdr));
1365                         dlhr.reorder = 1;
1366                         args[argc++] = &dlhr.hdr;
1367                 }
1368         }
1369         result = i2400m_set_init_config(i2400m, args, argc);
1370         if (result < 0)
1371                 goto error;
1372         /*
1373          * Update state: Here it just calls a get state; parsing the
1374          * result (System State TLV and RF Status TLV [done in the rx
1375          * path hooks]) will set the hardware and software RF-Kill
1376          * status.
1377          */
1378         result = i2400m_cmd_get_state(i2400m);
1379 error:
1380         if (result < 0)
1381                 dev_err(dev, "failed to initialize the device: %d\n", result);
1382         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
1383         return result;
1384 }
1385
1386
1387 /**
1388  * i2400m_dev_shutdown - Shutdown a running device
1389  *
1390  * @i2400m: device descriptor
1391  *
1392  * Gracefully stops the device, moving it to the lowest power
1393  * consumption state possible.
1394  */
1395 void i2400m_dev_shutdown(struct i2400m *i2400m)
1396 {
1397         int result = -ENODEV;
1398         struct device *dev = i2400m_dev(i2400m);
1399
1400         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
1401         result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
1402         d_fnend(3, dev, "(i2400m %p) = void [%d]\n", i2400m, result);
1403         return;
1404 }