blob: e7303edc3d8ef837111d3369360f0e8c806045f6 [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
58#include "mpt2sas_base.h"
59
60MODULE_AUTHOR(MPT2SAS_AUTHOR);
61MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
62MODULE_LICENSE("GPL");
63MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
64
65#define RAID_CHANNEL 1
66
67/* forward proto's */
68static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
69 struct _sas_node *sas_expander);
70static void _firmware_event_work(struct work_struct *work);
71
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053072static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
73
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053074static void _scsih_scan_start(struct Scsi_Host *shost);
75static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
76
Eric Moore635374e2009-03-09 01:21:12 -060077/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060078LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060079
80/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060081static u8 scsi_io_cb_idx = -1;
82static u8 tm_cb_idx = -1;
83static u8 ctl_cb_idx = -1;
84static u8 base_cb_idx = -1;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053085static u8 port_enable_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060086static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053087static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060088static u8 config_cb_idx = -1;
89static int mpt_ids;
90
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053091static u8 tm_tr_cb_idx = -1 ;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053092static u8 tm_tr_volume_cb_idx = -1 ;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053093static u8 tm_sas_control_cb_idx = -1;
94
Eric Moore635374e2009-03-09 01:21:12 -060095/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060096static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060097MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
98 "(default=0)");
99
Kashyap, Desaia3e1e552011-06-14 10:56:12 +0530100static ushort max_sectors = 0xFFFF;
101module_param(max_sectors, ushort, 0);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +0530102MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
Kashyap, Desaia3e1e552011-06-14 10:56:12 +0530103
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +0530104static int missing_delay[2] = {-1, -1};
105module_param_array(missing_delay, int, NULL, 0);
106MODULE_PARM_DESC(missing_delay, " device missing delay , io missing delay");
107
Eric Moore635374e2009-03-09 01:21:12 -0600108/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
109#define MPT2SAS_MAX_LUN (16895)
110static int max_lun = MPT2SAS_MAX_LUN;
111module_param(max_lun, int, 0);
112MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
113
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530114/* diag_buffer_enable is bitwise
115 * bit 0 set = TRACE
116 * bit 1 set = SNAPSHOT
117 * bit 2 set = EXTENDED
118 *
119 * Either bit can be set, or both
120 */
121static int diag_buffer_enable = -1;
122module_param(diag_buffer_enable, int, 0);
123MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
124 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
125
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +0530126static int disable_discovery = -1;
127module_param(disable_discovery, int, 0);
128MODULE_PARM_DESC(disable_discovery, " disable discovery ");
129
Martin K. Petersen5e95e732012-08-28 14:29:37 -0400130/* permit overriding the host protection capabilities mask (EEDP/T10 PI) */
131static int prot_mask = 0;
132module_param(prot_mask, int, 0);
133MODULE_PARM_DESC(prot_mask, " host protection capabilities mask, def=7 ");
134
Eric Moore635374e2009-03-09 01:21:12 -0600135/**
136 * struct sense_info - common structure for obtaining sense keys
137 * @skey: sense key
138 * @asc: additional sense code
139 * @ascq: additional sense code qualifier
140 */
141struct sense_info {
142 u8 skey;
143 u8 asc;
144 u8 ascq;
145};
146
147
Sreekanth Reddybd58ea32014-09-12 15:35:19 +0530148#define MPT2SAS_TURN_ON_PFA_LED (0xFFFC)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530149#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
150#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
Eric Moore635374e2009-03-09 01:21:12 -0600151/**
152 * struct fw_event_work - firmware event struct
153 * @list: link list framework
154 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530155 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600156 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530157 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600158 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530159 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600160 * @ignore: flag meaning this event has been marked to ignore
161 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
162 * @event_data: reply event data payload follows
163 *
164 * This object stored on ioc->fw_event_list.
165 */
166struct fw_event_work {
167 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530168 u8 cancel_pending_work;
169 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600170 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530171 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600172 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530173 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600174 u8 ignore;
175 u16 event;
Joe Lawrence00713ad2014-06-25 17:03:33 -0400176 char event_data[0] __aligned(4);
Eric Moore635374e2009-03-09 01:21:12 -0600177};
178
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530179/* raid transport support */
180static struct raid_template *mpt2sas_raid_template;
181
Eric Moore635374e2009-03-09 01:21:12 -0600182/**
183 * struct _scsi_io_transfer - scsi io transfer
184 * @handle: sas device handle (assigned by firmware)
185 * @is_raid: flag set for hidden raid components
186 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
187 * @data_length: data transfer length
188 * @data_dma: dma pointer to data
189 * @sense: sense data
190 * @lun: lun number
191 * @cdb_length: cdb length
192 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600193 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530194 * @VF_ID: virtual function id
195 * @VP_ID: virtual port id
196 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600197 * @sense_length: sense length
198 * @ioc_status: ioc status
199 * @scsi_state: scsi state
200 * @scsi_status: scsi staus
201 * @log_info: log information
202 * @transfer_length: data length transfer when there is a reply message
203 *
204 * Used for sending internal scsi commands to devices within this module.
205 * Refer to _scsi_send_scsi_io().
206 */
207struct _scsi_io_transfer {
208 u16 handle;
209 u8 is_raid;
210 enum dma_data_direction dir;
211 u32 data_length;
212 dma_addr_t data_dma;
213 u8 sense[SCSI_SENSE_BUFFERSIZE];
214 u32 lun;
215 u8 cdb_length;
216 u8 cdb[32];
217 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530218 u8 VF_ID;
219 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600220 u8 valid_reply;
221 /* the following bits are only valid when 'valid_reply = 1' */
222 u32 sense_length;
223 u16 ioc_status;
224 u8 scsi_state;
225 u8 scsi_status;
226 u32 log_info;
227 u32 transfer_length;
228};
229
230/*
231 * The pci device ids are defined in mpi/mpi2_cnfg.h.
232 */
233static struct pci_device_id scsih_pci_table[] = {
234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
235 PCI_ANY_ID, PCI_ANY_ID },
236 /* Falcon ~ 2008*/
237 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
238 PCI_ANY_ID, PCI_ANY_ID },
239 /* Liberator ~ 2108 */
240 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
241 PCI_ANY_ID, PCI_ANY_ID },
242 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
243 PCI_ANY_ID, PCI_ANY_ID },
244 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
245 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530246 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600247 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
248 PCI_ANY_ID, PCI_ANY_ID },
249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
250 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530251 /* Thunderbolt ~ 2208 */
252 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
253 PCI_ANY_ID, PCI_ANY_ID },
254 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
255 PCI_ANY_ID, PCI_ANY_ID },
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
257 PCI_ANY_ID, PCI_ANY_ID },
258 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
259 PCI_ANY_ID, PCI_ANY_ID },
260 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
261 PCI_ANY_ID, PCI_ANY_ID },
262 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
263 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530264 /* Mustang ~ 2308 */
265 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530266 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530267 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
268 PCI_ANY_ID, PCI_ANY_ID },
269 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530270 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530271 /* SSS6200 */
272 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
273 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600274 {0} /* Terminating entry */
275};
276MODULE_DEVICE_TABLE(pci, scsih_pci_table);
277
278/**
Eric Moored5d135b2009-05-18 13:02:08 -0600279 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600280 *
281 * Note: The logging levels are defined in mpt2sas_debug.h.
282 */
283static int
Eric Moored5d135b2009-05-18 13:02:08 -0600284_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600285{
286 int ret = param_set_int(val, kp);
287 struct MPT2SAS_ADAPTER *ioc;
288
289 if (ret)
290 return ret;
291
292 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600293 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600294 ioc->logging_level = logging_level;
295 return 0;
296}
Eric Moored5d135b2009-05-18 13:02:08 -0600297module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600298 &logging_level, 0644);
299
300/**
301 * _scsih_srch_boot_sas_address - search based on sas_address
302 * @sas_address: sas address
303 * @boot_device: boot device object from bios page 2
304 *
305 * Returns 1 when there's a match, 0 means no match.
306 */
307static inline int
308_scsih_srch_boot_sas_address(u64 sas_address,
309 Mpi2BootDeviceSasWwid_t *boot_device)
310{
311 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
312}
313
314/**
315 * _scsih_srch_boot_device_name - search based on device name
316 * @device_name: device name specified in INDENTIFY fram
317 * @boot_device: boot device object from bios page 2
318 *
319 * Returns 1 when there's a match, 0 means no match.
320 */
321static inline int
322_scsih_srch_boot_device_name(u64 device_name,
323 Mpi2BootDeviceDeviceName_t *boot_device)
324{
325 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
326}
327
328/**
329 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
330 * @enclosure_logical_id: enclosure logical id
331 * @slot_number: slot number
332 * @boot_device: boot device object from bios page 2
333 *
334 * Returns 1 when there's a match, 0 means no match.
335 */
336static inline int
337_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
338 Mpi2BootDeviceEnclosureSlot_t *boot_device)
339{
340 return (enclosure_logical_id == le64_to_cpu(boot_device->
341 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
342 SlotNumber)) ? 1 : 0;
343}
344
345/**
346 * _scsih_is_boot_device - search for matching boot device.
347 * @sas_address: sas address
348 * @device_name: device name specified in INDENTIFY fram
349 * @enclosure_logical_id: enclosure logical id
350 * @slot_number: slot number
351 * @form: specifies boot device form
352 * @boot_device: boot device object from bios page 2
353 *
354 * Returns 1 when there's a match, 0 means no match.
355 */
356static int
357_scsih_is_boot_device(u64 sas_address, u64 device_name,
358 u64 enclosure_logical_id, u16 slot, u8 form,
359 Mpi2BiosPage2BootDevice_t *boot_device)
360{
361 int rc = 0;
362
363 switch (form) {
364 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
365 if (!sas_address)
366 break;
367 rc = _scsih_srch_boot_sas_address(
368 sas_address, &boot_device->SasWwid);
369 break;
370 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
371 if (!enclosure_logical_id)
372 break;
373 rc = _scsih_srch_boot_encl_slot(
374 enclosure_logical_id,
375 slot, &boot_device->EnclosureSlot);
376 break;
377 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
378 if (!device_name)
379 break;
380 rc = _scsih_srch_boot_device_name(
381 device_name, &boot_device->DeviceName);
382 break;
383 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
384 break;
385 }
386
387 return rc;
388}
389
390/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530391 * _scsih_get_sas_address - set the sas_address for given device handle
392 * @handle: device handle
393 * @sas_address: sas address
394 *
395 * Returns 0 success, non-zero when failure
396 */
397static int
398_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
399 u64 *sas_address)
400{
401 Mpi2SasDevicePage0_t sas_device_pg0;
402 Mpi2ConfigReply_t mpi_reply;
403 u32 ioc_status;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530404 *sas_address = 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530405
406 if (handle <= ioc->sas_hba.num_phys) {
407 *sas_address = ioc->sas_hba.sas_address;
408 return 0;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530409 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530410
411 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
412 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530413 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
414 __FILE__, __LINE__, __func__);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530415 return -ENXIO;
416 }
417
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530418 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
419 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
420 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
421 return 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530422 }
423
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530424 /* we hit this becuase the given parent handle doesn't exist */
425 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
426 return -ENXIO;
427 /* else error case */
428 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
429 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
430 __FILE__, __LINE__, __func__);
431 return -EIO;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530432}
433
434/**
Eric Moore635374e2009-03-09 01:21:12 -0600435 * _scsih_determine_boot_device - determine boot device.
436 * @ioc: per adapter object
437 * @device: either sas_device or raid_device object
438 * @is_raid: [flag] 1 = raid object, 0 = sas object
439 *
440 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300441 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600442 * There are primary, alternate, and current entries in bios page 2. The order
443 * priority is primary, alternate, then current. This routine saves
444 * the corresponding device object and is_raid flag in the ioc object.
445 * The saved data to be used later in _scsih_probe_boot_devices().
446 */
447static void
448_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
449 void *device, u8 is_raid)
450{
451 struct _sas_device *sas_device;
452 struct _raid_device *raid_device;
453 u64 sas_address;
454 u64 device_name;
455 u64 enclosure_logical_id;
456 u16 slot;
457
458 /* only process this function when driver loads */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530459 if (!ioc->is_driver_loading)
460 return;
461
462 /* no Bios, return immediately */
463 if (!ioc->bios_pg3.BiosVersion)
Eric Moore635374e2009-03-09 01:21:12 -0600464 return;
465
466 if (!is_raid) {
467 sas_device = device;
468 sas_address = sas_device->sas_address;
469 device_name = sas_device->device_name;
470 enclosure_logical_id = sas_device->enclosure_logical_id;
471 slot = sas_device->slot;
472 } else {
473 raid_device = device;
474 sas_address = raid_device->wwid;
475 device_name = 0;
476 enclosure_logical_id = 0;
477 slot = 0;
478 }
479
480 if (!ioc->req_boot_device.device) {
481 if (_scsih_is_boot_device(sas_address, device_name,
482 enclosure_logical_id, slot,
483 (ioc->bios_pg2.ReqBootDeviceForm &
484 MPI2_BIOSPAGE2_FORM_MASK),
485 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530486 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600487 "%s: req_boot_device(0x%016llx)\n",
488 ioc->name, __func__,
489 (unsigned long long)sas_address));
490 ioc->req_boot_device.device = device;
491 ioc->req_boot_device.is_raid = is_raid;
492 }
493 }
494
495 if (!ioc->req_alt_boot_device.device) {
496 if (_scsih_is_boot_device(sas_address, device_name,
497 enclosure_logical_id, slot,
498 (ioc->bios_pg2.ReqAltBootDeviceForm &
499 MPI2_BIOSPAGE2_FORM_MASK),
500 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530501 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600502 "%s: req_alt_boot_device(0x%016llx)\n",
503 ioc->name, __func__,
504 (unsigned long long)sas_address));
505 ioc->req_alt_boot_device.device = device;
506 ioc->req_alt_boot_device.is_raid = is_raid;
507 }
508 }
509
510 if (!ioc->current_boot_device.device) {
511 if (_scsih_is_boot_device(sas_address, device_name,
512 enclosure_logical_id, slot,
513 (ioc->bios_pg2.CurrentBootDeviceForm &
514 MPI2_BIOSPAGE2_FORM_MASK),
515 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530516 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600517 "%s: current_boot_device(0x%016llx)\n",
518 ioc->name, __func__,
519 (unsigned long long)sas_address));
520 ioc->current_boot_device.device = device;
521 ioc->current_boot_device.is_raid = is_raid;
522 }
523 }
524}
525
526/**
527 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
528 * @ioc: per adapter object
529 * @sas_address: sas address
530 * Context: Calling function should acquire ioc->sas_device_lock
531 *
532 * This searches for sas_device based on sas_address, then return sas_device
533 * object.
534 */
535struct _sas_device *
536mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
537 u64 sas_address)
538{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530539 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600540
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530541 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
542 if (sas_device->sas_address == sas_address)
543 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600544
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530545 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
546 if (sas_device->sas_address == sas_address)
547 return sas_device;
548
549 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600550}
551
552/**
553 * _scsih_sas_device_find_by_handle - sas device search
554 * @ioc: per adapter object
555 * @handle: sas device handle (assigned by firmware)
556 * Context: Calling function should acquire ioc->sas_device_lock
557 *
558 * This searches for sas_device based on sas_address, then return sas_device
559 * object.
560 */
561static struct _sas_device *
562_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
563{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530564 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600565
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530566 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
567 if (sas_device->handle == handle)
568 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600569
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530570 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
571 if (sas_device->handle == handle)
572 return sas_device;
573
574 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600575}
576
577/**
578 * _scsih_sas_device_remove - remove sas_device from list.
579 * @ioc: per adapter object
580 * @sas_device: the sas_device object
581 * Context: This function will acquire ioc->sas_device_lock.
582 *
583 * Removing object and freeing associated memory from the ioc->sas_device_list.
584 */
585static void
586_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
587 struct _sas_device *sas_device)
588{
589 unsigned long flags;
590
Kashyap, Desai980ead32010-04-08 17:55:22 +0530591 if (!sas_device)
592 return;
593
Eric Moore635374e2009-03-09 01:21:12 -0600594 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530595 list_del(&sas_device->list);
596 kfree(sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600597 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
598}
599
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530600
Eric Moore635374e2009-03-09 01:21:12 -0600601/**
602 * _scsih_sas_device_add - insert sas_device to the list.
603 * @ioc: per adapter object
604 * @sas_device: the sas_device object
605 * Context: This function will acquire ioc->sas_device_lock.
606 *
607 * Adding new object to the ioc->sas_device_list.
608 */
609static void
610_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
611 struct _sas_device *sas_device)
612{
613 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600614
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530615 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600616 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
617 sas_device->handle, (unsigned long long)sas_device->sas_address));
618
619 spin_lock_irqsave(&ioc->sas_device_lock, flags);
620 list_add_tail(&sas_device->list, &ioc->sas_device_list);
621 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
622
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530623 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530624 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -0600625 _scsih_sas_device_remove(ioc, sas_device);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530626 } else if (!sas_device->starget) {
627 /* When asyn scanning is enabled, its not possible to remove
628 * devices while scanning is turned on due to an oops in
629 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
630 */
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +0530631 if (!ioc->is_driver_loading) {
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530632 mpt2sas_transport_port_remove(ioc,
633 sas_device->sas_address,
634 sas_device->sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +0530635 _scsih_sas_device_remove(ioc, sas_device);
636 }
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530637 }
Eric Moore635374e2009-03-09 01:21:12 -0600638}
639
640/**
641 * _scsih_sas_device_init_add - insert sas_device to the list.
642 * @ioc: per adapter object
643 * @sas_device: the sas_device object
644 * Context: This function will acquire ioc->sas_device_lock.
645 *
646 * Adding new object at driver load time to the ioc->sas_device_init_list.
647 */
648static void
649_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
650 struct _sas_device *sas_device)
651{
652 unsigned long flags;
653
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530654 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600655 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
656 sas_device->handle, (unsigned long long)sas_device->sas_address));
657
658 spin_lock_irqsave(&ioc->sas_device_lock, flags);
659 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
Eric Moore635374e2009-03-09 01:21:12 -0600660 _scsih_determine_boot_device(ioc, sas_device, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530661 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600662}
663
664/**
Eric Moore635374e2009-03-09 01:21:12 -0600665 * _scsih_raid_device_find_by_id - raid device search
666 * @ioc: per adapter object
667 * @id: sas device target id
668 * @channel: sas device channel
669 * Context: Calling function should acquire ioc->raid_device_lock
670 *
671 * This searches for raid_device based on target id, then return raid_device
672 * object.
673 */
674static struct _raid_device *
675_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
676{
677 struct _raid_device *raid_device, *r;
678
679 r = NULL;
680 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
681 if (raid_device->id == id && raid_device->channel == channel) {
682 r = raid_device;
683 goto out;
684 }
685 }
686
687 out:
688 return r;
689}
690
691/**
692 * _scsih_raid_device_find_by_handle - raid device search
693 * @ioc: per adapter object
694 * @handle: sas device handle (assigned by firmware)
695 * Context: Calling function should acquire ioc->raid_device_lock
696 *
697 * This searches for raid_device based on handle, then return raid_device
698 * object.
699 */
700static struct _raid_device *
701_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
702{
703 struct _raid_device *raid_device, *r;
704
705 r = NULL;
706 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
707 if (raid_device->handle != handle)
708 continue;
709 r = raid_device;
710 goto out;
711 }
712
713 out:
714 return r;
715}
716
717/**
718 * _scsih_raid_device_find_by_wwid - raid device search
719 * @ioc: per adapter object
720 * @handle: sas device handle (assigned by firmware)
721 * Context: Calling function should acquire ioc->raid_device_lock
722 *
723 * This searches for raid_device based on wwid, then return raid_device
724 * object.
725 */
726static struct _raid_device *
727_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
728{
729 struct _raid_device *raid_device, *r;
730
731 r = NULL;
732 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
733 if (raid_device->wwid != wwid)
734 continue;
735 r = raid_device;
736 goto out;
737 }
738
739 out:
740 return r;
741}
742
743/**
744 * _scsih_raid_device_add - add raid_device object
745 * @ioc: per adapter object
746 * @raid_device: raid_device object
747 *
748 * This is added to the raid_device_list link list.
749 */
750static void
751_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
752 struct _raid_device *raid_device)
753{
754 unsigned long flags;
755
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530756 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600757 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
758 raid_device->handle, (unsigned long long)raid_device->wwid));
759
760 spin_lock_irqsave(&ioc->raid_device_lock, flags);
761 list_add_tail(&raid_device->list, &ioc->raid_device_list);
762 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
763}
764
765/**
766 * _scsih_raid_device_remove - delete raid_device object
767 * @ioc: per adapter object
768 * @raid_device: raid_device object
769 *
Eric Moore635374e2009-03-09 01:21:12 -0600770 */
771static void
772_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
773 struct _raid_device *raid_device)
774{
775 unsigned long flags;
776
777 spin_lock_irqsave(&ioc->raid_device_lock, flags);
778 list_del(&raid_device->list);
Eric Moore635374e2009-03-09 01:21:12 -0600779 kfree(raid_device);
780 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
781}
782
783/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530784 * mpt2sas_scsih_expander_find_by_handle - expander device search
785 * @ioc: per adapter object
786 * @handle: expander handle (assigned by firmware)
787 * Context: Calling function should acquire ioc->sas_device_lock
788 *
789 * This searches for expander device based on handle, then returns the
790 * sas_node object.
791 */
792struct _sas_node *
793mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
794{
795 struct _sas_node *sas_expander, *r;
796
797 r = NULL;
798 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
799 if (sas_expander->handle != handle)
800 continue;
801 r = sas_expander;
802 goto out;
803 }
804 out:
805 return r;
806}
807
808/**
Eric Moore635374e2009-03-09 01:21:12 -0600809 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
810 * @ioc: per adapter object
811 * @sas_address: sas address
812 * Context: Calling function should acquire ioc->sas_node_lock.
813 *
814 * This searches for expander device based on sas_address, then returns the
815 * sas_node object.
816 */
817struct _sas_node *
818mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
819 u64 sas_address)
820{
821 struct _sas_node *sas_expander, *r;
822
823 r = NULL;
824 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
825 if (sas_expander->sas_address != sas_address)
826 continue;
827 r = sas_expander;
828 goto out;
829 }
830 out:
831 return r;
832}
833
834/**
835 * _scsih_expander_node_add - insert expander device to the list.
836 * @ioc: per adapter object
837 * @sas_expander: the sas_device object
838 * Context: This function will acquire ioc->sas_node_lock.
839 *
840 * Adding new object to the ioc->sas_expander_list.
841 *
842 * Return nothing.
843 */
844static void
845_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
846 struct _sas_node *sas_expander)
847{
848 unsigned long flags;
849
850 spin_lock_irqsave(&ioc->sas_node_lock, flags);
851 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
852 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
853}
854
855/**
856 * _scsih_is_end_device - determines if device is an end device
857 * @device_info: bitfield providing information about the device.
858 * Context: none
859 *
860 * Returns 1 if end device.
861 */
862static int
863_scsih_is_end_device(u32 device_info)
864{
865 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
866 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
867 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
868 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
869 return 1;
870 else
871 return 0;
872}
873
874/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530875 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600876 * @ioc: per adapter object
877 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600878 *
879 * Returns the smid stored scmd pointer.
880 */
881static struct scsi_cmnd *
882_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
883{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530884 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600885}
886
887/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530888 * _scsih_scsi_lookup_get_clear - returns scmd entry
889 * @ioc: per adapter object
890 * @smid: system request message index
891 *
892 * Returns the smid stored scmd pointer.
893 * Then will derefrence the stored scmd pointer.
894 */
895static inline struct scsi_cmnd *
896_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
897{
898 unsigned long flags;
899 struct scsi_cmnd *scmd;
900
901 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
902 scmd = ioc->scsi_lookup[smid - 1].scmd;
903 ioc->scsi_lookup[smid - 1].scmd = NULL;
904 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
905
906 return scmd;
907}
908
909/**
Eric Moore635374e2009-03-09 01:21:12 -0600910 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
911 * @ioc: per adapter object
912 * @smid: system request message index
913 * @scmd: pointer to scsi command object
914 * Context: This function will acquire ioc->scsi_lookup_lock.
915 *
916 * This will search for a scmd pointer in the scsi_lookup array,
917 * returning the revelent smid. A returned value of zero means invalid.
918 */
919static u16
920_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
921 *scmd)
922{
923 u16 smid;
924 unsigned long flags;
925 int i;
926
927 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
928 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530929 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600930 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530931 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600932 goto out;
933 }
934 }
935 out:
936 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
937 return smid;
938}
939
940/**
941 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
942 * @ioc: per adapter object
943 * @id: target id
944 * @channel: channel
945 * Context: This function will acquire ioc->scsi_lookup_lock.
946 *
947 * This will search for a matching channel:id in the scsi_lookup array,
948 * returning 1 if found.
949 */
950static u8
951_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
952 int channel)
953{
954 u8 found;
955 unsigned long flags;
956 int i;
957
958 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
959 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530960 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600961 if (ioc->scsi_lookup[i].scmd &&
962 (ioc->scsi_lookup[i].scmd->device->id == id &&
963 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
964 found = 1;
965 goto out;
966 }
967 }
968 out:
969 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
970 return found;
971}
972
973/**
Eric Moore993e0da2009-05-18 13:00:45 -0600974 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
975 * @ioc: per adapter object
976 * @id: target id
977 * @lun: lun number
978 * @channel: channel
979 * Context: This function will acquire ioc->scsi_lookup_lock.
980 *
981 * This will search for a matching channel:id:lun in the scsi_lookup array,
982 * returning 1 if found.
983 */
984static u8
985_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
986 unsigned int lun, int channel)
987{
988 u8 found;
989 unsigned long flags;
990 int i;
991
992 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
993 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530994 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600995 if (ioc->scsi_lookup[i].scmd &&
996 (ioc->scsi_lookup[i].scmd->device->id == id &&
997 ioc->scsi_lookup[i].scmd->device->channel == channel &&
998 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
999 found = 1;
1000 goto out;
1001 }
1002 }
1003 out:
1004 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1005 return found;
1006}
1007
1008/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301009 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -06001010 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301011 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -06001012 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301013 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001014 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301015static struct chain_tracker *
1016_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001017{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301018 struct chain_tracker *chain_req;
1019 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001020
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301021 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1022 if (list_empty(&ioc->free_chain_list)) {
1023 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05301024 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
1025 "available\n", ioc->name));
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301026 return NULL;
1027 }
1028 chain_req = list_entry(ioc->free_chain_list.next,
1029 struct chain_tracker, tracker_list);
1030 list_del_init(&chain_req->tracker_list);
1031 list_add_tail(&chain_req->tracker_list,
1032 &ioc->scsi_lookup[smid - 1].chain_list);
1033 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1034 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001035}
1036
1037/**
1038 * _scsih_build_scatter_gather - main sg creation routine
1039 * @ioc: per adapter object
1040 * @scmd: scsi command
1041 * @smid: system request message index
1042 * Context: none.
1043 *
1044 * The main routine that builds scatter gather table from a given
1045 * scsi request sent via the .queuecommand main handler.
1046 *
1047 * Returns 0 success, anything else error
1048 */
1049static int
1050_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1051 struct scsi_cmnd *scmd, u16 smid)
1052{
1053 Mpi2SCSIIORequest_t *mpi_request;
1054 dma_addr_t chain_dma;
1055 struct scatterlist *sg_scmd;
1056 void *sg_local, *chain;
1057 u32 chain_offset;
1058 u32 chain_length;
1059 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001060 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001061 u32 sges_in_segment;
1062 u32 sgl_flags;
1063 u32 sgl_flags_last_element;
1064 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301065 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001066
1067 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1068
1069 /* init scatter gather flags */
1070 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1071 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1072 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1073 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1074 << MPI2_SGE_FLAGS_SHIFT;
1075 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1076 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1077 << MPI2_SGE_FLAGS_SHIFT;
1078 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1079
1080 sg_scmd = scsi_sglist(scmd);
1081 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001082 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001083 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1084 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1085 return -ENOMEM;
1086 }
1087
1088 sg_local = &mpi_request->SGL;
1089 sges_in_segment = ioc->max_sges_in_main_message;
1090 if (sges_left <= sges_in_segment)
1091 goto fill_in_last_segment;
1092
1093 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1094 (sges_in_segment * ioc->sge_size))/4;
1095
1096 /* fill in main message segment when there is a chain following */
1097 while (sges_in_segment) {
1098 if (sges_in_segment == 1)
1099 ioc->base_add_sg_single(sg_local,
1100 sgl_flags_last_element | sg_dma_len(sg_scmd),
1101 sg_dma_address(sg_scmd));
1102 else
1103 ioc->base_add_sg_single(sg_local, sgl_flags |
1104 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1105 sg_scmd = sg_next(sg_scmd);
1106 sg_local += ioc->sge_size;
1107 sges_left--;
1108 sges_in_segment--;
1109 }
1110
1111 /* initializing the chain flags and pointers */
1112 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301113 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1114 if (!chain_req)
1115 return -1;
1116 chain = chain_req->chain_buffer;
1117 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001118 do {
1119 sges_in_segment = (sges_left <=
1120 ioc->max_sges_in_chain_message) ? sges_left :
1121 ioc->max_sges_in_chain_message;
1122 chain_offset = (sges_left == sges_in_segment) ?
1123 0 : (sges_in_segment * ioc->sge_size)/4;
1124 chain_length = sges_in_segment * ioc->sge_size;
1125 if (chain_offset) {
1126 chain_offset = chain_offset <<
1127 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1128 chain_length += ioc->sge_size;
1129 }
1130 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1131 chain_length, chain_dma);
1132 sg_local = chain;
1133 if (!chain_offset)
1134 goto fill_in_last_segment;
1135
1136 /* fill in chain segments */
1137 while (sges_in_segment) {
1138 if (sges_in_segment == 1)
1139 ioc->base_add_sg_single(sg_local,
1140 sgl_flags_last_element |
1141 sg_dma_len(sg_scmd),
1142 sg_dma_address(sg_scmd));
1143 else
1144 ioc->base_add_sg_single(sg_local, sgl_flags |
1145 sg_dma_len(sg_scmd),
1146 sg_dma_address(sg_scmd));
1147 sg_scmd = sg_next(sg_scmd);
1148 sg_local += ioc->sge_size;
1149 sges_left--;
1150 sges_in_segment--;
1151 }
1152
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301153 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1154 if (!chain_req)
1155 return -1;
1156 chain = chain_req->chain_buffer;
1157 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001158 } while (1);
1159
1160
1161 fill_in_last_segment:
1162
1163 /* fill the last segment */
1164 while (sges_left) {
1165 if (sges_left == 1)
1166 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1167 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1168 else
1169 ioc->base_add_sg_single(sg_local, sgl_flags |
1170 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1171 sg_scmd = sg_next(sg_scmd);
1172 sg_local += ioc->sge_size;
1173 sges_left--;
1174 }
1175
1176 return 0;
1177}
1178
1179/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301180 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001181 * @sdev: scsi device struct
1182 * @qdepth: requested queue depth
1183 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301184 *
1185 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001186 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301187static void
1188_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001189{
1190 struct Scsi_Host *shost = sdev->host;
1191 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301192 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1193 struct MPT2SAS_DEVICE *sas_device_priv_data;
1194 struct MPT2SAS_TARGET *sas_target_priv_data;
1195 struct _sas_device *sas_device;
1196 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001197
1198 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301199
1200 /* limit max device queue for SATA to 32 */
1201 sas_device_priv_data = sdev->hostdata;
1202 if (!sas_device_priv_data)
1203 goto not_sata;
1204 sas_target_priv_data = sas_device_priv_data->sas_target;
1205 if (!sas_target_priv_data)
1206 goto not_sata;
1207 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1208 goto not_sata;
1209 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1210 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1211 sas_device_priv_data->sas_target->sas_address);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301212 if (sas_device && sas_device->device_info &
1213 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1214 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301215 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301216
1217 not_sata:
1218
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;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301223 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1224}
1225
1226/**
1227 * _scsih_change_queue_depth - setting device queue depth
1228 * @sdev: scsi device struct
1229 * @qdepth: requested queue depth
1230 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1231 * (see include/scsi/scsi_host.h for definition)
1232 *
1233 * Returns queue depth.
1234 */
1235static int
1236_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1237{
1238 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1239 _scsih_adjust_queue_depth(sdev, qdepth);
1240 else if (reason == SCSI_QDEPTH_QFULL)
1241 scsi_track_queue_full(sdev, qdepth);
1242 else
1243 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001244
1245 if (sdev->inquiry_len > 7)
1246 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1247 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1248 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1249 sdev->ordered_tags, sdev->scsi_level,
1250 (sdev->inquiry[7] & 2) >> 1);
1251
1252 return sdev->queue_depth;
1253}
1254
1255/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301256 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001257 * @sdev: scsi device struct
1258 * @tag_type: requested tag type
1259 *
1260 * Returns queue tag type.
1261 */
1262static int
Eric Moored5d135b2009-05-18 13:02:08 -06001263_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001264{
1265 if (sdev->tagged_supported) {
1266 scsi_set_tag_type(sdev, tag_type);
1267 if (tag_type)
1268 scsi_activate_tcq(sdev, sdev->queue_depth);
1269 else
1270 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1271 } else
1272 tag_type = 0;
1273
1274 return tag_type;
1275}
1276
1277/**
Eric Moored5d135b2009-05-18 13:02:08 -06001278 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001279 * @starget: scsi target struct
1280 *
1281 * Returns 0 if ok. Any other return is assumed to be an error and
1282 * the device is ignored.
1283 */
1284static int
Eric Moored5d135b2009-05-18 13:02:08 -06001285_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001286{
1287 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1288 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1289 struct MPT2SAS_TARGET *sas_target_priv_data;
1290 struct _sas_device *sas_device;
1291 struct _raid_device *raid_device;
1292 unsigned long flags;
1293 struct sas_rphy *rphy;
1294
Joe Lawrencefeafe7c2014-06-25 17:03:14 -04001295 sas_target_priv_data = kzalloc(sizeof(*sas_target_priv_data),
1296 GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06001297 if (!sas_target_priv_data)
1298 return -ENOMEM;
1299
1300 starget->hostdata = sas_target_priv_data;
1301 sas_target_priv_data->starget = starget;
1302 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1303
1304 /* RAID volumes */
1305 if (starget->channel == RAID_CHANNEL) {
1306 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1307 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1308 starget->channel);
1309 if (raid_device) {
1310 sas_target_priv_data->handle = raid_device->handle;
1311 sas_target_priv_data->sas_address = raid_device->wwid;
1312 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301313 if (ioc->is_warpdrive)
1314 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001315 raid_device->starget = starget;
1316 }
1317 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1318 return 0;
1319 }
1320
1321 /* sas/sata devices */
1322 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1323 rphy = dev_to_rphy(starget->dev.parent);
1324 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1325 rphy->identify.sas_address);
1326
1327 if (sas_device) {
1328 sas_target_priv_data->handle = sas_device->handle;
1329 sas_target_priv_data->sas_address = sas_device->sas_address;
1330 sas_device->starget = starget;
1331 sas_device->id = starget->id;
1332 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301333 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001334 sas_target_priv_data->flags |=
1335 MPT_TARGET_FLAGS_RAID_COMPONENT;
1336 }
1337 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1338
1339 return 0;
1340}
1341
1342/**
Eric Moored5d135b2009-05-18 13:02:08 -06001343 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001344 * @starget: scsi target struct
1345 *
1346 * Returns nothing.
1347 */
1348static void
Eric Moored5d135b2009-05-18 13:02:08 -06001349_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001350{
1351 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1352 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1353 struct MPT2SAS_TARGET *sas_target_priv_data;
1354 struct _sas_device *sas_device;
1355 struct _raid_device *raid_device;
1356 unsigned long flags;
1357 struct sas_rphy *rphy;
1358
1359 sas_target_priv_data = starget->hostdata;
1360 if (!sas_target_priv_data)
1361 return;
1362
1363 if (starget->channel == RAID_CHANNEL) {
1364 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1365 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1366 starget->channel);
1367 if (raid_device) {
1368 raid_device->starget = NULL;
1369 raid_device->sdev = NULL;
1370 }
1371 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1372 goto out;
1373 }
1374
1375 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1376 rphy = dev_to_rphy(starget->dev.parent);
1377 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1378 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001379 if (sas_device && (sas_device->starget == starget) &&
1380 (sas_device->id == starget->id) &&
1381 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001382 sas_device->starget = NULL;
1383
1384 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1385
1386 out:
1387 kfree(sas_target_priv_data);
1388 starget->hostdata = NULL;
1389}
1390
1391/**
Eric Moored5d135b2009-05-18 13:02:08 -06001392 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001393 * @sdev: scsi device struct
1394 *
1395 * Returns 0 if ok. Any other return is assumed to be an error and
1396 * the device is ignored.
1397 */
1398static int
Eric Moored5d135b2009-05-18 13:02:08 -06001399_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001400{
1401 struct Scsi_Host *shost;
1402 struct MPT2SAS_ADAPTER *ioc;
1403 struct MPT2SAS_TARGET *sas_target_priv_data;
1404 struct MPT2SAS_DEVICE *sas_device_priv_data;
1405 struct scsi_target *starget;
1406 struct _raid_device *raid_device;
Sreekanth Reddy63e359d2013-07-25 11:32:51 +05301407 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -06001408 unsigned long flags;
1409
Joe Lawrencefeafe7c2014-06-25 17:03:14 -04001410 sas_device_priv_data = kzalloc(sizeof(*sas_device_priv_data),
1411 GFP_KERNEL);
Eric Moore635374e2009-03-09 01:21:12 -06001412 if (!sas_device_priv_data)
1413 return -ENOMEM;
1414
1415 sas_device_priv_data->lun = sdev->lun;
1416 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1417
1418 starget = scsi_target(sdev);
1419 sas_target_priv_data = starget->hostdata;
1420 sas_target_priv_data->num_luns++;
1421 sas_device_priv_data->sas_target = sas_target_priv_data;
1422 sdev->hostdata = sas_device_priv_data;
1423 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1424 sdev->no_uld_attach = 1;
1425
1426 shost = dev_to_shost(&starget->dev);
1427 ioc = shost_priv(shost);
1428 if (starget->channel == RAID_CHANNEL) {
1429 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1430 raid_device = _scsih_raid_device_find_by_id(ioc,
1431 starget->id, starget->channel);
1432 if (raid_device)
1433 raid_device->sdev = sdev; /* raid is single lun */
1434 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001435 }
1436
Sreekanth Reddy63e359d2013-07-25 11:32:51 +05301437 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1438 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1439 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1440 sas_target_priv_data->sas_address);
1441 if (sas_device && (sas_device->starget == NULL)) {
1442 sdev_printk(KERN_INFO, sdev,
1443 "%s : sas_device->starget set to starget @ %d\n",
1444 __func__, __LINE__);
1445 sas_device->starget = starget;
1446 }
1447 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1448 }
1449
Eric Moore635374e2009-03-09 01:21:12 -06001450 return 0;
1451}
1452
1453/**
Eric Moored5d135b2009-05-18 13:02:08 -06001454 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001455 * @sdev: scsi device struct
1456 *
1457 * Returns nothing.
1458 */
1459static void
Eric Moored5d135b2009-05-18 13:02:08 -06001460_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001461{
1462 struct MPT2SAS_TARGET *sas_target_priv_data;
1463 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301464 struct Scsi_Host *shost;
1465 struct MPT2SAS_ADAPTER *ioc;
1466 struct _sas_device *sas_device;
1467 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001468
1469 if (!sdev->hostdata)
1470 return;
1471
1472 starget = scsi_target(sdev);
1473 sas_target_priv_data = starget->hostdata;
1474 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301475
1476 shost = dev_to_shost(&starget->dev);
1477 ioc = shost_priv(shost);
1478
1479 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1480 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1481 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1482 sas_target_priv_data->sas_address);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +05301483 if (sas_device && !sas_target_priv_data->num_luns)
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301484 sas_device->starget = NULL;
1485 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1486 }
1487
Eric Moore635374e2009-03-09 01:21:12 -06001488 kfree(sdev->hostdata);
1489 sdev->hostdata = NULL;
1490}
1491
1492/**
Eric Moored5d135b2009-05-18 13:02:08 -06001493 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001494 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301495 * @handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -06001496 * @sdev: scsi device struct
1497 */
1498static void
Eric Moored5d135b2009-05-18 13:02:08 -06001499_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301500 u16 handle, struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001501{
1502 Mpi2ConfigReply_t mpi_reply;
1503 Mpi2SasDevicePage0_t sas_device_pg0;
1504 u32 ioc_status;
1505 u16 flags;
1506 u32 device_info;
1507
1508 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301509 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06001510 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1511 ioc->name, __FILE__, __LINE__, __func__);
1512 return;
1513 }
1514
1515 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1516 MPI2_IOCSTATUS_MASK;
1517 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1518 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1519 ioc->name, __FILE__, __LINE__, __func__);
1520 return;
1521 }
1522
1523 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301524 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001525
1526 sdev_printk(KERN_INFO, sdev,
1527 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1528 "sw_preserve(%s)\n",
1529 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1530 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1531 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1532 "n",
1533 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1534 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1535 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1536}
1537
1538/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301539 * _scsih_is_raid - return boolean indicating device is raid volume
1540 * @dev the device struct object
1541 */
1542static int
1543_scsih_is_raid(struct device *dev)
1544{
1545 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301546 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301547
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301548 if (ioc->is_warpdrive)
1549 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301550 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1551}
1552
1553/**
1554 * _scsih_get_resync - get raid volume resync percent complete
1555 * @dev the device struct object
1556 */
1557static void
1558_scsih_get_resync(struct device *dev)
1559{
1560 struct scsi_device *sdev = to_scsi_device(dev);
1561 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1562 static struct _raid_device *raid_device;
1563 unsigned long flags;
1564 Mpi2RaidVolPage0_t vol_pg0;
1565 Mpi2ConfigReply_t mpi_reply;
1566 u32 volume_status_flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301567 u8 percent_complete;
1568 u16 handle;
1569
1570 percent_complete = 0;
1571 handle = 0;
1572 if (ioc->is_warpdrive)
1573 goto out;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301574
1575 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1576 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1577 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301578 if (raid_device) {
1579 handle = raid_device->handle;
1580 percent_complete = raid_device->percent_complete;
1581 }
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301582 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1583
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301584 if (!handle)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301585 goto out;
1586
1587 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301588 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301589 sizeof(Mpi2RaidVolPage0_t))) {
1590 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1591 ioc->name, __FILE__, __LINE__, __func__);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301592 percent_complete = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301593 goto out;
1594 }
1595
1596 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301597 if (!(volume_status_flags &
1598 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1599 percent_complete = 0;
1600
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301601 out:
1602 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1603}
1604
1605/**
1606 * _scsih_get_state - get raid volume level
1607 * @dev the device struct object
1608 */
1609static void
1610_scsih_get_state(struct device *dev)
1611{
1612 struct scsi_device *sdev = to_scsi_device(dev);
1613 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1614 static struct _raid_device *raid_device;
1615 unsigned long flags;
1616 Mpi2RaidVolPage0_t vol_pg0;
1617 Mpi2ConfigReply_t mpi_reply;
1618 u32 volstate;
1619 enum raid_state state = RAID_STATE_UNKNOWN;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301620 u16 handle = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301621
1622 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1623 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1624 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301625 if (raid_device)
1626 handle = raid_device->handle;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301627 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1628
1629 if (!raid_device)
1630 goto out;
1631
1632 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301633 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301634 sizeof(Mpi2RaidVolPage0_t))) {
1635 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1636 ioc->name, __FILE__, __LINE__, __func__);
1637 goto out;
1638 }
1639
1640 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1641 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1642 state = RAID_STATE_RESYNCING;
1643 goto out;
1644 }
1645
1646 switch (vol_pg0.VolumeState) {
1647 case MPI2_RAID_VOL_STATE_OPTIMAL:
1648 case MPI2_RAID_VOL_STATE_ONLINE:
1649 state = RAID_STATE_ACTIVE;
1650 break;
1651 case MPI2_RAID_VOL_STATE_DEGRADED:
1652 state = RAID_STATE_DEGRADED;
1653 break;
1654 case MPI2_RAID_VOL_STATE_FAILED:
1655 case MPI2_RAID_VOL_STATE_MISSING:
1656 state = RAID_STATE_OFFLINE;
1657 break;
1658 }
1659 out:
1660 raid_set_state(mpt2sas_raid_template, dev, state);
1661}
1662
1663/**
1664 * _scsih_set_level - set raid level
1665 * @sdev: scsi device struct
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301666 * @volume_type: volume type
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301667 */
1668static void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301669_scsih_set_level(struct scsi_device *sdev, u8 volume_type)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301670{
1671 enum raid_level level = RAID_LEVEL_UNKNOWN;
1672
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301673 switch (volume_type) {
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301674 case MPI2_RAID_VOL_TYPE_RAID0:
1675 level = RAID_LEVEL_0;
1676 break;
1677 case MPI2_RAID_VOL_TYPE_RAID10:
1678 level = RAID_LEVEL_10;
1679 break;
1680 case MPI2_RAID_VOL_TYPE_RAID1E:
1681 level = RAID_LEVEL_1E;
1682 break;
1683 case MPI2_RAID_VOL_TYPE_RAID1:
1684 level = RAID_LEVEL_1;
1685 break;
1686 }
1687
1688 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1689}
1690
1691/**
Eric Moore635374e2009-03-09 01:21:12 -06001692 * _scsih_get_volume_capabilities - volume capabilities
1693 * @ioc: per adapter object
1694 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301695 *
1696 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001697 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301698static int
Eric Moore635374e2009-03-09 01:21:12 -06001699_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1700 struct _raid_device *raid_device)
1701{
1702 Mpi2RaidVolPage0_t *vol_pg0;
1703 Mpi2RaidPhysDiskPage0_t pd_pg0;
1704 Mpi2SasDevicePage0_t sas_device_pg0;
1705 Mpi2ConfigReply_t mpi_reply;
1706 u16 sz;
1707 u8 num_pds;
1708
1709 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1710 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301711 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1712 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1713 __func__));
1714 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001715 }
1716
1717 raid_device->num_pds = num_pds;
1718 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1719 sizeof(Mpi2RaidVol0PhysDisk_t));
1720 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1721 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301722 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1723 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1724 __func__));
1725 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001726 }
1727
1728 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1729 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301730 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1731 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1732 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001733 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301734 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001735 }
1736
1737 raid_device->volume_type = vol_pg0->VolumeType;
1738
1739 /* figure out what the underlying devices are by
1740 * obtaining the device_info bits for the 1st device
1741 */
1742 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1743 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1744 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1745 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1746 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1747 le16_to_cpu(pd_pg0.DevHandle)))) {
1748 raid_device->device_info =
1749 le32_to_cpu(sas_device_pg0.DeviceInfo);
1750 }
1751 }
1752
1753 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301754 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001755}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301756/**
1757 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1758 * @ioc: per adapter object
1759 */
1760static void
1761_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1762{
1763 Mpi2RaidVolPage1_t vol_pg1;
1764 Mpi2ConfigReply_t mpi_reply;
1765 struct _raid_device *raid_device;
1766 u16 handle;
1767 u16 ioc_status;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301768 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301769
1770 handle = 0xFFFF;
1771 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1772 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1773 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1774 MPI2_IOCSTATUS_MASK;
1775 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1776 break;
1777 handle = le16_to_cpu(vol_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301778 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301779 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1780 if (raid_device)
1781 raid_device->direct_io_enabled = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301782 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301783 }
1784 return;
1785}
1786
1787
1788/**
1789 * _scsih_get_num_volumes - Get number of volumes in the ioc
1790 * @ioc: per adapter object
1791 */
1792static u8
1793_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1794{
1795 Mpi2RaidVolPage1_t vol_pg1;
1796 Mpi2ConfigReply_t mpi_reply;
1797 u16 handle;
1798 u8 vol_cnt = 0;
1799 u16 ioc_status;
1800
1801 handle = 0xFFFF;
1802 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1803 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1804 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1805 MPI2_IOCSTATUS_MASK;
1806 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1807 break;
1808 vol_cnt++;
1809 handle = le16_to_cpu(vol_pg1.DevHandle);
1810 }
1811 return vol_cnt;
1812}
1813
1814
1815/**
1816 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1817 * @ioc: per adapter object
1818 * @raid_device: the raid_device object
1819 */
1820static void
1821_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1822 struct _raid_device *raid_device)
1823{
1824 Mpi2RaidVolPage0_t *vol_pg0;
1825 Mpi2RaidPhysDiskPage0_t pd_pg0;
1826 Mpi2ConfigReply_t mpi_reply;
1827 u16 sz;
1828 u8 num_pds, count;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301829 unsigned long stripe_sz, block_sz;
1830 u8 stripe_exp, block_exp;
1831 u64 dev_max_lba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301832
1833 if (!ioc->is_warpdrive)
1834 return;
1835
1836 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1837 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1838 "globally as drives are exposed\n", ioc->name);
1839 return;
1840 }
1841 if (_scsih_get_num_volumes(ioc) > 1) {
1842 _scsih_disable_ddio(ioc);
1843 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1844 "globally as number of drives > 1\n", ioc->name);
1845 return;
1846 }
1847 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1848 &num_pds)) || !num_pds) {
1849 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1850 "Failure in computing number of drives\n", ioc->name);
1851 return;
1852 }
1853
1854 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1855 sizeof(Mpi2RaidVol0PhysDisk_t));
1856 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1857 if (!vol_pg0) {
1858 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1859 "Memory allocation failure for RVPG0\n", ioc->name);
1860 return;
1861 }
1862
1863 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1864 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1865 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1866 "Failure in retrieving RVPG0\n", ioc->name);
1867 kfree(vol_pg0);
1868 return;
1869 }
1870
1871 /*
1872 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1873 * assumed for WARPDRIVE, disable direct I/O
1874 */
1875 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1876 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1877 "for the drive with handle(0x%04x): num_mem=%d, "
1878 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1879 num_pds, MPT_MAX_WARPDRIVE_PDS);
1880 kfree(vol_pg0);
1881 return;
1882 }
1883 for (count = 0; count < num_pds; count++) {
1884 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1885 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1886 vol_pg0->PhysDisk[count].PhysDiskNum) ||
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05301887 le16_to_cpu(pd_pg0.DevHandle) ==
1888 MPT2SAS_INVALID_DEVICE_HANDLE) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301889 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1890 "disabled for the drive with handle(0x%04x) member"
1891 "handle retrieval failed for member number=%d\n",
1892 ioc->name, raid_device->handle,
1893 vol_pg0->PhysDisk[count].PhysDiskNum);
1894 goto out_error;
1895 }
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301896 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1897 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1898 if (dev_max_lba >> 32) {
1899 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1900 "disabled for the drive with handle(0x%04x) member"
1901 "handle (0x%04x) unsupported max lba 0x%016llx\n",
1902 ioc->name, raid_device->handle,
1903 le16_to_cpu(pd_pg0.DevHandle),
1904 (unsigned long long)dev_max_lba);
1905 goto out_error;
1906 }
1907
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301908 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1909 }
1910
1911 /*
1912 * Assumption for WD: Direct I/O is not supported if the volume is
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301913 * not RAID0
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301914 */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301915 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301916 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1917 "for the drive with handle(0x%04x): type=%d, "
1918 "s_sz=%uK, blk_size=%u\n", ioc->name,
1919 raid_device->handle, raid_device->volume_type,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301920 (le32_to_cpu(vol_pg0->StripeSize) *
1921 le16_to_cpu(vol_pg0->BlockSize)) / 1024,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301922 le16_to_cpu(vol_pg0->BlockSize));
1923 goto out_error;
1924 }
1925
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301926 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301927 stripe_exp = find_first_bit(&stripe_sz, 32);
1928 if (stripe_exp == 32) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301929 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301930 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301931 ioc->name, raid_device->handle,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301932 (le32_to_cpu(vol_pg0->StripeSize) *
1933 le16_to_cpu(vol_pg0->BlockSize)) / 1024);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301934 goto out_error;
1935 }
1936 raid_device->stripe_exponent = stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301937 block_sz = le16_to_cpu(vol_pg0->BlockSize);
1938 block_exp = find_first_bit(&block_sz, 16);
1939 if (block_exp == 16) {
1940 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1941 "for the drive with handle(0x%04x) invalid block sz %u\n",
1942 ioc->name, raid_device->handle,
1943 le16_to_cpu(vol_pg0->BlockSize));
1944 goto out_error;
1945 }
1946 raid_device->block_exponent = block_exp;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301947 raid_device->direct_io_enabled = 1;
1948
1949 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1950 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1951 /*
1952 * WARPDRIVE: Though the following fields are not used for direct IO,
1953 * stored for future purpose:
1954 */
1955 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1956 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1957 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1958
1959
1960 kfree(vol_pg0);
1961 return;
1962
1963out_error:
1964 raid_device->direct_io_enabled = 0;
1965 for (count = 0; count < num_pds; count++)
1966 raid_device->pd_handle[count] = 0;
1967 kfree(vol_pg0);
1968 return;
1969}
Eric Moore635374e2009-03-09 01:21:12 -06001970
1971/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301972 * _scsih_enable_tlr - setting TLR flags
1973 * @ioc: per adapter object
1974 * @sdev: scsi device struct
1975 *
1976 * Enabling Transaction Layer Retries for tape devices when
1977 * vpd page 0x90 is present
1978 *
1979 */
1980static void
1981_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1982{
1983 /* only for TAPE */
1984 if (sdev->type != TYPE_TAPE)
1985 return;
1986
1987 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1988 return;
1989
1990 sas_enable_tlr(sdev);
1991 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1992 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1993 return;
1994
1995}
1996
1997/**
Eric Moored5d135b2009-05-18 13:02:08 -06001998 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001999 * @sdev: scsi device struct
2000 *
2001 * Returns 0 if ok. Any other return is assumed to be an error and
2002 * the device is ignored.
2003 */
2004static int
Eric Moored5d135b2009-05-18 13:02:08 -06002005_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06002006{
2007 struct Scsi_Host *shost = sdev->host;
2008 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2009 struct MPT2SAS_DEVICE *sas_device_priv_data;
2010 struct MPT2SAS_TARGET *sas_target_priv_data;
2011 struct _sas_device *sas_device;
2012 struct _raid_device *raid_device;
2013 unsigned long flags;
2014 int qdepth;
2015 u8 ssp_target = 0;
2016 char *ds = "";
2017 char *r_level = "";
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302018 u16 handle, volume_handle = 0;
2019 u64 volume_wwid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002020
2021 qdepth = 1;
2022 sas_device_priv_data = sdev->hostdata;
2023 sas_device_priv_data->configured_lun = 1;
2024 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
2025 sas_target_priv_data = sas_device_priv_data->sas_target;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302026 handle = sas_target_priv_data->handle;
Eric Moore635374e2009-03-09 01:21:12 -06002027
2028 /* raid volume handling */
2029 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
2030
2031 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302032 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06002033 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2034 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302035 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2036 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2037 __LINE__, __func__));
2038 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002039 }
2040
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302041 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2042 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2043 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2044 __LINE__, __func__));
2045 return 1;
2046 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302047 /*
2048 * WARPDRIVE: Initialize the required data for Direct IO
2049 */
2050 _scsih_init_warpdrive_properties(ioc, raid_device);
2051
Eric Moore635374e2009-03-09 01:21:12 -06002052 /* RAID Queue Depth Support
2053 * IS volume = underlying qdepth of drive type, either
2054 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2055 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2056 */
2057 if (raid_device->device_info &
2058 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2059 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2060 ds = "SSP";
2061 } else {
2062 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2063 if (raid_device->device_info &
2064 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2065 ds = "SATA";
2066 else
2067 ds = "STP";
2068 }
2069
2070 switch (raid_device->volume_type) {
2071 case MPI2_RAID_VOL_TYPE_RAID0:
2072 r_level = "RAID0";
2073 break;
2074 case MPI2_RAID_VOL_TYPE_RAID1E:
2075 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302076 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302077 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302078 MFG10_GF0_R10_DISPLAY) &&
2079 !(raid_device->num_pds % 2))
2080 r_level = "RAID10";
2081 else
2082 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002083 break;
2084 case MPI2_RAID_VOL_TYPE_RAID1:
2085 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2086 r_level = "RAID1";
2087 break;
2088 case MPI2_RAID_VOL_TYPE_RAID10:
2089 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2090 r_level = "RAID10";
2091 break;
2092 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2093 default:
2094 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2095 r_level = "RAIDX";
2096 break;
2097 }
2098
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302099 if (!ioc->hide_ir_msg)
2100 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2101 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2102 r_level, raid_device->handle,
2103 (unsigned long long)raid_device->wwid,
2104 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002105 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302106 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302107 if (!ioc->is_warpdrive)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302108 _scsih_set_level(sdev, raid_device->volume_type);
Eric Moore635374e2009-03-09 01:21:12 -06002109 return 0;
2110 }
2111
2112 /* non-raid handling */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302113 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2114 if (mpt2sas_config_get_volume_handle(ioc, handle,
2115 &volume_handle)) {
2116 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2117 "failure at %s:%d/%s()!\n", ioc->name,
2118 __FILE__, __LINE__, __func__));
2119 return 1;
2120 }
2121 if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
2122 volume_handle, &volume_wwid)) {
2123 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2124 "failure at %s:%d/%s()!\n", ioc->name,
2125 __FILE__, __LINE__, __func__));
2126 return 1;
2127 }
2128 }
2129
Eric Moore635374e2009-03-09 01:21:12 -06002130 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2131 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2132 sas_device_priv_data->sas_target->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302133 if (!sas_device) {
2134 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302135 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302136 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2137 __LINE__, __func__));
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302138 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002139 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302140 sas_device->volume_handle = volume_handle;
2141 sas_device->volume_wwid = volume_wwid;
2142 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2143 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2144 ssp_target = 1;
2145 ds = "SSP";
2146 } else {
2147 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2148 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2149 ds = "STP";
2150 else if (sas_device->device_info &
2151 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2152 ds = "SATA";
2153 }
2154 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2155 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2156 ds, sas_device->handle,
2157 (unsigned long long)sas_device->sas_address,
2158 sas_device->phy,
2159 (unsigned long long)sas_device->device_name);
2160 sdev_printk(KERN_INFO, sdev, "%s: "
2161 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2162 (unsigned long long) sas_device->enclosure_logical_id,
2163 sas_device->slot);
2164
2165 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2166 if (!ssp_target)
2167 _scsih_display_sata_capabilities(ioc, handle, sdev);
2168
Eric Moore635374e2009-03-09 01:21:12 -06002169
Mike Christiee881a172009-10-15 17:46:39 -07002170 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002171
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302172 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002173 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302174 _scsih_enable_tlr(ioc, sdev);
2175 }
Eric Moore635374e2009-03-09 01:21:12 -06002176 return 0;
2177}
2178
2179/**
Eric Moored5d135b2009-05-18 13:02:08 -06002180 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002181 * @sdev: scsi device struct
2182 * @bdev: pointer to block device context
2183 * @capacity: device size (in 512 byte sectors)
2184 * @params: three element array to place output:
2185 * params[0] number of heads (max 255)
2186 * params[1] number of sectors (max 63)
2187 * params[2] number of cylinders
2188 *
2189 * Return nothing.
2190 */
2191static int
Eric Moored5d135b2009-05-18 13:02:08 -06002192_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002193 sector_t capacity, int params[])
2194{
2195 int heads;
2196 int sectors;
2197 sector_t cylinders;
2198 ulong dummy;
2199
2200 heads = 64;
2201 sectors = 32;
2202
2203 dummy = heads * sectors;
2204 cylinders = capacity;
2205 sector_div(cylinders, dummy);
2206
2207 /*
2208 * Handle extended translation size for logical drives
2209 * > 1Gb
2210 */
2211 if ((ulong)capacity >= 0x200000) {
2212 heads = 255;
2213 sectors = 63;
2214 dummy = heads * sectors;
2215 cylinders = capacity;
2216 sector_div(cylinders, dummy);
2217 }
2218
2219 /* return result */
2220 params[0] = heads;
2221 params[1] = sectors;
2222 params[2] = cylinders;
2223
2224 return 0;
2225}
2226
2227/**
2228 * _scsih_response_code - translation of device response code
2229 * @ioc: per adapter object
2230 * @response_code: response code returned by the device
2231 *
2232 * Return nothing.
2233 */
2234static void
2235_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2236{
2237 char *desc;
2238
2239 switch (response_code) {
2240 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2241 desc = "task management request completed";
2242 break;
2243 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2244 desc = "invalid frame";
2245 break;
2246 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2247 desc = "task management request not supported";
2248 break;
2249 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2250 desc = "task management request failed";
2251 break;
2252 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2253 desc = "task management request succeeded";
2254 break;
2255 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2256 desc = "invalid lun";
2257 break;
2258 case 0xA:
2259 desc = "overlapped tag attempted";
2260 break;
2261 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2262 desc = "task queued, however not sent to target";
2263 break;
2264 default:
2265 desc = "unknown";
2266 break;
2267 }
2268 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2269 ioc->name, response_code, desc);
2270}
2271
2272/**
Eric Moored5d135b2009-05-18 13:02:08 -06002273 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002274 * @ioc: per adapter object
2275 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302276 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002277 * @reply: reply message frame(lower 32bit addr)
2278 * Context: none.
2279 *
2280 * The callback handler when using scsih_issue_tm.
2281 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302282 * Return 1 meaning mf should be freed from _base_interrupt
2283 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002284 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302285static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302286_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002287{
2288 MPI2DefaultReply_t *mpi_reply;
2289
2290 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302291 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002292 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302293 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302294 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002295 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2296 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2297 if (mpi_reply) {
2298 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2299 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2300 }
2301 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2302 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302303 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002304}
2305
2306/**
2307 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2308 * @ioc: per adapter object
2309 * @handle: device handle
2310 *
2311 * During taskmangement request, we need to freeze the device queue.
2312 */
2313void
2314mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2315{
2316 struct MPT2SAS_DEVICE *sas_device_priv_data;
2317 struct scsi_device *sdev;
2318 u8 skip = 0;
2319
2320 shost_for_each_device(sdev, ioc->shost) {
2321 if (skip)
2322 continue;
2323 sas_device_priv_data = sdev->hostdata;
2324 if (!sas_device_priv_data)
2325 continue;
2326 if (sas_device_priv_data->sas_target->handle == handle) {
2327 sas_device_priv_data->sas_target->tm_busy = 1;
2328 skip = 1;
2329 ioc->ignore_loginfos = 1;
2330 }
2331 }
2332}
2333
2334/**
2335 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2336 * @ioc: per adapter object
2337 * @handle: device handle
2338 *
2339 * During taskmangement request, we need to freeze the device queue.
2340 */
2341void
2342mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2343{
2344 struct MPT2SAS_DEVICE *sas_device_priv_data;
2345 struct scsi_device *sdev;
2346 u8 skip = 0;
2347
2348 shost_for_each_device(sdev, ioc->shost) {
2349 if (skip)
2350 continue;
2351 sas_device_priv_data = sdev->hostdata;
2352 if (!sas_device_priv_data)
2353 continue;
2354 if (sas_device_priv_data->sas_target->handle == handle) {
2355 sas_device_priv_data->sas_target->tm_busy = 0;
2356 skip = 1;
2357 ioc->ignore_loginfos = 0;
2358 }
2359 }
2360}
2361
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302362
Eric Moore635374e2009-03-09 01:21:12 -06002363/**
2364 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2365 * @ioc: per adapter struct
2366 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302367 * @channel: the channel assigned by the OS
2368 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002369 * @lun: lun number
2370 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2371 * @smid_task: smid assigned to the task
2372 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302373 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302374 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002375 *
2376 * A generic API for sending task management requests to firmware.
2377 *
Eric Moore635374e2009-03-09 01:21:12 -06002378 * The callback index is set inside `ioc->tm_cb_idx`.
2379 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302380 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002381 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302382int
2383mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2384 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002385 enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002386{
2387 Mpi2SCSITaskManagementRequest_t *mpi_request;
2388 Mpi2SCSITaskManagementReply_t *mpi_reply;
2389 u16 smid = 0;
2390 u32 ioc_state;
2391 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302392 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302393 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002394
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302395 if (m_type == TM_MUTEX_ON)
2396 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302397 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2398 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2399 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302400 rc = FAILED;
2401 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302402 }
2403
Eric Moore3cb54692010-07-08 14:44:34 -06002404 if (ioc->shost_recovery || ioc->remove_host ||
2405 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002406 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2407 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302408 rc = FAILED;
2409 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002410 }
Eric Moore635374e2009-03-09 01:21:12 -06002411
2412 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2413 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302414 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002415 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302416 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302417 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302418 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302419 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002420 }
2421
2422 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2423 mpt2sas_base_fault_info(ioc, ioc_state &
2424 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302425 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302426 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302427 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302428 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002429 }
2430
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302431 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002432 if (!smid) {
2433 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2434 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302435 rc = FAILED;
2436 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002437 }
2438
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302439 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2440 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2441
Eric Moore635374e2009-03-09 01:21:12 -06002442 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302443 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2444 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002445 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2446 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2447 ioc->tm_cmds.smid = smid;
2448 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302449 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002450 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2451 mpi_request->DevHandle = cpu_to_le16(handle);
2452 mpi_request->TaskType = type;
2453 mpi_request->TaskMID = cpu_to_le16(smid_task);
2454 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2455 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302456 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302457 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002458 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002459 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2460 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2461 ioc->name, __func__);
2462 _debug_dump_mf(mpi_request,
2463 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302464 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302465 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302466 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302467 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302468 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2469 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2470 goto err_out;
2471 }
Eric Moore635374e2009-03-09 01:21:12 -06002472 }
2473
2474 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2475 mpi_reply = ioc->tm_cmds.reply;
2476 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2477 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2478 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2479 le32_to_cpu(mpi_reply->IOCLogInfo),
2480 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302481 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002482 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302483 if (mpi_reply->IOCStatus)
2484 _debug_dump_mf(mpi_request,
2485 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2486 }
Eric Moore635374e2009-03-09 01:21:12 -06002487 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302488
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302489 switch (type) {
2490 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302491 rc = SUCCESS;
2492 if (scsi_lookup->scmd == NULL)
2493 break;
2494 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302495 break;
2496
2497 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2498 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2499 rc = FAILED;
2500 else
2501 rc = SUCCESS;
2502 break;
2503
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302504 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302505 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2506 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2507 rc = FAILED;
2508 else
2509 rc = SUCCESS;
2510 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302511 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2512 rc = SUCCESS;
2513 break;
2514 default:
2515 rc = FAILED;
2516 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302517 }
2518
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302519 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2520 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302521 if (m_type == TM_MUTEX_ON)
2522 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302523
2524 return rc;
2525
2526 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302527 if (m_type == TM_MUTEX_ON)
2528 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302529 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002530}
2531
2532/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302533 * _scsih_tm_display_info - displays info about the device
2534 * @ioc: per adapter struct
2535 * @scmd: pointer to scsi command object
2536 *
2537 * Called by task management callback handlers.
2538 */
2539static void
2540_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2541{
2542 struct scsi_target *starget = scmd->device->sdev_target;
2543 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2544 struct _sas_device *sas_device = NULL;
2545 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302546 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302547
2548 if (!priv_target)
2549 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302550 if (ioc->hide_ir_msg)
2551 device_str = "WarpDrive";
2552 else
2553 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302554
2555 scsi_print_command(scmd);
2556 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302557 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2558 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2559 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302560 } else {
2561 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2562 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2563 priv_target->sas_address);
2564 if (sas_device) {
2565 if (priv_target->flags &
2566 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2567 starget_printk(KERN_INFO, starget,
2568 "volume handle(0x%04x), "
2569 "volume wwid(0x%016llx)\n",
2570 sas_device->volume_handle,
2571 (unsigned long long)sas_device->volume_wwid);
2572 }
2573 starget_printk(KERN_INFO, starget,
2574 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2575 sas_device->handle,
2576 (unsigned long long)sas_device->sas_address,
2577 sas_device->phy);
2578 starget_printk(KERN_INFO, starget,
2579 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2580 (unsigned long long)sas_device->enclosure_logical_id,
2581 sas_device->slot);
2582 }
2583 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2584 }
2585}
2586
2587/**
Eric Moored5d135b2009-05-18 13:02:08 -06002588 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302589 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002590 *
2591 * Returns SUCCESS if command aborted else FAILED
2592 */
2593static int
Eric Moored5d135b2009-05-18 13:02:08 -06002594_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002595{
2596 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2597 struct MPT2SAS_DEVICE *sas_device_priv_data;
2598 u16 smid;
2599 u16 handle;
2600 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002601
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302602 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2603 "scmd(%p)\n", scmd);
2604 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002605
2606 sas_device_priv_data = scmd->device->hostdata;
2607 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302608 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2609 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002610 scmd->result = DID_NO_CONNECT << 16;
2611 scmd->scsi_done(scmd);
2612 r = SUCCESS;
2613 goto out;
2614 }
2615
2616 /* search for the command */
2617 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2618 if (!smid) {
2619 scmd->result = DID_RESET << 16;
2620 r = SUCCESS;
2621 goto out;
2622 }
2623
2624 /* for hidden raid components and volumes this is not supported */
2625 if (sas_device_priv_data->sas_target->flags &
2626 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2627 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2628 scmd->result = DID_RESET << 16;
2629 r = FAILED;
2630 goto out;
2631 }
2632
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302633 mpt2sas_halt_firmware(ioc);
2634
Eric Moore635374e2009-03-09 01:21:12 -06002635 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302636 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2637 scmd->device->id, scmd->device->lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002638 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002639
2640 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302641 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2642 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002643 return r;
2644}
2645
Eric Moore635374e2009-03-09 01:21:12 -06002646/**
Eric Moored5d135b2009-05-18 13:02:08 -06002647 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302648 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002649 *
2650 * Returns SUCCESS if command aborted else FAILED
2651 */
2652static int
Eric Moored5d135b2009-05-18 13:02:08 -06002653_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002654{
2655 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2656 struct MPT2SAS_DEVICE *sas_device_priv_data;
2657 struct _sas_device *sas_device;
2658 unsigned long flags;
2659 u16 handle;
2660 int r;
2661
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302662 struct scsi_target *starget = scmd->device->sdev_target;
2663
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302664 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302665 "scmd(%p)\n", scmd);
2666 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002667
2668 sas_device_priv_data = scmd->device->hostdata;
2669 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302670 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302671 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002672 scmd->result = DID_NO_CONNECT << 16;
2673 scmd->scsi_done(scmd);
2674 r = SUCCESS;
2675 goto out;
2676 }
2677
2678 /* for hidden raid components obtain the volume_handle */
2679 handle = 0;
2680 if (sas_device_priv_data->sas_target->flags &
2681 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2682 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2683 sas_device = _scsih_sas_device_find_by_handle(ioc,
2684 sas_device_priv_data->sas_target->handle);
2685 if (sas_device)
2686 handle = sas_device->volume_handle;
2687 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2688 } else
2689 handle = sas_device_priv_data->sas_target->handle;
2690
2691 if (!handle) {
2692 scmd->result = DID_RESET << 16;
2693 r = FAILED;
2694 goto out;
2695 }
2696
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302697 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2698 scmd->device->id, scmd->device->lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002699 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002700
2701 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302702 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2703 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002704 return r;
2705}
2706
2707/**
Eric Moored5d135b2009-05-18 13:02:08 -06002708 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302709 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002710 *
2711 * Returns SUCCESS if command aborted else FAILED
2712 */
2713static int
Eric Moored5d135b2009-05-18 13:02:08 -06002714_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002715{
2716 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2717 struct MPT2SAS_DEVICE *sas_device_priv_data;
2718 struct _sas_device *sas_device;
2719 unsigned long flags;
2720 u16 handle;
2721 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302722 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002723
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302724 starget_printk(KERN_INFO, starget, "attempting target reset! "
2725 "scmd(%p)\n", scmd);
2726 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002727
2728 sas_device_priv_data = scmd->device->hostdata;
2729 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302730 starget_printk(KERN_INFO, starget, "target been deleted! "
2731 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002732 scmd->result = DID_NO_CONNECT << 16;
2733 scmd->scsi_done(scmd);
2734 r = SUCCESS;
2735 goto out;
2736 }
2737
2738 /* for hidden raid components obtain the volume_handle */
2739 handle = 0;
2740 if (sas_device_priv_data->sas_target->flags &
2741 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2742 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2743 sas_device = _scsih_sas_device_find_by_handle(ioc,
2744 sas_device_priv_data->sas_target->handle);
2745 if (sas_device)
2746 handle = sas_device->volume_handle;
2747 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2748 } else
2749 handle = sas_device_priv_data->sas_target->handle;
2750
2751 if (!handle) {
2752 scmd->result = DID_RESET << 16;
2753 r = FAILED;
2754 goto out;
2755 }
2756
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302757 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2758 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002759 30, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002760
2761 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302762 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2763 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002764 return r;
2765}
2766
2767/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302768 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302769 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002770 *
2771 * Returns SUCCESS if command aborted else FAILED
2772 */
2773static int
Eric Moored5d135b2009-05-18 13:02:08 -06002774_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002775{
2776 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2777 int r, retval;
2778
2779 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2780 ioc->name, scmd);
2781 scsi_print_command(scmd);
2782
2783 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2784 FORCE_BIG_HAMMER);
2785 r = (retval < 0) ? FAILED : SUCCESS;
2786 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2787 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2788
2789 return r;
2790}
2791
2792/**
2793 * _scsih_fw_event_add - insert and queue up fw_event
2794 * @ioc: per adapter object
2795 * @fw_event: object describing the event
2796 * Context: This function will acquire ioc->fw_event_lock.
2797 *
2798 * This adds the firmware event object into link list, then queues it up to
2799 * be processed from user context.
2800 *
2801 * Return nothing.
2802 */
2803static void
2804_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2805{
2806 unsigned long flags;
2807
2808 if (ioc->firmware_event_thread == NULL)
2809 return;
2810
2811 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2812 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302813 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2814 queue_delayed_work(ioc->firmware_event_thread,
2815 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002816 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2817}
2818
2819/**
2820 * _scsih_fw_event_free - delete fw_event
2821 * @ioc: per adapter object
2822 * @fw_event: object describing the event
2823 * Context: This function will acquire ioc->fw_event_lock.
2824 *
2825 * This removes firmware event object from link list, frees associated memory.
2826 *
2827 * Return nothing.
2828 */
2829static void
2830_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2831 *fw_event)
2832{
2833 unsigned long flags;
2834
2835 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2836 list_del(&fw_event->list);
Eric Moore635374e2009-03-09 01:21:12 -06002837 kfree(fw_event);
2838 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2839}
2840
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302841
Eric Moore635374e2009-03-09 01:21:12 -06002842/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302843 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002844 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002845 *
2846 * Return nothing.
2847 */
2848static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302849_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002850{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302851 struct fw_event_work *fw_event;
2852
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302853 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302854 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002855
2856 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2857 if (!fw_event)
2858 return;
2859
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302860 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2861 fw_event->ioc = ioc;
2862 _scsih_fw_event_add(ioc, fw_event);
2863}
2864
2865/**
2866 * mpt2sas_port_enable_complete - port enable completed (fake event)
2867 * @ioc: per adapter object
2868 *
2869 * Return nothing.
2870 */
2871void
2872mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2873{
2874 struct fw_event_work *fw_event;
2875
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302876 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2877 if (!fw_event)
2878 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302879 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302880 fw_event->ioc = ioc;
2881 _scsih_fw_event_add(ioc, fw_event);
2882}
2883
2884/**
2885 * _scsih_fw_event_cleanup_queue - cleanup event queue
2886 * @ioc: per adapter object
2887 *
2888 * Walk the firmware event queue, either killing timers, or waiting
2889 * for outstanding events to complete
2890 *
2891 * Return nothing.
2892 */
2893static void
2894_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2895{
2896 struct fw_event_work *fw_event, *next;
2897
2898 if (list_empty(&ioc->fw_event_list) ||
2899 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002900 return;
2901
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302902 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05302903 if (cancel_delayed_work_sync(&fw_event->delayed_work)) {
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302904 _scsih_fw_event_free(ioc, fw_event);
2905 continue;
2906 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302907 }
Eric Moore635374e2009-03-09 01:21:12 -06002908}
2909
Eric Moore635374e2009-03-09 01:21:12 -06002910/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302911 * _scsih_ublock_io_all_device - unblock every device
2912 * @ioc: per adapter object
2913 *
2914 * change the device state from block to running
2915 */
2916static void
2917_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2918{
2919 struct MPT2SAS_DEVICE *sas_device_priv_data;
2920 struct scsi_device *sdev;
2921
2922 shost_for_each_device(sdev, ioc->shost) {
2923 sas_device_priv_data = sdev->hostdata;
2924 if (!sas_device_priv_data)
2925 continue;
2926 if (!sas_device_priv_data->block)
2927 continue;
2928 sas_device_priv_data->block = 0;
2929 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2930 "handle(0x%04x)\n",
2931 sas_device_priv_data->sas_target->handle));
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002932 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302933 }
2934}
2935/**
Eric Moore635374e2009-03-09 01:21:12 -06002936 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2937 * @ioc: per adapter object
2938 * @handle: device handle
2939 *
2940 * During device pull we need to appropiately set the sdev state.
2941 */
2942static void
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302943_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06002944{
2945 struct MPT2SAS_DEVICE *sas_device_priv_data;
2946 struct scsi_device *sdev;
2947
2948 shost_for_each_device(sdev, ioc->shost) {
2949 sas_device_priv_data = sdev->hostdata;
2950 if (!sas_device_priv_data)
2951 continue;
2952 if (!sas_device_priv_data->block)
2953 continue;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302954 if (sas_device_priv_data->sas_target->sas_address ==
2955 sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06002956 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2957 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302958 "sas address(0x%016llx)\n", ioc->name,
2959 (unsigned long long)sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06002960 sas_device_priv_data->block = 0;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002961 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Eric Moore635374e2009-03-09 01:21:12 -06002962 }
2963 }
2964}
2965
2966/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302967 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2968 * @ioc: per adapter object
2969 * @handle: device handle
2970 *
2971 * During device pull we need to appropiately set the sdev state.
2972 */
2973static void
2974_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2975{
2976 struct MPT2SAS_DEVICE *sas_device_priv_data;
2977 struct scsi_device *sdev;
2978
2979 shost_for_each_device(sdev, ioc->shost) {
2980 sas_device_priv_data = sdev->hostdata;
2981 if (!sas_device_priv_data)
2982 continue;
2983 if (sas_device_priv_data->block)
2984 continue;
2985 sas_device_priv_data->block = 1;
2986 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2987 "handle(0x%04x)\n",
2988 sas_device_priv_data->sas_target->handle));
2989 scsi_internal_device_block(sdev);
2990 }
2991}
2992
2993
2994/**
Eric Moore635374e2009-03-09 01:21:12 -06002995 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2996 * @ioc: per adapter object
2997 * @handle: device handle
2998 *
2999 * During device pull we need to appropiately set the sdev state.
3000 */
3001static void
3002_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3003{
3004 struct MPT2SAS_DEVICE *sas_device_priv_data;
3005 struct scsi_device *sdev;
3006
3007 shost_for_each_device(sdev, ioc->shost) {
3008 sas_device_priv_data = sdev->hostdata;
3009 if (!sas_device_priv_data)
3010 continue;
3011 if (sas_device_priv_data->block)
3012 continue;
3013 if (sas_device_priv_data->sas_target->handle == handle) {
3014 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
3015 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
3016 "handle(0x%04x)\n", ioc->name, handle));
3017 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05303018 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06003019 }
3020 }
3021}
3022
3023/**
3024 * _scsih_block_io_to_children_attached_to_ex
3025 * @ioc: per adapter object
3026 * @sas_expander: the sas_device object
3027 *
3028 * This routine set sdev state to SDEV_BLOCK for all devices
3029 * attached to this expander. This function called when expander is
3030 * pulled.
3031 */
3032static void
3033_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
3034 struct _sas_node *sas_expander)
3035{
3036 struct _sas_port *mpt2sas_port;
3037 struct _sas_device *sas_device;
3038 struct _sas_node *expander_sibling;
3039 unsigned long flags;
3040
3041 if (!sas_expander)
3042 return;
3043
3044 list_for_each_entry(mpt2sas_port,
3045 &sas_expander->sas_port_list, port_list) {
3046 if (mpt2sas_port->remote_identify.device_type ==
3047 SAS_END_DEVICE) {
3048 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3049 sas_device =
3050 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3051 mpt2sas_port->remote_identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303052 if (sas_device)
3053 set_bit(sas_device->handle,
3054 ioc->blocking_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003055 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06003056 }
3057 }
3058
3059 list_for_each_entry(mpt2sas_port,
3060 &sas_expander->sas_port_list, port_list) {
3061
3062 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303063 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003064 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303065 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003066 expander_sibling =
3067 mpt2sas_scsih_expander_find_by_sas_address(
3068 ioc, mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003069 _scsih_block_io_to_children_attached_to_ex(ioc,
3070 expander_sibling);
3071 }
3072 }
3073}
3074
3075/**
3076 * _scsih_block_io_to_children_attached_directly
3077 * @ioc: per adapter object
3078 * @event_data: topology change event data
3079 *
3080 * This routine set sdev state to SDEV_BLOCK for all devices
3081 * direct attached during device pull.
3082 */
3083static void
3084_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3085 Mpi2EventDataSasTopologyChangeList_t *event_data)
3086{
3087 int i;
3088 u16 handle;
3089 u16 reason_code;
3090 u8 phy_number;
3091
3092 for (i = 0; i < event_data->NumEntries; i++) {
3093 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3094 if (!handle)
3095 continue;
3096 phy_number = event_data->StartPhyNum + i;
3097 reason_code = event_data->PHY[i].PhyStatus &
3098 MPI2_EVENT_SAS_TOPO_RC_MASK;
3099 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3100 _scsih_block_io_device(ioc, handle);
3101 }
3102}
3103
3104/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303105 * _scsih_tm_tr_send - send task management request
3106 * @ioc: per adapter object
3107 * @handle: device handle
3108 * Context: interrupt time.
3109 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003110 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303111 * with controller firmware. This function will issue target reset
3112 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003113 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303114 *
3115 * This is designed to send muliple task management request at the same
3116 * time to the fifo. If the fifo is full, we will append the request,
3117 * and process it in a future completion.
3118 */
3119static void
3120_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3121{
3122 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303123 u16 smid;
3124 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303125 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3126 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303127 unsigned long flags;
3128 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303129 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303130
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303131 if (ioc->remove_host) {
3132 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3133 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3134 return;
3135 } else if (ioc->pci_error_recovery) {
3136 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3137 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3138 handle));
3139 return;
3140 }
3141 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3142 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3143 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3144 "operational: handle(0x%04x)\n", __func__, ioc->name,
3145 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303146 return;
3147 }
3148
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303149 /* if PD, then return */
3150 if (test_bit(handle, ioc->pd_handles))
3151 return;
3152
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303153 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3154 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303155 if (sas_device && sas_device->starget &&
3156 sas_device->starget->hostdata) {
3157 sas_target_priv_data = sas_device->starget->hostdata;
3158 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303159 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303160 }
3161 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303162
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303163 if (sas_target_priv_data) {
3164 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3165 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3166 (unsigned long long)sas_address));
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05303167 _scsih_ublock_io_device(ioc, sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303168 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303169 }
3170
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303171 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3172 if (!smid) {
3173 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3174 if (!delayed_tr)
3175 return;
3176 INIT_LIST_HEAD(&delayed_tr->list);
3177 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303178 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3179 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3180 "DELAYED:tr:handle(0x%04x), (open)\n",
3181 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303182 return;
3183 }
3184
Kashyap, Desai1278b112010-03-09 17:34:13 +05303185 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3186 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3187 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303188 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3189 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3190 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3191 mpi_request->DevHandle = cpu_to_le16(handle);
3192 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303193 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3194}
3195
3196
3197
3198/**
3199 * _scsih_sas_control_complete - completion routine
3200 * @ioc: per adapter object
3201 * @smid: system request message index
3202 * @msix_index: MSIX table index supplied by the OS
3203 * @reply: reply message frame(lower 32bit addr)
3204 * Context: interrupt time.
3205 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003206 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303207 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003208 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303209 *
3210 * Return 1 meaning mf should be freed from _base_interrupt
3211 * 0 means the mf is freed from this function.
3212 */
3213static u8
3214_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3215 u8 msix_index, u32 reply)
3216{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303217 Mpi2SasIoUnitControlReply_t *mpi_reply =
3218 mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303219 if (likely(mpi_reply)) {
3220 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3221 "sc_complete:handle(0x%04x), (open) "
3222 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3223 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3224 le16_to_cpu(mpi_reply->IOCStatus),
3225 le32_to_cpu(mpi_reply->IOCLogInfo)));
3226 } else {
3227 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3228 ioc->name, __FILE__, __LINE__, __func__);
3229 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303230 return 1;
3231}
3232
3233/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303234 * _scsih_tm_tr_volume_send - send target reset request for volumes
3235 * @ioc: per adapter object
3236 * @handle: device handle
3237 * Context: interrupt time.
3238 *
3239 * This is designed to send muliple task management request at the same
3240 * time to the fifo. If the fifo is full, we will append the request,
3241 * and process it in a future completion.
3242 */
3243static void
3244_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3245{
3246 Mpi2SCSITaskManagementRequest_t *mpi_request;
3247 u16 smid;
3248 struct _tr_list *delayed_tr;
3249
Eric Moore3cb54692010-07-08 14:44:34 -06003250 if (ioc->shost_recovery || ioc->remove_host ||
3251 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303252 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3253 "progress!\n", __func__, ioc->name));
3254 return;
3255 }
3256
3257 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3258 if (!smid) {
3259 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3260 if (!delayed_tr)
3261 return;
3262 INIT_LIST_HEAD(&delayed_tr->list);
3263 delayed_tr->handle = handle;
3264 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3265 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3266 "DELAYED:tr:handle(0x%04x), (open)\n",
3267 ioc->name, handle));
3268 return;
3269 }
3270
3271 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3272 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3273 ioc->tm_tr_volume_cb_idx));
3274 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3275 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3276 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3277 mpi_request->DevHandle = cpu_to_le16(handle);
3278 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3279 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3280}
3281
3282/**
3283 * _scsih_tm_volume_tr_complete - target reset completion
3284 * @ioc: per adapter object
3285 * @smid: system request message index
3286 * @msix_index: MSIX table index supplied by the OS
3287 * @reply: reply message frame(lower 32bit addr)
3288 * Context: interrupt time.
3289 *
3290 * Return 1 meaning mf should be freed from _base_interrupt
3291 * 0 means the mf is freed from this function.
3292 */
3293static u8
3294_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3295 u8 msix_index, u32 reply)
3296{
3297 u16 handle;
3298 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3299 Mpi2SCSITaskManagementReply_t *mpi_reply =
3300 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3301
Eric Moore3cb54692010-07-08 14:44:34 -06003302 if (ioc->shost_recovery || ioc->remove_host ||
3303 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303304 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3305 "progress!\n", __func__, ioc->name));
3306 return 1;
3307 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303308 if (unlikely(!mpi_reply)) {
3309 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3310 ioc->name, __FILE__, __LINE__, __func__);
3311 return 1;
3312 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303313 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3314 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3315 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3316 dewtprintk(ioc, printk("spurious interrupt: "
3317 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3318 le16_to_cpu(mpi_reply->DevHandle), smid));
3319 return 0;
3320 }
3321
3322 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3323 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3324 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3325 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3326 le32_to_cpu(mpi_reply->IOCLogInfo),
3327 le32_to_cpu(mpi_reply->TerminationCount)));
3328
3329 return _scsih_check_for_pending_tm(ioc, smid);
3330}
3331
3332/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303333 * _scsih_tm_tr_complete -
3334 * @ioc: per adapter object
3335 * @smid: system request message index
3336 * @msix_index: MSIX table index supplied by the OS
3337 * @reply: reply message frame(lower 32bit addr)
3338 * Context: interrupt time.
3339 *
3340 * This is the target reset completion routine.
3341 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003342 * handshake protocol with controller firmware.
3343 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303344 *
3345 * Return 1 meaning mf should be freed from _base_interrupt
3346 * 0 means the mf is freed from this function.
3347 */
3348static u8
3349_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3350 u32 reply)
3351{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303352 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303353 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303354 Mpi2SCSITaskManagementReply_t *mpi_reply =
3355 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3356 Mpi2SasIoUnitControlRequest_t *mpi_request;
3357 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303358 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303359
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303360 if (ioc->remove_host) {
3361 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3362 "removed\n", __func__, ioc->name));
3363 return 1;
3364 } else if (ioc->pci_error_recovery) {
3365 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3366 "error recovery\n", __func__, ioc->name));
3367 return 1;
3368 }
3369 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3370 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3371 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3372 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303373 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303374 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303375 if (unlikely(!mpi_reply)) {
3376 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3377 ioc->name, __FILE__, __LINE__, __func__);
3378 return 1;
3379 }
Kashyap, Desai1278b112010-03-09 17:34:13 +05303380 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3381 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3382 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3383 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3384 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3385 le16_to_cpu(mpi_reply->DevHandle), smid));
3386 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303387 }
3388
Kashyap, Desai1278b112010-03-09 17:34:13 +05303389 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3390 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3391 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3392 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3393 le32_to_cpu(mpi_reply->IOCLogInfo),
3394 le32_to_cpu(mpi_reply->TerminationCount)));
3395
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303396 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3397 if (!smid_sas_ctrl) {
3398 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3399 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303400 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303401 }
3402
Kashyap, Desai1278b112010-03-09 17:34:13 +05303403 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3404 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3405 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303406 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3407 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3408 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3409 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303410 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303411 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303412
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303413 return _scsih_check_for_pending_tm(ioc, smid);
3414}
3415
3416/**
3417 * _scsih_check_for_pending_tm - check for pending task management
3418 * @ioc: per adapter object
3419 * @smid: system request message index
3420 *
3421 * This will check delayed target reset list, and feed the
3422 * next reqeust.
3423 *
3424 * Return 1 meaning mf should be freed from _base_interrupt
3425 * 0 means the mf is freed from this function.
3426 */
3427static u8
3428_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3429{
3430 struct _tr_list *delayed_tr;
3431
3432 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3433 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3434 struct _tr_list, list);
3435 mpt2sas_base_free_smid(ioc, smid);
3436 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3437 list_del(&delayed_tr->list);
3438 kfree(delayed_tr);
3439 return 0;
3440 }
3441
Kashyap, Desai1278b112010-03-09 17:34:13 +05303442 if (!list_empty(&ioc->delayed_tr_list)) {
3443 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3444 struct _tr_list, list);
3445 mpt2sas_base_free_smid(ioc, smid);
3446 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3447 list_del(&delayed_tr->list);
3448 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303449 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303450 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303451
Kashyap, Desai1278b112010-03-09 17:34:13 +05303452 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303453}
3454
3455/**
Eric Moore635374e2009-03-09 01:21:12 -06003456 * _scsih_check_topo_delete_events - sanity check on topo events
3457 * @ioc: per adapter object
3458 * @event_data: the event data payload
3459 *
3460 * This routine added to better handle cable breaker.
3461 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003462 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003463 * add and delete events in a single shot. When there is a delete event
3464 * the routine will void any pending add events waiting in the event queue.
3465 *
3466 * Return nothing.
3467 */
3468static void
3469_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3470 Mpi2EventDataSasTopologyChangeList_t *event_data)
3471{
3472 struct fw_event_work *fw_event;
3473 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3474 u16 expander_handle;
3475 struct _sas_node *sas_expander;
3476 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303477 int i, reason_code;
3478 u16 handle;
3479
3480 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303481 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3482 if (!handle)
3483 continue;
3484 reason_code = event_data->PHY[i].PhyStatus &
3485 MPI2_EVENT_SAS_TOPO_RC_MASK;
3486 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3487 _scsih_tm_tr_send(ioc, handle);
3488 }
Eric Moore635374e2009-03-09 01:21:12 -06003489
3490 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3491 if (expander_handle < ioc->sas_hba.num_phys) {
3492 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3493 return;
3494 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303495 if (event_data->ExpStatus ==
3496 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3497 /* put expander attached devices into blocking state */
Eric Moore635374e2009-03-09 01:21:12 -06003498 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3499 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3500 expander_handle);
Eric Moore635374e2009-03-09 01:21:12 -06003501 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303502 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3503 do {
3504 handle = find_first_bit(ioc->blocking_handles,
3505 ioc->facts.MaxDevHandle);
3506 if (handle < ioc->facts.MaxDevHandle)
3507 _scsih_block_io_device(ioc, handle);
3508 } while (test_and_clear_bit(handle, ioc->blocking_handles));
Eric Moore635374e2009-03-09 01:21:12 -06003509 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3510 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3511
3512 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3513 return;
3514
3515 /* mark ignore flag for pending events */
3516 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3517 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3518 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3519 fw_event->ignore)
3520 continue;
Joe Lawrence00713ad2014-06-25 17:03:33 -04003521 local_event_data = (Mpi2EventDataSasTopologyChangeList_t *)
3522 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06003523 if (local_event_data->ExpStatus ==
3524 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3525 local_event_data->ExpStatus ==
3526 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3527 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3528 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303529 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003530 "setting ignoring flag\n", ioc->name));
3531 fw_event->ignore = 1;
3532 }
3533 }
3534 }
3535 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3536}
3537
3538/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303539 * _scsih_set_volume_delete_flag - setting volume delete flag
3540 * @ioc: per adapter object
3541 * @handle: device handle
3542 *
3543 * This
3544 * Return nothing.
3545 */
3546static void
3547_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3548{
3549 struct _raid_device *raid_device;
3550 struct MPT2SAS_TARGET *sas_target_priv_data;
3551 unsigned long flags;
3552
3553 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3554 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3555 if (raid_device && raid_device->starget &&
3556 raid_device->starget->hostdata) {
3557 sas_target_priv_data =
3558 raid_device->starget->hostdata;
3559 sas_target_priv_data->deleted = 1;
3560 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3561 "setting delete flag: handle(0x%04x), "
3562 "wwid(0x%016llx)\n", ioc->name, handle,
3563 (unsigned long long) raid_device->wwid));
3564 }
3565 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3566}
3567
3568/**
3569 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3570 * @handle: input handle
3571 * @a: handle for volume a
3572 * @b: handle for volume b
3573 *
3574 * IR firmware only supports two raid volumes. The purpose of this
3575 * routine is to set the volume handle in either a or b. When the given
3576 * input handle is non-zero, or when a and b have not been set before.
3577 */
3578static void
3579_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3580{
3581 if (!handle || handle == *a || handle == *b)
3582 return;
3583 if (!*a)
3584 *a = handle;
3585 else if (!*b)
3586 *b = handle;
3587}
3588
3589/**
3590 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3591 * @ioc: per adapter object
3592 * @event_data: the event data payload
3593 * Context: interrupt time.
3594 *
3595 * This routine will send target reset to volume, followed by target
3596 * resets to the PDs. This is called when a PD has been removed, or
3597 * volume has been deleted or removed. When the target reset is sent
3598 * to volume, the PD target resets need to be queued to start upon
3599 * completion of the volume target reset.
3600 *
3601 * Return nothing.
3602 */
3603static void
3604_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3605 Mpi2EventDataIrConfigChangeList_t *event_data)
3606{
3607 Mpi2EventIrConfigElement_t *element;
3608 int i;
3609 u16 handle, volume_handle, a, b;
3610 struct _tr_list *delayed_tr;
3611
3612 a = 0;
3613 b = 0;
3614
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303615 if (ioc->is_warpdrive)
3616 return;
3617
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303618 /* Volume Resets for Deleted or Removed */
3619 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3620 for (i = 0; i < event_data->NumElements; i++, element++) {
3621 if (element->ReasonCode ==
3622 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3623 element->ReasonCode ==
3624 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3625 volume_handle = le16_to_cpu(element->VolDevHandle);
3626 _scsih_set_volume_delete_flag(ioc, volume_handle);
3627 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3628 }
3629 }
3630
3631 /* Volume Resets for UNHIDE events */
3632 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3633 for (i = 0; i < event_data->NumElements; i++, element++) {
3634 if (le32_to_cpu(event_data->Flags) &
3635 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3636 continue;
3637 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3638 volume_handle = le16_to_cpu(element->VolDevHandle);
3639 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3640 }
3641 }
3642
3643 if (a)
3644 _scsih_tm_tr_volume_send(ioc, a);
3645 if (b)
3646 _scsih_tm_tr_volume_send(ioc, b);
3647
3648 /* PD target resets */
3649 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3650 for (i = 0; i < event_data->NumElements; i++, element++) {
3651 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3652 continue;
3653 handle = le16_to_cpu(element->PhysDiskDevHandle);
3654 volume_handle = le16_to_cpu(element->VolDevHandle);
3655 clear_bit(handle, ioc->pd_handles);
3656 if (!volume_handle)
3657 _scsih_tm_tr_send(ioc, handle);
3658 else if (volume_handle == a || volume_handle == b) {
3659 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3660 BUG_ON(!delayed_tr);
3661 INIT_LIST_HEAD(&delayed_tr->list);
3662 delayed_tr->handle = handle;
3663 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3664 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3665 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3666 handle));
3667 } else
3668 _scsih_tm_tr_send(ioc, handle);
3669 }
3670}
3671
3672
3673/**
3674 * _scsih_check_volume_delete_events - set delete flag for volumes
3675 * @ioc: per adapter object
3676 * @event_data: the event data payload
3677 * Context: interrupt time.
3678 *
3679 * This will handle the case when the cable connected to entire volume is
3680 * pulled. We will take care of setting the deleted flag so normal IO will
3681 * not be sent.
3682 *
3683 * Return nothing.
3684 */
3685static void
3686_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3687 Mpi2EventDataIrVolume_t *event_data)
3688{
3689 u32 state;
3690
3691 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3692 return;
3693 state = le32_to_cpu(event_data->NewValue);
3694 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3695 MPI2_RAID_VOL_STATE_FAILED)
3696 _scsih_set_volume_delete_flag(ioc,
3697 le16_to_cpu(event_data->VolDevHandle));
3698}
3699
3700/**
Eric Moore635374e2009-03-09 01:21:12 -06003701 * _scsih_flush_running_cmds - completing outstanding commands.
3702 * @ioc: per adapter object
3703 *
3704 * The flushing out of all pending scmd commands following host reset,
3705 * where all IO is dropped to the floor.
3706 *
3707 * Return nothing.
3708 */
3709static void
3710_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3711{
3712 struct scsi_cmnd *scmd;
3713 u16 smid;
3714 u16 count = 0;
3715
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303716 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303717 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003718 if (!scmd)
3719 continue;
3720 count++;
3721 mpt2sas_base_free_smid(ioc, smid);
3722 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003723 if (ioc->pci_error_recovery)
3724 scmd->result = DID_NO_CONNECT << 16;
3725 else
3726 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003727 scmd->scsi_done(scmd);
3728 }
3729 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3730 ioc->name, count));
3731}
3732
3733/**
Eric Moore3c621b32009-05-18 12:59:41 -06003734 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3735 * @scmd: pointer to scsi command object
3736 * @mpi_request: pointer to the SCSI_IO reqest message frame
3737 *
3738 * Supporting protection 1 and 3.
3739 *
3740 * Returns nothing
3741 */
3742static void
3743_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3744{
3745 u16 eedp_flags;
3746 unsigned char prot_op = scsi_get_prot_op(scmd);
3747 unsigned char prot_type = scsi_get_prot_type(scmd);
3748
Eric Moored334aa72010-04-22 10:47:40 -06003749 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003750 return;
3751
3752 if (prot_op == SCSI_PROT_READ_STRIP)
3753 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3754 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3755 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3756 else
3757 return;
3758
Eric Moore3c621b32009-05-18 12:59:41 -06003759 switch (prot_type) {
3760 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003761 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003762
3763 /*
3764 * enable ref/guard checking
3765 * auto increment ref tag
3766 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303767 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003768 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3769 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3770 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3771 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003772 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003773
Eric Moore3c621b32009-05-18 12:59:41 -06003774 case SCSI_PROT_DIF_TYPE3:
3775
3776 /*
3777 * enable guard checking
3778 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303779 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003780 break;
3781 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303782 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3783 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003784}
3785
3786/**
3787 * _scsih_eedp_error_handling - return sense code for EEDP errors
3788 * @scmd: pointer to scsi command object
3789 * @ioc_status: ioc status
3790 *
3791 * Returns nothing
3792 */
3793static void
3794_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3795{
3796 u8 ascq;
Eric Moore3c621b32009-05-18 12:59:41 -06003797
3798 switch (ioc_status) {
3799 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3800 ascq = 0x01;
3801 break;
3802 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3803 ascq = 0x02;
3804 break;
3805 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3806 ascq = 0x03;
3807 break;
3808 default:
3809 ascq = 0x00;
3810 break;
3811 }
3812
Martin K. Petersen9b5cd132012-08-28 14:29:36 -04003813 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10, ascq);
3814 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
Eric Moore3c621b32009-05-18 12:59:41 -06003815 SAM_STAT_CHECK_CONDITION;
3816}
3817
3818/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303819 * _scsih_scsi_direct_io_get - returns direct io flag
3820 * @ioc: per adapter object
3821 * @smid: system request message index
3822 *
3823 * Returns the smid stored scmd pointer.
3824 */
3825static inline u8
3826_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3827{
3828 return ioc->scsi_lookup[smid - 1].direct_io;
3829}
3830
3831/**
3832 * _scsih_scsi_direct_io_set - sets direct io flag
3833 * @ioc: per adapter object
3834 * @smid: system request message index
3835 * @direct_io: Zero or non-zero value to set in the direct_io flag
3836 *
3837 * Returns Nothing.
3838 */
3839static inline void
3840_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3841{
3842 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3843}
3844
3845
3846/**
3847 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3848 * @ioc: per adapter object
3849 * @scmd: pointer to scsi command object
3850 * @raid_device: pointer to raid device data structure
3851 * @mpi_request: pointer to the SCSI_IO reqest message frame
3852 * @smid: system request message index
3853 *
3854 * Returns nothing
3855 */
3856static void
3857_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3858 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3859 u16 smid)
3860{
3861 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3862 u32 stripe_sz, stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303863 u8 num_pds, *cdb_ptr, i;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303864 u8 cdb0 = scmd->cmnd[0];
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303865 u64 v_llba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303866
3867 /*
3868 * Try Direct I/O to RAID memeber disks
3869 */
3870 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3871 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3872 cdb_ptr = mpi_request->CDB.CDB32;
3873
3874 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3875 | cdb_ptr[5])) {
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303876 io_size = scsi_bufflen(scmd) >>
3877 raid_device->block_exponent;
3878 i = (cdb0 < READ_16) ? 2 : 6;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303879 /* get virtual lba */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303880 v_lba = be32_to_cpu(*(__be32 *)(&cdb_ptr[i]));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303881
3882 if (((u64)v_lba + (u64)io_size - 1) <=
3883 (u32)raid_device->max_lba) {
3884 stripe_sz = raid_device->stripe_sz;
3885 stripe_exp = raid_device->stripe_exponent;
3886 stripe_off = v_lba & (stripe_sz - 1);
3887
3888 /* Check whether IO falls within a stripe */
3889 if ((stripe_off + io_size) <= stripe_sz) {
3890 num_pds = raid_device->num_pds;
3891 p_lba = v_lba >> stripe_exp;
3892 stripe_unit = p_lba / num_pds;
3893 column = p_lba % num_pds;
3894 p_lba = (stripe_unit << stripe_exp) +
3895 stripe_off;
3896 mpi_request->DevHandle =
3897 cpu_to_le16(raid_device->
3898 pd_handle[column]);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303899 (*(__be32 *)(&cdb_ptr[i])) =
3900 cpu_to_be32(p_lba);
3901 /*
3902 * WD: To indicate this I/O is directI/O
3903 */
3904 _scsih_scsi_direct_io_set(ioc, smid, 1);
3905 }
3906 }
3907 } else {
3908 io_size = scsi_bufflen(scmd) >>
3909 raid_device->block_exponent;
3910 /* get virtual lba */
3911 v_llba = be64_to_cpu(*(__be64 *)(&cdb_ptr[2]));
3912
3913 if ((v_llba + (u64)io_size - 1) <=
3914 raid_device->max_lba) {
3915 stripe_sz = raid_device->stripe_sz;
3916 stripe_exp = raid_device->stripe_exponent;
3917 stripe_off = (u32) (v_llba & (stripe_sz - 1));
3918
3919 /* Check whether IO falls within a stripe */
3920 if ((stripe_off + io_size) <= stripe_sz) {
3921 num_pds = raid_device->num_pds;
3922 p_lba = (u32)(v_llba >> stripe_exp);
3923 stripe_unit = p_lba / num_pds;
3924 column = p_lba % num_pds;
3925 p_lba = (stripe_unit << stripe_exp) +
3926 stripe_off;
3927 mpi_request->DevHandle =
3928 cpu_to_le16(raid_device->
3929 pd_handle[column]);
3930 (*(__be64 *)(&cdb_ptr[2])) =
3931 cpu_to_be64((u64)p_lba);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303932 /*
3933 * WD: To indicate this I/O is directI/O
3934 */
3935 _scsih_scsi_direct_io_set(ioc, smid, 1);
3936 }
3937 }
3938 }
3939 }
3940}
3941
3942/**
Eric Moored5d135b2009-05-18 13:02:08 -06003943 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003944 * @scmd: pointer to scsi command object
3945 * @done: function pointer to be invoked on completion
3946 *
3947 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3948 *
3949 * Returns 0 on success. If there's a failure, return either:
3950 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3951 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3952 */
3953static int
Matthew Wilcox90f951e2014-03-27 16:40:33 -04003954_scsih_qcmd(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06003955{
Matthew Wilcox90f951e2014-03-27 16:40:33 -04003956 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Eric Moore635374e2009-03-09 01:21:12 -06003957 struct MPT2SAS_DEVICE *sas_device_priv_data;
3958 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303959 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003960 Mpi2SCSIIORequest_t *mpi_request;
3961 u32 mpi_control;
3962 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003963
Eric Moore635374e2009-03-09 01:21:12 -06003964 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303965 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003966 scmd->result = DID_NO_CONNECT << 16;
3967 scmd->scsi_done(scmd);
3968 return 0;
3969 }
3970
Kashyap, Desai78215782011-06-14 10:57:08 +05303971 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003972 scmd->result = DID_NO_CONNECT << 16;
3973 scmd->scsi_done(scmd);
3974 return 0;
3975 }
3976
Eric Moore635374e2009-03-09 01:21:12 -06003977 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303978 /* invalid device handle */
3979 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003980 scmd->result = DID_NO_CONNECT << 16;
3981 scmd->scsi_done(scmd);
3982 return 0;
3983 }
3984
Kashyap, Desai130b9582010-04-08 17:54:32 +05303985 /* host recovery or link resets sent via IOCTLs */
3986 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003987 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003988 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303989 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3990 return SCSI_MLQUEUE_DEVICE_BUSY;
3991 /* device has been deleted */
3992 else if (sas_target_priv_data->deleted) {
3993 scmd->result = DID_NO_CONNECT << 16;
3994 scmd->scsi_done(scmd);
3995 return 0;
3996 }
Eric Moore635374e2009-03-09 01:21:12 -06003997
3998 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3999 mpi_control = MPI2_SCSIIO_CONTROL_READ;
4000 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
4001 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
4002 else
4003 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
4004
4005 /* set tags */
4006 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
4007 if (scmd->device->tagged_supported) {
4008 if (scmd->device->ordered_tags)
4009 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
4010 else
4011 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
4012 } else
Sreekanth Reddy48ba2ef2013-02-02 00:58:20 +05304013 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Eric Moore635374e2009-03-09 01:21:12 -06004014 } else
4015 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304016 /* Make sure Device is not raid volume.
4017 * We do not expose raid functionality to upper layer for warpdrive.
4018 */
4019 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06004020 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06004021 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4022
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304023 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06004024 if (!smid) {
4025 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
4026 ioc->name, __func__);
4027 goto out;
4028 }
4029 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4030 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06004031 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06004032 if (scmd->cmd_len == 32)
4033 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06004034 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4035 if (sas_device_priv_data->sas_target->flags &
4036 MPT_TARGET_FLAGS_RAID_COMPONENT)
4037 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4038 else
Matthew Wilcox90f951e2014-03-27 16:40:33 -04004039 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Eric Moore635374e2009-03-09 01:21:12 -06004040 mpi_request->DevHandle =
4041 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4042 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4043 mpi_request->Control = cpu_to_le32(mpi_control);
4044 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4045 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4046 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4047 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05304048 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004049 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4050 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
4051 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304052 mpi_request->VF_ID = 0; /* TODO */
4053 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004054 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4055 mpi_request->LUN);
4056 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4057
4058 if (!mpi_request->DataLength) {
4059 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
4060 } else {
4061 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
4062 mpt2sas_base_free_smid(ioc, smid);
4063 goto out;
4064 }
4065 }
4066
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304067 raid_device = sas_target_priv_data->raid_device;
4068 if (raid_device && raid_device->direct_io_enabled)
4069 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4070 smid);
4071
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304072 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4073 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304074 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304075 else
4076 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004077 return 0;
4078
4079 out:
4080 return SCSI_MLQUEUE_HOST_BUSY;
4081}
4082
4083/**
4084 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4085 * @sense_buffer: sense data returned by target
4086 * @data: normalized skey/asc/ascq
4087 *
4088 * Return nothing.
4089 */
4090static void
4091_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4092{
4093 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4094 /* descriptor format */
4095 data->skey = sense_buffer[1] & 0x0F;
4096 data->asc = sense_buffer[2];
4097 data->ascq = sense_buffer[3];
4098 } else {
4099 /* fixed format */
4100 data->skey = sense_buffer[2] & 0x0F;
4101 data->asc = sense_buffer[12];
4102 data->ascq = sense_buffer[13];
4103 }
4104}
4105
4106#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4107/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004108 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004109 * @ioc: per adapter object
4110 * @scmd: pointer to scsi command object
4111 * @mpi_reply: reply mf payload returned from firmware
4112 *
4113 * scsi_status - SCSI Status code returned from target device
4114 * scsi_state - state info associated with SCSI_IO determined by ioc
4115 * ioc_status - ioc supplied status info
4116 *
4117 * Return nothing.
4118 */
4119static void
4120_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4121 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4122{
4123 u32 response_info;
4124 u8 *response_bytes;
4125 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4126 MPI2_IOCSTATUS_MASK;
4127 u8 scsi_state = mpi_reply->SCSIState;
4128 u8 scsi_status = mpi_reply->SCSIStatus;
4129 char *desc_ioc_state = NULL;
4130 char *desc_scsi_status = NULL;
4131 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304132 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304133 struct _sas_device *sas_device = NULL;
4134 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304135 struct scsi_target *starget = scmd->device->sdev_target;
4136 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304137 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304138
4139 if (!priv_target)
4140 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304141
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304142 if (ioc->hide_ir_msg)
4143 device_str = "WarpDrive";
4144 else
4145 device_str = "volume";
4146
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304147 if (log_info == 0x31170000)
4148 return;
Eric Moore635374e2009-03-09 01:21:12 -06004149
4150 switch (ioc_status) {
4151 case MPI2_IOCSTATUS_SUCCESS:
4152 desc_ioc_state = "success";
4153 break;
4154 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4155 desc_ioc_state = "invalid function";
4156 break;
4157 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4158 desc_ioc_state = "scsi recovered error";
4159 break;
4160 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4161 desc_ioc_state = "scsi invalid dev handle";
4162 break;
4163 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4164 desc_ioc_state = "scsi device not there";
4165 break;
4166 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4167 desc_ioc_state = "scsi data overrun";
4168 break;
4169 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4170 desc_ioc_state = "scsi data underrun";
4171 break;
4172 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4173 desc_ioc_state = "scsi io data error";
4174 break;
4175 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4176 desc_ioc_state = "scsi protocol error";
4177 break;
4178 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4179 desc_ioc_state = "scsi task terminated";
4180 break;
4181 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4182 desc_ioc_state = "scsi residual mismatch";
4183 break;
4184 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4185 desc_ioc_state = "scsi task mgmt failed";
4186 break;
4187 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4188 desc_ioc_state = "scsi ioc terminated";
4189 break;
4190 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4191 desc_ioc_state = "scsi ext terminated";
4192 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004193 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4194 desc_ioc_state = "eedp guard error";
4195 break;
4196 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4197 desc_ioc_state = "eedp ref tag error";
4198 break;
4199 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4200 desc_ioc_state = "eedp app tag error";
4201 break;
Eric Moore635374e2009-03-09 01:21:12 -06004202 default:
4203 desc_ioc_state = "unknown";
4204 break;
4205 }
4206
4207 switch (scsi_status) {
4208 case MPI2_SCSI_STATUS_GOOD:
4209 desc_scsi_status = "good";
4210 break;
4211 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4212 desc_scsi_status = "check condition";
4213 break;
4214 case MPI2_SCSI_STATUS_CONDITION_MET:
4215 desc_scsi_status = "condition met";
4216 break;
4217 case MPI2_SCSI_STATUS_BUSY:
4218 desc_scsi_status = "busy";
4219 break;
4220 case MPI2_SCSI_STATUS_INTERMEDIATE:
4221 desc_scsi_status = "intermediate";
4222 break;
4223 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4224 desc_scsi_status = "intermediate condmet";
4225 break;
4226 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4227 desc_scsi_status = "reservation conflict";
4228 break;
4229 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4230 desc_scsi_status = "command terminated";
4231 break;
4232 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4233 desc_scsi_status = "task set full";
4234 break;
4235 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4236 desc_scsi_status = "aca active";
4237 break;
4238 case MPI2_SCSI_STATUS_TASK_ABORTED:
4239 desc_scsi_status = "task aborted";
4240 break;
4241 default:
4242 desc_scsi_status = "unknown";
4243 break;
4244 }
4245
4246 desc_scsi_state[0] = '\0';
4247 if (!scsi_state)
4248 desc_scsi_state = " ";
4249 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4250 strcat(desc_scsi_state, "response info ");
4251 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4252 strcat(desc_scsi_state, "state terminated ");
4253 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4254 strcat(desc_scsi_state, "no status ");
4255 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4256 strcat(desc_scsi_state, "autosense failed ");
4257 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4258 strcat(desc_scsi_state, "autosense valid ");
4259
4260 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304261
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304262 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304263 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4264 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304265 } else {
4266 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4267 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4268 priv_target->sas_address);
4269 if (sas_device) {
4270 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4271 "phy(%d)\n", ioc->name, sas_device->sas_address,
4272 sas_device->phy);
4273 printk(MPT2SAS_WARN_FMT
4274 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4275 ioc->name, sas_device->enclosure_logical_id,
4276 sas_device->slot);
4277 }
4278 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304279 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304280
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304281 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4282 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4283 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004284 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4285 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4286 scsi_get_resid(scmd));
4287 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4288 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4289 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4290 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4291 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4292 scsi_status, desc_scsi_state, scsi_state);
4293
4294 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4295 struct sense_info data;
4296 _scsih_normalize_sense(scmd->sense_buffer, &data);
4297 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304298 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4299 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004300 }
4301
4302 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4303 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4304 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304305 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004306 }
4307}
4308#endif
4309
4310/**
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304311 * _scsih_turn_on_pfa_led - illuminate PFA LED
Eric Moore635374e2009-03-09 01:21:12 -06004312 * @ioc: per adapter object
4313 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304314 * Context: process
4315 *
4316 * Return nothing.
4317 */
4318static void
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304319_scsih_turn_on_pfa_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304320{
4321 Mpi2SepReply_t mpi_reply;
4322 Mpi2SepRequest_t mpi_request;
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304323 struct _sas_device *sas_device;
4324
4325 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4326 if (!sas_device)
4327 return;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304328
4329 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4330 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4331 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4332 mpi_request.SlotStatus =
4333 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4334 mpi_request.DevHandle = cpu_to_le16(handle);
4335 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4336 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4337 &mpi_request)) != 0) {
4338 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4339 __FILE__, __LINE__, __func__);
4340 return;
4341 }
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304342 sas_device->pfa_led_on = 1;
4343
4344
4345 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4346 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
4347 "enclosure_processor: ioc_status (0x%04x), loginfo(0x%08x)\n",
4348 ioc->name, le16_to_cpu(mpi_reply.IOCStatus),
4349 le32_to_cpu(mpi_reply.IOCLogInfo)));
4350 return;
4351 }
4352}
4353
4354/**
4355 * _scsih_turn_off_pfa_led - turn off PFA LED
4356 * @ioc: per adapter object
4357 * @sas_device: sas device whose PFA LED has to turned off
4358 * Context: process
4359 *
4360 * Return nothing.
4361 */
4362static void
4363_scsih_turn_off_pfa_led(struct MPT2SAS_ADAPTER *ioc,
4364 struct _sas_device *sas_device)
4365{
4366 Mpi2SepReply_t mpi_reply;
4367 Mpi2SepRequest_t mpi_request;
4368
4369 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4370 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4371 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4372 mpi_request.SlotStatus = 0;
4373 mpi_request.Slot = cpu_to_le16(sas_device->slot);
4374 mpi_request.DevHandle = 0;
4375 mpi_request.EnclosureHandle = cpu_to_le16(sas_device->enclosure_handle);
4376 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_ENCLOSURE_SLOT_ADDRESS;
4377 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4378 &mpi_request)) != 0) {
4379 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4380 __FILE__, __LINE__, __func__);
4381 return;
4382 }
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304383
4384 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4385 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4386 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4387 le16_to_cpu(mpi_reply.IOCStatus),
4388 le32_to_cpu(mpi_reply.IOCLogInfo)));
4389 return;
4390 }
4391}
4392
4393/**
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304394 * _scsih_send_event_to_turn_on_pfa_led - fire delayed event
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304395 * @ioc: per adapter object
4396 * @handle: device handle
4397 * Context: interrupt.
4398 *
4399 * Return nothing.
4400 */
4401static void
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304402_scsih_send_event_to_turn_on_pfa_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304403{
4404 struct fw_event_work *fw_event;
4405
4406 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4407 if (!fw_event)
4408 return;
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304409 fw_event->event = MPT2SAS_TURN_ON_PFA_LED;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304410 fw_event->device_handle = handle;
4411 fw_event->ioc = ioc;
4412 _scsih_fw_event_add(ioc, fw_event);
4413}
4414
4415/**
4416 * _scsih_smart_predicted_fault - process smart errors
4417 * @ioc: per adapter object
4418 * @handle: device handle
4419 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004420 *
4421 * Return nothing.
4422 */
4423static void
4424_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4425{
Eric Moore635374e2009-03-09 01:21:12 -06004426 struct scsi_target *starget;
4427 struct MPT2SAS_TARGET *sas_target_priv_data;
4428 Mpi2EventNotificationReply_t *event_reply;
4429 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4430 struct _sas_device *sas_device;
4431 ssize_t sz;
4432 unsigned long flags;
4433
4434 /* only handle non-raid devices */
4435 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4436 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4437 if (!sas_device) {
4438 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4439 return;
4440 }
4441 starget = sas_device->starget;
4442 sas_target_priv_data = starget->hostdata;
4443
4444 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4445 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4446 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4447 return;
4448 }
4449 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4450 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4451
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304452 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05304453 _scsih_send_event_to_turn_on_pfa_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004454
4455 /* insert into event log */
4456 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4457 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b42011-11-07 22:05:21 +11004458 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004459 if (!event_reply) {
4460 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4461 ioc->name, __FILE__, __LINE__, __func__);
4462 return;
4463 }
4464
4465 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4466 event_reply->Event =
4467 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4468 event_reply->MsgLength = sz/4;
4469 event_reply->EventDataLength =
4470 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4471 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4472 event_reply->EventData;
4473 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4474 event_data->ASC = 0x5D;
4475 event_data->DevHandle = cpu_to_le16(handle);
4476 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4477 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4478 kfree(event_reply);
4479}
4480
4481/**
Eric Moored5d135b2009-05-18 13:02:08 -06004482 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004483 * @ioc: per adapter object
4484 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304485 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004486 * @reply: reply message frame(lower 32bit addr)
4487 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304488 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004489 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304490 * Return 1 meaning mf should be freed from _base_interrupt
4491 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004492 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304493static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304494_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004495{
4496 Mpi2SCSIIORequest_t *mpi_request;
4497 Mpi2SCSIIOReply_t *mpi_reply;
4498 struct scsi_cmnd *scmd;
4499 u16 ioc_status;
4500 u32 xfer_cnt;
4501 u8 scsi_state;
4502 u8 scsi_status;
4503 u32 log_info;
4504 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304505 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304506 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004507
4508 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304509 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004510 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304511 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004512
4513 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4514
4515 if (mpi_reply == NULL) {
4516 scmd->result = DID_OK << 16;
4517 goto out;
4518 }
4519
4520 sas_device_priv_data = scmd->device->hostdata;
4521 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4522 sas_device_priv_data->sas_target->deleted) {
4523 scmd->result = DID_NO_CONNECT << 16;
4524 goto out;
4525 }
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304526 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304527 /*
4528 * WARPDRIVE: If direct_io is set then it is directIO,
4529 * the failed direct I/O should be redirected to volume
4530 */
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304531 if (_scsih_scsi_direct_io_get(ioc, smid) &&
4532 ((ioc_status & MPI2_IOCSTATUS_MASK)
4533 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304534 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4535 ioc->scsi_lookup[smid - 1].scmd = scmd;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304536 _scsih_scsi_direct_io_set(ioc, smid, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304537 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304538 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4539 mpi_request->DevHandle =
4540 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4541 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4542 sas_device_priv_data->sas_target->handle);
4543 return 0;
4544 }
4545
Eric Moore635374e2009-03-09 01:21:12 -06004546
4547 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304548 scsi_state = mpi_reply->SCSIState;
4549 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4550 response_code =
4551 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004552 if (!sas_device_priv_data->tlr_snoop_check) {
4553 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304554 /* Make sure Device is not raid volume.
4555 * We do not expose raid functionality to upper layer for warpdrive.
4556 */
4557 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304558 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304559 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4560 sas_disable_tlr(scmd->device);
4561 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4562 }
Eric Moore635374e2009-03-09 01:21:12 -06004563 }
4564
4565 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4566 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
Eric Moore635374e2009-03-09 01:21:12 -06004567 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4568 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4569 else
4570 log_info = 0;
4571 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004572 scsi_status = mpi_reply->SCSIStatus;
4573
4574 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4575 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4576 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4577 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4578 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4579 }
4580
4581 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4582 struct sense_info data;
4583 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4584 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004585 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004586 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004587 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004588 _scsih_normalize_sense(scmd->sense_buffer, &data);
4589 /* failure prediction threshold exceeded */
4590 if (data.asc == 0x5D)
4591 _scsih_smart_predicted_fault(ioc,
4592 le16_to_cpu(mpi_reply->DevHandle));
4593 }
4594
4595 switch (ioc_status) {
4596 case MPI2_IOCSTATUS_BUSY:
4597 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4598 scmd->result = SAM_STAT_BUSY;
4599 break;
4600
4601 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4602 scmd->result = DID_NO_CONNECT << 16;
4603 break;
4604
4605 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4606 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304607 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4608 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004609 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304610 scmd->result = DID_SOFT_ERROR << 16;
4611 break;
Eric Moore635374e2009-03-09 01:21:12 -06004612 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4613 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4614 scmd->result = DID_RESET << 16;
4615 break;
4616
4617 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4618 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4619 scmd->result = DID_SOFT_ERROR << 16;
4620 else
4621 scmd->result = (DID_OK << 16) | scsi_status;
4622 break;
4623
4624 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4625 scmd->result = (DID_OK << 16) | scsi_status;
4626
4627 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4628 break;
4629
4630 if (xfer_cnt < scmd->underflow) {
4631 if (scsi_status == SAM_STAT_BUSY)
4632 scmd->result = SAM_STAT_BUSY;
4633 else
4634 scmd->result = DID_SOFT_ERROR << 16;
4635 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4636 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4637 scmd->result = DID_SOFT_ERROR << 16;
4638 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4639 scmd->result = DID_RESET << 16;
4640 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4641 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4642 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4643 scmd->result = (DRIVER_SENSE << 24) |
4644 SAM_STAT_CHECK_CONDITION;
4645 scmd->sense_buffer[0] = 0x70;
4646 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4647 scmd->sense_buffer[12] = 0x20;
4648 scmd->sense_buffer[13] = 0;
4649 }
4650 break;
4651
4652 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4653 scsi_set_resid(scmd, 0);
4654 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4655 case MPI2_IOCSTATUS_SUCCESS:
4656 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304657 if (response_code ==
4658 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4659 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4660 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004661 scmd->result = DID_SOFT_ERROR << 16;
4662 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4663 scmd->result = DID_RESET << 16;
4664 break;
4665
Eric Moore3c621b32009-05-18 12:59:41 -06004666 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4667 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4668 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4669 _scsih_eedp_error_handling(scmd, ioc_status);
4670 break;
Eric Moore635374e2009-03-09 01:21:12 -06004671 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4672 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4673 case MPI2_IOCSTATUS_INVALID_SGL:
4674 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4675 case MPI2_IOCSTATUS_INVALID_FIELD:
4676 case MPI2_IOCSTATUS_INVALID_STATE:
4677 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4678 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4679 default:
4680 scmd->result = DID_SOFT_ERROR << 16;
4681 break;
4682
4683 }
4684
4685#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4686 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4687 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4688#endif
4689
4690 out:
4691 scsi_dma_unmap(scmd);
4692 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304693 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004694}
4695
4696/**
Eric Moore635374e2009-03-09 01:21:12 -06004697 * _scsih_sas_host_refresh - refreshing sas host object contents
4698 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004699 * Context: user
4700 *
4701 * During port enable, fw will send topology events for every device. Its
4702 * possible that the handles may change from the previous setting, so this
4703 * code keeping handles updating if changed.
4704 *
4705 * Return nothing.
4706 */
4707static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304708_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004709{
4710 u16 sz;
4711 u16 ioc_status;
4712 int i;
4713 Mpi2ConfigReply_t mpi_reply;
4714 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304715 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304716 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004717
4718 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4719 "updating handles for sas_host(0x%016llx)\n",
4720 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4721
4722 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4723 * sizeof(Mpi2SasIOUnit0PhyData_t));
4724 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4725 if (!sas_iounit_pg0) {
4726 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4727 ioc->name, __FILE__, __LINE__, __func__);
4728 return;
4729 }
Eric Moore635374e2009-03-09 01:21:12 -06004730
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304731 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4732 sas_iounit_pg0, sz)) != 0)
4733 goto out;
4734 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4735 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4736 goto out;
4737 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304738 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304739 if (i == 0)
4740 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4741 PhyData[0].ControllerDevHandle);
4742 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4743 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4744 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304745 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4746 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304747 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304748 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304749 }
Eric Moore635374e2009-03-09 01:21:12 -06004750 out:
4751 kfree(sas_iounit_pg0);
4752}
4753
4754/**
4755 * _scsih_sas_host_add - create sas host object
4756 * @ioc: per adapter object
4757 *
4758 * Creating host side data object, stored in ioc->sas_hba
4759 *
4760 * Return nothing.
4761 */
4762static void
4763_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4764{
4765 int i;
4766 Mpi2ConfigReply_t mpi_reply;
4767 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4768 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4769 Mpi2SasPhyPage0_t phy_pg0;
4770 Mpi2SasDevicePage0_t sas_device_pg0;
4771 Mpi2SasEnclosurePage0_t enclosure_pg0;
4772 u16 ioc_status;
4773 u16 sz;
4774 u16 device_missing_delay;
4775
4776 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4777 if (!ioc->sas_hba.num_phys) {
4778 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4779 ioc->name, __FILE__, __LINE__, __func__);
4780 return;
4781 }
4782
4783 /* sas_iounit page 0 */
4784 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4785 sizeof(Mpi2SasIOUnit0PhyData_t));
4786 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4787 if (!sas_iounit_pg0) {
4788 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4789 ioc->name, __FILE__, __LINE__, __func__);
4790 return;
4791 }
4792 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4793 sas_iounit_pg0, sz))) {
4794 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4795 ioc->name, __FILE__, __LINE__, __func__);
4796 goto out;
4797 }
4798 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4799 MPI2_IOCSTATUS_MASK;
4800 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4801 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4802 ioc->name, __FILE__, __LINE__, __func__);
4803 goto out;
4804 }
4805
4806 /* sas_iounit page 1 */
4807 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4808 sizeof(Mpi2SasIOUnit1PhyData_t));
4809 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4810 if (!sas_iounit_pg1) {
4811 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4812 ioc->name, __FILE__, __LINE__, __func__);
4813 goto out;
4814 }
4815 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4816 sas_iounit_pg1, sz))) {
4817 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4818 ioc->name, __FILE__, __LINE__, __func__);
4819 goto out;
4820 }
4821 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4822 MPI2_IOCSTATUS_MASK;
4823 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4824 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4825 ioc->name, __FILE__, __LINE__, __func__);
4826 goto out;
4827 }
4828
4829 ioc->io_missing_delay =
4830 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4831 device_missing_delay =
4832 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4833 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4834 ioc->device_missing_delay = (device_missing_delay &
4835 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4836 else
4837 ioc->device_missing_delay = device_missing_delay &
4838 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4839
4840 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4841 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4842 sizeof(struct _sas_phy), GFP_KERNEL);
4843 if (!ioc->sas_hba.phy) {
4844 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4845 ioc->name, __FILE__, __LINE__, __func__);
4846 goto out;
4847 }
4848 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4849 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4850 i))) {
4851 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4852 ioc->name, __FILE__, __LINE__, __func__);
4853 goto out;
4854 }
4855 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4856 MPI2_IOCSTATUS_MASK;
4857 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4858 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4859 ioc->name, __FILE__, __LINE__, __func__);
4860 goto out;
4861 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304862
4863 if (i == 0)
4864 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4865 PhyData[0].ControllerDevHandle);
4866 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004867 ioc->sas_hba.phy[i].phy_id = i;
4868 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4869 phy_pg0, ioc->sas_hba.parent_dev);
4870 }
4871 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304872 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004873 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4874 ioc->name, __FILE__, __LINE__, __func__);
4875 goto out;
4876 }
Eric Moore635374e2009-03-09 01:21:12 -06004877 ioc->sas_hba.enclosure_handle =
4878 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4879 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4880 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4881 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4882 (unsigned long long) ioc->sas_hba.sas_address,
4883 ioc->sas_hba.num_phys) ;
4884
4885 if (ioc->sas_hba.enclosure_handle) {
4886 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4887 &enclosure_pg0,
4888 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4889 ioc->sas_hba.enclosure_handle))) {
4890 ioc->sas_hba.enclosure_logical_id =
4891 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4892 }
4893 }
4894
4895 out:
4896 kfree(sas_iounit_pg1);
4897 kfree(sas_iounit_pg0);
4898}
4899
4900/**
4901 * _scsih_expander_add - creating expander object
4902 * @ioc: per adapter object
4903 * @handle: expander handle
4904 *
4905 * Creating expander object, stored in ioc->sas_expander_list.
4906 *
4907 * Return 0 for success, else error.
4908 */
4909static int
4910_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4911{
4912 struct _sas_node *sas_expander;
4913 Mpi2ConfigReply_t mpi_reply;
4914 Mpi2ExpanderPage0_t expander_pg0;
4915 Mpi2ExpanderPage1_t expander_pg1;
4916 Mpi2SasEnclosurePage0_t enclosure_pg0;
4917 u32 ioc_status;
4918 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304919 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004920 int i;
4921 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304922 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004923 int rc = 0;
4924
4925 if (!handle)
4926 return -1;
4927
Eric Moore3cb54692010-07-08 14:44:34 -06004928 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304929 return -1;
4930
Eric Moore635374e2009-03-09 01:21:12 -06004931 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4932 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4933 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4934 ioc->name, __FILE__, __LINE__, __func__);
4935 return -1;
4936 }
4937
4938 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4939 MPI2_IOCSTATUS_MASK;
4940 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4941 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4942 ioc->name, __FILE__, __LINE__, __func__);
4943 return -1;
4944 }
4945
4946 /* handle out of order topology events */
4947 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304948 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4949 != 0) {
4950 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4951 ioc->name, __FILE__, __LINE__, __func__);
4952 return -1;
4953 }
4954 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004955 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304956 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4957 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004958 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4959 if (!sas_expander) {
4960 rc = _scsih_expander_add(ioc, parent_handle);
4961 if (rc != 0)
4962 return rc;
4963 }
4964 }
4965
Eric Moore635374e2009-03-09 01:21:12 -06004966 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304967 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004968 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4969 sas_address);
4970 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4971
4972 if (sas_expander)
4973 return 0;
4974
4975 sas_expander = kzalloc(sizeof(struct _sas_node),
4976 GFP_KERNEL);
4977 if (!sas_expander) {
4978 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4979 ioc->name, __FILE__, __LINE__, __func__);
4980 return -1;
4981 }
4982
4983 sas_expander->handle = handle;
4984 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304985 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004986 sas_expander->sas_address = sas_address;
4987
4988 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4989 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304990 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004991 sas_expander->sas_address, sas_expander->num_phys);
4992
4993 if (!sas_expander->num_phys)
4994 goto out_fail;
4995 sas_expander->phy = kcalloc(sas_expander->num_phys,
4996 sizeof(struct _sas_phy), GFP_KERNEL);
4997 if (!sas_expander->phy) {
4998 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4999 ioc->name, __FILE__, __LINE__, __func__);
5000 rc = -1;
5001 goto out_fail;
5002 }
5003
5004 INIT_LIST_HEAD(&sas_expander->sas_port_list);
5005 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305006 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005007 if (!mpt2sas_port) {
5008 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5009 ioc->name, __FILE__, __LINE__, __func__);
5010 rc = -1;
5011 goto out_fail;
5012 }
5013 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
5014
5015 for (i = 0 ; i < sas_expander->num_phys ; i++) {
5016 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
5017 &expander_pg1, i, handle))) {
5018 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5019 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05305020 rc = -1;
5021 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06005022 }
5023 sas_expander->phy[i].handle = handle;
5024 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05305025
5026 if ((mpt2sas_transport_add_expander_phy(ioc,
5027 &sas_expander->phy[i], expander_pg1,
5028 sas_expander->parent_dev))) {
5029 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5030 ioc->name, __FILE__, __LINE__, __func__);
5031 rc = -1;
5032 goto out_fail;
5033 }
Eric Moore635374e2009-03-09 01:21:12 -06005034 }
5035
5036 if (sas_expander->enclosure_handle) {
5037 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
5038 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5039 sas_expander->enclosure_handle))) {
5040 sas_expander->enclosure_logical_id =
5041 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
5042 }
5043 }
5044
5045 _scsih_expander_node_add(ioc, sas_expander);
5046 return 0;
5047
5048 out_fail:
5049
Kashyap, Desai20f58952009-08-07 19:34:26 +05305050 if (mpt2sas_port)
5051 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305052 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005053 kfree(sas_expander);
5054 return rc;
5055}
5056
5057/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05305058 * _scsih_done - scsih callback handler.
5059 * @ioc: per adapter object
5060 * @smid: system request message index
5061 * @msix_index: MSIX table index supplied by the OS
5062 * @reply: reply message frame(lower 32bit addr)
5063 *
5064 * Callback handler when sending internal generated message frames.
5065 * The callback index passed is `ioc->scsih_cb_idx`
5066 *
5067 * Return 1 meaning mf should be freed from _base_interrupt
5068 * 0 means the mf is freed from this function.
5069 */
5070static u8
5071_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5072{
5073 MPI2DefaultReply_t *mpi_reply;
5074
5075 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5076 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
5077 return 1;
5078 if (ioc->scsih_cmds.smid != smid)
5079 return 1;
5080 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
5081 if (mpi_reply) {
5082 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5083 mpi_reply->MsgLength*4);
5084 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
5085 }
5086 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
5087 complete(&ioc->scsih_cmds.done);
5088 return 1;
5089}
5090
5091/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305092 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06005093 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305094 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06005095 *
5096 * Return nothing.
5097 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305098void
5099mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06005100{
5101 struct _sas_node *sas_expander;
5102 unsigned long flags;
5103
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305104 if (ioc->shost_recovery)
5105 return;
5106
Eric Moore635374e2009-03-09 01:21:12 -06005107 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305108 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5109 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305110 if (sas_expander)
5111 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06005112 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305113 if (sas_expander)
5114 _scsih_expander_node_remove(ioc, sas_expander);
Eric Moore635374e2009-03-09 01:21:12 -06005115}
5116
5117/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05305118 * _scsih_check_access_status - check access flags
5119 * @ioc: per adapter object
5120 * @sas_address: sas address
5121 * @handle: sas device handle
5122 * @access_flags: errors returned during discovery of the device
5123 *
5124 * Return 0 for success, else failure
5125 */
5126static u8
5127_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5128 u16 handle, u8 access_status)
5129{
5130 u8 rc = 1;
5131 char *desc = NULL;
5132
5133 switch (access_status) {
5134 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5135 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5136 rc = 0;
5137 break;
5138 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5139 desc = "sata capability failed";
5140 break;
5141 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5142 desc = "sata affiliation conflict";
5143 break;
5144 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5145 desc = "route not addressable";
5146 break;
5147 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5148 desc = "smp error not addressable";
5149 break;
5150 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5151 desc = "device blocked";
5152 break;
5153 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5154 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5155 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5156 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5157 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5158 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5159 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5160 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5161 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5162 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5163 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5164 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5165 desc = "sata initialization failed";
5166 break;
5167 default:
5168 desc = "unknown";
5169 break;
5170 }
5171
5172 if (!rc)
5173 return 0;
5174
5175 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5176 "handle(0x%04x)\n", ioc->name, desc,
5177 (unsigned long long)sas_address, handle);
5178 return rc;
5179}
5180
5181static void
5182_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5183{
5184 Mpi2ConfigReply_t mpi_reply;
5185 Mpi2SasDevicePage0_t sas_device_pg0;
5186 struct _sas_device *sas_device;
5187 u32 ioc_status;
5188 unsigned long flags;
5189 u64 sas_address;
5190 struct scsi_target *starget;
5191 struct MPT2SAS_TARGET *sas_target_priv_data;
5192 u32 device_info;
5193
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305194
Kashyap, Desaib4344272010-03-17 16:24:14 +05305195 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5196 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5197 return;
5198
5199 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5200 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5201 return;
5202
5203 /* check if this is end device */
5204 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5205 if (!(_scsih_is_end_device(device_info)))
5206 return;
5207
5208 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5209 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5210 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5211 sas_address);
5212
5213 if (!sas_device) {
5214 printk(MPT2SAS_ERR_FMT "device is not present "
5215 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5216 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5217 return;
5218 }
5219
5220 if (unlikely(sas_device->handle != handle)) {
5221 starget = sas_device->starget;
5222 sas_target_priv_data = starget->hostdata;
5223 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5224 " to (0x%04x)!!!\n", sas_device->handle, handle);
5225 sas_target_priv_data->handle = handle;
5226 sas_device->handle = handle;
5227 }
Kashyap, Desaib4344272010-03-17 16:24:14 +05305228
5229 /* 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 "device is not present "
5233 "handle(0x%04x), flags!!!\n", ioc->name, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305234 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305235 return;
5236 }
5237
5238 /* check if there were any issues with discovery */
5239 if (_scsih_check_access_status(ioc, sas_address, handle,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305240 sas_device_pg0.AccessStatus)) {
5241 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305242 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305243 }
5244 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305245 _scsih_ublock_io_device(ioc, sas_address);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305246
5247}
5248
5249/**
Eric Moore635374e2009-03-09 01:21:12 -06005250 * _scsih_add_device - creating sas device object
5251 * @ioc: per adapter object
5252 * @handle: sas device handle
5253 * @phy_num: phy number end device attached to
5254 * @is_pd: is this hidden raid component
5255 *
5256 * Creating end device object, stored in ioc->sas_device_list.
5257 *
5258 * Returns 0 for success, non-zero for failure.
5259 */
5260static int
5261_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5262{
5263 Mpi2ConfigReply_t mpi_reply;
5264 Mpi2SasDevicePage0_t sas_device_pg0;
5265 Mpi2SasEnclosurePage0_t enclosure_pg0;
5266 struct _sas_device *sas_device;
5267 u32 ioc_status;
5268 __le64 sas_address;
5269 u32 device_info;
5270 unsigned long flags;
5271
5272 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5273 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5274 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5275 ioc->name, __FILE__, __LINE__, __func__);
5276 return -1;
5277 }
5278
5279 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5280 MPI2_IOCSTATUS_MASK;
5281 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5282 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5283 ioc->name, __FILE__, __LINE__, __func__);
5284 return -1;
5285 }
5286
Kashyap, Desaib4344272010-03-17 16:24:14 +05305287 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5288
Eric Moore635374e2009-03-09 01:21:12 -06005289 /* check if device is present */
5290 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5291 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5292 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5293 ioc->name, __FILE__, __LINE__, __func__);
5294 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5295 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5296 return -1;
5297 }
5298
Kashyap, Desaib4344272010-03-17 16:24:14 +05305299 /* check if there were any issues with discovery */
5300 if (_scsih_check_access_status(ioc, sas_address, handle,
5301 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005302 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005303
5304 /* check if this is end device */
5305 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5306 if (!(_scsih_is_end_device(device_info))) {
5307 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5308 ioc->name, __FILE__, __LINE__, __func__);
5309 return -1;
5310 }
5311
Eric Moore635374e2009-03-09 01:21:12 -06005312
5313 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5314 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5315 sas_address);
5316 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5317
Kashyap, Desaib4344272010-03-17 16:24:14 +05305318 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005319 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005320
5321 sas_device = kzalloc(sizeof(struct _sas_device),
5322 GFP_KERNEL);
5323 if (!sas_device) {
5324 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5325 ioc->name, __FILE__, __LINE__, __func__);
5326 return -1;
5327 }
5328
5329 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305330 if (_scsih_get_sas_address(ioc, le16_to_cpu
5331 (sas_device_pg0.ParentDevHandle),
5332 &sas_device->sas_address_parent) != 0)
5333 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5334 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005335 sas_device->enclosure_handle =
5336 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5337 sas_device->slot =
5338 le16_to_cpu(sas_device_pg0.Slot);
5339 sas_device->device_info = device_info;
5340 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305341 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005342
5343 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305344 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5345 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5346 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005347 sas_device->enclosure_logical_id =
5348 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005349
5350 /* get device name */
5351 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5352
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305353 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005354 _scsih_sas_device_init_add(ioc, sas_device);
5355 else
5356 _scsih_sas_device_add(ioc, sas_device);
5357
5358 return 0;
5359}
5360
5361/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305362 * _scsih_remove_device - removing sas device object
5363 * @ioc: per adapter object
5364 * @sas_device_delete: the sas_device object
5365 *
5366 * Return nothing.
5367 */
5368static void
5369_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5370 struct _sas_device *sas_device)
5371{
Kashyap, Desai1278b112010-03-09 17:34:13 +05305372 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305373
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05305374 if ((ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM) &&
5375 (sas_device->pfa_led_on)) {
5376 _scsih_turn_off_pfa_led(ioc, sas_device);
5377 sas_device->pfa_led_on = 0;
5378 }
5379
Kashyap, Desai1278b112010-03-09 17:34:13 +05305380 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5381 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305382 sas_device->handle, (unsigned long long)
5383 sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05305384
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305385 if (sas_device->starget && sas_device->starget->hostdata) {
5386 sas_target_priv_data = sas_device->starget->hostdata;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305387 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305388 _scsih_ublock_io_device(ioc, sas_device->sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305389 sas_target_priv_data->handle =
5390 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305391 }
5392
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305393 if (!ioc->hide_drives)
5394 mpt2sas_transport_port_remove(ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305395 sas_device->sas_address,
5396 sas_device->sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305397
5398 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305399 "(0x%016llx)\n", ioc->name, sas_device->handle,
5400 (unsigned long long) sas_device->sas_address);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305401
5402 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5403 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305404 sas_device->handle, (unsigned long long)
5405 sas_device->sas_address));
5406 kfree(sas_device);
5407}
5408/**
5409 * _scsih_device_remove_by_handle - removing device object by handle
5410 * @ioc: per adapter object
5411 * @handle: device handle
5412 *
5413 * Return nothing.
5414 */
5415static void
5416_scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5417{
5418 struct _sas_device *sas_device;
5419 unsigned long flags;
5420
5421 if (ioc->shost_recovery)
5422 return;
5423
5424 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5425 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5426 if (sas_device)
5427 list_del(&sas_device->list);
5428 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5429 if (sas_device)
5430 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005431}
5432
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305433/**
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305434 * mpt2sas_device_remove_by_sas_address - removing device object by sas address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305435 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305436 * @sas_address: device sas_address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305437 *
5438 * Return nothing.
5439 */
5440void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305441mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
5442 u64 sas_address)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305443{
5444 struct _sas_device *sas_device;
5445 unsigned long flags;
5446
5447 if (ioc->shost_recovery)
5448 return;
5449
5450 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5451 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5452 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305453 if (sas_device)
5454 list_del(&sas_device->list);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305455 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305456 if (sas_device)
5457 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305458}
Eric Moore635374e2009-03-09 01:21:12 -06005459#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5460/**
5461 * _scsih_sas_topology_change_event_debug - debug for topology event
5462 * @ioc: per adapter object
5463 * @event_data: event data payload
5464 * Context: user.
5465 */
5466static void
5467_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5468 Mpi2EventDataSasTopologyChangeList_t *event_data)
5469{
5470 int i;
5471 u16 handle;
5472 u16 reason_code;
5473 u8 phy_number;
5474 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305475 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005476
5477 switch (event_data->ExpStatus) {
5478 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5479 status_str = "add";
5480 break;
5481 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5482 status_str = "remove";
5483 break;
5484 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305485 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005486 status_str = "responding";
5487 break;
5488 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5489 status_str = "remove delay";
5490 break;
5491 default:
5492 status_str = "unknown status";
5493 break;
5494 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305495 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005496 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305497 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005498 "start_phy(%02d), count(%d)\n",
5499 le16_to_cpu(event_data->ExpanderDevHandle),
5500 le16_to_cpu(event_data->EnclosureHandle),
5501 event_data->StartPhyNum, event_data->NumEntries);
5502 for (i = 0; i < event_data->NumEntries; i++) {
5503 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5504 if (!handle)
5505 continue;
5506 phy_number = event_data->StartPhyNum + i;
5507 reason_code = event_data->PHY[i].PhyStatus &
5508 MPI2_EVENT_SAS_TOPO_RC_MASK;
5509 switch (reason_code) {
5510 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305511 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005512 break;
5513 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305514 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005515 break;
5516 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305517 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005518 break;
5519 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305520 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005521 break;
5522 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305523 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005524 break;
5525 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305526 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005527 break;
5528 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305529 link_rate = event_data->PHY[i].LinkRate >> 4;
5530 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305531 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305532 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5533 handle, status_str, link_rate, prev_link_rate);
5534
Eric Moore635374e2009-03-09 01:21:12 -06005535 }
5536}
5537#endif
5538
5539/**
5540 * _scsih_sas_topology_change_event - handle topology changes
5541 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305542 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005543 * Context: user.
5544 *
5545 */
5546static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305547_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005548 struct fw_event_work *fw_event)
5549{
5550 int i;
5551 u16 parent_handle, handle;
5552 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305553 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005554 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305555 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005556 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305557 u8 link_rate, prev_link_rate;
Joe Lawrence00713ad2014-06-25 17:03:33 -04005558 Mpi2EventDataSasTopologyChangeList_t *event_data =
5559 (Mpi2EventDataSasTopologyChangeList_t *)
5560 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005561
5562#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5563 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5564 _scsih_sas_topology_change_event_debug(ioc, event_data);
5565#endif
5566
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305567 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305568 return;
5569
Eric Moore635374e2009-03-09 01:21:12 -06005570 if (!ioc->sas_hba.num_phys)
5571 _scsih_sas_host_add(ioc);
5572 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305573 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005574
5575 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305576 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005577 "event\n", ioc->name));
5578 return;
5579 }
5580
5581 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5582
5583 /* handle expander add */
5584 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5585 if (_scsih_expander_add(ioc, parent_handle) != 0)
5586 return;
5587
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305588 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5589 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5590 parent_handle);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305591 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305592 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305593 max_phys = sas_expander->num_phys;
5594 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305595 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305596 max_phys = ioc->sas_hba.num_phys;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305597 } else {
5598 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305599 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305600 }
5601 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305602
Eric Moore635374e2009-03-09 01:21:12 -06005603 /* handle siblings events */
5604 for (i = 0; i < event_data->NumEntries; i++) {
5605 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305606 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005607 "expander event\n", ioc->name));
5608 return;
5609 }
Eric Moore3cb54692010-07-08 14:44:34 -06005610 if (ioc->shost_recovery || ioc->remove_host ||
5611 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305612 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305613 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305614 if (phy_number >= max_phys)
5615 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305616 reason_code = event_data->PHY[i].PhyStatus &
5617 MPI2_EVENT_SAS_TOPO_RC_MASK;
5618 if ((event_data->PHY[i].PhyStatus &
5619 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5620 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005621 continue;
5622 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5623 if (!handle)
5624 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305625 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305626 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005627 switch (reason_code) {
5628 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305629
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305630 if (ioc->shost_recovery)
5631 break;
5632
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305633 if (link_rate == prev_link_rate)
5634 break;
5635
5636 mpt2sas_transport_update_links(ioc, sas_address,
5637 handle, phy_number, link_rate);
5638
Kashyap, Desaib4344272010-03-17 16:24:14 +05305639 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5640 break;
5641
5642 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305643 break;
Eric Moore635374e2009-03-09 01:21:12 -06005644 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305645
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305646 if (ioc->shost_recovery)
5647 break;
5648
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305649 mpt2sas_transport_update_links(ioc, sas_address,
5650 handle, phy_number, link_rate);
5651
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305652 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005653 break;
5654 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305655
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305656 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005657 break;
5658 }
5659 }
5660
5661 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305662 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5663 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305664 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005665
5666}
5667
5668#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5669/**
5670 * _scsih_sas_device_status_change_event_debug - debug for device event
5671 * @event_data: event data payload
5672 * Context: user.
5673 *
5674 * Return nothing.
5675 */
5676static void
5677_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5678 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5679{
5680 char *reason_str = NULL;
5681
5682 switch (event_data->ReasonCode) {
5683 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5684 reason_str = "smart data";
5685 break;
5686 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5687 reason_str = "unsupported device discovered";
5688 break;
5689 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5690 reason_str = "internal device reset";
5691 break;
5692 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5693 reason_str = "internal task abort";
5694 break;
5695 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5696 reason_str = "internal task abort set";
5697 break;
5698 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5699 reason_str = "internal clear task set";
5700 break;
5701 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5702 reason_str = "internal query task";
5703 break;
5704 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5705 reason_str = "sata init failure";
5706 break;
5707 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5708 reason_str = "internal device reset complete";
5709 break;
5710 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5711 reason_str = "internal task abort complete";
5712 break;
5713 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5714 reason_str = "internal async notification";
5715 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305716 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5717 reason_str = "expander reduced functionality";
5718 break;
5719 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5720 reason_str = "expander reduced functionality complete";
5721 break;
Eric Moore635374e2009-03-09 01:21:12 -06005722 default:
5723 reason_str = "unknown reason";
5724 break;
5725 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305726 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305727 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5728 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5729 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5730 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005731 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305732 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005733 event_data->ASC, event_data->ASCQ);
5734 printk(KERN_INFO "\n");
5735}
5736#endif
5737
5738/**
5739 * _scsih_sas_device_status_change_event - handle device status change
5740 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305741 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005742 * Context: user.
5743 *
5744 * Return nothing.
5745 */
5746static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305747_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5748 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005749{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305750 struct MPT2SAS_TARGET *target_priv_data;
5751 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305752 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305753 unsigned long flags;
5754 Mpi2EventDataSasDeviceStatusChange_t *event_data =
Joe Lawrence00713ad2014-06-25 17:03:33 -04005755 (Mpi2EventDataSasDeviceStatusChange_t *)
5756 fw_event->event_data;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305757
Eric Moore635374e2009-03-09 01:21:12 -06005758#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5759 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305760 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305761 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005762#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305763
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305764 /* In MPI Revision K (0xC), the internal device reset complete was
5765 * implemented, so avoid setting tm_busy flag for older firmware.
5766 */
5767 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5768 return;
5769
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305770 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305771 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305772 event_data->ReasonCode !=
5773 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305774 return;
5775
5776 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5777 sas_address = le64_to_cpu(event_data->SASAddress);
5778 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5779 sas_address);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305780
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305781 if (!sas_device || !sas_device->starget) {
5782 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305783 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305784 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305785
5786 target_priv_data = sas_device->starget->hostdata;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305787 if (!target_priv_data) {
5788 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305789 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305790 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305791
5792 if (event_data->ReasonCode ==
5793 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5794 target_priv_data->tm_busy = 1;
5795 else
5796 target_priv_data->tm_busy = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305797 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005798}
5799
5800#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5801/**
5802 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5803 * @ioc: per adapter object
5804 * @event_data: event data payload
5805 * Context: user.
5806 *
5807 * Return nothing.
5808 */
5809static void
5810_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5811 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5812{
5813 char *reason_str = NULL;
5814
5815 switch (event_data->ReasonCode) {
5816 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5817 reason_str = "enclosure add";
5818 break;
5819 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5820 reason_str = "enclosure remove";
5821 break;
5822 default:
5823 reason_str = "unknown reason";
5824 break;
5825 }
5826
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305827 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005828 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5829 " number slots(%d)\n", ioc->name, reason_str,
5830 le16_to_cpu(event_data->EnclosureHandle),
5831 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5832 le16_to_cpu(event_data->StartSlot));
5833}
5834#endif
5835
5836/**
5837 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5838 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305839 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005840 * Context: user.
5841 *
5842 * Return nothing.
5843 */
5844static void
5845_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305846 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005847{
5848#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5849 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5850 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Joe Lawrence00713ad2014-06-25 17:03:33 -04005851 (Mpi2EventDataSasEnclDevStatusChange_t *)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305852 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005853#endif
5854}
5855
5856/**
Andrew Mortona78e21d2012-02-08 12:52:22 -08005857 * _scsih_sas_broadcast_primitive_event - handle broadcast events
Eric Moore635374e2009-03-09 01:21:12 -06005858 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305859 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005860 * Context: user.
5861 *
5862 * Return nothing.
5863 */
5864static void
Andrew Mortona78e21d2012-02-08 12:52:22 -08005865_scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305866 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005867{
5868 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305869 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005870 u16 smid, handle;
5871 u32 lun;
5872 struct MPT2SAS_DEVICE *sas_device_priv_data;
5873 u32 termination_count;
5874 u32 query_count;
5875 Mpi2SCSITaskManagementReply_t *mpi_reply;
Joe Lawrence00713ad2014-06-25 17:03:33 -04005876 Mpi2EventDataSasBroadcastPrimitive_t *event_data =
5877 (Mpi2EventDataSasBroadcastPrimitive_t *)
5878 fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305879 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305880 unsigned long flags;
5881 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305882 u8 max_retries = 0;
5883 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305884
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305885 mutex_lock(&ioc->tm_cmds.mutex);
Reddy, Sreekanthc3a634b2013-02-26 17:36:12 +05305886 pr_info(MPT2SAS_FMT
5887 "%s: enter: phy number(%d), width(%d)\n",
5888 ioc->name, __func__, event_data->PhyNum,
5889 event_data->PortWidth);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305890
5891 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005892
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305893 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305894 mpi_reply = ioc->tm_cmds.reply;
5895broadcast_aen_retry:
5896
5897 /* sanity checks for retrying this loop */
5898 if (max_retries++ == 5) {
5899 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5900 ioc->name, __func__));
5901 goto out;
5902 } else if (max_retries > 1)
5903 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5904 ioc->name, __func__, max_retries - 1));
5905
Eric Moore635374e2009-03-09 01:21:12 -06005906 termination_count = 0;
5907 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305908 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305909 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305910 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005911 scmd = _scsih_scsi_lookup_get(ioc, smid);
5912 if (!scmd)
5913 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305914 sdev = scmd->device;
5915 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005916 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5917 continue;
5918 /* skip hidden raid components */
5919 if (sas_device_priv_data->sas_target->flags &
5920 MPT_TARGET_FLAGS_RAID_COMPONENT)
5921 continue;
5922 /* skip volumes */
5923 if (sas_device_priv_data->sas_target->flags &
5924 MPT_TARGET_FLAGS_VOLUME)
5925 continue;
5926
5927 handle = sas_device_priv_data->sas_target->handle;
5928 lun = sas_device_priv_data->lun;
5929 query_count++;
5930
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305931 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305932 goto out;
5933
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305934 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305935 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04005936 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305937 TM_MUTEX_OFF);
5938 if (r == FAILED) {
5939 sdev_printk(KERN_WARNING, sdev,
5940 "mpt2sas_scsih_issue_tm: FAILED when sending "
5941 "QUERY_TASK: scmd(%p)\n", scmd);
5942 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5943 goto broadcast_aen_retry;
5944 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305945 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5946 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305947 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5948 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5949 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5950 scmd);
5951 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5952 goto broadcast_aen_retry;
5953 }
5954
5955 /* see if IO is still owned by IOC and target */
5956 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005957 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5958 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305959 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305960 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005961 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305962 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305963 task_abort_retries = 0;
5964 tm_retry:
5965 if (task_abort_retries++ == 60) {
5966 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5967 "%s: ABORT_TASK: giving up\n", ioc->name,
5968 __func__));
5969 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5970 goto broadcast_aen_retry;
5971 }
5972
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305973 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305974 goto out_no_lock;
5975
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305976 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5977 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04005978 TM_MUTEX_OFF);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305979 if (r == FAILED) {
5980 sdev_printk(KERN_WARNING, sdev,
5981 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305982 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305983 goto tm_retry;
5984 }
5985
5986 if (task_abort_retries > 1)
5987 sdev_printk(KERN_WARNING, sdev,
5988 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5989 " scmd(%p)\n",
5990 task_abort_retries - 1, scmd);
5991
Eric Moore635374e2009-03-09 01:21:12 -06005992 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305993 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005994 }
Eric Moore635374e2009-03-09 01:21:12 -06005995
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305996 if (ioc->broadcast_aen_pending) {
5997 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5998 " pending AEN\n", ioc->name, __func__));
5999 ioc->broadcast_aen_pending = 0;
6000 goto broadcast_aen_retry;
6001 }
6002
6003 out:
6004 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
6005 out_no_lock:
6006
6007 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06006008 "%s - exit, query_count = %d termination_count = %d\n",
6009 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05306010
6011 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306012 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05306013 _scsih_ublock_io_all_device(ioc);
6014 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06006015}
6016
6017/**
6018 * _scsih_sas_discovery_event - handle discovery events
6019 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306020 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006021 * Context: user.
6022 *
6023 * Return nothing.
6024 */
6025static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306026_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
6027 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006028{
Joe Lawrence00713ad2014-06-25 17:03:33 -04006029 Mpi2EventDataSasDiscovery_t *event_data =
6030 (Mpi2EventDataSasDiscovery_t *)
6031 fw_event->event_data;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306032
Eric Moore635374e2009-03-09 01:21:12 -06006033#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6034 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306035 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06006036 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
6037 "start" : "stop");
6038 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05306039 printk("discovery_status(0x%08x)",
6040 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06006041 printk("\n");
6042 }
6043#endif
6044
6045 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05306046 !ioc->sas_hba.num_phys) {
6047 if (disable_discovery > 0 && ioc->shost_recovery) {
6048 /* Wait for the reset to complete */
6049 while (ioc->shost_recovery)
6050 ssleep(1);
6051 }
Eric Moore635374e2009-03-09 01:21:12 -06006052 _scsih_sas_host_add(ioc);
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05306053 }
Eric Moore635374e2009-03-09 01:21:12 -06006054}
6055
6056/**
6057 * _scsih_reprobe_lun - reprobing lun
6058 * @sdev: scsi device struct
6059 * @no_uld_attach: sdev->no_uld_attach flag setting
6060 *
6061 **/
6062static void
6063_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6064{
6065 int rc;
6066
6067 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6068 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6069 sdev->no_uld_attach ? "hidding" : "exposing");
6070 rc = scsi_device_reprobe(sdev);
6071}
6072
6073/**
Eric Moore635374e2009-03-09 01:21:12 -06006074 * _scsih_sas_volume_add - add new volume
6075 * @ioc: per adapter object
6076 * @element: IR config element data
6077 * Context: user.
6078 *
6079 * Return nothing.
6080 */
6081static void
6082_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
6083 Mpi2EventIrConfigElement_t *element)
6084{
6085 struct _raid_device *raid_device;
6086 unsigned long flags;
6087 u64 wwid;
6088 u16 handle = le16_to_cpu(element->VolDevHandle);
6089 int rc;
6090
Eric Moore635374e2009-03-09 01:21:12 -06006091 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6092 if (!wwid) {
6093 printk(MPT2SAS_ERR_FMT
6094 "failure at %s:%d/%s()!\n", ioc->name,
6095 __FILE__, __LINE__, __func__);
6096 return;
6097 }
6098
6099 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6100 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6101 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6102
6103 if (raid_device)
6104 return;
6105
6106 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6107 if (!raid_device) {
6108 printk(MPT2SAS_ERR_FMT
6109 "failure at %s:%d/%s()!\n", ioc->name,
6110 __FILE__, __LINE__, __func__);
6111 return;
6112 }
6113
6114 raid_device->id = ioc->sas_id++;
6115 raid_device->channel = RAID_CHANNEL;
6116 raid_device->handle = handle;
6117 raid_device->wwid = wwid;
6118 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306119 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06006120 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6121 raid_device->id, 0);
6122 if (rc)
6123 _scsih_raid_device_remove(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306124 } else {
6125 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006126 _scsih_determine_boot_device(ioc, raid_device, 1);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306127 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6128 }
Eric Moore635374e2009-03-09 01:21:12 -06006129}
6130
6131/**
6132 * _scsih_sas_volume_delete - delete volume
6133 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306134 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006135 * Context: user.
6136 *
6137 * Return nothing.
6138 */
6139static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306140_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006141{
6142 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006143 unsigned long flags;
6144 struct MPT2SAS_TARGET *sas_target_priv_data;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306145 struct scsi_target *starget = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006146
Eric Moore635374e2009-03-09 01:21:12 -06006147 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6148 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306149 if (raid_device) {
6150 if (raid_device->starget) {
6151 starget = raid_device->starget;
6152 sas_target_priv_data = starget->hostdata;
6153 sas_target_priv_data->deleted = 1;
6154 }
6155 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6156 "(0x%016llx)\n", ioc->name, raid_device->handle,
6157 (unsigned long long) raid_device->wwid);
6158 list_del(&raid_device->list);
6159 kfree(raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006160 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306161 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6162 if (starget)
6163 scsi_remove_target(&starget->dev);
Eric Moore635374e2009-03-09 01:21:12 -06006164}
6165
6166/**
6167 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6168 * @ioc: per adapter object
6169 * @element: IR config element data
6170 * Context: user.
6171 *
6172 * Return nothing.
6173 */
6174static void
6175_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6176 Mpi2EventIrConfigElement_t *element)
6177{
6178 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306179 struct scsi_target *starget = NULL;
6180 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006181 unsigned long flags;
6182 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6183
6184 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6185 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306186 if (sas_device) {
6187 sas_device->volume_handle = 0;
6188 sas_device->volume_wwid = 0;
6189 clear_bit(handle, ioc->pd_handles);
6190 if (sas_device->starget && sas_device->starget->hostdata) {
6191 starget = sas_device->starget;
6192 sas_target_priv_data = starget->hostdata;
6193 sas_target_priv_data->flags &=
6194 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6195 }
6196 }
Eric Moore635374e2009-03-09 01:21:12 -06006197 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6198 if (!sas_device)
6199 return;
6200
6201 /* exposing raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306202 if (starget)
6203 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006204}
6205
6206/**
6207 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6208 * @ioc: per adapter object
6209 * @element: IR config element data
6210 * Context: user.
6211 *
6212 * Return nothing.
6213 */
6214static void
6215_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6216 Mpi2EventIrConfigElement_t *element)
6217{
6218 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306219 struct scsi_target *starget = NULL;
6220 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006221 unsigned long flags;
6222 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306223 u16 volume_handle = 0;
6224 u64 volume_wwid = 0;
6225
6226 mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
6227 if (volume_handle)
6228 mpt2sas_config_get_volume_wwid(ioc, volume_handle,
6229 &volume_wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006230
6231 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6232 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306233 if (sas_device) {
6234 set_bit(handle, ioc->pd_handles);
6235 if (sas_device->starget && sas_device->starget->hostdata) {
6236 starget = sas_device->starget;
6237 sas_target_priv_data = starget->hostdata;
6238 sas_target_priv_data->flags |=
6239 MPT_TARGET_FLAGS_RAID_COMPONENT;
6240 sas_device->volume_handle = volume_handle;
6241 sas_device->volume_wwid = volume_wwid;
6242 }
6243 }
Eric Moore635374e2009-03-09 01:21:12 -06006244 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6245 if (!sas_device)
6246 return;
6247
6248 /* hiding raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306249 if (starget)
6250 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006251}
6252
6253/**
6254 * _scsih_sas_pd_delete - delete pd component
6255 * @ioc: per adapter object
6256 * @element: IR config element data
6257 * Context: user.
6258 *
6259 * Return nothing.
6260 */
6261static void
6262_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6263 Mpi2EventIrConfigElement_t *element)
6264{
Eric Moore635374e2009-03-09 01:21:12 -06006265 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6266
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306267 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006268}
6269
6270/**
6271 * _scsih_sas_pd_add - remove pd component
6272 * @ioc: per adapter object
6273 * @element: IR config element data
6274 * Context: user.
6275 *
6276 * Return nothing.
6277 */
6278static void
6279_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6280 Mpi2EventIrConfigElement_t *element)
6281{
6282 struct _sas_device *sas_device;
6283 unsigned long flags;
6284 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306285 Mpi2ConfigReply_t mpi_reply;
6286 Mpi2SasDevicePage0_t sas_device_pg0;
6287 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306288 u64 sas_address;
6289 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006290
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306291 set_bit(handle, ioc->pd_handles);
6292
Eric Moore635374e2009-03-09 01:21:12 -06006293 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6294 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6295 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306296 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306297 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306298
6299 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6300 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6301 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6302 ioc->name, __FILE__, __LINE__, __func__);
6303 return;
6304 }
6305
6306 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6307 MPI2_IOCSTATUS_MASK;
6308 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6309 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6310 ioc->name, __FILE__, __LINE__, __func__);
6311 return;
6312 }
6313
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306314 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6315 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6316 mpt2sas_transport_update_links(ioc, sas_address, handle,
6317 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306318
6319 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006320}
6321
6322#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6323/**
6324 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6325 * @ioc: per adapter object
6326 * @event_data: event data payload
6327 * Context: user.
6328 *
6329 * Return nothing.
6330 */
6331static void
6332_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6333 Mpi2EventDataIrConfigChangeList_t *event_data)
6334{
6335 Mpi2EventIrConfigElement_t *element;
6336 u8 element_type;
6337 int i;
6338 char *reason_str = NULL, *element_str = NULL;
6339
6340 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6341
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306342 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006343 ioc->name, (le32_to_cpu(event_data->Flags) &
6344 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6345 "foreign" : "native", event_data->NumElements);
6346 for (i = 0; i < event_data->NumElements; i++, element++) {
6347 switch (element->ReasonCode) {
6348 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6349 reason_str = "add";
6350 break;
6351 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6352 reason_str = "remove";
6353 break;
6354 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6355 reason_str = "no change";
6356 break;
6357 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6358 reason_str = "hide";
6359 break;
6360 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6361 reason_str = "unhide";
6362 break;
6363 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6364 reason_str = "volume_created";
6365 break;
6366 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6367 reason_str = "volume_deleted";
6368 break;
6369 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6370 reason_str = "pd_created";
6371 break;
6372 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6373 reason_str = "pd_deleted";
6374 break;
6375 default:
6376 reason_str = "unknown reason";
6377 break;
6378 }
6379 element_type = le16_to_cpu(element->ElementFlags) &
6380 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6381 switch (element_type) {
6382 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6383 element_str = "volume";
6384 break;
6385 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6386 element_str = "phys disk";
6387 break;
6388 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6389 element_str = "hot spare";
6390 break;
6391 default:
6392 element_str = "unknown element";
6393 break;
6394 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306395 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006396 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6397 reason_str, le16_to_cpu(element->VolDevHandle),
6398 le16_to_cpu(element->PhysDiskDevHandle),
6399 element->PhysDiskNum);
6400 }
6401}
6402#endif
6403
6404/**
6405 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6406 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306407 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006408 * Context: user.
6409 *
6410 * Return nothing.
6411 */
6412static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306413_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6414 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006415{
6416 Mpi2EventIrConfigElement_t *element;
6417 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306418 u8 foreign_config;
Joe Lawrence00713ad2014-06-25 17:03:33 -04006419 Mpi2EventDataIrConfigChangeList_t *event_data =
6420 (Mpi2EventDataIrConfigChangeList_t *)
6421 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006422
6423#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306424 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6425 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006426 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6427
6428#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306429
6430 if (ioc->shost_recovery)
6431 return;
6432
Kashyap, Desai62727a72009-08-07 19:35:18 +05306433 foreign_config = (le32_to_cpu(event_data->Flags) &
6434 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006435
6436 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6437 for (i = 0; i < event_data->NumElements; i++, element++) {
6438
6439 switch (element->ReasonCode) {
6440 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6441 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306442 if (!foreign_config)
6443 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006444 break;
6445 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6446 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306447 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306448 _scsih_sas_volume_delete(ioc,
6449 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006450 break;
6451 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306452 if (!ioc->is_warpdrive)
6453 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006454 break;
6455 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306456 if (!ioc->is_warpdrive)
6457 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006458 break;
6459 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306460 if (!ioc->is_warpdrive)
6461 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006462 break;
6463 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306464 if (!ioc->is_warpdrive)
6465 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006466 break;
6467 }
6468 }
6469}
6470
6471/**
6472 * _scsih_sas_ir_volume_event - IR volume event
6473 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306474 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006475 * Context: user.
6476 *
6477 * Return nothing.
6478 */
6479static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306480_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6481 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006482{
6483 u64 wwid;
6484 unsigned long flags;
6485 struct _raid_device *raid_device;
6486 u16 handle;
6487 u32 state;
6488 int rc;
Joe Lawrence00713ad2014-06-25 17:03:33 -04006489 Mpi2EventDataIrVolume_t *event_data =
6490 (Mpi2EventDataIrVolume_t *)
6491 fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006492
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306493 if (ioc->shost_recovery)
6494 return;
6495
Eric Moore635374e2009-03-09 01:21:12 -06006496 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6497 return;
6498
6499 handle = le16_to_cpu(event_data->VolDevHandle);
6500 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306501 if (!ioc->hide_ir_msg)
6502 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6503 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6504 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006505
Eric Moore635374e2009-03-09 01:21:12 -06006506 switch (state) {
6507 case MPI2_RAID_VOL_STATE_MISSING:
6508 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306509 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006510 break;
6511
6512 case MPI2_RAID_VOL_STATE_ONLINE:
6513 case MPI2_RAID_VOL_STATE_DEGRADED:
6514 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306515
6516 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6517 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6518 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6519
Eric Moore635374e2009-03-09 01:21:12 -06006520 if (raid_device)
6521 break;
6522
6523 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6524 if (!wwid) {
6525 printk(MPT2SAS_ERR_FMT
6526 "failure at %s:%d/%s()!\n", ioc->name,
6527 __FILE__, __LINE__, __func__);
6528 break;
6529 }
6530
6531 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6532 if (!raid_device) {
6533 printk(MPT2SAS_ERR_FMT
6534 "failure at %s:%d/%s()!\n", ioc->name,
6535 __FILE__, __LINE__, __func__);
6536 break;
6537 }
6538
6539 raid_device->id = ioc->sas_id++;
6540 raid_device->channel = RAID_CHANNEL;
6541 raid_device->handle = handle;
6542 raid_device->wwid = wwid;
6543 _scsih_raid_device_add(ioc, raid_device);
6544 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6545 raid_device->id, 0);
6546 if (rc)
6547 _scsih_raid_device_remove(ioc, raid_device);
6548 break;
6549
6550 case MPI2_RAID_VOL_STATE_INITIALIZING:
6551 default:
6552 break;
6553 }
6554}
6555
6556/**
6557 * _scsih_sas_ir_physical_disk_event - PD event
6558 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306559 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006560 * Context: user.
6561 *
6562 * Return nothing.
6563 */
6564static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306565_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6566 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006567{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306568 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006569 u32 state;
6570 struct _sas_device *sas_device;
6571 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306572 Mpi2ConfigReply_t mpi_reply;
6573 Mpi2SasDevicePage0_t sas_device_pg0;
6574 u32 ioc_status;
Joe Lawrence00713ad2014-06-25 17:03:33 -04006575 Mpi2EventDataIrPhysicalDisk_t *event_data =
6576 (Mpi2EventDataIrPhysicalDisk_t *)
6577 fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306578 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006579
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306580 if (ioc->shost_recovery)
6581 return;
6582
Eric Moore635374e2009-03-09 01:21:12 -06006583 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6584 return;
6585
6586 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6587 state = le32_to_cpu(event_data->NewValue);
6588
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306589 if (!ioc->hide_ir_msg)
6590 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6591 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6592 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006593
Eric Moore635374e2009-03-09 01:21:12 -06006594 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006595 case MPI2_RAID_PD_STATE_ONLINE:
6596 case MPI2_RAID_PD_STATE_DEGRADED:
6597 case MPI2_RAID_PD_STATE_REBUILDING:
6598 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306599 case MPI2_RAID_PD_STATE_HOT_SPARE:
6600
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306601 if (!ioc->is_warpdrive)
6602 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306603
6604 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6605 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6606 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6607
6608 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306609 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306610
6611 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6612 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6613 handle))) {
6614 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6615 ioc->name, __FILE__, __LINE__, __func__);
6616 return;
6617 }
6618
6619 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6620 MPI2_IOCSTATUS_MASK;
6621 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6622 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6623 ioc->name, __FILE__, __LINE__, __func__);
6624 return;
6625 }
6626
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306627 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6628 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6629 mpt2sas_transport_update_links(ioc, sas_address, handle,
6630 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306631
6632 _scsih_add_device(ioc, handle, 0, 1);
6633
Eric Moore635374e2009-03-09 01:21:12 -06006634 break;
6635
Kashyap, Desai62727a72009-08-07 19:35:18 +05306636 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006637 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6638 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006639 default:
6640 break;
6641 }
6642}
6643
6644#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6645/**
6646 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6647 * @ioc: per adapter object
6648 * @event_data: event data payload
6649 * Context: user.
6650 *
6651 * Return nothing.
6652 */
6653static void
6654_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6655 Mpi2EventDataIrOperationStatus_t *event_data)
6656{
6657 char *reason_str = NULL;
6658
6659 switch (event_data->RAIDOperation) {
6660 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6661 reason_str = "resync";
6662 break;
6663 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6664 reason_str = "online capacity expansion";
6665 break;
6666 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6667 reason_str = "consistency check";
6668 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306669 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6670 reason_str = "background init";
6671 break;
6672 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6673 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006674 break;
6675 }
6676
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306677 if (!reason_str)
6678 return;
6679
Eric Moore635374e2009-03-09 01:21:12 -06006680 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6681 "\thandle(0x%04x), percent complete(%d)\n",
6682 ioc->name, reason_str,
6683 le16_to_cpu(event_data->VolDevHandle),
6684 event_data->PercentComplete);
6685}
6686#endif
6687
6688/**
6689 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6690 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306691 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006692 * Context: user.
6693 *
6694 * Return nothing.
6695 */
6696static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306697_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6698 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006699{
Joe Lawrence00713ad2014-06-25 17:03:33 -04006700 Mpi2EventDataIrOperationStatus_t *event_data =
6701 (Mpi2EventDataIrOperationStatus_t *)
6702 fw_event->event_data;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306703 static struct _raid_device *raid_device;
6704 unsigned long flags;
6705 u16 handle;
6706
Eric Moore635374e2009-03-09 01:21:12 -06006707#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306708 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6709 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306710 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306711 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006712#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306713
6714 /* code added for raid transport support */
6715 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6716
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306717 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306718 handle = le16_to_cpu(event_data->VolDevHandle);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306719 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306720 if (raid_device)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306721 raid_device->percent_complete =
6722 event_data->PercentComplete;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306723 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306724 }
Eric Moore635374e2009-03-09 01:21:12 -06006725}
6726
6727/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306728 * _scsih_prep_device_scan - initialize parameters prior to device scan
6729 * @ioc: per adapter object
6730 *
6731 * Set the deleted flag prior to device scan. If the device is found during
6732 * the scan, then we clear the deleted flag.
6733 */
6734static void
6735_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6736{
6737 struct MPT2SAS_DEVICE *sas_device_priv_data;
6738 struct scsi_device *sdev;
6739
6740 shost_for_each_device(sdev, ioc->shost) {
6741 sas_device_priv_data = sdev->hostdata;
6742 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6743 sas_device_priv_data->sas_target->deleted = 1;
6744 }
6745}
6746
6747/**
Eric Moore635374e2009-03-09 01:21:12 -06006748 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6749 * @ioc: per adapter object
6750 * @sas_address: sas address
6751 * @slot: enclosure slot id
6752 * @handle: device handle
6753 *
6754 * After host reset, find out whether devices are still responding.
6755 * Used in _scsi_remove_unresponsive_sas_devices.
6756 *
6757 * Return nothing.
6758 */
6759static void
6760_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6761 u16 slot, u16 handle)
6762{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306763 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006764 struct scsi_target *starget;
6765 struct _sas_device *sas_device;
6766 unsigned long flags;
6767
6768 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6769 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6770 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306771 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006772 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306773 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306774 if (starget && starget->hostdata) {
6775 sas_target_priv_data = starget->hostdata;
6776 sas_target_priv_data->tm_busy = 0;
6777 sas_target_priv_data->deleted = 0;
6778 } else
6779 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306780 if (starget)
6781 starget_printk(KERN_INFO, starget,
6782 "handle(0x%04x), sas_addr(0x%016llx), "
6783 "enclosure logical id(0x%016llx), "
6784 "slot(%d)\n", handle,
6785 (unsigned long long)sas_device->sas_address,
6786 (unsigned long long)
6787 sas_device->enclosure_logical_id,
6788 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006789 if (sas_device->handle == handle)
6790 goto out;
6791 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6792 sas_device->handle);
6793 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306794 if (sas_target_priv_data)
6795 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006796 goto out;
6797 }
6798 }
6799 out:
6800 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6801}
6802
6803/**
6804 * _scsih_search_responding_sas_devices -
6805 * @ioc: per adapter object
6806 *
6807 * After host reset, find out whether devices are still responding.
6808 * If not remove.
6809 *
6810 * Return nothing.
6811 */
6812static void
6813_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6814{
6815 Mpi2SasDevicePage0_t sas_device_pg0;
6816 Mpi2ConfigReply_t mpi_reply;
6817 u16 ioc_status;
6818 __le64 sas_address;
6819 u16 handle;
6820 u32 device_info;
6821 u16 slot;
6822
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306823 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006824
6825 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306826 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006827
6828 handle = 0xFFFF;
6829 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6830 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6831 handle))) {
6832 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6833 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306834 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006835 break;
6836 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6837 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6838 if (!(_scsih_is_end_device(device_info)))
6839 continue;
6840 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6841 slot = le16_to_cpu(sas_device_pg0.Slot);
6842 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6843 handle);
6844 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306845out:
6846 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6847 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006848}
6849
6850/**
6851 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6852 * @ioc: per adapter object
6853 * @wwid: world wide identifier for raid volume
6854 * @handle: device handle
6855 *
6856 * After host reset, find out whether devices are still responding.
6857 * Used in _scsi_remove_unresponsive_raid_devices.
6858 *
6859 * Return nothing.
6860 */
6861static void
6862_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6863 u16 handle)
6864{
6865 struct MPT2SAS_TARGET *sas_target_priv_data;
6866 struct scsi_target *starget;
6867 struct _raid_device *raid_device;
6868 unsigned long flags;
6869
6870 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6871 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6872 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306873 starget = raid_device->starget;
6874 if (starget && starget->hostdata) {
6875 sas_target_priv_data = starget->hostdata;
6876 sas_target_priv_data->deleted = 0;
6877 } else
6878 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006879 raid_device->responding = 1;
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306880 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006881 starget_printk(KERN_INFO, raid_device->starget,
6882 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6883 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306884 /*
6885 * WARPDRIVE: The handles of the PDs might have changed
6886 * across the host reset so re-initialize the
6887 * required data for Direct IO
6888 */
6889 _scsih_init_warpdrive_properties(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306890 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6891 if (raid_device->handle == handle) {
6892 spin_unlock_irqrestore(&ioc->raid_device_lock,
6893 flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306894 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306895 }
Eric Moore635374e2009-03-09 01:21:12 -06006896 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6897 raid_device->handle);
6898 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306899 if (sas_target_priv_data)
6900 sas_target_priv_data->handle = handle;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306901 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306902 return;
Eric Moore635374e2009-03-09 01:21:12 -06006903 }
6904 }
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306905
Eric Moore635374e2009-03-09 01:21:12 -06006906 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6907}
6908
6909/**
6910 * _scsih_search_responding_raid_devices -
6911 * @ioc: per adapter object
6912 *
6913 * After host reset, find out whether devices are still responding.
6914 * If not remove.
6915 *
6916 * Return nothing.
6917 */
6918static void
6919_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6920{
6921 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306922 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306923 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006924 Mpi2ConfigReply_t mpi_reply;
6925 u16 ioc_status;
6926 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306927 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006928
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306929 if (!ioc->ir_firmware)
6930 return;
6931
6932 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6933 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006934
6935 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306936 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006937
6938 handle = 0xFFFF;
6939 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6940 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6941 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6942 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306943 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006944 break;
6945 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306946
6947 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6948 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6949 sizeof(Mpi2RaidVolPage0_t)))
6950 continue;
6951
6952 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6953 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6954 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6955 _scsih_mark_responding_raid_device(ioc,
6956 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006957 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306958
6959 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306960 if (!ioc->is_warpdrive) {
6961 phys_disk_num = 0xFF;
6962 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6963 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6964 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6965 phys_disk_num))) {
6966 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6967 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306968 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306969 break;
6970 phys_disk_num = pd_pg0.PhysDiskNum;
6971 handle = le16_to_cpu(pd_pg0.DevHandle);
6972 set_bit(handle, ioc->pd_handles);
6973 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306974 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306975out:
6976 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6977 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006978}
6979
6980/**
6981 * _scsih_mark_responding_expander - mark a expander as responding
6982 * @ioc: per adapter object
6983 * @sas_address: sas address
6984 * @handle:
6985 *
6986 * After host reset, find out whether devices are still responding.
6987 * Used in _scsi_remove_unresponsive_expanders.
6988 *
6989 * Return nothing.
6990 */
6991static void
6992_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6993 u16 handle)
6994{
6995 struct _sas_node *sas_expander;
6996 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306997 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006998
6999 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7000 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307001 if (sas_expander->sas_address != sas_address)
7002 continue;
7003 sas_expander->responding = 1;
7004 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06007005 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307006 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
7007 " from(0x%04x) to (0x%04x)!!!\n",
7008 (unsigned long long)sas_expander->sas_address,
7009 sas_expander->handle, handle);
7010 sas_expander->handle = handle;
7011 for (i = 0 ; i < sas_expander->num_phys ; i++)
7012 sas_expander->phy[i].handle = handle;
7013 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06007014 }
7015 out:
7016 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7017}
7018
7019/**
7020 * _scsih_search_responding_expanders -
7021 * @ioc: per adapter object
7022 *
7023 * After host reset, find out whether devices are still responding.
7024 * If not remove.
7025 *
7026 * Return nothing.
7027 */
7028static void
7029_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
7030{
7031 Mpi2ExpanderPage0_t expander_pg0;
7032 Mpi2ConfigReply_t mpi_reply;
7033 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05307034 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06007035 u16 handle;
7036
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307037 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007038
7039 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307040 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06007041
7042 handle = 0xFFFF;
7043 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7044 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7045
7046 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7047 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05307048 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06007049 break;
7050
7051 handle = le16_to_cpu(expander_pg0.DevHandle);
7052 sas_address = le64_to_cpu(expander_pg0.SASAddress);
7053 printk(KERN_INFO "\texpander present: handle(0x%04x), "
7054 "sas_addr(0x%016llx)\n", handle,
7055 (unsigned long long)sas_address);
7056 _scsih_mark_responding_expander(ioc, sas_address, handle);
7057 }
7058
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307059 out:
7060 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007061}
7062
7063/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307064 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06007065 * @ioc: per adapter object
7066 *
7067 * Return nothing.
7068 */
7069static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307070_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06007071{
7072 struct _sas_device *sas_device, *sas_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307073 struct _sas_node *sas_expander, *sas_expander_next;
Eric Moore635374e2009-03-09 01:21:12 -06007074 struct _raid_device *raid_device, *raid_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307075 struct list_head tmp_list;
7076 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007077
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307078 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
7079 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007080
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307081 /* removing unresponding end devices */
7082 printk(MPT2SAS_INFO_FMT "removing unresponding devices: end-devices\n",
7083 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007084 list_for_each_entry_safe(sas_device, sas_device_next,
7085 &ioc->sas_device_list, list) {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307086 if (!sas_device->responding)
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05307087 mpt2sas_device_remove_by_sas_address(ioc,
7088 sas_device->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307089 else
Eric Moore635374e2009-03-09 01:21:12 -06007090 sas_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007091 }
7092
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307093 /* removing unresponding volumes */
7094 if (ioc->ir_firmware) {
7095 printk(MPT2SAS_INFO_FMT "removing unresponding devices: "
7096 "volumes\n", ioc->name);
7097 list_for_each_entry_safe(raid_device, raid_device_next,
7098 &ioc->raid_device_list, list) {
7099 if (!raid_device->responding)
7100 _scsih_sas_volume_delete(ioc,
7101 raid_device->handle);
7102 else
7103 raid_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007104 }
Eric Moore635374e2009-03-09 01:21:12 -06007105 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307106 /* removing unresponding expanders */
7107 printk(MPT2SAS_INFO_FMT "removing unresponding devices: expanders\n",
7108 ioc->name);
7109 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7110 INIT_LIST_HEAD(&tmp_list);
7111 list_for_each_entry_safe(sas_expander, sas_expander_next,
7112 &ioc->sas_expander_list, list) {
7113 if (!sas_expander->responding)
7114 list_move_tail(&sas_expander->list, &tmp_list);
7115 else
Eric Moore635374e2009-03-09 01:21:12 -06007116 sas_expander->responding = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307117 }
7118 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7119 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7120 list) {
7121 list_del(&sas_expander->list);
7122 _scsih_expander_node_remove(ioc, sas_expander);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307123 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307124 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
7125 ioc->name);
7126 /* unblock devices */
7127 _scsih_ublock_io_all_device(ioc);
7128}
7129
7130static void
7131_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
7132 struct _sas_node *sas_expander, u16 handle)
7133{
7134 Mpi2ExpanderPage1_t expander_pg1;
7135 Mpi2ConfigReply_t mpi_reply;
7136 int i;
7137
7138 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7139 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
7140 &expander_pg1, i, handle))) {
7141 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7142 ioc->name, __FILE__, __LINE__, __func__);
7143 return;
7144 }
7145
7146 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
7147 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7148 expander_pg1.NegotiatedLinkRate >> 4);
7149 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307150}
7151
7152/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307153 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307154 * @ioc: per adapter object
7155 *
7156 * Return nothing.
7157 */
7158static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307159_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307160{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307161 Mpi2ExpanderPage0_t expander_pg0;
7162 Mpi2SasDevicePage0_t sas_device_pg0;
7163 Mpi2RaidVolPage1_t volume_pg1;
7164 Mpi2RaidVolPage0_t volume_pg0;
7165 Mpi2RaidPhysDiskPage0_t pd_pg0;
7166 Mpi2EventIrConfigElement_t element;
7167 Mpi2ConfigReply_t mpi_reply;
7168 u8 phys_disk_num;
7169 u16 ioc_status;
7170 u16 handle, parent_handle;
7171 u64 sas_address;
7172 struct _sas_device *sas_device;
7173 struct _sas_node *expander_device;
7174 static struct _raid_device *raid_device;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307175 u8 retry_count;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307176 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307177
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307178 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307179
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307180 _scsih_sas_host_refresh(ioc);
7181
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307182 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders start\n",
7183 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307184 /* expanders */
7185 handle = 0xFFFF;
7186 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7187 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7188 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7189 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307190 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7191 printk(MPT2SAS_INFO_FMT "\tbreak from expander scan: "
7192 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7193 ioc->name, ioc_status,
7194 le32_to_cpu(mpi_reply.IOCLogInfo));
7195 break;
7196 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307197 handle = le16_to_cpu(expander_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307198 spin_lock_irqsave(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307199 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7200 ioc, le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307201 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307202 if (expander_device)
7203 _scsih_refresh_expander_links(ioc, expander_device,
7204 handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307205 else {
7206 printk(MPT2SAS_INFO_FMT "\tBEFORE adding expander: "
7207 "handle (0x%04x), sas_addr(0x%016llx)\n",
7208 ioc->name, handle, (unsigned long long)
7209 le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307210 _scsih_expander_add(ioc, handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307211 printk(MPT2SAS_INFO_FMT "\tAFTER adding expander: "
7212 "handle (0x%04x), sas_addr(0x%016llx)\n",
7213 ioc->name, handle, (unsigned long long)
7214 le64_to_cpu(expander_pg0.SASAddress));
7215 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307216 }
7217
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307218 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders complete\n",
7219 ioc->name);
7220
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307221 if (!ioc->ir_firmware)
7222 goto skip_to_sas;
7223
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307224 printk(MPT2SAS_INFO_FMT "\tscan devices phys disk start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307225 /* phys disk */
7226 phys_disk_num = 0xFF;
7227 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7228 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7229 phys_disk_num))) {
7230 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7231 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307232 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7233 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan:"
7234 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7235 ioc->name, ioc_status,
7236 le32_to_cpu(mpi_reply.IOCLogInfo));
7237 break;
7238 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307239 phys_disk_num = pd_pg0.PhysDiskNum;
7240 handle = le16_to_cpu(pd_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307241 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307242 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307243 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307244 if (sas_device)
7245 continue;
7246 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7247 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7248 handle) != 0)
7249 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307250 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7251 MPI2_IOCSTATUS_MASK;
7252 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7253 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan "
7254 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7255 ioc->name, ioc_status,
7256 le32_to_cpu(mpi_reply.IOCLogInfo));
7257 break;
7258 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307259 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7260 if (!_scsih_get_sas_address(ioc, parent_handle,
7261 &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307262 printk(MPT2SAS_INFO_FMT "\tBEFORE adding phys disk: "
7263 " handle (0x%04x), sas_addr(0x%016llx)\n",
7264 ioc->name, handle, (unsigned long long)
7265 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307266 mpt2sas_transport_update_links(ioc, sas_address,
7267 handle, sas_device_pg0.PhyNum,
7268 MPI2_SAS_NEG_LINK_RATE_1_5);
7269 set_bit(handle, ioc->pd_handles);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307270 retry_count = 0;
7271 /* This will retry adding the end device.
7272 * _scsih_add_device() will decide on retries and
7273 * return "1" when it should be retried
7274 */
7275 while (_scsih_add_device(ioc, handle, retry_count++,
7276 1)) {
7277 ssleep(1);
7278 }
7279 printk(MPT2SAS_INFO_FMT "\tAFTER adding phys disk: "
7280 " handle (0x%04x), sas_addr(0x%016llx)\n",
7281 ioc->name, handle, (unsigned long long)
7282 le64_to_cpu(sas_device_pg0.SASAddress));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307283 }
7284 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307285
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307286 printk(MPT2SAS_INFO_FMT "\tscan devices: phys disk complete\n",
7287 ioc->name);
7288
7289 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307290 /* volumes */
7291 handle = 0xFFFF;
7292 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7293 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7294 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7295 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307296 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7297 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7298 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7299 ioc->name, ioc_status,
7300 le32_to_cpu(mpi_reply.IOCLogInfo));
7301 break;
7302 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307303 handle = le16_to_cpu(volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307304 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307305 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7306 le64_to_cpu(volume_pg1.WWID));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307307 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307308 if (raid_device)
7309 continue;
7310 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7311 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7312 sizeof(Mpi2RaidVolPage0_t)))
7313 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307314 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7315 MPI2_IOCSTATUS_MASK;
7316 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7317 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7318 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7319 ioc->name, ioc_status,
7320 le32_to_cpu(mpi_reply.IOCLogInfo));
7321 break;
7322 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307323 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7324 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7325 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7326 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7327 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7328 element.VolDevHandle = volume_pg1.DevHandle;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307329 printk(MPT2SAS_INFO_FMT "\tBEFORE adding volume: "
7330 " handle (0x%04x)\n", ioc->name,
7331 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307332 _scsih_sas_volume_add(ioc, &element);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307333 printk(MPT2SAS_INFO_FMT "\tAFTER adding volume: "
7334 " handle (0x%04x)\n", ioc->name,
7335 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307336 }
7337 }
7338
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307339 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes complete\n",
7340 ioc->name);
7341
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307342 skip_to_sas:
7343
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307344 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices start\n",
7345 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307346 /* sas devices */
7347 handle = 0xFFFF;
7348 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7349 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7350 handle))) {
7351 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7352 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307353 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7354 printk(MPT2SAS_INFO_FMT "\tbreak from end device scan:"
7355 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7356 ioc->name, ioc_status,
7357 le32_to_cpu(mpi_reply.IOCLogInfo));
7358 break;
7359 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307360 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7361 if (!(_scsih_is_end_device(
7362 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7363 continue;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307364 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307365 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7366 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307367 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307368 if (sas_device)
7369 continue;
7370 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7371 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307372 printk(MPT2SAS_INFO_FMT "\tBEFORE adding end device: "
7373 "handle (0x%04x), sas_addr(0x%016llx)\n",
7374 ioc->name, handle, (unsigned long long)
7375 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307376 mpt2sas_transport_update_links(ioc, sas_address, handle,
7377 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307378 retry_count = 0;
7379 /* This will retry adding the end device.
7380 * _scsih_add_device() will decide on retries and
7381 * return "1" when it should be retried
7382 */
7383 while (_scsih_add_device(ioc, handle, retry_count++,
7384 0)) {
7385 ssleep(1);
7386 }
7387 printk(MPT2SAS_INFO_FMT "\tAFTER adding end device: "
7388 "handle (0x%04x), sas_addr(0x%016llx)\n",
7389 ioc->name, handle, (unsigned long long)
7390 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307391 }
7392 }
7393
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307394 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices complete\n",
7395 ioc->name);
7396
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307397 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307398}
7399
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307400
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307401/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307402 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7403 * @ioc: per adapter object
7404 * @reset_phase: phase
7405 *
7406 * The handler for doing any required cleanup or initialization.
7407 *
7408 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7409 * MPT2_IOC_DONE_RESET
7410 *
7411 * Return nothing.
7412 */
7413void
7414mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7415{
7416 switch (reset_phase) {
7417 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307418 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307419 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307420 break;
7421 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307422 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307423 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307424 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7425 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7426 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7427 complete(&ioc->scsih_cmds.done);
7428 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307429 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7430 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7431 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7432 complete(&ioc->tm_cmds.done);
7433 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307434 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307435 _scsih_flush_running_cmds(ioc);
7436 break;
7437 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307438 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307439 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307440 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307441 _scsih_prep_device_scan(ioc);
7442 _scsih_search_responding_sas_devices(ioc);
7443 _scsih_search_responding_raid_devices(ioc);
7444 _scsih_search_responding_expanders(ioc);
sreekanth.reddy@lsi.com14aa7f72012-07-17 15:56:13 +05307445 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7446 !ioc->sas_hba.num_phys)) {
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307447 _scsih_prep_device_scan(ioc);
7448 _scsih_search_responding_sas_devices(ioc);
7449 _scsih_search_responding_raid_devices(ioc);
7450 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307451 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307452 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307453 break;
Eric Moore635374e2009-03-09 01:21:12 -06007454 }
7455}
7456
7457/**
7458 * _firmware_event_work - delayed task for processing firmware events
7459 * @ioc: per adapter object
7460 * @work: equal to the fw_event_work object
7461 * Context: user.
7462 *
7463 * Return nothing.
7464 */
7465static void
7466_firmware_event_work(struct work_struct *work)
7467{
7468 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307469 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007470 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7471
Eric Moore635374e2009-03-09 01:21:12 -06007472 /* the queue is being flushed so ignore this event */
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05307473 if (ioc->remove_host ||
Eric Moore3cb54692010-07-08 14:44:34 -06007474 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007475 _scsih_fw_event_free(ioc, fw_event);
7476 return;
7477 }
Eric Moore635374e2009-03-09 01:21:12 -06007478
Eric Moore635374e2009-03-09 01:21:12 -06007479 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307480 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307481 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307482 ssleep(1);
7483 _scsih_remove_unresponding_sas_devices(ioc);
7484 _scsih_scan_for_devices_after_reset(ioc);
7485 break;
7486 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307487 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307488
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +05307489 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7490 mpt2sas_base_update_missing_delay(ioc, missing_delay[0],
7491 missing_delay[1]);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307492
7493 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7494 "from worker thread\n", ioc->name));
7495 break;
Sreekanth Reddybd58ea32014-09-12 15:35:19 +05307496 case MPT2SAS_TURN_ON_PFA_LED:
7497 _scsih_turn_on_pfa_led(ioc, fw_event->device_handle);
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307498 break;
Eric Moore635374e2009-03-09 01:21:12 -06007499 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307500 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007501 break;
7502 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307503 _scsih_sas_device_status_change_event(ioc,
7504 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007505 break;
7506 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307507 _scsih_sas_discovery_event(ioc,
7508 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007509 break;
7510 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Andrew Mortona78e21d2012-02-08 12:52:22 -08007511 _scsih_sas_broadcast_primitive_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307512 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007513 break;
7514 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7515 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307516 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007517 break;
7518 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307519 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007520 break;
7521 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307522 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007523 break;
7524 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307525 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007526 break;
7527 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307528 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007529 break;
Eric Moore635374e2009-03-09 01:21:12 -06007530 }
7531 _scsih_fw_event_free(ioc, fw_event);
7532}
7533
7534/**
7535 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7536 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307537 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007538 * @reply: reply message frame(lower 32bit addr)
7539 * Context: interrupt.
7540 *
7541 * This function merely adds a new work task into ioc->firmware_event_thread.
7542 * The tasks are worked from _firmware_event_work in user context.
7543 *
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307544 * Returns void.
Eric Moore635374e2009-03-09 01:21:12 -06007545 */
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307546void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307547mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7548 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007549{
7550 struct fw_event_work *fw_event;
7551 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007552 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307553 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007554
7555 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007556 if (ioc->remove_host || ioc->pci_error_recovery)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307557 return;
Eric Moore635374e2009-03-09 01:21:12 -06007558
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307559 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05307560
7561 if (unlikely(!mpi_reply)) {
7562 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7563 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307564 return;
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05307565 }
7566
Eric Moore635374e2009-03-09 01:21:12 -06007567 event = le16_to_cpu(mpi_reply->Event);
7568
7569 switch (event) {
7570 /* handle these */
7571 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7572 {
7573 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7574 (Mpi2EventDataSasBroadcastPrimitive_t *)
7575 mpi_reply->EventData;
7576
7577 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307578 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307579 return;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307580
7581 if (ioc->broadcast_aen_busy) {
7582 ioc->broadcast_aen_pending++;
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307583 return;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307584 } else
7585 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007586 break;
7587 }
7588
7589 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7590 _scsih_check_topo_delete_events(ioc,
7591 (Mpi2EventDataSasTopologyChangeList_t *)
7592 mpi_reply->EventData);
7593 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307594 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7595 _scsih_check_ir_config_unhide_events(ioc,
7596 (Mpi2EventDataIrConfigChangeList_t *)
7597 mpi_reply->EventData);
7598 break;
7599 case MPI2_EVENT_IR_VOLUME:
7600 _scsih_check_volume_delete_events(ioc,
7601 (Mpi2EventDataIrVolume_t *)
7602 mpi_reply->EventData);
7603 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307604 case MPI2_EVENT_LOG_ENTRY_ADDED:
7605 {
7606 Mpi2EventDataLogEntryAdded_t *log_entry;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307607 __le32 *log_code;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307608
7609 if (!ioc->is_warpdrive)
7610 break;
7611
7612 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7613 mpi_reply->EventData;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307614 log_code = (__le32 *)log_entry->LogData;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307615
7616 if (le16_to_cpu(log_entry->LogEntryQualifier)
7617 != MPT2_WARPDRIVE_LOGENTRY)
7618 break;
7619
7620 switch (le32_to_cpu(*log_code)) {
7621 case MPT2_WARPDRIVE_LC_SSDT:
7622 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7623 "IO Throttling has occurred in the WarpDrive "
7624 "subsystem. Check WarpDrive documentation for "
7625 "additional details.\n", ioc->name);
7626 break;
7627 case MPT2_WARPDRIVE_LC_SSDLW:
7628 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7629 "Program/Erase Cycles for the WarpDrive subsystem "
7630 "in degraded range. Check WarpDrive documentation "
7631 "for additional details.\n", ioc->name);
7632 break;
7633 case MPT2_WARPDRIVE_LC_SSDLF:
7634 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7635 "There are no Program/Erase Cycles for the "
7636 "WarpDrive subsystem. The storage device will be "
7637 "in read-only mode. Check WarpDrive documentation "
7638 "for additional details.\n", ioc->name);
7639 break;
7640 case MPT2_WARPDRIVE_LC_BRMF:
7641 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7642 "The Backup Rail Monitor has failed on the "
7643 "WarpDrive subsystem. Check WarpDrive "
7644 "documentation for additional details.\n",
7645 ioc->name);
7646 break;
7647 }
7648
7649 break;
7650 }
Eric Moore635374e2009-03-09 01:21:12 -06007651 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7652 case MPI2_EVENT_IR_OPERATION_STATUS:
7653 case MPI2_EVENT_SAS_DISCOVERY:
7654 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007655 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007656 break;
7657
7658 default: /* ignore the rest */
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307659 return;
Eric Moore635374e2009-03-09 01:21:12 -06007660 }
7661
Joe Lawrence00713ad2014-06-25 17:03:33 -04007662 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7663 fw_event = kzalloc(sizeof(*fw_event) + sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007664 if (!fw_event) {
7665 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7666 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307667 return;
Eric Moore635374e2009-03-09 01:21:12 -06007668 }
Eric Moore635374e2009-03-09 01:21:12 -06007669
Joe Lawrence00713ad2014-06-25 17:03:33 -04007670 memcpy(fw_event->event_data, mpi_reply->EventData, sz);
Eric Moore635374e2009-03-09 01:21:12 -06007671 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307672 fw_event->VF_ID = mpi_reply->VF_ID;
7673 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007674 fw_event->event = event;
7675 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307676 return;
Eric Moore635374e2009-03-09 01:21:12 -06007677}
7678
7679/* shost template */
7680static struct scsi_host_template scsih_driver_template = {
7681 .module = THIS_MODULE,
7682 .name = "Fusion MPT SAS Host",
7683 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007684 .queuecommand = _scsih_qcmd,
7685 .target_alloc = _scsih_target_alloc,
7686 .slave_alloc = _scsih_slave_alloc,
7687 .slave_configure = _scsih_slave_configure,
7688 .target_destroy = _scsih_target_destroy,
7689 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307690 .scan_finished = _scsih_scan_finished,
7691 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007692 .change_queue_depth = _scsih_change_queue_depth,
7693 .change_queue_type = _scsih_change_queue_type,
7694 .eh_abort_handler = _scsih_abort,
7695 .eh_device_reset_handler = _scsih_dev_reset,
7696 .eh_target_reset_handler = _scsih_target_reset,
7697 .eh_host_reset_handler = _scsih_host_reset,
7698 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007699 .can_queue = 1,
7700 .this_id = -1,
7701 .sg_tablesize = MPT2SAS_SG_DEPTH,
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05307702 .max_sectors = 32767,
Eric Moore635374e2009-03-09 01:21:12 -06007703 .cmd_per_lun = 7,
7704 .use_clustering = ENABLE_CLUSTERING,
7705 .shost_attrs = mpt2sas_host_attrs,
7706 .sdev_attrs = mpt2sas_dev_attrs,
7707};
7708
7709/**
7710 * _scsih_expander_node_remove - removing expander device from list.
7711 * @ioc: per adapter object
7712 * @sas_expander: the sas_device object
7713 * Context: Calling function should acquire ioc->sas_node_lock.
7714 *
7715 * Removing object and freeing associated memory from the
7716 * ioc->sas_expander_list.
7717 *
7718 * Return nothing.
7719 */
7720static void
7721_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7722 struct _sas_node *sas_expander)
7723{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307724 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007725
7726 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307727 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007728 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307729 if (ioc->shost_recovery)
7730 return;
Eric Moore635374e2009-03-09 01:21:12 -06007731 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307732 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307733 mpt2sas_device_remove_by_sas_address(ioc,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307734 mpt2sas_port->remote_identify.sas_address);
7735 else if (mpt2sas_port->remote_identify.device_type ==
7736 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007737 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307738 SAS_FANOUT_EXPANDER_DEVICE)
7739 mpt2sas_expander_remove(ioc,
7740 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007741 }
7742
7743 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307744 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007745
7746 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7747 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7748 sas_expander->handle, (unsigned long long)
7749 sas_expander->sas_address);
7750
Eric Moore635374e2009-03-09 01:21:12 -06007751 kfree(sas_expander->phy);
7752 kfree(sas_expander);
7753}
7754
7755/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307756 * _scsih_ir_shutdown - IR shutdown notification
7757 * @ioc: per adapter object
7758 *
7759 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7760 * the host system is shutting down.
7761 *
7762 * Return nothing.
7763 */
7764static void
7765_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7766{
7767 Mpi2RaidActionRequest_t *mpi_request;
7768 Mpi2RaidActionReply_t *mpi_reply;
7769 u16 smid;
7770
7771 /* is IR firmware build loaded ? */
7772 if (!ioc->ir_firmware)
7773 return;
7774
Kashyap, Desai744090d2009-10-05 15:56:56 +05307775 mutex_lock(&ioc->scsih_cmds.mutex);
7776
7777 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7778 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7779 ioc->name, __func__);
7780 goto out;
7781 }
7782 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7783
7784 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7785 if (!smid) {
7786 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7787 ioc->name, __func__);
7788 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7789 goto out;
7790 }
7791
7792 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7793 ioc->scsih_cmds.smid = smid;
7794 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7795
7796 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7797 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7798
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307799 if (!ioc->hide_ir_msg)
7800 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307801 init_completion(&ioc->scsih_cmds.done);
7802 mpt2sas_base_put_smid_default(ioc, smid);
7803 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7804
7805 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7806 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7807 ioc->name, __func__);
7808 goto out;
7809 }
7810
7811 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7812 mpi_reply = ioc->scsih_cmds.reply;
7813
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307814 if (!ioc->hide_ir_msg)
7815 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7816 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7817 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7818 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307819 }
7820
7821 out:
7822 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7823 mutex_unlock(&ioc->scsih_cmds.mutex);
7824}
7825
7826/**
7827 * _scsih_shutdown - routine call during system shutdown
7828 * @pdev: PCI device struct
7829 *
7830 * Return nothing.
7831 */
7832static void
7833_scsih_shutdown(struct pci_dev *pdev)
7834{
7835 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7836 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307837 struct workqueue_struct *wq;
7838 unsigned long flags;
7839
7840 ioc->remove_host = 1;
7841 _scsih_fw_event_cleanup_queue(ioc);
7842
7843 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7844 wq = ioc->firmware_event_thread;
7845 ioc->firmware_event_thread = NULL;
7846 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7847 if (wq)
7848 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307849
7850 _scsih_ir_shutdown(ioc);
7851 mpt2sas_base_detach(ioc);
7852}
7853
7854/**
Eric Moored5d135b2009-05-18 13:02:08 -06007855 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007856 * @pdev: PCI device struct
7857 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307858 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007859 * Return nothing.
7860 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007861static void
Eric Moored5d135b2009-05-18 13:02:08 -06007862_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007863{
7864 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7865 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307866 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307867 struct _raid_device *raid_device, *next;
7868 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007869 struct workqueue_struct *wq;
7870 unsigned long flags;
7871
7872 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307873 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007874
7875 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7876 wq = ioc->firmware_event_thread;
7877 ioc->firmware_event_thread = NULL;
7878 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7879 if (wq)
7880 destroy_workqueue(wq);
7881
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307882 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307883 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307884 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7885 list) {
7886 if (raid_device->starget) {
7887 sas_target_priv_data =
7888 raid_device->starget->hostdata;
7889 sas_target_priv_data->deleted = 1;
7890 scsi_remove_target(&raid_device->starget->dev);
7891 }
7892 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7893 "(0x%016llx)\n", ioc->name, raid_device->handle,
7894 (unsigned long long) raid_device->wwid);
7895 _scsih_raid_device_remove(ioc, raid_device);
7896 }
7897
Eric Moore635374e2009-03-09 01:21:12 -06007898 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307899 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007900 &ioc->sas_hba.sas_port_list, port_list) {
7901 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307902 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307903 mpt2sas_device_remove_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007904 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307905 else if (mpt2sas_port->remote_identify.device_type ==
7906 SAS_EDGE_EXPANDER_DEVICE ||
7907 mpt2sas_port->remote_identify.device_type ==
7908 SAS_FANOUT_EXPANDER_DEVICE)
7909 mpt2sas_expander_remove(ioc,
7910 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007911 }
7912
7913 /* free phys attached to the sas_host */
7914 if (ioc->sas_hba.num_phys) {
7915 kfree(ioc->sas_hba.phy);
7916 ioc->sas_hba.phy = NULL;
7917 ioc->sas_hba.num_phys = 0;
7918 }
7919
7920 sas_remove_host(shost);
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05307921 scsi_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307922 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007923 list_del(&ioc->list);
Eric Moore635374e2009-03-09 01:21:12 -06007924 scsi_host_put(shost);
7925}
7926
7927/**
7928 * _scsih_probe_boot_devices - reports 1st device
7929 * @ioc: per adapter object
7930 *
7931 * If specified in bios page 2, this routine reports the 1st
7932 * device scsi-ml or sas transport for persistent boot device
7933 * purposes. Please refer to function _scsih_determine_boot_device()
7934 */
7935static void
7936_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7937{
7938 u8 is_raid;
7939 void *device;
7940 struct _sas_device *sas_device;
7941 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307942 u16 handle;
7943 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007944 u64 sas_address;
7945 unsigned long flags;
7946 int rc;
7947
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307948 /* no Bios, return immediately */
7949 if (!ioc->bios_pg3.BiosVersion)
7950 return;
7951
Eric Moore635374e2009-03-09 01:21:12 -06007952 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307953 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007954 if (ioc->req_boot_device.device) {
7955 device = ioc->req_boot_device.device;
7956 is_raid = ioc->req_boot_device.is_raid;
7957 } else if (ioc->req_alt_boot_device.device) {
7958 device = ioc->req_alt_boot_device.device;
7959 is_raid = ioc->req_alt_boot_device.is_raid;
7960 } else if (ioc->current_boot_device.device) {
7961 device = ioc->current_boot_device.device;
7962 is_raid = ioc->current_boot_device.is_raid;
7963 }
7964
7965 if (!device)
7966 return;
7967
7968 if (is_raid) {
7969 raid_device = device;
7970 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7971 raid_device->id, 0);
7972 if (rc)
7973 _scsih_raid_device_remove(ioc, raid_device);
7974 } else {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307975 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007976 sas_device = device;
7977 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307978 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007979 sas_address = sas_device->sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06007980 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7981 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307982
7983 if (ioc->hide_drives)
7984 return;
Eric Moore635374e2009-03-09 01:21:12 -06007985 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307986 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007987 _scsih_sas_device_remove(ioc, sas_device);
7988 } else if (!sas_device->starget) {
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307989 if (!ioc->is_driver_loading) {
7990 mpt2sas_transport_port_remove(ioc,
7991 sas_address,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307992 sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307993 _scsih_sas_device_remove(ioc, sas_device);
7994 }
Eric Moore635374e2009-03-09 01:21:12 -06007995 }
7996 }
7997}
7998
7999/**
8000 * _scsih_probe_raid - reporting raid volumes to scsi-ml
8001 * @ioc: per adapter object
8002 *
8003 * Called during initial loading of the driver.
8004 */
8005static void
8006_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
8007{
8008 struct _raid_device *raid_device, *raid_next;
8009 int rc;
8010
8011 list_for_each_entry_safe(raid_device, raid_next,
8012 &ioc->raid_device_list, list) {
8013 if (raid_device->starget)
8014 continue;
8015 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
8016 raid_device->id, 0);
8017 if (rc)
8018 _scsih_raid_device_remove(ioc, raid_device);
8019 }
8020}
8021
8022/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308023 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06008024 * @ioc: per adapter object
8025 *
8026 * Called during initial loading of the driver.
8027 */
8028static void
8029_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
8030{
8031 struct _sas_device *sas_device, *next;
8032 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06008033
8034 /* SAS Device List */
8035 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
8036 list) {
Eric Moore635374e2009-03-09 01:21:12 -06008037
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308038 if (ioc->hide_drives)
8039 continue;
8040
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05308041 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
8042 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05308043 list_del(&sas_device->list);
8044 kfree(sas_device);
8045 continue;
Eric Moore635374e2009-03-09 01:21:12 -06008046 } else if (!sas_device->starget) {
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05308047 if (!ioc->is_driver_loading) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05308048 mpt2sas_transport_port_remove(ioc,
8049 sas_device->sas_address,
8050 sas_device->sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05308051 list_del(&sas_device->list);
8052 kfree(sas_device);
8053 continue;
8054 }
Eric Moore635374e2009-03-09 01:21:12 -06008055 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05308056 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8057 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8058 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06008059 }
8060}
8061
8062/**
8063 * _scsih_probe_devices - probing for devices
8064 * @ioc: per adapter object
8065 *
8066 * Called during initial loading of the driver.
8067 */
8068static void
8069_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
8070{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308071 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06008072
8073 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8074 return; /* return when IOC doesn't support initiator mode */
8075
8076 _scsih_probe_boot_devices(ioc);
8077
8078 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308079 volume_mapping_flags =
8080 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8081 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8082 if (volume_mapping_flags ==
8083 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06008084 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308085 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008086 } else {
Eric Moore635374e2009-03-09 01:21:12 -06008087 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308088 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008089 }
8090 } else
8091 _scsih_probe_sas(ioc);
8092}
8093
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308094
8095/**
8096 * _scsih_scan_start - scsi lld callback for .scan_start
8097 * @shost: SCSI host pointer
8098 *
8099 * The shost has the ability to discover targets on its own instead
8100 * of scanning the entire bus. In our implemention, we will kick off
8101 * firmware discovery.
8102 */
8103static void
8104_scsih_scan_start(struct Scsi_Host *shost)
8105{
8106 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8107 int rc;
8108
8109 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8110 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
8111
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308112 if (disable_discovery > 0)
8113 return;
8114
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308115 ioc->start_scan = 1;
8116 rc = mpt2sas_port_enable(ioc);
8117
8118 if (rc != 0)
8119 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
8120}
8121
8122/**
8123 * _scsih_scan_finished - scsi lld callback for .scan_finished
8124 * @shost: SCSI host pointer
8125 * @time: elapsed time of the scan in jiffies
8126 *
8127 * This function will be called periodically until it returns 1 with the
8128 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8129 * we wait for firmware discovery to complete, then return 1.
8130 */
8131static int
8132_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8133{
8134 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8135
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308136 if (disable_discovery > 0) {
8137 ioc->is_driver_loading = 0;
8138 ioc->wait_for_discovery_to_complete = 0;
8139 return 1;
8140 }
8141
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308142 if (time >= (300 * HZ)) {
8143 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8144 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
8145 "(timeout=300s)\n", ioc->name);
8146 ioc->is_driver_loading = 0;
8147 return 1;
8148 }
8149
8150 if (ioc->start_scan)
8151 return 0;
8152
8153 if (ioc->start_scan_failed) {
8154 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
8155 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
8156 ioc->is_driver_loading = 0;
8157 ioc->wait_for_discovery_to_complete = 0;
8158 ioc->remove_host = 1;
8159 return 1;
8160 }
8161
8162 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
8163 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8164
8165 if (ioc->wait_for_discovery_to_complete) {
8166 ioc->wait_for_discovery_to_complete = 0;
8167 _scsih_probe_devices(ioc);
8168 }
8169 mpt2sas_base_start_watchdog(ioc);
8170 ioc->is_driver_loading = 0;
8171 return 1;
8172}
8173
8174
Eric Moore635374e2009-03-09 01:21:12 -06008175/**
Eric Moored5d135b2009-05-18 13:02:08 -06008176 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06008177 * @pdev: PCI device struct
8178 * @id: pci device id
8179 *
8180 * Returns 0 success, anything else error.
8181 */
8182static int
Eric Moored5d135b2009-05-18 13:02:08 -06008183_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06008184{
8185 struct MPT2SAS_ADAPTER *ioc;
8186 struct Scsi_Host *shost;
8187
8188 shost = scsi_host_alloc(&scsih_driver_template,
8189 sizeof(struct MPT2SAS_ADAPTER));
8190 if (!shost)
8191 return -ENODEV;
8192
8193 /* init local params */
8194 ioc = shost_priv(shost);
8195 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
8196 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06008197 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06008198 ioc->shost = shost;
8199 ioc->id = mpt_ids++;
8200 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
8201 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308202 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8203 ioc->is_warpdrive = 1;
8204 ioc->hide_ir_msg = 1;
8205 } else
8206 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06008207 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8208 ioc->tm_cb_idx = tm_cb_idx;
8209 ioc->ctl_cb_idx = ctl_cb_idx;
8210 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308211 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008212 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05308213 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008214 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308215 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308216 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308217 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008218 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05308219 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06008220 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05308221 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06008222 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8223 spin_lock_init(&ioc->scsi_lookup_lock);
8224 spin_lock_init(&ioc->sas_device_lock);
8225 spin_lock_init(&ioc->sas_node_lock);
8226 spin_lock_init(&ioc->fw_event_lock);
8227 spin_lock_init(&ioc->raid_device_lock);
8228
8229 INIT_LIST_HEAD(&ioc->sas_device_list);
8230 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8231 INIT_LIST_HEAD(&ioc->sas_expander_list);
8232 INIT_LIST_HEAD(&ioc->fw_event_list);
8233 INIT_LIST_HEAD(&ioc->raid_device_list);
8234 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308235 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308236 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04008237 INIT_LIST_HEAD(&ioc->reply_queue_list);
Eric Moore635374e2009-03-09 01:21:12 -06008238
8239 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06008240 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06008241 shost->max_lun = max_lun;
8242 shost->transportt = mpt2sas_transport_template;
8243 shost->unique_id = ioc->id;
8244
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308245 if (max_sectors != 0xFFFF) {
8246 if (max_sectors < 64) {
8247 shost->max_sectors = 64;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05308248 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8249 "for max_sectors, range is 64 to 32767. Assigning "
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308250 "value of 64.\n", ioc->name, max_sectors);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308251 } else if (max_sectors > 32767) {
8252 shost->max_sectors = 32767;
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308253 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8254 "for max_sectors, range is 64 to 8192. Assigning "
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308255 "default value of 32767.\n", ioc->name,
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308256 max_sectors);
8257 } else {
8258 shost->max_sectors = max_sectors & 0xFFFE;
8259 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
8260 "set to %d\n", ioc->name, shost->max_sectors);
8261 }
8262 }
8263
Martin K. Petersen5e95e732012-08-28 14:29:37 -04008264 /* register EEDP capabilities with SCSI layer */
8265 if (prot_mask)
8266 scsi_host_set_prot(shost, prot_mask);
8267 else
8268 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8269 | SHOST_DIF_TYPE2_PROTECTION
8270 | SHOST_DIF_TYPE3_PROTECTION);
8271
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308272 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06008273
Eric Moore635374e2009-03-09 01:21:12 -06008274 /* event thread */
8275 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8276 "fw_event%d", ioc->id);
8277 ioc->firmware_event_thread = create_singlethread_workqueue(
8278 ioc->firmware_event_name);
8279 if (!ioc->firmware_event_thread) {
8280 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8281 ioc->name, __FILE__, __LINE__, __func__);
8282 goto out_thread_fail;
8283 }
8284
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308285 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008286 if ((mpt2sas_base_attach(ioc))) {
8287 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8288 ioc->name, __FILE__, __LINE__, __func__);
8289 goto out_attach_fail;
8290 }
8291
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308292 if (ioc->is_warpdrive) {
8293 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8294 ioc->hide_drives = 0;
8295 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8296 ioc->hide_drives = 1;
8297 else {
8298 if (_scsih_get_num_volumes(ioc))
8299 ioc->hide_drives = 1;
8300 else
8301 ioc->hide_drives = 0;
8302 }
8303 } else
8304 ioc->hide_drives = 0;
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05308305
8306 if ((scsi_add_host(shost, &pdev->dev))) {
8307 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8308 ioc->name, __FILE__, __LINE__, __func__);
8309 goto out_add_shost_fail;
8310 }
8311
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05308312 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308313
Eric Moore635374e2009-03-09 01:21:12 -06008314 return 0;
8315
Reddy, Sreekanth02b77082014-07-14 12:00:49 +05308316 out_add_shost_fail:
8317 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008318 out_attach_fail:
8319 destroy_workqueue(ioc->firmware_event_thread);
8320 out_thread_fail:
8321 list_del(&ioc->list);
Tomas Henzlf555e052012-04-17 15:24:48 +02008322 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008323 return -ENODEV;
8324}
8325
8326#ifdef CONFIG_PM
8327/**
Eric Moored5d135b2009-05-18 13:02:08 -06008328 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008329 * @pdev: PCI device struct
8330 * @state: PM state change to (usually PCI_D3)
8331 *
8332 * Returns 0 success, anything else error.
8333 */
8334static int
Eric Moored5d135b2009-05-18 13:02:08 -06008335_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008336{
8337 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8338 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308339 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008340
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308341 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008342 scsi_block_requests(shost);
Sreekanth Reddy3627dba2013-07-25 11:25:57 +05308343 _scsih_ir_shutdown(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008344 device_state = pci_choose_state(pdev, state);
8345 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8346 "operating state [D%d]\n", ioc->name, pdev,
8347 pci_name(pdev), device_state);
8348
8349 mpt2sas_base_free_resources(ioc);
8350 pci_save_state(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06008351 pci_set_power_state(pdev, device_state);
8352 return 0;
8353}
8354
8355/**
Eric Moored5d135b2009-05-18 13:02:08 -06008356 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008357 * @pdev: PCI device struct
8358 *
8359 * Returns 0 success, anything else error.
8360 */
8361static int
Eric Moored5d135b2009-05-18 13:02:08 -06008362_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008363{
8364 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8365 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308366 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008367 int r;
8368
8369 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8370 "operating state [D%d]\n", ioc->name, pdev,
8371 pci_name(pdev), device_state);
8372
8373 pci_set_power_state(pdev, PCI_D0);
8374 pci_enable_wake(pdev, PCI_D0, 0);
8375 pci_restore_state(pdev);
8376 ioc->pdev = pdev;
8377 r = mpt2sas_base_map_resources(ioc);
8378 if (r)
8379 return r;
8380
8381 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8382 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308383 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008384 return 0;
8385}
8386#endif /* CONFIG_PM */
8387
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308388/**
8389 * _scsih_pci_error_detected - Called when a PCI error is detected.
8390 * @pdev: PCI device struct
8391 * @state: PCI channel state
8392 *
8393 * Description: Called when a PCI error is detected.
8394 *
8395 * Return value:
8396 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8397 */
8398static pci_ers_result_t
8399_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8400{
8401 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8402 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8403
8404 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8405 ioc->name, state);
8406
8407 switch (state) {
8408 case pci_channel_io_normal:
8409 return PCI_ERS_RESULT_CAN_RECOVER;
8410 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008411 /* Fatal error, prepare for slot reset */
8412 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308413 scsi_block_requests(ioc->shost);
8414 mpt2sas_base_stop_watchdog(ioc);
8415 mpt2sas_base_free_resources(ioc);
8416 return PCI_ERS_RESULT_NEED_RESET;
8417 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008418 /* Permanent error, prepare for device removal */
8419 ioc->pci_error_recovery = 1;
8420 mpt2sas_base_stop_watchdog(ioc);
8421 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308422 return PCI_ERS_RESULT_DISCONNECT;
8423 }
8424 return PCI_ERS_RESULT_NEED_RESET;
8425}
8426
8427/**
8428 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8429 * @pdev: PCI device struct
8430 *
8431 * Description: This routine is called by the pci error recovery
8432 * code after the PCI slot has been reset, just before we
8433 * should resume normal operations.
8434 */
8435static pci_ers_result_t
8436_scsih_pci_slot_reset(struct pci_dev *pdev)
8437{
8438 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8439 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8440 int rc;
8441
8442 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8443 ioc->name);
8444
Eric Moore3cb54692010-07-08 14:44:34 -06008445 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308446 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008447 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308448 rc = mpt2sas_base_map_resources(ioc);
8449 if (rc)
8450 return PCI_ERS_RESULT_DISCONNECT;
8451
8452
8453 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8454 FORCE_BIG_HAMMER);
8455
8456 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8457 (rc == 0) ? "success" : "failed");
8458
8459 if (!rc)
8460 return PCI_ERS_RESULT_RECOVERED;
8461 else
8462 return PCI_ERS_RESULT_DISCONNECT;
8463}
8464
8465/**
8466 * _scsih_pci_resume() - resume normal ops after PCI reset
8467 * @pdev: pointer to PCI device
8468 *
8469 * Called when the error recovery driver tells us that its
8470 * OK to resume normal operation. Use completion to allow
8471 * halted scsi ops to resume.
8472 */
8473static void
8474_scsih_pci_resume(struct pci_dev *pdev)
8475{
8476 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8477 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8478
8479 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8480
8481 pci_cleanup_aer_uncorrect_error_status(pdev);
8482 mpt2sas_base_start_watchdog(ioc);
8483 scsi_unblock_requests(ioc->shost);
8484}
8485
8486/**
8487 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8488 * @pdev: pointer to PCI device
8489 */
8490static pci_ers_result_t
8491_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8492{
8493 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8494 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8495
8496 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8497 ioc->name);
8498
8499 /* TODO - dump whatever for debugging purposes */
8500
8501 /* Request a slot reset. */
8502 return PCI_ERS_RESULT_NEED_RESET;
8503}
8504
Stephen Hemmingera55b2d22012-09-07 09:33:16 -07008505static const struct pci_error_handlers _scsih_err_handler = {
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308506 .error_detected = _scsih_pci_error_detected,
8507 .mmio_enabled = _scsih_pci_mmio_enabled,
8508 .slot_reset = _scsih_pci_slot_reset,
8509 .resume = _scsih_pci_resume,
8510};
Eric Moore635374e2009-03-09 01:21:12 -06008511
8512static struct pci_driver scsih_driver = {
8513 .name = MPT2SAS_DRIVER_NAME,
8514 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008515 .probe = _scsih_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008516 .remove = _scsih_remove,
Kashyap, Desai744090d2009-10-05 15:56:56 +05308517 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308518 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008519#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008520 .suspend = _scsih_suspend,
8521 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008522#endif
8523};
8524
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308525/* raid transport support */
8526static struct raid_function_template mpt2sas_raid_functions = {
8527 .cookie = &scsih_driver_template,
8528 .is_raid = _scsih_is_raid,
8529 .get_resync = _scsih_get_resync,
8530 .get_state = _scsih_get_state,
8531};
Eric Moore635374e2009-03-09 01:21:12 -06008532
8533/**
Eric Moored5d135b2009-05-18 13:02:08 -06008534 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008535 *
8536 * Returns 0 success, anything else error.
8537 */
8538static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008539_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008540{
8541 int error;
8542
8543 mpt_ids = 0;
8544 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8545 MPT2SAS_DRIVER_VERSION);
8546
8547 mpt2sas_transport_template =
8548 sas_attach_transport(&mpt2sas_transport_functions);
8549 if (!mpt2sas_transport_template)
8550 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308551 /* raid transport support */
8552 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8553 if (!mpt2sas_raid_template) {
8554 sas_release_transport(mpt2sas_transport_template);
8555 return -ENODEV;
8556 }
Eric Moore635374e2009-03-09 01:21:12 -06008557
8558 mpt2sas_base_initialize_callback_handler();
8559
8560 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008561 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008562
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008563 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008564 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008565
8566 /* base internal commands callback handler */
8567 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308568 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8569 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008570
8571 /* transport internal commands callback handler */
8572 transport_cb_idx = mpt2sas_base_register_callback_handler(
8573 mpt2sas_transport_done);
8574
Kashyap, Desai744090d2009-10-05 15:56:56 +05308575 /* scsih internal commands callback handler */
8576 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8577
Eric Moore635374e2009-03-09 01:21:12 -06008578 /* configuration page API internal commands callback handler */
8579 config_cb_idx = mpt2sas_base_register_callback_handler(
8580 mpt2sas_config_done);
8581
8582 /* ctl module callback handler */
8583 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8584
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308585 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8586 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308587
8588 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8589 _scsih_tm_volume_tr_complete);
8590
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308591 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8592 _scsih_sas_control_complete);
8593
Eric Moore635374e2009-03-09 01:21:12 -06008594 mpt2sas_ctl_init();
8595
8596 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308597 if (error) {
8598 /* raid transport support */
8599 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008600 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308601 }
Eric Moore635374e2009-03-09 01:21:12 -06008602
8603 return error;
8604}
8605
8606/**
Eric Moored5d135b2009-05-18 13:02:08 -06008607 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008608 *
8609 * Returns 0 success, anything else error.
8610 */
8611static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008612_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008613{
8614 printk(KERN_INFO "mpt2sas version %s unloading\n",
8615 MPT2SAS_DRIVER_VERSION);
8616
8617 pci_unregister_driver(&scsih_driver);
8618
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308619 mpt2sas_ctl_exit();
8620
Eric Moore635374e2009-03-09 01:21:12 -06008621 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8622 mpt2sas_base_release_callback_handler(tm_cb_idx);
8623 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308624 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008625 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308626 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008627 mpt2sas_base_release_callback_handler(config_cb_idx);
8628 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8629
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308630 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308631 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308632 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8633
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308634 /* raid transport support */
8635 raid_class_release(mpt2sas_raid_template);
8636 sas_release_transport(mpt2sas_transport_template);
8637
Eric Moore635374e2009-03-09 01:21:12 -06008638}
8639
Eric Moored5d135b2009-05-18 13:02:08 -06008640module_init(_scsih_init);
8641module_exit(_scsih_exit);