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