device.c 51 KB

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