device.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. */
  10. #define KMSG_COMPONENT "cio"
  11. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/errno.h>
  16. #include <linux/err.h>
  17. #include <linux/slab.h>
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/timer.h>
  22. #include <asm/ccwdev.h>
  23. #include <asm/cio.h>
  24. #include <asm/param.h> /* HZ */
  25. #include <asm/cmb.h>
  26. #include <asm/isc.h>
  27. #include "chp.h"
  28. #include "cio.h"
  29. #include "cio_debug.h"
  30. #include "css.h"
  31. #include "device.h"
  32. #include "ioasm.h"
  33. #include "io_sch.h"
  34. #include "blacklist.h"
  35. static struct timer_list recovery_timer;
  36. static DEFINE_SPINLOCK(recovery_lock);
  37. static int recovery_phase;
  38. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  39. /******************* bus type handling ***********************/
  40. /* The Linux driver model distinguishes between a bus type and
  41. * the bus itself. Of course we only have one channel
  42. * subsystem driver and one channel system per machine, but
  43. * we still use the abstraction. T.R. says it's a good idea. */
  44. static int
  45. ccw_bus_match (struct device * dev, struct device_driver * drv)
  46. {
  47. struct ccw_device *cdev = to_ccwdev(dev);
  48. struct ccw_driver *cdrv = to_ccwdrv(drv);
  49. const struct ccw_device_id *ids = cdrv->ids, *found;
  50. if (!ids)
  51. return 0;
  52. found = ccw_device_id_match(ids, &cdev->id);
  53. if (!found)
  54. return 0;
  55. cdev->id.driver_info = found->driver_info;
  56. return 1;
  57. }
  58. /* Store modalias string delimited by prefix/suffix string into buffer with
  59. * specified size. Return length of resulting string (excluding trailing '\0')
  60. * even if string doesn't fit buffer (snprintf semantics). */
  61. static int snprint_alias(char *buf, size_t size,
  62. struct ccw_device_id *id, const char *suffix)
  63. {
  64. int len;
  65. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  66. if (len > size)
  67. return len;
  68. buf += len;
  69. size -= len;
  70. if (id->dev_type != 0)
  71. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  72. id->dev_model, suffix);
  73. else
  74. len += snprintf(buf, size, "dtdm%s", suffix);
  75. return len;
  76. }
  77. /* Set up environment variables for ccw device uevent. Return 0 on success,
  78. * non-zero otherwise. */
  79. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  80. {
  81. struct ccw_device *cdev = to_ccwdev(dev);
  82. struct ccw_device_id *id = &(cdev->id);
  83. int ret;
  84. char modalias_buf[30];
  85. /* CU_TYPE= */
  86. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  87. if (ret)
  88. return ret;
  89. /* CU_MODEL= */
  90. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  91. if (ret)
  92. return ret;
  93. /* The next two can be zero, that's ok for us */
  94. /* DEV_TYPE= */
  95. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  96. if (ret)
  97. return ret;
  98. /* DEV_MODEL= */
  99. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  100. if (ret)
  101. return ret;
  102. /* MODALIAS= */
  103. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  104. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  105. return ret;
  106. }
  107. struct bus_type ccw_bus_type;
  108. static void io_subchannel_irq(struct subchannel *);
  109. static int io_subchannel_probe(struct subchannel *);
  110. static int io_subchannel_remove(struct subchannel *);
  111. static void io_subchannel_shutdown(struct subchannel *);
  112. static int io_subchannel_sch_event(struct subchannel *, int);
  113. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  114. int);
  115. static void recovery_func(unsigned long data);
  116. struct workqueue_struct *ccw_device_work;
  117. wait_queue_head_t ccw_device_init_wq;
  118. atomic_t ccw_device_init_count;
  119. static struct css_device_id io_subchannel_ids[] = {
  120. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  121. { /* end of list */ },
  122. };
  123. MODULE_DEVICE_TABLE(css, io_subchannel_ids);
  124. static int io_subchannel_prepare(struct subchannel *sch)
  125. {
  126. struct ccw_device *cdev;
  127. /*
  128. * Don't allow suspend while a ccw device registration
  129. * is still outstanding.
  130. */
  131. cdev = sch_get_cdev(sch);
  132. if (cdev && !device_is_registered(&cdev->dev))
  133. return -EAGAIN;
  134. return 0;
  135. }
  136. static void io_subchannel_settle(void)
  137. {
  138. wait_event(ccw_device_init_wq,
  139. atomic_read(&ccw_device_init_count) == 0);
  140. flush_workqueue(ccw_device_work);
  141. }
  142. static struct css_driver io_subchannel_driver = {
  143. .owner = THIS_MODULE,
  144. .subchannel_type = io_subchannel_ids,
  145. .name = "io_subchannel",
  146. .irq = io_subchannel_irq,
  147. .sch_event = io_subchannel_sch_event,
  148. .chp_event = io_subchannel_chp_event,
  149. .probe = io_subchannel_probe,
  150. .remove = io_subchannel_remove,
  151. .shutdown = io_subchannel_shutdown,
  152. .prepare = io_subchannel_prepare,
  153. .settle = io_subchannel_settle,
  154. };
  155. int __init io_subchannel_init(void)
  156. {
  157. int ret;
  158. init_waitqueue_head(&ccw_device_init_wq);
  159. atomic_set(&ccw_device_init_count, 0);
  160. setup_timer(&recovery_timer, recovery_func, 0);
  161. ccw_device_work = create_singlethread_workqueue("cio");
  162. if (!ccw_device_work)
  163. return -ENOMEM;
  164. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  165. if (!slow_path_wq) {
  166. ret = -ENOMEM;
  167. goto out_err;
  168. }
  169. if ((ret = bus_register (&ccw_bus_type)))
  170. goto out_err;
  171. ret = css_driver_register(&io_subchannel_driver);
  172. if (ret)
  173. goto out_err;
  174. return 0;
  175. out_err:
  176. if (ccw_device_work)
  177. destroy_workqueue(ccw_device_work);
  178. if (slow_path_wq)
  179. destroy_workqueue(slow_path_wq);
  180. return ret;
  181. }
  182. /************************ device handling **************************/
  183. /*
  184. * A ccw_device has some interfaces in sysfs in addition to the
  185. * standard ones.
  186. * The following entries are designed to export the information which
  187. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  188. * are obvious, so they don't have an entry :)
  189. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  190. */
  191. static ssize_t
  192. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  193. {
  194. struct subchannel *sch = to_subchannel(dev);
  195. struct chsc_ssd_info *ssd = &sch->ssd_info;
  196. ssize_t ret = 0;
  197. int chp;
  198. int mask;
  199. for (chp = 0; chp < 8; chp++) {
  200. mask = 0x80 >> chp;
  201. if (ssd->path_mask & mask)
  202. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  203. else
  204. ret += sprintf(buf + ret, "00 ");
  205. }
  206. ret += sprintf (buf+ret, "\n");
  207. return min((ssize_t)PAGE_SIZE, ret);
  208. }
  209. static ssize_t
  210. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  211. {
  212. struct subchannel *sch = to_subchannel(dev);
  213. struct pmcw *pmcw = &sch->schib.pmcw;
  214. return sprintf (buf, "%02x %02x %02x\n",
  215. pmcw->pim, pmcw->pam, pmcw->pom);
  216. }
  217. static ssize_t
  218. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  219. {
  220. struct ccw_device *cdev = to_ccwdev(dev);
  221. struct ccw_device_id *id = &(cdev->id);
  222. if (id->dev_type != 0)
  223. return sprintf(buf, "%04x/%02x\n",
  224. id->dev_type, id->dev_model);
  225. else
  226. return sprintf(buf, "n/a\n");
  227. }
  228. static ssize_t
  229. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  230. {
  231. struct ccw_device *cdev = to_ccwdev(dev);
  232. struct ccw_device_id *id = &(cdev->id);
  233. return sprintf(buf, "%04x/%02x\n",
  234. id->cu_type, id->cu_model);
  235. }
  236. static ssize_t
  237. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  238. {
  239. struct ccw_device *cdev = to_ccwdev(dev);
  240. struct ccw_device_id *id = &(cdev->id);
  241. int len;
  242. len = snprint_alias(buf, PAGE_SIZE, id, "\n");
  243. return len > PAGE_SIZE ? PAGE_SIZE : len;
  244. }
  245. static ssize_t
  246. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  247. {
  248. struct ccw_device *cdev = to_ccwdev(dev);
  249. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  250. }
  251. int ccw_device_is_orphan(struct ccw_device *cdev)
  252. {
  253. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  254. }
  255. static void ccw_device_unregister(struct ccw_device *cdev)
  256. {
  257. if (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 int check_for_io_on_path(struct subchannel *sch, int mask)
  949. {
  950. if (cio_update_schib(sch))
  951. return 0;
  952. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  953. return 1;
  954. return 0;
  955. }
  956. static void terminate_internal_io(struct subchannel *sch,
  957. struct ccw_device *cdev)
  958. {
  959. if (cio_clear(sch)) {
  960. /* Recheck device in case clear failed. */
  961. sch->lpm = 0;
  962. if (cdev->online)
  963. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  964. else
  965. css_schedule_eval(sch->schid);
  966. return;
  967. }
  968. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  969. /* Request retry of internal operation. */
  970. cdev->private->flags.intretry = 1;
  971. /* Call handler. */
  972. if (cdev->handler)
  973. cdev->handler(cdev, cdev->private->intparm,
  974. ERR_PTR(-EIO));
  975. }
  976. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  977. {
  978. struct ccw_device *cdev;
  979. cdev = sch_get_cdev(sch);
  980. if (!cdev)
  981. return;
  982. if (check_for_io_on_path(sch, mask)) {
  983. if (cdev->private->state == DEV_STATE_ONLINE)
  984. ccw_device_kill_io(cdev);
  985. else {
  986. terminate_internal_io(sch, cdev);
  987. /* Re-start path verification. */
  988. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  989. }
  990. } else
  991. /* trigger path verification. */
  992. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  993. }
  994. static int io_subchannel_chp_event(struct subchannel *sch,
  995. struct chp_link *link, int event)
  996. {
  997. int mask;
  998. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  999. if (!mask)
  1000. return 0;
  1001. switch (event) {
  1002. case CHP_VARY_OFF:
  1003. sch->opm &= ~mask;
  1004. sch->lpm &= ~mask;
  1005. io_subchannel_terminate_path(sch, mask);
  1006. break;
  1007. case CHP_VARY_ON:
  1008. sch->opm |= mask;
  1009. sch->lpm |= mask;
  1010. io_subchannel_verify(sch);
  1011. break;
  1012. case CHP_OFFLINE:
  1013. if (cio_update_schib(sch))
  1014. return -ENODEV;
  1015. io_subchannel_terminate_path(sch, mask);
  1016. break;
  1017. case CHP_ONLINE:
  1018. if (cio_update_schib(sch))
  1019. return -ENODEV;
  1020. sch->lpm |= mask & sch->opm;
  1021. io_subchannel_verify(sch);
  1022. break;
  1023. }
  1024. return 0;
  1025. }
  1026. static void
  1027. io_subchannel_shutdown(struct subchannel *sch)
  1028. {
  1029. struct ccw_device *cdev;
  1030. int ret;
  1031. cdev = sch_get_cdev(sch);
  1032. if (cio_is_console(sch->schid))
  1033. return;
  1034. if (!sch->schib.pmcw.ena)
  1035. /* Nothing to do. */
  1036. return;
  1037. ret = cio_disable_subchannel(sch);
  1038. if (ret != -EBUSY)
  1039. /* Subchannel is disabled, we're done. */
  1040. return;
  1041. cdev->private->state = DEV_STATE_QUIESCE;
  1042. if (cdev->handler)
  1043. cdev->handler(cdev, cdev->private->intparm,
  1044. ERR_PTR(-EIO));
  1045. ret = ccw_device_cancel_halt_clear(cdev);
  1046. if (ret == -EBUSY) {
  1047. ccw_device_set_timeout(cdev, HZ/10);
  1048. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1049. }
  1050. cio_disable_subchannel(sch);
  1051. }
  1052. static int device_is_disconnected(struct ccw_device *cdev)
  1053. {
  1054. if (!cdev)
  1055. return 0;
  1056. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1057. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1058. }
  1059. static int recovery_check(struct device *dev, void *data)
  1060. {
  1061. struct ccw_device *cdev = to_ccwdev(dev);
  1062. int *redo = data;
  1063. spin_lock_irq(cdev->ccwlock);
  1064. switch (cdev->private->state) {
  1065. case DEV_STATE_DISCONNECTED:
  1066. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1067. cdev->private->dev_id.ssid,
  1068. cdev->private->dev_id.devno);
  1069. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1070. *redo = 1;
  1071. break;
  1072. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1073. *redo = 1;
  1074. break;
  1075. }
  1076. spin_unlock_irq(cdev->ccwlock);
  1077. return 0;
  1078. }
  1079. static void recovery_work_func(struct work_struct *unused)
  1080. {
  1081. int redo = 0;
  1082. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1083. if (redo) {
  1084. spin_lock_irq(&recovery_lock);
  1085. if (!timer_pending(&recovery_timer)) {
  1086. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1087. recovery_phase++;
  1088. mod_timer(&recovery_timer, jiffies +
  1089. recovery_delay[recovery_phase] * HZ);
  1090. }
  1091. spin_unlock_irq(&recovery_lock);
  1092. } else
  1093. CIO_MSG_EVENT(4, "recovery: end\n");
  1094. }
  1095. static DECLARE_WORK(recovery_work, recovery_work_func);
  1096. static void recovery_func(unsigned long data)
  1097. {
  1098. /*
  1099. * We can't do our recovery in softirq context and it's not
  1100. * performance critical, so we schedule it.
  1101. */
  1102. schedule_work(&recovery_work);
  1103. }
  1104. static void ccw_device_schedule_recovery(void)
  1105. {
  1106. unsigned long flags;
  1107. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1108. spin_lock_irqsave(&recovery_lock, flags);
  1109. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1110. recovery_phase = 0;
  1111. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1112. }
  1113. spin_unlock_irqrestore(&recovery_lock, flags);
  1114. }
  1115. static int purge_fn(struct device *dev, void *data)
  1116. {
  1117. struct ccw_device *cdev = to_ccwdev(dev);
  1118. struct ccw_dev_id *id = &cdev->private->dev_id;
  1119. spin_lock_irq(cdev->ccwlock);
  1120. if (is_blacklisted(id->ssid, id->devno) &&
  1121. (cdev->private->state == DEV_STATE_OFFLINE)) {
  1122. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
  1123. id->devno);
  1124. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1125. }
  1126. spin_unlock_irq(cdev->ccwlock);
  1127. /* Abort loop in case of pending signal. */
  1128. if (signal_pending(current))
  1129. return -EINTR;
  1130. return 0;
  1131. }
  1132. /**
  1133. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1134. *
  1135. * Unregister all ccw devices that are offline and on the blacklist.
  1136. */
  1137. int ccw_purge_blacklisted(void)
  1138. {
  1139. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1140. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1141. return 0;
  1142. }
  1143. void ccw_device_set_disconnected(struct ccw_device *cdev)
  1144. {
  1145. if (!cdev)
  1146. return;
  1147. ccw_device_set_timeout(cdev, 0);
  1148. cdev->private->flags.fake_irb = 0;
  1149. cdev->private->state = DEV_STATE_DISCONNECTED;
  1150. if (cdev->online)
  1151. ccw_device_schedule_recovery();
  1152. }
  1153. void ccw_device_set_notoper(struct ccw_device *cdev)
  1154. {
  1155. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1156. CIO_TRACE_EVENT(2, "notoper");
  1157. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1158. ccw_device_set_timeout(cdev, 0);
  1159. cio_disable_subchannel(sch);
  1160. cdev->private->state = DEV_STATE_NOT_OPER;
  1161. }
  1162. enum io_sch_action {
  1163. IO_SCH_UNREG,
  1164. IO_SCH_ORPH_UNREG,
  1165. IO_SCH_ATTACH,
  1166. IO_SCH_UNREG_ATTACH,
  1167. IO_SCH_ORPH_ATTACH,
  1168. IO_SCH_REPROBE,
  1169. IO_SCH_VERIFY,
  1170. IO_SCH_DISC,
  1171. IO_SCH_NOP,
  1172. };
  1173. static enum io_sch_action sch_get_action(struct subchannel *sch)
  1174. {
  1175. struct ccw_device *cdev;
  1176. cdev = sch_get_cdev(sch);
  1177. if (cio_update_schib(sch)) {
  1178. /* Not operational. */
  1179. if (!cdev)
  1180. return IO_SCH_UNREG;
  1181. if (!ccw_device_notify(cdev, CIO_GONE))
  1182. return IO_SCH_UNREG;
  1183. return IO_SCH_ORPH_UNREG;
  1184. }
  1185. /* Operational. */
  1186. if (!cdev)
  1187. return IO_SCH_ATTACH;
  1188. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1189. if (!ccw_device_notify(cdev, CIO_GONE))
  1190. return IO_SCH_UNREG_ATTACH;
  1191. return IO_SCH_ORPH_ATTACH;
  1192. }
  1193. if ((sch->schib.pmcw.pam & sch->opm) == 0) {
  1194. if (!ccw_device_notify(cdev, CIO_NO_PATH))
  1195. return IO_SCH_UNREG;
  1196. return IO_SCH_DISC;
  1197. }
  1198. if (device_is_disconnected(cdev))
  1199. return IO_SCH_REPROBE;
  1200. if (cdev->online)
  1201. return IO_SCH_VERIFY;
  1202. return IO_SCH_NOP;
  1203. }
  1204. /**
  1205. * io_subchannel_sch_event - process subchannel event
  1206. * @sch: subchannel
  1207. * @process: non-zero if function is called in process context
  1208. *
  1209. * An unspecified event occurred for this subchannel. Adjust data according
  1210. * to the current operational state of the subchannel and device. Return
  1211. * zero when the event has been handled sufficiently or -EAGAIN when this
  1212. * function should be called again in process context.
  1213. */
  1214. static int io_subchannel_sch_event(struct subchannel *sch, int process)
  1215. {
  1216. unsigned long flags;
  1217. struct ccw_device *cdev;
  1218. struct ccw_dev_id dev_id;
  1219. enum io_sch_action action;
  1220. int rc = -EAGAIN;
  1221. spin_lock_irqsave(sch->lock, flags);
  1222. if (!device_is_registered(&sch->dev))
  1223. goto out_unlock;
  1224. if (work_pending(&sch->todo_work))
  1225. goto out_unlock;
  1226. cdev = sch_get_cdev(sch);
  1227. if (cdev && work_pending(&cdev->private->todo_work))
  1228. goto out_unlock;
  1229. action = sch_get_action(sch);
  1230. CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
  1231. sch->schid.ssid, sch->schid.sch_no, process,
  1232. action);
  1233. /* Perform immediate actions while holding the lock. */
  1234. switch (action) {
  1235. case IO_SCH_REPROBE:
  1236. /* Trigger device recognition. */
  1237. ccw_device_trigger_reprobe(cdev);
  1238. rc = 0;
  1239. goto out_unlock;
  1240. case IO_SCH_VERIFY:
  1241. /* Trigger path verification. */
  1242. io_subchannel_verify(sch);
  1243. rc = 0;
  1244. goto out_unlock;
  1245. case IO_SCH_DISC:
  1246. ccw_device_set_disconnected(cdev);
  1247. rc = 0;
  1248. goto out_unlock;
  1249. case IO_SCH_ORPH_UNREG:
  1250. case IO_SCH_ORPH_ATTACH:
  1251. ccw_device_set_disconnected(cdev);
  1252. break;
  1253. case IO_SCH_UNREG_ATTACH:
  1254. case IO_SCH_UNREG:
  1255. if (cdev)
  1256. ccw_device_set_notoper(cdev);
  1257. break;
  1258. case IO_SCH_NOP:
  1259. rc = 0;
  1260. goto out_unlock;
  1261. default:
  1262. break;
  1263. }
  1264. spin_unlock_irqrestore(sch->lock, flags);
  1265. /* All other actions require process context. */
  1266. if (!process)
  1267. goto out;
  1268. /* Handle attached ccw device. */
  1269. switch (action) {
  1270. case IO_SCH_ORPH_UNREG:
  1271. case IO_SCH_ORPH_ATTACH:
  1272. /* Move ccw device to orphanage. */
  1273. rc = ccw_device_move_to_orph(cdev);
  1274. if (rc)
  1275. goto out;
  1276. break;
  1277. case IO_SCH_UNREG_ATTACH:
  1278. /* Unregister ccw device. */
  1279. ccw_device_unregister(cdev);
  1280. break;
  1281. default:
  1282. break;
  1283. }
  1284. /* Handle subchannel. */
  1285. switch (action) {
  1286. case IO_SCH_ORPH_UNREG:
  1287. case IO_SCH_UNREG:
  1288. css_sch_device_unregister(sch);
  1289. break;
  1290. case IO_SCH_ORPH_ATTACH:
  1291. case IO_SCH_UNREG_ATTACH:
  1292. case IO_SCH_ATTACH:
  1293. dev_id.ssid = sch->schid.ssid;
  1294. dev_id.devno = sch->schib.pmcw.dev;
  1295. cdev = get_ccwdev_by_dev_id(&dev_id);
  1296. if (!cdev) {
  1297. sch_create_and_recog_new_device(sch);
  1298. break;
  1299. }
  1300. rc = ccw_device_move_to_sch(cdev, sch);
  1301. if (rc) {
  1302. /* Release reference from get_ccwdev_by_dev_id() */
  1303. put_device(&cdev->dev);
  1304. goto out;
  1305. }
  1306. spin_lock_irqsave(sch->lock, flags);
  1307. ccw_device_trigger_reprobe(cdev);
  1308. spin_unlock_irqrestore(sch->lock, flags);
  1309. /* Release reference from get_ccwdev_by_dev_id() */
  1310. put_device(&cdev->dev);
  1311. break;
  1312. default:
  1313. break;
  1314. }
  1315. return 0;
  1316. out_unlock:
  1317. spin_unlock_irqrestore(sch->lock, flags);
  1318. out:
  1319. return rc;
  1320. }
  1321. #ifdef CONFIG_CCW_CONSOLE
  1322. static struct ccw_device console_cdev;
  1323. static struct ccw_device_private console_private;
  1324. static int console_cdev_in_use;
  1325. static DEFINE_SPINLOCK(ccw_console_lock);
  1326. spinlock_t * cio_get_console_lock(void)
  1327. {
  1328. return &ccw_console_lock;
  1329. }
  1330. static int ccw_device_console_enable(struct ccw_device *cdev,
  1331. struct subchannel *sch)
  1332. {
  1333. int rc;
  1334. /* Attach subchannel private data. */
  1335. sch->private = cio_get_console_priv();
  1336. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1337. io_subchannel_init_fields(sch);
  1338. rc = cio_commit_config(sch);
  1339. if (rc)
  1340. return rc;
  1341. sch->driver = &io_subchannel_driver;
  1342. /* Initialize the ccw_device structure. */
  1343. cdev->dev.parent= &sch->dev;
  1344. io_subchannel_recog(cdev, sch);
  1345. /* Now wait for the async. recognition to come to an end. */
  1346. spin_lock_irq(cdev->ccwlock);
  1347. while (!dev_fsm_final_state(cdev))
  1348. wait_cons_dev();
  1349. rc = -EIO;
  1350. if (cdev->private->state != DEV_STATE_OFFLINE)
  1351. goto out_unlock;
  1352. ccw_device_online(cdev);
  1353. while (!dev_fsm_final_state(cdev))
  1354. wait_cons_dev();
  1355. if (cdev->private->state != DEV_STATE_ONLINE)
  1356. goto out_unlock;
  1357. rc = 0;
  1358. out_unlock:
  1359. spin_unlock_irq(cdev->ccwlock);
  1360. return rc;
  1361. }
  1362. struct ccw_device *
  1363. ccw_device_probe_console(void)
  1364. {
  1365. struct subchannel *sch;
  1366. int ret;
  1367. if (xchg(&console_cdev_in_use, 1) != 0)
  1368. return ERR_PTR(-EBUSY);
  1369. sch = cio_probe_console();
  1370. if (IS_ERR(sch)) {
  1371. console_cdev_in_use = 0;
  1372. return (void *) sch;
  1373. }
  1374. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1375. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1376. console_cdev.private = &console_private;
  1377. console_private.cdev = &console_cdev;
  1378. ret = ccw_device_console_enable(&console_cdev, sch);
  1379. if (ret) {
  1380. cio_release_console();
  1381. console_cdev_in_use = 0;
  1382. return ERR_PTR(ret);
  1383. }
  1384. console_cdev.online = 1;
  1385. return &console_cdev;
  1386. }
  1387. static int ccw_device_pm_restore(struct device *dev);
  1388. int ccw_device_force_console(void)
  1389. {
  1390. if (!console_cdev_in_use)
  1391. return -ENODEV;
  1392. return ccw_device_pm_restore(&console_cdev.dev);
  1393. }
  1394. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1395. #endif
  1396. /*
  1397. * get ccw_device matching the busid, but only if owned by cdrv
  1398. */
  1399. static int
  1400. __ccwdev_check_busid(struct device *dev, void *id)
  1401. {
  1402. char *bus_id;
  1403. bus_id = id;
  1404. return (strcmp(bus_id, dev_name(dev)) == 0);
  1405. }
  1406. /**
  1407. * get_ccwdev_by_busid() - obtain device from a bus id
  1408. * @cdrv: driver the device is owned by
  1409. * @bus_id: bus id of the device to be searched
  1410. *
  1411. * This function searches all devices owned by @cdrv for a device with a bus
  1412. * id matching @bus_id.
  1413. * Returns:
  1414. * If a match is found, its reference count of the found device is increased
  1415. * and it is returned; else %NULL is returned.
  1416. */
  1417. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1418. const char *bus_id)
  1419. {
  1420. struct device *dev;
  1421. struct device_driver *drv;
  1422. drv = get_driver(&cdrv->driver);
  1423. if (!drv)
  1424. return NULL;
  1425. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1426. __ccwdev_check_busid);
  1427. put_driver(drv);
  1428. return dev ? to_ccwdev(dev) : NULL;
  1429. }
  1430. /************************** device driver handling ************************/
  1431. /* This is the implementation of the ccw_driver class. The probe, remove
  1432. * and release methods are initially very similar to the device_driver
  1433. * implementations, with the difference that they have ccw_device
  1434. * arguments.
  1435. *
  1436. * A ccw driver also contains the information that is needed for
  1437. * device matching.
  1438. */
  1439. static int
  1440. ccw_device_probe (struct device *dev)
  1441. {
  1442. struct ccw_device *cdev = to_ccwdev(dev);
  1443. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1444. int ret;
  1445. cdev->drv = cdrv; /* to let the driver call _set_online */
  1446. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1447. if (ret) {
  1448. cdev->drv = NULL;
  1449. return ret;
  1450. }
  1451. return 0;
  1452. }
  1453. static int
  1454. ccw_device_remove (struct device *dev)
  1455. {
  1456. struct ccw_device *cdev = to_ccwdev(dev);
  1457. struct ccw_driver *cdrv = cdev->drv;
  1458. int ret;
  1459. if (cdrv->remove)
  1460. cdrv->remove(cdev);
  1461. if (cdev->online) {
  1462. cdev->online = 0;
  1463. spin_lock_irq(cdev->ccwlock);
  1464. ret = ccw_device_offline(cdev);
  1465. spin_unlock_irq(cdev->ccwlock);
  1466. if (ret == 0)
  1467. wait_event(cdev->private->wait_q,
  1468. dev_fsm_final_state(cdev));
  1469. else
  1470. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1471. "device 0.%x.%04x\n",
  1472. ret, cdev->private->dev_id.ssid,
  1473. cdev->private->dev_id.devno);
  1474. /* Give up reference obtained in ccw_device_set_online(). */
  1475. put_device(&cdev->dev);
  1476. }
  1477. ccw_device_set_timeout(cdev, 0);
  1478. cdev->drv = NULL;
  1479. return 0;
  1480. }
  1481. static void ccw_device_shutdown(struct device *dev)
  1482. {
  1483. struct ccw_device *cdev;
  1484. cdev = to_ccwdev(dev);
  1485. if (cdev->drv && cdev->drv->shutdown)
  1486. cdev->drv->shutdown(cdev);
  1487. disable_cmf(cdev);
  1488. }
  1489. static int ccw_device_pm_prepare(struct device *dev)
  1490. {
  1491. struct ccw_device *cdev = to_ccwdev(dev);
  1492. if (work_pending(&cdev->private->todo_work))
  1493. return -EAGAIN;
  1494. /* Fail while device is being set online/offline. */
  1495. if (atomic_read(&cdev->private->onoff))
  1496. return -EAGAIN;
  1497. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1498. return cdev->drv->prepare(cdev);
  1499. return 0;
  1500. }
  1501. static void ccw_device_pm_complete(struct device *dev)
  1502. {
  1503. struct ccw_device *cdev = to_ccwdev(dev);
  1504. if (cdev->online && cdev->drv && cdev->drv->complete)
  1505. cdev->drv->complete(cdev);
  1506. }
  1507. static int ccw_device_pm_freeze(struct device *dev)
  1508. {
  1509. struct ccw_device *cdev = to_ccwdev(dev);
  1510. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1511. int ret, cm_enabled;
  1512. /* Fail suspend while device is in transistional state. */
  1513. if (!dev_fsm_final_state(cdev))
  1514. return -EAGAIN;
  1515. if (!cdev->online)
  1516. return 0;
  1517. if (cdev->drv && cdev->drv->freeze) {
  1518. ret = cdev->drv->freeze(cdev);
  1519. if (ret)
  1520. return ret;
  1521. }
  1522. spin_lock_irq(sch->lock);
  1523. cm_enabled = cdev->private->cmb != NULL;
  1524. spin_unlock_irq(sch->lock);
  1525. if (cm_enabled) {
  1526. /* Don't have the css write on memory. */
  1527. ret = ccw_set_cmf(cdev, 0);
  1528. if (ret)
  1529. return ret;
  1530. }
  1531. /* From here on, disallow device driver I/O. */
  1532. spin_lock_irq(sch->lock);
  1533. ret = cio_disable_subchannel(sch);
  1534. spin_unlock_irq(sch->lock);
  1535. return ret;
  1536. }
  1537. static int ccw_device_pm_thaw(struct device *dev)
  1538. {
  1539. struct ccw_device *cdev = to_ccwdev(dev);
  1540. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1541. int ret, cm_enabled;
  1542. if (!cdev->online)
  1543. return 0;
  1544. spin_lock_irq(sch->lock);
  1545. /* Allow device driver I/O again. */
  1546. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1547. cm_enabled = cdev->private->cmb != NULL;
  1548. spin_unlock_irq(sch->lock);
  1549. if (ret)
  1550. return ret;
  1551. if (cm_enabled) {
  1552. ret = ccw_set_cmf(cdev, 1);
  1553. if (ret)
  1554. return ret;
  1555. }
  1556. if (cdev->drv && cdev->drv->thaw)
  1557. ret = cdev->drv->thaw(cdev);
  1558. return ret;
  1559. }
  1560. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1561. {
  1562. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1563. if (cio_is_console(sch->schid))
  1564. goto out;
  1565. /*
  1566. * While we were sleeping, devices may have gone or become
  1567. * available again. Kick re-detection.
  1568. */
  1569. spin_lock_irq(sch->lock);
  1570. cdev->private->flags.resuming = 1;
  1571. ccw_device_recognition(cdev);
  1572. spin_unlock_irq(sch->lock);
  1573. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1574. cdev->private->state == DEV_STATE_DISCONNECTED);
  1575. out:
  1576. cdev->private->flags.resuming = 0;
  1577. }
  1578. static int resume_handle_boxed(struct ccw_device *cdev)
  1579. {
  1580. cdev->private->state = DEV_STATE_BOXED;
  1581. if (ccw_device_notify(cdev, CIO_BOXED))
  1582. return 0;
  1583. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1584. return -ENODEV;
  1585. }
  1586. static int resume_handle_disc(struct ccw_device *cdev)
  1587. {
  1588. cdev->private->state = DEV_STATE_DISCONNECTED;
  1589. if (ccw_device_notify(cdev, CIO_GONE))
  1590. return 0;
  1591. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1592. return -ENODEV;
  1593. }
  1594. static int ccw_device_pm_restore(struct device *dev)
  1595. {
  1596. struct ccw_device *cdev = to_ccwdev(dev);
  1597. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1598. int ret = 0, cm_enabled;
  1599. __ccw_device_pm_restore(cdev);
  1600. spin_lock_irq(sch->lock);
  1601. if (cio_is_console(sch->schid)) {
  1602. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1603. spin_unlock_irq(sch->lock);
  1604. goto out_restore;
  1605. }
  1606. cdev->private->flags.donotify = 0;
  1607. /* check recognition results */
  1608. switch (cdev->private->state) {
  1609. case DEV_STATE_OFFLINE:
  1610. break;
  1611. case DEV_STATE_BOXED:
  1612. ret = resume_handle_boxed(cdev);
  1613. spin_unlock_irq(sch->lock);
  1614. if (ret)
  1615. goto out;
  1616. goto out_restore;
  1617. case DEV_STATE_DISCONNECTED:
  1618. goto out_disc_unlock;
  1619. default:
  1620. goto out_unreg_unlock;
  1621. }
  1622. /* check if the device id has changed */
  1623. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1624. CIO_MSG_EVENT(0, "resume: sch 0.%x.%04x: failed (devno "
  1625. "changed from %04x to %04x)\n",
  1626. sch->schid.ssid, sch->schid.sch_no,
  1627. cdev->private->dev_id.devno,
  1628. sch->schib.pmcw.dev);
  1629. goto out_unreg_unlock;
  1630. }
  1631. /* check if the device type has changed */
  1632. if (!ccw_device_test_sense_data(cdev)) {
  1633. ccw_device_update_sense_data(cdev);
  1634. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  1635. ret = -ENODEV;
  1636. goto out_unlock;
  1637. }
  1638. if (!cdev->online) {
  1639. ret = 0;
  1640. goto out_unlock;
  1641. }
  1642. ret = ccw_device_online(cdev);
  1643. if (ret)
  1644. goto out_disc_unlock;
  1645. cm_enabled = cdev->private->cmb != NULL;
  1646. spin_unlock_irq(sch->lock);
  1647. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1648. if (cdev->private->state != DEV_STATE_ONLINE) {
  1649. spin_lock_irq(sch->lock);
  1650. goto out_disc_unlock;
  1651. }
  1652. if (cm_enabled) {
  1653. ret = ccw_set_cmf(cdev, 1);
  1654. if (ret) {
  1655. CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
  1656. "(rc=%d)\n", cdev->private->dev_id.ssid,
  1657. cdev->private->dev_id.devno, ret);
  1658. ret = 0;
  1659. }
  1660. }
  1661. out_restore:
  1662. if (cdev->online && cdev->drv && cdev->drv->restore)
  1663. ret = cdev->drv->restore(cdev);
  1664. out:
  1665. return ret;
  1666. out_disc_unlock:
  1667. ret = resume_handle_disc(cdev);
  1668. spin_unlock_irq(sch->lock);
  1669. if (ret)
  1670. return ret;
  1671. goto out_restore;
  1672. out_unreg_unlock:
  1673. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
  1674. ret = -ENODEV;
  1675. out_unlock:
  1676. spin_unlock_irq(sch->lock);
  1677. return ret;
  1678. }
  1679. static struct dev_pm_ops ccw_pm_ops = {
  1680. .prepare = ccw_device_pm_prepare,
  1681. .complete = ccw_device_pm_complete,
  1682. .freeze = ccw_device_pm_freeze,
  1683. .thaw = ccw_device_pm_thaw,
  1684. .restore = ccw_device_pm_restore,
  1685. };
  1686. struct bus_type ccw_bus_type = {
  1687. .name = "ccw",
  1688. .match = ccw_bus_match,
  1689. .uevent = ccw_uevent,
  1690. .probe = ccw_device_probe,
  1691. .remove = ccw_device_remove,
  1692. .shutdown = ccw_device_shutdown,
  1693. .pm = &ccw_pm_ops,
  1694. };
  1695. /**
  1696. * ccw_driver_register() - register a ccw driver
  1697. * @cdriver: driver to be registered
  1698. *
  1699. * This function is mainly a wrapper around driver_register().
  1700. * Returns:
  1701. * %0 on success and a negative error value on failure.
  1702. */
  1703. int ccw_driver_register(struct ccw_driver *cdriver)
  1704. {
  1705. struct device_driver *drv = &cdriver->driver;
  1706. drv->bus = &ccw_bus_type;
  1707. drv->name = cdriver->name;
  1708. drv->owner = cdriver->owner;
  1709. return driver_register(drv);
  1710. }
  1711. /**
  1712. * ccw_driver_unregister() - deregister a ccw driver
  1713. * @cdriver: driver to be deregistered
  1714. *
  1715. * This function is mainly a wrapper around driver_unregister().
  1716. */
  1717. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1718. {
  1719. driver_unregister(&cdriver->driver);
  1720. }
  1721. /* Helper func for qdio. */
  1722. struct subchannel_id
  1723. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1724. {
  1725. struct subchannel *sch;
  1726. sch = to_subchannel(cdev->dev.parent);
  1727. return sch->schid;
  1728. }
  1729. static void ccw_device_todo(struct work_struct *work)
  1730. {
  1731. struct ccw_device_private *priv;
  1732. struct ccw_device *cdev;
  1733. struct subchannel *sch;
  1734. enum cdev_todo todo;
  1735. priv = container_of(work, struct ccw_device_private, todo_work);
  1736. cdev = priv->cdev;
  1737. sch = to_subchannel(cdev->dev.parent);
  1738. /* Find out todo. */
  1739. spin_lock_irq(cdev->ccwlock);
  1740. todo = priv->todo;
  1741. priv->todo = CDEV_TODO_NOTHING;
  1742. CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
  1743. priv->dev_id.ssid, priv->dev_id.devno, todo);
  1744. spin_unlock_irq(cdev->ccwlock);
  1745. /* Perform todo. */
  1746. switch (todo) {
  1747. case CDEV_TODO_ENABLE_CMF:
  1748. cmf_reenable(cdev);
  1749. break;
  1750. case CDEV_TODO_REBIND:
  1751. ccw_device_do_unbind_bind(cdev);
  1752. break;
  1753. case CDEV_TODO_REGISTER:
  1754. io_subchannel_register(cdev);
  1755. break;
  1756. case CDEV_TODO_UNREG_EVAL:
  1757. if (!sch_is_pseudo_sch(sch))
  1758. css_schedule_eval(sch->schid);
  1759. /* fall-through */
  1760. case CDEV_TODO_UNREG:
  1761. if (sch_is_pseudo_sch(sch))
  1762. ccw_device_unregister(cdev);
  1763. else
  1764. ccw_device_call_sch_unregister(cdev);
  1765. break;
  1766. default:
  1767. break;
  1768. }
  1769. /* Release workqueue ref. */
  1770. put_device(&cdev->dev);
  1771. }
  1772. /**
  1773. * ccw_device_sched_todo - schedule ccw device operation
  1774. * @cdev: ccw device
  1775. * @todo: todo
  1776. *
  1777. * Schedule the operation identified by @todo to be performed on the slow path
  1778. * workqueue. Do nothing if another operation with higher priority is already
  1779. * scheduled. Needs to be called with ccwdev lock held.
  1780. */
  1781. void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
  1782. {
  1783. CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
  1784. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  1785. todo);
  1786. if (cdev->private->todo >= todo)
  1787. return;
  1788. cdev->private->todo = todo;
  1789. /* Get workqueue ref. */
  1790. if (!get_device(&cdev->dev))
  1791. return;
  1792. if (!queue_work(slow_path_wq, &cdev->private->todo_work)) {
  1793. /* Already queued, release workqueue ref. */
  1794. put_device(&cdev->dev);
  1795. }
  1796. }
  1797. MODULE_LICENSE("GPL");
  1798. EXPORT_SYMBOL(ccw_device_set_online);
  1799. EXPORT_SYMBOL(ccw_device_set_offline);
  1800. EXPORT_SYMBOL(ccw_driver_register);
  1801. EXPORT_SYMBOL(ccw_driver_unregister);
  1802. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1803. EXPORT_SYMBOL(ccw_bus_type);
  1804. EXPORT_SYMBOL(ccw_device_work);
  1805. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);