device.c 50 KB

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