Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * IPWireless 3G PCMCIA Network Driver
0004  *
0005  * Original code
0006  *   by Stephen Blackheath <stephen@blacksapphire.com>,
0007  *      Ben Martel <benm@symmetric.co.nz>
0008  *
0009  * Copyrighted as follows:
0010  *   Copyright (C) 2004 by Symmetric Systems Ltd (NZ)
0011  *
0012  * Various driver changes and rewrites, port to new kernels
0013  *   Copyright (C) 2006-2007 Jiri Kosina
0014  *
0015  * Misc code cleanups and updates
0016  *   Copyright (C) 2007 David Sterba
0017  */
0018 
0019 #include <linux/interrupt.h>
0020 #include <linux/io.h>
0021 #include <linux/irq.h>
0022 #include <linux/kernel.h>
0023 #include <linux/list.h>
0024 #include <linux/slab.h>
0025 
0026 #include "hardware.h"
0027 #include "setup_protocol.h"
0028 #include "network.h"
0029 #include "main.h"
0030 
0031 static void ipw_send_setup_packet(struct ipw_hardware *hw);
0032 static void handle_received_SETUP_packet(struct ipw_hardware *ipw,
0033                      unsigned int address,
0034                      const unsigned char *data, int len,
0035                      int is_last);
0036 static void ipwireless_setup_timer(struct timer_list *t);
0037 static void handle_received_CTRL_packet(struct ipw_hardware *hw,
0038         unsigned int channel_idx, const unsigned char *data, int len);
0039 
0040 /*#define TIMING_DIAGNOSTICS*/
0041 
0042 #ifdef TIMING_DIAGNOSTICS
0043 
0044 static struct timing_stats {
0045     unsigned long last_report_time;
0046     unsigned long read_time;
0047     unsigned long write_time;
0048     unsigned long read_bytes;
0049     unsigned long write_bytes;
0050     unsigned long start_time;
0051 };
0052 
0053 static void start_timing(void)
0054 {
0055     timing_stats.start_time = jiffies;
0056 }
0057 
0058 static void end_read_timing(unsigned length)
0059 {
0060     timing_stats.read_time += (jiffies - start_time);
0061     timing_stats.read_bytes += length + 2;
0062     report_timing();
0063 }
0064 
0065 static void end_write_timing(unsigned length)
0066 {
0067     timing_stats.write_time += (jiffies - start_time);
0068     timing_stats.write_bytes += length + 2;
0069     report_timing();
0070 }
0071 
0072 static void report_timing(void)
0073 {
0074     unsigned long since = jiffies - timing_stats.last_report_time;
0075 
0076     /* If it's been more than one second... */
0077     if (since >= HZ) {
0078         int first = (timing_stats.last_report_time == 0);
0079 
0080         timing_stats.last_report_time = jiffies;
0081         if (!first)
0082             printk(KERN_INFO IPWIRELESS_PCCARD_NAME
0083                    ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
0084                    jiffies_to_usecs(since),
0085                    timing_stats.read_bytes,
0086                    jiffies_to_usecs(timing_stats.read_time),
0087                    timing_stats.write_bytes,
0088                    jiffies_to_usecs(timing_stats.write_time));
0089 
0090         timing_stats.read_time = 0;
0091         timing_stats.write_time = 0;
0092         timing_stats.read_bytes = 0;
0093         timing_stats.write_bytes = 0;
0094     }
0095 }
0096 #else
0097 static void start_timing(void) { }
0098 static void end_read_timing(unsigned length) { }
0099 static void end_write_timing(unsigned length) { }
0100 #endif
0101 
0102 /* Imported IPW definitions */
0103 
0104 #define LL_MTU_V1 318
0105 #define LL_MTU_V2 250
0106 #define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
0107 
0108 #define PRIO_DATA  2
0109 #define PRIO_CTRL  1
0110 #define PRIO_SETUP 0
0111 
0112 /* Addresses */
0113 #define ADDR_SETUP_PROT 0
0114 
0115 /* Protocol ids */
0116 enum {
0117     /* Identifier for the Com Data protocol */
0118     TL_PROTOCOLID_COM_DATA = 0,
0119 
0120     /* Identifier for the Com Control protocol */
0121     TL_PROTOCOLID_COM_CTRL = 1,
0122 
0123     /* Identifier for the Setup protocol */
0124     TL_PROTOCOLID_SETUP = 2
0125 };
0126 
0127 /* Number of bytes in NL packet header (cannot do
0128  * sizeof(nl_packet_header) since it's a bitfield) */
0129 #define NL_FIRST_PACKET_HEADER_SIZE        3
0130 
0131 /* Number of bytes in NL packet header (cannot do
0132  * sizeof(nl_packet_header) since it's a bitfield) */
0133 #define NL_FOLLOWING_PACKET_HEADER_SIZE    1
0134 
0135 struct nl_first_packet_header {
0136     unsigned char protocol:3;
0137     unsigned char address:3;
0138     unsigned char packet_rank:2;
0139     unsigned char length_lsb;
0140     unsigned char length_msb;
0141 };
0142 
0143 struct nl_packet_header {
0144     unsigned char protocol:3;
0145     unsigned char address:3;
0146     unsigned char packet_rank:2;
0147 };
0148 
0149 /* Value of 'packet_rank' above */
0150 #define NL_INTERMEDIATE_PACKET    0x0
0151 #define NL_LAST_PACKET            0x1
0152 #define NL_FIRST_PACKET           0x2
0153 
0154 union nl_packet {
0155     /* Network packet header of the first packet (a special case) */
0156     struct nl_first_packet_header hdr_first;
0157     /* Network packet header of the following packets (if any) */
0158     struct nl_packet_header hdr;
0159     /* Complete network packet (header + data) */
0160     unsigned char rawpkt[LL_MTU_MAX];
0161 } __attribute__ ((__packed__));
0162 
0163 #define HW_VERSION_UNKNOWN -1
0164 #define HW_VERSION_1 1
0165 #define HW_VERSION_2 2
0166 
0167 /* IPW I/O ports */
0168 #define IOIER 0x00      /* Interrupt Enable Register */
0169 #define IOIR  0x02      /* Interrupt Source/ACK register */
0170 #define IODCR 0x04      /* Data Control Register */
0171 #define IODRR 0x06      /* Data Read Register */
0172 #define IODWR 0x08      /* Data Write Register */
0173 #define IOESR 0x0A      /* Embedded Driver Status Register */
0174 #define IORXR 0x0C      /* Rx Fifo Register (Host to Embedded) */
0175 #define IOTXR 0x0E      /* Tx Fifo Register (Embedded to Host) */
0176 
0177 /* I/O ports and bit definitions for version 1 of the hardware */
0178 
0179 /* IER bits*/
0180 #define IER_RXENABLED   0x1
0181 #define IER_TXENABLED   0x2
0182 
0183 /* ISR bits */
0184 #define IR_RXINTR       0x1
0185 #define IR_TXINTR       0x2
0186 
0187 /* DCR bits */
0188 #define DCR_RXDONE      0x1
0189 #define DCR_TXDONE      0x2
0190 #define DCR_RXRESET     0x4
0191 #define DCR_TXRESET     0x8
0192 
0193 /* I/O ports and bit definitions for version 2 of the hardware */
0194 
0195 struct MEMCCR {
0196     unsigned short reg_config_option;   /* PCCOR: Configuration Option Register */
0197     unsigned short reg_config_and_status;   /* PCCSR: Configuration and Status Register */
0198     unsigned short reg_pin_replacement; /* PCPRR: Pin Replacemant Register */
0199     unsigned short reg_socket_and_copy; /* PCSCR: Socket and Copy Register */
0200     unsigned short reg_ext_status;      /* PCESR: Extendend Status Register */
0201     unsigned short reg_io_base;     /* PCIOB: I/O Base Register */
0202 };
0203 
0204 struct MEMINFREG {
0205     unsigned short memreg_tx_old;   /* TX Register (R/W) */
0206     unsigned short pad1;
0207     unsigned short memreg_rx_done;  /* RXDone Register (R/W) */
0208     unsigned short pad2;
0209     unsigned short memreg_rx;   /* RX Register (R/W) */
0210     unsigned short pad3;
0211     unsigned short memreg_pc_interrupt_ack; /* PC intr Ack Register (W) */
0212     unsigned short pad4;
0213     unsigned long memreg_card_present;/* Mask for Host to check (R) for
0214                        * CARD_PRESENT_VALUE */
0215     unsigned short memreg_tx_new;   /* TX2 (new) Register (R/W) */
0216 };
0217 
0218 #define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
0219 
0220 #define MEMTX_TX                       0x0001
0221 #define MEMRX_RX                       0x0001
0222 #define MEMRX_RX_DONE                  0x0001
0223 #define MEMRX_PCINTACKK                0x0001
0224 
0225 #define NL_NUM_OF_PRIORITIES       3
0226 #define NL_NUM_OF_PROTOCOLS        3
0227 #define NL_NUM_OF_ADDRESSES        NO_OF_IPW_CHANNELS
0228 
0229 struct ipw_hardware {
0230     unsigned int base_port;
0231     short hw_version;
0232     unsigned short ll_mtu;
0233     spinlock_t lock;
0234 
0235     int initializing;
0236     int init_loops;
0237     struct timer_list setup_timer;
0238 
0239     /* Flag if hw is ready to send next packet */
0240     int tx_ready;
0241     /* Count of pending packets to be sent */
0242     int tx_queued;
0243     struct list_head tx_queue[NL_NUM_OF_PRIORITIES];
0244 
0245     int rx_bytes_queued;
0246     struct list_head rx_queue;
0247     /* Pool of rx_packet structures that are not currently used. */
0248     struct list_head rx_pool;
0249     int rx_pool_size;
0250     /* True if reception of data is blocked while userspace processes it. */
0251     int blocking_rx;
0252     /* True if there is RX data ready on the hardware. */
0253     int rx_ready;
0254     unsigned short last_memtx_serial;
0255     /*
0256      * Newer versions of the V2 card firmware send serial numbers in the
0257      * MemTX register. 'serial_number_detected' is set true when we detect
0258      * a non-zero serial number (indicating the new firmware).  Thereafter,
0259      * the driver can safely ignore the Timer Recovery re-sends to avoid
0260      * out-of-sync problems.
0261      */
0262     int serial_number_detected;
0263     struct work_struct work_rx;
0264 
0265     /* True if we are to send the set-up data to the hardware. */
0266     int to_setup;
0267 
0268     /* Card has been removed */
0269     int removed;
0270     /* Saved irq value when we disable the interrupt. */
0271     int irq;
0272     /* True if this driver is shutting down. */
0273     int shutting_down;
0274     /* Modem control lines */
0275     unsigned int control_lines[NL_NUM_OF_ADDRESSES];
0276     struct ipw_rx_packet *packet_assembler[NL_NUM_OF_ADDRESSES];
0277 
0278     struct tasklet_struct tasklet;
0279 
0280     /* The handle for the network layer, for the sending of events to it. */
0281     struct ipw_network *network;
0282     struct MEMINFREG __iomem *memory_info_regs;
0283     struct MEMCCR __iomem *memregs_CCR;
0284     void (*reboot_callback) (void *data);
0285     void *reboot_callback_data;
0286 
0287     unsigned short __iomem *memreg_tx;
0288 };
0289 
0290 /*
0291  * Packet info structure for tx packets.
0292  * Note: not all the fields defined here are required for all protocols
0293  */
0294 struct ipw_tx_packet {
0295     struct list_head queue;
0296     /* channel idx + 1 */
0297     unsigned char dest_addr;
0298     /* SETUP, CTRL or DATA */
0299     unsigned char protocol;
0300     /* Length of data block, which starts at the end of this structure */
0301     unsigned short length;
0302     /* Sending state */
0303     /* Offset of where we've sent up to so far */
0304     unsigned long offset;
0305     /* Count of packet fragments, starting at 0 */
0306     int fragment_count;
0307 
0308     /* Called after packet is sent and before is freed */
0309     void (*packet_callback) (void *cb_data, unsigned int packet_length);
0310     void *callback_data;
0311 };
0312 
0313 /* Signals from DTE */
0314 #define COMCTRL_RTS 0
0315 #define COMCTRL_DTR 1
0316 
0317 /* Signals from DCE */
0318 #define COMCTRL_CTS 2
0319 #define COMCTRL_DCD 3
0320 #define COMCTRL_DSR 4
0321 #define COMCTRL_RI  5
0322 
0323 struct ipw_control_packet_body {
0324     /* DTE signal or DCE signal */
0325     unsigned char sig_no;
0326     /* 0: set signal, 1: clear signal */
0327     unsigned char value;
0328 } __attribute__ ((__packed__));
0329 
0330 struct ipw_control_packet {
0331     struct ipw_tx_packet header;
0332     struct ipw_control_packet_body body;
0333 };
0334 
0335 struct ipw_rx_packet {
0336     struct list_head queue;
0337     unsigned int capacity;
0338     unsigned int length;
0339     unsigned int protocol;
0340     unsigned int channel_idx;
0341 };
0342 
0343 static char *data_type(const unsigned char *buf, unsigned length)
0344 {
0345     struct nl_packet_header *hdr = (struct nl_packet_header *) buf;
0346 
0347     if (length == 0)
0348         return "     ";
0349 
0350     if (hdr->packet_rank & NL_FIRST_PACKET) {
0351         switch (hdr->protocol) {
0352         case TL_PROTOCOLID_COM_DATA:    return "DATA ";
0353         case TL_PROTOCOLID_COM_CTRL:    return "CTRL ";
0354         case TL_PROTOCOLID_SETUP:   return "SETUP";
0355         default: return "???? ";
0356         }
0357     } else
0358         return "     ";
0359 }
0360 
0361 #define DUMP_MAX_BYTES 64
0362 
0363 static void dump_data_bytes(const char *type, const unsigned char *data,
0364                 unsigned length)
0365 {
0366     char prefix[56];
0367 
0368     sprintf(prefix, IPWIRELESS_PCCARD_NAME ": %s %s ",
0369             type, data_type(data, length));
0370     print_hex_dump_bytes(prefix, 0, (void *)data,
0371             length < DUMP_MAX_BYTES ? length : DUMP_MAX_BYTES);
0372 }
0373 
0374 static void swap_packet_bitfield_to_le(unsigned char *data)
0375 {
0376 #ifdef __BIG_ENDIAN_BITFIELD
0377     unsigned char tmp = *data, ret = 0;
0378 
0379     /*
0380      * transform bits from aa.bbb.ccc to ccc.bbb.aa
0381      */
0382     ret |= (tmp & 0xc0) >> 6;
0383     ret |= (tmp & 0x38) >> 1;
0384     ret |= (tmp & 0x07) << 5;
0385     *data = ret & 0xff;
0386 #endif
0387 }
0388 
0389 static void swap_packet_bitfield_from_le(unsigned char *data)
0390 {
0391 #ifdef __BIG_ENDIAN_BITFIELD
0392     unsigned char tmp = *data, ret = 0;
0393 
0394     /*
0395      * transform bits from ccc.bbb.aa to aa.bbb.ccc
0396      */
0397     ret |= (tmp & 0xe0) >> 5;
0398     ret |= (tmp & 0x1c) << 1;
0399     ret |= (tmp & 0x03) << 6;
0400     *data = ret & 0xff;
0401 #endif
0402 }
0403 
0404 static void do_send_fragment(struct ipw_hardware *hw, unsigned char *data,
0405                 unsigned length)
0406 {
0407     unsigned i;
0408     unsigned long flags;
0409 
0410     start_timing();
0411     BUG_ON(length > hw->ll_mtu);
0412 
0413     if (ipwireless_debug)
0414         dump_data_bytes("send", data, length);
0415 
0416     spin_lock_irqsave(&hw->lock, flags);
0417 
0418     hw->tx_ready = 0;
0419     swap_packet_bitfield_to_le(data);
0420 
0421     if (hw->hw_version == HW_VERSION_1) {
0422         outw((unsigned short) length, hw->base_port + IODWR);
0423 
0424         for (i = 0; i < length; i += 2) {
0425             unsigned short d = data[i];
0426             __le16 raw_data;
0427 
0428             if (i + 1 < length)
0429                 d |= data[i + 1] << 8;
0430             raw_data = cpu_to_le16(d);
0431             outw(raw_data, hw->base_port + IODWR);
0432         }
0433 
0434         outw(DCR_TXDONE, hw->base_port + IODCR);
0435     } else if (hw->hw_version == HW_VERSION_2) {
0436         outw((unsigned short) length, hw->base_port);
0437 
0438         for (i = 0; i < length; i += 2) {
0439             unsigned short d = data[i];
0440             __le16 raw_data;
0441 
0442             if (i + 1 < length)
0443                 d |= data[i + 1] << 8;
0444             raw_data = cpu_to_le16(d);
0445             outw(raw_data, hw->base_port);
0446         }
0447         while ((i & 3) != 2) {
0448             outw((unsigned short) 0xDEAD, hw->base_port);
0449             i += 2;
0450         }
0451         writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
0452     }
0453 
0454     spin_unlock_irqrestore(&hw->lock, flags);
0455 
0456     end_write_timing(length);
0457 }
0458 
0459 static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
0460 {
0461     unsigned short fragment_data_len;
0462     unsigned short data_left = packet->length - packet->offset;
0463     unsigned short header_size;
0464     union nl_packet pkt;
0465 
0466     header_size =
0467         (packet->fragment_count == 0)
0468         ? NL_FIRST_PACKET_HEADER_SIZE
0469         : NL_FOLLOWING_PACKET_HEADER_SIZE;
0470     fragment_data_len = hw->ll_mtu - header_size;
0471     if (data_left < fragment_data_len)
0472         fragment_data_len = data_left;
0473 
0474     /*
0475      * hdr_first is now in machine bitfield order, which will be swapped
0476      * to le just before it goes to hw
0477      */
0478     pkt.hdr_first.protocol = packet->protocol;
0479     pkt.hdr_first.address = packet->dest_addr;
0480     pkt.hdr_first.packet_rank = 0;
0481 
0482     /* First packet? */
0483     if (packet->fragment_count == 0) {
0484         pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
0485         pkt.hdr_first.length_lsb = (unsigned char) packet->length;
0486         pkt.hdr_first.length_msb =
0487             (unsigned char) (packet->length >> 8);
0488     }
0489 
0490     memcpy(pkt.rawpkt + header_size,
0491            ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
0492            packet->offset, fragment_data_len);
0493     packet->offset += fragment_data_len;
0494     packet->fragment_count++;
0495 
0496     /* Last packet? (May also be first packet.) */
0497     if (packet->offset == packet->length)
0498         pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
0499     do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
0500 
0501     /* If this packet has unsent data, then re-queue it. */
0502     if (packet->offset < packet->length) {
0503         /*
0504          * Re-queue it at the head of the highest priority queue so
0505          * it goes before all other packets
0506          */
0507         unsigned long flags;
0508 
0509         spin_lock_irqsave(&hw->lock, flags);
0510         list_add(&packet->queue, &hw->tx_queue[0]);
0511         hw->tx_queued++;
0512         spin_unlock_irqrestore(&hw->lock, flags);
0513     } else {
0514         if (packet->packet_callback)
0515             packet->packet_callback(packet->callback_data,
0516                     packet->length);
0517         kfree(packet);
0518     }
0519 }
0520 
0521 static void ipw_setup_hardware(struct ipw_hardware *hw)
0522 {
0523     unsigned long flags;
0524 
0525     spin_lock_irqsave(&hw->lock, flags);
0526     if (hw->hw_version == HW_VERSION_1) {
0527         /* Reset RX FIFO */
0528         outw(DCR_RXRESET, hw->base_port + IODCR);
0529         /* SB: Reset TX FIFO */
0530         outw(DCR_TXRESET, hw->base_port + IODCR);
0531 
0532         /* Enable TX and RX interrupts. */
0533         outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
0534     } else {
0535         /*
0536          * Set INTRACK bit (bit 0), which means we must explicitly
0537          * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
0538          */
0539         unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
0540 
0541         csr |= 1;
0542         writew(csr, &hw->memregs_CCR->reg_config_and_status);
0543     }
0544     spin_unlock_irqrestore(&hw->lock, flags);
0545 }
0546 
0547 /*
0548  * If 'packet' is NULL, then this function allocates a new packet, setting its
0549  * length to 0 and ensuring it has the specified minimum amount of free space.
0550  *
0551  * If 'packet' is not NULL, then this function enlarges it if it doesn't
0552  * have the specified minimum amount of free space.
0553  *
0554  */
0555 static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
0556                        struct ipw_rx_packet *packet,
0557                        int minimum_free_space)
0558 {
0559 
0560     if (!packet) {
0561         unsigned long flags;
0562 
0563         spin_lock_irqsave(&hw->lock, flags);
0564         if (!list_empty(&hw->rx_pool)) {
0565             packet = list_first_entry(&hw->rx_pool,
0566                     struct ipw_rx_packet, queue);
0567             hw->rx_pool_size--;
0568             spin_unlock_irqrestore(&hw->lock, flags);
0569             list_del(&packet->queue);
0570         } else {
0571             const int min_capacity =
0572                 ipwireless_ppp_mru(hw->network) + 2;
0573             int new_capacity;
0574 
0575             spin_unlock_irqrestore(&hw->lock, flags);
0576             new_capacity =
0577                 (minimum_free_space > min_capacity
0578                  ? minimum_free_space
0579                  : min_capacity);
0580             packet = kmalloc(sizeof(struct ipw_rx_packet)
0581                     + new_capacity, GFP_ATOMIC);
0582             if (!packet)
0583                 return NULL;
0584             packet->capacity = new_capacity;
0585         }
0586         packet->length = 0;
0587     }
0588 
0589     if (packet->length + minimum_free_space > packet->capacity) {
0590         struct ipw_rx_packet *old_packet = packet;
0591 
0592         packet = kmalloc(sizeof(struct ipw_rx_packet) +
0593                 old_packet->length + minimum_free_space,
0594                 GFP_ATOMIC);
0595         if (!packet) {
0596             kfree(old_packet);
0597             return NULL;
0598         }
0599         memcpy(packet, old_packet,
0600                 sizeof(struct ipw_rx_packet)
0601                     + old_packet->length);
0602         packet->capacity = old_packet->length + minimum_free_space;
0603         kfree(old_packet);
0604     }
0605 
0606     return packet;
0607 }
0608 
0609 static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
0610 {
0611     if (hw->rx_pool_size > 6)
0612         kfree(packet);
0613     else {
0614         hw->rx_pool_size++;
0615         list_add(&packet->queue, &hw->rx_pool);
0616     }
0617 }
0618 
0619 static void queue_received_packet(struct ipw_hardware *hw,
0620                   unsigned int protocol,
0621                   unsigned int address,
0622                   const unsigned char *data, int length,
0623                   int is_last)
0624 {
0625     unsigned int channel_idx = address - 1;
0626     struct ipw_rx_packet *packet = NULL;
0627     unsigned long flags;
0628 
0629     /* Discard packet if channel index is out of range. */
0630     if (channel_idx >= NL_NUM_OF_ADDRESSES) {
0631         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
0632                ": data packet has bad address %u\n", address);
0633         return;
0634     }
0635 
0636     /*
0637      * ->packet_assembler is safe to touch unlocked, this is the only place
0638      */
0639     if (protocol == TL_PROTOCOLID_COM_DATA) {
0640         struct ipw_rx_packet **assem =
0641             &hw->packet_assembler[channel_idx];
0642 
0643         /*
0644          * Create a new packet, or assembler already contains one
0645          * enlarge it by 'length' bytes.
0646          */
0647         (*assem) = pool_allocate(hw, *assem, length);
0648         if (!(*assem)) {
0649             printk(KERN_ERR IPWIRELESS_PCCARD_NAME
0650                 ": no memory for incoming data packet, dropped!\n");
0651             return;
0652         }
0653         (*assem)->protocol = protocol;
0654         (*assem)->channel_idx = channel_idx;
0655 
0656         /* Append this packet data onto existing data. */
0657         memcpy((unsigned char *)(*assem) +
0658                    sizeof(struct ipw_rx_packet)
0659                 + (*assem)->length, data, length);
0660         (*assem)->length += length;
0661         if (is_last) {
0662             packet = *assem;
0663             *assem = NULL;
0664             /* Count queued DATA bytes only */
0665             spin_lock_irqsave(&hw->lock, flags);
0666             hw->rx_bytes_queued += packet->length;
0667             spin_unlock_irqrestore(&hw->lock, flags);
0668         }
0669     } else {
0670         /* If it's a CTRL packet, don't assemble, just queue it. */
0671         packet = pool_allocate(hw, NULL, length);
0672         if (!packet) {
0673             printk(KERN_ERR IPWIRELESS_PCCARD_NAME
0674                 ": no memory for incoming ctrl packet, dropped!\n");
0675             return;
0676         }
0677         packet->protocol = protocol;
0678         packet->channel_idx = channel_idx;
0679         memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
0680                 data, length);
0681         packet->length = length;
0682     }
0683 
0684     /*
0685      * If this is the last packet, then send the assembled packet on to the
0686      * network layer.
0687      */
0688     if (packet) {
0689         spin_lock_irqsave(&hw->lock, flags);
0690         list_add_tail(&packet->queue, &hw->rx_queue);
0691         /* Block reception of incoming packets if queue is full. */
0692         hw->blocking_rx =
0693             (hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
0694 
0695         spin_unlock_irqrestore(&hw->lock, flags);
0696         schedule_work(&hw->work_rx);
0697     }
0698 }
0699 
0700 /*
0701  * Workqueue callback
0702  */
0703 static void ipw_receive_data_work(struct work_struct *work_rx)
0704 {
0705     struct ipw_hardware *hw =
0706         container_of(work_rx, struct ipw_hardware, work_rx);
0707     unsigned long flags;
0708 
0709     spin_lock_irqsave(&hw->lock, flags);
0710     while (!list_empty(&hw->rx_queue)) {
0711         struct ipw_rx_packet *packet =
0712             list_first_entry(&hw->rx_queue,
0713                     struct ipw_rx_packet, queue);
0714 
0715         if (hw->shutting_down)
0716             break;
0717         list_del(&packet->queue);
0718 
0719         /*
0720          * Note: ipwireless_network_packet_received must be called in a
0721          * process context (i.e. via schedule_work) because the tty
0722          * output code can sleep in the tty_flip_buffer_push call.
0723          */
0724         if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
0725             if (hw->network != NULL) {
0726                 /* If the network hasn't been disconnected. */
0727                 spin_unlock_irqrestore(&hw->lock, flags);
0728                 /*
0729                  * This must run unlocked due to tty processing
0730                  * and mutex locking
0731                  */
0732                 ipwireless_network_packet_received(
0733                         hw->network,
0734                         packet->channel_idx,
0735                         (unsigned char *)packet
0736                         + sizeof(struct ipw_rx_packet),
0737                         packet->length);
0738                 spin_lock_irqsave(&hw->lock, flags);
0739             }
0740             /* Count queued DATA bytes only */
0741             hw->rx_bytes_queued -= packet->length;
0742         } else {
0743             /*
0744              * This is safe to be called locked, callchain does
0745              * not block
0746              */
0747             handle_received_CTRL_packet(hw, packet->channel_idx,
0748                     (unsigned char *)packet
0749                     + sizeof(struct ipw_rx_packet),
0750                     packet->length);
0751         }
0752         pool_free(hw, packet);
0753         /*
0754          * Unblock reception of incoming packets if queue is no longer
0755          * full.
0756          */
0757         hw->blocking_rx =
0758             hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
0759         if (hw->shutting_down)
0760             break;
0761     }
0762     spin_unlock_irqrestore(&hw->lock, flags);
0763 }
0764 
0765 static void handle_received_CTRL_packet(struct ipw_hardware *hw,
0766                     unsigned int channel_idx,
0767                     const unsigned char *data, int len)
0768 {
0769     const struct ipw_control_packet_body *body =
0770         (const struct ipw_control_packet_body *) data;
0771     unsigned int changed_mask;
0772 
0773     if (len != sizeof(struct ipw_control_packet_body)) {
0774         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
0775                ": control packet was %d bytes - wrong size!\n",
0776                len);
0777         return;
0778     }
0779 
0780     switch (body->sig_no) {
0781     case COMCTRL_CTS:
0782         changed_mask = IPW_CONTROL_LINE_CTS;
0783         break;
0784     case COMCTRL_DCD:
0785         changed_mask = IPW_CONTROL_LINE_DCD;
0786         break;
0787     case COMCTRL_DSR:
0788         changed_mask = IPW_CONTROL_LINE_DSR;
0789         break;
0790     case COMCTRL_RI:
0791         changed_mask = IPW_CONTROL_LINE_RI;
0792         break;
0793     default:
0794         changed_mask = 0;
0795     }
0796 
0797     if (changed_mask != 0) {
0798         if (body->value)
0799             hw->control_lines[channel_idx] |= changed_mask;
0800         else
0801             hw->control_lines[channel_idx] &= ~changed_mask;
0802         if (hw->network)
0803             ipwireless_network_notify_control_line_change(
0804                     hw->network,
0805                     channel_idx,
0806                     hw->control_lines[channel_idx],
0807                     changed_mask);
0808     }
0809 }
0810 
0811 static void handle_received_packet(struct ipw_hardware *hw,
0812                    const union nl_packet *packet,
0813                    unsigned short len)
0814 {
0815     unsigned int protocol = packet->hdr.protocol;
0816     unsigned int address = packet->hdr.address;
0817     unsigned int header_length;
0818     const unsigned char *data;
0819     unsigned int data_len;
0820     int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
0821 
0822     if (packet->hdr.packet_rank & NL_FIRST_PACKET)
0823         header_length = NL_FIRST_PACKET_HEADER_SIZE;
0824     else
0825         header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
0826 
0827     data = packet->rawpkt + header_length;
0828     data_len = len - header_length;
0829     switch (protocol) {
0830     case TL_PROTOCOLID_COM_DATA:
0831     case TL_PROTOCOLID_COM_CTRL:
0832         queue_received_packet(hw, protocol, address, data, data_len,
0833                 is_last);
0834         break;
0835     case TL_PROTOCOLID_SETUP:
0836         handle_received_SETUP_packet(hw, address, data, data_len,
0837                 is_last);
0838         break;
0839     }
0840 }
0841 
0842 static void acknowledge_data_read(struct ipw_hardware *hw)
0843 {
0844     if (hw->hw_version == HW_VERSION_1)
0845         outw(DCR_RXDONE, hw->base_port + IODCR);
0846     else
0847         writew(MEMRX_PCINTACKK,
0848                 &hw->memory_info_regs->memreg_pc_interrupt_ack);
0849 }
0850 
0851 /*
0852  * Retrieve a packet from the IPW hardware.
0853  */
0854 static void do_receive_packet(struct ipw_hardware *hw)
0855 {
0856     unsigned len;
0857     unsigned i;
0858     unsigned char pkt[LL_MTU_MAX];
0859 
0860     start_timing();
0861 
0862     if (hw->hw_version == HW_VERSION_1) {
0863         len = inw(hw->base_port + IODRR);
0864         if (len > hw->ll_mtu) {
0865             printk(KERN_INFO IPWIRELESS_PCCARD_NAME
0866                    ": received a packet of %u bytes - longer than the MTU!\n", len);
0867             outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
0868             return;
0869         }
0870 
0871         for (i = 0; i < len; i += 2) {
0872             __le16 raw_data = inw(hw->base_port + IODRR);
0873             unsigned short data = le16_to_cpu(raw_data);
0874 
0875             pkt[i] = (unsigned char) data;
0876             pkt[i + 1] = (unsigned char) (data >> 8);
0877         }
0878     } else {
0879         len = inw(hw->base_port);
0880         if (len > hw->ll_mtu) {
0881             printk(KERN_INFO IPWIRELESS_PCCARD_NAME
0882                    ": received a packet of %u bytes - longer than the MTU!\n", len);
0883             writew(MEMRX_PCINTACKK,
0884                 &hw->memory_info_regs->memreg_pc_interrupt_ack);
0885             return;
0886         }
0887 
0888         for (i = 0; i < len; i += 2) {
0889             __le16 raw_data = inw(hw->base_port);
0890             unsigned short data = le16_to_cpu(raw_data);
0891 
0892             pkt[i] = (unsigned char) data;
0893             pkt[i + 1] = (unsigned char) (data >> 8);
0894         }
0895 
0896         while ((i & 3) != 2) {
0897             inw(hw->base_port);
0898             i += 2;
0899         }
0900     }
0901 
0902     acknowledge_data_read(hw);
0903 
0904     swap_packet_bitfield_from_le(pkt);
0905 
0906     if (ipwireless_debug)
0907         dump_data_bytes("recv", pkt, len);
0908 
0909     handle_received_packet(hw, (union nl_packet *) pkt, len);
0910 
0911     end_read_timing(len);
0912 }
0913 
0914 static int get_current_packet_priority(struct ipw_hardware *hw)
0915 {
0916     /*
0917      * If we're initializing, don't send anything of higher priority than
0918      * PRIO_SETUP.  The network layer therefore need not care about
0919      * hardware initialization - any of its stuff will simply be queued
0920      * until setup is complete.
0921      */
0922     return (hw->to_setup || hw->initializing
0923             ? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
0924 }
0925 
0926 /*
0927  * return 1 if something has been received from hw
0928  */
0929 static int get_packets_from_hw(struct ipw_hardware *hw)
0930 {
0931     int received = 0;
0932     unsigned long flags;
0933 
0934     spin_lock_irqsave(&hw->lock, flags);
0935     while (hw->rx_ready && !hw->blocking_rx) {
0936         received = 1;
0937         hw->rx_ready--;
0938         spin_unlock_irqrestore(&hw->lock, flags);
0939 
0940         do_receive_packet(hw);
0941 
0942         spin_lock_irqsave(&hw->lock, flags);
0943     }
0944     spin_unlock_irqrestore(&hw->lock, flags);
0945 
0946     return received;
0947 }
0948 
0949 /*
0950  * Send pending packet up to given priority, prioritize SETUP data until
0951  * hardware is fully setup.
0952  *
0953  * return 1 if more packets can be sent
0954  */
0955 static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
0956 {
0957     int more_to_send = 0;
0958     unsigned long flags;
0959 
0960     spin_lock_irqsave(&hw->lock, flags);
0961     if (hw->tx_queued && hw->tx_ready) {
0962         int priority;
0963         struct ipw_tx_packet *packet = NULL;
0964 
0965         /* Pick a packet */
0966         for (priority = 0; priority < priority_limit; priority++) {
0967             if (!list_empty(&hw->tx_queue[priority])) {
0968                 packet = list_first_entry(
0969                         &hw->tx_queue[priority],
0970                         struct ipw_tx_packet,
0971                         queue);
0972 
0973                 hw->tx_queued--;
0974                 list_del(&packet->queue);
0975 
0976                 break;
0977             }
0978         }
0979         if (!packet) {
0980             hw->tx_queued = 0;
0981             spin_unlock_irqrestore(&hw->lock, flags);
0982             return 0;
0983         }
0984 
0985         spin_unlock_irqrestore(&hw->lock, flags);
0986 
0987         /* Send */
0988         do_send_packet(hw, packet);
0989 
0990         /* Check if more to send */
0991         spin_lock_irqsave(&hw->lock, flags);
0992         for (priority = 0; priority < priority_limit; priority++)
0993             if (!list_empty(&hw->tx_queue[priority])) {
0994                 more_to_send = 1;
0995                 break;
0996             }
0997 
0998         if (!more_to_send)
0999             hw->tx_queued = 0;
1000     }
1001     spin_unlock_irqrestore(&hw->lock, flags);
1002 
1003     return more_to_send;
1004 }
1005 
1006 /*
1007  * Send and receive all queued packets.
1008  */
1009 static void ipwireless_do_tasklet(struct tasklet_struct *t)
1010 {
1011     struct ipw_hardware *hw = from_tasklet(hw, t, tasklet);
1012     unsigned long flags;
1013 
1014     spin_lock_irqsave(&hw->lock, flags);
1015     if (hw->shutting_down) {
1016         spin_unlock_irqrestore(&hw->lock, flags);
1017         return;
1018     }
1019 
1020     if (hw->to_setup == 1) {
1021         /*
1022          * Initial setup data sent to hardware
1023          */
1024         hw->to_setup = 2;
1025         spin_unlock_irqrestore(&hw->lock, flags);
1026 
1027         ipw_setup_hardware(hw);
1028         ipw_send_setup_packet(hw);
1029 
1030         send_pending_packet(hw, PRIO_SETUP + 1);
1031         get_packets_from_hw(hw);
1032     } else {
1033         int priority_limit = get_current_packet_priority(hw);
1034         int again;
1035 
1036         spin_unlock_irqrestore(&hw->lock, flags);
1037 
1038         do {
1039             again = send_pending_packet(hw, priority_limit);
1040             again |= get_packets_from_hw(hw);
1041         } while (again);
1042     }
1043 }
1044 
1045 /*
1046  * return true if the card is physically present.
1047  */
1048 static int is_card_present(struct ipw_hardware *hw)
1049 {
1050     if (hw->hw_version == HW_VERSION_1)
1051         return inw(hw->base_port + IOIR) != 0xFFFF;
1052     else
1053         return readl(&hw->memory_info_regs->memreg_card_present) ==
1054             CARD_PRESENT_VALUE;
1055 }
1056 
1057 static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
1058                           struct ipw_hardware *hw)
1059 {
1060     unsigned short irqn;
1061 
1062     irqn = inw(hw->base_port + IOIR);
1063 
1064     /* Check if card is present */
1065     if (irqn == 0xFFFF)
1066         return IRQ_NONE;
1067     else if (irqn != 0) {
1068         unsigned short ack = 0;
1069         unsigned long flags;
1070 
1071         /* Transmit complete. */
1072         if (irqn & IR_TXINTR) {
1073             ack |= IR_TXINTR;
1074             spin_lock_irqsave(&hw->lock, flags);
1075             hw->tx_ready = 1;
1076             spin_unlock_irqrestore(&hw->lock, flags);
1077         }
1078         /* Received data */
1079         if (irqn & IR_RXINTR) {
1080             ack |= IR_RXINTR;
1081             spin_lock_irqsave(&hw->lock, flags);
1082             hw->rx_ready++;
1083             spin_unlock_irqrestore(&hw->lock, flags);
1084         }
1085         if (ack != 0) {
1086             outw(ack, hw->base_port + IOIR);
1087             tasklet_schedule(&hw->tasklet);
1088         }
1089         return IRQ_HANDLED;
1090     }
1091     return IRQ_NONE;
1092 }
1093 
1094 static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
1095 {
1096     unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
1097 
1098     csr &= 0xfffd;
1099     writew(csr, &hw->memregs_CCR->reg_config_and_status);
1100 }
1101 
1102 static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
1103                              struct ipw_hardware *hw)
1104 {
1105     int tx = 0;
1106     int rx = 0;
1107     int rx_repeat = 0;
1108     int try_mem_tx_old;
1109     unsigned long flags;
1110 
1111     do {
1112 
1113     unsigned short memtx = readw(hw->memreg_tx);
1114     unsigned short memtx_serial;
1115     unsigned short memrxdone =
1116         readw(&hw->memory_info_regs->memreg_rx_done);
1117 
1118     try_mem_tx_old = 0;
1119 
1120     /* check whether the interrupt was generated by ipwireless card */
1121     if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
1122 
1123         /* check if the card uses memreg_tx_old register */
1124         if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1125             memtx = readw(&hw->memory_info_regs->memreg_tx_old);
1126             if (memtx & MEMTX_TX) {
1127                 printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1128                     ": Using memreg_tx_old\n");
1129                 hw->memreg_tx =
1130                     &hw->memory_info_regs->memreg_tx_old;
1131             } else {
1132                 return IRQ_NONE;
1133             }
1134         } else
1135             return IRQ_NONE;
1136     }
1137 
1138     /*
1139      * See if the card is physically present. Note that while it is
1140      * powering up, it appears not to be present.
1141      */
1142     if (!is_card_present(hw)) {
1143         acknowledge_pcmcia_interrupt(hw);
1144         return IRQ_HANDLED;
1145     }
1146 
1147     memtx_serial = memtx & (unsigned short) 0xff00;
1148     if (memtx & MEMTX_TX) {
1149         writew(memtx_serial, hw->memreg_tx);
1150 
1151         if (hw->serial_number_detected) {
1152             if (memtx_serial != hw->last_memtx_serial) {
1153                 hw->last_memtx_serial = memtx_serial;
1154                 spin_lock_irqsave(&hw->lock, flags);
1155                 hw->rx_ready++;
1156                 spin_unlock_irqrestore(&hw->lock, flags);
1157                 rx = 1;
1158             } else
1159                 /* Ignore 'Timer Recovery' duplicates. */
1160                 rx_repeat = 1;
1161         } else {
1162             /*
1163              * If a non-zero serial number is seen, then enable
1164              * serial number checking.
1165              */
1166             if (memtx_serial != 0) {
1167                 hw->serial_number_detected = 1;
1168                 printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1169                     ": memreg_tx serial num detected\n");
1170 
1171                 spin_lock_irqsave(&hw->lock, flags);
1172                 hw->rx_ready++;
1173                 spin_unlock_irqrestore(&hw->lock, flags);
1174             }
1175             rx = 1;
1176         }
1177     }
1178     if (memrxdone & MEMRX_RX_DONE) {
1179         writew(0, &hw->memory_info_regs->memreg_rx_done);
1180         spin_lock_irqsave(&hw->lock, flags);
1181         hw->tx_ready = 1;
1182         spin_unlock_irqrestore(&hw->lock, flags);
1183         tx = 1;
1184     }
1185     if (tx)
1186         writew(MEMRX_PCINTACKK,
1187                 &hw->memory_info_regs->memreg_pc_interrupt_ack);
1188 
1189     acknowledge_pcmcia_interrupt(hw);
1190 
1191     if (tx || rx)
1192         tasklet_schedule(&hw->tasklet);
1193     else if (!rx_repeat) {
1194         if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1195             if (hw->serial_number_detected)
1196                 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1197                     ": spurious interrupt - new_tx mode\n");
1198             else {
1199                 printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1200                     ": no valid memreg_tx value - switching to the old memreg_tx\n");
1201                 hw->memreg_tx =
1202                     &hw->memory_info_regs->memreg_tx_old;
1203                 try_mem_tx_old = 1;
1204             }
1205         } else
1206             printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
1207                     ": spurious interrupt - old_tx mode\n");
1208     }
1209 
1210     } while (try_mem_tx_old == 1);
1211 
1212     return IRQ_HANDLED;
1213 }
1214 
1215 irqreturn_t ipwireless_interrupt(int irq, void *dev_id)
1216 {
1217     struct ipw_dev *ipw = dev_id;
1218 
1219     if (ipw->hardware->hw_version == HW_VERSION_1)
1220         return ipwireless_handle_v1_interrupt(irq, ipw->hardware);
1221     else
1222         return ipwireless_handle_v2_v3_interrupt(irq, ipw->hardware);
1223 }
1224 
1225 static void flush_packets_to_hw(struct ipw_hardware *hw)
1226 {
1227     int priority_limit;
1228     unsigned long flags;
1229 
1230     spin_lock_irqsave(&hw->lock, flags);
1231     priority_limit = get_current_packet_priority(hw);
1232     spin_unlock_irqrestore(&hw->lock, flags);
1233 
1234     while (send_pending_packet(hw, priority_limit));
1235 }
1236 
1237 static void send_packet(struct ipw_hardware *hw, int priority,
1238             struct ipw_tx_packet *packet)
1239 {
1240     unsigned long flags;
1241 
1242     spin_lock_irqsave(&hw->lock, flags);
1243     list_add_tail(&packet->queue, &hw->tx_queue[priority]);
1244     hw->tx_queued++;
1245     spin_unlock_irqrestore(&hw->lock, flags);
1246 
1247     flush_packets_to_hw(hw);
1248 }
1249 
1250 /* Create data packet, non-atomic allocation */
1251 static void *alloc_data_packet(int data_size,
1252                 unsigned char dest_addr,
1253                 unsigned char protocol)
1254 {
1255     struct ipw_tx_packet *packet = kzalloc(
1256             sizeof(struct ipw_tx_packet) + data_size,
1257             GFP_ATOMIC);
1258 
1259     if (!packet)
1260         return NULL;
1261 
1262     INIT_LIST_HEAD(&packet->queue);
1263     packet->dest_addr = dest_addr;
1264     packet->protocol = protocol;
1265     packet->length = data_size;
1266 
1267     return packet;
1268 }
1269 
1270 static void *alloc_ctrl_packet(int header_size,
1271                    unsigned char dest_addr,
1272                    unsigned char protocol,
1273                    unsigned char sig_no)
1274 {
1275     /*
1276      * sig_no is located right after ipw_tx_packet struct in every
1277      * CTRL or SETUP packets, we can use ipw_control_packet as a
1278      * common struct
1279      */
1280     struct ipw_control_packet *packet = kzalloc(header_size, GFP_ATOMIC);
1281 
1282     if (!packet)
1283         return NULL;
1284 
1285     INIT_LIST_HEAD(&packet->header.queue);
1286     packet->header.dest_addr = dest_addr;
1287     packet->header.protocol = protocol;
1288     packet->header.length = header_size - sizeof(struct ipw_tx_packet);
1289     packet->body.sig_no = sig_no;
1290 
1291     return packet;
1292 }
1293 
1294 int ipwireless_send_packet(struct ipw_hardware *hw, unsigned int channel_idx,
1295                 const unsigned char *data, unsigned int length,
1296                 void (*callback) (void *cb, unsigned int length),
1297                 void *callback_data)
1298 {
1299     struct ipw_tx_packet *packet;
1300 
1301     packet = alloc_data_packet(length, (channel_idx + 1),
1302             TL_PROTOCOLID_COM_DATA);
1303     if (!packet)
1304         return -ENOMEM;
1305     packet->packet_callback = callback;
1306     packet->callback_data = callback_data;
1307     memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
1308             length);
1309 
1310     send_packet(hw, PRIO_DATA, packet);
1311     return 0;
1312 }
1313 
1314 static int set_control_line(struct ipw_hardware *hw, int prio,
1315                unsigned int channel_idx, int line, int state)
1316 {
1317     struct ipw_control_packet *packet;
1318     int protocolid = TL_PROTOCOLID_COM_CTRL;
1319 
1320     if (prio == PRIO_SETUP)
1321         protocolid = TL_PROTOCOLID_SETUP;
1322 
1323     packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
1324             (channel_idx + 1), protocolid, line);
1325     if (!packet)
1326         return -ENOMEM;
1327     packet->header.length = sizeof(struct ipw_control_packet_body);
1328     packet->body.value = (state == 0 ? 0 : 1);
1329     send_packet(hw, prio, &packet->header);
1330     return 0;
1331 }
1332 
1333 
1334 static int set_DTR(struct ipw_hardware *hw, int priority,
1335            unsigned int channel_idx, int state)
1336 {
1337     if (state != 0)
1338         hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
1339     else
1340         hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
1341 
1342     return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
1343 }
1344 
1345 static int set_RTS(struct ipw_hardware *hw, int priority,
1346            unsigned int channel_idx, int state)
1347 {
1348     if (state != 0)
1349         hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
1350     else
1351         hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
1352 
1353     return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
1354 }
1355 
1356 int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
1357                int state)
1358 {
1359     return set_DTR(hw, PRIO_CTRL, channel_idx, state);
1360 }
1361 
1362 int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
1363                int state)
1364 {
1365     return set_RTS(hw, PRIO_CTRL, channel_idx, state);
1366 }
1367 
1368 struct ipw_setup_get_version_query_packet {
1369     struct ipw_tx_packet header;
1370     struct tl_setup_get_version_qry body;
1371 };
1372 
1373 struct ipw_setup_config_packet {
1374     struct ipw_tx_packet header;
1375     struct tl_setup_config_msg body;
1376 };
1377 
1378 struct ipw_setup_config_done_packet {
1379     struct ipw_tx_packet header;
1380     struct tl_setup_config_done_msg body;
1381 };
1382 
1383 struct ipw_setup_open_packet {
1384     struct ipw_tx_packet header;
1385     struct tl_setup_open_msg body;
1386 };
1387 
1388 struct ipw_setup_info_packet {
1389     struct ipw_tx_packet header;
1390     struct tl_setup_info_msg body;
1391 };
1392 
1393 struct ipw_setup_reboot_msg_ack {
1394     struct ipw_tx_packet header;
1395     struct TlSetupRebootMsgAck body;
1396 };
1397 
1398 /* This handles the actual initialization of the card */
1399 static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
1400 {
1401     struct ipw_setup_config_packet *config_packet;
1402     struct ipw_setup_config_done_packet *config_done_packet;
1403     struct ipw_setup_open_packet *open_packet;
1404     struct ipw_setup_info_packet *info_packet;
1405     int port;
1406     unsigned int channel_idx;
1407 
1408     /* generate config packet */
1409     for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1410         config_packet = alloc_ctrl_packet(
1411                 sizeof(struct ipw_setup_config_packet),
1412                 ADDR_SETUP_PROT,
1413                 TL_PROTOCOLID_SETUP,
1414                 TL_SETUP_SIGNO_CONFIG_MSG);
1415         if (!config_packet)
1416             goto exit_nomem;
1417         config_packet->header.length = sizeof(struct tl_setup_config_msg);
1418         config_packet->body.port_no = port;
1419         config_packet->body.prio_data = PRIO_DATA;
1420         config_packet->body.prio_ctrl = PRIO_CTRL;
1421         send_packet(hw, PRIO_SETUP, &config_packet->header);
1422     }
1423     config_done_packet = alloc_ctrl_packet(
1424             sizeof(struct ipw_setup_config_done_packet),
1425             ADDR_SETUP_PROT,
1426             TL_PROTOCOLID_SETUP,
1427             TL_SETUP_SIGNO_CONFIG_DONE_MSG);
1428     if (!config_done_packet)
1429         goto exit_nomem;
1430     config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
1431     send_packet(hw, PRIO_SETUP, &config_done_packet->header);
1432 
1433     /* generate open packet */
1434     for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
1435         open_packet = alloc_ctrl_packet(
1436                 sizeof(struct ipw_setup_open_packet),
1437                 ADDR_SETUP_PROT,
1438                 TL_PROTOCOLID_SETUP,
1439                 TL_SETUP_SIGNO_OPEN_MSG);
1440         if (!open_packet)
1441             goto exit_nomem;
1442         open_packet->header.length = sizeof(struct tl_setup_open_msg);
1443         open_packet->body.port_no = port;
1444         send_packet(hw, PRIO_SETUP, &open_packet->header);
1445     }
1446     for (channel_idx = 0;
1447             channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
1448         int ret;
1449 
1450         ret = set_DTR(hw, PRIO_SETUP, channel_idx,
1451             (hw->control_lines[channel_idx] &
1452              IPW_CONTROL_LINE_DTR) != 0);
1453         if (ret) {
1454             printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1455                     ": error setting DTR (%d)\n", ret);
1456             return;
1457         }
1458 
1459         ret = set_RTS(hw, PRIO_SETUP, channel_idx,
1460             (hw->control_lines [channel_idx] &
1461              IPW_CONTROL_LINE_RTS) != 0);
1462         if (ret) {
1463             printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1464                     ": error setting RTS (%d)\n", ret);
1465             return;
1466         }
1467     }
1468     /*
1469      * For NDIS we assume that we are using sync PPP frames, for COM async.
1470      * This driver uses NDIS mode too. We don't bother with translation
1471      * from async -> sync PPP.
1472      */
1473     info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
1474             ADDR_SETUP_PROT,
1475             TL_PROTOCOLID_SETUP,
1476             TL_SETUP_SIGNO_INFO_MSG);
1477     if (!info_packet)
1478         goto exit_nomem;
1479     info_packet->header.length = sizeof(struct tl_setup_info_msg);
1480     info_packet->body.driver_type = NDISWAN_DRIVER;
1481     info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
1482     info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
1483     send_packet(hw, PRIO_SETUP, &info_packet->header);
1484 
1485     /* Initialization is now complete, so we clear the 'to_setup' flag */
1486     hw->to_setup = 0;
1487 
1488     return;
1489 
1490 exit_nomem:
1491     printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1492             ": not enough memory to alloc control packet\n");
1493     hw->to_setup = -1;
1494 }
1495 
1496 static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
1497         unsigned char vers_no)
1498 {
1499     del_timer(&hw->setup_timer);
1500     hw->initializing = 0;
1501     printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
1502 
1503     if (vers_no == TL_SETUP_VERSION)
1504         __handle_setup_get_version_rsp(hw);
1505     else
1506         printk(KERN_ERR IPWIRELESS_PCCARD_NAME
1507                 ": invalid hardware version no %u\n",
1508                 (unsigned int) vers_no);
1509 }
1510 
1511 static void ipw_send_setup_packet(struct ipw_hardware *hw)
1512 {
1513     struct ipw_setup_get_version_query_packet *ver_packet;
1514 
1515     ver_packet = alloc_ctrl_packet(
1516             sizeof(struct ipw_setup_get_version_query_packet),
1517             ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1518             TL_SETUP_SIGNO_GET_VERSION_QRY);
1519     if (!ver_packet)
1520         return;
1521     ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
1522 
1523     /*
1524      * Response is handled in handle_received_SETUP_packet
1525      */
1526     send_packet(hw, PRIO_SETUP, &ver_packet->header);
1527 }
1528 
1529 static void handle_received_SETUP_packet(struct ipw_hardware *hw,
1530                      unsigned int address,
1531                      const unsigned char *data, int len,
1532                      int is_last)
1533 {
1534     const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
1535 
1536     if (address != ADDR_SETUP_PROT) {
1537         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1538                ": setup packet has bad address %d\n", address);
1539         return;
1540     }
1541 
1542     switch (rx_msg->sig_no) {
1543     case TL_SETUP_SIGNO_GET_VERSION_RSP:
1544         if (hw->to_setup)
1545             handle_setup_get_version_rsp(hw,
1546                     rx_msg->version_rsp_msg.version);
1547         break;
1548 
1549     case TL_SETUP_SIGNO_OPEN_MSG:
1550         if (ipwireless_debug) {
1551             unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
1552 
1553             printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1554                    ": OPEN_MSG [channel %u] reply received\n",
1555                    channel_idx);
1556         }
1557         break;
1558 
1559     case TL_SETUP_SIGNO_INFO_MSG_ACK:
1560         if (ipwireless_debug)
1561             printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1562                    ": card successfully configured as NDISWAN\n");
1563         break;
1564 
1565     case TL_SETUP_SIGNO_REBOOT_MSG:
1566         if (hw->to_setup)
1567             printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1568                    ": Setup not completed - ignoring reboot msg\n");
1569         else {
1570             struct ipw_setup_reboot_msg_ack *packet;
1571 
1572             printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
1573                    ": Acknowledging REBOOT message\n");
1574             packet = alloc_ctrl_packet(
1575                     sizeof(struct ipw_setup_reboot_msg_ack),
1576                     ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
1577                     TL_SETUP_SIGNO_REBOOT_MSG_ACK);
1578             if (!packet) {
1579                 pr_err(IPWIRELESS_PCCARD_NAME
1580                        ": Not enough memory to send reboot packet");
1581                 break;
1582             }
1583             packet->header.length =
1584                 sizeof(struct TlSetupRebootMsgAck);
1585             send_packet(hw, PRIO_SETUP, &packet->header);
1586             if (hw->reboot_callback)
1587                 hw->reboot_callback(hw->reboot_callback_data);
1588         }
1589         break;
1590 
1591     default:
1592         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1593                ": unknown setup message %u received\n",
1594                (unsigned int) rx_msg->sig_no);
1595     }
1596 }
1597 
1598 static void do_close_hardware(struct ipw_hardware *hw)
1599 {
1600     unsigned int irqn;
1601 
1602     if (hw->hw_version == HW_VERSION_1) {
1603         /* Disable TX and RX interrupts. */
1604         outw(0, hw->base_port + IOIER);
1605 
1606         /* Acknowledge any outstanding interrupt requests */
1607         irqn = inw(hw->base_port + IOIR);
1608         if (irqn & IR_TXINTR)
1609             outw(IR_TXINTR, hw->base_port + IOIR);
1610         if (irqn & IR_RXINTR)
1611             outw(IR_RXINTR, hw->base_port + IOIR);
1612 
1613         synchronize_irq(hw->irq);
1614     }
1615 }
1616 
1617 struct ipw_hardware *ipwireless_hardware_create(void)
1618 {
1619     int i;
1620     struct ipw_hardware *hw =
1621         kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
1622 
1623     if (!hw)
1624         return NULL;
1625 
1626     hw->irq = -1;
1627     hw->initializing = 1;
1628     hw->tx_ready = 1;
1629     hw->rx_bytes_queued = 0;
1630     hw->rx_pool_size = 0;
1631     hw->last_memtx_serial = (unsigned short) 0xffff;
1632     for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
1633         INIT_LIST_HEAD(&hw->tx_queue[i]);
1634 
1635     INIT_LIST_HEAD(&hw->rx_queue);
1636     INIT_LIST_HEAD(&hw->rx_pool);
1637     spin_lock_init(&hw->lock);
1638     tasklet_setup(&hw->tasklet, ipwireless_do_tasklet);
1639     INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1640     timer_setup(&hw->setup_timer, ipwireless_setup_timer, 0);
1641 
1642     return hw;
1643 }
1644 
1645 void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
1646         unsigned int base_port,
1647         void __iomem *attr_memory,
1648         void __iomem *common_memory,
1649         int is_v2_card,
1650         void (*reboot_callback) (void *data),
1651         void *reboot_callback_data)
1652 {
1653     if (hw->removed) {
1654         hw->removed = 0;
1655         enable_irq(hw->irq);
1656     }
1657     hw->base_port = base_port;
1658     hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
1659     hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
1660     hw->memregs_CCR = (struct MEMCCR __iomem *)
1661             ((unsigned short __iomem *) attr_memory + 0x200);
1662     hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
1663     hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
1664     hw->reboot_callback = reboot_callback;
1665     hw->reboot_callback_data = reboot_callback_data;
1666 }
1667 
1668 void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
1669 {
1670     hw->initializing = 1;
1671     hw->init_loops = 0;
1672     printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1673            ": waiting for card to start up...\n");
1674     ipwireless_setup_timer(&hw->setup_timer);
1675 }
1676 
1677 static void ipwireless_setup_timer(struct timer_list *t)
1678 {
1679     struct ipw_hardware *hw = from_timer(hw, t, setup_timer);
1680 
1681     hw->init_loops++;
1682 
1683     if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
1684             hw->hw_version == HW_VERSION_2 &&
1685             hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
1686         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1687                 ": failed to startup using TX2, trying TX\n");
1688 
1689         hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
1690         hw->init_loops = 0;
1691     }
1692     /* Give up after a certain number of retries */
1693     if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
1694         printk(KERN_INFO IPWIRELESS_PCCARD_NAME
1695                ": card failed to start up!\n");
1696         hw->initializing = 0;
1697     } else {
1698         /* Do not attempt to write to the board if it is not present. */
1699         if (is_card_present(hw)) {
1700             unsigned long flags;
1701 
1702             spin_lock_irqsave(&hw->lock, flags);
1703             hw->to_setup = 1;
1704             hw->tx_ready = 1;
1705             spin_unlock_irqrestore(&hw->lock, flags);
1706             tasklet_schedule(&hw->tasklet);
1707         }
1708 
1709         mod_timer(&hw->setup_timer,
1710             jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
1711     }
1712 }
1713 
1714 /*
1715  * Stop any interrupts from executing so that, once this function returns,
1716  * other layers of the driver can be sure they won't get any more callbacks.
1717  * Thus must be called on a proper process context.
1718  */
1719 void ipwireless_stop_interrupts(struct ipw_hardware *hw)
1720 {
1721     if (!hw->shutting_down) {
1722         /* Tell everyone we are going down. */
1723         hw->shutting_down = 1;
1724         del_timer(&hw->setup_timer);
1725 
1726         /* Prevent the hardware from sending any more interrupts */
1727         do_close_hardware(hw);
1728     }
1729 }
1730 
1731 void ipwireless_hardware_free(struct ipw_hardware *hw)
1732 {
1733     int i;
1734     struct ipw_rx_packet *rp, *rq;
1735     struct ipw_tx_packet *tp, *tq;
1736 
1737     ipwireless_stop_interrupts(hw);
1738 
1739     flush_work(&hw->work_rx);
1740 
1741     for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
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 }