cmd.c 48 KB

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