device.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834
  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_device_id io_subchannel_ids[] = {
  113. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  114. { /* end of list */ },
  115. };
  116. MODULE_DEVICE_TABLE(css, io_subchannel_ids);
  117. static struct css_driver io_subchannel_driver = {
  118. .owner = THIS_MODULE,
  119. .subchannel_type = io_subchannel_ids,
  120. .name = "io_subchannel",
  121. .irq = io_subchannel_irq,
  122. .sch_event = io_subchannel_sch_event,
  123. .chp_event = io_subchannel_chp_event,
  124. .probe = io_subchannel_probe,
  125. .remove = io_subchannel_remove,
  126. .shutdown = io_subchannel_shutdown,
  127. };
  128. struct workqueue_struct *ccw_device_work;
  129. struct workqueue_struct *ccw_device_notify_work;
  130. wait_queue_head_t ccw_device_init_wq;
  131. atomic_t ccw_device_init_count;
  132. static void recovery_func(unsigned long data);
  133. static int __init
  134. init_ccw_bus_type (void)
  135. {
  136. int ret;
  137. init_waitqueue_head(&ccw_device_init_wq);
  138. atomic_set(&ccw_device_init_count, 0);
  139. setup_timer(&recovery_timer, recovery_func, 0);
  140. ccw_device_work = create_singlethread_workqueue("cio");
  141. if (!ccw_device_work)
  142. return -ENOMEM; /* FIXME: better errno ? */
  143. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  144. if (!ccw_device_notify_work) {
  145. ret = -ENOMEM; /* FIXME: better errno ? */
  146. goto out_err;
  147. }
  148. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  149. if (!slow_path_wq) {
  150. ret = -ENOMEM; /* FIXME: better errno ? */
  151. goto out_err;
  152. }
  153. if ((ret = bus_register (&ccw_bus_type)))
  154. goto out_err;
  155. ret = css_driver_register(&io_subchannel_driver);
  156. if (ret)
  157. goto out_err;
  158. wait_event(ccw_device_init_wq,
  159. atomic_read(&ccw_device_init_count) == 0);
  160. flush_workqueue(ccw_device_work);
  161. return 0;
  162. out_err:
  163. if (ccw_device_work)
  164. destroy_workqueue(ccw_device_work);
  165. if (ccw_device_notify_work)
  166. destroy_workqueue(ccw_device_notify_work);
  167. if (slow_path_wq)
  168. destroy_workqueue(slow_path_wq);
  169. return ret;
  170. }
  171. static void __exit
  172. cleanup_ccw_bus_type (void)
  173. {
  174. css_driver_unregister(&io_subchannel_driver);
  175. bus_unregister(&ccw_bus_type);
  176. destroy_workqueue(ccw_device_notify_work);
  177. destroy_workqueue(ccw_device_work);
  178. }
  179. subsys_initcall(init_ccw_bus_type);
  180. module_exit(cleanup_ccw_bus_type);
  181. /************************ device handling **************************/
  182. /*
  183. * A ccw_device has some interfaces in sysfs in addition to the
  184. * standard ones.
  185. * The following entries are designed to export the information which
  186. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  187. * are obvious, so they don't have an entry :)
  188. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  189. */
  190. static ssize_t
  191. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  192. {
  193. struct subchannel *sch = to_subchannel(dev);
  194. struct chsc_ssd_info *ssd = &sch->ssd_info;
  195. ssize_t ret = 0;
  196. int chp;
  197. int mask;
  198. for (chp = 0; chp < 8; chp++) {
  199. mask = 0x80 >> chp;
  200. if (ssd->path_mask & mask)
  201. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  202. else
  203. ret += sprintf(buf + ret, "00 ");
  204. }
  205. ret += sprintf (buf+ret, "\n");
  206. return min((ssize_t)PAGE_SIZE, ret);
  207. }
  208. static ssize_t
  209. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  210. {
  211. struct subchannel *sch = to_subchannel(dev);
  212. struct pmcw *pmcw = &sch->schib.pmcw;
  213. return sprintf (buf, "%02x %02x %02x\n",
  214. pmcw->pim, pmcw->pam, pmcw->pom);
  215. }
  216. static ssize_t
  217. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  218. {
  219. struct ccw_device *cdev = to_ccwdev(dev);
  220. struct ccw_device_id *id = &(cdev->id);
  221. if (id->dev_type != 0)
  222. return sprintf(buf, "%04x/%02x\n",
  223. id->dev_type, id->dev_model);
  224. else
  225. return sprintf(buf, "n/a\n");
  226. }
  227. static ssize_t
  228. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  229. {
  230. struct ccw_device *cdev = to_ccwdev(dev);
  231. struct ccw_device_id *id = &(cdev->id);
  232. return sprintf(buf, "%04x/%02x\n",
  233. id->cu_type, id->cu_model);
  234. }
  235. static ssize_t
  236. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  237. {
  238. struct ccw_device *cdev = to_ccwdev(dev);
  239. struct ccw_device_id *id = &(cdev->id);
  240. int len;
  241. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  242. return len > PAGE_SIZE ? PAGE_SIZE : len;
  243. }
  244. static ssize_t
  245. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  246. {
  247. struct ccw_device *cdev = to_ccwdev(dev);
  248. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  249. }
  250. int ccw_device_is_orphan(struct ccw_device *cdev)
  251. {
  252. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  253. }
  254. static void ccw_device_unregister(struct ccw_device *cdev)
  255. {
  256. if (test_and_clear_bit(1, &cdev->private->registered))
  257. device_del(&cdev->dev);
  258. }
  259. static void ccw_device_remove_orphan_cb(struct device *dev)
  260. {
  261. struct ccw_device *cdev = to_ccwdev(dev);
  262. ccw_device_unregister(cdev);
  263. put_device(&cdev->dev);
  264. }
  265. static void ccw_device_remove_sch_cb(struct device *dev)
  266. {
  267. struct subchannel *sch;
  268. sch = to_subchannel(dev);
  269. css_sch_device_unregister(sch);
  270. /* Reset intparm to zeroes. */
  271. sch->schib.pmcw.intparm = 0;
  272. cio_modify(sch);
  273. put_device(&sch->dev);
  274. }
  275. static void
  276. ccw_device_remove_disconnected(struct ccw_device *cdev)
  277. {
  278. unsigned long flags;
  279. int rc;
  280. /*
  281. * Forced offline in disconnected state means
  282. * 'throw away device'.
  283. */
  284. if (ccw_device_is_orphan(cdev)) {
  285. /*
  286. * Deregister ccw device.
  287. * Unfortunately, we cannot do this directly from the
  288. * attribute method.
  289. */
  290. spin_lock_irqsave(cdev->ccwlock, flags);
  291. cdev->private->state = DEV_STATE_NOT_OPER;
  292. spin_unlock_irqrestore(cdev->ccwlock, flags);
  293. rc = device_schedule_callback(&cdev->dev,
  294. ccw_device_remove_orphan_cb);
  295. if (rc)
  296. CIO_MSG_EVENT(0, "Couldn't unregister orphan "
  297. "0.%x.%04x\n",
  298. cdev->private->dev_id.ssid,
  299. cdev->private->dev_id.devno);
  300. return;
  301. }
  302. /* Deregister subchannel, which will kill the ccw device. */
  303. rc = device_schedule_callback(cdev->dev.parent,
  304. ccw_device_remove_sch_cb);
  305. if (rc)
  306. CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
  307. "0.%x.%04x\n",
  308. cdev->private->dev_id.ssid,
  309. cdev->private->dev_id.devno);
  310. }
  311. /**
  312. * ccw_device_set_offline() - disable a ccw device for I/O
  313. * @cdev: target ccw device
  314. *
  315. * This function calls the driver's set_offline() function for @cdev, if
  316. * given, and then disables @cdev.
  317. * Returns:
  318. * %0 on success and a negative error value on failure.
  319. * Context:
  320. * enabled, ccw device lock not held
  321. */
  322. int ccw_device_set_offline(struct ccw_device *cdev)
  323. {
  324. int ret;
  325. if (!cdev)
  326. return -ENODEV;
  327. if (!cdev->online || !cdev->drv)
  328. return -EINVAL;
  329. if (cdev->drv->set_offline) {
  330. ret = cdev->drv->set_offline(cdev);
  331. if (ret != 0)
  332. return ret;
  333. }
  334. cdev->online = 0;
  335. spin_lock_irq(cdev->ccwlock);
  336. ret = ccw_device_offline(cdev);
  337. if (ret == -ENODEV) {
  338. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  339. cdev->private->state = DEV_STATE_OFFLINE;
  340. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  341. }
  342. spin_unlock_irq(cdev->ccwlock);
  343. return ret;
  344. }
  345. spin_unlock_irq(cdev->ccwlock);
  346. if (ret == 0)
  347. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  348. else {
  349. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  350. "device 0.%x.%04x\n",
  351. ret, cdev->private->dev_id.ssid,
  352. cdev->private->dev_id.devno);
  353. cdev->online = 1;
  354. }
  355. return ret;
  356. }
  357. /**
  358. * ccw_device_set_online() - enable a ccw device for I/O
  359. * @cdev: target ccw device
  360. *
  361. * This function first enables @cdev and then calls the driver's set_online()
  362. * function for @cdev, if given. If set_online() returns an error, @cdev is
  363. * disabled again.
  364. * Returns:
  365. * %0 on success and a negative error value on failure.
  366. * Context:
  367. * enabled, ccw device lock not held
  368. */
  369. int ccw_device_set_online(struct ccw_device *cdev)
  370. {
  371. int ret;
  372. if (!cdev)
  373. return -ENODEV;
  374. if (cdev->online || !cdev->drv)
  375. return -EINVAL;
  376. spin_lock_irq(cdev->ccwlock);
  377. ret = ccw_device_online(cdev);
  378. spin_unlock_irq(cdev->ccwlock);
  379. if (ret == 0)
  380. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  381. else {
  382. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  383. "device 0.%x.%04x\n",
  384. ret, cdev->private->dev_id.ssid,
  385. cdev->private->dev_id.devno);
  386. return ret;
  387. }
  388. if (cdev->private->state != DEV_STATE_ONLINE)
  389. return -ENODEV;
  390. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  391. cdev->online = 1;
  392. return 0;
  393. }
  394. spin_lock_irq(cdev->ccwlock);
  395. ret = ccw_device_offline(cdev);
  396. spin_unlock_irq(cdev->ccwlock);
  397. if (ret == 0)
  398. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  399. else
  400. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  401. "device 0.%x.%04x\n",
  402. ret, cdev->private->dev_id.ssid,
  403. cdev->private->dev_id.devno);
  404. return (ret == 0) ? -ENODEV : ret;
  405. }
  406. static void online_store_handle_offline(struct ccw_device *cdev)
  407. {
  408. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  409. ccw_device_remove_disconnected(cdev);
  410. else if (cdev->drv && cdev->drv->set_offline)
  411. ccw_device_set_offline(cdev);
  412. }
  413. static int online_store_recog_and_online(struct ccw_device *cdev)
  414. {
  415. int ret;
  416. /* Do device recognition, if needed. */
  417. if (cdev->id.cu_type == 0) {
  418. ret = ccw_device_recognition(cdev);
  419. if (ret) {
  420. CIO_MSG_EVENT(0, "Couldn't start recognition "
  421. "for device 0.%x.%04x (ret=%d)\n",
  422. cdev->private->dev_id.ssid,
  423. cdev->private->dev_id.devno, ret);
  424. return ret;
  425. }
  426. wait_event(cdev->private->wait_q,
  427. cdev->private->flags.recog_done);
  428. }
  429. if (cdev->drv && cdev->drv->set_online)
  430. ccw_device_set_online(cdev);
  431. return 0;
  432. }
  433. static void online_store_handle_online(struct ccw_device *cdev, int force)
  434. {
  435. int ret;
  436. ret = online_store_recog_and_online(cdev);
  437. if (ret)
  438. return;
  439. if (force && cdev->private->state == DEV_STATE_BOXED) {
  440. ret = ccw_device_stlck(cdev);
  441. if (ret) {
  442. dev_warn(&cdev->dev,
  443. "ccw_device_stlck returned %d!\n", ret);
  444. return;
  445. }
  446. if (cdev->id.cu_type == 0)
  447. cdev->private->state = DEV_STATE_NOT_OPER;
  448. online_store_recog_and_online(cdev);
  449. }
  450. }
  451. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  452. const char *buf, size_t count)
  453. {
  454. struct ccw_device *cdev = to_ccwdev(dev);
  455. int force, ret;
  456. unsigned long i;
  457. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  458. return -EAGAIN;
  459. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  460. atomic_set(&cdev->private->onoff, 0);
  461. return -EINVAL;
  462. }
  463. if (!strncmp(buf, "force\n", count)) {
  464. force = 1;
  465. i = 1;
  466. ret = 0;
  467. } else {
  468. force = 0;
  469. ret = strict_strtoul(buf, 16, &i);
  470. }
  471. if (ret)
  472. goto out;
  473. switch (i) {
  474. case 0:
  475. online_store_handle_offline(cdev);
  476. ret = count;
  477. break;
  478. case 1:
  479. online_store_handle_online(cdev, force);
  480. ret = count;
  481. break;
  482. default:
  483. ret = -EINVAL;
  484. }
  485. out:
  486. if (cdev->drv)
  487. module_put(cdev->drv->owner);
  488. atomic_set(&cdev->private->onoff, 0);
  489. return ret;
  490. }
  491. static ssize_t
  492. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  493. {
  494. struct ccw_device *cdev = to_ccwdev(dev);
  495. struct subchannel *sch;
  496. if (ccw_device_is_orphan(cdev))
  497. return sprintf(buf, "no device\n");
  498. switch (cdev->private->state) {
  499. case DEV_STATE_BOXED:
  500. return sprintf(buf, "boxed\n");
  501. case DEV_STATE_DISCONNECTED:
  502. case DEV_STATE_DISCONNECTED_SENSE_ID:
  503. case DEV_STATE_NOT_OPER:
  504. sch = to_subchannel(dev->parent);
  505. if (!sch->lpm)
  506. return sprintf(buf, "no path\n");
  507. else
  508. return sprintf(buf, "no device\n");
  509. default:
  510. /* All other states considered fine. */
  511. return sprintf(buf, "good\n");
  512. }
  513. }
  514. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  515. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  516. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  517. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  518. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  519. static DEVICE_ATTR(online, 0644, online_show, online_store);
  520. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  521. static struct attribute *io_subchannel_attrs[] = {
  522. &dev_attr_chpids.attr,
  523. &dev_attr_pimpampom.attr,
  524. NULL,
  525. };
  526. static struct attribute_group io_subchannel_attr_group = {
  527. .attrs = io_subchannel_attrs,
  528. };
  529. static struct attribute * ccwdev_attrs[] = {
  530. &dev_attr_devtype.attr,
  531. &dev_attr_cutype.attr,
  532. &dev_attr_modalias.attr,
  533. &dev_attr_online.attr,
  534. &dev_attr_cmb_enable.attr,
  535. &dev_attr_availability.attr,
  536. NULL,
  537. };
  538. static struct attribute_group ccwdev_attr_group = {
  539. .attrs = ccwdev_attrs,
  540. };
  541. static struct attribute_group *ccwdev_attr_groups[] = {
  542. &ccwdev_attr_group,
  543. NULL,
  544. };
  545. /* this is a simple abstraction for device_register that sets the
  546. * correct bus type and adds the bus specific files */
  547. static int ccw_device_register(struct ccw_device *cdev)
  548. {
  549. struct device *dev = &cdev->dev;
  550. int ret;
  551. dev->bus = &ccw_bus_type;
  552. if ((ret = device_add(dev)))
  553. return ret;
  554. set_bit(1, &cdev->private->registered);
  555. return ret;
  556. }
  557. struct match_data {
  558. struct ccw_dev_id dev_id;
  559. struct ccw_device * sibling;
  560. };
  561. static int
  562. match_devno(struct device * dev, void * data)
  563. {
  564. struct match_data * d = data;
  565. struct ccw_device * cdev;
  566. cdev = to_ccwdev(dev);
  567. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  568. !ccw_device_is_orphan(cdev) &&
  569. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  570. (cdev != d->sibling))
  571. return 1;
  572. return 0;
  573. }
  574. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  575. struct ccw_device *sibling)
  576. {
  577. struct device *dev;
  578. struct match_data data;
  579. data.dev_id = *dev_id;
  580. data.sibling = sibling;
  581. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  582. return dev ? to_ccwdev(dev) : NULL;
  583. }
  584. static int match_orphan(struct device *dev, void *data)
  585. {
  586. struct ccw_dev_id *dev_id;
  587. struct ccw_device *cdev;
  588. dev_id = data;
  589. cdev = to_ccwdev(dev);
  590. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  591. }
  592. static struct ccw_device *
  593. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  594. struct ccw_dev_id *dev_id)
  595. {
  596. struct device *dev;
  597. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  598. match_orphan);
  599. return dev ? to_ccwdev(dev) : NULL;
  600. }
  601. static void
  602. ccw_device_add_changed(struct work_struct *work)
  603. {
  604. struct ccw_device_private *priv;
  605. struct ccw_device *cdev;
  606. priv = container_of(work, struct ccw_device_private, kick_work);
  607. cdev = priv->cdev;
  608. if (device_add(&cdev->dev)) {
  609. put_device(&cdev->dev);
  610. return;
  611. }
  612. set_bit(1, &cdev->private->registered);
  613. }
  614. void ccw_device_do_unreg_rereg(struct work_struct *work)
  615. {
  616. struct ccw_device_private *priv;
  617. struct ccw_device *cdev;
  618. struct subchannel *sch;
  619. priv = container_of(work, struct ccw_device_private, kick_work);
  620. cdev = priv->cdev;
  621. sch = to_subchannel(cdev->dev.parent);
  622. ccw_device_unregister(cdev);
  623. PREPARE_WORK(&cdev->private->kick_work,
  624. ccw_device_add_changed);
  625. queue_work(ccw_device_work, &cdev->private->kick_work);
  626. }
  627. static void
  628. ccw_device_release(struct device *dev)
  629. {
  630. struct ccw_device *cdev;
  631. cdev = to_ccwdev(dev);
  632. kfree(cdev->private);
  633. kfree(cdev);
  634. }
  635. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  636. {
  637. struct ccw_device *cdev;
  638. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  639. if (cdev) {
  640. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  641. GFP_KERNEL | GFP_DMA);
  642. if (cdev->private)
  643. return cdev;
  644. }
  645. kfree(cdev);
  646. return ERR_PTR(-ENOMEM);
  647. }
  648. static int io_subchannel_initialize_dev(struct subchannel *sch,
  649. struct ccw_device *cdev)
  650. {
  651. cdev->private->cdev = cdev;
  652. atomic_set(&cdev->private->onoff, 0);
  653. cdev->dev.parent = &sch->dev;
  654. cdev->dev.release = ccw_device_release;
  655. INIT_WORK(&cdev->private->kick_work, NULL);
  656. cdev->dev.groups = ccwdev_attr_groups;
  657. /* Do first half of device_register. */
  658. device_initialize(&cdev->dev);
  659. if (!get_device(&sch->dev)) {
  660. if (cdev->dev.release)
  661. cdev->dev.release(&cdev->dev);
  662. return -ENODEV;
  663. }
  664. return 0;
  665. }
  666. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  667. {
  668. struct ccw_device *cdev;
  669. int ret;
  670. cdev = io_subchannel_allocate_dev(sch);
  671. if (!IS_ERR(cdev)) {
  672. ret = io_subchannel_initialize_dev(sch, cdev);
  673. if (ret) {
  674. kfree(cdev);
  675. cdev = ERR_PTR(ret);
  676. }
  677. }
  678. return cdev;
  679. }
  680. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  681. static void sch_attach_device(struct subchannel *sch,
  682. struct ccw_device *cdev)
  683. {
  684. css_update_ssd_info(sch);
  685. spin_lock_irq(sch->lock);
  686. sch_set_cdev(sch, cdev);
  687. cdev->private->schid = sch->schid;
  688. cdev->ccwlock = sch->lock;
  689. ccw_device_trigger_reprobe(cdev);
  690. spin_unlock_irq(sch->lock);
  691. }
  692. static void sch_attach_disconnected_device(struct subchannel *sch,
  693. struct ccw_device *cdev)
  694. {
  695. struct subchannel *other_sch;
  696. int ret;
  697. other_sch = to_subchannel(get_device(cdev->dev.parent));
  698. ret = device_move(&cdev->dev, &sch->dev);
  699. if (ret) {
  700. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  701. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  702. cdev->private->dev_id.devno, ret);
  703. put_device(&other_sch->dev);
  704. return;
  705. }
  706. sch_set_cdev(other_sch, NULL);
  707. /* No need to keep a subchannel without ccw device around. */
  708. css_sch_device_unregister(other_sch);
  709. put_device(&other_sch->dev);
  710. sch_attach_device(sch, cdev);
  711. }
  712. static void sch_attach_orphaned_device(struct subchannel *sch,
  713. struct ccw_device *cdev)
  714. {
  715. int ret;
  716. /* Try to move the ccw device to its new subchannel. */
  717. ret = device_move(&cdev->dev, &sch->dev);
  718. if (ret) {
  719. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  720. "failed (ret=%d)!\n",
  721. cdev->private->dev_id.ssid,
  722. cdev->private->dev_id.devno, ret);
  723. return;
  724. }
  725. sch_attach_device(sch, cdev);
  726. }
  727. static void sch_create_and_recog_new_device(struct subchannel *sch)
  728. {
  729. struct ccw_device *cdev;
  730. /* Need to allocate a new ccw device. */
  731. cdev = io_subchannel_create_ccwdev(sch);
  732. if (IS_ERR(cdev)) {
  733. /* OK, we did everything we could... */
  734. css_sch_device_unregister(sch);
  735. return;
  736. }
  737. spin_lock_irq(sch->lock);
  738. sch_set_cdev(sch, cdev);
  739. spin_unlock_irq(sch->lock);
  740. /* Start recognition for the new ccw device. */
  741. if (io_subchannel_recog(cdev, sch)) {
  742. spin_lock_irq(sch->lock);
  743. sch_set_cdev(sch, NULL);
  744. spin_unlock_irq(sch->lock);
  745. if (cdev->dev.release)
  746. cdev->dev.release(&cdev->dev);
  747. css_sch_device_unregister(sch);
  748. }
  749. }
  750. void ccw_device_move_to_orphanage(struct work_struct *work)
  751. {
  752. struct ccw_device_private *priv;
  753. struct ccw_device *cdev;
  754. struct ccw_device *replacing_cdev;
  755. struct subchannel *sch;
  756. int ret;
  757. struct channel_subsystem *css;
  758. struct ccw_dev_id dev_id;
  759. priv = container_of(work, struct ccw_device_private, kick_work);
  760. cdev = priv->cdev;
  761. sch = to_subchannel(cdev->dev.parent);
  762. css = to_css(sch->dev.parent);
  763. dev_id.devno = sch->schib.pmcw.dev;
  764. dev_id.ssid = sch->schid.ssid;
  765. /*
  766. * Move the orphaned ccw device to the orphanage so the replacing
  767. * ccw device can take its place on the subchannel.
  768. */
  769. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  770. if (ret) {
  771. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  772. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  773. cdev->private->dev_id.devno, ret);
  774. return;
  775. }
  776. cdev->ccwlock = css->pseudo_subchannel->lock;
  777. /*
  778. * Search for the replacing ccw device
  779. * - among the disconnected devices
  780. * - in the orphanage
  781. */
  782. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  783. if (replacing_cdev) {
  784. sch_attach_disconnected_device(sch, replacing_cdev);
  785. return;
  786. }
  787. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  788. if (replacing_cdev) {
  789. sch_attach_orphaned_device(sch, replacing_cdev);
  790. return;
  791. }
  792. sch_create_and_recog_new_device(sch);
  793. }
  794. /*
  795. * Register recognized device.
  796. */
  797. static void
  798. io_subchannel_register(struct work_struct *work)
  799. {
  800. struct ccw_device_private *priv;
  801. struct ccw_device *cdev;
  802. struct subchannel *sch;
  803. int ret;
  804. unsigned long flags;
  805. priv = container_of(work, struct ccw_device_private, kick_work);
  806. cdev = priv->cdev;
  807. sch = to_subchannel(cdev->dev.parent);
  808. css_update_ssd_info(sch);
  809. /*
  810. * io_subchannel_register() will also be called after device
  811. * recognition has been done for a boxed device (which will already
  812. * be registered). We need to reprobe since we may now have sense id
  813. * information.
  814. */
  815. if (klist_node_attached(&cdev->dev.knode_parent)) {
  816. if (!cdev->drv) {
  817. ret = device_reprobe(&cdev->dev);
  818. if (ret)
  819. /* We can't do much here. */
  820. CIO_MSG_EVENT(0, "device_reprobe() returned"
  821. " %d for 0.%x.%04x\n", ret,
  822. cdev->private->dev_id.ssid,
  823. cdev->private->dev_id.devno);
  824. }
  825. goto out;
  826. }
  827. /*
  828. * Now we know this subchannel will stay, we can throw
  829. * our delayed uevent.
  830. */
  831. sch->dev.uevent_suppress = 0;
  832. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  833. /* make it known to the system */
  834. ret = ccw_device_register(cdev);
  835. if (ret) {
  836. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  837. cdev->private->dev_id.ssid,
  838. cdev->private->dev_id.devno, ret);
  839. put_device(&cdev->dev);
  840. spin_lock_irqsave(sch->lock, flags);
  841. sch_set_cdev(sch, NULL);
  842. spin_unlock_irqrestore(sch->lock, flags);
  843. kfree (cdev->private);
  844. kfree (cdev);
  845. put_device(&sch->dev);
  846. if (atomic_dec_and_test(&ccw_device_init_count))
  847. wake_up(&ccw_device_init_wq);
  848. return;
  849. }
  850. put_device(&cdev->dev);
  851. out:
  852. cdev->private->flags.recog_done = 1;
  853. put_device(&sch->dev);
  854. wake_up(&cdev->private->wait_q);
  855. if (atomic_dec_and_test(&ccw_device_init_count))
  856. wake_up(&ccw_device_init_wq);
  857. }
  858. static void ccw_device_call_sch_unregister(struct work_struct *work)
  859. {
  860. struct ccw_device_private *priv;
  861. struct ccw_device *cdev;
  862. struct subchannel *sch;
  863. priv = container_of(work, struct ccw_device_private, kick_work);
  864. cdev = priv->cdev;
  865. sch = to_subchannel(cdev->dev.parent);
  866. css_sch_device_unregister(sch);
  867. /* Reset intparm to zeroes. */
  868. sch->schib.pmcw.intparm = 0;
  869. cio_modify(sch);
  870. put_device(&cdev->dev);
  871. put_device(&sch->dev);
  872. }
  873. /*
  874. * subchannel recognition done. Called from the state machine.
  875. */
  876. void
  877. io_subchannel_recog_done(struct ccw_device *cdev)
  878. {
  879. struct subchannel *sch;
  880. if (css_init_done == 0) {
  881. cdev->private->flags.recog_done = 1;
  882. return;
  883. }
  884. switch (cdev->private->state) {
  885. case DEV_STATE_NOT_OPER:
  886. cdev->private->flags.recog_done = 1;
  887. /* Remove device found not operational. */
  888. if (!get_device(&cdev->dev))
  889. break;
  890. sch = to_subchannel(cdev->dev.parent);
  891. PREPARE_WORK(&cdev->private->kick_work,
  892. ccw_device_call_sch_unregister);
  893. queue_work(slow_path_wq, &cdev->private->kick_work);
  894. if (atomic_dec_and_test(&ccw_device_init_count))
  895. wake_up(&ccw_device_init_wq);
  896. break;
  897. case DEV_STATE_BOXED:
  898. /* Device did not respond in time. */
  899. case DEV_STATE_OFFLINE:
  900. /*
  901. * We can't register the device in interrupt context so
  902. * we schedule a work item.
  903. */
  904. if (!get_device(&cdev->dev))
  905. break;
  906. PREPARE_WORK(&cdev->private->kick_work,
  907. io_subchannel_register);
  908. queue_work(slow_path_wq, &cdev->private->kick_work);
  909. break;
  910. }
  911. }
  912. static int
  913. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  914. {
  915. int rc;
  916. struct ccw_device_private *priv;
  917. sch_set_cdev(sch, cdev);
  918. cdev->ccwlock = sch->lock;
  919. /* Init private data. */
  920. priv = cdev->private;
  921. priv->dev_id.devno = sch->schib.pmcw.dev;
  922. priv->dev_id.ssid = sch->schid.ssid;
  923. priv->schid = sch->schid;
  924. priv->state = DEV_STATE_NOT_OPER;
  925. INIT_LIST_HEAD(&priv->cmb_list);
  926. init_waitqueue_head(&priv->wait_q);
  927. init_timer(&priv->timer);
  928. /* Set an initial name for the device. */
  929. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  930. sch->schid.ssid, sch->schib.pmcw.dev);
  931. /* Increase counter of devices currently in recognition. */
  932. atomic_inc(&ccw_device_init_count);
  933. /* Start async. device sensing. */
  934. spin_lock_irq(sch->lock);
  935. rc = ccw_device_recognition(cdev);
  936. spin_unlock_irq(sch->lock);
  937. if (rc) {
  938. if (atomic_dec_and_test(&ccw_device_init_count))
  939. wake_up(&ccw_device_init_wq);
  940. }
  941. return rc;
  942. }
  943. static void ccw_device_move_to_sch(struct work_struct *work)
  944. {
  945. struct ccw_device_private *priv;
  946. int rc;
  947. struct subchannel *sch;
  948. struct ccw_device *cdev;
  949. struct subchannel *former_parent;
  950. priv = container_of(work, struct ccw_device_private, kick_work);
  951. sch = priv->sch;
  952. cdev = priv->cdev;
  953. former_parent = ccw_device_is_orphan(cdev) ?
  954. NULL : to_subchannel(get_device(cdev->dev.parent));
  955. mutex_lock(&sch->reg_mutex);
  956. /* Try to move the ccw device to its new subchannel. */
  957. rc = device_move(&cdev->dev, &sch->dev);
  958. mutex_unlock(&sch->reg_mutex);
  959. if (rc) {
  960. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  961. "0.%x.%04x failed (ret=%d)!\n",
  962. cdev->private->dev_id.ssid,
  963. cdev->private->dev_id.devno, sch->schid.ssid,
  964. sch->schid.sch_no, rc);
  965. css_sch_device_unregister(sch);
  966. goto out;
  967. }
  968. if (former_parent) {
  969. spin_lock_irq(former_parent->lock);
  970. sch_set_cdev(former_parent, NULL);
  971. spin_unlock_irq(former_parent->lock);
  972. css_sch_device_unregister(former_parent);
  973. /* Reset intparm to zeroes. */
  974. former_parent->schib.pmcw.intparm = 0;
  975. cio_modify(former_parent);
  976. }
  977. sch_attach_device(sch, cdev);
  978. out:
  979. if (former_parent)
  980. put_device(&former_parent->dev);
  981. put_device(&cdev->dev);
  982. }
  983. static void io_subchannel_irq(struct subchannel *sch)
  984. {
  985. struct ccw_device *cdev;
  986. cdev = sch_get_cdev(sch);
  987. CIO_TRACE_EVENT(3, "IRQ");
  988. CIO_TRACE_EVENT(3, sch->dev.bus_id);
  989. if (cdev)
  990. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  991. }
  992. static void io_subchannel_init_fields(struct subchannel *sch)
  993. {
  994. if (cio_is_console(sch->schid))
  995. sch->opm = 0xff;
  996. else
  997. sch->opm = chp_get_sch_opm(sch);
  998. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  999. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  1000. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1001. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1002. sch->schib.pmcw.dev, sch->schid.ssid,
  1003. sch->schid.sch_no, sch->schib.pmcw.pim,
  1004. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1005. /* Initially set up some fields in the pmcw. */
  1006. sch->schib.pmcw.ena = 0;
  1007. sch->schib.pmcw.csense = 1; /* concurrent sense */
  1008. if ((sch->lpm & (sch->lpm - 1)) != 0)
  1009. sch->schib.pmcw.mp = 1; /* multipath mode */
  1010. /* clean up possible residual cmf stuff */
  1011. sch->schib.pmcw.mme = 0;
  1012. sch->schib.pmcw.mbfc = 0;
  1013. sch->schib.pmcw.mbi = 0;
  1014. sch->schib.mba = 0;
  1015. }
  1016. static int io_subchannel_probe(struct subchannel *sch)
  1017. {
  1018. struct ccw_device *cdev;
  1019. int rc;
  1020. unsigned long flags;
  1021. struct ccw_dev_id dev_id;
  1022. cdev = sch_get_cdev(sch);
  1023. if (cdev) {
  1024. rc = sysfs_create_group(&sch->dev.kobj,
  1025. &io_subchannel_attr_group);
  1026. if (rc)
  1027. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1028. "attributes for subchannel "
  1029. "0.%x.%04x (rc=%d)\n",
  1030. sch->schid.ssid, sch->schid.sch_no, rc);
  1031. /*
  1032. * This subchannel already has an associated ccw_device.
  1033. * Throw the delayed uevent for the subchannel, register
  1034. * the ccw_device and exit. This happens for all early
  1035. * devices, e.g. the console.
  1036. */
  1037. sch->dev.uevent_suppress = 0;
  1038. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1039. cdev->dev.groups = ccwdev_attr_groups;
  1040. device_initialize(&cdev->dev);
  1041. ccw_device_register(cdev);
  1042. /*
  1043. * Check if the device is already online. If it is
  1044. * the reference count needs to be corrected
  1045. * (see ccw_device_online and css_init_done for the
  1046. * ugly details).
  1047. */
  1048. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1049. cdev->private->state != DEV_STATE_OFFLINE &&
  1050. cdev->private->state != DEV_STATE_BOXED)
  1051. get_device(&cdev->dev);
  1052. return 0;
  1053. }
  1054. io_subchannel_init_fields(sch);
  1055. /*
  1056. * First check if a fitting device may be found amongst the
  1057. * disconnected devices or in the orphanage.
  1058. */
  1059. dev_id.devno = sch->schib.pmcw.dev;
  1060. dev_id.ssid = sch->schid.ssid;
  1061. rc = sysfs_create_group(&sch->dev.kobj,
  1062. &io_subchannel_attr_group);
  1063. if (rc)
  1064. return rc;
  1065. /* Allocate I/O subchannel private data. */
  1066. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1067. GFP_KERNEL | GFP_DMA);
  1068. if (!sch->private) {
  1069. rc = -ENOMEM;
  1070. goto out_err;
  1071. }
  1072. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1073. if (!cdev)
  1074. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1075. &dev_id);
  1076. if (cdev) {
  1077. /*
  1078. * Schedule moving the device until when we have a registered
  1079. * subchannel to move to and succeed the probe. We can
  1080. * unregister later again, when the probe is through.
  1081. */
  1082. cdev->private->sch = sch;
  1083. PREPARE_WORK(&cdev->private->kick_work,
  1084. ccw_device_move_to_sch);
  1085. queue_work(slow_path_wq, &cdev->private->kick_work);
  1086. return 0;
  1087. }
  1088. cdev = io_subchannel_create_ccwdev(sch);
  1089. if (IS_ERR(cdev)) {
  1090. rc = PTR_ERR(cdev);
  1091. goto out_err;
  1092. }
  1093. rc = io_subchannel_recog(cdev, sch);
  1094. if (rc) {
  1095. spin_lock_irqsave(sch->lock, flags);
  1096. sch_set_cdev(sch, NULL);
  1097. spin_unlock_irqrestore(sch->lock, flags);
  1098. if (cdev->dev.release)
  1099. cdev->dev.release(&cdev->dev);
  1100. goto out_err;
  1101. }
  1102. return 0;
  1103. out_err:
  1104. kfree(sch->private);
  1105. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1106. return rc;
  1107. }
  1108. static int
  1109. io_subchannel_remove (struct subchannel *sch)
  1110. {
  1111. struct ccw_device *cdev;
  1112. unsigned long flags;
  1113. cdev = sch_get_cdev(sch);
  1114. if (!cdev)
  1115. return 0;
  1116. /* Set ccw device to not operational and drop reference. */
  1117. spin_lock_irqsave(cdev->ccwlock, flags);
  1118. sch_set_cdev(sch, NULL);
  1119. cdev->private->state = DEV_STATE_NOT_OPER;
  1120. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1121. ccw_device_unregister(cdev);
  1122. put_device(&cdev->dev);
  1123. kfree(sch->private);
  1124. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1125. return 0;
  1126. }
  1127. static int io_subchannel_notify(struct subchannel *sch, int event)
  1128. {
  1129. struct ccw_device *cdev;
  1130. cdev = sch_get_cdev(sch);
  1131. if (!cdev)
  1132. return 0;
  1133. return ccw_device_notify(cdev, event);
  1134. }
  1135. static void io_subchannel_verify(struct subchannel *sch)
  1136. {
  1137. struct ccw_device *cdev;
  1138. cdev = sch_get_cdev(sch);
  1139. if (cdev)
  1140. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1141. }
  1142. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1143. {
  1144. int cc;
  1145. cc = stsch(sch->schid, &sch->schib);
  1146. if (cc)
  1147. return 0;
  1148. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1149. return 1;
  1150. return 0;
  1151. }
  1152. static void terminate_internal_io(struct subchannel *sch,
  1153. struct ccw_device *cdev)
  1154. {
  1155. if (cio_clear(sch)) {
  1156. /* Recheck device in case clear failed. */
  1157. sch->lpm = 0;
  1158. if (cdev->online)
  1159. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1160. else
  1161. css_schedule_eval(sch->schid);
  1162. return;
  1163. }
  1164. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1165. /* Request retry of internal operation. */
  1166. cdev->private->flags.intretry = 1;
  1167. /* Call handler. */
  1168. if (cdev->handler)
  1169. cdev->handler(cdev, cdev->private->intparm,
  1170. ERR_PTR(-EIO));
  1171. }
  1172. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1173. {
  1174. struct ccw_device *cdev;
  1175. cdev = sch_get_cdev(sch);
  1176. if (!cdev)
  1177. return;
  1178. if (check_for_io_on_path(sch, mask)) {
  1179. if (cdev->private->state == DEV_STATE_ONLINE)
  1180. ccw_device_kill_io(cdev);
  1181. else {
  1182. terminate_internal_io(sch, cdev);
  1183. /* Re-start path verification. */
  1184. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1185. }
  1186. } else
  1187. /* trigger path verification. */
  1188. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1189. }
  1190. static int io_subchannel_chp_event(struct subchannel *sch,
  1191. struct chp_link *link, int event)
  1192. {
  1193. int mask;
  1194. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1195. if (!mask)
  1196. return 0;
  1197. switch (event) {
  1198. case CHP_VARY_OFF:
  1199. sch->opm &= ~mask;
  1200. sch->lpm &= ~mask;
  1201. io_subchannel_terminate_path(sch, mask);
  1202. break;
  1203. case CHP_VARY_ON:
  1204. sch->opm |= mask;
  1205. sch->lpm |= mask;
  1206. io_subchannel_verify(sch);
  1207. break;
  1208. case CHP_OFFLINE:
  1209. if (stsch(sch->schid, &sch->schib))
  1210. return -ENXIO;
  1211. if (!css_sch_is_valid(&sch->schib))
  1212. return -ENODEV;
  1213. io_subchannel_terminate_path(sch, mask);
  1214. break;
  1215. case CHP_ONLINE:
  1216. if (stsch(sch->schid, &sch->schib))
  1217. return -ENXIO;
  1218. sch->lpm |= mask & sch->opm;
  1219. io_subchannel_verify(sch);
  1220. break;
  1221. }
  1222. return 0;
  1223. }
  1224. static void
  1225. io_subchannel_shutdown(struct subchannel *sch)
  1226. {
  1227. struct ccw_device *cdev;
  1228. int ret;
  1229. cdev = sch_get_cdev(sch);
  1230. if (cio_is_console(sch->schid))
  1231. return;
  1232. if (!sch->schib.pmcw.ena)
  1233. /* Nothing to do. */
  1234. return;
  1235. ret = cio_disable_subchannel(sch);
  1236. if (ret != -EBUSY)
  1237. /* Subchannel is disabled, we're done. */
  1238. return;
  1239. cdev->private->state = DEV_STATE_QUIESCE;
  1240. if (cdev->handler)
  1241. cdev->handler(cdev, cdev->private->intparm,
  1242. ERR_PTR(-EIO));
  1243. ret = ccw_device_cancel_halt_clear(cdev);
  1244. if (ret == -EBUSY) {
  1245. ccw_device_set_timeout(cdev, HZ/10);
  1246. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1247. }
  1248. cio_disable_subchannel(sch);
  1249. }
  1250. static int io_subchannel_get_status(struct subchannel *sch)
  1251. {
  1252. struct schib schib;
  1253. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1254. return CIO_GONE;
  1255. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1256. return CIO_REVALIDATE;
  1257. if (!sch->lpm)
  1258. return CIO_NO_PATH;
  1259. return CIO_OPER;
  1260. }
  1261. static int device_is_disconnected(struct ccw_device *cdev)
  1262. {
  1263. if (!cdev)
  1264. return 0;
  1265. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1266. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1267. }
  1268. static int recovery_check(struct device *dev, void *data)
  1269. {
  1270. struct ccw_device *cdev = to_ccwdev(dev);
  1271. int *redo = data;
  1272. spin_lock_irq(cdev->ccwlock);
  1273. switch (cdev->private->state) {
  1274. case DEV_STATE_DISCONNECTED:
  1275. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1276. cdev->private->dev_id.ssid,
  1277. cdev->private->dev_id.devno);
  1278. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1279. *redo = 1;
  1280. break;
  1281. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1282. *redo = 1;
  1283. break;
  1284. }
  1285. spin_unlock_irq(cdev->ccwlock);
  1286. return 0;
  1287. }
  1288. static void recovery_work_func(struct work_struct *unused)
  1289. {
  1290. int redo = 0;
  1291. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1292. if (redo) {
  1293. spin_lock_irq(&recovery_lock);
  1294. if (!timer_pending(&recovery_timer)) {
  1295. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1296. recovery_phase++;
  1297. mod_timer(&recovery_timer, jiffies +
  1298. recovery_delay[recovery_phase] * HZ);
  1299. }
  1300. spin_unlock_irq(&recovery_lock);
  1301. } else
  1302. CIO_MSG_EVENT(4, "recovery: end\n");
  1303. }
  1304. static DECLARE_WORK(recovery_work, recovery_work_func);
  1305. static void recovery_func(unsigned long data)
  1306. {
  1307. /*
  1308. * We can't do our recovery in softirq context and it's not
  1309. * performance critical, so we schedule it.
  1310. */
  1311. schedule_work(&recovery_work);
  1312. }
  1313. static void ccw_device_schedule_recovery(void)
  1314. {
  1315. unsigned long flags;
  1316. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1317. spin_lock_irqsave(&recovery_lock, flags);
  1318. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1319. recovery_phase = 0;
  1320. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1321. }
  1322. spin_unlock_irqrestore(&recovery_lock, flags);
  1323. }
  1324. static void device_set_disconnected(struct ccw_device *cdev)
  1325. {
  1326. if (!cdev)
  1327. return;
  1328. ccw_device_set_timeout(cdev, 0);
  1329. cdev->private->flags.fake_irb = 0;
  1330. cdev->private->state = DEV_STATE_DISCONNECTED;
  1331. if (cdev->online)
  1332. ccw_device_schedule_recovery();
  1333. }
  1334. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1335. {
  1336. int event, ret, disc;
  1337. unsigned long flags;
  1338. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
  1339. struct ccw_device *cdev;
  1340. spin_lock_irqsave(sch->lock, flags);
  1341. cdev = sch_get_cdev(sch);
  1342. disc = device_is_disconnected(cdev);
  1343. if (disc && slow) {
  1344. /* Disconnected devices are evaluated directly only.*/
  1345. spin_unlock_irqrestore(sch->lock, flags);
  1346. return 0;
  1347. }
  1348. /* No interrupt after machine check - kill pending timers. */
  1349. if (cdev)
  1350. ccw_device_set_timeout(cdev, 0);
  1351. if (!disc && !slow) {
  1352. /* Non-disconnected devices are evaluated on the slow path. */
  1353. spin_unlock_irqrestore(sch->lock, flags);
  1354. return -EAGAIN;
  1355. }
  1356. event = io_subchannel_get_status(sch);
  1357. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1358. sch->schid.ssid, sch->schid.sch_no, event,
  1359. disc ? "disconnected" : "normal",
  1360. slow ? "slow" : "fast");
  1361. /* Analyze subchannel status. */
  1362. action = NONE;
  1363. switch (event) {
  1364. case CIO_NO_PATH:
  1365. if (disc) {
  1366. /* Check if paths have become available. */
  1367. action = REPROBE;
  1368. break;
  1369. }
  1370. /* fall through */
  1371. case CIO_GONE:
  1372. /* Prevent unwanted effects when opening lock. */
  1373. cio_disable_subchannel(sch);
  1374. device_set_disconnected(cdev);
  1375. /* Ask driver what to do with device. */
  1376. action = UNREGISTER;
  1377. spin_unlock_irqrestore(sch->lock, flags);
  1378. ret = io_subchannel_notify(sch, event);
  1379. spin_lock_irqsave(sch->lock, flags);
  1380. if (ret)
  1381. action = NONE;
  1382. break;
  1383. case CIO_REVALIDATE:
  1384. /* Device will be removed, so no notify necessary. */
  1385. if (disc)
  1386. /* Reprobe because immediate unregister might block. */
  1387. action = REPROBE;
  1388. else
  1389. action = UNREGISTER_PROBE;
  1390. break;
  1391. case CIO_OPER:
  1392. if (disc)
  1393. /* Get device operational again. */
  1394. action = REPROBE;
  1395. break;
  1396. }
  1397. /* Perform action. */
  1398. ret = 0;
  1399. switch (action) {
  1400. case UNREGISTER:
  1401. case UNREGISTER_PROBE:
  1402. /* Unregister device (will use subchannel lock). */
  1403. spin_unlock_irqrestore(sch->lock, flags);
  1404. css_sch_device_unregister(sch);
  1405. spin_lock_irqsave(sch->lock, flags);
  1406. /* Reset intparm to zeroes. */
  1407. sch->schib.pmcw.intparm = 0;
  1408. cio_modify(sch);
  1409. break;
  1410. case REPROBE:
  1411. ccw_device_trigger_reprobe(cdev);
  1412. break;
  1413. default:
  1414. break;
  1415. }
  1416. spin_unlock_irqrestore(sch->lock, flags);
  1417. /* Probe if necessary. */
  1418. if (action == UNREGISTER_PROBE)
  1419. ret = css_probe_device(sch->schid);
  1420. return ret;
  1421. }
  1422. #ifdef CONFIG_CCW_CONSOLE
  1423. static struct ccw_device console_cdev;
  1424. static struct ccw_device_private console_private;
  1425. static int console_cdev_in_use;
  1426. static DEFINE_SPINLOCK(ccw_console_lock);
  1427. spinlock_t * cio_get_console_lock(void)
  1428. {
  1429. return &ccw_console_lock;
  1430. }
  1431. static int ccw_device_console_enable(struct ccw_device *cdev,
  1432. struct subchannel *sch)
  1433. {
  1434. int rc;
  1435. /* Attach subchannel private data. */
  1436. sch->private = cio_get_console_priv();
  1437. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1438. io_subchannel_init_fields(sch);
  1439. sch->driver = &io_subchannel_driver;
  1440. /* Initialize the ccw_device structure. */
  1441. cdev->dev.parent= &sch->dev;
  1442. rc = io_subchannel_recog(cdev, sch);
  1443. if (rc)
  1444. return rc;
  1445. /* Now wait for the async. recognition to come to an end. */
  1446. spin_lock_irq(cdev->ccwlock);
  1447. while (!dev_fsm_final_state(cdev))
  1448. wait_cons_dev();
  1449. rc = -EIO;
  1450. if (cdev->private->state != DEV_STATE_OFFLINE)
  1451. goto out_unlock;
  1452. ccw_device_online(cdev);
  1453. while (!dev_fsm_final_state(cdev))
  1454. wait_cons_dev();
  1455. if (cdev->private->state != DEV_STATE_ONLINE)
  1456. goto out_unlock;
  1457. rc = 0;
  1458. out_unlock:
  1459. spin_unlock_irq(cdev->ccwlock);
  1460. return 0;
  1461. }
  1462. struct ccw_device *
  1463. ccw_device_probe_console(void)
  1464. {
  1465. struct subchannel *sch;
  1466. int ret;
  1467. if (xchg(&console_cdev_in_use, 1) != 0)
  1468. return ERR_PTR(-EBUSY);
  1469. sch = cio_probe_console();
  1470. if (IS_ERR(sch)) {
  1471. console_cdev_in_use = 0;
  1472. return (void *) sch;
  1473. }
  1474. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1475. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1476. console_cdev.private = &console_private;
  1477. console_private.cdev = &console_cdev;
  1478. ret = ccw_device_console_enable(&console_cdev, sch);
  1479. if (ret) {
  1480. cio_release_console();
  1481. console_cdev_in_use = 0;
  1482. return ERR_PTR(ret);
  1483. }
  1484. console_cdev.online = 1;
  1485. return &console_cdev;
  1486. }
  1487. #endif
  1488. /*
  1489. * get ccw_device matching the busid, but only if owned by cdrv
  1490. */
  1491. static int
  1492. __ccwdev_check_busid(struct device *dev, void *id)
  1493. {
  1494. char *bus_id;
  1495. bus_id = id;
  1496. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1497. }
  1498. /**
  1499. * get_ccwdev_by_busid() - obtain device from a bus id
  1500. * @cdrv: driver the device is owned by
  1501. * @bus_id: bus id of the device to be searched
  1502. *
  1503. * This function searches all devices owned by @cdrv for a device with a bus
  1504. * id matching @bus_id.
  1505. * Returns:
  1506. * If a match is found, its reference count of the found device is increased
  1507. * and it is returned; else %NULL is returned.
  1508. */
  1509. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1510. const char *bus_id)
  1511. {
  1512. struct device *dev;
  1513. struct device_driver *drv;
  1514. drv = get_driver(&cdrv->driver);
  1515. if (!drv)
  1516. return NULL;
  1517. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1518. __ccwdev_check_busid);
  1519. put_driver(drv);
  1520. return dev ? to_ccwdev(dev) : NULL;
  1521. }
  1522. /************************** device driver handling ************************/
  1523. /* This is the implementation of the ccw_driver class. The probe, remove
  1524. * and release methods are initially very similar to the device_driver
  1525. * implementations, with the difference that they have ccw_device
  1526. * arguments.
  1527. *
  1528. * A ccw driver also contains the information that is needed for
  1529. * device matching.
  1530. */
  1531. static int
  1532. ccw_device_probe (struct device *dev)
  1533. {
  1534. struct ccw_device *cdev = to_ccwdev(dev);
  1535. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1536. int ret;
  1537. cdev->drv = cdrv; /* to let the driver call _set_online */
  1538. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1539. if (ret) {
  1540. cdev->drv = NULL;
  1541. return ret;
  1542. }
  1543. return 0;
  1544. }
  1545. static int
  1546. ccw_device_remove (struct device *dev)
  1547. {
  1548. struct ccw_device *cdev = to_ccwdev(dev);
  1549. struct ccw_driver *cdrv = cdev->drv;
  1550. int ret;
  1551. if (cdrv->remove)
  1552. cdrv->remove(cdev);
  1553. if (cdev->online) {
  1554. cdev->online = 0;
  1555. spin_lock_irq(cdev->ccwlock);
  1556. ret = ccw_device_offline(cdev);
  1557. spin_unlock_irq(cdev->ccwlock);
  1558. if (ret == 0)
  1559. wait_event(cdev->private->wait_q,
  1560. dev_fsm_final_state(cdev));
  1561. else
  1562. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1563. "device 0.%x.%04x\n",
  1564. ret, cdev->private->dev_id.ssid,
  1565. cdev->private->dev_id.devno);
  1566. }
  1567. ccw_device_set_timeout(cdev, 0);
  1568. cdev->drv = NULL;
  1569. return 0;
  1570. }
  1571. static void ccw_device_shutdown(struct device *dev)
  1572. {
  1573. struct ccw_device *cdev;
  1574. cdev = to_ccwdev(dev);
  1575. if (cdev->drv && cdev->drv->shutdown)
  1576. cdev->drv->shutdown(cdev);
  1577. disable_cmf(cdev);
  1578. }
  1579. struct bus_type ccw_bus_type = {
  1580. .name = "ccw",
  1581. .match = ccw_bus_match,
  1582. .uevent = ccw_uevent,
  1583. .probe = ccw_device_probe,
  1584. .remove = ccw_device_remove,
  1585. .shutdown = ccw_device_shutdown,
  1586. };
  1587. /**
  1588. * ccw_driver_register() - register a ccw driver
  1589. * @cdriver: driver to be registered
  1590. *
  1591. * This function is mainly a wrapper around driver_register().
  1592. * Returns:
  1593. * %0 on success and a negative error value on failure.
  1594. */
  1595. int ccw_driver_register(struct ccw_driver *cdriver)
  1596. {
  1597. struct device_driver *drv = &cdriver->driver;
  1598. drv->bus = &ccw_bus_type;
  1599. drv->name = cdriver->name;
  1600. drv->owner = cdriver->owner;
  1601. return driver_register(drv);
  1602. }
  1603. /**
  1604. * ccw_driver_unregister() - deregister a ccw driver
  1605. * @cdriver: driver to be deregistered
  1606. *
  1607. * This function is mainly a wrapper around driver_unregister().
  1608. */
  1609. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1610. {
  1611. driver_unregister(&cdriver->driver);
  1612. }
  1613. /* Helper func for qdio. */
  1614. struct subchannel_id
  1615. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1616. {
  1617. struct subchannel *sch;
  1618. sch = to_subchannel(cdev->dev.parent);
  1619. return sch->schid;
  1620. }
  1621. MODULE_LICENSE("GPL");
  1622. EXPORT_SYMBOL(ccw_device_set_online);
  1623. EXPORT_SYMBOL(ccw_device_set_offline);
  1624. EXPORT_SYMBOL(ccw_driver_register);
  1625. EXPORT_SYMBOL(ccw_driver_unregister);
  1626. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1627. EXPORT_SYMBOL(ccw_bus_type);
  1628. EXPORT_SYMBOL(ccw_device_work);
  1629. EXPORT_SYMBOL(ccw_device_notify_work);
  1630. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);