virtio_ccw.c 40 KB

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