1 /** @file
2   Header file for IDE Bus Driver.
3 
4   Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5   SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #ifndef _IDE_BUS_H_
10 #define _IDE_BUS_H_
11 
12 
13 
14 #include <FrameworkDxe.h>
15 
16 #include <Protocol/IdeControllerInit.h>
17 #include <Protocol/BlockIo.h>
18 #include <Protocol/PciIo.h>
19 #include <Protocol/DiskInfo.h>
20 #include <Protocol/DevicePath.h>
21 
22 #include <Library/DebugLib.h>
23 #include <Library/UefiDriverEntryPoint.h>
24 #include <Library/BaseLib.h>
25 #include <Library/UefiLib.h>
26 #include <Library/BaseMemoryLib.h>
27 #include <Library/ReportStatusCodeLib.h>
28 #include <Library/MemoryAllocationLib.h>
29 #include <Library/PerformanceLib.h>
30 #include <Library/UefiBootServicesTableLib.h>
31 #include <Library/UefiRuntimeServicesTableLib.h>
32 #include <Library/DevicePathLib.h>
33 
34 #include <Guid/EventGroup.h>
35 
36 #include <IndustryStandard/Pci.h>
37 #include "IdeData.h"
38 
39 //
40 // Global Variables
41 //
42 extern EFI_DRIVER_BINDING_PROTOCOL      gIDEBusDriverBinding;
43 extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL  gIDEBusDriverDiagnostics;
44 extern EFI_DRIVER_DIAGNOSTICS2_PROTOCOL gIDEBusDriverDiagnostics2;
45 
46 //
47 // Extra Definition to porting
48 //
49 #define MAX_IDE_DEVICE    4
50 #define MAX_IDE_CHANNELS  2
51 #define MAX_IDE_DRIVES    2
52 
53 #define INVALID_DEVICE_TYPE 0xff
54 #define ATA_DEVICE_TYPE     0x00
55 #define ATAPI_DEVICE_TYPE   0x01
56 
57 typedef struct {
58   BOOLEAN HaveScannedDevice[MAX_IDE_DEVICE];
59   BOOLEAN DeviceFound[MAX_IDE_DEVICE];
60   BOOLEAN DeviceProcessed[MAX_IDE_DEVICE];
61 } IDE_BUS_DRIVER_PRIVATE_DATA;
62 
63 #define IDE_BLK_IO_DEV_SIGNATURE  SIGNATURE_32 ('i', 'b', 'i', 'd')
64 
65 typedef struct {
66   UINT32                      Signature;
67 
68   EFI_HANDLE                  Handle;
69   EFI_BLOCK_IO_PROTOCOL       BlkIo;
70   EFI_BLOCK_IO_MEDIA          BlkMedia;
71   EFI_DISK_INFO_PROTOCOL      DiskInfo;
72   EFI_DEVICE_PATH_PROTOCOL    *DevicePath;
73   EFI_PCI_IO_PROTOCOL         *PciIo;
74   IDE_BUS_DRIVER_PRIVATE_DATA *IdeBusDriverPrivateData;
75 
76   //
77   // Local Data for IDE interface goes here
78   //
79   EFI_IDE_CHANNEL             Channel;
80   EFI_IDE_DEVICE              Device;
81   UINT16                      Lun;
82   IDE_DEVICE_TYPE             Type;
83 
84   IDE_BASE_REGISTERS          *IoPort;
85   UINT16                      AtapiError;
86 
87   ATAPI_INQUIRY_DATA                *InquiryData;
88   EFI_IDENTIFY_DATA           *IdData;
89   ATA_PIO_MODE                PioMode;
90   EFI_ATA_MODE                UdmaMode;
91   CHAR8                       ModelName[41];
92   ATAPI_REQUEST_SENSE_DATA          *SenseData;
93   UINT8                       SenseDataNumber;
94   UINT8                       *Cache;
95 
96   //
97   // ExitBootService Event, it is used to clear pending IDE interrupt
98   //
99   EFI_EVENT                   ExitBootServiceEvent;
100 
101   EFI_UNICODE_STRING_TABLE    *ControllerNameTable;
102 } IDE_BLK_IO_DEV;
103 
104 #include "ComponentName.h"
105 
106 #define IDE_BLOCK_IO_DEV_FROM_THIS(a)           CR (a, IDE_BLK_IO_DEV, BlkIo, IDE_BLK_IO_DEV_SIGNATURE)
107 #define IDE_BLOCK_IO_DEV_FROM_DISK_INFO_THIS(a) CR (a, IDE_BLK_IO_DEV, DiskInfo, IDE_BLK_IO_DEV_SIGNATURE)
108 
109 #include "Ide.h"
110 
111 
112 /**
113   Supported function of Driver Binding protocol for this driver.
114 
115   @param This                A pointer to the EFI_DRIVER_BINDING_PROTOCOL instance.
116   @param ControllerHandle    The handle of the controller to test.
117   @param RemainingDevicePath A pointer to the remaining portion of a device path.
118 
119   @retval  EFI_SUCCESS Driver loaded.
120   @retval  other       Driver not loaded.
121 
122 **/
123 EFI_STATUS
124 EFIAPI
125 IDEBusDriverBindingSupported (
126   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
127   IN EFI_HANDLE                   Controller,
128   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
129   );
130 
131 /**
132   Start function of Driver binding protocol which start this driver on Controller
133   by detecting all disks and installing BlockIo protocol on them.
134 
135   @param  This                Protocol instance pointer.
136   @param  Controller          Handle of device to bind driver to.
137   @param  RemainingDevicePath produce all possible children.
138 
139   @retval  EFI_SUCCESS         This driver is added to ControllerHandle.
140   @retval  EFI_ALREADY_STARTED This driver is already running on ControllerHandle.
141   @retval  other               This driver does not support this device.
142 
143 **/
144 EFI_STATUS
145 EFIAPI
146 IDEBusDriverBindingStart (
147   IN EFI_DRIVER_BINDING_PROTOCOL  *This,
148   IN EFI_HANDLE                   Controller,
149   IN EFI_DEVICE_PATH_PROTOCOL     *RemainingDevicePath
150   );
151 
152 /**
153   Stop function of Driver Binding Protocol which is to stop the driver on Controller Handle and all
154   child handle attached to the controller handle if there are.
155 
156   @param  This Protocol instance pointer.
157   @param  Controller Handle of device to stop driver on
158   @param  NumberOfChildren Not used
159   @param  ChildHandleBuffer Not used
160 
161   @retval  EFI_SUCCESS This driver is removed DeviceHandle
162   @retval  other This driver was not removed from this device
163 
164 **/
165 EFI_STATUS
166 EFIAPI
167 IDEBusDriverBindingStop (
168   IN  EFI_DRIVER_BINDING_PROTOCOL *This,
169   IN  EFI_HANDLE                  Controller,
170   IN  UINTN                       NumberOfChildren,
171   IN  EFI_HANDLE                  *ChildHandleBuffer
172   );
173 
174 //
175 // EFI Driver Configuration Functions
176 //
177 
178 
179 
180 
181 //
182 // EFI Driver Diagnostics Functions
183 //
184 /**
185   Runs diagnostics on a controller.
186 
187   @param  This             A pointer to the EFI_DRIVER_DIAGNOSTICS_PROTOCOLinstance.
188   @param  ControllerHandle The handle of the controller to run diagnostics on.
189   @param  ChildHandle      The handle of the child controller to run diagnostics on
190                            This is an optional parameter that may be NULL.  It will
191                            be NULL for device drivers.  It will also be NULL for a
192                            bus drivers that wish to run diagnostics on the bus controller.
193                            It will not be NULL for a bus driver that wishes to run
194                            diagnostics on one of its child controllers.
195   @param  DiagnosticType   Indicates type of diagnostics to perform on the controller
196                            specified by ControllerHandle and ChildHandle.
197   @param  Language         A pointer to a three character ISO 639-2 language identifier.
198                            This is the language in which the optional error message should
199                            be returned in Buffer, and it must match one of the languages
200                            specified in SupportedLanguages. The number of languages supported by
201                            a driver is up to the driver writer.
202   @param  ErrorType        A GUID that defines the format of the data returned in Buffer.
203   @param  BufferSize       The size, in bytes, of the data returned in Buffer.
204   @param  Buffer           A buffer that contains a Null-terminated Unicode string
205                            plus some additional data whose format is defined by ErrorType.
206                            Buffer is allocated by this function with AllocatePool(), and
207                            it is the caller's responsibility to free it with a call to FreePool().
208 
209   @retval  EFI_SUCCESS           The controller specified by ControllerHandle and ChildHandle passed
210                                  the diagnostic.
211   @retval  EFI_INVALID_PARAMETER ControllerHandle is NULL.
212   @retval  EFI_INVALID_PARAMETER ChildHandle is not NULL and it is not a valid EFI_HANDLE.
213   @retval  EFI_INVALID_PARAMETER Language is NULL.
214   @retval  EFI_INVALID_PARAMETER ErrorType is NULL.
215   @retval  EFI_INVALID_PARAMETER BufferType is NULL.
216   @retval  EFI_INVALID_PARAMETER Buffer is NULL.
217   @retval  EFI_UNSUPPORTED       The driver specified by This does not support running
218                                  diagnostics for the controller specified by ControllerHandle
219                                  and ChildHandle.
220   @retval  EFI_UNSUPPORTED       The driver specified by This does not support the
221                                  type of diagnostic specified by DiagnosticType.
222   @retval  EFI_UNSUPPORTED       The driver specified by This does not support the language
223                                  specified by Language.
224   @retval  EFI_OUT_OF_RESOURCES  There are not enough resources available to complete the
225                                  diagnostics.
226   @retval  EFI_OUT_OF_RESOURCES  There are not enough resources available to return the
227                                  status information in ErrorType, BufferSize,and Buffer.
228   @retval  EFI_DEVICE_ERROR      The controller specified by ControllerHandle and ChildHandle
229                                  did not pass the diagnostic.
230 **/
231 EFI_STATUS
232 EFIAPI
233 IDEBusDriverDiagnosticsRunDiagnostics (
234   IN  EFI_DRIVER_DIAGNOSTICS_PROTOCOL               *This,
235   IN  EFI_HANDLE                                    ControllerHandle,
236   IN  EFI_HANDLE                                    ChildHandle  OPTIONAL,
237   IN  EFI_DRIVER_DIAGNOSTIC_TYPE                    DiagnosticType,
238   IN  CHAR8                                         *Language,
239   OUT EFI_GUID                                      **ErrorType,
240   OUT UINTN                                         *BufferSize,
241   OUT CHAR16                                        **Buffer
242   );
243 
244 /**
245   issue ATA or ATAPI command to reset a block IO device.
246   @param  This                  Block IO protocol instance pointer.
247   @param  ExtendedVerification  If FALSE,for ATAPI device, driver will only invoke ATAPI reset method
248                                 If TRUE, for ATAPI device, driver need invoke ATA reset method after
249                                 invoke ATAPI reset method
250 
251   @retval EFI_DEVICE_ERROR      When the device is neighther ATA device or ATAPI device.
252   @retval EFI_SUCCESS           The device reset successfully
253 
254 **/
255 EFI_STATUS
256 EFIAPI
257 IDEBlkIoReset (
258   IN  EFI_BLOCK_IO_PROTOCOL       *This,
259   IN  BOOLEAN                     ExtendedVerification
260   );
261 
262 /**
263   Read data from a block IO device.
264 
265   @param  This       Block IO protocol instance pointer.
266   @param  MediaId    The media ID of the device
267   @param  Lba        Starting LBA address to read data
268   @param  BufferSize The size of data to be read
269   @param  Buffer     Caller supplied buffer to save data
270 
271   @retval EFI_DEVICE_ERROR  unknown device type
272   @retval EFI_SUCCESS       read the data successfully.
273 
274 **/
275 EFI_STATUS
276 EFIAPI
277 IDEBlkIoReadBlocks (
278   IN  EFI_BLOCK_IO_PROTOCOL       *This,
279   IN  UINT32                      MediaId,
280   IN  EFI_LBA                     Lba,
281   IN  UINTN                       BufferSize,
282   OUT VOID                        *Buffer
283   );
284 
285 /**
286   Write data to block io device
287 
288   @param  This       Protocol instance pointer.
289   @param  MediaId    The media ID of the device
290   @param  Lba        Starting LBA address to write data
291   @param  BufferSize The size of data to be written
292   @param  Buffer     Caller supplied buffer to save data
293 
294   @retval EFI_DEVICE_ERROR  unknown device type
295   @retval other             write data status
296 
297 **/
298 EFI_STATUS
299 EFIAPI
300 IDEBlkIoWriteBlocks (
301   IN  EFI_BLOCK_IO_PROTOCOL       *This,
302   IN  UINT32                      MediaId,
303   IN  EFI_LBA                     Lba,
304   IN  UINTN                       BufferSize,
305   IN  VOID                        *Buffer
306   );
307 
308 /**
309   Flushes all modified data to a physical block devices
310 
311   @param  This  Indicates a pointer to the calling context which to sepcify a
312                 sepcific block device
313 
314   @retval EFI_SUCCESS   Always return success.
315 **/
316 EFI_STATUS
317 EFIAPI
318 IDEBlkIoFlushBlocks (
319   IN  EFI_BLOCK_IO_PROTOCOL       *This
320   );
321 /**
322   This function is used by the IDE bus driver to get inquiry data.
323   Data format of Identify data is defined by the Interface GUID.
324 
325   @param  This                  Pointer to the EFI_DISK_INFO_PROTOCOL instance.
326   @param  InquiryData           Pointer to a buffer for the inquiry data.
327   @param  InquiryDataSize       Pointer to the value for the inquiry data size.
328 
329   @retval EFI_SUCCESS           The command was accepted without any errors.
330   @retval EFI_NOT_FOUND         Device does not support this data class
331   @retval EFI_DEVICE_ERROR      Error reading InquiryData from device
332   @retval EFI_BUFFER_TOO_SMALL  IntquiryDataSize not big enough
333 
334 **/
335 EFI_STATUS
336 EFIAPI
337 IDEDiskInfoInquiry (
338   IN EFI_DISK_INFO_PROTOCOL       *This,
339   IN OUT VOID                     *InquiryData,
340   IN OUT UINT32                   *InquiryDataSize
341   );
342 
343 /**
344   This function is used by the IDE bus driver to get identify data.
345   Data format of Identify data is defined by the Interface GUID.
346 
347   @param  This                  Pointer to the EFI_DISK_INFO_PROTOCOL instance.
348   @param  IdentifyData          Pointer to a buffer for the identify data.
349   @param  IdentifyDataSize      Pointer to the value for the identify data size.
350 
351   @retval EFI_SUCCESS           The command was accepted without any errors.
352   @retval EFI_NOT_FOUND         Device does not support this data class
353   @retval EFI_DEVICE_ERROR      Error reading IdentifyData from device
354   @retval EFI_BUFFER_TOO_SMALL  IdentifyDataSize not big enough
355 
356 **/
357 EFI_STATUS
358 EFIAPI
359 IDEDiskInfoIdentify (
360   IN EFI_DISK_INFO_PROTOCOL       *This,
361   IN OUT VOID                     *IdentifyData,
362   IN OUT UINT32                   *IdentifyDataSize
363   );
364 
365 /**
366   This function is used by the IDE bus driver to get sense data.
367   Data format of Sense data is defined by the Interface GUID.
368 
369   @param  This                  Pointer to the EFI_DISK_INFO_PROTOCOL instance.
370   @param  SenseData             Pointer to the SenseData.
371   @param  SenseDataSize         Size of SenseData in bytes.
372   @param  SenseDataNumber       Pointer to the value for the identify data size.
373 
374   @retval EFI_SUCCESS           The command was accepted without any errors.
375   @retval EFI_NOT_FOUND         Device does not support this data class
376   @retval EFI_DEVICE_ERROR      Error reading InquiryData from device
377   @retval EFI_BUFFER_TOO_SMALL  SenseDataSize not big enough
378 
379 **/
380 EFI_STATUS
381 EFIAPI
382 IDEDiskInfoSenseData (
383   IN EFI_DISK_INFO_PROTOCOL       *This,
384   IN OUT VOID                     *SenseData,
385   IN OUT UINT32                   *SenseDataSize,
386   OUT UINT8                       *SenseDataNumber
387   );
388 
389 /**
390   This function is used by the IDE bus driver to get controller information.
391 
392   @param  This                  Pointer to the EFI_DISK_INFO_PROTOCOL instance.
393   @param  IdeChannel            Pointer to the Ide Channel number. Primary or secondary.
394   @param  IdeDevice             Pointer to the Ide Device number. Master or slave.
395 
396   @retval EFI_SUCCESS           IdeChannel and IdeDevice are valid
397   @retval EFI_UNSUPPORTED       This is not an IDE device
398 
399 **/
400 EFI_STATUS
401 EFIAPI
402 IDEDiskInfoWhichIde (
403   IN EFI_DISK_INFO_PROTOCOL       *This,
404   OUT UINT32                      *IdeChannel,
405   OUT UINT32                      *IdeDevice
406   );
407 /**
408   The is an event(generally the event is exitBootService event) call back function.
409   Clear pending IDE interrupt before OS loader/kernel take control of the IDE device.
410 
411   @param  Event   Pointer to this event
412   @param  Context Event handler private data
413 
414 **/
415 VOID
416 EFIAPI
417 ClearInterrupt (
418   IN EFI_EVENT  Event,
419   IN VOID       *Context
420   );
421 #endif
422