18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * IPWireless 3G PCMCIA Network Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Original code
68c2ecf20Sopenharmony_ci *   by Stephen Blackheath <stephen@blacksapphire.com>,
78c2ecf20Sopenharmony_ci *      Ben Martel <benm@symmetric.co.nz>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyrighted as follows:
108c2ecf20Sopenharmony_ci *   Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Various driver changes and rewrites, port to new kernels
138c2ecf20Sopenharmony_ci *   Copyright (C) 2006-2007 Jiri Kosina
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * Misc code cleanups and updates
168c2ecf20Sopenharmony_ci *   Copyright (C) 2007 David Sterba
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
208c2ecf20Sopenharmony_ci#include <linux/io.h>
218c2ecf20Sopenharmony_ci#include <linux/irq.h>
228c2ecf20Sopenharmony_ci#include <linux/kernel.h>
238c2ecf20Sopenharmony_ci#include <linux/list.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "hardware.h"
278c2ecf20Sopenharmony_ci#include "setup_protocol.h"
288c2ecf20Sopenharmony_ci#include "network.h"
298c2ecf20Sopenharmony_ci#include "main.h"
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic void ipw_send_setup_packet(struct ipw_hardware *hw);
328c2ecf20Sopenharmony_cistatic void handle_received_SETUP_packet(struct ipw_hardware *ipw,
338c2ecf20Sopenharmony_ci					 unsigned int address,
348c2ecf20Sopenharmony_ci					 const unsigned char *data, int len,
358c2ecf20Sopenharmony_ci					 int is_last);
368c2ecf20Sopenharmony_cistatic void ipwireless_setup_timer(struct timer_list *t);
378c2ecf20Sopenharmony_cistatic void handle_received_CTRL_packet(struct ipw_hardware *hw,
388c2ecf20Sopenharmony_ci		unsigned int channel_idx, const unsigned char *data, int len);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*#define TIMING_DIAGNOSTICS*/
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#ifdef TIMING_DIAGNOSTICS
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic struct timing_stats {
458c2ecf20Sopenharmony_ci	unsigned long last_report_time;
468c2ecf20Sopenharmony_ci	unsigned long read_time;
478c2ecf20Sopenharmony_ci	unsigned long write_time;
488c2ecf20Sopenharmony_ci	unsigned long read_bytes;
498c2ecf20Sopenharmony_ci	unsigned long write_bytes;
508c2ecf20Sopenharmony_ci	unsigned long start_time;
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic void start_timing(void)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	timing_stats.start_time = jiffies;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_cistatic void end_read_timing(unsigned length)
598c2ecf20Sopenharmony_ci{
608c2ecf20Sopenharmony_ci	timing_stats.read_time += (jiffies - start_time);
618c2ecf20Sopenharmony_ci	timing_stats.read_bytes += length + 2;
628c2ecf20Sopenharmony_ci	report_timing();
638c2ecf20Sopenharmony_ci}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic void end_write_timing(unsigned length)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	timing_stats.write_time += (jiffies - start_time);
688c2ecf20Sopenharmony_ci	timing_stats.write_bytes += length + 2;
698c2ecf20Sopenharmony_ci	report_timing();
708c2ecf20Sopenharmony_ci}
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic void report_timing(void)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	unsigned long since = jiffies - timing_stats.last_report_time;
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* If it's been more than one second... */
778c2ecf20Sopenharmony_ci	if (since >= HZ) {
788c2ecf20Sopenharmony_ci		int first = (timing_stats.last_report_time == 0);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci		timing_stats.last_report_time = jiffies;
818c2ecf20Sopenharmony_ci		if (!first)
828c2ecf20Sopenharmony_ci			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
838c2ecf20Sopenharmony_ci			       ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
848c2ecf20Sopenharmony_ci			       jiffies_to_usecs(since),
858c2ecf20Sopenharmony_ci			       timing_stats.read_bytes,
868c2ecf20Sopenharmony_ci			       jiffies_to_usecs(timing_stats.read_time),
878c2ecf20Sopenharmony_ci			       timing_stats.write_bytes,
888c2ecf20Sopenharmony_ci			       jiffies_to_usecs(timing_stats.write_time));
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci		timing_stats.read_time = 0;
918c2ecf20Sopenharmony_ci		timing_stats.write_time = 0;
928c2ecf20Sopenharmony_ci		timing_stats.read_bytes = 0;
938c2ecf20Sopenharmony_ci		timing_stats.write_bytes = 0;
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci#else
978c2ecf20Sopenharmony_cistatic void start_timing(void) { }
988c2ecf20Sopenharmony_cistatic void end_read_timing(unsigned length) { }
998c2ecf20Sopenharmony_cistatic void end_write_timing(unsigned length) { }
1008c2ecf20Sopenharmony_ci#endif
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci/* Imported IPW definitions */
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define LL_MTU_V1 318
1058c2ecf20Sopenharmony_ci#define LL_MTU_V2 250
1068c2ecf20Sopenharmony_ci#define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci#define PRIO_DATA  2
1098c2ecf20Sopenharmony_ci#define PRIO_CTRL  1
1108c2ecf20Sopenharmony_ci#define PRIO_SETUP 0
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* Addresses */
1138c2ecf20Sopenharmony_ci#define ADDR_SETUP_PROT 0
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* Protocol ids */
1168c2ecf20Sopenharmony_cienum {
1178c2ecf20Sopenharmony_ci	/* Identifier for the Com Data protocol */
1188c2ecf20Sopenharmony_ci	TL_PROTOCOLID_COM_DATA = 0,
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Identifier for the Com Control protocol */
1218c2ecf20Sopenharmony_ci	TL_PROTOCOLID_COM_CTRL = 1,
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	/* Identifier for the Setup protocol */
1248c2ecf20Sopenharmony_ci	TL_PROTOCOLID_SETUP = 2
1258c2ecf20Sopenharmony_ci};
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/* Number of bytes in NL packet header (cannot do
1288c2ecf20Sopenharmony_ci * sizeof(nl_packet_header) since it's a bitfield) */
1298c2ecf20Sopenharmony_ci#define NL_FIRST_PACKET_HEADER_SIZE        3
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* Number of bytes in NL packet header (cannot do
1328c2ecf20Sopenharmony_ci * sizeof(nl_packet_header) since it's a bitfield) */
1338c2ecf20Sopenharmony_ci#define NL_FOLLOWING_PACKET_HEADER_SIZE    1
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistruct nl_first_packet_header {
1368c2ecf20Sopenharmony_ci	unsigned char protocol:3;
1378c2ecf20Sopenharmony_ci	unsigned char address:3;
1388c2ecf20Sopenharmony_ci	unsigned char packet_rank:2;
1398c2ecf20Sopenharmony_ci	unsigned char length_lsb;
1408c2ecf20Sopenharmony_ci	unsigned char length_msb;
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistruct nl_packet_header {
1448c2ecf20Sopenharmony_ci	unsigned char protocol:3;
1458c2ecf20Sopenharmony_ci	unsigned char address:3;
1468c2ecf20Sopenharmony_ci	unsigned char packet_rank:2;
1478c2ecf20Sopenharmony_ci};
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/* Value of 'packet_rank' above */
1508c2ecf20Sopenharmony_ci#define NL_INTERMEDIATE_PACKET    0x0
1518c2ecf20Sopenharmony_ci#define NL_LAST_PACKET            0x1
1528c2ecf20Sopenharmony_ci#define NL_FIRST_PACKET           0x2
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ciunion nl_packet {
1558c2ecf20Sopenharmony_ci	/* Network packet header of the first packet (a special case) */
1568c2ecf20Sopenharmony_ci	struct nl_first_packet_header hdr_first;
1578c2ecf20Sopenharmony_ci	/* Network packet header of the following packets (if any) */
1588c2ecf20Sopenharmony_ci	struct nl_packet_header hdr;
1598c2ecf20Sopenharmony_ci	/* Complete network packet (header + data) */
1608c2ecf20Sopenharmony_ci	unsigned char rawpkt[LL_MTU_MAX];
1618c2ecf20Sopenharmony_ci} __attribute__ ((__packed__));
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#define HW_VERSION_UNKNOWN -1
1648c2ecf20Sopenharmony_ci#define HW_VERSION_1 1
1658c2ecf20Sopenharmony_ci#define HW_VERSION_2 2
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci/* IPW I/O ports */
1688c2ecf20Sopenharmony_ci#define IOIER 0x00		/* Interrupt Enable Register */
1698c2ecf20Sopenharmony_ci#define IOIR  0x02		/* Interrupt Source/ACK register */
1708c2ecf20Sopenharmony_ci#define IODCR 0x04		/* Data Control Register */
1718c2ecf20Sopenharmony_ci#define IODRR 0x06		/* Data Read Register */
1728c2ecf20Sopenharmony_ci#define IODWR 0x08		/* Data Write Register */
1738c2ecf20Sopenharmony_ci#define IOESR 0x0A		/* Embedded Driver Status Register */
1748c2ecf20Sopenharmony_ci#define IORXR 0x0C		/* Rx Fifo Register (Host to Embedded) */
1758c2ecf20Sopenharmony_ci#define IOTXR 0x0E		/* Tx Fifo Register (Embedded to Host) */
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/* I/O ports and bit definitions for version 1 of the hardware */
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* IER bits*/
1808c2ecf20Sopenharmony_ci#define IER_RXENABLED   0x1
1818c2ecf20Sopenharmony_ci#define IER_TXENABLED   0x2
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* ISR bits */
1848c2ecf20Sopenharmony_ci#define IR_RXINTR       0x1
1858c2ecf20Sopenharmony_ci#define IR_TXINTR       0x2
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/* DCR bits */
1888c2ecf20Sopenharmony_ci#define DCR_RXDONE      0x1
1898c2ecf20Sopenharmony_ci#define DCR_TXDONE      0x2
1908c2ecf20Sopenharmony_ci#define DCR_RXRESET     0x4
1918c2ecf20Sopenharmony_ci#define DCR_TXRESET     0x8
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/* I/O ports and bit definitions for version 2 of the hardware */
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistruct MEMCCR {
1968c2ecf20Sopenharmony_ci	unsigned short reg_config_option;	/* PCCOR: Configuration Option Register */
1978c2ecf20Sopenharmony_ci	unsigned short reg_config_and_status;	/* PCCSR: Configuration and Status Register */
1988c2ecf20Sopenharmony_ci	unsigned short reg_pin_replacement;	/* PCPRR: Pin Replacemant Register */
1998c2ecf20Sopenharmony_ci	unsigned short reg_socket_and_copy;	/* PCSCR: Socket and Copy Register */
2008c2ecf20Sopenharmony_ci	unsigned short reg_ext_status;		/* PCESR: Extendend Status Register */
2018c2ecf20Sopenharmony_ci	unsigned short reg_io_base;		/* PCIOB: I/O Base Register */
2028c2ecf20Sopenharmony_ci};
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistruct MEMINFREG {
2058c2ecf20Sopenharmony_ci	unsigned short memreg_tx_old;	/* TX Register (R/W) */
2068c2ecf20Sopenharmony_ci	unsigned short pad1;
2078c2ecf20Sopenharmony_ci	unsigned short memreg_rx_done;	/* RXDone Register (R/W) */
2088c2ecf20Sopenharmony_ci	unsigned short pad2;
2098c2ecf20Sopenharmony_ci	unsigned short memreg_rx;	/* RX Register (R/W) */
2108c2ecf20Sopenharmony_ci	unsigned short pad3;
2118c2ecf20Sopenharmony_ci	unsigned short memreg_pc_interrupt_ack;	/* PC intr Ack Register (W) */
2128c2ecf20Sopenharmony_ci	unsigned short pad4;
2138c2ecf20Sopenharmony_ci	unsigned long memreg_card_present;/* Mask for Host to check (R) for
2148c2ecf20Sopenharmony_ci					   * CARD_PRESENT_VALUE */
2158c2ecf20Sopenharmony_ci	unsigned short memreg_tx_new;	/* TX2 (new) Register (R/W) */
2168c2ecf20Sopenharmony_ci};
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci#define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci#define MEMTX_TX                       0x0001
2218c2ecf20Sopenharmony_ci#define MEMRX_RX                       0x0001
2228c2ecf20Sopenharmony_ci#define MEMRX_RX_DONE                  0x0001
2238c2ecf20Sopenharmony_ci#define MEMRX_PCINTACKK                0x0001
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci#define NL_NUM_OF_PRIORITIES       3
2268c2ecf20Sopenharmony_ci#define NL_NUM_OF_PROTOCOLS        3
2278c2ecf20Sopenharmony_ci#define NL_NUM_OF_ADDRESSES        NO_OF_IPW_CHANNELS
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cistruct ipw_hardware {
2308c2ecf20Sopenharmony_ci	unsigned int base_port;
2318c2ecf20Sopenharmony_ci	short hw_version;
2328c2ecf20Sopenharmony_ci	unsigned short ll_mtu;
2338c2ecf20Sopenharmony_ci	spinlock_t lock;
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	int initializing;
2368c2ecf20Sopenharmony_ci	int init_loops;
2378c2ecf20Sopenharmony_ci	struct timer_list setup_timer;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	/* Flag if hw is ready to send next packet */
2408c2ecf20Sopenharmony_ci	int tx_ready;
2418c2ecf20Sopenharmony_ci	/* Count of pending packets to be sent */
2428c2ecf20Sopenharmony_ci	int tx_queued;
2438c2ecf20Sopenharmony_ci	struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	int rx_bytes_queued;
2468c2ecf20Sopenharmony_ci	struct list_head rx_queue;
2478c2ecf20Sopenharmony_ci	/* Pool of rx_packet structures that are not currently used. */
2488c2ecf20Sopenharmony_ci	struct list_head rx_pool;
2498c2ecf20Sopenharmony_ci	int rx_pool_size;
2508c2ecf20Sopenharmony_ci	/* True if reception of data is blocked while userspace processes it. */
2518c2ecf20Sopenharmony_ci	int blocking_rx;
2528c2ecf20Sopenharmony_ci	/* True if there is RX data ready on the hardware. */
2538c2ecf20Sopenharmony_ci	int rx_ready;
2548c2ecf20Sopenharmony_ci	unsigned short last_memtx_serial;
2558c2ecf20Sopenharmony_ci	/*
2568c2ecf20Sopenharmony_ci	 * Newer versions of the V2 card firmware send serial numbers in the
2578c2ecf20Sopenharmony_ci	 * MemTX register. 'serial_number_detected' is set true when we detect
2588c2ecf20Sopenharmony_ci	 * a non-zero serial number (indicating the new firmware).  Thereafter,
2598c2ecf20Sopenharmony_ci	 * the driver can safely ignore the Timer Recovery re-sends to avoid
2608c2ecf20Sopenharmony_ci	 * out-of-sync problems.
2618c2ecf20Sopenharmony_ci	 */
2628c2ecf20Sopenharmony_ci	int serial_number_detected;
2638c2ecf20Sopenharmony_ci	struct work_struct work_rx;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	/* True if we are to send the set-up data to the hardware. */
2668c2ecf20Sopenharmony_ci	int to_setup;
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	/* Card has been removed */
2698c2ecf20Sopenharmony_ci	int removed;
2708c2ecf20Sopenharmony_ci	/* Saved irq value when we disable the interrupt. */
2718c2ecf20Sopenharmony_ci	int irq;
2728c2ecf20Sopenharmony_ci	/* True if this driver is shutting down. */
2738c2ecf20Sopenharmony_ci	int shutting_down;
2748c2ecf20Sopenharmony_ci	/* Modem control lines */
2758c2ecf20Sopenharmony_ci	unsigned int control_lines[NL_NUM_OF_ADDRESSES];
2768c2ecf20Sopenharmony_ci	struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	struct tasklet_struct tasklet;
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	/* The handle for the network layer, for the sending of events to it. */
2818c2ecf20Sopenharmony_ci	struct ipw_network *network;
2828c2ecf20Sopenharmony_ci	struct MEMINFREG __iomem *memory_info_regs;
2838c2ecf20Sopenharmony_ci	struct MEMCCR __iomem *memregs_CCR;
2848c2ecf20Sopenharmony_ci	void (*reboot_callback) (void *data);
2858c2ecf20Sopenharmony_ci	void *reboot_callback_data;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	unsigned short __iomem *memreg_tx;
2888c2ecf20Sopenharmony_ci};
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci/*
2918c2ecf20Sopenharmony_ci * Packet info structure for tx packets.
2928c2ecf20Sopenharmony_ci * Note: not all the fields defined here are required for all protocols
2938c2ecf20Sopenharmony_ci */
2948c2ecf20Sopenharmony_cistruct ipw_tx_packet {
2958c2ecf20Sopenharmony_ci	struct list_head queue;
2968c2ecf20Sopenharmony_ci	/* channel idx + 1 */
2978c2ecf20Sopenharmony_ci	unsigned char dest_addr;
2988c2ecf20Sopenharmony_ci	/* SETUP, CTRL or DATA */
2998c2ecf20Sopenharmony_ci	unsigned char protocol;
3008c2ecf20Sopenharmony_ci	/* Length of data block, which starts at the end of this structure */
3018c2ecf20Sopenharmony_ci	unsigned short length;
3028c2ecf20Sopenharmony_ci	/* Sending state */
3038c2ecf20Sopenharmony_ci	/* Offset of where we've sent up to so far */
3048c2ecf20Sopenharmony_ci	unsigned long offset;
3058c2ecf20Sopenharmony_ci	/* Count of packet fragments, starting at 0 */
3068c2ecf20Sopenharmony_ci	int fragment_count;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	/* Called after packet is sent and before is freed */
3098c2ecf20Sopenharmony_ci	void (*packet_callback) (void *cb_data, unsigned int packet_length);
3108c2ecf20Sopenharmony_ci	void *callback_data;
3118c2ecf20Sopenharmony_ci};
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci/* Signals from DTE */
3148c2ecf20Sopenharmony_ci#define COMCTRL_RTS	0
3158c2ecf20Sopenharmony_ci#define COMCTRL_DTR	1
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci/* Signals from DCE */
3188c2ecf20Sopenharmony_ci#define COMCTRL_CTS	2
3198c2ecf20Sopenharmony_ci#define COMCTRL_DCD	3
3208c2ecf20Sopenharmony_ci#define COMCTRL_DSR	4
3218c2ecf20Sopenharmony_ci#define COMCTRL_RI	5
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_cistruct ipw_control_packet_body {
3248c2ecf20Sopenharmony_ci	/* DTE signal or DCE signal */
3258c2ecf20Sopenharmony_ci	unsigned char sig_no;
3268c2ecf20Sopenharmony_ci	/* 0: set signal, 1: clear signal */
3278c2ecf20Sopenharmony_ci	unsigned char value;
3288c2ecf20Sopenharmony_ci} __attribute__ ((__packed__));
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistruct ipw_control_packet {
3318c2ecf20Sopenharmony_ci	struct ipw_tx_packet header;
3328c2ecf20Sopenharmony_ci	struct ipw_control_packet_body body;
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistruct ipw_rx_packet {
3368c2ecf20Sopenharmony_ci	struct list_head queue;
3378c2ecf20Sopenharmony_ci	unsigned int capacity;
3388c2ecf20Sopenharmony_ci	unsigned int length;
3398c2ecf20Sopenharmony_ci	unsigned int protocol;
3408c2ecf20Sopenharmony_ci	unsigned int channel_idx;
3418c2ecf20Sopenharmony_ci};
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic char *data_type(const unsigned char *buf, unsigned length)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	if (length == 0)
3488c2ecf20Sopenharmony_ci		return "     ";
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	if (hdr->packet_rank & NL_FIRST_PACKET) {
3518c2ecf20Sopenharmony_ci		switch (hdr->protocol) {
3528c2ecf20Sopenharmony_ci		case TL_PROTOCOLID_COM_DATA:	return "DATA ";
3538c2ecf20Sopenharmony_ci		case TL_PROTOCOLID_COM_CTRL:	return "CTRL ";
3548c2ecf20Sopenharmony_ci		case TL_PROTOCOLID_SETUP:	return "SETUP";
3558c2ecf20Sopenharmony_ci		default: return "???? ";
3568c2ecf20Sopenharmony_ci		}
3578c2ecf20Sopenharmony_ci	} else
3588c2ecf20Sopenharmony_ci		return "     ";
3598c2ecf20Sopenharmony_ci}
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci#define DUMP_MAX_BYTES 64
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic void dump_data_bytes(const char *type, const unsigned char *data,
3648c2ecf20Sopenharmony_ci			    unsigned length)
3658c2ecf20Sopenharmony_ci{
3668c2ecf20Sopenharmony_ci	char prefix[56];
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
3698c2ecf20Sopenharmony_ci			type, data_type(data, length));
3708c2ecf20Sopenharmony_ci	print_hex_dump_bytes(prefix, 0, (void *)data,
3718c2ecf20Sopenharmony_ci			length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
3728c2ecf20Sopenharmony_ci}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_cistatic void swap_packet_bitfield_to_le(unsigned char *data)
3758c2ecf20Sopenharmony_ci{
3768c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN_BITFIELD
3778c2ecf20Sopenharmony_ci	unsigned char tmp = *data, ret = 0;
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/*
3808c2ecf20Sopenharmony_ci	 * transform bits from aa.bbb.ccc to ccc.bbb.aa
3818c2ecf20Sopenharmony_ci	 */
3828c2ecf20Sopenharmony_ci	ret |= (tmp & 0xc0) >> 6;
3838c2ecf20Sopenharmony_ci	ret |= (tmp & 0x38) >> 1;
3848c2ecf20Sopenharmony_ci	ret |= (tmp & 0x07) << 5;
3858c2ecf20Sopenharmony_ci	*data = ret & 0xff;
3868c2ecf20Sopenharmony_ci#endif
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic void swap_packet_bitfield_from_le(unsigned char *data)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci#ifdef __BIG_ENDIAN_BITFIELD
3928c2ecf20Sopenharmony_ci	unsigned char tmp = *data, ret = 0;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci	/*
3958c2ecf20Sopenharmony_ci	 * transform bits from ccc.bbb.aa to aa.bbb.ccc
3968c2ecf20Sopenharmony_ci	 */
3978c2ecf20Sopenharmony_ci	ret |= (tmp & 0xe0) >> 5;
3988c2ecf20Sopenharmony_ci	ret |= (tmp & 0x1c) << 1;
3998c2ecf20Sopenharmony_ci	ret |= (tmp & 0x03) << 6;
4008c2ecf20Sopenharmony_ci	*data = ret & 0xff;
4018c2ecf20Sopenharmony_ci#endif
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic void do_send_fragment(struct ipw_hardware *hw, unsigned char *data,
4058c2ecf20Sopenharmony_ci			    unsigned length)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	unsigned i;
4088c2ecf20Sopenharmony_ci	unsigned long flags;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	start_timing();
4118c2ecf20Sopenharmony_ci	BUG_ON(length > hw->ll_mtu);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	if (ipwireless_debug)
4148c2ecf20Sopenharmony_ci		dump_data_bytes("send", data, length);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	hw->tx_ready = 0;
4198c2ecf20Sopenharmony_ci	swap_packet_bitfield_to_le(data);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	if (hw->hw_version == HW_VERSION_1) {
4228c2ecf20Sopenharmony_ci		outw((unsigned short) length, hw->base_port + IODWR);
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci		for (i = 0; i < length; i += 2) {
4258c2ecf20Sopenharmony_ci			unsigned short d = data[i];
4268c2ecf20Sopenharmony_ci			__le16 raw_data;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci			if (i + 1 < length)
4298c2ecf20Sopenharmony_ci				d |= data[i + 1] << 8;
4308c2ecf20Sopenharmony_ci			raw_data = cpu_to_le16(d);
4318c2ecf20Sopenharmony_ci			outw(raw_data, hw->base_port + IODWR);
4328c2ecf20Sopenharmony_ci		}
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci		outw(DCR_TXDONE, hw->base_port + IODCR);
4358c2ecf20Sopenharmony_ci	} else if (hw->hw_version == HW_VERSION_2) {
4368c2ecf20Sopenharmony_ci		outw((unsigned short) length, hw->base_port);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci		for (i = 0; i < length; i += 2) {
4398c2ecf20Sopenharmony_ci			unsigned short d = data[i];
4408c2ecf20Sopenharmony_ci			__le16 raw_data;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci			if (i + 1 < length)
4438c2ecf20Sopenharmony_ci				d |= data[i + 1] << 8;
4448c2ecf20Sopenharmony_ci			raw_data = cpu_to_le16(d);
4458c2ecf20Sopenharmony_ci			outw(raw_data, hw->base_port);
4468c2ecf20Sopenharmony_ci		}
4478c2ecf20Sopenharmony_ci		while ((i & 3) != 2) {
4488c2ecf20Sopenharmony_ci			outw((unsigned short) 0xDEAD, hw->base_port);
4498c2ecf20Sopenharmony_ci			i += 2;
4508c2ecf20Sopenharmony_ci		}
4518c2ecf20Sopenharmony_ci		writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hw->lock, flags);
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	end_write_timing(length);
4578c2ecf20Sopenharmony_ci}
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_cistatic void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
4608c2ecf20Sopenharmony_ci{
4618c2ecf20Sopenharmony_ci	unsigned short fragment_data_len;
4628c2ecf20Sopenharmony_ci	unsigned short data_left = packet->length - packet->offset;
4638c2ecf20Sopenharmony_ci	unsigned short header_size;
4648c2ecf20Sopenharmony_ci	union nl_packet pkt;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	header_size =
4678c2ecf20Sopenharmony_ci	    (packet->fragment_count == 0)
4688c2ecf20Sopenharmony_ci	    ? NL_FIRST_PACKET_HEADER_SIZE
4698c2ecf20Sopenharmony_ci	    : NL_FOLLOWING_PACKET_HEADER_SIZE;
4708c2ecf20Sopenharmony_ci	fragment_data_len = hw->ll_mtu - header_size;
4718c2ecf20Sopenharmony_ci	if (data_left < fragment_data_len)
4728c2ecf20Sopenharmony_ci		fragment_data_len = data_left;
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	/*
4758c2ecf20Sopenharmony_ci	 * hdr_first is now in machine bitfield order, which will be swapped
4768c2ecf20Sopenharmony_ci	 * to le just before it goes to hw
4778c2ecf20Sopenharmony_ci	 */
4788c2ecf20Sopenharmony_ci	pkt.hdr_first.protocol = packet->protocol;
4798c2ecf20Sopenharmony_ci	pkt.hdr_first.address = packet->dest_addr;
4808c2ecf20Sopenharmony_ci	pkt.hdr_first.packet_rank = 0;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	/* First packet? */
4838c2ecf20Sopenharmony_ci	if (packet->fragment_count == 0) {
4848c2ecf20Sopenharmony_ci		pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
4858c2ecf20Sopenharmony_ci		pkt.hdr_first.length_lsb = (unsigned char) packet->length;
4868c2ecf20Sopenharmony_ci		pkt.hdr_first.length_msb =
4878c2ecf20Sopenharmony_ci			(unsigned char) (packet->length >> 8);
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	memcpy(pkt.rawpkt + header_size,
4918c2ecf20Sopenharmony_ci	       ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
4928c2ecf20Sopenharmony_ci	       packet->offset, fragment_data_len);
4938c2ecf20Sopenharmony_ci	packet->offset += fragment_data_len;
4948c2ecf20Sopenharmony_ci	packet->fragment_count++;
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	/* Last packet? (May also be first packet.) */
4978c2ecf20Sopenharmony_ci	if (packet->offset == packet->length)
4988c2ecf20Sopenharmony_ci		pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
4998c2ecf20Sopenharmony_ci	do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* If this packet has unsent data, then re-queue it. */
5028c2ecf20Sopenharmony_ci	if (packet->offset < packet->length) {
5038c2ecf20Sopenharmony_ci		/*
5048c2ecf20Sopenharmony_ci		 * Re-queue it at the head of the highest priority queue so
5058c2ecf20Sopenharmony_ci		 * it goes before all other packets
5068c2ecf20Sopenharmony_ci		 */
5078c2ecf20Sopenharmony_ci		unsigned long flags;
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hw->lock, flags);
5108c2ecf20Sopenharmony_ci		list_add(&packet->queue, &hw->tx_queue[0]);
5118c2ecf20Sopenharmony_ci		hw->tx_queued++;
5128c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
5138c2ecf20Sopenharmony_ci	} else {
5148c2ecf20Sopenharmony_ci		if (packet->packet_callback)
5158c2ecf20Sopenharmony_ci			packet->packet_callback(packet->callback_data,
5168c2ecf20Sopenharmony_ci					packet->length);
5178c2ecf20Sopenharmony_ci		kfree(packet);
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistatic void ipw_setup_hardware(struct ipw_hardware *hw)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	unsigned long flags;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
5268c2ecf20Sopenharmony_ci	if (hw->hw_version == HW_VERSION_1) {
5278c2ecf20Sopenharmony_ci		/* Reset RX FIFO */
5288c2ecf20Sopenharmony_ci		outw(DCR_RXRESET, hw->base_port + IODCR);
5298c2ecf20Sopenharmony_ci		/* SB: Reset TX FIFO */
5308c2ecf20Sopenharmony_ci		outw(DCR_TXRESET, hw->base_port + IODCR);
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci		/* Enable TX and RX interrupts. */
5338c2ecf20Sopenharmony_ci		outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
5348c2ecf20Sopenharmony_ci	} else {
5358c2ecf20Sopenharmony_ci		/*
5368c2ecf20Sopenharmony_ci		 * Set INTRACK bit (bit 0), which means we must explicitly
5378c2ecf20Sopenharmony_ci		 * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
5388c2ecf20Sopenharmony_ci		 */
5398c2ecf20Sopenharmony_ci		unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci		csr |= 1;
5428c2ecf20Sopenharmony_ci		writew(csr, &hw->memregs_CCR->reg_config_and_status);
5438c2ecf20Sopenharmony_ci	}
5448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hw->lock, flags);
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci/*
5488c2ecf20Sopenharmony_ci * If 'packet' is NULL, then this function allocates a new packet, setting its
5498c2ecf20Sopenharmony_ci * length to 0 and ensuring it has the specified minimum amount of free space.
5508c2ecf20Sopenharmony_ci *
5518c2ecf20Sopenharmony_ci * If 'packet' is not NULL, then this function enlarges it if it doesn't
5528c2ecf20Sopenharmony_ci * have the specified minimum amount of free space.
5538c2ecf20Sopenharmony_ci *
5548c2ecf20Sopenharmony_ci */
5558c2ecf20Sopenharmony_cistatic struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
5568c2ecf20Sopenharmony_ci					   struct ipw_rx_packet *packet,
5578c2ecf20Sopenharmony_ci					   int minimum_free_space)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if (!packet) {
5618c2ecf20Sopenharmony_ci		unsigned long flags;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hw->lock, flags);
5648c2ecf20Sopenharmony_ci		if (!list_empty(&hw->rx_pool)) {
5658c2ecf20Sopenharmony_ci			packet = list_first_entry(&hw->rx_pool,
5668c2ecf20Sopenharmony_ci					struct ipw_rx_packet, queue);
5678c2ecf20Sopenharmony_ci			hw->rx_pool_size--;
5688c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hw->lock, flags);
5698c2ecf20Sopenharmony_ci			list_del(&packet->queue);
5708c2ecf20Sopenharmony_ci		} else {
5718c2ecf20Sopenharmony_ci			const int min_capacity =
5728c2ecf20Sopenharmony_ci				ipwireless_ppp_mru(hw->network) + 2;
5738c2ecf20Sopenharmony_ci			int new_capacity;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hw->lock, flags);
5768c2ecf20Sopenharmony_ci			new_capacity =
5778c2ecf20Sopenharmony_ci				(minimum_free_space > min_capacity
5788c2ecf20Sopenharmony_ci				 ? minimum_free_space
5798c2ecf20Sopenharmony_ci				 : min_capacity);
5808c2ecf20Sopenharmony_ci			packet = kmalloc(sizeof(struct ipw_rx_packet)
5818c2ecf20Sopenharmony_ci					+ new_capacity, GFP_ATOMIC);
5828c2ecf20Sopenharmony_ci			if (!packet)
5838c2ecf20Sopenharmony_ci				return NULL;
5848c2ecf20Sopenharmony_ci			packet->capacity = new_capacity;
5858c2ecf20Sopenharmony_ci		}
5868c2ecf20Sopenharmony_ci		packet->length = 0;
5878c2ecf20Sopenharmony_ci	}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	if (packet->length + minimum_free_space > packet->capacity) {
5908c2ecf20Sopenharmony_ci		struct ipw_rx_packet *old_packet = packet;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci		packet = kmalloc(sizeof(struct ipw_rx_packet) +
5938c2ecf20Sopenharmony_ci				old_packet->length + minimum_free_space,
5948c2ecf20Sopenharmony_ci				GFP_ATOMIC);
5958c2ecf20Sopenharmony_ci		if (!packet) {
5968c2ecf20Sopenharmony_ci			kfree(old_packet);
5978c2ecf20Sopenharmony_ci			return NULL;
5988c2ecf20Sopenharmony_ci		}
5998c2ecf20Sopenharmony_ci		memcpy(packet, old_packet,
6008c2ecf20Sopenharmony_ci				sizeof(struct ipw_rx_packet)
6018c2ecf20Sopenharmony_ci					+ old_packet->length);
6028c2ecf20Sopenharmony_ci		packet->capacity = old_packet->length + minimum_free_space;
6038c2ecf20Sopenharmony_ci		kfree(old_packet);
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	return packet;
6078c2ecf20Sopenharmony_ci}
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_cistatic void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
6108c2ecf20Sopenharmony_ci{
6118c2ecf20Sopenharmony_ci	if (hw->rx_pool_size > 6)
6128c2ecf20Sopenharmony_ci		kfree(packet);
6138c2ecf20Sopenharmony_ci	else {
6148c2ecf20Sopenharmony_ci		hw->rx_pool_size++;
6158c2ecf20Sopenharmony_ci		list_add(&packet->queue, &hw->rx_pool);
6168c2ecf20Sopenharmony_ci	}
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_cistatic void queue_received_packet(struct ipw_hardware *hw,
6208c2ecf20Sopenharmony_ci				  unsigned int protocol,
6218c2ecf20Sopenharmony_ci				  unsigned int address,
6228c2ecf20Sopenharmony_ci				  const unsigned char *data, int length,
6238c2ecf20Sopenharmony_ci				  int is_last)
6248c2ecf20Sopenharmony_ci{
6258c2ecf20Sopenharmony_ci	unsigned int channel_idx = address - 1;
6268c2ecf20Sopenharmony_ci	struct ipw_rx_packet *packet = NULL;
6278c2ecf20Sopenharmony_ci	unsigned long flags;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	/* Discard packet if channel index is out of range. */
6308c2ecf20Sopenharmony_ci	if (channel_idx >= NL_NUM_OF_ADDRESSES) {
6318c2ecf20Sopenharmony_ci		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
6328c2ecf20Sopenharmony_ci		       ": data packet has bad address %u\n", address);
6338c2ecf20Sopenharmony_ci		return;
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	/*
6378c2ecf20Sopenharmony_ci	 * ->packet_assembler is safe to touch unlocked, this is the only place
6388c2ecf20Sopenharmony_ci	 */
6398c2ecf20Sopenharmony_ci	if (protocol == TL_PROTOCOLID_COM_DATA) {
6408c2ecf20Sopenharmony_ci		struct ipw_rx_packet **assem =
6418c2ecf20Sopenharmony_ci			&hw->packet_assembler[channel_idx];
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci		/*
6448c2ecf20Sopenharmony_ci		 * Create a new packet, or assembler already contains one
6458c2ecf20Sopenharmony_ci		 * enlarge it by 'length' bytes.
6468c2ecf20Sopenharmony_ci		 */
6478c2ecf20Sopenharmony_ci		(*assem) = pool_allocate(hw, *assem, length);
6488c2ecf20Sopenharmony_ci		if (!(*assem)) {
6498c2ecf20Sopenharmony_ci			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
6508c2ecf20Sopenharmony_ci				": no memory for incoming data packet, dropped!\n");
6518c2ecf20Sopenharmony_ci			return;
6528c2ecf20Sopenharmony_ci		}
6538c2ecf20Sopenharmony_ci		(*assem)->protocol = protocol;
6548c2ecf20Sopenharmony_ci		(*assem)->channel_idx = channel_idx;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci		/* Append this packet data onto existing data. */
6578c2ecf20Sopenharmony_ci		memcpy((unsigned char *)(*assem) +
6588c2ecf20Sopenharmony_ci			       sizeof(struct ipw_rx_packet)
6598c2ecf20Sopenharmony_ci				+ (*assem)->length, data, length);
6608c2ecf20Sopenharmony_ci		(*assem)->length += length;
6618c2ecf20Sopenharmony_ci		if (is_last) {
6628c2ecf20Sopenharmony_ci			packet = *assem;
6638c2ecf20Sopenharmony_ci			*assem = NULL;
6648c2ecf20Sopenharmony_ci			/* Count queued DATA bytes only */
6658c2ecf20Sopenharmony_ci			spin_lock_irqsave(&hw->lock, flags);
6668c2ecf20Sopenharmony_ci			hw->rx_bytes_queued += packet->length;
6678c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hw->lock, flags);
6688c2ecf20Sopenharmony_ci		}
6698c2ecf20Sopenharmony_ci	} else {
6708c2ecf20Sopenharmony_ci		/* If it's a CTRL packet, don't assemble, just queue it. */
6718c2ecf20Sopenharmony_ci		packet = pool_allocate(hw, NULL, length);
6728c2ecf20Sopenharmony_ci		if (!packet) {
6738c2ecf20Sopenharmony_ci			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
6748c2ecf20Sopenharmony_ci				": no memory for incoming ctrl packet, dropped!\n");
6758c2ecf20Sopenharmony_ci			return;
6768c2ecf20Sopenharmony_ci		}
6778c2ecf20Sopenharmony_ci		packet->protocol = protocol;
6788c2ecf20Sopenharmony_ci		packet->channel_idx = channel_idx;
6798c2ecf20Sopenharmony_ci		memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
6808c2ecf20Sopenharmony_ci				data, length);
6818c2ecf20Sopenharmony_ci		packet->length = length;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	/*
6858c2ecf20Sopenharmony_ci	 * If this is the last packet, then send the assembled packet on to the
6868c2ecf20Sopenharmony_ci	 * network layer.
6878c2ecf20Sopenharmony_ci	 */
6888c2ecf20Sopenharmony_ci	if (packet) {
6898c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hw->lock, flags);
6908c2ecf20Sopenharmony_ci		list_add_tail(&packet->queue, &hw->rx_queue);
6918c2ecf20Sopenharmony_ci		/* Block reception of incoming packets if queue is full. */
6928c2ecf20Sopenharmony_ci		hw->blocking_rx =
6938c2ecf20Sopenharmony_ci			(hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
6968c2ecf20Sopenharmony_ci		schedule_work(&hw->work_rx);
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci/*
7018c2ecf20Sopenharmony_ci * Workqueue callback
7028c2ecf20Sopenharmony_ci */
7038c2ecf20Sopenharmony_cistatic void ipw_receive_data_work(struct work_struct *work_rx)
7048c2ecf20Sopenharmony_ci{
7058c2ecf20Sopenharmony_ci	struct ipw_hardware *hw =
7068c2ecf20Sopenharmony_ci	    container_of(work_rx, struct ipw_hardware, work_rx);
7078c2ecf20Sopenharmony_ci	unsigned long flags;
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
7108c2ecf20Sopenharmony_ci	while (!list_empty(&hw->rx_queue)) {
7118c2ecf20Sopenharmony_ci		struct ipw_rx_packet *packet =
7128c2ecf20Sopenharmony_ci			list_first_entry(&hw->rx_queue,
7138c2ecf20Sopenharmony_ci					struct ipw_rx_packet, queue);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci		if (hw->shutting_down)
7168c2ecf20Sopenharmony_ci			break;
7178c2ecf20Sopenharmony_ci		list_del(&packet->queue);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci		/*
7208c2ecf20Sopenharmony_ci		 * Note: ipwireless_network_packet_received must be called in a
7218c2ecf20Sopenharmony_ci		 * process context (i.e. via schedule_work) because the tty
7228c2ecf20Sopenharmony_ci		 * output code can sleep in the tty_flip_buffer_push call.
7238c2ecf20Sopenharmony_ci		 */
7248c2ecf20Sopenharmony_ci		if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
7258c2ecf20Sopenharmony_ci			if (hw->network != NULL) {
7268c2ecf20Sopenharmony_ci				/* If the network hasn't been disconnected. */
7278c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&hw->lock, flags);
7288c2ecf20Sopenharmony_ci				/*
7298c2ecf20Sopenharmony_ci				 * This must run unlocked due to tty processing
7308c2ecf20Sopenharmony_ci				 * and mutex locking
7318c2ecf20Sopenharmony_ci				 */
7328c2ecf20Sopenharmony_ci				ipwireless_network_packet_received(
7338c2ecf20Sopenharmony_ci						hw->network,
7348c2ecf20Sopenharmony_ci						packet->channel_idx,
7358c2ecf20Sopenharmony_ci						(unsigned char *)packet
7368c2ecf20Sopenharmony_ci						+ sizeof(struct ipw_rx_packet),
7378c2ecf20Sopenharmony_ci						packet->length);
7388c2ecf20Sopenharmony_ci				spin_lock_irqsave(&hw->lock, flags);
7398c2ecf20Sopenharmony_ci			}
7408c2ecf20Sopenharmony_ci			/* Count queued DATA bytes only */
7418c2ecf20Sopenharmony_ci			hw->rx_bytes_queued -= packet->length;
7428c2ecf20Sopenharmony_ci		} else {
7438c2ecf20Sopenharmony_ci			/*
7448c2ecf20Sopenharmony_ci			 * This is safe to be called locked, callchain does
7458c2ecf20Sopenharmony_ci			 * not block
7468c2ecf20Sopenharmony_ci			 */
7478c2ecf20Sopenharmony_ci			handle_received_CTRL_packet(hw, packet->channel_idx,
7488c2ecf20Sopenharmony_ci					(unsigned char *)packet
7498c2ecf20Sopenharmony_ci					+ sizeof(struct ipw_rx_packet),
7508c2ecf20Sopenharmony_ci					packet->length);
7518c2ecf20Sopenharmony_ci		}
7528c2ecf20Sopenharmony_ci		pool_free(hw, packet);
7538c2ecf20Sopenharmony_ci		/*
7548c2ecf20Sopenharmony_ci		 * Unblock reception of incoming packets if queue is no longer
7558c2ecf20Sopenharmony_ci		 * full.
7568c2ecf20Sopenharmony_ci		 */
7578c2ecf20Sopenharmony_ci		hw->blocking_rx =
7588c2ecf20Sopenharmony_ci			hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
7598c2ecf20Sopenharmony_ci		if (hw->shutting_down)
7608c2ecf20Sopenharmony_ci			break;
7618c2ecf20Sopenharmony_ci	}
7628c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hw->lock, flags);
7638c2ecf20Sopenharmony_ci}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_cistatic void handle_received_CTRL_packet(struct ipw_hardware *hw,
7668c2ecf20Sopenharmony_ci					unsigned int channel_idx,
7678c2ecf20Sopenharmony_ci					const unsigned char *data, int len)
7688c2ecf20Sopenharmony_ci{
7698c2ecf20Sopenharmony_ci	const struct ipw_control_packet_body *body =
7708c2ecf20Sopenharmony_ci		(const struct ipw_control_packet_body *) data;
7718c2ecf20Sopenharmony_ci	unsigned int changed_mask;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci	if (len != sizeof(struct ipw_control_packet_body)) {
7748c2ecf20Sopenharmony_ci		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
7758c2ecf20Sopenharmony_ci		       ": control packet was %d bytes - wrong size!\n",
7768c2ecf20Sopenharmony_ci		       len);
7778c2ecf20Sopenharmony_ci		return;
7788c2ecf20Sopenharmony_ci	}
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	switch (body->sig_no) {
7818c2ecf20Sopenharmony_ci	case COMCTRL_CTS:
7828c2ecf20Sopenharmony_ci		changed_mask = IPW_CONTROL_LINE_CTS;
7838c2ecf20Sopenharmony_ci		break;
7848c2ecf20Sopenharmony_ci	case COMCTRL_DCD:
7858c2ecf20Sopenharmony_ci		changed_mask = IPW_CONTROL_LINE_DCD;
7868c2ecf20Sopenharmony_ci		break;
7878c2ecf20Sopenharmony_ci	case COMCTRL_DSR:
7888c2ecf20Sopenharmony_ci		changed_mask = IPW_CONTROL_LINE_DSR;
7898c2ecf20Sopenharmony_ci		break;
7908c2ecf20Sopenharmony_ci	case COMCTRL_RI:
7918c2ecf20Sopenharmony_ci		changed_mask = IPW_CONTROL_LINE_RI;
7928c2ecf20Sopenharmony_ci		break;
7938c2ecf20Sopenharmony_ci	default:
7948c2ecf20Sopenharmony_ci		changed_mask = 0;
7958c2ecf20Sopenharmony_ci	}
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	if (changed_mask != 0) {
7988c2ecf20Sopenharmony_ci		if (body->value)
7998c2ecf20Sopenharmony_ci			hw->control_lines[channel_idx] |= changed_mask;
8008c2ecf20Sopenharmony_ci		else
8018c2ecf20Sopenharmony_ci			hw->control_lines[channel_idx] &= ~changed_mask;
8028c2ecf20Sopenharmony_ci		if (hw->network)
8038c2ecf20Sopenharmony_ci			ipwireless_network_notify_control_line_change(
8048c2ecf20Sopenharmony_ci					hw->network,
8058c2ecf20Sopenharmony_ci					channel_idx,
8068c2ecf20Sopenharmony_ci					hw->control_lines[channel_idx],
8078c2ecf20Sopenharmony_ci					changed_mask);
8088c2ecf20Sopenharmony_ci	}
8098c2ecf20Sopenharmony_ci}
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cistatic void handle_received_packet(struct ipw_hardware *hw,
8128c2ecf20Sopenharmony_ci				   const union nl_packet *packet,
8138c2ecf20Sopenharmony_ci				   unsigned short len)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	unsigned int protocol = packet->hdr.protocol;
8168c2ecf20Sopenharmony_ci	unsigned int address = packet->hdr.address;
8178c2ecf20Sopenharmony_ci	unsigned int header_length;
8188c2ecf20Sopenharmony_ci	const unsigned char *data;
8198c2ecf20Sopenharmony_ci	unsigned int data_len;
8208c2ecf20Sopenharmony_ci	int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	if (packet->hdr.packet_rank & NL_FIRST_PACKET)
8238c2ecf20Sopenharmony_ci		header_length = NL_FIRST_PACKET_HEADER_SIZE;
8248c2ecf20Sopenharmony_ci	else
8258c2ecf20Sopenharmony_ci		header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci	data = packet->rawpkt + header_length;
8288c2ecf20Sopenharmony_ci	data_len = len - header_length;
8298c2ecf20Sopenharmony_ci	switch (protocol) {
8308c2ecf20Sopenharmony_ci	case TL_PROTOCOLID_COM_DATA:
8318c2ecf20Sopenharmony_ci	case TL_PROTOCOLID_COM_CTRL:
8328c2ecf20Sopenharmony_ci		queue_received_packet(hw, protocol, address, data, data_len,
8338c2ecf20Sopenharmony_ci				is_last);
8348c2ecf20Sopenharmony_ci		break;
8358c2ecf20Sopenharmony_ci	case TL_PROTOCOLID_SETUP:
8368c2ecf20Sopenharmony_ci		handle_received_SETUP_packet(hw, address, data, data_len,
8378c2ecf20Sopenharmony_ci				is_last);
8388c2ecf20Sopenharmony_ci		break;
8398c2ecf20Sopenharmony_ci	}
8408c2ecf20Sopenharmony_ci}
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_cistatic void acknowledge_data_read(struct ipw_hardware *hw)
8438c2ecf20Sopenharmony_ci{
8448c2ecf20Sopenharmony_ci	if (hw->hw_version == HW_VERSION_1)
8458c2ecf20Sopenharmony_ci		outw(DCR_RXDONE, hw->base_port + IODCR);
8468c2ecf20Sopenharmony_ci	else
8478c2ecf20Sopenharmony_ci		writew(MEMRX_PCINTACKK,
8488c2ecf20Sopenharmony_ci				&hw->memory_info_regs->memreg_pc_interrupt_ack);
8498c2ecf20Sopenharmony_ci}
8508c2ecf20Sopenharmony_ci
8518c2ecf20Sopenharmony_ci/*
8528c2ecf20Sopenharmony_ci * Retrieve a packet from the IPW hardware.
8538c2ecf20Sopenharmony_ci */
8548c2ecf20Sopenharmony_cistatic void do_receive_packet(struct ipw_hardware *hw)
8558c2ecf20Sopenharmony_ci{
8568c2ecf20Sopenharmony_ci	unsigned len;
8578c2ecf20Sopenharmony_ci	unsigned i;
8588c2ecf20Sopenharmony_ci	unsigned char pkt[LL_MTU_MAX];
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	start_timing();
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_ci	if (hw->hw_version == HW_VERSION_1) {
8638c2ecf20Sopenharmony_ci		len = inw(hw->base_port + IODRR);
8648c2ecf20Sopenharmony_ci		if (len > hw->ll_mtu) {
8658c2ecf20Sopenharmony_ci			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
8668c2ecf20Sopenharmony_ci			       ": received a packet of %u bytes - longer than the MTU!\n", len);
8678c2ecf20Sopenharmony_ci			outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
8688c2ecf20Sopenharmony_ci			return;
8698c2ecf20Sopenharmony_ci		}
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci		for (i = 0; i < len; i += 2) {
8728c2ecf20Sopenharmony_ci			__le16 raw_data = inw(hw->base_port + IODRR);
8738c2ecf20Sopenharmony_ci			unsigned short data = le16_to_cpu(raw_data);
8748c2ecf20Sopenharmony_ci
8758c2ecf20Sopenharmony_ci			pkt[i] = (unsigned char) data;
8768c2ecf20Sopenharmony_ci			pkt[i + 1] = (unsigned char) (data >> 8);
8778c2ecf20Sopenharmony_ci		}
8788c2ecf20Sopenharmony_ci	} else {
8798c2ecf20Sopenharmony_ci		len = inw(hw->base_port);
8808c2ecf20Sopenharmony_ci		if (len > hw->ll_mtu) {
8818c2ecf20Sopenharmony_ci			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
8828c2ecf20Sopenharmony_ci			       ": received a packet of %u bytes - longer than the MTU!\n", len);
8838c2ecf20Sopenharmony_ci			writew(MEMRX_PCINTACKK,
8848c2ecf20Sopenharmony_ci				&hw->memory_info_regs->memreg_pc_interrupt_ack);
8858c2ecf20Sopenharmony_ci			return;
8868c2ecf20Sopenharmony_ci		}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci		for (i = 0; i < len; i += 2) {
8898c2ecf20Sopenharmony_ci			__le16 raw_data = inw(hw->base_port);
8908c2ecf20Sopenharmony_ci			unsigned short data = le16_to_cpu(raw_data);
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci			pkt[i] = (unsigned char) data;
8938c2ecf20Sopenharmony_ci			pkt[i + 1] = (unsigned char) (data >> 8);
8948c2ecf20Sopenharmony_ci		}
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci		while ((i & 3) != 2) {
8978c2ecf20Sopenharmony_ci			inw(hw->base_port);
8988c2ecf20Sopenharmony_ci			i += 2;
8998c2ecf20Sopenharmony_ci		}
9008c2ecf20Sopenharmony_ci	}
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	acknowledge_data_read(hw);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	swap_packet_bitfield_from_le(pkt);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	if (ipwireless_debug)
9078c2ecf20Sopenharmony_ci		dump_data_bytes("recv", pkt, len);
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci	handle_received_packet(hw, (union nl_packet *) pkt, len);
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	end_read_timing(len);
9128c2ecf20Sopenharmony_ci}
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_cistatic int get_current_packet_priority(struct ipw_hardware *hw)
9158c2ecf20Sopenharmony_ci{
9168c2ecf20Sopenharmony_ci	/*
9178c2ecf20Sopenharmony_ci	 * If we're initializing, don't send anything of higher priority than
9188c2ecf20Sopenharmony_ci	 * PRIO_SETUP.  The network layer therefore need not care about
9198c2ecf20Sopenharmony_ci	 * hardware initialization - any of its stuff will simply be queued
9208c2ecf20Sopenharmony_ci	 * until setup is complete.
9218c2ecf20Sopenharmony_ci	 */
9228c2ecf20Sopenharmony_ci	return (hw->to_setup || hw->initializing
9238c2ecf20Sopenharmony_ci			? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
9248c2ecf20Sopenharmony_ci}
9258c2ecf20Sopenharmony_ci
9268c2ecf20Sopenharmony_ci/*
9278c2ecf20Sopenharmony_ci * return 1 if something has been received from hw
9288c2ecf20Sopenharmony_ci */
9298c2ecf20Sopenharmony_cistatic int get_packets_from_hw(struct ipw_hardware *hw)
9308c2ecf20Sopenharmony_ci{
9318c2ecf20Sopenharmony_ci	int received = 0;
9328c2ecf20Sopenharmony_ci	unsigned long flags;
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
9358c2ecf20Sopenharmony_ci	while (hw->rx_ready && !hw->blocking_rx) {
9368c2ecf20Sopenharmony_ci		received = 1;
9378c2ecf20Sopenharmony_ci		hw->rx_ready--;
9388c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci		do_receive_packet(hw);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hw->lock, flags);
9438c2ecf20Sopenharmony_ci	}
9448c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hw->lock, flags);
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci	return received;
9478c2ecf20Sopenharmony_ci}
9488c2ecf20Sopenharmony_ci
9498c2ecf20Sopenharmony_ci/*
9508c2ecf20Sopenharmony_ci * Send pending packet up to given priority, prioritize SETUP data until
9518c2ecf20Sopenharmony_ci * hardware is fully setup.
9528c2ecf20Sopenharmony_ci *
9538c2ecf20Sopenharmony_ci * return 1 if more packets can be sent
9548c2ecf20Sopenharmony_ci */
9558c2ecf20Sopenharmony_cistatic int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
9568c2ecf20Sopenharmony_ci{
9578c2ecf20Sopenharmony_ci	int more_to_send = 0;
9588c2ecf20Sopenharmony_ci	unsigned long flags;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
9618c2ecf20Sopenharmony_ci	if (hw->tx_queued && hw->tx_ready) {
9628c2ecf20Sopenharmony_ci		int priority;
9638c2ecf20Sopenharmony_ci		struct ipw_tx_packet *packet = NULL;
9648c2ecf20Sopenharmony_ci
9658c2ecf20Sopenharmony_ci		/* Pick a packet */
9668c2ecf20Sopenharmony_ci		for (priority = 0; priority < priority_limit; priority++) {
9678c2ecf20Sopenharmony_ci			if (!list_empty(&hw->tx_queue[priority])) {
9688c2ecf20Sopenharmony_ci				packet = list_first_entry(
9698c2ecf20Sopenharmony_ci						&hw->tx_queue[priority],
9708c2ecf20Sopenharmony_ci						struct ipw_tx_packet,
9718c2ecf20Sopenharmony_ci						queue);
9728c2ecf20Sopenharmony_ci
9738c2ecf20Sopenharmony_ci				hw->tx_queued--;
9748c2ecf20Sopenharmony_ci				list_del(&packet->queue);
9758c2ecf20Sopenharmony_ci
9768c2ecf20Sopenharmony_ci				break;
9778c2ecf20Sopenharmony_ci			}
9788c2ecf20Sopenharmony_ci		}
9798c2ecf20Sopenharmony_ci		if (!packet) {
9808c2ecf20Sopenharmony_ci			hw->tx_queued = 0;
9818c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hw->lock, flags);
9828c2ecf20Sopenharmony_ci			return 0;
9838c2ecf20Sopenharmony_ci		}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ci		/* Send */
9888c2ecf20Sopenharmony_ci		do_send_packet(hw, packet);
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci		/* Check if more to send */
9918c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hw->lock, flags);
9928c2ecf20Sopenharmony_ci		for (priority = 0; priority < priority_limit; priority++)
9938c2ecf20Sopenharmony_ci			if (!list_empty(&hw->tx_queue[priority])) {
9948c2ecf20Sopenharmony_ci				more_to_send = 1;
9958c2ecf20Sopenharmony_ci				break;
9968c2ecf20Sopenharmony_ci			}
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci		if (!more_to_send)
9998c2ecf20Sopenharmony_ci			hw->tx_queued = 0;
10008c2ecf20Sopenharmony_ci	}
10018c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hw->lock, flags);
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_ci	return more_to_send;
10048c2ecf20Sopenharmony_ci}
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci/*
10078c2ecf20Sopenharmony_ci * Send and receive all queued packets.
10088c2ecf20Sopenharmony_ci */
10098c2ecf20Sopenharmony_cistatic void ipwireless_do_tasklet(struct tasklet_struct *t)
10108c2ecf20Sopenharmony_ci{
10118c2ecf20Sopenharmony_ci	struct ipw_hardware *hw = from_tasklet(hw, t, tasklet);
10128c2ecf20Sopenharmony_ci	unsigned long flags;
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
10158c2ecf20Sopenharmony_ci	if (hw->shutting_down) {
10168c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
10178c2ecf20Sopenharmony_ci		return;
10188c2ecf20Sopenharmony_ci	}
10198c2ecf20Sopenharmony_ci
10208c2ecf20Sopenharmony_ci	if (hw->to_setup == 1) {
10218c2ecf20Sopenharmony_ci		/*
10228c2ecf20Sopenharmony_ci		 * Initial setup data sent to hardware
10238c2ecf20Sopenharmony_ci		 */
10248c2ecf20Sopenharmony_ci		hw->to_setup = 2;
10258c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
10268c2ecf20Sopenharmony_ci
10278c2ecf20Sopenharmony_ci		ipw_setup_hardware(hw);
10288c2ecf20Sopenharmony_ci		ipw_send_setup_packet(hw);
10298c2ecf20Sopenharmony_ci
10308c2ecf20Sopenharmony_ci		send_pending_packet(hw, PRIO_SETUP + 1);
10318c2ecf20Sopenharmony_ci		get_packets_from_hw(hw);
10328c2ecf20Sopenharmony_ci	} else {
10338c2ecf20Sopenharmony_ci		int priority_limit = get_current_packet_priority(hw);
10348c2ecf20Sopenharmony_ci		int again;
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci		do {
10398c2ecf20Sopenharmony_ci			again = send_pending_packet(hw, priority_limit);
10408c2ecf20Sopenharmony_ci			again |= get_packets_from_hw(hw);
10418c2ecf20Sopenharmony_ci		} while (again);
10428c2ecf20Sopenharmony_ci	}
10438c2ecf20Sopenharmony_ci}
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_ci/*
10468c2ecf20Sopenharmony_ci * return true if the card is physically present.
10478c2ecf20Sopenharmony_ci */
10488c2ecf20Sopenharmony_cistatic int is_card_present(struct ipw_hardware *hw)
10498c2ecf20Sopenharmony_ci{
10508c2ecf20Sopenharmony_ci	if (hw->hw_version == HW_VERSION_1)
10518c2ecf20Sopenharmony_ci		return inw(hw->base_port + IOIR) != 0xFFFF;
10528c2ecf20Sopenharmony_ci	else
10538c2ecf20Sopenharmony_ci		return readl(&hw->memory_info_regs->memreg_card_present) ==
10548c2ecf20Sopenharmony_ci		    CARD_PRESENT_VALUE;
10558c2ecf20Sopenharmony_ci}
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_cistatic irqreturn_t ipwireless_handle_v1_interrupt(int irq,
10588c2ecf20Sopenharmony_ci						  struct ipw_hardware *hw)
10598c2ecf20Sopenharmony_ci{
10608c2ecf20Sopenharmony_ci	unsigned short irqn;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	irqn = inw(hw->base_port + IOIR);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci	/* Check if card is present */
10658c2ecf20Sopenharmony_ci	if (irqn == 0xFFFF)
10668c2ecf20Sopenharmony_ci		return IRQ_NONE;
10678c2ecf20Sopenharmony_ci	else if (irqn != 0) {
10688c2ecf20Sopenharmony_ci		unsigned short ack = 0;
10698c2ecf20Sopenharmony_ci		unsigned long flags;
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci		/* Transmit complete. */
10728c2ecf20Sopenharmony_ci		if (irqn & IR_TXINTR) {
10738c2ecf20Sopenharmony_ci			ack |= IR_TXINTR;
10748c2ecf20Sopenharmony_ci			spin_lock_irqsave(&hw->lock, flags);
10758c2ecf20Sopenharmony_ci			hw->tx_ready = 1;
10768c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hw->lock, flags);
10778c2ecf20Sopenharmony_ci		}
10788c2ecf20Sopenharmony_ci		/* Received data */
10798c2ecf20Sopenharmony_ci		if (irqn & IR_RXINTR) {
10808c2ecf20Sopenharmony_ci			ack |= IR_RXINTR;
10818c2ecf20Sopenharmony_ci			spin_lock_irqsave(&hw->lock, flags);
10828c2ecf20Sopenharmony_ci			hw->rx_ready++;
10838c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hw->lock, flags);
10848c2ecf20Sopenharmony_ci		}
10858c2ecf20Sopenharmony_ci		if (ack != 0) {
10868c2ecf20Sopenharmony_ci			outw(ack, hw->base_port + IOIR);
10878c2ecf20Sopenharmony_ci			tasklet_schedule(&hw->tasklet);
10888c2ecf20Sopenharmony_ci		}
10898c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
10908c2ecf20Sopenharmony_ci	}
10918c2ecf20Sopenharmony_ci	return IRQ_NONE;
10928c2ecf20Sopenharmony_ci}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_cistatic void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
10958c2ecf20Sopenharmony_ci{
10968c2ecf20Sopenharmony_ci	unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	csr &= 0xfffd;
10998c2ecf20Sopenharmony_ci	writew(csr, &hw->memregs_CCR->reg_config_and_status);
11008c2ecf20Sopenharmony_ci}
11018c2ecf20Sopenharmony_ci
11028c2ecf20Sopenharmony_cistatic irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
11038c2ecf20Sopenharmony_ci						     struct ipw_hardware *hw)
11048c2ecf20Sopenharmony_ci{
11058c2ecf20Sopenharmony_ci	int tx = 0;
11068c2ecf20Sopenharmony_ci	int rx = 0;
11078c2ecf20Sopenharmony_ci	int rx_repeat = 0;
11088c2ecf20Sopenharmony_ci	int try_mem_tx_old;
11098c2ecf20Sopenharmony_ci	unsigned long flags;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	do {
11128c2ecf20Sopenharmony_ci
11138c2ecf20Sopenharmony_ci	unsigned short memtx = readw(hw->memreg_tx);
11148c2ecf20Sopenharmony_ci	unsigned short memtx_serial;
11158c2ecf20Sopenharmony_ci	unsigned short memrxdone =
11168c2ecf20Sopenharmony_ci		readw(&hw->memory_info_regs->memreg_rx_done);
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	try_mem_tx_old = 0;
11198c2ecf20Sopenharmony_ci
11208c2ecf20Sopenharmony_ci	/* check whether the interrupt was generated by ipwireless card */
11218c2ecf20Sopenharmony_ci	if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci		/* check if the card uses memreg_tx_old register */
11248c2ecf20Sopenharmony_ci		if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
11258c2ecf20Sopenharmony_ci			memtx = readw(&hw->memory_info_regs->memreg_tx_old);
11268c2ecf20Sopenharmony_ci			if (memtx & MEMTX_TX) {
11278c2ecf20Sopenharmony_ci				printk(KERN_INFO IPWIRELESS_PCCARD_NAME
11288c2ecf20Sopenharmony_ci					": Using memreg_tx_old\n");
11298c2ecf20Sopenharmony_ci				hw->memreg_tx =
11308c2ecf20Sopenharmony_ci					&hw->memory_info_regs->memreg_tx_old;
11318c2ecf20Sopenharmony_ci			} else {
11328c2ecf20Sopenharmony_ci				return IRQ_NONE;
11338c2ecf20Sopenharmony_ci			}
11348c2ecf20Sopenharmony_ci		} else
11358c2ecf20Sopenharmony_ci			return IRQ_NONE;
11368c2ecf20Sopenharmony_ci	}
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci	/*
11398c2ecf20Sopenharmony_ci	 * See if the card is physically present. Note that while it is
11408c2ecf20Sopenharmony_ci	 * powering up, it appears not to be present.
11418c2ecf20Sopenharmony_ci	 */
11428c2ecf20Sopenharmony_ci	if (!is_card_present(hw)) {
11438c2ecf20Sopenharmony_ci		acknowledge_pcmcia_interrupt(hw);
11448c2ecf20Sopenharmony_ci		return IRQ_HANDLED;
11458c2ecf20Sopenharmony_ci	}
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_ci	memtx_serial = memtx & (unsigned short) 0xff00;
11488c2ecf20Sopenharmony_ci	if (memtx & MEMTX_TX) {
11498c2ecf20Sopenharmony_ci		writew(memtx_serial, hw->memreg_tx);
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci		if (hw->serial_number_detected) {
11528c2ecf20Sopenharmony_ci			if (memtx_serial != hw->last_memtx_serial) {
11538c2ecf20Sopenharmony_ci				hw->last_memtx_serial = memtx_serial;
11548c2ecf20Sopenharmony_ci				spin_lock_irqsave(&hw->lock, flags);
11558c2ecf20Sopenharmony_ci				hw->rx_ready++;
11568c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&hw->lock, flags);
11578c2ecf20Sopenharmony_ci				rx = 1;
11588c2ecf20Sopenharmony_ci			} else
11598c2ecf20Sopenharmony_ci				/* Ignore 'Timer Recovery' duplicates. */
11608c2ecf20Sopenharmony_ci				rx_repeat = 1;
11618c2ecf20Sopenharmony_ci		} else {
11628c2ecf20Sopenharmony_ci			/*
11638c2ecf20Sopenharmony_ci			 * If a non-zero serial number is seen, then enable
11648c2ecf20Sopenharmony_ci			 * serial number checking.
11658c2ecf20Sopenharmony_ci			 */
11668c2ecf20Sopenharmony_ci			if (memtx_serial != 0) {
11678c2ecf20Sopenharmony_ci				hw->serial_number_detected = 1;
11688c2ecf20Sopenharmony_ci				printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
11698c2ecf20Sopenharmony_ci					": memreg_tx serial num detected\n");
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci				spin_lock_irqsave(&hw->lock, flags);
11728c2ecf20Sopenharmony_ci				hw->rx_ready++;
11738c2ecf20Sopenharmony_ci				spin_unlock_irqrestore(&hw->lock, flags);
11748c2ecf20Sopenharmony_ci			}
11758c2ecf20Sopenharmony_ci			rx = 1;
11768c2ecf20Sopenharmony_ci		}
11778c2ecf20Sopenharmony_ci	}
11788c2ecf20Sopenharmony_ci	if (memrxdone & MEMRX_RX_DONE) {
11798c2ecf20Sopenharmony_ci		writew(0, &hw->memory_info_regs->memreg_rx_done);
11808c2ecf20Sopenharmony_ci		spin_lock_irqsave(&hw->lock, flags);
11818c2ecf20Sopenharmony_ci		hw->tx_ready = 1;
11828c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&hw->lock, flags);
11838c2ecf20Sopenharmony_ci		tx = 1;
11848c2ecf20Sopenharmony_ci	}
11858c2ecf20Sopenharmony_ci	if (tx)
11868c2ecf20Sopenharmony_ci		writew(MEMRX_PCINTACKK,
11878c2ecf20Sopenharmony_ci				&hw->memory_info_regs->memreg_pc_interrupt_ack);
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	acknowledge_pcmcia_interrupt(hw);
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	if (tx || rx)
11928c2ecf20Sopenharmony_ci		tasklet_schedule(&hw->tasklet);
11938c2ecf20Sopenharmony_ci	else if (!rx_repeat) {
11948c2ecf20Sopenharmony_ci		if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
11958c2ecf20Sopenharmony_ci			if (hw->serial_number_detected)
11968c2ecf20Sopenharmony_ci				printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
11978c2ecf20Sopenharmony_ci					": spurious interrupt - new_tx mode\n");
11988c2ecf20Sopenharmony_ci			else {
11998c2ecf20Sopenharmony_ci				printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
12008c2ecf20Sopenharmony_ci					": no valid memreg_tx value - switching to the old memreg_tx\n");
12018c2ecf20Sopenharmony_ci				hw->memreg_tx =
12028c2ecf20Sopenharmony_ci					&hw->memory_info_regs->memreg_tx_old;
12038c2ecf20Sopenharmony_ci				try_mem_tx_old = 1;
12048c2ecf20Sopenharmony_ci			}
12058c2ecf20Sopenharmony_ci		} else
12068c2ecf20Sopenharmony_ci			printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
12078c2ecf20Sopenharmony_ci					": spurious interrupt - old_tx mode\n");
12088c2ecf20Sopenharmony_ci	}
12098c2ecf20Sopenharmony_ci
12108c2ecf20Sopenharmony_ci	} while (try_mem_tx_old == 1);
12118c2ecf20Sopenharmony_ci
12128c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
12138c2ecf20Sopenharmony_ci}
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ciirqreturn_t ipwireless_interrupt(int irq, void *dev_id)
12168c2ecf20Sopenharmony_ci{
12178c2ecf20Sopenharmony_ci	struct ipw_dev *ipw = dev_id;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	if (ipw->hardware->hw_version == HW_VERSION_1)
12208c2ecf20Sopenharmony_ci		return ipwireless_handle_v1_interrupt(irq, ipw->hardware);
12218c2ecf20Sopenharmony_ci	else
12228c2ecf20Sopenharmony_ci		return ipwireless_handle_v2_v3_interrupt(irq, ipw->hardware);
12238c2ecf20Sopenharmony_ci}
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_cistatic void flush_packets_to_hw(struct ipw_hardware *hw)
12268c2ecf20Sopenharmony_ci{
12278c2ecf20Sopenharmony_ci	int priority_limit;
12288c2ecf20Sopenharmony_ci	unsigned long flags;
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
12318c2ecf20Sopenharmony_ci	priority_limit = get_current_packet_priority(hw);
12328c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hw->lock, flags);
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	while (send_pending_packet(hw, priority_limit));
12358c2ecf20Sopenharmony_ci}
12368c2ecf20Sopenharmony_ci
12378c2ecf20Sopenharmony_cistatic void send_packet(struct ipw_hardware *hw, int priority,
12388c2ecf20Sopenharmony_ci			struct ipw_tx_packet *packet)
12398c2ecf20Sopenharmony_ci{
12408c2ecf20Sopenharmony_ci	unsigned long flags;
12418c2ecf20Sopenharmony_ci
12428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&hw->lock, flags);
12438c2ecf20Sopenharmony_ci	list_add_tail(&packet->queue, &hw->tx_queue[priority]);
12448c2ecf20Sopenharmony_ci	hw->tx_queued++;
12458c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&hw->lock, flags);
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci	flush_packets_to_hw(hw);
12488c2ecf20Sopenharmony_ci}
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci/* Create data packet, non-atomic allocation */
12518c2ecf20Sopenharmony_cistatic void *alloc_data_packet(int data_size,
12528c2ecf20Sopenharmony_ci				unsigned char dest_addr,
12538c2ecf20Sopenharmony_ci				unsigned char protocol)
12548c2ecf20Sopenharmony_ci{
12558c2ecf20Sopenharmony_ci	struct ipw_tx_packet *packet = kzalloc(
12568c2ecf20Sopenharmony_ci			sizeof(struct ipw_tx_packet) + data_size,
12578c2ecf20Sopenharmony_ci			GFP_ATOMIC);
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	if (!packet)
12608c2ecf20Sopenharmony_ci		return NULL;
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&packet->queue);
12638c2ecf20Sopenharmony_ci	packet->dest_addr = dest_addr;
12648c2ecf20Sopenharmony_ci	packet->protocol = protocol;
12658c2ecf20Sopenharmony_ci	packet->length = data_size;
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	return packet;
12688c2ecf20Sopenharmony_ci}
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_cistatic void *alloc_ctrl_packet(int header_size,
12718c2ecf20Sopenharmony_ci			       unsigned char dest_addr,
12728c2ecf20Sopenharmony_ci			       unsigned char protocol,
12738c2ecf20Sopenharmony_ci			       unsigned char sig_no)
12748c2ecf20Sopenharmony_ci{
12758c2ecf20Sopenharmony_ci	/*
12768c2ecf20Sopenharmony_ci	 * sig_no is located right after ipw_tx_packet struct in every
12778c2ecf20Sopenharmony_ci	 * CTRL or SETUP packets, we can use ipw_control_packet as a
12788c2ecf20Sopenharmony_ci	 * common struct
12798c2ecf20Sopenharmony_ci	 */
12808c2ecf20Sopenharmony_ci	struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_ci	if (!packet)
12838c2ecf20Sopenharmony_ci		return NULL;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&packet->header.queue);
12868c2ecf20Sopenharmony_ci	packet->header.dest_addr = dest_addr;
12878c2ecf20Sopenharmony_ci	packet->header.protocol = protocol;
12888c2ecf20Sopenharmony_ci	packet->header.length = header_size - sizeof(struct ipw_tx_packet);
12898c2ecf20Sopenharmony_ci	packet->body.sig_no = sig_no;
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	return packet;
12928c2ecf20Sopenharmony_ci}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_ciint ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
12958c2ecf20Sopenharmony_ci			    const unsigned char *data, unsigned int length,
12968c2ecf20Sopenharmony_ci			    void (*callback) (void *cb, unsigned int length),
12978c2ecf20Sopenharmony_ci			    void *callback_data)
12988c2ecf20Sopenharmony_ci{
12998c2ecf20Sopenharmony_ci	struct ipw_tx_packet *packet;
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	packet = alloc_data_packet(length, (channel_idx + 1),
13028c2ecf20Sopenharmony_ci			TL_PROTOCOLID_COM_DATA);
13038c2ecf20Sopenharmony_ci	if (!packet)
13048c2ecf20Sopenharmony_ci		return -ENOMEM;
13058c2ecf20Sopenharmony_ci	packet->packet_callback = callback;
13068c2ecf20Sopenharmony_ci	packet->callback_data = callback_data;
13078c2ecf20Sopenharmony_ci	memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
13088c2ecf20Sopenharmony_ci			length);
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci	send_packet(hw, PRIO_DATA, packet);
13118c2ecf20Sopenharmony_ci	return 0;
13128c2ecf20Sopenharmony_ci}
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_cistatic int set_control_line(struct ipw_hardware *hw, int prio,
13158c2ecf20Sopenharmony_ci			   unsigned int channel_idx, int line, int state)
13168c2ecf20Sopenharmony_ci{
13178c2ecf20Sopenharmony_ci	struct ipw_control_packet *packet;
13188c2ecf20Sopenharmony_ci	int protocolid = TL_PROTOCOLID_COM_CTRL;
13198c2ecf20Sopenharmony_ci
13208c2ecf20Sopenharmony_ci	if (prio == PRIO_SETUP)
13218c2ecf20Sopenharmony_ci		protocolid = TL_PROTOCOLID_SETUP;
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
13248c2ecf20Sopenharmony_ci			(channel_idx + 1), protocolid, line);
13258c2ecf20Sopenharmony_ci	if (!packet)
13268c2ecf20Sopenharmony_ci		return -ENOMEM;
13278c2ecf20Sopenharmony_ci	packet->header.length = sizeof(struct ipw_control_packet_body);
13288c2ecf20Sopenharmony_ci	packet->body.value = (state == 0 ? 0 : 1);
13298c2ecf20Sopenharmony_ci	send_packet(hw, prio, &packet->header);
13308c2ecf20Sopenharmony_ci	return 0;
13318c2ecf20Sopenharmony_ci}
13328c2ecf20Sopenharmony_ci
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_cistatic int set_DTR(struct ipw_hardware *hw, int priority,
13358c2ecf20Sopenharmony_ci		   unsigned int channel_idx, int state)
13368c2ecf20Sopenharmony_ci{
13378c2ecf20Sopenharmony_ci	if (state != 0)
13388c2ecf20Sopenharmony_ci		hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
13398c2ecf20Sopenharmony_ci	else
13408c2ecf20Sopenharmony_ci		hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
13438c2ecf20Sopenharmony_ci}
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_cistatic int set_RTS(struct ipw_hardware *hw, int priority,
13468c2ecf20Sopenharmony_ci		   unsigned int channel_idx, int state)
13478c2ecf20Sopenharmony_ci{
13488c2ecf20Sopenharmony_ci	if (state != 0)
13498c2ecf20Sopenharmony_ci		hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
13508c2ecf20Sopenharmony_ci	else
13518c2ecf20Sopenharmony_ci		hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
13548c2ecf20Sopenharmony_ci}
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_ciint ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
13578c2ecf20Sopenharmony_ci		       int state)
13588c2ecf20Sopenharmony_ci{
13598c2ecf20Sopenharmony_ci	return set_DTR(hw, PRIO_CTRL, channel_idx, state);
13608c2ecf20Sopenharmony_ci}
13618c2ecf20Sopenharmony_ci
13628c2ecf20Sopenharmony_ciint ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
13638c2ecf20Sopenharmony_ci		       int state)
13648c2ecf20Sopenharmony_ci{
13658c2ecf20Sopenharmony_ci	return set_RTS(hw, PRIO_CTRL, channel_idx, state);
13668c2ecf20Sopenharmony_ci}
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_cistruct ipw_setup_get_version_query_packet {
13698c2ecf20Sopenharmony_ci	struct ipw_tx_packet header;
13708c2ecf20Sopenharmony_ci	struct tl_setup_get_version_qry body;
13718c2ecf20Sopenharmony_ci};
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_cistruct ipw_setup_config_packet {
13748c2ecf20Sopenharmony_ci	struct ipw_tx_packet header;
13758c2ecf20Sopenharmony_ci	struct tl_setup_config_msg body;
13768c2ecf20Sopenharmony_ci};
13778c2ecf20Sopenharmony_ci
13788c2ecf20Sopenharmony_cistruct ipw_setup_config_done_packet {
13798c2ecf20Sopenharmony_ci	struct ipw_tx_packet header;
13808c2ecf20Sopenharmony_ci	struct tl_setup_config_done_msg body;
13818c2ecf20Sopenharmony_ci};
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_cistruct ipw_setup_open_packet {
13848c2ecf20Sopenharmony_ci	struct ipw_tx_packet header;
13858c2ecf20Sopenharmony_ci	struct tl_setup_open_msg body;
13868c2ecf20Sopenharmony_ci};
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_cistruct ipw_setup_info_packet {
13898c2ecf20Sopenharmony_ci	struct ipw_tx_packet header;
13908c2ecf20Sopenharmony_ci	struct tl_setup_info_msg body;
13918c2ecf20Sopenharmony_ci};
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_cistruct ipw_setup_reboot_msg_ack {
13948c2ecf20Sopenharmony_ci	struct ipw_tx_packet header;
13958c2ecf20Sopenharmony_ci	struct TlSetupRebootMsgAck body;
13968c2ecf20Sopenharmony_ci};
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci/* This handles the actual initialization of the card */
13998c2ecf20Sopenharmony_cistatic void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
14008c2ecf20Sopenharmony_ci{
14018c2ecf20Sopenharmony_ci	struct ipw_setup_config_packet *config_packet;
14028c2ecf20Sopenharmony_ci	struct ipw_setup_config_done_packet *config_done_packet;
14038c2ecf20Sopenharmony_ci	struct ipw_setup_open_packet *open_packet;
14048c2ecf20Sopenharmony_ci	struct ipw_setup_info_packet *info_packet;
14058c2ecf20Sopenharmony_ci	int port;
14068c2ecf20Sopenharmony_ci	unsigned int channel_idx;
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_ci	/* generate config packet */
14098c2ecf20Sopenharmony_ci	for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
14108c2ecf20Sopenharmony_ci		config_packet = alloc_ctrl_packet(
14118c2ecf20Sopenharmony_ci				sizeof(struct ipw_setup_config_packet),
14128c2ecf20Sopenharmony_ci				ADDR_SETUP_PROT,
14138c2ecf20Sopenharmony_ci				TL_PROTOCOLID_SETUP,
14148c2ecf20Sopenharmony_ci				TL_SETUP_SIGNO_CONFIG_MSG);
14158c2ecf20Sopenharmony_ci		if (!config_packet)
14168c2ecf20Sopenharmony_ci			goto exit_nomem;
14178c2ecf20Sopenharmony_ci		config_packet->header.length = sizeof(struct tl_setup_config_msg);
14188c2ecf20Sopenharmony_ci		config_packet->body.port_no = port;
14198c2ecf20Sopenharmony_ci		config_packet->body.prio_data = PRIO_DATA;
14208c2ecf20Sopenharmony_ci		config_packet->body.prio_ctrl = PRIO_CTRL;
14218c2ecf20Sopenharmony_ci		send_packet(hw, PRIO_SETUP, &config_packet->header);
14228c2ecf20Sopenharmony_ci	}
14238c2ecf20Sopenharmony_ci	config_done_packet = alloc_ctrl_packet(
14248c2ecf20Sopenharmony_ci			sizeof(struct ipw_setup_config_done_packet),
14258c2ecf20Sopenharmony_ci			ADDR_SETUP_PROT,
14268c2ecf20Sopenharmony_ci			TL_PROTOCOLID_SETUP,
14278c2ecf20Sopenharmony_ci			TL_SETUP_SIGNO_CONFIG_DONE_MSG);
14288c2ecf20Sopenharmony_ci	if (!config_done_packet)
14298c2ecf20Sopenharmony_ci		goto exit_nomem;
14308c2ecf20Sopenharmony_ci	config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
14318c2ecf20Sopenharmony_ci	send_packet(hw, PRIO_SETUP, &config_done_packet->header);
14328c2ecf20Sopenharmony_ci
14338c2ecf20Sopenharmony_ci	/* generate open packet */
14348c2ecf20Sopenharmony_ci	for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
14358c2ecf20Sopenharmony_ci		open_packet = alloc_ctrl_packet(
14368c2ecf20Sopenharmony_ci				sizeof(struct ipw_setup_open_packet),
14378c2ecf20Sopenharmony_ci				ADDR_SETUP_PROT,
14388c2ecf20Sopenharmony_ci				TL_PROTOCOLID_SETUP,
14398c2ecf20Sopenharmony_ci				TL_SETUP_SIGNO_OPEN_MSG);
14408c2ecf20Sopenharmony_ci		if (!open_packet)
14418c2ecf20Sopenharmony_ci			goto exit_nomem;
14428c2ecf20Sopenharmony_ci		open_packet->header.length = sizeof(struct tl_setup_open_msg);
14438c2ecf20Sopenharmony_ci		open_packet->body.port_no = port;
14448c2ecf20Sopenharmony_ci		send_packet(hw, PRIO_SETUP, &open_packet->header);
14458c2ecf20Sopenharmony_ci	}
14468c2ecf20Sopenharmony_ci	for (channel_idx = 0;
14478c2ecf20Sopenharmony_ci			channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
14488c2ecf20Sopenharmony_ci		int ret;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_ci		ret = set_DTR(hw, PRIO_SETUP, channel_idx,
14518c2ecf20Sopenharmony_ci			(hw->control_lines[channel_idx] &
14528c2ecf20Sopenharmony_ci			 IPW_CONTROL_LINE_DTR) != 0);
14538c2ecf20Sopenharmony_ci		if (ret) {
14548c2ecf20Sopenharmony_ci			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
14558c2ecf20Sopenharmony_ci					": error setting DTR (%d)\n", ret);
14568c2ecf20Sopenharmony_ci			return;
14578c2ecf20Sopenharmony_ci		}
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci		ret = set_RTS(hw, PRIO_SETUP, channel_idx,
14608c2ecf20Sopenharmony_ci			(hw->control_lines [channel_idx] &
14618c2ecf20Sopenharmony_ci			 IPW_CONTROL_LINE_RTS) != 0);
14628c2ecf20Sopenharmony_ci		if (ret) {
14638c2ecf20Sopenharmony_ci			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
14648c2ecf20Sopenharmony_ci					": error setting RTS (%d)\n", ret);
14658c2ecf20Sopenharmony_ci			return;
14668c2ecf20Sopenharmony_ci		}
14678c2ecf20Sopenharmony_ci	}
14688c2ecf20Sopenharmony_ci	/*
14698c2ecf20Sopenharmony_ci	 * For NDIS we assume that we are using sync PPP frames, for COM async.
14708c2ecf20Sopenharmony_ci	 * This driver uses NDIS mode too. We don't bother with translation
14718c2ecf20Sopenharmony_ci	 * from async -> sync PPP.
14728c2ecf20Sopenharmony_ci	 */
14738c2ecf20Sopenharmony_ci	info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
14748c2ecf20Sopenharmony_ci			ADDR_SETUP_PROT,
14758c2ecf20Sopenharmony_ci			TL_PROTOCOLID_SETUP,
14768c2ecf20Sopenharmony_ci			TL_SETUP_SIGNO_INFO_MSG);
14778c2ecf20Sopenharmony_ci	if (!info_packet)
14788c2ecf20Sopenharmony_ci		goto exit_nomem;
14798c2ecf20Sopenharmony_ci	info_packet->header.length = sizeof(struct tl_setup_info_msg);
14808c2ecf20Sopenharmony_ci	info_packet->body.driver_type = NDISWAN_DRIVER;
14818c2ecf20Sopenharmony_ci	info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
14828c2ecf20Sopenharmony_ci	info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
14838c2ecf20Sopenharmony_ci	send_packet(hw, PRIO_SETUP, &info_packet->header);
14848c2ecf20Sopenharmony_ci
14858c2ecf20Sopenharmony_ci	/* Initialization is now complete, so we clear the 'to_setup' flag */
14868c2ecf20Sopenharmony_ci	hw->to_setup = 0;
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	return;
14898c2ecf20Sopenharmony_ci
14908c2ecf20Sopenharmony_ciexit_nomem:
14918c2ecf20Sopenharmony_ci	printk(KERN_ERR IPWIRELESS_PCCARD_NAME
14928c2ecf20Sopenharmony_ci			": not enough memory to alloc control packet\n");
14938c2ecf20Sopenharmony_ci	hw->to_setup = -1;
14948c2ecf20Sopenharmony_ci}
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_cistatic void handle_setup_get_version_rsp(struct ipw_hardware *hw,
14978c2ecf20Sopenharmony_ci		unsigned char vers_no)
14988c2ecf20Sopenharmony_ci{
14998c2ecf20Sopenharmony_ci	del_timer(&hw->setup_timer);
15008c2ecf20Sopenharmony_ci	hw->initializing = 0;
15018c2ecf20Sopenharmony_ci	printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
15028c2ecf20Sopenharmony_ci
15038c2ecf20Sopenharmony_ci	if (vers_no == TL_SETUP_VERSION)
15048c2ecf20Sopenharmony_ci		__handle_setup_get_version_rsp(hw);
15058c2ecf20Sopenharmony_ci	else
15068c2ecf20Sopenharmony_ci		printk(KERN_ERR IPWIRELESS_PCCARD_NAME
15078c2ecf20Sopenharmony_ci				": invalid hardware version no %u\n",
15088c2ecf20Sopenharmony_ci				(unsigned int) vers_no);
15098c2ecf20Sopenharmony_ci}
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_cistatic void ipw_send_setup_packet(struct ipw_hardware *hw)
15128c2ecf20Sopenharmony_ci{
15138c2ecf20Sopenharmony_ci	struct ipw_setup_get_version_query_packet *ver_packet;
15148c2ecf20Sopenharmony_ci
15158c2ecf20Sopenharmony_ci	ver_packet = alloc_ctrl_packet(
15168c2ecf20Sopenharmony_ci			sizeof(struct ipw_setup_get_version_query_packet),
15178c2ecf20Sopenharmony_ci			ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
15188c2ecf20Sopenharmony_ci			TL_SETUP_SIGNO_GET_VERSION_QRY);
15198c2ecf20Sopenharmony_ci	if (!ver_packet)
15208c2ecf20Sopenharmony_ci		return;
15218c2ecf20Sopenharmony_ci	ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	/*
15248c2ecf20Sopenharmony_ci	 * Response is handled in handle_received_SETUP_packet
15258c2ecf20Sopenharmony_ci	 */
15268c2ecf20Sopenharmony_ci	send_packet(hw, PRIO_SETUP, &ver_packet->header);
15278c2ecf20Sopenharmony_ci}
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_cistatic void handle_received_SETUP_packet(struct ipw_hardware *hw,
15308c2ecf20Sopenharmony_ci					 unsigned int address,
15318c2ecf20Sopenharmony_ci					 const unsigned char *data, int len,
15328c2ecf20Sopenharmony_ci					 int is_last)
15338c2ecf20Sopenharmony_ci{
15348c2ecf20Sopenharmony_ci	const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
15358c2ecf20Sopenharmony_ci
15368c2ecf20Sopenharmony_ci	if (address != ADDR_SETUP_PROT) {
15378c2ecf20Sopenharmony_ci		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
15388c2ecf20Sopenharmony_ci		       ": setup packet has bad address %d\n", address);
15398c2ecf20Sopenharmony_ci		return;
15408c2ecf20Sopenharmony_ci	}
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	switch (rx_msg->sig_no) {
15438c2ecf20Sopenharmony_ci	case TL_SETUP_SIGNO_GET_VERSION_RSP:
15448c2ecf20Sopenharmony_ci		if (hw->to_setup)
15458c2ecf20Sopenharmony_ci			handle_setup_get_version_rsp(hw,
15468c2ecf20Sopenharmony_ci					rx_msg->version_rsp_msg.version);
15478c2ecf20Sopenharmony_ci		break;
15488c2ecf20Sopenharmony_ci
15498c2ecf20Sopenharmony_ci	case TL_SETUP_SIGNO_OPEN_MSG:
15508c2ecf20Sopenharmony_ci		if (ipwireless_debug) {
15518c2ecf20Sopenharmony_ci			unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
15548c2ecf20Sopenharmony_ci			       ": OPEN_MSG [channel %u] reply received\n",
15558c2ecf20Sopenharmony_ci			       channel_idx);
15568c2ecf20Sopenharmony_ci		}
15578c2ecf20Sopenharmony_ci		break;
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	case TL_SETUP_SIGNO_INFO_MSG_ACK:
15608c2ecf20Sopenharmony_ci		if (ipwireless_debug)
15618c2ecf20Sopenharmony_ci			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
15628c2ecf20Sopenharmony_ci			       ": card successfully configured as NDISWAN\n");
15638c2ecf20Sopenharmony_ci		break;
15648c2ecf20Sopenharmony_ci
15658c2ecf20Sopenharmony_ci	case TL_SETUP_SIGNO_REBOOT_MSG:
15668c2ecf20Sopenharmony_ci		if (hw->to_setup)
15678c2ecf20Sopenharmony_ci			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
15688c2ecf20Sopenharmony_ci			       ": Setup not completed - ignoring reboot msg\n");
15698c2ecf20Sopenharmony_ci		else {
15708c2ecf20Sopenharmony_ci			struct ipw_setup_reboot_msg_ack *packet;
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
15738c2ecf20Sopenharmony_ci			       ": Acknowledging REBOOT message\n");
15748c2ecf20Sopenharmony_ci			packet = alloc_ctrl_packet(
15758c2ecf20Sopenharmony_ci					sizeof(struct ipw_setup_reboot_msg_ack),
15768c2ecf20Sopenharmony_ci					ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
15778c2ecf20Sopenharmony_ci					TL_SETUP_SIGNO_REBOOT_MSG_ACK);
15788c2ecf20Sopenharmony_ci			if (!packet) {
15798c2ecf20Sopenharmony_ci				pr_err(IPWIRELESS_PCCARD_NAME
15808c2ecf20Sopenharmony_ci				       ": Not enough memory to send reboot packet");
15818c2ecf20Sopenharmony_ci				break;
15828c2ecf20Sopenharmony_ci			}
15838c2ecf20Sopenharmony_ci			packet->header.length =
15848c2ecf20Sopenharmony_ci				sizeof(struct TlSetupRebootMsgAck);
15858c2ecf20Sopenharmony_ci			send_packet(hw, PRIO_SETUP, &packet->header);
15868c2ecf20Sopenharmony_ci			if (hw->reboot_callback)
15878c2ecf20Sopenharmony_ci				hw->reboot_callback(hw->reboot_callback_data);
15888c2ecf20Sopenharmony_ci		}
15898c2ecf20Sopenharmony_ci		break;
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci	default:
15928c2ecf20Sopenharmony_ci		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
15938c2ecf20Sopenharmony_ci		       ": unknown setup message %u received\n",
15948c2ecf20Sopenharmony_ci		       (unsigned int) rx_msg->sig_no);
15958c2ecf20Sopenharmony_ci	}
15968c2ecf20Sopenharmony_ci}
15978c2ecf20Sopenharmony_ci
15988c2ecf20Sopenharmony_cistatic void do_close_hardware(struct ipw_hardware *hw)
15998c2ecf20Sopenharmony_ci{
16008c2ecf20Sopenharmony_ci	unsigned int irqn;
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_ci	if (hw->hw_version == HW_VERSION_1) {
16038c2ecf20Sopenharmony_ci		/* Disable TX and RX interrupts. */
16048c2ecf20Sopenharmony_ci		outw(0, hw->base_port + IOIER);
16058c2ecf20Sopenharmony_ci
16068c2ecf20Sopenharmony_ci		/* Acknowledge any outstanding interrupt requests */
16078c2ecf20Sopenharmony_ci		irqn = inw(hw->base_port + IOIR);
16088c2ecf20Sopenharmony_ci		if (irqn & IR_TXINTR)
16098c2ecf20Sopenharmony_ci			outw(IR_TXINTR, hw->base_port + IOIR);
16108c2ecf20Sopenharmony_ci		if (irqn & IR_RXINTR)
16118c2ecf20Sopenharmony_ci			outw(IR_RXINTR, hw->base_port + IOIR);
16128c2ecf20Sopenharmony_ci
16138c2ecf20Sopenharmony_ci		synchronize_irq(hw->irq);
16148c2ecf20Sopenharmony_ci	}
16158c2ecf20Sopenharmony_ci}
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_cistruct ipw_hardware *ipwireless_hardware_create(void)
16188c2ecf20Sopenharmony_ci{
16198c2ecf20Sopenharmony_ci	int i;
16208c2ecf20Sopenharmony_ci	struct ipw_hardware *hw =
16218c2ecf20Sopenharmony_ci		kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	if (!hw)
16248c2ecf20Sopenharmony_ci		return NULL;
16258c2ecf20Sopenharmony_ci
16268c2ecf20Sopenharmony_ci	hw->irq = -1;
16278c2ecf20Sopenharmony_ci	hw->initializing = 1;
16288c2ecf20Sopenharmony_ci	hw->tx_ready = 1;
16298c2ecf20Sopenharmony_ci	hw->rx_bytes_queued = 0;
16308c2ecf20Sopenharmony_ci	hw->rx_pool_size = 0;
16318c2ecf20Sopenharmony_ci	hw->last_memtx_serial = (unsigned short) 0xffff;
16328c2ecf20Sopenharmony_ci	for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
16338c2ecf20Sopenharmony_ci		INIT_LIST_HEAD(&hw->tx_queue[i]);
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hw->rx_queue);
16368c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&hw->rx_pool);
16378c2ecf20Sopenharmony_ci	spin_lock_init(&hw->lock);
16388c2ecf20Sopenharmony_ci	tasklet_setup(&hw->tasklet, ipwireless_do_tasklet);
16398c2ecf20Sopenharmony_ci	INIT_WORK(&hw->work_rx, ipw_receive_data_work);
16408c2ecf20Sopenharmony_ci	timer_setup(&hw->setup_timer, ipwireless_setup_timer, 0);
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ci	return hw;
16438c2ecf20Sopenharmony_ci}
16448c2ecf20Sopenharmony_ci
16458c2ecf20Sopenharmony_civoid ipwireless_init_hardware_v1(struct ipw_hardware *hw,
16468c2ecf20Sopenharmony_ci		unsigned int base_port,
16478c2ecf20Sopenharmony_ci		void __iomem *attr_memory,
16488c2ecf20Sopenharmony_ci		void __iomem *common_memory,
16498c2ecf20Sopenharmony_ci		int is_v2_card,
16508c2ecf20Sopenharmony_ci		void (*reboot_callback) (void *data),
16518c2ecf20Sopenharmony_ci		void *reboot_callback_data)
16528c2ecf20Sopenharmony_ci{
16538c2ecf20Sopenharmony_ci	if (hw->removed) {
16548c2ecf20Sopenharmony_ci		hw->removed = 0;
16558c2ecf20Sopenharmony_ci		enable_irq(hw->irq);
16568c2ecf20Sopenharmony_ci	}
16578c2ecf20Sopenharmony_ci	hw->base_port = base_port;
16588c2ecf20Sopenharmony_ci	hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
16598c2ecf20Sopenharmony_ci	hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
16608c2ecf20Sopenharmony_ci	hw->memregs_CCR = (struct MEMCCR __iomem *)
16618c2ecf20Sopenharmony_ci			((unsigned short __iomem *) attr_memory + 0x200);
16628c2ecf20Sopenharmony_ci	hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
16638c2ecf20Sopenharmony_ci	hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
16648c2ecf20Sopenharmony_ci	hw->reboot_callback = reboot_callback;
16658c2ecf20Sopenharmony_ci	hw->reboot_callback_data = reboot_callback_data;
16668c2ecf20Sopenharmony_ci}
16678c2ecf20Sopenharmony_ci
16688c2ecf20Sopenharmony_civoid ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
16698c2ecf20Sopenharmony_ci{
16708c2ecf20Sopenharmony_ci	hw->initializing = 1;
16718c2ecf20Sopenharmony_ci	hw->init_loops = 0;
16728c2ecf20Sopenharmony_ci	printk(KERN_INFO IPWIRELESS_PCCARD_NAME
16738c2ecf20Sopenharmony_ci	       ": waiting for card to start up...\n");
16748c2ecf20Sopenharmony_ci	ipwireless_setup_timer(&hw->setup_timer);
16758c2ecf20Sopenharmony_ci}
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_cistatic void ipwireless_setup_timer(struct timer_list *t)
16788c2ecf20Sopenharmony_ci{
16798c2ecf20Sopenharmony_ci	struct ipw_hardware *hw = from_timer(hw, t, setup_timer);
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	hw->init_loops++;
16828c2ecf20Sopenharmony_ci
16838c2ecf20Sopenharmony_ci	if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
16848c2ecf20Sopenharmony_ci			hw->hw_version == HW_VERSION_2 &&
16858c2ecf20Sopenharmony_ci			hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
16868c2ecf20Sopenharmony_ci		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
16878c2ecf20Sopenharmony_ci				": failed to startup using TX2, trying TX\n");
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci		hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
16908c2ecf20Sopenharmony_ci		hw->init_loops = 0;
16918c2ecf20Sopenharmony_ci	}
16928c2ecf20Sopenharmony_ci	/* Give up after a certain number of retries */
16938c2ecf20Sopenharmony_ci	if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
16948c2ecf20Sopenharmony_ci		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
16958c2ecf20Sopenharmony_ci		       ": card failed to start up!\n");
16968c2ecf20Sopenharmony_ci		hw->initializing = 0;
16978c2ecf20Sopenharmony_ci	} else {
16988c2ecf20Sopenharmony_ci		/* Do not attempt to write to the board if it is not present. */
16998c2ecf20Sopenharmony_ci		if (is_card_present(hw)) {
17008c2ecf20Sopenharmony_ci			unsigned long flags;
17018c2ecf20Sopenharmony_ci
17028c2ecf20Sopenharmony_ci			spin_lock_irqsave(&hw->lock, flags);
17038c2ecf20Sopenharmony_ci			hw->to_setup = 1;
17048c2ecf20Sopenharmony_ci			hw->tx_ready = 1;
17058c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&hw->lock, flags);
17068c2ecf20Sopenharmony_ci			tasklet_schedule(&hw->tasklet);
17078c2ecf20Sopenharmony_ci		}
17088c2ecf20Sopenharmony_ci
17098c2ecf20Sopenharmony_ci		mod_timer(&hw->setup_timer,
17108c2ecf20Sopenharmony_ci			jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
17118c2ecf20Sopenharmony_ci	}
17128c2ecf20Sopenharmony_ci}
17138c2ecf20Sopenharmony_ci
17148c2ecf20Sopenharmony_ci/*
17158c2ecf20Sopenharmony_ci * Stop any interrupts from executing so that, once this function returns,
17168c2ecf20Sopenharmony_ci * other layers of the driver can be sure they won't get any more callbacks.
17178c2ecf20Sopenharmony_ci * Thus must be called on a proper process context.
17188c2ecf20Sopenharmony_ci */
17198c2ecf20Sopenharmony_civoid ipwireless_stop_interrupts(struct ipw_hardware *hw)
17208c2ecf20Sopenharmony_ci{
17218c2ecf20Sopenharmony_ci	if (!hw->shutting_down) {
17228c2ecf20Sopenharmony_ci		/* Tell everyone we are going down. */
17238c2ecf20Sopenharmony_ci		hw->shutting_down = 1;
17248c2ecf20Sopenharmony_ci		del_timer(&hw->setup_timer);
17258c2ecf20Sopenharmony_ci
17268c2ecf20Sopenharmony_ci		/* Prevent the hardware from sending any more interrupts */
17278c2ecf20Sopenharmony_ci		do_close_hardware(hw);
17288c2ecf20Sopenharmony_ci	}
17298c2ecf20Sopenharmony_ci}
17308c2ecf20Sopenharmony_ci
17318c2ecf20Sopenharmony_civoid ipwireless_hardware_free(struct ipw_hardware *hw)
17328c2ecf20Sopenharmony_ci{
17338c2ecf20Sopenharmony_ci	int i;
17348c2ecf20Sopenharmony_ci	struct ipw_rx_packet *rp, *rq;
17358c2ecf20Sopenharmony_ci	struct ipw_tx_packet *tp, *tq;
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci	ipwireless_stop_interrupts(hw);
17388c2ecf20Sopenharmony_ci
17398c2ecf20Sopenharmony_ci	flush_work(&hw->work_rx);
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci	for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
17428c2ecf20Sopenharmony_ci		kfree(hw->packet_assembler[i]);
17438c2ecf20Sopenharmony_ci
17448c2ecf20Sopenharmony_ci	for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
17458c2ecf20Sopenharmony_ci		list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
17468c2ecf20Sopenharmony_ci			list_del(&tp->queue);
17478c2ecf20Sopenharmony_ci			kfree(tp);
17488c2ecf20Sopenharmony_ci		}
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_ci	list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
17518c2ecf20Sopenharmony_ci		list_del(&rp->queue);
17528c2ecf20Sopenharmony_ci		kfree(rp);
17538c2ecf20Sopenharmony_ci	}
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
17568c2ecf20Sopenharmony_ci		list_del(&rp->queue);
17578c2ecf20Sopenharmony_ci		kfree(rp);
17588c2ecf20Sopenharmony_ci	}
17598c2ecf20Sopenharmony_ci	kfree(hw);
17608c2ecf20Sopenharmony_ci}
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci/*
17638c2ecf20Sopenharmony_ci * Associate the specified network with this hardware, so it will receive events
17648c2ecf20Sopenharmony_ci * from it.
17658c2ecf20Sopenharmony_ci */
17668c2ecf20Sopenharmony_civoid ipwireless_associate_network(struct ipw_hardware *hw,
17678c2ecf20Sopenharmony_ci				  struct ipw_network *network)
17688c2ecf20Sopenharmony_ci{
17698c2ecf20Sopenharmony_ci	hw->network = network;
17708c2ecf20Sopenharmony_ci}
1771