device.c 54 KB

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