device.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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. static int ccw_device_register(struct ccw_device *cdev)
  469. {
  470. struct device *dev = &cdev->dev;
  471. int ret;
  472. dev->bus = &ccw_bus_type;
  473. if ((ret = device_add(dev)))
  474. return ret;
  475. set_bit(1, &cdev->private->registered);
  476. if ((ret = device_add_files(dev))) {
  477. if (test_and_clear_bit(1, &cdev->private->registered))
  478. device_del(dev);
  479. }
  480. return ret;
  481. }
  482. struct match_data {
  483. struct ccw_dev_id dev_id;
  484. struct ccw_device * sibling;
  485. };
  486. static int
  487. match_devno(struct device * dev, void * data)
  488. {
  489. struct match_data * d = data;
  490. struct ccw_device * cdev;
  491. cdev = to_ccwdev(dev);
  492. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  493. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  494. (cdev != d->sibling)) {
  495. cdev->private->state = DEV_STATE_NOT_OPER;
  496. return 1;
  497. }
  498. return 0;
  499. }
  500. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  501. struct ccw_device *sibling)
  502. {
  503. struct device *dev;
  504. struct match_data data;
  505. data.dev_id = *dev_id;
  506. data.sibling = sibling;
  507. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  508. return dev ? to_ccwdev(dev) : NULL;
  509. }
  510. static void
  511. ccw_device_add_changed(struct work_struct *work)
  512. {
  513. struct ccw_device_private *priv;
  514. struct ccw_device *cdev;
  515. priv = container_of(work, struct ccw_device_private, kick_work);
  516. cdev = priv->cdev;
  517. if (device_add(&cdev->dev)) {
  518. put_device(&cdev->dev);
  519. return;
  520. }
  521. set_bit(1, &cdev->private->registered);
  522. if (device_add_files(&cdev->dev)) {
  523. if (test_and_clear_bit(1, &cdev->private->registered))
  524. device_unregister(&cdev->dev);
  525. }
  526. }
  527. extern int css_get_ssd_info(struct subchannel *sch);
  528. void
  529. ccw_device_do_unreg_rereg(struct work_struct *work)
  530. {
  531. struct ccw_device_private *priv;
  532. struct ccw_device *cdev;
  533. struct subchannel *sch;
  534. int need_rename;
  535. priv = container_of(work, struct ccw_device_private, kick_work);
  536. cdev = priv->cdev;
  537. sch = to_subchannel(cdev->dev.parent);
  538. if (cdev->private->dev_id.devno != sch->schib.pmcw.dev) {
  539. /*
  540. * The device number has changed. This is usually only when
  541. * a device has been detached under VM and then re-appeared
  542. * on another subchannel because of a different attachment
  543. * order than before. Ideally, we should should just switch
  544. * subchannels, but unfortunately, this is not possible with
  545. * the current implementation.
  546. * Instead, we search for the old subchannel for this device
  547. * number and deregister so there are no collisions with the
  548. * newly registered ccw_device.
  549. * FIXME: Find another solution so the block layer doesn't
  550. * get possibly sick...
  551. */
  552. struct ccw_device *other_cdev;
  553. struct ccw_dev_id dev_id;
  554. need_rename = 1;
  555. dev_id.devno = sch->schib.pmcw.dev;
  556. dev_id.ssid = sch->schid.ssid;
  557. other_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  558. if (other_cdev) {
  559. struct subchannel *other_sch;
  560. other_sch = to_subchannel(other_cdev->dev.parent);
  561. if (get_device(&other_sch->dev)) {
  562. stsch(other_sch->schid, &other_sch->schib);
  563. if (other_sch->schib.pmcw.dnv) {
  564. other_sch->schib.pmcw.intparm = 0;
  565. cio_modify(other_sch);
  566. }
  567. css_sch_device_unregister(other_sch);
  568. }
  569. }
  570. /* Update ssd info here. */
  571. css_get_ssd_info(sch);
  572. cdev->private->dev_id.devno = sch->schib.pmcw.dev;
  573. } else
  574. need_rename = 0;
  575. device_remove_files(&cdev->dev);
  576. if (test_and_clear_bit(1, &cdev->private->registered))
  577. device_del(&cdev->dev);
  578. if (need_rename)
  579. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  580. sch->schid.ssid, sch->schib.pmcw.dev);
  581. PREPARE_WORK(&cdev->private->kick_work,
  582. ccw_device_add_changed);
  583. queue_work(ccw_device_work, &cdev->private->kick_work);
  584. }
  585. static void
  586. ccw_device_release(struct device *dev)
  587. {
  588. struct ccw_device *cdev;
  589. cdev = to_ccwdev(dev);
  590. kfree(cdev->private);
  591. kfree(cdev);
  592. }
  593. /*
  594. * Register recognized device.
  595. */
  596. static void
  597. io_subchannel_register(struct work_struct *work)
  598. {
  599. struct ccw_device_private *priv;
  600. struct ccw_device *cdev;
  601. struct subchannel *sch;
  602. int ret;
  603. unsigned long flags;
  604. priv = container_of(work, struct ccw_device_private, kick_work);
  605. cdev = priv->cdev;
  606. sch = to_subchannel(cdev->dev.parent);
  607. /*
  608. * io_subchannel_register() will also be called after device
  609. * recognition has been done for a boxed device (which will already
  610. * be registered). We need to reprobe since we may now have sense id
  611. * information.
  612. */
  613. if (klist_node_attached(&cdev->dev.knode_parent)) {
  614. if (!cdev->drv) {
  615. ret = device_reprobe(&cdev->dev);
  616. if (ret)
  617. /* We can't do much here. */
  618. dev_info(&cdev->dev, "device_reprobe() returned"
  619. " %d\n", ret);
  620. }
  621. goto out;
  622. }
  623. /* make it known to the system */
  624. ret = ccw_device_register(cdev);
  625. if (ret) {
  626. printk (KERN_WARNING "%s: could not register %s\n",
  627. __func__, cdev->dev.bus_id);
  628. put_device(&cdev->dev);
  629. spin_lock_irqsave(&sch->lock, flags);
  630. sch->dev.driver_data = NULL;
  631. spin_unlock_irqrestore(&sch->lock, flags);
  632. kfree (cdev->private);
  633. kfree (cdev);
  634. put_device(&sch->dev);
  635. if (atomic_dec_and_test(&ccw_device_init_count))
  636. wake_up(&ccw_device_init_wq);
  637. return;
  638. }
  639. ret = subchannel_add_files(cdev->dev.parent);
  640. if (ret)
  641. printk(KERN_WARNING "%s: could not add attributes to %s\n",
  642. __func__, sch->dev.bus_id);
  643. put_device(&cdev->dev);
  644. out:
  645. cdev->private->flags.recog_done = 1;
  646. put_device(&sch->dev);
  647. wake_up(&cdev->private->wait_q);
  648. if (atomic_dec_and_test(&ccw_device_init_count))
  649. wake_up(&ccw_device_init_wq);
  650. }
  651. void
  652. ccw_device_call_sch_unregister(struct work_struct *work)
  653. {
  654. struct ccw_device_private *priv;
  655. struct ccw_device *cdev;
  656. struct subchannel *sch;
  657. priv = container_of(work, struct ccw_device_private, kick_work);
  658. cdev = priv->cdev;
  659. sch = to_subchannel(cdev->dev.parent);
  660. css_sch_device_unregister(sch);
  661. /* Reset intparm to zeroes. */
  662. sch->schib.pmcw.intparm = 0;
  663. cio_modify(sch);
  664. put_device(&cdev->dev);
  665. put_device(&sch->dev);
  666. }
  667. /*
  668. * subchannel recognition done. Called from the state machine.
  669. */
  670. void
  671. io_subchannel_recog_done(struct ccw_device *cdev)
  672. {
  673. struct subchannel *sch;
  674. if (css_init_done == 0) {
  675. cdev->private->flags.recog_done = 1;
  676. return;
  677. }
  678. switch (cdev->private->state) {
  679. case DEV_STATE_NOT_OPER:
  680. cdev->private->flags.recog_done = 1;
  681. /* Remove device found not operational. */
  682. if (!get_device(&cdev->dev))
  683. break;
  684. sch = to_subchannel(cdev->dev.parent);
  685. PREPARE_WORK(&cdev->private->kick_work,
  686. ccw_device_call_sch_unregister);
  687. queue_work(slow_path_wq, &cdev->private->kick_work);
  688. if (atomic_dec_and_test(&ccw_device_init_count))
  689. wake_up(&ccw_device_init_wq);
  690. break;
  691. case DEV_STATE_BOXED:
  692. /* Device did not respond in time. */
  693. case DEV_STATE_OFFLINE:
  694. /*
  695. * We can't register the device in interrupt context so
  696. * we schedule a work item.
  697. */
  698. if (!get_device(&cdev->dev))
  699. break;
  700. PREPARE_WORK(&cdev->private->kick_work,
  701. io_subchannel_register);
  702. queue_work(slow_path_wq, &cdev->private->kick_work);
  703. break;
  704. }
  705. }
  706. static int
  707. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  708. {
  709. int rc;
  710. struct ccw_device_private *priv;
  711. sch->dev.driver_data = cdev;
  712. sch->driver = &io_subchannel_driver;
  713. cdev->ccwlock = &sch->lock;
  714. /* Init private data. */
  715. priv = cdev->private;
  716. priv->dev_id.devno = sch->schib.pmcw.dev;
  717. priv->dev_id.ssid = sch->schid.ssid;
  718. priv->schid = sch->schid;
  719. priv->state = DEV_STATE_NOT_OPER;
  720. INIT_LIST_HEAD(&priv->cmb_list);
  721. init_waitqueue_head(&priv->wait_q);
  722. init_timer(&priv->timer);
  723. /* Set an initial name for the device. */
  724. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  725. sch->schid.ssid, sch->schib.pmcw.dev);
  726. /* Increase counter of devices currently in recognition. */
  727. atomic_inc(&ccw_device_init_count);
  728. /* Start async. device sensing. */
  729. spin_lock_irq(&sch->lock);
  730. rc = ccw_device_recognition(cdev);
  731. spin_unlock_irq(&sch->lock);
  732. if (rc) {
  733. if (atomic_dec_and_test(&ccw_device_init_count))
  734. wake_up(&ccw_device_init_wq);
  735. }
  736. return rc;
  737. }
  738. static int
  739. io_subchannel_probe (struct subchannel *sch)
  740. {
  741. struct ccw_device *cdev;
  742. int rc;
  743. unsigned long flags;
  744. if (sch->dev.driver_data) {
  745. /*
  746. * This subchannel already has an associated ccw_device.
  747. * Register it and exit. This happens for all early
  748. * device, e.g. the console.
  749. */
  750. cdev = sch->dev.driver_data;
  751. device_initialize(&cdev->dev);
  752. ccw_device_register(cdev);
  753. subchannel_add_files(&sch->dev);
  754. /*
  755. * Check if the device is already online. If it is
  756. * the reference count needs to be corrected
  757. * (see ccw_device_online and css_init_done for the
  758. * ugly details).
  759. */
  760. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  761. cdev->private->state != DEV_STATE_OFFLINE &&
  762. cdev->private->state != DEV_STATE_BOXED)
  763. get_device(&cdev->dev);
  764. return 0;
  765. }
  766. cdev = kzalloc (sizeof(*cdev), GFP_KERNEL);
  767. if (!cdev)
  768. return -ENOMEM;
  769. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  770. GFP_KERNEL | GFP_DMA);
  771. if (!cdev->private) {
  772. kfree(cdev);
  773. return -ENOMEM;
  774. }
  775. cdev->private->cdev = cdev;
  776. atomic_set(&cdev->private->onoff, 0);
  777. cdev->dev.parent = &sch->dev;
  778. cdev->dev.release = ccw_device_release;
  779. INIT_LIST_HEAD(&cdev->private->kick_work.entry);
  780. /* Do first half of device_register. */
  781. device_initialize(&cdev->dev);
  782. if (!get_device(&sch->dev)) {
  783. if (cdev->dev.release)
  784. cdev->dev.release(&cdev->dev);
  785. return -ENODEV;
  786. }
  787. rc = io_subchannel_recog(cdev, sch);
  788. if (rc) {
  789. spin_lock_irqsave(&sch->lock, flags);
  790. sch->dev.driver_data = NULL;
  791. spin_unlock_irqrestore(&sch->lock, flags);
  792. if (cdev->dev.release)
  793. cdev->dev.release(&cdev->dev);
  794. }
  795. return rc;
  796. }
  797. static void ccw_device_unregister(struct work_struct *work)
  798. {
  799. struct ccw_device_private *priv;
  800. struct ccw_device *cdev;
  801. priv = container_of(work, struct ccw_device_private, kick_work);
  802. cdev = priv->cdev;
  803. if (test_and_clear_bit(1, &cdev->private->registered))
  804. device_unregister(&cdev->dev);
  805. put_device(&cdev->dev);
  806. }
  807. static int
  808. io_subchannel_remove (struct subchannel *sch)
  809. {
  810. struct ccw_device *cdev;
  811. unsigned long flags;
  812. if (!sch->dev.driver_data)
  813. return 0;
  814. cdev = sch->dev.driver_data;
  815. /* Set ccw device to not operational and drop reference. */
  816. spin_lock_irqsave(cdev->ccwlock, flags);
  817. sch->dev.driver_data = NULL;
  818. cdev->private->state = DEV_STATE_NOT_OPER;
  819. spin_unlock_irqrestore(cdev->ccwlock, flags);
  820. /*
  821. * Put unregistration on workqueue to avoid livelocks on the css bus
  822. * semaphore.
  823. */
  824. if (get_device(&cdev->dev)) {
  825. PREPARE_WORK(&cdev->private->kick_work,
  826. ccw_device_unregister);
  827. queue_work(ccw_device_work, &cdev->private->kick_work);
  828. }
  829. return 0;
  830. }
  831. static int
  832. io_subchannel_notify(struct device *dev, int event)
  833. {
  834. struct ccw_device *cdev;
  835. cdev = dev->driver_data;
  836. if (!cdev)
  837. return 0;
  838. if (!cdev->drv)
  839. return 0;
  840. if (!cdev->online)
  841. return 0;
  842. return cdev->drv->notify ? cdev->drv->notify(cdev, event) : 0;
  843. }
  844. static void
  845. io_subchannel_verify(struct device *dev)
  846. {
  847. struct ccw_device *cdev;
  848. cdev = dev->driver_data;
  849. if (cdev)
  850. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  851. }
  852. static void
  853. io_subchannel_ioterm(struct device *dev)
  854. {
  855. struct ccw_device *cdev;
  856. cdev = dev->driver_data;
  857. if (!cdev)
  858. return;
  859. /* Internal I/O will be retried by the interrupt handler. */
  860. if (cdev->private->flags.intretry)
  861. return;
  862. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  863. if (cdev->handler)
  864. cdev->handler(cdev, cdev->private->intparm,
  865. ERR_PTR(-EIO));
  866. }
  867. static void
  868. io_subchannel_shutdown(struct subchannel *sch)
  869. {
  870. struct ccw_device *cdev;
  871. int ret;
  872. cdev = sch->dev.driver_data;
  873. if (cio_is_console(sch->schid))
  874. return;
  875. if (!sch->schib.pmcw.ena)
  876. /* Nothing to do. */
  877. return;
  878. ret = cio_disable_subchannel(sch);
  879. if (ret != -EBUSY)
  880. /* Subchannel is disabled, we're done. */
  881. return;
  882. cdev->private->state = DEV_STATE_QUIESCE;
  883. if (cdev->handler)
  884. cdev->handler(cdev, cdev->private->intparm,
  885. ERR_PTR(-EIO));
  886. ret = ccw_device_cancel_halt_clear(cdev);
  887. if (ret == -EBUSY) {
  888. ccw_device_set_timeout(cdev, HZ/10);
  889. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  890. }
  891. cio_disable_subchannel(sch);
  892. }
  893. #ifdef CONFIG_CCW_CONSOLE
  894. static struct ccw_device console_cdev;
  895. static struct ccw_device_private console_private;
  896. static int console_cdev_in_use;
  897. static int
  898. ccw_device_console_enable (struct ccw_device *cdev, struct subchannel *sch)
  899. {
  900. int rc;
  901. /* Initialize the ccw_device structure. */
  902. cdev->dev.parent= &sch->dev;
  903. rc = io_subchannel_recog(cdev, sch);
  904. if (rc)
  905. return rc;
  906. /* Now wait for the async. recognition to come to an end. */
  907. spin_lock_irq(cdev->ccwlock);
  908. while (!dev_fsm_final_state(cdev))
  909. wait_cons_dev();
  910. rc = -EIO;
  911. if (cdev->private->state != DEV_STATE_OFFLINE)
  912. goto out_unlock;
  913. ccw_device_online(cdev);
  914. while (!dev_fsm_final_state(cdev))
  915. wait_cons_dev();
  916. if (cdev->private->state != DEV_STATE_ONLINE)
  917. goto out_unlock;
  918. rc = 0;
  919. out_unlock:
  920. spin_unlock_irq(cdev->ccwlock);
  921. return 0;
  922. }
  923. struct ccw_device *
  924. ccw_device_probe_console(void)
  925. {
  926. struct subchannel *sch;
  927. int ret;
  928. if (xchg(&console_cdev_in_use, 1) != 0)
  929. return ERR_PTR(-EBUSY);
  930. sch = cio_probe_console();
  931. if (IS_ERR(sch)) {
  932. console_cdev_in_use = 0;
  933. return (void *) sch;
  934. }
  935. memset(&console_cdev, 0, sizeof(struct ccw_device));
  936. memset(&console_private, 0, sizeof(struct ccw_device_private));
  937. console_cdev.private = &console_private;
  938. console_private.cdev = &console_cdev;
  939. ret = ccw_device_console_enable(&console_cdev, sch);
  940. if (ret) {
  941. cio_release_console();
  942. console_cdev_in_use = 0;
  943. return ERR_PTR(ret);
  944. }
  945. console_cdev.online = 1;
  946. return &console_cdev;
  947. }
  948. #endif
  949. /*
  950. * get ccw_device matching the busid, but only if owned by cdrv
  951. */
  952. static int
  953. __ccwdev_check_busid(struct device *dev, void *id)
  954. {
  955. char *bus_id;
  956. bus_id = id;
  957. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  958. }
  959. struct ccw_device *
  960. get_ccwdev_by_busid(struct ccw_driver *cdrv, const char *bus_id)
  961. {
  962. struct device *dev;
  963. struct device_driver *drv;
  964. drv = get_driver(&cdrv->driver);
  965. if (!drv)
  966. return NULL;
  967. dev = driver_find_device(drv, NULL, (void *)bus_id,
  968. __ccwdev_check_busid);
  969. put_driver(drv);
  970. return dev ? to_ccwdev(dev) : NULL;
  971. }
  972. /************************** device driver handling ************************/
  973. /* This is the implementation of the ccw_driver class. The probe, remove
  974. * and release methods are initially very similar to the device_driver
  975. * implementations, with the difference that they have ccw_device
  976. * arguments.
  977. *
  978. * A ccw driver also contains the information that is needed for
  979. * device matching.
  980. */
  981. static int
  982. ccw_device_probe (struct device *dev)
  983. {
  984. struct ccw_device *cdev = to_ccwdev(dev);
  985. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  986. int ret;
  987. cdev->drv = cdrv; /* to let the driver call _set_online */
  988. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  989. if (ret) {
  990. cdev->drv = NULL;
  991. return ret;
  992. }
  993. return 0;
  994. }
  995. static int
  996. ccw_device_remove (struct device *dev)
  997. {
  998. struct ccw_device *cdev = to_ccwdev(dev);
  999. struct ccw_driver *cdrv = cdev->drv;
  1000. int ret;
  1001. pr_debug("removing device %s\n", cdev->dev.bus_id);
  1002. if (cdrv->remove)
  1003. cdrv->remove(cdev);
  1004. if (cdev->online) {
  1005. cdev->online = 0;
  1006. spin_lock_irq(cdev->ccwlock);
  1007. ret = ccw_device_offline(cdev);
  1008. spin_unlock_irq(cdev->ccwlock);
  1009. if (ret == 0)
  1010. wait_event(cdev->private->wait_q,
  1011. dev_fsm_final_state(cdev));
  1012. else
  1013. //FIXME: we can't fail!
  1014. pr_debug("ccw_device_offline returned %d, device %s\n",
  1015. ret, cdev->dev.bus_id);
  1016. }
  1017. ccw_device_set_timeout(cdev, 0);
  1018. cdev->drv = NULL;
  1019. return 0;
  1020. }
  1021. struct bus_type ccw_bus_type = {
  1022. .name = "ccw",
  1023. .match = ccw_bus_match,
  1024. .uevent = ccw_uevent,
  1025. .probe = ccw_device_probe,
  1026. .remove = ccw_device_remove,
  1027. };
  1028. int
  1029. ccw_driver_register (struct ccw_driver *cdriver)
  1030. {
  1031. struct device_driver *drv = &cdriver->driver;
  1032. drv->bus = &ccw_bus_type;
  1033. drv->name = cdriver->name;
  1034. return driver_register(drv);
  1035. }
  1036. void
  1037. ccw_driver_unregister (struct ccw_driver *cdriver)
  1038. {
  1039. driver_unregister(&cdriver->driver);
  1040. }
  1041. /* Helper func for qdio. */
  1042. struct subchannel_id
  1043. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1044. {
  1045. struct subchannel *sch;
  1046. sch = to_subchannel(cdev->dev.parent);
  1047. return sch->schid;
  1048. }
  1049. MODULE_LICENSE("GPL");
  1050. EXPORT_SYMBOL(ccw_device_set_online);
  1051. EXPORT_SYMBOL(ccw_device_set_offline);
  1052. EXPORT_SYMBOL(ccw_driver_register);
  1053. EXPORT_SYMBOL(ccw_driver_unregister);
  1054. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1055. EXPORT_SYMBOL(ccw_bus_type);
  1056. EXPORT_SYMBOL(ccw_device_work);
  1057. EXPORT_SYMBOL(ccw_device_notify_work);
  1058. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);