hardware.c 45 KB

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