cmd.c 20 KB

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