powerpc: Add C2K to configuration
[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
321         pr_debug(" <- %s:%d\n", __func__, __LINE__);
322         return result;
323 }
324
325 static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
326                                  enum ps3_match_id match_id)
327 {
328         int result;
329         struct ps3_storage_device *p;
330         u64 port, blk_size, num_blocks;
331         unsigned int num_regions, i;
332
333         pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
334
335         result = ps3_repository_read_stor_dev_info(repo->bus_index,
336                                                    repo->dev_index, &port,
337                                                    &blk_size, &num_blocks,
338                                                    &num_regions);
339         if (result) {
340                 printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
341                        __func__, __LINE__, result);
342                 return -ENODEV;
343         }
344
345         pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
346                  "num_regions %u\n", __func__, __LINE__, repo->bus_index,
347                  repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
348                  num_regions);
349
350         p = kzalloc(sizeof(struct ps3_storage_device) +
351                     num_regions * sizeof(struct ps3_storage_region),
352                     GFP_KERNEL);
353         if (!p) {
354                 result = -ENOMEM;
355                 goto fail_malloc;
356         }
357
358         p->sbd.match_id = match_id;
359         p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
360         p->sbd.bus_id = repo->bus_id;
361         p->sbd.dev_id = repo->dev_id;
362         p->sbd.d_region = &p->dma_region;
363         p->blk_size = blk_size;
364         p->num_regions = num_regions;
365
366         result = ps3_repository_find_interrupt(repo,
367                                                PS3_INTERRUPT_TYPE_EVENT_PORT,
368                                                &p->sbd.interrupt_id);
369         if (result) {
370                 printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
371                        __LINE__, result);
372                 result = -ENODEV;
373                 goto fail_find_interrupt;
374         }
375
376         for (i = 0; i < num_regions; i++) {
377                 unsigned int id;
378                 u64 start, size;
379
380                 result = ps3_repository_read_stor_dev_region(repo->bus_index,
381                                                              repo->dev_index,
382                                                              i, &id, &start,
383                                                              &size);
384                 if (result) {
385                         printk(KERN_ERR
386                                "%s:%u: read_stor_dev_region failed %d\n",
387                                __func__, __LINE__, result);
388                         result = -ENODEV;
389                         goto fail_read_region;
390                 }
391                 pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
392                          __func__, __LINE__, i, id, start, size);
393
394                 p->regions[i].id = id;
395                 p->regions[i].start = start;
396                 p->regions[i].size = size;
397         }
398
399         result = ps3_system_bus_device_register(&p->sbd);
400         if (result) {
401                 pr_debug("%s:%u ps3_system_bus_device_register failed\n",
402                          __func__, __LINE__);
403                 goto fail_device_register;
404         }
405
406         pr_debug(" <- %s:%u\n", __func__, __LINE__);
407         return 0;
408
409 fail_device_register:
410 fail_read_region:
411 fail_find_interrupt:
412         kfree(p);
413 fail_malloc:
414         pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
415         return result;
416 }
417
418 static int __init ps3_register_vuart_devices(void)
419 {
420         int result;
421         unsigned int port_number;
422
423         pr_debug(" -> %s:%d\n", __func__, __LINE__);
424
425         result = ps3_repository_read_vuart_av_port(&port_number);
426         if (result)
427                 port_number = 0; /* av default */
428
429         result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
430         WARN_ON(result);
431
432         result = ps3_repository_read_vuart_sysmgr_port(&port_number);
433         if (result)
434                 port_number = 2; /* sysmgr default */
435
436         result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
437                 port_number);
438         WARN_ON(result);
439
440         pr_debug(" <- %s:%d\n", __func__, __LINE__);
441         return result;
442 }
443
444 static int __init ps3_register_sound_devices(void)
445 {
446         int result;
447         struct layout {
448                 struct ps3_system_bus_device dev;
449                 struct ps3_dma_region d_region;
450                 struct ps3_mmio_region m_region;
451         } *p;
452
453         pr_debug(" -> %s:%d\n", __func__, __LINE__);
454
455         p = kzalloc(sizeof(*p), GFP_KERNEL);
456         if (!p)
457                 return -ENOMEM;
458
459         p->dev.match_id = PS3_MATCH_ID_SOUND;
460         p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
461         p->dev.d_region = &p->d_region;
462         p->dev.m_region = &p->m_region;
463
464         result = ps3_system_bus_device_register(&p->dev);
465
466         if (result)
467                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
468                         __func__, __LINE__);
469
470         pr_debug(" <- %s:%d\n", __func__, __LINE__);
471         return result;
472 }
473
474 static int __init ps3_register_graphics_devices(void)
475 {
476         int result;
477         struct layout {
478                 struct ps3_system_bus_device dev;
479         } *p;
480
481         pr_debug(" -> %s:%d\n", __func__, __LINE__);
482
483         p = kzalloc(sizeof(struct layout), GFP_KERNEL);
484
485         if (!p)
486                 return -ENOMEM;
487
488         p->dev.match_id = PS3_MATCH_ID_GRAPHICS;
489         p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
490
491         result = ps3_system_bus_device_register(&p->dev);
492
493         if (result)
494                 pr_debug("%s:%d ps3_system_bus_device_register failed\n",
495                         __func__, __LINE__);
496
497         pr_debug(" <- %s:%d\n", __func__, __LINE__);
498         return result;
499 }
500
501 /**
502  * ps3_setup_dynamic_device - Setup a dynamic device from the repository
503  */
504
505 static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
506 {
507         int result;
508
509         switch (repo->dev_type) {
510         case PS3_DEV_TYPE_STOR_DISK:
511                 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
512
513                 /* Some devices are not accessable from the Other OS lpar. */
514                 if (result == -ENODEV) {
515                         result = 0;
516                         pr_debug("%s:%u: not accessable\n", __func__,
517                                  __LINE__);
518                 }
519
520                 if (result)
521                         pr_debug("%s:%u ps3_setup_storage_dev failed\n",
522                                  __func__, __LINE__);
523                 break;
524
525         case PS3_DEV_TYPE_STOR_ROM:
526                 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
527                 if (result)
528                         pr_debug("%s:%u ps3_setup_storage_dev failed\n",
529                                  __func__, __LINE__);
530                 break;
531
532         case PS3_DEV_TYPE_STOR_FLASH:
533                 result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
534                 if (result)
535                         pr_debug("%s:%u ps3_setup_storage_dev failed\n",
536                                  __func__, __LINE__);
537                 break;
538
539         default:
540                 result = 0;
541                 pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
542                         repo->dev_type);
543         }
544
545         return result;
546 }
547
548 /**
549  * ps3_setup_static_device - Setup a static device from the repository
550  */
551
552 static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
553 {
554         int result;
555
556         switch (repo->dev_type) {
557         case PS3_DEV_TYPE_SB_GELIC:
558                 result = ps3_setup_gelic_device(repo);
559                 if (result) {
560                         pr_debug("%s:%d ps3_setup_gelic_device failed\n",
561                                 __func__, __LINE__);
562                 }
563                 break;
564         case PS3_DEV_TYPE_SB_USB:
565
566                 /* Each USB device has both an EHCI and an OHCI HC */
567
568                 result = ps3_setup_ehci_device(repo);
569
570                 if (result) {
571                         pr_debug("%s:%d ps3_setup_ehci_device failed\n",
572                                 __func__, __LINE__);
573                 }
574
575                 result = ps3_setup_ohci_device(repo);
576
577                 if (result) {
578                         pr_debug("%s:%d ps3_setup_ohci_device failed\n",
579                                 __func__, __LINE__);
580                 }
581                 break;
582
583         default:
584                 return ps3_setup_dynamic_device(repo);
585         }
586
587         return result;
588 }
589
590 static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
591 {
592         struct ps3_repository_device repo;
593         int res;
594         unsigned int retries;
595         unsigned long rem;
596
597         /*
598          * On some firmware versions (e.g. 1.90), the device may not show up
599          * in the repository immediately
600          */
601         for (retries = 0; retries < 10; retries++) {
602                 res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
603                 if (!res)
604                         goto found;
605
606                 rem = msleep_interruptible(100);
607                 if (rem)
608                         break;
609         }
610         pr_warning("%s:%u: device %lu:%lu not found\n", __func__, __LINE__,
611                    bus_id, dev_id);
612         return;
613
614 found:
615         if (retries)
616                 pr_debug("%s:%u: device %lu:%lu found after %u retries\n",
617                          __func__, __LINE__, bus_id, dev_id, retries);
618
619         ps3_setup_dynamic_device(&repo);
620         return;
621 }
622
623 #define PS3_NOTIFICATION_DEV_ID         ULONG_MAX
624 #define PS3_NOTIFICATION_INTERRUPT_ID   0
625
626 struct ps3_notification_device {
627         struct ps3_system_bus_device sbd;
628         spinlock_t lock;
629         u64 tag;
630         u64 lv1_status;
631         struct completion done;
632 };
633
634 enum ps3_notify_type {
635         notify_device_ready = 0,
636         notify_region_probe = 1,
637         notify_region_update = 2,
638 };
639
640 struct ps3_notify_cmd {
641         u64 operation_code;             /* must be zero */
642         u64 event_mask;                 /* OR of 1UL << enum ps3_notify_type */
643 };
644
645 struct ps3_notify_event {
646         u64 event_type;                 /* enum ps3_notify_type */
647         u64 bus_id;
648         u64 dev_id;
649         u64 dev_type;
650         u64 dev_port;
651 };
652
653 static irqreturn_t ps3_notification_interrupt(int irq, void *data)
654 {
655         struct ps3_notification_device *dev = data;
656         int res;
657         u64 tag, status;
658
659         spin_lock(&dev->lock);
660         res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
661                                            &status);
662         if (tag != dev->tag)
663                 pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
664                        __func__, __LINE__, tag, dev->tag);
665
666         if (res) {
667                 pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
668                        status);
669         } else {
670                 pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
671                          __LINE__, status);
672                 dev->lv1_status = status;
673                 complete(&dev->done);
674         }
675         spin_unlock(&dev->lock);
676         return IRQ_HANDLED;
677 }
678
679 static int ps3_notification_read_write(struct ps3_notification_device *dev,
680                                        u64 lpar, int write)
681 {
682         const char *op = write ? "write" : "read";
683         unsigned long flags;
684         int res;
685
686         init_completion(&dev->done);
687         spin_lock_irqsave(&dev->lock, flags);
688         res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
689                                         &dev->tag)
690                     : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
691                                        &dev->tag);
692         spin_unlock_irqrestore(&dev->lock, flags);
693         if (res) {
694                 pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
695                 return -EPERM;
696         }
697         pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
698
699         res = wait_event_interruptible(dev->done.wait,
700                                        dev->done.done || kthread_should_stop());
701         if (kthread_should_stop())
702                 res = -EINTR;
703         if (res) {
704                 pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
705                 return res;
706         }
707
708         if (dev->lv1_status) {
709                 pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
710                        __LINE__, op, dev->lv1_status);
711                 return -EIO;
712         }
713         pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
714
715         return 0;
716 }
717
718 static struct task_struct *probe_task;
719
720 /**
721  * ps3_probe_thread - Background repository probing at system startup.
722  *
723  * This implementation only supports background probing on a single bus.
724  * It uses the hypervisor's storage device notification mechanism to wait until
725  * a storage device is ready.  The device notification mechanism uses a
726  * pseudo device to asynchronously notify the guest when storage devices become
727  * ready.  The notification device has a block size of 512 bytes.
728  */
729
730 static int ps3_probe_thread(void *data)
731 {
732         struct ps3_notification_device dev;
733         int res;
734         unsigned int irq;
735         u64 lpar;
736         void *buf;
737         struct ps3_notify_cmd *notify_cmd;
738         struct ps3_notify_event *notify_event;
739
740         pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
741
742         buf = kzalloc(512, GFP_KERNEL);
743         if (!buf)
744                 return -ENOMEM;
745
746         lpar = ps3_mm_phys_to_lpar(__pa(buf));
747         notify_cmd = buf;
748         notify_event = buf;
749
750         /* dummy system bus device */
751         dev.sbd.bus_id = (u64)data;
752         dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
753         dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
754
755         res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
756         if (res) {
757                 pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
758                        __LINE__, ps3_result(res));
759                 goto fail_free;
760         }
761
762         res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
763                                               &irq);
764         if (res) {
765                 pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
766                        __func__, __LINE__, res);
767                goto fail_close_device;
768         }
769
770         spin_lock_init(&dev.lock);
771
772         res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
773                           "ps3_notification", &dev);
774         if (res) {
775                 pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
776                        res);
777                 goto fail_sb_event_receive_port_destroy;
778         }
779
780         /* Setup and write the request for device notification. */
781         notify_cmd->operation_code = 0; /* must be zero */
782         notify_cmd->event_mask = 1UL << notify_region_probe;
783
784         res = ps3_notification_read_write(&dev, lpar, 1);
785         if (res)
786                 goto fail_free_irq;
787
788         /* Loop here processing the requested notification events. */
789         do {
790                 try_to_freeze();
791
792                 memset(notify_event, 0, sizeof(*notify_event));
793
794                 res = ps3_notification_read_write(&dev, lpar, 0);
795                 if (res)
796                         break;
797
798                 pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
799                          " type %lu port %lu\n", __func__, __LINE__,
800                          notify_event->event_type, notify_event->bus_id,
801                          notify_event->dev_id, notify_event->dev_type,
802                          notify_event->dev_port);
803
804                 if (notify_event->event_type != notify_region_probe ||
805                     notify_event->bus_id != dev.sbd.bus_id) {
806                         pr_warning("%s:%u: bad notify_event: event %lu, "
807                                    "dev_id %lu, dev_type %lu\n",
808                                    __func__, __LINE__, notify_event->event_type,
809                                    notify_event->dev_id,
810                                    notify_event->dev_type);
811                         continue;
812                 }
813
814                 ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
815
816         } while (!kthread_should_stop());
817
818 fail_free_irq:
819         free_irq(irq, &dev);
820 fail_sb_event_receive_port_destroy:
821         ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
822 fail_close_device:
823         lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
824 fail_free:
825         kfree(buf);
826
827         probe_task = NULL;
828
829         pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
830
831         return 0;
832 }
833
834 /**
835  * ps3_stop_probe_thread - Stops the background probe thread.
836  *
837  */
838
839 static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
840                                  void *data)
841 {
842         if (probe_task)
843                 kthread_stop(probe_task);
844         return 0;
845 }
846
847 static struct notifier_block nb = {
848         .notifier_call = ps3_stop_probe_thread
849 };
850
851 /**
852  * ps3_start_probe_thread - Starts the background probe thread.
853  *
854  */
855
856 static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
857 {
858         int result;
859         struct task_struct *task;
860         struct ps3_repository_device repo;
861
862         pr_debug(" -> %s:%d\n", __func__, __LINE__);
863
864         memset(&repo, 0, sizeof(repo));
865
866         repo.bus_type = bus_type;
867
868         result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
869
870         if (result) {
871                 printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
872                 return -ENODEV;
873         }
874
875         result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
876
877         if (result) {
878                 printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
879                         result);
880                 return -ENODEV;
881         }
882
883         task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
884                            "ps3-probe-%u", bus_type);
885
886         if (IS_ERR(task)) {
887                 result = PTR_ERR(task);
888                 printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
889                        result);
890                 return result;
891         }
892
893         probe_task = task;
894         register_reboot_notifier(&nb);
895
896         pr_debug(" <- %s:%d\n", __func__, __LINE__);
897         return 0;
898 }
899
900 /**
901  * ps3_register_devices - Probe the system and register devices found.
902  *
903  * A device_initcall() routine.
904  */
905
906 static int __init ps3_register_devices(void)
907 {
908         int result;
909
910         if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
911                 return -ENODEV;
912
913         pr_debug(" -> %s:%d\n", __func__, __LINE__);
914
915         /* ps3_repository_dump_bus_info(); */
916
917         result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
918
919         ps3_register_vuart_devices();
920
921         ps3_register_graphics_devices();
922
923         ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
924
925         ps3_register_sound_devices();
926
927         ps3_register_lpm_devices();
928
929         pr_debug(" <- %s:%d\n", __func__, __LINE__);
930         return 0;
931 }
932
933 device_initcall(ps3_register_devices);