sati_write_buffer.c revision 230557
1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris*
5230557Sjimharris* GPL LICENSE SUMMARY
6230557Sjimharris*
7230557Sjimharris* Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris*
9230557Sjimharris* This program is free software; you can redistribute it and/or modify
10230557Sjimharris* it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris* published by the Free Software Foundation.
12230557Sjimharris*
13230557Sjimharris* This program is distributed in the hope that it will be useful, but
14230557Sjimharris* WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris* General Public License for more details.
17230557Sjimharris*
18230557Sjimharris* You should have received a copy of the GNU General Public License
19230557Sjimharris* along with this program; if not, write to the Free Software
20230557Sjimharris* Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris* The full GNU General Public License is included in this distribution
22230557Sjimharris* in the file called LICENSE.GPL.
23230557Sjimharris*
24230557Sjimharris* BSD LICENSE
25230557Sjimharris*
26230557Sjimharris* Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris* All rights reserved.
28230557Sjimharris*
29230557Sjimharris* Redistribution and use in source and binary forms, with or without
30230557Sjimharris* modification, are permitted provided that the following conditions
31230557Sjimharris* are met:
32230557Sjimharris*
33230557Sjimharris*   * Redistributions of source code must retain the above copyright
34230557Sjimharris*     notice, this list of conditions and the following disclaimer.
35230557Sjimharris*   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris*     notice, this list of conditions and the following disclaimer in
37230557Sjimharris*     the documentation and/or other materials provided with the
38230557Sjimharris*     distribution.
39230557Sjimharris*
40230557Sjimharris* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris */
52230557Sjimharris
53230557Sjimharris#include <sys/cdefs.h>
54230557Sjimharris__FBSDID("$FreeBSD$");
55230557Sjimharris
56230557Sjimharris/**
57230557Sjimharris* @file
58230557Sjimharris* @brief This file contains the method implementations to translate
59230557Sjimharris*        SCSI Write Buffer command based of the SAT2v07 spec.
60230557Sjimharris*/
61230557Sjimharris
62230557Sjimharris#include <dev/isci/scil/sati_write_buffer.h>
63230557Sjimharris#include <dev/isci/scil/sati_callbacks.h>
64230557Sjimharris#include <dev/isci/scil/sati_util.h>
65230557Sjimharris
66230557Sjimharris#define WRITE_BUFFER_WRITE_DATA              0x02
67230557Sjimharris#define WRITE_BUFFER_DOWNLOAD_SAVE           0x05
68230557Sjimharris#define WRITE_BUFFER_OFFSET_DOWNLOAD_SAVE    0x07
69230557Sjimharris#define BLOCK_SIZE                           512
70230557Sjimharris
71230557Sjimharris/**
72230557Sjimharris* @brief This method will translate the SCSI Write Buffer command
73230557Sjimharris*        into a corresponding ATA Write Buffer and Download Microcode commands.
74230557Sjimharris*        For more information on the parameters passed to this method,
75230557Sjimharris*        please reference sati_translate_command().
76230557Sjimharris*
77230557Sjimharris* @return Indicates if the command translation succeeded.
78230557Sjimharris* @retval SATI_SUCCESS indicates that the translation was supported and occurred
79230557Sjimharris*         without error.
80230557Sjimharris* @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
81230557Sjimharris*         there is a translation failure.
82230557Sjimharris*/
83230557SjimharrisSATI_STATUS sati_write_buffer_translate_command(
84230557Sjimharris   SATI_TRANSLATOR_SEQUENCE_T * sequence,
85230557Sjimharris   void                       * scsi_io,
86230557Sjimharris   void                       * ata_io
87230557Sjimharris)
88230557Sjimharris{
89230557Sjimharris   U8 * cdb = sati_cb_get_cdb_address(scsi_io);
90230557Sjimharris   SATI_STATUS status = SATI_FAILURE;
91230557Sjimharris   U32 allocation_length;
92230557Sjimharris   U32 buffer_offset;
93230557Sjimharris
94230557Sjimharris   allocation_length = ((sati_get_cdb_byte(cdb, 6) << 16) |
95230557Sjimharris                        (sati_get_cdb_byte(cdb, 7) << 8)  |
96230557Sjimharris                        (sati_get_cdb_byte(cdb, 8)));
97230557Sjimharris
98230557Sjimharris   buffer_offset = ((sati_get_cdb_byte(cdb, 3) << 16) |
99230557Sjimharris                    (sati_get_cdb_byte(cdb, 4) << 8)  |
100230557Sjimharris                    (sati_get_cdb_byte(cdb, 5)));
101230557Sjimharris
102230557Sjimharris   sequence->allocation_length = allocation_length;
103230557Sjimharris
104230557Sjimharris   switch(sati_get_cdb_byte(cdb, 1))
105230557Sjimharris   {
106230557Sjimharris      case WRITE_BUFFER_WRITE_DATA:
107230557Sjimharris         if((allocation_length == BLOCK_SIZE) && (buffer_offset == 0) &&
108230557Sjimharris            (sati_get_cdb_byte(cdb, 2) == 0))
109230557Sjimharris         {
110230557Sjimharris            sati_ata_write_buffer_construct(ata_io, sequence);
111230557Sjimharris            sequence->type = SATI_SEQUENCE_WRITE_BUFFER;
112230557Sjimharris            sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
113230557Sjimharris            status = SATI_SUCCESS;
114230557Sjimharris         }
115230557Sjimharris         else
116230557Sjimharris         {
117230557Sjimharris            sati_scsi_sense_data_construct(
118230557Sjimharris               sequence,
119230557Sjimharris               scsi_io,
120230557Sjimharris               SCSI_STATUS_CHECK_CONDITION,
121230557Sjimharris               SCSI_SENSE_ILLEGAL_REQUEST,
122230557Sjimharris               SCSI_ASC_INVALID_FIELD_IN_CDB,
123230557Sjimharris               SCSI_ASCQ_INVALID_FIELD_IN_CDB
124230557Sjimharris            );
125230557Sjimharris
126230557Sjimharris            sequence->state = SATI_SEQUENCE_STATE_FINAL;
127230557Sjimharris            status = SATI_FAILURE_CHECK_RESPONSE_DATA;
128230557Sjimharris         }
129230557Sjimharris      break;
130230557Sjimharris
131230557Sjimharris      case WRITE_BUFFER_DOWNLOAD_SAVE:
132230557Sjimharris
133230557Sjimharris         sati_ata_download_microcode_construct(
134230557Sjimharris            ata_io,
135230557Sjimharris            sequence,
136230557Sjimharris            ATA_MICROCODE_DOWNLOAD_SAVE,
137230557Sjimharris            allocation_length,
138230557Sjimharris            buffer_offset
139230557Sjimharris         );
140230557Sjimharris
141230557Sjimharris         sequence->type = SATI_SEQUENCE_WRITE_BUFFER_MICROCODE;
142230557Sjimharris         sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
143230557Sjimharris         status = SATI_SUCCESS;
144230557Sjimharris      break;
145230557Sjimharris
146230557Sjimharris      case WRITE_BUFFER_OFFSET_DOWNLOAD_SAVE:
147230557Sjimharris         if(((allocation_length & 0x000001FF) == 0) && //Bits 08:00 need to be zero per SAT2v7
148230557Sjimharris            ((buffer_offset & 0x000001FF) == 0)     &&
149230557Sjimharris            (allocation_length <= sequence->device->max_blocks_per_microcode_command) &&
150230557Sjimharris            (allocation_length >= sequence->device->min_blocks_per_microcode_command))
151230557Sjimharris         {
152230557Sjimharris            sati_ata_download_microcode_construct(
153230557Sjimharris               ata_io,
154230557Sjimharris               sequence,
155230557Sjimharris               ATA_MICROCODE_OFFSET_DOWNLOAD,
156230557Sjimharris               allocation_length,
157230557Sjimharris               buffer_offset
158230557Sjimharris            );
159230557Sjimharris
160230557Sjimharris            sequence->type = SATI_SEQUENCE_WRITE_BUFFER_MICROCODE;
161230557Sjimharris            sequence->state = SATI_SEQUENCE_STATE_AWAIT_RESPONSE;
162230557Sjimharris            status = SATI_SUCCESS;
163230557Sjimharris         }
164230557Sjimharris         else
165230557Sjimharris         {
166230557Sjimharris            sati_scsi_sense_data_construct(
167230557Sjimharris               sequence,
168230557Sjimharris               scsi_io,
169230557Sjimharris               SCSI_STATUS_CHECK_CONDITION,
170230557Sjimharris               SCSI_SENSE_ILLEGAL_REQUEST,
171230557Sjimharris               SCSI_ASC_INVALID_FIELD_IN_CDB,
172230557Sjimharris               SCSI_ASCQ_INVALID_FIELD_IN_CDB
173230557Sjimharris            );
174230557Sjimharris
175230557Sjimharris            sequence->state = SATI_SEQUENCE_STATE_FINAL;
176230557Sjimharris            status = SATI_FAILURE_CHECK_RESPONSE_DATA;
177230557Sjimharris         }
178230557Sjimharris      break;
179230557Sjimharris
180230557Sjimharris      default: //unsupported Write Buffer Mode
181230557Sjimharris         sati_scsi_sense_data_construct(
182230557Sjimharris            sequence,
183230557Sjimharris            scsi_io,
184230557Sjimharris            SCSI_STATUS_CHECK_CONDITION,
185230557Sjimharris            SCSI_SENSE_ILLEGAL_REQUEST,
186230557Sjimharris            SCSI_ASC_INVALID_FIELD_IN_CDB,
187230557Sjimharris            SCSI_ASCQ_INVALID_FIELD_IN_CDB
188230557Sjimharris         );
189230557Sjimharris
190230557Sjimharris         sequence->state = SATI_SEQUENCE_STATE_FINAL;
191230557Sjimharris         status = SATI_FAILURE_CHECK_RESPONSE_DATA;
192230557Sjimharris      break;
193230557Sjimharris   }
194230557Sjimharris   return status;
195230557Sjimharris}
196230557Sjimharris
197230557Sjimharris/**
198230557Sjimharris* @brief This method will complete the Write Buffer Translation by checking
199230557Sjimharris*        for ATA errors and then creating a unit attention condition for
200230557Sjimharris*        changed microcode.
201230557Sjimharris*
202230557Sjimharris* @return Indicates if the command translation succeeded.
203230557Sjimharris* @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
204230557Sjimharris*         there is a translation failure.
205230557Sjimharris* @retval SATI_COMPLETE indicates that the translation was supported, occurred without
206230557Sjimharris*         error, and no additional translation is necessary.
207230557Sjimharris*/
208230557SjimharrisSATI_STATUS sati_write_buffer_translate_response(
209230557Sjimharris   SATI_TRANSLATOR_SEQUENCE_T * sequence,
210230557Sjimharris   void                       * scsi_io,
211230557Sjimharris   void                       * ata_io
212230557Sjimharris)
213230557Sjimharris{
214230557Sjimharris   U8 * register_fis = sati_cb_get_d2h_register_fis_address(ata_io);
215230557Sjimharris   U8 ata_status = (U8) sati_get_ata_status(register_fis);
216230557Sjimharris   SATI_STATUS status = SATI_FAILURE;
217230557Sjimharris
218230557Sjimharris   if (ata_status & ATA_STATUS_REG_ERROR_BIT)
219230557Sjimharris   {
220230557Sjimharris      sati_scsi_sense_data_construct(
221230557Sjimharris         sequence,
222230557Sjimharris         scsi_io,
223230557Sjimharris         SCSI_STATUS_CHECK_CONDITION,
224230557Sjimharris         SCSI_SENSE_ABORTED_COMMAND,
225230557Sjimharris         SCSI_ASC_NO_ADDITIONAL_SENSE,
226230557Sjimharris         SCSI_ASCQ_NO_ADDITIONAL_SENSE
227230557Sjimharris      );
228230557Sjimharris      status = SATI_FAILURE_CHECK_RESPONSE_DATA;
229230557Sjimharris   }
230230557Sjimharris   else
231230557Sjimharris   {
232230557Sjimharris      switch(sequence->type)
233230557Sjimharris      {
234230557Sjimharris         case SATI_SEQUENCE_WRITE_BUFFER_MICROCODE:
235230557Sjimharris            sati_scsi_sense_data_construct(
236230557Sjimharris               sequence,
237230557Sjimharris               scsi_io,
238230557Sjimharris               SCSI_STATUS_GOOD,
239230557Sjimharris               SCSI_SENSE_UNIT_ATTENTION,
240230557Sjimharris               SCSI_ASC_MICROCODE_HAS_CHANGED,
241230557Sjimharris               SCSI_ASCQ_MICROCODE_HAS_CHANGED
242230557Sjimharris            );
243230557Sjimharris            status = SATI_COMPLETE;
244230557Sjimharris         break;
245230557Sjimharris
246230557Sjimharris         default:
247230557Sjimharris            status = SATI_COMPLETE;
248230557Sjimharris         break;
249230557Sjimharris      }
250230557Sjimharris   }
251230557Sjimharris
252230557Sjimharris   sequence->state = SATI_SEQUENCE_STATE_FINAL;
253230557Sjimharris   return status;
254230557Sjimharris}
255