device.c 54 KB

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