]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Network/IScsiDxe/IScsiDriver.c
Sync BaseTools Branch (version r2321) to EDKII main trunk.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / IScsiDxe / IScsiDriver.c
CommitLineData
12618416 1/** @file\r
fd82de4e 2 The entry point of IScsi driver.\r
6a690e23 3\r
fd627b16 4Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>\r
e5eed7d3 5This program and the accompanying materials\r
7a444476 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
6a690e23 12\r
12618416 13**/\r
6a690e23 14\r
15#include "IScsiImpl.h"\r
16\r
17EFI_DRIVER_BINDING_PROTOCOL gIScsiDriverBinding = {\r
18 IScsiDriverBindingSupported,\r
19 IScsiDriverBindingStart,\r
20 IScsiDriverBindingStop,\r
21 0xa,\r
22 NULL,\r
23 NULL\r
24};\r
25\r
5af5b3fd 26EFI_GUID gIScsiPrivateGuid = ISCSI_PRIVATE_GUID;\r
6a690e23 27\r
6a690e23 28\r
fd82de4e 29/**\r
30 Tests to see if this driver supports a given controller. If a child device is provided, \r
31 it further tests to see if this driver supports creating a handle for the specified child device.\r
32\r
33 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
34 @param[in] ControllerHandle The handle of the controller to test. This handle \r
35 must support a protocol interface that supplies \r
36 an I/O abstraction to the driver.\r
37 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. \r
38 This parameter is ignored by device drivers, and is optional for bus drivers.\r
39\r
40\r
41 @retval EFI_SUCCESS The device specified by ControllerHandle and\r
42 RemainingDevicePath is supported by the driver specified by This.\r
43 @retval EFI_ALREADY_STARTED The device specified by ControllerHandle and\r
44 RemainingDevicePath is already being managed by the driver\r
45 specified by This.\r
46 @retval EFI_ACCESS_DENIED The device specified by ControllerHandle and\r
47 RemainingDevicePath is already being managed by a different\r
48 driver or an application that requires exclusive acces.\r
49 Currently not implemented.\r
50 @retval EFI_UNSUPPORTED The device specified by ControllerHandle and\r
51 RemainingDevicePath is not supported by the driver specified by This.\r
12618416 52**/\r
53EFI_STATUS\r
54EFIAPI\r
55IScsiDriverBindingSupported (\r
fd82de4e 56 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
12618416 57 IN EFI_HANDLE ControllerHandle,\r
fd82de4e 58 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
12618416 59 )\r
6a690e23 60{\r
61 EFI_STATUS Status;\r
62 EFI_DEVICE_PATH_PROTOCOL *CurrentDevicePath;\r
63\r
64 Status = gBS->OpenProtocol (\r
65 ControllerHandle,\r
5af5b3fd 66 &gIScsiPrivateGuid,\r
6a690e23 67 NULL,\r
68 This->DriverBindingHandle,\r
69 ControllerHandle,\r
70 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
71 );\r
72 if (!EFI_ERROR (Status)) {\r
73 return EFI_ALREADY_STARTED;\r
74 }\r
75\r
76 Status = gBS->OpenProtocol (\r
77 ControllerHandle,\r
78 &gEfiTcp4ServiceBindingProtocolGuid,\r
79 NULL,\r
80 This->DriverBindingHandle,\r
81 ControllerHandle,\r
82 EFI_OPEN_PROTOCOL_TEST_PROTOCOL\r
83 );\r
84 if (EFI_ERROR (Status)) {\r
85 return EFI_UNSUPPORTED;\r
86 }\r
87\r
88 CurrentDevicePath = RemainingDevicePath;\r
89 if (CurrentDevicePath != NULL) {\r
90 while (!IsDevicePathEnd (CurrentDevicePath)) {\r
91 if ((CurrentDevicePath->Type == MESSAGING_DEVICE_PATH) && (CurrentDevicePath->SubType == MSG_ISCSI_DP)) {\r
92 return EFI_SUCCESS;\r
93 }\r
94\r
95 CurrentDevicePath = NextDevicePathNode (CurrentDevicePath);\r
96 }\r
97\r
98 return EFI_UNSUPPORTED;\r
99 }\r
100\r
101 return EFI_SUCCESS;\r
102}\r
103\r
12618416 104/**\r
55a64ae0 105 Start this driver on ControllerHandle. \r
106 \r
107 The Start() function is designed to be invoked from the EFI boot service ConnectController(). \r
108 As a result, much of the error checking on the parameters to Start() has been moved into this \r
109 common boot service. It is legal to call Start() from other locations, but the following calling \r
110 restrictions must be followed or the system behavior will not be deterministic.\r
fd82de4e 111 1. ControllerHandle must be a valid EFI_HANDLE.\r
112 2. If RemainingDevicePath is not NULL, then it must be a pointer to a naturally aligned\r
113 EFI_DEVICE_PATH_PROTOCOL.\r
114 3. Prior to calling Start(), the Supported() function for the driver specified by This must\r
115 have been called with the same calling parameters, and Supported() must have returned EFI_SUCCESS. \r
116\r
117 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
118 @param[in] ControllerHandle The handle of the controller to start. This handle \r
119 must support a protocol interface that supplies \r
120 an I/O abstraction to the driver.\r
121 @param[in] RemainingDevicePath A pointer to the remaining portion of a device path. \r
122 This parameter is ignored by device drivers, and is optional for bus drivers.\r
123\r
124 @retval EFI_SUCCESS The device was started.\r
125 @retval EFI_DEVICE_ERROR The device could not be started due to a device error.\r
126 Currently not implemented.\r
127 @retval EFI_OUT_OF_RESOURCES The request could not be completed due to a lack of resources.\r
128 @retval Others The driver failded to start the device.\r
12618416 129**/\r
6a690e23 130EFI_STATUS\r
131EFIAPI\r
132IScsiDriverBindingStart (\r
fd82de4e 133 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
6a690e23 134 IN EFI_HANDLE ControllerHandle,\r
fd82de4e 135 IN EFI_DEVICE_PATH_PROTOCOL *RemainingDevicePath OPTIONAL\r
6a690e23 136 )\r
6a690e23 137{\r
138 EFI_STATUS Status;\r
139 ISCSI_DRIVER_DATA *Private;\r
fd627b16 140 VOID *Interface;\r
141\r
142 Private = IScsiCreateDriverData (This->DriverBindingHandle, ControllerHandle);\r
143 if (Private == NULL) {\r
144 return EFI_OUT_OF_RESOURCES;\r
145 }\r
146\r
147 //\r
148 // Create a underlayer child instance, but not need to configure it. Just open ChildHandle\r
149 // via BY_DRIVER. That is, establishing the relationship between ControllerHandle and ChildHandle.\r
150 // Therefore, when DisconnectController(), especially VLAN virtual controller handle,\r
151 // IScsiDriverBindingStop() will be called.\r
152 //\r
153 Status = NetLibCreateServiceChild (\r
154 ControllerHandle,\r
155 This->DriverBindingHandle,\r
156 &gEfiTcp4ServiceBindingProtocolGuid,\r
157 &Private->ChildHandle\r
158 );\r
159\r
160 if (EFI_ERROR (Status)) {\r
161 goto ON_ERROR;\r
162 }\r
163\r
164 Status = gBS->OpenProtocol (\r
165 Private->ChildHandle,\r
166 &gEfiTcp4ProtocolGuid,\r
167 &Interface,\r
168 This->DriverBindingHandle,\r
169 ControllerHandle,\r
170 EFI_OPEN_PROTOCOL_BY_DRIVER\r
171 );\r
172 if (EFI_ERROR (Status)) {\r
173 goto ON_ERROR;\r
174 }\r
175\r
176 //
177 // Always install private protocol no matter what happens later. We need to \r
178 // keep the relationship between ControllerHandle and ChildHandle.\r
179 //\r
180 Status = gBS->InstallProtocolInterface (\r
181 &ControllerHandle,\r
182 &gIScsiPrivateGuid,\r
183 EFI_NATIVE_INTERFACE,\r
184 &Private->IScsiIdentifier\r
185 );\r
186 if (EFI_ERROR (Status)) {\r
187 goto ON_ERROR;\r
188 }\r
6a690e23 189\r
190 //\r
191 // Try to add a port configuration page for this controller.\r
192 //\r
193 IScsiConfigUpdateForm (This->DriverBindingHandle, ControllerHandle, TRUE);\r
194\r
6a690e23 195 //\r
196 // Get the iSCSI configuration data of this controller.\r
197 //\r
198 Status = IScsiGetConfigData (Private);\r
199 if (EFI_ERROR (Status)) {\r
200 goto ON_ERROR;\r
201 }\r
202 //\r
203 // Try to login and create an iSCSI session according to the configuration.\r
204 //\r
205 Status = IScsiSessionLogin (Private);\r
206 if (Status == EFI_MEDIA_CHANGED) {\r
207 //\r
208 // The specified target is not available and the redirection information is\r
209 // got, login the session again with the updated target address.\r
210 //\r
211 Status = IScsiSessionLogin (Private);\r
212 }\r
213\r
214 if (EFI_ERROR (Status)) {\r
215 goto ON_ERROR;\r
216 }\r
217 //\r
218 // Duplicate the Session's tcp connection device path. The source port field\r
219 // will be set to zero as one iSCSI session is comprised of several iSCSI\r
220 // connections.\r
221 //\r
222 Private->DevicePath = IScsiGetTcpConnDevicePath (Private);\r
223 if (Private->DevicePath == NULL) {\r
224 goto ON_ERROR;\r
225 }\r
226 //\r
227 // Install the updated device path onto the ExtScsiPassThruHandle.\r
228 //\r
229 Status = gBS->InstallProtocolInterface (\r
230 &Private->ExtScsiPassThruHandle,\r
231 &gEfiDevicePathProtocolGuid,\r
232 EFI_NATIVE_INTERFACE,\r
233 Private->DevicePath\r
234 );\r
235 if (EFI_ERROR (Status)) {\r
236 goto ON_ERROR;\r
237 }\r
fd627b16 238\r
6a690e23 239 //\r
240 // Update/Publish the iSCSI Boot Firmware Table.\r
241 //\r
242 IScsiPublishIbft ();\r
243\r
244 return EFI_SUCCESS;\r
245\r
246ON_ERROR:\r
247\r
248 IScsiSessionAbort (&Private->Session);\r
6a690e23 249\r
250 return Status;\r
251}\r
252\r
12618416 253/**\r
fd82de4e 254 Stop this driver on ControllerHandle. \r
255 \r
256 Release the control of this controller and remove the IScsi functions. The Stop()\r
257 function is designed to be invoked from the EFI boot service DisconnectController(). \r
258 As a result, much of the error checking on the parameters to Stop() has been moved \r
259 into this common boot service. It is legal to call Stop() from other locations, \r
260 but the following calling restrictions must be followed or the system behavior will not be deterministic.\r
261 1. ControllerHandle must be a valid EFI_HANDLE that was used on a previous call to this\r
262 same driver's Start() function.\r
263 2. The first NumberOfChildren handles of ChildHandleBuffer must all be a valid\r
264 EFI_HANDLE. In addition, all of these handles must have been created in this driver's\r
265 Start() function, and the Start() function must have called OpenProtocol() on\r
266 ControllerHandle with an Attribute of EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER.\r
267 \r
268 @param[in] This A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.\r
269 @param[in] ControllerHandle A handle to the device being stopped. The handle must \r
270 support a bus specific I/O protocol for the driver \r
271 to use to stop the device.\r
272 @param[in] NumberOfChildren The number of child device handles in ChildHandleBuffer.Not used.\r
273 @param[in] ChildHandleBuffer An array of child handles to be freed. May be NULL \r
274 if NumberOfChildren is 0.Not used.\r
275\r
276 @retval EFI_SUCCESS The device was stopped.\r
277 @retval EFI_DEVICE_ERROR The device could not be stopped due to a device error.\r
12618416 278**/\r
6a690e23 279EFI_STATUS\r
280EFIAPI\r
281IScsiDriverBindingStop (\r
282 IN EFI_DRIVER_BINDING_PROTOCOL *This,\r
283 IN EFI_HANDLE ControllerHandle,\r
284 IN UINTN NumberOfChildren,\r
c5de0d55 285 IN EFI_HANDLE *ChildHandleBuffer OPTIONAL\r
6a690e23 286 )\r
6a690e23 287{\r
288 EFI_HANDLE IScsiController;\r
289 EFI_STATUS Status;\r
290 ISCSI_PRIVATE_PROTOCOL *IScsiIdentifier;\r
291 ISCSI_DRIVER_DATA *Private;\r
292 EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru;\r
293 ISCSI_CONNECTION *Conn;\r
294\r
295 if (NumberOfChildren != 0) {\r
296 //\r
297 // We should have only one child.\r
298 //\r
299 Status = gBS->OpenProtocol (\r
300 ChildHandleBuffer[0],\r
301 &gEfiExtScsiPassThruProtocolGuid,\r
302 (VOID **) &PassThru,\r
303 This->DriverBindingHandle,\r
304 ControllerHandle,\r
305 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
306 );\r
307 if (EFI_ERROR (Status)) {\r
308 return EFI_DEVICE_ERROR;\r
309 }\r
310\r
311 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru);\r
312 Conn = NET_LIST_HEAD (&Private->Session.Conns, ISCSI_CONNECTION, Link);\r
313\r
314 //\r
315 // Previously the TCP4 protocol is opened BY_CHILD_CONTROLLER. Just close\r
316 // the protocol here but not uninstall the device path protocol and\r
317 // EXT SCSI PASS THRU protocol installed on ExtScsiPassThruHandle.\r
318 //\r
319 gBS->CloseProtocol (\r
320 Conn->Tcp4Io.Handle,\r
321 &gEfiTcp4ProtocolGuid,\r
322 Private->Image,\r
323 Private->ExtScsiPassThruHandle\r
324 );\r
325\r
326 return EFI_SUCCESS;\r
327 }\r
328 //\r
329 // Get the handle of the controller we are controling.\r
330 //\r
331 IScsiController = NetLibGetNicHandle (ControllerHandle, &gEfiTcp4ProtocolGuid);\r
332\r
333 Status = gBS->OpenProtocol (\r
334 IScsiController,\r
5af5b3fd 335 &gIScsiPrivateGuid,\r
6a690e23 336 (VOID **)&IScsiIdentifier,\r
337 This->DriverBindingHandle,\r
338 ControllerHandle,\r
339 EFI_OPEN_PROTOCOL_GET_PROTOCOL\r
340 );\r
341 if (EFI_ERROR (Status)) {\r
342 return EFI_DEVICE_ERROR;\r
343 }\r
344\r
345 Private = ISCSI_DRIVER_DATA_FROM_IDENTIFIER (IScsiIdentifier);\r
346\r
fd627b16 347 if (Private->ChildHandle != NULL) {\r
348 Status = gBS->CloseProtocol (\r
349 Private->ChildHandle,\r
350 &gEfiTcp4ProtocolGuid,\r
351 This->DriverBindingHandle,\r
352 IScsiController\r
353 );\r
354\r
355 ASSERT (!EFI_ERROR (Status));\r
356\r
357 Status = NetLibDestroyServiceChild (\r
358 IScsiController,\r
359 This->DriverBindingHandle,\r
360 &gEfiTcp4ServiceBindingProtocolGuid,\r
361 Private->ChildHandle\r
362 );\r
363 ASSERT (!EFI_ERROR (Status));\r
364 }\r
365\r
366 IScsiConfigUpdateForm (This->DriverBindingHandle, IScsiController, FALSE);\r
367\r
6a690e23 368 //\r
369 // Uninstall the private protocol.\r
370 //\r
371 gBS->UninstallProtocolInterface (\r
372 IScsiController,\r
5af5b3fd 373 &gIScsiPrivateGuid,\r
6a690e23 374 &Private->IScsiIdentifier\r
375 );\r
376\r
377 //\r
378 // Update the iSCSI Boot Firware Table.\r
379 //\r
380 IScsiPublishIbft ();\r
381\r
382 IScsiSessionAbort (&Private->Session);\r
383 IScsiCleanDriverData (Private);\r
384\r
385 return EFI_SUCCESS;\r
386}\r
387\r
12618416 388/**\r
fd82de4e 389 Unloads an image(the iSCSI driver).\r
6a690e23 390\r
fd82de4e 391 @param[in] ImageHandle Handle that identifies the image to be unloaded.\r
6a690e23 392\r
fd82de4e 393 @retval EFI_SUCCESS The image has been unloaded.\r
963dbb30 394 @retval Others Other errors as indicated.\r
12618416 395**/\r
396EFI_STATUS\r
397EFIAPI\r
398EfiIScsiUnload (\r
399 IN EFI_HANDLE ImageHandle\r
400 )\r
6a690e23 401{\r
402 EFI_STATUS Status;\r
403 UINTN DeviceHandleCount;\r
404 EFI_HANDLE *DeviceHandleBuffer;\r
405 UINTN Index;\r
406\r
407 //\r
408 // Try to disonnect the driver from the devices it's controlling.\r
409 //\r
410 Status = gBS->LocateHandleBuffer (\r
411 AllHandles,\r
412 NULL,\r
413 NULL,\r
414 &DeviceHandleCount,\r
415 &DeviceHandleBuffer\r
416 );\r
417 if (!EFI_ERROR (Status)) {\r
418 for (Index = 0; Index < DeviceHandleCount; Index++) {\r
419 Status = gBS->DisconnectController (\r
420 DeviceHandleBuffer[Index],\r
421 ImageHandle,\r
422 NULL\r
423 );\r
424 }\r
425\r
426 if (DeviceHandleBuffer != NULL) {\r
766c7483 427 FreePool (DeviceHandleBuffer);\r
6a690e23 428 }\r
429 }\r
430 //\r
431 // Unload the iSCSI configuration form.\r
432 //\r
433 IScsiConfigFormUnload (gIScsiDriverBinding.DriverBindingHandle);\r
434\r
435 //\r
436 // Uninstall the protocols installed by iSCSI driver.\r
437 //\r
438 Status = gBS->UninstallMultipleProtocolInterfaces (\r
439 ImageHandle,\r
440 &gEfiDriverBindingProtocolGuid,\r
441 &gIScsiDriverBinding,\r
442 &gEfiComponentName2ProtocolGuid,\r
443 &gIScsiComponentName2,\r
444 &gEfiComponentNameProtocolGuid,\r
445 &gIScsiComponentName,\r
446 &gEfiIScsiInitiatorNameProtocolGuid,\r
447 &gIScsiInitiatorName,\r
448 NULL\r
449 );\r
450\r
451 return Status;\r
452}\r
453\r
12618416 454/**\r
fd82de4e 455 This is the declaration of an EFI image entry point. This entry point is\r
456 the same for UEFI Applications, UEFI OS Loaders, and UEFI Drivers including\r
457 both device drivers and bus drivers. It initialize the global variables and \r
458 publish the driver binding protocol.\r
6a690e23 459\r
fd82de4e 460 @param[in] ImageHandle The firmware allocated handle for the UEFI image.\r
461 @param[in] SystemTable A pointer to the EFI System Table.\r
6a690e23 462\r
fd82de4e 463 @retval EFI_SUCCESS The operation completed successfully.\r
464 @retval EFI_ACCESS_DENIED EFI_ISCSI_INITIATOR_NAME_PROTOCOL was installed unexpectedly.\r
963dbb30 465 @retval Others Other errors as indicated.\r
12618416 466**/\r
467EFI_STATUS\r
468EFIAPI\r
469IScsiDriverEntryPoint (\r
470 IN EFI_HANDLE ImageHandle,\r
471 IN EFI_SYSTEM_TABLE *SystemTable\r
472 )\r
6a690e23 473{\r
f2a94e25 474 EFI_STATUS Status;\r
475 EFI_ISCSI_INITIATOR_NAME_PROTOCOL *IScsiInitiatorName;\r
476\r
477 //\r
478 // There should be only one EFI_ISCSI_INITIATOR_NAME_PROTOCOL.\r
479 //\r
480 Status = gBS->LocateProtocol (\r
481 &gEfiIScsiInitiatorNameProtocolGuid,\r
482 NULL,\r
b4df5011 483 (VOID**) &IScsiInitiatorName\r
f2a94e25 484 );\r
485\r
486 if (!EFI_ERROR (Status)) {\r
487 return EFI_ACCESS_DENIED;\r
488 }\r
6a690e23 489\r
490 //\r
491 // Initialize the EFI Driver Library\r
492 //\r
493 Status = EfiLibInstallDriverBindingComponentName2 (\r
494 ImageHandle,\r
495 SystemTable,\r
496 &gIScsiDriverBinding,\r
497 ImageHandle,\r
498 &gIScsiComponentName,\r
499 &gIScsiComponentName2\r
500 );\r
6a690e23 501\r
502 if (!EFI_ERROR (Status)) {\r
f2a94e25 503 //\r
504 // Install the iSCSI Initiator Name Protocol.\r
505 //\r
6a690e23 506 Status = gBS->InstallProtocolInterface (\r
507 &ImageHandle,\r
508 &gEfiIScsiInitiatorNameProtocolGuid,\r
509 EFI_NATIVE_INTERFACE,\r
510 &gIScsiInitiatorName\r
511 );\r
512 if (EFI_ERROR (Status)) {\r
513 gBS->UninstallMultipleProtocolInterfaces (\r
514 ImageHandle,\r
515 &gEfiDriverBindingProtocolGuid,\r
516 &gIScsiDriverBinding,\r
517 &gEfiComponentName2ProtocolGuid,\r
518 &gIScsiComponentName2,\r
519 &gEfiComponentNameProtocolGuid,\r
520 &gIScsiComponentName,\r
521 NULL\r
522 );\r
f2a94e25 523 return Status;\r
524 }\r
525 \r
526 //\r
527 // Initialize the configuration form of iSCSI.\r
528 //\r
963dbb30 529 Status = IScsiConfigFormInit ();\r
f2a94e25 530 if (EFI_ERROR (Status)) {\r
531 gBS->UninstallMultipleProtocolInterfaces (\r
532 ImageHandle,\r
533 &gEfiDriverBindingProtocolGuid,\r
534 &gIScsiDriverBinding,\r
535 &gEfiComponentName2ProtocolGuid,\r
536 &gIScsiComponentName2,\r
537 &gEfiComponentNameProtocolGuid,\r
538 &gIScsiComponentName,\r
539 &gEfiIScsiInitiatorNameProtocolGuid,\r
540 &gIScsiInitiatorName,\r
541 NULL\r
542 );\r
6a690e23 543 }\r
544 }\r
6a690e23 545 return Status;\r
546}\r
547\r