Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * IPWireless 3G PCMCIA Network Driver
   4 *
   5 * Original code
   6 *   by Stephen Blackheath <stephen@blacksapphire.com>,
   7 *      Ben Martel <benm@symmetric.co.nz>
   8 *
   9 * Copyrighted as follows:
  10 *   Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
  11 *
  12 * Various driver changes and rewrites, port to new kernels
  13 *   Copyright (C) 2006-2007 Jiri Kosina
  14 *
  15 * Misc code cleanups and updates
  16 *   Copyright (C) 2007 David Sterba
  17 */
  18
  19#include <linux/interrupt.h>
  20#include <linux/io.h>
  21#include <linux/irq.h>
  22#include <linux/kernel.h>
  23#include <linux/list.h>
  24#include <linux/slab.h>
  25
  26#include "hardware.h"
  27#include "setup_protocol.h"
  28#include "network.h"
  29#include "main.h"
  30
  31static void ipw_send_setup_packet(struct ipw_hardware *hw);
  32static void handle_received_SETUP_packet(struct ipw_hardware *ipw,
  33					 unsigned int address,
  34					 const unsigned char *data, int len,
  35					 int is_last);
  36static void ipwireless_setup_timer(struct timer_list *t);
  37static void handle_received_CTRL_packet(struct ipw_hardware *hw,
  38		unsigned int channel_idx, const unsigned char *data, int len);
  39
  40/*#define TIMING_DIAGNOSTICS*/
  41
  42#ifdef TIMING_DIAGNOSTICS
  43
  44static struct timing_stats {
  45	unsigned long last_report_time;
  46	unsigned long read_time;
  47	unsigned long write_time;
  48	unsigned long read_bytes;
  49	unsigned long write_bytes;
  50	unsigned long start_time;
  51};
  52
  53static void start_timing(void)
  54{
  55	timing_stats.start_time = jiffies;
  56}
  57
  58static void end_read_timing(unsigned length)
  59{
  60	timing_stats.read_time += (jiffies - start_time);
  61	timing_stats.read_bytes += length + 2;
  62	report_timing();
  63}
  64
  65static void end_write_timing(unsigned length)
  66{
  67	timing_stats.write_time += (jiffies - start_time);
  68	timing_stats.write_bytes += length + 2;
  69	report_timing();
  70}
  71
  72static void report_timing(void)
  73{
  74	unsigned long since = jiffies - timing_stats.last_report_time;
  75
  76	/* If it's been more than one second... */
  77	if (since >= HZ) {
  78		int first = (timing_stats.last_report_time == 0);
  79
  80		timing_stats.last_report_time = jiffies;
  81		if (!first)
  82			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  83			       ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
  84			       jiffies_to_usecs(since),
  85			       timing_stats.read_bytes,
  86			       jiffies_to_usecs(timing_stats.read_time),
  87			       timing_stats.write_bytes,
  88			       jiffies_to_usecs(timing_stats.write_time));
  89
  90		timing_stats.read_time = 0;
  91		timing_stats.write_time = 0;
  92		timing_stats.read_bytes = 0;
  93		timing_stats.write_bytes = 0;
  94	}
  95}
  96#else
  97static void start_timing(void) { }
  98static void end_read_timing(unsigned length) { }
  99static void end_write_timing(unsigned length) { }
 100#endif
 101
 102/* Imported IPW definitions */
 103
 104#define LL_MTU_V1 318
 105#define LL_MTU_V2 250
 106#define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
 107
 108#define PRIO_DATA  2
 109#define PRIO_CTRL  1
 110#define PRIO_SETUP 0
 111
 112/* Addresses */
 113#define ADDR_SETUP_PROT 0
 114
 115/* Protocol ids */
 116enum {
 117	/* Identifier for the Com Data protocol */
 118	TL_PROTOCOLID_COM_DATA = 0,
 119
 120	/* Identifier for the Com Control protocol */
 121	TL_PROTOCOLID_COM_CTRL = 1,
 122
 123	/* Identifier for the Setup protocol */
 124	TL_PROTOCOLID_SETUP = 2
 125};
 126
 127/* Number of bytes in NL packet header (cannot do
 128 * sizeof(nl_packet_header) since it's a bitfield) */
 129#define NL_FIRST_PACKET_HEADER_SIZE        3
 130
 131/* Number of bytes in NL packet header (cannot do
 132 * sizeof(nl_packet_header) since it's a bitfield) */
 133#define NL_FOLLOWING_PACKET_HEADER_SIZE    1
 134
 135struct nl_first_packet_header {
 136	unsigned char protocol:3;
 137	unsigned char address:3;
 138	unsigned char packet_rank:2;
 139	unsigned char length_lsb;
 140	unsigned char length_msb;
 141};
 142
 143struct nl_packet_header {
 144	unsigned char protocol:3;
 145	unsigned char address:3;
 146	unsigned char packet_rank:2;
 147};
 148
 149/* Value of 'packet_rank' above */
 150#define NL_INTERMEDIATE_PACKET    0x0
 151#define NL_LAST_PACKET            0x1
 152#define NL_FIRST_PACKET           0x2
 153
 154union nl_packet {
 155	/* Network packet header of the first packet (a special case) */
 156	struct nl_first_packet_header hdr_first;
 157	/* Network packet header of the following packets (if any) */
 158	struct nl_packet_header hdr;
 159	/* Complete network packet (header + data) */
 160	unsigned char rawpkt[LL_MTU_MAX];
 161} __attribute__ ((__packed__));
 162
 163#define HW_VERSION_UNKNOWN -1
 164#define HW_VERSION_1 1
 165#define HW_VERSION_2 2
 166
 167/* IPW I/O ports */
 168#define IOIER 0x00		/* Interrupt Enable Register */
 169#define IOIR  0x02		/* Interrupt Source/ACK register */
 170#define IODCR 0x04		/* Data Control Register */
 171#define IODRR 0x06		/* Data Read Register */
 172#define IODWR 0x08		/* Data Write Register */
 173#define IOESR 0x0A		/* Embedded Driver Status Register */
 174#define IORXR 0x0C		/* Rx Fifo Register (Host to Embedded) */
 175#define IOTXR 0x0E		/* Tx Fifo Register (Embedded to Host) */
 176
 177/* I/O ports and bit definitions for version 1 of the hardware */
 178
 179/* IER bits*/
 180#define IER_RXENABLED   0x1
 181#define IER_TXENABLED   0x2
 182
 183/* ISR bits */
 184#define IR_RXINTR       0x1
 185#define IR_TXINTR       0x2
 186
 187/* DCR bits */
 188#define DCR_RXDONE      0x1
 189#define DCR_TXDONE      0x2
 190#define DCR_RXRESET     0x4
 191#define DCR_TXRESET     0x8
 192
 193/* I/O ports and bit definitions for version 2 of the hardware */
 194
 195struct MEMCCR {
 196	unsigned short reg_config_option;	/* PCCOR: Configuration Option Register */
 197	unsigned short reg_config_and_status;	/* PCCSR: Configuration and Status Register */
 198	unsigned short reg_pin_replacement;	/* PCPRR: Pin Replacemant Register */
 199	unsigned short reg_socket_and_copy;	/* PCSCR: Socket and Copy Register */
 200	unsigned short reg_ext_status;		/* PCESR: Extendend Status Register */
 201	unsigned short reg_io_base;		/* PCIOB: I/O Base Register */
 202};
 203
 204struct MEMINFREG {
 205	unsigned short memreg_tx_old;	/* TX Register (R/W) */
 206	unsigned short pad1;
 207	unsigned short memreg_rx_done;	/* RXDone Register (R/W) */
 208	unsigned short pad2;
 209	unsigned short memreg_rx;	/* RX Register (R/W) */
 210	unsigned short pad3;
 211	unsigned short memreg_pc_interrupt_ack;	/* PC intr Ack Register (W) */
 212	unsigned short pad4;
 213	unsigned long memreg_card_present;/* Mask for Host to check (R) for
 214					   * CARD_PRESENT_VALUE */
 215	unsigned short memreg_tx_new;	/* TX2 (new) Register (R/W) */
 216};
 217
 218#define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
 219
 220#define MEMTX_TX                       0x0001
 221#define MEMRX_RX                       0x0001
 222#define MEMRX_RX_DONE                  0x0001
 223#define MEMRX_PCINTACKK                0x0001
 224
 225#define NL_NUM_OF_PRIORITIES       3
 226#define NL_NUM_OF_PROTOCOLS        3
 227#define NL_NUM_OF_ADDRESSES        NO_OF_IPW_CHANNELS
 228
 229struct ipw_hardware {
 230	unsigned int base_port;
 231	short hw_version;
 232	unsigned short ll_mtu;
 233	spinlock_t lock;
 234
 235	int initializing;
 236	int init_loops;
 237	struct timer_list setup_timer;
 238
 239	/* Flag if hw is ready to send next packet */
 240	int tx_ready;
 241	/* Count of pending packets to be sent */
 242	int tx_queued;
 243	struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
 244
 245	int rx_bytes_queued;
 246	struct list_head rx_queue;
 247	/* Pool of rx_packet structures that are not currently used. */
 248	struct list_head rx_pool;
 249	int rx_pool_size;
 250	/* True if reception of data is blocked while userspace processes it. */
 251	int blocking_rx;
 252	/* True if there is RX data ready on the hardware. */
 253	int rx_ready;
 254	unsigned short last_memtx_serial;
 255	/*
 256	 * Newer versions of the V2 card firmware send serial numbers in the
 257	 * MemTX register. 'serial_number_detected' is set true when we detect
 258	 * a non-zero serial number (indicating the new firmware).  Thereafter,
 259	 * the driver can safely ignore the Timer Recovery re-sends to avoid
 260	 * out-of-sync problems.
 261	 */
 262	int serial_number_detected;
 263	struct work_struct work_rx;
 264
 265	/* True if we are to send the set-up data to the hardware. */
 266	int to_setup;
 267
 268	/* Card has been removed */
 269	int removed;
 270	/* Saved irq value when we disable the interrupt. */
 271	int irq;
 272	/* True if this driver is shutting down. */
 273	int shutting_down;
 274	/* Modem control lines */
 275	unsigned int control_lines[NL_NUM_OF_ADDRESSES];
 276	struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
 277
 278	struct tasklet_struct tasklet;
 279
 280	/* The handle for the network layer, for the sending of events to it. */
 281	struct ipw_network *network;
 282	struct MEMINFREG __iomem *memory_info_regs;
 283	struct MEMCCR __iomem *memregs_CCR;
 284	void (*reboot_callback) (void *data);
 285	void *reboot_callback_data;
 286
 287	unsigned short __iomem *memreg_tx;
 288};
 289
 290/*
 291 * Packet info structure for tx packets.
 292 * Note: not all the fields defined here are required for all protocols
 293 */
 294struct ipw_tx_packet {
 295	struct list_head queue;
 296	/* channel idx + 1 */
 297	unsigned char dest_addr;
 298	/* SETUP, CTRL or DATA */
 299	unsigned char protocol;
 300	/* Length of data block, which starts at the end of this structure */
 301	unsigned short length;
 302	/* Sending state */
 303	/* Offset of where we've sent up to so far */
 304	unsigned long offset;
 305	/* Count of packet fragments, starting at 0 */
 306	int fragment_count;
 307
 308	/* Called after packet is sent and before is freed */
 309	void (*packet_callback) (void *cb_data, unsigned int packet_length);
 310	void *callback_data;
 311};
 312
 313/* Signals from DTE */
 314#define COMCTRL_RTS	0
 315#define COMCTRL_DTR	1
 316
 317/* Signals from DCE */
 318#define COMCTRL_CTS	2
 319#define COMCTRL_DCD	3
 320#define COMCTRL_DSR	4
 321#define COMCTRL_RI	5
 322
 323struct ipw_control_packet_body {
 324	/* DTE signal or DCE signal */
 325	unsigned char sig_no;
 326	/* 0: set signal, 1: clear signal */
 327	unsigned char value;
 328} __attribute__ ((__packed__));
 329
 330struct ipw_control_packet {
 331	struct ipw_tx_packet header;
 332	struct ipw_control_packet_body body;
 333};
 334
 335struct ipw_rx_packet {
 336	struct list_head queue;
 337	unsigned int capacity;
 338	unsigned int length;
 339	unsigned int protocol;
 340	unsigned int channel_idx;
 341};
 342
 343static char *data_type(const unsigned char *buf, unsigned length)
 344{
 345	struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
 346
 347	if (length == 0)
 348		return "     ";
 349
 350	if (hdr->packet_rank & NL_FIRST_PACKET) {
 351		switch (hdr->protocol) {
 352		case TL_PROTOCOLID_COM_DATA:	return "DATA ";
 353		case TL_PROTOCOLID_COM_CTRL:	return "CTRL ";
 354		case TL_PROTOCOLID_SETUP:	return "SETUP";
 355		default: return "???? ";
 356		}
 357	} else
 358		return "     ";
 359}
 360
 361#define DUMP_MAX_BYTES 64
 362
 363static void dump_data_bytes(const char *type, const unsigned char *data,
 364			    unsigned length)
 365{
 366	char prefix[56];
 367
 368	sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
 369			type, data_type(data, length));
 370	print_hex_dump_bytes(prefix, 0, (void *)data,
 371			length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
 372}
 373
 374static void swap_packet_bitfield_to_le(unsigned char *data)
 375{
 376#ifdef __BIG_ENDIAN_BITFIELD
 377	unsigned char tmp = *data, ret = 0;
 378
 379	/*
 380	 * transform bits from aa.bbb.ccc to ccc.bbb.aa
 381	 */
 382	ret |= (tmp & 0xc0) >> 6;
 383	ret |= (tmp & 0x38) >> 1;
 384	ret |= (tmp & 0x07) << 5;
 385	*data = ret & 0xff;
 386#endif
 387}
 388
 389static void swap_packet_bitfield_from_le(unsigned char *data)
 390{
 391#ifdef __BIG_ENDIAN_BITFIELD
 392	unsigned char tmp = *data, ret = 0;
 393
 394	/*
 395	 * transform bits from ccc.bbb.aa to aa.bbb.ccc
 396	 */
 397	ret |= (tmp & 0xe0) >> 5;
 398	ret |= (tmp & 0x1c) << 1;
 399	ret |= (tmp & 0x03) << 6;
 400	*data = ret & 0xff;
 401#endif
 402}
 403
 404static void do_send_fragment(struct ipw_hardware *hw, unsigned char *data,
 405			    unsigned length)
 406{
 407	unsigned i;
 408	unsigned long flags;
 409
 410	start_timing();
 411	BUG_ON(length > hw->ll_mtu);
 412
 413	if (ipwireless_debug)
 414		dump_data_bytes("send", data, length);
 415
 416	spin_lock_irqsave(&hw->lock, flags);
 417
 418	hw->tx_ready = 0;
 419	swap_packet_bitfield_to_le(data);
 420
 421	if (hw->hw_version == HW_VERSION_1) {
 422		outw((unsigned short) length, hw->base_port + IODWR);
 423
 424		for (i = 0; i < length; i += 2) {
 425			unsigned short d = data[i];
 426			__le16 raw_data;
 427
 428			if (i + 1 < length)
 429				d |= data[i + 1] << 8;
 430			raw_data = cpu_to_le16(d);
 431			outw(raw_data, hw->base_port + IODWR);
 432		}
 433
 434		outw(DCR_TXDONE, hw->base_port + IODCR);
 435	} else if (hw->hw_version == HW_VERSION_2) {
 436		outw((unsigned short) length, hw->base_port);
 437
 438		for (i = 0; i < length; i += 2) {
 439			unsigned short d = data[i];
 440			__le16 raw_data;
 441
 442			if (i + 1 < length)
 443				d |= data[i + 1] << 8;
 444			raw_data = cpu_to_le16(d);
 445			outw(raw_data, hw->base_port);
 446		}
 447		while ((i & 3) != 2) {
 448			outw((unsigned short) 0xDEAD, hw->base_port);
 449			i += 2;
 450		}
 451		writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
 452	}
 453
 454	spin_unlock_irqrestore(&hw->lock, flags);
 455
 456	end_write_timing(length);
 457}
 458
 459static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
 460{
 461	unsigned short fragment_data_len;
 462	unsigned short data_left = packet->length - packet->offset;
 463	unsigned short header_size;
 464	union nl_packet pkt;
 465
 466	header_size =
 467	    (packet->fragment_count == 0)
 468	    ? NL_FIRST_PACKET_HEADER_SIZE
 469	    : NL_FOLLOWING_PACKET_HEADER_SIZE;
 470	fragment_data_len = hw->ll_mtu - header_size;
 471	if (data_left < fragment_data_len)
 472		fragment_data_len = data_left;
 473
 474	/*
 475	 * hdr_first is now in machine bitfield order, which will be swapped
 476	 * to le just before it goes to hw
 477	 */
 478	pkt.hdr_first.protocol = packet->protocol;
 479	pkt.hdr_first.address = packet->dest_addr;
 480	pkt.hdr_first.packet_rank = 0;
 481
 482	/* First packet? */
 483	if (packet->fragment_count == 0) {
 484		pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
 485		pkt.hdr_first.length_lsb = (unsigned char) packet->length;
 486		pkt.hdr_first.length_msb =
 487			(unsigned char) (packet->length >> 8);
 488	}
 489
 490	memcpy(pkt.rawpkt + header_size,
 491	       ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
 492	       packet->offset, fragment_data_len);
 493	packet->offset += fragment_data_len;
 494	packet->fragment_count++;
 495
 496	/* Last packet? (May also be first packet.) */
 497	if (packet->offset == packet->length)
 498		pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
 499	do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
 500
 501	/* If this packet has unsent data, then re-queue it. */
 502	if (packet->offset < packet->length) {
 503		/*
 504		 * Re-queue it at the head of the highest priority queue so
 505		 * it goes before all other packets
 506		 */
 507		unsigned long flags;
 508
 509		spin_lock_irqsave(&hw->lock, flags);
 510		list_add(&packet->queue, &hw->tx_queue[0]);
 511		hw->tx_queued++;
 512		spin_unlock_irqrestore(&hw->lock, flags);
 513	} else {
 514		if (packet->packet_callback)
 515			packet->packet_callback(packet->callback_data,
 516					packet->length);
 517		kfree(packet);
 518	}
 519}
 520
 521static void ipw_setup_hardware(struct ipw_hardware *hw)
 522{
 523	unsigned long flags;
 524
 525	spin_lock_irqsave(&hw->lock, flags);
 526	if (hw->hw_version == HW_VERSION_1) {
 527		/* Reset RX FIFO */
 528		outw(DCR_RXRESET, hw->base_port + IODCR);
 529		/* SB: Reset TX FIFO */
 530		outw(DCR_TXRESET, hw->base_port + IODCR);
 531
 532		/* Enable TX and RX interrupts. */
 533		outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
 534	} else {
 535		/*
 536		 * Set INTRACK bit (bit 0), which means we must explicitly
 537		 * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
 538		 */
 539		unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
 540
 541		csr |= 1;
 542		writew(csr, &hw->memregs_CCR->reg_config_and_status);
 543	}
 544	spin_unlock_irqrestore(&hw->lock, flags);
 545}
 546
 547/*
 548 * If 'packet' is NULL, then this function allocates a new packet, setting its
 549 * length to 0 and ensuring it has the specified minimum amount of free space.
 550 *
 551 * If 'packet' is not NULL, then this function enlarges it if it doesn't
 552 * have the specified minimum amount of free space.
 553 *
 554 */
 555static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
 556					   struct ipw_rx_packet *packet,
 557					   int minimum_free_space)
 558{
 559
 560	if (!packet) {
 561		unsigned long flags;
 562
 563		spin_lock_irqsave(&hw->lock, flags);
 564		if (!list_empty(&hw->rx_pool)) {
 565			packet = list_first_entry(&hw->rx_pool,
 566					struct ipw_rx_packet, queue);
 567			hw->rx_pool_size--;
 568			spin_unlock_irqrestore(&hw->lock, flags);
 569			list_del(&packet->queue);
 570		} else {
 571			const int min_capacity =
 572				ipwireless_ppp_mru(hw->network) + 2;
 573			int new_capacity;
 574
 575			spin_unlock_irqrestore(&hw->lock, flags);
 576			new_capacity =
 577				(minimum_free_space > min_capacity
 578				 ? minimum_free_space
 579				 : min_capacity);
 580			packet = kmalloc(sizeof(struct ipw_rx_packet)
 581					+ new_capacity, GFP_ATOMIC);
 582			if (!packet)
 583				return NULL;
 584			packet->capacity = new_capacity;
 585		}
 586		packet->length = 0;
 587	}
 588
 589	if (packet->length + minimum_free_space > packet->capacity) {
 590		struct ipw_rx_packet *old_packet = packet;
 591
 592		packet = kmalloc(sizeof(struct ipw_rx_packet) +
 593				old_packet->length + minimum_free_space,
 594				GFP_ATOMIC);
 595		if (!packet) {
 596			kfree(old_packet);
 597			return NULL;
 598		}
 599		memcpy(packet, old_packet,
 600				sizeof(struct ipw_rx_packet)
 601					+ old_packet->length);
 602		packet->capacity = old_packet->length + minimum_free_space;
 603		kfree(old_packet);
 604	}
 605
 606	return packet;
 607}
 608
 609static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
 610{
 611	if (hw->rx_pool_size > 6)
 612		kfree(packet);
 613	else {
 614		hw->rx_pool_size++;
 615		list_add(&packet->queue, &hw->rx_pool);
 616	}
 617}
 618
 619static void queue_received_packet(struct ipw_hardware *hw,
 620				  unsigned int protocol,
 621				  unsigned int address,
 622				  const unsigned char *data, int length,
 623				  int is_last)
 624{
 625	unsigned int channel_idx = address - 1;
 626	struct ipw_rx_packet *packet = NULL;
 627	unsigned long flags;
 628
 629	/* Discard packet if channel index is out of range. */
 630	if (channel_idx >= NL_NUM_OF_ADDRESSES) {
 631		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 632		       ": data packet has bad address %u\n", address);
 633		return;
 634	}
 635
 636	/*
 637	 * ->packet_assembler is safe to touch unlocked, this is the only place
 638	 */
 639	if (protocol == TL_PROTOCOLID_COM_DATA) {
 640		struct ipw_rx_packet **assem =
 641			&hw->packet_assembler[channel_idx];
 642
 643		/*
 644		 * Create a new packet, or assembler already contains one
 645		 * enlarge it by 'length' bytes.
 646		 */
 647		(*assem) = pool_allocate(hw, *assem, length);
 648		if (!(*assem)) {
 649			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
 650				": no memory for incoming data packet, dropped!\n");
 651			return;
 652		}
 653		(*assem)->protocol = protocol;
 654		(*assem)->channel_idx = channel_idx;
 655
 656		/* Append this packet data onto existing data. */
 657		memcpy((unsigned char *)(*assem) +
 658			       sizeof(struct ipw_rx_packet)
 659				+ (*assem)->length, data, length);
 660		(*assem)->length += length;
 661		if (is_last) {
 662			packet = *assem;
 663			*assem = NULL;
 664			/* Count queued DATA bytes only */
 665			spin_lock_irqsave(&hw->lock, flags);
 666			hw->rx_bytes_queued += packet->length;
 667			spin_unlock_irqrestore(&hw->lock, flags);
 668		}
 669	} else {
 670		/* If it's a CTRL packet, don't assemble, just queue it. */
 671		packet = pool_allocate(hw, NULL, length);
 672		if (!packet) {
 673			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
 674				": no memory for incoming ctrl packet, dropped!\n");
 675			return;
 676		}
 677		packet->protocol = protocol;
 678		packet->channel_idx = channel_idx;
 679		memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
 680				data, length);
 681		packet->length = length;
 682	}
 683
 684	/*
 685	 * If this is the last packet, then send the assembled packet on to the
 686	 * network layer.
 687	 */
 688	if (packet) {
 689		spin_lock_irqsave(&hw->lock, flags);
 690		list_add_tail(&packet->queue, &hw->rx_queue);
 691		/* Block reception of incoming packets if queue is full. */
 692		hw->blocking_rx =
 693			(hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
 694
 695		spin_unlock_irqrestore(&hw->lock, flags);
 696		schedule_work(&hw->work_rx);
 697	}
 698}
 699
 700/*
 701 * Workqueue callback
 702 */
 703static void ipw_receive_data_work(struct work_struct *work_rx)
 704{
 705	struct ipw_hardware *hw =
 706	    container_of(work_rx, struct ipw_hardware, work_rx);
 707	unsigned long flags;
 708
 709	spin_lock_irqsave(&hw->lock, flags);
 710	while (!list_empty(&hw->rx_queue)) {
 711		struct ipw_rx_packet *packet =
 712			list_first_entry(&hw->rx_queue,
 713					struct ipw_rx_packet, queue);
 714
 715		if (hw->shutting_down)
 716			break;
 717		list_del(&packet->queue);
 718
 719		/*
 720		 * Note: ipwireless_network_packet_received must be called in a
 721		 * process context (i.e. via schedule_work) because the tty
 722		 * output code can sleep in the tty_flip_buffer_push call.
 723		 */
 724		if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
 725			if (hw->network != NULL) {
 726				/* If the network hasn't been disconnected. */
 727				spin_unlock_irqrestore(&hw->lock, flags);
 728				/*
 729				 * This must run unlocked due to tty processing
 730				 * and mutex locking
 731				 */
 732				ipwireless_network_packet_received(
 733						hw->network,
 734						packet->channel_idx,
 735						(unsigned char *)packet
 736						+ sizeof(struct ipw_rx_packet),
 737						packet->length);
 738				spin_lock_irqsave(&hw->lock, flags);
 739			}
 740			/* Count queued DATA bytes only */
 741			hw->rx_bytes_queued -= packet->length;
 742		} else {
 743			/*
 744			 * This is safe to be called locked, callchain does
 745			 * not block
 746			 */
 747			handle_received_CTRL_packet(hw, packet->channel_idx,
 748					(unsigned char *)packet
 749					+ sizeof(struct ipw_rx_packet),
 750					packet->length);
 751		}
 752		pool_free(hw, packet);
 753		/*
 754		 * Unblock reception of incoming packets if queue is no longer
 755		 * full.
 756		 */
 757		hw->blocking_rx =
 758			hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
 759		if (hw->shutting_down)
 760			break;
 761	}
 762	spin_unlock_irqrestore(&hw->lock, flags);
 763}
 764
 765static void handle_received_CTRL_packet(struct ipw_hardware *hw,
 766					unsigned int channel_idx,
 767					const unsigned char *data, int len)
 768{
 769	const struct ipw_control_packet_body *body =
 770		(const struct ipw_control_packet_body *) data;
 771	unsigned int changed_mask;
 772
 773	if (len != sizeof(struct ipw_control_packet_body)) {
 774		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 775		       ": control packet was %d bytes - wrong size!\n",
 776		       len);
 777		return;
 778	}
 779
 780	switch (body->sig_no) {
 781	case COMCTRL_CTS:
 782		changed_mask = IPW_CONTROL_LINE_CTS;
 783		break;
 784	case COMCTRL_DCD:
 785		changed_mask = IPW_CONTROL_LINE_DCD;
 786		break;
 787	case COMCTRL_DSR:
 788		changed_mask = IPW_CONTROL_LINE_DSR;
 789		break;
 790	case COMCTRL_RI:
 791		changed_mask = IPW_CONTROL_LINE_RI;
 792		break;
 793	default:
 794		changed_mask = 0;
 795	}
 796
 797	if (changed_mask != 0) {
 798		if (body->value)
 799			hw->control_lines[channel_idx] |= changed_mask;
 800		else
 801			hw->control_lines[channel_idx] &= ~changed_mask;
 802		if (hw->network)
 803			ipwireless_network_notify_control_line_change(
 804					hw->network,
 805					channel_idx,
 806					hw->control_lines[channel_idx],
 807					changed_mask);
 808	}
 809}
 810
 811static void handle_received_packet(struct ipw_hardware *hw,
 812				   const union nl_packet *packet,
 813				   unsigned short len)
 814{
 815	unsigned int protocol = packet->hdr.protocol;
 816	unsigned int address = packet->hdr.address;
 817	unsigned int header_length;
 818	const unsigned char *data;
 819	unsigned int data_len;
 820	int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
 821
 822	if (packet->hdr.packet_rank & NL_FIRST_PACKET)
 823		header_length = NL_FIRST_PACKET_HEADER_SIZE;
 824	else
 825		header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
 826
 827	data = packet->rawpkt + header_length;
 828	data_len = len - header_length;
 829	switch (protocol) {
 830	case TL_PROTOCOLID_COM_DATA:
 831	case TL_PROTOCOLID_COM_CTRL:
 832		queue_received_packet(hw, protocol, address, data, data_len,
 833				is_last);
 834		break;
 835	case TL_PROTOCOLID_SETUP:
 836		handle_received_SETUP_packet(hw, address, data, data_len,
 837				is_last);
 838		break;
 839	}
 840}
 841
 842static void acknowledge_data_read(struct ipw_hardware *hw)
 843{
 844	if (hw->hw_version == HW_VERSION_1)
 845		outw(DCR_RXDONE, hw->base_port + IODCR);
 846	else
 847		writew(MEMRX_PCINTACKK,
 848				&hw->memory_info_regs->memreg_pc_interrupt_ack);
 849}
 850
 851/*
 852 * Retrieve a packet from the IPW hardware.
 853 */
 854static void do_receive_packet(struct ipw_hardware *hw)
 855{
 856	unsigned len;
 857	unsigned i;
 858	unsigned char pkt[LL_MTU_MAX];
 859
 860	start_timing();
 861
 862	if (hw->hw_version == HW_VERSION_1) {
 863		len = inw(hw->base_port + IODRR);
 864		if (len > hw->ll_mtu) {
 865			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 866			       ": received a packet of %u bytes - longer than the MTU!\n", len);
 867			outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
 868			return;
 869		}
 870
 871		for (i = 0; i < len; i += 2) {
 872			__le16 raw_data = inw(hw->base_port + IODRR);
 873			unsigned short data = le16_to_cpu(raw_data);
 874
 875			pkt[i] = (unsigned char) data;
 876			pkt[i + 1] = (unsigned char) (data >> 8);
 877		}
 878	} else {
 879		len = inw(hw->base_port);
 880		if (len > hw->ll_mtu) {
 881			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 882			       ": received a packet of %u bytes - longer than the MTU!\n", len);
 883			writew(MEMRX_PCINTACKK,
 884				&hw->memory_info_regs->memreg_pc_interrupt_ack);
 885			return;
 886		}
 887
 888		for (i = 0; i < len; i += 2) {
 889			__le16 raw_data = inw(hw->base_port);
 890			unsigned short data = le16_to_cpu(raw_data);
 891
 892			pkt[i] = (unsigned char) data;
 893			pkt[i + 1] = (unsigned char) (data >> 8);
 894		}
 895
 896		while ((i & 3) != 2) {
 897			inw(hw->base_port);
 898			i += 2;
 899		}
 900	}
 901
 902	acknowledge_data_read(hw);
 903
 904	swap_packet_bitfield_from_le(pkt);
 905
 906	if (ipwireless_debug)
 907		dump_data_bytes("recv", pkt, len);
 908
 909	handle_received_packet(hw, (union nl_packet *) pkt, len);
 910
 911	end_read_timing(len);
 912}
 913
 914static int get_current_packet_priority(struct ipw_hardware *hw)
 915{
 916	/*
 917	 * If we're initializing, don't send anything of higher priority than
 918	 * PRIO_SETUP.  The network layer therefore need not care about
 919	 * hardware initialization - any of its stuff will simply be queued
 920	 * until setup is complete.
 921	 */
 922	return (hw->to_setup || hw->initializing
 923			? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
 924}
 925
 926/*
 927 * return 1 if something has been received from hw
 928 */
 929static int get_packets_from_hw(struct ipw_hardware *hw)
 930{
 931	int received = 0;
 932	unsigned long flags;
 933
 934	spin_lock_irqsave(&hw->lock, flags);
 935	while (hw->rx_ready && !hw->blocking_rx) {
 936		received = 1;
 937		hw->rx_ready--;
 938		spin_unlock_irqrestore(&hw->lock, flags);
 939
 940		do_receive_packet(hw);
 941
 942		spin_lock_irqsave(&hw->lock, flags);
 943	}
 944	spin_unlock_irqrestore(&hw->lock, flags);
 945
 946	return received;
 947}
 948
 949/*
 950 * Send pending packet up to given priority, prioritize SETUP data until
 951 * hardware is fully setup.
 952 *
 953 * return 1 if more packets can be sent
 954 */
 955static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
 956{
 957	int more_to_send = 0;
 958	unsigned long flags;
 959
 960	spin_lock_irqsave(&hw->lock, flags);
 961	if (hw->tx_queued && hw->tx_ready) {
 962		int priority;
 963		struct ipw_tx_packet *packet = NULL;
 964
 965		/* Pick a packet */
 966		for (priority = 0; priority < priority_limit; priority++) {
 967			if (!list_empty(&hw->tx_queue[priority])) {
 968				packet = list_first_entry(
 969						&hw->tx_queue[priority],
 970						struct ipw_tx_packet,
 971						queue);
 972
 973				hw->tx_queued--;
 974				list_del(&packet->queue);
 975
 976				break;
 977			}
 978		}
 979		if (!packet) {
 980			hw->tx_queued = 0;
 981			spin_unlock_irqrestore(&hw->lock, flags);
 982			return 0;
 983		}
 984
 985		spin_unlock_irqrestore(&hw->lock, flags);
 986
 987		/* Send */
 988		do_send_packet(hw, packet);
 989
 990		/* Check if more to send */
 991		spin_lock_irqsave(&hw->lock, flags);
 992		for (priority = 0; priority < priority_limit; priority++)
 993			if (!list_empty(&hw->tx_queue[priority])) {
 994				more_to_send = 1;
 995				break;
 996			}
 997
 998		if (!more_to_send)
 999			hw->tx_queued = 0;
1000	}
1001	spin_unlock_irqrestore(&hw->lock, flags);
1002
1003	return more_to_send;
1004}
1005
1006/*
1007 * Send and receive all queued packets.
1008 */
1009static void ipwireless_do_tasklet(struct tasklet_struct *t)
1010{
1011	struct ipw_hardware *hw = from_tasklet(hw, t, tasklet);
1012	unsigned long flags;
1013
1014	spin_lock_irqsave(&hw->lock, flags);
1015	if (hw->shutting_down) {
1016		spin_unlock_irqrestore(&hw->lock, flags);
1017		return;
1018	}
1019
1020	if (hw->to_setup == 1) {
1021		/*
1022		 * Initial setup data sent to hardware
1023		 */
1024		hw->to_setup = 2;
1025		spin_unlock_irqrestore(&hw->lock, flags);
1026
1027		ipw_setup_hardware(hw);
1028		ipw_send_setup_packet(hw);
1029
1030		send_pending_packet(hw, PRIO_SETUP + 1);
1031		get_packets_from_hw(hw);
1032	} else {
1033		int priority_limit = get_current_packet_priority(hw);
1034		int again;
1035
1036		spin_unlock_irqrestore(&hw->lock, flags);
1037
1038		do {
1039			again = send_pending_packet(hw, priority_limit);
1040			again |= get_packets_from_hw(hw);
1041		} while (again);
1042	}
1043}
1044
1045/*
1046 * return true if the card is physically present.
1047 */
1048static int is_card_present(struct ipw_hardware *hw)
1049{
1050	if (hw->hw_version == HW_VERSION_1)
1051		return inw(hw->base_port + IOIR) != 0xFFFF;
1052	else
1053		return readl(&hw->memory_info_regs->memreg_card_present) ==
1054		    CARD_PRESENT_VALUE;
1055}
1056
1057static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1058						  struct ipw_hardware *hw)
1059{
1060	unsigned short irqn;
1061
1062	irqn = inw(hw->base_port + IOIR);
1063
1064	/* Check if card is present */
1065	if (irqn == 0xFFFF)
1066		return IRQ_NONE;
1067	else if (irqn != 0) {
1068		unsigned short ack = 0;
1069		unsigned long flags;
1070
1071		/* Transmit complete. */
1072		if (irqn & IR_TXINTR) {
1073			ack |= IR_TXINTR;
1074			spin_lock_irqsave(&hw->lock, flags);
1075			hw->tx_ready = 1;
1076			spin_unlock_irqrestore(&hw->lock, flags);
1077		}
1078		/* Received data */
1079		if (irqn & IR_RXINTR) {
1080			ack |= IR_RXINTR;
1081			spin_lock_irqsave(&hw->lock, flags);
1082			hw->rx_ready++;
1083			spin_unlock_irqrestore(&hw->lock, flags);
1084		}
1085		if (ack != 0) {
1086			outw(ack, hw->base_port + IOIR);
1087			tasklet_schedule(&hw->tasklet);
1088		}
1089		return IRQ_HANDLED;
1090	}
1091	return IRQ_NONE;
1092}
1093
1094static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1095{
1096	unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1097
1098	csr &= 0xfffd;
1099	writew(csr, &hw->memregs_CCR->reg_config_and_status);
1100}
1101
1102static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1103						     struct ipw_hardware *hw)
1104{
1105	int tx = 0;
1106	int rx = 0;
1107	int rx_repeat = 0;
1108	int try_mem_tx_old;
1109	unsigned long flags;
1110
1111	do {
1112
1113	unsigned short memtx = readw(hw->memreg_tx);
1114	unsigned short memtx_serial;
1115	unsigned short memrxdone =
1116		readw(&hw->memory_info_regs->memreg_rx_done);
1117
1118	try_mem_tx_old = 0;
1119
1120	/* check whether the interrupt was generated by ipwireless card */
1121	if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1122
1123		/* check if the card uses memreg_tx_old register */
1124		if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1125			memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1126			if (memtx & MEMTX_TX) {
1127				printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1128					": Using memreg_tx_old\n");
1129				hw->memreg_tx =
1130					&hw->memory_info_regs->memreg_tx_old;
1131			} else {
1132				return IRQ_NONE;
1133			}
1134		} else
1135			return IRQ_NONE;
1136	}
1137
1138	/*
1139	 * See if the card is physically present. Note that while it is
1140	 * powering up, it appears not to be present.
1141	 */
1142	if (!is_card_present(hw)) {
1143		acknowledge_pcmcia_interrupt(hw);
1144		return IRQ_HANDLED;
1145	}
1146
1147	memtx_serial = memtx & (unsigned short) 0xff00;
1148	if (memtx & MEMTX_TX) {
1149		writew(memtx_serial, hw->memreg_tx);
1150
1151		if (hw->serial_number_detected) {
1152			if (memtx_serial != hw->last_memtx_serial) {
1153				hw->last_memtx_serial = memtx_serial;
1154				spin_lock_irqsave(&hw->lock, flags);
1155				hw->rx_ready++;
1156				spin_unlock_irqrestore(&hw->lock, flags);
1157				rx = 1;
1158			} else
1159				/* Ignore 'Timer Recovery' duplicates. */
1160				rx_repeat = 1;
1161		} else {
1162			/*
1163			 * If a non-zero serial number is seen, then enable
1164			 * serial number checking.
1165			 */
1166			if (memtx_serial != 0) {
1167				hw->serial_number_detected = 1;
1168				printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1169					": memreg_tx serial num detected\n");
1170
1171				spin_lock_irqsave(&hw->lock, flags);
1172				hw->rx_ready++;
1173				spin_unlock_irqrestore(&hw->lock, flags);
1174			}
1175			rx = 1;
1176		}
1177	}
1178	if (memrxdone & MEMRX_RX_DONE) {
1179		writew(0, &hw->memory_info_regs->memreg_rx_done);
1180		spin_lock_irqsave(&hw->lock, flags);
1181		hw->tx_ready = 1;
1182		spin_unlock_irqrestore(&hw->lock, flags);
1183		tx = 1;
1184	}
1185	if (tx)
1186		writew(MEMRX_PCINTACKK,
1187				&hw->memory_info_regs->memreg_pc_interrupt_ack);
1188
1189	acknowledge_pcmcia_interrupt(hw);
1190
1191	if (tx || rx)
1192		tasklet_schedule(&hw->tasklet);
1193	else if (!rx_repeat) {
1194		if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1195			if (hw->serial_number_detected)
1196				printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1197					": spurious interrupt - new_tx mode\n");
1198			else {
1199				printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1200					": no valid memreg_tx value - switching to the old memreg_tx\n");
1201				hw->memreg_tx =
1202					&hw->memory_info_regs->memreg_tx_old;
1203				try_mem_tx_old = 1;
1204			}
1205		} else
1206			printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1207					": spurious interrupt - old_tx mode\n");
1208	}
1209
1210	} while (try_mem_tx_old == 1);
1211
1212	return IRQ_HANDLED;
1213}
1214
1215irqreturn_t ipwireless_interrupt(int irq, void *dev_id)
1216{
1217	struct ipw_dev *ipw = dev_id;
1218
1219	if (ipw->hardware->hw_version == HW_VERSION_1)
1220		return ipwireless_handle_v1_interrupt(irq, ipw->hardware);
1221	else
1222		return ipwireless_handle_v2_v3_interrupt(irq, ipw->hardware);
1223}
1224
1225static void flush_packets_to_hw(struct ipw_hardware *hw)
1226{
1227	int priority_limit;
1228	unsigned long flags;
1229
1230	spin_lock_irqsave(&hw->lock, flags);
1231	priority_limit = get_current_packet_priority(hw);
1232	spin_unlock_irqrestore(&hw->lock, flags);
1233
1234	while (send_pending_packet(hw, priority_limit));
1235}
1236
1237static void send_packet(struct ipw_hardware *hw, int priority,
1238			struct ipw_tx_packet *packet)
1239{
1240	unsigned long flags;
1241
1242	spin_lock_irqsave(&hw->lock, flags);
1243	list_add_tail(&packet->queue, &hw->tx_queue[priority]);
1244	hw->tx_queued++;
1245	spin_unlock_irqrestore(&hw->lock, flags);
1246
1247	flush_packets_to_hw(hw);
1248}
1249
1250/* Create data packet, non-atomic allocation */
1251static void *alloc_data_packet(int data_size,
1252				unsigned char dest_addr,
1253				unsigned char protocol)
1254{
1255	struct ipw_tx_packet *packet = kzalloc(
1256			sizeof(struct ipw_tx_packet) + data_size,
1257			GFP_ATOMIC);
1258
1259	if (!packet)
1260		return NULL;
1261
1262	INIT_LIST_HEAD(&packet->queue);
1263	packet->dest_addr = dest_addr;
1264	packet->protocol = protocol;
1265	packet->length = data_size;
1266
1267	return packet;
1268}
1269
1270static void *alloc_ctrl_packet(int header_size,
1271			       unsigned char dest_addr,
1272			       unsigned char protocol,
1273			       unsigned char sig_no)
1274{
1275	/*
1276	 * sig_no is located right after ipw_tx_packet struct in every
1277	 * CTRL or SETUP packets, we can use ipw_control_packet as a
1278	 * common struct
1279	 */
1280	struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1281
1282	if (!packet)
1283		return NULL;
1284
1285	INIT_LIST_HEAD(&packet->header.queue);
1286	packet->header.dest_addr = dest_addr;
1287	packet->header.protocol = protocol;
1288	packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1289	packet->body.sig_no = sig_no;
1290
1291	return packet;
1292}
1293
1294int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
1295			    const u8 *data, unsigned int length,
1296			    void (*callback) (void *cb, unsigned int length),
1297			    void *callback_data)
1298{
1299	struct ipw_tx_packet *packet;
1300
1301	packet = alloc_data_packet(length, (channel_idx + 1),
1302			TL_PROTOCOLID_COM_DATA);
1303	if (!packet)
1304		return -ENOMEM;
1305	packet->packet_callback = callback;
1306	packet->callback_data = callback_data;
1307	memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
1308			length);
1309
1310	send_packet(hw, PRIO_DATA, packet);
1311	return 0;
1312}
1313
1314static int set_control_line(struct ipw_hardware *hw, int prio,
1315			   unsigned int channel_idx, int line, int state)
1316{
1317	struct ipw_control_packet *packet;
1318	int protocolid = TL_PROTOCOLID_COM_CTRL;
1319
1320	if (prio == PRIO_SETUP)
1321		protocolid = TL_PROTOCOLID_SETUP;
1322
1323	packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
1324			(channel_idx + 1), protocolid, line);
1325	if (!packet)
1326		return -ENOMEM;
1327	packet->header.length = sizeof(struct ipw_control_packet_body);
1328	packet->body.value = (state == 0 ? 0 : 1);
1329	send_packet(hw, prio, &packet->header);
1330	return 0;
1331}
1332
1333
1334static int set_DTR(struct ipw_hardware *hw, int priority,
1335		   unsigned int channel_idx, int state)
1336{
1337	if (state != 0)
1338		hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1339	else
1340		hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1341
1342	return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1343}
1344
1345static int set_RTS(struct ipw_hardware *hw, int priority,
1346		   unsigned int channel_idx, int state)
1347{
1348	if (state != 0)
1349		hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1350	else
1351		hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1352
1353	return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1354}
1355
1356int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1357		       int state)
1358{
1359	return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1360}
1361
1362int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1363		       int state)
1364{
1365	return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1366}
1367
1368struct ipw_setup_get_version_query_packet {
1369	struct ipw_tx_packet header;
1370	struct tl_setup_get_version_qry body;
1371};
1372
1373struct ipw_setup_config_packet {
1374	struct ipw_tx_packet header;
1375	struct tl_setup_config_msg body;
1376};
1377
1378struct ipw_setup_config_done_packet {
1379	struct ipw_tx_packet header;
1380	struct tl_setup_config_done_msg body;
1381};
1382
1383struct ipw_setup_open_packet {
1384	struct ipw_tx_packet header;
1385	struct tl_setup_open_msg body;
1386};
1387
1388struct ipw_setup_info_packet {
1389	struct ipw_tx_packet header;
1390	struct tl_setup_info_msg body;
1391};
1392
1393struct ipw_setup_reboot_msg_ack {
1394	struct ipw_tx_packet header;
1395	struct TlSetupRebootMsgAck body;
1396};
1397
1398/* This handles the actual initialization of the card */
1399static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1400{
1401	struct ipw_setup_config_packet *config_packet;
1402	struct ipw_setup_config_done_packet *config_done_packet;
1403	struct ipw_setup_open_packet *open_packet;
1404	struct ipw_setup_info_packet *info_packet;
1405	int port;
1406	unsigned int channel_idx;
1407
1408	/* generate config packet */
1409	for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1410		config_packet = alloc_ctrl_packet(
1411				sizeof(struct ipw_setup_config_packet),
1412				ADDR_SETUP_PROT,
1413				TL_PROTOCOLID_SETUP,
1414				TL_SETUP_SIGNO_CONFIG_MSG);
1415		if (!config_packet)
1416			goto exit_nomem;
1417		config_packet->header.length = sizeof(struct tl_setup_config_msg);
1418		config_packet->body.port_no = port;
1419		config_packet->body.prio_data = PRIO_DATA;
1420		config_packet->body.prio_ctrl = PRIO_CTRL;
1421		send_packet(hw, PRIO_SETUP, &config_packet->header);
1422	}
1423	config_done_packet = alloc_ctrl_packet(
1424			sizeof(struct ipw_setup_config_done_packet),
1425			ADDR_SETUP_PROT,
1426			TL_PROTOCOLID_SETUP,
1427			TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1428	if (!config_done_packet)
1429		goto exit_nomem;
1430	config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1431	send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1432
1433	/* generate open packet */
1434	for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1435		open_packet = alloc_ctrl_packet(
1436				sizeof(struct ipw_setup_open_packet),
1437				ADDR_SETUP_PROT,
1438				TL_PROTOCOLID_SETUP,
1439				TL_SETUP_SIGNO_OPEN_MSG);
1440		if (!open_packet)
1441			goto exit_nomem;
1442		open_packet->header.length = sizeof(struct tl_setup_open_msg);
1443		open_packet->body.port_no = port;
1444		send_packet(hw, PRIO_SETUP, &open_packet->header);
1445	}
1446	for (channel_idx = 0;
1447			channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1448		int ret;
1449
1450		ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1451			(hw->control_lines[channel_idx] &
1452			 IPW_CONTROL_LINE_DTR) != 0);
1453		if (ret) {
1454			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1455					": error setting DTR (%d)\n", ret);
1456			return;
1457		}
1458
1459		ret = set_RTS(hw, PRIO_SETUP, channel_idx,
1460			(hw->control_lines [channel_idx] &
1461			 IPW_CONTROL_LINE_RTS) != 0);
1462		if (ret) {
1463			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1464					": error setting RTS (%d)\n", ret);
1465			return;
1466		}
1467	}
1468	/*
1469	 * For NDIS we assume that we are using sync PPP frames, for COM async.
1470	 * This driver uses NDIS mode too. We don't bother with translation
1471	 * from async -> sync PPP.
1472	 */
1473	info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1474			ADDR_SETUP_PROT,
1475			TL_PROTOCOLID_SETUP,
1476			TL_SETUP_SIGNO_INFO_MSG);
1477	if (!info_packet)
1478		goto exit_nomem;
1479	info_packet->header.length = sizeof(struct tl_setup_info_msg);
1480	info_packet->body.driver_type = NDISWAN_DRIVER;
1481	info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1482	info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1483	send_packet(hw, PRIO_SETUP, &info_packet->header);
1484
1485	/* Initialization is now complete, so we clear the 'to_setup' flag */
1486	hw->to_setup = 0;
1487
1488	return;
1489
1490exit_nomem:
1491	printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1492			": not enough memory to alloc control packet\n");
1493	hw->to_setup = -1;
1494}
1495
1496static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1497		unsigned char vers_no)
1498{
1499	del_timer(&hw->setup_timer);
1500	hw->initializing = 0;
1501	printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1502
1503	if (vers_no == TL_SETUP_VERSION)
1504		__handle_setup_get_version_rsp(hw);
1505	else
1506		printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1507				": invalid hardware version no %u\n",
1508				(unsigned int) vers_no);
1509}
1510
1511static void ipw_send_setup_packet(struct ipw_hardware *hw)
1512{
1513	struct ipw_setup_get_version_query_packet *ver_packet;
1514
1515	ver_packet = alloc_ctrl_packet(
1516			sizeof(struct ipw_setup_get_version_query_packet),
1517			ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1518			TL_SETUP_SIGNO_GET_VERSION_QRY);
1519	if (!ver_packet)
1520		return;
1521	ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1522
1523	/*
1524	 * Response is handled in handle_received_SETUP_packet
1525	 */
1526	send_packet(hw, PRIO_SETUP, &ver_packet->header);
1527}
1528
1529static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1530					 unsigned int address,
1531					 const unsigned char *data, int len,
1532					 int is_last)
1533{
1534	const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
1535
1536	if (address != ADDR_SETUP_PROT) {
1537		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1538		       ": setup packet has bad address %d\n", address);
1539		return;
1540	}
1541
1542	switch (rx_msg->sig_no) {
1543	case TL_SETUP_SIGNO_GET_VERSION_RSP:
1544		if (hw->to_setup)
1545			handle_setup_get_version_rsp(hw,
1546					rx_msg->version_rsp_msg.version);
1547		break;
1548
1549	case TL_SETUP_SIGNO_OPEN_MSG:
1550		if (ipwireless_debug) {
1551			unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1552
1553			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1554			       ": OPEN_MSG [channel %u] reply received\n",
1555			       channel_idx);
1556		}
1557		break;
1558
1559	case TL_SETUP_SIGNO_INFO_MSG_ACK:
1560		if (ipwireless_debug)
1561			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1562			       ": card successfully configured as NDISWAN\n");
1563		break;
1564
1565	case TL_SETUP_SIGNO_REBOOT_MSG:
1566		if (hw->to_setup)
1567			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1568			       ": Setup not completed - ignoring reboot msg\n");
1569		else {
1570			struct ipw_setup_reboot_msg_ack *packet;
1571
1572			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1573			       ": Acknowledging REBOOT message\n");
1574			packet = alloc_ctrl_packet(
1575					sizeof(struct ipw_setup_reboot_msg_ack),
1576					ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1577					TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1578			if (!packet) {
1579				pr_err(IPWIRELESS_PCCARD_NAME
1580				       ": Not enough memory to send reboot packet");
1581				break;
1582			}
1583			packet->header.length =
1584				sizeof(struct TlSetupRebootMsgAck);
1585			send_packet(hw, PRIO_SETUP, &packet->header);
1586			if (hw->reboot_callback)
1587				hw->reboot_callback(hw->reboot_callback_data);
1588		}
1589		break;
1590
1591	default:
1592		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1593		       ": unknown setup message %u received\n",
1594		       (unsigned int) rx_msg->sig_no);
1595	}
1596}
1597
1598static void do_close_hardware(struct ipw_hardware *hw)
1599{
1600	unsigned int irqn;
1601
1602	if (hw->hw_version == HW_VERSION_1) {
1603		/* Disable TX and RX interrupts. */
1604		outw(0, hw->base_port + IOIER);
1605
1606		/* Acknowledge any outstanding interrupt requests */
1607		irqn = inw(hw->base_port + IOIR);
1608		if (irqn & IR_TXINTR)
1609			outw(IR_TXINTR, hw->base_port + IOIR);
1610		if (irqn & IR_RXINTR)
1611			outw(IR_RXINTR, hw->base_port + IOIR);
1612
1613		synchronize_irq(hw->irq);
1614	}
1615}
1616
1617struct ipw_hardware *ipwireless_hardware_create(void)
1618{
1619	int i;
1620	struct ipw_hardware *hw =
1621		kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1622
1623	if (!hw)
1624		return NULL;
1625
1626	hw->irq = -1;
1627	hw->initializing = 1;
1628	hw->tx_ready = 1;
1629	hw->rx_bytes_queued = 0;
1630	hw->rx_pool_size = 0;
1631	hw->last_memtx_serial = (unsigned short) 0xffff;
1632	for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1633		INIT_LIST_HEAD(&hw->tx_queue[i]);
1634
1635	INIT_LIST_HEAD(&hw->rx_queue);
1636	INIT_LIST_HEAD(&hw->rx_pool);
1637	spin_lock_init(&hw->lock);
1638	tasklet_setup(&hw->tasklet, ipwireless_do_tasklet);
1639	INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1640	timer_setup(&hw->setup_timer, ipwireless_setup_timer, 0);
 
1641
1642	return hw;
1643}
1644
1645void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1646		unsigned int base_port,
1647		void __iomem *attr_memory,
1648		void __iomem *common_memory,
1649		int is_v2_card,
1650		void (*reboot_callback) (void *data),
1651		void *reboot_callback_data)
1652{
1653	if (hw->removed) {
1654		hw->removed = 0;
1655		enable_irq(hw->irq);
1656	}
1657	hw->base_port = base_port;
1658	hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
1659	hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
1660	hw->memregs_CCR = (struct MEMCCR __iomem *)
1661			((unsigned short __iomem *) attr_memory + 0x200);
1662	hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1663	hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1664	hw->reboot_callback = reboot_callback;
1665	hw->reboot_callback_data = reboot_callback_data;
1666}
1667
1668void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1669{
1670	hw->initializing = 1;
1671	hw->init_loops = 0;
1672	printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1673	       ": waiting for card to start up...\n");
1674	ipwireless_setup_timer(&hw->setup_timer);
1675}
1676
1677static void ipwireless_setup_timer(struct timer_list *t)
1678{
1679	struct ipw_hardware *hw = from_timer(hw, t, setup_timer);
1680
1681	hw->init_loops++;
1682
1683	if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1684			hw->hw_version == HW_VERSION_2 &&
1685			hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1686		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1687				": failed to startup using TX2, trying TX\n");
1688
1689		hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1690		hw->init_loops = 0;
1691	}
1692	/* Give up after a certain number of retries */
1693	if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1694		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1695		       ": card failed to start up!\n");
1696		hw->initializing = 0;
1697	} else {
1698		/* Do not attempt to write to the board if it is not present. */
1699		if (is_card_present(hw)) {
1700			unsigned long flags;
1701
1702			spin_lock_irqsave(&hw->lock, flags);
1703			hw->to_setup = 1;
1704			hw->tx_ready = 1;
1705			spin_unlock_irqrestore(&hw->lock, flags);
1706			tasklet_schedule(&hw->tasklet);
1707		}
1708
1709		mod_timer(&hw->setup_timer,
1710			jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1711	}
1712}
1713
1714/*
1715 * Stop any interrupts from executing so that, once this function returns,
1716 * other layers of the driver can be sure they won't get any more callbacks.
1717 * Thus must be called on a proper process context.
1718 */
1719void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1720{
1721	if (!hw->shutting_down) {
1722		/* Tell everyone we are going down. */
1723		hw->shutting_down = 1;
1724		del_timer(&hw->setup_timer);
1725
1726		/* Prevent the hardware from sending any more interrupts */
1727		do_close_hardware(hw);
1728	}
1729}
1730
1731void ipwireless_hardware_free(struct ipw_hardware *hw)
1732{
1733	int i;
1734	struct ipw_rx_packet *rp, *rq;
1735	struct ipw_tx_packet *tp, *tq;
1736
1737	ipwireless_stop_interrupts(hw);
1738
1739	flush_work(&hw->work_rx);
1740
1741	for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
1742		kfree(hw->packet_assembler[i]);
1743
1744	for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1745		list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
1746			list_del(&tp->queue);
1747			kfree(tp);
1748		}
1749
1750	list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
1751		list_del(&rp->queue);
1752		kfree(rp);
1753	}
1754
1755	list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
1756		list_del(&rp->queue);
1757		kfree(rp);
1758	}
1759	kfree(hw);
1760}
1761
1762/*
1763 * Associate the specified network with this hardware, so it will receive events
1764 * from it.
1765 */
1766void ipwireless_associate_network(struct ipw_hardware *hw,
1767				  struct ipw_network *network)
1768{
1769	hw->network = network;
1770}
v4.10.11
 
   1/*
   2 * IPWireless 3G PCMCIA Network Driver
   3 *
   4 * Original code
   5 *   by Stephen Blackheath <stephen@blacksapphire.com>,
   6 *      Ben Martel <benm@symmetric.co.nz>
   7 *
   8 * Copyrighted as follows:
   9 *   Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
  10 *
  11 * Various driver changes and rewrites, port to new kernels
  12 *   Copyright (C) 2006-2007 Jiri Kosina
  13 *
  14 * Misc code cleanups and updates
  15 *   Copyright (C) 2007 David Sterba
  16 */
  17
  18#include <linux/interrupt.h>
  19#include <linux/io.h>
  20#include <linux/irq.h>
  21#include <linux/kernel.h>
  22#include <linux/list.h>
  23#include <linux/slab.h>
  24
  25#include "hardware.h"
  26#include "setup_protocol.h"
  27#include "network.h"
  28#include "main.h"
  29
  30static void ipw_send_setup_packet(struct ipw_hardware *hw);
  31static void handle_received_SETUP_packet(struct ipw_hardware *ipw,
  32					 unsigned int address,
  33					 const unsigned char *data, int len,
  34					 int is_last);
  35static void ipwireless_setup_timer(unsigned long data);
  36static void handle_received_CTRL_packet(struct ipw_hardware *hw,
  37		unsigned int channel_idx, const unsigned char *data, int len);
  38
  39/*#define TIMING_DIAGNOSTICS*/
  40
  41#ifdef TIMING_DIAGNOSTICS
  42
  43static struct timing_stats {
  44	unsigned long last_report_time;
  45	unsigned long read_time;
  46	unsigned long write_time;
  47	unsigned long read_bytes;
  48	unsigned long write_bytes;
  49	unsigned long start_time;
  50};
  51
  52static void start_timing(void)
  53{
  54	timing_stats.start_time = jiffies;
  55}
  56
  57static void end_read_timing(unsigned length)
  58{
  59	timing_stats.read_time += (jiffies - start_time);
  60	timing_stats.read_bytes += length + 2;
  61	report_timing();
  62}
  63
  64static void end_write_timing(unsigned length)
  65{
  66	timing_stats.write_time += (jiffies - start_time);
  67	timing_stats.write_bytes += length + 2;
  68	report_timing();
  69}
  70
  71static void report_timing(void)
  72{
  73	unsigned long since = jiffies - timing_stats.last_report_time;
  74
  75	/* If it's been more than one second... */
  76	if (since >= HZ) {
  77		int first = (timing_stats.last_report_time == 0);
  78
  79		timing_stats.last_report_time = jiffies;
  80		if (!first)
  81			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  82			       ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
  83			       jiffies_to_usecs(since),
  84			       timing_stats.read_bytes,
  85			       jiffies_to_usecs(timing_stats.read_time),
  86			       timing_stats.write_bytes,
  87			       jiffies_to_usecs(timing_stats.write_time));
  88
  89		timing_stats.read_time = 0;
  90		timing_stats.write_time = 0;
  91		timing_stats.read_bytes = 0;
  92		timing_stats.write_bytes = 0;
  93	}
  94}
  95#else
  96static void start_timing(void) { }
  97static void end_read_timing(unsigned length) { }
  98static void end_write_timing(unsigned length) { }
  99#endif
 100
 101/* Imported IPW definitions */
 102
 103#define LL_MTU_V1 318
 104#define LL_MTU_V2 250
 105#define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
 106
 107#define PRIO_DATA  2
 108#define PRIO_CTRL  1
 109#define PRIO_SETUP 0
 110
 111/* Addresses */
 112#define ADDR_SETUP_PROT 0
 113
 114/* Protocol ids */
 115enum {
 116	/* Identifier for the Com Data protocol */
 117	TL_PROTOCOLID_COM_DATA = 0,
 118
 119	/* Identifier for the Com Control protocol */
 120	TL_PROTOCOLID_COM_CTRL = 1,
 121
 122	/* Identifier for the Setup protocol */
 123	TL_PROTOCOLID_SETUP = 2
 124};
 125
 126/* Number of bytes in NL packet header (cannot do
 127 * sizeof(nl_packet_header) since it's a bitfield) */
 128#define NL_FIRST_PACKET_HEADER_SIZE        3
 129
 130/* Number of bytes in NL packet header (cannot do
 131 * sizeof(nl_packet_header) since it's a bitfield) */
 132#define NL_FOLLOWING_PACKET_HEADER_SIZE    1
 133
 134struct nl_first_packet_header {
 135	unsigned char protocol:3;
 136	unsigned char address:3;
 137	unsigned char packet_rank:2;
 138	unsigned char length_lsb;
 139	unsigned char length_msb;
 140};
 141
 142struct nl_packet_header {
 143	unsigned char protocol:3;
 144	unsigned char address:3;
 145	unsigned char packet_rank:2;
 146};
 147
 148/* Value of 'packet_rank' above */
 149#define NL_INTERMEDIATE_PACKET    0x0
 150#define NL_LAST_PACKET            0x1
 151#define NL_FIRST_PACKET           0x2
 152
 153union nl_packet {
 154	/* Network packet header of the first packet (a special case) */
 155	struct nl_first_packet_header hdr_first;
 156	/* Network packet header of the following packets (if any) */
 157	struct nl_packet_header hdr;
 158	/* Complete network packet (header + data) */
 159	unsigned char rawpkt[LL_MTU_MAX];
 160} __attribute__ ((__packed__));
 161
 162#define HW_VERSION_UNKNOWN -1
 163#define HW_VERSION_1 1
 164#define HW_VERSION_2 2
 165
 166/* IPW I/O ports */
 167#define IOIER 0x00		/* Interrupt Enable Register */
 168#define IOIR  0x02		/* Interrupt Source/ACK register */
 169#define IODCR 0x04		/* Data Control Register */
 170#define IODRR 0x06		/* Data Read Register */
 171#define IODWR 0x08		/* Data Write Register */
 172#define IOESR 0x0A		/* Embedded Driver Status Register */
 173#define IORXR 0x0C		/* Rx Fifo Register (Host to Embedded) */
 174#define IOTXR 0x0E		/* Tx Fifo Register (Embedded to Host) */
 175
 176/* I/O ports and bit definitions for version 1 of the hardware */
 177
 178/* IER bits*/
 179#define IER_RXENABLED   0x1
 180#define IER_TXENABLED   0x2
 181
 182/* ISR bits */
 183#define IR_RXINTR       0x1
 184#define IR_TXINTR       0x2
 185
 186/* DCR bits */
 187#define DCR_RXDONE      0x1
 188#define DCR_TXDONE      0x2
 189#define DCR_RXRESET     0x4
 190#define DCR_TXRESET     0x8
 191
 192/* I/O ports and bit definitions for version 2 of the hardware */
 193
 194struct MEMCCR {
 195	unsigned short reg_config_option;	/* PCCOR: Configuration Option Register */
 196	unsigned short reg_config_and_status;	/* PCCSR: Configuration and Status Register */
 197	unsigned short reg_pin_replacement;	/* PCPRR: Pin Replacemant Register */
 198	unsigned short reg_socket_and_copy;	/* PCSCR: Socket and Copy Register */
 199	unsigned short reg_ext_status;		/* PCESR: Extendend Status Register */
 200	unsigned short reg_io_base;		/* PCIOB: I/O Base Register */
 201};
 202
 203struct MEMINFREG {
 204	unsigned short memreg_tx_old;	/* TX Register (R/W) */
 205	unsigned short pad1;
 206	unsigned short memreg_rx_done;	/* RXDone Register (R/W) */
 207	unsigned short pad2;
 208	unsigned short memreg_rx;	/* RX Register (R/W) */
 209	unsigned short pad3;
 210	unsigned short memreg_pc_interrupt_ack;	/* PC intr Ack Register (W) */
 211	unsigned short pad4;
 212	unsigned long memreg_card_present;/* Mask for Host to check (R) for
 213					   * CARD_PRESENT_VALUE */
 214	unsigned short memreg_tx_new;	/* TX2 (new) Register (R/W) */
 215};
 216
 217#define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
 218
 219#define MEMTX_TX                       0x0001
 220#define MEMRX_RX                       0x0001
 221#define MEMRX_RX_DONE                  0x0001
 222#define MEMRX_PCINTACKK                0x0001
 223
 224#define NL_NUM_OF_PRIORITIES       3
 225#define NL_NUM_OF_PROTOCOLS        3
 226#define NL_NUM_OF_ADDRESSES        NO_OF_IPW_CHANNELS
 227
 228struct ipw_hardware {
 229	unsigned int base_port;
 230	short hw_version;
 231	unsigned short ll_mtu;
 232	spinlock_t lock;
 233
 234	int initializing;
 235	int init_loops;
 236	struct timer_list setup_timer;
 237
 238	/* Flag if hw is ready to send next packet */
 239	int tx_ready;
 240	/* Count of pending packets to be sent */
 241	int tx_queued;
 242	struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
 243
 244	int rx_bytes_queued;
 245	struct list_head rx_queue;
 246	/* Pool of rx_packet structures that are not currently used. */
 247	struct list_head rx_pool;
 248	int rx_pool_size;
 249	/* True if reception of data is blocked while userspace processes it. */
 250	int blocking_rx;
 251	/* True if there is RX data ready on the hardware. */
 252	int rx_ready;
 253	unsigned short last_memtx_serial;
 254	/*
 255	 * Newer versions of the V2 card firmware send serial numbers in the
 256	 * MemTX register. 'serial_number_detected' is set true when we detect
 257	 * a non-zero serial number (indicating the new firmware).  Thereafter,
 258	 * the driver can safely ignore the Timer Recovery re-sends to avoid
 259	 * out-of-sync problems.
 260	 */
 261	int serial_number_detected;
 262	struct work_struct work_rx;
 263
 264	/* True if we are to send the set-up data to the hardware. */
 265	int to_setup;
 266
 267	/* Card has been removed */
 268	int removed;
 269	/* Saved irq value when we disable the interrupt. */
 270	int irq;
 271	/* True if this driver is shutting down. */
 272	int shutting_down;
 273	/* Modem control lines */
 274	unsigned int control_lines[NL_NUM_OF_ADDRESSES];
 275	struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
 276
 277	struct tasklet_struct tasklet;
 278
 279	/* The handle for the network layer, for the sending of events to it. */
 280	struct ipw_network *network;
 281	struct MEMINFREG __iomem *memory_info_regs;
 282	struct MEMCCR __iomem *memregs_CCR;
 283	void (*reboot_callback) (void *data);
 284	void *reboot_callback_data;
 285
 286	unsigned short __iomem *memreg_tx;
 287};
 288
 289/*
 290 * Packet info structure for tx packets.
 291 * Note: not all the fields defined here are required for all protocols
 292 */
 293struct ipw_tx_packet {
 294	struct list_head queue;
 295	/* channel idx + 1 */
 296	unsigned char dest_addr;
 297	/* SETUP, CTRL or DATA */
 298	unsigned char protocol;
 299	/* Length of data block, which starts at the end of this structure */
 300	unsigned short length;
 301	/* Sending state */
 302	/* Offset of where we've sent up to so far */
 303	unsigned long offset;
 304	/* Count of packet fragments, starting at 0 */
 305	int fragment_count;
 306
 307	/* Called after packet is sent and before is freed */
 308	void (*packet_callback) (void *cb_data, unsigned int packet_length);
 309	void *callback_data;
 310};
 311
 312/* Signals from DTE */
 313#define COMCTRL_RTS	0
 314#define COMCTRL_DTR	1
 315
 316/* Signals from DCE */
 317#define COMCTRL_CTS	2
 318#define COMCTRL_DCD	3
 319#define COMCTRL_DSR	4
 320#define COMCTRL_RI	5
 321
 322struct ipw_control_packet_body {
 323	/* DTE signal or DCE signal */
 324	unsigned char sig_no;
 325	/* 0: set signal, 1: clear signal */
 326	unsigned char value;
 327} __attribute__ ((__packed__));
 328
 329struct ipw_control_packet {
 330	struct ipw_tx_packet header;
 331	struct ipw_control_packet_body body;
 332};
 333
 334struct ipw_rx_packet {
 335	struct list_head queue;
 336	unsigned int capacity;
 337	unsigned int length;
 338	unsigned int protocol;
 339	unsigned int channel_idx;
 340};
 341
 342static char *data_type(const unsigned char *buf, unsigned length)
 343{
 344	struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
 345
 346	if (length == 0)
 347		return "     ";
 348
 349	if (hdr->packet_rank & NL_FIRST_PACKET) {
 350		switch (hdr->protocol) {
 351		case TL_PROTOCOLID_COM_DATA:	return "DATA ";
 352		case TL_PROTOCOLID_COM_CTRL:	return "CTRL ";
 353		case TL_PROTOCOLID_SETUP:	return "SETUP";
 354		default: return "???? ";
 355		}
 356	} else
 357		return "     ";
 358}
 359
 360#define DUMP_MAX_BYTES 64
 361
 362static void dump_data_bytes(const char *type, const unsigned char *data,
 363			    unsigned length)
 364{
 365	char prefix[56];
 366
 367	sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
 368			type, data_type(data, length));
 369	print_hex_dump_bytes(prefix, 0, (void *)data,
 370			length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
 371}
 372
 373static void swap_packet_bitfield_to_le(unsigned char *data)
 374{
 375#ifdef __BIG_ENDIAN_BITFIELD
 376	unsigned char tmp = *data, ret = 0;
 377
 378	/*
 379	 * transform bits from aa.bbb.ccc to ccc.bbb.aa
 380	 */
 381	ret |= (tmp & 0xc0) >> 6;
 382	ret |= (tmp & 0x38) >> 1;
 383	ret |= (tmp & 0x07) << 5;
 384	*data = ret & 0xff;
 385#endif
 386}
 387
 388static void swap_packet_bitfield_from_le(unsigned char *data)
 389{
 390#ifdef __BIG_ENDIAN_BITFIELD
 391	unsigned char tmp = *data, ret = 0;
 392
 393	/*
 394	 * transform bits from ccc.bbb.aa to aa.bbb.ccc
 395	 */
 396	ret |= (tmp & 0xe0) >> 5;
 397	ret |= (tmp & 0x1c) << 1;
 398	ret |= (tmp & 0x03) << 6;
 399	*data = ret & 0xff;
 400#endif
 401}
 402
 403static void do_send_fragment(struct ipw_hardware *hw, unsigned char *data,
 404			    unsigned length)
 405{
 406	unsigned i;
 407	unsigned long flags;
 408
 409	start_timing();
 410	BUG_ON(length > hw->ll_mtu);
 411
 412	if (ipwireless_debug)
 413		dump_data_bytes("send", data, length);
 414
 415	spin_lock_irqsave(&hw->lock, flags);
 416
 417	hw->tx_ready = 0;
 418	swap_packet_bitfield_to_le(data);
 419
 420	if (hw->hw_version == HW_VERSION_1) {
 421		outw((unsigned short) length, hw->base_port + IODWR);
 422
 423		for (i = 0; i < length; i += 2) {
 424			unsigned short d = data[i];
 425			__le16 raw_data;
 426
 427			if (i + 1 < length)
 428				d |= data[i + 1] << 8;
 429			raw_data = cpu_to_le16(d);
 430			outw(raw_data, hw->base_port + IODWR);
 431		}
 432
 433		outw(DCR_TXDONE, hw->base_port + IODCR);
 434	} else if (hw->hw_version == HW_VERSION_2) {
 435		outw((unsigned short) length, hw->base_port);
 436
 437		for (i = 0; i < length; i += 2) {
 438			unsigned short d = data[i];
 439			__le16 raw_data;
 440
 441			if (i + 1 < length)
 442				d |= data[i + 1] << 8;
 443			raw_data = cpu_to_le16(d);
 444			outw(raw_data, hw->base_port);
 445		}
 446		while ((i & 3) != 2) {
 447			outw((unsigned short) 0xDEAD, hw->base_port);
 448			i += 2;
 449		}
 450		writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
 451	}
 452
 453	spin_unlock_irqrestore(&hw->lock, flags);
 454
 455	end_write_timing(length);
 456}
 457
 458static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
 459{
 460	unsigned short fragment_data_len;
 461	unsigned short data_left = packet->length - packet->offset;
 462	unsigned short header_size;
 463	union nl_packet pkt;
 464
 465	header_size =
 466	    (packet->fragment_count == 0)
 467	    ? NL_FIRST_PACKET_HEADER_SIZE
 468	    : NL_FOLLOWING_PACKET_HEADER_SIZE;
 469	fragment_data_len = hw->ll_mtu - header_size;
 470	if (data_left < fragment_data_len)
 471		fragment_data_len = data_left;
 472
 473	/*
 474	 * hdr_first is now in machine bitfield order, which will be swapped
 475	 * to le just before it goes to hw
 476	 */
 477	pkt.hdr_first.protocol = packet->protocol;
 478	pkt.hdr_first.address = packet->dest_addr;
 479	pkt.hdr_first.packet_rank = 0;
 480
 481	/* First packet? */
 482	if (packet->fragment_count == 0) {
 483		pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
 484		pkt.hdr_first.length_lsb = (unsigned char) packet->length;
 485		pkt.hdr_first.length_msb =
 486			(unsigned char) (packet->length >> 8);
 487	}
 488
 489	memcpy(pkt.rawpkt + header_size,
 490	       ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
 491	       packet->offset, fragment_data_len);
 492	packet->offset += fragment_data_len;
 493	packet->fragment_count++;
 494
 495	/* Last packet? (May also be first packet.) */
 496	if (packet->offset == packet->length)
 497		pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
 498	do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
 499
 500	/* If this packet has unsent data, then re-queue it. */
 501	if (packet->offset < packet->length) {
 502		/*
 503		 * Re-queue it at the head of the highest priority queue so
 504		 * it goes before all other packets
 505		 */
 506		unsigned long flags;
 507
 508		spin_lock_irqsave(&hw->lock, flags);
 509		list_add(&packet->queue, &hw->tx_queue[0]);
 510		hw->tx_queued++;
 511		spin_unlock_irqrestore(&hw->lock, flags);
 512	} else {
 513		if (packet->packet_callback)
 514			packet->packet_callback(packet->callback_data,
 515					packet->length);
 516		kfree(packet);
 517	}
 518}
 519
 520static void ipw_setup_hardware(struct ipw_hardware *hw)
 521{
 522	unsigned long flags;
 523
 524	spin_lock_irqsave(&hw->lock, flags);
 525	if (hw->hw_version == HW_VERSION_1) {
 526		/* Reset RX FIFO */
 527		outw(DCR_RXRESET, hw->base_port + IODCR);
 528		/* SB: Reset TX FIFO */
 529		outw(DCR_TXRESET, hw->base_port + IODCR);
 530
 531		/* Enable TX and RX interrupts. */
 532		outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
 533	} else {
 534		/*
 535		 * Set INTRACK bit (bit 0), which means we must explicitly
 536		 * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
 537		 */
 538		unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
 539
 540		csr |= 1;
 541		writew(csr, &hw->memregs_CCR->reg_config_and_status);
 542	}
 543	spin_unlock_irqrestore(&hw->lock, flags);
 544}
 545
 546/*
 547 * If 'packet' is NULL, then this function allocates a new packet, setting its
 548 * length to 0 and ensuring it has the specified minimum amount of free space.
 549 *
 550 * If 'packet' is not NULL, then this function enlarges it if it doesn't
 551 * have the specified minimum amount of free space.
 552 *
 553 */
 554static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
 555					   struct ipw_rx_packet *packet,
 556					   int minimum_free_space)
 557{
 558
 559	if (!packet) {
 560		unsigned long flags;
 561
 562		spin_lock_irqsave(&hw->lock, flags);
 563		if (!list_empty(&hw->rx_pool)) {
 564			packet = list_first_entry(&hw->rx_pool,
 565					struct ipw_rx_packet, queue);
 566			hw->rx_pool_size--;
 567			spin_unlock_irqrestore(&hw->lock, flags);
 568			list_del(&packet->queue);
 569		} else {
 570			const int min_capacity =
 571				ipwireless_ppp_mru(hw->network) + 2;
 572			int new_capacity;
 573
 574			spin_unlock_irqrestore(&hw->lock, flags);
 575			new_capacity =
 576				(minimum_free_space > min_capacity
 577				 ? minimum_free_space
 578				 : min_capacity);
 579			packet = kmalloc(sizeof(struct ipw_rx_packet)
 580					+ new_capacity, GFP_ATOMIC);
 581			if (!packet)
 582				return NULL;
 583			packet->capacity = new_capacity;
 584		}
 585		packet->length = 0;
 586	}
 587
 588	if (packet->length + minimum_free_space > packet->capacity) {
 589		struct ipw_rx_packet *old_packet = packet;
 590
 591		packet = kmalloc(sizeof(struct ipw_rx_packet) +
 592				old_packet->length + minimum_free_space,
 593				GFP_ATOMIC);
 594		if (!packet) {
 595			kfree(old_packet);
 596			return NULL;
 597		}
 598		memcpy(packet, old_packet,
 599				sizeof(struct ipw_rx_packet)
 600					+ old_packet->length);
 601		packet->capacity = old_packet->length + minimum_free_space;
 602		kfree(old_packet);
 603	}
 604
 605	return packet;
 606}
 607
 608static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
 609{
 610	if (hw->rx_pool_size > 6)
 611		kfree(packet);
 612	else {
 613		hw->rx_pool_size++;
 614		list_add(&packet->queue, &hw->rx_pool);
 615	}
 616}
 617
 618static void queue_received_packet(struct ipw_hardware *hw,
 619				  unsigned int protocol,
 620				  unsigned int address,
 621				  const unsigned char *data, int length,
 622				  int is_last)
 623{
 624	unsigned int channel_idx = address - 1;
 625	struct ipw_rx_packet *packet = NULL;
 626	unsigned long flags;
 627
 628	/* Discard packet if channel index is out of range. */
 629	if (channel_idx >= NL_NUM_OF_ADDRESSES) {
 630		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 631		       ": data packet has bad address %u\n", address);
 632		return;
 633	}
 634
 635	/*
 636	 * ->packet_assembler is safe to touch unlocked, this is the only place
 637	 */
 638	if (protocol == TL_PROTOCOLID_COM_DATA) {
 639		struct ipw_rx_packet **assem =
 640			&hw->packet_assembler[channel_idx];
 641
 642		/*
 643		 * Create a new packet, or assembler already contains one
 644		 * enlarge it by 'length' bytes.
 645		 */
 646		(*assem) = pool_allocate(hw, *assem, length);
 647		if (!(*assem)) {
 648			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
 649				": no memory for incoming data packet, dropped!\n");
 650			return;
 651		}
 652		(*assem)->protocol = protocol;
 653		(*assem)->channel_idx = channel_idx;
 654
 655		/* Append this packet data onto existing data. */
 656		memcpy((unsigned char *)(*assem) +
 657			       sizeof(struct ipw_rx_packet)
 658				+ (*assem)->length, data, length);
 659		(*assem)->length += length;
 660		if (is_last) {
 661			packet = *assem;
 662			*assem = NULL;
 663			/* Count queued DATA bytes only */
 664			spin_lock_irqsave(&hw->lock, flags);
 665			hw->rx_bytes_queued += packet->length;
 666			spin_unlock_irqrestore(&hw->lock, flags);
 667		}
 668	} else {
 669		/* If it's a CTRL packet, don't assemble, just queue it. */
 670		packet = pool_allocate(hw, NULL, length);
 671		if (!packet) {
 672			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
 673				": no memory for incoming ctrl packet, dropped!\n");
 674			return;
 675		}
 676		packet->protocol = protocol;
 677		packet->channel_idx = channel_idx;
 678		memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
 679				data, length);
 680		packet->length = length;
 681	}
 682
 683	/*
 684	 * If this is the last packet, then send the assembled packet on to the
 685	 * network layer.
 686	 */
 687	if (packet) {
 688		spin_lock_irqsave(&hw->lock, flags);
 689		list_add_tail(&packet->queue, &hw->rx_queue);
 690		/* Block reception of incoming packets if queue is full. */
 691		hw->blocking_rx =
 692			(hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
 693
 694		spin_unlock_irqrestore(&hw->lock, flags);
 695		schedule_work(&hw->work_rx);
 696	}
 697}
 698
 699/*
 700 * Workqueue callback
 701 */
 702static void ipw_receive_data_work(struct work_struct *work_rx)
 703{
 704	struct ipw_hardware *hw =
 705	    container_of(work_rx, struct ipw_hardware, work_rx);
 706	unsigned long flags;
 707
 708	spin_lock_irqsave(&hw->lock, flags);
 709	while (!list_empty(&hw->rx_queue)) {
 710		struct ipw_rx_packet *packet =
 711			list_first_entry(&hw->rx_queue,
 712					struct ipw_rx_packet, queue);
 713
 714		if (hw->shutting_down)
 715			break;
 716		list_del(&packet->queue);
 717
 718		/*
 719		 * Note: ipwireless_network_packet_received must be called in a
 720		 * process context (i.e. via schedule_work) because the tty
 721		 * output code can sleep in the tty_flip_buffer_push call.
 722		 */
 723		if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
 724			if (hw->network != NULL) {
 725				/* If the network hasn't been disconnected. */
 726				spin_unlock_irqrestore(&hw->lock, flags);
 727				/*
 728				 * This must run unlocked due to tty processing
 729				 * and mutex locking
 730				 */
 731				ipwireless_network_packet_received(
 732						hw->network,
 733						packet->channel_idx,
 734						(unsigned char *)packet
 735						+ sizeof(struct ipw_rx_packet),
 736						packet->length);
 737				spin_lock_irqsave(&hw->lock, flags);
 738			}
 739			/* Count queued DATA bytes only */
 740			hw->rx_bytes_queued -= packet->length;
 741		} else {
 742			/*
 743			 * This is safe to be called locked, callchain does
 744			 * not block
 745			 */
 746			handle_received_CTRL_packet(hw, packet->channel_idx,
 747					(unsigned char *)packet
 748					+ sizeof(struct ipw_rx_packet),
 749					packet->length);
 750		}
 751		pool_free(hw, packet);
 752		/*
 753		 * Unblock reception of incoming packets if queue is no longer
 754		 * full.
 755		 */
 756		hw->blocking_rx =
 757			hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
 758		if (hw->shutting_down)
 759			break;
 760	}
 761	spin_unlock_irqrestore(&hw->lock, flags);
 762}
 763
 764static void handle_received_CTRL_packet(struct ipw_hardware *hw,
 765					unsigned int channel_idx,
 766					const unsigned char *data, int len)
 767{
 768	const struct ipw_control_packet_body *body =
 769		(const struct ipw_control_packet_body *) data;
 770	unsigned int changed_mask;
 771
 772	if (len != sizeof(struct ipw_control_packet_body)) {
 773		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 774		       ": control packet was %d bytes - wrong size!\n",
 775		       len);
 776		return;
 777	}
 778
 779	switch (body->sig_no) {
 780	case COMCTRL_CTS:
 781		changed_mask = IPW_CONTROL_LINE_CTS;
 782		break;
 783	case COMCTRL_DCD:
 784		changed_mask = IPW_CONTROL_LINE_DCD;
 785		break;
 786	case COMCTRL_DSR:
 787		changed_mask = IPW_CONTROL_LINE_DSR;
 788		break;
 789	case COMCTRL_RI:
 790		changed_mask = IPW_CONTROL_LINE_RI;
 791		break;
 792	default:
 793		changed_mask = 0;
 794	}
 795
 796	if (changed_mask != 0) {
 797		if (body->value)
 798			hw->control_lines[channel_idx] |= changed_mask;
 799		else
 800			hw->control_lines[channel_idx] &= ~changed_mask;
 801		if (hw->network)
 802			ipwireless_network_notify_control_line_change(
 803					hw->network,
 804					channel_idx,
 805					hw->control_lines[channel_idx],
 806					changed_mask);
 807	}
 808}
 809
 810static void handle_received_packet(struct ipw_hardware *hw,
 811				   const union nl_packet *packet,
 812				   unsigned short len)
 813{
 814	unsigned int protocol = packet->hdr.protocol;
 815	unsigned int address = packet->hdr.address;
 816	unsigned int header_length;
 817	const unsigned char *data;
 818	unsigned int data_len;
 819	int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
 820
 821	if (packet->hdr.packet_rank & NL_FIRST_PACKET)
 822		header_length = NL_FIRST_PACKET_HEADER_SIZE;
 823	else
 824		header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
 825
 826	data = packet->rawpkt + header_length;
 827	data_len = len - header_length;
 828	switch (protocol) {
 829	case TL_PROTOCOLID_COM_DATA:
 830	case TL_PROTOCOLID_COM_CTRL:
 831		queue_received_packet(hw, protocol, address, data, data_len,
 832				is_last);
 833		break;
 834	case TL_PROTOCOLID_SETUP:
 835		handle_received_SETUP_packet(hw, address, data, data_len,
 836				is_last);
 837		break;
 838	}
 839}
 840
 841static void acknowledge_data_read(struct ipw_hardware *hw)
 842{
 843	if (hw->hw_version == HW_VERSION_1)
 844		outw(DCR_RXDONE, hw->base_port + IODCR);
 845	else
 846		writew(MEMRX_PCINTACKK,
 847				&hw->memory_info_regs->memreg_pc_interrupt_ack);
 848}
 849
 850/*
 851 * Retrieve a packet from the IPW hardware.
 852 */
 853static void do_receive_packet(struct ipw_hardware *hw)
 854{
 855	unsigned len;
 856	unsigned i;
 857	unsigned char pkt[LL_MTU_MAX];
 858
 859	start_timing();
 860
 861	if (hw->hw_version == HW_VERSION_1) {
 862		len = inw(hw->base_port + IODRR);
 863		if (len > hw->ll_mtu) {
 864			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 865			       ": received a packet of %u bytes - longer than the MTU!\n", len);
 866			outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
 867			return;
 868		}
 869
 870		for (i = 0; i < len; i += 2) {
 871			__le16 raw_data = inw(hw->base_port + IODRR);
 872			unsigned short data = le16_to_cpu(raw_data);
 873
 874			pkt[i] = (unsigned char) data;
 875			pkt[i + 1] = (unsigned char) (data >> 8);
 876		}
 877	} else {
 878		len = inw(hw->base_port);
 879		if (len > hw->ll_mtu) {
 880			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
 881			       ": received a packet of %u bytes - longer than the MTU!\n", len);
 882			writew(MEMRX_PCINTACKK,
 883				&hw->memory_info_regs->memreg_pc_interrupt_ack);
 884			return;
 885		}
 886
 887		for (i = 0; i < len; i += 2) {
 888			__le16 raw_data = inw(hw->base_port);
 889			unsigned short data = le16_to_cpu(raw_data);
 890
 891			pkt[i] = (unsigned char) data;
 892			pkt[i + 1] = (unsigned char) (data >> 8);
 893		}
 894
 895		while ((i & 3) != 2) {
 896			inw(hw->base_port);
 897			i += 2;
 898		}
 899	}
 900
 901	acknowledge_data_read(hw);
 902
 903	swap_packet_bitfield_from_le(pkt);
 904
 905	if (ipwireless_debug)
 906		dump_data_bytes("recv", pkt, len);
 907
 908	handle_received_packet(hw, (union nl_packet *) pkt, len);
 909
 910	end_read_timing(len);
 911}
 912
 913static int get_current_packet_priority(struct ipw_hardware *hw)
 914{
 915	/*
 916	 * If we're initializing, don't send anything of higher priority than
 917	 * PRIO_SETUP.  The network layer therefore need not care about
 918	 * hardware initialization - any of its stuff will simply be queued
 919	 * until setup is complete.
 920	 */
 921	return (hw->to_setup || hw->initializing
 922			? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
 923}
 924
 925/*
 926 * return 1 if something has been received from hw
 927 */
 928static int get_packets_from_hw(struct ipw_hardware *hw)
 929{
 930	int received = 0;
 931	unsigned long flags;
 932
 933	spin_lock_irqsave(&hw->lock, flags);
 934	while (hw->rx_ready && !hw->blocking_rx) {
 935		received = 1;
 936		hw->rx_ready--;
 937		spin_unlock_irqrestore(&hw->lock, flags);
 938
 939		do_receive_packet(hw);
 940
 941		spin_lock_irqsave(&hw->lock, flags);
 942	}
 943	spin_unlock_irqrestore(&hw->lock, flags);
 944
 945	return received;
 946}
 947
 948/*
 949 * Send pending packet up to given priority, prioritize SETUP data until
 950 * hardware is fully setup.
 951 *
 952 * return 1 if more packets can be sent
 953 */
 954static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
 955{
 956	int more_to_send = 0;
 957	unsigned long flags;
 958
 959	spin_lock_irqsave(&hw->lock, flags);
 960	if (hw->tx_queued && hw->tx_ready) {
 961		int priority;
 962		struct ipw_tx_packet *packet = NULL;
 963
 964		/* Pick a packet */
 965		for (priority = 0; priority < priority_limit; priority++) {
 966			if (!list_empty(&hw->tx_queue[priority])) {
 967				packet = list_first_entry(
 968						&hw->tx_queue[priority],
 969						struct ipw_tx_packet,
 970						queue);
 971
 972				hw->tx_queued--;
 973				list_del(&packet->queue);
 974
 975				break;
 976			}
 977		}
 978		if (!packet) {
 979			hw->tx_queued = 0;
 980			spin_unlock_irqrestore(&hw->lock, flags);
 981			return 0;
 982		}
 983
 984		spin_unlock_irqrestore(&hw->lock, flags);
 985
 986		/* Send */
 987		do_send_packet(hw, packet);
 988
 989		/* Check if more to send */
 990		spin_lock_irqsave(&hw->lock, flags);
 991		for (priority = 0; priority < priority_limit; priority++)
 992			if (!list_empty(&hw->tx_queue[priority])) {
 993				more_to_send = 1;
 994				break;
 995			}
 996
 997		if (!more_to_send)
 998			hw->tx_queued = 0;
 999	}
1000	spin_unlock_irqrestore(&hw->lock, flags);
1001
1002	return more_to_send;
1003}
1004
1005/*
1006 * Send and receive all queued packets.
1007 */
1008static void ipwireless_do_tasklet(unsigned long hw_)
1009{
1010	struct ipw_hardware *hw = (struct ipw_hardware *) hw_;
1011	unsigned long flags;
1012
1013	spin_lock_irqsave(&hw->lock, flags);
1014	if (hw->shutting_down) {
1015		spin_unlock_irqrestore(&hw->lock, flags);
1016		return;
1017	}
1018
1019	if (hw->to_setup == 1) {
1020		/*
1021		 * Initial setup data sent to hardware
1022		 */
1023		hw->to_setup = 2;
1024		spin_unlock_irqrestore(&hw->lock, flags);
1025
1026		ipw_setup_hardware(hw);
1027		ipw_send_setup_packet(hw);
1028
1029		send_pending_packet(hw, PRIO_SETUP + 1);
1030		get_packets_from_hw(hw);
1031	} else {
1032		int priority_limit = get_current_packet_priority(hw);
1033		int again;
1034
1035		spin_unlock_irqrestore(&hw->lock, flags);
1036
1037		do {
1038			again = send_pending_packet(hw, priority_limit);
1039			again |= get_packets_from_hw(hw);
1040		} while (again);
1041	}
1042}
1043
1044/*
1045 * return true if the card is physically present.
1046 */
1047static int is_card_present(struct ipw_hardware *hw)
1048{
1049	if (hw->hw_version == HW_VERSION_1)
1050		return inw(hw->base_port + IOIR) != 0xFFFF;
1051	else
1052		return readl(&hw->memory_info_regs->memreg_card_present) ==
1053		    CARD_PRESENT_VALUE;
1054}
1055
1056static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1057						  struct ipw_hardware *hw)
1058{
1059	unsigned short irqn;
1060
1061	irqn = inw(hw->base_port + IOIR);
1062
1063	/* Check if card is present */
1064	if (irqn == 0xFFFF)
1065		return IRQ_NONE;
1066	else if (irqn != 0) {
1067		unsigned short ack = 0;
1068		unsigned long flags;
1069
1070		/* Transmit complete. */
1071		if (irqn & IR_TXINTR) {
1072			ack |= IR_TXINTR;
1073			spin_lock_irqsave(&hw->lock, flags);
1074			hw->tx_ready = 1;
1075			spin_unlock_irqrestore(&hw->lock, flags);
1076		}
1077		/* Received data */
1078		if (irqn & IR_RXINTR) {
1079			ack |= IR_RXINTR;
1080			spin_lock_irqsave(&hw->lock, flags);
1081			hw->rx_ready++;
1082			spin_unlock_irqrestore(&hw->lock, flags);
1083		}
1084		if (ack != 0) {
1085			outw(ack, hw->base_port + IOIR);
1086			tasklet_schedule(&hw->tasklet);
1087		}
1088		return IRQ_HANDLED;
1089	}
1090	return IRQ_NONE;
1091}
1092
1093static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1094{
1095	unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1096
1097	csr &= 0xfffd;
1098	writew(csr, &hw->memregs_CCR->reg_config_and_status);
1099}
1100
1101static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1102						     struct ipw_hardware *hw)
1103{
1104	int tx = 0;
1105	int rx = 0;
1106	int rx_repeat = 0;
1107	int try_mem_tx_old;
1108	unsigned long flags;
1109
1110	do {
1111
1112	unsigned short memtx = readw(hw->memreg_tx);
1113	unsigned short memtx_serial;
1114	unsigned short memrxdone =
1115		readw(&hw->memory_info_regs->memreg_rx_done);
1116
1117	try_mem_tx_old = 0;
1118
1119	/* check whether the interrupt was generated by ipwireless card */
1120	if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1121
1122		/* check if the card uses memreg_tx_old register */
1123		if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1124			memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1125			if (memtx & MEMTX_TX) {
1126				printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1127					": Using memreg_tx_old\n");
1128				hw->memreg_tx =
1129					&hw->memory_info_regs->memreg_tx_old;
1130			} else {
1131				return IRQ_NONE;
1132			}
1133		} else
1134			return IRQ_NONE;
1135	}
1136
1137	/*
1138	 * See if the card is physically present. Note that while it is
1139	 * powering up, it appears not to be present.
1140	 */
1141	if (!is_card_present(hw)) {
1142		acknowledge_pcmcia_interrupt(hw);
1143		return IRQ_HANDLED;
1144	}
1145
1146	memtx_serial = memtx & (unsigned short) 0xff00;
1147	if (memtx & MEMTX_TX) {
1148		writew(memtx_serial, hw->memreg_tx);
1149
1150		if (hw->serial_number_detected) {
1151			if (memtx_serial != hw->last_memtx_serial) {
1152				hw->last_memtx_serial = memtx_serial;
1153				spin_lock_irqsave(&hw->lock, flags);
1154				hw->rx_ready++;
1155				spin_unlock_irqrestore(&hw->lock, flags);
1156				rx = 1;
1157			} else
1158				/* Ignore 'Timer Recovery' duplicates. */
1159				rx_repeat = 1;
1160		} else {
1161			/*
1162			 * If a non-zero serial number is seen, then enable
1163			 * serial number checking.
1164			 */
1165			if (memtx_serial != 0) {
1166				hw->serial_number_detected = 1;
1167				printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1168					": memreg_tx serial num detected\n");
1169
1170				spin_lock_irqsave(&hw->lock, flags);
1171				hw->rx_ready++;
1172				spin_unlock_irqrestore(&hw->lock, flags);
1173			}
1174			rx = 1;
1175		}
1176	}
1177	if (memrxdone & MEMRX_RX_DONE) {
1178		writew(0, &hw->memory_info_regs->memreg_rx_done);
1179		spin_lock_irqsave(&hw->lock, flags);
1180		hw->tx_ready = 1;
1181		spin_unlock_irqrestore(&hw->lock, flags);
1182		tx = 1;
1183	}
1184	if (tx)
1185		writew(MEMRX_PCINTACKK,
1186				&hw->memory_info_regs->memreg_pc_interrupt_ack);
1187
1188	acknowledge_pcmcia_interrupt(hw);
1189
1190	if (tx || rx)
1191		tasklet_schedule(&hw->tasklet);
1192	else if (!rx_repeat) {
1193		if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1194			if (hw->serial_number_detected)
1195				printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1196					": spurious interrupt - new_tx mode\n");
1197			else {
1198				printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1199					": no valid memreg_tx value - switching to the old memreg_tx\n");
1200				hw->memreg_tx =
1201					&hw->memory_info_regs->memreg_tx_old;
1202				try_mem_tx_old = 1;
1203			}
1204		} else
1205			printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1206					": spurious interrupt - old_tx mode\n");
1207	}
1208
1209	} while (try_mem_tx_old == 1);
1210
1211	return IRQ_HANDLED;
1212}
1213
1214irqreturn_t ipwireless_interrupt(int irq, void *dev_id)
1215{
1216	struct ipw_dev *ipw = dev_id;
1217
1218	if (ipw->hardware->hw_version == HW_VERSION_1)
1219		return ipwireless_handle_v1_interrupt(irq, ipw->hardware);
1220	else
1221		return ipwireless_handle_v2_v3_interrupt(irq, ipw->hardware);
1222}
1223
1224static void flush_packets_to_hw(struct ipw_hardware *hw)
1225{
1226	int priority_limit;
1227	unsigned long flags;
1228
1229	spin_lock_irqsave(&hw->lock, flags);
1230	priority_limit = get_current_packet_priority(hw);
1231	spin_unlock_irqrestore(&hw->lock, flags);
1232
1233	while (send_pending_packet(hw, priority_limit));
1234}
1235
1236static void send_packet(struct ipw_hardware *hw, int priority,
1237			struct ipw_tx_packet *packet)
1238{
1239	unsigned long flags;
1240
1241	spin_lock_irqsave(&hw->lock, flags);
1242	list_add_tail(&packet->queue, &hw->tx_queue[priority]);
1243	hw->tx_queued++;
1244	spin_unlock_irqrestore(&hw->lock, flags);
1245
1246	flush_packets_to_hw(hw);
1247}
1248
1249/* Create data packet, non-atomic allocation */
1250static void *alloc_data_packet(int data_size,
1251				unsigned char dest_addr,
1252				unsigned char protocol)
1253{
1254	struct ipw_tx_packet *packet = kzalloc(
1255			sizeof(struct ipw_tx_packet) + data_size,
1256			GFP_ATOMIC);
1257
1258	if (!packet)
1259		return NULL;
1260
1261	INIT_LIST_HEAD(&packet->queue);
1262	packet->dest_addr = dest_addr;
1263	packet->protocol = protocol;
1264	packet->length = data_size;
1265
1266	return packet;
1267}
1268
1269static void *alloc_ctrl_packet(int header_size,
1270			       unsigned char dest_addr,
1271			       unsigned char protocol,
1272			       unsigned char sig_no)
1273{
1274	/*
1275	 * sig_no is located right after ipw_tx_packet struct in every
1276	 * CTRL or SETUP packets, we can use ipw_control_packet as a
1277	 * common struct
1278	 */
1279	struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1280
1281	if (!packet)
1282		return NULL;
1283
1284	INIT_LIST_HEAD(&packet->header.queue);
1285	packet->header.dest_addr = dest_addr;
1286	packet->header.protocol = protocol;
1287	packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1288	packet->body.sig_no = sig_no;
1289
1290	return packet;
1291}
1292
1293int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
1294			    const unsigned char *data, unsigned int length,
1295			    void (*callback) (void *cb, unsigned int length),
1296			    void *callback_data)
1297{
1298	struct ipw_tx_packet *packet;
1299
1300	packet = alloc_data_packet(length, (channel_idx + 1),
1301			TL_PROTOCOLID_COM_DATA);
1302	if (!packet)
1303		return -ENOMEM;
1304	packet->packet_callback = callback;
1305	packet->callback_data = callback_data;
1306	memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
1307			length);
1308
1309	send_packet(hw, PRIO_DATA, packet);
1310	return 0;
1311}
1312
1313static int set_control_line(struct ipw_hardware *hw, int prio,
1314			   unsigned int channel_idx, int line, int state)
1315{
1316	struct ipw_control_packet *packet;
1317	int protocolid = TL_PROTOCOLID_COM_CTRL;
1318
1319	if (prio == PRIO_SETUP)
1320		protocolid = TL_PROTOCOLID_SETUP;
1321
1322	packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
1323			(channel_idx + 1), protocolid, line);
1324	if (!packet)
1325		return -ENOMEM;
1326	packet->header.length = sizeof(struct ipw_control_packet_body);
1327	packet->body.value = (state == 0 ? 0 : 1);
1328	send_packet(hw, prio, &packet->header);
1329	return 0;
1330}
1331
1332
1333static int set_DTR(struct ipw_hardware *hw, int priority,
1334		   unsigned int channel_idx, int state)
1335{
1336	if (state != 0)
1337		hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1338	else
1339		hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1340
1341	return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1342}
1343
1344static int set_RTS(struct ipw_hardware *hw, int priority,
1345		   unsigned int channel_idx, int state)
1346{
1347	if (state != 0)
1348		hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1349	else
1350		hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1351
1352	return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1353}
1354
1355int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1356		       int state)
1357{
1358	return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1359}
1360
1361int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1362		       int state)
1363{
1364	return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1365}
1366
1367struct ipw_setup_get_version_query_packet {
1368	struct ipw_tx_packet header;
1369	struct tl_setup_get_version_qry body;
1370};
1371
1372struct ipw_setup_config_packet {
1373	struct ipw_tx_packet header;
1374	struct tl_setup_config_msg body;
1375};
1376
1377struct ipw_setup_config_done_packet {
1378	struct ipw_tx_packet header;
1379	struct tl_setup_config_done_msg body;
1380};
1381
1382struct ipw_setup_open_packet {
1383	struct ipw_tx_packet header;
1384	struct tl_setup_open_msg body;
1385};
1386
1387struct ipw_setup_info_packet {
1388	struct ipw_tx_packet header;
1389	struct tl_setup_info_msg body;
1390};
1391
1392struct ipw_setup_reboot_msg_ack {
1393	struct ipw_tx_packet header;
1394	struct TlSetupRebootMsgAck body;
1395};
1396
1397/* This handles the actual initialization of the card */
1398static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1399{
1400	struct ipw_setup_config_packet *config_packet;
1401	struct ipw_setup_config_done_packet *config_done_packet;
1402	struct ipw_setup_open_packet *open_packet;
1403	struct ipw_setup_info_packet *info_packet;
1404	int port;
1405	unsigned int channel_idx;
1406
1407	/* generate config packet */
1408	for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1409		config_packet = alloc_ctrl_packet(
1410				sizeof(struct ipw_setup_config_packet),
1411				ADDR_SETUP_PROT,
1412				TL_PROTOCOLID_SETUP,
1413				TL_SETUP_SIGNO_CONFIG_MSG);
1414		if (!config_packet)
1415			goto exit_nomem;
1416		config_packet->header.length = sizeof(struct tl_setup_config_msg);
1417		config_packet->body.port_no = port;
1418		config_packet->body.prio_data = PRIO_DATA;
1419		config_packet->body.prio_ctrl = PRIO_CTRL;
1420		send_packet(hw, PRIO_SETUP, &config_packet->header);
1421	}
1422	config_done_packet = alloc_ctrl_packet(
1423			sizeof(struct ipw_setup_config_done_packet),
1424			ADDR_SETUP_PROT,
1425			TL_PROTOCOLID_SETUP,
1426			TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1427	if (!config_done_packet)
1428		goto exit_nomem;
1429	config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1430	send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1431
1432	/* generate open packet */
1433	for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1434		open_packet = alloc_ctrl_packet(
1435				sizeof(struct ipw_setup_open_packet),
1436				ADDR_SETUP_PROT,
1437				TL_PROTOCOLID_SETUP,
1438				TL_SETUP_SIGNO_OPEN_MSG);
1439		if (!open_packet)
1440			goto exit_nomem;
1441		open_packet->header.length = sizeof(struct tl_setup_open_msg);
1442		open_packet->body.port_no = port;
1443		send_packet(hw, PRIO_SETUP, &open_packet->header);
1444	}
1445	for (channel_idx = 0;
1446			channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1447		int ret;
1448
1449		ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1450			(hw->control_lines[channel_idx] &
1451			 IPW_CONTROL_LINE_DTR) != 0);
1452		if (ret) {
1453			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1454					": error setting DTR (%d)\n", ret);
1455			return;
1456		}
1457
1458		ret = set_RTS(hw, PRIO_SETUP, channel_idx,
1459			(hw->control_lines [channel_idx] &
1460			 IPW_CONTROL_LINE_RTS) != 0);
1461		if (ret) {
1462			printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1463					": error setting RTS (%d)\n", ret);
1464			return;
1465		}
1466	}
1467	/*
1468	 * For NDIS we assume that we are using sync PPP frames, for COM async.
1469	 * This driver uses NDIS mode too. We don't bother with translation
1470	 * from async -> sync PPP.
1471	 */
1472	info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1473			ADDR_SETUP_PROT,
1474			TL_PROTOCOLID_SETUP,
1475			TL_SETUP_SIGNO_INFO_MSG);
1476	if (!info_packet)
1477		goto exit_nomem;
1478	info_packet->header.length = sizeof(struct tl_setup_info_msg);
1479	info_packet->body.driver_type = NDISWAN_DRIVER;
1480	info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1481	info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1482	send_packet(hw, PRIO_SETUP, &info_packet->header);
1483
1484	/* Initialization is now complete, so we clear the 'to_setup' flag */
1485	hw->to_setup = 0;
1486
1487	return;
1488
1489exit_nomem:
1490	printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1491			": not enough memory to alloc control packet\n");
1492	hw->to_setup = -1;
1493}
1494
1495static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1496		unsigned char vers_no)
1497{
1498	del_timer(&hw->setup_timer);
1499	hw->initializing = 0;
1500	printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1501
1502	if (vers_no == TL_SETUP_VERSION)
1503		__handle_setup_get_version_rsp(hw);
1504	else
1505		printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1506				": invalid hardware version no %u\n",
1507				(unsigned int) vers_no);
1508}
1509
1510static void ipw_send_setup_packet(struct ipw_hardware *hw)
1511{
1512	struct ipw_setup_get_version_query_packet *ver_packet;
1513
1514	ver_packet = alloc_ctrl_packet(
1515			sizeof(struct ipw_setup_get_version_query_packet),
1516			ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1517			TL_SETUP_SIGNO_GET_VERSION_QRY);
 
 
1518	ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1519
1520	/*
1521	 * Response is handled in handle_received_SETUP_packet
1522	 */
1523	send_packet(hw, PRIO_SETUP, &ver_packet->header);
1524}
1525
1526static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1527					 unsigned int address,
1528					 const unsigned char *data, int len,
1529					 int is_last)
1530{
1531	const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
1532
1533	if (address != ADDR_SETUP_PROT) {
1534		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1535		       ": setup packet has bad address %d\n", address);
1536		return;
1537	}
1538
1539	switch (rx_msg->sig_no) {
1540	case TL_SETUP_SIGNO_GET_VERSION_RSP:
1541		if (hw->to_setup)
1542			handle_setup_get_version_rsp(hw,
1543					rx_msg->version_rsp_msg.version);
1544		break;
1545
1546	case TL_SETUP_SIGNO_OPEN_MSG:
1547		if (ipwireless_debug) {
1548			unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1549
1550			printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1551			       ": OPEN_MSG [channel %u] reply received\n",
1552			       channel_idx);
1553		}
1554		break;
1555
1556	case TL_SETUP_SIGNO_INFO_MSG_ACK:
1557		if (ipwireless_debug)
1558			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1559			       ": card successfully configured as NDISWAN\n");
1560		break;
1561
1562	case TL_SETUP_SIGNO_REBOOT_MSG:
1563		if (hw->to_setup)
1564			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1565			       ": Setup not completed - ignoring reboot msg\n");
1566		else {
1567			struct ipw_setup_reboot_msg_ack *packet;
1568
1569			printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1570			       ": Acknowledging REBOOT message\n");
1571			packet = alloc_ctrl_packet(
1572					sizeof(struct ipw_setup_reboot_msg_ack),
1573					ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1574					TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1575			if (!packet) {
1576				pr_err(IPWIRELESS_PCCARD_NAME
1577				       ": Not enough memory to send reboot packet");
1578				break;
1579			}
1580			packet->header.length =
1581				sizeof(struct TlSetupRebootMsgAck);
1582			send_packet(hw, PRIO_SETUP, &packet->header);
1583			if (hw->reboot_callback)
1584				hw->reboot_callback(hw->reboot_callback_data);
1585		}
1586		break;
1587
1588	default:
1589		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1590		       ": unknown setup message %u received\n",
1591		       (unsigned int) rx_msg->sig_no);
1592	}
1593}
1594
1595static void do_close_hardware(struct ipw_hardware *hw)
1596{
1597	unsigned int irqn;
1598
1599	if (hw->hw_version == HW_VERSION_1) {
1600		/* Disable TX and RX interrupts. */
1601		outw(0, hw->base_port + IOIER);
1602
1603		/* Acknowledge any outstanding interrupt requests */
1604		irqn = inw(hw->base_port + IOIR);
1605		if (irqn & IR_TXINTR)
1606			outw(IR_TXINTR, hw->base_port + IOIR);
1607		if (irqn & IR_RXINTR)
1608			outw(IR_RXINTR, hw->base_port + IOIR);
1609
1610		synchronize_irq(hw->irq);
1611	}
1612}
1613
1614struct ipw_hardware *ipwireless_hardware_create(void)
1615{
1616	int i;
1617	struct ipw_hardware *hw =
1618		kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1619
1620	if (!hw)
1621		return NULL;
1622
1623	hw->irq = -1;
1624	hw->initializing = 1;
1625	hw->tx_ready = 1;
1626	hw->rx_bytes_queued = 0;
1627	hw->rx_pool_size = 0;
1628	hw->last_memtx_serial = (unsigned short) 0xffff;
1629	for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1630		INIT_LIST_HEAD(&hw->tx_queue[i]);
1631
1632	INIT_LIST_HEAD(&hw->rx_queue);
1633	INIT_LIST_HEAD(&hw->rx_pool);
1634	spin_lock_init(&hw->lock);
1635	tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw);
1636	INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1637	setup_timer(&hw->setup_timer, ipwireless_setup_timer,
1638			(unsigned long) hw);
1639
1640	return hw;
1641}
1642
1643void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1644		unsigned int base_port,
1645		void __iomem *attr_memory,
1646		void __iomem *common_memory,
1647		int is_v2_card,
1648		void (*reboot_callback) (void *data),
1649		void *reboot_callback_data)
1650{
1651	if (hw->removed) {
1652		hw->removed = 0;
1653		enable_irq(hw->irq);
1654	}
1655	hw->base_port = base_port;
1656	hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
1657	hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
1658	hw->memregs_CCR = (struct MEMCCR __iomem *)
1659			((unsigned short __iomem *) attr_memory + 0x200);
1660	hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1661	hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1662	hw->reboot_callback = reboot_callback;
1663	hw->reboot_callback_data = reboot_callback_data;
1664}
1665
1666void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1667{
1668	hw->initializing = 1;
1669	hw->init_loops = 0;
1670	printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1671	       ": waiting for card to start up...\n");
1672	ipwireless_setup_timer((unsigned long) hw);
1673}
1674
1675static void ipwireless_setup_timer(unsigned long data)
1676{
1677	struct ipw_hardware *hw = (struct ipw_hardware *) data;
1678
1679	hw->init_loops++;
1680
1681	if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1682			hw->hw_version == HW_VERSION_2 &&
1683			hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1684		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1685				": failed to startup using TX2, trying TX\n");
1686
1687		hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1688		hw->init_loops = 0;
1689	}
1690	/* Give up after a certain number of retries */
1691	if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1692		printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1693		       ": card failed to start up!\n");
1694		hw->initializing = 0;
1695	} else {
1696		/* Do not attempt to write to the board if it is not present. */
1697		if (is_card_present(hw)) {
1698			unsigned long flags;
1699
1700			spin_lock_irqsave(&hw->lock, flags);
1701			hw->to_setup = 1;
1702			hw->tx_ready = 1;
1703			spin_unlock_irqrestore(&hw->lock, flags);
1704			tasklet_schedule(&hw->tasklet);
1705		}
1706
1707		mod_timer(&hw->setup_timer,
1708			jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1709	}
1710}
1711
1712/*
1713 * Stop any interrupts from executing so that, once this function returns,
1714 * other layers of the driver can be sure they won't get any more callbacks.
1715 * Thus must be called on a proper process context.
1716 */
1717void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1718{
1719	if (!hw->shutting_down) {
1720		/* Tell everyone we are going down. */
1721		hw->shutting_down = 1;
1722		del_timer(&hw->setup_timer);
1723
1724		/* Prevent the hardware from sending any more interrupts */
1725		do_close_hardware(hw);
1726	}
1727}
1728
1729void ipwireless_hardware_free(struct ipw_hardware *hw)
1730{
1731	int i;
1732	struct ipw_rx_packet *rp, *rq;
1733	struct ipw_tx_packet *tp, *tq;
1734
1735	ipwireless_stop_interrupts(hw);
1736
1737	flush_work(&hw->work_rx);
1738
1739	for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
1740		kfree(hw->packet_assembler[i]);
1741
1742	for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1743		list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
1744			list_del(&tp->queue);
1745			kfree(tp);
1746		}
1747
1748	list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
1749		list_del(&rp->queue);
1750		kfree(rp);
1751	}
1752
1753	list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
1754		list_del(&rp->queue);
1755		kfree(rp);
1756	}
1757	kfree(hw);
1758}
1759
1760/*
1761 * Associate the specified network with this hardware, so it will receive events
1762 * from it.
1763 */
1764void ipwireless_associate_network(struct ipw_hardware *hw,
1765				  struct ipw_network *network)
1766{
1767	hw->network = network;
1768}