device.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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. cdev = ERR_PTR(ret);
  677. }
  678. return cdev;
  679. }
  680. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  681. static void sch_attach_device(struct subchannel *sch,
  682. struct ccw_device *cdev)
  683. {
  684. css_update_ssd_info(sch);
  685. spin_lock_irq(sch->lock);
  686. sch_set_cdev(sch, cdev);
  687. cdev->private->schid = sch->schid;
  688. cdev->ccwlock = sch->lock;
  689. ccw_device_trigger_reprobe(cdev);
  690. spin_unlock_irq(sch->lock);
  691. }
  692. static void sch_attach_disconnected_device(struct subchannel *sch,
  693. struct ccw_device *cdev)
  694. {
  695. struct subchannel *other_sch;
  696. int ret;
  697. /* Get reference for new parent. */
  698. if (!get_device(&sch->dev))
  699. return;
  700. other_sch = to_subchannel(cdev->dev.parent);
  701. /* Note: device_move() changes cdev->dev.parent */
  702. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  703. if (ret) {
  704. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  705. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  706. cdev->private->dev_id.devno, ret);
  707. /* Put reference for new parent. */
  708. put_device(&sch->dev);
  709. return;
  710. }
  711. sch_set_cdev(other_sch, NULL);
  712. /* No need to keep a subchannel without ccw device around. */
  713. css_sch_device_unregister(other_sch);
  714. sch_attach_device(sch, cdev);
  715. /* Put reference for old parent. */
  716. put_device(&other_sch->dev);
  717. }
  718. static void sch_attach_orphaned_device(struct subchannel *sch,
  719. struct ccw_device *cdev)
  720. {
  721. int ret;
  722. struct subchannel *pseudo_sch;
  723. /* Get reference for new parent. */
  724. if (!get_device(&sch->dev))
  725. return;
  726. pseudo_sch = to_subchannel(cdev->dev.parent);
  727. /*
  728. * Try to move the ccw device to its new subchannel.
  729. * Note: device_move() changes cdev->dev.parent
  730. */
  731. ret = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  732. if (ret) {
  733. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  734. "failed (ret=%d)!\n",
  735. cdev->private->dev_id.ssid,
  736. cdev->private->dev_id.devno, ret);
  737. /* Put reference for new parent. */
  738. put_device(&sch->dev);
  739. return;
  740. }
  741. sch_attach_device(sch, cdev);
  742. /* Put reference on pseudo subchannel. */
  743. put_device(&pseudo_sch->dev);
  744. }
  745. static void sch_create_and_recog_new_device(struct subchannel *sch)
  746. {
  747. struct ccw_device *cdev;
  748. /* Need to allocate a new ccw device. */
  749. cdev = io_subchannel_create_ccwdev(sch);
  750. if (IS_ERR(cdev)) {
  751. /* OK, we did everything we could... */
  752. css_sch_device_unregister(sch);
  753. return;
  754. }
  755. spin_lock_irq(sch->lock);
  756. sch_set_cdev(sch, cdev);
  757. spin_unlock_irq(sch->lock);
  758. /* Start recognition for the new ccw device. */
  759. if (io_subchannel_recog(cdev, sch)) {
  760. spin_lock_irq(sch->lock);
  761. sch_set_cdev(sch, NULL);
  762. spin_unlock_irq(sch->lock);
  763. css_sch_device_unregister(sch);
  764. /* Put reference from io_subchannel_create_ccwdev(). */
  765. put_device(&sch->dev);
  766. /* Give up initial reference. */
  767. put_device(&cdev->dev);
  768. }
  769. }
  770. void ccw_device_move_to_orphanage(struct work_struct *work)
  771. {
  772. struct ccw_device_private *priv;
  773. struct ccw_device *cdev;
  774. struct ccw_device *replacing_cdev;
  775. struct subchannel *sch;
  776. int ret;
  777. struct channel_subsystem *css;
  778. struct ccw_dev_id dev_id;
  779. priv = container_of(work, struct ccw_device_private, kick_work);
  780. cdev = priv->cdev;
  781. sch = to_subchannel(cdev->dev.parent);
  782. css = to_css(sch->dev.parent);
  783. dev_id.devno = sch->schib.pmcw.dev;
  784. dev_id.ssid = sch->schid.ssid;
  785. /* Increase refcount for pseudo subchannel. */
  786. get_device(&css->pseudo_subchannel->dev);
  787. /*
  788. * Move the orphaned ccw device to the orphanage so the replacing
  789. * ccw device can take its place on the subchannel.
  790. * Note: device_move() changes cdev->dev.parent
  791. */
  792. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev,
  793. DPM_ORDER_NONE);
  794. if (ret) {
  795. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  796. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  797. cdev->private->dev_id.devno, ret);
  798. /* Decrease refcount for pseudo subchannel again. */
  799. put_device(&css->pseudo_subchannel->dev);
  800. return;
  801. }
  802. cdev->ccwlock = css->pseudo_subchannel->lock;
  803. /*
  804. * Search for the replacing ccw device
  805. * - among the disconnected devices
  806. * - in the orphanage
  807. */
  808. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  809. if (replacing_cdev) {
  810. sch_attach_disconnected_device(sch, replacing_cdev);
  811. /* Release reference from get_disc_ccwdev_by_dev_id() */
  812. put_device(&replacing_cdev->dev);
  813. /* Release reference of subchannel from old cdev. */
  814. put_device(&sch->dev);
  815. return;
  816. }
  817. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  818. if (replacing_cdev) {
  819. sch_attach_orphaned_device(sch, replacing_cdev);
  820. /* Release reference from get_orphaned_ccwdev_by_dev_id() */
  821. put_device(&replacing_cdev->dev);
  822. /* Release reference of subchannel from old cdev. */
  823. put_device(&sch->dev);
  824. return;
  825. }
  826. sch_create_and_recog_new_device(sch);
  827. /* Release reference of subchannel from old cdev. */
  828. put_device(&sch->dev);
  829. }
  830. /*
  831. * Register recognized device.
  832. */
  833. static void
  834. io_subchannel_register(struct work_struct *work)
  835. {
  836. struct ccw_device_private *priv;
  837. struct ccw_device *cdev;
  838. struct subchannel *sch;
  839. int ret;
  840. unsigned long flags;
  841. priv = container_of(work, struct ccw_device_private, kick_work);
  842. cdev = priv->cdev;
  843. sch = to_subchannel(cdev->dev.parent);
  844. /*
  845. * Check if subchannel is still registered. It may have become
  846. * unregistered if a machine check hit us after finishing
  847. * device recognition but before the register work could be
  848. * queued.
  849. */
  850. if (!device_is_registered(&sch->dev))
  851. goto out_err;
  852. css_update_ssd_info(sch);
  853. /*
  854. * io_subchannel_register() will also be called after device
  855. * recognition has been done for a boxed device (which will already
  856. * be registered). We need to reprobe since we may now have sense id
  857. * information.
  858. */
  859. if (device_is_registered(&cdev->dev)) {
  860. if (!cdev->drv) {
  861. ret = device_reprobe(&cdev->dev);
  862. if (ret)
  863. /* We can't do much here. */
  864. CIO_MSG_EVENT(0, "device_reprobe() returned"
  865. " %d for 0.%x.%04x\n", ret,
  866. cdev->private->dev_id.ssid,
  867. cdev->private->dev_id.devno);
  868. }
  869. goto out;
  870. }
  871. /*
  872. * Now we know this subchannel will stay, we can throw
  873. * our delayed uevent.
  874. */
  875. dev_set_uevent_suppress(&sch->dev, 0);
  876. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  877. /* make it known to the system */
  878. ret = ccw_device_register(cdev);
  879. if (ret) {
  880. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  881. cdev->private->dev_id.ssid,
  882. cdev->private->dev_id.devno, ret);
  883. spin_lock_irqsave(sch->lock, flags);
  884. sch_set_cdev(sch, NULL);
  885. spin_unlock_irqrestore(sch->lock, flags);
  886. /* Release initial device reference. */
  887. put_device(&cdev->dev);
  888. goto out_err;
  889. }
  890. out:
  891. cdev->private->flags.recog_done = 1;
  892. wake_up(&cdev->private->wait_q);
  893. out_err:
  894. /* Release reference for workqueue processing. */
  895. put_device(&cdev->dev);
  896. if (atomic_dec_and_test(&ccw_device_init_count))
  897. wake_up(&ccw_device_init_wq);
  898. }
  899. static void ccw_device_call_sch_unregister(struct work_struct *work)
  900. {
  901. struct ccw_device_private *priv;
  902. struct ccw_device *cdev;
  903. struct subchannel *sch;
  904. priv = container_of(work, struct ccw_device_private, kick_work);
  905. cdev = priv->cdev;
  906. /* Get subchannel reference for local processing. */
  907. if (!get_device(cdev->dev.parent))
  908. return;
  909. sch = to_subchannel(cdev->dev.parent);
  910. css_sch_device_unregister(sch);
  911. /* Release cdev reference for workqueue processing.*/
  912. put_device(&cdev->dev);
  913. /* Release subchannel reference for local processing. */
  914. put_device(&sch->dev);
  915. }
  916. void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
  917. {
  918. PREPARE_WORK(&cdev->private->kick_work,
  919. ccw_device_call_sch_unregister);
  920. queue_work(slow_path_wq, &cdev->private->kick_work);
  921. }
  922. /*
  923. * subchannel recognition done. Called from the state machine.
  924. */
  925. void
  926. io_subchannel_recog_done(struct ccw_device *cdev)
  927. {
  928. if (css_init_done == 0) {
  929. cdev->private->flags.recog_done = 1;
  930. return;
  931. }
  932. switch (cdev->private->state) {
  933. case DEV_STATE_BOXED:
  934. /* Device did not respond in time. */
  935. case DEV_STATE_NOT_OPER:
  936. cdev->private->flags.recog_done = 1;
  937. /* Remove device found not operational. */
  938. if (!get_device(&cdev->dev))
  939. break;
  940. ccw_device_schedule_sch_unregister(cdev);
  941. if (atomic_dec_and_test(&ccw_device_init_count))
  942. wake_up(&ccw_device_init_wq);
  943. break;
  944. case DEV_STATE_OFFLINE:
  945. /*
  946. * We can't register the device in interrupt context so
  947. * we schedule a work item.
  948. */
  949. if (!get_device(&cdev->dev))
  950. break;
  951. PREPARE_WORK(&cdev->private->kick_work,
  952. io_subchannel_register);
  953. queue_work(slow_path_wq, &cdev->private->kick_work);
  954. break;
  955. }
  956. }
  957. static int
  958. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  959. {
  960. int rc;
  961. struct ccw_device_private *priv;
  962. sch_set_cdev(sch, cdev);
  963. cdev->ccwlock = sch->lock;
  964. /* Init private data. */
  965. priv = cdev->private;
  966. priv->dev_id.devno = sch->schib.pmcw.dev;
  967. priv->dev_id.ssid = sch->schid.ssid;
  968. priv->schid = sch->schid;
  969. priv->state = DEV_STATE_NOT_OPER;
  970. INIT_LIST_HEAD(&priv->cmb_list);
  971. init_waitqueue_head(&priv->wait_q);
  972. init_timer(&priv->timer);
  973. /* Set an initial name for the device. */
  974. if (cio_is_console(sch->schid))
  975. cdev->dev.init_name = cio_get_console_cdev_name(sch);
  976. else
  977. dev_set_name(&cdev->dev, "0.%x.%04x",
  978. sch->schid.ssid, sch->schib.pmcw.dev);
  979. /* Increase counter of devices currently in recognition. */
  980. atomic_inc(&ccw_device_init_count);
  981. /* Start async. device sensing. */
  982. spin_lock_irq(sch->lock);
  983. rc = ccw_device_recognition(cdev);
  984. spin_unlock_irq(sch->lock);
  985. if (rc) {
  986. if (atomic_dec_and_test(&ccw_device_init_count))
  987. wake_up(&ccw_device_init_wq);
  988. }
  989. return rc;
  990. }
  991. static void ccw_device_move_to_sch(struct work_struct *work)
  992. {
  993. struct ccw_device_private *priv;
  994. int rc;
  995. struct subchannel *sch;
  996. struct ccw_device *cdev;
  997. struct subchannel *former_parent;
  998. priv = container_of(work, struct ccw_device_private, kick_work);
  999. sch = priv->sch;
  1000. cdev = priv->cdev;
  1001. former_parent = to_subchannel(cdev->dev.parent);
  1002. /* Get reference for new parent. */
  1003. if (!get_device(&sch->dev))
  1004. return;
  1005. mutex_lock(&sch->reg_mutex);
  1006. /*
  1007. * Try to move the ccw device to its new subchannel.
  1008. * Note: device_move() changes cdev->dev.parent
  1009. */
  1010. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  1011. mutex_unlock(&sch->reg_mutex);
  1012. if (rc) {
  1013. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  1014. "0.%x.%04x failed (ret=%d)!\n",
  1015. cdev->private->dev_id.ssid,
  1016. cdev->private->dev_id.devno, sch->schid.ssid,
  1017. sch->schid.sch_no, rc);
  1018. css_sch_device_unregister(sch);
  1019. /* Put reference for new parent again. */
  1020. put_device(&sch->dev);
  1021. goto out;
  1022. }
  1023. if (!sch_is_pseudo_sch(former_parent)) {
  1024. spin_lock_irq(former_parent->lock);
  1025. sch_set_cdev(former_parent, NULL);
  1026. spin_unlock_irq(former_parent->lock);
  1027. css_sch_device_unregister(former_parent);
  1028. /* Reset intparm to zeroes. */
  1029. former_parent->config.intparm = 0;
  1030. cio_commit_config(former_parent);
  1031. }
  1032. sch_attach_device(sch, cdev);
  1033. out:
  1034. /* Put reference for old parent. */
  1035. put_device(&former_parent->dev);
  1036. put_device(&cdev->dev);
  1037. }
  1038. static void io_subchannel_irq(struct subchannel *sch)
  1039. {
  1040. struct ccw_device *cdev;
  1041. cdev = sch_get_cdev(sch);
  1042. CIO_TRACE_EVENT(6, "IRQ");
  1043. CIO_TRACE_EVENT(6, dev_name(&sch->dev));
  1044. if (cdev)
  1045. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  1046. }
  1047. void io_subchannel_init_config(struct subchannel *sch)
  1048. {
  1049. memset(&sch->config, 0, sizeof(sch->config));
  1050. sch->config.csense = 1;
  1051. /* Use subchannel mp mode when there is more than 1 installed CHPID. */
  1052. if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
  1053. sch->config.mp = 1;
  1054. }
  1055. static void io_subchannel_init_fields(struct subchannel *sch)
  1056. {
  1057. if (cio_is_console(sch->schid))
  1058. sch->opm = 0xff;
  1059. else
  1060. sch->opm = chp_get_sch_opm(sch);
  1061. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  1062. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  1063. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1064. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1065. sch->schib.pmcw.dev, sch->schid.ssid,
  1066. sch->schid.sch_no, sch->schib.pmcw.pim,
  1067. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1068. io_subchannel_init_config(sch);
  1069. }
  1070. static void io_subchannel_do_unreg(struct work_struct *work)
  1071. {
  1072. struct subchannel *sch;
  1073. sch = container_of(work, struct subchannel, work);
  1074. css_sch_device_unregister(sch);
  1075. put_device(&sch->dev);
  1076. }
  1077. /* Schedule unregister if we have no cdev. */
  1078. static void io_subchannel_schedule_removal(struct subchannel *sch)
  1079. {
  1080. get_device(&sch->dev);
  1081. INIT_WORK(&sch->work, io_subchannel_do_unreg);
  1082. queue_work(slow_path_wq, &sch->work);
  1083. }
  1084. /*
  1085. * Note: We always return 0 so that we bind to the device even on error.
  1086. * This is needed so that our remove function is called on unregister.
  1087. */
  1088. static int io_subchannel_probe(struct subchannel *sch)
  1089. {
  1090. struct ccw_device *cdev;
  1091. int rc;
  1092. unsigned long flags;
  1093. struct ccw_dev_id dev_id;
  1094. cdev = sch_get_cdev(sch);
  1095. if (cdev) {
  1096. rc = sysfs_create_group(&sch->dev.kobj,
  1097. &io_subchannel_attr_group);
  1098. if (rc)
  1099. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1100. "attributes for subchannel "
  1101. "0.%x.%04x (rc=%d)\n",
  1102. sch->schid.ssid, sch->schid.sch_no, rc);
  1103. /*
  1104. * This subchannel already has an associated ccw_device.
  1105. * Throw the delayed uevent for the subchannel, register
  1106. * the ccw_device and exit. This happens for all early
  1107. * devices, e.g. the console.
  1108. */
  1109. dev_set_uevent_suppress(&sch->dev, 0);
  1110. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1111. cdev->dev.groups = ccwdev_attr_groups;
  1112. device_initialize(&cdev->dev);
  1113. ccw_device_register(cdev);
  1114. /*
  1115. * Check if the device is already online. If it is
  1116. * the reference count needs to be corrected since we
  1117. * didn't obtain a reference in ccw_device_set_online.
  1118. */
  1119. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1120. cdev->private->state != DEV_STATE_OFFLINE &&
  1121. cdev->private->state != DEV_STATE_BOXED)
  1122. get_device(&cdev->dev);
  1123. return 0;
  1124. }
  1125. io_subchannel_init_fields(sch);
  1126. rc = cio_commit_config(sch);
  1127. if (rc)
  1128. goto out_schedule;
  1129. rc = sysfs_create_group(&sch->dev.kobj,
  1130. &io_subchannel_attr_group);
  1131. if (rc)
  1132. goto out_schedule;
  1133. /* Allocate I/O subchannel private data. */
  1134. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1135. GFP_KERNEL | GFP_DMA);
  1136. if (!sch->private)
  1137. goto out_err;
  1138. /*
  1139. * First check if a fitting device may be found amongst the
  1140. * disconnected devices or in the orphanage.
  1141. */
  1142. dev_id.devno = sch->schib.pmcw.dev;
  1143. dev_id.ssid = sch->schid.ssid;
  1144. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1145. if (!cdev)
  1146. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1147. &dev_id);
  1148. if (cdev) {
  1149. /*
  1150. * Schedule moving the device until when we have a registered
  1151. * subchannel to move to and succeed the probe. We can
  1152. * unregister later again, when the probe is through.
  1153. */
  1154. cdev->private->sch = sch;
  1155. PREPARE_WORK(&cdev->private->kick_work,
  1156. ccw_device_move_to_sch);
  1157. queue_work(slow_path_wq, &cdev->private->kick_work);
  1158. return 0;
  1159. }
  1160. cdev = io_subchannel_create_ccwdev(sch);
  1161. if (IS_ERR(cdev))
  1162. goto out_err;
  1163. rc = io_subchannel_recog(cdev, sch);
  1164. if (rc) {
  1165. spin_lock_irqsave(sch->lock, flags);
  1166. io_subchannel_recog_done(cdev);
  1167. spin_unlock_irqrestore(sch->lock, flags);
  1168. }
  1169. return 0;
  1170. out_err:
  1171. kfree(sch->private);
  1172. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1173. out_schedule:
  1174. io_subchannel_schedule_removal(sch);
  1175. return 0;
  1176. }
  1177. static int
  1178. io_subchannel_remove (struct subchannel *sch)
  1179. {
  1180. struct ccw_device *cdev;
  1181. unsigned long flags;
  1182. cdev = sch_get_cdev(sch);
  1183. if (!cdev)
  1184. return 0;
  1185. /* Set ccw device to not operational and drop reference. */
  1186. spin_lock_irqsave(cdev->ccwlock, flags);
  1187. sch_set_cdev(sch, NULL);
  1188. cdev->private->state = DEV_STATE_NOT_OPER;
  1189. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1190. ccw_device_unregister(cdev);
  1191. put_device(&cdev->dev);
  1192. kfree(sch->private);
  1193. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1194. return 0;
  1195. }
  1196. static int io_subchannel_notify(struct subchannel *sch, int event)
  1197. {
  1198. struct ccw_device *cdev;
  1199. cdev = sch_get_cdev(sch);
  1200. if (!cdev)
  1201. return 0;
  1202. return ccw_device_notify(cdev, event);
  1203. }
  1204. static void io_subchannel_verify(struct subchannel *sch)
  1205. {
  1206. struct ccw_device *cdev;
  1207. cdev = sch_get_cdev(sch);
  1208. if (cdev)
  1209. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1210. }
  1211. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1212. {
  1213. if (cio_update_schib(sch))
  1214. return 0;
  1215. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1216. return 1;
  1217. return 0;
  1218. }
  1219. static void terminate_internal_io(struct subchannel *sch,
  1220. struct ccw_device *cdev)
  1221. {
  1222. if (cio_clear(sch)) {
  1223. /* Recheck device in case clear failed. */
  1224. sch->lpm = 0;
  1225. if (cdev->online)
  1226. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1227. else
  1228. css_schedule_eval(sch->schid);
  1229. return;
  1230. }
  1231. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1232. /* Request retry of internal operation. */
  1233. cdev->private->flags.intretry = 1;
  1234. /* Call handler. */
  1235. if (cdev->handler)
  1236. cdev->handler(cdev, cdev->private->intparm,
  1237. ERR_PTR(-EIO));
  1238. }
  1239. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1240. {
  1241. struct ccw_device *cdev;
  1242. cdev = sch_get_cdev(sch);
  1243. if (!cdev)
  1244. return;
  1245. if (check_for_io_on_path(sch, mask)) {
  1246. if (cdev->private->state == DEV_STATE_ONLINE)
  1247. ccw_device_kill_io(cdev);
  1248. else {
  1249. terminate_internal_io(sch, cdev);
  1250. /* Re-start path verification. */
  1251. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1252. }
  1253. } else
  1254. /* trigger path verification. */
  1255. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1256. }
  1257. static int io_subchannel_chp_event(struct subchannel *sch,
  1258. struct chp_link *link, int event)
  1259. {
  1260. int mask;
  1261. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1262. if (!mask)
  1263. return 0;
  1264. switch (event) {
  1265. case CHP_VARY_OFF:
  1266. sch->opm &= ~mask;
  1267. sch->lpm &= ~mask;
  1268. io_subchannel_terminate_path(sch, mask);
  1269. break;
  1270. case CHP_VARY_ON:
  1271. sch->opm |= mask;
  1272. sch->lpm |= mask;
  1273. io_subchannel_verify(sch);
  1274. break;
  1275. case CHP_OFFLINE:
  1276. if (cio_update_schib(sch))
  1277. return -ENODEV;
  1278. io_subchannel_terminate_path(sch, mask);
  1279. break;
  1280. case CHP_ONLINE:
  1281. if (cio_update_schib(sch))
  1282. return -ENODEV;
  1283. sch->lpm |= mask & sch->opm;
  1284. io_subchannel_verify(sch);
  1285. break;
  1286. }
  1287. return 0;
  1288. }
  1289. static void
  1290. io_subchannel_shutdown(struct subchannel *sch)
  1291. {
  1292. struct ccw_device *cdev;
  1293. int ret;
  1294. cdev = sch_get_cdev(sch);
  1295. if (cio_is_console(sch->schid))
  1296. return;
  1297. if (!sch->schib.pmcw.ena)
  1298. /* Nothing to do. */
  1299. return;
  1300. ret = cio_disable_subchannel(sch);
  1301. if (ret != -EBUSY)
  1302. /* Subchannel is disabled, we're done. */
  1303. return;
  1304. cdev->private->state = DEV_STATE_QUIESCE;
  1305. if (cdev->handler)
  1306. cdev->handler(cdev, cdev->private->intparm,
  1307. ERR_PTR(-EIO));
  1308. ret = ccw_device_cancel_halt_clear(cdev);
  1309. if (ret == -EBUSY) {
  1310. ccw_device_set_timeout(cdev, HZ/10);
  1311. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1312. }
  1313. cio_disable_subchannel(sch);
  1314. }
  1315. static int io_subchannel_get_status(struct subchannel *sch)
  1316. {
  1317. struct schib schib;
  1318. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1319. return CIO_GONE;
  1320. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1321. return CIO_REVALIDATE;
  1322. if (!sch->lpm)
  1323. return CIO_NO_PATH;
  1324. return CIO_OPER;
  1325. }
  1326. static int device_is_disconnected(struct ccw_device *cdev)
  1327. {
  1328. if (!cdev)
  1329. return 0;
  1330. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1331. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1332. }
  1333. static int recovery_check(struct device *dev, void *data)
  1334. {
  1335. struct ccw_device *cdev = to_ccwdev(dev);
  1336. int *redo = data;
  1337. spin_lock_irq(cdev->ccwlock);
  1338. switch (cdev->private->state) {
  1339. case DEV_STATE_DISCONNECTED:
  1340. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1341. cdev->private->dev_id.ssid,
  1342. cdev->private->dev_id.devno);
  1343. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1344. *redo = 1;
  1345. break;
  1346. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1347. *redo = 1;
  1348. break;
  1349. }
  1350. spin_unlock_irq(cdev->ccwlock);
  1351. return 0;
  1352. }
  1353. static void recovery_work_func(struct work_struct *unused)
  1354. {
  1355. int redo = 0;
  1356. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1357. if (redo) {
  1358. spin_lock_irq(&recovery_lock);
  1359. if (!timer_pending(&recovery_timer)) {
  1360. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1361. recovery_phase++;
  1362. mod_timer(&recovery_timer, jiffies +
  1363. recovery_delay[recovery_phase] * HZ);
  1364. }
  1365. spin_unlock_irq(&recovery_lock);
  1366. } else
  1367. CIO_MSG_EVENT(4, "recovery: end\n");
  1368. }
  1369. static DECLARE_WORK(recovery_work, recovery_work_func);
  1370. static void recovery_func(unsigned long data)
  1371. {
  1372. /*
  1373. * We can't do our recovery in softirq context and it's not
  1374. * performance critical, so we schedule it.
  1375. */
  1376. schedule_work(&recovery_work);
  1377. }
  1378. static void ccw_device_schedule_recovery(void)
  1379. {
  1380. unsigned long flags;
  1381. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1382. spin_lock_irqsave(&recovery_lock, flags);
  1383. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1384. recovery_phase = 0;
  1385. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1386. }
  1387. spin_unlock_irqrestore(&recovery_lock, flags);
  1388. }
  1389. static int purge_fn(struct device *dev, void *data)
  1390. {
  1391. struct ccw_device *cdev = to_ccwdev(dev);
  1392. struct ccw_device_private *priv = cdev->private;
  1393. int unreg;
  1394. spin_lock_irq(cdev->ccwlock);
  1395. unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
  1396. (priv->state == DEV_STATE_OFFLINE);
  1397. spin_unlock_irq(cdev->ccwlock);
  1398. if (!unreg)
  1399. goto out;
  1400. if (!get_device(&cdev->dev))
  1401. goto out;
  1402. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
  1403. priv->dev_id.devno);
  1404. ccw_device_schedule_sch_unregister(cdev);
  1405. out:
  1406. /* Abort loop in case of pending signal. */
  1407. if (signal_pending(current))
  1408. return -EINTR;
  1409. return 0;
  1410. }
  1411. /**
  1412. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1413. *
  1414. * Unregister all ccw devices that are offline and on the blacklist.
  1415. */
  1416. int ccw_purge_blacklisted(void)
  1417. {
  1418. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1419. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1420. return 0;
  1421. }
  1422. static void device_set_disconnected(struct ccw_device *cdev)
  1423. {
  1424. if (!cdev)
  1425. return;
  1426. ccw_device_set_timeout(cdev, 0);
  1427. cdev->private->flags.fake_irb = 0;
  1428. cdev->private->state = DEV_STATE_DISCONNECTED;
  1429. if (cdev->online)
  1430. ccw_device_schedule_recovery();
  1431. }
  1432. void ccw_device_set_notoper(struct ccw_device *cdev)
  1433. {
  1434. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1435. CIO_TRACE_EVENT(2, "notoper");
  1436. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1437. ccw_device_set_timeout(cdev, 0);
  1438. cio_disable_subchannel(sch);
  1439. cdev->private->state = DEV_STATE_NOT_OPER;
  1440. }
  1441. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1442. {
  1443. int event, ret, disc;
  1444. unsigned long flags;
  1445. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
  1446. struct ccw_device *cdev;
  1447. spin_lock_irqsave(sch->lock, flags);
  1448. cdev = sch_get_cdev(sch);
  1449. disc = device_is_disconnected(cdev);
  1450. if (disc && slow) {
  1451. /* Disconnected devices are evaluated directly only.*/
  1452. spin_unlock_irqrestore(sch->lock, flags);
  1453. return 0;
  1454. }
  1455. /* No interrupt after machine check - kill pending timers. */
  1456. if (cdev)
  1457. ccw_device_set_timeout(cdev, 0);
  1458. if (!disc && !slow) {
  1459. /* Non-disconnected devices are evaluated on the slow path. */
  1460. spin_unlock_irqrestore(sch->lock, flags);
  1461. return -EAGAIN;
  1462. }
  1463. event = io_subchannel_get_status(sch);
  1464. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1465. sch->schid.ssid, sch->schid.sch_no, event,
  1466. disc ? "disconnected" : "normal",
  1467. slow ? "slow" : "fast");
  1468. /* Analyze subchannel status. */
  1469. action = NONE;
  1470. switch (event) {
  1471. case CIO_NO_PATH:
  1472. if (disc) {
  1473. /* Check if paths have become available. */
  1474. action = REPROBE;
  1475. break;
  1476. }
  1477. /* fall through */
  1478. case CIO_GONE:
  1479. /* Ask driver what to do with device. */
  1480. if (io_subchannel_notify(sch, event))
  1481. action = DISC;
  1482. else
  1483. action = UNREGISTER;
  1484. break;
  1485. case CIO_REVALIDATE:
  1486. /* Device will be removed, so no notify necessary. */
  1487. if (disc)
  1488. /* Reprobe because immediate unregister might block. */
  1489. action = REPROBE;
  1490. else
  1491. action = UNREGISTER_PROBE;
  1492. break;
  1493. case CIO_OPER:
  1494. if (disc)
  1495. /* Get device operational again. */
  1496. action = REPROBE;
  1497. break;
  1498. }
  1499. /* Perform action. */
  1500. ret = 0;
  1501. switch (action) {
  1502. case UNREGISTER:
  1503. case UNREGISTER_PROBE:
  1504. ccw_device_set_notoper(cdev);
  1505. /* Unregister device (will use subchannel lock). */
  1506. spin_unlock_irqrestore(sch->lock, flags);
  1507. css_sch_device_unregister(sch);
  1508. spin_lock_irqsave(sch->lock, flags);
  1509. break;
  1510. case REPROBE:
  1511. ccw_device_trigger_reprobe(cdev);
  1512. break;
  1513. case DISC:
  1514. device_set_disconnected(cdev);
  1515. break;
  1516. default:
  1517. break;
  1518. }
  1519. spin_unlock_irqrestore(sch->lock, flags);
  1520. /* Probe if necessary. */
  1521. if (action == UNREGISTER_PROBE)
  1522. ret = css_probe_device(sch->schid);
  1523. return ret;
  1524. }
  1525. #ifdef CONFIG_CCW_CONSOLE
  1526. static struct ccw_device console_cdev;
  1527. static char console_cdev_name[10] = "0.x.xxxx";
  1528. static struct ccw_device_private console_private;
  1529. static int console_cdev_in_use;
  1530. static DEFINE_SPINLOCK(ccw_console_lock);
  1531. spinlock_t * cio_get_console_lock(void)
  1532. {
  1533. return &ccw_console_lock;
  1534. }
  1535. static int ccw_device_console_enable(struct ccw_device *cdev,
  1536. struct subchannel *sch)
  1537. {
  1538. int rc;
  1539. /* Attach subchannel private data. */
  1540. sch->private = cio_get_console_priv();
  1541. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1542. io_subchannel_init_fields(sch);
  1543. rc = cio_commit_config(sch);
  1544. if (rc)
  1545. return rc;
  1546. sch->driver = &io_subchannel_driver;
  1547. /* Initialize the ccw_device structure. */
  1548. cdev->dev.parent= &sch->dev;
  1549. rc = io_subchannel_recog(cdev, sch);
  1550. if (rc)
  1551. return rc;
  1552. /* Now wait for the async. recognition to come to an end. */
  1553. spin_lock_irq(cdev->ccwlock);
  1554. while (!dev_fsm_final_state(cdev))
  1555. wait_cons_dev();
  1556. rc = -EIO;
  1557. if (cdev->private->state != DEV_STATE_OFFLINE)
  1558. goto out_unlock;
  1559. ccw_device_online(cdev);
  1560. while (!dev_fsm_final_state(cdev))
  1561. wait_cons_dev();
  1562. if (cdev->private->state != DEV_STATE_ONLINE)
  1563. goto out_unlock;
  1564. rc = 0;
  1565. out_unlock:
  1566. spin_unlock_irq(cdev->ccwlock);
  1567. return 0;
  1568. }
  1569. struct ccw_device *
  1570. ccw_device_probe_console(void)
  1571. {
  1572. struct subchannel *sch;
  1573. int ret;
  1574. if (xchg(&console_cdev_in_use, 1) != 0)
  1575. return ERR_PTR(-EBUSY);
  1576. sch = cio_probe_console();
  1577. if (IS_ERR(sch)) {
  1578. console_cdev_in_use = 0;
  1579. return (void *) sch;
  1580. }
  1581. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1582. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1583. console_cdev.private = &console_private;
  1584. console_private.cdev = &console_cdev;
  1585. ret = ccw_device_console_enable(&console_cdev, sch);
  1586. if (ret) {
  1587. cio_release_console();
  1588. console_cdev_in_use = 0;
  1589. return ERR_PTR(ret);
  1590. }
  1591. console_cdev.online = 1;
  1592. return &console_cdev;
  1593. }
  1594. static int ccw_device_pm_restore(struct device *dev);
  1595. int ccw_device_force_console(void)
  1596. {
  1597. if (!console_cdev_in_use)
  1598. return -ENODEV;
  1599. return ccw_device_pm_restore(&console_cdev.dev);
  1600. }
  1601. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1602. const char *cio_get_console_cdev_name(struct subchannel *sch)
  1603. {
  1604. snprintf(console_cdev_name, 10, "0.%x.%04x",
  1605. sch->schid.ssid, sch->schib.pmcw.dev);
  1606. return (const char *)console_cdev_name;
  1607. }
  1608. #endif
  1609. /*
  1610. * get ccw_device matching the busid, but only if owned by cdrv
  1611. */
  1612. static int
  1613. __ccwdev_check_busid(struct device *dev, void *id)
  1614. {
  1615. char *bus_id;
  1616. bus_id = id;
  1617. return (strcmp(bus_id, dev_name(dev)) == 0);
  1618. }
  1619. /**
  1620. * get_ccwdev_by_busid() - obtain device from a bus id
  1621. * @cdrv: driver the device is owned by
  1622. * @bus_id: bus id of the device to be searched
  1623. *
  1624. * This function searches all devices owned by @cdrv for a device with a bus
  1625. * id matching @bus_id.
  1626. * Returns:
  1627. * If a match is found, its reference count of the found device is increased
  1628. * and it is returned; else %NULL is returned.
  1629. */
  1630. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1631. const char *bus_id)
  1632. {
  1633. struct device *dev;
  1634. struct device_driver *drv;
  1635. drv = get_driver(&cdrv->driver);
  1636. if (!drv)
  1637. return NULL;
  1638. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1639. __ccwdev_check_busid);
  1640. put_driver(drv);
  1641. return dev ? to_ccwdev(dev) : NULL;
  1642. }
  1643. /************************** device driver handling ************************/
  1644. /* This is the implementation of the ccw_driver class. The probe, remove
  1645. * and release methods are initially very similar to the device_driver
  1646. * implementations, with the difference that they have ccw_device
  1647. * arguments.
  1648. *
  1649. * A ccw driver also contains the information that is needed for
  1650. * device matching.
  1651. */
  1652. static int
  1653. ccw_device_probe (struct device *dev)
  1654. {
  1655. struct ccw_device *cdev = to_ccwdev(dev);
  1656. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1657. int ret;
  1658. cdev->drv = cdrv; /* to let the driver call _set_online */
  1659. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1660. if (ret) {
  1661. cdev->drv = NULL;
  1662. return ret;
  1663. }
  1664. return 0;
  1665. }
  1666. static int
  1667. ccw_device_remove (struct device *dev)
  1668. {
  1669. struct ccw_device *cdev = to_ccwdev(dev);
  1670. struct ccw_driver *cdrv = cdev->drv;
  1671. int ret;
  1672. if (cdrv->remove)
  1673. cdrv->remove(cdev);
  1674. if (cdev->online) {
  1675. cdev->online = 0;
  1676. spin_lock_irq(cdev->ccwlock);
  1677. ret = ccw_device_offline(cdev);
  1678. spin_unlock_irq(cdev->ccwlock);
  1679. if (ret == 0)
  1680. wait_event(cdev->private->wait_q,
  1681. dev_fsm_final_state(cdev));
  1682. else
  1683. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1684. "device 0.%x.%04x\n",
  1685. ret, cdev->private->dev_id.ssid,
  1686. cdev->private->dev_id.devno);
  1687. /* Give up reference obtained in ccw_device_set_online(). */
  1688. put_device(&cdev->dev);
  1689. }
  1690. ccw_device_set_timeout(cdev, 0);
  1691. cdev->drv = NULL;
  1692. return 0;
  1693. }
  1694. static void ccw_device_shutdown(struct device *dev)
  1695. {
  1696. struct ccw_device *cdev;
  1697. cdev = to_ccwdev(dev);
  1698. if (cdev->drv && cdev->drv->shutdown)
  1699. cdev->drv->shutdown(cdev);
  1700. disable_cmf(cdev);
  1701. }
  1702. static int ccw_device_pm_prepare(struct device *dev)
  1703. {
  1704. struct ccw_device *cdev = to_ccwdev(dev);
  1705. if (work_pending(&cdev->private->kick_work))
  1706. return -EAGAIN;
  1707. /* Fail while device is being set online/offline. */
  1708. if (atomic_read(&cdev->private->onoff))
  1709. return -EAGAIN;
  1710. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1711. return cdev->drv->prepare(cdev);
  1712. return 0;
  1713. }
  1714. static void ccw_device_pm_complete(struct device *dev)
  1715. {
  1716. struct ccw_device *cdev = to_ccwdev(dev);
  1717. if (cdev->online && cdev->drv && cdev->drv->complete)
  1718. cdev->drv->complete(cdev);
  1719. }
  1720. static int ccw_device_pm_freeze(struct device *dev)
  1721. {
  1722. struct ccw_device *cdev = to_ccwdev(dev);
  1723. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1724. int ret, cm_enabled;
  1725. /* Fail suspend while device is in transistional state. */
  1726. if (!dev_fsm_final_state(cdev))
  1727. return -EAGAIN;
  1728. if (!cdev->online)
  1729. return 0;
  1730. if (cdev->drv && cdev->drv->freeze) {
  1731. ret = cdev->drv->freeze(cdev);
  1732. if (ret)
  1733. return ret;
  1734. }
  1735. spin_lock_irq(sch->lock);
  1736. cm_enabled = cdev->private->cmb != NULL;
  1737. spin_unlock_irq(sch->lock);
  1738. if (cm_enabled) {
  1739. /* Don't have the css write on memory. */
  1740. ret = ccw_set_cmf(cdev, 0);
  1741. if (ret)
  1742. return ret;
  1743. }
  1744. /* From here on, disallow device driver I/O. */
  1745. spin_lock_irq(sch->lock);
  1746. ret = cio_disable_subchannel(sch);
  1747. spin_unlock_irq(sch->lock);
  1748. return ret;
  1749. }
  1750. static int ccw_device_pm_thaw(struct device *dev)
  1751. {
  1752. struct ccw_device *cdev = to_ccwdev(dev);
  1753. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1754. int ret, cm_enabled;
  1755. if (!cdev->online)
  1756. return 0;
  1757. spin_lock_irq(sch->lock);
  1758. /* Allow device driver I/O again. */
  1759. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1760. cm_enabled = cdev->private->cmb != NULL;
  1761. spin_unlock_irq(sch->lock);
  1762. if (ret)
  1763. return ret;
  1764. if (cm_enabled) {
  1765. ret = ccw_set_cmf(cdev, 1);
  1766. if (ret)
  1767. return ret;
  1768. }
  1769. if (cdev->drv && cdev->drv->thaw)
  1770. ret = cdev->drv->thaw(cdev);
  1771. return ret;
  1772. }
  1773. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1774. {
  1775. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1776. int ret;
  1777. if (cio_is_console(sch->schid))
  1778. goto out;
  1779. /*
  1780. * While we were sleeping, devices may have gone or become
  1781. * available again. Kick re-detection.
  1782. */
  1783. spin_lock_irq(sch->lock);
  1784. cdev->private->flags.resuming = 1;
  1785. ret = ccw_device_recognition(cdev);
  1786. spin_unlock_irq(sch->lock);
  1787. if (ret) {
  1788. CIO_MSG_EVENT(0, "Couldn't start recognition for device "
  1789. "%s (ret=%d)\n", dev_name(&cdev->dev), ret);
  1790. spin_lock_irq(sch->lock);
  1791. cdev->private->state = DEV_STATE_DISCONNECTED;
  1792. spin_unlock_irq(sch->lock);
  1793. /* notify driver after the resume cb */
  1794. goto out;
  1795. }
  1796. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1797. cdev->private->state == DEV_STATE_DISCONNECTED);
  1798. out:
  1799. cdev->private->flags.resuming = 0;
  1800. }
  1801. static int resume_handle_boxed(struct ccw_device *cdev)
  1802. {
  1803. cdev->private->state = DEV_STATE_BOXED;
  1804. if (ccw_device_notify(cdev, CIO_BOXED))
  1805. return 0;
  1806. ccw_device_schedule_sch_unregister(cdev);
  1807. return -ENODEV;
  1808. }
  1809. static int resume_handle_disc(struct ccw_device *cdev)
  1810. {
  1811. cdev->private->state = DEV_STATE_DISCONNECTED;
  1812. if (ccw_device_notify(cdev, CIO_GONE))
  1813. return 0;
  1814. ccw_device_schedule_sch_unregister(cdev);
  1815. return -ENODEV;
  1816. }
  1817. static int ccw_device_pm_restore(struct device *dev)
  1818. {
  1819. struct ccw_device *cdev = to_ccwdev(dev);
  1820. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1821. int ret = 0, cm_enabled;
  1822. __ccw_device_pm_restore(cdev);
  1823. spin_lock_irq(sch->lock);
  1824. if (cio_is_console(sch->schid)) {
  1825. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1826. spin_unlock_irq(sch->lock);
  1827. goto out_restore;
  1828. }
  1829. cdev->private->flags.donotify = 0;
  1830. /* check recognition results */
  1831. switch (cdev->private->state) {
  1832. case DEV_STATE_OFFLINE:
  1833. break;
  1834. case DEV_STATE_BOXED:
  1835. ret = resume_handle_boxed(cdev);
  1836. spin_unlock_irq(sch->lock);
  1837. if (ret)
  1838. goto out;
  1839. goto out_restore;
  1840. case DEV_STATE_DISCONNECTED:
  1841. goto out_disc_unlock;
  1842. default:
  1843. goto out_unreg_unlock;
  1844. }
  1845. /* check if the device id has changed */
  1846. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1847. CIO_MSG_EVENT(0, "resume: sch %s: failed (devno changed from "
  1848. "%04x to %04x)\n", dev_name(&sch->dev),
  1849. cdev->private->dev_id.devno,
  1850. sch->schib.pmcw.dev);
  1851. goto out_unreg_unlock;
  1852. }
  1853. /* check if the device type has changed */
  1854. if (!ccw_device_test_sense_data(cdev)) {
  1855. ccw_device_update_sense_data(cdev);
  1856. PREPARE_WORK(&cdev->private->kick_work,
  1857. ccw_device_do_unbind_bind);
  1858. queue_work(ccw_device_work, &cdev->private->kick_work);
  1859. ret = -ENODEV;
  1860. goto out_unlock;
  1861. }
  1862. if (!cdev->online) {
  1863. ret = 0;
  1864. goto out_unlock;
  1865. }
  1866. ret = ccw_device_online(cdev);
  1867. if (ret)
  1868. goto out_disc_unlock;
  1869. cm_enabled = cdev->private->cmb != NULL;
  1870. spin_unlock_irq(sch->lock);
  1871. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1872. if (cdev->private->state != DEV_STATE_ONLINE) {
  1873. spin_lock_irq(sch->lock);
  1874. goto out_disc_unlock;
  1875. }
  1876. if (cm_enabled) {
  1877. ret = ccw_set_cmf(cdev, 1);
  1878. if (ret) {
  1879. CIO_MSG_EVENT(2, "resume: cdev %s: cmf failed "
  1880. "(rc=%d)\n", dev_name(&cdev->dev), ret);
  1881. ret = 0;
  1882. }
  1883. }
  1884. out_restore:
  1885. if (cdev->online && cdev->drv && cdev->drv->restore)
  1886. ret = cdev->drv->restore(cdev);
  1887. out:
  1888. return ret;
  1889. out_disc_unlock:
  1890. ret = resume_handle_disc(cdev);
  1891. spin_unlock_irq(sch->lock);
  1892. if (ret)
  1893. return ret;
  1894. goto out_restore;
  1895. out_unreg_unlock:
  1896. ccw_device_schedule_sch_unregister(cdev);
  1897. ret = -ENODEV;
  1898. out_unlock:
  1899. spin_unlock_irq(sch->lock);
  1900. return ret;
  1901. }
  1902. static struct dev_pm_ops ccw_pm_ops = {
  1903. .prepare = ccw_device_pm_prepare,
  1904. .complete = ccw_device_pm_complete,
  1905. .freeze = ccw_device_pm_freeze,
  1906. .thaw = ccw_device_pm_thaw,
  1907. .restore = ccw_device_pm_restore,
  1908. };
  1909. struct bus_type ccw_bus_type = {
  1910. .name = "ccw",
  1911. .match = ccw_bus_match,
  1912. .uevent = ccw_uevent,
  1913. .probe = ccw_device_probe,
  1914. .remove = ccw_device_remove,
  1915. .shutdown = ccw_device_shutdown,
  1916. .pm = &ccw_pm_ops,
  1917. };
  1918. /**
  1919. * ccw_driver_register() - register a ccw driver
  1920. * @cdriver: driver to be registered
  1921. *
  1922. * This function is mainly a wrapper around driver_register().
  1923. * Returns:
  1924. * %0 on success and a negative error value on failure.
  1925. */
  1926. int ccw_driver_register(struct ccw_driver *cdriver)
  1927. {
  1928. struct device_driver *drv = &cdriver->driver;
  1929. drv->bus = &ccw_bus_type;
  1930. drv->name = cdriver->name;
  1931. drv->owner = cdriver->owner;
  1932. return driver_register(drv);
  1933. }
  1934. /**
  1935. * ccw_driver_unregister() - deregister a ccw driver
  1936. * @cdriver: driver to be deregistered
  1937. *
  1938. * This function is mainly a wrapper around driver_unregister().
  1939. */
  1940. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1941. {
  1942. driver_unregister(&cdriver->driver);
  1943. }
  1944. /* Helper func for qdio. */
  1945. struct subchannel_id
  1946. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1947. {
  1948. struct subchannel *sch;
  1949. sch = to_subchannel(cdev->dev.parent);
  1950. return sch->schid;
  1951. }
  1952. MODULE_LICENSE("GPL");
  1953. EXPORT_SYMBOL(ccw_device_set_online);
  1954. EXPORT_SYMBOL(ccw_device_set_offline);
  1955. EXPORT_SYMBOL(ccw_driver_register);
  1956. EXPORT_SYMBOL(ccw_driver_unregister);
  1957. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1958. EXPORT_SYMBOL(ccw_bus_type);
  1959. EXPORT_SYMBOL(ccw_device_work);
  1960. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);