hardware.c 45 KB

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