device.c 56 KB

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