cmd.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  1. /*
  2. * This file is part of wl1271
  3. *
  4. * Copyright (C) 2009-2010 Nokia Corporation
  5. *
  6. * Contact: Luciano Coelho <luciano.coelho@nokia.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/spi/spi.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ieee80211.h>
  28. #include <linux/slab.h>
  29. #include "wlcore.h"
  30. #include "debug.h"
  31. #include "io.h"
  32. #include "acx.h"
  33. #include "wl12xx_80211.h"
  34. #include "cmd.h"
  35. #include "event.h"
  36. #include "tx.h"
  37. #include "hw_ops.h"
  38. #define WL1271_CMD_FAST_POLL_COUNT 50
  39. #define WL1271_WAIT_EVENT_FAST_POLL_COUNT 20
  40. /*
  41. * send command to firmware
  42. *
  43. * @wl: wl struct
  44. * @id: command id
  45. * @buf: buffer containing the command, must work with dma
  46. * @len: length of the buffer
  47. */
  48. int wl1271_cmd_send(struct wl1271 *wl, u16 id, void *buf, size_t len,
  49. size_t res_len)
  50. {
  51. struct wl1271_cmd_header *cmd;
  52. unsigned long timeout;
  53. u32 intr;
  54. int ret = 0;
  55. u16 status;
  56. u16 poll_count = 0;
  57. if (WARN_ON(unlikely(wl->state == WLCORE_STATE_RESTARTING)))
  58. return -EIO;
  59. cmd = buf;
  60. cmd->id = cpu_to_le16(id);
  61. cmd->status = 0;
  62. WARN_ON(len % 4 != 0);
  63. WARN_ON(test_bit(WL1271_FLAG_IN_ELP, &wl->flags));
  64. ret = wlcore_write(wl, wl->cmd_box_addr, buf, len, false);
  65. if (ret < 0)
  66. goto fail;
  67. /*
  68. * TODO: we just need this because one bit is in a different
  69. * place. Is there any better way?
  70. */
  71. ret = wl->ops->trigger_cmd(wl, wl->cmd_box_addr, buf, len);
  72. if (ret < 0)
  73. goto fail;
  74. timeout = jiffies + msecs_to_jiffies(WL1271_COMMAND_TIMEOUT);
  75. ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  76. if (ret < 0)
  77. goto fail;
  78. while (!(intr & WL1271_ACX_INTR_CMD_COMPLETE)) {
  79. if (time_after(jiffies, timeout)) {
  80. wl1271_error("command complete timeout");
  81. ret = -ETIMEDOUT;
  82. goto fail;
  83. }
  84. poll_count++;
  85. if (poll_count < WL1271_CMD_FAST_POLL_COUNT)
  86. udelay(10);
  87. else
  88. msleep(1);
  89. ret = wlcore_read_reg(wl, REG_INTERRUPT_NO_CLEAR, &intr);
  90. if (ret < 0)
  91. goto fail;
  92. }
  93. /* read back the status code of the command */
  94. if (res_len == 0)
  95. res_len = sizeof(struct wl1271_cmd_header);
  96. ret = wlcore_read(wl, wl->cmd_box_addr, cmd, res_len, false);
  97. if (ret < 0)
  98. goto fail;
  99. status = le16_to_cpu(cmd->status);
  100. if (status != CMD_STATUS_SUCCESS) {
  101. wl1271_error("command execute failure %d", status);
  102. ret = -EIO;
  103. goto fail;
  104. }
  105. ret = wlcore_write_reg(wl, REG_INTERRUPT_ACK,
  106. WL1271_ACX_INTR_CMD_COMPLETE);
  107. if (ret < 0)
  108. goto fail;
  109. return 0;
  110. fail:
  111. wl12xx_queue_recovery_work(wl);
  112. return ret;
  113. }
  114. EXPORT_SYMBOL_GPL(wl1271_cmd_send);
  115. /*
  116. * Poll the mailbox event field until any of the bits in the mask is set or a
  117. * timeout occurs (WL1271_EVENT_TIMEOUT in msecs)
  118. */
  119. int wlcore_cmd_wait_for_event_or_timeout(struct wl1271 *wl,
  120. u32 mask, bool *timeout)
  121. {
  122. u32 *events_vector;
  123. u32 event;
  124. unsigned long timeout_time;
  125. u16 poll_count = 0;
  126. int ret = 0;
  127. *timeout = false;
  128. events_vector = kmalloc(sizeof(*events_vector), GFP_KERNEL | GFP_DMA);
  129. if (!events_vector)
  130. return -ENOMEM;
  131. timeout_time = jiffies + msecs_to_jiffies(WL1271_EVENT_TIMEOUT);
  132. do {
  133. if (time_after(jiffies, timeout_time)) {
  134. wl1271_debug(DEBUG_CMD, "timeout waiting for event %d",
  135. (int)mask);
  136. *timeout = true;
  137. goto out;
  138. }
  139. poll_count++;
  140. if (poll_count < WL1271_WAIT_EVENT_FAST_POLL_COUNT)
  141. usleep_range(50, 51);
  142. else
  143. usleep_range(1000, 5000);
  144. /* read from both event fields */
  145. ret = wlcore_read(wl, wl->mbox_ptr[0], events_vector,
  146. sizeof(*events_vector), false);
  147. if (ret < 0)
  148. goto out;
  149. event = *events_vector & mask;
  150. ret = wlcore_read(wl, wl->mbox_ptr[1], events_vector,
  151. sizeof(*events_vector), false);
  152. if (ret < 0)
  153. goto out;
  154. event |= *events_vector & mask;
  155. } while (!event);
  156. out:
  157. kfree(events_vector);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL_GPL(wlcore_cmd_wait_for_event_or_timeout);
  161. int wl12xx_cmd_role_enable(struct wl1271 *wl, u8 *addr, u8 role_type,
  162. u8 *role_id)
  163. {
  164. struct wl12xx_cmd_role_enable *cmd;
  165. int ret;
  166. wl1271_debug(DEBUG_CMD, "cmd role enable");
  167. if (WARN_ON(*role_id != WL12XX_INVALID_ROLE_ID))
  168. return -EBUSY;
  169. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  170. if (!cmd) {
  171. ret = -ENOMEM;
  172. goto out;
  173. }
  174. /* get role id */
  175. cmd->role_id = find_first_zero_bit(wl->roles_map, WL12XX_MAX_ROLES);
  176. if (cmd->role_id >= WL12XX_MAX_ROLES) {
  177. ret = -EBUSY;
  178. goto out_free;
  179. }
  180. memcpy(cmd->mac_address, addr, ETH_ALEN);
  181. cmd->role_type = role_type;
  182. ret = wl1271_cmd_send(wl, CMD_ROLE_ENABLE, cmd, sizeof(*cmd), 0);
  183. if (ret < 0) {
  184. wl1271_error("failed to initiate cmd role enable");
  185. goto out_free;
  186. }
  187. __set_bit(cmd->role_id, wl->roles_map);
  188. *role_id = cmd->role_id;
  189. out_free:
  190. kfree(cmd);
  191. out:
  192. return ret;
  193. }
  194. int wl12xx_cmd_role_disable(struct wl1271 *wl, u8 *role_id)
  195. {
  196. struct wl12xx_cmd_role_disable *cmd;
  197. int ret;
  198. wl1271_debug(DEBUG_CMD, "cmd role disable");
  199. if (WARN_ON(*role_id == WL12XX_INVALID_ROLE_ID))
  200. return -ENOENT;
  201. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  202. if (!cmd) {
  203. ret = -ENOMEM;
  204. goto out;
  205. }
  206. cmd->role_id = *role_id;
  207. ret = wl1271_cmd_send(wl, CMD_ROLE_DISABLE, cmd, sizeof(*cmd), 0);
  208. if (ret < 0) {
  209. wl1271_error("failed to initiate cmd role disable");
  210. goto out_free;
  211. }
  212. __clear_bit(*role_id, wl->roles_map);
  213. *role_id = WL12XX_INVALID_ROLE_ID;
  214. out_free:
  215. kfree(cmd);
  216. out:
  217. return ret;
  218. }
  219. int wl12xx_allocate_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  220. {
  221. unsigned long flags;
  222. u8 link = find_first_zero_bit(wl->links_map, WL12XX_MAX_LINKS);
  223. if (link >= WL12XX_MAX_LINKS)
  224. return -EBUSY;
  225. /* these bits are used by op_tx */
  226. spin_lock_irqsave(&wl->wl_lock, flags);
  227. __set_bit(link, wl->links_map);
  228. __set_bit(link, wlvif->links_map);
  229. spin_unlock_irqrestore(&wl->wl_lock, flags);
  230. *hlid = link;
  231. return 0;
  232. }
  233. void wl12xx_free_link(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 *hlid)
  234. {
  235. unsigned long flags;
  236. if (*hlid == WL12XX_INVALID_LINK_ID)
  237. return;
  238. /* these bits are used by op_tx */
  239. spin_lock_irqsave(&wl->wl_lock, flags);
  240. __clear_bit(*hlid, wl->links_map);
  241. __clear_bit(*hlid, wlvif->links_map);
  242. spin_unlock_irqrestore(&wl->wl_lock, flags);
  243. /*
  244. * At this point op_tx() will not add more packets to the queues. We
  245. * can purge them.
  246. */
  247. wl1271_tx_reset_link_queues(wl, *hlid);
  248. *hlid = WL12XX_INVALID_LINK_ID;
  249. }
  250. static int wl12xx_get_new_session_id(struct wl1271 *wl,
  251. struct wl12xx_vif *wlvif)
  252. {
  253. if (wlvif->session_counter >= SESSION_COUNTER_MAX)
  254. wlvif->session_counter = 0;
  255. wlvif->session_counter++;
  256. return wlvif->session_counter;
  257. }
  258. static u8 wlcore_get_native_channel_type(u8 nl_channel_type)
  259. {
  260. switch (nl_channel_type) {
  261. case NL80211_CHAN_NO_HT:
  262. return WLCORE_CHAN_NO_HT;
  263. case NL80211_CHAN_HT20:
  264. return WLCORE_CHAN_HT20;
  265. case NL80211_CHAN_HT40MINUS:
  266. return WLCORE_CHAN_HT40MINUS;
  267. case NL80211_CHAN_HT40PLUS:
  268. return WLCORE_CHAN_HT40PLUS;
  269. default:
  270. WARN_ON(1);
  271. return WLCORE_CHAN_NO_HT;
  272. }
  273. }
  274. static int wl12xx_cmd_role_start_dev(struct wl1271 *wl,
  275. struct wl12xx_vif *wlvif,
  276. enum ieee80211_band band,
  277. int channel)
  278. {
  279. struct wl12xx_cmd_role_start *cmd;
  280. int ret;
  281. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  282. if (!cmd) {
  283. ret = -ENOMEM;
  284. goto out;
  285. }
  286. wl1271_debug(DEBUG_CMD, "cmd role start dev %d", wlvif->dev_role_id);
  287. cmd->role_id = wlvif->dev_role_id;
  288. if (band == IEEE80211_BAND_5GHZ)
  289. cmd->band = WLCORE_BAND_5GHZ;
  290. cmd->channel = channel;
  291. if (wlvif->dev_hlid == WL12XX_INVALID_LINK_ID) {
  292. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->dev_hlid);
  293. if (ret)
  294. goto out_free;
  295. }
  296. cmd->device.hlid = wlvif->dev_hlid;
  297. cmd->device.session = wl12xx_get_new_session_id(wl, wlvif);
  298. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d",
  299. cmd->role_id, cmd->device.hlid, cmd->device.session);
  300. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  301. if (ret < 0) {
  302. wl1271_error("failed to initiate cmd role enable");
  303. goto err_hlid;
  304. }
  305. goto out_free;
  306. err_hlid:
  307. /* clear links on error */
  308. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  309. out_free:
  310. kfree(cmd);
  311. out:
  312. return ret;
  313. }
  314. static int wl12xx_cmd_role_stop_dev(struct wl1271 *wl,
  315. struct wl12xx_vif *wlvif)
  316. {
  317. struct wl12xx_cmd_role_stop *cmd;
  318. int ret;
  319. if (WARN_ON(wlvif->dev_hlid == WL12XX_INVALID_LINK_ID))
  320. return -EINVAL;
  321. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  322. if (!cmd) {
  323. ret = -ENOMEM;
  324. goto out;
  325. }
  326. wl1271_debug(DEBUG_CMD, "cmd role stop dev");
  327. cmd->role_id = wlvif->dev_role_id;
  328. cmd->disc_type = DISCONNECT_IMMEDIATE;
  329. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  330. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  331. if (ret < 0) {
  332. wl1271_error("failed to initiate cmd role stop");
  333. goto out_free;
  334. }
  335. wl12xx_free_link(wl, wlvif, &wlvif->dev_hlid);
  336. out_free:
  337. kfree(cmd);
  338. out:
  339. return ret;
  340. }
  341. int wl12xx_cmd_role_start_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  342. {
  343. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  344. struct wl12xx_cmd_role_start *cmd;
  345. u32 supported_rates;
  346. int ret;
  347. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  348. if (!cmd) {
  349. ret = -ENOMEM;
  350. goto out;
  351. }
  352. wl1271_debug(DEBUG_CMD, "cmd role start sta %d", wlvif->role_id);
  353. cmd->role_id = wlvif->role_id;
  354. if (wlvif->band == IEEE80211_BAND_5GHZ)
  355. cmd->band = WLCORE_BAND_5GHZ;
  356. cmd->channel = wlvif->channel;
  357. cmd->sta.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  358. cmd->sta.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  359. cmd->sta.ssid_type = WL12XX_SSID_TYPE_ANY;
  360. cmd->sta.ssid_len = wlvif->ssid_len;
  361. memcpy(cmd->sta.ssid, wlvif->ssid, wlvif->ssid_len);
  362. memcpy(cmd->sta.bssid, vif->bss_conf.bssid, ETH_ALEN);
  363. supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
  364. wlcore_hw_sta_get_ap_rate_mask(wl, wlvif);
  365. if (wlvif->p2p)
  366. supported_rates &= ~CONF_TX_CCK_RATES;
  367. cmd->sta.local_rates = cpu_to_le32(supported_rates);
  368. cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
  369. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  370. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  371. if (ret)
  372. goto out_free;
  373. }
  374. cmd->sta.hlid = wlvif->sta.hlid;
  375. cmd->sta.session = wl12xx_get_new_session_id(wl, wlvif);
  376. /*
  377. * We don't have the correct remote rates in this stage, and there
  378. * is no way to update them later, so use our supported rates instead.
  379. * The fw will take the configured rate policies into account anyway.
  380. */
  381. cmd->sta.remote_rates = cpu_to_le32(supported_rates);
  382. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  383. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  384. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  385. wlvif->basic_rate_set, wlvif->rate_set);
  386. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  387. if (ret < 0) {
  388. wl1271_error("failed to initiate cmd role start sta");
  389. goto err_hlid;
  390. }
  391. goto out_free;
  392. err_hlid:
  393. /* clear links on error. */
  394. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  395. out_free:
  396. kfree(cmd);
  397. out:
  398. return ret;
  399. }
  400. /* use this function to stop ibss as well */
  401. int wl12xx_cmd_role_stop_sta(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  402. {
  403. struct wl12xx_cmd_role_stop *cmd;
  404. int ret;
  405. if (WARN_ON(wlvif->sta.hlid == WL12XX_INVALID_LINK_ID))
  406. return -EINVAL;
  407. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  408. if (!cmd) {
  409. ret = -ENOMEM;
  410. goto out;
  411. }
  412. wl1271_debug(DEBUG_CMD, "cmd role stop sta %d", wlvif->role_id);
  413. cmd->role_id = wlvif->role_id;
  414. cmd->disc_type = DISCONNECT_IMMEDIATE;
  415. cmd->reason = cpu_to_le16(WLAN_REASON_UNSPECIFIED);
  416. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  417. if (ret < 0) {
  418. wl1271_error("failed to initiate cmd role stop sta");
  419. goto out_free;
  420. }
  421. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  422. out_free:
  423. kfree(cmd);
  424. out:
  425. return ret;
  426. }
  427. int wl12xx_cmd_role_start_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  428. {
  429. struct wl12xx_cmd_role_start *cmd;
  430. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  431. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  432. u32 supported_rates;
  433. int ret;
  434. wl1271_debug(DEBUG_CMD, "cmd role start ap %d", wlvif->role_id);
  435. /* trying to use hidden SSID with an old hostapd version */
  436. if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
  437. wl1271_error("got a null SSID from beacon/bss");
  438. ret = -EINVAL;
  439. goto out;
  440. }
  441. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  442. if (!cmd) {
  443. ret = -ENOMEM;
  444. goto out;
  445. }
  446. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
  447. if (ret < 0)
  448. goto out_free;
  449. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  450. if (ret < 0)
  451. goto out_free_global;
  452. cmd->role_id = wlvif->role_id;
  453. cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
  454. cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
  455. cmd->ap.global_hlid = wlvif->ap.global_hlid;
  456. cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
  457. cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  458. cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  459. cmd->ap.dtim_interval = bss_conf->dtim_period;
  460. cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
  461. /* FIXME: Change when adding DFS */
  462. cmd->ap.reset_tsf = 1; /* By default reset AP TSF */
  463. cmd->channel = wlvif->channel;
  464. cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
  465. if (!bss_conf->hidden_ssid) {
  466. /* take the SSID from the beacon for backward compatibility */
  467. cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
  468. cmd->ap.ssid_len = wlvif->ssid_len;
  469. memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
  470. } else {
  471. cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
  472. cmd->ap.ssid_len = bss_conf->ssid_len;
  473. memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
  474. }
  475. supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
  476. wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
  477. wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
  478. supported_rates);
  479. cmd->ap.local_rates = cpu_to_le32(supported_rates);
  480. switch (wlvif->band) {
  481. case IEEE80211_BAND_2GHZ:
  482. cmd->band = WLCORE_BAND_2_4GHZ;
  483. break;
  484. case IEEE80211_BAND_5GHZ:
  485. cmd->band = WLCORE_BAND_5GHZ;
  486. break;
  487. default:
  488. wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
  489. cmd->band = WLCORE_BAND_2_4GHZ;
  490. break;
  491. }
  492. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  493. if (ret < 0) {
  494. wl1271_error("failed to initiate cmd role start ap");
  495. goto out_free_bcast;
  496. }
  497. goto out_free;
  498. out_free_bcast:
  499. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  500. out_free_global:
  501. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  502. out_free:
  503. kfree(cmd);
  504. out:
  505. return ret;
  506. }
  507. int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  508. {
  509. struct wl12xx_cmd_role_stop *cmd;
  510. int ret;
  511. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  512. if (!cmd) {
  513. ret = -ENOMEM;
  514. goto out;
  515. }
  516. wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
  517. cmd->role_id = wlvif->role_id;
  518. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  519. if (ret < 0) {
  520. wl1271_error("failed to initiate cmd role stop ap");
  521. goto out_free;
  522. }
  523. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  524. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  525. out_free:
  526. kfree(cmd);
  527. out:
  528. return ret;
  529. }
  530. int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  531. {
  532. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  533. struct wl12xx_cmd_role_start *cmd;
  534. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  535. int ret;
  536. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  537. if (!cmd) {
  538. ret = -ENOMEM;
  539. goto out;
  540. }
  541. wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
  542. cmd->role_id = wlvif->role_id;
  543. if (wlvif->band == IEEE80211_BAND_5GHZ)
  544. cmd->band = WLCORE_BAND_5GHZ;
  545. cmd->channel = wlvif->channel;
  546. cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  547. cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  548. cmd->ibss.dtim_interval = bss_conf->dtim_period;
  549. cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
  550. cmd->ibss.ssid_len = wlvif->ssid_len;
  551. memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
  552. memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
  553. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  554. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  555. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  556. if (ret)
  557. goto out_free;
  558. }
  559. cmd->ibss.hlid = wlvif->sta.hlid;
  560. cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
  561. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  562. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  563. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  564. wlvif->basic_rate_set, wlvif->rate_set);
  565. wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
  566. vif->bss_conf.bssid);
  567. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  568. if (ret < 0) {
  569. wl1271_error("failed to initiate cmd role enable");
  570. goto err_hlid;
  571. }
  572. goto out_free;
  573. err_hlid:
  574. /* clear links on error. */
  575. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  576. out_free:
  577. kfree(cmd);
  578. out:
  579. return ret;
  580. }
  581. /**
  582. * send test command to firmware
  583. *
  584. * @wl: wl struct
  585. * @buf: buffer containing the command, with all headers, must work with dma
  586. * @len: length of the buffer
  587. * @answer: is answer needed
  588. */
  589. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  590. {
  591. int ret;
  592. size_t res_len = 0;
  593. wl1271_debug(DEBUG_CMD, "cmd test");
  594. if (answer)
  595. res_len = buf_len;
  596. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  597. if (ret < 0) {
  598. wl1271_warning("TEST command failed");
  599. return ret;
  600. }
  601. return ret;
  602. }
  603. EXPORT_SYMBOL_GPL(wl1271_cmd_test);
  604. /**
  605. * read acx from firmware
  606. *
  607. * @wl: wl struct
  608. * @id: acx id
  609. * @buf: buffer for the response, including all headers, must work with dma
  610. * @len: length of buf
  611. */
  612. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf, size_t len)
  613. {
  614. struct acx_header *acx = buf;
  615. int ret;
  616. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  617. acx->id = cpu_to_le16(id);
  618. /* payload length, does not include any headers */
  619. acx->len = cpu_to_le16(len - sizeof(*acx));
  620. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, sizeof(*acx), len);
  621. if (ret < 0)
  622. wl1271_error("INTERROGATE command failed");
  623. return ret;
  624. }
  625. /**
  626. * write acx value to firmware
  627. *
  628. * @wl: wl struct
  629. * @id: acx id
  630. * @buf: buffer containing acx, including all headers, must work with dma
  631. * @len: length of buf
  632. */
  633. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  634. {
  635. struct acx_header *acx = buf;
  636. int ret;
  637. wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
  638. acx->id = cpu_to_le16(id);
  639. /* payload length, does not include any headers */
  640. acx->len = cpu_to_le16(len - sizeof(*acx));
  641. ret = wl1271_cmd_send(wl, CMD_CONFIGURE, acx, len, 0);
  642. if (ret < 0) {
  643. wl1271_warning("CONFIGURE command NOK");
  644. return ret;
  645. }
  646. return 0;
  647. }
  648. EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
  649. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  650. {
  651. struct cmd_enabledisable_path *cmd;
  652. int ret;
  653. u16 cmd_rx, cmd_tx;
  654. wl1271_debug(DEBUG_CMD, "cmd data path");
  655. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  656. if (!cmd) {
  657. ret = -ENOMEM;
  658. goto out;
  659. }
  660. /* the channel here is only used for calibration, so hardcoded to 1 */
  661. cmd->channel = 1;
  662. if (enable) {
  663. cmd_rx = CMD_ENABLE_RX;
  664. cmd_tx = CMD_ENABLE_TX;
  665. } else {
  666. cmd_rx = CMD_DISABLE_RX;
  667. cmd_tx = CMD_DISABLE_TX;
  668. }
  669. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  670. if (ret < 0) {
  671. wl1271_error("rx %s cmd for channel %d failed",
  672. enable ? "start" : "stop", cmd->channel);
  673. goto out;
  674. }
  675. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  676. enable ? "start" : "stop", cmd->channel);
  677. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  678. if (ret < 0) {
  679. wl1271_error("tx %s cmd for channel %d failed",
  680. enable ? "start" : "stop", cmd->channel);
  681. goto out;
  682. }
  683. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  684. enable ? "start" : "stop", cmd->channel);
  685. out:
  686. kfree(cmd);
  687. return ret;
  688. }
  689. EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
  690. int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  691. u8 ps_mode, u16 auto_ps_timeout)
  692. {
  693. struct wl1271_cmd_ps_params *ps_params = NULL;
  694. int ret = 0;
  695. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  696. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  697. if (!ps_params) {
  698. ret = -ENOMEM;
  699. goto out;
  700. }
  701. ps_params->role_id = wlvif->role_id;
  702. ps_params->ps_mode = ps_mode;
  703. ps_params->auto_ps_timeout = auto_ps_timeout;
  704. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  705. sizeof(*ps_params), 0);
  706. if (ret < 0) {
  707. wl1271_error("cmd set_ps_mode failed");
  708. goto out;
  709. }
  710. out:
  711. kfree(ps_params);
  712. return ret;
  713. }
  714. int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
  715. u16 template_id, void *buf, size_t buf_len,
  716. int index, u32 rates)
  717. {
  718. struct wl1271_cmd_template_set *cmd;
  719. int ret = 0;
  720. wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
  721. template_id, role_id);
  722. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  723. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  724. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  725. if (!cmd) {
  726. ret = -ENOMEM;
  727. goto out;
  728. }
  729. /* during initialization wlvif is NULL */
  730. cmd->role_id = role_id;
  731. cmd->len = cpu_to_le16(buf_len);
  732. cmd->template_type = template_id;
  733. cmd->enabled_rates = cpu_to_le32(rates);
  734. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  735. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  736. cmd->index = index;
  737. if (buf)
  738. memcpy(cmd->template_data, buf, buf_len);
  739. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  740. if (ret < 0) {
  741. wl1271_warning("cmd set_template failed: %d", ret);
  742. goto out_free;
  743. }
  744. out_free:
  745. kfree(cmd);
  746. out:
  747. return ret;
  748. }
  749. int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  750. {
  751. struct sk_buff *skb = NULL;
  752. int size;
  753. void *ptr;
  754. int ret = -ENOMEM;
  755. if (wlvif->bss_type == BSS_TYPE_IBSS) {
  756. size = sizeof(struct wl12xx_null_data_template);
  757. ptr = NULL;
  758. } else {
  759. skb = ieee80211_nullfunc_get(wl->hw,
  760. wl12xx_wlvif_to_vif(wlvif));
  761. if (!skb)
  762. goto out;
  763. size = skb->len;
  764. ptr = skb->data;
  765. }
  766. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  767. CMD_TEMPL_NULL_DATA, ptr, size, 0,
  768. wlvif->basic_rate);
  769. out:
  770. dev_kfree_skb(skb);
  771. if (ret)
  772. wl1271_warning("cmd buld null data failed %d", ret);
  773. return ret;
  774. }
  775. int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
  776. struct wl12xx_vif *wlvif)
  777. {
  778. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  779. struct sk_buff *skb = NULL;
  780. int ret = -ENOMEM;
  781. skb = ieee80211_nullfunc_get(wl->hw, vif);
  782. if (!skb)
  783. goto out;
  784. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
  785. skb->data, skb->len,
  786. wlvif->sta.klv_template_id,
  787. wlvif->basic_rate);
  788. out:
  789. dev_kfree_skb(skb);
  790. if (ret)
  791. wl1271_warning("cmd build klv null data failed %d", ret);
  792. return ret;
  793. }
  794. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  795. u16 aid)
  796. {
  797. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  798. struct sk_buff *skb;
  799. int ret = 0;
  800. skb = ieee80211_pspoll_get(wl->hw, vif);
  801. if (!skb)
  802. goto out;
  803. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  804. CMD_TEMPL_PS_POLL, skb->data,
  805. skb->len, 0, wlvif->basic_rate_set);
  806. out:
  807. dev_kfree_skb(skb);
  808. return ret;
  809. }
  810. int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  811. u8 role_id, u8 band,
  812. const u8 *ssid, size_t ssid_len,
  813. const u8 *ie, size_t ie_len, bool sched_scan)
  814. {
  815. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  816. struct sk_buff *skb;
  817. int ret;
  818. u32 rate;
  819. u16 template_id_2_4 = wl->scan_templ_id_2_4;
  820. u16 template_id_5 = wl->scan_templ_id_5;
  821. skb = ieee80211_probereq_get(wl->hw, vif, ssid, ssid_len,
  822. ie, ie_len);
  823. if (!skb) {
  824. ret = -ENOMEM;
  825. goto out;
  826. }
  827. wl1271_dump(DEBUG_SCAN, "PROBE REQ: ", skb->data, skb->len);
  828. if (sched_scan &&
  829. (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
  830. template_id_2_4 = wl->sched_scan_templ_id_2_4;
  831. template_id_5 = wl->sched_scan_templ_id_5;
  832. }
  833. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  834. if (band == IEEE80211_BAND_2GHZ)
  835. ret = wl1271_cmd_template_set(wl, role_id,
  836. template_id_2_4,
  837. skb->data, skb->len, 0, rate);
  838. else
  839. ret = wl1271_cmd_template_set(wl, role_id,
  840. template_id_5,
  841. skb->data, skb->len, 0, rate);
  842. out:
  843. dev_kfree_skb(skb);
  844. return ret;
  845. }
  846. EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
  847. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  848. struct wl12xx_vif *wlvif,
  849. struct sk_buff *skb)
  850. {
  851. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  852. int ret;
  853. u32 rate;
  854. if (!skb)
  855. skb = ieee80211_ap_probereq_get(wl->hw, vif);
  856. if (!skb)
  857. goto out;
  858. wl1271_dump(DEBUG_SCAN, "AP PROBE REQ: ", skb->data, skb->len);
  859. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
  860. if (wlvif->band == IEEE80211_BAND_2GHZ)
  861. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  862. CMD_TEMPL_CFG_PROBE_REQ_2_4,
  863. skb->data, skb->len, 0, rate);
  864. else
  865. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  866. CMD_TEMPL_CFG_PROBE_REQ_5,
  867. skb->data, skb->len, 0, rate);
  868. if (ret < 0)
  869. wl1271_error("Unable to set ap probe request template.");
  870. out:
  871. return skb;
  872. }
  873. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  874. {
  875. int ret, extra = 0;
  876. u16 fc;
  877. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  878. struct sk_buff *skb;
  879. struct wl12xx_arp_rsp_template *tmpl;
  880. struct ieee80211_hdr_3addr *hdr;
  881. struct arphdr *arp_hdr;
  882. skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
  883. WL1271_EXTRA_SPACE_MAX);
  884. if (!skb) {
  885. wl1271_error("failed to allocate buffer for arp rsp template");
  886. return -ENOMEM;
  887. }
  888. skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
  889. tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
  890. memset(tmpl, 0, sizeof(*tmpl));
  891. /* llc layer */
  892. memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  893. tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
  894. /* arp header */
  895. arp_hdr = &tmpl->arp_hdr;
  896. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  897. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  898. arp_hdr->ar_hln = ETH_ALEN;
  899. arp_hdr->ar_pln = 4;
  900. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  901. /* arp payload */
  902. memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
  903. tmpl->sender_ip = wlvif->ip_addr;
  904. /* encryption space */
  905. switch (wlvif->encryption_type) {
  906. case KEY_TKIP:
  907. if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
  908. extra = WL1271_EXTRA_SPACE_TKIP;
  909. break;
  910. case KEY_AES:
  911. extra = WL1271_EXTRA_SPACE_AES;
  912. break;
  913. case KEY_NONE:
  914. case KEY_WEP:
  915. case KEY_GEM:
  916. extra = 0;
  917. break;
  918. default:
  919. wl1271_warning("Unknown encryption type: %d",
  920. wlvif->encryption_type);
  921. ret = -EINVAL;
  922. goto out;
  923. }
  924. if (extra) {
  925. u8 *space = skb_push(skb, extra);
  926. memset(space, 0, extra);
  927. }
  928. /* QoS header - BE */
  929. if (wlvif->sta.qos)
  930. memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
  931. /* mac80211 header */
  932. hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
  933. memset(hdr, 0, sizeof(*hdr));
  934. fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
  935. if (wlvif->sta.qos)
  936. fc |= IEEE80211_STYPE_QOS_DATA;
  937. else
  938. fc |= IEEE80211_STYPE_DATA;
  939. if (wlvif->encryption_type != KEY_NONE)
  940. fc |= IEEE80211_FCTL_PROTECTED;
  941. hdr->frame_control = cpu_to_le16(fc);
  942. memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
  943. memcpy(hdr->addr2, vif->addr, ETH_ALEN);
  944. memset(hdr->addr3, 0xff, ETH_ALEN);
  945. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
  946. skb->data, skb->len, 0,
  947. wlvif->basic_rate);
  948. out:
  949. dev_kfree_skb(skb);
  950. return ret;
  951. }
  952. int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
  953. {
  954. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  955. struct ieee80211_qos_hdr template;
  956. memset(&template, 0, sizeof(template));
  957. memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
  958. memcpy(template.addr2, vif->addr, ETH_ALEN);
  959. memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
  960. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  961. IEEE80211_STYPE_QOS_NULLFUNC |
  962. IEEE80211_FCTL_TODS);
  963. /* FIXME: not sure what priority to use here */
  964. template.qos_ctrl = cpu_to_le16(0);
  965. return wl1271_cmd_template_set(wl, wlvif->role_id,
  966. CMD_TEMPL_QOS_NULL_DATA, &template,
  967. sizeof(template), 0,
  968. wlvif->basic_rate);
  969. }
  970. int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
  971. {
  972. struct wl1271_cmd_set_keys *cmd;
  973. int ret = 0;
  974. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  975. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  976. if (!cmd) {
  977. ret = -ENOMEM;
  978. goto out;
  979. }
  980. cmd->hlid = hlid;
  981. cmd->key_id = id;
  982. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  983. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  984. cmd->key_type = KEY_WEP;
  985. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  986. if (ret < 0) {
  987. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  988. goto out;
  989. }
  990. out:
  991. kfree(cmd);
  992. return ret;
  993. }
  994. int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  995. u16 action, u8 id, u8 key_type,
  996. u8 key_size, const u8 *key, const u8 *addr,
  997. u32 tx_seq_32, u16 tx_seq_16)
  998. {
  999. struct wl1271_cmd_set_keys *cmd;
  1000. int ret = 0;
  1001. /* hlid might have already been deleted */
  1002. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
  1003. return 0;
  1004. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1005. if (!cmd) {
  1006. ret = -ENOMEM;
  1007. goto out;
  1008. }
  1009. cmd->hlid = wlvif->sta.hlid;
  1010. if (key_type == KEY_WEP)
  1011. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1012. else if (is_broadcast_ether_addr(addr))
  1013. cmd->lid_key_type = BROADCAST_LID_TYPE;
  1014. else
  1015. cmd->lid_key_type = UNICAST_LID_TYPE;
  1016. cmd->key_action = cpu_to_le16(action);
  1017. cmd->key_size = key_size;
  1018. cmd->key_type = key_type;
  1019. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1020. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1021. cmd->key_id = id;
  1022. if (key_type == KEY_TKIP) {
  1023. /*
  1024. * We get the key in the following form:
  1025. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1026. * but the target is expecting:
  1027. * TKIP - RX MIC - TX MIC
  1028. */
  1029. memcpy(cmd->key, key, 16);
  1030. memcpy(cmd->key + 16, key + 24, 8);
  1031. memcpy(cmd->key + 24, key + 16, 8);
  1032. } else {
  1033. memcpy(cmd->key, key, key_size);
  1034. }
  1035. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  1036. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1037. if (ret < 0) {
  1038. wl1271_warning("could not set keys");
  1039. goto out;
  1040. }
  1041. out:
  1042. kfree(cmd);
  1043. return ret;
  1044. }
  1045. /*
  1046. * TODO: merge with sta/ibss into 1 set_key function.
  1047. * note there are slight diffs
  1048. */
  1049. int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1050. u16 action, u8 id, u8 key_type,
  1051. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1052. u16 tx_seq_16)
  1053. {
  1054. struct wl1271_cmd_set_keys *cmd;
  1055. int ret = 0;
  1056. u8 lid_type;
  1057. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1058. if (!cmd)
  1059. return -ENOMEM;
  1060. if (hlid == wlvif->ap.bcast_hlid) {
  1061. if (key_type == KEY_WEP)
  1062. lid_type = WEP_DEFAULT_LID_TYPE;
  1063. else
  1064. lid_type = BROADCAST_LID_TYPE;
  1065. } else {
  1066. lid_type = UNICAST_LID_TYPE;
  1067. }
  1068. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  1069. " hlid: %d", (int)action, (int)id, (int)lid_type,
  1070. (int)key_type, (int)hlid);
  1071. cmd->lid_key_type = lid_type;
  1072. cmd->hlid = hlid;
  1073. cmd->key_action = cpu_to_le16(action);
  1074. cmd->key_size = key_size;
  1075. cmd->key_type = key_type;
  1076. cmd->key_id = id;
  1077. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1078. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1079. if (key_type == KEY_TKIP) {
  1080. /*
  1081. * We get the key in the following form:
  1082. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1083. * but the target is expecting:
  1084. * TKIP - RX MIC - TX MIC
  1085. */
  1086. memcpy(cmd->key, key, 16);
  1087. memcpy(cmd->key + 16, key + 24, 8);
  1088. memcpy(cmd->key + 24, key + 16, 8);
  1089. } else {
  1090. memcpy(cmd->key, key, key_size);
  1091. }
  1092. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  1093. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1094. if (ret < 0) {
  1095. wl1271_warning("could not set ap keys");
  1096. goto out;
  1097. }
  1098. out:
  1099. kfree(cmd);
  1100. return ret;
  1101. }
  1102. int wl12xx_cmd_set_peer_state(struct wl1271 *wl, u8 hlid)
  1103. {
  1104. struct wl12xx_cmd_set_peer_state *cmd;
  1105. int ret = 0;
  1106. wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
  1107. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1108. if (!cmd) {
  1109. ret = -ENOMEM;
  1110. goto out;
  1111. }
  1112. cmd->hlid = hlid;
  1113. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  1114. ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
  1115. if (ret < 0) {
  1116. wl1271_error("failed to send set peer state command");
  1117. goto out_free;
  1118. }
  1119. out_free:
  1120. kfree(cmd);
  1121. out:
  1122. return ret;
  1123. }
  1124. int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1125. struct ieee80211_sta *sta, u8 hlid)
  1126. {
  1127. struct wl12xx_cmd_add_peer *cmd;
  1128. int i, ret;
  1129. u32 sta_rates;
  1130. wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
  1131. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1132. if (!cmd) {
  1133. ret = -ENOMEM;
  1134. goto out;
  1135. }
  1136. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  1137. cmd->bss_index = WL1271_AP_BSS_INDEX;
  1138. cmd->aid = sta->aid;
  1139. cmd->hlid = hlid;
  1140. cmd->sp_len = sta->max_sp;
  1141. cmd->wmm = sta->wme ? 1 : 0;
  1142. for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
  1143. if (sta->wme && (sta->uapsd_queues & BIT(i)))
  1144. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1145. WL1271_PSD_UPSD_TRIGGER;
  1146. else
  1147. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1148. WL1271_PSD_LEGACY;
  1149. sta_rates = sta->supp_rates[wlvif->band];
  1150. if (sta->ht_cap.ht_supported)
  1151. sta_rates |=
  1152. (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
  1153. (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
  1154. cmd->supported_rates =
  1155. cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
  1156. wlvif->band));
  1157. wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
  1158. cmd->supported_rates, sta->uapsd_queues);
  1159. ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
  1160. if (ret < 0) {
  1161. wl1271_error("failed to initiate cmd add peer");
  1162. goto out_free;
  1163. }
  1164. out_free:
  1165. kfree(cmd);
  1166. out:
  1167. return ret;
  1168. }
  1169. int wl12xx_cmd_remove_peer(struct wl1271 *wl, u8 hlid)
  1170. {
  1171. struct wl12xx_cmd_remove_peer *cmd;
  1172. int ret;
  1173. bool timeout = false;
  1174. wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
  1175. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1176. if (!cmd) {
  1177. ret = -ENOMEM;
  1178. goto out;
  1179. }
  1180. cmd->hlid = hlid;
  1181. /* We never send a deauth, mac80211 is in charge of this */
  1182. cmd->reason_opcode = 0;
  1183. cmd->send_deauth_flag = 0;
  1184. ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
  1185. if (ret < 0) {
  1186. wl1271_error("failed to initiate cmd remove peer");
  1187. goto out_free;
  1188. }
  1189. ret = wl->ops->wait_for_event(wl,
  1190. WLCORE_EVENT_PEER_REMOVE_COMPLETE,
  1191. &timeout);
  1192. /*
  1193. * We are ok with a timeout here. The event is sometimes not sent
  1194. * due to a firmware bug. In case of another error (like SDIO timeout)
  1195. * queue a recovery.
  1196. */
  1197. if (ret)
  1198. wl12xx_queue_recovery_work(wl);
  1199. out_free:
  1200. kfree(cmd);
  1201. out:
  1202. return ret;
  1203. }
  1204. int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
  1205. {
  1206. struct wl12xx_cmd_config_fwlog *cmd;
  1207. int ret = 0;
  1208. wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
  1209. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1210. if (!cmd) {
  1211. ret = -ENOMEM;
  1212. goto out;
  1213. }
  1214. cmd->logger_mode = wl->conf.fwlog.mode;
  1215. cmd->log_severity = wl->conf.fwlog.severity;
  1216. cmd->timestamp = wl->conf.fwlog.timestamp;
  1217. cmd->output = wl->conf.fwlog.output;
  1218. cmd->threshold = wl->conf.fwlog.threshold;
  1219. ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
  1220. if (ret < 0) {
  1221. wl1271_error("failed to send config firmware logger command");
  1222. goto out_free;
  1223. }
  1224. out_free:
  1225. kfree(cmd);
  1226. out:
  1227. return ret;
  1228. }
  1229. int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
  1230. {
  1231. struct wl12xx_cmd_start_fwlog *cmd;
  1232. int ret = 0;
  1233. wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
  1234. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1235. if (!cmd) {
  1236. ret = -ENOMEM;
  1237. goto out;
  1238. }
  1239. ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
  1240. if (ret < 0) {
  1241. wl1271_error("failed to send start firmware logger command");
  1242. goto out_free;
  1243. }
  1244. out_free:
  1245. kfree(cmd);
  1246. out:
  1247. return ret;
  1248. }
  1249. int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
  1250. {
  1251. struct wl12xx_cmd_stop_fwlog *cmd;
  1252. int ret = 0;
  1253. wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
  1254. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1255. if (!cmd) {
  1256. ret = -ENOMEM;
  1257. goto out;
  1258. }
  1259. ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
  1260. if (ret < 0) {
  1261. wl1271_error("failed to send stop firmware logger command");
  1262. goto out_free;
  1263. }
  1264. out_free:
  1265. kfree(cmd);
  1266. out:
  1267. return ret;
  1268. }
  1269. static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1270. u8 role_id, enum ieee80211_band band, u8 channel)
  1271. {
  1272. struct wl12xx_cmd_roc *cmd;
  1273. int ret = 0;
  1274. wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
  1275. if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
  1276. return -EINVAL;
  1277. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1278. if (!cmd) {
  1279. ret = -ENOMEM;
  1280. goto out;
  1281. }
  1282. cmd->role_id = role_id;
  1283. cmd->channel = channel;
  1284. switch (band) {
  1285. case IEEE80211_BAND_2GHZ:
  1286. cmd->band = WLCORE_BAND_2_4GHZ;
  1287. break;
  1288. case IEEE80211_BAND_5GHZ:
  1289. cmd->band = WLCORE_BAND_5GHZ;
  1290. break;
  1291. default:
  1292. wl1271_error("roc - unknown band: %d", (int)wlvif->band);
  1293. ret = -EINVAL;
  1294. goto out_free;
  1295. }
  1296. ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
  1297. if (ret < 0) {
  1298. wl1271_error("failed to send ROC command");
  1299. goto out_free;
  1300. }
  1301. out_free:
  1302. kfree(cmd);
  1303. out:
  1304. return ret;
  1305. }
  1306. static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
  1307. {
  1308. struct wl12xx_cmd_croc *cmd;
  1309. int ret = 0;
  1310. wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
  1311. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1312. if (!cmd) {
  1313. ret = -ENOMEM;
  1314. goto out;
  1315. }
  1316. cmd->role_id = role_id;
  1317. ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
  1318. sizeof(*cmd), 0);
  1319. if (ret < 0) {
  1320. wl1271_error("failed to send ROC command");
  1321. goto out_free;
  1322. }
  1323. out_free:
  1324. kfree(cmd);
  1325. out:
  1326. return ret;
  1327. }
  1328. int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
  1329. enum ieee80211_band band, u8 channel)
  1330. {
  1331. int ret = 0;
  1332. if (WARN_ON(test_bit(role_id, wl->roc_map)))
  1333. return 0;
  1334. ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
  1335. if (ret < 0)
  1336. goto out;
  1337. __set_bit(role_id, wl->roc_map);
  1338. out:
  1339. return ret;
  1340. }
  1341. int wl12xx_croc(struct wl1271 *wl, u8 role_id)
  1342. {
  1343. int ret = 0;
  1344. if (WARN_ON(!test_bit(role_id, wl->roc_map)))
  1345. return 0;
  1346. ret = wl12xx_cmd_croc(wl, role_id);
  1347. if (ret < 0)
  1348. goto out;
  1349. __clear_bit(role_id, wl->roc_map);
  1350. /*
  1351. * Rearm the tx watchdog when removing the last ROC. This prevents
  1352. * recoveries due to just finished ROCs - when Tx hasn't yet had
  1353. * a chance to get out.
  1354. */
  1355. if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
  1356. wl12xx_rearm_tx_watchdog_locked(wl);
  1357. out:
  1358. return ret;
  1359. }
  1360. int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1361. {
  1362. struct wl12xx_cmd_stop_channel_switch *cmd;
  1363. int ret;
  1364. wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
  1365. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1366. if (!cmd) {
  1367. ret = -ENOMEM;
  1368. goto out;
  1369. }
  1370. cmd->role_id = wlvif->role_id;
  1371. ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
  1372. if (ret < 0) {
  1373. wl1271_error("failed to stop channel switch command");
  1374. goto out_free;
  1375. }
  1376. out_free:
  1377. kfree(cmd);
  1378. out:
  1379. return ret;
  1380. }
  1381. /* start dev role and roc on its channel */
  1382. int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1383. enum ieee80211_band band, int channel)
  1384. {
  1385. int ret;
  1386. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1387. wlvif->bss_type == BSS_TYPE_IBSS)))
  1388. return -EINVAL;
  1389. ret = wl12xx_cmd_role_enable(wl,
  1390. wl12xx_wlvif_to_vif(wlvif)->addr,
  1391. WL1271_ROLE_DEVICE,
  1392. &wlvif->dev_role_id);
  1393. if (ret < 0)
  1394. goto out;
  1395. ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
  1396. if (ret < 0)
  1397. goto out_disable;
  1398. ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
  1399. if (ret < 0)
  1400. goto out_stop;
  1401. return 0;
  1402. out_stop:
  1403. wl12xx_cmd_role_stop_dev(wl, wlvif);
  1404. out_disable:
  1405. wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1406. out:
  1407. return ret;
  1408. }
  1409. /* croc dev hlid, and stop the role */
  1410. int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1411. {
  1412. int ret;
  1413. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1414. wlvif->bss_type == BSS_TYPE_IBSS)))
  1415. return -EINVAL;
  1416. /* flush all pending packets */
  1417. ret = wlcore_tx_work_locked(wl);
  1418. if (ret < 0)
  1419. goto out;
  1420. if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
  1421. ret = wl12xx_croc(wl, wlvif->dev_role_id);
  1422. if (ret < 0)
  1423. goto out;
  1424. }
  1425. ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
  1426. if (ret < 0)
  1427. goto out;
  1428. ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1429. if (ret < 0)
  1430. goto out;
  1431. out:
  1432. return ret;
  1433. }