device.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/list.h>
  17. #include <linux/device.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/timer.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/param.h> /* HZ */
  23. #include <asm/cmb.h>
  24. #include <asm/isc.h>
  25. #include "chp.h"
  26. #include "cio.h"
  27. #include "cio_debug.h"
  28. #include "css.h"
  29. #include "device.h"
  30. #include "ioasm.h"
  31. #include "io_sch.h"
  32. static struct timer_list recovery_timer;
  33. static DEFINE_SPINLOCK(recovery_lock);
  34. static int recovery_phase;
  35. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  36. /******************* bus type handling ***********************/
  37. /* The Linux driver model distinguishes between a bus type and
  38. * the bus itself. Of course we only have one channel
  39. * subsystem driver and one channel system per machine, but
  40. * we still use the abstraction. T.R. says it's a good idea. */
  41. static int
  42. ccw_bus_match (struct device * dev, struct device_driver * drv)
  43. {
  44. struct ccw_device *cdev = to_ccwdev(dev);
  45. struct ccw_driver *cdrv = to_ccwdrv(drv);
  46. const struct ccw_device_id *ids = cdrv->ids, *found;
  47. if (!ids)
  48. return 0;
  49. found = ccw_device_id_match(ids, &cdev->id);
  50. if (!found)
  51. return 0;
  52. cdev->id.driver_info = found->driver_info;
  53. return 1;
  54. }
  55. /* Store modalias string delimited by prefix/suffix string into buffer with
  56. * specified size. Return length of resulting string (excluding trailing '\0')
  57. * even if string doesn't fit buffer (snprintf semantics). */
  58. static int snprint_alias(char *buf, size_t size,
  59. struct ccw_device_id *id, const char *suffix)
  60. {
  61. int len;
  62. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  63. if (len > size)
  64. return len;
  65. buf += len;
  66. size -= len;
  67. if (id->dev_type != 0)
  68. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  69. id->dev_model, suffix);
  70. else
  71. len += snprintf(buf, size, "dtdm%s", suffix);
  72. return len;
  73. }
  74. /* Set up environment variables for ccw device uevent. Return 0 on success,
  75. * non-zero otherwise. */
  76. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  77. {
  78. struct ccw_device *cdev = to_ccwdev(dev);
  79. struct ccw_device_id *id = &(cdev->id);
  80. int ret;
  81. char modalias_buf[30];
  82. /* CU_TYPE= */
  83. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  84. if (ret)
  85. return ret;
  86. /* CU_MODEL= */
  87. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  88. if (ret)
  89. return ret;
  90. /* The next two can be zero, that's ok for us */
  91. /* DEV_TYPE= */
  92. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  93. if (ret)
  94. return ret;
  95. /* DEV_MODEL= */
  96. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  97. if (ret)
  98. return ret;
  99. /* MODALIAS= */
  100. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  101. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  102. return ret;
  103. }
  104. struct bus_type ccw_bus_type;
  105. static void io_subchannel_irq(struct subchannel *);
  106. static int io_subchannel_probe(struct subchannel *);
  107. static int io_subchannel_remove(struct subchannel *);
  108. static void io_subchannel_shutdown(struct subchannel *);
  109. static int io_subchannel_sch_event(struct subchannel *, int);
  110. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  111. int);
  112. static struct css_driver io_subchannel_driver = {
  113. .owner = THIS_MODULE,
  114. .subchannel_type = SUBCHANNEL_TYPE_IO,
  115. .name = "io_subchannel",
  116. .irq = io_subchannel_irq,
  117. .sch_event = io_subchannel_sch_event,
  118. .chp_event = io_subchannel_chp_event,
  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(0, "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(0, "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(0, "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(0, "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(0, "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 force, ret;
  451. unsigned long i;
  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. ret = 0;
  462. } else {
  463. force = 0;
  464. ret = strict_strtoul(buf, 16, &i);
  465. }
  466. if (ret)
  467. goto out;
  468. switch (i) {
  469. case 0:
  470. online_store_handle_offline(cdev);
  471. ret = count;
  472. break;
  473. case 1:
  474. online_store_handle_online(cdev, force);
  475. ret = count;
  476. break;
  477. default:
  478. ret = -EINVAL;
  479. }
  480. out:
  481. if (cdev->drv)
  482. module_put(cdev->drv->owner);
  483. atomic_set(&cdev->private->onoff, 0);
  484. return ret;
  485. }
  486. static ssize_t
  487. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  488. {
  489. struct ccw_device *cdev = to_ccwdev(dev);
  490. struct subchannel *sch;
  491. if (ccw_device_is_orphan(cdev))
  492. return sprintf(buf, "no device\n");
  493. switch (cdev->private->state) {
  494. case DEV_STATE_BOXED:
  495. return sprintf(buf, "boxed\n");
  496. case DEV_STATE_DISCONNECTED:
  497. case DEV_STATE_DISCONNECTED_SENSE_ID:
  498. case DEV_STATE_NOT_OPER:
  499. sch = to_subchannel(dev->parent);
  500. if (!sch->lpm)
  501. return sprintf(buf, "no path\n");
  502. else
  503. return sprintf(buf, "no device\n");
  504. default:
  505. /* All other states considered fine. */
  506. return sprintf(buf, "good\n");
  507. }
  508. }
  509. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  510. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  511. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  512. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  513. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  514. static DEVICE_ATTR(online, 0644, online_show, online_store);
  515. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  516. static struct attribute *io_subchannel_attrs[] = {
  517. &dev_attr_chpids.attr,
  518. &dev_attr_pimpampom.attr,
  519. NULL,
  520. };
  521. static struct attribute_group io_subchannel_attr_group = {
  522. .attrs = io_subchannel_attrs,
  523. };
  524. static struct attribute * ccwdev_attrs[] = {
  525. &dev_attr_devtype.attr,
  526. &dev_attr_cutype.attr,
  527. &dev_attr_modalias.attr,
  528. &dev_attr_online.attr,
  529. &dev_attr_cmb_enable.attr,
  530. &dev_attr_availability.attr,
  531. NULL,
  532. };
  533. static struct attribute_group ccwdev_attr_group = {
  534. .attrs = ccwdev_attrs,
  535. };
  536. static struct attribute_group *ccwdev_attr_groups[] = {
  537. &ccwdev_attr_group,
  538. NULL,
  539. };
  540. /* this is a simple abstraction for device_register that sets the
  541. * correct bus type and adds the bus specific files */
  542. static int ccw_device_register(struct ccw_device *cdev)
  543. {
  544. struct device *dev = &cdev->dev;
  545. int ret;
  546. dev->bus = &ccw_bus_type;
  547. if ((ret = device_add(dev)))
  548. return ret;
  549. set_bit(1, &cdev->private->registered);
  550. return ret;
  551. }
  552. struct match_data {
  553. struct ccw_dev_id dev_id;
  554. struct ccw_device * sibling;
  555. };
  556. static int
  557. match_devno(struct device * dev, void * data)
  558. {
  559. struct match_data * d = data;
  560. struct ccw_device * cdev;
  561. cdev = to_ccwdev(dev);
  562. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  563. !ccw_device_is_orphan(cdev) &&
  564. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  565. (cdev != d->sibling))
  566. return 1;
  567. return 0;
  568. }
  569. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  570. struct ccw_device *sibling)
  571. {
  572. struct device *dev;
  573. struct match_data data;
  574. data.dev_id = *dev_id;
  575. data.sibling = sibling;
  576. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  577. return dev ? to_ccwdev(dev) : NULL;
  578. }
  579. static int match_orphan(struct device *dev, void *data)
  580. {
  581. struct ccw_dev_id *dev_id;
  582. struct ccw_device *cdev;
  583. dev_id = data;
  584. cdev = to_ccwdev(dev);
  585. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  586. }
  587. static struct ccw_device *
  588. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  589. struct ccw_dev_id *dev_id)
  590. {
  591. struct device *dev;
  592. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  593. match_orphan);
  594. return dev ? to_ccwdev(dev) : NULL;
  595. }
  596. static void
  597. ccw_device_add_changed(struct work_struct *work)
  598. {
  599. struct ccw_device_private *priv;
  600. struct ccw_device *cdev;
  601. priv = container_of(work, struct ccw_device_private, kick_work);
  602. cdev = priv->cdev;
  603. if (device_add(&cdev->dev)) {
  604. put_device(&cdev->dev);
  605. return;
  606. }
  607. set_bit(1, &cdev->private->registered);
  608. }
  609. void ccw_device_do_unreg_rereg(struct work_struct *work)
  610. {
  611. struct ccw_device_private *priv;
  612. struct ccw_device *cdev;
  613. struct subchannel *sch;
  614. priv = container_of(work, struct ccw_device_private, kick_work);
  615. cdev = priv->cdev;
  616. sch = to_subchannel(cdev->dev.parent);
  617. ccw_device_unregister(cdev);
  618. PREPARE_WORK(&cdev->private->kick_work,
  619. ccw_device_add_changed);
  620. queue_work(ccw_device_work, &cdev->private->kick_work);
  621. }
  622. static void
  623. ccw_device_release(struct device *dev)
  624. {
  625. struct ccw_device *cdev;
  626. cdev = to_ccwdev(dev);
  627. kfree(cdev->private);
  628. kfree(cdev);
  629. }
  630. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  631. {
  632. struct ccw_device *cdev;
  633. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  634. if (cdev) {
  635. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  636. GFP_KERNEL | GFP_DMA);
  637. if (cdev->private)
  638. return cdev;
  639. }
  640. kfree(cdev);
  641. return ERR_PTR(-ENOMEM);
  642. }
  643. static int io_subchannel_initialize_dev(struct subchannel *sch,
  644. struct ccw_device *cdev)
  645. {
  646. cdev->private->cdev = cdev;
  647. atomic_set(&cdev->private->onoff, 0);
  648. cdev->dev.parent = &sch->dev;
  649. cdev->dev.release = ccw_device_release;
  650. INIT_WORK(&cdev->private->kick_work, NULL);
  651. cdev->dev.groups = ccwdev_attr_groups;
  652. /* Do first half of device_register. */
  653. device_initialize(&cdev->dev);
  654. if (!get_device(&sch->dev)) {
  655. if (cdev->dev.release)
  656. cdev->dev.release(&cdev->dev);
  657. return -ENODEV;
  658. }
  659. return 0;
  660. }
  661. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  662. {
  663. struct ccw_device *cdev;
  664. int ret;
  665. cdev = io_subchannel_allocate_dev(sch);
  666. if (!IS_ERR(cdev)) {
  667. ret = io_subchannel_initialize_dev(sch, cdev);
  668. if (ret) {
  669. kfree(cdev);
  670. cdev = ERR_PTR(ret);
  671. }
  672. }
  673. return cdev;
  674. }
  675. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  676. static void sch_attach_device(struct subchannel *sch,
  677. struct ccw_device *cdev)
  678. {
  679. css_update_ssd_info(sch);
  680. spin_lock_irq(sch->lock);
  681. sch_set_cdev(sch, cdev);
  682. cdev->private->schid = sch->schid;
  683. cdev->ccwlock = sch->lock;
  684. ccw_device_trigger_reprobe(cdev);
  685. spin_unlock_irq(sch->lock);
  686. }
  687. static void sch_attach_disconnected_device(struct subchannel *sch,
  688. struct ccw_device *cdev)
  689. {
  690. struct subchannel *other_sch;
  691. int ret;
  692. other_sch = to_subchannel(get_device(cdev->dev.parent));
  693. ret = device_move(&cdev->dev, &sch->dev);
  694. if (ret) {
  695. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  696. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  697. cdev->private->dev_id.devno, ret);
  698. put_device(&other_sch->dev);
  699. return;
  700. }
  701. sch_set_cdev(other_sch, NULL);
  702. /* No need to keep a subchannel without ccw device around. */
  703. css_sch_device_unregister(other_sch);
  704. put_device(&other_sch->dev);
  705. sch_attach_device(sch, cdev);
  706. }
  707. static void sch_attach_orphaned_device(struct subchannel *sch,
  708. struct ccw_device *cdev)
  709. {
  710. int ret;
  711. /* Try to move the ccw device to its new subchannel. */
  712. ret = device_move(&cdev->dev, &sch->dev);
  713. if (ret) {
  714. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  715. "failed (ret=%d)!\n",
  716. cdev->private->dev_id.ssid,
  717. cdev->private->dev_id.devno, ret);
  718. return;
  719. }
  720. sch_attach_device(sch, cdev);
  721. }
  722. static void sch_create_and_recog_new_device(struct subchannel *sch)
  723. {
  724. struct ccw_device *cdev;
  725. /* Need to allocate a new ccw device. */
  726. cdev = io_subchannel_create_ccwdev(sch);
  727. if (IS_ERR(cdev)) {
  728. /* OK, we did everything we could... */
  729. css_sch_device_unregister(sch);
  730. return;
  731. }
  732. spin_lock_irq(sch->lock);
  733. sch_set_cdev(sch, cdev);
  734. spin_unlock_irq(sch->lock);
  735. /* Start recognition for the new ccw device. */
  736. if (io_subchannel_recog(cdev, sch)) {
  737. spin_lock_irq(sch->lock);
  738. sch_set_cdev(sch, NULL);
  739. spin_unlock_irq(sch->lock);
  740. if (cdev->dev.release)
  741. cdev->dev.release(&cdev->dev);
  742. css_sch_device_unregister(sch);
  743. }
  744. }
  745. void ccw_device_move_to_orphanage(struct work_struct *work)
  746. {
  747. struct ccw_device_private *priv;
  748. struct ccw_device *cdev;
  749. struct ccw_device *replacing_cdev;
  750. struct subchannel *sch;
  751. int ret;
  752. struct channel_subsystem *css;
  753. struct ccw_dev_id dev_id;
  754. priv = container_of(work, struct ccw_device_private, kick_work);
  755. cdev = priv->cdev;
  756. sch = to_subchannel(cdev->dev.parent);
  757. css = to_css(sch->dev.parent);
  758. dev_id.devno = sch->schib.pmcw.dev;
  759. dev_id.ssid = sch->schid.ssid;
  760. /*
  761. * Move the orphaned ccw device to the orphanage so the replacing
  762. * ccw device can take its place on the subchannel.
  763. */
  764. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  765. if (ret) {
  766. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  767. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  768. cdev->private->dev_id.devno, ret);
  769. return;
  770. }
  771. cdev->ccwlock = css->pseudo_subchannel->lock;
  772. /*
  773. * Search for the replacing ccw device
  774. * - among the disconnected devices
  775. * - in the orphanage
  776. */
  777. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  778. if (replacing_cdev) {
  779. sch_attach_disconnected_device(sch, replacing_cdev);
  780. return;
  781. }
  782. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  783. if (replacing_cdev) {
  784. sch_attach_orphaned_device(sch, replacing_cdev);
  785. return;
  786. }
  787. sch_create_and_recog_new_device(sch);
  788. }
  789. /*
  790. * Register recognized device.
  791. */
  792. static void
  793. io_subchannel_register(struct work_struct *work)
  794. {
  795. struct ccw_device_private *priv;
  796. struct ccw_device *cdev;
  797. struct subchannel *sch;
  798. int ret;
  799. unsigned long flags;
  800. priv = container_of(work, struct ccw_device_private, kick_work);
  801. cdev = priv->cdev;
  802. sch = to_subchannel(cdev->dev.parent);
  803. css_update_ssd_info(sch);
  804. /*
  805. * io_subchannel_register() will also be called after device
  806. * recognition has been done for a boxed device (which will already
  807. * be registered). We need to reprobe since we may now have sense id
  808. * information.
  809. */
  810. if (klist_node_attached(&cdev->dev.knode_parent)) {
  811. if (!cdev->drv) {
  812. ret = device_reprobe(&cdev->dev);
  813. if (ret)
  814. /* We can't do much here. */
  815. CIO_MSG_EVENT(0, "device_reprobe() returned"
  816. " %d for 0.%x.%04x\n", ret,
  817. cdev->private->dev_id.ssid,
  818. cdev->private->dev_id.devno);
  819. }
  820. goto out;
  821. }
  822. /*
  823. * Now we know this subchannel will stay, we can throw
  824. * our delayed uevent.
  825. */
  826. sch->dev.uevent_suppress = 0;
  827. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  828. /* make it known to the system */
  829. ret = ccw_device_register(cdev);
  830. if (ret) {
  831. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  832. cdev->private->dev_id.ssid,
  833. cdev->private->dev_id.devno, ret);
  834. put_device(&cdev->dev);
  835. spin_lock_irqsave(sch->lock, flags);
  836. sch_set_cdev(sch, NULL);
  837. spin_unlock_irqrestore(sch->lock, flags);
  838. kfree (cdev->private);
  839. kfree (cdev);
  840. put_device(&sch->dev);
  841. if (atomic_dec_and_test(&ccw_device_init_count))
  842. wake_up(&ccw_device_init_wq);
  843. return;
  844. }
  845. put_device(&cdev->dev);
  846. out:
  847. cdev->private->flags.recog_done = 1;
  848. put_device(&sch->dev);
  849. wake_up(&cdev->private->wait_q);
  850. if (atomic_dec_and_test(&ccw_device_init_count))
  851. wake_up(&ccw_device_init_wq);
  852. }
  853. static void ccw_device_call_sch_unregister(struct work_struct *work)
  854. {
  855. struct ccw_device_private *priv;
  856. struct ccw_device *cdev;
  857. struct subchannel *sch;
  858. priv = container_of(work, struct ccw_device_private, kick_work);
  859. cdev = priv->cdev;
  860. sch = to_subchannel(cdev->dev.parent);
  861. css_sch_device_unregister(sch);
  862. /* Reset intparm to zeroes. */
  863. sch->schib.pmcw.intparm = 0;
  864. cio_modify(sch);
  865. put_device(&cdev->dev);
  866. put_device(&sch->dev);
  867. }
  868. /*
  869. * subchannel recognition done. Called from the state machine.
  870. */
  871. void
  872. io_subchannel_recog_done(struct ccw_device *cdev)
  873. {
  874. struct subchannel *sch;
  875. if (css_init_done == 0) {
  876. cdev->private->flags.recog_done = 1;
  877. return;
  878. }
  879. switch (cdev->private->state) {
  880. case DEV_STATE_NOT_OPER:
  881. cdev->private->flags.recog_done = 1;
  882. /* Remove device found not operational. */
  883. if (!get_device(&cdev->dev))
  884. break;
  885. sch = to_subchannel(cdev->dev.parent);
  886. PREPARE_WORK(&cdev->private->kick_work,
  887. ccw_device_call_sch_unregister);
  888. queue_work(slow_path_wq, &cdev->private->kick_work);
  889. if (atomic_dec_and_test(&ccw_device_init_count))
  890. wake_up(&ccw_device_init_wq);
  891. break;
  892. case DEV_STATE_BOXED:
  893. /* Device did not respond in time. */
  894. case DEV_STATE_OFFLINE:
  895. /*
  896. * We can't register the device in interrupt context so
  897. * we schedule a work item.
  898. */
  899. if (!get_device(&cdev->dev))
  900. break;
  901. PREPARE_WORK(&cdev->private->kick_work,
  902. io_subchannel_register);
  903. queue_work(slow_path_wq, &cdev->private->kick_work);
  904. break;
  905. }
  906. }
  907. static int
  908. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  909. {
  910. int rc;
  911. struct ccw_device_private *priv;
  912. sch_set_cdev(sch, cdev);
  913. cdev->ccwlock = sch->lock;
  914. /* Init private data. */
  915. priv = cdev->private;
  916. priv->dev_id.devno = sch->schib.pmcw.dev;
  917. priv->dev_id.ssid = sch->schid.ssid;
  918. priv->schid = sch->schid;
  919. priv->state = DEV_STATE_NOT_OPER;
  920. INIT_LIST_HEAD(&priv->cmb_list);
  921. init_waitqueue_head(&priv->wait_q);
  922. init_timer(&priv->timer);
  923. /* Set an initial name for the device. */
  924. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  925. sch->schid.ssid, sch->schib.pmcw.dev);
  926. /* Increase counter of devices currently in recognition. */
  927. atomic_inc(&ccw_device_init_count);
  928. /* Start async. device sensing. */
  929. spin_lock_irq(sch->lock);
  930. rc = ccw_device_recognition(cdev);
  931. spin_unlock_irq(sch->lock);
  932. if (rc) {
  933. if (atomic_dec_and_test(&ccw_device_init_count))
  934. wake_up(&ccw_device_init_wq);
  935. }
  936. return rc;
  937. }
  938. static void ccw_device_move_to_sch(struct work_struct *work)
  939. {
  940. struct ccw_device_private *priv;
  941. int rc;
  942. struct subchannel *sch;
  943. struct ccw_device *cdev;
  944. struct subchannel *former_parent;
  945. priv = container_of(work, struct ccw_device_private, kick_work);
  946. sch = priv->sch;
  947. cdev = priv->cdev;
  948. former_parent = ccw_device_is_orphan(cdev) ?
  949. NULL : to_subchannel(get_device(cdev->dev.parent));
  950. mutex_lock(&sch->reg_mutex);
  951. /* Try to move the ccw device to its new subchannel. */
  952. rc = device_move(&cdev->dev, &sch->dev);
  953. mutex_unlock(&sch->reg_mutex);
  954. if (rc) {
  955. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  956. "0.%x.%04x failed (ret=%d)!\n",
  957. cdev->private->dev_id.ssid,
  958. cdev->private->dev_id.devno, sch->schid.ssid,
  959. sch->schid.sch_no, rc);
  960. css_sch_device_unregister(sch);
  961. goto out;
  962. }
  963. if (former_parent) {
  964. spin_lock_irq(former_parent->lock);
  965. sch_set_cdev(former_parent, NULL);
  966. spin_unlock_irq(former_parent->lock);
  967. css_sch_device_unregister(former_parent);
  968. /* Reset intparm to zeroes. */
  969. former_parent->schib.pmcw.intparm = 0;
  970. cio_modify(former_parent);
  971. }
  972. sch_attach_device(sch, cdev);
  973. out:
  974. if (former_parent)
  975. put_device(&former_parent->dev);
  976. put_device(&cdev->dev);
  977. }
  978. static void io_subchannel_irq(struct subchannel *sch)
  979. {
  980. struct ccw_device *cdev;
  981. cdev = sch_get_cdev(sch);
  982. CIO_TRACE_EVENT(3, "IRQ");
  983. CIO_TRACE_EVENT(3, sch->dev.bus_id);
  984. if (cdev)
  985. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  986. }
  987. static void io_subchannel_init_fields(struct subchannel *sch)
  988. {
  989. if (cio_is_console(sch->schid))
  990. sch->opm = 0xff;
  991. else
  992. sch->opm = chp_get_sch_opm(sch);
  993. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  994. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  995. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  996. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  997. sch->schib.pmcw.dev, sch->schid.ssid,
  998. sch->schid.sch_no, sch->schib.pmcw.pim,
  999. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1000. /* Initially set up some fields in the pmcw. */
  1001. sch->schib.pmcw.ena = 0;
  1002. sch->schib.pmcw.csense = 1; /* concurrent sense */
  1003. if ((sch->lpm & (sch->lpm - 1)) != 0)
  1004. sch->schib.pmcw.mp = 1; /* multipath mode */
  1005. /* clean up possible residual cmf stuff */
  1006. sch->schib.pmcw.mme = 0;
  1007. sch->schib.pmcw.mbfc = 0;
  1008. sch->schib.pmcw.mbi = 0;
  1009. sch->schib.mba = 0;
  1010. }
  1011. static int io_subchannel_probe(struct subchannel *sch)
  1012. {
  1013. struct ccw_device *cdev;
  1014. int rc;
  1015. unsigned long flags;
  1016. struct ccw_dev_id dev_id;
  1017. cdev = sch_get_cdev(sch);
  1018. if (cdev) {
  1019. rc = sysfs_create_group(&sch->dev.kobj,
  1020. &io_subchannel_attr_group);
  1021. if (rc)
  1022. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1023. "attributes for subchannel "
  1024. "0.%x.%04x (rc=%d)\n",
  1025. sch->schid.ssid, sch->schid.sch_no, rc);
  1026. /*
  1027. * This subchannel already has an associated ccw_device.
  1028. * Throw the delayed uevent for the subchannel, register
  1029. * the ccw_device and exit. This happens for all early
  1030. * devices, e.g. the console.
  1031. */
  1032. sch->dev.uevent_suppress = 0;
  1033. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1034. cdev->dev.groups = ccwdev_attr_groups;
  1035. device_initialize(&cdev->dev);
  1036. ccw_device_register(cdev);
  1037. /*
  1038. * Check if the device is already online. If it is
  1039. * the reference count needs to be corrected
  1040. * (see ccw_device_online and css_init_done for the
  1041. * ugly details).
  1042. */
  1043. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1044. cdev->private->state != DEV_STATE_OFFLINE &&
  1045. cdev->private->state != DEV_STATE_BOXED)
  1046. get_device(&cdev->dev);
  1047. return 0;
  1048. }
  1049. io_subchannel_init_fields(sch);
  1050. /*
  1051. * First check if a fitting device may be found amongst the
  1052. * disconnected devices or in the orphanage.
  1053. */
  1054. dev_id.devno = sch->schib.pmcw.dev;
  1055. dev_id.ssid = sch->schid.ssid;
  1056. rc = sysfs_create_group(&sch->dev.kobj,
  1057. &io_subchannel_attr_group);
  1058. if (rc)
  1059. return rc;
  1060. /* Allocate I/O subchannel private data. */
  1061. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1062. GFP_KERNEL | GFP_DMA);
  1063. if (!sch->private) {
  1064. rc = -ENOMEM;
  1065. goto out_err;
  1066. }
  1067. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1068. if (!cdev)
  1069. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1070. &dev_id);
  1071. if (cdev) {
  1072. /*
  1073. * Schedule moving the device until when we have a registered
  1074. * subchannel to move to and succeed the probe. We can
  1075. * unregister later again, when the probe is through.
  1076. */
  1077. cdev->private->sch = sch;
  1078. PREPARE_WORK(&cdev->private->kick_work,
  1079. ccw_device_move_to_sch);
  1080. queue_work(slow_path_wq, &cdev->private->kick_work);
  1081. return 0;
  1082. }
  1083. cdev = io_subchannel_create_ccwdev(sch);
  1084. if (IS_ERR(cdev)) {
  1085. rc = PTR_ERR(cdev);
  1086. goto out_err;
  1087. }
  1088. rc = io_subchannel_recog(cdev, sch);
  1089. if (rc) {
  1090. spin_lock_irqsave(sch->lock, flags);
  1091. sch_set_cdev(sch, NULL);
  1092. spin_unlock_irqrestore(sch->lock, flags);
  1093. if (cdev->dev.release)
  1094. cdev->dev.release(&cdev->dev);
  1095. goto out_err;
  1096. }
  1097. return 0;
  1098. out_err:
  1099. kfree(sch->private);
  1100. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1101. return rc;
  1102. }
  1103. static int
  1104. io_subchannel_remove (struct subchannel *sch)
  1105. {
  1106. struct ccw_device *cdev;
  1107. unsigned long flags;
  1108. cdev = sch_get_cdev(sch);
  1109. if (!cdev)
  1110. return 0;
  1111. /* Set ccw device to not operational and drop reference. */
  1112. spin_lock_irqsave(cdev->ccwlock, flags);
  1113. sch_set_cdev(sch, NULL);
  1114. cdev->private->state = DEV_STATE_NOT_OPER;
  1115. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1116. ccw_device_unregister(cdev);
  1117. put_device(&cdev->dev);
  1118. kfree(sch->private);
  1119. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1120. return 0;
  1121. }
  1122. static int io_subchannel_notify(struct subchannel *sch, int event)
  1123. {
  1124. struct ccw_device *cdev;
  1125. cdev = sch_get_cdev(sch);
  1126. if (!cdev)
  1127. return 0;
  1128. return ccw_device_notify(cdev, event);
  1129. }
  1130. static void io_subchannel_verify(struct subchannel *sch)
  1131. {
  1132. struct ccw_device *cdev;
  1133. cdev = sch_get_cdev(sch);
  1134. if (cdev)
  1135. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1136. }
  1137. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1138. {
  1139. int cc;
  1140. cc = stsch(sch->schid, &sch->schib);
  1141. if (cc)
  1142. return 0;
  1143. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1144. return 1;
  1145. return 0;
  1146. }
  1147. static void terminate_internal_io(struct subchannel *sch,
  1148. struct ccw_device *cdev)
  1149. {
  1150. if (cio_clear(sch)) {
  1151. /* Recheck device in case clear failed. */
  1152. sch->lpm = 0;
  1153. if (cdev->online)
  1154. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1155. else
  1156. css_schedule_eval(sch->schid);
  1157. return;
  1158. }
  1159. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1160. /* Request retry of internal operation. */
  1161. cdev->private->flags.intretry = 1;
  1162. /* Call handler. */
  1163. if (cdev->handler)
  1164. cdev->handler(cdev, cdev->private->intparm,
  1165. ERR_PTR(-EIO));
  1166. }
  1167. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1168. {
  1169. struct ccw_device *cdev;
  1170. cdev = sch_get_cdev(sch);
  1171. if (!cdev)
  1172. return;
  1173. if (check_for_io_on_path(sch, mask)) {
  1174. if (cdev->private->state == DEV_STATE_ONLINE)
  1175. ccw_device_kill_io(cdev);
  1176. else {
  1177. terminate_internal_io(sch, cdev);
  1178. /* Re-start path verification. */
  1179. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1180. }
  1181. } else
  1182. /* trigger path verification. */
  1183. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1184. }
  1185. static int io_subchannel_chp_event(struct subchannel *sch,
  1186. struct chp_link *link, int event)
  1187. {
  1188. int mask;
  1189. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1190. if (!mask)
  1191. return 0;
  1192. switch (event) {
  1193. case CHP_VARY_OFF:
  1194. sch->opm &= ~mask;
  1195. sch->lpm &= ~mask;
  1196. io_subchannel_terminate_path(sch, mask);
  1197. break;
  1198. case CHP_VARY_ON:
  1199. sch->opm |= mask;
  1200. sch->lpm |= mask;
  1201. io_subchannel_verify(sch);
  1202. break;
  1203. case CHP_OFFLINE:
  1204. if (stsch(sch->schid, &sch->schib))
  1205. return -ENXIO;
  1206. if (!css_sch_is_valid(&sch->schib))
  1207. return -ENODEV;
  1208. io_subchannel_terminate_path(sch, mask);
  1209. break;
  1210. case CHP_ONLINE:
  1211. if (stsch(sch->schid, &sch->schib))
  1212. return -ENXIO;
  1213. sch->lpm |= mask & sch->opm;
  1214. io_subchannel_verify(sch);
  1215. break;
  1216. }
  1217. return 0;
  1218. }
  1219. static void
  1220. io_subchannel_shutdown(struct subchannel *sch)
  1221. {
  1222. struct ccw_device *cdev;
  1223. int ret;
  1224. cdev = sch_get_cdev(sch);
  1225. if (cio_is_console(sch->schid))
  1226. return;
  1227. if (!sch->schib.pmcw.ena)
  1228. /* Nothing to do. */
  1229. return;
  1230. ret = cio_disable_subchannel(sch);
  1231. if (ret != -EBUSY)
  1232. /* Subchannel is disabled, we're done. */
  1233. return;
  1234. cdev->private->state = DEV_STATE_QUIESCE;
  1235. if (cdev->handler)
  1236. cdev->handler(cdev, cdev->private->intparm,
  1237. ERR_PTR(-EIO));
  1238. ret = ccw_device_cancel_halt_clear(cdev);
  1239. if (ret == -EBUSY) {
  1240. ccw_device_set_timeout(cdev, HZ/10);
  1241. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1242. }
  1243. cio_disable_subchannel(sch);
  1244. }
  1245. static int io_subchannel_get_status(struct subchannel *sch)
  1246. {
  1247. struct schib schib;
  1248. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1249. return CIO_GONE;
  1250. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1251. return CIO_REVALIDATE;
  1252. if (!sch->lpm)
  1253. return CIO_NO_PATH;
  1254. return CIO_OPER;
  1255. }
  1256. static int device_is_disconnected(struct ccw_device *cdev)
  1257. {
  1258. if (!cdev)
  1259. return 0;
  1260. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1261. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1262. }
  1263. static int recovery_check(struct device *dev, void *data)
  1264. {
  1265. struct ccw_device *cdev = to_ccwdev(dev);
  1266. int *redo = data;
  1267. spin_lock_irq(cdev->ccwlock);
  1268. switch (cdev->private->state) {
  1269. case DEV_STATE_DISCONNECTED:
  1270. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1271. cdev->private->dev_id.ssid,
  1272. cdev->private->dev_id.devno);
  1273. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1274. *redo = 1;
  1275. break;
  1276. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1277. *redo = 1;
  1278. break;
  1279. }
  1280. spin_unlock_irq(cdev->ccwlock);
  1281. return 0;
  1282. }
  1283. static void recovery_work_func(struct work_struct *unused)
  1284. {
  1285. int redo = 0;
  1286. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1287. if (redo) {
  1288. spin_lock_irq(&recovery_lock);
  1289. if (!timer_pending(&recovery_timer)) {
  1290. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1291. recovery_phase++;
  1292. mod_timer(&recovery_timer, jiffies +
  1293. recovery_delay[recovery_phase] * HZ);
  1294. }
  1295. spin_unlock_irq(&recovery_lock);
  1296. } else
  1297. CIO_MSG_EVENT(4, "recovery: end\n");
  1298. }
  1299. static DECLARE_WORK(recovery_work, recovery_work_func);
  1300. static void recovery_func(unsigned long data)
  1301. {
  1302. /*
  1303. * We can't do our recovery in softirq context and it's not
  1304. * performance critical, so we schedule it.
  1305. */
  1306. schedule_work(&recovery_work);
  1307. }
  1308. static void ccw_device_schedule_recovery(void)
  1309. {
  1310. unsigned long flags;
  1311. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1312. spin_lock_irqsave(&recovery_lock, flags);
  1313. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1314. recovery_phase = 0;
  1315. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1316. }
  1317. spin_unlock_irqrestore(&recovery_lock, flags);
  1318. }
  1319. static void device_set_disconnected(struct ccw_device *cdev)
  1320. {
  1321. if (!cdev)
  1322. return;
  1323. ccw_device_set_timeout(cdev, 0);
  1324. cdev->private->flags.fake_irb = 0;
  1325. cdev->private->state = DEV_STATE_DISCONNECTED;
  1326. if (cdev->online)
  1327. ccw_device_schedule_recovery();
  1328. }
  1329. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1330. {
  1331. int event, ret, disc;
  1332. unsigned long flags;
  1333. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
  1334. struct ccw_device *cdev;
  1335. spin_lock_irqsave(sch->lock, flags);
  1336. cdev = sch_get_cdev(sch);
  1337. disc = device_is_disconnected(cdev);
  1338. if (disc && slow) {
  1339. /* Disconnected devices are evaluated directly only.*/
  1340. spin_unlock_irqrestore(sch->lock, flags);
  1341. return 0;
  1342. }
  1343. /* No interrupt after machine check - kill pending timers. */
  1344. if (cdev)
  1345. ccw_device_set_timeout(cdev, 0);
  1346. if (!disc && !slow) {
  1347. /* Non-disconnected devices are evaluated on the slow path. */
  1348. spin_unlock_irqrestore(sch->lock, flags);
  1349. return -EAGAIN;
  1350. }
  1351. event = io_subchannel_get_status(sch);
  1352. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1353. sch->schid.ssid, sch->schid.sch_no, event,
  1354. disc ? "disconnected" : "normal",
  1355. slow ? "slow" : "fast");
  1356. /* Analyze subchannel status. */
  1357. action = NONE;
  1358. switch (event) {
  1359. case CIO_NO_PATH:
  1360. if (disc) {
  1361. /* Check if paths have become available. */
  1362. action = REPROBE;
  1363. break;
  1364. }
  1365. /* fall through */
  1366. case CIO_GONE:
  1367. /* Prevent unwanted effects when opening lock. */
  1368. cio_disable_subchannel(sch);
  1369. device_set_disconnected(cdev);
  1370. /* Ask driver what to do with device. */
  1371. action = UNREGISTER;
  1372. spin_unlock_irqrestore(sch->lock, flags);
  1373. ret = io_subchannel_notify(sch, event);
  1374. spin_lock_irqsave(sch->lock, flags);
  1375. if (ret)
  1376. action = NONE;
  1377. break;
  1378. case CIO_REVALIDATE:
  1379. /* Device will be removed, so no notify necessary. */
  1380. if (disc)
  1381. /* Reprobe because immediate unregister might block. */
  1382. action = REPROBE;
  1383. else
  1384. action = UNREGISTER_PROBE;
  1385. break;
  1386. case CIO_OPER:
  1387. if (disc)
  1388. /* Get device operational again. */
  1389. action = REPROBE;
  1390. break;
  1391. }
  1392. /* Perform action. */
  1393. ret = 0;
  1394. switch (action) {
  1395. case UNREGISTER:
  1396. case UNREGISTER_PROBE:
  1397. /* Unregister device (will use subchannel lock). */
  1398. spin_unlock_irqrestore(sch->lock, flags);
  1399. css_sch_device_unregister(sch);
  1400. spin_lock_irqsave(sch->lock, flags);
  1401. /* Reset intparm to zeroes. */
  1402. sch->schib.pmcw.intparm = 0;
  1403. cio_modify(sch);
  1404. break;
  1405. case REPROBE:
  1406. ccw_device_trigger_reprobe(cdev);
  1407. break;
  1408. default:
  1409. break;
  1410. }
  1411. spin_unlock_irqrestore(sch->lock, flags);
  1412. /* Probe if necessary. */
  1413. if (action == UNREGISTER_PROBE)
  1414. ret = css_probe_device(sch->schid);
  1415. return ret;
  1416. }
  1417. #ifdef CONFIG_CCW_CONSOLE
  1418. static struct ccw_device console_cdev;
  1419. static struct ccw_device_private console_private;
  1420. static int console_cdev_in_use;
  1421. static DEFINE_SPINLOCK(ccw_console_lock);
  1422. spinlock_t * cio_get_console_lock(void)
  1423. {
  1424. return &ccw_console_lock;
  1425. }
  1426. static int ccw_device_console_enable(struct ccw_device *cdev,
  1427. struct subchannel *sch)
  1428. {
  1429. int rc;
  1430. /* Attach subchannel private data. */
  1431. sch->private = cio_get_console_priv();
  1432. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1433. io_subchannel_init_fields(sch);
  1434. sch->driver = &io_subchannel_driver;
  1435. /* Initialize the ccw_device structure. */
  1436. cdev->dev.parent= &sch->dev;
  1437. rc = io_subchannel_recog(cdev, sch);
  1438. if (rc)
  1439. return rc;
  1440. /* Now wait for the async. recognition to come to an end. */
  1441. spin_lock_irq(cdev->ccwlock);
  1442. while (!dev_fsm_final_state(cdev))
  1443. wait_cons_dev();
  1444. rc = -EIO;
  1445. if (cdev->private->state != DEV_STATE_OFFLINE)
  1446. goto out_unlock;
  1447. ccw_device_online(cdev);
  1448. while (!dev_fsm_final_state(cdev))
  1449. wait_cons_dev();
  1450. if (cdev->private->state != DEV_STATE_ONLINE)
  1451. goto out_unlock;
  1452. rc = 0;
  1453. out_unlock:
  1454. spin_unlock_irq(cdev->ccwlock);
  1455. return 0;
  1456. }
  1457. struct ccw_device *
  1458. ccw_device_probe_console(void)
  1459. {
  1460. struct subchannel *sch;
  1461. int ret;
  1462. if (xchg(&console_cdev_in_use, 1) != 0)
  1463. return ERR_PTR(-EBUSY);
  1464. sch = cio_probe_console();
  1465. if (IS_ERR(sch)) {
  1466. console_cdev_in_use = 0;
  1467. return (void *) sch;
  1468. }
  1469. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1470. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1471. console_cdev.private = &console_private;
  1472. console_private.cdev = &console_cdev;
  1473. ret = ccw_device_console_enable(&console_cdev, sch);
  1474. if (ret) {
  1475. cio_release_console();
  1476. console_cdev_in_use = 0;
  1477. return ERR_PTR(ret);
  1478. }
  1479. console_cdev.online = 1;
  1480. return &console_cdev;
  1481. }
  1482. #endif
  1483. /*
  1484. * get ccw_device matching the busid, but only if owned by cdrv
  1485. */
  1486. static int
  1487. __ccwdev_check_busid(struct device *dev, void *id)
  1488. {
  1489. char *bus_id;
  1490. bus_id = id;
  1491. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1492. }
  1493. /**
  1494. * get_ccwdev_by_busid() - obtain device from a bus id
  1495. * @cdrv: driver the device is owned by
  1496. * @bus_id: bus id of the device to be searched
  1497. *
  1498. * This function searches all devices owned by @cdrv for a device with a bus
  1499. * id matching @bus_id.
  1500. * Returns:
  1501. * If a match is found, its reference count of the found device is increased
  1502. * and it is returned; else %NULL is returned.
  1503. */
  1504. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1505. const char *bus_id)
  1506. {
  1507. struct device *dev;
  1508. struct device_driver *drv;
  1509. drv = get_driver(&cdrv->driver);
  1510. if (!drv)
  1511. return NULL;
  1512. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1513. __ccwdev_check_busid);
  1514. put_driver(drv);
  1515. return dev ? to_ccwdev(dev) : NULL;
  1516. }
  1517. /************************** device driver handling ************************/
  1518. /* This is the implementation of the ccw_driver class. The probe, remove
  1519. * and release methods are initially very similar to the device_driver
  1520. * implementations, with the difference that they have ccw_device
  1521. * arguments.
  1522. *
  1523. * A ccw driver also contains the information that is needed for
  1524. * device matching.
  1525. */
  1526. static int
  1527. ccw_device_probe (struct device *dev)
  1528. {
  1529. struct ccw_device *cdev = to_ccwdev(dev);
  1530. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1531. int ret;
  1532. cdev->drv = cdrv; /* to let the driver call _set_online */
  1533. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1534. if (ret) {
  1535. cdev->drv = NULL;
  1536. return ret;
  1537. }
  1538. return 0;
  1539. }
  1540. static int
  1541. ccw_device_remove (struct device *dev)
  1542. {
  1543. struct ccw_device *cdev = to_ccwdev(dev);
  1544. struct ccw_driver *cdrv = cdev->drv;
  1545. int ret;
  1546. if (cdrv->remove)
  1547. cdrv->remove(cdev);
  1548. if (cdev->online) {
  1549. cdev->online = 0;
  1550. spin_lock_irq(cdev->ccwlock);
  1551. ret = ccw_device_offline(cdev);
  1552. spin_unlock_irq(cdev->ccwlock);
  1553. if (ret == 0)
  1554. wait_event(cdev->private->wait_q,
  1555. dev_fsm_final_state(cdev));
  1556. else
  1557. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1558. "device 0.%x.%04x\n",
  1559. ret, cdev->private->dev_id.ssid,
  1560. cdev->private->dev_id.devno);
  1561. }
  1562. ccw_device_set_timeout(cdev, 0);
  1563. cdev->drv = NULL;
  1564. return 0;
  1565. }
  1566. static void ccw_device_shutdown(struct device *dev)
  1567. {
  1568. struct ccw_device *cdev;
  1569. cdev = to_ccwdev(dev);
  1570. if (cdev->drv && cdev->drv->shutdown)
  1571. cdev->drv->shutdown(cdev);
  1572. disable_cmf(cdev);
  1573. }
  1574. struct bus_type ccw_bus_type = {
  1575. .name = "ccw",
  1576. .match = ccw_bus_match,
  1577. .uevent = ccw_uevent,
  1578. .probe = ccw_device_probe,
  1579. .remove = ccw_device_remove,
  1580. .shutdown = ccw_device_shutdown,
  1581. };
  1582. /**
  1583. * ccw_driver_register() - register a ccw driver
  1584. * @cdriver: driver to be registered
  1585. *
  1586. * This function is mainly a wrapper around driver_register().
  1587. * Returns:
  1588. * %0 on success and a negative error value on failure.
  1589. */
  1590. int ccw_driver_register(struct ccw_driver *cdriver)
  1591. {
  1592. struct device_driver *drv = &cdriver->driver;
  1593. drv->bus = &ccw_bus_type;
  1594. drv->name = cdriver->name;
  1595. drv->owner = cdriver->owner;
  1596. return driver_register(drv);
  1597. }
  1598. /**
  1599. * ccw_driver_unregister() - deregister a ccw driver
  1600. * @cdriver: driver to be deregistered
  1601. *
  1602. * This function is mainly a wrapper around driver_unregister().
  1603. */
  1604. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1605. {
  1606. driver_unregister(&cdriver->driver);
  1607. }
  1608. /* Helper func for qdio. */
  1609. struct subchannel_id
  1610. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1611. {
  1612. struct subchannel *sch;
  1613. sch = to_subchannel(cdev->dev.parent);
  1614. return sch->schid;
  1615. }
  1616. MODULE_LICENSE("GPL");
  1617. EXPORT_SYMBOL(ccw_device_set_online);
  1618. EXPORT_SYMBOL(ccw_device_set_offline);
  1619. EXPORT_SYMBOL(ccw_driver_register);
  1620. EXPORT_SYMBOL(ccw_driver_unregister);
  1621. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1622. EXPORT_SYMBOL(ccw_bus_type);
  1623. EXPORT_SYMBOL(ccw_device_work);
  1624. EXPORT_SYMBOL(ccw_device_notify_work);
  1625. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);