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 mode sense 10-byte commands.
60 */
61
62#if !defined(DISABLE_SATI_MODE_SENSE)
63
64#include <dev/isci/scil/sati_mode_sense.h>
65#include <dev/isci/scil/sati_mode_sense_10.h>
66#include <dev/isci/scil/sati_mode_pages.h>
67#include <dev/isci/scil/sati_callbacks.h>
68#include <dev/isci/scil/sati_util.h>
69#include <dev/isci/scil/intel_scsi.h>
70#include <dev/isci/scil/intel_ata.h>
71
72//******************************************************************************
73//* P R I V A T E   M E T H O D S
74//******************************************************************************
75
76/**
77 * @brief This method builds the mode parameter header for a 10-byte SCSI
78 *        mode sense data response.  The parameter header is 4 bytes in
79 *        size.
80 *        For more information on the parameters passed to this method,
81 *        please reference sati_translate_command().
82 *
83 * @param[in] identify This parameter specifies the ATA remote device's
84 *            received IDENTIFY DEVICE data.
85 * @param[in] mode_data_length This parameter specifies the amount of data
86 *            to be returned as part of this mode sense request.
87 *
88 * @return This method returns the number of bytes written into the
89 *         mode sense data buffer.
90 */
91static
92U32 sati_mode_sense_10_build_header(
93   SATI_TRANSLATOR_SEQUENCE_T * sequence,
94   void                       * scsi_io,
95   ATA_IDENTIFY_DEVICE_DATA_T * identify,
96   U16                          mode_data_length
97)
98{
99   U8 * cdb = sati_cb_get_cdb_address(scsi_io);
100
101   // Fill in the length of the mode parameter data returned (do not include
102   // the size of the mode data length field in the total).  Adjust the
103   // mode data length to not include the mode data length fields itself
104   // (i.e. subtract 2).
105   mode_data_length -= 2;
106   sati_set_data_byte(sequence, scsi_io, 0, (U8)(mode_data_length >> 8) & 0xFF);
107   sati_set_data_byte(sequence, scsi_io, 1, (U8)(mode_data_length & 0xFF));
108
109   // Medium Type is 0 for SBC devices
110   sati_set_data_byte(sequence, scsi_io, 2, SCSI_MODE_HEADER_MEDIUM_TYPE_SBC);
111
112   // Write Protect (WP), Rsvd, DPOFUA, Rsvd
113   if (sequence->device->capabilities & SATI_DEVICE_CAP_DMA_FUA_ENABLE)
114      sati_set_data_byte(sequence,scsi_io,3,SCSI_MODE_SENSE_HEADER_FUA_ENABLE);
115   else
116      sati_set_data_byte(sequence, scsi_io, 3, 0);
117
118   // Set the reserved bytes to 0.  The LONGLBA field in byte 4 is overridden
119   // later in this method if LLBAA is enabled.
120   sati_set_data_byte(sequence, scsi_io, 4, 0);
121   sati_set_data_byte(sequence, scsi_io, 5, 0);
122
123   // The MSB for the block descriptor length is never used since the
124   // largest block descriptor in this translator is 16-bytes in size.
125   sati_set_data_byte(sequence, scsi_io, 6, 0);
126
127   // Set the LSB block descriptor length if block descriptors are utilized.
128   if (sati_get_cdb_byte(cdb, 1) & SCSI_MODE_SENSE_DBD_ENABLE)
129      sati_set_data_byte(sequence, scsi_io, 7, 0);
130   else
131   {
132      // If Long Logical Block Address are allowed, then the block descriptor
133      // is larger (16 bytes total).
134      if (sati_get_cdb_byte(cdb, 1) & SCSI_MODE_SENSE_LLBAA_ENABLE)
135      {
136         sati_set_data_byte(sequence, scsi_io, 4, 1);
137         sati_set_data_byte(
138            sequence, scsi_io, 7, SCSI_MODE_SENSE_LLBA_BLOCK_DESCRIPTOR_LENGTH
139         );
140      }
141      else
142      {
143         sati_set_data_byte(
144            sequence, scsi_io, 7, SCSI_MODE_SENSE_STD_BLOCK_DESCRIPTOR_LENGTH
145         );
146      }
147   }
148
149   return SCSI_MODE_SENSE_10_HEADER_LENGTH;
150}
151
152static
153U32 sati_mode_sense_10_build_llba_block_descriptor(
154   SATI_TRANSLATOR_SEQUENCE_T * sequence,
155   void                       * scsi_io,
156   ATA_IDENTIFY_DEVICE_DATA_T * identify,
157   U32                          offset
158)
159{
160   U32  lba_low     = 0;
161   U32  lba_high    = 0;
162   U32  sector_size = 0;
163
164   // Extract the sector information (sector size, logical blocks) from
165   // the retrieved ATA identify device data.
166   sati_ata_identify_device_get_sector_info(
167      identify, &lba_low, &lba_high, &sector_size
168   );
169
170   // Fill in the 8-byte logical block area
171   sati_set_data_byte(sequence, scsi_io, offset,   (U8)((lba_high>>24) & 0xFF));
172   sati_set_data_byte(sequence, scsi_io, offset+1, (U8)((lba_high>>16) & 0xFF));
173   sati_set_data_byte(sequence, scsi_io, offset+2, (U8)((lba_high>>8)  & 0xFF));
174   sati_set_data_byte(sequence, scsi_io, offset+3, (U8)(lba_high & 0xFF));
175   sati_set_data_byte(sequence, scsi_io, offset+4, (U8)((lba_low>>24) & 0xFF));
176   sati_set_data_byte(sequence, scsi_io, offset+5, (U8)((lba_low>>16) & 0xFF));
177   sati_set_data_byte(sequence, scsi_io, offset+6, (U8)((lba_low>>8)  & 0xFF));
178   sati_set_data_byte(sequence, scsi_io, offset+7, (U8)(lba_low & 0xFF));
179
180   // Clear the reserved fields.
181   sati_set_data_byte(sequence, scsi_io, offset+8, 0);
182   sati_set_data_byte(sequence, scsi_io, offset+9, 0);
183   sati_set_data_byte(sequence, scsi_io, offset+10, 0);
184   sati_set_data_byte(sequence, scsi_io, offset+11, 0);
185
186   // Fill in the four byte Block Length field
187   sati_set_data_byte(sequence,scsi_io, offset+12, (U8)((sector_size>>24) & 0xFF));
188   sati_set_data_byte(sequence,scsi_io, offset+13, (U8)((sector_size>>16) & 0xFF));
189   sati_set_data_byte(sequence,scsi_io, offset+14, (U8)((sector_size>>8)  & 0xFF));
190   sati_set_data_byte(sequence,scsi_io, offset+15, (U8)(sector_size & 0xFF));
191
192   return SCSI_MODE_SENSE_LLBA_BLOCK_DESCRIPTOR_LENGTH;
193}
194
195/**
196 * @brief This method perform the data translation common to all SCSI MODE
197 *        SENSE 10 byte commands.  This includes building the mode page
198 *        header and block descriptor (if requested).
199 *        For more information on the parameters passed to this method,
200 *        please reference sati_translate_command().
201 *
202 * @param[in] identify This parameter specifies the remote device's IDENTIFY
203 *            DEVICE data to be used during translation.
204 * @param[in] transfer_length This parameter specifies the size of the
205 *            mode page (including header & block descriptor).
206 *
207 * @return This method returns the number of bytes written into the user's
208 *         mode page data buffer.
209 */
210static
211U32 sati_mode_sense_10_translate_data(
212   SATI_TRANSLATOR_SEQUENCE_T * sequence,
213   ATA_IDENTIFY_DEVICE_DATA_T * identify,
214   void                       * scsi_io,
215   U16                          transfer_length
216)
217{
218   U8  * cdb = sati_cb_get_cdb_address(scsi_io);
219   U32   offset;
220
221   offset = sati_mode_sense_10_build_header(
222               sequence, scsi_io, identify, transfer_length
223            );
224
225   // Determine if the caller disabled block descriptors (DBD).  If not,
226   // then generate a block descriptor.
227   if ((sati_get_cdb_byte(cdb, 1) & SCSI_MODE_SENSE_DBD_ENABLE) == 0)
228   {
229      // If the user requested the Long LBA format descriptor, then build
230      // it
231      if (sati_get_cdb_byte(cdb, 1) & SCSI_MODE_SENSE_LLBAA_ENABLE)
232         offset += sati_mode_sense_10_build_llba_block_descriptor(
233                      sequence, scsi_io, identify, offset
234                   );
235      else
236         offset += sati_mode_sense_build_std_block_descriptor(
237                      sequence, scsi_io, identify, offset
238                   );
239   }
240
241   return offset;
242}
243
244//******************************************************************************
245//* P R O T E C T E D   M E T H O D S
246//******************************************************************************
247
248/**
249 * @brief This method will translate the SCSI mode sense 6 byte command
250 *        into corresponding ATA commands.  If the command is well-formed,
251 *        then the translation will result in an ATA IDENTIFY DEVICE
252 *        command.
253 *        For more information on the parameters passed to this method,
254 *        please reference sati_translate_command().
255 *
256 * @return Indicate if the command translation succeeded.
257 * @retval SCI_SUCCESS This is returned if the command translation was
258 *         successful.
259 * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
260 *         sense data has been created as a result of something specified
261 *         in the CDB.
262 */
263SATI_STATUS sati_mode_sense_10_translate_command(
264   SATI_TRANSLATOR_SEQUENCE_T * sequence,
265   void                       * scsi_io,
266   void                       * ata_io
267)
268{
269   U8 * cdb = sati_cb_get_cdb_address(scsi_io);
270
271   sequence->command_specific_data.scratch = 0;
272
273   // Set the data length based on the allocation length field in the CDB.
274   sequence->allocation_length = (sati_get_cdb_byte(cdb, 7) << 8) |
275                                 (sati_get_cdb_byte(cdb, 8));
276
277   return sati_mode_sense_translate_command(sequence, scsi_io, ata_io, 10);
278}
279
280/**
281 * @brief This method will perform data translation from the supplied ATA
282 *        input data (i.e. an ATA IDENTIFY DEVICE block) into a CACHING
283 *        mode page format.  The data will be written into the user's mode
284 *        page data buffer.  This function operates specifically for MODE
285 *        SENSE 10 commands.
286 *        For more information on the parameters passed to this method,
287 *        please reference sati_translate_data().
288 *
289 * @return none.
290 */
291void sati_mode_sense_10_caching_translate_data(
292   SATI_TRANSLATOR_SEQUENCE_T * sequence,
293   void                       * ata_input_data,
294   void                       * scsi_io
295)
296{
297   ATA_IDENTIFY_DEVICE_DATA_T * identify = (ATA_IDENTIFY_DEVICE_DATA_T*)
298                                           ata_input_data;
299   U16   data_length = sati_mode_sense_calculate_page_header(scsi_io, 10)
300                       + SCSI_MODE_PAGE_08_LENGTH;
301   U32   page_offset = sati_mode_sense_10_translate_data(
302                          sequence, identify, scsi_io, data_length
303                       );
304
305   sati_mode_sense_caching_translate_data(
306      sequence, scsi_io, identify, page_offset
307   );
308}
309
310/**
311 * @brief This method will perform data translation from the supplied ATA
312 *        input data (i.e. an ATA IDENTIFY DEVICE block) into a INFORMATIONAL
313 *        EXCEPTIONS CONTROL mode page format.  The data will be written
314 *        into the user's mode page data buffer.  This function operates
315 *        specifically for MODE SENSE 10 commands.
316 *        For more information on the parameters passed to this method,
317 *        please reference sati_translate_data().
318 *
319 * @return none.
320 */
321void sati_mode_sense_10_informational_excp_control_translate_data(
322   SATI_TRANSLATOR_SEQUENCE_T * sequence,
323   void                       * ata_input_data,
324   void                       * scsi_io
325)
326{
327   ATA_IDENTIFY_DEVICE_DATA_T * identify = (ATA_IDENTIFY_DEVICE_DATA_T*)
328                                           ata_input_data;
329   U16   data_length  = sati_mode_sense_calculate_page_header(scsi_io, 10)
330                        + SCSI_MODE_PAGE_1C_LENGTH;
331   U32   page_offset  = sati_mode_sense_10_translate_data(
332                           sequence, identify, scsi_io, data_length
333                        );
334
335   sati_mode_sense_informational_excp_control_translate_data(
336      sequence, scsi_io, identify, page_offset
337   );
338}
339
340/**
341* @brief This method will perform data translation from the supplied ATA
342*        input data (i.e. an ATA IDENTIFY DEVICE block) into a Read Write Error
343*         mode page format.  The data will be written
344*        into the user's mode page data buffer.  This function operates
345*        specifically for MODE SENSE 10 commands.
346*        For more information on the parameters passed to this method,
347*        please reference sati_translate_data().
348*
349* @return none.
350*/
351void sati_mode_sense_10_read_write_error_translate_data(
352   SATI_TRANSLATOR_SEQUENCE_T * sequence,
353   void                       * ata_input_data,
354   void                       * scsi_io
355)
356{
357   ATA_IDENTIFY_DEVICE_DATA_T * identify = (ATA_IDENTIFY_DEVICE_DATA_T*)
358      ata_input_data;
359
360   U16   data_length  = sati_mode_sense_calculate_page_header(scsi_io, 10)
361      + SCSI_MODE_PAGE_01_LENGTH;
362
363   U32   page_offset  = sati_mode_sense_10_translate_data(
364                           sequence, identify, scsi_io, data_length
365                        );
366
367   sati_mode_sense_read_write_error_translate_data(
368      sequence, scsi_io, identify, page_offset
369   );
370}
371
372/**
373* @brief This method will perform data translation from the supplied ATA
374*        input data (i.e. an ATA IDENTIFY DEVICE block) into a Disconnect
375*        Reconnect mode page format.  The data will be written
376*        into the user's mode page data buffer.  This function operates
377*        specifically for MODE SENSE 10 commands.
378*        For more information on the parameters passed to this method,
379*        please reference sati_translate_data().
380*
381* @return none.
382*/
383void sati_mode_sense_10_disconnect_reconnect_translate_data(
384   SATI_TRANSLATOR_SEQUENCE_T * sequence,
385   void                       * ata_input_data,
386   void                       * scsi_io
387)
388{
389   ATA_IDENTIFY_DEVICE_DATA_T * identify = (ATA_IDENTIFY_DEVICE_DATA_T*)
390      ata_input_data;
391
392   U8   data_length = (U8) sati_mode_sense_calculate_page_header(scsi_io, 10)
393      + SCSI_MODE_PAGE_02_LENGTH;
394
395   U32  page_offset = sati_mode_sense_10_translate_data(
396                        sequence, identify, scsi_io, data_length
397                      );
398
399   sati_mode_sense_disconnect_reconnect_translate_data(
400      sequence, scsi_io, identify, page_offset
401   );
402}
403
404/**
405* @brief This method will perform data translation from the supplied ATA
406*        input data (i.e. an ATA IDENTIFY DEVICE block) into a Control
407*         mode page format.  The data will be written
408*        into the user's mode page data buffer.  This function operates
409*        specifically for MODE SENSE 10 commands.
410*        For more information on the parameters passed to this method,
411*        please reference sati_translate_data().
412*
413* @return none.
414*/
415void sati_mode_sense_10_control_translate_data(
416   SATI_TRANSLATOR_SEQUENCE_T * sequence,
417   void                       * ata_input_data,
418   void                       * scsi_io
419)
420{
421   ATA_IDENTIFY_DEVICE_DATA_T * identify = (ATA_IDENTIFY_DEVICE_DATA_T*)
422      ata_input_data;
423
424   U8   data_length = (U8) sati_mode_sense_calculate_page_header(scsi_io, 10)
425      + SCSI_MODE_PAGE_0A_LENGTH;
426
427   U32  page_offset = sati_mode_sense_10_translate_data(
428                         sequence, identify, scsi_io, data_length
429                      );
430
431   sati_mode_sense_control_translate_data(
432      sequence, scsi_io, identify, page_offset
433   );
434}
435
436/**
437* @brief This method will perform data translation from the supplied ATA
438*        input data (i.e. an ATA IDENTIFY DEVICE block) into a Power
439*        Condition mode page format.  The data will be written
440*        into the user's mode page data buffer.  This function operates
441*        specifically for MODE SENSE 10 commands.
442*        For more information on the parameters passed to this method,
443*        please reference sati_translate_data().
444*
445* @return none.
446*/
447void sati_mode_sense_10_power_condition_translate_data(
448   SATI_TRANSLATOR_SEQUENCE_T * sequence,
449   void                       * ata_input_data,
450   void                       * scsi_io
451)
452{
453   ATA_IDENTIFY_DEVICE_DATA_T * identify = (ATA_IDENTIFY_DEVICE_DATA_T*)
454      ata_input_data;
455
456   U8 data_length;
457   U32  page_offset;
458
459   data_length = (U8) sati_mode_sense_calculate_page_header(scsi_io, 10)
460      + SCSI_MODE_PAGE_1A_LENGTH;
461
462   page_offset = sati_mode_sense_10_translate_data(
463      sequence, identify, scsi_io, data_length
464   );
465
466   sati_mode_sense_power_condition_translate_data(
467      sequence, scsi_io, identify, page_offset
468   );
469}
470
471
472/**
473 * @brief This method will perform data translation from the supplied ATA
474 *        input data (i.e. an ATA IDENTIFY DEVICE block) into an ALL
475 *        PAGES mode page format.  The ALL PAGES mode page is basically a
476 *        conglomeration of all mode pages and sub-pages into a single
477 *        page.  The data will be written into the user's mode page
478 *        data buffer.  This function operates specifically for MODE
479 *        SENSE 10 commands.
480 *        For more information on the parameters passed to this method,
481 *        please reference sati_translate_data().
482 *
483 * @return none.
484 */
485void sati_mode_sense_10_all_pages_translate_data(
486   SATI_TRANSLATOR_SEQUENCE_T * sequence,
487   void                       * ata_input_data,
488   void                       * scsi_io
489)
490{
491   ATA_IDENTIFY_DEVICE_DATA_T * identify = (ATA_IDENTIFY_DEVICE_DATA_T*)
492                                           ata_input_data;
493   U8   data_length = (U8) sati_mode_sense_calculate_page_header(scsi_io, 10)
494                           + SCSI_MODE_PAGE_01_LENGTH
495                           + SCSI_MODE_PAGE_02_LENGTH
496                           + SCSI_MODE_PAGE_08_LENGTH
497                           + SCSI_MODE_PAGE_0A_LENGTH
498                           + SCSI_MODE_PAGE_1C_LENGTH;
499   U32  page_offset = sati_mode_sense_10_translate_data(
500                         sequence, identify, scsi_io, data_length
501                      );
502
503   sati_mode_sense_all_pages_translate_data(
504      sequence, scsi_io, identify, page_offset
505   );
506}
507
508#endif // !defined(DISABLE_SATI_MODE_SENSE)
509
510