virtio_ccw.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. /*
  2. * ccw based virtio transport
  3. *
  4. * Copyright IBM Corp. 2012
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License (version 2 only)
  8. * as published by the Free Software Foundation.
  9. *
  10. * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com>
  11. */
  12. #include <linux/kernel_stat.h>
  13. #include <linux/init.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/err.h>
  16. #include <linux/virtio.h>
  17. #include <linux/virtio_config.h>
  18. #include <linux/slab.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/virtio_ring.h>
  21. #include <linux/pfn.h>
  22. #include <linux/async.h>
  23. #include <linux/wait.h>
  24. #include <linux/list.h>
  25. #include <linux/bitops.h>
  26. #include <linux/module.h>
  27. #include <linux/io.h>
  28. #include <linux/kvm_para.h>
  29. #include <asm/setup.h>
  30. #include <asm/irq.h>
  31. #include <asm/cio.h>
  32. #include <asm/ccwdev.h>
  33. /*
  34. * virtio related functions
  35. */
  36. struct vq_config_block {
  37. __u16 index;
  38. __u16 num;
  39. } __packed;
  40. #define VIRTIO_CCW_CONFIG_SIZE 0x100
  41. /* same as PCI config space size, should be enough for all drivers */
  42. struct virtio_ccw_device {
  43. struct virtio_device vdev;
  44. __u8 *status;
  45. __u8 config[VIRTIO_CCW_CONFIG_SIZE];
  46. struct ccw_device *cdev;
  47. __u32 curr_io;
  48. int err;
  49. wait_queue_head_t wait_q;
  50. spinlock_t lock;
  51. struct list_head virtqueues;
  52. unsigned long indicators;
  53. unsigned long indicators2;
  54. struct vq_config_block *config_block;
  55. };
  56. struct vq_info_block {
  57. __u64 queue;
  58. __u32 align;
  59. __u16 index;
  60. __u16 num;
  61. } __packed;
  62. struct virtio_feature_desc {
  63. __u32 features;
  64. __u8 index;
  65. } __packed;
  66. struct virtio_ccw_vq_info {
  67. struct virtqueue *vq;
  68. int num;
  69. void *queue;
  70. struct vq_info_block *info_block;
  71. struct list_head node;
  72. };
  73. #define KVM_VIRTIO_CCW_RING_ALIGN 4096
  74. #define KVM_S390_VIRTIO_CCW_NOTIFY 3
  75. #define CCW_CMD_SET_VQ 0x13
  76. #define CCW_CMD_VDEV_RESET 0x33
  77. #define CCW_CMD_SET_IND 0x43
  78. #define CCW_CMD_SET_CONF_IND 0x53
  79. #define CCW_CMD_READ_FEAT 0x12
  80. #define CCW_CMD_WRITE_FEAT 0x11
  81. #define CCW_CMD_READ_CONF 0x22
  82. #define CCW_CMD_WRITE_CONF 0x21
  83. #define CCW_CMD_WRITE_STATUS 0x31
  84. #define CCW_CMD_READ_VQ_CONF 0x32
  85. #define VIRTIO_CCW_DOING_SET_VQ 0x00010000
  86. #define VIRTIO_CCW_DOING_RESET 0x00040000
  87. #define VIRTIO_CCW_DOING_READ_FEAT 0x00080000
  88. #define VIRTIO_CCW_DOING_WRITE_FEAT 0x00100000
  89. #define VIRTIO_CCW_DOING_READ_CONFIG 0x00200000
  90. #define VIRTIO_CCW_DOING_WRITE_CONFIG 0x00400000
  91. #define VIRTIO_CCW_DOING_WRITE_STATUS 0x00800000
  92. #define VIRTIO_CCW_DOING_SET_IND 0x01000000
  93. #define VIRTIO_CCW_DOING_READ_VQ_CONF 0x02000000
  94. #define VIRTIO_CCW_DOING_SET_CONF_IND 0x04000000
  95. #define VIRTIO_CCW_INTPARM_MASK 0xffff0000
  96. static struct virtio_ccw_device *to_vc_device(struct virtio_device *vdev)
  97. {
  98. return container_of(vdev, struct virtio_ccw_device, vdev);
  99. }
  100. static int doing_io(struct virtio_ccw_device *vcdev, __u32 flag)
  101. {
  102. unsigned long flags;
  103. __u32 ret;
  104. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  105. if (vcdev->err)
  106. ret = 0;
  107. else
  108. ret = vcdev->curr_io & flag;
  109. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  110. return ret;
  111. }
  112. static int ccw_io_helper(struct virtio_ccw_device *vcdev,
  113. struct ccw1 *ccw, __u32 intparm)
  114. {
  115. int ret;
  116. unsigned long flags;
  117. int flag = intparm & VIRTIO_CCW_INTPARM_MASK;
  118. do {
  119. spin_lock_irqsave(get_ccwdev_lock(vcdev->cdev), flags);
  120. ret = ccw_device_start(vcdev->cdev, ccw, intparm, 0, 0);
  121. if (!ret)
  122. vcdev->curr_io |= flag;
  123. spin_unlock_irqrestore(get_ccwdev_lock(vcdev->cdev), flags);
  124. cpu_relax();
  125. } while (ret == -EBUSY);
  126. wait_event(vcdev->wait_q, doing_io(vcdev, flag) == 0);
  127. return ret ? ret : vcdev->err;
  128. }
  129. static inline long do_kvm_notify(struct subchannel_id schid,
  130. unsigned long queue_index)
  131. {
  132. register unsigned long __nr asm("1") = KVM_S390_VIRTIO_CCW_NOTIFY;
  133. register struct subchannel_id __schid asm("2") = schid;
  134. register unsigned long __index asm("3") = queue_index;
  135. register long __rc asm("2");
  136. asm volatile ("diag 2,4,0x500\n"
  137. : "=d" (__rc) : "d" (__nr), "d" (__schid), "d" (__index)
  138. : "memory", "cc");
  139. return __rc;
  140. }
  141. static void virtio_ccw_kvm_notify(struct virtqueue *vq)
  142. {
  143. struct virtio_ccw_vq_info *info = vq->priv;
  144. struct virtio_ccw_device *vcdev;
  145. struct subchannel_id schid;
  146. vcdev = to_vc_device(info->vq->vdev);
  147. ccw_device_get_schid(vcdev->cdev, &schid);
  148. do_kvm_notify(schid, virtqueue_get_queue_index(vq));
  149. }
  150. static int virtio_ccw_read_vq_conf(struct virtio_ccw_device *vcdev,
  151. struct ccw1 *ccw, int index)
  152. {
  153. vcdev->config_block->index = index;
  154. ccw->cmd_code = CCW_CMD_READ_VQ_CONF;
  155. ccw->flags = 0;
  156. ccw->count = sizeof(struct vq_config_block);
  157. ccw->cda = (__u32)(unsigned long)(vcdev->config_block);
  158. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_VQ_CONF);
  159. return vcdev->config_block->num;
  160. }
  161. static void virtio_ccw_del_vq(struct virtqueue *vq, struct ccw1 *ccw)
  162. {
  163. struct virtio_ccw_device *vcdev = to_vc_device(vq->vdev);
  164. struct virtio_ccw_vq_info *info = vq->priv;
  165. unsigned long flags;
  166. unsigned long size;
  167. int ret;
  168. unsigned int index = virtqueue_get_queue_index(vq);
  169. /* Remove from our list. */
  170. spin_lock_irqsave(&vcdev->lock, flags);
  171. list_del(&info->node);
  172. spin_unlock_irqrestore(&vcdev->lock, flags);
  173. /* Release from host. */
  174. info->info_block->queue = 0;
  175. info->info_block->align = 0;
  176. info->info_block->index = index;
  177. info->info_block->num = 0;
  178. ccw->cmd_code = CCW_CMD_SET_VQ;
  179. ccw->flags = 0;
  180. ccw->count = sizeof(*info->info_block);
  181. ccw->cda = (__u32)(unsigned long)(info->info_block);
  182. ret = ccw_io_helper(vcdev, ccw,
  183. VIRTIO_CCW_DOING_SET_VQ | index);
  184. /*
  185. * -ENODEV isn't considered an error: The device is gone anyway.
  186. * This may happen on device detach.
  187. */
  188. if (ret && (ret != -ENODEV))
  189. dev_warn(&vq->vdev->dev, "Error %d while deleting queue %d",
  190. ret, index);
  191. vring_del_virtqueue(vq);
  192. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  193. free_pages_exact(info->queue, size);
  194. kfree(info->info_block);
  195. kfree(info);
  196. }
  197. static void virtio_ccw_del_vqs(struct virtio_device *vdev)
  198. {
  199. struct virtqueue *vq, *n;
  200. struct ccw1 *ccw;
  201. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  202. if (!ccw)
  203. return;
  204. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  205. virtio_ccw_del_vq(vq, ccw);
  206. kfree(ccw);
  207. }
  208. static struct virtqueue *virtio_ccw_setup_vq(struct virtio_device *vdev,
  209. int i, vq_callback_t *callback,
  210. const char *name,
  211. struct ccw1 *ccw)
  212. {
  213. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  214. int err;
  215. struct virtqueue *vq = NULL;
  216. struct virtio_ccw_vq_info *info;
  217. unsigned long size = 0; /* silence the compiler */
  218. unsigned long flags;
  219. /* Allocate queue. */
  220. info = kzalloc(sizeof(struct virtio_ccw_vq_info), GFP_KERNEL);
  221. if (!info) {
  222. dev_warn(&vcdev->cdev->dev, "no info\n");
  223. err = -ENOMEM;
  224. goto out_err;
  225. }
  226. info->info_block = kzalloc(sizeof(*info->info_block),
  227. GFP_DMA | GFP_KERNEL);
  228. if (!info->info_block) {
  229. dev_warn(&vcdev->cdev->dev, "no info block\n");
  230. err = -ENOMEM;
  231. goto out_err;
  232. }
  233. info->num = virtio_ccw_read_vq_conf(vcdev, ccw, i);
  234. size = PAGE_ALIGN(vring_size(info->num, KVM_VIRTIO_CCW_RING_ALIGN));
  235. info->queue = alloc_pages_exact(size, GFP_KERNEL | __GFP_ZERO);
  236. if (info->queue == NULL) {
  237. dev_warn(&vcdev->cdev->dev, "no queue\n");
  238. err = -ENOMEM;
  239. goto out_err;
  240. }
  241. vq = vring_new_virtqueue(i, info->num, KVM_VIRTIO_CCW_RING_ALIGN, vdev,
  242. true, info->queue, virtio_ccw_kvm_notify,
  243. callback, name);
  244. if (!vq) {
  245. /* For now, we fail if we can't get the requested size. */
  246. dev_warn(&vcdev->cdev->dev, "no vq\n");
  247. err = -ENOMEM;
  248. goto out_err;
  249. }
  250. /* Register it with the host. */
  251. info->info_block->queue = (__u64)info->queue;
  252. info->info_block->align = KVM_VIRTIO_CCW_RING_ALIGN;
  253. info->info_block->index = i;
  254. info->info_block->num = info->num;
  255. ccw->cmd_code = CCW_CMD_SET_VQ;
  256. ccw->flags = 0;
  257. ccw->count = sizeof(*info->info_block);
  258. ccw->cda = (__u32)(unsigned long)(info->info_block);
  259. err = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_VQ | i);
  260. if (err) {
  261. dev_warn(&vcdev->cdev->dev, "SET_VQ failed\n");
  262. goto out_err;
  263. }
  264. info->vq = vq;
  265. vq->priv = info;
  266. /* Save it to our list. */
  267. spin_lock_irqsave(&vcdev->lock, flags);
  268. list_add(&info->node, &vcdev->virtqueues);
  269. spin_unlock_irqrestore(&vcdev->lock, flags);
  270. return vq;
  271. out_err:
  272. if (vq)
  273. vring_del_virtqueue(vq);
  274. if (info) {
  275. if (info->queue)
  276. free_pages_exact(info->queue, size);
  277. kfree(info->info_block);
  278. }
  279. kfree(info);
  280. return ERR_PTR(err);
  281. }
  282. static int virtio_ccw_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  283. struct virtqueue *vqs[],
  284. vq_callback_t *callbacks[],
  285. const char *names[])
  286. {
  287. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  288. unsigned long *indicatorp = NULL;
  289. int ret, i;
  290. struct ccw1 *ccw;
  291. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  292. if (!ccw)
  293. return -ENOMEM;
  294. for (i = 0; i < nvqs; ++i) {
  295. vqs[i] = virtio_ccw_setup_vq(vdev, i, callbacks[i], names[i],
  296. ccw);
  297. if (IS_ERR(vqs[i])) {
  298. ret = PTR_ERR(vqs[i]);
  299. vqs[i] = NULL;
  300. goto out;
  301. }
  302. }
  303. ret = -ENOMEM;
  304. /* We need a data area under 2G to communicate. */
  305. indicatorp = kmalloc(sizeof(&vcdev->indicators), GFP_DMA | GFP_KERNEL);
  306. if (!indicatorp)
  307. goto out;
  308. *indicatorp = (unsigned long) &vcdev->indicators;
  309. /* Register queue indicators with host. */
  310. vcdev->indicators = 0;
  311. ccw->cmd_code = CCW_CMD_SET_IND;
  312. ccw->flags = 0;
  313. ccw->count = sizeof(vcdev->indicators);
  314. ccw->cda = (__u32)(unsigned long) indicatorp;
  315. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_IND);
  316. if (ret)
  317. goto out;
  318. /* Register indicators2 with host for config changes */
  319. *indicatorp = (unsigned long) &vcdev->indicators2;
  320. vcdev->indicators2 = 0;
  321. ccw->cmd_code = CCW_CMD_SET_CONF_IND;
  322. ccw->flags = 0;
  323. ccw->count = sizeof(vcdev->indicators2);
  324. ccw->cda = (__u32)(unsigned long) indicatorp;
  325. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_SET_CONF_IND);
  326. if (ret)
  327. goto out;
  328. kfree(indicatorp);
  329. kfree(ccw);
  330. return 0;
  331. out:
  332. kfree(indicatorp);
  333. kfree(ccw);
  334. virtio_ccw_del_vqs(vdev);
  335. return ret;
  336. }
  337. static void virtio_ccw_reset(struct virtio_device *vdev)
  338. {
  339. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  340. struct ccw1 *ccw;
  341. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  342. if (!ccw)
  343. return;
  344. /* Zero status bits. */
  345. *vcdev->status = 0;
  346. /* Send a reset ccw on device. */
  347. ccw->cmd_code = CCW_CMD_VDEV_RESET;
  348. ccw->flags = 0;
  349. ccw->count = 0;
  350. ccw->cda = 0;
  351. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_RESET);
  352. kfree(ccw);
  353. }
  354. static u32 virtio_ccw_get_features(struct virtio_device *vdev)
  355. {
  356. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  357. struct virtio_feature_desc *features;
  358. int ret, rc;
  359. struct ccw1 *ccw;
  360. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  361. if (!ccw)
  362. return 0;
  363. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  364. if (!features) {
  365. rc = 0;
  366. goto out_free;
  367. }
  368. /* Read the feature bits from the host. */
  369. /* TODO: Features > 32 bits */
  370. features->index = 0;
  371. ccw->cmd_code = CCW_CMD_READ_FEAT;
  372. ccw->flags = 0;
  373. ccw->count = sizeof(*features);
  374. ccw->cda = (__u32)(unsigned long)features;
  375. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_FEAT);
  376. if (ret) {
  377. rc = 0;
  378. goto out_free;
  379. }
  380. rc = le32_to_cpu(features->features);
  381. out_free:
  382. kfree(features);
  383. kfree(ccw);
  384. return rc;
  385. }
  386. static void virtio_ccw_finalize_features(struct virtio_device *vdev)
  387. {
  388. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  389. struct virtio_feature_desc *features;
  390. int i;
  391. struct ccw1 *ccw;
  392. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  393. if (!ccw)
  394. return;
  395. features = kzalloc(sizeof(*features), GFP_DMA | GFP_KERNEL);
  396. if (!features)
  397. goto out_free;
  398. /* Give virtio_ring a chance to accept features. */
  399. vring_transport_features(vdev);
  400. for (i = 0; i < sizeof(*vdev->features) / sizeof(features->features);
  401. i++) {
  402. int highbits = i % 2 ? 32 : 0;
  403. features->index = i;
  404. features->features = cpu_to_le32(vdev->features[i / 2]
  405. >> highbits);
  406. /* Write the feature bits to the host. */
  407. ccw->cmd_code = CCW_CMD_WRITE_FEAT;
  408. ccw->flags = 0;
  409. ccw->count = sizeof(*features);
  410. ccw->cda = (__u32)(unsigned long)features;
  411. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_FEAT);
  412. }
  413. out_free:
  414. kfree(features);
  415. kfree(ccw);
  416. }
  417. static void virtio_ccw_get_config(struct virtio_device *vdev,
  418. unsigned int offset, void *buf, unsigned len)
  419. {
  420. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  421. int ret;
  422. struct ccw1 *ccw;
  423. void *config_area;
  424. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  425. if (!ccw)
  426. return;
  427. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  428. if (!config_area)
  429. goto out_free;
  430. /* Read the config area from the host. */
  431. ccw->cmd_code = CCW_CMD_READ_CONF;
  432. ccw->flags = 0;
  433. ccw->count = offset + len;
  434. ccw->cda = (__u32)(unsigned long)config_area;
  435. ret = ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_READ_CONFIG);
  436. if (ret)
  437. goto out_free;
  438. memcpy(vcdev->config, config_area, sizeof(vcdev->config));
  439. memcpy(buf, &vcdev->config[offset], len);
  440. out_free:
  441. kfree(config_area);
  442. kfree(ccw);
  443. }
  444. static void virtio_ccw_set_config(struct virtio_device *vdev,
  445. unsigned int offset, const void *buf,
  446. unsigned len)
  447. {
  448. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  449. struct ccw1 *ccw;
  450. void *config_area;
  451. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  452. if (!ccw)
  453. return;
  454. config_area = kzalloc(VIRTIO_CCW_CONFIG_SIZE, GFP_DMA | GFP_KERNEL);
  455. if (!config_area)
  456. goto out_free;
  457. memcpy(&vcdev->config[offset], buf, len);
  458. /* Write the config area to the host. */
  459. memcpy(config_area, vcdev->config, sizeof(vcdev->config));
  460. ccw->cmd_code = CCW_CMD_WRITE_CONF;
  461. ccw->flags = 0;
  462. ccw->count = offset + len;
  463. ccw->cda = (__u32)(unsigned long)config_area;
  464. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_CONFIG);
  465. out_free:
  466. kfree(config_area);
  467. kfree(ccw);
  468. }
  469. static u8 virtio_ccw_get_status(struct virtio_device *vdev)
  470. {
  471. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  472. return *vcdev->status;
  473. }
  474. static void virtio_ccw_set_status(struct virtio_device *vdev, u8 status)
  475. {
  476. struct virtio_ccw_device *vcdev = to_vc_device(vdev);
  477. struct ccw1 *ccw;
  478. ccw = kzalloc(sizeof(*ccw), GFP_DMA | GFP_KERNEL);
  479. if (!ccw)
  480. return;
  481. /* Write the status to the host. */
  482. *vcdev->status = status;
  483. ccw->cmd_code = CCW_CMD_WRITE_STATUS;
  484. ccw->flags = 0;
  485. ccw->count = sizeof(status);
  486. ccw->cda = (__u32)(unsigned long)vcdev->status;
  487. ccw_io_helper(vcdev, ccw, VIRTIO_CCW_DOING_WRITE_STATUS);
  488. kfree(ccw);
  489. }
  490. static struct virtio_config_ops virtio_ccw_config_ops = {
  491. .get_features = virtio_ccw_get_features,
  492. .finalize_features = virtio_ccw_finalize_features,
  493. .get = virtio_ccw_get_config,
  494. .set = virtio_ccw_set_config,
  495. .get_status = virtio_ccw_get_status,
  496. .set_status = virtio_ccw_set_status,
  497. .reset = virtio_ccw_reset,
  498. .find_vqs = virtio_ccw_find_vqs,
  499. .del_vqs = virtio_ccw_del_vqs,
  500. };
  501. /*
  502. * ccw bus driver related functions
  503. */
  504. static void virtio_ccw_release_dev(struct device *_d)
  505. {
  506. struct virtio_device *dev = container_of(_d, struct virtio_device,
  507. dev);
  508. struct virtio_ccw_device *vcdev = to_vc_device(dev);
  509. kfree(vcdev->status);
  510. kfree(vcdev->config_block);
  511. kfree(vcdev);
  512. }
  513. static int irb_is_error(struct irb *irb)
  514. {
  515. if (scsw_cstat(&irb->scsw) != 0)
  516. return 1;
  517. if (scsw_dstat(&irb->scsw) & ~(DEV_STAT_CHN_END | DEV_STAT_DEV_END))
  518. return 1;
  519. if (scsw_cc(&irb->scsw) != 0)
  520. return 1;
  521. return 0;
  522. }
  523. static struct virtqueue *virtio_ccw_vq_by_ind(struct virtio_ccw_device *vcdev,
  524. int index)
  525. {
  526. struct virtio_ccw_vq_info *info;
  527. unsigned long flags;
  528. struct virtqueue *vq;
  529. vq = NULL;
  530. spin_lock_irqsave(&vcdev->lock, flags);
  531. list_for_each_entry(info, &vcdev->virtqueues, node) {
  532. if (virtqueue_get_queue_index(info->vq) == index) {
  533. vq = info->vq;
  534. break;
  535. }
  536. }
  537. spin_unlock_irqrestore(&vcdev->lock, flags);
  538. return vq;
  539. }
  540. static void virtio_ccw_int_handler(struct ccw_device *cdev,
  541. unsigned long intparm,
  542. struct irb *irb)
  543. {
  544. __u32 activity = intparm & VIRTIO_CCW_INTPARM_MASK;
  545. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  546. int i;
  547. struct virtqueue *vq;
  548. struct virtio_driver *drv;
  549. /* Check if it's a notification from the host. */
  550. if ((intparm == 0) &&
  551. (scsw_stctl(&irb->scsw) ==
  552. (SCSW_STCTL_ALERT_STATUS | SCSW_STCTL_STATUS_PEND))) {
  553. /* OK */
  554. }
  555. if (irb_is_error(irb))
  556. vcdev->err = -EIO; /* XXX - use real error */
  557. if (vcdev->curr_io & activity) {
  558. switch (activity) {
  559. case VIRTIO_CCW_DOING_READ_FEAT:
  560. case VIRTIO_CCW_DOING_WRITE_FEAT:
  561. case VIRTIO_CCW_DOING_READ_CONFIG:
  562. case VIRTIO_CCW_DOING_WRITE_CONFIG:
  563. case VIRTIO_CCW_DOING_WRITE_STATUS:
  564. case VIRTIO_CCW_DOING_SET_VQ:
  565. case VIRTIO_CCW_DOING_SET_IND:
  566. case VIRTIO_CCW_DOING_SET_CONF_IND:
  567. case VIRTIO_CCW_DOING_RESET:
  568. case VIRTIO_CCW_DOING_READ_VQ_CONF:
  569. vcdev->curr_io &= ~activity;
  570. wake_up(&vcdev->wait_q);
  571. break;
  572. default:
  573. /* don't know what to do... */
  574. dev_warn(&cdev->dev, "Suspicious activity '%08x'\n",
  575. activity);
  576. WARN_ON(1);
  577. break;
  578. }
  579. }
  580. for_each_set_bit(i, &vcdev->indicators,
  581. sizeof(vcdev->indicators) * BITS_PER_BYTE) {
  582. /* The bit clear must happen before the vring kick. */
  583. clear_bit(i, &vcdev->indicators);
  584. barrier();
  585. vq = virtio_ccw_vq_by_ind(vcdev, i);
  586. vring_interrupt(0, vq);
  587. }
  588. if (test_bit(0, &vcdev->indicators2)) {
  589. drv = container_of(vcdev->vdev.dev.driver,
  590. struct virtio_driver, driver);
  591. if (drv && drv->config_changed)
  592. drv->config_changed(&vcdev->vdev);
  593. clear_bit(0, &vcdev->indicators2);
  594. }
  595. }
  596. /*
  597. * We usually want to autoonline all devices, but give the admin
  598. * a way to exempt devices from this.
  599. */
  600. #define __DEV_WORDS ((__MAX_SUBCHANNEL + (8*sizeof(long) - 1)) / \
  601. (8*sizeof(long)))
  602. static unsigned long devs_no_auto[__MAX_SSID + 1][__DEV_WORDS];
  603. static char *no_auto = "";
  604. module_param(no_auto, charp, 0444);
  605. MODULE_PARM_DESC(no_auto, "list of ccw bus id ranges not to be auto-onlined");
  606. static int virtio_ccw_check_autoonline(struct ccw_device *cdev)
  607. {
  608. struct ccw_dev_id id;
  609. ccw_device_get_id(cdev, &id);
  610. if (test_bit(id.devno, devs_no_auto[id.ssid]))
  611. return 0;
  612. return 1;
  613. }
  614. static void virtio_ccw_auto_online(void *data, async_cookie_t cookie)
  615. {
  616. struct ccw_device *cdev = data;
  617. int ret;
  618. ret = ccw_device_set_online(cdev);
  619. if (ret)
  620. dev_warn(&cdev->dev, "Failed to set online: %d\n", ret);
  621. }
  622. static int virtio_ccw_probe(struct ccw_device *cdev)
  623. {
  624. cdev->handler = virtio_ccw_int_handler;
  625. if (virtio_ccw_check_autoonline(cdev))
  626. async_schedule(virtio_ccw_auto_online, cdev);
  627. return 0;
  628. }
  629. static void virtio_ccw_remove(struct ccw_device *cdev)
  630. {
  631. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  632. if (cdev->online) {
  633. unregister_virtio_device(&vcdev->vdev);
  634. dev_set_drvdata(&cdev->dev, NULL);
  635. }
  636. cdev->handler = NULL;
  637. }
  638. static int virtio_ccw_offline(struct ccw_device *cdev)
  639. {
  640. struct virtio_ccw_device *vcdev = dev_get_drvdata(&cdev->dev);
  641. unregister_virtio_device(&vcdev->vdev);
  642. dev_set_drvdata(&cdev->dev, NULL);
  643. return 0;
  644. }
  645. static int virtio_ccw_online(struct ccw_device *cdev)
  646. {
  647. int ret;
  648. struct virtio_ccw_device *vcdev;
  649. vcdev = kzalloc(sizeof(*vcdev), GFP_KERNEL);
  650. if (!vcdev) {
  651. dev_warn(&cdev->dev, "Could not get memory for virtio\n");
  652. ret = -ENOMEM;
  653. goto out_free;
  654. }
  655. vcdev->config_block = kzalloc(sizeof(*vcdev->config_block),
  656. GFP_DMA | GFP_KERNEL);
  657. if (!vcdev->config_block) {
  658. ret = -ENOMEM;
  659. goto out_free;
  660. }
  661. vcdev->status = kzalloc(sizeof(*vcdev->status), GFP_DMA | GFP_KERNEL);
  662. if (!vcdev->status) {
  663. ret = -ENOMEM;
  664. goto out_free;
  665. }
  666. vcdev->vdev.dev.parent = &cdev->dev;
  667. vcdev->vdev.dev.release = virtio_ccw_release_dev;
  668. vcdev->vdev.config = &virtio_ccw_config_ops;
  669. vcdev->cdev = cdev;
  670. init_waitqueue_head(&vcdev->wait_q);
  671. INIT_LIST_HEAD(&vcdev->virtqueues);
  672. spin_lock_init(&vcdev->lock);
  673. dev_set_drvdata(&cdev->dev, vcdev);
  674. vcdev->vdev.id.vendor = cdev->id.cu_type;
  675. vcdev->vdev.id.device = cdev->id.cu_model;
  676. ret = register_virtio_device(&vcdev->vdev);
  677. if (ret) {
  678. dev_warn(&cdev->dev, "Failed to register virtio device: %d\n",
  679. ret);
  680. goto out_put;
  681. }
  682. return 0;
  683. out_put:
  684. dev_set_drvdata(&cdev->dev, NULL);
  685. put_device(&vcdev->vdev.dev);
  686. return ret;
  687. out_free:
  688. if (vcdev) {
  689. kfree(vcdev->status);
  690. kfree(vcdev->config_block);
  691. }
  692. kfree(vcdev);
  693. return ret;
  694. }
  695. static int virtio_ccw_cio_notify(struct ccw_device *cdev, int event)
  696. {
  697. /* TODO: Check whether we need special handling here. */
  698. return 0;
  699. }
  700. static struct ccw_device_id virtio_ids[] = {
  701. { CCW_DEVICE(0x3832, 0) },
  702. {},
  703. };
  704. MODULE_DEVICE_TABLE(ccw, virtio_ids);
  705. static struct ccw_driver virtio_ccw_driver = {
  706. .driver = {
  707. .owner = THIS_MODULE,
  708. .name = "virtio_ccw",
  709. },
  710. .ids = virtio_ids,
  711. .probe = virtio_ccw_probe,
  712. .remove = virtio_ccw_remove,
  713. .set_offline = virtio_ccw_offline,
  714. .set_online = virtio_ccw_online,
  715. .notify = virtio_ccw_cio_notify,
  716. .int_class = IRQIO_VIR,
  717. };
  718. static int __init pure_hex(char **cp, unsigned int *val, int min_digit,
  719. int max_digit, int max_val)
  720. {
  721. int diff;
  722. diff = 0;
  723. *val = 0;
  724. while (diff <= max_digit) {
  725. int value = hex_to_bin(**cp);
  726. if (value < 0)
  727. break;
  728. *val = *val * 16 + value;
  729. (*cp)++;
  730. diff++;
  731. }
  732. if ((diff < min_digit) || (diff > max_digit) || (*val > max_val))
  733. return 1;
  734. return 0;
  735. }
  736. static int __init parse_busid(char *str, unsigned int *cssid,
  737. unsigned int *ssid, unsigned int *devno)
  738. {
  739. char *str_work;
  740. int rc, ret;
  741. rc = 1;
  742. if (*str == '\0')
  743. goto out;
  744. str_work = str;
  745. ret = pure_hex(&str_work, cssid, 1, 2, __MAX_CSSID);
  746. if (ret || (str_work[0] != '.'))
  747. goto out;
  748. str_work++;
  749. ret = pure_hex(&str_work, ssid, 1, 1, __MAX_SSID);
  750. if (ret || (str_work[0] != '.'))
  751. goto out;
  752. str_work++;
  753. ret = pure_hex(&str_work, devno, 4, 4, __MAX_SUBCHANNEL);
  754. if (ret || (str_work[0] != '\0'))
  755. goto out;
  756. rc = 0;
  757. out:
  758. return rc;
  759. }
  760. static void __init no_auto_parse(void)
  761. {
  762. unsigned int from_cssid, to_cssid, from_ssid, to_ssid, from, to;
  763. char *parm, *str;
  764. int rc;
  765. str = no_auto;
  766. while ((parm = strsep(&str, ","))) {
  767. rc = parse_busid(strsep(&parm, "-"), &from_cssid,
  768. &from_ssid, &from);
  769. if (rc)
  770. continue;
  771. if (parm != NULL) {
  772. rc = parse_busid(parm, &to_cssid,
  773. &to_ssid, &to);
  774. if ((from_ssid > to_ssid) ||
  775. ((from_ssid == to_ssid) && (from > to)))
  776. rc = -EINVAL;
  777. } else {
  778. to_cssid = from_cssid;
  779. to_ssid = from_ssid;
  780. to = from;
  781. }
  782. if (rc)
  783. continue;
  784. while ((from_ssid < to_ssid) ||
  785. ((from_ssid == to_ssid) && (from <= to))) {
  786. set_bit(from, devs_no_auto[from_ssid]);
  787. from++;
  788. if (from > __MAX_SUBCHANNEL) {
  789. from_ssid++;
  790. from = 0;
  791. }
  792. }
  793. }
  794. }
  795. static int __init virtio_ccw_init(void)
  796. {
  797. /* parse no_auto string before we do anything further */
  798. no_auto_parse();
  799. return ccw_driver_register(&virtio_ccw_driver);
  800. }
  801. module_init(virtio_ccw_init);
  802. static void __exit virtio_ccw_exit(void)
  803. {
  804. ccw_driver_unregister(&virtio_ccw_driver);
  805. }
  806. module_exit(virtio_ccw_exit);