device.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
  6. * IBM Corporation
  7. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  8. * Cornelia Huck (cornelia.huck@de.ibm.com)
  9. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/errno.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/device.h>
  19. #include <linux/workqueue.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/param.h> /* HZ */
  23. #include "cio.h"
  24. #include "cio_debug.h"
  25. #include "css.h"
  26. #include "device.h"
  27. #include "ioasm.h"
  28. /******************* bus type handling ***********************/
  29. /* The Linux driver model distinguishes between a bus type and
  30. * the bus itself. Of course we only have one channel
  31. * subsystem driver and one channel system per machine, but
  32. * we still use the abstraction. T.R. says it's a good idea. */
  33. static int
  34. ccw_bus_match (struct device * dev, struct device_driver * drv)
  35. {
  36. struct ccw_device *cdev = to_ccwdev(dev);
  37. struct ccw_driver *cdrv = to_ccwdrv(drv);
  38. const struct ccw_device_id *ids = cdrv->ids, *found;
  39. if (!ids)
  40. return 0;
  41. found = ccw_device_id_match(ids, &cdev->id);
  42. if (!found)
  43. return 0;
  44. cdev->id.driver_info = found->driver_info;
  45. return 1;
  46. }
  47. /* Store modalias string delimited by prefix/suffix string into buffer with
  48. * specified size. Return length of resulting string (excluding trailing '\0')
  49. * even if string doesn't fit buffer (snprintf semantics). */
  50. static int snprint_alias(char *buf, size_t size, const char *prefix,
  51. struct ccw_device_id *id, const char *suffix)
  52. {
  53. int len;
  54. len = snprintf(buf, size, "%sccw:t%04Xm%02X", prefix, id->cu_type,
  55. id->cu_model);
  56. if (len > size)
  57. return len;
  58. buf += len;
  59. size -= len;
  60. if (id->dev_type != 0)
  61. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  62. id->dev_model, suffix);
  63. else
  64. len += snprintf(buf, size, "dtdm%s", suffix);
  65. return len;
  66. }
  67. /* Set up environment variables for ccw device uevent. Return 0 on success,
  68. * non-zero otherwise. */
  69. static int ccw_uevent(struct device *dev, char **envp, int num_envp,
  70. char *buffer, int buffer_size)
  71. {
  72. struct ccw_device *cdev = to_ccwdev(dev);
  73. struct ccw_device_id *id = &(cdev->id);
  74. int i = 0;
  75. int len;
  76. /* CU_TYPE= */
  77. len = snprintf(buffer, buffer_size, "CU_TYPE=%04X", id->cu_type) + 1;
  78. if (len > buffer_size || i >= num_envp)
  79. return -ENOMEM;
  80. envp[i++] = buffer;
  81. buffer += len;
  82. buffer_size -= len;
  83. /* CU_MODEL= */
  84. len = snprintf(buffer, buffer_size, "CU_MODEL=%02X", id->cu_model) + 1;
  85. if (len > buffer_size || i >= num_envp)
  86. return -ENOMEM;
  87. envp[i++] = buffer;
  88. buffer += len;
  89. buffer_size -= len;
  90. /* The next two can be zero, that's ok for us */
  91. /* DEV_TYPE= */
  92. len = snprintf(buffer, buffer_size, "DEV_TYPE=%04X", id->dev_type) + 1;
  93. if (len > buffer_size || i >= num_envp)
  94. return -ENOMEM;
  95. envp[i++] = buffer;
  96. buffer += len;
  97. buffer_size -= len;
  98. /* DEV_MODEL= */
  99. len = snprintf(buffer, buffer_size, "DEV_MODEL=%02X",
  100. (unsigned char) id->dev_model) + 1;
  101. if (len > buffer_size || i >= num_envp)
  102. return -ENOMEM;
  103. envp[i++] = buffer;
  104. buffer += len;
  105. buffer_size -= len;
  106. /* MODALIAS= */
  107. len = snprint_alias(buffer, buffer_size, "MODALIAS=", id, "") + 1;
  108. if (len > buffer_size || i >= num_envp)
  109. return -ENOMEM;
  110. envp[i++] = buffer;
  111. buffer += len;
  112. buffer_size -= len;
  113. envp[i] = NULL;
  114. return 0;
  115. }
  116. struct bus_type ccw_bus_type;
  117. static int io_subchannel_probe (struct subchannel *);
  118. static int io_subchannel_remove (struct subchannel *);
  119. static int io_subchannel_notify(struct device *, int);
  120. static void io_subchannel_verify(struct device *);
  121. static void io_subchannel_ioterm(struct device *);
  122. static void io_subchannel_shutdown(struct subchannel *);
  123. struct css_driver io_subchannel_driver = {
  124. .subchannel_type = SUBCHANNEL_TYPE_IO,
  125. .drv = {
  126. .name = "io_subchannel",
  127. .bus = &css_bus_type,
  128. },
  129. .irq = io_subchannel_irq,
  130. .notify = io_subchannel_notify,
  131. .verify = io_subchannel_verify,
  132. .termination = io_subchannel_ioterm,
  133. .probe = io_subchannel_probe,
  134. .remove = io_subchannel_remove,
  135. .shutdown = io_subchannel_shutdown,
  136. };
  137. struct workqueue_struct *ccw_device_work;
  138. struct workqueue_struct *ccw_device_notify_work;
  139. wait_queue_head_t ccw_device_init_wq;
  140. atomic_t ccw_device_init_count;
  141. static int __init
  142. init_ccw_bus_type (void)
  143. {
  144. int ret;
  145. init_waitqueue_head(&ccw_device_init_wq);
  146. atomic_set(&ccw_device_init_count, 0);
  147. ccw_device_work = create_singlethread_workqueue("cio");
  148. if (!ccw_device_work)
  149. return -ENOMEM; /* FIXME: better errno ? */
  150. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  151. if (!ccw_device_notify_work) {
  152. ret = -ENOMEM; /* FIXME: better errno ? */
  153. goto out_err;
  154. }
  155. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  156. if (!slow_path_wq) {
  157. ret = -ENOMEM; /* FIXME: better errno ? */
  158. goto out_err;
  159. }
  160. if ((ret = bus_register (&ccw_bus_type)))
  161. goto out_err;
  162. if ((ret = driver_register(&io_subchannel_driver.drv)))
  163. goto out_err;
  164. wait_event(ccw_device_init_wq,
  165. atomic_read(&ccw_device_init_count) == 0);
  166. flush_workqueue(ccw_device_work);
  167. return 0;
  168. out_err:
  169. if (ccw_device_work)
  170. destroy_workqueue(ccw_device_work);
  171. if (ccw_device_notify_work)
  172. destroy_workqueue(ccw_device_notify_work);
  173. if (slow_path_wq)
  174. destroy_workqueue(slow_path_wq);
  175. return ret;
  176. }
  177. static void __exit
  178. cleanup_ccw_bus_type (void)
  179. {
  180. driver_unregister(&io_subchannel_driver.drv);
  181. bus_unregister(&ccw_bus_type);
  182. destroy_workqueue(ccw_device_notify_work);
  183. destroy_workqueue(ccw_device_work);
  184. }
  185. subsys_initcall(init_ccw_bus_type);
  186. module_exit(cleanup_ccw_bus_type);
  187. /************************ device handling **************************/
  188. /*
  189. * A ccw_device has some interfaces in sysfs in addition to the
  190. * standard ones.
  191. * The following entries are designed to export the information which
  192. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  193. * are obvious, so they don't have an entry :)
  194. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  195. */
  196. static ssize_t
  197. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  198. {
  199. struct subchannel *sch = to_subchannel(dev);
  200. struct ssd_info *ssd = &sch->ssd_info;
  201. ssize_t ret = 0;
  202. int chp;
  203. for (chp = 0; chp < 8; chp++)
  204. ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
  205. ret += sprintf (buf+ret, "\n");
  206. return min((ssize_t)PAGE_SIZE, ret);
  207. }
  208. static ssize_t
  209. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  210. {
  211. struct subchannel *sch = to_subchannel(dev);
  212. struct pmcw *pmcw = &sch->schib.pmcw;
  213. return sprintf (buf, "%02x %02x %02x\n",
  214. pmcw->pim, pmcw->pam, pmcw->pom);
  215. }
  216. static ssize_t
  217. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  218. {
  219. struct ccw_device *cdev = to_ccwdev(dev);
  220. struct ccw_device_id *id = &(cdev->id);
  221. if (id->dev_type != 0)
  222. return sprintf(buf, "%04x/%02x\n",
  223. id->dev_type, id->dev_model);
  224. else
  225. return sprintf(buf, "n/a\n");
  226. }
  227. static ssize_t
  228. cutype_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. return sprintf(buf, "%04x/%02x\n",
  233. id->cu_type, id->cu_model);
  234. }
  235. static ssize_t
  236. modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
  237. {
  238. struct ccw_device *cdev = to_ccwdev(dev);
  239. struct ccw_device_id *id = &(cdev->id);
  240. int len;
  241. len = snprint_alias(buf, PAGE_SIZE, "", id, "\n") + 1;
  242. return len > PAGE_SIZE ? PAGE_SIZE : len;
  243. }
  244. static ssize_t
  245. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  246. {
  247. struct ccw_device *cdev = to_ccwdev(dev);
  248. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  249. }
  250. int ccw_device_is_orphan(struct ccw_device *cdev)
  251. {
  252. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  253. }
  254. static void ccw_device_unregister(struct work_struct *work)
  255. {
  256. struct ccw_device_private *priv;
  257. struct ccw_device *cdev;
  258. priv = container_of(work, struct ccw_device_private, kick_work);
  259. cdev = priv->cdev;
  260. if (test_and_clear_bit(1, &cdev->private->registered))
  261. device_unregister(&cdev->dev);
  262. put_device(&cdev->dev);
  263. }
  264. static void
  265. ccw_device_remove_disconnected(struct ccw_device *cdev)
  266. {
  267. struct subchannel *sch;
  268. unsigned long flags;
  269. /*
  270. * Forced offline in disconnected state means
  271. * 'throw away device'.
  272. */
  273. if (ccw_device_is_orphan(cdev)) {
  274. /* Deregister ccw device. */
  275. spin_lock_irqsave(cdev->ccwlock, flags);
  276. cdev->private->state = DEV_STATE_NOT_OPER;
  277. spin_unlock_irqrestore(cdev->ccwlock, flags);
  278. if (get_device(&cdev->dev)) {
  279. PREPARE_WORK(&cdev->private->kick_work,
  280. ccw_device_unregister);
  281. queue_work(ccw_device_work, &cdev->private->kick_work);
  282. }
  283. return ;
  284. }
  285. sch = to_subchannel(cdev->dev.parent);
  286. css_sch_device_unregister(sch);
  287. /* Reset intparm to zeroes. */
  288. sch->schib.pmcw.intparm = 0;
  289. cio_modify(sch);
  290. put_device(&sch->dev);
  291. }
  292. int
  293. ccw_device_set_offline(struct ccw_device *cdev)
  294. {
  295. int ret;
  296. if (!cdev)
  297. return -ENODEV;
  298. if (!cdev->online || !cdev->drv)
  299. return -EINVAL;
  300. if (cdev->drv->set_offline) {
  301. ret = cdev->drv->set_offline(cdev);
  302. if (ret != 0)
  303. return ret;
  304. }
  305. cdev->online = 0;
  306. spin_lock_irq(cdev->ccwlock);
  307. ret = ccw_device_offline(cdev);
  308. if (ret == -ENODEV) {
  309. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  310. cdev->private->state = DEV_STATE_OFFLINE;
  311. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  312. }
  313. spin_unlock_irq(cdev->ccwlock);
  314. return ret;
  315. }
  316. spin_unlock_irq(cdev->ccwlock);
  317. if (ret == 0)
  318. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  319. else {
  320. pr_debug("ccw_device_offline returned %d, device %s\n",
  321. ret, cdev->dev.bus_id);
  322. cdev->online = 1;
  323. }
  324. return ret;
  325. }
  326. int
  327. ccw_device_set_online(struct ccw_device *cdev)
  328. {
  329. int ret;
  330. if (!cdev)
  331. return -ENODEV;
  332. if (cdev->online || !cdev->drv)
  333. return -EINVAL;
  334. spin_lock_irq(cdev->ccwlock);
  335. ret = ccw_device_online(cdev);
  336. spin_unlock_irq(cdev->ccwlock);
  337. if (ret == 0)
  338. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  339. else {
  340. pr_debug("ccw_device_online returned %d, device %s\n",
  341. ret, cdev->dev.bus_id);
  342. return ret;
  343. }
  344. if (cdev->private->state != DEV_STATE_ONLINE)
  345. return -ENODEV;
  346. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  347. cdev->online = 1;
  348. return 0;
  349. }
  350. spin_lock_irq(cdev->ccwlock);
  351. ret = ccw_device_offline(cdev);
  352. spin_unlock_irq(cdev->ccwlock);
  353. if (ret == 0)
  354. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  355. else
  356. pr_debug("ccw_device_offline returned %d, device %s\n",
  357. ret, cdev->dev.bus_id);
  358. return (ret == 0) ? -ENODEV : ret;
  359. }
  360. static ssize_t
  361. online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  362. {
  363. struct ccw_device *cdev = to_ccwdev(dev);
  364. int i, force, ret;
  365. char *tmp;
  366. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  367. return -EAGAIN;
  368. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  369. atomic_set(&cdev->private->onoff, 0);
  370. return -EINVAL;
  371. }
  372. if (!strncmp(buf, "force\n", count)) {
  373. force = 1;
  374. i = 1;
  375. } else {
  376. force = 0;
  377. i = simple_strtoul(buf, &tmp, 16);
  378. }
  379. if (i == 1) {
  380. /* Do device recognition, if needed. */
  381. if (cdev->id.cu_type == 0) {
  382. ret = ccw_device_recognition(cdev);
  383. if (ret) {
  384. printk(KERN_WARNING"Couldn't start recognition "
  385. "for device %s (ret=%d)\n",
  386. cdev->dev.bus_id, ret);
  387. goto out;
  388. }
  389. wait_event(cdev->private->wait_q,
  390. cdev->private->flags.recog_done);
  391. }
  392. if (cdev->drv && cdev->drv->set_online)
  393. ccw_device_set_online(cdev);
  394. } else if (i == 0) {
  395. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  396. ccw_device_remove_disconnected(cdev);
  397. else if (cdev->drv && cdev->drv->set_offline)
  398. ccw_device_set_offline(cdev);
  399. }
  400. if (force && cdev->private->state == DEV_STATE_BOXED) {
  401. ret = ccw_device_stlck(cdev);
  402. if (ret) {
  403. printk(KERN_WARNING"ccw_device_stlck for device %s "
  404. "returned %d!\n", cdev->dev.bus_id, ret);
  405. goto out;
  406. }
  407. /* Do device recognition, if needed. */
  408. if (cdev->id.cu_type == 0) {
  409. cdev->private->state = DEV_STATE_NOT_OPER;
  410. ret = ccw_device_recognition(cdev);
  411. if (ret) {
  412. printk(KERN_WARNING"Couldn't start recognition "
  413. "for device %s (ret=%d)\n",
  414. cdev->dev.bus_id, ret);
  415. goto out;
  416. }
  417. wait_event(cdev->private->wait_q,
  418. cdev->private->flags.recog_done);
  419. }
  420. if (cdev->drv && cdev->drv->set_online)
  421. ccw_device_set_online(cdev);
  422. }
  423. out:
  424. if (cdev->drv)
  425. module_put(cdev->drv->owner);
  426. atomic_set(&cdev->private->onoff, 0);
  427. return count;
  428. }
  429. static ssize_t
  430. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  431. {
  432. struct ccw_device *cdev = to_ccwdev(dev);
  433. struct subchannel *sch;
  434. if (ccw_device_is_orphan(cdev))
  435. return sprintf(buf, "no device\n");
  436. switch (cdev->private->state) {
  437. case DEV_STATE_BOXED:
  438. return sprintf(buf, "boxed\n");
  439. case DEV_STATE_DISCONNECTED:
  440. case DEV_STATE_DISCONNECTED_SENSE_ID:
  441. case DEV_STATE_NOT_OPER:
  442. sch = to_subchannel(dev->parent);
  443. if (!sch->lpm)
  444. return sprintf(buf, "no path\n");
  445. else
  446. return sprintf(buf, "no device\n");
  447. default:
  448. /* All other states considered fine. */
  449. return sprintf(buf, "good\n");
  450. }
  451. }
  452. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  453. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  454. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  455. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  456. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  457. static DEVICE_ATTR(online, 0644, online_show, online_store);
  458. extern struct device_attribute dev_attr_cmb_enable;
  459. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  460. static struct attribute * subch_attrs[] = {
  461. &dev_attr_chpids.attr,
  462. &dev_attr_pimpampom.attr,
  463. NULL,
  464. };
  465. static struct attribute_group subch_attr_group = {
  466. .attrs = subch_attrs,
  467. };
  468. struct attribute_group *subch_attr_groups[] = {
  469. &subch_attr_group,
  470. NULL,
  471. };
  472. static struct attribute * ccwdev_attrs[] = {
  473. &dev_attr_devtype.attr,
  474. &dev_attr_cutype.attr,
  475. &dev_attr_modalias.attr,
  476. &dev_attr_online.attr,
  477. &dev_attr_cmb_enable.attr,
  478. &dev_attr_availability.attr,
  479. NULL,
  480. };
  481. static struct attribute_group ccwdev_attr_group = {
  482. .attrs = ccwdev_attrs,
  483. };
  484. static int
  485. device_add_files (struct device *dev)
  486. {
  487. return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
  488. }
  489. static void
  490. device_remove_files(struct device *dev)
  491. {
  492. sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
  493. }
  494. /* this is a simple abstraction for device_register that sets the
  495. * correct bus type and adds the bus specific files */
  496. static int ccw_device_register(struct ccw_device *cdev)
  497. {
  498. struct device *dev = &cdev->dev;
  499. int ret;
  500. dev->bus = &ccw_bus_type;
  501. if ((ret = device_add(dev)))
  502. return ret;
  503. set_bit(1, &cdev->private->registered);
  504. if ((ret = device_add_files(dev))) {
  505. if (test_and_clear_bit(1, &cdev->private->registered))
  506. device_del(dev);
  507. }
  508. return ret;
  509. }
  510. struct match_data {
  511. struct ccw_dev_id dev_id;
  512. struct ccw_device * sibling;
  513. };
  514. static int
  515. match_devno(struct device * dev, void * data)
  516. {
  517. struct match_data * d = data;
  518. struct ccw_device * cdev;
  519. cdev = to_ccwdev(dev);
  520. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  521. !ccw_device_is_orphan(cdev) &&
  522. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  523. (cdev != d->sibling))
  524. return 1;
  525. return 0;
  526. }
  527. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  528. struct ccw_device *sibling)
  529. {
  530. struct device *dev;
  531. struct match_data data;
  532. data.dev_id = *dev_id;
  533. data.sibling = sibling;
  534. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  535. return dev ? to_ccwdev(dev) : NULL;
  536. }
  537. static int match_orphan(struct device *dev, void *data)
  538. {
  539. struct ccw_dev_id *dev_id;
  540. struct ccw_device *cdev;
  541. dev_id = data;
  542. cdev = to_ccwdev(dev);
  543. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  544. }
  545. static struct ccw_device *
  546. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  547. struct ccw_dev_id *dev_id)
  548. {
  549. struct device *dev;
  550. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  551. match_orphan);
  552. return dev ? to_ccwdev(dev) : NULL;
  553. }
  554. static void
  555. ccw_device_add_changed(struct work_struct *work)
  556. {
  557. struct ccw_device_private *priv;
  558. struct ccw_device *cdev;
  559. priv = container_of(work, struct ccw_device_private, kick_work);
  560. cdev = priv->cdev;
  561. if (device_add(&cdev->dev)) {
  562. put_device(&cdev->dev);
  563. return;
  564. }
  565. set_bit(1, &cdev->private->registered);
  566. if (device_add_files(&cdev->dev)) {
  567. if (test_and_clear_bit(1, &cdev->private->registered))
  568. device_unregister(&cdev->dev);
  569. }
  570. }
  571. void ccw_device_do_unreg_rereg(struct work_struct *work)
  572. {
  573. struct ccw_device_private *priv;
  574. struct ccw_device *cdev;
  575. struct subchannel *sch;
  576. priv = container_of(work, struct ccw_device_private, kick_work);
  577. cdev = priv->cdev;
  578. sch = to_subchannel(cdev->dev.parent);
  579. device_remove_files(&cdev->dev);
  580. if (test_and_clear_bit(1, &cdev->private->registered))
  581. device_del(&cdev->dev);
  582. PREPARE_WORK(&cdev->private->kick_work,
  583. ccw_device_add_changed);
  584. queue_work(ccw_device_work, &cdev->private->kick_work);
  585. }
  586. static void
  587. ccw_device_release(struct device *dev)
  588. {
  589. struct ccw_device *cdev;
  590. cdev = to_ccwdev(dev);
  591. kfree(cdev->private);
  592. kfree(cdev);
  593. }
  594. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  595. {
  596. struct ccw_device *cdev;
  597. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  598. if (cdev) {
  599. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  600. GFP_KERNEL | GFP_DMA);
  601. if (cdev->private)
  602. return cdev;
  603. }
  604. kfree(cdev);
  605. return ERR_PTR(-ENOMEM);
  606. }
  607. static int io_subchannel_initialize_dev(struct subchannel *sch,
  608. struct ccw_device *cdev)
  609. {
  610. cdev->private->cdev = cdev;
  611. atomic_set(&cdev->private->onoff, 0);
  612. cdev->dev.parent = &sch->dev;
  613. cdev->dev.release = ccw_device_release;
  614. INIT_LIST_HEAD(&cdev->private->kick_work.entry);
  615. /* Do first half of device_register. */
  616. device_initialize(&cdev->dev);
  617. if (!get_device(&sch->dev)) {
  618. if (cdev->dev.release)
  619. cdev->dev.release(&cdev->dev);
  620. return -ENODEV;
  621. }
  622. return 0;
  623. }
  624. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  625. {
  626. struct ccw_device *cdev;
  627. int ret;
  628. cdev = io_subchannel_allocate_dev(sch);
  629. if (!IS_ERR(cdev)) {
  630. ret = io_subchannel_initialize_dev(sch, cdev);
  631. if (ret) {
  632. kfree(cdev);
  633. cdev = ERR_PTR(ret);
  634. }
  635. }
  636. return cdev;
  637. }
  638. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  639. static void sch_attach_device(struct subchannel *sch,
  640. struct ccw_device *cdev)
  641. {
  642. spin_lock_irq(sch->lock);
  643. sch->dev.driver_data = cdev;
  644. cdev->private->schid = sch->schid;
  645. cdev->ccwlock = sch->lock;
  646. device_trigger_reprobe(sch);
  647. spin_unlock_irq(sch->lock);
  648. }
  649. static void sch_attach_disconnected_device(struct subchannel *sch,
  650. struct ccw_device *cdev)
  651. {
  652. struct subchannel *other_sch;
  653. int ret;
  654. other_sch = to_subchannel(get_device(cdev->dev.parent));
  655. ret = device_move(&cdev->dev, &sch->dev);
  656. if (ret) {
  657. CIO_MSG_EVENT(2, "Moving disconnected device 0.%x.%04x failed "
  658. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  659. cdev->private->dev_id.devno, ret);
  660. put_device(&other_sch->dev);
  661. return;
  662. }
  663. other_sch->dev.driver_data = NULL;
  664. /* No need to keep a subchannel without ccw device around. */
  665. css_sch_device_unregister(other_sch);
  666. put_device(&other_sch->dev);
  667. sch_attach_device(sch, cdev);
  668. }
  669. static void sch_attach_orphaned_device(struct subchannel *sch,
  670. struct ccw_device *cdev)
  671. {
  672. int ret;
  673. /* Try to move the ccw device to its new subchannel. */
  674. ret = device_move(&cdev->dev, &sch->dev);
  675. if (ret) {
  676. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  677. "failed (ret=%d)!\n",
  678. cdev->private->dev_id.ssid,
  679. cdev->private->dev_id.devno, ret);
  680. return;
  681. }
  682. sch_attach_device(sch, cdev);
  683. }
  684. static void sch_create_and_recog_new_device(struct subchannel *sch)
  685. {
  686. struct ccw_device *cdev;
  687. /* Need to allocate a new ccw device. */
  688. cdev = io_subchannel_create_ccwdev(sch);
  689. if (IS_ERR(cdev)) {
  690. /* OK, we did everything we could... */
  691. css_sch_device_unregister(sch);
  692. return;
  693. }
  694. spin_lock_irq(sch->lock);
  695. sch->dev.driver_data = cdev;
  696. spin_unlock_irq(sch->lock);
  697. /* Start recognition for the new ccw device. */
  698. if (io_subchannel_recog(cdev, sch)) {
  699. spin_lock_irq(sch->lock);
  700. sch->dev.driver_data = NULL;
  701. spin_unlock_irq(sch->lock);
  702. if (cdev->dev.release)
  703. cdev->dev.release(&cdev->dev);
  704. css_sch_device_unregister(sch);
  705. }
  706. }
  707. void ccw_device_move_to_orphanage(struct work_struct *work)
  708. {
  709. struct ccw_device_private *priv;
  710. struct ccw_device *cdev;
  711. struct ccw_device *replacing_cdev;
  712. struct subchannel *sch;
  713. int ret;
  714. struct channel_subsystem *css;
  715. struct ccw_dev_id dev_id;
  716. priv = container_of(work, struct ccw_device_private, kick_work);
  717. cdev = priv->cdev;
  718. sch = to_subchannel(cdev->dev.parent);
  719. css = to_css(sch->dev.parent);
  720. dev_id.devno = sch->schib.pmcw.dev;
  721. dev_id.ssid = sch->schid.ssid;
  722. /*
  723. * Move the orphaned ccw device to the orphanage so the replacing
  724. * ccw device can take its place on the subchannel.
  725. */
  726. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  727. if (ret) {
  728. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  729. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  730. cdev->private->dev_id.devno, ret);
  731. return;
  732. }
  733. cdev->ccwlock = css->pseudo_subchannel->lock;
  734. /*
  735. * Search for the replacing ccw device
  736. * - among the disconnected devices
  737. * - in the orphanage
  738. */
  739. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  740. if (replacing_cdev) {
  741. sch_attach_disconnected_device(sch, replacing_cdev);
  742. return;
  743. }
  744. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  745. if (replacing_cdev) {
  746. sch_attach_orphaned_device(sch, replacing_cdev);
  747. return;
  748. }
  749. sch_create_and_recog_new_device(sch);
  750. }
  751. /*
  752. * Register recognized device.
  753. */
  754. static void
  755. io_subchannel_register(struct work_struct *work)
  756. {
  757. struct ccw_device_private *priv;
  758. struct ccw_device *cdev;
  759. struct subchannel *sch;
  760. int ret;
  761. unsigned long flags;
  762. priv = container_of(work, struct ccw_device_private, kick_work);
  763. cdev = priv->cdev;
  764. sch = to_subchannel(cdev->dev.parent);
  765. /*
  766. * io_subchannel_register() will also be called after device
  767. * recognition has been done for a boxed device (which will already
  768. * be registered). We need to reprobe since we may now have sense id
  769. * information.
  770. */
  771. if (klist_node_attached(&cdev->dev.knode_parent)) {
  772. if (!cdev->drv) {
  773. ret = device_reprobe(&cdev->dev);
  774. if (ret)
  775. /* We can't do much here. */
  776. dev_info(&cdev->dev, "device_reprobe() returned"
  777. " %d\n", ret);
  778. }
  779. goto out;
  780. }
  781. /* make it known to the system */
  782. ret = ccw_device_register(cdev);
  783. if (ret) {
  784. printk (KERN_WARNING "%s: could not register %s\n",
  785. __func__, cdev->dev.bus_id);
  786. put_device(&cdev->dev);
  787. spin_lock_irqsave(sch->lock, flags);
  788. sch->dev.driver_data = NULL;
  789. spin_unlock_irqrestore(sch->lock, flags);
  790. kfree (cdev->private);
  791. kfree (cdev);
  792. put_device(&sch->dev);
  793. if (atomic_dec_and_test(&ccw_device_init_count))
  794. wake_up(&ccw_device_init_wq);
  795. return;
  796. }
  797. put_device(&cdev->dev);
  798. out:
  799. cdev->private->flags.recog_done = 1;
  800. put_device(&sch->dev);
  801. wake_up(&cdev->private->wait_q);
  802. if (atomic_dec_and_test(&ccw_device_init_count))
  803. wake_up(&ccw_device_init_wq);
  804. }
  805. void
  806. ccw_device_call_sch_unregister(struct work_struct *work)
  807. {
  808. struct ccw_device_private *priv;
  809. struct ccw_device *cdev;
  810. struct subchannel *sch;
  811. priv = container_of(work, struct ccw_device_private, kick_work);
  812. cdev = priv->cdev;
  813. sch = to_subchannel(cdev->dev.parent);
  814. css_sch_device_unregister(sch);
  815. /* Reset intparm to zeroes. */
  816. sch->schib.pmcw.intparm = 0;
  817. cio_modify(sch);
  818. put_device(&cdev->dev);
  819. put_device(&sch->dev);
  820. }
  821. /*
  822. * subchannel recognition done. Called from the state machine.
  823. */
  824. void
  825. io_subchannel_recog_done(struct ccw_device *cdev)
  826. {
  827. struct subchannel *sch;
  828. if (css_init_done == 0) {
  829. cdev->private->flags.recog_done = 1;
  830. return;
  831. }
  832. switch (cdev->private->state) {
  833. case DEV_STATE_NOT_OPER:
  834. cdev->private->flags.recog_done = 1;
  835. /* Remove device found not operational. */
  836. if (!get_device(&cdev->dev))
  837. break;
  838. sch = to_subchannel(cdev->dev.parent);
  839. PREPARE_WORK(&cdev->private->kick_work,
  840. ccw_device_call_sch_unregister);
  841. queue_work(slow_path_wq, &cdev->private->kick_work);
  842. if (atomic_dec_and_test(&ccw_device_init_count))
  843. wake_up(&ccw_device_init_wq);
  844. break;
  845. case DEV_STATE_BOXED:
  846. /* Device did not respond in time. */
  847. case DEV_STATE_OFFLINE:
  848. /*
  849. * We can't register the device in interrupt context so
  850. * we schedule a work item.
  851. */
  852. if (!get_device(&cdev->dev))
  853. break;
  854. PREPARE_WORK(&cdev->private->kick_work,
  855. io_subchannel_register);
  856. queue_work(slow_path_wq, &cdev->private->kick_work);
  857. break;
  858. }
  859. }
  860. static int
  861. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  862. {
  863. int rc;
  864. struct ccw_device_private *priv;
  865. sch->dev.driver_data = cdev;
  866. sch->driver = &io_subchannel_driver;
  867. cdev->ccwlock = sch->lock;
  868. /* Init private data. */
  869. priv = cdev->private;
  870. priv->dev_id.devno = sch->schib.pmcw.dev;
  871. priv->dev_id.ssid = sch->schid.ssid;
  872. priv->schid = sch->schid;
  873. priv->state = DEV_STATE_NOT_OPER;
  874. INIT_LIST_HEAD(&priv->cmb_list);
  875. init_waitqueue_head(&priv->wait_q);
  876. init_timer(&priv->timer);
  877. /* Set an initial name for the device. */
  878. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  879. sch->schid.ssid, sch->schib.pmcw.dev);
  880. /* Increase counter of devices currently in recognition. */
  881. atomic_inc(&ccw_device_init_count);
  882. /* Start async. device sensing. */
  883. spin_lock_irq(sch->lock);
  884. rc = ccw_device_recognition(cdev);
  885. spin_unlock_irq(sch->lock);
  886. if (rc) {
  887. if (atomic_dec_and_test(&ccw_device_init_count))
  888. wake_up(&ccw_device_init_wq);
  889. }
  890. return rc;
  891. }
  892. static void ccw_device_move_to_sch(struct work_struct *work)
  893. {
  894. struct ccw_device_private *priv;
  895. int rc;
  896. struct subchannel *sch;
  897. struct ccw_device *cdev;
  898. struct subchannel *former_parent;
  899. priv = container_of(work, struct ccw_device_private, kick_work);
  900. sch = priv->sch;
  901. cdev = priv->cdev;
  902. former_parent = ccw_device_is_orphan(cdev) ?
  903. NULL : to_subchannel(get_device(cdev->dev.parent));
  904. mutex_lock(&sch->reg_mutex);
  905. /* Try to move the ccw device to its new subchannel. */
  906. rc = device_move(&cdev->dev, &sch->dev);
  907. mutex_unlock(&sch->reg_mutex);
  908. if (rc) {
  909. CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
  910. "0.%x.%04x failed (ret=%d)!\n",
  911. cdev->private->dev_id.ssid,
  912. cdev->private->dev_id.devno, sch->schid.ssid,
  913. sch->schid.sch_no, rc);
  914. css_sch_device_unregister(sch);
  915. goto out;
  916. }
  917. if (former_parent) {
  918. spin_lock_irq(former_parent->lock);
  919. former_parent->dev.driver_data = NULL;
  920. spin_unlock_irq(former_parent->lock);
  921. css_sch_device_unregister(former_parent);
  922. /* Reset intparm to zeroes. */
  923. former_parent->schib.pmcw.intparm = 0;
  924. cio_modify(former_parent);
  925. }
  926. sch_attach_device(sch, cdev);
  927. out:
  928. if (former_parent)
  929. put_device(&former_parent->dev);
  930. put_device(&cdev->dev);
  931. }
  932. static int
  933. io_subchannel_probe (struct subchannel *sch)
  934. {
  935. struct ccw_device *cdev;
  936. int rc;
  937. unsigned long flags;
  938. struct ccw_dev_id dev_id;
  939. if (sch->dev.driver_data) {
  940. /*
  941. * This subchannel already has an associated ccw_device.
  942. * Register it and exit. This happens for all early
  943. * device, e.g. the console.
  944. */
  945. cdev = sch->dev.driver_data;
  946. device_initialize(&cdev->dev);
  947. ccw_device_register(cdev);
  948. /*
  949. * Check if the device is already online. If it is
  950. * the reference count needs to be corrected
  951. * (see ccw_device_online and css_init_done for the
  952. * ugly details).
  953. */
  954. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  955. cdev->private->state != DEV_STATE_OFFLINE &&
  956. cdev->private->state != DEV_STATE_BOXED)
  957. get_device(&cdev->dev);
  958. return 0;
  959. }
  960. /*
  961. * First check if a fitting device may be found amongst the
  962. * disconnected devices or in the orphanage.
  963. */
  964. dev_id.devno = sch->schib.pmcw.dev;
  965. dev_id.ssid = sch->schid.ssid;
  966. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  967. if (!cdev)
  968. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  969. &dev_id);
  970. if (cdev) {
  971. /*
  972. * Schedule moving the device until when we have a registered
  973. * subchannel to move to and succeed the probe. We can
  974. * unregister later again, when the probe is through.
  975. */
  976. cdev->private->sch = sch;
  977. PREPARE_WORK(&cdev->private->kick_work,
  978. ccw_device_move_to_sch);
  979. queue_work(slow_path_wq, &cdev->private->kick_work);
  980. return 0;
  981. }
  982. cdev = io_subchannel_create_ccwdev(sch);
  983. if (IS_ERR(cdev))
  984. return PTR_ERR(cdev);
  985. rc = io_subchannel_recog(cdev, sch);
  986. if (rc) {
  987. spin_lock_irqsave(sch->lock, flags);
  988. sch->dev.driver_data = NULL;
  989. spin_unlock_irqrestore(sch->lock, flags);
  990. if (cdev->dev.release)
  991. cdev->dev.release(&cdev->dev);
  992. }
  993. return rc;
  994. }
  995. static int
  996. io_subchannel_remove (struct subchannel *sch)
  997. {
  998. struct ccw_device *cdev;
  999. unsigned long flags;
  1000. if (!sch->dev.driver_data)
  1001. return 0;
  1002. cdev = sch->dev.driver_data;
  1003. /* Set ccw device to not operational and drop reference. */
  1004. spin_lock_irqsave(cdev->ccwlock, flags);
  1005. sch->dev.driver_data = NULL;
  1006. cdev->private->state = DEV_STATE_NOT_OPER;
  1007. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1008. /*
  1009. * Put unregistration on workqueue to avoid livelocks on the css bus
  1010. * semaphore.
  1011. */
  1012. if (get_device(&cdev->dev)) {
  1013. PREPARE_WORK(&cdev->private->kick_work,
  1014. ccw_device_unregister);
  1015. queue_work(ccw_device_work, &cdev->private->kick_work);
  1016. }
  1017. return 0;
  1018. }
  1019. static int
  1020. io_subchannel_notify(struct device *dev, int event)
  1021. {
  1022. struct ccw_device *cdev;
  1023. cdev = dev->driver_data;
  1024. if (!cdev)
  1025. return 0;
  1026. if (!cdev->drv)
  1027. return 0;
  1028. if (!cdev->online)
  1029. return 0;
  1030. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  1031. }
  1032. static void
  1033. io_subchannel_verify(struct device *dev)
  1034. {
  1035. struct ccw_device *cdev;
  1036. cdev = dev->driver_data;
  1037. if (cdev)
  1038. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1039. }
  1040. static void
  1041. io_subchannel_ioterm(struct device *dev)
  1042. {
  1043. struct ccw_device *cdev;
  1044. cdev = dev->driver_data;
  1045. if (!cdev)
  1046. return;
  1047. /* Internal I/O will be retried by the interrupt handler. */
  1048. if (cdev->private->flags.intretry)
  1049. return;
  1050. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1051. if (cdev->handler)
  1052. cdev->handler(cdev, cdev->private->intparm,
  1053. ERR_PTR(-EIO));
  1054. }
  1055. static void
  1056. io_subchannel_shutdown(struct subchannel *sch)
  1057. {
  1058. struct ccw_device *cdev;
  1059. int ret;
  1060. cdev = sch->dev.driver_data;
  1061. if (cio_is_console(sch->schid))
  1062. return;
  1063. if (!sch->schib.pmcw.ena)
  1064. /* Nothing to do. */
  1065. return;
  1066. ret = cio_disable_subchannel(sch);
  1067. if (ret != -EBUSY)
  1068. /* Subchannel is disabled, we're done. */
  1069. return;
  1070. cdev->private->state = DEV_STATE_QUIESCE;
  1071. if (cdev->handler)
  1072. cdev->handler(cdev, cdev->private->intparm,
  1073. ERR_PTR(-EIO));
  1074. ret = ccw_device_cancel_halt_clear(cdev);
  1075. if (ret == -EBUSY) {
  1076. ccw_device_set_timeout(cdev, HZ/10);
  1077. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1078. }
  1079. cio_disable_subchannel(sch);
  1080. }
  1081. #ifdef CONFIG_CCW_CONSOLE
  1082. static struct ccw_device console_cdev;
  1083. static struct ccw_device_private console_private;
  1084. static int console_cdev_in_use;
  1085. static DEFINE_SPINLOCK(ccw_console_lock);
  1086. spinlock_t * cio_get_console_lock(void)
  1087. {
  1088. return &ccw_console_lock;
  1089. }
  1090. static int
  1091. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  1092. {
  1093. int rc;
  1094. /* Initialize the ccw_device structure. */
  1095. cdev->dev.parent= &sch->dev;
  1096. rc = io_subchannel_recog(cdev, sch);
  1097. if (rc)
  1098. return rc;
  1099. /* Now wait for the async. recognition to come to an end. */
  1100. spin_lock_irq(cdev->ccwlock);
  1101. while (!dev_fsm_final_state(cdev))
  1102. wait_cons_dev();
  1103. rc = -EIO;
  1104. if (cdev->private->state != DEV_STATE_OFFLINE)
  1105. goto out_unlock;
  1106. ccw_device_online(cdev);
  1107. while (!dev_fsm_final_state(cdev))
  1108. wait_cons_dev();
  1109. if (cdev->private->state != DEV_STATE_ONLINE)
  1110. goto out_unlock;
  1111. rc = 0;
  1112. out_unlock:
  1113. spin_unlock_irq(cdev->ccwlock);
  1114. return 0;
  1115. }
  1116. struct ccw_device *
  1117. ccw_device_probe_console(void)
  1118. {
  1119. struct subchannel *sch;
  1120. int ret;
  1121. if (xchg(&console_cdev_in_use, 1) != 0)
  1122. return ERR_PTR(-EBUSY);
  1123. sch = cio_probe_console();
  1124. if (IS_ERR(sch)) {
  1125. console_cdev_in_use = 0;
  1126. return (void *) sch;
  1127. }
  1128. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1129. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1130. console_cdev.private = &console_private;
  1131. console_private.cdev = &console_cdev;
  1132. ret = ccw_device_console_enable(&console_cdev, sch);
  1133. if (ret) {
  1134. cio_release_console();
  1135. console_cdev_in_use = 0;
  1136. return ERR_PTR(ret);
  1137. }
  1138. console_cdev.online = 1;
  1139. return &console_cdev;
  1140. }
  1141. #endif
  1142. /*
  1143. * get ccw_device matching the busid, but only if owned by cdrv
  1144. */
  1145. static int
  1146. __ccwdev_check_busid(struct device *dev, void *id)
  1147. {
  1148. char *bus_id;
  1149. bus_id = id;
  1150. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1151. }
  1152. struct ccw_device *
  1153. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  1154. {
  1155. struct device *dev;
  1156. struct device_driver *drv;
  1157. drv = get_driver(&cdrv->driver);
  1158. if (!drv)
  1159. return NULL;
  1160. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1161. __ccwdev_check_busid);
  1162. put_driver(drv);
  1163. return dev ? to_ccwdev(dev) : NULL;
  1164. }
  1165. /************************** device driver handling ************************/
  1166. /* This is the implementation of the ccw_driver class. The probe, remove
  1167. * and release methods are initially very similar to the device_driver
  1168. * implementations, with the difference that they have ccw_device
  1169. * arguments.
  1170. *
  1171. * A ccw driver also contains the information that is needed for
  1172. * device matching.
  1173. */
  1174. static int
  1175. ccw_device_probe (struct device *dev)
  1176. {
  1177. struct ccw_device *cdev = to_ccwdev(dev);
  1178. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1179. int ret;
  1180. cdev->drv = cdrv; /* to let the driver call _set_online */
  1181. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1182. if (ret) {
  1183. cdev->drv = NULL;
  1184. return ret;
  1185. }
  1186. return 0;
  1187. }
  1188. static int
  1189. ccw_device_remove (struct device *dev)
  1190. {
  1191. struct ccw_device *cdev = to_ccwdev(dev);
  1192. struct ccw_driver *cdrv = cdev->drv;
  1193. int ret;
  1194. pr_debug("removing device %s\n", cdev->dev.bus_id);
  1195. if (cdrv->remove)
  1196. cdrv->remove(cdev);
  1197. if (cdev->online) {
  1198. cdev->online = 0;
  1199. spin_lock_irq(cdev->ccwlock);
  1200. ret = ccw_device_offline(cdev);
  1201. spin_unlock_irq(cdev->ccwlock);
  1202. if (ret == 0)
  1203. wait_event(cdev->private->wait_q,
  1204. dev_fsm_final_state(cdev));
  1205. else
  1206. //FIXME: we can't fail!
  1207. pr_debug("ccw_device_offline returned %d, device %s\n",
  1208. ret, cdev->dev.bus_id);
  1209. }
  1210. ccw_device_set_timeout(cdev, 0);
  1211. cdev->drv = NULL;
  1212. return 0;
  1213. }
  1214. struct bus_type ccw_bus_type = {
  1215. .name = "ccw",
  1216. .match = ccw_bus_match,
  1217. .uevent = ccw_uevent,
  1218. .probe = ccw_device_probe,
  1219. .remove = ccw_device_remove,
  1220. };
  1221. int
  1222. ccw_driver_register (struct ccw_driver *cdriver)
  1223. {
  1224. struct device_driver *drv = &cdriver->driver;
  1225. drv->bus = &ccw_bus_type;
  1226. drv->name = cdriver->name;
  1227. return driver_register(drv);
  1228. }
  1229. void
  1230. ccw_driver_unregister (struct ccw_driver *cdriver)
  1231. {
  1232. driver_unregister(&cdriver->driver);
  1233. }
  1234. /* Helper func for qdio. */
  1235. struct subchannel_id
  1236. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1237. {
  1238. struct subchannel *sch;
  1239. sch = to_subchannel(cdev->dev.parent);
  1240. return sch->schid;
  1241. }
  1242. MODULE_LICENSE("GPL");
  1243. EXPORT_SYMBOL(ccw_device_set_online);
  1244. EXPORT_SYMBOL(ccw_device_set_offline);
  1245. EXPORT_SYMBOL(ccw_driver_register);
  1246. EXPORT_SYMBOL(ccw_driver_unregister);
  1247. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1248. EXPORT_SYMBOL(ccw_bus_type);
  1249. EXPORT_SYMBOL(ccw_device_work);
  1250. EXPORT_SYMBOL(ccw_device_notify_work);
  1251. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);