hardware.c 46 KB

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