device.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. * $Revision: 1.131 $
  5. *
  6. * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
  7. * IBM Corporation
  8. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  9. * Cornelia Huck (cohuck@de.ibm.com)
  10. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  11. */
  12. #include <linux/config.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <linux/slab.h>
  19. #include <linux/list.h>
  20. #include <linux/device.h>
  21. #include <linux/workqueue.h>
  22. #include <asm/ccwdev.h>
  23. #include <asm/cio.h>
  24. #include "cio.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. /*
  48. * Hotplugging interface for ccw devices.
  49. * Heavily modeled on pci and usb hotplug.
  50. */
  51. static int
  52. ccw_hotplug (struct device *dev, char **envp, int num_envp,
  53. char *buffer, int buffer_size)
  54. {
  55. struct ccw_device *cdev = to_ccwdev(dev);
  56. int i = 0;
  57. int length = 0;
  58. if (!cdev)
  59. return -ENODEV;
  60. /* what we want to pass to /sbin/hotplug */
  61. envp[i++] = buffer;
  62. length += scnprintf(buffer, buffer_size - length, "CU_TYPE=%04X",
  63. cdev->id.cu_type);
  64. if ((buffer_size - length <= 0) || (i >= num_envp))
  65. return -ENOMEM;
  66. ++length;
  67. buffer += length;
  68. envp[i++] = buffer;
  69. length += scnprintf(buffer, buffer_size - length, "CU_MODEL=%02X",
  70. cdev->id.cu_model);
  71. if ((buffer_size - length <= 0) || (i >= num_envp))
  72. return -ENOMEM;
  73. ++length;
  74. buffer += length;
  75. /* The next two can be zero, that's ok for us */
  76. envp[i++] = buffer;
  77. length += scnprintf(buffer, buffer_size - length, "DEV_TYPE=%04X",
  78. cdev->id.dev_type);
  79. if ((buffer_size - length <= 0) || (i >= num_envp))
  80. return -ENOMEM;
  81. ++length;
  82. buffer += length;
  83. envp[i++] = buffer;
  84. length += scnprintf(buffer, buffer_size - length, "DEV_MODEL=%02X",
  85. cdev->id.dev_model);
  86. if ((buffer_size - length <= 0) || (i >= num_envp))
  87. return -ENOMEM;
  88. envp[i] = 0;
  89. return 0;
  90. }
  91. struct bus_type ccw_bus_type = {
  92. .name = "ccw",
  93. .match = &ccw_bus_match,
  94. .hotplug = &ccw_hotplug,
  95. };
  96. static int io_subchannel_probe (struct device *);
  97. static int io_subchannel_remove (struct device *);
  98. void io_subchannel_irq (struct device *);
  99. static int io_subchannel_notify(struct device *, int);
  100. static void io_subchannel_verify(struct device *);
  101. static void io_subchannel_ioterm(struct device *);
  102. static void io_subchannel_shutdown(struct device *);
  103. struct css_driver io_subchannel_driver = {
  104. .subchannel_type = SUBCHANNEL_TYPE_IO,
  105. .drv = {
  106. .name = "io_subchannel",
  107. .bus = &css_bus_type,
  108. .probe = &io_subchannel_probe,
  109. .remove = &io_subchannel_remove,
  110. .shutdown = &io_subchannel_shutdown,
  111. },
  112. .irq = io_subchannel_irq,
  113. .notify = io_subchannel_notify,
  114. .verify = io_subchannel_verify,
  115. .termination = io_subchannel_ioterm,
  116. };
  117. struct workqueue_struct *ccw_device_work;
  118. struct workqueue_struct *ccw_device_notify_work;
  119. static wait_queue_head_t ccw_device_init_wq;
  120. static atomic_t ccw_device_init_count;
  121. static int __init
  122. init_ccw_bus_type (void)
  123. {
  124. int ret;
  125. init_waitqueue_head(&ccw_device_init_wq);
  126. atomic_set(&ccw_device_init_count, 0);
  127. ccw_device_work = create_singlethread_workqueue("cio");
  128. if (!ccw_device_work)
  129. return -ENOMEM; /* FIXME: better errno ? */
  130. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  131. if (!ccw_device_notify_work) {
  132. ret = -ENOMEM; /* FIXME: better errno ? */
  133. goto out_err;
  134. }
  135. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  136. if (!slow_path_wq) {
  137. ret = -ENOMEM; /* FIXME: better errno ? */
  138. goto out_err;
  139. }
  140. if ((ret = bus_register (&ccw_bus_type)))
  141. goto out_err;
  142. if ((ret = driver_register(&io_subchannel_driver.drv)))
  143. goto out_err;
  144. wait_event(ccw_device_init_wq,
  145. atomic_read(&ccw_device_init_count) == 0);
  146. flush_workqueue(ccw_device_work);
  147. return 0;
  148. out_err:
  149. if (ccw_device_work)
  150. destroy_workqueue(ccw_device_work);
  151. if (ccw_device_notify_work)
  152. destroy_workqueue(ccw_device_notify_work);
  153. if (slow_path_wq)
  154. destroy_workqueue(slow_path_wq);
  155. return ret;
  156. }
  157. static void __exit
  158. cleanup_ccw_bus_type (void)
  159. {
  160. driver_unregister(&io_subchannel_driver.drv);
  161. bus_unregister(&ccw_bus_type);
  162. destroy_workqueue(ccw_device_notify_work);
  163. destroy_workqueue(ccw_device_work);
  164. }
  165. subsys_initcall(init_ccw_bus_type);
  166. module_exit(cleanup_ccw_bus_type);
  167. /************************ device handling **************************/
  168. /*
  169. * A ccw_device has some interfaces in sysfs in addition to the
  170. * standard ones.
  171. * The following entries are designed to export the information which
  172. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  173. * are obvious, so they don't have an entry :)
  174. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  175. */
  176. static ssize_t
  177. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  178. {
  179. struct subchannel *sch = to_subchannel(dev);
  180. struct ssd_info *ssd = &sch->ssd_info;
  181. ssize_t ret = 0;
  182. int chp;
  183. for (chp = 0; chp < 8; chp++)
  184. ret += sprintf (buf+ret, "%02x ", ssd->chpid[chp]);
  185. ret += sprintf (buf+ret, "\n");
  186. return min((ssize_t)PAGE_SIZE, ret);
  187. }
  188. static ssize_t
  189. pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
  190. {
  191. struct subchannel *sch = to_subchannel(dev);
  192. struct pmcw *pmcw = &sch->schib.pmcw;
  193. return sprintf (buf, "%02x %02x %02x\n",
  194. pmcw->pim, pmcw->pam, pmcw->pom);
  195. }
  196. static ssize_t
  197. devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
  198. {
  199. struct ccw_device *cdev = to_ccwdev(dev);
  200. struct ccw_device_id *id = &(cdev->id);
  201. if (id->dev_type != 0)
  202. return sprintf(buf, "%04x/%02x\n",
  203. id->dev_type, id->dev_model);
  204. else
  205. return sprintf(buf, "n/a\n");
  206. }
  207. static ssize_t
  208. cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
  209. {
  210. struct ccw_device *cdev = to_ccwdev(dev);
  211. struct ccw_device_id *id = &(cdev->id);
  212. return sprintf(buf, "%04x/%02x\n",
  213. id->cu_type, id->cu_model);
  214. }
  215. static ssize_t
  216. online_show (struct device *dev, struct device_attribute *attr, char *buf)
  217. {
  218. struct ccw_device *cdev = to_ccwdev(dev);
  219. return sprintf(buf, cdev->online ? "1\n" : "0\n");
  220. }
  221. static void
  222. ccw_device_remove_disconnected(struct ccw_device *cdev)
  223. {
  224. struct subchannel *sch;
  225. /*
  226. * Forced offline in disconnected state means
  227. * 'throw away device'.
  228. */
  229. sch = to_subchannel(cdev->dev.parent);
  230. device_unregister(&sch->dev);
  231. /* Reset intparm to zeroes. */
  232. sch->schib.pmcw.intparm = 0;
  233. cio_modify(sch);
  234. put_device(&sch->dev);
  235. }
  236. int
  237. ccw_device_set_offline(struct ccw_device *cdev)
  238. {
  239. int ret;
  240. if (!cdev)
  241. return -ENODEV;
  242. if (!cdev->online || !cdev->drv)
  243. return -EINVAL;
  244. if (cdev->drv->set_offline) {
  245. ret = cdev->drv->set_offline(cdev);
  246. if (ret != 0)
  247. return ret;
  248. }
  249. cdev->online = 0;
  250. spin_lock_irq(cdev->ccwlock);
  251. ret = ccw_device_offline(cdev);
  252. if (ret == -ENODEV) {
  253. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  254. cdev->private->state = DEV_STATE_OFFLINE;
  255. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  256. }
  257. spin_unlock_irq(cdev->ccwlock);
  258. return ret;
  259. }
  260. spin_unlock_irq(cdev->ccwlock);
  261. if (ret == 0)
  262. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  263. else {
  264. pr_debug("ccw_device_offline returned %d, device %s\n",
  265. ret, cdev->dev.bus_id);
  266. cdev->online = 1;
  267. }
  268. return ret;
  269. }
  270. int
  271. ccw_device_set_online(struct ccw_device *cdev)
  272. {
  273. int ret;
  274. if (!cdev)
  275. return -ENODEV;
  276. if (cdev->online || !cdev->drv)
  277. return -EINVAL;
  278. spin_lock_irq(cdev->ccwlock);
  279. ret = ccw_device_online(cdev);
  280. spin_unlock_irq(cdev->ccwlock);
  281. if (ret == 0)
  282. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  283. else {
  284. pr_debug("ccw_device_online returned %d, device %s\n",
  285. ret, cdev->dev.bus_id);
  286. return ret;
  287. }
  288. if (cdev->private->state != DEV_STATE_ONLINE)
  289. return -ENODEV;
  290. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  291. cdev->online = 1;
  292. return 0;
  293. }
  294. spin_lock_irq(cdev->ccwlock);
  295. ret = ccw_device_offline(cdev);
  296. spin_unlock_irq(cdev->ccwlock);
  297. if (ret == 0)
  298. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  299. else
  300. pr_debug("ccw_device_offline returned %d, device %s\n",
  301. ret, cdev->dev.bus_id);
  302. return (ret = 0) ? -ENODEV : ret;
  303. }
  304. static ssize_t
  305. online_store (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  306. {
  307. struct ccw_device *cdev = to_ccwdev(dev);
  308. int i, force, ret;
  309. char *tmp;
  310. if (atomic_compare_and_swap(0, 1, &cdev->private->onoff))
  311. return -EAGAIN;
  312. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  313. atomic_set(&cdev->private->onoff, 0);
  314. return -EINVAL;
  315. }
  316. if (!strncmp(buf, "force\n", count)) {
  317. force = 1;
  318. i = 1;
  319. } else {
  320. force = 0;
  321. i = simple_strtoul(buf, &tmp, 16);
  322. }
  323. if (i == 1) {
  324. /* Do device recognition, if needed. */
  325. if (cdev->id.cu_type == 0) {
  326. ret = ccw_device_recognition(cdev);
  327. if (ret) {
  328. printk(KERN_WARNING"Couldn't start recognition "
  329. "for device %s (ret=%d)\n",
  330. cdev->dev.bus_id, ret);
  331. goto out;
  332. }
  333. wait_event(cdev->private->wait_q,
  334. cdev->private->flags.recog_done);
  335. }
  336. if (cdev->drv && cdev->drv->set_online)
  337. ccw_device_set_online(cdev);
  338. } else if (i == 0) {
  339. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  340. ccw_device_remove_disconnected(cdev);
  341. else if (cdev->drv && cdev->drv->set_offline)
  342. ccw_device_set_offline(cdev);
  343. }
  344. if (force && cdev->private->state == DEV_STATE_BOXED) {
  345. ret = ccw_device_stlck(cdev);
  346. if (ret) {
  347. printk(KERN_WARNING"ccw_device_stlck for device %s "
  348. "returned %d!\n", cdev->dev.bus_id, ret);
  349. goto out;
  350. }
  351. /* Do device recognition, if needed. */
  352. if (cdev->id.cu_type == 0) {
  353. cdev->private->state = DEV_STATE_NOT_OPER;
  354. ret = ccw_device_recognition(cdev);
  355. if (ret) {
  356. printk(KERN_WARNING"Couldn't start recognition "
  357. "for device %s (ret=%d)\n",
  358. cdev->dev.bus_id, ret);
  359. goto out;
  360. }
  361. wait_event(cdev->private->wait_q,
  362. cdev->private->flags.recog_done);
  363. }
  364. if (cdev->drv && cdev->drv->set_online)
  365. ccw_device_set_online(cdev);
  366. }
  367. out:
  368. if (cdev->drv)
  369. module_put(cdev->drv->owner);
  370. atomic_set(&cdev->private->onoff, 0);
  371. return count;
  372. }
  373. static ssize_t
  374. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  375. {
  376. struct ccw_device *cdev = to_ccwdev(dev);
  377. struct subchannel *sch;
  378. switch (cdev->private->state) {
  379. case DEV_STATE_BOXED:
  380. return sprintf(buf, "boxed\n");
  381. case DEV_STATE_DISCONNECTED:
  382. case DEV_STATE_DISCONNECTED_SENSE_ID:
  383. case DEV_STATE_NOT_OPER:
  384. sch = to_subchannel(dev->parent);
  385. if (!sch->lpm)
  386. return sprintf(buf, "no path\n");
  387. else
  388. return sprintf(buf, "no device\n");
  389. default:
  390. /* All other states considered fine. */
  391. return sprintf(buf, "good\n");
  392. }
  393. }
  394. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  395. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  396. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  397. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  398. static DEVICE_ATTR(online, 0644, online_show, online_store);
  399. extern struct device_attribute dev_attr_cmb_enable;
  400. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  401. static struct attribute * subch_attrs[] = {
  402. &dev_attr_chpids.attr,
  403. &dev_attr_pimpampom.attr,
  404. NULL,
  405. };
  406. static struct attribute_group subch_attr_group = {
  407. .attrs = subch_attrs,
  408. };
  409. static inline int
  410. subchannel_add_files (struct device *dev)
  411. {
  412. return sysfs_create_group(&dev->kobj, &subch_attr_group);
  413. }
  414. static struct attribute * ccwdev_attrs[] = {
  415. &dev_attr_devtype.attr,
  416. &dev_attr_cutype.attr,
  417. &dev_attr_online.attr,
  418. &dev_attr_cmb_enable.attr,
  419. &dev_attr_availability.attr,
  420. NULL,
  421. };
  422. static struct attribute_group ccwdev_attr_group = {
  423. .attrs = ccwdev_attrs,
  424. };
  425. static inline int
  426. device_add_files (struct device *dev)
  427. {
  428. return sysfs_create_group(&dev->kobj, &ccwdev_attr_group);
  429. }
  430. static inline void
  431. device_remove_files(struct device *dev)
  432. {
  433. sysfs_remove_group(&dev->kobj, &ccwdev_attr_group);
  434. }
  435. /* this is a simple abstraction for device_register that sets the
  436. * correct bus type and adds the bus specific files */
  437. int
  438. ccw_device_register(struct ccw_device *cdev)
  439. {
  440. struct device *dev = &cdev->dev;
  441. int ret;
  442. dev->bus = &ccw_bus_type;
  443. if ((ret = device_add(dev)))
  444. return ret;
  445. set_bit(1, &cdev->private->registered);
  446. if ((ret = device_add_files(dev))) {
  447. if (test_and_clear_bit(1, &cdev->private->registered))
  448. device_del(dev);
  449. }
  450. return ret;
  451. }
  452. struct match_data {
  453. unsigned int devno;
  454. struct ccw_device * sibling;
  455. };
  456. static int
  457. match_devno(struct device * dev, void * data)
  458. {
  459. struct match_data * d = (struct match_data *)data;
  460. struct ccw_device * cdev;
  461. cdev = to_ccwdev(dev);
  462. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  463. (cdev->private->devno == d->devno) &&
  464. (cdev != d->sibling)) {
  465. cdev->private->state = DEV_STATE_NOT_OPER;
  466. return 1;
  467. }
  468. return 0;
  469. }
  470. static struct ccw_device *
  471. get_disc_ccwdev_by_devno(unsigned int devno, struct ccw_device *sibling)
  472. {
  473. struct device *dev;
  474. struct match_data data = {
  475. .devno = devno,
  476. .sibling = sibling,
  477. };
  478. dev = bus_find_device(&css_bus_type, NULL, &data, match_devno);
  479. return dev ? to_ccwdev(dev) : NULL;
  480. }
  481. static void
  482. ccw_device_add_changed(void *data)
  483. {
  484. struct ccw_device *cdev;
  485. cdev = (struct ccw_device *)data;
  486. if (device_add(&cdev->dev)) {
  487. put_device(&cdev->dev);
  488. return;
  489. }
  490. set_bit(1, &cdev->private->registered);
  491. if (device_add_files(&cdev->dev)) {
  492. if (test_and_clear_bit(1, &cdev->private->registered))
  493. device_unregister(&cdev->dev);
  494. }
  495. }
  496. extern int css_get_ssd_info(struct subchannel *sch);
  497. void
  498. ccw_device_do_unreg_rereg(void *data)
  499. {
  500. struct ccw_device *cdev;
  501. struct subchannel *sch;
  502. int need_rename;
  503. cdev = (struct ccw_device *)data;
  504. sch = to_subchannel(cdev->dev.parent);
  505. if (cdev->private->devno != sch->schib.pmcw.dev) {
  506. /*
  507. * The device number has changed. This is usually only when
  508. * a device has been detached under VM and then re-appeared
  509. * on another subchannel because of a different attachment
  510. * order than before. Ideally, we should should just switch
  511. * subchannels, but unfortunately, this is not possible with
  512. * the current implementation.
  513. * Instead, we search for the old subchannel for this device
  514. * number and deregister so there are no collisions with the
  515. * newly registered ccw_device.
  516. * FIXME: Find another solution so the block layer doesn't
  517. * get possibly sick...
  518. */
  519. struct ccw_device *other_cdev;
  520. need_rename = 1;
  521. other_cdev = get_disc_ccwdev_by_devno(sch->schib.pmcw.dev,
  522. cdev);
  523. if (other_cdev) {
  524. struct subchannel *other_sch;
  525. other_sch = to_subchannel(other_cdev->dev.parent);
  526. if (get_device(&other_sch->dev)) {
  527. stsch(other_sch->irq, &other_sch->schib);
  528. if (other_sch->schib.pmcw.dnv) {
  529. other_sch->schib.pmcw.intparm = 0;
  530. cio_modify(other_sch);
  531. }
  532. device_unregister(&other_sch->dev);
  533. }
  534. }
  535. /* Update ssd info here. */
  536. css_get_ssd_info(sch);
  537. cdev->private->devno = sch->schib.pmcw.dev;
  538. } else
  539. need_rename = 0;
  540. device_remove_files(&cdev->dev);
  541. if (test_and_clear_bit(1, &cdev->private->registered))
  542. device_del(&cdev->dev);
  543. if (need_rename)
  544. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.0.%04x",
  545. sch->schib.pmcw.dev);
  546. PREPARE_WORK(&cdev->private->kick_work,
  547. ccw_device_add_changed, (void *)cdev);
  548. queue_work(ccw_device_work, &cdev->private->kick_work);
  549. }
  550. static void
  551. ccw_device_release(struct device *dev)
  552. {
  553. struct ccw_device *cdev;
  554. cdev = to_ccwdev(dev);
  555. kfree(cdev->private);
  556. kfree(cdev);
  557. }
  558. /*
  559. * Register recognized device.
  560. */
  561. static void
  562. io_subchannel_register(void *data)
  563. {
  564. struct ccw_device *cdev;
  565. struct subchannel *sch;
  566. int ret;
  567. unsigned long flags;
  568. cdev = (struct ccw_device *) data;
  569. sch = to_subchannel(cdev->dev.parent);
  570. if (klist_node_attached(&cdev->dev.knode_parent)) {
  571. bus_rescan_devices(&ccw_bus_type);
  572. goto out;
  573. }
  574. /* make it known to the system */
  575. ret = ccw_device_register(cdev);
  576. if (ret) {
  577. printk (KERN_WARNING "%s: could not register %s\n",
  578. __func__, cdev->dev.bus_id);
  579. put_device(&cdev->dev);
  580. spin_lock_irqsave(&sch->lock, flags);
  581. sch->dev.driver_data = NULL;
  582. spin_unlock_irqrestore(&sch->lock, flags);
  583. kfree (cdev->private);
  584. kfree (cdev);
  585. put_device(&sch->dev);
  586. if (atomic_dec_and_test(&ccw_device_init_count))
  587. wake_up(&ccw_device_init_wq);
  588. return;
  589. }
  590. ret = subchannel_add_files(cdev->dev.parent);
  591. if (ret)
  592. printk(KERN_WARNING "%s: could not add attributes to %s\n",
  593. __func__, sch->dev.bus_id);
  594. put_device(&cdev->dev);
  595. out:
  596. cdev->private->flags.recog_done = 1;
  597. put_device(&sch->dev);
  598. wake_up(&cdev->private->wait_q);
  599. if (atomic_dec_and_test(&ccw_device_init_count))
  600. wake_up(&ccw_device_init_wq);
  601. }
  602. void
  603. ccw_device_call_sch_unregister(void *data)
  604. {
  605. struct ccw_device *cdev = data;
  606. struct subchannel *sch;
  607. sch = to_subchannel(cdev->dev.parent);
  608. device_unregister(&sch->dev);
  609. /* Reset intparm to zeroes. */
  610. sch->schib.pmcw.intparm = 0;
  611. cio_modify(sch);
  612. put_device(&cdev->dev);
  613. put_device(&sch->dev);
  614. }
  615. /*
  616. * subchannel recognition done. Called from the state machine.
  617. */
  618. void
  619. io_subchannel_recog_done(struct ccw_device *cdev)
  620. {
  621. struct subchannel *sch;
  622. if (css_init_done == 0) {
  623. cdev->private->flags.recog_done = 1;
  624. return;
  625. }
  626. switch (cdev->private->state) {
  627. case DEV_STATE_NOT_OPER:
  628. cdev->private->flags.recog_done = 1;
  629. /* Remove device found not operational. */
  630. if (!get_device(&cdev->dev))
  631. break;
  632. sch = to_subchannel(cdev->dev.parent);
  633. PREPARE_WORK(&cdev->private->kick_work,
  634. ccw_device_call_sch_unregister, (void *) cdev);
  635. queue_work(slow_path_wq, &cdev->private->kick_work);
  636. if (atomic_dec_and_test(&ccw_device_init_count))
  637. wake_up(&ccw_device_init_wq);
  638. break;
  639. case DEV_STATE_BOXED:
  640. /* Device did not respond in time. */
  641. case DEV_STATE_OFFLINE:
  642. /*
  643. * We can't register the device in interrupt context so
  644. * we schedule a work item.
  645. */
  646. if (!get_device(&cdev->dev))
  647. break;
  648. PREPARE_WORK(&cdev->private->kick_work,
  649. io_subchannel_register, (void *) cdev);
  650. queue_work(slow_path_wq, &cdev->private->kick_work);
  651. break;
  652. }
  653. }
  654. static int
  655. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  656. {
  657. int rc;
  658. struct ccw_device_private *priv;
  659. sch->dev.driver_data = cdev;
  660. sch->driver = &io_subchannel_driver;
  661. cdev->ccwlock = &sch->lock;
  662. /* Init private data. */
  663. priv = cdev->private;
  664. priv->devno = sch->schib.pmcw.dev;
  665. priv->irq = sch->irq;
  666. priv->state = DEV_STATE_NOT_OPER;
  667. INIT_LIST_HEAD(&priv->cmb_list);
  668. init_waitqueue_head(&priv->wait_q);
  669. init_timer(&priv->timer);
  670. /* Set an initial name for the device. */
  671. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.0.%04x",
  672. sch->schib.pmcw.dev);
  673. /* Increase counter of devices currently in recognition. */
  674. atomic_inc(&ccw_device_init_count);
  675. /* Start async. device sensing. */
  676. spin_lock_irq(&sch->lock);
  677. rc = ccw_device_recognition(cdev);
  678. spin_unlock_irq(&sch->lock);
  679. if (rc) {
  680. if (atomic_dec_and_test(&ccw_device_init_count))
  681. wake_up(&ccw_device_init_wq);
  682. }
  683. return rc;
  684. }
  685. static int
  686. io_subchannel_probe (struct device *pdev)
  687. {
  688. struct subchannel *sch;
  689. struct ccw_device *cdev;
  690. int rc;
  691. unsigned long flags;
  692. sch = to_subchannel(pdev);
  693. if (sch->dev.driver_data) {
  694. /*
  695. * This subchannel already has an associated ccw_device.
  696. * Register it and exit. This happens for all early
  697. * device, e.g. the console.
  698. */
  699. cdev = sch->dev.driver_data;
  700. device_initialize(&cdev->dev);
  701. ccw_device_register(cdev);
  702. subchannel_add_files(&sch->dev);
  703. /*
  704. * Check if the device is already online. If it is
  705. * the reference count needs to be corrected
  706. * (see ccw_device_online and css_init_done for the
  707. * ugly details).
  708. */
  709. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  710. cdev->private->state != DEV_STATE_OFFLINE &&
  711. cdev->private->state != DEV_STATE_BOXED)
  712. get_device(&cdev->dev);
  713. return 0;
  714. }
  715. cdev = kmalloc (sizeof(*cdev), GFP_KERNEL);
  716. if (!cdev)
  717. return -ENOMEM;
  718. memset(cdev, 0, sizeof(struct ccw_device));
  719. cdev->private = kmalloc(sizeof(struct ccw_device_private),
  720. GFP_KERNEL | GFP_DMA);
  721. if (!cdev->private) {
  722. kfree(cdev);
  723. return -ENOMEM;
  724. }
  725. memset(cdev->private, 0, sizeof(struct ccw_device_private));
  726. atomic_set(&cdev->private->onoff, 0);
  727. cdev->dev = (struct device) {
  728. .parent = pdev,
  729. .release = ccw_device_release,
  730. };
  731. INIT_LIST_HEAD(&cdev->private->kick_work.entry);
  732. /* Do first half of device_register. */
  733. device_initialize(&cdev->dev);
  734. if (!get_device(&sch->dev)) {
  735. if (cdev->dev.release)
  736. cdev->dev.release(&cdev->dev);
  737. return -ENODEV;
  738. }
  739. rc = io_subchannel_recog(cdev, to_subchannel(pdev));
  740. if (rc) {
  741. spin_lock_irqsave(&sch->lock, flags);
  742. sch->dev.driver_data = NULL;
  743. spin_unlock_irqrestore(&sch->lock, flags);
  744. if (cdev->dev.release)
  745. cdev->dev.release(&cdev->dev);
  746. }
  747. return rc;
  748. }
  749. static void
  750. ccw_device_unregister(void *data)
  751. {
  752. struct ccw_device *cdev;
  753. cdev = (struct ccw_device *)data;
  754. if (test_and_clear_bit(1, &cdev->private->registered))
  755. device_unregister(&cdev->dev);
  756. put_device(&cdev->dev);
  757. }
  758. static int
  759. io_subchannel_remove (struct device *dev)
  760. {
  761. struct ccw_device *cdev;
  762. unsigned long flags;
  763. if (!dev->driver_data)
  764. return 0;
  765. cdev = dev->driver_data;
  766. /* Set ccw device to not operational and drop reference. */
  767. spin_lock_irqsave(cdev->ccwlock, flags);
  768. dev->driver_data = NULL;
  769. cdev->private->state = DEV_STATE_NOT_OPER;
  770. spin_unlock_irqrestore(cdev->ccwlock, flags);
  771. /*
  772. * Put unregistration on workqueue to avoid livelocks on the css bus
  773. * semaphore.
  774. */
  775. if (get_device(&cdev->dev)) {
  776. PREPARE_WORK(&cdev->private->kick_work,
  777. ccw_device_unregister, (void *) cdev);
  778. queue_work(ccw_device_work, &cdev->private->kick_work);
  779. }
  780. return 0;
  781. }
  782. static int
  783. io_subchannel_notify(struct device *dev, int event)
  784. {
  785. struct ccw_device *cdev;
  786. cdev = dev->driver_data;
  787. if (!cdev)
  788. return 0;
  789. if (!cdev->drv)
  790. return 0;
  791. if (!cdev->online)
  792. return 0;
  793. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  794. }
  795. static void
  796. io_subchannel_verify(struct device *dev)
  797. {
  798. struct ccw_device *cdev;
  799. cdev = dev->driver_data;
  800. if (cdev)
  801. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  802. }
  803. static void
  804. io_subchannel_ioterm(struct device *dev)
  805. {
  806. struct ccw_device *cdev;
  807. cdev = dev->driver_data;
  808. if (!cdev)
  809. return;
  810. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  811. if (cdev->handler)
  812. cdev->handler(cdev, cdev->private->intparm,
  813. ERR_PTR(-EIO));
  814. }
  815. static void
  816. io_subchannel_shutdown(struct device *dev)
  817. {
  818. struct subchannel *sch;
  819. struct ccw_device *cdev;
  820. int ret;
  821. sch = to_subchannel(dev);
  822. cdev = dev->driver_data;
  823. if (cio_is_console(sch->irq))
  824. return;
  825. if (!sch->schib.pmcw.ena)
  826. /* Nothing to do. */
  827. return;
  828. ret = cio_disable_subchannel(sch);
  829. if (ret != -EBUSY)
  830. /* Subchannel is disabled, we're done. */
  831. return;
  832. cdev->private->state = DEV_STATE_QUIESCE;
  833. if (cdev->handler)
  834. cdev->handler(cdev, cdev->private->intparm,
  835. ERR_PTR(-EIO));
  836. ret = ccw_device_cancel_halt_clear(cdev);
  837. if (ret == -EBUSY) {
  838. ccw_device_set_timeout(cdev, HZ/10);
  839. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  840. }
  841. cio_disable_subchannel(sch);
  842. }
  843. #ifdef CONFIG_CCW_CONSOLE
  844. static struct ccw_device console_cdev;
  845. static struct ccw_device_private console_private;
  846. static int console_cdev_in_use;
  847. static int
  848. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  849. {
  850. int rc;
  851. /* Initialize the ccw_device structure. */
  852. cdev->dev = (struct device) {
  853. .parent = &sch->dev,
  854. };
  855. /* Initialize the subchannel structure */
  856. sch->dev.parent = &css_bus_device;
  857. sch->dev.bus = &css_bus_type;
  858. rc = io_subchannel_recog(cdev, sch);
  859. if (rc)
  860. return rc;
  861. /* Now wait for the async. recognition to come to an end. */
  862. spin_lock_irq(cdev->ccwlock);
  863. while (!dev_fsm_final_state(cdev))
  864. wait_cons_dev();
  865. rc = -EIO;
  866. if (cdev->private->state != DEV_STATE_OFFLINE)
  867. goto out_unlock;
  868. ccw_device_online(cdev);
  869. while (!dev_fsm_final_state(cdev))
  870. wait_cons_dev();
  871. if (cdev->private->state != DEV_STATE_ONLINE)
  872. goto out_unlock;
  873. rc = 0;
  874. out_unlock:
  875. spin_unlock_irq(cdev->ccwlock);
  876. return 0;
  877. }
  878. struct ccw_device *
  879. ccw_device_probe_console(void)
  880. {
  881. struct subchannel *sch;
  882. int ret;
  883. if (xchg(&console_cdev_in_use, 1) != 0)
  884. return NULL;
  885. sch = cio_probe_console();
  886. if (IS_ERR(sch)) {
  887. console_cdev_in_use = 0;
  888. return (void *) sch;
  889. }
  890. memset(&console_cdev, 0, sizeof(struct ccw_device));
  891. memset(&console_private, 0, sizeof(struct ccw_device_private));
  892. console_cdev.private = &console_private;
  893. ret = ccw_device_console_enable(&console_cdev, sch);
  894. if (ret) {
  895. cio_release_console();
  896. console_cdev_in_use = 0;
  897. return ERR_PTR(ret);
  898. }
  899. console_cdev.online = 1;
  900. return &console_cdev;
  901. }
  902. #endif
  903. /*
  904. * get ccw_device matching the busid, but only if owned by cdrv
  905. */
  906. static int
  907. __ccwdev_check_busid(struct device *dev, void *id)
  908. {
  909. char *bus_id;
  910. bus_id = (char *)id;
  911. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  912. }
  913. struct ccw_device *
  914. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  915. {
  916. struct device *dev;
  917. struct device_driver *drv;
  918. drv = get_driver(&cdrv->driver);
  919. if (!drv)
  920. return NULL;
  921. dev = driver_find_device(drv, NULL, (void *)bus_id,
  922. __ccwdev_check_busid);
  923. put_driver(drv);
  924. return dev ? to_ccwdev(dev) : 0;
  925. }
  926. /************************** device driver handling ************************/
  927. /* This is the implementation of the ccw_driver class. The probe, remove
  928. * and release methods are initially very similar to the device_driver
  929. * implementations, with the difference that they have ccw_device
  930. * arguments.
  931. *
  932. * A ccw driver also contains the information that is needed for
  933. * device matching.
  934. */
  935. static int
  936. ccw_device_probe (struct device *dev)
  937. {
  938. struct ccw_device *cdev = to_ccwdev(dev);
  939. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  940. int ret;
  941. cdev->drv = cdrv; /* to let the driver call _set_online */
  942. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  943. if (ret) {
  944. cdev->drv = 0;
  945. return ret;
  946. }
  947. return 0;
  948. }
  949. static int
  950. ccw_device_remove (struct device *dev)
  951. {
  952. struct ccw_device *cdev = to_ccwdev(dev);
  953. struct ccw_driver *cdrv = cdev->drv;
  954. int ret;
  955. pr_debug("removing device %s\n", cdev->dev.bus_id);
  956. if (cdrv->remove)
  957. cdrv->remove(cdev);
  958. if (cdev->online) {
  959. cdev->online = 0;
  960. spin_lock_irq(cdev->ccwlock);
  961. ret = ccw_device_offline(cdev);
  962. spin_unlock_irq(cdev->ccwlock);
  963. if (ret == 0)
  964. wait_event(cdev->private->wait_q,
  965. dev_fsm_final_state(cdev));
  966. else
  967. //FIXME: we can't fail!
  968. pr_debug("ccw_device_offline returned %d, device %s\n",
  969. ret, cdev->dev.bus_id);
  970. }
  971. ccw_device_set_timeout(cdev, 0);
  972. cdev->drv = 0;
  973. return 0;
  974. }
  975. int
  976. ccw_driver_register (struct ccw_driver *cdriver)
  977. {
  978. struct device_driver *drv = &cdriver->driver;
  979. drv->bus = &ccw_bus_type;
  980. drv->name = cdriver->name;
  981. drv->probe = ccw_device_probe;
  982. drv->remove = ccw_device_remove;
  983. return driver_register(drv);
  984. }
  985. void
  986. ccw_driver_unregister (struct ccw_driver *cdriver)
  987. {
  988. driver_unregister(&cdriver->driver);
  989. }
  990. MODULE_LICENSE("GPL");
  991. EXPORT_SYMBOL(ccw_device_set_online);
  992. EXPORT_SYMBOL(ccw_device_set_offline);
  993. EXPORT_SYMBOL(ccw_driver_register);
  994. EXPORT_SYMBOL(ccw_driver_unregister);
  995. EXPORT_SYMBOL(get_ccwdev_by_busid);
  996. EXPORT_SYMBOL(ccw_bus_type);
  997. EXPORT_SYMBOL(ccw_device_work);
  998. EXPORT_SYMBOL(ccw_device_notify_work);