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