xref: /freebsd/sys/dev/isci/scil/sati_device.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 all of the defintions for the SATI remote
59f11c7f63SJim Harris  *        device object.  Some translations require information to be
60f11c7f63SJim Harris  *        remembered on a per device basis.  This information is stored
61f11c7f63SJim Harris  *        in the object defined in this file.
62f11c7f63SJim Harris  */
63f11c7f63SJim Harris 
64f11c7f63SJim Harris #include <dev/isci/scil/sati_device.h>
65f11c7f63SJim Harris #include <dev/isci/scil/sci_util.h>  // Move this file.
66f11c7f63SJim Harris #include <dev/isci/scil/sati_unmap.h>
67f11c7f63SJim Harris #include <dev/isci/scil/intel_scsi.h>
68f11c7f63SJim Harris 
69f11c7f63SJim Harris /**
70f11c7f63SJim Harris  * @brief This method simply initializes the data members in the device
71f11c7f63SJim Harris  *        object to their appropriate values.
72f11c7f63SJim Harris  *
73f11c7f63SJim Harris  * @param[in] device This parameter specifies the device for which to
74f11c7f63SJim Harris  *            initialize the data members.
75f11c7f63SJim Harris  * @param[in] is_ncq_enabled This parameter specifies if NCQ is to be
76f11c7f63SJim Harris  *            utilized for this particular SATI device.
77f11c7f63SJim Harris  * @param[in] max_ncq_depth This parameter specifies the maximum desired
78f11c7f63SJim Harris  *            NCQ depth.  Once this value is set it can never be increased.
79f11c7f63SJim Harris  * @param[in] ignore_fua This parameter specifies FUA is to be ignored and not
80f11c7f63SJim Harris  *            sent to the end device. Some OS (Windows) has quirky behaviors with FUA
81f11c7f63SJim Harris  *            and recommend driver developers ignore the bit.
82f11c7f63SJim Harris  *
83f11c7f63SJim Harris  * @return none
84f11c7f63SJim Harris  */
sati_device_construct(SATI_DEVICE_T * device,BOOL is_ncq_enabled,U8 max_ncq_depth,BOOL ignore_fua)85f11c7f63SJim Harris void sati_device_construct(
86f11c7f63SJim Harris    SATI_DEVICE_T * device,
87f11c7f63SJim Harris    BOOL            is_ncq_enabled,
88f11c7f63SJim Harris    U8              max_ncq_depth,
89f11c7f63SJim Harris    BOOL            ignore_fua
90f11c7f63SJim Harris )
91f11c7f63SJim Harris {
92f11c7f63SJim Harris    device->state                   = SATI_DEVICE_STATE_OPERATIONAL;
93f11c7f63SJim Harris    device->capabilities            = 0;
94f11c7f63SJim Harris    device->descriptor_sense_enable = SCSI_MODE_PAGE_CONTROL_D_SENSE_DISABLE;
95f11c7f63SJim Harris 
96f11c7f63SJim Harris    // The user requested that NCQ be utilized if it is supported by
97f11c7f63SJim Harris    // the device.
98f11c7f63SJim Harris    if (is_ncq_enabled == TRUE)
99f11c7f63SJim Harris       device->capabilities |= SATI_DEVICE_CAP_NCQ_REQUESTED_ENABLE;
100f11c7f63SJim Harris 
101f11c7f63SJim Harris    device->ncq_depth      = max_ncq_depth;
102f11c7f63SJim Harris 
103f11c7f63SJim Harris    // The user requested that FUA is ignored (windows performance issue)
104f11c7f63SJim Harris    if (ignore_fua == TRUE)
105f11c7f63SJim Harris       device->capabilities |= SATI_DEVICE_CAP_IGNORE_FUA;
106f11c7f63SJim Harris 
107f11c7f63SJim Harris }
108f11c7f63SJim Harris 
109f11c7f63SJim Harris /**
110f11c7f63SJim Harris  * @brief This method will update the SATI_DEVICE capabilities based on
111f11c7f63SJim Harris  *        the supplied ATA_IDENTIFY_DEVICE_DATA.
112f11c7f63SJim Harris  *
113f11c7f63SJim Harris  * @param[in] device This parameter specifies the device for which to update
114f11c7f63SJim Harris  *            the supported capabilities.
115f11c7f63SJim Harris  * @param[in] identify This parameter specifies the ata identify device
116f11c7f63SJim Harris  *            information from which to extract the capabilities of the
117f11c7f63SJim Harris  *            device.
118f11c7f63SJim Harris  *
119f11c7f63SJim Harris  * @return none
120f11c7f63SJim Harris  */
sati_device_update_capabilities(SATI_DEVICE_T * device,ATA_IDENTIFY_DEVICE_DATA_T * identify)121f11c7f63SJim Harris void sati_device_update_capabilities(
122f11c7f63SJim Harris    SATI_DEVICE_T              * device,
123f11c7f63SJim Harris    ATA_IDENTIFY_DEVICE_DATA_T * identify
124f11c7f63SJim Harris )
125f11c7f63SJim Harris {
126f11c7f63SJim Harris    U16 capabilities = 0;
127f11c7f63SJim Harris 
128f11c7f63SJim Harris    if (identify->capabilities1 & ATA_IDENTIFY_CAPABILITIES1_NORMAL_DMA_ENABLE)
129f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_UDMA_ENABLE;
130f11c7f63SJim Harris 
131f11c7f63SJim Harris    if (identify->command_set_supported1
132f11c7f63SJim Harris        & ATA_IDENTIFY_COMMAND_SET_SUPPORTED1_48BIT_ENABLE)
133f11c7f63SJim Harris    {
134f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_48BIT_ENABLE;
135f11c7f63SJim Harris    }
136f11c7f63SJim Harris 
137f11c7f63SJim Harris    if (identify->command_set_supported0
138f11c7f63SJim Harris        & ATA_IDENTIFY_COMMAND_SET_SUPPORTED0_SMART_ENABLE)
139f11c7f63SJim Harris    {
140f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_SMART_SUPPORT;
141f11c7f63SJim Harris    }
142f11c7f63SJim Harris 
143f11c7f63SJim Harris    if (identify->command_set_enabled0
144f11c7f63SJim Harris        & ATA_IDENTIFY_COMMAND_SET_SUPPORTED0_SMART_ENABLE)
145f11c7f63SJim Harris    {
146f11c7f63SJim Harris        capabilities |= SATI_DEVICE_CAP_SMART_ENABLE;
147f11c7f63SJim Harris    }
148f11c7f63SJim Harris 
149f11c7f63SJim Harris    // Save the NCQ related capabilities information.
150f11c7f63SJim Harris    if (identify->serial_ata_capabilities
151f11c7f63SJim Harris        & ATA_IDENTIFY_SATA_CAPABILITIES_NCQ_ENABLE)
152f11c7f63SJim Harris    {
153f11c7f63SJim Harris       if (device->capabilities & SATI_DEVICE_CAP_NCQ_REQUESTED_ENABLE)
154f11c7f63SJim Harris       {
155f11c7f63SJim Harris          capabilities      |= SATI_DEVICE_CAP_NCQ_REQUESTED_ENABLE;
156f11c7f63SJim Harris          capabilities      |= SATI_DEVICE_CAP_NCQ_SUPPORTED_ENABLE;
157f11c7f63SJim Harris          capabilities      |= SATI_DEVICE_CAP_DMA_FUA_ENABLE;
158f11c7f63SJim Harris          device->ncq_depth  = MIN(
159f11c7f63SJim Harris                                  device->ncq_depth,
160f11c7f63SJim Harris                                  (U8) (identify->queue_depth
161f11c7f63SJim Harris                                  & ATA_IDENTIFY_NCQ_QUEUE_DEPTH_ENABLE) + 1
162f11c7f63SJim Harris                               );
163f11c7f63SJim Harris       }
164f11c7f63SJim Harris    }
165f11c7f63SJim Harris 
166f11c7f63SJim Harris    // if the user requested that FUA is ignored; transfer it so we don't lose on update.
167f11c7f63SJim Harris    if (device->capabilities & SATI_DEVICE_CAP_IGNORE_FUA)
168f11c7f63SJim Harris 	   capabilities |= SATI_DEVICE_CAP_IGNORE_FUA;
169f11c7f63SJim Harris 
170f11c7f63SJim Harris    if (identify->general_config_bits & ATA_IDENTIFY_REMOVABLE_MEDIA_ENABLE)
171f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_REMOVABLE_MEDIA;
172f11c7f63SJim Harris 
173f11c7f63SJim Harris    if(identify->command_set_supported2 & ATA_IDENTIFY_WRITE_UNCORRECTABLE_SUPPORT )
174f11c7f63SJim Harris    {
175f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_WRITE_UNCORRECTABLE_ENABLE;
176f11c7f63SJim Harris    }
177f11c7f63SJim Harris 
178f11c7f63SJim Harris    if(identify->physical_logical_sector_info &
179f11c7f63SJim Harris       ATA_IDENTIFY_LOGICAL_SECTOR_PER_PHYSICAL_SECTOR_ENABLE)
180f11c7f63SJim Harris    {
181f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_MULTIPLE_SECTORS_PER_PHYSCIAL_SECTOR;
182f11c7f63SJim Harris    }
183f11c7f63SJim Harris 
184f11c7f63SJim Harris    if(identify->command_set_supported_extention &
185f11c7f63SJim Harris       ATA_IDENTIFY_COMMAND_SET_SMART_SELF_TEST_SUPPORTED)
186f11c7f63SJim Harris    {
187f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_SMART_SELF_TEST_SUPPORT;
188f11c7f63SJim Harris    }
189f11c7f63SJim Harris 
190f11c7f63SJim Harris    if (identify->nominal_media_rotation_rate == 1)
191f11c7f63SJim Harris    {
192f11c7f63SJim Harris        capabilities |= SATI_DEVICE_CAP_SSD;
193f11c7f63SJim Harris    }
194f11c7f63SJim Harris 
195f11c7f63SJim Harris    // Save off the logical block size reported by the drive
196f11c7f63SJim Harris    // See if Word 106 is valid and reports a logical sector size
197f11c7f63SJim Harris    if ((identify->physical_logical_sector_info & 0x5000) == 0x5000)
198f11c7f63SJim Harris    {
199f11c7f63SJim Harris        device->logical_block_size = (identify->words_per_logical_sector[3] << 24) |
200f11c7f63SJim Harris                                     (identify->words_per_logical_sector[2] << 16) |
201f11c7f63SJim Harris                                     (identify->words_per_logical_sector[1] << 8) |
202f11c7f63SJim Harris                                     (identify->words_per_logical_sector[0]);
203f11c7f63SJim Harris    }
204f11c7f63SJim Harris    else
205f11c7f63SJim Harris    {
206f11c7f63SJim Harris        device->logical_block_size = 512;
207f11c7f63SJim Harris    }
208f11c7f63SJim Harris 
209f11c7f63SJim Harris    // Determine DSM TRIM capabilities
210f11c7f63SJim Harris    // Defend against SSDs which report TRIM support, but set
211f11c7f63SJim Harris    //  max_lba_range_entry_blocks to zero, by disabling TRIM for
212f11c7f63SJim Harris    //  those SSDs.
213f11c7f63SJim Harris    if (
214f11c7f63SJim Harris      (identify->data_set_management & ATA_IDENTIFY_COMMAND_SET_DSM_TRIM_SUPPORTED)
215f11c7f63SJim Harris      && (identify->max_lba_range_entry_blocks > 0)
216f11c7f63SJim Harris       )
217f11c7f63SJim Harris    {
218f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_DSM_TRIM_SUPPORT;
219f11c7f63SJim Harris       device->max_lba_range_entry_blocks = identify->max_lba_range_entry_blocks;
220f11c7f63SJim Harris    }
221f11c7f63SJim Harris 
222f11c7f63SJim Harris    if (identify->additional_supported
223f11c7f63SJim Harris        & ATA_IDENTIFY_COMMAND_ADDL_SUPPORTED_DETERMINISTIC_READ)
224f11c7f63SJim Harris    {
225f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_DETERMINISTIC_READ_AFTER_TRIM;
226f11c7f63SJim Harris    }
227f11c7f63SJim Harris 
228f11c7f63SJim Harris    if (identify->additional_supported
229f11c7f63SJim Harris        & ATA_IDENTIFY_COMMAND_ADDL_SUPPORTED_READ_ZERO)
230f11c7f63SJim Harris    {
231f11c7f63SJim Harris       capabilities |= SATI_DEVICE_CAP_READ_ZERO_AFTER_TRIM;
232f11c7f63SJim Harris    }
233f11c7f63SJim Harris 
234f11c7f63SJim Harris    if (identify->capabilities1
235f11c7f63SJim Harris        & ATA_IDENTIFY_CAPABILITIES1_STANDBY_ENABLE)
236f11c7f63SJim Harris    {
237f11c7f63SJim Harris        capabilities |= SATI_DEVICE_CAP_STANDBY_ENABLE;
238f11c7f63SJim Harris    }
239f11c7f63SJim Harris 
240f11c7f63SJim Harris    device->min_blocks_per_microcode_command = identify->min_num_blocks_per_microcode;
241f11c7f63SJim Harris    device->max_blocks_per_microcode_command = identify->max_num_blocks_per_microcode;
242f11c7f63SJim Harris 
243f11c7f63SJim Harris    device->capabilities = capabilities;
244f11c7f63SJim Harris }
245f11c7f63SJim Harris 
246