hardware.c 45 KB

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