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 unmap command.
64 #if !defined(DISABLE_SATI_UNMAP)
66 #include <dev/isci/scil/sati_unmap.h>
67 #include <dev/isci/scil/sati_callbacks.h>
68 #include <dev/isci/scil/sati_translator_sequence.h>
69 #include <dev/isci/scil/sati_util.h>
70 #include <dev/isci/scil/intel_ata.h>
71 #include <dev/isci/scil/intel_scsi.h>
72 #include <dev/isci/scil/intel_sat.h>
74 //******************************************************************************
75 //* P R I V A T E M E T H O D S
76 //******************************************************************************
79 * @brief This method translates a given number of DSM
80 * requests into DSM blocks based on the devices logical block size
82 * @return Number of DSM blocks required for the DSM descriptor count
84 U32 sati_unmap_calculate_dsm_blocks(
85 SATI_TRANSLATOR_SEQUENCE_T * sequence,
86 U32 dsm_descriptor_count
89 U32 blocks = (dsm_descriptor_count * sizeof(TRIM_PAIR))/sequence->device->logical_block_size;
90 if ((dsm_descriptor_count * sizeof(TRIM_PAIR)) % sequence->device->logical_block_size)
98 * @brief This method performs the SCSI Unmap command translation
101 * - setting the command register
102 * - setting the device head register
103 * - filling in fields in the SATI_TRANSLATOR_SEQUENCE object.
104 * For more information on the parameters passed to this method,
105 * please reference sati_translate_command().
107 * @return Indicate if the method was successfully completed.
108 * @retval SATI_SUCCESS This is returned in all other cases.
110 SATI_STATUS sati_unmap_construct(
111 SATI_TRANSLATOR_SEQUENCE_T * sequence,
117 U8 * h2d_register_fis = sati_cb_get_h2d_register_fis_address(ata_io);
118 U8 * d2h_register_fis = sati_cb_get_d2h_register_fis_address(ata_io);
120 sati_set_ata_command(h2d_register_fis, ATA_DATA_SET_MANAGEMENT);
121 sati_set_ata_features(h2d_register_fis, 0x01);
122 sati_set_ata_sector_count(h2d_register_fis, (U8)sector_count);
123 sati_set_ata_device_head(h2d_register_fis, ATA_DEV_HEAD_REG_LBA_MODE_ENABLE);
125 // Set the completion status since the core will not do that for
126 // the udma fast path.
127 sati_set_ata_status(d2h_register_fis, 0x00);
129 // Set up the direction and protocol for SCIC
130 sequence->data_direction = SATI_DATA_DIRECTION_OUT;
131 sequence->protocol = SAT_PROTOCOL_UDMA_DATA_OUT;
132 // The UNMAP translation will always require a callback
133 // on every response so it can free memory if an error
135 sequence->is_translate_response_required = TRUE;
137 ASSERT(sector_count < 0x100);
143 * @brief This method updates the unmap sequence state to the next
146 * @return Indicate if the method was successfully completed.
147 * @retval SATI_SUCCESS This is returned in all other cases.
149 SATI_STATUS sati_unmap_load_next_descriptor(
150 SATI_TRANSLATOR_SEQUENCE_T * sequence,
154 SATI_UNMAP_PROCESSING_STATE_T * unmap_process_state;
156 U8 unmap_block_descriptor[16];
158 unmap_process_state = &sequence->command_specific_data.unmap_process_state;
160 // Load the next descriptor
161 for(index = unmap_process_state->current_unmap_block_descriptor_index;
162 index < unmap_process_state->current_unmap_block_descriptor_index +
163 SATI_UNMAP_SIZEOF_SCSI_UNMAP_BLOCK_DESCRIPTOR;
166 sati_get_data_byte(sequence,
169 &unmap_block_descriptor[index-unmap_process_state->current_unmap_block_descriptor_index]);
172 // Update the internal state for the next translation pass
173 unmap_process_state->current_lba_count = (unmap_block_descriptor[8] << 24) |
174 (unmap_block_descriptor[9] << 16) |
175 (unmap_block_descriptor[10] << 8) |
176 (unmap_block_descriptor[11]);
177 unmap_process_state->current_lba = ((SATI_LBA)(unmap_block_descriptor[0]) << 56) |
178 ((SATI_LBA)(unmap_block_descriptor[1]) << 48) |
179 ((SATI_LBA)(unmap_block_descriptor[2]) << 40) |
180 ((SATI_LBA)(unmap_block_descriptor[3]) << 32) |
181 ((SATI_LBA)(unmap_block_descriptor[4]) << 24) |
182 ((SATI_LBA)(unmap_block_descriptor[5]) << 16) |
183 ((SATI_LBA)(unmap_block_descriptor[6]) << 8) |
184 ((SATI_LBA)(unmap_block_descriptor[7]));
185 unmap_process_state->next_lba = 0;
187 // Update the index for the next descriptor to translate
188 unmap_process_state->current_unmap_block_descriptor_index += SATI_UNMAP_SIZEOF_SCSI_UNMAP_BLOCK_DESCRIPTOR;
194 * @brief This method determines the max number of blocks of DSM data
195 * that can be satisfied by the device and the SW
197 * @return Number of blocks supported
198 * @retval Number of blocks supported
200 U32 sati_unmap_get_max_buffer_size_in_blocks(
201 SATI_TRANSLATOR_SEQUENCE_T * sequence
204 // Currently this SATI implementation only supports a single
205 // 4k block of memory for the DMA write operation for simplicity
206 // (no need to handle more than one SG element).
207 // Since most run time UNMAP requests use 1K or less buffer space,
208 // there is no performance degradation with only supporting a
209 // single physical page. For best results allocate the maximum
210 // amount of memory the device can handle up to the maximum of 4K.
211 return MIN(SATI_DSM_MAX_BUFFER_SIZE/sequence->device->logical_block_size,
212 sequence->device->max_lba_range_entry_blocks);
216 * @brief This method will be called before starting the first unmap translation
218 * @return Indicate if the translation was successful.
219 * @retval SATI_SUCCESS This is returned if the command translation was
220 * successful and no further processing.
221 * @retval SATI_COMPLETE - The initial processing was completed successfully
222 * @retval SATI_FAILURE_CHECK_RESPONSE_DATA - Failed the initial processing
224 SATI_STATUS sati_unmap_initial_processing(
225 SATI_TRANSLATOR_SEQUENCE_T * sequence,
230 SATI_UNMAP_PROCESSING_STATE_T * unmap_process_state;
233 U32 descriptor_length;
236 U8 unmap_param_list[8];
238 unmap_process_state = &sequence->command_specific_data.unmap_process_state;
240 // Set up the sequence type for unmap translation
241 sequence->type = SATI_SEQUENCE_UNMAP;
243 // Make sure the device is TRIM capable
244 if ((sequence->device->capabilities & SATI_DEVICE_CAP_DSM_TRIM_SUPPORT)
245 != SATI_DEVICE_CAP_DSM_TRIM_SUPPORT)
247 // Can't send TRIM request to device that does not support it
248 sati_scsi_sense_data_construct(
251 SCSI_STATUS_CHECK_CONDITION,
252 SCSI_SENSE_ILLEGAL_REQUEST,
253 SCSI_ASC_INVALID_FIELD_IN_CDB,
254 SCSI_ASCQ_INVALID_FIELD_IN_CDB
256 return SATI_FAILURE_CHECK_RESPONSE_DATA;
259 // get the amount of data being sent from the cdb
260 cdb = sati_cb_get_cdb_address(scsi_io);
261 unmap_length = (sati_get_cdb_byte(cdb, 7) << 8) | sati_get_cdb_byte(cdb, 8);
263 // If nothing has been requested return success now.
264 if (unmap_length == 0)
266 // SAT: This is not an error
269 if (unmap_length < SATI_UNMAP_SIZEOF_SCSI_UNMAP_PARAMETER_LIST)
271 // Not enough length specified in the CDB
272 sati_scsi_sense_data_construct(
275 SCSI_STATUS_CHECK_CONDITION,
276 SCSI_SENSE_ILLEGAL_REQUEST,
277 SCSI_ASC_INVALID_FIELD_IN_CDB,
278 SCSI_ASCQ_INVALID_FIELD_IN_CDB
280 return SATI_FAILURE_CHECK_RESPONSE_DATA;
283 sequence->allocation_length = unmap_length;
285 // Get the unmap parameter header
286 for(index = 0; index < SATI_UNMAP_SIZEOF_SCSI_UNMAP_PARAMETER_LIST; index++)
288 sati_get_data_byte(sequence, scsi_io, index, &unmap_param_list[index]);
290 descriptor_length = (unmap_param_list[2] << 8) | unmap_param_list[3];
292 // Check length again
293 if (descriptor_length == 0)
295 // SAT: This is not an error
299 if ((U32)(unmap_length - SATI_UNMAP_SIZEOF_SCSI_UNMAP_PARAMETER_LIST) < descriptor_length)
301 // Not enough length specified in the CDB
302 sati_scsi_sense_data_construct(
305 SCSI_STATUS_CHECK_CONDITION,
306 SCSI_SENSE_ILLEGAL_REQUEST,
307 SCSI_ASC_INVALID_FIELD_IN_CDB,
308 SCSI_ASCQ_INVALID_FIELD_IN_CDB
310 return SATI_FAILURE_CHECK_RESPONSE_DATA;
313 // Save the maximum unmap block descriptors in this request
314 unmap_process_state->max_unmap_block_descriptors =
315 descriptor_length/SATI_UNMAP_SIZEOF_SCSI_UNMAP_BLOCK_DESCRIPTOR;
317 // Determine the maximum size of the write buffer that will be required
318 // for the translation in terms of number of blocks
319 max_dsm_blocks = sati_unmap_get_max_buffer_size_in_blocks(sequence);
321 // Save the maximum number of DSM descriptors we can send during the translation
322 unmap_process_state->max_lba_range_entries =
323 (max_dsm_blocks*sequence->device->logical_block_size)/sizeof(TRIM_PAIR);
325 // Get the write buffer for the translation
326 sati_cb_allocate_dma_buffer(
328 max_dsm_blocks*sequence->device->logical_block_size,
329 &(unmap_process_state->virtual_unmap_buffer),
330 &(unmap_process_state->physical_unmap_buffer_low),
331 &(unmap_process_state->physical_unmap_buffer_high));
333 // Makes sure we have a buffer
334 if (unmap_process_state->virtual_unmap_buffer == NULL)
337 sati_scsi_sense_data_construct(
342 SCSI_ASC_NO_ADDITIONAL_SENSE,
343 SCSI_ASCQ_NO_ADDITIONAL_SENSE
345 return SATI_FAILURE_CHECK_RESPONSE_DATA;
348 // Get the first SGL entry. This code will only use one 4K page so will
349 // only utilize the first sge.
350 sati_cb_sgl_next_sge(scsi_io,
353 &(unmap_process_state->unmap_buffer_sgl_pair));
355 // Load the first descriptor to start the translation loop
356 unmap_process_state->current_unmap_block_descriptor_index =
357 SATI_UNMAP_SIZEOF_SCSI_UNMAP_PARAMETER_LIST;
358 sati_unmap_load_next_descriptor(sequence,scsi_io);
360 // Next state will be incomplete since translation
361 // will require a callback and possibly more requests.
362 sequence->state = SATI_SEQUENCE_STATE_INCOMPLETE;
364 return SATI_COMPLETE;
368 * @brief This method will process each unmap sequence.
370 * @return Indicate if the translation was successful.
371 * @retval SATI_SUCCESS
373 SATI_STATUS sati_unmap_process(
374 SATI_TRANSLATOR_SEQUENCE_T * sequence,
379 SATI_UNMAP_PROCESSING_STATE_T * unmap_process_state;
380 SATI_LBA dsm_descriptor_lba_count;
383 U32 dsm_remainder_bytes;
387 unmap_process_state = &sequence->command_specific_data.unmap_process_state;
389 // Set up the starting address of the buffer for this portion of the translation
390 unmap_process_state->current_dsm_descriptor = unmap_process_state->virtual_unmap_buffer;
393 // Translate as much as we can
394 while ((dsm_descriptor < unmap_process_state->max_lba_range_entries) &&
395 (unmap_process_state->current_lba_count > 0)) {
396 // See if the LBA count will fit in to a single descriptor
397 if (unmap_process_state->current_lba_count > SATI_DSM_MAX_SECTOR_COUNT) {
398 // Can't fit all of the lbas for this descriptor in to
399 // one DSM request. Adjust the current LbaCount and total
400 // remaining for the next descriptor
401 dsm_descriptor_lba_count = SATI_DSM_MAX_SECTOR_COUNT;
402 unmap_process_state->current_lba_count -= SATI_DSM_MAX_SECTOR_COUNT;
403 unmap_process_state->next_lba =
404 unmap_process_state->current_lba + SATI_DSM_MAX_SECTOR_COUNT;
406 // It all fits in to one descriptor
407 dsm_descriptor_lba_count = unmap_process_state->current_lba_count;
408 unmap_process_state->current_lba_count = 0;
411 // Fill in the ATA DSM descriptor
412 ((PTRIM_PAIR)(unmap_process_state->current_dsm_descriptor))->sector_address =
413 unmap_process_state->current_lba;
414 ((PTRIM_PAIR)(unmap_process_state->current_dsm_descriptor))->sector_count =
415 dsm_descriptor_lba_count;
417 // See if we can move on to the next descriptor
418 if (unmap_process_state->current_lba_count == 0) {
419 // See if there is another descriptor
420 --unmap_process_state->max_unmap_block_descriptors;
421 if (unmap_process_state->max_unmap_block_descriptors > 0) {
422 // Move on to the next descriptor
423 sati_unmap_load_next_descriptor(sequence,scsi_io);
426 // Move to the next LBA in this descriptor
427 unmap_process_state->current_lba = unmap_process_state->next_lba;
430 // Make sure the LBA does not exceed 48 bits...
431 ASSERT(unmap_process_state->current_lba <= SATI_DSM_MAX_SECTOR_ADDRESS);
433 // Increment the number of descriptors used and point to the next entry
435 unmap_process_state->current_dsm_descriptor =
436 (U8 *)(unmap_process_state->current_dsm_descriptor) + sizeof(TRIM_PAIR);
439 // Calculate number of blocks we have filled in
440 dsm_blocks = sati_unmap_calculate_dsm_blocks(sequence,dsm_descriptor);
441 dsm_bytes = dsm_blocks * sequence->device->logical_block_size;
442 max_dsm_blocks = sati_unmap_get_max_buffer_size_in_blocks(sequence);
444 // The current_dsm_descriptor points to the next location in the buffer
445 // Get the remaining bytes from the last translated descriptor
446 // to the end of the 4k buffer.
447 dsm_remainder_bytes = sequence->device->logical_block_size;
448 dsm_remainder_bytes -= (U32)((POINTER_UINT)unmap_process_state->current_dsm_descriptor &
449 (sequence->device->logical_block_size-1));
451 // If there was no remainder, the complete buffer was filled in.
452 if (dsm_remainder_bytes != sequence->device->logical_block_size)
454 // Add on the remaining unfilled blocks
455 dsm_remainder_bytes += (sequence->device->logical_block_size * (max_dsm_blocks - dsm_blocks));
457 // According to ATA-8, if the DSM buffer is not completely filled with
458 // valid DSM descriptor data, the remaining portion of the
459 // buffer must be filled in with zeros.
460 memset((U8 *)unmap_process_state->current_dsm_descriptor, 0, dsm_remainder_bytes);
463 // Tell scic to utilize this sgl pair for write DMA processing of
464 // the SCSI UNMAP translation with the total number of bytes for this transfer
465 sati_cb_sge_write(unmap_process_state->unmap_buffer_sgl_pair,
466 unmap_process_state->physical_unmap_buffer_low,
467 unmap_process_state->physical_unmap_buffer_high,
470 // Construct the unmap ATA request
471 sati_unmap_construct(sequence,
476 // Determine sequence next state based on whether there is more translation
478 if (unmap_process_state->current_lba_count == 0)
480 // used for completion routine to determine if there is more processing
481 sequence->state = SATI_SEQUENCE_STATE_FINAL;
483 // This requests has already translated the SGL, have SCIC skip SGL translataion
484 return SATI_SUCCESS_SGL_TRANSLATED;
487 //******************************************************************************
488 //* P U B L I C M E T H O D S
489 //******************************************************************************
492 * @brief This method will handle termination of the
493 * SCSI unmap translation and frees previously allocated
498 void sati_unmap_terminate(
499 SATI_TRANSLATOR_SEQUENCE_T * sequence,
504 SATI_UNMAP_PROCESSING_STATE_T * unmap_process_state;
505 unmap_process_state = &sequence->command_specific_data.unmap_process_state;
507 if (unmap_process_state->virtual_unmap_buffer != NULL)
509 sati_cb_free_dma_buffer(scsi_io, unmap_process_state->virtual_unmap_buffer);
510 unmap_process_state->virtual_unmap_buffer = NULL;
515 * @brief This method will translate the SCSI Unmap command
516 * into corresponding ATA commands. Depending upon the capabilities
517 * supported by the target different ATA commands can be selected.
518 * Additionally, in some cases more than a single ATA command may
521 * @return Indicate if the command translation succeeded.
522 * @retval SATI_SUCCESS This is returned if the command translation was
524 * @retval SATI_COMPLETE This is returned if the command translation was
525 * successful and no ATA commands need to be set.
526 * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
527 * sense data has been created as a result of something specified
528 * in the parameter data fields.
530 SATI_STATUS sati_unmap_translate_command(
531 SATI_TRANSLATOR_SEQUENCE_T * sequence,
536 SATI_STATUS status = SATI_FAILURE_CHECK_RESPONSE_DATA;
537 SATI_UNMAP_PROCESSING_STATE_T * unmap_process_state;
539 unmap_process_state = &sequence->command_specific_data.unmap_process_state;
541 // Determine if this is the first step in the unmap sequence
542 if ( sequence->state == SATI_SEQUENCE_STATE_INITIAL )
544 status = sati_unmap_initial_processing(sequence,scsi_io,ata_io);
545 if (status != SATI_COMPLETE)
550 // Translate the next portion of the UNMAP request
551 return sati_unmap_process(sequence, scsi_io, ata_io);
555 * @brief This method will translate the ATA command register FIS
556 * response into an appropriate SCSI response for Unmap.
557 * For more information on the parameters passed to this method,
558 * please reference sati_translate_response().
560 * @return Indicate if the response translation succeeded.
561 * @retval SATI_SUCCESS This is returned if the command translation was
563 * @retval SATI_COMPLETE This is returned if the command translation was
564 * successful and no ATA commands need to be set.
565 * @retval SATI_FAILURE_CHECK_RESPONSE_DATA This value is returned if
566 * sense data has been created as a result of something specified
567 * in the parameter data fields.
569 SATI_STATUS sati_unmap_translate_response(
570 SATI_TRANSLATOR_SEQUENCE_T * sequence,
575 U8 * register_fis = sati_cb_get_d2h_register_fis_address(ata_io);
576 SATI_UNMAP_PROCESSING_STATE_T * unmap_process_state;
577 SATI_STATUS sati_status = SATI_COMPLETE;
579 unmap_process_state = &sequence->command_specific_data.unmap_process_state;
581 if (sati_get_ata_status(register_fis) & ATA_STATUS_REG_ERROR_BIT)
583 sequence->state = SATI_SEQUENCE_STATE_FINAL;
584 sati_scsi_sense_data_construct(
587 SCSI_STATUS_CHECK_CONDITION,
588 SCSI_SENSE_ABORTED_COMMAND,
589 SCSI_ASC_NO_ADDITIONAL_SENSE,
590 SCSI_ASCQ_NO_ADDITIONAL_SENSE
592 // All done, terminate the translation
593 sati_unmap_terminate(sequence, scsi_io, ata_io);
597 if (sequence->state != SATI_SEQUENCE_STATE_INCOMPLETE)
599 // All done, terminate the translation
600 sati_unmap_terminate(sequence, scsi_io, ata_io);
605 sati_status = SATI_SEQUENCE_STATE_INCOMPLETE;
611 #endif // !defined(DISABLE_SATI_UNMAP)