cregit-Linux how code gets into the kernel

Release 4.11 drivers/scsi/xen-scsifront.c

Directory: drivers/scsi
/*
 * Xen SCSI frontend driver
 *
 * Copyright (c) 2008, FUJITSU Limited
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation; or, when distributed
 * separately from the Linux kernel or incorporated into other
 * software packages, subject to the following license:
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this source file (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/device.h>
#include <linux/wait.h>
#include <linux/interrupt.h>
#include <linux/mutex.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/blkdev.h>
#include <linux/pfn.h>
#include <linux/slab.h>
#include <linux/bitops.h>

#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi.h>
#include <scsi/scsi_host.h>

#include <xen/xen.h>
#include <xen/xenbus.h>
#include <xen/grant_table.h>
#include <xen/events.h>
#include <xen/page.h>

#include <xen/interface/grant_table.h>
#include <xen/interface/io/vscsiif.h>
#include <xen/interface/io/protocols.h>

#include <asm/xen/hypervisor.h>



#define GRANT_INVALID_REF	0


#define VSCSIFRONT_OP_ADD_LUN	1

#define VSCSIFRONT_OP_DEL_LUN	2

#define VSCSIFRONT_OP_READD_LUN	3

/* Tuning point. */

#define VSCSIIF_DEFAULT_CMD_PER_LUN 10

#define VSCSIIF_MAX_TARGET          64

#define VSCSIIF_MAX_LUN             255


#define VSCSIIF_RING_SIZE	__CONST_RING_SIZE(vscsiif, PAGE_SIZE)

#define VSCSIIF_MAX_REQS	VSCSIIF_RING_SIZE


#define vscsiif_grants_sg(_sg)	(PFN_UP((_sg) *         \
                                sizeof(struct scsiif_request_segment)))


struct vscsifrnt_shadow {
	/* command between backend and frontend */
	
unsigned char act;
	
uint8_t nr_segments;
	
uint16_t rqid;
	
uint16_t ref_rqid;

	
unsigned int nr_grants;		/* number of grants in gref[] */
	
struct scsiif_request_segment *sg;	/* scatter/gather elements */
	
struct scsiif_request_segment seg[VSCSIIF_SG_TABLESIZE];

	/* Do reset or abort function. */
	
wait_queue_head_t wq_reset;	/* reset work queue           */
	
int wait_reset;			/* reset work queue condition */
	
int32_t rslt_reset;		/* reset response status:     */
					/* SUCCESS or FAILED or:      */

#define RSLT_RESET_WAITING	0

#define RSLT_RESET_ERR		-1

	/* Requested struct scsi_cmnd is stored from kernel. */
	
struct scsi_cmnd *sc;
	
int gref[vscsiif_grants_sg(SG_ALL) + SG_ALL];
};


struct vscsifrnt_info {
	
struct xenbus_device *dev;

	
struct Scsi_Host *host;
	
int host_active;

	
unsigned int evtchn;
	
unsigned int irq;

	
grant_ref_t ring_ref;
	
struct vscsiif_front_ring ring;
	
struct vscsiif_response	ring_rsp;

	
spinlock_t shadow_lock;
	DECLARE_BITMAP(shadow_free_bitmap, VSCSIIF_MAX_REQS);
	
struct vscsifrnt_shadow *shadow[VSCSIIF_MAX_REQS];

	/* Following items are protected by the host lock. */
	
wait_queue_head_t wq_sync;
	
wait_queue_head_t wq_pause;
	
unsigned int wait_ring_available:1;
	
unsigned int waiting_pause:1;
	
unsigned int pause:1;
	
unsigned callers;

	
char dev_state_path[64];
	
struct task_struct *curr;
};

static DEFINE_MUTEX(scsifront_mutex);


static void scsifront_wake_up(struct vscsifrnt_info *info) { info->wait_ring_available = 0; wake_up(&info->wq_sync); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross25100.00%1100.00%
Total25100.00%1100.00%


static int scsifront_get_rqid(struct vscsifrnt_info *info) { unsigned long flags; int free; spin_lock_irqsave(&info->shadow_lock, flags); free = find_first_bit(info->shadow_free_bitmap, VSCSIIF_MAX_REQS); __clear_bit(free, info->shadow_free_bitmap); spin_unlock_irqrestore(&info->shadow_lock, flags); return free; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross61100.00%1100.00%
Total61100.00%1100.00%


static int _scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id) { int empty = bitmap_empty(info->shadow_free_bitmap, VSCSIIF_MAX_REQS); __set_bit(id, info->shadow_free_bitmap); info->shadow[id] = NULL; return empty || info->wait_ring_available; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross51100.00%1100.00%
Total51100.00%1100.00%


static void scsifront_put_rqid(struct vscsifrnt_info *info, uint32_t id) { unsigned long flags; int kick; spin_lock_irqsave(&info->shadow_lock, flags); kick = _scsifront_put_rqid(info, id); spin_unlock_irqrestore(&info->shadow_lock, flags); if (kick) scsifront_wake_up(info); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross59100.00%1100.00%
Total59100.00%1100.00%


static int scsifront_do_request(struct vscsifrnt_info *info, struct vscsifrnt_shadow *shadow) { struct vscsiif_front_ring *ring = &(info->ring); struct vscsiif_request *ring_req; struct scsi_cmnd *sc = shadow->sc; uint32_t id; int i, notify; if (RING_FULL(&info->ring)) return -EBUSY; id = scsifront_get_rqid(info); /* use id in response */ if (id >= VSCSIIF_MAX_REQS) return -EBUSY; info->shadow[id] = shadow; shadow->rqid = id; ring_req = RING_GET_REQUEST(&(info->ring), ring->req_prod_pvt); ring->req_prod_pvt++; ring_req->rqid = id; ring_req->act = shadow->act; ring_req->ref_rqid = shadow->ref_rqid; ring_req->nr_segments = shadow->nr_segments; ring_req->id = sc->device->id; ring_req->lun = sc->device->lun; ring_req->channel = sc->device->channel; ring_req->cmd_len = sc->cmd_len; BUG_ON(sc->cmd_len > VSCSIIF_MAX_COMMAND_SIZE); memcpy(ring_req->cmnd, sc->cmnd, sc->cmd_len); ring_req->sc_data_direction = (uint8_t)sc->sc_data_direction; ring_req->timeout_per_command = sc->request->timeout / HZ; for (i = 0; i < (shadow->nr_segments & ~VSCSIIF_SG_GRANT); i++) ring_req->seg[i] = shadow->seg[i]; RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(ring, notify); if (notify) notify_remote_via_irq(info->irq); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross288100.00%2100.00%
Total288100.00%2100.00%


static void scsifront_gnttab_done(struct vscsifrnt_info *info, struct vscsifrnt_shadow *shadow) { int i; if (shadow->sc->sc_data_direction == DMA_NONE) return; for (i = 0; i < shadow->nr_grants; i++) { if (unlikely(gnttab_query_foreign_access(shadow->gref[i]))) { shost_printk(KERN_ALERT, info->host, KBUILD_MODNAME "grant still in use by backend\n"); BUG(); } gnttab_end_foreign_access(shadow->gref[i], 0, 0UL); } kfree(shadow->sg); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross100100.00%2100.00%
Total100100.00%2100.00%


static void scsifront_cdb_cmd_done(struct vscsifrnt_info *info, struct vscsiif_response *ring_rsp) { struct vscsifrnt_shadow *shadow; struct scsi_cmnd *sc; uint32_t id; uint8_t sense_len; id = ring_rsp->rqid; shadow = info->shadow[id]; sc = shadow->sc; BUG_ON(sc == NULL); scsifront_gnttab_done(info, shadow); scsifront_put_rqid(info, id); sc->result = ring_rsp->rslt; scsi_set_resid(sc, ring_rsp->residual_len); sense_len = min_t(uint8_t, VSCSIIF_SENSE_BUFFERSIZE, ring_rsp->sense_len); if (sense_len) memcpy(sc->sense_buffer, ring_rsp->sense_buffer, sense_len); sc->scsi_done(sc); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross128100.00%2100.00%
Total128100.00%2100.00%


static void scsifront_sync_cmd_done(struct vscsifrnt_info *info, struct vscsiif_response *ring_rsp) { uint16_t id = ring_rsp->rqid; unsigned long flags; struct vscsifrnt_shadow *shadow = info->shadow[id]; int kick; spin_lock_irqsave(&info->shadow_lock, flags); shadow->wait_reset = 1; switch (shadow->rslt_reset) { case RSLT_RESET_WAITING: shadow->rslt_reset = ring_rsp->rslt; break; case RSLT_RESET_ERR: kick = _scsifront_put_rqid(info, id); spin_unlock_irqrestore(&info->shadow_lock, flags); kfree(shadow); if (kick) scsifront_wake_up(info); return; default: shost_printk(KERN_ERR, info->host, KBUILD_MODNAME "bad reset state %d, possibly leaking %u\n", shadow->rslt_reset, id); break; } spin_unlock_irqrestore(&info->shadow_lock, flags); wake_up(&shadow->wq_reset); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross153100.00%1100.00%
Total153100.00%1100.00%


static void scsifront_do_response(struct vscsifrnt_info *info, struct vscsiif_response *ring_rsp) { if (WARN(ring_rsp->rqid >= VSCSIIF_MAX_REQS || test_bit(ring_rsp->rqid, info->shadow_free_bitmap), "illegal rqid %u returned by backend!\n", ring_rsp->rqid)) return; if (info->shadow[ring_rsp->rqid]->act == VSCSIIF_ACT_SCSI_CDB) scsifront_cdb_cmd_done(info, ring_rsp); else scsifront_sync_cmd_done(info, ring_rsp); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross75100.00%1100.00%
Total75100.00%1100.00%


static int scsifront_ring_drain(struct vscsifrnt_info *info) { struct vscsiif_response *ring_rsp; RING_IDX i, rp; int more_to_do = 0; rp = info->ring.sring->rsp_prod; rmb(); /* ordering required respective to dom0 */ for (i = info->ring.rsp_cons; i != rp; i++) { ring_rsp = RING_GET_RESPONSE(&info->ring, i); scsifront_do_response(info, ring_rsp); } info->ring.rsp_cons = i; if (i != info->ring.req_prod_pvt) RING_FINAL_CHECK_FOR_RESPONSES(&info->ring, more_to_do); else info->ring.sring->rsp_event = i + 1; return more_to_do; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross122100.00%2100.00%
Total122100.00%2100.00%


static int scsifront_cmd_done(struct vscsifrnt_info *info) { int more_to_do; unsigned long flags; spin_lock_irqsave(info->host->host_lock, flags); more_to_do = scsifront_ring_drain(info); info->wait_ring_available = 0; spin_unlock_irqrestore(info->host->host_lock, flags); wake_up(&info->wq_sync); return more_to_do; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross64100.00%2100.00%
Total64100.00%2100.00%


static irqreturn_t scsifront_irq_fn(int irq, void *dev_id) { struct vscsifrnt_info *info = dev_id; while (scsifront_cmd_done(info)) /* Yield point for this unbounded loop. */ cond_resched(); return IRQ_HANDLED; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross34100.00%1100.00%
Total34100.00%1100.00%


static void scsifront_finish_all(struct vscsifrnt_info *info) { unsigned i; struct vscsiif_response resp; scsifront_ring_drain(info); for (i = 0; i < VSCSIIF_MAX_REQS; i++) { if (test_bit(i, info->shadow_free_bitmap)) continue; resp.rqid = i; resp.sense_len = 0; resp.rslt = DID_RESET << 16; resp.residual_len = 0; scsifront_do_response(info, &resp); } }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross84100.00%2100.00%
Total84100.00%2100.00%


static int map_data_for_request(struct vscsifrnt_info *info, struct scsi_cmnd *sc, struct vscsifrnt_shadow *shadow) { grant_ref_t gref_head; struct page *page; int err, ref, ref_cnt = 0; int grant_ro = (sc->sc_data_direction == DMA_TO_DEVICE); unsigned int i, off, len, bytes; unsigned int data_len = scsi_bufflen(sc); unsigned int data_grants = 0, seg_grants = 0; struct scatterlist *sg; struct scsiif_request_segment *seg; if (sc->sc_data_direction == DMA_NONE || !data_len) return 0; scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) data_grants += PFN_UP(sg->offset + sg->length); if (data_grants > VSCSIIF_SG_TABLESIZE) { if (data_grants > info->host->sg_tablesize) { shost_printk(KERN_ERR, info->host, KBUILD_MODNAME "Unable to map request_buffer for command!\n"); return -E2BIG; } seg_grants = vscsiif_grants_sg(data_grants); shadow->sg = kcalloc(data_grants, sizeof(struct scsiif_request_segment), GFP_ATOMIC); if (!shadow->sg) return -ENOMEM; } seg = shadow->sg ? : shadow->seg; err = gnttab_alloc_grant_references(seg_grants + data_grants, &gref_head); if (err) { kfree(shadow->sg); shost_printk(KERN_ERR, info->host, KBUILD_MODNAME "gnttab_alloc_grant_references() error\n"); return -ENOMEM; } if (seg_grants) { page = virt_to_page(seg); off = (unsigned long)seg & ~PAGE_MASK; len = sizeof(struct scsiif_request_segment) * data_grants; while (len > 0) { bytes = min_t(unsigned int, len, PAGE_SIZE - off); ref = gnttab_claim_grant_reference(&gref_head); BUG_ON(ref == -ENOSPC); gnttab_grant_foreign_access_ref(ref, info->dev->otherend_id, xen_page_to_gfn(page), 1); shadow->gref[ref_cnt] = ref; shadow->seg[ref_cnt].gref = ref; shadow->seg[ref_cnt].offset = (uint16_t)off; shadow->seg[ref_cnt].length = (uint16_t)bytes; page++; len -= bytes; off = 0; ref_cnt++; } BUG_ON(seg_grants < ref_cnt); seg_grants = ref_cnt; } scsi_for_each_sg(sc, sg, scsi_sg_count(sc), i) { page = sg_page(sg); off = sg->offset; len = sg->length; while (len > 0 && data_len > 0) { /* * sg sends a scatterlist that is larger than * the data_len it wants transferred for certain * IO sizes. */ bytes = min_t(unsigned int, len, PAGE_SIZE - off); bytes = min(bytes, data_len); ref = gnttab_claim_grant_reference(&gref_head); BUG_ON(ref == -ENOSPC); gnttab_grant_foreign_access_ref(ref, info->dev->otherend_id, xen_page_to_gfn(page), grant_ro); shadow->gref[ref_cnt] = ref; seg->gref = ref; seg->offset = (uint16_t)off; seg->length = (uint16_t)bytes; page++; seg++; len -= bytes; data_len -= bytes; off = 0; ref_cnt++; } } if (seg_grants) shadow->nr_segments = VSCSIIF_SG_GRANT | seg_grants; else shadow->nr_segments = (uint8_t)ref_cnt; shadow->nr_grants = ref_cnt; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross58298.48%360.00%
Julien Grall81.35%120.00%
Dan Carpenter10.17%120.00%
Total591100.00%5100.00%


static int scsifront_enter(struct vscsifrnt_info *info) { if (info->pause) return 1; info->callers++; return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross28100.00%1100.00%
Total28100.00%1100.00%


static void scsifront_return(struct vscsifrnt_info *info) { info->callers--; if (info->callers) return; if (!info->waiting_pause) return; info->waiting_pause = 0; wake_up(&info->wq_pause); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross45100.00%1100.00%
Total45100.00%1100.00%


static int scsifront_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc) { struct vscsifrnt_info *info = shost_priv(shost); struct vscsifrnt_shadow *shadow = scsi_cmd_priv(sc); unsigned long flags; int err; sc->result = 0; memset(shadow, 0, sizeof(*shadow)); shadow->sc = sc; shadow->act = VSCSIIF_ACT_SCSI_CDB; spin_lock_irqsave(shost->host_lock, flags); if (scsifront_enter(info)) { spin_unlock_irqrestore(shost->host_lock, flags); return SCSI_MLQUEUE_HOST_BUSY; } err = map_data_for_request(info, sc, shadow); if (err < 0) { pr_debug("%s: err %d\n", __func__, err); scsifront_return(info); spin_unlock_irqrestore(shost->host_lock, flags); if (err == -ENOMEM) return SCSI_MLQUEUE_HOST_BUSY; sc->result = DID_ERROR << 16; sc->scsi_done(sc); return 0; } if (scsifront_do_request(info, shadow)) { scsifront_gnttab_done(info, shadow); goto busy; } scsifront_return(info); spin_unlock_irqrestore(shost->host_lock, flags); return 0; busy: scsifront_return(info); spin_unlock_irqrestore(shost->host_lock, flags); pr_debug("%s: busy\n", __func__); return SCSI_MLQUEUE_HOST_BUSY; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross238100.00%3100.00%
Total238100.00%3100.00%

/* * Any exception handling (reset or abort) must be forwarded to the backend. * We have to wait until an answer is returned. This answer contains the * result to be returned to the requestor. */
static int scsifront_action_handler(struct scsi_cmnd *sc, uint8_t act) { struct Scsi_Host *host = sc->device->host; struct vscsifrnt_info *info = shost_priv(host); struct vscsifrnt_shadow *shadow, *s = scsi_cmd_priv(sc); int err = 0; shadow = kzalloc(sizeof(*shadow), GFP_NOIO); if (!shadow) return FAILED; shadow->act = act; shadow->rslt_reset = RSLT_RESET_WAITING; shadow->sc = sc; shadow->ref_rqid = s->rqid; init_waitqueue_head(&shadow->wq_reset); spin_lock_irq(host->host_lock); for (;;) { if (scsifront_enter(info)) goto fail; if (!scsifront_do_request(info, shadow)) break; scsifront_return(info); if (err) goto fail; info->wait_ring_available = 1; spin_unlock_irq(host->host_lock); err = wait_event_interruptible(info->wq_sync, !info->wait_ring_available); spin_lock_irq(host->host_lock); } spin_unlock_irq(host->host_lock); err = wait_event_interruptible(shadow->wq_reset, shadow->wait_reset); spin_lock_irq(host->host_lock); if (!err) { err = shadow->rslt_reset; scsifront_put_rqid(info, shadow->rqid); kfree(shadow); } else { spin_lock(&info->shadow_lock); shadow->rslt_reset = RSLT_RESET_ERR; spin_unlock(&info->shadow_lock); err = FAILED; } scsifront_return(info); spin_unlock_irq(host->host_lock); return err; fail: spin_unlock_irq(host->host_lock); kfree(shadow); return FAILED; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross29898.03%375.00%
David Vrabel61.97%125.00%
Total304100.00%4100.00%


static int scsifront_eh_abort_handler(struct scsi_cmnd *sc) { pr_debug("%s\n", __func__); return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_ABORT); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross26100.00%1100.00%
Total26100.00%1100.00%


static int scsifront_dev_reset_handler(struct scsi_cmnd *sc) { pr_debug("%s\n", __func__); return scsifront_action_handler(sc, VSCSIIF_ACT_SCSI_RESET); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross26100.00%1100.00%
Total26100.00%1100.00%


static int scsifront_sdev_configure(struct scsi_device *sdev) { struct vscsifrnt_info *info = shost_priv(sdev->host); if (info && current == info->curr) xenbus_printf(XBT_NIL, info->dev->nodename, info->dev_state_path, "%d", XenbusStateConnected); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross55100.00%1100.00%
Total55100.00%1100.00%


static void scsifront_sdev_destroy(struct scsi_device *sdev) { struct vscsifrnt_info *info = shost_priv(sdev->host); if (info && current == info->curr) xenbus_printf(XBT_NIL, info->dev->nodename, info->dev_state_path, "%d", XenbusStateClosed); }

Contributors

PersonTokensPropCommitsCommitProp
Juergen Gross52100.00%1100.00%
Total52100.00%1100.00%

static struct scsi_host_template scsifront_sht = { .module = THIS_MODULE, .name = "Xen SCSI frontend driver", .queuecommand = scsifront_queuecommand, .eh_abort_handler = scsifront_eh_abort_handler, .eh_device_reset_handler = scsifront_dev_reset_handler, .slave_configure = scsifront_sdev_configure, .slave_destroy = scsifront_sdev_destroy, .cmd_per_lun = VSCSIIF_DEFAULT_CMD_PER_LUN, .can_queue = VSCSIIF_MAX_REQS, .this_id = -1, .cmd_size = sizeof(struct vscsifrnt_shadow), .sg_tablesize = VSCSIIF_SG_TABLESIZE, .use_clustering