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> |
K. Y. Srinivasan | 972621c | 2011-05-10 07:54:19 -0700 | [diff] [blame] | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 21 | */ |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame^] | 22 | |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/sched.h> |
| 25 | #include <linux/completion.h> |
| 26 | #include <linux/string.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/delay.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 29 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 31 | #include <linux/module.h> |
| 32 | #include <linux/device.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 33 | #include <scsi/scsi.h> |
| 34 | #include <scsi/scsi_cmnd.h> |
| 35 | #include <scsi/scsi_host.h> |
| 36 | #include <scsi/scsi_device.h> |
| 37 | #include <scsi/scsi_tcq.h> |
| 38 | #include <scsi/scsi_eh.h> |
| 39 | #include <scsi/scsi_devinfo.h> |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 40 | #include <scsi/scsi_dbg.h> |
K. Y. Srinivasan | 3f335ea | 2011-05-12 19:34:15 -0700 | [diff] [blame] | 41 | |
| 42 | #include "hyperv.h" |
K. Y. Srinivasan | cdee150 | 2011-05-12 19:34:34 -0700 | [diff] [blame] | 43 | #include "hyperv_storage.h" |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 44 | |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 45 | |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame^] | 46 | static int storvsc_ringbuffer_size = STORVSC_RING_BUFFER_SIZE; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 47 | |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame^] | 48 | module_param(storvsc_ringbuffer_size, int, S_IRUGO); |
| 49 | MODULE_PARM_DESC(storvsc_ringbuffer_size, "Ring buffer size (bytes)"); |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 50 | |
K. Y. Srinivasan | a1be170 | 2011-08-27 11:31:23 -0700 | [diff] [blame^] | 51 | struct hv_host_device { |
| 52 | struct hv_device *dev; |
| 53 | struct kmem_cache *request_pool; |
| 54 | unsigned int port; |
| 55 | unsigned char path; |
| 56 | unsigned char target; |
| 57 | }; |
| 58 | |
| 59 | struct storvsc_cmd_request { |
| 60 | struct list_head entry; |
| 61 | struct scsi_cmnd *cmd; |
| 62 | |
| 63 | unsigned int bounce_sgl_count; |
| 64 | struct scatterlist *bounce_sgl; |
| 65 | |
| 66 | struct hv_storvsc_request request; |
| 67 | }; |
K. Y. Srinivasan | 8dcf37d | 2011-08-27 11:31:22 -0700 | [diff] [blame] | 68 | |
| 69 | static inline struct storvsc_device *alloc_stor_device(struct hv_device *device) |
| 70 | { |
| 71 | struct storvsc_device *stor_device; |
| 72 | |
| 73 | stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL); |
| 74 | if (!stor_device) |
| 75 | return NULL; |
| 76 | |
| 77 | stor_device->destroy = false; |
| 78 | init_waitqueue_head(&stor_device->waiting_to_drain); |
| 79 | stor_device->device = device; |
| 80 | device->ext = stor_device; |
| 81 | |
| 82 | return stor_device; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static inline struct storvsc_device *get_in_stor_device( |
| 87 | struct hv_device *device) |
| 88 | { |
| 89 | struct storvsc_device *stor_device; |
| 90 | unsigned long flags; |
| 91 | |
| 92 | spin_lock_irqsave(&device->channel->inbound_lock, flags); |
| 93 | stor_device = (struct storvsc_device *)device->ext; |
| 94 | |
| 95 | if (!stor_device) |
| 96 | goto get_in_err; |
| 97 | |
| 98 | /* |
| 99 | * If the device is being destroyed; allow incoming |
| 100 | * traffic only to cleanup outstanding requests. |
| 101 | */ |
| 102 | |
| 103 | if (stor_device->destroy && |
| 104 | (atomic_read(&stor_device->num_outstanding_req) == 0)) |
| 105 | stor_device = NULL; |
| 106 | |
| 107 | get_in_err: |
| 108 | spin_unlock_irqrestore(&device->channel->inbound_lock, flags); |
| 109 | return stor_device; |
| 110 | |
| 111 | } |
| 112 | |
| 113 | static int storvsc_channel_init(struct hv_device *device) |
| 114 | { |
| 115 | struct storvsc_device *stor_device; |
| 116 | struct hv_storvsc_request *request; |
| 117 | struct vstor_packet *vstor_packet; |
| 118 | int ret, t; |
| 119 | |
| 120 | stor_device = get_out_stor_device(device); |
| 121 | if (!stor_device) |
| 122 | return -ENODEV; |
| 123 | |
| 124 | request = &stor_device->init_request; |
| 125 | vstor_packet = &request->vstor_packet; |
| 126 | |
| 127 | /* |
| 128 | * Now, initiate the vsc/vsp initialization protocol on the open |
| 129 | * channel |
| 130 | */ |
| 131 | memset(request, 0, sizeof(struct hv_storvsc_request)); |
| 132 | init_completion(&request->wait_event); |
| 133 | vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION; |
| 134 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 135 | |
| 136 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 137 | sizeof(struct vstor_packet), |
| 138 | (unsigned long)request, |
| 139 | VM_PKT_DATA_INBAND, |
| 140 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 141 | if (ret != 0) |
| 142 | goto cleanup; |
| 143 | |
| 144 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 145 | if (t == 0) { |
| 146 | ret = -ETIMEDOUT; |
| 147 | goto cleanup; |
| 148 | } |
| 149 | |
| 150 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 151 | vstor_packet->status != 0) |
| 152 | goto cleanup; |
| 153 | |
| 154 | |
| 155 | /* reuse the packet for version range supported */ |
| 156 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 157 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION; |
| 158 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 159 | |
| 160 | vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT; |
| 161 | FILL_VMSTOR_REVISION(vstor_packet->version.revision); |
| 162 | |
| 163 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 164 | sizeof(struct vstor_packet), |
| 165 | (unsigned long)request, |
| 166 | VM_PKT_DATA_INBAND, |
| 167 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 168 | if (ret != 0) |
| 169 | goto cleanup; |
| 170 | |
| 171 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 172 | if (t == 0) { |
| 173 | ret = -ETIMEDOUT; |
| 174 | goto cleanup; |
| 175 | } |
| 176 | |
| 177 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 178 | vstor_packet->status != 0) |
| 179 | goto cleanup; |
| 180 | |
| 181 | |
| 182 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 183 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES; |
| 184 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 185 | vstor_packet->storage_channel_properties.port_number = |
| 186 | stor_device->port_number; |
| 187 | |
| 188 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 189 | sizeof(struct vstor_packet), |
| 190 | (unsigned long)request, |
| 191 | VM_PKT_DATA_INBAND, |
| 192 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 193 | |
| 194 | if (ret != 0) |
| 195 | goto cleanup; |
| 196 | |
| 197 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 198 | if (t == 0) { |
| 199 | ret = -ETIMEDOUT; |
| 200 | goto cleanup; |
| 201 | } |
| 202 | |
| 203 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 204 | vstor_packet->status != 0) |
| 205 | goto cleanup; |
| 206 | |
| 207 | stor_device->path_id = vstor_packet->storage_channel_properties.path_id; |
| 208 | stor_device->target_id |
| 209 | = vstor_packet->storage_channel_properties.target_id; |
| 210 | |
| 211 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
| 212 | vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION; |
| 213 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 214 | |
| 215 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 216 | sizeof(struct vstor_packet), |
| 217 | (unsigned long)request, |
| 218 | VM_PKT_DATA_INBAND, |
| 219 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 220 | |
| 221 | if (ret != 0) |
| 222 | goto cleanup; |
| 223 | |
| 224 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
| 225 | if (t == 0) { |
| 226 | ret = -ETIMEDOUT; |
| 227 | goto cleanup; |
| 228 | } |
| 229 | |
| 230 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
| 231 | vstor_packet->status != 0) |
| 232 | goto cleanup; |
| 233 | |
| 234 | |
| 235 | cleanup: |
| 236 | return ret; |
| 237 | } |
| 238 | |
| 239 | static void storvsc_on_io_completion(struct hv_device *device, |
| 240 | struct vstor_packet *vstor_packet, |
| 241 | struct hv_storvsc_request *request) |
| 242 | { |
| 243 | struct storvsc_device *stor_device; |
| 244 | struct vstor_packet *stor_pkt; |
| 245 | |
| 246 | stor_device = (struct storvsc_device *)device->ext; |
| 247 | |
| 248 | stor_pkt = &request->vstor_packet; |
| 249 | |
| 250 | |
| 251 | /* Copy over the status...etc */ |
| 252 | stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status; |
| 253 | stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status; |
| 254 | stor_pkt->vm_srb.sense_info_length = |
| 255 | vstor_packet->vm_srb.sense_info_length; |
| 256 | |
| 257 | if (vstor_packet->vm_srb.scsi_status != 0 || |
| 258 | vstor_packet->vm_srb.srb_status != 1){ |
| 259 | DPRINT_WARN(STORVSC, |
| 260 | "cmd 0x%x scsi status 0x%x srb status 0x%x\n", |
| 261 | stor_pkt->vm_srb.cdb[0], |
| 262 | vstor_packet->vm_srb.scsi_status, |
| 263 | vstor_packet->vm_srb.srb_status); |
| 264 | } |
| 265 | |
| 266 | if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) { |
| 267 | /* CHECK_CONDITION */ |
| 268 | if (vstor_packet->vm_srb.srb_status & 0x80) { |
| 269 | /* autosense data available */ |
| 270 | DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data " |
| 271 | "valid - len %d\n", request, |
| 272 | vstor_packet->vm_srb.sense_info_length); |
| 273 | |
| 274 | memcpy(request->sense_buffer, |
| 275 | vstor_packet->vm_srb.sense_data, |
| 276 | vstor_packet->vm_srb.sense_info_length); |
| 277 | |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | stor_pkt->vm_srb.data_transfer_length = |
| 282 | vstor_packet->vm_srb.data_transfer_length; |
| 283 | |
| 284 | request->on_io_completion(request); |
| 285 | |
| 286 | if (atomic_dec_and_test(&stor_device->num_outstanding_req) && |
| 287 | stor_device->drain_notify) |
| 288 | wake_up(&stor_device->waiting_to_drain); |
| 289 | |
| 290 | |
| 291 | } |
| 292 | |
| 293 | static void storvsc_on_receive(struct hv_device *device, |
| 294 | struct vstor_packet *vstor_packet, |
| 295 | struct hv_storvsc_request *request) |
| 296 | { |
| 297 | switch (vstor_packet->operation) { |
| 298 | case VSTOR_OPERATION_COMPLETE_IO: |
| 299 | storvsc_on_io_completion(device, vstor_packet, request); |
| 300 | break; |
| 301 | case VSTOR_OPERATION_REMOVE_DEVICE: |
| 302 | |
| 303 | default: |
| 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | static void storvsc_on_channel_callback(void *context) |
| 309 | { |
| 310 | struct hv_device *device = (struct hv_device *)context; |
| 311 | struct storvsc_device *stor_device; |
| 312 | u32 bytes_recvd; |
| 313 | u64 request_id; |
| 314 | unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)]; |
| 315 | struct hv_storvsc_request *request; |
| 316 | int ret; |
| 317 | |
| 318 | |
| 319 | stor_device = get_in_stor_device(device); |
| 320 | if (!stor_device) |
| 321 | return; |
| 322 | |
| 323 | do { |
| 324 | ret = vmbus_recvpacket(device->channel, packet, |
| 325 | ALIGN(sizeof(struct vstor_packet), 8), |
| 326 | &bytes_recvd, &request_id); |
| 327 | if (ret == 0 && bytes_recvd > 0) { |
| 328 | |
| 329 | request = (struct hv_storvsc_request *) |
| 330 | (unsigned long)request_id; |
| 331 | |
| 332 | if ((request == &stor_device->init_request) || |
| 333 | (request == &stor_device->reset_request)) { |
| 334 | |
| 335 | memcpy(&request->vstor_packet, packet, |
| 336 | sizeof(struct vstor_packet)); |
| 337 | complete(&request->wait_event); |
| 338 | } else { |
| 339 | storvsc_on_receive(device, |
| 340 | (struct vstor_packet *)packet, |
| 341 | request); |
| 342 | } |
| 343 | } else { |
| 344 | break; |
| 345 | } |
| 346 | } while (1); |
| 347 | |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size) |
| 352 | { |
| 353 | struct vmstorage_channel_properties props; |
| 354 | int ret; |
| 355 | |
| 356 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); |
| 357 | |
| 358 | /* Open the channel */ |
| 359 | ret = vmbus_open(device->channel, |
| 360 | ring_size, |
| 361 | ring_size, |
| 362 | (void *)&props, |
| 363 | sizeof(struct vmstorage_channel_properties), |
| 364 | storvsc_on_channel_callback, device); |
| 365 | |
| 366 | if (ret != 0) |
| 367 | return ret; |
| 368 | |
| 369 | ret = storvsc_channel_init(device); |
| 370 | |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | int storvsc_dev_add(struct hv_device *device, |
| 375 | void *additional_info) |
| 376 | { |
| 377 | struct storvsc_device *stor_device; |
| 378 | struct storvsc_device_info *device_info; |
| 379 | int ret = 0; |
| 380 | |
| 381 | device_info = (struct storvsc_device_info *)additional_info; |
| 382 | stor_device = alloc_stor_device(device); |
| 383 | if (!stor_device) |
| 384 | return -ENOMEM; |
| 385 | |
| 386 | /* Save the channel properties to our storvsc channel */ |
| 387 | |
| 388 | /* |
| 389 | * If we support more than 1 scsi channel, we need to set the |
| 390 | * port number here to the scsi channel but how do we get the |
| 391 | * scsi channel prior to the bus scan. |
| 392 | * |
| 393 | * The host does not support this. |
| 394 | */ |
| 395 | |
| 396 | stor_device->port_number = device_info->port_number; |
| 397 | /* Send it back up */ |
| 398 | ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size); |
| 399 | if (ret) { |
| 400 | kfree(stor_device); |
| 401 | return ret; |
| 402 | } |
| 403 | device_info->path_id = stor_device->path_id; |
| 404 | device_info->target_id = stor_device->target_id; |
| 405 | |
| 406 | return ret; |
| 407 | } |
| 408 | |
| 409 | int storvsc_dev_remove(struct hv_device *device) |
| 410 | { |
| 411 | struct storvsc_device *stor_device; |
| 412 | unsigned long flags; |
| 413 | |
| 414 | stor_device = (struct storvsc_device *)device->ext; |
| 415 | |
| 416 | spin_lock_irqsave(&device->channel->inbound_lock, flags); |
| 417 | stor_device->destroy = true; |
| 418 | spin_unlock_irqrestore(&device->channel->inbound_lock, flags); |
| 419 | |
| 420 | /* |
| 421 | * At this point, all outbound traffic should be disable. We |
| 422 | * only allow inbound traffic (responses) to proceed so that |
| 423 | * outstanding requests can be completed. |
| 424 | */ |
| 425 | |
| 426 | storvsc_wait_to_drain(stor_device); |
| 427 | |
| 428 | /* |
| 429 | * Since we have already drained, we don't need to busy wait |
| 430 | * as was done in final_release_stor_device() |
| 431 | * Note that we cannot set the ext pointer to NULL until |
| 432 | * we have drained - to drain the outgoing packets, we need to |
| 433 | * allow incoming packets. |
| 434 | */ |
| 435 | spin_lock_irqsave(&device->channel->inbound_lock, flags); |
| 436 | device->ext = NULL; |
| 437 | spin_unlock_irqrestore(&device->channel->inbound_lock, flags); |
| 438 | |
| 439 | /* Close the channel */ |
| 440 | vmbus_close(device->channel); |
| 441 | |
| 442 | kfree(stor_device); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | int storvsc_do_io(struct hv_device *device, |
| 447 | struct hv_storvsc_request *request) |
| 448 | { |
| 449 | struct storvsc_device *stor_device; |
| 450 | struct vstor_packet *vstor_packet; |
| 451 | int ret = 0; |
| 452 | |
| 453 | vstor_packet = &request->vstor_packet; |
| 454 | stor_device = get_out_stor_device(device); |
| 455 | |
| 456 | if (!stor_device) |
| 457 | return -ENODEV; |
| 458 | |
| 459 | |
| 460 | request->device = device; |
| 461 | |
| 462 | |
| 463 | vstor_packet->flags |= REQUEST_COMPLETION_FLAG; |
| 464 | |
| 465 | vstor_packet->vm_srb.length = sizeof(struct vmscsi_request); |
| 466 | |
| 467 | |
| 468 | vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE; |
| 469 | |
| 470 | |
| 471 | vstor_packet->vm_srb.data_transfer_length = |
| 472 | request->data_buffer.len; |
| 473 | |
| 474 | vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB; |
| 475 | |
| 476 | if (request->data_buffer.len) { |
| 477 | ret = vmbus_sendpacket_multipagebuffer(device->channel, |
| 478 | &request->data_buffer, |
| 479 | vstor_packet, |
| 480 | sizeof(struct vstor_packet), |
| 481 | (unsigned long)request); |
| 482 | } else { |
| 483 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 484 | sizeof(struct vstor_packet), |
| 485 | (unsigned long)request, |
| 486 | VM_PKT_DATA_INBAND, |
| 487 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 488 | } |
| 489 | |
| 490 | if (ret != 0) |
| 491 | return ret; |
| 492 | |
| 493 | atomic_inc(&stor_device->num_outstanding_req); |
| 494 | |
| 495 | return ret; |
| 496 | } |
| 497 | |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 498 | static void storvsc_get_ide_info(struct hv_device *dev, int *target, int *path) |
| 499 | { |
| 500 | *target = |
| 501 | dev->dev_instance.b[5] << 8 | dev->dev_instance.b[4]; |
| 502 | |
| 503 | *path = |
| 504 | dev->dev_instance.b[3] << 24 | |
| 505 | dev->dev_instance.b[2] << 16 | |
| 506 | dev->dev_instance.b[1] << 8 | dev->dev_instance.b[0]; |
| 507 | } |
| 508 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 509 | |
K. Y. Srinivasan | 5b60ace | 2011-05-10 07:54:25 -0700 | [diff] [blame] | 510 | static int storvsc_device_alloc(struct scsi_device *sdevice) |
| 511 | { |
| 512 | /* |
| 513 | * This enables luns to be located sparsely. Otherwise, we may not |
| 514 | * discovered them. |
| 515 | */ |
| 516 | sdevice->sdev_bflags |= BLIST_SPARSELUN | BLIST_LARGELUN; |
| 517 | return 0; |
| 518 | } |
| 519 | |
K. Y. Srinivasan | a1ebfea | 2011-05-10 07:54:26 -0700 | [diff] [blame] | 520 | static int storvsc_merge_bvec(struct request_queue *q, |
| 521 | struct bvec_merge_data *bmd, struct bio_vec *bvec) |
| 522 | { |
| 523 | /* checking done by caller. */ |
| 524 | return bvec->bv_len; |
| 525 | } |
| 526 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 527 | static int storvsc_device_configure(struct scsi_device *sdevice) |
| 528 | { |
| 529 | scsi_adjust_queue_depth(sdevice, MSG_SIMPLE_TAG, |
| 530 | STORVSC_MAX_IO_REQUESTS); |
| 531 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 532 | blk_queue_max_segment_size(sdevice->request_queue, PAGE_SIZE); |
| 533 | |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 534 | blk_queue_merge_bvec(sdevice->request_queue, storvsc_merge_bvec); |
| 535 | |
| 536 | blk_queue_bounce_limit(sdevice->request_queue, BLK_BOUNCE_ANY); |
K. Y. Srinivasan | 419f2d0 | 2011-05-10 07:54:27 -0700 | [diff] [blame] | 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
K. Y. Srinivasan | 49a3c7a | 2011-05-10 07:54:28 -0700 | [diff] [blame] | 541 | static void destroy_bounce_buffer(struct scatterlist *sgl, |
| 542 | unsigned int sg_count) |
| 543 | { |
| 544 | int i; |
| 545 | struct page *page_buf; |
| 546 | |
| 547 | for (i = 0; i < sg_count; i++) { |
| 548 | page_buf = sg_page((&sgl[i])); |
| 549 | if (page_buf != NULL) |
| 550 | __free_page(page_buf); |
| 551 | } |
| 552 | |
| 553 | kfree(sgl); |
| 554 | } |
| 555 | |
K. Y. Srinivasan | 3862ef3 | 2011-05-10 07:54:29 -0700 | [diff] [blame] | 556 | static int do_bounce_buffer(struct scatterlist *sgl, unsigned int sg_count) |
| 557 | { |
| 558 | int i; |
| 559 | |
| 560 | /* No need to check */ |
| 561 | if (sg_count < 2) |
| 562 | return -1; |
| 563 | |
| 564 | /* We have at least 2 sg entries */ |
| 565 | for (i = 0; i < sg_count; i++) { |
| 566 | if (i == 0) { |
| 567 | /* make sure 1st one does not have hole */ |
| 568 | if (sgl[i].offset + sgl[i].length != PAGE_SIZE) |
| 569 | return i; |
| 570 | } else if (i == sg_count - 1) { |
| 571 | /* make sure last one does not have hole */ |
| 572 | if (sgl[i].offset != 0) |
| 573 | return i; |
| 574 | } else { |
| 575 | /* make sure no hole in the middle */ |
| 576 | if (sgl[i].length != PAGE_SIZE || sgl[i].offset != 0) |
| 577 | return i; |
| 578 | } |
| 579 | } |
| 580 | return -1; |
| 581 | } |
| 582 | |
K. Y. Srinivasan | a9753cb | 2011-05-10 07:54:30 -0700 | [diff] [blame] | 583 | static struct scatterlist *create_bounce_buffer(struct scatterlist *sgl, |
| 584 | unsigned int sg_count, |
| 585 | unsigned int len) |
| 586 | { |
| 587 | int i; |
| 588 | int num_pages; |
| 589 | struct scatterlist *bounce_sgl; |
| 590 | struct page *page_buf; |
| 591 | |
| 592 | num_pages = ALIGN(len, PAGE_SIZE) >> PAGE_SHIFT; |
| 593 | |
| 594 | bounce_sgl = kcalloc(num_pages, sizeof(struct scatterlist), GFP_ATOMIC); |
| 595 | if (!bounce_sgl) |
| 596 | return NULL; |
| 597 | |
| 598 | for (i = 0; i < num_pages; i++) { |
| 599 | page_buf = alloc_page(GFP_ATOMIC); |
| 600 | if (!page_buf) |
| 601 | goto cleanup; |
| 602 | sg_set_page(&bounce_sgl[i], page_buf, 0, 0); |
| 603 | } |
| 604 | |
| 605 | return bounce_sgl; |
| 606 | |
| 607 | cleanup: |
| 608 | destroy_bounce_buffer(bounce_sgl, num_pages); |
| 609 | return NULL; |
| 610 | } |
| 611 | |
K. Y. Srinivasan | 29fe2c9 | 2011-05-10 07:54:31 -0700 | [diff] [blame] | 612 | |
| 613 | /* Assume the original sgl has enough room */ |
| 614 | static unsigned int copy_from_bounce_buffer(struct scatterlist *orig_sgl, |
| 615 | struct scatterlist *bounce_sgl, |
| 616 | unsigned int orig_sgl_count) |
| 617 | { |
| 618 | int i; |
| 619 | int j = 0; |
| 620 | unsigned long src, dest; |
| 621 | unsigned int srclen, destlen, copylen; |
| 622 | unsigned int total_copied = 0; |
| 623 | unsigned long bounce_addr = 0; |
| 624 | unsigned long dest_addr = 0; |
| 625 | unsigned long flags; |
| 626 | |
| 627 | local_irq_save(flags); |
| 628 | |
| 629 | for (i = 0; i < orig_sgl_count; i++) { |
| 630 | dest_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 631 | KM_IRQ0) + orig_sgl[i].offset; |
| 632 | dest = dest_addr; |
| 633 | destlen = orig_sgl[i].length; |
| 634 | |
| 635 | if (bounce_addr == 0) |
| 636 | bounce_addr = |
| 637 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), |
| 638 | KM_IRQ0); |
| 639 | |
| 640 | while (destlen) { |
| 641 | src = bounce_addr + bounce_sgl[j].offset; |
| 642 | srclen = bounce_sgl[j].length - bounce_sgl[j].offset; |
| 643 | |
| 644 | copylen = min(srclen, destlen); |
| 645 | memcpy((void *)dest, (void *)src, copylen); |
| 646 | |
| 647 | total_copied += copylen; |
| 648 | bounce_sgl[j].offset += copylen; |
| 649 | destlen -= copylen; |
| 650 | dest += copylen; |
| 651 | |
| 652 | if (bounce_sgl[j].offset == bounce_sgl[j].length) { |
| 653 | /* full */ |
| 654 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 655 | j++; |
| 656 | |
| 657 | /* if we need to use another bounce buffer */ |
| 658 | if (destlen || i != orig_sgl_count - 1) |
| 659 | bounce_addr = |
| 660 | (unsigned long)kmap_atomic( |
| 661 | sg_page((&bounce_sgl[j])), KM_IRQ0); |
| 662 | } else if (destlen == 0 && i == orig_sgl_count - 1) { |
| 663 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 664 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | kunmap_atomic((void *)(dest_addr - orig_sgl[i].offset), |
| 669 | KM_IRQ0); |
| 670 | } |
| 671 | |
| 672 | local_irq_restore(flags); |
| 673 | |
| 674 | return total_copied; |
| 675 | } |
| 676 | |
K. Y. Srinivasan | 6a8ff44 | 2011-05-10 07:54:32 -0700 | [diff] [blame] | 677 | |
| 678 | /* Assume the bounce_sgl has enough room ie using the create_bounce_buffer() */ |
| 679 | static unsigned int copy_to_bounce_buffer(struct scatterlist *orig_sgl, |
| 680 | struct scatterlist *bounce_sgl, |
| 681 | unsigned int orig_sgl_count) |
| 682 | { |
| 683 | int i; |
| 684 | int j = 0; |
| 685 | unsigned long src, dest; |
| 686 | unsigned int srclen, destlen, copylen; |
| 687 | unsigned int total_copied = 0; |
| 688 | unsigned long bounce_addr = 0; |
| 689 | unsigned long src_addr = 0; |
| 690 | unsigned long flags; |
| 691 | |
| 692 | local_irq_save(flags); |
| 693 | |
| 694 | for (i = 0; i < orig_sgl_count; i++) { |
| 695 | src_addr = (unsigned long)kmap_atomic(sg_page((&orig_sgl[i])), |
| 696 | KM_IRQ0) + orig_sgl[i].offset; |
| 697 | src = src_addr; |
| 698 | srclen = orig_sgl[i].length; |
| 699 | |
| 700 | if (bounce_addr == 0) |
| 701 | bounce_addr = |
| 702 | (unsigned long)kmap_atomic(sg_page((&bounce_sgl[j])), |
| 703 | KM_IRQ0); |
| 704 | |
| 705 | while (srclen) { |
| 706 | /* assume bounce offset always == 0 */ |
| 707 | dest = bounce_addr + bounce_sgl[j].length; |
| 708 | destlen = PAGE_SIZE - bounce_sgl[j].length; |
| 709 | |
| 710 | copylen = min(srclen, destlen); |
| 711 | memcpy((void *)dest, (void *)src, copylen); |
| 712 | |
| 713 | total_copied += copylen; |
| 714 | bounce_sgl[j].length += copylen; |
| 715 | srclen -= copylen; |
| 716 | src += copylen; |
| 717 | |
| 718 | if (bounce_sgl[j].length == PAGE_SIZE) { |
| 719 | /* full..move to next entry */ |
| 720 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 721 | j++; |
| 722 | |
| 723 | /* if we need to use another bounce buffer */ |
| 724 | if (srclen || i != orig_sgl_count - 1) |
| 725 | bounce_addr = |
| 726 | (unsigned long)kmap_atomic( |
| 727 | sg_page((&bounce_sgl[j])), KM_IRQ0); |
| 728 | |
| 729 | } else if (srclen == 0 && i == orig_sgl_count - 1) { |
| 730 | /* unmap the last bounce that is < PAGE_SIZE */ |
| 731 | kunmap_atomic((void *)bounce_addr, KM_IRQ0); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | kunmap_atomic((void *)(src_addr - orig_sgl[i].offset), KM_IRQ0); |
| 736 | } |
| 737 | |
| 738 | local_irq_restore(flags); |
| 739 | |
| 740 | return total_copied; |
| 741 | } |
| 742 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 743 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 744 | static int storvsc_remove(struct hv_device *dev) |
| 745 | { |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 746 | struct Scsi_Host *host = dev_get_drvdata(&dev->device); |
| 747 | struct hv_host_device *host_dev = |
| 748 | (struct hv_host_device *)host->hostdata; |
| 749 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 750 | scsi_remove_host(host); |
| 751 | |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 752 | scsi_host_put(host); |
K. Y. Srinivasan | d36b0a0 | 2011-06-06 15:49:29 -0700 | [diff] [blame] | 753 | |
K. Y. Srinivasan | 2935a40 | 2011-06-06 15:49:28 -0700 | [diff] [blame] | 754 | storvsc_dev_remove(dev); |
| 755 | if (host_dev->request_pool) { |
| 756 | kmem_cache_destroy(host_dev->request_pool); |
| 757 | host_dev->request_pool = NULL; |
| 758 | } |
K. Y. Srinivasan | 5c5c023 | 2011-05-10 07:54:33 -0700 | [diff] [blame] | 759 | return 0; |
| 760 | } |
| 761 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 762 | |
| 763 | static int storvsc_get_chs(struct scsi_device *sdev, struct block_device * bdev, |
| 764 | sector_t capacity, int *info) |
| 765 | { |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 766 | sector_t nsect = capacity; |
| 767 | sector_t cylinders = nsect; |
| 768 | int heads, sectors_pt; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 769 | |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 770 | /* |
| 771 | * We are making up these values; let us keep it simple. |
| 772 | */ |
| 773 | heads = 0xff; |
| 774 | sectors_pt = 0x3f; /* Sectors per track */ |
| 775 | sector_div(cylinders, heads * sectors_pt); |
| 776 | if ((sector_t)(cylinders + 1) * heads * sectors_pt < nsect) |
| 777 | cylinders = 0xffff; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 778 | |
| 779 | info[0] = heads; |
K. Y. Srinivasan | 5326fd5 | 2011-05-10 07:54:52 -0700 | [diff] [blame] | 780 | info[1] = sectors_pt; |
| 781 | info[2] = (int)cylinders; |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 782 | |
K. Y. Srinivasan | 62838ce | 2011-05-10 07:54:34 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 785 | |
| 786 | static int storvsc_host_reset(struct hv_device *device) |
| 787 | { |
| 788 | struct storvsc_device *stor_device; |
| 789 | struct hv_storvsc_request *request; |
| 790 | struct vstor_packet *vstor_packet; |
| 791 | int ret, t; |
| 792 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 793 | |
K. Y. Srinivasan | 1eaaddf | 2011-08-27 11:31:03 -0700 | [diff] [blame] | 794 | stor_device = get_out_stor_device(device); |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 795 | if (!stor_device) |
K. Y. Srinivasan | f3b7416 | 2011-08-25 09:49:08 -0700 | [diff] [blame] | 796 | return -ENODEV; |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 797 | |
| 798 | request = &stor_device->reset_request; |
| 799 | vstor_packet = &request->vstor_packet; |
| 800 | |
| 801 | init_completion(&request->wait_event); |
| 802 | |
| 803 | vstor_packet->operation = VSTOR_OPERATION_RESET_BUS; |
| 804 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; |
| 805 | vstor_packet->vm_srb.path_id = stor_device->path_id; |
| 806 | |
| 807 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
| 808 | sizeof(struct vstor_packet), |
| 809 | (unsigned long)&stor_device->reset_request, |
| 810 | VM_PKT_DATA_INBAND, |
| 811 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
| 812 | if (ret != 0) |
| 813 | goto cleanup; |
| 814 | |
K. Y. Srinivasan | 46d2eb6 | 2011-06-16 13:16:36 -0700 | [diff] [blame] | 815 | t = wait_for_completion_timeout(&request->wait_event, 5*HZ); |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 816 | if (t == 0) { |
| 817 | ret = -ETIMEDOUT; |
| 818 | goto cleanup; |
| 819 | } |
| 820 | |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 821 | |
| 822 | /* |
| 823 | * At this point, all outstanding requests in the adapter |
| 824 | * should have been flushed out and return to us |
| 825 | */ |
| 826 | |
| 827 | cleanup: |
K. Y. Srinivasan | aa3d789 | 2011-05-10 07:54:37 -0700 | [diff] [blame] | 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 832 | /* |
| 833 | * storvsc_host_reset_handler - Reset the scsi HBA |
| 834 | */ |
| 835 | static int storvsc_host_reset_handler(struct scsi_cmnd *scmnd) |
| 836 | { |
| 837 | int ret; |
| 838 | struct hv_host_device *host_dev = |
| 839 | (struct hv_host_device *)scmnd->device->host->hostdata; |
| 840 | struct hv_device *dev = host_dev->dev; |
| 841 | |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 842 | ret = storvsc_host_reset(dev); |
| 843 | if (ret != 0) |
| 844 | return ret; |
| 845 | |
K. Y. Srinivasan | 96e690be | 2011-05-10 07:54:38 -0700 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 849 | |
| 850 | /* |
| 851 | * storvsc_commmand_completion - Command completion processing |
| 852 | */ |
| 853 | static void storvsc_commmand_completion(struct hv_storvsc_request *request) |
| 854 | { |
| 855 | struct storvsc_cmd_request *cmd_request = |
| 856 | (struct storvsc_cmd_request *)request->context; |
| 857 | struct scsi_cmnd *scmnd = cmd_request->cmd; |
| 858 | struct hv_host_device *host_dev = |
| 859 | (struct hv_host_device *)scmnd->device->host->hostdata; |
| 860 | void (*scsi_done_fn)(struct scsi_cmnd *); |
| 861 | struct scsi_sense_hdr sense_hdr; |
| 862 | struct vmscsi_request *vm_srb; |
| 863 | |
K. Y. Srinivasan | 3612ef9 | 2011-08-27 11:31:20 -0700 | [diff] [blame] | 864 | vm_srb = &request->vstor_packet.vm_srb; |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 865 | if (cmd_request->bounce_sgl_count) { |
K. Y. Srinivasan | 3612ef9 | 2011-08-27 11:31:20 -0700 | [diff] [blame] | 866 | if (vm_srb->data_in == READ_TYPE) { |
| 867 | copy_from_bounce_buffer(scsi_sglist(scmnd), |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 868 | cmd_request->bounce_sgl, |
| 869 | scsi_sg_count(scmnd)); |
K. Y. Srinivasan | 3612ef9 | 2011-08-27 11:31:20 -0700 | [diff] [blame] | 870 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
| 871 | cmd_request->bounce_sgl_count); |
| 872 | } |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 873 | } |
| 874 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 875 | scmnd->result = vm_srb->scsi_status; |
| 876 | |
| 877 | if (scmnd->result) { |
| 878 | if (scsi_normalize_sense(scmnd->sense_buffer, |
| 879 | SCSI_SENSE_BUFFERSIZE, &sense_hdr)) |
| 880 | scsi_print_sense_hdr("storvsc", &sense_hdr); |
| 881 | } |
| 882 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 883 | scsi_set_resid(scmnd, |
| 884 | request->data_buffer.len - |
| 885 | vm_srb->data_transfer_length); |
| 886 | |
| 887 | scsi_done_fn = scmnd->scsi_done; |
| 888 | |
| 889 | scmnd->host_scribble = NULL; |
| 890 | scmnd->scsi_done = NULL; |
| 891 | |
K. Y. Srinivasan | 8a411ba | 2011-05-10 07:54:41 -0700 | [diff] [blame] | 892 | scsi_done_fn(scmnd); |
| 893 | |
| 894 | kmem_cache_free(host_dev->request_pool, cmd_request); |
| 895 | } |
| 896 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 897 | |
| 898 | /* |
| 899 | * storvsc_queuecommand - Initiate command processing |
| 900 | */ |
| 901 | static int storvsc_queuecommand_lck(struct scsi_cmnd *scmnd, |
| 902 | void (*done)(struct scsi_cmnd *)) |
| 903 | { |
| 904 | int ret; |
| 905 | struct hv_host_device *host_dev = |
| 906 | (struct hv_host_device *)scmnd->device->host->hostdata; |
| 907 | struct hv_device *dev = host_dev->dev; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 908 | struct hv_storvsc_request *request; |
| 909 | struct storvsc_cmd_request *cmd_request; |
| 910 | unsigned int request_size = 0; |
| 911 | int i; |
| 912 | struct scatterlist *sgl; |
| 913 | unsigned int sg_count = 0; |
| 914 | struct vmscsi_request *vm_srb; |
| 915 | |
| 916 | |
| 917 | /* If retrying, no need to prep the cmd */ |
| 918 | if (scmnd->host_scribble) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 919 | |
| 920 | cmd_request = |
| 921 | (struct storvsc_cmd_request *)scmnd->host_scribble; |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 922 | |
| 923 | goto retry_request; |
| 924 | } |
| 925 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 926 | scmnd->scsi_done = done; |
| 927 | |
| 928 | request_size = sizeof(struct storvsc_cmd_request); |
| 929 | |
| 930 | cmd_request = kmem_cache_zalloc(host_dev->request_pool, |
| 931 | GFP_ATOMIC); |
| 932 | if (!cmd_request) { |
| 933 | scmnd->scsi_done = NULL; |
| 934 | return SCSI_MLQUEUE_DEVICE_BUSY; |
| 935 | } |
| 936 | |
| 937 | /* Setup the cmd request */ |
| 938 | cmd_request->bounce_sgl_count = 0; |
| 939 | cmd_request->bounce_sgl = NULL; |
| 940 | cmd_request->cmd = scmnd; |
| 941 | |
| 942 | scmnd->host_scribble = (unsigned char *)cmd_request; |
| 943 | |
| 944 | request = &cmd_request->request; |
| 945 | vm_srb = &request->vstor_packet.vm_srb; |
| 946 | |
| 947 | |
| 948 | /* Build the SRB */ |
| 949 | switch (scmnd->sc_data_direction) { |
| 950 | case DMA_TO_DEVICE: |
| 951 | vm_srb->data_in = WRITE_TYPE; |
| 952 | break; |
| 953 | case DMA_FROM_DEVICE: |
| 954 | vm_srb->data_in = READ_TYPE; |
| 955 | break; |
| 956 | default: |
| 957 | vm_srb->data_in = UNKNOWN_TYPE; |
| 958 | break; |
| 959 | } |
| 960 | |
| 961 | request->on_io_completion = storvsc_commmand_completion; |
| 962 | request->context = cmd_request;/* scmnd; */ |
| 963 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 964 | vm_srb->port_number = host_dev->port; |
| 965 | vm_srb->path_id = scmnd->device->channel; |
| 966 | vm_srb->target_id = scmnd->device->id; |
| 967 | vm_srb->lun = scmnd->device->lun; |
| 968 | |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 969 | vm_srb->cdb_length = scmnd->cmd_len; |
| 970 | |
| 971 | memcpy(vm_srb->cdb, scmnd->cmnd, vm_srb->cdb_length); |
| 972 | |
| 973 | request->sense_buffer = scmnd->sense_buffer; |
| 974 | |
| 975 | |
| 976 | request->data_buffer.len = scsi_bufflen(scmnd); |
| 977 | if (scsi_sg_count(scmnd)) { |
| 978 | sgl = (struct scatterlist *)scsi_sglist(scmnd); |
| 979 | sg_count = scsi_sg_count(scmnd); |
| 980 | |
| 981 | /* check if we need to bounce the sgl */ |
| 982 | if (do_bounce_buffer(sgl, scsi_sg_count(scmnd)) != -1) { |
| 983 | cmd_request->bounce_sgl = |
| 984 | create_bounce_buffer(sgl, scsi_sg_count(scmnd), |
| 985 | scsi_bufflen(scmnd)); |
| 986 | if (!cmd_request->bounce_sgl) { |
| 987 | scmnd->scsi_done = NULL; |
| 988 | scmnd->host_scribble = NULL; |
| 989 | kmem_cache_free(host_dev->request_pool, |
| 990 | cmd_request); |
| 991 | |
| 992 | return SCSI_MLQUEUE_HOST_BUSY; |
| 993 | } |
| 994 | |
| 995 | cmd_request->bounce_sgl_count = |
| 996 | ALIGN(scsi_bufflen(scmnd), PAGE_SIZE) >> |
| 997 | PAGE_SHIFT; |
| 998 | |
K. Y. Srinivasan | fa23b8c | 2011-08-27 11:31:21 -0700 | [diff] [blame] | 999 | if (vm_srb->data_in == WRITE_TYPE) |
| 1000 | copy_to_bounce_buffer(sgl, |
| 1001 | cmd_request->bounce_sgl, |
| 1002 | scsi_sg_count(scmnd)); |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1003 | |
| 1004 | sgl = cmd_request->bounce_sgl; |
| 1005 | sg_count = cmd_request->bounce_sgl_count; |
| 1006 | } |
| 1007 | |
| 1008 | request->data_buffer.offset = sgl[0].offset; |
| 1009 | |
| 1010 | for (i = 0; i < sg_count; i++) |
| 1011 | request->data_buffer.pfn_array[i] = |
| 1012 | page_to_pfn(sg_page((&sgl[i]))); |
| 1013 | |
| 1014 | } else if (scsi_sglist(scmnd)) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1015 | request->data_buffer.offset = |
| 1016 | virt_to_phys(scsi_sglist(scmnd)) & (PAGE_SIZE-1); |
| 1017 | request->data_buffer.pfn_array[0] = |
| 1018 | virt_to_phys(scsi_sglist(scmnd)) >> PAGE_SHIFT; |
| 1019 | } |
| 1020 | |
| 1021 | retry_request: |
| 1022 | /* Invokes the vsc to start an IO */ |
K. Y. Srinivasan | 636f0fd | 2011-05-10 07:54:50 -0700 | [diff] [blame] | 1023 | ret = storvsc_do_io(dev, &cmd_request->request); |
| 1024 | |
K. Y. Srinivasan | d2598f0 | 2011-08-25 09:48:58 -0700 | [diff] [blame] | 1025 | if (ret == -EAGAIN) { |
K. Y. Srinivasan | c5b463a | 2011-05-10 07:54:42 -0700 | [diff] [blame] | 1026 | /* no more space */ |
| 1027 | |
| 1028 | if (cmd_request->bounce_sgl_count) { |
| 1029 | /* |
| 1030 | * FIXME: We can optimize on writes by just skipping |
| 1031 | * this |
| 1032 | */ |
| 1033 | copy_from_bounce_buffer(scsi_sglist(scmnd), |
| 1034 | cmd_request->bounce_sgl, |
| 1035 | scsi_sg_count(scmnd)); |
| 1036 | destroy_bounce_buffer(cmd_request->bounce_sgl, |
| 1037 | cmd_request->bounce_sgl_count); |
| 1038 | } |
| 1039 | |
| 1040 | kmem_cache_free(host_dev->request_pool, cmd_request); |
| 1041 | |
| 1042 | scmnd->scsi_done = NULL; |
| 1043 | scmnd->host_scribble = NULL; |
| 1044 | |
| 1045 | ret = SCSI_MLQUEUE_DEVICE_BUSY; |
| 1046 | } |
| 1047 | |
| 1048 | return ret; |
| 1049 | } |
| 1050 | |
| 1051 | static DEF_SCSI_QCMD(storvsc_queuecommand) |
| 1052 | |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1053 | |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1054 | /* Scsi driver */ |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1055 | static struct scsi_host_template scsi_driver = { |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1056 | .module = THIS_MODULE, |
| 1057 | .name = "storvsc_host_t", |
| 1058 | .bios_param = storvsc_get_chs, |
| 1059 | .queuecommand = storvsc_queuecommand, |
| 1060 | .eh_host_reset_handler = storvsc_host_reset_handler, |
| 1061 | .slave_alloc = storvsc_device_alloc, |
| 1062 | .slave_configure = storvsc_device_configure, |
| 1063 | .cmd_per_lun = 1, |
| 1064 | /* 64 max_queue * 1 target */ |
Lars Lindley | 0686e4f | 2010-03-11 23:51:23 +0100 | [diff] [blame] | 1065 | .can_queue = STORVSC_MAX_IO_REQUESTS*STORVSC_MAX_TARGETS, |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1066 | .this_id = -1, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1067 | /* 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] | 1068 | /* currently 32 */ |
| 1069 | .sg_tablesize = MAX_MULTIPAGE_BUFFER_COUNT, |
| 1070 | /* |
| 1071 | * ENABLE_CLUSTERING allows mutiple physically contig bio_vecs to merge |
| 1072 | * into 1 sg element. If set, we must limit the max_segment_size to |
| 1073 | * PAGE_SIZE, otherwise we may get 1 sg element that represents |
| 1074 | * multiple |
| 1075 | */ |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1076 | /* physically contig pfns (ie sg[x].length > PAGE_SIZE). */ |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1077 | .use_clustering = ENABLE_CLUSTERING, |
Bill Pemberton | 454f18a | 2009-07-27 16:47:24 -0400 | [diff] [blame] | 1078 | /* 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] | 1079 | .dma_boundary = PAGE_SIZE-1, |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1080 | }; |
| 1081 | |
K. Y. Srinivasan | 21e3774 | 2011-08-27 11:31:18 -0700 | [diff] [blame] | 1082 | /* |
| 1083 | * The storvsc_probe function assumes that the IDE guid |
| 1084 | * is the second entry. |
| 1085 | */ |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1086 | static const struct hv_vmbus_device_id id_table[] = { |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1087 | /* SCSI guid */ |
| 1088 | { VMBUS_DEVICE(0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, |
| 1089 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f) }, |
K. Y. Srinivasan | 21e3774 | 2011-08-27 11:31:18 -0700 | [diff] [blame] | 1090 | /* IDE guid */ |
| 1091 | { VMBUS_DEVICE(0x32, 0x26, 0x41, 0x32, 0xcb, 0x86, 0xa2, 0x44, |
| 1092 | 0x9b, 0x5c, 0x50, 0xd1, 0x41, 0x73, 0x54, 0xf5) }, |
Greg Kroah-Hartman | c45cf2d | 2011-08-25 11:41:33 -0700 | [diff] [blame] | 1093 | { }, |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1094 | }; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1095 | |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1096 | MODULE_DEVICE_TABLE(vmbus, id_table); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1097 | |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1098 | |
Hank Janssen | 3e18951 | 2010-03-04 22:11:00 +0000 | [diff] [blame] | 1099 | /* |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1100 | * storvsc_probe - Add a new device for this driver |
| 1101 | */ |
| 1102 | |
| 1103 | static int storvsc_probe(struct hv_device *device) |
| 1104 | { |
| 1105 | int ret; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1106 | struct Scsi_Host *host; |
| 1107 | struct hv_host_device *host_dev; |
| 1108 | struct storvsc_device_info device_info; |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1109 | bool dev_is_ide; |
| 1110 | int path = 0; |
| 1111 | int target = 0; |
| 1112 | |
K. Y. Srinivasan | 21e3774 | 2011-08-27 11:31:18 -0700 | [diff] [blame] | 1113 | if (!memcmp(&device->dev_type.b, id_table[1].guid, sizeof(uuid_le))) |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1114 | dev_is_ide = true; |
| 1115 | else |
| 1116 | dev_is_ide = false; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1117 | |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1118 | host = scsi_host_alloc(&scsi_driver, |
| 1119 | sizeof(struct hv_host_device)); |
| 1120 | if (!host) |
| 1121 | return -ENOMEM; |
| 1122 | |
| 1123 | dev_set_drvdata(&device->device, host); |
| 1124 | |
| 1125 | host_dev = (struct hv_host_device *)host->hostdata; |
| 1126 | memset(host_dev, 0, sizeof(struct hv_host_device)); |
| 1127 | |
| 1128 | host_dev->port = host->host_no; |
| 1129 | host_dev->dev = device; |
| 1130 | |
| 1131 | host_dev->request_pool = |
| 1132 | kmem_cache_create(dev_name(&device->device), |
| 1133 | sizeof(struct storvsc_cmd_request), 0, |
| 1134 | SLAB_HWCACHE_ALIGN, NULL); |
| 1135 | |
| 1136 | if (!host_dev->request_pool) { |
| 1137 | scsi_host_put(host); |
| 1138 | return -ENOMEM; |
| 1139 | } |
| 1140 | |
| 1141 | device_info.port_number = host->host_no; |
K. Y. Srinivasan | fa4d123 | 2011-05-10 07:56:01 -0700 | [diff] [blame] | 1142 | device_info.ring_buffer_size = storvsc_ringbuffer_size; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1143 | /* Call to the vsc driver to add the device */ |
K. Y. Srinivasan | 58f1f5c | 2011-05-10 07:54:49 -0700 | [diff] [blame] | 1144 | ret = storvsc_dev_add(device, (void *)&device_info); |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1145 | |
| 1146 | if (ret != 0) { |
| 1147 | kmem_cache_destroy(host_dev->request_pool); |
| 1148 | scsi_host_put(host); |
K. Y. Srinivasan | 8e85468 | 2011-08-25 09:49:09 -0700 | [diff] [blame] | 1149 | return -ENODEV; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1152 | if (dev_is_ide) |
| 1153 | storvsc_get_ide_info(device, &target, &path); |
| 1154 | |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1155 | host_dev->path = device_info.path_id; |
| 1156 | host_dev->target = device_info.target_id; |
| 1157 | |
| 1158 | /* max # of devices per target */ |
| 1159 | host->max_lun = STORVSC_MAX_LUNS_PER_TARGET; |
| 1160 | /* max # of targets per channel */ |
| 1161 | host->max_id = STORVSC_MAX_TARGETS; |
| 1162 | /* max # of channels */ |
| 1163 | host->max_channel = STORVSC_MAX_CHANNELS - 1; |
| 1164 | |
| 1165 | /* Register the HBA and start the scsi bus scan */ |
| 1166 | ret = scsi_add_host(host, &device->device); |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1167 | if (ret != 0) |
| 1168 | goto err_out; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1169 | |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1170 | if (!dev_is_ide) { |
| 1171 | scsi_scan_host(host); |
| 1172 | return 0; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1173 | } |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1174 | ret = scsi_add_device(host, 0, target, 0); |
| 1175 | if (ret) { |
| 1176 | scsi_remove_host(host); |
| 1177 | goto err_out; |
| 1178 | } |
| 1179 | return 0; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1180 | |
K. Y. Srinivasan | bd1f5d6 | 2011-08-27 11:31:17 -0700 | [diff] [blame] | 1181 | err_out: |
| 1182 | storvsc_dev_remove(device); |
| 1183 | kmem_cache_destroy(host_dev->request_pool); |
| 1184 | scsi_host_put(host); |
| 1185 | return -ENODEV; |
K. Y. Srinivasan | f5c7887 | 2011-05-10 07:54:43 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
K. Y. Srinivasan | 7bd05b9 | 2011-05-10 07:54:45 -0700 | [diff] [blame] | 1188 | /* The one and only one */ |
| 1189 | |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 1190 | static struct hv_driver storvsc_drv = { |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1191 | .name = "storvsc", |
K. Y. Srinivasan | d847b5f | 2011-08-25 09:48:33 -0700 | [diff] [blame] | 1192 | .id_table = id_table, |
K. Y. Srinivasan | 40bf63e | 2011-05-10 07:56:09 -0700 | [diff] [blame] | 1193 | .probe = storvsc_probe, |
| 1194 | .remove = storvsc_remove, |
K. Y. Srinivasan | 39ae6fa | 2011-05-10 07:54:46 -0700 | [diff] [blame] | 1195 | }; |
K. Y. Srinivasan | 7bd05b9 | 2011-05-10 07:54:45 -0700 | [diff] [blame] | 1196 | |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 1197 | static int __init storvsc_drv_init(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1198 | { |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 1199 | u32 max_outstanding_req_per_channel; |
| 1200 | |
| 1201 | /* |
| 1202 | * Divide the ring buffer data size (which is 1 page less |
| 1203 | * than the ring buffer size since that page is reserved for |
| 1204 | * the ring buffer indices) by the max request size (which is |
| 1205 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) |
| 1206 | */ |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 1207 | max_outstanding_req_per_channel = |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1208 | ((storvsc_ringbuffer_size - PAGE_SIZE) / |
| 1209 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + |
| 1210 | sizeof(struct vstor_packet) + sizeof(u64), |
| 1211 | sizeof(u64))); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1212 | |
K. Y. Srinivasan | 01415ab3 | 2011-05-10 07:55:58 -0700 | [diff] [blame] | 1213 | if (max_outstanding_req_per_channel < |
K. Y. Srinivasan | f8feed0 | 2011-05-10 07:54:24 -0700 | [diff] [blame] | 1214 | STORVSC_MAX_IO_REQUESTS) |
K. Y. Srinivasan | b06efc1 | 2011-08-25 09:49:10 -0700 | [diff] [blame] | 1215 | return -EINVAL; |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1216 | |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1217 | return vmbus_driver_register(&storvsc_drv); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 1220 | static void __exit storvsc_drv_exit(void) |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1221 | { |
Greg Kroah-Hartman | 768fa21 | 2011-08-25 15:07:32 -0700 | [diff] [blame] | 1222 | vmbus_driver_unregister(&storvsc_drv); |
Hank Janssen | bef4a34 | 2009-07-13 16:01:31 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Greg Kroah-Hartman | ff568d3 | 2009-09-02 08:37:47 -0700 | [diff] [blame] | 1225 | MODULE_LICENSE("GPL"); |
Hank Janssen | 26c14cc | 2010-02-11 23:02:42 +0000 | [diff] [blame] | 1226 | MODULE_VERSION(HV_DRV_VERSION); |
Stephen Hemminger | 3afc7cc3 | 2010-05-06 21:44:45 -0700 | [diff] [blame] | 1227 | MODULE_DESCRIPTION("Microsoft Hyper-V virtual storage driver"); |
K. Y. Srinivasan | d9bbae8 | 2011-06-06 15:49:27 -0700 | [diff] [blame] | 1228 | module_init(storvsc_drv_init); |
K. Y. Srinivasan | c63ba9e | 2011-06-06 15:49:26 -0700 | [diff] [blame] | 1229 | module_exit(storvsc_drv_exit); |