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