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> | |
bef4a34a | 20 | */ |
5654e932 | 21 | #include <linux/kernel.h> |
0ffa63b0 | 22 | #include <linux/string.h> |
5a0e3ad6 | 23 | #include <linux/slab.h> |
0ffa63b0 | 24 | #include <linux/mm.h> |
b4362c9c | 25 | #include <linux/delay.h> |
4983b39a | 26 | #include "osd.h" |
645954c5 | 27 | #include "logging.h" |
bb969793 | 28 | #include "storvsc_api.h" |
f8e5add2 | 29 | #include "vmbus_packet_format.h" |
2dd88b51 | 30 | #include "vstorage.h" |
50ea95df | 31 | #include "channel.h" |
bef4a34a HJ |
32 | |
33 | ||
7dd03fc4 | 34 | struct storvsc_request_extension { |
068c5df2 | 35 | /* LIST_ENTRY ListEntry; */ |
bef4a34a | 36 | |
3d8cdf22 HJ |
37 | struct hv_storvsc_request *request; |
38 | struct hv_device *device; | |
bef4a34a | 39 | |
454f18a9 | 40 | /* Synchronize the request/response if needed */ |
3d8cdf22 | 41 | struct osd_waitevent *wait_event; |
bef4a34a | 42 | |
3d8cdf22 | 43 | struct vstor_packet vstor_packet; |
7dd03fc4 | 44 | }; |
bef4a34a | 45 | |
454f18a9 | 46 | /* A storvsc device is a device object that contains a vmbus channel */ |
7dd03fc4 | 47 | struct storvsc_device { |
3d8cdf22 | 48 | struct hv_device *device; |
bef4a34a | 49 | |
068c5df2 | 50 | /* 0 indicates the device is being destroyed */ |
3d8cdf22 | 51 | atomic_t ref_count; |
bef4a34a | 52 | |
f638859e | 53 | atomic_t num_outstanding_req; |
bef4a34a | 54 | |
454f18a9 BP |
55 | /* |
56 | * Each unique Port/Path/Target represents 1 channel ie scsi | |
57 | * controller. In reality, the pathid, targetid is always 0 | |
58 | * and the port is set by us | |
59 | */ | |
3d8cdf22 HJ |
60 | unsigned int port_number; |
61 | unsigned char path_id; | |
62 | unsigned char target_id; | |
bef4a34a | 63 | |
068c5df2 GKH |
64 | /* LIST_ENTRY OutstandingRequestList; */ |
65 | /* HANDLE OutstandingRequestLock; */ | |
bef4a34a | 66 | |
454f18a9 | 67 | /* Used for vsc/vsp channel reset process */ |
3d8cdf22 HJ |
68 | struct storvsc_request_extension init_request; |
69 | struct storvsc_request_extension reset_request; | |
7dd03fc4 | 70 | }; |
bef4a34a HJ |
71 | |
72 | ||
3d8cdf22 | 73 | static const char *g_driver_name = "storvsc"; |
bef4a34a | 74 | |
454f18a9 | 75 | /* {ba6163d9-04a1-4d29-b605-72e2ffb1dc7f} */ |
caf26a31 GKH |
76 | static const struct hv_guid gStorVscDeviceType = { |
77 | .data = { | |
78 | 0xd9, 0x63, 0x61, 0xba, 0xa1, 0x04, 0x29, 0x4d, | |
79 | 0xb6, 0x05, 0x72, 0xe2, 0xff, 0xb1, 0xdc, 0x7f | |
80 | } | |
bef4a34a HJ |
81 | }; |
82 | ||
454f18a9 | 83 | |
f638859e | 84 | static inline struct storvsc_device *alloc_stor_device(struct hv_device *device) |
bef4a34a | 85 | { |
f638859e | 86 | struct storvsc_device *stor_device; |
bef4a34a | 87 | |
f638859e HJ |
88 | stor_device = kzalloc(sizeof(struct storvsc_device), GFP_KERNEL); |
89 | if (!stor_device) | |
bef4a34a HJ |
90 | return NULL; |
91 | ||
454f18a9 | 92 | /* Set to 2 to allow both inbound and outbound traffics */ |
3d8cdf22 | 93 | /* (ie get_stor_device() and must_get_stor_device()) to proceed. */ |
f638859e | 94 | atomic_cmpxchg(&stor_device->ref_count, 0, 2); |
bef4a34a | 95 | |
f638859e | 96 | stor_device->device = device; |
ca623ad3 | 97 | device->ext = stor_device; |
bef4a34a | 98 | |
f638859e | 99 | return stor_device; |
bef4a34a HJ |
100 | } |
101 | ||
f638859e | 102 | static inline void free_stor_device(struct storvsc_device *device) |
bef4a34a | 103 | { |
f638859e HJ |
104 | /* ASSERT(atomic_read(&device->ref_count) == 0); */ |
105 | kfree(device); | |
bef4a34a HJ |
106 | } |
107 | ||
454f18a9 | 108 | /* Get the stordevice object iff exists and its refcount > 1 */ |
f638859e | 109 | static inline struct storvsc_device *get_stor_device(struct hv_device *device) |
bef4a34a | 110 | { |
f638859e | 111 | struct storvsc_device *stor_device; |
bef4a34a | 112 | |
ca623ad3 | 113 | stor_device = (struct storvsc_device *)device->ext; |
f638859e HJ |
114 | if (stor_device && atomic_read(&stor_device->ref_count) > 1) |
115 | atomic_inc(&stor_device->ref_count); | |
bef4a34a | 116 | else |
f638859e | 117 | stor_device = NULL; |
bef4a34a | 118 | |
f638859e | 119 | return stor_device; |
bef4a34a HJ |
120 | } |
121 | ||
454f18a9 | 122 | /* Get the stordevice object iff exists and its refcount > 0 */ |
02e37db7 | 123 | static inline struct storvsc_device *must_get_stor_device( |
f638859e | 124 | struct hv_device *device) |
bef4a34a | 125 | { |
f638859e | 126 | struct storvsc_device *stor_device; |
bef4a34a | 127 | |
ca623ad3 | 128 | stor_device = (struct storvsc_device *)device->ext; |
f638859e HJ |
129 | if (stor_device && atomic_read(&stor_device->ref_count)) |
130 | atomic_inc(&stor_device->ref_count); | |
bef4a34a | 131 | else |
f638859e | 132 | stor_device = NULL; |
bef4a34a | 133 | |
f638859e | 134 | return stor_device; |
bef4a34a HJ |
135 | } |
136 | ||
f638859e | 137 | static inline void put_stor_device(struct hv_device *device) |
bef4a34a | 138 | { |
f638859e | 139 | struct storvsc_device *stor_device; |
bef4a34a | 140 | |
ca623ad3 | 141 | stor_device = (struct storvsc_device *)device->ext; |
f638859e | 142 | /* ASSERT(stor_device); */ |
bef4a34a | 143 | |
f638859e HJ |
144 | atomic_dec(&stor_device->ref_count); |
145 | /* ASSERT(atomic_read(&stor_device->ref_count)); */ | |
bef4a34a HJ |
146 | } |
147 | ||
02e37db7 HJ |
148 | /* Drop ref count to 1 to effectively disable get_stor_device() */ |
149 | static inline struct storvsc_device *release_stor_device( | |
f638859e | 150 | struct hv_device *device) |
bef4a34a | 151 | { |
f638859e | 152 | struct storvsc_device *stor_device; |
bef4a34a | 153 | |
ca623ad3 | 154 | stor_device = (struct storvsc_device *)device->ext; |
f638859e | 155 | /* ASSERT(stor_device); */ |
bef4a34a | 156 | |
454f18a9 | 157 | /* Busy wait until the ref drop to 2, then set it to 1 */ |
f638859e | 158 | while (atomic_cmpxchg(&stor_device->ref_count, 2, 1) != 2) |
b4362c9c | 159 | udelay(100); |
bef4a34a | 160 | |
f638859e | 161 | return stor_device; |
bef4a34a HJ |
162 | } |
163 | ||
f638859e | 164 | /* Drop ref count to 0. No one can use stor_device object. */ |
02e37db7 | 165 | static inline struct storvsc_device *final_release_stor_device( |
f638859e | 166 | struct hv_device *device) |
bef4a34a | 167 | { |
f638859e | 168 | struct storvsc_device *stor_device; |
bef4a34a | 169 | |
ca623ad3 | 170 | stor_device = (struct storvsc_device *)device->ext; |
f638859e | 171 | /* ASSERT(stor_device); */ |
bef4a34a | 172 | |
454f18a9 | 173 | /* Busy wait until the ref drop to 1, then set it to 0 */ |
f638859e | 174 | while (atomic_cmpxchg(&stor_device->ref_count, 1, 0) != 1) |
b4362c9c | 175 | udelay(100); |
bef4a34a | 176 | |
ca623ad3 | 177 | device->ext = NULL; |
f638859e | 178 | return stor_device; |
bef4a34a HJ |
179 | } |
180 | ||
f638859e | 181 | static int stor_vsc_channel_init(struct hv_device *device) |
bef4a34a | 182 | { |
f638859e | 183 | struct storvsc_device *stor_device; |
7dd03fc4 | 184 | struct storvsc_request_extension *request; |
f638859e | 185 | struct vstor_packet *vstor_packet; |
068c5df2 | 186 | int ret; |
bef4a34a | 187 | |
f638859e HJ |
188 | stor_device = get_stor_device(device); |
189 | if (!stor_device) { | |
068c5df2 GKH |
190 | DPRINT_ERR(STORVSC, "unable to get stor device..." |
191 | "device being destroyed?"); | |
bef4a34a HJ |
192 | return -1; |
193 | } | |
194 | ||
f638859e HJ |
195 | request = &stor_device->init_request; |
196 | vstor_packet = &request->vstor_packet; | |
bef4a34a | 197 | |
068c5df2 GKH |
198 | /* |
199 | * Now, initiate the vsc/vsp initialization protocol on the open | |
200 | * channel | |
201 | */ | |
8c960e49 | 202 | memset(request, 0, sizeof(struct storvsc_request_extension)); |
3d8cdf22 HJ |
203 | request->wait_event = osd_waitevent_create(); |
204 | if (!request->wait_event) { | |
80d11b2a BP |
205 | ret = -ENOMEM; |
206 | goto nomem; | |
207 | } | |
bef4a34a | 208 | |
f638859e HJ |
209 | vstor_packet->operation = VSTOR_OPERATION_BEGIN_INITIALIZATION; |
210 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
bef4a34a HJ |
211 | |
212 | /*SpinlockAcquire(gDriverExt.packetListLock); | |
213 | INSERT_TAIL_LIST(&gDriverExt.packetList, &packet->listEntry.entry); | |
214 | SpinlockRelease(gDriverExt.packetListLock);*/ | |
215 | ||
216 | DPRINT_INFO(STORVSC, "BEGIN_INITIALIZATION_OPERATION..."); | |
217 | ||
f638859e | 218 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 GKH |
219 | sizeof(struct vstor_packet), |
220 | (unsigned long)request, | |
415f2287 | 221 | VM_PKT_DATA_INBAND, |
b60d71e2 | 222 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
068c5df2 GKH |
223 | if (ret != 0) { |
224 | DPRINT_ERR(STORVSC, | |
225 | "unable to send BEGIN_INITIALIZATION_OPERATION"); | |
bef4a34a HJ |
226 | goto Cleanup; |
227 | } | |
228 | ||
3d8cdf22 | 229 | osd_waitevent_wait(request->wait_event); |
bef4a34a | 230 | |
f638859e HJ |
231 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
232 | vstor_packet->status != 0) { | |
068c5df2 GKH |
233 | DPRINT_ERR(STORVSC, "BEGIN_INITIALIZATION_OPERATION failed " |
234 | "(op %d status 0x%x)", | |
f638859e | 235 | vstor_packet->operation, vstor_packet->status); |
bef4a34a HJ |
236 | goto Cleanup; |
237 | } | |
238 | ||
239 | DPRINT_INFO(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION..."); | |
240 | ||
454f18a9 | 241 | /* reuse the packet for version range supported */ |
f638859e HJ |
242 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
243 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROTOCOL_VERSION; | |
244 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
bef4a34a | 245 | |
f638859e HJ |
246 | vstor_packet->version.major_minor = VMSTOR_PROTOCOL_VERSION_CURRENT; |
247 | FILL_VMSTOR_REVISION(vstor_packet->version.revision); | |
bef4a34a | 248 | |
f638859e | 249 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 GKH |
250 | sizeof(struct vstor_packet), |
251 | (unsigned long)request, | |
415f2287 | 252 | VM_PKT_DATA_INBAND, |
b60d71e2 | 253 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
068c5df2 GKH |
254 | if (ret != 0) { |
255 | DPRINT_ERR(STORVSC, | |
256 | "unable to send BEGIN_INITIALIZATION_OPERATION"); | |
bef4a34a HJ |
257 | goto Cleanup; |
258 | } | |
259 | ||
3d8cdf22 | 260 | osd_waitevent_wait(request->wait_event); |
bef4a34a | 261 | |
454f18a9 | 262 | /* TODO: Check returned version */ |
f638859e HJ |
263 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
264 | vstor_packet->status != 0) { | |
068c5df2 GKH |
265 | DPRINT_ERR(STORVSC, "QUERY_PROTOCOL_VERSION_OPERATION failed " |
266 | "(op %d status 0x%x)", | |
f638859e | 267 | vstor_packet->operation, vstor_packet->status); |
bef4a34a HJ |
268 | goto Cleanup; |
269 | } | |
270 | ||
454f18a9 | 271 | /* Query channel properties */ |
bef4a34a HJ |
272 | DPRINT_INFO(STORVSC, "QUERY_PROPERTIES_OPERATION..."); |
273 | ||
f638859e HJ |
274 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
275 | vstor_packet->operation = VSTOR_OPERATION_QUERY_PROPERTIES; | |
276 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
277 | vstor_packet->storage_channel_properties.port_number = | |
278 | stor_device->port_number; | |
bef4a34a | 279 | |
f638859e | 280 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 GKH |
281 | sizeof(struct vstor_packet), |
282 | (unsigned long)request, | |
415f2287 | 283 | VM_PKT_DATA_INBAND, |
b60d71e2 | 284 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
068c5df2 GKH |
285 | |
286 | if (ret != 0) { | |
287 | DPRINT_ERR(STORVSC, | |
288 | "unable to send QUERY_PROPERTIES_OPERATION"); | |
bef4a34a HJ |
289 | goto Cleanup; |
290 | } | |
291 | ||
3d8cdf22 | 292 | osd_waitevent_wait(request->wait_event); |
bef4a34a | 293 | |
454f18a9 | 294 | /* TODO: Check returned version */ |
f638859e HJ |
295 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
296 | vstor_packet->status != 0) { | |
068c5df2 GKH |
297 | DPRINT_ERR(STORVSC, "QUERY_PROPERTIES_OPERATION failed " |
298 | "(op %d status 0x%x)", | |
f638859e | 299 | vstor_packet->operation, vstor_packet->status); |
bef4a34a HJ |
300 | goto Cleanup; |
301 | } | |
302 | ||
f638859e HJ |
303 | stor_device->path_id = vstor_packet->storage_channel_properties.path_id; |
304 | stor_device->target_id | |
305 | = vstor_packet->storage_channel_properties.target_id; | |
bef4a34a | 306 | |
068c5df2 | 307 | DPRINT_DBG(STORVSC, "channel flag 0x%x, max xfer len 0x%x", |
f638859e HJ |
308 | vstor_packet->storage_channel_properties.flags, |
309 | vstor_packet->storage_channel_properties.max_transfer_bytes); | |
bef4a34a HJ |
310 | |
311 | DPRINT_INFO(STORVSC, "END_INITIALIZATION_OPERATION..."); | |
312 | ||
f638859e HJ |
313 | memset(vstor_packet, 0, sizeof(struct vstor_packet)); |
314 | vstor_packet->operation = VSTOR_OPERATION_END_INITIALIZATION; | |
315 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
bef4a34a | 316 | |
f638859e | 317 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 GKH |
318 | sizeof(struct vstor_packet), |
319 | (unsigned long)request, | |
415f2287 | 320 | VM_PKT_DATA_INBAND, |
b60d71e2 | 321 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
068c5df2 GKH |
322 | |
323 | if (ret != 0) { | |
324 | DPRINT_ERR(STORVSC, | |
325 | "unable to send END_INITIALIZATION_OPERATION"); | |
bef4a34a HJ |
326 | goto Cleanup; |
327 | } | |
328 | ||
3d8cdf22 | 329 | osd_waitevent_wait(request->wait_event); |
bef4a34a | 330 | |
f638859e HJ |
331 | if (vstor_packet->operation != VSTOR_OPERATION_COMPLETE_IO || |
332 | vstor_packet->status != 0) { | |
068c5df2 GKH |
333 | DPRINT_ERR(STORVSC, "END_INITIALIZATION_OPERATION failed " |
334 | "(op %d status 0x%x)", | |
f638859e | 335 | vstor_packet->operation, vstor_packet->status); |
bef4a34a HJ |
336 | goto Cleanup; |
337 | } | |
338 | ||
339 | DPRINT_INFO(STORVSC, "**** storage channel up and running!! ****"); | |
340 | ||
341 | Cleanup: | |
3d8cdf22 HJ |
342 | kfree(request->wait_event); |
343 | request->wait_event = NULL; | |
80d11b2a | 344 | nomem: |
f638859e | 345 | put_stor_device(device); |
bef4a34a HJ |
346 | return ret; |
347 | } | |
348 | ||
f638859e HJ |
349 | static void stor_vsc_on_io_completion(struct hv_device *device, |
350 | struct vstor_packet *vstor_packet, | |
351 | struct storvsc_request_extension *request_ext) | |
163680b4 GKH |
352 | { |
353 | struct hv_storvsc_request *request; | |
f638859e | 354 | struct storvsc_device *stor_device; |
163680b4 | 355 | |
f638859e HJ |
356 | stor_device = must_get_stor_device(device); |
357 | if (!stor_device) { | |
163680b4 GKH |
358 | DPRINT_ERR(STORVSC, "unable to get stor device..." |
359 | "device being destroyed?"); | |
163680b4 GKH |
360 | return; |
361 | } | |
362 | ||
363 | DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION - request extension %p " | |
f638859e HJ |
364 | "completed bytes xfer %u", request_ext, |
365 | vstor_packet->vm_srb.data_transfer_length); | |
163680b4 | 366 | |
f638859e HJ |
367 | /* ASSERT(request_ext != NULL); */ |
368 | /* ASSERT(request_ext->request != NULL); */ | |
163680b4 | 369 | |
f638859e | 370 | request = request_ext->request; |
163680b4 | 371 | |
e2e64432 | 372 | /* ASSERT(request->OnIOCompletion != NULL); */ |
163680b4 GKH |
373 | |
374 | /* Copy over the status...etc */ | |
f638859e | 375 | request->status = vstor_packet->vm_srb.scsi_status; |
163680b4 | 376 | |
f638859e | 377 | if (request->status != 0 || vstor_packet->vm_srb.srb_status != 1) { |
163680b4 GKH |
378 | DPRINT_WARN(STORVSC, |
379 | "cmd 0x%x scsi status 0x%x srb status 0x%x\n", | |
f638859e HJ |
380 | request->cdb[0], vstor_packet->vm_srb.scsi_status, |
381 | vstor_packet->vm_srb.srb_status); | |
163680b4 GKH |
382 | } |
383 | ||
8a046024 | 384 | if ((request->status & 0xFF) == 0x02) { |
163680b4 | 385 | /* CHECK_CONDITION */ |
f638859e | 386 | if (vstor_packet->vm_srb.srb_status & 0x80) { |
163680b4 GKH |
387 | /* autosense data available */ |
388 | DPRINT_WARN(STORVSC, "storvsc pkt %p autosense data " | |
f638859e HJ |
389 | "valid - len %d\n", request_ext, |
390 | vstor_packet->vm_srb.sense_info_length); | |
163680b4 | 391 | |
f638859e | 392 | /* ASSERT(vstor_packet->vm_srb.sense_info_length <= */ |
e2e64432 | 393 | /* request->SenseBufferSize); */ |
8a046024 | 394 | memcpy(request->sense_buffer, |
f638859e HJ |
395 | vstor_packet->vm_srb.sense_data, |
396 | vstor_packet->vm_srb.sense_info_length); | |
163680b4 | 397 | |
8a046024 | 398 | request->sense_buffer_size = |
f638859e | 399 | vstor_packet->vm_srb.sense_info_length; |
163680b4 GKH |
400 | } |
401 | } | |
402 | ||
403 | /* TODO: */ | |
f638859e | 404 | request->bytes_xfer = vstor_packet->vm_srb.data_transfer_length; |
163680b4 | 405 | |
8a046024 | 406 | request->on_io_completion(request); |
163680b4 | 407 | |
f638859e | 408 | atomic_dec(&stor_device->num_outstanding_req); |
163680b4 | 409 | |
f638859e | 410 | put_stor_device(device); |
163680b4 GKH |
411 | } |
412 | ||
f638859e HJ |
413 | static void stor_vsc_on_receive(struct hv_device *device, |
414 | struct vstor_packet *vstor_packet, | |
415 | struct storvsc_request_extension *request_ext) | |
163680b4 | 416 | { |
f638859e | 417 | switch (vstor_packet->operation) { |
d2aaba45 | 418 | case VSTOR_OPERATION_COMPLETE_IO: |
163680b4 | 419 | DPRINT_DBG(STORVSC, "IO_COMPLETE_OPERATION"); |
f638859e | 420 | stor_vsc_on_io_completion(device, vstor_packet, request_ext); |
163680b4 | 421 | break; |
d2aaba45 | 422 | case VSTOR_OPERATION_REMOVE_DEVICE: |
163680b4 GKH |
423 | DPRINT_INFO(STORVSC, "REMOVE_DEVICE_OPERATION"); |
424 | /* TODO: */ | |
425 | break; | |
426 | ||
427 | default: | |
428 | DPRINT_INFO(STORVSC, "Unknown operation received - %d", | |
f638859e | 429 | vstor_packet->operation); |
163680b4 GKH |
430 | break; |
431 | } | |
432 | } | |
433 | ||
02e37db7 | 434 | static void stor_vsc_on_channel_callback(void *context) |
163680b4 GKH |
435 | { |
436 | struct hv_device *device = (struct hv_device *)context; | |
f638859e HJ |
437 | struct storvsc_device *stor_device; |
438 | u32 bytes_recvd; | |
439 | u64 request_id; | |
73509681 | 440 | unsigned char packet[ALIGN(sizeof(struct vstor_packet), 8)]; |
163680b4 GKH |
441 | struct storvsc_request_extension *request; |
442 | int ret; | |
443 | ||
e2e64432 | 444 | /* ASSERT(device); */ |
163680b4 | 445 | |
f638859e HJ |
446 | stor_device = must_get_stor_device(device); |
447 | if (!stor_device) { | |
163680b4 GKH |
448 | DPRINT_ERR(STORVSC, "unable to get stor device..." |
449 | "device being destroyed?"); | |
163680b4 GKH |
450 | return; |
451 | } | |
452 | ||
453 | do { | |
50ea95df | 454 | ret = vmbus_recvpacket(device->channel, packet, |
73509681 | 455 | ALIGN(sizeof(struct vstor_packet), 8), |
f638859e HJ |
456 | &bytes_recvd, &request_id); |
457 | if (ret == 0 && bytes_recvd > 0) { | |
163680b4 | 458 | DPRINT_DBG(STORVSC, "receive %d bytes - tid %llx", |
f638859e | 459 | bytes_recvd, request_id); |
163680b4 | 460 | |
f638859e HJ |
461 | /* ASSERT(bytes_recvd == |
462 | sizeof(struct vstor_packet)); */ | |
163680b4 GKH |
463 | |
464 | request = (struct storvsc_request_extension *) | |
f638859e | 465 | (unsigned long)request_id; |
e2e64432 | 466 | /* ASSERT(request);c */ |
163680b4 | 467 | |
f638859e HJ |
468 | /* if (vstor_packet.Flags & SYNTHETIC_FLAG) */ |
469 | if ((request == &stor_device->init_request) || | |
470 | (request == &stor_device->reset_request)) { | |
163680b4 GKH |
471 | /* DPRINT_INFO(STORVSC, |
472 | * "reset completion - operation " | |
473 | * "%u status %u", | |
f638859e HJ |
474 | * vstor_packet.Operation, |
475 | * vstor_packet.Status); */ | |
163680b4 | 476 | |
3d8cdf22 | 477 | memcpy(&request->vstor_packet, packet, |
163680b4 GKH |
478 | sizeof(struct vstor_packet)); |
479 | ||
3d8cdf22 | 480 | osd_waitevent_set(request->wait_event); |
163680b4 | 481 | } else { |
02e37db7 | 482 | stor_vsc_on_receive(device, |
163680b4 GKH |
483 | (struct vstor_packet *)packet, |
484 | request); | |
485 | } | |
486 | } else { | |
487 | /* DPRINT_DBG(STORVSC, "nothing else to read..."); */ | |
488 | break; | |
489 | } | |
490 | } while (1); | |
491 | ||
02e37db7 | 492 | put_stor_device(device); |
163680b4 GKH |
493 | return; |
494 | } | |
495 | ||
f638859e | 496 | static int stor_vsc_connect_to_vsp(struct hv_device *device) |
bef4a34a | 497 | { |
b3715ee4 | 498 | struct vmstorage_channel_properties props; |
f638859e | 499 | struct storvsc_driver_object *stor_driver; |
068c5df2 | 500 | int ret; |
bef4a34a | 501 | |
ca623ad3 | 502 | stor_driver = (struct storvsc_driver_object *)device->drv; |
8c960e49 | 503 | memset(&props, 0, sizeof(struct vmstorage_channel_properties)); |
bef4a34a | 504 | |
454f18a9 | 505 | /* Open the channel */ |
f638859e HJ |
506 | ret = vmbus_open(device->channel, |
507 | stor_driver->ring_buffer_size, | |
508 | stor_driver->ring_buffer_size, | |
60f841ac GKH |
509 | (void *)&props, |
510 | sizeof(struct vmstorage_channel_properties), | |
f638859e | 511 | stor_vsc_on_channel_callback, device); |
068c5df2 GKH |
512 | |
513 | DPRINT_DBG(STORVSC, "storage props: path id %d, tgt id %d, max xfer %d", | |
d2aaba45 | 514 | props.path_id, props.target_id, props.max_transfer_bytes); |
068c5df2 GKH |
515 | |
516 | if (ret != 0) { | |
bef4a34a HJ |
517 | DPRINT_ERR(STORVSC, "unable to open channel: %d", ret); |
518 | return -1; | |
519 | } | |
520 | ||
f638859e | 521 | ret = stor_vsc_channel_init(device); |
bef4a34a HJ |
522 | |
523 | return ret; | |
524 | } | |
525 | ||
3e189519 | 526 | /* |
02e37db7 | 527 | * stor_vsc_on_device_add - Callback when the device belonging to this driver |
3d8cdf22 | 528 | * is added |
163680b4 | 529 | */ |
f638859e HJ |
530 | static int stor_vsc_on_device_add(struct hv_device *device, |
531 | void *additional_info) | |
163680b4 | 532 | { |
f638859e | 533 | struct storvsc_device *stor_device; |
163680b4 | 534 | /* struct vmstorage_channel_properties *props; */ |
f638859e | 535 | struct storvsc_device_info *device_info; |
163680b4 GKH |
536 | int ret = 0; |
537 | ||
f638859e HJ |
538 | device_info = (struct storvsc_device_info *)additional_info; |
539 | stor_device = alloc_stor_device(device); | |
540 | if (!stor_device) { | |
163680b4 GKH |
541 | ret = -1; |
542 | goto Cleanup; | |
543 | } | |
544 | ||
545 | /* Save the channel properties to our storvsc channel */ | |
546 | /* props = (struct vmstorage_channel_properties *) | |
0686e4f4 | 547 | * channel->offerMsg.Offer.u.Standard.UserDefined; */ |
163680b4 GKH |
548 | |
549 | /* FIXME: */ | |
550 | /* | |
551 | * If we support more than 1 scsi channel, we need to set the | |
552 | * port number here to the scsi channel but how do we get the | |
553 | * scsi channel prior to the bus scan | |
554 | */ | |
555 | ||
556 | /* storChannel->PortNumber = 0; | |
557 | storChannel->PathId = props->PathId; | |
558 | storChannel->TargetId = props->TargetId; */ | |
559 | ||
f638859e | 560 | stor_device->port_number = device_info->port_number; |
163680b4 | 561 | /* Send it back up */ |
f638859e | 562 | ret = stor_vsc_connect_to_vsp(device); |
163680b4 | 563 | |
f638859e HJ |
564 | /* device_info->PortNumber = stor_device->PortNumber; */ |
565 | device_info->path_id = stor_device->path_id; | |
566 | device_info->target_id = stor_device->target_id; | |
163680b4 GKH |
567 | |
568 | DPRINT_DBG(STORVSC, "assigned port %u, path %u target %u\n", | |
f638859e HJ |
569 | stor_device->port_number, stor_device->path_id, |
570 | stor_device->target_id); | |
163680b4 GKH |
571 | |
572 | Cleanup: | |
163680b4 GKH |
573 | return ret; |
574 | } | |
bef4a34a | 575 | |
3e189519 | 576 | /* |
02e37db7 | 577 | * stor_vsc_on_device_remove - Callback when the our device is being removed |
068c5df2 | 578 | */ |
f638859e | 579 | static int stor_vsc_on_device_remove(struct hv_device *device) |
bef4a34a | 580 | { |
f638859e | 581 | struct storvsc_device *stor_device; |
bef4a34a | 582 | |
068c5df2 | 583 | DPRINT_INFO(STORVSC, "disabling storage device (%p)...", |
ca623ad3 | 584 | device->ext); |
bef4a34a | 585 | |
f638859e | 586 | stor_device = release_stor_device(device); |
bef4a34a | 587 | |
454f18a9 BP |
588 | /* |
589 | * At this point, all outbound traffic should be disable. We | |
590 | * only allow inbound traffic (responses) to proceed so that | |
591 | * outstanding requests can be completed. | |
592 | */ | |
f638859e | 593 | while (atomic_read(&stor_device->num_outstanding_req)) { |
068c5df2 | 594 | DPRINT_INFO(STORVSC, "waiting for %d requests to complete...", |
f638859e | 595 | atomic_read(&stor_device->num_outstanding_req)); |
b4362c9c | 596 | udelay(100); |
bef4a34a HJ |
597 | } |
598 | ||
068c5df2 | 599 | DPRINT_INFO(STORVSC, "removing storage device (%p)...", |
ca623ad3 | 600 | device->ext); |
bef4a34a | 601 | |
f638859e | 602 | stor_device = final_release_stor_device(device); |
bef4a34a | 603 | |
f638859e | 604 | DPRINT_INFO(STORVSC, "storage device (%p) safe to remove", stor_device); |
bef4a34a | 605 | |
454f18a9 | 606 | /* Close the channel */ |
f638859e | 607 | vmbus_close(device->channel); |
bef4a34a | 608 | |
f638859e | 609 | free_stor_device(stor_device); |
068c5df2 | 610 | return 0; |
454f18a9 | 611 | } |
bef4a34a | 612 | |
f638859e | 613 | int stor_vsc_on_host_reset(struct hv_device *device) |
bef4a34a | 614 | { |
f638859e | 615 | struct storvsc_device *stor_device; |
7dd03fc4 | 616 | struct storvsc_request_extension *request; |
f638859e | 617 | struct vstor_packet *vstor_packet; |
068c5df2 | 618 | int ret; |
bef4a34a | 619 | |
bef4a34a HJ |
620 | DPRINT_INFO(STORVSC, "resetting host adapter..."); |
621 | ||
f638859e HJ |
622 | stor_device = get_stor_device(device); |
623 | if (!stor_device) { | |
068c5df2 GKH |
624 | DPRINT_ERR(STORVSC, "unable to get stor device..." |
625 | "device being destroyed?"); | |
bef4a34a HJ |
626 | return -1; |
627 | } | |
628 | ||
f638859e HJ |
629 | request = &stor_device->reset_request; |
630 | vstor_packet = &request->vstor_packet; | |
bef4a34a | 631 | |
3d8cdf22 HJ |
632 | request->wait_event = osd_waitevent_create(); |
633 | if (!request->wait_event) { | |
80d11b2a BP |
634 | ret = -ENOMEM; |
635 | goto Cleanup; | |
636 | } | |
bef4a34a | 637 | |
f638859e HJ |
638 | vstor_packet->operation = VSTOR_OPERATION_RESET_BUS; |
639 | vstor_packet->flags = REQUEST_COMPLETION_FLAG; | |
640 | vstor_packet->vm_srb.path_id = stor_device->path_id; | |
bef4a34a | 641 | |
f638859e | 642 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 | 643 | sizeof(struct vstor_packet), |
f638859e | 644 | (unsigned long)&stor_device->reset_request, |
415f2287 | 645 | VM_PKT_DATA_INBAND, |
b60d71e2 | 646 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
068c5df2 GKH |
647 | if (ret != 0) { |
648 | DPRINT_ERR(STORVSC, "Unable to send reset packet %p ret %d", | |
f638859e | 649 | vstor_packet, ret); |
bef4a34a HJ |
650 | goto Cleanup; |
651 | } | |
652 | ||
454f18a9 | 653 | /* FIXME: Add a timeout */ |
3d8cdf22 | 654 | osd_waitevent_wait(request->wait_event); |
bef4a34a | 655 | |
3d8cdf22 | 656 | kfree(request->wait_event); |
bef4a34a HJ |
657 | DPRINT_INFO(STORVSC, "host adapter reset completed"); |
658 | ||
454f18a9 BP |
659 | /* |
660 | * At this point, all outstanding requests in the adapter | |
661 | * should have been flushed out and return to us | |
662 | */ | |
bef4a34a HJ |
663 | |
664 | Cleanup: | |
f638859e | 665 | put_stor_device(device); |
bef4a34a HJ |
666 | return ret; |
667 | } | |
668 | ||
3e189519 | 669 | /* |
02e37db7 | 670 | * stor_vsc_on_io_request - Callback to initiate an I/O request |
068c5df2 | 671 | */ |
f638859e HJ |
672 | static int stor_vsc_on_io_request(struct hv_device *device, |
673 | struct hv_storvsc_request *request) | |
bef4a34a | 674 | { |
f638859e HJ |
675 | struct storvsc_device *stor_device; |
676 | struct storvsc_request_extension *request_extension; | |
677 | struct vstor_packet *vstor_packet; | |
068c5df2 | 678 | int ret = 0; |
bef4a34a | 679 | |
f638859e HJ |
680 | request_extension = |
681 | (struct storvsc_request_extension *)request->extension; | |
682 | vstor_packet = &request_extension->vstor_packet; | |
683 | stor_device = get_stor_device(device); | |
bef4a34a | 684 | |
068c5df2 | 685 | DPRINT_DBG(STORVSC, "enter - Device %p, DeviceExt %p, Request %p, " |
f638859e HJ |
686 | "Extension %p", device, stor_device, request, |
687 | request_extension); | |
bef4a34a HJ |
688 | |
689 | DPRINT_DBG(STORVSC, "req %p len %d bus %d, target %d, lun %d cdblen %d", | |
ca623ad3 | 690 | request, request->data_buffer.len, request->bus, |
f638859e | 691 | request->target_id, request->lun_id, request->cdb_len); |
bef4a34a | 692 | |
f638859e | 693 | if (!stor_device) { |
068c5df2 GKH |
694 | DPRINT_ERR(STORVSC, "unable to get stor device..." |
695 | "device being destroyed?"); | |
bef4a34a HJ |
696 | return -2; |
697 | } | |
698 | ||
f638859e HJ |
699 | /* print_hex_dump_bytes("", DUMP_PREFIX_NONE, request->Cdb, |
700 | * request->CdbLen); */ | |
bef4a34a | 701 | |
f638859e HJ |
702 | request_extension->request = request; |
703 | request_extension->device = device; | |
bef4a34a | 704 | |
f638859e | 705 | memset(vstor_packet, 0 , sizeof(struct vstor_packet)); |
bef4a34a | 706 | |
f638859e | 707 | vstor_packet->flags |= REQUEST_COMPLETION_FLAG; |
bef4a34a | 708 | |
f638859e | 709 | vstor_packet->vm_srb.length = sizeof(struct vmscsi_request); |
bef4a34a | 710 | |
f638859e HJ |
711 | vstor_packet->vm_srb.port_number = request->host; |
712 | vstor_packet->vm_srb.path_id = request->bus; | |
713 | vstor_packet->vm_srb.target_id = request->target_id; | |
714 | vstor_packet->vm_srb.lun = request->lun_id; | |
bef4a34a | 715 | |
f638859e | 716 | vstor_packet->vm_srb.sense_info_length = SENSE_BUFFER_SIZE; |
bef4a34a | 717 | |
454f18a9 | 718 | /* Copy over the scsi command descriptor block */ |
f638859e HJ |
719 | vstor_packet->vm_srb.cdb_length = request->cdb_len; |
720 | memcpy(&vstor_packet->vm_srb.cdb, request->cdb, request->cdb_len); | |
bef4a34a | 721 | |
f638859e | 722 | vstor_packet->vm_srb.data_in = request->type; |
ca623ad3 | 723 | vstor_packet->vm_srb.data_transfer_length = request->data_buffer.len; |
bef4a34a | 724 | |
f638859e | 725 | vstor_packet->operation = VSTOR_OPERATION_EXECUTE_SRB; |
bef4a34a | 726 | |
068c5df2 GKH |
727 | DPRINT_DBG(STORVSC, "srb - len %d port %d, path %d, target %d, " |
728 | "lun %d senselen %d cdblen %d", | |
f638859e HJ |
729 | vstor_packet->vm_srb.length, |
730 | vstor_packet->vm_srb.port_number, | |
731 | vstor_packet->vm_srb.path_id, | |
732 | vstor_packet->vm_srb.target_id, | |
733 | vstor_packet->vm_srb.lun, | |
734 | vstor_packet->vm_srb.sense_info_length, | |
735 | vstor_packet->vm_srb.cdb_length); | |
736 | ||
ca623ad3 | 737 | if (request_extension->request->data_buffer.len) { |
f638859e HJ |
738 | ret = vmbus_sendpacket_multipagebuffer(device->channel, |
739 | &request_extension->request->data_buffer, | |
740 | vstor_packet, | |
b3715ee4 | 741 | sizeof(struct vstor_packet), |
f638859e | 742 | (unsigned long)request_extension); |
068c5df2 | 743 | } else { |
f638859e | 744 | ret = vmbus_sendpacket(device->channel, vstor_packet, |
b60d71e2 | 745 | sizeof(struct vstor_packet), |
f638859e | 746 | (unsigned long)request_extension, |
415f2287 | 747 | VM_PKT_DATA_INBAND, |
b60d71e2 | 748 | VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED); |
bef4a34a HJ |
749 | } |
750 | ||
068c5df2 GKH |
751 | if (ret != 0) { |
752 | DPRINT_DBG(STORVSC, "Unable to send packet %p ret %d", | |
f638859e | 753 | vstor_packet, ret); |
bef4a34a HJ |
754 | } |
755 | ||
f638859e | 756 | atomic_inc(&stor_device->num_outstanding_req); |
bef4a34a | 757 | |
f638859e | 758 | put_stor_device(device); |
bef4a34a HJ |
759 | return ret; |
760 | } | |
761 | ||
3e189519 | 762 | /* |
02e37db7 | 763 | * stor_vsc_on_cleanup - Perform any cleanup when the driver is removed |
068c5df2 | 764 | */ |
f638859e | 765 | static void stor_vsc_on_cleanup(struct hv_driver *driver) |
bef4a34a | 766 | { |
bef4a34a HJ |
767 | } |
768 | ||
3e189519 | 769 | /* |
eb4f3e0a | 770 | * stor_vsc_initialize - Main entry point |
163680b4 | 771 | */ |
f638859e | 772 | int stor_vsc_initialize(struct hv_driver *driver) |
bef4a34a | 773 | { |
f638859e | 774 | struct storvsc_driver_object *stor_driver; |
bef4a34a | 775 | |
f638859e | 776 | stor_driver = (struct storvsc_driver_object *)driver; |
bef4a34a | 777 | |
163680b4 GKH |
778 | DPRINT_DBG(STORVSC, "sizeof(STORVSC_REQUEST)=%zd " |
779 | "sizeof(struct storvsc_request_extension)=%zd " | |
780 | "sizeof(struct vstor_packet)=%zd, " | |
781 | "sizeof(struct vmscsi_request)=%zd", | |
782 | sizeof(struct hv_storvsc_request), | |
783 | sizeof(struct storvsc_request_extension), | |
784 | sizeof(struct vstor_packet), | |
785 | sizeof(struct vmscsi_request)); | |
bef4a34a | 786 | |
163680b4 | 787 | /* Make sure we are at least 2 pages since 1 page is used for control */ |
f638859e | 788 | /* ASSERT(stor_driver->RingBufferSize >= (PAGE_SIZE << 1)); */ |
bef4a34a | 789 | |
f638859e | 790 | driver->name = g_driver_name; |
ca623ad3 | 791 | memcpy(&driver->dev_type, &gStorVscDeviceType, |
163680b4 | 792 | sizeof(struct hv_guid)); |
bef4a34a | 793 | |
f638859e HJ |
794 | stor_driver->request_ext_size = |
795 | sizeof(struct storvsc_request_extension); | |
bef4a34a | 796 | |
163680b4 GKH |
797 | /* |
798 | * Divide the ring buffer data size (which is 1 page less | |
799 | * than the ring buffer size since that page is reserved for | |
800 | * the ring buffer indices) by the max request size (which is | |
430a8e9a | 801 | * vmbus_channel_packet_multipage_buffer + struct vstor_packet + u64) |
163680b4 | 802 | */ |
f638859e HJ |
803 | stor_driver->max_outstanding_req_per_channel = |
804 | ((stor_driver->ring_buffer_size - PAGE_SIZE) / | |
73509681 | 805 | ALIGN(MAX_MULTIPAGE_BUFFER_PACKET + |
163680b4 GKH |
806 | sizeof(struct vstor_packet) + sizeof(u64), |
807 | sizeof(u64))); | |
bef4a34a | 808 | |
163680b4 | 809 | DPRINT_INFO(STORVSC, "max io %u, currently %u\n", |
f638859e | 810 | stor_driver->max_outstanding_req_per_channel, |
163680b4 | 811 | STORVSC_MAX_IO_REQUESTS); |
bef4a34a | 812 | |
163680b4 | 813 | /* Setup the dispatch table */ |
ca623ad3 HZ |
814 | stor_driver->base.dev_add = stor_vsc_on_device_add; |
815 | stor_driver->base.dev_rm = stor_vsc_on_device_remove; | |
816 | stor_driver->base.cleanup = stor_vsc_on_cleanup; | |
bef4a34a | 817 | |
f638859e | 818 | stor_driver->on_io_request = stor_vsc_on_io_request; |
bef4a34a | 819 | |
163680b4 | 820 | return 0; |
bef4a34a | 821 | } |