device.c 34 KB

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