1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  *  Copyright (C) 2008, cozybit Inc.
4  *  Copyright (C) 2007, Red Hat, Inc.
5  *  Copyright (C) 2003-2006, Marvell International Ltd.
6  */
7 #include <linux/spinlock.h>
8 #include <linux/device.h>
9 #include <linux/kthread.h>
10 #include <net/mac80211.h>
11 
12 #include "deb_defs.h"
13 
14 #ifndef DRV_NAME
15 #define DRV_NAME "libertas_tf"
16 #endif
17 
18 #define	MRVL_DEFAULT_RETRIES			9
19 #define MRVL_PER_PACKET_RATE			0x10
20 #define MRVL_MAX_BCN_SIZE			440
21 #define CMD_OPTION_WAITFORRSP			0x0002
22 
23 /* Return command are almost always the same as the host command, but with
24  * bit 15 set high.  There are a few exceptions, though...
25  */
26 #define CMD_RET(cmd)			(0x8000 | cmd)
27 
28 /* Command codes */
29 #define CMD_GET_HW_SPEC				0x0003
30 #define CMD_802_11_RESET			0x0005
31 #define CMD_MAC_MULTICAST_ADR			0x0010
32 #define CMD_802_11_RADIO_CONTROL		0x001c
33 #define CMD_802_11_RF_CHANNEL			0x001d
34 #define CMD_802_11_RF_TX_POWER			0x001e
35 #define CMD_MAC_CONTROL				0x0028
36 #define CMD_802_11_MAC_ADDRESS			0x004d
37 #define	CMD_SET_BOOT2_VER			0x00a5
38 #define CMD_802_11_BEACON_CTRL			0x00b0
39 #define CMD_802_11_BEACON_SET			0x00cb
40 #define CMD_802_11_SET_MODE			0x00cc
41 #define CMD_802_11_SET_BSSID			0x00cd
42 
43 #define CMD_ACT_GET			0x0000
44 #define CMD_ACT_SET			0x0001
45 
46 /* Define action or option for CMD_802_11_RESET */
47 #define CMD_ACT_HALT			0x0003
48 
49 /* Define action or option for CMD_MAC_CONTROL */
50 #define CMD_ACT_MAC_RX_ON			0x0001
51 #define CMD_ACT_MAC_TX_ON			0x0002
52 #define CMD_ACT_MAC_MULTICAST_ENABLE		0x0020
53 #define CMD_ACT_MAC_BROADCAST_ENABLE		0x0040
54 #define CMD_ACT_MAC_PROMISCUOUS_ENABLE		0x0080
55 #define CMD_ACT_MAC_ALL_MULTICAST_ENABLE	0x0100
56 
57 /* Define action or option for CMD_802_11_RADIO_CONTROL */
58 #define CMD_TYPE_AUTO_PREAMBLE		0x0001
59 #define CMD_TYPE_SHORT_PREAMBLE		0x0002
60 #define CMD_TYPE_LONG_PREAMBLE		0x0003
61 
62 #define TURN_ON_RF			0x01
63 #define RADIO_ON			0x01
64 #define RADIO_OFF			0x00
65 
66 #define SET_AUTO_PREAMBLE		0x05
67 #define SET_SHORT_PREAMBLE		0x03
68 #define SET_LONG_PREAMBLE		0x01
69 
70 /* Define action or option for CMD_802_11_RF_CHANNEL */
71 #define CMD_OPT_802_11_RF_CHANNEL_GET	0x00
72 #define CMD_OPT_802_11_RF_CHANNEL_SET	0x01
73 
74 /* Codes for CMD_802_11_SET_MODE */
75 enum lbtf_mode {
76 	LBTF_PASSIVE_MODE,
77 	LBTF_STA_MODE,
78 	LBTF_AP_MODE,
79 };
80 
81 /** Card Event definition */
82 #define MACREG_INT_CODE_FIRMWARE_READY		48
83 /** Buffer Constants */
84 
85 /*	The size of SQ memory PPA, DPA are 8 DWORDs, that keep the physical
86 *	addresses of TxPD buffers. Station has only 8 TxPD available, Whereas
87 *	driver has more local TxPDs. Each TxPD on the host memory is associated
88 *	with a Tx control node. The driver maintains 8 RxPD descriptors for
89 *	station firmware to store Rx packet information.
90 *
91 *	Current version of MAC has a 32x6 multicast address buffer.
92 *
93 *	802.11b can have up to  14 channels, the driver keeps the
94 *	BSSID(MAC address) of each APs or Ad hoc stations it has sensed.
95 */
96 
97 #define MRVDRV_MAX_MULTICAST_LIST_SIZE	32
98 #define LBS_NUM_CMD_BUFFERS             10
99 #define LBS_CMD_BUFFER_SIZE             (2 * 1024)
100 #define MRVDRV_MAX_CHANNEL_SIZE		14
101 #define MRVDRV_SNAP_HEADER_LEN          8
102 
103 #define	LBS_UPLD_SIZE			2312
104 #define DEV_NAME_LEN			32
105 
106 /** Misc constants */
107 /* This section defines 802.11 specific contants */
108 
109 #define MRVDRV_MAX_REGION_CODE			6
110 /**
111  * the table to keep region code
112  */
113 #define LBTF_REGDOMAIN_US	0x10
114 #define LBTF_REGDOMAIN_CA	0x20
115 #define LBTF_REGDOMAIN_EU	0x30
116 #define LBTF_REGDOMAIN_SP	0x31
117 #define LBTF_REGDOMAIN_FR	0x32
118 #define LBTF_REGDOMAIN_JP	0x40
119 
120 #define SBI_EVENT_CAUSE_SHIFT		3
121 
122 /** RxPD status */
123 
124 #define MRVDRV_RXPD_STATUS_OK                0x0001
125 
126 
127 /* This is for firmware specific length */
128 #define EXTRA_LEN	36
129 
130 #define MRVDRV_ETH_TX_PACKET_BUFFER_SIZE \
131 	(ETH_FRAME_LEN + sizeof(struct txpd) + EXTRA_LEN)
132 
133 #define MRVDRV_ETH_RX_PACKET_BUFFER_SIZE \
134 	(ETH_FRAME_LEN + sizeof(struct rxpd) \
135 	 + MRVDRV_SNAP_HEADER_LEN + EXTRA_LEN)
136 
137 #define	CMD_F_HOSTCMD		(1 << 0)
138 #define FW_CAPINFO_WPA  	(1 << 0)
139 
140 #define RF_ANTENNA_1		0x1
141 #define RF_ANTENNA_2		0x2
142 #define RF_ANTENNA_AUTO		0xFFFF
143 
144 #define LBTF_EVENT_BCN_SENT	55
145 
146 /** Global Variable Declaration */
147 /** mv_ms_type */
148 enum mv_ms_type {
149 	MVMS_DAT = 0,
150 	MVMS_CMD = 1,
151 	MVMS_TXDONE = 2,
152 	MVMS_EVENT
153 };
154 
155 extern struct workqueue_struct *lbtf_wq;
156 
157 struct lbtf_private;
158 
159 struct lbtf_offset_value {
160 	u32 offset;
161 	u32 value;
162 };
163 
164 struct channel_range {
165 	u8 regdomain;
166 	u8 start;
167 	u8 end; /* exclusive (channel must be less than end) */
168 };
169 
170 struct if_usb_card;
171 
172 struct lbtf_ops {
173 	/** Hardware access */
174 	int (*hw_host_to_card)(struct lbtf_private *priv, u8 type,
175 			       u8 *payload, u16 nb);
176 	int (*hw_prog_firmware)(struct lbtf_private *priv);
177 	int (*hw_reset_device)(struct lbtf_private *priv);
178 };
179 
180 /** Private structure for the MV device */
181 struct lbtf_private {
182 	void *card;
183 	struct ieee80211_hw *hw;
184 	const struct lbtf_ops *ops;
185 
186 	/* Command response buffer */
187 	u8 cmd_resp_buff[LBS_UPLD_SIZE];
188 	/* Download sent:
189 	   bit0 1/0=data_sent/data_tx_done,
190 	   bit1 1/0=cmd_sent/cmd_tx_done,
191 	   all other bits reserved 0 */
192 	struct ieee80211_vif *vif;
193 
194 	struct work_struct cmd_work;
195 	struct work_struct tx_work;
196 
197 	/** Wlan adapter data structure*/
198 	/** STATUS variables */
199 	u32 fwrelease;
200 	u32 fwcapinfo;
201 	/* protected with big lock */
202 
203 	struct mutex lock;
204 
205 	/** command-related variables */
206 	u16 seqnum;
207 	/* protected by big lock */
208 
209 	struct cmd_ctrl_node *cmd_array;
210 	/** Current command */
211 	struct cmd_ctrl_node *cur_cmd;
212 	/** command Queues */
213 	/** Free command buffers */
214 	struct list_head cmdfreeq;
215 	/** Pending command buffers */
216 	struct list_head cmdpendingq;
217 
218 	/** spin locks */
219 	spinlock_t driver_lock;
220 
221 	/** Timers */
222 	struct timer_list command_timer;
223 	int nr_retries;
224 	int cmd_timed_out;
225 
226 	u8 cmd_response_rxed;
227 
228 	/** capability Info used in Association, start, join */
229 	u16 capability;
230 
231 	/** MAC address information */
232 	u8 current_addr[ETH_ALEN];
233 	u8 multicastlist[MRVDRV_MAX_MULTICAST_LIST_SIZE][ETH_ALEN];
234 	u32 nr_of_multicastmacaddr;
235 	int cur_freq;
236 
237 	struct sk_buff *skb_to_tx;
238 	struct sk_buff *tx_skb;
239 
240 	/** NIC Operation characteristics */
241 	u16 mac_control;
242 	u16 regioncode;
243 	struct channel_range range;
244 
245 	u8 radioon;
246 	u32 preamble;
247 
248 	struct ieee80211_channel channels[14];
249 	struct ieee80211_rate rates[12];
250 	struct ieee80211_supported_band band;
251 	struct lbtf_offset_value offsetvalue;
252 
253 	u8 surpriseremoved;
254 	struct sk_buff_head bc_ps_buf;
255 
256 	/* Most recently reported noise in dBm */
257 	s8 noise;
258 };
259 
260 /* 802.11-related definitions */
261 
262 /* TxPD descriptor */
263 struct txpd {
264 	/* Current Tx packet status */
265 	__le32 tx_status;
266 	/* Tx control */
267 	__le32 tx_control;
268 	__le32 tx_packet_location;
269 	/* Tx packet length */
270 	__le16 tx_packet_length;
271 	/* First 2 byte of destination MAC address */
272 	u8 tx_dest_addr_high[2];
273 	/* Last 4 byte of destination MAC address */
274 	u8 tx_dest_addr_low[4];
275 	/* Pkt Priority */
276 	u8 priority;
277 	/* Pkt Trasnit Power control */
278 	u8 powermgmt;
279 	/* Time the packet has been queued in the driver (units = 2ms) */
280 	u8 pktdelay_2ms;
281 	/* reserved */
282 	u8 reserved1;
283 };
284 
285 /* RxPD Descriptor */
286 struct rxpd {
287 	/* Current Rx packet status */
288 	__le16 status;
289 
290 	/* SNR */
291 	u8 snr;
292 
293 	/* Tx control */
294 	u8 rx_control;
295 
296 	/* Pkt length */
297 	__le16 pkt_len;
298 
299 	/* Noise Floor */
300 	u8 nf;
301 
302 	/* Rx Packet Rate */
303 	u8 rx_rate;
304 
305 	/* Pkt addr */
306 	__le32 pkt_ptr;
307 
308 	/* Next Rx RxPD addr */
309 	__le32 next_rxpd_ptr;
310 
311 	/* Pkt Priority */
312 	u8 priority;
313 	u8 reserved[3];
314 };
315 
316 struct cmd_header {
317 	__le16 command;
318 	__le16 size;
319 	__le16 seqnum;
320 	__le16 result;
321 } __packed;
322 
323 struct cmd_ctrl_node {
324 	struct list_head list;
325 	int result;
326 	/* command response */
327 	int (*callback)(struct lbtf_private *,
328 			unsigned long, struct cmd_header *);
329 	unsigned long callback_arg;
330 	/* command data */
331 	struct cmd_header *cmdbuf;
332 	/* wait queue */
333 	u16 cmdwaitqwoken;
334 	wait_queue_head_t cmdwait_q;
335 };
336 
337 /*
338  * Define data structure for CMD_GET_HW_SPEC
339  * This structure defines the response for the GET_HW_SPEC command
340  */
341 struct cmd_ds_get_hw_spec {
342 	struct cmd_header hdr;
343 
344 	/* HW Interface version number */
345 	__le16 hwifversion;
346 	/* HW version number */
347 	__le16 version;
348 	/* Max number of TxPD FW can handle */
349 	__le16 nr_txpd;
350 	/* Max no of Multicast address */
351 	__le16 nr_mcast_adr;
352 	/* MAC address */
353 	u8 permanentaddr[6];
354 
355 	/* region Code */
356 	__le16 regioncode;
357 
358 	/* Number of antenna used */
359 	__le16 nr_antenna;
360 
361 	/* FW release number, example 0x01030304 = 2.3.4p1 */
362 	__le32 fwrelease;
363 
364 	/* Base Address of TxPD queue */
365 	__le32 wcb_base;
366 	/* Read Pointer of RxPd queue */
367 	__le32 rxpd_rdptr;
368 
369 	/* Write Pointer of RxPd queue */
370 	__le32 rxpd_wrptr;
371 
372 	/*FW/HW capability */
373 	__le32 fwcapinfo;
374 } __packed;
375 
376 struct cmd_ds_mac_control {
377 	struct cmd_header hdr;
378 	__le16 action;
379 	u16 reserved;
380 };
381 
382 struct cmd_ds_802_11_mac_address {
383 	struct cmd_header hdr;
384 
385 	__le16 action;
386 	uint8_t macadd[ETH_ALEN];
387 };
388 
389 struct cmd_ds_mac_multicast_addr {
390 	struct cmd_header hdr;
391 
392 	__le16 action;
393 	__le16 nr_of_adrs;
394 	u8 maclist[ETH_ALEN * MRVDRV_MAX_MULTICAST_LIST_SIZE];
395 };
396 
397 struct cmd_ds_set_mode {
398 	struct cmd_header hdr;
399 
400 	__le16 mode;
401 };
402 
403 struct cmd_ds_set_bssid {
404 	struct cmd_header hdr;
405 
406 	u8 bssid[6];
407 	u8 activate;
408 };
409 
410 struct cmd_ds_802_11_radio_control {
411 	struct cmd_header hdr;
412 
413 	__le16 action;
414 	__le16 control;
415 };
416 
417 
418 struct cmd_ds_802_11_rf_channel {
419 	struct cmd_header hdr;
420 
421 	__le16 action;
422 	__le16 channel;
423 	__le16 rftype;      /* unused */
424 	__le16 reserved;    /* unused */
425 	u8 channellist[32]; /* unused */
426 };
427 
428 struct cmd_ds_set_boot2_ver {
429 	struct cmd_header hdr;
430 
431 	__le16 action;
432 	__le16 version;
433 };
434 
435 struct cmd_ds_802_11_reset {
436 	struct cmd_header hdr;
437 
438 	__le16 action;
439 };
440 
441 struct cmd_ds_802_11_beacon_control {
442 	struct cmd_header hdr;
443 
444 	__le16 action;
445 	__le16 beacon_enable;
446 	__le16 beacon_period;
447 };
448 
449 struct cmd_ds_802_11_beacon_set {
450 	struct cmd_header hdr;
451 
452 	__le16 len;
453 	u8 beacon[MRVL_MAX_BCN_SIZE];
454 };
455 
456 struct cmd_ctrl_node;
457 
458 /** Function Prototype Declaration */
459 void lbtf_set_mac_control(struct lbtf_private *priv);
460 
461 int lbtf_free_cmd_buffer(struct lbtf_private *priv);
462 
463 int lbtf_allocate_cmd_buffer(struct lbtf_private *priv);
464 int lbtf_execute_next_command(struct lbtf_private *priv);
465 int lbtf_set_radio_control(struct lbtf_private *priv);
466 int lbtf_update_hw_spec(struct lbtf_private *priv);
467 int lbtf_cmd_set_mac_multicast_addr(struct lbtf_private *priv);
468 void lbtf_set_mode(struct lbtf_private *priv, enum lbtf_mode mode);
469 void lbtf_set_bssid(struct lbtf_private *priv, bool activate, const u8 *bssid);
470 int lbtf_set_mac_address(struct lbtf_private *priv, uint8_t *mac_addr);
471 
472 int lbtf_set_channel(struct lbtf_private *priv, u8 channel);
473 
474 int lbtf_beacon_set(struct lbtf_private *priv, struct sk_buff *beacon);
475 int lbtf_beacon_ctrl(struct lbtf_private *priv, bool beacon_enable,
476 		     int beacon_int);
477 
478 
479 int lbtf_process_rx_command(struct lbtf_private *priv);
480 void lbtf_complete_command(struct lbtf_private *priv, struct cmd_ctrl_node *cmd,
481 			  int result);
482 void lbtf_cmd_response_rx(struct lbtf_private *priv);
483 
484 /* main.c */
485 struct chan_freq_power *lbtf_get_region_cfp_table(u8 region,
486 	int *cfp_no);
487 struct lbtf_private *lbtf_add_card(void *card, struct device *dmdev,
488 				   const struct lbtf_ops *ops);
489 int lbtf_remove_card(struct lbtf_private *priv);
490 int lbtf_start_card(struct lbtf_private *priv);
491 int lbtf_rx(struct lbtf_private *priv, struct sk_buff *skb);
492 void lbtf_send_tx_feedback(struct lbtf_private *priv, u8 retrycnt, u8 fail);
493 void lbtf_bcn_sent(struct lbtf_private *priv);
494 
495 /* support functions for cmd.c */
496 /* lbtf_cmd() infers the size of the buffer to copy data back into, from
497    the size of the target of the pointer. Since the command to be sent
498    may often be smaller, that size is set in cmd->size by the caller.*/
499 #define lbtf_cmd(priv, cmdnr, cmd, cb, cb_arg)	({		\
500 	uint16_t __sz = le16_to_cpu((cmd)->hdr.size);		\
501 	(cmd)->hdr.size = cpu_to_le16(sizeof(*(cmd)));		\
502 	__lbtf_cmd(priv, cmdnr, &(cmd)->hdr, __sz, cb, cb_arg);	\
503 })
504 
505 #define lbtf_cmd_with_response(priv, cmdnr, cmd)	\
506 	lbtf_cmd(priv, cmdnr, cmd, lbtf_cmd_copyback, (unsigned long) (cmd))
507 
508 void lbtf_cmd_async(struct lbtf_private *priv, uint16_t command,
509 	struct cmd_header *in_cmd, int in_cmd_size);
510 
511 int __lbtf_cmd(struct lbtf_private *priv, uint16_t command,
512 	      struct cmd_header *in_cmd, int in_cmd_size,
513 	      int (*callback)(struct lbtf_private *, unsigned long,
514 			      struct cmd_header *),
515 	      unsigned long callback_arg);
516 
517 int lbtf_cmd_copyback(struct lbtf_private *priv, unsigned long extra,
518 		     struct cmd_header *resp);
519