]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/hv/storvsc.c
Staging: hv: Introduce a function to wait to drain outgoing I/O
[mirror_ubuntu-artful-kernel.git] / drivers / staging / hv / storvsc.c
CommitLineData
bef4a34a 1/*
bef4a34a
HJ
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>
1f91bca8
S
20 *
21 * 4/3/2011: K. Y. Srinivasan - Significant restructuring and cleanup.
bef4a34a 22 */
5654e932 23#include <linux/kernel.h>
0c3b7b2f 24#include <linux/sched.h>
93958465 25#include <linux/completion.h>
0ffa63b0 26#include <linux/string.h>
5a0e3ad6 27#include <linux/slab.h>
0ffa63b0 28#include <linux/mm.h>
b4362c9c 29#include <linux/delay.h>
e3fe0bb6 30#include "hv_api.h"
645954c5 31#include "logging.h"
bb969793 32#include "storvsc_api.h"
f8e5add2 33#include "vmbus_packet_format.h"
2dd88b51 34#include "vstorage.h"
50ea95df 35#include "channel.h"
bef4a34a
HJ
36
37
f638859e 38static inline struct storvsc_device *alloc_stor_device(struct hv_device *device)
bef4a34a 39{
f638859e 40 struct storvsc_device *stor_device;
bef4a34a 41
f638859e
HJ
42 stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL);
43 if (!stor_device)
bef4a34a
HJ
44 return NULL;
45
454f18a9 46 /* Set to 2 to allow both inbound and outbound traffics */
3d8cdf22 47 /* (ie get_stor_device() and must_get_stor_device()) to proceed. */
f638859e 48 atomic_cmpxchg(&stor_device->ref_count, 0, 2);
bef4a34a 49
604df37d 50 init_waitqueue_head(&stor_device->waiting_to_drain);
f638859e 51 stor_device->device = device;
ca623ad3 52 device->ext = stor_device;
bef4a34a 53
f638859e 54 return stor_device;
bef4a34a
HJ
55}
56
f638859e 57static inline void free_stor_device(struct storvsc_device *device)
bef4a34a 58{
f638859e 59 kfree(device);
bef4a34a
HJ
60}
61
454f18a9 62/* Get the stordevice object iff exists and its refcount > 0 */
02e37db7 63static inline struct storvsc_device *must_get_stor_device(
f638859e 64 struct hv_device *device)
bef4a34a 65{
f638859e 66 struct storvsc_device *stor_device;
bef4a34a 67
ca623ad3 68 stor_device = (struct storvsc_device *)device->ext;
f638859e
HJ
69 if (stor_device && atomic_read(&stor_device->ref_count))
70 atomic_inc(&stor_device->ref_count);
bef4a34a 71 else
f638859e 72 stor_device = NULL;
bef4a34a 73
f638859e 74 return stor_device;
bef4a34a
HJ
75}
76
02e37db7
HJ
77/* Drop ref count to 1 to effectively disable get_stor_device() */
78static inline struct storvsc_device *release_stor_device(
f638859e 79 struct hv_device *device)
bef4a34a 80{
f638859e 81 struct storvsc_device *stor_device;
bef4a34a 82
ca623ad3 83 stor_device = (struct storvsc_device *)device->ext;
bef4a34a 84
454f18a9 85 /* Busy wait until the ref drop to 2, then set it to 1 */
f638859e 86 while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2)
b4362c9c 87 udelay(100);
bef4a34a 88
f638859e 89 return stor_device;
bef4a34a
HJ
90}
91
f638859e 92/* Drop ref count to 0. No one can use stor_device object. */
02e37db7 93static inline struct storvsc_device *final_release_stor_device(
f638859e 94 struct hv_device *device)
bef4a34a 95{
f638859e 96 struct storvsc_device *stor_device;
bef4a34a 97
ca623ad3 98 stor_device = (struct storvsc_device *)device->ext;
bef4a34a 99
454f18a9 100 /* Busy wait until the ref drop to 1, then set it to 0 */
f638859e 101 while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1)
b4362c9c 102 udelay(100);
bef4a34a 103
ca623ad3 104 device->ext = NULL;
f638859e 105 return stor_device;
bef4a34a
HJ
106}
107
f638859e 108static int stor_vsc_channel_init(struct hv_device *device)
bef4a34a 109{
f638859e 110 struct storvsc_device *stor_device;
d7a1bdb9 111 struct hv_storvsc_request *request;
f638859e 112 struct vstor_packet *vstor_packet;
93958465 113 int ret, t;
bef4a34a 114
f638859e
HJ
115 stor_device = get_stor_device(device);
116 if (!stor_device) {
068c5df2
GKH
117 DPRINT_ERR(STORVSC, "unable to get stor device..."
118 "device being destroyed?");
bef4a34a
HJ
119 return -1;
120 }
121
f638859e
HJ
122 request = &stor_device->init_request;
123 vstor_packet = &request->vstor_packet;
bef4a34a 124
068c5df2
GKH
125 /*
126 * Now, initiate the vsc/vsp initialization protocol on the open
127 * channel
128 */
d7a1bdb9 129 memset(request, 0, sizeof(struct hv_storvsc_request));
93958465 130 init_completion(&request->wait_event);
f638859e
HJ
131 vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION;
132 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
bef4a34a 133
bef4a34a
HJ
134 DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION...");
135
f638859e 136 ret = vmbus_sendpacket(device->channel, vstor_packet,
b60d71e2
GKH
137 sizeof(struct vstor_packet),
138 (unsigned long)request,
415f2287 139 VM_PKT_DATA_INBAND,
b60d71e2 140 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
068c5df2
GKH
141 if (ret != 0) {
142 DPRINT_ERR(STORVSC,
143 "unable to send BEGIN_INITIALIZATION_OPERATION");
0c3b7b2f
S
144 goto cleanup;
145 }
146
93958465
S
147 t = wait_for_completion_timeout(&request->wait_event, HZ);
148 if (t == 0) {
0c3b7b2f
S
149 ret = -ETIMEDOUT;
150 goto cleanup;
bef4a34a
HJ
151 }
152
f638859e
HJ
153 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
154 vstor_packet->status != 0) {
068c5df2
GKH
155 DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed "
156 "(op %d status 0x%x)",
f638859e 157 vstor_packet->operation, vstor_packet->status);
0c3b7b2f 158 goto cleanup;
bef4a34a
HJ
159 }
160
161 DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION...");
162
454f18a9 163 /* reuse the packet for version range supported */
f638859e
HJ
164 memset(vstor_packet, 0, sizeof(struct vstor_packet));
165 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION;
166 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
bef4a34a 167
f638859e
HJ
168 vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT;
169 FILL_VMSTOR_REVISION(vstor_packet->version.revision);
bef4a34a 170
f638859e 171 ret = vmbus_sendpacket(device->channel, vstor_packet,
b60d71e2
GKH
172 sizeof(struct vstor_packet),
173 (unsigned long)request,
415f2287 174 VM_PKT_DATA_INBAND,
b60d71e2 175 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
068c5df2
GKH
176 if (ret != 0) {
177 DPRINT_ERR(STORVSC,
178 "unable to send BEGIN_INITIALIZATION_OPERATION");
0c3b7b2f 179 goto cleanup;
bef4a34a
HJ
180 }
181
93958465
S
182 t = wait_for_completion_timeout(&request->wait_event, HZ);
183 if (t == 0) {
0c3b7b2f
S
184 ret = -ETIMEDOUT;
185 goto cleanup;
186 }
bef4a34a 187
454f18a9 188 /* TODO: Check returned version */
f638859e
HJ
189 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
190 vstor_packet->status != 0) {
068c5df2
GKH
191 DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed "
192 "(op %d status 0x%x)",
f638859e 193 vstor_packet->operation, vstor_packet->status);
0c3b7b2f 194 goto cleanup;
bef4a34a
HJ
195 }
196
454f18a9 197 /* Query channel properties */
bef4a34a
HJ
198 DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION...");
199
f638859e
HJ
200 memset(vstor_packet, 0, sizeof(struct vstor_packet));
201 vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES;
202 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
203 vstor_packet->storage_channel_properties.port_number =
204 stor_device->port_number;
bef4a34a 205
f638859e 206 ret = vmbus_sendpacket(device->channel, vstor_packet,
b60d71e2
GKH
207 sizeof(struct vstor_packet),
208 (unsigned long)request,
415f2287 209 VM_PKT_DATA_INBAND,
b60d71e2 210 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
068c5df2
GKH
211
212 if (ret != 0) {
213 DPRINT_ERR(STORVSC,
214 "unable to send QUERY_PROPERTIES_OPERATION");
0c3b7b2f 215 goto cleanup;
bef4a34a
HJ
216 }
217
93958465
S
218 t = wait_for_completion_timeout(&request->wait_event, HZ);
219 if (t == 0) {
0c3b7b2f
S
220 ret = -ETIMEDOUT;
221 goto cleanup;
222 }
bef4a34a 223
454f18a9 224 /* TODO: Check returned version */
f638859e
HJ
225 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
226 vstor_packet->status != 0) {
068c5df2
GKH
227 DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed "
228 "(op %d status 0x%x)",
f638859e 229 vstor_packet->operation, vstor_packet->status);
0c3b7b2f 230 goto cleanup;
bef4a34a
HJ
231 }
232
f638859e
HJ
233 stor_device->path_id = vstor_packet->storage_channel_properties.path_id;
234 stor_device->target_id
235 = vstor_packet->storage_channel_properties.target_id;
bef4a34a 236
068c5df2 237 DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x",
f638859e
HJ
238 vstor_packet->storage_channel_properties.flags,
239 vstor_packet->storage_channel_properties.max_transfer_bytes);
bef4a34a
HJ
240
241 DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION...");
242
f638859e
HJ
243 memset(vstor_packet, 0, sizeof(struct vstor_packet));
244 vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION;
245 vstor_packet->flags = REQUEST_COMPLETION_FLAG;
bef4a34a 246
f638859e 247 ret = vmbus_sendpacket(device->channel, vstor_packet,
b60d71e2
GKH
248 sizeof(struct vstor_packet),
249 (unsigned long)request,
415f2287 250 VM_PKT_DATA_INBAND,
b60d71e2 251 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
068c5df2
GKH
252
253 if (ret != 0) {
254 DPRINT_ERR(STORVSC,
255 "unable to send END_INITIALIZATION_OPERATION");
0c3b7b2f 256 goto cleanup;
bef4a34a
HJ
257 }
258
93958465
S
259 t = wait_for_completion_timeout(&request->wait_event, HZ);
260 if (t == 0) {
0c3b7b2f
S
261 ret = -ETIMEDOUT;
262 goto cleanup;
263 }
bef4a34a 264
f638859e
HJ
265 if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO ||
266 vstor_packet->status != 0) {
068c5df2
GKH
267 DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed "
268 "(op %d status 0x%x)",
f638859e 269 vstor_packet->operation, vstor_packet->status);
0c3b7b2f 270 goto cleanup;
bef4a34a
HJ
271 }
272
273 DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****");
274
0c3b7b2f 275cleanup:
f638859e 276 put_stor_device(device);
bef4a34a
HJ
277 return ret;
278}
279
f638859e
HJ
280static void stor_vsc_on_io_completion(struct hv_device *device,
281 struct vstor_packet *vstor_packet,
d7a1bdb9 282 struct hv_storvsc_request *request)
163680b4 283{
f638859e 284 struct storvsc_device *stor_device;
a617e395 285 struct vstor_packet *stor_pkt;
163680b4 286
f638859e
HJ
287 stor_device = must_get_stor_device(device);
288 if (!stor_device) {
163680b4
GKH
289 DPRINT_ERR(STORVSC, "unable to get stor device..."
290 "device being destroyed?");
163680b4
GKH
291 return;
292 }
293
a617e395 294 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request %p "
d7a1bdb9 295 "completed bytes xfer %u", request,
f638859e 296 vstor_packet->vm_srb.data_transfer_length);
163680b4 297
a617e395 298 stor_pkt = &request->vstor_packet;
163680b4 299
163680b4
GKH
300
301 /* Copy over the status...etc */
a617e395
S
302 stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status;
303 stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status;
304 stor_pkt->vm_srb.sense_info_length =
305 vstor_packet->vm_srb.sense_info_length;
163680b4 306
6dc3f0a7
S
307 if (vstor_packet->vm_srb.scsi_status != 0 ||
308 vstor_packet->vm_srb.srb_status != 1) {
163680b4
GKH
309 DPRINT_WARN(STORVSC,
310 "cmd 0x%x scsi status 0x%x srb status 0x%x\n",
a617e395 311 stor_pkt->vm_srb.cdb[0],
373dd8a9 312 vstor_packet->vm_srb.scsi_status,
f638859e 313 vstor_packet->vm_srb.srb_status);
163680b4
GKH
314 }
315
6dc3f0a7 316 if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) {
163680b4 317 /* CHECK_CONDITION */
f638859e 318 if (vstor_packet->vm_srb.srb_status & 0x80) {
163680b4
GKH
319 /* autosense data available */
320 DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data "
d7a1bdb9 321 "valid - len %d\n", request,
f638859e 322 vstor_packet->vm_srb.sense_info_length);
163680b4 323
d7a1bdb9 324 memcpy(request->sense_buffer,
f638859e
HJ
325 vstor_packet->vm_srb.sense_data,
326 vstor_packet->vm_srb.sense_info_length);
163680b4 327
163680b4
GKH
328 }
329 }
330
a617e395
S
331 stor_pkt->vm_srb.data_transfer_length =
332 vstor_packet->vm_srb.data_transfer_length;
163680b4 333
d7a1bdb9 334 request->on_io_completion(request);
163680b4 335
604df37d
S
336 if (atomic_dec_and_test(&stor_device->num_outstanding_req) &&
337 stor_device->drain_notify)
338 wake_up(&stor_device->waiting_to_drain);
339
163680b4 340
f638859e 341 put_stor_device(device);
163680b4
GKH
342}
343
f638859e
HJ
344static void stor_vsc_on_receive(struct hv_device *device,
345 struct vstor_packet *vstor_packet,
d7a1bdb9 346 struct hv_storvsc_request *request)
163680b4 347{
f638859e 348 switch (vstor_packet->operation) {
d2aaba45 349 case VSTOR_OPERATION_COMPLETE_IO:
163680b4 350 DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION");
d7a1bdb9 351 stor_vsc_on_io_completion(device, vstor_packet, request);
163680b4 352 break;
d2aaba45 353 case VSTOR_OPERATION_REMOVE_DEVICE:
163680b4
GKH
354 DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION");
355 /* TODO: */
356 break;
357
358 default:
359 DPRINT_INFO(STORVSC, "Unknown operation received - %d",
f638859e 360 vstor_packet->operation);
163680b4
GKH
361 break;
362 }
363}
364
02e37db7 365static void stor_vsc_on_channel_callback(void *context)
163680b4
GKH
366{
367 struct hv_device *device = (struct hv_device *)context;
f638859e
HJ
368 struct storvsc_device *stor_device;
369 u32 bytes_recvd;
370 u64 request_id;
73509681 371 unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)];
d7a1bdb9 372 struct hv_storvsc_request *request;
163680b4
GKH
373 int ret;
374
163680b4 375
f638859e
HJ
376 stor_device = must_get_stor_device(device);
377 if (!stor_device) {
163680b4
GKH
378 DPRINT_ERR(STORVSC, "unable to get stor device..."
379 "device being destroyed?");
163680b4
GKH
380 return;
381 }
382
383 do {
50ea95df 384 ret = vmbus_recvpacket(device->channel, packet,
73509681 385 ALIGN(sizeof(struct vstor_packet), 8),
f638859e
HJ
386 &bytes_recvd, &request_id);
387 if (ret == 0 && bytes_recvd > 0) {
163680b4 388 DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx",
f638859e 389 bytes_recvd, request_id);
163680b4 390
163680b4 391
d7a1bdb9 392 request = (struct hv_storvsc_request *)
f638859e 393 (unsigned long)request_id;
163680b4 394
f638859e
HJ
395 if ((request == &stor_device->init_request) ||
396 (request == &stor_device->reset_request)) {
163680b4 397
3d8cdf22 398 memcpy(&request->vstor_packet, packet,
163680b4 399 sizeof(struct vstor_packet));
93958465 400 complete(&request->wait_event);
163680b4 401 } else {
02e37db7 402 stor_vsc_on_receive(device,
163680b4
GKH
403 (struct vstor_packet *)packet,
404 request);
405 }
406 } else {
163680b4
GKH
407 break;
408 }
409 } while (1);
410
02e37db7 411 put_stor_device(device);
163680b4
GKH
412 return;
413}
414
f638859e 415static int stor_vsc_connect_to_vsp(struct hv_device *device)
bef4a34a 416{
b3715ee4 417 struct vmstorage_channel_properties props;
2e79505d 418 struct storvsc_driver *stor_driver;
068c5df2 419 int ret;
bef4a34a 420
786930b3 421 stor_driver = drv_to_stordrv(device->device.driver);
8c960e49 422 memset(&props, 0, sizeof(struct vmstorage_channel_properties));
bef4a34a 423
454f18a9 424 /* Open the channel */
f638859e
HJ
425 ret = vmbus_open(device->channel,
426 stor_driver->ring_buffer_size,
427 stor_driver->ring_buffer_size,
60f841ac
GKH
428 (void *)&props,
429 sizeof(struct vmstorage_channel_properties),
f638859e 430 stor_vsc_on_channel_callback, device);
068c5df2
GKH
431
432 DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d",
d2aaba45 433 props.path_id, props.target_id, props.max_transfer_bytes);
068c5df2
GKH
434
435 if (ret != 0) {
bef4a34a
HJ
436 DPRINT_ERR(STORVSC, "unable to open channel: %d", ret);
437 return -1;
438 }
439
f638859e 440 ret = stor_vsc_channel_init(device);
bef4a34a
HJ
441
442 return ret;
443}
444
3e189519 445/*
02e37db7 446 * stor_vsc_on_device_add - Callback when the device belonging to this driver
3d8cdf22 447 * is added
163680b4 448 */
2ac5dad1 449int storvsc_dev_add(struct hv_device *device,
f638859e 450 void *additional_info)
163680b4 451{
f638859e 452 struct storvsc_device *stor_device;
f638859e 453 struct storvsc_device_info *device_info;
163680b4
GKH
454 int ret = 0;
455
f638859e
HJ
456 device_info = (struct storvsc_device_info *)additional_info;
457 stor_device = alloc_stor_device(device);
458 if (!stor_device) {
163680b4 459 ret = -1;
0c3b7b2f 460 goto cleanup;
163680b4
GKH
461 }
462
463 /* Save the channel properties to our storvsc channel */
163680b4
GKH
464
465 /* FIXME: */
466 /*
467 * If we support more than 1 scsi channel, we need to set the
468 * port number here to the scsi channel but how do we get the
469 * scsi channel prior to the bus scan
470 */
471
f638859e 472 stor_device->port_number = device_info->port_number;
163680b4 473 /* Send it back up */
f638859e 474 ret = stor_vsc_connect_to_vsp(device);
163680b4 475
f638859e
HJ
476 device_info->path_id = stor_device->path_id;
477 device_info->target_id = stor_device->target_id;
163680b4
GKH
478
479 DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n",
f638859e
HJ
480 stor_device->port_number, stor_device->path_id,
481 stor_device->target_id);
163680b4 482
0c3b7b2f 483cleanup:
163680b4
GKH
484 return ret;
485}
bef4a34a 486
3e189519 487/*
02e37db7 488 * stor_vsc_on_device_remove - Callback when the our device is being removed
068c5df2 489 */
cb706b04 490int storvsc_dev_remove(struct hv_device *device)
bef4a34a 491{
f638859e 492 struct storvsc_device *stor_device;
bef4a34a 493
068c5df2 494 DPRINT_INFO(STORVSC, "disabling storage device (%p)...",
ca623ad3 495 device->ext);
bef4a34a 496
f638859e 497 stor_device = release_stor_device(device);
bef4a34a 498
454f18a9
BP
499 /*
500 * At this point, all outbound traffic should be disable. We
501 * only allow inbound traffic (responses) to proceed so that
502 * outstanding requests can be completed.
503 */
f638859e 504 while (atomic_read(&stor_device->num_outstanding_req)) {
068c5df2 505 DPRINT_INFO(STORVSC, "waiting for %d requests to complete...",
f638859e 506 atomic_read(&stor_device->num_outstanding_req));
b4362c9c 507 udelay(100);
bef4a34a
HJ
508 }
509
068c5df2 510 DPRINT_INFO(STORVSC, "removing storage device (%p)...",
ca623ad3 511 device->ext);
bef4a34a 512
f638859e 513 stor_device = final_release_stor_device(device);
bef4a34a 514
f638859e 515 DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", stor_device);
bef4a34a 516
454f18a9 517 /* Close the channel */
f638859e 518 vmbus_close(device->channel);
bef4a34a 519
f638859e 520 free_stor_device(stor_device);
068c5df2 521 return 0;
454f18a9 522}
bef4a34a 523
3e189519 524/*
02e37db7 525 * stor_vsc_on_io_request - Callback to initiate an I/O request
068c5df2 526 */
bb465926 527int storvsc_do_io(struct hv_device *device,
f638859e 528 struct hv_storvsc_request *request)
bef4a34a 529{
f638859e 530 struct storvsc_device *stor_device;
f638859e 531 struct vstor_packet *vstor_packet;
068c5df2 532 int ret = 0;
bef4a34a 533
d7a1bdb9 534 vstor_packet = &request->vstor_packet;
f638859e 535 stor_device = get_stor_device(device);
bef4a34a 536
068c5df2 537 DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, "
d7a1bdb9 538 , device, stor_device, request);
bef4a34a 539
473f9409 540 DPRINT_DBG(STORVSC, "req %p len %d",
d7a1bdb9 541 request, request->data_buffer.len);
bef4a34a 542
f638859e 543 if (!stor_device) {
068c5df2
GKH
544 DPRINT_ERR(STORVSC, "unable to get stor device..."
545 "device being destroyed?");
bef4a34a
HJ
546 return -2;
547 }
548
bef4a34a 549
d7a1bdb9 550 request->device = device;
bef4a34a 551
a617e395 552
f638859e 553 vstor_packet->flags |= REQUEST_COMPLETION_FLAG;
bef4a34a 554
f638859e 555 vstor_packet->vm_srb.length = sizeof(struct vmscsi_request);
bef4a34a 556
bef4a34a 557
f638859e 558 vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE;
bef4a34a 559
bef4a34a 560
e9e936c6 561 vstor_packet->vm_srb.data_transfer_length =
d7a1bdb9 562 request->data_buffer.len;
bef4a34a 563
f638859e 564 vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB;
bef4a34a 565
068c5df2
GKH
566 DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, "
567 "lun %d senselen %d cdblen %d",
f638859e
HJ
568 vstor_packet->vm_srb.length,
569 vstor_packet->vm_srb.port_number,
570 vstor_packet->vm_srb.path_id,
571 vstor_packet->vm_srb.target_id,
572 vstor_packet->vm_srb.lun,
573 vstor_packet->vm_srb.sense_info_length,
574 vstor_packet->vm_srb.cdb_length);
575
d7a1bdb9 576 if (request->data_buffer.len) {
f638859e 577 ret = vmbus_sendpacket_multipagebuffer(device->channel,
d7a1bdb9 578 &request->data_buffer,
f638859e 579 vstor_packet,
b3715ee4 580 sizeof(struct vstor_packet),
d7a1bdb9 581 (unsigned long)request);
068c5df2 582 } else {
f638859e 583 ret = vmbus_sendpacket(device->channel, vstor_packet,
b60d71e2 584 sizeof(struct vstor_packet),
d7a1bdb9 585 (unsigned long)request,
415f2287 586 VM_PKT_DATA_INBAND,
b60d71e2 587 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
bef4a34a
HJ
588 }
589
068c5df2
GKH
590 if (ret != 0) {
591 DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d",
f638859e 592 vstor_packet, ret);
bef4a34a
HJ
593 }
594
f638859e 595 atomic_inc(&stor_device->num_outstanding_req);
bef4a34a 596
f638859e 597 put_stor_device(device);
bef4a34a
HJ
598 return ret;
599}
600
1f91bca8
S
601/*
602 * The channel properties uniquely specify how the device is to be
603 * presented to the guest. Map this information for use by the block
604 * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest
605 * (storvsc_drv) and so scsi devices in the guest are handled by
606 * native upper level Linux drivers. Consequently, Hyper-V
607 * block driver, while being a generic block driver, presently does not
608 * deal with anything other than devices that would need to be presented
609 * to the guest as an IDE disk.
610 *
611 * This function maps the channel properties as embedded in the input
612 * parameter device_info onto information necessary to register the
613 * corresponding block device.
614 *
615 * Currently, there is no way to stop the emulation of the block device
616 * on the host side. And so, to prevent the native IDE drivers in Linux
617 * from taking over these devices (to be managedby Hyper-V block
618 * driver), we will take over if need be the major of the IDE controllers.
619 *
620 */
621
43c51f7d 622int storvsc_get_major_info(struct storvsc_device_info *device_info,
1f91bca8
S
623 struct storvsc_major_info *major_info)
624{
625 static bool ide0_registered;
626 static bool ide1_registered;
627
628 /*
629 * For now we only support IDE disks.
630 */
631 major_info->devname = "ide";
632 major_info->diskname = "hd";
633
634 if (device_info->path_id) {
635 major_info->major = 22;
e46ee8ef 636 if (!ide1_registered) {
1f91bca8 637 major_info->do_register = true;
1f91bca8 638 ide1_registered = true;
e46ee8ef
S
639 } else
640 major_info->do_register = false;
641
1f91bca8
S
642 if (device_info->target_id)
643 major_info->index = 3;
e46ee8ef 644 else
1f91bca8
S
645 major_info->index = 2;
646
647 return 0;
648 } else {
649 major_info->major = 3;
e46ee8ef 650 if (!ide0_registered) {
1f91bca8 651 major_info->do_register = true;
1f91bca8 652 ide0_registered = true;
e46ee8ef
S
653 } else
654 major_info->do_register = false;
655
1f91bca8
S
656 if (device_info->target_id)
657 major_info->index = 1;
658 else
659 major_info->index = 0;
660
661 return 0;
662 }
663
664 return -ENODEV;
665}
666