device.c 35 KB

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