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