xref: /freebsd/sys/dev/isci/scil/sati_read_capacity.c (revision f11c7f63056671247335df83a3fe80b94c6616ac)
1 /*-
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  * redistributing this file, you may do so under either license.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
33  *   * Redistributions of source code must retain the above copyright
34  *     notice, this list of conditions and the following disclaimer.
35  *   * Redistributions in binary form must reproduce the above copyright
36  *     notice, this list of conditions and the following disclaimer in
37  *     the documentation and/or other materials provided with the
38  *     distribution.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  */
52 
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
55 
56 /**
57  * @file
58  * @brief This file contains the method implementations required to
59  *        translate the SCSI read capacity (10 byte) command.
60  */
61 
62 #if !defined(DISABLE_SATI_READ_CAPACITY)
63 
64 #include <dev/isci/scil/sati_read_capacity.h>
65 #include <dev/isci/scil/sati_callbacks.h>
66 #include <dev/isci/scil/sati_util.h>
67 #include <dev/isci/scil/intel_ata.h>
68 #include <dev/isci/scil/intel_scsi.h>
69 
70 /**
71  * @brief This method will translate the read capacity 10 SCSI command into
72  *        an ATA IDENTIFY DEVICE command.
73  *        For more information on the parameters passed to this method,
74  *        please reference sati_translate_command().
75  *
76  * @return Indicate if the command translation succeeded.
77  * @retval SCI_SUCCESS This is returned if the command translation was
78  *         successful.
79  * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if the
80  *         LBA field is not 0, the PMI bit is not 0.
81  */
82 SATI_STATUS sati_read_capacity_10_translate_command(
83    SATI_TRANSLATOR_SEQUENCE_T * sequence,
84    void                       * scsi_io,
85    void                       * ata_io
86 )
87 {
88    U8 * cdb = sati_cb_get_cdb_address(scsi_io);
89 
90    /**
91     * SAT dictates:
92     * - the LBA field must be 0
93     * - the PMI bit must be 0
94     */
95    if (
96          (
97             (sati_get_cdb_byte(cdb, 2) != 0)
98          || (sati_get_cdb_byte(cdb, 3) != 0)
99          || (sati_get_cdb_byte(cdb, 4) != 0)
100          || (sati_get_cdb_byte(cdb, 5) != 0)
101          )
102          || ((sati_get_cdb_byte(cdb, 8) & SCSI_READ_CAPACITY_PMI_BIT_ENABLE)
103               == 1)
104       )
105    {
106       sati_scsi_sense_data_construct(
107          sequence,
108          scsi_io,
109          SCSI_STATUS_CHECK_CONDITION,
110          SCSI_SENSE_ILLEGAL_REQUEST,
111          SCSI_ASC_INVALID_FIELD_IN_CDB,
112          SCSI_ASCQ_INVALID_FIELD_IN_CDB
113       );
114       return SATI_FAILURE_CHECK_RESPONSE_DATA;
115    }
116 
117    // The CDB is properly formed.
118    sequence->allocation_length = SCSI_READ_CAPACITY_10_DATA_LENGTH;
119    sequence->type              = SATI_SEQUENCE_READ_CAPACITY_10;
120 
121    sati_ata_identify_device_construct(ata_io, sequence);
122    return SATI_SUCCESS;
123 }
124 
125 
126 
127 /**
128  * @brief This method will translate the read capacity 16 SCSI command into
129  *        an ATA IDENTIFY DEVICE command.
130  *        For more information on the parameters passed to this method,
131  *        please reference sati_translate_command().
132  *
133  * @return Indicate if the command translation succeeded.
134  * @retval SCI_SUCCESS This is returned if the command translation was
135  *         successful.
136  * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if the
137  *         LBA field is not 0, the PMI bit is not 0.
138  */
139 SATI_STATUS sati_read_capacity_16_translate_command(
140    SATI_TRANSLATOR_SEQUENCE_T * sequence,
141    void                       * scsi_io,
142    void                       * ata_io
143 )
144 {
145    U8 * cdb = sati_cb_get_cdb_address(scsi_io);
146 
147    /**
148     * SAT dictates:
149     * - the LBA field must be 0
150     * - the PMI bit must be 0
151     */
152    if (
153          (
154             (sati_get_cdb_byte(cdb, 2) != 0)
155          || (sati_get_cdb_byte(cdb, 3) != 0)
156          || (sati_get_cdb_byte(cdb, 4) != 0)
157          || (sati_get_cdb_byte(cdb, 5) != 0)
158          || (sati_get_cdb_byte(cdb, 6) != 0)
159          || (sati_get_cdb_byte(cdb, 7) != 0)
160          || (sati_get_cdb_byte(cdb, 8) != 0)
161          || (sati_get_cdb_byte(cdb, 9) != 0)
162          )
163          || ((sati_get_cdb_byte(cdb, 14) & SCSI_READ_CAPACITY_PMI_BIT_ENABLE)
164               == 1)
165       )
166    {
167       sati_scsi_sense_data_construct(
168          sequence,
169          scsi_io,
170          SCSI_STATUS_CHECK_CONDITION,
171          SCSI_SENSE_ILLEGAL_REQUEST,
172          SCSI_ASC_INVALID_FIELD_IN_CDB,
173          SCSI_ASCQ_INVALID_FIELD_IN_CDB
174       );
175       return SATI_FAILURE_CHECK_RESPONSE_DATA;
176    }
177 
178    // The CDB is properly formed.
179    sequence->allocation_length = (sati_get_cdb_byte(cdb, 10) << 24) |
180                                  (sati_get_cdb_byte(cdb, 11) << 16) |
181                                  (sati_get_cdb_byte(cdb, 12) << 8)  |
182                                  (sati_get_cdb_byte(cdb, 13));
183 
184    sequence->type              = SATI_SEQUENCE_READ_CAPACITY_16;
185 
186    sati_ata_identify_device_construct(ata_io, sequence);
187    return SATI_SUCCESS;
188 }
189 
190 /**
191  * @brief This method will translate the ATA Identify Device data into
192  *        SCSI read capacity 10 data.
193  *        For more information on the parameters passed to this method,
194  *        please reference sati_translate_data().
195  *
196  * @return none
197  */
198 void sati_read_capacity_10_translate_data(
199    SATI_TRANSLATOR_SEQUENCE_T * sequence,
200    void                       * ata_input_data,
201    void                       * scsi_io
202 )
203 {
204    U32  lba_low     = 0;
205    U32  lba_high    = 0;
206    U32  sector_size = 0;
207 
208    // Extract the sector information (sector size, logical blocks) from
209    // the retrieved ATA identify device data.
210    sati_ata_identify_device_get_sector_info(
211       (ATA_IDENTIFY_DEVICE_DATA_T*)ata_input_data,
212       &lba_high,
213       &lba_low,
214       &sector_size
215    );
216 
217    // SATA drives report a value that is one LBA larger than the last LBA.
218    // SCSI wants the last LBA.  Make the correction here.  lba_low is
219    // always decremented since it is an unsigned long the value 0 will
220    // wrap to 0xFFFFFFFF.
221    if ((lba_low == 0) && (lba_high == 0))
222       lba_high -= 1;
223    lba_low -= 1;
224 
225    if(lba_high != 0)
226    {
227       sati_set_data_byte(sequence, scsi_io, 0, 0xFF);
228       sati_set_data_byte(sequence, scsi_io, 1, 0xFF);
229       sati_set_data_byte(sequence, scsi_io, 2, 0xFF);
230       sati_set_data_byte(sequence, scsi_io, 3, 0xFF);
231    }
232    else
233    {
234       // Build CDB for Read Capacity 10
235       // Fill in the Logical Block Address bytes.
236       sati_set_data_byte(sequence, scsi_io, 0, (U8)((lba_low >> 24) & 0xFF));
237       sati_set_data_byte(sequence, scsi_io, 1, (U8)((lba_low >> 16) & 0xFF));
238       sati_set_data_byte(sequence, scsi_io, 2, (U8)((lba_low >> 8)  & 0xFF));
239       sati_set_data_byte(sequence, scsi_io, 3, (U8)(lba_low & 0xFF));
240    }
241    // Fill in the sector size field.
242    sati_set_data_byte(sequence, scsi_io, 4, (U8)((sector_size >> 24) & 0xFF));
243    sati_set_data_byte(sequence, scsi_io, 5, (U8)((sector_size >> 16) & 0xFF));
244    sati_set_data_byte(sequence, scsi_io, 6, (U8)((sector_size >> 8)  & 0xFF));
245    sati_set_data_byte(sequence, scsi_io, 7, (U8)(sector_size & 0xFF));
246 }
247 
248 /**
249  * @brief This method will translate the ATA Identify Device data into
250  *        SCSI read capacity 16 data.
251  *        For more information on the parameters passed to this method,
252  *        please reference sati_translate_data().
253  *
254  * @return none
255  */
256 void sati_read_capacity_16_translate_data(
257    SATI_TRANSLATOR_SEQUENCE_T * sequence,
258    void                       * ata_input_data,
259    void                       * scsi_io
260 )
261 {
262    U32  lba_low     = 0;
263    U32  lba_high    = 0;
264    U32  sector_size = 0;
265    ATA_IDENTIFY_DEVICE_DATA_T * identify_device_data;
266    U16  physical_per_logical_enable_bit = 0;
267    U8   physical_per_logical_sector_exponent = 0;
268    U16  physical_per_logical_sector = 0;
269    U16  logical_sector_alignment = 0;
270    U16  scsi_logical_sector_alignment = 0;
271    U8   byte14 = 0;
272 
273    //A number of data fields need to be extracted from ATA identify device data
274    identify_device_data = (ATA_IDENTIFY_DEVICE_DATA_T*)ata_input_data;
275 
276    // Extract the sector information (sector size, logical blocks) from
277    // the retrieved ATA identify device data.
278    sati_ata_identify_device_get_sector_info(
279       (ATA_IDENTIFY_DEVICE_DATA_T*)ata_input_data,
280       &lba_high,
281       &lba_low,
282       &sector_size
283    );
284 
285    // SATA drives report a value that is one LBA larger than the last LBA.
286    // SCSI wants the last LBA.  Make the correction here.  lba_low is
287    // always decremented since it is an unsigned long the value 0 will
288    // wrap to 0xFFFFFFFF.
289    if ((lba_low == 0) && (lba_high == 0))
290       lba_high -= 1;
291    lba_low -= 1;
292 
293    // Build the CDB for Read Capacity 16
294    // Fill in the Logical Block Address bytes.
295    sati_set_data_byte(sequence, scsi_io, 0, (U8)((lba_high >> 24) & 0xFF));
296    sati_set_data_byte(sequence, scsi_io, 1, (U8)((lba_high >> 16) & 0xFF));
297    sati_set_data_byte(sequence, scsi_io, 2, (U8)((lba_high >> 8)  & 0xFF));
298    sati_set_data_byte(sequence, scsi_io, 3, (U8)(lba_high & 0xFF));
299 
300    sati_set_data_byte(sequence, scsi_io, 4, (U8)((lba_low >> 24) & 0xFF));
301    sati_set_data_byte(sequence, scsi_io, 5, (U8)((lba_low >> 16) & 0xFF));
302    sati_set_data_byte(sequence, scsi_io, 6, (U8)((lba_low >> 8)  & 0xFF));
303    sati_set_data_byte(sequence, scsi_io, 7, (U8)(lba_low & 0xFF));
304 
305    //Fill in the sector size field.
306    sati_set_data_byte(sequence, scsi_io, 8,  (U8)((sector_size >> 24) & 0xFF));
307    sati_set_data_byte(sequence, scsi_io, 9,  (U8)((sector_size >> 16) & 0xFF));
308    sati_set_data_byte(sequence, scsi_io, 10, (U8)((sector_size >> 8)  & 0xFF));
309    sati_set_data_byte(sequence, scsi_io, 11, (U8)(sector_size & 0xFF));
310 
311    //Check Bit 13 of ATA_IDENTIFY_DEVICE_DATA physical_logical_sector_info
312    //(Word 106) is enabled
313    physical_per_logical_enable_bit = (identify_device_data->physical_logical_sector_info
314       & ATA_IDENTIFY_LOGICAL_SECTOR_PER_PHYSICAL_SECTOR_ENABLE);
315 
316    //Extract the Physical per logical sector exponent field and calculate
317    //Physical per logical sector value
318    physical_per_logical_sector_exponent = (U8) (identify_device_data->physical_logical_sector_info
319       & ATA_IDENTIFY_LOGICAL_SECTOR_PER_PHYSICAL_SECTOR_MASK);
320    physical_per_logical_sector = 1 << (physical_per_logical_sector_exponent);
321 
322    //If the data is valid, fill in the logical blocks per physical block exponent field.
323    //Else set logical blocks per physical block exponent to 1
324    if (physical_per_logical_enable_bit != 0)
325       sati_set_data_byte(
326          sequence,
327          scsi_io,
328          13,
329          (U8)(physical_per_logical_sector_exponent & 0xFF)
330       );
331    else
332       sati_set_data_byte(sequence, scsi_io, 13, 0);
333 
334    //Fill in the lowest aligned logical block address field.
335    logical_sector_alignment = identify_device_data->logical_sector_alignment;
336    if (logical_sector_alignment == 0)
337       scsi_logical_sector_alignment = 0;
338    else
339       scsi_logical_sector_alignment = (physical_per_logical_sector - logical_sector_alignment)
340          % physical_per_logical_sector;
341 
342    //Follow SAT for reporting tprz and tpe
343    if ((sequence->device->capabilities & SATI_DEVICE_CAP_DSM_TRIM_SUPPORT) &&
344        (sequence->device->capabilities & SATI_DEVICE_CAP_DETERMINISTIC_READ_AFTER_TRIM))
345    {
346       // tpe
347       byte14 |= 0x80;
348       // tprz
349       if (sequence->device->capabilities & SATI_DEVICE_CAP_READ_ZERO_AFTER_TRIM)
350           byte14 |= 0x40;
351    }
352    sati_set_data_byte(
353        sequence,
354        scsi_io,
355        14,
356        (U8)(((scsi_logical_sector_alignment >>8) & 0x3F) | byte14));
357 
358    sati_set_data_byte(
359        sequence,
360        scsi_io,
361        15,
362        (U8)(scsi_logical_sector_alignment & 0xFF));
363 }
364 
365 #endif // !defined(DISABLE_SATI_READ_CAPACITY)
366 
367