hardware.c 46 KB

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