]> err.no Git - linux-2.6/blob - drivers/char/pcmcia/ipwireless/hardware.c
ipwireless: Do not return value from sending funcs
[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, 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 void 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         BUG_ON(length > hw->ll_mtu);
411
412         if (ipwireless_debug)
413                 dump_data_bytes("send", data, length);
414
415         spin_lock_irqsave(&hw->lock, flags);
416
417         hw->tx_ready = 0;
418         swap_packet_bitfield_to_le(data);
419
420         if (hw->hw_version == HW_VERSION_1) {
421                 outw((unsigned short) length, hw->base_port + IODWR);
422
423                 for (i = 0; i < length; i += 2) {
424                         unsigned short d = data[i];
425                         __le16 raw_data;
426
427                         if (i + 1 < length)
428                                 d |= data[i + 1] << 8;
429                         raw_data = cpu_to_le16(d);
430                         outw(raw_data, hw->base_port + IODWR);
431                 }
432
433                 outw(DCR_TXDONE, hw->base_port + IODCR);
434         } else if (hw->hw_version == HW_VERSION_2) {
435                 outw((unsigned short) length, hw->base_port);
436
437                 for (i = 0; i < length; i += 2) {
438                         unsigned short d = data[i];
439                         __le16 raw_data;
440
441                         if (i + 1 < length)
442                                 d |= data[i + 1] << 8;
443                         raw_data = cpu_to_le16(d);
444                         outw(raw_data, hw->base_port);
445                 }
446                 while ((i & 3) != 2) {
447                         outw((unsigned short) 0xDEAD, hw->base_port);
448                         i += 2;
449                 }
450                 writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
451         }
452
453         spin_unlock_irqrestore(&hw->lock, flags);
454
455         end_write_timing(length);
456 }
457
458 static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
459 {
460         unsigned short fragment_data_len;
461         unsigned short data_left = packet->length - packet->offset;
462         unsigned short header_size;
463         union nl_packet pkt;
464
465         header_size =
466             (packet->fragment_count == 0)
467             ? NL_FIRST_PACKET_HEADER_SIZE
468             : NL_FOLLOWING_PACKET_HEADER_SIZE;
469         fragment_data_len = hw->ll_mtu - header_size;
470         if (data_left < fragment_data_len)
471                 fragment_data_len = data_left;
472
473         /*
474          * hdr_first is now in machine bitfield order, which will be swapped
475          * to le just before it goes to hw
476          */
477         pkt.hdr_first.protocol = packet->protocol;
478         pkt.hdr_first.address = packet->dest_addr;
479         pkt.hdr_first.packet_rank = 0;
480
481         /* First packet? */
482         if (packet->fragment_count == 0) {
483                 pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
484                 pkt.hdr_first.length_lsb = (unsigned char) packet->length;
485                 pkt.hdr_first.length_msb =
486                         (unsigned char) (packet->length >> 8);
487         }
488
489         memcpy(pkt.rawpkt + header_size,
490                ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
491                packet->offset, fragment_data_len);
492         packet->offset += fragment_data_len;
493         packet->fragment_count++;
494
495         /* Last packet? (May also be first packet.) */
496         if (packet->offset == packet->length)
497                 pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
498         do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
499
500         /* If this packet has unsent data, then re-queue it. */
501         if (packet->offset < packet->length) {
502                 /*
503                  * Re-queue it at the head of the highest priority queue so
504                  * it goes before all other packets
505                  */
506                 unsigned long flags;
507
508                 spin_lock_irqsave(&hw->lock, flags);
509                 list_add(&packet->queue, &hw->tx_queue[0]);
510                 hw->tx_queued++;
511                 spin_unlock_irqrestore(&hw->lock, flags);
512         } else {
513                 if (packet->packet_callback)
514                         packet->packet_callback(packet->callback_data,
515                                         packet->length);
516                 kfree(packet);
517         }
518 }
519
520 static void ipw_setup_hardware(struct ipw_hardware *hw)
521 {
522         unsigned long flags;
523
524         spin_lock_irqsave(&hw->lock, flags);
525         if (hw->hw_version == HW_VERSION_1) {
526                 /* Reset RX FIFO */
527                 outw(DCR_RXRESET, hw->base_port + IODCR);
528                 /* SB: Reset TX FIFO */
529                 outw(DCR_TXRESET, hw->base_port + IODCR);
530
531                 /* Enable TX and RX interrupts. */
532                 outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
533         } else {
534                 /*
535                  * Set INTRACK bit (bit 0), which means we must explicitly
536                  * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
537                  */
538                 unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
539
540                 csr |= 1;
541                 writew(csr, &hw->memregs_CCR->reg_config_and_status);
542         }
543         spin_unlock_irqrestore(&hw->lock, flags);
544 }
545
546 /*
547  * If 'packet' is NULL, then this function allocates a new packet, setting its
548  * length to 0 and ensuring it has the specified minimum amount of free space.
549  *
550  * If 'packet' is not NULL, then this function enlarges it if it doesn't
551  * have the specified minimum amount of free space.
552  *
553  */
554 static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
555                                            struct ipw_rx_packet *packet,
556                                            int minimum_free_space)
557 {
558
559         if (!packet) {
560                 unsigned long flags;
561
562                 spin_lock_irqsave(&hw->lock, flags);
563                 if (!list_empty(&hw->rx_pool)) {
564                         packet = list_first_entry(&hw->rx_pool,
565                                         struct ipw_rx_packet, queue);
566                         list_del(&packet->queue);
567                         hw->rx_pool_size--;
568                         spin_unlock_irqrestore(&hw->lock, flags);
569                 } else {
570                         static int min_capacity = 256;
571                         int new_capacity;
572
573                         spin_unlock_irqrestore(&hw->lock, flags);
574                         new_capacity =
575                                 (minimum_free_space > min_capacity
576                                  ? minimum_free_space
577                                  : min_capacity);
578                         packet = kmalloc(sizeof(struct ipw_rx_packet)
579                                         + new_capacity, GFP_ATOMIC);
580                         if (!packet)
581                                 return NULL;
582                         packet->capacity = new_capacity;
583                 }
584                 packet->length = 0;
585         }
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                         kfree(old_packet);
595                         return NULL;
596                 }
597                 memcpy(packet, old_packet,
598                                 sizeof(struct ipw_rx_packet)
599                                         + old_packet->length);
600                 packet->capacity = old_packet->length + minimum_free_space;
601                 kfree(old_packet);
602         }
603
604         return packet;
605 }
606
607 static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
608 {
609         if (hw->rx_pool_size > 6)
610                 kfree(packet);
611         else {
612                 hw->rx_pool_size++;
613                 list_add_tail(&packet->queue, &hw->rx_pool);
614         }
615 }
616
617 static void queue_received_packet(struct ipw_hardware *hw,
618                                   unsigned int protocol, unsigned int address,
619                                   unsigned char *data, int length, int is_last)
620 {
621         unsigned int channel_idx = address - 1;
622         struct ipw_rx_packet *packet = NULL;
623         unsigned long flags;
624
625         /* Discard packet if channel index is out of range. */
626         if (channel_idx >= NL_NUM_OF_ADDRESSES) {
627                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
628                        ": data packet has bad address %u\n", address);
629                 return;
630         }
631
632         /*
633          * ->packet_assembler is safe to touch unlocked, this is the only place
634          */
635         if (protocol == TL_PROTOCOLID_COM_DATA) {
636                 struct ipw_rx_packet **assem =
637                         &hw->packet_assembler[channel_idx];
638
639                 /*
640                  * Create a new packet, or assembler already contains one
641                  * enlarge it by 'length' bytes.
642                  */
643                 (*assem) = pool_allocate(hw, *assem, length);
644                 if (!(*assem)) {
645                         printk(KERN_ERR IPWIRELESS_PCCARD_NAME
646                                 ": no memory for incomming data packet, dropped!\n");
647                         return;
648                 }
649                 (*assem)->protocol = protocol;
650                 (*assem)->channel_idx = channel_idx;
651
652                 /* Append this packet data onto existing data. */
653                 memcpy((unsigned char *)(*assem) +
654                                sizeof(struct ipw_rx_packet)
655                                 + (*assem)->length, data, length);
656                 (*assem)->length += length;
657                 if (is_last) {
658                         packet = *assem;
659                         *assem = NULL;
660                         /* Count queued DATA bytes only */
661                         spin_lock_irqsave(&hw->lock, flags);
662                         hw->rx_bytes_queued += packet->length;
663                         spin_unlock_irqrestore(&hw->lock, flags);
664                 }
665         } else {
666                 /* If it's a CTRL packet, don't assemble, just queue it. */
667                 packet = pool_allocate(hw, NULL, length);
668                 if (!packet) {
669                         printk(KERN_ERR IPWIRELESS_PCCARD_NAME
670                                 ": no memory for incomming ctrl packet, dropped!\n");
671                         return;
672                 }
673                 packet->protocol = protocol;
674                 packet->channel_idx = channel_idx;
675                 memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
676                                 data, length);
677                 packet->length = length;
678         }
679
680         /*
681          * If this is the last packet, then send the assembled packet on to the
682          * network layer.
683          */
684         if (packet) {
685                 spin_lock_irqsave(&hw->lock, flags);
686                 list_add_tail(&packet->queue, &hw->rx_queue);
687                 /* Block reception of incoming packets if queue is full. */
688                 hw->blocking_rx =
689                         (hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
690
691                 spin_unlock_irqrestore(&hw->lock, flags);
692                 schedule_work(&hw->work_rx);
693         }
694 }
695
696 /*
697  * Workqueue callback
698  */
699 static void ipw_receive_data_work(struct work_struct *work_rx)
700 {
701         struct ipw_hardware *hw =
702             container_of(work_rx, struct ipw_hardware, work_rx);
703         unsigned long flags;
704
705         spin_lock_irqsave(&hw->lock, flags);
706         while (!list_empty(&hw->rx_queue)) {
707                 struct ipw_rx_packet *packet =
708                         list_first_entry(&hw->rx_queue,
709                                         struct ipw_rx_packet, queue);
710
711                 if (hw->shutting_down)
712                         break;
713                 list_del(&packet->queue);
714
715                 /*
716                  * Note: ipwireless_network_packet_received must be called in a
717                  * process context (i.e. via schedule_work) because the tty
718                  * output code can sleep in the tty_flip_buffer_push call.
719                  */
720                 if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
721                         if (hw->network != NULL) {
722                                 /* If the network hasn't been disconnected. */
723                                 spin_unlock_irqrestore(&hw->lock, flags);
724                                 /*
725                                  * This must run unlocked due to tty processing
726                                  * and mutex locking
727                                  */
728                                 ipwireless_network_packet_received(
729                                                 hw->network,
730                                                 packet->channel_idx,
731                                                 (unsigned char *)packet
732                                                 + sizeof(struct ipw_rx_packet),
733                                                 packet->length);
734                                 spin_lock_irqsave(&hw->lock, flags);
735                         }
736                         /* Count queued DATA bytes only */
737                         hw->rx_bytes_queued -= packet->length;
738                 } else {
739                         /*
740                          * This is safe to be called locked, callchain does
741                          * not block
742                          */
743                         handle_received_CTRL_packet(hw, packet->channel_idx,
744                                         (unsigned char *)packet
745                                         + sizeof(struct ipw_rx_packet),
746                                         packet->length);
747                 }
748                 pool_free(hw, packet);
749                 /*
750                  * Unblock reception of incoming packets if queue is no longer
751                  * full.
752                  */
753                 hw->blocking_rx =
754                         hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
755                 if (hw->shutting_down)
756                         break;
757         }
758         spin_unlock_irqrestore(&hw->lock, flags);
759 }
760
761 static void handle_received_CTRL_packet(struct ipw_hardware *hw,
762                                         unsigned int channel_idx,
763                                         unsigned char *data, int len)
764 {
765         struct ipw_control_packet_body *body =
766                 (struct ipw_control_packet_body *) data;
767         unsigned int changed_mask;
768
769         if (len != sizeof(struct ipw_control_packet_body)) {
770                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
771                        ": control packet was %d bytes - wrong size!\n",
772                        len);
773                 return;
774         }
775
776         switch (body->sig_no) {
777         case COMCTRL_CTS:
778                 changed_mask = IPW_CONTROL_LINE_CTS;
779                 break;
780         case COMCTRL_DCD:
781                 changed_mask = IPW_CONTROL_LINE_DCD;
782                 break;
783         case COMCTRL_DSR:
784                 changed_mask = IPW_CONTROL_LINE_DSR;
785                 break;
786         case COMCTRL_RI:
787                 changed_mask = IPW_CONTROL_LINE_RI;
788                 break;
789         default:
790                 changed_mask = 0;
791         }
792
793         if (changed_mask != 0) {
794                 if (body->value)
795                         hw->control_lines[channel_idx] |= changed_mask;
796                 else
797                         hw->control_lines[channel_idx] &= ~changed_mask;
798                 if (hw->network)
799                         ipwireless_network_notify_control_line_change(
800                                         hw->network,
801                                         channel_idx,
802                                         hw->control_lines[channel_idx],
803                                         changed_mask);
804         }
805 }
806
807 static void handle_received_packet(struct ipw_hardware *hw,
808                                    union nl_packet *packet,
809                                    unsigned short len)
810 {
811         unsigned int protocol = packet->hdr.protocol;
812         unsigned int address = packet->hdr.address;
813         unsigned int header_length;
814         unsigned char *data;
815         unsigned int data_len;
816         int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
817
818         if (packet->hdr.packet_rank & NL_FIRST_PACKET)
819                 header_length = NL_FIRST_PACKET_HEADER_SIZE;
820         else
821                 header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
822
823         data = packet->rawpkt + header_length;
824         data_len = len - header_length;
825         switch (protocol) {
826         case TL_PROTOCOLID_COM_DATA:
827         case TL_PROTOCOLID_COM_CTRL:
828                 queue_received_packet(hw, protocol, address, data, data_len,
829                                 is_last);
830                 break;
831         case TL_PROTOCOLID_SETUP:
832                 handle_received_SETUP_packet(hw, address, data, data_len,
833                                 is_last);
834                 break;
835         }
836 }
837
838 static void acknowledge_data_read(struct ipw_hardware *hw)
839 {
840         if (hw->hw_version == HW_VERSION_1)
841                 outw(DCR_RXDONE, hw->base_port + IODCR);
842         else
843                 writew(MEMRX_PCINTACKK,
844                                 &hw->memory_info_regs->memreg_pc_interrupt_ack);
845 }
846
847 /*
848  * Retrieve a packet from the IPW hardware.
849  */
850 static void do_receive_packet(struct ipw_hardware *hw)
851 {
852         unsigned len;
853         unsigned i;
854         unsigned char pkt[LL_MTU_MAX];
855
856         start_timing();
857
858         if (hw->hw_version == HW_VERSION_1) {
859                 len = inw(hw->base_port + IODRR);
860                 if (len > hw->ll_mtu) {
861                         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
862                                ": received a packet of %u bytes - longer than the MTU!\n", len);
863                         outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
864                         return;
865                 }
866
867                 for (i = 0; i < len; i += 2) {
868                         __le16 raw_data = inw(hw->base_port + IODRR);
869                         unsigned short data = le16_to_cpu(raw_data);
870
871                         pkt[i] = (unsigned char) data;
872                         pkt[i + 1] = (unsigned char) (data >> 8);
873                 }
874         } else {
875                 len = inw(hw->base_port);
876                 if (len > hw->ll_mtu) {
877                         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
878                                ": received a packet of %u bytes - 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);
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);
894                         i += 2;
895                 }
896         }
897
898         acknowledge_data_read(hw);
899
900         swap_packet_bitfield_from_le(pkt);
901
902         if (ipwireless_debug)
903                 dump_data_bytes("recv", pkt, len);
904
905         handle_received_packet(hw, (union nl_packet *) pkt, len);
906
907         end_read_timing(len);
908 }
909
910 static int get_current_packet_priority(struct ipw_hardware *hw)
911 {
912         /*
913          * If we're initializing, don't send anything of higher priority than
914          * PRIO_SETUP.  The network layer therefore need not care about
915          * hardware initialization - any of its stuff will simply be queued
916          * until setup is complete.
917          */
918         return (hw->to_setup || hw->initializing
919                         ? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
920 }
921
922 /*
923  * return 1 if something has been received from hw
924  */
925 static int get_packets_from_hw(struct ipw_hardware *hw)
926 {
927         int received = 0;
928         unsigned long flags;
929
930         spin_lock_irqsave(&hw->lock, flags);
931         while (hw->rx_ready && !hw->blocking_rx) {
932                 received = 1;
933                 hw->rx_ready--;
934                 spin_unlock_irqrestore(&hw->lock, flags);
935
936                 do_receive_packet(hw);
937
938                 spin_lock_irqsave(&hw->lock, flags);
939         }
940         spin_unlock_irqrestore(&hw->lock, flags);
941
942         return received;
943 }
944
945 /*
946  * Send pending packet up to given priority, prioritize SETUP data until
947  * hardware is fully setup.
948  *
949  * return 1 if more packets can be sent
950  */
951 static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
952 {
953         int more_to_send = 0;
954         unsigned long flags;
955
956         spin_lock_irqsave(&hw->lock, flags);
957         if (hw->tx_queued && hw->tx_ready) {
958                 int priority;
959                 struct ipw_tx_packet *packet = NULL;
960
961                 /* Pick a packet */
962                 for (priority = 0; priority < priority_limit; priority++) {
963                         if (!list_empty(&hw->tx_queue[priority])) {
964                                 packet = list_first_entry(
965                                                 &hw->tx_queue[priority],
966                                                 struct ipw_tx_packet,
967                                                 queue);
968
969                                 hw->tx_queued--;
970                                 list_del(&packet->queue);
971
972                                 break;
973                         }
974                 }
975                 if (!packet) {
976                         hw->tx_queued = 0;
977                         spin_unlock_irqrestore(&hw->lock, flags);
978                         return 0;
979                 }
980
981                 spin_unlock_irqrestore(&hw->lock, flags);
982
983                 /* Send */
984                 do_send_packet(hw, packet);
985
986                 /* Check if more to send */
987                 spin_lock_irqsave(&hw->lock, flags);
988                 for (priority = 0; priority < priority_limit; priority++)
989                         if (!list_empty(&hw->tx_queue[priority])) {
990                                 more_to_send = 1;
991                                 break;
992                         }
993
994                 if (!more_to_send)
995                         hw->tx_queued = 0;
996         }
997         spin_unlock_irqrestore(&hw->lock, flags);
998
999         return more_to_send;
1000 }
1001
1002 /*
1003  * Send and receive all queued packets.
1004  */
1005 static void ipwireless_do_tasklet(unsigned long hw_)
1006 {
1007         struct ipw_hardware *hw = (struct ipw_hardware *) hw_;
1008         unsigned long flags;
1009
1010         spin_lock_irqsave(&hw->lock, flags);
1011         if (hw->shutting_down) {
1012                 spin_unlock_irqrestore(&hw->lock, flags);
1013                 return;
1014         }
1015
1016         if (hw->to_setup == 1) {
1017                 /*
1018                  * Initial setup data sent to hardware
1019                  */
1020                 hw->to_setup = 2;
1021                 spin_unlock_irqrestore(&hw->lock, flags);
1022
1023                 ipw_setup_hardware(hw);
1024                 ipw_send_setup_packet(hw);
1025
1026                 send_pending_packet(hw, PRIO_SETUP + 1);
1027                 get_packets_from_hw(hw);
1028         } else {
1029                 int priority_limit = get_current_packet_priority(hw);
1030                 int again;
1031
1032                 spin_unlock_irqrestore(&hw->lock, flags);
1033
1034                 do {
1035                         again = send_pending_packet(hw, priority_limit);
1036                         again |= get_packets_from_hw(hw);
1037                 } while (again);
1038         }
1039 }
1040
1041 /*
1042  * return true if the card is physically present.
1043  */
1044 static int is_card_present(struct ipw_hardware *hw)
1045 {
1046         if (hw->hw_version == HW_VERSION_1)
1047                 return inw(hw->base_port + IOIR) != 0xFFFF;
1048         else
1049                 return readl(&hw->memory_info_regs->memreg_card_present) ==
1050                     CARD_PRESENT_VALUE;
1051 }
1052
1053 static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1054                                                   struct ipw_hardware *hw)
1055 {
1056         unsigned short irqn;
1057
1058         irqn = inw(hw->base_port + IOIR);
1059
1060         /* Check if card is present */
1061         if (irqn == 0xFFFF)
1062                 return IRQ_NONE;
1063         else if (irqn != 0) {
1064                 unsigned short ack = 0;
1065                 unsigned long flags;
1066
1067                 /* Transmit complete. */
1068                 if (irqn & IR_TXINTR) {
1069                         ack |= IR_TXINTR;
1070                         spin_lock_irqsave(&hw->lock, flags);
1071                         hw->tx_ready = 1;
1072                         spin_unlock_irqrestore(&hw->lock, flags);
1073                 }
1074                 /* Received data */
1075                 if (irqn & IR_RXINTR) {
1076                         ack |= IR_RXINTR;
1077                         spin_lock_irqsave(&hw->lock, flags);
1078                         hw->rx_ready++;
1079                         spin_unlock_irqrestore(&hw->lock, flags);
1080                 }
1081                 if (ack != 0) {
1082                         outw(ack, hw->base_port + IOIR);
1083                         tasklet_schedule(&hw->tasklet);
1084                 }
1085                 return IRQ_HANDLED;
1086         }
1087         return IRQ_NONE;
1088 }
1089
1090 static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1091 {
1092         unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1093
1094         csr &= 0xfffd;
1095         writew(csr, &hw->memregs_CCR->reg_config_and_status);
1096 }
1097
1098 static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1099                                                      struct ipw_hardware *hw)
1100 {
1101         int tx = 0;
1102         int rx = 0;
1103         int rx_repeat = 0;
1104         int try_mem_tx_old;
1105         unsigned long flags;
1106
1107         do {
1108
1109         unsigned short memtx = readw(hw->memreg_tx);
1110         unsigned short memtx_serial;
1111         unsigned short memrxdone =
1112                 readw(&hw->memory_info_regs->memreg_rx_done);
1113
1114         try_mem_tx_old = 0;
1115
1116         /* check whether the interrupt was generated by ipwireless card */
1117         if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1118
1119                 /* check if the card uses memreg_tx_old register */
1120                 if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1121                         memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1122                         if (memtx & MEMTX_TX) {
1123                                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1124                                         ": Using memreg_tx_old\n");
1125                                 hw->memreg_tx =
1126                                         &hw->memory_info_regs->memreg_tx_old;
1127                         } else {
1128                                 return IRQ_NONE;
1129                         }
1130                 } else
1131                         return IRQ_NONE;
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->lock, flags);
1151                                 hw->rx_ready++;
1152                                 spin_unlock_irqrestore(&hw->lock, 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->lock, flags);
1168                                 hw->rx_ready++;
1169                                 spin_unlock_irqrestore(&hw->lock, 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->lock, flags);
1177                 hw->tx_ready = 1;
1178                 spin_unlock_irqrestore(&hw->lock, 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 - switching to the old memreg_tx\n");
1197                                 hw->memreg_tx =
1198                                         &hw->memory_info_regs->memreg_tx_old;
1199                                 try_mem_tx_old = 1;
1200                         }
1201                 } else
1202                         printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1203                                         ": spurious interrupt - old_tx mode\n");
1204         }
1205
1206         } while (try_mem_tx_old == 1);
1207
1208         return IRQ_HANDLED;
1209 }
1210
1211 irqreturn_t ipwireless_interrupt(int irq, void *dev_id)
1212 {
1213         struct ipw_hardware *hw = dev_id;
1214
1215         if (hw->hw_version == HW_VERSION_1)
1216                 return ipwireless_handle_v1_interrupt(irq, hw);
1217         else
1218                 return ipwireless_handle_v2_v3_interrupt(irq, hw);
1219 }
1220
1221 static void flush_packets_to_hw(struct ipw_hardware *hw)
1222 {
1223         int priority_limit;
1224         unsigned long flags;
1225
1226         spin_lock_irqsave(&hw->lock, flags);
1227         priority_limit = get_current_packet_priority(hw);
1228         spin_unlock_irqrestore(&hw->lock, flags);
1229
1230         while (send_pending_packet(hw, priority_limit));
1231 }
1232
1233 static void send_packet(struct ipw_hardware *hw, int priority,
1234                         struct ipw_tx_packet *packet)
1235 {
1236         unsigned long flags;
1237
1238         spin_lock_irqsave(&hw->lock, flags);
1239         list_add_tail(&packet->queue, &hw->tx_queue[priority]);
1240         hw->tx_queued++;
1241         spin_unlock_irqrestore(&hw->lock, flags);
1242
1243         flush_packets_to_hw(hw);
1244 }
1245
1246 /* Create data packet, non-atomic allocation */
1247 static void *alloc_data_packet(int data_size,
1248                                 unsigned char dest_addr,
1249                                 unsigned char protocol)
1250 {
1251         struct ipw_tx_packet *packet = kzalloc(
1252                         sizeof(struct ipw_tx_packet) + data_size,
1253                         GFP_ATOMIC);
1254
1255         if (!packet)
1256                 return NULL;
1257
1258         INIT_LIST_HEAD(&packet->queue);
1259         packet->dest_addr = dest_addr;
1260         packet->protocol = protocol;
1261         packet->length = data_size;
1262
1263         return packet;
1264 }
1265
1266 static void *alloc_ctrl_packet(int header_size,
1267                                unsigned char dest_addr,
1268                                unsigned char protocol,
1269                                unsigned char sig_no)
1270 {
1271         /*
1272          * sig_no is located right after ipw_tx_packet struct in every
1273          * CTRL or SETUP packets, we can use ipw_control_packet as a
1274          * common struct
1275          */
1276         struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1277
1278         if (!packet)
1279                 return NULL;
1280
1281         INIT_LIST_HEAD(&packet->header.queue);
1282         packet->header.dest_addr = dest_addr;
1283         packet->header.protocol = protocol;
1284         packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1285         packet->body.sig_no = sig_no;
1286
1287         return packet;
1288 }
1289
1290 int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
1291                             unsigned char *data, unsigned int length,
1292                             void (*callback) (void *cb, unsigned int length),
1293                             void *callback_data)
1294 {
1295         struct ipw_tx_packet *packet;
1296
1297         packet = alloc_data_packet(length, (channel_idx + 1),
1298                         TL_PROTOCOLID_COM_DATA);
1299         if (!packet)
1300                 return -ENOMEM;
1301         packet->packet_callback = callback;
1302         packet->callback_data = callback_data;
1303         memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
1304                         length);
1305
1306         send_packet(hw, PRIO_DATA, packet);
1307         return 0;
1308 }
1309
1310 static int set_control_line(struct ipw_hardware *hw, int prio,
1311                            unsigned int channel_idx, int line, int state)
1312 {
1313         struct ipw_control_packet *packet;
1314         int protocolid = TL_PROTOCOLID_COM_CTRL;
1315
1316         if (prio == PRIO_SETUP)
1317                 protocolid = TL_PROTOCOLID_SETUP;
1318
1319         packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
1320                         (channel_idx + 1), protocolid, line);
1321         if (!packet)
1322                 return -ENOMEM;
1323         packet->header.length = sizeof(struct ipw_control_packet_body);
1324         packet->body.value = (state == 0 ? 0 : 1);
1325         send_packet(hw, prio, &packet->header);
1326         return 0;
1327 }
1328
1329
1330 static int set_DTR(struct ipw_hardware *hw, int priority,
1331                    unsigned int channel_idx, int state)
1332 {
1333         if (state != 0)
1334                 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1335         else
1336                 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1337
1338         return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1339 }
1340
1341 static int set_RTS(struct ipw_hardware *hw, int priority,
1342                    unsigned int channel_idx, int state)
1343 {
1344         if (state != 0)
1345                 hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1346         else
1347                 hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1348
1349         return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1350 }
1351
1352 int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1353                        int state)
1354 {
1355         return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1356 }
1357
1358 int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1359                        int state)
1360 {
1361         return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1362 }
1363
1364 struct ipw_setup_get_version_query_packet {
1365         struct ipw_tx_packet header;
1366         struct tl_setup_get_version_qry body;
1367 };
1368
1369 struct ipw_setup_config_packet {
1370         struct ipw_tx_packet header;
1371         struct tl_setup_config_msg body;
1372 };
1373
1374 struct ipw_setup_config_done_packet {
1375         struct ipw_tx_packet header;
1376         struct tl_setup_config_done_msg body;
1377 };
1378
1379 struct ipw_setup_open_packet {
1380         struct ipw_tx_packet header;
1381         struct tl_setup_open_msg body;
1382 };
1383
1384 struct ipw_setup_info_packet {
1385         struct ipw_tx_packet header;
1386         struct tl_setup_info_msg body;
1387 };
1388
1389 struct ipw_setup_reboot_msg_ack {
1390         struct ipw_tx_packet header;
1391         struct TlSetupRebootMsgAck body;
1392 };
1393
1394 /* This handles the actual initialization of the card */
1395 static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1396 {
1397         struct ipw_setup_config_packet *config_packet;
1398         struct ipw_setup_config_done_packet *config_done_packet;
1399         struct ipw_setup_open_packet *open_packet;
1400         struct ipw_setup_info_packet *info_packet;
1401         int port;
1402         unsigned int channel_idx;
1403
1404         /* generate config packet */
1405         for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1406                 config_packet = alloc_ctrl_packet(
1407                                 sizeof(struct ipw_setup_config_packet),
1408                                 ADDR_SETUP_PROT,
1409                                 TL_PROTOCOLID_SETUP,
1410                                 TL_SETUP_SIGNO_CONFIG_MSG);
1411                 if (!config_packet)
1412                         goto exit_nomem;
1413                 config_packet->header.length = sizeof(struct tl_setup_config_msg);
1414                 config_packet->body.port_no = port;
1415                 config_packet->body.prio_data = PRIO_DATA;
1416                 config_packet->body.prio_ctrl = PRIO_CTRL;
1417                 send_packet(hw, PRIO_SETUP, &config_packet->header);
1418         }
1419         config_done_packet = alloc_ctrl_packet(
1420                         sizeof(struct ipw_setup_config_done_packet),
1421                         ADDR_SETUP_PROT,
1422                         TL_PROTOCOLID_SETUP,
1423                         TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1424         if (!config_done_packet)
1425                 goto exit_nomem;
1426         config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1427         send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1428
1429         /* generate open packet */
1430         for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1431                 open_packet = alloc_ctrl_packet(
1432                                 sizeof(struct ipw_setup_open_packet),
1433                                 ADDR_SETUP_PROT,
1434                                 TL_PROTOCOLID_SETUP,
1435                                 TL_SETUP_SIGNO_OPEN_MSG);
1436                 if (!open_packet)
1437                         goto exit_nomem;
1438                 open_packet->header.length = sizeof(struct tl_setup_open_msg);
1439                 open_packet->body.port_no = port;
1440                 send_packet(hw, PRIO_SETUP, &open_packet->header);
1441         }
1442         for (channel_idx = 0;
1443                         channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1444                 int ret;
1445
1446                 ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1447                         (hw->control_lines[channel_idx] &
1448                          IPW_CONTROL_LINE_DTR) != 0);
1449                 if (ret) {
1450                         printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1451                                         ": error setting DTR (%d)\n", ret);
1452                         return;
1453                 }
1454
1455                 set_RTS(hw, PRIO_SETUP, channel_idx,
1456                         (hw->control_lines [channel_idx] &
1457                          IPW_CONTROL_LINE_RTS) != 0);
1458                 if (ret) {
1459                         printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1460                                         ": error setting RTS (%d)\n", ret);
1461                         return;
1462                 }
1463         }
1464         /*
1465          * For NDIS we assume that we are using sync PPP frames, for COM async.
1466          * This driver uses NDIS mode too. We don't bother with translation
1467          * from async -> sync PPP.
1468          */
1469         info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1470                         ADDR_SETUP_PROT,
1471                         TL_PROTOCOLID_SETUP,
1472                         TL_SETUP_SIGNO_INFO_MSG);
1473         if (!info_packet)
1474                 goto exit_nomem;
1475         info_packet->header.length = sizeof(struct tl_setup_info_msg);
1476         info_packet->body.driver_type = NDISWAN_DRIVER;
1477         info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1478         info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1479         send_packet(hw, PRIO_SETUP, &info_packet->header);
1480
1481         /* Initialization is now complete, so we clear the 'to_setup' flag */
1482         hw->to_setup = 0;
1483
1484         return;
1485
1486 exit_nomem:
1487         printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1488                         ": not enough memory to alloc control packet\n");
1489         hw->to_setup = -1;
1490 }
1491
1492 static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1493                 unsigned char vers_no)
1494 {
1495         del_timer(&hw->setup_timer);
1496         hw->initializing = 0;
1497         printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1498
1499         if (vers_no == TL_SETUP_VERSION)
1500                 __handle_setup_get_version_rsp(hw);
1501         else
1502                 printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1503                                 ": invalid hardware version no %u\n",
1504                                 (unsigned int) vers_no);
1505 }
1506
1507 static void ipw_send_setup_packet(struct ipw_hardware *hw)
1508 {
1509         struct ipw_setup_get_version_query_packet *ver_packet;
1510
1511         ver_packet = alloc_ctrl_packet(
1512                         sizeof(struct ipw_setup_get_version_query_packet),
1513                         ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1514                         TL_SETUP_SIGNO_GET_VERSION_QRY);
1515         ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1516
1517         /*
1518          * Response is handled in handle_received_SETUP_packet
1519          */
1520         send_packet(hw, PRIO_SETUP, &ver_packet->header);
1521 }
1522
1523 static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1524                                          unsigned int address,
1525                                          unsigned char *data, int len,
1526                                          int is_last)
1527 {
1528         union ipw_setup_rx_msg *rx_msg = (union ipw_setup_rx_msg *) data;
1529
1530         if (address != ADDR_SETUP_PROT) {
1531                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1532                        ": setup packet has bad address %d\n", address);
1533                 return;
1534         }
1535
1536         switch (rx_msg->sig_no) {
1537         case TL_SETUP_SIGNO_GET_VERSION_RSP:
1538                 if (hw->to_setup)
1539                         handle_setup_get_version_rsp(hw,
1540                                         rx_msg->version_rsp_msg.version);
1541                 break;
1542
1543         case TL_SETUP_SIGNO_OPEN_MSG:
1544                 if (ipwireless_debug) {
1545                         unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1546
1547                         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1548                                ": OPEN_MSG [channel %u] reply received\n",
1549                                channel_idx);
1550                 }
1551                 break;
1552
1553         case TL_SETUP_SIGNO_INFO_MSG_ACK:
1554                 if (ipwireless_debug)
1555                         printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1556                                ": card successfully configured as NDISWAN\n");
1557                 break;
1558
1559         case TL_SETUP_SIGNO_REBOOT_MSG:
1560                 if (hw->to_setup)
1561                         printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1562                                ": Setup not completed - ignoring reboot msg\n");
1563                 else {
1564                         struct ipw_setup_reboot_msg_ack *packet;
1565
1566                         printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1567                                ": Acknowledging REBOOT message\n");
1568                         packet = alloc_ctrl_packet(
1569                                         sizeof(struct ipw_setup_reboot_msg_ack),
1570                                         ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1571                                         TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1572                         packet->header.length =
1573                                 sizeof(struct TlSetupRebootMsgAck);
1574                         send_packet(hw, PRIO_SETUP, &packet->header);
1575                         if (hw->reboot_callback)
1576                                 hw->reboot_callback(hw->reboot_callback_data);
1577                 }
1578                 break;
1579
1580         default:
1581                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1582                        ": unknown setup message %u received\n",
1583                        (unsigned int) rx_msg->sig_no);
1584         }
1585 }
1586
1587 static void do_close_hardware(struct ipw_hardware *hw)
1588 {
1589         unsigned int irqn;
1590
1591         if (hw->hw_version == HW_VERSION_1) {
1592                 /* Disable TX and RX interrupts. */
1593                 outw(0, hw->base_port + IOIER);
1594
1595                 /* Acknowledge any outstanding interrupt requests */
1596                 irqn = inw(hw->base_port + IOIR);
1597                 if (irqn & IR_TXINTR)
1598                         outw(IR_TXINTR, hw->base_port + IOIR);
1599                 if (irqn & IR_RXINTR)
1600                         outw(IR_RXINTR, hw->base_port + IOIR);
1601
1602                 synchronize_irq(hw->irq);
1603         }
1604 }
1605
1606 struct ipw_hardware *ipwireless_hardware_create(void)
1607 {
1608         int i;
1609         struct ipw_hardware *hw =
1610                 kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1611
1612         if (!hw)
1613                 return NULL;
1614
1615         hw->irq = -1;
1616         hw->initializing = 1;
1617         hw->tx_ready = 1;
1618         hw->rx_bytes_queued = 0;
1619         hw->rx_pool_size = 0;
1620         hw->last_memtx_serial = (unsigned short) 0xffff;
1621         for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1622                 INIT_LIST_HEAD(&hw->tx_queue[i]);
1623
1624         INIT_LIST_HEAD(&hw->rx_queue);
1625         INIT_LIST_HEAD(&hw->rx_pool);
1626         spin_lock_init(&hw->lock);
1627         tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw);
1628         INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1629         setup_timer(&hw->setup_timer, ipwireless_setup_timer,
1630                         (unsigned long) hw);
1631
1632         return hw;
1633 }
1634
1635 void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1636                 unsigned int base_port,
1637                 void __iomem *attr_memory,
1638                 void __iomem *common_memory,
1639                 int is_v2_card,
1640                 void (*reboot_callback) (void *data),
1641                 void *reboot_callback_data)
1642 {
1643         if (hw->removed) {
1644                 hw->removed = 0;
1645                 enable_irq(hw->irq);
1646         }
1647         hw->base_port = base_port;
1648         hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
1649         hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
1650         hw->memregs_CCR = (struct MEMCCR __iomem *)
1651                         ((unsigned short __iomem *) attr_memory + 0x200);
1652         hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1653         hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1654         hw->reboot_callback = reboot_callback;
1655         hw->reboot_callback_data = reboot_callback_data;
1656 }
1657
1658 void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1659 {
1660         hw->initializing = 1;
1661         hw->init_loops = 0;
1662         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1663                ": waiting for card to start up...\n");
1664         ipwireless_setup_timer((unsigned long) hw);
1665 }
1666
1667 static void ipwireless_setup_timer(unsigned long data)
1668 {
1669         struct ipw_hardware *hw = (struct ipw_hardware *) data;
1670
1671         hw->init_loops++;
1672
1673         if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1674                         hw->hw_version == HW_VERSION_2 &&
1675                         hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1676                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1677                                 ": failed to startup using TX2, trying TX\n");
1678
1679                 hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1680                 hw->init_loops = 0;
1681         }
1682         /* Give up after a certain number of retries */
1683         if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1684                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1685                        ": card failed to start up!\n");
1686                 hw->initializing = 0;
1687         } else {
1688                 /* Do not attempt to write to the board if it is not present. */
1689                 if (is_card_present(hw)) {
1690                         unsigned long flags;
1691
1692                         spin_lock_irqsave(&hw->lock, flags);
1693                         hw->to_setup = 1;
1694                         hw->tx_ready = 1;
1695                         spin_unlock_irqrestore(&hw->lock, flags);
1696                         tasklet_schedule(&hw->tasklet);
1697                 }
1698
1699                 mod_timer(&hw->setup_timer,
1700                         jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1701         }
1702 }
1703
1704 /*
1705  * Stop any interrupts from executing so that, once this function returns,
1706  * other layers of the driver can be sure they won't get any more callbacks.
1707  * Thus must be called on a proper process context.
1708  */
1709 void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1710 {
1711         if (!hw->shutting_down) {
1712                 /* Tell everyone we are going down. */
1713                 hw->shutting_down = 1;
1714                 del_timer(&hw->setup_timer);
1715
1716                 /* Prevent the hardware from sending any more interrupts */
1717                 do_close_hardware(hw);
1718         }
1719 }
1720
1721 void ipwireless_hardware_free(struct ipw_hardware *hw)
1722 {
1723         int i;
1724         struct ipw_rx_packet *rp, *rq;
1725         struct ipw_tx_packet *tp, *tq;
1726
1727         ipwireless_stop_interrupts(hw);
1728
1729         flush_scheduled_work();
1730
1731         for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
1732                 if (hw->packet_assembler[i] != NULL)
1733                         kfree(hw->packet_assembler[i]);
1734
1735         for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1736                 list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
1737                         list_del(&tp->queue);
1738                         kfree(tp);
1739                 }
1740
1741         list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
1742                 list_del(&rp->queue);
1743                 kfree(rp);
1744         }
1745
1746         list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
1747                 list_del(&rp->queue);
1748                 kfree(rp);
1749         }
1750         kfree(hw);
1751 }
1752
1753 /*
1754  * Associate the specified network with this hardware, so it will receive events
1755  * from it.
1756  */
1757 void ipwireless_associate_network(struct ipw_hardware *hw,
1758                                   struct ipw_network *network)
1759 {
1760         hw->network = network;
1761 }