device.c 53 KB

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