device.c 39 KB

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