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