device.c 38 KB

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