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