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. #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. spin_lock_irq(cdev->ccwlock);
  285. sch = to_subchannel(cdev->dev.parent);
  286. cdev->online = 0;
  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_warn("%s: The device entered boxed state while being set offline\n",
  316. dev_name(&cdev->dev));
  317. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  318. pr_warn("%s: The device stopped operating while being set offline\n",
  319. 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_warn("%s: Setting the device online failed because it is boxed\n",
  377. dev_name(&cdev->dev));
  378. } else if (cdev->private->state == DEV_STATE_NOT_OPER) {
  379. pr_warn("%s: Setting the device online failed because it is not operational\n",
  380. dev_name(&cdev->dev));
  381. }
  382. /* Give up online reference since onlining failed. */
  383. put_device(&cdev->dev);
  384. return -ENODEV;
  385. }
  386. spin_unlock_irq(cdev->ccwlock);
  387. if (cdev->drv->set_online)
  388. ret = cdev->drv->set_online(cdev);
  389. if (ret)
  390. goto rollback;
  391. spin_lock_irq(cdev->ccwlock);
  392. cdev->online = 1;
  393. spin_unlock_irq(cdev->ccwlock);
  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;
  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;
  491. }
  492. if (!strncmp(buf, "force\n", count)) {
  493. force = 1;
  494. i = 1;
  495. ret = 0;
  496. } else {
  497. force = 0;
  498. ret = kstrtoul(buf, 16, &i);
  499. }
  500. if (ret)
  501. goto out;
  502. device_lock(dev);
  503. switch (i) {
  504. case 0:
  505. ret = online_store_handle_offline(cdev);
  506. break;
  507. case 1:
  508. ret = online_store_handle_online(cdev, force);
  509. break;
  510. default:
  511. ret = -EINVAL;
  512. }
  513. device_unlock(dev);
  514. out:
  515. atomic_set(&cdev->private->onoff, 0);
  516. return (ret < 0) ? ret : count;
  517. }
  518. static ssize_t
  519. available_show (struct device *dev, struct device_attribute *attr, char *buf)
  520. {
  521. struct ccw_device *cdev = to_ccwdev(dev);
  522. struct subchannel *sch;
  523. if (ccw_device_is_orphan(cdev))
  524. return sprintf(buf, "no device\n");
  525. switch (cdev->private->state) {
  526. case DEV_STATE_BOXED:
  527. return sprintf(buf, "boxed\n");
  528. case DEV_STATE_DISCONNECTED:
  529. case DEV_STATE_DISCONNECTED_SENSE_ID:
  530. case DEV_STATE_NOT_OPER:
  531. sch = to_subchannel(dev->parent);
  532. if (!sch->lpm)
  533. return sprintf(buf, "no path\n");
  534. else
  535. return sprintf(buf, "no device\n");
  536. default:
  537. /* All other states considered fine. */
  538. return sprintf(buf, "good\n");
  539. }
  540. }
  541. static ssize_t
  542. initiate_logging(struct device *dev, struct device_attribute *attr,
  543. const char *buf, size_t count)
  544. {
  545. struct subchannel *sch = to_subchannel(dev);
  546. int rc;
  547. rc = chsc_siosl(sch->schid);
  548. if (rc < 0) {
  549. pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
  550. sch->schid.ssid, sch->schid.sch_no, rc);
  551. return rc;
  552. }
  553. pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
  554. sch->schid.ssid, sch->schid.sch_no);
  555. return count;
  556. }
  557. static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
  558. char *buf)
  559. {
  560. struct subchannel *sch = to_subchannel(dev);
  561. return sprintf(buf, "%02x\n", sch->vpm);
  562. }
  563. static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
  564. static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
  565. static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
  566. static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
  567. static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
  568. static DEVICE_ATTR(online, 0644, online_show, online_store);
  569. static DEVICE_ATTR(availability, 0444, available_show, NULL);
  570. static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
  571. static DEVICE_ATTR(vpm, 0444, vpm_show, NULL);
  572. static struct attribute *io_subchannel_attrs[] = {
  573. &dev_attr_chpids.attr,
  574. &dev_attr_pimpampom.attr,
  575. &dev_attr_logging.attr,
  576. &dev_attr_vpm.attr,
  577. NULL,
  578. };
  579. static struct attribute_group io_subchannel_attr_group = {
  580. .attrs = io_subchannel_attrs,
  581. };
  582. static struct attribute * ccwdev_attrs[] = {
  583. &dev_attr_devtype.attr,
  584. &dev_attr_cutype.attr,
  585. &dev_attr_modalias.attr,
  586. &dev_attr_online.attr,
  587. &dev_attr_cmb_enable.attr,
  588. &dev_attr_availability.attr,
  589. NULL,
  590. };
  591. static struct attribute_group ccwdev_attr_group = {
  592. .attrs = ccwdev_attrs,
  593. };
  594. static const struct attribute_group *ccwdev_attr_groups[] = {
  595. &ccwdev_attr_group,
  596. NULL,
  597. };
  598. static int ccw_device_add(struct ccw_device *cdev)
  599. {
  600. struct device *dev = &cdev->dev;
  601. dev->bus = &ccw_bus_type;
  602. return device_add(dev);
  603. }
  604. static int match_dev_id(struct device *dev, void *data)
  605. {
  606. struct ccw_device *cdev = to_ccwdev(dev);
  607. struct ccw_dev_id *dev_id = data;
  608. return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
  609. }
  610. /**
  611. * get_ccwdev_by_dev_id() - obtain device from a ccw device id
  612. * @dev_id: id of the device to be searched
  613. *
  614. * This function searches all devices attached to the ccw bus for a device
  615. * matching @dev_id.
  616. * Returns:
  617. * If a device is found its reference count is increased and returned;
  618. * else %NULL is returned.
  619. */
  620. struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
  621. {
  622. struct device *dev;
  623. dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
  624. return dev ? to_ccwdev(dev) : NULL;
  625. }
  626. EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
  627. static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
  628. {
  629. int ret;
  630. if (device_is_registered(&cdev->dev)) {
  631. device_release_driver(&cdev->dev);
  632. ret = device_attach(&cdev->dev);
  633. WARN_ON(ret == -ENODEV);
  634. }
  635. }
  636. static void
  637. ccw_device_release(struct device *dev)
  638. {
  639. struct ccw_device *cdev;
  640. cdev = to_ccwdev(dev);
  641. /* Release reference of parent subchannel. */
  642. put_device(cdev->dev.parent);
  643. kfree(cdev->private);
  644. kfree(cdev);
  645. }
  646. static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
  647. {
  648. struct ccw_device *cdev;
  649. cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
  650. if (cdev) {
  651. cdev->private = kzalloc(sizeof(struct ccw_device_private),
  652. GFP_KERNEL | GFP_DMA);
  653. if (cdev->private)
  654. return cdev;
  655. }
  656. kfree(cdev);
  657. return ERR_PTR(-ENOMEM);
  658. }
  659. static void ccw_device_todo(struct work_struct *work);
  660. static int io_subchannel_initialize_dev(struct subchannel *sch,
  661. struct ccw_device *cdev)
  662. {
  663. struct ccw_device_private *priv = cdev->private;
  664. int ret;
  665. priv->cdev = cdev;
  666. priv->int_class = IRQIO_CIO;
  667. priv->state = DEV_STATE_NOT_OPER;
  668. priv->dev_id.devno = sch->schib.pmcw.dev;
  669. priv->dev_id.ssid = sch->schid.ssid;
  670. INIT_WORK(&priv->todo_work, ccw_device_todo);
  671. INIT_LIST_HEAD(&priv->cmb_list);
  672. init_waitqueue_head(&priv->wait_q);
  673. init_timer(&priv->timer);
  674. atomic_set(&priv->onoff, 0);
  675. cdev->ccwlock = sch->lock;
  676. cdev->dev.parent = &sch->dev;
  677. cdev->dev.release = ccw_device_release;
  678. cdev->dev.groups = ccwdev_attr_groups;
  679. /* Do first half of device_register. */
  680. device_initialize(&cdev->dev);
  681. ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
  682. cdev->private->dev_id.devno);
  683. if (ret)
  684. goto out_put;
  685. if (!get_device(&sch->dev)) {
  686. ret = -ENODEV;
  687. goto out_put;
  688. }
  689. priv->flags.initialized = 1;
  690. spin_lock_irq(sch->lock);
  691. sch_set_cdev(sch, cdev);
  692. spin_unlock_irq(sch->lock);
  693. return 0;
  694. out_put:
  695. /* Release reference from device_initialize(). */
  696. put_device(&cdev->dev);
  697. return ret;
  698. }
  699. static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
  700. {
  701. struct ccw_device *cdev;
  702. int ret;
  703. cdev = io_subchannel_allocate_dev(sch);
  704. if (!IS_ERR(cdev)) {
  705. ret = io_subchannel_initialize_dev(sch, cdev);
  706. if (ret)
  707. cdev = ERR_PTR(ret);
  708. }
  709. return cdev;
  710. }
  711. static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
  712. static void sch_create_and_recog_new_device(struct subchannel *sch)
  713. {
  714. struct ccw_device *cdev;
  715. /* Need to allocate a new ccw device. */
  716. cdev = io_subchannel_create_ccwdev(sch);
  717. if (IS_ERR(cdev)) {
  718. /* OK, we did everything we could... */
  719. css_sch_device_unregister(sch);
  720. return;
  721. }
  722. /* Start recognition for the new ccw device. */
  723. io_subchannel_recog(cdev, sch);
  724. }
  725. /*
  726. * Register recognized device.
  727. */
  728. static void io_subchannel_register(struct ccw_device *cdev)
  729. {
  730. struct subchannel *sch;
  731. int ret, adjust_init_count = 1;
  732. unsigned long flags;
  733. sch = to_subchannel(cdev->dev.parent);
  734. /*
  735. * Check if subchannel is still registered. It may have become
  736. * unregistered if a machine check hit us after finishing
  737. * device recognition but before the register work could be
  738. * queued.
  739. */
  740. if (!device_is_registered(&sch->dev))
  741. goto out_err;
  742. css_update_ssd_info(sch);
  743. /*
  744. * io_subchannel_register() will also be called after device
  745. * recognition has been done for a boxed device (which will already
  746. * be registered). We need to reprobe since we may now have sense id
  747. * information.
  748. */
  749. if (device_is_registered(&cdev->dev)) {
  750. if (!cdev->drv) {
  751. ret = device_reprobe(&cdev->dev);
  752. if (ret)
  753. /* We can't do much here. */
  754. CIO_MSG_EVENT(0, "device_reprobe() returned"
  755. " %d for 0.%x.%04x\n", ret,
  756. cdev->private->dev_id.ssid,
  757. cdev->private->dev_id.devno);
  758. }
  759. adjust_init_count = 0;
  760. goto out;
  761. }
  762. /*
  763. * Now we know this subchannel will stay, we can throw
  764. * our delayed uevent.
  765. */
  766. dev_set_uevent_suppress(&sch->dev, 0);
  767. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  768. /* make it known to the system */
  769. ret = ccw_device_add(cdev);
  770. if (ret) {
  771. CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
  772. cdev->private->dev_id.ssid,
  773. cdev->private->dev_id.devno, ret);
  774. spin_lock_irqsave(sch->lock, flags);
  775. sch_set_cdev(sch, NULL);
  776. spin_unlock_irqrestore(sch->lock, flags);
  777. /* Release initial device reference. */
  778. put_device(&cdev->dev);
  779. goto out_err;
  780. }
  781. out:
  782. cdev->private->flags.recog_done = 1;
  783. wake_up(&cdev->private->wait_q);
  784. out_err:
  785. if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
  786. wake_up(&ccw_device_init_wq);
  787. }
  788. static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
  789. {
  790. struct subchannel *sch;
  791. /* Get subchannel reference for local processing. */
  792. if (!get_device(cdev->dev.parent))
  793. return;
  794. sch = to_subchannel(cdev->dev.parent);
  795. css_sch_device_unregister(sch);
  796. /* Release subchannel reference for local processing. */
  797. put_device(&sch->dev);
  798. }
  799. /*
  800. * subchannel recognition done. Called from the state machine.
  801. */
  802. void
  803. io_subchannel_recog_done(struct ccw_device *cdev)
  804. {
  805. if (css_init_done == 0) {
  806. cdev->private->flags.recog_done = 1;
  807. return;
  808. }
  809. switch (cdev->private->state) {
  810. case DEV_STATE_BOXED:
  811. /* Device did not respond in time. */
  812. case DEV_STATE_NOT_OPER:
  813. cdev->private->flags.recog_done = 1;
  814. /* Remove device found not operational. */
  815. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  816. if (atomic_dec_and_test(&ccw_device_init_count))
  817. wake_up(&ccw_device_init_wq);
  818. break;
  819. case DEV_STATE_OFFLINE:
  820. /*
  821. * We can't register the device in interrupt context so
  822. * we schedule a work item.
  823. */
  824. ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
  825. break;
  826. }
  827. }
  828. static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
  829. {
  830. /* Increase counter of devices currently in recognition. */
  831. atomic_inc(&ccw_device_init_count);
  832. /* Start async. device sensing. */
  833. spin_lock_irq(sch->lock);
  834. ccw_device_recognition(cdev);
  835. spin_unlock_irq(sch->lock);
  836. }
  837. static int ccw_device_move_to_sch(struct ccw_device *cdev,
  838. struct subchannel *sch)
  839. {
  840. struct subchannel *old_sch;
  841. int rc, old_enabled = 0;
  842. old_sch = to_subchannel(cdev->dev.parent);
  843. /* Obtain child reference for new parent. */
  844. if (!get_device(&sch->dev))
  845. return -ENODEV;
  846. if (!sch_is_pseudo_sch(old_sch)) {
  847. spin_lock_irq(old_sch->lock);
  848. old_enabled = old_sch->schib.pmcw.ena;
  849. rc = 0;
  850. if (old_enabled)
  851. rc = cio_disable_subchannel(old_sch);
  852. spin_unlock_irq(old_sch->lock);
  853. if (rc == -EBUSY) {
  854. /* Release child reference for new parent. */
  855. put_device(&sch->dev);
  856. return rc;
  857. }
  858. }
  859. mutex_lock(&sch->reg_mutex);
  860. rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
  861. mutex_unlock(&sch->reg_mutex);
  862. if (rc) {
  863. CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
  864. cdev->private->dev_id.ssid,
  865. cdev->private->dev_id.devno, sch->schid.ssid,
  866. sch->schib.pmcw.dev, rc);
  867. if (old_enabled) {
  868. /* Try to reenable the old subchannel. */
  869. spin_lock_irq(old_sch->lock);
  870. cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
  871. spin_unlock_irq(old_sch->lock);
  872. }
  873. /* Release child reference for new parent. */
  874. put_device(&sch->dev);
  875. return rc;
  876. }
  877. /* Clean up old subchannel. */
  878. if (!sch_is_pseudo_sch(old_sch)) {
  879. spin_lock_irq(old_sch->lock);
  880. sch_set_cdev(old_sch, NULL);
  881. spin_unlock_irq(old_sch->lock);
  882. css_schedule_eval(old_sch->schid);
  883. }
  884. /* Release child reference for old parent. */
  885. put_device(&old_sch->dev);
  886. /* Initialize new subchannel. */
  887. spin_lock_irq(sch->lock);
  888. cdev->ccwlock = sch->lock;
  889. if (!sch_is_pseudo_sch(sch))
  890. sch_set_cdev(sch, cdev);
  891. spin_unlock_irq(sch->lock);
  892. if (!sch_is_pseudo_sch(sch))
  893. css_update_ssd_info(sch);
  894. return 0;
  895. }
  896. static int ccw_device_move_to_orph(struct ccw_device *cdev)
  897. {
  898. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  899. struct channel_subsystem *css = to_css(sch->dev.parent);
  900. return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
  901. }
  902. static void io_subchannel_irq(struct subchannel *sch)
  903. {
  904. struct ccw_device *cdev;
  905. cdev = sch_get_cdev(sch);
  906. CIO_TRACE_EVENT(6, "IRQ");
  907. CIO_TRACE_EVENT(6, dev_name(&sch->dev));
  908. if (cdev)
  909. dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
  910. else
  911. inc_irq_stat(IRQIO_CIO);
  912. }
  913. void io_subchannel_init_config(struct subchannel *sch)
  914. {
  915. memset(&sch->config, 0, sizeof(sch->config));
  916. sch->config.csense = 1;
  917. }
  918. static void io_subchannel_init_fields(struct subchannel *sch)
  919. {
  920. if (cio_is_console(sch->schid))
  921. sch->opm = 0xff;
  922. else
  923. sch->opm = chp_get_sch_opm(sch);
  924. sch->lpm = sch->schib.pmcw.pam & sch->opm;
  925. sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
  926. CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
  927. " - PIM = %02X, PAM = %02X, POM = %02X\n",
  928. sch->schib.pmcw.dev, sch->schid.ssid,
  929. sch->schid.sch_no, sch->schib.pmcw.pim,
  930. sch->schib.pmcw.pam, sch->schib.pmcw.pom);
  931. io_subchannel_init_config(sch);
  932. }
  933. /*
  934. * Note: We always return 0 so that we bind to the device even on error.
  935. * This is needed so that our remove function is called on unregister.
  936. */
  937. static int io_subchannel_probe(struct subchannel *sch)
  938. {
  939. struct io_subchannel_private *io_priv;
  940. struct ccw_device *cdev;
  941. int rc;
  942. if (cio_is_console(sch->schid)) {
  943. rc = sysfs_create_group(&sch->dev.kobj,
  944. &io_subchannel_attr_group);
  945. if (rc)
  946. CIO_MSG_EVENT(0, "Failed to create io subchannel "
  947. "attributes for subchannel "
  948. "0.%x.%04x (rc=%d)\n",
  949. sch->schid.ssid, sch->schid.sch_no, rc);
  950. /*
  951. * The console subchannel already has an associated ccw_device.
  952. * Throw the delayed uevent for the subchannel, register
  953. * the ccw_device and exit.
  954. */
  955. dev_set_uevent_suppress(&sch->dev, 0);
  956. kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
  957. cdev = sch_get_cdev(sch);
  958. rc = ccw_device_add(cdev);
  959. if (rc) {
  960. /* Release online reference. */
  961. put_device(&cdev->dev);
  962. goto out_schedule;
  963. }
  964. if (atomic_dec_and_test(&ccw_device_init_count))
  965. wake_up(&ccw_device_init_wq);
  966. return 0;
  967. }
  968. io_subchannel_init_fields(sch);
  969. rc = cio_commit_config(sch);
  970. if (rc)
  971. goto out_schedule;
  972. rc = sysfs_create_group(&sch->dev.kobj,
  973. &io_subchannel_attr_group);
  974. if (rc)
  975. goto out_schedule;
  976. /* Allocate I/O subchannel private data. */
  977. io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
  978. if (!io_priv)
  979. goto out_schedule;
  980. set_io_private(sch, io_priv);
  981. css_schedule_eval(sch->schid);
  982. return 0;
  983. out_schedule:
  984. spin_lock_irq(sch->lock);
  985. css_sched_sch_todo(sch, SCH_TODO_UNREG);
  986. spin_unlock_irq(sch->lock);
  987. return 0;
  988. }
  989. static int
  990. io_subchannel_remove (struct subchannel *sch)
  991. {
  992. struct io_subchannel_private *io_priv = to_io_private(sch);
  993. struct ccw_device *cdev;
  994. cdev = sch_get_cdev(sch);
  995. if (!cdev)
  996. goto out_free;
  997. io_subchannel_quiesce(sch);
  998. /* Set ccw device to not operational and drop reference. */
  999. spin_lock_irq(cdev->ccwlock);
  1000. sch_set_cdev(sch, NULL);
  1001. set_io_private(sch, NULL);
  1002. cdev->private->state = DEV_STATE_NOT_OPER;
  1003. spin_unlock_irq(cdev->ccwlock);
  1004. ccw_device_unregister(cdev);
  1005. out_free:
  1006. kfree(io_priv);
  1007. sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
  1008. return 0;
  1009. }
  1010. static void io_subchannel_verify(struct subchannel *sch)
  1011. {
  1012. struct ccw_device *cdev;
  1013. cdev = sch_get_cdev(sch);
  1014. if (cdev)
  1015. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1016. }
  1017. static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
  1018. {
  1019. struct ccw_device *cdev;
  1020. cdev = sch_get_cdev(sch);
  1021. if (!cdev)
  1022. return;
  1023. if (cio_update_schib(sch))
  1024. goto err;
  1025. /* Check for I/O on path. */
  1026. if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
  1027. goto out;
  1028. if (cdev->private->state == DEV_STATE_ONLINE) {
  1029. ccw_device_kill_io(cdev);
  1030. goto out;
  1031. }
  1032. if (cio_clear(sch))
  1033. goto err;
  1034. out:
  1035. /* Trigger path verification. */
  1036. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1037. return;
  1038. err:
  1039. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  1040. }
  1041. static int io_subchannel_chp_event(struct subchannel *sch,
  1042. struct chp_link *link, int event)
  1043. {
  1044. struct ccw_device *cdev = sch_get_cdev(sch);
  1045. int mask;
  1046. mask = chp_ssd_get_mask(&sch->ssd_info, link);
  1047. if (!mask)
  1048. return 0;
  1049. switch (event) {
  1050. case CHP_VARY_OFF:
  1051. sch->opm &= ~mask;
  1052. sch->lpm &= ~mask;
  1053. if (cdev)
  1054. cdev->private->path_gone_mask |= mask;
  1055. io_subchannel_terminate_path(sch, mask);
  1056. break;
  1057. case CHP_VARY_ON:
  1058. sch->opm |= mask;
  1059. sch->lpm |= mask;
  1060. if (cdev)
  1061. cdev->private->path_new_mask |= mask;
  1062. io_subchannel_verify(sch);
  1063. break;
  1064. case CHP_OFFLINE:
  1065. if (cio_update_schib(sch))
  1066. return -ENODEV;
  1067. if (cdev)
  1068. cdev->private->path_gone_mask |= mask;
  1069. io_subchannel_terminate_path(sch, mask);
  1070. break;
  1071. case CHP_ONLINE:
  1072. if (cio_update_schib(sch))
  1073. return -ENODEV;
  1074. sch->lpm |= mask & sch->opm;
  1075. if (cdev)
  1076. cdev->private->path_new_mask |= mask;
  1077. io_subchannel_verify(sch);
  1078. break;
  1079. }
  1080. return 0;
  1081. }
  1082. static void io_subchannel_quiesce(struct subchannel *sch)
  1083. {
  1084. struct ccw_device *cdev;
  1085. int ret;
  1086. spin_lock_irq(sch->lock);
  1087. cdev = sch_get_cdev(sch);
  1088. if (cio_is_console(sch->schid))
  1089. goto out_unlock;
  1090. if (!sch->schib.pmcw.ena)
  1091. goto out_unlock;
  1092. ret = cio_disable_subchannel(sch);
  1093. if (ret != -EBUSY)
  1094. goto out_unlock;
  1095. if (cdev->handler)
  1096. cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
  1097. while (ret == -EBUSY) {
  1098. cdev->private->state = DEV_STATE_QUIESCE;
  1099. cdev->private->iretry = 255;
  1100. ret = ccw_device_cancel_halt_clear(cdev);
  1101. if (ret == -EBUSY) {
  1102. ccw_device_set_timeout(cdev, HZ/10);
  1103. spin_unlock_irq(sch->lock);
  1104. wait_event(cdev->private->wait_q,
  1105. cdev->private->state != DEV_STATE_QUIESCE);
  1106. spin_lock_irq(sch->lock);
  1107. }
  1108. ret = cio_disable_subchannel(sch);
  1109. }
  1110. out_unlock:
  1111. spin_unlock_irq(sch->lock);
  1112. }
  1113. static void io_subchannel_shutdown(struct subchannel *sch)
  1114. {
  1115. io_subchannel_quiesce(sch);
  1116. }
  1117. static int device_is_disconnected(struct ccw_device *cdev)
  1118. {
  1119. if (!cdev)
  1120. return 0;
  1121. return (cdev->private->state == DEV_STATE_DISCONNECTED ||
  1122. cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
  1123. }
  1124. static int recovery_check(struct device *dev, void *data)
  1125. {
  1126. struct ccw_device *cdev = to_ccwdev(dev);
  1127. int *redo = data;
  1128. spin_lock_irq(cdev->ccwlock);
  1129. switch (cdev->private->state) {
  1130. case DEV_STATE_DISCONNECTED:
  1131. CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
  1132. cdev->private->dev_id.ssid,
  1133. cdev->private->dev_id.devno);
  1134. dev_fsm_event(cdev, DEV_EVENT_VERIFY);
  1135. *redo = 1;
  1136. break;
  1137. case DEV_STATE_DISCONNECTED_SENSE_ID:
  1138. *redo = 1;
  1139. break;
  1140. }
  1141. spin_unlock_irq(cdev->ccwlock);
  1142. return 0;
  1143. }
  1144. static void recovery_work_func(struct work_struct *unused)
  1145. {
  1146. int redo = 0;
  1147. bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
  1148. if (redo) {
  1149. spin_lock_irq(&recovery_lock);
  1150. if (!timer_pending(&recovery_timer)) {
  1151. if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
  1152. recovery_phase++;
  1153. mod_timer(&recovery_timer, jiffies +
  1154. recovery_delay[recovery_phase] * HZ);
  1155. }
  1156. spin_unlock_irq(&recovery_lock);
  1157. } else
  1158. CIO_MSG_EVENT(4, "recovery: end\n");
  1159. }
  1160. static DECLARE_WORK(recovery_work, recovery_work_func);
  1161. static void recovery_func(unsigned long data)
  1162. {
  1163. /*
  1164. * We can't do our recovery in softirq context and it's not
  1165. * performance critical, so we schedule it.
  1166. */
  1167. schedule_work(&recovery_work);
  1168. }
  1169. static void ccw_device_schedule_recovery(void)
  1170. {
  1171. unsigned long flags;
  1172. CIO_MSG_EVENT(4, "recovery: schedule\n");
  1173. spin_lock_irqsave(&recovery_lock, flags);
  1174. if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
  1175. recovery_phase = 0;
  1176. mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
  1177. }
  1178. spin_unlock_irqrestore(&recovery_lock, flags);
  1179. }
  1180. static int purge_fn(struct device *dev, void *data)
  1181. {
  1182. struct ccw_device *cdev = to_ccwdev(dev);
  1183. struct ccw_dev_id *id = &cdev->private->dev_id;
  1184. spin_lock_irq(cdev->ccwlock);
  1185. if (is_blacklisted(id->ssid, id->devno) &&
  1186. (cdev->private->state == DEV_STATE_OFFLINE) &&
  1187. (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
  1188. CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
  1189. id->devno);
  1190. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1191. atomic_set(&cdev->private->onoff, 0);
  1192. }
  1193. spin_unlock_irq(cdev->ccwlock);
  1194. /* Abort loop in case of pending signal. */
  1195. if (signal_pending(current))
  1196. return -EINTR;
  1197. return 0;
  1198. }
  1199. /**
  1200. * ccw_purge_blacklisted - purge unused, blacklisted devices
  1201. *
  1202. * Unregister all ccw devices that are offline and on the blacklist.
  1203. */
  1204. int ccw_purge_blacklisted(void)
  1205. {
  1206. CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
  1207. bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
  1208. return 0;
  1209. }
  1210. void ccw_device_set_disconnected(struct ccw_device *cdev)
  1211. {
  1212. if (!cdev)
  1213. return;
  1214. ccw_device_set_timeout(cdev, 0);
  1215. cdev->private->flags.fake_irb = 0;
  1216. cdev->private->state = DEV_STATE_DISCONNECTED;
  1217. if (cdev->online)
  1218. ccw_device_schedule_recovery();
  1219. }
  1220. void ccw_device_set_notoper(struct ccw_device *cdev)
  1221. {
  1222. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1223. CIO_TRACE_EVENT(2, "notoper");
  1224. CIO_TRACE_EVENT(2, dev_name(&sch->dev));
  1225. ccw_device_set_timeout(cdev, 0);
  1226. cio_disable_subchannel(sch);
  1227. cdev->private->state = DEV_STATE_NOT_OPER;
  1228. }
  1229. enum io_sch_action {
  1230. IO_SCH_UNREG,
  1231. IO_SCH_ORPH_UNREG,
  1232. IO_SCH_ATTACH,
  1233. IO_SCH_UNREG_ATTACH,
  1234. IO_SCH_ORPH_ATTACH,
  1235. IO_SCH_REPROBE,
  1236. IO_SCH_VERIFY,
  1237. IO_SCH_DISC,
  1238. IO_SCH_NOP,
  1239. };
  1240. static enum io_sch_action sch_get_action(struct subchannel *sch)
  1241. {
  1242. struct ccw_device *cdev;
  1243. cdev = sch_get_cdev(sch);
  1244. if (cio_update_schib(sch)) {
  1245. /* Not operational. */
  1246. if (!cdev)
  1247. return IO_SCH_UNREG;
  1248. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1249. return IO_SCH_UNREG;
  1250. return IO_SCH_ORPH_UNREG;
  1251. }
  1252. /* Operational. */
  1253. if (!cdev)
  1254. return IO_SCH_ATTACH;
  1255. if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
  1256. if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
  1257. return IO_SCH_UNREG_ATTACH;
  1258. return IO_SCH_ORPH_ATTACH;
  1259. }
  1260. if ((sch->schib.pmcw.pam & sch->opm) == 0) {
  1261. if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
  1262. return IO_SCH_UNREG;
  1263. return IO_SCH_DISC;
  1264. }
  1265. if (device_is_disconnected(cdev))
  1266. return IO_SCH_REPROBE;
  1267. if (cdev->online && !cdev->private->flags.resuming)
  1268. return IO_SCH_VERIFY;
  1269. if (cdev->private->state == DEV_STATE_NOT_OPER)
  1270. return IO_SCH_UNREG_ATTACH;
  1271. return IO_SCH_NOP;
  1272. }
  1273. /**
  1274. * io_subchannel_sch_event - process subchannel event
  1275. * @sch: subchannel
  1276. * @process: non-zero if function is called in process context
  1277. *
  1278. * An unspecified event occurred for this subchannel. Adjust data according
  1279. * to the current operational state of the subchannel and device. Return
  1280. * zero when the event has been handled sufficiently or -EAGAIN when this
  1281. * function should be called again in process context.
  1282. */
  1283. static int io_subchannel_sch_event(struct subchannel *sch, int process)
  1284. {
  1285. unsigned long flags;
  1286. struct ccw_device *cdev;
  1287. struct ccw_dev_id dev_id;
  1288. enum io_sch_action action;
  1289. int rc = -EAGAIN;
  1290. spin_lock_irqsave(sch->lock, flags);
  1291. if (!device_is_registered(&sch->dev))
  1292. goto out_unlock;
  1293. if (work_pending(&sch->todo_work))
  1294. goto out_unlock;
  1295. cdev = sch_get_cdev(sch);
  1296. if (cdev && work_pending(&cdev->private->todo_work))
  1297. goto out_unlock;
  1298. action = sch_get_action(sch);
  1299. CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
  1300. sch->schid.ssid, sch->schid.sch_no, process,
  1301. action);
  1302. /* Perform immediate actions while holding the lock. */
  1303. switch (action) {
  1304. case IO_SCH_REPROBE:
  1305. /* Trigger device recognition. */
  1306. ccw_device_trigger_reprobe(cdev);
  1307. rc = 0;
  1308. goto out_unlock;
  1309. case IO_SCH_VERIFY:
  1310. /* Trigger path verification. */
  1311. io_subchannel_verify(sch);
  1312. rc = 0;
  1313. goto out_unlock;
  1314. case IO_SCH_DISC:
  1315. ccw_device_set_disconnected(cdev);
  1316. rc = 0;
  1317. goto out_unlock;
  1318. case IO_SCH_ORPH_UNREG:
  1319. case IO_SCH_ORPH_ATTACH:
  1320. ccw_device_set_disconnected(cdev);
  1321. break;
  1322. case IO_SCH_UNREG_ATTACH:
  1323. case IO_SCH_UNREG:
  1324. if (!cdev)
  1325. break;
  1326. if (cdev->private->state == DEV_STATE_SENSE_ID) {
  1327. /*
  1328. * Note: delayed work triggered by this event
  1329. * and repeated calls to sch_event are synchronized
  1330. * by the above check for work_pending(cdev).
  1331. */
  1332. dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
  1333. } else
  1334. ccw_device_set_notoper(cdev);
  1335. break;
  1336. case IO_SCH_NOP:
  1337. rc = 0;
  1338. goto out_unlock;
  1339. default:
  1340. break;
  1341. }
  1342. spin_unlock_irqrestore(sch->lock, flags);
  1343. /* All other actions require process context. */
  1344. if (!process)
  1345. goto out;
  1346. /* Handle attached ccw device. */
  1347. switch (action) {
  1348. case IO_SCH_ORPH_UNREG:
  1349. case IO_SCH_ORPH_ATTACH:
  1350. /* Move ccw device to orphanage. */
  1351. rc = ccw_device_move_to_orph(cdev);
  1352. if (rc)
  1353. goto out;
  1354. break;
  1355. case IO_SCH_UNREG_ATTACH:
  1356. spin_lock_irqsave(sch->lock, flags);
  1357. if (cdev->private->flags.resuming) {
  1358. /* Device will be handled later. */
  1359. rc = 0;
  1360. goto out_unlock;
  1361. }
  1362. sch_set_cdev(sch, NULL);
  1363. spin_unlock_irqrestore(sch->lock, flags);
  1364. /* Unregister ccw device. */
  1365. ccw_device_unregister(cdev);
  1366. break;
  1367. default:
  1368. break;
  1369. }
  1370. /* Handle subchannel. */
  1371. switch (action) {
  1372. case IO_SCH_ORPH_UNREG:
  1373. case IO_SCH_UNREG:
  1374. if (!cdev || !cdev->private->flags.resuming)
  1375. css_sch_device_unregister(sch);
  1376. break;
  1377. case IO_SCH_ORPH_ATTACH:
  1378. case IO_SCH_UNREG_ATTACH:
  1379. case IO_SCH_ATTACH:
  1380. dev_id.ssid = sch->schid.ssid;
  1381. dev_id.devno = sch->schib.pmcw.dev;
  1382. cdev = get_ccwdev_by_dev_id(&dev_id);
  1383. if (!cdev) {
  1384. sch_create_and_recog_new_device(sch);
  1385. break;
  1386. }
  1387. rc = ccw_device_move_to_sch(cdev, sch);
  1388. if (rc) {
  1389. /* Release reference from get_ccwdev_by_dev_id() */
  1390. put_device(&cdev->dev);
  1391. goto out;
  1392. }
  1393. spin_lock_irqsave(sch->lock, flags);
  1394. ccw_device_trigger_reprobe(cdev);
  1395. spin_unlock_irqrestore(sch->lock, flags);
  1396. /* Release reference from get_ccwdev_by_dev_id() */
  1397. put_device(&cdev->dev);
  1398. break;
  1399. default:
  1400. break;
  1401. }
  1402. return 0;
  1403. out_unlock:
  1404. spin_unlock_irqrestore(sch->lock, flags);
  1405. out:
  1406. return rc;
  1407. }
  1408. static void ccw_device_set_int_class(struct ccw_device *cdev)
  1409. {
  1410. struct ccw_driver *cdrv = cdev->drv;
  1411. /* Note: we interpret class 0 in this context as an uninitialized
  1412. * field since it translates to a non-I/O interrupt class. */
  1413. if (cdrv->int_class != 0)
  1414. cdev->private->int_class = cdrv->int_class;
  1415. else
  1416. cdev->private->int_class = IRQIO_CIO;
  1417. }
  1418. #ifdef CONFIG_CCW_CONSOLE
  1419. int __init ccw_device_enable_console(struct ccw_device *cdev)
  1420. {
  1421. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1422. int rc;
  1423. if (!cdev->drv || !cdev->handler)
  1424. return -EINVAL;
  1425. io_subchannel_init_fields(sch);
  1426. rc = cio_commit_config(sch);
  1427. if (rc)
  1428. return rc;
  1429. sch->driver = &io_subchannel_driver;
  1430. io_subchannel_recog(cdev, sch);
  1431. /* Now wait for the async. recognition to come to an end. */
  1432. spin_lock_irq(cdev->ccwlock);
  1433. while (!dev_fsm_final_state(cdev))
  1434. ccw_device_wait_idle(cdev);
  1435. /* Hold on to an extra reference while device is online. */
  1436. get_device(&cdev->dev);
  1437. rc = ccw_device_online(cdev);
  1438. if (rc)
  1439. goto out_unlock;
  1440. while (!dev_fsm_final_state(cdev))
  1441. ccw_device_wait_idle(cdev);
  1442. if (cdev->private->state == DEV_STATE_ONLINE)
  1443. cdev->online = 1;
  1444. else
  1445. rc = -EIO;
  1446. out_unlock:
  1447. spin_unlock_irq(cdev->ccwlock);
  1448. if (rc) /* Give up online reference since onlining failed. */
  1449. put_device(&cdev->dev);
  1450. return rc;
  1451. }
  1452. struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
  1453. {
  1454. struct io_subchannel_private *io_priv;
  1455. struct ccw_device *cdev;
  1456. struct subchannel *sch;
  1457. sch = cio_probe_console();
  1458. if (IS_ERR(sch))
  1459. return ERR_CAST(sch);
  1460. io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
  1461. if (!io_priv) {
  1462. put_device(&sch->dev);
  1463. return ERR_PTR(-ENOMEM);
  1464. }
  1465. set_io_private(sch, io_priv);
  1466. cdev = io_subchannel_create_ccwdev(sch);
  1467. if (IS_ERR(cdev)) {
  1468. put_device(&sch->dev);
  1469. kfree(io_priv);
  1470. return cdev;
  1471. }
  1472. cdev->drv = drv;
  1473. ccw_device_set_int_class(cdev);
  1474. return cdev;
  1475. }
  1476. void __init ccw_device_destroy_console(struct ccw_device *cdev)
  1477. {
  1478. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1479. struct io_subchannel_private *io_priv = to_io_private(sch);
  1480. set_io_private(sch, NULL);
  1481. put_device(&sch->dev);
  1482. put_device(&cdev->dev);
  1483. kfree(io_priv);
  1484. }
  1485. /**
  1486. * ccw_device_wait_idle() - busy wait for device to become idle
  1487. * @cdev: ccw device
  1488. *
  1489. * Poll until activity control is zero, that is, no function or data
  1490. * transfer is pending/active.
  1491. * Called with device lock being held.
  1492. */
  1493. void ccw_device_wait_idle(struct ccw_device *cdev)
  1494. {
  1495. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1496. while (1) {
  1497. cio_tsch(sch);
  1498. if (sch->schib.scsw.cmd.actl == 0)
  1499. break;
  1500. udelay_simple(100);
  1501. }
  1502. }
  1503. static int ccw_device_pm_restore(struct device *dev);
  1504. int ccw_device_force_console(struct ccw_device *cdev)
  1505. {
  1506. return ccw_device_pm_restore(&cdev->dev);
  1507. }
  1508. EXPORT_SYMBOL_GPL(ccw_device_force_console);
  1509. #endif
  1510. /*
  1511. * get ccw_device matching the busid, but only if owned by cdrv
  1512. */
  1513. static int
  1514. __ccwdev_check_busid(struct device *dev, void *id)
  1515. {
  1516. char *bus_id;
  1517. bus_id = id;
  1518. return (strcmp(bus_id, dev_name(dev)) == 0);
  1519. }
  1520. /**
  1521. * get_ccwdev_by_busid() - obtain device from a bus id
  1522. * @cdrv: driver the device is owned by
  1523. * @bus_id: bus id of the device to be searched
  1524. *
  1525. * This function searches all devices owned by @cdrv for a device with a bus
  1526. * id matching @bus_id.
  1527. * Returns:
  1528. * If a match is found, its reference count of the found device is increased
  1529. * and it is returned; else %NULL is returned.
  1530. */
  1531. struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
  1532. const char *bus_id)
  1533. {
  1534. struct device *dev;
  1535. dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
  1536. __ccwdev_check_busid);
  1537. return dev ? to_ccwdev(dev) : NULL;
  1538. }
  1539. /************************** device driver handling ************************/
  1540. /* This is the implementation of the ccw_driver class. The probe, remove
  1541. * and release methods are initially very similar to the device_driver
  1542. * implementations, with the difference that they have ccw_device
  1543. * arguments.
  1544. *
  1545. * A ccw driver also contains the information that is needed for
  1546. * device matching.
  1547. */
  1548. static int
  1549. ccw_device_probe (struct device *dev)
  1550. {
  1551. struct ccw_device *cdev = to_ccwdev(dev);
  1552. struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
  1553. int ret;
  1554. cdev->drv = cdrv; /* to let the driver call _set_online */
  1555. ccw_device_set_int_class(cdev);
  1556. ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
  1557. if (ret) {
  1558. cdev->drv = NULL;
  1559. cdev->private->int_class = IRQIO_CIO;
  1560. return ret;
  1561. }
  1562. return 0;
  1563. }
  1564. static int 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. spin_lock_irq(cdev->ccwlock);
  1572. if (cdev->online) {
  1573. cdev->online = 0;
  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. spin_lock_irq(cdev->ccwlock);
  1587. }
  1588. ccw_device_set_timeout(cdev, 0);
  1589. cdev->drv = NULL;
  1590. cdev->private->int_class = IRQIO_CIO;
  1591. spin_unlock_irq(cdev->ccwlock);
  1592. __disable_cmf(cdev);
  1593. return 0;
  1594. }
  1595. static void ccw_device_shutdown(struct device *dev)
  1596. {
  1597. struct ccw_device *cdev;
  1598. cdev = to_ccwdev(dev);
  1599. if (cdev->drv && cdev->drv->shutdown)
  1600. cdev->drv->shutdown(cdev);
  1601. __disable_cmf(cdev);
  1602. }
  1603. static int ccw_device_pm_prepare(struct device *dev)
  1604. {
  1605. struct ccw_device *cdev = to_ccwdev(dev);
  1606. if (work_pending(&cdev->private->todo_work))
  1607. return -EAGAIN;
  1608. /* Fail while device is being set online/offline. */
  1609. if (atomic_read(&cdev->private->onoff))
  1610. return -EAGAIN;
  1611. if (cdev->online && cdev->drv && cdev->drv->prepare)
  1612. return cdev->drv->prepare(cdev);
  1613. return 0;
  1614. }
  1615. static void ccw_device_pm_complete(struct device *dev)
  1616. {
  1617. struct ccw_device *cdev = to_ccwdev(dev);
  1618. if (cdev->online && cdev->drv && cdev->drv->complete)
  1619. cdev->drv->complete(cdev);
  1620. }
  1621. static int ccw_device_pm_freeze(struct device *dev)
  1622. {
  1623. struct ccw_device *cdev = to_ccwdev(dev);
  1624. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1625. int ret, cm_enabled;
  1626. /* Fail suspend while device is in transistional state. */
  1627. if (!dev_fsm_final_state(cdev))
  1628. return -EAGAIN;
  1629. if (!cdev->online)
  1630. return 0;
  1631. if (cdev->drv && cdev->drv->freeze) {
  1632. ret = cdev->drv->freeze(cdev);
  1633. if (ret)
  1634. return ret;
  1635. }
  1636. spin_lock_irq(sch->lock);
  1637. cm_enabled = cdev->private->cmb != NULL;
  1638. spin_unlock_irq(sch->lock);
  1639. if (cm_enabled) {
  1640. /* Don't have the css write on memory. */
  1641. ret = ccw_set_cmf(cdev, 0);
  1642. if (ret)
  1643. return ret;
  1644. }
  1645. /* From here on, disallow device driver I/O. */
  1646. spin_lock_irq(sch->lock);
  1647. ret = cio_disable_subchannel(sch);
  1648. spin_unlock_irq(sch->lock);
  1649. return ret;
  1650. }
  1651. static int ccw_device_pm_thaw(struct device *dev)
  1652. {
  1653. struct ccw_device *cdev = to_ccwdev(dev);
  1654. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1655. int ret, cm_enabled;
  1656. if (!cdev->online)
  1657. return 0;
  1658. spin_lock_irq(sch->lock);
  1659. /* Allow device driver I/O again. */
  1660. ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1661. cm_enabled = cdev->private->cmb != NULL;
  1662. spin_unlock_irq(sch->lock);
  1663. if (ret)
  1664. return ret;
  1665. if (cm_enabled) {
  1666. ret = ccw_set_cmf(cdev, 1);
  1667. if (ret)
  1668. return ret;
  1669. }
  1670. if (cdev->drv && cdev->drv->thaw)
  1671. ret = cdev->drv->thaw(cdev);
  1672. return ret;
  1673. }
  1674. static void __ccw_device_pm_restore(struct ccw_device *cdev)
  1675. {
  1676. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1677. spin_lock_irq(sch->lock);
  1678. if (cio_is_console(sch->schid)) {
  1679. cio_enable_subchannel(sch, (u32)(addr_t)sch);
  1680. goto out_unlock;
  1681. }
  1682. /*
  1683. * While we were sleeping, devices may have gone or become
  1684. * available again. Kick re-detection.
  1685. */
  1686. cdev->private->flags.resuming = 1;
  1687. cdev->private->path_new_mask = LPM_ANYPATH;
  1688. css_sched_sch_todo(sch, SCH_TODO_EVAL);
  1689. spin_unlock_irq(sch->lock);
  1690. css_wait_for_slow_path();
  1691. /* cdev may have been moved to a different subchannel. */
  1692. sch = to_subchannel(cdev->dev.parent);
  1693. spin_lock_irq(sch->lock);
  1694. if (cdev->private->state != DEV_STATE_ONLINE &&
  1695. cdev->private->state != DEV_STATE_OFFLINE)
  1696. goto out_unlock;
  1697. ccw_device_recognition(cdev);
  1698. spin_unlock_irq(sch->lock);
  1699. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
  1700. cdev->private->state == DEV_STATE_DISCONNECTED);
  1701. spin_lock_irq(sch->lock);
  1702. out_unlock:
  1703. cdev->private->flags.resuming = 0;
  1704. spin_unlock_irq(sch->lock);
  1705. }
  1706. static int resume_handle_boxed(struct ccw_device *cdev)
  1707. {
  1708. cdev->private->state = DEV_STATE_BOXED;
  1709. if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
  1710. return 0;
  1711. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1712. return -ENODEV;
  1713. }
  1714. static int resume_handle_disc(struct ccw_device *cdev)
  1715. {
  1716. cdev->private->state = DEV_STATE_DISCONNECTED;
  1717. if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
  1718. return 0;
  1719. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1720. return -ENODEV;
  1721. }
  1722. static int ccw_device_pm_restore(struct device *dev)
  1723. {
  1724. struct ccw_device *cdev = to_ccwdev(dev);
  1725. struct subchannel *sch;
  1726. int ret = 0;
  1727. __ccw_device_pm_restore(cdev);
  1728. sch = to_subchannel(cdev->dev.parent);
  1729. spin_lock_irq(sch->lock);
  1730. if (cio_is_console(sch->schid))
  1731. goto out_restore;
  1732. /* check recognition results */
  1733. switch (cdev->private->state) {
  1734. case DEV_STATE_OFFLINE:
  1735. case DEV_STATE_ONLINE:
  1736. cdev->private->flags.donotify = 0;
  1737. break;
  1738. case DEV_STATE_BOXED:
  1739. ret = resume_handle_boxed(cdev);
  1740. if (ret)
  1741. goto out_unlock;
  1742. goto out_restore;
  1743. default:
  1744. ret = resume_handle_disc(cdev);
  1745. if (ret)
  1746. goto out_unlock;
  1747. goto out_restore;
  1748. }
  1749. /* check if the device type has changed */
  1750. if (!ccw_device_test_sense_data(cdev)) {
  1751. ccw_device_update_sense_data(cdev);
  1752. ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
  1753. ret = -ENODEV;
  1754. goto out_unlock;
  1755. }
  1756. if (!cdev->online)
  1757. goto out_unlock;
  1758. if (ccw_device_online(cdev)) {
  1759. ret = resume_handle_disc(cdev);
  1760. if (ret)
  1761. goto out_unlock;
  1762. goto out_restore;
  1763. }
  1764. spin_unlock_irq(sch->lock);
  1765. wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
  1766. spin_lock_irq(sch->lock);
  1767. if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
  1768. ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
  1769. ret = -ENODEV;
  1770. goto out_unlock;
  1771. }
  1772. /* reenable cmf, if needed */
  1773. if (cdev->private->cmb) {
  1774. spin_unlock_irq(sch->lock);
  1775. ret = ccw_set_cmf(cdev, 1);
  1776. spin_lock_irq(sch->lock);
  1777. if (ret) {
  1778. CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
  1779. "(rc=%d)\n", cdev->private->dev_id.ssid,
  1780. cdev->private->dev_id.devno, ret);
  1781. ret = 0;
  1782. }
  1783. }
  1784. out_restore:
  1785. spin_unlock_irq(sch->lock);
  1786. if (cdev->online && cdev->drv && cdev->drv->restore)
  1787. ret = cdev->drv->restore(cdev);
  1788. return ret;
  1789. out_unlock:
  1790. spin_unlock_irq(sch->lock);
  1791. return ret;
  1792. }
  1793. static const struct dev_pm_ops ccw_pm_ops = {
  1794. .prepare = ccw_device_pm_prepare,
  1795. .complete = ccw_device_pm_complete,
  1796. .freeze = ccw_device_pm_freeze,
  1797. .thaw = ccw_device_pm_thaw,
  1798. .restore = ccw_device_pm_restore,
  1799. };
  1800. static struct bus_type ccw_bus_type = {
  1801. .name = "ccw",
  1802. .match = ccw_bus_match,
  1803. .uevent = ccw_uevent,
  1804. .probe = ccw_device_probe,
  1805. .remove = ccw_device_remove,
  1806. .shutdown = ccw_device_shutdown,
  1807. .pm = &ccw_pm_ops,
  1808. };
  1809. /**
  1810. * ccw_driver_register() - register a ccw driver
  1811. * @cdriver: driver to be registered
  1812. *
  1813. * This function is mainly a wrapper around driver_register().
  1814. * Returns:
  1815. * %0 on success and a negative error value on failure.
  1816. */
  1817. int ccw_driver_register(struct ccw_driver *cdriver)
  1818. {
  1819. struct device_driver *drv = &cdriver->driver;
  1820. drv->bus = &ccw_bus_type;
  1821. return driver_register(drv);
  1822. }
  1823. /**
  1824. * ccw_driver_unregister() - deregister a ccw driver
  1825. * @cdriver: driver to be deregistered
  1826. *
  1827. * This function is mainly a wrapper around driver_unregister().
  1828. */
  1829. void ccw_driver_unregister(struct ccw_driver *cdriver)
  1830. {
  1831. driver_unregister(&cdriver->driver);
  1832. }
  1833. static void ccw_device_todo(struct work_struct *work)
  1834. {
  1835. struct ccw_device_private *priv;
  1836. struct ccw_device *cdev;
  1837. struct subchannel *sch;
  1838. enum cdev_todo todo;
  1839. priv = container_of(work, struct ccw_device_private, todo_work);
  1840. cdev = priv->cdev;
  1841. sch = to_subchannel(cdev->dev.parent);
  1842. /* Find out todo. */
  1843. spin_lock_irq(cdev->ccwlock);
  1844. todo = priv->todo;
  1845. priv->todo = CDEV_TODO_NOTHING;
  1846. CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
  1847. priv->dev_id.ssid, priv->dev_id.devno, todo);
  1848. spin_unlock_irq(cdev->ccwlock);
  1849. /* Perform todo. */
  1850. switch (todo) {
  1851. case CDEV_TODO_ENABLE_CMF:
  1852. cmf_reenable(cdev);
  1853. break;
  1854. case CDEV_TODO_REBIND:
  1855. ccw_device_do_unbind_bind(cdev);
  1856. break;
  1857. case CDEV_TODO_REGISTER:
  1858. io_subchannel_register(cdev);
  1859. break;
  1860. case CDEV_TODO_UNREG_EVAL:
  1861. if (!sch_is_pseudo_sch(sch))
  1862. css_schedule_eval(sch->schid);
  1863. /* fall-through */
  1864. case CDEV_TODO_UNREG:
  1865. if (sch_is_pseudo_sch(sch))
  1866. ccw_device_unregister(cdev);
  1867. else
  1868. ccw_device_call_sch_unregister(cdev);
  1869. break;
  1870. default:
  1871. break;
  1872. }
  1873. /* Release workqueue ref. */
  1874. put_device(&cdev->dev);
  1875. }
  1876. /**
  1877. * ccw_device_sched_todo - schedule ccw device operation
  1878. * @cdev: ccw device
  1879. * @todo: todo
  1880. *
  1881. * Schedule the operation identified by @todo to be performed on the slow path
  1882. * workqueue. Do nothing if another operation with higher priority is already
  1883. * scheduled. Needs to be called with ccwdev lock held.
  1884. */
  1885. void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
  1886. {
  1887. CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
  1888. cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
  1889. todo);
  1890. if (cdev->private->todo >= todo)
  1891. return;
  1892. cdev->private->todo = todo;
  1893. /* Get workqueue ref. */
  1894. if (!get_device(&cdev->dev))
  1895. return;
  1896. if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
  1897. /* Already queued, release workqueue ref. */
  1898. put_device(&cdev->dev);
  1899. }
  1900. }
  1901. /**
  1902. * ccw_device_siosl() - initiate logging
  1903. * @cdev: ccw device
  1904. *
  1905. * This function is used to invoke model-dependent logging within the channel
  1906. * subsystem.
  1907. */
  1908. int ccw_device_siosl(struct ccw_device *cdev)
  1909. {
  1910. struct subchannel *sch = to_subchannel(cdev->dev.parent);
  1911. return chsc_siosl(sch->schid);
  1912. }
  1913. EXPORT_SYMBOL_GPL(ccw_device_siosl);
  1914. MODULE_LICENSE("GPL");
  1915. EXPORT_SYMBOL(ccw_device_set_online);
  1916. EXPORT_SYMBOL(ccw_device_set_offline);
  1917. EXPORT_SYMBOL(ccw_driver_register);
  1918. EXPORT_SYMBOL(ccw_driver_unregister);
  1919. EXPORT_SYMBOL(get_ccwdev_by_busid);