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.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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.
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.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
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
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.
53 #include <sys/cdefs.h>
54 __FBSDID("$FreeBSD$");
58 * @brief This file contains the method implementations required to
59 * translate the SCSI passthru command.
62 #if !defined(DISABLE_SATI_PASSTHROUGH)
64 #include <dev/isci/scil/sati.h>
65 #include <dev/isci/scil/sati_passthrough.h>
66 #include <dev/isci/scil/sati_util.h>
67 #include <dev/isci/scil/sati_callbacks.h>
68 #include <dev/isci/scil/intel_ata.h>
70 #define PASSTHROUGH_CDB_PROTOCOL_MASK 0x1E
71 #define PASSTHROUGH_CDB_EXTEND_MASK 0x1
72 #define PASSTHROUGH_CDB_CK_COND_MASK 0x20
73 #define PASSTHROUGH_CDB_T_DIR_MASK 0x8
75 #define PASSTHROUGH_ISOLATE_BITS(cdb, index, mask, shift) (((sati_get_cdb_byte(cdb, index) & mask) >> shift))
77 #define PASSTHROUGH_CDB_PROTOCOL(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 1, PASSTHROUGH_CDB_PROTOCOL_MASK, 1)
78 #define PASSTHROUGH_CDB_EXTEND(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 1, PASSTHROUGH_CDB_EXTEND_MASK, 0)
79 #define PASSTHROUGH_CDB_CK_COND(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 2, PASSTHROUGH_CDB_CK_COND_MASK, 5)
80 #define PASSTHROUGH_CDB_T_DIR(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 2, PASSTHROUGH_CDB_T_DIR_MASK, 3)
82 #define PASSTHROUGH_CDB_MULTIPLE_COUNT(cdb) (sati_get_cdb_byte(cdb, 1) >> 5)
83 #define PASSTHROUGH_CDB_COMMAND(cdb, index) sati_get_cdb_byte(cdb, index)
86 #define PASSTHROUGH_PIO_DATA_IN 0x4
87 #define PASSTHROUGH_PIO_DATA_OUT 0x5
88 #define PASSTHROUGH_DMA 0x6
89 #define PASSTHROUGH_UDMA_DATA_IN 0xA
90 #define PASSTHROUGH_UDMA_DATA_OUT 0xB
91 #define PASSTHROUGH_RETURN_RESPONSE 0xF
94 * @brief This function will check the multiple_count field in the SCSI CDB
95 * and if multiple_count is nonzero the function will check the
96 * ATA command code. Only Read and Write Multiple commands are allowed
97 * when multiple_count is a nonzero value.
99 * @param[in] cdb The SCSI cdb for the ATA pass-through command
102 @retval TRUE - multiple_count is nonzero with a unsupported command
103 @retval FALSE - multiple_count is zero or the command supports a nonzero value
106 BOOL sati_passthrough_multiple_count_error(
112 if(PASSTHROUGH_CDB_MULTIPLE_COUNT(cdb) > 0)
114 if(sati_get_cdb_byte(cdb, 0 ) == SCSI_ATA_PASSTHRU_12)
116 ata_command_code = PASSTHROUGH_CDB_COMMAND(cdb, 9);
120 ata_command_code = PASSTHROUGH_CDB_COMMAND(cdb, 14);
123 switch(ata_command_code)
124 { //MULTICOUNT bit is supported
125 case ATA_READ_MULTIPLE:
126 case ATA_READ_MULTIPLE_EXT:
127 case ATA_WRITE_MULTIPLE:
128 case ATA_WRITE_MULTIPLE_EXT:
129 case ATA_WRITE_MULTIPLE_FUA_EXT:
137 //MULTICOUNT bit is not set
142 * @brief This method will construct the sense data buffer in the user's
143 * sense data buffer location. Additionally, it will set the user's
146 * @param[in] sequence This parameter specifies the translation sequence
147 * for which to construct the sense data.
148 * @param[in] register_fis This parameter specifies the fis from which
150 * @param[in,out] scsi_io This parameter specifies the user's IO request
151 * for which to construct the sense data.
152 * @param[in] scsi_status This parameter specifies the SCSI status
153 * value for the user's IO request.
154 * @param[in] sense_key This parameter specifies the sense key to
155 * be set for the user's IO request.
156 * @param[in] additional_sense_code This parameter specifies the
157 * additional sense code (ASC) key to be set for the user's
159 * @param[in] additional_sense_code_qualifier This parameter specifies
160 * the additional sense code qualifier (ASCQ) key to be set
161 * for the user's IO request.
166 void sati_passthrough_construct_sense(
167 SATI_TRANSLATOR_SEQUENCE_T * sequence,
172 U8 additional_sense_code,
173 U8 additional_sense_code_qualifier
179 unsigned char sector_count_upper;
180 unsigned char lba_upper;
182 #ifdef SATI_TRANSPORT_SUPPORTS_SAS
183 SCI_SSP_RESPONSE_IU_T * rsp_iu = (SCI_SSP_RESPONSE_IU_T*)
184 sati_cb_get_response_iu_address(scsi_io);
186 sati_scsi_common_response_iu_construct(
189 SCSI_FIXED_SENSE_DATA_BASE_LENGTH,
190 SCSI_RESPONSE_DATA_PRES_SENSE_DATA
193 sense_data = (U8*) rsp_iu->data;
194 sense_len = SSP_RESPONSE_IU_MAX_DATA * 4; // dwords to bytes
196 sense_data = sati_cb_get_sense_data_address(scsi_io);
197 sense_len = sati_cb_get_sense_data_length(scsi_io);
198 #endif // SATI_TRANSPORT_SUPPORTS_SAS
200 sati_scsi_sense_data_construct(
205 additional_sense_code,
206 additional_sense_code_qualifier
209 cdb = sati_cb_get_cdb_address(scsi_io);
211 if (sati_get_ata_sector_count_ext(register_fis) != 0) {
212 sector_count_upper = 1;
214 sector_count_upper = 0;
217 if (sati_get_ata_lba_high_ext(register_fis) != 0 ||
218 sati_get_ata_lba_mid_ext(register_fis) != 0 ||
219 sati_get_ata_lba_low_ext(register_fis) != 0) {
225 // Information section
226 sati_set_sense_data_byte(sense_data, sense_len, 3, (U8)sati_get_ata_error(register_fis));
227 sati_set_sense_data_byte(sense_data, sense_len, 4, (U8)sati_get_ata_status(register_fis));
228 sati_set_sense_data_byte(sense_data, sense_len, 5, sati_get_ata_device(register_fis));
229 sati_set_sense_data_byte(sense_data, sense_len, 6, sati_get_ata_sector_count(register_fis));
231 // Command specific section
232 sati_set_sense_data_byte(sense_data, sense_len, 8, (PASSTHROUGH_CDB_EXTEND(cdb) << 7) | (sector_count_upper << 6) | (lba_upper << 5));
233 sati_set_sense_data_byte(sense_data, sense_len, 9, sati_get_ata_lba_high(register_fis));
234 sati_set_sense_data_byte(sense_data, sense_len, 10, sati_get_ata_lba_mid(register_fis));
235 sati_set_sense_data_byte(sense_data, sense_len, 11, sati_get_ata_lba_low(register_fis));
237 sequence->is_sense_response_set = TRUE;
241 * @brief This method will verify that the T_DIR bit matches the protocol bit.
242 * It will additionally set the direction on the sequence.
244 * @param[in,out] sequence This parameter specifies the translation sequence
245 * for which to construct the sense data.
246 * @param[in] cdb The CDB containing the passthrough command
251 SATI_STATUS sati_passthrough_check_direction(
252 SATI_TRANSLATOR_SEQUENCE_T * sequence,
256 if ((sequence->protocol == PASSTHROUGH_PIO_DATA_IN) ||
257 (sequence->protocol == PASSTHROUGH_UDMA_DATA_IN))
259 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x0)
265 sequence->data_direction = SATI_DATA_DIRECTION_IN;
268 else if ((sequence->protocol == PASSTHROUGH_PIO_DATA_OUT) ||
269 (sequence->protocol == PASSTHROUGH_UDMA_DATA_OUT))
271 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x1)
277 sequence->data_direction = SATI_DATA_DIRECTION_OUT;
282 sequence->data_direction = SATI_DATA_DIRECTION_NONE;
285 return SATI_COMPLETE;
288 //******************************************************************************
289 //* P U B L I C M E T H O D S
290 //******************************************************************************
293 * @brief This method will translate the SCSI Passthrough command
294 * into the corresponding ATA command.
296 * @return Indicate if the command translation succeeded.
297 * @retval SATI_SUCCESS This is returned if the command translation was
299 * @retval SATI_FAILURE This is returned if the command translation was
303 SATI_STATUS sati_passthrough_12_translate_command(
304 SATI_TRANSLATOR_SEQUENCE_T * sequence,
313 status = SATI_FAILURE;
315 sequence->type = SATI_SEQUENCE_ATA_PASSTHROUGH_12;
316 sequence->state = SATI_SEQUENCE_STATE_TRANSLATE_DATA;
318 cdb = sati_cb_get_cdb_address(scsi_io);
319 sequence->protocol = PASSTHROUGH_CDB_PROTOCOL (cdb);
320 register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
323 * CAM will send passthrough commands with protocol set to multiword
324 * DMA even though no multiword DMA mode is selected on the device.
325 * This is because some controllers (LSI) will only accept
326 * ATA_PASSTHROUGH commands with DMA mode - not UDMA_IN/OUT.
328 * Since isci does not support multiword DMA, fix this up here.
330 if (sequence->protocol == PASSTHROUGH_DMA)
332 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x1)
334 sequence->protocol = PASSTHROUGH_UDMA_DATA_IN;
338 sequence->protocol = PASSTHROUGH_UDMA_DATA_OUT;
342 if (sati_passthrough_check_direction(sequence, cdb) != SATI_COMPLETE
343 || sati_passthrough_multiple_count_error(cdb)
346 // Fail due to mismatch
347 sati_scsi_sense_data_construct(
350 SCSI_STATUS_CHECK_CONDITION,
351 SCSI_SENSE_ILLEGAL_REQUEST,
352 SCSI_ASC_INVALID_FIELD_IN_CDB,
353 SCSI_ASCQ_INVALID_FIELD_IN_CDB
355 return SATI_FAILURE_CHECK_RESPONSE_DATA;
358 sati_set_ata_features(register_fis, sati_get_cdb_byte(cdb, 3));
359 sati_set_ata_sector_count(register_fis, sati_get_cdb_byte(cdb, 4));
360 sati_set_ata_lba_low(register_fis, sati_get_cdb_byte(cdb, 5));
361 sati_set_ata_lba_mid(register_fis, sati_get_cdb_byte(cdb, 6));
362 sati_set_ata_lba_high(register_fis, sati_get_cdb_byte(cdb, 7));
363 sati_set_ata_device_head(register_fis, sati_get_cdb_byte(cdb, 8));
364 sati_set_ata_command(register_fis, sati_get_cdb_byte(cdb, 9));
366 sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
372 * @brief This method will translate the SCSI Passthrough command
373 * into the corresponding ATA command.
375 * @return Indicate if the command translation succeeded.
376 * @retval SATI_SUCCESS This is returned if the command translation was
378 * @retval SATI_FAILURE This is returned if the command translation was
381 SATI_STATUS sati_passthrough_16_translate_command(
382 SATI_TRANSLATOR_SEQUENCE_T * sequence,
391 status = SATI_FAILURE;
393 sequence->type = SATI_SEQUENCE_ATA_PASSTHROUGH_16;
394 sequence->state = SATI_SEQUENCE_STATE_TRANSLATE_DATA;
396 cdb = sati_cb_get_cdb_address(scsi_io);
397 sequence->protocol = PASSTHROUGH_CDB_PROTOCOL(cdb);
398 register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
401 * CAM will send passthrough commands with protocol set to multiword
402 * DMA even though no multiword DMA mode is selected on the device.
403 * This is because some controllers (LSI) will only accept
404 * ATA_PASSTHROUGH commands with DMA mode - not UDMA_IN/OUT.
406 * Since isci does not support multiword DMA, fix this up here.
408 if (sequence->protocol == PASSTHROUGH_DMA)
410 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x1)
412 sequence->protocol = PASSTHROUGH_UDMA_DATA_IN;
416 sequence->protocol = PASSTHROUGH_UDMA_DATA_OUT;
420 if (sati_passthrough_check_direction(sequence, cdb) != SATI_COMPLETE
421 || sati_passthrough_multiple_count_error(cdb)
424 // Fail due to mismatch
425 sati_scsi_sense_data_construct(
428 SCSI_STATUS_CHECK_CONDITION,
429 SCSI_SENSE_ILLEGAL_REQUEST,
430 SCSI_ASC_INVALID_FIELD_IN_CDB,
431 SCSI_ASCQ_INVALID_FIELD_IN_CDB
433 return SATI_FAILURE_CHECK_RESPONSE_DATA;
436 if (PASSTHROUGH_CDB_EXTEND(cdb) == 1)
438 sati_set_ata_features_exp(register_fis, sati_get_cdb_byte(cdb, 3));
439 sati_set_ata_sector_count_exp(register_fis, sati_get_cdb_byte(cdb, 5));
440 sati_set_ata_lba_low_exp(register_fis, sati_get_cdb_byte(cdb, 7));
441 sati_set_ata_lba_mid_exp(register_fis, sati_get_cdb_byte(cdb, 9));
442 sati_set_ata_lba_high_exp(register_fis, sati_get_cdb_byte(cdb, 11));
444 sati_set_ata_features(register_fis, sati_get_cdb_byte(cdb, 4));
445 sati_set_ata_sector_count(register_fis, sati_get_cdb_byte(cdb, 6));
446 sati_set_ata_lba_low(register_fis, sati_get_cdb_byte(cdb, 8));
447 sati_set_ata_lba_mid(register_fis, sati_get_cdb_byte(cdb, 10));
448 sati_set_ata_lba_high(register_fis, sati_get_cdb_byte(cdb, 12));
449 sati_set_ata_device_head(register_fis, sati_get_cdb_byte(cdb, 13));
450 sati_set_ata_command(register_fis, sati_get_cdb_byte(cdb, 14));
452 sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
458 * @brief This method will translate the ATA command
461 * @return Indicate if the command translation succeeded.
462 * @retval SATI_COMPLETE This is returned if the command translation was
464 * @retval SATI_FAILURE This is returned if the command translation was
467 SATI_STATUS sati_passthrough_translate_response(
468 SATI_TRANSLATOR_SEQUENCE_T * sequence,
476 cdb = sati_cb_get_cdb_address(scsi_io);
477 register_fis = sati_cb_get_d2h_register_fis_address(ata_io);
479 // Check for device errors
480 if (sati_get_ata_status(register_fis) & ATA_STATUS_REG_ERROR_BIT)
482 sati_translate_error(sequence, scsi_io, (U8)sati_get_ata_error(register_fis));
483 return SATI_FAILURE_CHECK_RESPONSE_DATA;
486 // If the user set the check condition bit, fill out the sense data
487 if (PASSTHROUGH_CDB_CK_COND(cdb) ||
488 PASSTHROUGH_CDB_PROTOCOL(cdb) == PASSTHROUGH_RETURN_RESPONSE)
490 sati_passthrough_construct_sense(
494 SCSI_STATUS_CHECK_CONDITION,
495 SCSI_SENSE_RECOVERED_ERROR,
496 SCSI_ASC_NO_ADDITIONAL_SENSE,
497 SCSI_ASCQ_ATA_PASS_THROUGH_INFORMATION_AVAILABLE
501 sequence->state = SATI_SEQUENCE_STATE_FINAL;
503 return SATI_COMPLETE;
506 #endif // !defined(DISABLE_SATI_PASSTHROUGH)