device.c 37 KB

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