cmd.c 47 KB

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