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