virtio_ccw.c 40 KB

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