device.c 50 KB

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