hardware.c 46 KB

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