1272209Sandrew/*-
2272209Sandrew * This file is provided under a dual BSD/GPLv2 license.  When using or
3272209Sandrew * redistributing this file, you may do so under either license.
4272209Sandrew *
5272209Sandrew * GPL LICENSE SUMMARY
6272209Sandrew *
7272209Sandrew * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8272209Sandrew *
9272209Sandrew * This program is free software; you can redistribute it and/or modify
10272209Sandrew * it under the terms of version 2 of the GNU General Public License as
11272209Sandrew * published by the Free Software Foundation.
12272209Sandrew *
13272209Sandrew * This program is distributed in the hope that it will be useful, but
14272209Sandrew * WITHOUT ANY WARRANTY; without even the implied warranty of
15272209Sandrew * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16272209Sandrew * General Public License for more details.
17272209Sandrew *
18272209Sandrew * You should have received a copy of the GNU General Public License
19272209Sandrew * along with this program; if not, write to the Free Software
20272209Sandrew * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21272209Sandrew * The full GNU General Public License is included in this distribution
22272209Sandrew * in the file called LICENSE.GPL.
23272209Sandrew *
24272209Sandrew * BSD LICENSE
25272209Sandrew *
26272209Sandrew * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27272209Sandrew * All rights reserved.
28272209Sandrew *
29272209Sandrew * Redistribution and use in source and binary forms, with or without
30272209Sandrew * modification, are permitted provided that the following conditions
31272209Sandrew * are met:
32272209Sandrew *
33272209Sandrew *   * Redistributions of source code must retain the above copyright
34272209Sandrew *     notice, this list of conditions and the following disclaimer.
35272209Sandrew *   * Redistributions in binary form must reproduce the above copyright
36272209Sandrew *     notice, this list of conditions and the following disclaimer in
37272209Sandrew *     the documentation and/or other materials provided with the
38272209Sandrew *     distribution.
39272209Sandrew *
40272209Sandrew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41272209Sandrew * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42272209Sandrew * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43272209Sandrew * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44272209Sandrew * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45272209Sandrew * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46272209Sandrew * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47272209Sandrew * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48272209Sandrew * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49272209Sandrew * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50272209Sandrew * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51272209Sandrew */
52272209Sandrew
53272209Sandrew#include <sys/cdefs.h>
54272209Sandrew__FBSDID("$FreeBSD$");
55272209Sandrew
56272209Sandrew/**
57272209Sandrew * @file
58272209Sandrew * @brief This file contains the method implementations required to
59272209Sandrew *        translate the SCSI read capacity (10 byte) command.
60272209Sandrew */
61272209Sandrew
62272209Sandrew#if !defined(DISABLE_SATI_READ_CAPACITY)
63272209Sandrew
64272209Sandrew#include <dev/isci/scil/sati_read_capacity.h>
65272209Sandrew#include <dev/isci/scil/sati_callbacks.h>
66272209Sandrew#include <dev/isci/scil/sati_util.h>
67272209Sandrew#include <dev/isci/scil/intel_ata.h>
68272209Sandrew#include <dev/isci/scil/intel_scsi.h>
69272209Sandrew
70272209Sandrew/**
71272209Sandrew * @brief This method will translate the read capacity 10 SCSI command into
72272209Sandrew *        an ATA IDENTIFY DEVICE command.
73272209Sandrew *        For more information on the parameters passed to this method,
74272209Sandrew *        please reference sati_translate_command().
75272209Sandrew *
76272209Sandrew * @return Indicate if the command translation succeeded.
77272209Sandrew * @retval SCI_SUCCESS This is returned if the command translation was
78272209Sandrew *         successful.
79272209Sandrew * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if the
80272209Sandrew *         LBA field is not 0, the PMI bit is not 0.
81272209Sandrew */
82272209SandrewSATI_STATUS sati_read_capacity_10_translate_command(
83272209Sandrew   SATI_TRANSLATOR_SEQUENCE_T * sequence,
84272209Sandrew   void                       * scsi_io,
85272209Sandrew   void                       * ata_io
86272209Sandrew)
87272209Sandrew{
88272209Sandrew   U8 * cdb = sati_cb_get_cdb_address(scsi_io);
89272209Sandrew
90272209Sandrew   /**
91272209Sandrew    * SAT dictates:
92272209Sandrew    * - the LBA field must be 0
93272209Sandrew    * - the PMI bit must be 0
94272209Sandrew    */
95272209Sandrew   if (
96272209Sandrew         (
97272209Sandrew            (sati_get_cdb_byte(cdb, 2) != 0)
98272209Sandrew         || (sati_get_cdb_byte(cdb, 3) != 0)
99272209Sandrew         || (sati_get_cdb_byte(cdb, 4) != 0)
100272209Sandrew         || (sati_get_cdb_byte(cdb, 5) != 0)
101272209Sandrew         )
102272209Sandrew         || ((sati_get_cdb_byte(cdb, 8) & SCSI_READ_CAPACITY_PMI_BIT_ENABLE)
103272209Sandrew              == 1)
104272209Sandrew      )
105272209Sandrew   {
106272209Sandrew      sati_scsi_sense_data_construct(
107272209Sandrew         sequence,
108272209Sandrew         scsi_io,
109272209Sandrew         SCSI_STATUS_CHECK_CONDITION,
110272209Sandrew         SCSI_SENSE_ILLEGAL_REQUEST,
111272209Sandrew         SCSI_ASC_INVALID_FIELD_IN_CDB,
112272209Sandrew         SCSI_ASCQ_INVALID_FIELD_IN_CDB
113272209Sandrew      );
114272209Sandrew      return SATI_FAILURE_CHECK_RESPONSE_DATA;
115272209Sandrew   }
116272209Sandrew
117272209Sandrew   // The CDB is properly formed.
118272209Sandrew   sequence->allocation_length = SCSI_READ_CAPACITY_10_DATA_LENGTH;
119272209Sandrew   sequence->type              = SATI_SEQUENCE_READ_CAPACITY_10;
120272209Sandrew
121272209Sandrew   sati_ata_identify_device_construct(ata_io, sequence);
122272209Sandrew   return SATI_SUCCESS;
123272209Sandrew}
124272209Sandrew
125272209Sandrew
126272209Sandrew
127272209Sandrew/**
128272209Sandrew * @brief This method will translate the read capacity 16 SCSI command into
129272209Sandrew *        an ATA IDENTIFY DEVICE command.
130272209Sandrew *        For more information on the parameters passed to this method,
131272209Sandrew *        please reference sati_translate_command().
132272209Sandrew *
133272209Sandrew * @return Indicate if the command translation succeeded.
134272209Sandrew * @retval SCI_SUCCESS This is returned if the command translation was
135272209Sandrew *         successful.
136272209Sandrew * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if the
137272209Sandrew *         LBA field is not 0, the PMI bit is not 0.
138272209Sandrew */
139272209SandrewSATI_STATUS sati_read_capacity_16_translate_command(
140272209Sandrew   SATI_TRANSLATOR_SEQUENCE_T * sequence,
141272209Sandrew   void                       * scsi_io,
142272209Sandrew   void                       * ata_io
143272209Sandrew)
144272209Sandrew{
145272209Sandrew   U8 * cdb = sati_cb_get_cdb_address(scsi_io);
146272209Sandrew
147272209Sandrew   /**
148272209Sandrew    * SAT dictates:
149272209Sandrew    * - the LBA field must be 0
150272209Sandrew    * - the PMI bit must be 0
151272209Sandrew    */
152272209Sandrew   if (
153272209Sandrew         (
154272209Sandrew            (sati_get_cdb_byte(cdb, 2) != 0)
155272209Sandrew         || (sati_get_cdb_byte(cdb, 3) != 0)
156272209Sandrew         || (sati_get_cdb_byte(cdb, 4) != 0)
157272209Sandrew         || (sati_get_cdb_byte(cdb, 5) != 0)
158272209Sandrew         || (sati_get_cdb_byte(cdb, 6) != 0)
159272209Sandrew         || (sati_get_cdb_byte(cdb, 7) != 0)
160272209Sandrew         || (sati_get_cdb_byte(cdb, 8) != 0)
161272209Sandrew         || (sati_get_cdb_byte(cdb, 9) != 0)
162272209Sandrew         )
163272209Sandrew         || ((sati_get_cdb_byte(cdb, 14) & SCSI_READ_CAPACITY_PMI_BIT_ENABLE)
164272209Sandrew              == 1)
165272209Sandrew      )
166272209Sandrew   {
167272209Sandrew      sati_scsi_sense_data_construct(
168272209Sandrew         sequence,
169272209Sandrew         scsi_io,
170272209Sandrew         SCSI_STATUS_CHECK_CONDITION,
171272209Sandrew         SCSI_SENSE_ILLEGAL_REQUEST,
172272209Sandrew         SCSI_ASC_INVALID_FIELD_IN_CDB,
173272209Sandrew         SCSI_ASCQ_INVALID_FIELD_IN_CDB
174272209Sandrew      );
175272209Sandrew      return SATI_FAILURE_CHECK_RESPONSE_DATA;
176272209Sandrew   }
177272209Sandrew
178272209Sandrew   // The CDB is properly formed.
179272209Sandrew   sequence->allocation_length = (sati_get_cdb_byte(cdb, 10) << 24) |
180272209Sandrew                                 (sati_get_cdb_byte(cdb, 11) << 16) |
181272209Sandrew                                 (sati_get_cdb_byte(cdb, 12) << 8)  |
182272209Sandrew                                 (sati_get_cdb_byte(cdb, 13));
183272209Sandrew
184272209Sandrew   sequence->type              = SATI_SEQUENCE_READ_CAPACITY_16;
185272209Sandrew
186272209Sandrew   sati_ata_identify_device_construct(ata_io, sequence);
187272209Sandrew   return SATI_SUCCESS;
188272209Sandrew}
189272209Sandrew
190272209Sandrew/**
191272209Sandrew * @brief This method will translate the ATA Identify Device data into
192272209Sandrew *        SCSI read capacity 10 data.
193272209Sandrew *        For more information on the parameters passed to this method,
194272209Sandrew *        please reference sati_translate_data().
195272209Sandrew *
196272209Sandrew * @return none
197272209Sandrew */
198272209Sandrewvoid sati_read_capacity_10_translate_data(
199272209Sandrew   SATI_TRANSLATOR_SEQUENCE_T * sequence,
200272209Sandrew   void                       * ata_input_data,
201272209Sandrew   void                       * scsi_io
202272209Sandrew)
203272209Sandrew{
204272209Sandrew   U32  lba_low     = 0;
205272209Sandrew   U32  lba_high    = 0;
206272209Sandrew   U32  sector_size = 0;
207272209Sandrew
208272209Sandrew   // Extract the sector information (sector size, logical blocks) from
209272209Sandrew   // the retrieved ATA identify device data.
210272209Sandrew   sati_ata_identify_device_get_sector_info(
211272209Sandrew      (ATA_IDENTIFY_DEVICE_DATA_T*)ata_input_data,
212272209Sandrew      &lba_high,
213272209Sandrew      &lba_low,
214272209Sandrew      &sector_size
215272209Sandrew   );
216272209Sandrew
217272209Sandrew   // SATA drives report a value that is one LBA larger than the last LBA.
218272209Sandrew   // SCSI wants the last LBA.  Make the correction here.  lba_low is
219272209Sandrew   // always decremented since it is an unsigned long the value 0 will
220272209Sandrew   // wrap to 0xFFFFFFFF.
221272209Sandrew   if ((lba_low == 0) && (lba_high == 0))
222272209Sandrew      lba_high -= 1;
223272209Sandrew   lba_low -= 1;
224272209Sandrew
225272209Sandrew   if(lba_high != 0)
226272209Sandrew   {
227272209Sandrew      sati_set_data_byte(sequence, scsi_io, 0, 0xFF);
228272209Sandrew      sati_set_data_byte(sequence, scsi_io, 1, 0xFF);
229272209Sandrew      sati_set_data_byte(sequence, scsi_io, 2, 0xFF);
230272209Sandrew      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 */
256void 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   //Explicitly set byte 12 to 0.  SATI requires that all bytes in the data
312   //response be explicitly set to some value.
313   sati_set_data_byte(sequence, scsi_io, 12, 0);
314
315   //Check Bit 13 of ATA_IDENTIFY_DEVICE_DATA physical_logical_sector_info
316   //(Word 106) is enabled
317   physical_per_logical_enable_bit = (identify_device_data->physical_logical_sector_info
318      & ATA_IDENTIFY_LOGICAL_SECTOR_PER_PHYSICAL_SECTOR_ENABLE);
319
320   //Extract the Physical per logical sector exponent field and calculate
321   //Physical per logical sector value
322   physical_per_logical_sector_exponent = (U8) (identify_device_data->physical_logical_sector_info
323      & ATA_IDENTIFY_LOGICAL_SECTOR_PER_PHYSICAL_SECTOR_MASK);
324   physical_per_logical_sector = 1 << (physical_per_logical_sector_exponent);
325
326   //If the data is valid, fill in the logical blocks per physical block exponent field.
327   //Else set logical blocks per physical block exponent to 1
328   if (physical_per_logical_enable_bit != 0)
329      sati_set_data_byte(
330         sequence,
331         scsi_io,
332         13,
333         (U8)(physical_per_logical_sector_exponent & 0xFF)
334      );
335   else
336      sati_set_data_byte(sequence, scsi_io, 13, 0);
337
338   //Fill in the lowest aligned logical block address field.
339   logical_sector_alignment = identify_device_data->logical_sector_alignment;
340   if (logical_sector_alignment == 0)
341      scsi_logical_sector_alignment = 0;
342   else
343      scsi_logical_sector_alignment = (physical_per_logical_sector - logical_sector_alignment)
344         % physical_per_logical_sector;
345
346   //Follow SAT for reporting tprz and tpe
347   if ((sequence->device->capabilities & SATI_DEVICE_CAP_DSM_TRIM_SUPPORT) &&
348       (sequence->device->capabilities & SATI_DEVICE_CAP_DETERMINISTIC_READ_AFTER_TRIM))
349   {
350      // tpe
351      byte14 |= 0x80;
352      // tprz
353      if (sequence->device->capabilities & SATI_DEVICE_CAP_READ_ZERO_AFTER_TRIM)
354          byte14 |= 0x40;
355   }
356   sati_set_data_byte(
357       sequence,
358       scsi_io,
359       14,
360       (U8)(((scsi_logical_sector_alignment >>8) & 0x3F) | byte14));
361
362   sati_set_data_byte(
363       sequence,
364       scsi_io,
365       15,
366       (U8)(scsi_logical_sector_alignment & 0xFF));
367}
368
369#endif // !defined(DISABLE_SATI_READ_CAPACITY)
370
371