device.c 54 KB

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