OSDN Git Service

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