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