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