device.c 35 KB

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