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