cmd.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  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 nl80211_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 == NL80211_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 == NL80211_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. /* If MESH --> ssid_len is always 0 */
  511. if (!ieee80211_vif_is_mesh(vif)) {
  512. /* trying to use hidden SSID with an old hostapd version */
  513. if (wlvif->ssid_len == 0 && !bss_conf->hidden_ssid) {
  514. wl1271_error("got a null SSID from beacon/bss");
  515. ret = -EINVAL;
  516. goto out;
  517. }
  518. }
  519. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  520. if (!cmd) {
  521. ret = -ENOMEM;
  522. goto out;
  523. }
  524. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.global_hlid);
  525. if (ret < 0)
  526. goto out_free;
  527. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  528. if (ret < 0)
  529. goto out_free_global;
  530. /* use the previous security seq, if this is a recovery/resume */
  531. wl->links[wlvif->ap.bcast_hlid].total_freed_pkts =
  532. wlvif->total_freed_pkts;
  533. cmd->role_id = wlvif->role_id;
  534. cmd->ap.aging_period = cpu_to_le16(wl->conf.tx.ap_aging_period);
  535. cmd->ap.bss_index = WL1271_AP_BSS_INDEX;
  536. cmd->ap.global_hlid = wlvif->ap.global_hlid;
  537. cmd->ap.broadcast_hlid = wlvif->ap.bcast_hlid;
  538. cmd->ap.global_session_id = wl->session_ids[wlvif->ap.global_hlid];
  539. cmd->ap.bcast_session_id = wl->session_ids[wlvif->ap.bcast_hlid];
  540. cmd->ap.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  541. cmd->ap.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  542. cmd->ap.dtim_interval = bss_conf->dtim_period;
  543. cmd->ap.beacon_expiry = WL1271_AP_DEF_BEACON_EXP;
  544. /* FIXME: Change when adding DFS */
  545. cmd->ap.reset_tsf = 1; /* By default reset AP TSF */
  546. cmd->ap.wmm = wlvif->wmm_enabled;
  547. cmd->channel = wlvif->channel;
  548. cmd->channel_type = wlcore_get_native_channel_type(wlvif->channel_type);
  549. if (!bss_conf->hidden_ssid) {
  550. /* take the SSID from the beacon for backward compatibility */
  551. cmd->ap.ssid_type = WL12XX_SSID_TYPE_PUBLIC;
  552. cmd->ap.ssid_len = wlvif->ssid_len;
  553. memcpy(cmd->ap.ssid, wlvif->ssid, wlvif->ssid_len);
  554. } else {
  555. cmd->ap.ssid_type = WL12XX_SSID_TYPE_HIDDEN;
  556. cmd->ap.ssid_len = bss_conf->ssid_len;
  557. memcpy(cmd->ap.ssid, bss_conf->ssid, bss_conf->ssid_len);
  558. }
  559. supported_rates = CONF_TX_ENABLED_RATES | CONF_TX_MCS_RATES |
  560. wlcore_hw_ap_get_mimo_wide_rate_mask(wl, wlvif);
  561. if (wlvif->p2p)
  562. supported_rates &= ~CONF_TX_CCK_RATES;
  563. wl1271_debug(DEBUG_CMD, "cmd role start ap with supported_rates 0x%08x",
  564. supported_rates);
  565. cmd->ap.local_rates = cpu_to_le32(supported_rates);
  566. switch (wlvif->band) {
  567. case NL80211_BAND_2GHZ:
  568. cmd->band = WLCORE_BAND_2_4GHZ;
  569. break;
  570. case NL80211_BAND_5GHZ:
  571. cmd->band = WLCORE_BAND_5GHZ;
  572. break;
  573. default:
  574. wl1271_warning("ap start - unknown band: %d", (int)wlvif->band);
  575. cmd->band = WLCORE_BAND_2_4GHZ;
  576. break;
  577. }
  578. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  579. if (ret < 0) {
  580. wl1271_error("failed to initiate cmd role start ap");
  581. goto out_free_bcast;
  582. }
  583. goto out_free;
  584. out_free_bcast:
  585. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  586. out_free_global:
  587. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  588. out_free:
  589. kfree(cmd);
  590. out:
  591. return ret;
  592. }
  593. int wl12xx_cmd_role_stop_ap(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  594. {
  595. struct wl12xx_cmd_role_stop *cmd;
  596. int ret;
  597. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  598. if (!cmd) {
  599. ret = -ENOMEM;
  600. goto out;
  601. }
  602. wl1271_debug(DEBUG_CMD, "cmd role stop ap %d", wlvif->role_id);
  603. cmd->role_id = wlvif->role_id;
  604. ret = wl1271_cmd_send(wl, CMD_ROLE_STOP, cmd, sizeof(*cmd), 0);
  605. if (ret < 0) {
  606. wl1271_error("failed to initiate cmd role stop ap");
  607. goto out_free;
  608. }
  609. wl12xx_free_link(wl, wlvif, &wlvif->ap.bcast_hlid);
  610. wl12xx_free_link(wl, wlvif, &wlvif->ap.global_hlid);
  611. out_free:
  612. kfree(cmd);
  613. out:
  614. return ret;
  615. }
  616. int wl12xx_cmd_role_start_ibss(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  617. {
  618. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  619. struct wl12xx_cmd_role_start *cmd;
  620. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  621. int ret;
  622. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  623. if (!cmd) {
  624. ret = -ENOMEM;
  625. goto out;
  626. }
  627. wl1271_debug(DEBUG_CMD, "cmd role start ibss %d", wlvif->role_id);
  628. cmd->role_id = wlvif->role_id;
  629. if (wlvif->band == NL80211_BAND_5GHZ)
  630. cmd->band = WLCORE_BAND_5GHZ;
  631. cmd->channel = wlvif->channel;
  632. cmd->ibss.basic_rate_set = cpu_to_le32(wlvif->basic_rate_set);
  633. cmd->ibss.beacon_interval = cpu_to_le16(wlvif->beacon_int);
  634. cmd->ibss.dtim_interval = bss_conf->dtim_period;
  635. cmd->ibss.ssid_type = WL12XX_SSID_TYPE_ANY;
  636. cmd->ibss.ssid_len = wlvif->ssid_len;
  637. memcpy(cmd->ibss.ssid, wlvif->ssid, wlvif->ssid_len);
  638. memcpy(cmd->ibss.bssid, vif->bss_conf.bssid, ETH_ALEN);
  639. cmd->sta.local_rates = cpu_to_le32(wlvif->rate_set);
  640. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID) {
  641. ret = wl12xx_allocate_link(wl, wlvif, &wlvif->sta.hlid);
  642. if (ret)
  643. goto out_free;
  644. }
  645. cmd->ibss.hlid = wlvif->sta.hlid;
  646. cmd->ibss.remote_rates = cpu_to_le32(wlvif->rate_set);
  647. wl1271_debug(DEBUG_CMD, "role start: roleid=%d, hlid=%d, session=%d "
  648. "basic_rate_set: 0x%x, remote_rates: 0x%x",
  649. wlvif->role_id, cmd->sta.hlid, cmd->sta.session,
  650. wlvif->basic_rate_set, wlvif->rate_set);
  651. wl1271_debug(DEBUG_CMD, "vif->bss_conf.bssid = %pM",
  652. vif->bss_conf.bssid);
  653. ret = wl1271_cmd_send(wl, CMD_ROLE_START, cmd, sizeof(*cmd), 0);
  654. if (ret < 0) {
  655. wl1271_error("failed to initiate cmd role enable");
  656. goto err_hlid;
  657. }
  658. goto out_free;
  659. err_hlid:
  660. /* clear links on error. */
  661. wl12xx_free_link(wl, wlvif, &wlvif->sta.hlid);
  662. out_free:
  663. kfree(cmd);
  664. out:
  665. return ret;
  666. }
  667. /**
  668. * send test command to firmware
  669. *
  670. * @wl: wl struct
  671. * @buf: buffer containing the command, with all headers, must work with dma
  672. * @len: length of the buffer
  673. * @answer: is answer needed
  674. */
  675. int wl1271_cmd_test(struct wl1271 *wl, void *buf, size_t buf_len, u8 answer)
  676. {
  677. int ret;
  678. size_t res_len = 0;
  679. wl1271_debug(DEBUG_CMD, "cmd test");
  680. if (answer)
  681. res_len = buf_len;
  682. ret = wl1271_cmd_send(wl, CMD_TEST, buf, buf_len, res_len);
  683. if (ret < 0) {
  684. wl1271_warning("TEST command failed");
  685. return ret;
  686. }
  687. return ret;
  688. }
  689. EXPORT_SYMBOL_GPL(wl1271_cmd_test);
  690. /**
  691. * read acx from firmware
  692. *
  693. * @wl: wl struct
  694. * @id: acx id
  695. * @buf: buffer for the response, including all headers, must work with dma
  696. * @len: length of buf
  697. */
  698. int wl1271_cmd_interrogate(struct wl1271 *wl, u16 id, void *buf,
  699. size_t cmd_len, size_t res_len)
  700. {
  701. struct acx_header *acx = buf;
  702. int ret;
  703. wl1271_debug(DEBUG_CMD, "cmd interrogate");
  704. acx->id = cpu_to_le16(id);
  705. /* response payload length, does not include any headers */
  706. acx->len = cpu_to_le16(res_len - sizeof(*acx));
  707. ret = wl1271_cmd_send(wl, CMD_INTERROGATE, acx, cmd_len, res_len);
  708. if (ret < 0)
  709. wl1271_error("INTERROGATE command failed");
  710. return ret;
  711. }
  712. /**
  713. * write acx value to firmware
  714. *
  715. * @wl: wl struct
  716. * @id: acx id
  717. * @buf: buffer containing acx, including all headers, must work with dma
  718. * @len: length of buf
  719. * @valid_rets: bitmap of valid cmd status codes (i.e. return values).
  720. * return the cmd status on success.
  721. */
  722. int wlcore_cmd_configure_failsafe(struct wl1271 *wl, u16 id, void *buf,
  723. size_t len, unsigned long valid_rets)
  724. {
  725. struct acx_header *acx = buf;
  726. int ret;
  727. wl1271_debug(DEBUG_CMD, "cmd configure (%d)", id);
  728. if (WARN_ON_ONCE(len < sizeof(*acx)))
  729. return -EIO;
  730. acx->id = cpu_to_le16(id);
  731. /* payload length, does not include any headers */
  732. acx->len = cpu_to_le16(len - sizeof(*acx));
  733. ret = wlcore_cmd_send_failsafe(wl, CMD_CONFIGURE, acx, len, 0,
  734. valid_rets);
  735. if (ret < 0) {
  736. wl1271_warning("CONFIGURE command NOK");
  737. return ret;
  738. }
  739. return ret;
  740. }
  741. /*
  742. * wrapper for wlcore_cmd_configure that accepts only success status.
  743. * return 0 on success
  744. */
  745. int wl1271_cmd_configure(struct wl1271 *wl, u16 id, void *buf, size_t len)
  746. {
  747. int ret = wlcore_cmd_configure_failsafe(wl, id, buf, len, 0);
  748. if (ret < 0)
  749. return ret;
  750. return 0;
  751. }
  752. EXPORT_SYMBOL_GPL(wl1271_cmd_configure);
  753. int wl1271_cmd_data_path(struct wl1271 *wl, bool enable)
  754. {
  755. struct cmd_enabledisable_path *cmd;
  756. int ret;
  757. u16 cmd_rx, cmd_tx;
  758. wl1271_debug(DEBUG_CMD, "cmd data path");
  759. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  760. if (!cmd) {
  761. ret = -ENOMEM;
  762. goto out;
  763. }
  764. /* the channel here is only used for calibration, so hardcoded to 1 */
  765. cmd->channel = 1;
  766. if (enable) {
  767. cmd_rx = CMD_ENABLE_RX;
  768. cmd_tx = CMD_ENABLE_TX;
  769. } else {
  770. cmd_rx = CMD_DISABLE_RX;
  771. cmd_tx = CMD_DISABLE_TX;
  772. }
  773. ret = wl1271_cmd_send(wl, cmd_rx, cmd, sizeof(*cmd), 0);
  774. if (ret < 0) {
  775. wl1271_error("rx %s cmd for channel %d failed",
  776. enable ? "start" : "stop", cmd->channel);
  777. goto out;
  778. }
  779. wl1271_debug(DEBUG_BOOT, "rx %s cmd channel %d",
  780. enable ? "start" : "stop", cmd->channel);
  781. ret = wl1271_cmd_send(wl, cmd_tx, cmd, sizeof(*cmd), 0);
  782. if (ret < 0) {
  783. wl1271_error("tx %s cmd for channel %d failed",
  784. enable ? "start" : "stop", cmd->channel);
  785. goto out;
  786. }
  787. wl1271_debug(DEBUG_BOOT, "tx %s cmd channel %d",
  788. enable ? "start" : "stop", cmd->channel);
  789. out:
  790. kfree(cmd);
  791. return ret;
  792. }
  793. EXPORT_SYMBOL_GPL(wl1271_cmd_data_path);
  794. int wl1271_cmd_ps_mode(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  795. u8 ps_mode, u16 auto_ps_timeout)
  796. {
  797. struct wl1271_cmd_ps_params *ps_params = NULL;
  798. int ret = 0;
  799. wl1271_debug(DEBUG_CMD, "cmd set ps mode");
  800. ps_params = kzalloc(sizeof(*ps_params), GFP_KERNEL);
  801. if (!ps_params) {
  802. ret = -ENOMEM;
  803. goto out;
  804. }
  805. ps_params->role_id = wlvif->role_id;
  806. ps_params->ps_mode = ps_mode;
  807. ps_params->auto_ps_timeout = auto_ps_timeout;
  808. ret = wl1271_cmd_send(wl, CMD_SET_PS_MODE, ps_params,
  809. sizeof(*ps_params), 0);
  810. if (ret < 0) {
  811. wl1271_error("cmd set_ps_mode failed");
  812. goto out;
  813. }
  814. out:
  815. kfree(ps_params);
  816. return ret;
  817. }
  818. int wl1271_cmd_template_set(struct wl1271 *wl, u8 role_id,
  819. u16 template_id, void *buf, size_t buf_len,
  820. int index, u32 rates)
  821. {
  822. struct wl1271_cmd_template_set *cmd;
  823. int ret = 0;
  824. wl1271_debug(DEBUG_CMD, "cmd template_set %d (role %d)",
  825. template_id, role_id);
  826. WARN_ON(buf_len > WL1271_CMD_TEMPL_MAX_SIZE);
  827. buf_len = min_t(size_t, buf_len, WL1271_CMD_TEMPL_MAX_SIZE);
  828. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  829. if (!cmd) {
  830. ret = -ENOMEM;
  831. goto out;
  832. }
  833. /* during initialization wlvif is NULL */
  834. cmd->role_id = role_id;
  835. cmd->len = cpu_to_le16(buf_len);
  836. cmd->template_type = template_id;
  837. cmd->enabled_rates = cpu_to_le32(rates);
  838. cmd->short_retry_limit = wl->conf.tx.tmpl_short_retry_limit;
  839. cmd->long_retry_limit = wl->conf.tx.tmpl_long_retry_limit;
  840. cmd->index = index;
  841. if (buf)
  842. memcpy(cmd->template_data, buf, buf_len);
  843. ret = wl1271_cmd_send(wl, CMD_SET_TEMPLATE, cmd, sizeof(*cmd), 0);
  844. if (ret < 0) {
  845. wl1271_warning("cmd set_template failed: %d", ret);
  846. goto out_free;
  847. }
  848. out_free:
  849. kfree(cmd);
  850. out:
  851. return ret;
  852. }
  853. int wl12xx_cmd_build_null_data(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  854. {
  855. struct sk_buff *skb = NULL;
  856. int size;
  857. void *ptr;
  858. int ret = -ENOMEM;
  859. if (wlvif->bss_type == BSS_TYPE_IBSS) {
  860. size = sizeof(struct wl12xx_null_data_template);
  861. ptr = NULL;
  862. } else {
  863. skb = ieee80211_nullfunc_get(wl->hw,
  864. wl12xx_wlvif_to_vif(wlvif));
  865. if (!skb)
  866. goto out;
  867. size = skb->len;
  868. ptr = skb->data;
  869. }
  870. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  871. CMD_TEMPL_NULL_DATA, ptr, size, 0,
  872. wlvif->basic_rate);
  873. out:
  874. dev_kfree_skb(skb);
  875. if (ret)
  876. wl1271_warning("cmd buld null data failed %d", ret);
  877. return ret;
  878. }
  879. int wl12xx_cmd_build_klv_null_data(struct wl1271 *wl,
  880. struct wl12xx_vif *wlvif)
  881. {
  882. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  883. struct sk_buff *skb = NULL;
  884. int ret = -ENOMEM;
  885. skb = ieee80211_nullfunc_get(wl->hw, vif);
  886. if (!skb)
  887. goto out;
  888. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_KLV,
  889. skb->data, skb->len,
  890. wlvif->sta.klv_template_id,
  891. wlvif->basic_rate);
  892. out:
  893. dev_kfree_skb(skb);
  894. if (ret)
  895. wl1271_warning("cmd build klv null data failed %d", ret);
  896. return ret;
  897. }
  898. int wl1271_cmd_build_ps_poll(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  899. u16 aid)
  900. {
  901. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  902. struct sk_buff *skb;
  903. int ret = 0;
  904. skb = ieee80211_pspoll_get(wl->hw, vif);
  905. if (!skb)
  906. goto out;
  907. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  908. CMD_TEMPL_PS_POLL, skb->data,
  909. skb->len, 0, wlvif->basic_rate_set);
  910. out:
  911. dev_kfree_skb(skb);
  912. return ret;
  913. }
  914. int wl12xx_cmd_build_probe_req(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  915. u8 role_id, u8 band,
  916. const u8 *ssid, size_t ssid_len,
  917. const u8 *ie0, size_t ie0_len, const u8 *ie1,
  918. size_t ie1_len, bool sched_scan)
  919. {
  920. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  921. struct sk_buff *skb;
  922. int ret;
  923. u32 rate;
  924. u16 template_id_2_4 = wl->scan_templ_id_2_4;
  925. u16 template_id_5 = wl->scan_templ_id_5;
  926. wl1271_debug(DEBUG_SCAN, "build probe request band %d", band);
  927. skb = ieee80211_probereq_get(wl->hw, vif->addr, ssid, ssid_len,
  928. ie0_len + ie1_len);
  929. if (!skb) {
  930. ret = -ENOMEM;
  931. goto out;
  932. }
  933. if (ie0_len)
  934. memcpy(skb_put(skb, ie0_len), ie0, ie0_len);
  935. if (ie1_len)
  936. memcpy(skb_put(skb, ie1_len), ie1, ie1_len);
  937. if (sched_scan &&
  938. (wl->quirks & WLCORE_QUIRK_DUAL_PROBE_TMPL)) {
  939. template_id_2_4 = wl->sched_scan_templ_id_2_4;
  940. template_id_5 = wl->sched_scan_templ_id_5;
  941. }
  942. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[band]);
  943. if (band == NL80211_BAND_2GHZ)
  944. ret = wl1271_cmd_template_set(wl, role_id,
  945. template_id_2_4,
  946. skb->data, skb->len, 0, rate);
  947. else
  948. ret = wl1271_cmd_template_set(wl, role_id,
  949. template_id_5,
  950. skb->data, skb->len, 0, rate);
  951. out:
  952. dev_kfree_skb(skb);
  953. return ret;
  954. }
  955. EXPORT_SYMBOL_GPL(wl12xx_cmd_build_probe_req);
  956. struct sk_buff *wl1271_cmd_build_ap_probe_req(struct wl1271 *wl,
  957. struct wl12xx_vif *wlvif,
  958. struct sk_buff *skb)
  959. {
  960. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  961. int ret;
  962. u32 rate;
  963. if (!skb)
  964. skb = ieee80211_ap_probereq_get(wl->hw, vif);
  965. if (!skb)
  966. goto out;
  967. wl1271_debug(DEBUG_SCAN, "set ap probe request template");
  968. rate = wl1271_tx_min_rate_get(wl, wlvif->bitrate_masks[wlvif->band]);
  969. if (wlvif->band == NL80211_BAND_2GHZ)
  970. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  971. CMD_TEMPL_CFG_PROBE_REQ_2_4,
  972. skb->data, skb->len, 0, rate);
  973. else
  974. ret = wl1271_cmd_template_set(wl, wlvif->role_id,
  975. CMD_TEMPL_CFG_PROBE_REQ_5,
  976. skb->data, skb->len, 0, rate);
  977. if (ret < 0)
  978. wl1271_error("Unable to set ap probe request template.");
  979. out:
  980. return skb;
  981. }
  982. int wl1271_cmd_build_arp_rsp(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  983. {
  984. int ret, extra = 0;
  985. u16 fc;
  986. struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
  987. struct sk_buff *skb;
  988. struct wl12xx_arp_rsp_template *tmpl;
  989. struct ieee80211_hdr_3addr *hdr;
  990. struct arphdr *arp_hdr;
  991. skb = dev_alloc_skb(sizeof(*hdr) + sizeof(__le16) + sizeof(*tmpl) +
  992. WL1271_EXTRA_SPACE_MAX);
  993. if (!skb) {
  994. wl1271_error("failed to allocate buffer for arp rsp template");
  995. return -ENOMEM;
  996. }
  997. skb_reserve(skb, sizeof(*hdr) + WL1271_EXTRA_SPACE_MAX);
  998. tmpl = (struct wl12xx_arp_rsp_template *)skb_put(skb, sizeof(*tmpl));
  999. memset(tmpl, 0, sizeof(*tmpl));
  1000. /* llc layer */
  1001. memcpy(tmpl->llc_hdr, rfc1042_header, sizeof(rfc1042_header));
  1002. tmpl->llc_type = cpu_to_be16(ETH_P_ARP);
  1003. /* arp header */
  1004. arp_hdr = &tmpl->arp_hdr;
  1005. arp_hdr->ar_hrd = cpu_to_be16(ARPHRD_ETHER);
  1006. arp_hdr->ar_pro = cpu_to_be16(ETH_P_IP);
  1007. arp_hdr->ar_hln = ETH_ALEN;
  1008. arp_hdr->ar_pln = 4;
  1009. arp_hdr->ar_op = cpu_to_be16(ARPOP_REPLY);
  1010. /* arp payload */
  1011. memcpy(tmpl->sender_hw, vif->addr, ETH_ALEN);
  1012. tmpl->sender_ip = wlvif->ip_addr;
  1013. /* encryption space */
  1014. switch (wlvif->encryption_type) {
  1015. case KEY_TKIP:
  1016. if (wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE)
  1017. extra = WL1271_EXTRA_SPACE_TKIP;
  1018. break;
  1019. case KEY_AES:
  1020. extra = WL1271_EXTRA_SPACE_AES;
  1021. break;
  1022. case KEY_NONE:
  1023. case KEY_WEP:
  1024. case KEY_GEM:
  1025. extra = 0;
  1026. break;
  1027. default:
  1028. wl1271_warning("Unknown encryption type: %d",
  1029. wlvif->encryption_type);
  1030. ret = -EINVAL;
  1031. goto out;
  1032. }
  1033. if (extra) {
  1034. u8 *space = skb_push(skb, extra);
  1035. memset(space, 0, extra);
  1036. }
  1037. /* QoS header - BE */
  1038. if (wlvif->sta.qos)
  1039. memset(skb_push(skb, sizeof(__le16)), 0, sizeof(__le16));
  1040. /* mac80211 header */
  1041. hdr = (struct ieee80211_hdr_3addr *)skb_push(skb, sizeof(*hdr));
  1042. memset(hdr, 0, sizeof(*hdr));
  1043. fc = IEEE80211_FTYPE_DATA | IEEE80211_FCTL_TODS;
  1044. if (wlvif->sta.qos)
  1045. fc |= IEEE80211_STYPE_QOS_DATA;
  1046. else
  1047. fc |= IEEE80211_STYPE_DATA;
  1048. if (wlvif->encryption_type != KEY_NONE)
  1049. fc |= IEEE80211_FCTL_PROTECTED;
  1050. hdr->frame_control = cpu_to_le16(fc);
  1051. memcpy(hdr->addr1, vif->bss_conf.bssid, ETH_ALEN);
  1052. memcpy(hdr->addr2, vif->addr, ETH_ALEN);
  1053. eth_broadcast_addr(hdr->addr3);
  1054. ret = wl1271_cmd_template_set(wl, wlvif->role_id, CMD_TEMPL_ARP_RSP,
  1055. skb->data, skb->len, 0,
  1056. wlvif->basic_rate);
  1057. out:
  1058. dev_kfree_skb(skb);
  1059. return ret;
  1060. }
  1061. int wl1271_build_qos_null_data(struct wl1271 *wl, struct ieee80211_vif *vif)
  1062. {
  1063. struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
  1064. struct ieee80211_qos_hdr template;
  1065. memset(&template, 0, sizeof(template));
  1066. memcpy(template.addr1, vif->bss_conf.bssid, ETH_ALEN);
  1067. memcpy(template.addr2, vif->addr, ETH_ALEN);
  1068. memcpy(template.addr3, vif->bss_conf.bssid, ETH_ALEN);
  1069. template.frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1070. IEEE80211_STYPE_QOS_NULLFUNC |
  1071. IEEE80211_FCTL_TODS);
  1072. /* FIXME: not sure what priority to use here */
  1073. template.qos_ctrl = cpu_to_le16(0);
  1074. return wl1271_cmd_template_set(wl, wlvif->role_id,
  1075. CMD_TEMPL_QOS_NULL_DATA, &template,
  1076. sizeof(template), 0,
  1077. wlvif->basic_rate);
  1078. }
  1079. int wl12xx_cmd_set_default_wep_key(struct wl1271 *wl, u8 id, u8 hlid)
  1080. {
  1081. struct wl1271_cmd_set_keys *cmd;
  1082. int ret = 0;
  1083. wl1271_debug(DEBUG_CMD, "cmd set_default_wep_key %d", id);
  1084. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1085. if (!cmd) {
  1086. ret = -ENOMEM;
  1087. goto out;
  1088. }
  1089. cmd->hlid = hlid;
  1090. cmd->key_id = id;
  1091. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1092. cmd->key_action = cpu_to_le16(KEY_SET_ID);
  1093. cmd->key_type = KEY_WEP;
  1094. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1095. if (ret < 0) {
  1096. wl1271_warning("cmd set_default_wep_key failed: %d", ret);
  1097. goto out;
  1098. }
  1099. out:
  1100. kfree(cmd);
  1101. return ret;
  1102. }
  1103. int wl1271_cmd_set_sta_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1104. u16 action, u8 id, u8 key_type,
  1105. u8 key_size, const u8 *key, const u8 *addr,
  1106. u32 tx_seq_32, u16 tx_seq_16)
  1107. {
  1108. struct wl1271_cmd_set_keys *cmd;
  1109. int ret = 0;
  1110. /* hlid might have already been deleted */
  1111. if (wlvif->sta.hlid == WL12XX_INVALID_LINK_ID)
  1112. return 0;
  1113. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1114. if (!cmd) {
  1115. ret = -ENOMEM;
  1116. goto out;
  1117. }
  1118. cmd->hlid = wlvif->sta.hlid;
  1119. if (key_type == KEY_WEP)
  1120. cmd->lid_key_type = WEP_DEFAULT_LID_TYPE;
  1121. else if (is_broadcast_ether_addr(addr))
  1122. cmd->lid_key_type = BROADCAST_LID_TYPE;
  1123. else
  1124. cmd->lid_key_type = UNICAST_LID_TYPE;
  1125. cmd->key_action = cpu_to_le16(action);
  1126. cmd->key_size = key_size;
  1127. cmd->key_type = key_type;
  1128. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1129. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1130. cmd->key_id = id;
  1131. if (key_type == KEY_TKIP) {
  1132. /*
  1133. * We get the key in the following form:
  1134. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1135. * but the target is expecting:
  1136. * TKIP - RX MIC - TX MIC
  1137. */
  1138. memcpy(cmd->key, key, 16);
  1139. memcpy(cmd->key + 16, key + 24, 8);
  1140. memcpy(cmd->key + 24, key + 16, 8);
  1141. } else {
  1142. memcpy(cmd->key, key, key_size);
  1143. }
  1144. wl1271_dump(DEBUG_CRYPT, "TARGET KEY: ", cmd, sizeof(*cmd));
  1145. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1146. if (ret < 0) {
  1147. wl1271_warning("could not set keys");
  1148. goto out;
  1149. }
  1150. out:
  1151. kfree(cmd);
  1152. return ret;
  1153. }
  1154. /*
  1155. * TODO: merge with sta/ibss into 1 set_key function.
  1156. * note there are slight diffs
  1157. */
  1158. int wl1271_cmd_set_ap_key(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1159. u16 action, u8 id, u8 key_type,
  1160. u8 key_size, const u8 *key, u8 hlid, u32 tx_seq_32,
  1161. u16 tx_seq_16)
  1162. {
  1163. struct wl1271_cmd_set_keys *cmd;
  1164. int ret = 0;
  1165. u8 lid_type;
  1166. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1167. if (!cmd)
  1168. return -ENOMEM;
  1169. if (hlid == wlvif->ap.bcast_hlid) {
  1170. if (key_type == KEY_WEP)
  1171. lid_type = WEP_DEFAULT_LID_TYPE;
  1172. else
  1173. lid_type = BROADCAST_LID_TYPE;
  1174. } else {
  1175. lid_type = UNICAST_LID_TYPE;
  1176. }
  1177. wl1271_debug(DEBUG_CRYPT, "ap key action: %d id: %d lid: %d type: %d"
  1178. " hlid: %d", (int)action, (int)id, (int)lid_type,
  1179. (int)key_type, (int)hlid);
  1180. cmd->lid_key_type = lid_type;
  1181. cmd->hlid = hlid;
  1182. cmd->key_action = cpu_to_le16(action);
  1183. cmd->key_size = key_size;
  1184. cmd->key_type = key_type;
  1185. cmd->key_id = id;
  1186. cmd->ac_seq_num16[0] = cpu_to_le16(tx_seq_16);
  1187. cmd->ac_seq_num32[0] = cpu_to_le32(tx_seq_32);
  1188. if (key_type == KEY_TKIP) {
  1189. /*
  1190. * We get the key in the following form:
  1191. * TKIP (16 bytes) - TX MIC (8 bytes) - RX MIC (8 bytes)
  1192. * but the target is expecting:
  1193. * TKIP - RX MIC - TX MIC
  1194. */
  1195. memcpy(cmd->key, key, 16);
  1196. memcpy(cmd->key + 16, key + 24, 8);
  1197. memcpy(cmd->key + 24, key + 16, 8);
  1198. } else {
  1199. memcpy(cmd->key, key, key_size);
  1200. }
  1201. wl1271_dump(DEBUG_CRYPT, "TARGET AP KEY: ", cmd, sizeof(*cmd));
  1202. ret = wl1271_cmd_send(wl, CMD_SET_KEYS, cmd, sizeof(*cmd), 0);
  1203. if (ret < 0) {
  1204. wl1271_warning("could not set ap keys");
  1205. goto out;
  1206. }
  1207. out:
  1208. kfree(cmd);
  1209. return ret;
  1210. }
  1211. int wl12xx_cmd_set_peer_state(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1212. u8 hlid)
  1213. {
  1214. struct wl12xx_cmd_set_peer_state *cmd;
  1215. int ret = 0;
  1216. wl1271_debug(DEBUG_CMD, "cmd set peer state (hlid=%d)", hlid);
  1217. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1218. if (!cmd) {
  1219. ret = -ENOMEM;
  1220. goto out;
  1221. }
  1222. cmd->hlid = hlid;
  1223. cmd->state = WL1271_CMD_STA_STATE_CONNECTED;
  1224. /* wmm param is valid only for station role */
  1225. if (wlvif->bss_type == BSS_TYPE_STA_BSS)
  1226. cmd->wmm = wlvif->wmm_enabled;
  1227. ret = wl1271_cmd_send(wl, CMD_SET_PEER_STATE, cmd, sizeof(*cmd), 0);
  1228. if (ret < 0) {
  1229. wl1271_error("failed to send set peer state command");
  1230. goto out_free;
  1231. }
  1232. out_free:
  1233. kfree(cmd);
  1234. out:
  1235. return ret;
  1236. }
  1237. int wl12xx_cmd_add_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1238. struct ieee80211_sta *sta, u8 hlid)
  1239. {
  1240. struct wl12xx_cmd_add_peer *cmd;
  1241. int i, ret;
  1242. u32 sta_rates;
  1243. wl1271_debug(DEBUG_CMD, "cmd add peer %d", (int)hlid);
  1244. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1245. if (!cmd) {
  1246. ret = -ENOMEM;
  1247. goto out;
  1248. }
  1249. memcpy(cmd->addr, sta->addr, ETH_ALEN);
  1250. cmd->bss_index = WL1271_AP_BSS_INDEX;
  1251. cmd->aid = sta->aid;
  1252. cmd->hlid = hlid;
  1253. cmd->sp_len = sta->max_sp;
  1254. cmd->wmm = sta->wme ? 1 : 0;
  1255. cmd->session_id = wl->session_ids[hlid];
  1256. cmd->role_id = wlvif->role_id;
  1257. for (i = 0; i < NUM_ACCESS_CATEGORIES_COPY; i++)
  1258. if (sta->wme && (sta->uapsd_queues & BIT(i)))
  1259. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1260. WL1271_PSD_UPSD_TRIGGER;
  1261. else
  1262. cmd->psd_type[NUM_ACCESS_CATEGORIES_COPY-1-i] =
  1263. WL1271_PSD_LEGACY;
  1264. sta_rates = sta->supp_rates[wlvif->band];
  1265. if (sta->ht_cap.ht_supported)
  1266. sta_rates |=
  1267. (sta->ht_cap.mcs.rx_mask[0] << HW_HT_RATES_OFFSET) |
  1268. (sta->ht_cap.mcs.rx_mask[1] << HW_MIMO_RATES_OFFSET);
  1269. cmd->supported_rates =
  1270. cpu_to_le32(wl1271_tx_enabled_rates_get(wl, sta_rates,
  1271. wlvif->band));
  1272. if (!cmd->supported_rates) {
  1273. wl1271_debug(DEBUG_CMD,
  1274. "peer has no supported rates yet, configuring basic rates: 0x%x",
  1275. wlvif->basic_rate_set);
  1276. cmd->supported_rates = cpu_to_le32(wlvif->basic_rate_set);
  1277. }
  1278. wl1271_debug(DEBUG_CMD, "new peer rates=0x%x queues=0x%x",
  1279. cmd->supported_rates, sta->uapsd_queues);
  1280. ret = wl1271_cmd_send(wl, CMD_ADD_PEER, cmd, sizeof(*cmd), 0);
  1281. if (ret < 0) {
  1282. wl1271_error("failed to initiate cmd add peer");
  1283. goto out_free;
  1284. }
  1285. out_free:
  1286. kfree(cmd);
  1287. out:
  1288. return ret;
  1289. }
  1290. int wl12xx_cmd_remove_peer(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1291. u8 hlid)
  1292. {
  1293. struct wl12xx_cmd_remove_peer *cmd;
  1294. int ret;
  1295. bool timeout = false;
  1296. wl1271_debug(DEBUG_CMD, "cmd remove peer %d", (int)hlid);
  1297. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1298. if (!cmd) {
  1299. ret = -ENOMEM;
  1300. goto out;
  1301. }
  1302. cmd->hlid = hlid;
  1303. /* We never send a deauth, mac80211 is in charge of this */
  1304. cmd->reason_opcode = 0;
  1305. cmd->send_deauth_flag = 0;
  1306. cmd->role_id = wlvif->role_id;
  1307. ret = wl1271_cmd_send(wl, CMD_REMOVE_PEER, cmd, sizeof(*cmd), 0);
  1308. if (ret < 0) {
  1309. wl1271_error("failed to initiate cmd remove peer");
  1310. goto out_free;
  1311. }
  1312. ret = wl->ops->wait_for_event(wl,
  1313. WLCORE_EVENT_PEER_REMOVE_COMPLETE,
  1314. &timeout);
  1315. /*
  1316. * We are ok with a timeout here. The event is sometimes not sent
  1317. * due to a firmware bug. In case of another error (like SDIO timeout)
  1318. * queue a recovery.
  1319. */
  1320. if (ret)
  1321. wl12xx_queue_recovery_work(wl);
  1322. out_free:
  1323. kfree(cmd);
  1324. out:
  1325. return ret;
  1326. }
  1327. static int wlcore_get_reg_conf_ch_idx(enum nl80211_band band, u16 ch)
  1328. {
  1329. /*
  1330. * map the given band/channel to the respective predefined
  1331. * bit expected by the fw
  1332. */
  1333. switch (band) {
  1334. case NL80211_BAND_2GHZ:
  1335. /* channels 1..14 are mapped to 0..13 */
  1336. if (ch >= 1 && ch <= 14)
  1337. return ch - 1;
  1338. break;
  1339. case NL80211_BAND_5GHZ:
  1340. switch (ch) {
  1341. case 8 ... 16:
  1342. /* channels 8,12,16 are mapped to 18,19,20 */
  1343. return 18 + (ch-8)/4;
  1344. case 34 ... 48:
  1345. /* channels 34,36..48 are mapped to 21..28 */
  1346. return 21 + (ch-34)/2;
  1347. case 52 ... 64:
  1348. /* channels 52,56..64 are mapped to 29..32 */
  1349. return 29 + (ch-52)/4;
  1350. case 100 ... 140:
  1351. /* channels 100,104..140 are mapped to 33..43 */
  1352. return 33 + (ch-100)/4;
  1353. case 149 ... 165:
  1354. /* channels 149,153..165 are mapped to 44..48 */
  1355. return 44 + (ch-149)/4;
  1356. default:
  1357. break;
  1358. }
  1359. break;
  1360. default:
  1361. break;
  1362. }
  1363. wl1271_error("%s: unknown band/channel: %d/%d", __func__, band, ch);
  1364. return -1;
  1365. }
  1366. void wlcore_set_pending_regdomain_ch(struct wl1271 *wl, u16 channel,
  1367. enum nl80211_band band)
  1368. {
  1369. int ch_bit_idx = 0;
  1370. if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
  1371. return;
  1372. ch_bit_idx = wlcore_get_reg_conf_ch_idx(band, channel);
  1373. if (ch_bit_idx >= 0 && ch_bit_idx <= WL1271_MAX_CHANNELS)
  1374. set_bit(ch_bit_idx, (long *)wl->reg_ch_conf_pending);
  1375. }
  1376. int wlcore_cmd_regdomain_config_locked(struct wl1271 *wl)
  1377. {
  1378. struct wl12xx_cmd_regdomain_dfs_config *cmd = NULL;
  1379. int ret = 0, i, b, ch_bit_idx;
  1380. u32 tmp_ch_bitmap[2];
  1381. struct wiphy *wiphy = wl->hw->wiphy;
  1382. struct ieee80211_supported_band *band;
  1383. bool timeout = false;
  1384. if (!(wl->quirks & WLCORE_QUIRK_REGDOMAIN_CONF))
  1385. return 0;
  1386. wl1271_debug(DEBUG_CMD, "cmd reg domain config");
  1387. memset(tmp_ch_bitmap, 0, sizeof(tmp_ch_bitmap));
  1388. for (b = NL80211_BAND_2GHZ; b <= NL80211_BAND_5GHZ; b++) {
  1389. band = wiphy->bands[b];
  1390. for (i = 0; i < band->n_channels; i++) {
  1391. struct ieee80211_channel *channel = &band->channels[i];
  1392. u16 ch = channel->hw_value;
  1393. u32 flags = channel->flags;
  1394. if (flags & (IEEE80211_CHAN_DISABLED |
  1395. IEEE80211_CHAN_NO_IR))
  1396. continue;
  1397. if ((flags & IEEE80211_CHAN_RADAR) &&
  1398. channel->dfs_state != NL80211_DFS_AVAILABLE)
  1399. continue;
  1400. ch_bit_idx = wlcore_get_reg_conf_ch_idx(b, ch);
  1401. if (ch_bit_idx < 0)
  1402. continue;
  1403. set_bit(ch_bit_idx, (long *)tmp_ch_bitmap);
  1404. }
  1405. }
  1406. tmp_ch_bitmap[0] |= wl->reg_ch_conf_pending[0];
  1407. tmp_ch_bitmap[1] |= wl->reg_ch_conf_pending[1];
  1408. if (!memcmp(tmp_ch_bitmap, wl->reg_ch_conf_last, sizeof(tmp_ch_bitmap)))
  1409. goto out;
  1410. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1411. if (!cmd) {
  1412. ret = -ENOMEM;
  1413. goto out;
  1414. }
  1415. cmd->ch_bit_map1 = cpu_to_le32(tmp_ch_bitmap[0]);
  1416. cmd->ch_bit_map2 = cpu_to_le32(tmp_ch_bitmap[1]);
  1417. cmd->dfs_region = wl->dfs_region;
  1418. wl1271_debug(DEBUG_CMD,
  1419. "cmd reg domain bitmap1: 0x%08x, bitmap2: 0x%08x",
  1420. cmd->ch_bit_map1, cmd->ch_bit_map2);
  1421. ret = wl1271_cmd_send(wl, CMD_DFS_CHANNEL_CONFIG, cmd, sizeof(*cmd), 0);
  1422. if (ret < 0) {
  1423. wl1271_error("failed to send reg domain dfs config");
  1424. goto out;
  1425. }
  1426. ret = wl->ops->wait_for_event(wl,
  1427. WLCORE_EVENT_DFS_CONFIG_COMPLETE,
  1428. &timeout);
  1429. if (ret < 0 || timeout) {
  1430. wl1271_error("reg domain conf %serror",
  1431. timeout ? "completion " : "");
  1432. ret = timeout ? -ETIMEDOUT : ret;
  1433. goto out;
  1434. }
  1435. memcpy(wl->reg_ch_conf_last, tmp_ch_bitmap, sizeof(tmp_ch_bitmap));
  1436. memset(wl->reg_ch_conf_pending, 0, sizeof(wl->reg_ch_conf_pending));
  1437. out:
  1438. kfree(cmd);
  1439. return ret;
  1440. }
  1441. int wl12xx_cmd_config_fwlog(struct wl1271 *wl)
  1442. {
  1443. struct wl12xx_cmd_config_fwlog *cmd;
  1444. int ret = 0;
  1445. wl1271_debug(DEBUG_CMD, "cmd config firmware logger");
  1446. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1447. if (!cmd) {
  1448. ret = -ENOMEM;
  1449. goto out;
  1450. }
  1451. cmd->logger_mode = wl->conf.fwlog.mode;
  1452. cmd->log_severity = wl->conf.fwlog.severity;
  1453. cmd->timestamp = wl->conf.fwlog.timestamp;
  1454. cmd->output = wl->conf.fwlog.output;
  1455. cmd->threshold = wl->conf.fwlog.threshold;
  1456. ret = wl1271_cmd_send(wl, CMD_CONFIG_FWLOGGER, cmd, sizeof(*cmd), 0);
  1457. if (ret < 0) {
  1458. wl1271_error("failed to send config firmware logger command");
  1459. goto out_free;
  1460. }
  1461. out_free:
  1462. kfree(cmd);
  1463. out:
  1464. return ret;
  1465. }
  1466. int wl12xx_cmd_start_fwlog(struct wl1271 *wl)
  1467. {
  1468. struct wl12xx_cmd_start_fwlog *cmd;
  1469. int ret = 0;
  1470. wl1271_debug(DEBUG_CMD, "cmd start firmware logger");
  1471. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1472. if (!cmd) {
  1473. ret = -ENOMEM;
  1474. goto out;
  1475. }
  1476. ret = wl1271_cmd_send(wl, CMD_START_FWLOGGER, cmd, sizeof(*cmd), 0);
  1477. if (ret < 0) {
  1478. wl1271_error("failed to send start firmware logger command");
  1479. goto out_free;
  1480. }
  1481. out_free:
  1482. kfree(cmd);
  1483. out:
  1484. return ret;
  1485. }
  1486. int wl12xx_cmd_stop_fwlog(struct wl1271 *wl)
  1487. {
  1488. struct wl12xx_cmd_stop_fwlog *cmd;
  1489. int ret = 0;
  1490. wl1271_debug(DEBUG_CMD, "cmd stop firmware logger");
  1491. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1492. if (!cmd) {
  1493. ret = -ENOMEM;
  1494. goto out;
  1495. }
  1496. ret = wl1271_cmd_send(wl, CMD_STOP_FWLOGGER, cmd, sizeof(*cmd), 0);
  1497. if (ret < 0) {
  1498. wl1271_error("failed to send stop firmware logger command");
  1499. goto out_free;
  1500. }
  1501. out_free:
  1502. kfree(cmd);
  1503. out:
  1504. return ret;
  1505. }
  1506. static int wl12xx_cmd_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1507. u8 role_id, enum nl80211_band band, u8 channel)
  1508. {
  1509. struct wl12xx_cmd_roc *cmd;
  1510. int ret = 0;
  1511. wl1271_debug(DEBUG_CMD, "cmd roc %d (%d)", channel, role_id);
  1512. if (WARN_ON(role_id == WL12XX_INVALID_ROLE_ID))
  1513. return -EINVAL;
  1514. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1515. if (!cmd) {
  1516. ret = -ENOMEM;
  1517. goto out;
  1518. }
  1519. cmd->role_id = role_id;
  1520. cmd->channel = channel;
  1521. switch (band) {
  1522. case NL80211_BAND_2GHZ:
  1523. cmd->band = WLCORE_BAND_2_4GHZ;
  1524. break;
  1525. case NL80211_BAND_5GHZ:
  1526. cmd->band = WLCORE_BAND_5GHZ;
  1527. break;
  1528. default:
  1529. wl1271_error("roc - unknown band: %d", (int)wlvif->band);
  1530. ret = -EINVAL;
  1531. goto out_free;
  1532. }
  1533. ret = wl1271_cmd_send(wl, CMD_REMAIN_ON_CHANNEL, cmd, sizeof(*cmd), 0);
  1534. if (ret < 0) {
  1535. wl1271_error("failed to send ROC command");
  1536. goto out_free;
  1537. }
  1538. out_free:
  1539. kfree(cmd);
  1540. out:
  1541. return ret;
  1542. }
  1543. static int wl12xx_cmd_croc(struct wl1271 *wl, u8 role_id)
  1544. {
  1545. struct wl12xx_cmd_croc *cmd;
  1546. int ret = 0;
  1547. wl1271_debug(DEBUG_CMD, "cmd croc (%d)", role_id);
  1548. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1549. if (!cmd) {
  1550. ret = -ENOMEM;
  1551. goto out;
  1552. }
  1553. cmd->role_id = role_id;
  1554. ret = wl1271_cmd_send(wl, CMD_CANCEL_REMAIN_ON_CHANNEL, cmd,
  1555. sizeof(*cmd), 0);
  1556. if (ret < 0) {
  1557. wl1271_error("failed to send ROC command");
  1558. goto out_free;
  1559. }
  1560. out_free:
  1561. kfree(cmd);
  1562. out:
  1563. return ret;
  1564. }
  1565. int wl12xx_roc(struct wl1271 *wl, struct wl12xx_vif *wlvif, u8 role_id,
  1566. enum nl80211_band band, u8 channel)
  1567. {
  1568. int ret = 0;
  1569. if (WARN_ON(test_bit(role_id, wl->roc_map)))
  1570. return 0;
  1571. ret = wl12xx_cmd_roc(wl, wlvif, role_id, band, channel);
  1572. if (ret < 0)
  1573. goto out;
  1574. __set_bit(role_id, wl->roc_map);
  1575. out:
  1576. return ret;
  1577. }
  1578. int wl12xx_croc(struct wl1271 *wl, u8 role_id)
  1579. {
  1580. int ret = 0;
  1581. if (WARN_ON(!test_bit(role_id, wl->roc_map)))
  1582. return 0;
  1583. ret = wl12xx_cmd_croc(wl, role_id);
  1584. if (ret < 0)
  1585. goto out;
  1586. __clear_bit(role_id, wl->roc_map);
  1587. /*
  1588. * Rearm the tx watchdog when removing the last ROC. This prevents
  1589. * recoveries due to just finished ROCs - when Tx hasn't yet had
  1590. * a chance to get out.
  1591. */
  1592. if (find_first_bit(wl->roc_map, WL12XX_MAX_ROLES) >= WL12XX_MAX_ROLES)
  1593. wl12xx_rearm_tx_watchdog_locked(wl);
  1594. out:
  1595. return ret;
  1596. }
  1597. int wl12xx_cmd_stop_channel_switch(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1598. {
  1599. struct wl12xx_cmd_stop_channel_switch *cmd;
  1600. int ret;
  1601. wl1271_debug(DEBUG_ACX, "cmd stop channel switch");
  1602. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1603. if (!cmd) {
  1604. ret = -ENOMEM;
  1605. goto out;
  1606. }
  1607. cmd->role_id = wlvif->role_id;
  1608. ret = wl1271_cmd_send(wl, CMD_STOP_CHANNEL_SWICTH, cmd, sizeof(*cmd), 0);
  1609. if (ret < 0) {
  1610. wl1271_error("failed to stop channel switch command");
  1611. goto out_free;
  1612. }
  1613. out_free:
  1614. kfree(cmd);
  1615. out:
  1616. return ret;
  1617. }
  1618. /* start dev role and roc on its channel */
  1619. int wl12xx_start_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1620. enum nl80211_band band, int channel)
  1621. {
  1622. int ret;
  1623. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1624. wlvif->bss_type == BSS_TYPE_IBSS)))
  1625. return -EINVAL;
  1626. /* the dev role is already started for p2p mgmt interfaces */
  1627. if (!wlcore_is_p2p_mgmt(wlvif)) {
  1628. ret = wl12xx_cmd_role_enable(wl,
  1629. wl12xx_wlvif_to_vif(wlvif)->addr,
  1630. WL1271_ROLE_DEVICE,
  1631. &wlvif->dev_role_id);
  1632. if (ret < 0)
  1633. goto out;
  1634. }
  1635. ret = wl12xx_cmd_role_start_dev(wl, wlvif, band, channel);
  1636. if (ret < 0)
  1637. goto out_disable;
  1638. ret = wl12xx_roc(wl, wlvif, wlvif->dev_role_id, band, channel);
  1639. if (ret < 0)
  1640. goto out_stop;
  1641. return 0;
  1642. out_stop:
  1643. wl12xx_cmd_role_stop_dev(wl, wlvif);
  1644. out_disable:
  1645. if (!wlcore_is_p2p_mgmt(wlvif))
  1646. wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1647. out:
  1648. return ret;
  1649. }
  1650. /* croc dev hlid, and stop the role */
  1651. int wl12xx_stop_dev(struct wl1271 *wl, struct wl12xx_vif *wlvif)
  1652. {
  1653. int ret;
  1654. if (WARN_ON(!(wlvif->bss_type == BSS_TYPE_STA_BSS ||
  1655. wlvif->bss_type == BSS_TYPE_IBSS)))
  1656. return -EINVAL;
  1657. /* flush all pending packets */
  1658. ret = wlcore_tx_work_locked(wl);
  1659. if (ret < 0)
  1660. goto out;
  1661. if (test_bit(wlvif->dev_role_id, wl->roc_map)) {
  1662. ret = wl12xx_croc(wl, wlvif->dev_role_id);
  1663. if (ret < 0)
  1664. goto out;
  1665. }
  1666. ret = wl12xx_cmd_role_stop_dev(wl, wlvif);
  1667. if (ret < 0)
  1668. goto out;
  1669. if (!wlcore_is_p2p_mgmt(wlvif)) {
  1670. ret = wl12xx_cmd_role_disable(wl, &wlvif->dev_role_id);
  1671. if (ret < 0)
  1672. goto out;
  1673. }
  1674. out:
  1675. return ret;
  1676. }
  1677. int wlcore_cmd_generic_cfg(struct wl1271 *wl, struct wl12xx_vif *wlvif,
  1678. u8 feature, u8 enable, u8 value)
  1679. {
  1680. struct wlcore_cmd_generic_cfg *cmd;
  1681. int ret;
  1682. wl1271_debug(DEBUG_CMD,
  1683. "cmd generic cfg (role %d feature %d enable %d value %d)",
  1684. wlvif->role_id, feature, enable, value);
  1685. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  1686. if (!cmd)
  1687. return -ENOMEM;
  1688. cmd->role_id = wlvif->role_id;
  1689. cmd->feature = feature;
  1690. cmd->enable = enable;
  1691. cmd->value = value;
  1692. ret = wl1271_cmd_send(wl, CMD_GENERIC_CFG, cmd, sizeof(*cmd), 0);
  1693. if (ret < 0) {
  1694. wl1271_error("failed to send generic cfg command");
  1695. goto out_free;
  1696. }
  1697. out_free:
  1698. kfree(cmd);
  1699. return ret;
  1700. }
  1701. EXPORT_SYMBOL_GPL(wlcore_cmd_generic_cfg);