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