device.c 54 KB

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