The Pedigree Project  0.1
ScsiDisk.h
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef SCSIDISK_H
21 #define SCSIDISK_H
22 
23 #include "pedigree/kernel/compiler.h"
24 #include "pedigree/kernel/machine/Disk.h"
25 #include "pedigree/kernel/processor/types.h"
26 #include "pedigree/kernel/utilities/Cache.h"
27 #include "pedigree/kernel/utilities/CacheConstants.h"
28 #include "pedigree/kernel/utilities/String.h"
29 
30 class ScsiCommand;
31 
33 {
34  private:
35  enum ScsiPeripheralType
36  {
37  BlockDevice = 0x00,
38  SequentialDevice = 0x01,
39  PrinterDevice = 0x02,
40  ProcessorDevice = 0x03,
41  WriteOnceDevice = 0x04,
42  CdDvdDevice = 0x05,
43  OpticalMemoryDevice = 0x07,
44  MediumChangerDevice = 0x08,
45  ArrayDevice = 0x0C,
46  EnclosureDevice = 0x0D,
47  NoDevice = 0x1F,
48  };
49 
50  public:
51  struct Sense
52  {
53  uint8_t ResponseCode;
54  uint8_t Obsolete;
55  uint32_t SenseKey : 4;
56  uint32_t rsvd : 1;
57  uint32_t ili : 1;
58  uint32_t eom : 1;
59  uint32_t filemark : 1;
60  uint32_t info;
61  uint8_t addLen;
62  uint32_t commandInfo;
63  uint8_t Asc;
64  uint8_t AscQ;
65  uint8_t fieldCode;
66  uint8_t senseKeySpecific[3];
67  } PACKED;
68 
69  struct Inquiry
70  {
71  uint8_t Peripheral;
72  uint8_t Removable;
73  uint8_t Version;
74  uint8_t Flags1;
75  uint8_t AddLength;
76  uint8_t Rsvd[2];
77  uint8_t Flags2;
78  uint8_t VendIdent[8];
79  uint8_t ProdIdent[16];
80  uint8_t ProdRev[4];
81  } PACKED;
82 
83  struct Capacity
84  {
85  uint32_t LBA;
86  uint32_t BlockSize;
87  } PACKED;
88 
89  ScsiDisk();
90  virtual ~ScsiDisk();
91 
92  bool initialise(class ScsiController *pController, size_t nUnit);
93 
94  virtual uintptr_t read(uint64_t location);
95  virtual void write(uint64_t location);
96  virtual void flush(uint64_t location);
97  virtual void align(uint64_t location);
98 
99  virtual void getName(String &str)
100  {
101  str = String("SCSI Disk");
102  }
103 
104  // These are the internal functions that the controller calls when it is
105  // ready to process our request.
106  virtual uint64_t doRead(uint64_t location);
107  virtual uint64_t doWrite(uint64_t location);
108  virtual uint64_t doSync(uint64_t location);
109 
110  virtual size_t getSize() const
111  {
112  return m_NumBlocks * m_NativeBlockSize;
113  }
114 
115  virtual size_t getBlockCount() const
116  {
117  return m_NumBlocks;
118  }
119 
120  virtual size_t getBlockSize() const
121  {
122  return m_BlockSize;
123  }
124 
129  virtual size_t getNativeBlockSize() const
130  {
131  return m_NativeBlockSize;
132  }
133 
134  virtual void pin(uint64_t location);
135  virtual void unpin(uint64_t location);
136 
137  protected:
138  Cache &getCache()
139  {
140  return m_Cache;
141  }
142 
143  Inquiry *getInquiry() const
144  {
145  return m_Inquiry;
146  }
147 
148  private:
149  bool unitReady();
150 
151  bool readSense(Sense *s);
152 
153  bool sendCommand(
154  ScsiCommand *pCommand, uintptr_t pRespBuffer, uint16_t nRespBytes,
155  bool bWrite = false);
156 
157  bool getCapacityInternal(size_t *blockNumber, size_t *blockSize);
158 
159  static void cacheCallback(
160  CacheConstants::CallbackCause cause, uintptr_t loc, uintptr_t page,
161  void *meta);
162 
163  class ScsiController *m_pController;
164  size_t m_nUnit;
165 
166  Cache m_Cache;
167  Inquiry *m_Inquiry;
168 
169  uint64_t m_AlignPoints[8];
170  size_t m_nAlignPoints;
171 
172  size_t m_NumBlocks;
173  size_t m_BlockSize;
174  size_t m_NativeBlockSize;
175 
176  ScsiPeripheralType m_DeviceType;
177 
179  virtual size_t defaultBlockSize()
180  {
181  return m_BlockSize;
182  }
183 };
184 
185 #endif
virtual size_t getBlockSize() const
Gets the block size of the disk.
Definition: ScsiDisk.h:120
Definition: String.h:49
Definition: Disk.h:32
Definition: Cache.h:118
virtual size_t getNativeBlockSize() const
Definition: ScsiDisk.h:129
virtual void getName(String &str)
Definition: ScsiDisk.h:99
virtual size_t getSize() const
Gets the size of the disk.
Definition: ScsiDisk.h:110
virtual size_t defaultBlockSize()
Definition: ScsiDisk.h:179