cmd.c 47 KB

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