cmd.c 46 KB

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