Sreekanth Reddy | 7786ab6 | 2015-11-11 17:30:28 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Scsi Host Layer for MPT (Message Passing Technology) based controllers |
| 3 | * |
| 4 | * Copyright (C) 2012-2014 LSI Corporation |
| 5 | * Copyright (C) 2013-2015 Avago Technologies |
| 6 | * (mailto: MPT-FusionLinux.pdl@avagotech.com) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version 2 |
| 11 | * of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * NO WARRANTY |
| 19 | * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 20 | * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT |
| 21 | * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, |
| 22 | * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is |
| 23 | * solely responsible for determining the appropriateness of using and |
| 24 | * distributing the Program and assumes all risks associated with its |
| 25 | * exercise of rights under this Agreement, including but not limited to |
| 26 | * the risks and costs of program errors, damage to or loss of data, |
| 27 | * programs or equipment, and unavailability or interruption of operations. |
| 28 | |
| 29 | * DISCLAIMER OF LIABILITY |
| 30 | * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY |
| 31 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 32 | * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND |
| 33 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
| 34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 35 | * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED |
| 36 | * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES |
| 37 | |
| 38 | * You should have received a copy of the GNU General Public License |
| 39 | * along with this program. |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * _scsih_disable_ddio - Disable direct I/O for all the volumes |
| 44 | * @ioc: per adapter object |
| 45 | */ |
| 46 | static void |
| 47 | _scsih_disable_ddio(struct MPT3SAS_ADAPTER *ioc) |
| 48 | { |
| 49 | Mpi2RaidVolPage1_t vol_pg1; |
| 50 | Mpi2ConfigReply_t mpi_reply; |
| 51 | struct _raid_device *raid_device; |
| 52 | u16 handle; |
| 53 | u16 ioc_status; |
| 54 | unsigned long flags; |
| 55 | |
| 56 | handle = 0xFFFF; |
| 57 | while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply, |
| 58 | &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) { |
| 59 | ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & |
| 60 | MPI2_IOCSTATUS_MASK; |
| 61 | if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE) |
| 62 | break; |
| 63 | handle = le16_to_cpu(vol_pg1.DevHandle); |
| 64 | spin_lock_irqsave(&ioc->raid_device_lock, flags); |
| 65 | raid_device = _scsih_raid_device_find_by_handle(ioc, handle); |
| 66 | if (raid_device) |
| 67 | raid_device->direct_io_enabled = 0; |
| 68 | spin_unlock_irqrestore(&ioc->raid_device_lock, flags); |
| 69 | } |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /** |
| 75 | * _scsih_get_num_volumes - Get number of volumes in the ioc |
| 76 | * @ioc: per adapter object |
| 77 | */ |
| 78 | static u8 |
| 79 | _scsih_get_num_volumes(struct MPT3SAS_ADAPTER *ioc) |
| 80 | { |
| 81 | Mpi2RaidVolPage1_t vol_pg1; |
| 82 | Mpi2ConfigReply_t mpi_reply; |
| 83 | u16 handle; |
| 84 | u8 vol_cnt = 0; |
| 85 | u16 ioc_status; |
| 86 | |
| 87 | handle = 0xFFFF; |
| 88 | while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply, |
| 89 | &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) { |
| 90 | ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & |
| 91 | MPI2_IOCSTATUS_MASK; |
| 92 | if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE) |
| 93 | break; |
| 94 | vol_cnt++; |
| 95 | handle = le16_to_cpu(vol_pg1.DevHandle); |
| 96 | } |
| 97 | return vol_cnt; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O. |
| 103 | * @ioc: per adapter object |
| 104 | * @raid_device: the raid_device object |
| 105 | */ |
| 106 | static void |
| 107 | _scsih_init_warpdrive_properties(struct MPT3SAS_ADAPTER *ioc, |
| 108 | struct _raid_device *raid_device) |
| 109 | { |
| 110 | Mpi2RaidVolPage0_t *vol_pg0; |
| 111 | Mpi2RaidPhysDiskPage0_t pd_pg0; |
| 112 | Mpi2ConfigReply_t mpi_reply; |
| 113 | u16 sz; |
| 114 | u8 num_pds, count; |
| 115 | unsigned long stripe_sz, block_sz; |
| 116 | u8 stripe_exp, block_exp; |
| 117 | u64 dev_max_lba; |
| 118 | |
| 119 | if (!ioc->is_warpdrive) |
| 120 | return; |
| 121 | |
| 122 | if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) { |
| 123 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 124 | "globally as drives are exposed\n", ioc->name); |
| 125 | return; |
| 126 | } |
| 127 | if (_scsih_get_num_volumes(ioc) > 1) { |
| 128 | _scsih_disable_ddio(ioc); |
| 129 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 130 | "globally as number of drives > 1\n", ioc->name); |
| 131 | return; |
| 132 | } |
| 133 | if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle, |
| 134 | &num_pds)) || !num_pds) { |
| 135 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 136 | "Failure in computing number of drives\n", ioc->name); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds * |
| 141 | sizeof(Mpi2RaidVol0PhysDisk_t)); |
| 142 | vol_pg0 = kzalloc(sz, GFP_KERNEL); |
| 143 | if (!vol_pg0) { |
| 144 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 145 | "Memory allocation failure for RVPG0\n", ioc->name); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0, |
| 150 | MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) { |
| 151 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 152 | "Failure in retrieving RVPG0\n", ioc->name); |
| 153 | kfree(vol_pg0); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * WARPDRIVE:If number of physical disks in a volume exceeds the max pds |
| 159 | * assumed for WARPDRIVE, disable direct I/O |
| 160 | */ |
| 161 | if (num_pds > MPT_MAX_WARPDRIVE_PDS) { |
| 162 | pr_warn(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 163 | "for the drive with handle(0x%04x): num_mem=%d, " |
| 164 | "max_mem_allowed=%d\n", ioc->name, raid_device->handle, |
| 165 | num_pds, MPT_MAX_WARPDRIVE_PDS); |
| 166 | kfree(vol_pg0); |
| 167 | return; |
| 168 | } |
| 169 | for (count = 0; count < num_pds; count++) { |
| 170 | if (mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply, |
| 171 | &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM, |
| 172 | vol_pg0->PhysDisk[count].PhysDiskNum) || |
| 173 | pd_pg0.DevHandle == MPT3SAS_INVALID_DEVICE_HANDLE) { |
| 174 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is " |
| 175 | "disabled for the drive with handle(0x%04x) member" |
| 176 | "handle retrieval failed for member number=%d\n", |
| 177 | ioc->name, raid_device->handle, |
| 178 | vol_pg0->PhysDisk[count].PhysDiskNum); |
| 179 | goto out_error; |
| 180 | } |
| 181 | /* Disable direct I/O if member drive lba exceeds 4 bytes */ |
| 182 | dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA); |
| 183 | if (dev_max_lba >> 32) { |
| 184 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is " |
| 185 | "disabled for the drive with handle(0x%04x) member" |
| 186 | " handle (0x%04x) unsupported max lba 0x%016llx\n", |
| 187 | ioc->name, raid_device->handle, |
| 188 | le16_to_cpu(pd_pg0.DevHandle), |
| 189 | (unsigned long long)dev_max_lba); |
| 190 | goto out_error; |
| 191 | } |
| 192 | |
| 193 | raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle); |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * Assumption for WD: Direct I/O is not supported if the volume is |
| 198 | * not RAID0 |
| 199 | */ |
| 200 | if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) { |
| 201 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 202 | "for the drive with handle(0x%04x): type=%d, " |
| 203 | "s_sz=%uK, blk_size=%u\n", ioc->name, |
| 204 | raid_device->handle, raid_device->volume_type, |
| 205 | (le32_to_cpu(vol_pg0->StripeSize) * |
| 206 | le16_to_cpu(vol_pg0->BlockSize)) / 1024, |
| 207 | le16_to_cpu(vol_pg0->BlockSize)); |
| 208 | goto out_error; |
| 209 | } |
| 210 | |
| 211 | stripe_sz = le32_to_cpu(vol_pg0->StripeSize); |
| 212 | stripe_exp = find_first_bit(&stripe_sz, 32); |
| 213 | if (stripe_exp == 32) { |
| 214 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 215 | "for the drive with handle(0x%04x) invalid stripe sz %uK\n", |
| 216 | ioc->name, raid_device->handle, |
| 217 | (le32_to_cpu(vol_pg0->StripeSize) * |
| 218 | le16_to_cpu(vol_pg0->BlockSize)) / 1024); |
| 219 | goto out_error; |
| 220 | } |
| 221 | raid_device->stripe_exponent = stripe_exp; |
| 222 | block_sz = le16_to_cpu(vol_pg0->BlockSize); |
| 223 | block_exp = find_first_bit(&block_sz, 16); |
| 224 | if (block_exp == 16) { |
| 225 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled " |
| 226 | "for the drive with handle(0x%04x) invalid block sz %u\n", |
| 227 | ioc->name, raid_device->handle, |
| 228 | le16_to_cpu(vol_pg0->BlockSize)); |
| 229 | goto out_error; |
| 230 | } |
| 231 | raid_device->block_exponent = block_exp; |
| 232 | raid_device->direct_io_enabled = 1; |
| 233 | |
| 234 | pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is Enabled for the drive" |
| 235 | " with handle(0x%04x)\n", ioc->name, raid_device->handle); |
| 236 | /* |
| 237 | * WARPDRIVE: Though the following fields are not used for direct IO, |
| 238 | * stored for future purpose: |
| 239 | */ |
| 240 | raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA); |
| 241 | raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize); |
| 242 | raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize); |
| 243 | |
| 244 | |
| 245 | kfree(vol_pg0); |
| 246 | return; |
| 247 | |
| 248 | out_error: |
| 249 | raid_device->direct_io_enabled = 0; |
| 250 | for (count = 0; count < num_pds; count++) |
| 251 | raid_device->pd_handle[count] = 0; |
| 252 | kfree(vol_pg0); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * _scsih_scsi_direct_io_get - returns direct io flag |
| 258 | * @ioc: per adapter object |
| 259 | * @smid: system request message index |
| 260 | * |
| 261 | * Returns the smid stored scmd pointer. |
| 262 | */ |
| 263 | static inline u8 |
| 264 | _scsih_scsi_direct_io_get(struct MPT3SAS_ADAPTER *ioc, u16 smid) |
| 265 | { |
| 266 | return ioc->scsi_lookup[smid - 1].direct_io; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * _scsih_scsi_direct_io_set - sets direct io flag |
| 271 | * @ioc: per adapter object |
| 272 | * @smid: system request message index |
| 273 | * @direct_io: Zero or non-zero value to set in the direct_io flag |
| 274 | * |
| 275 | * Returns Nothing. |
| 276 | */ |
| 277 | static inline void |
| 278 | _scsih_scsi_direct_io_set(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 direct_io) |
| 279 | { |
| 280 | ioc->scsi_lookup[smid - 1].direct_io = direct_io; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O |
| 285 | * @ioc: per adapter object |
| 286 | * @scmd: pointer to scsi command object |
| 287 | * @raid_device: pointer to raid device data structure |
| 288 | * @mpi_request: pointer to the SCSI_IO reqest message frame |
| 289 | * @smid: system request message index |
| 290 | * |
| 291 | * Returns nothing |
| 292 | */ |
| 293 | static void |
| 294 | _scsih_setup_direct_io(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd, |
| 295 | struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request, |
| 296 | u16 smid) |
| 297 | { |
| 298 | sector_t v_lba, p_lba, stripe_off, stripe_unit, column, io_size; |
| 299 | u32 stripe_sz, stripe_exp; |
| 300 | u8 num_pds, cmd = scmd->cmnd[0]; |
| 301 | |
| 302 | if (cmd != READ_10 && cmd != WRITE_10 && |
| 303 | cmd != READ_16 && cmd != WRITE_16) |
| 304 | return; |
| 305 | |
| 306 | if (cmd == READ_10 || cmd == WRITE_10) |
| 307 | v_lba = get_unaligned_be32(&mpi_request->CDB.CDB32[2]); |
| 308 | else |
| 309 | v_lba = get_unaligned_be64(&mpi_request->CDB.CDB32[2]); |
| 310 | |
| 311 | io_size = scsi_bufflen(scmd) >> raid_device->block_exponent; |
| 312 | |
| 313 | if (v_lba + io_size - 1 > raid_device->max_lba) |
| 314 | return; |
| 315 | |
| 316 | stripe_sz = raid_device->stripe_sz; |
| 317 | stripe_exp = raid_device->stripe_exponent; |
| 318 | stripe_off = v_lba & (stripe_sz - 1); |
| 319 | |
| 320 | /* Return unless IO falls within a stripe */ |
| 321 | if (stripe_off + io_size > stripe_sz) |
| 322 | return; |
| 323 | |
| 324 | num_pds = raid_device->num_pds; |
| 325 | p_lba = v_lba >> stripe_exp; |
| 326 | stripe_unit = p_lba / num_pds; |
| 327 | column = p_lba % num_pds; |
| 328 | p_lba = (stripe_unit << stripe_exp) + stripe_off; |
| 329 | mpi_request->DevHandle = cpu_to_le16(raid_device->pd_handle[column]); |
| 330 | |
| 331 | if (cmd == READ_10 || cmd == WRITE_10) |
| 332 | put_unaligned_be32(lower_32_bits(p_lba), |
| 333 | &mpi_request->CDB.CDB32[2]); |
| 334 | else |
| 335 | put_unaligned_be64(p_lba, &mpi_request->CDB.CDB32[2]); |
| 336 | |
| 337 | _scsih_scsi_direct_io_set(ioc, smid, 1); |
| 338 | } |