device.c 49 KB

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