cmd.c 41 KB

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