xen-scsifront.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * Xen SCSI frontend driver
  3. *
  4. * Copyright (c) 2008, FUJITSU Limited
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version 2
  8. * as published by the Free Software Foundation; or, when distributed
  9. * separately from the Linux kernel or incorporated into other
  10. * software packages, subject to the following license:
  11. *
  12. * Permission is hereby granted, free of charge, to any person obtaining a copy
  13. * of this source file (the "Software"), to deal in the Software without
  14. * restriction, including without limitation the rights to use, copy, modify,
  15. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  16. * and to permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be included in
  20. * all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  25. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  26. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  27. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  28. * IN THE SOFTWARE.
  29. */
  30. #include <linux/module.h>
  31. #include <linux/kernel.h>
  32. #include <linux/device.h>
  33. #include <linux/wait.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/mutex.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/sched.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/pfn.h>
  40. #include <linux/slab.h>
  41. #include <linux/bitops.h>
  42. #include <scsi/scsi_cmnd.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi.h>
  45. #include <scsi/scsi_host.h>
  46. #include <xen/xen.h>
  47. #include <xen/xenbus.h>
  48. #include <xen/grant_table.h>
  49. #include <xen/events.h>
  50. #include <xen/page.h>
  51. #include <xen/interface/grant_table.h>
  52. #include <xen/interface/io/vscsiif.h>
  53. #include <xen/interface/io/protocols.h>
  54. #include <asm/xen/hypervisor.h>
  55. #define VSCSIFRONT_OP_ADD_LUN 1
  56. #define VSCSIFRONT_OP_DEL_LUN 2
  57. #define VSCSIFRONT_OP_READD_LUN 3
  58. /* Tuning point. */
  59. #define VSCSIIF_DEFAULT_CMD_PER_LUN 10
  60. #define VSCSIIF_MAX_TARGET 64
  61. #define VSCSIIF_MAX_LUN 255
  62. #define VSCSIIF_RING_SIZE __CONST_RING_SIZE(vscsiif, PAGE_SIZE)
  63. #define VSCSIIF_MAX_REQS VSCSIIF_RING_SIZE
  64. #define vscsiif_grants_sg(_sg) (PFN_UP((_sg) * \
  65. sizeof(struct scsiif_request_segment)))
  66. struct vscsifrnt_shadow {
  67. /* command between backend and frontend */
  68. unsigned char act;
  69. uint8_t nr_segments;
  70. uint16_t rqid;
  71. uint16_t ref_rqid;
  72. bool inflight;
  73. unsigned int nr_grants; /* number of grants in gref[] */
  74. struct scsiif_request_segment *sg; /* scatter/gather elements */
  75. struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];
  76. /* Do reset or abort function. */
  77. wait_queue_head_t wq_reset; /* reset work queue */
  78. int wait_reset; /* reset work queue condition */
  79. int32_t rslt_reset; /* reset response status: */
  80. /* SUCCESS or FAILED or: */
  81. #define RSLT_RESET_WAITING 0
  82. #define RSLT_RESET_ERR -1
  83. /* Requested struct scsi_cmnd is stored from kernel. */
  84. struct scsi_cmnd *sc;
  85. int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL];
  86. };
  87. struct vscsifrnt_info {
  88. struct xenbus_device *dev;
  89. struct Scsi_Host *host;
  90. enum {
  91. STATE_INACTIVE,
  92. STATE_ACTIVE,
  93. STATE_ERROR
  94. } host_active;
  95. unsigned int evtchn;
  96. unsigned int irq;
  97. grant_ref_t ring_ref;
  98. struct vscsiif_front_ring ring;
  99. struct vscsiif_response ring_rsp;
  100. spinlock_t shadow_lock;
  101. DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS);
  102. struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS];
  103. /* Following items are protected by the host lock. */
  104. wait_queue_head_t wq_sync;
  105. wait_queue_head_t wq_pause;
  106. unsigned int wait_ring_available:1;
  107. unsigned int waiting_pause:1;
  108. unsigned int pause:1;
  109. unsigned callers;
  110. char dev_state_path[64];
  111. struct task_struct *curr;
  112. };
  113. static DEFINE_MUTEX(scsifront_mutex);
  114. static void scsifront_wake_up(struct vscsifrnt_info *info)
  115. {
  116. info->wait_ring_available = 0;
  117. wake_up(&info->wq_sync);
  118. }
  119. static int scsifront_get_rqid(struct vscsifrnt_info *info)
  120. {
  121. unsigned long flags;
  122. int free;
  123. spin_lock_irqsave(&info->shadow_lock, flags);
  124. free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
  125. __clear_bit(free, info->shadow_free_bitmap);
  126. spin_unlock_irqrestore(&info->shadow_lock, flags);
  127. return free;
  128. }
  129. static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
  130. {
  131. int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
  132. __set_bit(id, info->shadow_free_bitmap);
  133. info->shadow[id] = NULL;
  134. return empty || info->wait_ring_available;
  135. }
  136. static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id)
  137. {
  138. unsigned long flags;
  139. int kick;
  140. spin_lock_irqsave(&info->shadow_lock, flags);
  141. kick = _scsifront_put_rqid(info, id);
  142. spin_unlock_irqrestore(&info->shadow_lock, flags);
  143. if (kick)
  144. scsifront_wake_up(info);
  145. }
  146. static int scsifront_do_request(struct vscsifrnt_info *info,
  147. struct vscsifrnt_shadow *shadow)
  148. {
  149. struct vscsiif_front_ring *ring = &(info->ring);
  150. struct vscsiif_request *ring_req;
  151. struct scsi_cmnd *sc = shadow->sc;
  152. uint32_t id;
  153. int i, notify;
  154. if (RING_FULL(&info->ring))
  155. return -EBUSY;
  156. id = scsifront_get_rqid(info); /* use id in response */
  157. if (id >= VSCSIIF_MAX_REQS)
  158. return -EBUSY;
  159. info->shadow[id] = shadow;
  160. shadow->rqid = id;
  161. ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt);
  162. ring->req_prod_pvt++;
  163. ring_req->rqid = id;
  164. ring_req->act = shadow->act;
  165. ring_req->ref_rqid = shadow->ref_rqid;
  166. ring_req->nr_segments = shadow->nr_segments;
  167. ring_req->id = sc->device->id;
  168. ring_req->lun = sc->device->lun;
  169. ring_req->channel = sc->device->channel;
  170. ring_req->cmd_len = sc->cmd_len;
  171. BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE);
  172. memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len);
  173. ring_req->sc_data_direction = (uint8_t)sc->sc_data_direction;
  174. ring_req->timeout_per_command = scsi_cmd_to_rq(sc)->timeout / HZ;
  175. for (i = 0; i < (shadow->nr_segments & ~VSCSIIF_SG_GRANT); i++)
  176. ring_req->seg[i] = shadow->seg[i];
  177. shadow->inflight = true;
  178. RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify);
  179. if (notify)
  180. notify_remote_via_irq(info->irq);
  181. return 0;
  182. }
  183. static void scsifront_set_error(struct vscsifrnt_info *info, const char *msg)
  184. {
  185. shost_printk(KERN_ERR, info->host, KBUILD_MODNAME "%s\n"
  186. "Disabling device for further use\n", msg);
  187. info->host_active = STATE_ERROR;
  188. }
  189. static void scsifront_gnttab_done(struct vscsifrnt_info *info,
  190. struct vscsifrnt_shadow *shadow)
  191. {
  192. int i;
  193. if (shadow->sc->sc_data_direction == DMA_NONE)
  194. return;
  195. for (i = 0; i < shadow->nr_grants; i++) {
  196. if (unlikely(!gnttab_try_end_foreign_access(shadow->gref[i]))) {
  197. scsifront_set_error(info, "grant still in use by backend");
  198. return;
  199. }
  200. }
  201. kfree(shadow->sg);
  202. }
  203. static unsigned int scsifront_host_byte(int32_t rslt)
  204. {
  205. switch (XEN_VSCSIIF_RSLT_HOST(rslt)) {
  206. case XEN_VSCSIIF_RSLT_HOST_OK:
  207. return DID_OK;
  208. case XEN_VSCSIIF_RSLT_HOST_NO_CONNECT:
  209. return DID_NO_CONNECT;
  210. case XEN_VSCSIIF_RSLT_HOST_BUS_BUSY:
  211. return DID_BUS_BUSY;
  212. case XEN_VSCSIIF_RSLT_HOST_TIME_OUT:
  213. return DID_TIME_OUT;
  214. case XEN_VSCSIIF_RSLT_HOST_BAD_TARGET:
  215. return DID_BAD_TARGET;
  216. case XEN_VSCSIIF_RSLT_HOST_ABORT:
  217. return DID_ABORT;
  218. case XEN_VSCSIIF_RSLT_HOST_PARITY:
  219. return DID_PARITY;
  220. case XEN_VSCSIIF_RSLT_HOST_ERROR:
  221. return DID_ERROR;
  222. case XEN_VSCSIIF_RSLT_HOST_RESET:
  223. return DID_RESET;
  224. case XEN_VSCSIIF_RSLT_HOST_BAD_INTR:
  225. return DID_BAD_INTR;
  226. case XEN_VSCSIIF_RSLT_HOST_PASSTHROUGH:
  227. return DID_PASSTHROUGH;
  228. case XEN_VSCSIIF_RSLT_HOST_SOFT_ERROR:
  229. return DID_SOFT_ERROR;
  230. case XEN_VSCSIIF_RSLT_HOST_IMM_RETRY:
  231. return DID_IMM_RETRY;
  232. case XEN_VSCSIIF_RSLT_HOST_REQUEUE:
  233. return DID_REQUEUE;
  234. case XEN_VSCSIIF_RSLT_HOST_TRANSPORT_DISRUPTED:
  235. return DID_TRANSPORT_DISRUPTED;
  236. case XEN_VSCSIIF_RSLT_HOST_TRANSPORT_FAILFAST:
  237. return DID_TRANSPORT_FAILFAST;
  238. case XEN_VSCSIIF_RSLT_HOST_TRANSPORT_MARGINAL:
  239. return DID_TRANSPORT_MARGINAL;
  240. default:
  241. return DID_ERROR;
  242. }
  243. }
  244. static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info,
  245. struct vscsiif_response *ring_rsp)
  246. {
  247. struct vscsifrnt_shadow *shadow;
  248. struct scsi_cmnd *sc;
  249. uint32_t id;
  250. uint8_t sense_len;
  251. id = ring_rsp->rqid;
  252. shadow = info->shadow[id];
  253. sc = shadow->sc;
  254. BUG_ON(sc == NULL);
  255. scsifront_gnttab_done(info, shadow);
  256. if (info->host_active == STATE_ERROR)
  257. return;
  258. scsifront_put_rqid(info, id);
  259. set_host_byte(sc, scsifront_host_byte(ring_rsp->rslt));
  260. set_status_byte(sc, XEN_VSCSIIF_RSLT_STATUS(ring_rsp->rslt));
  261. scsi_set_resid(sc, ring_rsp->residual_len);
  262. sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE,
  263. ring_rsp->sense_len);
  264. if (sense_len)
  265. memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len);
  266. scsi_done(sc);
  267. }
  268. static void scsifront_sync_cmd_done(struct vscsifrnt_info *info,
  269. struct vscsiif_response *ring_rsp)
  270. {
  271. uint16_t id = ring_rsp->rqid;
  272. unsigned long flags;
  273. struct vscsifrnt_shadow *shadow = info->shadow[id];
  274. int kick;
  275. spin_lock_irqsave(&info->shadow_lock, flags);
  276. shadow->wait_reset = 1;
  277. switch (shadow->rslt_reset) {
  278. case RSLT_RESET_WAITING:
  279. if (ring_rsp->rslt == XEN_VSCSIIF_RSLT_RESET_SUCCESS)
  280. shadow->rslt_reset = SUCCESS;
  281. else
  282. shadow->rslt_reset = FAILED;
  283. break;
  284. case RSLT_RESET_ERR:
  285. kick = _scsifront_put_rqid(info, id);
  286. spin_unlock_irqrestore(&info->shadow_lock, flags);
  287. kfree(shadow);
  288. if (kick)
  289. scsifront_wake_up(info);
  290. return;
  291. default:
  292. scsifront_set_error(info, "bad reset state");
  293. break;
  294. }
  295. spin_unlock_irqrestore(&info->shadow_lock, flags);
  296. wake_up(&shadow->wq_reset);
  297. }
  298. static void scsifront_do_response(struct vscsifrnt_info *info,
  299. struct vscsiif_response *ring_rsp)
  300. {
  301. struct vscsifrnt_shadow *shadow;
  302. if (ring_rsp->rqid >= VSCSIIF_MAX_REQS ||
  303. !info->shadow[ring_rsp->rqid]->inflight) {
  304. scsifront_set_error(info, "illegal rqid returned by backend!");
  305. return;
  306. }
  307. shadow = info->shadow[ring_rsp->rqid];
  308. shadow->inflight = false;
  309. if (shadow->act == VSCSIIF_ACT_SCSI_CDB)
  310. scsifront_cdb_cmd_done(info, ring_rsp);
  311. else
  312. scsifront_sync_cmd_done(info, ring_rsp);
  313. }
  314. static int scsifront_ring_drain(struct vscsifrnt_info *info,
  315. unsigned int *eoiflag)
  316. {
  317. struct vscsiif_response ring_rsp;
  318. RING_IDX i, rp;
  319. int more_to_do = 0;
  320. rp = READ_ONCE(info->ring.sring->rsp_prod);
  321. virt_rmb(); /* ordering required respective to backend */
  322. if (RING_RESPONSE_PROD_OVERFLOW(&info->ring, rp)) {
  323. scsifront_set_error(info, "illegal number of responses");
  324. return 0;
  325. }
  326. for (i = info->ring.rsp_cons; i != rp; i++) {
  327. RING_COPY_RESPONSE(&info->ring, i, &ring_rsp);
  328. scsifront_do_response(info, &ring_rsp);
  329. if (info->host_active == STATE_ERROR)
  330. return 0;
  331. *eoiflag &= ~XEN_EOI_FLAG_SPURIOUS;
  332. }
  333. info->ring.rsp_cons = i;
  334. if (i != info->ring.req_prod_pvt)
  335. RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do);
  336. else
  337. info->ring.sring->rsp_event = i + 1;
  338. return more_to_do;
  339. }
  340. static int scsifront_cmd_done(struct vscsifrnt_info *info,
  341. unsigned int *eoiflag)
  342. {
  343. int more_to_do;
  344. unsigned long flags;
  345. spin_lock_irqsave(info->host->host_lock, flags);
  346. more_to_do = scsifront_ring_drain(info, eoiflag);
  347. info->wait_ring_available = 0;
  348. spin_unlock_irqrestore(info->host->host_lock, flags);
  349. wake_up(&info->wq_sync);
  350. return more_to_do;
  351. }
  352. static irqreturn_t scsifront_irq_fn(int irq, void *dev_id)
  353. {
  354. struct vscsifrnt_info *info = dev_id;
  355. unsigned int eoiflag = XEN_EOI_FLAG_SPURIOUS;
  356. if (info->host_active == STATE_ERROR) {
  357. xen_irq_lateeoi(irq, XEN_EOI_FLAG_SPURIOUS);
  358. return IRQ_HANDLED;
  359. }
  360. while (scsifront_cmd_done(info, &eoiflag))
  361. /* Yield point for this unbounded loop. */
  362. cond_resched();
  363. xen_irq_lateeoi(irq, eoiflag);
  364. return IRQ_HANDLED;
  365. }
  366. static void scsifront_finish_all(struct vscsifrnt_info *info)
  367. {
  368. unsigned int i, dummy;
  369. struct vscsiif_response resp;
  370. scsifront_ring_drain(info, &dummy);
  371. for (i = 0; i < VSCSIIF_MAX_REQS; i++) {
  372. if (test_bit(i, info->shadow_free_bitmap))
  373. continue;
  374. resp.rqid = i;
  375. resp.sense_len = 0;
  376. resp.rslt = DID_RESET << 16;
  377. resp.residual_len = 0;
  378. scsifront_do_response(info, &resp);
  379. }
  380. }
  381. static int map_data_for_request(struct vscsifrnt_info *info,
  382. struct scsi_cmnd *sc,
  383. struct vscsifrnt_shadow *shadow)
  384. {
  385. grant_ref_t gref_head;
  386. struct page *page;
  387. int err, ref, ref_cnt = 0;
  388. int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE);
  389. unsigned int i, off, len, bytes;
  390. unsigned int data_len = scsi_bufflen(sc);
  391. unsigned int data_grants = 0, seg_grants = 0;
  392. struct scatterlist *sg;
  393. struct scsiif_request_segment *seg;
  394. if (sc->sc_data_direction == DMA_NONE || !data_len)
  395. return 0;
  396. scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i)
  397. data_grants += PFN_UP(sg->offset + sg->length);
  398. if (data_grants > VSCSIIF_SG_TABLESIZE) {
  399. if (data_grants > info->host->sg_tablesize) {
  400. shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
  401. "Unable to map request_buffer for command!\n");
  402. return -E2BIG;
  403. }
  404. seg_grants = vscsiif_grants_sg(data_grants);
  405. shadow->sg = kcalloc(data_grants,
  406. sizeof(struct scsiif_request_segment), GFP_ATOMIC);
  407. if (!shadow->sg)
  408. return -ENOMEM;
  409. }
  410. seg = shadow->sg ? : shadow->seg;
  411. err = gnttab_alloc_grant_references(seg_grants + data_grants,
  412. &gref_head);
  413. if (err) {
  414. kfree(shadow->sg);
  415. shost_printk(KERN_ERR, info->host, KBUILD_MODNAME
  416. "gnttab_alloc_grant_references() error\n");
  417. return -ENOMEM;
  418. }
  419. if (seg_grants) {
  420. page = virt_to_page(seg);
  421. off = offset_in_page(seg);
  422. len = sizeof(struct scsiif_request_segment) * data_grants;
  423. while (len > 0) {
  424. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  425. ref = gnttab_claim_grant_reference(&gref_head);
  426. BUG_ON(ref == -ENOSPC);
  427. gnttab_grant_foreign_access_ref(ref,
  428. info->dev->otherend_id,
  429. xen_page_to_gfn(page), 1);
  430. shadow->gref[ref_cnt] = ref;
  431. shadow->seg[ref_cnt].gref = ref;
  432. shadow->seg[ref_cnt].offset = (uint16_t)off;
  433. shadow->seg[ref_cnt].length = (uint16_t)bytes;
  434. page++;
  435. len -= bytes;
  436. off = 0;
  437. ref_cnt++;
  438. }
  439. BUG_ON(seg_grants < ref_cnt);
  440. seg_grants = ref_cnt;
  441. }
  442. scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) {
  443. page = sg_page(sg);
  444. off = sg->offset;
  445. len = sg->length;
  446. while (len > 0 && data_len > 0) {
  447. /*
  448. * sg sends a scatterlist that is larger than
  449. * the data_len it wants transferred for certain
  450. * IO sizes.
  451. */
  452. bytes = min_t(unsigned int, len, PAGE_SIZE - off);
  453. bytes = min(bytes, data_len);
  454. ref = gnttab_claim_grant_reference(&gref_head);
  455. BUG_ON(ref == -ENOSPC);
  456. gnttab_grant_foreign_access_ref(ref,
  457. info->dev->otherend_id,
  458. xen_page_to_gfn(page),
  459. grant_ro);
  460. shadow->gref[ref_cnt] = ref;
  461. seg->gref = ref;
  462. seg->offset = (uint16_t)off;
  463. seg->length = (uint16_t)bytes;
  464. page++;
  465. seg++;
  466. len -= bytes;
  467. data_len -= bytes;
  468. off = 0;
  469. ref_cnt++;
  470. }
  471. }
  472. if (seg_grants)
  473. shadow->nr_segments = VSCSIIF_SG_GRANT | seg_grants;
  474. else
  475. shadow->nr_segments = (uint8_t)ref_cnt;
  476. shadow->nr_grants = ref_cnt;
  477. return 0;
  478. }
  479. static int scsifront_enter(struct vscsifrnt_info *info)
  480. {
  481. if (info->pause)
  482. return 1;
  483. info->callers++;
  484. return 0;
  485. }
  486. static void scsifront_return(struct vscsifrnt_info *info)
  487. {
  488. info->callers--;
  489. if (info->callers)
  490. return;
  491. if (!info->waiting_pause)
  492. return;
  493. info->waiting_pause = 0;
  494. wake_up(&info->wq_pause);
  495. }
  496. static int scsifront_queuecommand(struct Scsi_Host *shost,
  497. struct scsi_cmnd *sc)
  498. {
  499. struct vscsifrnt_info *info = shost_priv(shost);
  500. struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc);
  501. unsigned long flags;
  502. int err;
  503. if (info->host_active == STATE_ERROR)
  504. return SCSI_MLQUEUE_HOST_BUSY;
  505. sc->result = 0;
  506. shadow->sc = sc;
  507. shadow->act = VSCSIIF_ACT_SCSI_CDB;
  508. spin_lock_irqsave(shost->host_lock, flags);
  509. if (scsifront_enter(info)) {
  510. spin_unlock_irqrestore(shost->host_lock, flags);
  511. return SCSI_MLQUEUE_HOST_BUSY;
  512. }
  513. err = map_data_for_request(info, sc, shadow);
  514. if (err < 0) {
  515. pr_debug("%s: err %d\n", __func__, err);
  516. scsifront_return(info);
  517. spin_unlock_irqrestore(shost->host_lock, flags);
  518. if (err == -ENOMEM)
  519. return SCSI_MLQUEUE_HOST_BUSY;
  520. sc->result = DID_ERROR << 16;
  521. scsi_done(sc);
  522. return 0;
  523. }
  524. if (scsifront_do_request(info, shadow)) {
  525. scsifront_gnttab_done(info, shadow);
  526. goto busy;
  527. }
  528. scsifront_return(info);
  529. spin_unlock_irqrestore(shost->host_lock, flags);
  530. return 0;
  531. busy:
  532. scsifront_return(info);
  533. spin_unlock_irqrestore(shost->host_lock, flags);
  534. pr_debug("%s: busy\n", __func__);
  535. return SCSI_MLQUEUE_HOST_BUSY;
  536. }
  537. /*
  538. * Any exception handling (reset or abort) must be forwarded to the backend.
  539. * We have to wait until an answer is returned. This answer contains the
  540. * result to be returned to the requestor.
  541. */
  542. static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act)
  543. {
  544. struct Scsi_Host *host = sc->device->host;
  545. struct vscsifrnt_info *info = shost_priv(host);
  546. struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc);
  547. int err = 0;
  548. if (info->host_active == STATE_ERROR)
  549. return FAILED;
  550. shadow = kzalloc(sizeof(*shadow), GFP_NOIO);
  551. if (!shadow)
  552. return FAILED;
  553. shadow->act = act;
  554. shadow->rslt_reset = RSLT_RESET_WAITING;
  555. shadow->sc = sc;
  556. shadow->ref_rqid = s->rqid;
  557. init_waitqueue_head(&shadow->wq_reset);
  558. spin_lock_irq(host->host_lock);
  559. for (;;) {
  560. if (scsifront_enter(info))
  561. goto fail;
  562. if (!scsifront_do_request(info, shadow))
  563. break;
  564. scsifront_return(info);
  565. if (err)
  566. goto fail;
  567. info->wait_ring_available = 1;
  568. spin_unlock_irq(host->host_lock);
  569. err = wait_event_interruptible(info->wq_sync,
  570. !info->wait_ring_available);
  571. spin_lock_irq(host->host_lock);
  572. }
  573. spin_unlock_irq(host->host_lock);
  574. err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset);
  575. spin_lock_irq(host->host_lock);
  576. if (!err) {
  577. err = shadow->rslt_reset;
  578. scsifront_put_rqid(info, shadow->rqid);
  579. kfree(shadow);
  580. } else {
  581. spin_lock(&info->shadow_lock);
  582. shadow->rslt_reset = RSLT_RESET_ERR;
  583. spin_unlock(&info->shadow_lock);
  584. err = FAILED;
  585. }
  586. scsifront_return(info);
  587. spin_unlock_irq(host->host_lock);
  588. return err;
  589. fail:
  590. spin_unlock_irq(host->host_lock);
  591. kfree(shadow);
  592. return FAILED;
  593. }
  594. static int scsifront_eh_abort_handler(struct scsi_cmnd *sc)
  595. {
  596. pr_debug("%s\n", __func__);
  597. return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT);
  598. }
  599. static int scsifront_dev_reset_handler(struct scsi_cmnd *sc)
  600. {
  601. pr_debug("%s\n", __func__);
  602. return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET);
  603. }
  604. static int scsifront_sdev_configure(struct scsi_device *sdev)
  605. {
  606. struct vscsifrnt_info *info = shost_priv(sdev->host);
  607. int err;
  608. if (info->host_active == STATE_ERROR)
  609. return -EIO;
  610. if (current == info->curr) {
  611. err = xenbus_printf(XBT_NIL, info->dev->nodename,
  612. info->dev_state_path, "%d", XenbusStateConnected);
  613. if (err) {
  614. xenbus_dev_error(info->dev, err,
  615. "%s: writing dev_state_path", __func__);
  616. return err;
  617. }
  618. }
  619. return 0;
  620. }
  621. static void scsifront_sdev_destroy(struct scsi_device *sdev)
  622. {
  623. struct vscsifrnt_info *info = shost_priv(sdev->host);
  624. int err;
  625. if (current == info->curr) {
  626. err = xenbus_printf(XBT_NIL, info->dev->nodename,
  627. info->dev_state_path, "%d", XenbusStateClosed);
  628. if (err)
  629. xenbus_dev_error(info->dev, err,
  630. "%s: writing dev_state_path", __func__);
  631. }
  632. }
  633. static const struct scsi_host_template scsifront_sht = {
  634. .module = THIS_MODULE,
  635. .name = "Xen SCSI frontend driver",
  636. .queuecommand = scsifront_queuecommand,
  637. .eh_abort_handler = scsifront_eh_abort_handler,
  638. .eh_device_reset_handler = scsifront_dev_reset_handler,
  639. .slave_configure = scsifront_sdev_configure,
  640. .slave_destroy = scsifront_sdev_destroy,
  641. .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN,
  642. .can_queue = VSCSIIF_MAX_REQS,
  643. .this_id = -1,
  644. .cmd_size = sizeof(struct vscsifrnt_shadow),
  645. .sg_tablesize = VSCSIIF_SG_TABLESIZE,
  646. .proc_name = "scsifront",
  647. };
  648. static int scsifront_alloc_ring(struct vscsifrnt_info *info)
  649. {
  650. struct xenbus_device *dev = info->dev;
  651. struct vscsiif_sring *sring;
  652. int err;
  653. /***** Frontend to Backend ring start *****/
  654. err = xenbus_setup_ring(dev, GFP_KERNEL, (void **)&sring, 1,
  655. &info->ring_ref);
  656. if (err)
  657. return err;
  658. XEN_FRONT_RING_INIT(&info->ring, sring, PAGE_SIZE);
  659. err = xenbus_alloc_evtchn(dev, &info->evtchn);
  660. if (err) {
  661. xenbus_dev_fatal(dev, err, "xenbus_alloc_evtchn");
  662. goto free_gnttab;
  663. }
  664. err = bind_evtchn_to_irq_lateeoi(info->evtchn);
  665. if (err <= 0) {
  666. xenbus_dev_fatal(dev, err, "bind_evtchn_to_irq");
  667. goto free_gnttab;
  668. }
  669. info->irq = err;
  670. err = request_threaded_irq(info->irq, NULL, scsifront_irq_fn,
  671. IRQF_ONESHOT, "scsifront", info);
  672. if (err) {
  673. xenbus_dev_fatal(dev, err, "request_threaded_irq");
  674. goto free_irq;
  675. }
  676. return 0;
  677. /* free resource */
  678. free_irq:
  679. unbind_from_irqhandler(info->irq, info);
  680. free_gnttab:
  681. xenbus_teardown_ring((void **)&sring, 1, &info->ring_ref);
  682. return err;
  683. }
  684. static void scsifront_free_ring(struct vscsifrnt_info *info)
  685. {
  686. unbind_from_irqhandler(info->irq, info);
  687. xenbus_teardown_ring((void **)&info->ring.sring, 1, &info->ring_ref);
  688. }
  689. static int scsifront_init_ring(struct vscsifrnt_info *info)
  690. {
  691. struct xenbus_device *dev = info->dev;
  692. struct xenbus_transaction xbt;
  693. int err;
  694. pr_debug("%s\n", __func__);
  695. err = scsifront_alloc_ring(info);
  696. if (err)
  697. return err;
  698. pr_debug("%s: %u %u\n", __func__, info->ring_ref, info->evtchn);
  699. again:
  700. err = xenbus_transaction_start(&xbt);
  701. if (err)
  702. xenbus_dev_fatal(dev, err, "starting transaction");
  703. err = xenbus_printf(xbt, dev->nodename, "ring-ref", "%u",
  704. info->ring_ref);
  705. if (err) {
  706. xenbus_dev_fatal(dev, err, "%s", "writing ring-ref");
  707. goto fail;
  708. }
  709. err = xenbus_printf(xbt, dev->nodename, "event-channel", "%u",
  710. info->evtchn);
  711. if (err) {
  712. xenbus_dev_fatal(dev, err, "%s", "writing event-channel");
  713. goto fail;
  714. }
  715. err = xenbus_transaction_end(xbt, 0);
  716. if (err) {
  717. if (err == -EAGAIN)
  718. goto again;
  719. xenbus_dev_fatal(dev, err, "completing transaction");
  720. goto free_sring;
  721. }
  722. return 0;
  723. fail:
  724. xenbus_transaction_end(xbt, 1);
  725. free_sring:
  726. scsifront_free_ring(info);
  727. return err;
  728. }
  729. static int scsifront_probe(struct xenbus_device *dev,
  730. const struct xenbus_device_id *id)
  731. {
  732. struct vscsifrnt_info *info;
  733. struct Scsi_Host *host;
  734. int err = -ENOMEM;
  735. char name[TASK_COMM_LEN];
  736. host = scsi_host_alloc(&scsifront_sht, sizeof(*info));
  737. if (!host) {
  738. xenbus_dev_fatal(dev, err, "fail to allocate scsi host");
  739. return err;
  740. }
  741. info = shost_priv(host);
  742. dev_set_drvdata(&dev->dev, info);
  743. info->dev = dev;
  744. bitmap_fill(info->shadow_free_bitmap, VSCSIIF_MAX_REQS);
  745. err = scsifront_init_ring(info);
  746. if (err) {
  747. scsi_host_put(host);
  748. return err;
  749. }
  750. init_waitqueue_head(&info->wq_sync);
  751. init_waitqueue_head(&info->wq_pause);
  752. spin_lock_init(&info->shadow_lock);
  753. snprintf(name, TASK_COMM_LEN, "vscsiif.%d", host->host_no);
  754. host->max_id = VSCSIIF_MAX_TARGET;
  755. host->max_channel = 0;
  756. host->max_lun = VSCSIIF_MAX_LUN;
  757. host->max_sectors = (host->sg_tablesize - 1) * PAGE_SIZE / 512;
  758. host->max_cmd_len = VSCSIIF_MAX_COMMAND_SIZE;
  759. err = scsi_add_host(host, &dev->dev);
  760. if (err) {
  761. dev_err(&dev->dev, "fail to add scsi host %d\n", err);
  762. goto free_sring;
  763. }
  764. info->host = host;
  765. info->host_active = STATE_ACTIVE;
  766. xenbus_switch_state(dev, XenbusStateInitialised);
  767. return 0;
  768. free_sring:
  769. scsifront_free_ring(info);
  770. scsi_host_put(host);
  771. return err;
  772. }
  773. static int scsifront_resume(struct xenbus_device *dev)
  774. {
  775. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  776. struct Scsi_Host *host = info->host;
  777. int err;
  778. spin_lock_irq(host->host_lock);
  779. /* Finish all still pending commands. */
  780. scsifront_finish_all(info);
  781. spin_unlock_irq(host->host_lock);
  782. /* Reconnect to dom0. */
  783. scsifront_free_ring(info);
  784. err = scsifront_init_ring(info);
  785. if (err) {
  786. dev_err(&dev->dev, "fail to resume %d\n", err);
  787. scsi_host_put(host);
  788. return err;
  789. }
  790. xenbus_switch_state(dev, XenbusStateInitialised);
  791. return 0;
  792. }
  793. static int scsifront_suspend(struct xenbus_device *dev)
  794. {
  795. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  796. struct Scsi_Host *host = info->host;
  797. int err = 0;
  798. /* No new commands for the backend. */
  799. spin_lock_irq(host->host_lock);
  800. info->pause = 1;
  801. while (info->callers && !err) {
  802. info->waiting_pause = 1;
  803. info->wait_ring_available = 0;
  804. spin_unlock_irq(host->host_lock);
  805. wake_up(&info->wq_sync);
  806. err = wait_event_interruptible(info->wq_pause,
  807. !info->waiting_pause);
  808. spin_lock_irq(host->host_lock);
  809. }
  810. spin_unlock_irq(host->host_lock);
  811. return err;
  812. }
  813. static void scsifront_remove(struct xenbus_device *dev)
  814. {
  815. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  816. pr_debug("%s: %s removed\n", __func__, dev->nodename);
  817. mutex_lock(&scsifront_mutex);
  818. if (info->host_active != STATE_INACTIVE) {
  819. /* Scsi_host not yet removed */
  820. scsi_remove_host(info->host);
  821. info->host_active = STATE_INACTIVE;
  822. }
  823. mutex_unlock(&scsifront_mutex);
  824. scsifront_free_ring(info);
  825. scsi_host_put(info->host);
  826. }
  827. static void scsifront_disconnect(struct vscsifrnt_info *info)
  828. {
  829. struct xenbus_device *dev = info->dev;
  830. struct Scsi_Host *host = info->host;
  831. pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
  832. /*
  833. * When this function is executed, all devices of
  834. * Frontend have been deleted.
  835. * Therefore, it need not block I/O before remove_host.
  836. */
  837. mutex_lock(&scsifront_mutex);
  838. if (info->host_active != STATE_INACTIVE) {
  839. scsi_remove_host(host);
  840. info->host_active = STATE_INACTIVE;
  841. }
  842. mutex_unlock(&scsifront_mutex);
  843. xenbus_frontend_closed(dev);
  844. }
  845. static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
  846. {
  847. struct xenbus_device *dev = info->dev;
  848. int i, err = 0;
  849. char str[64];
  850. char **dir;
  851. unsigned int dir_n = 0;
  852. unsigned int device_state;
  853. unsigned int hst, chn, tgt, lun;
  854. struct scsi_device *sdev;
  855. if (info->host_active == STATE_ERROR)
  856. return;
  857. dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
  858. if (IS_ERR(dir))
  859. return;
  860. /* mark current task as the one allowed to modify device states */
  861. BUG_ON(info->curr);
  862. info->curr = current;
  863. for (i = 0; i < dir_n; i++) {
  864. /* read status */
  865. snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
  866. err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
  867. &device_state);
  868. if (XENBUS_EXIST_ERR(err))
  869. continue;
  870. /* virtual SCSI device */
  871. snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
  872. err = xenbus_scanf(XBT_NIL, dev->otherend, str,
  873. "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
  874. if (XENBUS_EXIST_ERR(err))
  875. continue;
  876. /*
  877. * Front device state path, used in slave_configure called
  878. * on successfull scsi_add_device, and in slave_destroy called
  879. * on remove of a device.
  880. */
  881. snprintf(info->dev_state_path, sizeof(info->dev_state_path),
  882. "vscsi-devs/%s/state", dir[i]);
  883. switch (op) {
  884. case VSCSIFRONT_OP_ADD_LUN:
  885. if (device_state != XenbusStateInitialised)
  886. break;
  887. if (scsi_add_device(info->host, chn, tgt, lun)) {
  888. dev_err(&dev->dev, "scsi_add_device\n");
  889. err = xenbus_printf(XBT_NIL, dev->nodename,
  890. info->dev_state_path,
  891. "%d", XenbusStateClosed);
  892. if (err)
  893. xenbus_dev_error(dev, err,
  894. "%s: writing dev_state_path", __func__);
  895. }
  896. break;
  897. case VSCSIFRONT_OP_DEL_LUN:
  898. if (device_state != XenbusStateClosing)
  899. break;
  900. sdev = scsi_device_lookup(info->host, chn, tgt, lun);
  901. if (sdev) {
  902. scsi_remove_device(sdev);
  903. scsi_device_put(sdev);
  904. }
  905. break;
  906. case VSCSIFRONT_OP_READD_LUN:
  907. if (device_state == XenbusStateConnected) {
  908. err = xenbus_printf(XBT_NIL, dev->nodename,
  909. info->dev_state_path,
  910. "%d", XenbusStateConnected);
  911. if (err)
  912. xenbus_dev_error(dev, err,
  913. "%s: writing dev_state_path", __func__);
  914. }
  915. break;
  916. default:
  917. break;
  918. }
  919. }
  920. info->curr = NULL;
  921. kfree(dir);
  922. }
  923. static void scsifront_read_backend_params(struct xenbus_device *dev,
  924. struct vscsifrnt_info *info)
  925. {
  926. unsigned int sg_grant, nr_segs;
  927. struct Scsi_Host *host = info->host;
  928. sg_grant = xenbus_read_unsigned(dev->otherend, "feature-sg-grant", 0);
  929. nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
  930. nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
  931. nr_segs = min_t(unsigned int, nr_segs,
  932. VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
  933. sizeof(struct scsiif_request_segment));
  934. if (!info->pause && sg_grant)
  935. dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
  936. else if (info->pause && nr_segs < host->sg_tablesize)
  937. dev_warn(&dev->dev,
  938. "SG entries decreased from %d to %u - device may not work properly anymore\n",
  939. host->sg_tablesize, nr_segs);
  940. host->sg_tablesize = nr_segs;
  941. host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
  942. }
  943. static void scsifront_backend_changed(struct xenbus_device *dev,
  944. enum xenbus_state backend_state)
  945. {
  946. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  947. pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
  948. switch (backend_state) {
  949. case XenbusStateUnknown:
  950. case XenbusStateInitialising:
  951. case XenbusStateInitWait:
  952. case XenbusStateInitialised:
  953. break;
  954. case XenbusStateConnected:
  955. scsifront_read_backend_params(dev, info);
  956. if (info->pause) {
  957. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
  958. xenbus_switch_state(dev, XenbusStateConnected);
  959. info->pause = 0;
  960. return;
  961. }
  962. if (xenbus_read_driver_state(dev->nodename) ==
  963. XenbusStateInitialised)
  964. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  965. if (dev->state != XenbusStateConnected)
  966. xenbus_switch_state(dev, XenbusStateConnected);
  967. break;
  968. case XenbusStateClosed:
  969. if (dev->state == XenbusStateClosed)
  970. break;
  971. fallthrough; /* Missed the backend's Closing state */
  972. case XenbusStateClosing:
  973. scsifront_disconnect(info);
  974. break;
  975. case XenbusStateReconfiguring:
  976. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
  977. xenbus_switch_state(dev, XenbusStateReconfiguring);
  978. break;
  979. case XenbusStateReconfigured:
  980. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  981. xenbus_switch_state(dev, XenbusStateConnected);
  982. break;
  983. }
  984. }
  985. static const struct xenbus_device_id scsifront_ids[] = {
  986. { "vscsi" },
  987. { "" }
  988. };
  989. static struct xenbus_driver scsifront_driver = {
  990. .ids = scsifront_ids,
  991. .probe = scsifront_probe,
  992. .remove = scsifront_remove,
  993. .resume = scsifront_resume,
  994. .suspend = scsifront_suspend,
  995. .otherend_changed = scsifront_backend_changed,
  996. };
  997. static int __init scsifront_init(void)
  998. {
  999. if (!xen_domain())
  1000. return -ENODEV;
  1001. return xenbus_register_frontend(&scsifront_driver);
  1002. }
  1003. module_init(scsifront_init);
  1004. static void __exit scsifront_exit(void)
  1005. {
  1006. xenbus_unregister_driver(&scsifront_driver);
  1007. }
  1008. module_exit(scsifront_exit);
  1009. MODULE_DESCRIPTION("Xen SCSI frontend driver");
  1010. MODULE_LICENSE("GPL");
  1011. MODULE_ALIAS("xen:vscsi");
  1012. MODULE_AUTHOR("Juergen Gross <jgross@suse.com>");