xref: /linux/drivers/firewire/core-transaction.c (revision f879306834818ebd1722a4372079610cdd466fec)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Core IEEE1394 transaction logic
4  *
5  * Copyright (C) 2004-2006 Kristian Hoegsberg <krh@bitplanet.net>
6  */
7 
8 #include <linux/bug.h>
9 #include <linux/completion.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/firewire.h>
13 #include <linux/firewire-constants.h>
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/idr.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/rculist.h>
22 #include <linux/slab.h>
23 #include <linux/spinlock.h>
24 #include <linux/string.h>
25 #include <linux/timer.h>
26 #include <linux/types.h>
27 #include <linux/workqueue.h>
28 
29 #include <asm/byteorder.h>
30 
31 #include "core.h"
32 #include "packet-header-definitions.h"
33 #include "phy-packet-definitions.h"
34 #include <trace/events/firewire.h>
35 
36 #define HEADER_DESTINATION_IS_BROADCAST(header) \
37 	((async_header_get_destination(header) & 0x3f) == 0x3f)
38 
39 /* returns 0 if the split timeout handler is already running */
40 static int try_cancel_split_timeout(struct fw_transaction *t)
41 {
42 	if (t->is_split_transaction)
43 		return del_timer(&t->split_timeout_timer);
44 	else
45 		return 1;
46 }
47 
48 static int close_transaction(struct fw_transaction *transaction, struct fw_card *card, int rcode,
49 			     u32 response_tstamp)
50 {
51 	struct fw_transaction *t = NULL, *iter;
52 	unsigned long flags;
53 
54 	spin_lock_irqsave(&card->lock, flags);
55 	list_for_each_entry(iter, &card->transaction_list, link) {
56 		if (iter == transaction) {
57 			if (!try_cancel_split_timeout(iter)) {
58 				spin_unlock_irqrestore(&card->lock, flags);
59 				goto timed_out;
60 			}
61 			list_del_init(&iter->link);
62 			card->tlabel_mask &= ~(1ULL << iter->tlabel);
63 			t = iter;
64 			break;
65 		}
66 	}
67 	spin_unlock_irqrestore(&card->lock, flags);
68 
69 	if (t) {
70 		if (!t->with_tstamp) {
71 			t->callback.without_tstamp(card, rcode, NULL, 0, t->callback_data);
72 		} else {
73 			t->callback.with_tstamp(card, rcode, t->packet.timestamp, response_tstamp,
74 						NULL, 0, t->callback_data);
75 		}
76 		return 0;
77 	}
78 
79  timed_out:
80 	return -ENOENT;
81 }
82 
83 /*
84  * Only valid for transactions that are potentially pending (ie have
85  * been sent).
86  */
87 int fw_cancel_transaction(struct fw_card *card,
88 			  struct fw_transaction *transaction)
89 {
90 	u32 tstamp;
91 
92 	/*
93 	 * Cancel the packet transmission if it's still queued.  That
94 	 * will call the packet transmission callback which cancels
95 	 * the transaction.
96 	 */
97 
98 	if (card->driver->cancel_packet(card, &transaction->packet) == 0)
99 		return 0;
100 
101 	/*
102 	 * If the request packet has already been sent, we need to see
103 	 * if the transaction is still pending and remove it in that case.
104 	 */
105 
106 	if (transaction->packet.ack == 0) {
107 		// The timestamp is reused since it was just read now.
108 		tstamp = transaction->packet.timestamp;
109 	} else {
110 		u32 curr_cycle_time = 0;
111 
112 		(void)fw_card_read_cycle_time(card, &curr_cycle_time);
113 		tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time);
114 	}
115 
116 	return close_transaction(transaction, card, RCODE_CANCELLED, tstamp);
117 }
118 EXPORT_SYMBOL(fw_cancel_transaction);
119 
120 static void split_transaction_timeout_callback(struct timer_list *timer)
121 {
122 	struct fw_transaction *t = from_timer(t, timer, split_timeout_timer);
123 	struct fw_card *card = t->card;
124 	unsigned long flags;
125 
126 	spin_lock_irqsave(&card->lock, flags);
127 	if (list_empty(&t->link)) {
128 		spin_unlock_irqrestore(&card->lock, flags);
129 		return;
130 	}
131 	list_del(&t->link);
132 	card->tlabel_mask &= ~(1ULL << t->tlabel);
133 	spin_unlock_irqrestore(&card->lock, flags);
134 
135 	if (!t->with_tstamp) {
136 		t->callback.without_tstamp(card, RCODE_CANCELLED, NULL, 0, t->callback_data);
137 	} else {
138 		t->callback.with_tstamp(card, RCODE_CANCELLED, t->packet.timestamp,
139 					t->split_timeout_cycle, NULL, 0, t->callback_data);
140 	}
141 }
142 
143 static void start_split_transaction_timeout(struct fw_transaction *t,
144 					    struct fw_card *card)
145 {
146 	unsigned long flags;
147 
148 	spin_lock_irqsave(&card->lock, flags);
149 
150 	if (list_empty(&t->link) || WARN_ON(t->is_split_transaction)) {
151 		spin_unlock_irqrestore(&card->lock, flags);
152 		return;
153 	}
154 
155 	t->is_split_transaction = true;
156 	mod_timer(&t->split_timeout_timer,
157 		  jiffies + card->split_timeout_jiffies);
158 
159 	spin_unlock_irqrestore(&card->lock, flags);
160 }
161 
162 static u32 compute_split_timeout_timestamp(struct fw_card *card, u32 request_timestamp);
163 
164 static void transmit_complete_callback(struct fw_packet *packet,
165 				       struct fw_card *card, int status)
166 {
167 	struct fw_transaction *t =
168 	    container_of(packet, struct fw_transaction, packet);
169 
170 	trace_async_request_outbound_complete((uintptr_t)t, card->index, packet->generation,
171 					      packet->speed, status, packet->timestamp);
172 
173 	switch (status) {
174 	case ACK_COMPLETE:
175 		close_transaction(t, card, RCODE_COMPLETE, packet->timestamp);
176 		break;
177 	case ACK_PENDING:
178 	{
179 		t->split_timeout_cycle =
180 			compute_split_timeout_timestamp(card, packet->timestamp) & 0xffff;
181 		start_split_transaction_timeout(t, card);
182 		break;
183 	}
184 	case ACK_BUSY_X:
185 	case ACK_BUSY_A:
186 	case ACK_BUSY_B:
187 		close_transaction(t, card, RCODE_BUSY, packet->timestamp);
188 		break;
189 	case ACK_DATA_ERROR:
190 		close_transaction(t, card, RCODE_DATA_ERROR, packet->timestamp);
191 		break;
192 	case ACK_TYPE_ERROR:
193 		close_transaction(t, card, RCODE_TYPE_ERROR, packet->timestamp);
194 		break;
195 	default:
196 		/*
197 		 * In this case the ack is really a juju specific
198 		 * rcode, so just forward that to the callback.
199 		 */
200 		close_transaction(t, card, status, packet->timestamp);
201 		break;
202 	}
203 }
204 
205 static void fw_fill_request(struct fw_packet *packet, int tcode, int tlabel,
206 		int destination_id, int source_id, int generation, int speed,
207 		unsigned long long offset, void *payload, size_t length)
208 {
209 	int ext_tcode;
210 
211 	if (tcode == TCODE_STREAM_DATA) {
212 		// The value of destination_id argument should include tag, channel, and sy fields
213 		// as isochronous packet header has.
214 		packet->header[0] = destination_id;
215 		isoc_header_set_data_length(packet->header, length);
216 		isoc_header_set_tcode(packet->header, TCODE_STREAM_DATA);
217 		packet->header_length = 4;
218 		packet->payload = payload;
219 		packet->payload_length = length;
220 
221 		goto common;
222 	}
223 
224 	if (tcode > 0x10) {
225 		ext_tcode = tcode & ~0x10;
226 		tcode = TCODE_LOCK_REQUEST;
227 	} else
228 		ext_tcode = 0;
229 
230 	async_header_set_retry(packet->header, RETRY_X);
231 	async_header_set_tlabel(packet->header, tlabel);
232 	async_header_set_tcode(packet->header, tcode);
233 	async_header_set_destination(packet->header, destination_id);
234 	async_header_set_source(packet->header, source_id);
235 	async_header_set_offset(packet->header, offset);
236 
237 	switch (tcode) {
238 	case TCODE_WRITE_QUADLET_REQUEST:
239 		async_header_set_quadlet_data(packet->header, *(u32 *)payload);
240 		packet->header_length = 16;
241 		packet->payload_length = 0;
242 		break;
243 
244 	case TCODE_LOCK_REQUEST:
245 	case TCODE_WRITE_BLOCK_REQUEST:
246 		async_header_set_data_length(packet->header, length);
247 		async_header_set_extended_tcode(packet->header, ext_tcode);
248 		packet->header_length = 16;
249 		packet->payload = payload;
250 		packet->payload_length = length;
251 		break;
252 
253 	case TCODE_READ_QUADLET_REQUEST:
254 		packet->header_length = 12;
255 		packet->payload_length = 0;
256 		break;
257 
258 	case TCODE_READ_BLOCK_REQUEST:
259 		async_header_set_data_length(packet->header, length);
260 		async_header_set_extended_tcode(packet->header, ext_tcode);
261 		packet->header_length = 16;
262 		packet->payload_length = 0;
263 		break;
264 
265 	default:
266 		WARN(1, "wrong tcode %d\n", tcode);
267 	}
268  common:
269 	packet->speed = speed;
270 	packet->generation = generation;
271 	packet->ack = 0;
272 	packet->payload_mapped = false;
273 }
274 
275 static int allocate_tlabel(struct fw_card *card)
276 {
277 	int tlabel;
278 
279 	tlabel = card->current_tlabel;
280 	while (card->tlabel_mask & (1ULL << tlabel)) {
281 		tlabel = (tlabel + 1) & 0x3f;
282 		if (tlabel == card->current_tlabel)
283 			return -EBUSY;
284 	}
285 
286 	card->current_tlabel = (tlabel + 1) & 0x3f;
287 	card->tlabel_mask |= 1ULL << tlabel;
288 
289 	return tlabel;
290 }
291 
292 /**
293  * __fw_send_request() - submit a request packet for transmission to generate callback for response
294  *			 subaction with or without time stamp.
295  * @card:		interface to send the request at
296  * @t:			transaction instance to which the request belongs
297  * @tcode:		transaction code
298  * @destination_id:	destination node ID, consisting of bus_ID and phy_ID
299  * @generation:		bus generation in which request and response are valid
300  * @speed:		transmission speed
301  * @offset:		48bit wide offset into destination's address space
302  * @payload:		data payload for the request subaction
303  * @length:		length of the payload, in bytes
304  * @callback:		union of two functions whether to receive time stamp or not for response
305  *			subaction.
306  * @with_tstamp:	Whether to receive time stamp or not for response subaction.
307  * @callback_data:	data to be passed to the transaction completion callback
308  *
309  * Submit a request packet into the asynchronous request transmission queue.
310  * Can be called from atomic context.  If you prefer a blocking API, use
311  * fw_run_transaction() in a context that can sleep.
312  *
313  * In case of lock requests, specify one of the firewire-core specific %TCODE_
314  * constants instead of %TCODE_LOCK_REQUEST in @tcode.
315  *
316  * Make sure that the value in @destination_id is not older than the one in
317  * @generation.  Otherwise the request is in danger to be sent to a wrong node.
318  *
319  * In case of asynchronous stream packets i.e. %TCODE_STREAM_DATA, the caller
320  * needs to synthesize @destination_id with fw_stream_packet_destination_id().
321  * It will contain tag, channel, and sy data instead of a node ID then.
322  *
323  * The payload buffer at @data is going to be DMA-mapped except in case of
324  * @length <= 8 or of local (loopback) requests.  Hence make sure that the
325  * buffer complies with the restrictions of the streaming DMA mapping API.
326  * @payload must not be freed before the @callback is called.
327  *
328  * In case of request types without payload, @data is NULL and @length is 0.
329  *
330  * After the transaction is completed successfully or unsuccessfully, the
331  * @callback will be called.  Among its parameters is the response code which
332  * is either one of the rcodes per IEEE 1394 or, in case of internal errors,
333  * the firewire-core specific %RCODE_SEND_ERROR.  The other firewire-core
334  * specific rcodes (%RCODE_CANCELLED, %RCODE_BUSY, %RCODE_GENERATION,
335  * %RCODE_NO_ACK) denote transaction timeout, busy responder, stale request
336  * generation, or missing ACK respectively.
337  *
338  * Note some timing corner cases:  fw_send_request() may complete much earlier
339  * than when the request packet actually hits the wire.  On the other hand,
340  * transaction completion and hence execution of @callback may happen even
341  * before fw_send_request() returns.
342  */
343 void __fw_send_request(struct fw_card *card, struct fw_transaction *t, int tcode,
344 		int destination_id, int generation, int speed, unsigned long long offset,
345 		void *payload, size_t length, union fw_transaction_callback callback,
346 		bool with_tstamp, void *callback_data)
347 {
348 	unsigned long flags;
349 	int tlabel;
350 
351 	/*
352 	 * Allocate tlabel from the bitmap and put the transaction on
353 	 * the list while holding the card spinlock.
354 	 */
355 
356 	spin_lock_irqsave(&card->lock, flags);
357 
358 	tlabel = allocate_tlabel(card);
359 	if (tlabel < 0) {
360 		spin_unlock_irqrestore(&card->lock, flags);
361 		if (!with_tstamp) {
362 			callback.without_tstamp(card, RCODE_SEND_ERROR, NULL, 0, callback_data);
363 		} else {
364 			// Timestamping on behalf of hardware.
365 			u32 curr_cycle_time = 0;
366 			u32 tstamp;
367 
368 			(void)fw_card_read_cycle_time(card, &curr_cycle_time);
369 			tstamp = cycle_time_to_ohci_tstamp(curr_cycle_time);
370 
371 			callback.with_tstamp(card, RCODE_SEND_ERROR, tstamp, tstamp, NULL, 0,
372 					     callback_data);
373 		}
374 		return;
375 	}
376 
377 	t->node_id = destination_id;
378 	t->tlabel = tlabel;
379 	t->card = card;
380 	t->is_split_transaction = false;
381 	timer_setup(&t->split_timeout_timer, split_transaction_timeout_callback, 0);
382 	t->callback = callback;
383 	t->with_tstamp = with_tstamp;
384 	t->callback_data = callback_data;
385 
386 	fw_fill_request(&t->packet, tcode, t->tlabel, destination_id, card->node_id, generation,
387 			speed, offset, payload, length);
388 	t->packet.callback = transmit_complete_callback;
389 
390 	list_add_tail(&t->link, &card->transaction_list);
391 
392 	spin_unlock_irqrestore(&card->lock, flags);
393 
394 	trace_async_request_outbound_initiate((uintptr_t)t, card->index, generation, speed,
395 					      t->packet.header, payload,
396 					      tcode_is_read_request(tcode) ? 0 : length / 4);
397 
398 	card->driver->send_request(card, &t->packet);
399 }
400 EXPORT_SYMBOL_GPL(__fw_send_request);
401 
402 struct transaction_callback_data {
403 	struct completion done;
404 	void *payload;
405 	int rcode;
406 };
407 
408 static void transaction_callback(struct fw_card *card, int rcode,
409 				 void *payload, size_t length, void *data)
410 {
411 	struct transaction_callback_data *d = data;
412 
413 	if (rcode == RCODE_COMPLETE)
414 		memcpy(d->payload, payload, length);
415 	d->rcode = rcode;
416 	complete(&d->done);
417 }
418 
419 /**
420  * fw_run_transaction() - send request and sleep until transaction is completed
421  * @card:		card interface for this request
422  * @tcode:		transaction code
423  * @destination_id:	destination node ID, consisting of bus_ID and phy_ID
424  * @generation:		bus generation in which request and response are valid
425  * @speed:		transmission speed
426  * @offset:		48bit wide offset into destination's address space
427  * @payload:		data payload for the request subaction
428  * @length:		length of the payload, in bytes
429  *
430  * Returns the RCODE.  See fw_send_request() for parameter documentation.
431  * Unlike fw_send_request(), @data points to the payload of the request or/and
432  * to the payload of the response.  DMA mapping restrictions apply to outbound
433  * request payloads of >= 8 bytes but not to inbound response payloads.
434  */
435 int fw_run_transaction(struct fw_card *card, int tcode, int destination_id,
436 		       int generation, int speed, unsigned long long offset,
437 		       void *payload, size_t length)
438 {
439 	struct transaction_callback_data d;
440 	struct fw_transaction t;
441 
442 	timer_setup_on_stack(&t.split_timeout_timer, NULL, 0);
443 	init_completion(&d.done);
444 	d.payload = payload;
445 	fw_send_request(card, &t, tcode, destination_id, generation, speed,
446 			offset, payload, length, transaction_callback, &d);
447 	wait_for_completion(&d.done);
448 	destroy_timer_on_stack(&t.split_timeout_timer);
449 
450 	return d.rcode;
451 }
452 EXPORT_SYMBOL(fw_run_transaction);
453 
454 static DEFINE_MUTEX(phy_config_mutex);
455 static DECLARE_COMPLETION(phy_config_done);
456 
457 static void transmit_phy_packet_callback(struct fw_packet *packet,
458 					 struct fw_card *card, int status)
459 {
460 	trace_async_phy_outbound_complete((uintptr_t)packet, card->index, packet->generation, status,
461 					  packet->timestamp);
462 	complete(&phy_config_done);
463 }
464 
465 static struct fw_packet phy_config_packet = {
466 	.header_length	= 12,
467 	.header[0]	= TCODE_LINK_INTERNAL << 4,
468 	.payload_length	= 0,
469 	.speed		= SCODE_100,
470 	.callback	= transmit_phy_packet_callback,
471 };
472 
473 void fw_send_phy_config(struct fw_card *card,
474 			int node_id, int generation, int gap_count)
475 {
476 	long timeout = DIV_ROUND_UP(HZ, 10);
477 	u32 data = 0;
478 
479 	phy_packet_set_packet_identifier(&data, PHY_PACKET_PACKET_IDENTIFIER_PHY_CONFIG);
480 
481 	if (node_id != FW_PHY_CONFIG_NO_NODE_ID) {
482 		phy_packet_phy_config_set_root_id(&data, node_id);
483 		phy_packet_phy_config_set_force_root_node(&data, true);
484 	}
485 
486 	if (gap_count == FW_PHY_CONFIG_CURRENT_GAP_COUNT) {
487 		gap_count = card->driver->read_phy_reg(card, 1);
488 		if (gap_count < 0)
489 			return;
490 
491 		gap_count &= 63;
492 		if (gap_count == 63)
493 			return;
494 	}
495 	phy_packet_phy_config_set_gap_count(&data, gap_count);
496 	phy_packet_phy_config_set_gap_count_optimization(&data, true);
497 
498 	mutex_lock(&phy_config_mutex);
499 
500 	phy_config_packet.header[1] = data;
501 	phy_config_packet.header[2] = ~data;
502 	phy_config_packet.generation = generation;
503 	reinit_completion(&phy_config_done);
504 
505 	trace_async_phy_outbound_initiate((uintptr_t)&phy_config_packet, card->index,
506 					  phy_config_packet.generation, phy_config_packet.header[1],
507 					  phy_config_packet.header[2]);
508 
509 	card->driver->send_request(card, &phy_config_packet);
510 	wait_for_completion_timeout(&phy_config_done, timeout);
511 
512 	mutex_unlock(&phy_config_mutex);
513 }
514 
515 static struct fw_address_handler *lookup_overlapping_address_handler(
516 	struct list_head *list, unsigned long long offset, size_t length)
517 {
518 	struct fw_address_handler *handler;
519 
520 	list_for_each_entry_rcu(handler, list, link) {
521 		if (handler->offset < offset + length &&
522 		    offset < handler->offset + handler->length)
523 			return handler;
524 	}
525 
526 	return NULL;
527 }
528 
529 static bool is_enclosing_handler(struct fw_address_handler *handler,
530 				 unsigned long long offset, size_t length)
531 {
532 	return handler->offset <= offset &&
533 		offset + length <= handler->offset + handler->length;
534 }
535 
536 static struct fw_address_handler *lookup_enclosing_address_handler(
537 	struct list_head *list, unsigned long long offset, size_t length)
538 {
539 	struct fw_address_handler *handler;
540 
541 	list_for_each_entry_rcu(handler, list, link) {
542 		if (is_enclosing_handler(handler, offset, length))
543 			return handler;
544 	}
545 
546 	return NULL;
547 }
548 
549 static DEFINE_SPINLOCK(address_handler_list_lock);
550 static LIST_HEAD(address_handler_list);
551 
552 const struct fw_address_region fw_high_memory_region =
553 	{ .start = FW_MAX_PHYSICAL_RANGE, .end = 0xffffe0000000ULL, };
554 EXPORT_SYMBOL(fw_high_memory_region);
555 
556 static const struct fw_address_region low_memory_region =
557 	{ .start = 0x000000000000ULL, .end = FW_MAX_PHYSICAL_RANGE, };
558 
559 #if 0
560 const struct fw_address_region fw_private_region =
561 	{ .start = 0xffffe0000000ULL, .end = 0xfffff0000000ULL,  };
562 const struct fw_address_region fw_csr_region =
563 	{ .start = CSR_REGISTER_BASE,
564 	  .end   = CSR_REGISTER_BASE | CSR_CONFIG_ROM_END,  };
565 const struct fw_address_region fw_unit_space_region =
566 	{ .start = 0xfffff0000900ULL, .end = 0x1000000000000ULL, };
567 #endif  /*  0  */
568 
569 /**
570  * fw_core_add_address_handler() - register for incoming requests
571  * @handler:	callback
572  * @region:	region in the IEEE 1212 node space address range
573  *
574  * region->start, ->end, and handler->length have to be quadlet-aligned.
575  *
576  * When a request is received that falls within the specified address range,
577  * the specified callback is invoked.  The parameters passed to the callback
578  * give the details of the particular request.
579  *
580  * To be called in process context.
581  * Return value:  0 on success, non-zero otherwise.
582  *
583  * The start offset of the handler's address region is determined by
584  * fw_core_add_address_handler() and is returned in handler->offset.
585  *
586  * Address allocations are exclusive, except for the FCP registers.
587  */
588 int fw_core_add_address_handler(struct fw_address_handler *handler,
589 				const struct fw_address_region *region)
590 {
591 	struct fw_address_handler *other;
592 	int ret = -EBUSY;
593 
594 	if (region->start & 0xffff000000000003ULL ||
595 	    region->start >= region->end ||
596 	    region->end   > 0x0001000000000000ULL ||
597 	    handler->length & 3 ||
598 	    handler->length == 0)
599 		return -EINVAL;
600 
601 	spin_lock(&address_handler_list_lock);
602 
603 	handler->offset = region->start;
604 	while (handler->offset + handler->length <= region->end) {
605 		if (is_in_fcp_region(handler->offset, handler->length))
606 			other = NULL;
607 		else
608 			other = lookup_overlapping_address_handler
609 					(&address_handler_list,
610 					 handler->offset, handler->length);
611 		if (other != NULL) {
612 			handler->offset += other->length;
613 		} else {
614 			list_add_tail_rcu(&handler->link, &address_handler_list);
615 			ret = 0;
616 			break;
617 		}
618 	}
619 
620 	spin_unlock(&address_handler_list_lock);
621 
622 	return ret;
623 }
624 EXPORT_SYMBOL(fw_core_add_address_handler);
625 
626 /**
627  * fw_core_remove_address_handler() - unregister an address handler
628  * @handler: callback
629  *
630  * To be called in process context.
631  *
632  * When fw_core_remove_address_handler() returns, @handler->callback() is
633  * guaranteed to not run on any CPU anymore.
634  */
635 void fw_core_remove_address_handler(struct fw_address_handler *handler)
636 {
637 	spin_lock(&address_handler_list_lock);
638 	list_del_rcu(&handler->link);
639 	spin_unlock(&address_handler_list_lock);
640 	synchronize_rcu();
641 }
642 EXPORT_SYMBOL(fw_core_remove_address_handler);
643 
644 struct fw_request {
645 	struct kref kref;
646 	struct fw_packet response;
647 	u32 request_header[ASYNC_HEADER_QUADLET_COUNT];
648 	int ack;
649 	u32 timestamp;
650 	u32 length;
651 	u32 data[];
652 };
653 
654 void fw_request_get(struct fw_request *request)
655 {
656 	kref_get(&request->kref);
657 }
658 
659 static void release_request(struct kref *kref)
660 {
661 	struct fw_request *request = container_of(kref, struct fw_request, kref);
662 
663 	kfree(request);
664 }
665 
666 void fw_request_put(struct fw_request *request)
667 {
668 	kref_put(&request->kref, release_request);
669 }
670 
671 static void free_response_callback(struct fw_packet *packet,
672 				   struct fw_card *card, int status)
673 {
674 	struct fw_request *request = container_of(packet, struct fw_request, response);
675 
676 	trace_async_response_outbound_complete((uintptr_t)request, card->index, packet->generation,
677 					       packet->speed, status, packet->timestamp);
678 
679 	// Decrease the reference count since not at in-flight.
680 	fw_request_put(request);
681 
682 	// Decrease the reference count to release the object.
683 	fw_request_put(request);
684 }
685 
686 int fw_get_response_length(struct fw_request *r)
687 {
688 	int tcode, ext_tcode, data_length;
689 
690 	tcode = async_header_get_tcode(r->request_header);
691 
692 	switch (tcode) {
693 	case TCODE_WRITE_QUADLET_REQUEST:
694 	case TCODE_WRITE_BLOCK_REQUEST:
695 		return 0;
696 
697 	case TCODE_READ_QUADLET_REQUEST:
698 		return 4;
699 
700 	case TCODE_READ_BLOCK_REQUEST:
701 		data_length = async_header_get_data_length(r->request_header);
702 		return data_length;
703 
704 	case TCODE_LOCK_REQUEST:
705 		ext_tcode = async_header_get_extended_tcode(r->request_header);
706 		data_length = async_header_get_data_length(r->request_header);
707 		switch (ext_tcode) {
708 		case EXTCODE_FETCH_ADD:
709 		case EXTCODE_LITTLE_ADD:
710 			return data_length;
711 		default:
712 			return data_length / 2;
713 		}
714 
715 	default:
716 		WARN(1, "wrong tcode %d\n", tcode);
717 		return 0;
718 	}
719 }
720 
721 void fw_fill_response(struct fw_packet *response, u32 *request_header,
722 		      int rcode, void *payload, size_t length)
723 {
724 	int tcode, tlabel, extended_tcode, source, destination;
725 
726 	tcode = async_header_get_tcode(request_header);
727 	tlabel = async_header_get_tlabel(request_header);
728 	source = async_header_get_destination(request_header); // Exchange.
729 	destination = async_header_get_source(request_header); // Exchange.
730 	extended_tcode = async_header_get_extended_tcode(request_header);
731 
732 	async_header_set_retry(response->header, RETRY_1);
733 	async_header_set_tlabel(response->header, tlabel);
734 	async_header_set_destination(response->header, destination);
735 	async_header_set_source(response->header, source);
736 	async_header_set_rcode(response->header, rcode);
737 	response->header[2] = 0;	// The field is reserved.
738 
739 	switch (tcode) {
740 	case TCODE_WRITE_QUADLET_REQUEST:
741 	case TCODE_WRITE_BLOCK_REQUEST:
742 		async_header_set_tcode(response->header, TCODE_WRITE_RESPONSE);
743 		response->header_length = 12;
744 		response->payload_length = 0;
745 		break;
746 
747 	case TCODE_READ_QUADLET_REQUEST:
748 		async_header_set_tcode(response->header, TCODE_READ_QUADLET_RESPONSE);
749 		if (payload != NULL)
750 			async_header_set_quadlet_data(response->header, *(u32 *)payload);
751 		else
752 			async_header_set_quadlet_data(response->header, 0);
753 		response->header_length = 16;
754 		response->payload_length = 0;
755 		break;
756 
757 	case TCODE_READ_BLOCK_REQUEST:
758 	case TCODE_LOCK_REQUEST:
759 		async_header_set_tcode(response->header, tcode + 2);
760 		async_header_set_data_length(response->header, length);
761 		async_header_set_extended_tcode(response->header, extended_tcode);
762 		response->header_length = 16;
763 		response->payload = payload;
764 		response->payload_length = length;
765 		break;
766 
767 	default:
768 		WARN(1, "wrong tcode %d\n", tcode);
769 	}
770 
771 	response->payload_mapped = false;
772 }
773 EXPORT_SYMBOL(fw_fill_response);
774 
775 static u32 compute_split_timeout_timestamp(struct fw_card *card,
776 					   u32 request_timestamp)
777 {
778 	unsigned int cycles;
779 	u32 timestamp;
780 
781 	cycles = card->split_timeout_cycles;
782 	cycles += request_timestamp & 0x1fff;
783 
784 	timestamp = request_timestamp & ~0x1fff;
785 	timestamp += (cycles / 8000) << 13;
786 	timestamp |= cycles % 8000;
787 
788 	return timestamp;
789 }
790 
791 static struct fw_request *allocate_request(struct fw_card *card,
792 					   struct fw_packet *p)
793 {
794 	struct fw_request *request;
795 	u32 *data, length;
796 	int request_tcode;
797 
798 	request_tcode = async_header_get_tcode(p->header);
799 	switch (request_tcode) {
800 	case TCODE_WRITE_QUADLET_REQUEST:
801 		data = &p->header[3];
802 		length = 4;
803 		break;
804 
805 	case TCODE_WRITE_BLOCK_REQUEST:
806 	case TCODE_LOCK_REQUEST:
807 		data = p->payload;
808 		length = async_header_get_data_length(p->header);
809 		break;
810 
811 	case TCODE_READ_QUADLET_REQUEST:
812 		data = NULL;
813 		length = 4;
814 		break;
815 
816 	case TCODE_READ_BLOCK_REQUEST:
817 		data = NULL;
818 		length = async_header_get_data_length(p->header);
819 		break;
820 
821 	default:
822 		fw_notice(card, "ERROR - corrupt request received - %08x %08x %08x\n",
823 			 p->header[0], p->header[1], p->header[2]);
824 		return NULL;
825 	}
826 
827 	request = kmalloc(sizeof(*request) + length, GFP_ATOMIC);
828 	if (request == NULL)
829 		return NULL;
830 	kref_init(&request->kref);
831 
832 	request->response.speed = p->speed;
833 	request->response.timestamp =
834 			compute_split_timeout_timestamp(card, p->timestamp);
835 	request->response.generation = p->generation;
836 	request->response.ack = 0;
837 	request->response.callback = free_response_callback;
838 	request->ack = p->ack;
839 	request->timestamp = p->timestamp;
840 	request->length = length;
841 	if (data)
842 		memcpy(request->data, data, length);
843 
844 	memcpy(request->request_header, p->header, sizeof(p->header));
845 
846 	return request;
847 }
848 
849 /**
850  * fw_send_response: - send response packet for asynchronous transaction.
851  * @card:	interface to send the response at.
852  * @request:	firewire request data for the transaction.
853  * @rcode:	response code to send.
854  *
855  * Submit a response packet into the asynchronous response transmission queue. The @request
856  * is going to be released when the transmission successfully finishes later.
857  */
858 void fw_send_response(struct fw_card *card,
859 		      struct fw_request *request, int rcode)
860 {
861 	u32 *data = NULL;
862 	unsigned int data_length = 0;
863 
864 	/* unified transaction or broadcast transaction: don't respond */
865 	if (request->ack != ACK_PENDING ||
866 	    HEADER_DESTINATION_IS_BROADCAST(request->request_header)) {
867 		fw_request_put(request);
868 		return;
869 	}
870 
871 	if (rcode == RCODE_COMPLETE) {
872 		data = request->data;
873 		data_length = fw_get_response_length(request);
874 	}
875 
876 	fw_fill_response(&request->response, request->request_header, rcode, data, data_length);
877 
878 	// Increase the reference count so that the object is kept during in-flight.
879 	fw_request_get(request);
880 
881 	trace_async_response_outbound_initiate((uintptr_t)request, card->index,
882 					       request->response.generation, request->response.speed,
883 					       request->response.header, data,
884 					       data ? data_length / 4 : 0);
885 
886 	card->driver->send_response(card, &request->response);
887 }
888 EXPORT_SYMBOL(fw_send_response);
889 
890 /**
891  * fw_get_request_speed() - returns speed at which the @request was received
892  * @request: firewire request data
893  */
894 int fw_get_request_speed(struct fw_request *request)
895 {
896 	return request->response.speed;
897 }
898 EXPORT_SYMBOL(fw_get_request_speed);
899 
900 /**
901  * fw_request_get_timestamp: Get timestamp of the request.
902  * @request: The opaque pointer to request structure.
903  *
904  * Get timestamp when 1394 OHCI controller receives the asynchronous request subaction. The
905  * timestamp consists of the low order 3 bits of second field and the full 13 bits of count
906  * field of isochronous cycle time register.
907  *
908  * Returns: timestamp of the request.
909  */
910 u32 fw_request_get_timestamp(const struct fw_request *request)
911 {
912 	return request->timestamp;
913 }
914 EXPORT_SYMBOL_GPL(fw_request_get_timestamp);
915 
916 static void handle_exclusive_region_request(struct fw_card *card,
917 					    struct fw_packet *p,
918 					    struct fw_request *request,
919 					    unsigned long long offset)
920 {
921 	struct fw_address_handler *handler;
922 	int tcode, destination, source;
923 
924 	destination = async_header_get_destination(p->header);
925 	source = async_header_get_source(p->header);
926 	tcode = async_header_get_tcode(p->header);
927 	if (tcode == TCODE_LOCK_REQUEST)
928 		tcode = 0x10 + async_header_get_extended_tcode(p->header);
929 
930 	rcu_read_lock();
931 	handler = lookup_enclosing_address_handler(&address_handler_list,
932 						   offset, request->length);
933 	if (handler)
934 		handler->address_callback(card, request,
935 					  tcode, destination, source,
936 					  p->generation, offset,
937 					  request->data, request->length,
938 					  handler->callback_data);
939 	rcu_read_unlock();
940 
941 	if (!handler)
942 		fw_send_response(card, request, RCODE_ADDRESS_ERROR);
943 }
944 
945 static void handle_fcp_region_request(struct fw_card *card,
946 				      struct fw_packet *p,
947 				      struct fw_request *request,
948 				      unsigned long long offset)
949 {
950 	struct fw_address_handler *handler;
951 	int tcode, destination, source;
952 
953 	if ((offset != (CSR_REGISTER_BASE | CSR_FCP_COMMAND) &&
954 	     offset != (CSR_REGISTER_BASE | CSR_FCP_RESPONSE)) ||
955 	    request->length > 0x200) {
956 		fw_send_response(card, request, RCODE_ADDRESS_ERROR);
957 
958 		return;
959 	}
960 
961 	tcode = async_header_get_tcode(p->header);
962 	destination = async_header_get_destination(p->header);
963 	source = async_header_get_source(p->header);
964 
965 	if (tcode != TCODE_WRITE_QUADLET_REQUEST &&
966 	    tcode != TCODE_WRITE_BLOCK_REQUEST) {
967 		fw_send_response(card, request, RCODE_TYPE_ERROR);
968 
969 		return;
970 	}
971 
972 	rcu_read_lock();
973 	list_for_each_entry_rcu(handler, &address_handler_list, link) {
974 		if (is_enclosing_handler(handler, offset, request->length))
975 			handler->address_callback(card, request, tcode,
976 						  destination, source,
977 						  p->generation, offset,
978 						  request->data,
979 						  request->length,
980 						  handler->callback_data);
981 	}
982 	rcu_read_unlock();
983 
984 	fw_send_response(card, request, RCODE_COMPLETE);
985 }
986 
987 void fw_core_handle_request(struct fw_card *card, struct fw_packet *p)
988 {
989 	struct fw_request *request;
990 	unsigned long long offset;
991 	unsigned int tcode;
992 
993 	if (p->ack != ACK_PENDING && p->ack != ACK_COMPLETE)
994 		return;
995 
996 	tcode = async_header_get_tcode(p->header);
997 	if (tcode_is_link_internal(tcode)) {
998 		trace_async_phy_inbound((uintptr_t)p, card->index, p->generation, p->ack, p->timestamp,
999 					 p->header[1], p->header[2]);
1000 		fw_cdev_handle_phy_packet(card, p);
1001 		return;
1002 	}
1003 
1004 	request = allocate_request(card, p);
1005 	if (request == NULL) {
1006 		/* FIXME: send statically allocated busy packet. */
1007 		return;
1008 	}
1009 
1010 	trace_async_request_inbound((uintptr_t)request, card->index, p->generation, p->speed,
1011 				    p->ack, p->timestamp, p->header, request->data,
1012 				    tcode_is_read_request(tcode) ? 0 : request->length / 4);
1013 
1014 	offset = async_header_get_offset(p->header);
1015 
1016 	if (!is_in_fcp_region(offset, request->length))
1017 		handle_exclusive_region_request(card, p, request, offset);
1018 	else
1019 		handle_fcp_region_request(card, p, request, offset);
1020 
1021 }
1022 EXPORT_SYMBOL(fw_core_handle_request);
1023 
1024 void fw_core_handle_response(struct fw_card *card, struct fw_packet *p)
1025 {
1026 	struct fw_transaction *t = NULL, *iter;
1027 	unsigned long flags;
1028 	u32 *data;
1029 	size_t data_length;
1030 	int tcode, tlabel, source, rcode;
1031 
1032 	tcode = async_header_get_tcode(p->header);
1033 	tlabel = async_header_get_tlabel(p->header);
1034 	source = async_header_get_source(p->header);
1035 	rcode = async_header_get_rcode(p->header);
1036 
1037 	// FIXME: sanity check packet, is length correct, does tcodes
1038 	// and addresses match to the transaction request queried later.
1039 	//
1040 	// For the tracepoints event, let us decode the header here against the concern.
1041 
1042 	switch (tcode) {
1043 	case TCODE_READ_QUADLET_RESPONSE:
1044 		data = (u32 *) &p->header[3];
1045 		data_length = 4;
1046 		break;
1047 
1048 	case TCODE_WRITE_RESPONSE:
1049 		data = NULL;
1050 		data_length = 0;
1051 		break;
1052 
1053 	case TCODE_READ_BLOCK_RESPONSE:
1054 	case TCODE_LOCK_RESPONSE:
1055 		data = p->payload;
1056 		data_length = async_header_get_data_length(p->header);
1057 		break;
1058 
1059 	default:
1060 		/* Should never happen, this is just to shut up gcc. */
1061 		data = NULL;
1062 		data_length = 0;
1063 		break;
1064 	}
1065 
1066 	spin_lock_irqsave(&card->lock, flags);
1067 	list_for_each_entry(iter, &card->transaction_list, link) {
1068 		if (iter->node_id == source && iter->tlabel == tlabel) {
1069 			if (!try_cancel_split_timeout(iter)) {
1070 				spin_unlock_irqrestore(&card->lock, flags);
1071 				goto timed_out;
1072 			}
1073 			list_del_init(&iter->link);
1074 			card->tlabel_mask &= ~(1ULL << iter->tlabel);
1075 			t = iter;
1076 			break;
1077 		}
1078 	}
1079 	spin_unlock_irqrestore(&card->lock, flags);
1080 
1081 	trace_async_response_inbound((uintptr_t)t, card->index, p->generation, p->speed, p->ack,
1082 				     p->timestamp, p->header, data, data_length / 4);
1083 
1084 	if (!t) {
1085  timed_out:
1086 		fw_notice(card, "unsolicited response (source %x, tlabel %x)\n",
1087 			  source, tlabel);
1088 		return;
1089 	}
1090 
1091 	/*
1092 	 * The response handler may be executed while the request handler
1093 	 * is still pending.  Cancel the request handler.
1094 	 */
1095 	card->driver->cancel_packet(card, &t->packet);
1096 
1097 	if (!t->with_tstamp) {
1098 		t->callback.without_tstamp(card, rcode, data, data_length, t->callback_data);
1099 	} else {
1100 		t->callback.with_tstamp(card, rcode, t->packet.timestamp, p->timestamp, data,
1101 					data_length, t->callback_data);
1102 	}
1103 }
1104 EXPORT_SYMBOL(fw_core_handle_response);
1105 
1106 /**
1107  * fw_rcode_string - convert a firewire result code to an error description
1108  * @rcode: the result code
1109  */
1110 const char *fw_rcode_string(int rcode)
1111 {
1112 	static const char *const names[] = {
1113 		[RCODE_COMPLETE]       = "no error",
1114 		[RCODE_CONFLICT_ERROR] = "conflict error",
1115 		[RCODE_DATA_ERROR]     = "data error",
1116 		[RCODE_TYPE_ERROR]     = "type error",
1117 		[RCODE_ADDRESS_ERROR]  = "address error",
1118 		[RCODE_SEND_ERROR]     = "send error",
1119 		[RCODE_CANCELLED]      = "timeout",
1120 		[RCODE_BUSY]           = "busy",
1121 		[RCODE_GENERATION]     = "bus reset",
1122 		[RCODE_NO_ACK]         = "no ack",
1123 	};
1124 
1125 	if ((unsigned int)rcode < ARRAY_SIZE(names) && names[rcode])
1126 		return names[rcode];
1127 	else
1128 		return "unknown";
1129 }
1130 EXPORT_SYMBOL(fw_rcode_string);
1131 
1132 static const struct fw_address_region topology_map_region =
1133 	{ .start = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP,
1134 	  .end   = CSR_REGISTER_BASE | CSR_TOPOLOGY_MAP_END, };
1135 
1136 static void handle_topology_map(struct fw_card *card, struct fw_request *request,
1137 		int tcode, int destination, int source, int generation,
1138 		unsigned long long offset, void *payload, size_t length,
1139 		void *callback_data)
1140 {
1141 	int start;
1142 
1143 	if (!tcode_is_read_request(tcode)) {
1144 		fw_send_response(card, request, RCODE_TYPE_ERROR);
1145 		return;
1146 	}
1147 
1148 	if ((offset & 3) > 0 || (length & 3) > 0) {
1149 		fw_send_response(card, request, RCODE_ADDRESS_ERROR);
1150 		return;
1151 	}
1152 
1153 	start = (offset - topology_map_region.start) / 4;
1154 	memcpy(payload, &card->topology_map[start], length);
1155 
1156 	fw_send_response(card, request, RCODE_COMPLETE);
1157 }
1158 
1159 static struct fw_address_handler topology_map = {
1160 	.length			= 0x400,
1161 	.address_callback	= handle_topology_map,
1162 };
1163 
1164 static const struct fw_address_region registers_region =
1165 	{ .start = CSR_REGISTER_BASE,
1166 	  .end   = CSR_REGISTER_BASE | CSR_CONFIG_ROM, };
1167 
1168 static void update_split_timeout(struct fw_card *card)
1169 {
1170 	unsigned int cycles;
1171 
1172 	cycles = card->split_timeout_hi * 8000 + (card->split_timeout_lo >> 19);
1173 
1174 	/* minimum per IEEE 1394, maximum which doesn't overflow OHCI */
1175 	cycles = clamp(cycles, 800u, 3u * 8000u);
1176 
1177 	card->split_timeout_cycles = cycles;
1178 	card->split_timeout_jiffies = DIV_ROUND_UP(cycles * HZ, 8000);
1179 }
1180 
1181 static void handle_registers(struct fw_card *card, struct fw_request *request,
1182 		int tcode, int destination, int source, int generation,
1183 		unsigned long long offset, void *payload, size_t length,
1184 		void *callback_data)
1185 {
1186 	int reg = offset & ~CSR_REGISTER_BASE;
1187 	__be32 *data = payload;
1188 	int rcode = RCODE_COMPLETE;
1189 	unsigned long flags;
1190 
1191 	switch (reg) {
1192 	case CSR_PRIORITY_BUDGET:
1193 		if (!card->priority_budget_implemented) {
1194 			rcode = RCODE_ADDRESS_ERROR;
1195 			break;
1196 		}
1197 		fallthrough;
1198 
1199 	case CSR_NODE_IDS:
1200 		/*
1201 		 * per IEEE 1394-2008 8.3.22.3, not IEEE 1394.1-2004 3.2.8
1202 		 * and 9.6, but interoperable with IEEE 1394.1-2004 bridges
1203 		 */
1204 		fallthrough;
1205 
1206 	case CSR_STATE_CLEAR:
1207 	case CSR_STATE_SET:
1208 	case CSR_CYCLE_TIME:
1209 	case CSR_BUS_TIME:
1210 	case CSR_BUSY_TIMEOUT:
1211 		if (tcode == TCODE_READ_QUADLET_REQUEST)
1212 			*data = cpu_to_be32(card->driver->read_csr(card, reg));
1213 		else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1214 			card->driver->write_csr(card, reg, be32_to_cpu(*data));
1215 		else
1216 			rcode = RCODE_TYPE_ERROR;
1217 		break;
1218 
1219 	case CSR_RESET_START:
1220 		if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1221 			card->driver->write_csr(card, CSR_STATE_CLEAR,
1222 						CSR_STATE_BIT_ABDICATE);
1223 		else
1224 			rcode = RCODE_TYPE_ERROR;
1225 		break;
1226 
1227 	case CSR_SPLIT_TIMEOUT_HI:
1228 		if (tcode == TCODE_READ_QUADLET_REQUEST) {
1229 			*data = cpu_to_be32(card->split_timeout_hi);
1230 		} else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
1231 			spin_lock_irqsave(&card->lock, flags);
1232 			card->split_timeout_hi = be32_to_cpu(*data) & 7;
1233 			update_split_timeout(card);
1234 			spin_unlock_irqrestore(&card->lock, flags);
1235 		} else {
1236 			rcode = RCODE_TYPE_ERROR;
1237 		}
1238 		break;
1239 
1240 	case CSR_SPLIT_TIMEOUT_LO:
1241 		if (tcode == TCODE_READ_QUADLET_REQUEST) {
1242 			*data = cpu_to_be32(card->split_timeout_lo);
1243 		} else if (tcode == TCODE_WRITE_QUADLET_REQUEST) {
1244 			spin_lock_irqsave(&card->lock, flags);
1245 			card->split_timeout_lo =
1246 					be32_to_cpu(*data) & 0xfff80000;
1247 			update_split_timeout(card);
1248 			spin_unlock_irqrestore(&card->lock, flags);
1249 		} else {
1250 			rcode = RCODE_TYPE_ERROR;
1251 		}
1252 		break;
1253 
1254 	case CSR_MAINT_UTILITY:
1255 		if (tcode == TCODE_READ_QUADLET_REQUEST)
1256 			*data = card->maint_utility_register;
1257 		else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1258 			card->maint_utility_register = *data;
1259 		else
1260 			rcode = RCODE_TYPE_ERROR;
1261 		break;
1262 
1263 	case CSR_BROADCAST_CHANNEL:
1264 		if (tcode == TCODE_READ_QUADLET_REQUEST)
1265 			*data = cpu_to_be32(card->broadcast_channel);
1266 		else if (tcode == TCODE_WRITE_QUADLET_REQUEST)
1267 			card->broadcast_channel =
1268 			    (be32_to_cpu(*data) & BROADCAST_CHANNEL_VALID) |
1269 			    BROADCAST_CHANNEL_INITIAL;
1270 		else
1271 			rcode = RCODE_TYPE_ERROR;
1272 		break;
1273 
1274 	case CSR_BUS_MANAGER_ID:
1275 	case CSR_BANDWIDTH_AVAILABLE:
1276 	case CSR_CHANNELS_AVAILABLE_HI:
1277 	case CSR_CHANNELS_AVAILABLE_LO:
1278 		/*
1279 		 * FIXME: these are handled by the OHCI hardware and
1280 		 * the stack never sees these request. If we add
1281 		 * support for a new type of controller that doesn't
1282 		 * handle this in hardware we need to deal with these
1283 		 * transactions.
1284 		 */
1285 		BUG();
1286 		break;
1287 
1288 	default:
1289 		rcode = RCODE_ADDRESS_ERROR;
1290 		break;
1291 	}
1292 
1293 	fw_send_response(card, request, rcode);
1294 }
1295 
1296 static struct fw_address_handler registers = {
1297 	.length			= 0x400,
1298 	.address_callback	= handle_registers,
1299 };
1300 
1301 static void handle_low_memory(struct fw_card *card, struct fw_request *request,
1302 		int tcode, int destination, int source, int generation,
1303 		unsigned long long offset, void *payload, size_t length,
1304 		void *callback_data)
1305 {
1306 	/*
1307 	 * This catches requests not handled by the physical DMA unit,
1308 	 * i.e., wrong transaction types or unauthorized source nodes.
1309 	 */
1310 	fw_send_response(card, request, RCODE_TYPE_ERROR);
1311 }
1312 
1313 static struct fw_address_handler low_memory = {
1314 	.length			= FW_MAX_PHYSICAL_RANGE,
1315 	.address_callback	= handle_low_memory,
1316 };
1317 
1318 MODULE_AUTHOR("Kristian Hoegsberg <krh@bitplanet.net>");
1319 MODULE_DESCRIPTION("Core IEEE1394 transaction logic");
1320 MODULE_LICENSE("GPL");
1321 
1322 static const u32 vendor_textual_descriptor[] = {
1323 	/* textual descriptor leaf () */
1324 	0x00060000,
1325 	0x00000000,
1326 	0x00000000,
1327 	0x4c696e75,		/* L i n u */
1328 	0x78204669,		/* x   F i */
1329 	0x72657769,		/* r e w i */
1330 	0x72650000,		/* r e     */
1331 };
1332 
1333 static const u32 model_textual_descriptor[] = {
1334 	/* model descriptor leaf () */
1335 	0x00030000,
1336 	0x00000000,
1337 	0x00000000,
1338 	0x4a756a75,		/* J u j u */
1339 };
1340 
1341 static struct fw_descriptor vendor_id_descriptor = {
1342 	.length = ARRAY_SIZE(vendor_textual_descriptor),
1343 	.immediate = 0x03001f11,
1344 	.key = 0x81000000,
1345 	.data = vendor_textual_descriptor,
1346 };
1347 
1348 static struct fw_descriptor model_id_descriptor = {
1349 	.length = ARRAY_SIZE(model_textual_descriptor),
1350 	.immediate = 0x17023901,
1351 	.key = 0x81000000,
1352 	.data = model_textual_descriptor,
1353 };
1354 
1355 static int __init fw_core_init(void)
1356 {
1357 	int ret;
1358 
1359 	fw_workqueue = alloc_workqueue("firewire", WQ_MEM_RECLAIM, 0);
1360 	if (!fw_workqueue)
1361 		return -ENOMEM;
1362 
1363 	ret = bus_register(&fw_bus_type);
1364 	if (ret < 0) {
1365 		destroy_workqueue(fw_workqueue);
1366 		return ret;
1367 	}
1368 
1369 	fw_cdev_major = register_chrdev(0, "firewire", &fw_device_ops);
1370 	if (fw_cdev_major < 0) {
1371 		bus_unregister(&fw_bus_type);
1372 		destroy_workqueue(fw_workqueue);
1373 		return fw_cdev_major;
1374 	}
1375 
1376 	fw_core_add_address_handler(&topology_map, &topology_map_region);
1377 	fw_core_add_address_handler(&registers, &registers_region);
1378 	fw_core_add_address_handler(&low_memory, &low_memory_region);
1379 	fw_core_add_descriptor(&vendor_id_descriptor);
1380 	fw_core_add_descriptor(&model_id_descriptor);
1381 
1382 	return 0;
1383 }
1384 
1385 static void __exit fw_core_cleanup(void)
1386 {
1387 	unregister_chrdev(fw_cdev_major, "firewire");
1388 	bus_unregister(&fw_bus_type);
1389 	destroy_workqueue(fw_workqueue);
1390 	idr_destroy(&fw_device_idr);
1391 }
1392 
1393 module_init(fw_core_init);
1394 module_exit(fw_core_cleanup);
1395