blob: acfaa9f3913f9cabc03b67da23aa6666da4fd677 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Sreekanth Reddye9edbe32014-09-12 15:35:18 +05305 * Copyright (C) 2007-2014 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.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; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
Eric Moore635374e2009-03-09 01:21:12 -060044#include <linux/module.h>
45#include <linux/kernel.h>
46#include <linux/init.h>
47#include <linux/errno.h>
48#include <linux/blkdev.h>
49#include <linux/sched.h>
50#include <linux/workqueue.h>
51#include <linux/delay.h>
52#include <linux/pci.h>
53#include <linux/interrupt.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053054#include <linux/aer.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053055#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060057
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +053058#include <asm/unaligned.h>
59
Eric Moore635374e2009-03-09 01:21:12 -060060#include "mpt2sas_base.h"
61
62MODULE_AUTHOR(MPT2SAS_AUTHOR);
63MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
64MODULE_LICENSE("GPL");
65MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
66
67#define RAID_CHANNEL 1
68
69/* forward proto's */
70static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
71 struct _sas_node *sas_expander);
72static void _firmware_event_work(struct work_struct *work);
73
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053074static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
75
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053076static void _scsih_scan_start(struct Scsi_Host *shost);
77static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
78
Eric Moore635374e2009-03-09 01:21:12 -060079/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060080LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060081
82/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060083static u8 scsi_io_cb_idx = -1;
84static u8 tm_cb_idx = -1;
85static u8 ctl_cb_idx = -1;
86static u8 base_cb_idx = -1;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053087static u8 port_enable_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060088static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053089static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060090static u8 config_cb_idx = -1;
91static int mpt_ids;
92
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053093static u8 tm_tr_cb_idx = -1 ;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053094static u8 tm_tr_volume_cb_idx = -1 ;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053095static u8 tm_sas_control_cb_idx = -1;
96
Eric Moore635374e2009-03-09 01:21:12 -060097/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060098static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060099MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
100 "(default=0)");
101
Kashyap, Desaia3e1e552011-06-14 10:56:12 +0530102static ushort max_sectors = 0xFFFF;
103module_param(max_sectors, ushort, 0);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +0530104MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
Kashyap, Desaia3e1e552011-06-14 10:56:12 +0530105
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +0530106static int missing_delay[2] = {-1, -1};
107module_param_array(missing_delay, int, NULL, 0);
108MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
109
Eric Moore635374e2009-03-09 01:21:12 -0600110/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
111#define MPT2SAS_MAX_LUN (16895)
112static int max_lun = MPT2SAS_MAX_LUN;
113module_param(max_lun, int, 0);
114MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
115
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530116/* diag_buffer_enable is bitwise
117 * bit 0 set = TRACE
118 * bit 1 set = SNAPSHOT
119 * bit 2 set = EXTENDED
120 *
121 * Either bit can be set, or both
122 */
123static int diag_buffer_enable = -1;
124module_param(diag_buffer_enable, int, 0);
125MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
126 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
127
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +0530128static int disable_discovery = -1;
129module_param(disable_discovery, int, 0);
130MODULE_PARM_DESC(disable_discovery, " disable discovery ");
131
Martin K. Petersen5e95e732012-08-28 14:29:37 -0400132/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
133static int prot_mask = 0;
134module_param(prot_mask, int, 0);
135MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
136
Eric Moore635374e2009-03-09 01:21:12 -0600137/**
138 * struct sense_info - common structure for obtaining sense keys
139 * @skey: sense key
140 * @asc: additional sense code
141 * @ascq: additional sense code qualifier
142 */
143struct sense_info {
144 u8 skey;
145 u8 asc;
146 u8 ascq;
147};
148
149
Sreekanth Reddybd58ea32014-09-12 15:35:19 +0530150#define MPT2SAS_TURN_ON_PFA_LED (0xFFFC)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530151#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
152#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
Eric Moore635374e2009-03-09 01:21:12 -0600153/**
154 * struct fw_event_work - firmware event struct
155 * @list: link list framework
156 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530157 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600158 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530159 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600160 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530161 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600162 * @ignore: flag meaning this event has been marked to ignore
163 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
164 * @event_data: reply event data payload follows
165 *
166 * This object stored on ioc->fw_event_list.
167 */
168struct fw_event_work {
169 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530170 u8 cancel_pending_work;
171 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600172 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530173 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600174 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530175 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600176 u8 ignore;
177 u16 event;
Joe Lawrence00713ad2014-06-25 17:03:33 -0400178 char event_data[0] __aligned(4);
Eric Moore635374e2009-03-09 01:21:12 -0600179};
180
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530181/* raid transport support */
182static struct raid_template *mpt2sas_raid_template;
183
Eric Moore635374e2009-03-09 01:21:12 -0600184/**
185 * struct _scsi_io_transfer - scsi io transfer
186 * @handle: sas device handle (assigned by firmware)
187 * @is_raid: flag set for hidden raid components
188 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
189 * @data_length: data transfer length
190 * @data_dma: dma pointer to data
191 * @sense: sense data
192 * @lun: lun number
193 * @cdb_length: cdb length
194 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600195 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530196 * @VF_ID: virtual function id
197 * @VP_ID: virtual port id
198 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600199 * @sense_length: sense length
200 * @ioc_status: ioc status
201 * @scsi_state: scsi state
202 * @scsi_status: scsi staus
203 * @log_info: log information
204 * @transfer_length: data length transfer when there is a reply message
205 *
206 * Used for sending internal scsi commands to devices within this module.
207 * Refer to _scsi_send_scsi_io().
208 */
209struct _scsi_io_transfer {
210 u16 handle;
211 u8 is_raid;
212 enum dma_data_direction dir;
213 u32 data_length;
214 dma_addr_t data_dma;
215 u8 sense[SCSI_SENSE_BUFFERSIZE];
216 u32 lun;
217 u8 cdb_length;
218 u8 cdb[32];
219 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530220 u8 VF_ID;
221 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600222 u8 valid_reply;
223 /* the following bits are only valid when 'valid_reply = 1' */
224 u32 sense_length;
225 u16 ioc_status;
226 u8 scsi_state;
227 u8 scsi_status;
228 u32 log_info;
229 u32 transfer_length;
230};
231
232/*
233 * The pci device ids are defined in mpi/mpi2_cnfg.h.
234 */
235static struct pci_device_id scsih_pci_table[] = {
236 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
237 PCI_ANY_ID, PCI_ANY_ID },
238 /* Falcon ~ 2008*/
239 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
240 PCI_ANY_ID, PCI_ANY_ID },
241 /* Liberator ~ 2108 */
242 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
243 PCI_ANY_ID, PCI_ANY_ID },
244 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
245 PCI_ANY_ID, PCI_ANY_ID },
246 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
247 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530248 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
250 PCI_ANY_ID, PCI_ANY_ID },
251 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
252 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530253 /* Thunderbolt ~ 2208 */
254 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
255 PCI_ANY_ID, PCI_ANY_ID },
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
257 PCI_ANY_ID, PCI_ANY_ID },
258 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
259 PCI_ANY_ID, PCI_ANY_ID },
260 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
261 PCI_ANY_ID, PCI_ANY_ID },
262 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
263 PCI_ANY_ID, PCI_ANY_ID },
264 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
265 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530266 /* Mustang ~ 2308 */
267 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530268 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530269 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
270 PCI_ANY_ID, PCI_ANY_ID },
271 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530272 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530273 /* SSS6200 */
274 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
275 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600276 {0} /* Terminating entry */
277};
278MODULE_DEVICE_TABLE(pci, scsih_pci_table);
279
280/**
Eric Moored5d135b2009-05-18 13:02:08 -0600281 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600282 *
283 * Note: The logging levels are defined in mpt2sas_debug.h.
284 */
285static int
Eric Moored5d135b2009-05-18 13:02:08 -0600286_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600287{
288 int ret = param_set_int(val, kp);
289 struct MPT2SAS_ADAPTER *ioc;
290
291 if (ret)
292 return ret;
293
294 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600295 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600296 ioc->logging_level = logging_level;
297 return 0;
298}
Eric Moored5d135b2009-05-18 13:02:08 -0600299module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600300 &logging_level, 0644);
301
302/**
303 * _scsih_srch_boot_sas_address - search based on sas_address
304 * @sas_address: sas address
305 * @boot_device: boot device object from bios page 2
306 *
307 * Returns 1 when there's a match, 0 means no match.
308 */
309static inline int
310_scsih_srch_boot_sas_address(u64 sas_address,
311 Mpi2BootDeviceSasWwid_t *boot_device)
312{
313 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
314}
315
316/**
317 * _scsih_srch_boot_device_name - search based on device name
318 * @device_name: device name specified in INDENTIFY fram
319 * @boot_device: boot device object from bios page 2
320 *
321 * Returns 1 when there's a match, 0 means no match.
322 */
323static inline int
324_scsih_srch_boot_device_name(u64 device_name,
325 Mpi2BootDeviceDeviceName_t *boot_device)
326{
327 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
328}
329
330/**
331 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
332 * @enclosure_logical_id: enclosure logical id
333 * @slot_number: slot number
334 * @boot_device: boot device object from bios page 2
335 *
336 * Returns 1 when there's a match, 0 means no match.
337 */
338static inline int
339_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
340 Mpi2BootDeviceEnclosureSlot_t *boot_device)
341{
342 return (enclosure_logical_id == le64_to_cpu(boot_device->
343 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
344 SlotNumber)) ? 1 : 0;
345}
346
347/**
348 * _scsih_is_boot_device - search for matching boot device.
349 * @sas_address: sas address
350 * @device_name: device name specified in INDENTIFY fram
351 * @enclosure_logical_id: enclosure logical id
352 * @slot_number: slot number
353 * @form: specifies boot device form
354 * @boot_device: boot device object from bios page 2
355 *
356 * Returns 1 when there's a match, 0 means no match.
357 */
358static int
359_scsih_is_boot_device(u64 sas_address, u64 device_name,
360 u64 enclosure_logical_id, u16 slot, u8 form,
361 Mpi2BiosPage2BootDevice_t *boot_device)
362{
363 int rc = 0;
364
365 switch (form) {
366 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
367 if (!sas_address)
368 break;
369 rc = _scsih_srch_boot_sas_address(
370 sas_address, &boot_device->SasWwid);
371 break;
372 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
373 if (!enclosure_logical_id)
374 break;
375 rc = _scsih_srch_boot_encl_slot(
376 enclosure_logical_id,
377 slot, &boot_device->EnclosureSlot);
378 break;
379 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
380 if (!device_name)
381 break;
382 rc = _scsih_srch_boot_device_name(
383 device_name, &boot_device->DeviceName);
384 break;
385 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
386 break;
387 }
388
389 return rc;
390}
391
392/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530393 * _scsih_get_sas_address - set the sas_address for given device handle
394 * @handle: device handle
395 * @sas_address: sas address
396 *
397 * Returns 0 success, non-zero when failure
398 */
399static int
400_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
401 u64 *sas_address)
402{
403 Mpi2SasDevicePage0_t sas_device_pg0;
404 Mpi2ConfigReply_t mpi_reply;
405 u32 ioc_status;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530406 *sas_address = 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530407
408 if (handle <= ioc->sas_hba.num_phys) {
409 *sas_address = ioc->sas_hba.sas_address;
410 return 0;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530411 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530412
413 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
414 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530415 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
416 __FILE__, __LINE__, __func__);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530417 return -ENXIO;
418 }
419
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530420 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
421 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
422 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
423 return 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530424 }
425
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530426 /* we hit this becuase the given parent handle doesn't exist */
427 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
428 return -ENXIO;
429 /* else error case */
430 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
431 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
432 __FILE__, __LINE__, __func__);
433 return -EIO;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530434}
435
436/**
Eric Moore635374e2009-03-09 01:21:12 -0600437 * _scsih_determine_boot_device - determine boot device.
438 * @ioc: per adapter object
439 * @device: either sas_device or raid_device object
440 * @is_raid: [flag] 1 = raid object, 0 = sas object
441 *
442 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300443 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600444 * There are primary, alternate, and current entries in bios page 2. The order
445 * priority is primary, alternate, then current. This routine saves
446 * the corresponding device object and is_raid flag in the ioc object.
447 * The saved data to be used later in _scsih_probe_boot_devices().
448 */
449static void
450_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
451 void *device, u8 is_raid)
452{
453 struct _sas_device *sas_device;
454 struct _raid_device *raid_device;
455 u64 sas_address;
456 u64 device_name;
457 u64 enclosure_logical_id;
458 u16 slot;
459
460 /* only process this function when driver loads */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530461 if (!ioc->is_driver_loading)
462 return;
463
464 /* no Bios, return immediately */
465 if (!ioc->bios_pg3.BiosVersion)
Eric Moore635374e2009-03-09 01:21:12 -0600466 return;
467
468 if (!is_raid) {
469 sas_device = device;
470 sas_address = sas_device->sas_address;
471 device_name = sas_device->device_name;
472 enclosure_logical_id = sas_device->enclosure_logical_id;
473 slot = sas_device->slot;
474 } else {
475 raid_device = device;
476 sas_address = raid_device->wwid;
477 device_name = 0;
478 enclosure_logical_id = 0;
479 slot = 0;
480 }
481
482 if (!ioc->req_boot_device.device) {
483 if (_scsih_is_boot_device(sas_address, device_name,
484 enclosure_logical_id, slot,
485 (ioc->bios_pg2.ReqBootDeviceForm &
486 MPI2_BIOSPAGE2_FORM_MASK),
487 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530488 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600489 "%s: req_boot_device(0x%016llx)\n",
490 ioc->name, __func__,
491 (unsigned long long)sas_address));
492 ioc->req_boot_device.device = device;
493 ioc->req_boot_device.is_raid = is_raid;
494 }
495 }
496
497 if (!ioc->req_alt_boot_device.device) {
498 if (_scsih_is_boot_device(sas_address, device_name,
499 enclosure_logical_id, slot,
500 (ioc->bios_pg2.ReqAltBootDeviceForm &
501 MPI2_BIOSPAGE2_FORM_MASK),
502 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530503 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600504 "%s: req_alt_boot_device(0x%016llx)\n",
505 ioc->name, __func__,
506 (unsigned long long)sas_address));
507 ioc->req_alt_boot_device.device = device;
508 ioc->req_alt_boot_device.is_raid = is_raid;
509 }
510 }
511
512 if (!ioc->current_boot_device.device) {
513 if (_scsih_is_boot_device(sas_address, device_name,
514 enclosure_logical_id, slot,
515 (ioc->bios_pg2.CurrentBootDeviceForm &
516 MPI2_BIOSPAGE2_FORM_MASK),
517 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530518 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600519 "%s: current_boot_device(0x%016llx)\n",
520 ioc->name, __func__,
521 (unsigned long long)sas_address));
522 ioc->current_boot_device.device = device;
523 ioc->current_boot_device.is_raid = is_raid;
524 }
525 }
526}
527
528/**
529 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
530 * @ioc: per adapter object
531 * @sas_address: sas address
532 * Context: Calling function should acquire ioc->sas_device_lock
533 *
534 * This searches for sas_device based on sas_address, then return sas_device
535 * object.
536 */
537struct _sas_device *
538mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
539 u64 sas_address)
540{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530541 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600542
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530543 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
544 if (sas_device->sas_address == sas_address)
545 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600546
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530547 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
548 if (sas_device->sas_address == sas_address)
549 return sas_device;
550
551 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600552}
553
554/**
555 * _scsih_sas_device_find_by_handle - sas device search
556 * @ioc: per adapter object
557 * @handle: sas device handle (assigned by firmware)
558 * Context: Calling function should acquire ioc->sas_device_lock
559 *
560 * This searches for sas_device based on sas_address, then return sas_device
561 * object.
562 */
563static struct _sas_device *
564_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
565{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530566 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600567
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530568 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
569 if (sas_device->handle == handle)
570 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600571
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530572 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
573 if (sas_device->handle == handle)
574 return sas_device;
575
576 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600577}
578
579/**
580 * _scsih_sas_device_remove - remove sas_device from list.
581 * @ioc: per adapter object
582 * @sas_device: the sas_device object
583 * Context: This function will acquire ioc->sas_device_lock.
584 *
585 * Removing object and freeing associated memory from the ioc->sas_device_list.
586 */
587static void
588_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
589 struct _sas_device *sas_device)
590{
591 unsigned long flags;
592
Kashyap, Desai980ead32010-04-08 17:55:22 +0530593 if (!sas_device)
594 return;
595
Eric Moore635374e2009-03-09 01:21:12 -0600596 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530597 list_del(&sas_device->list);
598 kfree(sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600599 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
600}
601
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530602
Eric Moore635374e2009-03-09 01:21:12 -0600603/**
604 * _scsih_sas_device_add - insert sas_device to the list.
605 * @ioc: per adapter object
606 * @sas_device: the sas_device object
607 * Context: This function will acquire ioc->sas_device_lock.
608 *
609 * Adding new object to the ioc->sas_device_list.
610 */
611static void
612_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
613 struct _sas_device *sas_device)
614{
615 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600616
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530617 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600618 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
619 sas_device->handle, (unsigned long long)sas_device->sas_address));
620
621 spin_lock_irqsave(&ioc->sas_device_lock, flags);
622 list_add_tail(&sas_device->list, &ioc->sas_device_list);
623 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
624
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530625 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530626 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -0600627 _scsih_sas_device_remove(ioc, sas_device);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530628 } else if (!sas_device->starget) {
629 /* When asyn scanning is enabled, its not possible to remove
630 * devices while scanning is turned on due to an oops in
631 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
632 */
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +0530633 if (!ioc->is_driver_loading) {
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530634 mpt2sas_transport_port_remove(ioc,
635 sas_device->sas_address,
636 sas_device->sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +0530637 _scsih_sas_device_remove(ioc, sas_device);
638 }
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530639 }
Eric Moore635374e2009-03-09 01:21:12 -0600640}
641
642/**
643 * _scsih_sas_device_init_add - insert sas_device to the list.
644 * @ioc: per adapter object
645 * @sas_device: the sas_device object
646 * Context: This function will acquire ioc->sas_device_lock.
647 *
648 * Adding new object at driver load time to the ioc->sas_device_init_list.
649 */
650static void
651_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
652 struct _sas_device *sas_device)
653{
654 unsigned long flags;
655
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530656 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600657 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
658 sas_device->handle, (unsigned long long)sas_device->sas_address));
659
660 spin_lock_irqsave(&ioc->sas_device_lock, flags);
661 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
Eric Moore635374e2009-03-09 01:21:12 -0600662 _scsih_determine_boot_device(ioc, sas_device, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530663 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600664}
665
666/**
Eric Moore635374e2009-03-09 01:21:12 -0600667 * _scsih_raid_device_find_by_id - raid device search
668 * @ioc: per adapter object
669 * @id: sas device target id
670 * @channel: sas device channel
671 * Context: Calling function should acquire ioc->raid_device_lock
672 *
673 * This searches for raid_device based on target id, then return raid_device
674 * object.
675 */
676static struct _raid_device *
677_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
678{
679 struct _raid_device *raid_device, *r;
680
681 r = NULL;
682 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
683 if (raid_device->id == id && raid_device->channel == channel) {
684 r = raid_device;
685 goto out;
686 }
687 }
688
689 out:
690 return r;
691}
692
693/**
694 * _scsih_raid_device_find_by_handle - raid device search
695 * @ioc: per adapter object
696 * @handle: sas device handle (assigned by firmware)
697 * Context: Calling function should acquire ioc->raid_device_lock
698 *
699 * This searches for raid_device based on handle, then return raid_device
700 * object.
701 */
702static struct _raid_device *
703_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
704{
705 struct _raid_device *raid_device, *r;
706
707 r = NULL;
708 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
709 if (raid_device->handle != handle)
710 continue;
711 r = raid_device;
712 goto out;
713 }
714
715 out:
716 return r;
717}
718
719/**
720 * _scsih_raid_device_find_by_wwid - raid device search
721 * @ioc: per adapter object
722 * @handle: sas device handle (assigned by firmware)
723 * Context: Calling function should acquire ioc->raid_device_lock
724 *
725 * This searches for raid_device based on wwid, then return raid_device
726 * object.
727 */
728static struct _raid_device *
729_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
730{
731 struct _raid_device *raid_device, *r;
732
733 r = NULL;
734 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
735 if (raid_device->wwid != wwid)
736 continue;
737 r = raid_device;
738 goto out;
739 }
740
741 out:
742 return r;
743}
744
745/**
746 * _scsih_raid_device_add - add raid_device object
747 * @ioc: per adapter object
748 * @raid_device: raid_device object
749 *
750 * This is added to the raid_device_list link list.
751 */
752static void
753_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
754 struct _raid_device *raid_device)
755{
756 unsigned long flags;
757
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530758 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600759 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
760 raid_device->handle, (unsigned long long)raid_device->wwid));
761
762 spin_lock_irqsave(&ioc->raid_device_lock, flags);
763 list_add_tail(&raid_device->list, &ioc->raid_device_list);
764 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
765}
766
767/**
768 * _scsih_raid_device_remove - delete raid_device object
769 * @ioc: per adapter object
770 * @raid_device: raid_device object
771 *
Eric Moore635374e2009-03-09 01:21:12 -0600772 */
773static void
774_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
775 struct _raid_device *raid_device)
776{
777 unsigned long flags;
778
779 spin_lock_irqsave(&ioc->raid_device_lock, flags);
780 list_del(&raid_device->list);
Eric Moore635374e2009-03-09 01:21:12 -0600781 kfree(raid_device);
782 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
783}
784
785/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530786 * mpt2sas_scsih_expander_find_by_handle - expander device search
787 * @ioc: per adapter object
788 * @handle: expander handle (assigned by firmware)
789 * Context: Calling function should acquire ioc->sas_device_lock
790 *
791 * This searches for expander device based on handle, then returns the
792 * sas_node object.
793 */
794struct _sas_node *
795mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
796{
797 struct _sas_node *sas_expander, *r;
798
799 r = NULL;
800 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
801 if (sas_expander->handle != handle)
802 continue;
803 r = sas_expander;
804 goto out;
805 }
806 out:
807 return r;
808}
809
810/**
Eric Moore635374e2009-03-09 01:21:12 -0600811 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
812 * @ioc: per adapter object
813 * @sas_address: sas address
814 * Context: Calling function should acquire ioc->sas_node_lock.
815 *
816 * This searches for expander device based on sas_address, then returns the
817 * sas_node object.
818 */
819struct _sas_node *
820mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
821 u64 sas_address)
822{
823 struct _sas_node *sas_expander, *r;
824
825 r = NULL;
826 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
827 if (sas_expander->sas_address != sas_address)
828 continue;
829 r = sas_expander;
830 goto out;
831 }
832 out:
833 return r;
834}
835
836/**
837 * _scsih_expander_node_add - insert expander device to the list.
838 * @ioc: per adapter object
839 * @sas_expander: the sas_device object
840 * Context: This function will acquire ioc->sas_node_lock.
841 *
842 * Adding new object to the ioc->sas_expander_list.
843 *
844 * Return nothing.
845 */
846static void
847_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
848 struct _sas_node *sas_expander)
849{
850 unsigned long flags;
851
852 spin_lock_irqsave(&ioc->sas_node_lock, flags);
853 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
854 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
855}
856
857/**
858 * _scsih_is_end_device - determines if device is an end device
859 * @device_info: bitfield providing information about the device.
860 * Context: none
861 *
862 * Returns 1 if end device.
863 */
864static int
865_scsih_is_end_device(u32 device_info)
866{
867 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
868 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
869 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
870 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
871 return 1;
872 else
873 return 0;
874}
875
876/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530877 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600878 * @ioc: per adapter object
879 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600880 *
881 * Returns the smid stored scmd pointer.
882 */
883static struct scsi_cmnd *
884_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
885{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530886 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600887}
888
889/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530890 * _scsih_scsi_lookup_get_clear - returns scmd entry
891 * @ioc: per adapter object
892 * @smid: system request message index
893 *
894 * Returns the smid stored scmd pointer.
895 * Then will derefrence the stored scmd pointer.
896 */
897static inline struct scsi_cmnd *
898_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
899{
900 unsigned long flags;
901 struct scsi_cmnd *scmd;
902
903 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
904 scmd = ioc->scsi_lookup[smid - 1].scmd;
905 ioc->scsi_lookup[smid - 1].scmd = NULL;
906 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
907
908 return scmd;
909}
910
911/**
Eric Moore635374e2009-03-09 01:21:12 -0600912 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
913 * @ioc: per adapter object
914 * @smid: system request message index
915 * @scmd: pointer to scsi command object
916 * Context: This function will acquire ioc->scsi_lookup_lock.
917 *
918 * This will search for a scmd pointer in the scsi_lookup array,
919 * returning the revelent smid. A returned value of zero means invalid.
920 */
921static u16
922_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
923 *scmd)
924{
925 u16 smid;
926 unsigned long flags;
927 int i;
928
929 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
930 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530931 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600932 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530933 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600934 goto out;
935 }
936 }
937 out:
938 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
939 return smid;
940}
941
942/**
943 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
944 * @ioc: per adapter object
945 * @id: target id
946 * @channel: channel
947 * Context: This function will acquire ioc->scsi_lookup_lock.
948 *
949 * This will search for a matching channel:id in the scsi_lookup array,
950 * returning 1 if found.
951 */
952static u8
953_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
954 int channel)
955{
956 u8 found;
957 unsigned long flags;
958 int i;
959
960 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
961 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530962 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600963 if (ioc->scsi_lookup[i].scmd &&
964 (ioc->scsi_lookup[i].scmd->device->id == id &&
965 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
966 found = 1;
967 goto out;
968 }
969 }
970 out:
971 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
972 return found;
973}
974
975/**
Eric Moore993e0da2009-05-18 13:00:45 -0600976 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
977 * @ioc: per adapter object
978 * @id: target id
979 * @lun: lun number
980 * @channel: channel
981 * Context: This function will acquire ioc->scsi_lookup_lock.
982 *
983 * This will search for a matching channel:id:lun in the scsi_lookup array,
984 * returning 1 if found.
985 */
986static u8
987_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
988 unsigned int lun, int channel)
989{
990 u8 found;
991 unsigned long flags;
992 int i;
993
994 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
995 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530996 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600997 if (ioc->scsi_lookup[i].scmd &&
998 (ioc->scsi_lookup[i].scmd->device->id == id &&
999 ioc->scsi_lookup[i].scmd->device->channel == channel &&
1000 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
1001 found = 1;
1002 goto out;
1003 }
1004 }
1005 out:
1006 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1007 return found;
1008}
1009
1010/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301011 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -06001012 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301013 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -06001014 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301015 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001016 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301017static struct chain_tracker *
1018_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001019{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301020 struct chain_tracker *chain_req;
1021 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001022
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301023 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1024 if (list_empty(&ioc->free_chain_list)) {
1025 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05301026 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
1027 "available\n", ioc->name));
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301028 return NULL;
1029 }
1030 chain_req = list_entry(ioc->free_chain_list.next,
1031 struct chain_tracker, tracker_list);
1032 list_del_init(&chain_req->tracker_list);
1033 list_add_tail(&chain_req->tracker_list,
1034 &ioc->scsi_lookup[smid - 1].chain_list);
1035 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1036 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001037}
1038
1039/**
1040 * _scsih_build_scatter_gather - main sg creation routine
1041 * @ioc: per adapter object
1042 * @scmd: scsi command
1043 * @smid: system request message index
1044 * Context: none.
1045 *
1046 * The main routine that builds scatter gather table from a given
1047 * scsi request sent via the .queuecommand main handler.
1048 *
1049 * Returns 0 success, anything else error
1050 */
1051static int
1052_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1053 struct scsi_cmnd *scmd, u16 smid)
1054{
1055 Mpi2SCSIIORequest_t *mpi_request;
1056 dma_addr_t chain_dma;
1057 struct scatterlist *sg_scmd;
1058 void *sg_local, *chain;
1059 u32 chain_offset;
1060 u32 chain_length;
1061 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001062 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001063 u32 sges_in_segment;
1064 u32 sgl_flags;
1065 u32 sgl_flags_last_element;
1066 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301067 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001068
1069 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1070
1071 /* init scatter gather flags */
1072 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1073 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1074 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1075 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1076 << MPI2_SGE_FLAGS_SHIFT;
1077 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1078 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1079 << MPI2_SGE_FLAGS_SHIFT;
1080 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1081
1082 sg_scmd = scsi_sglist(scmd);
1083 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001084 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001085 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1086 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1087 return -ENOMEM;
1088 }
1089
1090 sg_local = &mpi_request->SGL;
1091 sges_in_segment = ioc->max_sges_in_main_message;
1092 if (sges_left <= sges_in_segment)
1093 goto fill_in_last_segment;
1094
1095 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1096 (sges_in_segment * ioc->sge_size))/4;
1097
1098 /* fill in main message segment when there is a chain following */
1099 while (sges_in_segment) {
1100 if (sges_in_segment == 1)
1101 ioc->base_add_sg_single(sg_local,
1102 sgl_flags_last_element | sg_dma_len(sg_scmd),
1103 sg_dma_address(sg_scmd));
1104 else
1105 ioc->base_add_sg_single(sg_local, sgl_flags |
1106 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1107 sg_scmd = sg_next(sg_scmd);
1108 sg_local += ioc->sge_size;
1109 sges_left--;
1110 sges_in_segment--;
1111 }
1112
1113 /* initializing the chain flags and pointers */
1114 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301115 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1116 if (!chain_req)
1117 return -1;
1118 chain = chain_req->chain_buffer;
1119 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001120 do {
1121 sges_in_segment = (sges_left <=
1122 ioc->max_sges_in_chain_message) ? sges_left :
1123 ioc->max_sges_in_chain_message;
1124 chain_offset = (sges_left == sges_in_segment) ?
1125 0 : (sges_in_segment * ioc->sge_size)/4;
1126 chain_length = sges_in_segment * ioc->sge_size;
1127 if (chain_offset) {
1128 chain_offset = chain_offset <<
1129 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1130 chain_length += ioc->sge_size;
1131 }
1132 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1133 chain_length, chain_dma);
1134 sg_local = chain;
1135 if (!chain_offset)
1136 goto fill_in_last_segment;
1137
1138 /* fill in chain segments */
1139 while (sges_in_segment) {
1140 if (sges_in_segment == 1)
1141 ioc->base_add_sg_single(sg_local,
1142 sgl_flags_last_element |
1143 sg_dma_len(sg_scmd),
1144 sg_dma_address(sg_scmd));
1145 else
1146 ioc->base_add_sg_single(sg_local, sgl_flags |
1147 sg_dma_len(sg_scmd),
1148 sg_dma_address(sg_scmd));
1149 sg_scmd = sg_next(sg_scmd);
1150 sg_local += ioc->sge_size;
1151 sges_left--;
1152 sges_in_segment--;
1153 }
1154
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301155 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1156 if (!chain_req)
1157 return -1;
1158 chain = chain_req->chain_buffer;
1159 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001160 } while (1);
1161
1162
1163 fill_in_last_segment:
1164
1165 /* fill the last segment */
1166 while (sges_left) {
1167 if (sges_left == 1)
1168 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1169 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1170 else
1171 ioc->base_add_sg_single(sg_local, sgl_flags |
1172 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1173 sg_scmd = sg_next(sg_scmd);
1174 sg_local += ioc->sge_size;
1175 sges_left--;
1176 }
1177
1178 return 0;
1179}
1180
1181/**
Christoph Hellwig286c9402014-11-13 15:11:59 +01001182 * _scsih_change_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001183 * @sdev: scsi device struct
1184 * @qdepth: requested queue depth
1185 *
Christoph Hellwig286c9402014-11-13 15:11:59 +01001186 * Returns queue depth.
Eric Moore635374e2009-03-09 01:21:12 -06001187 */
Christoph Hellwig286c9402014-11-13 15:11:59 +01001188static int
1189_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001190{
1191 struct Scsi_Host *shost = sdev->host;
1192 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301193 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1194 struct MPT2SAS_DEVICE *sas_device_priv_data;
1195 struct MPT2SAS_TARGET *sas_target_priv_data;
1196 struct _sas_device *sas_device;
1197 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001198
1199 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301200
1201 /* limit max device queue for SATA to 32 */
1202 sas_device_priv_data = sdev->hostdata;
1203 if (!sas_device_priv_data)
1204 goto not_sata;
1205 sas_target_priv_data = sas_device_priv_data->sas_target;
1206 if (!sas_target_priv_data)
1207 goto not_sata;
1208 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1209 goto not_sata;
1210 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1211 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1212 sas_device_priv_data->sas_target->sas_address);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301213 if (sas_device && sas_device->device_info &
1214 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1215 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301216 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301217
1218 not_sata:
Eric Moore635374e2009-03-09 01:21:12 -06001219 if (!sdev->tagged_supported)
1220 max_depth = 1;
1221 if (qdepth > max_depth)
1222 qdepth = max_depth;
Christoph Hellwig286c9402014-11-13 15:11:59 +01001223 return scsi_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06001224}
1225
1226/**
Eric Moored5d135b2009-05-18 13:02:08 -06001227 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001228 * @starget: scsi target struct
1229 *
1230 * Returns 0 if ok. Any other return is assumed to be an error and
1231 * the device is ignored.
1232 */
1233static int
Eric Moored5d135b2009-05-18 13:02:08 -06001234_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001235{
1236 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1237 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1238 struct MPT2SAS_TARGET *sas_target_priv_data;
1239 struct _sas_device *sas_device;
1240 struct _raid_device *raid_device;
1241 unsigned long flags;
1242 struct sas_rphy *rphy;
1243
Joe Lawrencefeafe7c2014-06-25 17:03:14 -04001244 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1245 GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06001246 if (!sas_target_priv_data)
1247 return -ENOMEM;
1248
1249 starget->hostdata = sas_target_priv_data;
1250 sas_target_priv_data->starget = starget;
1251 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1252
1253 /* RAID volumes */
1254 if (starget->channel == RAID_CHANNEL) {
1255 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1256 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1257 starget->channel);
1258 if (raid_device) {
1259 sas_target_priv_data->handle = raid_device->handle;
1260 sas_target_priv_data->sas_address = raid_device->wwid;
1261 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301262 if (ioc->is_warpdrive)
1263 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001264 raid_device->starget = starget;
1265 }
1266 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1267 return 0;
1268 }
1269
1270 /* sas/sata devices */
1271 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1272 rphy = dev_to_rphy(starget->dev.parent);
1273 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1274 rphy->identify.sas_address);
1275
1276 if (sas_device) {
1277 sas_target_priv_data->handle = sas_device->handle;
1278 sas_target_priv_data->sas_address = sas_device->sas_address;
1279 sas_device->starget = starget;
1280 sas_device->id = starget->id;
1281 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301282 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001283 sas_target_priv_data->flags |=
1284 MPT_TARGET_FLAGS_RAID_COMPONENT;
1285 }
1286 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1287
1288 return 0;
1289}
1290
1291/**
Eric Moored5d135b2009-05-18 13:02:08 -06001292 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001293 * @starget: scsi target struct
1294 *
1295 * Returns nothing.
1296 */
1297static void
Eric Moored5d135b2009-05-18 13:02:08 -06001298_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001299{
1300 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1301 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1302 struct MPT2SAS_TARGET *sas_target_priv_data;
1303 struct _sas_device *sas_device;
1304 struct _raid_device *raid_device;
1305 unsigned long flags;
1306 struct sas_rphy *rphy;
1307
1308 sas_target_priv_data = starget->hostdata;
1309 if (!sas_target_priv_data)
1310 return;
1311
1312 if (starget->channel == RAID_CHANNEL) {
1313 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1314 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1315 starget->channel);
1316 if (raid_device) {
1317 raid_device->starget = NULL;
1318 raid_device->sdev = NULL;
1319 }
1320 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1321 goto out;
1322 }
1323
1324 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1325 rphy = dev_to_rphy(starget->dev.parent);
1326 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1327 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001328 if (sas_device && (sas_device->starget == starget) &&
1329 (sas_device->id == starget->id) &&
1330 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001331 sas_device->starget = NULL;
1332
1333 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1334
1335 out:
1336 kfree(sas_target_priv_data);
1337 starget->hostdata = NULL;
1338}
1339
1340/**
Eric Moored5d135b2009-05-18 13:02:08 -06001341 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001342 * @sdev: scsi device struct
1343 *
1344 * Returns 0 if ok. Any other return is assumed to be an error and
1345 * the device is ignored.
1346 */
1347static int
Eric Moored5d135b2009-05-18 13:02:08 -06001348_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001349{
1350 struct Scsi_Host *shost;
1351 struct MPT2SAS_ADAPTER *ioc;
1352 struct MPT2SAS_TARGET *sas_target_priv_data;
1353 struct MPT2SAS_DEVICE *sas_device_priv_data;
1354 struct scsi_target *starget;
1355 struct _raid_device *raid_device;
Sreekanth Reddy63e359d2013-07-25 11:32:51 +05301356 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -06001357 unsigned long flags;
1358
Joe Lawrencefeafe7c2014-06-25 17:03:14 -04001359 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1360 GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06001361 if (!sas_device_priv_data)
1362 return -ENOMEM;
1363
1364 sas_device_priv_data->lun = sdev->lun;
1365 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1366
1367 starget = scsi_target(sdev);
1368 sas_target_priv_data = starget->hostdata;
1369 sas_target_priv_data->num_luns++;
1370 sas_device_priv_data->sas_target = sas_target_priv_data;
1371 sdev->hostdata = sas_device_priv_data;
1372 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1373 sdev->no_uld_attach = 1;
1374
1375 shost = dev_to_shost(&starget->dev);
1376 ioc = shost_priv(shost);
1377 if (starget->channel == RAID_CHANNEL) {
1378 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1379 raid_device = _scsih_raid_device_find_by_id(ioc,
1380 starget->id, starget->channel);
1381 if (raid_device)
1382 raid_device->sdev = sdev; /* raid is single lun */
1383 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001384 }
1385
Sreekanth Reddy63e359d2013-07-25 11:32:51 +05301386 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1387 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1388 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1389 sas_target_priv_data->sas_address);
1390 if (sas_device && (sas_device->starget == NULL)) {
1391 sdev_printk(KERN_INFO, sdev,
1392 "%s : sas_device->starget set to starget @ %d\n",
1393 __func__, __LINE__);
1394 sas_device->starget = starget;
1395 }
1396 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1397 }
1398
Eric Moore635374e2009-03-09 01:21:12 -06001399 return 0;
1400}
1401
1402/**
Eric Moored5d135b2009-05-18 13:02:08 -06001403 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001404 * @sdev: scsi device struct
1405 *
1406 * Returns nothing.
1407 */
1408static void
Eric Moored5d135b2009-05-18 13:02:08 -06001409_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001410{
1411 struct MPT2SAS_TARGET *sas_target_priv_data;
1412 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301413 struct Scsi_Host *shost;
1414 struct MPT2SAS_ADAPTER *ioc;
1415 struct _sas_device *sas_device;
1416 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001417
1418 if (!sdev->hostdata)
1419 return;
1420
1421 starget = scsi_target(sdev);
1422 sas_target_priv_data = starget->hostdata;
1423 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301424
1425 shost = dev_to_shost(&starget->dev);
1426 ioc = shost_priv(shost);
1427
1428 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1429 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1430 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1431 sas_target_priv_data->sas_address);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +05301432 if (sas_device && !sas_target_priv_data->num_luns)
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301433 sas_device->starget = NULL;
1434 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1435 }
1436
Eric Moore635374e2009-03-09 01:21:12 -06001437 kfree(sdev->hostdata);
1438 sdev->hostdata = NULL;
1439}
1440
1441/**
Eric Moored5d135b2009-05-18 13:02:08 -06001442 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001443 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301444 * @handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -06001445 * @sdev: scsi device struct
1446 */
1447static void
Eric Moored5d135b2009-05-18 13:02:08 -06001448_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301449 u16 handle, struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001450{
1451 Mpi2ConfigReply_t mpi_reply;
1452 Mpi2SasDevicePage0_t sas_device_pg0;
1453 u32 ioc_status;
1454 u16 flags;
1455 u32 device_info;
1456
1457 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301458 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06001459 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1460 ioc->name, __FILE__, __LINE__, __func__);
1461 return;
1462 }
1463
1464 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1465 MPI2_IOCSTATUS_MASK;
1466 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1467 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1468 ioc->name, __FILE__, __LINE__, __func__);
1469 return;
1470 }
1471
1472 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301473 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001474
1475 sdev_printk(KERN_INFO, sdev,
1476 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1477 "sw_preserve(%s)\n",
1478 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1479 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1480 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1481 "n",
1482 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1483 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1484 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1485}
1486
1487/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301488 * _scsih_is_raid - return boolean indicating device is raid volume
1489 * @dev the device struct object
1490 */
1491static int
1492_scsih_is_raid(struct device *dev)
1493{
1494 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301495 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301496
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301497 if (ioc->is_warpdrive)
1498 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301499 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1500}
1501
1502/**
1503 * _scsih_get_resync - get raid volume resync percent complete
1504 * @dev the device struct object
1505 */
1506static void
1507_scsih_get_resync(struct device *dev)
1508{
1509 struct scsi_device *sdev = to_scsi_device(dev);
1510 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1511 static struct _raid_device *raid_device;
1512 unsigned long flags;
1513 Mpi2RaidVolPage0_t vol_pg0;
1514 Mpi2ConfigReply_t mpi_reply;
1515 u32 volume_status_flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301516 u8 percent_complete;
1517 u16 handle;
1518
1519 percent_complete = 0;
1520 handle = 0;
1521 if (ioc->is_warpdrive)
1522 goto out;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301523
1524 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1525 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1526 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301527 if (raid_device) {
1528 handle = raid_device->handle;
1529 percent_complete = raid_device->percent_complete;
1530 }
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301531 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1532
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301533 if (!handle)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301534 goto out;
1535
1536 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301537 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301538 sizeof(Mpi2RaidVolPage0_t))) {
1539 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1540 ioc->name, __FILE__, __LINE__, __func__);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301541 percent_complete = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301542 goto out;
1543 }
1544
1545 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301546 if (!(volume_status_flags &
1547 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1548 percent_complete = 0;
1549
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301550 out:
1551 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1552}
1553
1554/**
1555 * _scsih_get_state - get raid volume level
1556 * @dev the device struct object
1557 */
1558static void
1559_scsih_get_state(struct device *dev)
1560{
1561 struct scsi_device *sdev = to_scsi_device(dev);
1562 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1563 static struct _raid_device *raid_device;
1564 unsigned long flags;
1565 Mpi2RaidVolPage0_t vol_pg0;
1566 Mpi2ConfigReply_t mpi_reply;
1567 u32 volstate;
1568 enum raid_state state = RAID_STATE_UNKNOWN;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301569 u16 handle = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301570
1571 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1572 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1573 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301574 if (raid_device)
1575 handle = raid_device->handle;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301576 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1577
1578 if (!raid_device)
1579 goto out;
1580
1581 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301582 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301583 sizeof(Mpi2RaidVolPage0_t))) {
1584 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1585 ioc->name, __FILE__, __LINE__, __func__);
1586 goto out;
1587 }
1588
1589 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1590 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1591 state = RAID_STATE_RESYNCING;
1592 goto out;
1593 }
1594
1595 switch (vol_pg0.VolumeState) {
1596 case MPI2_RAID_VOL_STATE_OPTIMAL:
1597 case MPI2_RAID_VOL_STATE_ONLINE:
1598 state = RAID_STATE_ACTIVE;
1599 break;
1600 case MPI2_RAID_VOL_STATE_DEGRADED:
1601 state = RAID_STATE_DEGRADED;
1602 break;
1603 case MPI2_RAID_VOL_STATE_FAILED:
1604 case MPI2_RAID_VOL_STATE_MISSING:
1605 state = RAID_STATE_OFFLINE;
1606 break;
1607 }
1608 out:
1609 raid_set_state(mpt2sas_raid_template, dev, state);
1610}
1611
1612/**
1613 * _scsih_set_level - set raid level
1614 * @sdev: scsi device struct
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301615 * @volume_type: volume type
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301616 */
1617static void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301618_scsih_set_level(struct scsi_device *sdev, u8 volume_type)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301619{
1620 enum raid_level level = RAID_LEVEL_UNKNOWN;
1621
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301622 switch (volume_type) {
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301623 case MPI2_RAID_VOL_TYPE_RAID0:
1624 level = RAID_LEVEL_0;
1625 break;
1626 case MPI2_RAID_VOL_TYPE_RAID10:
1627 level = RAID_LEVEL_10;
1628 break;
1629 case MPI2_RAID_VOL_TYPE_RAID1E:
1630 level = RAID_LEVEL_1E;
1631 break;
1632 case MPI2_RAID_VOL_TYPE_RAID1:
1633 level = RAID_LEVEL_1;
1634 break;
1635 }
1636
1637 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1638}
1639
1640/**
Eric Moore635374e2009-03-09 01:21:12 -06001641 * _scsih_get_volume_capabilities - volume capabilities
1642 * @ioc: per adapter object
1643 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301644 *
1645 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001646 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301647static int
Eric Moore635374e2009-03-09 01:21:12 -06001648_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1649 struct _raid_device *raid_device)
1650{
1651 Mpi2RaidVolPage0_t *vol_pg0;
1652 Mpi2RaidPhysDiskPage0_t pd_pg0;
1653 Mpi2SasDevicePage0_t sas_device_pg0;
1654 Mpi2ConfigReply_t mpi_reply;
1655 u16 sz;
1656 u8 num_pds;
1657
1658 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1659 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301660 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1661 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1662 __func__));
1663 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001664 }
1665
1666 raid_device->num_pds = num_pds;
1667 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1668 sizeof(Mpi2RaidVol0PhysDisk_t));
1669 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1670 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301671 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1672 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1673 __func__));
1674 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001675 }
1676
1677 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1678 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301679 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1680 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1681 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001682 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301683 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001684 }
1685
1686 raid_device->volume_type = vol_pg0->VolumeType;
1687
1688 /* figure out what the underlying devices are by
1689 * obtaining the device_info bits for the 1st device
1690 */
1691 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1692 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1693 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1694 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1695 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1696 le16_to_cpu(pd_pg0.DevHandle)))) {
1697 raid_device->device_info =
1698 le32_to_cpu(sas_device_pg0.DeviceInfo);
1699 }
1700 }
1701
1702 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301703 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001704}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301705/**
1706 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1707 * @ioc: per adapter object
1708 */
1709static void
1710_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1711{
1712 Mpi2RaidVolPage1_t vol_pg1;
1713 Mpi2ConfigReply_t mpi_reply;
1714 struct _raid_device *raid_device;
1715 u16 handle;
1716 u16 ioc_status;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301717 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301718
1719 handle = 0xFFFF;
1720 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1721 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1722 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1723 MPI2_IOCSTATUS_MASK;
1724 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1725 break;
1726 handle = le16_to_cpu(vol_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301727 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301728 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1729 if (raid_device)
1730 raid_device->direct_io_enabled = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301731 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301732 }
1733 return;
1734}
1735
1736
1737/**
1738 * _scsih_get_num_volumes - Get number of volumes in the ioc
1739 * @ioc: per adapter object
1740 */
1741static u8
1742_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1743{
1744 Mpi2RaidVolPage1_t vol_pg1;
1745 Mpi2ConfigReply_t mpi_reply;
1746 u16 handle;
1747 u8 vol_cnt = 0;
1748 u16 ioc_status;
1749
1750 handle = 0xFFFF;
1751 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1752 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1753 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1754 MPI2_IOCSTATUS_MASK;
1755 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1756 break;
1757 vol_cnt++;
1758 handle = le16_to_cpu(vol_pg1.DevHandle);
1759 }
1760 return vol_cnt;
1761}
1762
1763
1764/**
1765 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1766 * @ioc: per adapter object
1767 * @raid_device: the raid_device object
1768 */
1769static void
1770_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1771 struct _raid_device *raid_device)
1772{
1773 Mpi2RaidVolPage0_t *vol_pg0;
1774 Mpi2RaidPhysDiskPage0_t pd_pg0;
1775 Mpi2ConfigReply_t mpi_reply;
1776 u16 sz;
1777 u8 num_pds, count;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301778 unsigned long stripe_sz, block_sz;
1779 u8 stripe_exp, block_exp;
1780 u64 dev_max_lba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301781
1782 if (!ioc->is_warpdrive)
1783 return;
1784
1785 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1786 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1787 "globally as drives are exposed\n", ioc->name);
1788 return;
1789 }
1790 if (_scsih_get_num_volumes(ioc) > 1) {
1791 _scsih_disable_ddio(ioc);
1792 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1793 "globally as number of drives > 1\n", ioc->name);
1794 return;
1795 }
1796 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1797 &num_pds)) || !num_pds) {
1798 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1799 "Failure in computing number of drives\n", ioc->name);
1800 return;
1801 }
1802
1803 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1804 sizeof(Mpi2RaidVol0PhysDisk_t));
1805 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1806 if (!vol_pg0) {
1807 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1808 "Memory allocation failure for RVPG0\n", ioc->name);
1809 return;
1810 }
1811
1812 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1813 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1814 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1815 "Failure in retrieving RVPG0\n", ioc->name);
1816 kfree(vol_pg0);
1817 return;
1818 }
1819
1820 /*
1821 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1822 * assumed for WARPDRIVE, disable direct I/O
1823 */
1824 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1825 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1826 "for the drive with handle(0x%04x): num_mem=%d, "
1827 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1828 num_pds, MPT_MAX_WARPDRIVE_PDS);
1829 kfree(vol_pg0);
1830 return;
1831 }
1832 for (count = 0; count < num_pds; count++) {
1833 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1834 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1835 vol_pg0->PhysDisk[count].PhysDiskNum) ||
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05301836 le16_to_cpu(pd_pg0.DevHandle) ==
1837 MPT2SAS_INVALID_DEVICE_HANDLE) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301838 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1839 "disabled for the drive with handle(0x%04x) member"
1840 "handle retrieval failed for member number=%d\n",
1841 ioc->name, raid_device->handle,
1842 vol_pg0->PhysDisk[count].PhysDiskNum);
1843 goto out_error;
1844 }
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301845 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1846 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1847 if (dev_max_lba >> 32) {
1848 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1849 "disabled for the drive with handle(0x%04x) member"
1850 "handle (0x%04x) unsupported max lba 0x%016llx\n",
1851 ioc->name, raid_device->handle,
1852 le16_to_cpu(pd_pg0.DevHandle),
1853 (unsigned long long)dev_max_lba);
1854 goto out_error;
1855 }
1856
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301857 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1858 }
1859
1860 /*
1861 * Assumption for WD: Direct I/O is not supported if the volume is
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301862 * not RAID0
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301863 */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301864 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301865 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1866 "for the drive with handle(0x%04x): type=%d, "
1867 "s_sz=%uK, blk_size=%u\n", ioc->name,
1868 raid_device->handle, raid_device->volume_type,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301869 (le32_to_cpu(vol_pg0->StripeSize) *
1870 le16_to_cpu(vol_pg0->BlockSize)) / 1024,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301871 le16_to_cpu(vol_pg0->BlockSize));
1872 goto out_error;
1873 }
1874
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301875 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301876 stripe_exp = find_first_bit(&stripe_sz, 32);
1877 if (stripe_exp == 32) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301878 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301879 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301880 ioc->name, raid_device->handle,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301881 (le32_to_cpu(vol_pg0->StripeSize) *
1882 le16_to_cpu(vol_pg0->BlockSize)) / 1024);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301883 goto out_error;
1884 }
1885 raid_device->stripe_exponent = stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301886 block_sz = le16_to_cpu(vol_pg0->BlockSize);
1887 block_exp = find_first_bit(&block_sz, 16);
1888 if (block_exp == 16) {
1889 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1890 "for the drive with handle(0x%04x) invalid block sz %u\n",
1891 ioc->name, raid_device->handle,
1892 le16_to_cpu(vol_pg0->BlockSize));
1893 goto out_error;
1894 }
1895 raid_device->block_exponent = block_exp;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301896 raid_device->direct_io_enabled = 1;
1897
1898 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1899 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1900 /*
1901 * WARPDRIVE: Though the following fields are not used for direct IO,
1902 * stored for future purpose:
1903 */
1904 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1905 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1906 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1907
1908
1909 kfree(vol_pg0);
1910 return;
1911
1912out_error:
1913 raid_device->direct_io_enabled = 0;
1914 for (count = 0; count < num_pds; count++)
1915 raid_device->pd_handle[count] = 0;
1916 kfree(vol_pg0);
1917 return;
1918}
Eric Moore635374e2009-03-09 01:21:12 -06001919
1920/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301921 * _scsih_enable_tlr - setting TLR flags
1922 * @ioc: per adapter object
1923 * @sdev: scsi device struct
1924 *
1925 * Enabling Transaction Layer Retries for tape devices when
1926 * vpd page 0x90 is present
1927 *
1928 */
1929static void
1930_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1931{
1932 /* only for TAPE */
1933 if (sdev->type != TYPE_TAPE)
1934 return;
1935
1936 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1937 return;
1938
1939 sas_enable_tlr(sdev);
1940 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1941 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1942 return;
1943
1944}
1945
1946/**
Eric Moored5d135b2009-05-18 13:02:08 -06001947 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001948 * @sdev: scsi device struct
1949 *
1950 * Returns 0 if ok. Any other return is assumed to be an error and
1951 * the device is ignored.
1952 */
1953static int
Eric Moored5d135b2009-05-18 13:02:08 -06001954_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001955{
1956 struct Scsi_Host *shost = sdev->host;
1957 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1958 struct MPT2SAS_DEVICE *sas_device_priv_data;
1959 struct MPT2SAS_TARGET *sas_target_priv_data;
1960 struct _sas_device *sas_device;
1961 struct _raid_device *raid_device;
1962 unsigned long flags;
1963 int qdepth;
1964 u8 ssp_target = 0;
1965 char *ds = "";
1966 char *r_level = "";
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301967 u16 handle, volume_handle = 0;
1968 u64 volume_wwid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001969
1970 qdepth = 1;
1971 sas_device_priv_data = sdev->hostdata;
1972 sas_device_priv_data->configured_lun = 1;
1973 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1974 sas_target_priv_data = sas_device_priv_data->sas_target;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301975 handle = sas_target_priv_data->handle;
Eric Moore635374e2009-03-09 01:21:12 -06001976
1977 /* raid volume handling */
1978 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1979
1980 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301981 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06001982 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1983 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301984 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1985 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1986 __LINE__, __func__));
1987 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001988 }
1989
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301990 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1991 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1992 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1993 __LINE__, __func__));
1994 return 1;
1995 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301996 /*
1997 * WARPDRIVE: Initialize the required data for Direct IO
1998 */
1999 _scsih_init_warpdrive_properties(ioc, raid_device);
2000
Eric Moore635374e2009-03-09 01:21:12 -06002001 /* RAID Queue Depth Support
2002 * IS volume = underlying qdepth of drive type, either
2003 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2004 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2005 */
2006 if (raid_device->device_info &
2007 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2008 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2009 ds = "SSP";
2010 } else {
2011 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2012 if (raid_device->device_info &
2013 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2014 ds = "SATA";
2015 else
2016 ds = "STP";
2017 }
2018
2019 switch (raid_device->volume_type) {
2020 case MPI2_RAID_VOL_TYPE_RAID0:
2021 r_level = "RAID0";
2022 break;
2023 case MPI2_RAID_VOL_TYPE_RAID1E:
2024 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302025 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302026 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302027 MFG10_GF0_R10_DISPLAY) &&
2028 !(raid_device->num_pds % 2))
2029 r_level = "RAID10";
2030 else
2031 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002032 break;
2033 case MPI2_RAID_VOL_TYPE_RAID1:
2034 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2035 r_level = "RAID1";
2036 break;
2037 case MPI2_RAID_VOL_TYPE_RAID10:
2038 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2039 r_level = "RAID10";
2040 break;
2041 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2042 default:
2043 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2044 r_level = "RAIDX";
2045 break;
2046 }
2047
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302048 if (!ioc->hide_ir_msg)
2049 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2050 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2051 r_level, raid_device->handle,
2052 (unsigned long long)raid_device->wwid,
2053 raid_device->num_pds, ds);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002054 _scsih_change_queue_depth(sdev, qdepth);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302055 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302056 if (!ioc->is_warpdrive)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302057 _scsih_set_level(sdev, raid_device->volume_type);
Eric Moore635374e2009-03-09 01:21:12 -06002058 return 0;
2059 }
2060
2061 /* non-raid handling */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302062 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2063 if (mpt2sas_config_get_volume_handle(ioc, handle,
2064 &volume_handle)) {
2065 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2066 "failure at %s:%d/%s()!\n", ioc->name,
2067 __FILE__, __LINE__, __func__));
2068 return 1;
2069 }
2070 if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
2071 volume_handle, &volume_wwid)) {
2072 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2073 "failure at %s:%d/%s()!\n", ioc->name,
2074 __FILE__, __LINE__, __func__));
2075 return 1;
2076 }
2077 }
2078
Eric Moore635374e2009-03-09 01:21:12 -06002079 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2080 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2081 sas_device_priv_data->sas_target->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302082 if (!sas_device) {
2083 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302084 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302085 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2086 __LINE__, __func__));
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302087 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002088 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302089 sas_device->volume_handle = volume_handle;
2090 sas_device->volume_wwid = volume_wwid;
2091 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2092 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2093 ssp_target = 1;
2094 ds = "SSP";
2095 } else {
2096 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2097 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2098 ds = "STP";
2099 else if (sas_device->device_info &
2100 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2101 ds = "SATA";
2102 }
2103 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2104 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2105 ds, sas_device->handle,
2106 (unsigned long long)sas_device->sas_address,
2107 sas_device->phy,
2108 (unsigned long long)sas_device->device_name);
2109 sdev_printk(KERN_INFO, sdev, "%s: "
2110 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2111 (unsigned long long) sas_device->enclosure_logical_id,
2112 sas_device->slot);
2113
2114 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2115 if (!ssp_target)
2116 _scsih_display_sata_capabilities(ioc, handle, sdev);
2117
Eric Moore635374e2009-03-09 01:21:12 -06002118
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002119 _scsih_change_queue_depth(sdev, qdepth);
Eric Moore635374e2009-03-09 01:21:12 -06002120
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302121 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002122 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302123 _scsih_enable_tlr(ioc, sdev);
2124 }
Eric Moore635374e2009-03-09 01:21:12 -06002125 return 0;
2126}
2127
2128/**
Eric Moored5d135b2009-05-18 13:02:08 -06002129 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002130 * @sdev: scsi device struct
2131 * @bdev: pointer to block device context
2132 * @capacity: device size (in 512 byte sectors)
2133 * @params: three element array to place output:
2134 * params[0] number of heads (max 255)
2135 * params[1] number of sectors (max 63)
2136 * params[2] number of cylinders
2137 *
2138 * Return nothing.
2139 */
2140static int
Eric Moored5d135b2009-05-18 13:02:08 -06002141_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002142 sector_t capacity, int params[])
2143{
2144 int heads;
2145 int sectors;
2146 sector_t cylinders;
2147 ulong dummy;
2148
2149 heads = 64;
2150 sectors = 32;
2151
2152 dummy = heads * sectors;
2153 cylinders = capacity;
2154 sector_div(cylinders, dummy);
2155
2156 /*
2157 * Handle extended translation size for logical drives
2158 * > 1Gb
2159 */
2160 if ((ulong)capacity >= 0x200000) {
2161 heads = 255;
2162 sectors = 63;
2163 dummy = heads * sectors;
2164 cylinders = capacity;
2165 sector_div(cylinders, dummy);
2166 }
2167
2168 /* return result */
2169 params[0] = heads;
2170 params[1] = sectors;
2171 params[2] = cylinders;
2172
2173 return 0;
2174}
2175
2176/**
2177 * _scsih_response_code - translation of device response code
2178 * @ioc: per adapter object
2179 * @response_code: response code returned by the device
2180 *
2181 * Return nothing.
2182 */
2183static void
2184_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2185{
2186 char *desc;
2187
2188 switch (response_code) {
2189 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2190 desc = "task management request completed";
2191 break;
2192 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2193 desc = "invalid frame";
2194 break;
2195 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2196 desc = "task management request not supported";
2197 break;
2198 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2199 desc = "task management request failed";
2200 break;
2201 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2202 desc = "task management request succeeded";
2203 break;
2204 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2205 desc = "invalid lun";
2206 break;
2207 case 0xA:
2208 desc = "overlapped tag attempted";
2209 break;
2210 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2211 desc = "task queued, however not sent to target";
2212 break;
2213 default:
2214 desc = "unknown";
2215 break;
2216 }
2217 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2218 ioc->name, response_code, desc);
2219}
2220
2221/**
Eric Moored5d135b2009-05-18 13:02:08 -06002222 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002223 * @ioc: per adapter object
2224 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302225 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002226 * @reply: reply message frame(lower 32bit addr)
2227 * Context: none.
2228 *
2229 * The callback handler when using scsih_issue_tm.
2230 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302231 * Return 1 meaning mf should be freed from _base_interrupt
2232 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002233 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302234static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302235_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002236{
2237 MPI2DefaultReply_t *mpi_reply;
2238
2239 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302240 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002241 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302242 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302243 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002244 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2245 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2246 if (mpi_reply) {
2247 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2248 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2249 }
2250 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2251 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302252 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002253}
2254
2255/**
2256 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2257 * @ioc: per adapter object
2258 * @handle: device handle
2259 *
2260 * During taskmangement request, we need to freeze the device queue.
2261 */
2262void
2263mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2264{
2265 struct MPT2SAS_DEVICE *sas_device_priv_data;
2266 struct scsi_device *sdev;
2267 u8 skip = 0;
2268
2269 shost_for_each_device(sdev, ioc->shost) {
2270 if (skip)
2271 continue;
2272 sas_device_priv_data = sdev->hostdata;
2273 if (!sas_device_priv_data)
2274 continue;
2275 if (sas_device_priv_data->sas_target->handle == handle) {
2276 sas_device_priv_data->sas_target->tm_busy = 1;
2277 skip = 1;
2278 ioc->ignore_loginfos = 1;
2279 }
2280 }
2281}
2282
2283/**
2284 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2285 * @ioc: per adapter object
2286 * @handle: device handle
2287 *
2288 * During taskmangement request, we need to freeze the device queue.
2289 */
2290void
2291mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2292{
2293 struct MPT2SAS_DEVICE *sas_device_priv_data;
2294 struct scsi_device *sdev;
2295 u8 skip = 0;
2296
2297 shost_for_each_device(sdev, ioc->shost) {
2298 if (skip)
2299 continue;
2300 sas_device_priv_data = sdev->hostdata;
2301 if (!sas_device_priv_data)
2302 continue;
2303 if (sas_device_priv_data->sas_target->handle == handle) {
2304 sas_device_priv_data->sas_target->tm_busy = 0;
2305 skip = 1;
2306 ioc->ignore_loginfos = 0;
2307 }
2308 }
2309}
2310
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302311
Eric Moore635374e2009-03-09 01:21:12 -06002312/**
2313 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2314 * @ioc: per adapter struct
2315 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302316 * @channel: the channel assigned by the OS
2317 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002318 * @lun: lun number
2319 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2320 * @smid_task: smid assigned to the task
2321 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302322 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302323 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002324 *
2325 * A generic API for sending task management requests to firmware.
2326 *
Eric Moore635374e2009-03-09 01:21:12 -06002327 * The callback index is set inside `ioc->tm_cb_idx`.
2328 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302329 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002330 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302331int
2332mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2333 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002334 enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002335{
2336 Mpi2SCSITaskManagementRequest_t *mpi_request;
2337 Mpi2SCSITaskManagementReply_t *mpi_reply;
2338 u16 smid = 0;
2339 u32 ioc_state;
2340 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302341 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302342 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002343
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302344 if (m_type == TM_MUTEX_ON)
2345 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302346 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2347 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2348 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302349 rc = FAILED;
2350 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302351 }
2352
Eric Moore3cb54692010-07-08 14:44:34 -06002353 if (ioc->shost_recovery || ioc->remove_host ||
2354 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002355 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2356 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302357 rc = FAILED;
2358 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002359 }
Eric Moore635374e2009-03-09 01:21:12 -06002360
2361 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2362 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302363 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002364 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302365 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302366 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302367 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302368 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002369 }
2370
2371 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2372 mpt2sas_base_fault_info(ioc, ioc_state &
2373 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302374 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302375 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302376 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302377 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002378 }
2379
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302380 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002381 if (!smid) {
2382 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2383 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302384 rc = FAILED;
2385 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002386 }
2387
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302388 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2389 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2390
Eric Moore635374e2009-03-09 01:21:12 -06002391 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302392 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2393 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002394 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2395 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2396 ioc->tm_cmds.smid = smid;
2397 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302398 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002399 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2400 mpi_request->DevHandle = cpu_to_le16(handle);
2401 mpi_request->TaskType = type;
2402 mpi_request->TaskMID = cpu_to_le16(smid_task);
2403 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2404 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302405 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302406 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002407 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002408 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2409 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2410 ioc->name, __func__);
2411 _debug_dump_mf(mpi_request,
2412 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302413 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302414 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302415 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302416 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302417 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2418 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2419 goto err_out;
2420 }
Eric Moore635374e2009-03-09 01:21:12 -06002421 }
2422
2423 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2424 mpi_reply = ioc->tm_cmds.reply;
2425 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2426 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2427 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2428 le32_to_cpu(mpi_reply->IOCLogInfo),
2429 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302430 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002431 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302432 if (mpi_reply->IOCStatus)
2433 _debug_dump_mf(mpi_request,
2434 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2435 }
Eric Moore635374e2009-03-09 01:21:12 -06002436 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302437
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302438 switch (type) {
2439 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302440 rc = SUCCESS;
2441 if (scsi_lookup->scmd == NULL)
2442 break;
2443 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302444 break;
2445
2446 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2447 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2448 rc = FAILED;
2449 else
2450 rc = SUCCESS;
2451 break;
2452
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302453 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302454 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2455 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2456 rc = FAILED;
2457 else
2458 rc = SUCCESS;
2459 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302460 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2461 rc = SUCCESS;
2462 break;
2463 default:
2464 rc = FAILED;
2465 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302466 }
2467
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302468 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2469 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302470 if (m_type == TM_MUTEX_ON)
2471 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302472
2473 return rc;
2474
2475 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302476 if (m_type == TM_MUTEX_ON)
2477 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302478 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002479}
2480
2481/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302482 * _scsih_tm_display_info - displays info about the device
2483 * @ioc: per adapter struct
2484 * @scmd: pointer to scsi command object
2485 *
2486 * Called by task management callback handlers.
2487 */
2488static void
2489_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2490{
2491 struct scsi_target *starget = scmd->device->sdev_target;
2492 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2493 struct _sas_device *sas_device = NULL;
2494 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302495 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302496
2497 if (!priv_target)
2498 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302499 if (ioc->hide_ir_msg)
2500 device_str = "WarpDrive";
2501 else
2502 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302503
2504 scsi_print_command(scmd);
2505 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302506 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2507 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2508 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302509 } else {
2510 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2511 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2512 priv_target->sas_address);
2513 if (sas_device) {
2514 if (priv_target->flags &
2515 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2516 starget_printk(KERN_INFO, starget,
2517 "volume handle(0x%04x), "
2518 "volume wwid(0x%016llx)\n",
2519 sas_device->volume_handle,
2520 (unsigned long long)sas_device->volume_wwid);
2521 }
2522 starget_printk(KERN_INFO, starget,
2523 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2524 sas_device->handle,
2525 (unsigned long long)sas_device->sas_address,
2526 sas_device->phy);
2527 starget_printk(KERN_INFO, starget,
2528 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2529 (unsigned long long)sas_device->enclosure_logical_id,
2530 sas_device->slot);
2531 }
2532 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2533 }
2534}
2535
2536/**
Eric Moored5d135b2009-05-18 13:02:08 -06002537 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302538 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002539 *
2540 * Returns SUCCESS if command aborted else FAILED
2541 */
2542static int
Eric Moored5d135b2009-05-18 13:02:08 -06002543_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002544{
2545 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2546 struct MPT2SAS_DEVICE *sas_device_priv_data;
2547 u16 smid;
2548 u16 handle;
2549 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002550
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302551 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2552 "scmd(%p)\n", scmd);
2553 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002554
2555 sas_device_priv_data = scmd->device->hostdata;
2556 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302557 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2558 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002559 scmd->result = DID_NO_CONNECT << 16;
2560 scmd->scsi_done(scmd);
2561 r = SUCCESS;
2562 goto out;
2563 }
2564
2565 /* search for the command */
2566 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2567 if (!smid) {
2568 scmd->result = DID_RESET << 16;
2569 r = SUCCESS;
2570 goto out;
2571 }
2572
2573 /* for hidden raid components and volumes this is not supported */
2574 if (sas_device_priv_data->sas_target->flags &
2575 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2576 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2577 scmd->result = DID_RESET << 16;
2578 r = FAILED;
2579 goto out;
2580 }
2581
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302582 mpt2sas_halt_firmware(ioc);
2583
Eric Moore635374e2009-03-09 01:21:12 -06002584 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302585 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2586 scmd->device->id, scmd->device->lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002587 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002588
2589 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302590 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2591 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002592 return r;
2593}
2594
Eric Moore635374e2009-03-09 01:21:12 -06002595/**
Eric Moored5d135b2009-05-18 13:02:08 -06002596 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302597 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002598 *
2599 * Returns SUCCESS if command aborted else FAILED
2600 */
2601static int
Eric Moored5d135b2009-05-18 13:02:08 -06002602_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002603{
2604 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2605 struct MPT2SAS_DEVICE *sas_device_priv_data;
2606 struct _sas_device *sas_device;
2607 unsigned long flags;
2608 u16 handle;
2609 int r;
2610
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302611 struct scsi_target *starget = scmd->device->sdev_target;
2612
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302613 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302614 "scmd(%p)\n", scmd);
2615 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002616
2617 sas_device_priv_data = scmd->device->hostdata;
2618 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302619 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302620 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002621 scmd->result = DID_NO_CONNECT << 16;
2622 scmd->scsi_done(scmd);
2623 r = SUCCESS;
2624 goto out;
2625 }
2626
2627 /* for hidden raid components obtain the volume_handle */
2628 handle = 0;
2629 if (sas_device_priv_data->sas_target->flags &
2630 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2631 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2632 sas_device = _scsih_sas_device_find_by_handle(ioc,
2633 sas_device_priv_data->sas_target->handle);
2634 if (sas_device)
2635 handle = sas_device->volume_handle;
2636 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2637 } else
2638 handle = sas_device_priv_data->sas_target->handle;
2639
2640 if (!handle) {
2641 scmd->result = DID_RESET << 16;
2642 r = FAILED;
2643 goto out;
2644 }
2645
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302646 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2647 scmd->device->id, scmd->device->lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002648 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002649
2650 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302651 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2652 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002653 return r;
2654}
2655
2656/**
Eric Moored5d135b2009-05-18 13:02:08 -06002657 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302658 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002659 *
2660 * Returns SUCCESS if command aborted else FAILED
2661 */
2662static int
Eric Moored5d135b2009-05-18 13:02:08 -06002663_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002664{
2665 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2666 struct MPT2SAS_DEVICE *sas_device_priv_data;
2667 struct _sas_device *sas_device;
2668 unsigned long flags;
2669 u16 handle;
2670 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302671 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002672
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302673 starget_printk(KERN_INFO, starget, "attempting target reset! "
2674 "scmd(%p)\n", scmd);
2675 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002676
2677 sas_device_priv_data = scmd->device->hostdata;
2678 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302679 starget_printk(KERN_INFO, starget, "target been deleted! "
2680 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002681 scmd->result = DID_NO_CONNECT << 16;
2682 scmd->scsi_done(scmd);
2683 r = SUCCESS;
2684 goto out;
2685 }
2686
2687 /* for hidden raid components obtain the volume_handle */
2688 handle = 0;
2689 if (sas_device_priv_data->sas_target->flags &
2690 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2691 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2692 sas_device = _scsih_sas_device_find_by_handle(ioc,
2693 sas_device_priv_data->sas_target->handle);
2694 if (sas_device)
2695 handle = sas_device->volume_handle;
2696 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2697 } else
2698 handle = sas_device_priv_data->sas_target->handle;
2699
2700 if (!handle) {
2701 scmd->result = DID_RESET << 16;
2702 r = FAILED;
2703 goto out;
2704 }
2705
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302706 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2707 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002708 30, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002709
2710 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302711 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2712 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002713 return r;
2714}
2715
2716/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302717 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302718 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002719 *
2720 * Returns SUCCESS if command aborted else FAILED
2721 */
2722static int
Eric Moored5d135b2009-05-18 13:02:08 -06002723_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002724{
2725 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2726 int r, retval;
2727
2728 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2729 ioc->name, scmd);
2730 scsi_print_command(scmd);
2731
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302732 if (ioc->is_driver_loading) {
2733 printk(MPT2SAS_INFO_FMT "Blocking the host reset\n",
2734 ioc->name);
2735 r = FAILED;
2736 goto out;
2737 }
2738
Eric Moore635374e2009-03-09 01:21:12 -06002739 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2740 FORCE_BIG_HAMMER);
2741 r = (retval < 0) ? FAILED : SUCCESS;
Sreekanth Reddyddb588b2015-01-12 11:38:57 +05302742
2743 out:
Eric Moore635374e2009-03-09 01:21:12 -06002744 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2745 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2746
2747 return r;
2748}
2749
2750/**
2751 * _scsih_fw_event_add - insert and queue up fw_event
2752 * @ioc: per adapter object
2753 * @fw_event: object describing the event
2754 * Context: This function will acquire ioc->fw_event_lock.
2755 *
2756 * This adds the firmware event object into link list, then queues it up to
2757 * be processed from user context.
2758 *
2759 * Return nothing.
2760 */
2761static void
2762_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2763{
2764 unsigned long flags;
2765
2766 if (ioc->firmware_event_thread == NULL)
2767 return;
2768
2769 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2770 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302771 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2772 queue_delayed_work(ioc->firmware_event_thread,
2773 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002774 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2775}
2776
2777/**
2778 * _scsih_fw_event_free - delete fw_event
2779 * @ioc: per adapter object
2780 * @fw_event: object describing the event
2781 * Context: This function will acquire ioc->fw_event_lock.
2782 *
2783 * This removes firmware event object from link list, frees associated memory.
2784 *
2785 * Return nothing.
2786 */
2787static void
2788_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2789 *fw_event)
2790{
2791 unsigned long flags;
2792
2793 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2794 list_del(&fw_event->list);
Eric Moore635374e2009-03-09 01:21:12 -06002795 kfree(fw_event);
2796 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2797}
2798
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302799
Eric Moore635374e2009-03-09 01:21:12 -06002800/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302801 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002802 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002803 *
2804 * Return nothing.
2805 */
2806static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302807_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002808{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302809 struct fw_event_work *fw_event;
2810
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302811 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302812 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002813
2814 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2815 if (!fw_event)
2816 return;
2817
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302818 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2819 fw_event->ioc = ioc;
2820 _scsih_fw_event_add(ioc, fw_event);
2821}
2822
2823/**
2824 * mpt2sas_port_enable_complete - port enable completed (fake event)
2825 * @ioc: per adapter object
2826 *
2827 * Return nothing.
2828 */
2829void
2830mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2831{
2832 struct fw_event_work *fw_event;
2833
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302834 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2835 if (!fw_event)
2836 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302837 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302838 fw_event->ioc = ioc;
2839 _scsih_fw_event_add(ioc, fw_event);
2840}
2841
2842/**
2843 * _scsih_fw_event_cleanup_queue - cleanup event queue
2844 * @ioc: per adapter object
2845 *
2846 * Walk the firmware event queue, either killing timers, or waiting
2847 * for outstanding events to complete
2848 *
2849 * Return nothing.
2850 */
2851static void
2852_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2853{
2854 struct fw_event_work *fw_event, *next;
2855
2856 if (list_empty(&ioc->fw_event_list) ||
2857 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002858 return;
2859
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302860 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05302861 if (cancel_delayed_work_sync(&fw_event->delayed_work)) {
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302862 _scsih_fw_event_free(ioc, fw_event);
2863 continue;
2864 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302865 }
Eric Moore635374e2009-03-09 01:21:12 -06002866}
2867
Eric Moore635374e2009-03-09 01:21:12 -06002868/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302869 * _scsih_ublock_io_all_device - unblock every device
2870 * @ioc: per adapter object
2871 *
2872 * change the device state from block to running
2873 */
2874static void
2875_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2876{
2877 struct MPT2SAS_DEVICE *sas_device_priv_data;
2878 struct scsi_device *sdev;
2879
2880 shost_for_each_device(sdev, ioc->shost) {
2881 sas_device_priv_data = sdev->hostdata;
2882 if (!sas_device_priv_data)
2883 continue;
2884 if (!sas_device_priv_data->block)
2885 continue;
2886 sas_device_priv_data->block = 0;
2887 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2888 "handle(0x%04x)\n",
2889 sas_device_priv_data->sas_target->handle));
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002890 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302891 }
2892}
2893/**
Eric Moore635374e2009-03-09 01:21:12 -06002894 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2895 * @ioc: per adapter object
2896 * @handle: device handle
2897 *
2898 * During device pull we need to appropiately set the sdev state.
2899 */
2900static void
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302901_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06002902{
2903 struct MPT2SAS_DEVICE *sas_device_priv_data;
2904 struct scsi_device *sdev;
2905
2906 shost_for_each_device(sdev, ioc->shost) {
2907 sas_device_priv_data = sdev->hostdata;
2908 if (!sas_device_priv_data)
2909 continue;
2910 if (!sas_device_priv_data->block)
2911 continue;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302912 if (sas_device_priv_data->sas_target->sas_address ==
2913 sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06002914 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2915 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302916 "sas address(0x%016llx)\n", ioc->name,
2917 (unsigned long long)sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06002918 sas_device_priv_data->block = 0;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002919 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Eric Moore635374e2009-03-09 01:21:12 -06002920 }
2921 }
2922}
2923
2924/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302925 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2926 * @ioc: per adapter object
2927 * @handle: device handle
2928 *
2929 * During device pull we need to appropiately set the sdev state.
2930 */
2931static void
2932_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2933{
2934 struct MPT2SAS_DEVICE *sas_device_priv_data;
2935 struct scsi_device *sdev;
2936
2937 shost_for_each_device(sdev, ioc->shost) {
2938 sas_device_priv_data = sdev->hostdata;
2939 if (!sas_device_priv_data)
2940 continue;
2941 if (sas_device_priv_data->block)
2942 continue;
2943 sas_device_priv_data->block = 1;
2944 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2945 "handle(0x%04x)\n",
2946 sas_device_priv_data->sas_target->handle));
2947 scsi_internal_device_block(sdev);
2948 }
2949}
2950
2951
2952/**
Eric Moore635374e2009-03-09 01:21:12 -06002953 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2954 * @ioc: per adapter object
2955 * @handle: device handle
2956 *
2957 * During device pull we need to appropiately set the sdev state.
2958 */
2959static void
2960_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2961{
2962 struct MPT2SAS_DEVICE *sas_device_priv_data;
2963 struct scsi_device *sdev;
2964
2965 shost_for_each_device(sdev, ioc->shost) {
2966 sas_device_priv_data = sdev->hostdata;
2967 if (!sas_device_priv_data)
2968 continue;
2969 if (sas_device_priv_data->block)
2970 continue;
2971 if (sas_device_priv_data->sas_target->handle == handle) {
2972 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2973 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2974 "handle(0x%04x)\n", ioc->name, handle));
2975 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302976 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002977 }
2978 }
2979}
2980
2981/**
2982 * _scsih_block_io_to_children_attached_to_ex
2983 * @ioc: per adapter object
2984 * @sas_expander: the sas_device object
2985 *
2986 * This routine set sdev state to SDEV_BLOCK for all devices
2987 * attached to this expander. This function called when expander is
2988 * pulled.
2989 */
2990static void
2991_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2992 struct _sas_node *sas_expander)
2993{
2994 struct _sas_port *mpt2sas_port;
2995 struct _sas_device *sas_device;
2996 struct _sas_node *expander_sibling;
2997 unsigned long flags;
2998
2999 if (!sas_expander)
3000 return;
3001
3002 list_for_each_entry(mpt2sas_port,
3003 &sas_expander->sas_port_list, port_list) {
3004 if (mpt2sas_port->remote_identify.device_type ==
3005 SAS_END_DEVICE) {
3006 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3007 sas_device =
3008 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3009 mpt2sas_port->remote_identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303010 if (sas_device)
3011 set_bit(sas_device->handle,
3012 ioc->blocking_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003013 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06003014 }
3015 }
3016
3017 list_for_each_entry(mpt2sas_port,
3018 &sas_expander->sas_port_list, port_list) {
3019
3020 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303021 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003022 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303023 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003024 expander_sibling =
3025 mpt2sas_scsih_expander_find_by_sas_address(
3026 ioc, mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003027 _scsih_block_io_to_children_attached_to_ex(ioc,
3028 expander_sibling);
3029 }
3030 }
3031}
3032
3033/**
3034 * _scsih_block_io_to_children_attached_directly
3035 * @ioc: per adapter object
3036 * @event_data: topology change event data
3037 *
3038 * This routine set sdev state to SDEV_BLOCK for all devices
3039 * direct attached during device pull.
3040 */
3041static void
3042_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3043 Mpi2EventDataSasTopologyChangeList_t *event_data)
3044{
3045 int i;
3046 u16 handle;
3047 u16 reason_code;
3048 u8 phy_number;
3049
3050 for (i = 0; i < event_data->NumEntries; i++) {
3051 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3052 if (!handle)
3053 continue;
3054 phy_number = event_data->StartPhyNum + i;
3055 reason_code = event_data->PHY[i].PhyStatus &
3056 MPI2_EVENT_SAS_TOPO_RC_MASK;
3057 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3058 _scsih_block_io_device(ioc, handle);
3059 }
3060}
3061
3062/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303063 * _scsih_tm_tr_send - send task management request
3064 * @ioc: per adapter object
3065 * @handle: device handle
3066 * Context: interrupt time.
3067 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003068 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303069 * with controller firmware. This function will issue target reset
3070 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003071 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303072 *
3073 * This is designed to send muliple task management request at the same
3074 * time to the fifo. If the fifo is full, we will append the request,
3075 * and process it in a future completion.
3076 */
3077static void
3078_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3079{
3080 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303081 u16 smid;
3082 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303083 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3084 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303085 unsigned long flags;
3086 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303087 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303088
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303089 if (ioc->remove_host) {
3090 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3091 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3092 return;
3093 } else if (ioc->pci_error_recovery) {
3094 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3095 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3096 handle));
3097 return;
3098 }
3099 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3100 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3101 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3102 "operational: handle(0x%04x)\n", __func__, ioc->name,
3103 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303104 return;
3105 }
3106
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303107 /* if PD, then return */
3108 if (test_bit(handle, ioc->pd_handles))
3109 return;
3110
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303111 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3112 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303113 if (sas_device && sas_device->starget &&
3114 sas_device->starget->hostdata) {
3115 sas_target_priv_data = sas_device->starget->hostdata;
3116 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303117 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303118 }
3119 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303120
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303121 if (sas_target_priv_data) {
3122 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3123 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3124 (unsigned long long)sas_address));
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05303125 _scsih_ublock_io_device(ioc, sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303126 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303127 }
3128
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303129 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3130 if (!smid) {
3131 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3132 if (!delayed_tr)
3133 return;
3134 INIT_LIST_HEAD(&delayed_tr->list);
3135 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303136 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3137 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3138 "DELAYED:tr:handle(0x%04x), (open)\n",
3139 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303140 return;
3141 }
3142
Kashyap, Desai1278b112010-03-09 17:34:13 +05303143 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3144 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3145 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303146 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3147 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3148 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3149 mpi_request->DevHandle = cpu_to_le16(handle);
3150 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303151 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3152}
3153
3154
3155
3156/**
3157 * _scsih_sas_control_complete - completion routine
3158 * @ioc: per adapter object
3159 * @smid: system request message index
3160 * @msix_index: MSIX table index supplied by the OS
3161 * @reply: reply message frame(lower 32bit addr)
3162 * Context: interrupt time.
3163 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003164 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303165 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003166 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303167 *
3168 * Return 1 meaning mf should be freed from _base_interrupt
3169 * 0 means the mf is freed from this function.
3170 */
3171static u8
3172_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3173 u8 msix_index, u32 reply)
3174{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303175 Mpi2SasIoUnitControlReply_t *mpi_reply =
3176 mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303177 if (likely(mpi_reply)) {
3178 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3179 "sc_complete:handle(0x%04x), (open) "
3180 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3181 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3182 le16_to_cpu(mpi_reply->IOCStatus),
3183 le32_to_cpu(mpi_reply->IOCLogInfo)));
3184 } else {
3185 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3186 ioc->name, __FILE__, __LINE__, __func__);
3187 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303188 return 1;
3189}
3190
3191/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303192 * _scsih_tm_tr_volume_send - send target reset request for volumes
3193 * @ioc: per adapter object
3194 * @handle: device handle
3195 * Context: interrupt time.
3196 *
3197 * This is designed to send muliple task management request at the same
3198 * time to the fifo. If the fifo is full, we will append the request,
3199 * and process it in a future completion.
3200 */
3201static void
3202_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3203{
3204 Mpi2SCSITaskManagementRequest_t *mpi_request;
3205 u16 smid;
3206 struct _tr_list *delayed_tr;
3207
Eric Moore3cb54692010-07-08 14:44:34 -06003208 if (ioc->shost_recovery || ioc->remove_host ||
3209 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303210 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3211 "progress!\n", __func__, ioc->name));
3212 return;
3213 }
3214
3215 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3216 if (!smid) {
3217 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3218 if (!delayed_tr)
3219 return;
3220 INIT_LIST_HEAD(&delayed_tr->list);
3221 delayed_tr->handle = handle;
3222 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3223 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3224 "DELAYED:tr:handle(0x%04x), (open)\n",
3225 ioc->name, handle));
3226 return;
3227 }
3228
3229 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3230 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3231 ioc->tm_tr_volume_cb_idx));
3232 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3233 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3234 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3235 mpi_request->DevHandle = cpu_to_le16(handle);
3236 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3237 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3238}
3239
3240/**
3241 * _scsih_tm_volume_tr_complete - target reset completion
3242 * @ioc: per adapter object
3243 * @smid: system request message index
3244 * @msix_index: MSIX table index supplied by the OS
3245 * @reply: reply message frame(lower 32bit addr)
3246 * Context: interrupt time.
3247 *
3248 * Return 1 meaning mf should be freed from _base_interrupt
3249 * 0 means the mf is freed from this function.
3250 */
3251static u8
3252_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3253 u8 msix_index, u32 reply)
3254{
3255 u16 handle;
3256 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3257 Mpi2SCSITaskManagementReply_t *mpi_reply =
3258 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3259
Eric Moore3cb54692010-07-08 14:44:34 -06003260 if (ioc->shost_recovery || ioc->remove_host ||
3261 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303262 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3263 "progress!\n", __func__, ioc->name));
3264 return 1;
3265 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303266 if (unlikely(!mpi_reply)) {
3267 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3268 ioc->name, __FILE__, __LINE__, __func__);
3269 return 1;
3270 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303271 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3272 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3273 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3274 dewtprintk(ioc, printk("spurious interrupt: "
3275 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3276 le16_to_cpu(mpi_reply->DevHandle), smid));
3277 return 0;
3278 }
3279
3280 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3281 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3282 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3283 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3284 le32_to_cpu(mpi_reply->IOCLogInfo),
3285 le32_to_cpu(mpi_reply->TerminationCount)));
3286
3287 return _scsih_check_for_pending_tm(ioc, smid);
3288}
3289
3290/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303291 * _scsih_tm_tr_complete -
3292 * @ioc: per adapter object
3293 * @smid: system request message index
3294 * @msix_index: MSIX table index supplied by the OS
3295 * @reply: reply message frame(lower 32bit addr)
3296 * Context: interrupt time.
3297 *
3298 * This is the target reset completion routine.
3299 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003300 * handshake protocol with controller firmware.
3301 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303302 *
3303 * Return 1 meaning mf should be freed from _base_interrupt
3304 * 0 means the mf is freed from this function.
3305 */
3306static u8
3307_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3308 u32 reply)
3309{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303310 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303311 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303312 Mpi2SCSITaskManagementReply_t *mpi_reply =
3313 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3314 Mpi2SasIoUnitControlRequest_t *mpi_request;
3315 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303316 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303317
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303318 if (ioc->remove_host) {
3319 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3320 "removed\n", __func__, ioc->name));
3321 return 1;
3322 } else if (ioc->pci_error_recovery) {
3323 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3324 "error recovery\n", __func__, ioc->name));
3325 return 1;
3326 }
3327 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3328 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3329 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3330 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303331 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303332 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303333 if (unlikely(!mpi_reply)) {
3334 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3335 ioc->name, __FILE__, __LINE__, __func__);
3336 return 1;
3337 }
Kashyap, Desai1278b112010-03-09 17:34:13 +05303338 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3339 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3340 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3341 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3342 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3343 le16_to_cpu(mpi_reply->DevHandle), smid));
3344 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303345 }
3346
Kashyap, Desai1278b112010-03-09 17:34:13 +05303347 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3348 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3349 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3350 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3351 le32_to_cpu(mpi_reply->IOCLogInfo),
3352 le32_to_cpu(mpi_reply->TerminationCount)));
3353
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303354 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3355 if (!smid_sas_ctrl) {
3356 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3357 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303358 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303359 }
3360
Kashyap, Desai1278b112010-03-09 17:34:13 +05303361 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3362 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3363 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303364 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3365 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3366 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3367 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303368 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303369 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303370
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303371 return _scsih_check_for_pending_tm(ioc, smid);
3372}
3373
3374/**
3375 * _scsih_check_for_pending_tm - check for pending task management
3376 * @ioc: per adapter object
3377 * @smid: system request message index
3378 *
3379 * This will check delayed target reset list, and feed the
3380 * next reqeust.
3381 *
3382 * Return 1 meaning mf should be freed from _base_interrupt
3383 * 0 means the mf is freed from this function.
3384 */
3385static u8
3386_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3387{
3388 struct _tr_list *delayed_tr;
3389
3390 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3391 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3392 struct _tr_list, list);
3393 mpt2sas_base_free_smid(ioc, smid);
3394 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3395 list_del(&delayed_tr->list);
3396 kfree(delayed_tr);
3397 return 0;
3398 }
3399
Kashyap, Desai1278b112010-03-09 17:34:13 +05303400 if (!list_empty(&ioc->delayed_tr_list)) {
3401 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3402 struct _tr_list, list);
3403 mpt2sas_base_free_smid(ioc, smid);
3404 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3405 list_del(&delayed_tr->list);
3406 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303407 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303408 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303409
Kashyap, Desai1278b112010-03-09 17:34:13 +05303410 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303411}
3412
3413/**
Eric Moore635374e2009-03-09 01:21:12 -06003414 * _scsih_check_topo_delete_events - sanity check on topo events
3415 * @ioc: per adapter object
3416 * @event_data: the event data payload
3417 *
3418 * This routine added to better handle cable breaker.
3419 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003420 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003421 * add and delete events in a single shot. When there is a delete event
3422 * the routine will void any pending add events waiting in the event queue.
3423 *
3424 * Return nothing.
3425 */
3426static void
3427_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3428 Mpi2EventDataSasTopologyChangeList_t *event_data)
3429{
3430 struct fw_event_work *fw_event;
3431 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3432 u16 expander_handle;
3433 struct _sas_node *sas_expander;
3434 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303435 int i, reason_code;
3436 u16 handle;
3437
3438 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303439 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3440 if (!handle)
3441 continue;
3442 reason_code = event_data->PHY[i].PhyStatus &
3443 MPI2_EVENT_SAS_TOPO_RC_MASK;
3444 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3445 _scsih_tm_tr_send(ioc, handle);
3446 }
Eric Moore635374e2009-03-09 01:21:12 -06003447
3448 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3449 if (expander_handle < ioc->sas_hba.num_phys) {
3450 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3451 return;
3452 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303453 if (event_data->ExpStatus ==
3454 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3455 /* put expander attached devices into blocking state */
Eric Moore635374e2009-03-09 01:21:12 -06003456 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3457 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3458 expander_handle);
Eric Moore635374e2009-03-09 01:21:12 -06003459 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303460 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3461 do {
3462 handle = find_first_bit(ioc->blocking_handles,
3463 ioc->facts.MaxDevHandle);
3464 if (handle < ioc->facts.MaxDevHandle)
3465 _scsih_block_io_device(ioc, handle);
3466 } while (test_and_clear_bit(handle, ioc->blocking_handles));
Eric Moore635374e2009-03-09 01:21:12 -06003467 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3468 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3469
3470 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3471 return;
3472
3473 /* mark ignore flag for pending events */
3474 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3475 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3476 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3477 fw_event->ignore)
3478 continue;
Joe Lawrence00713ad2014-06-25 17:03:33 -04003479 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3480 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06003481 if (local_event_data->ExpStatus ==
3482 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3483 local_event_data->ExpStatus ==
3484 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3485 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3486 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303487 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003488 "setting ignoring flag\n", ioc->name));
3489 fw_event->ignore = 1;
3490 }
3491 }
3492 }
3493 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3494}
3495
3496/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303497 * _scsih_set_volume_delete_flag - setting volume delete flag
3498 * @ioc: per adapter object
3499 * @handle: device handle
3500 *
3501 * This
3502 * Return nothing.
3503 */
3504static void
3505_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3506{
3507 struct _raid_device *raid_device;
3508 struct MPT2SAS_TARGET *sas_target_priv_data;
3509 unsigned long flags;
3510
3511 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3512 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3513 if (raid_device && raid_device->starget &&
3514 raid_device->starget->hostdata) {
3515 sas_target_priv_data =
3516 raid_device->starget->hostdata;
3517 sas_target_priv_data->deleted = 1;
3518 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3519 "setting delete flag: handle(0x%04x), "
3520 "wwid(0x%016llx)\n", ioc->name, handle,
3521 (unsigned long long) raid_device->wwid));
3522 }
3523 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3524}
3525
3526/**
3527 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3528 * @handle: input handle
3529 * @a: handle for volume a
3530 * @b: handle for volume b
3531 *
3532 * IR firmware only supports two raid volumes. The purpose of this
3533 * routine is to set the volume handle in either a or b. When the given
3534 * input handle is non-zero, or when a and b have not been set before.
3535 */
3536static void
3537_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3538{
3539 if (!handle || handle == *a || handle == *b)
3540 return;
3541 if (!*a)
3542 *a = handle;
3543 else if (!*b)
3544 *b = handle;
3545}
3546
3547/**
3548 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3549 * @ioc: per adapter object
3550 * @event_data: the event data payload
3551 * Context: interrupt time.
3552 *
3553 * This routine will send target reset to volume, followed by target
3554 * resets to the PDs. This is called when a PD has been removed, or
3555 * volume has been deleted or removed. When the target reset is sent
3556 * to volume, the PD target resets need to be queued to start upon
3557 * completion of the volume target reset.
3558 *
3559 * Return nothing.
3560 */
3561static void
3562_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3563 Mpi2EventDataIrConfigChangeList_t *event_data)
3564{
3565 Mpi2EventIrConfigElement_t *element;
3566 int i;
3567 u16 handle, volume_handle, a, b;
3568 struct _tr_list *delayed_tr;
3569
3570 a = 0;
3571 b = 0;
3572
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303573 if (ioc->is_warpdrive)
3574 return;
3575
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303576 /* Volume Resets for Deleted or Removed */
3577 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3578 for (i = 0; i < event_data->NumElements; i++, element++) {
3579 if (element->ReasonCode ==
3580 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3581 element->ReasonCode ==
3582 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3583 volume_handle = le16_to_cpu(element->VolDevHandle);
3584 _scsih_set_volume_delete_flag(ioc, volume_handle);
3585 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3586 }
3587 }
3588
3589 /* Volume Resets for UNHIDE events */
3590 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3591 for (i = 0; i < event_data->NumElements; i++, element++) {
3592 if (le32_to_cpu(event_data->Flags) &
3593 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3594 continue;
3595 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3596 volume_handle = le16_to_cpu(element->VolDevHandle);
3597 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3598 }
3599 }
3600
3601 if (a)
3602 _scsih_tm_tr_volume_send(ioc, a);
3603 if (b)
3604 _scsih_tm_tr_volume_send(ioc, b);
3605
3606 /* PD target resets */
3607 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3608 for (i = 0; i < event_data->NumElements; i++, element++) {
3609 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3610 continue;
3611 handle = le16_to_cpu(element->PhysDiskDevHandle);
3612 volume_handle = le16_to_cpu(element->VolDevHandle);
3613 clear_bit(handle, ioc->pd_handles);
3614 if (!volume_handle)
3615 _scsih_tm_tr_send(ioc, handle);
3616 else if (volume_handle == a || volume_handle == b) {
3617 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3618 BUG_ON(!delayed_tr);
3619 INIT_LIST_HEAD(&delayed_tr->list);
3620 delayed_tr->handle = handle;
3621 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3622 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3623 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3624 handle));
3625 } else
3626 _scsih_tm_tr_send(ioc, handle);
3627 }
3628}
3629
3630
3631/**
3632 * _scsih_check_volume_delete_events - set delete flag for volumes
3633 * @ioc: per adapter object
3634 * @event_data: the event data payload
3635 * Context: interrupt time.
3636 *
3637 * This will handle the case when the cable connected to entire volume is
3638 * pulled. We will take care of setting the deleted flag so normal IO will
3639 * not be sent.
3640 *
3641 * Return nothing.
3642 */
3643static void
3644_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3645 Mpi2EventDataIrVolume_t *event_data)
3646{
3647 u32 state;
3648
3649 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3650 return;
3651 state = le32_to_cpu(event_data->NewValue);
3652 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3653 MPI2_RAID_VOL_STATE_FAILED)
3654 _scsih_set_volume_delete_flag(ioc,
3655 le16_to_cpu(event_data->VolDevHandle));
3656}
3657
3658/**
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05303659 * _scsih_temp_threshold_events - display temperature threshold exceeded events
3660 * @ioc: per adapter object
3661 * @event_data: the temp threshold event data
3662 * Context: interrupt time.
3663 *
3664 * Return nothing.
3665 */
3666static void
3667_scsih_temp_threshold_events(struct MPT2SAS_ADAPTER *ioc,
3668 Mpi2EventDataTemperature_t *event_data)
3669{
3670 if (ioc->temp_sensors_count >= event_data->SensorNum) {
3671 printk(MPT2SAS_ERR_FMT "Temperature Threshold flags %s%s%s%s"
3672 " exceeded for Sensor: %d !!!\n", ioc->name,
3673 ((le16_to_cpu(event_data->Status) & 0x1) == 1) ? "0 " : " ",
3674 ((le16_to_cpu(event_data->Status) & 0x2) == 2) ? "1 " : " ",
3675 ((le16_to_cpu(event_data->Status) & 0x4) == 4) ? "2 " : " ",
3676 ((le16_to_cpu(event_data->Status) & 0x8) == 8) ? "3 " : " ",
3677 event_data->SensorNum);
3678 printk(MPT2SAS_ERR_FMT "Current Temp In Celsius: %d\n",
3679 ioc->name, event_data->CurrentTemperature);
3680 }
3681}
3682
3683/**
Eric Moore635374e2009-03-09 01:21:12 -06003684 * _scsih_flush_running_cmds - completing outstanding commands.
3685 * @ioc: per adapter object
3686 *
3687 * The flushing out of all pending scmd commands following host reset,
3688 * where all IO is dropped to the floor.
3689 *
3690 * Return nothing.
3691 */
3692static void
3693_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3694{
3695 struct scsi_cmnd *scmd;
3696 u16 smid;
3697 u16 count = 0;
3698
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303699 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303700 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003701 if (!scmd)
3702 continue;
3703 count++;
3704 mpt2sas_base_free_smid(ioc, smid);
3705 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003706 if (ioc->pci_error_recovery)
3707 scmd->result = DID_NO_CONNECT << 16;
3708 else
3709 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003710 scmd->scsi_done(scmd);
3711 }
3712 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3713 ioc->name, count));
3714}
3715
3716/**
Eric Moore3c621b32009-05-18 12:59:41 -06003717 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3718 * @scmd: pointer to scsi command object
3719 * @mpi_request: pointer to the SCSI_IO reqest message frame
3720 *
3721 * Supporting protection 1 and 3.
3722 *
3723 * Returns nothing
3724 */
3725static void
3726_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3727{
3728 u16 eedp_flags;
3729 unsigned char prot_op = scsi_get_prot_op(scmd);
3730 unsigned char prot_type = scsi_get_prot_type(scmd);
3731
Eric Moored334aa72010-04-22 10:47:40 -06003732 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003733 return;
3734
3735 if (prot_op == SCSI_PROT_READ_STRIP)
3736 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3737 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3738 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3739 else
3740 return;
3741
Eric Moore3c621b32009-05-18 12:59:41 -06003742 switch (prot_type) {
3743 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003744 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003745
3746 /*
3747 * enable ref/guard checking
3748 * auto increment ref tag
3749 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303750 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003751 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3752 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3753 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3754 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003755 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003756
Eric Moore3c621b32009-05-18 12:59:41 -06003757 case SCSI_PROT_DIF_TYPE3:
3758
3759 /*
3760 * enable guard checking
3761 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303762 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003763 break;
3764 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303765 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3766 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003767}
3768
3769/**
3770 * _scsih_eedp_error_handling - return sense code for EEDP errors
3771 * @scmd: pointer to scsi command object
3772 * @ioc_status: ioc status
3773 *
3774 * Returns nothing
3775 */
3776static void
3777_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3778{
3779 u8 ascq;
Eric Moore3c621b32009-05-18 12:59:41 -06003780
3781 switch (ioc_status) {
3782 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3783 ascq = 0x01;
3784 break;
3785 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3786 ascq = 0x02;
3787 break;
3788 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3789 ascq = 0x03;
3790 break;
3791 default:
3792 ascq = 0x00;
3793 break;
3794 }
3795
Martin K. Petersen9b5cd132012-08-28 14:29:36 -04003796 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10, ascq);
3797 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
Eric Moore3c621b32009-05-18 12:59:41 -06003798 SAM_STAT_CHECK_CONDITION;
3799}
3800
3801/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303802 * _scsih_scsi_direct_io_get - returns direct io flag
3803 * @ioc: per adapter object
3804 * @smid: system request message index
3805 *
3806 * Returns the smid stored scmd pointer.
3807 */
3808static inline u8
3809_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3810{
3811 return ioc->scsi_lookup[smid - 1].direct_io;
3812}
3813
3814/**
3815 * _scsih_scsi_direct_io_set - sets direct io flag
3816 * @ioc: per adapter object
3817 * @smid: system request message index
3818 * @direct_io: Zero or non-zero value to set in the direct_io flag
3819 *
3820 * Returns Nothing.
3821 */
3822static inline void
3823_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3824{
3825 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3826}
3827
3828
3829/**
3830 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3831 * @ioc: per adapter object
3832 * @scmd: pointer to scsi command object
3833 * @raid_device: pointer to raid device data structure
3834 * @mpi_request: pointer to the SCSI_IO reqest message frame
3835 * @smid: system request message index
3836 *
3837 * Returns nothing
3838 */
3839static void
3840_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3841 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3842 u16 smid)
3843{
Sreekanth Reddyf78f6942014-09-19 12:17:27 +05303844 sector_t v_lba, p_lba, stripe_off, column, io_size;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303845 u32 stripe_sz, stripe_exp;
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303846 u8 num_pds, cmd = scmd->cmnd[0];
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303847
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303848 if (cmd != READ_10 && cmd != WRITE_10 &&
3849 cmd != READ_16 && cmd != WRITE_16)
3850 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303851
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303852 if (cmd == READ_10 || cmd == WRITE_10)
3853 v_lba = get_unaligned_be32(&mpi_request->CDB.CDB32[2]);
3854 else
3855 v_lba = get_unaligned_be64(&mpi_request->CDB.CDB32[2]);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303856
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303857 io_size = scsi_bufflen(scmd) >> raid_device->block_exponent;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303858
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303859 if (v_lba + io_size - 1 > raid_device->max_lba)
3860 return;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303861
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303862 stripe_sz = raid_device->stripe_sz;
3863 stripe_exp = raid_device->stripe_exponent;
3864 stripe_off = v_lba & (stripe_sz - 1);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303865
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303866 /* Return unless IO falls within a stripe */
3867 if (stripe_off + io_size > stripe_sz)
3868 return;
3869
3870 num_pds = raid_device->num_pds;
3871 p_lba = v_lba >> stripe_exp;
Sreekanth Reddyf78f6942014-09-19 12:17:27 +05303872 column = sector_div(p_lba, num_pds);
3873 p_lba = (p_lba << stripe_exp) + stripe_off;
3874
Sreekanth Reddydaeaa9d2014-09-12 15:35:21 +05303875 mpi_request->DevHandle = cpu_to_le16(raid_device->pd_handle[column]);
3876
3877 if (cmd == READ_10 || cmd == WRITE_10)
3878 put_unaligned_be32(lower_32_bits(p_lba),
3879 &mpi_request->CDB.CDB32[2]);
3880 else
3881 put_unaligned_be64(p_lba, &mpi_request->CDB.CDB32[2]);
3882
3883 _scsih_scsi_direct_io_set(ioc, smid, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303884}
3885
3886/**
Eric Moored5d135b2009-05-18 13:02:08 -06003887 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003888 * @scmd: pointer to scsi command object
3889 * @done: function pointer to be invoked on completion
3890 *
3891 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3892 *
3893 * Returns 0 on success. If there's a failure, return either:
3894 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3895 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3896 */
3897static int
Matthew Wilcox90f951e2014-03-27 16:40:33 -04003898_scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06003899{
Matthew Wilcox90f951e2014-03-27 16:40:33 -04003900 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Eric Moore635374e2009-03-09 01:21:12 -06003901 struct MPT2SAS_DEVICE *sas_device_priv_data;
3902 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303903 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003904 Mpi2SCSIIORequest_t *mpi_request;
3905 u32 mpi_control;
3906 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003907
Eric Moore635374e2009-03-09 01:21:12 -06003908 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303909 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003910 scmd->result = DID_NO_CONNECT << 16;
3911 scmd->scsi_done(scmd);
3912 return 0;
3913 }
3914
Kashyap, Desai78215782011-06-14 10:57:08 +05303915 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003916 scmd->result = DID_NO_CONNECT << 16;
3917 scmd->scsi_done(scmd);
3918 return 0;
3919 }
3920
Eric Moore635374e2009-03-09 01:21:12 -06003921 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303922 /* invalid device handle */
3923 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003924 scmd->result = DID_NO_CONNECT << 16;
3925 scmd->scsi_done(scmd);
3926 return 0;
3927 }
3928
Kashyap, Desai130b9582010-04-08 17:54:32 +05303929 /* host recovery or link resets sent via IOCTLs */
3930 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003931 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003932 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303933 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3934 return SCSI_MLQUEUE_DEVICE_BUSY;
3935 /* device has been deleted */
3936 else if (sas_target_priv_data->deleted) {
3937 scmd->result = DID_NO_CONNECT << 16;
3938 scmd->scsi_done(scmd);
3939 return 0;
3940 }
Eric Moore635374e2009-03-09 01:21:12 -06003941
3942 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3943 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3944 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3945 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3946 else
3947 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3948
3949 /* set tags */
Christoph Hellwig609aa222014-10-30 11:54:58 +01003950 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3951
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303952 /* Make sure Device is not raid volume.
3953 * We do not expose raid functionality to upper layer for warpdrive.
3954 */
3955 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003956 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003957 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3958
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303959 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003960 if (!smid) {
3961 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3962 ioc->name, __func__);
3963 goto out;
3964 }
3965 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3966 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003967 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003968 if (scmd->cmd_len == 32)
3969 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003970 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3971 if (sas_device_priv_data->sas_target->flags &
3972 MPT_TARGET_FLAGS_RAID_COMPONENT)
3973 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3974 else
Matthew Wilcox90f951e2014-03-27 16:40:33 -04003975 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Eric Moore635374e2009-03-09 01:21:12 -06003976 mpi_request->DevHandle =
3977 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3978 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3979 mpi_request->Control = cpu_to_le32(mpi_control);
3980 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3981 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3982 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3983 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303984 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003985 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3986 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3987 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303988 mpi_request->VF_ID = 0; /* TODO */
3989 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003990 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3991 mpi_request->LUN);
3992 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3993
3994 if (!mpi_request->DataLength) {
3995 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3996 } else {
3997 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3998 mpt2sas_base_free_smid(ioc, smid);
3999 goto out;
4000 }
4001 }
4002
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304003 raid_device = sas_target_priv_data->raid_device;
4004 if (raid_device && raid_device->direct_io_enabled)
4005 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4006 smid);
4007
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304008 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4009 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304010 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304011 else
4012 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004013 return 0;
4014
4015 out:
4016 return SCSI_MLQUEUE_HOST_BUSY;
4017}
4018
4019/**
4020 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4021 * @sense_buffer: sense data returned by target
4022 * @data: normalized skey/asc/ascq
4023 *
4024 * Return nothing.
4025 */
4026static void
4027_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4028{
4029 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4030 /* descriptor format */
4031 data->skey = sense_buffer[1] & 0x0F;
4032 data->asc = sense_buffer[2];
4033 data->ascq = sense_buffer[3];
4034 } else {
4035 /* fixed format */
4036 data->skey = sense_buffer[2] & 0x0F;
4037 data->asc = sense_buffer[12];
4038 data->ascq = sense_buffer[13];
4039 }
4040}
4041
4042#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4043/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004044 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004045 * @ioc: per adapter object
4046 * @scmd: pointer to scsi command object
4047 * @mpi_reply: reply mf payload returned from firmware
4048 *
4049 * scsi_status - SCSI Status code returned from target device
4050 * scsi_state - state info associated with SCSI_IO determined by ioc
4051 * ioc_status - ioc supplied status info
4052 *
4053 * Return nothing.
4054 */
4055static void
4056_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4057 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4058{
4059 u32 response_info;
4060 u8 *response_bytes;
4061 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4062 MPI2_IOCSTATUS_MASK;
4063 u8 scsi_state = mpi_reply->SCSIState;
4064 u8 scsi_status = mpi_reply->SCSIStatus;
4065 char *desc_ioc_state = NULL;
4066 char *desc_scsi_status = NULL;
4067 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304068 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304069 struct _sas_device *sas_device = NULL;
4070 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304071 struct scsi_target *starget = scmd->device->sdev_target;
4072 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304073 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304074
4075 if (!priv_target)
4076 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304077
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304078 if (ioc->hide_ir_msg)
4079 device_str = "WarpDrive";
4080 else
4081 device_str = "volume";
4082
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304083 if (log_info == 0x31170000)
4084 return;
Eric Moore635374e2009-03-09 01:21:12 -06004085
4086 switch (ioc_status) {
4087 case MPI2_IOCSTATUS_SUCCESS:
4088 desc_ioc_state = "success";
4089 break;
4090 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4091 desc_ioc_state = "invalid function";
4092 break;
4093 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4094 desc_ioc_state = "scsi recovered error";
4095 break;
4096 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4097 desc_ioc_state = "scsi invalid dev handle";
4098 break;
4099 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4100 desc_ioc_state = "scsi device not there";
4101 break;
4102 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4103 desc_ioc_state = "scsi data overrun";
4104 break;
4105 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4106 desc_ioc_state = "scsi data underrun";
4107 break;
4108 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4109 desc_ioc_state = "scsi io data error";
4110 break;
4111 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4112 desc_ioc_state = "scsi protocol error";
4113 break;
4114 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4115 desc_ioc_state = "scsi task terminated";
4116 break;
4117 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4118 desc_ioc_state = "scsi residual mismatch";
4119 break;
4120 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4121 desc_ioc_state = "scsi task mgmt failed";
4122 break;
4123 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4124 desc_ioc_state = "scsi ioc terminated";
4125 break;
4126 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4127 desc_ioc_state = "scsi ext terminated";
4128 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004129 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4130 desc_ioc_state = "eedp guard error";
4131 break;
4132 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4133 desc_ioc_state = "eedp ref tag error";
4134 break;
4135 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4136 desc_ioc_state = "eedp app tag error";
4137 break;
Eric Moore635374e2009-03-09 01:21:12 -06004138 default:
4139 desc_ioc_state = "unknown";
4140 break;
4141 }
4142
4143 switch (scsi_status) {
4144 case MPI2_SCSI_STATUS_GOOD:
4145 desc_scsi_status = "good";
4146 break;
4147 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4148 desc_scsi_status = "check condition";
4149 break;
4150 case MPI2_SCSI_STATUS_CONDITION_MET:
4151 desc_scsi_status = "condition met";
4152 break;
4153 case MPI2_SCSI_STATUS_BUSY:
4154 desc_scsi_status = "busy";
4155 break;
4156 case MPI2_SCSI_STATUS_INTERMEDIATE:
4157 desc_scsi_status = "intermediate";
4158 break;
4159 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4160 desc_scsi_status = "intermediate condmet";
4161 break;
4162 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4163 desc_scsi_status = "reservation conflict";
4164 break;
4165 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4166 desc_scsi_status = "command terminated";
4167 break;
4168 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4169 desc_scsi_status = "task set full";
4170 break;
4171 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4172 desc_scsi_status = "aca active";
4173 break;
4174 case MPI2_SCSI_STATUS_TASK_ABORTED:
4175 desc_scsi_status = "task aborted";
4176 break;
4177 default:
4178 desc_scsi_status = "unknown";
4179 break;
4180 }
4181
4182 desc_scsi_state[0] = '\0';
4183 if (!scsi_state)
4184 desc_scsi_state = " ";
4185 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4186 strcat(desc_scsi_state, "response info ");
4187 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4188 strcat(desc_scsi_state, "state terminated ");
4189 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4190 strcat(desc_scsi_state, "no status ");
4191 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4192 strcat(desc_scsi_state, "autosense failed ");
4193 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4194 strcat(desc_scsi_state, "autosense valid ");
4195
4196 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304197
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304198 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304199 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4200 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304201 } else {
4202 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4203 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4204 priv_target->sas_address);
4205 if (sas_device) {
4206 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4207 "phy(%d)\n", ioc->name, sas_device->sas_address,
4208 sas_device->phy);
4209 printk(MPT2SAS_WARN_FMT
4210 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4211 ioc->name, sas_device->enclosure_logical_id,
4212 sas_device->slot);
4213 }
4214 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304215 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304216
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304217 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4218 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4219 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004220 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4221 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4222 scsi_get_resid(scmd));
4223 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4224 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4225 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4226 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4227 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4228 scsi_status, desc_scsi_state, scsi_state);
4229
4230 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4231 struct sense_info data;
4232 _scsih_normalize_sense(scmd->sense_buffer, &data);
4233 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304234 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4235 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004236 }
4237
4238 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4239 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4240 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304241 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004242 }
4243}
4244#endif
4245
4246/**
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304247 * _scsih_turn_on_pfa_led - illuminate PFA LED
Eric Moore635374e2009-03-09 01:21:12 -06004248 * @ioc: per adapter object
4249 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304250 * Context: process
4251 *
4252 * Return nothing.
4253 */
4254static void
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304255_scsih_turn_on_pfa_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304256{
4257 Mpi2SepReply_t mpi_reply;
4258 Mpi2SepRequest_t mpi_request;
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304259 struct _sas_device *sas_device;
4260
4261 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4262 if (!sas_device)
4263 return;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304264
4265 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4266 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4267 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4268 mpi_request.SlotStatus =
4269 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4270 mpi_request.DevHandle = cpu_to_le16(handle);
4271 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4272 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4273 &mpi_request)) != 0) {
4274 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4275 __FILE__, __LINE__, __func__);
4276 return;
4277 }
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304278 sas_device->pfa_led_on = 1;
4279
4280
4281 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4282 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
4283 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4284 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4285 le32_to_cpu(mpi_reply.IOCLogInfo)));
4286 return;
4287 }
4288}
4289
4290/**
4291 * _scsih_turn_off_pfa_led - turn off PFA LED
4292 * @ioc: per adapter object
4293 * @sas_device: sas device whose PFA LED has to turned off
4294 * Context: process
4295 *
4296 * Return nothing.
4297 */
4298static void
4299_scsih_turn_off_pfa_led(struct MPT2SAS_ADAPTER *ioc,
4300 struct _sas_device *sas_device)
4301{
4302 Mpi2SepReply_t mpi_reply;
4303 Mpi2SepRequest_t mpi_request;
4304
4305 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4306 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4307 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4308 mpi_request.SlotStatus = 0;
4309 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4310 mpi_request.DevHandle = 0;
4311 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4312 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4313 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4314 &mpi_request)) != 0) {
4315 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4316 __FILE__, __LINE__, __func__);
4317 return;
4318 }
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304319
4320 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4321 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4322 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4323 le16_to_cpu(mpi_reply.IOCStatus),
4324 le32_to_cpu(mpi_reply.IOCLogInfo)));
4325 return;
4326 }
4327}
4328
4329/**
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304330 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304331 * @ioc: per adapter object
4332 * @handle: device handle
4333 * Context: interrupt.
4334 *
4335 * Return nothing.
4336 */
4337static void
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304338_scsih_send_event_to_turn_on_pfa_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304339{
4340 struct fw_event_work *fw_event;
4341
4342 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4343 if (!fw_event)
4344 return;
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304345 fw_event->event = MPT2SAS_TURN_ON_PFA_LED;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304346 fw_event->device_handle = handle;
4347 fw_event->ioc = ioc;
4348 _scsih_fw_event_add(ioc, fw_event);
4349}
4350
4351/**
4352 * _scsih_smart_predicted_fault - process smart errors
4353 * @ioc: per adapter object
4354 * @handle: device handle
4355 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004356 *
4357 * Return nothing.
4358 */
4359static void
4360_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4361{
Eric Moore635374e2009-03-09 01:21:12 -06004362 struct scsi_target *starget;
4363 struct MPT2SAS_TARGET *sas_target_priv_data;
4364 Mpi2EventNotificationReply_t *event_reply;
4365 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4366 struct _sas_device *sas_device;
4367 ssize_t sz;
4368 unsigned long flags;
4369
4370 /* only handle non-raid devices */
4371 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4372 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4373 if (!sas_device) {
4374 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4375 return;
4376 }
4377 starget = sas_device->starget;
4378 sas_target_priv_data = starget->hostdata;
4379
4380 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4381 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4382 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4383 return;
4384 }
4385 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4386 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4387
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304388 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304389 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004390
4391 /* insert into event log */
4392 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4393 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b42011-11-07 22:05:21 +11004394 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004395 if (!event_reply) {
4396 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4397 ioc->name, __FILE__, __LINE__, __func__);
4398 return;
4399 }
4400
4401 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4402 event_reply->Event =
4403 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4404 event_reply->MsgLength = sz/4;
4405 event_reply->EventDataLength =
4406 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4407 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4408 event_reply->EventData;
4409 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4410 event_data->ASC = 0x5D;
4411 event_data->DevHandle = cpu_to_le16(handle);
4412 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4413 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4414 kfree(event_reply);
4415}
4416
4417/**
Eric Moored5d135b2009-05-18 13:02:08 -06004418 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004419 * @ioc: per adapter object
4420 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304421 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004422 * @reply: reply message frame(lower 32bit addr)
4423 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304424 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004425 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304426 * Return 1 meaning mf should be freed from _base_interrupt
4427 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004428 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304429static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304430_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004431{
4432 Mpi2SCSIIORequest_t *mpi_request;
4433 Mpi2SCSIIOReply_t *mpi_reply;
4434 struct scsi_cmnd *scmd;
4435 u16 ioc_status;
4436 u32 xfer_cnt;
4437 u8 scsi_state;
4438 u8 scsi_status;
4439 u32 log_info;
4440 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304441 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304442 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004443
4444 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304445 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004446 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304447 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004448
4449 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4450
4451 if (mpi_reply == NULL) {
4452 scmd->result = DID_OK << 16;
4453 goto out;
4454 }
4455
4456 sas_device_priv_data = scmd->device->hostdata;
4457 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4458 sas_device_priv_data->sas_target->deleted) {
4459 scmd->result = DID_NO_CONNECT << 16;
4460 goto out;
4461 }
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304462 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304463 /*
4464 * WARPDRIVE: If direct_io is set then it is directIO,
4465 * the failed direct I/O should be redirected to volume
4466 */
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304467 if (_scsih_scsi_direct_io_get(ioc, smid) &&
4468 ((ioc_status & MPI2_IOCSTATUS_MASK)
4469 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304470 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4471 ioc->scsi_lookup[smid - 1].scmd = scmd;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304472 _scsih_scsi_direct_io_set(ioc, smid, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304473 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304474 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4475 mpi_request->DevHandle =
4476 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4477 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4478 sas_device_priv_data->sas_target->handle);
4479 return 0;
4480 }
4481
Eric Moore635374e2009-03-09 01:21:12 -06004482
4483 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304484 scsi_state = mpi_reply->SCSIState;
4485 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4486 response_code =
4487 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004488 if (!sas_device_priv_data->tlr_snoop_check) {
4489 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304490 /* Make sure Device is not raid volume.
4491 * We do not expose raid functionality to upper layer for warpdrive.
4492 */
4493 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304494 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304495 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4496 sas_disable_tlr(scmd->device);
4497 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4498 }
Eric Moore635374e2009-03-09 01:21:12 -06004499 }
4500
4501 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4502 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
Eric Moore635374e2009-03-09 01:21:12 -06004503 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4504 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4505 else
4506 log_info = 0;
4507 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004508 scsi_status = mpi_reply->SCSIStatus;
4509
4510 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4511 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4512 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4513 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4514 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4515 }
4516
4517 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4518 struct sense_info data;
4519 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4520 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004521 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004522 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004523 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004524 _scsih_normalize_sense(scmd->sense_buffer, &data);
4525 /* failure prediction threshold exceeded */
4526 if (data.asc == 0x5D)
4527 _scsih_smart_predicted_fault(ioc,
4528 le16_to_cpu(mpi_reply->DevHandle));
4529 }
4530
4531 switch (ioc_status) {
4532 case MPI2_IOCSTATUS_BUSY:
4533 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4534 scmd->result = SAM_STAT_BUSY;
4535 break;
4536
4537 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4538 scmd->result = DID_NO_CONNECT << 16;
4539 break;
4540
4541 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4542 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304543 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4544 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004545 }
Sreekanth Reddyd2227f72015-01-12 11:39:01 +05304546 if (log_info == 0x32010081) {
4547 scmd->result = DID_RESET << 16;
4548 break;
4549 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304550 scmd->result = DID_SOFT_ERROR << 16;
4551 break;
Eric Moore635374e2009-03-09 01:21:12 -06004552 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4553 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4554 scmd->result = DID_RESET << 16;
4555 break;
4556
4557 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4558 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4559 scmd->result = DID_SOFT_ERROR << 16;
4560 else
4561 scmd->result = (DID_OK << 16) | scsi_status;
4562 break;
4563
4564 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4565 scmd->result = (DID_OK << 16) | scsi_status;
4566
4567 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4568 break;
4569
4570 if (xfer_cnt < scmd->underflow) {
4571 if (scsi_status == SAM_STAT_BUSY)
4572 scmd->result = SAM_STAT_BUSY;
4573 else
4574 scmd->result = DID_SOFT_ERROR << 16;
4575 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4576 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4577 scmd->result = DID_SOFT_ERROR << 16;
4578 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4579 scmd->result = DID_RESET << 16;
4580 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4581 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4582 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4583 scmd->result = (DRIVER_SENSE << 24) |
4584 SAM_STAT_CHECK_CONDITION;
4585 scmd->sense_buffer[0] = 0x70;
4586 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4587 scmd->sense_buffer[12] = 0x20;
4588 scmd->sense_buffer[13] = 0;
4589 }
4590 break;
4591
4592 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4593 scsi_set_resid(scmd, 0);
4594 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4595 case MPI2_IOCSTATUS_SUCCESS:
4596 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304597 if (response_code ==
4598 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4599 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4600 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004601 scmd->result = DID_SOFT_ERROR << 16;
4602 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4603 scmd->result = DID_RESET << 16;
4604 break;
4605
Eric Moore3c621b32009-05-18 12:59:41 -06004606 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4607 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4608 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4609 _scsih_eedp_error_handling(scmd, ioc_status);
4610 break;
Eric Moore635374e2009-03-09 01:21:12 -06004611 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4612 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4613 case MPI2_IOCSTATUS_INVALID_SGL:
4614 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4615 case MPI2_IOCSTATUS_INVALID_FIELD:
4616 case MPI2_IOCSTATUS_INVALID_STATE:
4617 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4618 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4619 default:
4620 scmd->result = DID_SOFT_ERROR << 16;
4621 break;
4622
4623 }
4624
4625#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4626 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4627 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4628#endif
4629
4630 out:
4631 scsi_dma_unmap(scmd);
4632 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304633 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004634}
4635
4636/**
Eric Moore635374e2009-03-09 01:21:12 -06004637 * _scsih_sas_host_refresh - refreshing sas host object contents
4638 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004639 * Context: user
4640 *
4641 * During port enable, fw will send topology events for every device. Its
4642 * possible that the handles may change from the previous setting, so this
4643 * code keeping handles updating if changed.
4644 *
4645 * Return nothing.
4646 */
4647static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304648_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004649{
4650 u16 sz;
4651 u16 ioc_status;
4652 int i;
4653 Mpi2ConfigReply_t mpi_reply;
4654 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304655 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304656 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004657
4658 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4659 "updating handles for sas_host(0x%016llx)\n",
4660 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4661
4662 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4663 * sizeof(Mpi2SasIOUnit0PhyData_t));
4664 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4665 if (!sas_iounit_pg0) {
4666 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4667 ioc->name, __FILE__, __LINE__, __func__);
4668 return;
4669 }
Eric Moore635374e2009-03-09 01:21:12 -06004670
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304671 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4672 sas_iounit_pg0, sz)) != 0)
4673 goto out;
4674 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4675 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4676 goto out;
4677 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304678 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304679 if (i == 0)
4680 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4681 PhyData[0].ControllerDevHandle);
4682 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4683 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4684 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304685 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4686 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304687 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304688 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304689 }
Eric Moore635374e2009-03-09 01:21:12 -06004690 out:
4691 kfree(sas_iounit_pg0);
4692}
4693
4694/**
4695 * _scsih_sas_host_add - create sas host object
4696 * @ioc: per adapter object
4697 *
4698 * Creating host side data object, stored in ioc->sas_hba
4699 *
4700 * Return nothing.
4701 */
4702static void
4703_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4704{
4705 int i;
4706 Mpi2ConfigReply_t mpi_reply;
4707 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4708 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4709 Mpi2SasPhyPage0_t phy_pg0;
4710 Mpi2SasDevicePage0_t sas_device_pg0;
4711 Mpi2SasEnclosurePage0_t enclosure_pg0;
4712 u16 ioc_status;
4713 u16 sz;
4714 u16 device_missing_delay;
4715
4716 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4717 if (!ioc->sas_hba.num_phys) {
4718 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4719 ioc->name, __FILE__, __LINE__, __func__);
4720 return;
4721 }
4722
4723 /* sas_iounit page 0 */
4724 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4725 sizeof(Mpi2SasIOUnit0PhyData_t));
4726 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4727 if (!sas_iounit_pg0) {
4728 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4729 ioc->name, __FILE__, __LINE__, __func__);
4730 return;
4731 }
4732 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4733 sas_iounit_pg0, sz))) {
4734 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4735 ioc->name, __FILE__, __LINE__, __func__);
4736 goto out;
4737 }
4738 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4739 MPI2_IOCSTATUS_MASK;
4740 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4741 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4742 ioc->name, __FILE__, __LINE__, __func__);
4743 goto out;
4744 }
4745
4746 /* sas_iounit page 1 */
4747 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4748 sizeof(Mpi2SasIOUnit1PhyData_t));
4749 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4750 if (!sas_iounit_pg1) {
4751 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4752 ioc->name, __FILE__, __LINE__, __func__);
4753 goto out;
4754 }
4755 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4756 sas_iounit_pg1, sz))) {
4757 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4758 ioc->name, __FILE__, __LINE__, __func__);
4759 goto out;
4760 }
4761 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4762 MPI2_IOCSTATUS_MASK;
4763 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4764 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4765 ioc->name, __FILE__, __LINE__, __func__);
4766 goto out;
4767 }
4768
4769 ioc->io_missing_delay =
4770 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4771 device_missing_delay =
4772 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4773 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4774 ioc->device_missing_delay = (device_missing_delay &
4775 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4776 else
4777 ioc->device_missing_delay = device_missing_delay &
4778 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4779
4780 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4781 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4782 sizeof(struct _sas_phy), GFP_KERNEL);
4783 if (!ioc->sas_hba.phy) {
4784 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4785 ioc->name, __FILE__, __LINE__, __func__);
4786 goto out;
4787 }
4788 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4789 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4790 i))) {
4791 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4792 ioc->name, __FILE__, __LINE__, __func__);
4793 goto out;
4794 }
4795 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4796 MPI2_IOCSTATUS_MASK;
4797 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4798 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4799 ioc->name, __FILE__, __LINE__, __func__);
4800 goto out;
4801 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304802
4803 if (i == 0)
4804 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4805 PhyData[0].ControllerDevHandle);
4806 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004807 ioc->sas_hba.phy[i].phy_id = i;
4808 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4809 phy_pg0, ioc->sas_hba.parent_dev);
4810 }
4811 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304812 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004813 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4814 ioc->name, __FILE__, __LINE__, __func__);
4815 goto out;
4816 }
Eric Moore635374e2009-03-09 01:21:12 -06004817 ioc->sas_hba.enclosure_handle =
4818 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4819 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4820 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4821 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4822 (unsigned long long) ioc->sas_hba.sas_address,
4823 ioc->sas_hba.num_phys) ;
4824
4825 if (ioc->sas_hba.enclosure_handle) {
4826 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4827 &enclosure_pg0,
4828 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4829 ioc->sas_hba.enclosure_handle))) {
4830 ioc->sas_hba.enclosure_logical_id =
4831 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4832 }
4833 }
4834
4835 out:
4836 kfree(sas_iounit_pg1);
4837 kfree(sas_iounit_pg0);
4838}
4839
4840/**
4841 * _scsih_expander_add - creating expander object
4842 * @ioc: per adapter object
4843 * @handle: expander handle
4844 *
4845 * Creating expander object, stored in ioc->sas_expander_list.
4846 *
4847 * Return 0 for success, else error.
4848 */
4849static int
4850_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4851{
4852 struct _sas_node *sas_expander;
4853 Mpi2ConfigReply_t mpi_reply;
4854 Mpi2ExpanderPage0_t expander_pg0;
4855 Mpi2ExpanderPage1_t expander_pg1;
4856 Mpi2SasEnclosurePage0_t enclosure_pg0;
4857 u32 ioc_status;
4858 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304859 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004860 int i;
4861 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304862 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004863 int rc = 0;
4864
4865 if (!handle)
4866 return -1;
4867
Eric Moore3cb54692010-07-08 14:44:34 -06004868 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304869 return -1;
4870
Eric Moore635374e2009-03-09 01:21:12 -06004871 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4872 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4873 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4874 ioc->name, __FILE__, __LINE__, __func__);
4875 return -1;
4876 }
4877
4878 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4879 MPI2_IOCSTATUS_MASK;
4880 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4881 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4882 ioc->name, __FILE__, __LINE__, __func__);
4883 return -1;
4884 }
4885
4886 /* handle out of order topology events */
4887 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304888 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4889 != 0) {
4890 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4891 ioc->name, __FILE__, __LINE__, __func__);
4892 return -1;
4893 }
4894 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004895 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304896 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4897 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004898 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4899 if (!sas_expander) {
4900 rc = _scsih_expander_add(ioc, parent_handle);
4901 if (rc != 0)
4902 return rc;
4903 }
4904 }
4905
Eric Moore635374e2009-03-09 01:21:12 -06004906 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304907 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004908 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4909 sas_address);
4910 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4911
4912 if (sas_expander)
4913 return 0;
4914
4915 sas_expander = kzalloc(sizeof(struct _sas_node),
4916 GFP_KERNEL);
4917 if (!sas_expander) {
4918 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4919 ioc->name, __FILE__, __LINE__, __func__);
4920 return -1;
4921 }
4922
4923 sas_expander->handle = handle;
4924 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304925 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004926 sas_expander->sas_address = sas_address;
4927
4928 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4929 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304930 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004931 sas_expander->sas_address, sas_expander->num_phys);
4932
4933 if (!sas_expander->num_phys)
4934 goto out_fail;
4935 sas_expander->phy = kcalloc(sas_expander->num_phys,
4936 sizeof(struct _sas_phy), GFP_KERNEL);
4937 if (!sas_expander->phy) {
4938 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4939 ioc->name, __FILE__, __LINE__, __func__);
4940 rc = -1;
4941 goto out_fail;
4942 }
4943
4944 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4945 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304946 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004947 if (!mpt2sas_port) {
4948 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4949 ioc->name, __FILE__, __LINE__, __func__);
4950 rc = -1;
4951 goto out_fail;
4952 }
4953 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4954
4955 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4956 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4957 &expander_pg1, i, handle))) {
4958 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4959 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304960 rc = -1;
4961 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004962 }
4963 sas_expander->phy[i].handle = handle;
4964 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304965
4966 if ((mpt2sas_transport_add_expander_phy(ioc,
4967 &sas_expander->phy[i], expander_pg1,
4968 sas_expander->parent_dev))) {
4969 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4970 ioc->name, __FILE__, __LINE__, __func__);
4971 rc = -1;
4972 goto out_fail;
4973 }
Eric Moore635374e2009-03-09 01:21:12 -06004974 }
4975
4976 if (sas_expander->enclosure_handle) {
4977 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4978 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4979 sas_expander->enclosure_handle))) {
4980 sas_expander->enclosure_logical_id =
4981 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4982 }
4983 }
4984
4985 _scsih_expander_node_add(ioc, sas_expander);
4986 return 0;
4987
4988 out_fail:
4989
Kashyap, Desai20f58952009-08-07 19:34:26 +05304990 if (mpt2sas_port)
4991 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304992 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004993 kfree(sas_expander);
4994 return rc;
4995}
4996
4997/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304998 * _scsih_done - scsih callback handler.
4999 * @ioc: per adapter object
5000 * @smid: system request message index
5001 * @msix_index: MSIX table index supplied by the OS
5002 * @reply: reply message frame(lower 32bit addr)
5003 *
5004 * Callback handler when sending internal generated message frames.
5005 * The callback index passed is `ioc->scsih_cb_idx`
5006 *
5007 * Return 1 meaning mf should be freed from _base_interrupt
5008 * 0 means the mf is freed from this function.
5009 */
5010static u8
5011_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5012{
5013 MPI2DefaultReply_t *mpi_reply;
5014
5015 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5016 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
5017 return 1;
5018 if (ioc->scsih_cmds.smid != smid)
5019 return 1;
5020 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
5021 if (mpi_reply) {
5022 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5023 mpi_reply->MsgLength*4);
5024 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
5025 }
5026 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
5027 complete(&ioc->scsih_cmds.done);
5028 return 1;
5029}
5030
5031/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305032 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06005033 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305034 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06005035 *
5036 * Return nothing.
5037 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305038void
5039mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06005040{
5041 struct _sas_node *sas_expander;
5042 unsigned long flags;
5043
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305044 if (ioc->shost_recovery)
5045 return;
5046
Eric Moore635374e2009-03-09 01:21:12 -06005047 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305048 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5049 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305050 if (sas_expander)
5051 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06005052 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305053 if (sas_expander)
5054 _scsih_expander_node_remove(ioc, sas_expander);
Eric Moore635374e2009-03-09 01:21:12 -06005055}
5056
5057/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05305058 * _scsih_check_access_status - check access flags
5059 * @ioc: per adapter object
5060 * @sas_address: sas address
5061 * @handle: sas device handle
5062 * @access_flags: errors returned during discovery of the device
5063 *
5064 * Return 0 for success, else failure
5065 */
5066static u8
5067_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5068 u16 handle, u8 access_status)
5069{
5070 u8 rc = 1;
5071 char *desc = NULL;
5072
5073 switch (access_status) {
5074 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5075 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5076 rc = 0;
5077 break;
5078 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5079 desc = "sata capability failed";
5080 break;
5081 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5082 desc = "sata affiliation conflict";
5083 break;
5084 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5085 desc = "route not addressable";
5086 break;
5087 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5088 desc = "smp error not addressable";
5089 break;
5090 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5091 desc = "device blocked";
5092 break;
5093 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5094 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5095 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5096 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5097 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5098 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5099 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5100 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5101 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5102 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5103 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5104 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5105 desc = "sata initialization failed";
5106 break;
5107 default:
5108 desc = "unknown";
5109 break;
5110 }
5111
5112 if (!rc)
5113 return 0;
5114
5115 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5116 "handle(0x%04x)\n", ioc->name, desc,
5117 (unsigned long long)sas_address, handle);
5118 return rc;
5119}
5120
5121static void
5122_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5123{
5124 Mpi2ConfigReply_t mpi_reply;
5125 Mpi2SasDevicePage0_t sas_device_pg0;
5126 struct _sas_device *sas_device;
5127 u32 ioc_status;
5128 unsigned long flags;
5129 u64 sas_address;
5130 struct scsi_target *starget;
5131 struct MPT2SAS_TARGET *sas_target_priv_data;
5132 u32 device_info;
5133
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305134
Kashyap, Desaib4344272010-03-17 16:24:14 +05305135 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5136 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5137 return;
5138
5139 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5140 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5141 return;
5142
5143 /* check if this is end device */
5144 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5145 if (!(_scsih_is_end_device(device_info)))
5146 return;
5147
5148 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5149 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5150 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5151 sas_address);
5152
5153 if (!sas_device) {
5154 printk(MPT2SAS_ERR_FMT "device is not present "
5155 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5156 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5157 return;
5158 }
5159
5160 if (unlikely(sas_device->handle != handle)) {
5161 starget = sas_device->starget;
5162 sas_target_priv_data = starget->hostdata;
5163 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5164 " to (0x%04x)!!!\n", sas_device->handle, handle);
5165 sas_target_priv_data->handle = handle;
5166 sas_device->handle = handle;
5167 }
Kashyap, Desaib4344272010-03-17 16:24:14 +05305168
5169 /* check if device is present */
5170 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5171 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5172 printk(MPT2SAS_ERR_FMT "device is not present "
5173 "handle(0x%04x), flags!!!\n", ioc->name, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305174 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305175 return;
5176 }
5177
5178 /* check if there were any issues with discovery */
5179 if (_scsih_check_access_status(ioc, sas_address, handle,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305180 sas_device_pg0.AccessStatus)) {
5181 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305182 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305183 }
5184 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305185 _scsih_ublock_io_device(ioc, sas_address);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305186
5187}
5188
5189/**
Eric Moore635374e2009-03-09 01:21:12 -06005190 * _scsih_add_device - creating sas device object
5191 * @ioc: per adapter object
5192 * @handle: sas device handle
5193 * @phy_num: phy number end device attached to
5194 * @is_pd: is this hidden raid component
5195 *
5196 * Creating end device object, stored in ioc->sas_device_list.
5197 *
5198 * Returns 0 for success, non-zero for failure.
5199 */
5200static int
5201_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5202{
5203 Mpi2ConfigReply_t mpi_reply;
5204 Mpi2SasDevicePage0_t sas_device_pg0;
5205 Mpi2SasEnclosurePage0_t enclosure_pg0;
5206 struct _sas_device *sas_device;
5207 u32 ioc_status;
5208 __le64 sas_address;
5209 u32 device_info;
5210 unsigned long flags;
5211
5212 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5213 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5214 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5215 ioc->name, __FILE__, __LINE__, __func__);
5216 return -1;
5217 }
5218
5219 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5220 MPI2_IOCSTATUS_MASK;
5221 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5222 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5223 ioc->name, __FILE__, __LINE__, __func__);
5224 return -1;
5225 }
5226
Kashyap, Desaib4344272010-03-17 16:24:14 +05305227 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5228
Eric Moore635374e2009-03-09 01:21:12 -06005229 /* check if device is present */
5230 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5231 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5232 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5233 ioc->name, __FILE__, __LINE__, __func__);
5234 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5235 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5236 return -1;
5237 }
5238
Kashyap, Desaib4344272010-03-17 16:24:14 +05305239 /* check if there were any issues with discovery */
5240 if (_scsih_check_access_status(ioc, sas_address, handle,
5241 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005242 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005243
5244 /* check if this is end device */
5245 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5246 if (!(_scsih_is_end_device(device_info))) {
5247 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5248 ioc->name, __FILE__, __LINE__, __func__);
5249 return -1;
5250 }
5251
Eric Moore635374e2009-03-09 01:21:12 -06005252
5253 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5254 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5255 sas_address);
5256 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5257
Kashyap, Desaib4344272010-03-17 16:24:14 +05305258 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005259 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005260
5261 sas_device = kzalloc(sizeof(struct _sas_device),
5262 GFP_KERNEL);
5263 if (!sas_device) {
5264 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5265 ioc->name, __FILE__, __LINE__, __func__);
5266 return -1;
5267 }
5268
5269 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305270 if (_scsih_get_sas_address(ioc, le16_to_cpu
5271 (sas_device_pg0.ParentDevHandle),
5272 &sas_device->sas_address_parent) != 0)
5273 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5274 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005275 sas_device->enclosure_handle =
5276 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5277 sas_device->slot =
5278 le16_to_cpu(sas_device_pg0.Slot);
5279 sas_device->device_info = device_info;
5280 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305281 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005282
5283 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305284 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5285 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5286 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005287 sas_device->enclosure_logical_id =
5288 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005289
5290 /* get device name */
5291 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5292
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305293 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005294 _scsih_sas_device_init_add(ioc, sas_device);
5295 else
5296 _scsih_sas_device_add(ioc, sas_device);
5297
5298 return 0;
5299}
5300
5301/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305302 * _scsih_remove_device - removing sas device object
5303 * @ioc: per adapter object
5304 * @sas_device_delete: the sas_device object
5305 *
5306 * Return nothing.
5307 */
5308static void
5309_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5310 struct _sas_device *sas_device)
5311{
Kashyap, Desai1278b112010-03-09 17:34:13 +05305312 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305313
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05305314 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5315 (sas_device->pfa_led_on)) {
5316 _scsih_turn_off_pfa_led(ioc, sas_device);
5317 sas_device->pfa_led_on = 0;
5318 }
5319
Kashyap, Desai1278b112010-03-09 17:34:13 +05305320 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5321 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305322 sas_device->handle, (unsigned long long)
5323 sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05305324
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305325 if (sas_device->starget && sas_device->starget->hostdata) {
5326 sas_target_priv_data = sas_device->starget->hostdata;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305327 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305328 _scsih_ublock_io_device(ioc, sas_device->sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305329 sas_target_priv_data->handle =
5330 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305331 }
5332
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305333 if (!ioc->hide_drives)
5334 mpt2sas_transport_port_remove(ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305335 sas_device->sas_address,
5336 sas_device->sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305337
5338 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305339 "(0x%016llx)\n", ioc->name, sas_device->handle,
5340 (unsigned long long) sas_device->sas_address);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305341
5342 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5343 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305344 sas_device->handle, (unsigned long long)
5345 sas_device->sas_address));
5346 kfree(sas_device);
5347}
5348/**
5349 * _scsih_device_remove_by_handle - removing device object by handle
5350 * @ioc: per adapter object
5351 * @handle: device handle
5352 *
5353 * Return nothing.
5354 */
5355static void
5356_scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5357{
5358 struct _sas_device *sas_device;
5359 unsigned long flags;
5360
5361 if (ioc->shost_recovery)
5362 return;
5363
5364 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5365 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5366 if (sas_device)
5367 list_del(&sas_device->list);
5368 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5369 if (sas_device)
5370 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005371}
5372
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305373/**
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305374 * mpt2sas_device_remove_by_sas_address - removing device object by sas address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305375 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305376 * @sas_address: device sas_address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305377 *
5378 * Return nothing.
5379 */
5380void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305381mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
5382 u64 sas_address)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305383{
5384 struct _sas_device *sas_device;
5385 unsigned long flags;
5386
5387 if (ioc->shost_recovery)
5388 return;
5389
5390 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5391 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5392 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305393 if (sas_device)
5394 list_del(&sas_device->list);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305395 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305396 if (sas_device)
5397 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305398}
Eric Moore635374e2009-03-09 01:21:12 -06005399#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5400/**
5401 * _scsih_sas_topology_change_event_debug - debug for topology event
5402 * @ioc: per adapter object
5403 * @event_data: event data payload
5404 * Context: user.
5405 */
5406static void
5407_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5408 Mpi2EventDataSasTopologyChangeList_t *event_data)
5409{
5410 int i;
5411 u16 handle;
5412 u16 reason_code;
5413 u8 phy_number;
5414 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305415 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005416
5417 switch (event_data->ExpStatus) {
5418 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5419 status_str = "add";
5420 break;
5421 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5422 status_str = "remove";
5423 break;
5424 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305425 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005426 status_str = "responding";
5427 break;
5428 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5429 status_str = "remove delay";
5430 break;
5431 default:
5432 status_str = "unknown status";
5433 break;
5434 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305435 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005436 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305437 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005438 "start_phy(%02d), count(%d)\n",
5439 le16_to_cpu(event_data->ExpanderDevHandle),
5440 le16_to_cpu(event_data->EnclosureHandle),
5441 event_data->StartPhyNum, event_data->NumEntries);
5442 for (i = 0; i < event_data->NumEntries; i++) {
5443 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5444 if (!handle)
5445 continue;
5446 phy_number = event_data->StartPhyNum + i;
5447 reason_code = event_data->PHY[i].PhyStatus &
5448 MPI2_EVENT_SAS_TOPO_RC_MASK;
5449 switch (reason_code) {
5450 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305451 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005452 break;
5453 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305454 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005455 break;
5456 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305457 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005458 break;
5459 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305460 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005461 break;
5462 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305463 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005464 break;
5465 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305466 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005467 break;
5468 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305469 link_rate = event_data->PHY[i].LinkRate >> 4;
5470 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305471 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305472 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5473 handle, status_str, link_rate, prev_link_rate);
5474
Eric Moore635374e2009-03-09 01:21:12 -06005475 }
5476}
5477#endif
5478
5479/**
5480 * _scsih_sas_topology_change_event - handle topology changes
5481 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305482 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005483 * Context: user.
5484 *
5485 */
5486static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305487_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005488 struct fw_event_work *fw_event)
5489{
5490 int i;
5491 u16 parent_handle, handle;
5492 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305493 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005494 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305495 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005496 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305497 u8 link_rate, prev_link_rate;
Joe Lawrence00713ad2014-06-25 17:03:33 -04005498 Mpi2EventDataSasTopologyChangeList_t *event_data =
5499 (Mpi2EventDataSasTopologyChangeList_t *)
5500 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005501
5502#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5503 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5504 _scsih_sas_topology_change_event_debug(ioc, event_data);
5505#endif
5506
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305507 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305508 return;
5509
Eric Moore635374e2009-03-09 01:21:12 -06005510 if (!ioc->sas_hba.num_phys)
5511 _scsih_sas_host_add(ioc);
5512 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305513 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005514
5515 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305516 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005517 "event\n", ioc->name));
5518 return;
5519 }
5520
5521 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5522
5523 /* handle expander add */
5524 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5525 if (_scsih_expander_add(ioc, parent_handle) != 0)
5526 return;
5527
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305528 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5529 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5530 parent_handle);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305531 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305532 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305533 max_phys = sas_expander->num_phys;
5534 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305535 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305536 max_phys = ioc->sas_hba.num_phys;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305537 } else {
5538 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305539 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305540 }
5541 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305542
Eric Moore635374e2009-03-09 01:21:12 -06005543 /* handle siblings events */
5544 for (i = 0; i < event_data->NumEntries; i++) {
5545 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305546 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005547 "expander event\n", ioc->name));
5548 return;
5549 }
Eric Moore3cb54692010-07-08 14:44:34 -06005550 if (ioc->shost_recovery || ioc->remove_host ||
5551 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305552 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305553 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305554 if (phy_number >= max_phys)
5555 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305556 reason_code = event_data->PHY[i].PhyStatus &
5557 MPI2_EVENT_SAS_TOPO_RC_MASK;
5558 if ((event_data->PHY[i].PhyStatus &
5559 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5560 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005561 continue;
5562 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5563 if (!handle)
5564 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305565 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305566 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005567 switch (reason_code) {
5568 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305569
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305570 if (ioc->shost_recovery)
5571 break;
5572
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305573 if (link_rate == prev_link_rate)
5574 break;
5575
5576 mpt2sas_transport_update_links(ioc, sas_address,
5577 handle, phy_number, link_rate);
5578
Kashyap, Desaib4344272010-03-17 16:24:14 +05305579 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5580 break;
5581
5582 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305583 break;
Eric Moore635374e2009-03-09 01:21:12 -06005584 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305585
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305586 if (ioc->shost_recovery)
5587 break;
5588
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305589 mpt2sas_transport_update_links(ioc, sas_address,
5590 handle, phy_number, link_rate);
5591
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305592 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005593 break;
5594 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305595
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305596 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005597 break;
5598 }
5599 }
5600
5601 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305602 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5603 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305604 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005605
5606}
5607
5608#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5609/**
5610 * _scsih_sas_device_status_change_event_debug - debug for device event
5611 * @event_data: event data payload
5612 * Context: user.
5613 *
5614 * Return nothing.
5615 */
5616static void
5617_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5618 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5619{
5620 char *reason_str = NULL;
5621
5622 switch (event_data->ReasonCode) {
5623 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5624 reason_str = "smart data";
5625 break;
5626 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5627 reason_str = "unsupported device discovered";
5628 break;
5629 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5630 reason_str = "internal device reset";
5631 break;
5632 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5633 reason_str = "internal task abort";
5634 break;
5635 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5636 reason_str = "internal task abort set";
5637 break;
5638 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5639 reason_str = "internal clear task set";
5640 break;
5641 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5642 reason_str = "internal query task";
5643 break;
5644 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5645 reason_str = "sata init failure";
5646 break;
5647 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5648 reason_str = "internal device reset complete";
5649 break;
5650 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5651 reason_str = "internal task abort complete";
5652 break;
5653 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5654 reason_str = "internal async notification";
5655 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305656 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5657 reason_str = "expander reduced functionality";
5658 break;
5659 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5660 reason_str = "expander reduced functionality complete";
5661 break;
Eric Moore635374e2009-03-09 01:21:12 -06005662 default:
5663 reason_str = "unknown reason";
5664 break;
5665 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305666 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305667 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5668 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5669 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5670 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005671 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305672 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005673 event_data->ASC, event_data->ASCQ);
5674 printk(KERN_INFO "\n");
5675}
5676#endif
5677
5678/**
5679 * _scsih_sas_device_status_change_event - handle device status change
5680 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305681 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005682 * Context: user.
5683 *
5684 * Return nothing.
5685 */
5686static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305687_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5688 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005689{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305690 struct MPT2SAS_TARGET *target_priv_data;
5691 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305692 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305693 unsigned long flags;
5694 Mpi2EventDataSasDeviceStatusChange_t *event_data =
Joe Lawrence00713ad2014-06-25 17:03:33 -04005695 (Mpi2EventDataSasDeviceStatusChange_t *)
5696 fw_event->event_data;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305697
Eric Moore635374e2009-03-09 01:21:12 -06005698#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5699 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305700 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305701 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005702#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305703
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305704 /* In MPI Revision K (0xC), the internal device reset complete was
5705 * implemented, so avoid setting tm_busy flag for older firmware.
5706 */
5707 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5708 return;
5709
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305710 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305711 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305712 event_data->ReasonCode !=
5713 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305714 return;
5715
5716 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5717 sas_address = le64_to_cpu(event_data->SASAddress);
5718 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5719 sas_address);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305720
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305721 if (!sas_device || !sas_device->starget) {
5722 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305723 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305724 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305725
5726 target_priv_data = sas_device->starget->hostdata;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305727 if (!target_priv_data) {
5728 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305729 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305730 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305731
5732 if (event_data->ReasonCode ==
5733 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5734 target_priv_data->tm_busy = 1;
5735 else
5736 target_priv_data->tm_busy = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305737 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005738}
5739
5740#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5741/**
5742 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5743 * @ioc: per adapter object
5744 * @event_data: event data payload
5745 * Context: user.
5746 *
5747 * Return nothing.
5748 */
5749static void
5750_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5751 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5752{
5753 char *reason_str = NULL;
5754
5755 switch (event_data->ReasonCode) {
5756 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5757 reason_str = "enclosure add";
5758 break;
5759 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5760 reason_str = "enclosure remove";
5761 break;
5762 default:
5763 reason_str = "unknown reason";
5764 break;
5765 }
5766
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305767 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005768 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5769 " number slots(%d)\n", ioc->name, reason_str,
5770 le16_to_cpu(event_data->EnclosureHandle),
5771 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5772 le16_to_cpu(event_data->StartSlot));
5773}
5774#endif
5775
5776/**
5777 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5778 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305779 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005780 * Context: user.
5781 *
5782 * Return nothing.
5783 */
5784static void
5785_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305786 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005787{
5788#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5789 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5790 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Joe Lawrence00713ad2014-06-25 17:03:33 -04005791 (Mpi2EventDataSasEnclDevStatusChange_t *)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305792 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005793#endif
5794}
5795
5796/**
Andrew Mortona78e21d2012-02-08 12:52:22 -08005797 * _scsih_sas_broadcast_primitive_event - handle broadcast events
Eric Moore635374e2009-03-09 01:21:12 -06005798 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305799 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005800 * Context: user.
5801 *
5802 * Return nothing.
5803 */
5804static void
Andrew Mortona78e21d2012-02-08 12:52:22 -08005805_scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305806 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005807{
5808 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305809 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005810 u16 smid, handle;
5811 u32 lun;
5812 struct MPT2SAS_DEVICE *sas_device_priv_data;
5813 u32 termination_count;
5814 u32 query_count;
5815 Mpi2SCSITaskManagementReply_t *mpi_reply;
Joe Lawrence00713ad2014-06-25 17:03:33 -04005816 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5817 (Mpi2EventDataSasBroadcastPrimitive_t *)
5818 fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305819 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305820 unsigned long flags;
5821 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305822 u8 max_retries = 0;
5823 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305824
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305825 mutex_lock(&ioc->tm_cmds.mutex);
Reddy, Sreekanthc3a634b2013-02-26 17:36:12 +05305826 pr_info(MPT2SAS_FMT
5827 "%s: enter: phy number(%d), width(%d)\n",
5828 ioc->name, __func__, event_data->PhyNum,
5829 event_data->PortWidth);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305830
5831 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005832
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305833 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305834 mpi_reply = ioc->tm_cmds.reply;
5835broadcast_aen_retry:
5836
5837 /* sanity checks for retrying this loop */
5838 if (max_retries++ == 5) {
5839 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5840 ioc->name, __func__));
5841 goto out;
5842 } else if (max_retries > 1)
5843 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5844 ioc->name, __func__, max_retries - 1));
5845
Eric Moore635374e2009-03-09 01:21:12 -06005846 termination_count = 0;
5847 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305848 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305849 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305850 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005851 scmd = _scsih_scsi_lookup_get(ioc, smid);
5852 if (!scmd)
5853 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305854 sdev = scmd->device;
5855 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005856 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5857 continue;
5858 /* skip hidden raid components */
5859 if (sas_device_priv_data->sas_target->flags &
5860 MPT_TARGET_FLAGS_RAID_COMPONENT)
5861 continue;
5862 /* skip volumes */
5863 if (sas_device_priv_data->sas_target->flags &
5864 MPT_TARGET_FLAGS_VOLUME)
5865 continue;
5866
5867 handle = sas_device_priv_data->sas_target->handle;
5868 lun = sas_device_priv_data->lun;
5869 query_count++;
5870
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305871 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305872 goto out;
5873
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305874 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305875 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04005876 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305877 TM_MUTEX_OFF);
5878 if (r == FAILED) {
5879 sdev_printk(KERN_WARNING, sdev,
5880 "mpt2sas_scsih_issue_tm: FAILED when sending "
5881 "QUERY_TASK: scmd(%p)\n", scmd);
5882 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5883 goto broadcast_aen_retry;
5884 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305885 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5886 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305887 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5888 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5889 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5890 scmd);
5891 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5892 goto broadcast_aen_retry;
5893 }
5894
5895 /* see if IO is still owned by IOC and target */
5896 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005897 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5898 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305899 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305900 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005901 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305902 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305903 task_abort_retries = 0;
5904 tm_retry:
5905 if (task_abort_retries++ == 60) {
5906 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5907 "%s: ABORT_TASK: giving up\n", ioc->name,
5908 __func__));
5909 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5910 goto broadcast_aen_retry;
5911 }
5912
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305913 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305914 goto out_no_lock;
5915
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305916 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5917 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04005918 TM_MUTEX_OFF);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305919 if (r == FAILED) {
5920 sdev_printk(KERN_WARNING, sdev,
5921 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305922 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305923 goto tm_retry;
5924 }
5925
5926 if (task_abort_retries > 1)
5927 sdev_printk(KERN_WARNING, sdev,
5928 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5929 " scmd(%p)\n",
5930 task_abort_retries - 1, scmd);
5931
Eric Moore635374e2009-03-09 01:21:12 -06005932 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305933 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005934 }
Eric Moore635374e2009-03-09 01:21:12 -06005935
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305936 if (ioc->broadcast_aen_pending) {
5937 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5938 " pending AEN\n", ioc->name, __func__));
5939 ioc->broadcast_aen_pending = 0;
5940 goto broadcast_aen_retry;
5941 }
5942
5943 out:
5944 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5945 out_no_lock:
5946
5947 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005948 "%s - exit, query_count = %d termination_count = %d\n",
5949 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305950
5951 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305952 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305953 _scsih_ublock_io_all_device(ioc);
5954 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005955}
5956
5957/**
5958 * _scsih_sas_discovery_event - handle discovery events
5959 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305960 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005961 * Context: user.
5962 *
5963 * Return nothing.
5964 */
5965static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305966_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5967 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005968{
Joe Lawrence00713ad2014-06-25 17:03:33 -04005969 Mpi2EventDataSasDiscovery_t *event_data =
5970 (Mpi2EventDataSasDiscovery_t *)
5971 fw_event->event_data;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305972
Eric Moore635374e2009-03-09 01:21:12 -06005973#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5974 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305975 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005976 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5977 "start" : "stop");
5978 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305979 printk("discovery_status(0x%08x)",
5980 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005981 printk("\n");
5982 }
5983#endif
5984
5985 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305986 !ioc->sas_hba.num_phys) {
5987 if (disable_discovery > 0 && ioc->shost_recovery) {
5988 /* Wait for the reset to complete */
5989 while (ioc->shost_recovery)
5990 ssleep(1);
5991 }
Eric Moore635374e2009-03-09 01:21:12 -06005992 _scsih_sas_host_add(ioc);
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305993 }
Eric Moore635374e2009-03-09 01:21:12 -06005994}
5995
5996/**
5997 * _scsih_reprobe_lun - reprobing lun
5998 * @sdev: scsi device struct
5999 * @no_uld_attach: sdev->no_uld_attach flag setting
6000 *
6001 **/
6002static void
6003_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6004{
6005 int rc;
6006
6007 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6008 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6009 sdev->no_uld_attach ? "hidding" : "exposing");
6010 rc = scsi_device_reprobe(sdev);
6011}
6012
6013/**
Eric Moore635374e2009-03-09 01:21:12 -06006014 * _scsih_sas_volume_add - add new volume
6015 * @ioc: per adapter object
6016 * @element: IR config element data
6017 * Context: user.
6018 *
6019 * Return nothing.
6020 */
6021static void
6022_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
6023 Mpi2EventIrConfigElement_t *element)
6024{
6025 struct _raid_device *raid_device;
6026 unsigned long flags;
6027 u64 wwid;
6028 u16 handle = le16_to_cpu(element->VolDevHandle);
6029 int rc;
6030
Eric Moore635374e2009-03-09 01:21:12 -06006031 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6032 if (!wwid) {
6033 printk(MPT2SAS_ERR_FMT
6034 "failure at %s:%d/%s()!\n", ioc->name,
6035 __FILE__, __LINE__, __func__);
6036 return;
6037 }
6038
6039 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6040 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6041 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6042
6043 if (raid_device)
6044 return;
6045
6046 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6047 if (!raid_device) {
6048 printk(MPT2SAS_ERR_FMT
6049 "failure at %s:%d/%s()!\n", ioc->name,
6050 __FILE__, __LINE__, __func__);
6051 return;
6052 }
6053
6054 raid_device->id = ioc->sas_id++;
6055 raid_device->channel = RAID_CHANNEL;
6056 raid_device->handle = handle;
6057 raid_device->wwid = wwid;
6058 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306059 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06006060 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6061 raid_device->id, 0);
6062 if (rc)
6063 _scsih_raid_device_remove(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306064 } else {
6065 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006066 _scsih_determine_boot_device(ioc, raid_device, 1);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306067 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6068 }
Eric Moore635374e2009-03-09 01:21:12 -06006069}
6070
6071/**
6072 * _scsih_sas_volume_delete - delete volume
6073 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306074 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006075 * Context: user.
6076 *
6077 * Return nothing.
6078 */
6079static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306080_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006081{
6082 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006083 unsigned long flags;
6084 struct MPT2SAS_TARGET *sas_target_priv_data;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306085 struct scsi_target *starget = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006086
Eric Moore635374e2009-03-09 01:21:12 -06006087 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6088 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306089 if (raid_device) {
6090 if (raid_device->starget) {
6091 starget = raid_device->starget;
6092 sas_target_priv_data = starget->hostdata;
6093 sas_target_priv_data->deleted = 1;
6094 }
6095 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6096 "(0x%016llx)\n", ioc->name, raid_device->handle,
6097 (unsigned long long) raid_device->wwid);
6098 list_del(&raid_device->list);
6099 kfree(raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006100 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306101 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6102 if (starget)
6103 scsi_remove_target(&starget->dev);
Eric Moore635374e2009-03-09 01:21:12 -06006104}
6105
6106/**
6107 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6108 * @ioc: per adapter object
6109 * @element: IR config element data
6110 * Context: user.
6111 *
6112 * Return nothing.
6113 */
6114static void
6115_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6116 Mpi2EventIrConfigElement_t *element)
6117{
6118 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306119 struct scsi_target *starget = NULL;
6120 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006121 unsigned long flags;
6122 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6123
6124 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6125 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306126 if (sas_device) {
6127 sas_device->volume_handle = 0;
6128 sas_device->volume_wwid = 0;
6129 clear_bit(handle, ioc->pd_handles);
6130 if (sas_device->starget && sas_device->starget->hostdata) {
6131 starget = sas_device->starget;
6132 sas_target_priv_data = starget->hostdata;
6133 sas_target_priv_data->flags &=
6134 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6135 }
6136 }
Eric Moore635374e2009-03-09 01:21:12 -06006137 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6138 if (!sas_device)
6139 return;
6140
6141 /* exposing raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306142 if (starget)
6143 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006144}
6145
6146/**
6147 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6148 * @ioc: per adapter object
6149 * @element: IR config element data
6150 * Context: user.
6151 *
6152 * Return nothing.
6153 */
6154static void
6155_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6156 Mpi2EventIrConfigElement_t *element)
6157{
6158 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306159 struct scsi_target *starget = NULL;
6160 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006161 unsigned long flags;
6162 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306163 u16 volume_handle = 0;
6164 u64 volume_wwid = 0;
6165
6166 mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
6167 if (volume_handle)
6168 mpt2sas_config_get_volume_wwid(ioc, volume_handle,
6169 &volume_wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006170
6171 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6172 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306173 if (sas_device) {
6174 set_bit(handle, ioc->pd_handles);
6175 if (sas_device->starget && sas_device->starget->hostdata) {
6176 starget = sas_device->starget;
6177 sas_target_priv_data = starget->hostdata;
6178 sas_target_priv_data->flags |=
6179 MPT_TARGET_FLAGS_RAID_COMPONENT;
6180 sas_device->volume_handle = volume_handle;
6181 sas_device->volume_wwid = volume_wwid;
6182 }
6183 }
Eric Moore635374e2009-03-09 01:21:12 -06006184 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6185 if (!sas_device)
6186 return;
6187
6188 /* hiding raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306189 if (starget)
6190 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006191}
6192
6193/**
6194 * _scsih_sas_pd_delete - delete pd component
6195 * @ioc: per adapter object
6196 * @element: IR config element data
6197 * Context: user.
6198 *
6199 * Return nothing.
6200 */
6201static void
6202_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6203 Mpi2EventIrConfigElement_t *element)
6204{
Eric Moore635374e2009-03-09 01:21:12 -06006205 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6206
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306207 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006208}
6209
6210/**
6211 * _scsih_sas_pd_add - remove pd component
6212 * @ioc: per adapter object
6213 * @element: IR config element data
6214 * Context: user.
6215 *
6216 * Return nothing.
6217 */
6218static void
6219_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6220 Mpi2EventIrConfigElement_t *element)
6221{
6222 struct _sas_device *sas_device;
6223 unsigned long flags;
6224 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306225 Mpi2ConfigReply_t mpi_reply;
6226 Mpi2SasDevicePage0_t sas_device_pg0;
6227 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306228 u64 sas_address;
6229 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006230
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306231 set_bit(handle, ioc->pd_handles);
6232
Eric Moore635374e2009-03-09 01:21:12 -06006233 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6234 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6235 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306236 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306237 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306238
6239 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6240 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6241 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6242 ioc->name, __FILE__, __LINE__, __func__);
6243 return;
6244 }
6245
6246 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6247 MPI2_IOCSTATUS_MASK;
6248 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6249 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6250 ioc->name, __FILE__, __LINE__, __func__);
6251 return;
6252 }
6253
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306254 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6255 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6256 mpt2sas_transport_update_links(ioc, sas_address, handle,
6257 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306258
6259 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006260}
6261
6262#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6263/**
6264 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6265 * @ioc: per adapter object
6266 * @event_data: event data payload
6267 * Context: user.
6268 *
6269 * Return nothing.
6270 */
6271static void
6272_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6273 Mpi2EventDataIrConfigChangeList_t *event_data)
6274{
6275 Mpi2EventIrConfigElement_t *element;
6276 u8 element_type;
6277 int i;
6278 char *reason_str = NULL, *element_str = NULL;
6279
6280 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6281
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306282 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006283 ioc->name, (le32_to_cpu(event_data->Flags) &
6284 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6285 "foreign" : "native", event_data->NumElements);
6286 for (i = 0; i < event_data->NumElements; i++, element++) {
6287 switch (element->ReasonCode) {
6288 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6289 reason_str = "add";
6290 break;
6291 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6292 reason_str = "remove";
6293 break;
6294 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6295 reason_str = "no change";
6296 break;
6297 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6298 reason_str = "hide";
6299 break;
6300 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6301 reason_str = "unhide";
6302 break;
6303 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6304 reason_str = "volume_created";
6305 break;
6306 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6307 reason_str = "volume_deleted";
6308 break;
6309 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6310 reason_str = "pd_created";
6311 break;
6312 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6313 reason_str = "pd_deleted";
6314 break;
6315 default:
6316 reason_str = "unknown reason";
6317 break;
6318 }
6319 element_type = le16_to_cpu(element->ElementFlags) &
6320 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6321 switch (element_type) {
6322 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6323 element_str = "volume";
6324 break;
6325 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6326 element_str = "phys disk";
6327 break;
6328 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6329 element_str = "hot spare";
6330 break;
6331 default:
6332 element_str = "unknown element";
6333 break;
6334 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306335 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006336 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6337 reason_str, le16_to_cpu(element->VolDevHandle),
6338 le16_to_cpu(element->PhysDiskDevHandle),
6339 element->PhysDiskNum);
6340 }
6341}
6342#endif
6343
6344/**
6345 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6346 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306347 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006348 * Context: user.
6349 *
6350 * Return nothing.
6351 */
6352static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306353_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6354 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006355{
6356 Mpi2EventIrConfigElement_t *element;
6357 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306358 u8 foreign_config;
Joe Lawrence00713ad2014-06-25 17:03:33 -04006359 Mpi2EventDataIrConfigChangeList_t *event_data =
6360 (Mpi2EventDataIrConfigChangeList_t *)
6361 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006362
6363#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306364 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6365 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006366 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6367
6368#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306369
6370 if (ioc->shost_recovery)
6371 return;
6372
Kashyap, Desai62727a72009-08-07 19:35:18 +05306373 foreign_config = (le32_to_cpu(event_data->Flags) &
6374 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006375
6376 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6377 for (i = 0; i < event_data->NumElements; i++, element++) {
6378
6379 switch (element->ReasonCode) {
6380 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6381 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306382 if (!foreign_config)
6383 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006384 break;
6385 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6386 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306387 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306388 _scsih_sas_volume_delete(ioc,
6389 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006390 break;
6391 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306392 if (!ioc->is_warpdrive)
6393 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006394 break;
6395 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306396 if (!ioc->is_warpdrive)
6397 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006398 break;
6399 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306400 if (!ioc->is_warpdrive)
6401 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006402 break;
6403 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306404 if (!ioc->is_warpdrive)
6405 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006406 break;
6407 }
6408 }
6409}
6410
6411/**
6412 * _scsih_sas_ir_volume_event - IR volume event
6413 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306414 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006415 * Context: user.
6416 *
6417 * Return nothing.
6418 */
6419static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306420_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6421 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006422{
6423 u64 wwid;
6424 unsigned long flags;
6425 struct _raid_device *raid_device;
6426 u16 handle;
6427 u32 state;
6428 int rc;
Joe Lawrence00713ad2014-06-25 17:03:33 -04006429 Mpi2EventDataIrVolume_t *event_data =
6430 (Mpi2EventDataIrVolume_t *)
6431 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006432
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306433 if (ioc->shost_recovery)
6434 return;
6435
Eric Moore635374e2009-03-09 01:21:12 -06006436 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6437 return;
6438
6439 handle = le16_to_cpu(event_data->VolDevHandle);
6440 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306441 if (!ioc->hide_ir_msg)
6442 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6443 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6444 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006445
Eric Moore635374e2009-03-09 01:21:12 -06006446 switch (state) {
6447 case MPI2_RAID_VOL_STATE_MISSING:
6448 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306449 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006450 break;
6451
6452 case MPI2_RAID_VOL_STATE_ONLINE:
6453 case MPI2_RAID_VOL_STATE_DEGRADED:
6454 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306455
6456 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6457 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6458 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6459
Eric Moore635374e2009-03-09 01:21:12 -06006460 if (raid_device)
6461 break;
6462
6463 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6464 if (!wwid) {
6465 printk(MPT2SAS_ERR_FMT
6466 "failure at %s:%d/%s()!\n", ioc->name,
6467 __FILE__, __LINE__, __func__);
6468 break;
6469 }
6470
6471 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6472 if (!raid_device) {
6473 printk(MPT2SAS_ERR_FMT
6474 "failure at %s:%d/%s()!\n", ioc->name,
6475 __FILE__, __LINE__, __func__);
6476 break;
6477 }
6478
6479 raid_device->id = ioc->sas_id++;
6480 raid_device->channel = RAID_CHANNEL;
6481 raid_device->handle = handle;
6482 raid_device->wwid = wwid;
6483 _scsih_raid_device_add(ioc, raid_device);
6484 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6485 raid_device->id, 0);
6486 if (rc)
6487 _scsih_raid_device_remove(ioc, raid_device);
6488 break;
6489
6490 case MPI2_RAID_VOL_STATE_INITIALIZING:
6491 default:
6492 break;
6493 }
6494}
6495
6496/**
6497 * _scsih_sas_ir_physical_disk_event - PD event
6498 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306499 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006500 * Context: user.
6501 *
6502 * Return nothing.
6503 */
6504static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306505_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6506 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006507{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306508 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006509 u32 state;
6510 struct _sas_device *sas_device;
6511 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306512 Mpi2ConfigReply_t mpi_reply;
6513 Mpi2SasDevicePage0_t sas_device_pg0;
6514 u32 ioc_status;
Joe Lawrence00713ad2014-06-25 17:03:33 -04006515 Mpi2EventDataIrPhysicalDisk_t *event_data =
6516 (Mpi2EventDataIrPhysicalDisk_t *)
6517 fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306518 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006519
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306520 if (ioc->shost_recovery)
6521 return;
6522
Eric Moore635374e2009-03-09 01:21:12 -06006523 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6524 return;
6525
6526 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6527 state = le32_to_cpu(event_data->NewValue);
6528
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306529 if (!ioc->hide_ir_msg)
6530 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6531 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6532 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006533
Eric Moore635374e2009-03-09 01:21:12 -06006534 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006535 case MPI2_RAID_PD_STATE_ONLINE:
6536 case MPI2_RAID_PD_STATE_DEGRADED:
6537 case MPI2_RAID_PD_STATE_REBUILDING:
6538 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306539 case MPI2_RAID_PD_STATE_HOT_SPARE:
6540
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306541 if (!ioc->is_warpdrive)
6542 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306543
6544 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6545 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6546 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6547
6548 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306549 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306550
6551 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6552 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6553 handle))) {
6554 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6555 ioc->name, __FILE__, __LINE__, __func__);
6556 return;
6557 }
6558
6559 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6560 MPI2_IOCSTATUS_MASK;
6561 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6562 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6563 ioc->name, __FILE__, __LINE__, __func__);
6564 return;
6565 }
6566
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306567 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6568 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6569 mpt2sas_transport_update_links(ioc, sas_address, handle,
6570 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306571
6572 _scsih_add_device(ioc, handle, 0, 1);
6573
Eric Moore635374e2009-03-09 01:21:12 -06006574 break;
6575
Kashyap, Desai62727a72009-08-07 19:35:18 +05306576 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006577 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6578 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006579 default:
6580 break;
6581 }
6582}
6583
6584#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6585/**
6586 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6587 * @ioc: per adapter object
6588 * @event_data: event data payload
6589 * Context: user.
6590 *
6591 * Return nothing.
6592 */
6593static void
6594_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6595 Mpi2EventDataIrOperationStatus_t *event_data)
6596{
6597 char *reason_str = NULL;
6598
6599 switch (event_data->RAIDOperation) {
6600 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6601 reason_str = "resync";
6602 break;
6603 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6604 reason_str = "online capacity expansion";
6605 break;
6606 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6607 reason_str = "consistency check";
6608 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306609 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6610 reason_str = "background init";
6611 break;
6612 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6613 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006614 break;
6615 }
6616
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306617 if (!reason_str)
6618 return;
6619
Eric Moore635374e2009-03-09 01:21:12 -06006620 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6621 "\thandle(0x%04x), percent complete(%d)\n",
6622 ioc->name, reason_str,
6623 le16_to_cpu(event_data->VolDevHandle),
6624 event_data->PercentComplete);
6625}
6626#endif
6627
6628/**
6629 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6630 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306631 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006632 * Context: user.
6633 *
6634 * Return nothing.
6635 */
6636static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306637_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6638 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006639{
Joe Lawrence00713ad2014-06-25 17:03:33 -04006640 Mpi2EventDataIrOperationStatus_t *event_data =
6641 (Mpi2EventDataIrOperationStatus_t *)
6642 fw_event->event_data;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306643 static struct _raid_device *raid_device;
6644 unsigned long flags;
6645 u16 handle;
6646
Eric Moore635374e2009-03-09 01:21:12 -06006647#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306648 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6649 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306650 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306651 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006652#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306653
6654 /* code added for raid transport support */
6655 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6656
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306657 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306658 handle = le16_to_cpu(event_data->VolDevHandle);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306659 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306660 if (raid_device)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306661 raid_device->percent_complete =
6662 event_data->PercentComplete;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306663 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306664 }
Eric Moore635374e2009-03-09 01:21:12 -06006665}
6666
6667/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306668 * _scsih_prep_device_scan - initialize parameters prior to device scan
6669 * @ioc: per adapter object
6670 *
6671 * Set the deleted flag prior to device scan. If the device is found during
6672 * the scan, then we clear the deleted flag.
6673 */
6674static void
6675_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6676{
6677 struct MPT2SAS_DEVICE *sas_device_priv_data;
6678 struct scsi_device *sdev;
6679
6680 shost_for_each_device(sdev, ioc->shost) {
6681 sas_device_priv_data = sdev->hostdata;
6682 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6683 sas_device_priv_data->sas_target->deleted = 1;
6684 }
6685}
6686
6687/**
Eric Moore635374e2009-03-09 01:21:12 -06006688 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6689 * @ioc: per adapter object
6690 * @sas_address: sas address
6691 * @slot: enclosure slot id
6692 * @handle: device handle
6693 *
6694 * After host reset, find out whether devices are still responding.
6695 * Used in _scsi_remove_unresponsive_sas_devices.
6696 *
6697 * Return nothing.
6698 */
6699static void
6700_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6701 u16 slot, u16 handle)
6702{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306703 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006704 struct scsi_target *starget;
6705 struct _sas_device *sas_device;
6706 unsigned long flags;
6707
6708 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6709 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6710 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306711 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006712 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306713 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306714 if (starget && starget->hostdata) {
6715 sas_target_priv_data = starget->hostdata;
6716 sas_target_priv_data->tm_busy = 0;
6717 sas_target_priv_data->deleted = 0;
6718 } else
6719 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306720 if (starget)
6721 starget_printk(KERN_INFO, starget,
6722 "handle(0x%04x), sas_addr(0x%016llx), "
6723 "enclosure logical id(0x%016llx), "
6724 "slot(%d)\n", handle,
6725 (unsigned long long)sas_device->sas_address,
6726 (unsigned long long)
6727 sas_device->enclosure_logical_id,
6728 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006729 if (sas_device->handle == handle)
6730 goto out;
6731 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6732 sas_device->handle);
6733 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306734 if (sas_target_priv_data)
6735 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006736 goto out;
6737 }
6738 }
6739 out:
6740 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6741}
6742
6743/**
6744 * _scsih_search_responding_sas_devices -
6745 * @ioc: per adapter object
6746 *
6747 * After host reset, find out whether devices are still responding.
6748 * If not remove.
6749 *
6750 * Return nothing.
6751 */
6752static void
6753_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6754{
6755 Mpi2SasDevicePage0_t sas_device_pg0;
6756 Mpi2ConfigReply_t mpi_reply;
6757 u16 ioc_status;
6758 __le64 sas_address;
6759 u16 handle;
6760 u32 device_info;
6761 u16 slot;
6762
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306763 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006764
6765 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306766 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006767
6768 handle = 0xFFFF;
6769 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6770 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6771 handle))) {
6772 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6773 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306774 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006775 break;
6776 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6777 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6778 if (!(_scsih_is_end_device(device_info)))
6779 continue;
6780 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6781 slot = le16_to_cpu(sas_device_pg0.Slot);
6782 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6783 handle);
6784 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306785out:
6786 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6787 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006788}
6789
6790/**
6791 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6792 * @ioc: per adapter object
6793 * @wwid: world wide identifier for raid volume
6794 * @handle: device handle
6795 *
6796 * After host reset, find out whether devices are still responding.
6797 * Used in _scsi_remove_unresponsive_raid_devices.
6798 *
6799 * Return nothing.
6800 */
6801static void
6802_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6803 u16 handle)
6804{
6805 struct MPT2SAS_TARGET *sas_target_priv_data;
6806 struct scsi_target *starget;
6807 struct _raid_device *raid_device;
6808 unsigned long flags;
6809
6810 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6811 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6812 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306813 starget = raid_device->starget;
6814 if (starget && starget->hostdata) {
6815 sas_target_priv_data = starget->hostdata;
6816 sas_target_priv_data->deleted = 0;
6817 } else
6818 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006819 raid_device->responding = 1;
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306820 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006821 starget_printk(KERN_INFO, raid_device->starget,
6822 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6823 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306824 /*
6825 * WARPDRIVE: The handles of the PDs might have changed
6826 * across the host reset so re-initialize the
6827 * required data for Direct IO
6828 */
6829 _scsih_init_warpdrive_properties(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306830 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6831 if (raid_device->handle == handle) {
6832 spin_unlock_irqrestore(&ioc->raid_device_lock,
6833 flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306834 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306835 }
Eric Moore635374e2009-03-09 01:21:12 -06006836 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6837 raid_device->handle);
6838 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306839 if (sas_target_priv_data)
6840 sas_target_priv_data->handle = handle;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306841 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306842 return;
Eric Moore635374e2009-03-09 01:21:12 -06006843 }
6844 }
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306845
Eric Moore635374e2009-03-09 01:21:12 -06006846 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6847}
6848
6849/**
6850 * _scsih_search_responding_raid_devices -
6851 * @ioc: per adapter object
6852 *
6853 * After host reset, find out whether devices are still responding.
6854 * If not remove.
6855 *
6856 * Return nothing.
6857 */
6858static void
6859_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6860{
6861 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306862 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306863 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006864 Mpi2ConfigReply_t mpi_reply;
6865 u16 ioc_status;
6866 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306867 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006868
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306869 if (!ioc->ir_firmware)
6870 return;
6871
6872 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6873 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006874
6875 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306876 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006877
6878 handle = 0xFFFF;
6879 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6880 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6881 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6882 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306883 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006884 break;
6885 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306886
6887 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6888 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6889 sizeof(Mpi2RaidVolPage0_t)))
6890 continue;
6891
6892 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6893 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6894 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6895 _scsih_mark_responding_raid_device(ioc,
6896 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006897 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306898
6899 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306900 if (!ioc->is_warpdrive) {
6901 phys_disk_num = 0xFF;
6902 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6903 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6904 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6905 phys_disk_num))) {
6906 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6907 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306908 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306909 break;
6910 phys_disk_num = pd_pg0.PhysDiskNum;
6911 handle = le16_to_cpu(pd_pg0.DevHandle);
6912 set_bit(handle, ioc->pd_handles);
6913 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306914 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306915out:
6916 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6917 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006918}
6919
6920/**
6921 * _scsih_mark_responding_expander - mark a expander as responding
6922 * @ioc: per adapter object
6923 * @sas_address: sas address
6924 * @handle:
6925 *
6926 * After host reset, find out whether devices are still responding.
6927 * Used in _scsi_remove_unresponsive_expanders.
6928 *
6929 * Return nothing.
6930 */
6931static void
6932_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6933 u16 handle)
6934{
6935 struct _sas_node *sas_expander;
6936 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306937 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006938
6939 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6940 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306941 if (sas_expander->sas_address != sas_address)
6942 continue;
6943 sas_expander->responding = 1;
6944 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006945 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306946 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6947 " from(0x%04x) to (0x%04x)!!!\n",
6948 (unsigned long long)sas_expander->sas_address,
6949 sas_expander->handle, handle);
6950 sas_expander->handle = handle;
6951 for (i = 0 ; i < sas_expander->num_phys ; i++)
6952 sas_expander->phy[i].handle = handle;
6953 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006954 }
6955 out:
6956 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6957}
6958
6959/**
6960 * _scsih_search_responding_expanders -
6961 * @ioc: per adapter object
6962 *
6963 * After host reset, find out whether devices are still responding.
6964 * If not remove.
6965 *
6966 * Return nothing.
6967 */
6968static void
6969_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6970{
6971 Mpi2ExpanderPage0_t expander_pg0;
6972 Mpi2ConfigReply_t mpi_reply;
6973 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306974 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006975 u16 handle;
6976
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306977 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006978
6979 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306980 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006981
6982 handle = 0xFFFF;
6983 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6984 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6985
6986 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6987 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306988 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006989 break;
6990
6991 handle = le16_to_cpu(expander_pg0.DevHandle);
6992 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6993 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6994 "sas_addr(0x%016llx)\n", handle,
6995 (unsigned long long)sas_address);
6996 _scsih_mark_responding_expander(ioc, sas_address, handle);
6997 }
6998
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306999 out:
7000 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007001}
7002
7003/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307004 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06007005 * @ioc: per adapter object
7006 *
7007 * Return nothing.
7008 */
7009static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307010_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06007011{
7012 struct _sas_device *sas_device, *sas_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307013 struct _sas_node *sas_expander, *sas_expander_next;
Eric Moore635374e2009-03-09 01:21:12 -06007014 struct _raid_device *raid_device, *raid_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307015 struct list_head tmp_list;
7016 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007017
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307018 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
7019 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007020
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307021 /* removing unresponding end devices */
7022 printk(MPT2SAS_INFO_FMT "removing unresponding devices: end-devices\n",
7023 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007024 list_for_each_entry_safe(sas_device, sas_device_next,
7025 &ioc->sas_device_list, list) {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307026 if (!sas_device->responding)
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05307027 mpt2sas_device_remove_by_sas_address(ioc,
7028 sas_device->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307029 else
Eric Moore635374e2009-03-09 01:21:12 -06007030 sas_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007031 }
7032
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307033 /* removing unresponding volumes */
7034 if (ioc->ir_firmware) {
7035 printk(MPT2SAS_INFO_FMT "removing unresponding devices: "
7036 "volumes\n", ioc->name);
7037 list_for_each_entry_safe(raid_device, raid_device_next,
7038 &ioc->raid_device_list, list) {
7039 if (!raid_device->responding)
7040 _scsih_sas_volume_delete(ioc,
7041 raid_device->handle);
7042 else
7043 raid_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007044 }
Eric Moore635374e2009-03-09 01:21:12 -06007045 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307046 /* removing unresponding expanders */
7047 printk(MPT2SAS_INFO_FMT "removing unresponding devices: expanders\n",
7048 ioc->name);
7049 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7050 INIT_LIST_HEAD(&tmp_list);
7051 list_for_each_entry_safe(sas_expander, sas_expander_next,
7052 &ioc->sas_expander_list, list) {
7053 if (!sas_expander->responding)
7054 list_move_tail(&sas_expander->list, &tmp_list);
7055 else
Eric Moore635374e2009-03-09 01:21:12 -06007056 sas_expander->responding = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307057 }
7058 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7059 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7060 list) {
7061 list_del(&sas_expander->list);
7062 _scsih_expander_node_remove(ioc, sas_expander);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307063 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307064 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
7065 ioc->name);
7066 /* unblock devices */
7067 _scsih_ublock_io_all_device(ioc);
7068}
7069
7070static void
7071_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
7072 struct _sas_node *sas_expander, u16 handle)
7073{
7074 Mpi2ExpanderPage1_t expander_pg1;
7075 Mpi2ConfigReply_t mpi_reply;
7076 int i;
7077
7078 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7079 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
7080 &expander_pg1, i, handle))) {
7081 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7082 ioc->name, __FILE__, __LINE__, __func__);
7083 return;
7084 }
7085
7086 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
7087 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7088 expander_pg1.NegotiatedLinkRate >> 4);
7089 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307090}
7091
7092/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307093 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307094 * @ioc: per adapter object
7095 *
7096 * Return nothing.
7097 */
7098static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307099_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307100{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307101 Mpi2ExpanderPage0_t expander_pg0;
7102 Mpi2SasDevicePage0_t sas_device_pg0;
7103 Mpi2RaidVolPage1_t volume_pg1;
7104 Mpi2RaidVolPage0_t volume_pg0;
7105 Mpi2RaidPhysDiskPage0_t pd_pg0;
7106 Mpi2EventIrConfigElement_t element;
7107 Mpi2ConfigReply_t mpi_reply;
7108 u8 phys_disk_num;
7109 u16 ioc_status;
7110 u16 handle, parent_handle;
7111 u64 sas_address;
7112 struct _sas_device *sas_device;
7113 struct _sas_node *expander_device;
7114 static struct _raid_device *raid_device;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307115 u8 retry_count;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307116 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307117
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307118 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307119
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307120 _scsih_sas_host_refresh(ioc);
7121
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307122 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders start\n",
7123 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307124 /* expanders */
7125 handle = 0xFFFF;
7126 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7127 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7128 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7129 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307130 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7131 printk(MPT2SAS_INFO_FMT "\tbreak from expander scan: "
7132 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7133 ioc->name, ioc_status,
7134 le32_to_cpu(mpi_reply.IOCLogInfo));
7135 break;
7136 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307137 handle = le16_to_cpu(expander_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307138 spin_lock_irqsave(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307139 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7140 ioc, le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307141 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307142 if (expander_device)
7143 _scsih_refresh_expander_links(ioc, expander_device,
7144 handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307145 else {
7146 printk(MPT2SAS_INFO_FMT "\tBEFORE adding expander: "
7147 "handle (0x%04x), sas_addr(0x%016llx)\n",
7148 ioc->name, handle, (unsigned long long)
7149 le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307150 _scsih_expander_add(ioc, handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307151 printk(MPT2SAS_INFO_FMT "\tAFTER adding expander: "
7152 "handle (0x%04x), sas_addr(0x%016llx)\n",
7153 ioc->name, handle, (unsigned long long)
7154 le64_to_cpu(expander_pg0.SASAddress));
7155 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307156 }
7157
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307158 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders complete\n",
7159 ioc->name);
7160
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307161 if (!ioc->ir_firmware)
7162 goto skip_to_sas;
7163
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307164 printk(MPT2SAS_INFO_FMT "\tscan devices phys disk start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307165 /* phys disk */
7166 phys_disk_num = 0xFF;
7167 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7168 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7169 phys_disk_num))) {
7170 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7171 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307172 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7173 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan:"
7174 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7175 ioc->name, ioc_status,
7176 le32_to_cpu(mpi_reply.IOCLogInfo));
7177 break;
7178 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307179 phys_disk_num = pd_pg0.PhysDiskNum;
7180 handle = le16_to_cpu(pd_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307181 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307182 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307183 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307184 if (sas_device)
7185 continue;
7186 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7187 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7188 handle) != 0)
7189 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307190 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7191 MPI2_IOCSTATUS_MASK;
7192 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7193 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan "
7194 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7195 ioc->name, ioc_status,
7196 le32_to_cpu(mpi_reply.IOCLogInfo));
7197 break;
7198 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307199 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7200 if (!_scsih_get_sas_address(ioc, parent_handle,
7201 &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307202 printk(MPT2SAS_INFO_FMT "\tBEFORE adding phys disk: "
7203 " handle (0x%04x), sas_addr(0x%016llx)\n",
7204 ioc->name, handle, (unsigned long long)
7205 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307206 mpt2sas_transport_update_links(ioc, sas_address,
7207 handle, sas_device_pg0.PhyNum,
7208 MPI2_SAS_NEG_LINK_RATE_1_5);
7209 set_bit(handle, ioc->pd_handles);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307210 retry_count = 0;
7211 /* This will retry adding the end device.
7212 * _scsih_add_device() will decide on retries and
7213 * return "1" when it should be retried
7214 */
7215 while (_scsih_add_device(ioc, handle, retry_count++,
7216 1)) {
7217 ssleep(1);
7218 }
7219 printk(MPT2SAS_INFO_FMT "\tAFTER adding phys disk: "
7220 " handle (0x%04x), sas_addr(0x%016llx)\n",
7221 ioc->name, handle, (unsigned long long)
7222 le64_to_cpu(sas_device_pg0.SASAddress));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307223 }
7224 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307225
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307226 printk(MPT2SAS_INFO_FMT "\tscan devices: phys disk complete\n",
7227 ioc->name);
7228
7229 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307230 /* volumes */
7231 handle = 0xFFFF;
7232 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7233 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7234 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7235 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307236 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7237 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7238 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7239 ioc->name, ioc_status,
7240 le32_to_cpu(mpi_reply.IOCLogInfo));
7241 break;
7242 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307243 handle = le16_to_cpu(volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307244 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307245 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7246 le64_to_cpu(volume_pg1.WWID));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307247 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307248 if (raid_device)
7249 continue;
7250 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7251 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7252 sizeof(Mpi2RaidVolPage0_t)))
7253 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307254 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7255 MPI2_IOCSTATUS_MASK;
7256 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7257 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7258 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7259 ioc->name, ioc_status,
7260 le32_to_cpu(mpi_reply.IOCLogInfo));
7261 break;
7262 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307263 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7264 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7265 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7266 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7267 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7268 element.VolDevHandle = volume_pg1.DevHandle;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307269 printk(MPT2SAS_INFO_FMT "\tBEFORE adding volume: "
7270 " handle (0x%04x)\n", ioc->name,
7271 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307272 _scsih_sas_volume_add(ioc, &element);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307273 printk(MPT2SAS_INFO_FMT "\tAFTER adding volume: "
7274 " handle (0x%04x)\n", ioc->name,
7275 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307276 }
7277 }
7278
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307279 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes complete\n",
7280 ioc->name);
7281
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307282 skip_to_sas:
7283
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307284 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices start\n",
7285 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307286 /* sas devices */
7287 handle = 0xFFFF;
7288 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7289 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7290 handle))) {
7291 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7292 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307293 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7294 printk(MPT2SAS_INFO_FMT "\tbreak from end device scan:"
7295 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7296 ioc->name, ioc_status,
7297 le32_to_cpu(mpi_reply.IOCLogInfo));
7298 break;
7299 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307300 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7301 if (!(_scsih_is_end_device(
7302 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7303 continue;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307304 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307305 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7306 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307307 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307308 if (sas_device)
7309 continue;
7310 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7311 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307312 printk(MPT2SAS_INFO_FMT "\tBEFORE adding end device: "
7313 "handle (0x%04x), sas_addr(0x%016llx)\n",
7314 ioc->name, handle, (unsigned long long)
7315 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307316 mpt2sas_transport_update_links(ioc, sas_address, handle,
7317 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307318 retry_count = 0;
7319 /* This will retry adding the end device.
7320 * _scsih_add_device() will decide on retries and
7321 * return "1" when it should be retried
7322 */
7323 while (_scsih_add_device(ioc, handle, retry_count++,
7324 0)) {
7325 ssleep(1);
7326 }
7327 printk(MPT2SAS_INFO_FMT "\tAFTER adding end device: "
7328 "handle (0x%04x), sas_addr(0x%016llx)\n",
7329 ioc->name, handle, (unsigned long long)
7330 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307331 }
7332 }
7333
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307334 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices complete\n",
7335 ioc->name);
7336
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307337 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307338}
7339
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307340
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307341/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307342 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7343 * @ioc: per adapter object
7344 * @reset_phase: phase
7345 *
7346 * The handler for doing any required cleanup or initialization.
7347 *
7348 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7349 * MPT2_IOC_DONE_RESET
7350 *
7351 * Return nothing.
7352 */
7353void
7354mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7355{
7356 switch (reset_phase) {
7357 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307358 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307359 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307360 break;
7361 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307362 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307363 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307364 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7365 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7366 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7367 complete(&ioc->scsih_cmds.done);
7368 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307369 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7370 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7371 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7372 complete(&ioc->tm_cmds.done);
7373 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307374 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307375 _scsih_flush_running_cmds(ioc);
7376 break;
7377 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307378 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307379 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307380 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307381 _scsih_prep_device_scan(ioc);
7382 _scsih_search_responding_sas_devices(ioc);
7383 _scsih_search_responding_raid_devices(ioc);
7384 _scsih_search_responding_expanders(ioc);
sreekanth.reddy@lsi.com14aa7f72012-07-17 15:56:13 +05307385 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7386 !ioc->sas_hba.num_phys)) {
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307387 _scsih_prep_device_scan(ioc);
7388 _scsih_search_responding_sas_devices(ioc);
7389 _scsih_search_responding_raid_devices(ioc);
7390 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307391 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307392 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307393 break;
Eric Moore635374e2009-03-09 01:21:12 -06007394 }
7395}
7396
7397/**
7398 * _firmware_event_work - delayed task for processing firmware events
7399 * @ioc: per adapter object
7400 * @work: equal to the fw_event_work object
7401 * Context: user.
7402 *
7403 * Return nothing.
7404 */
7405static void
7406_firmware_event_work(struct work_struct *work)
7407{
7408 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307409 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007410 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7411
Eric Moore635374e2009-03-09 01:21:12 -06007412 /* the queue is being flushed so ignore this event */
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05307413 if (ioc->remove_host ||
Eric Moore3cb54692010-07-08 14:44:34 -06007414 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007415 _scsih_fw_event_free(ioc, fw_event);
7416 return;
7417 }
Eric Moore635374e2009-03-09 01:21:12 -06007418
Eric Moore635374e2009-03-09 01:21:12 -06007419 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307420 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307421 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307422 ssleep(1);
7423 _scsih_remove_unresponding_sas_devices(ioc);
7424 _scsih_scan_for_devices_after_reset(ioc);
7425 break;
7426 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307427 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307428
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +05307429 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7430 mpt2sas_base_update_missing_delay(ioc, missing_delay[0],
7431 missing_delay[1]);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307432
7433 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7434 "from worker thread\n", ioc->name));
7435 break;
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05307436 case MPT2SAS_TURN_ON_PFA_LED:
7437 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307438 break;
Eric Moore635374e2009-03-09 01:21:12 -06007439 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307440 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007441 break;
7442 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307443 _scsih_sas_device_status_change_event(ioc,
7444 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007445 break;
7446 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307447 _scsih_sas_discovery_event(ioc,
7448 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007449 break;
7450 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Andrew Mortona78e21d2012-02-08 12:52:22 -08007451 _scsih_sas_broadcast_primitive_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307452 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007453 break;
7454 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7455 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307456 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007457 break;
7458 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307459 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007460 break;
7461 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307462 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007463 break;
7464 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307465 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007466 break;
7467 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307468 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007469 break;
Eric Moore635374e2009-03-09 01:21:12 -06007470 }
7471 _scsih_fw_event_free(ioc, fw_event);
7472}
7473
7474/**
7475 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7476 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307477 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007478 * @reply: reply message frame(lower 32bit addr)
7479 * Context: interrupt.
7480 *
7481 * This function merely adds a new work task into ioc->firmware_event_thread.
7482 * The tasks are worked from _firmware_event_work in user context.
7483 *
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307484 * Returns void.
Eric Moore635374e2009-03-09 01:21:12 -06007485 */
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307486void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307487mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7488 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007489{
7490 struct fw_event_work *fw_event;
7491 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007492 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307493 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007494
7495 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007496 if (ioc->remove_host || ioc->pci_error_recovery)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307497 return;
Eric Moore635374e2009-03-09 01:21:12 -06007498
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307499 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05307500
7501 if (unlikely(!mpi_reply)) {
7502 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7503 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307504 return;
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05307505 }
7506
Eric Moore635374e2009-03-09 01:21:12 -06007507 event = le16_to_cpu(mpi_reply->Event);
7508
7509 switch (event) {
7510 /* handle these */
7511 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7512 {
7513 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7514 (Mpi2EventDataSasBroadcastPrimitive_t *)
7515 mpi_reply->EventData;
7516
7517 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307518 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307519 return;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307520
7521 if (ioc->broadcast_aen_busy) {
7522 ioc->broadcast_aen_pending++;
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307523 return;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307524 } else
7525 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007526 break;
7527 }
7528
7529 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7530 _scsih_check_topo_delete_events(ioc,
7531 (Mpi2EventDataSasTopologyChangeList_t *)
7532 mpi_reply->EventData);
7533 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307534 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7535 _scsih_check_ir_config_unhide_events(ioc,
7536 (Mpi2EventDataIrConfigChangeList_t *)
7537 mpi_reply->EventData);
7538 break;
7539 case MPI2_EVENT_IR_VOLUME:
7540 _scsih_check_volume_delete_events(ioc,
7541 (Mpi2EventDataIrVolume_t *)
7542 mpi_reply->EventData);
7543 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307544 case MPI2_EVENT_LOG_ENTRY_ADDED:
7545 {
7546 Mpi2EventDataLogEntryAdded_t *log_entry;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307547 __le32 *log_code;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307548
7549 if (!ioc->is_warpdrive)
7550 break;
7551
7552 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7553 mpi_reply->EventData;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307554 log_code = (__le32 *)log_entry->LogData;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307555
7556 if (le16_to_cpu(log_entry->LogEntryQualifier)
7557 != MPT2_WARPDRIVE_LOGENTRY)
7558 break;
7559
7560 switch (le32_to_cpu(*log_code)) {
7561 case MPT2_WARPDRIVE_LC_SSDT:
7562 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7563 "IO Throttling has occurred in the WarpDrive "
7564 "subsystem. Check WarpDrive documentation for "
7565 "additional details.\n", ioc->name);
7566 break;
7567 case MPT2_WARPDRIVE_LC_SSDLW:
7568 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7569 "Program/Erase Cycles for the WarpDrive subsystem "
7570 "in degraded range. Check WarpDrive documentation "
7571 "for additional details.\n", ioc->name);
7572 break;
7573 case MPT2_WARPDRIVE_LC_SSDLF:
7574 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7575 "There are no Program/Erase Cycles for the "
7576 "WarpDrive subsystem. The storage device will be "
7577 "in read-only mode. Check WarpDrive documentation "
7578 "for additional details.\n", ioc->name);
7579 break;
7580 case MPT2_WARPDRIVE_LC_BRMF:
7581 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7582 "The Backup Rail Monitor has failed on the "
7583 "WarpDrive subsystem. Check WarpDrive "
7584 "documentation for additional details.\n",
7585 ioc->name);
7586 break;
7587 }
7588
7589 break;
7590 }
Eric Moore635374e2009-03-09 01:21:12 -06007591 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7592 case MPI2_EVENT_IR_OPERATION_STATUS:
7593 case MPI2_EVENT_SAS_DISCOVERY:
7594 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007595 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007596 break;
7597
Sreekanth Reddy2d8ce8c2015-01-12 11:38:56 +05307598 case MPI2_EVENT_TEMP_THRESHOLD:
7599 _scsih_temp_threshold_events(ioc,
7600 (Mpi2EventDataTemperature_t *)
7601 mpi_reply->EventData);
7602 break;
7603
Eric Moore635374e2009-03-09 01:21:12 -06007604 default: /* ignore the rest */
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307605 return;
Eric Moore635374e2009-03-09 01:21:12 -06007606 }
7607
Joe Lawrence00713ad2014-06-25 17:03:33 -04007608 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7609 fw_event = kzalloc(sizeof(*fw_event) + sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007610 if (!fw_event) {
7611 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7612 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307613 return;
Eric Moore635374e2009-03-09 01:21:12 -06007614 }
Eric Moore635374e2009-03-09 01:21:12 -06007615
Joe Lawrence00713ad2014-06-25 17:03:33 -04007616 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
Eric Moore635374e2009-03-09 01:21:12 -06007617 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307618 fw_event->VF_ID = mpi_reply->VF_ID;
7619 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007620 fw_event->event = event;
7621 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307622 return;
Eric Moore635374e2009-03-09 01:21:12 -06007623}
7624
7625/* shost template */
7626static struct scsi_host_template scsih_driver_template = {
7627 .module = THIS_MODULE,
7628 .name = "Fusion MPT SAS Host",
7629 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007630 .queuecommand = _scsih_qcmd,
7631 .target_alloc = _scsih_target_alloc,
7632 .slave_alloc = _scsih_slave_alloc,
7633 .slave_configure = _scsih_slave_configure,
7634 .target_destroy = _scsih_target_destroy,
7635 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307636 .scan_finished = _scsih_scan_finished,
7637 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007638 .change_queue_depth = _scsih_change_queue_depth,
Eric Moored5d135b2009-05-18 13:02:08 -06007639 .eh_abort_handler = _scsih_abort,
7640 .eh_device_reset_handler = _scsih_dev_reset,
7641 .eh_target_reset_handler = _scsih_target_reset,
7642 .eh_host_reset_handler = _scsih_host_reset,
7643 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007644 .can_queue = 1,
7645 .this_id = -1,
7646 .sg_tablesize = MPT2SAS_SG_DEPTH,
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05307647 .max_sectors = 32767,
Eric Moore635374e2009-03-09 01:21:12 -06007648 .cmd_per_lun = 7,
7649 .use_clustering = ENABLE_CLUSTERING,
7650 .shost_attrs = mpt2sas_host_attrs,
7651 .sdev_attrs = mpt2sas_dev_attrs,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007652 .track_queue_depth = 1,
Eric Moore635374e2009-03-09 01:21:12 -06007653};
7654
7655/**
7656 * _scsih_expander_node_remove - removing expander device from list.
7657 * @ioc: per adapter object
7658 * @sas_expander: the sas_device object
7659 * Context: Calling function should acquire ioc->sas_node_lock.
7660 *
7661 * Removing object and freeing associated memory from the
7662 * ioc->sas_expander_list.
7663 *
7664 * Return nothing.
7665 */
7666static void
7667_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7668 struct _sas_node *sas_expander)
7669{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307670 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007671
7672 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307673 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007674 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307675 if (ioc->shost_recovery)
7676 return;
Eric Moore635374e2009-03-09 01:21:12 -06007677 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307678 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307679 mpt2sas_device_remove_by_sas_address(ioc,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307680 mpt2sas_port->remote_identify.sas_address);
7681 else if (mpt2sas_port->remote_identify.device_type ==
7682 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007683 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307684 SAS_FANOUT_EXPANDER_DEVICE)
7685 mpt2sas_expander_remove(ioc,
7686 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007687 }
7688
7689 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307690 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007691
7692 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7693 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7694 sas_expander->handle, (unsigned long long)
7695 sas_expander->sas_address);
7696
Eric Moore635374e2009-03-09 01:21:12 -06007697 kfree(sas_expander->phy);
7698 kfree(sas_expander);
7699}
7700
7701/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307702 * _scsih_ir_shutdown - IR shutdown notification
7703 * @ioc: per adapter object
7704 *
7705 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7706 * the host system is shutting down.
7707 *
7708 * Return nothing.
7709 */
7710static void
7711_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7712{
7713 Mpi2RaidActionRequest_t *mpi_request;
7714 Mpi2RaidActionReply_t *mpi_reply;
7715 u16 smid;
7716
7717 /* is IR firmware build loaded ? */
7718 if (!ioc->ir_firmware)
7719 return;
7720
Kashyap, Desai744090d2009-10-05 15:56:56 +05307721 mutex_lock(&ioc->scsih_cmds.mutex);
7722
7723 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7724 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7725 ioc->name, __func__);
7726 goto out;
7727 }
7728 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7729
7730 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7731 if (!smid) {
7732 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7733 ioc->name, __func__);
7734 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7735 goto out;
7736 }
7737
7738 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7739 ioc->scsih_cmds.smid = smid;
7740 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7741
7742 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7743 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7744
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307745 if (!ioc->hide_ir_msg)
7746 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307747 init_completion(&ioc->scsih_cmds.done);
7748 mpt2sas_base_put_smid_default(ioc, smid);
7749 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7750
7751 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7752 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7753 ioc->name, __func__);
7754 goto out;
7755 }
7756
7757 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7758 mpi_reply = ioc->scsih_cmds.reply;
7759
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307760 if (!ioc->hide_ir_msg)
7761 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7762 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7763 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7764 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307765 }
7766
7767 out:
7768 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7769 mutex_unlock(&ioc->scsih_cmds.mutex);
7770}
7771
7772/**
7773 * _scsih_shutdown - routine call during system shutdown
7774 * @pdev: PCI device struct
7775 *
7776 * Return nothing.
7777 */
7778static void
7779_scsih_shutdown(struct pci_dev *pdev)
7780{
7781 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7782 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307783 struct workqueue_struct *wq;
7784 unsigned long flags;
7785
7786 ioc->remove_host = 1;
7787 _scsih_fw_event_cleanup_queue(ioc);
7788
7789 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7790 wq = ioc->firmware_event_thread;
7791 ioc->firmware_event_thread = NULL;
7792 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7793 if (wq)
7794 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307795
7796 _scsih_ir_shutdown(ioc);
7797 mpt2sas_base_detach(ioc);
7798}
7799
7800/**
Eric Moored5d135b2009-05-18 13:02:08 -06007801 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007802 * @pdev: PCI device struct
7803 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307804 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007805 * Return nothing.
7806 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007807static void
Eric Moored5d135b2009-05-18 13:02:08 -06007808_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007809{
7810 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7811 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307812 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307813 struct _raid_device *raid_device, *next;
7814 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007815 struct workqueue_struct *wq;
7816 unsigned long flags;
7817
7818 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307819 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007820
7821 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7822 wq = ioc->firmware_event_thread;
7823 ioc->firmware_event_thread = NULL;
7824 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7825 if (wq)
7826 destroy_workqueue(wq);
7827
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307828 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307829 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307830 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7831 list) {
7832 if (raid_device->starget) {
7833 sas_target_priv_data =
7834 raid_device->starget->hostdata;
7835 sas_target_priv_data->deleted = 1;
7836 scsi_remove_target(&raid_device->starget->dev);
7837 }
7838 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7839 "(0x%016llx)\n", ioc->name, raid_device->handle,
7840 (unsigned long long) raid_device->wwid);
7841 _scsih_raid_device_remove(ioc, raid_device);
7842 }
7843
Eric Moore635374e2009-03-09 01:21:12 -06007844 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307845 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007846 &ioc->sas_hba.sas_port_list, port_list) {
7847 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307848 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307849 mpt2sas_device_remove_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007850 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307851 else if (mpt2sas_port->remote_identify.device_type ==
7852 SAS_EDGE_EXPANDER_DEVICE ||
7853 mpt2sas_port->remote_identify.device_type ==
7854 SAS_FANOUT_EXPANDER_DEVICE)
7855 mpt2sas_expander_remove(ioc,
7856 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007857 }
7858
7859 /* free phys attached to the sas_host */
7860 if (ioc->sas_hba.num_phys) {
7861 kfree(ioc->sas_hba.phy);
7862 ioc->sas_hba.phy = NULL;
7863 ioc->sas_hba.num_phys = 0;
7864 }
7865
7866 sas_remove_host(shost);
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05307867 scsi_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307868 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007869 list_del(&ioc->list);
Eric Moore635374e2009-03-09 01:21:12 -06007870 scsi_host_put(shost);
7871}
7872
7873/**
7874 * _scsih_probe_boot_devices - reports 1st device
7875 * @ioc: per adapter object
7876 *
7877 * If specified in bios page 2, this routine reports the 1st
7878 * device scsi-ml or sas transport for persistent boot device
7879 * purposes. Please refer to function _scsih_determine_boot_device()
7880 */
7881static void
7882_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7883{
7884 u8 is_raid;
7885 void *device;
7886 struct _sas_device *sas_device;
7887 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307888 u16 handle;
7889 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007890 u64 sas_address;
7891 unsigned long flags;
7892 int rc;
7893
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307894 /* no Bios, return immediately */
7895 if (!ioc->bios_pg3.BiosVersion)
7896 return;
7897
Eric Moore635374e2009-03-09 01:21:12 -06007898 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307899 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007900 if (ioc->req_boot_device.device) {
7901 device = ioc->req_boot_device.device;
7902 is_raid = ioc->req_boot_device.is_raid;
7903 } else if (ioc->req_alt_boot_device.device) {
7904 device = ioc->req_alt_boot_device.device;
7905 is_raid = ioc->req_alt_boot_device.is_raid;
7906 } else if (ioc->current_boot_device.device) {
7907 device = ioc->current_boot_device.device;
7908 is_raid = ioc->current_boot_device.is_raid;
7909 }
7910
7911 if (!device)
7912 return;
7913
7914 if (is_raid) {
7915 raid_device = device;
7916 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7917 raid_device->id, 0);
7918 if (rc)
7919 _scsih_raid_device_remove(ioc, raid_device);
7920 } else {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307921 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007922 sas_device = device;
7923 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307924 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007925 sas_address = sas_device->sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06007926 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7927 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307928
7929 if (ioc->hide_drives)
7930 return;
Eric Moore635374e2009-03-09 01:21:12 -06007931 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307932 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007933 _scsih_sas_device_remove(ioc, sas_device);
7934 } else if (!sas_device->starget) {
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307935 if (!ioc->is_driver_loading) {
7936 mpt2sas_transport_port_remove(ioc,
7937 sas_address,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307938 sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307939 _scsih_sas_device_remove(ioc, sas_device);
7940 }
Eric Moore635374e2009-03-09 01:21:12 -06007941 }
7942 }
7943}
7944
7945/**
7946 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7947 * @ioc: per adapter object
7948 *
7949 * Called during initial loading of the driver.
7950 */
7951static void
7952_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7953{
7954 struct _raid_device *raid_device, *raid_next;
7955 int rc;
7956
7957 list_for_each_entry_safe(raid_device, raid_next,
7958 &ioc->raid_device_list, list) {
7959 if (raid_device->starget)
7960 continue;
7961 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7962 raid_device->id, 0);
7963 if (rc)
7964 _scsih_raid_device_remove(ioc, raid_device);
7965 }
7966}
7967
7968/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307969 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007970 * @ioc: per adapter object
7971 *
7972 * Called during initial loading of the driver.
7973 */
7974static void
7975_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7976{
7977 struct _sas_device *sas_device, *next;
7978 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007979
7980 /* SAS Device List */
7981 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7982 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007983
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307984 if (ioc->hide_drives)
7985 continue;
7986
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307987 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7988 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307989 list_del(&sas_device->list);
7990 kfree(sas_device);
7991 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007992 } else if (!sas_device->starget) {
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307993 if (!ioc->is_driver_loading) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307994 mpt2sas_transport_port_remove(ioc,
7995 sas_device->sas_address,
7996 sas_device->sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307997 list_del(&sas_device->list);
7998 kfree(sas_device);
7999 continue;
8000 }
Eric Moore635374e2009-03-09 01:21:12 -06008001 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05308002 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8003 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8004 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06008005 }
8006}
8007
8008/**
8009 * _scsih_probe_devices - probing for devices
8010 * @ioc: per adapter object
8011 *
8012 * Called during initial loading of the driver.
8013 */
8014static void
8015_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
8016{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308017 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06008018
8019 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8020 return; /* return when IOC doesn't support initiator mode */
8021
8022 _scsih_probe_boot_devices(ioc);
8023
8024 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308025 volume_mapping_flags =
8026 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8027 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8028 if (volume_mapping_flags ==
8029 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06008030 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308031 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008032 } else {
Eric Moore635374e2009-03-09 01:21:12 -06008033 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308034 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008035 }
8036 } else
8037 _scsih_probe_sas(ioc);
8038}
8039
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308040
8041/**
8042 * _scsih_scan_start - scsi lld callback for .scan_start
8043 * @shost: SCSI host pointer
8044 *
8045 * The shost has the ability to discover targets on its own instead
8046 * of scanning the entire bus. In our implemention, we will kick off
8047 * firmware discovery.
8048 */
8049static void
8050_scsih_scan_start(struct Scsi_Host *shost)
8051{
8052 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8053 int rc;
8054
8055 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8056 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
8057
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308058 if (disable_discovery > 0)
8059 return;
8060
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308061 ioc->start_scan = 1;
8062 rc = mpt2sas_port_enable(ioc);
8063
8064 if (rc != 0)
8065 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
8066}
8067
8068/**
8069 * _scsih_scan_finished - scsi lld callback for .scan_finished
8070 * @shost: SCSI host pointer
8071 * @time: elapsed time of the scan in jiffies
8072 *
8073 * This function will be called periodically until it returns 1 with the
8074 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8075 * we wait for firmware discovery to complete, then return 1.
8076 */
8077static int
8078_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8079{
8080 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8081
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308082 if (disable_discovery > 0) {
8083 ioc->is_driver_loading = 0;
8084 ioc->wait_for_discovery_to_complete = 0;
8085 return 1;
8086 }
8087
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308088 if (time >= (300 * HZ)) {
8089 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8090 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
8091 "(timeout=300s)\n", ioc->name);
8092 ioc->is_driver_loading = 0;
8093 return 1;
8094 }
8095
8096 if (ioc->start_scan)
8097 return 0;
8098
8099 if (ioc->start_scan_failed) {
8100 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
8101 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
8102 ioc->is_driver_loading = 0;
8103 ioc->wait_for_discovery_to_complete = 0;
8104 ioc->remove_host = 1;
8105 return 1;
8106 }
8107
8108 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
8109 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8110
8111 if (ioc->wait_for_discovery_to_complete) {
8112 ioc->wait_for_discovery_to_complete = 0;
8113 _scsih_probe_devices(ioc);
8114 }
8115 mpt2sas_base_start_watchdog(ioc);
8116 ioc->is_driver_loading = 0;
8117 return 1;
8118}
8119
8120
Eric Moore635374e2009-03-09 01:21:12 -06008121/**
Eric Moored5d135b2009-05-18 13:02:08 -06008122 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06008123 * @pdev: PCI device struct
8124 * @id: pci device id
8125 *
8126 * Returns 0 success, anything else error.
8127 */
8128static int
Eric Moored5d135b2009-05-18 13:02:08 -06008129_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06008130{
8131 struct MPT2SAS_ADAPTER *ioc;
8132 struct Scsi_Host *shost;
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308133 int rv;
Eric Moore635374e2009-03-09 01:21:12 -06008134
8135 shost = scsi_host_alloc(&scsih_driver_template,
8136 sizeof(struct MPT2SAS_ADAPTER));
8137 if (!shost)
8138 return -ENODEV;
8139
8140 /* init local params */
8141 ioc = shost_priv(shost);
8142 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
8143 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06008144 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06008145 ioc->shost = shost;
8146 ioc->id = mpt_ids++;
8147 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
8148 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308149 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8150 ioc->is_warpdrive = 1;
8151 ioc->hide_ir_msg = 1;
8152 } else
8153 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06008154 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8155 ioc->tm_cb_idx = tm_cb_idx;
8156 ioc->ctl_cb_idx = ctl_cb_idx;
8157 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308158 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008159 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05308160 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008161 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308162 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308163 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308164 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008165 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05308166 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06008167 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05308168 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06008169 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8170 spin_lock_init(&ioc->scsi_lookup_lock);
8171 spin_lock_init(&ioc->sas_device_lock);
8172 spin_lock_init(&ioc->sas_node_lock);
8173 spin_lock_init(&ioc->fw_event_lock);
8174 spin_lock_init(&ioc->raid_device_lock);
8175
8176 INIT_LIST_HEAD(&ioc->sas_device_list);
8177 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8178 INIT_LIST_HEAD(&ioc->sas_expander_list);
8179 INIT_LIST_HEAD(&ioc->fw_event_list);
8180 INIT_LIST_HEAD(&ioc->raid_device_list);
8181 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308182 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308183 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04008184 INIT_LIST_HEAD(&ioc->reply_queue_list);
Eric Moore635374e2009-03-09 01:21:12 -06008185
8186 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06008187 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06008188 shost->max_lun = max_lun;
8189 shost->transportt = mpt2sas_transport_template;
8190 shost->unique_id = ioc->id;
8191
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308192 if (max_sectors != 0xFFFF) {
8193 if (max_sectors < 64) {
8194 shost->max_sectors = 64;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05308195 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8196 "for max_sectors, range is 64 to 32767. Assigning "
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308197 "value of 64.\n", ioc->name, max_sectors);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308198 } else if (max_sectors > 32767) {
8199 shost->max_sectors = 32767;
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308200 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8201 "for max_sectors, range is 64 to 8192. Assigning "
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308202 "default value of 32767.\n", ioc->name,
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308203 max_sectors);
8204 } else {
8205 shost->max_sectors = max_sectors & 0xFFFE;
8206 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
8207 "set to %d\n", ioc->name, shost->max_sectors);
8208 }
8209 }
8210
Martin K. Petersen5e95e732012-08-28 14:29:37 -04008211 /* register EEDP capabilities with SCSI layer */
8212 if (prot_mask)
8213 scsi_host_set_prot(shost, prot_mask);
8214 else
8215 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8216 | SHOST_DIF_TYPE2_PROTECTION
8217 | SHOST_DIF_TYPE3_PROTECTION);
8218
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308219 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06008220
Eric Moore635374e2009-03-09 01:21:12 -06008221 /* event thread */
8222 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8223 "fw_event%d", ioc->id);
8224 ioc->firmware_event_thread = create_singlethread_workqueue(
8225 ioc->firmware_event_name);
8226 if (!ioc->firmware_event_thread) {
8227 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8228 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308229 rv = -ENODEV;
Eric Moore635374e2009-03-09 01:21:12 -06008230 goto out_thread_fail;
8231 }
8232
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308233 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008234 if ((mpt2sas_base_attach(ioc))) {
8235 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8236 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308237 rv = -ENODEV;
Eric Moore635374e2009-03-09 01:21:12 -06008238 goto out_attach_fail;
8239 }
8240
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308241 if (ioc->is_warpdrive) {
8242 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8243 ioc->hide_drives = 0;
8244 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8245 ioc->hide_drives = 1;
8246 else {
8247 if (_scsih_get_num_volumes(ioc))
8248 ioc->hide_drives = 1;
8249 else
8250 ioc->hide_drives = 0;
8251 }
8252 } else
8253 ioc->hide_drives = 0;
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05308254
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308255 rv = scsi_add_host(shost, &pdev->dev);
8256 if (rv) {
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05308257 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8258 ioc->name, __FILE__, __LINE__, __func__);
8259 goto out_add_shost_fail;
8260 }
8261
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05308262 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308263
Eric Moore635374e2009-03-09 01:21:12 -06008264 return 0;
8265
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05308266 out_add_shost_fail:
8267 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008268 out_attach_fail:
8269 destroy_workqueue(ioc->firmware_event_thread);
8270 out_thread_fail:
8271 list_del(&ioc->list);
Tomas Henzlf555e052012-04-17 15:24:48 +02008272 scsi_host_put(shost);
Sreekanth Reddyb65f1d42014-09-12 15:35:33 +05308273 return rv;
Eric Moore635374e2009-03-09 01:21:12 -06008274}
8275
8276#ifdef CONFIG_PM
8277/**
Eric Moored5d135b2009-05-18 13:02:08 -06008278 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008279 * @pdev: PCI device struct
8280 * @state: PM state change to (usually PCI_D3)
8281 *
8282 * Returns 0 success, anything else error.
8283 */
8284static int
Eric Moored5d135b2009-05-18 13:02:08 -06008285_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008286{
8287 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8288 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308289 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008290
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308291 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008292 scsi_block_requests(shost);
Sreekanth Reddy3627dba2013-07-25 11:25:57 +05308293 _scsih_ir_shutdown(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008294 device_state = pci_choose_state(pdev, state);
8295 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8296 "operating state [D%d]\n", ioc->name, pdev,
8297 pci_name(pdev), device_state);
8298
8299 mpt2sas_base_free_resources(ioc);
8300 pci_save_state(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06008301 pci_set_power_state(pdev, device_state);
8302 return 0;
8303}
8304
8305/**
Eric Moored5d135b2009-05-18 13:02:08 -06008306 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008307 * @pdev: PCI device struct
8308 *
8309 * Returns 0 success, anything else error.
8310 */
8311static int
Eric Moored5d135b2009-05-18 13:02:08 -06008312_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008313{
8314 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8315 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308316 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008317 int r;
8318
8319 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8320 "operating state [D%d]\n", ioc->name, pdev,
8321 pci_name(pdev), device_state);
8322
8323 pci_set_power_state(pdev, PCI_D0);
8324 pci_enable_wake(pdev, PCI_D0, 0);
8325 pci_restore_state(pdev);
8326 ioc->pdev = pdev;
8327 r = mpt2sas_base_map_resources(ioc);
8328 if (r)
8329 return r;
8330
8331 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8332 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308333 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008334 return 0;
8335}
8336#endif /* CONFIG_PM */
8337
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308338/**
8339 * _scsih_pci_error_detected - Called when a PCI error is detected.
8340 * @pdev: PCI device struct
8341 * @state: PCI channel state
8342 *
8343 * Description: Called when a PCI error is detected.
8344 *
8345 * Return value:
8346 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8347 */
8348static pci_ers_result_t
8349_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8350{
8351 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8352 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8353
8354 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8355 ioc->name, state);
8356
8357 switch (state) {
8358 case pci_channel_io_normal:
8359 return PCI_ERS_RESULT_CAN_RECOVER;
8360 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008361 /* Fatal error, prepare for slot reset */
8362 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308363 scsi_block_requests(ioc->shost);
8364 mpt2sas_base_stop_watchdog(ioc);
8365 mpt2sas_base_free_resources(ioc);
8366 return PCI_ERS_RESULT_NEED_RESET;
8367 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008368 /* Permanent error, prepare for device removal */
8369 ioc->pci_error_recovery = 1;
8370 mpt2sas_base_stop_watchdog(ioc);
8371 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308372 return PCI_ERS_RESULT_DISCONNECT;
8373 }
8374 return PCI_ERS_RESULT_NEED_RESET;
8375}
8376
8377/**
8378 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8379 * @pdev: PCI device struct
8380 *
8381 * Description: This routine is called by the pci error recovery
8382 * code after the PCI slot has been reset, just before we
8383 * should resume normal operations.
8384 */
8385static pci_ers_result_t
8386_scsih_pci_slot_reset(struct pci_dev *pdev)
8387{
8388 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8389 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8390 int rc;
8391
8392 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8393 ioc->name);
8394
Eric Moore3cb54692010-07-08 14:44:34 -06008395 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308396 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008397 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308398 rc = mpt2sas_base_map_resources(ioc);
8399 if (rc)
8400 return PCI_ERS_RESULT_DISCONNECT;
8401
8402
8403 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8404 FORCE_BIG_HAMMER);
8405
8406 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8407 (rc == 0) ? "success" : "failed");
8408
8409 if (!rc)
8410 return PCI_ERS_RESULT_RECOVERED;
8411 else
8412 return PCI_ERS_RESULT_DISCONNECT;
8413}
8414
8415/**
8416 * _scsih_pci_resume() - resume normal ops after PCI reset
8417 * @pdev: pointer to PCI device
8418 *
8419 * Called when the error recovery driver tells us that its
8420 * OK to resume normal operation. Use completion to allow
8421 * halted scsi ops to resume.
8422 */
8423static void
8424_scsih_pci_resume(struct pci_dev *pdev)
8425{
8426 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8427 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8428
8429 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8430
8431 pci_cleanup_aer_uncorrect_error_status(pdev);
8432 mpt2sas_base_start_watchdog(ioc);
8433 scsi_unblock_requests(ioc->shost);
8434}
8435
8436/**
8437 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8438 * @pdev: pointer to PCI device
8439 */
8440static pci_ers_result_t
8441_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8442{
8443 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8444 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8445
8446 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8447 ioc->name);
8448
8449 /* TODO - dump whatever for debugging purposes */
8450
8451 /* Request a slot reset. */
8452 return PCI_ERS_RESULT_NEED_RESET;
8453}
8454
Stephen Hemmingera55b2d22012-09-07 09:33:16 -07008455static const struct pci_error_handlers _scsih_err_handler = {
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308456 .error_detected = _scsih_pci_error_detected,
8457 .mmio_enabled = _scsih_pci_mmio_enabled,
8458 .slot_reset = _scsih_pci_slot_reset,
8459 .resume = _scsih_pci_resume,
8460};
Eric Moore635374e2009-03-09 01:21:12 -06008461
8462static struct pci_driver scsih_driver = {
8463 .name = MPT2SAS_DRIVER_NAME,
8464 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008465 .probe = _scsih_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008466 .remove = _scsih_remove,
Kashyap, Desai744090d2009-10-05 15:56:56 +05308467 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308468 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008469#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008470 .suspend = _scsih_suspend,
8471 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008472#endif
8473};
8474
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308475/* raid transport support */
8476static struct raid_function_template mpt2sas_raid_functions = {
8477 .cookie = &scsih_driver_template,
8478 .is_raid = _scsih_is_raid,
8479 .get_resync = _scsih_get_resync,
8480 .get_state = _scsih_get_state,
8481};
Eric Moore635374e2009-03-09 01:21:12 -06008482
8483/**
Eric Moored5d135b2009-05-18 13:02:08 -06008484 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008485 *
8486 * Returns 0 success, anything else error.
8487 */
8488static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008489_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008490{
8491 int error;
8492
8493 mpt_ids = 0;
8494 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8495 MPT2SAS_DRIVER_VERSION);
8496
8497 mpt2sas_transport_template =
8498 sas_attach_transport(&mpt2sas_transport_functions);
8499 if (!mpt2sas_transport_template)
8500 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308501 /* raid transport support */
8502 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8503 if (!mpt2sas_raid_template) {
8504 sas_release_transport(mpt2sas_transport_template);
8505 return -ENODEV;
8506 }
Eric Moore635374e2009-03-09 01:21:12 -06008507
8508 mpt2sas_base_initialize_callback_handler();
8509
8510 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008511 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008512
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008513 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008514 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008515
8516 /* base internal commands callback handler */
8517 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308518 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8519 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008520
8521 /* transport internal commands callback handler */
8522 transport_cb_idx = mpt2sas_base_register_callback_handler(
8523 mpt2sas_transport_done);
8524
Kashyap, Desai744090d2009-10-05 15:56:56 +05308525 /* scsih internal commands callback handler */
8526 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8527
Eric Moore635374e2009-03-09 01:21:12 -06008528 /* configuration page API internal commands callback handler */
8529 config_cb_idx = mpt2sas_base_register_callback_handler(
8530 mpt2sas_config_done);
8531
8532 /* ctl module callback handler */
8533 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8534
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308535 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8536 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308537
8538 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8539 _scsih_tm_volume_tr_complete);
8540
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308541 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8542 _scsih_sas_control_complete);
8543
Eric Moore635374e2009-03-09 01:21:12 -06008544 mpt2sas_ctl_init();
8545
8546 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308547 if (error) {
8548 /* raid transport support */
8549 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008550 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308551 }
Eric Moore635374e2009-03-09 01:21:12 -06008552
8553 return error;
8554}
8555
8556/**
Eric Moored5d135b2009-05-18 13:02:08 -06008557 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008558 *
8559 * Returns 0 success, anything else error.
8560 */
8561static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008562_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008563{
8564 printk(KERN_INFO "mpt2sas version %s unloading\n",
8565 MPT2SAS_DRIVER_VERSION);
8566
8567 pci_unregister_driver(&scsih_driver);
8568
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308569 mpt2sas_ctl_exit();
8570
Eric Moore635374e2009-03-09 01:21:12 -06008571 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8572 mpt2sas_base_release_callback_handler(tm_cb_idx);
8573 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308574 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008575 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308576 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008577 mpt2sas_base_release_callback_handler(config_cb_idx);
8578 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8579
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308580 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308581 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308582 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8583
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308584 /* raid transport support */
8585 raid_class_release(mpt2sas_raid_template);
8586 sas_release_transport(mpt2sas_transport_template);
8587
Eric Moore635374e2009-03-09 01:21:12 -06008588}
8589
Eric Moored5d135b2009-05-18 13:02:08 -06008590module_init(_scsih_init);
8591module_exit(_scsih_exit);