iso.c 13.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * IEEE 1394 for Linux
 *
 * kernel ISO transmission/reception
 *
 * Copyright (C) 2002 Maas Digital LLC
 *
 * This code is licensed under the GPL.  See the file COPYING in the root
 * directory of the kernel sources for details.
 */

12
#include <linux/pci.h>
A
Andrew Morton 已提交
13
#include <linux/sched.h>
14
#include <linux/mm.h>
15 16 17
#include <linux/slab.h>

#include "hosts.h"
L
Linus Torvalds 已提交
18 19
#include "iso.h"

20 21 22
/**
 * hpsb_iso_stop - stop DMA
 */
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32
void hpsb_iso_stop(struct hpsb_iso *iso)
{
	if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
		return;

	iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
				  XMIT_STOP : RECV_STOP, 0);
	iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
}

33 34 35
/**
 * hpsb_iso_shutdown - deallocate buffer and DMA context
 */
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48
void hpsb_iso_shutdown(struct hpsb_iso *iso)
{
	if (iso->flags & HPSB_ISO_DRIVER_INIT) {
		hpsb_iso_stop(iso);
		iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
					  XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
		iso->flags &= ~HPSB_ISO_DRIVER_INIT;
	}

	dma_region_free(&iso->data_buf);
	kfree(iso);
}

49 50
static struct hpsb_iso *hpsb_iso_common_init(struct hpsb_host *host,
					     enum hpsb_iso_type type,
L
Linus Torvalds 已提交
51 52
					     unsigned int data_buf_size,
					     unsigned int buf_packets,
53
					     int channel, int dma_mode,
L
Linus Torvalds 已提交
54
					     int irq_interval,
55 56
					     void (*callback) (struct hpsb_iso
							       *))
L
Linus Torvalds 已提交
57 58 59 60 61 62
{
	struct hpsb_iso *iso;
	int dma_direction;

	/* make sure driver supports the ISO API */
	if (!host->driver->isoctl) {
63 64
		printk(KERN_INFO
		       "ieee1394: host driver '%s' does not support the rawiso API\n",
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73
		       host->driver->name);
		return NULL;
	}

	/* sanitize parameters */

	if (buf_packets < 2)
		buf_packets = 2;

74 75 76
	if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
	    || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
		dma_mode = HPSB_ISO_DMA_DEFAULT;
L
Linus Torvalds 已提交
77

B
Ben Collins 已提交
78
	if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
79 80
		irq_interval = buf_packets / 4;
	if (irq_interval == 0)	/* really interrupt for each packet */
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91
		irq_interval = 1;

	if (channel < -1 || channel >= 64)
		return NULL;

	/* channel = -1 is OK for multi-channel recv but not for xmit */
	if (type == HPSB_ISO_XMIT && channel < 0)
		return NULL;

	/* allocate and write the struct hpsb_iso */

92 93 94 95
	iso =
	    kmalloc(sizeof(*iso) +
		    buf_packets * sizeof(struct hpsb_iso_packet_info),
		    GFP_KERNEL);
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	if (!iso)
		return NULL;

	iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);

	iso->type = type;
	iso->host = host;
	iso->hostdata = NULL;
	iso->callback = callback;
	init_waitqueue_head(&iso->waitq);
	iso->channel = channel;
	iso->irq_interval = irq_interval;
	iso->dma_mode = dma_mode;
	dma_region_init(&iso->data_buf);
	iso->buf_size = PAGE_ALIGN(data_buf_size);
	iso->buf_packets = buf_packets;
	iso->pkt_dma = 0;
	iso->first_packet = 0;
	spin_lock_init(&iso->lock);

	if (iso->type == HPSB_ISO_XMIT) {
		iso->n_ready_packets = iso->buf_packets;
		dma_direction = PCI_DMA_TODEVICE;
	} else {
		iso->n_ready_packets = 0;
		dma_direction = PCI_DMA_FROMDEVICE;
	}

	atomic_set(&iso->overflows, 0);
B
Ben Collins 已提交
125
	iso->bytes_discarded = 0;
L
Linus Torvalds 已提交
126 127 128 129
	iso->flags = 0;
	iso->prebuffer = 0;

	/* allocate the packet buffer */
130 131
	if (dma_region_alloc
	    (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
L
Linus Torvalds 已提交
132 133 134 135
		goto err;

	return iso;

136
      err:
L
Linus Torvalds 已提交
137 138 139 140
	hpsb_iso_shutdown(iso);
	return NULL;
}

141 142 143
/**
 * hpsb_iso_n_ready - returns number of packets ready to send or receive
 */
144
int hpsb_iso_n_ready(struct hpsb_iso *iso)
L
Linus Torvalds 已提交
145 146 147 148 149 150 151 152 153 154 155
{
	unsigned long flags;
	int val;

	spin_lock_irqsave(&iso->lock, flags);
	val = iso->n_ready_packets;
	spin_unlock_irqrestore(&iso->lock, flags);

	return val;
}

156 157 158
/**
 * hpsb_iso_xmit_init - allocate the buffer and DMA context
 */
159
struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host,
L
Linus Torvalds 已提交
160 161 162 163 164
				    unsigned int data_buf_size,
				    unsigned int buf_packets,
				    int channel,
				    int speed,
				    int irq_interval,
165
				    void (*callback) (struct hpsb_iso *))
L
Linus Torvalds 已提交
166 167 168
{
	struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
						    data_buf_size, buf_packets,
169 170 171
						    channel,
						    HPSB_ISO_DMA_DEFAULT,
						    irq_interval, callback);
L
Linus Torvalds 已提交
172 173 174 175 176 177 178 179 180 181 182 183
	if (!iso)
		return NULL;

	iso->speed = speed;

	/* tell the driver to start working */
	if (host->driver->isoctl(iso, XMIT_INIT, 0))
		goto err;

	iso->flags |= HPSB_ISO_DRIVER_INIT;
	return iso;

184
      err:
L
Linus Torvalds 已提交
185 186 187 188
	hpsb_iso_shutdown(iso);
	return NULL;
}

189 190 191 192 193
/**
 * hpsb_iso_recv_init - allocate the buffer and DMA context
 *
 * Note, if channel = -1, multi-channel receive is enabled.
 */
194
struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host,
L
Linus Torvalds 已提交
195 196 197 198 199
				    unsigned int data_buf_size,
				    unsigned int buf_packets,
				    int channel,
				    int dma_mode,
				    int irq_interval,
200
				    void (*callback) (struct hpsb_iso *))
L
Linus Torvalds 已提交
201 202 203
{
	struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
						    data_buf_size, buf_packets,
204 205
						    channel, dma_mode,
						    irq_interval, callback);
L
Linus Torvalds 已提交
206 207 208 209 210 211 212 213 214 215
	if (!iso)
		return NULL;

	/* tell the driver to start working */
	if (host->driver->isoctl(iso, RECV_INIT, 0))
		goto err;

	iso->flags |= HPSB_ISO_DRIVER_INIT;
	return iso;

216
      err:
L
Linus Torvalds 已提交
217 218 219 220
	hpsb_iso_shutdown(iso);
	return NULL;
}

221 222 223 224 225
/**
 * hpsb_iso_recv_listen_channel
 *
 * multi-channel only
 */
L
Linus Torvalds 已提交
226 227 228 229 230 231 232
int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
{
	if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
		return -EINVAL;
	return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
}

233 234 235 236 237
/**
 * hpsb_iso_recv_unlisten_channel
 *
 * multi-channel only
 */
L
Linus Torvalds 已提交
238 239
int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
{
240 241 242
	if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
		return -EINVAL;
	return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
L
Linus Torvalds 已提交
243 244
}

245 246 247 248 249
/**
 * hpsb_iso_recv_set_channel_mask
 *
 * multi-channel only
 */
L
Linus Torvalds 已提交
250 251 252 253
int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
{
	if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
		return -EINVAL;
254 255
	return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
					 (unsigned long)&mask);
L
Linus Torvalds 已提交
256 257
}

258 259 260 261 262 263
/**
 * hpsb_iso_recv_flush - check for arrival of new packets
 *
 * check for arrival of new packets immediately (even if irq_interval
 * has not yet been reached)
 */
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
int hpsb_iso_recv_flush(struct hpsb_iso *iso)
{
	if (iso->type != HPSB_ISO_RECV)
		return -EINVAL;
	return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
}

static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
{
	int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
	if (retval)
		return retval;

	iso->flags |= HPSB_ISO_DRIVER_STARTED;
	return retval;
}

281 282 283
/**
 * hpsb_iso_xmit_start - start DMA
 */
L
Linus Torvalds 已提交
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
{
	if (iso->type != HPSB_ISO_XMIT)
		return -1;

	if (iso->flags & HPSB_ISO_DRIVER_STARTED)
		return 0;

	if (cycle < -1)
		cycle = -1;
	else if (cycle >= 8000)
		cycle %= 8000;

	iso->xmit_cycle = cycle;

	if (prebuffer < 0)
B
Ben Collins 已提交
300
		prebuffer = iso->buf_packets - 1;
L
Linus Torvalds 已提交
301 302 303
	else if (prebuffer == 0)
		prebuffer = 1;

B
Ben Collins 已提交
304 305
	if (prebuffer >= iso->buf_packets)
		prebuffer = iso->buf_packets - 1;
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315

	iso->prebuffer = prebuffer;

	/* remember the starting cycle; DMA will commence from xmit_queue_packets()
	   once enough packets have been buffered */
	iso->start_cycle = cycle;

	return 0;
}

316 317 318
/**
 * hpsb_iso_recv_start - start DMA
 */
L
Linus Torvalds 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
{
	int retval = 0;
	int isoctl_args[3];

	if (iso->type != HPSB_ISO_RECV)
		return -1;

	if (iso->flags & HPSB_ISO_DRIVER_STARTED)
		return 0;

	if (cycle < -1)
		cycle = -1;
	else if (cycle >= 8000)
		cycle %= 8000;

	isoctl_args[0] = cycle;

	if (tag_mask < 0)
		/* match all tags */
		tag_mask = 0xF;
	isoctl_args[1] = tag_mask;

	isoctl_args[2] = sync;

344 345 346
	retval =
	    iso->host->driver->isoctl(iso, RECV_START,
				      (unsigned long)&isoctl_args[0]);
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354
	if (retval)
		return retval;

	iso->flags |= HPSB_ISO_DRIVER_STARTED;
	return retval;
}

/* check to make sure the user has not supplied bogus values of offset/len
355
 * that would cause the kernel to access memory outside the buffer */
L
Linus Torvalds 已提交
356 357
static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
				     unsigned int offset, unsigned short len,
358 359
				     unsigned int *out_offset,
				     unsigned short *out_len)
L
Linus Torvalds 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
{
	if (offset >= iso->buf_size)
		return -EFAULT;

	/* make sure the packet does not go beyond the end of the buffer */
	if (offset + len > iso->buf_size)
		return -EFAULT;

	/* check for wrap-around */
	if (offset + len < offset)
		return -EFAULT;

	/* now we can trust 'offset' and 'length' */
	*out_offset = offset;
	*out_len = len;

	return 0;
}

379 380 381 382 383 384
/**
 * hpsb_iso_xmit_queue_packet - queue a packet for transmission.
 *
 * @offset is relative to the beginning of the DMA buffer, where the packet's
 * data payload should already have been placed.
 */
385 386
int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
			       u8 tag, u8 sy)
L
Linus Torvalds 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
{
	struct hpsb_iso_packet_info *info;
	unsigned long flags;
	int rv;

	if (iso->type != HPSB_ISO_XMIT)
		return -EINVAL;

	/* is there space in the buffer? */
	if (iso->n_ready_packets <= 0) {
		return -EBUSY;
	}

	info = &iso->infos[iso->first_packet];

	/* check for bogus offset/length */
403 404
	if (hpsb_iso_check_offset_len
	    (iso, offset, len, &info->offset, &info->len))
L
Linus Torvalds 已提交
405 406 407 408 409 410 411
		return -EFAULT;

	info->tag = tag;
	info->sy = sy;

	spin_lock_irqsave(&iso->lock, flags);

412
	rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
L
Linus Torvalds 已提交
413 414 415 416
	if (rv)
		goto out;

	/* increment cursors */
417 418
	iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
	iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000;
L
Linus Torvalds 已提交
419 420 421 422 423 424 425 426 427 428
	iso->n_ready_packets--;

	if (iso->prebuffer != 0) {
		iso->prebuffer--;
		if (iso->prebuffer <= 0) {
			iso->prebuffer = 0;
			rv = do_iso_xmit_start(iso, iso->start_cycle);
		}
	}

429
      out:
L
Linus Torvalds 已提交
430 431 432 433
	spin_unlock_irqrestore(&iso->lock, flags);
	return rv;
}

434 435 436
/**
 * hpsb_iso_xmit_sync - wait until all queued packets have been transmitted
 */
L
Linus Torvalds 已提交
437 438 439 440 441
int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
{
	if (iso->type != HPSB_ISO_XMIT)
		return -EINVAL;

442 443 444
	return wait_event_interruptible(iso->waitq,
					hpsb_iso_n_ready(iso) ==
					iso->buf_packets);
L
Linus Torvalds 已提交
445 446
}

447 448 449 450 451 452 453 454 455
/**
 * hpsb_iso_packet_sent
 *
 * Available to low-level drivers.
 *
 * Call after a packet has been transmitted to the bus (interrupt context is
 * OK).  @cycle is the _exact_ cycle the packet was sent on.  @error should be
 * non-zero if some sort of error occurred when sending the packet.
 */
L
Linus Torvalds 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
{
	unsigned long flags;
	spin_lock_irqsave(&iso->lock, flags);

	/* predict the cycle of the next packet to be queued */

	/* jump ahead by the number of packets that are already buffered */
	cycle += iso->buf_packets - iso->n_ready_packets;
	cycle %= 8000;

	iso->xmit_cycle = cycle;
	iso->n_ready_packets++;
	iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;

	if (iso->n_ready_packets == iso->buf_packets || error != 0) {
		/* the buffer has run empty! */
		atomic_inc(&iso->overflows);
	}

	spin_unlock_irqrestore(&iso->lock, flags);
}

479 480 481 482 483 484 485
/**
 * hpsb_iso_packet_received
 *
 * Available to low-level drivers.
 *
 * Call after a packet has been received (interrupt context is OK).
 */
L
Linus Torvalds 已提交
486
void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
487 488
			      u16 total_len, u16 cycle, u8 channel, u8 tag,
			      u8 sy)
L
Linus Torvalds 已提交
489 490 491 492 493 494 495
{
	unsigned long flags;
	spin_lock_irqsave(&iso->lock, flags);

	if (iso->n_ready_packets == iso->buf_packets) {
		/* overflow! */
		atomic_inc(&iso->overflows);
B
Ben Collins 已提交
496 497
		/* Record size of this discarded packet */
		iso->bytes_discarded += total_len;
L
Linus Torvalds 已提交
498 499 500 501
	} else {
		struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
		info->offset = offset;
		info->len = len;
B
Ben Collins 已提交
502
		info->total_len = total_len;
L
Linus Torvalds 已提交
503 504 505 506 507
		info->cycle = cycle;
		info->channel = channel;
		info->tag = tag;
		info->sy = sy;

508
		iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
L
Linus Torvalds 已提交
509 510 511 512 513 514
		iso->n_ready_packets++;
	}

	spin_unlock_irqrestore(&iso->lock, flags);
}

515 516 517 518 519
/**
 * hpsb_iso_recv_release_packets - release packets, reuse buffer
 *
 * @n_packets have been read out of the buffer, re-use the buffer space
 */
L
Linus Torvalds 已提交
520 521 522 523 524 525 526 527 528 529 530 531
int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
{
	unsigned long flags;
	unsigned int i;
	int rv = 0;

	if (iso->type != HPSB_ISO_RECV)
		return -1;

	spin_lock_irqsave(&iso->lock, flags);
	for (i = 0; i < n_packets; i++) {
		rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
532 533
					       (unsigned long)&iso->infos[iso->
									  first_packet]);
L
Linus Torvalds 已提交
534 535 536
		if (rv)
			break;

537
		iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
L
Linus Torvalds 已提交
538
		iso->n_ready_packets--;
B
Ben Collins 已提交
539 540

		/* release memory from packets discarded when queue was full  */
541
		if (iso->n_ready_packets == 0) {	/* Release only after all prior packets handled */
B
Ben Collins 已提交
542 543 544 545
			if (iso->bytes_discarded != 0) {
				struct hpsb_iso_packet_info inf;
				inf.total_len = iso->bytes_discarded;
				iso->host->driver->isoctl(iso, RECV_RELEASE,
546
							  (unsigned long)&inf);
B
Ben Collins 已提交
547 548 549
				iso->bytes_discarded = 0;
			}
		}
L
Linus Torvalds 已提交
550 551 552 553 554
	}
	spin_unlock_irqrestore(&iso->lock, flags);
	return rv;
}

555 556 557 558 559 560 561
/**
 * hpsb_iso_wake
 *
 * Available to low-level drivers.
 *
 * Call to wake waiting processes after buffer space has opened up.
 */
L
Linus Torvalds 已提交
562 563 564 565 566 567 568
void hpsb_iso_wake(struct hpsb_iso *iso)
{
	wake_up_interruptible(&iso->waitq);

	if (iso->callback)
		iso->callback(iso);
}