control.c 43 KB

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