device.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402
  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. static 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. static 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. /*
  764. * Now we know this subchannel will stay, we can throw
  765. * our delayed uevent.
  766. */
  767. sch->dev.uevent_suppress = 0;
  768. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  769. /* make it known to the system */
  770. ret = ccw_device_register(cdev);
  771. if (ret) {
  772. printk (KERN_WARNING "%s: could not register %s\n",
  773. __func__, cdev->dev.bus_id);
  774. put_device(&cdev->dev);
  775. spin_lock_irqsave(sch->lock, flags);
  776. sch->dev.driver_data = NULL;
  777. spin_unlock_irqrestore(sch->lock, flags);
  778. kfree (cdev->private);
  779. kfree (cdev);
  780. put_device(&sch->dev);
  781. if (atomic_dec_and_test(&ccw_device_init_count))
  782. wake_up(&ccw_device_init_wq);
  783. return;
  784. }
  785. put_device(&cdev->dev);
  786. out:
  787. cdev->private->flags.recog_done = 1;
  788. put_device(&sch->dev);
  789. wake_up(&cdev->private->wait_q);
  790. if (atomic_dec_and_test(&ccw_device_init_count))
  791. wake_up(&ccw_device_init_wq);
  792. }
  793. void
  794. ccw_device_call_sch_unregister(struct work_struct *work)
  795. {
  796. struct ccw_device_private *priv;
  797. struct ccw_device *cdev;
  798. struct subchannel *sch;
  799. priv = container_of(work, struct ccw_device_private, kick_work);
  800. cdev = priv->cdev;
  801. sch = to_subchannel(cdev->dev.parent);
  802. css_sch_device_unregister(sch);
  803. /* Reset intparm to zeroes. */
  804. sch->schib.pmcw.intparm = 0;
  805. cio_modify(sch);
  806. put_device(&cdev->dev);
  807. put_device(&sch->dev);
  808. }
  809. /*
  810. * subchannel recognition done. Called from the state machine.
  811. */
  812. void
  813. io_subchannel_recog_done(struct ccw_device *cdev)
  814. {
  815. struct subchannel *sch;
  816. if (css_init_done == 0) {
  817. cdev->private->flags.recog_done = 1;
  818. return;
  819. }
  820. switch (cdev->private->state) {
  821. case DEV_STATE_NOT_OPER:
  822. cdev->private->flags.recog_done = 1;
  823. /* Remove device found not operational. */
  824. if (!get_device(&cdev->dev))
  825. break;
  826. sch = to_subchannel(cdev->dev.parent);
  827. PREPARE_WORK(&cdev->private->kick_work,
  828. ccw_device_call_sch_unregister);
  829. queue_work(slow_path_wq, &cdev->private->kick_work);
  830. if (atomic_dec_and_test(&ccw_device_init_count))
  831. wake_up(&ccw_device_init_wq);
  832. break;
  833. case DEV_STATE_BOXED:
  834. /* Device did not respond in time. */
  835. case DEV_STATE_OFFLINE:
  836. /*
  837. * We can't register the device in interrupt context so
  838. * we schedule a work item.
  839. */
  840. if (!get_device(&cdev->dev))
  841. break;
  842. PREPARE_WORK(&cdev->private->kick_work,
  843. io_subchannel_register);
  844. queue_work(slow_path_wq, &cdev->private->kick_work);
  845. break;
  846. }
  847. }
  848. static int
  849. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  850. {
  851. int rc;
  852. struct ccw_device_private *priv;
  853. sch->dev.driver_data = cdev;
  854. sch->driver = &io_subchannel_driver;
  855. cdev->ccwlock = sch->lock;
  856. /* Init private data. */
  857. priv = cdev->private;
  858. priv->dev_id.devno = sch->schib.pmcw.dev;
  859. priv->dev_id.ssid = sch->schid.ssid;
  860. priv->schid = sch->schid;
  861. priv->state = DEV_STATE_NOT_OPER;
  862. INIT_LIST_HEAD(&priv->cmb_list);
  863. init_waitqueue_head(&priv->wait_q);
  864. init_timer(&priv->timer);
  865. /* Set an initial name for the device. */
  866. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  867. sch->schid.ssid, sch->schib.pmcw.dev);
  868. /* Increase counter of devices currently in recognition. */
  869. atomic_inc(&ccw_device_init_count);
  870. /* Start async. device sensing. */
  871. spin_lock_irq(sch->lock);
  872. rc = ccw_device_recognition(cdev);
  873. spin_unlock_irq(sch->lock);
  874. if (rc) {
  875. if (atomic_dec_and_test(&ccw_device_init_count))
  876. wake_up(&ccw_device_init_wq);
  877. }
  878. return rc;
  879. }
  880. static void ccw_device_move_to_sch(struct work_struct *work)
  881. {
  882. struct ccw_device_private *priv;
  883. int rc;
  884. struct subchannel *sch;
  885. struct ccw_device *cdev;
  886. struct subchannel *former_parent;
  887. priv = container_of(work, struct ccw_device_private, kick_work);
  888. sch = priv->sch;
  889. cdev = priv->cdev;
  890. former_parent = ccw_device_is_orphan(cdev) ?
  891. NULL : to_subchannel(get_device(cdev->dev.parent));
  892. mutex_lock(&sch->reg_mutex);
  893. /* Try to move the ccw device to its new subchannel. */
  894. rc = device_move(&cdev->dev, &sch->dev);
  895. mutex_unlock(&sch->reg_mutex);
  896. if (rc) {
  897. CIO_MSG_EVENT(2, "Moving device 0.%x.%04x to subchannel "
  898. "0.%x.%04x failed (ret=%d)!\n",
  899. cdev->private->dev_id.ssid,
  900. cdev->private->dev_id.devno, sch->schid.ssid,
  901. sch->schid.sch_no, rc);
  902. css_sch_device_unregister(sch);
  903. goto out;
  904. }
  905. if (former_parent) {
  906. spin_lock_irq(former_parent->lock);
  907. former_parent->dev.driver_data = NULL;
  908. spin_unlock_irq(former_parent->lock);
  909. css_sch_device_unregister(former_parent);
  910. /* Reset intparm to zeroes. */
  911. former_parent->schib.pmcw.intparm = 0;
  912. cio_modify(former_parent);
  913. }
  914. sch_attach_device(sch, cdev);
  915. out:
  916. if (former_parent)
  917. put_device(&former_parent->dev);
  918. put_device(&cdev->dev);
  919. }
  920. static int
  921. io_subchannel_probe (struct subchannel *sch)
  922. {
  923. struct ccw_device *cdev;
  924. int rc;
  925. unsigned long flags;
  926. struct ccw_dev_id dev_id;
  927. if (sch->dev.driver_data) {
  928. /*
  929. * This subchannel already has an associated ccw_device.
  930. * Register it and exit. This happens for all early
  931. * device, e.g. the console.
  932. */
  933. cdev = sch->dev.driver_data;
  934. device_initialize(&cdev->dev);
  935. ccw_device_register(cdev);
  936. /*
  937. * Check if the device is already online. If it is
  938. * the reference count needs to be corrected
  939. * (see ccw_device_online and css_init_done for the
  940. * ugly details).
  941. */
  942. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  943. cdev->private->state != DEV_STATE_OFFLINE &&
  944. cdev->private->state != DEV_STATE_BOXED)
  945. get_device(&cdev->dev);
  946. return 0;
  947. }
  948. /*
  949. * First check if a fitting device may be found amongst the
  950. * disconnected devices or in the orphanage.
  951. */
  952. dev_id.devno = sch->schib.pmcw.dev;
  953. dev_id.ssid = sch->schid.ssid;
  954. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  955. if (!cdev)
  956. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  957. &dev_id);
  958. if (cdev) {
  959. /*
  960. * Schedule moving the device until when we have a registered
  961. * subchannel to move to and succeed the probe. We can
  962. * unregister later again, when the probe is through.
  963. */
  964. cdev->private->sch = sch;
  965. PREPARE_WORK(&cdev->private->kick_work,
  966. ccw_device_move_to_sch);
  967. queue_work(slow_path_wq, &cdev->private->kick_work);
  968. return 0;
  969. }
  970. cdev = io_subchannel_create_ccwdev(sch);
  971. if (IS_ERR(cdev))
  972. return PTR_ERR(cdev);
  973. rc = io_subchannel_recog(cdev, sch);
  974. if (rc) {
  975. spin_lock_irqsave(sch->lock, flags);
  976. sch->dev.driver_data = NULL;
  977. spin_unlock_irqrestore(sch->lock, flags);
  978. if (cdev->dev.release)
  979. cdev->dev.release(&cdev->dev);
  980. }
  981. return rc;
  982. }
  983. static int
  984. io_subchannel_remove (struct subchannel *sch)
  985. {
  986. struct ccw_device *cdev;
  987. unsigned long flags;
  988. if (!sch->dev.driver_data)
  989. return 0;
  990. cdev = sch->dev.driver_data;
  991. /* Set ccw device to not operational and drop reference. */
  992. spin_lock_irqsave(cdev->ccwlock, flags);
  993. sch->dev.driver_data = NULL;
  994. cdev->private->state = DEV_STATE_NOT_OPER;
  995. spin_unlock_irqrestore(cdev->ccwlock, flags);
  996. ccw_device_unregister(cdev);
  997. put_device(&cdev->dev);
  998. return 0;
  999. }
  1000. static int
  1001. io_subchannel_notify(struct device *dev, int event)
  1002. {
  1003. struct ccw_device *cdev;
  1004. cdev = dev->driver_data;
  1005. if (!cdev)
  1006. return 0;
  1007. if (!cdev->drv)
  1008. return 0;
  1009. if (!cdev->online)
  1010. return 0;
  1011. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  1012. }
  1013. static void
  1014. io_subchannel_verify(struct device *dev)
  1015. {
  1016. struct ccw_device *cdev;
  1017. cdev = dev->driver_data;
  1018. if (cdev)
  1019. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1020. }
  1021. static void
  1022. io_subchannel_ioterm(struct device *dev)
  1023. {
  1024. struct ccw_device *cdev;
  1025. cdev = dev->driver_data;
  1026. if (!cdev)
  1027. return;
  1028. /* Internal I/O will be retried by the interrupt handler. */
  1029. if (cdev->private->flags.intretry)
  1030. return;
  1031. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1032. if (cdev->handler)
  1033. cdev->handler(cdev, cdev->private->intparm,
  1034. ERR_PTR(-EIO));
  1035. }
  1036. static void
  1037. io_subchannel_shutdown(struct subchannel *sch)
  1038. {
  1039. struct ccw_device *cdev;
  1040. int ret;
  1041. cdev = sch->dev.driver_data;
  1042. if (cio_is_console(sch->schid))
  1043. return;
  1044. if (!sch->schib.pmcw.ena)
  1045. /* Nothing to do. */
  1046. return;
  1047. ret = cio_disable_subchannel(sch);
  1048. if (ret != -EBUSY)
  1049. /* Subchannel is disabled, we're done. */
  1050. return;
  1051. cdev->private->state = DEV_STATE_QUIESCE;
  1052. if (cdev->handler)
  1053. cdev->handler(cdev, cdev->private->intparm,
  1054. ERR_PTR(-EIO));
  1055. ret = ccw_device_cancel_halt_clear(cdev);
  1056. if (ret == -EBUSY) {
  1057. ccw_device_set_timeout(cdev, HZ/10);
  1058. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1059. }
  1060. cio_disable_subchannel(sch);
  1061. }
  1062. #ifdef CONFIG_CCW_CONSOLE
  1063. static struct ccw_device console_cdev;
  1064. static struct ccw_device_private console_private;
  1065. static int console_cdev_in_use;
  1066. static DEFINE_SPINLOCK(ccw_console_lock);
  1067. spinlock_t * cio_get_console_lock(void)
  1068. {
  1069. return &ccw_console_lock;
  1070. }
  1071. static int
  1072. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  1073. {
  1074. int rc;
  1075. /* Initialize the ccw_device structure. */
  1076. cdev->dev.parent= &sch->dev;
  1077. rc = io_subchannel_recog(cdev, sch);
  1078. if (rc)
  1079. return rc;
  1080. /* Now wait for the async. recognition to come to an end. */
  1081. spin_lock_irq(cdev->ccwlock);
  1082. while (!dev_fsm_final_state(cdev))
  1083. wait_cons_dev();
  1084. rc = -EIO;
  1085. if (cdev->private->state != DEV_STATE_OFFLINE)
  1086. goto out_unlock;
  1087. ccw_device_online(cdev);
  1088. while (!dev_fsm_final_state(cdev))
  1089. wait_cons_dev();
  1090. if (cdev->private->state != DEV_STATE_ONLINE)
  1091. goto out_unlock;
  1092. rc = 0;
  1093. out_unlock:
  1094. spin_unlock_irq(cdev->ccwlock);
  1095. return 0;
  1096. }
  1097. struct ccw_device *
  1098. ccw_device_probe_console(void)
  1099. {
  1100. struct subchannel *sch;
  1101. int ret;
  1102. if (xchg(&console_cdev_in_use, 1) != 0)
  1103. return ERR_PTR(-EBUSY);
  1104. sch = cio_probe_console();
  1105. if (IS_ERR(sch)) {
  1106. console_cdev_in_use = 0;
  1107. return (void *) sch;
  1108. }
  1109. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1110. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1111. console_cdev.private = &console_private;
  1112. console_private.cdev = &console_cdev;
  1113. ret = ccw_device_console_enable(&console_cdev, sch);
  1114. if (ret) {
  1115. cio_release_console();
  1116. console_cdev_in_use = 0;
  1117. return ERR_PTR(ret);
  1118. }
  1119. console_cdev.online = 1;
  1120. return &console_cdev;
  1121. }
  1122. #endif
  1123. /*
  1124. * get ccw_device matching the busid, but only if owned by cdrv
  1125. */
  1126. static int
  1127. __ccwdev_check_busid(struct device *dev, void *id)
  1128. {
  1129. char *bus_id;
  1130. bus_id = id;
  1131. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1132. }
  1133. struct ccw_device *
  1134. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  1135. {
  1136. struct device *dev;
  1137. struct device_driver *drv;
  1138. drv = get_driver(&cdrv->driver);
  1139. if (!drv)
  1140. return NULL;
  1141. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1142. __ccwdev_check_busid);
  1143. put_driver(drv);
  1144. return dev ? to_ccwdev(dev) : NULL;
  1145. }
  1146. /************************** device driver handling ************************/
  1147. /* This is the implementation of the ccw_driver class. The probe, remove
  1148. * and release methods are initially very similar to the device_driver
  1149. * implementations, with the difference that they have ccw_device
  1150. * arguments.
  1151. *
  1152. * A ccw driver also contains the information that is needed for
  1153. * device matching.
  1154. */
  1155. static int
  1156. ccw_device_probe (struct device *dev)
  1157. {
  1158. struct ccw_device *cdev = to_ccwdev(dev);
  1159. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1160. int ret;
  1161. cdev->drv = cdrv; /* to let the driver call _set_online */
  1162. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1163. if (ret) {
  1164. cdev->drv = NULL;
  1165. return ret;
  1166. }
  1167. return 0;
  1168. }
  1169. static int
  1170. ccw_device_remove (struct device *dev)
  1171. {
  1172. struct ccw_device *cdev = to_ccwdev(dev);
  1173. struct ccw_driver *cdrv = cdev->drv;
  1174. int ret;
  1175. pr_debug("removing device %s\n", cdev->dev.bus_id);
  1176. if (cdrv->remove)
  1177. cdrv->remove(cdev);
  1178. if (cdev->online) {
  1179. cdev->online = 0;
  1180. spin_lock_irq(cdev->ccwlock);
  1181. ret = ccw_device_offline(cdev);
  1182. spin_unlock_irq(cdev->ccwlock);
  1183. if (ret == 0)
  1184. wait_event(cdev->private->wait_q,
  1185. dev_fsm_final_state(cdev));
  1186. else
  1187. //FIXME: we can't fail!
  1188. pr_debug("ccw_device_offline returned %d, device %s\n",
  1189. ret, cdev->dev.bus_id);
  1190. }
  1191. ccw_device_set_timeout(cdev, 0);
  1192. cdev->drv = NULL;
  1193. return 0;
  1194. }
  1195. struct bus_type ccw_bus_type = {
  1196. .name = "ccw",
  1197. .match = ccw_bus_match,
  1198. .uevent = ccw_uevent,
  1199. .probe = ccw_device_probe,
  1200. .remove = ccw_device_remove,
  1201. };
  1202. int
  1203. ccw_driver_register (struct ccw_driver *cdriver)
  1204. {
  1205. struct device_driver *drv = &cdriver->driver;
  1206. drv->bus = &ccw_bus_type;
  1207. drv->name = cdriver->name;
  1208. return driver_register(drv);
  1209. }
  1210. void
  1211. ccw_driver_unregister (struct ccw_driver *cdriver)
  1212. {
  1213. driver_unregister(&cdriver->driver);
  1214. }
  1215. /* Helper func for qdio. */
  1216. struct subchannel_id
  1217. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1218. {
  1219. struct subchannel *sch;
  1220. sch = to_subchannel(cdev->dev.parent);
  1221. return sch->schid;
  1222. }
  1223. MODULE_LICENSE("GPL");
  1224. EXPORT_SYMBOL(ccw_device_set_online);
  1225. EXPORT_SYMBOL(ccw_device_set_offline);
  1226. EXPORT_SYMBOL(ccw_driver_register);
  1227. EXPORT_SYMBOL(ccw_driver_unregister);
  1228. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1229. EXPORT_SYMBOL(ccw_bus_type);
  1230. EXPORT_SYMBOL(ccw_device_work);
  1231. EXPORT_SYMBOL(ccw_device_notify_work);
  1232. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);