device.c 28 KB

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