control.c 42 KB

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