]>
Commit | Line | Data |
---|---|---|
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> | |
6028210e | 20 | * K. Y. Srinivasan <kys@microsoft.com> |
1f91bca8 | 21 | * |
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> |
3f335ea2 S |
30 | |
31 | #include "hyperv.h" | |
e3fe0bb6 | 32 | #include "hv_api.h" |
645954c5 | 33 | #include "logging.h" |
bb969793 | 34 | #include "storvsc_api.h" |
f8e5add2 | 35 | #include "vmbus_packet_format.h" |
2dd88b51 | 36 | #include "vstorage.h" |
50ea95df | 37 | #include "channel.h" |
bef4a34a HJ |
38 | |
39 | ||
f638859e | 40 | static inline struct storvsc_device *alloc_stor_device(struct hv_device *device) |
bef4a34a | 41 | { |
f638859e | 42 | struct storvsc_device *stor_device; |
bef4a34a | 43 | |
f638859e HJ |
44 | stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL); |
45 | if (!stor_device) | |
bef4a34a HJ |
46 | return NULL; |
47 | ||
454f18a9 | 48 | /* Set to 2 to allow both inbound and outbound traffics */ |
3d8cdf22 | 49 | /* (ie get_stor_device() and must_get_stor_device()) to proceed. */ |
f638859e | 50 | atomic_cmpxchg(&stor_device->ref_count, 0, 2); |
bef4a34a | 51 | |
604df37d | 52 | init_waitqueue_head(&stor_device->waiting_to_drain); |
f638859e | 53 | stor_device->device = device; |
ca623ad3 | 54 | device->ext = stor_device; |
bef4a34a | 55 | |
f638859e | 56 | return stor_device; |
bef4a34a HJ |
57 | } |
58 | ||
f638859e | 59 | static inline void free_stor_device(struct storvsc_device *device) |
bef4a34a | 60 | { |
f638859e | 61 | kfree(device); |
bef4a34a HJ |
62 | } |
63 | ||
454f18a9 | 64 | /* Get the stordevice object iff exists and its refcount > 0 */ |
02e37db7 | 65 | static inline struct storvsc_device *must_get_stor_device( |
f638859e | 66 | struct hv_device *device) |
bef4a34a | 67 | { |
f638859e | 68 | struct storvsc_device *stor_device; |
bef4a34a | 69 | |
ca623ad3 | 70 | stor_device = (struct storvsc_device *)device->ext; |
f638859e HJ |
71 | if (stor_device && atomic_read(&stor_device->ref_count)) |
72 | atomic_inc(&stor_device->ref_count); | |
bef4a34a | 73 | else |
f638859e | 74 | stor_device = NULL; |
bef4a34a | 75 | |
f638859e | 76 | return stor_device; |
bef4a34a HJ |
77 | } |
78 | ||
02e37db7 HJ |
79 | /* Drop ref count to 1 to effectively disable get_stor_device() */ |
80 | static inline struct storvsc_device *release_stor_device( | |
f638859e | 81 | struct hv_device *device) |
bef4a34a | 82 | { |
f638859e | 83 | struct storvsc_device *stor_device; |
bef4a34a | 84 | |
ca623ad3 | 85 | stor_device = (struct storvsc_device *)device->ext; |
bef4a34a | 86 | |
454f18a9 | 87 | /* Busy wait until the ref drop to 2, then set it to 1 */ |
f638859e | 88 | while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2) |
b4362c9c | 89 | udelay(100); |
bef4a34a | 90 | |
f638859e | 91 | return stor_device; |
bef4a34a HJ |
92 | } |
93 | ||
f638859e | 94 | /* Drop ref count to 0. No one can use stor_device object. */ |
02e37db7 | 95 | static inline struct storvsc_device *final_release_stor_device( |
f638859e | 96 | struct hv_device *device) |
bef4a34a | 97 | { |
f638859e | 98 | struct storvsc_device *stor_device; |
bef4a34a | 99 | |
ca623ad3 | 100 | stor_device = (struct storvsc_device *)device->ext; |
bef4a34a | 101 | |
454f18a9 | 102 | /* Busy wait until the ref drop to 1, then set it to 0 */ |
f638859e | 103 | while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1) |
b4362c9c | 104 | udelay(100); |
bef4a34a | 105 | |
ca623ad3 | 106 | device->ext = NULL; |
f638859e | 107 | return stor_device; |
bef4a34a HJ |
108 | } |
109 | ||
3fa22517 | 110 | static int storvsc_channel_init(struct hv_device *device) |
bef4a34a | 111 | { |
f638859e | 112 | struct storvsc_device *stor_device; |
d7a1bdb9 | 113 | struct hv_storvsc_request *request; |
f638859e | 114 | struct vstor_packet *vstor_packet; |
93958465 | 115 | int ret, t; |
bef4a34a | 116 | |
f638859e | 117 | stor_device = get_stor_device(device); |
5f61ec34 | 118 | if (!stor_device) |
bef4a34a | 119 | return -1; |
bef4a34a | 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); |
5f61ec34 | 140 | if (ret != 0) |
0c3b7b2f | 141 | goto cleanup; |
0c3b7b2f | 142 | |
93958465 S |
143 | t = wait_for_completion_timeout(&request->wait_event, HZ); |
144 | if (t == 0) { | |
0c3b7b2f S |
145 | ret = -ETIMEDOUT; |
146 | goto cleanup; | |
bef4a34a HJ |
147 | } |
148 | ||
f638859e | 149 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
5f61ec34 | 150 | vstor_packet->status != 0) |
0c3b7b2f | 151 | goto cleanup; |
bef4a34a HJ |
152 | |
153 | DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION..."); | |
154 | ||
454f18a9 | 155 | /* reuse the packet for version range supported */ |
f638859e HJ |
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; | |
bef4a34a | 159 | |
f638859e HJ |
160 | vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT; |
161 | FILL_VMSTOR_REVISION(vstor_packet->version.revision); | |
bef4a34a | 162 | |
f638859e | 163 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 GKH |
164 | sizeof(struct vstor_packet), |
165 | (unsigned long)request, | |
415f2287 | 166 | VM_PKT_DATA_INBAND, |
b60d71e2 | 167 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
5f61ec34 | 168 | if (ret != 0) |
0c3b7b2f | 169 | goto cleanup; |
bef4a34a | 170 | |
93958465 S |
171 | t = wait_for_completion_timeout(&request->wait_event, HZ); |
172 | if (t == 0) { | |
0c3b7b2f S |
173 | ret = -ETIMEDOUT; |
174 | goto cleanup; | |
175 | } | |
bef4a34a | 176 | |
454f18a9 | 177 | /* TODO: Check returned version */ |
f638859e | 178 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
5f61ec34 | 179 | vstor_packet->status != 0) |
0c3b7b2f | 180 | goto cleanup; |
bef4a34a | 181 | |
454f18a9 | 182 | /* Query channel properties */ |
bef4a34a HJ |
183 | DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION..."); |
184 | ||
f638859e HJ |
185 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
186 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES; | |
187 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
188 | vstor_packet->storage_channel_properties.port_number = | |
189 | stor_device->port_number; | |
bef4a34a | 190 | |
f638859e | 191 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 GKH |
192 | sizeof(struct vstor_packet), |
193 | (unsigned long)request, | |
415f2287 | 194 | VM_PKT_DATA_INBAND, |
b60d71e2 | 195 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
068c5df2 | 196 | |
5f61ec34 | 197 | if (ret != 0) |
0c3b7b2f | 198 | goto cleanup; |
bef4a34a | 199 | |
93958465 S |
200 | t = wait_for_completion_timeout(&request->wait_event, HZ); |
201 | if (t == 0) { | |
0c3b7b2f S |
202 | ret = -ETIMEDOUT; |
203 | goto cleanup; | |
204 | } | |
bef4a34a | 205 | |
454f18a9 | 206 | /* TODO: Check returned version */ |
f638859e | 207 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
5f61ec34 | 208 | vstor_packet->status != 0) |
0c3b7b2f | 209 | goto cleanup; |
bef4a34a | 210 | |
f638859e HJ |
211 | stor_device->path_id = vstor_packet->storage_channel_properties.path_id; |
212 | stor_device->target_id | |
213 | = vstor_packet->storage_channel_properties.target_id; | |
bef4a34a | 214 | |
bef4a34a HJ |
215 | DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION..."); |
216 | ||
f638859e HJ |
217 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
218 | vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION; | |
219 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
bef4a34a | 220 | |
f638859e | 221 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 GKH |
222 | sizeof(struct vstor_packet), |
223 | (unsigned long)request, | |
415f2287 | 224 | VM_PKT_DATA_INBAND, |
b60d71e2 | 225 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
068c5df2 | 226 | |
5f61ec34 | 227 | if (ret != 0) |
0c3b7b2f | 228 | goto cleanup; |
bef4a34a | 229 | |
93958465 S |
230 | t = wait_for_completion_timeout(&request->wait_event, HZ); |
231 | if (t == 0) { | |
0c3b7b2f S |
232 | ret = -ETIMEDOUT; |
233 | goto cleanup; | |
234 | } | |
bef4a34a | 235 | |
f638859e | 236 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
5f61ec34 | 237 | vstor_packet->status != 0) |
0c3b7b2f | 238 | goto cleanup; |
bef4a34a HJ |
239 | |
240 | DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****"); | |
241 | ||
0c3b7b2f | 242 | cleanup: |
f638859e | 243 | put_stor_device(device); |
bef4a34a HJ |
244 | return ret; |
245 | } | |
246 | ||
a38b94f9 | 247 | static void storvsc_on_io_completion(struct hv_device *device, |
f638859e | 248 | struct vstor_packet *vstor_packet, |
d7a1bdb9 | 249 | struct hv_storvsc_request *request) |
163680b4 | 250 | { |
f638859e | 251 | struct storvsc_device *stor_device; |
a617e395 | 252 | struct vstor_packet *stor_pkt; |
163680b4 | 253 | |
f638859e | 254 | stor_device = must_get_stor_device(device); |
5f61ec34 | 255 | if (!stor_device) |
163680b4 | 256 | return; |
163680b4 | 257 | |
a617e395 | 258 | stor_pkt = &request->vstor_packet; |
163680b4 | 259 | |
163680b4 GKH |
260 | |
261 | /* Copy over the status...etc */ | |
a617e395 S |
262 | stor_pkt->vm_srb.scsi_status = vstor_packet->vm_srb.scsi_status; |
263 | stor_pkt->vm_srb.srb_status = vstor_packet->vm_srb.srb_status; | |
264 | stor_pkt->vm_srb.sense_info_length = | |
265 | vstor_packet->vm_srb.sense_info_length; | |
163680b4 | 266 | |
6dc3f0a7 | 267 | if (vstor_packet->vm_srb.scsi_status != 0 || |
5f61ec34 | 268 | vstor_packet->vm_srb.srb_status != 1){ |
163680b4 GKH |
269 | DPRINT_WARN(STORVSC, |
270 | "cmd 0x%x scsi status 0x%x srb status 0x%x\n", | |
a617e395 | 271 | stor_pkt->vm_srb.cdb[0], |
373dd8a9 | 272 | vstor_packet->vm_srb.scsi_status, |
f638859e | 273 | vstor_packet->vm_srb.srb_status); |
163680b4 GKH |
274 | } |
275 | ||
6dc3f0a7 | 276 | if ((vstor_packet->vm_srb.scsi_status & 0xFF) == 0x02) { |
163680b4 | 277 | /* CHECK_CONDITION */ |
f638859e | 278 | if (vstor_packet->vm_srb.srb_status & 0x80) { |
163680b4 GKH |
279 | /* autosense data available */ |
280 | DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data " | |
d7a1bdb9 | 281 | "valid - len %d\n", request, |
f638859e | 282 | vstor_packet->vm_srb.sense_info_length); |
163680b4 | 283 | |
d7a1bdb9 | 284 | memcpy(request->sense_buffer, |
f638859e HJ |
285 | vstor_packet->vm_srb.sense_data, |
286 | vstor_packet->vm_srb.sense_info_length); | |
163680b4 | 287 | |
163680b4 GKH |
288 | } |
289 | } | |
290 | ||
a617e395 S |
291 | stor_pkt->vm_srb.data_transfer_length = |
292 | vstor_packet->vm_srb.data_transfer_length; | |
163680b4 | 293 | |
d7a1bdb9 | 294 | request->on_io_completion(request); |
163680b4 | 295 | |
604df37d S |
296 | if (atomic_dec_and_test(&stor_device->num_outstanding_req) && |
297 | stor_device->drain_notify) | |
298 | wake_up(&stor_device->waiting_to_drain); | |
299 | ||
163680b4 | 300 | |
f638859e | 301 | put_stor_device(device); |
163680b4 GKH |
302 | } |
303 | ||
8e6766f8 | 304 | static void storvsc_on_receive(struct hv_device *device, |
f638859e | 305 | struct vstor_packet *vstor_packet, |
d7a1bdb9 | 306 | struct hv_storvsc_request *request) |
163680b4 | 307 | { |
f638859e | 308 | switch (vstor_packet->operation) { |
d2aaba45 | 309 | case VSTOR_OPERATION_COMPLETE_IO: |
a38b94f9 | 310 | storvsc_on_io_completion(device, vstor_packet, request); |
163680b4 | 311 | break; |
d2aaba45 | 312 | case VSTOR_OPERATION_REMOVE_DEVICE: |
163680b4 GKH |
313 | DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION"); |
314 | /* TODO: */ | |
315 | break; | |
316 | ||
317 | default: | |
318 | DPRINT_INFO(STORVSC, "Unknown operation received - %d", | |
f638859e | 319 | vstor_packet->operation); |
163680b4 GKH |
320 | break; |
321 | } | |
322 | } | |
323 | ||
24d9aab0 | 324 | static void storvsc_on_channel_callback(void *context) |
163680b4 GKH |
325 | { |
326 | struct hv_device *device = (struct hv_device *)context; | |
f638859e HJ |
327 | struct storvsc_device *stor_device; |
328 | u32 bytes_recvd; | |
329 | u64 request_id; | |
73509681 | 330 | unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)]; |
d7a1bdb9 | 331 | struct hv_storvsc_request *request; |
163680b4 GKH |
332 | int ret; |
333 | ||
163680b4 | 334 | |
f638859e | 335 | stor_device = must_get_stor_device(device); |
5f61ec34 | 336 | if (!stor_device) |
163680b4 | 337 | return; |
163680b4 GKH |
338 | |
339 | do { | |
50ea95df | 340 | ret = vmbus_recvpacket(device->channel, packet, |
73509681 | 341 | ALIGN(sizeof(struct vstor_packet), 8), |
f638859e HJ |
342 | &bytes_recvd, &request_id); |
343 | if (ret == 0 && bytes_recvd > 0) { | |
163680b4 | 344 | |
d7a1bdb9 | 345 | request = (struct hv_storvsc_request *) |
f638859e | 346 | (unsigned long)request_id; |
163680b4 | 347 | |
f638859e HJ |
348 | if ((request == &stor_device->init_request) || |
349 | (request == &stor_device->reset_request)) { | |
163680b4 | 350 | |
3d8cdf22 | 351 | memcpy(&request->vstor_packet, packet, |
163680b4 | 352 | sizeof(struct vstor_packet)); |
93958465 | 353 | complete(&request->wait_event); |
163680b4 | 354 | } else { |
8e6766f8 | 355 | storvsc_on_receive(device, |
163680b4 GKH |
356 | (struct vstor_packet *)packet, |
357 | request); | |
358 | } | |
359 | } else { | |
163680b4 GKH |
360 | break; |
361 | } | |
362 | } while (1); | |
363 | ||
02e37db7 | 364 | put_stor_device(device); |
163680b4 GKH |
365 | return; |
366 | } | |
367 | ||
fa4d123a | 368 | static int storvsc_connect_to_vsp(struct hv_device *device, u32 ring_size) |
bef4a34a | 369 | { |
b3715ee4 | 370 | struct vmstorage_channel_properties props; |
068c5df2 | 371 | int ret; |
bef4a34a | 372 | |
8c960e49 | 373 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); |
bef4a34a | 374 | |
454f18a9 | 375 | /* Open the channel */ |
f638859e | 376 | ret = vmbus_open(device->channel, |
fa4d123a S |
377 | ring_size, |
378 | ring_size, | |
60f841ac GKH |
379 | (void *)&props, |
380 | sizeof(struct vmstorage_channel_properties), | |
24d9aab0 | 381 | storvsc_on_channel_callback, device); |
068c5df2 | 382 | |
5f61ec34 | 383 | if (ret != 0) |
bef4a34a | 384 | return -1; |
bef4a34a | 385 | |
3fa22517 | 386 | ret = storvsc_channel_init(device); |
bef4a34a HJ |
387 | |
388 | return ret; | |
389 | } | |
390 | ||
2ac5dad1 | 391 | int storvsc_dev_add(struct hv_device *device, |
f638859e | 392 | void *additional_info) |
163680b4 | 393 | { |
f638859e | 394 | struct storvsc_device *stor_device; |
f638859e | 395 | struct storvsc_device_info *device_info; |
163680b4 GKH |
396 | int ret = 0; |
397 | ||
f638859e HJ |
398 | device_info = (struct storvsc_device_info *)additional_info; |
399 | stor_device = alloc_stor_device(device); | |
400 | if (!stor_device) { | |
163680b4 | 401 | ret = -1; |
0c3b7b2f | 402 | goto cleanup; |
163680b4 GKH |
403 | } |
404 | ||
405 | /* Save the channel properties to our storvsc channel */ | |
163680b4 GKH |
406 | |
407 | /* FIXME: */ | |
408 | /* | |
409 | * If we support more than 1 scsi channel, we need to set the | |
410 | * port number here to the scsi channel but how do we get the | |
411 | * scsi channel prior to the bus scan | |
412 | */ | |
413 | ||
f638859e | 414 | stor_device->port_number = device_info->port_number; |
163680b4 | 415 | /* Send it back up */ |
fa4d123a | 416 | ret = storvsc_connect_to_vsp(device, device_info->ring_buffer_size); |
163680b4 | 417 | |
f638859e HJ |
418 | device_info->path_id = stor_device->path_id; |
419 | device_info->target_id = stor_device->target_id; | |
163680b4 | 420 | |
0c3b7b2f | 421 | cleanup: |
163680b4 GKH |
422 | return ret; |
423 | } | |
bef4a34a | 424 | |
cb706b04 | 425 | int storvsc_dev_remove(struct hv_device *device) |
bef4a34a | 426 | { |
f638859e | 427 | struct storvsc_device *stor_device; |
bef4a34a | 428 | |
068c5df2 | 429 | DPRINT_INFO(STORVSC, "disabling storage device (%p)...", |
ca623ad3 | 430 | device->ext); |
bef4a34a | 431 | |
f638859e | 432 | stor_device = release_stor_device(device); |
bef4a34a | 433 | |
454f18a9 BP |
434 | /* |
435 | * At this point, all outbound traffic should be disable. We | |
436 | * only allow inbound traffic (responses) to proceed so that | |
437 | * outstanding requests can be completed. | |
438 | */ | |
19fc2f2a S |
439 | |
440 | storvsc_wait_to_drain(stor_device); | |
bef4a34a | 441 | |
f638859e | 442 | stor_device = final_release_stor_device(device); |
bef4a34a | 443 | |
454f18a9 | 444 | /* Close the channel */ |
f638859e | 445 | vmbus_close(device->channel); |
bef4a34a | 446 | |
f638859e | 447 | free_stor_device(stor_device); |
068c5df2 | 448 | return 0; |
454f18a9 | 449 | } |
bef4a34a | 450 | |
bb465926 | 451 | int storvsc_do_io(struct hv_device *device, |
f638859e | 452 | struct hv_storvsc_request *request) |
bef4a34a | 453 | { |
f638859e | 454 | struct storvsc_device *stor_device; |
f638859e | 455 | struct vstor_packet *vstor_packet; |
068c5df2 | 456 | int ret = 0; |
bef4a34a | 457 | |
d7a1bdb9 | 458 | vstor_packet = &request->vstor_packet; |
f638859e | 459 | stor_device = get_stor_device(device); |
bef4a34a | 460 | |
5f61ec34 | 461 | if (!stor_device) |
bef4a34a | 462 | return -2; |
bef4a34a | 463 | |
bef4a34a | 464 | |
d7a1bdb9 | 465 | request->device = device; |
bef4a34a | 466 | |
a617e395 | 467 | |
f638859e | 468 | vstor_packet->flags |= REQUEST_COMPLETION_FLAG; |
bef4a34a | 469 | |
f638859e | 470 | vstor_packet->vm_srb.length = sizeof(struct vmscsi_request); |
bef4a34a | 471 | |
bef4a34a | 472 | |
f638859e | 473 | vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE; |
bef4a34a | 474 | |
bef4a34a | 475 | |
e9e936c6 | 476 | vstor_packet->vm_srb.data_transfer_length = |
d7a1bdb9 | 477 | request->data_buffer.len; |
bef4a34a | 478 | |
f638859e | 479 | vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB; |
bef4a34a | 480 | |
d7a1bdb9 | 481 | if (request->data_buffer.len) { |
f638859e | 482 | ret = vmbus_sendpacket_multipagebuffer(device->channel, |
d7a1bdb9 | 483 | &request->data_buffer, |
f638859e | 484 | vstor_packet, |
b3715ee4 | 485 | sizeof(struct vstor_packet), |
d7a1bdb9 | 486 | (unsigned long)request); |
068c5df2 | 487 | } else { |
f638859e | 488 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 | 489 | sizeof(struct vstor_packet), |
d7a1bdb9 | 490 | (unsigned long)request, |
415f2287 | 491 | VM_PKT_DATA_INBAND, |
b60d71e2 | 492 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
bef4a34a HJ |
493 | } |
494 | ||
5f61ec34 S |
495 | if (ret != 0) |
496 | return ret; | |
bef4a34a | 497 | |
f638859e | 498 | atomic_inc(&stor_device->num_outstanding_req); |
bef4a34a | 499 | |
f638859e | 500 | put_stor_device(device); |
bef4a34a HJ |
501 | return ret; |
502 | } | |
503 | ||
1f91bca8 S |
504 | /* |
505 | * The channel properties uniquely specify how the device is to be | |
506 | * presented to the guest. Map this information for use by the block | |
507 | * driver. For Linux guests on Hyper-V, we emulate a scsi HBA in the guest | |
508 | * (storvsc_drv) and so scsi devices in the guest are handled by | |
509 | * native upper level Linux drivers. Consequently, Hyper-V | |
510 | * block driver, while being a generic block driver, presently does not | |
511 | * deal with anything other than devices that would need to be presented | |
512 | * to the guest as an IDE disk. | |
513 | * | |
514 | * This function maps the channel properties as embedded in the input | |
515 | * parameter device_info onto information necessary to register the | |
516 | * corresponding block device. | |
517 | * | |
518 | * Currently, there is no way to stop the emulation of the block device | |
519 | * on the host side. And so, to prevent the native IDE drivers in Linux | |
520 | * from taking over these devices (to be managedby Hyper-V block | |
521 | * driver), we will take over if need be the major of the IDE controllers. | |
522 | * | |
523 | */ | |
524 | ||
43c51f7d | 525 | int storvsc_get_major_info(struct storvsc_device_info *device_info, |
1f91bca8 S |
526 | struct storvsc_major_info *major_info) |
527 | { | |
528 | static bool ide0_registered; | |
529 | static bool ide1_registered; | |
530 | ||
531 | /* | |
532 | * For now we only support IDE disks. | |
533 | */ | |
534 | major_info->devname = "ide"; | |
535 | major_info->diskname = "hd"; | |
536 | ||
537 | if (device_info->path_id) { | |
538 | major_info->major = 22; | |
e46ee8ef | 539 | if (!ide1_registered) { |
1f91bca8 | 540 | major_info->do_register = true; |
1f91bca8 | 541 | ide1_registered = true; |
e46ee8ef S |
542 | } else |
543 | major_info->do_register = false; | |
544 | ||
1f91bca8 S |
545 | if (device_info->target_id) |
546 | major_info->index = 3; | |
e46ee8ef | 547 | else |
1f91bca8 S |
548 | major_info->index = 2; |
549 | ||
550 | return 0; | |
551 | } else { | |
552 | major_info->major = 3; | |
e46ee8ef | 553 | if (!ide0_registered) { |
1f91bca8 | 554 | major_info->do_register = true; |
1f91bca8 | 555 | ide0_registered = true; |
e46ee8ef S |
556 | } else |
557 | major_info->do_register = false; | |
558 | ||
1f91bca8 S |
559 | if (device_info->target_id) |
560 | major_info->index = 1; | |
561 | else | |
562 | major_info->index = 0; | |
563 | ||
564 | return 0; | |
565 | } | |
566 | ||
567 | return -ENODEV; | |
568 | } | |
569 |