device.c 54 KB

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