device.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  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. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/device.h>
  19. #include <linux/workqueue.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/param.h> /* HZ */
  23. #include "cio.h"
  24. #include "cio_debug.h"
  25. #include "css.h"
  26. #include "device.h"
  27. #include "ioasm.h"
  28. /******************* bus type handling ***********************/
  29. /* The Linux driver model distinguishes between a bus type and
  30. * the bus itself. Of course we only have one channel
  31. * subsystem driver and one channel system per machine, but
  32. * we still use the abstraction. T.R. says it's a good idea. */
  33. static int
  34. ccw_bus_match (struct device * dev, struct device_driver * drv)
  35. {
  36. struct ccw_device *cdev = to_ccwdev(dev);
  37. struct ccw_driver *cdrv = to_ccwdrv(drv);
  38. const struct ccw_device_id *ids = cdrv->ids, *found;
  39. if (!ids)
  40. return 0;
  41. found = ccw_device_id_match(ids, &cdev->id);
  42. if (!found)
  43. return 0;
  44. cdev->id.driver_info = found->driver_info;
  45. return 1;
  46. }
  47. /* Store modalias string delimited by prefix/suffix string into buffer with
  48. * specified size. Return length of resulting string (excluding trailing '\0')
  49. * even if string doesn't fit buffer (snprintf semantics). */
  50. static int snprint_alias(char *buf, size_t size,
  51. struct ccw_device_id *id, const char *suffix)
  52. {
  53. int len;
  54. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  55. if (len > size)
  56. return len;
  57. buf += len;
  58. size -= len;
  59. if (id->dev_type != 0)
  60. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  61. id->dev_model, suffix);
  62. else
  63. len += snprintf(buf, size, "dtdm%s", suffix);
  64. return len;
  65. }
  66. /* Set up environment variables for ccw device uevent. Return 0 on success,
  67. * non-zero otherwise. */
  68. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  69. {
  70. struct ccw_device *cdev = to_ccwdev(dev);
  71. struct ccw_device_id *id = &(cdev->id);
  72. int ret;
  73. char modalias_buf[30];
  74. /* CU_TYPE= */
  75. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  76. if (ret)
  77. return ret;
  78. /* CU_MODEL= */
  79. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  80. if (ret)
  81. return ret;
  82. /* The next two can be zero, that's ok for us */
  83. /* DEV_TYPE= */
  84. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  85. if (ret)
  86. return ret;
  87. /* DEV_MODEL= */
  88. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  89. if (ret)
  90. return ret;
  91. /* MODALIAS= */
  92. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  93. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  94. return ret;
  95. }
  96. struct bus_type ccw_bus_type;
  97. static int io_subchannel_probe (struct subchannel *);
  98. static int io_subchannel_remove (struct subchannel *);
  99. static int io_subchannel_notify(struct device *, int);
  100. static void io_subchannel_verify(struct device *);
  101. static void io_subchannel_ioterm(struct device *);
  102. static void io_subchannel_shutdown(struct subchannel *);
  103. static struct css_driver io_subchannel_driver = {
  104. .subchannel_type = SUBCHANNEL_TYPE_IO,
  105. .drv = {
  106. .name = "io_subchannel",
  107. .bus = &css_bus_type,
  108. },
  109. .irq = io_subchannel_irq,
  110. .notify = io_subchannel_notify,
  111. .verify = io_subchannel_verify,
  112. .termination = io_subchannel_ioterm,
  113. .probe = io_subchannel_probe,
  114. .remove = io_subchannel_remove,
  115. .shutdown = io_subchannel_shutdown,
  116. };
  117. struct workqueue_struct *ccw_device_work;
  118. struct workqueue_struct *ccw_device_notify_work;
  119. wait_queue_head_t ccw_device_init_wq;
  120. atomic_t ccw_device_init_count;
  121. static int __init
  122. init_ccw_bus_type (void)
  123. {
  124. int ret;
  125. init_waitqueue_head(&ccw_device_init_wq);
  126. atomic_set(&ccw_device_init_count, 0);
  127. ccw_device_work = create_singlethread_workqueue("cio");
  128. if (!ccw_device_work)
  129. return -ENOMEM; /* FIXME: better errno ? */
  130. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  131. if (!ccw_device_notify_work) {
  132. ret = -ENOMEM; /* FIXME: better errno ? */
  133. goto out_err;
  134. }
  135. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  136. if (!slow_path_wq) {
  137. ret = -ENOMEM; /* FIXME: better errno ? */
  138. goto out_err;
  139. }
  140. if ((ret = bus_register (&ccw_bus_type)))
  141. goto out_err;
  142. if ((ret = driver_register(&io_subchannel_driver.drv)))
  143. goto out_err;
  144. wait_event(ccw_device_init_wq,
  145. atomic_read(&ccw_device_init_count) == 0);
  146. flush_workqueue(ccw_device_work);
  147. return 0;
  148. out_err:
  149. if (ccw_device_work)
  150. destroy_workqueue(ccw_device_work);
  151. if (ccw_device_notify_work)
  152. destroy_workqueue(ccw_device_notify_work);
  153. if (slow_path_wq)
  154. destroy_workqueue(slow_path_wq);
  155. return ret;
  156. }
  157. static void __exit
  158. cleanup_ccw_bus_type (void)
  159. {
  160. driver_unregister(&io_subchannel_driver.drv);
  161. bus_unregister(&ccw_bus_type);
  162. destroy_workqueue(ccw_device_notify_work);
  163. destroy_workqueue(ccw_device_work);
  164. }
  165. subsys_initcall(init_ccw_bus_type);
  166. module_exit(cleanup_ccw_bus_type);
  167. /************************ device handling **************************/
  168. /*
  169. * A ccw_device has some interfaces in sysfs in addition to the
  170. * standard ones.
  171. * The following entries are designed to export the information which
  172. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  173. * are obvious, so they don't have an entry :)
  174. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  175. */
  176. static ssize_t
  177. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  178. {
  179. struct subchannel *sch = to_subchannel(dev);
  180. struct chsc_ssd_info *ssd = &sch->ssd_info;
  181. ssize_t ret = 0;
  182. int chp;
  183. int mask;
  184. for (chp = 0; chp < 8; chp++) {
  185. mask = 0x80 >> chp;
  186. if (ssd->path_mask & mask)
  187. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  188. else
  189. ret += sprintf(buf + ret, "00 ");
  190. }
  191. ret += sprintf (buf+ret, "\n");
  192. return min((ssize_t)PAGE_SIZE, ret);
  193. }
  194. static ssize_t
  195. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  196. {
  197. struct subchannel *sch = to_subchannel(dev);
  198. struct pmcw *pmcw = &sch->schib.pmcw;
  199. return sprintf (buf, "%02x %02x %02x\n",
  200. pmcw->pim, pmcw->pam, pmcw->pom);
  201. }
  202. static ssize_t
  203. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  204. {
  205. struct ccw_device *cdev = to_ccwdev(dev);
  206. struct ccw_device_id *id = &(cdev->id);
  207. if (id->dev_type != 0)
  208. return sprintf(buf, "%04x/%02x\n",
  209. id->dev_type, id->dev_model);
  210. else
  211. return sprintf(buf, "n/a\n");
  212. }
  213. static ssize_t
  214. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  215. {
  216. struct ccw_device *cdev = to_ccwdev(dev);
  217. struct ccw_device_id *id = &(cdev->id);
  218. return sprintf(buf, "%04x/%02x\n",
  219. id->cu_type, id->cu_model);
  220. }
  221. static ssize_t
  222. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  223. {
  224. struct ccw_device *cdev = to_ccwdev(dev);
  225. struct ccw_device_id *id = &(cdev->id);
  226. int len;
  227. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  228. return len > PAGE_SIZE ? PAGE_SIZE : len;
  229. }
  230. static ssize_t
  231. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  232. {
  233. struct ccw_device *cdev = to_ccwdev(dev);
  234. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  235. }
  236. int ccw_device_is_orphan(struct ccw_device *cdev)
  237. {
  238. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  239. }
  240. static void ccw_device_unregister(struct ccw_device *cdev)
  241. {
  242. if (test_and_clear_bit(1, &cdev->private->registered))
  243. device_del(&cdev->dev);
  244. }
  245. static void ccw_device_remove_orphan_cb(struct device *dev)
  246. {
  247. struct ccw_device *cdev = to_ccwdev(dev);
  248. ccw_device_unregister(cdev);
  249. put_device(&cdev->dev);
  250. }
  251. static void ccw_device_remove_sch_cb(struct device *dev)
  252. {
  253. struct subchannel *sch;
  254. sch = to_subchannel(dev);
  255. css_sch_device_unregister(sch);
  256. /* Reset intparm to zeroes. */
  257. sch->schib.pmcw.intparm = 0;
  258. cio_modify(sch);
  259. put_device(&sch->dev);
  260. }
  261. static void
  262. ccw_device_remove_disconnected(struct ccw_device *cdev)
  263. {
  264. unsigned long flags;
  265. int rc;
  266. /*
  267. * Forced offline in disconnected state means
  268. * 'throw away device'.
  269. */
  270. if (ccw_device_is_orphan(cdev)) {
  271. /*
  272. * Deregister ccw device.
  273. * Unfortunately, we cannot do this directly from the
  274. * attribute method.
  275. */
  276. spin_lock_irqsave(cdev->ccwlock, flags);
  277. cdev->private->state = DEV_STATE_NOT_OPER;
  278. spin_unlock_irqrestore(cdev->ccwlock, flags);
  279. rc = device_schedule_callback(&cdev->dev,
  280. ccw_device_remove_orphan_cb);
  281. if (rc)
  282. CIO_MSG_EVENT(2, "Couldn't unregister orphan "
  283. "0.%x.%04x\n",
  284. cdev->private->dev_id.ssid,
  285. cdev->private->dev_id.devno);
  286. return;
  287. }
  288. /* Deregister subchannel, which will kill the ccw device. */
  289. rc = device_schedule_callback(cdev->dev.parent,
  290. ccw_device_remove_sch_cb);
  291. if (rc)
  292. CIO_MSG_EVENT(2, "Couldn't unregister disconnected device "
  293. "0.%x.%04x\n",
  294. cdev->private->dev_id.ssid,
  295. cdev->private->dev_id.devno);
  296. }
  297. int
  298. ccw_device_set_offline(struct ccw_device *cdev)
  299. {
  300. int ret;
  301. if (!cdev)
  302. return -ENODEV;
  303. if (!cdev->online || !cdev->drv)
  304. return -EINVAL;
  305. if (cdev->drv->set_offline) {
  306. ret = cdev->drv->set_offline(cdev);
  307. if (ret != 0)
  308. return ret;
  309. }
  310. cdev->online = 0;
  311. spin_lock_irq(cdev->ccwlock);
  312. ret = ccw_device_offline(cdev);
  313. if (ret == -ENODEV) {
  314. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  315. cdev->private->state = DEV_STATE_OFFLINE;
  316. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  317. }
  318. spin_unlock_irq(cdev->ccwlock);
  319. return ret;
  320. }
  321. spin_unlock_irq(cdev->ccwlock);
  322. if (ret == 0)
  323. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  324. else {
  325. CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
  326. "device 0.%x.%04x\n",
  327. ret, cdev->private->dev_id.ssid,
  328. cdev->private->dev_id.devno);
  329. cdev->online = 1;
  330. }
  331. return ret;
  332. }
  333. int
  334. ccw_device_set_online(struct ccw_device *cdev)
  335. {
  336. int ret;
  337. if (!cdev)
  338. return -ENODEV;
  339. if (cdev->online || !cdev->drv)
  340. return -EINVAL;
  341. spin_lock_irq(cdev->ccwlock);
  342. ret = ccw_device_online(cdev);
  343. spin_unlock_irq(cdev->ccwlock);
  344. if (ret == 0)
  345. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  346. else {
  347. CIO_MSG_EVENT(2, "ccw_device_online returned %d, "
  348. "device 0.%x.%04x\n",
  349. ret, cdev->private->dev_id.ssid,
  350. cdev->private->dev_id.devno);
  351. return ret;
  352. }
  353. if (cdev->private->state != DEV_STATE_ONLINE)
  354. return -ENODEV;
  355. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  356. cdev->online = 1;
  357. return 0;
  358. }
  359. spin_lock_irq(cdev->ccwlock);
  360. ret = ccw_device_offline(cdev);
  361. spin_unlock_irq(cdev->ccwlock);
  362. if (ret == 0)
  363. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  364. else
  365. CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
  366. "device 0.%x.%04x\n",
  367. ret, cdev->private->dev_id.ssid,
  368. cdev->private->dev_id.devno);
  369. return (ret == 0) ? -ENODEV : ret;
  370. }
  371. static void online_store_handle_offline(struct ccw_device *cdev)
  372. {
  373. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  374. ccw_device_remove_disconnected(cdev);
  375. else if (cdev->drv && cdev->drv->set_offline)
  376. ccw_device_set_offline(cdev);
  377. }
  378. static int online_store_recog_and_online(struct ccw_device *cdev)
  379. {
  380. int ret;
  381. /* Do device recognition, if needed. */
  382. if (cdev->id.cu_type == 0) {
  383. ret = ccw_device_recognition(cdev);
  384. if (ret) {
  385. CIO_MSG_EVENT(0, "Couldn't start recognition "
  386. "for device 0.%x.%04x (ret=%d)\n",
  387. cdev->private->dev_id.ssid,
  388. cdev->private->dev_id.devno, ret);
  389. return ret;
  390. }
  391. wait_event(cdev->private->wait_q,
  392. cdev->private->flags.recog_done);
  393. }
  394. if (cdev->drv && cdev->drv->set_online)
  395. ccw_device_set_online(cdev);
  396. return 0;
  397. }
  398. static void online_store_handle_online(struct ccw_device *cdev, int force)
  399. {
  400. int ret;
  401. ret = online_store_recog_and_online(cdev);
  402. if (ret)
  403. return;
  404. if (force && cdev->private->state == DEV_STATE_BOXED) {
  405. ret = ccw_device_stlck(cdev);
  406. if (ret) {
  407. dev_warn(&cdev->dev,
  408. "ccw_device_stlck returned %d!\n", ret);
  409. return;
  410. }
  411. if (cdev->id.cu_type == 0)
  412. cdev->private->state = DEV_STATE_NOT_OPER;
  413. online_store_recog_and_online(cdev);
  414. }
  415. }
  416. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  417. const char *buf, size_t count)
  418. {
  419. struct ccw_device *cdev = to_ccwdev(dev);
  420. int i, force;
  421. char *tmp;
  422. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  423. return -EAGAIN;
  424. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  425. atomic_set(&cdev->private->onoff, 0);
  426. return -EINVAL;
  427. }
  428. if (!strncmp(buf, "force\n", count)) {
  429. force = 1;
  430. i = 1;
  431. } else {
  432. force = 0;
  433. i = simple_strtoul(buf, &tmp, 16);
  434. }
  435. switch (i) {
  436. case 0:
  437. online_store_handle_offline(cdev);
  438. break;
  439. case 1:
  440. online_store_handle_online(cdev, force);
  441. break;
  442. default:
  443. count = -EINVAL;
  444. }
  445. if (cdev->drv)
  446. module_put(cdev->drv->owner);
  447. atomic_set(&cdev->private->onoff, 0);
  448. return count;
  449. }
  450. static ssize_t
  451. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  452. {
  453. struct ccw_device *cdev = to_ccwdev(dev);
  454. struct subchannel *sch;
  455. if (ccw_device_is_orphan(cdev))
  456. return sprintf(buf, "no device\n");
  457. switch (cdev->private->state) {
  458. case DEV_STATE_BOXED:
  459. return sprintf(buf, "boxed\n");
  460. case DEV_STATE_DISCONNECTED:
  461. case DEV_STATE_DISCONNECTED_SENSE_ID:
  462. case DEV_STATE_NOT_OPER:
  463. sch = to_subchannel(dev->parent);
  464. if (!sch->lpm)
  465. return sprintf(buf, "no path\n");
  466. else
  467. return sprintf(buf, "no device\n");
  468. default:
  469. /* All other states considered fine. */
  470. return sprintf(buf, "good\n");
  471. }
  472. }
  473. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  474. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  475. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  476. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  477. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  478. static DEVICE_ATTR(online, 0644, online_show, online_store);
  479. extern struct device_attribute dev_attr_cmb_enable;
  480. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  481. static struct attribute * subch_attrs[] = {
  482. &dev_attr_chpids.attr,
  483. &dev_attr_pimpampom.attr,
  484. NULL,
  485. };
  486. static struct attribute_group subch_attr_group = {
  487. .attrs = subch_attrs,
  488. };
  489. struct attribute_group *subch_attr_groups[] = {
  490. &subch_attr_group,
  491. NULL,
  492. };
  493. static struct attribute * ccwdev_attrs[] = {
  494. &dev_attr_devtype.attr,
  495. &dev_attr_cutype.attr,
  496. &dev_attr_modalias.attr,
  497. &dev_attr_online.attr,
  498. &dev_attr_cmb_enable.attr,
  499. &dev_attr_availability.attr,
  500. NULL,
  501. };
  502. static struct attribute_group ccwdev_attr_group = {
  503. .attrs = ccwdev_attrs,
  504. };
  505. static struct attribute_group *ccwdev_attr_groups[] = {
  506. &ccwdev_attr_group,
  507. NULL,
  508. };
  509. /* this is a simple abstraction for device_register that sets the
  510. * correct bus type and adds the bus specific files */
  511. static int ccw_device_register(struct ccw_device *cdev)
  512. {
  513. struct device *dev = &cdev->dev;
  514. int ret;
  515. dev->bus = &ccw_bus_type;
  516. if ((ret = device_add(dev)))
  517. return ret;
  518. set_bit(1, &cdev->private->registered);
  519. return ret;
  520. }
  521. struct match_data {
  522. struct ccw_dev_id dev_id;
  523. struct ccw_device * sibling;
  524. };
  525. static int
  526. match_devno(struct device * dev, void * data)
  527. {
  528. struct match_data * d = data;
  529. struct ccw_device * cdev;
  530. cdev = to_ccwdev(dev);
  531. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  532. !ccw_device_is_orphan(cdev) &&
  533. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  534. (cdev != d->sibling))
  535. return 1;
  536. return 0;
  537. }
  538. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  539. struct ccw_device *sibling)
  540. {
  541. struct device *dev;
  542. struct match_data data;
  543. data.dev_id = *dev_id;
  544. data.sibling = sibling;
  545. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  546. return dev ? to_ccwdev(dev) : NULL;
  547. }
  548. static int match_orphan(struct device *dev, void *data)
  549. {
  550. struct ccw_dev_id *dev_id;
  551. struct ccw_device *cdev;
  552. dev_id = data;
  553. cdev = to_ccwdev(dev);
  554. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  555. }
  556. static struct ccw_device *
  557. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  558. struct ccw_dev_id *dev_id)
  559. {
  560. struct device *dev;
  561. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  562. match_orphan);
  563. return dev ? to_ccwdev(dev) : NULL;
  564. }
  565. static void
  566. ccw_device_add_changed(struct work_struct *work)
  567. {
  568. struct ccw_device_private *priv;
  569. struct ccw_device *cdev;
  570. priv = container_of(work, struct ccw_device_private, kick_work);
  571. cdev = priv->cdev;
  572. if (device_add(&cdev->dev)) {
  573. put_device(&cdev->dev);
  574. return;
  575. }
  576. set_bit(1, &cdev->private->registered);
  577. }
  578. void ccw_device_do_unreg_rereg(struct work_struct *work)
  579. {
  580. struct ccw_device_private *priv;
  581. struct ccw_device *cdev;
  582. struct subchannel *sch;
  583. priv = container_of(work, struct ccw_device_private, kick_work);
  584. cdev = priv->cdev;
  585. sch = to_subchannel(cdev->dev.parent);
  586. ccw_device_unregister(cdev);
  587. PREPARE_WORK(&cdev->private->kick_work,
  588. ccw_device_add_changed);
  589. queue_work(ccw_device_work, &cdev->private->kick_work);
  590. }
  591. static void
  592. ccw_device_release(struct device *dev)
  593. {
  594. struct ccw_device *cdev;
  595. cdev = to_ccwdev(dev);
  596. kfree(cdev->private);
  597. kfree(cdev);
  598. }
  599. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  600. {
  601. struct ccw_device *cdev;
  602. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  603. if (cdev) {
  604. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  605. GFP_KERNEL | GFP_DMA);
  606. if (cdev->private)
  607. return cdev;
  608. }
  609. kfree(cdev);
  610. return ERR_PTR(-ENOMEM);
  611. }
  612. static int io_subchannel_initialize_dev(struct subchannel *sch,
  613. struct ccw_device *cdev)
  614. {
  615. cdev->private->cdev = cdev;
  616. atomic_set(&cdev->private->onoff, 0);
  617. cdev->dev.parent = &sch->dev;
  618. cdev->dev.release = ccw_device_release;
  619. INIT_LIST_HEAD(&cdev->private->kick_work.entry);
  620. cdev->dev.groups = ccwdev_attr_groups;
  621. /* Do first half of device_register. */
  622. device_initialize(&cdev->dev);
  623. if (!get_device(&sch->dev)) {
  624. if (cdev->dev.release)
  625. cdev->dev.release(&cdev->dev);
  626. return -ENODEV;
  627. }
  628. return 0;
  629. }
  630. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  631. {
  632. struct ccw_device *cdev;
  633. int ret;
  634. cdev = io_subchannel_allocate_dev(sch);
  635. if (!IS_ERR(cdev)) {
  636. ret = io_subchannel_initialize_dev(sch, cdev);
  637. if (ret) {
  638. kfree(cdev);
  639. cdev = ERR_PTR(ret);
  640. }
  641. }
  642. return cdev;
  643. }
  644. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  645. static void sch_attach_device(struct subchannel *sch,
  646. struct ccw_device *cdev)
  647. {
  648. css_update_ssd_info(sch);
  649. spin_lock_irq(sch->lock);
  650. sch->dev.driver_data = cdev;
  651. cdev->private->schid = sch->schid;
  652. cdev->ccwlock = sch->lock;
  653. device_trigger_reprobe(sch);
  654. spin_unlock_irq(sch->lock);
  655. }
  656. static void sch_attach_disconnected_device(struct subchannel *sch,
  657. struct ccw_device *cdev)
  658. {
  659. struct subchannel *other_sch;
  660. int ret;
  661. other_sch = to_subchannel(get_device(cdev->dev.parent));
  662. ret = device_move(&cdev->dev, &sch->dev);
  663. if (ret) {
  664. CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
  665. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  666. cdev->private->dev_id.devno, ret);
  667. put_device(&other_sch->dev);
  668. return;
  669. }
  670. other_sch->dev.driver_data = NULL;
  671. /* No need to keep a subchannel without ccw device around. */
  672. css_sch_device_unregister(other_sch);
  673. put_device(&other_sch->dev);
  674. sch_attach_device(sch, cdev);
  675. }
  676. static void sch_attach_orphaned_device(struct subchannel *sch,
  677. struct ccw_device *cdev)
  678. {
  679. int ret;
  680. /* Try to move the ccw device to its new subchannel. */
  681. ret = device_move(&cdev->dev, &sch->dev);
  682. if (ret) {
  683. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  684. "failed (ret=%d)!\n",
  685. cdev->private->dev_id.ssid,
  686. cdev->private->dev_id.devno, ret);
  687. return;
  688. }
  689. sch_attach_device(sch, cdev);
  690. }
  691. static void sch_create_and_recog_new_device(struct subchannel *sch)
  692. {
  693. struct ccw_device *cdev;
  694. /* Need to allocate a new ccw device. */
  695. cdev = io_subchannel_create_ccwdev(sch);
  696. if (IS_ERR(cdev)) {
  697. /* OK, we did everything we could... */
  698. css_sch_device_unregister(sch);
  699. return;
  700. }
  701. spin_lock_irq(sch->lock);
  702. sch->dev.driver_data = cdev;
  703. spin_unlock_irq(sch->lock);
  704. /* Start recognition for the new ccw device. */
  705. if (io_subchannel_recog(cdev, sch)) {
  706. spin_lock_irq(sch->lock);
  707. sch->dev.driver_data = NULL;
  708. spin_unlock_irq(sch->lock);
  709. if (cdev->dev.release)
  710. cdev->dev.release(&cdev->dev);
  711. css_sch_device_unregister(sch);
  712. }
  713. }
  714. void ccw_device_move_to_orphanage(struct work_struct *work)
  715. {
  716. struct ccw_device_private *priv;
  717. struct ccw_device *cdev;
  718. struct ccw_device *replacing_cdev;
  719. struct subchannel *sch;
  720. int ret;
  721. struct channel_subsystem *css;
  722. struct ccw_dev_id dev_id;
  723. priv = container_of(work, struct ccw_device_private, kick_work);
  724. cdev = priv->cdev;
  725. sch = to_subchannel(cdev->dev.parent);
  726. css = to_css(sch->dev.parent);
  727. dev_id.devno = sch->schib.pmcw.dev;
  728. dev_id.ssid = sch->schid.ssid;
  729. /*
  730. * Move the orphaned ccw device to the orphanage so the replacing
  731. * ccw device can take its place on the subchannel.
  732. */
  733. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  734. if (ret) {
  735. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  736. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  737. cdev->private->dev_id.devno, ret);
  738. return;
  739. }
  740. cdev->ccwlock = css->pseudo_subchannel->lock;
  741. /*
  742. * Search for the replacing ccw device
  743. * - among the disconnected devices
  744. * - in the orphanage
  745. */
  746. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  747. if (replacing_cdev) {
  748. sch_attach_disconnected_device(sch, replacing_cdev);
  749. return;
  750. }
  751. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  752. if (replacing_cdev) {
  753. sch_attach_orphaned_device(sch, replacing_cdev);
  754. return;
  755. }
  756. sch_create_and_recog_new_device(sch);
  757. }
  758. /*
  759. * Register recognized device.
  760. */
  761. static void
  762. io_subchannel_register(struct work_struct *work)
  763. {
  764. struct ccw_device_private *priv;
  765. struct ccw_device *cdev;
  766. struct subchannel *sch;
  767. int ret;
  768. unsigned long flags;
  769. priv = container_of(work, struct ccw_device_private, kick_work);
  770. cdev = priv->cdev;
  771. sch = to_subchannel(cdev->dev.parent);
  772. css_update_ssd_info(sch);
  773. /*
  774. * io_subchannel_register() will also be called after device
  775. * recognition has been done for a boxed device (which will already
  776. * be registered). We need to reprobe since we may now have sense id
  777. * information.
  778. */
  779. if (klist_node_attached(&cdev->dev.knode_parent)) {
  780. if (!cdev->drv) {
  781. ret = device_reprobe(&cdev->dev);
  782. if (ret)
  783. /* We can't do much here. */
  784. CIO_MSG_EVENT(2, "device_reprobe() returned"
  785. " %d for 0.%x.%04x\n", ret,
  786. cdev->private->dev_id.ssid,
  787. cdev->private->dev_id.devno);
  788. }
  789. goto out;
  790. }
  791. /*
  792. * Now we know this subchannel will stay, we can throw
  793. * our delayed uevent.
  794. */
  795. sch->dev.uevent_suppress = 0;
  796. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  797. /* make it known to the system */
  798. ret = ccw_device_register(cdev);
  799. if (ret) {
  800. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  801. cdev->private->dev_id.ssid,
  802. cdev->private->dev_id.devno, ret);
  803. put_device(&cdev->dev);
  804. spin_lock_irqsave(sch->lock, flags);
  805. sch->dev.driver_data = NULL;
  806. spin_unlock_irqrestore(sch->lock, flags);
  807. kfree (cdev->private);
  808. kfree (cdev);
  809. put_device(&sch->dev);
  810. if (atomic_dec_and_test(&ccw_device_init_count))
  811. wake_up(&ccw_device_init_wq);
  812. return;
  813. }
  814. put_device(&cdev->dev);
  815. out:
  816. cdev->private->flags.recog_done = 1;
  817. put_device(&sch->dev);
  818. wake_up(&cdev->private->wait_q);
  819. if (atomic_dec_and_test(&ccw_device_init_count))
  820. wake_up(&ccw_device_init_wq);
  821. }
  822. void
  823. ccw_device_call_sch_unregister(struct work_struct *work)
  824. {
  825. struct ccw_device_private *priv;
  826. struct ccw_device *cdev;
  827. struct subchannel *sch;
  828. priv = container_of(work, struct ccw_device_private, kick_work);
  829. cdev = priv->cdev;
  830. sch = to_subchannel(cdev->dev.parent);
  831. css_sch_device_unregister(sch);
  832. /* Reset intparm to zeroes. */
  833. sch->schib.pmcw.intparm = 0;
  834. cio_modify(sch);
  835. put_device(&cdev->dev);
  836. put_device(&sch->dev);
  837. }
  838. /*
  839. * subchannel recognition done. Called from the state machine.
  840. */
  841. void
  842. io_subchannel_recog_done(struct ccw_device *cdev)
  843. {
  844. struct subchannel *sch;
  845. if (css_init_done == 0) {
  846. cdev->private->flags.recog_done = 1;
  847. return;
  848. }
  849. switch (cdev->private->state) {
  850. case DEV_STATE_NOT_OPER:
  851. cdev->private->flags.recog_done = 1;
  852. /* Remove device found not operational. */
  853. if (!get_device(&cdev->dev))
  854. break;
  855. sch = to_subchannel(cdev->dev.parent);
  856. PREPARE_WORK(&cdev->private->kick_work,
  857. ccw_device_call_sch_unregister);
  858. queue_work(slow_path_wq, &cdev->private->kick_work);
  859. if (atomic_dec_and_test(&ccw_device_init_count))
  860. wake_up(&ccw_device_init_wq);
  861. break;
  862. case DEV_STATE_BOXED:
  863. /* Device did not respond in time. */
  864. case DEV_STATE_OFFLINE:
  865. /*
  866. * We can't register the device in interrupt context so
  867. * we schedule a work item.
  868. */
  869. if (!get_device(&cdev->dev))
  870. break;
  871. PREPARE_WORK(&cdev->private->kick_work,
  872. io_subchannel_register);
  873. queue_work(slow_path_wq, &cdev->private->kick_work);
  874. break;
  875. }
  876. }
  877. static int
  878. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  879. {
  880. int rc;
  881. struct ccw_device_private *priv;
  882. sch->dev.driver_data = cdev;
  883. sch->driver = &io_subchannel_driver;
  884. cdev->ccwlock = sch->lock;
  885. /* Init private data. */
  886. priv = cdev->private;
  887. priv->dev_id.devno = sch->schib.pmcw.dev;
  888. priv->dev_id.ssid = sch->schid.ssid;
  889. priv->schid = sch->schid;
  890. priv->state = DEV_STATE_NOT_OPER;
  891. INIT_LIST_HEAD(&priv->cmb_list);
  892. init_waitqueue_head(&priv->wait_q);
  893. init_timer(&priv->timer);
  894. /* Set an initial name for the device. */
  895. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  896. sch->schid.ssid, sch->schib.pmcw.dev);
  897. /* Increase counter of devices currently in recognition. */
  898. atomic_inc(&ccw_device_init_count);
  899. /* Start async. device sensing. */
  900. spin_lock_irq(sch->lock);
  901. rc = ccw_device_recognition(cdev);
  902. spin_unlock_irq(sch->lock);
  903. if (rc) {
  904. if (atomic_dec_and_test(&ccw_device_init_count))
  905. wake_up(&ccw_device_init_wq);
  906. }
  907. return rc;
  908. }
  909. static void ccw_device_move_to_sch(struct work_struct *work)
  910. {
  911. struct ccw_device_private *priv;
  912. int rc;
  913. struct subchannel *sch;
  914. struct ccw_device *cdev;
  915. struct subchannel *former_parent;
  916. priv = container_of(work, struct ccw_device_private, kick_work);
  917. sch = priv->sch;
  918. cdev = priv->cdev;
  919. former_parent = ccw_device_is_orphan(cdev) ?
  920. NULL : to_subchannel(get_device(cdev->dev.parent));
  921. mutex_lock(&sch->reg_mutex);
  922. /* Try to move the ccw device to its new subchannel. */
  923. rc = device_move(&cdev->dev, &sch->dev);
  924. mutex_unlock(&sch->reg_mutex);
  925. if (rc) {
  926. CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
  927. "0.%x.%04x failed (ret=%d)!\n",
  928. cdev->private->dev_id.ssid,
  929. cdev->private->dev_id.devno, sch->schid.ssid,
  930. sch->schid.sch_no, rc);
  931. css_sch_device_unregister(sch);
  932. goto out;
  933. }
  934. if (former_parent) {
  935. spin_lock_irq(former_parent->lock);
  936. former_parent->dev.driver_data = NULL;
  937. spin_unlock_irq(former_parent->lock);
  938. css_sch_device_unregister(former_parent);
  939. /* Reset intparm to zeroes. */
  940. former_parent->schib.pmcw.intparm = 0;
  941. cio_modify(former_parent);
  942. }
  943. sch_attach_device(sch, cdev);
  944. out:
  945. if (former_parent)
  946. put_device(&former_parent->dev);
  947. put_device(&cdev->dev);
  948. }
  949. static int
  950. io_subchannel_probe (struct subchannel *sch)
  951. {
  952. struct ccw_device *cdev;
  953. int rc;
  954. unsigned long flags;
  955. struct ccw_dev_id dev_id;
  956. if (sch->dev.driver_data) {
  957. /*
  958. * This subchannel already has an associated ccw_device.
  959. * Register it and exit. This happens for all early
  960. * device, e.g. the console.
  961. */
  962. cdev = sch->dev.driver_data;
  963. device_initialize(&cdev->dev);
  964. ccw_device_register(cdev);
  965. /*
  966. * Check if the device is already online. If it is
  967. * the reference count needs to be corrected
  968. * (see ccw_device_online and css_init_done for the
  969. * ugly details).
  970. */
  971. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  972. cdev->private->state != DEV_STATE_OFFLINE &&
  973. cdev->private->state != DEV_STATE_BOXED)
  974. get_device(&cdev->dev);
  975. return 0;
  976. }
  977. /*
  978. * First check if a fitting device may be found amongst the
  979. * disconnected devices or in the orphanage.
  980. */
  981. dev_id.devno = sch->schib.pmcw.dev;
  982. dev_id.ssid = sch->schid.ssid;
  983. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  984. if (!cdev)
  985. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  986. &dev_id);
  987. if (cdev) {
  988. /*
  989. * Schedule moving the device until when we have a registered
  990. * subchannel to move to and succeed the probe. We can
  991. * unregister later again, when the probe is through.
  992. */
  993. cdev->private->sch = sch;
  994. PREPARE_WORK(&cdev->private->kick_work,
  995. ccw_device_move_to_sch);
  996. queue_work(slow_path_wq, &cdev->private->kick_work);
  997. return 0;
  998. }
  999. cdev = io_subchannel_create_ccwdev(sch);
  1000. if (IS_ERR(cdev))
  1001. return PTR_ERR(cdev);
  1002. rc = io_subchannel_recog(cdev, sch);
  1003. if (rc) {
  1004. spin_lock_irqsave(sch->lock, flags);
  1005. sch->dev.driver_data = NULL;
  1006. spin_unlock_irqrestore(sch->lock, flags);
  1007. if (cdev->dev.release)
  1008. cdev->dev.release(&cdev->dev);
  1009. }
  1010. return rc;
  1011. }
  1012. static int
  1013. io_subchannel_remove (struct subchannel *sch)
  1014. {
  1015. struct ccw_device *cdev;
  1016. unsigned long flags;
  1017. if (!sch->dev.driver_data)
  1018. return 0;
  1019. cdev = sch->dev.driver_data;
  1020. /* Set ccw device to not operational and drop reference. */
  1021. spin_lock_irqsave(cdev->ccwlock, flags);
  1022. sch->dev.driver_data = NULL;
  1023. cdev->private->state = DEV_STATE_NOT_OPER;
  1024. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1025. ccw_device_unregister(cdev);
  1026. put_device(&cdev->dev);
  1027. return 0;
  1028. }
  1029. static int
  1030. io_subchannel_notify(struct device *dev, int event)
  1031. {
  1032. struct ccw_device *cdev;
  1033. cdev = dev->driver_data;
  1034. if (!cdev)
  1035. return 0;
  1036. if (!cdev->drv)
  1037. return 0;
  1038. if (!cdev->online)
  1039. return 0;
  1040. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  1041. }
  1042. static void
  1043. io_subchannel_verify(struct device *dev)
  1044. {
  1045. struct ccw_device *cdev;
  1046. cdev = dev->driver_data;
  1047. if (cdev)
  1048. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1049. }
  1050. static void
  1051. io_subchannel_ioterm(struct device *dev)
  1052. {
  1053. struct ccw_device *cdev;
  1054. cdev = dev->driver_data;
  1055. if (!cdev)
  1056. return;
  1057. /* Internal I/O will be retried by the interrupt handler. */
  1058. if (cdev->private->flags.intretry)
  1059. return;
  1060. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1061. if (cdev->handler)
  1062. cdev->handler(cdev, cdev->private->intparm,
  1063. ERR_PTR(-EIO));
  1064. }
  1065. static void
  1066. io_subchannel_shutdown(struct subchannel *sch)
  1067. {
  1068. struct ccw_device *cdev;
  1069. int ret;
  1070. cdev = sch->dev.driver_data;
  1071. if (cio_is_console(sch->schid))
  1072. return;
  1073. if (!sch->schib.pmcw.ena)
  1074. /* Nothing to do. */
  1075. return;
  1076. ret = cio_disable_subchannel(sch);
  1077. if (ret != -EBUSY)
  1078. /* Subchannel is disabled, we're done. */
  1079. return;
  1080. cdev->private->state = DEV_STATE_QUIESCE;
  1081. if (cdev->handler)
  1082. cdev->handler(cdev, cdev->private->intparm,
  1083. ERR_PTR(-EIO));
  1084. ret = ccw_device_cancel_halt_clear(cdev);
  1085. if (ret == -EBUSY) {
  1086. ccw_device_set_timeout(cdev, HZ/10);
  1087. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1088. }
  1089. cio_disable_subchannel(sch);
  1090. }
  1091. #ifdef CONFIG_CCW_CONSOLE
  1092. static struct ccw_device console_cdev;
  1093. static struct ccw_device_private console_private;
  1094. static int console_cdev_in_use;
  1095. static DEFINE_SPINLOCK(ccw_console_lock);
  1096. spinlock_t * cio_get_console_lock(void)
  1097. {
  1098. return &ccw_console_lock;
  1099. }
  1100. static int
  1101. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  1102. {
  1103. int rc;
  1104. /* Initialize the ccw_device structure. */
  1105. cdev->dev.parent= &sch->dev;
  1106. rc = io_subchannel_recog(cdev, sch);
  1107. if (rc)
  1108. return rc;
  1109. /* Now wait for the async. recognition to come to an end. */
  1110. spin_lock_irq(cdev->ccwlock);
  1111. while (!dev_fsm_final_state(cdev))
  1112. wait_cons_dev();
  1113. rc = -EIO;
  1114. if (cdev->private->state != DEV_STATE_OFFLINE)
  1115. goto out_unlock;
  1116. ccw_device_online(cdev);
  1117. while (!dev_fsm_final_state(cdev))
  1118. wait_cons_dev();
  1119. if (cdev->private->state != DEV_STATE_ONLINE)
  1120. goto out_unlock;
  1121. rc = 0;
  1122. out_unlock:
  1123. spin_unlock_irq(cdev->ccwlock);
  1124. return 0;
  1125. }
  1126. struct ccw_device *
  1127. ccw_device_probe_console(void)
  1128. {
  1129. struct subchannel *sch;
  1130. int ret;
  1131. if (xchg(&console_cdev_in_use, 1) != 0)
  1132. return ERR_PTR(-EBUSY);
  1133. sch = cio_probe_console();
  1134. if (IS_ERR(sch)) {
  1135. console_cdev_in_use = 0;
  1136. return (void *) sch;
  1137. }
  1138. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1139. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1140. console_cdev.private = &console_private;
  1141. console_private.cdev = &console_cdev;
  1142. ret = ccw_device_console_enable(&console_cdev, sch);
  1143. if (ret) {
  1144. cio_release_console();
  1145. console_cdev_in_use = 0;
  1146. return ERR_PTR(ret);
  1147. }
  1148. console_cdev.online = 1;
  1149. return &console_cdev;
  1150. }
  1151. #endif
  1152. /*
  1153. * get ccw_device matching the busid, but only if owned by cdrv
  1154. */
  1155. static int
  1156. __ccwdev_check_busid(struct device *dev, void *id)
  1157. {
  1158. char *bus_id;
  1159. bus_id = id;
  1160. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1161. }
  1162. struct ccw_device *
  1163. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  1164. {
  1165. struct device *dev;
  1166. struct device_driver *drv;
  1167. drv = get_driver(&cdrv->driver);
  1168. if (!drv)
  1169. return NULL;
  1170. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1171. __ccwdev_check_busid);
  1172. put_driver(drv);
  1173. return dev ? to_ccwdev(dev) : NULL;
  1174. }
  1175. /************************** device driver handling ************************/
  1176. /* This is the implementation of the ccw_driver class. The probe, remove
  1177. * and release methods are initially very similar to the device_driver
  1178. * implementations, with the difference that they have ccw_device
  1179. * arguments.
  1180. *
  1181. * A ccw driver also contains the information that is needed for
  1182. * device matching.
  1183. */
  1184. static int
  1185. ccw_device_probe (struct device *dev)
  1186. {
  1187. struct ccw_device *cdev = to_ccwdev(dev);
  1188. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1189. int ret;
  1190. cdev->drv = cdrv; /* to let the driver call _set_online */
  1191. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1192. if (ret) {
  1193. cdev->drv = NULL;
  1194. return ret;
  1195. }
  1196. return 0;
  1197. }
  1198. static int
  1199. ccw_device_remove (struct device *dev)
  1200. {
  1201. struct ccw_device *cdev = to_ccwdev(dev);
  1202. struct ccw_driver *cdrv = cdev->drv;
  1203. int ret;
  1204. if (cdrv->remove)
  1205. cdrv->remove(cdev);
  1206. if (cdev->online) {
  1207. cdev->online = 0;
  1208. spin_lock_irq(cdev->ccwlock);
  1209. ret = ccw_device_offline(cdev);
  1210. spin_unlock_irq(cdev->ccwlock);
  1211. if (ret == 0)
  1212. wait_event(cdev->private->wait_q,
  1213. dev_fsm_final_state(cdev));
  1214. else
  1215. //FIXME: we can't fail!
  1216. CIO_MSG_EVENT(2, "ccw_device_offline returned %d, "
  1217. "device 0.%x.%04x\n",
  1218. ret, cdev->private->dev_id.ssid,
  1219. cdev->private->dev_id.devno);
  1220. }
  1221. ccw_device_set_timeout(cdev, 0);
  1222. cdev->drv = NULL;
  1223. return 0;
  1224. }
  1225. struct bus_type ccw_bus_type = {
  1226. .name = "ccw",
  1227. .match = ccw_bus_match,
  1228. .uevent = ccw_uevent,
  1229. .probe = ccw_device_probe,
  1230. .remove = ccw_device_remove,
  1231. };
  1232. int
  1233. ccw_driver_register (struct ccw_driver *cdriver)
  1234. {
  1235. struct device_driver *drv = &cdriver->driver;
  1236. drv->bus = &ccw_bus_type;
  1237. drv->name = cdriver->name;
  1238. return driver_register(drv);
  1239. }
  1240. void
  1241. ccw_driver_unregister (struct ccw_driver *cdriver)
  1242. {
  1243. driver_unregister(&cdriver->driver);
  1244. }
  1245. /* Helper func for qdio. */
  1246. struct subchannel_id
  1247. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1248. {
  1249. struct subchannel *sch;
  1250. sch = to_subchannel(cdev->dev.parent);
  1251. return sch->schid;
  1252. }
  1253. MODULE_LICENSE("GPL");
  1254. EXPORT_SYMBOL(ccw_device_set_online);
  1255. EXPORT_SYMBOL(ccw_device_set_offline);
  1256. EXPORT_SYMBOL(ccw_driver_register);
  1257. EXPORT_SYMBOL(ccw_driver_unregister);
  1258. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1259. EXPORT_SYMBOL(ccw_bus_type);
  1260. EXPORT_SYMBOL(ccw_device_work);
  1261. EXPORT_SYMBOL(ccw_device_notify_work);
  1262. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);