control.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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. /* The RX path in rx.c will put any response for this message
  672. * in i2400m->ack_skb and wake us up. If we cancel the wait,
  673. * we need to change the value of i2400m->ack_skb to something
  674. * not -EINPROGRESS so RX knows there is no one waiting. */
  675. result = wait_for_completion_interruptible_timeout(
  676. &i2400m->msg_completion, ack_timeout);
  677. if (result == 0) {
  678. dev_err(dev, "timeout waiting for reply to message 0x%04x\n",
  679. msg_type);
  680. result = -ETIMEDOUT;
  681. i2400m_msg_to_dev_cancel_wait(i2400m, result);
  682. goto error_wait_for_completion;
  683. } else if (result < 0) {
  684. dev_err(dev, "error waiting for reply to message 0x%04x: %d\n",
  685. msg_type, result);
  686. i2400m_msg_to_dev_cancel_wait(i2400m, result);
  687. goto error_wait_for_completion;
  688. }
  689. /* Pull out the ack data from i2400m->ack_skb -- see if it is
  690. * an error and act accordingly */
  691. spin_lock_irqsave(&i2400m->rx_lock, flags);
  692. ack_skb = i2400m->ack_skb;
  693. if (IS_ERR(ack_skb))
  694. result = PTR_ERR(ack_skb);
  695. else
  696. result = 0;
  697. i2400m->ack_skb = NULL;
  698. spin_unlock_irqrestore(&i2400m->rx_lock, flags);
  699. if (result < 0)
  700. goto error_ack_status;
  701. ack_l3l4_hdr = wimax_msg_data_len(ack_skb, &ack_len);
  702. /* Check the ack and deliver it if it is ok */
  703. result = i2400m_msg_size_check(i2400m, ack_l3l4_hdr, ack_len);
  704. if (result < 0) {
  705. dev_err(dev, "HW BUG? reply to message 0x%04x: %d\n",
  706. msg_type, result);
  707. goto error_bad_ack_len;
  708. }
  709. if (msg_type != le16_to_cpu(ack_l3l4_hdr->type)) {
  710. dev_err(dev, "HW BUG? bad reply 0x%04x to message 0x%04x\n",
  711. le16_to_cpu(ack_l3l4_hdr->type), msg_type);
  712. result = -EIO;
  713. goto error_bad_ack_type;
  714. }
  715. i2400m_msg_ack_hook(i2400m, ack_l3l4_hdr, ack_len);
  716. mutex_unlock(&i2400m->msg_mutex);
  717. d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %p\n",
  718. i2400m, buf, buf_len, ack_skb);
  719. return ack_skb;
  720. error_bad_ack_type:
  721. error_bad_ack_len:
  722. kfree_skb(ack_skb);
  723. error_ack_status:
  724. error_wait_for_completion:
  725. error_tx:
  726. mutex_unlock(&i2400m->msg_mutex);
  727. error_bad_msg:
  728. d_fnend(3, dev, "(i2400m %p buf %p len %zu) = %d\n",
  729. i2400m, buf, buf_len, result);
  730. return ERR_PTR(result);
  731. }
  732. /*
  733. * Definitions for the Enter Power Save command
  734. *
  735. * The Enter Power Save command requests the device to go into power
  736. * saving mode. The device will ack or nak the command depending on it
  737. * being ready for it. If it acks, we tell the USB subsystem to
  738. *
  739. * As well, the device might request to go into power saving mode by
  740. * sending a report (REPORT_POWERSAVE_READY), in which case, we issue
  741. * this command. The hookups in the RX coder allow
  742. */
  743. enum {
  744. I2400M_WAKEUP_ENABLED = 0x01,
  745. I2400M_WAKEUP_DISABLED = 0x02,
  746. I2400M_TLV_TYPE_WAKEUP_MODE = 144,
  747. };
  748. struct i2400m_cmd_enter_power_save {
  749. struct i2400m_l3l4_hdr hdr;
  750. struct i2400m_tlv_hdr tlv;
  751. __le32 val;
  752. } __attribute__((packed));
  753. /*
  754. * Request entering power save
  755. *
  756. * This command is (mainly) executed when the device indicates that it
  757. * is ready to go into powersave mode via a REPORT_POWERSAVE_READY.
  758. */
  759. int i2400m_cmd_enter_powersave(struct i2400m *i2400m)
  760. {
  761. int result;
  762. struct device *dev = i2400m_dev(i2400m);
  763. struct sk_buff *ack_skb;
  764. struct i2400m_cmd_enter_power_save *cmd;
  765. char strerr[32];
  766. result = -ENOMEM;
  767. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  768. if (cmd == NULL)
  769. goto error_alloc;
  770. cmd->hdr.type = cpu_to_le16(I2400M_MT_CMD_ENTER_POWERSAVE);
  771. cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
  772. cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  773. cmd->tlv.type = cpu_to_le16(I2400M_TLV_TYPE_WAKEUP_MODE);
  774. cmd->tlv.length = cpu_to_le16(sizeof(cmd->val));
  775. cmd->val = cpu_to_le32(I2400M_WAKEUP_ENABLED);
  776. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  777. result = PTR_ERR(ack_skb);
  778. if (IS_ERR(ack_skb)) {
  779. dev_err(dev, "Failed to issue 'Enter power save' command: %d\n",
  780. result);
  781. goto error_msg_to_dev;
  782. }
  783. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  784. strerr, sizeof(strerr));
  785. if (result == -EACCES)
  786. d_printf(1, dev, "Cannot enter power save mode\n");
  787. else if (result < 0)
  788. dev_err(dev, "'Enter power save' (0x%04x) command failed: "
  789. "%d - %s\n", I2400M_MT_CMD_ENTER_POWERSAVE,
  790. result, strerr);
  791. else
  792. d_printf(1, dev, "device ready to power save\n");
  793. kfree_skb(ack_skb);
  794. error_msg_to_dev:
  795. kfree(cmd);
  796. error_alloc:
  797. return result;
  798. }
  799. EXPORT_SYMBOL_GPL(i2400m_cmd_enter_powersave);
  800. /*
  801. * Definitions for getting device information
  802. */
  803. enum {
  804. I2400M_TLV_DETAILED_DEVICE_INFO = 140
  805. };
  806. /**
  807. * i2400m_get_device_info - Query the device for detailed device information
  808. *
  809. * @i2400m: device descriptor
  810. *
  811. * Returns: an skb whose skb->data points to a 'struct
  812. * i2400m_tlv_detailed_device_info'. When done, kfree_skb() it. The
  813. * skb is *guaranteed* to contain the whole TLV data structure.
  814. *
  815. * On error, IS_ERR(skb) is true and ERR_PTR(skb) is the error
  816. * code.
  817. */
  818. struct sk_buff *i2400m_get_device_info(struct i2400m *i2400m)
  819. {
  820. int result;
  821. struct device *dev = i2400m_dev(i2400m);
  822. struct sk_buff *ack_skb;
  823. struct i2400m_l3l4_hdr *cmd;
  824. const struct i2400m_l3l4_hdr *ack;
  825. size_t ack_len;
  826. const struct i2400m_tlv_hdr *tlv;
  827. const struct i2400m_tlv_detailed_device_info *ddi;
  828. char strerr[32];
  829. ack_skb = ERR_PTR(-ENOMEM);
  830. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  831. if (cmd == NULL)
  832. goto error_alloc;
  833. cmd->type = cpu_to_le16(I2400M_MT_GET_DEVICE_INFO);
  834. cmd->length = 0;
  835. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  836. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  837. if (IS_ERR(ack_skb)) {
  838. dev_err(dev, "Failed to issue 'get device info' command: %ld\n",
  839. PTR_ERR(ack_skb));
  840. goto error_msg_to_dev;
  841. }
  842. ack = wimax_msg_data_len(ack_skb, &ack_len);
  843. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  844. if (result < 0) {
  845. dev_err(dev, "'get device info' (0x%04x) command failed: "
  846. "%d - %s\n", I2400M_MT_GET_DEVICE_INFO, result,
  847. strerr);
  848. goto error_cmd_failed;
  849. }
  850. tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
  851. I2400M_TLV_DETAILED_DEVICE_INFO, sizeof(*ddi));
  852. if (tlv == NULL) {
  853. dev_err(dev, "GET DEVICE INFO: "
  854. "detailed device info TLV not found (0x%04x)\n",
  855. I2400M_TLV_DETAILED_DEVICE_INFO);
  856. result = -EIO;
  857. goto error_no_tlv;
  858. }
  859. skb_pull(ack_skb, (void *) tlv - (void *) ack_skb->data);
  860. error_msg_to_dev:
  861. kfree(cmd);
  862. error_alloc:
  863. return ack_skb;
  864. error_no_tlv:
  865. error_cmd_failed:
  866. kfree_skb(ack_skb);
  867. kfree(cmd);
  868. return ERR_PTR(result);
  869. }
  870. /* Firmware interface versions we support */
  871. enum {
  872. I2400M_HDIv_MAJOR = 9,
  873. I2400M_HDIv_MINOR = 1,
  874. I2400M_HDIv_MINOR_2 = 2,
  875. };
  876. /**
  877. * i2400m_firmware_check - check firmware versions are compatible with
  878. * the driver
  879. *
  880. * @i2400m: device descriptor
  881. *
  882. * Returns: 0 if ok, < 0 errno code an error and a message in the
  883. * kernel log.
  884. *
  885. * Long function, but quite simple; first chunk launches the command
  886. * and double checks the reply for the right TLV. Then we process the
  887. * TLV (where the meat is).
  888. *
  889. * Once we process the TLV that gives us the firmware's interface
  890. * version, we encode it and save it in i2400m->fw_version for future
  891. * reference.
  892. */
  893. int i2400m_firmware_check(struct i2400m *i2400m)
  894. {
  895. int result;
  896. struct device *dev = i2400m_dev(i2400m);
  897. struct sk_buff *ack_skb;
  898. struct i2400m_l3l4_hdr *cmd;
  899. const struct i2400m_l3l4_hdr *ack;
  900. size_t ack_len;
  901. const struct i2400m_tlv_hdr *tlv;
  902. const struct i2400m_tlv_l4_message_versions *l4mv;
  903. char strerr[32];
  904. unsigned major, minor, branch;
  905. result = -ENOMEM;
  906. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  907. if (cmd == NULL)
  908. goto error_alloc;
  909. cmd->type = cpu_to_le16(I2400M_MT_GET_LM_VERSION);
  910. cmd->length = 0;
  911. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  912. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  913. if (IS_ERR(ack_skb)) {
  914. result = PTR_ERR(ack_skb);
  915. dev_err(dev, "Failed to issue 'get lm version' command: %-d\n",
  916. result);
  917. goto error_msg_to_dev;
  918. }
  919. ack = wimax_msg_data_len(ack_skb, &ack_len);
  920. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  921. if (result < 0) {
  922. dev_err(dev, "'get lm version' (0x%04x) command failed: "
  923. "%d - %s\n", I2400M_MT_GET_LM_VERSION, result,
  924. strerr);
  925. goto error_cmd_failed;
  926. }
  927. tlv = i2400m_tlv_find(i2400m, ack->pl, ack_len - sizeof(*ack),
  928. I2400M_TLV_L4_MESSAGE_VERSIONS, sizeof(*l4mv));
  929. if (tlv == NULL) {
  930. dev_err(dev, "get lm version: TLV not found (0x%04x)\n",
  931. I2400M_TLV_L4_MESSAGE_VERSIONS);
  932. result = -EIO;
  933. goto error_no_tlv;
  934. }
  935. l4mv = container_of(tlv, typeof(*l4mv), hdr);
  936. major = le16_to_cpu(l4mv->major);
  937. minor = le16_to_cpu(l4mv->minor);
  938. branch = le16_to_cpu(l4mv->branch);
  939. result = -EINVAL;
  940. if (major != I2400M_HDIv_MAJOR) {
  941. dev_err(dev, "unsupported major fw version "
  942. "%u.%u.%u\n", major, minor, branch);
  943. goto error_bad_major;
  944. }
  945. result = 0;
  946. if (minor < I2400M_HDIv_MINOR_2 && minor > I2400M_HDIv_MINOR)
  947. dev_warn(dev, "untested minor fw version %u.%u.%u\n",
  948. major, minor, branch);
  949. /* Yes, we ignore the branch -- we don't have to track it */
  950. i2400m->fw_version = major << 16 | minor;
  951. dev_info(dev, "firmware interface version %u.%u.%u\n",
  952. major, minor, branch);
  953. error_bad_major:
  954. error_no_tlv:
  955. error_cmd_failed:
  956. kfree_skb(ack_skb);
  957. error_msg_to_dev:
  958. kfree(cmd);
  959. error_alloc:
  960. return result;
  961. }
  962. /*
  963. * Send an DoExitIdle command to the device to ask it to go out of
  964. * basestation-idle mode.
  965. *
  966. * @i2400m: device descriptor
  967. *
  968. * This starts a renegotiation with the basestation that might involve
  969. * another crypto handshake with user space.
  970. *
  971. * Returns: 0 if ok, < 0 errno code on error.
  972. */
  973. int i2400m_cmd_exit_idle(struct i2400m *i2400m)
  974. {
  975. int result;
  976. struct device *dev = i2400m_dev(i2400m);
  977. struct sk_buff *ack_skb;
  978. struct i2400m_l3l4_hdr *cmd;
  979. char strerr[32];
  980. result = -ENOMEM;
  981. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  982. if (cmd == NULL)
  983. goto error_alloc;
  984. cmd->type = cpu_to_le16(I2400M_MT_CMD_EXIT_IDLE);
  985. cmd->length = 0;
  986. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  987. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  988. result = PTR_ERR(ack_skb);
  989. if (IS_ERR(ack_skb)) {
  990. dev_err(dev, "Failed to issue 'exit idle' command: %d\n",
  991. result);
  992. goto error_msg_to_dev;
  993. }
  994. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  995. strerr, sizeof(strerr));
  996. kfree_skb(ack_skb);
  997. error_msg_to_dev:
  998. kfree(cmd);
  999. error_alloc:
  1000. return result;
  1001. }
  1002. /*
  1003. * Query the device for its state, update the WiMAX stack's idea of it
  1004. *
  1005. * @i2400m: device descriptor
  1006. *
  1007. * Returns: 0 if ok, < 0 errno code on error.
  1008. *
  1009. * Executes a 'Get State' command and parses the returned
  1010. * TLVs.
  1011. *
  1012. * Because this is almost identical to a 'Report State', we use
  1013. * i2400m_report_state_hook() to parse the answer. This will set the
  1014. * carrier state, as well as the RF Kill switches state.
  1015. */
  1016. int i2400m_cmd_get_state(struct i2400m *i2400m)
  1017. {
  1018. int result;
  1019. struct device *dev = i2400m_dev(i2400m);
  1020. struct sk_buff *ack_skb;
  1021. struct i2400m_l3l4_hdr *cmd;
  1022. const struct i2400m_l3l4_hdr *ack;
  1023. size_t ack_len;
  1024. char strerr[32];
  1025. result = -ENOMEM;
  1026. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1027. if (cmd == NULL)
  1028. goto error_alloc;
  1029. cmd->type = cpu_to_le16(I2400M_MT_GET_STATE);
  1030. cmd->length = 0;
  1031. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  1032. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  1033. if (IS_ERR(ack_skb)) {
  1034. dev_err(dev, "Failed to issue 'get state' command: %ld\n",
  1035. PTR_ERR(ack_skb));
  1036. result = PTR_ERR(ack_skb);
  1037. goto error_msg_to_dev;
  1038. }
  1039. ack = wimax_msg_data_len(ack_skb, &ack_len);
  1040. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  1041. if (result < 0) {
  1042. dev_err(dev, "'get state' (0x%04x) command failed: "
  1043. "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
  1044. goto error_cmd_failed;
  1045. }
  1046. i2400m_report_state_hook(i2400m, ack, ack_len - sizeof(*ack),
  1047. "GET STATE");
  1048. result = 0;
  1049. kfree_skb(ack_skb);
  1050. error_cmd_failed:
  1051. error_msg_to_dev:
  1052. kfree(cmd);
  1053. error_alloc:
  1054. return result;
  1055. }
  1056. EXPORT_SYMBOL_GPL(i2400m_cmd_get_state);
  1057. /**
  1058. * Set basic configuration settings
  1059. *
  1060. * @i2400m: device descriptor
  1061. * @args: array of pointers to the TLV headers to send for
  1062. * configuration (each followed by its payload).
  1063. * TLV headers and payloads must be properly initialized, with the
  1064. * right endianess (LE).
  1065. * @arg_size: number of pointers in the @args array
  1066. */
  1067. int i2400m_set_init_config(struct i2400m *i2400m,
  1068. const struct i2400m_tlv_hdr **arg, size_t args)
  1069. {
  1070. int result;
  1071. struct device *dev = i2400m_dev(i2400m);
  1072. struct sk_buff *ack_skb;
  1073. struct i2400m_l3l4_hdr *cmd;
  1074. char strerr[32];
  1075. unsigned argc, argsize, tlv_size;
  1076. const struct i2400m_tlv_hdr *tlv_hdr;
  1077. void *buf, *itr;
  1078. d_fnstart(3, dev, "(i2400m %p arg %p args %zu)\n", i2400m, arg, args);
  1079. result = 0;
  1080. if (args == 0)
  1081. goto none;
  1082. /* Compute the size of all the TLVs, so we can alloc a
  1083. * contiguous command block to copy them. */
  1084. argsize = 0;
  1085. for (argc = 0; argc < args; argc++) {
  1086. tlv_hdr = arg[argc];
  1087. argsize += sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
  1088. }
  1089. WARN_ON(argc >= 9); /* As per hw spec */
  1090. /* Alloc the space for the command and TLVs*/
  1091. result = -ENOMEM;
  1092. buf = kzalloc(sizeof(*cmd) + argsize, GFP_KERNEL);
  1093. if (buf == NULL)
  1094. goto error_alloc;
  1095. cmd = buf;
  1096. cmd->type = cpu_to_le16(I2400M_MT_SET_INIT_CONFIG);
  1097. cmd->length = cpu_to_le16(argsize);
  1098. cmd->version = cpu_to_le16(I2400M_L3L4_VERSION);
  1099. /* Copy the TLVs */
  1100. itr = buf + sizeof(*cmd);
  1101. for (argc = 0; argc < args; argc++) {
  1102. tlv_hdr = arg[argc];
  1103. tlv_size = sizeof(*tlv_hdr) + le16_to_cpu(tlv_hdr->length);
  1104. memcpy(itr, tlv_hdr, tlv_size);
  1105. itr += tlv_size;
  1106. }
  1107. /* Send the message! */
  1108. ack_skb = i2400m_msg_to_dev(i2400m, buf, sizeof(*cmd) + argsize);
  1109. result = PTR_ERR(ack_skb);
  1110. if (IS_ERR(ack_skb)) {
  1111. dev_err(dev, "Failed to issue 'init config' command: %d\n",
  1112. result);
  1113. goto error_msg_to_dev;
  1114. }
  1115. result = i2400m_msg_check_status(wimax_msg_data(ack_skb),
  1116. strerr, sizeof(strerr));
  1117. if (result < 0)
  1118. dev_err(dev, "'init config' (0x%04x) command failed: %d - %s\n",
  1119. I2400M_MT_SET_INIT_CONFIG, result, strerr);
  1120. kfree_skb(ack_skb);
  1121. error_msg_to_dev:
  1122. kfree(buf);
  1123. error_alloc:
  1124. none:
  1125. d_fnend(3, dev, "(i2400m %p arg %p args %zu) = %d\n",
  1126. i2400m, arg, args, result);
  1127. return result;
  1128. }
  1129. EXPORT_SYMBOL_GPL(i2400m_set_init_config);
  1130. /**
  1131. * i2400m_set_idle_timeout - Set the device's idle mode timeout
  1132. *
  1133. * @i2400m: i2400m device descriptor
  1134. *
  1135. * @msecs: milliseconds for the timeout to enter idle mode. Between
  1136. * 100 to 300000 (5m); 0 to disable. In increments of 100.
  1137. *
  1138. * After this @msecs of the link being idle (no data being sent or
  1139. * received), the device will negotiate with the basestation entering
  1140. * idle mode for saving power. The connection is maintained, but
  1141. * getting out of it (done in tx.c) will require some negotiation,
  1142. * possible crypto re-handshake and a possible DHCP re-lease.
  1143. *
  1144. * Only available if fw_version >= 0x00090002.
  1145. *
  1146. * Returns: 0 if ok, < 0 errno code on error.
  1147. */
  1148. int i2400m_set_idle_timeout(struct i2400m *i2400m, unsigned msecs)
  1149. {
  1150. int result;
  1151. struct device *dev = i2400m_dev(i2400m);
  1152. struct sk_buff *ack_skb;
  1153. struct {
  1154. struct i2400m_l3l4_hdr hdr;
  1155. struct i2400m_tlv_config_idle_timeout cit;
  1156. } *cmd;
  1157. const struct i2400m_l3l4_hdr *ack;
  1158. size_t ack_len;
  1159. char strerr[32];
  1160. result = -ENOSYS;
  1161. if (i2400m_le_v1_3(i2400m))
  1162. goto error_alloc;
  1163. result = -ENOMEM;
  1164. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1165. if (cmd == NULL)
  1166. goto error_alloc;
  1167. cmd->hdr.type = cpu_to_le16(I2400M_MT_GET_STATE);
  1168. cmd->hdr.length = cpu_to_le16(sizeof(*cmd) - sizeof(cmd->hdr));
  1169. cmd->hdr.version = cpu_to_le16(I2400M_L3L4_VERSION);
  1170. cmd->cit.hdr.type =
  1171. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
  1172. cmd->cit.hdr.length = cpu_to_le16(sizeof(cmd->cit.timeout));
  1173. cmd->cit.timeout = cpu_to_le32(msecs);
  1174. ack_skb = i2400m_msg_to_dev(i2400m, cmd, sizeof(*cmd));
  1175. if (IS_ERR(ack_skb)) {
  1176. dev_err(dev, "Failed to issue 'set idle timeout' command: "
  1177. "%ld\n", PTR_ERR(ack_skb));
  1178. result = PTR_ERR(ack_skb);
  1179. goto error_msg_to_dev;
  1180. }
  1181. ack = wimax_msg_data_len(ack_skb, &ack_len);
  1182. result = i2400m_msg_check_status(ack, strerr, sizeof(strerr));
  1183. if (result < 0) {
  1184. dev_err(dev, "'set idle timeout' (0x%04x) command failed: "
  1185. "%d - %s\n", I2400M_MT_GET_STATE, result, strerr);
  1186. goto error_cmd_failed;
  1187. }
  1188. result = 0;
  1189. kfree_skb(ack_skb);
  1190. error_cmd_failed:
  1191. error_msg_to_dev:
  1192. kfree(cmd);
  1193. error_alloc:
  1194. return result;
  1195. }
  1196. /**
  1197. * i2400m_dev_initialize - Initialize the device once communications are ready
  1198. *
  1199. * @i2400m: device descriptor
  1200. *
  1201. * Returns: 0 if ok, < 0 errno code on error.
  1202. *
  1203. * Configures the device to work the way we like it.
  1204. *
  1205. * At the point of this call, the device is registered with the WiMAX
  1206. * and netdev stacks, firmware is uploaded and we can talk to the
  1207. * device normally.
  1208. */
  1209. int i2400m_dev_initialize(struct i2400m *i2400m)
  1210. {
  1211. int result;
  1212. struct device *dev = i2400m_dev(i2400m);
  1213. struct i2400m_tlv_config_idle_parameters idle_params;
  1214. struct i2400m_tlv_config_idle_timeout idle_timeout;
  1215. struct i2400m_tlv_config_d2h_data_format df;
  1216. struct i2400m_tlv_config_dl_host_reorder dlhr;
  1217. const struct i2400m_tlv_hdr *args[9];
  1218. unsigned argc = 0;
  1219. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  1220. /* Disable idle mode? (enabled by default) */
  1221. if (i2400m_idle_mode_disabled) {
  1222. if (i2400m_le_v1_3(i2400m)) {
  1223. idle_params.hdr.type =
  1224. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_PARAMETERS);
  1225. idle_params.hdr.length = cpu_to_le16(
  1226. sizeof(idle_params) - sizeof(idle_params.hdr));
  1227. idle_params.idle_timeout = 0;
  1228. idle_params.idle_paging_interval = 0;
  1229. args[argc++] = &idle_params.hdr;
  1230. } else {
  1231. idle_timeout.hdr.type =
  1232. cpu_to_le16(I2400M_TLV_CONFIG_IDLE_TIMEOUT);
  1233. idle_timeout.hdr.length = cpu_to_le16(
  1234. sizeof(idle_timeout) - sizeof(idle_timeout.hdr));
  1235. idle_timeout.timeout = 0;
  1236. args[argc++] = &idle_timeout.hdr;
  1237. }
  1238. }
  1239. if (i2400m_ge_v1_4(i2400m)) {
  1240. /* Enable extended RX data format? */
  1241. df.hdr.type =
  1242. cpu_to_le16(I2400M_TLV_CONFIG_D2H_DATA_FORMAT);
  1243. df.hdr.length = cpu_to_le16(
  1244. sizeof(df) - sizeof(df.hdr));
  1245. df.format = 1;
  1246. args[argc++] = &df.hdr;
  1247. /* Enable RX data reordering?
  1248. * (switch flipped in rx.c:i2400m_rx_setup() after fw upload) */
  1249. if (i2400m->rx_reorder) {
  1250. dlhr.hdr.type =
  1251. cpu_to_le16(I2400M_TLV_CONFIG_DL_HOST_REORDER);
  1252. dlhr.hdr.length = cpu_to_le16(
  1253. sizeof(dlhr) - sizeof(dlhr.hdr));
  1254. dlhr.reorder = 1;
  1255. args[argc++] = &dlhr.hdr;
  1256. }
  1257. }
  1258. result = i2400m_set_init_config(i2400m, args, argc);
  1259. if (result < 0)
  1260. goto error;
  1261. /*
  1262. * Update state: Here it just calls a get state; parsing the
  1263. * result (System State TLV and RF Status TLV [done in the rx
  1264. * path hooks]) will set the hardware and software RF-Kill
  1265. * status.
  1266. */
  1267. result = i2400m_cmd_get_state(i2400m);
  1268. error:
  1269. if (result < 0)
  1270. dev_err(dev, "failed to initialize the device: %d\n", result);
  1271. d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
  1272. return result;
  1273. }
  1274. /**
  1275. * i2400m_dev_shutdown - Shutdown a running device
  1276. *
  1277. * @i2400m: device descriptor
  1278. *
  1279. * Gracefully stops the device, moving it to the lowest power
  1280. * consumption state possible.
  1281. */
  1282. void i2400m_dev_shutdown(struct i2400m *i2400m)
  1283. {
  1284. int result = -ENODEV;
  1285. struct device *dev = i2400m_dev(i2400m);
  1286. d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
  1287. result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
  1288. d_fnend(3, dev, "(i2400m %p) = void [%d]\n", i2400m, result);
  1289. return;
  1290. }