hardware.c 46 KB

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