control.c 42 KB

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