control.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. * Parse a 'state report' and extract carrier on/off information
  373. *
  374. * @i2400m: device descriptor
  375. * @l3l4_hdr: pointer to message; it has been already validated for
  376. * consistent size.
  377. * @size: size of the message (header + payload). The header length
  378. * declaration is assumed to be congruent with @size (as in
  379. * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
  380. *
  381. * Extract from the report state the system state TLV and infer from
  382. * there if we have a carrier or not. Update our local state and tell
  383. * netdev.
  384. *
  385. * When setting the carrier, it's fine to set OFF twice (for example),
  386. * as netif_carrier_off() will not generate two OFF events (just on
  387. * the transitions).
  388. */
  389. static
  390. void i2400m_report_state_hook(struct i2400m *i2400m,
  391. const struct i2400m_l3l4_hdr *l3l4_hdr,
  392. size_t size, const char *tag)
  393. {
  394. struct device *dev = i2400m_dev(i2400m);
  395. const struct i2400m_tlv_hdr *tlv;
  396. const struct i2400m_tlv_system_state *ss;
  397. const struct i2400m_tlv_rf_switches_status *rfss;
  398. const struct i2400m_tlv_media_status *ms;
  399. size_t tlv_size = le16_to_cpu(l3l4_hdr->length);
  400. d_fnstart(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s)\n",
  401. i2400m, l3l4_hdr, size, tag);
  402. tlv = NULL;
  403. while ((tlv = i2400m_tlv_buffer_walk(i2400m, &l3l4_hdr->pl,
  404. tlv_size, tlv))) {
  405. if (0 == i2400m_tlv_match(tlv, I2400M_TLV_SYSTEM_STATE,
  406. sizeof(*ss))) {
  407. ss = container_of(tlv, typeof(*ss), hdr);
  408. d_printf(2, dev, "%s: system state TLV "
  409. "found (0x%04x), state 0x%08x\n",
  410. tag, I2400M_TLV_SYSTEM_STATE,
  411. le32_to_cpu(ss->state));
  412. i2400m_report_tlv_system_state(i2400m, ss);
  413. }
  414. if (0 == i2400m_tlv_match(tlv, I2400M_TLV_RF_STATUS,
  415. sizeof(*rfss))) {
  416. rfss = container_of(tlv, typeof(*rfss), hdr);
  417. d_printf(2, dev, "%s: RF status TLV "
  418. "found (0x%04x), sw 0x%02x hw 0x%02x\n",
  419. tag, I2400M_TLV_RF_STATUS,
  420. le32_to_cpu(rfss->sw_rf_switch),
  421. le32_to_cpu(rfss->hw_rf_switch));
  422. i2400m_report_tlv_rf_switches_status(i2400m, rfss);
  423. }
  424. if (0 == i2400m_tlv_match(tlv, I2400M_TLV_MEDIA_STATUS,
  425. sizeof(*ms))) {
  426. ms = container_of(tlv, typeof(*ms), hdr);
  427. d_printf(2, dev, "%s: Media Status TLV: %u\n",
  428. tag, le32_to_cpu(ms->media_status));
  429. i2400m_report_tlv_media_status(i2400m, ms);
  430. }
  431. }
  432. d_fnend(4, dev, "(i2400m %p, l3l4_hdr %p, size %zu, %s) = void\n",
  433. i2400m, l3l4_hdr, size, tag);
  434. }
  435. /*
  436. * i2400m_report_hook - (maybe) act on a report
  437. *
  438. * @i2400m: device descriptor
  439. * @l3l4_hdr: pointer to message; it has been already validated for
  440. * consistent size.
  441. * @size: size of the message (header + payload). The header length
  442. * declaration is assumed to be congruent with @size (as in
  443. * sizeof(*l3l4_hdr) + l3l4_hdr->length == size)
  444. *
  445. * Extract information we might need (like carrien on/off) from a
  446. * device report.
  447. */
  448. void i2400m_report_hook(struct i2400m *i2400m,
  449. const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
  450. {
  451. struct device *dev = i2400m_dev(i2400m);
  452. unsigned msg_type;
  453. d_fnstart(3, dev, "(i2400m %p l3l4_hdr %p size %zu)\n",
  454. i2400m, l3l4_hdr, size);
  455. /* Chew on the message, we might need some information from
  456. * here */
  457. msg_type = le16_to_cpu(l3l4_hdr->type);
  458. switch (msg_type) {
  459. case I2400M_MT_REPORT_STATE: /* carrier detection... */
  460. i2400m_report_state_hook(i2400m,
  461. l3l4_hdr, size, "REPORT STATE");
  462. break;
  463. /* If the device is ready for power save, then ask it to do
  464. * it. */
  465. case I2400M_MT_REPORT_POWERSAVE_READY: /* zzzzz */
  466. if (l3l4_hdr->status == cpu_to_le16(I2400M_MS_DONE_OK)) {
  467. d_printf(1, dev, "ready for powersave, requesting\n");
  468. i2400m_cmd_enter_powersave(i2400m);
  469. }
  470. break;
  471. };
  472. d_fnend(3, dev, "(i2400m %p l3l4_hdr %p size %zu) = void\n",
  473. i2400m, l3l4_hdr, size);
  474. }
  475. /*
  476. * i2400m_msg_ack_hook - process cmd/set/get ack for internal status
  477. *
  478. * @i2400m: device descriptor
  479. * @l3l4_hdr: pointer to message; it has been already validated for
  480. * consistent size.
  481. * @size: size of the message
  482. *
  483. * Extract information we might need from acks to commands and act on
  484. * it. This is akin to i2400m_report_hook(). Note most of this
  485. * processing should be done in the function that calls the
  486. * command. This is here for some cases where it can't happen...
  487. */
  488. void i2400m_msg_ack_hook(struct i2400m *i2400m,
  489. const struct i2400m_l3l4_hdr *l3l4_hdr, size_t size)
  490. {
  491. int result;
  492. struct device *dev = i2400m_dev(i2400m);
  493. unsigned ack_type, ack_status;
  494. char strerr[32];
  495. /* Chew on the message, we might need some information from
  496. * here */
  497. ack_type = le16_to_cpu(l3l4_hdr->type);
  498. ack_status = le16_to_cpu(l3l4_hdr->status);
  499. switch (ack_type) {
  500. case I2400M_MT_CMD_ENTER_POWERSAVE:
  501. /* This is just left here for the sake of example, as
  502. * the processing is done somewhere else. */
  503. if (0) {
  504. result = i2400m_msg_check_status(
  505. l3l4_hdr, strerr, sizeof(strerr));
  506. if (result >= 0)
  507. d_printf(1, dev, "ready for power save: %zd\n",
  508. size);
  509. }
  510. break;
  511. };
  512. return;
  513. }
  514. /*
  515. * i2400m_msg_size_check() - verify message size and header are congruent
  516. *
  517. * It is ok if the total message size is larger than the expected
  518. * size, as there can be padding.
  519. */
  520. int i2400m_msg_size_check(struct i2400m *i2400m,
  521. const struct i2400m_l3l4_hdr *l3l4_hdr,
  522. size_t msg_size)
  523. {
  524. int result;
  525. struct device *dev = i2400m_dev(i2400m);
  526. size_t expected_size;
  527. d_fnstart(4, dev, "(i2400m %p l3l4_hdr %p msg_size %zu)\n",
  528. i2400m, l3l4_hdr, msg_size);
  529. if (msg_size < sizeof(*l3l4_hdr)) {
  530. dev_err(dev, "bad size for message header "
  531. "(expected at least %zu, got %zu)\n",
  532. (size_t) sizeof(*l3l4_hdr), msg_size);
  533. result = -EIO;
  534. goto error_hdr_size;
  535. }
  536. expected_size = le16_to_cpu(l3l4_hdr->length) + sizeof(*l3l4_hdr);
  537. if (msg_size < expected_size) {
  538. dev_err(dev, "bad size for message code 0x%04x (expected %zu, "
  539. "got %zu)\n", le16_to_cpu(l3l4_hdr->type),
  540. expected_size, msg_size);
  541. result = -EIO;
  542. } else
  543. result = 0;
  544. error_hdr_size:
  545. d_fnend(4, dev,
  546. "(i2400m %p l3l4_hdr %p msg_size %zu) = %d\n",
  547. i2400m, l3l4_hdr, msg_size, result);
  548. return result;
  549. }
  550. /*
  551. * Cancel a wait for a command ACK
  552. *
  553. * @i2400m: device descriptor
  554. * @code: [negative] errno code to cancel with (don't use
  555. * -EINPROGRESS)
  556. *
  557. * If there is an ack already filled out, free it.
  558. */
  559. void i2400m_msg_to_dev_cancel_wait(struct i2400m *i2400m, int code)
  560. {
  561. struct sk_buff *ack_skb;
  562. unsigned long flags;
  563. spin_lock_irqsave(&i2400m->rx_lock, flags);
  564. ack_skb = i2400m->ack_skb;
  565. if (ack_skb && !IS_ERR(ack_skb))
  566. kfree_skb(ack_skb);
  567. i2400m->ack_skb = ERR_PTR(code);
  568. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  569. }
  570. /**
  571. * i2400m_msg_to_dev - Send a control message to the device and get a response
  572. *
  573. * @i2400m: device descriptor
  574. *
  575. * @msg_skb: an skb *
  576. *
  577. * @buf: pointer to the buffer containing the message to be sent; it
  578. * has to start with a &struct i2400M_l3l4_hdr and then
  579. * followed by the payload. Once this function returns, the
  580. * buffer can be reused.
  581. *
  582. * @buf_len: buffer size
  583. *
  584. * Returns:
  585. *
  586. * Pointer to skb containing the ack message. You need to check the
  587. * pointer with IS_ERR(), as it might be an error code. Error codes
  588. * could happen because:
  589. *
  590. * - the message wasn't formatted correctly
  591. * - couldn't send the message
  592. * - failed waiting for a response
  593. * - the ack message wasn't formatted correctly
  594. *
  595. * The returned skb has been allocated with wimax_msg_to_user_alloc(),
  596. * it contains the reponse in a netlink attribute and is ready to be
  597. * passed up to user space with wimax_msg_to_user_send(). To access
  598. * the payload and its length, use wimax_msg_{data,len}() on the skb.
  599. *
  600. * The skb has to be freed with kfree_skb() once done.
  601. *
  602. * Description:
  603. *
  604. * This function delivers a message/command to the device and waits
  605. * for an ack to be received. The format is described in
  606. * linux/wimax/i2400m.h. In summary, a command/get/set is followed by an
  607. * ack.
  608. *
  609. * This function will not check the ack status, that's left up to the
  610. * caller. Once done with the ack skb, it has to be kfree_skb()ed.
  611. *
  612. * The i2400m handles only one message at the same time, thus we need
  613. * the mutex to exclude other players.
  614. *
  615. * We write the message and then wait for an answer to come back. The
  616. * RX path intercepts control messages and handles them in
  617. * i2400m_rx_ctl(). Reports (notifications) are (maybe) processed
  618. * locally and then forwarded (as needed) to user space on the WiMAX
  619. * stack message pipe. Acks are saved and passed back to us through an
  620. * skb in i2400m->ack_skb which is ready to be given to generic
  621. * netlink if need be.
  622. */
  623. struct sk_buff *i2400m_msg_to_dev(struct i2400m *i2400m,
  624. const void *buf, size_t buf_len)
  625. {
  626. int result;
  627. struct device *dev = i2400m_dev(i2400m);
  628. const struct i2400m_l3l4_hdr *msg_l3l4_hdr;
  629. struct sk_buff *ack_skb;
  630. const struct i2400m_l3l4_hdr *ack_l3l4_hdr;
  631. size_t ack_len;
  632. int ack_timeout;
  633. unsigned msg_type;
  634. unsigned long flags;
  635. d_fnstart(3, dev, "(i2400m %p buf %p len %zu)\n",
  636. i2400m, buf, buf_len);
  637. if (i2400m->boot_mode)
  638. return ERR_PTR(-ENODEV);
  639. msg_l3l4_hdr = buf;
  640. /* Check msg & payload consistency */
  641. result = i2400m_msg_size_check(i2400m, msg_l3l4_hdr, buf_len);
  642. if (result < 0)
  643. goto error_bad_msg;
  644. msg_type = le16_to_cpu(msg_l3l4_hdr->type);
  645. d_printf(1, dev, "CMD/GET/SET 0x%04x %zu bytes\n",
  646. msg_type, buf_len);
  647. d_dump(2, dev, buf, buf_len);
  648. /* Setup the completion, ack_skb ("we are waiting") and send
  649. * the message to the device */
  650. mutex_lock(&i2400m->msg_mutex);
  651. spin_lock_irqsave(&i2400m->rx_lock, flags);
  652. i2400m->ack_skb = ERR_PTR(-EINPROGRESS);
  653. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  654. init_completion(&i2400m->msg_completion);
  655. result = i2400m_tx(i2400m, buf, buf_len, I2400M_PT_CTRL);
  656. if (result < 0) {
  657. dev_err(dev, "can't send message 0x%04x: %d\n",
  658. le16_to_cpu(msg_l3l4_hdr->type), result);
  659. goto error_tx;
  660. }
  661. /* Some commands take longer to execute because of crypto ops,
  662. * so we give them some more leeway on timeout */
  663. switch (msg_type) {
  664. case I2400M_MT_GET_TLS_OPERATION_RESULT:
  665. case I2400M_MT_CMD_SEND_EAP_RESPONSE:
  666. ack_timeout = 5 * HZ;
  667. break;
  668. default:
  669. ack_timeout = HZ;
  670. };
  671. if (unlikely(i2400m->trace_msg_from_user))
  672. wimax_msg(&i2400m->wimax_dev, "echo", buf, buf_len, GFP_KERNEL);
  673. /* The RX path in rx.c will put any response for this message
  674. * in i2400m->ack_skb and wake us up. If we cancel the wait,
  675. * we need to change the value of i2400m->ack_skb to something
  676. * not -EINPROGRESS so RX knows there is no one waiting. */
  677. result = wait_for_completion_interruptible_timeout(
  678. &i2400m->msg_completion, ack_timeout);
  679. if (result == 0) {
  680. dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
  681. msg_type);
  682. result = -ETIMEDOUT;
  683. i2400m_msg_to_dev_cancel_wait(i2400m, result);
  684. goto error_wait_for_completion;
  685. } else if (result < 0) {
  686. dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
  687. msg_type, result);
  688. i2400m_msg_to_dev_cancel_wait(i2400m, result);
  689. goto error_wait_for_completion;
  690. }
  691. /* Pull out the ack data from i2400m->ack_skb -- see if it is
  692. * an error and act accordingly */
  693. spin_lock_irqsave(&i2400m->rx_lock, flags);
  694. ack_skb = i2400m->ack_skb;
  695. if (IS_ERR(ack_skb))
  696. result = PTR_ERR(ack_skb);
  697. else
  698. result = 0;
  699. i2400m->ack_skb = NULL;
  700. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  701. if (result < 0)
  702. goto error_ack_status;
  703. ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
  704. /* Check the ack and deliver it if it is ok */
  705. if (unlikely(i2400m->trace_msg_from_user))
  706. wimax_msg(&i2400m->wimax_dev, "echo",
  707. ack_l3l4_hdr, ack_len, GFP_KERNEL);
  708. result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
  709. if (result < 0) {
  710. dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
  711. msg_type, result);
  712. goto error_bad_ack_len;
  713. }
  714. if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
  715. dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
  716. le16_to_cpu(ack_l3l4_hdr->type), msg_type);
  717. result = -EIO;
  718. goto error_bad_ack_type;
  719. }
  720. i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
  721. mutex_unlock(&i2400m->msg_mutex);
  722. d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
  723. i2400m, buf, buf_len, ack_skb);
  724. return ack_skb;
  725. error_bad_ack_type:
  726. error_bad_ack_len:
  727. kfree_skb(ack_skb);
  728. error_ack_status:
  729. error_wait_for_completion:
  730. error_tx:
  731. mutex_unlock(&i2400m->msg_mutex);
  732. error_bad_msg:
  733. d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
  734. i2400m, buf, buf_len, result);
  735. return ERR_PTR(result);
  736. }
  737. /*
  738. * Definitions for the Enter Power Save command
  739. *
  740. * The Enter Power Save command requests the device to go into power
  741. * saving mode. The device will ack or nak the command depending on it
  742. * being ready for it. If it acks, we tell the USB subsystem to
  743. *
  744. * As well, the device might request to go into power saving mode by
  745. * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
  746. * this command. The hookups in the RX coder allow
  747. */
  748. enum {
  749. I2400M_WAKEUP_ENABLED = 0x01,
  750. I2400M_WAKEUP_DISABLED = 0x02,
  751. I2400M_TLV_TYPE_WAKEUP_MODE = 144,
  752. };
  753. struct i2400m_cmd_enter_power_save {
  754. struct i2400m_l3l4_hdr hdr;
  755. struct i2400m_tlv_hdr tlv;
  756. __le32 val;
  757. } __attribute__((packed));
  758. /*
  759. * Request entering power save
  760. *
  761. * This command is (mainly) executed when the device indicates that it
  762. * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
  763. */
  764. int i2400m_cmd_enter_powersave(struct i2400m *i2400m)
  765. {
  766. int result;
  767. struct device *dev = i2400m_dev(i2400m);
  768. struct sk_buff *ack_skb;
  769. struct i2400m_cmd_enter_power_save *cmd;
  770. char strerr[32];
  771. result = -ENOMEM;
  772. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  773. if (cmd == NULL)
  774. goto error_alloc;
  775. cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
  776. cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
  777. cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  778. cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
  779. cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
  780. cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
  781. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  782. result = PTR_ERR(ack_skb);
  783. if (IS_ERR(ack_skb)) {
  784. dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
  785. result);
  786. goto error_msg_to_dev;
  787. }
  788. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  789. strerr, sizeof(strerr));
  790. if (result == -EACCES)
  791. d_printf(1, dev, "Cannot enter power save mode\n");
  792. else if (result < 0)
  793. dev_err(dev, "'Enter power save' (0x%04x) command failed: "
  794. "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
  795. result, strerr);
  796. else
  797. d_printf(1, dev, "device ready to power save\n");
  798. kfree_skb(ack_skb);
  799. error_msg_to_dev:
  800. kfree(cmd);
  801. error_alloc:
  802. return result;
  803. }
  804. EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
  805. /*
  806. * Definitions for getting device information
  807. */
  808. enum {
  809. I2400M_TLV_DETAILED_DEVICE_INFO = 140
  810. };
  811. /**
  812. * i2400m_get_device_info - Query the device for detailed device information
  813. *
  814. * @i2400m: device descriptor
  815. *
  816. * Returns: an skb whose skb->data points to a 'struct
  817. * i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
  818. * skb is *guaranteed* to contain the whole TLV data structure.
  819. *
  820. * On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
  821. * code.
  822. */
  823. struct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
  824. {
  825. int result;
  826. struct device *dev = i2400m_dev(i2400m);
  827. struct sk_buff *ack_skb;
  828. struct i2400m_l3l4_hdr *cmd;
  829. const struct i2400m_l3l4_hdr *ack;
  830. size_t ack_len;
  831. const struct i2400m_tlv_hdr *tlv;
  832. const struct i2400m_tlv_detailed_device_info *ddi;
  833. char strerr[32];
  834. ack_skb = ERR_PTR(-ENOMEM);
  835. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  836. if (cmd == NULL)
  837. goto error_alloc;
  838. cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
  839. cmd->length = 0;
  840. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  841. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  842. if (IS_ERR(ack_skb)) {
  843. dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
  844. PTR_ERR(ack_skb));
  845. goto error_msg_to_dev;
  846. }
  847. ack = wimax_msg_data_len(ack_skb, &ack_len);
  848. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  849. if (result < 0) {
  850. dev_err(dev, "'get device info' (0x%04x) command failed: "
  851. "%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
  852. strerr);
  853. goto error_cmd_failed;
  854. }
  855. tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
  856. I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
  857. if (tlv == NULL) {
  858. dev_err(dev, "GET DEVICE INFO: "
  859. "detailed device info TLV not found (0x%04x)\n",
  860. I2400M_TLV_DETAILED_DEVICE_INFO);
  861. result = -EIO;
  862. goto error_no_tlv;
  863. }
  864. skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
  865. error_msg_to_dev:
  866. kfree(cmd);
  867. error_alloc:
  868. return ack_skb;
  869. error_no_tlv:
  870. error_cmd_failed:
  871. kfree_skb(ack_skb);
  872. kfree(cmd);
  873. return ERR_PTR(result);
  874. }
  875. /* Firmware interface versions we support */
  876. enum {
  877. I2400M_HDIv_MAJOR = 9,
  878. I2400M_HDIv_MINOR = 1,
  879. I2400M_HDIv_MINOR_2 = 2,
  880. };
  881. /**
  882. * i2400m_firmware_check - check firmware versions are compatible with
  883. * the driver
  884. *
  885. * @i2400m: device descriptor
  886. *
  887. * Returns: 0 if ok, < 0 errno code an error and a message in the
  888. * kernel log.
  889. *
  890. * Long function, but quite simple; first chunk launches the command
  891. * and double checks the reply for the right TLV. Then we process the
  892. * TLV (where the meat is).
  893. *
  894. * Once we process the TLV that gives us the firmware's interface
  895. * version, we encode it and save it in i2400m->fw_version for future
  896. * reference.
  897. */
  898. int i2400m_firmware_check(struct i2400m *i2400m)
  899. {
  900. int result;
  901. struct device *dev = i2400m_dev(i2400m);
  902. struct sk_buff *ack_skb;
  903. struct i2400m_l3l4_hdr *cmd;
  904. const struct i2400m_l3l4_hdr *ack;
  905. size_t ack_len;
  906. const struct i2400m_tlv_hdr *tlv;
  907. const struct i2400m_tlv_l4_message_versions *l4mv;
  908. char strerr[32];
  909. unsigned major, minor, branch;
  910. result = -ENOMEM;
  911. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  912. if (cmd == NULL)
  913. goto error_alloc;
  914. cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
  915. cmd->length = 0;
  916. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  917. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  918. if (IS_ERR(ack_skb)) {
  919. result = PTR_ERR(ack_skb);
  920. dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
  921. result);
  922. goto error_msg_to_dev;
  923. }
  924. ack = wimax_msg_data_len(ack_skb, &ack_len);
  925. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  926. if (result < 0) {
  927. dev_err(dev, "'get lm version' (0x%04x) command failed: "
  928. "%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
  929. strerr);
  930. goto error_cmd_failed;
  931. }
  932. tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
  933. I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
  934. if (tlv == NULL) {
  935. dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
  936. I2400M_TLV_L4_MESSAGE_VERSIONS);
  937. result = -EIO;
  938. goto error_no_tlv;
  939. }
  940. l4mv = container_of(tlv, typeof(*l4mv), hdr);
  941. major = le16_to_cpu(l4mv->major);
  942. minor = le16_to_cpu(l4mv->minor);
  943. branch = le16_to_cpu(l4mv->branch);
  944. result = -EINVAL;
  945. if (major != I2400M_HDIv_MAJOR) {
  946. dev_err(dev, "unsupported major fw version "
  947. "%u.%u.%u\n", major, minor, branch);
  948. goto error_bad_major;
  949. }
  950. result = 0;
  951. if (minor < I2400M_HDIv_MINOR_2 && minor > I2400M_HDIv_MINOR)
  952. dev_warn(dev, "untested minor fw version %u.%u.%u\n",
  953. major, minor, branch);
  954. /* Yes, we ignore the branch -- we don't have to track it */
  955. i2400m->fw_version = major << 16 | minor;
  956. dev_info(dev, "firmware interface version %u.%u.%u\n",
  957. major, minor, branch);
  958. error_bad_major:
  959. error_no_tlv:
  960. error_cmd_failed:
  961. kfree_skb(ack_skb);
  962. error_msg_to_dev:
  963. kfree(cmd);
  964. error_alloc:
  965. return result;
  966. }
  967. /*
  968. * Send an DoExitIdle command to the device to ask it to go out of
  969. * basestation-idle mode.
  970. *
  971. * @i2400m: device descriptor
  972. *
  973. * This starts a renegotiation with the basestation that might involve
  974. * another crypto handshake with user space.
  975. *
  976. * Returns: 0 if ok, < 0 errno code on error.
  977. */
  978. int i2400m_cmd_exit_idle(struct i2400m *i2400m)
  979. {
  980. int result;
  981. struct device *dev = i2400m_dev(i2400m);
  982. struct sk_buff *ack_skb;
  983. struct i2400m_l3l4_hdr *cmd;
  984. char strerr[32];
  985. result = -ENOMEM;
  986. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  987. if (cmd == NULL)
  988. goto error_alloc;
  989. cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
  990. cmd->length = 0;
  991. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  992. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  993. result = PTR_ERR(ack_skb);
  994. if (IS_ERR(ack_skb)) {
  995. dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
  996. result);
  997. goto error_msg_to_dev;
  998. }
  999. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  1000. strerr, sizeof(strerr));
  1001. kfree_skb(ack_skb);
  1002. error_msg_to_dev:
  1003. kfree(cmd);
  1004. error_alloc:
  1005. return result;
  1006. }
  1007. /*
  1008. * Query the device for its state, update the WiMAX stack's idea of it
  1009. *
  1010. * @i2400m: device descriptor
  1011. *
  1012. * Returns: 0 if ok, < 0 errno code on error.
  1013. *
  1014. * Executes a 'Get State' command and parses the returned
  1015. * TLVs.
  1016. *
  1017. * Because this is almost identical to a 'Report State', we use
  1018. * i2400m_report_state_hook() to parse the answer. This will set the
  1019. * carrier state, as well as the RF Kill switches state.
  1020. */
  1021. int i2400m_cmd_get_state(struct i2400m *i2400m)
  1022. {
  1023. int result;
  1024. struct device *dev = i2400m_dev(i2400m);
  1025. struct sk_buff *ack_skb;
  1026. struct i2400m_l3l4_hdr *cmd;
  1027. const struct i2400m_l3l4_hdr *ack;
  1028. size_t ack_len;
  1029. char strerr[32];
  1030. result = -ENOMEM;
  1031. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1032. if (cmd == NULL)
  1033. goto error_alloc;
  1034. cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
  1035. cmd->length = 0;
  1036. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  1037. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  1038. if (IS_ERR(ack_skb)) {
  1039. dev_err(dev, "Failed to issue 'get state' command: %ld\n",
  1040. PTR_ERR(ack_skb));
  1041. result = PTR_ERR(ack_skb);
  1042. goto error_msg_to_dev;
  1043. }
  1044. ack = wimax_msg_data_len(ack_skb, &ack_len);
  1045. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  1046. if (result < 0) {
  1047. dev_err(dev, "'get state' (0x%04x) command failed: "
  1048. "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
  1049. goto error_cmd_failed;
  1050. }
  1051. i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
  1052. "GET STATE");
  1053. result = 0;
  1054. kfree_skb(ack_skb);
  1055. error_cmd_failed:
  1056. error_msg_to_dev:
  1057. kfree(cmd);
  1058. error_alloc:
  1059. return result;
  1060. }
  1061. EXPORT_SYMBOL_GPL(i2400m_cmd_get_state);
  1062. /**
  1063. * Set basic configuration settings
  1064. *
  1065. * @i2400m: device descriptor
  1066. * @args: array of pointers to the TLV headers to send for
  1067. * configuration (each followed by its payload).
  1068. * TLV headers and payloads must be properly initialized, with the
  1069. * right endianess (LE).
  1070. * @arg_size: number of pointers in the @args array
  1071. */
  1072. int i2400m_set_init_config(struct i2400m *i2400m,
  1073. const struct i2400m_tlv_hdr **arg, size_t args)
  1074. {
  1075. int result;
  1076. struct device *dev = i2400m_dev(i2400m);
  1077. struct sk_buff *ack_skb;
  1078. struct i2400m_l3l4_hdr *cmd;
  1079. char strerr[32];
  1080. unsigned argc, argsize, tlv_size;
  1081. const struct i2400m_tlv_hdr *tlv_hdr;
  1082. void *buf, *itr;
  1083. d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
  1084. result = 0;
  1085. if (args == 0)
  1086. goto none;
  1087. /* Compute the size of all the TLVs, so we can alloc a
  1088. * contiguous command block to copy them. */
  1089. argsize = 0;
  1090. for (argc = 0; argc < args; argc++) {
  1091. tlv_hdr = arg[argc];
  1092. argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
  1093. }
  1094. WARN_ON(argc >= 9); /* As per hw spec */
  1095. /* Alloc the space for the command and TLVs*/
  1096. result = -ENOMEM;
  1097. buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
  1098. if (buf == NULL)
  1099. goto error_alloc;
  1100. cmd = buf;
  1101. cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
  1102. cmd->length = cpu_to_le16(argsize);
  1103. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  1104. /* Copy the TLVs */
  1105. itr = buf + sizeof(*cmd);
  1106. for (argc = 0; argc < args; argc++) {
  1107. tlv_hdr = arg[argc];
  1108. tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
  1109. memcpy(itr, tlv_hdr, tlv_size);
  1110. itr += tlv_size;
  1111. }
  1112. /* Send the message! */
  1113. ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
  1114. result = PTR_ERR(ack_skb);
  1115. if (IS_ERR(ack_skb)) {
  1116. dev_err(dev, "Failed to issue 'init config' command: %d\n",
  1117. result);
  1118. goto error_msg_to_dev;
  1119. }
  1120. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  1121. strerr, sizeof(strerr));
  1122. if (result < 0)
  1123. dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
  1124. I2400M_MT_SET_INIT_CONFIG, result, strerr);
  1125. kfree_skb(ack_skb);
  1126. error_msg_to_dev:
  1127. kfree(buf);
  1128. error_alloc:
  1129. none:
  1130. d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
  1131. i2400m, arg, args, result);
  1132. return result;
  1133. }
  1134. EXPORT_SYMBOL_GPL(i2400m_set_init_config);
  1135. /**
  1136. * i2400m_set_idle_timeout - Set the device's idle mode timeout
  1137. *
  1138. * @i2400m: i2400m device descriptor
  1139. *
  1140. * @msecs: milliseconds for the timeout to enter idle mode. Between
  1141. * 100 to 300000 (5m); 0 to disable. In increments of 100.
  1142. *
  1143. * After this @msecs of the link being idle (no data being sent or
  1144. * received), the device will negotiate with the basestation entering
  1145. * idle mode for saving power. The connection is maintained, but
  1146. * getting out of it (done in tx.c) will require some negotiation,
  1147. * possible crypto re-handshake and a possible DHCP re-lease.
  1148. *
  1149. * Only available if fw_version >= 0x00090002.
  1150. *
  1151. * Returns: 0 if ok, < 0 errno code on error.
  1152. */
  1153. int i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
  1154. {
  1155. int result;
  1156. struct device *dev = i2400m_dev(i2400m);
  1157. struct sk_buff *ack_skb;
  1158. struct {
  1159. struct i2400m_l3l4_hdr hdr;
  1160. struct i2400m_tlv_config_idle_timeout cit;
  1161. } *cmd;
  1162. const struct i2400m_l3l4_hdr *ack;
  1163. size_t ack_len;
  1164. char strerr[32];
  1165. result = -ENOSYS;
  1166. if (i2400m_le_v1_3(i2400m))
  1167. goto error_alloc;
  1168. result = -ENOMEM;
  1169. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1170. if (cmd == NULL)
  1171. goto error_alloc;
  1172. cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
  1173. cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
  1174. cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  1175. cmd->cit.hdr.type =
  1176. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
  1177. cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
  1178. cmd->cit.timeout = cpu_to_le32(msecs);
  1179. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  1180. if (IS_ERR(ack_skb)) {
  1181. dev_err(dev, "Failed to issue 'set idle timeout' command: "
  1182. "%ld\n", PTR_ERR(ack_skb));
  1183. result = PTR_ERR(ack_skb);
  1184. goto error_msg_to_dev;
  1185. }
  1186. ack = wimax_msg_data_len(ack_skb, &ack_len);
  1187. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  1188. if (result < 0) {
  1189. dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
  1190. "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
  1191. goto error_cmd_failed;
  1192. }
  1193. result = 0;
  1194. kfree_skb(ack_skb);
  1195. error_cmd_failed:
  1196. error_msg_to_dev:
  1197. kfree(cmd);
  1198. error_alloc:
  1199. return result;
  1200. }
  1201. /**
  1202. * i2400m_dev_initialize - Initialize the device once communications are ready
  1203. *
  1204. * @i2400m: device descriptor
  1205. *
  1206. * Returns: 0 if ok, < 0 errno code on error.
  1207. *
  1208. * Configures the device to work the way we like it.
  1209. *
  1210. * At the point of this call, the device is registered with the WiMAX
  1211. * and netdev stacks, firmware is uploaded and we can talk to the
  1212. * device normally.
  1213. */
  1214. int i2400m_dev_initialize(struct i2400m *i2400m)
  1215. {
  1216. int result;
  1217. struct device *dev = i2400m_dev(i2400m);
  1218. struct i2400m_tlv_config_idle_parameters idle_params;
  1219. struct i2400m_tlv_config_idle_timeout idle_timeout;
  1220. struct i2400m_tlv_config_d2h_data_format df;
  1221. struct i2400m_tlv_config_dl_host_reorder dlhr;
  1222. const struct i2400m_tlv_hdr *args[9];
  1223. unsigned argc = 0;
  1224. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  1225. /* Disable idle mode? (enabled by default) */
  1226. if (i2400m_idle_mode_disabled) {
  1227. if (i2400m_le_v1_3(i2400m)) {
  1228. idle_params.hdr.type =
  1229. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
  1230. idle_params.hdr.length = cpu_to_le16(
  1231. sizeof(idle_params) - sizeof(idle_params.hdr));
  1232. idle_params.idle_timeout = 0;
  1233. idle_params.idle_paging_interval = 0;
  1234. args[argc++] = &idle_params.hdr;
  1235. } else {
  1236. idle_timeout.hdr.type =
  1237. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
  1238. idle_timeout.hdr.length = cpu_to_le16(
  1239. sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
  1240. idle_timeout.timeout = 0;
  1241. args[argc++] = &idle_timeout.hdr;
  1242. }
  1243. }
  1244. if (i2400m_ge_v1_4(i2400m)) {
  1245. /* Enable extended RX data format? */
  1246. df.hdr.type =
  1247. cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
  1248. df.hdr.length = cpu_to_le16(
  1249. sizeof(df) - sizeof(df.hdr));
  1250. df.format = 1;
  1251. args[argc++] = &df.hdr;
  1252. /* Enable RX data reordering?
  1253. * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
  1254. if (i2400m->rx_reorder) {
  1255. dlhr.hdr.type =
  1256. cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
  1257. dlhr.hdr.length = cpu_to_le16(
  1258. sizeof(dlhr) - sizeof(dlhr.hdr));
  1259. dlhr.reorder = 1;
  1260. args[argc++] = &dlhr.hdr;
  1261. }
  1262. }
  1263. result = i2400m_set_init_config(i2400m, args, argc);
  1264. if (result < 0)
  1265. goto error;
  1266. /*
  1267. * Update state: Here it just calls a get state; parsing the
  1268. * result (System State TLV and RF Status TLV [done in the rx
  1269. * path hooks]) will set the hardware and software RF-Kill
  1270. * status.
  1271. */
  1272. result = i2400m_cmd_get_state(i2400m);
  1273. error:
  1274. if (result < 0)
  1275. dev_err(dev, "failed to initialize the device: %d\n", result);
  1276. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  1277. return result;
  1278. }
  1279. /**
  1280. * i2400m_dev_shutdown - Shutdown a running device
  1281. *
  1282. * @i2400m: device descriptor
  1283. *
  1284. * Gracefully stops the device, moving it to the lowest power
  1285. * consumption state possible.
  1286. */
  1287. void i2400m_dev_shutdown(struct i2400m *i2400m)
  1288. {
  1289. int result = -ENODEV;
  1290. struct device *dev = i2400m_dev(i2400m);
  1291. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  1292. result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
  1293. d_fnend(3, dev, "(i2400m %p) = void [%d]\n", i2400m, result);
  1294. return;
  1295. }