1 /*
2  *  Copyright (C) 2008, cozybit Inc.
3  *  Copyright (C) 2003-2006, Marvell International Ltd.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or (at
8  *  your option) any later version.
9  */
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 
12 #include <linux/slab.h>
13 
14 #include "libertas_tf.h"
15 
16 static const struct channel_range channel_ranges[] = {
17 	{ LBTF_REGDOMAIN_US,		1, 12 },
18 	{ LBTF_REGDOMAIN_CA,		1, 12 },
19 	{ LBTF_REGDOMAIN_EU,		1, 14 },
20 	{ LBTF_REGDOMAIN_JP,		1, 14 },
21 	{ LBTF_REGDOMAIN_SP,		1, 14 },
22 	{ LBTF_REGDOMAIN_FR,		1, 14 },
23 };
24 
25 static u16 lbtf_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
26 {
27 	LBTF_REGDOMAIN_US, LBTF_REGDOMAIN_CA, LBTF_REGDOMAIN_EU,
28 	LBTF_REGDOMAIN_SP, LBTF_REGDOMAIN_FR, LBTF_REGDOMAIN_JP,
29 };
30 
31 static struct cmd_ctrl_node *lbtf_get_cmd_ctrl_node(struct lbtf_private *priv);
32 
33 
34 /**
35  *  lbtf_cmd_copyback - Simple callback that copies response back into command
36  *
37  *  @priv	A pointer to struct lbtf_private structure
38  *  @extra	A pointer to the original command structure for which
39  *		'resp' is a response
40  *  @resp	A pointer to the command response
41  *
42  *  Returns: 0 on success, error on failure
43  */
lbtf_cmd_copyback(struct lbtf_private * priv,unsigned long extra,struct cmd_header * resp)44 int lbtf_cmd_copyback(struct lbtf_private *priv, unsigned long extra,
45 		     struct cmd_header *resp)
46 {
47 	struct cmd_header *buf = (void *)extra;
48 	uint16_t copy_len;
49 
50 	copy_len = min(le16_to_cpu(buf->size), le16_to_cpu(resp->size));
51 	memcpy(buf, resp, copy_len);
52 	return 0;
53 }
54 EXPORT_SYMBOL_GPL(lbtf_cmd_copyback);
55 
56 #define CHAN_TO_IDX(chan) ((chan) - 1)
57 
lbtf_geo_init(struct lbtf_private * priv)58 static void lbtf_geo_init(struct lbtf_private *priv)
59 {
60 	const struct channel_range *range = channel_ranges;
61 	u8 ch;
62 	int i;
63 
64 	for (i = 0; i < ARRAY_SIZE(channel_ranges); i++)
65 		if (channel_ranges[i].regdomain == priv->regioncode) {
66 			range = &channel_ranges[i];
67 			break;
68 		}
69 
70 	for (ch = priv->range.start; ch < priv->range.end; ch++)
71 		priv->channels[CHAN_TO_IDX(ch)].flags = 0;
72 }
73 
74 /**
75  *  lbtf_update_hw_spec: Updates the hardware details.
76  *
77  *  @priv    	A pointer to struct lbtf_private structure
78  *
79  *  Returns: 0 on success, error on failure
80  */
lbtf_update_hw_spec(struct lbtf_private * priv)81 int lbtf_update_hw_spec(struct lbtf_private *priv)
82 {
83 	struct cmd_ds_get_hw_spec cmd;
84 	int ret = -1;
85 	u32 i;
86 
87 	lbtf_deb_enter(LBTF_DEB_CMD);
88 
89 	memset(&cmd, 0, sizeof(cmd));
90 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
91 	memcpy(cmd.permanentaddr, priv->current_addr, ETH_ALEN);
92 	ret = lbtf_cmd_with_response(priv, CMD_GET_HW_SPEC, &cmd);
93 	if (ret)
94 		goto out;
95 
96 	priv->fwcapinfo = le32_to_cpu(cmd.fwcapinfo);
97 
98 	/* The firmware release is in an interesting format: the patch
99 	 * level is in the most significant nibble ... so fix that: */
100 	priv->fwrelease = le32_to_cpu(cmd.fwrelease);
101 	priv->fwrelease = (priv->fwrelease << 8) |
102 		(priv->fwrelease >> 24 & 0xff);
103 
104 	printk(KERN_INFO "libertastf: %pM, fw %u.%u.%up%u, cap 0x%08x\n",
105 		cmd.permanentaddr,
106 		priv->fwrelease >> 24 & 0xff,
107 		priv->fwrelease >> 16 & 0xff,
108 		priv->fwrelease >>  8 & 0xff,
109 		priv->fwrelease       & 0xff,
110 		priv->fwcapinfo);
111 	lbtf_deb_cmd("GET_HW_SPEC: hardware interface 0x%x, hardware spec 0x%04x\n",
112 		    cmd.hwifversion, cmd.version);
113 
114 	/* Clamp region code to 8-bit since FW spec indicates that it should
115 	 * only ever be 8-bit, even though the field size is 16-bit.  Some
116 	 * firmware returns non-zero high 8 bits here.
117 	 */
118 	priv->regioncode = le16_to_cpu(cmd.regioncode) & 0xFF;
119 
120 	for (i = 0; i < MRVDRV_MAX_REGION_CODE; i++) {
121 		/* use the region code to search for the index */
122 		if (priv->regioncode == lbtf_region_code_to_index[i])
123 			break;
124 	}
125 
126 	/* if it's unidentified region code, use the default (USA) */
127 	if (i >= MRVDRV_MAX_REGION_CODE) {
128 		priv->regioncode = 0x10;
129 		pr_info("unidentified region code; using the default (USA)\n");
130 	}
131 
132 	if (priv->current_addr[0] == 0xff)
133 		memmove(priv->current_addr, cmd.permanentaddr, ETH_ALEN);
134 
135 	SET_IEEE80211_PERM_ADDR(priv->hw, priv->current_addr);
136 
137 	lbtf_geo_init(priv);
138 out:
139 	lbtf_deb_leave(LBTF_DEB_CMD);
140 	return ret;
141 }
142 
143 /**
144  *  lbtf_set_channel: Set the radio channel
145  *
146  *  @priv	A pointer to struct lbtf_private structure
147  *  @channel	The desired channel, or 0 to clear a locked channel
148  *
149  *  Returns: 0 on success, error on failure
150  */
lbtf_set_channel(struct lbtf_private * priv,u8 channel)151 int lbtf_set_channel(struct lbtf_private *priv, u8 channel)
152 {
153 	int ret = 0;
154 	struct cmd_ds_802_11_rf_channel cmd;
155 
156 	lbtf_deb_enter(LBTF_DEB_CMD);
157 
158 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
159 	cmd.action = cpu_to_le16(CMD_OPT_802_11_RF_CHANNEL_SET);
160 	cmd.channel = cpu_to_le16(channel);
161 
162 	ret = lbtf_cmd_with_response(priv, CMD_802_11_RF_CHANNEL, &cmd);
163 	lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
164 	return ret;
165 }
166 
lbtf_beacon_set(struct lbtf_private * priv,struct sk_buff * beacon)167 int lbtf_beacon_set(struct lbtf_private *priv, struct sk_buff *beacon)
168 {
169 	struct cmd_ds_802_11_beacon_set cmd;
170 	int size;
171 
172 	lbtf_deb_enter(LBTF_DEB_CMD);
173 
174 	if (beacon->len > MRVL_MAX_BCN_SIZE) {
175 		lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", -1);
176 		return -1;
177 	}
178 	size =  sizeof(cmd) - sizeof(cmd.beacon) + beacon->len;
179 	cmd.hdr.size = cpu_to_le16(size);
180 	cmd.len = cpu_to_le16(beacon->len);
181 	memcpy(cmd.beacon, (u8 *) beacon->data, beacon->len);
182 
183 	lbtf_cmd_async(priv, CMD_802_11_BEACON_SET, &cmd.hdr, size);
184 
185 	lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", 0);
186 	return 0;
187 }
188 
lbtf_beacon_ctrl(struct lbtf_private * priv,bool beacon_enable,int beacon_int)189 int lbtf_beacon_ctrl(struct lbtf_private *priv, bool beacon_enable,
190 		     int beacon_int)
191 {
192 	struct cmd_ds_802_11_beacon_control cmd;
193 	lbtf_deb_enter(LBTF_DEB_CMD);
194 
195 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
196 	cmd.action = cpu_to_le16(CMD_ACT_SET);
197 	cmd.beacon_enable = cpu_to_le16(beacon_enable);
198 	cmd.beacon_period = cpu_to_le16(beacon_int);
199 
200 	lbtf_cmd_async(priv, CMD_802_11_BEACON_CTRL, &cmd.hdr, sizeof(cmd));
201 
202 	lbtf_deb_leave(LBTF_DEB_CMD);
203 	return 0;
204 }
205 
lbtf_queue_cmd(struct lbtf_private * priv,struct cmd_ctrl_node * cmdnode)206 static void lbtf_queue_cmd(struct lbtf_private *priv,
207 			  struct cmd_ctrl_node *cmdnode)
208 {
209 	unsigned long flags;
210 	lbtf_deb_enter(LBTF_DEB_HOST);
211 
212 	if (!cmdnode) {
213 		lbtf_deb_host("QUEUE_CMD: cmdnode is NULL\n");
214 		goto qcmd_done;
215 	}
216 
217 	if (!cmdnode->cmdbuf->size) {
218 		lbtf_deb_host("DNLD_CMD: cmd size is zero\n");
219 		goto qcmd_done;
220 	}
221 
222 	cmdnode->result = 0;
223 	spin_lock_irqsave(&priv->driver_lock, flags);
224 	list_add_tail(&cmdnode->list, &priv->cmdpendingq);
225 	spin_unlock_irqrestore(&priv->driver_lock, flags);
226 
227 	lbtf_deb_host("QUEUE_CMD: inserted command 0x%04x into cmdpendingq\n",
228 		     le16_to_cpu(cmdnode->cmdbuf->command));
229 
230 qcmd_done:
231 	lbtf_deb_leave(LBTF_DEB_HOST);
232 }
233 
lbtf_submit_command(struct lbtf_private * priv,struct cmd_ctrl_node * cmdnode)234 static void lbtf_submit_command(struct lbtf_private *priv,
235 			       struct cmd_ctrl_node *cmdnode)
236 {
237 	unsigned long flags;
238 	struct cmd_header *cmd;
239 	uint16_t cmdsize;
240 	uint16_t command;
241 	int timeo = 5 * HZ;
242 	int ret;
243 
244 	lbtf_deb_enter(LBTF_DEB_HOST);
245 
246 	cmd = cmdnode->cmdbuf;
247 
248 	spin_lock_irqsave(&priv->driver_lock, flags);
249 	priv->cur_cmd = cmdnode;
250 	cmdsize = le16_to_cpu(cmd->size);
251 	command = le16_to_cpu(cmd->command);
252 
253 	lbtf_deb_cmd("DNLD_CMD: command 0x%04x, seq %d, size %d\n",
254 		     command, le16_to_cpu(cmd->seqnum), cmdsize);
255 	lbtf_deb_hex(LBTF_DEB_CMD, "DNLD_CMD", (void *) cmdnode->cmdbuf, cmdsize);
256 
257 	ret = priv->hw_host_to_card(priv, MVMS_CMD, (u8 *) cmd, cmdsize);
258 	spin_unlock_irqrestore(&priv->driver_lock, flags);
259 
260 	if (ret) {
261 		pr_info("DNLD_CMD: hw_host_to_card failed: %d\n", ret);
262 		/* Let the timer kick in and retry, and potentially reset
263 		   the whole thing if the condition persists */
264 		timeo = HZ;
265 	}
266 
267 	/* Setup the timer after transmit command */
268 	mod_timer(&priv->command_timer, jiffies + timeo);
269 
270 	lbtf_deb_leave(LBTF_DEB_HOST);
271 }
272 
273 /**
274  *  This function inserts command node to cmdfreeq
275  *  after cleans it. Requires priv->driver_lock held.
276  */
__lbtf_cleanup_and_insert_cmd(struct lbtf_private * priv,struct cmd_ctrl_node * cmdnode)277 static void __lbtf_cleanup_and_insert_cmd(struct lbtf_private *priv,
278 					 struct cmd_ctrl_node *cmdnode)
279 {
280 	lbtf_deb_enter(LBTF_DEB_HOST);
281 
282 	if (!cmdnode)
283 		goto cl_ins_out;
284 
285 	cmdnode->callback = NULL;
286 	cmdnode->callback_arg = 0;
287 
288 	memset(cmdnode->cmdbuf, 0, LBS_CMD_BUFFER_SIZE);
289 
290 	list_add_tail(&cmdnode->list, &priv->cmdfreeq);
291 
292 cl_ins_out:
293 	lbtf_deb_leave(LBTF_DEB_HOST);
294 }
295 
lbtf_cleanup_and_insert_cmd(struct lbtf_private * priv,struct cmd_ctrl_node * ptempcmd)296 static void lbtf_cleanup_and_insert_cmd(struct lbtf_private *priv,
297 	struct cmd_ctrl_node *ptempcmd)
298 {
299 	unsigned long flags;
300 
301 	spin_lock_irqsave(&priv->driver_lock, flags);
302 	__lbtf_cleanup_and_insert_cmd(priv, ptempcmd);
303 	spin_unlock_irqrestore(&priv->driver_lock, flags);
304 }
305 
lbtf_complete_command(struct lbtf_private * priv,struct cmd_ctrl_node * cmd,int result)306 void lbtf_complete_command(struct lbtf_private *priv, struct cmd_ctrl_node *cmd,
307 			  int result)
308 {
309 	cmd->result = result;
310 	cmd->cmdwaitqwoken = 1;
311 	wake_up_interruptible(&cmd->cmdwait_q);
312 
313 	if (!cmd->callback)
314 		__lbtf_cleanup_and_insert_cmd(priv, cmd);
315 	priv->cur_cmd = NULL;
316 }
317 
lbtf_cmd_set_mac_multicast_addr(struct lbtf_private * priv)318 int lbtf_cmd_set_mac_multicast_addr(struct lbtf_private *priv)
319 {
320 	struct cmd_ds_mac_multicast_addr cmd;
321 
322 	lbtf_deb_enter(LBTF_DEB_CMD);
323 
324 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
325 	cmd.action = cpu_to_le16(CMD_ACT_SET);
326 
327 	cmd.nr_of_adrs = cpu_to_le16((u16) priv->nr_of_multicastmacaddr);
328 
329 	lbtf_deb_cmd("MULTICAST_ADR: setting %d addresses\n", cmd.nr_of_adrs);
330 
331 	memcpy(cmd.maclist, priv->multicastlist,
332 	       priv->nr_of_multicastmacaddr * ETH_ALEN);
333 
334 	lbtf_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &cmd.hdr, sizeof(cmd));
335 
336 	lbtf_deb_leave(LBTF_DEB_CMD);
337 	return 0;
338 }
339 
lbtf_set_mode(struct lbtf_private * priv,enum lbtf_mode mode)340 void lbtf_set_mode(struct lbtf_private *priv, enum lbtf_mode mode)
341 {
342 	struct cmd_ds_set_mode cmd;
343 	lbtf_deb_enter(LBTF_DEB_WEXT);
344 
345 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
346 	cmd.mode = cpu_to_le16(mode);
347 	lbtf_deb_wext("Switching to mode: 0x%x\n", mode);
348 	lbtf_cmd_async(priv, CMD_802_11_SET_MODE, &cmd.hdr, sizeof(cmd));
349 
350 	lbtf_deb_leave(LBTF_DEB_WEXT);
351 }
352 
lbtf_set_bssid(struct lbtf_private * priv,bool activate,const u8 * bssid)353 void lbtf_set_bssid(struct lbtf_private *priv, bool activate, const u8 *bssid)
354 {
355 	struct cmd_ds_set_bssid cmd;
356 	lbtf_deb_enter(LBTF_DEB_CMD);
357 
358 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
359 	cmd.activate = activate ? 1 : 0;
360 	if (activate)
361 		memcpy(cmd.bssid, bssid, ETH_ALEN);
362 
363 	lbtf_cmd_async(priv, CMD_802_11_SET_BSSID, &cmd.hdr, sizeof(cmd));
364 	lbtf_deb_leave(LBTF_DEB_CMD);
365 }
366 
lbtf_set_mac_address(struct lbtf_private * priv,uint8_t * mac_addr)367 int lbtf_set_mac_address(struct lbtf_private *priv, uint8_t *mac_addr)
368 {
369 	struct cmd_ds_802_11_mac_address cmd;
370 	lbtf_deb_enter(LBTF_DEB_CMD);
371 
372 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
373 	cmd.action = cpu_to_le16(CMD_ACT_SET);
374 
375 	memcpy(cmd.macadd, mac_addr, ETH_ALEN);
376 
377 	lbtf_cmd_async(priv, CMD_802_11_MAC_ADDRESS, &cmd.hdr, sizeof(cmd));
378 	lbtf_deb_leave(LBTF_DEB_CMD);
379 	return 0;
380 }
381 
lbtf_set_radio_control(struct lbtf_private * priv)382 int lbtf_set_radio_control(struct lbtf_private *priv)
383 {
384 	int ret = 0;
385 	struct cmd_ds_802_11_radio_control cmd;
386 
387 	lbtf_deb_enter(LBTF_DEB_CMD);
388 
389 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
390 	cmd.action = cpu_to_le16(CMD_ACT_SET);
391 
392 	switch (priv->preamble) {
393 	case CMD_TYPE_SHORT_PREAMBLE:
394 		cmd.control = cpu_to_le16(SET_SHORT_PREAMBLE);
395 		break;
396 
397 	case CMD_TYPE_LONG_PREAMBLE:
398 		cmd.control = cpu_to_le16(SET_LONG_PREAMBLE);
399 		break;
400 
401 	case CMD_TYPE_AUTO_PREAMBLE:
402 	default:
403 		cmd.control = cpu_to_le16(SET_AUTO_PREAMBLE);
404 		break;
405 	}
406 
407 	if (priv->radioon)
408 		cmd.control |= cpu_to_le16(TURN_ON_RF);
409 	else
410 		cmd.control &= cpu_to_le16(~TURN_ON_RF);
411 
412 	lbtf_deb_cmd("RADIO_SET: radio %d, preamble %d\n", priv->radioon,
413 		    priv->preamble);
414 
415 	ret = lbtf_cmd_with_response(priv, CMD_802_11_RADIO_CONTROL, &cmd);
416 
417 	lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
418 	return ret;
419 }
420 
lbtf_set_mac_control(struct lbtf_private * priv)421 void lbtf_set_mac_control(struct lbtf_private *priv)
422 {
423 	struct cmd_ds_mac_control cmd;
424 	lbtf_deb_enter(LBTF_DEB_CMD);
425 
426 	cmd.hdr.size = cpu_to_le16(sizeof(cmd));
427 	cmd.action = cpu_to_le16(priv->mac_control);
428 	cmd.reserved = 0;
429 
430 	lbtf_cmd_async(priv, CMD_MAC_CONTROL,
431 		&cmd.hdr, sizeof(cmd));
432 
433 	lbtf_deb_leave(LBTF_DEB_CMD);
434 }
435 
436 /**
437  *  lbtf_allocate_cmd_buffer - Allocates cmd buffer, links it to free cmd queue
438  *
439  *  @priv	A pointer to struct lbtf_private structure
440  *
441  *  Returns: 0 on success.
442  */
lbtf_allocate_cmd_buffer(struct lbtf_private * priv)443 int lbtf_allocate_cmd_buffer(struct lbtf_private *priv)
444 {
445 	int ret = 0;
446 	u32 bufsize;
447 	u32 i;
448 	struct cmd_ctrl_node *cmdarray;
449 
450 	lbtf_deb_enter(LBTF_DEB_HOST);
451 
452 	/* Allocate and initialize the command array */
453 	bufsize = sizeof(struct cmd_ctrl_node) * LBS_NUM_CMD_BUFFERS;
454 	cmdarray = kzalloc(bufsize, GFP_KERNEL);
455 	if (!cmdarray) {
456 		lbtf_deb_host("ALLOC_CMD_BUF: tempcmd_array is NULL\n");
457 		ret = -1;
458 		goto done;
459 	}
460 	priv->cmd_array = cmdarray;
461 
462 	/* Allocate and initialize each command buffer in the command array */
463 	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
464 		cmdarray[i].cmdbuf = kzalloc(LBS_CMD_BUFFER_SIZE, GFP_KERNEL);
465 		if (!cmdarray[i].cmdbuf) {
466 			lbtf_deb_host("ALLOC_CMD_BUF: ptempvirtualaddr is NULL\n");
467 			ret = -1;
468 			goto done;
469 		}
470 	}
471 
472 	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
473 		init_waitqueue_head(&cmdarray[i].cmdwait_q);
474 		lbtf_cleanup_and_insert_cmd(priv, &cmdarray[i]);
475 	}
476 
477 	ret = 0;
478 
479 done:
480 	lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %d", ret);
481 	return ret;
482 }
483 
484 /**
485  *  lbtf_free_cmd_buffer - Frees the cmd buffer.
486  *
487  *  @priv	A pointer to struct lbtf_private structure
488  *
489  *  Returns: 0
490  */
lbtf_free_cmd_buffer(struct lbtf_private * priv)491 int lbtf_free_cmd_buffer(struct lbtf_private *priv)
492 {
493 	struct cmd_ctrl_node *cmdarray;
494 	unsigned int i;
495 
496 	lbtf_deb_enter(LBTF_DEB_HOST);
497 
498 	/* need to check if cmd array is allocated or not */
499 	if (priv->cmd_array == NULL) {
500 		lbtf_deb_host("FREE_CMD_BUF: cmd_array is NULL\n");
501 		goto done;
502 	}
503 
504 	cmdarray = priv->cmd_array;
505 
506 	/* Release shared memory buffers */
507 	for (i = 0; i < LBS_NUM_CMD_BUFFERS; i++) {
508 		kfree(cmdarray[i].cmdbuf);
509 		cmdarray[i].cmdbuf = NULL;
510 	}
511 
512 	/* Release cmd_ctrl_node */
513 	kfree(priv->cmd_array);
514 	priv->cmd_array = NULL;
515 
516 done:
517 	lbtf_deb_leave(LBTF_DEB_HOST);
518 	return 0;
519 }
520 
521 /**
522  *  lbtf_get_cmd_ctrl_node - Gets free cmd node from free cmd queue.
523  *
524  *  @priv		A pointer to struct lbtf_private structure
525  *
526  *  Returns: pointer to a struct cmd_ctrl_node or NULL if none available.
527  */
lbtf_get_cmd_ctrl_node(struct lbtf_private * priv)528 static struct cmd_ctrl_node *lbtf_get_cmd_ctrl_node(struct lbtf_private *priv)
529 {
530 	struct cmd_ctrl_node *tempnode;
531 	unsigned long flags;
532 
533 	lbtf_deb_enter(LBTF_DEB_HOST);
534 
535 	if (!priv)
536 		return NULL;
537 
538 	spin_lock_irqsave(&priv->driver_lock, flags);
539 
540 	if (!list_empty(&priv->cmdfreeq)) {
541 		tempnode = list_first_entry(&priv->cmdfreeq,
542 					    struct cmd_ctrl_node, list);
543 		list_del(&tempnode->list);
544 	} else {
545 		lbtf_deb_host("GET_CMD_NODE: cmd_ctrl_node is not available\n");
546 		tempnode = NULL;
547 	}
548 
549 	spin_unlock_irqrestore(&priv->driver_lock, flags);
550 
551 	lbtf_deb_leave(LBTF_DEB_HOST);
552 	return tempnode;
553 }
554 
555 /**
556  *  lbtf_execute_next_command: execute next command in cmd pending queue.
557  *
558  *  @priv     A pointer to struct lbtf_private structure
559  *
560  *  Returns: 0 on success.
561  */
lbtf_execute_next_command(struct lbtf_private * priv)562 int lbtf_execute_next_command(struct lbtf_private *priv)
563 {
564 	struct cmd_ctrl_node *cmdnode = NULL;
565 	struct cmd_header *cmd;
566 	unsigned long flags;
567 	int ret = 0;
568 
569 	/* Debug group is lbtf_deb_THREAD and not lbtf_deb_HOST, because the
570 	 * only caller to us is lbtf_thread() and we get even when a
571 	 * data packet is received */
572 	lbtf_deb_enter(LBTF_DEB_THREAD);
573 
574 	spin_lock_irqsave(&priv->driver_lock, flags);
575 
576 	if (priv->cur_cmd) {
577 		pr_alert("EXEC_NEXT_CMD: already processing command!\n");
578 		spin_unlock_irqrestore(&priv->driver_lock, flags);
579 		ret = -1;
580 		goto done;
581 	}
582 
583 	if (!list_empty(&priv->cmdpendingq)) {
584 		cmdnode = list_first_entry(&priv->cmdpendingq,
585 					   struct cmd_ctrl_node, list);
586 	}
587 
588 	if (cmdnode) {
589 		cmd = cmdnode->cmdbuf;
590 
591 		list_del(&cmdnode->list);
592 		lbtf_deb_host("EXEC_NEXT_CMD: sending command 0x%04x\n",
593 			    le16_to_cpu(cmd->command));
594 		spin_unlock_irqrestore(&priv->driver_lock, flags);
595 		lbtf_submit_command(priv, cmdnode);
596 	} else
597 		spin_unlock_irqrestore(&priv->driver_lock, flags);
598 
599 	ret = 0;
600 done:
601 	lbtf_deb_leave(LBTF_DEB_THREAD);
602 	return ret;
603 }
604 
__lbtf_cmd_async(struct lbtf_private * priv,uint16_t command,struct cmd_header * in_cmd,int in_cmd_size,int (* callback)(struct lbtf_private *,unsigned long,struct cmd_header *),unsigned long callback_arg)605 static struct cmd_ctrl_node *__lbtf_cmd_async(struct lbtf_private *priv,
606 	uint16_t command, struct cmd_header *in_cmd, int in_cmd_size,
607 	int (*callback)(struct lbtf_private *, unsigned long,
608 			struct cmd_header *),
609 	unsigned long callback_arg)
610 {
611 	struct cmd_ctrl_node *cmdnode;
612 
613 	lbtf_deb_enter(LBTF_DEB_HOST);
614 
615 	if (priv->surpriseremoved) {
616 		lbtf_deb_host("PREP_CMD: card removed\n");
617 		cmdnode = ERR_PTR(-ENOENT);
618 		goto done;
619 	}
620 
621 	cmdnode = lbtf_get_cmd_ctrl_node(priv);
622 	if (cmdnode == NULL) {
623 		lbtf_deb_host("PREP_CMD: cmdnode is NULL\n");
624 
625 		/* Wake up main thread to execute next command */
626 		queue_work(lbtf_wq, &priv->cmd_work);
627 		cmdnode = ERR_PTR(-ENOBUFS);
628 		goto done;
629 	}
630 
631 	cmdnode->callback = callback;
632 	cmdnode->callback_arg = callback_arg;
633 
634 	/* Copy the incoming command to the buffer */
635 	memcpy(cmdnode->cmdbuf, in_cmd, in_cmd_size);
636 
637 	/* Set sequence number, clean result, move to buffer */
638 	priv->seqnum++;
639 	cmdnode->cmdbuf->command = cpu_to_le16(command);
640 	cmdnode->cmdbuf->size    = cpu_to_le16(in_cmd_size);
641 	cmdnode->cmdbuf->seqnum  = cpu_to_le16(priv->seqnum);
642 	cmdnode->cmdbuf->result  = 0;
643 
644 	lbtf_deb_host("PREP_CMD: command 0x%04x\n", command);
645 
646 	cmdnode->cmdwaitqwoken = 0;
647 	lbtf_queue_cmd(priv, cmdnode);
648 	queue_work(lbtf_wq, &priv->cmd_work);
649 
650  done:
651 	lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %p", cmdnode);
652 	return cmdnode;
653 }
654 
lbtf_cmd_async(struct lbtf_private * priv,uint16_t command,struct cmd_header * in_cmd,int in_cmd_size)655 void lbtf_cmd_async(struct lbtf_private *priv, uint16_t command,
656 	struct cmd_header *in_cmd, int in_cmd_size)
657 {
658 	lbtf_deb_enter(LBTF_DEB_CMD);
659 	__lbtf_cmd_async(priv, command, in_cmd, in_cmd_size, NULL, 0);
660 	lbtf_deb_leave(LBTF_DEB_CMD);
661 }
662 
__lbtf_cmd(struct lbtf_private * priv,uint16_t command,struct cmd_header * in_cmd,int in_cmd_size,int (* callback)(struct lbtf_private *,unsigned long,struct cmd_header *),unsigned long callback_arg)663 int __lbtf_cmd(struct lbtf_private *priv, uint16_t command,
664 	      struct cmd_header *in_cmd, int in_cmd_size,
665 	      int (*callback)(struct lbtf_private *,
666 			      unsigned long, struct cmd_header *),
667 	      unsigned long callback_arg)
668 {
669 	struct cmd_ctrl_node *cmdnode;
670 	unsigned long flags;
671 	int ret = 0;
672 
673 	lbtf_deb_enter(LBTF_DEB_HOST);
674 
675 	cmdnode = __lbtf_cmd_async(priv, command, in_cmd, in_cmd_size,
676 				  callback, callback_arg);
677 	if (IS_ERR(cmdnode)) {
678 		ret = PTR_ERR(cmdnode);
679 		goto done;
680 	}
681 
682 	might_sleep();
683 	ret = wait_event_interruptible(cmdnode->cmdwait_q,
684 				       cmdnode->cmdwaitqwoken);
685 	if (ret) {
686 		pr_info("PREP_CMD: command 0x%04x interrupted by signal: %d\n",
687 			    command, ret);
688 		goto done;
689 	}
690 
691 	spin_lock_irqsave(&priv->driver_lock, flags);
692 	ret = cmdnode->result;
693 	if (ret)
694 		pr_info("PREP_CMD: command 0x%04x failed: %d\n",
695 			    command, ret);
696 
697 	__lbtf_cleanup_and_insert_cmd(priv, cmdnode);
698 	spin_unlock_irqrestore(&priv->driver_lock, flags);
699 
700 done:
701 	lbtf_deb_leave_args(LBTF_DEB_HOST, "ret %d", ret);
702 	return ret;
703 }
704 EXPORT_SYMBOL_GPL(__lbtf_cmd);
705 
706 /* Call holding driver_lock */
lbtf_cmd_response_rx(struct lbtf_private * priv)707 void lbtf_cmd_response_rx(struct lbtf_private *priv)
708 {
709 	priv->cmd_response_rxed = 1;
710 	queue_work(lbtf_wq, &priv->cmd_work);
711 }
712 EXPORT_SYMBOL_GPL(lbtf_cmd_response_rx);
713 
lbtf_process_rx_command(struct lbtf_private * priv)714 int lbtf_process_rx_command(struct lbtf_private *priv)
715 {
716 	uint16_t respcmd, curcmd;
717 	struct cmd_header *resp;
718 	int ret = 0;
719 	unsigned long flags;
720 	uint16_t result;
721 
722 	lbtf_deb_enter(LBTF_DEB_CMD);
723 
724 	mutex_lock(&priv->lock);
725 	spin_lock_irqsave(&priv->driver_lock, flags);
726 
727 	if (!priv->cur_cmd) {
728 		ret = -1;
729 		spin_unlock_irqrestore(&priv->driver_lock, flags);
730 		goto done;
731 	}
732 
733 	resp = (void *)priv->cmd_resp_buff;
734 	curcmd = le16_to_cpu(priv->cur_cmd->cmdbuf->command);
735 	respcmd = le16_to_cpu(resp->command);
736 	result = le16_to_cpu(resp->result);
737 
738 	if (net_ratelimit())
739 		pr_info("libertastf: cmd response 0x%04x, seq %d, size %d\n",
740 			respcmd, le16_to_cpu(resp->seqnum),
741 			le16_to_cpu(resp->size));
742 
743 	if (resp->seqnum != priv->cur_cmd->cmdbuf->seqnum) {
744 		spin_unlock_irqrestore(&priv->driver_lock, flags);
745 		ret = -1;
746 		goto done;
747 	}
748 	if (respcmd != CMD_RET(curcmd)) {
749 		spin_unlock_irqrestore(&priv->driver_lock, flags);
750 		ret = -1;
751 		goto done;
752 	}
753 
754 	if (resp->result == cpu_to_le16(0x0004)) {
755 		/* 0x0004 means -EAGAIN. Drop the response, let it time out
756 		   and be resubmitted */
757 		spin_unlock_irqrestore(&priv->driver_lock, flags);
758 		ret = -1;
759 		goto done;
760 	}
761 
762 	/* Now we got response from FW, cancel the command timer */
763 	del_timer(&priv->command_timer);
764 	priv->cmd_timed_out = 0;
765 	if (priv->nr_retries)
766 		priv->nr_retries = 0;
767 
768 	/* If the command is not successful, cleanup and return failure */
769 	if ((result != 0 || !(respcmd & 0x8000))) {
770 		/*
771 		 * Handling errors here
772 		 */
773 		switch (respcmd) {
774 		case CMD_RET(CMD_GET_HW_SPEC):
775 		case CMD_RET(CMD_802_11_RESET):
776 			pr_info("libertastf: reset failed\n");
777 			break;
778 
779 		}
780 		lbtf_complete_command(priv, priv->cur_cmd, result);
781 		spin_unlock_irqrestore(&priv->driver_lock, flags);
782 
783 		ret = -1;
784 		goto done;
785 	}
786 
787 	spin_unlock_irqrestore(&priv->driver_lock, flags);
788 
789 	if (priv->cur_cmd && priv->cur_cmd->callback) {
790 		ret = priv->cur_cmd->callback(priv, priv->cur_cmd->callback_arg,
791 				resp);
792 	}
793 	spin_lock_irqsave(&priv->driver_lock, flags);
794 
795 	if (priv->cur_cmd) {
796 		/* Clean up and Put current command back to cmdfreeq */
797 		lbtf_complete_command(priv, priv->cur_cmd, result);
798 	}
799 	spin_unlock_irqrestore(&priv->driver_lock, flags);
800 
801 done:
802 	mutex_unlock(&priv->lock);
803 	lbtf_deb_leave_args(LBTF_DEB_CMD, "ret %d", ret);
804 	return ret;
805 }
806