device.c 37 KB

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