device.c 49 KB

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