device.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. // SPDX-License-Identifier: GPL-1.0+
  2. /*
  3. * bus driver for ccw devices
  4. *
  5. * Copyright IBM Corp. 2002, 2008
  6. * Author(s): Arnd Bergmann ([email protected])
  7. * Cornelia Huck ([email protected])
  8. * Martin Schwidefsky ([email protected])
  9. */
  10. #define KMSG_COMPONENT "cio"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/export.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/delay.h>
  22. #include <linux/timer.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/sched/signal.h>
  25. #include <linux/dma-mapping.h>
  26. #include <asm/ccwdev.h>
  27. #include <asm/cio.h>
  28. #include <asm/param.h> /* HZ */
  29. #include <asm/cmb.h>
  30. #include <asm/isc.h>
  31. #include "chp.h"
  32. #include "cio.h"
  33. #include "cio_debug.h"
  34. #include "css.h"
  35. #include "device.h"
  36. #include "ioasm.h"
  37. #include "io_sch.h"
  38. #include "blacklist.h"
  39. #include "chsc.h"
  40. static struct timer_list recovery_timer;
  41. static DEFINE_SPINLOCK(recovery_lock);
  42. static int recovery_phase;
  43. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  44. static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
  45. static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
  46. static struct bus_type ccw_bus_type;
  47. /******************* bus type handling ***********************/
  48. /* The Linux driver model distinguishes between a bus type and
  49. * the bus itself. Of course we only have one channel
  50. * subsystem driver and one channel system per machine, but
  51. * we still use the abstraction. T.R. says it's a good idea. */
  52. static int
  53. ccw_bus_match (struct device * dev, struct device_driver * drv)
  54. {
  55. struct ccw_device *cdev = to_ccwdev(dev);
  56. struct ccw_driver *cdrv = to_ccwdrv(drv);
  57. const struct ccw_device_id *ids = cdrv->ids, *found;
  58. if (!ids)
  59. return 0;
  60. found = ccw_device_id_match(ids, &cdev->id);
  61. if (!found)
  62. return 0;
  63. cdev->id.driver_info = found->driver_info;
  64. return 1;
  65. }
  66. /* Store modalias string delimited by prefix/suffix string into buffer with
  67. * specified size. Return length of resulting string (excluding trailing '\0')
  68. * even if string doesn't fit buffer (snprintf semantics). */
  69. static int snprint_alias(char *buf, size_t size,
  70. struct ccw_device_id *id, const char *suffix)
  71. {
  72. int len;
  73. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  74. if (len > size)
  75. return len;
  76. buf += len;
  77. size -= len;
  78. if (id->dev_type != 0)
  79. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  80. id->dev_model, suffix);
  81. else
  82. len += snprintf(buf, size, "dtdm%s", suffix);
  83. return len;
  84. }
  85. /* Set up environment variables for ccw device uevent. Return 0 on success,
  86. * non-zero otherwise. */
  87. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  88. {
  89. struct ccw_device *cdev = to_ccwdev(dev);
  90. struct ccw_device_id *id = &(cdev->id);
  91. int ret;
  92. char modalias_buf[30];
  93. /* CU_TYPE= */
  94. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  95. if (ret)
  96. return ret;
  97. /* CU_MODEL= */
  98. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  99. if (ret)
  100. return ret;
  101. /* The next two can be zero, that's ok for us */
  102. /* DEV_TYPE= */
  103. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  104. if (ret)
  105. return ret;
  106. /* DEV_MODEL= */
  107. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  108. if (ret)
  109. return ret;
  110. /* MODALIAS= */
  111. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  112. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  113. return ret;
  114. }
  115. static void io_subchannel_irq(struct subchannel *);
  116. static int io_subchannel_probe(struct subchannel *);
  117. static void io_subchannel_remove(struct subchannel *);
  118. static void io_subchannel_shutdown(struct subchannel *);
  119. static int io_subchannel_sch_event(struct subchannel *, int);
  120. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  121. int);
  122. static void recovery_func(struct timer_list *unused);
  123. static struct css_device_id io_subchannel_ids[] = {
  124. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  125. { /* end of list */ },
  126. };
  127. static int io_subchannel_settle(void)
  128. {
  129. int ret;
  130. ret = wait_event_interruptible(ccw_device_init_wq,
  131. atomic_read(&ccw_device_init_count) == 0);
  132. if (ret)
  133. return -EINTR;
  134. flush_workqueue(cio_work_q);
  135. return 0;
  136. }
  137. static struct css_driver io_subchannel_driver = {
  138. .drv = {
  139. .owner = THIS_MODULE,
  140. .name = "io_subchannel",
  141. },
  142. .subchannel_type = io_subchannel_ids,
  143. .irq = io_subchannel_irq,
  144. .sch_event = io_subchannel_sch_event,
  145. .chp_event = io_subchannel_chp_event,
  146. .probe = io_subchannel_probe,
  147. .remove = io_subchannel_remove,
  148. .shutdown = io_subchannel_shutdown,
  149. .settle = io_subchannel_settle,
  150. };
  151. int __init io_subchannel_init(void)
  152. {
  153. int ret;
  154. timer_setup(&recovery_timer, recovery_func, 0);
  155. ret = bus_register(&ccw_bus_type);
  156. if (ret)
  157. return ret;
  158. ret = css_driver_register(&io_subchannel_driver);
  159. if (ret)
  160. bus_unregister(&ccw_bus_type);
  161. return ret;
  162. }
  163. /************************ device handling **************************/
  164. static ssize_t
  165. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  166. {
  167. struct ccw_device *cdev = to_ccwdev(dev);
  168. struct ccw_device_id *id = &(cdev->id);
  169. if (id->dev_type != 0)
  170. return sprintf(buf, "%04x/%02x\n",
  171. id->dev_type, id->dev_model);
  172. else
  173. return sprintf(buf, "n/a\n");
  174. }
  175. static ssize_t
  176. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  177. {
  178. struct ccw_device *cdev = to_ccwdev(dev);
  179. struct ccw_device_id *id = &(cdev->id);
  180. return sprintf(buf, "%04x/%02x\n",
  181. id->cu_type, id->cu_model);
  182. }
  183. static ssize_t
  184. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  185. {
  186. struct ccw_device *cdev = to_ccwdev(dev);
  187. struct ccw_device_id *id = &(cdev->id);
  188. int len;
  189. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  190. return len > PAGE_SIZE ? PAGE_SIZE : len;
  191. }
  192. static ssize_t
  193. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  194. {
  195. struct ccw_device *cdev = to_ccwdev(dev);
  196. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  197. }
  198. int ccw_device_is_orphan(struct ccw_device *cdev)
  199. {
  200. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  201. }
  202. static void ccw_device_unregister(struct ccw_device *cdev)
  203. {
  204. if (device_is_registered(&cdev->dev)) {
  205. /* Undo device_add(). */
  206. device_del(&cdev->dev);
  207. }
  208. if (cdev->private->flags.initialized) {
  209. cdev->private->flags.initialized = 0;
  210. /* Release reference from device_initialize(). */
  211. put_device(&cdev->dev);
  212. }
  213. }
  214. static void io_subchannel_quiesce(struct subchannel *);
  215. /**
  216. * ccw_device_set_offline() - disable a ccw device for I/O
  217. * @cdev: target ccw device
  218. *
  219. * This function calls the driver's set_offline() function for @cdev, if
  220. * given, and then disables @cdev.
  221. * Returns:
  222. * %0 on success and a negative error value on failure.
  223. * Context:
  224. * enabled, ccw device lock not held
  225. */
  226. int ccw_device_set_offline(struct ccw_device *cdev)
  227. {
  228. struct subchannel *sch;
  229. int ret, state;
  230. if (!cdev)
  231. return -ENODEV;
  232. if (!cdev->online || !cdev->drv)
  233. return -EINVAL;
  234. if (cdev->drv->set_offline) {
  235. ret = cdev->drv->set_offline(cdev);
  236. if (ret != 0)
  237. return ret;
  238. }
  239. spin_lock_irq(cdev->ccwlock);
  240. sch = to_subchannel(cdev->dev.parent);
  241. cdev->online = 0;
  242. /* Wait until a final state or DISCONNECTED is reached */
  243. while (!dev_fsm_final_state(cdev) &&
  244. cdev->private->state != DEV_STATE_DISCONNECTED) {
  245. spin_unlock_irq(cdev->ccwlock);
  246. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  247. cdev->private->state == DEV_STATE_DISCONNECTED));
  248. spin_lock_irq(cdev->ccwlock);
  249. }
  250. do {
  251. ret = ccw_device_offline(cdev);
  252. if (!ret)
  253. break;
  254. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
  255. "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
  256. cdev->private->dev_id.devno);
  257. if (ret != -EBUSY)
  258. goto error;
  259. state = cdev->private->state;
  260. spin_unlock_irq(cdev->ccwlock);
  261. io_subchannel_quiesce(sch);
  262. spin_lock_irq(cdev->ccwlock);
  263. cdev->private->state = state;
  264. } while (ret == -EBUSY);
  265. spin_unlock_irq(cdev->ccwlock);
  266. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  267. cdev->private->state == DEV_STATE_DISCONNECTED));
  268. /* Inform the user if set offline failed. */
  269. if (cdev->private->state == DEV_STATE_BOXED) {
  270. pr_warn("%s: The device entered boxed state while being set offline\n",
  271. dev_name(&cdev->dev));
  272. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  273. pr_warn("%s: The device stopped operating while being set offline\n",
  274. dev_name(&cdev->dev));
  275. }
  276. /* Give up reference from ccw_device_set_online(). */
  277. put_device(&cdev->dev);
  278. return 0;
  279. error:
  280. cdev->private->state = DEV_STATE_OFFLINE;
  281. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  282. spin_unlock_irq(cdev->ccwlock);
  283. /* Give up reference from ccw_device_set_online(). */
  284. put_device(&cdev->dev);
  285. return -ENODEV;
  286. }
  287. /**
  288. * ccw_device_set_online() - enable a ccw device for I/O
  289. * @cdev: target ccw device
  290. *
  291. * This function first enables @cdev and then calls the driver's set_online()
  292. * function for @cdev, if given. If set_online() returns an error, @cdev is
  293. * disabled again.
  294. * Returns:
  295. * %0 on success and a negative error value on failure.
  296. * Context:
  297. * enabled, ccw device lock not held
  298. */
  299. int ccw_device_set_online(struct ccw_device *cdev)
  300. {
  301. int ret;
  302. int ret2;
  303. if (!cdev)
  304. return -ENODEV;
  305. if (cdev->online || !cdev->drv)
  306. return -EINVAL;
  307. /* Hold on to an extra reference while device is online. */
  308. if (!get_device(&cdev->dev))
  309. return -ENODEV;
  310. spin_lock_irq(cdev->ccwlock);
  311. ret = ccw_device_online(cdev);
  312. spin_unlock_irq(cdev->ccwlock);
  313. if (ret == 0)
  314. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  315. else {
  316. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  317. "device 0.%x.%04x\n",
  318. ret, cdev->private->dev_id.ssid,
  319. cdev->private->dev_id.devno);
  320. /* Give up online reference since onlining failed. */
  321. put_device(&cdev->dev);
  322. return ret;
  323. }
  324. spin_lock_irq(cdev->ccwlock);
  325. /* Check if online processing was successful */
  326. if ((cdev->private->state != DEV_STATE_ONLINE) &&
  327. (cdev->private->state != DEV_STATE_W4SENSE)) {
  328. spin_unlock_irq(cdev->ccwlock);
  329. /* Inform the user that set online failed. */
  330. if (cdev->private->state == DEV_STATE_BOXED) {
  331. pr_warn("%s: Setting the device online failed because it is boxed\n",
  332. dev_name(&cdev->dev));
  333. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  334. pr_warn("%s: Setting the device online failed because it is not operational\n",
  335. dev_name(&cdev->dev));
  336. }
  337. /* Give up online reference since onlining failed. */
  338. put_device(&cdev->dev);
  339. return -ENODEV;
  340. }
  341. spin_unlock_irq(cdev->ccwlock);
  342. if (cdev->drv->set_online)
  343. ret = cdev->drv->set_online(cdev);
  344. if (ret)
  345. goto rollback;
  346. spin_lock_irq(cdev->ccwlock);
  347. cdev->online = 1;
  348. spin_unlock_irq(cdev->ccwlock);
  349. return 0;
  350. rollback:
  351. spin_lock_irq(cdev->ccwlock);
  352. /* Wait until a final state or DISCONNECTED is reached */
  353. while (!dev_fsm_final_state(cdev) &&
  354. cdev->private->state != DEV_STATE_DISCONNECTED) {
  355. spin_unlock_irq(cdev->ccwlock);
  356. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  357. cdev->private->state == DEV_STATE_DISCONNECTED));
  358. spin_lock_irq(cdev->ccwlock);
  359. }
  360. ret2 = ccw_device_offline(cdev);
  361. if (ret2)
  362. goto error;
  363. spin_unlock_irq(cdev->ccwlock);
  364. wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
  365. cdev->private->state == DEV_STATE_DISCONNECTED));
  366. /* Give up online reference since onlining failed. */
  367. put_device(&cdev->dev);
  368. return ret;
  369. error:
  370. CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
  371. "device 0.%x.%04x\n",
  372. ret2, cdev->private->dev_id.ssid,
  373. cdev->private->dev_id.devno);
  374. cdev->private->state = DEV_STATE_OFFLINE;
  375. spin_unlock_irq(cdev->ccwlock);
  376. /* Give up online reference since onlining failed. */
  377. put_device(&cdev->dev);
  378. return ret;
  379. }
  380. static int online_store_handle_offline(struct ccw_device *cdev)
  381. {
  382. if (cdev->private->state == DEV_STATE_DISCONNECTED) {
  383. spin_lock_irq(cdev->ccwlock);
  384. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
  385. spin_unlock_irq(cdev->ccwlock);
  386. return 0;
  387. }
  388. if (cdev->drv && cdev->drv->set_offline)
  389. return ccw_device_set_offline(cdev);
  390. return -EINVAL;
  391. }
  392. static int online_store_recog_and_online(struct ccw_device *cdev)
  393. {
  394. /* Do device recognition, if needed. */
  395. if (cdev->private->state == DEV_STATE_BOXED) {
  396. spin_lock_irq(cdev->ccwlock);
  397. ccw_device_recognition(cdev);
  398. spin_unlock_irq(cdev->ccwlock);
  399. wait_event(cdev->private->wait_q,
  400. cdev->private->flags.recog_done);
  401. if (cdev->private->state != DEV_STATE_OFFLINE)
  402. /* recognition failed */
  403. return -EAGAIN;
  404. }
  405. if (cdev->drv && cdev->drv->set_online)
  406. return ccw_device_set_online(cdev);
  407. return -EINVAL;
  408. }
  409. static int online_store_handle_online(struct ccw_device *cdev, int force)
  410. {
  411. int ret;
  412. ret = online_store_recog_and_online(cdev);
  413. if (ret && !force)
  414. return ret;
  415. if (force && cdev->private->state == DEV_STATE_BOXED) {
  416. ret = ccw_device_stlck(cdev);
  417. if (ret)
  418. return ret;
  419. if (cdev->id.cu_type == 0)
  420. cdev->private->state = DEV_STATE_NOT_OPER;
  421. ret = online_store_recog_and_online(cdev);
  422. if (ret)
  423. return ret;
  424. }
  425. return 0;
  426. }
  427. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  428. const char *buf, size_t count)
  429. {
  430. struct ccw_device *cdev = to_ccwdev(dev);
  431. int force, ret;
  432. unsigned long i;
  433. /* Prevent conflict between multiple on-/offline processing requests. */
  434. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  435. return -EAGAIN;
  436. /* Prevent conflict between internal I/Os and on-/offline processing. */
  437. if (!dev_fsm_final_state(cdev) &&
  438. cdev->private->state != DEV_STATE_DISCONNECTED) {
  439. ret = -EAGAIN;
  440. goto out;
  441. }
  442. /* Prevent conflict between pending work and on-/offline processing.*/
  443. if (work_pending(&cdev->private->todo_work)) {
  444. ret = -EAGAIN;
  445. goto out;
  446. }
  447. if (!strncmp(buf, "force\n", count)) {
  448. force = 1;
  449. i = 1;
  450. ret = 0;
  451. } else {
  452. force = 0;
  453. ret = kstrtoul(buf, 16, &i);
  454. }
  455. if (ret)
  456. goto out;
  457. device_lock(dev);
  458. switch (i) {
  459. case 0:
  460. ret = online_store_handle_offline(cdev);
  461. break;
  462. case 1:
  463. ret = online_store_handle_online(cdev, force);
  464. break;
  465. default:
  466. ret = -EINVAL;
  467. }
  468. device_unlock(dev);
  469. out:
  470. atomic_set(&cdev->private->onoff, 0);
  471. return (ret < 0) ? ret : count;
  472. }
  473. static ssize_t
  474. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  475. {
  476. struct ccw_device *cdev = to_ccwdev(dev);
  477. struct subchannel *sch;
  478. if (ccw_device_is_orphan(cdev))
  479. return sprintf(buf, "no device\n");
  480. switch (cdev->private->state) {
  481. case DEV_STATE_BOXED:
  482. return sprintf(buf, "boxed\n");
  483. case DEV_STATE_DISCONNECTED:
  484. case DEV_STATE_DISCONNECTED_SENSE_ID:
  485. case DEV_STATE_NOT_OPER:
  486. sch = to_subchannel(dev->parent);
  487. if (!sch->lpm)
  488. return sprintf(buf, "no path\n");
  489. else
  490. return sprintf(buf, "no device\n");
  491. default:
  492. /* All other states considered fine. */
  493. return sprintf(buf, "good\n");
  494. }
  495. }
  496. static ssize_t
  497. initiate_logging(struct device *dev, struct device_attribute *attr,
  498. const char *buf, size_t count)
  499. {
  500. struct subchannel *sch = to_subchannel(dev);
  501. int rc;
  502. rc = chsc_siosl(sch->schid);
  503. if (rc < 0) {
  504. pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
  505. sch->schid.ssid, sch->schid.sch_no, rc);
  506. return rc;
  507. }
  508. pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
  509. sch->schid.ssid, sch->schid.sch_no);
  510. return count;
  511. }
  512. static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
  513. char *buf)
  514. {
  515. struct subchannel *sch = to_subchannel(dev);
  516. return sprintf(buf, "%02x\n", sch->vpm);
  517. }
  518. static DEVICE_ATTR_RO(devtype);
  519. static DEVICE_ATTR_RO(cutype);
  520. static DEVICE_ATTR_RO(modalias);
  521. static DEVICE_ATTR_RW(online);
  522. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  523. static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
  524. static DEVICE_ATTR_RO(vpm);
  525. static struct attribute *io_subchannel_attrs[] = {
  526. &dev_attr_logging.attr,
  527. &dev_attr_vpm.attr,
  528. NULL,
  529. };
  530. static const struct attribute_group io_subchannel_attr_group = {
  531. .attrs = io_subchannel_attrs,
  532. };
  533. static struct attribute * ccwdev_attrs[] = {
  534. &dev_attr_devtype.attr,
  535. &dev_attr_cutype.attr,
  536. &dev_attr_modalias.attr,
  537. &dev_attr_online.attr,
  538. &dev_attr_cmb_enable.attr,
  539. &dev_attr_availability.attr,
  540. NULL,
  541. };
  542. static const struct attribute_group ccwdev_attr_group = {
  543. .attrs = ccwdev_attrs,
  544. };
  545. static const struct attribute_group *ccwdev_attr_groups[] = {
  546. &ccwdev_attr_group,
  547. NULL,
  548. };
  549. static int match_dev_id(struct device *dev, const void *data)
  550. {
  551. struct ccw_device *cdev = to_ccwdev(dev);
  552. struct ccw_dev_id *dev_id = (void *)data;
  553. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  554. }
  555. /**
  556. * get_ccwdev_by_dev_id() - obtain device from a ccw device id
  557. * @dev_id: id of the device to be searched
  558. *
  559. * This function searches all devices attached to the ccw bus for a device
  560. * matching @dev_id.
  561. * Returns:
  562. * If a device is found its reference count is increased and returned;
  563. * else %NULL is returned.
  564. */
  565. struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
  566. {
  567. struct device *dev;
  568. dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
  569. return dev ? to_ccwdev(dev) : NULL;
  570. }
  571. EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
  572. static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
  573. {
  574. int ret;
  575. if (device_is_registered(&cdev->dev)) {
  576. device_release_driver(&cdev->dev);
  577. ret = device_attach(&cdev->dev);
  578. WARN_ON(ret == -ENODEV);
  579. }
  580. }
  581. static void
  582. ccw_device_release(struct device *dev)
  583. {
  584. struct ccw_device *cdev;
  585. cdev = to_ccwdev(dev);
  586. cio_gp_dma_free(cdev->private->dma_pool, cdev->private->dma_area,
  587. sizeof(*cdev->private->dma_area));
  588. cio_gp_dma_destroy(cdev->private->dma_pool, &cdev->dev);
  589. /* Release reference of parent subchannel. */
  590. put_device(cdev->dev.parent);
  591. kfree(cdev->private);
  592. kfree(cdev);
  593. }
  594. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  595. {
  596. struct ccw_device *cdev;
  597. struct gen_pool *dma_pool;
  598. int ret;
  599. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  600. if (!cdev) {
  601. ret = -ENOMEM;
  602. goto err_cdev;
  603. }
  604. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  605. GFP_KERNEL | GFP_DMA);
  606. if (!cdev->private) {
  607. ret = -ENOMEM;
  608. goto err_priv;
  609. }
  610. cdev->dev.dma_mask = sch->dev.dma_mask;
  611. ret = dma_set_coherent_mask(&cdev->dev, sch->dev.coherent_dma_mask);
  612. if (ret)
  613. goto err_coherent_mask;
  614. dma_pool = cio_gp_dma_create(&cdev->dev, 1);
  615. if (!dma_pool) {
  616. ret = -ENOMEM;
  617. goto err_dma_pool;
  618. }
  619. cdev->private->dma_pool = dma_pool;
  620. cdev->private->dma_area = cio_gp_dma_zalloc(dma_pool, &cdev->dev,
  621. sizeof(*cdev->private->dma_area));
  622. if (!cdev->private->dma_area) {
  623. ret = -ENOMEM;
  624. goto err_dma_area;
  625. }
  626. return cdev;
  627. err_dma_area:
  628. cio_gp_dma_destroy(dma_pool, &cdev->dev);
  629. err_dma_pool:
  630. err_coherent_mask:
  631. kfree(cdev->private);
  632. err_priv:
  633. kfree(cdev);
  634. err_cdev:
  635. return ERR_PTR(ret);
  636. }
  637. static void ccw_device_todo(struct work_struct *work);
  638. static int io_subchannel_initialize_dev(struct subchannel *sch,
  639. struct ccw_device *cdev)
  640. {
  641. struct ccw_device_private *priv = cdev->private;
  642. int ret;
  643. priv->cdev = cdev;
  644. priv->int_class = IRQIO_CIO;
  645. priv->state = DEV_STATE_NOT_OPER;
  646. priv->dev_id.devno = sch->schib.pmcw.dev;
  647. priv->dev_id.ssid = sch->schid.ssid;
  648. INIT_WORK(&priv->todo_work, ccw_device_todo);
  649. INIT_LIST_HEAD(&priv->cmb_list);
  650. init_waitqueue_head(&priv->wait_q);
  651. timer_setup(&priv->timer, ccw_device_timeout, 0);
  652. atomic_set(&priv->onoff, 0);
  653. cdev->ccwlock = sch->lock;
  654. cdev->dev.parent = &sch->dev;
  655. cdev->dev.release = ccw_device_release;
  656. cdev->dev.bus = &ccw_bus_type;
  657. cdev->dev.groups = ccwdev_attr_groups;
  658. /* Do first half of device_register. */
  659. device_initialize(&cdev->dev);
  660. ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
  661. cdev->private->dev_id.devno);
  662. if (ret)
  663. goto out_put;
  664. if (!get_device(&sch->dev)) {
  665. ret = -ENODEV;
  666. goto out_put;
  667. }
  668. priv->flags.initialized = 1;
  669. spin_lock_irq(sch->lock);
  670. sch_set_cdev(sch, cdev);
  671. spin_unlock_irq(sch->lock);
  672. return 0;
  673. out_put:
  674. /* Release reference from device_initialize(). */
  675. put_device(&cdev->dev);
  676. return ret;
  677. }
  678. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  679. {
  680. struct ccw_device *cdev;
  681. int ret;
  682. cdev = io_subchannel_allocate_dev(sch);
  683. if (!IS_ERR(cdev)) {
  684. ret = io_subchannel_initialize_dev(sch, cdev);
  685. if (ret)
  686. cdev = ERR_PTR(ret);
  687. }
  688. return cdev;
  689. }
  690. static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
  691. static void sch_create_and_recog_new_device(struct subchannel *sch)
  692. {
  693. struct ccw_device *cdev;
  694. /* Need to allocate a new ccw device. */
  695. cdev = io_subchannel_create_ccwdev(sch);
  696. if (IS_ERR(cdev)) {
  697. /* OK, we did everything we could... */
  698. css_sch_device_unregister(sch);
  699. return;
  700. }
  701. /* Start recognition for the new ccw device. */
  702. io_subchannel_recog(cdev, sch);
  703. }
  704. /*
  705. * Register recognized device.
  706. */
  707. static void io_subchannel_register(struct ccw_device *cdev)
  708. {
  709. struct subchannel *sch;
  710. int ret, adjust_init_count = 1;
  711. unsigned long flags;
  712. sch = to_subchannel(cdev->dev.parent);
  713. /*
  714. * Check if subchannel is still registered. It may have become
  715. * unregistered if a machine check hit us after finishing
  716. * device recognition but before the register work could be
  717. * queued.
  718. */
  719. if (!device_is_registered(&sch->dev))
  720. goto out_err;
  721. css_update_ssd_info(sch);
  722. /*
  723. * io_subchannel_register() will also be called after device
  724. * recognition has been done for a boxed device (which will already
  725. * be registered). We need to reprobe since we may now have sense id
  726. * information.
  727. */
  728. if (device_is_registered(&cdev->dev)) {
  729. if (!cdev->drv) {
  730. ret = device_reprobe(&cdev->dev);
  731. if (ret)
  732. /* We can't do much here. */
  733. CIO_MSG_EVENT(0, "device_reprobe() returned"
  734. " %d for 0.%x.%04x\n", ret,
  735. cdev->private->dev_id.ssid,
  736. cdev->private->dev_id.devno);
  737. }
  738. adjust_init_count = 0;
  739. goto out;
  740. }
  741. /* make it known to the system */
  742. ret = device_add(&cdev->dev);
  743. if (ret) {
  744. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  745. cdev->private->dev_id.ssid,
  746. cdev->private->dev_id.devno, ret);
  747. spin_lock_irqsave(sch->lock, flags);
  748. sch_set_cdev(sch, NULL);
  749. spin_unlock_irqrestore(sch->lock, flags);
  750. /* Release initial device reference. */
  751. put_device(&cdev->dev);
  752. goto out_err;
  753. }
  754. out:
  755. cdev->private->flags.recog_done = 1;
  756. wake_up(&cdev->private->wait_q);
  757. out_err:
  758. if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
  759. wake_up(&ccw_device_init_wq);
  760. }
  761. /*
  762. * subchannel recognition done. Called from the state machine.
  763. */
  764. void
  765. io_subchannel_recog_done(struct ccw_device *cdev)
  766. {
  767. if (css_init_done == 0) {
  768. cdev->private->flags.recog_done = 1;
  769. return;
  770. }
  771. switch (cdev->private->state) {
  772. case DEV_STATE_BOXED:
  773. /* Device did not respond in time. */
  774. case DEV_STATE_NOT_OPER:
  775. cdev->private->flags.recog_done = 1;
  776. /* Remove device found not operational. */
  777. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  778. if (atomic_dec_and_test(&ccw_device_init_count))
  779. wake_up(&ccw_device_init_wq);
  780. break;
  781. case DEV_STATE_OFFLINE:
  782. /*
  783. * We can't register the device in interrupt context so
  784. * we schedule a work item.
  785. */
  786. ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
  787. break;
  788. }
  789. }
  790. static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  791. {
  792. /* Increase counter of devices currently in recognition. */
  793. atomic_inc(&ccw_device_init_count);
  794. /* Start async. device sensing. */
  795. spin_lock_irq(sch->lock);
  796. ccw_device_recognition(cdev);
  797. spin_unlock_irq(sch->lock);
  798. }
  799. static int ccw_device_move_to_sch(struct ccw_device *cdev,
  800. struct subchannel *sch)
  801. {
  802. struct subchannel *old_sch;
  803. int rc, old_enabled = 0;
  804. old_sch = to_subchannel(cdev->dev.parent);
  805. /* Obtain child reference for new parent. */
  806. if (!get_device(&sch->dev))
  807. return -ENODEV;
  808. if (!sch_is_pseudo_sch(old_sch)) {
  809. spin_lock_irq(old_sch->lock);
  810. old_enabled = old_sch->schib.pmcw.ena;
  811. rc = 0;
  812. if (old_enabled)
  813. rc = cio_disable_subchannel(old_sch);
  814. spin_unlock_irq(old_sch->lock);
  815. if (rc == -EBUSY) {
  816. /* Release child reference for new parent. */
  817. put_device(&sch->dev);
  818. return rc;
  819. }
  820. }
  821. mutex_lock(&sch->reg_mutex);
  822. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  823. mutex_unlock(&sch->reg_mutex);
  824. if (rc) {
  825. CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
  826. cdev->private->dev_id.ssid,
  827. cdev->private->dev_id.devno, sch->schid.ssid,
  828. sch->schib.pmcw.dev, rc);
  829. if (old_enabled) {
  830. /* Try to reenable the old subchannel. */
  831. spin_lock_irq(old_sch->lock);
  832. cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
  833. spin_unlock_irq(old_sch->lock);
  834. }
  835. /* Release child reference for new parent. */
  836. put_device(&sch->dev);
  837. return rc;
  838. }
  839. /* Clean up old subchannel. */
  840. if (!sch_is_pseudo_sch(old_sch)) {
  841. spin_lock_irq(old_sch->lock);
  842. sch_set_cdev(old_sch, NULL);
  843. spin_unlock_irq(old_sch->lock);
  844. css_schedule_eval(old_sch->schid);
  845. }
  846. /* Release child reference for old parent. */
  847. put_device(&old_sch->dev);
  848. /* Initialize new subchannel. */
  849. spin_lock_irq(sch->lock);
  850. cdev->ccwlock = sch->lock;
  851. if (!sch_is_pseudo_sch(sch))
  852. sch_set_cdev(sch, cdev);
  853. spin_unlock_irq(sch->lock);
  854. if (!sch_is_pseudo_sch(sch))
  855. css_update_ssd_info(sch);
  856. return 0;
  857. }
  858. static int ccw_device_move_to_orph(struct ccw_device *cdev)
  859. {
  860. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  861. struct channel_subsystem *css = to_css(sch->dev.parent);
  862. return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
  863. }
  864. static void io_subchannel_irq(struct subchannel *sch)
  865. {
  866. struct ccw_device *cdev;
  867. cdev = sch_get_cdev(sch);
  868. CIO_TRACE_EVENT(6, "IRQ");
  869. CIO_TRACE_EVENT(6, dev_name(&sch->dev));
  870. if (cdev)
  871. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  872. else
  873. inc_irq_stat(IRQIO_CIO);
  874. }
  875. void io_subchannel_init_config(struct subchannel *sch)
  876. {
  877. memset(&sch->config, 0, sizeof(sch->config));
  878. sch->config.csense = 1;
  879. }
  880. static void io_subchannel_init_fields(struct subchannel *sch)
  881. {
  882. if (cio_is_console(sch->schid))
  883. sch->opm = 0xff;
  884. else
  885. sch->opm = chp_get_sch_opm(sch);
  886. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  887. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  888. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  889. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  890. sch->schib.pmcw.dev, sch->schid.ssid,
  891. sch->schid.sch_no, sch->schib.pmcw.pim,
  892. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  893. io_subchannel_init_config(sch);
  894. }
  895. /*
  896. * Note: We always return 0 so that we bind to the device even on error.
  897. * This is needed so that our remove function is called on unregister.
  898. */
  899. static int io_subchannel_probe(struct subchannel *sch)
  900. {
  901. struct io_subchannel_private *io_priv;
  902. struct ccw_device *cdev;
  903. int rc;
  904. if (cio_is_console(sch->schid)) {
  905. rc = sysfs_create_group(&sch->dev.kobj,
  906. &io_subchannel_attr_group);
  907. if (rc)
  908. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  909. "attributes for subchannel "
  910. "0.%x.%04x (rc=%d)\n",
  911. sch->schid.ssid, sch->schid.sch_no, rc);
  912. /*
  913. * The console subchannel already has an associated ccw_device.
  914. * Register it and exit.
  915. */
  916. cdev = sch_get_cdev(sch);
  917. rc = device_add(&cdev->dev);
  918. if (rc) {
  919. /* Release online reference. */
  920. put_device(&cdev->dev);
  921. goto out_schedule;
  922. }
  923. if (atomic_dec_and_test(&ccw_device_init_count))
  924. wake_up(&ccw_device_init_wq);
  925. return 0;
  926. }
  927. io_subchannel_init_fields(sch);
  928. rc = cio_commit_config(sch);
  929. if (rc)
  930. goto out_schedule;
  931. rc = sysfs_create_group(&sch->dev.kobj,
  932. &io_subchannel_attr_group);
  933. if (rc)
  934. goto out_schedule;
  935. /* Allocate I/O subchannel private data. */
  936. io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
  937. if (!io_priv)
  938. goto out_schedule;
  939. io_priv->dma_area = dma_alloc_coherent(&sch->dev,
  940. sizeof(*io_priv->dma_area),
  941. &io_priv->dma_area_dma, GFP_KERNEL);
  942. if (!io_priv->dma_area) {
  943. kfree(io_priv);
  944. goto out_schedule;
  945. }
  946. set_io_private(sch, io_priv);
  947. css_schedule_eval(sch->schid);
  948. return 0;
  949. out_schedule:
  950. spin_lock_irq(sch->lock);
  951. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  952. spin_unlock_irq(sch->lock);
  953. return 0;
  954. }
  955. static void io_subchannel_remove(struct subchannel *sch)
  956. {
  957. struct io_subchannel_private *io_priv = to_io_private(sch);
  958. struct ccw_device *cdev;
  959. cdev = sch_get_cdev(sch);
  960. if (!cdev)
  961. goto out_free;
  962. ccw_device_unregister(cdev);
  963. spin_lock_irq(sch->lock);
  964. sch_set_cdev(sch, NULL);
  965. set_io_private(sch, NULL);
  966. spin_unlock_irq(sch->lock);
  967. out_free:
  968. dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
  969. io_priv->dma_area, io_priv->dma_area_dma);
  970. kfree(io_priv);
  971. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  972. }
  973. static void io_subchannel_verify(struct subchannel *sch)
  974. {
  975. struct ccw_device *cdev;
  976. cdev = sch_get_cdev(sch);
  977. if (cdev)
  978. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  979. else
  980. css_schedule_eval(sch->schid);
  981. }
  982. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  983. {
  984. struct ccw_device *cdev;
  985. cdev = sch_get_cdev(sch);
  986. if (!cdev)
  987. return;
  988. if (cio_update_schib(sch))
  989. goto err;
  990. /* Check for I/O on path. */
  991. if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
  992. goto out;
  993. if (cdev->private->state == DEV_STATE_ONLINE) {
  994. ccw_device_kill_io(cdev);
  995. goto out;
  996. }
  997. if (cio_clear(sch))
  998. goto err;
  999. out:
  1000. /* Trigger path verification. */
  1001. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1002. return;
  1003. err:
  1004. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  1005. }
  1006. static int io_subchannel_chp_event(struct subchannel *sch,
  1007. struct chp_link *link, int event)
  1008. {
  1009. struct ccw_device *cdev = sch_get_cdev(sch);
  1010. int mask, chpid, valid_bit;
  1011. int path_event[8];
  1012. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1013. if (!mask)
  1014. return 0;
  1015. switch (event) {
  1016. case CHP_VARY_OFF:
  1017. sch->opm &= ~mask;
  1018. sch->lpm &= ~mask;
  1019. if (cdev)
  1020. cdev->private->path_gone_mask |= mask;
  1021. io_subchannel_terminate_path(sch, mask);
  1022. break;
  1023. case CHP_VARY_ON:
  1024. sch->opm |= mask;
  1025. sch->lpm |= mask;
  1026. if (cdev)
  1027. cdev->private->path_new_mask |= mask;
  1028. io_subchannel_verify(sch);
  1029. break;
  1030. case CHP_OFFLINE:
  1031. if (cio_update_schib(sch))
  1032. return -ENODEV;
  1033. if (cdev)
  1034. cdev->private->path_gone_mask |= mask;
  1035. io_subchannel_terminate_path(sch, mask);
  1036. break;
  1037. case CHP_ONLINE:
  1038. if (cio_update_schib(sch))
  1039. return -ENODEV;
  1040. sch->lpm |= mask & sch->opm;
  1041. if (cdev)
  1042. cdev->private->path_new_mask |= mask;
  1043. io_subchannel_verify(sch);
  1044. break;
  1045. case CHP_FCES_EVENT:
  1046. /* Forward Endpoint Security event */
  1047. for (chpid = 0, valid_bit = 0x80; chpid < 8; chpid++,
  1048. valid_bit >>= 1) {
  1049. if (mask & valid_bit)
  1050. path_event[chpid] = PE_PATH_FCES_EVENT;
  1051. else
  1052. path_event[chpid] = PE_NONE;
  1053. }
  1054. if (cdev && cdev->drv && cdev->drv->path_event)
  1055. cdev->drv->path_event(cdev, path_event);
  1056. break;
  1057. }
  1058. return 0;
  1059. }
  1060. static void io_subchannel_quiesce(struct subchannel *sch)
  1061. {
  1062. struct ccw_device *cdev;
  1063. int ret;
  1064. spin_lock_irq(sch->lock);
  1065. cdev = sch_get_cdev(sch);
  1066. if (cio_is_console(sch->schid))
  1067. goto out_unlock;
  1068. if (!sch->schib.pmcw.ena)
  1069. goto out_unlock;
  1070. ret = cio_disable_subchannel(sch);
  1071. if (ret != -EBUSY)
  1072. goto out_unlock;
  1073. if (cdev->handler)
  1074. cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
  1075. while (ret == -EBUSY) {
  1076. cdev->private->state = DEV_STATE_QUIESCE;
  1077. cdev->private->iretry = 255;
  1078. ret = ccw_device_cancel_halt_clear(cdev);
  1079. if (ret == -EBUSY) {
  1080. ccw_device_set_timeout(cdev, HZ/10);
  1081. spin_unlock_irq(sch->lock);
  1082. wait_event(cdev->private->wait_q,
  1083. cdev->private->state != DEV_STATE_QUIESCE);
  1084. spin_lock_irq(sch->lock);
  1085. }
  1086. ret = cio_disable_subchannel(sch);
  1087. }
  1088. out_unlock:
  1089. spin_unlock_irq(sch->lock);
  1090. }
  1091. static void io_subchannel_shutdown(struct subchannel *sch)
  1092. {
  1093. io_subchannel_quiesce(sch);
  1094. }
  1095. static int device_is_disconnected(struct ccw_device *cdev)
  1096. {
  1097. if (!cdev)
  1098. return 0;
  1099. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1100. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1101. }
  1102. static int recovery_check(struct device *dev, void *data)
  1103. {
  1104. struct ccw_device *cdev = to_ccwdev(dev);
  1105. struct subchannel *sch;
  1106. int *redo = data;
  1107. spin_lock_irq(cdev->ccwlock);
  1108. switch (cdev->private->state) {
  1109. case DEV_STATE_ONLINE:
  1110. sch = to_subchannel(cdev->dev.parent);
  1111. if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
  1112. break;
  1113. fallthrough;
  1114. case DEV_STATE_DISCONNECTED:
  1115. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1116. cdev->private->dev_id.ssid,
  1117. cdev->private->dev_id.devno);
  1118. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1119. *redo = 1;
  1120. break;
  1121. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1122. *redo = 1;
  1123. break;
  1124. }
  1125. spin_unlock_irq(cdev->ccwlock);
  1126. return 0;
  1127. }
  1128. static void recovery_work_func(struct work_struct *unused)
  1129. {
  1130. int redo = 0;
  1131. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1132. if (redo) {
  1133. spin_lock_irq(&recovery_lock);
  1134. if (!timer_pending(&recovery_timer)) {
  1135. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1136. recovery_phase++;
  1137. mod_timer(&recovery_timer, jiffies +
  1138. recovery_delay[recovery_phase] * HZ);
  1139. }
  1140. spin_unlock_irq(&recovery_lock);
  1141. } else
  1142. CIO_MSG_EVENT(3, "recovery: end\n");
  1143. }
  1144. static DECLARE_WORK(recovery_work, recovery_work_func);
  1145. static void recovery_func(struct timer_list *unused)
  1146. {
  1147. /*
  1148. * We can't do our recovery in softirq context and it's not
  1149. * performance critical, so we schedule it.
  1150. */
  1151. schedule_work(&recovery_work);
  1152. }
  1153. void ccw_device_schedule_recovery(void)
  1154. {
  1155. unsigned long flags;
  1156. CIO_MSG_EVENT(3, "recovery: schedule\n");
  1157. spin_lock_irqsave(&recovery_lock, flags);
  1158. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1159. recovery_phase = 0;
  1160. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1161. }
  1162. spin_unlock_irqrestore(&recovery_lock, flags);
  1163. }
  1164. static int purge_fn(struct device *dev, void *data)
  1165. {
  1166. struct ccw_device *cdev = to_ccwdev(dev);
  1167. struct ccw_dev_id *id = &cdev->private->dev_id;
  1168. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1169. spin_lock_irq(cdev->ccwlock);
  1170. if (is_blacklisted(id->ssid, id->devno) &&
  1171. (cdev->private->state == DEV_STATE_OFFLINE) &&
  1172. (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
  1173. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
  1174. id->devno);
  1175. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1176. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  1177. atomic_set(&cdev->private->onoff, 0);
  1178. }
  1179. spin_unlock_irq(cdev->ccwlock);
  1180. /* Abort loop in case of pending signal. */
  1181. if (signal_pending(current))
  1182. return -EINTR;
  1183. return 0;
  1184. }
  1185. /**
  1186. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1187. *
  1188. * Unregister all ccw devices that are offline and on the blacklist.
  1189. */
  1190. int ccw_purge_blacklisted(void)
  1191. {
  1192. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1193. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1194. return 0;
  1195. }
  1196. void ccw_device_set_disconnected(struct ccw_device *cdev)
  1197. {
  1198. if (!cdev)
  1199. return;
  1200. ccw_device_set_timeout(cdev, 0);
  1201. cdev->private->flags.fake_irb = 0;
  1202. cdev->private->state = DEV_STATE_DISCONNECTED;
  1203. if (cdev->online)
  1204. ccw_device_schedule_recovery();
  1205. }
  1206. void ccw_device_set_notoper(struct ccw_device *cdev)
  1207. {
  1208. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1209. CIO_TRACE_EVENT(2, "notoper");
  1210. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1211. ccw_device_set_timeout(cdev, 0);
  1212. cio_disable_subchannel(sch);
  1213. cdev->private->state = DEV_STATE_NOT_OPER;
  1214. }
  1215. enum io_sch_action {
  1216. IO_SCH_UNREG,
  1217. IO_SCH_ORPH_UNREG,
  1218. IO_SCH_UNREG_CDEV,
  1219. IO_SCH_ATTACH,
  1220. IO_SCH_UNREG_ATTACH,
  1221. IO_SCH_ORPH_ATTACH,
  1222. IO_SCH_REPROBE,
  1223. IO_SCH_VERIFY,
  1224. IO_SCH_DISC,
  1225. IO_SCH_NOP,
  1226. };
  1227. static enum io_sch_action sch_get_action(struct subchannel *sch)
  1228. {
  1229. struct ccw_device *cdev;
  1230. cdev = sch_get_cdev(sch);
  1231. if (cio_update_schib(sch)) {
  1232. /* Not operational. */
  1233. if (!cdev)
  1234. return IO_SCH_UNREG;
  1235. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1236. return IO_SCH_UNREG;
  1237. return IO_SCH_ORPH_UNREG;
  1238. }
  1239. /* Operational. */
  1240. if (!cdev)
  1241. return IO_SCH_ATTACH;
  1242. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1243. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1244. return IO_SCH_UNREG_ATTACH;
  1245. return IO_SCH_ORPH_ATTACH;
  1246. }
  1247. if ((sch->schib.pmcw.pam & sch->opm) == 0) {
  1248. if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
  1249. return IO_SCH_UNREG_CDEV;
  1250. return IO_SCH_DISC;
  1251. }
  1252. if (device_is_disconnected(cdev))
  1253. return IO_SCH_REPROBE;
  1254. if (cdev->online)
  1255. return IO_SCH_VERIFY;
  1256. if (cdev->private->state == DEV_STATE_NOT_OPER)
  1257. return IO_SCH_UNREG_ATTACH;
  1258. return IO_SCH_NOP;
  1259. }
  1260. /**
  1261. * io_subchannel_sch_event - process subchannel event
  1262. * @sch: subchannel
  1263. * @process: non-zero if function is called in process context
  1264. *
  1265. * An unspecified event occurred for this subchannel. Adjust data according
  1266. * to the current operational state of the subchannel and device. Return
  1267. * zero when the event has been handled sufficiently or -EAGAIN when this
  1268. * function should be called again in process context.
  1269. */
  1270. static int io_subchannel_sch_event(struct subchannel *sch, int process)
  1271. {
  1272. unsigned long flags;
  1273. struct ccw_device *cdev;
  1274. struct ccw_dev_id dev_id;
  1275. enum io_sch_action action;
  1276. int rc = -EAGAIN;
  1277. spin_lock_irqsave(sch->lock, flags);
  1278. if (!device_is_registered(&sch->dev))
  1279. goto out_unlock;
  1280. if (work_pending(&sch->todo_work))
  1281. goto out_unlock;
  1282. cdev = sch_get_cdev(sch);
  1283. if (cdev && work_pending(&cdev->private->todo_work))
  1284. goto out_unlock;
  1285. action = sch_get_action(sch);
  1286. CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
  1287. sch->schid.ssid, sch->schid.sch_no, process,
  1288. action);
  1289. /* Perform immediate actions while holding the lock. */
  1290. switch (action) {
  1291. case IO_SCH_REPROBE:
  1292. /* Trigger device recognition. */
  1293. ccw_device_trigger_reprobe(cdev);
  1294. rc = 0;
  1295. goto out_unlock;
  1296. case IO_SCH_VERIFY:
  1297. /* Trigger path verification. */
  1298. io_subchannel_verify(sch);
  1299. rc = 0;
  1300. goto out_unlock;
  1301. case IO_SCH_DISC:
  1302. ccw_device_set_disconnected(cdev);
  1303. rc = 0;
  1304. goto out_unlock;
  1305. case IO_SCH_ORPH_UNREG:
  1306. case IO_SCH_ORPH_ATTACH:
  1307. ccw_device_set_disconnected(cdev);
  1308. break;
  1309. case IO_SCH_UNREG_CDEV:
  1310. case IO_SCH_UNREG_ATTACH:
  1311. case IO_SCH_UNREG:
  1312. if (!cdev)
  1313. break;
  1314. if (cdev->private->state == DEV_STATE_SENSE_ID) {
  1315. /*
  1316. * Note: delayed work triggered by this event
  1317. * and repeated calls to sch_event are synchronized
  1318. * by the above check for work_pending(cdev).
  1319. */
  1320. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  1321. } else
  1322. ccw_device_set_notoper(cdev);
  1323. break;
  1324. case IO_SCH_NOP:
  1325. rc = 0;
  1326. goto out_unlock;
  1327. default:
  1328. break;
  1329. }
  1330. spin_unlock_irqrestore(sch->lock, flags);
  1331. /* All other actions require process context. */
  1332. if (!process)
  1333. goto out;
  1334. /* Handle attached ccw device. */
  1335. switch (action) {
  1336. case IO_SCH_ORPH_UNREG:
  1337. case IO_SCH_ORPH_ATTACH:
  1338. /* Move ccw device to orphanage. */
  1339. rc = ccw_device_move_to_orph(cdev);
  1340. if (rc)
  1341. goto out;
  1342. break;
  1343. case IO_SCH_UNREG_CDEV:
  1344. case IO_SCH_UNREG_ATTACH:
  1345. spin_lock_irqsave(sch->lock, flags);
  1346. sch_set_cdev(sch, NULL);
  1347. spin_unlock_irqrestore(sch->lock, flags);
  1348. /* Unregister ccw device. */
  1349. ccw_device_unregister(cdev);
  1350. break;
  1351. default:
  1352. break;
  1353. }
  1354. /* Handle subchannel. */
  1355. switch (action) {
  1356. case IO_SCH_ORPH_UNREG:
  1357. case IO_SCH_UNREG:
  1358. css_sch_device_unregister(sch);
  1359. break;
  1360. case IO_SCH_ORPH_ATTACH:
  1361. case IO_SCH_UNREG_ATTACH:
  1362. case IO_SCH_ATTACH:
  1363. dev_id.ssid = sch->schid.ssid;
  1364. dev_id.devno = sch->schib.pmcw.dev;
  1365. cdev = get_ccwdev_by_dev_id(&dev_id);
  1366. if (!cdev) {
  1367. sch_create_and_recog_new_device(sch);
  1368. break;
  1369. }
  1370. rc = ccw_device_move_to_sch(cdev, sch);
  1371. if (rc) {
  1372. /* Release reference from get_ccwdev_by_dev_id() */
  1373. put_device(&cdev->dev);
  1374. goto out;
  1375. }
  1376. spin_lock_irqsave(sch->lock, flags);
  1377. ccw_device_trigger_reprobe(cdev);
  1378. spin_unlock_irqrestore(sch->lock, flags);
  1379. /* Release reference from get_ccwdev_by_dev_id() */
  1380. put_device(&cdev->dev);
  1381. break;
  1382. default:
  1383. break;
  1384. }
  1385. return 0;
  1386. out_unlock:
  1387. spin_unlock_irqrestore(sch->lock, flags);
  1388. out:
  1389. return rc;
  1390. }
  1391. static void ccw_device_set_int_class(struct ccw_device *cdev)
  1392. {
  1393. struct ccw_driver *cdrv = cdev->drv;
  1394. /* Note: we interpret class 0 in this context as an uninitialized
  1395. * field since it translates to a non-I/O interrupt class. */
  1396. if (cdrv->int_class != 0)
  1397. cdev->private->int_class = cdrv->int_class;
  1398. else
  1399. cdev->private->int_class = IRQIO_CIO;
  1400. }
  1401. #ifdef CONFIG_CCW_CONSOLE
  1402. int __init ccw_device_enable_console(struct ccw_device *cdev)
  1403. {
  1404. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1405. int rc;
  1406. if (!cdev->drv || !cdev->handler)
  1407. return -EINVAL;
  1408. io_subchannel_init_fields(sch);
  1409. rc = cio_commit_config(sch);
  1410. if (rc)
  1411. return rc;
  1412. sch->driver = &io_subchannel_driver;
  1413. io_subchannel_recog(cdev, sch);
  1414. /* Now wait for the async. recognition to come to an end. */
  1415. spin_lock_irq(cdev->ccwlock);
  1416. while (!dev_fsm_final_state(cdev))
  1417. ccw_device_wait_idle(cdev);
  1418. /* Hold on to an extra reference while device is online. */
  1419. get_device(&cdev->dev);
  1420. rc = ccw_device_online(cdev);
  1421. if (rc)
  1422. goto out_unlock;
  1423. while (!dev_fsm_final_state(cdev))
  1424. ccw_device_wait_idle(cdev);
  1425. if (cdev->private->state == DEV_STATE_ONLINE)
  1426. cdev->online = 1;
  1427. else
  1428. rc = -EIO;
  1429. out_unlock:
  1430. spin_unlock_irq(cdev->ccwlock);
  1431. if (rc) /* Give up online reference since onlining failed. */
  1432. put_device(&cdev->dev);
  1433. return rc;
  1434. }
  1435. struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
  1436. {
  1437. struct io_subchannel_private *io_priv;
  1438. struct ccw_device *cdev;
  1439. struct subchannel *sch;
  1440. sch = cio_probe_console();
  1441. if (IS_ERR(sch))
  1442. return ERR_CAST(sch);
  1443. io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
  1444. if (!io_priv)
  1445. goto err_priv;
  1446. io_priv->dma_area = dma_alloc_coherent(&sch->dev,
  1447. sizeof(*io_priv->dma_area),
  1448. &io_priv->dma_area_dma, GFP_KERNEL);
  1449. if (!io_priv->dma_area)
  1450. goto err_dma_area;
  1451. set_io_private(sch, io_priv);
  1452. cdev = io_subchannel_create_ccwdev(sch);
  1453. if (IS_ERR(cdev)) {
  1454. dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
  1455. io_priv->dma_area, io_priv->dma_area_dma);
  1456. set_io_private(sch, NULL);
  1457. put_device(&sch->dev);
  1458. kfree(io_priv);
  1459. return cdev;
  1460. }
  1461. cdev->drv = drv;
  1462. ccw_device_set_int_class(cdev);
  1463. return cdev;
  1464. err_dma_area:
  1465. kfree(io_priv);
  1466. err_priv:
  1467. put_device(&sch->dev);
  1468. return ERR_PTR(-ENOMEM);
  1469. }
  1470. void __init ccw_device_destroy_console(struct ccw_device *cdev)
  1471. {
  1472. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1473. struct io_subchannel_private *io_priv = to_io_private(sch);
  1474. set_io_private(sch, NULL);
  1475. dma_free_coherent(&sch->dev, sizeof(*io_priv->dma_area),
  1476. io_priv->dma_area, io_priv->dma_area_dma);
  1477. put_device(&sch->dev);
  1478. put_device(&cdev->dev);
  1479. kfree(io_priv);
  1480. }
  1481. /**
  1482. * ccw_device_wait_idle() - busy wait for device to become idle
  1483. * @cdev: ccw device
  1484. *
  1485. * Poll until activity control is zero, that is, no function or data
  1486. * transfer is pending/active.
  1487. * Called with device lock being held.
  1488. */
  1489. void ccw_device_wait_idle(struct ccw_device *cdev)
  1490. {
  1491. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1492. while (1) {
  1493. cio_tsch(sch);
  1494. if (sch->schib.scsw.cmd.actl == 0)
  1495. break;
  1496. udelay(100);
  1497. }
  1498. }
  1499. #endif
  1500. /**
  1501. * get_ccwdev_by_busid() - obtain device from a bus id
  1502. * @cdrv: driver the device is owned by
  1503. * @bus_id: bus id of the device to be searched
  1504. *
  1505. * This function searches all devices owned by @cdrv for a device with a bus
  1506. * id matching @bus_id.
  1507. * Returns:
  1508. * If a match is found, its reference count of the found device is increased
  1509. * and it is returned; else %NULL is returned.
  1510. */
  1511. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1512. const char *bus_id)
  1513. {
  1514. struct device *dev;
  1515. dev = driver_find_device_by_name(&cdrv->driver, bus_id);
  1516. return dev ? to_ccwdev(dev) : NULL;
  1517. }
  1518. /************************** device driver handling ************************/
  1519. /* This is the implementation of the ccw_driver class. The probe, remove
  1520. * and release methods are initially very similar to the device_driver
  1521. * implementations, with the difference that they have ccw_device
  1522. * arguments.
  1523. *
  1524. * A ccw driver also contains the information that is needed for
  1525. * device matching.
  1526. */
  1527. static int
  1528. ccw_device_probe (struct device *dev)
  1529. {
  1530. struct ccw_device *cdev = to_ccwdev(dev);
  1531. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1532. int ret;
  1533. cdev->drv = cdrv; /* to let the driver call _set_online */
  1534. ccw_device_set_int_class(cdev);
  1535. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1536. if (ret) {
  1537. cdev->drv = NULL;
  1538. cdev->private->int_class = IRQIO_CIO;
  1539. return ret;
  1540. }
  1541. return 0;
  1542. }
  1543. static void ccw_device_remove(struct device *dev)
  1544. {
  1545. struct ccw_device *cdev = to_ccwdev(dev);
  1546. struct ccw_driver *cdrv = cdev->drv;
  1547. struct subchannel *sch;
  1548. int ret;
  1549. if (cdrv->remove)
  1550. cdrv->remove(cdev);
  1551. spin_lock_irq(cdev->ccwlock);
  1552. if (cdev->online) {
  1553. cdev->online = 0;
  1554. ret = ccw_device_offline(cdev);
  1555. spin_unlock_irq(cdev->ccwlock);
  1556. if (ret == 0)
  1557. wait_event(cdev->private->wait_q,
  1558. dev_fsm_final_state(cdev));
  1559. else
  1560. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1561. "device 0.%x.%04x\n",
  1562. ret, cdev->private->dev_id.ssid,
  1563. cdev->private->dev_id.devno);
  1564. /* Give up reference obtained in ccw_device_set_online(). */
  1565. put_device(&cdev->dev);
  1566. spin_lock_irq(cdev->ccwlock);
  1567. }
  1568. ccw_device_set_timeout(cdev, 0);
  1569. cdev->drv = NULL;
  1570. cdev->private->int_class = IRQIO_CIO;
  1571. sch = to_subchannel(cdev->dev.parent);
  1572. spin_unlock_irq(cdev->ccwlock);
  1573. io_subchannel_quiesce(sch);
  1574. __disable_cmf(cdev);
  1575. }
  1576. static void ccw_device_shutdown(struct device *dev)
  1577. {
  1578. struct ccw_device *cdev;
  1579. cdev = to_ccwdev(dev);
  1580. if (cdev->drv && cdev->drv->shutdown)
  1581. cdev->drv->shutdown(cdev);
  1582. __disable_cmf(cdev);
  1583. }
  1584. static struct bus_type ccw_bus_type = {
  1585. .name = "ccw",
  1586. .match = ccw_bus_match,
  1587. .uevent = ccw_uevent,
  1588. .probe = ccw_device_probe,
  1589. .remove = ccw_device_remove,
  1590. .shutdown = ccw_device_shutdown,
  1591. };
  1592. /**
  1593. * ccw_driver_register() - register a ccw driver
  1594. * @cdriver: driver to be registered
  1595. *
  1596. * This function is mainly a wrapper around driver_register().
  1597. * Returns:
  1598. * %0 on success and a negative error value on failure.
  1599. */
  1600. int ccw_driver_register(struct ccw_driver *cdriver)
  1601. {
  1602. struct device_driver *drv = &cdriver->driver;
  1603. drv->bus = &ccw_bus_type;
  1604. return driver_register(drv);
  1605. }
  1606. /**
  1607. * ccw_driver_unregister() - deregister a ccw driver
  1608. * @cdriver: driver to be deregistered
  1609. *
  1610. * This function is mainly a wrapper around driver_unregister().
  1611. */
  1612. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1613. {
  1614. driver_unregister(&cdriver->driver);
  1615. }
  1616. static void ccw_device_todo(struct work_struct *work)
  1617. {
  1618. struct ccw_device_private *priv;
  1619. struct ccw_device *cdev;
  1620. struct subchannel *sch;
  1621. enum cdev_todo todo;
  1622. priv = container_of(work, struct ccw_device_private, todo_work);
  1623. cdev = priv->cdev;
  1624. sch = to_subchannel(cdev->dev.parent);
  1625. /* Find out todo. */
  1626. spin_lock_irq(cdev->ccwlock);
  1627. todo = priv->todo;
  1628. priv->todo = CDEV_TODO_NOTHING;
  1629. CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
  1630. priv->dev_id.ssid, priv->dev_id.devno, todo);
  1631. spin_unlock_irq(cdev->ccwlock);
  1632. /* Perform todo. */
  1633. switch (todo) {
  1634. case CDEV_TODO_ENABLE_CMF:
  1635. cmf_reenable(cdev);
  1636. break;
  1637. case CDEV_TODO_REBIND:
  1638. ccw_device_do_unbind_bind(cdev);
  1639. break;
  1640. case CDEV_TODO_REGISTER:
  1641. io_subchannel_register(cdev);
  1642. break;
  1643. case CDEV_TODO_UNREG_EVAL:
  1644. if (!sch_is_pseudo_sch(sch))
  1645. css_schedule_eval(sch->schid);
  1646. fallthrough;
  1647. case CDEV_TODO_UNREG:
  1648. spin_lock_irq(sch->lock);
  1649. sch_set_cdev(sch, NULL);
  1650. spin_unlock_irq(sch->lock);
  1651. ccw_device_unregister(cdev);
  1652. break;
  1653. default:
  1654. break;
  1655. }
  1656. /* Release workqueue ref. */
  1657. put_device(&cdev->dev);
  1658. }
  1659. /**
  1660. * ccw_device_sched_todo - schedule ccw device operation
  1661. * @cdev: ccw device
  1662. * @todo: todo
  1663. *
  1664. * Schedule the operation identified by @todo to be performed on the slow path
  1665. * workqueue. Do nothing if another operation with higher priority is already
  1666. * scheduled. Needs to be called with ccwdev lock held.
  1667. */
  1668. void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
  1669. {
  1670. CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
  1671. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  1672. todo);
  1673. if (cdev->private->todo >= todo)
  1674. return;
  1675. cdev->private->todo = todo;
  1676. /* Get workqueue ref. */
  1677. if (!get_device(&cdev->dev))
  1678. return;
  1679. if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
  1680. /* Already queued, release workqueue ref. */
  1681. put_device(&cdev->dev);
  1682. }
  1683. }
  1684. /**
  1685. * ccw_device_siosl() - initiate logging
  1686. * @cdev: ccw device
  1687. *
  1688. * This function is used to invoke model-dependent logging within the channel
  1689. * subsystem.
  1690. */
  1691. int ccw_device_siosl(struct ccw_device *cdev)
  1692. {
  1693. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1694. return chsc_siosl(sch->schid);
  1695. }
  1696. EXPORT_SYMBOL_GPL(ccw_device_siosl);
  1697. EXPORT_SYMBOL(ccw_device_set_online);
  1698. EXPORT_SYMBOL(ccw_device_set_offline);
  1699. EXPORT_SYMBOL(ccw_driver_register);
  1700. EXPORT_SYMBOL(ccw_driver_unregister);
  1701. EXPORT_SYMBOL(get_ccwdev_by_busid);