device.c 49 KB

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