device.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  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. 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 DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  565. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  566. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  567. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  568. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  569. static DEVICE_ATTR(online, 0644, online_show, online_store);
  570. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  571. static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
  572. static struct attribute *io_subchannel_attrs[] = {
  573. &dev_attr_chpids.attr,
  574. &dev_attr_pimpampom.attr,
  575. &dev_attr_logging.attr,
  576. NULL,
  577. };
  578. static struct attribute_group io_subchannel_attr_group = {
  579. .attrs = io_subchannel_attrs,
  580. };
  581. static struct attribute * ccwdev_attrs[] = {
  582. &dev_attr_devtype.attr,
  583. &dev_attr_cutype.attr,
  584. &dev_attr_modalias.attr,
  585. &dev_attr_online.attr,
  586. &dev_attr_cmb_enable.attr,
  587. &dev_attr_availability.attr,
  588. NULL,
  589. };
  590. static struct attribute_group ccwdev_attr_group = {
  591. .attrs = ccwdev_attrs,
  592. };
  593. static const struct attribute_group *ccwdev_attr_groups[] = {
  594. &ccwdev_attr_group,
  595. NULL,
  596. };
  597. /* this is a simple abstraction for device_register that sets the
  598. * correct bus type and adds the bus specific files */
  599. static int ccw_device_register(struct ccw_device *cdev)
  600. {
  601. struct device *dev = &cdev->dev;
  602. int ret;
  603. dev->bus = &ccw_bus_type;
  604. ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
  605. cdev->private->dev_id.devno);
  606. if (ret)
  607. return ret;
  608. return device_add(dev);
  609. }
  610. static int match_dev_id(struct device *dev, void *data)
  611. {
  612. struct ccw_device *cdev = to_ccwdev(dev);
  613. struct ccw_dev_id *dev_id = data;
  614. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  615. }
  616. static struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
  617. {
  618. struct device *dev;
  619. dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
  620. return dev ? to_ccwdev(dev) : NULL;
  621. }
  622. static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
  623. {
  624. int ret;
  625. if (device_is_registered(&cdev->dev)) {
  626. device_release_driver(&cdev->dev);
  627. ret = device_attach(&cdev->dev);
  628. WARN_ON(ret == -ENODEV);
  629. }
  630. }
  631. static void
  632. ccw_device_release(struct device *dev)
  633. {
  634. struct ccw_device *cdev;
  635. cdev = to_ccwdev(dev);
  636. /* Release reference of parent subchannel. */
  637. put_device(cdev->dev.parent);
  638. kfree(cdev->private);
  639. kfree(cdev);
  640. }
  641. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  642. {
  643. struct ccw_device *cdev;
  644. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  645. if (cdev) {
  646. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  647. GFP_KERNEL | GFP_DMA);
  648. if (cdev->private)
  649. return cdev;
  650. }
  651. kfree(cdev);
  652. return ERR_PTR(-ENOMEM);
  653. }
  654. static void ccw_device_todo(struct work_struct *work);
  655. static int io_subchannel_initialize_dev(struct subchannel *sch,
  656. struct ccw_device *cdev)
  657. {
  658. cdev->private->cdev = cdev;
  659. atomic_set(&cdev->private->onoff, 0);
  660. cdev->dev.parent = &sch->dev;
  661. cdev->dev.release = ccw_device_release;
  662. INIT_WORK(&cdev->private->todo_work, ccw_device_todo);
  663. cdev->dev.groups = ccwdev_attr_groups;
  664. /* Do first half of device_register. */
  665. device_initialize(&cdev->dev);
  666. if (!get_device(&sch->dev)) {
  667. /* Release reference from device_initialize(). */
  668. put_device(&cdev->dev);
  669. return -ENODEV;
  670. }
  671. cdev->private->flags.initialized = 1;
  672. return 0;
  673. }
  674. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  675. {
  676. struct ccw_device *cdev;
  677. int ret;
  678. cdev = io_subchannel_allocate_dev(sch);
  679. if (!IS_ERR(cdev)) {
  680. ret = io_subchannel_initialize_dev(sch, cdev);
  681. if (ret)
  682. cdev = ERR_PTR(ret);
  683. }
  684. return cdev;
  685. }
  686. static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
  687. static void sch_create_and_recog_new_device(struct subchannel *sch)
  688. {
  689. struct ccw_device *cdev;
  690. /* Need to allocate a new ccw device. */
  691. cdev = io_subchannel_create_ccwdev(sch);
  692. if (IS_ERR(cdev)) {
  693. /* OK, we did everything we could... */
  694. css_sch_device_unregister(sch);
  695. return;
  696. }
  697. /* Start recognition for the new ccw device. */
  698. io_subchannel_recog(cdev, sch);
  699. }
  700. /*
  701. * Register recognized device.
  702. */
  703. static void io_subchannel_register(struct ccw_device *cdev)
  704. {
  705. struct subchannel *sch;
  706. int ret, adjust_init_count = 1;
  707. unsigned long flags;
  708. sch = to_subchannel(cdev->dev.parent);
  709. /*
  710. * Check if subchannel is still registered. It may have become
  711. * unregistered if a machine check hit us after finishing
  712. * device recognition but before the register work could be
  713. * queued.
  714. */
  715. if (!device_is_registered(&sch->dev))
  716. goto out_err;
  717. css_update_ssd_info(sch);
  718. /*
  719. * io_subchannel_register() will also be called after device
  720. * recognition has been done for a boxed device (which will already
  721. * be registered). We need to reprobe since we may now have sense id
  722. * information.
  723. */
  724. if (device_is_registered(&cdev->dev)) {
  725. if (!cdev->drv) {
  726. ret = device_reprobe(&cdev->dev);
  727. if (ret)
  728. /* We can't do much here. */
  729. CIO_MSG_EVENT(0, "device_reprobe() returned"
  730. " %d for 0.%x.%04x\n", ret,
  731. cdev->private->dev_id.ssid,
  732. cdev->private->dev_id.devno);
  733. }
  734. adjust_init_count = 0;
  735. goto out;
  736. }
  737. /*
  738. * Now we know this subchannel will stay, we can throw
  739. * our delayed uevent.
  740. */
  741. dev_set_uevent_suppress(&sch->dev, 0);
  742. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  743. /* make it known to the system */
  744. ret = ccw_device_register(cdev);
  745. if (ret) {
  746. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  747. cdev->private->dev_id.ssid,
  748. cdev->private->dev_id.devno, ret);
  749. spin_lock_irqsave(sch->lock, flags);
  750. sch_set_cdev(sch, NULL);
  751. spin_unlock_irqrestore(sch->lock, flags);
  752. /* Release initial device reference. */
  753. put_device(&cdev->dev);
  754. goto out_err;
  755. }
  756. out:
  757. cdev->private->flags.recog_done = 1;
  758. wake_up(&cdev->private->wait_q);
  759. out_err:
  760. if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
  761. wake_up(&ccw_device_init_wq);
  762. }
  763. static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
  764. {
  765. struct subchannel *sch;
  766. /* Get subchannel reference for local processing. */
  767. if (!get_device(cdev->dev.parent))
  768. return;
  769. sch = to_subchannel(cdev->dev.parent);
  770. css_sch_device_unregister(sch);
  771. /* Release subchannel reference for local processing. */
  772. put_device(&sch->dev);
  773. }
  774. /*
  775. * subchannel recognition done. Called from the state machine.
  776. */
  777. void
  778. io_subchannel_recog_done(struct ccw_device *cdev)
  779. {
  780. if (css_init_done == 0) {
  781. cdev->private->flags.recog_done = 1;
  782. return;
  783. }
  784. switch (cdev->private->state) {
  785. case DEV_STATE_BOXED:
  786. /* Device did not respond in time. */
  787. case DEV_STATE_NOT_OPER:
  788. cdev->private->flags.recog_done = 1;
  789. /* Remove device found not operational. */
  790. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  791. if (atomic_dec_and_test(&ccw_device_init_count))
  792. wake_up(&ccw_device_init_wq);
  793. break;
  794. case DEV_STATE_OFFLINE:
  795. /*
  796. * We can't register the device in interrupt context so
  797. * we schedule a work item.
  798. */
  799. ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
  800. break;
  801. }
  802. }
  803. static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  804. {
  805. struct ccw_device_private *priv;
  806. cdev->ccwlock = sch->lock;
  807. /* Init private data. */
  808. priv = cdev->private;
  809. priv->dev_id.devno = sch->schib.pmcw.dev;
  810. priv->dev_id.ssid = sch->schid.ssid;
  811. priv->schid = sch->schid;
  812. priv->state = DEV_STATE_NOT_OPER;
  813. INIT_LIST_HEAD(&priv->cmb_list);
  814. init_waitqueue_head(&priv->wait_q);
  815. init_timer(&priv->timer);
  816. /* Increase counter of devices currently in recognition. */
  817. atomic_inc(&ccw_device_init_count);
  818. /* Start async. device sensing. */
  819. spin_lock_irq(sch->lock);
  820. sch_set_cdev(sch, cdev);
  821. ccw_device_recognition(cdev);
  822. spin_unlock_irq(sch->lock);
  823. }
  824. static int ccw_device_move_to_sch(struct ccw_device *cdev,
  825. struct subchannel *sch)
  826. {
  827. struct subchannel *old_sch;
  828. int rc, old_enabled = 0;
  829. old_sch = to_subchannel(cdev->dev.parent);
  830. /* Obtain child reference for new parent. */
  831. if (!get_device(&sch->dev))
  832. return -ENODEV;
  833. if (!sch_is_pseudo_sch(old_sch)) {
  834. spin_lock_irq(old_sch->lock);
  835. old_enabled = old_sch->schib.pmcw.ena;
  836. rc = 0;
  837. if (old_enabled)
  838. rc = cio_disable_subchannel(old_sch);
  839. spin_unlock_irq(old_sch->lock);
  840. if (rc == -EBUSY) {
  841. /* Release child reference for new parent. */
  842. put_device(&sch->dev);
  843. return rc;
  844. }
  845. }
  846. mutex_lock(&sch->reg_mutex);
  847. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  848. mutex_unlock(&sch->reg_mutex);
  849. if (rc) {
  850. CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
  851. cdev->private->dev_id.ssid,
  852. cdev->private->dev_id.devno, sch->schid.ssid,
  853. sch->schib.pmcw.dev, rc);
  854. if (old_enabled) {
  855. /* Try to reenable the old subchannel. */
  856. spin_lock_irq(old_sch->lock);
  857. cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
  858. spin_unlock_irq(old_sch->lock);
  859. }
  860. /* Release child reference for new parent. */
  861. put_device(&sch->dev);
  862. return rc;
  863. }
  864. /* Clean up old subchannel. */
  865. if (!sch_is_pseudo_sch(old_sch)) {
  866. spin_lock_irq(old_sch->lock);
  867. sch_set_cdev(old_sch, NULL);
  868. spin_unlock_irq(old_sch->lock);
  869. css_schedule_eval(old_sch->schid);
  870. }
  871. /* Release child reference for old parent. */
  872. put_device(&old_sch->dev);
  873. /* Initialize new subchannel. */
  874. spin_lock_irq(sch->lock);
  875. cdev->private->schid = sch->schid;
  876. cdev->ccwlock = sch->lock;
  877. if (!sch_is_pseudo_sch(sch))
  878. sch_set_cdev(sch, cdev);
  879. spin_unlock_irq(sch->lock);
  880. if (!sch_is_pseudo_sch(sch))
  881. css_update_ssd_info(sch);
  882. return 0;
  883. }
  884. static int ccw_device_move_to_orph(struct ccw_device *cdev)
  885. {
  886. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  887. struct channel_subsystem *css = to_css(sch->dev.parent);
  888. return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
  889. }
  890. static void io_subchannel_irq(struct subchannel *sch)
  891. {
  892. struct ccw_device *cdev;
  893. cdev = sch_get_cdev(sch);
  894. CIO_TRACE_EVENT(6, "IRQ");
  895. CIO_TRACE_EVENT(6, dev_name(&sch->dev));
  896. if (cdev)
  897. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  898. }
  899. void io_subchannel_init_config(struct subchannel *sch)
  900. {
  901. memset(&sch->config, 0, sizeof(sch->config));
  902. sch->config.csense = 1;
  903. }
  904. static void io_subchannel_init_fields(struct subchannel *sch)
  905. {
  906. if (cio_is_console(sch->schid))
  907. sch->opm = 0xff;
  908. else
  909. sch->opm = chp_get_sch_opm(sch);
  910. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  911. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  912. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  913. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  914. sch->schib.pmcw.dev, sch->schid.ssid,
  915. sch->schid.sch_no, sch->schib.pmcw.pim,
  916. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  917. io_subchannel_init_config(sch);
  918. }
  919. /*
  920. * Note: We always return 0 so that we bind to the device even on error.
  921. * This is needed so that our remove function is called on unregister.
  922. */
  923. static int io_subchannel_probe(struct subchannel *sch)
  924. {
  925. struct io_subchannel_private *io_priv;
  926. struct ccw_device *cdev;
  927. int rc;
  928. if (cio_is_console(sch->schid)) {
  929. rc = sysfs_create_group(&sch->dev.kobj,
  930. &io_subchannel_attr_group);
  931. if (rc)
  932. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  933. "attributes for subchannel "
  934. "0.%x.%04x (rc=%d)\n",
  935. sch->schid.ssid, sch->schid.sch_no, rc);
  936. /*
  937. * The console subchannel already has an associated ccw_device.
  938. * Throw the delayed uevent for the subchannel, register
  939. * the ccw_device and exit.
  940. */
  941. dev_set_uevent_suppress(&sch->dev, 0);
  942. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  943. cdev = sch_get_cdev(sch);
  944. cdev->dev.groups = ccwdev_attr_groups;
  945. device_initialize(&cdev->dev);
  946. cdev->private->flags.initialized = 1;
  947. ccw_device_register(cdev);
  948. /*
  949. * Check if the device is already online. If it is
  950. * the reference count needs to be corrected since we
  951. * didn't obtain a reference in ccw_device_set_online.
  952. */
  953. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  954. cdev->private->state != DEV_STATE_OFFLINE &&
  955. cdev->private->state != DEV_STATE_BOXED)
  956. get_device(&cdev->dev);
  957. return 0;
  958. }
  959. io_subchannel_init_fields(sch);
  960. rc = cio_commit_config(sch);
  961. if (rc)
  962. goto out_schedule;
  963. rc = sysfs_create_group(&sch->dev.kobj,
  964. &io_subchannel_attr_group);
  965. if (rc)
  966. goto out_schedule;
  967. /* Allocate I/O subchannel private data. */
  968. io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
  969. if (!io_priv)
  970. goto out_schedule;
  971. set_io_private(sch, io_priv);
  972. css_schedule_eval(sch->schid);
  973. return 0;
  974. out_schedule:
  975. spin_lock_irq(sch->lock);
  976. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  977. spin_unlock_irq(sch->lock);
  978. return 0;
  979. }
  980. static int
  981. io_subchannel_remove (struct subchannel *sch)
  982. {
  983. struct io_subchannel_private *io_priv = to_io_private(sch);
  984. struct ccw_device *cdev;
  985. cdev = sch_get_cdev(sch);
  986. if (!cdev)
  987. goto out_free;
  988. io_subchannel_quiesce(sch);
  989. /* Set ccw device to not operational and drop reference. */
  990. spin_lock_irq(cdev->ccwlock);
  991. sch_set_cdev(sch, NULL);
  992. set_io_private(sch, NULL);
  993. cdev->private->state = DEV_STATE_NOT_OPER;
  994. spin_unlock_irq(cdev->ccwlock);
  995. ccw_device_unregister(cdev);
  996. out_free:
  997. kfree(io_priv);
  998. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  999. return 0;
  1000. }
  1001. static void io_subchannel_verify(struct subchannel *sch)
  1002. {
  1003. struct ccw_device *cdev;
  1004. cdev = sch_get_cdev(sch);
  1005. if (cdev)
  1006. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1007. }
  1008. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1009. {
  1010. struct ccw_device *cdev;
  1011. cdev = sch_get_cdev(sch);
  1012. if (!cdev)
  1013. return;
  1014. if (cio_update_schib(sch))
  1015. goto err;
  1016. /* Check for I/O on path. */
  1017. if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
  1018. goto out;
  1019. if (cdev->private->state == DEV_STATE_ONLINE) {
  1020. ccw_device_kill_io(cdev);
  1021. goto out;
  1022. }
  1023. if (cio_clear(sch))
  1024. goto err;
  1025. out:
  1026. /* Trigger path verification. */
  1027. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1028. return;
  1029. err:
  1030. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  1031. }
  1032. static int io_subchannel_chp_event(struct subchannel *sch,
  1033. struct chp_link *link, int event)
  1034. {
  1035. struct ccw_device *cdev = sch_get_cdev(sch);
  1036. int mask;
  1037. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1038. if (!mask)
  1039. return 0;
  1040. switch (event) {
  1041. case CHP_VARY_OFF:
  1042. sch->opm &= ~mask;
  1043. sch->lpm &= ~mask;
  1044. if (cdev)
  1045. cdev->private->path_gone_mask |= mask;
  1046. io_subchannel_terminate_path(sch, mask);
  1047. break;
  1048. case CHP_VARY_ON:
  1049. sch->opm |= mask;
  1050. sch->lpm |= mask;
  1051. if (cdev)
  1052. cdev->private->path_new_mask |= mask;
  1053. io_subchannel_verify(sch);
  1054. break;
  1055. case CHP_OFFLINE:
  1056. if (cio_update_schib(sch))
  1057. return -ENODEV;
  1058. if (cdev)
  1059. cdev->private->path_gone_mask |= mask;
  1060. io_subchannel_terminate_path(sch, mask);
  1061. break;
  1062. case CHP_ONLINE:
  1063. if (cio_update_schib(sch))
  1064. return -ENODEV;
  1065. sch->lpm |= mask & sch->opm;
  1066. if (cdev)
  1067. cdev->private->path_new_mask |= mask;
  1068. io_subchannel_verify(sch);
  1069. break;
  1070. }
  1071. return 0;
  1072. }
  1073. static void io_subchannel_quiesce(struct subchannel *sch)
  1074. {
  1075. struct ccw_device *cdev;
  1076. int ret;
  1077. spin_lock_irq(sch->lock);
  1078. cdev = sch_get_cdev(sch);
  1079. if (cio_is_console(sch->schid))
  1080. goto out_unlock;
  1081. if (!sch->schib.pmcw.ena)
  1082. goto out_unlock;
  1083. ret = cio_disable_subchannel(sch);
  1084. if (ret != -EBUSY)
  1085. goto out_unlock;
  1086. if (cdev->handler)
  1087. cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
  1088. while (ret == -EBUSY) {
  1089. cdev->private->state = DEV_STATE_QUIESCE;
  1090. cdev->private->iretry = 255;
  1091. ret = ccw_device_cancel_halt_clear(cdev);
  1092. if (ret == -EBUSY) {
  1093. ccw_device_set_timeout(cdev, HZ/10);
  1094. spin_unlock_irq(sch->lock);
  1095. wait_event(cdev->private->wait_q,
  1096. cdev->private->state != DEV_STATE_QUIESCE);
  1097. spin_lock_irq(sch->lock);
  1098. }
  1099. ret = cio_disable_subchannel(sch);
  1100. }
  1101. out_unlock:
  1102. spin_unlock_irq(sch->lock);
  1103. }
  1104. static void io_subchannel_shutdown(struct subchannel *sch)
  1105. {
  1106. io_subchannel_quiesce(sch);
  1107. }
  1108. static int device_is_disconnected(struct ccw_device *cdev)
  1109. {
  1110. if (!cdev)
  1111. return 0;
  1112. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1113. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1114. }
  1115. static int recovery_check(struct device *dev, void *data)
  1116. {
  1117. struct ccw_device *cdev = to_ccwdev(dev);
  1118. int *redo = data;
  1119. spin_lock_irq(cdev->ccwlock);
  1120. switch (cdev->private->state) {
  1121. case DEV_STATE_DISCONNECTED:
  1122. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1123. cdev->private->dev_id.ssid,
  1124. cdev->private->dev_id.devno);
  1125. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1126. *redo = 1;
  1127. break;
  1128. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1129. *redo = 1;
  1130. break;
  1131. }
  1132. spin_unlock_irq(cdev->ccwlock);
  1133. return 0;
  1134. }
  1135. static void recovery_work_func(struct work_struct *unused)
  1136. {
  1137. int redo = 0;
  1138. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1139. if (redo) {
  1140. spin_lock_irq(&recovery_lock);
  1141. if (!timer_pending(&recovery_timer)) {
  1142. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1143. recovery_phase++;
  1144. mod_timer(&recovery_timer, jiffies +
  1145. recovery_delay[recovery_phase] * HZ);
  1146. }
  1147. spin_unlock_irq(&recovery_lock);
  1148. } else
  1149. CIO_MSG_EVENT(4, "recovery: end\n");
  1150. }
  1151. static DECLARE_WORK(recovery_work, recovery_work_func);
  1152. static void recovery_func(unsigned long data)
  1153. {
  1154. /*
  1155. * We can't do our recovery in softirq context and it's not
  1156. * performance critical, so we schedule it.
  1157. */
  1158. schedule_work(&recovery_work);
  1159. }
  1160. static void ccw_device_schedule_recovery(void)
  1161. {
  1162. unsigned long flags;
  1163. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1164. spin_lock_irqsave(&recovery_lock, flags);
  1165. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1166. recovery_phase = 0;
  1167. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1168. }
  1169. spin_unlock_irqrestore(&recovery_lock, flags);
  1170. }
  1171. static int purge_fn(struct device *dev, void *data)
  1172. {
  1173. struct ccw_device *cdev = to_ccwdev(dev);
  1174. struct ccw_dev_id *id = &cdev->private->dev_id;
  1175. spin_lock_irq(cdev->ccwlock);
  1176. if (is_blacklisted(id->ssid, id->devno) &&
  1177. (cdev->private->state == DEV_STATE_OFFLINE) &&
  1178. (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
  1179. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
  1180. id->devno);
  1181. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1182. atomic_set(&cdev->private->onoff, 0);
  1183. }
  1184. spin_unlock_irq(cdev->ccwlock);
  1185. /* Abort loop in case of pending signal. */
  1186. if (signal_pending(current))
  1187. return -EINTR;
  1188. return 0;
  1189. }
  1190. /**
  1191. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1192. *
  1193. * Unregister all ccw devices that are offline and on the blacklist.
  1194. */
  1195. int ccw_purge_blacklisted(void)
  1196. {
  1197. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1198. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1199. return 0;
  1200. }
  1201. void ccw_device_set_disconnected(struct ccw_device *cdev)
  1202. {
  1203. if (!cdev)
  1204. return;
  1205. ccw_device_set_timeout(cdev, 0);
  1206. cdev->private->flags.fake_irb = 0;
  1207. cdev->private->state = DEV_STATE_DISCONNECTED;
  1208. if (cdev->online)
  1209. ccw_device_schedule_recovery();
  1210. }
  1211. void ccw_device_set_notoper(struct ccw_device *cdev)
  1212. {
  1213. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1214. CIO_TRACE_EVENT(2, "notoper");
  1215. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1216. ccw_device_set_timeout(cdev, 0);
  1217. cio_disable_subchannel(sch);
  1218. cdev->private->state = DEV_STATE_NOT_OPER;
  1219. }
  1220. enum io_sch_action {
  1221. IO_SCH_UNREG,
  1222. IO_SCH_ORPH_UNREG,
  1223. IO_SCH_ATTACH,
  1224. IO_SCH_UNREG_ATTACH,
  1225. IO_SCH_ORPH_ATTACH,
  1226. IO_SCH_REPROBE,
  1227. IO_SCH_VERIFY,
  1228. IO_SCH_DISC,
  1229. IO_SCH_NOP,
  1230. };
  1231. static enum io_sch_action sch_get_action(struct subchannel *sch)
  1232. {
  1233. struct ccw_device *cdev;
  1234. cdev = sch_get_cdev(sch);
  1235. if (cio_update_schib(sch)) {
  1236. /* Not operational. */
  1237. if (!cdev)
  1238. return IO_SCH_UNREG;
  1239. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1240. return IO_SCH_UNREG;
  1241. return IO_SCH_ORPH_UNREG;
  1242. }
  1243. /* Operational. */
  1244. if (!cdev)
  1245. return IO_SCH_ATTACH;
  1246. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1247. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1248. return IO_SCH_UNREG_ATTACH;
  1249. return IO_SCH_ORPH_ATTACH;
  1250. }
  1251. if ((sch->schib.pmcw.pam & sch->opm) == 0) {
  1252. if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
  1253. return IO_SCH_UNREG;
  1254. return IO_SCH_DISC;
  1255. }
  1256. if (device_is_disconnected(cdev))
  1257. return IO_SCH_REPROBE;
  1258. if (cdev->online)
  1259. return IO_SCH_VERIFY;
  1260. return IO_SCH_NOP;
  1261. }
  1262. /**
  1263. * io_subchannel_sch_event - process subchannel event
  1264. * @sch: subchannel
  1265. * @process: non-zero if function is called in process context
  1266. *
  1267. * An unspecified event occurred for this subchannel. Adjust data according
  1268. * to the current operational state of the subchannel and device. Return
  1269. * zero when the event has been handled sufficiently or -EAGAIN when this
  1270. * function should be called again in process context.
  1271. */
  1272. static int io_subchannel_sch_event(struct subchannel *sch, int process)
  1273. {
  1274. unsigned long flags;
  1275. struct ccw_device *cdev;
  1276. struct ccw_dev_id dev_id;
  1277. enum io_sch_action action;
  1278. int rc = -EAGAIN;
  1279. spin_lock_irqsave(sch->lock, flags);
  1280. if (!device_is_registered(&sch->dev))
  1281. goto out_unlock;
  1282. if (work_pending(&sch->todo_work))
  1283. goto out_unlock;
  1284. cdev = sch_get_cdev(sch);
  1285. if (cdev && work_pending(&cdev->private->todo_work))
  1286. goto out_unlock;
  1287. action = sch_get_action(sch);
  1288. CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
  1289. sch->schid.ssid, sch->schid.sch_no, process,
  1290. action);
  1291. /* Perform immediate actions while holding the lock. */
  1292. switch (action) {
  1293. case IO_SCH_REPROBE:
  1294. /* Trigger device recognition. */
  1295. ccw_device_trigger_reprobe(cdev);
  1296. rc = 0;
  1297. goto out_unlock;
  1298. case IO_SCH_VERIFY:
  1299. if (cdev->private->flags.resuming == 1) {
  1300. if (cio_enable_subchannel(sch, (u32)(addr_t)sch)) {
  1301. ccw_device_set_notoper(cdev);
  1302. break;
  1303. }
  1304. }
  1305. /* Trigger path verification. */
  1306. io_subchannel_verify(sch);
  1307. rc = 0;
  1308. goto out_unlock;
  1309. case IO_SCH_DISC:
  1310. ccw_device_set_disconnected(cdev);
  1311. rc = 0;
  1312. goto out_unlock;
  1313. case IO_SCH_ORPH_UNREG:
  1314. case IO_SCH_ORPH_ATTACH:
  1315. ccw_device_set_disconnected(cdev);
  1316. break;
  1317. case IO_SCH_UNREG_ATTACH:
  1318. case IO_SCH_UNREG:
  1319. if (!cdev)
  1320. break;
  1321. if (cdev->private->state == DEV_STATE_SENSE_ID) {
  1322. /*
  1323. * Note: delayed work triggered by this event
  1324. * and repeated calls to sch_event are synchronized
  1325. * by the above check for work_pending(cdev).
  1326. */
  1327. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  1328. } else
  1329. ccw_device_set_notoper(cdev);
  1330. break;
  1331. case IO_SCH_NOP:
  1332. rc = 0;
  1333. goto out_unlock;
  1334. default:
  1335. break;
  1336. }
  1337. spin_unlock_irqrestore(sch->lock, flags);
  1338. /* All other actions require process context. */
  1339. if (!process)
  1340. goto out;
  1341. /* Handle attached ccw device. */
  1342. switch (action) {
  1343. case IO_SCH_ORPH_UNREG:
  1344. case IO_SCH_ORPH_ATTACH:
  1345. /* Move ccw device to orphanage. */
  1346. rc = ccw_device_move_to_orph(cdev);
  1347. if (rc)
  1348. goto out;
  1349. break;
  1350. case IO_SCH_UNREG_ATTACH:
  1351. if (cdev->private->flags.resuming) {
  1352. /* Device will be handled later. */
  1353. rc = 0;
  1354. goto out;
  1355. }
  1356. /* Unregister ccw device. */
  1357. ccw_device_unregister(cdev);
  1358. break;
  1359. default:
  1360. break;
  1361. }
  1362. /* Handle subchannel. */
  1363. switch (action) {
  1364. case IO_SCH_ORPH_UNREG:
  1365. case IO_SCH_UNREG:
  1366. if (!cdev || !cdev->private->flags.resuming)
  1367. css_sch_device_unregister(sch);
  1368. break;
  1369. case IO_SCH_ORPH_ATTACH:
  1370. case IO_SCH_UNREG_ATTACH:
  1371. case IO_SCH_ATTACH:
  1372. dev_id.ssid = sch->schid.ssid;
  1373. dev_id.devno = sch->schib.pmcw.dev;
  1374. cdev = get_ccwdev_by_dev_id(&dev_id);
  1375. if (!cdev) {
  1376. sch_create_and_recog_new_device(sch);
  1377. break;
  1378. }
  1379. rc = ccw_device_move_to_sch(cdev, sch);
  1380. if (rc) {
  1381. /* Release reference from get_ccwdev_by_dev_id() */
  1382. put_device(&cdev->dev);
  1383. goto out;
  1384. }
  1385. spin_lock_irqsave(sch->lock, flags);
  1386. ccw_device_trigger_reprobe(cdev);
  1387. spin_unlock_irqrestore(sch->lock, flags);
  1388. /* Release reference from get_ccwdev_by_dev_id() */
  1389. put_device(&cdev->dev);
  1390. break;
  1391. default:
  1392. break;
  1393. }
  1394. return 0;
  1395. out_unlock:
  1396. spin_unlock_irqrestore(sch->lock, flags);
  1397. out:
  1398. return rc;
  1399. }
  1400. #ifdef CONFIG_CCW_CONSOLE
  1401. static struct ccw_device console_cdev;
  1402. static struct ccw_device_private console_private;
  1403. static int console_cdev_in_use;
  1404. static DEFINE_SPINLOCK(ccw_console_lock);
  1405. spinlock_t * cio_get_console_lock(void)
  1406. {
  1407. return &ccw_console_lock;
  1408. }
  1409. static int ccw_device_console_enable(struct ccw_device *cdev,
  1410. struct subchannel *sch)
  1411. {
  1412. struct io_subchannel_private *io_priv = cio_get_console_priv();
  1413. int rc;
  1414. /* Attach subchannel private data. */
  1415. memset(io_priv, 0, sizeof(*io_priv));
  1416. set_io_private(sch, io_priv);
  1417. io_subchannel_init_fields(sch);
  1418. rc = cio_commit_config(sch);
  1419. if (rc)
  1420. return rc;
  1421. sch->driver = &io_subchannel_driver;
  1422. /* Initialize the ccw_device structure. */
  1423. cdev->dev.parent= &sch->dev;
  1424. sch_set_cdev(sch, cdev);
  1425. io_subchannel_recog(cdev, sch);
  1426. /* Now wait for the async. recognition to come to an end. */
  1427. spin_lock_irq(cdev->ccwlock);
  1428. while (!dev_fsm_final_state(cdev))
  1429. wait_cons_dev();
  1430. rc = -EIO;
  1431. if (cdev->private->state != DEV_STATE_OFFLINE)
  1432. goto out_unlock;
  1433. ccw_device_online(cdev);
  1434. while (!dev_fsm_final_state(cdev))
  1435. wait_cons_dev();
  1436. if (cdev->private->state != DEV_STATE_ONLINE)
  1437. goto out_unlock;
  1438. rc = 0;
  1439. out_unlock:
  1440. spin_unlock_irq(cdev->ccwlock);
  1441. return rc;
  1442. }
  1443. struct ccw_device *
  1444. ccw_device_probe_console(void)
  1445. {
  1446. struct subchannel *sch;
  1447. int ret;
  1448. if (xchg(&console_cdev_in_use, 1) != 0)
  1449. return ERR_PTR(-EBUSY);
  1450. sch = cio_probe_console();
  1451. if (IS_ERR(sch)) {
  1452. console_cdev_in_use = 0;
  1453. return (void *) sch;
  1454. }
  1455. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1456. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1457. console_cdev.private = &console_private;
  1458. console_private.cdev = &console_cdev;
  1459. ret = ccw_device_console_enable(&console_cdev, sch);
  1460. if (ret) {
  1461. cio_release_console();
  1462. console_cdev_in_use = 0;
  1463. return ERR_PTR(ret);
  1464. }
  1465. console_cdev.online = 1;
  1466. return &console_cdev;
  1467. }
  1468. static int ccw_device_pm_restore(struct device *dev);
  1469. int ccw_device_force_console(void)
  1470. {
  1471. if (!console_cdev_in_use)
  1472. return -ENODEV;
  1473. return ccw_device_pm_restore(&console_cdev.dev);
  1474. }
  1475. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1476. #endif
  1477. /*
  1478. * get ccw_device matching the busid, but only if owned by cdrv
  1479. */
  1480. static int
  1481. __ccwdev_check_busid(struct device *dev, void *id)
  1482. {
  1483. char *bus_id;
  1484. bus_id = id;
  1485. return (strcmp(bus_id, dev_name(dev)) == 0);
  1486. }
  1487. /**
  1488. * get_ccwdev_by_busid() - obtain device from a bus id
  1489. * @cdrv: driver the device is owned by
  1490. * @bus_id: bus id of the device to be searched
  1491. *
  1492. * This function searches all devices owned by @cdrv for a device with a bus
  1493. * id matching @bus_id.
  1494. * Returns:
  1495. * If a match is found, its reference count of the found device is increased
  1496. * and it is returned; else %NULL is returned.
  1497. */
  1498. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1499. const char *bus_id)
  1500. {
  1501. struct device *dev;
  1502. struct device_driver *drv;
  1503. drv = get_driver(&cdrv->driver);
  1504. if (!drv)
  1505. return NULL;
  1506. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1507. __ccwdev_check_busid);
  1508. put_driver(drv);
  1509. return dev ? to_ccwdev(dev) : NULL;
  1510. }
  1511. /************************** device driver handling ************************/
  1512. /* This is the implementation of the ccw_driver class. The probe, remove
  1513. * and release methods are initially very similar to the device_driver
  1514. * implementations, with the difference that they have ccw_device
  1515. * arguments.
  1516. *
  1517. * A ccw driver also contains the information that is needed for
  1518. * device matching.
  1519. */
  1520. static int
  1521. ccw_device_probe (struct device *dev)
  1522. {
  1523. struct ccw_device *cdev = to_ccwdev(dev);
  1524. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1525. int ret;
  1526. cdev->drv = cdrv; /* to let the driver call _set_online */
  1527. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1528. if (ret) {
  1529. cdev->drv = NULL;
  1530. return ret;
  1531. }
  1532. return 0;
  1533. }
  1534. static int
  1535. ccw_device_remove (struct device *dev)
  1536. {
  1537. struct ccw_device *cdev = to_ccwdev(dev);
  1538. struct ccw_driver *cdrv = cdev->drv;
  1539. int ret;
  1540. if (cdrv->remove)
  1541. cdrv->remove(cdev);
  1542. if (cdev->online) {
  1543. cdev->online = 0;
  1544. spin_lock_irq(cdev->ccwlock);
  1545. ret = ccw_device_offline(cdev);
  1546. spin_unlock_irq(cdev->ccwlock);
  1547. if (ret == 0)
  1548. wait_event(cdev->private->wait_q,
  1549. dev_fsm_final_state(cdev));
  1550. else
  1551. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1552. "device 0.%x.%04x\n",
  1553. ret, cdev->private->dev_id.ssid,
  1554. cdev->private->dev_id.devno);
  1555. /* Give up reference obtained in ccw_device_set_online(). */
  1556. put_device(&cdev->dev);
  1557. }
  1558. ccw_device_set_timeout(cdev, 0);
  1559. cdev->drv = NULL;
  1560. return 0;
  1561. }
  1562. static void ccw_device_shutdown(struct device *dev)
  1563. {
  1564. struct ccw_device *cdev;
  1565. cdev = to_ccwdev(dev);
  1566. if (cdev->drv && cdev->drv->shutdown)
  1567. cdev->drv->shutdown(cdev);
  1568. disable_cmf(cdev);
  1569. }
  1570. static int ccw_device_pm_prepare(struct device *dev)
  1571. {
  1572. struct ccw_device *cdev = to_ccwdev(dev);
  1573. if (work_pending(&cdev->private->todo_work))
  1574. return -EAGAIN;
  1575. /* Fail while device is being set online/offline. */
  1576. if (atomic_read(&cdev->private->onoff))
  1577. return -EAGAIN;
  1578. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1579. return cdev->drv->prepare(cdev);
  1580. return 0;
  1581. }
  1582. static void ccw_device_pm_complete(struct device *dev)
  1583. {
  1584. struct ccw_device *cdev = to_ccwdev(dev);
  1585. if (cdev->online && cdev->drv && cdev->drv->complete)
  1586. cdev->drv->complete(cdev);
  1587. }
  1588. static int ccw_device_pm_freeze(struct device *dev)
  1589. {
  1590. struct ccw_device *cdev = to_ccwdev(dev);
  1591. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1592. int ret, cm_enabled;
  1593. /* Fail suspend while device is in transistional state. */
  1594. if (!dev_fsm_final_state(cdev))
  1595. return -EAGAIN;
  1596. if (!cdev->online)
  1597. return 0;
  1598. if (cdev->drv && cdev->drv->freeze) {
  1599. ret = cdev->drv->freeze(cdev);
  1600. if (ret)
  1601. return ret;
  1602. }
  1603. spin_lock_irq(sch->lock);
  1604. cm_enabled = cdev->private->cmb != NULL;
  1605. spin_unlock_irq(sch->lock);
  1606. if (cm_enabled) {
  1607. /* Don't have the css write on memory. */
  1608. ret = ccw_set_cmf(cdev, 0);
  1609. if (ret)
  1610. return ret;
  1611. }
  1612. /* From here on, disallow device driver I/O. */
  1613. spin_lock_irq(sch->lock);
  1614. ret = cio_disable_subchannel(sch);
  1615. spin_unlock_irq(sch->lock);
  1616. return ret;
  1617. }
  1618. static int ccw_device_pm_thaw(struct device *dev)
  1619. {
  1620. struct ccw_device *cdev = to_ccwdev(dev);
  1621. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1622. int ret, cm_enabled;
  1623. if (!cdev->online)
  1624. return 0;
  1625. spin_lock_irq(sch->lock);
  1626. /* Allow device driver I/O again. */
  1627. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1628. cm_enabled = cdev->private->cmb != NULL;
  1629. spin_unlock_irq(sch->lock);
  1630. if (ret)
  1631. return ret;
  1632. if (cm_enabled) {
  1633. ret = ccw_set_cmf(cdev, 1);
  1634. if (ret)
  1635. return ret;
  1636. }
  1637. if (cdev->drv && cdev->drv->thaw)
  1638. ret = cdev->drv->thaw(cdev);
  1639. return ret;
  1640. }
  1641. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1642. {
  1643. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1644. spin_lock_irq(sch->lock);
  1645. if (cio_is_console(sch->schid)) {
  1646. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1647. goto out_unlock;
  1648. }
  1649. /*
  1650. * While we were sleeping, devices may have gone or become
  1651. * available again. Kick re-detection.
  1652. */
  1653. cdev->private->flags.resuming = 1;
  1654. cdev->private->path_new_mask = LPM_ANYPATH;
  1655. css_schedule_eval(sch->schid);
  1656. spin_unlock_irq(sch->lock);
  1657. css_complete_work();
  1658. /* cdev may have been moved to a different subchannel. */
  1659. sch = to_subchannel(cdev->dev.parent);
  1660. spin_lock_irq(sch->lock);
  1661. if (cdev->private->state != DEV_STATE_ONLINE &&
  1662. cdev->private->state != DEV_STATE_OFFLINE)
  1663. goto out_unlock;
  1664. ccw_device_recognition(cdev);
  1665. spin_unlock_irq(sch->lock);
  1666. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1667. cdev->private->state == DEV_STATE_DISCONNECTED);
  1668. spin_lock_irq(sch->lock);
  1669. out_unlock:
  1670. cdev->private->flags.resuming = 0;
  1671. spin_unlock_irq(sch->lock);
  1672. }
  1673. static int resume_handle_boxed(struct ccw_device *cdev)
  1674. {
  1675. cdev->private->state = DEV_STATE_BOXED;
  1676. if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
  1677. return 0;
  1678. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1679. return -ENODEV;
  1680. }
  1681. static int resume_handle_disc(struct ccw_device *cdev)
  1682. {
  1683. cdev->private->state = DEV_STATE_DISCONNECTED;
  1684. if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
  1685. return 0;
  1686. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1687. return -ENODEV;
  1688. }
  1689. static int ccw_device_pm_restore(struct device *dev)
  1690. {
  1691. struct ccw_device *cdev = to_ccwdev(dev);
  1692. struct subchannel *sch;
  1693. int ret = 0;
  1694. __ccw_device_pm_restore(cdev);
  1695. sch = to_subchannel(cdev->dev.parent);
  1696. spin_lock_irq(sch->lock);
  1697. if (cio_is_console(sch->schid))
  1698. goto out_restore;
  1699. /* check recognition results */
  1700. switch (cdev->private->state) {
  1701. case DEV_STATE_OFFLINE:
  1702. case DEV_STATE_ONLINE:
  1703. cdev->private->flags.donotify = 0;
  1704. break;
  1705. case DEV_STATE_BOXED:
  1706. ret = resume_handle_boxed(cdev);
  1707. if (ret)
  1708. goto out_unlock;
  1709. goto out_restore;
  1710. default:
  1711. ret = resume_handle_disc(cdev);
  1712. if (ret)
  1713. goto out_unlock;
  1714. goto out_restore;
  1715. }
  1716. /* check if the device type has changed */
  1717. if (!ccw_device_test_sense_data(cdev)) {
  1718. ccw_device_update_sense_data(cdev);
  1719. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  1720. ret = -ENODEV;
  1721. goto out_unlock;
  1722. }
  1723. if (!cdev->online)
  1724. goto out_unlock;
  1725. if (ccw_device_online(cdev)) {
  1726. ret = resume_handle_disc(cdev);
  1727. if (ret)
  1728. goto out_unlock;
  1729. goto out_restore;
  1730. }
  1731. spin_unlock_irq(sch->lock);
  1732. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1733. spin_lock_irq(sch->lock);
  1734. if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
  1735. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1736. ret = -ENODEV;
  1737. goto out_unlock;
  1738. }
  1739. /* reenable cmf, if needed */
  1740. if (cdev->private->cmb) {
  1741. spin_unlock_irq(sch->lock);
  1742. ret = ccw_set_cmf(cdev, 1);
  1743. spin_lock_irq(sch->lock);
  1744. if (ret) {
  1745. CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
  1746. "(rc=%d)\n", cdev->private->dev_id.ssid,
  1747. cdev->private->dev_id.devno, ret);
  1748. ret = 0;
  1749. }
  1750. }
  1751. out_restore:
  1752. spin_unlock_irq(sch->lock);
  1753. if (cdev->online && cdev->drv && cdev->drv->restore)
  1754. ret = cdev->drv->restore(cdev);
  1755. return ret;
  1756. out_unlock:
  1757. spin_unlock_irq(sch->lock);
  1758. return ret;
  1759. }
  1760. static const struct dev_pm_ops ccw_pm_ops = {
  1761. .prepare = ccw_device_pm_prepare,
  1762. .complete = ccw_device_pm_complete,
  1763. .freeze = ccw_device_pm_freeze,
  1764. .thaw = ccw_device_pm_thaw,
  1765. .restore = ccw_device_pm_restore,
  1766. };
  1767. static struct bus_type ccw_bus_type = {
  1768. .name = "ccw",
  1769. .match = ccw_bus_match,
  1770. .uevent = ccw_uevent,
  1771. .probe = ccw_device_probe,
  1772. .remove = ccw_device_remove,
  1773. .shutdown = ccw_device_shutdown,
  1774. .pm = &ccw_pm_ops,
  1775. };
  1776. /**
  1777. * ccw_driver_register() - register a ccw driver
  1778. * @cdriver: driver to be registered
  1779. *
  1780. * This function is mainly a wrapper around driver_register().
  1781. * Returns:
  1782. * %0 on success and a negative error value on failure.
  1783. */
  1784. int ccw_driver_register(struct ccw_driver *cdriver)
  1785. {
  1786. struct device_driver *drv = &cdriver->driver;
  1787. drv->bus = &ccw_bus_type;
  1788. return driver_register(drv);
  1789. }
  1790. /**
  1791. * ccw_driver_unregister() - deregister a ccw driver
  1792. * @cdriver: driver to be deregistered
  1793. *
  1794. * This function is mainly a wrapper around driver_unregister().
  1795. */
  1796. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1797. {
  1798. driver_unregister(&cdriver->driver);
  1799. }
  1800. /* Helper func for qdio. */
  1801. struct subchannel_id
  1802. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1803. {
  1804. struct subchannel *sch;
  1805. sch = to_subchannel(cdev->dev.parent);
  1806. return sch->schid;
  1807. }
  1808. static void ccw_device_todo(struct work_struct *work)
  1809. {
  1810. struct ccw_device_private *priv;
  1811. struct ccw_device *cdev;
  1812. struct subchannel *sch;
  1813. enum cdev_todo todo;
  1814. priv = container_of(work, struct ccw_device_private, todo_work);
  1815. cdev = priv->cdev;
  1816. sch = to_subchannel(cdev->dev.parent);
  1817. /* Find out todo. */
  1818. spin_lock_irq(cdev->ccwlock);
  1819. todo = priv->todo;
  1820. priv->todo = CDEV_TODO_NOTHING;
  1821. CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
  1822. priv->dev_id.ssid, priv->dev_id.devno, todo);
  1823. spin_unlock_irq(cdev->ccwlock);
  1824. /* Perform todo. */
  1825. switch (todo) {
  1826. case CDEV_TODO_ENABLE_CMF:
  1827. cmf_reenable(cdev);
  1828. break;
  1829. case CDEV_TODO_REBIND:
  1830. ccw_device_do_unbind_bind(cdev);
  1831. break;
  1832. case CDEV_TODO_REGISTER:
  1833. io_subchannel_register(cdev);
  1834. break;
  1835. case CDEV_TODO_UNREG_EVAL:
  1836. if (!sch_is_pseudo_sch(sch))
  1837. css_schedule_eval(sch->schid);
  1838. /* fall-through */
  1839. case CDEV_TODO_UNREG:
  1840. if (sch_is_pseudo_sch(sch))
  1841. ccw_device_unregister(cdev);
  1842. else
  1843. ccw_device_call_sch_unregister(cdev);
  1844. break;
  1845. default:
  1846. break;
  1847. }
  1848. /* Release workqueue ref. */
  1849. put_device(&cdev->dev);
  1850. }
  1851. /**
  1852. * ccw_device_sched_todo - schedule ccw device operation
  1853. * @cdev: ccw device
  1854. * @todo: todo
  1855. *
  1856. * Schedule the operation identified by @todo to be performed on the slow path
  1857. * workqueue. Do nothing if another operation with higher priority is already
  1858. * scheduled. Needs to be called with ccwdev lock held.
  1859. */
  1860. void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
  1861. {
  1862. CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
  1863. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  1864. todo);
  1865. if (cdev->private->todo >= todo)
  1866. return;
  1867. cdev->private->todo = todo;
  1868. /* Get workqueue ref. */
  1869. if (!get_device(&cdev->dev))
  1870. return;
  1871. if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
  1872. /* Already queued, release workqueue ref. */
  1873. put_device(&cdev->dev);
  1874. }
  1875. }
  1876. /**
  1877. * ccw_device_siosl() - initiate logging
  1878. * @cdev: ccw device
  1879. *
  1880. * This function is used to invoke model-dependent logging within the channel
  1881. * subsystem.
  1882. */
  1883. int ccw_device_siosl(struct ccw_device *cdev)
  1884. {
  1885. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1886. return chsc_siosl(sch->schid);
  1887. }
  1888. EXPORT_SYMBOL_GPL(ccw_device_siosl);
  1889. MODULE_LICENSE("GPL");
  1890. EXPORT_SYMBOL(ccw_device_set_online);
  1891. EXPORT_SYMBOL(ccw_device_set_offline);
  1892. EXPORT_SYMBOL(ccw_driver_register);
  1893. EXPORT_SYMBOL(ccw_driver_unregister);
  1894. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1895. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);