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  #include <linux/kernel.h>
42  #include <linux/module.h>
43  #include <linux/errno.h>
44  #include <linux/types.h>
45  #include <linux/unaligned.h>
46  
47  #include "mpt3sas_base.h"
48  
49  /**
50   * _warpdrive_disable_ddio - Disable direct I/O for all the volumes
51   * @ioc: per adapter object
52   */
53  static void
_warpdrive_disable_ddio(struct MPT3SAS_ADAPTER * ioc)54  _warpdrive_disable_ddio(struct MPT3SAS_ADAPTER *ioc)
55  {
56  	Mpi2RaidVolPage1_t vol_pg1;
57  	Mpi2ConfigReply_t mpi_reply;
58  	struct _raid_device *raid_device;
59  	u16 handle;
60  	u16 ioc_status;
61  	unsigned long flags;
62  
63  	handle = 0xFFFF;
64  	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
65  	    &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
66  		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
67  		    MPI2_IOCSTATUS_MASK;
68  		if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
69  			break;
70  		handle = le16_to_cpu(vol_pg1.DevHandle);
71  		spin_lock_irqsave(&ioc->raid_device_lock, flags);
72  		raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
73  		if (raid_device)
74  			raid_device->direct_io_enabled = 0;
75  		spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
76  	}
77  	return;
78  }
79  
80  
81  /**
82   * mpt3sas_get_num_volumes - Get number of volumes in the ioc
83   * @ioc: per adapter object
84   */
85  u8
mpt3sas_get_num_volumes(struct MPT3SAS_ADAPTER * ioc)86  mpt3sas_get_num_volumes(struct MPT3SAS_ADAPTER *ioc)
87  {
88  	Mpi2RaidVolPage1_t vol_pg1;
89  	Mpi2ConfigReply_t mpi_reply;
90  	u16 handle;
91  	u8 vol_cnt = 0;
92  	u16 ioc_status;
93  
94  	handle = 0xFFFF;
95  	while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
96  	    &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
97  		ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
98  		    MPI2_IOCSTATUS_MASK;
99  		if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
100  			break;
101  		vol_cnt++;
102  		handle = le16_to_cpu(vol_pg1.DevHandle);
103  	}
104  	return vol_cnt;
105  }
106  
107  
108  /**
109   * mpt3sas_init_warpdrive_properties - Set properties for warpdrive direct I/O.
110   * @ioc: per adapter object
111   * @raid_device: the raid_device object
112   */
113  void
mpt3sas_init_warpdrive_properties(struct MPT3SAS_ADAPTER * ioc,struct _raid_device * raid_device)114  mpt3sas_init_warpdrive_properties(struct MPT3SAS_ADAPTER *ioc,
115  	struct _raid_device *raid_device)
116  {
117  	Mpi2RaidVolPage0_t *vol_pg0;
118  	Mpi2RaidPhysDiskPage0_t pd_pg0;
119  	Mpi2ConfigReply_t mpi_reply;
120  	u16 sz;
121  	u8 num_pds, count;
122  	unsigned long stripe_sz, block_sz;
123  	u8 stripe_exp, block_exp;
124  	u64 dev_max_lba;
125  
126  	if (!ioc->is_warpdrive)
127  		return;
128  
129  	if (ioc->mfg_pg10_hide_flag ==  MFG_PAGE10_EXPOSE_ALL_DISKS) {
130  		ioc_info(ioc, "WarpDrive : Direct IO is disabled globally as drives are exposed\n");
131  		return;
132  	}
133  	if (mpt3sas_get_num_volumes(ioc) > 1) {
134  		_warpdrive_disable_ddio(ioc);
135  		ioc_info(ioc, "WarpDrive : Direct IO is disabled globally as number of drives > 1\n");
136  		return;
137  	}
138  	if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
139  	    &num_pds)) || !num_pds) {
140  		ioc_info(ioc, "WarpDrive : Direct IO is disabled Failure in computing number of drives\n");
141  		return;
142  	}
143  
144  	sz = struct_size(vol_pg0, PhysDisk, num_pds);
145  	vol_pg0 = kzalloc(sz, GFP_KERNEL);
146  	if (!vol_pg0) {
147  		ioc_info(ioc, "WarpDrive : Direct IO is disabled Memory allocation failure for RVPG0\n");
148  		return;
149  	}
150  
151  	if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
152  	     MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
153  		ioc_info(ioc, "WarpDrive : Direct IO is disabled Failure in retrieving RVPG0\n");
154  		kfree(vol_pg0);
155  		return;
156  	}
157  
158  	/*
159  	 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
160  	 * assumed for WARPDRIVE, disable direct I/O
161  	 */
162  	if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
163  		ioc_warn(ioc, "WarpDrive : Direct IO is disabled for the drive with handle(0x%04x): num_mem=%d, max_mem_allowed=%d\n",
164  			 raid_device->handle, num_pds, MPT_MAX_WARPDRIVE_PDS);
165  		kfree(vol_pg0);
166  		return;
167  	}
168  	for (count = 0; count < num_pds; count++) {
169  		if (mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
170  		    &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
171  		    vol_pg0->PhysDisk[count].PhysDiskNum) ||
172  		    le16_to_cpu(pd_pg0.DevHandle) ==
173  		    MPT3SAS_INVALID_DEVICE_HANDLE) {
174  			ioc_info(ioc, "WarpDrive : Direct IO is disabled for the drive with handle(0x%04x) member handle retrieval failed for member number=%d\n",
175  				 raid_device->handle,
176  				 vol_pg0->PhysDisk[count].PhysDiskNum);
177  			goto out_error;
178  		}
179  		/* Disable direct I/O if member drive lba exceeds 4 bytes */
180  		dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
181  		if (dev_max_lba >> 32) {
182  			ioc_info(ioc, "WarpDrive : Direct IO is disabled for the drive with handle(0x%04x) member handle (0x%04x) unsupported max lba 0x%016llx\n",
183  				 raid_device->handle,
184  				 le16_to_cpu(pd_pg0.DevHandle),
185  				 (u64)dev_max_lba);
186  			goto out_error;
187  		}
188  
189  		raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
190  	}
191  
192  	/*
193  	 * Assumption for WD: Direct I/O is not supported if the volume is
194  	 * not RAID0
195  	 */
196  	if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
197  		ioc_info(ioc, "WarpDrive : Direct IO is disabled for the drive with handle(0x%04x): type=%d, s_sz=%uK, blk_size=%u\n",
198  			 raid_device->handle, raid_device->volume_type,
199  			 (le32_to_cpu(vol_pg0->StripeSize) *
200  			  le16_to_cpu(vol_pg0->BlockSize)) / 1024,
201  			 le16_to_cpu(vol_pg0->BlockSize));
202  		goto out_error;
203  	}
204  
205  	stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
206  	stripe_exp = find_first_bit(&stripe_sz, 32);
207  	if (stripe_exp == 32) {
208  		ioc_info(ioc, "WarpDrive : Direct IO is disabled for the drive with handle(0x%04x) invalid stripe sz %uK\n",
209  			 raid_device->handle,
210  			 (le32_to_cpu(vol_pg0->StripeSize) *
211  			  le16_to_cpu(vol_pg0->BlockSize)) / 1024);
212  		goto out_error;
213  	}
214  	raid_device->stripe_exponent = stripe_exp;
215  	block_sz = le16_to_cpu(vol_pg0->BlockSize);
216  	block_exp = find_first_bit(&block_sz, 16);
217  	if (block_exp == 16) {
218  		ioc_info(ioc, "WarpDrive : Direct IO is disabled for the drive with handle(0x%04x) invalid block sz %u\n",
219  			 raid_device->handle, le16_to_cpu(vol_pg0->BlockSize));
220  		goto out_error;
221  	}
222  	raid_device->block_exponent = block_exp;
223  	raid_device->direct_io_enabled = 1;
224  
225  	ioc_info(ioc, "WarpDrive : Direct IO is Enabled for the drive with handle(0x%04x)\n",
226  		 raid_device->handle);
227  	/*
228  	 * WARPDRIVE: Though the following fields are not used for direct IO,
229  	 * stored for future purpose:
230  	 */
231  	raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
232  	raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
233  	raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
234  
235  
236  	kfree(vol_pg0);
237  	return;
238  
239  out_error:
240  	raid_device->direct_io_enabled = 0;
241  	for (count = 0; count < num_pds; count++)
242  		raid_device->pd_handle[count] = 0;
243  	kfree(vol_pg0);
244  	return;
245  }
246  
247  /**
248   * mpt3sas_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
249   * @ioc: per adapter object
250   * @scmd: pointer to scsi command object
251   * @raid_device: pointer to raid device data structure
252   * @mpi_request: pointer to the SCSI_IO reqest message frame
253   */
254  void
mpt3sas_setup_direct_io(struct MPT3SAS_ADAPTER * ioc,struct scsi_cmnd * scmd,struct _raid_device * raid_device,Mpi25SCSIIORequest_t * mpi_request)255  mpt3sas_setup_direct_io(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
256  	struct _raid_device *raid_device, Mpi25SCSIIORequest_t *mpi_request)
257  {
258  	sector_t v_lba, p_lba, stripe_off, column, io_size;
259  	u32 stripe_sz, stripe_exp;
260  	u8 num_pds, cmd = scmd->cmnd[0];
261  	struct scsiio_tracker *st = scsi_cmd_priv(scmd);
262  
263  	if (cmd != READ_10 && cmd != WRITE_10 &&
264  	    cmd != READ_16 && cmd != WRITE_16)
265  		return;
266  
267  	if (cmd == READ_10 || cmd == WRITE_10)
268  		v_lba = get_unaligned_be32(&mpi_request->CDB.CDB32[2]);
269  	else
270  		v_lba = get_unaligned_be64(&mpi_request->CDB.CDB32[2]);
271  
272  	io_size = scsi_bufflen(scmd) >> raid_device->block_exponent;
273  
274  	if (v_lba + io_size - 1 > raid_device->max_lba)
275  		return;
276  
277  	stripe_sz = raid_device->stripe_sz;
278  	stripe_exp = raid_device->stripe_exponent;
279  	stripe_off = v_lba & (stripe_sz - 1);
280  
281  	/* Return unless IO falls within a stripe */
282  	if (stripe_off + io_size > stripe_sz)
283  		return;
284  
285  	num_pds = raid_device->num_pds;
286  	p_lba = v_lba >> stripe_exp;
287  	column = sector_div(p_lba, num_pds);
288  	p_lba = (p_lba << stripe_exp) + stripe_off;
289  	mpi_request->DevHandle = cpu_to_le16(raid_device->pd_handle[column]);
290  
291  	if (cmd == READ_10 || cmd == WRITE_10)
292  		put_unaligned_be32(lower_32_bits(p_lba),
293  				   &mpi_request->CDB.CDB32[2]);
294  	else
295  		put_unaligned_be64(p_lba, &mpi_request->CDB.CDB32[2]);
296  
297  	st->direct_io = 1;
298  }
299