device.c 46 KB

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