device.c 54 KB

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