device.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832
  1. /*
  2. * drivers/s390/cio/device.c
  3. * bus driver for ccw devices
  4. *
  5. * Copyright IBM Corp. 2002,2008
  6. * Author(s): Arnd Bergmann (arndb@de.ibm.com)
  7. * Cornelia Huck (cornelia.huck@de.ibm.com)
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com)
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/errno.h>
  14. #include <linux/err.h>
  15. #include <linux/slab.h>
  16. #include <linux/list.h>
  17. #include <linux/device.h>
  18. #include <linux/workqueue.h>
  19. #include <linux/timer.h>
  20. #include <asm/ccwdev.h>
  21. #include <asm/cio.h>
  22. #include <asm/param.h> /* HZ */
  23. #include <asm/cmb.h>
  24. #include <asm/isc.h>
  25. #include "chp.h"
  26. #include "cio.h"
  27. #include "cio_debug.h"
  28. #include "css.h"
  29. #include "device.h"
  30. #include "ioasm.h"
  31. #include "io_sch.h"
  32. static struct timer_list recovery_timer;
  33. static DEFINE_SPINLOCK(recovery_lock);
  34. static int recovery_phase;
  35. static const unsigned long recovery_delay[] = { 3, 30, 300 };
  36. /******************* bus type handling ***********************/
  37. /* The Linux driver model distinguishes between a bus type and
  38. * the bus itself. Of course we only have one channel
  39. * subsystem driver and one channel system per machine, but
  40. * we still use the abstraction. T.R. says it's a good idea. */
  41. static int
  42. ccw_bus_match (struct device * dev, struct device_driver * drv)
  43. {
  44. struct ccw_device *cdev = to_ccwdev(dev);
  45. struct ccw_driver *cdrv = to_ccwdrv(drv);
  46. const struct ccw_device_id *ids = cdrv->ids, *found;
  47. if (!ids)
  48. return 0;
  49. found = ccw_device_id_match(ids, &cdev->id);
  50. if (!found)
  51. return 0;
  52. cdev->id.driver_info = found->driver_info;
  53. return 1;
  54. }
  55. /* Store modalias string delimited by prefix/suffix string into buffer with
  56. * specified size. Return length of resulting string (excluding trailing '\0')
  57. * even if string doesn't fit buffer (snprintf semantics). */
  58. static int snprint_alias(char *buf, size_t size,
  59. struct ccw_device_id *id, const char *suffix)
  60. {
  61. int len;
  62. len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
  63. if (len > size)
  64. return len;
  65. buf += len;
  66. size -= len;
  67. if (id->dev_type != 0)
  68. len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
  69. id->dev_model, suffix);
  70. else
  71. len += snprintf(buf, size, "dtdm%s", suffix);
  72. return len;
  73. }
  74. /* Set up environment variables for ccw device uevent. Return 0 on success,
  75. * non-zero otherwise. */
  76. static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
  77. {
  78. struct ccw_device *cdev = to_ccwdev(dev);
  79. struct ccw_device_id *id = &(cdev->id);
  80. int ret;
  81. char modalias_buf[30];
  82. /* CU_TYPE= */
  83. ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
  84. if (ret)
  85. return ret;
  86. /* CU_MODEL= */
  87. ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
  88. if (ret)
  89. return ret;
  90. /* The next two can be zero, that's ok for us */
  91. /* DEV_TYPE= */
  92. ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
  93. if (ret)
  94. return ret;
  95. /* DEV_MODEL= */
  96. ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
  97. if (ret)
  98. return ret;
  99. /* MODALIAS= */
  100. snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
  101. ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
  102. return ret;
  103. }
  104. struct bus_type ccw_bus_type;
  105. static void io_subchannel_irq(struct subchannel *);
  106. static int io_subchannel_probe(struct subchannel *);
  107. static int io_subchannel_remove(struct subchannel *);
  108. static void io_subchannel_shutdown(struct subchannel *);
  109. static int io_subchannel_sch_event(struct subchannel *, int);
  110. static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
  111. int);
  112. static struct css_device_id io_subchannel_ids[] = {
  113. { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
  114. { /* end of list */ },
  115. };
  116. MODULE_DEVICE_TABLE(css, io_subchannel_ids);
  117. static struct css_driver io_subchannel_driver = {
  118. .owner = THIS_MODULE,
  119. .subchannel_type = io_subchannel_ids,
  120. .name = "io_subchannel",
  121. .irq = io_subchannel_irq,
  122. .sch_event = io_subchannel_sch_event,
  123. .chp_event = io_subchannel_chp_event,
  124. .probe = io_subchannel_probe,
  125. .remove = io_subchannel_remove,
  126. .shutdown = io_subchannel_shutdown,
  127. };
  128. struct workqueue_struct *ccw_device_work;
  129. struct workqueue_struct *ccw_device_notify_work;
  130. wait_queue_head_t ccw_device_init_wq;
  131. atomic_t ccw_device_init_count;
  132. static void recovery_func(unsigned long data);
  133. static int __init
  134. init_ccw_bus_type (void)
  135. {
  136. int ret;
  137. init_waitqueue_head(&ccw_device_init_wq);
  138. atomic_set(&ccw_device_init_count, 0);
  139. setup_timer(&recovery_timer, recovery_func, 0);
  140. ccw_device_work = create_singlethread_workqueue("cio");
  141. if (!ccw_device_work)
  142. return -ENOMEM; /* FIXME: better errno ? */
  143. ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
  144. if (!ccw_device_notify_work) {
  145. ret = -ENOMEM; /* FIXME: better errno ? */
  146. goto out_err;
  147. }
  148. slow_path_wq = create_singlethread_workqueue("kslowcrw");
  149. if (!slow_path_wq) {
  150. ret = -ENOMEM; /* FIXME: better errno ? */
  151. goto out_err;
  152. }
  153. if ((ret = bus_register (&ccw_bus_type)))
  154. goto out_err;
  155. ret = css_driver_register(&io_subchannel_driver);
  156. if (ret)
  157. goto out_err;
  158. wait_event(ccw_device_init_wq,
  159. atomic_read(&ccw_device_init_count) == 0);
  160. flush_workqueue(ccw_device_work);
  161. return 0;
  162. out_err:
  163. if (ccw_device_work)
  164. destroy_workqueue(ccw_device_work);
  165. if (ccw_device_notify_work)
  166. destroy_workqueue(ccw_device_notify_work);
  167. if (slow_path_wq)
  168. destroy_workqueue(slow_path_wq);
  169. return ret;
  170. }
  171. static void __exit
  172. cleanup_ccw_bus_type (void)
  173. {
  174. css_driver_unregister(&io_subchannel_driver);
  175. bus_unregister(&ccw_bus_type);
  176. destroy_workqueue(ccw_device_notify_work);
  177. destroy_workqueue(ccw_device_work);
  178. }
  179. subsys_initcall(init_ccw_bus_type);
  180. module_exit(cleanup_ccw_bus_type);
  181. /************************ device handling **************************/
  182. /*
  183. * A ccw_device has some interfaces in sysfs in addition to the
  184. * standard ones.
  185. * The following entries are designed to export the information which
  186. * resided in 2.4 in /proc/subchannels. Subchannel and device number
  187. * are obvious, so they don't have an entry :)
  188. * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
  189. */
  190. static ssize_t
  191. chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
  192. {
  193. struct subchannel *sch = to_subchannel(dev);
  194. struct chsc_ssd_info *ssd = &sch->ssd_info;
  195. ssize_t ret = 0;
  196. int chp;
  197. int mask;
  198. for (chp = 0; chp < 8; chp++) {
  199. mask = 0x80 >> chp;
  200. if (ssd->path_mask & mask)
  201. ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
  202. else
  203. ret += sprintf(buf + ret, "00 ");
  204. }
  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");
  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. int ccw_device_is_orphan(struct ccw_device *cdev)
  251. {
  252. return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
  253. }
  254. static void ccw_device_unregister(struct ccw_device *cdev)
  255. {
  256. if (test_and_clear_bit(1, &cdev->private->registered))
  257. device_del(&cdev->dev);
  258. }
  259. static void ccw_device_remove_orphan_cb(struct device *dev)
  260. {
  261. struct ccw_device *cdev = to_ccwdev(dev);
  262. ccw_device_unregister(cdev);
  263. put_device(&cdev->dev);
  264. }
  265. static void ccw_device_remove_sch_cb(struct device *dev)
  266. {
  267. struct subchannel *sch;
  268. sch = to_subchannel(dev);
  269. css_sch_device_unregister(sch);
  270. /* Reset intparm to zeroes. */
  271. sch->schib.pmcw.intparm = 0;
  272. cio_modify(sch);
  273. put_device(&sch->dev);
  274. }
  275. static void
  276. ccw_device_remove_disconnected(struct ccw_device *cdev)
  277. {
  278. unsigned long flags;
  279. int rc;
  280. /*
  281. * Forced offline in disconnected state means
  282. * 'throw away device'.
  283. */
  284. if (ccw_device_is_orphan(cdev)) {
  285. /*
  286. * Deregister ccw device.
  287. * Unfortunately, we cannot do this directly from the
  288. * attribute method.
  289. */
  290. spin_lock_irqsave(cdev->ccwlock, flags);
  291. cdev->private->state = DEV_STATE_NOT_OPER;
  292. spin_unlock_irqrestore(cdev->ccwlock, flags);
  293. rc = device_schedule_callback(&cdev->dev,
  294. ccw_device_remove_orphan_cb);
  295. if (rc)
  296. CIO_MSG_EVENT(0, "Couldn't unregister orphan "
  297. "0.%x.%04x\n",
  298. cdev->private->dev_id.ssid,
  299. cdev->private->dev_id.devno);
  300. return;
  301. }
  302. /* Deregister subchannel, which will kill the ccw device. */
  303. rc = device_schedule_callback(cdev->dev.parent,
  304. ccw_device_remove_sch_cb);
  305. if (rc)
  306. CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
  307. "0.%x.%04x\n",
  308. cdev->private->dev_id.ssid,
  309. cdev->private->dev_id.devno);
  310. }
  311. /**
  312. * ccw_device_set_offline() - disable a ccw device for I/O
  313. * @cdev: target ccw device
  314. *
  315. * This function calls the driver's set_offline() function for @cdev, if
  316. * given, and then disables @cdev.
  317. * Returns:
  318. * %0 on success and a negative error value on failure.
  319. * Context:
  320. * enabled, ccw device lock not held
  321. */
  322. int ccw_device_set_offline(struct ccw_device *cdev)
  323. {
  324. int ret;
  325. if (!cdev)
  326. return -ENODEV;
  327. if (!cdev->online || !cdev->drv)
  328. return -EINVAL;
  329. if (cdev->drv->set_offline) {
  330. ret = cdev->drv->set_offline(cdev);
  331. if (ret != 0)
  332. return ret;
  333. }
  334. cdev->online = 0;
  335. spin_lock_irq(cdev->ccwlock);
  336. ret = ccw_device_offline(cdev);
  337. if (ret == -ENODEV) {
  338. if (cdev->private->state != DEV_STATE_NOT_OPER) {
  339. cdev->private->state = DEV_STATE_OFFLINE;
  340. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  341. }
  342. spin_unlock_irq(cdev->ccwlock);
  343. return ret;
  344. }
  345. spin_unlock_irq(cdev->ccwlock);
  346. if (ret == 0)
  347. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  348. else {
  349. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  350. "device 0.%x.%04x\n",
  351. ret, cdev->private->dev_id.ssid,
  352. cdev->private->dev_id.devno);
  353. cdev->online = 1;
  354. }
  355. return ret;
  356. }
  357. /**
  358. * ccw_device_set_online() - enable a ccw device for I/O
  359. * @cdev: target ccw device
  360. *
  361. * This function first enables @cdev and then calls the driver's set_online()
  362. * function for @cdev, if given. If set_online() returns an error, @cdev is
  363. * disabled again.
  364. * Returns:
  365. * %0 on success and a negative error value on failure.
  366. * Context:
  367. * enabled, ccw device lock not held
  368. */
  369. int ccw_device_set_online(struct ccw_device *cdev)
  370. {
  371. int ret;
  372. if (!cdev)
  373. return -ENODEV;
  374. if (cdev->online || !cdev->drv)
  375. return -EINVAL;
  376. spin_lock_irq(cdev->ccwlock);
  377. ret = ccw_device_online(cdev);
  378. spin_unlock_irq(cdev->ccwlock);
  379. if (ret == 0)
  380. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  381. else {
  382. CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
  383. "device 0.%x.%04x\n",
  384. ret, cdev->private->dev_id.ssid,
  385. cdev->private->dev_id.devno);
  386. return ret;
  387. }
  388. if (cdev->private->state != DEV_STATE_ONLINE)
  389. return -ENODEV;
  390. if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
  391. cdev->online = 1;
  392. return 0;
  393. }
  394. spin_lock_irq(cdev->ccwlock);
  395. ret = ccw_device_offline(cdev);
  396. spin_unlock_irq(cdev->ccwlock);
  397. if (ret == 0)
  398. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  399. else
  400. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  401. "device 0.%x.%04x\n",
  402. ret, cdev->private->dev_id.ssid,
  403. cdev->private->dev_id.devno);
  404. return (ret == 0) ? -ENODEV : ret;
  405. }
  406. static void online_store_handle_offline(struct ccw_device *cdev)
  407. {
  408. if (cdev->private->state == DEV_STATE_DISCONNECTED)
  409. ccw_device_remove_disconnected(cdev);
  410. else if (cdev->drv && cdev->drv->set_offline)
  411. ccw_device_set_offline(cdev);
  412. }
  413. static int online_store_recog_and_online(struct ccw_device *cdev)
  414. {
  415. int ret;
  416. /* Do device recognition, if needed. */
  417. if (cdev->id.cu_type == 0) {
  418. ret = ccw_device_recognition(cdev);
  419. if (ret) {
  420. CIO_MSG_EVENT(0, "Couldn't start recognition "
  421. "for device 0.%x.%04x (ret=%d)\n",
  422. cdev->private->dev_id.ssid,
  423. cdev->private->dev_id.devno, ret);
  424. return ret;
  425. }
  426. wait_event(cdev->private->wait_q,
  427. cdev->private->flags.recog_done);
  428. }
  429. if (cdev->drv && cdev->drv->set_online)
  430. ccw_device_set_online(cdev);
  431. return 0;
  432. }
  433. static int online_store_handle_online(struct ccw_device *cdev, int force)
  434. {
  435. int ret;
  436. ret = online_store_recog_and_online(cdev);
  437. if (ret)
  438. return ret;
  439. if (force && cdev->private->state == DEV_STATE_BOXED) {
  440. ret = ccw_device_stlck(cdev);
  441. if (ret)
  442. return ret;
  443. if (cdev->id.cu_type == 0)
  444. cdev->private->state = DEV_STATE_NOT_OPER;
  445. online_store_recog_and_online(cdev);
  446. }
  447. return 0;
  448. }
  449. static ssize_t online_store (struct device *dev, struct device_attribute *attr,
  450. const char *buf, size_t count)
  451. {
  452. struct ccw_device *cdev = to_ccwdev(dev);
  453. int force, ret;
  454. unsigned long i;
  455. if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
  456. return -EAGAIN;
  457. if (cdev->drv && !try_module_get(cdev->drv->owner)) {
  458. atomic_set(&cdev->private->onoff, 0);
  459. return -EINVAL;
  460. }
  461. if (!strncmp(buf, "force\n", count)) {
  462. force = 1;
  463. i = 1;
  464. ret = 0;
  465. } else {
  466. force = 0;
  467. ret = strict_strtoul(buf, 16, &i);
  468. }
  469. if (ret)
  470. goto out;
  471. switch (i) {
  472. case 0:
  473. online_store_handle_offline(cdev);
  474. ret = count;
  475. break;
  476. case 1:
  477. ret = online_store_handle_online(cdev, force);
  478. if (!ret)
  479. ret = count;
  480. break;
  481. default:
  482. ret = -EINVAL;
  483. }
  484. out:
  485. if (cdev->drv)
  486. module_put(cdev->drv->owner);
  487. atomic_set(&cdev->private->onoff, 0);
  488. return ret;
  489. }
  490. static ssize_t
  491. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  492. {
  493. struct ccw_device *cdev = to_ccwdev(dev);
  494. struct subchannel *sch;
  495. if (ccw_device_is_orphan(cdev))
  496. return sprintf(buf, "no device\n");
  497. switch (cdev->private->state) {
  498. case DEV_STATE_BOXED:
  499. return sprintf(buf, "boxed\n");
  500. case DEV_STATE_DISCONNECTED:
  501. case DEV_STATE_DISCONNECTED_SENSE_ID:
  502. case DEV_STATE_NOT_OPER:
  503. sch = to_subchannel(dev->parent);
  504. if (!sch->lpm)
  505. return sprintf(buf, "no path\n");
  506. else
  507. return sprintf(buf, "no device\n");
  508. default:
  509. /* All other states considered fine. */
  510. return sprintf(buf, "good\n");
  511. }
  512. }
  513. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  514. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  515. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  516. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  517. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  518. static DEVICE_ATTR(online, 0644, online_show, online_store);
  519. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  520. static struct attribute *io_subchannel_attrs[] = {
  521. &dev_attr_chpids.attr,
  522. &dev_attr_pimpampom.attr,
  523. NULL,
  524. };
  525. static struct attribute_group io_subchannel_attr_group = {
  526. .attrs = io_subchannel_attrs,
  527. };
  528. static struct attribute * ccwdev_attrs[] = {
  529. &dev_attr_devtype.attr,
  530. &dev_attr_cutype.attr,
  531. &dev_attr_modalias.attr,
  532. &dev_attr_online.attr,
  533. &dev_attr_cmb_enable.attr,
  534. &dev_attr_availability.attr,
  535. NULL,
  536. };
  537. static struct attribute_group ccwdev_attr_group = {
  538. .attrs = ccwdev_attrs,
  539. };
  540. static struct attribute_group *ccwdev_attr_groups[] = {
  541. &ccwdev_attr_group,
  542. NULL,
  543. };
  544. /* this is a simple abstraction for device_register that sets the
  545. * correct bus type and adds the bus specific files */
  546. static int ccw_device_register(struct ccw_device *cdev)
  547. {
  548. struct device *dev = &cdev->dev;
  549. int ret;
  550. dev->bus = &ccw_bus_type;
  551. if ((ret = device_add(dev)))
  552. return ret;
  553. set_bit(1, &cdev->private->registered);
  554. return ret;
  555. }
  556. struct match_data {
  557. struct ccw_dev_id dev_id;
  558. struct ccw_device * sibling;
  559. };
  560. static int
  561. match_devno(struct device * dev, void * data)
  562. {
  563. struct match_data * d = data;
  564. struct ccw_device * cdev;
  565. cdev = to_ccwdev(dev);
  566. if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
  567. !ccw_device_is_orphan(cdev) &&
  568. ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
  569. (cdev != d->sibling))
  570. return 1;
  571. return 0;
  572. }
  573. static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
  574. struct ccw_device *sibling)
  575. {
  576. struct device *dev;
  577. struct match_data data;
  578. data.dev_id = *dev_id;
  579. data.sibling = sibling;
  580. dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
  581. return dev ? to_ccwdev(dev) : NULL;
  582. }
  583. static int match_orphan(struct device *dev, void *data)
  584. {
  585. struct ccw_dev_id *dev_id;
  586. struct ccw_device *cdev;
  587. dev_id = data;
  588. cdev = to_ccwdev(dev);
  589. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  590. }
  591. static struct ccw_device *
  592. get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
  593. struct ccw_dev_id *dev_id)
  594. {
  595. struct device *dev;
  596. dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
  597. match_orphan);
  598. return dev ? to_ccwdev(dev) : NULL;
  599. }
  600. static void
  601. ccw_device_add_changed(struct work_struct *work)
  602. {
  603. struct ccw_device_private *priv;
  604. struct ccw_device *cdev;
  605. priv = container_of(work, struct ccw_device_private, kick_work);
  606. cdev = priv->cdev;
  607. if (device_add(&cdev->dev)) {
  608. put_device(&cdev->dev);
  609. return;
  610. }
  611. set_bit(1, &cdev->private->registered);
  612. }
  613. void ccw_device_do_unreg_rereg(struct work_struct *work)
  614. {
  615. struct ccw_device_private *priv;
  616. struct ccw_device *cdev;
  617. struct subchannel *sch;
  618. priv = container_of(work, struct ccw_device_private, kick_work);
  619. cdev = priv->cdev;
  620. sch = to_subchannel(cdev->dev.parent);
  621. ccw_device_unregister(cdev);
  622. PREPARE_WORK(&cdev->private->kick_work,
  623. ccw_device_add_changed);
  624. queue_work(ccw_device_work, &cdev->private->kick_work);
  625. }
  626. static void
  627. ccw_device_release(struct device *dev)
  628. {
  629. struct ccw_device *cdev;
  630. cdev = to_ccwdev(dev);
  631. kfree(cdev->private);
  632. kfree(cdev);
  633. }
  634. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  635. {
  636. struct ccw_device *cdev;
  637. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  638. if (cdev) {
  639. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  640. GFP_KERNEL | GFP_DMA);
  641. if (cdev->private)
  642. return cdev;
  643. }
  644. kfree(cdev);
  645. return ERR_PTR(-ENOMEM);
  646. }
  647. static int io_subchannel_initialize_dev(struct subchannel *sch,
  648. struct ccw_device *cdev)
  649. {
  650. cdev->private->cdev = cdev;
  651. atomic_set(&cdev->private->onoff, 0);
  652. cdev->dev.parent = &sch->dev;
  653. cdev->dev.release = ccw_device_release;
  654. INIT_WORK(&cdev->private->kick_work, NULL);
  655. cdev->dev.groups = ccwdev_attr_groups;
  656. /* Do first half of device_register. */
  657. device_initialize(&cdev->dev);
  658. if (!get_device(&sch->dev)) {
  659. if (cdev->dev.release)
  660. cdev->dev.release(&cdev->dev);
  661. return -ENODEV;
  662. }
  663. return 0;
  664. }
  665. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  666. {
  667. struct ccw_device *cdev;
  668. int ret;
  669. cdev = io_subchannel_allocate_dev(sch);
  670. if (!IS_ERR(cdev)) {
  671. ret = io_subchannel_initialize_dev(sch, cdev);
  672. if (ret) {
  673. kfree(cdev);
  674. cdev = ERR_PTR(ret);
  675. }
  676. }
  677. return cdev;
  678. }
  679. static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
  680. static void sch_attach_device(struct subchannel *sch,
  681. struct ccw_device *cdev)
  682. {
  683. css_update_ssd_info(sch);
  684. spin_lock_irq(sch->lock);
  685. sch_set_cdev(sch, cdev);
  686. cdev->private->schid = sch->schid;
  687. cdev->ccwlock = sch->lock;
  688. ccw_device_trigger_reprobe(cdev);
  689. spin_unlock_irq(sch->lock);
  690. }
  691. static void sch_attach_disconnected_device(struct subchannel *sch,
  692. struct ccw_device *cdev)
  693. {
  694. struct subchannel *other_sch;
  695. int ret;
  696. other_sch = to_subchannel(get_device(cdev->dev.parent));
  697. ret = device_move(&cdev->dev, &sch->dev);
  698. if (ret) {
  699. CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
  700. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  701. cdev->private->dev_id.devno, ret);
  702. put_device(&other_sch->dev);
  703. return;
  704. }
  705. sch_set_cdev(other_sch, NULL);
  706. /* No need to keep a subchannel without ccw device around. */
  707. css_sch_device_unregister(other_sch);
  708. put_device(&other_sch->dev);
  709. sch_attach_device(sch, cdev);
  710. }
  711. static void sch_attach_orphaned_device(struct subchannel *sch,
  712. struct ccw_device *cdev)
  713. {
  714. int ret;
  715. /* Try to move the ccw device to its new subchannel. */
  716. ret = device_move(&cdev->dev, &sch->dev);
  717. if (ret) {
  718. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
  719. "failed (ret=%d)!\n",
  720. cdev->private->dev_id.ssid,
  721. cdev->private->dev_id.devno, ret);
  722. return;
  723. }
  724. sch_attach_device(sch, cdev);
  725. }
  726. static void sch_create_and_recog_new_device(struct subchannel *sch)
  727. {
  728. struct ccw_device *cdev;
  729. /* Need to allocate a new ccw device. */
  730. cdev = io_subchannel_create_ccwdev(sch);
  731. if (IS_ERR(cdev)) {
  732. /* OK, we did everything we could... */
  733. css_sch_device_unregister(sch);
  734. return;
  735. }
  736. spin_lock_irq(sch->lock);
  737. sch_set_cdev(sch, cdev);
  738. spin_unlock_irq(sch->lock);
  739. /* Start recognition for the new ccw device. */
  740. if (io_subchannel_recog(cdev, sch)) {
  741. spin_lock_irq(sch->lock);
  742. sch_set_cdev(sch, NULL);
  743. spin_unlock_irq(sch->lock);
  744. if (cdev->dev.release)
  745. cdev->dev.release(&cdev->dev);
  746. css_sch_device_unregister(sch);
  747. }
  748. }
  749. void ccw_device_move_to_orphanage(struct work_struct *work)
  750. {
  751. struct ccw_device_private *priv;
  752. struct ccw_device *cdev;
  753. struct ccw_device *replacing_cdev;
  754. struct subchannel *sch;
  755. int ret;
  756. struct channel_subsystem *css;
  757. struct ccw_dev_id dev_id;
  758. priv = container_of(work, struct ccw_device_private, kick_work);
  759. cdev = priv->cdev;
  760. sch = to_subchannel(cdev->dev.parent);
  761. css = to_css(sch->dev.parent);
  762. dev_id.devno = sch->schib.pmcw.dev;
  763. dev_id.ssid = sch->schid.ssid;
  764. /*
  765. * Move the orphaned ccw device to the orphanage so the replacing
  766. * ccw device can take its place on the subchannel.
  767. */
  768. ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
  769. if (ret) {
  770. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
  771. "(ret=%d)!\n", cdev->private->dev_id.ssid,
  772. cdev->private->dev_id.devno, ret);
  773. return;
  774. }
  775. cdev->ccwlock = css->pseudo_subchannel->lock;
  776. /*
  777. * Search for the replacing ccw device
  778. * - among the disconnected devices
  779. * - in the orphanage
  780. */
  781. replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
  782. if (replacing_cdev) {
  783. sch_attach_disconnected_device(sch, replacing_cdev);
  784. return;
  785. }
  786. replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
  787. if (replacing_cdev) {
  788. sch_attach_orphaned_device(sch, replacing_cdev);
  789. return;
  790. }
  791. sch_create_and_recog_new_device(sch);
  792. }
  793. /*
  794. * Register recognized device.
  795. */
  796. static void
  797. io_subchannel_register(struct work_struct *work)
  798. {
  799. struct ccw_device_private *priv;
  800. struct ccw_device *cdev;
  801. struct subchannel *sch;
  802. int ret;
  803. unsigned long flags;
  804. priv = container_of(work, struct ccw_device_private, kick_work);
  805. cdev = priv->cdev;
  806. sch = to_subchannel(cdev->dev.parent);
  807. css_update_ssd_info(sch);
  808. /*
  809. * io_subchannel_register() will also be called after device
  810. * recognition has been done for a boxed device (which will already
  811. * be registered). We need to reprobe since we may now have sense id
  812. * information.
  813. */
  814. if (klist_node_attached(&cdev->dev.knode_parent)) {
  815. if (!cdev->drv) {
  816. ret = device_reprobe(&cdev->dev);
  817. if (ret)
  818. /* We can't do much here. */
  819. CIO_MSG_EVENT(0, "device_reprobe() returned"
  820. " %d for 0.%x.%04x\n", ret,
  821. cdev->private->dev_id.ssid,
  822. cdev->private->dev_id.devno);
  823. }
  824. goto out;
  825. }
  826. /*
  827. * Now we know this subchannel will stay, we can throw
  828. * our delayed uevent.
  829. */
  830. sch->dev.uevent_suppress = 0;
  831. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  832. /* make it known to the system */
  833. ret = ccw_device_register(cdev);
  834. if (ret) {
  835. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  836. cdev->private->dev_id.ssid,
  837. cdev->private->dev_id.devno, ret);
  838. put_device(&cdev->dev);
  839. spin_lock_irqsave(sch->lock, flags);
  840. sch_set_cdev(sch, NULL);
  841. spin_unlock_irqrestore(sch->lock, flags);
  842. kfree (cdev->private);
  843. kfree (cdev);
  844. put_device(&sch->dev);
  845. if (atomic_dec_and_test(&ccw_device_init_count))
  846. wake_up(&ccw_device_init_wq);
  847. return;
  848. }
  849. put_device(&cdev->dev);
  850. out:
  851. cdev->private->flags.recog_done = 1;
  852. put_device(&sch->dev);
  853. wake_up(&cdev->private->wait_q);
  854. if (atomic_dec_and_test(&ccw_device_init_count))
  855. wake_up(&ccw_device_init_wq);
  856. }
  857. static void ccw_device_call_sch_unregister(struct work_struct *work)
  858. {
  859. struct ccw_device_private *priv;
  860. struct ccw_device *cdev;
  861. struct subchannel *sch;
  862. priv = container_of(work, struct ccw_device_private, kick_work);
  863. cdev = priv->cdev;
  864. sch = to_subchannel(cdev->dev.parent);
  865. css_sch_device_unregister(sch);
  866. /* Reset intparm to zeroes. */
  867. sch->schib.pmcw.intparm = 0;
  868. cio_modify(sch);
  869. put_device(&cdev->dev);
  870. put_device(&sch->dev);
  871. }
  872. /*
  873. * subchannel recognition done. Called from the state machine.
  874. */
  875. void
  876. io_subchannel_recog_done(struct ccw_device *cdev)
  877. {
  878. struct subchannel *sch;
  879. if (css_init_done == 0) {
  880. cdev->private->flags.recog_done = 1;
  881. return;
  882. }
  883. switch (cdev->private->state) {
  884. case DEV_STATE_NOT_OPER:
  885. cdev->private->flags.recog_done = 1;
  886. /* Remove device found not operational. */
  887. if (!get_device(&cdev->dev))
  888. break;
  889. sch = to_subchannel(cdev->dev.parent);
  890. PREPARE_WORK(&cdev->private->kick_work,
  891. ccw_device_call_sch_unregister);
  892. queue_work(slow_path_wq, &cdev->private->kick_work);
  893. if (atomic_dec_and_test(&ccw_device_init_count))
  894. wake_up(&ccw_device_init_wq);
  895. break;
  896. case DEV_STATE_BOXED:
  897. /* Device did not respond in time. */
  898. case DEV_STATE_OFFLINE:
  899. /*
  900. * We can't register the device in interrupt context so
  901. * we schedule a work item.
  902. */
  903. if (!get_device(&cdev->dev))
  904. break;
  905. PREPARE_WORK(&cdev->private->kick_work,
  906. io_subchannel_register);
  907. queue_work(slow_path_wq, &cdev->private->kick_work);
  908. break;
  909. }
  910. }
  911. static int
  912. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  913. {
  914. int rc;
  915. struct ccw_device_private *priv;
  916. sch_set_cdev(sch, cdev);
  917. cdev->ccwlock = sch->lock;
  918. /* Init private data. */
  919. priv = cdev->private;
  920. priv->dev_id.devno = sch->schib.pmcw.dev;
  921. priv->dev_id.ssid = sch->schid.ssid;
  922. priv->schid = sch->schid;
  923. priv->state = DEV_STATE_NOT_OPER;
  924. INIT_LIST_HEAD(&priv->cmb_list);
  925. init_waitqueue_head(&priv->wait_q);
  926. init_timer(&priv->timer);
  927. /* Set an initial name for the device. */
  928. snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
  929. sch->schid.ssid, sch->schib.pmcw.dev);
  930. /* Increase counter of devices currently in recognition. */
  931. atomic_inc(&ccw_device_init_count);
  932. /* Start async. device sensing. */
  933. spin_lock_irq(sch->lock);
  934. rc = ccw_device_recognition(cdev);
  935. spin_unlock_irq(sch->lock);
  936. if (rc) {
  937. if (atomic_dec_and_test(&ccw_device_init_count))
  938. wake_up(&ccw_device_init_wq);
  939. }
  940. return rc;
  941. }
  942. static void ccw_device_move_to_sch(struct work_struct *work)
  943. {
  944. struct ccw_device_private *priv;
  945. int rc;
  946. struct subchannel *sch;
  947. struct ccw_device *cdev;
  948. struct subchannel *former_parent;
  949. priv = container_of(work, struct ccw_device_private, kick_work);
  950. sch = priv->sch;
  951. cdev = priv->cdev;
  952. former_parent = ccw_device_is_orphan(cdev) ?
  953. NULL : to_subchannel(get_device(cdev->dev.parent));
  954. mutex_lock(&sch->reg_mutex);
  955. /* Try to move the ccw device to its new subchannel. */
  956. rc = device_move(&cdev->dev, &sch->dev);
  957. mutex_unlock(&sch->reg_mutex);
  958. if (rc) {
  959. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  960. "0.%x.%04x failed (ret=%d)!\n",
  961. cdev->private->dev_id.ssid,
  962. cdev->private->dev_id.devno, sch->schid.ssid,
  963. sch->schid.sch_no, rc);
  964. css_sch_device_unregister(sch);
  965. goto out;
  966. }
  967. if (former_parent) {
  968. spin_lock_irq(former_parent->lock);
  969. sch_set_cdev(former_parent, NULL);
  970. spin_unlock_irq(former_parent->lock);
  971. css_sch_device_unregister(former_parent);
  972. /* Reset intparm to zeroes. */
  973. former_parent->schib.pmcw.intparm = 0;
  974. cio_modify(former_parent);
  975. }
  976. sch_attach_device(sch, cdev);
  977. out:
  978. if (former_parent)
  979. put_device(&former_parent->dev);
  980. put_device(&cdev->dev);
  981. }
  982. static void io_subchannel_irq(struct subchannel *sch)
  983. {
  984. struct ccw_device *cdev;
  985. cdev = sch_get_cdev(sch);
  986. CIO_TRACE_EVENT(3, "IRQ");
  987. CIO_TRACE_EVENT(3, sch->dev.bus_id);
  988. if (cdev)
  989. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  990. }
  991. static void io_subchannel_init_fields(struct subchannel *sch)
  992. {
  993. if (cio_is_console(sch->schid))
  994. sch->opm = 0xff;
  995. else
  996. sch->opm = chp_get_sch_opm(sch);
  997. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  998. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  999. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1000. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1001. sch->schib.pmcw.dev, sch->schid.ssid,
  1002. sch->schid.sch_no, sch->schib.pmcw.pim,
  1003. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1004. /* Initially set up some fields in the pmcw. */
  1005. sch->schib.pmcw.ena = 0;
  1006. sch->schib.pmcw.csense = 1; /* concurrent sense */
  1007. if ((sch->lpm & (sch->lpm - 1)) != 0)
  1008. sch->schib.pmcw.mp = 1; /* multipath mode */
  1009. /* clean up possible residual cmf stuff */
  1010. sch->schib.pmcw.mme = 0;
  1011. sch->schib.pmcw.mbfc = 0;
  1012. sch->schib.pmcw.mbi = 0;
  1013. sch->schib.mba = 0;
  1014. }
  1015. static int io_subchannel_probe(struct subchannel *sch)
  1016. {
  1017. struct ccw_device *cdev;
  1018. int rc;
  1019. unsigned long flags;
  1020. struct ccw_dev_id dev_id;
  1021. cdev = sch_get_cdev(sch);
  1022. if (cdev) {
  1023. rc = sysfs_create_group(&sch->dev.kobj,
  1024. &io_subchannel_attr_group);
  1025. if (rc)
  1026. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1027. "attributes for subchannel "
  1028. "0.%x.%04x (rc=%d)\n",
  1029. sch->schid.ssid, sch->schid.sch_no, rc);
  1030. /*
  1031. * This subchannel already has an associated ccw_device.
  1032. * Throw the delayed uevent for the subchannel, register
  1033. * the ccw_device and exit. This happens for all early
  1034. * devices, e.g. the console.
  1035. */
  1036. sch->dev.uevent_suppress = 0;
  1037. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1038. cdev->dev.groups = ccwdev_attr_groups;
  1039. device_initialize(&cdev->dev);
  1040. ccw_device_register(cdev);
  1041. /*
  1042. * Check if the device is already online. If it is
  1043. * the reference count needs to be corrected
  1044. * (see ccw_device_online and css_init_done for the
  1045. * ugly details).
  1046. */
  1047. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1048. cdev->private->state != DEV_STATE_OFFLINE &&
  1049. cdev->private->state != DEV_STATE_BOXED)
  1050. get_device(&cdev->dev);
  1051. return 0;
  1052. }
  1053. io_subchannel_init_fields(sch);
  1054. /*
  1055. * First check if a fitting device may be found amongst the
  1056. * disconnected devices or in the orphanage.
  1057. */
  1058. dev_id.devno = sch->schib.pmcw.dev;
  1059. dev_id.ssid = sch->schid.ssid;
  1060. rc = sysfs_create_group(&sch->dev.kobj,
  1061. &io_subchannel_attr_group);
  1062. if (rc)
  1063. return rc;
  1064. /* Allocate I/O subchannel private data. */
  1065. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1066. GFP_KERNEL | GFP_DMA);
  1067. if (!sch->private) {
  1068. rc = -ENOMEM;
  1069. goto out_err;
  1070. }
  1071. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1072. if (!cdev)
  1073. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1074. &dev_id);
  1075. if (cdev) {
  1076. /*
  1077. * Schedule moving the device until when we have a registered
  1078. * subchannel to move to and succeed the probe. We can
  1079. * unregister later again, when the probe is through.
  1080. */
  1081. cdev->private->sch = sch;
  1082. PREPARE_WORK(&cdev->private->kick_work,
  1083. ccw_device_move_to_sch);
  1084. queue_work(slow_path_wq, &cdev->private->kick_work);
  1085. return 0;
  1086. }
  1087. cdev = io_subchannel_create_ccwdev(sch);
  1088. if (IS_ERR(cdev)) {
  1089. rc = PTR_ERR(cdev);
  1090. goto out_err;
  1091. }
  1092. rc = io_subchannel_recog(cdev, sch);
  1093. if (rc) {
  1094. spin_lock_irqsave(sch->lock, flags);
  1095. sch_set_cdev(sch, NULL);
  1096. spin_unlock_irqrestore(sch->lock, flags);
  1097. if (cdev->dev.release)
  1098. cdev->dev.release(&cdev->dev);
  1099. goto out_err;
  1100. }
  1101. return 0;
  1102. out_err:
  1103. kfree(sch->private);
  1104. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1105. return rc;
  1106. }
  1107. static int
  1108. io_subchannel_remove (struct subchannel *sch)
  1109. {
  1110. struct ccw_device *cdev;
  1111. unsigned long flags;
  1112. cdev = sch_get_cdev(sch);
  1113. if (!cdev)
  1114. return 0;
  1115. /* Set ccw device to not operational and drop reference. */
  1116. spin_lock_irqsave(cdev->ccwlock, flags);
  1117. sch_set_cdev(sch, NULL);
  1118. cdev->private->state = DEV_STATE_NOT_OPER;
  1119. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1120. ccw_device_unregister(cdev);
  1121. put_device(&cdev->dev);
  1122. kfree(sch->private);
  1123. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1124. return 0;
  1125. }
  1126. static int io_subchannel_notify(struct subchannel *sch, int event)
  1127. {
  1128. struct ccw_device *cdev;
  1129. cdev = sch_get_cdev(sch);
  1130. if (!cdev)
  1131. return 0;
  1132. return ccw_device_notify(cdev, event);
  1133. }
  1134. static void io_subchannel_verify(struct subchannel *sch)
  1135. {
  1136. struct ccw_device *cdev;
  1137. cdev = sch_get_cdev(sch);
  1138. if (cdev)
  1139. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1140. }
  1141. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1142. {
  1143. int cc;
  1144. cc = stsch(sch->schid, &sch->schib);
  1145. if (cc)
  1146. return 0;
  1147. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1148. return 1;
  1149. return 0;
  1150. }
  1151. static void terminate_internal_io(struct subchannel *sch,
  1152. struct ccw_device *cdev)
  1153. {
  1154. if (cio_clear(sch)) {
  1155. /* Recheck device in case clear failed. */
  1156. sch->lpm = 0;
  1157. if (cdev->online)
  1158. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1159. else
  1160. css_schedule_eval(sch->schid);
  1161. return;
  1162. }
  1163. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1164. /* Request retry of internal operation. */
  1165. cdev->private->flags.intretry = 1;
  1166. /* Call handler. */
  1167. if (cdev->handler)
  1168. cdev->handler(cdev, cdev->private->intparm,
  1169. ERR_PTR(-EIO));
  1170. }
  1171. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1172. {
  1173. struct ccw_device *cdev;
  1174. cdev = sch_get_cdev(sch);
  1175. if (!cdev)
  1176. return;
  1177. if (check_for_io_on_path(sch, mask)) {
  1178. if (cdev->private->state == DEV_STATE_ONLINE)
  1179. ccw_device_kill_io(cdev);
  1180. else {
  1181. terminate_internal_io(sch, cdev);
  1182. /* Re-start path verification. */
  1183. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1184. }
  1185. } else
  1186. /* trigger path verification. */
  1187. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1188. }
  1189. static int io_subchannel_chp_event(struct subchannel *sch,
  1190. struct chp_link *link, int event)
  1191. {
  1192. int mask;
  1193. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1194. if (!mask)
  1195. return 0;
  1196. switch (event) {
  1197. case CHP_VARY_OFF:
  1198. sch->opm &= ~mask;
  1199. sch->lpm &= ~mask;
  1200. io_subchannel_terminate_path(sch, mask);
  1201. break;
  1202. case CHP_VARY_ON:
  1203. sch->opm |= mask;
  1204. sch->lpm |= mask;
  1205. io_subchannel_verify(sch);
  1206. break;
  1207. case CHP_OFFLINE:
  1208. if (stsch(sch->schid, &sch->schib))
  1209. return -ENXIO;
  1210. if (!css_sch_is_valid(&sch->schib))
  1211. return -ENODEV;
  1212. io_subchannel_terminate_path(sch, mask);
  1213. break;
  1214. case CHP_ONLINE:
  1215. if (stsch(sch->schid, &sch->schib))
  1216. return -ENXIO;
  1217. sch->lpm |= mask & sch->opm;
  1218. io_subchannel_verify(sch);
  1219. break;
  1220. }
  1221. return 0;
  1222. }
  1223. static void
  1224. io_subchannel_shutdown(struct subchannel *sch)
  1225. {
  1226. struct ccw_device *cdev;
  1227. int ret;
  1228. cdev = sch_get_cdev(sch);
  1229. if (cio_is_console(sch->schid))
  1230. return;
  1231. if (!sch->schib.pmcw.ena)
  1232. /* Nothing to do. */
  1233. return;
  1234. ret = cio_disable_subchannel(sch);
  1235. if (ret != -EBUSY)
  1236. /* Subchannel is disabled, we're done. */
  1237. return;
  1238. cdev->private->state = DEV_STATE_QUIESCE;
  1239. if (cdev->handler)
  1240. cdev->handler(cdev, cdev->private->intparm,
  1241. ERR_PTR(-EIO));
  1242. ret = ccw_device_cancel_halt_clear(cdev);
  1243. if (ret == -EBUSY) {
  1244. ccw_device_set_timeout(cdev, HZ/10);
  1245. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1246. }
  1247. cio_disable_subchannel(sch);
  1248. }
  1249. static int io_subchannel_get_status(struct subchannel *sch)
  1250. {
  1251. struct schib schib;
  1252. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1253. return CIO_GONE;
  1254. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1255. return CIO_REVALIDATE;
  1256. if (!sch->lpm)
  1257. return CIO_NO_PATH;
  1258. return CIO_OPER;
  1259. }
  1260. static int device_is_disconnected(struct ccw_device *cdev)
  1261. {
  1262. if (!cdev)
  1263. return 0;
  1264. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1265. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1266. }
  1267. static int recovery_check(struct device *dev, void *data)
  1268. {
  1269. struct ccw_device *cdev = to_ccwdev(dev);
  1270. int *redo = data;
  1271. spin_lock_irq(cdev->ccwlock);
  1272. switch (cdev->private->state) {
  1273. case DEV_STATE_DISCONNECTED:
  1274. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1275. cdev->private->dev_id.ssid,
  1276. cdev->private->dev_id.devno);
  1277. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1278. *redo = 1;
  1279. break;
  1280. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1281. *redo = 1;
  1282. break;
  1283. }
  1284. spin_unlock_irq(cdev->ccwlock);
  1285. return 0;
  1286. }
  1287. static void recovery_work_func(struct work_struct *unused)
  1288. {
  1289. int redo = 0;
  1290. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1291. if (redo) {
  1292. spin_lock_irq(&recovery_lock);
  1293. if (!timer_pending(&recovery_timer)) {
  1294. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1295. recovery_phase++;
  1296. mod_timer(&recovery_timer, jiffies +
  1297. recovery_delay[recovery_phase] * HZ);
  1298. }
  1299. spin_unlock_irq(&recovery_lock);
  1300. } else
  1301. CIO_MSG_EVENT(4, "recovery: end\n");
  1302. }
  1303. static DECLARE_WORK(recovery_work, recovery_work_func);
  1304. static void recovery_func(unsigned long data)
  1305. {
  1306. /*
  1307. * We can't do our recovery in softirq context and it's not
  1308. * performance critical, so we schedule it.
  1309. */
  1310. schedule_work(&recovery_work);
  1311. }
  1312. static void ccw_device_schedule_recovery(void)
  1313. {
  1314. unsigned long flags;
  1315. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1316. spin_lock_irqsave(&recovery_lock, flags);
  1317. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1318. recovery_phase = 0;
  1319. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1320. }
  1321. spin_unlock_irqrestore(&recovery_lock, flags);
  1322. }
  1323. static void device_set_disconnected(struct ccw_device *cdev)
  1324. {
  1325. if (!cdev)
  1326. return;
  1327. ccw_device_set_timeout(cdev, 0);
  1328. cdev->private->flags.fake_irb = 0;
  1329. cdev->private->state = DEV_STATE_DISCONNECTED;
  1330. if (cdev->online)
  1331. ccw_device_schedule_recovery();
  1332. }
  1333. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1334. {
  1335. int event, ret, disc;
  1336. unsigned long flags;
  1337. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
  1338. struct ccw_device *cdev;
  1339. spin_lock_irqsave(sch->lock, flags);
  1340. cdev = sch_get_cdev(sch);
  1341. disc = device_is_disconnected(cdev);
  1342. if (disc && slow) {
  1343. /* Disconnected devices are evaluated directly only.*/
  1344. spin_unlock_irqrestore(sch->lock, flags);
  1345. return 0;
  1346. }
  1347. /* No interrupt after machine check - kill pending timers. */
  1348. if (cdev)
  1349. ccw_device_set_timeout(cdev, 0);
  1350. if (!disc && !slow) {
  1351. /* Non-disconnected devices are evaluated on the slow path. */
  1352. spin_unlock_irqrestore(sch->lock, flags);
  1353. return -EAGAIN;
  1354. }
  1355. event = io_subchannel_get_status(sch);
  1356. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1357. sch->schid.ssid, sch->schid.sch_no, event,
  1358. disc ? "disconnected" : "normal",
  1359. slow ? "slow" : "fast");
  1360. /* Analyze subchannel status. */
  1361. action = NONE;
  1362. switch (event) {
  1363. case CIO_NO_PATH:
  1364. if (disc) {
  1365. /* Check if paths have become available. */
  1366. action = REPROBE;
  1367. break;
  1368. }
  1369. /* fall through */
  1370. case CIO_GONE:
  1371. /* Prevent unwanted effects when opening lock. */
  1372. cio_disable_subchannel(sch);
  1373. device_set_disconnected(cdev);
  1374. /* Ask driver what to do with device. */
  1375. action = UNREGISTER;
  1376. spin_unlock_irqrestore(sch->lock, flags);
  1377. ret = io_subchannel_notify(sch, event);
  1378. spin_lock_irqsave(sch->lock, flags);
  1379. if (ret)
  1380. action = NONE;
  1381. break;
  1382. case CIO_REVALIDATE:
  1383. /* Device will be removed, so no notify necessary. */
  1384. if (disc)
  1385. /* Reprobe because immediate unregister might block. */
  1386. action = REPROBE;
  1387. else
  1388. action = UNREGISTER_PROBE;
  1389. break;
  1390. case CIO_OPER:
  1391. if (disc)
  1392. /* Get device operational again. */
  1393. action = REPROBE;
  1394. break;
  1395. }
  1396. /* Perform action. */
  1397. ret = 0;
  1398. switch (action) {
  1399. case UNREGISTER:
  1400. case UNREGISTER_PROBE:
  1401. /* Unregister device (will use subchannel lock). */
  1402. spin_unlock_irqrestore(sch->lock, flags);
  1403. css_sch_device_unregister(sch);
  1404. spin_lock_irqsave(sch->lock, flags);
  1405. /* Reset intparm to zeroes. */
  1406. sch->schib.pmcw.intparm = 0;
  1407. cio_modify(sch);
  1408. break;
  1409. case REPROBE:
  1410. ccw_device_trigger_reprobe(cdev);
  1411. break;
  1412. default:
  1413. break;
  1414. }
  1415. spin_unlock_irqrestore(sch->lock, flags);
  1416. /* Probe if necessary. */
  1417. if (action == UNREGISTER_PROBE)
  1418. ret = css_probe_device(sch->schid);
  1419. return ret;
  1420. }
  1421. #ifdef CONFIG_CCW_CONSOLE
  1422. static struct ccw_device console_cdev;
  1423. static struct ccw_device_private console_private;
  1424. static int console_cdev_in_use;
  1425. static DEFINE_SPINLOCK(ccw_console_lock);
  1426. spinlock_t * cio_get_console_lock(void)
  1427. {
  1428. return &ccw_console_lock;
  1429. }
  1430. static int ccw_device_console_enable(struct ccw_device *cdev,
  1431. struct subchannel *sch)
  1432. {
  1433. int rc;
  1434. /* Attach subchannel private data. */
  1435. sch->private = cio_get_console_priv();
  1436. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1437. io_subchannel_init_fields(sch);
  1438. sch->driver = &io_subchannel_driver;
  1439. /* Initialize the ccw_device structure. */
  1440. cdev->dev.parent= &sch->dev;
  1441. rc = io_subchannel_recog(cdev, sch);
  1442. if (rc)
  1443. return rc;
  1444. /* Now wait for the async. recognition to come to an end. */
  1445. spin_lock_irq(cdev->ccwlock);
  1446. while (!dev_fsm_final_state(cdev))
  1447. wait_cons_dev();
  1448. rc = -EIO;
  1449. if (cdev->private->state != DEV_STATE_OFFLINE)
  1450. goto out_unlock;
  1451. ccw_device_online(cdev);
  1452. while (!dev_fsm_final_state(cdev))
  1453. wait_cons_dev();
  1454. if (cdev->private->state != DEV_STATE_ONLINE)
  1455. goto out_unlock;
  1456. rc = 0;
  1457. out_unlock:
  1458. spin_unlock_irq(cdev->ccwlock);
  1459. return 0;
  1460. }
  1461. struct ccw_device *
  1462. ccw_device_probe_console(void)
  1463. {
  1464. struct subchannel *sch;
  1465. int ret;
  1466. if (xchg(&console_cdev_in_use, 1) != 0)
  1467. return ERR_PTR(-EBUSY);
  1468. sch = cio_probe_console();
  1469. if (IS_ERR(sch)) {
  1470. console_cdev_in_use = 0;
  1471. return (void *) sch;
  1472. }
  1473. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1474. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1475. console_cdev.private = &console_private;
  1476. console_private.cdev = &console_cdev;
  1477. ret = ccw_device_console_enable(&console_cdev, sch);
  1478. if (ret) {
  1479. cio_release_console();
  1480. console_cdev_in_use = 0;
  1481. return ERR_PTR(ret);
  1482. }
  1483. console_cdev.online = 1;
  1484. return &console_cdev;
  1485. }
  1486. #endif
  1487. /*
  1488. * get ccw_device matching the busid, but only if owned by cdrv
  1489. */
  1490. static int
  1491. __ccwdev_check_busid(struct device *dev, void *id)
  1492. {
  1493. char *bus_id;
  1494. bus_id = id;
  1495. return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
  1496. }
  1497. /**
  1498. * get_ccwdev_by_busid() - obtain device from a bus id
  1499. * @cdrv: driver the device is owned by
  1500. * @bus_id: bus id of the device to be searched
  1501. *
  1502. * This function searches all devices owned by @cdrv for a device with a bus
  1503. * id matching @bus_id.
  1504. * Returns:
  1505. * If a match is found, its reference count of the found device is increased
  1506. * and it is returned; else %NULL is returned.
  1507. */
  1508. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1509. const char *bus_id)
  1510. {
  1511. struct device *dev;
  1512. struct device_driver *drv;
  1513. drv = get_driver(&cdrv->driver);
  1514. if (!drv)
  1515. return NULL;
  1516. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1517. __ccwdev_check_busid);
  1518. put_driver(drv);
  1519. return dev ? to_ccwdev(dev) : NULL;
  1520. }
  1521. /************************** device driver handling ************************/
  1522. /* This is the implementation of the ccw_driver class. The probe, remove
  1523. * and release methods are initially very similar to the device_driver
  1524. * implementations, with the difference that they have ccw_device
  1525. * arguments.
  1526. *
  1527. * A ccw driver also contains the information that is needed for
  1528. * device matching.
  1529. */
  1530. static int
  1531. ccw_device_probe (struct device *dev)
  1532. {
  1533. struct ccw_device *cdev = to_ccwdev(dev);
  1534. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1535. int ret;
  1536. cdev->drv = cdrv; /* to let the driver call _set_online */
  1537. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1538. if (ret) {
  1539. cdev->drv = NULL;
  1540. return ret;
  1541. }
  1542. return 0;
  1543. }
  1544. static int
  1545. ccw_device_remove (struct device *dev)
  1546. {
  1547. struct ccw_device *cdev = to_ccwdev(dev);
  1548. struct ccw_driver *cdrv = cdev->drv;
  1549. int ret;
  1550. if (cdrv->remove)
  1551. cdrv->remove(cdev);
  1552. if (cdev->online) {
  1553. cdev->online = 0;
  1554. spin_lock_irq(cdev->ccwlock);
  1555. ret = ccw_device_offline(cdev);
  1556. spin_unlock_irq(cdev->ccwlock);
  1557. if (ret == 0)
  1558. wait_event(cdev->private->wait_q,
  1559. dev_fsm_final_state(cdev));
  1560. else
  1561. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1562. "device 0.%x.%04x\n",
  1563. ret, cdev->private->dev_id.ssid,
  1564. cdev->private->dev_id.devno);
  1565. }
  1566. ccw_device_set_timeout(cdev, 0);
  1567. cdev->drv = NULL;
  1568. return 0;
  1569. }
  1570. static void ccw_device_shutdown(struct device *dev)
  1571. {
  1572. struct ccw_device *cdev;
  1573. cdev = to_ccwdev(dev);
  1574. if (cdev->drv && cdev->drv->shutdown)
  1575. cdev->drv->shutdown(cdev);
  1576. disable_cmf(cdev);
  1577. }
  1578. struct bus_type ccw_bus_type = {
  1579. .name = "ccw",
  1580. .match = ccw_bus_match,
  1581. .uevent = ccw_uevent,
  1582. .probe = ccw_device_probe,
  1583. .remove = ccw_device_remove,
  1584. .shutdown = ccw_device_shutdown,
  1585. };
  1586. /**
  1587. * ccw_driver_register() - register a ccw driver
  1588. * @cdriver: driver to be registered
  1589. *
  1590. * This function is mainly a wrapper around driver_register().
  1591. * Returns:
  1592. * %0 on success and a negative error value on failure.
  1593. */
  1594. int ccw_driver_register(struct ccw_driver *cdriver)
  1595. {
  1596. struct device_driver *drv = &cdriver->driver;
  1597. drv->bus = &ccw_bus_type;
  1598. drv->name = cdriver->name;
  1599. drv->owner = cdriver->owner;
  1600. return driver_register(drv);
  1601. }
  1602. /**
  1603. * ccw_driver_unregister() - deregister a ccw driver
  1604. * @cdriver: driver to be deregistered
  1605. *
  1606. * This function is mainly a wrapper around driver_unregister().
  1607. */
  1608. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1609. {
  1610. driver_unregister(&cdriver->driver);
  1611. }
  1612. /* Helper func for qdio. */
  1613. struct subchannel_id
  1614. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1615. {
  1616. struct subchannel *sch;
  1617. sch = to_subchannel(cdev->dev.parent);
  1618. return sch->schid;
  1619. }
  1620. MODULE_LICENSE("GPL");
  1621. EXPORT_SYMBOL(ccw_device_set_online);
  1622. EXPORT_SYMBOL(ccw_device_set_offline);
  1623. EXPORT_SYMBOL(ccw_driver_register);
  1624. EXPORT_SYMBOL(ccw_driver_unregister);
  1625. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1626. EXPORT_SYMBOL(ccw_bus_type);
  1627. EXPORT_SYMBOL(ccw_device_work);
  1628. EXPORT_SYMBOL(ccw_device_notify_work);
  1629. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);