cmd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. * Copyright (C) 2008, cozybit Inc.
  3. * Copyright (C) 2003-2006, Marvell International Ltd.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/slab.h>
  12. #include "libertas_tf.h"
  13. static const struct channel_range channel_ranges[] = {
  14. { LBTF_REGDOMAIN_US, 1, 12 },
  15. { LBTF_REGDOMAIN_CA, 1, 12 },
  16. { LBTF_REGDOMAIN_EU, 1, 14 },
  17. { LBTF_REGDOMAIN_JP, 1, 14 },
  18. { LBTF_REGDOMAIN_SP, 1, 14 },
  19. { LBTF_REGDOMAIN_FR, 1, 14 },
  20. };
  21. static u16 lbtf_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
  22. {
  23. LBTF_REGDOMAIN_US, LBTF_REGDOMAIN_CA, LBTF_REGDOMAIN_EU,
  24. LBTF_REGDOMAIN_SP, LBTF_REGDOMAIN_FR, LBTF_REGDOMAIN_JP,
  25. };
  26. static struct cmd_ctrl_node *lbtf_get_cmd_ctrl_node(struct lbtf_private *priv);
  27. /**
  28. * lbtf_cmd_copyback - Simple callback that copies response back into command
  29. *
  30. * @priv A pointer to struct lbtf_private structure
  31. * @extra A pointer to the original command structure for which
  32. * 'resp' is a response
  33. * @resp A pointer to the command response
  34. *
  35. * Returns: 0 on success, error on failure
  36. */
  37. int lbtf_cmd_copyback(struct lbtf_private *priv, unsigned long extra,
  38. struct cmd_header *resp)
  39. {
  40. struct cmd_header *buf = (void *)extra;
  41. uint16_t copy_len;
  42. copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
  43. memcpy(buf, resp, copy_len);
  44. return 0;
  45. }
  46. EXPORT_SYMBOL_GPL(lbtf_cmd_copyback);
  47. #define CHAN_TO_IDX(chan) ((chan) - 1)
  48. static void lbtf_geo_init(struct lbtf_private *priv)
  49. {
  50. const struct channel_range *range = channel_ranges;
  51. u8 ch;
  52. int i;
  53. for (i = 0; i < ARRAY_SIZE(channel_ranges); i++)
  54. if (channel_ranges[i].regdomain == priv->regioncode) {
  55. range = &channel_ranges[i];
  56. break;
  57. }
  58. for (ch = priv->range.start; ch < priv->range.end; ch++)
  59. priv->channels[CHAN_TO_IDX(ch)].flags = 0;
  60. }
  61. /**
  62. * lbtf_update_hw_spec: Updates the hardware details.
  63. *
  64. * @priv A pointer to struct lbtf_private structure
  65. *
  66. * Returns: 0 on success, error on failure
  67. */
  68. int lbtf_update_hw_spec(struct lbtf_private *priv)
  69. {
  70. struct cmd_ds_get_hw_spec cmd;
  71. int ret = -1;
  72. u32 i;
  73. lbtf_deb_enter(LBTF_DEB_CMD);
  74. memset(&cmd, 0, sizeof(cmd));
  75. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  76. memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
  77. ret = lbtf_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
  78. if (ret)
  79. goto out;
  80. priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
  81. /* The firmware release is in an interesting format: the patch
  82. * level is in the most significant nibble ... so fix that: */
  83. priv->fwrelease = le32_to_cpu(cmd.fwrelease);
  84. priv->fwrelease = (priv->fwrelease << 8) |
  85. (priv->fwrelease >> 24 & 0xff);
  86. printk(KERN_INFO "libertastf: %pM, fw %u.%u.%up%u, cap 0x%08x\n",
  87. cmd.permanentaddr,
  88. priv->fwrelease >> 24 & 0xff,
  89. priv->fwrelease >> 16 & 0xff,
  90. priv->fwrelease >> 8 & 0xff,
  91. priv->fwrelease & 0xff,
  92. priv->fwcapinfo);
  93. lbtf_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
  94. cmd.hwifversion, cmd.version);
  95. /* Clamp region code to 8-bit since FW spec indicates that it should
  96. * only ever be 8-bit, even though the field size is 16-bit. Some
  97. * firmware returns non-zero high 8 bits here.
  98. */
  99. priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
  100. for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
  101. /* use the region code to search for the index */
  102. if (priv->regioncode == lbtf_region_code_to_index[i])
  103. break;
  104. }
  105. /* if it's unidentified region code, use the default (USA) */
  106. if (i >= MRVDRV_MAX_REGION_CODE) {
  107. priv->regioncode = 0x10;
  108. pr_info("unidentified region code; using the default (USA)\n");
  109. }
  110. if (priv->current_addr[0] == 0xff)
  111. memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
  112. SET_IEEE80211_PERM_ADDR(priv->hw, priv->current_addr);
  113. lbtf_geo_init(priv);
  114. out:
  115. lbtf_deb_leave(LBTF_DEB_CMD);
  116. return ret;
  117. }
  118. /**
  119. * lbtf_set_channel: Set the radio channel
  120. *
  121. * @priv A pointer to struct lbtf_private structure
  122. * @channel The desired channel, or 0 to clear a locked channel
  123. *
  124. * Returns: 0 on success, error on failure
  125. */
  126. int lbtf_set_channel(struct lbtf_private *priv, u8 channel)
  127. {
  128. int ret = 0;
  129. struct cmd_ds_802_11_rf_channel cmd;
  130. lbtf_deb_enter(LBTF_DEB_CMD);
  131. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  132. cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
  133. cmd.channel = cpu_to_le16(channel);
  134. ret = lbtf_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
  135. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
  136. return ret;
  137. }
  138. int lbtf_beacon_set(struct lbtf_private *priv, struct sk_buff *beacon)
  139. {
  140. struct cmd_ds_802_11_beacon_set cmd;
  141. int size;
  142. lbtf_deb_enter(LBTF_DEB_CMD);
  143. if (beacon->len > MRVL_MAX_BCN_SIZE) {
  144. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", -1);
  145. return -1;
  146. }
  147. size = sizeof(cmd) - sizeof(cmd.beacon) + beacon->len;
  148. cmd.hdr.size = cpu_to_le16(size);
  149. cmd.len = cpu_to_le16(beacon->len);
  150. memcpy(cmd.beacon, (u8 *) beacon->data, beacon->len);
  151. lbtf_cmd_async(priv, CMD_802_11_BEACON_SET, &cmd.hdr, size);
  152. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", 0);
  153. return 0;
  154. }
  155. int lbtf_beacon_ctrl(struct lbtf_private *priv, bool beacon_enable,
  156. int beacon_int)
  157. {
  158. struct cmd_ds_802_11_beacon_control cmd;
  159. lbtf_deb_enter(LBTF_DEB_CMD);
  160. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  161. cmd.action = cpu_to_le16(CMD_ACT_SET);
  162. cmd.beacon_enable = cpu_to_le16(beacon_enable);
  163. cmd.beacon_period = cpu_to_le16(beacon_int);
  164. lbtf_cmd_async(priv, CMD_802_11_BEACON_CTRL, &cmd.hdr, sizeof(cmd));
  165. lbtf_deb_leave(LBTF_DEB_CMD);
  166. return 0;
  167. }
  168. static void lbtf_queue_cmd(struct lbtf_private *priv,
  169. struct cmd_ctrl_node *cmdnode)
  170. {
  171. unsigned long flags;
  172. lbtf_deb_enter(LBTF_DEB_HOST);
  173. if (!cmdnode) {
  174. lbtf_deb_host("QUEUE_CMD: cmdnode is NULL\n");
  175. goto qcmd_done;
  176. }
  177. if (!cmdnode->cmdbuf->size) {
  178. lbtf_deb_host("DNLD_CMD: cmd size is zero\n");
  179. goto qcmd_done;
  180. }
  181. cmdnode->result = 0;
  182. spin_lock_irqsave(&priv->driver_lock, flags);
  183. list_add_tail(&cmdnode->list, &priv->cmdpendingq);
  184. spin_unlock_irqrestore(&priv->driver_lock, flags);
  185. lbtf_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
  186. le16_to_cpu(cmdnode->cmdbuf->command));
  187. qcmd_done:
  188. lbtf_deb_leave(LBTF_DEB_HOST);
  189. }
  190. static void lbtf_submit_command(struct lbtf_private *priv,
  191. struct cmd_ctrl_node *cmdnode)
  192. {
  193. unsigned long flags;
  194. struct cmd_header *cmd;
  195. uint16_t cmdsize;
  196. uint16_t command;
  197. int timeo = 5 * HZ;
  198. int ret;
  199. lbtf_deb_enter(LBTF_DEB_HOST);
  200. cmd = cmdnode->cmdbuf;
  201. spin_lock_irqsave(&priv->driver_lock, flags);
  202. priv->cur_cmd = cmdnode;
  203. cmdsize = le16_to_cpu(cmd->size);
  204. command = le16_to_cpu(cmd->command);
  205. lbtf_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
  206. command, le16_to_cpu(cmd->seqnum), cmdsize);
  207. lbtf_deb_hex(LBTF_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
  208. ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
  209. spin_unlock_irqrestore(&priv->driver_lock, flags);
  210. if (ret) {
  211. pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
  212. /* Let the timer kick in and retry, and potentially reset
  213. the whole thing if the condition persists */
  214. timeo = HZ;
  215. }
  216. /* Setup the timer after transmit command */
  217. mod_timer(&priv->command_timer, jiffies + timeo);
  218. lbtf_deb_leave(LBTF_DEB_HOST);
  219. }
  220. /**
  221. * This function inserts command node to cmdfreeq
  222. * after cleans it. Requires priv->driver_lock held.
  223. */
  224. static void __lbtf_cleanup_and_insert_cmd(struct lbtf_private *priv,
  225. struct cmd_ctrl_node *cmdnode)
  226. {
  227. lbtf_deb_enter(LBTF_DEB_HOST);
  228. if (!cmdnode)
  229. goto cl_ins_out;
  230. cmdnode->callback = NULL;
  231. cmdnode->callback_arg = 0;
  232. memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
  233. list_add_tail(&cmdnode->list, &priv->cmdfreeq);
  234. cl_ins_out:
  235. lbtf_deb_leave(LBTF_DEB_HOST);
  236. }
  237. static void lbtf_cleanup_and_insert_cmd(struct lbtf_private *priv,
  238. struct cmd_ctrl_node *ptempcmd)
  239. {
  240. unsigned long flags;
  241. spin_lock_irqsave(&priv->driver_lock, flags);
  242. __lbtf_cleanup_and_insert_cmd(priv, ptempcmd);
  243. spin_unlock_irqrestore(&priv->driver_lock, flags);
  244. }
  245. void lbtf_complete_command(struct lbtf_private *priv, struct cmd_ctrl_node *cmd,
  246. int result)
  247. {
  248. cmd->result = result;
  249. cmd->cmdwaitqwoken = 1;
  250. wake_up_interruptible(&cmd->cmdwait_q);
  251. if (!cmd->callback)
  252. __lbtf_cleanup_and_insert_cmd(priv, cmd);
  253. priv->cur_cmd = NULL;
  254. }
  255. int lbtf_cmd_set_mac_multicast_addr(struct lbtf_private *priv)
  256. {
  257. struct cmd_ds_mac_multicast_addr cmd;
  258. lbtf_deb_enter(LBTF_DEB_CMD);
  259. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  260. cmd.action = cpu_to_le16(CMD_ACT_SET);
  261. cmd.nr_of_adrs = cpu_to_le16((u16) priv->nr_of_multicastmacaddr);
  262. lbtf_deb_cmd("MULTICAST_ADR: setting %d addresses\n", cmd.nr_of_adrs);
  263. memcpy(cmd.maclist, priv->multicastlist,
  264. priv->nr_of_multicastmacaddr * ETH_ALEN);
  265. lbtf_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &cmd.hdr, sizeof(cmd));
  266. lbtf_deb_leave(LBTF_DEB_CMD);
  267. return 0;
  268. }
  269. void lbtf_set_mode(struct lbtf_private *priv, enum lbtf_mode mode)
  270. {
  271. struct cmd_ds_set_mode cmd;
  272. lbtf_deb_enter(LBTF_DEB_WEXT);
  273. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  274. cmd.mode = cpu_to_le16(mode);
  275. lbtf_deb_wext("Switching to mode: 0x%x\n", mode);
  276. lbtf_cmd_async(priv, CMD_802_11_SET_MODE, &cmd.hdr, sizeof(cmd));
  277. lbtf_deb_leave(LBTF_DEB_WEXT);
  278. }
  279. void lbtf_set_bssid(struct lbtf_private *priv, bool activate, const u8 *bssid)
  280. {
  281. struct cmd_ds_set_bssid cmd;
  282. lbtf_deb_enter(LBTF_DEB_CMD);
  283. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  284. cmd.activate = activate ? 1 : 0;
  285. if (activate)
  286. memcpy(cmd.bssid, bssid, ETH_ALEN);
  287. lbtf_cmd_async(priv, CMD_802_11_SET_BSSID, &cmd.hdr, sizeof(cmd));
  288. lbtf_deb_leave(LBTF_DEB_CMD);
  289. }
  290. int lbtf_set_mac_address(struct lbtf_private *priv, uint8_t *mac_addr)
  291. {
  292. struct cmd_ds_802_11_mac_address cmd;
  293. lbtf_deb_enter(LBTF_DEB_CMD);
  294. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  295. cmd.action = cpu_to_le16(CMD_ACT_SET);
  296. memcpy(cmd.macadd, mac_addr, ETH_ALEN);
  297. lbtf_cmd_async(priv, CMD_802_11_MAC_ADDRESS, &cmd.hdr, sizeof(cmd));
  298. lbtf_deb_leave(LBTF_DEB_CMD);
  299. return 0;
  300. }
  301. int lbtf_set_radio_control(struct lbtf_private *priv)
  302. {
  303. int ret = 0;
  304. struct cmd_ds_802_11_radio_control cmd;
  305. lbtf_deb_enter(LBTF_DEB_CMD);
  306. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  307. cmd.action = cpu_to_le16(CMD_ACT_SET);
  308. switch (priv->preamble) {
  309. case CMD_TYPE_SHORT_PREAMBLE:
  310. cmd.control = cpu_to_le16(SET_SHORT_PREAMBLE);
  311. break;
  312. case CMD_TYPE_LONG_PREAMBLE:
  313. cmd.control = cpu_to_le16(SET_LONG_PREAMBLE);
  314. break;
  315. case CMD_TYPE_AUTO_PREAMBLE:
  316. default:
  317. cmd.control = cpu_to_le16(SET_AUTO_PREAMBLE);
  318. break;
  319. }
  320. if (priv->radioon)
  321. cmd.control |= cpu_to_le16(TURN_ON_RF);
  322. else
  323. cmd.control &= cpu_to_le16(~TURN_ON_RF);
  324. lbtf_deb_cmd("RADIO_SET: radio %d, preamble %d\n", priv->radioon,
  325. priv->preamble);
  326. ret = lbtf_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
  327. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
  328. return ret;
  329. }
  330. void lbtf_set_mac_control(struct lbtf_private *priv)
  331. {
  332. struct cmd_ds_mac_control cmd;
  333. lbtf_deb_enter(LBTF_DEB_CMD);
  334. cmd.hdr.size = cpu_to_le16(sizeof(cmd));
  335. cmd.action = cpu_to_le16(priv->mac_control);
  336. cmd.reserved = 0;
  337. lbtf_cmd_async(priv, CMD_MAC_CONTROL,
  338. &cmd.hdr, sizeof(cmd));
  339. lbtf_deb_leave(LBTF_DEB_CMD);
  340. }
  341. /**
  342. * lbtf_allocate_cmd_buffer - Allocates cmd buffer, links it to free cmd queue
  343. *
  344. * @priv A pointer to struct lbtf_private structure
  345. *
  346. * Returns: 0 on success.
  347. */
  348. int lbtf_allocate_cmd_buffer(struct lbtf_private *priv)
  349. {
  350. int ret = 0;
  351. u32 bufsize;
  352. u32 i;
  353. struct cmd_ctrl_node *cmdarray;
  354. lbtf_deb_enter(LBTF_DEB_HOST);
  355. /* Allocate and initialize the command array */
  356. bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
  357. cmdarray = kzalloc(bufsize, GFP_KERNEL);
  358. if (!cmdarray) {
  359. lbtf_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
  360. ret = -1;
  361. goto done;
  362. }
  363. priv->cmd_array = cmdarray;
  364. /* Allocate and initialize each command buffer in the command array */
  365. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  366. cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
  367. if (!cmdarray[i].cmdbuf) {
  368. lbtf_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
  369. ret = -1;
  370. goto done;
  371. }
  372. }
  373. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  374. init_waitqueue_head(&cmdarray[i].cmdwait_q);
  375. lbtf_cleanup_and_insert_cmd(priv, &cmdarray[i]);
  376. }
  377. ret = 0;
  378. done:
  379. lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %d", ret);
  380. return ret;
  381. }
  382. /**
  383. * lbtf_free_cmd_buffer - Frees the cmd buffer.
  384. *
  385. * @priv A pointer to struct lbtf_private structure
  386. *
  387. * Returns: 0
  388. */
  389. int lbtf_free_cmd_buffer(struct lbtf_private *priv)
  390. {
  391. struct cmd_ctrl_node *cmdarray;
  392. unsigned int i;
  393. lbtf_deb_enter(LBTF_DEB_HOST);
  394. /* need to check if cmd array is allocated or not */
  395. if (priv->cmd_array == NULL) {
  396. lbtf_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
  397. goto done;
  398. }
  399. cmdarray = priv->cmd_array;
  400. /* Release shared memory buffers */
  401. for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
  402. kfree(cmdarray[i].cmdbuf);
  403. cmdarray[i].cmdbuf = NULL;
  404. }
  405. /* Release cmd_ctrl_node */
  406. kfree(priv->cmd_array);
  407. priv->cmd_array = NULL;
  408. done:
  409. lbtf_deb_leave(LBTF_DEB_HOST);
  410. return 0;
  411. }
  412. /**
  413. * lbtf_get_cmd_ctrl_node - Gets free cmd node from free cmd queue.
  414. *
  415. * @priv A pointer to struct lbtf_private structure
  416. *
  417. * Returns: pointer to a struct cmd_ctrl_node or NULL if none available.
  418. */
  419. static struct cmd_ctrl_node *lbtf_get_cmd_ctrl_node(struct lbtf_private *priv)
  420. {
  421. struct cmd_ctrl_node *tempnode;
  422. unsigned long flags;
  423. lbtf_deb_enter(LBTF_DEB_HOST);
  424. if (!priv)
  425. return NULL;
  426. spin_lock_irqsave(&priv->driver_lock, flags);
  427. if (!list_empty(&priv->cmdfreeq)) {
  428. tempnode = list_first_entry(&priv->cmdfreeq,
  429. struct cmd_ctrl_node, list);
  430. list_del(&tempnode->list);
  431. } else {
  432. lbtf_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
  433. tempnode = NULL;
  434. }
  435. spin_unlock_irqrestore(&priv->driver_lock, flags);
  436. lbtf_deb_leave(LBTF_DEB_HOST);
  437. return tempnode;
  438. }
  439. /**
  440. * lbtf_execute_next_command: execute next command in cmd pending queue.
  441. *
  442. * @priv A pointer to struct lbtf_private structure
  443. *
  444. * Returns: 0 on success.
  445. */
  446. int lbtf_execute_next_command(struct lbtf_private *priv)
  447. {
  448. struct cmd_ctrl_node *cmdnode = NULL;
  449. struct cmd_header *cmd;
  450. unsigned long flags;
  451. int ret = 0;
  452. /* Debug group is lbtf_deb_THREAD and not lbtf_deb_HOST, because the
  453. * only caller to us is lbtf_thread() and we get even when a
  454. * data packet is received */
  455. lbtf_deb_enter(LBTF_DEB_THREAD);
  456. spin_lock_irqsave(&priv->driver_lock, flags);
  457. if (priv->cur_cmd) {
  458. pr_alert("EXEC_NEXT_CMD: already processing command!\n");
  459. spin_unlock_irqrestore(&priv->driver_lock, flags);
  460. ret = -1;
  461. goto done;
  462. }
  463. if (!list_empty(&priv->cmdpendingq)) {
  464. cmdnode = list_first_entry(&priv->cmdpendingq,
  465. struct cmd_ctrl_node, list);
  466. }
  467. if (cmdnode) {
  468. cmd = cmdnode->cmdbuf;
  469. list_del(&cmdnode->list);
  470. lbtf_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
  471. le16_to_cpu(cmd->command));
  472. spin_unlock_irqrestore(&priv->driver_lock, flags);
  473. lbtf_submit_command(priv, cmdnode);
  474. } else
  475. spin_unlock_irqrestore(&priv->driver_lock, flags);
  476. ret = 0;
  477. done:
  478. lbtf_deb_leave(LBTF_DEB_THREAD);
  479. return ret;
  480. }
  481. static struct cmd_ctrl_node *__lbtf_cmd_async(struct lbtf_private *priv,
  482. uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
  483. int (*callback)(struct lbtf_private *, unsigned long,
  484. struct cmd_header *),
  485. unsigned long callback_arg)
  486. {
  487. struct cmd_ctrl_node *cmdnode;
  488. lbtf_deb_enter(LBTF_DEB_HOST);
  489. if (priv->surpriseremoved) {
  490. lbtf_deb_host("PREP_CMD: card removed\n");
  491. cmdnode = ERR_PTR(-ENOENT);
  492. goto done;
  493. }
  494. cmdnode = lbtf_get_cmd_ctrl_node(priv);
  495. if (cmdnode == NULL) {
  496. lbtf_deb_host("PREP_CMD: cmdnode is NULL\n");
  497. /* Wake up main thread to execute next command */
  498. queue_work(lbtf_wq, &priv->cmd_work);
  499. cmdnode = ERR_PTR(-ENOBUFS);
  500. goto done;
  501. }
  502. cmdnode->callback = callback;
  503. cmdnode->callback_arg = callback_arg;
  504. /* Copy the incoming command to the buffer */
  505. memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
  506. /* Set sequence number, clean result, move to buffer */
  507. priv->seqnum++;
  508. cmdnode->cmdbuf->command = cpu_to_le16(command);
  509. cmdnode->cmdbuf->size = cpu_to_le16(in_cmd_size);
  510. cmdnode->cmdbuf->seqnum = cpu_to_le16(priv->seqnum);
  511. cmdnode->cmdbuf->result = 0;
  512. lbtf_deb_host("PREP_CMD: command 0x%04x\n", command);
  513. cmdnode->cmdwaitqwoken = 0;
  514. lbtf_queue_cmd(priv, cmdnode);
  515. queue_work(lbtf_wq, &priv->cmd_work);
  516. done:
  517. lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %p", cmdnode);
  518. return cmdnode;
  519. }
  520. void lbtf_cmd_async(struct lbtf_private *priv, uint16_t command,
  521. struct cmd_header *in_cmd, int in_cmd_size)
  522. {
  523. lbtf_deb_enter(LBTF_DEB_CMD);
  524. __lbtf_cmd_async(priv, command, in_cmd, in_cmd_size, NULL, 0);
  525. lbtf_deb_leave(LBTF_DEB_CMD);
  526. }
  527. int __lbtf_cmd(struct lbtf_private *priv, uint16_t command,
  528. struct cmd_header *in_cmd, int in_cmd_size,
  529. int (*callback)(struct lbtf_private *,
  530. unsigned long, struct cmd_header *),
  531. unsigned long callback_arg)
  532. {
  533. struct cmd_ctrl_node *cmdnode;
  534. unsigned long flags;
  535. int ret = 0;
  536. lbtf_deb_enter(LBTF_DEB_HOST);
  537. cmdnode = __lbtf_cmd_async(priv, command, in_cmd, in_cmd_size,
  538. callback, callback_arg);
  539. if (IS_ERR(cmdnode)) {
  540. ret = PTR_ERR(cmdnode);
  541. goto done;
  542. }
  543. might_sleep();
  544. ret = wait_event_interruptible(cmdnode->cmdwait_q,
  545. cmdnode->cmdwaitqwoken);
  546. if (ret) {
  547. pr_info("PREP_CMD: command 0x%04x interrupted by signal: %d\n",
  548. command, ret);
  549. goto done;
  550. }
  551. spin_lock_irqsave(&priv->driver_lock, flags);
  552. ret = cmdnode->result;
  553. if (ret)
  554. pr_info("PREP_CMD: command 0x%04x failed: %d\n",
  555. command, ret);
  556. __lbtf_cleanup_and_insert_cmd(priv, cmdnode);
  557. spin_unlock_irqrestore(&priv->driver_lock, flags);
  558. done:
  559. lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %d", ret);
  560. return ret;
  561. }
  562. EXPORT_SYMBOL_GPL(__lbtf_cmd);
  563. /* Call holding driver_lock */
  564. void lbtf_cmd_response_rx(struct lbtf_private *priv)
  565. {
  566. priv->cmd_response_rxed = 1;
  567. queue_work(lbtf_wq, &priv->cmd_work);
  568. }
  569. EXPORT_SYMBOL_GPL(lbtf_cmd_response_rx);
  570. int lbtf_process_rx_command(struct lbtf_private *priv)
  571. {
  572. uint16_t respcmd, curcmd;
  573. struct cmd_header *resp;
  574. int ret = 0;
  575. unsigned long flags;
  576. uint16_t result;
  577. lbtf_deb_enter(LBTF_DEB_CMD);
  578. mutex_lock(&priv->lock);
  579. spin_lock_irqsave(&priv->driver_lock, flags);
  580. if (!priv->cur_cmd) {
  581. ret = -1;
  582. spin_unlock_irqrestore(&priv->driver_lock, flags);
  583. goto done;
  584. }
  585. resp = (void *)priv->cmd_resp_buff;
  586. curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
  587. respcmd = le16_to_cpu(resp->command);
  588. result = le16_to_cpu(resp->result);
  589. if (net_ratelimit())
  590. pr_info("libertastf: cmd response 0x%04x, seq %d, size %d\n",
  591. respcmd, le16_to_cpu(resp->seqnum),
  592. le16_to_cpu(resp->size));
  593. if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
  594. spin_unlock_irqrestore(&priv->driver_lock, flags);
  595. ret = -1;
  596. goto done;
  597. }
  598. if (respcmd != CMD_RET(curcmd)) {
  599. spin_unlock_irqrestore(&priv->driver_lock, flags);
  600. ret = -1;
  601. goto done;
  602. }
  603. if (resp->result == cpu_to_le16(0x0004)) {
  604. /* 0x0004 means -EAGAIN. Drop the response, let it time out
  605. and be resubmitted */
  606. spin_unlock_irqrestore(&priv->driver_lock, flags);
  607. ret = -1;
  608. goto done;
  609. }
  610. /* Now we got response from FW, cancel the command timer */
  611. del_timer(&priv->command_timer);
  612. priv->cmd_timed_out = 0;
  613. if (priv->nr_retries)
  614. priv->nr_retries = 0;
  615. /* If the command is not successful, cleanup and return failure */
  616. if ((result != 0 || !(respcmd & 0x8000))) {
  617. /*
  618. * Handling errors here
  619. */
  620. switch (respcmd) {
  621. case CMD_RET(CMD_GET_HW_SPEC):
  622. case CMD_RET(CMD_802_11_RESET):
  623. pr_info("libertastf: reset failed\n");
  624. break;
  625. }
  626. lbtf_complete_command(priv, priv->cur_cmd, result);
  627. spin_unlock_irqrestore(&priv->driver_lock, flags);
  628. ret = -1;
  629. goto done;
  630. }
  631. spin_unlock_irqrestore(&priv->driver_lock, flags);
  632. if (priv->cur_cmd && priv->cur_cmd->callback) {
  633. ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
  634. resp);
  635. }
  636. spin_lock_irqsave(&priv->driver_lock, flags);
  637. if (priv->cur_cmd) {
  638. /* Clean up and Put current command back to cmdfreeq */
  639. lbtf_complete_command(priv, priv->cur_cmd, result);
  640. }
  641. spin_unlock_irqrestore(&priv->driver_lock, flags);
  642. done:
  643. mutex_unlock(&priv->lock);
  644. lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
  645. return ret;
  646. }