device.c 51 KB

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