device.c 52 KB

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