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