device.c 28 KB

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