hardware.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  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. const 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, const 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 void 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. BUG_ON(length > hw->ll_mtu);
  345. if (ipwireless_debug)
  346. dump_data_bytes("send", data, length);
  347. spin_lock_irqsave(&hw->lock, flags);
  348. hw->tx_ready = 0;
  349. swap_packet_bitfield_to_le(data);
  350. if (hw->hw_version == HW_VERSION_1) {
  351. outw((unsigned short) length, hw->base_port + IODWR);
  352. for (i = 0; i < length; i += 2) {
  353. unsigned short d = data[i];
  354. __le16 raw_data;
  355. if (i + 1 < length)
  356. d |= data[i + 1] << 8;
  357. raw_data = cpu_to_le16(d);
  358. outw(raw_data, hw->base_port + IODWR);
  359. }
  360. outw(DCR_TXDONE, hw->base_port + IODCR);
  361. } else if (hw->hw_version == HW_VERSION_2) {
  362. outw((unsigned short) length, hw->base_port);
  363. for (i = 0; i < length; i += 2) {
  364. unsigned short d = data[i];
  365. __le16 raw_data;
  366. if (i + 1 < length)
  367. d |= data[i + 1] << 8;
  368. raw_data = cpu_to_le16(d);
  369. outw(raw_data, hw->base_port);
  370. }
  371. while ((i & 3) != 2) {
  372. outw((unsigned short) 0xDEAD, hw->base_port);
  373. i += 2;
  374. }
  375. writew(MEMRX_RX, &hw->memory_info_regs->memreg_rx);
  376. }
  377. spin_unlock_irqrestore(&hw->lock, flags);
  378. end_write_timing(length);
  379. }
  380. static void do_send_packet(struct ipw_hardware *hw, struct ipw_tx_packet *packet)
  381. {
  382. unsigned short fragment_data_len;
  383. unsigned short data_left = packet->length - packet->offset;
  384. unsigned short header_size;
  385. union nl_packet pkt;
  386. header_size =
  387. (packet->fragment_count == 0)
  388. ? NL_FIRST_PACKET_HEADER_SIZE
  389. : NL_FOLLOWING_PACKET_HEADER_SIZE;
  390. fragment_data_len = hw->ll_mtu - header_size;
  391. if (data_left < fragment_data_len)
  392. fragment_data_len = data_left;
  393. /*
  394. * hdr_first is now in machine bitfield order, which will be swapped
  395. * to le just before it goes to hw
  396. */
  397. pkt.hdr_first.protocol = packet->protocol;
  398. pkt.hdr_first.address = packet->dest_addr;
  399. pkt.hdr_first.packet_rank = 0;
  400. /* First packet? */
  401. if (packet->fragment_count == 0) {
  402. pkt.hdr_first.packet_rank |= NL_FIRST_PACKET;
  403. pkt.hdr_first.length_lsb = (unsigned char) packet->length;
  404. pkt.hdr_first.length_msb =
  405. (unsigned char) (packet->length >> 8);
  406. }
  407. memcpy(pkt.rawpkt + header_size,
  408. ((unsigned char *) packet) + sizeof(struct ipw_tx_packet) +
  409. packet->offset, fragment_data_len);
  410. packet->offset += fragment_data_len;
  411. packet->fragment_count++;
  412. /* Last packet? (May also be first packet.) */
  413. if (packet->offset == packet->length)
  414. pkt.hdr_first.packet_rank |= NL_LAST_PACKET;
  415. do_send_fragment(hw, pkt.rawpkt, header_size + fragment_data_len);
  416. /* If this packet has unsent data, then re-queue it. */
  417. if (packet->offset < packet->length) {
  418. /*
  419. * Re-queue it at the head of the highest priority queue so
  420. * it goes before all other packets
  421. */
  422. unsigned long flags;
  423. spin_lock_irqsave(&hw->lock, flags);
  424. list_add(&packet->queue, &hw->tx_queue[0]);
  425. hw->tx_queued++;
  426. spin_unlock_irqrestore(&hw->lock, flags);
  427. } else {
  428. if (packet->packet_callback)
  429. packet->packet_callback(packet->callback_data,
  430. packet->length);
  431. kfree(packet);
  432. }
  433. }
  434. static void ipw_setup_hardware(struct ipw_hardware *hw)
  435. {
  436. unsigned long flags;
  437. spin_lock_irqsave(&hw->lock, flags);
  438. if (hw->hw_version == HW_VERSION_1) {
  439. /* Reset RX FIFO */
  440. outw(DCR_RXRESET, hw->base_port + IODCR);
  441. /* SB: Reset TX FIFO */
  442. outw(DCR_TXRESET, hw->base_port + IODCR);
  443. /* Enable TX and RX interrupts. */
  444. outw(IER_TXENABLED | IER_RXENABLED, hw->base_port + IOIER);
  445. } else {
  446. /*
  447. * Set INTRACK bit (bit 0), which means we must explicitly
  448. * acknowledge interrupts by clearing bit 2 of reg_config_and_status.
  449. */
  450. unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
  451. csr |= 1;
  452. writew(csr, &hw->memregs_CCR->reg_config_and_status);
  453. }
  454. spin_unlock_irqrestore(&hw->lock, flags);
  455. }
  456. /*
  457. * If 'packet' is NULL, then this function allocates a new packet, setting its
  458. * length to 0 and ensuring it has the specified minimum amount of free space.
  459. *
  460. * If 'packet' is not NULL, then this function enlarges it if it doesn't
  461. * have the specified minimum amount of free space.
  462. *
  463. */
  464. static struct ipw_rx_packet *pool_allocate(struct ipw_hardware *hw,
  465. struct ipw_rx_packet *packet,
  466. int minimum_free_space)
  467. {
  468. if (!packet) {
  469. unsigned long flags;
  470. spin_lock_irqsave(&hw->lock, flags);
  471. if (!list_empty(&hw->rx_pool)) {
  472. packet = list_first_entry(&hw->rx_pool,
  473. struct ipw_rx_packet, queue);
  474. hw->rx_pool_size--;
  475. spin_unlock_irqrestore(&hw->lock, flags);
  476. list_del(&packet->queue);
  477. } else {
  478. const int min_capacity =
  479. ipwireless_ppp_mru(hw->network) + 2;
  480. int new_capacity;
  481. spin_unlock_irqrestore(&hw->lock, flags);
  482. new_capacity =
  483. (minimum_free_space > min_capacity
  484. ? minimum_free_space
  485. : min_capacity);
  486. packet = kmalloc(sizeof(struct ipw_rx_packet)
  487. + new_capacity, GFP_ATOMIC);
  488. if (!packet)
  489. return NULL;
  490. packet->capacity = new_capacity;
  491. }
  492. packet->length = 0;
  493. }
  494. if (packet->length + minimum_free_space > packet->capacity) {
  495. struct ipw_rx_packet *old_packet = packet;
  496. packet = kmalloc(sizeof(struct ipw_rx_packet) +
  497. old_packet->length + minimum_free_space,
  498. GFP_ATOMIC);
  499. if (!packet) {
  500. kfree(old_packet);
  501. return NULL;
  502. }
  503. memcpy(packet, old_packet,
  504. sizeof(struct ipw_rx_packet)
  505. + old_packet->length);
  506. packet->capacity = old_packet->length + minimum_free_space;
  507. kfree(old_packet);
  508. }
  509. return packet;
  510. }
  511. static void pool_free(struct ipw_hardware *hw, struct ipw_rx_packet *packet)
  512. {
  513. if (hw->rx_pool_size > 6)
  514. kfree(packet);
  515. else {
  516. hw->rx_pool_size++;
  517. list_add(&packet->queue, &hw->rx_pool);
  518. }
  519. }
  520. static void queue_received_packet(struct ipw_hardware *hw,
  521. unsigned int protocol,
  522. unsigned int address,
  523. const unsigned char *data, int length,
  524. int is_last)
  525. {
  526. unsigned int channel_idx = address - 1;
  527. struct ipw_rx_packet *packet = NULL;
  528. unsigned long flags;
  529. /* Discard packet if channel index is out of range. */
  530. if (channel_idx >= NL_NUM_OF_ADDRESSES) {
  531. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  532. ": data packet has bad address %u\n", address);
  533. return;
  534. }
  535. /*
  536. * ->packet_assembler is safe to touch unlocked, this is the only place
  537. */
  538. if (protocol == TL_PROTOCOLID_COM_DATA) {
  539. struct ipw_rx_packet **assem =
  540. &hw->packet_assembler[channel_idx];
  541. /*
  542. * Create a new packet, or assembler already contains one
  543. * enlarge it by 'length' bytes.
  544. */
  545. (*assem) = pool_allocate(hw, *assem, length);
  546. if (!(*assem)) {
  547. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  548. ": no memory for incoming data packet, dropped!\n");
  549. return;
  550. }
  551. (*assem)->protocol = protocol;
  552. (*assem)->channel_idx = channel_idx;
  553. /* Append this packet data onto existing data. */
  554. memcpy((unsigned char *)(*assem) +
  555. sizeof(struct ipw_rx_packet)
  556. + (*assem)->length, data, length);
  557. (*assem)->length += length;
  558. if (is_last) {
  559. packet = *assem;
  560. *assem = NULL;
  561. /* Count queued DATA bytes only */
  562. spin_lock_irqsave(&hw->lock, flags);
  563. hw->rx_bytes_queued += packet->length;
  564. spin_unlock_irqrestore(&hw->lock, flags);
  565. }
  566. } else {
  567. /* If it's a CTRL packet, don't assemble, just queue it. */
  568. packet = pool_allocate(hw, NULL, length);
  569. if (!packet) {
  570. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  571. ": no memory for incoming ctrl packet, dropped!\n");
  572. return;
  573. }
  574. packet->protocol = protocol;
  575. packet->channel_idx = channel_idx;
  576. memcpy((unsigned char *)packet + sizeof(struct ipw_rx_packet),
  577. data, length);
  578. packet->length = length;
  579. }
  580. /*
  581. * If this is the last packet, then send the assembled packet on to the
  582. * network layer.
  583. */
  584. if (packet) {
  585. spin_lock_irqsave(&hw->lock, flags);
  586. list_add_tail(&packet->queue, &hw->rx_queue);
  587. /* Block reception of incoming packets if queue is full. */
  588. hw->blocking_rx =
  589. (hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE);
  590. spin_unlock_irqrestore(&hw->lock, flags);
  591. schedule_work(&hw->work_rx);
  592. }
  593. }
  594. /*
  595. * Workqueue callback
  596. */
  597. static void ipw_receive_data_work(struct work_struct *work_rx)
  598. {
  599. struct ipw_hardware *hw =
  600. container_of(work_rx, struct ipw_hardware, work_rx);
  601. unsigned long flags;
  602. spin_lock_irqsave(&hw->lock, flags);
  603. while (!list_empty(&hw->rx_queue)) {
  604. struct ipw_rx_packet *packet =
  605. list_first_entry(&hw->rx_queue,
  606. struct ipw_rx_packet, queue);
  607. if (hw->shutting_down)
  608. break;
  609. list_del(&packet->queue);
  610. /*
  611. * Note: ipwireless_network_packet_received must be called in a
  612. * process context (i.e. via schedule_work) because the tty
  613. * output code can sleep in the tty_flip_buffer_push call.
  614. */
  615. if (packet->protocol == TL_PROTOCOLID_COM_DATA) {
  616. if (hw->network != NULL) {
  617. /* If the network hasn't been disconnected. */
  618. spin_unlock_irqrestore(&hw->lock, flags);
  619. /*
  620. * This must run unlocked due to tty processing
  621. * and mutex locking
  622. */
  623. ipwireless_network_packet_received(
  624. hw->network,
  625. packet->channel_idx,
  626. (unsigned char *)packet
  627. + sizeof(struct ipw_rx_packet),
  628. packet->length);
  629. spin_lock_irqsave(&hw->lock, flags);
  630. }
  631. /* Count queued DATA bytes only */
  632. hw->rx_bytes_queued -= packet->length;
  633. } else {
  634. /*
  635. * This is safe to be called locked, callchain does
  636. * not block
  637. */
  638. handle_received_CTRL_packet(hw, packet->channel_idx,
  639. (unsigned char *)packet
  640. + sizeof(struct ipw_rx_packet),
  641. packet->length);
  642. }
  643. pool_free(hw, packet);
  644. /*
  645. * Unblock reception of incoming packets if queue is no longer
  646. * full.
  647. */
  648. hw->blocking_rx =
  649. hw->rx_bytes_queued >= IPWIRELESS_RX_QUEUE_SIZE;
  650. if (hw->shutting_down)
  651. break;
  652. }
  653. spin_unlock_irqrestore(&hw->lock, flags);
  654. }
  655. static void handle_received_CTRL_packet(struct ipw_hardware *hw,
  656. unsigned int channel_idx,
  657. const unsigned char *data, int len)
  658. {
  659. const struct ipw_control_packet_body *body =
  660. (const struct ipw_control_packet_body *) data;
  661. unsigned int changed_mask;
  662. if (len != sizeof(struct ipw_control_packet_body)) {
  663. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  664. ": control packet was %d bytes - wrong size!\n",
  665. len);
  666. return;
  667. }
  668. switch (body->sig_no) {
  669. case COMCTRL_CTS:
  670. changed_mask = IPW_CONTROL_LINE_CTS;
  671. break;
  672. case COMCTRL_DCD:
  673. changed_mask = IPW_CONTROL_LINE_DCD;
  674. break;
  675. case COMCTRL_DSR:
  676. changed_mask = IPW_CONTROL_LINE_DSR;
  677. break;
  678. case COMCTRL_RI:
  679. changed_mask = IPW_CONTROL_LINE_RI;
  680. break;
  681. default:
  682. changed_mask = 0;
  683. }
  684. if (changed_mask != 0) {
  685. if (body->value)
  686. hw->control_lines[channel_idx] |= changed_mask;
  687. else
  688. hw->control_lines[channel_idx] &= ~changed_mask;
  689. if (hw->network)
  690. ipwireless_network_notify_control_line_change(
  691. hw->network,
  692. channel_idx,
  693. hw->control_lines[channel_idx],
  694. changed_mask);
  695. }
  696. }
  697. static void handle_received_packet(struct ipw_hardware *hw,
  698. const union nl_packet *packet,
  699. unsigned short len)
  700. {
  701. unsigned int protocol = packet->hdr.protocol;
  702. unsigned int address = packet->hdr.address;
  703. unsigned int header_length;
  704. const unsigned char *data;
  705. unsigned int data_len;
  706. int is_last = packet->hdr.packet_rank & NL_LAST_PACKET;
  707. if (packet->hdr.packet_rank & NL_FIRST_PACKET)
  708. header_length = NL_FIRST_PACKET_HEADER_SIZE;
  709. else
  710. header_length = NL_FOLLOWING_PACKET_HEADER_SIZE;
  711. data = packet->rawpkt + header_length;
  712. data_len = len - header_length;
  713. switch (protocol) {
  714. case TL_PROTOCOLID_COM_DATA:
  715. case TL_PROTOCOLID_COM_CTRL:
  716. queue_received_packet(hw, protocol, address, data, data_len,
  717. is_last);
  718. break;
  719. case TL_PROTOCOLID_SETUP:
  720. handle_received_SETUP_packet(hw, address, data, data_len,
  721. is_last);
  722. break;
  723. }
  724. }
  725. static void acknowledge_data_read(struct ipw_hardware *hw)
  726. {
  727. if (hw->hw_version == HW_VERSION_1)
  728. outw(DCR_RXDONE, hw->base_port + IODCR);
  729. else
  730. writew(MEMRX_PCINTACKK,
  731. &hw->memory_info_regs->memreg_pc_interrupt_ack);
  732. }
  733. /*
  734. * Retrieve a packet from the IPW hardware.
  735. */
  736. static void do_receive_packet(struct ipw_hardware *hw)
  737. {
  738. unsigned len;
  739. unsigned i;
  740. unsigned char pkt[LL_MTU_MAX];
  741. start_timing();
  742. if (hw->hw_version == HW_VERSION_1) {
  743. len = inw(hw->base_port + IODRR);
  744. if (len > hw->ll_mtu) {
  745. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  746. ": received a packet of %u bytes - longer than the MTU!\n", len);
  747. outw(DCR_RXDONE | DCR_RXRESET, hw->base_port + IODCR);
  748. return;
  749. }
  750. for (i = 0; i < len; i += 2) {
  751. __le16 raw_data = inw(hw->base_port + IODRR);
  752. unsigned short data = le16_to_cpu(raw_data);
  753. pkt[i] = (unsigned char) data;
  754. pkt[i + 1] = (unsigned char) (data >> 8);
  755. }
  756. } else {
  757. len = inw(hw->base_port);
  758. if (len > hw->ll_mtu) {
  759. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  760. ": received a packet of %u bytes - longer than the MTU!\n", len);
  761. writew(MEMRX_PCINTACKK,
  762. &hw->memory_info_regs->memreg_pc_interrupt_ack);
  763. return;
  764. }
  765. for (i = 0; i < len; i += 2) {
  766. __le16 raw_data = inw(hw->base_port);
  767. unsigned short data = le16_to_cpu(raw_data);
  768. pkt[i] = (unsigned char) data;
  769. pkt[i + 1] = (unsigned char) (data >> 8);
  770. }
  771. while ((i & 3) != 2) {
  772. inw(hw->base_port);
  773. i += 2;
  774. }
  775. }
  776. acknowledge_data_read(hw);
  777. swap_packet_bitfield_from_le(pkt);
  778. if (ipwireless_debug)
  779. dump_data_bytes("recv", pkt, len);
  780. handle_received_packet(hw, (union nl_packet *) pkt, len);
  781. end_read_timing(len);
  782. }
  783. static int get_current_packet_priority(struct ipw_hardware *hw)
  784. {
  785. /*
  786. * If we're initializing, don't send anything of higher priority than
  787. * PRIO_SETUP. The network layer therefore need not care about
  788. * hardware initialization - any of its stuff will simply be queued
  789. * until setup is complete.
  790. */
  791. return (hw->to_setup || hw->initializing
  792. ? PRIO_SETUP + 1 : NL_NUM_OF_PRIORITIES);
  793. }
  794. /*
  795. * return 1 if something has been received from hw
  796. */
  797. static int get_packets_from_hw(struct ipw_hardware *hw)
  798. {
  799. int received = 0;
  800. unsigned long flags;
  801. spin_lock_irqsave(&hw->lock, flags);
  802. while (hw->rx_ready && !hw->blocking_rx) {
  803. received = 1;
  804. hw->rx_ready--;
  805. spin_unlock_irqrestore(&hw->lock, flags);
  806. do_receive_packet(hw);
  807. spin_lock_irqsave(&hw->lock, flags);
  808. }
  809. spin_unlock_irqrestore(&hw->lock, flags);
  810. return received;
  811. }
  812. /*
  813. * Send pending packet up to given priority, prioritize SETUP data until
  814. * hardware is fully setup.
  815. *
  816. * return 1 if more packets can be sent
  817. */
  818. static int send_pending_packet(struct ipw_hardware *hw, int priority_limit)
  819. {
  820. int more_to_send = 0;
  821. unsigned long flags;
  822. spin_lock_irqsave(&hw->lock, flags);
  823. if (hw->tx_queued && hw->tx_ready) {
  824. int priority;
  825. struct ipw_tx_packet *packet = NULL;
  826. /* Pick a packet */
  827. for (priority = 0; priority < priority_limit; priority++) {
  828. if (!list_empty(&hw->tx_queue[priority])) {
  829. packet = list_first_entry(
  830. &hw->tx_queue[priority],
  831. struct ipw_tx_packet,
  832. queue);
  833. hw->tx_queued--;
  834. list_del(&packet->queue);
  835. break;
  836. }
  837. }
  838. if (!packet) {
  839. hw->tx_queued = 0;
  840. spin_unlock_irqrestore(&hw->lock, flags);
  841. return 0;
  842. }
  843. spin_unlock_irqrestore(&hw->lock, flags);
  844. /* Send */
  845. do_send_packet(hw, packet);
  846. /* Check if more to send */
  847. spin_lock_irqsave(&hw->lock, flags);
  848. for (priority = 0; priority < priority_limit; priority++)
  849. if (!list_empty(&hw->tx_queue[priority])) {
  850. more_to_send = 1;
  851. break;
  852. }
  853. if (!more_to_send)
  854. hw->tx_queued = 0;
  855. }
  856. spin_unlock_irqrestore(&hw->lock, flags);
  857. return more_to_send;
  858. }
  859. /*
  860. * Send and receive all queued packets.
  861. */
  862. static void ipwireless_do_tasklet(unsigned long hw_)
  863. {
  864. struct ipw_hardware *hw = (struct ipw_hardware *) hw_;
  865. unsigned long flags;
  866. spin_lock_irqsave(&hw->lock, flags);
  867. if (hw->shutting_down) {
  868. spin_unlock_irqrestore(&hw->lock, flags);
  869. return;
  870. }
  871. if (hw->to_setup == 1) {
  872. /*
  873. * Initial setup data sent to hardware
  874. */
  875. hw->to_setup = 2;
  876. spin_unlock_irqrestore(&hw->lock, flags);
  877. ipw_setup_hardware(hw);
  878. ipw_send_setup_packet(hw);
  879. send_pending_packet(hw, PRIO_SETUP + 1);
  880. get_packets_from_hw(hw);
  881. } else {
  882. int priority_limit = get_current_packet_priority(hw);
  883. int again;
  884. spin_unlock_irqrestore(&hw->lock, flags);
  885. do {
  886. again = send_pending_packet(hw, priority_limit);
  887. again |= get_packets_from_hw(hw);
  888. } while (again);
  889. }
  890. }
  891. /*
  892. * return true if the card is physically present.
  893. */
  894. static int is_card_present(struct ipw_hardware *hw)
  895. {
  896. if (hw->hw_version == HW_VERSION_1)
  897. return inw(hw->base_port + IOIR) != 0xFFFF;
  898. else
  899. return readl(&hw->memory_info_regs->memreg_card_present) ==
  900. CARD_PRESENT_VALUE;
  901. }
  902. static irqreturn_t ipwireless_handle_v1_interrupt(int irq,
  903. struct ipw_hardware *hw)
  904. {
  905. unsigned short irqn;
  906. irqn = inw(hw->base_port + IOIR);
  907. /* Check if card is present */
  908. if (irqn == 0xFFFF)
  909. return IRQ_NONE;
  910. else if (irqn != 0) {
  911. unsigned short ack = 0;
  912. unsigned long flags;
  913. /* Transmit complete. */
  914. if (irqn & IR_TXINTR) {
  915. ack |= IR_TXINTR;
  916. spin_lock_irqsave(&hw->lock, flags);
  917. hw->tx_ready = 1;
  918. spin_unlock_irqrestore(&hw->lock, flags);
  919. }
  920. /* Received data */
  921. if (irqn & IR_RXINTR) {
  922. ack |= IR_RXINTR;
  923. spin_lock_irqsave(&hw->lock, flags);
  924. hw->rx_ready++;
  925. spin_unlock_irqrestore(&hw->lock, flags);
  926. }
  927. if (ack != 0) {
  928. outw(ack, hw->base_port + IOIR);
  929. tasklet_schedule(&hw->tasklet);
  930. }
  931. return IRQ_HANDLED;
  932. }
  933. return IRQ_NONE;
  934. }
  935. static void acknowledge_pcmcia_interrupt(struct ipw_hardware *hw)
  936. {
  937. unsigned short csr = readw(&hw->memregs_CCR->reg_config_and_status);
  938. csr &= 0xfffd;
  939. writew(csr, &hw->memregs_CCR->reg_config_and_status);
  940. }
  941. static irqreturn_t ipwireless_handle_v2_v3_interrupt(int irq,
  942. struct ipw_hardware *hw)
  943. {
  944. int tx = 0;
  945. int rx = 0;
  946. int rx_repeat = 0;
  947. int try_mem_tx_old;
  948. unsigned long flags;
  949. do {
  950. unsigned short memtx = readw(hw->memreg_tx);
  951. unsigned short memtx_serial;
  952. unsigned short memrxdone =
  953. readw(&hw->memory_info_regs->memreg_rx_done);
  954. try_mem_tx_old = 0;
  955. /* check whether the interrupt was generated by ipwireless card */
  956. if (!(memtx & MEMTX_TX) && !(memrxdone & MEMRX_RX_DONE)) {
  957. /* check if the card uses memreg_tx_old register */
  958. if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
  959. memtx = readw(&hw->memory_info_regs->memreg_tx_old);
  960. if (memtx & MEMTX_TX) {
  961. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  962. ": Using memreg_tx_old\n");
  963. hw->memreg_tx =
  964. &hw->memory_info_regs->memreg_tx_old;
  965. } else {
  966. return IRQ_NONE;
  967. }
  968. } else
  969. return IRQ_NONE;
  970. }
  971. /*
  972. * See if the card is physically present. Note that while it is
  973. * powering up, it appears not to be present.
  974. */
  975. if (!is_card_present(hw)) {
  976. acknowledge_pcmcia_interrupt(hw);
  977. return IRQ_HANDLED;
  978. }
  979. memtx_serial = memtx & (unsigned short) 0xff00;
  980. if (memtx & MEMTX_TX) {
  981. writew(memtx_serial, hw->memreg_tx);
  982. if (hw->serial_number_detected) {
  983. if (memtx_serial != hw->last_memtx_serial) {
  984. hw->last_memtx_serial = memtx_serial;
  985. spin_lock_irqsave(&hw->lock, flags);
  986. hw->rx_ready++;
  987. spin_unlock_irqrestore(&hw->lock, flags);
  988. rx = 1;
  989. } else
  990. /* Ignore 'Timer Recovery' duplicates. */
  991. rx_repeat = 1;
  992. } else {
  993. /*
  994. * If a non-zero serial number is seen, then enable
  995. * serial number checking.
  996. */
  997. if (memtx_serial != 0) {
  998. hw->serial_number_detected = 1;
  999. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
  1000. ": memreg_tx serial num detected\n");
  1001. spin_lock_irqsave(&hw->lock, flags);
  1002. hw->rx_ready++;
  1003. spin_unlock_irqrestore(&hw->lock, flags);
  1004. }
  1005. rx = 1;
  1006. }
  1007. }
  1008. if (memrxdone & MEMRX_RX_DONE) {
  1009. writew(0, &hw->memory_info_regs->memreg_rx_done);
  1010. spin_lock_irqsave(&hw->lock, flags);
  1011. hw->tx_ready = 1;
  1012. spin_unlock_irqrestore(&hw->lock, flags);
  1013. tx = 1;
  1014. }
  1015. if (tx)
  1016. writew(MEMRX_PCINTACKK,
  1017. &hw->memory_info_regs->memreg_pc_interrupt_ack);
  1018. acknowledge_pcmcia_interrupt(hw);
  1019. if (tx || rx)
  1020. tasklet_schedule(&hw->tasklet);
  1021. else if (!rx_repeat) {
  1022. if (hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
  1023. if (hw->serial_number_detected)
  1024. printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
  1025. ": spurious interrupt - new_tx mode\n");
  1026. else {
  1027. printk(KERN_WARNING IPWIRELESS_PCCARD_NAME
  1028. ": no valid memreg_tx value - 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)
  1041. {
  1042. struct ipw_dev *ipw = dev_id;
  1043. if (ipw->hardware->hw_version == HW_VERSION_1)
  1044. return ipwireless_handle_v1_interrupt(irq, ipw->hardware);
  1045. else
  1046. return ipwireless_handle_v2_v3_interrupt(irq, ipw->hardware);
  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->lock, flags);
  1053. priority_limit = get_current_packet_priority(hw);
  1054. spin_unlock_irqrestore(&hw->lock, 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->lock, flags);
  1062. list_add_tail(&packet->queue, &hw->tx_queue[priority]);
  1063. hw->tx_queued++;
  1064. spin_unlock_irqrestore(&hw->lock, 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. const 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, (channel_idx + 1),
  1110. TL_PROTOCOLID_COM_DATA);
  1111. if (!packet)
  1112. return -ENOMEM;
  1113. packet->packet_callback = callback;
  1114. packet->callback_data = callback_data;
  1115. memcpy((unsigned char *) packet + sizeof(struct ipw_tx_packet), data,
  1116. length);
  1117. send_packet(hw, PRIO_DATA, packet);
  1118. return 0;
  1119. }
  1120. static int set_control_line(struct ipw_hardware *hw, int prio,
  1121. unsigned int channel_idx, int line, int state)
  1122. {
  1123. struct ipw_control_packet *packet;
  1124. int protocolid = TL_PROTOCOLID_COM_CTRL;
  1125. if (prio == PRIO_SETUP)
  1126. protocolid = TL_PROTOCOLID_SETUP;
  1127. packet = alloc_ctrl_packet(sizeof(struct ipw_control_packet),
  1128. (channel_idx + 1), protocolid, line);
  1129. if (!packet)
  1130. return -ENOMEM;
  1131. packet->header.length = sizeof(struct ipw_control_packet_body);
  1132. packet->body.value = (state == 0 ? 0 : 1);
  1133. send_packet(hw, prio, &packet->header);
  1134. return 0;
  1135. }
  1136. static int set_DTR(struct ipw_hardware *hw, int priority,
  1137. unsigned int channel_idx, int state)
  1138. {
  1139. if (state != 0)
  1140. hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_DTR;
  1141. else
  1142. hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_DTR;
  1143. return set_control_line(hw, priority, channel_idx, COMCTRL_DTR, state);
  1144. }
  1145. static int set_RTS(struct ipw_hardware *hw, int priority,
  1146. unsigned int channel_idx, int state)
  1147. {
  1148. if (state != 0)
  1149. hw->control_lines[channel_idx] |= IPW_CONTROL_LINE_RTS;
  1150. else
  1151. hw->control_lines[channel_idx] &= ~IPW_CONTROL_LINE_RTS;
  1152. return set_control_line(hw, priority, channel_idx, COMCTRL_RTS, state);
  1153. }
  1154. int ipwireless_set_DTR(struct ipw_hardware *hw, unsigned int channel_idx,
  1155. int state)
  1156. {
  1157. return set_DTR(hw, PRIO_CTRL, channel_idx, state);
  1158. }
  1159. int ipwireless_set_RTS(struct ipw_hardware *hw, unsigned int channel_idx,
  1160. int state)
  1161. {
  1162. return set_RTS(hw, PRIO_CTRL, channel_idx, state);
  1163. }
  1164. struct ipw_setup_get_version_query_packet {
  1165. struct ipw_tx_packet header;
  1166. struct tl_setup_get_version_qry body;
  1167. };
  1168. struct ipw_setup_config_packet {
  1169. struct ipw_tx_packet header;
  1170. struct tl_setup_config_msg body;
  1171. };
  1172. struct ipw_setup_config_done_packet {
  1173. struct ipw_tx_packet header;
  1174. struct tl_setup_config_done_msg body;
  1175. };
  1176. struct ipw_setup_open_packet {
  1177. struct ipw_tx_packet header;
  1178. struct tl_setup_open_msg body;
  1179. };
  1180. struct ipw_setup_info_packet {
  1181. struct ipw_tx_packet header;
  1182. struct tl_setup_info_msg body;
  1183. };
  1184. struct ipw_setup_reboot_msg_ack {
  1185. struct ipw_tx_packet header;
  1186. struct TlSetupRebootMsgAck body;
  1187. };
  1188. /* This handles the actual initialization of the card */
  1189. static void __handle_setup_get_version_rsp(struct ipw_hardware *hw)
  1190. {
  1191. struct ipw_setup_config_packet *config_packet;
  1192. struct ipw_setup_config_done_packet *config_done_packet;
  1193. struct ipw_setup_open_packet *open_packet;
  1194. struct ipw_setup_info_packet *info_packet;
  1195. int port;
  1196. unsigned int channel_idx;
  1197. /* generate config packet */
  1198. for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
  1199. config_packet = alloc_ctrl_packet(
  1200. sizeof(struct ipw_setup_config_packet),
  1201. ADDR_SETUP_PROT,
  1202. TL_PROTOCOLID_SETUP,
  1203. TL_SETUP_SIGNO_CONFIG_MSG);
  1204. if (!config_packet)
  1205. goto exit_nomem;
  1206. config_packet->header.length = sizeof(struct tl_setup_config_msg);
  1207. config_packet->body.port_no = port;
  1208. config_packet->body.prio_data = PRIO_DATA;
  1209. config_packet->body.prio_ctrl = PRIO_CTRL;
  1210. send_packet(hw, PRIO_SETUP, &config_packet->header);
  1211. }
  1212. config_done_packet = alloc_ctrl_packet(
  1213. sizeof(struct ipw_setup_config_done_packet),
  1214. ADDR_SETUP_PROT,
  1215. TL_PROTOCOLID_SETUP,
  1216. TL_SETUP_SIGNO_CONFIG_DONE_MSG);
  1217. if (!config_done_packet)
  1218. goto exit_nomem;
  1219. config_done_packet->header.length = sizeof(struct tl_setup_config_done_msg);
  1220. send_packet(hw, PRIO_SETUP, &config_done_packet->header);
  1221. /* generate open packet */
  1222. for (port = 1; port <= NL_NUM_OF_ADDRESSES; port++) {
  1223. open_packet = alloc_ctrl_packet(
  1224. sizeof(struct ipw_setup_open_packet),
  1225. ADDR_SETUP_PROT,
  1226. TL_PROTOCOLID_SETUP,
  1227. TL_SETUP_SIGNO_OPEN_MSG);
  1228. if (!open_packet)
  1229. goto exit_nomem;
  1230. open_packet->header.length = sizeof(struct tl_setup_open_msg);
  1231. open_packet->body.port_no = port;
  1232. send_packet(hw, PRIO_SETUP, &open_packet->header);
  1233. }
  1234. for (channel_idx = 0;
  1235. channel_idx < NL_NUM_OF_ADDRESSES; channel_idx++) {
  1236. int ret;
  1237. ret = set_DTR(hw, PRIO_SETUP, channel_idx,
  1238. (hw->control_lines[channel_idx] &
  1239. IPW_CONTROL_LINE_DTR) != 0);
  1240. if (ret) {
  1241. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  1242. ": error setting DTR (%d)\n", ret);
  1243. return;
  1244. }
  1245. ret = set_RTS(hw, PRIO_SETUP, channel_idx,
  1246. (hw->control_lines [channel_idx] &
  1247. IPW_CONTROL_LINE_RTS) != 0);
  1248. if (ret) {
  1249. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  1250. ": error setting RTS (%d)\n", ret);
  1251. return;
  1252. }
  1253. }
  1254. /*
  1255. * For NDIS we assume that we are using sync PPP frames, for COM async.
  1256. * This driver uses NDIS mode too. We don't bother with translation
  1257. * from async -> sync PPP.
  1258. */
  1259. info_packet = alloc_ctrl_packet(sizeof(struct ipw_setup_info_packet),
  1260. ADDR_SETUP_PROT,
  1261. TL_PROTOCOLID_SETUP,
  1262. TL_SETUP_SIGNO_INFO_MSG);
  1263. if (!info_packet)
  1264. goto exit_nomem;
  1265. info_packet->header.length = sizeof(struct tl_setup_info_msg);
  1266. info_packet->body.driver_type = NDISWAN_DRIVER;
  1267. info_packet->body.major_version = NDISWAN_DRIVER_MAJOR_VERSION;
  1268. info_packet->body.minor_version = NDISWAN_DRIVER_MINOR_VERSION;
  1269. send_packet(hw, PRIO_SETUP, &info_packet->header);
  1270. /* Initialization is now complete, so we clear the 'to_setup' flag */
  1271. hw->to_setup = 0;
  1272. return;
  1273. exit_nomem:
  1274. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  1275. ": not enough memory to alloc control packet\n");
  1276. hw->to_setup = -1;
  1277. }
  1278. static void handle_setup_get_version_rsp(struct ipw_hardware *hw,
  1279. unsigned char vers_no)
  1280. {
  1281. del_timer(&hw->setup_timer);
  1282. hw->initializing = 0;
  1283. printk(KERN_INFO IPWIRELESS_PCCARD_NAME ": card is ready.\n");
  1284. if (vers_no == TL_SETUP_VERSION)
  1285. __handle_setup_get_version_rsp(hw);
  1286. else
  1287. printk(KERN_ERR IPWIRELESS_PCCARD_NAME
  1288. ": invalid hardware version no %u\n",
  1289. (unsigned int) vers_no);
  1290. }
  1291. static void ipw_send_setup_packet(struct ipw_hardware *hw)
  1292. {
  1293. struct ipw_setup_get_version_query_packet *ver_packet;
  1294. ver_packet = alloc_ctrl_packet(
  1295. sizeof(struct ipw_setup_get_version_query_packet),
  1296. ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
  1297. TL_SETUP_SIGNO_GET_VERSION_QRY);
  1298. ver_packet->header.length = sizeof(struct tl_setup_get_version_qry);
  1299. /*
  1300. * Response is handled in handle_received_SETUP_packet
  1301. */
  1302. send_packet(hw, PRIO_SETUP, &ver_packet->header);
  1303. }
  1304. static void handle_received_SETUP_packet(struct ipw_hardware *hw,
  1305. unsigned int address,
  1306. const unsigned char *data, int len,
  1307. int is_last)
  1308. {
  1309. const union ipw_setup_rx_msg *rx_msg = (const union ipw_setup_rx_msg *) data;
  1310. if (address != ADDR_SETUP_PROT) {
  1311. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  1312. ": setup packet has bad address %d\n", address);
  1313. return;
  1314. }
  1315. switch (rx_msg->sig_no) {
  1316. case TL_SETUP_SIGNO_GET_VERSION_RSP:
  1317. if (hw->to_setup)
  1318. handle_setup_get_version_rsp(hw,
  1319. rx_msg->version_rsp_msg.version);
  1320. break;
  1321. case TL_SETUP_SIGNO_OPEN_MSG:
  1322. if (ipwireless_debug) {
  1323. unsigned int channel_idx = rx_msg->open_msg.port_no - 1;
  1324. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  1325. ": OPEN_MSG [channel %u] reply received\n",
  1326. channel_idx);
  1327. }
  1328. break;
  1329. case TL_SETUP_SIGNO_INFO_MSG_ACK:
  1330. if (ipwireless_debug)
  1331. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
  1332. ": card successfully configured as NDISWAN\n");
  1333. break;
  1334. case TL_SETUP_SIGNO_REBOOT_MSG:
  1335. if (hw->to_setup)
  1336. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
  1337. ": Setup not completed - ignoring reboot msg\n");
  1338. else {
  1339. struct ipw_setup_reboot_msg_ack *packet;
  1340. printk(KERN_DEBUG IPWIRELESS_PCCARD_NAME
  1341. ": Acknowledging REBOOT message\n");
  1342. packet = alloc_ctrl_packet(
  1343. sizeof(struct ipw_setup_reboot_msg_ack),
  1344. ADDR_SETUP_PROT, TL_PROTOCOLID_SETUP,
  1345. TL_SETUP_SIGNO_REBOOT_MSG_ACK);
  1346. if (!packet) {
  1347. pr_err(IPWIRELESS_PCCARD_NAME
  1348. ": Not enough memory to send reboot packet");
  1349. break;
  1350. }
  1351. packet->header.length =
  1352. sizeof(struct TlSetupRebootMsgAck);
  1353. send_packet(hw, PRIO_SETUP, &packet->header);
  1354. if (hw->reboot_callback)
  1355. hw->reboot_callback(hw->reboot_callback_data);
  1356. }
  1357. break;
  1358. default:
  1359. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  1360. ": unknown setup message %u received\n",
  1361. (unsigned int) rx_msg->sig_no);
  1362. }
  1363. }
  1364. static void do_close_hardware(struct ipw_hardware *hw)
  1365. {
  1366. unsigned int irqn;
  1367. if (hw->hw_version == HW_VERSION_1) {
  1368. /* Disable TX and RX interrupts. */
  1369. outw(0, hw->base_port + IOIER);
  1370. /* Acknowledge any outstanding interrupt requests */
  1371. irqn = inw(hw->base_port + IOIR);
  1372. if (irqn & IR_TXINTR)
  1373. outw(IR_TXINTR, hw->base_port + IOIR);
  1374. if (irqn & IR_RXINTR)
  1375. outw(IR_RXINTR, hw->base_port + IOIR);
  1376. synchronize_irq(hw->irq);
  1377. }
  1378. }
  1379. struct ipw_hardware *ipwireless_hardware_create(void)
  1380. {
  1381. int i;
  1382. struct ipw_hardware *hw =
  1383. kzalloc(sizeof(struct ipw_hardware), GFP_KERNEL);
  1384. if (!hw)
  1385. return NULL;
  1386. hw->irq = -1;
  1387. hw->initializing = 1;
  1388. hw->tx_ready = 1;
  1389. hw->rx_bytes_queued = 0;
  1390. hw->rx_pool_size = 0;
  1391. hw->last_memtx_serial = (unsigned short) 0xffff;
  1392. for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
  1393. INIT_LIST_HEAD(&hw->tx_queue[i]);
  1394. INIT_LIST_HEAD(&hw->rx_queue);
  1395. INIT_LIST_HEAD(&hw->rx_pool);
  1396. spin_lock_init(&hw->lock);
  1397. tasklet_init(&hw->tasklet, ipwireless_do_tasklet, (unsigned long) hw);
  1398. INIT_WORK(&hw->work_rx, ipw_receive_data_work);
  1399. setup_timer(&hw->setup_timer, ipwireless_setup_timer,
  1400. (unsigned long) hw);
  1401. return hw;
  1402. }
  1403. void ipwireless_init_hardware_v1(struct ipw_hardware *hw,
  1404. unsigned int base_port,
  1405. void __iomem *attr_memory,
  1406. void __iomem *common_memory,
  1407. int is_v2_card,
  1408. void (*reboot_callback) (void *data),
  1409. void *reboot_callback_data)
  1410. {
  1411. if (hw->removed) {
  1412. hw->removed = 0;
  1413. enable_irq(hw->irq);
  1414. }
  1415. hw->base_port = base_port;
  1416. hw->hw_version = (is_v2_card ? HW_VERSION_2 : HW_VERSION_1);
  1417. hw->ll_mtu = (hw->hw_version == HW_VERSION_1 ? LL_MTU_V1 : LL_MTU_V2);
  1418. hw->memregs_CCR = (struct MEMCCR __iomem *)
  1419. ((unsigned short __iomem *) attr_memory + 0x200);
  1420. hw->memory_info_regs = (struct MEMINFREG __iomem *) common_memory;
  1421. hw->memreg_tx = &hw->memory_info_regs->memreg_tx_new;
  1422. hw->reboot_callback = reboot_callback;
  1423. hw->reboot_callback_data = reboot_callback_data;
  1424. }
  1425. void ipwireless_init_hardware_v2_v3(struct ipw_hardware *hw)
  1426. {
  1427. hw->initializing = 1;
  1428. hw->init_loops = 0;
  1429. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  1430. ": waiting for card to start up...\n");
  1431. ipwireless_setup_timer((unsigned long) hw);
  1432. }
  1433. static void ipwireless_setup_timer(unsigned long data)
  1434. {
  1435. struct ipw_hardware *hw = (struct ipw_hardware *) data;
  1436. hw->init_loops++;
  1437. if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY &&
  1438. hw->hw_version == HW_VERSION_2 &&
  1439. hw->memreg_tx == &hw->memory_info_regs->memreg_tx_new) {
  1440. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  1441. ": failed to startup using TX2, trying TX\n");
  1442. hw->memreg_tx = &hw->memory_info_regs->memreg_tx_old;
  1443. hw->init_loops = 0;
  1444. }
  1445. /* Give up after a certain number of retries */
  1446. if (hw->init_loops == TL_SETUP_MAX_VERSION_QRY) {
  1447. printk(KERN_INFO IPWIRELESS_PCCARD_NAME
  1448. ": card failed to start up!\n");
  1449. hw->initializing = 0;
  1450. } else {
  1451. /* Do not attempt to write to the board if it is not present. */
  1452. if (is_card_present(hw)) {
  1453. unsigned long flags;
  1454. spin_lock_irqsave(&hw->lock, flags);
  1455. hw->to_setup = 1;
  1456. hw->tx_ready = 1;
  1457. spin_unlock_irqrestore(&hw->lock, flags);
  1458. tasklet_schedule(&hw->tasklet);
  1459. }
  1460. mod_timer(&hw->setup_timer,
  1461. jiffies + msecs_to_jiffies(TL_SETUP_VERSION_QRY_TMO));
  1462. }
  1463. }
  1464. /*
  1465. * Stop any interrupts from executing so that, once this function returns,
  1466. * other layers of the driver can be sure they won't get any more callbacks.
  1467. * Thus must be called on a proper process context.
  1468. */
  1469. void ipwireless_stop_interrupts(struct ipw_hardware *hw)
  1470. {
  1471. if (!hw->shutting_down) {
  1472. /* Tell everyone we are going down. */
  1473. hw->shutting_down = 1;
  1474. del_timer(&hw->setup_timer);
  1475. /* Prevent the hardware from sending any more interrupts */
  1476. do_close_hardware(hw);
  1477. }
  1478. }
  1479. void ipwireless_hardware_free(struct ipw_hardware *hw)
  1480. {
  1481. int i;
  1482. struct ipw_rx_packet *rp, *rq;
  1483. struct ipw_tx_packet *tp, *tq;
  1484. ipwireless_stop_interrupts(hw);
  1485. flush_work(&hw->work_rx);
  1486. for (i = 0; i < NL_NUM_OF_ADDRESSES; i++)
  1487. kfree(hw->packet_assembler[i]);
  1488. for (i = 0; i < NL_NUM_OF_PRIORITIES; i++)
  1489. list_for_each_entry_safe(tp, tq, &hw->tx_queue[i], queue) {
  1490. list_del(&tp->queue);
  1491. kfree(tp);
  1492. }
  1493. list_for_each_entry_safe(rp, rq, &hw->rx_queue, queue) {
  1494. list_del(&rp->queue);
  1495. kfree(rp);
  1496. }
  1497. list_for_each_entry_safe(rp, rq, &hw->rx_pool, queue) {
  1498. list_del(&rp->queue);
  1499. kfree(rp);
  1500. }
  1501. kfree(hw);
  1502. }
  1503. /*
  1504. * Associate the specified network with this hardware, so it will receive events
  1505. * from it.
  1506. */
  1507. void ipwireless_associate_network(struct ipw_hardware *hw,
  1508. struct ipw_network *network)
  1509. {
  1510. hw->network = network;
  1511. }