device.c 54 KB

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