ieee1394_transactions.c 13.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * IEEE 1394 for Linux
 *
 * Transaction support.
 *
 * Copyright (C) 1999 Andreas E. Bombe
 *
 * This code is licensed under the GPL.  See the file COPYING in the root
 * directory of the kernel sources for details.
 */

#include <linux/sched.h>
#include <linux/bitops.h>
#include <linux/smp_lock.h>
#include <linux/interrupt.h>

#include <asm/errno.h>

#include "ieee1394.h"
#include "ieee1394_types.h"
#include "hosts.h"
#include "ieee1394_core.h"
#include "highlevel.h"
#include "nodemgr.h"
25
#include "ieee1394_transactions.h"
L
Linus Torvalds 已提交
26 27 28 29 30 31 32 33 34 35

#define PREP_ASYNC_HEAD_ADDRESS(tc) \
        packet->tcode = tc; \
        packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \
                | (1 << 8) | (tc << 4); \
        packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \
        packet->header[2] = addr & 0xffffffff

static void fill_async_readquad(struct hpsb_packet *packet, u64 addr)
{
36 37 38 39
	PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);
	packet->header_size = 12;
	packet->data_size = 0;
	packet->expect_response = 1;
L
Linus Torvalds 已提交
40 41
}

42 43
static void fill_async_readblock(struct hpsb_packet *packet, u64 addr,
				 int length)
L
Linus Torvalds 已提交
44
{
45 46 47 48 49
	PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);
	packet->header[3] = length << 16;
	packet->header_size = 16;
	packet->data_size = 0;
	packet->expect_response = 1;
L
Linus Torvalds 已提交
50 51
}

52 53
static void fill_async_writequad(struct hpsb_packet *packet, u64 addr,
				 quadlet_t data)
L
Linus Torvalds 已提交
54
{
55 56 57 58 59
	PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);
	packet->header[3] = data;
	packet->header_size = 16;
	packet->data_size = 0;
	packet->expect_response = 1;
L
Linus Torvalds 已提交
60 61
}

62 63
static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr,
				  int length)
L
Linus Torvalds 已提交
64
{
65 66 67 68 69
	PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);
	packet->header[3] = length << 16;
	packet->header_size = 16;
	packet->expect_response = 1;
	packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);
L
Linus Torvalds 已提交
70 71 72
}

static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,
73
			    int length)
L
Linus Torvalds 已提交
74
{
75 76 77 78 79
	PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);
	packet->header[3] = (length << 16) | extcode;
	packet->header_size = 16;
	packet->data_size = length;
	packet->expect_response = 1;
L
Linus Torvalds 已提交
80 81 82
}

static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,
83
			    int tag, int sync)
L
Linus Torvalds 已提交
84
{
85 86
	packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
	    | (TCODE_ISO_DATA << 4) | sync;
L
Linus Torvalds 已提交
87

88 89 90 91
	packet->header_size = 4;
	packet->data_size = length;
	packet->type = hpsb_iso;
	packet->tcode = TCODE_ISO_DATA;
L
Linus Torvalds 已提交
92 93 94 95
}

static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data)
{
96 97 98 99 100 101 102
	packet->header[0] = data;
	packet->header[1] = ~data;
	packet->header_size = 8;
	packet->data_size = 0;
	packet->expect_response = 0;
	packet->type = hpsb_raw;	/* No CRC added */
	packet->speed_code = IEEE1394_SPEED_100;	/* Force speed to be 100Mbps */
L
Linus Torvalds 已提交
103 104 105 106 107 108
}

static void fill_async_stream_packet(struct hpsb_packet *packet, int length,
				     int channel, int tag, int sync)
{
	packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)
109
	    | (TCODE_STREAM_DATA << 4) | sync;
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138

	packet->header_size = 4;
	packet->data_size = length;
	packet->type = hpsb_async;
	packet->tcode = TCODE_ISO_DATA;
}

/**
 * hpsb_get_tlabel - allocate a transaction label
 * @packet: the packet who's tlabel/tpool we set
 *
 * Every asynchronous transaction on the 1394 bus needs a transaction
 * label to match the response to the request.  This label has to be
 * different from any other transaction label in an outstanding request to
 * the same node to make matching possible without ambiguity.
 *
 * There are 64 different tlabels, so an allocated tlabel has to be freed
 * with hpsb_free_tlabel() after the transaction is complete (unless it's
 * reused again for the same target node).
 *
 * Return value: Zero on success, otherwise non-zero. A non-zero return
 * generally means there are no available tlabels. If this is called out
 * of interrupt or atomic context, then it will sleep until can return a
 * tlabel.
 */
int hpsb_get_tlabel(struct hpsb_packet *packet)
{
	unsigned long flags;
	struct hpsb_tlabel_pool *tp;
139
	int n = NODEID_TO_NODE(packet->node_id);
L
Linus Torvalds 已提交
140

141 142 143
	if (unlikely(n == ALL_NODES))
		return 0;
	tp = &packet->host->tpool[n];
L
Linus Torvalds 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

	if (irqs_disabled() || in_atomic()) {
		if (down_trylock(&tp->count))
			return 1;
	} else {
		down(&tp->count);
	}

	spin_lock_irqsave(&tp->lock, flags);

	packet->tlabel = find_next_zero_bit(tp->pool, 64, tp->next);
	if (packet->tlabel > 63)
		packet->tlabel = find_first_zero_bit(tp->pool, 64);
	tp->next = (packet->tlabel + 1) % 64;
	/* Should _never_ happen */
	BUG_ON(test_and_set_bit(packet->tlabel, tp->pool));
	tp->allocations++;
	spin_unlock_irqrestore(&tp->lock, flags);

	return 0;
}

/**
 * hpsb_free_tlabel - free an allocated transaction label
 * @packet: packet whos tlabel/tpool needs to be cleared
 *
 * Frees the transaction label allocated with hpsb_get_tlabel().  The
 * tlabel has to be freed after the transaction is complete (i.e. response
 * was received for a split transaction or packet was sent for a unified
 * transaction).
 *
 * A tlabel must not be freed twice.
 */
void hpsb_free_tlabel(struct hpsb_packet *packet)
{
179
	unsigned long flags;
L
Linus Torvalds 已提交
180
	struct hpsb_tlabel_pool *tp;
181
	int n = NODEID_TO_NODE(packet->node_id);
L
Linus Torvalds 已提交
182

183 184 185
	if (unlikely(n == ALL_NODES))
		return;
	tp = &packet->host->tpool[n];
L
Linus Torvalds 已提交
186 187 188

	BUG_ON(packet->tlabel > 63 || packet->tlabel < 0);

189
	spin_lock_irqsave(&tp->lock, flags);
L
Linus Torvalds 已提交
190
	BUG_ON(!test_and_clear_bit(packet->tlabel, tp->pool));
191
	spin_unlock_irqrestore(&tp->lock, flags);
L
Linus Torvalds 已提交
192 193 194 195 196 197

	up(&tp->count);
}

int hpsb_packet_success(struct hpsb_packet *packet)
{
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	switch (packet->ack_code) {
	case ACK_PENDING:
		switch ((packet->header[1] >> 12) & 0xf) {
		case RCODE_COMPLETE:
			return 0;
		case RCODE_CONFLICT_ERROR:
			return -EAGAIN;
		case RCODE_DATA_ERROR:
			return -EREMOTEIO;
		case RCODE_TYPE_ERROR:
			return -EACCES;
		case RCODE_ADDRESS_ERROR:
			return -EINVAL;
		default:
			HPSB_ERR("received reserved rcode %d from node %d",
				 (packet->header[1] >> 12) & 0xf,
				 packet->node_id);
			return -EAGAIN;
		}
		HPSB_PANIC("reached unreachable code 1 in %s", __FUNCTION__);

	case ACK_BUSY_X:
	case ACK_BUSY_A:
	case ACK_BUSY_B:
		return -EBUSY;

	case ACK_TYPE_ERROR:
		return -EACCES;

	case ACK_COMPLETE:
		if (packet->tcode == TCODE_WRITEQ
		    || packet->tcode == TCODE_WRITEB) {
			return 0;
		} else {
			HPSB_ERR("impossible ack_complete from node %d "
				 "(tcode %d)", packet->node_id, packet->tcode);
			return -EAGAIN;
		}

	case ACK_DATA_ERROR:
		if (packet->tcode == TCODE_WRITEB
		    || packet->tcode == TCODE_LOCK_REQUEST) {
			return -EAGAIN;
		} else {
			HPSB_ERR("impossible ack_data_error from node %d "
				 "(tcode %d)", packet->node_id, packet->tcode);
			return -EAGAIN;
		}

	case ACK_ADDRESS_ERROR:
		return -EINVAL;

	case ACK_TARDY:
	case ACK_CONFLICT_ERROR:
	case ACKX_NONE:
	case ACKX_SEND_ERROR:
	case ACKX_ABORTED:
	case ACKX_TIMEOUT:
		/* error while sending */
		return -EAGAIN;

	default:
		HPSB_ERR("got invalid ack %d from node %d (tcode %d)",
			 packet->ack_code, packet->node_id, packet->tcode);
		return -EAGAIN;
	}

	HPSB_PANIC("reached unreachable code 2 in %s", __FUNCTION__);
L
Linus Torvalds 已提交
266 267 268 269 270
}

struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,
					 u64 addr, size_t length)
{
271
	struct hpsb_packet *packet;
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

	if (length == 0)
		return NULL;

	packet = hpsb_alloc_packet(length);
	if (!packet)
		return NULL;

	packet->host = host;
	packet->node_id = node;

	if (hpsb_get_tlabel(packet)) {
		hpsb_free_packet(packet);
		return NULL;
	}

	if (length == 4)
		fill_async_readquad(packet, addr);
	else
		fill_async_readblock(packet, addr, length);

	return packet;
}

296 297 298
struct hpsb_packet *hpsb_make_writepacket(struct hpsb_host *host, nodeid_t node,
					  u64 addr, quadlet_t * buffer,
					  size_t length)
L
Linus Torvalds 已提交
299 300 301 302 303 304 305 306 307 308
{
	struct hpsb_packet *packet;

	if (length == 0)
		return NULL;

	packet = hpsb_alloc_packet(length);
	if (!packet)
		return NULL;

309
	if (length % 4) {	/* zero padding bytes */
L
Linus Torvalds 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		packet->data[length >> 2] = 0;
	}
	packet->host = host;
	packet->node_id = node;

	if (hpsb_get_tlabel(packet)) {
		hpsb_free_packet(packet);
		return NULL;
	}

	if (length == 4) {
		fill_async_writequad(packet, addr, buffer ? *buffer : 0);
	} else {
		fill_async_writeblock(packet, addr, length);
		if (buffer)
			memcpy(packet->data, buffer, length);
	}

	return packet;
}

331 332 333
struct hpsb_packet *hpsb_make_streampacket(struct hpsb_host *host, u8 * buffer,
					   int length, int channel, int tag,
					   int sync)
L
Linus Torvalds 已提交
334 335 336 337 338 339 340 341 342 343
{
	struct hpsb_packet *packet;

	if (length == 0)
		return NULL;

	packet = hpsb_alloc_packet(length);
	if (!packet)
		return NULL;

344
	if (length % 4) {	/* zero padding bytes */
L
Linus Torvalds 已提交
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
		packet->data[length >> 2] = 0;
	}
	packet->host = host;

	if (hpsb_get_tlabel(packet)) {
		hpsb_free_packet(packet);
		return NULL;
	}

	fill_async_stream_packet(packet, length, channel, tag, sync);
	if (buffer)
		memcpy(packet->data, buffer, length);

	return packet;
}

struct hpsb_packet *hpsb_make_lockpacket(struct hpsb_host *host, nodeid_t node,
362 363
					 u64 addr, int extcode,
					 quadlet_t * data, quadlet_t arg)
L
Linus Torvalds 已提交
364 365 366 367 368
{
	struct hpsb_packet *p;
	u32 length;

	p = hpsb_alloc_packet(8);
369 370
	if (!p)
		return NULL;
L
Linus Torvalds 已提交
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

	p->host = host;
	p->node_id = node;
	if (hpsb_get_tlabel(p)) {
		hpsb_free_packet(p);
		return NULL;
	}

	switch (extcode) {
	case EXTCODE_FETCH_ADD:
	case EXTCODE_LITTLE_ADD:
		length = 4;
		if (data)
			p->data[0] = *data;
		break;
	default:
		length = 8;
		if (data) {
			p->data[0] = arg;
			p->data[1] = *data;
		}
		break;
	}
	fill_async_lock(p, addr, extcode, length);

	return p;
}

399 400 401
struct hpsb_packet *hpsb_make_lock64packet(struct hpsb_host *host,
					   nodeid_t node, u64 addr, int extcode,
					   octlet_t * data, octlet_t arg)
L
Linus Torvalds 已提交
402 403 404 405 406
{
	struct hpsb_packet *p;
	u32 length;

	p = hpsb_alloc_packet(16);
407 408
	if (!p)
		return NULL;
L
Linus Torvalds 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440

	p->host = host;
	p->node_id = node;
	if (hpsb_get_tlabel(p)) {
		hpsb_free_packet(p);
		return NULL;
	}

	switch (extcode) {
	case EXTCODE_FETCH_ADD:
	case EXTCODE_LITTLE_ADD:
		length = 8;
		if (data) {
			p->data[0] = *data >> 32;
			p->data[1] = *data & 0xffffffff;
		}
		break;
	default:
		length = 16;
		if (data) {
			p->data[0] = arg >> 32;
			p->data[1] = arg & 0xffffffff;
			p->data[2] = *data >> 32;
			p->data[3] = *data & 0xffffffff;
		}
		break;
	}
	fill_async_lock(p, addr, extcode, length);

	return p;
}

441
struct hpsb_packet *hpsb_make_phypacket(struct hpsb_host *host, quadlet_t data)
L
Linus Torvalds 已提交
442
{
443
	struct hpsb_packet *p;
L
Linus Torvalds 已提交
444

445 446 447
	p = hpsb_alloc_packet(0);
	if (!p)
		return NULL;
L
Linus Torvalds 已提交
448

449 450
	p->host = host;
	fill_phy_packet(p, data);
L
Linus Torvalds 已提交
451

452
	return p;
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461
}

struct hpsb_packet *hpsb_make_isopacket(struct hpsb_host *host,
					int length, int channel,
					int tag, int sync)
{
	struct hpsb_packet *p;

	p = hpsb_alloc_packet(length);
462 463
	if (!p)
		return NULL;
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

	p->host = host;
	fill_iso_packet(p, length, channel, tag, sync);

	p->generation = get_hpsb_generation(host);

	return p;
}

/*
 * FIXME - these functions should probably read from / write to user space to
 * avoid in kernel buffers for user space callers
 */

int hpsb_read(struct hpsb_host *host, nodeid_t node, unsigned int generation,
479
	      u64 addr, quadlet_t * buffer, size_t length)
L
Linus Torvalds 已提交
480
{
481 482
	struct hpsb_packet *packet;
	int retval = 0;
L
Linus Torvalds 已提交
483

484 485
	if (length == 0)
		return -EINVAL;
L
Linus Torvalds 已提交
486

487
	BUG_ON(in_interrupt());	// We can't be called in an interrupt, yet
L
Linus Torvalds 已提交
488 489 490

	packet = hpsb_make_readpacket(host, node, addr, length);

491 492 493
	if (!packet) {
		return -ENOMEM;
	}
L
Linus Torvalds 已提交
494 495

	packet->generation = generation;
496
	retval = hpsb_send_packet_and_wait(packet);
L
Linus Torvalds 已提交
497 498 499
	if (retval < 0)
		goto hpsb_read_fail;

500
	retval = hpsb_packet_success(packet);
L
Linus Torvalds 已提交
501

502 503 504 505 506 507 508
	if (retval == 0) {
		if (length == 4) {
			*buffer = packet->header[3];
		} else {
			memcpy(buffer, packet->data, length);
		}
	}
L
Linus Torvalds 已提交
509

510 511 512
      hpsb_read_fail:
	hpsb_free_tlabel(packet);
	hpsb_free_packet(packet);
L
Linus Torvalds 已提交
513

514
	return retval;
L
Linus Torvalds 已提交
515 516 517
}

int hpsb_write(struct hpsb_host *host, nodeid_t node, unsigned int generation,
518
	       u64 addr, quadlet_t * buffer, size_t length)
L
Linus Torvalds 已提交
519 520 521 522 523 524 525
{
	struct hpsb_packet *packet;
	int retval;

	if (length == 0)
		return -EINVAL;

526
	BUG_ON(in_interrupt());	// We can't be called in an interrupt, yet
L
Linus Torvalds 已提交
527

528
	packet = hpsb_make_writepacket(host, node, addr, buffer, length);
L
Linus Torvalds 已提交
529 530 531 532 533

	if (!packet)
		return -ENOMEM;

	packet->generation = generation;
534
	retval = hpsb_send_packet_and_wait(packet);
L
Linus Torvalds 已提交
535 536 537
	if (retval < 0)
		goto hpsb_write_fail;

538
	retval = hpsb_packet_success(packet);
L
Linus Torvalds 已提交
539

540 541 542
      hpsb_write_fail:
	hpsb_free_tlabel(packet);
	hpsb_free_packet(packet);
L
Linus Torvalds 已提交
543

544
	return retval;
L
Linus Torvalds 已提交
545 546
}

547
#if 0
L
Linus Torvalds 已提交
548 549

int hpsb_lock(struct hpsb_host *host, nodeid_t node, unsigned int generation,
550
	      u64 addr, int extcode, quadlet_t * data, quadlet_t arg)
L
Linus Torvalds 已提交
551
{
552 553
	struct hpsb_packet *packet;
	int retval = 0;
L
Linus Torvalds 已提交
554

555
	BUG_ON(in_interrupt());	// We can't be called in an interrupt, yet
L
Linus Torvalds 已提交
556 557

	packet = hpsb_make_lockpacket(host, node, addr, extcode, data, arg);
558 559
	if (!packet)
		return -ENOMEM;
L
Linus Torvalds 已提交
560 561

	packet->generation = generation;
562
	retval = hpsb_send_packet_and_wait(packet);
L
Linus Torvalds 已提交
563 564 565
	if (retval < 0)
		goto hpsb_lock_fail;

566
	retval = hpsb_packet_success(packet);
L
Linus Torvalds 已提交
567

568 569 570
	if (retval == 0) {
		*data = packet->data[0];
	}
L
Linus Torvalds 已提交
571

572 573 574
      hpsb_lock_fail:
	hpsb_free_tlabel(packet);
	hpsb_free_packet(packet);
L
Linus Torvalds 已提交
575

576
	return retval;
L
Linus Torvalds 已提交
577 578 579
}

int hpsb_send_gasp(struct hpsb_host *host, int channel, unsigned int generation,
580
		   quadlet_t * buffer, size_t length, u32 specifier_id,
L
Linus Torvalds 已提交
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
		   unsigned int version)
{
	struct hpsb_packet *packet;
	int retval = 0;
	u16 specifier_id_hi = (specifier_id & 0x00ffff00) >> 8;
	u8 specifier_id_lo = specifier_id & 0xff;

	HPSB_VERBOSE("Send GASP: channel = %d, length = %Zd", channel, length);

	length += 8;

	packet = hpsb_make_streampacket(host, NULL, length, channel, 3, 0);
	if (!packet)
		return -ENOMEM;

	packet->data[0] = cpu_to_be32((host->node_id << 16) | specifier_id_hi);
597 598
	packet->data[1] =
	    cpu_to_be32((specifier_id_lo << 24) | (version & 0x00ffffff));
L
Linus Torvalds 已提交
599 600 601 602 603 604 605 606 607 608 609 610 611

	memcpy(&(packet->data[2]), buffer, length - 8);

	packet->generation = generation;

	packet->no_waiter = 1;

	retval = hpsb_send_packet(packet);
	if (retval < 0)
		hpsb_free_packet(packet);

	return retval;
}
612

613
#endif				/*  0  */