device.c 51 KB

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