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