device.c 28 KB

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