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