blob: 997e1bf6297f8b1d1a6f797594bdeda16d1e7183 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IEEE 1394 for Linux
3 *
4 * Copyright (C) 1999 Andreas E. Bombe
5 *
6 * This code is licensed under the GPL. See the file COPYING in the root
7 * directory of the kernel sources for details.
8 *
9 *
10 * Contributions:
11 *
12 * Christian Toegel <christian.toegel@gmx.at>
13 * unregister address space
14 *
15 * Manfred Weihs <weihs@ict.tuwien.ac.at>
16 * unregister address space
17 *
18 */
19
20#include <linux/config.h>
21#include <linux/slab.h>
22#include <linux/list.h>
23#include <linux/bitops.h>
24
25#include "ieee1394.h"
26#include "ieee1394_types.h"
27#include "hosts.h"
28#include "ieee1394_core.h"
29#include "highlevel.h"
30#include "nodemgr.h"
31
32
33struct hl_host_info {
34 struct list_head list;
35 struct hpsb_host *host;
36 size_t size;
37 unsigned long key;
38 void *data;
39};
40
41
42static LIST_HEAD(hl_drivers);
43static DECLARE_RWSEM(hl_drivers_sem);
44
45static LIST_HEAD(hl_irqs);
46static DEFINE_RWLOCK(hl_irqs_lock);
47
48static DEFINE_RWLOCK(addr_space_lock);
49
50/* addr_space list will have zero and max already included as bounds */
51static struct hpsb_address_ops dummy_ops = { NULL, NULL, NULL, NULL };
52static struct hpsb_address_serve dummy_zero_addr, dummy_max_addr;
53
54
55static struct hl_host_info *hl_get_hostinfo(struct hpsb_highlevel *hl,
56 struct hpsb_host *host)
57{
58 struct hl_host_info *hi = NULL;
59
60 if (!hl || !host)
61 return NULL;
62
63 read_lock(&hl->host_info_lock);
64 list_for_each_entry(hi, &hl->host_info_list, list) {
65 if (hi->host == host) {
66 read_unlock(&hl->host_info_lock);
67 return hi;
68 }
69 }
70 read_unlock(&hl->host_info_lock);
71
72 return NULL;
73}
74
75
76/* Returns a per host/driver data structure that was previously stored by
77 * hpsb_create_hostinfo. */
78void *hpsb_get_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
79{
80 struct hl_host_info *hi = hl_get_hostinfo(hl, host);
81
82 if (hi)
83 return hi->data;
84
85 return NULL;
86}
87
88
89/* If size is zero, then the return here is only valid for error checking */
90void *hpsb_create_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
91 size_t data_size)
92{
93 struct hl_host_info *hi;
94 void *data;
95 unsigned long flags;
96
97 hi = hl_get_hostinfo(hl, host);
98 if (hi) {
99 HPSB_ERR("%s called hpsb_create_hostinfo when hostinfo already exists",
100 hl->name);
101 return NULL;
102 }
103
104 hi = kmalloc(sizeof(*hi) + data_size, GFP_ATOMIC);
105 if (!hi)
106 return NULL;
107
108 memset(hi, 0, sizeof(*hi) + data_size);
109
110 if (data_size) {
111 data = hi->data = hi + 1;
112 hi->size = data_size;
113 } else
114 data = hi;
115
116 hi->host = host;
117
118 write_lock_irqsave(&hl->host_info_lock, flags);
119 list_add_tail(&hi->list, &hl->host_info_list);
120 write_unlock_irqrestore(&hl->host_info_lock, flags);
121
122 return data;
123}
124
125
126int hpsb_set_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host,
127 void *data)
128{
129 struct hl_host_info *hi;
130
131 hi = hl_get_hostinfo(hl, host);
132 if (hi) {
133 if (!hi->size && !hi->data) {
134 hi->data = data;
135 return 0;
136 } else
137 HPSB_ERR("%s called hpsb_set_hostinfo when hostinfo already has data",
138 hl->name);
139 } else
140 HPSB_ERR("%s called hpsb_set_hostinfo when no hostinfo exists",
141 hl->name);
142
143 return -EINVAL;
144}
145
146
147void hpsb_destroy_hostinfo(struct hpsb_highlevel *hl, struct hpsb_host *host)
148{
149 struct hl_host_info *hi;
150
151 hi = hl_get_hostinfo(hl, host);
152 if (hi) {
153 unsigned long flags;
154 write_lock_irqsave(&hl->host_info_lock, flags);
155 list_del(&hi->list);
156 write_unlock_irqrestore(&hl->host_info_lock, flags);
157 kfree(hi);
158 }
159
160 return;
161}
162
163
164void hpsb_set_hostinfo_key(struct hpsb_highlevel *hl, struct hpsb_host *host, unsigned long key)
165{
166 struct hl_host_info *hi;
167
168 hi = hl_get_hostinfo(hl, host);
169 if (hi)
170 hi->key = key;
171
172 return;
173}
174
175
176void *hpsb_get_hostinfo_bykey(struct hpsb_highlevel *hl, unsigned long key)
177{
178 struct hl_host_info *hi;
179 void *data = NULL;
180
181 if (!hl)
182 return NULL;
183
184 read_lock(&hl->host_info_lock);
185 list_for_each_entry(hi, &hl->host_info_list, list) {
186 if (hi->key == key) {
187 data = hi->data;
188 break;
189 }
190 }
191 read_unlock(&hl->host_info_lock);
192
193 return data;
194}
195
196
197static int highlevel_for_each_host_reg(struct hpsb_host *host, void *__data)
198{
199 struct hpsb_highlevel *hl = __data;
200
201 hl->add_host(host);
202
203 if (host->update_config_rom) {
204 if (hpsb_update_config_rom_image(host) < 0) {
205 HPSB_ERR("Failed to generate Configuration ROM image for host "
206 "%s-%d", hl->name, host->id);
207 }
208 }
209
210 return 0;
211}
212
213void hpsb_register_highlevel(struct hpsb_highlevel *hl)
214{
215 INIT_LIST_HEAD(&hl->addr_list);
216 INIT_LIST_HEAD(&hl->host_info_list);
217
218 rwlock_init(&hl->host_info_lock);
219
220 down_write(&hl_drivers_sem);
221 list_add_tail(&hl->hl_list, &hl_drivers);
222 up_write(&hl_drivers_sem);
223
224 write_lock(&hl_irqs_lock);
225 list_add_tail(&hl->irq_list, &hl_irqs);
226 write_unlock(&hl_irqs_lock);
227
228 if (hl->add_host)
229 nodemgr_for_each_host(hl, highlevel_for_each_host_reg);
230
231 return;
232}
233
234static void __delete_addr(struct hpsb_address_serve *as)
235{
236 list_del(&as->host_list);
237 list_del(&as->hl_list);
238 kfree(as);
239}
240
241static void __unregister_host(struct hpsb_highlevel *hl, struct hpsb_host *host, int update_cr)
242{
243 unsigned long flags;
244 struct list_head *lh, *next;
245 struct hpsb_address_serve *as;
246
247 /* First, let the highlevel driver unreg */
248 if (hl->remove_host)
249 hl->remove_host(host);
250
251 /* Remove any addresses that are matched for this highlevel driver
252 * and this particular host. */
253 write_lock_irqsave(&addr_space_lock, flags);
254 list_for_each_safe (lh, next, &hl->addr_list) {
255 as = list_entry(lh, struct hpsb_address_serve, hl_list);
256
257 if (as->host == host)
258 __delete_addr(as);
259 }
260 write_unlock_irqrestore(&addr_space_lock, flags);
261
262 /* Now update the config-rom to reflect anything removed by the
263 * highlevel driver. */
264 if (update_cr && host->update_config_rom) {
265 if (hpsb_update_config_rom_image(host) < 0) {
266 HPSB_ERR("Failed to generate Configuration ROM image for host "
267 "%s-%d", hl->name, host->id);
268 }
269 }
270
271 /* And finally, remove all the host info associated between these
272 * two. */
273 hpsb_destroy_hostinfo(hl, host);
274}
275
276static int highlevel_for_each_host_unreg(struct hpsb_host *host, void *__data)
277{
278 struct hpsb_highlevel *hl = __data;
279
280 __unregister_host(hl, host, 1);
281
282 return 0;
283}
284
285void hpsb_unregister_highlevel(struct hpsb_highlevel *hl)
286{
287 write_lock(&hl_irqs_lock);
288 list_del(&hl->irq_list);
289 write_unlock(&hl_irqs_lock);
290
291 down_write(&hl_drivers_sem);
292 list_del(&hl->hl_list);
293 up_write(&hl_drivers_sem);
294
295 nodemgr_for_each_host(hl, highlevel_for_each_host_unreg);
296}
297
298u64 hpsb_allocate_and_register_addrspace(struct hpsb_highlevel *hl,
299 struct hpsb_host *host,
300 struct hpsb_address_ops *ops,
301 u64 size, u64 alignment,
302 u64 start, u64 end)
303{
304 struct hpsb_address_serve *as, *a1, *a2;
305 struct list_head *entry;
306 u64 retval = ~0ULL;
307 unsigned long flags;
308 u64 align_mask = ~(alignment - 1);
309
310 if ((alignment & 3) || (alignment > 0x800000000000ULL) ||
311 ((hweight32(alignment >> 32) +
312 hweight32(alignment & 0xffffffff) != 1))) {
313 HPSB_ERR("%s called with invalid alignment: 0x%048llx",
314 __FUNCTION__, (unsigned long long)alignment);
315 return retval;
316 }
317
318 if (start == ~0ULL && end == ~0ULL) {
319 start = CSR1212_ALL_SPACE_BASE + 0xffff00000000ULL; /* ohci1394.c limit */
320 end = CSR1212_ALL_SPACE_END;
321 }
322
323 if (((start|end) & ~align_mask) || (start >= end) || (end > 0x1000000000000ULL)) {
324 HPSB_ERR("%s called with invalid addresses (start = %012Lx end = %012Lx)",
325 __FUNCTION__, (unsigned long long)start, (unsigned long long)end);
326 return retval;
327 }
328
329 as = (struct hpsb_address_serve *)
330 kmalloc(sizeof(struct hpsb_address_serve), GFP_KERNEL);
331 if (as == NULL) {
332 return retval;
333 }
334
335 INIT_LIST_HEAD(&as->host_list);
336 INIT_LIST_HEAD(&as->hl_list);
337 as->op = ops;
338 as->host = host;
339
340 write_lock_irqsave(&addr_space_lock, flags);
341
342 list_for_each(entry, &host->addr_space) {
343 u64 a1sa, a1ea;
344 u64 a2sa, a2ea;
345
346 a1 = list_entry(entry, struct hpsb_address_serve, host_list);
347 a2 = list_entry(entry->next, struct hpsb_address_serve, host_list);
348
349 a1sa = a1->start & align_mask;
350 a1ea = (a1->end + alignment -1) & align_mask;
351 a2sa = a2->start & align_mask;
352 a2ea = (a2->end + alignment -1) & align_mask;
353
354 if ((a2sa - a1ea >= size) && (a2sa - start >= size) && (a2sa > start)) {
355 as->start = max(start, a1ea);
356 as->end = as->start + size;
357 list_add(&as->host_list, entry);
358 list_add_tail(&as->hl_list, &hl->addr_list);
359 retval = as->start;
360 break;
361 }
362 }
363
364 write_unlock_irqrestore(&addr_space_lock, flags);
365
366 if (retval == ~0ULL) {
367 kfree(as);
368 }
369
370 return retval;
371}
372
373int hpsb_register_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
374 struct hpsb_address_ops *ops, u64 start, u64 end)
375{
376 struct hpsb_address_serve *as;
377 struct list_head *lh;
378 int retval = 0;
379 unsigned long flags;
380
381 if (((start|end) & 3) || (start >= end) || (end > 0x1000000000000ULL)) {
382 HPSB_ERR("%s called with invalid addresses", __FUNCTION__);
383 return 0;
384 }
385
386 as = (struct hpsb_address_serve *)
387 kmalloc(sizeof(struct hpsb_address_serve), GFP_ATOMIC);
388 if (as == NULL) {
389 return 0;
390 }
391
392 INIT_LIST_HEAD(&as->host_list);
393 INIT_LIST_HEAD(&as->hl_list);
394 as->op = ops;
395 as->start = start;
396 as->end = end;
397 as->host = host;
398
399 write_lock_irqsave(&addr_space_lock, flags);
400
401 list_for_each(lh, &host->addr_space) {
402 struct hpsb_address_serve *as_this =
403 list_entry(lh, struct hpsb_address_serve, host_list);
404 struct hpsb_address_serve *as_next =
405 list_entry(lh->next, struct hpsb_address_serve, host_list);
406
407 if (as_this->end > as->start)
408 break;
409
410 if (as_next->start >= as->end) {
411 list_add(&as->host_list, lh);
412 list_add_tail(&as->hl_list, &hl->addr_list);
413 retval = 1;
414 break;
415 }
416 }
417 write_unlock_irqrestore(&addr_space_lock, flags);
418
419 if (retval == 0)
420 kfree(as);
421
422 return retval;
423}
424
425int hpsb_unregister_addrspace(struct hpsb_highlevel *hl, struct hpsb_host *host,
426 u64 start)
427{
428 int retval = 0;
429 struct hpsb_address_serve *as;
430 struct list_head *lh, *next;
431 unsigned long flags;
432
433 write_lock_irqsave(&addr_space_lock, flags);
434
435 list_for_each_safe (lh, next, &hl->addr_list) {
436 as = list_entry(lh, struct hpsb_address_serve, hl_list);
437 if (as->start == start && as->host == host) {
438 __delete_addr(as);
439 retval = 1;
440 break;
441 }
442 }
443
444 write_unlock_irqrestore(&addr_space_lock, flags);
445
446 return retval;
447}
448
449int hpsb_listen_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
450 unsigned int channel)
451{
452 if (channel > 63) {
453 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
454 return -EINVAL;
455 }
456
457 if (host->iso_listen_count[channel]++ == 0) {
458 return host->driver->devctl(host, ISO_LISTEN_CHANNEL, channel);
459 }
460
461 return 0;
462}
463
464void hpsb_unlisten_channel(struct hpsb_highlevel *hl, struct hpsb_host *host,
465 unsigned int channel)
466{
467 if (channel > 63) {
468 HPSB_ERR("%s called with invalid channel", __FUNCTION__);
469 return;
470 }
471
472 if (--host->iso_listen_count[channel] == 0) {
473 host->driver->devctl(host, ISO_UNLISTEN_CHANNEL, channel);
474 }
475}
476
477static void init_hpsb_highlevel(struct hpsb_host *host)
478{
479 INIT_LIST_HEAD(&dummy_zero_addr.host_list);
480 INIT_LIST_HEAD(&dummy_zero_addr.hl_list);
481 INIT_LIST_HEAD(&dummy_max_addr.host_list);
482 INIT_LIST_HEAD(&dummy_max_addr.hl_list);
483
484 dummy_zero_addr.op = dummy_max_addr.op = &dummy_ops;
485
486 dummy_zero_addr.start = dummy_zero_addr.end = 0;
487 dummy_max_addr.start = dummy_max_addr.end = ((u64) 1) << 48;
488
489 list_add_tail(&dummy_zero_addr.host_list, &host->addr_space);
490 list_add_tail(&dummy_max_addr.host_list, &host->addr_space);
491}
492
493void highlevel_add_host(struct hpsb_host *host)
494{
495 struct hpsb_highlevel *hl;
496
497 init_hpsb_highlevel(host);
498
499 down_read(&hl_drivers_sem);
500 list_for_each_entry(hl, &hl_drivers, hl_list) {
501 if (hl->add_host)
502 hl->add_host(host);
503 }
504 up_read(&hl_drivers_sem);
505 if (host->update_config_rom) {
506 if (hpsb_update_config_rom_image(host) < 0)
507 HPSB_ERR("Failed to generate Configuration ROM image for "
508 "host %s-%d", hl->name, host->id);
509 }
510}
511
512void highlevel_remove_host(struct hpsb_host *host)
513{
514 struct hpsb_highlevel *hl;
515
516 down_read(&hl_drivers_sem);
517 list_for_each_entry(hl, &hl_drivers, hl_list)
518 __unregister_host(hl, host, 0);
519 up_read(&hl_drivers_sem);
520}
521
522void highlevel_host_reset(struct hpsb_host *host)
523{
524 struct hpsb_highlevel *hl;
525
526 read_lock(&hl_irqs_lock);
527 list_for_each_entry(hl, &hl_irqs, irq_list) {
528 if (hl->host_reset)
529 hl->host_reset(host);
530 }
531 read_unlock(&hl_irqs_lock);
532}
533
534void highlevel_iso_receive(struct hpsb_host *host, void *data, size_t length)
535{
536 struct hpsb_highlevel *hl;
537 int channel = (((quadlet_t *)data)[0] >> 8) & 0x3f;
538
539 read_lock(&hl_irqs_lock);
540 list_for_each_entry(hl, &hl_irqs, irq_list) {
541 if (hl->iso_receive)
542 hl->iso_receive(host, channel, data, length);
543 }
544 read_unlock(&hl_irqs_lock);
545}
546
547void highlevel_fcp_request(struct hpsb_host *host, int nodeid, int direction,
548 void *data, size_t length)
549{
550 struct hpsb_highlevel *hl;
551 int cts = ((quadlet_t *)data)[0] >> 4;
552
553 read_lock(&hl_irqs_lock);
554 list_for_each_entry(hl, &hl_irqs, irq_list) {
555 if (hl->fcp_request)
556 hl->fcp_request(host, nodeid, direction, cts, data,
557 length);
558 }
559 read_unlock(&hl_irqs_lock);
560}
561
562int highlevel_read(struct hpsb_host *host, int nodeid, void *data,
563 u64 addr, unsigned int length, u16 flags)
564{
565 struct hpsb_address_serve *as;
566 unsigned int partlength;
567 int rcode = RCODE_ADDRESS_ERROR;
568
569 read_lock(&addr_space_lock);
570
571 list_for_each_entry(as, &host->addr_space, host_list) {
572 if (as->start > addr)
573 break;
574
575 if (as->end > addr) {
576 partlength = min(as->end - addr, (u64) length);
577
578 if (as->op->read) {
579 rcode = as->op->read(host, nodeid, data,
580 addr, partlength, flags);
581 } else {
582 rcode = RCODE_TYPE_ERROR;
583 }
584
585 data += partlength;
586 length -= partlength;
587 addr += partlength;
588
589 if ((rcode != RCODE_COMPLETE) || !length) {
590 break;
591 }
592 }
593 }
594
595 read_unlock(&addr_space_lock);
596
597 if (length && (rcode == RCODE_COMPLETE)) {
598 rcode = RCODE_ADDRESS_ERROR;
599 }
600
601 return rcode;
602}
603
604int highlevel_write(struct hpsb_host *host, int nodeid, int destid,
605 void *data, u64 addr, unsigned int length, u16 flags)
606{
607 struct hpsb_address_serve *as;
608 unsigned int partlength;
609 int rcode = RCODE_ADDRESS_ERROR;
610
611 read_lock(&addr_space_lock);
612
613 list_for_each_entry(as, &host->addr_space, host_list) {
614 if (as->start > addr)
615 break;
616
617 if (as->end > addr) {
618 partlength = min(as->end - addr, (u64) length);
619
620 if (as->op->write) {
621 rcode = as->op->write(host, nodeid, destid,
622 data, addr, partlength, flags);
623 } else {
624 rcode = RCODE_TYPE_ERROR;
625 }
626
627 data += partlength;
628 length -= partlength;
629 addr += partlength;
630
631 if ((rcode != RCODE_COMPLETE) || !length) {
632 break;
633 }
634 }
635 }
636
637 read_unlock(&addr_space_lock);
638
639 if (length && (rcode == RCODE_COMPLETE)) {
640 rcode = RCODE_ADDRESS_ERROR;
641 }
642
643 return rcode;
644}
645
646
647int highlevel_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
648 u64 addr, quadlet_t data, quadlet_t arg, int ext_tcode, u16 flags)
649{
650 struct hpsb_address_serve *as;
651 int rcode = RCODE_ADDRESS_ERROR;
652
653 read_lock(&addr_space_lock);
654
655 list_for_each_entry(as, &host->addr_space, host_list) {
656 if (as->start > addr)
657 break;
658
659 if (as->end > addr) {
660 if (as->op->lock) {
661 rcode = as->op->lock(host, nodeid, store, addr,
662 data, arg, ext_tcode, flags);
663 } else {
664 rcode = RCODE_TYPE_ERROR;
665 }
666
667 break;
668 }
669 }
670
671 read_unlock(&addr_space_lock);
672
673 return rcode;
674}
675
676int highlevel_lock64(struct hpsb_host *host, int nodeid, octlet_t *store,
677 u64 addr, octlet_t data, octlet_t arg, int ext_tcode, u16 flags)
678{
679 struct hpsb_address_serve *as;
680 int rcode = RCODE_ADDRESS_ERROR;
681
682 read_lock(&addr_space_lock);
683
684 list_for_each_entry(as, &host->addr_space, host_list) {
685 if (as->start > addr)
686 break;
687
688 if (as->end > addr) {
689 if (as->op->lock64) {
690 rcode = as->op->lock64(host, nodeid, store,
691 addr, data, arg,
692 ext_tcode, flags);
693 } else {
694 rcode = RCODE_TYPE_ERROR;
695 }
696
697 break;
698 }
699 }
700
701 read_unlock(&addr_space_lock);
702
703 return rcode;
704}