2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23 * The full GNU General Public License is included in this distribution
24 * in the file called LICENSE.GPL.
28 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
35 * * Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * * Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in
39 * the documentation and/or other materials provided with the
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
60 * @brief This file contains the method implementations required to
61 * translate the SCSI passthru command.
64 #if !defined(DISABLE_SATI_PASSTHROUGH)
66 #include <dev/isci/scil/sati.h>
67 #include <dev/isci/scil/sati_passthrough.h>
68 #include <dev/isci/scil/sati_util.h>
69 #include <dev/isci/scil/sati_callbacks.h>
70 #include <dev/isci/scil/intel_ata.h>
72 #define PASSTHROUGH_CDB_PROTOCOL_MASK 0x1E
73 #define PASSTHROUGH_CDB_EXTEND_MASK 0x1
74 #define PASSTHROUGH_CDB_CK_COND_MASK 0x20
75 #define PASSTHROUGH_CDB_T_DIR_MASK 0x8
77 #define PASSTHROUGH_ISOLATE_BITS(cdb, index, mask, shift) (((sati_get_cdb_byte(cdb, index) & mask) >> shift))
79 #define PASSTHROUGH_CDB_PROTOCOL(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 1, PASSTHROUGH_CDB_PROTOCOL_MASK, 1)
80 #define PASSTHROUGH_CDB_EXTEND(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 1, PASSTHROUGH_CDB_EXTEND_MASK, 0)
81 #define PASSTHROUGH_CDB_CK_COND(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 2, PASSTHROUGH_CDB_CK_COND_MASK, 5)
82 #define PASSTHROUGH_CDB_T_DIR(cdb) PASSTHROUGH_ISOLATE_BITS(cdb, 2, PASSTHROUGH_CDB_T_DIR_MASK, 3)
84 #define PASSTHROUGH_CDB_MULTIPLE_COUNT(cdb) (sati_get_cdb_byte(cdb, 1) >> 5)
85 #define PASSTHROUGH_CDB_COMMAND(cdb, index) sati_get_cdb_byte(cdb, index)
88 #define PASSTHROUGH_PIO_DATA_IN 0x4
89 #define PASSTHROUGH_PIO_DATA_OUT 0x5
90 #define PASSTHROUGH_DMA 0x6
91 #define PASSTHROUGH_UDMA_DATA_IN 0xA
92 #define PASSTHROUGH_UDMA_DATA_OUT 0xB
93 #define PASSTHROUGH_RETURN_RESPONSE 0xF
96 * @brief This function will check the multiple_count field in the SCSI CDB
97 * and if multiple_count is nonzero the function will check the
98 * ATA command code. Only Read and Write Multiple commands are allowed
99 * when multiple_count is a nonzero value.
101 * @param[in] cdb The SCSI cdb for the ATA pass-through command
104 @retval TRUE - multiple_count is nonzero with a unsupported command
105 @retval FALSE - multiple_count is zero or the command supports a nonzero value
108 BOOL sati_passthrough_multiple_count_error(
114 if(PASSTHROUGH_CDB_MULTIPLE_COUNT(cdb) > 0)
116 if(sati_get_cdb_byte(cdb, 0 ) == SCSI_ATA_PASSTHRU_12)
118 ata_command_code = PASSTHROUGH_CDB_COMMAND(cdb, 9);
122 ata_command_code = PASSTHROUGH_CDB_COMMAND(cdb, 14);
125 switch(ata_command_code)
126 { //MULTICOUNT bit is supported
127 case ATA_READ_MULTIPLE:
128 case ATA_READ_MULTIPLE_EXT:
129 case ATA_WRITE_MULTIPLE:
130 case ATA_WRITE_MULTIPLE_EXT:
131 case ATA_WRITE_MULTIPLE_FUA_EXT:
139 //MULTICOUNT bit is not set
144 * @brief This method will construct the sense data buffer in the user's
145 * sense data buffer location. Additionally, it will set the user's
148 * @param[in] sequence This parameter specifies the translation sequence
149 * for which to construct the sense data.
150 * @param[in] register_fis This parameter specifies the fis from which
152 * @param[in,out] scsi_io This parameter specifies the user's IO request
153 * for which to construct the sense data.
154 * @param[in] scsi_status This parameter specifies the SCSI status
155 * value for the user's IO request.
156 * @param[in] sense_key This parameter specifies the sense key to
157 * be set for the user's IO request.
158 * @param[in] additional_sense_code This parameter specifies the
159 * additional sense code (ASC) key to be set for the user's
161 * @param[in] additional_sense_code_qualifier This parameter specifies
162 * the additional sense code qualifier (ASCQ) key to be set
163 * for the user's IO request.
168 void sati_passthrough_construct_sense(
169 SATI_TRANSLATOR_SEQUENCE_T * sequence,
174 U8 additional_sense_code,
175 U8 additional_sense_code_qualifier
181 unsigned char sector_count_upper;
182 unsigned char lba_upper;
184 #ifdef SATI_TRANSPORT_SUPPORTS_SAS
185 SCI_SSP_RESPONSE_IU_T * rsp_iu = (SCI_SSP_RESPONSE_IU_T*)
186 sati_cb_get_response_iu_address(scsi_io);
188 sati_scsi_common_response_iu_construct(
191 SCSI_FIXED_SENSE_DATA_BASE_LENGTH,
192 SCSI_RESPONSE_DATA_PRES_SENSE_DATA
195 sense_data = (U8*) rsp_iu->data;
196 sense_len = SSP_RESPONSE_IU_MAX_DATA * 4; // dwords to bytes
198 sense_data = sati_cb_get_sense_data_address(scsi_io);
199 sense_len = sati_cb_get_sense_data_length(scsi_io);
200 #endif // SATI_TRANSPORT_SUPPORTS_SAS
202 sati_scsi_sense_data_construct(
207 additional_sense_code,
208 additional_sense_code_qualifier
211 cdb = sati_cb_get_cdb_address(scsi_io);
213 if (sati_get_ata_sector_count_ext(register_fis) != 0) {
214 sector_count_upper = 1;
216 sector_count_upper = 0;
219 if (sati_get_ata_lba_high_ext(register_fis) != 0 ||
220 sati_get_ata_lba_mid_ext(register_fis) != 0 ||
221 sati_get_ata_lba_low_ext(register_fis) != 0) {
227 // Information section
228 sati_set_sense_data_byte(sense_data, sense_len, 3, (U8)sati_get_ata_error(register_fis));
229 sati_set_sense_data_byte(sense_data, sense_len, 4, (U8)sati_get_ata_status(register_fis));
230 sati_set_sense_data_byte(sense_data, sense_len, 5, sati_get_ata_device(register_fis));
231 sati_set_sense_data_byte(sense_data, sense_len, 6, sati_get_ata_sector_count(register_fis));
233 // Command specific section
234 sati_set_sense_data_byte(sense_data, sense_len, 8, (PASSTHROUGH_CDB_EXTEND(cdb) << 7) | (sector_count_upper << 6) | (lba_upper << 5));
235 sati_set_sense_data_byte(sense_data, sense_len, 9, sati_get_ata_lba_low(register_fis));
236 sati_set_sense_data_byte(sense_data, sense_len, 10, sati_get_ata_lba_mid(register_fis));
237 sati_set_sense_data_byte(sense_data, sense_len, 11, sati_get_ata_lba_high(register_fis));
239 sequence->is_sense_response_set = TRUE;
243 * @brief This method will verify that the T_DIR bit matches the protocol bit.
244 * It will additionally set the direction on the sequence.
246 * @param[in,out] sequence This parameter specifies the translation sequence
247 * for which to construct the sense data.
248 * @param[in] cdb The CDB containing the passthrough command
253 SATI_STATUS sati_passthrough_check_direction(
254 SATI_TRANSLATOR_SEQUENCE_T * sequence,
258 if ((sequence->protocol == PASSTHROUGH_PIO_DATA_IN) ||
259 (sequence->protocol == PASSTHROUGH_UDMA_DATA_IN))
261 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x0)
267 sequence->data_direction = SATI_DATA_DIRECTION_IN;
270 else if ((sequence->protocol == PASSTHROUGH_PIO_DATA_OUT) ||
271 (sequence->protocol == PASSTHROUGH_UDMA_DATA_OUT))
273 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x1)
279 sequence->data_direction = SATI_DATA_DIRECTION_OUT;
284 sequence->data_direction = SATI_DATA_DIRECTION_NONE;
287 return SATI_COMPLETE;
290 //******************************************************************************
291 //* P U B L I C M E T H O D S
292 //******************************************************************************
295 * @brief This method will translate the SCSI Passthrough command
296 * into the corresponding ATA command.
298 * @return Indicate if the command translation succeeded.
299 * @retval SATI_SUCCESS This is returned if the command translation was
301 * @retval SATI_FAILURE This is returned if the command translation was
305 SATI_STATUS sati_passthrough_12_translate_command(
306 SATI_TRANSLATOR_SEQUENCE_T * sequence,
315 status = SATI_FAILURE;
317 sequence->type = SATI_SEQUENCE_ATA_PASSTHROUGH_12;
318 sequence->state = SATI_SEQUENCE_STATE_TRANSLATE_DATA;
320 cdb = sati_cb_get_cdb_address(scsi_io);
321 sequence->protocol = PASSTHROUGH_CDB_PROTOCOL (cdb);
322 register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
325 * CAM will send passthrough commands with protocol set to multiword
326 * DMA even though no multiword DMA mode is selected on the device.
327 * This is because some controllers (LSI) will only accept
328 * ATA_PASSTHROUGH commands with DMA mode - not UDMA_IN/OUT.
330 * Since isci does not support multiword DMA, fix this up here.
332 if (sequence->protocol == PASSTHROUGH_DMA)
334 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x1)
336 sequence->protocol = PASSTHROUGH_UDMA_DATA_IN;
340 sequence->protocol = PASSTHROUGH_UDMA_DATA_OUT;
344 if (sati_passthrough_check_direction(sequence, cdb) != SATI_COMPLETE
345 || sati_passthrough_multiple_count_error(cdb)
348 // Fail due to mismatch
349 sati_scsi_sense_data_construct(
352 SCSI_STATUS_CHECK_CONDITION,
353 SCSI_SENSE_ILLEGAL_REQUEST,
354 SCSI_ASC_INVALID_FIELD_IN_CDB,
355 SCSI_ASCQ_INVALID_FIELD_IN_CDB
357 return SATI_FAILURE_CHECK_RESPONSE_DATA;
360 sati_set_ata_features(register_fis, sati_get_cdb_byte(cdb, 3));
361 sati_set_ata_sector_count(register_fis, sati_get_cdb_byte(cdb, 4));
362 sati_set_ata_lba_low(register_fis, sati_get_cdb_byte(cdb, 5));
363 sati_set_ata_lba_mid(register_fis, sati_get_cdb_byte(cdb, 6));
364 sati_set_ata_lba_high(register_fis, sati_get_cdb_byte(cdb, 7));
365 sati_set_ata_device_head(register_fis, sati_get_cdb_byte(cdb, 8));
366 sati_set_ata_command(register_fis, sati_get_cdb_byte(cdb, 9));
368 sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
374 * @brief This method will translate the SCSI Passthrough command
375 * into the corresponding ATA command.
377 * @return Indicate if the command translation succeeded.
378 * @retval SATI_SUCCESS This is returned if the command translation was
380 * @retval SATI_FAILURE This is returned if the command translation was
383 SATI_STATUS sati_passthrough_16_translate_command(
384 SATI_TRANSLATOR_SEQUENCE_T * sequence,
393 status = SATI_FAILURE;
395 sequence->type = SATI_SEQUENCE_ATA_PASSTHROUGH_16;
396 sequence->state = SATI_SEQUENCE_STATE_TRANSLATE_DATA;
398 cdb = sati_cb_get_cdb_address(scsi_io);
399 sequence->protocol = PASSTHROUGH_CDB_PROTOCOL(cdb);
400 register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
403 * CAM will send passthrough commands with protocol set to multiword
404 * DMA even though no multiword DMA mode is selected on the device.
405 * This is because some controllers (LSI) will only accept
406 * ATA_PASSTHROUGH commands with DMA mode - not UDMA_IN/OUT.
408 * Since isci does not support multiword DMA, fix this up here.
410 if (sequence->protocol == PASSTHROUGH_DMA)
412 if (PASSTHROUGH_CDB_T_DIR(cdb) == 0x1)
414 sequence->protocol = PASSTHROUGH_UDMA_DATA_IN;
418 sequence->protocol = PASSTHROUGH_UDMA_DATA_OUT;
422 if (sati_passthrough_check_direction(sequence, cdb) != SATI_COMPLETE
423 || sati_passthrough_multiple_count_error(cdb)
426 // Fail due to mismatch
427 sati_scsi_sense_data_construct(
430 SCSI_STATUS_CHECK_CONDITION,
431 SCSI_SENSE_ILLEGAL_REQUEST,
432 SCSI_ASC_INVALID_FIELD_IN_CDB,
433 SCSI_ASCQ_INVALID_FIELD_IN_CDB
435 return SATI_FAILURE_CHECK_RESPONSE_DATA;
438 if (PASSTHROUGH_CDB_EXTEND(cdb) == 1)
440 sati_set_ata_features_exp(register_fis, sati_get_cdb_byte(cdb, 3));
441 sati_set_ata_sector_count_exp(register_fis, sati_get_cdb_byte(cdb, 5));
442 sati_set_ata_lba_low_exp(register_fis, sati_get_cdb_byte(cdb, 7));
443 sati_set_ata_lba_mid_exp(register_fis, sati_get_cdb_byte(cdb, 9));
444 sati_set_ata_lba_high_exp(register_fis, sati_get_cdb_byte(cdb, 11));
447 if (PASSTHROUGH_CDB_CK_COND(cdb) ||
448 PASSTHROUGH_CDB_PROTOCOL(cdb) == PASSTHROUGH_RETURN_RESPONSE)
450 sequence->is_translate_response_required = TRUE;
453 sati_set_ata_features(register_fis, sati_get_cdb_byte(cdb, 4));
454 sati_set_ata_sector_count(register_fis, sati_get_cdb_byte(cdb, 6));
455 sati_set_ata_lba_low(register_fis, sati_get_cdb_byte(cdb, 8));
456 sati_set_ata_lba_mid(register_fis, sati_get_cdb_byte(cdb, 10));
457 sati_set_ata_lba_high(register_fis, sati_get_cdb_byte(cdb, 12));
458 sati_set_ata_device_head(register_fis, sati_get_cdb_byte(cdb, 13));
459 sati_set_ata_command(register_fis, sati_get_cdb_byte(cdb, 14));
461 sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
467 * @brief This method will translate the ATA command
470 * @return Indicate if the command translation succeeded.
471 * @retval SATI_COMPLETE This is returned if the command translation was
473 * @retval SATI_FAILURE This is returned if the command translation was
476 SATI_STATUS sati_passthrough_translate_response(
477 SATI_TRANSLATOR_SEQUENCE_T * sequence,
485 cdb = sati_cb_get_cdb_address(scsi_io);
486 register_fis = sati_cb_get_d2h_register_fis_address(ata_io);
488 // Check for device errors
489 if (sati_get_ata_status(register_fis) & ATA_STATUS_REG_ERROR_BIT)
491 sati_translate_error(sequence, scsi_io, (U8)sati_get_ata_error(register_fis));
492 return SATI_FAILURE_CHECK_RESPONSE_DATA;
495 sequence->state = SATI_SEQUENCE_STATE_FINAL;
497 // If the user set the check condition bit, fill out the sense data
498 if (PASSTHROUGH_CDB_CK_COND(cdb) ||
499 PASSTHROUGH_CDB_PROTOCOL(cdb) == PASSTHROUGH_RETURN_RESPONSE)
501 sati_passthrough_construct_sense(
505 SCSI_STATUS_CHECK_CONDITION,
506 SCSI_SENSE_RECOVERED_ERROR,
507 SCSI_ASC_NO_ADDITIONAL_SENSE,
508 SCSI_ASCQ_ATA_PASS_THROUGH_INFORMATION_AVAILABLE
510 return SATI_FAILURE_CHECK_RESPONSE_DATA;
513 return SATI_COMPLETE;
516 #endif // !defined(DISABLE_SATI_PASSTHROUGH)