Merge branch 'linux-next' of git://git.infradead.org/~dedekind/ubi-2.6
[linux-2.6] / arch / powerpc / platforms / ps3 / device-init.c
1 /*
2  *  PS3 device registration routines.
3  *
4  *  Copyright (C) 2007 Sony Computer Entertainment Inc.
5  *  Copyright 2007 Sony Corp.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; version 2 of the License.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <linux/delay.h>
22 #include <linux/freezer.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/init.h>
26 #include <linux/reboot.h>
27
28 #include <asm/firmware.h>
29 #include <asm/lv1call.h>
30 #include <asm/ps3stor.h>
31
32 #include "platform.h"
33
34 static int __init ps3_register_lpm_devices(void)
35 {
36         int result;
37         u64 tmp1;
38         u64 tmp2;
39         struct ps3_system_bus_device *dev;
40
41         pr_debug(" -> %s:%d\n", __func__, __LINE__);
42
43         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
44         if (!dev)
45                 return -ENOMEM;
46
47         dev->match_id = PS3_MATCH_ID_LPM;
48         dev->dev_type = PS3_DEVICE_TYPE_LPM;
49
50         /* The current lpm driver only supports a single BE processor. */
51
52         result = ps3_repository_read_be_node_id(0, &dev->lpm.node_id);
53
54         if (result) {
55                 pr_debug("%s:%d: ps3_repository_read_be_node_id failed \n",
56                         __func__, __LINE__);
57                 goto fail_read_repo;
58         }
59
60         result = ps3_repository_read_lpm_privileges(dev->lpm.node_id, &tmp1,
61                 &dev->lpm.rights);
62
63         if (result) {
64                 pr_debug("%s:%d: ps3_repository_read_lpm_privleges failed \n",
65                         __func__, __LINE__);
66                 goto fail_read_repo;
67         }
68
69         lv1_get_logical_partition_id(&tmp2);
70
71         if (tmp1 != tmp2) {
72                 pr_debug("%s:%d: wrong lpar\n",
73                         __func__, __LINE__);
74                 result = -ENODEV;
75                 goto fail_rights;
76         }
77
78         if (!(dev->lpm.rights & PS3_LPM_RIGHTS_USE_LPM)) {
79                 pr_debug("%s:%d: don't have rights to use lpm\n",
80                         __func__, __LINE__);
81                 result = -EPERM;
82                 goto fail_rights;
83         }
84
85         pr_debug("%s:%d: pu_id %lu, rights %lu(%lxh)\n",
86                 __func__, __LINE__, dev->lpm.pu_id, dev->lpm.rights,
87                 dev->lpm.rights);
88
89         result = ps3_repository_read_pu_id(0, &dev->lpm.pu_id);
90
91         if (result) {
92                 pr_debug("%s:%d: ps3_repository_read_pu_id failed \n",
93                         __func__, __LINE__);
94                 goto fail_read_repo;
95         }
96
97         result = ps3_system_bus_device_register(dev);
98
99         if (result) {
100                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
101                         __func__, __LINE__);
102                 goto fail_register;
103         }
104
105         pr_debug(" <- %s:%d\n", __func__, __LINE__);
106         return 0;
107
108
109 fail_register:
110 fail_rights:
111 fail_read_repo:
112         kfree(dev);
113         pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
114         return result;
115 }
116
117 /**
118  * ps3_setup_gelic_device - Setup and register a gelic device instance.
119  *
120  * Allocates memory for a struct ps3_system_bus_device instance, initialises the
121  * structure members, and registers the device instance with the system bus.
122  */
123
124 static int __init ps3_setup_gelic_device(
125         const struct ps3_repository_device *repo)
126 {
127         int result;
128         struct layout {
129                 struct ps3_system_bus_device dev;
130                 struct ps3_dma_region d_region;
131         } *p;
132
133         pr_debug(" -> %s:%d\n", __func__, __LINE__);
134
135         BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
136         BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
137
138         p = kzalloc(sizeof(struct layout), GFP_KERNEL);
139
140         if (!p) {
141                 result = -ENOMEM;
142                 goto fail_malloc;
143         }
144
145         p->dev.match_id = PS3_MATCH_ID_GELIC;
146         p->dev.dev_type = PS3_DEVICE_TYPE_SB;
147         p->dev.bus_id = repo->bus_id;
148         p->dev.dev_id = repo->dev_id;
149         p->dev.d_region = &p->d_region;
150
151         result = ps3_repository_find_interrupt(repo,
152                 PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
153
154         if (result) {
155                 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
156                         __func__, __LINE__);
157                 goto fail_find_interrupt;
158         }
159
160         BUG_ON(p->dev.interrupt_id != 0);
161
162         result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
163                 PS3_DMA_OTHER, NULL, 0);
164
165         if (result) {
166                 pr_debug("%s:%d ps3_dma_region_init failed\n",
167                         __func__, __LINE__);
168                 goto fail_dma_init;
169         }
170
171         result = ps3_system_bus_device_register(&p->dev);
172
173         if (result) {
174                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
175                         __func__, __LINE__);
176                 goto fail_device_register;
177         }
178
179         pr_debug(" <- %s:%d\n", __func__, __LINE__);
180         return result;
181
182 fail_device_register:
183 fail_dma_init:
184 fail_find_interrupt:
185         kfree(p);
186 fail_malloc:
187         pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
188         return result;
189 }
190
191 static int __init_refok ps3_setup_uhc_device(
192         const struct ps3_repository_device *repo, enum ps3_match_id match_id,
193         enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
194 {
195         int result;
196         struct layout {
197                 struct ps3_system_bus_device dev;
198                 struct ps3_dma_region d_region;
199                 struct ps3_mmio_region m_region;
200         } *p;
201         u64 bus_addr;
202         u64 len;
203
204         pr_debug(" -> %s:%d\n", __func__, __LINE__);
205
206         BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
207         BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
208
209         p = kzalloc(sizeof(struct layout), GFP_KERNEL);
210
211         if (!p) {
212                 result = -ENOMEM;
213                 goto fail_malloc;
214         }
215
216         p->dev.match_id = match_id;
217         p->dev.dev_type = PS3_DEVICE_TYPE_SB;
218         p->dev.bus_id = repo->bus_id;
219         p->dev.dev_id = repo->dev_id;
220         p->dev.d_region = &p->d_region;
221         p->dev.m_region = &p->m_region;
222
223         result = ps3_repository_find_interrupt(repo,
224                 interrupt_type, &p->dev.interrupt_id);
225
226         if (result) {
227                 pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
228                         __func__, __LINE__);
229                 goto fail_find_interrupt;
230         }
231
232         result = ps3_repository_find_reg(repo, reg_type,
233                 &bus_addr, &len);
234
235         if (result) {
236                 pr_debug("%s:%d ps3_repository_find_reg failed\n",
237                         __func__, __LINE__);
238                 goto fail_find_reg;
239         }
240
241         result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
242                 PS3_DMA_INTERNAL, NULL, 0);
243
244         if (result) {
245                 pr_debug("%s:%d ps3_dma_region_init failed\n",
246                         __func__, __LINE__);
247                 goto fail_dma_init;
248         }
249
250         result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
251                 PS3_MMIO_4K);
252
253         if (result) {
254                 pr_debug("%s:%d ps3_mmio_region_init failed\n",
255                         __func__, __LINE__);
256                 goto fail_mmio_init;
257         }
258
259         result = ps3_system_bus_device_register(&p->dev);
260
261         if (result) {
262                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
263                         __func__, __LINE__);
264                 goto fail_device_register;
265         }
266
267         pr_debug(" <- %s:%d\n", __func__, __LINE__);
268         return result;
269
270 fail_device_register:
271 fail_mmio_init:
272 fail_dma_init:
273 fail_find_reg:
274 fail_find_interrupt:
275         kfree(p);
276 fail_malloc:
277         pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
278         return result;
279 }
280
281 static int __init ps3_setup_ehci_device(
282         const struct ps3_repository_device *repo)
283 {
284         return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
285                 PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
286 }
287
288 static int __init ps3_setup_ohci_device(
289         const struct ps3_repository_device *repo)
290 {
291         return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
292                 PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
293 }
294
295 static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
296         unsigned int port_number)
297 {
298         int result;
299         struct layout {
300                 struct ps3_system_bus_device dev;
301         } *p;
302
303         pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
304                 match_id, port_number);
305
306         p = kzalloc(sizeof(struct layout), GFP_KERNEL);
307
308         if (!p)
309                 return -ENOMEM;
310
311         p->dev.match_id = match_id;
312         p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
313         p->dev.port_number = port_number;
314
315         result = ps3_system_bus_device_register(&p->dev);
316
317         if (result) {
318                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
319                         __func__, __LINE__);
320                 goto fail_device_register;
321         }
322         pr_debug(" <- %s:%d\n", __func__, __LINE__);
323         return 0;
324
325 fail_device_register:
326         kfree(p);
327         pr_debug(" <- %s:%d fail\n", __func__, __LINE__);
328         return result;
329 }
330
331 static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
332                                  enum ps3_match_id match_id)
333 {
334         int result;
335         struct ps3_storage_device *p;
336         u64 port, blk_size, num_blocks;
337         unsigned int num_regions, i;
338
339         pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
340
341         result = ps3_repository_read_stor_dev_info(repo->bus_index,
342                                                    repo->dev_index, &port,
343                                                    &blk_size, &num_blocks,
344                                                    &num_regions);
345         if (result) {
346                 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
347                        __func__, __LINE__, result);
348                 return -ENODEV;
349         }
350
351         pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
352                  "num_regions %u\n", __func__, __LINE__, repo->bus_index,
353                  repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
354                  num_regions);
355
356         p = kzalloc(sizeof(struct ps3_storage_device) +
357                     num_regions * sizeof(struct ps3_storage_region),
358                     GFP_KERNEL);
359         if (!p) {
360                 result = -ENOMEM;
361                 goto fail_malloc;
362         }
363
364         p->sbd.match_id = match_id;
365         p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
366         p->sbd.bus_id = repo->bus_id;
367         p->sbd.dev_id = repo->dev_id;
368         p->sbd.d_region = &p->dma_region;
369         p->blk_size = blk_size;
370         p->num_regions = num_regions;
371
372         result = ps3_repository_find_interrupt(repo,
373                                                PS3_INTERRUPT_TYPE_EVENT_PORT,
374                                                &p->sbd.interrupt_id);
375         if (result) {
376                 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
377                        __LINE__, result);
378                 result = -ENODEV;
379                 goto fail_find_interrupt;
380         }
381
382         for (i = 0; i < num_regions; i++) {
383                 unsigned int id;
384                 u64 start, size;
385
386                 result = ps3_repository_read_stor_dev_region(repo->bus_index,
387                                                              repo->dev_index,
388                                                              i, &id, &start,
389                                                              &size);
390                 if (result) {
391                         printk(KERN_ERR
392                                "%s:%u: read_stor_dev_region failed %d\n",
393                                __func__, __LINE__, result);
394                         result = -ENODEV;
395                         goto fail_read_region;
396                 }
397                 pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
398                          __func__, __LINE__, i, id, start, size);
399
400                 p->regions[i].id = id;
401                 p->regions[i].start = start;
402                 p->regions[i].size = size;
403         }
404
405         result = ps3_system_bus_device_register(&p->sbd);
406         if (result) {
407                 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
408                          __func__, __LINE__);
409                 goto fail_device_register;
410         }
411
412         pr_debug(" <- %s:%u\n", __func__, __LINE__);
413         return 0;
414
415 fail_device_register:
416 fail_read_region:
417 fail_find_interrupt:
418         kfree(p);
419 fail_malloc:
420         pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
421         return result;
422 }
423
424 static int __init ps3_register_vuart_devices(void)
425 {
426         int result;
427         unsigned int port_number;
428
429         pr_debug(" -> %s:%d\n", __func__, __LINE__);
430
431         result = ps3_repository_read_vuart_av_port(&port_number);
432         if (result)
433                 port_number = 0; /* av default */
434
435         result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
436         WARN_ON(result);
437
438         result = ps3_repository_read_vuart_sysmgr_port(&port_number);
439         if (result)
440                 port_number = 2; /* sysmgr default */
441
442         result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
443                 port_number);
444         WARN_ON(result);
445
446         pr_debug(" <- %s:%d\n", __func__, __LINE__);
447         return result;
448 }
449
450 static int __init ps3_register_sound_devices(void)
451 {
452         int result;
453         struct layout {
454                 struct ps3_system_bus_device dev;
455                 struct ps3_dma_region d_region;
456                 struct ps3_mmio_region m_region;
457         } *p;
458
459         pr_debug(" -> %s:%d\n", __func__, __LINE__);
460
461         p = kzalloc(sizeof(*p), GFP_KERNEL);
462         if (!p)
463                 return -ENOMEM;
464
465         p->dev.match_id = PS3_MATCH_ID_SOUND;
466         p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
467         p->dev.d_region = &p->d_region;
468         p->dev.m_region = &p->m_region;
469
470         result = ps3_system_bus_device_register(&p->dev);
471
472         if (result) {
473                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
474                         __func__, __LINE__);
475                 goto fail_device_register;
476         }
477         pr_debug(" <- %s:%d\n", __func__, __LINE__);
478         return 0;
479
480 fail_device_register:
481         kfree(p);
482         pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
483         return result;
484 }
485
486 static int __init ps3_register_graphics_devices(void)
487 {
488         int result;
489         struct layout {
490                 struct ps3_system_bus_device dev;
491         } *p;
492
493         pr_debug(" -> %s:%d\n", __func__, __LINE__);
494
495         p = kzalloc(sizeof(struct layout), GFP_KERNEL);
496
497         if (!p)
498                 return -ENOMEM;
499
500         p->dev.match_id = PS3_MATCH_ID_GPU;
501         p->dev.match_sub_id = PS3_MATCH_SUB_ID_GPU_FB;
502         p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
503
504         result = ps3_system_bus_device_register(&p->dev);
505
506         if (result) {
507                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
508                         __func__, __LINE__);
509                 goto fail_device_register;
510         }
511
512         pr_debug(" <- %s:%d\n", __func__, __LINE__);
513         return 0;
514
515 fail_device_register:
516         kfree(p);
517         pr_debug(" <- %s:%d failed\n", __func__, __LINE__);
518         return result;
519 }
520
521 /**
522  * ps3_setup_dynamic_device - Setup a dynamic device from the repository
523  */
524
525 static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
526 {
527         int result;
528
529         switch (repo->dev_type) {
530         case PS3_DEV_TYPE_STOR_DISK:
531                 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
532
533                 /* Some devices are not accessable from the Other OS lpar. */
534                 if (result == -ENODEV) {
535                         result = 0;
536                         pr_debug("%s:%u: not accessable\n", __func__,
537                                  __LINE__);
538                 }
539
540                 if (result)
541                         pr_debug("%s:%u ps3_setup_storage_dev failed\n",
542                                  __func__, __LINE__);
543                 break;
544
545         case PS3_DEV_TYPE_STOR_ROM:
546                 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
547                 if (result)
548                         pr_debug("%s:%u ps3_setup_storage_dev failed\n",
549                                  __func__, __LINE__);
550                 break;
551
552         case PS3_DEV_TYPE_STOR_FLASH:
553                 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
554                 if (result)
555                         pr_debug("%s:%u ps3_setup_storage_dev failed\n",
556                                  __func__, __LINE__);
557                 break;
558
559         default:
560                 result = 0;
561                 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
562                         repo->dev_type);
563         }
564
565         return result;
566 }
567
568 /**
569  * ps3_setup_static_device - Setup a static device from the repository
570  */
571
572 static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
573 {
574         int result;
575
576         switch (repo->dev_type) {
577         case PS3_DEV_TYPE_SB_GELIC:
578                 result = ps3_setup_gelic_device(repo);
579                 if (result) {
580                         pr_debug("%s:%d ps3_setup_gelic_device failed\n",
581                                 __func__, __LINE__);
582                 }
583                 break;
584         case PS3_DEV_TYPE_SB_USB:
585
586                 /* Each USB device has both an EHCI and an OHCI HC */
587
588                 result = ps3_setup_ehci_device(repo);
589
590                 if (result) {
591                         pr_debug("%s:%d ps3_setup_ehci_device failed\n",
592                                 __func__, __LINE__);
593                 }
594
595                 result = ps3_setup_ohci_device(repo);
596
597                 if (result) {
598                         pr_debug("%s:%d ps3_setup_ohci_device failed\n",
599                                 __func__, __LINE__);
600                 }
601                 break;
602
603         default:
604                 return ps3_setup_dynamic_device(repo);
605         }
606
607         return result;
608 }
609
610 static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
611 {
612         struct ps3_repository_device repo;
613         int res;
614         unsigned int retries;
615         unsigned long rem;
616
617         /*
618          * On some firmware versions (e.g. 1.90), the device may not show up
619          * in the repository immediately
620          */
621         for (retries = 0; retries < 10; retries++) {
622                 res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
623                 if (!res)
624                         goto found;
625
626                 rem = msleep_interruptible(100);
627                 if (rem)
628                         break;
629         }
630         pr_warning("%s:%u: device %lu:%lu not found\n", __func__, __LINE__,
631                    bus_id, dev_id);
632         return;
633
634 found:
635         if (retries)
636                 pr_debug("%s:%u: device %lu:%lu found after %u retries\n",
637                          __func__, __LINE__, bus_id, dev_id, retries);
638
639         ps3_setup_dynamic_device(&repo);
640         return;
641 }
642
643 #define PS3_NOTIFICATION_DEV_ID         ULONG_MAX
644 #define PS3_NOTIFICATION_INTERRUPT_ID   0
645
646 struct ps3_notification_device {
647         struct ps3_system_bus_device sbd;
648         spinlock_t lock;
649         u64 tag;
650         u64 lv1_status;
651         struct completion done;
652 };
653
654 enum ps3_notify_type {
655         notify_device_ready = 0,
656         notify_region_probe = 1,
657         notify_region_update = 2,
658 };
659
660 struct ps3_notify_cmd {
661         u64 operation_code;             /* must be zero */
662         u64 event_mask;                 /* OR of 1UL << enum ps3_notify_type */
663 };
664
665 struct ps3_notify_event {
666         u64 event_type;                 /* enum ps3_notify_type */
667         u64 bus_id;
668         u64 dev_id;
669         u64 dev_type;
670         u64 dev_port;
671 };
672
673 static irqreturn_t ps3_notification_interrupt(int irq, void *data)
674 {
675         struct ps3_notification_device *dev = data;
676         int res;
677         u64 tag, status;
678
679         spin_lock(&dev->lock);
680         res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
681                                            &status);
682         if (tag != dev->tag)
683                 pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
684                        __func__, __LINE__, tag, dev->tag);
685
686         if (res) {
687                 pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
688                        status);
689         } else {
690                 pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
691                          __LINE__, status);
692                 dev->lv1_status = status;
693                 complete(&dev->done);
694         }
695         spin_unlock(&dev->lock);
696         return IRQ_HANDLED;
697 }
698
699 static int ps3_notification_read_write(struct ps3_notification_device *dev,
700                                        u64 lpar, int write)
701 {
702         const char *op = write ? "write" : "read";
703         unsigned long flags;
704         int res;
705
706         init_completion(&dev->done);
707         spin_lock_irqsave(&dev->lock, flags);
708         res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
709                                         &dev->tag)
710                     : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
711                                        &dev->tag);
712         spin_unlock_irqrestore(&dev->lock, flags);
713         if (res) {
714                 pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
715                 return -EPERM;
716         }
717         pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
718
719         res = wait_event_interruptible(dev->done.wait,
720                                        dev->done.done || kthread_should_stop());
721         if (kthread_should_stop())
722                 res = -EINTR;
723         if (res) {
724                 pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
725                 return res;
726         }
727
728         if (dev->lv1_status) {
729                 pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
730                        __LINE__, op, dev->lv1_status);
731                 return -EIO;
732         }
733         pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
734
735         return 0;
736 }
737
738 static struct task_struct *probe_task;
739
740 /**
741  * ps3_probe_thread - Background repository probing at system startup.
742  *
743  * This implementation only supports background probing on a single bus.
744  * It uses the hypervisor's storage device notification mechanism to wait until
745  * a storage device is ready.  The device notification mechanism uses a
746  * pseudo device to asynchronously notify the guest when storage devices become
747  * ready.  The notification device has a block size of 512 bytes.
748  */
749
750 static int ps3_probe_thread(void *data)
751 {
752         struct ps3_notification_device dev;
753         int res;
754         unsigned int irq;
755         u64 lpar;
756         void *buf;
757         struct ps3_notify_cmd *notify_cmd;
758         struct ps3_notify_event *notify_event;
759
760         pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
761
762         buf = kzalloc(512, GFP_KERNEL);
763         if (!buf)
764                 return -ENOMEM;
765
766         lpar = ps3_mm_phys_to_lpar(__pa(buf));
767         notify_cmd = buf;
768         notify_event = buf;
769
770         /* dummy system bus device */
771         dev.sbd.bus_id = (u64)data;
772         dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
773         dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
774
775         res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
776         if (res) {
777                 pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
778                        __LINE__, ps3_result(res));
779                 goto fail_free;
780         }
781
782         res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
783                                               &irq);
784         if (res) {
785                 pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
786                        __func__, __LINE__, res);
787                goto fail_close_device;
788         }
789
790         spin_lock_init(&dev.lock);
791
792         res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
793                           "ps3_notification", &dev);
794         if (res) {
795                 pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
796                        res);
797                 goto fail_sb_event_receive_port_destroy;
798         }
799
800         /* Setup and write the request for device notification. */
801         notify_cmd->operation_code = 0; /* must be zero */
802         notify_cmd->event_mask = 1UL << notify_region_probe;
803
804         res = ps3_notification_read_write(&dev, lpar, 1);
805         if (res)
806                 goto fail_free_irq;
807
808         /* Loop here processing the requested notification events. */
809         do {
810                 try_to_freeze();
811
812                 memset(notify_event, 0, sizeof(*notify_event));
813
814                 res = ps3_notification_read_write(&dev, lpar, 0);
815                 if (res)
816                         break;
817
818                 pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
819                          " type %lu port %lu\n", __func__, __LINE__,
820                          notify_event->event_type, notify_event->bus_id,
821                          notify_event->dev_id, notify_event->dev_type,
822                          notify_event->dev_port);
823
824                 if (notify_event->event_type != notify_region_probe ||
825                     notify_event->bus_id != dev.sbd.bus_id) {
826                         pr_warning("%s:%u: bad notify_event: event %lu, "
827                                    "dev_id %lu, dev_type %lu\n",
828                                    __func__, __LINE__, notify_event->event_type,
829                                    notify_event->dev_id,
830                                    notify_event->dev_type);
831                         continue;
832                 }
833
834                 ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
835
836         } while (!kthread_should_stop());
837
838 fail_free_irq:
839         free_irq(irq, &dev);
840 fail_sb_event_receive_port_destroy:
841         ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
842 fail_close_device:
843         lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
844 fail_free:
845         kfree(buf);
846
847         probe_task = NULL;
848
849         pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
850
851         return 0;
852 }
853
854 /**
855  * ps3_stop_probe_thread - Stops the background probe thread.
856  *
857  */
858
859 static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
860                                  void *data)
861 {
862         if (probe_task)
863                 kthread_stop(probe_task);
864         return 0;
865 }
866
867 static struct notifier_block nb = {
868         .notifier_call = ps3_stop_probe_thread
869 };
870
871 /**
872  * ps3_start_probe_thread - Starts the background probe thread.
873  *
874  */
875
876 static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
877 {
878         int result;
879         struct task_struct *task;
880         struct ps3_repository_device repo;
881
882         pr_debug(" -> %s:%d\n", __func__, __LINE__);
883
884         memset(&repo, 0, sizeof(repo));
885
886         repo.bus_type = bus_type;
887
888         result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
889
890         if (result) {
891                 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
892                 return -ENODEV;
893         }
894
895         result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
896
897         if (result) {
898                 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
899                         result);
900                 return -ENODEV;
901         }
902
903         task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
904                            "ps3-probe-%u", bus_type);
905
906         if (IS_ERR(task)) {
907                 result = PTR_ERR(task);
908                 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
909                        result);
910                 return result;
911         }
912
913         probe_task = task;
914         register_reboot_notifier(&nb);
915
916         pr_debug(" <- %s:%d\n", __func__, __LINE__);
917         return 0;
918 }
919
920 /**
921  * ps3_register_devices - Probe the system and register devices found.
922  *
923  * A device_initcall() routine.
924  */
925
926 static int __init ps3_register_devices(void)
927 {
928         int result;
929
930         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
931                 return -ENODEV;
932
933         pr_debug(" -> %s:%d\n", __func__, __LINE__);
934
935         /* ps3_repository_dump_bus_info(); */
936
937         result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
938
939         ps3_register_vuart_devices();
940
941         ps3_register_graphics_devices();
942
943         ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
944
945         ps3_register_sound_devices();
946
947         ps3_register_lpm_devices();
948
949         pr_debug(" <- %s:%d\n", __func__, __LINE__);
950         return 0;
951 }
952
953 device_initcall(ps3_register_devices);