21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
31 static void handle_received_SETUP_packet(
struct ipw_hardware *ipw,
33 const unsigned char *
data,
int len,
35 static void ipwireless_setup_timer(
unsigned long data);
37 unsigned int channel_idx,
const unsigned char *
data,
int len);
41 #ifdef TIMING_DIAGNOSTICS
43 static struct timing_stats {
44 unsigned long last_report_time;
45 unsigned long read_time;
46 unsigned long write_time;
47 unsigned long read_bytes;
48 unsigned long write_bytes;
52 static void start_timing(
void)
54 timing_stats.start_time =
jiffies;
57 static void end_read_timing(
unsigned length)
60 timing_stats.read_bytes += length + 2;
64 static void end_write_timing(
unsigned length)
67 timing_stats.write_bytes += length + 2;
71 static void report_timing(
void)
73 unsigned long since =
jiffies - timing_stats.last_report_time;
77 int first = (timing_stats.last_report_time == 0);
79 timing_stats.last_report_time =
jiffies;
82 ": %u us elapsed - read %lu bytes in %u us, wrote %lu bytes in %u us\n",
84 timing_stats.read_bytes,
86 timing_stats.write_bytes,
89 timing_stats.read_time = 0;
90 timing_stats.write_time = 0;
91 timing_stats.read_bytes = 0;
92 timing_stats.write_bytes = 0;
96 static void start_timing(
void) { }
97 static void end_read_timing(
unsigned length) { }
98 static void end_write_timing(
unsigned length) { }
103 #define LL_MTU_V1 318
104 #define LL_MTU_V2 250
105 #define LL_MTU_MAX (LL_MTU_V1 > LL_MTU_V2 ? LL_MTU_V1 : LL_MTU_V2)
112 #define ADDR_SETUP_PROT 0
128 #define NL_FIRST_PACKET_HEADER_SIZE 3
132 #define NL_FOLLOWING_PACKET_HEADER_SIZE 1
149 #define NL_INTERMEDIATE_PACKET 0x0
150 #define NL_LAST_PACKET 0x1
151 #define NL_FIRST_PACKET 0x2
162 #define HW_VERSION_UNKNOWN -1
163 #define HW_VERSION_1 1
164 #define HW_VERSION_2 2
179 #define IER_RXENABLED 0x1
180 #define IER_TXENABLED 0x2
183 #define IR_RXINTR 0x1
184 #define IR_TXINTR 0x2
187 #define DCR_RXDONE 0x1
188 #define DCR_TXDONE 0x2
189 #define DCR_RXRESET 0x4
190 #define DCR_TXRESET 0x8
217 #define CARD_PRESENT_VALUE (0xBEEFCAFEUL)
219 #define MEMTX_TX 0x0001
220 #define MEMRX_RX 0x0001
221 #define MEMRX_RX_DONE 0x0001
222 #define MEMRX_PCINTACKK 0x0001
224 #define NL_NUM_OF_PRIORITIES 3
225 #define NL_NUM_OF_PROTOCOLS 3
226 #define NL_NUM_OF_ADDRESSES NO_OF_IPW_CHANNELS
313 #define COMCTRL_RTS 0
314 #define COMCTRL_DTR 1
317 #define COMCTRL_CTS 2
318 #define COMCTRL_DCD 3
319 #define COMCTRL_DSR 4
354 default:
return "???? ";
360 #define DUMP_MAX_BYTES 64
362 static void dump_data_bytes(
const char *
type,
const unsigned char *
data,
367 sprintf(prefix, IPWIRELESS_PCCARD_NAME
": %s %s ",
369 print_hex_dump_bytes(prefix, 0, (
void *)data,
373 static void swap_packet_bitfield_to_le(
unsigned char *
data)
375 #ifdef __BIG_ENDIAN_BITFIELD
381 ret |= tmp & 0xc0 >> 6;
382 ret |= tmp & 0x38 >> 1;
383 ret |= tmp & 0x07 << 5;
388 static void swap_packet_bitfield_from_le(
unsigned char *
data)
390 #ifdef __BIG_ENDIAN_BITFIELD
396 ret |= tmp & 0xe0 >> 5;
397 ret |= tmp & 0x1c << 1;
398 ret |= tmp & 0x03 << 6;
413 dump_data_bytes(
"send", data, length);
418 swap_packet_bitfield_to_le(data);
423 for (i = 0; i <
length; i += 2) {
424 unsigned short d = data[
i];
428 d |= data[i + 1] << 8;
437 for (i = 0; i <
length; i += 2) {
438 unsigned short d = data[
i];
442 d |= data[i + 1] << 8;
446 while ((i & 3) != 2) {
453 spin_unlock_irqrestore(&hw->
lock, flags);
455 end_write_timing(length);
460 unsigned short fragment_data_len;
461 unsigned short data_left = packet->
length - packet->
offset;
469 fragment_data_len = hw->
ll_mtu - header_size;
470 if (data_left < fragment_data_len)
471 fragment_data_len = data_left;
477 pkt.hdr_first.protocol = packet->
protocol;
478 pkt.hdr_first.address = packet->
dest_addr;
479 pkt.hdr_first.packet_rank = 0;
484 pkt.hdr_first.length_lsb = (
unsigned char) packet->
length;
485 pkt.hdr_first.length_msb =
486 (
unsigned char) (packet->
length >> 8);
489 memcpy(pkt.rawpkt + header_size,
491 packet->
offset, fragment_data_len);
492 packet->
offset += fragment_data_len;
498 do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
511 spin_unlock_irqrestore(&hw->
lock, flags);
543 spin_unlock_irqrestore(&hw->
lock, flags);
556 int minimum_free_space)
563 if (!list_empty(&hw->
rx_pool)) {
567 spin_unlock_irqrestore(&hw->
lock, flags);
570 const int min_capacity =
574 spin_unlock_irqrestore(&hw->
lock, flags);
576 (minimum_free_space > min_capacity
592 old_packet->
length + minimum_free_space,
598 memcpy(packet, old_packet,
618 static void queue_received_packet(
struct ipw_hardware *hw,
621 const unsigned char *data,
int length,
631 ": data packet has bad address %u\n", address);
646 (*assem) = pool_allocate(hw, *assem, length);
649 ": no memory for incomming data packet, dropped!\n");
656 memcpy((
unsigned char *)(*assem) +
658 + (*assem)->length, data, length);
659 (*assem)->length +=
length;
666 spin_unlock_irqrestore(&hw->
lock, flags);
670 packet = pool_allocate(hw,
NULL, length);
673 ": no memory for incomming ctrl packet, dropped!\n");
694 spin_unlock_irqrestore(&hw->
lock, flags);
702 static void ipw_receive_data_work(
struct work_struct *work_rx)
709 while (!list_empty(&hw->
rx_queue)) {
726 spin_unlock_irqrestore(&hw->
lock, flags);
734 (
unsigned char *)packet
746 handle_received_CTRL_packet(hw, packet->
channel_idx,
747 (
unsigned char *)packet
751 pool_free(hw, packet);
761 spin_unlock_irqrestore(&hw->
lock, flags);
764 static void handle_received_CTRL_packet(
struct ipw_hardware *hw,
765 unsigned int channel_idx,
766 const unsigned char *data,
int len)
770 unsigned int changed_mask;
774 ": control packet was %d bytes - wrong size!\n",
796 if (changed_mask != 0) {
810 static void handle_received_packet(
struct ipw_hardware *hw,
814 unsigned int protocol = packet->
hdr.protocol;
815 unsigned int address = packet->
hdr.address;
817 const unsigned char *
data;
831 queue_received_packet(hw, protocol, address, data, data_len,
835 handle_received_SETUP_packet(hw, address, data, data_len,
841 static void acknowledge_data_read(
struct ipw_hardware *hw)
865 ": received a packet of %u bytes - longer than the MTU!\n", len);
870 for (i = 0; i < len; i += 2) {
874 pkt[
i] = (
unsigned char) data;
875 pkt[i + 1] = (
unsigned char) (data >> 8);
881 ": received a packet of %u bytes - longer than the MTU!\n", len);
887 for (i = 0; i < len; i += 2) {
891 pkt[
i] = (
unsigned char) data;
892 pkt[i + 1] = (
unsigned char) (data >> 8);
895 while ((i & 3) != 2) {
901 acknowledge_data_read(hw);
903 swap_packet_bitfield_from_le(pkt);
906 dump_data_bytes(
"recv", pkt, len);
908 handle_received_packet(hw, (
union nl_packet *) pkt, len);
910 end_read_timing(len);
913 static int get_current_packet_priority(
struct ipw_hardware *hw)
937 spin_unlock_irqrestore(&hw->
lock, flags);
939 do_receive_packet(hw);
943 spin_unlock_irqrestore(&hw->
lock, flags);
954 static int send_pending_packet(
struct ipw_hardware *hw,
int priority_limit)
956 int more_to_send = 0;
965 for (priority = 0; priority < priority_limit; priority++) {
966 if (!list_empty(&hw->
tx_queue[priority])) {
980 spin_unlock_irqrestore(&hw->
lock, flags);
984 spin_unlock_irqrestore(&hw->
lock, flags);
987 do_send_packet(hw, packet);
991 for (priority = 0; priority < priority_limit; priority++)
992 if (!list_empty(&hw->
tx_queue[priority])) {
1000 spin_unlock_irqrestore(&hw->
lock, flags);
1002 return more_to_send;
1008 static void ipwireless_do_tasklet(
unsigned long hw_)
1011 unsigned long flags;
1015 spin_unlock_irqrestore(&hw->
lock, flags);
1024 spin_unlock_irqrestore(&hw->
lock, flags);
1026 ipw_setup_hardware(hw);
1027 ipw_send_setup_packet(hw);
1030 get_packets_from_hw(hw);
1032 int priority_limit = get_current_packet_priority(hw);
1035 spin_unlock_irqrestore(&hw->
lock, flags);
1038 again = send_pending_packet(hw, priority_limit);
1039 again |= get_packets_from_hw(hw);
1059 unsigned short irqn;
1066 else if (irqn != 0) {
1067 unsigned short ack = 0;
1068 unsigned long flags;
1075 spin_unlock_irqrestore(&hw->
lock, flags);
1082 spin_unlock_irqrestore(&hw->
lock, flags);
1086 tasklet_schedule(&hw->
tasklet);
1093 static void acknowledge_pcmcia_interrupt(
struct ipw_hardware *hw)
1101 static irqreturn_t ipwireless_handle_v2_v3_interrupt(
int irq,
1108 unsigned long flags;
1113 unsigned short memtx_serial;
1114 unsigned short memrxdone =
1125 if (memtx & MEMTX_TX) {
1127 ": Using memreg_tx_old\n");
1141 if (!is_card_present(hw)) {
1142 acknowledge_pcmcia_interrupt(hw);
1146 memtx_serial = memtx & (
unsigned short) 0xff00;
1147 if (memtx & MEMTX_TX) {
1155 spin_unlock_irqrestore(&hw->
lock, flags);
1165 if (memtx_serial != 0) {
1168 ": memreg_tx serial num detected\n");
1172 spin_unlock_irqrestore(&hw->
lock, flags);
1177 if (memrxdone & MEMRX_RX_DONE) {
1181 spin_unlock_irqrestore(&hw->
lock, flags);
1188 acknowledge_pcmcia_interrupt(hw);
1191 tasklet_schedule(&hw->
tasklet);
1192 else if (!rx_repeat) {
1196 ": spurious interrupt - new_tx mode\n");
1199 ": no valid memreg_tx value - switching to the old memreg_tx\n");
1206 ": spurious interrupt - old_tx mode\n");
1209 }
while (try_mem_tx_old == 1);
1219 return ipwireless_handle_v1_interrupt(irq, ipw->
hardware);
1221 return ipwireless_handle_v2_v3_interrupt(irq, ipw->
hardware);
1224 static void flush_packets_to_hw(
struct ipw_hardware *hw)
1227 unsigned long flags;
1230 priority_limit = get_current_packet_priority(hw);
1231 spin_unlock_irqrestore(&hw->
lock, flags);
1233 while (send_pending_packet(hw, priority_limit));
1239 unsigned long flags;
1244 spin_unlock_irqrestore(&hw->
lock, flags);
1246 flush_packets_to_hw(hw);
1250 static void *alloc_data_packet(
int data_size,
1252 unsigned char protocol)
1261 INIT_LIST_HEAD(&packet->
queue);
1269 static void *alloc_ctrl_packet(
int header_size,
1270 unsigned char dest_addr,
1271 unsigned char protocol,
1284 INIT_LIST_HEAD(&packet->
header.queue);
1294 const unsigned char *data,
unsigned int length,
1295 void (*
callback) (
void *
cb,
unsigned int length),
1300 packet = alloc_data_packet(length, (channel_idx + 1),
1314 unsigned int channel_idx,
int line,
int state)
1323 (channel_idx + 1), protocolid, line);
1327 packet->
body.value = (state == 0 ? 0 : 1);
1333 static int set_DTR(
struct ipw_hardware *hw,
int priority,
1334 unsigned int channel_idx,
int state)
1341 return set_control_line(hw, priority, channel_idx,
COMCTRL_DTR, state);
1344 static int set_RTS(
struct ipw_hardware *hw,
int priority,
1345 unsigned int channel_idx,
int state)
1352 return set_control_line(hw, priority, channel_idx,
COMCTRL_RTS, state);
1358 return set_DTR(hw,
PRIO_CTRL, channel_idx, state);
1364 return set_RTS(hw,
PRIO_CTRL, channel_idx, state);
1398 static void __handle_setup_get_version_rsp(
struct ipw_hardware *hw)
1405 unsigned int channel_idx;
1409 config_packet = alloc_ctrl_packet(
1417 config_packet->
body.port_no =
port;
1422 config_done_packet = alloc_ctrl_packet(
1427 if (!config_done_packet)
1434 open_packet = alloc_ctrl_packet(
1445 for (channel_idx = 0;
1454 ": error setting DTR (%d)\n", ret);
1463 ": error setting RTS (%d)\n", ret);
1491 ": not enough memory to alloc control packet\n");
1495 static void handle_setup_get_version_rsp(
struct ipw_hardware *hw,
1496 unsigned char vers_no)
1503 __handle_setup_get_version_rsp(hw);
1506 ": invalid hardware version no %u\n",
1507 (
unsigned int) vers_no);
1510 static void ipw_send_setup_packet(
struct ipw_hardware *hw)
1514 ver_packet = alloc_ctrl_packet(
1526 static void handle_received_SETUP_packet(
struct ipw_hardware *hw,
1527 unsigned int address,
1528 const unsigned char *data,
int len,
1535 ": setup packet has bad address %d\n", address);
1539 switch (rx_msg->
sig_no) {
1542 handle_setup_get_version_rsp(hw,
1548 unsigned int channel_idx = rx_msg->
open_msg.port_no - 1;
1551 ": OPEN_MSG [channel %u] reply received\n",
1559 ": card successfully configured as NDISWAN\n");
1565 ": Setup not completed - ignoring reboot msg\n");
1570 ": Acknowledging REBOOT message\n");
1571 packet = alloc_ctrl_packet(
1585 ": unknown setup message %u received\n",
1586 (
unsigned int) rx_msg->
sig_no);
1600 if (irqn & IR_TXINTR)
1631 INIT_WORK(&hw->work_rx, ipw_receive_data_work);
1632 setup_timer(&hw->setup_timer, ipwireless_setup_timer,
1633 (
unsigned long) hw);
1654 ((
unsigned short __iomem *) attr_memory + 0x200);
1666 ": waiting for card to start up...\n");
1667 ipwireless_setup_timer((
unsigned long) hw);
1670 static void ipwireless_setup_timer(
unsigned long data)
1680 ": failed to startup using TX2, trying TX\n");
1688 ": card failed to start up!\n");
1692 if (is_card_present(hw)) {
1693 unsigned long flags;
1698 spin_unlock_irqrestore(&hw->
lock, flags);
1699 tasklet_schedule(&hw->
tasklet);
1720 do_close_hardware(hw);