control.c 43 KB

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