virtio_ccw.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ccw based virtio transport
  4. *
  5. * Copyright IBM Corp. 2012, 2014
  6. *
  7. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  8. */
  9. #include <linux/kernel_stat.h>
  10. #include <linux/init.h>
  11. #include <linux/memblock.h>
  12. #include <linux/err.h>
  13. #include <linux/virtio.h>
  14. #include <linux/virtio_config.h>
  15. #include <linux/slab.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/virtio_ring.h>
  18. #include <linux/pfn.h>
  19. #include <linux/async.h>
  20. #include <linux/wait.h>
  21. #include <linux/list.h>
  22. #include <linux/bitops.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/io.h>
  25. #include <linux/kvm_para.h>
  26. #include <linux/notifier.h>
  27. #include <asm/diag.h>
  28. #include <asm/setup.h>
  29. #include <asm/irq.h>
  30. #include <asm/cio.h>
  31. #include <asm/ccwdev.h>
  32. #include <asm/virtio-ccw.h>
  33. #include <asm/isc.h>
  34. #include <asm/airq.h>
  35. /*
  36. * virtio related functions
  37. */
  38. struct vq_config_block {
  39. __u16 index;
  40. __u16 num;
  41. } __packed;
  42. #define VIRTIO_CCW_CONFIG_SIZE 0x100
  43. /* same as PCI config space size, should be enough for all drivers */
  44. struct virtio_ccw_device {
  45. struct virtio_device vdev;
  46. __u8 *status;
  47. __u8 config[VIRTIO_CCW_CONFIG_SIZE];
  48. struct ccw_device *cdev;
  49. __u32 curr_io;
  50. int err;
  51. unsigned int revision; /* Transport revision */
  52. wait_queue_head_t wait_q;
  53. spinlock_t lock;
  54. struct mutex io_lock; /* Serializes I/O requests */
  55. struct list_head virtqueues;
  56. unsigned long indicators;
  57. unsigned long indicators2;
  58. struct vq_config_block *config_block;
  59. bool is_thinint;
  60. bool going_away;
  61. bool device_lost;
  62. unsigned int config_ready;
  63. void *airq_info;
  64. };
  65. struct vq_info_block_legacy {
  66. __u64 queue;
  67. __u32 align;
  68. __u16 index;
  69. __u16 num;
  70. } __packed;
  71. struct vq_info_block {
  72. __u64 desc;
  73. __u32 res0;
  74. __u16 index;
  75. __u16 num;
  76. __u64 avail;
  77. __u64 used;
  78. } __packed;
  79. struct virtio_feature_desc {
  80. __le32 features;
  81. __u8 index;
  82. } __packed;
  83. struct virtio_thinint_area {
  84. unsigned long summary_indicator;
  85. unsigned long indicator;
  86. u64 bit_nr;
  87. u8 isc;
  88. } __packed;
  89. struct virtio_rev_info {
  90. __u16 revision;
  91. __u16 length;
  92. __u8 data[];
  93. };
  94. /* the highest virtio-ccw revision we support */
  95. #define VIRTIO_CCW_REV_MAX 1
  96. struct virtio_ccw_vq_info {
  97. struct virtqueue *vq;
  98. int num;
  99. void *queue;
  100. union {
  101. struct vq_info_block s;
  102. struct vq_info_block_legacy l;
  103. } *info_block;
  104. int bit_nr;
  105. struct list_head node;
  106. long cookie;
  107. };
  108. #define VIRTIO_AIRQ_ISC IO_SCH_ISC /* inherit from subchannel */
  109. #define VIRTIO_IV_BITS (L1_CACHE_BYTES * 8)
  110. #define MAX_AIRQ_AREAS 20
  111. static int virtio_ccw_use_airq = 1;
  112. struct airq_info {
  113. rwlock_t lock;
  114. u8 summary_indicator;
  115. struct airq_struct airq;
  116. struct airq_iv *aiv;
  117. };
  118. static struct airq_info *airq_areas[MAX_AIRQ_AREAS];
  119. #define CCW_CMD_SET_VQ 0x13
  120. #define CCW_CMD_VDEV_RESET 0x33
  121. #define CCW_CMD_SET_IND 0x43
  122. #define CCW_CMD_SET_CONF_IND 0x53
  123. #define CCW_CMD_READ_FEAT 0x12
  124. #define CCW_CMD_WRITE_FEAT 0x11
  125. #define CCW_CMD_READ_CONF 0x22
  126. #define CCW_CMD_WRITE_CONF 0x21
  127. #define CCW_CMD_WRITE_STATUS 0x31
  128. #define CCW_CMD_READ_VQ_CONF 0x32
  129. #define CCW_CMD_READ_STATUS 0x72
  130. #define CCW_CMD_SET_IND_ADAPTER 0x73
  131. #define CCW_CMD_SET_VIRTIO_REV 0x83
  132. #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
  133. #define VIRTIO_CCW_DOING_RESET 0x00040000
  134. #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
  135. #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
  136. #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
  137. #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
  138. #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
  139. #define VIRTIO_CCW_DOING_SET_IND 0x01000000
  140. #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
  141. #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
  142. #define VIRTIO_CCW_DOING_SET_IND_ADAPTER 0x08000000
  143. #define VIRTIO_CCW_DOING_SET_VIRTIO_REV 0x10000000
  144. #define VIRTIO_CCW_DOING_READ_STATUS 0x20000000
  145. #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
  146. static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
  147. {
  148. return container_of(vdev, struct virtio_ccw_device, vdev);
  149. }
  150. static void drop_airq_indicator(struct virtqueue *vq, struct airq_info *info)
  151. {
  152. unsigned long i, flags;
  153. write_lock_irqsave(&info->lock, flags);
  154. for (i = 0; i < airq_iv_end(info->aiv); i++) {
  155. if (vq == (void *)airq_iv_get_ptr(info->aiv, i)) {
  156. airq_iv_free_bit(info->aiv, i);
  157. airq_iv_set_ptr(info->aiv, i, 0);
  158. break;
  159. }
  160. }
  161. write_unlock_irqrestore(&info->lock, flags);
  162. }
  163. static void virtio_airq_handler(struct airq_struct *airq)
  164. {
  165. struct airq_info *info = container_of(airq, struct airq_info, airq);
  166. unsigned long ai;
  167. inc_irq_stat(IRQIO_VAI);
  168. read_lock(&info->lock);
  169. /* Walk through indicators field, summary indicator active. */
  170. for (ai = 0;;) {
  171. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  172. if (ai == -1UL)
  173. break;
  174. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  175. }
  176. info->summary_indicator = 0;
  177. smp_wmb();
  178. /* Walk through indicators field, summary indicator not active. */
  179. for (ai = 0;;) {
  180. ai = airq_iv_scan(info->aiv, ai, airq_iv_end(info->aiv));
  181. if (ai == -1UL)
  182. break;
  183. vring_interrupt(0, (void *)airq_iv_get_ptr(info->aiv, ai));
  184. }
  185. read_unlock(&info->lock);
  186. }
  187. static struct airq_info *new_airq_info(void)
  188. {
  189. struct airq_info *info;
  190. int rc;
  191. info = kzalloc(sizeof(*info), GFP_KERNEL);
  192. if (!info)
  193. return NULL;
  194. rwlock_init(&info->lock);
  195. info->aiv = airq_iv_create(VIRTIO_IV_BITS, AIRQ_IV_ALLOC | AIRQ_IV_PTR);
  196. if (!info->aiv) {
  197. kfree(info);
  198. return NULL;
  199. }
  200. info->airq.handler = virtio_airq_handler;
  201. info->airq.lsi_ptr = &info->summary_indicator;
  202. info->airq.lsi_mask = 0xff;
  203. info->airq.isc = VIRTIO_AIRQ_ISC;
  204. rc = register_adapter_interrupt(&info->airq);
  205. if (rc) {
  206. airq_iv_release(info->aiv);
  207. kfree(info);
  208. return NULL;
  209. }
  210. return info;
  211. }
  212. static unsigned long get_airq_indicator(struct virtqueue *vqs[], int nvqs,
  213. u64 *first, void **airq_info)
  214. {
  215. int i, j;
  216. struct airq_info *info;
  217. unsigned long indicator_addr = 0;
  218. unsigned long bit, flags;
  219. for (i = 0; i < MAX_AIRQ_AREAS && !indicator_addr; i++) {
  220. if (!airq_areas[i])
  221. airq_areas[i] = new_airq_info();
  222. info = airq_areas[i];
  223. if (!info)
  224. return 0;
  225. write_lock_irqsave(&info->lock, flags);
  226. bit = airq_iv_alloc(info->aiv, nvqs);
  227. if (bit == -1UL) {
  228. /* Not enough vacancies. */
  229. write_unlock_irqrestore(&info->lock, flags);
  230. continue;
  231. }
  232. *first = bit;
  233. *airq_info = info;
  234. indicator_addr = (unsigned long)info->aiv->vector;
  235. for (j = 0; j < nvqs; j++) {
  236. airq_iv_set_ptr(info->aiv, bit + j,
  237. (unsigned long)vqs[j]);
  238. }
  239. write_unlock_irqrestore(&info->lock, flags);
  240. }
  241. return indicator_addr;
  242. }
  243. static void virtio_ccw_drop_indicators(struct virtio_ccw_device *vcdev)
  244. {
  245. struct virtio_ccw_vq_info *info;
  246. list_for_each_entry(info, &vcdev->virtqueues, node)
  247. drop_airq_indicator(info->vq, vcdev->airq_info);
  248. }
  249. static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
  250. {
  251. unsigned long flags;
  252. __u32 ret;
  253. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  254. if (vcdev->err)
  255. ret = 0;
  256. else
  257. ret = vcdev->curr_io & flag;
  258. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  259. return ret;
  260. }
  261. static int ccw_io_helper(struct virtio_ccw_device *vcdev,
  262. struct ccw1 *ccw, __u32 intparm)
  263. {
  264. int ret;
  265. unsigned long flags;
  266. int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
  267. mutex_lock(&vcdev->io_lock);
  268. do {
  269. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  270. ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
  271. if (!ret) {
  272. if (!vcdev->curr_io)
  273. vcdev->err = 0;
  274. vcdev->curr_io |= flag;
  275. }
  276. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  277. cpu_relax();
  278. } while (ret == -EBUSY);
  279. wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
  280. ret = ret ? ret : vcdev->err;
  281. mutex_unlock(&vcdev->io_lock);
  282. return ret;
  283. }
  284. static void virtio_ccw_drop_indicator(struct virtio_ccw_device *vcdev,
  285. struct ccw1 *ccw)
  286. {
  287. int ret;
  288. unsigned long *indicatorp = NULL;
  289. struct virtio_thinint_area *thinint_area = NULL;
  290. struct airq_info *airq_info = vcdev->airq_info;
  291. if (vcdev->is_thinint) {
  292. thinint_area = kzalloc(sizeof(*thinint_area),
  293. GFP_DMA | GFP_KERNEL);
  294. if (!thinint_area)
  295. return;
  296. thinint_area->summary_indicator =
  297. (unsigned long) &airq_info->summary_indicator;
  298. thinint_area->isc = VIRTIO_AIRQ_ISC;
  299. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  300. ccw->count = sizeof(*thinint_area);
  301. ccw->cda = (__u32)(unsigned long) thinint_area;
  302. } else {
  303. /* payload is the address of the indicators */
  304. indicatorp = kmalloc(sizeof(&vcdev->indicators),
  305. GFP_DMA | GFP_KERNEL);
  306. if (!indicatorp)
  307. return;
  308. *indicatorp = 0;
  309. ccw->cmd_code = CCW_CMD_SET_IND;
  310. ccw->count = sizeof(&vcdev->indicators);
  311. ccw->cda = (__u32)(unsigned long) indicatorp;
  312. }
  313. /* Deregister indicators from host. */
  314. vcdev->indicators = 0;
  315. ccw->flags = 0;
  316. ret = ccw_io_helper(vcdev, ccw,
  317. vcdev->is_thinint ?
  318. VIRTIO_CCW_DOING_SET_IND_ADAPTER :
  319. VIRTIO_CCW_DOING_SET_IND);
  320. if (ret && (ret != -ENODEV))
  321. dev_info(&vcdev->cdev->dev,
  322. "Failed to deregister indicators (%d)\n", ret);
  323. else if (vcdev->is_thinint)
  324. virtio_ccw_drop_indicators(vcdev);
  325. kfree(indicatorp);
  326. kfree(thinint_area);
  327. }
  328. static inline long __do_kvm_notify(struct subchannel_id schid,
  329. unsigned long queue_index,
  330. long cookie)
  331. {
  332. register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
  333. register struct subchannel_id __schid asm("2") = schid;
  334. register unsigned long __index asm("3") = queue_index;
  335. register long __rc asm("2");
  336. register long __cookie asm("4") = cookie;
  337. asm volatile ("diag 2,4,0x500\n"
  338. : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index),
  339. "d"(__cookie)
  340. : "memory", "cc");
  341. return __rc;
  342. }
  343. static inline long do_kvm_notify(struct subchannel_id schid,
  344. unsigned long queue_index,
  345. long cookie)
  346. {
  347. diag_stat_inc(DIAG_STAT_X500);
  348. return __do_kvm_notify(schid, queue_index, cookie);
  349. }
  350. static bool virtio_ccw_kvm_notify(struct virtqueue *vq)
  351. {
  352. struct virtio_ccw_vq_info *info = vq->priv;
  353. struct virtio_ccw_device *vcdev;
  354. struct subchannel_id schid;
  355. vcdev = to_vc_device(info->vq->vdev);
  356. ccw_device_get_schid(vcdev->cdev, &schid);
  357. info->cookie = do_kvm_notify(schid, vq->index, info->cookie);
  358. if (info->cookie < 0)
  359. return false;
  360. return true;
  361. }
  362. static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
  363. struct ccw1 *ccw, int index)
  364. {
  365. int ret;
  366. vcdev->config_block->index = index;
  367. ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
  368. ccw->flags = 0;
  369. ccw->count = sizeof(struct vq_config_block);
  370. ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
  371. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
  372. if (ret)
  373. return ret;
  374. return vcdev->config_block->num;
  375. }
  376. static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
  377. {
  378. struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
  379. struct virtio_ccw_vq_info *info = vq->priv;
  380. unsigned long flags;
  381. unsigned long size;
  382. int ret;
  383. unsigned int index = vq->index;
  384. /* Remove from our list. */
  385. spin_lock_irqsave(&vcdev->lock, flags);
  386. list_del(&info->node);
  387. spin_unlock_irqrestore(&vcdev->lock, flags);
  388. /* Release from host. */
  389. if (vcdev->revision == 0) {
  390. info->info_block->l.queue = 0;
  391. info->info_block->l.align = 0;
  392. info->info_block->l.index = index;
  393. info->info_block->l.num = 0;
  394. ccw->count = sizeof(info->info_block->l);
  395. } else {
  396. info->info_block->s.desc = 0;
  397. info->info_block->s.index = index;
  398. info->info_block->s.num = 0;
  399. info->info_block->s.avail = 0;
  400. info->info_block->s.used = 0;
  401. ccw->count = sizeof(info->info_block->s);
  402. }
  403. ccw->cmd_code = CCW_CMD_SET_VQ;
  404. ccw->flags = 0;
  405. ccw->cda = (__u32)(unsigned long)(info->info_block);
  406. ret = ccw_io_helper(vcdev, ccw,
  407. VIRTIO_CCW_DOING_SET_VQ | index);
  408. /*
  409. * -ENODEV isn't considered an error: The device is gone anyway.
  410. * This may happen on device detach.
  411. */
  412. if (ret && (ret != -ENODEV))
  413. dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d\n",
  414. ret, index);
  415. vring_del_virtqueue(vq);
  416. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  417. free_pages_exact(info->queue, size);
  418. kfree(info->info_block);
  419. kfree(info);
  420. }
  421. static void virtio_ccw_del_vqs(struct virtio_device *vdev)
  422. {
  423. struct virtqueue *vq, *n;
  424. struct ccw1 *ccw;
  425. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  426. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  427. if (!ccw)
  428. return;
  429. virtio_ccw_drop_indicator(vcdev, ccw);
  430. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  431. virtio_ccw_del_vq(vq, ccw);
  432. kfree(ccw);
  433. }
  434. static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
  435. int i, vq_callback_t *callback,
  436. const char *name, bool ctx,
  437. struct ccw1 *ccw)
  438. {
  439. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  440. int err;
  441. struct virtqueue *vq = NULL;
  442. struct virtio_ccw_vq_info *info;
  443. unsigned long size = 0; /* silence the compiler */
  444. unsigned long flags;
  445. /* Allocate queue. */
  446. info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
  447. if (!info) {
  448. dev_warn(&vcdev->cdev->dev, "no info\n");
  449. err = -ENOMEM;
  450. goto out_err;
  451. }
  452. info->info_block = kzalloc(sizeof(*info->info_block),
  453. GFP_DMA | GFP_KERNEL);
  454. if (!info->info_block) {
  455. dev_warn(&vcdev->cdev->dev, "no info block\n");
  456. err = -ENOMEM;
  457. goto out_err;
  458. }
  459. info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
  460. if (info->num < 0) {
  461. err = info->num;
  462. goto out_err;
  463. }
  464. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  465. info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  466. if (info->queue == NULL) {
  467. dev_warn(&vcdev->cdev->dev, "no queue\n");
  468. err = -ENOMEM;
  469. goto out_err;
  470. }
  471. vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
  472. true, ctx, info->queue, virtio_ccw_kvm_notify,
  473. callback, name);
  474. if (!vq) {
  475. /* For now, we fail if we can't get the requested size. */
  476. dev_warn(&vcdev->cdev->dev, "no vq\n");
  477. err = -ENOMEM;
  478. goto out_err;
  479. }
  480. /* Register it with the host. */
  481. if (vcdev->revision == 0) {
  482. info->info_block->l.queue = (__u64)info->queue;
  483. info->info_block->l.align = KVM_VIRTIO_CCW_RING_ALIGN;
  484. info->info_block->l.index = i;
  485. info->info_block->l.num = info->num;
  486. ccw->count = sizeof(info->info_block->l);
  487. } else {
  488. info->info_block->s.desc = (__u64)info->queue;
  489. info->info_block->s.index = i;
  490. info->info_block->s.num = info->num;
  491. info->info_block->s.avail = (__u64)virtqueue_get_avail(vq);
  492. info->info_block->s.used = (__u64)virtqueue_get_used(vq);
  493. ccw->count = sizeof(info->info_block->s);
  494. }
  495. ccw->cmd_code = CCW_CMD_SET_VQ;
  496. ccw->flags = 0;
  497. ccw->cda = (__u32)(unsigned long)(info->info_block);
  498. err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
  499. if (err) {
  500. dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
  501. goto out_err;
  502. }
  503. info->vq = vq;
  504. vq->priv = info;
  505. /* Save it to our list. */
  506. spin_lock_irqsave(&vcdev->lock, flags);
  507. list_add(&info->node, &vcdev->virtqueues);
  508. spin_unlock_irqrestore(&vcdev->lock, flags);
  509. return vq;
  510. out_err:
  511. if (vq)
  512. vring_del_virtqueue(vq);
  513. if (info) {
  514. if (info->queue)
  515. free_pages_exact(info->queue, size);
  516. kfree(info->info_block);
  517. }
  518. kfree(info);
  519. return ERR_PTR(err);
  520. }
  521. static int virtio_ccw_register_adapter_ind(struct virtio_ccw_device *vcdev,
  522. struct virtqueue *vqs[], int nvqs,
  523. struct ccw1 *ccw)
  524. {
  525. int ret;
  526. struct virtio_thinint_area *thinint_area = NULL;
  527. struct airq_info *info;
  528. thinint_area = kzalloc(sizeof(*thinint_area), GFP_DMA | GFP_KERNEL);
  529. if (!thinint_area) {
  530. ret = -ENOMEM;
  531. goto out;
  532. }
  533. /* Try to get an indicator. */
  534. thinint_area->indicator = get_airq_indicator(vqs, nvqs,
  535. &thinint_area->bit_nr,
  536. &vcdev->airq_info);
  537. if (!thinint_area->indicator) {
  538. ret = -ENOSPC;
  539. goto out;
  540. }
  541. info = vcdev->airq_info;
  542. thinint_area->summary_indicator =
  543. (unsigned long) &info->summary_indicator;
  544. thinint_area->isc = VIRTIO_AIRQ_ISC;
  545. ccw->cmd_code = CCW_CMD_SET_IND_ADAPTER;
  546. ccw->flags = CCW_FLAG_SLI;
  547. ccw->count = sizeof(*thinint_area);
  548. ccw->cda = (__u32)(unsigned long)thinint_area;
  549. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND_ADAPTER);
  550. if (ret) {
  551. if (ret == -EOPNOTSUPP) {
  552. /*
  553. * The host does not support adapter interrupts
  554. * for virtio-ccw, stop trying.
  555. */
  556. virtio_ccw_use_airq = 0;
  557. pr_info("Adapter interrupts unsupported on host\n");
  558. } else
  559. dev_warn(&vcdev->cdev->dev,
  560. "enabling adapter interrupts = %d\n", ret);
  561. virtio_ccw_drop_indicators(vcdev);
  562. }
  563. out:
  564. kfree(thinint_area);
  565. return ret;
  566. }
  567. static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  568. struct virtqueue *vqs[],
  569. vq_callback_t *callbacks[],
  570. const char * const names[],
  571. const bool *ctx,
  572. struct irq_affinity *desc)
  573. {
  574. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  575. unsigned long *indicatorp = NULL;
  576. int ret, i;
  577. struct ccw1 *ccw;
  578. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  579. if (!ccw)
  580. return -ENOMEM;
  581. for (i = 0; i < nvqs; ++i) {
  582. vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
  583. ctx ? ctx[i] : false, ccw);
  584. if (IS_ERR(vqs[i])) {
  585. ret = PTR_ERR(vqs[i]);
  586. vqs[i] = NULL;
  587. goto out;
  588. }
  589. }
  590. ret = -ENOMEM;
  591. /*
  592. * We need a data area under 2G to communicate. Our payload is
  593. * the address of the indicators.
  594. */
  595. indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
  596. if (!indicatorp)
  597. goto out;
  598. *indicatorp = (unsigned long) &vcdev->indicators;
  599. if (vcdev->is_thinint) {
  600. ret = virtio_ccw_register_adapter_ind(vcdev, vqs, nvqs, ccw);
  601. if (ret)
  602. /* no error, just fall back to legacy interrupts */
  603. vcdev->is_thinint = false;
  604. }
  605. if (!vcdev->is_thinint) {
  606. /* Register queue indicators with host. */
  607. vcdev->indicators = 0;
  608. ccw->cmd_code = CCW_CMD_SET_IND;
  609. ccw->flags = 0;
  610. ccw->count = sizeof(&vcdev->indicators);
  611. ccw->cda = (__u32)(unsigned long) indicatorp;
  612. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
  613. if (ret)
  614. goto out;
  615. }
  616. /* Register indicators2 with host for config changes */
  617. *indicatorp = (unsigned long) &vcdev->indicators2;
  618. vcdev->indicators2 = 0;
  619. ccw->cmd_code = CCW_CMD_SET_CONF_IND;
  620. ccw->flags = 0;
  621. ccw->count = sizeof(&vcdev->indicators2);
  622. ccw->cda = (__u32)(unsigned long) indicatorp;
  623. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
  624. if (ret)
  625. goto out;
  626. kfree(indicatorp);
  627. kfree(ccw);
  628. return 0;
  629. out:
  630. kfree(indicatorp);
  631. kfree(ccw);
  632. virtio_ccw_del_vqs(vdev);
  633. return ret;
  634. }
  635. static void virtio_ccw_reset(struct virtio_device *vdev)
  636. {
  637. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  638. struct ccw1 *ccw;
  639. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  640. if (!ccw)
  641. return;
  642. /* Zero status bits. */
  643. *vcdev->status = 0;
  644. /* Send a reset ccw on device. */
  645. ccw->cmd_code = CCW_CMD_VDEV_RESET;
  646. ccw->flags = 0;
  647. ccw->count = 0;
  648. ccw->cda = 0;
  649. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
  650. kfree(ccw);
  651. }
  652. static u64 virtio_ccw_get_features(struct virtio_device *vdev)
  653. {
  654. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  655. struct virtio_feature_desc *features;
  656. int ret;
  657. u64 rc;
  658. struct ccw1 *ccw;
  659. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  660. if (!ccw)
  661. return 0;
  662. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  663. if (!features) {
  664. rc = 0;
  665. goto out_free;
  666. }
  667. /* Read the feature bits from the host. */
  668. features->index = 0;
  669. ccw->cmd_code = CCW_CMD_READ_FEAT;
  670. ccw->flags = 0;
  671. ccw->count = sizeof(*features);
  672. ccw->cda = (__u32)(unsigned long)features;
  673. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  674. if (ret) {
  675. rc = 0;
  676. goto out_free;
  677. }
  678. rc = le32_to_cpu(features->features);
  679. if (vcdev->revision == 0)
  680. goto out_free;
  681. /* Read second half of the feature bits from the host. */
  682. features->index = 1;
  683. ccw->cmd_code = CCW_CMD_READ_FEAT;
  684. ccw->flags = 0;
  685. ccw->count = sizeof(*features);
  686. ccw->cda = (__u32)(unsigned long)features;
  687. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  688. if (ret == 0)
  689. rc |= (u64)le32_to_cpu(features->features) << 32;
  690. out_free:
  691. kfree(features);
  692. kfree(ccw);
  693. return rc;
  694. }
  695. static int virtio_ccw_finalize_features(struct virtio_device *vdev)
  696. {
  697. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  698. struct virtio_feature_desc *features;
  699. struct ccw1 *ccw;
  700. int ret;
  701. if (vcdev->revision >= 1 &&
  702. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  703. dev_err(&vdev->dev, "virtio: device uses revision 1 "
  704. "but does not have VIRTIO_F_VERSION_1\n");
  705. return -EINVAL;
  706. }
  707. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  708. if (!ccw)
  709. return -ENOMEM;
  710. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  711. if (!features) {
  712. ret = -ENOMEM;
  713. goto out_free;
  714. }
  715. /* Give virtio_ring a chance to accept features. */
  716. vring_transport_features(vdev);
  717. features->index = 0;
  718. features->features = cpu_to_le32((u32)vdev->features);
  719. /* Write the first half of the feature bits to the host. */
  720. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  721. ccw->flags = 0;
  722. ccw->count = sizeof(*features);
  723. ccw->cda = (__u32)(unsigned long)features;
  724. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  725. if (ret)
  726. goto out_free;
  727. if (vcdev->revision == 0)
  728. goto out_free;
  729. features->index = 1;
  730. features->features = cpu_to_le32(vdev->features >> 32);
  731. /* Write the second half of the feature bits to the host. */
  732. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  733. ccw->flags = 0;
  734. ccw->count = sizeof(*features);
  735. ccw->cda = (__u32)(unsigned long)features;
  736. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  737. out_free:
  738. kfree(features);
  739. kfree(ccw);
  740. return ret;
  741. }
  742. static void virtio_ccw_get_config(struct virtio_device *vdev,
  743. unsigned int offset, void *buf, unsigned len)
  744. {
  745. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  746. int ret;
  747. struct ccw1 *ccw;
  748. void *config_area;
  749. unsigned long flags;
  750. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  751. if (!ccw)
  752. return;
  753. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  754. if (!config_area)
  755. goto out_free;
  756. /* Read the config area from the host. */
  757. ccw->cmd_code = CCW_CMD_READ_CONF;
  758. ccw->flags = 0;
  759. ccw->count = offset + len;
  760. ccw->cda = (__u32)(unsigned long)config_area;
  761. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  762. if (ret)
  763. goto out_free;
  764. spin_lock_irqsave(&vcdev->lock, flags);
  765. memcpy(vcdev->config, config_area, offset + len);
  766. if (vcdev->config_ready < offset + len)
  767. vcdev->config_ready = offset + len;
  768. spin_unlock_irqrestore(&vcdev->lock, flags);
  769. if (buf)
  770. memcpy(buf, config_area + offset, len);
  771. out_free:
  772. kfree(config_area);
  773. kfree(ccw);
  774. }
  775. static void virtio_ccw_set_config(struct virtio_device *vdev,
  776. unsigned int offset, const void *buf,
  777. unsigned len)
  778. {
  779. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  780. struct ccw1 *ccw;
  781. void *config_area;
  782. unsigned long flags;
  783. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  784. if (!ccw)
  785. return;
  786. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  787. if (!config_area)
  788. goto out_free;
  789. /* Make sure we don't overwrite fields. */
  790. if (vcdev->config_ready < offset)
  791. virtio_ccw_get_config(vdev, 0, NULL, offset);
  792. spin_lock_irqsave(&vcdev->lock, flags);
  793. memcpy(&vcdev->config[offset], buf, len);
  794. /* Write the config area to the host. */
  795. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  796. spin_unlock_irqrestore(&vcdev->lock, flags);
  797. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  798. ccw->flags = 0;
  799. ccw->count = offset + len;
  800. ccw->cda = (__u32)(unsigned long)config_area;
  801. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  802. out_free:
  803. kfree(config_area);
  804. kfree(ccw);
  805. }
  806. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  807. {
  808. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  809. u8 old_status = *vcdev->status;
  810. struct ccw1 *ccw;
  811. if (vcdev->revision < 1)
  812. return *vcdev->status;
  813. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  814. if (!ccw)
  815. return old_status;
  816. ccw->cmd_code = CCW_CMD_READ_STATUS;
  817. ccw->flags = 0;
  818. ccw->count = sizeof(*vcdev->status);
  819. ccw->cda = (__u32)(unsigned long)vcdev->status;
  820. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_STATUS);
  821. /*
  822. * If the channel program failed (should only happen if the device
  823. * was hotunplugged, and then we clean up via the machine check
  824. * handler anyway), vcdev->status was not overwritten and we just
  825. * return the old status, which is fine.
  826. */
  827. kfree(ccw);
  828. return *vcdev->status;
  829. }
  830. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  831. {
  832. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  833. u8 old_status = *vcdev->status;
  834. struct ccw1 *ccw;
  835. int ret;
  836. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  837. if (!ccw)
  838. return;
  839. /* Write the status to the host. */
  840. *vcdev->status = status;
  841. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  842. ccw->flags = 0;
  843. ccw->count = sizeof(status);
  844. ccw->cda = (__u32)(unsigned long)vcdev->status;
  845. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  846. /* Write failed? We assume status is unchanged. */
  847. if (ret)
  848. *vcdev->status = old_status;
  849. kfree(ccw);
  850. }
  851. static const struct virtio_config_ops virtio_ccw_config_ops = {
  852. .get_features = virtio_ccw_get_features,
  853. .finalize_features = virtio_ccw_finalize_features,
  854. .get = virtio_ccw_get_config,
  855. .set = virtio_ccw_set_config,
  856. .get_status = virtio_ccw_get_status,
  857. .set_status = virtio_ccw_set_status,
  858. .reset = virtio_ccw_reset,
  859. .find_vqs = virtio_ccw_find_vqs,
  860. .del_vqs = virtio_ccw_del_vqs,
  861. };
  862. /*
  863. * ccw bus driver related functions
  864. */
  865. static void virtio_ccw_release_dev(struct device *_d)
  866. {
  867. struct virtio_device *dev = dev_to_virtio(_d);
  868. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  869. kfree(vcdev->status);
  870. kfree(vcdev->config_block);
  871. kfree(vcdev);
  872. }
  873. static int irb_is_error(struct irb *irb)
  874. {
  875. if (scsw_cstat(&irb->scsw) != 0)
  876. return 1;
  877. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  878. return 1;
  879. if (scsw_cc(&irb->scsw) != 0)
  880. return 1;
  881. return 0;
  882. }
  883. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  884. int index)
  885. {
  886. struct virtio_ccw_vq_info *info;
  887. unsigned long flags;
  888. struct virtqueue *vq;
  889. vq = NULL;
  890. spin_lock_irqsave(&vcdev->lock, flags);
  891. list_for_each_entry(info, &vcdev->virtqueues, node) {
  892. if (info->vq->index == index) {
  893. vq = info->vq;
  894. break;
  895. }
  896. }
  897. spin_unlock_irqrestore(&vcdev->lock, flags);
  898. return vq;
  899. }
  900. static void virtio_ccw_check_activity(struct virtio_ccw_device *vcdev,
  901. __u32 activity)
  902. {
  903. if (vcdev->curr_io & activity) {
  904. switch (activity) {
  905. case VIRTIO_CCW_DOING_READ_FEAT:
  906. case VIRTIO_CCW_DOING_WRITE_FEAT:
  907. case VIRTIO_CCW_DOING_READ_CONFIG:
  908. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  909. case VIRTIO_CCW_DOING_WRITE_STATUS:
  910. case VIRTIO_CCW_DOING_READ_STATUS:
  911. case VIRTIO_CCW_DOING_SET_VQ:
  912. case VIRTIO_CCW_DOING_SET_IND:
  913. case VIRTIO_CCW_DOING_SET_CONF_IND:
  914. case VIRTIO_CCW_DOING_RESET:
  915. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  916. case VIRTIO_CCW_DOING_SET_IND_ADAPTER:
  917. case VIRTIO_CCW_DOING_SET_VIRTIO_REV:
  918. vcdev->curr_io &= ~activity;
  919. wake_up(&vcdev->wait_q);
  920. break;
  921. default:
  922. /* don't know what to do... */
  923. dev_warn(&vcdev->cdev->dev,
  924. "Suspicious activity '%08x'\n", activity);
  925. WARN_ON(1);
  926. break;
  927. }
  928. }
  929. }
  930. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  931. unsigned long intparm,
  932. struct irb *irb)
  933. {
  934. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  935. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  936. int i;
  937. struct virtqueue *vq;
  938. if (!vcdev)
  939. return;
  940. if (IS_ERR(irb)) {
  941. vcdev->err = PTR_ERR(irb);
  942. virtio_ccw_check_activity(vcdev, activity);
  943. /* Don't poke around indicators, something's wrong. */
  944. return;
  945. }
  946. /* Check if it's a notification from the host. */
  947. if ((intparm == 0) &&
  948. (scsw_stctl(&irb->scsw) ==
  949. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  950. /* OK */
  951. }
  952. if (irb_is_error(irb)) {
  953. /* Command reject? */
  954. if ((scsw_dstat(&irb->scsw) & DEV_STAT_UNIT_CHECK) &&
  955. (irb->ecw[0] & SNS0_CMD_REJECT))
  956. vcdev->err = -EOPNOTSUPP;
  957. else
  958. /* Map everything else to -EIO. */
  959. vcdev->err = -EIO;
  960. }
  961. virtio_ccw_check_activity(vcdev, activity);
  962. for_each_set_bit(i, &vcdev->indicators,
  963. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  964. /* The bit clear must happen before the vring kick. */
  965. clear_bit(i, &vcdev->indicators);
  966. barrier();
  967. vq = virtio_ccw_vq_by_ind(vcdev, i);
  968. vring_interrupt(0, vq);
  969. }
  970. if (test_bit(0, &vcdev->indicators2)) {
  971. virtio_config_changed(&vcdev->vdev);
  972. clear_bit(0, &vcdev->indicators2);
  973. }
  974. }
  975. /*
  976. * We usually want to autoonline all devices, but give the admin
  977. * a way to exempt devices from this.
  978. */
  979. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  980. (8*sizeof(long)))
  981. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  982. static char *no_auto = "";
  983. module_param(no_auto, charp, 0444);
  984. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  985. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  986. {
  987. struct ccw_dev_id id;
  988. ccw_device_get_id(cdev, &id);
  989. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  990. return 0;
  991. return 1;
  992. }
  993. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  994. {
  995. struct ccw_device *cdev = data;
  996. int ret;
  997. ret = ccw_device_set_online(cdev);
  998. if (ret)
  999. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  1000. }
  1001. static int virtio_ccw_probe(struct ccw_device *cdev)
  1002. {
  1003. cdev->handler = virtio_ccw_int_handler;
  1004. if (virtio_ccw_check_autoonline(cdev))
  1005. async_schedule(virtio_ccw_auto_online, cdev);
  1006. return 0;
  1007. }
  1008. static struct virtio_ccw_device *virtio_grab_drvdata(struct ccw_device *cdev)
  1009. {
  1010. unsigned long flags;
  1011. struct virtio_ccw_device *vcdev;
  1012. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1013. vcdev = dev_get_drvdata(&cdev->dev);
  1014. if (!vcdev || vcdev->going_away) {
  1015. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1016. return NULL;
  1017. }
  1018. vcdev->going_away = true;
  1019. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1020. return vcdev;
  1021. }
  1022. static void virtio_ccw_remove(struct ccw_device *cdev)
  1023. {
  1024. unsigned long flags;
  1025. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1026. if (vcdev && cdev->online) {
  1027. if (vcdev->device_lost)
  1028. virtio_break_device(&vcdev->vdev);
  1029. unregister_virtio_device(&vcdev->vdev);
  1030. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1031. dev_set_drvdata(&cdev->dev, NULL);
  1032. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1033. }
  1034. cdev->handler = NULL;
  1035. }
  1036. static int virtio_ccw_offline(struct ccw_device *cdev)
  1037. {
  1038. unsigned long flags;
  1039. struct virtio_ccw_device *vcdev = virtio_grab_drvdata(cdev);
  1040. if (!vcdev)
  1041. return 0;
  1042. if (vcdev->device_lost)
  1043. virtio_break_device(&vcdev->vdev);
  1044. unregister_virtio_device(&vcdev->vdev);
  1045. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1046. dev_set_drvdata(&cdev->dev, NULL);
  1047. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1048. return 0;
  1049. }
  1050. static int virtio_ccw_set_transport_rev(struct virtio_ccw_device *vcdev)
  1051. {
  1052. struct virtio_rev_info *rev;
  1053. struct ccw1 *ccw;
  1054. int ret;
  1055. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  1056. if (!ccw)
  1057. return -ENOMEM;
  1058. rev = kzalloc(sizeof(*rev), GFP_DMA | GFP_KERNEL);
  1059. if (!rev) {
  1060. kfree(ccw);
  1061. return -ENOMEM;
  1062. }
  1063. /* Set transport revision */
  1064. ccw->cmd_code = CCW_CMD_SET_VIRTIO_REV;
  1065. ccw->flags = 0;
  1066. ccw->count = sizeof(*rev);
  1067. ccw->cda = (__u32)(unsigned long)rev;
  1068. vcdev->revision = VIRTIO_CCW_REV_MAX;
  1069. do {
  1070. rev->revision = vcdev->revision;
  1071. /* none of our supported revisions carry payload */
  1072. rev->length = 0;
  1073. ret = ccw_io_helper(vcdev, ccw,
  1074. VIRTIO_CCW_DOING_SET_VIRTIO_REV);
  1075. if (ret == -EOPNOTSUPP) {
  1076. if (vcdev->revision == 0)
  1077. /*
  1078. * The host device does not support setting
  1079. * the revision: let's operate it in legacy
  1080. * mode.
  1081. */
  1082. ret = 0;
  1083. else
  1084. vcdev->revision--;
  1085. }
  1086. } while (ret == -EOPNOTSUPP);
  1087. kfree(ccw);
  1088. kfree(rev);
  1089. return ret;
  1090. }
  1091. static int virtio_ccw_online(struct ccw_device *cdev)
  1092. {
  1093. int ret;
  1094. struct virtio_ccw_device *vcdev;
  1095. unsigned long flags;
  1096. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  1097. if (!vcdev) {
  1098. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  1099. ret = -ENOMEM;
  1100. goto out_free;
  1101. }
  1102. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  1103. GFP_DMA | GFP_KERNEL);
  1104. if (!vcdev->config_block) {
  1105. ret = -ENOMEM;
  1106. goto out_free;
  1107. }
  1108. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  1109. if (!vcdev->status) {
  1110. ret = -ENOMEM;
  1111. goto out_free;
  1112. }
  1113. vcdev->is_thinint = virtio_ccw_use_airq; /* at least try */
  1114. vcdev->vdev.dev.parent = &cdev->dev;
  1115. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  1116. vcdev->vdev.config = &virtio_ccw_config_ops;
  1117. vcdev->cdev = cdev;
  1118. init_waitqueue_head(&vcdev->wait_q);
  1119. INIT_LIST_HEAD(&vcdev->virtqueues);
  1120. spin_lock_init(&vcdev->lock);
  1121. mutex_init(&vcdev->io_lock);
  1122. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1123. dev_set_drvdata(&cdev->dev, vcdev);
  1124. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1125. vcdev->vdev.id.vendor = cdev->id.cu_type;
  1126. vcdev->vdev.id.device = cdev->id.cu_model;
  1127. ret = virtio_ccw_set_transport_rev(vcdev);
  1128. if (ret)
  1129. goto out_free;
  1130. ret = register_virtio_device(&vcdev->vdev);
  1131. if (ret) {
  1132. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  1133. ret);
  1134. goto out_put;
  1135. }
  1136. return 0;
  1137. out_put:
  1138. spin_lock_irqsave(get_ccwdev_lock(cdev), flags);
  1139. dev_set_drvdata(&cdev->dev, NULL);
  1140. spin_unlock_irqrestore(get_ccwdev_lock(cdev), flags);
  1141. put_device(&vcdev->vdev.dev);
  1142. return ret;
  1143. out_free:
  1144. if (vcdev) {
  1145. kfree(vcdev->status);
  1146. kfree(vcdev->config_block);
  1147. }
  1148. kfree(vcdev);
  1149. return ret;
  1150. }
  1151. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  1152. {
  1153. int rc;
  1154. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1155. /*
  1156. * Make sure vcdev is set
  1157. * i.e. set_offline/remove callback not already running
  1158. */
  1159. if (!vcdev)
  1160. return NOTIFY_DONE;
  1161. switch (event) {
  1162. case CIO_GONE:
  1163. vcdev->device_lost = true;
  1164. rc = NOTIFY_DONE;
  1165. break;
  1166. case CIO_OPER:
  1167. rc = NOTIFY_OK;
  1168. break;
  1169. default:
  1170. rc = NOTIFY_DONE;
  1171. break;
  1172. }
  1173. return rc;
  1174. }
  1175. static struct ccw_device_id virtio_ids[] = {
  1176. { CCW_DEVICE(0x3832, 0) },
  1177. {},
  1178. };
  1179. #ifdef CONFIG_PM_SLEEP
  1180. static int virtio_ccw_freeze(struct ccw_device *cdev)
  1181. {
  1182. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1183. return virtio_device_freeze(&vcdev->vdev);
  1184. }
  1185. static int virtio_ccw_restore(struct ccw_device *cdev)
  1186. {
  1187. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  1188. int ret;
  1189. ret = virtio_ccw_set_transport_rev(vcdev);
  1190. if (ret)
  1191. return ret;
  1192. return virtio_device_restore(&vcdev->vdev);
  1193. }
  1194. #endif
  1195. static struct ccw_driver virtio_ccw_driver = {
  1196. .driver = {
  1197. .owner = THIS_MODULE,
  1198. .name = "virtio_ccw",
  1199. },
  1200. .ids = virtio_ids,
  1201. .probe = virtio_ccw_probe,
  1202. .remove = virtio_ccw_remove,
  1203. .set_offline = virtio_ccw_offline,
  1204. .set_online = virtio_ccw_online,
  1205. .notify = virtio_ccw_cio_notify,
  1206. .int_class = IRQIO_VIR,
  1207. #ifdef CONFIG_PM_SLEEP
  1208. .freeze = virtio_ccw_freeze,
  1209. .thaw = virtio_ccw_restore,
  1210. .restore = virtio_ccw_restore,
  1211. #endif
  1212. };
  1213. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  1214. int max_digit, int max_val)
  1215. {
  1216. int diff;
  1217. diff = 0;
  1218. *val = 0;
  1219. while (diff <= max_digit) {
  1220. int value = hex_to_bin(**cp);
  1221. if (value < 0)
  1222. break;
  1223. *val = *val * 16 + value;
  1224. (*cp)++;
  1225. diff++;
  1226. }
  1227. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  1228. return 1;
  1229. return 0;
  1230. }
  1231. static int __init parse_busid(char *str, unsigned int *cssid,
  1232. unsigned int *ssid, unsigned int *devno)
  1233. {
  1234. char *str_work;
  1235. int rc, ret;
  1236. rc = 1;
  1237. if (*str == '\0')
  1238. goto out;
  1239. str_work = str;
  1240. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  1241. if (ret || (str_work[0] != '.'))
  1242. goto out;
  1243. str_work++;
  1244. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  1245. if (ret || (str_work[0] != '.'))
  1246. goto out;
  1247. str_work++;
  1248. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  1249. if (ret || (str_work[0] != '\0'))
  1250. goto out;
  1251. rc = 0;
  1252. out:
  1253. return rc;
  1254. }
  1255. static void __init no_auto_parse(void)
  1256. {
  1257. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  1258. char *parm, *str;
  1259. int rc;
  1260. str = no_auto;
  1261. while ((parm = strsep(&str, ","))) {
  1262. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  1263. &from_ssid, &from);
  1264. if (rc)
  1265. continue;
  1266. if (parm != NULL) {
  1267. rc = parse_busid(parm, &to_cssid,
  1268. &to_ssid, &to);
  1269. if ((from_ssid > to_ssid) ||
  1270. ((from_ssid == to_ssid) && (from > to)))
  1271. rc = -EINVAL;
  1272. } else {
  1273. to_cssid = from_cssid;
  1274. to_ssid = from_ssid;
  1275. to = from;
  1276. }
  1277. if (rc)
  1278. continue;
  1279. while ((from_ssid < to_ssid) ||
  1280. ((from_ssid == to_ssid) && (from <= to))) {
  1281. set_bit(from, devs_no_auto[from_ssid]);
  1282. from++;
  1283. if (from > __MAX_SUBCHANNEL) {
  1284. from_ssid++;
  1285. from = 0;
  1286. }
  1287. }
  1288. }
  1289. }
  1290. static int __init virtio_ccw_init(void)
  1291. {
  1292. /* parse no_auto string before we do anything further */
  1293. no_auto_parse();
  1294. return ccw_driver_register(&virtio_ccw_driver);
  1295. }
  1296. device_initcall(virtio_ccw_init);