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 reassign blocks command.
62 #if !defined(DISABLE_SATI_REASSIGN_BLOCKS)
64 #include <dev/isci/scil/sati_reassign_blocks.h>
65 #include <dev/isci/scil/sati_callbacks.h>
66 #include <dev/isci/scil/sati_move.h>
67 #include <dev/isci/scil/sati_write.h>
68 #include <dev/isci/scil/sati_translator_sequence.h>
69 #include <dev/isci/scil/sati_util.h>
70 #include <dev/isci/scil/intel_scsi.h>
73 //******************************************************************************
74 //* P R I V A T E M E T H O D S
75 //******************************************************************************
76 // static SATI_REASSIGN_BLOCKS_PROCESSING_STATE_T reassign_blocks_processing_state;
79 * @brief This method copies short 24bits LBA bytes to the command register
80 * @return Indicate if the method was successfully completed.
81 * @retval SATI_SUCCESS This is returned in all other cases.
84 void set_current_lba(U8 * lba, void * ata_io)
86 U8 * register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
88 sati_set_ata_lba_low(register_fis, lba[0]);
89 sati_set_ata_lba_mid(register_fis, lba[1]);
90 sati_set_ata_lba_high(register_fis, lba[2]);
91 sati_set_ata_device_head(register_fis, ATA_DEV_HEAD_REG_LBA_MODE_ENABLE | (lba[3] & 0x0F));
97 * @brief This method copies short 48bits LBA bytes to the command register
98 * @return Indicate if the method was successfully completed.
99 * @retval SATI_SUCCESS This is returned in all other cases.
102 void set_current_long_lba(U8 * lba, void * ata_io)
104 U8 * register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
106 sati_set_ata_lba_low(register_fis, lba[0]);
107 sati_set_ata_lba_mid(register_fis, lba[1]);
108 sati_set_ata_lba_high(register_fis, lba[2]);
109 sati_set_ata_lba_low_exp(register_fis, lba[3]);
110 sati_set_ata_lba_mid_exp(register_fis, lba[4]);
111 sati_set_ata_lba_high_exp(register_fis, lba[5]);
115 * @brief This method performs the SCSI VERIFY command translation
118 * - setting the command register
119 * - setting the device head register
120 * - filling in fields in the SATI_TRANSLATOR_SEQUENCE object.
121 * For more information on the parameters passed to this method,
122 * please reference sati_translate_command().
124 * @return Indicate if the method was successfully completed.
125 * @retval SATI_SUCCESS This is returned in all other cases.
128 SATI_STATUS sati_reassign_blocks_verify_command(
129 SATI_TRANSLATOR_SEQUENCE_T * sequence,
134 U8 * register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
136 sati_ata_non_data_command(ata_io, sequence);
138 // Ensure the device supports the 48 bit feature set.
139 if (sequence->device->capabilities & SATI_DEVICE_CAP_48BIT_ENABLE)
140 sati_set_ata_command(register_fis, ATA_READ_VERIFY_SECTORS_EXT);
142 sati_set_ata_command(register_fis, ATA_READ_VERIFY_SECTORS);
148 * @brief This method performs the SCSI Write sector command translation
151 * - setting the command register
152 * - setting the device head register
153 * - filling in fields in the SATI_TRANSLATOR_SEQUENCE object.
154 * For more information on the parameters passed to this method,
155 * please reference sati_translate_command().
157 * @return Indicate if the method was successfully completed.
158 * @retval SATI_SUCCESS This is returned in all other cases.
161 SATI_STATUS sati_reassign_blocks_write_command(
162 SATI_TRANSLATOR_SEQUENCE_T * sequence,
167 U8 * register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
169 sati_ata_non_data_command(ata_io, sequence);
170 sequence->data_direction = SATI_DATA_DIRECTION_OUT;
172 // sati_set_ata_sector_count(register_fis, 1);
173 // status=sati_move_set_sector_count(sequence,scsi_io,ata_io,1,0);
175 // Ensure the device supports the 48 bit feature set.
176 if (sequence->device->capabilities & SATI_DEVICE_CAP_48BIT_ENABLE)
177 sati_set_ata_command(register_fis, ATA_WRITE_DMA_EXT);
179 sati_set_ata_command(register_fis, ATA_WRITE_DMA);
181 return SATI_SUCCESS; //sati_move_set_sector_count(sequence,scsi_io,ata_io,1,0);
185 * @brief This method performs the retrieving of parameter LBA praparation and setting
186 * processing flags before/after calling SCSI Verify sector command.
187 * @return Indicate if the method was successfully completed.
188 * @retval SATI_SUCCESS This is returned in all other cases.
191 SATI_STATUS sati_reassign_blocks_verify_condition(
192 SATI_TRANSLATOR_SEQUENCE_T * sequence,
197 U8 current_lba_bytes[8] = {0,0,0,0,0,0,0,0};
201 SATI_STATUS status = SATI_FAILURE_CHECK_RESPONSE_DATA;
203 lba_offset = sequence->command_specific_data.reassign_blocks_process_state.lba_offset;
204 page_size = sequence->command_specific_data.reassign_blocks_process_state.lba_size;
206 for(index = 0; index < page_size; index++)
208 sati_get_data_byte(sequence, scsi_io, lba_offset+index, ¤t_lba_bytes[index]);
212 set_current_lba(current_lba_bytes, ata_io);
214 set_current_long_lba(current_lba_bytes, ata_io);
216 status = sati_reassign_blocks_verify_command(sequence, scsi_io, ata_io);
217 sequence->command_specific_data.reassign_blocks_process_state.ata_command_sent_for_current_lba++;
218 sequence->command_specific_data.reassign_blocks_process_state.ata_command_status = SATI_REASSIGN_BLOCKS_READY_TO_SEND;
223 * @brief This method performs the retrieving of parameter LBA praparation and setting
224 * processing flags before/after calling SCSI Write sector command.
225 * @return Indicate if the method was successfully completed.
226 * @retval SATI_SUCCESS This is returned in all other cases.
229 SATI_STATUS sati_reassign_blocks_write_condition(
230 SATI_TRANSLATOR_SEQUENCE_T * sequence,
235 U8 current_lba_bytes[8] = {0,0,0,0,0,0,0,0};
239 SATI_STATUS status = SATI_FAILURE_CHECK_RESPONSE_DATA;
241 lba_offset = sequence->command_specific_data.reassign_blocks_process_state.lba_offset;
242 page_size = sequence->command_specific_data.reassign_blocks_process_state.lba_size;
244 for(index = 0; index < page_size; index++)
246 sati_get_data_byte(sequence, scsi_io, lba_offset+index, ¤t_lba_bytes[index]);
250 set_current_lba(current_lba_bytes, ata_io);
252 set_current_long_lba(current_lba_bytes, ata_io);
254 status = sati_reassign_blocks_write_command(sequence, scsi_io, ata_io);
255 sequence->command_specific_data.reassign_blocks_process_state.ata_command_sent_for_current_lba++;
256 sequence->command_specific_data.reassign_blocks_process_state.ata_command_status = SATI_REASSIGN_BLOCKS_READY_TO_SEND;
262 * @brief This method will perform the pre-processing of Reassign Blocks command and parameter.
265 void sati_reassign_blocks_initial_processing(
266 SATI_TRANSLATOR_SEQUENCE_T * sequence,
276 U32 data_transfer_length;
277 U8 header_bytes[4]={0,0,0,0};
279 U8 * cdb = sati_cb_get_cdb_address(scsi_io);
281 //A long LBA (LONGLBA) bit set to zero specifies that the REASSIGN BLOCKS defective LBA list contains four-byte LBAs.
282 //A LONGLBA bit set to one specifies that the REASSIGN BLOCKS defective LBA list contains eight-byte LBAs.
283 if ((sati_get_cdb_byte(cdb, 1) & SCSI_REASSIGN_BLOCKS_LONGLBA_BIT) == 0)
286 page_size = 4; //beginning of lba list
294 //The long list (LONGLIST) bit specifies which parameter list header
295 if ((sati_get_cdb_byte(cdb, 1) & SCSI_REASSIGN_BLOCKS_LONGLIST_BIT) == 0)
304 sequence->allocation_length = 4; //Pre-set allocation_length so that the header can be retrieved
306 //Get 4 bytes for headers (byte 2 & byte 3 for short header; long header all 4 bytes)
307 for(index = 0; index < 4; index++)
309 sati_get_data_byte(sequence, scsi_io, index, &header_bytes[index]);
312 lba_offset = 4; //beginning of lba list
314 if (long_list_bit==0)
316 //Header byte 2 and 3 is the parameter list length
317 data_transfer_length = (header_bytes[2]<<8) + header_bytes[3] + lba_offset;
321 //Header byte 0, 1, 2 and 3 contain the parameter list length
322 data_transfer_length = (header_bytes[0]<<24) + (header_bytes[1]<<16) +
323 (header_bytes[2]<<8) + header_bytes[3] + lba_offset;
326 sequence->allocation_length = data_transfer_length;
328 //Initialized the global processing state
329 sequence->command_specific_data.reassign_blocks_process_state.lba_size = page_size;
330 sequence->command_specific_data.reassign_blocks_process_state.lba_offset = lba_offset;
331 sequence->command_specific_data.reassign_blocks_process_state.ata_command_sent_for_current_lba = 0;
332 sequence->command_specific_data.reassign_blocks_process_state.block_lists_size = data_transfer_length - lba_offset;
333 sequence->command_specific_data.reassign_blocks_process_state.size_of_data_processed = 0;
334 sequence->command_specific_data.reassign_blocks_process_state.current_lba_processed = FALSE;
335 sequence->command_specific_data.reassign_blocks_process_state.ata_command_status = SATI_REASSIGN_BLOCKS_COMMAND_FAIL;
339 * @brief This method will get the data size of not yet processed data.
341 * @param[in] lba_process_state This parameter points to the processing state fields
342 * of current block lba.
344 * @return This method returns the sizeof not yet processed data.
347 U32 sati_reassign_blocks_unprocessed_data_size(
348 SATI_REASSIGN_BLOCKS_PROCESSING_STATE_T * lba_process_state
351 U32 unprocessed_data_size;
353 if(lba_process_state->block_lists_size >= lba_process_state->size_of_data_processed)
355 unprocessed_data_size = lba_process_state->block_lists_size -
356 lba_process_state->size_of_data_processed;
360 unprocessed_data_size = 0;
363 return unprocessed_data_size;
368 * @brief This method will check verify the sector and issue multiple ATA set feature commands to complete the translation.
370 * @param[in] reassign_blocks_process_state This parameter points to the processing state fields
371 * of current lba block.
374 * @return Indicate if the translation was successful.
375 * @retval SATI_SUCCESS
376 * @retval SATI_COMPLETE
377 * @retval SATI_FAILURE_CHECK_RESPONSE_DATA
380 SATI_STATUS sati_reassign_blocks_process_each_lba(
381 SATI_TRANSLATOR_SEQUENCE_T * sequence,
386 SATI_STATUS status = SATI_FAILURE_CHECK_RESPONSE_DATA;
387 SATI_REASSIGN_BLOCKS_PROCESSING_STATE_T * reassign_blocks_process_state;
389 reassign_blocks_process_state = &sequence->command_specific_data.reassign_blocks_process_state;
391 if((reassign_blocks_process_state->ata_command_sent_for_current_lba == 0)&&
392 (reassign_blocks_process_state->ata_command_status == SATI_REASSIGN_BLOCKS_COMMAND_FAIL))
394 reassign_blocks_process_state->size_of_data_processed += reassign_blocks_process_state->lba_size;
395 status = sati_reassign_blocks_verify_condition(sequence, scsi_io, ata_io);
397 else if((reassign_blocks_process_state->ata_command_sent_for_current_lba == 0)&&
398 (reassign_blocks_process_state->ata_command_status == SATI_REASSIGN_BLOCKS_COMMAND_SUCCESS))
401 reassign_blocks_process_state->size_of_data_processed += reassign_blocks_process_state->lba_size;
402 reassign_blocks_process_state->lba_offset += reassign_blocks_process_state->lba_size;
403 status = sati_reassign_blocks_verify_condition(sequence, scsi_io, ata_io);
405 else if((reassign_blocks_process_state->ata_command_sent_for_current_lba == 1)&&
406 (reassign_blocks_process_state->ata_command_status == SATI_REASSIGN_BLOCKS_COMMAND_FAIL))
408 reassign_blocks_process_state->size_of_data_processed += reassign_blocks_process_state->lba_size;
409 status = sati_reassign_blocks_write_condition(sequence, scsi_io, ata_io);
411 else if((reassign_blocks_process_state->ata_command_sent_for_current_lba == 2) &&
412 (reassign_blocks_process_state->ata_command_status == SATI_REASSIGN_BLOCKS_COMMAND_SUCCESS))
414 reassign_blocks_process_state->size_of_data_processed += reassign_blocks_process_state->lba_size;
415 status = sati_reassign_blocks_verify_condition(sequence, scsi_io, ata_io);
417 else //commands sent is 2; SATI_REASSIGN_BLOCKS_COMMAND_FAIL
419 status = SATI_FAILURE_CHECK_RESPONSE_DATA;
426 * @brief This method will process the each lba.
428 * @param[in] reassign_blocks_process_state This parameter points to the processing state fields
431 * @return Indicate if the translation was successful.
432 * @retval SATI_SUCCESS
433 * @retval SATI_COMPLETE
434 * @retval SATI_FAILURE_CHECK_RESPONSE_DATA
437 SATI_STATUS sati_reassign_blocks_process(
438 SATI_TRANSLATOR_SEQUENCE_T * sequence,
443 SATI_STATUS status = SATI_FAILURE_CHECK_RESPONSE_DATA;
445 U32 page_size = 0; // in bytes
446 U32 size_of_data_to_be_processed;
448 SATI_REASSIGN_BLOCKS_PROCESSING_STATE_T * reassign_blocks_process_state;
450 reassign_blocks_process_state = &sequence->command_specific_data.reassign_blocks_process_state;
452 lba_offset = reassign_blocks_process_state->lba_offset;
453 page_size = reassign_blocks_process_state->lba_size;
456 if(sati_reassign_blocks_unprocessed_data_size(reassign_blocks_process_state) < page_size)
461 // Any more lba blocks? If not, done.
462 if(reassign_blocks_process_state->block_lists_size ==
463 reassign_blocks_process_state->size_of_data_processed)
465 sequence->state = SATI_SEQUENCE_STATE_FINAL;
466 status = SATI_COMPLETE;
468 //start processing next lba
471 size_of_data_to_be_processed = reassign_blocks_process_state->block_lists_size
472 - reassign_blocks_process_state->size_of_data_processed;
474 status = sati_reassign_blocks_process_each_lba(sequence, scsi_io, ata_io);
481 //******************************************************************************
482 //* P U B L I C M E T H O D S
483 //******************************************************************************
486 * @brief This method will translate the SCSI Reassign Blocks command
487 * into corresponding ATA commands. Depending upon the capabilities
488 * supported by the target different ATA commands can be selected.
489 * Additionally, in some cases more than a single ATA command may
492 * @return Indicate if the command translation succeeded.
493 * @retval SCI_SUCCESS This is returned if the command translation was
495 * @retval SCI_COMPLETE This is returned if the command translation was
496 * successful and no ATA commands need to be set.
497 * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
498 * sense data has been created as a result of something specified
499 * in the parameter data fields.
501 SATI_STATUS sati_reassign_blocks_translate_command(
502 SATI_TRANSLATOR_SEQUENCE_T * sequence,
507 SATI_STATUS status = SATI_FAILURE_CHECK_RESPONSE_DATA;
508 SATI_REASSIGN_BLOCKS_PROCESSING_STATE_T * reassign_blocks_process_state;
510 reassign_blocks_process_state = &sequence->command_specific_data.reassign_blocks_process_state;
512 sequence->type = SATI_SEQUENCE_REASSIGN_BLOCKS;
514 //Initial processing if
515 if ( sequence->state != SATI_SEQUENCE_STATE_INCOMPLETE )
517 sati_reassign_blocks_initial_processing(
524 // start processing current lba
525 if(reassign_blocks_process_state->current_lba_processed)
527 reassign_blocks_process_state->ata_command_sent_for_current_lba = 0;
528 reassign_blocks_process_state->current_lba_processed = FALSE;
531 status = sati_reassign_blocks_process(sequence, scsi_io, ata_io);
533 if(reassign_blocks_process_state->block_lists_size ==
534 reassign_blocks_process_state->size_of_data_processed)
537 sequence->state = SATI_SEQUENCE_STATE_FINAL;
541 sequence->state = SATI_SEQUENCE_STATE_INCOMPLETE;
544 if(status == SATI_FAILURE_CHECK_RESPONSE_DATA)
546 sequence->state = SATI_SEQUENCE_STATE_FINAL;
547 sati_scsi_sense_data_construct(
550 SCSI_STATUS_CHECK_CONDITION,
551 SCSI_SENSE_MEDIUM_ERROR,
552 SCSI_ASC_UNRECOVERED_READ_ERROR,
553 SCSI_ASCQ_UNRECOVERED_READ_ERROR_AUTO_REALLOCATE_FAIL
561 * @brief This method will translate the ATA command register FIS
562 * response into an appropriate SCSI response for Reassign Blocks
563 * For more information on the parameters passed to this method,
564 * please reference sati_translate_response().
566 * @return Indicate if the response translation succeeded.
567 * @retval SCI_SUCCESS This is returned if the data translation was
570 SATI_STATUS sati_reassign_blocks_translate_response(
571 SATI_TRANSLATOR_SEQUENCE_T * sequence,
576 U8 * register_fis = sati_cb_get_d2h_register_fis_address(ata_io);
577 SATI_REASSIGN_BLOCKS_PROCESSING_STATE_T * reassign_blocks_process_state;
579 reassign_blocks_process_state = &sequence->command_specific_data.reassign_blocks_process_state;
581 if (sati_get_ata_status(register_fis) & ATA_STATUS_REG_ERROR_BIT)
583 reassign_blocks_process_state->ata_command_status = SATI_REASSIGN_BLOCKS_COMMAND_FAIL;
585 //Checking for the number of ATA commands attempted on current LBA, stop
586 //the seaquence after 2 commands have returned errors.
587 if(reassign_blocks_process_state->ata_command_sent_for_current_lba < 2)
589 sequence->state = SATI_SEQUENCE_STATE_INCOMPLETE;
590 reassign_blocks_process_state->size_of_data_processed -= reassign_blocks_process_state->lba_size;
591 return SATI_SEQUENCE_INCOMPLETE;
595 sati_scsi_sense_data_construct(
598 SCSI_STATUS_CHECK_CONDITION,
599 SCSI_SENSE_MEDIUM_ERROR,
600 SCSI_ASC_UNRECOVERED_READ_ERROR,
601 SCSI_ASCQ_UNRECOVERED_READ_ERROR_AUTO_REALLOCATE_FAIL
605 return SATI_FAILURE_CHECK_RESPONSE_DATA;
609 reassign_blocks_process_state->ata_command_status = SATI_REASSIGN_BLOCKS_COMMAND_SUCCESS;
610 if (reassign_blocks_process_state->ata_command_sent_for_current_lba != 2)
611 reassign_blocks_process_state->current_lba_processed = TRUE;
613 if (sequence->state == SATI_SEQUENCE_STATE_INCOMPLETE)
615 return SATI_SEQUENCE_INCOMPLETE;
618 return SATI_COMPLETE;
621 #endif // !defined(DISABLE_SATI_REASSIGN_BLOCKS)