mpt3sas_warpdrive.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. * DISCLAIMER OF LIABILITY
  29. * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
  32. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  33. * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  34. * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
  35. * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program.
  38. */
  39. #include <linux/kernel.h>
  40. #include <linux/module.h>
  41. #include <linux/errno.h>
  42. #include <linux/types.h>
  43. #include <asm/unaligned.h>
  44. #include "mpt3sas_base.h"
  45. /**
  46. * _warpdrive_disable_ddio - Disable direct I/O for all the volumes
  47. * @ioc: per adapter object
  48. */
  49. static void
  50. _warpdrive_disable_ddio(struct MPT3SAS_ADAPTER *ioc)
  51. {
  52. Mpi2RaidVolPage1_t vol_pg1;
  53. Mpi2ConfigReply_t mpi_reply;
  54. struct _raid_device *raid_device;
  55. u16 handle;
  56. u16 ioc_status;
  57. unsigned long flags;
  58. handle = 0xFFFF;
  59. while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  60. &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  61. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  62. MPI2_IOCSTATUS_MASK;
  63. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  64. break;
  65. handle = le16_to_cpu(vol_pg1.DevHandle);
  66. spin_lock_irqsave(&ioc->raid_device_lock, flags);
  67. raid_device = mpt3sas_raid_device_find_by_handle(ioc, handle);
  68. if (raid_device)
  69. raid_device->direct_io_enabled = 0;
  70. spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
  71. }
  72. return;
  73. }
  74. /**
  75. * mpt3sas_get_num_volumes - Get number of volumes in the ioc
  76. * @ioc: per adapter object
  77. */
  78. u8
  79. mpt3sas_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. handle = 0xFFFF;
  87. while (!(mpt3sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
  88. &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
  89. ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
  90. MPI2_IOCSTATUS_MASK;
  91. if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  92. break;
  93. vol_cnt++;
  94. handle = le16_to_cpu(vol_pg1.DevHandle);
  95. }
  96. return vol_cnt;
  97. }
  98. /**
  99. * mpt3sas_init_warpdrive_properties - Set properties for warpdrive direct I/O.
  100. * @ioc: per adapter object
  101. * @raid_device: the raid_device object
  102. */
  103. void
  104. mpt3sas_init_warpdrive_properties(struct MPT3SAS_ADAPTER *ioc,
  105. struct _raid_device *raid_device)
  106. {
  107. Mpi2RaidVolPage0_t *vol_pg0;
  108. Mpi2RaidPhysDiskPage0_t pd_pg0;
  109. Mpi2ConfigReply_t mpi_reply;
  110. u16 sz;
  111. u8 num_pds, count;
  112. unsigned long stripe_sz, block_sz;
  113. u8 stripe_exp, block_exp;
  114. u64 dev_max_lba;
  115. if (!ioc->is_warpdrive)
  116. return;
  117. if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
  118. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  119. "globally as drives are exposed\n", ioc->name);
  120. return;
  121. }
  122. if (mpt3sas_get_num_volumes(ioc) > 1) {
  123. _warpdrive_disable_ddio(ioc);
  124. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  125. "globally as number of drives > 1\n", ioc->name);
  126. return;
  127. }
  128. if ((mpt3sas_config_get_number_pds(ioc, raid_device->handle,
  129. &num_pds)) || !num_pds) {
  130. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  131. "Failure in computing number of drives\n", ioc->name);
  132. return;
  133. }
  134. sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
  135. sizeof(Mpi2RaidVol0PhysDisk_t));
  136. vol_pg0 = kzalloc(sz, GFP_KERNEL);
  137. if (!vol_pg0) {
  138. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  139. "Memory allocation failure for RVPG0\n", ioc->name);
  140. return;
  141. }
  142. if ((mpt3sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
  143. MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
  144. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  145. "Failure in retrieving RVPG0\n", ioc->name);
  146. kfree(vol_pg0);
  147. return;
  148. }
  149. /*
  150. * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
  151. * assumed for WARPDRIVE, disable direct I/O
  152. */
  153. if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
  154. pr_warn(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  155. "for the drive with handle(0x%04x): num_mem=%d, "
  156. "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
  157. num_pds, MPT_MAX_WARPDRIVE_PDS);
  158. kfree(vol_pg0);
  159. return;
  160. }
  161. for (count = 0; count < num_pds; count++) {
  162. if (mpt3sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
  163. &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
  164. vol_pg0->PhysDisk[count].PhysDiskNum) ||
  165. pd_pg0.DevHandle == MPT3SAS_INVALID_DEVICE_HANDLE) {
  166. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is "
  167. "disabled for the drive with handle(0x%04x) member"
  168. "handle retrieval failed for member number=%d\n",
  169. ioc->name, raid_device->handle,
  170. vol_pg0->PhysDisk[count].PhysDiskNum);
  171. goto out_error;
  172. }
  173. /* Disable direct I/O if member drive lba exceeds 4 bytes */
  174. dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
  175. if (dev_max_lba >> 32) {
  176. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is "
  177. "disabled for the drive with handle(0x%04x) member"
  178. " handle (0x%04x) unsupported max lba 0x%016llx\n",
  179. ioc->name, raid_device->handle,
  180. le16_to_cpu(pd_pg0.DevHandle),
  181. (unsigned long long)dev_max_lba);
  182. goto out_error;
  183. }
  184. raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
  185. }
  186. /*
  187. * Assumption for WD: Direct I/O is not supported if the volume is
  188. * not RAID0
  189. */
  190. if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
  191. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  192. "for the drive with handle(0x%04x): type=%d, "
  193. "s_sz=%uK, blk_size=%u\n", ioc->name,
  194. raid_device->handle, raid_device->volume_type,
  195. (le32_to_cpu(vol_pg0->StripeSize) *
  196. le16_to_cpu(vol_pg0->BlockSize)) / 1024,
  197. le16_to_cpu(vol_pg0->BlockSize));
  198. goto out_error;
  199. }
  200. stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
  201. stripe_exp = find_first_bit(&stripe_sz, 32);
  202. if (stripe_exp == 32) {
  203. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  204. "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
  205. ioc->name, raid_device->handle,
  206. (le32_to_cpu(vol_pg0->StripeSize) *
  207. le16_to_cpu(vol_pg0->BlockSize)) / 1024);
  208. goto out_error;
  209. }
  210. raid_device->stripe_exponent = stripe_exp;
  211. block_sz = le16_to_cpu(vol_pg0->BlockSize);
  212. block_exp = find_first_bit(&block_sz, 16);
  213. if (block_exp == 16) {
  214. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is disabled "
  215. "for the drive with handle(0x%04x) invalid block sz %u\n",
  216. ioc->name, raid_device->handle,
  217. le16_to_cpu(vol_pg0->BlockSize));
  218. goto out_error;
  219. }
  220. raid_device->block_exponent = block_exp;
  221. raid_device->direct_io_enabled = 1;
  222. pr_info(MPT3SAS_FMT "WarpDrive : Direct IO is Enabled for the drive"
  223. " with handle(0x%04x)\n", ioc->name, raid_device->handle);
  224. /*
  225. * WARPDRIVE: Though the following fields are not used for direct IO,
  226. * stored for future purpose:
  227. */
  228. raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
  229. raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
  230. raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
  231. kfree(vol_pg0);
  232. return;
  233. out_error:
  234. raid_device->direct_io_enabled = 0;
  235. for (count = 0; count < num_pds; count++)
  236. raid_device->pd_handle[count] = 0;
  237. kfree(vol_pg0);
  238. return;
  239. }
  240. /**
  241. * mpt3sas_scsi_direct_io_get - returns direct io flag
  242. * @ioc: per adapter object
  243. * @smid: system request message index
  244. *
  245. * Returns the smid stored scmd pointer.
  246. */
  247. inline u8
  248. mpt3sas_scsi_direct_io_get(struct MPT3SAS_ADAPTER *ioc, u16 smid)
  249. {
  250. return ioc->scsi_lookup[smid - 1].direct_io;
  251. }
  252. /**
  253. * mpt3sas_scsi_direct_io_set - sets direct io flag
  254. * @ioc: per adapter object
  255. * @smid: system request message index
  256. * @direct_io: Zero or non-zero value to set in the direct_io flag
  257. *
  258. * Returns Nothing.
  259. */
  260. inline void
  261. mpt3sas_scsi_direct_io_set(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
  262. {
  263. ioc->scsi_lookup[smid - 1].direct_io = direct_io;
  264. }
  265. /**
  266. * mpt3sas_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
  267. * @ioc: per adapter object
  268. * @scmd: pointer to scsi command object
  269. * @raid_device: pointer to raid device data structure
  270. * @mpi_request: pointer to the SCSI_IO reqest message frame
  271. * @smid: system request message index
  272. *
  273. * Returns nothing
  274. */
  275. void
  276. mpt3sas_setup_direct_io(struct MPT3SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
  277. struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
  278. u16 smid)
  279. {
  280. sector_t v_lba, p_lba, stripe_off, column, io_size;
  281. u32 stripe_sz, stripe_exp;
  282. u8 num_pds, cmd = scmd->cmnd[0];
  283. if (cmd != READ_10 && cmd != WRITE_10 &&
  284. cmd != READ_16 && cmd != WRITE_16)
  285. return;
  286. if (cmd == READ_10 || cmd == WRITE_10)
  287. v_lba = get_unaligned_be32(&mpi_request->CDB.CDB32[2]);
  288. else
  289. v_lba = get_unaligned_be64(&mpi_request->CDB.CDB32[2]);
  290. io_size = scsi_bufflen(scmd) >> raid_device->block_exponent;
  291. if (v_lba + io_size - 1 > raid_device->max_lba)
  292. return;
  293. stripe_sz = raid_device->stripe_sz;
  294. stripe_exp = raid_device->stripe_exponent;
  295. stripe_off = v_lba & (stripe_sz - 1);
  296. /* Return unless IO falls within a stripe */
  297. if (stripe_off + io_size > stripe_sz)
  298. return;
  299. num_pds = raid_device->num_pds;
  300. p_lba = v_lba >> stripe_exp;
  301. column = sector_div(p_lba, num_pds);
  302. p_lba = (p_lba << stripe_exp) + stripe_off;
  303. mpi_request->DevHandle = cpu_to_le16(raid_device->pd_handle[column]);
  304. if (cmd == READ_10 || cmd == WRITE_10)
  305. put_unaligned_be32(lower_32_bits(p_lba),
  306. &mpi_request->CDB.CDB32[2]);
  307. else
  308. put_unaligned_be64(p_lba, &mpi_request->CDB.CDB32[2]);
  309. mpt3sas_scsi_direct_io_set(ioc, smid, 1);
  310. }