device.c 29 KB

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