]> git.proxmox.com Git - mirror_edk2.git/blame - NetworkPkg/IScsiDxe/IScsiProto.c
NetworkPkg: Fix the wrong Timer event check
[mirror_edk2.git] / NetworkPkg / IScsiDxe / IScsiProto.c
CommitLineData
4c5a5e0c 1/** @file\r
2 The implementation of iSCSI protocol based on RFC3720.\r
3\r
7c275b3c 4Copyright (c) 2004 - 2016, Intel Corporation. All rights reserved.<BR>\r
4c5a5e0c 5This program and the accompanying materials\r
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
12\r
13**/\r
14\r
15#include "IScsiImpl.h"\r
16\r
17UINT32 mDataSegPad = 0;\r
18\r
19/**\r
20 Attach the iSCSI connection to the iSCSI session. \r
21\r
22 @param[in, out] Session The iSCSI session.\r
23 @param[in, out] Conn The iSCSI connection.\r
24\r
25**/\r
26VOID\r
27IScsiAttatchConnection (\r
28 IN OUT ISCSI_SESSION *Session,\r
29 IN OUT ISCSI_CONNECTION *Conn\r
30 )\r
31{\r
32 InsertTailList (&Session->Conns, &Conn->Link);\r
33 Conn->Session = Session;\r
34 Session->NumConns++;\r
35}\r
36\r
37/**\r
38 Detach the iSCSI connection from the session it belongs to. \r
39\r
40 @param[in, out] Conn The iSCSI connection.\r
41\r
42**/\r
43VOID\r
44IScsiDetatchConnection (\r
45 IN OUT ISCSI_CONNECTION *Conn\r
46 )\r
47{\r
48 RemoveEntryList (&Conn->Link);\r
49 Conn->Session->NumConns--;\r
50 Conn->Session = NULL;\r
51}\r
52\r
53\r
54/**\r
55 Check the sequence number according to RFC3720. \r
56\r
57 @param[in, out] ExpSN The currently expected sequence number.\r
58 @param[in] NewSN The sequence number to check.\r
59\r
60 @retval EFI_SUCCESS The check passed and the ExpSN is increased.\r
61 @retval EFI_NOT_READY Response was sent due to a retransmission request.\r
62 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.\r
63\r
64**/\r
65EFI_STATUS\r
66IScsiCheckSN (\r
67 IN OUT UINT32 *ExpSN,\r
68 IN UINT32 NewSN\r
69 )\r
70{\r
71 if (!ISCSI_SEQ_EQ (NewSN, *ExpSN)) {\r
72 if (ISCSI_SEQ_LT (NewSN, *ExpSN)) {\r
73 //\r
74 // Duplicate\r
75 //\r
76 return EFI_NOT_READY;\r
77 } else {\r
78 return EFI_PROTOCOL_ERROR;\r
79 }\r
80 } else {\r
81 //\r
82 // Advance the ExpSN\r
83 //\r
84 (*ExpSN)++;\r
85 return EFI_SUCCESS;\r
86 }\r
87}\r
88\r
89\r
90/**\r
91 Update the sequence numbers for the iSCSI command.\r
92\r
93 @param[in, out] Session The iSCSI session.\r
94 @param[in] MaxCmdSN Maximum CmdSN from the target.\r
95 @param[in] ExpCmdSN Next expected CmdSN from the target.\r
96\r
97**/\r
98VOID\r
99IScsiUpdateCmdSN (\r
100 IN OUT ISCSI_SESSION *Session,\r
101 IN UINT32 MaxCmdSN,\r
102 IN UINT32 ExpCmdSN\r
103 )\r
104{\r
105 if (ISCSI_SEQ_LT (MaxCmdSN, ExpCmdSN - 1)) {\r
106 return ;\r
107 }\r
108\r
109 if (ISCSI_SEQ_GT (MaxCmdSN, Session->MaxCmdSN)) {\r
110 Session->MaxCmdSN = MaxCmdSN;\r
111 }\r
112\r
113 if (ISCSI_SEQ_GT (ExpCmdSN, Session->ExpCmdSN)) {\r
114 Session->ExpCmdSN = ExpCmdSN;\r
115 }\r
116}\r
117\r
118\r
119/**\r
120 This function does the iSCSI connection login.\r
121\r
122 @param[in, out] Conn The iSCSI connection to login.\r
123 @param Timeout The timeout value in millisecond.\r
124\r
125 @retval EFI_SUCCESS The iSCSI connection is logged into the iSCSI target.\r
126 @retval EFI_TIMEOUT Timeout occurred during the login procedure.\r
127 @retval Others Other errors as indicated. \r
128\r
129**/\r
130EFI_STATUS\r
131IScsiConnLogin (\r
132 IN OUT ISCSI_CONNECTION *Conn,\r
133 IN UINT16 Timeout\r
134 )\r
135{\r
136 EFI_STATUS Status;\r
137\r
138 //\r
139 // Start the timer, and wait Timeout seconds to establish the TCP connection.\r
140 //\r
689c9d97
HW
141 Status = gBS->SetTimer (\r
142 Conn->TimeoutEvent,\r
143 TimerRelative,\r
144 MultU64x32 (Timeout, TICKS_PER_MS)\r
145 );\r
4c5a5e0c 146 if (EFI_ERROR (Status)) {\r
147 return Status;\r
148 }\r
149\r
150 //\r
151 // Try to establish the tcp connection.\r
152 //\r
153 Status = TcpIoConnect (&Conn->TcpIo, Conn->TimeoutEvent);\r
154 gBS->SetTimer (Conn->TimeoutEvent, TimerCancel, 0);\r
155\r
156 if (EFI_ERROR (Status)) {\r
157 return Status;\r
158 }\r
159\r
160 Conn->State = CONN_STATE_IN_LOGIN;\r
161\r
162 //\r
163 // Connection is established, start the iSCSI Login.\r
164 //\r
165 do {\r
166 Status = IScsiSendLoginReq (Conn);\r
167 if (EFI_ERROR (Status)) {\r
168 break;\r
169 }\r
170\r
171 Status = IScsiReceiveLoginRsp (Conn);\r
172 if (EFI_ERROR (Status)) {\r
173 break;\r
174 }\r
175 } while (Conn->CurrentStage != ISCSI_FULL_FEATURE_PHASE);\r
176\r
177 return Status;\r
178}\r
179\r
180\r
181/**\r
182 Reset the iSCSI connection.\r
183\r
184 @param[in, out] Conn The iSCSI connection to reset.\r
185\r
186**/\r
187VOID\r
188IScsiConnReset (\r
189 IN OUT ISCSI_CONNECTION *Conn\r
190 )\r
191{\r
192 TcpIoReset (&Conn->TcpIo);\r
193}\r
194\r
195\r
196/**\r
197 Create a TCP connection for the iSCSI session.\r
198\r
199 @param[in] Session Points to the iSCSI session.\r
200\r
201 @return The newly created iSCSI connection.\r
202\r
203**/\r
204ISCSI_CONNECTION *\r
205IScsiCreateConnection (\r
206 IN ISCSI_SESSION *Session\r
207 )\r
208{\r
209 ISCSI_DRIVER_DATA *Private;\r
210 ISCSI_SESSION_CONFIG_NVDATA *NvData;\r
211 ISCSI_CONNECTION *Conn;\r
212 TCP_IO_CONFIG_DATA TcpIoConfig;\r
213 TCP4_IO_CONFIG_DATA *Tcp4IoConfig;\r
214 TCP6_IO_CONFIG_DATA *Tcp6IoConfig;\r
215 EFI_STATUS Status;\r
216\r
217 Private = Session->Private;\r
218 NvData = &Session->ConfigData->SessionConfigData;\r
219\r
220 Conn = AllocateZeroPool (sizeof (ISCSI_CONNECTION));\r
221 if (Conn == NULL) {\r
222 return NULL;\r
223 }\r
224\r
225 Conn->Signature = ISCSI_CONNECTION_SIGNATURE;\r
226 Conn->State = CONN_STATE_FREE;\r
227 Conn->CurrentStage = ISCSI_SECURITY_NEGOTIATION;\r
228 Conn->NextStage = ISCSI_LOGIN_OPERATIONAL_NEGOTIATION;\r
229 Conn->AuthStep = ISCSI_AUTH_INITIAL;\r
230 Conn->ExpStatSN = 0;\r
231 Conn->PartialReqSent = FALSE;\r
232 Conn->PartialRspRcvd = FALSE;\r
233 Conn->ParamNegotiated = FALSE;\r
234 Conn->Cid = Session->NextCid++;\r
0b10bb6f 235 Conn->Ipv6Flag = NvData->IpMode == IP_MODE_IP6 || Session->ConfigData->AutoConfigureMode == IP_MODE_AUTOCONFIG_IP6;\r
4c5a5e0c 236\r
237 Status = gBS->CreateEvent (\r
238 EVT_TIMER,\r
239 TPL_CALLBACK,\r
240 NULL,\r
241 NULL,\r
242 &Conn->TimeoutEvent\r
243 );\r
244 if (EFI_ERROR (Status)) {\r
245 FreePool (Conn);\r
246 return NULL;\r
247 }\r
248\r
249 NetbufQueInit (&Conn->RspQue);\r
250\r
251 //\r
252 // Set the default connection-only parameters.\r
253 //\r
254 Conn->MaxRecvDataSegmentLength = DEFAULT_MAX_RECV_DATA_SEG_LEN;\r
255 Conn->HeaderDigest = IScsiDigestNone;\r
256 Conn->DataDigest = IScsiDigestNone;\r
257\r
258 if (!Conn->Ipv6Flag) {\r
259 Tcp4IoConfig = &TcpIoConfig.Tcp4IoConfigData;\r
260 \r
261 CopyMem (&Tcp4IoConfig->LocalIp, &NvData->LocalIp, sizeof (EFI_IPv4_ADDRESS));\r
262 CopyMem (&Tcp4IoConfig->SubnetMask, &NvData->SubnetMask, sizeof (EFI_IPv4_ADDRESS));\r
263 CopyMem (&Tcp4IoConfig->Gateway, &NvData->Gateway, sizeof (EFI_IPv4_ADDRESS));\r
264 CopyMem (&Tcp4IoConfig->RemoteIp, &NvData->TargetIp, sizeof (EFI_IPv4_ADDRESS));\r
265\r
03b08d7a
YT
266 Tcp4IoConfig->RemotePort = NvData->TargetPort;\r
267 Tcp4IoConfig->ActiveFlag = TRUE;\r
268 Tcp4IoConfig->StationPort = 0;\r
4c5a5e0c 269 } else {\r
270 Tcp6IoConfig = &TcpIoConfig.Tcp6IoConfigData;\r
271 \r
272 CopyMem (&Tcp6IoConfig->RemoteIp, &NvData->TargetIp, sizeof (EFI_IPv6_ADDRESS));\r
03b08d7a
YT
273 Tcp6IoConfig->RemotePort = NvData->TargetPort;\r
274 Tcp6IoConfig->ActiveFlag = TRUE;\r
275 Tcp6IoConfig->StationPort = 0;\r
4c5a5e0c 276 }\r
277\r
278 //\r
279 // Create the TCP IO for this connection.\r
280 //\r
281 Status = TcpIoCreateSocket (\r
282 Private->Image,\r
283 Private->Controller,\r
284 (UINT8) (!Conn->Ipv6Flag ? TCP_VERSION_4: TCP_VERSION_6),\r
285 &TcpIoConfig,\r
286 &Conn->TcpIo\r
287 );\r
288 if (EFI_ERROR (Status)) {\r
289 gBS->CloseEvent (Conn->TimeoutEvent);\r
290 FreePool (Conn);\r
291 Conn = NULL;\r
292 }\r
293\r
294 return Conn;\r
295}\r
296\r
297\r
298/**\r
299 Destroy an iSCSI connection.\r
300\r
301 @param[in] Conn The connection to destroy.\r
302\r
303**/\r
304VOID\r
305IScsiDestroyConnection (\r
306 IN ISCSI_CONNECTION *Conn\r
307 )\r
308{\r
309 TcpIoDestroySocket (&Conn->TcpIo);\r
310\r
311 NetbufQueFlush (&Conn->RspQue);\r
312 gBS->CloseEvent (Conn->TimeoutEvent);\r
313 FreePool (Conn);\r
314}\r
315\r
75dce340 316/**\r
317 Retrieve the IPv6 Address/Prefix/Gateway from the established TCP connection, these informations\r
318 will be filled in the iSCSI Boot Firmware Table.\r
319\r
320 @param[in] Conn The connection used in the iSCSI login phase.\r
321\r
322 @retval EFI_SUCCESS Get the NIC information successfully.\r
323 @retval Others Other errors as indicated.\r
324 \r
325**/\r
326EFI_STATUS\r
327IScsiGetIp6NicInfo (\r
328 IN ISCSI_CONNECTION *Conn\r
329 )\r
330{\r
331 ISCSI_SESSION_CONFIG_NVDATA *NvData;\r
332 EFI_TCP6_PROTOCOL *Tcp6;\r
333 EFI_IP6_MODE_DATA Ip6ModeData;\r
334 EFI_STATUS Status;\r
335 EFI_IPv6_ADDRESS *TargetIp;\r
336 UINTN Index;\r
337 UINT8 SubnetPrefixLength;\r
338 UINTN RouteEntry;\r
339\r
340 NvData = &Conn->Session->ConfigData->SessionConfigData;\r
341 TargetIp = &NvData->TargetIp.v6;\r
342 Tcp6 = Conn->TcpIo.Tcp.Tcp6;\r
343\r
344 ZeroMem (&Ip6ModeData, sizeof (EFI_IP6_MODE_DATA));\r
345 Status = Tcp6->GetModeData (\r
346 Tcp6,\r
347 NULL,\r
348 NULL,\r
349 &Ip6ModeData,\r
350 NULL,\r
351 NULL\r
352 );\r
353 if (EFI_ERROR (Status)) {\r
354 return Status;\r
355 }\r
356\r
357 if (!Ip6ModeData.IsConfigured) {\r
358 Status = EFI_ABORTED;\r
359 goto ON_EXIT;\r
360 }\r
361\r
362 IP6_COPY_ADDRESS (&NvData->LocalIp, &Ip6ModeData.ConfigData.StationAddress);\r
363\r
364 NvData->PrefixLength = 0;\r
365 for (Index = 0; Index < Ip6ModeData.AddressCount; Index++) {\r
366 if (EFI_IP6_EQUAL (&NvData->LocalIp.v6, &Ip6ModeData.AddressList[Index].Address)) {\r
367 NvData->PrefixLength = Ip6ModeData.AddressList[Index].PrefixLength;\r
368 break;\r
369 }\r
370 }\r
371\r
372 SubnetPrefixLength = 0;\r
373 RouteEntry = Ip6ModeData.RouteCount;\r
374 for (Index = 0; Index < Ip6ModeData.RouteCount; Index++) {\r
375 if (NetIp6IsNetEqual (TargetIp, &Ip6ModeData.RouteTable[Index].Destination, Ip6ModeData.RouteTable[Index].PrefixLength)) {\r
376 if (SubnetPrefixLength < Ip6ModeData.RouteTable[Index].PrefixLength) {\r
377 SubnetPrefixLength = Ip6ModeData.RouteTable[Index].PrefixLength;\r
378 RouteEntry = Index;\r
379 }\r
380 }\r
381 }\r
382 if (RouteEntry != Ip6ModeData.RouteCount) {\r
383 IP6_COPY_ADDRESS (&NvData->Gateway, &Ip6ModeData.RouteTable[RouteEntry].Gateway);\r
384 }\r
385\r
386ON_EXIT:\r
387 if (Ip6ModeData.AddressList != NULL) {\r
388 FreePool (Ip6ModeData.AddressList);\r
389 }\r
390 if (Ip6ModeData.GroupTable!= NULL) {\r
391 FreePool (Ip6ModeData.GroupTable);\r
392 }\r
393 if (Ip6ModeData.RouteTable!= NULL) {\r
394 FreePool (Ip6ModeData.RouteTable);\r
395 }\r
396 if (Ip6ModeData.NeighborCache!= NULL) {\r
397 FreePool (Ip6ModeData.NeighborCache);\r
398 }\r
399 if (Ip6ModeData.PrefixTable!= NULL) {\r
400 FreePool (Ip6ModeData.PrefixTable);\r
401 }\r
402 if (Ip6ModeData.IcmpTypeList!= NULL) {\r
403 FreePool (Ip6ModeData.IcmpTypeList);\r
404 }\r
405\r
406 return Status;\r
407}\r
4c5a5e0c 408\r
409/**\r
410 Login the iSCSI session.\r
411\r
412 @param[in] Session The iSCSI session.\r
413\r
414 @retval EFI_SUCCESS The iSCSI session login procedure finished.\r
415 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
416 @retval EFI_NO_MEDIA There was a media error.\r
417 @retval Others Other errors as indicated.\r
418\r
419**/\r
420EFI_STATUS\r
421IScsiSessionLogin (\r
422 IN ISCSI_SESSION *Session\r
423 )\r
424{\r
425 EFI_STATUS Status;\r
426 ISCSI_CONNECTION *Conn;\r
427 VOID *Tcp;\r
428 EFI_GUID *ProtocolGuid;\r
429 UINT8 RetryCount;\r
430 BOOLEAN MediaPresent;\r
431\r
432 //\r
433 // Check media status before session login.\r
434 //\r
435 MediaPresent = TRUE;\r
436 NetLibDetectMedia (Session->Private->Controller, &MediaPresent);\r
437 if (!MediaPresent) {\r
438 return EFI_NO_MEDIA;\r
439 }\r
440\r
441 //\r
442 // Set session identifier\r
443 //\r
444 CopyMem (Session->Isid, Session->ConfigData->SessionConfigData.IsId, 6);\r
445\r
446 RetryCount = 0;\r
447\r
448 do {\r
449 //\r
450 // Create a connection for the session.\r
451 //\r
452 Conn = IScsiCreateConnection (Session);\r
453 if (Conn == NULL) {\r
454 return EFI_OUT_OF_RESOURCES;\r
455 }\r
456\r
457 IScsiAttatchConnection (Session, Conn);\r
458\r
459 //\r
460 // Login througth the newly created connection.\r
461 //\r
462 Status = IScsiConnLogin (Conn, Session->ConfigData->SessionConfigData.ConnectTimeout);\r
463 if (EFI_ERROR (Status)) {\r
464 IScsiConnReset (Conn);\r
465 IScsiDetatchConnection (Conn);\r
466 IScsiDestroyConnection (Conn);\r
467 }\r
468\r
469 if (Status != EFI_TIMEOUT) {\r
470 break;\r
471 }\r
472\r
473 RetryCount++;\r
474 } while (RetryCount <= Session->ConfigData->SessionConfigData.ConnectRetryCount);\r
475\r
476 if (!EFI_ERROR (Status)) {\r
477 Session->State = SESSION_STATE_LOGGED_IN;\r
478\r
0b10bb6f 479 if (!Conn->Ipv6Flag) {\r
4c5a5e0c 480 ProtocolGuid = &gEfiTcp4ProtocolGuid; \r
481 } else {\r
482 ProtocolGuid = &gEfiTcp6ProtocolGuid;\r
483 }\r
484\r
485 Status = gBS->OpenProtocol (\r
486 Conn->TcpIo.Handle,\r
487 ProtocolGuid,\r
488 (VOID **) &Tcp,\r
489 Session->Private->Image,\r
490 Session->Private->ExtScsiPassThruHandle,\r
491 EFI_OPEN_PROTOCOL_BY_CHILD_CONTROLLER \r
492 );\r
493\r
494 ASSERT_EFI_ERROR (Status);\r
75dce340 495\r
0b10bb6f 496 if (Conn->Ipv6Flag) {\r
75dce340 497 Status = IScsiGetIp6NicInfo (Conn);\r
498 }\r
4c5a5e0c 499 }\r
500\r
501 return Status;\r
502}\r
503\r
504\r
505/**\r
506 Wait for IPsec negotiation, then try to login the iSCSI session again.\r
507\r
508 @param[in] Session The iSCSI session.\r
509\r
510 @retval EFI_SUCCESS The iSCSI session login procedure finished.\r
511 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
512 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.\r
513\r
514**/\r
515EFI_STATUS\r
516IScsiSessionReLogin (\r
517 IN ISCSI_SESSION *Session\r
518 )\r
519{\r
520\r
521 EFI_STATUS Status;\r
522 EFI_STATUS TimerStatus;\r
523 EFI_EVENT Timer;\r
524\r
525 Status = gBS->CreateEvent (EVT_TIMER, TPL_CALLBACK, NULL, NULL, &Timer);\r
526 if (EFI_ERROR (Status)) {\r
527 return Status;\r
528 }\r
529\r
530 Status = gBS->SetTimer (\r
531 Timer,\r
532 TimerRelative,\r
533 ISCSI_WAIT_IPSEC_TIMEOUT\r
534 );\r
535\r
536 if (EFI_ERROR (Status)) {\r
537 gBS->CloseEvent (Timer);\r
538 return Status;\r
539 }\r
540\r
541 do {\r
542\r
543 TimerStatus = gBS->CheckEvent (Timer);\r
544\r
545 if (!EFI_ERROR (TimerStatus)) {\r
546 Status = IScsiSessionLogin (Session);\r
547 }\r
548\r
549 } while (TimerStatus == EFI_NOT_READY);\r
550\r
551 gBS->CloseEvent (Timer);\r
552 return Status;\r
553}\r
554\r
555\r
556/**\r
557 Build and send the iSCSI login request to the iSCSI target according to\r
558 the current login stage.\r
559\r
560 @param[in] Conn The connection in the iSCSI login phase.\r
561\r
562 @retval EFI_SUCCESS The iSCSI login request PDU is built and sent on this\r
563 connection.\r
564 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
565 @retval EFI_DEVICE_ERROR Some kind of device error occurred.\r
566\r
567**/\r
568EFI_STATUS\r
569IScsiSendLoginReq (\r
570 IN ISCSI_CONNECTION *Conn\r
571 )\r
572{\r
573 NET_BUF *Pdu;\r
574 EFI_STATUS Status;\r
575\r
576 //\r
577 // Build the Login Request PDU.\r
578 //\r
579 Pdu = IScsiPrepareLoginReq (Conn);\r
580 if (Pdu == NULL) {\r
581 return EFI_DEVICE_ERROR;\r
582 }\r
583 //\r
584 // Send it to the iSCSI target.\r
585 //\r
586 Status = TcpIoTransmit (&Conn->TcpIo, Pdu);\r
587\r
588 NetbufFree (Pdu);\r
589\r
590 return Status;\r
591}\r
592\r
593\r
594/**\r
595 Receive and process the iSCSI login response.\r
596\r
597 @param[in] Conn The connection in the iSCSI login phase.\r
598 \r
599 @retval EFI_SUCCESS The iSCSI login response PDU is received and processed.\r
600 @retval Others Other errors as indicated.\r
601\r
602**/\r
603EFI_STATUS\r
604IScsiReceiveLoginRsp (\r
605 IN ISCSI_CONNECTION *Conn\r
606 )\r
607{\r
608 EFI_STATUS Status;\r
609 NET_BUF *Pdu;\r
610\r
94866d40
ED
611 Pdu = NULL;\r
612\r
4c5a5e0c 613 //\r
614 // Receive the iSCSI login response.\r
615 //\r
616 Status = IScsiReceivePdu (Conn, &Pdu, NULL, FALSE, FALSE, NULL);\r
617 if (EFI_ERROR (Status)) {\r
618 return Status;\r
619 }\r
7a49cd08
ED
620 ASSERT (Pdu != NULL);\r
621\r
4c5a5e0c 622 //\r
623 // A Login Response is received; process it.\r
624 //\r
625 Status = IScsiProcessLoginRsp (Conn, Pdu);\r
626\r
627 NetbufFree (Pdu);\r
628\r
629 return Status;\r
630}\r
631\r
632\r
633/**\r
634 Add an iSCSI key-value pair as a string into the data segment of the Login Request PDU.\r
635 The DataSegmentLength and the actual size of the net buffer containing this PDU will be\r
636 updated.\r
637\r
638 @param[in, out] Pdu The iSCSI PDU whose data segment the key-value pair will\r
639 be added to.\r
640 @param[in] Key The key name string.\r
641 @param[in] Value The value string.\r
642\r
643 @retval EFI_SUCCESS The key-value pair is added to the PDU's data segment and\r
644 the correspondence length fields are updated.\r
645 @retval EFI_OUT_OF_RESOURCES There is not enough space in the PDU to add the key-value\r
646 pair.\r
7a49cd08 647 @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer.\r
4c5a5e0c 648**/\r
649EFI_STATUS\r
650IScsiAddKeyValuePair (\r
651 IN OUT NET_BUF *Pdu,\r
652 IN CHAR8 *Key,\r
653 IN CHAR8 *Value\r
654 )\r
655{\r
656 UINT32 DataSegLen;\r
657 UINT32 KeyLen;\r
658 UINT32 ValueLen;\r
659 UINT32 TotalLen;\r
660 ISCSI_LOGIN_REQUEST *LoginReq;\r
661 CHAR8 *Data;\r
662\r
663 LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufGetByte (Pdu, 0, NULL);\r
7a49cd08
ED
664 if (LoginReq == NULL) {\r
665 return EFI_PROTOCOL_ERROR;\r
666 }\r
4c5a5e0c 667 DataSegLen = NTOH24 (LoginReq->DataSegmentLength);\r
668\r
669 KeyLen = (UINT32) AsciiStrLen (Key);\r
670 ValueLen = (UINT32) AsciiStrLen (Value);\r
671\r
672 //\r
673 // 1 byte for the key value separator '=' and 1 byte for the null\r
674 // delimiter after the value.\r
675 //\r
676 TotalLen = KeyLen + 1 + ValueLen + 1;\r
677\r
678 //\r
679 // Allocate the space for the key-value pair.\r
680 //\r
681 Data = (CHAR8 *) NetbufAllocSpace (Pdu, TotalLen, NET_BUF_TAIL);\r
682 if (Data == NULL) {\r
683 return EFI_OUT_OF_RESOURCES;\r
684 }\r
685 //\r
686 // Add the key.\r
687 //\r
688 CopyMem (Data, Key, KeyLen);\r
689 Data += KeyLen;\r
690\r
691 *Data = '=';\r
692 Data++;\r
693\r
694 //\r
695 // Add the value.\r
696 //\r
697 CopyMem (Data, Value, ValueLen);\r
698 Data += ValueLen;\r
699\r
700 *Data = '\0';\r
701\r
702 //\r
703 // Update the DataSegmentLength\r
704 //\r
705 ISCSI_SET_DATASEG_LEN (LoginReq, DataSegLen + TotalLen);\r
706\r
707 return EFI_SUCCESS;\r
708}\r
709\r
710\r
711/**\r
712 Prepare the iSCSI login request to be sent according to the current login status.\r
713\r
714 @param[in, out] Conn The connection in the iSCSI login phase.\r
715\r
716 @return The pointer to the net buffer containing the iSCSI login request built.\r
717 @retval NULL Other errors as indicated.\r
718\r
719**/\r
720NET_BUF *\r
721IScsiPrepareLoginReq (\r
722 IN OUT ISCSI_CONNECTION *Conn\r
723 )\r
724{\r
725 ISCSI_SESSION *Session;\r
726 NET_BUF *Nbuf;\r
727 ISCSI_LOGIN_REQUEST *LoginReq;\r
728 EFI_STATUS Status;\r
729\r
730 Session = Conn->Session;\r
731\r
732 Nbuf = NetbufAlloc (sizeof (ISCSI_LOGIN_REQUEST) + DEFAULT_MAX_RECV_DATA_SEG_LEN);\r
733 if (Nbuf == NULL) {\r
734 return NULL;\r
735 }\r
736\r
737 LoginReq = (ISCSI_LOGIN_REQUEST *) NetbufAllocSpace (Nbuf, sizeof (ISCSI_LOGIN_REQUEST), NET_BUF_TAIL);\r
7c275b3c
ZL
738 if (LoginReq == NULL) {\r
739 NetbufFree (Nbuf);\r
740 return NULL;\r
741 }\r
4c5a5e0c 742 ZeroMem (LoginReq, sizeof (ISCSI_LOGIN_REQUEST));\r
743\r
744 //\r
745 // Init the login request pdu\r
746 //\r
747 ISCSI_SET_OPCODE (LoginReq, ISCSI_OPCODE_LOGIN_REQ, ISCSI_REQ_IMMEDIATE);\r
748 ISCSI_SET_STAGES (LoginReq, Conn->CurrentStage, Conn->NextStage);\r
749 LoginReq->VersionMax = ISCSI_VERSION_MAX;\r
750 LoginReq->VersionMin = ISCSI_VERSION_MIN;\r
751 LoginReq->Tsih = HTONS (Session->Tsih);\r
752 LoginReq->InitiatorTaskTag = HTONL (Session->InitiatorTaskTag);\r
753 LoginReq->Cid = HTONS (Conn->Cid);\r
754 LoginReq->CmdSN = HTONL (Session->CmdSN);\r
755\r
756 //\r
757 // For the first Login Request on a coonection this is ExpStatSN for the\r
758 // old connection, and this field is only valid if the Login Request restarts\r
759 // a connection.\r
760 // For subsequent Login Requests it is used to acknowledge the Login Responses\r
761 // with their increasing StatSN values.\r
762 //\r
763 LoginReq->ExpStatSN = HTONL (Conn->ExpStatSN);\r
764 CopyMem (LoginReq->Isid, Session->Isid, sizeof (LoginReq->Isid));\r
765\r
766 if (Conn->PartialRspRcvd) {\r
767 //\r
768 // A partial response. The initiator must send an empty Login Request.\r
769 //\r
770 return Nbuf;\r
771 }\r
772\r
773 Status = EFI_SUCCESS;\r
774\r
775 switch (Conn->CurrentStage) {\r
776 case ISCSI_SECURITY_NEGOTIATION:\r
777 //\r
778 // Both none authentication and CHAP authentication share the CHAP path.\r
779 // \r
780 //\r
781 if (Session->AuthType != ISCSI_AUTH_TYPE_KRB) {\r
782 Status = IScsiCHAPToSendReq (Conn, Nbuf);\r
783 }\r
784\r
785 break;\r
786\r
787 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION:\r
788 //\r
d1c85a17 789 // Only negotiate the parameter once.\r
4c5a5e0c 790 //\r
791 if (!Conn->ParamNegotiated) {\r
792 IScsiFillOpParams (Conn, Nbuf);\r
793 }\r
794 \r
795 ISCSI_SET_FLAG (LoginReq, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT);\r
796 break;\r
797\r
798 default:\r
799 //\r
800 // An error occurs...\r
801 //\r
802 Status = EFI_DEVICE_ERROR;\r
803 break;\r
804 }\r
805\r
806 if (EFI_ERROR (Status)) {\r
807 NetbufFree (Nbuf);\r
808 Nbuf = NULL;\r
809 } else {\r
810 //\r
811 // Pad the data segment if needed.\r
812 //\r
813 IScsiPadSegment (Nbuf, ISCSI_GET_DATASEG_LEN (LoginReq));\r
814 //\r
815 // Check whether we will issue the stage transition signal?\r
816 //\r
817 Conn->TransitInitiated = ISCSI_FLAG_ON (LoginReq, ISCSI_LOGIN_REQ_PDU_FLAG_TRANSIT);\r
818 }\r
819\r
820 return Nbuf;\r
821}\r
822\r
823\r
824/**\r
825 Process the iSCSI Login Response.\r
826\r
827 @param[in, out] Conn The connection on which the iSCSI login response is received.\r
828 @param[in, out] Pdu The iSCSI login response PDU.\r
829\r
830 @retval EFI_SUCCESS The iSCSI login response PDU is processed, and all checks are passed.\r
831 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.\r
832 @retval EFI_MEDIA_CHANGED Target is redirected.\r
833 @retval Others Other errors as indicated.\r
834\r
835**/\r
836EFI_STATUS\r
837IScsiProcessLoginRsp (\r
838 IN OUT ISCSI_CONNECTION *Conn,\r
839 IN OUT NET_BUF *Pdu\r
840 )\r
841{\r
842 EFI_STATUS Status;\r
843 ISCSI_SESSION *Session;\r
844 ISCSI_LOGIN_RESPONSE *LoginRsp;\r
845 BOOLEAN Transit;\r
846 BOOLEAN Continue;\r
847 UINT8 CurrentStage;\r
848 UINT8 NextStage;\r
849 UINT8 *DataSeg;\r
850 UINT32 DataSegLen;\r
851\r
852 Status = EFI_SUCCESS;\r
853 Session = Conn->Session;\r
854\r
855 LoginRsp = (ISCSI_LOGIN_RESPONSE *) NetbufGetByte (Pdu, 0, NULL);\r
7a49cd08
ED
856 if (LoginRsp == NULL) {\r
857 return EFI_PROTOCOL_ERROR;\r
858 }\r
4c5a5e0c 859 if (!ISCSI_CHECK_OPCODE (LoginRsp, ISCSI_OPCODE_LOGIN_RSP)) {\r
860 //\r
861 // It is not a Login Response.\r
862 //\r
863 return EFI_PROTOCOL_ERROR;\r
864 }\r
865 //\r
866 // Get the data segment, if any.\r
867 //\r
868 DataSegLen = ISCSI_GET_DATASEG_LEN (LoginRsp);\r
869 if (DataSegLen != 0) {\r
870 DataSeg = NetbufGetByte (Pdu, sizeof (ISCSI_LOGIN_RESPONSE), NULL);\r
871 } else {\r
872 DataSeg = NULL;\r
873 }\r
874 //\r
875 // Check the status class in the login response PDU.\r
876 //\r
877 switch (LoginRsp->StatusClass) {\r
878 case ISCSI_LOGIN_STATUS_SUCCESS:\r
879 //\r
880 // Just break here; the response and the data segment will be processed later.\r
881 //\r
882 break;\r
883\r
884 case ISCSI_LOGIN_STATUS_REDIRECTION:\r
885 //\r
886 // The target may be moved to a different address.\r
887 //\r
888 if (DataSeg == NULL) {\r
889 return EFI_PROTOCOL_ERROR;\r
890 }\r
891 //\r
892 // Process the TargetAddress key-value strings in the data segment to update the\r
893 // target address info.\r
894 //\r
895 Status = IScsiUpdateTargetAddress (Session, (CHAR8 *) DataSeg, DataSegLen);\r
896 if (EFI_ERROR (Status)) {\r
897 return Status;\r
898 }\r
899 //\r
900 // Session will be restarted on this error status because the Target is\r
901 // redirected by this Login Response.\r
902 //\r
903 return EFI_MEDIA_CHANGED;\r
904\r
905 default:\r
906 //\r
907 // Initiator Error, Target Error, or any other undefined error code.\r
908 //\r
909 return EFI_PROTOCOL_ERROR;\r
910 }\r
911 //\r
912 // The status is success; extract the wanted fields from the header segment.\r
913 //\r
914 Transit = ISCSI_FLAG_ON (LoginRsp, ISCSI_LOGIN_RSP_PDU_FLAG_TRANSIT);\r
915 Continue = ISCSI_FLAG_ON (LoginRsp, ISCSI_LOGIN_RSP_PDU_FLAG_CONTINUE);\r
916\r
917 CurrentStage = ISCSI_GET_CURRENT_STAGE (LoginRsp);\r
918 NextStage = ISCSI_GET_NEXT_STAGE (LoginRsp);\r
919\r
920 LoginRsp->InitiatorTaskTag = NTOHL (LoginRsp->InitiatorTaskTag);\r
921\r
922 if ((Transit && Continue) ||\r
923 (CurrentStage != Conn->CurrentStage) ||\r
924 (!Conn->TransitInitiated && Transit) ||\r
925 (Transit && (NextStage != Conn->NextStage)) ||\r
926 (CompareMem (Session->Isid, LoginRsp->Isid, sizeof (LoginRsp->Isid)) != 0) ||\r
927 (LoginRsp->InitiatorTaskTag != Session->InitiatorTaskTag)\r
928 ) {\r
929 //\r
930 // A Login Response with the C bit set to 1 MUST have the T bit set to 0.\r
931 // The CSG in the Login Response MUST be the same with the I-end of this connection.\r
932 // The T bit can't be 1 if the last Login Response sent by the initiator doesn't\r
933 // initiate the transistion.\r
934 // The NSG MUST be the same with the I-end of this connection if Transit is required.\r
935 // The ISID in the Login Response MUST be the same with this session.\r
936 //\r
937 return EFI_PROTOCOL_ERROR;\r
938 }\r
939\r
940 LoginRsp->StatSN = NTOHL (LoginRsp->StatSN);\r
941 LoginRsp->ExpCmdSN = NTOHL (LoginRsp->ExpCmdSN);\r
942 LoginRsp->MaxCmdSN = NTOHL (LoginRsp->MaxCmdSN);\r
943\r
944 if ((Conn->CurrentStage == ISCSI_SECURITY_NEGOTIATION) && (Conn->AuthStep == ISCSI_AUTH_INITIAL)) {\r
945 //\r
946 // If the Login Request is a leading Login Request, the target MUST use\r
947 // the value presented in CmdSN as the target value for ExpCmdSN.\r
948 //\r
949 if ((Session->State == SESSION_STATE_FREE) && (Session->CmdSN != LoginRsp->ExpCmdSN)) {\r
950 return EFI_PROTOCOL_ERROR; \r
951 }\r
952\r
953 //\r
954 // It's the initial Login Response, initialize the local ExpStatSN, MaxCmdSN\r
955 // and ExpCmdSN.\r
956 //\r
957 Conn->ExpStatSN = LoginRsp->StatSN + 1;\r
958 Session->MaxCmdSN = LoginRsp->MaxCmdSN;\r
959 Session->ExpCmdSN = LoginRsp->ExpCmdSN;\r
960 } else {\r
961 //\r
962 // Check the StatSN of this PDU.\r
963 //\r
964 Status = IScsiCheckSN (&Conn->ExpStatSN, LoginRsp->StatSN);\r
965 if (!EFI_ERROR (Status)) {\r
966 //\r
967 // Update the MaxCmdSN and ExpCmdSN.\r
968 //\r
969 IScsiUpdateCmdSN (Session, LoginRsp->MaxCmdSN, LoginRsp->ExpCmdSN);\r
970 } else {\r
971 return Status;\r
972 }\r
973 }\r
974 //\r
975 // Trim off the header segment.\r
976 //\r
977 NetbufTrim (Pdu, sizeof (ISCSI_LOGIN_RESPONSE), NET_BUF_HEAD);\r
978\r
979 //\r
980 // Queue this login response first in case it's a partial response so that\r
981 // later when the full response list is received we can combine these scattered\r
982 // responses' data segment and then process it.\r
983 //\r
984 NET_GET_REF (Pdu);\r
985 NetbufQueAppend (&Conn->RspQue, Pdu);\r
986\r
987 Conn->PartialRspRcvd = Continue;\r
6c657d74 988 if (Continue) {\r
4c5a5e0c 989 //\r
990 // It is a partial response; must wait for another or more Request/Response\r
991 // conversations to get the full response.\r
992 //\r
993 return EFI_SUCCESS;\r
994 }\r
995\r
996 switch (CurrentStage) {\r
997 case ISCSI_SECURITY_NEGOTIATION:\r
998 //\r
999 // In security negotiation stage, let CHAP module handle it.\r
1000 //\r
1001 if (Session->AuthType != ISCSI_AUTH_TYPE_KRB) {\r
1002 Status = IScsiCHAPOnRspReceived (Conn);\r
1003 }\r
1004 break;\r
1005\r
1006 case ISCSI_LOGIN_OPERATIONAL_NEGOTIATION:\r
1007 //\r
1008 // Response received with negotiation response on iSCSI parameters: check them.\r
1009 //\r
1010 Status = IScsiCheckOpParams (Conn);\r
1011 if (!EFI_ERROR (Status)) {\r
1012 Conn->ParamNegotiated = TRUE;\r
1013 }\r
1014\r
1015 break;\r
1016\r
1017 default:\r
1018 //\r
1019 // Should never get here.\r
1020 //\r
1021 Status = EFI_PROTOCOL_ERROR;\r
1022 break;\r
1023 }\r
1024\r
1025 if (Transit && (Status == EFI_SUCCESS)) {\r
1026 //\r
1027 // Do the state transition.\r
1028 //\r
1029 Conn->CurrentStage = Conn->NextStage;\r
1030\r
1031 if (Conn->CurrentStage == ISCSI_LOGIN_OPERATIONAL_NEGOTIATION) {\r
1032 Conn->NextStage = ISCSI_FULL_FEATURE_PHASE;\r
1033 } else {\r
1034 //\r
1035 // CurrentStage is iSCSI Full Feature. It is the Login-Final Response;\r
1036 // get the TSIH from the Login Response.\r
1037 //\r
1038 Session->Tsih = NTOHS (LoginRsp->Tsih);\r
1039 }\r
1040 }\r
1041 //\r
1042 // Flush the response(s) received.\r
1043 //\r
1044 NetbufQueFlush (&Conn->RspQue);\r
1045\r
1046 return Status;\r
1047}\r
1048\r
1049\r
1050/**\r
1051 Updated the target information according the data received in the iSCSI\r
1052 login response with an target redirection status.\r
1053\r
1054 @param[in, out] Session The iSCSI session.\r
1055 @param[in] Data The data segment that should contain the\r
1056 TargetAddress key-value list.\r
1057 @param[in] Len Length of the data.\r
1058 \r
1059 @retval EFI_SUCCESS The target address is updated.\r
1060 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
1061 @retval EFI_NOT_FOUND The TargetAddress key is not found.\r
1062 @retval Others Other errors as indicated.\r
1063\r
1064**/\r
1065EFI_STATUS\r
1066IScsiUpdateTargetAddress (\r
1067 IN OUT ISCSI_SESSION *Session,\r
1068 IN CHAR8 *Data,\r
1069 IN UINT32 Len\r
1070 )\r
1071{\r
a2d59ef2
YT
1072 LIST_ENTRY *KeyValueList;\r
1073 CHAR8 *TargetAddress;\r
1074 CHAR8 *IpStr;\r
1075 EFI_STATUS Status;\r
1076 UINTN Number;\r
1077 UINT8 IpMode;\r
1078 ISCSI_SESSION_CONFIG_NVDATA *NvData;\r
4c5a5e0c 1079\r
1080 KeyValueList = IScsiBuildKeyValueList (Data, Len);\r
1081 if (KeyValueList == NULL) {\r
1082 return EFI_OUT_OF_RESOURCES;\r
1083 }\r
1084\r
1085 Status = EFI_NOT_FOUND;\r
a2d59ef2
YT
1086 NvData = &Session->ConfigData->SessionConfigData;\r
1087 \r
4c5a5e0c 1088 while (TRUE) {\r
1089 TargetAddress = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_ADDRESS);\r
1090 if (TargetAddress == NULL) {\r
1091 break;\r
1092 }\r
1093\r
9c3dcd0f
ZL
1094 //\r
1095 // RFC 3720 defines format of the TargetAddress=domainname[:port][,portal-group-tag]\r
1096 // The domainname can be specified as either a DNS host name, adotted-decimal IPv4 address,\r
1097 // or a bracketed IPv6 address as specified in [RFC2732].\r
1098 //\r
1099 if (NET_IS_DIGIT (TargetAddress[0])) {\r
1100 //\r
1101 // The domainname of the target is presented in a dotted-decimal IPv4 address format.\r
1102 //\r
1103 IpStr = TargetAddress;\r
1104\r
1105 while ((*TargetAddress != '\0') && (*TargetAddress != ':') && (*TargetAddress != ',')) {\r
1106 //\r
1107 // NULL, ':', or ',' ends the IPv4 string.\r
1108 //\r
1109 TargetAddress++;\r
1110 }\r
1111 } else if (*TargetAddress == ISCSI_REDIRECT_ADDR_START_DELIMITER){\r
4c5a5e0c 1112 //\r
9c3dcd0f 1113 // The domainname of the target is presented in a bracketed IPv6 address format.\r
4c5a5e0c 1114 //\r
9c3dcd0f
ZL
1115 TargetAddress ++;\r
1116 IpStr = TargetAddress;\r
1117 while ((*TargetAddress != '\0') && (*TargetAddress != ISCSI_REDIRECT_ADDR_END_DELIMITER)) {\r
1118 //\r
1119 // ']' ends the IPv6 string.\r
1120 //\r
1121 TargetAddress++;\r
1122 }\r
1123\r
1124 if (*TargetAddress != ISCSI_REDIRECT_ADDR_END_DELIMITER) {\r
1125 continue;\r
1126 }\r
1127\r
1128 *TargetAddress = '\0';\r
1129 TargetAddress ++;\r
1130\r
1131 } else {\r
1132 //\r
1133 // The domainname of the target is presented in the format of a DNS host name.\r
1134 // Temporary not supported.\r
4c5a5e0c 1135 continue;\r
1136 }\r
1137\r
a2d59ef2
YT
1138 //\r
1139 // Save the origial user setting which specifies the proxy/virtual iSCSI target.\r
1140 //\r
1141 NvData->OriginalTargetPort = NvData->TargetPort;\r
1142\r
4c5a5e0c 1143 if (*TargetAddress == ',') {\r
1144 //\r
1145 // Comma and the portal group tag MUST be ommitted if the TargetAddress is sent\r
1146 // as the result of a redirection.\r
1147 //\r
1148 continue;\r
1149 } else if (*TargetAddress == ':') {\r
1150 *TargetAddress = '\0';\r
1151\r
1152 TargetAddress++;\r
1153\r
1154 Number = AsciiStrDecimalToUintn (TargetAddress);\r
1155 if (Number > 0xFFFF) {\r
1156 continue;\r
1157 } else {\r
a2d59ef2 1158 NvData->TargetPort = (UINT16) Number;\r
4c5a5e0c 1159 }\r
1160 } else {\r
1161 //\r
9c3dcd0f 1162 // The string only contains the Target address. Use the well-known port.\r
4c5a5e0c 1163 //\r
a2d59ef2 1164 NvData->TargetPort = ISCSI_WELL_KNOWN_PORT;\r
4c5a5e0c 1165 }\r
a2d59ef2
YT
1166\r
1167 //\r
1168 // Save the origial user setting which specifies the proxy/virtual iSCSI target.\r
1169 // \r
1170 CopyMem (&NvData->OriginalTargetIp, &NvData->TargetIp, sizeof (EFI_IP_ADDRESS));\r
1171\r
4c5a5e0c 1172 //\r
1173 // Update the target IP address.\r
1174 //\r
a2d59ef2
YT
1175 if (NvData->IpMode < IP_MODE_AUTOCONFIG) {\r
1176 IpMode = NvData->IpMode;\r
4c5a5e0c 1177 } else {\r
1178 IpMode = Session->ConfigData->AutoConfigureMode;\r
1179 }\r
1180\r
1181 Status = IScsiAsciiStrToIp (\r
1182 IpStr,\r
1183 IpMode,\r
1184 &Session->ConfigData->SessionConfigData.TargetIp\r
1185 );\r
1186\r
1187 if (EFI_ERROR (Status)) {\r
1188 continue;\r
1189 } else {\r
a2d59ef2 1190 NvData->RedirectFlag = TRUE;\r
4c5a5e0c 1191 break;\r
1192 }\r
1193 }\r
1194\r
1195 IScsiFreeKeyValueList (KeyValueList);\r
1196\r
1197 return Status;\r
1198}\r
1199\r
1200\r
1201/**\r
1202 The callback function to free the net buffer list.\r
1203\r
1204 @param[in] Arg The opaque parameter.\r
1205\r
1206**/\r
1207VOID\r
1208EFIAPI\r
1209IScsiFreeNbufList (\r
1210 VOID *Arg\r
1211 )\r
1212{\r
1213 ASSERT (Arg != NULL);\r
1214\r
1215 NetbufFreeList ((LIST_ENTRY *) Arg);\r
1216 FreePool (Arg);\r
1217}\r
1218\r
1219\r
1220/**\r
1221 The callback function called in NetBufFree; it does nothing.\r
1222\r
1223 @param[in] Arg The opaque parameter.\r
1224\r
1225**/\r
1226VOID\r
1227EFIAPI\r
1228IScsiNbufExtFree (\r
1229 VOID *Arg\r
1230 )\r
1231{\r
1232}\r
1233\r
1234\r
1235/**\r
1236 Receive an iSCSI response PDU. An iSCSI response PDU contains an iSCSI PDU header and\r
1237 an optional data segment. The two parts will be put into two blocks of buffers in the\r
1238 net buffer. The digest check will be conducted in this function if needed and the digests\r
1239 will be trimmed from the PDU buffer.\r
1240\r
1241 @param[in] Conn The iSCSI connection to receive data from.\r
1242 @param[out] Pdu The received iSCSI pdu.\r
1243 @param[in] Context The context used to describe information on the caller provided\r
1244 buffer to receive data segment of the iSCSI pdu. It is optional.\r
1245 @param[in] HeaderDigest Whether there will be header digest received.\r
1246 @param[in] DataDigest Whether there will be data digest.\r
1247 @param[in] TimeoutEvent The timeout event. It is optional.\r
1248\r
1249 @retval EFI_SUCCESS An iSCSI pdu is received.\r
1250 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
1251 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.\r
1252 @retval Others Other errors as indicated.\r
1253\r
1254**/\r
1255EFI_STATUS\r
1256IScsiReceivePdu (\r
1257 IN ISCSI_CONNECTION *Conn,\r
1258 OUT NET_BUF **Pdu,\r
1259 IN ISCSI_IN_BUFFER_CONTEXT *Context, OPTIONAL\r
1260 IN BOOLEAN HeaderDigest,\r
1261 IN BOOLEAN DataDigest,\r
1262 IN EFI_EVENT TimeoutEvent OPTIONAL\r
1263 )\r
1264{\r
1265 LIST_ENTRY *NbufList;\r
1266 UINT32 Len;\r
1267 NET_BUF *PduHdr;\r
1268 UINT8 *Header;\r
1269 EFI_STATUS Status;\r
1270 UINT32 PadLen;\r
1271 UINT32 InDataOffset;\r
1272 NET_FRAGMENT Fragment[2];\r
1273 UINT32 FragmentCount;\r
1274 NET_BUF *DataSeg;\r
1275 UINT32 PadAndCRC32[2];\r
1276\r
1277 NbufList = AllocatePool (sizeof (LIST_ENTRY));\r
1278 if (NbufList == NULL) {\r
1279 return EFI_OUT_OF_RESOURCES;\r
1280 }\r
1281\r
1282 InitializeListHead (NbufList);\r
1283\r
1284 //\r
1285 // The header digest will be received together with the PDU header, if exists.\r
1286 //\r
1287 Len = sizeof (ISCSI_BASIC_HEADER) + (HeaderDigest ? sizeof (UINT32) : 0);\r
1288 PduHdr = NetbufAlloc (Len);\r
1289 if (PduHdr == NULL) {\r
1290 Status = EFI_OUT_OF_RESOURCES;\r
1291 goto ON_EXIT;\r
1292 }\r
1293\r
1294 Header = NetbufAllocSpace (PduHdr, Len, NET_BUF_TAIL);\r
7c275b3c
ZL
1295 if (Header == NULL) {\r
1296 Status = EFI_OUT_OF_RESOURCES;\r
1297 goto ON_EXIT;\r
1298 }\r
4c5a5e0c 1299 InsertTailList (NbufList, &PduHdr->List);\r
1300\r
1301 //\r
1302 // First step, receive the BHS of the PDU.\r
1303 //\r
1304 Status = TcpIoReceive (&Conn->TcpIo, PduHdr, FALSE, TimeoutEvent);\r
1305\r
1306 if (EFI_ERROR (Status)) {\r
1307 goto ON_EXIT;\r
1308 }\r
1309\r
1310 if (HeaderDigest) {\r
1311 //\r
1312 // TODO: check the header-digest.\r
1313 //\r
1314 //\r
1315 // Trim off the digest.\r
1316 //\r
1317 NetbufTrim (PduHdr, sizeof (UINT32), NET_BUF_TAIL);\r
1318 }\r
1319\r
1320 Len = ISCSI_GET_DATASEG_LEN (Header);\r
1321 if (Len == 0) {\r
1322 //\r
1323 // No data segment.\r
1324 //\r
1325 goto FORM_PDU;\r
1326 }\r
1327 //\r
1328 // Get the length of the padding bytes of the data segment.\r
1329 //\r
1330 PadLen = ISCSI_GET_PAD_LEN (Len);\r
1331\r
1332 switch (ISCSI_GET_OPCODE (Header)) {\r
1333 case ISCSI_OPCODE_SCSI_DATA_IN:\r
1334 //\r
1335 // To reduce memory copy overhead, try to use the buffer described by Context \r
1336 // if the PDU is an iSCSI SCSI data.\r
1337 //\r
1338 InDataOffset = ISCSI_GET_BUFFER_OFFSET (Header);\r
1339 if ((Context == NULL) || ((InDataOffset + Len) > Context->InDataLen)) {\r
1340 Status = EFI_PROTOCOL_ERROR;\r
1341 goto ON_EXIT;\r
1342 }\r
1343\r
1344 Fragment[0].Len = Len;\r
1345 Fragment[0].Bulk = Context->InData + InDataOffset;\r
1346\r
1347 if (DataDigest || (PadLen != 0)) {\r
1348 //\r
1349 // The data segment is padded. Use two fragments to receive it:\r
1350 // the first to receive the useful data; the second to receive the padding.\r
1351 //\r
1352 Fragment[1].Len = PadLen + (DataDigest ? sizeof (UINT32) : 0);\r
1353 Fragment[1].Bulk = (UINT8 *)PadAndCRC32 + (4 - PadLen);\r
1354\r
1355 FragmentCount = 2;\r
1356 } else {\r
1357 FragmentCount = 1;\r
1358 }\r
1359\r
1360 DataSeg = NetbufFromExt (&Fragment[0], FragmentCount, 0, 0, IScsiNbufExtFree, NULL);\r
1361 if (DataSeg == NULL) {\r
1362 Status = EFI_OUT_OF_RESOURCES;\r
1363 goto ON_EXIT;\r
1364 }\r
1365\r
1366 break;\r
1367\r
1368 case ISCSI_OPCODE_SCSI_RSP:\r
1369 case ISCSI_OPCODE_NOP_IN:\r
1370 case ISCSI_OPCODE_LOGIN_RSP:\r
1371 case ISCSI_OPCODE_TEXT_RSP:\r
1372 case ISCSI_OPCODE_ASYNC_MSG:\r
1373 case ISCSI_OPCODE_REJECT:\r
1374 case ISCSI_OPCODE_VENDOR_T0:\r
1375 case ISCSI_OPCODE_VENDOR_T1:\r
1376 case ISCSI_OPCODE_VENDOR_T2:\r
1377 //\r
1378 // Allocate buffer to receive the data segment.\r
1379 //\r
1380 Len += PadLen + (DataDigest ? sizeof (UINT32) : 0);\r
1381 DataSeg = NetbufAlloc (Len);\r
1382 if (DataSeg == NULL) {\r
1383 Status = EFI_OUT_OF_RESOURCES;\r
1384 goto ON_EXIT;\r
1385 }\r
1386\r
1387 NetbufAllocSpace (DataSeg, Len, NET_BUF_TAIL);\r
1388 break;\r
1389\r
1390 default:\r
1391 Status = EFI_PROTOCOL_ERROR;\r
1392 goto ON_EXIT;\r
1393 }\r
1394\r
1395 InsertTailList (NbufList, &DataSeg->List);\r
1396\r
1397 //\r
1398 // Receive the data segment with the data digest, if any.\r
1399 //\r
1400 Status = TcpIoReceive (&Conn->TcpIo, DataSeg, FALSE, TimeoutEvent);\r
1401\r
1402 if (EFI_ERROR (Status)) {\r
1403 goto ON_EXIT;\r
1404 }\r
1405\r
1406 if (DataDigest) {\r
1407 //\r
1408 // TODO: Check the data digest.\r
1409 //\r
1410 NetbufTrim (DataSeg, sizeof (UINT32), NET_BUF_TAIL);\r
1411 }\r
1412\r
1413 if (PadLen != 0) {\r
1414 //\r
1415 // Trim off the padding bytes in the data segment.\r
1416 //\r
1417 NetbufTrim (DataSeg, PadLen, NET_BUF_TAIL);\r
1418 }\r
1419\r
1420FORM_PDU:\r
1421 //\r
1422 // Form the pdu from a list of pdu segments.\r
1423 //\r
1424 *Pdu = NetbufFromBufList (NbufList, 0, 0, IScsiFreeNbufList, NbufList);\r
1425 if (*Pdu == NULL) {\r
1426 Status = EFI_OUT_OF_RESOURCES;\r
1427 }\r
1428\r
1429ON_EXIT:\r
1430\r
1431 if (EFI_ERROR (Status)) {\r
1432 //\r
1433 // Free the Nbufs in this NbufList and the NbufList itself.\r
1434 //\r
1435 IScsiFreeNbufList (NbufList);\r
1436 }\r
1437\r
1438 return Status;\r
1439}\r
1440\r
1441\r
1442/**\r
75acfab6 1443 Check and get the result of the parameter negotiation.\r
4c5a5e0c 1444\r
1445 @param[in, out] Conn The connection in iSCSI login.\r
1446\r
1447 @retval EFI_SUCCESS The parmeter check is passed and negotiation is finished.\r
1448 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol error occurred.\r
1449 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
1450\r
1451**/\r
1452EFI_STATUS\r
1453IScsiCheckOpParams (\r
1454 IN OUT ISCSI_CONNECTION *Conn\r
1455 )\r
1456{\r
1457 EFI_STATUS Status;\r
1458 LIST_ENTRY *KeyValueList;\r
1459 CHAR8 *Data;\r
1460 UINT32 Len;\r
1461 ISCSI_SESSION *Session;\r
1462 CHAR8 *Value;\r
1463 UINTN NumericValue;\r
1464\r
1465 ASSERT (Conn->RspQue.BufNum != 0);\r
1466\r
1467 Session = Conn->Session;\r
1468\r
1469 Len = Conn->RspQue.BufSize;\r
1470 Data = AllocatePool (Len);\r
1471 if (Data == NULL) {\r
1472 return EFI_OUT_OF_RESOURCES;\r
1473 }\r
1474\r
1475 NetbufQueCopy (&Conn->RspQue, 0, Len, (UINT8 *) Data);\r
1476\r
1477 Status = EFI_PROTOCOL_ERROR;\r
1478\r
1479 //\r
1480 // Extract the Key-Value pairs into a list.\r
1481 //\r
1482 KeyValueList = IScsiBuildKeyValueList (Data, Len);\r
1483 if (KeyValueList == NULL) {\r
1484 FreePool (Data);\r
1485 return Status;\r
1486 }\r
1487 //\r
1488 // HeaderDigest\r
1489 //\r
1490 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_HEADER_DIGEST);\r
1491 if (Value == NULL) {\r
1492 goto ON_ERROR;\r
1493 }\r
1494\r
1495 if (AsciiStrCmp (Value, "CRC32") == 0) {\r
1496 if (Conn->HeaderDigest != IScsiDigestCRC32) {\r
1497 goto ON_ERROR;\r
1498 }\r
1499 } else if (AsciiStrCmp (Value, ISCSI_KEY_VALUE_NONE) == 0) {\r
1500 Conn->HeaderDigest = IScsiDigestNone;\r
1501 } else {\r
1502 goto ON_ERROR;\r
1503 }\r
1504 //\r
1505 // DataDigest\r
1506 //\r
1507 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_DIGEST);\r
1508 if (Value == NULL) {\r
1509 goto ON_ERROR;\r
1510 }\r
1511\r
1512 if (AsciiStrCmp (Value, "CRC32") == 0) {\r
1513 if (Conn->DataDigest != IScsiDigestCRC32) {\r
1514 goto ON_ERROR;\r
1515 }\r
1516 } else if (AsciiStrCmp (Value, ISCSI_KEY_VALUE_NONE) == 0) {\r
1517 Conn->DataDigest = IScsiDigestNone;\r
1518 } else {\r
1519 goto ON_ERROR;\r
1520 }\r
1521 //\r
1522 // ErrorRecoveryLevel: result fuction is Minimum.\r
1523 //\r
1524 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_ERROR_RECOVERY_LEVEL);\r
1525 if (Value == NULL) {\r
1526 goto ON_ERROR;\r
1527 }\r
1528\r
1529 NumericValue = IScsiNetNtoi (Value);\r
1530 if (NumericValue > 2) {\r
1531 goto ON_ERROR;\r
1532 }\r
1533\r
1534 Session->ErrorRecoveryLevel = (UINT8) MIN (Session->ErrorRecoveryLevel, NumericValue);\r
1535\r
1536 //\r
1537 // InitialR2T: result function is OR.\r
1538 //\r
1539 if (!Session->InitialR2T) {\r
1540 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_INITIAL_R2T);\r
1541 if (Value == NULL) {\r
1542 goto ON_ERROR;\r
1543 }\r
1544\r
1545 Session->InitialR2T = (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0);\r
1546 }\r
1547\r
1548 //\r
1549 // ImmediateData: result function is AND.\r
1550 //\r
1551 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_IMMEDIATE_DATA);\r
1552 if (Value == NULL) {\r
1553 goto ON_ERROR;\r
1554 }\r
1555\r
1556 Session->ImmediateData = (BOOLEAN) (Session->ImmediateData && (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0));\r
1557\r
1558 //\r
1559 // MaxRecvDataSegmentLength is declarative.\r
1560 //\r
1561 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH);\r
1562 if (Value != NULL) {\r
1563 Conn->MaxRecvDataSegmentLength = (UINT32) IScsiNetNtoi (Value);\r
1564 }\r
1565 //\r
1566 // MaxBurstLength: result funtion is Mininum.\r
1567 //\r
1568 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_BURST_LENGTH);\r
1569 if (Value == NULL) {\r
1570 goto ON_ERROR;\r
1571 }\r
1572\r
1573 NumericValue = IScsiNetNtoi (Value);\r
1574 Session->MaxBurstLength = (UINT32) MIN (Session->MaxBurstLength, NumericValue);\r
1575\r
1576 //\r
1577 // FirstBurstLength: result function is Minimum. Irrelevant when InitialR2T=Yes and\r
1578 // ImmediateData=No.\r
1579 //\r
1580 if (!(Session->InitialR2T && !Session->ImmediateData)) {\r
1581 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_FIRST_BURST_LENGTH);\r
1582 if (Value == NULL) {\r
1583 goto ON_ERROR;\r
1584 }\r
1585\r
1586 NumericValue = IScsiNetNtoi (Value);\r
1587 Session->FirstBurstLength = (UINT32) MIN (Session->FirstBurstLength, NumericValue);\r
1588 }\r
1589\r
1590 //\r
1591 // MaxConnections: result function is Minimum.\r
1592 //\r
1593 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_CONNECTIONS);\r
1594 if (Value == NULL) {\r
1595 goto ON_ERROR;\r
1596 }\r
1597\r
1598 NumericValue = IScsiNetNtoi (Value);\r
1599 if ((NumericValue == 0) || (NumericValue > 65535)) {\r
1600 goto ON_ERROR;\r
1601 }\r
1602\r
1603 Session->MaxConnections = (UINT32) MIN (Session->MaxConnections, NumericValue);\r
1604\r
1605 //\r
1606 // DataPDUInOrder: result function is OR.\r
1607 //\r
1608 if (!Session->DataPDUInOrder) {\r
1609 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_PDU_IN_ORDER);\r
1610 if (Value == NULL) {\r
1611 goto ON_ERROR;\r
1612 }\r
1613\r
1614 Session->DataPDUInOrder = (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0);\r
1615 }\r
1616\r
1617 //\r
1618 // DataSequenceInorder: result function is OR.\r
1619 //\r
1620 if (!Session->DataSequenceInOrder) {\r
1621 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER);\r
1622 if (Value == NULL) {\r
1623 goto ON_ERROR;\r
1624 }\r
1625\r
1626 Session->DataSequenceInOrder = (BOOLEAN) (AsciiStrCmp (Value, "Yes") == 0);\r
1627 }\r
1628\r
1629 //\r
1630 // DefaultTime2Wait: result function is Maximum.\r
1631 //\r
1632 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DEFAULT_TIME2WAIT);\r
1633 if (Value == NULL) {\r
1634 goto ON_ERROR;\r
1635 }\r
1636\r
1637 NumericValue = IScsiNetNtoi (Value);\r
1638 if (NumericValue == 0) {\r
1639 Session->DefaultTime2Wait = 0;\r
1640 } else if (NumericValue > 3600) {\r
1641 goto ON_ERROR;\r
1642 } else {\r
1643 Session->DefaultTime2Wait = (UINT32) MAX (Session->DefaultTime2Wait, NumericValue);\r
1644 }\r
1645 //\r
1646 // DefaultTime2Retain: result function is Minimum.\r
1647 //\r
1648 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DEFAULT_TIME2RETAIN);\r
1649 if (Value == NULL) {\r
1650 goto ON_ERROR;\r
1651 }\r
1652\r
1653 NumericValue = IScsiNetNtoi (Value);\r
1654 if (NumericValue == 0) {\r
1655 Session->DefaultTime2Retain = 0;\r
1656 } else if (NumericValue > 3600) {\r
1657 goto ON_ERROR;\r
1658 } else {\r
1659 Session->DefaultTime2Retain = (UINT32) MIN (Session->DefaultTime2Retain, NumericValue);\r
1660 }\r
1661 //\r
1662 // MaxOutstandingR2T: result function is Minimum.\r
1663 //\r
1664 Value = IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_MAX_OUTSTANDING_R2T);\r
1665 if (Value == NULL) {\r
1666 goto ON_ERROR;\r
1667 }\r
1668\r
1669 NumericValue = IScsiNetNtoi (Value);\r
1670 if ((NumericValue == 0) || (NumericValue > 65535)) {\r
1671 goto ON_ERROR;\r
1672 }\r
1673\r
1674 Session->MaxOutstandingR2T = (UINT16) MIN (Session->MaxOutstandingR2T, NumericValue);\r
1675\r
1676 //\r
1677 // Remove declarative key-value pairs, if any.\r
1678 //\r
1679 IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_SESSION_TYPE);\r
1680 IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_ALIAS);\r
1681 IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_TARGET_PORTAL_GROUP_TAG);\r
1682\r
1683 \r
1684 //\r
1685 // Remove the key-value that may not needed for result function is OR.\r
1686 //\r
1687 IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_INITIAL_R2T);\r
1688 IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_PDU_IN_ORDER);\r
1689 IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER);\r
1690\r
75acfab6 1691 //\r
1692 // Remove irrelevant parameter, if any.\r
1693 //\r
1694 if (Session->InitialR2T && !Session->ImmediateData) {\r
1695 IScsiGetValueByKeyFromList (KeyValueList, ISCSI_KEY_FIRST_BURST_LENGTH);\r
1696 }\r
4c5a5e0c 1697\r
1698 if (IsListEmpty (KeyValueList)) {\r
1699 //\r
1700 // Succeed if no more keys in the list.\r
1701 //\r
1702 Status = EFI_SUCCESS;\r
1703 }\r
1704\r
1705ON_ERROR:\r
1706\r
1707 IScsiFreeKeyValueList (KeyValueList);\r
1708\r
1709 FreePool (Data);\r
1710\r
1711 return Status;\r
1712}\r
1713\r
1714\r
1715/**\r
75acfab6 1716 Fill the operational parameters.\r
4c5a5e0c 1717\r
1718 @param[in] Conn The connection in iSCSI login.\r
1719 @param[in, out] Pdu The iSCSI login request PDU to fill the parameters.\r
1720\r
1721**/\r
1722VOID\r
1723IScsiFillOpParams (\r
1724 IN ISCSI_CONNECTION *Conn,\r
1725 IN OUT NET_BUF *Pdu\r
1726 )\r
1727{\r
1728 ISCSI_SESSION *Session;\r
1729 CHAR8 Value[256];\r
1730\r
1731 Session = Conn->Session;\r
1732\r
1733 AsciiSPrint (Value, sizeof (Value), "%a", (Conn->HeaderDigest == IScsiDigestCRC32) ? "None,CRC32" : "None");\r
1734 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_HEADER_DIGEST, Value);\r
1735\r
1736 AsciiSPrint (Value, sizeof (Value), "%a", (Conn->DataDigest == IScsiDigestCRC32) ? "None,CRC32" : "None");\r
1737 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DATA_DIGEST, Value);\r
1738\r
1739 AsciiSPrint (Value, sizeof (Value), "%d", Session->ErrorRecoveryLevel);\r
1740 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_ERROR_RECOVERY_LEVEL, Value);\r
1741\r
1742 AsciiSPrint (Value, sizeof (Value), "%a", Session->InitialR2T ? "Yes" : "No");\r
1743 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_INITIAL_R2T, Value);\r
1744\r
1745 AsciiSPrint (Value, sizeof (Value), "%a", Session->ImmediateData ? "Yes" : "No");\r
1746 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_IMMEDIATE_DATA, Value);\r
1747\r
1748 AsciiSPrint (Value, sizeof (Value), "%d", MAX_RECV_DATA_SEG_LEN_IN_FFP);\r
1749 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_RECV_DATA_SEGMENT_LENGTH, Value);\r
1750\r
1751 AsciiSPrint (Value, sizeof (Value), "%d", Session->MaxBurstLength);\r
1752 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_BURST_LENGTH, Value);\r
1753\r
1754 AsciiSPrint (Value, sizeof (Value), "%d", Session->FirstBurstLength);\r
1755 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_FIRST_BURST_LENGTH, Value);\r
1756\r
1757 AsciiSPrint (Value, sizeof (Value), "%d", Session->MaxConnections);\r
1758 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_CONNECTIONS, Value);\r
1759\r
1760 AsciiSPrint (Value, sizeof (Value), "%a", Session->DataPDUInOrder ? "Yes" : "No");\r
1761 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DATA_PDU_IN_ORDER, Value);\r
1762\r
1763 AsciiSPrint (Value, sizeof (Value), "%a", Session->DataSequenceInOrder ? "Yes" : "No");\r
1764 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DATA_SEQUENCE_IN_ORDER, Value);\r
1765\r
1766 AsciiSPrint (Value, sizeof (Value), "%d", Session->DefaultTime2Wait);\r
1767 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DEFAULT_TIME2WAIT, Value);\r
1768\r
1769 AsciiSPrint (Value, sizeof (Value), "%d", Session->DefaultTime2Retain);\r
1770 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_DEFAULT_TIME2RETAIN, Value);\r
1771\r
1772 AsciiSPrint (Value, sizeof (Value), "%d", Session->MaxOutstandingR2T);\r
1773 IScsiAddKeyValuePair (Pdu, ISCSI_KEY_MAX_OUTSTANDING_R2T, Value);\r
1774}\r
1775\r
1776\r
1777/**\r
1778 Pad the iSCSI AHS or data segment to an integer number of 4 byte words.\r
1779\r
1780 @param[in, out] Pdu The iSCSI pdu which contains segments to pad.\r
1781 @param[in] Len The length of the last segment in the PDU.\r
1782\r
1783 @retval EFI_SUCCESS The segment is padded or there is no need to pad it.\r
1784 @retval EFI_OUT_OF_RESOURCES There is not enough remaining free space to add the\r
1785 padding bytes.\r
1786**/\r
1787EFI_STATUS\r
1788IScsiPadSegment (\r
1789 IN OUT NET_BUF *Pdu,\r
1790 IN UINT32 Len\r
1791 )\r
1792{\r
1793 UINT32 PadLen;\r
1794 UINT8 *Data;\r
1795\r
1796 PadLen = ISCSI_GET_PAD_LEN (Len);\r
1797\r
1798 if (PadLen != 0) {\r
1799 Data = NetbufAllocSpace (Pdu, PadLen, NET_BUF_TAIL);\r
1800 if (Data == NULL) {\r
1801 return EFI_OUT_OF_RESOURCES;\r
1802 }\r
1803\r
1804 ZeroMem (Data, PadLen);\r
1805 }\r
1806\r
1807 return EFI_SUCCESS;\r
1808}\r
1809\r
1810\r
1811/**\r
1812 Build a key-value list from the data segment.\r
1813\r
1814 @param[in] Data The data segment containing the key-value pairs.\r
1815 @param[in] Len Length of the data segment.\r
1816\r
1817 @return The key-value list.\r
1818 @retval NULL Other errors as indicated.\r
1819\r
1820**/\r
1821LIST_ENTRY *\r
1822IScsiBuildKeyValueList (\r
1823 IN CHAR8 *Data,\r
1824 IN UINT32 Len\r
1825 )\r
1826{\r
1827 LIST_ENTRY *ListHead;\r
1828 ISCSI_KEY_VALUE_PAIR *KeyValuePair;\r
1829\r
1830 ListHead = AllocatePool (sizeof (LIST_ENTRY));\r
1831 if (ListHead == NULL) {\r
1832 return NULL;\r
1833 }\r
1834\r
1835 InitializeListHead (ListHead);\r
1836\r
1837 while (Len > 0) {\r
1838 KeyValuePair = AllocatePool (sizeof (ISCSI_KEY_VALUE_PAIR));\r
1839 if (KeyValuePair == NULL) {\r
1840 goto ON_ERROR;\r
1841 }\r
1842\r
1843 InitializeListHead (&KeyValuePair->List);\r
1844\r
1845 KeyValuePair->Key = Data;\r
1846\r
1847 while ((Len > 0) && (*Data != '=')) {\r
1848 Len--;\r
1849 Data++;\r
1850 }\r
1851\r
1852 if (*Data == '=') {\r
1853 *Data = '\0';\r
1854\r
1855 Data++;\r
1856 Len--;\r
1857 } else {\r
1858 FreePool (KeyValuePair);\r
1859 goto ON_ERROR;\r
1860 }\r
1861\r
1862 KeyValuePair->Value = Data;\r
1863\r
1864 InsertTailList (ListHead, &KeyValuePair->List);;\r
1865\r
1866 Data += AsciiStrLen (KeyValuePair->Value) + 1;\r
1867 Len -= (UINT32) AsciiStrLen (KeyValuePair->Value) + 1;\r
1868 }\r
1869\r
1870 return ListHead;\r
1871\r
1872ON_ERROR:\r
1873\r
1874 IScsiFreeKeyValueList (ListHead);\r
1875\r
1876 return NULL;\r
1877}\r
1878\r
1879\r
1880/**\r
1881 Get the value string by the key name from the key-value list. If found,\r
1882 the key-value entry will be removed from the list.\r
1883\r
1884 @param[in, out] KeyValueList The key-value list.\r
1885 @param[in] Key The key name to find.\r
1886\r
1887 @return The value string.\r
1888 @retval NULL The key value pair cannot be found.\r
1889\r
1890**/\r
1891CHAR8 *\r
1892IScsiGetValueByKeyFromList (\r
1893 IN OUT LIST_ENTRY *KeyValueList,\r
1894 IN CHAR8 *Key\r
1895 )\r
1896{\r
1897 LIST_ENTRY *Entry;\r
1898 ISCSI_KEY_VALUE_PAIR *KeyValuePair;\r
1899 CHAR8 *Value;\r
1900\r
1901 Value = NULL;\r
1902\r
1903 NET_LIST_FOR_EACH (Entry, KeyValueList) {\r
1904 KeyValuePair = NET_LIST_USER_STRUCT (Entry, ISCSI_KEY_VALUE_PAIR, List);\r
1905\r
1906 if (AsciiStrCmp (KeyValuePair->Key, Key) == 0) {\r
1907 Value = KeyValuePair->Value;\r
1908\r
1909 RemoveEntryList (&KeyValuePair->List);\r
1910 FreePool (KeyValuePair);\r
1911 break;\r
1912 }\r
1913 }\r
1914\r
1915 return Value;\r
1916}\r
1917\r
1918\r
1919/**\r
1920 Free the key-value list.\r
1921\r
1922 @param[in] KeyValueList The key-value list.\r
1923\r
1924**/\r
1925VOID\r
1926IScsiFreeKeyValueList (\r
1927 IN LIST_ENTRY *KeyValueList\r
1928 )\r
1929{\r
1930 LIST_ENTRY *Entry;\r
1931 ISCSI_KEY_VALUE_PAIR *KeyValuePair;\r
1932\r
1933 while (!IsListEmpty (KeyValueList)) {\r
1934 Entry = NetListRemoveHead (KeyValueList);\r
1935 KeyValuePair = NET_LIST_USER_STRUCT (Entry, ISCSI_KEY_VALUE_PAIR, List);\r
1936\r
1937 FreePool (KeyValuePair);\r
1938 }\r
1939\r
1940 FreePool (KeyValueList);\r
1941}\r
1942\r
1943\r
1944/**\r
1945 Normalize the iSCSI name according to RFC.\r
1946\r
1947 @param[in, out] Name The iSCSI name.\r
1948 @param[in] Len Length of the iSCSI name.\r
1949\r
1950 @retval EFI_SUCCESS The iSCSI name is valid and normalized.\r
1951 @retval EFI_PROTOCOL_ERROR The iSCSI name is malformatted or not in the IQN format.\r
1952\r
1953**/\r
1954EFI_STATUS\r
1955IScsiNormalizeName (\r
1956 IN OUT CHAR8 *Name,\r
1957 IN UINTN Len\r
1958 )\r
1959{\r
1960 UINTN Index;\r
1961\r
1962 for (Index = 0; Index < Len; Index++) {\r
1963 if (NET_IS_UPPER_CASE_CHAR (Name[Index])) {\r
1964 //\r
1965 // Convert the upper-case characters to lower-case ones.\r
1966 //\r
1967 Name[Index] = (CHAR8) (Name[Index] - 'A' + 'a');\r
1968 }\r
1969\r
1970 if (!NET_IS_LOWER_CASE_CHAR (Name[Index]) &&\r
1971 !NET_IS_DIGIT (Name[Index]) &&\r
1972 (Name[Index] != '-') &&\r
1973 (Name[Index] != '.') &&\r
1974 (Name[Index] != ':')\r
1975 ) {\r
1976 //\r
1977 // ASCII dash, dot, colon lower-case characters and digit characters\r
1978 // are allowed.\r
1979 //\r
1980 return EFI_PROTOCOL_ERROR;\r
1981 }\r
1982 }\r
1983\r
1984 if ((Len < 4) || (CompareMem (Name, "iqn.", 4) != 0)) {\r
1985 //\r
1986 // Only IQN format is accepted now.\r
1987 //\r
1988 return EFI_PROTOCOL_ERROR;\r
1989 }\r
1990\r
1991 return EFI_SUCCESS;\r
1992}\r
1993\r
1994\r
1995/**\r
1996 Create an iSCSI task control block.\r
1997\r
1998 @param[in] Conn The connection on which the task control block will be created.\r
1999 @param[out] Tcb The newly created task control block.\r
2000\r
2001 @retval EFI_SUCCESS The task control block is created.\r
2002 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
2003 @retval EFI_NOT_READY The target cannot accept new commands.\r
2004\r
2005**/\r
2006EFI_STATUS\r
2007IScsiNewTcb (\r
2008 IN ISCSI_CONNECTION *Conn,\r
2009 OUT ISCSI_TCB **Tcb\r
2010 )\r
2011{\r
2012 ISCSI_SESSION *Session;\r
2013 ISCSI_TCB *NewTcb;\r
2014\r
2015 ASSERT (Tcb != NULL);\r
2016\r
2017 Session = Conn->Session;\r
2018\r
2019 if (ISCSI_SEQ_GT (Session->CmdSN, Session->MaxCmdSN)) {\r
2020 return EFI_NOT_READY;\r
2021 }\r
2022\r
2023 NewTcb = AllocateZeroPool (sizeof (ISCSI_TCB));\r
2024 if (NewTcb == NULL) {\r
2025 return EFI_OUT_OF_RESOURCES;\r
2026 }\r
2027\r
2028 InitializeListHead (&NewTcb->Link);\r
2029\r
2030 NewTcb->SoFarInOrder = TRUE;\r
2031 NewTcb->InitiatorTaskTag = Session->InitiatorTaskTag;\r
2032 NewTcb->CmdSN = Session->CmdSN;\r
2033 NewTcb->Conn = Conn;\r
2034\r
2035 InsertTailList (&Session->TcbList, &NewTcb->Link);\r
2036\r
2037 //\r
2038 // Advance the initiator task tag.\r
2039 //\r
2040 Session->InitiatorTaskTag++;\r
2041 Session->CmdSN++;\r
2042\r
2043 *Tcb = NewTcb;\r
2044\r
2045 return EFI_SUCCESS;\r
2046}\r
2047\r
2048\r
2049/**\r
2050 Delete the tcb from the connection and destroy it.\r
2051\r
2052 @param[in] Tcb The tcb to delete.\r
2053\r
2054**/\r
2055VOID\r
2056IScsiDelTcb (\r
2057 IN ISCSI_TCB *Tcb\r
2058 )\r
2059{\r
2060 RemoveEntryList (&Tcb->Link);\r
2061\r
2062 FreePool (Tcb);\r
2063}\r
2064\r
2065\r
2066/**\r
2067 Find the task control block by the initator task tag.\r
2068\r
2069 @param[in] TcbList The tcb list.\r
2070 @param[in] InitiatorTaskTag The initiator task tag.\r
2071\r
2072 @return The task control block found.\r
2073 @retval NULL The task control block cannot be found.\r
2074\r
2075**/\r
2076ISCSI_TCB *\r
2077IScsiFindTcbByITT (\r
2078 IN LIST_ENTRY *TcbList,\r
2079 IN UINT32 InitiatorTaskTag\r
2080 )\r
2081{\r
2082 ISCSI_TCB *Tcb;\r
2083 LIST_ENTRY *Entry;\r
2084\r
2085 Tcb = NULL;\r
2086\r
2087 NET_LIST_FOR_EACH (Entry, TcbList) {\r
2088 Tcb = NET_LIST_USER_STRUCT (Entry, ISCSI_TCB, Link);\r
2089\r
2090 if (Tcb->InitiatorTaskTag == InitiatorTaskTag) {\r
2091 break;\r
2092 }\r
2093 }\r
2094\r
2095 return Tcb;\r
2096}\r
2097\r
2098\r
2099/**\r
2100 Create a data segment, pad it, and calculate the CRC if needed.\r
2101\r
2102 @param[in] Data The data to fill into the data segment.\r
2103 @param[in] Len Length of the data.\r
2104 @param[in] DataDigest Whether to calculate CRC for this data segment.\r
2105\r
2106 @return The net buffer wrapping the data segment.\r
2107\r
2108**/\r
2109NET_BUF *\r
2110IScsiNewDataSegment (\r
2111 IN UINT8 *Data,\r
2112 IN UINT32 Len,\r
2113 IN BOOLEAN DataDigest\r
2114 )\r
2115{\r
2116 NET_FRAGMENT Fragment[2];\r
2117 UINT32 FragmentCount;\r
2118 UINT32 PadLen;\r
2119 NET_BUF *DataSeg;\r
2120\r
2121 Fragment[0].Len = Len;\r
2122 Fragment[0].Bulk = Data;\r
2123\r
2124 PadLen = ISCSI_GET_PAD_LEN (Len);\r
2125 if (PadLen != 0) {\r
2126 Fragment[1].Len = PadLen;\r
2127 Fragment[1].Bulk = (UINT8 *) &mDataSegPad;\r
2128\r
2129 FragmentCount = 2;\r
2130 } else {\r
2131 FragmentCount = 1;\r
2132 }\r
2133\r
2134 DataSeg = NetbufFromExt (&Fragment[0], FragmentCount, 0, 0, IScsiNbufExtFree, NULL);\r
2135\r
2136 return DataSeg;\r
2137}\r
2138\r
2139\r
2140/**\r
2141 Create a iSCSI SCSI command PDU to encapsulate the command issued\r
2142 by SCSI through the EXT SCSI PASS THRU Protocol.\r
2143\r
2144 @param[in] Packet The EXT SCSI PASS THRU request packet containing the SCSI command.\r
2145 @param[in] Lun The LUN.\r
d1c85a17 2146 @param[in] Tcb The tcb associated with this SCSI command.\r
4c5a5e0c 2147\r
2148 @return The created iSCSI SCSI command PDU.\r
2149 @retval NULL Other errors as indicated.\r
2150\r
2151**/\r
2152NET_BUF *\r
2153IScsiNewScsiCmdPdu (\r
2154 IN EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet,\r
2155 IN UINT64 Lun,\r
2156 IN ISCSI_TCB *Tcb\r
2157 )\r
2158{\r
2159 LIST_ENTRY *NbufList;\r
2160 NET_BUF *Pdu;\r
2161 NET_BUF *PduHeader;\r
2162 NET_BUF *DataSeg;\r
2163 SCSI_COMMAND *ScsiCmd;\r
2164 UINT8 AHSLength;\r
2165 UINT32 Length;\r
2166 ISCSI_ADDITIONAL_HEADER *Header;\r
2167 ISCSI_BI_EXP_READ_DATA_LEN_AHS *BiExpReadDataLenAHS;\r
2168 ISCSI_SESSION *Session;\r
2169 UINT32 ImmediateDataLen;\r
2170\r
2171 AHSLength = 0;\r
2172\r
2173 if (Packet->DataDirection == DataBi) {\r
2174 //\r
2175 // Bidirectional Read/Write command, the bidirectional expected\r
2176 // read data length AHS is required.\r
2177 //\r
2178 AHSLength += sizeof (ISCSI_BI_EXP_READ_DATA_LEN_AHS);\r
2179 }\r
2180\r
2181 if (Packet->CdbLength > 16) {\r
2182 //\r
2183 // The CDB exceeds 16 bytes. An extended CDB AHS is required.\r
2184 //\r
2185 AHSLength = (UINT8) (AHSLength + ISCSI_ROUNDUP (Packet->CdbLength - 16) + sizeof (ISCSI_ADDITIONAL_HEADER));\r
2186 }\r
2187\r
2188 Length = sizeof (SCSI_COMMAND) + AHSLength;\r
2189 PduHeader = NetbufAlloc (Length);\r
2190 if (PduHeader == NULL) {\r
2191 return NULL;\r
2192 }\r
2193\r
2194 ScsiCmd = (SCSI_COMMAND *) NetbufAllocSpace (PduHeader, Length, NET_BUF_TAIL);\r
2195 if (ScsiCmd == NULL) {\r
2196 NetbufFree (PduHeader);\r
2197 return NULL;\r
2198 } \r
2199 Header = (ISCSI_ADDITIONAL_HEADER *) (ScsiCmd + 1);\r
2200\r
2201 ZeroMem (ScsiCmd, Length);\r
2202\r
2203 ISCSI_SET_OPCODE (ScsiCmd, ISCSI_OPCODE_SCSI_CMD, 0);\r
2204 ISCSI_SET_FLAG (ScsiCmd, ISCSI_TASK_ATTR_SIMPLE);\r
2205\r
2206 //\r
2207 // Set the READ/WRITE flags according to the IO type of this request.\r
2208 //\r
2209 switch (Packet->DataDirection) {\r
2210 case DataIn:\r
2211 ISCSI_SET_FLAG (ScsiCmd, SCSI_CMD_PDU_FLAG_READ);\r
2212 ScsiCmd->ExpDataXferLength = NTOHL (Packet->InTransferLength);\r
2213 break;\r
2214\r
2215 case DataOut:\r
2216 ISCSI_SET_FLAG (ScsiCmd, SCSI_CMD_PDU_FLAG_WRITE);\r
2217 ScsiCmd->ExpDataXferLength = NTOHL (Packet->OutTransferLength);\r
2218 break;\r
2219\r
2220 case DataBi:\r
2221 ISCSI_SET_FLAG (ScsiCmd, SCSI_CMD_PDU_FLAG_READ | SCSI_CMD_PDU_FLAG_WRITE);\r
2222 ScsiCmd->ExpDataXferLength = NTOHL (Packet->OutTransferLength);\r
2223\r
2224 //\r
2225 // Fill the bidirectional expected read data length AHS.\r
2226 //\r
2227 BiExpReadDataLenAHS = (ISCSI_BI_EXP_READ_DATA_LEN_AHS *) Header;\r
2228 Header = (ISCSI_ADDITIONAL_HEADER *) (BiExpReadDataLenAHS + 1);\r
2229\r
2230 BiExpReadDataLenAHS->Length = NTOHS (5);\r
2231 BiExpReadDataLenAHS->Type = ISCSI_AHS_TYPE_BI_EXP_READ_DATA_LEN;\r
2232 BiExpReadDataLenAHS->ExpReadDataLength = NTOHL (Packet->InTransferLength);\r
2233\r
2234 break;\r
2235 }\r
2236\r
2237 ScsiCmd->TotalAHSLength = AHSLength;\r
2238 CopyMem (ScsiCmd->Lun, &Lun, sizeof (ScsiCmd->Lun));\r
2239 ScsiCmd->InitiatorTaskTag = NTOHL (Tcb->InitiatorTaskTag);\r
2240 ScsiCmd->CmdSN = NTOHL (Tcb->CmdSN);\r
2241 ScsiCmd->ExpStatSN = NTOHL (Tcb->Conn->ExpStatSN);\r
2242\r
2243 CopyMem (ScsiCmd->Cdb, Packet->Cdb, sizeof (ScsiCmd->Cdb));\r
2244\r
2245 if (Packet->CdbLength > 16) {\r
2246 Header->Length = NTOHS ((UINT16) (Packet->CdbLength - 15));\r
2247 Header->Type = ISCSI_AHS_TYPE_EXT_CDB;\r
2248\r
2249 CopyMem (Header + 1, (UINT8 *) Packet->Cdb + 16, Packet->CdbLength - 16);\r
2250 }\r
2251\r
2252 Pdu = PduHeader;\r
2253 Session = Tcb->Conn->Session;\r
2254 ImmediateDataLen = 0;\r
2255\r
2256 if (Session->ImmediateData && (Packet->OutTransferLength != 0)) {\r
2257 //\r
2258 // Send immediate data in this SCSI Command PDU. The length of the immeidate\r
2259 // data is the minimum of FirstBurstLength, the data length to be xfered, and\r
2260 // the MaxRecvdataSegmentLength on this connection.\r
2261 //\r
2262 ImmediateDataLen = MIN (Session->FirstBurstLength, Packet->OutTransferLength);\r
2263 ImmediateDataLen = MIN (ImmediateDataLen, Tcb->Conn->MaxRecvDataSegmentLength);\r
2264\r
2265 //\r
2266 // Update the data segment length in the PDU header.\r
2267 //\r
2268 ISCSI_SET_DATASEG_LEN (ScsiCmd, ImmediateDataLen);\r
2269\r
2270 //\r
2271 // Create the data segment.\r
2272 //\r
2273 DataSeg = IScsiNewDataSegment ((UINT8 *) Packet->OutDataBuffer, ImmediateDataLen, FALSE);\r
2274 if (DataSeg == NULL) {\r
2275 NetbufFree (PduHeader);\r
2276 Pdu = NULL;\r
2277 goto ON_EXIT;\r
2278 }\r
2279\r
2280 NbufList = AllocatePool (sizeof (LIST_ENTRY));\r
2281 if (NbufList == NULL) {\r
2282 NetbufFree (PduHeader);\r
2283 NetbufFree (DataSeg);\r
2284\r
2285 Pdu = NULL;\r
2286 goto ON_EXIT;\r
2287 }\r
2288\r
2289 InitializeListHead (NbufList);\r
2290 InsertTailList (NbufList, &PduHeader->List);\r
2291 InsertTailList (NbufList, &DataSeg->List);\r
2292\r
2293 Pdu = NetbufFromBufList (NbufList, 0, 0, IScsiFreeNbufList, NbufList);\r
2294 if (Pdu == NULL) {\r
2295 IScsiFreeNbufList (NbufList);\r
2296 }\r
2297 }\r
2298\r
2299 if (Session->InitialR2T ||\r
2300 (ImmediateDataLen == Session->FirstBurstLength) ||\r
2301 (ImmediateDataLen == Packet->OutTransferLength)\r
2302 ) {\r
2303 //\r
2304 // Unsolicited data out sequence is not allowed,\r
2305 // or FirstBustLength data is already sent out by immediate data,\r
2306 // or all the OUT data accompany this SCSI packet are sent as\r
2307 // immediate data. The final flag should be set on this SCSI Command\r
2308 // PDU.\r
2309 //\r
2310 ISCSI_SET_FLAG (ScsiCmd, ISCSI_BHS_FLAG_FINAL);\r
2311 }\r
2312\r
2313ON_EXIT:\r
2314\r
2315 return Pdu;\r
2316}\r
2317\r
2318\r
2319/**\r
2320 Create a new iSCSI SCSI Data Out PDU.\r
2321\r
2322 @param[in] Data The data to put into the Data Out PDU.\r
2323 @param[in] Len Length of the data.\r
2324 @param[in] DataSN The DataSN of the Data Out PDU.\r
2325 @param[in] Tcb The task control block of this Data Out PDU.\r
2326 @param[in] Lun The LUN.\r
2327\r
2328 @return The net buffer wrapping the Data Out PDU.\r
2329 @retval NULL Other errors as indicated.\r
2330\r
2331**/\r
2332NET_BUF *\r
2333IScsiNewDataOutPdu (\r
2334 IN UINT8 *Data,\r
2335 IN UINT32 Len,\r
2336 IN UINT32 DataSN,\r
2337 IN ISCSI_TCB *Tcb,\r
2338 IN UINT64 Lun\r
2339 )\r
2340{\r
2341 LIST_ENTRY *NbufList;\r
2342 NET_BUF *PduHdr;\r
2343 NET_BUF *DataSeg;\r
2344 NET_BUF *Pdu;\r
2345 ISCSI_SCSI_DATA_OUT *DataOutHdr;\r
2346 ISCSI_XFER_CONTEXT *XferContext;\r
2347\r
2348 NbufList = AllocatePool (sizeof (LIST_ENTRY));\r
2349 if (NbufList == NULL) {\r
2350 return NULL;\r
2351 }\r
2352\r
2353 InitializeListHead (NbufList);\r
2354\r
2355 //\r
2356 // Allocate memory for the BHS.\r
2357 //\r
2358 PduHdr = NetbufAlloc (sizeof (ISCSI_SCSI_DATA_OUT));\r
2359 if (PduHdr == NULL) {\r
2360 FreePool (NbufList);\r
2361 return NULL;\r
2362 }\r
2363 //\r
2364 // Insert the BHS into the buffer list.\r
2365 //\r
2366 InsertTailList (NbufList, &PduHdr->List);\r
2367\r
2368 DataOutHdr = (ISCSI_SCSI_DATA_OUT *) NetbufAllocSpace (PduHdr, sizeof (ISCSI_SCSI_DATA_OUT), NET_BUF_TAIL);\r
7c275b3c
ZL
2369 if (DataOutHdr == NULL) {\r
2370 IScsiFreeNbufList (NbufList);\r
2371 return NULL;\r
2372 }\r
4c5a5e0c 2373 XferContext = &Tcb->XferContext;\r
2374\r
2375 ZeroMem (DataOutHdr, sizeof (ISCSI_SCSI_DATA_OUT));\r
2376\r
2377 //\r
2378 // Set the flags and fields of the Data Out PDU BHS.\r
2379 //\r
2380 ISCSI_SET_OPCODE (DataOutHdr, ISCSI_OPCODE_SCSI_DATA_OUT, 0);\r
2381 ISCSI_SET_DATASEG_LEN (DataOutHdr, Len);\r
2382\r
2383 DataOutHdr->InitiatorTaskTag = HTONL (Tcb->InitiatorTaskTag);\r
2384 DataOutHdr->TargetTransferTag = HTONL (XferContext->TargetTransferTag);\r
2385 DataOutHdr->ExpStatSN = HTONL (Tcb->Conn->ExpStatSN);\r
2386 DataOutHdr->DataSN = HTONL (DataSN);\r
2387 DataOutHdr->BufferOffset = HTONL (XferContext->Offset);\r
2388\r
2389 if (XferContext->TargetTransferTag != ISCSI_RESERVED_TAG) {\r
2390 CopyMem (&DataOutHdr->Lun, &Lun, sizeof (DataOutHdr->Lun));\r
2391 }\r
2392 //\r
2393 // Build the data segment for this Data Out PDU.\r
2394 //\r
2395 DataSeg = IScsiNewDataSegment (Data, Len, FALSE);\r
2396 if (DataSeg == NULL) {\r
2397 IScsiFreeNbufList (NbufList);\r
2398 return NULL;\r
2399 }\r
2400 //\r
2401 // Put the data segment into the buffer list and combine it with the BHS\r
2402 // into a full Data Out PDU.\r
2403 //\r
2404 InsertTailList (NbufList, &DataSeg->List);\r
2405 Pdu = NetbufFromBufList (NbufList, 0, 0, IScsiFreeNbufList, NbufList);\r
2406 if (Pdu == NULL) {\r
2407 IScsiFreeNbufList (NbufList);\r
2408 }\r
2409\r
2410 return Pdu;\r
2411}\r
2412\r
2413\r
2414/**\r
2415 Generate a consecutive sequence of iSCSI SCSI Data Out PDUs.\r
2416\r
2417 @param[in] Data The data which will be carried by the sequence of iSCSI SCSI Data Out PDUs.\r
2418 @param[in] Tcb The task control block of the data to send out.\r
2419 @param[in] Lun The LUN the data will be sent to.\r
2420\r
2421 @return A list of net buffers with each of them wrapping an iSCSI SCSI Data Out PDU.\r
2422 @retval NULL Other errors as indicated.\r
2423\r
2424**/\r
2425LIST_ENTRY *\r
2426IScsiGenerateDataOutPduSequence (\r
2427 IN UINT8 *Data,\r
2428 IN ISCSI_TCB *Tcb,\r
2429 IN UINT64 Lun\r
2430 )\r
2431{\r
2432 LIST_ENTRY *PduList;\r
2433 UINT32 DataSN;\r
2434 UINT32 DataLen;\r
2435 NET_BUF *DataOutPdu;\r
2436 ISCSI_CONNECTION *Conn;\r
2437 ISCSI_XFER_CONTEXT *XferContext;\r
7a49cd08 2438 UINT8 *DataOutPacket;\r
4c5a5e0c 2439\r
2440 PduList = AllocatePool (sizeof (LIST_ENTRY));\r
2441 if (PduList == NULL) {\r
2442 return NULL;\r
2443 }\r
2444\r
2445 InitializeListHead (PduList);\r
2446\r
2447 DataSN = 0;\r
2448 Conn = Tcb->Conn;\r
2449 DataOutPdu = NULL;\r
2450 XferContext = &Tcb->XferContext;\r
2451\r
2452 while (XferContext->DesiredLength > 0) {\r
2453 //\r
2454 // Determine the length of data this Data Out PDU can carry.\r
2455 //\r
2456 DataLen = MIN (XferContext->DesiredLength, Conn->MaxRecvDataSegmentLength);\r
2457\r
2458 //\r
2459 // Create a Data Out PDU.\r
2460 //\r
2461 DataOutPdu = IScsiNewDataOutPdu (Data, DataLen, DataSN, Tcb, Lun);\r
2462 if (DataOutPdu == NULL) {\r
2463 IScsiFreeNbufList (PduList);\r
2464 PduList = NULL;\r
2465\r
2466 goto ON_EXIT;\r
2467 }\r
2468\r
2469 InsertTailList (PduList, &DataOutPdu->List);\r
2470\r
2471 //\r
2472 // Update the context and DataSN.\r
2473 //\r
2474 Data += DataLen;\r
2475 XferContext->Offset += DataLen;\r
2476 XferContext->DesiredLength -= DataLen;\r
2477 DataSN++;\r
2478 }\r
2479 //\r
2480 // Set the F bit for the last data out PDU in this sequence.\r
2481 //\r
7a49cd08
ED
2482 DataOutPacket = NetbufGetByte (DataOutPdu, 0, NULL);\r
2483 if (DataOutPacket == NULL) {\r
2484 IScsiFreeNbufList (PduList);\r
2485 PduList = NULL;\r
2486 goto ON_EXIT;\r
2487 }\r
2488\r
2489 ISCSI_SET_FLAG (DataOutPacket, ISCSI_BHS_FLAG_FINAL);\r
4c5a5e0c 2490\r
2491ON_EXIT:\r
2492\r
2493 return PduList;\r
2494}\r
2495\r
2496/**\r
2497 Send the Data in a sequence of Data Out PDUs one by one.\r
2498\r
2499 @param[in] Data The data to carry by Data Out PDUs.\r
2500 @param[in] Lun The LUN the data will be sent to.\r
2501 @param[in] Tcb The task control block.\r
2502\r
2503 @retval EFI_SUCCES The data is sent out to the LUN.\r
2504 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
2505 @retval Others Other errors as indicated.\r
2506\r
2507**/\r
2508EFI_STATUS\r
2509IScsiSendDataOutPduSequence (\r
2510 IN UINT8 *Data,\r
2511 IN UINT64 Lun,\r
2512 IN ISCSI_TCB *Tcb\r
2513 )\r
2514{\r
2515 LIST_ENTRY *DataOutPduList;\r
2516 LIST_ENTRY *Entry;\r
2517 NET_BUF *Pdu;\r
2518 EFI_STATUS Status;\r
2519\r
2520 //\r
2521 // Generate the Data Out PDU sequence.\r
2522 //\r
2523 DataOutPduList = IScsiGenerateDataOutPduSequence (Data, Tcb, Lun);\r
2524 if (DataOutPduList == NULL) {\r
2525 return EFI_OUT_OF_RESOURCES;\r
2526 }\r
2527\r
2528 Status = EFI_SUCCESS;\r
2529\r
2530 //\r
2531 // Send the Data Out PDU's one by one.\r
2532 //\r
2533 NET_LIST_FOR_EACH (Entry, DataOutPduList) {\r
2534 Pdu = NET_LIST_USER_STRUCT (Entry, NET_BUF, List);\r
2535\r
2536 Status = TcpIoTransmit (&Tcb->Conn->TcpIo, Pdu);\r
2537\r
2538 if (EFI_ERROR (Status)) {\r
2539 break;\r
2540 }\r
2541 }\r
2542\r
2543 IScsiFreeNbufList (DataOutPduList);\r
2544\r
2545 return Status;\r
2546}\r
2547\r
2548\r
2549/**\r
2550 Process the received iSCSI SCSI Data In PDU.\r
2551\r
2552 @param[in] Pdu The Data In PDU received.\r
2553 @param[in] Tcb The task control block.\r
2554 @param[in, out] Packet The EXT SCSI PASS THRU request packet.\r
2555\r
2556 @retval EFI_SUCCES The check on the Data IN PDU is passed and some update\r
2557 actions are taken.\r
2558 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.\r
2559 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.\r
2560 @retval Others Other errors as indicated.\r
2561\r
2562**/\r
2563EFI_STATUS\r
2564IScsiOnDataInRcvd (\r
2565 IN NET_BUF *Pdu,\r
2566 IN ISCSI_TCB *Tcb,\r
2567 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
2568 )\r
2569{\r
2570 ISCSI_SCSI_DATA_IN *DataInHdr;\r
2571 EFI_STATUS Status;\r
2572\r
2573 DataInHdr = (ISCSI_SCSI_DATA_IN *) NetbufGetByte (Pdu, 0, NULL);\r
7a49cd08
ED
2574 if (DataInHdr == NULL) {\r
2575 return EFI_PROTOCOL_ERROR;\r
2576 }\r
4c5a5e0c 2577\r
2578 DataInHdr->InitiatorTaskTag = NTOHL (DataInHdr->InitiatorTaskTag);\r
2579 DataInHdr->ExpCmdSN = NTOHL (DataInHdr->ExpCmdSN);\r
2580 DataInHdr->MaxCmdSN = NTOHL (DataInHdr->MaxCmdSN);\r
2581 DataInHdr->DataSN = NTOHL (DataInHdr->DataSN);\r
2582\r
2583 //\r
2584 // Check the DataSN.\r
2585 //\r
2586 Status = IScsiCheckSN (&Tcb->ExpDataSN, DataInHdr->DataSN);\r
2587 if (EFI_ERROR (Status)) {\r
2588 return Status;\r
2589 }\r
2590\r
2591 if (DataInHdr->InitiatorTaskTag != Tcb->InitiatorTaskTag) {\r
2592 return EFI_PROTOCOL_ERROR;\r
2593 }\r
2594 //\r
2595 // Update the command related sequence numbers.\r
2596 //\r
2597 IScsiUpdateCmdSN (Tcb->Conn->Session, DataInHdr->MaxCmdSN, DataInHdr->ExpCmdSN);\r
2598\r
2599 if (ISCSI_FLAG_ON (DataInHdr, SCSI_DATA_IN_PDU_FLAG_STATUS_VALID)) {\r
2600 if (!ISCSI_FLAG_ON (DataInHdr, ISCSI_BHS_FLAG_FINAL)) {\r
2601 //\r
2602 // The S bit is on but the F bit is off.\r
2603 //\r
2604 return EFI_PROTOCOL_ERROR;\r
2605 }\r
2606\r
2607 Tcb->StatusXferd = TRUE;\r
2608\r
2609 if (ISCSI_FLAG_ON (DataInHdr, SCSI_DATA_IN_PDU_FLAG_OVERFLOW | SCSI_DATA_IN_PDU_FLAG_UNDERFLOW)) {\r
2610 //\r
2611 // Underflow and Overflow are mutual flags.\r
2612 //\r
2613 return EFI_PROTOCOL_ERROR;\r
2614 }\r
2615 //\r
2616 // S bit is on, the StatSN is valid.\r
2617 //\r
2618 Status = IScsiCheckSN (&Tcb->Conn->ExpStatSN, NTOHL (DataInHdr->StatSN));\r
2619 if (EFI_ERROR (Status)) {\r
2620 return Status;\r
2621 }\r
2622\r
2623 Packet->HostAdapterStatus = 0;\r
2624 Packet->TargetStatus = DataInHdr->Status;\r
2625\r
2626 if (ISCSI_FLAG_ON (DataInHdr, SCSI_RSP_PDU_FLAG_OVERFLOW)) {\r
2627 Packet->InTransferLength += NTOHL (DataInHdr->ResidualCount);\r
2628 Status = EFI_BAD_BUFFER_SIZE;\r
2629 }\r
2630\r
2631 if (ISCSI_FLAG_ON (DataInHdr, SCSI_RSP_PDU_FLAG_UNDERFLOW)) {\r
2632 Packet->InTransferLength -= NTOHL (DataInHdr->ResidualCount);\r
2633 }\r
2634 }\r
2635\r
2636 return Status;\r
2637}\r
2638\r
2639\r
2640/**\r
2641 Process the received iSCSI R2T PDU.\r
2642\r
2643 @param[in] Pdu The R2T PDU received.\r
2644 @param[in] Tcb The task control block.\r
2645 @param[in] Lun The Lun.\r
2646 @param[in, out] Packet The EXT SCSI PASS THRU request packet.\r
2647\r
2648 @retval EFI_SUCCES The R2T PDU is valid and the solicited data is sent out.\r
2649 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.\r
2650 @retval Others Other errors as indicated.\r
2651\r
2652**/\r
2653EFI_STATUS\r
2654IScsiOnR2TRcvd (\r
2655 IN NET_BUF *Pdu,\r
2656 IN ISCSI_TCB *Tcb,\r
2657 IN UINT64 Lun,\r
2658 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
2659 )\r
2660{\r
2661 ISCSI_READY_TO_TRANSFER *R2THdr;\r
2662 EFI_STATUS Status;\r
2663 ISCSI_XFER_CONTEXT *XferContext;\r
2664 UINT8 *Data;\r
2665\r
2666 R2THdr = (ISCSI_READY_TO_TRANSFER *) NetbufGetByte (Pdu, 0, NULL);\r
7a49cd08
ED
2667 if (R2THdr == NULL) {\r
2668 return EFI_PROTOCOL_ERROR;\r
2669 }\r
4c5a5e0c 2670\r
2671 R2THdr->InitiatorTaskTag = NTOHL (R2THdr->InitiatorTaskTag);\r
2672 R2THdr->TargetTransferTag = NTOHL (R2THdr->TargetTransferTag);\r
2673 R2THdr->StatSN = NTOHL (R2THdr->StatSN);\r
2674 R2THdr->R2TSeqNum = NTOHL (R2THdr->R2TSeqNum);\r
2675 R2THdr->BufferOffset = NTOHL (R2THdr->BufferOffset);\r
2676 R2THdr->DesiredDataTransferLength = NTOHL (R2THdr->DesiredDataTransferLength);\r
2677\r
2678 if ((R2THdr->InitiatorTaskTag != Tcb->InitiatorTaskTag) || !ISCSI_SEQ_EQ (R2THdr->StatSN, Tcb->Conn->ExpStatSN)) {\r
2679 return EFI_PROTOCOL_ERROR;;\r
2680 }\r
2681 //\r
2682 // Check the sequence number.\r
2683 //\r
2684 Status = IScsiCheckSN (&Tcb->ExpDataSN, R2THdr->R2TSeqNum);\r
2685 if (EFI_ERROR (Status)) {\r
2686 return Status;\r
2687 }\r
2688\r
2689 XferContext = &Tcb->XferContext;\r
2690 XferContext->TargetTransferTag = R2THdr->TargetTransferTag;\r
2691 XferContext->Offset = R2THdr->BufferOffset;\r
2692 XferContext->DesiredLength = R2THdr->DesiredDataTransferLength;\r
2693\r
2694 if (((XferContext->Offset + XferContext->DesiredLength) > Packet->OutTransferLength) ||\r
2695 (XferContext->DesiredLength > Tcb->Conn->Session->MaxBurstLength)\r
2696 ) {\r
2697 return EFI_PROTOCOL_ERROR;\r
2698 }\r
2699 //\r
2700 // Send the data solicited by this R2T.\r
2701 //\r
2702 Data = (UINT8 *) Packet->OutDataBuffer + XferContext->Offset;\r
2703 Status = IScsiSendDataOutPduSequence (Data, Lun, Tcb);\r
2704\r
2705 return Status;\r
2706}\r
2707\r
2708\r
2709/**\r
2710 Process the received iSCSI SCSI Response PDU.\r
2711\r
2712 @param[in] Pdu The Response PDU received.\r
2713 @param[in] Tcb The task control block.\r
2714 @param[in, out] Packet The EXT SCSI PASS THRU request packet.\r
2715\r
2716 @retval EFI_SUCCES The Response PDU is processed.\r
2717 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.\r
2718 @retval EFI_BAD_BUFFER_SIZEE The buffer was not the proper size for the request.\r
2719 @retval Others Other errors as indicated.\r
2720\r
2721**/\r
2722EFI_STATUS\r
2723IScsiOnScsiRspRcvd (\r
2724 IN NET_BUF *Pdu,\r
2725 IN ISCSI_TCB *Tcb,\r
2726 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
2727 )\r
2728{\r
2729 SCSI_RESPONSE *ScsiRspHdr;\r
2730 ISCSI_SENSE_DATA *SenseData;\r
2731 EFI_STATUS Status;\r
2732 UINT32 DataSegLen;\r
2733\r
2734 ScsiRspHdr = (SCSI_RESPONSE *) NetbufGetByte (Pdu, 0, NULL);\r
7a49cd08
ED
2735 if (ScsiRspHdr == NULL) {\r
2736 return EFI_PROTOCOL_ERROR;\r
2737 }\r
4c5a5e0c 2738\r
2739 ScsiRspHdr->InitiatorTaskTag = NTOHL (ScsiRspHdr->InitiatorTaskTag);\r
2740 if (ScsiRspHdr->InitiatorTaskTag != Tcb->InitiatorTaskTag) {\r
2741 return EFI_PROTOCOL_ERROR;\r
2742 }\r
2743\r
2744 ScsiRspHdr->StatSN = NTOHL (ScsiRspHdr->StatSN);\r
2745\r
2746 Status = IScsiCheckSN (&Tcb->Conn->ExpStatSN, ScsiRspHdr->StatSN);\r
2747 if (EFI_ERROR (Status)) {\r
2748 return Status;\r
2749 }\r
2750\r
2751 ScsiRspHdr->MaxCmdSN = NTOHL (ScsiRspHdr->MaxCmdSN);\r
2752 ScsiRspHdr->ExpCmdSN = NTOHL (ScsiRspHdr->ExpCmdSN);\r
2753 IScsiUpdateCmdSN (Tcb->Conn->Session, ScsiRspHdr->MaxCmdSN, ScsiRspHdr->ExpCmdSN);\r
2754\r
2755 Tcb->StatusXferd = TRUE;\r
2756\r
2757 Packet->HostAdapterStatus = ScsiRspHdr->Response;\r
2758 if (Packet->HostAdapterStatus != ISCSI_SERVICE_RSP_COMMAND_COMPLETE_AT_TARGET) {\r
2759 return EFI_SUCCESS;\r
2760 }\r
2761\r
2762 Packet->TargetStatus = ScsiRspHdr->Status;\r
2763\r
2764 if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW | SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW) ||\r
2765 ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_OVERFLOW | SCSI_RSP_PDU_FLAG_UNDERFLOW)\r
2766 ) {\r
2767 return EFI_PROTOCOL_ERROR;\r
2768 }\r
2769\r
2770 if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_BI_READ_OVERFLOW)) {\r
2771 Packet->InTransferLength += NTOHL (ScsiRspHdr->BiReadResidualCount);\r
2772 Status = EFI_BAD_BUFFER_SIZE;\r
2773 }\r
2774\r
2775 if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_BI_READ_UNDERFLOW)) {\r
2776 Packet->InTransferLength -= NTOHL (ScsiRspHdr->BiReadResidualCount);\r
2777 }\r
2778\r
2779 if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_OVERFLOW)) {\r
2780 if (Packet->DataDirection == DataIn) {\r
2781 Packet->InTransferLength += NTOHL (ScsiRspHdr->ResidualCount);\r
2782 } else {\r
2783 Packet->OutTransferLength += NTOHL (ScsiRspHdr->ResidualCount);\r
2784 }\r
2785\r
2786 Status = EFI_BAD_BUFFER_SIZE;\r
2787 }\r
2788\r
2789 if (ISCSI_FLAG_ON (ScsiRspHdr, SCSI_RSP_PDU_FLAG_UNDERFLOW)) {\r
2790 if (Packet->DataDirection == DataIn) {\r
2791 Packet->InTransferLength -= NTOHL (ScsiRspHdr->ResidualCount);\r
2792 } else {\r
2793 Packet->OutTransferLength -= NTOHL (ScsiRspHdr->ResidualCount);\r
2794 }\r
2795 }\r
2796\r
2797 DataSegLen = ISCSI_GET_DATASEG_LEN (ScsiRspHdr);\r
2798 if (DataSegLen != 0) {\r
2799 SenseData = (ISCSI_SENSE_DATA *) NetbufGetByte (Pdu, sizeof (SCSI_RESPONSE), NULL);\r
7a49cd08
ED
2800 if (SenseData == NULL) {\r
2801 return EFI_PROTOCOL_ERROR;\r
2802 }\r
4c5a5e0c 2803\r
2804 SenseData->Length = NTOHS (SenseData->Length);\r
2805\r
2806 Packet->SenseDataLength = (UINT8) MIN (SenseData->Length, Packet->SenseDataLength);\r
2807 if (Packet->SenseDataLength != 0) {\r
2808 CopyMem (Packet->SenseData, &SenseData->Data[0], Packet->SenseDataLength);\r
2809 }\r
2810 } else {\r
2811 Packet->SenseDataLength = 0;\r
2812 }\r
2813\r
2814 return Status;\r
2815}\r
2816\r
2817\r
2818/**\r
2819 Process the received NOP In PDU.\r
2820\r
2821 @param[in] Pdu The NOP In PDU received.\r
2822 @param[in] Tcb The task control block.\r
2823\r
2824 @retval EFI_SUCCES The NOP In PDU is processed and the related sequence\r
2825 numbers are updated.\r
2826 @retval EFI_PROTOCOL_ERROR Some kind of iSCSI protocol errror occurred.\r
2827\r
2828**/\r
2829EFI_STATUS\r
2830IScsiOnNopInRcvd (\r
2831 IN NET_BUF *Pdu,\r
2832 IN ISCSI_TCB *Tcb\r
2833 )\r
2834{\r
2835 ISCSI_NOP_IN *NopInHdr;\r
2836 EFI_STATUS Status;\r
2837\r
2838 NopInHdr = (ISCSI_NOP_IN *) NetbufGetByte (Pdu, 0, NULL);\r
7a49cd08
ED
2839 if (NopInHdr == NULL) {\r
2840 return EFI_PROTOCOL_ERROR;\r
2841 }\r
4c5a5e0c 2842\r
2843 NopInHdr->StatSN = NTOHL (NopInHdr->StatSN);\r
2844 NopInHdr->ExpCmdSN = NTOHL (NopInHdr->ExpCmdSN);\r
2845 NopInHdr->MaxCmdSN = NTOHL (NopInHdr->MaxCmdSN);\r
2846\r
2847 if (NopInHdr->InitiatorTaskTag == ISCSI_RESERVED_TAG) {\r
2848 if (NopInHdr->StatSN != Tcb->Conn->ExpStatSN) {\r
2849 return EFI_PROTOCOL_ERROR;\r
2850 }\r
2851 } else {\r
2852 Status = IScsiCheckSN (&Tcb->Conn->ExpStatSN, NopInHdr->StatSN);\r
2853 if (EFI_ERROR (Status)) {\r
2854 return Status;\r
2855 }\r
2856 }\r
2857\r
2858 IScsiUpdateCmdSN (Tcb->Conn->Session, NopInHdr->MaxCmdSN, NopInHdr->ExpCmdSN);\r
2859\r
2860 return EFI_SUCCESS;\r
2861}\r
2862\r
2863\r
2864/**\r
2865 Execute the SCSI command issued through the EXT SCSI PASS THRU protocol.\r
2866\r
2867 @param[in] PassThru The EXT SCSI PASS THRU protocol.\r
2868 @param[in] Target The target ID.\r
2869 @param[in] Lun The LUN.\r
2870 @param[in, out] Packet The request packet containing IO request, SCSI command\r
2871 buffer and buffers to read/write.\r
2872 \r
2873 @retval EFI_SUCCES The SCSI command is executed and the result is updated to \r
2874 the Packet.\r
2875 @retval EFI_DEVICE_ERROR Session state was not as required.\r
2876 @retval EFI_OUT_OF_RESOURCES Failed to allocate memory.\r
7a49cd08 2877 @retval EFI_PROTOCOL_ERROR There is no such data in the net buffer.\r
cb162780 2878 @retval EFI_NOT_READY The target can not accept new commands.\r
4c5a5e0c 2879 @retval Others Other errors as indicated.\r
2880\r
2881**/\r
2882EFI_STATUS\r
2883IScsiExecuteScsiCommand (\r
2884 IN EFI_EXT_SCSI_PASS_THRU_PROTOCOL *PassThru,\r
2885 IN UINT8 *Target,\r
2886 IN UINT64 Lun,\r
2887 IN OUT EFI_EXT_SCSI_PASS_THRU_SCSI_REQUEST_PACKET *Packet\r
2888 )\r
2889{\r
2890 EFI_STATUS Status;\r
2891 ISCSI_DRIVER_DATA *Private;\r
2892 ISCSI_SESSION *Session;\r
2893 EFI_EVENT TimeoutEvent;\r
2894 ISCSI_CONNECTION *Conn;\r
2895 ISCSI_TCB *Tcb;\r
2896 NET_BUF *Pdu;\r
2897 ISCSI_XFER_CONTEXT *XferContext;\r
2898 UINT8 *Data;\r
2899 ISCSI_IN_BUFFER_CONTEXT InBufferContext;\r
2900 UINT64 Timeout;\r
2901 UINT8 *PduHdr;\r
2902\r
2903 Private = ISCSI_DRIVER_DATA_FROM_EXT_SCSI_PASS_THRU (PassThru);\r
2904 Session = Private->Session;\r
2905 Status = EFI_SUCCESS;\r
2906 Tcb = NULL;\r
2907 TimeoutEvent = NULL;\r
2908 Timeout = 0;\r
2909\r
2910 if (Session->State != SESSION_STATE_LOGGED_IN) {\r
e8b1fb2e
YT
2911 Status = EFI_DEVICE_ERROR;\r
2912 goto ON_EXIT;\r
4c5a5e0c 2913 }\r
2914\r
2915 Conn = NET_LIST_USER_STRUCT_S (\r
2916 Session->Conns.ForwardLink,\r
2917 ISCSI_CONNECTION,\r
2918 Link,\r
2919 ISCSI_CONNECTION_SIGNATURE\r
2920 );\r
2921\r
2922 if (Packet->Timeout != 0) {\r
2923 Timeout = MultU64x32 (Packet->Timeout, 4);\r
2924 }\r
2925\r
2926 Status = IScsiNewTcb (Conn, &Tcb);\r
2927 if (EFI_ERROR (Status)) {\r
2928 goto ON_EXIT;\r
2929 }\r
2930 //\r
2931 // Encapsulate the SCSI request packet into an iSCSI SCSI Command PDU.\r
2932 //\r
2933 Pdu = IScsiNewScsiCmdPdu (Packet, Lun, Tcb);\r
2934 if (Pdu == NULL) {\r
2935 Status = EFI_OUT_OF_RESOURCES;\r
2936 goto ON_EXIT;\r
2937 }\r
2938\r
2939 XferContext = &Tcb->XferContext;\r
2940 PduHdr = NetbufGetByte (Pdu, 0, NULL);\r
7a49cd08
ED
2941 if (PduHdr == NULL) {\r
2942 Status = EFI_PROTOCOL_ERROR;\r
2943 NetbufFree (Pdu);\r
2944 goto ON_EXIT;\r
2945 }\r
4c5a5e0c 2946 XferContext->Offset = ISCSI_GET_DATASEG_LEN (PduHdr);\r
2947\r
2948 //\r
2949 // Transmit the SCSI Command PDU.\r
2950 //\r
2951 Status = TcpIoTransmit (&Conn->TcpIo, Pdu);\r
2952\r
2953 NetbufFree (Pdu);\r
2954\r
2955 if (EFI_ERROR (Status)) {\r
2956 goto ON_EXIT;\r
2957 }\r
2958\r
2959 if (!Session->InitialR2T &&\r
2960 (XferContext->Offset < Session->FirstBurstLength) &&\r
2961 (XferContext->Offset < Packet->OutTransferLength)\r
2962 ) {\r
2963 //\r
2964 // Unsolicited Data-Out sequence is allowed. There is remaining SCSI\r
2965 // OUT data, and the limit of FirstBurstLength is not reached.\r
2966 //\r
2967 XferContext->TargetTransferTag = ISCSI_RESERVED_TAG;\r
2968 XferContext->DesiredLength = MIN (\r
2969 Session->FirstBurstLength,\r
2970 Packet->OutTransferLength - XferContext->Offset\r
2971 );\r
2972\r
2973 Data = (UINT8 *) Packet->OutDataBuffer + XferContext->Offset;\r
2974 Status = IScsiSendDataOutPduSequence (Data, Lun, Tcb);\r
2975 if (EFI_ERROR (Status)) {\r
2976 goto ON_EXIT;\r
2977 }\r
2978 }\r
2979\r
2980 InBufferContext.InData = (UINT8 *) Packet->InDataBuffer;\r
2981 InBufferContext.InDataLen = Packet->InTransferLength;\r
2982\r
2983 while (!Tcb->StatusXferd) {\r
2984 //\r
2985 // Start the timeout timer.\r
2986 //\r
2987 if (Timeout != 0) {\r
2988 Status = gBS->SetTimer (Conn->TimeoutEvent, TimerRelative, Timeout);\r
2989 if (EFI_ERROR (Status)) {\r
2990 goto ON_EXIT;\r
2991 }\r
2992\r
2993 TimeoutEvent = Conn->TimeoutEvent;\r
2994 }\r
2995\r
2996 //\r
2997 // Try to receive PDU from target.\r
2998 //\r
2999 Status = IScsiReceivePdu (Conn, &Pdu, &InBufferContext, FALSE, FALSE, TimeoutEvent);\r
3000 if (EFI_ERROR (Status)) {\r
3001 goto ON_EXIT;\r
3002 }\r
3003\r
7a49cd08
ED
3004 PduHdr = NetbufGetByte (Pdu, 0, NULL);\r
3005 if (PduHdr == NULL) {\r
3006 Status = EFI_PROTOCOL_ERROR;\r
3007 NetbufFree (Pdu);\r
3008 goto ON_EXIT;\r
3009 }\r
3010 switch (ISCSI_GET_OPCODE (PduHdr)) {\r
4c5a5e0c 3011 case ISCSI_OPCODE_SCSI_DATA_IN:\r
3012 Status = IScsiOnDataInRcvd (Pdu, Tcb, Packet);\r
3013 break;\r
3014\r
3015 case ISCSI_OPCODE_R2T:\r
3016 Status = IScsiOnR2TRcvd (Pdu, Tcb, Lun, Packet);\r
3017 break;\r
3018\r
3019 case ISCSI_OPCODE_SCSI_RSP:\r
3020 Status = IScsiOnScsiRspRcvd (Pdu, Tcb, Packet);\r
3021 break;\r
3022\r
3023 case ISCSI_OPCODE_NOP_IN:\r
3024 Status = IScsiOnNopInRcvd (Pdu, Tcb);\r
3025 break;\r
3026\r
3027 case ISCSI_OPCODE_VENDOR_T0:\r
3028 case ISCSI_OPCODE_VENDOR_T1:\r
3029 case ISCSI_OPCODE_VENDOR_T2:\r
3030 //\r
3031 // These messages are vendor specific. Skip them.\r
3032 //\r
3033 break;\r
3034\r
3035 default:\r
3036 Status = EFI_PROTOCOL_ERROR;\r
3037 break;\r
3038 }\r
3039\r
3040 NetbufFree (Pdu);\r
3041\r
3042 if (EFI_ERROR (Status)) {\r
3043 break;\r
3044 }\r
3045 }\r
3046\r
3047ON_EXIT:\r
3048\r
3049 if (TimeoutEvent != NULL) {\r
3050 gBS->SetTimer (TimeoutEvent, TimerCancel, 0);\r
3051 }\r
3052\r
3053 if (Tcb != NULL) {\r
3054 IScsiDelTcb (Tcb);\r
3055 }\r
3056\r
4c5a5e0c 3057 return Status;\r
3058}\r
3059\r
3060\r
3061/**\r
3062 Reinstate the session on some error.\r
3063\r
3064 @param[in] Session The iSCSI session\r
3065\r
3066 @retval EFI_SUCCESS The session is reinstated from some error.\r
3067 @retval Other Reinstatement failed.\r
3068\r
3069**/\r
3070EFI_STATUS\r
3071IScsiSessionReinstatement (\r
3072 IN ISCSI_SESSION *Session\r
3073 )\r
3074{\r
3075 EFI_STATUS Status;\r
3076\r
e8b1fb2e 3077 ASSERT (Session->State != SESSION_STATE_FREE);\r
4c5a5e0c 3078\r
3079 //\r
3080 // Abort the session and re-init it.\r
3081 //\r
3082 IScsiSessionAbort (Session);\r
3083 IScsiSessionInit (Session, TRUE);\r
3084\r
3085 //\r
3086 // Login again.\r
3087 //\r
3088 Status = IScsiSessionLogin (Session);\r
3089\r
3090 return Status;\r
3091}\r
3092\r
3093\r
3094/**\r
3095 Initialize some session parameters before login.\r
3096\r
3097 @param[in, out] Session The iSCSI session.\r
3098 @param[in] Recovery Whether the request is from a fresh new start or recovery.\r
3099\r
3100**/\r
3101VOID\r
3102IScsiSessionInit (\r
3103 IN OUT ISCSI_SESSION *Session,\r
3104 IN BOOLEAN Recovery\r
3105 )\r
3106{\r
3107 if (!Recovery) {\r
3108 Session->Signature = ISCSI_SESSION_SIGNATURE;\r
3109 Session->State = SESSION_STATE_FREE;\r
3110\r
3111 InitializeListHead (&Session->Conns);\r
3112 InitializeListHead (&Session->TcbList);\r
3113 }\r
3114\r
3115 Session->Tsih = 0;\r
3116\r
3117 Session->CmdSN = 1;\r
3118 Session->InitiatorTaskTag = 1;\r
3119 Session->NextCid = 1;\r
3120\r
3121 Session->TargetPortalGroupTag = 0;\r
3122 Session->MaxConnections = ISCSI_MAX_CONNS_PER_SESSION;\r
3123 Session->InitialR2T = FALSE;\r
3124 Session->ImmediateData = TRUE;\r
3125 Session->MaxBurstLength = 262144;\r
3126 Session->FirstBurstLength = MAX_RECV_DATA_SEG_LEN_IN_FFP;\r
3127 Session->DefaultTime2Wait = 2;\r
3128 Session->DefaultTime2Retain = 20;\r
3129 Session->MaxOutstandingR2T = DEFAULT_MAX_OUTSTANDING_R2T;\r
3130 Session->DataPDUInOrder = TRUE;\r
3131 Session->DataSequenceInOrder = TRUE;\r
3132 Session->ErrorRecoveryLevel = 0;\r
3133}\r
3134\r
3135\r
3136/**\r
3137 Abort the iSCSI session. That is, reset all the connection(s), and free the\r
3138 resources.\r
3139\r
3140 @param[in, out] Session The iSCSI session.\r
3141\r
3142**/\r
3143VOID\r
3144IScsiSessionAbort (\r
3145 IN OUT ISCSI_SESSION *Session\r
3146 )\r
3147{\r
3148 ISCSI_CONNECTION *Conn;\r
3149 EFI_GUID *ProtocolGuid;\r
3150\r
3151 if (Session->State != SESSION_STATE_LOGGED_IN) {\r
3152 return ;\r
3153 }\r
3154\r
3155 ASSERT (!IsListEmpty (&Session->Conns));\r
3156\r
3157 while (!IsListEmpty (&Session->Conns)) {\r
3158 Conn = NET_LIST_USER_STRUCT_S (\r
3159 Session->Conns.ForwardLink,\r
3160 ISCSI_CONNECTION,\r
3161 Link,\r
3162 ISCSI_CONNECTION_SIGNATURE\r
3163 );\r
3164 if (!Conn->Ipv6Flag) {\r
3165 ProtocolGuid = &gEfiTcp4ProtocolGuid;\r
3166 } else {\r
3167 ProtocolGuid = &gEfiTcp6ProtocolGuid; \r
3168 }\r
3169\r
3170 gBS->CloseProtocol (\r
3171 Conn->TcpIo.Handle,\r
3172 ProtocolGuid,\r
3173 Session->Private->Image,\r
3174 Session->Private->ExtScsiPassThruHandle\r
3175 );\r
3176\r
3177 IScsiConnReset (Conn);\r
3178\r
3179 IScsiDetatchConnection (Conn);\r
3180 IScsiDestroyConnection (Conn);\r
3181 }\r
3182\r
3183 Session->State = SESSION_STATE_FAILED;\r
3184\r
3185 return ;\r
3186}\r