device-init.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #include <linux/delay.h>
  21. #include <linux/freezer.h>
  22. #include <linux/kernel.h>
  23. #include <linux/kthread.h>
  24. #include <linux/init.h>
  25. #include <linux/reboot.h>
  26. #include <asm/firmware.h>
  27. #include <asm/lv1call.h>
  28. #include <asm/ps3stor.h>
  29. #include "platform.h"
  30. static int __init ps3_register_lpm_devices(void)
  31. {
  32. int result;
  33. u64 tmp1;
  34. u64 tmp2;
  35. struct ps3_system_bus_device *dev;
  36. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  37. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  38. if (!dev)
  39. return -ENOMEM;
  40. dev->match_id = PS3_MATCH_ID_LPM;
  41. dev->dev_type = PS3_DEVICE_TYPE_LPM;
  42. /* The current lpm driver only supports a single BE processor. */
  43. result = ps3_repository_read_be_node_id(0, &dev->lpm.node_id);
  44. if (result) {
  45. pr_debug("%s:%d: ps3_repository_read_be_node_id failed \n",
  46. __func__, __LINE__);
  47. goto fail_read_repo;
  48. }
  49. result = ps3_repository_read_lpm_privileges(dev->lpm.node_id, &tmp1,
  50. &dev->lpm.rights);
  51. if (result) {
  52. pr_debug("%s:%d: ps3_repository_read_lpm_privleges failed \n",
  53. __func__, __LINE__);
  54. goto fail_read_repo;
  55. }
  56. lv1_get_logical_partition_id(&tmp2);
  57. if (tmp1 != tmp2) {
  58. pr_debug("%s:%d: wrong lpar\n",
  59. __func__, __LINE__);
  60. result = -ENODEV;
  61. goto fail_rights;
  62. }
  63. if (!(dev->lpm.rights & PS3_LPM_RIGHTS_USE_LPM)) {
  64. pr_debug("%s:%d: don't have rights to use lpm\n",
  65. __func__, __LINE__);
  66. result = -EPERM;
  67. goto fail_rights;
  68. }
  69. pr_debug("%s:%d: pu_id %lu, rights %lu(%lxh)\n",
  70. __func__, __LINE__, dev->lpm.pu_id, dev->lpm.rights,
  71. dev->lpm.rights);
  72. result = ps3_repository_read_pu_id(0, &dev->lpm.pu_id);
  73. if (result) {
  74. pr_debug("%s:%d: ps3_repository_read_pu_id failed \n",
  75. __func__, __LINE__);
  76. goto fail_read_repo;
  77. }
  78. result = ps3_system_bus_device_register(dev);
  79. if (result) {
  80. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  81. __func__, __LINE__);
  82. goto fail_register;
  83. }
  84. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  85. return 0;
  86. fail_register:
  87. fail_rights:
  88. fail_read_repo:
  89. kfree(dev);
  90. pr_debug(" <- %s:%d: failed\n", __func__, __LINE__);
  91. return result;
  92. }
  93. /**
  94. * ps3_setup_gelic_device - Setup and register a gelic device instance.
  95. *
  96. * Allocates memory for a struct ps3_system_bus_device instance, initialises the
  97. * structure members, and registers the device instance with the system bus.
  98. */
  99. static int __init ps3_setup_gelic_device(
  100. const struct ps3_repository_device *repo)
  101. {
  102. int result;
  103. struct layout {
  104. struct ps3_system_bus_device dev;
  105. struct ps3_dma_region d_region;
  106. } *p;
  107. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  108. BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
  109. BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_GELIC);
  110. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  111. if (!p) {
  112. result = -ENOMEM;
  113. goto fail_malloc;
  114. }
  115. p->dev.match_id = PS3_MATCH_ID_GELIC;
  116. p->dev.dev_type = PS3_DEVICE_TYPE_SB;
  117. p->dev.bus_id = repo->bus_id;
  118. p->dev.dev_id = repo->dev_id;
  119. p->dev.d_region = &p->d_region;
  120. result = ps3_repository_find_interrupt(repo,
  121. PS3_INTERRUPT_TYPE_EVENT_PORT, &p->dev.interrupt_id);
  122. if (result) {
  123. pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
  124. __func__, __LINE__);
  125. goto fail_find_interrupt;
  126. }
  127. BUG_ON(p->dev.interrupt_id != 0);
  128. result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
  129. PS3_DMA_OTHER, NULL, 0);
  130. if (result) {
  131. pr_debug("%s:%d ps3_dma_region_init failed\n",
  132. __func__, __LINE__);
  133. goto fail_dma_init;
  134. }
  135. result = ps3_system_bus_device_register(&p->dev);
  136. if (result) {
  137. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  138. __func__, __LINE__);
  139. goto fail_device_register;
  140. }
  141. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  142. return result;
  143. fail_device_register:
  144. fail_dma_init:
  145. fail_find_interrupt:
  146. kfree(p);
  147. fail_malloc:
  148. pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
  149. return result;
  150. }
  151. static int __init_refok ps3_setup_uhc_device(
  152. const struct ps3_repository_device *repo, enum ps3_match_id match_id,
  153. enum ps3_interrupt_type interrupt_type, enum ps3_reg_type reg_type)
  154. {
  155. int result;
  156. struct layout {
  157. struct ps3_system_bus_device dev;
  158. struct ps3_dma_region d_region;
  159. struct ps3_mmio_region m_region;
  160. } *p;
  161. u64 bus_addr;
  162. u64 len;
  163. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  164. BUG_ON(repo->bus_type != PS3_BUS_TYPE_SB);
  165. BUG_ON(repo->dev_type != PS3_DEV_TYPE_SB_USB);
  166. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  167. if (!p) {
  168. result = -ENOMEM;
  169. goto fail_malloc;
  170. }
  171. p->dev.match_id = match_id;
  172. p->dev.dev_type = PS3_DEVICE_TYPE_SB;
  173. p->dev.bus_id = repo->bus_id;
  174. p->dev.dev_id = repo->dev_id;
  175. p->dev.d_region = &p->d_region;
  176. p->dev.m_region = &p->m_region;
  177. result = ps3_repository_find_interrupt(repo,
  178. interrupt_type, &p->dev.interrupt_id);
  179. if (result) {
  180. pr_debug("%s:%d ps3_repository_find_interrupt failed\n",
  181. __func__, __LINE__);
  182. goto fail_find_interrupt;
  183. }
  184. result = ps3_repository_find_reg(repo, reg_type,
  185. &bus_addr, &len);
  186. if (result) {
  187. pr_debug("%s:%d ps3_repository_find_reg failed\n",
  188. __func__, __LINE__);
  189. goto fail_find_reg;
  190. }
  191. result = ps3_dma_region_init(&p->dev, p->dev.d_region, PS3_DMA_64K,
  192. PS3_DMA_INTERNAL, NULL, 0);
  193. if (result) {
  194. pr_debug("%s:%d ps3_dma_region_init failed\n",
  195. __func__, __LINE__);
  196. goto fail_dma_init;
  197. }
  198. result = ps3_mmio_region_init(&p->dev, p->dev.m_region, bus_addr, len,
  199. PS3_MMIO_4K);
  200. if (result) {
  201. pr_debug("%s:%d ps3_mmio_region_init failed\n",
  202. __func__, __LINE__);
  203. goto fail_mmio_init;
  204. }
  205. result = ps3_system_bus_device_register(&p->dev);
  206. if (result) {
  207. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  208. __func__, __LINE__);
  209. goto fail_device_register;
  210. }
  211. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  212. return result;
  213. fail_device_register:
  214. fail_mmio_init:
  215. fail_dma_init:
  216. fail_find_reg:
  217. fail_find_interrupt:
  218. kfree(p);
  219. fail_malloc:
  220. pr_debug(" <- %s:%d: fail.\n", __func__, __LINE__);
  221. return result;
  222. }
  223. static int __init ps3_setup_ehci_device(
  224. const struct ps3_repository_device *repo)
  225. {
  226. return ps3_setup_uhc_device(repo, PS3_MATCH_ID_EHCI,
  227. PS3_INTERRUPT_TYPE_SB_EHCI, PS3_REG_TYPE_SB_EHCI);
  228. }
  229. static int __init ps3_setup_ohci_device(
  230. const struct ps3_repository_device *repo)
  231. {
  232. return ps3_setup_uhc_device(repo, PS3_MATCH_ID_OHCI,
  233. PS3_INTERRUPT_TYPE_SB_OHCI, PS3_REG_TYPE_SB_OHCI);
  234. }
  235. static int __init ps3_setup_vuart_device(enum ps3_match_id match_id,
  236. unsigned int port_number)
  237. {
  238. int result;
  239. struct layout {
  240. struct ps3_system_bus_device dev;
  241. } *p;
  242. pr_debug(" -> %s:%d: match_id %u, port %u\n", __func__, __LINE__,
  243. match_id, port_number);
  244. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  245. if (!p)
  246. return -ENOMEM;
  247. p->dev.match_id = match_id;
  248. p->dev.dev_type = PS3_DEVICE_TYPE_VUART;
  249. p->dev.port_number = port_number;
  250. result = ps3_system_bus_device_register(&p->dev);
  251. if (result)
  252. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  253. __func__, __LINE__);
  254. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  255. return result;
  256. }
  257. static int ps3_setup_storage_dev(const struct ps3_repository_device *repo,
  258. enum ps3_match_id match_id)
  259. {
  260. int result;
  261. struct ps3_storage_device *p;
  262. u64 port, blk_size, num_blocks;
  263. unsigned int num_regions, i;
  264. pr_debug(" -> %s:%u: match_id %u\n", __func__, __LINE__, match_id);
  265. result = ps3_repository_read_stor_dev_info(repo->bus_index,
  266. repo->dev_index, &port,
  267. &blk_size, &num_blocks,
  268. &num_regions);
  269. if (result) {
  270. printk(KERN_ERR "%s:%u: _read_stor_dev_info failed %d\n",
  271. __func__, __LINE__, result);
  272. return -ENODEV;
  273. }
  274. pr_debug("%s:%u: (%u:%u:%u): port %lu blk_size %lu num_blocks %lu "
  275. "num_regions %u\n", __func__, __LINE__, repo->bus_index,
  276. repo->dev_index, repo->dev_type, port, blk_size, num_blocks,
  277. num_regions);
  278. p = kzalloc(sizeof(struct ps3_storage_device) +
  279. num_regions * sizeof(struct ps3_storage_region),
  280. GFP_KERNEL);
  281. if (!p) {
  282. result = -ENOMEM;
  283. goto fail_malloc;
  284. }
  285. p->sbd.match_id = match_id;
  286. p->sbd.dev_type = PS3_DEVICE_TYPE_SB;
  287. p->sbd.bus_id = repo->bus_id;
  288. p->sbd.dev_id = repo->dev_id;
  289. p->sbd.d_region = &p->dma_region;
  290. p->blk_size = blk_size;
  291. p->num_regions = num_regions;
  292. result = ps3_repository_find_interrupt(repo,
  293. PS3_INTERRUPT_TYPE_EVENT_PORT,
  294. &p->sbd.interrupt_id);
  295. if (result) {
  296. printk(KERN_ERR "%s:%u: find_interrupt failed %d\n", __func__,
  297. __LINE__, result);
  298. result = -ENODEV;
  299. goto fail_find_interrupt;
  300. }
  301. for (i = 0; i < num_regions; i++) {
  302. unsigned int id;
  303. u64 start, size;
  304. result = ps3_repository_read_stor_dev_region(repo->bus_index,
  305. repo->dev_index,
  306. i, &id, &start,
  307. &size);
  308. if (result) {
  309. printk(KERN_ERR
  310. "%s:%u: read_stor_dev_region failed %d\n",
  311. __func__, __LINE__, result);
  312. result = -ENODEV;
  313. goto fail_read_region;
  314. }
  315. pr_debug("%s:%u: region %u: id %u start %lu size %lu\n",
  316. __func__, __LINE__, i, id, start, size);
  317. p->regions[i].id = id;
  318. p->regions[i].start = start;
  319. p->regions[i].size = size;
  320. }
  321. result = ps3_system_bus_device_register(&p->sbd);
  322. if (result) {
  323. pr_debug("%s:%u ps3_system_bus_device_register failed\n",
  324. __func__, __LINE__);
  325. goto fail_device_register;
  326. }
  327. pr_debug(" <- %s:%u\n", __func__, __LINE__);
  328. return 0;
  329. fail_device_register:
  330. fail_read_region:
  331. fail_find_interrupt:
  332. kfree(p);
  333. fail_malloc:
  334. pr_debug(" <- %s:%u: fail.\n", __func__, __LINE__);
  335. return result;
  336. }
  337. static int __init ps3_register_vuart_devices(void)
  338. {
  339. int result;
  340. unsigned int port_number;
  341. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  342. result = ps3_repository_read_vuart_av_port(&port_number);
  343. if (result)
  344. port_number = 0; /* av default */
  345. result = ps3_setup_vuart_device(PS3_MATCH_ID_AV_SETTINGS, port_number);
  346. WARN_ON(result);
  347. result = ps3_repository_read_vuart_sysmgr_port(&port_number);
  348. if (result)
  349. port_number = 2; /* sysmgr default */
  350. result = ps3_setup_vuart_device(PS3_MATCH_ID_SYSTEM_MANAGER,
  351. port_number);
  352. WARN_ON(result);
  353. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  354. return result;
  355. }
  356. static int __init ps3_register_sound_devices(void)
  357. {
  358. int result;
  359. struct layout {
  360. struct ps3_system_bus_device dev;
  361. struct ps3_dma_region d_region;
  362. struct ps3_mmio_region m_region;
  363. } *p;
  364. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  365. p = kzalloc(sizeof(*p), GFP_KERNEL);
  366. if (!p)
  367. return -ENOMEM;
  368. p->dev.match_id = PS3_MATCH_ID_SOUND;
  369. p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
  370. p->dev.d_region = &p->d_region;
  371. p->dev.m_region = &p->m_region;
  372. result = ps3_system_bus_device_register(&p->dev);
  373. if (result)
  374. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  375. __func__, __LINE__);
  376. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  377. return result;
  378. }
  379. static int __init ps3_register_graphics_devices(void)
  380. {
  381. int result;
  382. struct layout {
  383. struct ps3_system_bus_device dev;
  384. } *p;
  385. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  386. p = kzalloc(sizeof(struct layout), GFP_KERNEL);
  387. if (!p)
  388. return -ENOMEM;
  389. p->dev.match_id = PS3_MATCH_ID_GRAPHICS;
  390. p->dev.dev_type = PS3_DEVICE_TYPE_IOC0;
  391. result = ps3_system_bus_device_register(&p->dev);
  392. if (result)
  393. pr_debug("%s:%d ps3_system_bus_device_register failed\n",
  394. __func__, __LINE__);
  395. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  396. return result;
  397. }
  398. /**
  399. * ps3_setup_dynamic_device - Setup a dynamic device from the repository
  400. */
  401. static int ps3_setup_dynamic_device(const struct ps3_repository_device *repo)
  402. {
  403. int result;
  404. switch (repo->dev_type) {
  405. case PS3_DEV_TYPE_STOR_DISK:
  406. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_DISK);
  407. /* Some devices are not accessable from the Other OS lpar. */
  408. if (result == -ENODEV) {
  409. result = 0;
  410. pr_debug("%s:%u: not accessable\n", __func__,
  411. __LINE__);
  412. }
  413. if (result)
  414. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  415. __func__, __LINE__);
  416. break;
  417. case PS3_DEV_TYPE_STOR_ROM:
  418. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_ROM);
  419. if (result)
  420. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  421. __func__, __LINE__);
  422. break;
  423. case PS3_DEV_TYPE_STOR_FLASH:
  424. result = ps3_setup_storage_dev(repo, PS3_MATCH_ID_STOR_FLASH);
  425. if (result)
  426. pr_debug("%s:%u ps3_setup_storage_dev failed\n",
  427. __func__, __LINE__);
  428. break;
  429. default:
  430. result = 0;
  431. pr_debug("%s:%u: unsupported dev_type %u\n", __func__, __LINE__,
  432. repo->dev_type);
  433. }
  434. return result;
  435. }
  436. /**
  437. * ps3_setup_static_device - Setup a static device from the repository
  438. */
  439. static int __init ps3_setup_static_device(const struct ps3_repository_device *repo)
  440. {
  441. int result;
  442. switch (repo->dev_type) {
  443. case PS3_DEV_TYPE_SB_GELIC:
  444. result = ps3_setup_gelic_device(repo);
  445. if (result) {
  446. pr_debug("%s:%d ps3_setup_gelic_device failed\n",
  447. __func__, __LINE__);
  448. }
  449. break;
  450. case PS3_DEV_TYPE_SB_USB:
  451. /* Each USB device has both an EHCI and an OHCI HC */
  452. result = ps3_setup_ehci_device(repo);
  453. if (result) {
  454. pr_debug("%s:%d ps3_setup_ehci_device failed\n",
  455. __func__, __LINE__);
  456. }
  457. result = ps3_setup_ohci_device(repo);
  458. if (result) {
  459. pr_debug("%s:%d ps3_setup_ohci_device failed\n",
  460. __func__, __LINE__);
  461. }
  462. break;
  463. default:
  464. return ps3_setup_dynamic_device(repo);
  465. }
  466. return result;
  467. }
  468. static void ps3_find_and_add_device(u64 bus_id, u64 dev_id)
  469. {
  470. struct ps3_repository_device repo;
  471. int res;
  472. unsigned int retries;
  473. unsigned long rem;
  474. /*
  475. * On some firmware versions (e.g. 1.90), the device may not show up
  476. * in the repository immediately
  477. */
  478. for (retries = 0; retries < 10; retries++) {
  479. res = ps3_repository_find_device_by_id(&repo, bus_id, dev_id);
  480. if (!res)
  481. goto found;
  482. rem = msleep_interruptible(100);
  483. if (rem)
  484. break;
  485. }
  486. pr_warning("%s:%u: device %lu:%lu not found\n", __func__, __LINE__,
  487. bus_id, dev_id);
  488. return;
  489. found:
  490. if (retries)
  491. pr_debug("%s:%u: device %lu:%lu found after %u retries\n",
  492. __func__, __LINE__, bus_id, dev_id, retries);
  493. ps3_setup_dynamic_device(&repo);
  494. return;
  495. }
  496. #define PS3_NOTIFICATION_DEV_ID ULONG_MAX
  497. #define PS3_NOTIFICATION_INTERRUPT_ID 0
  498. struct ps3_notification_device {
  499. struct ps3_system_bus_device sbd;
  500. spinlock_t lock;
  501. u64 tag;
  502. u64 lv1_status;
  503. struct completion done;
  504. };
  505. enum ps3_notify_type {
  506. notify_device_ready = 0,
  507. notify_region_probe = 1,
  508. notify_region_update = 2,
  509. };
  510. struct ps3_notify_cmd {
  511. u64 operation_code; /* must be zero */
  512. u64 event_mask; /* OR of 1UL << enum ps3_notify_type */
  513. };
  514. struct ps3_notify_event {
  515. u64 event_type; /* enum ps3_notify_type */
  516. u64 bus_id;
  517. u64 dev_id;
  518. u64 dev_type;
  519. u64 dev_port;
  520. };
  521. static irqreturn_t ps3_notification_interrupt(int irq, void *data)
  522. {
  523. struct ps3_notification_device *dev = data;
  524. int res;
  525. u64 tag, status;
  526. spin_lock(&dev->lock);
  527. res = lv1_storage_get_async_status(PS3_NOTIFICATION_DEV_ID, &tag,
  528. &status);
  529. if (tag != dev->tag)
  530. pr_err("%s:%u: tag mismatch, got %lx, expected %lx\n",
  531. __func__, __LINE__, tag, dev->tag);
  532. if (res) {
  533. pr_err("%s:%u: res %d status 0x%lx\n", __func__, __LINE__, res,
  534. status);
  535. } else {
  536. pr_debug("%s:%u: completed, status 0x%lx\n", __func__,
  537. __LINE__, status);
  538. dev->lv1_status = status;
  539. complete(&dev->done);
  540. }
  541. spin_unlock(&dev->lock);
  542. return IRQ_HANDLED;
  543. }
  544. static int ps3_notification_read_write(struct ps3_notification_device *dev,
  545. u64 lpar, int write)
  546. {
  547. const char *op = write ? "write" : "read";
  548. unsigned long flags;
  549. int res;
  550. init_completion(&dev->done);
  551. spin_lock_irqsave(&dev->lock, flags);
  552. res = write ? lv1_storage_write(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  553. &dev->tag)
  554. : lv1_storage_read(dev->sbd.dev_id, 0, 0, 1, 0, lpar,
  555. &dev->tag);
  556. spin_unlock_irqrestore(&dev->lock, flags);
  557. if (res) {
  558. pr_err("%s:%u: %s failed %d\n", __func__, __LINE__, op, res);
  559. return -EPERM;
  560. }
  561. pr_debug("%s:%u: notification %s issued\n", __func__, __LINE__, op);
  562. res = wait_event_interruptible(dev->done.wait,
  563. dev->done.done || kthread_should_stop());
  564. if (kthread_should_stop())
  565. res = -EINTR;
  566. if (res) {
  567. pr_debug("%s:%u: interrupted %s\n", __func__, __LINE__, op);
  568. return res;
  569. }
  570. if (dev->lv1_status) {
  571. pr_err("%s:%u: %s not completed, status 0x%lx\n", __func__,
  572. __LINE__, op, dev->lv1_status);
  573. return -EIO;
  574. }
  575. pr_debug("%s:%u: notification %s completed\n", __func__, __LINE__, op);
  576. return 0;
  577. }
  578. static struct task_struct *probe_task;
  579. /**
  580. * ps3_probe_thread - Background repository probing at system startup.
  581. *
  582. * This implementation only supports background probing on a single bus.
  583. * It uses the hypervisor's storage device notification mechanism to wait until
  584. * a storage device is ready. The device notification mechanism uses a
  585. * pseudo device to asynchronously notify the guest when storage devices become
  586. * ready. The notification device has a block size of 512 bytes.
  587. */
  588. static int ps3_probe_thread(void *data)
  589. {
  590. struct ps3_notification_device dev;
  591. int res;
  592. unsigned int irq;
  593. u64 lpar;
  594. void *buf;
  595. struct ps3_notify_cmd *notify_cmd;
  596. struct ps3_notify_event *notify_event;
  597. pr_debug(" -> %s:%u: kthread started\n", __func__, __LINE__);
  598. buf = kzalloc(512, GFP_KERNEL);
  599. if (!buf)
  600. return -ENOMEM;
  601. lpar = ps3_mm_phys_to_lpar(__pa(buf));
  602. notify_cmd = buf;
  603. notify_event = buf;
  604. /* dummy system bus device */
  605. dev.sbd.bus_id = (u64)data;
  606. dev.sbd.dev_id = PS3_NOTIFICATION_DEV_ID;
  607. dev.sbd.interrupt_id = PS3_NOTIFICATION_INTERRUPT_ID;
  608. res = lv1_open_device(dev.sbd.bus_id, dev.sbd.dev_id, 0);
  609. if (res) {
  610. pr_err("%s:%u: lv1_open_device failed %s\n", __func__,
  611. __LINE__, ps3_result(res));
  612. goto fail_free;
  613. }
  614. res = ps3_sb_event_receive_port_setup(&dev.sbd, PS3_BINDING_CPU_ANY,
  615. &irq);
  616. if (res) {
  617. pr_err("%s:%u: ps3_sb_event_receive_port_setup failed %d\n",
  618. __func__, __LINE__, res);
  619. goto fail_close_device;
  620. }
  621. spin_lock_init(&dev.lock);
  622. res = request_irq(irq, ps3_notification_interrupt, IRQF_DISABLED,
  623. "ps3_notification", &dev);
  624. if (res) {
  625. pr_err("%s:%u: request_irq failed %d\n", __func__, __LINE__,
  626. res);
  627. goto fail_sb_event_receive_port_destroy;
  628. }
  629. /* Setup and write the request for device notification. */
  630. notify_cmd->operation_code = 0; /* must be zero */
  631. notify_cmd->event_mask = 1UL << notify_region_probe;
  632. res = ps3_notification_read_write(&dev, lpar, 1);
  633. if (res)
  634. goto fail_free_irq;
  635. /* Loop here processing the requested notification events. */
  636. do {
  637. try_to_freeze();
  638. memset(notify_event, 0, sizeof(*notify_event));
  639. res = ps3_notification_read_write(&dev, lpar, 0);
  640. if (res)
  641. break;
  642. pr_debug("%s:%u: notify event type 0x%lx bus id %lu dev id %lu"
  643. " type %lu port %lu\n", __func__, __LINE__,
  644. notify_event->event_type, notify_event->bus_id,
  645. notify_event->dev_id, notify_event->dev_type,
  646. notify_event->dev_port);
  647. if (notify_event->event_type != notify_region_probe ||
  648. notify_event->bus_id != dev.sbd.bus_id) {
  649. pr_warning("%s:%u: bad notify_event: event %lu, "
  650. "dev_id %lu, dev_type %lu\n",
  651. __func__, __LINE__, notify_event->event_type,
  652. notify_event->dev_id,
  653. notify_event->dev_type);
  654. continue;
  655. }
  656. ps3_find_and_add_device(dev.sbd.bus_id, notify_event->dev_id);
  657. } while (!kthread_should_stop());
  658. fail_free_irq:
  659. free_irq(irq, &dev);
  660. fail_sb_event_receive_port_destroy:
  661. ps3_sb_event_receive_port_destroy(&dev.sbd, irq);
  662. fail_close_device:
  663. lv1_close_device(dev.sbd.bus_id, dev.sbd.dev_id);
  664. fail_free:
  665. kfree(buf);
  666. probe_task = NULL;
  667. pr_debug(" <- %s:%u: kthread finished\n", __func__, __LINE__);
  668. return 0;
  669. }
  670. /**
  671. * ps3_stop_probe_thread - Stops the background probe thread.
  672. *
  673. */
  674. static int ps3_stop_probe_thread(struct notifier_block *nb, unsigned long code,
  675. void *data)
  676. {
  677. if (probe_task)
  678. kthread_stop(probe_task);
  679. return 0;
  680. }
  681. static struct notifier_block nb = {
  682. .notifier_call = ps3_stop_probe_thread
  683. };
  684. /**
  685. * ps3_start_probe_thread - Starts the background probe thread.
  686. *
  687. */
  688. static int __init ps3_start_probe_thread(enum ps3_bus_type bus_type)
  689. {
  690. int result;
  691. struct task_struct *task;
  692. struct ps3_repository_device repo;
  693. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  694. memset(&repo, 0, sizeof(repo));
  695. repo.bus_type = bus_type;
  696. result = ps3_repository_find_bus(repo.bus_type, 0, &repo.bus_index);
  697. if (result) {
  698. printk(KERN_ERR "%s: Cannot find bus (%d)\n", __func__, result);
  699. return -ENODEV;
  700. }
  701. result = ps3_repository_read_bus_id(repo.bus_index, &repo.bus_id);
  702. if (result) {
  703. printk(KERN_ERR "%s: read_bus_id failed %d\n", __func__,
  704. result);
  705. return -ENODEV;
  706. }
  707. task = kthread_run(ps3_probe_thread, (void *)repo.bus_id,
  708. "ps3-probe-%u", bus_type);
  709. if (IS_ERR(task)) {
  710. result = PTR_ERR(task);
  711. printk(KERN_ERR "%s: kthread_run failed %d\n", __func__,
  712. result);
  713. return result;
  714. }
  715. probe_task = task;
  716. register_reboot_notifier(&nb);
  717. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  718. return 0;
  719. }
  720. /**
  721. * ps3_register_devices - Probe the system and register devices found.
  722. *
  723. * A device_initcall() routine.
  724. */
  725. static int __init ps3_register_devices(void)
  726. {
  727. int result;
  728. if (!firmware_has_feature(FW_FEATURE_PS3_LV1))
  729. return -ENODEV;
  730. pr_debug(" -> %s:%d\n", __func__, __LINE__);
  731. /* ps3_repository_dump_bus_info(); */
  732. result = ps3_start_probe_thread(PS3_BUS_TYPE_STORAGE);
  733. ps3_register_vuart_devices();
  734. ps3_register_graphics_devices();
  735. ps3_repository_find_devices(PS3_BUS_TYPE_SB, ps3_setup_static_device);
  736. ps3_register_sound_devices();
  737. ps3_register_lpm_devices();
  738. pr_debug(" <- %s:%d\n", __func__, __LINE__);
  739. return 0;
  740. }
  741. device_initcall(ps3_register_devices);