device.c 51 KB

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