Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1 | /* |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 2 | * Copyright (c) 2009, Microsoft Corporation. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms and conditions of the GNU General Public License, |
| 6 | * version 2, as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 15 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 16 | * |
| 17 | * Authors: |
| 18 | * Haiyang Zhang <haiyangz@microsoft.com> |
| 19 | * Hank Janssen <hjanssen@microsoft.com> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 20 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/blkdev.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 25 | #include <scsi/scsi.h> |
| 26 | #include <scsi/scsi_cmnd.h> |
| 27 | #include <scsi/scsi_host.h> |
| 28 | #include <scsi/scsi_device.h> |
| 29 | #include <scsi/scsi_tcq.h> |
| 30 | #include <scsi/scsi_eh.h> |
| 31 | #include <scsi/scsi_devinfo.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 32 | #include <scsi/scsi_dbg.h> |
Greg Kroah-Hartman | 4983b39 | 2009-08-19 16:14:47 -0700 | [diff] [blame] | 33 | #include "osd.h" |
Greg Kroah-Hartman | 645954c | 2009-08-28 16:22:59 -0700 | [diff] [blame] | 34 | #include "logging.h" |
Greg Kroah-Hartman | 870cde8 | 2009-08-19 16:21:28 -0700 | [diff] [blame] | 35 | #include "vmbus.h" |
Greg Kroah-Hartman | 731a788 | 2009-08-28 16:23:49 -0700 | [diff] [blame] | 36 | #include "StorVscApi.h" |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 37 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 38 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 39 | struct host_device_context { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 40 | /* must be 1st field |
| 41 | * FIXME this is a bug */ |
| 42 | struct work_struct host_rescan_work; |
| 43 | |
| 44 | /* point back to our device context */ |
| 45 | struct device_context *device_ctx; |
| 46 | struct kmem_cache *request_pool; |
| 47 | unsigned int port; |
| 48 | unsigned char path; |
| 49 | unsigned char target; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | struct storvsc_cmd_request { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 53 | struct list_head entry; |
| 54 | struct scsi_cmnd *cmd; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 55 | |
| 56 | unsigned int bounce_sgl_count; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 57 | struct scatterlist *bounce_sgl; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 58 | |
Nicolas Palix | 0b3f683 | 2009-07-29 14:10:19 +0200 | [diff] [blame] | 59 | struct hv_storvsc_request request; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 60 | /* !!!DO NOT ADD ANYTHING BELOW HERE!!! */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 61 | /* The extension buffer falls right here and is pointed to by |
| 62 | * request.Extension; |
| 63 | * Which sounds like a very bad design... */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | struct storvsc_driver_context { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 67 | /* !! These must be the first 2 fields !! */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 68 | /* FIXME this is a bug... */ |
| 69 | struct driver_context drv_ctx; |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 70 | struct storvsc_driver_object drv_obj; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 73 | /* Static decl */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 74 | static int storvsc_probe(struct device *dev); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 75 | static int storvsc_queuecommand(struct scsi_cmnd *scmnd, |
| 76 | void (*done)(struct scsi_cmnd *)); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 77 | static int storvsc_device_alloc(struct scsi_device *); |
| 78 | static int storvsc_device_configure(struct scsi_device *); |
| 79 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 80 | static void storvsc_host_rescan_callback(struct work_struct *work); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 81 | static void storvsc_host_rescan(struct hv_device *device_obj); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 82 | static int storvsc_remove(struct device *dev); |
| 83 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 84 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
| 85 | unsigned int sg_count, |
| 86 | unsigned int len); |
| 87 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
| 88 | unsigned int sg_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 89 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 90 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, |
| 91 | struct scatterlist *bounce_sgl, |
| 92 | unsigned int orig_sgl_count); |
| 93 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, |
| 94 | struct scatterlist *bounce_sgl, |
| 95 | unsigned int orig_sgl_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 96 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 97 | static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], |
| 98 | unsigned int *lun_count); |
| 99 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device *bdev, |
| 100 | sector_t capacity, int *info); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 101 | |
| 102 | |
| 103 | static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE; |
| 104 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 105 | /* The one and only one */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 106 | static struct storvsc_driver_context g_storvsc_drv; |
| 107 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 108 | /* Scsi driver */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 109 | static struct scsi_host_template scsi_driver = { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 110 | .module = THIS_MODULE, |
| 111 | .name = "storvsc_host_t", |
| 112 | .bios_param = storvsc_get_chs, |
| 113 | .queuecommand = storvsc_queuecommand, |
| 114 | .eh_host_reset_handler = storvsc_host_reset_handler, |
| 115 | .slave_alloc = storvsc_device_alloc, |
| 116 | .slave_configure = storvsc_device_configure, |
| 117 | .cmd_per_lun = 1, |
| 118 | /* 64 max_queue * 1 target */ |
| 119 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
| 120 | .this_id = -1, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 121 | /* no use setting to 0 since ll_blk_rw reset it to 1 */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 122 | /* currently 32 */ |
| 123 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, |
| 124 | /* |
| 125 | * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge |
| 126 | * into 1 sg element. If set, we must limit the max_segment_size to |
| 127 | * PAGE_SIZE, otherwise we may get 1 sg element that represents |
| 128 | * multiple |
| 129 | */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 130 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 131 | .use_clustering = ENABLE_CLUSTERING, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 132 | /* Make sure we dont get a sg segment crosses a page boundary */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 133 | .dma_boundary = PAGE_SIZE-1, |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 137 | /** |
| 138 | * storvsc_drv_init - StorVsc driver initialization. |
| 139 | */ |
Greg Kroah-Hartman | bd1de70 | 2009-07-29 09:04:51 -0700 | [diff] [blame] | 140 | static int storvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 141 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 142 | int ret; |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 143 | struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 144 | struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 145 | |
| 146 | DPRINT_ENTER(STORVSC_DRV); |
| 147 | |
| 148 | vmbus_get_interface(&storvsc_drv_obj->Base.VmbusChannelInterface); |
| 149 | |
| 150 | storvsc_drv_obj->RingBufferSize = storvsc_ringbuffer_size; |
| 151 | storvsc_drv_obj->OnHostRescan = storvsc_host_rescan; |
| 152 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 153 | /* Callback to client driver to complete the initialization */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 154 | pfn_drv_init(&storvsc_drv_obj->Base); |
| 155 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 156 | DPRINT_INFO(STORVSC_DRV, |
| 157 | "request extension size %u, max outstanding reqs %u", |
| 158 | storvsc_drv_obj->RequestExtSize, |
| 159 | storvsc_drv_obj->MaxOutstandingRequestsPerChannel); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 160 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 161 | if (storvsc_drv_obj->MaxOutstandingRequestsPerChannel < |
| 162 | STORVSC_MAX_IO_REQUESTS) { |
| 163 | DPRINT_ERR(STORVSC_DRV, |
| 164 | "The number of outstanding io requests (%d) " |
| 165 | "is larger than that supported (%d) internally.", |
| 166 | STORVSC_MAX_IO_REQUESTS, |
| 167 | storvsc_drv_obj->MaxOutstandingRequestsPerChannel); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 168 | return -1; |
| 169 | } |
| 170 | |
| 171 | drv_ctx->driver.name = storvsc_drv_obj->Base.name; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 172 | memcpy(&drv_ctx->class_id, &storvsc_drv_obj->Base.deviceType, |
| 173 | sizeof(struct hv_guid)); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 174 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 175 | drv_ctx->probe = storvsc_probe; |
| 176 | drv_ctx->remove = storvsc_remove; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 177 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 178 | /* The driver belongs to vmbus */ |
Bill Pemberton | 5d48a1c | 2009-07-27 16:47:36 -0400 | [diff] [blame] | 179 | ret = vmbus_child_driver_register(drv_ctx); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 180 | |
| 181 | DPRINT_EXIT(STORVSC_DRV); |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 186 | static int storvsc_drv_exit_cb(struct device *dev, void *data) |
| 187 | { |
| 188 | struct device **curr = (struct device **)data; |
| 189 | *curr = dev; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 190 | return 1; /* stop iterating */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Greg Kroah-Hartman | bd1de70 | 2009-07-29 09:04:51 -0700 | [diff] [blame] | 193 | static void storvsc_drv_exit(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 194 | { |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 195 | struct storvsc_driver_object *storvsc_drv_obj = &g_storvsc_drv.drv_obj; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 196 | struct driver_context *drv_ctx = &g_storvsc_drv.drv_ctx; |
| 197 | struct device *current_dev = NULL; |
Bill Pemberton | 2295ba2 | 2009-07-28 13:46:22 -0400 | [diff] [blame] | 198 | int ret; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 199 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 200 | DPRINT_ENTER(STORVSC_DRV); |
| 201 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 202 | while (1) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 203 | current_dev = NULL; |
| 204 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 205 | /* Get the device */ |
Bill Pemberton | 2295ba2 | 2009-07-28 13:46:22 -0400 | [diff] [blame] | 206 | ret = driver_for_each_device(&drv_ctx->driver, NULL, |
| 207 | (void *) ¤t_dev, |
| 208 | storvsc_drv_exit_cb); |
| 209 | |
| 210 | if (ret) |
| 211 | DPRINT_WARN(STORVSC_DRV, |
| 212 | "driver_for_each_device returned %d", ret); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 213 | |
| 214 | if (current_dev == NULL) |
| 215 | break; |
| 216 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 217 | /* Initiate removal from the top-down */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 218 | device_unregister(current_dev); |
| 219 | } |
| 220 | |
| 221 | if (storvsc_drv_obj->Base.OnCleanup) |
| 222 | storvsc_drv_obj->Base.OnCleanup(&storvsc_drv_obj->Base); |
| 223 | |
| 224 | vmbus_child_driver_unregister(drv_ctx); |
| 225 | |
| 226 | DPRINT_EXIT(STORVSC_DRV); |
| 227 | |
| 228 | return; |
| 229 | } |
| 230 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 231 | /** |
| 232 | * storvsc_probe - Add a new device for this driver |
| 233 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 234 | static int storvsc_probe(struct device *device) |
| 235 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 236 | int ret; |
| 237 | struct driver_context *driver_ctx = |
| 238 | driver_to_driver_context(device->driver); |
| 239 | struct storvsc_driver_context *storvsc_drv_ctx = |
| 240 | (struct storvsc_driver_context *)driver_ctx; |
| 241 | struct storvsc_driver_object *storvsc_drv_obj = |
| 242 | &storvsc_drv_ctx->drv_obj; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 243 | struct device_context *device_ctx = device_to_device_context(device); |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 244 | struct hv_device *device_obj = &device_ctx->device_obj; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 245 | struct Scsi_Host *host; |
| 246 | struct host_device_context *host_device_ctx; |
Greg Kroah-Hartman | 9f0c7d2 | 2009-08-28 16:20:07 -0700 | [diff] [blame] | 247 | struct storvsc_device_info device_info; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 248 | |
| 249 | DPRINT_ENTER(STORVSC_DRV); |
| 250 | |
| 251 | if (!storvsc_drv_obj->Base.OnDeviceAdd) |
| 252 | return -1; |
| 253 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 254 | host = scsi_host_alloc(&scsi_driver, |
| 255 | sizeof(struct host_device_context)); |
| 256 | if (!host) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 257 | DPRINT_ERR(STORVSC_DRV, "unable to allocate scsi host object"); |
| 258 | return -ENOMEM; |
| 259 | } |
| 260 | |
Greg Kroah-Hartman | 0883c52 | 2009-07-24 10:58:22 -0700 | [diff] [blame] | 261 | dev_set_drvdata(device, host); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 262 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 263 | host_device_ctx = (struct host_device_context *)host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 264 | memset(host_device_ctx, 0, sizeof(struct host_device_context)); |
| 265 | |
| 266 | host_device_ctx->port = host->host_no; |
| 267 | host_device_ctx->device_ctx = device_ctx; |
| 268 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 269 | INIT_WORK(&host_device_ctx->host_rescan_work, |
| 270 | storvsc_host_rescan_callback); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 271 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 272 | host_device_ctx->request_pool = |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 273 | kmem_cache_create(dev_name(&device_ctx->device), |
| 274 | sizeof(struct storvsc_cmd_request) + |
| 275 | storvsc_drv_obj->RequestExtSize, 0, |
| 276 | SLAB_HWCACHE_ALIGN, NULL); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 277 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 278 | if (!host_device_ctx->request_pool) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 279 | scsi_host_put(host); |
| 280 | DPRINT_EXIT(STORVSC_DRV); |
| 281 | |
| 282 | return -ENOMEM; |
| 283 | } |
| 284 | |
| 285 | device_info.PortNumber = host->host_no; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 286 | /* Call to the vsc driver to add the device */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 287 | ret = storvsc_drv_obj->Base.OnDeviceAdd(device_obj, |
| 288 | (void *)&device_info); |
| 289 | if (ret != 0) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 290 | DPRINT_ERR(STORVSC_DRV, "unable to add scsi vsc device"); |
| 291 | kmem_cache_destroy(host_device_ctx->request_pool); |
| 292 | scsi_host_put(host); |
| 293 | DPRINT_EXIT(STORVSC_DRV); |
| 294 | |
| 295 | return -1; |
| 296 | } |
| 297 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 298 | /* host_device_ctx->port = device_info.PortNumber; */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 299 | host_device_ctx->path = device_info.PathId; |
| 300 | host_device_ctx->target = device_info.TargetId; |
| 301 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 302 | /* max # of devices per target */ |
| 303 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; |
| 304 | /* max # of targets per channel */ |
| 305 | host->max_id = STORVSC_MAX_TARGETS; |
| 306 | /* max # of channels */ |
| 307 | host->max_channel = STORVSC_MAX_CHANNELS - 1; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 308 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 309 | /* Register the HBA and start the scsi bus scan */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 310 | ret = scsi_add_host(host, device); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 311 | if (ret != 0) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 312 | DPRINT_ERR(STORVSC_DRV, "unable to add scsi host device"); |
| 313 | |
| 314 | storvsc_drv_obj->Base.OnDeviceRemove(device_obj); |
| 315 | |
| 316 | kmem_cache_destroy(host_device_ctx->request_pool); |
| 317 | scsi_host_put(host); |
| 318 | DPRINT_EXIT(STORVSC_DRV); |
| 319 | |
| 320 | return -1; |
| 321 | } |
| 322 | |
| 323 | scsi_scan_host(host); |
| 324 | |
| 325 | DPRINT_EXIT(STORVSC_DRV); |
| 326 | |
| 327 | return ret; |
| 328 | } |
| 329 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 330 | /** |
| 331 | * storvsc_remove - Callback when our device is removed |
| 332 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 333 | static int storvsc_remove(struct device *device) |
| 334 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 335 | int ret; |
| 336 | struct driver_context *driver_ctx = |
| 337 | driver_to_driver_context(device->driver); |
| 338 | struct storvsc_driver_context *storvsc_drv_ctx = |
| 339 | (struct storvsc_driver_context *)driver_ctx; |
| 340 | struct storvsc_driver_object *storvsc_drv_obj = |
| 341 | &storvsc_drv_ctx->drv_obj; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 342 | struct device_context *device_ctx = device_to_device_context(device); |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 343 | struct hv_device *device_obj = &device_ctx->device_obj; |
Greg Kroah-Hartman | 0883c52 | 2009-07-24 10:58:22 -0700 | [diff] [blame] | 344 | struct Scsi_Host *host = dev_get_drvdata(device); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 345 | struct host_device_context *host_device_ctx = |
| 346 | (struct host_device_context *)host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 347 | |
| 348 | |
| 349 | DPRINT_ENTER(STORVSC_DRV); |
| 350 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 351 | if (!storvsc_drv_obj->Base.OnDeviceRemove) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 352 | DPRINT_EXIT(STORVSC_DRV); |
| 353 | return -1; |
| 354 | } |
| 355 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 356 | /* |
| 357 | * Call to the vsc driver to let it know that the device is being |
| 358 | * removed |
| 359 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 360 | ret = storvsc_drv_obj->Base.OnDeviceRemove(device_obj); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 361 | if (ret != 0) { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 362 | /* TODO: */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 363 | DPRINT_ERR(STORVSC, "unable to remove vsc device (ret %d)", |
| 364 | ret); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 367 | if (host_device_ctx->request_pool) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 368 | kmem_cache_destroy(host_device_ctx->request_pool); |
| 369 | host_device_ctx->request_pool = NULL; |
| 370 | } |
| 371 | |
| 372 | DPRINT_INFO(STORVSC, "removing host adapter (%p)...", host); |
| 373 | scsi_remove_host(host); |
| 374 | |
| 375 | DPRINT_INFO(STORVSC, "releasing host adapter (%p)...", host); |
| 376 | scsi_host_put(host); |
| 377 | |
| 378 | DPRINT_EXIT(STORVSC_DRV); |
| 379 | |
| 380 | return ret; |
| 381 | } |
| 382 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 383 | /** |
| 384 | * storvsc_commmand_completion - Command completion processing |
| 385 | */ |
Nicolas Palix | 0b3f683 | 2009-07-29 14:10:19 +0200 | [diff] [blame] | 386 | static void storvsc_commmand_completion(struct hv_storvsc_request *request) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 387 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 388 | struct storvsc_cmd_request *cmd_request = |
| 389 | (struct storvsc_cmd_request *)request->Context; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 390 | struct scsi_cmnd *scmnd = cmd_request->cmd; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 391 | struct host_device_context *host_device_ctx = |
| 392 | (struct host_device_context *)scmnd->device->host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 393 | void (*scsi_done_fn)(struct scsi_cmnd *); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 394 | struct scsi_sense_hdr sense_hdr; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 395 | |
| 396 | ASSERT(request == &cmd_request->request); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 397 | ASSERT((unsigned long)scmnd->host_scribble == |
| 398 | (unsigned long)cmd_request); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 399 | ASSERT(scmnd); |
| 400 | ASSERT(scmnd->scsi_done); |
| 401 | |
| 402 | DPRINT_ENTER(STORVSC_DRV); |
| 403 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 404 | if (cmd_request->bounce_sgl_count) { |
| 405 | /* using bounce buffer */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 406 | /* printk("copy_from_bounce_buffer\n"); */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 407 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 408 | /* FIXME: We can optimize on writes by just skipping this */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 409 | copy_from_bounce_buffer(scsi_sglist(scmnd), |
| 410 | cmd_request->bounce_sgl, |
| 411 | scsi_sg_count(scmnd)); |
| 412 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
| 413 | cmd_request->bounce_sgl_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | scmnd->result = request->Status; |
| 417 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 418 | if (scmnd->result) { |
| 419 | if (scsi_normalize_sense(scmnd->sense_buffer, |
| 420 | request->SenseBufferSize, &sense_hdr)) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 421 | scsi_print_sense_hdr("storvsc", &sense_hdr); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | ASSERT(request->BytesXfer <= request->DataBuffer.Length); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 425 | scsi_set_resid(scmnd, request->DataBuffer.Length - request->BytesXfer); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 426 | |
| 427 | scsi_done_fn = scmnd->scsi_done; |
| 428 | |
| 429 | scmnd->host_scribble = NULL; |
| 430 | scmnd->scsi_done = NULL; |
| 431 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 432 | /* !!DO NOT MODIFY the scmnd after this call */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 433 | scsi_done_fn(scmnd); |
| 434 | |
| 435 | kmem_cache_free(host_device_ctx->request_pool, cmd_request); |
| 436 | |
| 437 | DPRINT_EXIT(STORVSC_DRV); |
| 438 | } |
| 439 | |
| 440 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count) |
| 441 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 442 | int i; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 443 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 444 | /* No need to check */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 445 | if (sg_count < 2) |
| 446 | return -1; |
| 447 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 448 | /* We have at least 2 sg entries */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 449 | for (i = 0; i < sg_count; i++) { |
| 450 | if (i == 0) { |
| 451 | /* make sure 1st one does not have hole */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 452 | if (sgl[i].offset + sgl[i].length != PAGE_SIZE) |
| 453 | return i; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 454 | } else if (i == sg_count - 1) { |
| 455 | /* make sure last one does not have hole */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 456 | if (sgl[i].offset != 0) |
| 457 | return i; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 458 | } else { |
| 459 | /* make sure no hole in the middle */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 460 | if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 461 | return i; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | return -1; |
| 465 | } |
| 466 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 467 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
| 468 | unsigned int sg_count, |
| 469 | unsigned int len) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 470 | { |
| 471 | int i; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 472 | int num_pages; |
| 473 | struct scatterlist *bounce_sgl; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 474 | struct page *page_buf; |
| 475 | |
| 476 | num_pages = ALIGN_UP(len, PAGE_SIZE) >> PAGE_SHIFT; |
| 477 | |
Nicolas Palix | 06da0bc | 2009-07-22 15:39:13 +0200 | [diff] [blame] | 478 | bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 479 | if (!bounce_sgl) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 480 | return NULL; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 481 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 482 | for (i = 0; i < num_pages; i++) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 483 | page_buf = alloc_page(GFP_ATOMIC); |
| 484 | if (!page_buf) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 485 | goto cleanup; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 486 | sg_set_page(&bounce_sgl[i], page_buf, 0, 0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | return bounce_sgl; |
| 490 | |
| 491 | cleanup: |
| 492 | destroy_bounce_buffer(bounce_sgl, num_pages); |
| 493 | return NULL; |
| 494 | } |
| 495 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 496 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
| 497 | unsigned int sg_count) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 498 | { |
| 499 | int i; |
| 500 | struct page *page_buf; |
| 501 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 502 | for (i = 0; i < sg_count; i++) { |
| 503 | page_buf = sg_page((&sgl[i])); |
| 504 | if (page_buf != NULL) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 505 | __free_page(page_buf); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | kfree(sgl); |
| 509 | } |
| 510 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 511 | /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 512 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, |
| 513 | struct scatterlist *bounce_sgl, |
| 514 | unsigned int orig_sgl_count) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 515 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 516 | int i; |
| 517 | int j = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 518 | unsigned long src, dest; |
| 519 | unsigned int srclen, destlen, copylen; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 520 | unsigned int total_copied = 0; |
| 521 | unsigned long bounce_addr = 0; |
| 522 | unsigned long src_addr = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 523 | unsigned long flags; |
| 524 | |
| 525 | local_irq_save(flags); |
| 526 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 527 | for (i = 0; i < orig_sgl_count; i++) { |
| 528 | src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 529 | KM_IRQ0) + orig_sgl[i].offset; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 530 | src = src_addr; |
| 531 | srclen = orig_sgl[i].length; |
| 532 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 533 | ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); |
| 534 | |
| 535 | if (j == 0) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 536 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 537 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 538 | while (srclen) { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 539 | /* assume bounce offset always == 0 */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 540 | dest = bounce_addr + bounce_sgl[j].length; |
| 541 | destlen = PAGE_SIZE - bounce_sgl[j].length; |
| 542 | |
Greg Kroah-Hartman | fc6a4b2 | 2009-07-15 11:05:14 -0700 | [diff] [blame] | 543 | copylen = min(srclen, destlen); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 544 | memcpy((void *)dest, (void *)src, copylen); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 545 | |
| 546 | total_copied += copylen; |
| 547 | bounce_sgl[j].length += copylen; |
| 548 | srclen -= copylen; |
| 549 | src += copylen; |
| 550 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 551 | if (bounce_sgl[j].length == PAGE_SIZE) { |
| 552 | /* full..move to next entry */ |
| 553 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 554 | j++; |
| 555 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 556 | /* if we need to use another bounce buffer */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 557 | if (srclen || i != orig_sgl_count - 1) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 558 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 559 | } else if (srclen == 0 && i == orig_sgl_count - 1) { |
| 560 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 561 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 562 | } |
| 563 | } |
| 564 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 565 | kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | local_irq_restore(flags); |
| 569 | |
| 570 | return total_copied; |
| 571 | } |
| 572 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 573 | /* Assume the original sgl has enough room */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 574 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, |
| 575 | struct scatterlist *bounce_sgl, |
| 576 | unsigned int orig_sgl_count) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 577 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 578 | int i; |
| 579 | int j = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 580 | unsigned long src, dest; |
| 581 | unsigned int srclen, destlen, copylen; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 582 | unsigned int total_copied = 0; |
| 583 | unsigned long bounce_addr = 0; |
| 584 | unsigned long dest_addr = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 585 | unsigned long flags; |
| 586 | |
| 587 | local_irq_save(flags); |
| 588 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 589 | for (i = 0; i < orig_sgl_count; i++) { |
| 590 | dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 591 | KM_IRQ0) + orig_sgl[i].offset; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 592 | dest = dest_addr; |
| 593 | destlen = orig_sgl[i].length; |
| 594 | ASSERT(orig_sgl[i].offset + orig_sgl[i].length <= PAGE_SIZE); |
| 595 | |
| 596 | if (j == 0) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 597 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 598 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 599 | while (destlen) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 600 | src = bounce_addr + bounce_sgl[j].offset; |
| 601 | srclen = bounce_sgl[j].length - bounce_sgl[j].offset; |
| 602 | |
Greg Kroah-Hartman | fc6a4b2 | 2009-07-15 11:05:14 -0700 | [diff] [blame] | 603 | copylen = min(srclen, destlen); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 604 | memcpy((void *)dest, (void *)src, copylen); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 605 | |
| 606 | total_copied += copylen; |
| 607 | bounce_sgl[j].offset += copylen; |
| 608 | destlen -= copylen; |
| 609 | dest += copylen; |
| 610 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 611 | if (bounce_sgl[j].offset == bounce_sgl[j].length) { |
| 612 | /* full */ |
| 613 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 614 | j++; |
| 615 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 616 | /* if we need to use another bounce buffer */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 617 | if (destlen || i != orig_sgl_count - 1) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 618 | bounce_addr = (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), KM_IRQ0); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 619 | } else if (destlen == 0 && i == orig_sgl_count - 1) { |
| 620 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 621 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 625 | kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset), |
| 626 | KM_IRQ0); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | local_irq_restore(flags); |
| 630 | |
| 631 | return total_copied; |
| 632 | } |
| 633 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 634 | /** |
| 635 | * storvsc_queuecommand - Initiate command processing |
| 636 | */ |
| 637 | static int storvsc_queuecommand(struct scsi_cmnd *scmnd, |
| 638 | void (*done)(struct scsi_cmnd *)) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 639 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 640 | int ret; |
| 641 | struct host_device_context *host_device_ctx = |
| 642 | (struct host_device_context *)scmnd->device->host->hostdata; |
| 643 | struct device_context *device_ctx = host_device_ctx->device_ctx; |
| 644 | struct driver_context *driver_ctx = |
| 645 | driver_to_driver_context(device_ctx->device.driver); |
| 646 | struct storvsc_driver_context *storvsc_drv_ctx = |
| 647 | (struct storvsc_driver_context *)driver_ctx; |
| 648 | struct storvsc_driver_object *storvsc_drv_obj = |
| 649 | &storvsc_drv_ctx->drv_obj; |
Nicolas Palix | 0b3f683 | 2009-07-29 14:10:19 +0200 | [diff] [blame] | 650 | struct hv_storvsc_request *request; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 651 | struct storvsc_cmd_request *cmd_request; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 652 | unsigned int request_size = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 653 | int i; |
| 654 | struct scatterlist *sgl; |
| 655 | |
| 656 | DPRINT_ENTER(STORVSC_DRV); |
| 657 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 658 | DPRINT_DBG(STORVSC_DRV, "scmnd %p dir %d, use_sg %d buf %p len %d " |
| 659 | "queue depth %d tagged %d", scmnd, scmnd->sc_data_direction, |
| 660 | scsi_sg_count(scmnd), scsi_sglist(scmnd), |
| 661 | scsi_bufflen(scmnd), scmnd->device->queue_depth, |
| 662 | scmnd->device->tagged_supported); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 663 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 664 | /* If retrying, no need to prep the cmd */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 665 | if (scmnd->host_scribble) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 666 | ASSERT(scmnd->scsi_done != NULL); |
| 667 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 668 | cmd_request = |
| 669 | (struct storvsc_cmd_request *)scmnd->host_scribble; |
| 670 | DPRINT_INFO(STORVSC_DRV, "retrying scmnd %p cmd_request %p", |
| 671 | scmnd, cmd_request); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 672 | |
| 673 | goto retry_request; |
| 674 | } |
| 675 | |
| 676 | ASSERT(scmnd->scsi_done == NULL); |
| 677 | ASSERT(scmnd->host_scribble == NULL); |
| 678 | |
| 679 | scmnd->scsi_done = done; |
| 680 | |
| 681 | request_size = sizeof(struct storvsc_cmd_request); |
| 682 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 683 | cmd_request = kmem_cache_alloc(host_device_ctx->request_pool, |
| 684 | GFP_ATOMIC); |
| 685 | if (!cmd_request) { |
| 686 | DPRINT_ERR(STORVSC_DRV, "scmnd (%p) - unable to allocate " |
| 687 | "storvsc_cmd_request...marking queue busy", scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 688 | scmnd->scsi_done = NULL; |
| 689 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 690 | } |
| 691 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 692 | /* Setup the cmd request */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 693 | cmd_request->bounce_sgl_count = 0; |
| 694 | cmd_request->bounce_sgl = NULL; |
| 695 | cmd_request->cmd = scmnd; |
| 696 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 697 | scmnd->host_scribble = (unsigned char *)cmd_request; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 698 | |
| 699 | request = &cmd_request->request; |
| 700 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 701 | request->Extension = |
| 702 | (void *)((unsigned long)cmd_request + request_size); |
| 703 | DPRINT_DBG(STORVSC_DRV, "req %p size %d ext %d", request, request_size, |
| 704 | storvsc_drv_obj->RequestExtSize); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 705 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 706 | /* Build the SRB */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 707 | switch (scmnd->sc_data_direction) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 708 | case DMA_TO_DEVICE: |
| 709 | request->Type = WRITE_TYPE; |
| 710 | break; |
| 711 | case DMA_FROM_DEVICE: |
| 712 | request->Type = READ_TYPE; |
| 713 | break; |
| 714 | default: |
| 715 | request->Type = UNKNOWN_TYPE; |
| 716 | break; |
| 717 | } |
| 718 | |
| 719 | request->OnIOCompletion = storvsc_commmand_completion; |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 720 | request->Context = cmd_request;/* scmnd; */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 721 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 722 | /* request->PortId = scmnd->device->channel; */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 723 | request->Host = host_device_ctx->port; |
| 724 | request->Bus = scmnd->device->channel; |
| 725 | request->TargetId = scmnd->device->id; |
| 726 | request->LunId = scmnd->device->lun; |
| 727 | |
| 728 | ASSERT(scmnd->cmd_len <= 16); |
| 729 | request->CdbLen = scmnd->cmd_len; |
| 730 | request->Cdb = scmnd->cmnd; |
| 731 | |
| 732 | request->SenseBuffer = scmnd->sense_buffer; |
| 733 | request->SenseBufferSize = SCSI_SENSE_BUFFERSIZE; |
| 734 | |
| 735 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 736 | request->DataBuffer.Length = scsi_bufflen(scmnd); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 737 | if (scsi_sg_count(scmnd)) { |
| 738 | sgl = (struct scatterlist *)scsi_sglist(scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 739 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 740 | /* check if we need to bounce the sgl */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 741 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { |
| 742 | DPRINT_INFO(STORVSC_DRV, |
| 743 | "need to bounce buffer for this scmnd %p", |
| 744 | scmnd); |
| 745 | cmd_request->bounce_sgl = |
| 746 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), |
| 747 | scsi_bufflen(scmnd)); |
| 748 | if (!cmd_request->bounce_sgl) { |
| 749 | DPRINT_ERR(STORVSC_DRV, |
| 750 | "unable to create bounce buffer for " |
| 751 | "this scmnd %p", scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 752 | |
| 753 | scmnd->scsi_done = NULL; |
| 754 | scmnd->host_scribble = NULL; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 755 | kmem_cache_free(host_device_ctx->request_pool, |
| 756 | cmd_request); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 757 | |
| 758 | return SCSI_MLQUEUE_HOST_BUSY; |
| 759 | } |
| 760 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 761 | cmd_request->bounce_sgl_count = |
| 762 | ALIGN_UP(scsi_bufflen(scmnd), PAGE_SIZE) >> |
| 763 | PAGE_SHIFT; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 764 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 765 | /* |
| 766 | * FIXME: We can optimize on reads by just skipping |
| 767 | * this |
| 768 | */ |
| 769 | copy_to_bounce_buffer(sgl, cmd_request->bounce_sgl, |
| 770 | scsi_sg_count(scmnd)); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 771 | |
| 772 | sgl = cmd_request->bounce_sgl; |
| 773 | } |
| 774 | |
| 775 | request->DataBuffer.Offset = sgl[0].offset; |
| 776 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 777 | for (i = 0; i < scsi_sg_count(scmnd); i++) { |
| 778 | DPRINT_DBG(STORVSC_DRV, "sgl[%d] len %d offset %d \n", |
| 779 | i, sgl[i].length, sgl[i].offset); |
| 780 | request->DataBuffer.PfnArray[i] = |
| 781 | page_to_pfn(sg_page((&sgl[i]))); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 782 | } |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 783 | } else if (scsi_sglist(scmnd)) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 784 | ASSERT(scsi_bufflen(scmnd) <= PAGE_SIZE); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 785 | request->DataBuffer.Offset = |
| 786 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); |
| 787 | request->DataBuffer.PfnArray[0] = |
| 788 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; |
| 789 | } else { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 790 | ASSERT(scsi_bufflen(scmnd) == 0); |
| 791 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 792 | |
| 793 | retry_request: |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 794 | /* Invokes the vsc to start an IO */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 795 | ret = storvsc_drv_obj->OnIORequest(&device_ctx->device_obj, |
| 796 | &cmd_request->request); |
| 797 | if (ret == -1) { |
| 798 | /* no more space */ |
| 799 | DPRINT_ERR(STORVSC_DRV, |
| 800 | "scmnd (%p) - queue FULL...marking queue busy", |
| 801 | scmnd); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 802 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 803 | if (cmd_request->bounce_sgl_count) { |
| 804 | /* |
| 805 | * FIXME: We can optimize on writes by just skipping |
| 806 | * this |
| 807 | */ |
| 808 | copy_from_bounce_buffer(scsi_sglist(scmnd), |
| 809 | cmd_request->bounce_sgl, |
| 810 | scsi_sg_count(scmnd)); |
| 811 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
| 812 | cmd_request->bounce_sgl_count); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | kmem_cache_free(host_device_ctx->request_pool, cmd_request); |
| 816 | |
| 817 | scmnd->scsi_done = NULL; |
| 818 | scmnd->host_scribble = NULL; |
| 819 | |
| 820 | ret = SCSI_MLQUEUE_DEVICE_BUSY; |
| 821 | } |
| 822 | |
| 823 | DPRINT_EXIT(STORVSC_DRV); |
| 824 | |
| 825 | return ret; |
| 826 | } |
| 827 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 828 | static int storvsc_merge_bvec(struct request_queue *q, |
| 829 | struct bvec_merge_data *bmd, struct bio_vec *bvec) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 830 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 831 | /* checking done by caller. */ |
| 832 | return bvec->bv_len; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 833 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 834 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 835 | /** |
| 836 | * storvsc_device_configure - Configure the specified scsi device |
| 837 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 838 | static int storvsc_device_alloc(struct scsi_device *sdevice) |
| 839 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 840 | DPRINT_DBG(STORVSC_DRV, "sdev (%p) - setting device flag to %d", |
| 841 | sdevice, BLIST_SPARSELUN); |
| 842 | /* |
| 843 | * This enables luns to be located sparsely. Otherwise, we may not |
| 844 | * discovered them. |
| 845 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 846 | sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | static int storvsc_device_configure(struct scsi_device *sdevice) |
| 851 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 852 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - curr queue depth %d", sdevice, |
| 853 | sdevice->queue_depth); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 854 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 855 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting queue depth to %d", |
| 856 | sdevice, STORVSC_MAX_IO_REQUESTS); |
| 857 | scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, |
| 858 | STORVSC_MAX_IO_REQUESTS); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 859 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 860 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - setting max segment size to %ld", |
| 861 | sdevice, PAGE_SIZE); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 862 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); |
| 863 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 864 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) - adding merge bio vec routine", |
| 865 | sdevice); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 866 | blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec); |
| 867 | |
| 868 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 869 | /* sdevice->timeout = (2000 * HZ);//(75 * HZ); */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 870 | |
| 871 | return 0; |
| 872 | } |
| 873 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 874 | /** |
| 875 | * storvsc_host_reset_handler - Reset the scsi HBA |
| 876 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 877 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) |
| 878 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 879 | int ret; |
| 880 | struct host_device_context *host_device_ctx = |
| 881 | (struct host_device_context *)scmnd->device->host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 882 | struct device_context *device_ctx = host_device_ctx->device_ctx; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 883 | struct driver_context *driver_ctx = |
| 884 | driver_to_driver_context(device_ctx->device.driver); |
| 885 | struct storvsc_driver_context *storvsc_drv_ctx = |
| 886 | (struct storvsc_driver_context *)driver_ctx; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 887 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 888 | struct storvsc_driver_object *storvsc_drv_obj = |
| 889 | &storvsc_drv_ctx->drv_obj; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 890 | |
| 891 | DPRINT_ENTER(STORVSC_DRV); |
| 892 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 893 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host resetting...", |
| 894 | scmnd->device, &device_ctx->device_obj); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 895 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 896 | /* Invokes the vsc to reset the host/bus */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 897 | ASSERT(storvsc_drv_obj->OnHostReset); |
| 898 | ret = storvsc_drv_obj->OnHostReset(&device_ctx->device_obj); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 899 | if (ret != 0) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 900 | DPRINT_EXIT(STORVSC_DRV); |
| 901 | return ret; |
| 902 | } |
| 903 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 904 | DPRINT_INFO(STORVSC_DRV, "sdev (%p) dev obj (%p) - host reseted", |
| 905 | scmnd->device, &device_ctx->device_obj); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 906 | |
| 907 | DPRINT_EXIT(STORVSC_DRV); |
| 908 | |
| 909 | return ret; |
| 910 | } |
| 911 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 912 | /** |
| 913 | * storvsc_host_rescan - Rescan the scsi HBA |
| 914 | */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 915 | static void storvsc_host_rescan_callback(struct work_struct *work) |
| 916 | { |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 917 | struct hv_device *device_obj = |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 918 | &((struct host_device_context *)work)->device_ctx->device_obj; |
| 919 | struct device_context *device_ctx = to_device_context(device_obj); |
Greg Kroah-Hartman | 0883c52 | 2009-07-24 10:58:22 -0700 | [diff] [blame] | 920 | struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 921 | struct scsi_device *sdev; |
| 922 | struct host_device_context *host_device_ctx; |
| 923 | struct scsi_device **sdevs_remove_list; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 924 | unsigned int sdevs_count = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 925 | unsigned int found; |
| 926 | unsigned int i; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 927 | unsigned int lun_count = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 928 | unsigned int *lun_list; |
| 929 | |
| 930 | DPRINT_ENTER(STORVSC_DRV); |
| 931 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 932 | host_device_ctx = (struct host_device_context *)host->hostdata; |
| 933 | lun_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, sizeof(unsigned int), |
| 934 | GFP_ATOMIC); |
| 935 | if (!lun_list) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 936 | DPRINT_ERR(STORVSC_DRV, "unable to allocate lun list"); |
| 937 | return; |
| 938 | } |
| 939 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 940 | sdevs_remove_list = kcalloc(STORVSC_MAX_LUNS_PER_TARGET, |
| 941 | sizeof(void *), GFP_ATOMIC); |
| 942 | if (!sdevs_remove_list) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 943 | kfree(lun_list); |
| 944 | DPRINT_ERR(STORVSC_DRV, "unable to allocate lun remove list"); |
| 945 | return; |
| 946 | } |
| 947 | |
Bill Pemberton | 7f6f27d | 2009-07-27 16:47:33 -0400 | [diff] [blame] | 948 | DPRINT_INFO(STORVSC_DRV, "rescanning host for new scsi devices..."); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 949 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 950 | /* Rescan for new device */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 951 | scsi_scan_target(&host->shost_gendev, host_device_ctx->path, |
| 952 | host_device_ctx->target, SCAN_WILD_CARD, 1); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 953 | |
| 954 | DPRINT_INFO(STORVSC_DRV, "rescanning host for removed scsi device..."); |
| 955 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 956 | /* Use the 1st device to send the report luns cmd */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 957 | shost_for_each_device(sdev, host) { |
| 958 | lun_count = STORVSC_MAX_LUNS_PER_TARGET; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 959 | storvsc_report_luns(sdev, lun_list, &lun_count); |
| 960 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 961 | DPRINT_INFO(STORVSC_DRV, |
| 962 | "report luns on scsi device (%p) found %u luns ", |
| 963 | sdev, lun_count); |
| 964 | DPRINT_INFO(STORVSC_DRV, |
| 965 | "existing luns on scsi device (%p) host (%d)", |
| 966 | sdev, host->host_no); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 967 | |
| 968 | scsi_device_put(sdev); |
| 969 | break; |
| 970 | } |
| 971 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 972 | for (i = 0; i < lun_count; i++) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 973 | DPRINT_INFO(STORVSC_DRV, "%d) lun %u", i, lun_list[i]); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 974 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 975 | /* Rescan for devices that may have been removed. |
| 976 | * We do not have to worry that new devices may have been added since |
| 977 | * this callback is serialized by the workqueue ie add/remove are done |
| 978 | * here. |
| 979 | */ |
| 980 | shost_for_each_device(sdev, host) { |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 981 | /* See if this device is still here */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 982 | found = 0; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 983 | for (i = 0; i < lun_count; i++) { |
| 984 | if (sdev->lun == lun_list[i]) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 985 | found = 1; |
| 986 | break; |
| 987 | } |
| 988 | } |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 989 | if (!found) { |
| 990 | DPRINT_INFO(STORVSC_DRV, "lun (%u) does not exists", |
| 991 | sdev->lun); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 992 | sdevs_remove_list[sdevs_count++] = sdev; |
| 993 | } |
| 994 | } |
| 995 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 996 | /* Now remove the devices */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 997 | for (i = 0; i < sdevs_count; i++) { |
| 998 | DPRINT_INFO(STORVSC_DRV, |
| 999 | "removing scsi device (%p) lun (%u)...", |
| 1000 | sdevs_remove_list[i], sdevs_remove_list[i]->lun); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1001 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1002 | /* make sure it is not removed from underneath us */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1003 | if (!scsi_device_get(sdevs_remove_list[i])) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1004 | scsi_remove_device(sdevs_remove_list[i]); |
| 1005 | scsi_device_put(sdevs_remove_list[i]); |
| 1006 | } |
| 1007 | } |
| 1008 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1009 | DPRINT_INFO(STORVSC_DRV, "rescan completed on dev obj (%p) " |
| 1010 | "target (%u) bus (%u)", device_obj, |
| 1011 | host_device_ctx->target, host_device_ctx->path); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1012 | |
| 1013 | kfree(lun_list); |
| 1014 | kfree(sdevs_remove_list); |
| 1015 | |
| 1016 | DPRINT_EXIT(STORVSC_DRV); |
| 1017 | } |
| 1018 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1019 | static int storvsc_report_luns(struct scsi_device *sdev, unsigned int luns[], |
| 1020 | unsigned int *lun_count) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1021 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1022 | int i, j; |
| 1023 | unsigned int lun = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1024 | unsigned int num_luns; |
| 1025 | int result; |
| 1026 | unsigned char *data; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1027 | struct scsi_sense_hdr sshdr; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1028 | unsigned char cmd[16] = {0}; |
| 1029 | /* Add 1 to cover the report_lun header */ |
| 1030 | unsigned int report_len = 8 * (STORVSC_MAX_LUNS_PER_TARGET+1); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1031 | unsigned long long *report_luns; |
| 1032 | const unsigned int in_lun_count = *lun_count; |
| 1033 | |
| 1034 | *lun_count = 0; |
| 1035 | |
| 1036 | report_luns = kzalloc(report_len, GFP_ATOMIC); |
| 1037 | if (!report_luns) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1038 | return -ENOMEM; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1039 | |
| 1040 | cmd[0] = REPORT_LUNS; |
| 1041 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1042 | /* cmd length */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1043 | *(unsigned int *)&cmd[6] = cpu_to_be32(report_len); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1044 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1045 | result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, |
| 1046 | (unsigned char *)report_luns, report_len, |
| 1047 | &sshdr, 30 * HZ, 3, NULL); |
| 1048 | if (result != 0) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1049 | kfree(report_luns); |
| 1050 | return -EBUSY; |
| 1051 | } |
| 1052 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1053 | /* get the length from the first four bytes */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1054 | report_len = be32_to_cpu(*(unsigned int *)&report_luns[0]); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1055 | |
| 1056 | num_luns = (report_len / sizeof(unsigned long long)); |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1057 | if (num_luns > in_lun_count) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1058 | kfree(report_luns); |
| 1059 | return -EINVAL; |
| 1060 | } |
| 1061 | |
| 1062 | *lun_count = num_luns; |
| 1063 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1064 | DPRINT_DBG(STORVSC_DRV, |
| 1065 | "report luns on scsi device (%p) found %u luns ", |
| 1066 | sdev, num_luns); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1067 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1068 | /* lun id starts at 1 */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1069 | for (i = 1; i < num_luns + 1; i++) { |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1070 | lun = 0; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1071 | data = (unsigned char *)&report_luns[i]; |
| 1072 | for (j = 0; j < sizeof(lun); j += 2) { |
| 1073 | lun = lun | (((data[j] << 8) | data[j + 1]) << |
| 1074 | (j * 8)); |
| 1075 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1076 | |
| 1077 | luns[i-1] = lun; |
| 1078 | } |
| 1079 | |
| 1080 | kfree(report_luns); |
| 1081 | return 0; |
| 1082 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1083 | |
Nicolas Palix | 3d3b551 | 2009-07-28 17:32:53 +0200 | [diff] [blame] | 1084 | static void storvsc_host_rescan(struct hv_device *device_obj) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1085 | { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1086 | struct device_context *device_ctx = to_device_context(device_obj); |
Greg Kroah-Hartman | 0883c52 | 2009-07-24 10:58:22 -0700 | [diff] [blame] | 1087 | struct Scsi_Host *host = dev_get_drvdata(&device_ctx->device); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1088 | struct host_device_context *host_device_ctx; |
| 1089 | |
| 1090 | DPRINT_ENTER(STORVSC_DRV); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1091 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1092 | host_device_ctx = (struct host_device_context *)host->hostdata; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1093 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1094 | DPRINT_INFO(STORVSC_DRV, "initiating rescan on dev obj (%p) " |
| 1095 | "target (%u) bus (%u)...", device_obj, |
| 1096 | host_device_ctx->target, host_device_ctx->path); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1097 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1098 | /* |
| 1099 | * We need to queue this since the scanning may block and the caller |
| 1100 | * may be in an intr context |
| 1101 | */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1102 | /* scsi_queue_work(host, &host_device_ctx->host_rescan_work); */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1103 | schedule_work(&host_device_ctx->host_rescan_work); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1104 | DPRINT_EXIT(STORVSC_DRV); |
| 1105 | } |
| 1106 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1107 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
| 1108 | sector_t capacity, int *info) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1109 | { |
| 1110 | sector_t total_sectors = capacity; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1111 | sector_t cylinder_times_heads = 0; |
| 1112 | sector_t temp = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1113 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1114 | int sectors_per_track = 0; |
| 1115 | int heads = 0; |
| 1116 | int cylinders = 0; |
| 1117 | int rem = 0; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1118 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1119 | if (total_sectors > (65535 * 16 * 255)) |
| 1120 | total_sectors = (65535 * 16 * 255); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1121 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1122 | if (total_sectors >= (65535 * 16 * 63)) { |
| 1123 | sectors_per_track = 255; |
| 1124 | heads = 16; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1125 | |
| 1126 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1127 | /* sector_div stores the quotient in cylinder_times_heads */ |
| 1128 | rem = sector_div(cylinder_times_heads, sectors_per_track); |
| 1129 | } else { |
| 1130 | sectors_per_track = 17; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1131 | |
| 1132 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1133 | /* sector_div stores the quotient in cylinder_times_heads */ |
| 1134 | rem = sector_div(cylinder_times_heads, sectors_per_track); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1135 | |
| 1136 | temp = cylinder_times_heads + 1023; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1137 | /* sector_div stores the quotient in temp */ |
| 1138 | rem = sector_div(temp, 1024); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1139 | |
| 1140 | heads = temp; |
| 1141 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1142 | if (heads < 4) |
| 1143 | heads = 4; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1144 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1145 | if (cylinder_times_heads >= (heads * 1024) || (heads > 16)) { |
| 1146 | sectors_per_track = 31; |
| 1147 | heads = 16; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1148 | |
| 1149 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1150 | /* |
| 1151 | * sector_div stores the quotient in |
| 1152 | * cylinder_times_heads |
| 1153 | */ |
| 1154 | rem = sector_div(cylinder_times_heads, |
| 1155 | sectors_per_track); |
| 1156 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1157 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1158 | if (cylinder_times_heads >= (heads * 1024)) { |
| 1159 | sectors_per_track = 63; |
| 1160 | heads = 16; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1161 | |
| 1162 | cylinder_times_heads = total_sectors; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1163 | /* |
| 1164 | * sector_div stores the quotient in |
| 1165 | * cylinder_times_heads |
| 1166 | */ |
| 1167 | rem = sector_div(cylinder_times_heads, |
| 1168 | sectors_per_track); |
| 1169 | } |
| 1170 | } |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1171 | |
| 1172 | temp = cylinder_times_heads; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1173 | /* sector_div stores the quotient in temp */ |
| 1174 | rem = sector_div(temp, heads); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1175 | cylinders = temp; |
| 1176 | |
| 1177 | info[0] = heads; |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1178 | info[1] = sectors_per_track; |
| 1179 | info[2] = cylinders; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1180 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1181 | DPRINT_INFO(STORVSC_DRV, "CHS (%d, %d, %d)", cylinders, heads, |
| 1182 | sectors_per_track); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1183 | |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1187 | static int __init storvsc_init(void) |
| 1188 | { |
| 1189 | int ret; |
| 1190 | |
| 1191 | DPRINT_ENTER(STORVSC_DRV); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1192 | DPRINT_INFO(STORVSC_DRV, "Storvsc initializing...."); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1193 | ret = storvsc_drv_init(StorVscInitialize); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1194 | DPRINT_EXIT(STORVSC_DRV); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1195 | return ret; |
| 1196 | } |
| 1197 | |
| 1198 | static void __exit storvsc_exit(void) |
| 1199 | { |
| 1200 | DPRINT_ENTER(STORVSC_DRV); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1201 | storvsc_drv_exit(); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1202 | DPRINT_ENTER(STORVSC_DRV); |
| 1203 | } |
| 1204 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame^] | 1205 | MODULE_LICENSE("GPL"); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1206 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1207 | module_init(storvsc_init); |
| 1208 | module_exit(storvsc_exit); |