xen-scsifront.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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 (info && 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 (info && 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 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 = (struct vscsifrnt_info *)host->hostdata;
  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 int 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. return 0;
  827. }
  828. static void scsifront_disconnect(struct vscsifrnt_info *info)
  829. {
  830. struct xenbus_device *dev = info->dev;
  831. struct Scsi_Host *host = info->host;
  832. pr_debug("%s: %s disconnect\n", __func__, dev->nodename);
  833. /*
  834. * When this function is executed, all devices of
  835. * Frontend have been deleted.
  836. * Therefore, it need not block I/O before remove_host.
  837. */
  838. mutex_lock(&scsifront_mutex);
  839. if (info->host_active != STATE_INACTIVE) {
  840. scsi_remove_host(host);
  841. info->host_active = STATE_INACTIVE;
  842. }
  843. mutex_unlock(&scsifront_mutex);
  844. xenbus_frontend_closed(dev);
  845. }
  846. static void scsifront_do_lun_hotplug(struct vscsifrnt_info *info, int op)
  847. {
  848. struct xenbus_device *dev = info->dev;
  849. int i, err = 0;
  850. char str[64];
  851. char **dir;
  852. unsigned int dir_n = 0;
  853. unsigned int device_state;
  854. unsigned int hst, chn, tgt, lun;
  855. struct scsi_device *sdev;
  856. if (info->host_active == STATE_ERROR)
  857. return;
  858. dir = xenbus_directory(XBT_NIL, dev->otherend, "vscsi-devs", &dir_n);
  859. if (IS_ERR(dir))
  860. return;
  861. /* mark current task as the one allowed to modify device states */
  862. BUG_ON(info->curr);
  863. info->curr = current;
  864. for (i = 0; i < dir_n; i++) {
  865. /* read status */
  866. snprintf(str, sizeof(str), "vscsi-devs/%s/state", dir[i]);
  867. err = xenbus_scanf(XBT_NIL, dev->otherend, str, "%u",
  868. &device_state);
  869. if (XENBUS_EXIST_ERR(err))
  870. continue;
  871. /* virtual SCSI device */
  872. snprintf(str, sizeof(str), "vscsi-devs/%s/v-dev", dir[i]);
  873. err = xenbus_scanf(XBT_NIL, dev->otherend, str,
  874. "%u:%u:%u:%u", &hst, &chn, &tgt, &lun);
  875. if (XENBUS_EXIST_ERR(err))
  876. continue;
  877. /*
  878. * Front device state path, used in slave_configure called
  879. * on successfull scsi_add_device, and in slave_destroy called
  880. * on remove of a device.
  881. */
  882. snprintf(info->dev_state_path, sizeof(info->dev_state_path),
  883. "vscsi-devs/%s/state", dir[i]);
  884. switch (op) {
  885. case VSCSIFRONT_OP_ADD_LUN:
  886. if (device_state != XenbusStateInitialised)
  887. break;
  888. if (scsi_add_device(info->host, chn, tgt, lun)) {
  889. dev_err(&dev->dev, "scsi_add_device\n");
  890. err = xenbus_printf(XBT_NIL, dev->nodename,
  891. info->dev_state_path,
  892. "%d", XenbusStateClosed);
  893. if (err)
  894. xenbus_dev_error(dev, err,
  895. "%s: writing dev_state_path", __func__);
  896. }
  897. break;
  898. case VSCSIFRONT_OP_DEL_LUN:
  899. if (device_state != XenbusStateClosing)
  900. break;
  901. sdev = scsi_device_lookup(info->host, chn, tgt, lun);
  902. if (sdev) {
  903. scsi_remove_device(sdev);
  904. scsi_device_put(sdev);
  905. }
  906. break;
  907. case VSCSIFRONT_OP_READD_LUN:
  908. if (device_state == XenbusStateConnected) {
  909. err = xenbus_printf(XBT_NIL, dev->nodename,
  910. info->dev_state_path,
  911. "%d", XenbusStateConnected);
  912. if (err)
  913. xenbus_dev_error(dev, err,
  914. "%s: writing dev_state_path", __func__);
  915. }
  916. break;
  917. default:
  918. break;
  919. }
  920. }
  921. info->curr = NULL;
  922. kfree(dir);
  923. }
  924. static void scsifront_read_backend_params(struct xenbus_device *dev,
  925. struct vscsifrnt_info *info)
  926. {
  927. unsigned int sg_grant, nr_segs;
  928. struct Scsi_Host *host = info->host;
  929. sg_grant = xenbus_read_unsigned(dev->otherend, "feature-sg-grant", 0);
  930. nr_segs = min_t(unsigned int, sg_grant, SG_ALL);
  931. nr_segs = max_t(unsigned int, nr_segs, VSCSIIF_SG_TABLESIZE);
  932. nr_segs = min_t(unsigned int, nr_segs,
  933. VSCSIIF_SG_TABLESIZE * PAGE_SIZE /
  934. sizeof(struct scsiif_request_segment));
  935. if (!info->pause && sg_grant)
  936. dev_info(&dev->dev, "using up to %d SG entries\n", nr_segs);
  937. else if (info->pause && nr_segs < host->sg_tablesize)
  938. dev_warn(&dev->dev,
  939. "SG entries decreased from %d to %u - device may not work properly anymore\n",
  940. host->sg_tablesize, nr_segs);
  941. host->sg_tablesize = nr_segs;
  942. host->max_sectors = (nr_segs - 1) * PAGE_SIZE / 512;
  943. }
  944. static void scsifront_backend_changed(struct xenbus_device *dev,
  945. enum xenbus_state backend_state)
  946. {
  947. struct vscsifrnt_info *info = dev_get_drvdata(&dev->dev);
  948. pr_debug("%s: %p %u %u\n", __func__, dev, dev->state, backend_state);
  949. switch (backend_state) {
  950. case XenbusStateUnknown:
  951. case XenbusStateInitialising:
  952. case XenbusStateInitWait:
  953. case XenbusStateInitialised:
  954. break;
  955. case XenbusStateConnected:
  956. scsifront_read_backend_params(dev, info);
  957. if (info->pause) {
  958. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_READD_LUN);
  959. xenbus_switch_state(dev, XenbusStateConnected);
  960. info->pause = 0;
  961. return;
  962. }
  963. if (xenbus_read_driver_state(dev->nodename) ==
  964. XenbusStateInitialised)
  965. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  966. if (dev->state != XenbusStateConnected)
  967. xenbus_switch_state(dev, XenbusStateConnected);
  968. break;
  969. case XenbusStateClosed:
  970. if (dev->state == XenbusStateClosed)
  971. break;
  972. fallthrough; /* Missed the backend's Closing state */
  973. case XenbusStateClosing:
  974. scsifront_disconnect(info);
  975. break;
  976. case XenbusStateReconfiguring:
  977. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_DEL_LUN);
  978. xenbus_switch_state(dev, XenbusStateReconfiguring);
  979. break;
  980. case XenbusStateReconfigured:
  981. scsifront_do_lun_hotplug(info, VSCSIFRONT_OP_ADD_LUN);
  982. xenbus_switch_state(dev, XenbusStateConnected);
  983. break;
  984. }
  985. }
  986. static const struct xenbus_device_id scsifront_ids[] = {
  987. { "vscsi" },
  988. { "" }
  989. };
  990. static struct xenbus_driver scsifront_driver = {
  991. .ids = scsifront_ids,
  992. .probe = scsifront_probe,
  993. .remove = scsifront_remove,
  994. .resume = scsifront_resume,
  995. .suspend = scsifront_suspend,
  996. .otherend_changed = scsifront_backend_changed,
  997. };
  998. static int __init scsifront_init(void)
  999. {
  1000. if (!xen_domain())
  1001. return -ENODEV;
  1002. return xenbus_register_frontend(&scsifront_driver);
  1003. }
  1004. module_init(scsifront_init);
  1005. static void __exit scsifront_exit(void)
  1006. {
  1007. xenbus_unregister_driver(&scsifront_driver);
  1008. }
  1009. module_exit(scsifront_exit);
  1010. MODULE_DESCRIPTION("Xen SCSI frontend driver");
  1011. MODULE_LICENSE("GPL");
  1012. MODULE_ALIAS("xen:vscsi");
  1013. MODULE_AUTHOR("Juergen Gross <[email protected]>");