control.c 42 KB

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