[IA64-SGI] Check for TIO errors on shub2 Altix
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ieee1394 / iso.c
blob08bd15d2a7b6cea63a675664ea333e9e0837b376
1 /*
2 * IEEE 1394 for Linux
4 * kernel ISO transmission/reception
6 * Copyright (C) 2002 Maas Digital LLC
8 * This code is licensed under the GPL. See the file COPYING in the root
9 * directory of the kernel sources for details.
12 #include <linux/pci.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
16 #include "hosts.h"
17 #include "iso.h"
19 void hpsb_iso_stop(struct hpsb_iso *iso)
21 if (!(iso->flags & HPSB_ISO_DRIVER_STARTED))
22 return;
24 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
25 XMIT_STOP : RECV_STOP, 0);
26 iso->flags &= ~HPSB_ISO_DRIVER_STARTED;
29 void hpsb_iso_shutdown(struct hpsb_iso *iso)
31 if (iso->flags & HPSB_ISO_DRIVER_INIT) {
32 hpsb_iso_stop(iso);
33 iso->host->driver->isoctl(iso, iso->type == HPSB_ISO_XMIT ?
34 XMIT_SHUTDOWN : RECV_SHUTDOWN, 0);
35 iso->flags &= ~HPSB_ISO_DRIVER_INIT;
38 dma_region_free(&iso->data_buf);
39 kfree(iso);
42 static struct hpsb_iso *hpsb_iso_common_init(struct hpsb_host *host,
43 enum hpsb_iso_type type,
44 unsigned int data_buf_size,
45 unsigned int buf_packets,
46 int channel, int dma_mode,
47 int irq_interval,
48 void (*callback) (struct hpsb_iso
49 *))
51 struct hpsb_iso *iso;
52 int dma_direction;
54 /* make sure driver supports the ISO API */
55 if (!host->driver->isoctl) {
56 printk(KERN_INFO
57 "ieee1394: host driver '%s' does not support the rawiso API\n",
58 host->driver->name);
59 return NULL;
62 /* sanitize parameters */
64 if (buf_packets < 2)
65 buf_packets = 2;
67 if ((dma_mode < HPSB_ISO_DMA_DEFAULT)
68 || (dma_mode > HPSB_ISO_DMA_PACKET_PER_BUFFER))
69 dma_mode = HPSB_ISO_DMA_DEFAULT;
71 if ((irq_interval < 0) || (irq_interval > buf_packets / 4))
72 irq_interval = buf_packets / 4;
73 if (irq_interval == 0) /* really interrupt for each packet */
74 irq_interval = 1;
76 if (channel < -1 || channel >= 64)
77 return NULL;
79 /* channel = -1 is OK for multi-channel recv but not for xmit */
80 if (type == HPSB_ISO_XMIT && channel < 0)
81 return NULL;
83 /* allocate and write the struct hpsb_iso */
85 iso =
86 kmalloc(sizeof(*iso) +
87 buf_packets * sizeof(struct hpsb_iso_packet_info),
88 GFP_KERNEL);
89 if (!iso)
90 return NULL;
92 iso->infos = (struct hpsb_iso_packet_info *)(iso + 1);
94 iso->type = type;
95 iso->host = host;
96 iso->hostdata = NULL;
97 iso->callback = callback;
98 init_waitqueue_head(&iso->waitq);
99 iso->channel = channel;
100 iso->irq_interval = irq_interval;
101 iso->dma_mode = dma_mode;
102 dma_region_init(&iso->data_buf);
103 iso->buf_size = PAGE_ALIGN(data_buf_size);
104 iso->buf_packets = buf_packets;
105 iso->pkt_dma = 0;
106 iso->first_packet = 0;
107 spin_lock_init(&iso->lock);
109 if (iso->type == HPSB_ISO_XMIT) {
110 iso->n_ready_packets = iso->buf_packets;
111 dma_direction = PCI_DMA_TODEVICE;
112 } else {
113 iso->n_ready_packets = 0;
114 dma_direction = PCI_DMA_FROMDEVICE;
117 atomic_set(&iso->overflows, 0);
118 iso->bytes_discarded = 0;
119 iso->flags = 0;
120 iso->prebuffer = 0;
122 /* allocate the packet buffer */
123 if (dma_region_alloc
124 (&iso->data_buf, iso->buf_size, host->pdev, dma_direction))
125 goto err;
127 return iso;
129 err:
130 hpsb_iso_shutdown(iso);
131 return NULL;
134 int hpsb_iso_n_ready(struct hpsb_iso *iso)
136 unsigned long flags;
137 int val;
139 spin_lock_irqsave(&iso->lock, flags);
140 val = iso->n_ready_packets;
141 spin_unlock_irqrestore(&iso->lock, flags);
143 return val;
146 struct hpsb_iso *hpsb_iso_xmit_init(struct hpsb_host *host,
147 unsigned int data_buf_size,
148 unsigned int buf_packets,
149 int channel,
150 int speed,
151 int irq_interval,
152 void (*callback) (struct hpsb_iso *))
154 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_XMIT,
155 data_buf_size, buf_packets,
156 channel,
157 HPSB_ISO_DMA_DEFAULT,
158 irq_interval, callback);
159 if (!iso)
160 return NULL;
162 iso->speed = speed;
164 /* tell the driver to start working */
165 if (host->driver->isoctl(iso, XMIT_INIT, 0))
166 goto err;
168 iso->flags |= HPSB_ISO_DRIVER_INIT;
169 return iso;
171 err:
172 hpsb_iso_shutdown(iso);
173 return NULL;
176 struct hpsb_iso *hpsb_iso_recv_init(struct hpsb_host *host,
177 unsigned int data_buf_size,
178 unsigned int buf_packets,
179 int channel,
180 int dma_mode,
181 int irq_interval,
182 void (*callback) (struct hpsb_iso *))
184 struct hpsb_iso *iso = hpsb_iso_common_init(host, HPSB_ISO_RECV,
185 data_buf_size, buf_packets,
186 channel, dma_mode,
187 irq_interval, callback);
188 if (!iso)
189 return NULL;
191 /* tell the driver to start working */
192 if (host->driver->isoctl(iso, RECV_INIT, 0))
193 goto err;
195 iso->flags |= HPSB_ISO_DRIVER_INIT;
196 return iso;
198 err:
199 hpsb_iso_shutdown(iso);
200 return NULL;
203 int hpsb_iso_recv_listen_channel(struct hpsb_iso *iso, unsigned char channel)
205 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
206 return -EINVAL;
207 return iso->host->driver->isoctl(iso, RECV_LISTEN_CHANNEL, channel);
210 int hpsb_iso_recv_unlisten_channel(struct hpsb_iso *iso, unsigned char channel)
212 if (iso->type != HPSB_ISO_RECV || iso->channel != -1 || channel >= 64)
213 return -EINVAL;
214 return iso->host->driver->isoctl(iso, RECV_UNLISTEN_CHANNEL, channel);
217 int hpsb_iso_recv_set_channel_mask(struct hpsb_iso *iso, u64 mask)
219 if (iso->type != HPSB_ISO_RECV || iso->channel != -1)
220 return -EINVAL;
221 return iso->host->driver->isoctl(iso, RECV_SET_CHANNEL_MASK,
222 (unsigned long)&mask);
225 int hpsb_iso_recv_flush(struct hpsb_iso *iso)
227 if (iso->type != HPSB_ISO_RECV)
228 return -EINVAL;
229 return iso->host->driver->isoctl(iso, RECV_FLUSH, 0);
232 static int do_iso_xmit_start(struct hpsb_iso *iso, int cycle)
234 int retval = iso->host->driver->isoctl(iso, XMIT_START, cycle);
235 if (retval)
236 return retval;
238 iso->flags |= HPSB_ISO_DRIVER_STARTED;
239 return retval;
242 int hpsb_iso_xmit_start(struct hpsb_iso *iso, int cycle, int prebuffer)
244 if (iso->type != HPSB_ISO_XMIT)
245 return -1;
247 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
248 return 0;
250 if (cycle < -1)
251 cycle = -1;
252 else if (cycle >= 8000)
253 cycle %= 8000;
255 iso->xmit_cycle = cycle;
257 if (prebuffer < 0)
258 prebuffer = iso->buf_packets - 1;
259 else if (prebuffer == 0)
260 prebuffer = 1;
262 if (prebuffer >= iso->buf_packets)
263 prebuffer = iso->buf_packets - 1;
265 iso->prebuffer = prebuffer;
267 /* remember the starting cycle; DMA will commence from xmit_queue_packets()
268 once enough packets have been buffered */
269 iso->start_cycle = cycle;
271 return 0;
274 int hpsb_iso_recv_start(struct hpsb_iso *iso, int cycle, int tag_mask, int sync)
276 int retval = 0;
277 int isoctl_args[3];
279 if (iso->type != HPSB_ISO_RECV)
280 return -1;
282 if (iso->flags & HPSB_ISO_DRIVER_STARTED)
283 return 0;
285 if (cycle < -1)
286 cycle = -1;
287 else if (cycle >= 8000)
288 cycle %= 8000;
290 isoctl_args[0] = cycle;
292 if (tag_mask < 0)
293 /* match all tags */
294 tag_mask = 0xF;
295 isoctl_args[1] = tag_mask;
297 isoctl_args[2] = sync;
299 retval =
300 iso->host->driver->isoctl(iso, RECV_START,
301 (unsigned long)&isoctl_args[0]);
302 if (retval)
303 return retval;
305 iso->flags |= HPSB_ISO_DRIVER_STARTED;
306 return retval;
309 /* check to make sure the user has not supplied bogus values of offset/len
310 that would cause the kernel to access memory outside the buffer */
312 static int hpsb_iso_check_offset_len(struct hpsb_iso *iso,
313 unsigned int offset, unsigned short len,
314 unsigned int *out_offset,
315 unsigned short *out_len)
317 if (offset >= iso->buf_size)
318 return -EFAULT;
320 /* make sure the packet does not go beyond the end of the buffer */
321 if (offset + len > iso->buf_size)
322 return -EFAULT;
324 /* check for wrap-around */
325 if (offset + len < offset)
326 return -EFAULT;
328 /* now we can trust 'offset' and 'length' */
329 *out_offset = offset;
330 *out_len = len;
332 return 0;
335 int hpsb_iso_xmit_queue_packet(struct hpsb_iso *iso, u32 offset, u16 len,
336 u8 tag, u8 sy)
338 struct hpsb_iso_packet_info *info;
339 unsigned long flags;
340 int rv;
342 if (iso->type != HPSB_ISO_XMIT)
343 return -EINVAL;
345 /* is there space in the buffer? */
346 if (iso->n_ready_packets <= 0) {
347 return -EBUSY;
350 info = &iso->infos[iso->first_packet];
352 /* check for bogus offset/length */
353 if (hpsb_iso_check_offset_len
354 (iso, offset, len, &info->offset, &info->len))
355 return -EFAULT;
357 info->tag = tag;
358 info->sy = sy;
360 spin_lock_irqsave(&iso->lock, flags);
362 rv = iso->host->driver->isoctl(iso, XMIT_QUEUE, (unsigned long)info);
363 if (rv)
364 goto out;
366 /* increment cursors */
367 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
368 iso->xmit_cycle = (iso->xmit_cycle + 1) % 8000;
369 iso->n_ready_packets--;
371 if (iso->prebuffer != 0) {
372 iso->prebuffer--;
373 if (iso->prebuffer <= 0) {
374 iso->prebuffer = 0;
375 rv = do_iso_xmit_start(iso, iso->start_cycle);
379 out:
380 spin_unlock_irqrestore(&iso->lock, flags);
381 return rv;
384 int hpsb_iso_xmit_sync(struct hpsb_iso *iso)
386 if (iso->type != HPSB_ISO_XMIT)
387 return -EINVAL;
389 return wait_event_interruptible(iso->waitq,
390 hpsb_iso_n_ready(iso) ==
391 iso->buf_packets);
394 void hpsb_iso_packet_sent(struct hpsb_iso *iso, int cycle, int error)
396 unsigned long flags;
397 spin_lock_irqsave(&iso->lock, flags);
399 /* predict the cycle of the next packet to be queued */
401 /* jump ahead by the number of packets that are already buffered */
402 cycle += iso->buf_packets - iso->n_ready_packets;
403 cycle %= 8000;
405 iso->xmit_cycle = cycle;
406 iso->n_ready_packets++;
407 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
409 if (iso->n_ready_packets == iso->buf_packets || error != 0) {
410 /* the buffer has run empty! */
411 atomic_inc(&iso->overflows);
414 spin_unlock_irqrestore(&iso->lock, flags);
417 void hpsb_iso_packet_received(struct hpsb_iso *iso, u32 offset, u16 len,
418 u16 total_len, u16 cycle, u8 channel, u8 tag,
419 u8 sy)
421 unsigned long flags;
422 spin_lock_irqsave(&iso->lock, flags);
424 if (iso->n_ready_packets == iso->buf_packets) {
425 /* overflow! */
426 atomic_inc(&iso->overflows);
427 /* Record size of this discarded packet */
428 iso->bytes_discarded += total_len;
429 } else {
430 struct hpsb_iso_packet_info *info = &iso->infos[iso->pkt_dma];
431 info->offset = offset;
432 info->len = len;
433 info->total_len = total_len;
434 info->cycle = cycle;
435 info->channel = channel;
436 info->tag = tag;
437 info->sy = sy;
439 iso->pkt_dma = (iso->pkt_dma + 1) % iso->buf_packets;
440 iso->n_ready_packets++;
443 spin_unlock_irqrestore(&iso->lock, flags);
446 int hpsb_iso_recv_release_packets(struct hpsb_iso *iso, unsigned int n_packets)
448 unsigned long flags;
449 unsigned int i;
450 int rv = 0;
452 if (iso->type != HPSB_ISO_RECV)
453 return -1;
455 spin_lock_irqsave(&iso->lock, flags);
456 for (i = 0; i < n_packets; i++) {
457 rv = iso->host->driver->isoctl(iso, RECV_RELEASE,
458 (unsigned long)&iso->infos[iso->
459 first_packet]);
460 if (rv)
461 break;
463 iso->first_packet = (iso->first_packet + 1) % iso->buf_packets;
464 iso->n_ready_packets--;
466 /* release memory from packets discarded when queue was full */
467 if (iso->n_ready_packets == 0) { /* Release only after all prior packets handled */
468 if (iso->bytes_discarded != 0) {
469 struct hpsb_iso_packet_info inf;
470 inf.total_len = iso->bytes_discarded;
471 iso->host->driver->isoctl(iso, RECV_RELEASE,
472 (unsigned long)&inf);
473 iso->bytes_discarded = 0;
477 spin_unlock_irqrestore(&iso->lock, flags);
478 return rv;
481 void hpsb_iso_wake(struct hpsb_iso *iso)
483 wake_up_interruptible(&iso->waitq);
485 if (iso->callback)
486 iso->callback(iso);