device.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213
  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. /* Get cdev reference for workqueue processing. */
  284. if (!get_device(&cdev->dev))
  285. return;
  286. if (ccw_device_is_orphan(cdev)) {
  287. /*
  288. * Deregister ccw device.
  289. * Unfortunately, we cannot do this directly from the
  290. * attribute method.
  291. */
  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. /* Reset intparm to zeroes. */
  912. sch->config.intparm = 0;
  913. cio_commit_config(sch);
  914. /* Release cdev reference for workqueue processing.*/
  915. put_device(&cdev->dev);
  916. /* Release subchannel reference for local processing. */
  917. put_device(&sch->dev);
  918. }
  919. void ccw_device_schedule_sch_unregister(struct ccw_device *cdev)
  920. {
  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. /* Remove device found not operational. */
  941. if (!get_device(&cdev->dev))
  942. break;
  943. ccw_device_schedule_sch_unregister(cdev);
  944. if (atomic_dec_and_test(&ccw_device_init_count))
  945. wake_up(&ccw_device_init_wq);
  946. break;
  947. case DEV_STATE_OFFLINE:
  948. /*
  949. * We can't register the device in interrupt context so
  950. * we schedule a work item.
  951. */
  952. if (!get_device(&cdev->dev))
  953. break;
  954. PREPARE_WORK(&cdev->private->kick_work,
  955. io_subchannel_register);
  956. queue_work(slow_path_wq, &cdev->private->kick_work);
  957. break;
  958. }
  959. }
  960. static int
  961. io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  962. {
  963. int rc;
  964. struct ccw_device_private *priv;
  965. sch_set_cdev(sch, cdev);
  966. cdev->ccwlock = sch->lock;
  967. /* Init private data. */
  968. priv = cdev->private;
  969. priv->dev_id.devno = sch->schib.pmcw.dev;
  970. priv->dev_id.ssid = sch->schid.ssid;
  971. priv->schid = sch->schid;
  972. priv->state = DEV_STATE_NOT_OPER;
  973. INIT_LIST_HEAD(&priv->cmb_list);
  974. init_waitqueue_head(&priv->wait_q);
  975. init_timer(&priv->timer);
  976. /* Set an initial name for the device. */
  977. if (cio_is_console(sch->schid))
  978. cdev->dev.init_name = cio_get_console_cdev_name(sch);
  979. else
  980. dev_set_name(&cdev->dev, "0.%x.%04x",
  981. sch->schid.ssid, sch->schib.pmcw.dev);
  982. /* Increase counter of devices currently in recognition. */
  983. atomic_inc(&ccw_device_init_count);
  984. /* Start async. device sensing. */
  985. spin_lock_irq(sch->lock);
  986. rc = ccw_device_recognition(cdev);
  987. spin_unlock_irq(sch->lock);
  988. if (rc) {
  989. if (atomic_dec_and_test(&ccw_device_init_count))
  990. wake_up(&ccw_device_init_wq);
  991. }
  992. return rc;
  993. }
  994. static void ccw_device_move_to_sch(struct work_struct *work)
  995. {
  996. struct ccw_device_private *priv;
  997. int rc;
  998. struct subchannel *sch;
  999. struct ccw_device *cdev;
  1000. struct subchannel *former_parent;
  1001. priv = container_of(work, struct ccw_device_private, kick_work);
  1002. sch = priv->sch;
  1003. cdev = priv->cdev;
  1004. former_parent = to_subchannel(cdev->dev.parent);
  1005. /* Get reference for new parent. */
  1006. if (!get_device(&sch->dev))
  1007. return;
  1008. mutex_lock(&sch->reg_mutex);
  1009. /*
  1010. * Try to move the ccw device to its new subchannel.
  1011. * Note: device_move() changes cdev->dev.parent
  1012. */
  1013. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  1014. mutex_unlock(&sch->reg_mutex);
  1015. if (rc) {
  1016. CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
  1017. "0.%x.%04x failed (ret=%d)!\n",
  1018. cdev->private->dev_id.ssid,
  1019. cdev->private->dev_id.devno, sch->schid.ssid,
  1020. sch->schid.sch_no, rc);
  1021. css_sch_device_unregister(sch);
  1022. /* Put reference for new parent again. */
  1023. put_device(&sch->dev);
  1024. goto out;
  1025. }
  1026. if (!sch_is_pseudo_sch(former_parent)) {
  1027. spin_lock_irq(former_parent->lock);
  1028. sch_set_cdev(former_parent, NULL);
  1029. spin_unlock_irq(former_parent->lock);
  1030. css_sch_device_unregister(former_parent);
  1031. /* Reset intparm to zeroes. */
  1032. former_parent->config.intparm = 0;
  1033. cio_commit_config(former_parent);
  1034. }
  1035. sch_attach_device(sch, cdev);
  1036. out:
  1037. /* Put reference for old parent. */
  1038. put_device(&former_parent->dev);
  1039. put_device(&cdev->dev);
  1040. }
  1041. static void io_subchannel_irq(struct subchannel *sch)
  1042. {
  1043. struct ccw_device *cdev;
  1044. cdev = sch_get_cdev(sch);
  1045. CIO_TRACE_EVENT(3, "IRQ");
  1046. CIO_TRACE_EVENT(3, dev_name(&sch->dev));
  1047. if (cdev)
  1048. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  1049. }
  1050. void io_subchannel_init_config(struct subchannel *sch)
  1051. {
  1052. memset(&sch->config, 0, sizeof(sch->config));
  1053. sch->config.csense = 1;
  1054. /* Use subchannel mp mode when there is more than 1 installed CHPID. */
  1055. if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0)
  1056. sch->config.mp = 1;
  1057. }
  1058. static void io_subchannel_init_fields(struct subchannel *sch)
  1059. {
  1060. if (cio_is_console(sch->schid))
  1061. sch->opm = 0xff;
  1062. else
  1063. sch->opm = chp_get_sch_opm(sch);
  1064. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  1065. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  1066. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  1067. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  1068. sch->schib.pmcw.dev, sch->schid.ssid,
  1069. sch->schid.sch_no, sch->schib.pmcw.pim,
  1070. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  1071. io_subchannel_init_config(sch);
  1072. }
  1073. static void io_subchannel_do_unreg(struct work_struct *work)
  1074. {
  1075. struct subchannel *sch;
  1076. sch = container_of(work, struct subchannel, work);
  1077. css_sch_device_unregister(sch);
  1078. /* Reset intparm to zeroes. */
  1079. sch->config.intparm = 0;
  1080. cio_commit_config(sch);
  1081. put_device(&sch->dev);
  1082. }
  1083. /* Schedule unregister if we have no cdev. */
  1084. static void io_subchannel_schedule_removal(struct subchannel *sch)
  1085. {
  1086. get_device(&sch->dev);
  1087. INIT_WORK(&sch->work, io_subchannel_do_unreg);
  1088. queue_work(slow_path_wq, &sch->work);
  1089. }
  1090. /*
  1091. * Note: We always return 0 so that we bind to the device even on error.
  1092. * This is needed so that our remove function is called on unregister.
  1093. */
  1094. static int io_subchannel_probe(struct subchannel *sch)
  1095. {
  1096. struct ccw_device *cdev;
  1097. int rc;
  1098. unsigned long flags;
  1099. struct ccw_dev_id dev_id;
  1100. cdev = sch_get_cdev(sch);
  1101. if (cdev) {
  1102. rc = sysfs_create_group(&sch->dev.kobj,
  1103. &io_subchannel_attr_group);
  1104. if (rc)
  1105. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  1106. "attributes for subchannel "
  1107. "0.%x.%04x (rc=%d)\n",
  1108. sch->schid.ssid, sch->schid.sch_no, rc);
  1109. /*
  1110. * This subchannel already has an associated ccw_device.
  1111. * Throw the delayed uevent for the subchannel, register
  1112. * the ccw_device and exit. This happens for all early
  1113. * devices, e.g. the console.
  1114. */
  1115. dev_set_uevent_suppress(&sch->dev, 0);
  1116. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  1117. cdev->dev.groups = ccwdev_attr_groups;
  1118. device_initialize(&cdev->dev);
  1119. ccw_device_register(cdev);
  1120. /*
  1121. * Check if the device is already online. If it is
  1122. * the reference count needs to be corrected since we
  1123. * didn't obtain a reference in ccw_device_set_online.
  1124. */
  1125. if (cdev->private->state != DEV_STATE_NOT_OPER &&
  1126. cdev->private->state != DEV_STATE_OFFLINE &&
  1127. cdev->private->state != DEV_STATE_BOXED)
  1128. get_device(&cdev->dev);
  1129. return 0;
  1130. }
  1131. io_subchannel_init_fields(sch);
  1132. rc = cio_commit_config(sch);
  1133. if (rc)
  1134. goto out_schedule;
  1135. rc = sysfs_create_group(&sch->dev.kobj,
  1136. &io_subchannel_attr_group);
  1137. if (rc)
  1138. goto out_schedule;
  1139. /* Allocate I/O subchannel private data. */
  1140. sch->private = kzalloc(sizeof(struct io_subchannel_private),
  1141. GFP_KERNEL | GFP_DMA);
  1142. if (!sch->private)
  1143. goto out_err;
  1144. /*
  1145. * First check if a fitting device may be found amongst the
  1146. * disconnected devices or in the orphanage.
  1147. */
  1148. dev_id.devno = sch->schib.pmcw.dev;
  1149. dev_id.ssid = sch->schid.ssid;
  1150. cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
  1151. if (!cdev)
  1152. cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
  1153. &dev_id);
  1154. if (cdev) {
  1155. /*
  1156. * Schedule moving the device until when we have a registered
  1157. * subchannel to move to and succeed the probe. We can
  1158. * unregister later again, when the probe is through.
  1159. */
  1160. cdev->private->sch = sch;
  1161. PREPARE_WORK(&cdev->private->kick_work,
  1162. ccw_device_move_to_sch);
  1163. queue_work(slow_path_wq, &cdev->private->kick_work);
  1164. return 0;
  1165. }
  1166. cdev = io_subchannel_create_ccwdev(sch);
  1167. if (IS_ERR(cdev))
  1168. goto out_err;
  1169. rc = io_subchannel_recog(cdev, sch);
  1170. if (rc) {
  1171. spin_lock_irqsave(sch->lock, flags);
  1172. io_subchannel_recog_done(cdev);
  1173. spin_unlock_irqrestore(sch->lock, flags);
  1174. }
  1175. return 0;
  1176. out_err:
  1177. kfree(sch->private);
  1178. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1179. out_schedule:
  1180. io_subchannel_schedule_removal(sch);
  1181. return 0;
  1182. }
  1183. static int
  1184. io_subchannel_remove (struct subchannel *sch)
  1185. {
  1186. struct ccw_device *cdev;
  1187. unsigned long flags;
  1188. cdev = sch_get_cdev(sch);
  1189. if (!cdev)
  1190. return 0;
  1191. /* Set ccw device to not operational and drop reference. */
  1192. spin_lock_irqsave(cdev->ccwlock, flags);
  1193. sch_set_cdev(sch, NULL);
  1194. cdev->private->state = DEV_STATE_NOT_OPER;
  1195. spin_unlock_irqrestore(cdev->ccwlock, flags);
  1196. ccw_device_unregister(cdev);
  1197. put_device(&cdev->dev);
  1198. kfree(sch->private);
  1199. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1200. return 0;
  1201. }
  1202. static int io_subchannel_notify(struct subchannel *sch, int event)
  1203. {
  1204. struct ccw_device *cdev;
  1205. cdev = sch_get_cdev(sch);
  1206. if (!cdev)
  1207. return 0;
  1208. return ccw_device_notify(cdev, event);
  1209. }
  1210. static void io_subchannel_verify(struct subchannel *sch)
  1211. {
  1212. struct ccw_device *cdev;
  1213. cdev = sch_get_cdev(sch);
  1214. if (cdev)
  1215. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1216. }
  1217. static int check_for_io_on_path(struct subchannel *sch, int mask)
  1218. {
  1219. if (cio_update_schib(sch))
  1220. return 0;
  1221. if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
  1222. return 1;
  1223. return 0;
  1224. }
  1225. static void terminate_internal_io(struct subchannel *sch,
  1226. struct ccw_device *cdev)
  1227. {
  1228. if (cio_clear(sch)) {
  1229. /* Recheck device in case clear failed. */
  1230. sch->lpm = 0;
  1231. if (cdev->online)
  1232. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1233. else
  1234. css_schedule_eval(sch->schid);
  1235. return;
  1236. }
  1237. cdev->private->state = DEV_STATE_CLEAR_VERIFY;
  1238. /* Request retry of internal operation. */
  1239. cdev->private->flags.intretry = 1;
  1240. /* Call handler. */
  1241. if (cdev->handler)
  1242. cdev->handler(cdev, cdev->private->intparm,
  1243. ERR_PTR(-EIO));
  1244. }
  1245. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1246. {
  1247. struct ccw_device *cdev;
  1248. cdev = sch_get_cdev(sch);
  1249. if (!cdev)
  1250. return;
  1251. if (check_for_io_on_path(sch, mask)) {
  1252. if (cdev->private->state == DEV_STATE_ONLINE)
  1253. ccw_device_kill_io(cdev);
  1254. else {
  1255. terminate_internal_io(sch, cdev);
  1256. /* Re-start path verification. */
  1257. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1258. }
  1259. } else
  1260. /* trigger path verification. */
  1261. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1262. }
  1263. static int io_subchannel_chp_event(struct subchannel *sch,
  1264. struct chp_link *link, int event)
  1265. {
  1266. int mask;
  1267. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1268. if (!mask)
  1269. return 0;
  1270. switch (event) {
  1271. case CHP_VARY_OFF:
  1272. sch->opm &= ~mask;
  1273. sch->lpm &= ~mask;
  1274. io_subchannel_terminate_path(sch, mask);
  1275. break;
  1276. case CHP_VARY_ON:
  1277. sch->opm |= mask;
  1278. sch->lpm |= mask;
  1279. io_subchannel_verify(sch);
  1280. break;
  1281. case CHP_OFFLINE:
  1282. if (cio_update_schib(sch))
  1283. return -ENODEV;
  1284. io_subchannel_terminate_path(sch, mask);
  1285. break;
  1286. case CHP_ONLINE:
  1287. if (cio_update_schib(sch))
  1288. return -ENODEV;
  1289. sch->lpm |= mask & sch->opm;
  1290. io_subchannel_verify(sch);
  1291. break;
  1292. }
  1293. return 0;
  1294. }
  1295. static void
  1296. io_subchannel_shutdown(struct subchannel *sch)
  1297. {
  1298. struct ccw_device *cdev;
  1299. int ret;
  1300. cdev = sch_get_cdev(sch);
  1301. if (cio_is_console(sch->schid))
  1302. return;
  1303. if (!sch->schib.pmcw.ena)
  1304. /* Nothing to do. */
  1305. return;
  1306. ret = cio_disable_subchannel(sch);
  1307. if (ret != -EBUSY)
  1308. /* Subchannel is disabled, we're done. */
  1309. return;
  1310. cdev->private->state = DEV_STATE_QUIESCE;
  1311. if (cdev->handler)
  1312. cdev->handler(cdev, cdev->private->intparm,
  1313. ERR_PTR(-EIO));
  1314. ret = ccw_device_cancel_halt_clear(cdev);
  1315. if (ret == -EBUSY) {
  1316. ccw_device_set_timeout(cdev, HZ/10);
  1317. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1318. }
  1319. cio_disable_subchannel(sch);
  1320. }
  1321. static int io_subchannel_get_status(struct subchannel *sch)
  1322. {
  1323. struct schib schib;
  1324. if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
  1325. return CIO_GONE;
  1326. if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
  1327. return CIO_REVALIDATE;
  1328. if (!sch->lpm)
  1329. return CIO_NO_PATH;
  1330. return CIO_OPER;
  1331. }
  1332. static int device_is_disconnected(struct ccw_device *cdev)
  1333. {
  1334. if (!cdev)
  1335. return 0;
  1336. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1337. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1338. }
  1339. static int recovery_check(struct device *dev, void *data)
  1340. {
  1341. struct ccw_device *cdev = to_ccwdev(dev);
  1342. int *redo = data;
  1343. spin_lock_irq(cdev->ccwlock);
  1344. switch (cdev->private->state) {
  1345. case DEV_STATE_DISCONNECTED:
  1346. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1347. cdev->private->dev_id.ssid,
  1348. cdev->private->dev_id.devno);
  1349. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1350. *redo = 1;
  1351. break;
  1352. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1353. *redo = 1;
  1354. break;
  1355. }
  1356. spin_unlock_irq(cdev->ccwlock);
  1357. return 0;
  1358. }
  1359. static void recovery_work_func(struct work_struct *unused)
  1360. {
  1361. int redo = 0;
  1362. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1363. if (redo) {
  1364. spin_lock_irq(&recovery_lock);
  1365. if (!timer_pending(&recovery_timer)) {
  1366. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1367. recovery_phase++;
  1368. mod_timer(&recovery_timer, jiffies +
  1369. recovery_delay[recovery_phase] * HZ);
  1370. }
  1371. spin_unlock_irq(&recovery_lock);
  1372. } else
  1373. CIO_MSG_EVENT(4, "recovery: end\n");
  1374. }
  1375. static DECLARE_WORK(recovery_work, recovery_work_func);
  1376. static void recovery_func(unsigned long data)
  1377. {
  1378. /*
  1379. * We can't do our recovery in softirq context and it's not
  1380. * performance critical, so we schedule it.
  1381. */
  1382. schedule_work(&recovery_work);
  1383. }
  1384. static void ccw_device_schedule_recovery(void)
  1385. {
  1386. unsigned long flags;
  1387. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1388. spin_lock_irqsave(&recovery_lock, flags);
  1389. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1390. recovery_phase = 0;
  1391. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1392. }
  1393. spin_unlock_irqrestore(&recovery_lock, flags);
  1394. }
  1395. static int purge_fn(struct device *dev, void *data)
  1396. {
  1397. struct ccw_device *cdev = to_ccwdev(dev);
  1398. struct ccw_device_private *priv = cdev->private;
  1399. int unreg;
  1400. spin_lock_irq(cdev->ccwlock);
  1401. unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) &&
  1402. (priv->state == DEV_STATE_OFFLINE);
  1403. spin_unlock_irq(cdev->ccwlock);
  1404. if (!unreg)
  1405. goto out;
  1406. if (!get_device(&cdev->dev))
  1407. goto out;
  1408. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid,
  1409. priv->dev_id.devno);
  1410. ccw_device_schedule_sch_unregister(cdev);
  1411. out:
  1412. /* Abort loop in case of pending signal. */
  1413. if (signal_pending(current))
  1414. return -EINTR;
  1415. return 0;
  1416. }
  1417. /**
  1418. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1419. *
  1420. * Unregister all ccw devices that are offline and on the blacklist.
  1421. */
  1422. int ccw_purge_blacklisted(void)
  1423. {
  1424. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1425. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1426. return 0;
  1427. }
  1428. static void device_set_disconnected(struct ccw_device *cdev)
  1429. {
  1430. if (!cdev)
  1431. return;
  1432. ccw_device_set_timeout(cdev, 0);
  1433. cdev->private->flags.fake_irb = 0;
  1434. cdev->private->state = DEV_STATE_DISCONNECTED;
  1435. if (cdev->online)
  1436. ccw_device_schedule_recovery();
  1437. }
  1438. void ccw_device_set_notoper(struct ccw_device *cdev)
  1439. {
  1440. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1441. CIO_TRACE_EVENT(2, "notoper");
  1442. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1443. ccw_device_set_timeout(cdev, 0);
  1444. cio_disable_subchannel(sch);
  1445. cdev->private->state = DEV_STATE_NOT_OPER;
  1446. }
  1447. static int io_subchannel_sch_event(struct subchannel *sch, int slow)
  1448. {
  1449. int event, ret, disc;
  1450. unsigned long flags;
  1451. enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
  1452. struct ccw_device *cdev;
  1453. spin_lock_irqsave(sch->lock, flags);
  1454. cdev = sch_get_cdev(sch);
  1455. disc = device_is_disconnected(cdev);
  1456. if (disc && slow) {
  1457. /* Disconnected devices are evaluated directly only.*/
  1458. spin_unlock_irqrestore(sch->lock, flags);
  1459. return 0;
  1460. }
  1461. /* No interrupt after machine check - kill pending timers. */
  1462. if (cdev)
  1463. ccw_device_set_timeout(cdev, 0);
  1464. if (!disc && !slow) {
  1465. /* Non-disconnected devices are evaluated on the slow path. */
  1466. spin_unlock_irqrestore(sch->lock, flags);
  1467. return -EAGAIN;
  1468. }
  1469. event = io_subchannel_get_status(sch);
  1470. CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
  1471. sch->schid.ssid, sch->schid.sch_no, event,
  1472. disc ? "disconnected" : "normal",
  1473. slow ? "slow" : "fast");
  1474. /* Analyze subchannel status. */
  1475. action = NONE;
  1476. switch (event) {
  1477. case CIO_NO_PATH:
  1478. if (disc) {
  1479. /* Check if paths have become available. */
  1480. action = REPROBE;
  1481. break;
  1482. }
  1483. /* fall through */
  1484. case CIO_GONE:
  1485. /* Ask driver what to do with device. */
  1486. if (io_subchannel_notify(sch, event))
  1487. action = DISC;
  1488. else
  1489. action = UNREGISTER;
  1490. break;
  1491. case CIO_REVALIDATE:
  1492. /* Device will be removed, so no notify necessary. */
  1493. if (disc)
  1494. /* Reprobe because immediate unregister might block. */
  1495. action = REPROBE;
  1496. else
  1497. action = UNREGISTER_PROBE;
  1498. break;
  1499. case CIO_OPER:
  1500. if (disc)
  1501. /* Get device operational again. */
  1502. action = REPROBE;
  1503. break;
  1504. }
  1505. /* Perform action. */
  1506. ret = 0;
  1507. switch (action) {
  1508. case UNREGISTER:
  1509. case UNREGISTER_PROBE:
  1510. ccw_device_set_notoper(cdev);
  1511. /* Unregister device (will use subchannel lock). */
  1512. spin_unlock_irqrestore(sch->lock, flags);
  1513. css_sch_device_unregister(sch);
  1514. spin_lock_irqsave(sch->lock, flags);
  1515. /* Reset intparm to zeroes. */
  1516. sch->config.intparm = 0;
  1517. cio_commit_config(sch);
  1518. break;
  1519. case REPROBE:
  1520. ccw_device_trigger_reprobe(cdev);
  1521. break;
  1522. case DISC:
  1523. device_set_disconnected(cdev);
  1524. break;
  1525. default:
  1526. break;
  1527. }
  1528. spin_unlock_irqrestore(sch->lock, flags);
  1529. /* Probe if necessary. */
  1530. if (action == UNREGISTER_PROBE)
  1531. ret = css_probe_device(sch->schid);
  1532. return ret;
  1533. }
  1534. #ifdef CONFIG_CCW_CONSOLE
  1535. static struct ccw_device console_cdev;
  1536. static char console_cdev_name[10] = "0.x.xxxx";
  1537. static struct ccw_device_private console_private;
  1538. static int console_cdev_in_use;
  1539. static DEFINE_SPINLOCK(ccw_console_lock);
  1540. spinlock_t * cio_get_console_lock(void)
  1541. {
  1542. return &ccw_console_lock;
  1543. }
  1544. static int ccw_device_console_enable(struct ccw_device *cdev,
  1545. struct subchannel *sch)
  1546. {
  1547. int rc;
  1548. /* Attach subchannel private data. */
  1549. sch->private = cio_get_console_priv();
  1550. memset(sch->private, 0, sizeof(struct io_subchannel_private));
  1551. io_subchannel_init_fields(sch);
  1552. rc = cio_commit_config(sch);
  1553. if (rc)
  1554. return rc;
  1555. sch->driver = &io_subchannel_driver;
  1556. /* Initialize the ccw_device structure. */
  1557. cdev->dev.parent= &sch->dev;
  1558. rc = io_subchannel_recog(cdev, sch);
  1559. if (rc)
  1560. return rc;
  1561. /* Now wait for the async. recognition to come to an end. */
  1562. spin_lock_irq(cdev->ccwlock);
  1563. while (!dev_fsm_final_state(cdev))
  1564. wait_cons_dev();
  1565. rc = -EIO;
  1566. if (cdev->private->state != DEV_STATE_OFFLINE)
  1567. goto out_unlock;
  1568. ccw_device_online(cdev);
  1569. while (!dev_fsm_final_state(cdev))
  1570. wait_cons_dev();
  1571. if (cdev->private->state != DEV_STATE_ONLINE)
  1572. goto out_unlock;
  1573. rc = 0;
  1574. out_unlock:
  1575. spin_unlock_irq(cdev->ccwlock);
  1576. return 0;
  1577. }
  1578. struct ccw_device *
  1579. ccw_device_probe_console(void)
  1580. {
  1581. struct subchannel *sch;
  1582. int ret;
  1583. if (xchg(&console_cdev_in_use, 1) != 0)
  1584. return ERR_PTR(-EBUSY);
  1585. sch = cio_probe_console();
  1586. if (IS_ERR(sch)) {
  1587. console_cdev_in_use = 0;
  1588. return (void *) sch;
  1589. }
  1590. memset(&console_cdev, 0, sizeof(struct ccw_device));
  1591. memset(&console_private, 0, sizeof(struct ccw_device_private));
  1592. console_cdev.private = &console_private;
  1593. console_private.cdev = &console_cdev;
  1594. ret = ccw_device_console_enable(&console_cdev, sch);
  1595. if (ret) {
  1596. cio_release_console();
  1597. console_cdev_in_use = 0;
  1598. return ERR_PTR(ret);
  1599. }
  1600. console_cdev.online = 1;
  1601. return &console_cdev;
  1602. }
  1603. static int ccw_device_pm_restore(struct device *dev);
  1604. int ccw_device_force_console(void)
  1605. {
  1606. if (!console_cdev_in_use)
  1607. return -ENODEV;
  1608. return ccw_device_pm_restore(&console_cdev.dev);
  1609. }
  1610. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1611. const char *cio_get_console_cdev_name(struct subchannel *sch)
  1612. {
  1613. snprintf(console_cdev_name, 10, "0.%x.%04x",
  1614. sch->schid.ssid, sch->schib.pmcw.dev);
  1615. return (const char *)console_cdev_name;
  1616. }
  1617. #endif
  1618. /*
  1619. * get ccw_device matching the busid, but only if owned by cdrv
  1620. */
  1621. static int
  1622. __ccwdev_check_busid(struct device *dev, void *id)
  1623. {
  1624. char *bus_id;
  1625. bus_id = id;
  1626. return (strcmp(bus_id, dev_name(dev)) == 0);
  1627. }
  1628. /**
  1629. * get_ccwdev_by_busid() - obtain device from a bus id
  1630. * @cdrv: driver the device is owned by
  1631. * @bus_id: bus id of the device to be searched
  1632. *
  1633. * This function searches all devices owned by @cdrv for a device with a bus
  1634. * id matching @bus_id.
  1635. * Returns:
  1636. * If a match is found, its reference count of the found device is increased
  1637. * and it is returned; else %NULL is returned.
  1638. */
  1639. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1640. const char *bus_id)
  1641. {
  1642. struct device *dev;
  1643. struct device_driver *drv;
  1644. drv = get_driver(&cdrv->driver);
  1645. if (!drv)
  1646. return NULL;
  1647. dev = driver_find_device(drv, NULL, (void *)bus_id,
  1648. __ccwdev_check_busid);
  1649. put_driver(drv);
  1650. return dev ? to_ccwdev(dev) : NULL;
  1651. }
  1652. /************************** device driver handling ************************/
  1653. /* This is the implementation of the ccw_driver class. The probe, remove
  1654. * and release methods are initially very similar to the device_driver
  1655. * implementations, with the difference that they have ccw_device
  1656. * arguments.
  1657. *
  1658. * A ccw driver also contains the information that is needed for
  1659. * device matching.
  1660. */
  1661. static int
  1662. ccw_device_probe (struct device *dev)
  1663. {
  1664. struct ccw_device *cdev = to_ccwdev(dev);
  1665. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1666. int ret;
  1667. cdev->drv = cdrv; /* to let the driver call _set_online */
  1668. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1669. if (ret) {
  1670. cdev->drv = NULL;
  1671. return ret;
  1672. }
  1673. return 0;
  1674. }
  1675. static int
  1676. ccw_device_remove (struct device *dev)
  1677. {
  1678. struct ccw_device *cdev = to_ccwdev(dev);
  1679. struct ccw_driver *cdrv = cdev->drv;
  1680. int ret;
  1681. if (cdrv->remove)
  1682. cdrv->remove(cdev);
  1683. if (cdev->online) {
  1684. cdev->online = 0;
  1685. spin_lock_irq(cdev->ccwlock);
  1686. ret = ccw_device_offline(cdev);
  1687. spin_unlock_irq(cdev->ccwlock);
  1688. if (ret == 0)
  1689. wait_event(cdev->private->wait_q,
  1690. dev_fsm_final_state(cdev));
  1691. else
  1692. CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
  1693. "device 0.%x.%04x\n",
  1694. ret, cdev->private->dev_id.ssid,
  1695. cdev->private->dev_id.devno);
  1696. /* Give up reference obtained in ccw_device_set_online(). */
  1697. put_device(&cdev->dev);
  1698. }
  1699. ccw_device_set_timeout(cdev, 0);
  1700. cdev->drv = NULL;
  1701. return 0;
  1702. }
  1703. static void ccw_device_shutdown(struct device *dev)
  1704. {
  1705. struct ccw_device *cdev;
  1706. cdev = to_ccwdev(dev);
  1707. if (cdev->drv && cdev->drv->shutdown)
  1708. cdev->drv->shutdown(cdev);
  1709. disable_cmf(cdev);
  1710. }
  1711. static int ccw_device_pm_prepare(struct device *dev)
  1712. {
  1713. struct ccw_device *cdev = to_ccwdev(dev);
  1714. if (work_pending(&cdev->private->kick_work))
  1715. return -EAGAIN;
  1716. /* Fail while device is being set online/offline. */
  1717. if (atomic_read(&cdev->private->onoff))
  1718. return -EAGAIN;
  1719. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1720. return cdev->drv->prepare(cdev);
  1721. return 0;
  1722. }
  1723. static void ccw_device_pm_complete(struct device *dev)
  1724. {
  1725. struct ccw_device *cdev = to_ccwdev(dev);
  1726. if (cdev->online && cdev->drv && cdev->drv->complete)
  1727. cdev->drv->complete(cdev);
  1728. }
  1729. static int ccw_device_pm_freeze(struct device *dev)
  1730. {
  1731. struct ccw_device *cdev = to_ccwdev(dev);
  1732. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1733. int ret, cm_enabled;
  1734. /* Fail suspend while device is in transistional state. */
  1735. if (!dev_fsm_final_state(cdev))
  1736. return -EAGAIN;
  1737. if (!cdev->online)
  1738. return 0;
  1739. if (cdev->drv && cdev->drv->freeze) {
  1740. ret = cdev->drv->freeze(cdev);
  1741. if (ret)
  1742. return ret;
  1743. }
  1744. spin_lock_irq(sch->lock);
  1745. cm_enabled = cdev->private->cmb != NULL;
  1746. spin_unlock_irq(sch->lock);
  1747. if (cm_enabled) {
  1748. /* Don't have the css write on memory. */
  1749. ret = ccw_set_cmf(cdev, 0);
  1750. if (ret)
  1751. return ret;
  1752. }
  1753. /* From here on, disallow device driver I/O. */
  1754. spin_lock_irq(sch->lock);
  1755. ret = cio_disable_subchannel(sch);
  1756. spin_unlock_irq(sch->lock);
  1757. return ret;
  1758. }
  1759. static int ccw_device_pm_thaw(struct device *dev)
  1760. {
  1761. struct ccw_device *cdev = to_ccwdev(dev);
  1762. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1763. int ret, cm_enabled;
  1764. if (!cdev->online)
  1765. return 0;
  1766. spin_lock_irq(sch->lock);
  1767. /* Allow device driver I/O again. */
  1768. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1769. cm_enabled = cdev->private->cmb != NULL;
  1770. spin_unlock_irq(sch->lock);
  1771. if (ret)
  1772. return ret;
  1773. if (cm_enabled) {
  1774. ret = ccw_set_cmf(cdev, 1);
  1775. if (ret)
  1776. return ret;
  1777. }
  1778. if (cdev->drv && cdev->drv->thaw)
  1779. ret = cdev->drv->thaw(cdev);
  1780. return ret;
  1781. }
  1782. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1783. {
  1784. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1785. int ret;
  1786. if (cio_is_console(sch->schid))
  1787. goto out;
  1788. /*
  1789. * While we were sleeping, devices may have gone or become
  1790. * available again. Kick re-detection.
  1791. */
  1792. spin_lock_irq(sch->lock);
  1793. cdev->private->flags.resuming = 1;
  1794. ret = ccw_device_recognition(cdev);
  1795. spin_unlock_irq(sch->lock);
  1796. if (ret) {
  1797. CIO_MSG_EVENT(0, "Couldn't start recognition for device "
  1798. "%s (ret=%d)\n", dev_name(&cdev->dev), ret);
  1799. spin_lock_irq(sch->lock);
  1800. cdev->private->state = DEV_STATE_DISCONNECTED;
  1801. spin_unlock_irq(sch->lock);
  1802. /* notify driver after the resume cb */
  1803. goto out;
  1804. }
  1805. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1806. cdev->private->state == DEV_STATE_DISCONNECTED);
  1807. out:
  1808. cdev->private->flags.resuming = 0;
  1809. }
  1810. static int resume_handle_boxed(struct ccw_device *cdev)
  1811. {
  1812. cdev->private->state = DEV_STATE_BOXED;
  1813. if (ccw_device_notify(cdev, CIO_BOXED))
  1814. return 0;
  1815. ccw_device_schedule_sch_unregister(cdev);
  1816. return -ENODEV;
  1817. }
  1818. static int resume_handle_disc(struct ccw_device *cdev)
  1819. {
  1820. cdev->private->state = DEV_STATE_DISCONNECTED;
  1821. if (ccw_device_notify(cdev, CIO_GONE))
  1822. return 0;
  1823. ccw_device_schedule_sch_unregister(cdev);
  1824. return -ENODEV;
  1825. }
  1826. static int ccw_device_pm_restore(struct device *dev)
  1827. {
  1828. struct ccw_device *cdev = to_ccwdev(dev);
  1829. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1830. int ret = 0, cm_enabled;
  1831. __ccw_device_pm_restore(cdev);
  1832. spin_lock_irq(sch->lock);
  1833. if (cio_is_console(sch->schid)) {
  1834. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1835. spin_unlock_irq(sch->lock);
  1836. goto out_restore;
  1837. }
  1838. cdev->private->flags.donotify = 0;
  1839. /* check recognition results */
  1840. switch (cdev->private->state) {
  1841. case DEV_STATE_OFFLINE:
  1842. break;
  1843. case DEV_STATE_BOXED:
  1844. ret = resume_handle_boxed(cdev);
  1845. spin_unlock_irq(sch->lock);
  1846. if (ret)
  1847. goto out;
  1848. goto out_restore;
  1849. case DEV_STATE_DISCONNECTED:
  1850. goto out_disc_unlock;
  1851. default:
  1852. goto out_unreg_unlock;
  1853. }
  1854. /* check if the device id has changed */
  1855. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1856. CIO_MSG_EVENT(0, "resume: sch %s: failed (devno changed from "
  1857. "%04x to %04x)\n", dev_name(&sch->dev),
  1858. cdev->private->dev_id.devno,
  1859. sch->schib.pmcw.dev);
  1860. goto out_unreg_unlock;
  1861. }
  1862. /* check if the device type has changed */
  1863. if (!ccw_device_test_sense_data(cdev)) {
  1864. ccw_device_update_sense_data(cdev);
  1865. PREPARE_WORK(&cdev->private->kick_work,
  1866. ccw_device_do_unbind_bind);
  1867. queue_work(ccw_device_work, &cdev->private->kick_work);
  1868. ret = -ENODEV;
  1869. goto out_unlock;
  1870. }
  1871. if (!cdev->online) {
  1872. ret = 0;
  1873. goto out_unlock;
  1874. }
  1875. ret = ccw_device_online(cdev);
  1876. if (ret)
  1877. goto out_disc_unlock;
  1878. cm_enabled = cdev->private->cmb != NULL;
  1879. spin_unlock_irq(sch->lock);
  1880. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1881. if (cdev->private->state != DEV_STATE_ONLINE) {
  1882. spin_lock_irq(sch->lock);
  1883. goto out_disc_unlock;
  1884. }
  1885. if (cm_enabled) {
  1886. ret = ccw_set_cmf(cdev, 1);
  1887. if (ret) {
  1888. CIO_MSG_EVENT(2, "resume: cdev %s: cmf failed "
  1889. "(rc=%d)\n", dev_name(&cdev->dev), ret);
  1890. ret = 0;
  1891. }
  1892. }
  1893. out_restore:
  1894. if (cdev->online && cdev->drv && cdev->drv->restore)
  1895. ret = cdev->drv->restore(cdev);
  1896. out:
  1897. return ret;
  1898. out_disc_unlock:
  1899. ret = resume_handle_disc(cdev);
  1900. spin_unlock_irq(sch->lock);
  1901. if (ret)
  1902. return ret;
  1903. goto out_restore;
  1904. out_unreg_unlock:
  1905. ccw_device_schedule_sch_unregister(cdev);
  1906. ret = -ENODEV;
  1907. out_unlock:
  1908. spin_unlock_irq(sch->lock);
  1909. return ret;
  1910. }
  1911. static struct dev_pm_ops ccw_pm_ops = {
  1912. .prepare = ccw_device_pm_prepare,
  1913. .complete = ccw_device_pm_complete,
  1914. .freeze = ccw_device_pm_freeze,
  1915. .thaw = ccw_device_pm_thaw,
  1916. .restore = ccw_device_pm_restore,
  1917. };
  1918. struct bus_type ccw_bus_type = {
  1919. .name = "ccw",
  1920. .match = ccw_bus_match,
  1921. .uevent = ccw_uevent,
  1922. .probe = ccw_device_probe,
  1923. .remove = ccw_device_remove,
  1924. .shutdown = ccw_device_shutdown,
  1925. .pm = &ccw_pm_ops,
  1926. };
  1927. /**
  1928. * ccw_driver_register() - register a ccw driver
  1929. * @cdriver: driver to be registered
  1930. *
  1931. * This function is mainly a wrapper around driver_register().
  1932. * Returns:
  1933. * %0 on success and a negative error value on failure.
  1934. */
  1935. int ccw_driver_register(struct ccw_driver *cdriver)
  1936. {
  1937. struct device_driver *drv = &cdriver->driver;
  1938. drv->bus = &ccw_bus_type;
  1939. drv->name = cdriver->name;
  1940. drv->owner = cdriver->owner;
  1941. return driver_register(drv);
  1942. }
  1943. /**
  1944. * ccw_driver_unregister() - deregister a ccw driver
  1945. * @cdriver: driver to be deregistered
  1946. *
  1947. * This function is mainly a wrapper around driver_unregister().
  1948. */
  1949. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1950. {
  1951. driver_unregister(&cdriver->driver);
  1952. }
  1953. /* Helper func for qdio. */
  1954. struct subchannel_id
  1955. ccw_device_get_subchannel_id(struct ccw_device *cdev)
  1956. {
  1957. struct subchannel *sch;
  1958. sch = to_subchannel(cdev->dev.parent);
  1959. return sch->schid;
  1960. }
  1961. MODULE_LICENSE("GPL");
  1962. EXPORT_SYMBOL(ccw_device_set_online);
  1963. EXPORT_SYMBOL(ccw_device_set_offline);
  1964. EXPORT_SYMBOL(ccw_driver_register);
  1965. EXPORT_SYMBOL(ccw_driver_unregister);
  1966. EXPORT_SYMBOL(get_ccwdev_by_busid);
  1967. EXPORT_SYMBOL(ccw_bus_type);
  1968. EXPORT_SYMBOL(ccw_device_work);
  1969. EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);