xref: /freebsd/sys/dev/isci/scil/sati_atapi.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
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.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
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.
14  *
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.
19  *
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.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
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
40  *     distribution.
41  *
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.
53  */
54 
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57 
58 /**
59  * @file
60  * @brief This file contains all of the method implementations that
61  *        can be utilized by a user to perform SCSI-to-ATA Translation.
62  *        SATI adheres to the www.t10.org SAT specification.
63  *
64  * For situations where compliance is not observed, the SATI will
65  * return an error indication (most likely INVALID FIELD IN CDB sense data).
66  */
67 
68 #include <dev/isci/scil/sati.h>
69 #include <dev/isci/scil/sati_util.h>
70 #include <dev/isci/scil/sati_atapi.h>
71 #include <dev/isci/scil/sati_callbacks.h>
72 #include <dev/isci/scil/intel_ata.h>
73 #include <dev/isci/scil/intel_scsi.h>
74 #include <dev/isci/scil/intel_sat.h>
75 #include <dev/isci/scil/sati_report_luns.h>
76 
77 
78 //******************************************************************************
79 //* P U B L I C   M E T H O D S
80 //******************************************************************************
81 
82 SATI_STATUS sati_atapi_translate_command(
83    SATI_TRANSLATOR_SEQUENCE_T * sequence,
84    SATI_DEVICE_T              * sati_device,
85    void                       * scsi_io,
86    void                       * atapi_io
87 )
88 {
89    SATI_STATUS   status;
90    U8          * cdb = sati_cb_get_cdb_address(scsi_io);
91 
92    SATA_FIS_REG_H2D_T * register_fis =
93       (SATA_FIS_REG_H2D_T *)sati_cb_get_h2d_register_fis_address(atapi_io);
94 
95    U8 io_direction = SATI_DATA_DIRECTION_IN;
96 
97    //No sense response has been set for the translation sequence yet
98    sequence->is_sense_response_set = FALSE;
99    // Default to no translation response required
100    sequence->is_translate_response_required = FALSE;
101 
102    sequence->number_data_bytes_set = 0;
103    sequence->device  = sati_device;
104    sequence->command_specific_data.scratch = 0;
105 
106    sati_cb_get_data_direction(scsi_io, &io_direction);
107 
108    //set sat protocol.
109    if (io_direction == SATI_DATA_DIRECTION_NONE)
110       sequence->protocol = SAT_PROTOCOL_PACKET_NON_DATA;
111    else if (io_direction == SATI_DATA_DIRECTION_IN)
112       sequence->protocol = SAT_PROTOCOL_PACKET_DMA_DATA_IN;
113    else if (io_direction == SATI_DATA_DIRECTION_OUT)
114       sequence->protocol = SAT_PROTOCOL_PACKET_DMA_DATA_OUT;
115 
116    // We don't send Report Luns command out.
117    if  (sati_get_cdb_byte(cdb, 0) == SCSI_REPORT_LUNS)
118    {
119       status = sati_report_luns_translate_command(
120                   sequence, scsi_io, atapi_io
121                );
122    }
123    else if (sati_cb_get_lun(scsi_io) != 0)
124    {
125       sati_scsi_sense_data_construct(
126          sequence,
127          scsi_io,
128          SCSI_STATUS_CHECK_CONDITION,
129          SCSI_SENSE_ILLEGAL_REQUEST,
130          SCSI_ASC_LOGICAL_UNIT_NOT_SUPPORTED,
131          0
132       );
133       status = SATI_FAILURE_CHECK_RESPONSE_DATA;
134    }
135    else
136    {
137       if (sequence->state == SATI_SEQUENCE_STATE_INCOMPLETE)
138       {  //Request Sense command is required.
139          U8 request_sense_cdb[SATI_ATAPI_REQUEST_SENSE_CDB_LENGTH] =
140             {0x3, 0x0, 0x0, 0x0, 0x12, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
141 
142          //set the sequence->protocol to DATA_IN anyway;
143          sequence->protocol = SAT_PROTOCOL_PACKET_DMA_DATA_IN;
144 
145          //set the cdb for Request Sense using command_specific_data field.
146          memcpy(sequence->command_specific_data.sati_atapi_data.request_sense_cdb,
147                 request_sense_cdb,
148                 SATI_ATAPI_REQUEST_SENSE_CDB_LENGTH
149                );
150       }
151 
152       //build Packet Fis for any other command translation.
153       register_fis->command = ATA_PACKET;
154       register_fis->features |= ATA_PACKET_FEATURE_DMA;
155 
156       register_fis->fis_type = SATA_FIS_TYPE_REGH2D;
157       register_fis->command_flag = 1;
158 
159       status = SATI_SUCCESS;
160    }
161 
162    return status;
163 }
164 
165 
166 SATI_STATUS sati_atapi_translate_command_response(
167    SATI_TRANSLATOR_SEQUENCE_T * sequence,
168    void                       * scsi_io,
169    void                       * atapi_io
170 )
171 {
172    SATI_STATUS   status       = SATI_COMPLETE;
173    U8          * register_fis = sati_cb_get_d2h_register_fis_address(atapi_io);
174    U8            ata_status;
175 
176    /**
177     * If the device fault bit is set in the status register, then
178     * set the sense data and return.
179     */
180    ata_status = (U8) sati_get_ata_status(register_fis);
181    if (ata_status & ATA_STATUS_REG_DEVICE_FAULT_BIT)
182    {
183       sati_scsi_sense_data_construct(
184          sequence,
185          scsi_io,
186          SCSI_STATUS_CHECK_CONDITION,
187          SCSI_SENSE_HARDWARE_ERROR,
188          SCSI_ASC_INTERNAL_TARGET_FAILURE,
189          SCSI_ASCQ_INTERNAL_TARGET_FAILURE
190       );
191 
192       sequence->device->state = SATI_DEVICE_STATE_DEVICE_FAULT_OCCURRED;
193 
194       return SATI_FAILURE_CHECK_RESPONSE_DATA;
195    }
196    else if (ata_status & ATA_STATUS_REG_ERROR_BIT)
197    {
198        //reset the register_fis.
199        memset(register_fis, 0, sizeof(SATA_FIS_REG_D2H_T));
200 
201        //Internal Request Sense command is needed.
202        sequence->state = SATI_SEQUENCE_STATE_INCOMPLETE;
203        return SATI_SEQUENCE_INCOMPLETE;
204    }
205 
206    return status;
207 }
208 
209 void sati_atapi_translate_request_sense_response(
210    SATI_TRANSLATOR_SEQUENCE_T * sequence,
211    void                       * scsi_io,
212    void                       * atapi_io
213 )
214 {
215    //sense data is already in place.
216    SCI_SSP_RESPONSE_IU_T * rsp_iu = (SCI_SSP_RESPONSE_IU_T*)
217                                  sati_cb_get_response_iu_address(scsi_io);
218 
219    sati_scsi_common_response_iu_construct(
220       rsp_iu,
221       SCSI_STATUS_CHECK_CONDITION,
222       sati_scsi_get_sense_data_length(sequence, scsi_io),
223       SCSI_RESPONSE_DATA_PRES_SENSE_DATA
224    );
225 
226    sequence->is_sense_response_set = TRUE;
227 
228    sequence->state = SATI_SEQUENCE_STATE_FINAL;
229 }
230 
231 
232 U32 sati_atapi_translate_number_of_bytes_transferred(
233    SATI_TRANSLATOR_SEQUENCE_T * sequence,
234    void                       * scsi_io,
235    void                       * atapi_io
236 )
237 {
238    U8* cdb = sati_cb_get_cdb_address(scsi_io);
239    U8 response_data;
240    U32 data_length = 0;
241 
242    switch(cdb[0])
243    {
244       case SCSI_MODE_SENSE_10:
245          sati_cb_get_data_byte(scsi_io, 1, &response_data);
246          data_length = response_data+2;
247          break;
248 
249       case 0x51: //READ DISC INFORMATION
250          sati_cb_get_data_byte(scsi_io, 1, &response_data);
251          data_length = response_data+2;
252          break;
253 
254       default:
255          break;
256    }
257 
258    return data_length;
259 }
260