[PATCH] scsi: kill overeager "not-ready" messages
[linux-2.6.22.y-op.git] / drivers / ieee1394 / highlevel.c
blob694da82d820b5c7012d004dd14fa1c31bb934bce
1 /*
2 * IEEE 1394 for Linux
4 * Copyright (C) 1999 Andreas E. Bombe
6 * This code is licensed under the GPL. See the file COPYING in the root
7 * directory of the kernel sources for details.
10 * Contributions:
12 * Christian Toegel <christian.toegel@gmx.at>
13 * unregister address space
15 * Manfred Weihs <weihs@ict.tuwien.ac.at>
16 * unregister address space
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/bitops.h>
24 #include "ieee1394.h"
25 #include "ieee1394_types.h"
26 #include "hosts.h"
27 #include "ieee1394_core.h"
28 #include "highlevel.h"
29 #include "nodemgr.h"
32 struct hl_host_info {
33 struct list_head list;
34 struct hpsb_host *host;
35 size_t size;
36 unsigned long key;
37 void *data;
41 static LIST_HEAD(hl_drivers);
42 static DECLARE_RWSEM(hl_drivers_sem);
44 static LIST_HEAD(hl_irqs);
45 static DEFINE_RWLOCK(hl_irqs_lock);
47 static DEFINE_RWLOCK(addr_space_lock);
49 /* addr_space list will have zero and max already included as bounds */
50 static struct hpsb_address_ops dummy_ops = { NULL, NULL, NULL, NULL };
51 static struct hpsb_address_serve dummy_zero_addr, dummy_max_addr;
54 static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
55 struct hpsb_host *host)
57 struct hl_host_info *hi = NULL;
59 if (!hl || !host)
60 return NULL;
62 read_lock(&hl->host_info_lock);
63 list_for_each_entry(hi, &hl->host_info_list, list) {
64 if (hi->host == host) {
65 read_unlock(&hl->host_info_lock);
66 return hi;
69 read_unlock(&hl->host_info_lock);
70 return NULL;
73 /* Returns a per host/driver data structure that was previously stored by
74 * hpsb_create_hostinfo. */
75 void *hpsb_get_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
77 struct hl_host_info *hi = hl_get_hostinfo(hl, host);
79 return hi ? hi->data : NULL;
82 /* If size is zero, then the return here is only valid for error checking */
83 void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
84 size_t data_size)
86 struct hl_host_info *hi;
87 void *data;
88 unsigned long flags;
90 hi = hl_get_hostinfo(hl, host);
91 if (hi) {
92 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already"
93 " exists", hl->name);
94 return NULL;
97 hi = kzalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
98 if (!hi)
99 return NULL;
101 if (data_size) {
102 data = hi->data = hi + 1;
103 hi->size = data_size;
104 } else
105 data = hi;
107 hi->host = host;
109 write_lock_irqsave(&hl->host_info_lock, flags);
110 list_add_tail(&hi->list, &hl->host_info_list);
111 write_unlock_irqrestore(&hl->host_info_lock, flags);
113 return data;
116 int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
117 void *data)
119 struct hl_host_info *hi;
121 hi = hl_get_hostinfo(hl, host);
122 if (hi) {
123 if (!hi->size && !hi->data) {
124 hi->data = data;
125 return 0;
126 } else
127 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo "
128 "already has data", hl->name);
129 } else
130 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
131 hl->name);
132 return -EINVAL;
135 void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
137 struct hl_host_info *hi;
139 hi = hl_get_hostinfo(hl, host);
140 if (hi) {
141 unsigned long flags;
142 write_lock_irqsave(&hl->host_info_lock, flags);
143 list_del(&hi->list);
144 write_unlock_irqrestore(&hl->host_info_lock, flags);
145 kfree(hi);
147 return;
150 void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host,
151 unsigned long key)
153 struct hl_host_info *hi;
155 hi = hl_get_hostinfo(hl, host);
156 if (hi)
157 hi->key = key;
158 return;
161 void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
163 struct hl_host_info *hi;
164 void *data = NULL;
166 if (!hl)
167 return NULL;
169 read_lock(&hl->host_info_lock);
170 list_for_each_entry(hi, &hl->host_info_list, list) {
171 if (hi->key == key) {
172 data = hi->data;
173 break;
176 read_unlock(&hl->host_info_lock);
177 return data;
180 static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
182 struct hpsb_highlevel *hl = __data;
184 hl->add_host(host);
186 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
187 HPSB_ERR("Failed to generate Configuration ROM image for host "
188 "%s-%d", hl->name, host->id);
189 return 0;
192 void hpsb_register_highlevel(struct hpsb_highlevel *hl)
194 unsigned long flags;
196 INIT_LIST_HEAD(&hl->addr_list);
197 INIT_LIST_HEAD(&hl->host_info_list);
199 rwlock_init(&hl->host_info_lock);
201 down_write(&hl_drivers_sem);
202 list_add_tail(&hl->hl_list, &hl_drivers);
203 up_write(&hl_drivers_sem);
205 write_lock_irqsave(&hl_irqs_lock, flags);
206 list_add_tail(&hl->irq_list, &hl_irqs);
207 write_unlock_irqrestore(&hl_irqs_lock, flags);
209 if (hl->add_host)
210 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
211 return;
214 static void __delete_addr(struct hpsb_address_serve *as)
216 list_del(&as->host_list);
217 list_del(&as->hl_list);
218 kfree(as);
221 static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host,
222 int update_cr)
224 unsigned long flags;
225 struct list_head *lh, *next;
226 struct hpsb_address_serve *as;
228 /* First, let the highlevel driver unreg */
229 if (hl->remove_host)
230 hl->remove_host(host);
232 /* Remove any addresses that are matched for this highlevel driver
233 * and this particular host. */
234 write_lock_irqsave(&addr_space_lock, flags);
235 list_for_each_safe (lh, next, &hl->addr_list) {
236 as = list_entry(lh, struct hpsb_address_serve, hl_list);
237 if (as->host == host)
238 __delete_addr(as);
240 write_unlock_irqrestore(&addr_space_lock, flags);
242 /* Now update the config-rom to reflect anything removed by the
243 * highlevel driver. */
244 if (update_cr && host->update_config_rom &&
245 hpsb_update_config_rom_image(host) < 0)
246 HPSB_ERR("Failed to generate Configuration ROM image for host "
247 "%s-%d", hl->name, host->id);
249 /* Finally remove all the host info associated between these two. */
250 hpsb_destroy_hostinfo(hl, host);
253 static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
255 struct hpsb_highlevel *hl = __data;
257 __unregister_host(hl, host, 1);
258 return 0;
261 void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
263 unsigned long flags;
265 write_lock_irqsave(&hl_irqs_lock, flags);
266 list_del(&hl->irq_list);
267 write_unlock_irqrestore(&hl_irqs_lock, flags);
269 down_write(&hl_drivers_sem);
270 list_del(&hl->hl_list);
271 up_write(&hl_drivers_sem);
273 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
276 u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
277 struct hpsb_host *host,
278 struct hpsb_address_ops *ops,
279 u64 size, u64 alignment,
280 u64 start, u64 end)
282 struct hpsb_address_serve *as, *a1, *a2;
283 struct list_head *entry;
284 u64 retval = CSR1212_INVALID_ADDR_SPACE;
285 unsigned long flags;
286 u64 align_mask = ~(alignment - 1);
288 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
289 (hweight64(alignment) != 1)) {
290 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
291 __FUNCTION__, (unsigned long long)alignment);
292 return retval;
295 /* default range,
296 * avoids controller's posted write area (see OHCI 1.1 clause 1.5) */
297 if (start == CSR1212_INVALID_ADDR_SPACE &&
298 end == CSR1212_INVALID_ADDR_SPACE) {
299 start = host->middle_addr_space;
300 end = CSR1212_ALL_SPACE_END;
303 if (((start|end) & ~align_mask) || (start >= end) ||
304 (end > CSR1212_ALL_SPACE_END)) {
305 HPSB_ERR("%s called with invalid addresses "
306 "(start = %012Lx end = %012Lx)", __FUNCTION__,
307 (unsigned long long)start,(unsigned long long)end);
308 return retval;
311 as = kmalloc(sizeof(*as), GFP_KERNEL);
312 if (!as)
313 return retval;
315 INIT_LIST_HEAD(&as->host_list);
316 INIT_LIST_HEAD(&as->hl_list);
317 as->op = ops;
318 as->host = host;
320 write_lock_irqsave(&addr_space_lock, flags);
321 list_for_each(entry, &host->addr_space) {
322 u64 a1sa, a1ea;
323 u64 a2sa, a2ea;
325 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
326 a2 = list_entry(entry->next, struct hpsb_address_serve,
327 host_list);
329 a1sa = a1->start & align_mask;
330 a1ea = (a1->end + alignment -1) & align_mask;
331 a2sa = a2->start & align_mask;
332 a2ea = (a2->end + alignment -1) & align_mask;
334 if ((a2sa - a1ea >= size) && (a2sa - start >= size) &&
335 (a2sa > start)) {
336 as->start = max(start, a1ea);
337 as->end = as->start + size;
338 list_add(&as->host_list, entry);
339 list_add_tail(&as->hl_list, &hl->addr_list);
340 retval = as->start;
341 break;
344 write_unlock_irqrestore(&addr_space_lock, flags);
346 if (retval == CSR1212_INVALID_ADDR_SPACE)
347 kfree(as);
348 return retval;
351 int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
352 struct hpsb_address_ops *ops, u64 start, u64 end)
354 struct hpsb_address_serve *as;
355 struct list_head *lh;
356 int retval = 0;
357 unsigned long flags;
359 if (((start|end) & 3) || (start >= end) ||
360 (end > CSR1212_ALL_SPACE_END)) {
361 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
362 return 0;
365 as = kmalloc(sizeof(*as), GFP_ATOMIC);
366 if (!as)
367 return 0;
369 INIT_LIST_HEAD(&as->host_list);
370 INIT_LIST_HEAD(&as->hl_list);
371 as->op = ops;
372 as->start = start;
373 as->end = end;
374 as->host = host;
376 write_lock_irqsave(&addr_space_lock, flags);
377 list_for_each(lh, &host->addr_space) {
378 struct hpsb_address_serve *as_this =
379 list_entry(lh, struct hpsb_address_serve, host_list);
380 struct hpsb_address_serve *as_next =
381 list_entry(lh->next, struct hpsb_address_serve,
382 host_list);
384 if (as_this->end > as->start)
385 break;
387 if (as_next->start >= as->end) {
388 list_add(&as->host_list, lh);
389 list_add_tail(&as->hl_list, &hl->addr_list);
390 retval = 1;
391 break;
394 write_unlock_irqrestore(&addr_space_lock, flags);
396 if (retval == 0)
397 kfree(as);
398 return retval;
401 int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
402 u64 start)
404 int retval = 0;
405 struct hpsb_address_serve *as;
406 struct list_head *lh, *next;
407 unsigned long flags;
409 write_lock_irqsave(&addr_space_lock, flags);
410 list_for_each_safe (lh, next, &hl->addr_list) {
411 as = list_entry(lh, struct hpsb_address_serve, hl_list);
412 if (as->start == start && as->host == host) {
413 __delete_addr(as);
414 retval = 1;
415 break;
418 write_unlock_irqrestore(&addr_space_lock, flags);
419 return retval;
422 int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
423 unsigned int channel)
425 if (channel > 63) {
426 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
427 return -EINVAL;
429 if (host->iso_listen_count[channel]++ == 0)
430 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
431 return 0;
434 void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
435 unsigned int channel)
437 if (channel > 63) {
438 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
439 return;
441 if (--host->iso_listen_count[channel] == 0)
442 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
445 static void init_hpsb_highlevel(struct hpsb_host *host)
447 INIT_LIST_HEAD(&dummy_zero_addr.host_list);
448 INIT_LIST_HEAD(&dummy_zero_addr.hl_list);
449 INIT_LIST_HEAD(&dummy_max_addr.host_list);
450 INIT_LIST_HEAD(&dummy_max_addr.hl_list);
452 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
454 dummy_zero_addr.start = dummy_zero_addr.end = 0;
455 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
457 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
458 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
461 void highlevel_add_host(struct hpsb_host *host)
463 struct hpsb_highlevel *hl;
465 init_hpsb_highlevel(host);
467 down_read(&hl_drivers_sem);
468 list_for_each_entry(hl, &hl_drivers, hl_list) {
469 if (hl->add_host)
470 hl->add_host(host);
472 up_read(&hl_drivers_sem);
473 if (host->update_config_rom && hpsb_update_config_rom_image(host) < 0)
474 HPSB_ERR("Failed to generate Configuration ROM image for host "
475 "%s-%d", hl->name, host->id);
478 void highlevel_remove_host(struct hpsb_host *host)
480 struct hpsb_highlevel *hl;
482 down_read(&hl_drivers_sem);
483 list_for_each_entry(hl, &hl_drivers, hl_list)
484 __unregister_host(hl, host, 0);
485 up_read(&hl_drivers_sem);
488 void highlevel_host_reset(struct hpsb_host *host)
490 unsigned long flags;
491 struct hpsb_highlevel *hl;
493 read_lock_irqsave(&hl_irqs_lock, flags);
494 list_for_each_entry(hl, &hl_irqs, irq_list) {
495 if (hl->host_reset)
496 hl->host_reset(host);
498 read_unlock_irqrestore(&hl_irqs_lock, flags);
501 void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
503 unsigned long flags;
504 struct hpsb_highlevel *hl;
505 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
507 read_lock_irqsave(&hl_irqs_lock, flags);
508 list_for_each_entry(hl, &hl_irqs, irq_list) {
509 if (hl->iso_receive)
510 hl->iso_receive(host, channel, data, length);
512 read_unlock_irqrestore(&hl_irqs_lock, flags);
515 void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
516 void *data, size_t length)
518 unsigned long flags;
519 struct hpsb_highlevel *hl;
520 int cts = ((quadlet_t *)data)[0] >> 4;
522 read_lock_irqsave(&hl_irqs_lock, flags);
523 list_for_each_entry(hl, &hl_irqs, irq_list) {
524 if (hl->fcp_request)
525 hl->fcp_request(host, nodeid, direction, cts, data,
526 length);
528 read_unlock_irqrestore(&hl_irqs_lock, flags);
531 int highlevel_read(struct hpsb_host *host, int nodeid, void *data, u64 addr,
532 unsigned int length, u16 flags)
534 struct hpsb_address_serve *as;
535 unsigned int partlength;
536 int rcode = RCODE_ADDRESS_ERROR;
538 read_lock(&addr_space_lock);
539 list_for_each_entry(as, &host->addr_space, host_list) {
540 if (as->start > addr)
541 break;
543 if (as->end > addr) {
544 partlength = min(as->end - addr, (u64) length);
546 if (as->op->read)
547 rcode = as->op->read(host, nodeid, data,
548 addr, partlength, flags);
549 else
550 rcode = RCODE_TYPE_ERROR;
552 data += partlength;
553 length -= partlength;
554 addr += partlength;
556 if ((rcode != RCODE_COMPLETE) || !length)
557 break;
560 read_unlock(&addr_space_lock);
562 if (length && (rcode == RCODE_COMPLETE))
563 rcode = RCODE_ADDRESS_ERROR;
564 return rcode;
567 int highlevel_write(struct hpsb_host *host, int nodeid, int destid, void *data,
568 u64 addr, unsigned int length, u16 flags)
570 struct hpsb_address_serve *as;
571 unsigned int partlength;
572 int rcode = RCODE_ADDRESS_ERROR;
574 read_lock(&addr_space_lock);
575 list_for_each_entry(as, &host->addr_space, host_list) {
576 if (as->start > addr)
577 break;
579 if (as->end > addr) {
580 partlength = min(as->end - addr, (u64) length);
582 if (as->op->write)
583 rcode = as->op->write(host, nodeid, destid,
584 data, addr, partlength,
585 flags);
586 else
587 rcode = RCODE_TYPE_ERROR;
589 data += partlength;
590 length -= partlength;
591 addr += partlength;
593 if ((rcode != RCODE_COMPLETE) || !length)
594 break;
597 read_unlock(&addr_space_lock);
599 if (length && (rcode == RCODE_COMPLETE))
600 rcode = RCODE_ADDRESS_ERROR;
601 return rcode;
604 int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
605 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode,
606 u16 flags)
608 struct hpsb_address_serve *as;
609 int rcode = RCODE_ADDRESS_ERROR;
611 read_lock(&addr_space_lock);
612 list_for_each_entry(as, &host->addr_space, host_list) {
613 if (as->start > addr)
614 break;
616 if (as->end > addr) {
617 if (as->op->lock)
618 rcode = as->op->lock(host, nodeid, store, addr,
619 data, arg, ext_tcode,
620 flags);
621 else
622 rcode = RCODE_TYPE_ERROR;
623 break;
626 read_unlock(&addr_space_lock);
627 return rcode;
630 int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
631 u64 addr, octlet_t data, octlet_t arg, int ext_tcode,
632 u16 flags)
634 struct hpsb_address_serve *as;
635 int rcode = RCODE_ADDRESS_ERROR;
637 read_lock(&addr_space_lock);
639 list_for_each_entry(as, &host->addr_space, host_list) {
640 if (as->start > addr)
641 break;
643 if (as->end > addr) {
644 if (as->op->lock64)
645 rcode = as->op->lock64(host, nodeid, store,
646 addr, data, arg,
647 ext_tcode, flags);
648 else
649 rcode = RCODE_TYPE_ERROR;
650 break;
653 read_unlock(&addr_space_lock);
654 return rcode;