xref: /freebsd/sys/dev/isci/scil/sati_write_buffer.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1f11c7f63SJim Harris /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3*718cf2ccSPedro F. Giffuni  *
4f11c7f63SJim Harris  * This file is provided under a dual BSD/GPLv2 license.  When using or
5f11c7f63SJim Harris  * redistributing this file, you may do so under either license.
6f11c7f63SJim Harris *
7f11c7f63SJim Harris * GPL LICENSE SUMMARY
8f11c7f63SJim Harris *
9f11c7f63SJim Harris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10f11c7f63SJim Harris *
11f11c7f63SJim Harris * This program is free software; you can redistribute it and/or modify
12f11c7f63SJim Harris * it under the terms of version 2 of the GNU General Public License as
13f11c7f63SJim Harris * published by the Free Software Foundation.
14f11c7f63SJim Harris *
15f11c7f63SJim Harris * This program is distributed in the hope that it will be useful, but
16f11c7f63SJim Harris * WITHOUT ANY WARRANTY; without even the implied warranty of
17f11c7f63SJim Harris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18f11c7f63SJim Harris * General Public License for more details.
19f11c7f63SJim Harris *
20f11c7f63SJim Harris * You should have received a copy of the GNU General Public License
21f11c7f63SJim Harris * along with this program; if not, write to the Free Software
22f11c7f63SJim Harris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23f11c7f63SJim Harris * The full GNU General Public License is included in this distribution
24f11c7f63SJim Harris * in the file called LICENSE.GPL.
25f11c7f63SJim Harris *
26f11c7f63SJim Harris * BSD LICENSE
27f11c7f63SJim Harris *
28f11c7f63SJim Harris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29f11c7f63SJim Harris * All rights reserved.
30f11c7f63SJim Harris *
31f11c7f63SJim Harris * Redistribution and use in source and binary forms, with or without
32f11c7f63SJim Harris * modification, are permitted provided that the following conditions
33f11c7f63SJim Harris * are met:
34f11c7f63SJim Harris *
35f11c7f63SJim Harris *   * Redistributions of source code must retain the above copyright
36f11c7f63SJim Harris *     notice, this list of conditions and the following disclaimer.
37f11c7f63SJim Harris *   * Redistributions in binary form must reproduce the above copyright
38f11c7f63SJim Harris *     notice, this list of conditions and the following disclaimer in
39f11c7f63SJim Harris *     the documentation and/or other materials provided with the
40f11c7f63SJim Harris *     distribution.
41f11c7f63SJim Harris *
42f11c7f63SJim Harris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43f11c7f63SJim Harris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44f11c7f63SJim Harris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45f11c7f63SJim Harris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46f11c7f63SJim Harris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47f11c7f63SJim Harris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48f11c7f63SJim Harris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49f11c7f63SJim Harris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50f11c7f63SJim Harris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51f11c7f63SJim Harris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52f11c7f63SJim Harris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53f11c7f63SJim Harris  */
54f11c7f63SJim Harris 
55f11c7f63SJim Harris #include <sys/cdefs.h>
56f11c7f63SJim Harris /**
57f11c7f63SJim Harris * @file
58f11c7f63SJim Harris * @brief This file contains the method implementations to translate
59f11c7f63SJim Harris *        SCSI Write Buffer command based of the SAT2v07 spec.
60f11c7f63SJim Harris */
61f11c7f63SJim Harris 
62f11c7f63SJim Harris #include <dev/isci/scil/sati_write_buffer.h>
63f11c7f63SJim Harris #include <dev/isci/scil/sati_callbacks.h>
64f11c7f63SJim Harris #include <dev/isci/scil/sati_util.h>
65f11c7f63SJim Harris 
66f11c7f63SJim Harris #define WRITE_BUFFER_WRITE_DATA              0x02
67f11c7f63SJim Harris #define WRITE_BUFFER_DOWNLOAD_SAVE           0x05
68f11c7f63SJim Harris #define WRITE_BUFFER_OFFSET_DOWNLOAD_SAVE    0x07
69cc318662SJim Harris #define DOWNLOAD_MICROCODE_BLOCK_SIZE        512
70f11c7f63SJim Harris 
71f11c7f63SJim Harris /**
72f11c7f63SJim Harris * @brief This method will translate the SCSI Write Buffer command
73f11c7f63SJim Harris *        into a corresponding ATA Write Buffer and Download Microcode commands.
74f11c7f63SJim Harris *        For more information on the parameters passed to this method,
75f11c7f63SJim Harris *        please reference sati_translate_command().
76f11c7f63SJim Harris *
77f11c7f63SJim Harris * @return Indicates if the command translation succeeded.
78f11c7f63SJim Harris * @retval SATI_SUCCESS indicates that the translation was supported and occurred
79f11c7f63SJim Harris *         without error.
80f11c7f63SJim Harris * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
81f11c7f63SJim Harris *         there is a translation failure.
82f11c7f63SJim Harris */
sati_write_buffer_translate_command(SATI_TRANSLATOR_SEQUENCE_T * sequence,void * scsi_io,void * ata_io)83f11c7f63SJim Harris SATI_STATUS sati_write_buffer_translate_command(
84f11c7f63SJim Harris    SATI_TRANSLATOR_SEQUENCE_T * sequence,
85f11c7f63SJim Harris    void                       * scsi_io,
86f11c7f63SJim Harris    void                       * ata_io
87f11c7f63SJim Harris )
88f11c7f63SJim Harris {
89f11c7f63SJim Harris    U8 * cdb = sati_cb_get_cdb_address(scsi_io);
90f11c7f63SJim Harris    SATI_STATUS status = SATI_FAILURE;
91f11c7f63SJim Harris    U32 allocation_length;
92cc318662SJim Harris    U32 allocation_blocks;
93f11c7f63SJim Harris    U32 buffer_offset;
94f11c7f63SJim Harris 
95f11c7f63SJim Harris    allocation_length = ((sati_get_cdb_byte(cdb, 6) << 16) |
96f11c7f63SJim Harris                         (sati_get_cdb_byte(cdb, 7) << 8)  |
97f11c7f63SJim Harris                         (sati_get_cdb_byte(cdb, 8)));
98f11c7f63SJim Harris 
99f11c7f63SJim Harris    buffer_offset = ((sati_get_cdb_byte(cdb, 3) << 16) |
100f11c7f63SJim Harris                     (sati_get_cdb_byte(cdb, 4) << 8)  |
101f11c7f63SJim Harris                     (sati_get_cdb_byte(cdb, 5)));
102f11c7f63SJim Harris 
103f11c7f63SJim Harris    sequence->allocation_length = allocation_length;
104cc318662SJim Harris    allocation_blocks = allocation_length / DOWNLOAD_MICROCODE_BLOCK_SIZE;
105f11c7f63SJim Harris 
106f11c7f63SJim Harris    switch(sati_get_cdb_byte(cdb, 1))
107f11c7f63SJim Harris    {
108f11c7f63SJim Harris       case WRITE_BUFFER_WRITE_DATA:
109cc318662SJim Harris          if((allocation_length == DOWNLOAD_MICROCODE_BLOCK_SIZE) &&
110cc318662SJim Harris             (buffer_offset == 0) &&
111f11c7f63SJim Harris             (sati_get_cdb_byte(cdb, 2) == 0))
112f11c7f63SJim Harris          {
113f11c7f63SJim Harris             sati_ata_write_buffer_construct(ata_io, sequence);
114f11c7f63SJim Harris             sequence->type = SATI_SEQUENCE_WRITE_BUFFER;
115f11c7f63SJim Harris             sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
116f11c7f63SJim Harris             status = SATI_SUCCESS;
117f11c7f63SJim Harris          }
118f11c7f63SJim Harris          else
119f11c7f63SJim Harris          {
120f11c7f63SJim Harris             sati_scsi_sense_data_construct(
121f11c7f63SJim Harris                sequence,
122f11c7f63SJim Harris                scsi_io,
123f11c7f63SJim Harris                SCSI_STATUS_CHECK_CONDITION,
124f11c7f63SJim Harris                SCSI_SENSE_ILLEGAL_REQUEST,
125f11c7f63SJim Harris                SCSI_ASC_INVALID_FIELD_IN_CDB,
126f11c7f63SJim Harris                SCSI_ASCQ_INVALID_FIELD_IN_CDB
127f11c7f63SJim Harris             );
128f11c7f63SJim Harris 
129f11c7f63SJim Harris             sequence->state = SATI_SEQUENCE_STATE_FINAL;
130f11c7f63SJim Harris             status = SATI_FAILURE_CHECK_RESPONSE_DATA;
131f11c7f63SJim Harris          }
132f11c7f63SJim Harris       break;
133f11c7f63SJim Harris 
134f11c7f63SJim Harris       case WRITE_BUFFER_DOWNLOAD_SAVE:
135f11c7f63SJim Harris 
136f11c7f63SJim Harris          sati_ata_download_microcode_construct(
137f11c7f63SJim Harris             ata_io,
138f11c7f63SJim Harris             sequence,
139f11c7f63SJim Harris             ATA_MICROCODE_DOWNLOAD_SAVE,
140f11c7f63SJim Harris             allocation_length,
141f11c7f63SJim Harris             buffer_offset
142f11c7f63SJim Harris          );
143f11c7f63SJim Harris 
144f11c7f63SJim Harris          sequence->type = SATI_SEQUENCE_WRITE_BUFFER_MICROCODE;
145f11c7f63SJim Harris          sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
146f11c7f63SJim Harris          status = SATI_SUCCESS;
147f11c7f63SJim Harris       break;
148f11c7f63SJim Harris 
149f11c7f63SJim Harris       case WRITE_BUFFER_OFFSET_DOWNLOAD_SAVE:
150f11c7f63SJim Harris          if(((allocation_length & 0x000001FF) == 0) && //Bits 08:00 need to be zero per SAT2v7
151f11c7f63SJim Harris             ((buffer_offset & 0x000001FF) == 0)     &&
152cc318662SJim Harris             (allocation_blocks <= sequence->device->max_blocks_per_microcode_command) &&
153cc318662SJim Harris             ((allocation_blocks >= sequence->device->min_blocks_per_microcode_command) ||
154cc318662SJim Harris             (allocation_length == 0)))
155f11c7f63SJim Harris          {
156f11c7f63SJim Harris             sati_ata_download_microcode_construct(
157f11c7f63SJim Harris                ata_io,
158f11c7f63SJim Harris                sequence,
159f11c7f63SJim Harris                ATA_MICROCODE_OFFSET_DOWNLOAD,
160f11c7f63SJim Harris                allocation_length,
161f11c7f63SJim Harris                buffer_offset
162f11c7f63SJim Harris             );
163f11c7f63SJim Harris 
164f11c7f63SJim Harris             sequence->type = SATI_SEQUENCE_WRITE_BUFFER_MICROCODE;
165f11c7f63SJim Harris             sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
166f11c7f63SJim Harris             status = SATI_SUCCESS;
167f11c7f63SJim Harris          }
168f11c7f63SJim Harris          else
169f11c7f63SJim Harris          {
170f11c7f63SJim Harris             sati_scsi_sense_data_construct(
171f11c7f63SJim Harris                sequence,
172f11c7f63SJim Harris                scsi_io,
173f11c7f63SJim Harris                SCSI_STATUS_CHECK_CONDITION,
174f11c7f63SJim Harris                SCSI_SENSE_ILLEGAL_REQUEST,
175f11c7f63SJim Harris                SCSI_ASC_INVALID_FIELD_IN_CDB,
176f11c7f63SJim Harris                SCSI_ASCQ_INVALID_FIELD_IN_CDB
177f11c7f63SJim Harris             );
178f11c7f63SJim Harris 
179f11c7f63SJim Harris             sequence->state = SATI_SEQUENCE_STATE_FINAL;
180f11c7f63SJim Harris             status = SATI_FAILURE_CHECK_RESPONSE_DATA;
181f11c7f63SJim Harris          }
182f11c7f63SJim Harris       break;
183f11c7f63SJim Harris 
184f11c7f63SJim Harris       default: //unsupported Write Buffer Mode
185f11c7f63SJim Harris          sati_scsi_sense_data_construct(
186f11c7f63SJim Harris             sequence,
187f11c7f63SJim Harris             scsi_io,
188f11c7f63SJim Harris             SCSI_STATUS_CHECK_CONDITION,
189f11c7f63SJim Harris             SCSI_SENSE_ILLEGAL_REQUEST,
190f11c7f63SJim Harris             SCSI_ASC_INVALID_FIELD_IN_CDB,
191f11c7f63SJim Harris             SCSI_ASCQ_INVALID_FIELD_IN_CDB
192f11c7f63SJim Harris          );
193f11c7f63SJim Harris 
194f11c7f63SJim Harris          sequence->state = SATI_SEQUENCE_STATE_FINAL;
195f11c7f63SJim Harris          status = SATI_FAILURE_CHECK_RESPONSE_DATA;
196f11c7f63SJim Harris       break;
197f11c7f63SJim Harris    }
198f11c7f63SJim Harris    return status;
199f11c7f63SJim Harris }
200f11c7f63SJim Harris 
201f11c7f63SJim Harris /**
202f11c7f63SJim Harris * @brief This method will complete the Write Buffer Translation by checking
203f11c7f63SJim Harris *        for ATA errors and then creating a unit attention condition for
204f11c7f63SJim Harris *        changed microcode.
205f11c7f63SJim Harris *
206f11c7f63SJim Harris * @return Indicates if the command translation succeeded.
207f11c7f63SJim Harris * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
208f11c7f63SJim Harris *         there is a translation failure.
209f11c7f63SJim Harris * @retval SATI_COMPLETE indicates that the translation was supported, occurred without
210f11c7f63SJim Harris *         error, and no additional translation is necessary.
211f11c7f63SJim Harris */
sati_write_buffer_translate_response(SATI_TRANSLATOR_SEQUENCE_T * sequence,void * scsi_io,void * ata_io)212f11c7f63SJim Harris SATI_STATUS sati_write_buffer_translate_response(
213f11c7f63SJim Harris    SATI_TRANSLATOR_SEQUENCE_T * sequence,
214f11c7f63SJim Harris    void                       * scsi_io,
215f11c7f63SJim Harris    void                       * ata_io
216f11c7f63SJim Harris )
217f11c7f63SJim Harris {
218f11c7f63SJim Harris    U8 * register_fis = sati_cb_get_d2h_register_fis_address(ata_io);
219f11c7f63SJim Harris    U8 ata_status = (U8) sati_get_ata_status(register_fis);
220f11c7f63SJim Harris    SATI_STATUS status = SATI_FAILURE;
221f11c7f63SJim Harris 
222f11c7f63SJim Harris    if (ata_status & ATA_STATUS_REG_ERROR_BIT)
223f11c7f63SJim Harris    {
224f11c7f63SJim Harris       sati_scsi_sense_data_construct(
225f11c7f63SJim Harris          sequence,
226f11c7f63SJim Harris          scsi_io,
227f11c7f63SJim Harris          SCSI_STATUS_CHECK_CONDITION,
228f11c7f63SJim Harris          SCSI_SENSE_ABORTED_COMMAND,
229f11c7f63SJim Harris          SCSI_ASC_NO_ADDITIONAL_SENSE,
230f11c7f63SJim Harris          SCSI_ASCQ_NO_ADDITIONAL_SENSE
231f11c7f63SJim Harris       );
232f11c7f63SJim Harris       status = SATI_FAILURE_CHECK_RESPONSE_DATA;
233f11c7f63SJim Harris    }
234f11c7f63SJim Harris    else
235f11c7f63SJim Harris    {
236f11c7f63SJim Harris       switch(sequence->type)
237f11c7f63SJim Harris       {
238f11c7f63SJim Harris          case SATI_SEQUENCE_WRITE_BUFFER_MICROCODE:
239f11c7f63SJim Harris             sati_scsi_sense_data_construct(
240f11c7f63SJim Harris                sequence,
241f11c7f63SJim Harris                scsi_io,
242f11c7f63SJim Harris                SCSI_STATUS_GOOD,
243f11c7f63SJim Harris                SCSI_SENSE_UNIT_ATTENTION,
244f11c7f63SJim Harris                SCSI_ASC_MICROCODE_HAS_CHANGED,
245f11c7f63SJim Harris                SCSI_ASCQ_MICROCODE_HAS_CHANGED
246f11c7f63SJim Harris             );
247f11c7f63SJim Harris             status = SATI_COMPLETE;
248f11c7f63SJim Harris          break;
249f11c7f63SJim Harris 
250f11c7f63SJim Harris          default:
251f11c7f63SJim Harris             status = SATI_COMPLETE;
252f11c7f63SJim Harris          break;
253f11c7f63SJim Harris       }
254f11c7f63SJim Harris    }
255f11c7f63SJim Harris 
256f11c7f63SJim Harris    sequence->state = SATI_SEQUENCE_STATE_FINAL;
257f11c7f63SJim Harris    return status;
258f11c7f63SJim Harris }
259