css.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * drivers/s390/cio/css.c
  3. * driver for channel subsystem
  4. *
  5. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  8. * Cornelia Huck (cornelia.huck@de.ibm.com)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/errno.h>
  15. #include <linux/list.h>
  16. #include <linux/reboot.h>
  17. #include "css.h"
  18. #include "cio.h"
  19. #include "cio_debug.h"
  20. #include "ioasm.h"
  21. #include "chsc.h"
  22. #include "device.h"
  23. #include "idset.h"
  24. #include "chp.h"
  25. int css_init_done = 0;
  26. static int need_reprobe = 0;
  27. static int max_ssid = 0;
  28. struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
  29. int css_characteristics_avail = 0;
  30. int
  31. for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
  32. {
  33. struct subchannel_id schid;
  34. int ret;
  35. init_subchannel_id(&schid);
  36. ret = -ENODEV;
  37. do {
  38. do {
  39. ret = fn(schid, data);
  40. if (ret)
  41. break;
  42. } while (schid.sch_no++ < __MAX_SUBCHANNEL);
  43. schid.sch_no = 0;
  44. } while (schid.ssid++ < max_ssid);
  45. return ret;
  46. }
  47. struct cb_data {
  48. void *data;
  49. struct idset *set;
  50. int (*fn_known_sch)(struct subchannel *, void *);
  51. int (*fn_unknown_sch)(struct subchannel_id, void *);
  52. };
  53. static int call_fn_known_sch(struct device *dev, void *data)
  54. {
  55. struct subchannel *sch = to_subchannel(dev);
  56. struct cb_data *cb = data;
  57. int rc = 0;
  58. idset_sch_del(cb->set, sch->schid);
  59. if (cb->fn_known_sch)
  60. rc = cb->fn_known_sch(sch, cb->data);
  61. return rc;
  62. }
  63. static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
  64. {
  65. struct cb_data *cb = data;
  66. int rc = 0;
  67. if (idset_sch_contains(cb->set, schid))
  68. rc = cb->fn_unknown_sch(schid, cb->data);
  69. return rc;
  70. }
  71. int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
  72. int (*fn_unknown)(struct subchannel_id,
  73. void *), void *data)
  74. {
  75. struct cb_data cb;
  76. int rc;
  77. cb.set = idset_sch_new();
  78. if (!cb.set)
  79. return -ENOMEM;
  80. idset_fill(cb.set);
  81. cb.data = data;
  82. cb.fn_known_sch = fn_known;
  83. cb.fn_unknown_sch = fn_unknown;
  84. /* Process registered subchannels. */
  85. rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
  86. if (rc)
  87. goto out;
  88. /* Process unregistered subchannels. */
  89. if (fn_unknown)
  90. rc = for_each_subchannel(call_fn_unknown_sch, &cb);
  91. out:
  92. idset_free(cb.set);
  93. return rc;
  94. }
  95. static struct subchannel *
  96. css_alloc_subchannel(struct subchannel_id schid)
  97. {
  98. struct subchannel *sch;
  99. int ret;
  100. sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
  101. if (sch == NULL)
  102. return ERR_PTR(-ENOMEM);
  103. ret = cio_validate_subchannel (sch, schid);
  104. if (ret < 0) {
  105. kfree(sch);
  106. return ERR_PTR(ret);
  107. }
  108. if (sch->st != SUBCHANNEL_TYPE_IO) {
  109. /* For now we ignore all non-io subchannels. */
  110. kfree(sch);
  111. return ERR_PTR(-EINVAL);
  112. }
  113. /*
  114. * Set intparm to subchannel address.
  115. * This is fine even on 64bit since the subchannel is always located
  116. * under 2G.
  117. */
  118. sch->schib.pmcw.intparm = (u32)(addr_t)sch;
  119. ret = cio_modify(sch);
  120. if (ret) {
  121. kfree(sch->lock);
  122. kfree(sch);
  123. return ERR_PTR(ret);
  124. }
  125. return sch;
  126. }
  127. static void
  128. css_free_subchannel(struct subchannel *sch)
  129. {
  130. if (sch) {
  131. /* Reset intparm to zeroes. */
  132. sch->schib.pmcw.intparm = 0;
  133. cio_modify(sch);
  134. kfree(sch->lock);
  135. kfree(sch);
  136. }
  137. }
  138. static void
  139. css_subchannel_release(struct device *dev)
  140. {
  141. struct subchannel *sch;
  142. sch = to_subchannel(dev);
  143. if (!cio_is_console(sch->schid)) {
  144. kfree(sch->lock);
  145. kfree(sch);
  146. }
  147. }
  148. static int css_sch_device_register(struct subchannel *sch)
  149. {
  150. int ret;
  151. mutex_lock(&sch->reg_mutex);
  152. ret = device_register(&sch->dev);
  153. mutex_unlock(&sch->reg_mutex);
  154. return ret;
  155. }
  156. void css_sch_device_unregister(struct subchannel *sch)
  157. {
  158. mutex_lock(&sch->reg_mutex);
  159. device_unregister(&sch->dev);
  160. mutex_unlock(&sch->reg_mutex);
  161. }
  162. static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
  163. {
  164. int i;
  165. int mask;
  166. memset(ssd, 0, sizeof(struct chsc_ssd_info));
  167. ssd->path_mask = pmcw->pim;
  168. for (i = 0; i < 8; i++) {
  169. mask = 0x80 >> i;
  170. if (pmcw->pim & mask) {
  171. chp_id_init(&ssd->chpid[i]);
  172. ssd->chpid[i].id = pmcw->chpid[i];
  173. }
  174. }
  175. }
  176. static void ssd_register_chpids(struct chsc_ssd_info *ssd)
  177. {
  178. int i;
  179. int mask;
  180. for (i = 0; i < 8; i++) {
  181. mask = 0x80 >> i;
  182. if (ssd->path_mask & mask)
  183. if (!chp_is_registered(ssd->chpid[i]))
  184. chp_new(ssd->chpid[i]);
  185. }
  186. }
  187. void css_update_ssd_info(struct subchannel *sch)
  188. {
  189. int ret;
  190. if (cio_is_console(sch->schid)) {
  191. /* Console is initialized too early for functions requiring
  192. * memory allocation. */
  193. ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
  194. } else {
  195. ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
  196. if (ret)
  197. ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
  198. ssd_register_chpids(&sch->ssd_info);
  199. }
  200. }
  201. static int css_register_subchannel(struct subchannel *sch)
  202. {
  203. int ret;
  204. /* Initialize the subchannel structure */
  205. sch->dev.parent = &channel_subsystems[0]->device;
  206. sch->dev.bus = &css_bus_type;
  207. sch->dev.release = &css_subchannel_release;
  208. sch->dev.groups = subch_attr_groups;
  209. /*
  210. * We don't want to generate uevents for I/O subchannels that don't
  211. * have a working ccw device behind them since they will be
  212. * unregistered before they can be used anyway, so we delay the add
  213. * uevent until after device recognition was successful.
  214. */
  215. if (!cio_is_console(sch->schid))
  216. /* Console is special, no need to suppress. */
  217. sch->dev.uevent_suppress = 1;
  218. css_update_ssd_info(sch);
  219. /* make it known to the system */
  220. ret = css_sch_device_register(sch);
  221. if (ret) {
  222. CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
  223. sch->schid.ssid, sch->schid.sch_no, ret);
  224. return ret;
  225. }
  226. return ret;
  227. }
  228. static int css_probe_device(struct subchannel_id schid)
  229. {
  230. int ret;
  231. struct subchannel *sch;
  232. sch = css_alloc_subchannel(schid);
  233. if (IS_ERR(sch))
  234. return PTR_ERR(sch);
  235. ret = css_register_subchannel(sch);
  236. if (ret)
  237. css_free_subchannel(sch);
  238. return ret;
  239. }
  240. static int
  241. check_subchannel(struct device * dev, void * data)
  242. {
  243. struct subchannel *sch;
  244. struct subchannel_id *schid = data;
  245. sch = to_subchannel(dev);
  246. return schid_equal(&sch->schid, schid);
  247. }
  248. struct subchannel *
  249. get_subchannel_by_schid(struct subchannel_id schid)
  250. {
  251. struct device *dev;
  252. dev = bus_find_device(&css_bus_type, NULL,
  253. &schid, check_subchannel);
  254. return dev ? to_subchannel(dev) : NULL;
  255. }
  256. /**
  257. * css_sch_is_valid() - check if a subchannel is valid
  258. * @schib: subchannel information block for the subchannel
  259. */
  260. int css_sch_is_valid(struct schib *schib)
  261. {
  262. if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
  263. return 0;
  264. return 1;
  265. }
  266. EXPORT_SYMBOL_GPL(css_sch_is_valid);
  267. static int css_get_subchannel_status(struct subchannel *sch)
  268. {
  269. struct schib schib;
  270. if (stsch(sch->schid, &schib))
  271. return CIO_GONE;
  272. if (!css_sch_is_valid(&schib))
  273. return CIO_GONE;
  274. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  275. return CIO_REVALIDATE;
  276. if (!sch->lpm)
  277. return CIO_NO_PATH;
  278. return CIO_OPER;
  279. }
  280. static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
  281. {
  282. int event, ret, disc;
  283. unsigned long flags;
  284. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
  285. spin_lock_irqsave(sch->lock, flags);
  286. disc = device_is_disconnected(sch);
  287. if (disc && slow) {
  288. /* Disconnected devices are evaluated directly only.*/
  289. spin_unlock_irqrestore(sch->lock, flags);
  290. return 0;
  291. }
  292. /* No interrupt after machine check - kill pending timers. */
  293. device_kill_pending_timer(sch);
  294. if (!disc && !slow) {
  295. /* Non-disconnected devices are evaluated on the slow path. */
  296. spin_unlock_irqrestore(sch->lock, flags);
  297. return -EAGAIN;
  298. }
  299. event = css_get_subchannel_status(sch);
  300. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  301. sch->schid.ssid, sch->schid.sch_no, event,
  302. disc ? "disconnected" : "normal",
  303. slow ? "slow" : "fast");
  304. /* Analyze subchannel status. */
  305. action = NONE;
  306. switch (event) {
  307. case CIO_NO_PATH:
  308. if (disc) {
  309. /* Check if paths have become available. */
  310. action = REPROBE;
  311. break;
  312. }
  313. /* fall through */
  314. case CIO_GONE:
  315. /* Prevent unwanted effects when opening lock. */
  316. cio_disable_subchannel(sch);
  317. device_set_disconnected(sch);
  318. /* Ask driver what to do with device. */
  319. action = UNREGISTER;
  320. if (sch->driver && sch->driver->notify) {
  321. spin_unlock_irqrestore(sch->lock, flags);
  322. ret = sch->driver->notify(sch, event);
  323. spin_lock_irqsave(sch->lock, flags);
  324. if (ret)
  325. action = NONE;
  326. }
  327. break;
  328. case CIO_REVALIDATE:
  329. /* Device will be removed, so no notify necessary. */
  330. if (disc)
  331. /* Reprobe because immediate unregister might block. */
  332. action = REPROBE;
  333. else
  334. action = UNREGISTER_PROBE;
  335. break;
  336. case CIO_OPER:
  337. if (disc)
  338. /* Get device operational again. */
  339. action = REPROBE;
  340. break;
  341. }
  342. /* Perform action. */
  343. ret = 0;
  344. switch (action) {
  345. case UNREGISTER:
  346. case UNREGISTER_PROBE:
  347. /* Unregister device (will use subchannel lock). */
  348. spin_unlock_irqrestore(sch->lock, flags);
  349. css_sch_device_unregister(sch);
  350. spin_lock_irqsave(sch->lock, flags);
  351. /* Reset intparm to zeroes. */
  352. sch->schib.pmcw.intparm = 0;
  353. cio_modify(sch);
  354. break;
  355. case REPROBE:
  356. device_trigger_reprobe(sch);
  357. break;
  358. default:
  359. break;
  360. }
  361. spin_unlock_irqrestore(sch->lock, flags);
  362. /* Probe if necessary. */
  363. if (action == UNREGISTER_PROBE)
  364. ret = css_probe_device(sch->schid);
  365. return ret;
  366. }
  367. static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
  368. {
  369. struct schib schib;
  370. if (!slow) {
  371. /* Will be done on the slow path. */
  372. return -EAGAIN;
  373. }
  374. if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
  375. /* Unusable - ignore. */
  376. return 0;
  377. }
  378. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
  379. "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
  380. return css_probe_device(schid);
  381. }
  382. static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
  383. {
  384. struct subchannel *sch;
  385. int ret;
  386. sch = get_subchannel_by_schid(schid);
  387. if (sch) {
  388. ret = css_evaluate_known_subchannel(sch, slow);
  389. put_device(&sch->dev);
  390. } else
  391. ret = css_evaluate_new_subchannel(schid, slow);
  392. if (ret == -EAGAIN)
  393. css_schedule_eval(schid);
  394. }
  395. static struct idset *slow_subchannel_set;
  396. static spinlock_t slow_subchannel_lock;
  397. static int __init slow_subchannel_init(void)
  398. {
  399. spin_lock_init(&slow_subchannel_lock);
  400. slow_subchannel_set = idset_sch_new();
  401. if (!slow_subchannel_set) {
  402. CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
  403. return -ENOMEM;
  404. }
  405. return 0;
  406. }
  407. static int slow_eval_known_fn(struct subchannel *sch, void *data)
  408. {
  409. int eval;
  410. int rc;
  411. spin_lock_irq(&slow_subchannel_lock);
  412. eval = idset_sch_contains(slow_subchannel_set, sch->schid);
  413. idset_sch_del(slow_subchannel_set, sch->schid);
  414. spin_unlock_irq(&slow_subchannel_lock);
  415. if (eval) {
  416. rc = css_evaluate_known_subchannel(sch, 1);
  417. if (rc == -EAGAIN)
  418. css_schedule_eval(sch->schid);
  419. }
  420. return 0;
  421. }
  422. static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
  423. {
  424. int eval;
  425. int rc = 0;
  426. spin_lock_irq(&slow_subchannel_lock);
  427. eval = idset_sch_contains(slow_subchannel_set, schid);
  428. idset_sch_del(slow_subchannel_set, schid);
  429. spin_unlock_irq(&slow_subchannel_lock);
  430. if (eval) {
  431. rc = css_evaluate_new_subchannel(schid, 1);
  432. switch (rc) {
  433. case -EAGAIN:
  434. css_schedule_eval(schid);
  435. rc = 0;
  436. break;
  437. case -ENXIO:
  438. case -ENOMEM:
  439. case -EIO:
  440. /* These should abort looping */
  441. break;
  442. default:
  443. rc = 0;
  444. }
  445. }
  446. return rc;
  447. }
  448. static void css_slow_path_func(struct work_struct *unused)
  449. {
  450. CIO_TRACE_EVENT(4, "slowpath");
  451. for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
  452. NULL);
  453. }
  454. static DECLARE_WORK(slow_path_work, css_slow_path_func);
  455. struct workqueue_struct *slow_path_wq;
  456. void css_schedule_eval(struct subchannel_id schid)
  457. {
  458. unsigned long flags;
  459. spin_lock_irqsave(&slow_subchannel_lock, flags);
  460. idset_sch_add(slow_subchannel_set, schid);
  461. queue_work(slow_path_wq, &slow_path_work);
  462. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  463. }
  464. void css_schedule_eval_all(void)
  465. {
  466. unsigned long flags;
  467. spin_lock_irqsave(&slow_subchannel_lock, flags);
  468. idset_fill(slow_subchannel_set);
  469. queue_work(slow_path_wq, &slow_path_work);
  470. spin_unlock_irqrestore(&slow_subchannel_lock, flags);
  471. }
  472. void css_wait_for_slow_path(void)
  473. {
  474. flush_workqueue(ccw_device_notify_work);
  475. flush_workqueue(slow_path_wq);
  476. }
  477. /* Reprobe subchannel if unregistered. */
  478. static int reprobe_subchannel(struct subchannel_id schid, void *data)
  479. {
  480. int ret;
  481. CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
  482. schid.ssid, schid.sch_no);
  483. if (need_reprobe)
  484. return -EAGAIN;
  485. ret = css_probe_device(schid);
  486. switch (ret) {
  487. case 0:
  488. break;
  489. case -ENXIO:
  490. case -ENOMEM:
  491. case -EIO:
  492. /* These should abort looping */
  493. break;
  494. default:
  495. ret = 0;
  496. }
  497. return ret;
  498. }
  499. /* Work function used to reprobe all unregistered subchannels. */
  500. static void reprobe_all(struct work_struct *unused)
  501. {
  502. int ret;
  503. CIO_MSG_EVENT(2, "reprobe start\n");
  504. need_reprobe = 0;
  505. /* Make sure initial subchannel scan is done. */
  506. wait_event(ccw_device_init_wq,
  507. atomic_read(&ccw_device_init_count) == 0);
  508. ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
  509. CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
  510. need_reprobe);
  511. }
  512. static DECLARE_WORK(css_reprobe_work, reprobe_all);
  513. /* Schedule reprobing of all unregistered subchannels. */
  514. void css_schedule_reprobe(void)
  515. {
  516. need_reprobe = 1;
  517. queue_work(slow_path_wq, &css_reprobe_work);
  518. }
  519. EXPORT_SYMBOL_GPL(css_schedule_reprobe);
  520. /*
  521. * Called from the machine check handler for subchannel report words.
  522. */
  523. void css_process_crw(int rsid1, int rsid2)
  524. {
  525. struct subchannel_id mchk_schid;
  526. CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
  527. rsid1, rsid2);
  528. init_subchannel_id(&mchk_schid);
  529. mchk_schid.sch_no = rsid1;
  530. if (rsid2 != 0)
  531. mchk_schid.ssid = (rsid2 >> 8) & 3;
  532. /*
  533. * Since we are always presented with IPI in the CRW, we have to
  534. * use stsch() to find out if the subchannel in question has come
  535. * or gone.
  536. */
  537. css_evaluate_subchannel(mchk_schid, 0);
  538. }
  539. static int __init
  540. __init_channel_subsystem(struct subchannel_id schid, void *data)
  541. {
  542. struct subchannel *sch;
  543. int ret;
  544. if (cio_is_console(schid))
  545. sch = cio_get_console_subchannel();
  546. else {
  547. sch = css_alloc_subchannel(schid);
  548. if (IS_ERR(sch))
  549. ret = PTR_ERR(sch);
  550. else
  551. ret = 0;
  552. switch (ret) {
  553. case 0:
  554. break;
  555. case -ENOMEM:
  556. panic("Out of memory in init_channel_subsystem\n");
  557. /* -ENXIO: no more subchannels. */
  558. case -ENXIO:
  559. return ret;
  560. /* -EIO: this subchannel set not supported. */
  561. case -EIO:
  562. return ret;
  563. default:
  564. return 0;
  565. }
  566. }
  567. /*
  568. * We register ALL valid subchannels in ioinfo, even those
  569. * that have been present before init_channel_subsystem.
  570. * These subchannels can't have been registered yet (kmalloc
  571. * not working) so we do it now. This is true e.g. for the
  572. * console subchannel.
  573. */
  574. css_register_subchannel(sch);
  575. return 0;
  576. }
  577. static void __init
  578. css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
  579. {
  580. if (css_characteristics_avail && css_general_characteristics.mcss) {
  581. css->global_pgid.pgid_high.ext_cssid.version = 0x80;
  582. css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
  583. } else {
  584. #ifdef CONFIG_SMP
  585. css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
  586. #else
  587. css->global_pgid.pgid_high.cpu_addr = 0;
  588. #endif
  589. }
  590. css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
  591. css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
  592. css->global_pgid.tod_high = tod_high;
  593. }
  594. static void
  595. channel_subsystem_release(struct device *dev)
  596. {
  597. struct channel_subsystem *css;
  598. css = to_css(dev);
  599. mutex_destroy(&css->mutex);
  600. kfree(css);
  601. }
  602. static ssize_t
  603. css_cm_enable_show(struct device *dev, struct device_attribute *attr,
  604. char *buf)
  605. {
  606. struct channel_subsystem *css = to_css(dev);
  607. int ret;
  608. if (!css)
  609. return 0;
  610. mutex_lock(&css->mutex);
  611. ret = sprintf(buf, "%x\n", css->cm_enabled);
  612. mutex_unlock(&css->mutex);
  613. return ret;
  614. }
  615. static ssize_t
  616. css_cm_enable_store(struct device *dev, struct device_attribute *attr,
  617. const char *buf, size_t count)
  618. {
  619. struct channel_subsystem *css = to_css(dev);
  620. int ret;
  621. mutex_lock(&css->mutex);
  622. switch (buf[0]) {
  623. case '0':
  624. ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
  625. break;
  626. case '1':
  627. ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
  628. break;
  629. default:
  630. ret = -EINVAL;
  631. }
  632. mutex_unlock(&css->mutex);
  633. return ret < 0 ? ret : count;
  634. }
  635. static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
  636. static int __init setup_css(int nr)
  637. {
  638. u32 tod_high;
  639. int ret;
  640. struct channel_subsystem *css;
  641. css = channel_subsystems[nr];
  642. memset(css, 0, sizeof(struct channel_subsystem));
  643. css->pseudo_subchannel =
  644. kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
  645. if (!css->pseudo_subchannel)
  646. return -ENOMEM;
  647. css->pseudo_subchannel->dev.parent = &css->device;
  648. css->pseudo_subchannel->dev.release = css_subchannel_release;
  649. sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
  650. ret = cio_create_sch_lock(css->pseudo_subchannel);
  651. if (ret) {
  652. kfree(css->pseudo_subchannel);
  653. return ret;
  654. }
  655. mutex_init(&css->mutex);
  656. css->valid = 1;
  657. css->cssid = nr;
  658. sprintf(css->device.bus_id, "css%x", nr);
  659. css->device.release = channel_subsystem_release;
  660. tod_high = (u32) (get_clock() >> 32);
  661. css_generate_pgid(css, tod_high);
  662. return 0;
  663. }
  664. static int css_reboot_event(struct notifier_block *this,
  665. unsigned long event,
  666. void *ptr)
  667. {
  668. int ret, i;
  669. ret = NOTIFY_DONE;
  670. for (i = 0; i <= __MAX_CSSID; i++) {
  671. struct channel_subsystem *css;
  672. css = channel_subsystems[i];
  673. mutex_lock(&css->mutex);
  674. if (css->cm_enabled)
  675. if (chsc_secm(css, 0))
  676. ret = NOTIFY_BAD;
  677. mutex_unlock(&css->mutex);
  678. }
  679. return ret;
  680. }
  681. static struct notifier_block css_reboot_notifier = {
  682. .notifier_call = css_reboot_event,
  683. };
  684. /*
  685. * Now that the driver core is running, we can setup our channel subsystem.
  686. * The struct subchannel's are created during probing (except for the
  687. * static console subchannel).
  688. */
  689. static int __init
  690. init_channel_subsystem (void)
  691. {
  692. int ret, i;
  693. ret = chsc_determine_css_characteristics();
  694. if (ret == -ENOMEM)
  695. goto out; /* No need to continue. */
  696. if (ret == 0)
  697. css_characteristics_avail = 1;
  698. ret = chsc_alloc_sei_area();
  699. if (ret)
  700. goto out;
  701. ret = slow_subchannel_init();
  702. if (ret)
  703. goto out;
  704. if ((ret = bus_register(&css_bus_type)))
  705. goto out;
  706. /* Try to enable MSS. */
  707. ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
  708. switch (ret) {
  709. case 0: /* Success. */
  710. max_ssid = __MAX_SSID;
  711. break;
  712. case -ENOMEM:
  713. goto out_bus;
  714. default:
  715. max_ssid = 0;
  716. }
  717. /* Setup css structure. */
  718. for (i = 0; i <= __MAX_CSSID; i++) {
  719. struct channel_subsystem *css;
  720. css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
  721. if (!css) {
  722. ret = -ENOMEM;
  723. goto out_unregister;
  724. }
  725. channel_subsystems[i] = css;
  726. ret = setup_css(i);
  727. if (ret)
  728. goto out_free;
  729. ret = device_register(&css->device);
  730. if (ret)
  731. goto out_free_all;
  732. if (css_characteristics_avail &&
  733. css_chsc_characteristics.secm) {
  734. ret = device_create_file(&css->device,
  735. &dev_attr_cm_enable);
  736. if (ret)
  737. goto out_device;
  738. }
  739. ret = device_register(&css->pseudo_subchannel->dev);
  740. if (ret)
  741. goto out_file;
  742. }
  743. ret = register_reboot_notifier(&css_reboot_notifier);
  744. if (ret)
  745. goto out_pseudo;
  746. css_init_done = 1;
  747. ctl_set_bit(6, 28);
  748. for_each_subchannel(__init_channel_subsystem, NULL);
  749. return 0;
  750. out_pseudo:
  751. device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
  752. out_file:
  753. device_remove_file(&channel_subsystems[i]->device,
  754. &dev_attr_cm_enable);
  755. out_device:
  756. device_unregister(&channel_subsystems[i]->device);
  757. out_free_all:
  758. kfree(channel_subsystems[i]->pseudo_subchannel->lock);
  759. kfree(channel_subsystems[i]->pseudo_subchannel);
  760. out_free:
  761. kfree(channel_subsystems[i]);
  762. out_unregister:
  763. while (i > 0) {
  764. struct channel_subsystem *css;
  765. i--;
  766. css = channel_subsystems[i];
  767. device_unregister(&css->pseudo_subchannel->dev);
  768. if (css_characteristics_avail && css_chsc_characteristics.secm)
  769. device_remove_file(&css->device,
  770. &dev_attr_cm_enable);
  771. device_unregister(&css->device);
  772. }
  773. out_bus:
  774. bus_unregister(&css_bus_type);
  775. out:
  776. chsc_free_sei_area();
  777. kfree(slow_subchannel_set);
  778. printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
  779. ret);
  780. return ret;
  781. }
  782. int sch_is_pseudo_sch(struct subchannel *sch)
  783. {
  784. return sch == to_css(sch->dev.parent)->pseudo_subchannel;
  785. }
  786. /*
  787. * find a driver for a subchannel. They identify by the subchannel
  788. * type with the exception that the console subchannel driver has its own
  789. * subchannel type although the device is an i/o subchannel
  790. */
  791. static int
  792. css_bus_match (struct device *dev, struct device_driver *drv)
  793. {
  794. struct subchannel *sch = to_subchannel(dev);
  795. struct css_driver *driver = to_cssdriver(drv);
  796. if (sch->st == driver->subchannel_type)
  797. return 1;
  798. return 0;
  799. }
  800. static int css_probe(struct device *dev)
  801. {
  802. struct subchannel *sch;
  803. int ret;
  804. sch = to_subchannel(dev);
  805. sch->driver = to_cssdriver(dev->driver);
  806. ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
  807. if (ret)
  808. sch->driver = NULL;
  809. return ret;
  810. }
  811. static int css_remove(struct device *dev)
  812. {
  813. struct subchannel *sch;
  814. int ret;
  815. sch = to_subchannel(dev);
  816. ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
  817. sch->driver = NULL;
  818. return ret;
  819. }
  820. static void css_shutdown(struct device *dev)
  821. {
  822. struct subchannel *sch;
  823. sch = to_subchannel(dev);
  824. if (sch->driver && sch->driver->shutdown)
  825. sch->driver->shutdown(sch);
  826. }
  827. struct bus_type css_bus_type = {
  828. .name = "css",
  829. .match = css_bus_match,
  830. .probe = css_probe,
  831. .remove = css_remove,
  832. .shutdown = css_shutdown,
  833. };
  834. /**
  835. * css_driver_register - register a css driver
  836. * @cdrv: css driver to register
  837. *
  838. * This is mainly a wrapper around driver_register that sets name
  839. * and bus_type in the embedded struct device_driver correctly.
  840. */
  841. int css_driver_register(struct css_driver *cdrv)
  842. {
  843. cdrv->drv.name = cdrv->name;
  844. cdrv->drv.bus = &css_bus_type;
  845. cdrv->drv.owner = cdrv->owner;
  846. return driver_register(&cdrv->drv);
  847. }
  848. EXPORT_SYMBOL_GPL(css_driver_register);
  849. /**
  850. * css_driver_unregister - unregister a css driver
  851. * @cdrv: css driver to unregister
  852. *
  853. * This is a wrapper around driver_unregister.
  854. */
  855. void css_driver_unregister(struct css_driver *cdrv)
  856. {
  857. driver_unregister(&cdrv->drv);
  858. }
  859. EXPORT_SYMBOL_GPL(css_driver_unregister);
  860. subsys_initcall(init_channel_subsystem);
  861. MODULE_LICENSE("GPL");
  862. EXPORT_SYMBOL(css_bus_type);
  863. EXPORT_SYMBOL_GPL(css_characteristics_avail);