device.c 53 KB

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