device.c 56 KB

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