blob: e19dfde72066e3b9223b98e9d32ff5118aa857c8 [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 Reddyd648d5a2013-07-25 11:28:01 +05305 * Copyright (C) 2007-2013 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
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530148#define MPT2SAS_TURN_ON_FAULT_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;
176 void *event_data;
177};
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
1295 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1296 if (!sas_target_priv_data)
1297 return -ENOMEM;
1298
1299 starget->hostdata = sas_target_priv_data;
1300 sas_target_priv_data->starget = starget;
1301 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1302
1303 /* RAID volumes */
1304 if (starget->channel == RAID_CHANNEL) {
1305 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1306 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1307 starget->channel);
1308 if (raid_device) {
1309 sas_target_priv_data->handle = raid_device->handle;
1310 sas_target_priv_data->sas_address = raid_device->wwid;
1311 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301312 if (ioc->is_warpdrive)
1313 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001314 raid_device->starget = starget;
1315 }
1316 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1317 return 0;
1318 }
1319
1320 /* sas/sata devices */
1321 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1322 rphy = dev_to_rphy(starget->dev.parent);
1323 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1324 rphy->identify.sas_address);
1325
1326 if (sas_device) {
1327 sas_target_priv_data->handle = sas_device->handle;
1328 sas_target_priv_data->sas_address = sas_device->sas_address;
1329 sas_device->starget = starget;
1330 sas_device->id = starget->id;
1331 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301332 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001333 sas_target_priv_data->flags |=
1334 MPT_TARGET_FLAGS_RAID_COMPONENT;
1335 }
1336 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1337
1338 return 0;
1339}
1340
1341/**
Eric Moored5d135b2009-05-18 13:02:08 -06001342 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001343 * @starget: scsi target struct
1344 *
1345 * Returns nothing.
1346 */
1347static void
Eric Moored5d135b2009-05-18 13:02:08 -06001348_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001349{
1350 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1351 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1352 struct MPT2SAS_TARGET *sas_target_priv_data;
1353 struct _sas_device *sas_device;
1354 struct _raid_device *raid_device;
1355 unsigned long flags;
1356 struct sas_rphy *rphy;
1357
1358 sas_target_priv_data = starget->hostdata;
1359 if (!sas_target_priv_data)
1360 return;
1361
1362 if (starget->channel == RAID_CHANNEL) {
1363 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1364 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1365 starget->channel);
1366 if (raid_device) {
1367 raid_device->starget = NULL;
1368 raid_device->sdev = NULL;
1369 }
1370 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1371 goto out;
1372 }
1373
1374 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1375 rphy = dev_to_rphy(starget->dev.parent);
1376 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1377 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001378 if (sas_device && (sas_device->starget == starget) &&
1379 (sas_device->id == starget->id) &&
1380 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001381 sas_device->starget = NULL;
1382
1383 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1384
1385 out:
1386 kfree(sas_target_priv_data);
1387 starget->hostdata = NULL;
1388}
1389
1390/**
Eric Moored5d135b2009-05-18 13:02:08 -06001391 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001392 * @sdev: scsi device struct
1393 *
1394 * Returns 0 if ok. Any other return is assumed to be an error and
1395 * the device is ignored.
1396 */
1397static int
Eric Moored5d135b2009-05-18 13:02:08 -06001398_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001399{
1400 struct Scsi_Host *shost;
1401 struct MPT2SAS_ADAPTER *ioc;
1402 struct MPT2SAS_TARGET *sas_target_priv_data;
1403 struct MPT2SAS_DEVICE *sas_device_priv_data;
1404 struct scsi_target *starget;
1405 struct _raid_device *raid_device;
Sreekanth Reddy63e359d2013-07-25 11:32:51 +05301406 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -06001407 unsigned long flags;
1408
1409 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1410 if (!sas_device_priv_data)
1411 return -ENOMEM;
1412
1413 sas_device_priv_data->lun = sdev->lun;
1414 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1415
1416 starget = scsi_target(sdev);
1417 sas_target_priv_data = starget->hostdata;
1418 sas_target_priv_data->num_luns++;
1419 sas_device_priv_data->sas_target = sas_target_priv_data;
1420 sdev->hostdata = sas_device_priv_data;
1421 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1422 sdev->no_uld_attach = 1;
1423
1424 shost = dev_to_shost(&starget->dev);
1425 ioc = shost_priv(shost);
1426 if (starget->channel == RAID_CHANNEL) {
1427 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1428 raid_device = _scsih_raid_device_find_by_id(ioc,
1429 starget->id, starget->channel);
1430 if (raid_device)
1431 raid_device->sdev = sdev; /* raid is single lun */
1432 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001433 }
1434
Sreekanth Reddy63e359d2013-07-25 11:32:51 +05301435 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1436 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1437 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1438 sas_target_priv_data->sas_address);
1439 if (sas_device && (sas_device->starget == NULL)) {
1440 sdev_printk(KERN_INFO, sdev,
1441 "%s : sas_device->starget set to starget @ %d\n",
1442 __func__, __LINE__);
1443 sas_device->starget = starget;
1444 }
1445 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1446 }
1447
Eric Moore635374e2009-03-09 01:21:12 -06001448 return 0;
1449}
1450
1451/**
Eric Moored5d135b2009-05-18 13:02:08 -06001452 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001453 * @sdev: scsi device struct
1454 *
1455 * Returns nothing.
1456 */
1457static void
Eric Moored5d135b2009-05-18 13:02:08 -06001458_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001459{
1460 struct MPT2SAS_TARGET *sas_target_priv_data;
1461 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301462 struct Scsi_Host *shost;
1463 struct MPT2SAS_ADAPTER *ioc;
1464 struct _sas_device *sas_device;
1465 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001466
1467 if (!sdev->hostdata)
1468 return;
1469
1470 starget = scsi_target(sdev);
1471 sas_target_priv_data = starget->hostdata;
1472 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301473
1474 shost = dev_to_shost(&starget->dev);
1475 ioc = shost_priv(shost);
1476
1477 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1478 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1479 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1480 sas_target_priv_data->sas_address);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +05301481 if (sas_device && !sas_target_priv_data->num_luns)
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301482 sas_device->starget = NULL;
1483 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1484 }
1485
Eric Moore635374e2009-03-09 01:21:12 -06001486 kfree(sdev->hostdata);
1487 sdev->hostdata = NULL;
1488}
1489
1490/**
Eric Moored5d135b2009-05-18 13:02:08 -06001491 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001492 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301493 * @handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -06001494 * @sdev: scsi device struct
1495 */
1496static void
Eric Moored5d135b2009-05-18 13:02:08 -06001497_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301498 u16 handle, struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001499{
1500 Mpi2ConfigReply_t mpi_reply;
1501 Mpi2SasDevicePage0_t sas_device_pg0;
1502 u32 ioc_status;
1503 u16 flags;
1504 u32 device_info;
1505
1506 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301507 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06001508 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1509 ioc->name, __FILE__, __LINE__, __func__);
1510 return;
1511 }
1512
1513 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1514 MPI2_IOCSTATUS_MASK;
1515 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1516 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1517 ioc->name, __FILE__, __LINE__, __func__);
1518 return;
1519 }
1520
1521 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301522 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001523
1524 sdev_printk(KERN_INFO, sdev,
1525 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1526 "sw_preserve(%s)\n",
1527 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1528 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1529 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1530 "n",
1531 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1532 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1533 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1534}
1535
1536/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301537 * _scsih_is_raid - return boolean indicating device is raid volume
1538 * @dev the device struct object
1539 */
1540static int
1541_scsih_is_raid(struct device *dev)
1542{
1543 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301544 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301545
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301546 if (ioc->is_warpdrive)
1547 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301548 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1549}
1550
1551/**
1552 * _scsih_get_resync - get raid volume resync percent complete
1553 * @dev the device struct object
1554 */
1555static void
1556_scsih_get_resync(struct device *dev)
1557{
1558 struct scsi_device *sdev = to_scsi_device(dev);
1559 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1560 static struct _raid_device *raid_device;
1561 unsigned long flags;
1562 Mpi2RaidVolPage0_t vol_pg0;
1563 Mpi2ConfigReply_t mpi_reply;
1564 u32 volume_status_flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301565 u8 percent_complete;
1566 u16 handle;
1567
1568 percent_complete = 0;
1569 handle = 0;
1570 if (ioc->is_warpdrive)
1571 goto out;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301572
1573 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1574 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1575 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301576 if (raid_device) {
1577 handle = raid_device->handle;
1578 percent_complete = raid_device->percent_complete;
1579 }
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301580 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1581
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301582 if (!handle)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301583 goto out;
1584
1585 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301586 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301587 sizeof(Mpi2RaidVolPage0_t))) {
1588 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1589 ioc->name, __FILE__, __LINE__, __func__);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301590 percent_complete = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301591 goto out;
1592 }
1593
1594 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301595 if (!(volume_status_flags &
1596 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1597 percent_complete = 0;
1598
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301599 out:
1600 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1601}
1602
1603/**
1604 * _scsih_get_state - get raid volume level
1605 * @dev the device struct object
1606 */
1607static void
1608_scsih_get_state(struct device *dev)
1609{
1610 struct scsi_device *sdev = to_scsi_device(dev);
1611 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1612 static struct _raid_device *raid_device;
1613 unsigned long flags;
1614 Mpi2RaidVolPage0_t vol_pg0;
1615 Mpi2ConfigReply_t mpi_reply;
1616 u32 volstate;
1617 enum raid_state state = RAID_STATE_UNKNOWN;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301618 u16 handle = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301619
1620 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1621 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1622 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301623 if (raid_device)
1624 handle = raid_device->handle;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301625 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1626
1627 if (!raid_device)
1628 goto out;
1629
1630 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301631 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301632 sizeof(Mpi2RaidVolPage0_t))) {
1633 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1634 ioc->name, __FILE__, __LINE__, __func__);
1635 goto out;
1636 }
1637
1638 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1639 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1640 state = RAID_STATE_RESYNCING;
1641 goto out;
1642 }
1643
1644 switch (vol_pg0.VolumeState) {
1645 case MPI2_RAID_VOL_STATE_OPTIMAL:
1646 case MPI2_RAID_VOL_STATE_ONLINE:
1647 state = RAID_STATE_ACTIVE;
1648 break;
1649 case MPI2_RAID_VOL_STATE_DEGRADED:
1650 state = RAID_STATE_DEGRADED;
1651 break;
1652 case MPI2_RAID_VOL_STATE_FAILED:
1653 case MPI2_RAID_VOL_STATE_MISSING:
1654 state = RAID_STATE_OFFLINE;
1655 break;
1656 }
1657 out:
1658 raid_set_state(mpt2sas_raid_template, dev, state);
1659}
1660
1661/**
1662 * _scsih_set_level - set raid level
1663 * @sdev: scsi device struct
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301664 * @volume_type: volume type
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301665 */
1666static void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301667_scsih_set_level(struct scsi_device *sdev, u8 volume_type)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301668{
1669 enum raid_level level = RAID_LEVEL_UNKNOWN;
1670
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301671 switch (volume_type) {
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301672 case MPI2_RAID_VOL_TYPE_RAID0:
1673 level = RAID_LEVEL_0;
1674 break;
1675 case MPI2_RAID_VOL_TYPE_RAID10:
1676 level = RAID_LEVEL_10;
1677 break;
1678 case MPI2_RAID_VOL_TYPE_RAID1E:
1679 level = RAID_LEVEL_1E;
1680 break;
1681 case MPI2_RAID_VOL_TYPE_RAID1:
1682 level = RAID_LEVEL_1;
1683 break;
1684 }
1685
1686 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1687}
1688
1689/**
Eric Moore635374e2009-03-09 01:21:12 -06001690 * _scsih_get_volume_capabilities - volume capabilities
1691 * @ioc: per adapter object
1692 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301693 *
1694 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001695 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301696static int
Eric Moore635374e2009-03-09 01:21:12 -06001697_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1698 struct _raid_device *raid_device)
1699{
1700 Mpi2RaidVolPage0_t *vol_pg0;
1701 Mpi2RaidPhysDiskPage0_t pd_pg0;
1702 Mpi2SasDevicePage0_t sas_device_pg0;
1703 Mpi2ConfigReply_t mpi_reply;
1704 u16 sz;
1705 u8 num_pds;
1706
1707 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1708 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301709 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1710 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1711 __func__));
1712 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001713 }
1714
1715 raid_device->num_pds = num_pds;
1716 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1717 sizeof(Mpi2RaidVol0PhysDisk_t));
1718 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1719 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301720 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1721 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1722 __func__));
1723 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001724 }
1725
1726 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1727 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301728 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1729 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1730 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001731 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301732 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001733 }
1734
1735 raid_device->volume_type = vol_pg0->VolumeType;
1736
1737 /* figure out what the underlying devices are by
1738 * obtaining the device_info bits for the 1st device
1739 */
1740 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1741 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1742 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1743 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1744 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1745 le16_to_cpu(pd_pg0.DevHandle)))) {
1746 raid_device->device_info =
1747 le32_to_cpu(sas_device_pg0.DeviceInfo);
1748 }
1749 }
1750
1751 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301752 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001753}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301754/**
1755 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1756 * @ioc: per adapter object
1757 */
1758static void
1759_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1760{
1761 Mpi2RaidVolPage1_t vol_pg1;
1762 Mpi2ConfigReply_t mpi_reply;
1763 struct _raid_device *raid_device;
1764 u16 handle;
1765 u16 ioc_status;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301766 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301767
1768 handle = 0xFFFF;
1769 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1770 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1771 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1772 MPI2_IOCSTATUS_MASK;
1773 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1774 break;
1775 handle = le16_to_cpu(vol_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301776 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301777 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1778 if (raid_device)
1779 raid_device->direct_io_enabled = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301780 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301781 }
1782 return;
1783}
1784
1785
1786/**
1787 * _scsih_get_num_volumes - Get number of volumes in the ioc
1788 * @ioc: per adapter object
1789 */
1790static u8
1791_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1792{
1793 Mpi2RaidVolPage1_t vol_pg1;
1794 Mpi2ConfigReply_t mpi_reply;
1795 u16 handle;
1796 u8 vol_cnt = 0;
1797 u16 ioc_status;
1798
1799 handle = 0xFFFF;
1800 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1801 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1802 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1803 MPI2_IOCSTATUS_MASK;
1804 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1805 break;
1806 vol_cnt++;
1807 handle = le16_to_cpu(vol_pg1.DevHandle);
1808 }
1809 return vol_cnt;
1810}
1811
1812
1813/**
1814 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1815 * @ioc: per adapter object
1816 * @raid_device: the raid_device object
1817 */
1818static void
1819_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1820 struct _raid_device *raid_device)
1821{
1822 Mpi2RaidVolPage0_t *vol_pg0;
1823 Mpi2RaidPhysDiskPage0_t pd_pg0;
1824 Mpi2ConfigReply_t mpi_reply;
1825 u16 sz;
1826 u8 num_pds, count;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301827 unsigned long stripe_sz, block_sz;
1828 u8 stripe_exp, block_exp;
1829 u64 dev_max_lba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301830
1831 if (!ioc->is_warpdrive)
1832 return;
1833
1834 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1835 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1836 "globally as drives are exposed\n", ioc->name);
1837 return;
1838 }
1839 if (_scsih_get_num_volumes(ioc) > 1) {
1840 _scsih_disable_ddio(ioc);
1841 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1842 "globally as number of drives > 1\n", ioc->name);
1843 return;
1844 }
1845 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1846 &num_pds)) || !num_pds) {
1847 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1848 "Failure in computing number of drives\n", ioc->name);
1849 return;
1850 }
1851
1852 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1853 sizeof(Mpi2RaidVol0PhysDisk_t));
1854 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1855 if (!vol_pg0) {
1856 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1857 "Memory allocation failure for RVPG0\n", ioc->name);
1858 return;
1859 }
1860
1861 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1862 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1863 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1864 "Failure in retrieving RVPG0\n", ioc->name);
1865 kfree(vol_pg0);
1866 return;
1867 }
1868
1869 /*
1870 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1871 * assumed for WARPDRIVE, disable direct I/O
1872 */
1873 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1874 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1875 "for the drive with handle(0x%04x): num_mem=%d, "
1876 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1877 num_pds, MPT_MAX_WARPDRIVE_PDS);
1878 kfree(vol_pg0);
1879 return;
1880 }
1881 for (count = 0; count < num_pds; count++) {
1882 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1883 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1884 vol_pg0->PhysDisk[count].PhysDiskNum) ||
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05301885 le16_to_cpu(pd_pg0.DevHandle) ==
1886 MPT2SAS_INVALID_DEVICE_HANDLE) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301887 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1888 "disabled for the drive with handle(0x%04x) member"
1889 "handle retrieval failed for member number=%d\n",
1890 ioc->name, raid_device->handle,
1891 vol_pg0->PhysDisk[count].PhysDiskNum);
1892 goto out_error;
1893 }
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301894 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1895 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1896 if (dev_max_lba >> 32) {
1897 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1898 "disabled for the drive with handle(0x%04x) member"
1899 "handle (0x%04x) unsupported max lba 0x%016llx\n",
1900 ioc->name, raid_device->handle,
1901 le16_to_cpu(pd_pg0.DevHandle),
1902 (unsigned long long)dev_max_lba);
1903 goto out_error;
1904 }
1905
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301906 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1907 }
1908
1909 /*
1910 * Assumption for WD: Direct I/O is not supported if the volume is
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301911 * not RAID0
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301912 */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301913 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301914 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1915 "for the drive with handle(0x%04x): type=%d, "
1916 "s_sz=%uK, blk_size=%u\n", ioc->name,
1917 raid_device->handle, raid_device->volume_type,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301918 (le32_to_cpu(vol_pg0->StripeSize) *
1919 le16_to_cpu(vol_pg0->BlockSize)) / 1024,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301920 le16_to_cpu(vol_pg0->BlockSize));
1921 goto out_error;
1922 }
1923
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301924 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301925 stripe_exp = find_first_bit(&stripe_sz, 32);
1926 if (stripe_exp == 32) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301927 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301928 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301929 ioc->name, raid_device->handle,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301930 (le32_to_cpu(vol_pg0->StripeSize) *
1931 le16_to_cpu(vol_pg0->BlockSize)) / 1024);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301932 goto out_error;
1933 }
1934 raid_device->stripe_exponent = stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301935 block_sz = le16_to_cpu(vol_pg0->BlockSize);
1936 block_exp = find_first_bit(&block_sz, 16);
1937 if (block_exp == 16) {
1938 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1939 "for the drive with handle(0x%04x) invalid block sz %u\n",
1940 ioc->name, raid_device->handle,
1941 le16_to_cpu(vol_pg0->BlockSize));
1942 goto out_error;
1943 }
1944 raid_device->block_exponent = block_exp;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301945 raid_device->direct_io_enabled = 1;
1946
1947 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1948 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1949 /*
1950 * WARPDRIVE: Though the following fields are not used for direct IO,
1951 * stored for future purpose:
1952 */
1953 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1954 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1955 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1956
1957
1958 kfree(vol_pg0);
1959 return;
1960
1961out_error:
1962 raid_device->direct_io_enabled = 0;
1963 for (count = 0; count < num_pds; count++)
1964 raid_device->pd_handle[count] = 0;
1965 kfree(vol_pg0);
1966 return;
1967}
Eric Moore635374e2009-03-09 01:21:12 -06001968
1969/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301970 * _scsih_enable_tlr - setting TLR flags
1971 * @ioc: per adapter object
1972 * @sdev: scsi device struct
1973 *
1974 * Enabling Transaction Layer Retries for tape devices when
1975 * vpd page 0x90 is present
1976 *
1977 */
1978static void
1979_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1980{
1981 /* only for TAPE */
1982 if (sdev->type != TYPE_TAPE)
1983 return;
1984
1985 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1986 return;
1987
1988 sas_enable_tlr(sdev);
1989 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1990 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1991 return;
1992
1993}
1994
1995/**
Eric Moored5d135b2009-05-18 13:02:08 -06001996 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001997 * @sdev: scsi device struct
1998 *
1999 * Returns 0 if ok. Any other return is assumed to be an error and
2000 * the device is ignored.
2001 */
2002static int
Eric Moored5d135b2009-05-18 13:02:08 -06002003_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06002004{
2005 struct Scsi_Host *shost = sdev->host;
2006 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
2007 struct MPT2SAS_DEVICE *sas_device_priv_data;
2008 struct MPT2SAS_TARGET *sas_target_priv_data;
2009 struct _sas_device *sas_device;
2010 struct _raid_device *raid_device;
2011 unsigned long flags;
2012 int qdepth;
2013 u8 ssp_target = 0;
2014 char *ds = "";
2015 char *r_level = "";
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302016 u16 handle, volume_handle = 0;
2017 u64 volume_wwid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002018
2019 qdepth = 1;
2020 sas_device_priv_data = sdev->hostdata;
2021 sas_device_priv_data->configured_lun = 1;
2022 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
2023 sas_target_priv_data = sas_device_priv_data->sas_target;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302024 handle = sas_target_priv_data->handle;
Eric Moore635374e2009-03-09 01:21:12 -06002025
2026 /* raid volume handling */
2027 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
2028
2029 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302030 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06002031 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2032 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302033 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2034 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2035 __LINE__, __func__));
2036 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002037 }
2038
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302039 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2040 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2041 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2042 __LINE__, __func__));
2043 return 1;
2044 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302045 /*
2046 * WARPDRIVE: Initialize the required data for Direct IO
2047 */
2048 _scsih_init_warpdrive_properties(ioc, raid_device);
2049
Eric Moore635374e2009-03-09 01:21:12 -06002050 /* RAID Queue Depth Support
2051 * IS volume = underlying qdepth of drive type, either
2052 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2053 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2054 */
2055 if (raid_device->device_info &
2056 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2057 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2058 ds = "SSP";
2059 } else {
2060 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2061 if (raid_device->device_info &
2062 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2063 ds = "SATA";
2064 else
2065 ds = "STP";
2066 }
2067
2068 switch (raid_device->volume_type) {
2069 case MPI2_RAID_VOL_TYPE_RAID0:
2070 r_level = "RAID0";
2071 break;
2072 case MPI2_RAID_VOL_TYPE_RAID1E:
2073 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302074 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302075 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302076 MFG10_GF0_R10_DISPLAY) &&
2077 !(raid_device->num_pds % 2))
2078 r_level = "RAID10";
2079 else
2080 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002081 break;
2082 case MPI2_RAID_VOL_TYPE_RAID1:
2083 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2084 r_level = "RAID1";
2085 break;
2086 case MPI2_RAID_VOL_TYPE_RAID10:
2087 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2088 r_level = "RAID10";
2089 break;
2090 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2091 default:
2092 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2093 r_level = "RAIDX";
2094 break;
2095 }
2096
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302097 if (!ioc->hide_ir_msg)
2098 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2099 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2100 r_level, raid_device->handle,
2101 (unsigned long long)raid_device->wwid,
2102 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002103 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302104 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302105 if (!ioc->is_warpdrive)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302106 _scsih_set_level(sdev, raid_device->volume_type);
Eric Moore635374e2009-03-09 01:21:12 -06002107 return 0;
2108 }
2109
2110 /* non-raid handling */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302111 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2112 if (mpt2sas_config_get_volume_handle(ioc, handle,
2113 &volume_handle)) {
2114 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2115 "failure at %s:%d/%s()!\n", ioc->name,
2116 __FILE__, __LINE__, __func__));
2117 return 1;
2118 }
2119 if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
2120 volume_handle, &volume_wwid)) {
2121 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2122 "failure at %s:%d/%s()!\n", ioc->name,
2123 __FILE__, __LINE__, __func__));
2124 return 1;
2125 }
2126 }
2127
Eric Moore635374e2009-03-09 01:21:12 -06002128 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2129 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2130 sas_device_priv_data->sas_target->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302131 if (!sas_device) {
2132 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302133 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302134 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2135 __LINE__, __func__));
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302136 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002137 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302138 sas_device->volume_handle = volume_handle;
2139 sas_device->volume_wwid = volume_wwid;
2140 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2141 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2142 ssp_target = 1;
2143 ds = "SSP";
2144 } else {
2145 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2146 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2147 ds = "STP";
2148 else if (sas_device->device_info &
2149 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2150 ds = "SATA";
2151 }
2152 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2153 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2154 ds, sas_device->handle,
2155 (unsigned long long)sas_device->sas_address,
2156 sas_device->phy,
2157 (unsigned long long)sas_device->device_name);
2158 sdev_printk(KERN_INFO, sdev, "%s: "
2159 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2160 (unsigned long long) sas_device->enclosure_logical_id,
2161 sas_device->slot);
2162
2163 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2164 if (!ssp_target)
2165 _scsih_display_sata_capabilities(ioc, handle, sdev);
2166
Eric Moore635374e2009-03-09 01:21:12 -06002167
Mike Christiee881a172009-10-15 17:46:39 -07002168 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002169
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302170 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002171 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302172 _scsih_enable_tlr(ioc, sdev);
2173 }
Eric Moore635374e2009-03-09 01:21:12 -06002174 return 0;
2175}
2176
2177/**
Eric Moored5d135b2009-05-18 13:02:08 -06002178 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002179 * @sdev: scsi device struct
2180 * @bdev: pointer to block device context
2181 * @capacity: device size (in 512 byte sectors)
2182 * @params: three element array to place output:
2183 * params[0] number of heads (max 255)
2184 * params[1] number of sectors (max 63)
2185 * params[2] number of cylinders
2186 *
2187 * Return nothing.
2188 */
2189static int
Eric Moored5d135b2009-05-18 13:02:08 -06002190_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002191 sector_t capacity, int params[])
2192{
2193 int heads;
2194 int sectors;
2195 sector_t cylinders;
2196 ulong dummy;
2197
2198 heads = 64;
2199 sectors = 32;
2200
2201 dummy = heads * sectors;
2202 cylinders = capacity;
2203 sector_div(cylinders, dummy);
2204
2205 /*
2206 * Handle extended translation size for logical drives
2207 * > 1Gb
2208 */
2209 if ((ulong)capacity >= 0x200000) {
2210 heads = 255;
2211 sectors = 63;
2212 dummy = heads * sectors;
2213 cylinders = capacity;
2214 sector_div(cylinders, dummy);
2215 }
2216
2217 /* return result */
2218 params[0] = heads;
2219 params[1] = sectors;
2220 params[2] = cylinders;
2221
2222 return 0;
2223}
2224
2225/**
2226 * _scsih_response_code - translation of device response code
2227 * @ioc: per adapter object
2228 * @response_code: response code returned by the device
2229 *
2230 * Return nothing.
2231 */
2232static void
2233_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2234{
2235 char *desc;
2236
2237 switch (response_code) {
2238 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2239 desc = "task management request completed";
2240 break;
2241 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2242 desc = "invalid frame";
2243 break;
2244 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2245 desc = "task management request not supported";
2246 break;
2247 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2248 desc = "task management request failed";
2249 break;
2250 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2251 desc = "task management request succeeded";
2252 break;
2253 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2254 desc = "invalid lun";
2255 break;
2256 case 0xA:
2257 desc = "overlapped tag attempted";
2258 break;
2259 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2260 desc = "task queued, however not sent to target";
2261 break;
2262 default:
2263 desc = "unknown";
2264 break;
2265 }
2266 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2267 ioc->name, response_code, desc);
2268}
2269
2270/**
Eric Moored5d135b2009-05-18 13:02:08 -06002271 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002272 * @ioc: per adapter object
2273 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302274 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002275 * @reply: reply message frame(lower 32bit addr)
2276 * Context: none.
2277 *
2278 * The callback handler when using scsih_issue_tm.
2279 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302280 * Return 1 meaning mf should be freed from _base_interrupt
2281 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002282 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302283static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302284_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002285{
2286 MPI2DefaultReply_t *mpi_reply;
2287
2288 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302289 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002290 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302291 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302292 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002293 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2294 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2295 if (mpi_reply) {
2296 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2297 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2298 }
2299 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2300 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302301 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002302}
2303
2304/**
2305 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2306 * @ioc: per adapter object
2307 * @handle: device handle
2308 *
2309 * During taskmangement request, we need to freeze the device queue.
2310 */
2311void
2312mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2313{
2314 struct MPT2SAS_DEVICE *sas_device_priv_data;
2315 struct scsi_device *sdev;
2316 u8 skip = 0;
2317
2318 shost_for_each_device(sdev, ioc->shost) {
2319 if (skip)
2320 continue;
2321 sas_device_priv_data = sdev->hostdata;
2322 if (!sas_device_priv_data)
2323 continue;
2324 if (sas_device_priv_data->sas_target->handle == handle) {
2325 sas_device_priv_data->sas_target->tm_busy = 1;
2326 skip = 1;
2327 ioc->ignore_loginfos = 1;
2328 }
2329 }
2330}
2331
2332/**
2333 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2334 * @ioc: per adapter object
2335 * @handle: device handle
2336 *
2337 * During taskmangement request, we need to freeze the device queue.
2338 */
2339void
2340mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2341{
2342 struct MPT2SAS_DEVICE *sas_device_priv_data;
2343 struct scsi_device *sdev;
2344 u8 skip = 0;
2345
2346 shost_for_each_device(sdev, ioc->shost) {
2347 if (skip)
2348 continue;
2349 sas_device_priv_data = sdev->hostdata;
2350 if (!sas_device_priv_data)
2351 continue;
2352 if (sas_device_priv_data->sas_target->handle == handle) {
2353 sas_device_priv_data->sas_target->tm_busy = 0;
2354 skip = 1;
2355 ioc->ignore_loginfos = 0;
2356 }
2357 }
2358}
2359
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302360
Eric Moore635374e2009-03-09 01:21:12 -06002361/**
2362 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2363 * @ioc: per adapter struct
2364 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302365 * @channel: the channel assigned by the OS
2366 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002367 * @lun: lun number
2368 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2369 * @smid_task: smid assigned to the task
2370 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302371 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302372 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002373 *
2374 * A generic API for sending task management requests to firmware.
2375 *
Eric Moore635374e2009-03-09 01:21:12 -06002376 * The callback index is set inside `ioc->tm_cb_idx`.
2377 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302378 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002379 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302380int
2381mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2382 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002383 enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002384{
2385 Mpi2SCSITaskManagementRequest_t *mpi_request;
2386 Mpi2SCSITaskManagementReply_t *mpi_reply;
2387 u16 smid = 0;
2388 u32 ioc_state;
2389 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302390 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302391 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002392
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302393 if (m_type == TM_MUTEX_ON)
2394 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302395 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2396 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2397 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302398 rc = FAILED;
2399 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302400 }
2401
Eric Moore3cb54692010-07-08 14:44:34 -06002402 if (ioc->shost_recovery || ioc->remove_host ||
2403 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002404 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2405 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302406 rc = FAILED;
2407 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002408 }
Eric Moore635374e2009-03-09 01:21:12 -06002409
2410 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2411 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302412 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002413 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302414 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302415 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302416 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302417 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002418 }
2419
2420 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2421 mpt2sas_base_fault_info(ioc, ioc_state &
2422 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302423 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302424 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302425 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302426 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002427 }
2428
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302429 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002430 if (!smid) {
2431 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2432 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302433 rc = FAILED;
2434 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002435 }
2436
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302437 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2438 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2439
Eric Moore635374e2009-03-09 01:21:12 -06002440 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302441 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2442 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002443 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2444 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2445 ioc->tm_cmds.smid = smid;
2446 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302447 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002448 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2449 mpi_request->DevHandle = cpu_to_le16(handle);
2450 mpi_request->TaskType = type;
2451 mpi_request->TaskMID = cpu_to_le16(smid_task);
2452 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2453 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302454 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302455 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002456 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002457 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2458 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2459 ioc->name, __func__);
2460 _debug_dump_mf(mpi_request,
2461 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302462 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302463 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302464 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302465 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302466 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2467 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2468 goto err_out;
2469 }
Eric Moore635374e2009-03-09 01:21:12 -06002470 }
2471
2472 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2473 mpi_reply = ioc->tm_cmds.reply;
2474 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2475 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2476 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2477 le32_to_cpu(mpi_reply->IOCLogInfo),
2478 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302479 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002480 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302481 if (mpi_reply->IOCStatus)
2482 _debug_dump_mf(mpi_request,
2483 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2484 }
Eric Moore635374e2009-03-09 01:21:12 -06002485 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302486
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302487 switch (type) {
2488 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302489 rc = SUCCESS;
2490 if (scsi_lookup->scmd == NULL)
2491 break;
2492 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302493 break;
2494
2495 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2496 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2497 rc = FAILED;
2498 else
2499 rc = SUCCESS;
2500 break;
2501
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302502 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302503 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2504 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2505 rc = FAILED;
2506 else
2507 rc = SUCCESS;
2508 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302509 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2510 rc = SUCCESS;
2511 break;
2512 default:
2513 rc = FAILED;
2514 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302515 }
2516
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302517 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2518 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302519 if (m_type == TM_MUTEX_ON)
2520 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302521
2522 return rc;
2523
2524 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302525 if (m_type == TM_MUTEX_ON)
2526 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302527 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002528}
2529
2530/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302531 * _scsih_tm_display_info - displays info about the device
2532 * @ioc: per adapter struct
2533 * @scmd: pointer to scsi command object
2534 *
2535 * Called by task management callback handlers.
2536 */
2537static void
2538_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2539{
2540 struct scsi_target *starget = scmd->device->sdev_target;
2541 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2542 struct _sas_device *sas_device = NULL;
2543 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302544 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302545
2546 if (!priv_target)
2547 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302548 if (ioc->hide_ir_msg)
2549 device_str = "WarpDrive";
2550 else
2551 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302552
2553 scsi_print_command(scmd);
2554 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302555 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2556 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2557 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302558 } else {
2559 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2560 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2561 priv_target->sas_address);
2562 if (sas_device) {
2563 if (priv_target->flags &
2564 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2565 starget_printk(KERN_INFO, starget,
2566 "volume handle(0x%04x), "
2567 "volume wwid(0x%016llx)\n",
2568 sas_device->volume_handle,
2569 (unsigned long long)sas_device->volume_wwid);
2570 }
2571 starget_printk(KERN_INFO, starget,
2572 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2573 sas_device->handle,
2574 (unsigned long long)sas_device->sas_address,
2575 sas_device->phy);
2576 starget_printk(KERN_INFO, starget,
2577 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2578 (unsigned long long)sas_device->enclosure_logical_id,
2579 sas_device->slot);
2580 }
2581 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2582 }
2583}
2584
2585/**
Eric Moored5d135b2009-05-18 13:02:08 -06002586 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302587 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002588 *
2589 * Returns SUCCESS if command aborted else FAILED
2590 */
2591static int
Eric Moored5d135b2009-05-18 13:02:08 -06002592_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002593{
2594 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2595 struct MPT2SAS_DEVICE *sas_device_priv_data;
2596 u16 smid;
2597 u16 handle;
2598 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002599
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302600 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2601 "scmd(%p)\n", scmd);
2602 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002603
2604 sas_device_priv_data = scmd->device->hostdata;
2605 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302606 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2607 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002608 scmd->result = DID_NO_CONNECT << 16;
2609 scmd->scsi_done(scmd);
2610 r = SUCCESS;
2611 goto out;
2612 }
2613
2614 /* search for the command */
2615 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2616 if (!smid) {
2617 scmd->result = DID_RESET << 16;
2618 r = SUCCESS;
2619 goto out;
2620 }
2621
2622 /* for hidden raid components and volumes this is not supported */
2623 if (sas_device_priv_data->sas_target->flags &
2624 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2625 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2626 scmd->result = DID_RESET << 16;
2627 r = FAILED;
2628 goto out;
2629 }
2630
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302631 mpt2sas_halt_firmware(ioc);
2632
Eric Moore635374e2009-03-09 01:21:12 -06002633 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302634 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2635 scmd->device->id, scmd->device->lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002636 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002637
2638 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302639 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2640 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002641 return r;
2642}
2643
Eric Moore635374e2009-03-09 01:21:12 -06002644/**
Eric Moored5d135b2009-05-18 13:02:08 -06002645 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302646 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002647 *
2648 * Returns SUCCESS if command aborted else FAILED
2649 */
2650static int
Eric Moored5d135b2009-05-18 13:02:08 -06002651_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002652{
2653 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2654 struct MPT2SAS_DEVICE *sas_device_priv_data;
2655 struct _sas_device *sas_device;
2656 unsigned long flags;
2657 u16 handle;
2658 int r;
2659
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302660 struct scsi_target *starget = scmd->device->sdev_target;
2661
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302662 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302663 "scmd(%p)\n", scmd);
2664 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002665
2666 sas_device_priv_data = scmd->device->hostdata;
2667 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302668 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302669 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002670 scmd->result = DID_NO_CONNECT << 16;
2671 scmd->scsi_done(scmd);
2672 r = SUCCESS;
2673 goto out;
2674 }
2675
2676 /* for hidden raid components obtain the volume_handle */
2677 handle = 0;
2678 if (sas_device_priv_data->sas_target->flags &
2679 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2680 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2681 sas_device = _scsih_sas_device_find_by_handle(ioc,
2682 sas_device_priv_data->sas_target->handle);
2683 if (sas_device)
2684 handle = sas_device->volume_handle;
2685 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2686 } else
2687 handle = sas_device_priv_data->sas_target->handle;
2688
2689 if (!handle) {
2690 scmd->result = DID_RESET << 16;
2691 r = FAILED;
2692 goto out;
2693 }
2694
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302695 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2696 scmd->device->id, scmd->device->lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002697 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002698
2699 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302700 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2701 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002702 return r;
2703}
2704
2705/**
Eric Moored5d135b2009-05-18 13:02:08 -06002706 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302707 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002708 *
2709 * Returns SUCCESS if command aborted else FAILED
2710 */
2711static int
Eric Moored5d135b2009-05-18 13:02:08 -06002712_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002713{
2714 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2715 struct MPT2SAS_DEVICE *sas_device_priv_data;
2716 struct _sas_device *sas_device;
2717 unsigned long flags;
2718 u16 handle;
2719 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302720 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002721
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302722 starget_printk(KERN_INFO, starget, "attempting target reset! "
2723 "scmd(%p)\n", scmd);
2724 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002725
2726 sas_device_priv_data = scmd->device->hostdata;
2727 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302728 starget_printk(KERN_INFO, starget, "target been deleted! "
2729 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002730 scmd->result = DID_NO_CONNECT << 16;
2731 scmd->scsi_done(scmd);
2732 r = SUCCESS;
2733 goto out;
2734 }
2735
2736 /* for hidden raid components obtain the volume_handle */
2737 handle = 0;
2738 if (sas_device_priv_data->sas_target->flags &
2739 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2740 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2741 sas_device = _scsih_sas_device_find_by_handle(ioc,
2742 sas_device_priv_data->sas_target->handle);
2743 if (sas_device)
2744 handle = sas_device->volume_handle;
2745 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2746 } else
2747 handle = sas_device_priv_data->sas_target->handle;
2748
2749 if (!handle) {
2750 scmd->result = DID_RESET << 16;
2751 r = FAILED;
2752 goto out;
2753 }
2754
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302755 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2756 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04002757 30, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002758
2759 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302760 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2761 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002762 return r;
2763}
2764
2765/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302766 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302767 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002768 *
2769 * Returns SUCCESS if command aborted else FAILED
2770 */
2771static int
Eric Moored5d135b2009-05-18 13:02:08 -06002772_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002773{
2774 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2775 int r, retval;
2776
2777 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2778 ioc->name, scmd);
2779 scsi_print_command(scmd);
2780
2781 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2782 FORCE_BIG_HAMMER);
2783 r = (retval < 0) ? FAILED : SUCCESS;
2784 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2785 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2786
2787 return r;
2788}
2789
2790/**
2791 * _scsih_fw_event_add - insert and queue up fw_event
2792 * @ioc: per adapter object
2793 * @fw_event: object describing the event
2794 * Context: This function will acquire ioc->fw_event_lock.
2795 *
2796 * This adds the firmware event object into link list, then queues it up to
2797 * be processed from user context.
2798 *
2799 * Return nothing.
2800 */
2801static void
2802_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2803{
2804 unsigned long flags;
2805
2806 if (ioc->firmware_event_thread == NULL)
2807 return;
2808
2809 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2810 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302811 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2812 queue_delayed_work(ioc->firmware_event_thread,
2813 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002814 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2815}
2816
2817/**
2818 * _scsih_fw_event_free - delete fw_event
2819 * @ioc: per adapter object
2820 * @fw_event: object describing the event
2821 * Context: This function will acquire ioc->fw_event_lock.
2822 *
2823 * This removes firmware event object from link list, frees associated memory.
2824 *
2825 * Return nothing.
2826 */
2827static void
2828_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2829 *fw_event)
2830{
2831 unsigned long flags;
2832
2833 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2834 list_del(&fw_event->list);
2835 kfree(fw_event->event_data);
2836 kfree(fw_event);
2837 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2838}
2839
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302840
Eric Moore635374e2009-03-09 01:21:12 -06002841/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302842 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002843 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002844 *
2845 * Return nothing.
2846 */
2847static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302848_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002849{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302850 struct fw_event_work *fw_event;
2851
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302852 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302853 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002854
2855 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2856 if (!fw_event)
2857 return;
2858
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302859 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2860 fw_event->ioc = ioc;
2861 _scsih_fw_event_add(ioc, fw_event);
2862}
2863
2864/**
2865 * mpt2sas_port_enable_complete - port enable completed (fake event)
2866 * @ioc: per adapter object
2867 *
2868 * Return nothing.
2869 */
2870void
2871mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2872{
2873 struct fw_event_work *fw_event;
2874
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302875 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2876 if (!fw_event)
2877 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302878 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302879 fw_event->ioc = ioc;
2880 _scsih_fw_event_add(ioc, fw_event);
2881}
2882
2883/**
2884 * _scsih_fw_event_cleanup_queue - cleanup event queue
2885 * @ioc: per adapter object
2886 *
2887 * Walk the firmware event queue, either killing timers, or waiting
2888 * for outstanding events to complete
2889 *
2890 * Return nothing.
2891 */
2892static void
2893_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2894{
2895 struct fw_event_work *fw_event, *next;
2896
2897 if (list_empty(&ioc->fw_event_list) ||
2898 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002899 return;
2900
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302901 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2902 if (cancel_delayed_work(&fw_event->delayed_work)) {
2903 _scsih_fw_event_free(ioc, fw_event);
2904 continue;
2905 }
2906 fw_event->cancel_pending_work = 1;
2907 }
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;
3521 local_event_data = fw_event->event_data;
3522 if (local_event_data->ExpStatus ==
3523 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3524 local_event_data->ExpStatus ==
3525 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3526 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3527 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303528 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003529 "setting ignoring flag\n", ioc->name));
3530 fw_event->ignore = 1;
3531 }
3532 }
3533 }
3534 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3535}
3536
3537/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303538 * _scsih_set_volume_delete_flag - setting volume delete flag
3539 * @ioc: per adapter object
3540 * @handle: device handle
3541 *
3542 * This
3543 * Return nothing.
3544 */
3545static void
3546_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3547{
3548 struct _raid_device *raid_device;
3549 struct MPT2SAS_TARGET *sas_target_priv_data;
3550 unsigned long flags;
3551
3552 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3553 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3554 if (raid_device && raid_device->starget &&
3555 raid_device->starget->hostdata) {
3556 sas_target_priv_data =
3557 raid_device->starget->hostdata;
3558 sas_target_priv_data->deleted = 1;
3559 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3560 "setting delete flag: handle(0x%04x), "
3561 "wwid(0x%016llx)\n", ioc->name, handle,
3562 (unsigned long long) raid_device->wwid));
3563 }
3564 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3565}
3566
3567/**
3568 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3569 * @handle: input handle
3570 * @a: handle for volume a
3571 * @b: handle for volume b
3572 *
3573 * IR firmware only supports two raid volumes. The purpose of this
3574 * routine is to set the volume handle in either a or b. When the given
3575 * input handle is non-zero, or when a and b have not been set before.
3576 */
3577static void
3578_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3579{
3580 if (!handle || handle == *a || handle == *b)
3581 return;
3582 if (!*a)
3583 *a = handle;
3584 else if (!*b)
3585 *b = handle;
3586}
3587
3588/**
3589 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3590 * @ioc: per adapter object
3591 * @event_data: the event data payload
3592 * Context: interrupt time.
3593 *
3594 * This routine will send target reset to volume, followed by target
3595 * resets to the PDs. This is called when a PD has been removed, or
3596 * volume has been deleted or removed. When the target reset is sent
3597 * to volume, the PD target resets need to be queued to start upon
3598 * completion of the volume target reset.
3599 *
3600 * Return nothing.
3601 */
3602static void
3603_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3604 Mpi2EventDataIrConfigChangeList_t *event_data)
3605{
3606 Mpi2EventIrConfigElement_t *element;
3607 int i;
3608 u16 handle, volume_handle, a, b;
3609 struct _tr_list *delayed_tr;
3610
3611 a = 0;
3612 b = 0;
3613
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303614 if (ioc->is_warpdrive)
3615 return;
3616
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303617 /* Volume Resets for Deleted or Removed */
3618 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3619 for (i = 0; i < event_data->NumElements; i++, element++) {
3620 if (element->ReasonCode ==
3621 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3622 element->ReasonCode ==
3623 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3624 volume_handle = le16_to_cpu(element->VolDevHandle);
3625 _scsih_set_volume_delete_flag(ioc, volume_handle);
3626 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3627 }
3628 }
3629
3630 /* Volume Resets for UNHIDE events */
3631 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3632 for (i = 0; i < event_data->NumElements; i++, element++) {
3633 if (le32_to_cpu(event_data->Flags) &
3634 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3635 continue;
3636 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3637 volume_handle = le16_to_cpu(element->VolDevHandle);
3638 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3639 }
3640 }
3641
3642 if (a)
3643 _scsih_tm_tr_volume_send(ioc, a);
3644 if (b)
3645 _scsih_tm_tr_volume_send(ioc, b);
3646
3647 /* PD target resets */
3648 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3649 for (i = 0; i < event_data->NumElements; i++, element++) {
3650 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3651 continue;
3652 handle = le16_to_cpu(element->PhysDiskDevHandle);
3653 volume_handle = le16_to_cpu(element->VolDevHandle);
3654 clear_bit(handle, ioc->pd_handles);
3655 if (!volume_handle)
3656 _scsih_tm_tr_send(ioc, handle);
3657 else if (volume_handle == a || volume_handle == b) {
3658 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3659 BUG_ON(!delayed_tr);
3660 INIT_LIST_HEAD(&delayed_tr->list);
3661 delayed_tr->handle = handle;
3662 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3663 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3664 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3665 handle));
3666 } else
3667 _scsih_tm_tr_send(ioc, handle);
3668 }
3669}
3670
3671
3672/**
3673 * _scsih_check_volume_delete_events - set delete flag for volumes
3674 * @ioc: per adapter object
3675 * @event_data: the event data payload
3676 * Context: interrupt time.
3677 *
3678 * This will handle the case when the cable connected to entire volume is
3679 * pulled. We will take care of setting the deleted flag so normal IO will
3680 * not be sent.
3681 *
3682 * Return nothing.
3683 */
3684static void
3685_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3686 Mpi2EventDataIrVolume_t *event_data)
3687{
3688 u32 state;
3689
3690 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3691 return;
3692 state = le32_to_cpu(event_data->NewValue);
3693 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3694 MPI2_RAID_VOL_STATE_FAILED)
3695 _scsih_set_volume_delete_flag(ioc,
3696 le16_to_cpu(event_data->VolDevHandle));
3697}
3698
3699/**
Eric Moore635374e2009-03-09 01:21:12 -06003700 * _scsih_flush_running_cmds - completing outstanding commands.
3701 * @ioc: per adapter object
3702 *
3703 * The flushing out of all pending scmd commands following host reset,
3704 * where all IO is dropped to the floor.
3705 *
3706 * Return nothing.
3707 */
3708static void
3709_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3710{
3711 struct scsi_cmnd *scmd;
3712 u16 smid;
3713 u16 count = 0;
3714
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303715 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303716 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003717 if (!scmd)
3718 continue;
3719 count++;
3720 mpt2sas_base_free_smid(ioc, smid);
3721 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003722 if (ioc->pci_error_recovery)
3723 scmd->result = DID_NO_CONNECT << 16;
3724 else
3725 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003726 scmd->scsi_done(scmd);
3727 }
3728 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3729 ioc->name, count));
3730}
3731
3732/**
Eric Moore3c621b32009-05-18 12:59:41 -06003733 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3734 * @scmd: pointer to scsi command object
3735 * @mpi_request: pointer to the SCSI_IO reqest message frame
3736 *
3737 * Supporting protection 1 and 3.
3738 *
3739 * Returns nothing
3740 */
3741static void
3742_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3743{
3744 u16 eedp_flags;
3745 unsigned char prot_op = scsi_get_prot_op(scmd);
3746 unsigned char prot_type = scsi_get_prot_type(scmd);
3747
Eric Moored334aa72010-04-22 10:47:40 -06003748 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003749 return;
3750
3751 if (prot_op == SCSI_PROT_READ_STRIP)
3752 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3753 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3754 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3755 else
3756 return;
3757
Eric Moore3c621b32009-05-18 12:59:41 -06003758 switch (prot_type) {
3759 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003760 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003761
3762 /*
3763 * enable ref/guard checking
3764 * auto increment ref tag
3765 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303766 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003767 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3768 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3769 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3770 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003771 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003772
Eric Moore3c621b32009-05-18 12:59:41 -06003773 case SCSI_PROT_DIF_TYPE3:
3774
3775 /*
3776 * enable guard checking
3777 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303778 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003779 break;
3780 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303781 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3782 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003783}
3784
3785/**
3786 * _scsih_eedp_error_handling - return sense code for EEDP errors
3787 * @scmd: pointer to scsi command object
3788 * @ioc_status: ioc status
3789 *
3790 * Returns nothing
3791 */
3792static void
3793_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3794{
3795 u8 ascq;
Eric Moore3c621b32009-05-18 12:59:41 -06003796
3797 switch (ioc_status) {
3798 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3799 ascq = 0x01;
3800 break;
3801 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3802 ascq = 0x02;
3803 break;
3804 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3805 ascq = 0x03;
3806 break;
3807 default:
3808 ascq = 0x00;
3809 break;
3810 }
3811
Martin K. Petersen9b5cd132012-08-28 14:29:36 -04003812 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10, ascq);
3813 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
Eric Moore3c621b32009-05-18 12:59:41 -06003814 SAM_STAT_CHECK_CONDITION;
3815}
3816
3817/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303818 * _scsih_scsi_direct_io_get - returns direct io flag
3819 * @ioc: per adapter object
3820 * @smid: system request message index
3821 *
3822 * Returns the smid stored scmd pointer.
3823 */
3824static inline u8
3825_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3826{
3827 return ioc->scsi_lookup[smid - 1].direct_io;
3828}
3829
3830/**
3831 * _scsih_scsi_direct_io_set - sets direct io flag
3832 * @ioc: per adapter object
3833 * @smid: system request message index
3834 * @direct_io: Zero or non-zero value to set in the direct_io flag
3835 *
3836 * Returns Nothing.
3837 */
3838static inline void
3839_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3840{
3841 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3842}
3843
3844
3845/**
3846 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3847 * @ioc: per adapter object
3848 * @scmd: pointer to scsi command object
3849 * @raid_device: pointer to raid device data structure
3850 * @mpi_request: pointer to the SCSI_IO reqest message frame
3851 * @smid: system request message index
3852 *
3853 * Returns nothing
3854 */
3855static void
3856_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3857 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3858 u16 smid)
3859{
3860 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3861 u32 stripe_sz, stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303862 u8 num_pds, *cdb_ptr, i;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303863 u8 cdb0 = scmd->cmnd[0];
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303864 u64 v_llba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303865
3866 /*
3867 * Try Direct I/O to RAID memeber disks
3868 */
3869 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3870 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3871 cdb_ptr = mpi_request->CDB.CDB32;
3872
3873 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3874 | cdb_ptr[5])) {
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303875 io_size = scsi_bufflen(scmd) >>
3876 raid_device->block_exponent;
3877 i = (cdb0 < READ_16) ? 2 : 6;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303878 /* get virtual lba */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303879 v_lba = be32_to_cpu(*(__be32 *)(&cdb_ptr[i]));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303880
3881 if (((u64)v_lba + (u64)io_size - 1) <=
3882 (u32)raid_device->max_lba) {
3883 stripe_sz = raid_device->stripe_sz;
3884 stripe_exp = raid_device->stripe_exponent;
3885 stripe_off = v_lba & (stripe_sz - 1);
3886
3887 /* Check whether IO falls within a stripe */
3888 if ((stripe_off + io_size) <= stripe_sz) {
3889 num_pds = raid_device->num_pds;
3890 p_lba = v_lba >> stripe_exp;
3891 stripe_unit = p_lba / num_pds;
3892 column = p_lba % num_pds;
3893 p_lba = (stripe_unit << stripe_exp) +
3894 stripe_off;
3895 mpi_request->DevHandle =
3896 cpu_to_le16(raid_device->
3897 pd_handle[column]);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303898 (*(__be32 *)(&cdb_ptr[i])) =
3899 cpu_to_be32(p_lba);
3900 /*
3901 * WD: To indicate this I/O is directI/O
3902 */
3903 _scsih_scsi_direct_io_set(ioc, smid, 1);
3904 }
3905 }
3906 } else {
3907 io_size = scsi_bufflen(scmd) >>
3908 raid_device->block_exponent;
3909 /* get virtual lba */
3910 v_llba = be64_to_cpu(*(__be64 *)(&cdb_ptr[2]));
3911
3912 if ((v_llba + (u64)io_size - 1) <=
3913 raid_device->max_lba) {
3914 stripe_sz = raid_device->stripe_sz;
3915 stripe_exp = raid_device->stripe_exponent;
3916 stripe_off = (u32) (v_llba & (stripe_sz - 1));
3917
3918 /* Check whether IO falls within a stripe */
3919 if ((stripe_off + io_size) <= stripe_sz) {
3920 num_pds = raid_device->num_pds;
3921 p_lba = (u32)(v_llba >> stripe_exp);
3922 stripe_unit = p_lba / num_pds;
3923 column = p_lba % num_pds;
3924 p_lba = (stripe_unit << stripe_exp) +
3925 stripe_off;
3926 mpi_request->DevHandle =
3927 cpu_to_le16(raid_device->
3928 pd_handle[column]);
3929 (*(__be64 *)(&cdb_ptr[2])) =
3930 cpu_to_be64((u64)p_lba);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303931 /*
3932 * WD: To indicate this I/O is directI/O
3933 */
3934 _scsih_scsi_direct_io_set(ioc, smid, 1);
3935 }
3936 }
3937 }
3938 }
3939}
3940
3941/**
Eric Moored5d135b2009-05-18 13:02:08 -06003942 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003943 * @scmd: pointer to scsi command object
3944 * @done: function pointer to be invoked on completion
3945 *
3946 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3947 *
3948 * Returns 0 on success. If there's a failure, return either:
3949 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3950 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3951 */
3952static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003953_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003954{
3955 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3956 struct MPT2SAS_DEVICE *sas_device_priv_data;
3957 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303958 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003959 Mpi2SCSIIORequest_t *mpi_request;
3960 u32 mpi_control;
3961 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003962
3963 scmd->scsi_done = done;
3964 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
4039 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4040 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
Jeff Garzikf2812332010-11-16 02:10:29 -05004083static DEF_SCSI_QCMD(_scsih_qcmd)
4084
Eric Moore635374e2009-03-09 01:21:12 -06004085/**
4086 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4087 * @sense_buffer: sense data returned by target
4088 * @data: normalized skey/asc/ascq
4089 *
4090 * Return nothing.
4091 */
4092static void
4093_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4094{
4095 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4096 /* descriptor format */
4097 data->skey = sense_buffer[1] & 0x0F;
4098 data->asc = sense_buffer[2];
4099 data->ascq = sense_buffer[3];
4100 } else {
4101 /* fixed format */
4102 data->skey = sense_buffer[2] & 0x0F;
4103 data->asc = sense_buffer[12];
4104 data->ascq = sense_buffer[13];
4105 }
4106}
4107
4108#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4109/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004110 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004111 * @ioc: per adapter object
4112 * @scmd: pointer to scsi command object
4113 * @mpi_reply: reply mf payload returned from firmware
4114 *
4115 * scsi_status - SCSI Status code returned from target device
4116 * scsi_state - state info associated with SCSI_IO determined by ioc
4117 * ioc_status - ioc supplied status info
4118 *
4119 * Return nothing.
4120 */
4121static void
4122_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4123 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4124{
4125 u32 response_info;
4126 u8 *response_bytes;
4127 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4128 MPI2_IOCSTATUS_MASK;
4129 u8 scsi_state = mpi_reply->SCSIState;
4130 u8 scsi_status = mpi_reply->SCSIStatus;
4131 char *desc_ioc_state = NULL;
4132 char *desc_scsi_status = NULL;
4133 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304134 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304135 struct _sas_device *sas_device = NULL;
4136 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304137 struct scsi_target *starget = scmd->device->sdev_target;
4138 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304139 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304140
4141 if (!priv_target)
4142 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304143
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304144 if (ioc->hide_ir_msg)
4145 device_str = "WarpDrive";
4146 else
4147 device_str = "volume";
4148
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304149 if (log_info == 0x31170000)
4150 return;
Eric Moore635374e2009-03-09 01:21:12 -06004151
4152 switch (ioc_status) {
4153 case MPI2_IOCSTATUS_SUCCESS:
4154 desc_ioc_state = "success";
4155 break;
4156 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4157 desc_ioc_state = "invalid function";
4158 break;
4159 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4160 desc_ioc_state = "scsi recovered error";
4161 break;
4162 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4163 desc_ioc_state = "scsi invalid dev handle";
4164 break;
4165 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4166 desc_ioc_state = "scsi device not there";
4167 break;
4168 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4169 desc_ioc_state = "scsi data overrun";
4170 break;
4171 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4172 desc_ioc_state = "scsi data underrun";
4173 break;
4174 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4175 desc_ioc_state = "scsi io data error";
4176 break;
4177 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4178 desc_ioc_state = "scsi protocol error";
4179 break;
4180 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4181 desc_ioc_state = "scsi task terminated";
4182 break;
4183 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4184 desc_ioc_state = "scsi residual mismatch";
4185 break;
4186 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4187 desc_ioc_state = "scsi task mgmt failed";
4188 break;
4189 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4190 desc_ioc_state = "scsi ioc terminated";
4191 break;
4192 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4193 desc_ioc_state = "scsi ext terminated";
4194 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004195 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4196 desc_ioc_state = "eedp guard error";
4197 break;
4198 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4199 desc_ioc_state = "eedp ref tag error";
4200 break;
4201 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4202 desc_ioc_state = "eedp app tag error";
4203 break;
Eric Moore635374e2009-03-09 01:21:12 -06004204 default:
4205 desc_ioc_state = "unknown";
4206 break;
4207 }
4208
4209 switch (scsi_status) {
4210 case MPI2_SCSI_STATUS_GOOD:
4211 desc_scsi_status = "good";
4212 break;
4213 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4214 desc_scsi_status = "check condition";
4215 break;
4216 case MPI2_SCSI_STATUS_CONDITION_MET:
4217 desc_scsi_status = "condition met";
4218 break;
4219 case MPI2_SCSI_STATUS_BUSY:
4220 desc_scsi_status = "busy";
4221 break;
4222 case MPI2_SCSI_STATUS_INTERMEDIATE:
4223 desc_scsi_status = "intermediate";
4224 break;
4225 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4226 desc_scsi_status = "intermediate condmet";
4227 break;
4228 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4229 desc_scsi_status = "reservation conflict";
4230 break;
4231 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4232 desc_scsi_status = "command terminated";
4233 break;
4234 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4235 desc_scsi_status = "task set full";
4236 break;
4237 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4238 desc_scsi_status = "aca active";
4239 break;
4240 case MPI2_SCSI_STATUS_TASK_ABORTED:
4241 desc_scsi_status = "task aborted";
4242 break;
4243 default:
4244 desc_scsi_status = "unknown";
4245 break;
4246 }
4247
4248 desc_scsi_state[0] = '\0';
4249 if (!scsi_state)
4250 desc_scsi_state = " ";
4251 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4252 strcat(desc_scsi_state, "response info ");
4253 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4254 strcat(desc_scsi_state, "state terminated ");
4255 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4256 strcat(desc_scsi_state, "no status ");
4257 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4258 strcat(desc_scsi_state, "autosense failed ");
4259 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4260 strcat(desc_scsi_state, "autosense valid ");
4261
4262 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304263
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304264 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304265 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4266 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304267 } else {
4268 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4269 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4270 priv_target->sas_address);
4271 if (sas_device) {
4272 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4273 "phy(%d)\n", ioc->name, sas_device->sas_address,
4274 sas_device->phy);
4275 printk(MPT2SAS_WARN_FMT
4276 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4277 ioc->name, sas_device->enclosure_logical_id,
4278 sas_device->slot);
4279 }
4280 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304281 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304282
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304283 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4284 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4285 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004286 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4287 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4288 scsi_get_resid(scmd));
4289 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4290 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4291 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4292 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4293 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4294 scsi_status, desc_scsi_state, scsi_state);
4295
4296 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4297 struct sense_info data;
4298 _scsih_normalize_sense(scmd->sense_buffer, &data);
4299 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304300 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4301 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004302 }
4303
4304 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4305 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4306 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304307 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004308 }
4309}
4310#endif
4311
4312/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304313 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004314 * @ioc: per adapter object
4315 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304316 * Context: process
4317 *
4318 * Return nothing.
4319 */
4320static void
4321_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4322{
4323 Mpi2SepReply_t mpi_reply;
4324 Mpi2SepRequest_t mpi_request;
4325
4326 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4327 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4328 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4329 mpi_request.SlotStatus =
4330 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4331 mpi_request.DevHandle = cpu_to_le16(handle);
4332 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4333 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4334 &mpi_request)) != 0) {
4335 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4336 __FILE__, __LINE__, __func__);
4337 return;
4338 }
4339
4340 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4341 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4342 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4343 le16_to_cpu(mpi_reply.IOCStatus),
4344 le32_to_cpu(mpi_reply.IOCLogInfo)));
4345 return;
4346 }
4347}
4348
4349/**
4350 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4351 * @ioc: per adapter object
4352 * @handle: device handle
4353 * Context: interrupt.
4354 *
4355 * Return nothing.
4356 */
4357static void
4358_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4359{
4360 struct fw_event_work *fw_event;
4361
4362 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4363 if (!fw_event)
4364 return;
4365 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4366 fw_event->device_handle = handle;
4367 fw_event->ioc = ioc;
4368 _scsih_fw_event_add(ioc, fw_event);
4369}
4370
4371/**
4372 * _scsih_smart_predicted_fault - process smart errors
4373 * @ioc: per adapter object
4374 * @handle: device handle
4375 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004376 *
4377 * Return nothing.
4378 */
4379static void
4380_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4381{
Eric Moore635374e2009-03-09 01:21:12 -06004382 struct scsi_target *starget;
4383 struct MPT2SAS_TARGET *sas_target_priv_data;
4384 Mpi2EventNotificationReply_t *event_reply;
4385 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4386 struct _sas_device *sas_device;
4387 ssize_t sz;
4388 unsigned long flags;
4389
4390 /* only handle non-raid devices */
4391 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4392 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4393 if (!sas_device) {
4394 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4395 return;
4396 }
4397 starget = sas_device->starget;
4398 sas_target_priv_data = starget->hostdata;
4399
4400 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4401 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4402 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4403 return;
4404 }
4405 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4406 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4407
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304408 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4409 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004410
4411 /* insert into event log */
4412 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4413 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b42011-11-07 22:05:21 +11004414 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004415 if (!event_reply) {
4416 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4417 ioc->name, __FILE__, __LINE__, __func__);
4418 return;
4419 }
4420
4421 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4422 event_reply->Event =
4423 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4424 event_reply->MsgLength = sz/4;
4425 event_reply->EventDataLength =
4426 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4427 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4428 event_reply->EventData;
4429 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4430 event_data->ASC = 0x5D;
4431 event_data->DevHandle = cpu_to_le16(handle);
4432 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4433 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4434 kfree(event_reply);
4435}
4436
4437/**
Eric Moored5d135b2009-05-18 13:02:08 -06004438 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004439 * @ioc: per adapter object
4440 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304441 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004442 * @reply: reply message frame(lower 32bit addr)
4443 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304444 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004445 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304446 * Return 1 meaning mf should be freed from _base_interrupt
4447 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004448 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304449static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304450_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004451{
4452 Mpi2SCSIIORequest_t *mpi_request;
4453 Mpi2SCSIIOReply_t *mpi_reply;
4454 struct scsi_cmnd *scmd;
4455 u16 ioc_status;
4456 u32 xfer_cnt;
4457 u8 scsi_state;
4458 u8 scsi_status;
4459 u32 log_info;
4460 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304461 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304462 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004463
4464 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304465 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004466 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304467 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004468
4469 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4470
4471 if (mpi_reply == NULL) {
4472 scmd->result = DID_OK << 16;
4473 goto out;
4474 }
4475
4476 sas_device_priv_data = scmd->device->hostdata;
4477 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4478 sas_device_priv_data->sas_target->deleted) {
4479 scmd->result = DID_NO_CONNECT << 16;
4480 goto out;
4481 }
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304482 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304483 /*
4484 * WARPDRIVE: If direct_io is set then it is directIO,
4485 * the failed direct I/O should be redirected to volume
4486 */
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304487 if (_scsih_scsi_direct_io_get(ioc, smid) &&
4488 ((ioc_status & MPI2_IOCSTATUS_MASK)
4489 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304490 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4491 ioc->scsi_lookup[smid - 1].scmd = scmd;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304492 _scsih_scsi_direct_io_set(ioc, smid, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304493 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304494 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4495 mpi_request->DevHandle =
4496 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4497 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4498 sas_device_priv_data->sas_target->handle);
4499 return 0;
4500 }
4501
Eric Moore635374e2009-03-09 01:21:12 -06004502
4503 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304504 scsi_state = mpi_reply->SCSIState;
4505 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4506 response_code =
4507 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004508 if (!sas_device_priv_data->tlr_snoop_check) {
4509 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304510 /* Make sure Device is not raid volume.
4511 * We do not expose raid functionality to upper layer for warpdrive.
4512 */
4513 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304514 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304515 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4516 sas_disable_tlr(scmd->device);
4517 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4518 }
Eric Moore635374e2009-03-09 01:21:12 -06004519 }
4520
4521 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4522 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
Eric Moore635374e2009-03-09 01:21:12 -06004523 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4524 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4525 else
4526 log_info = 0;
4527 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004528 scsi_status = mpi_reply->SCSIStatus;
4529
4530 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4531 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4532 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4533 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4534 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4535 }
4536
4537 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4538 struct sense_info data;
4539 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4540 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004541 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004542 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004543 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004544 _scsih_normalize_sense(scmd->sense_buffer, &data);
4545 /* failure prediction threshold exceeded */
4546 if (data.asc == 0x5D)
4547 _scsih_smart_predicted_fault(ioc,
4548 le16_to_cpu(mpi_reply->DevHandle));
4549 }
4550
4551 switch (ioc_status) {
4552 case MPI2_IOCSTATUS_BUSY:
4553 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4554 scmd->result = SAM_STAT_BUSY;
4555 break;
4556
4557 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4558 scmd->result = DID_NO_CONNECT << 16;
4559 break;
4560
4561 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4562 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304563 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4564 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004565 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304566 scmd->result = DID_SOFT_ERROR << 16;
4567 break;
Eric Moore635374e2009-03-09 01:21:12 -06004568 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4569 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4570 scmd->result = DID_RESET << 16;
4571 break;
4572
4573 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4574 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4575 scmd->result = DID_SOFT_ERROR << 16;
4576 else
4577 scmd->result = (DID_OK << 16) | scsi_status;
4578 break;
4579
4580 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4581 scmd->result = (DID_OK << 16) | scsi_status;
4582
4583 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4584 break;
4585
4586 if (xfer_cnt < scmd->underflow) {
4587 if (scsi_status == SAM_STAT_BUSY)
4588 scmd->result = SAM_STAT_BUSY;
4589 else
4590 scmd->result = DID_SOFT_ERROR << 16;
4591 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4592 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4593 scmd->result = DID_SOFT_ERROR << 16;
4594 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4595 scmd->result = DID_RESET << 16;
4596 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4597 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4598 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4599 scmd->result = (DRIVER_SENSE << 24) |
4600 SAM_STAT_CHECK_CONDITION;
4601 scmd->sense_buffer[0] = 0x70;
4602 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4603 scmd->sense_buffer[12] = 0x20;
4604 scmd->sense_buffer[13] = 0;
4605 }
4606 break;
4607
4608 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4609 scsi_set_resid(scmd, 0);
4610 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4611 case MPI2_IOCSTATUS_SUCCESS:
4612 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304613 if (response_code ==
4614 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4615 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4616 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004617 scmd->result = DID_SOFT_ERROR << 16;
4618 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4619 scmd->result = DID_RESET << 16;
4620 break;
4621
Eric Moore3c621b32009-05-18 12:59:41 -06004622 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4623 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4624 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4625 _scsih_eedp_error_handling(scmd, ioc_status);
4626 break;
Eric Moore635374e2009-03-09 01:21:12 -06004627 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4628 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4629 case MPI2_IOCSTATUS_INVALID_SGL:
4630 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4631 case MPI2_IOCSTATUS_INVALID_FIELD:
4632 case MPI2_IOCSTATUS_INVALID_STATE:
4633 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4634 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4635 default:
4636 scmd->result = DID_SOFT_ERROR << 16;
4637 break;
4638
4639 }
4640
4641#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4642 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4643 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4644#endif
4645
4646 out:
4647 scsi_dma_unmap(scmd);
4648 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304649 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004650}
4651
4652/**
Eric Moore635374e2009-03-09 01:21:12 -06004653 * _scsih_sas_host_refresh - refreshing sas host object contents
4654 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004655 * Context: user
4656 *
4657 * During port enable, fw will send topology events for every device. Its
4658 * possible that the handles may change from the previous setting, so this
4659 * code keeping handles updating if changed.
4660 *
4661 * Return nothing.
4662 */
4663static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304664_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004665{
4666 u16 sz;
4667 u16 ioc_status;
4668 int i;
4669 Mpi2ConfigReply_t mpi_reply;
4670 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304671 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304672 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004673
4674 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4675 "updating handles for sas_host(0x%016llx)\n",
4676 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4677
4678 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4679 * sizeof(Mpi2SasIOUnit0PhyData_t));
4680 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4681 if (!sas_iounit_pg0) {
4682 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4683 ioc->name, __FILE__, __LINE__, __func__);
4684 return;
4685 }
Eric Moore635374e2009-03-09 01:21:12 -06004686
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304687 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4688 sas_iounit_pg0, sz)) != 0)
4689 goto out;
4690 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4691 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4692 goto out;
4693 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304694 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304695 if (i == 0)
4696 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4697 PhyData[0].ControllerDevHandle);
4698 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4699 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4700 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304701 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4702 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304703 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304704 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304705 }
Eric Moore635374e2009-03-09 01:21:12 -06004706 out:
4707 kfree(sas_iounit_pg0);
4708}
4709
4710/**
4711 * _scsih_sas_host_add - create sas host object
4712 * @ioc: per adapter object
4713 *
4714 * Creating host side data object, stored in ioc->sas_hba
4715 *
4716 * Return nothing.
4717 */
4718static void
4719_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4720{
4721 int i;
4722 Mpi2ConfigReply_t mpi_reply;
4723 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4724 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4725 Mpi2SasPhyPage0_t phy_pg0;
4726 Mpi2SasDevicePage0_t sas_device_pg0;
4727 Mpi2SasEnclosurePage0_t enclosure_pg0;
4728 u16 ioc_status;
4729 u16 sz;
4730 u16 device_missing_delay;
4731
4732 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4733 if (!ioc->sas_hba.num_phys) {
4734 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4735 ioc->name, __FILE__, __LINE__, __func__);
4736 return;
4737 }
4738
4739 /* sas_iounit page 0 */
4740 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4741 sizeof(Mpi2SasIOUnit0PhyData_t));
4742 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4743 if (!sas_iounit_pg0) {
4744 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4745 ioc->name, __FILE__, __LINE__, __func__);
4746 return;
4747 }
4748 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4749 sas_iounit_pg0, sz))) {
4750 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4751 ioc->name, __FILE__, __LINE__, __func__);
4752 goto out;
4753 }
4754 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4755 MPI2_IOCSTATUS_MASK;
4756 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4757 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4758 ioc->name, __FILE__, __LINE__, __func__);
4759 goto out;
4760 }
4761
4762 /* sas_iounit page 1 */
4763 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4764 sizeof(Mpi2SasIOUnit1PhyData_t));
4765 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4766 if (!sas_iounit_pg1) {
4767 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4768 ioc->name, __FILE__, __LINE__, __func__);
4769 goto out;
4770 }
4771 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4772 sas_iounit_pg1, sz))) {
4773 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4774 ioc->name, __FILE__, __LINE__, __func__);
4775 goto out;
4776 }
4777 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4778 MPI2_IOCSTATUS_MASK;
4779 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4780 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4781 ioc->name, __FILE__, __LINE__, __func__);
4782 goto out;
4783 }
4784
4785 ioc->io_missing_delay =
4786 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4787 device_missing_delay =
4788 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4789 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4790 ioc->device_missing_delay = (device_missing_delay &
4791 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4792 else
4793 ioc->device_missing_delay = device_missing_delay &
4794 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4795
4796 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4797 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4798 sizeof(struct _sas_phy), GFP_KERNEL);
4799 if (!ioc->sas_hba.phy) {
4800 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4801 ioc->name, __FILE__, __LINE__, __func__);
4802 goto out;
4803 }
4804 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4805 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4806 i))) {
4807 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4808 ioc->name, __FILE__, __LINE__, __func__);
4809 goto out;
4810 }
4811 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4812 MPI2_IOCSTATUS_MASK;
4813 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4814 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4815 ioc->name, __FILE__, __LINE__, __func__);
4816 goto out;
4817 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304818
4819 if (i == 0)
4820 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4821 PhyData[0].ControllerDevHandle);
4822 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004823 ioc->sas_hba.phy[i].phy_id = i;
4824 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4825 phy_pg0, ioc->sas_hba.parent_dev);
4826 }
4827 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304828 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004829 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4830 ioc->name, __FILE__, __LINE__, __func__);
4831 goto out;
4832 }
Eric Moore635374e2009-03-09 01:21:12 -06004833 ioc->sas_hba.enclosure_handle =
4834 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4835 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4836 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4837 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4838 (unsigned long long) ioc->sas_hba.sas_address,
4839 ioc->sas_hba.num_phys) ;
4840
4841 if (ioc->sas_hba.enclosure_handle) {
4842 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4843 &enclosure_pg0,
4844 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4845 ioc->sas_hba.enclosure_handle))) {
4846 ioc->sas_hba.enclosure_logical_id =
4847 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4848 }
4849 }
4850
4851 out:
4852 kfree(sas_iounit_pg1);
4853 kfree(sas_iounit_pg0);
4854}
4855
4856/**
4857 * _scsih_expander_add - creating expander object
4858 * @ioc: per adapter object
4859 * @handle: expander handle
4860 *
4861 * Creating expander object, stored in ioc->sas_expander_list.
4862 *
4863 * Return 0 for success, else error.
4864 */
4865static int
4866_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4867{
4868 struct _sas_node *sas_expander;
4869 Mpi2ConfigReply_t mpi_reply;
4870 Mpi2ExpanderPage0_t expander_pg0;
4871 Mpi2ExpanderPage1_t expander_pg1;
4872 Mpi2SasEnclosurePage0_t enclosure_pg0;
4873 u32 ioc_status;
4874 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304875 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004876 int i;
4877 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304878 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004879 int rc = 0;
4880
4881 if (!handle)
4882 return -1;
4883
Eric Moore3cb54692010-07-08 14:44:34 -06004884 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304885 return -1;
4886
Eric Moore635374e2009-03-09 01:21:12 -06004887 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4888 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4889 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4890 ioc->name, __FILE__, __LINE__, __func__);
4891 return -1;
4892 }
4893
4894 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4895 MPI2_IOCSTATUS_MASK;
4896 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4897 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4898 ioc->name, __FILE__, __LINE__, __func__);
4899 return -1;
4900 }
4901
4902 /* handle out of order topology events */
4903 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304904 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4905 != 0) {
4906 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4907 ioc->name, __FILE__, __LINE__, __func__);
4908 return -1;
4909 }
4910 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004911 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304912 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4913 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004914 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4915 if (!sas_expander) {
4916 rc = _scsih_expander_add(ioc, parent_handle);
4917 if (rc != 0)
4918 return rc;
4919 }
4920 }
4921
Eric Moore635374e2009-03-09 01:21:12 -06004922 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304923 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004924 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4925 sas_address);
4926 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4927
4928 if (sas_expander)
4929 return 0;
4930
4931 sas_expander = kzalloc(sizeof(struct _sas_node),
4932 GFP_KERNEL);
4933 if (!sas_expander) {
4934 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4935 ioc->name, __FILE__, __LINE__, __func__);
4936 return -1;
4937 }
4938
4939 sas_expander->handle = handle;
4940 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304941 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004942 sas_expander->sas_address = sas_address;
4943
4944 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4945 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304946 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004947 sas_expander->sas_address, sas_expander->num_phys);
4948
4949 if (!sas_expander->num_phys)
4950 goto out_fail;
4951 sas_expander->phy = kcalloc(sas_expander->num_phys,
4952 sizeof(struct _sas_phy), GFP_KERNEL);
4953 if (!sas_expander->phy) {
4954 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4955 ioc->name, __FILE__, __LINE__, __func__);
4956 rc = -1;
4957 goto out_fail;
4958 }
4959
4960 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4961 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304962 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004963 if (!mpt2sas_port) {
4964 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4965 ioc->name, __FILE__, __LINE__, __func__);
4966 rc = -1;
4967 goto out_fail;
4968 }
4969 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4970
4971 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4972 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4973 &expander_pg1, i, handle))) {
4974 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4975 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304976 rc = -1;
4977 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004978 }
4979 sas_expander->phy[i].handle = handle;
4980 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304981
4982 if ((mpt2sas_transport_add_expander_phy(ioc,
4983 &sas_expander->phy[i], expander_pg1,
4984 sas_expander->parent_dev))) {
4985 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4986 ioc->name, __FILE__, __LINE__, __func__);
4987 rc = -1;
4988 goto out_fail;
4989 }
Eric Moore635374e2009-03-09 01:21:12 -06004990 }
4991
4992 if (sas_expander->enclosure_handle) {
4993 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4994 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4995 sas_expander->enclosure_handle))) {
4996 sas_expander->enclosure_logical_id =
4997 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4998 }
4999 }
5000
5001 _scsih_expander_node_add(ioc, sas_expander);
5002 return 0;
5003
5004 out_fail:
5005
Kashyap, Desai20f58952009-08-07 19:34:26 +05305006 if (mpt2sas_port)
5007 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305008 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005009 kfree(sas_expander);
5010 return rc;
5011}
5012
5013/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05305014 * _scsih_done - scsih callback handler.
5015 * @ioc: per adapter object
5016 * @smid: system request message index
5017 * @msix_index: MSIX table index supplied by the OS
5018 * @reply: reply message frame(lower 32bit addr)
5019 *
5020 * Callback handler when sending internal generated message frames.
5021 * The callback index passed is `ioc->scsih_cb_idx`
5022 *
5023 * Return 1 meaning mf should be freed from _base_interrupt
5024 * 0 means the mf is freed from this function.
5025 */
5026static u8
5027_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5028{
5029 MPI2DefaultReply_t *mpi_reply;
5030
5031 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5032 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
5033 return 1;
5034 if (ioc->scsih_cmds.smid != smid)
5035 return 1;
5036 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
5037 if (mpi_reply) {
5038 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5039 mpi_reply->MsgLength*4);
5040 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
5041 }
5042 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
5043 complete(&ioc->scsih_cmds.done);
5044 return 1;
5045}
5046
5047/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305048 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06005049 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305050 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06005051 *
5052 * Return nothing.
5053 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305054void
5055mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06005056{
5057 struct _sas_node *sas_expander;
5058 unsigned long flags;
5059
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305060 if (ioc->shost_recovery)
5061 return;
5062
Eric Moore635374e2009-03-09 01:21:12 -06005063 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305064 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5065 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305066 if (sas_expander)
5067 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06005068 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305069 if (sas_expander)
5070 _scsih_expander_node_remove(ioc, sas_expander);
Eric Moore635374e2009-03-09 01:21:12 -06005071}
5072
5073/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05305074 * _scsih_check_access_status - check access flags
5075 * @ioc: per adapter object
5076 * @sas_address: sas address
5077 * @handle: sas device handle
5078 * @access_flags: errors returned during discovery of the device
5079 *
5080 * Return 0 for success, else failure
5081 */
5082static u8
5083_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5084 u16 handle, u8 access_status)
5085{
5086 u8 rc = 1;
5087 char *desc = NULL;
5088
5089 switch (access_status) {
5090 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5091 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5092 rc = 0;
5093 break;
5094 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5095 desc = "sata capability failed";
5096 break;
5097 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5098 desc = "sata affiliation conflict";
5099 break;
5100 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5101 desc = "route not addressable";
5102 break;
5103 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5104 desc = "smp error not addressable";
5105 break;
5106 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5107 desc = "device blocked";
5108 break;
5109 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5110 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5111 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5112 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5113 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5114 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5115 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5116 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5117 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5118 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5119 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5120 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5121 desc = "sata initialization failed";
5122 break;
5123 default:
5124 desc = "unknown";
5125 break;
5126 }
5127
5128 if (!rc)
5129 return 0;
5130
5131 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5132 "handle(0x%04x)\n", ioc->name, desc,
5133 (unsigned long long)sas_address, handle);
5134 return rc;
5135}
5136
5137static void
5138_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5139{
5140 Mpi2ConfigReply_t mpi_reply;
5141 Mpi2SasDevicePage0_t sas_device_pg0;
5142 struct _sas_device *sas_device;
5143 u32 ioc_status;
5144 unsigned long flags;
5145 u64 sas_address;
5146 struct scsi_target *starget;
5147 struct MPT2SAS_TARGET *sas_target_priv_data;
5148 u32 device_info;
5149
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305150
Kashyap, Desaib4344272010-03-17 16:24:14 +05305151 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5152 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5153 return;
5154
5155 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5156 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5157 return;
5158
5159 /* check if this is end device */
5160 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5161 if (!(_scsih_is_end_device(device_info)))
5162 return;
5163
5164 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5165 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5166 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5167 sas_address);
5168
5169 if (!sas_device) {
5170 printk(MPT2SAS_ERR_FMT "device is not present "
5171 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5172 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5173 return;
5174 }
5175
5176 if (unlikely(sas_device->handle != handle)) {
5177 starget = sas_device->starget;
5178 sas_target_priv_data = starget->hostdata;
5179 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5180 " to (0x%04x)!!!\n", sas_device->handle, handle);
5181 sas_target_priv_data->handle = handle;
5182 sas_device->handle = handle;
5183 }
Kashyap, Desaib4344272010-03-17 16:24:14 +05305184
5185 /* check if device is present */
5186 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5187 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5188 printk(MPT2SAS_ERR_FMT "device is not present "
5189 "handle(0x%04x), flags!!!\n", ioc->name, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305190 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305191 return;
5192 }
5193
5194 /* check if there were any issues with discovery */
5195 if (_scsih_check_access_status(ioc, sas_address, handle,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305196 sas_device_pg0.AccessStatus)) {
5197 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305198 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305199 }
5200 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305201 _scsih_ublock_io_device(ioc, sas_address);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305202
5203}
5204
5205/**
Eric Moore635374e2009-03-09 01:21:12 -06005206 * _scsih_add_device - creating sas device object
5207 * @ioc: per adapter object
5208 * @handle: sas device handle
5209 * @phy_num: phy number end device attached to
5210 * @is_pd: is this hidden raid component
5211 *
5212 * Creating end device object, stored in ioc->sas_device_list.
5213 *
5214 * Returns 0 for success, non-zero for failure.
5215 */
5216static int
5217_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5218{
5219 Mpi2ConfigReply_t mpi_reply;
5220 Mpi2SasDevicePage0_t sas_device_pg0;
5221 Mpi2SasEnclosurePage0_t enclosure_pg0;
5222 struct _sas_device *sas_device;
5223 u32 ioc_status;
5224 __le64 sas_address;
5225 u32 device_info;
5226 unsigned long flags;
5227
5228 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5229 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5230 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5231 ioc->name, __FILE__, __LINE__, __func__);
5232 return -1;
5233 }
5234
5235 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5236 MPI2_IOCSTATUS_MASK;
5237 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5238 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5239 ioc->name, __FILE__, __LINE__, __func__);
5240 return -1;
5241 }
5242
Kashyap, Desaib4344272010-03-17 16:24:14 +05305243 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5244
Eric Moore635374e2009-03-09 01:21:12 -06005245 /* check if device is present */
5246 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5247 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5248 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5249 ioc->name, __FILE__, __LINE__, __func__);
5250 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5251 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5252 return -1;
5253 }
5254
Kashyap, Desaib4344272010-03-17 16:24:14 +05305255 /* check if there were any issues with discovery */
5256 if (_scsih_check_access_status(ioc, sas_address, handle,
5257 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005258 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005259
5260 /* check if this is end device */
5261 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5262 if (!(_scsih_is_end_device(device_info))) {
5263 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5264 ioc->name, __FILE__, __LINE__, __func__);
5265 return -1;
5266 }
5267
Eric Moore635374e2009-03-09 01:21:12 -06005268
5269 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5270 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5271 sas_address);
5272 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5273
Kashyap, Desaib4344272010-03-17 16:24:14 +05305274 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005275 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005276
5277 sas_device = kzalloc(sizeof(struct _sas_device),
5278 GFP_KERNEL);
5279 if (!sas_device) {
5280 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5281 ioc->name, __FILE__, __LINE__, __func__);
5282 return -1;
5283 }
5284
5285 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305286 if (_scsih_get_sas_address(ioc, le16_to_cpu
5287 (sas_device_pg0.ParentDevHandle),
5288 &sas_device->sas_address_parent) != 0)
5289 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5290 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005291 sas_device->enclosure_handle =
5292 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5293 sas_device->slot =
5294 le16_to_cpu(sas_device_pg0.Slot);
5295 sas_device->device_info = device_info;
5296 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305297 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005298
5299 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305300 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5301 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5302 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005303 sas_device->enclosure_logical_id =
5304 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005305
5306 /* get device name */
5307 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5308
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305309 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005310 _scsih_sas_device_init_add(ioc, sas_device);
5311 else
5312 _scsih_sas_device_add(ioc, sas_device);
5313
5314 return 0;
5315}
5316
5317/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305318 * _scsih_remove_device - removing sas device object
5319 * @ioc: per adapter object
5320 * @sas_device_delete: the sas_device object
5321 *
5322 * Return nothing.
5323 */
5324static void
5325_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5326 struct _sas_device *sas_device)
5327{
Kashyap, Desai1278b112010-03-09 17:34:13 +05305328 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305329
Kashyap, Desai1278b112010-03-09 17:34:13 +05305330 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5331 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305332 sas_device->handle, (unsigned long long)
5333 sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05305334
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305335 if (sas_device->starget && sas_device->starget->hostdata) {
5336 sas_target_priv_data = sas_device->starget->hostdata;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305337 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305338 _scsih_ublock_io_device(ioc, sas_device->sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305339 sas_target_priv_data->handle =
5340 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305341 }
5342
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305343 if (!ioc->hide_drives)
5344 mpt2sas_transport_port_remove(ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305345 sas_device->sas_address,
5346 sas_device->sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305347
5348 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305349 "(0x%016llx)\n", ioc->name, sas_device->handle,
5350 (unsigned long long) sas_device->sas_address);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305351
5352 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5353 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305354 sas_device->handle, (unsigned long long)
5355 sas_device->sas_address));
5356 kfree(sas_device);
5357}
5358/**
5359 * _scsih_device_remove_by_handle - removing device object by handle
5360 * @ioc: per adapter object
5361 * @handle: device handle
5362 *
5363 * Return nothing.
5364 */
5365static void
5366_scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5367{
5368 struct _sas_device *sas_device;
5369 unsigned long flags;
5370
5371 if (ioc->shost_recovery)
5372 return;
5373
5374 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5375 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5376 if (sas_device)
5377 list_del(&sas_device->list);
5378 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5379 if (sas_device)
5380 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005381}
5382
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305383/**
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305384 * mpt2sas_device_remove_by_sas_address - removing device object by sas address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305385 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305386 * @sas_address: device sas_address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305387 *
5388 * Return nothing.
5389 */
5390void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305391mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
5392 u64 sas_address)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305393{
5394 struct _sas_device *sas_device;
5395 unsigned long flags;
5396
5397 if (ioc->shost_recovery)
5398 return;
5399
5400 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5401 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5402 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305403 if (sas_device)
5404 list_del(&sas_device->list);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305405 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305406 if (sas_device)
5407 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305408}
Eric Moore635374e2009-03-09 01:21:12 -06005409#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5410/**
5411 * _scsih_sas_topology_change_event_debug - debug for topology event
5412 * @ioc: per adapter object
5413 * @event_data: event data payload
5414 * Context: user.
5415 */
5416static void
5417_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5418 Mpi2EventDataSasTopologyChangeList_t *event_data)
5419{
5420 int i;
5421 u16 handle;
5422 u16 reason_code;
5423 u8 phy_number;
5424 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305425 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005426
5427 switch (event_data->ExpStatus) {
5428 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5429 status_str = "add";
5430 break;
5431 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5432 status_str = "remove";
5433 break;
5434 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305435 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005436 status_str = "responding";
5437 break;
5438 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5439 status_str = "remove delay";
5440 break;
5441 default:
5442 status_str = "unknown status";
5443 break;
5444 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305445 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005446 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305447 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005448 "start_phy(%02d), count(%d)\n",
5449 le16_to_cpu(event_data->ExpanderDevHandle),
5450 le16_to_cpu(event_data->EnclosureHandle),
5451 event_data->StartPhyNum, event_data->NumEntries);
5452 for (i = 0; i < event_data->NumEntries; i++) {
5453 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5454 if (!handle)
5455 continue;
5456 phy_number = event_data->StartPhyNum + i;
5457 reason_code = event_data->PHY[i].PhyStatus &
5458 MPI2_EVENT_SAS_TOPO_RC_MASK;
5459 switch (reason_code) {
5460 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305461 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005462 break;
5463 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305464 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005465 break;
5466 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305467 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005468 break;
5469 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305470 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005471 break;
5472 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305473 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005474 break;
5475 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305476 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005477 break;
5478 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305479 link_rate = event_data->PHY[i].LinkRate >> 4;
5480 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305481 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305482 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5483 handle, status_str, link_rate, prev_link_rate);
5484
Eric Moore635374e2009-03-09 01:21:12 -06005485 }
5486}
5487#endif
5488
5489/**
5490 * _scsih_sas_topology_change_event - handle topology changes
5491 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305492 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005493 * Context: user.
5494 *
5495 */
5496static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305497_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005498 struct fw_event_work *fw_event)
5499{
5500 int i;
5501 u16 parent_handle, handle;
5502 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305503 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005504 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305505 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005506 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305507 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305508 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005509
5510#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5511 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5512 _scsih_sas_topology_change_event_debug(ioc, event_data);
5513#endif
5514
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305515 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305516 return;
5517
Eric Moore635374e2009-03-09 01:21:12 -06005518 if (!ioc->sas_hba.num_phys)
5519 _scsih_sas_host_add(ioc);
5520 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305521 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005522
5523 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305524 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005525 "event\n", ioc->name));
5526 return;
5527 }
5528
5529 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5530
5531 /* handle expander add */
5532 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5533 if (_scsih_expander_add(ioc, parent_handle) != 0)
5534 return;
5535
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305536 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5537 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5538 parent_handle);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305539 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305540 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305541 max_phys = sas_expander->num_phys;
5542 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305543 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305544 max_phys = ioc->sas_hba.num_phys;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305545 } else {
5546 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305547 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305548 }
5549 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305550
Eric Moore635374e2009-03-09 01:21:12 -06005551 /* handle siblings events */
5552 for (i = 0; i < event_data->NumEntries; i++) {
5553 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305554 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005555 "expander event\n", ioc->name));
5556 return;
5557 }
Eric Moore3cb54692010-07-08 14:44:34 -06005558 if (ioc->shost_recovery || ioc->remove_host ||
5559 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305560 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305561 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305562 if (phy_number >= max_phys)
5563 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305564 reason_code = event_data->PHY[i].PhyStatus &
5565 MPI2_EVENT_SAS_TOPO_RC_MASK;
5566 if ((event_data->PHY[i].PhyStatus &
5567 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5568 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005569 continue;
5570 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5571 if (!handle)
5572 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305573 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305574 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005575 switch (reason_code) {
5576 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305577
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305578 if (ioc->shost_recovery)
5579 break;
5580
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305581 if (link_rate == prev_link_rate)
5582 break;
5583
5584 mpt2sas_transport_update_links(ioc, sas_address,
5585 handle, phy_number, link_rate);
5586
Kashyap, Desaib4344272010-03-17 16:24:14 +05305587 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5588 break;
5589
5590 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305591 break;
Eric Moore635374e2009-03-09 01:21:12 -06005592 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305593
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305594 if (ioc->shost_recovery)
5595 break;
5596
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305597 mpt2sas_transport_update_links(ioc, sas_address,
5598 handle, phy_number, link_rate);
5599
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305600 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005601 break;
5602 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305603
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305604 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005605 break;
5606 }
5607 }
5608
5609 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305610 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5611 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305612 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005613
5614}
5615
5616#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5617/**
5618 * _scsih_sas_device_status_change_event_debug - debug for device event
5619 * @event_data: event data payload
5620 * Context: user.
5621 *
5622 * Return nothing.
5623 */
5624static void
5625_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5626 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5627{
5628 char *reason_str = NULL;
5629
5630 switch (event_data->ReasonCode) {
5631 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5632 reason_str = "smart data";
5633 break;
5634 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5635 reason_str = "unsupported device discovered";
5636 break;
5637 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5638 reason_str = "internal device reset";
5639 break;
5640 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5641 reason_str = "internal task abort";
5642 break;
5643 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5644 reason_str = "internal task abort set";
5645 break;
5646 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5647 reason_str = "internal clear task set";
5648 break;
5649 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5650 reason_str = "internal query task";
5651 break;
5652 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5653 reason_str = "sata init failure";
5654 break;
5655 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5656 reason_str = "internal device reset complete";
5657 break;
5658 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5659 reason_str = "internal task abort complete";
5660 break;
5661 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5662 reason_str = "internal async notification";
5663 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305664 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5665 reason_str = "expander reduced functionality";
5666 break;
5667 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5668 reason_str = "expander reduced functionality complete";
5669 break;
Eric Moore635374e2009-03-09 01:21:12 -06005670 default:
5671 reason_str = "unknown reason";
5672 break;
5673 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305674 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305675 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5676 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5677 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5678 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005679 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305680 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005681 event_data->ASC, event_data->ASCQ);
5682 printk(KERN_INFO "\n");
5683}
5684#endif
5685
5686/**
5687 * _scsih_sas_device_status_change_event - handle device status change
5688 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305689 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005690 * Context: user.
5691 *
5692 * Return nothing.
5693 */
5694static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305695_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5696 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005697{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305698 struct MPT2SAS_TARGET *target_priv_data;
5699 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305700 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305701 unsigned long flags;
5702 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5703 fw_event->event_data;
5704
Eric Moore635374e2009-03-09 01:21:12 -06005705#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5706 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305707 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305708 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005709#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305710
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305711 /* In MPI Revision K (0xC), the internal device reset complete was
5712 * implemented, so avoid setting tm_busy flag for older firmware.
5713 */
5714 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5715 return;
5716
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305717 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305718 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305719 event_data->ReasonCode !=
5720 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305721 return;
5722
5723 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5724 sas_address = le64_to_cpu(event_data->SASAddress);
5725 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5726 sas_address);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305727
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305728 if (!sas_device || !sas_device->starget) {
5729 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305730 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305731 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305732
5733 target_priv_data = sas_device->starget->hostdata;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305734 if (!target_priv_data) {
5735 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305736 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305737 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305738
5739 if (event_data->ReasonCode ==
5740 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5741 target_priv_data->tm_busy = 1;
5742 else
5743 target_priv_data->tm_busy = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305744 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005745}
5746
5747#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5748/**
5749 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5750 * @ioc: per adapter object
5751 * @event_data: event data payload
5752 * Context: user.
5753 *
5754 * Return nothing.
5755 */
5756static void
5757_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5758 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5759{
5760 char *reason_str = NULL;
5761
5762 switch (event_data->ReasonCode) {
5763 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5764 reason_str = "enclosure add";
5765 break;
5766 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5767 reason_str = "enclosure remove";
5768 break;
5769 default:
5770 reason_str = "unknown reason";
5771 break;
5772 }
5773
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305774 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005775 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5776 " number slots(%d)\n", ioc->name, reason_str,
5777 le16_to_cpu(event_data->EnclosureHandle),
5778 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5779 le16_to_cpu(event_data->StartSlot));
5780}
5781#endif
5782
5783/**
5784 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5785 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305786 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005787 * Context: user.
5788 *
5789 * Return nothing.
5790 */
5791static void
5792_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305793 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005794{
5795#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5796 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5797 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305798 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005799#endif
5800}
5801
5802/**
Andrew Mortona78e21d2012-02-08 12:52:22 -08005803 * _scsih_sas_broadcast_primitive_event - handle broadcast events
Eric Moore635374e2009-03-09 01:21:12 -06005804 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305805 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005806 * Context: user.
5807 *
5808 * Return nothing.
5809 */
5810static void
Andrew Mortona78e21d2012-02-08 12:52:22 -08005811_scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305812 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005813{
5814 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305815 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005816 u16 smid, handle;
5817 u32 lun;
5818 struct MPT2SAS_DEVICE *sas_device_priv_data;
5819 u32 termination_count;
5820 u32 query_count;
5821 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305822 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305823 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305824 unsigned long flags;
5825 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305826 u8 max_retries = 0;
5827 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305828
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305829 mutex_lock(&ioc->tm_cmds.mutex);
Reddy, Sreekanthc3a634b2013-02-26 17:36:12 +05305830 pr_info(MPT2SAS_FMT
5831 "%s: enter: phy number(%d), width(%d)\n",
5832 ioc->name, __func__, event_data->PhyNum,
5833 event_data->PortWidth);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305834
5835 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005836
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305837 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305838 mpi_reply = ioc->tm_cmds.reply;
5839broadcast_aen_retry:
5840
5841 /* sanity checks for retrying this loop */
5842 if (max_retries++ == 5) {
5843 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5844 ioc->name, __func__));
5845 goto out;
5846 } else if (max_retries > 1)
5847 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5848 ioc->name, __func__, max_retries - 1));
5849
Eric Moore635374e2009-03-09 01:21:12 -06005850 termination_count = 0;
5851 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305852 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305853 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305854 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005855 scmd = _scsih_scsi_lookup_get(ioc, smid);
5856 if (!scmd)
5857 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305858 sdev = scmd->device;
5859 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005860 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5861 continue;
5862 /* skip hidden raid components */
5863 if (sas_device_priv_data->sas_target->flags &
5864 MPT_TARGET_FLAGS_RAID_COMPONENT)
5865 continue;
5866 /* skip volumes */
5867 if (sas_device_priv_data->sas_target->flags &
5868 MPT_TARGET_FLAGS_VOLUME)
5869 continue;
5870
5871 handle = sas_device_priv_data->sas_target->handle;
5872 lun = sas_device_priv_data->lun;
5873 query_count++;
5874
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305875 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305876 goto out;
5877
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305878 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305879 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04005880 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305881 TM_MUTEX_OFF);
5882 if (r == FAILED) {
5883 sdev_printk(KERN_WARNING, sdev,
5884 "mpt2sas_scsih_issue_tm: FAILED when sending "
5885 "QUERY_TASK: scmd(%p)\n", scmd);
5886 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5887 goto broadcast_aen_retry;
5888 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305889 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5890 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305891 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5892 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5893 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5894 scmd);
5895 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5896 goto broadcast_aen_retry;
5897 }
5898
5899 /* see if IO is still owned by IOC and target */
5900 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005901 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5902 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305903 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305904 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005905 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305906 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305907 task_abort_retries = 0;
5908 tm_retry:
5909 if (task_abort_retries++ == 60) {
5910 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5911 "%s: ABORT_TASK: giving up\n", ioc->name,
5912 __func__));
5913 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5914 goto broadcast_aen_retry;
5915 }
5916
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305917 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305918 goto out_no_lock;
5919
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305920 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5921 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Matthew Wilcox2e45c8b2014-03-27 16:40:32 -04005922 TM_MUTEX_OFF);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305923 if (r == FAILED) {
5924 sdev_printk(KERN_WARNING, sdev,
5925 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305926 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305927 goto tm_retry;
5928 }
5929
5930 if (task_abort_retries > 1)
5931 sdev_printk(KERN_WARNING, sdev,
5932 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5933 " scmd(%p)\n",
5934 task_abort_retries - 1, scmd);
5935
Eric Moore635374e2009-03-09 01:21:12 -06005936 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305937 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005938 }
Eric Moore635374e2009-03-09 01:21:12 -06005939
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305940 if (ioc->broadcast_aen_pending) {
5941 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5942 " pending AEN\n", ioc->name, __func__));
5943 ioc->broadcast_aen_pending = 0;
5944 goto broadcast_aen_retry;
5945 }
5946
5947 out:
5948 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5949 out_no_lock:
5950
5951 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005952 "%s - exit, query_count = %d termination_count = %d\n",
5953 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305954
5955 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305956 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305957 _scsih_ublock_io_all_device(ioc);
5958 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005959}
5960
5961/**
5962 * _scsih_sas_discovery_event - handle discovery events
5963 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305964 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005965 * Context: user.
5966 *
5967 * Return nothing.
5968 */
5969static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305970_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5971 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005972{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305973 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5974
Eric Moore635374e2009-03-09 01:21:12 -06005975#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5976 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305977 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005978 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5979 "start" : "stop");
5980 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305981 printk("discovery_status(0x%08x)",
5982 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005983 printk("\n");
5984 }
5985#endif
5986
5987 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305988 !ioc->sas_hba.num_phys) {
5989 if (disable_discovery > 0 && ioc->shost_recovery) {
5990 /* Wait for the reset to complete */
5991 while (ioc->shost_recovery)
5992 ssleep(1);
5993 }
Eric Moore635374e2009-03-09 01:21:12 -06005994 _scsih_sas_host_add(ioc);
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305995 }
Eric Moore635374e2009-03-09 01:21:12 -06005996}
5997
5998/**
5999 * _scsih_reprobe_lun - reprobing lun
6000 * @sdev: scsi device struct
6001 * @no_uld_attach: sdev->no_uld_attach flag setting
6002 *
6003 **/
6004static void
6005_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
6006{
6007 int rc;
6008
6009 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6010 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6011 sdev->no_uld_attach ? "hidding" : "exposing");
6012 rc = scsi_device_reprobe(sdev);
6013}
6014
6015/**
Eric Moore635374e2009-03-09 01:21:12 -06006016 * _scsih_sas_volume_add - add new volume
6017 * @ioc: per adapter object
6018 * @element: IR config element data
6019 * Context: user.
6020 *
6021 * Return nothing.
6022 */
6023static void
6024_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
6025 Mpi2EventIrConfigElement_t *element)
6026{
6027 struct _raid_device *raid_device;
6028 unsigned long flags;
6029 u64 wwid;
6030 u16 handle = le16_to_cpu(element->VolDevHandle);
6031 int rc;
6032
Eric Moore635374e2009-03-09 01:21:12 -06006033 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6034 if (!wwid) {
6035 printk(MPT2SAS_ERR_FMT
6036 "failure at %s:%d/%s()!\n", ioc->name,
6037 __FILE__, __LINE__, __func__);
6038 return;
6039 }
6040
6041 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6042 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6043 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6044
6045 if (raid_device)
6046 return;
6047
6048 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6049 if (!raid_device) {
6050 printk(MPT2SAS_ERR_FMT
6051 "failure at %s:%d/%s()!\n", ioc->name,
6052 __FILE__, __LINE__, __func__);
6053 return;
6054 }
6055
6056 raid_device->id = ioc->sas_id++;
6057 raid_device->channel = RAID_CHANNEL;
6058 raid_device->handle = handle;
6059 raid_device->wwid = wwid;
6060 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306061 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06006062 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6063 raid_device->id, 0);
6064 if (rc)
6065 _scsih_raid_device_remove(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306066 } else {
6067 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006068 _scsih_determine_boot_device(ioc, raid_device, 1);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306069 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6070 }
Eric Moore635374e2009-03-09 01:21:12 -06006071}
6072
6073/**
6074 * _scsih_sas_volume_delete - delete volume
6075 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306076 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006077 * Context: user.
6078 *
6079 * Return nothing.
6080 */
6081static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306082_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006083{
6084 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006085 unsigned long flags;
6086 struct MPT2SAS_TARGET *sas_target_priv_data;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306087 struct scsi_target *starget = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006088
Eric Moore635374e2009-03-09 01:21:12 -06006089 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6090 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306091 if (raid_device) {
6092 if (raid_device->starget) {
6093 starget = raid_device->starget;
6094 sas_target_priv_data = starget->hostdata;
6095 sas_target_priv_data->deleted = 1;
6096 }
6097 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6098 "(0x%016llx)\n", ioc->name, raid_device->handle,
6099 (unsigned long long) raid_device->wwid);
6100 list_del(&raid_device->list);
6101 kfree(raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006102 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306103 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6104 if (starget)
6105 scsi_remove_target(&starget->dev);
Eric Moore635374e2009-03-09 01:21:12 -06006106}
6107
6108/**
6109 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6110 * @ioc: per adapter object
6111 * @element: IR config element data
6112 * Context: user.
6113 *
6114 * Return nothing.
6115 */
6116static void
6117_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6118 Mpi2EventIrConfigElement_t *element)
6119{
6120 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306121 struct scsi_target *starget = NULL;
6122 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006123 unsigned long flags;
6124 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6125
6126 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6127 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306128 if (sas_device) {
6129 sas_device->volume_handle = 0;
6130 sas_device->volume_wwid = 0;
6131 clear_bit(handle, ioc->pd_handles);
6132 if (sas_device->starget && sas_device->starget->hostdata) {
6133 starget = sas_device->starget;
6134 sas_target_priv_data = starget->hostdata;
6135 sas_target_priv_data->flags &=
6136 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6137 }
6138 }
Eric Moore635374e2009-03-09 01:21:12 -06006139 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6140 if (!sas_device)
6141 return;
6142
6143 /* exposing raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306144 if (starget)
6145 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006146}
6147
6148/**
6149 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6150 * @ioc: per adapter object
6151 * @element: IR config element data
6152 * Context: user.
6153 *
6154 * Return nothing.
6155 */
6156static void
6157_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6158 Mpi2EventIrConfigElement_t *element)
6159{
6160 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306161 struct scsi_target *starget = NULL;
6162 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006163 unsigned long flags;
6164 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306165 u16 volume_handle = 0;
6166 u64 volume_wwid = 0;
6167
6168 mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
6169 if (volume_handle)
6170 mpt2sas_config_get_volume_wwid(ioc, volume_handle,
6171 &volume_wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006172
6173 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6174 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306175 if (sas_device) {
6176 set_bit(handle, ioc->pd_handles);
6177 if (sas_device->starget && sas_device->starget->hostdata) {
6178 starget = sas_device->starget;
6179 sas_target_priv_data = starget->hostdata;
6180 sas_target_priv_data->flags |=
6181 MPT_TARGET_FLAGS_RAID_COMPONENT;
6182 sas_device->volume_handle = volume_handle;
6183 sas_device->volume_wwid = volume_wwid;
6184 }
6185 }
Eric Moore635374e2009-03-09 01:21:12 -06006186 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6187 if (!sas_device)
6188 return;
6189
6190 /* hiding raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306191 if (starget)
6192 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006193}
6194
6195/**
6196 * _scsih_sas_pd_delete - delete pd component
6197 * @ioc: per adapter object
6198 * @element: IR config element data
6199 * Context: user.
6200 *
6201 * Return nothing.
6202 */
6203static void
6204_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6205 Mpi2EventIrConfigElement_t *element)
6206{
Eric Moore635374e2009-03-09 01:21:12 -06006207 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6208
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306209 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006210}
6211
6212/**
6213 * _scsih_sas_pd_add - remove pd component
6214 * @ioc: per adapter object
6215 * @element: IR config element data
6216 * Context: user.
6217 *
6218 * Return nothing.
6219 */
6220static void
6221_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6222 Mpi2EventIrConfigElement_t *element)
6223{
6224 struct _sas_device *sas_device;
6225 unsigned long flags;
6226 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306227 Mpi2ConfigReply_t mpi_reply;
6228 Mpi2SasDevicePage0_t sas_device_pg0;
6229 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306230 u64 sas_address;
6231 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006232
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306233 set_bit(handle, ioc->pd_handles);
6234
Eric Moore635374e2009-03-09 01:21:12 -06006235 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6236 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6237 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306238 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306239 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306240
6241 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6242 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6243 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6244 ioc->name, __FILE__, __LINE__, __func__);
6245 return;
6246 }
6247
6248 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6249 MPI2_IOCSTATUS_MASK;
6250 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6251 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6252 ioc->name, __FILE__, __LINE__, __func__);
6253 return;
6254 }
6255
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306256 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6257 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6258 mpt2sas_transport_update_links(ioc, sas_address, handle,
6259 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306260
6261 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006262}
6263
6264#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6265/**
6266 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6267 * @ioc: per adapter object
6268 * @event_data: event data payload
6269 * Context: user.
6270 *
6271 * Return nothing.
6272 */
6273static void
6274_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6275 Mpi2EventDataIrConfigChangeList_t *event_data)
6276{
6277 Mpi2EventIrConfigElement_t *element;
6278 u8 element_type;
6279 int i;
6280 char *reason_str = NULL, *element_str = NULL;
6281
6282 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6283
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306284 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006285 ioc->name, (le32_to_cpu(event_data->Flags) &
6286 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6287 "foreign" : "native", event_data->NumElements);
6288 for (i = 0; i < event_data->NumElements; i++, element++) {
6289 switch (element->ReasonCode) {
6290 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6291 reason_str = "add";
6292 break;
6293 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6294 reason_str = "remove";
6295 break;
6296 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6297 reason_str = "no change";
6298 break;
6299 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6300 reason_str = "hide";
6301 break;
6302 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6303 reason_str = "unhide";
6304 break;
6305 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6306 reason_str = "volume_created";
6307 break;
6308 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6309 reason_str = "volume_deleted";
6310 break;
6311 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6312 reason_str = "pd_created";
6313 break;
6314 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6315 reason_str = "pd_deleted";
6316 break;
6317 default:
6318 reason_str = "unknown reason";
6319 break;
6320 }
6321 element_type = le16_to_cpu(element->ElementFlags) &
6322 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6323 switch (element_type) {
6324 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6325 element_str = "volume";
6326 break;
6327 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6328 element_str = "phys disk";
6329 break;
6330 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6331 element_str = "hot spare";
6332 break;
6333 default:
6334 element_str = "unknown element";
6335 break;
6336 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306337 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006338 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6339 reason_str, le16_to_cpu(element->VolDevHandle),
6340 le16_to_cpu(element->PhysDiskDevHandle),
6341 element->PhysDiskNum);
6342 }
6343}
6344#endif
6345
6346/**
6347 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6348 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306349 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006350 * Context: user.
6351 *
6352 * Return nothing.
6353 */
6354static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306355_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6356 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006357{
6358 Mpi2EventIrConfigElement_t *element;
6359 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306360 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306361 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006362
6363#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306364 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6365 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006366 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6367
6368#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306369
6370 if (ioc->shost_recovery)
6371 return;
6372
Kashyap, Desai62727a72009-08-07 19:35:18 +05306373 foreign_config = (le32_to_cpu(event_data->Flags) &
6374 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006375
6376 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6377 for (i = 0; i < event_data->NumElements; i++, element++) {
6378
6379 switch (element->ReasonCode) {
6380 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6381 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306382 if (!foreign_config)
6383 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006384 break;
6385 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6386 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306387 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306388 _scsih_sas_volume_delete(ioc,
6389 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006390 break;
6391 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306392 if (!ioc->is_warpdrive)
6393 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006394 break;
6395 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306396 if (!ioc->is_warpdrive)
6397 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006398 break;
6399 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306400 if (!ioc->is_warpdrive)
6401 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006402 break;
6403 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306404 if (!ioc->is_warpdrive)
6405 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006406 break;
6407 }
6408 }
6409}
6410
6411/**
6412 * _scsih_sas_ir_volume_event - IR volume event
6413 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306414 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006415 * Context: user.
6416 *
6417 * Return nothing.
6418 */
6419static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306420_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6421 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006422{
6423 u64 wwid;
6424 unsigned long flags;
6425 struct _raid_device *raid_device;
6426 u16 handle;
6427 u32 state;
6428 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306429 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006430
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306431 if (ioc->shost_recovery)
6432 return;
6433
Eric Moore635374e2009-03-09 01:21:12 -06006434 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6435 return;
6436
6437 handle = le16_to_cpu(event_data->VolDevHandle);
6438 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306439 if (!ioc->hide_ir_msg)
6440 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6441 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6442 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006443
Eric Moore635374e2009-03-09 01:21:12 -06006444 switch (state) {
6445 case MPI2_RAID_VOL_STATE_MISSING:
6446 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306447 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006448 break;
6449
6450 case MPI2_RAID_VOL_STATE_ONLINE:
6451 case MPI2_RAID_VOL_STATE_DEGRADED:
6452 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306453
6454 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6455 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6456 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6457
Eric Moore635374e2009-03-09 01:21:12 -06006458 if (raid_device)
6459 break;
6460
6461 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6462 if (!wwid) {
6463 printk(MPT2SAS_ERR_FMT
6464 "failure at %s:%d/%s()!\n", ioc->name,
6465 __FILE__, __LINE__, __func__);
6466 break;
6467 }
6468
6469 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6470 if (!raid_device) {
6471 printk(MPT2SAS_ERR_FMT
6472 "failure at %s:%d/%s()!\n", ioc->name,
6473 __FILE__, __LINE__, __func__);
6474 break;
6475 }
6476
6477 raid_device->id = ioc->sas_id++;
6478 raid_device->channel = RAID_CHANNEL;
6479 raid_device->handle = handle;
6480 raid_device->wwid = wwid;
6481 _scsih_raid_device_add(ioc, raid_device);
6482 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6483 raid_device->id, 0);
6484 if (rc)
6485 _scsih_raid_device_remove(ioc, raid_device);
6486 break;
6487
6488 case MPI2_RAID_VOL_STATE_INITIALIZING:
6489 default:
6490 break;
6491 }
6492}
6493
6494/**
6495 * _scsih_sas_ir_physical_disk_event - PD event
6496 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306497 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006498 * Context: user.
6499 *
6500 * Return nothing.
6501 */
6502static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306503_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6504 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006505{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306506 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006507 u32 state;
6508 struct _sas_device *sas_device;
6509 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306510 Mpi2ConfigReply_t mpi_reply;
6511 Mpi2SasDevicePage0_t sas_device_pg0;
6512 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306513 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306514 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006515
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306516 if (ioc->shost_recovery)
6517 return;
6518
Eric Moore635374e2009-03-09 01:21:12 -06006519 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6520 return;
6521
6522 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6523 state = le32_to_cpu(event_data->NewValue);
6524
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306525 if (!ioc->hide_ir_msg)
6526 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6527 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6528 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006529
Eric Moore635374e2009-03-09 01:21:12 -06006530 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006531 case MPI2_RAID_PD_STATE_ONLINE:
6532 case MPI2_RAID_PD_STATE_DEGRADED:
6533 case MPI2_RAID_PD_STATE_REBUILDING:
6534 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306535 case MPI2_RAID_PD_STATE_HOT_SPARE:
6536
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306537 if (!ioc->is_warpdrive)
6538 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306539
6540 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6541 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6542 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6543
6544 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306545 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306546
6547 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6548 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6549 handle))) {
6550 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6551 ioc->name, __FILE__, __LINE__, __func__);
6552 return;
6553 }
6554
6555 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6556 MPI2_IOCSTATUS_MASK;
6557 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6558 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6559 ioc->name, __FILE__, __LINE__, __func__);
6560 return;
6561 }
6562
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306563 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6564 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6565 mpt2sas_transport_update_links(ioc, sas_address, handle,
6566 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306567
6568 _scsih_add_device(ioc, handle, 0, 1);
6569
Eric Moore635374e2009-03-09 01:21:12 -06006570 break;
6571
Kashyap, Desai62727a72009-08-07 19:35:18 +05306572 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006573 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6574 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006575 default:
6576 break;
6577 }
6578}
6579
6580#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6581/**
6582 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6583 * @ioc: per adapter object
6584 * @event_data: event data payload
6585 * Context: user.
6586 *
6587 * Return nothing.
6588 */
6589static void
6590_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6591 Mpi2EventDataIrOperationStatus_t *event_data)
6592{
6593 char *reason_str = NULL;
6594
6595 switch (event_data->RAIDOperation) {
6596 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6597 reason_str = "resync";
6598 break;
6599 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6600 reason_str = "online capacity expansion";
6601 break;
6602 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6603 reason_str = "consistency check";
6604 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306605 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6606 reason_str = "background init";
6607 break;
6608 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6609 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006610 break;
6611 }
6612
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306613 if (!reason_str)
6614 return;
6615
Eric Moore635374e2009-03-09 01:21:12 -06006616 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6617 "\thandle(0x%04x), percent complete(%d)\n",
6618 ioc->name, reason_str,
6619 le16_to_cpu(event_data->VolDevHandle),
6620 event_data->PercentComplete);
6621}
6622#endif
6623
6624/**
6625 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6626 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306627 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006628 * Context: user.
6629 *
6630 * Return nothing.
6631 */
6632static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306633_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6634 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006635{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306636 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6637 static struct _raid_device *raid_device;
6638 unsigned long flags;
6639 u16 handle;
6640
Eric Moore635374e2009-03-09 01:21:12 -06006641#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306642 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6643 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306644 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306645 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006646#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306647
6648 /* code added for raid transport support */
6649 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6650
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306651 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306652 handle = le16_to_cpu(event_data->VolDevHandle);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306653 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306654 if (raid_device)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306655 raid_device->percent_complete =
6656 event_data->PercentComplete;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306657 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306658 }
Eric Moore635374e2009-03-09 01:21:12 -06006659}
6660
6661/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306662 * _scsih_prep_device_scan - initialize parameters prior to device scan
6663 * @ioc: per adapter object
6664 *
6665 * Set the deleted flag prior to device scan. If the device is found during
6666 * the scan, then we clear the deleted flag.
6667 */
6668static void
6669_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6670{
6671 struct MPT2SAS_DEVICE *sas_device_priv_data;
6672 struct scsi_device *sdev;
6673
6674 shost_for_each_device(sdev, ioc->shost) {
6675 sas_device_priv_data = sdev->hostdata;
6676 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6677 sas_device_priv_data->sas_target->deleted = 1;
6678 }
6679}
6680
6681/**
Eric Moore635374e2009-03-09 01:21:12 -06006682 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6683 * @ioc: per adapter object
6684 * @sas_address: sas address
6685 * @slot: enclosure slot id
6686 * @handle: device handle
6687 *
6688 * After host reset, find out whether devices are still responding.
6689 * Used in _scsi_remove_unresponsive_sas_devices.
6690 *
6691 * Return nothing.
6692 */
6693static void
6694_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6695 u16 slot, u16 handle)
6696{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306697 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006698 struct scsi_target *starget;
6699 struct _sas_device *sas_device;
6700 unsigned long flags;
6701
6702 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6703 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6704 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306705 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006706 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306707 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306708 if (starget && starget->hostdata) {
6709 sas_target_priv_data = starget->hostdata;
6710 sas_target_priv_data->tm_busy = 0;
6711 sas_target_priv_data->deleted = 0;
6712 } else
6713 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306714 if (starget)
6715 starget_printk(KERN_INFO, starget,
6716 "handle(0x%04x), sas_addr(0x%016llx), "
6717 "enclosure logical id(0x%016llx), "
6718 "slot(%d)\n", handle,
6719 (unsigned long long)sas_device->sas_address,
6720 (unsigned long long)
6721 sas_device->enclosure_logical_id,
6722 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006723 if (sas_device->handle == handle)
6724 goto out;
6725 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6726 sas_device->handle);
6727 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306728 if (sas_target_priv_data)
6729 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006730 goto out;
6731 }
6732 }
6733 out:
6734 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6735}
6736
6737/**
6738 * _scsih_search_responding_sas_devices -
6739 * @ioc: per adapter object
6740 *
6741 * After host reset, find out whether devices are still responding.
6742 * If not remove.
6743 *
6744 * Return nothing.
6745 */
6746static void
6747_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6748{
6749 Mpi2SasDevicePage0_t sas_device_pg0;
6750 Mpi2ConfigReply_t mpi_reply;
6751 u16 ioc_status;
6752 __le64 sas_address;
6753 u16 handle;
6754 u32 device_info;
6755 u16 slot;
6756
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306757 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006758
6759 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306760 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006761
6762 handle = 0xFFFF;
6763 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6764 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6765 handle))) {
6766 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6767 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306768 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006769 break;
6770 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6771 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6772 if (!(_scsih_is_end_device(device_info)))
6773 continue;
6774 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6775 slot = le16_to_cpu(sas_device_pg0.Slot);
6776 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6777 handle);
6778 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306779out:
6780 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6781 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006782}
6783
6784/**
6785 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6786 * @ioc: per adapter object
6787 * @wwid: world wide identifier for raid volume
6788 * @handle: device handle
6789 *
6790 * After host reset, find out whether devices are still responding.
6791 * Used in _scsi_remove_unresponsive_raid_devices.
6792 *
6793 * Return nothing.
6794 */
6795static void
6796_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6797 u16 handle)
6798{
6799 struct MPT2SAS_TARGET *sas_target_priv_data;
6800 struct scsi_target *starget;
6801 struct _raid_device *raid_device;
6802 unsigned long flags;
6803
6804 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6805 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6806 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306807 starget = raid_device->starget;
6808 if (starget && starget->hostdata) {
6809 sas_target_priv_data = starget->hostdata;
6810 sas_target_priv_data->deleted = 0;
6811 } else
6812 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006813 raid_device->responding = 1;
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306814 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006815 starget_printk(KERN_INFO, raid_device->starget,
6816 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6817 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306818 /*
6819 * WARPDRIVE: The handles of the PDs might have changed
6820 * across the host reset so re-initialize the
6821 * required data for Direct IO
6822 */
6823 _scsih_init_warpdrive_properties(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306824 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6825 if (raid_device->handle == handle) {
6826 spin_unlock_irqrestore(&ioc->raid_device_lock,
6827 flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306828 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306829 }
Eric Moore635374e2009-03-09 01:21:12 -06006830 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6831 raid_device->handle);
6832 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306833 if (sas_target_priv_data)
6834 sas_target_priv_data->handle = handle;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306835 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306836 return;
Eric Moore635374e2009-03-09 01:21:12 -06006837 }
6838 }
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306839
Eric Moore635374e2009-03-09 01:21:12 -06006840 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6841}
6842
6843/**
6844 * _scsih_search_responding_raid_devices -
6845 * @ioc: per adapter object
6846 *
6847 * After host reset, find out whether devices are still responding.
6848 * If not remove.
6849 *
6850 * Return nothing.
6851 */
6852static void
6853_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6854{
6855 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306856 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306857 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006858 Mpi2ConfigReply_t mpi_reply;
6859 u16 ioc_status;
6860 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306861 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006862
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306863 if (!ioc->ir_firmware)
6864 return;
6865
6866 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6867 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006868
6869 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306870 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006871
6872 handle = 0xFFFF;
6873 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6874 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6875 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6876 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306877 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006878 break;
6879 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306880
6881 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6882 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6883 sizeof(Mpi2RaidVolPage0_t)))
6884 continue;
6885
6886 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6887 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6888 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6889 _scsih_mark_responding_raid_device(ioc,
6890 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006891 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306892
6893 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306894 if (!ioc->is_warpdrive) {
6895 phys_disk_num = 0xFF;
6896 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6897 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6898 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6899 phys_disk_num))) {
6900 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6901 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306902 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306903 break;
6904 phys_disk_num = pd_pg0.PhysDiskNum;
6905 handle = le16_to_cpu(pd_pg0.DevHandle);
6906 set_bit(handle, ioc->pd_handles);
6907 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306908 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306909out:
6910 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6911 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006912}
6913
6914/**
6915 * _scsih_mark_responding_expander - mark a expander as responding
6916 * @ioc: per adapter object
6917 * @sas_address: sas address
6918 * @handle:
6919 *
6920 * After host reset, find out whether devices are still responding.
6921 * Used in _scsi_remove_unresponsive_expanders.
6922 *
6923 * Return nothing.
6924 */
6925static void
6926_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6927 u16 handle)
6928{
6929 struct _sas_node *sas_expander;
6930 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306931 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006932
6933 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6934 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306935 if (sas_expander->sas_address != sas_address)
6936 continue;
6937 sas_expander->responding = 1;
6938 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006939 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306940 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6941 " from(0x%04x) to (0x%04x)!!!\n",
6942 (unsigned long long)sas_expander->sas_address,
6943 sas_expander->handle, handle);
6944 sas_expander->handle = handle;
6945 for (i = 0 ; i < sas_expander->num_phys ; i++)
6946 sas_expander->phy[i].handle = handle;
6947 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006948 }
6949 out:
6950 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6951}
6952
6953/**
6954 * _scsih_search_responding_expanders -
6955 * @ioc: per adapter object
6956 *
6957 * After host reset, find out whether devices are still responding.
6958 * If not remove.
6959 *
6960 * Return nothing.
6961 */
6962static void
6963_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6964{
6965 Mpi2ExpanderPage0_t expander_pg0;
6966 Mpi2ConfigReply_t mpi_reply;
6967 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306968 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006969 u16 handle;
6970
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306971 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006972
6973 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306974 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006975
6976 handle = 0xFFFF;
6977 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6978 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6979
6980 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6981 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy1c50e8d2013-07-25 11:29:45 +05306982 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
Eric Moore635374e2009-03-09 01:21:12 -06006983 break;
6984
6985 handle = le16_to_cpu(expander_pg0.DevHandle);
6986 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6987 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6988 "sas_addr(0x%016llx)\n", handle,
6989 (unsigned long long)sas_address);
6990 _scsih_mark_responding_expander(ioc, sas_address, handle);
6991 }
6992
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306993 out:
6994 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006995}
6996
6997/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306998 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006999 * @ioc: per adapter object
7000 *
7001 * Return nothing.
7002 */
7003static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307004_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06007005{
7006 struct _sas_device *sas_device, *sas_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307007 struct _sas_node *sas_expander, *sas_expander_next;
Eric Moore635374e2009-03-09 01:21:12 -06007008 struct _raid_device *raid_device, *raid_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307009 struct list_head tmp_list;
7010 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007011
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307012 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
7013 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007014
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307015 /* removing unresponding end devices */
7016 printk(MPT2SAS_INFO_FMT "removing unresponding devices: end-devices\n",
7017 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007018 list_for_each_entry_safe(sas_device, sas_device_next,
7019 &ioc->sas_device_list, list) {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307020 if (!sas_device->responding)
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05307021 mpt2sas_device_remove_by_sas_address(ioc,
7022 sas_device->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307023 else
Eric Moore635374e2009-03-09 01:21:12 -06007024 sas_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007025 }
7026
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307027 /* removing unresponding volumes */
7028 if (ioc->ir_firmware) {
7029 printk(MPT2SAS_INFO_FMT "removing unresponding devices: "
7030 "volumes\n", ioc->name);
7031 list_for_each_entry_safe(raid_device, raid_device_next,
7032 &ioc->raid_device_list, list) {
7033 if (!raid_device->responding)
7034 _scsih_sas_volume_delete(ioc,
7035 raid_device->handle);
7036 else
7037 raid_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007038 }
Eric Moore635374e2009-03-09 01:21:12 -06007039 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307040 /* removing unresponding expanders */
7041 printk(MPT2SAS_INFO_FMT "removing unresponding devices: expanders\n",
7042 ioc->name);
7043 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7044 INIT_LIST_HEAD(&tmp_list);
7045 list_for_each_entry_safe(sas_expander, sas_expander_next,
7046 &ioc->sas_expander_list, list) {
7047 if (!sas_expander->responding)
7048 list_move_tail(&sas_expander->list, &tmp_list);
7049 else
Eric Moore635374e2009-03-09 01:21:12 -06007050 sas_expander->responding = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307051 }
7052 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7053 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7054 list) {
7055 list_del(&sas_expander->list);
7056 _scsih_expander_node_remove(ioc, sas_expander);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307057 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307058 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
7059 ioc->name);
7060 /* unblock devices */
7061 _scsih_ublock_io_all_device(ioc);
7062}
7063
7064static void
7065_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
7066 struct _sas_node *sas_expander, u16 handle)
7067{
7068 Mpi2ExpanderPage1_t expander_pg1;
7069 Mpi2ConfigReply_t mpi_reply;
7070 int i;
7071
7072 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7073 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
7074 &expander_pg1, i, handle))) {
7075 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7076 ioc->name, __FILE__, __LINE__, __func__);
7077 return;
7078 }
7079
7080 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
7081 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7082 expander_pg1.NegotiatedLinkRate >> 4);
7083 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307084}
7085
7086/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307087 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307088 * @ioc: per adapter object
7089 *
7090 * Return nothing.
7091 */
7092static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307093_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307094{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307095 Mpi2ExpanderPage0_t expander_pg0;
7096 Mpi2SasDevicePage0_t sas_device_pg0;
7097 Mpi2RaidVolPage1_t volume_pg1;
7098 Mpi2RaidVolPage0_t volume_pg0;
7099 Mpi2RaidPhysDiskPage0_t pd_pg0;
7100 Mpi2EventIrConfigElement_t element;
7101 Mpi2ConfigReply_t mpi_reply;
7102 u8 phys_disk_num;
7103 u16 ioc_status;
7104 u16 handle, parent_handle;
7105 u64 sas_address;
7106 struct _sas_device *sas_device;
7107 struct _sas_node *expander_device;
7108 static struct _raid_device *raid_device;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307109 u8 retry_count;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307110 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307111
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307112 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307113
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307114 _scsih_sas_host_refresh(ioc);
7115
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307116 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders start\n",
7117 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307118 /* expanders */
7119 handle = 0xFFFF;
7120 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7121 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7122 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7123 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307124 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7125 printk(MPT2SAS_INFO_FMT "\tbreak from expander scan: "
7126 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7127 ioc->name, ioc_status,
7128 le32_to_cpu(mpi_reply.IOCLogInfo));
7129 break;
7130 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307131 handle = le16_to_cpu(expander_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307132 spin_lock_irqsave(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307133 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7134 ioc, le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307135 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307136 if (expander_device)
7137 _scsih_refresh_expander_links(ioc, expander_device,
7138 handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307139 else {
7140 printk(MPT2SAS_INFO_FMT "\tBEFORE adding expander: "
7141 "handle (0x%04x), sas_addr(0x%016llx)\n",
7142 ioc->name, handle, (unsigned long long)
7143 le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307144 _scsih_expander_add(ioc, handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307145 printk(MPT2SAS_INFO_FMT "\tAFTER adding expander: "
7146 "handle (0x%04x), sas_addr(0x%016llx)\n",
7147 ioc->name, handle, (unsigned long long)
7148 le64_to_cpu(expander_pg0.SASAddress));
7149 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307150 }
7151
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307152 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders complete\n",
7153 ioc->name);
7154
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307155 if (!ioc->ir_firmware)
7156 goto skip_to_sas;
7157
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307158 printk(MPT2SAS_INFO_FMT "\tscan devices phys disk start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307159 /* phys disk */
7160 phys_disk_num = 0xFF;
7161 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7162 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7163 phys_disk_num))) {
7164 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7165 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307166 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7167 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan:"
7168 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7169 ioc->name, ioc_status,
7170 le32_to_cpu(mpi_reply.IOCLogInfo));
7171 break;
7172 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307173 phys_disk_num = pd_pg0.PhysDiskNum;
7174 handle = le16_to_cpu(pd_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307175 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307176 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307177 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307178 if (sas_device)
7179 continue;
7180 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7181 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7182 handle) != 0)
7183 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307184 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7185 MPI2_IOCSTATUS_MASK;
7186 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7187 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan "
7188 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7189 ioc->name, ioc_status,
7190 le32_to_cpu(mpi_reply.IOCLogInfo));
7191 break;
7192 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307193 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7194 if (!_scsih_get_sas_address(ioc, parent_handle,
7195 &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307196 printk(MPT2SAS_INFO_FMT "\tBEFORE adding phys disk: "
7197 " handle (0x%04x), sas_addr(0x%016llx)\n",
7198 ioc->name, handle, (unsigned long long)
7199 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307200 mpt2sas_transport_update_links(ioc, sas_address,
7201 handle, sas_device_pg0.PhyNum,
7202 MPI2_SAS_NEG_LINK_RATE_1_5);
7203 set_bit(handle, ioc->pd_handles);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307204 retry_count = 0;
7205 /* This will retry adding the end device.
7206 * _scsih_add_device() will decide on retries and
7207 * return "1" when it should be retried
7208 */
7209 while (_scsih_add_device(ioc, handle, retry_count++,
7210 1)) {
7211 ssleep(1);
7212 }
7213 printk(MPT2SAS_INFO_FMT "\tAFTER adding phys disk: "
7214 " handle (0x%04x), sas_addr(0x%016llx)\n",
7215 ioc->name, handle, (unsigned long long)
7216 le64_to_cpu(sas_device_pg0.SASAddress));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307217 }
7218 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307219
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307220 printk(MPT2SAS_INFO_FMT "\tscan devices: phys disk complete\n",
7221 ioc->name);
7222
7223 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307224 /* volumes */
7225 handle = 0xFFFF;
7226 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7227 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7228 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7229 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307230 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7231 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7232 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7233 ioc->name, ioc_status,
7234 le32_to_cpu(mpi_reply.IOCLogInfo));
7235 break;
7236 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307237 handle = le16_to_cpu(volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307238 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307239 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7240 le64_to_cpu(volume_pg1.WWID));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307241 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307242 if (raid_device)
7243 continue;
7244 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7245 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7246 sizeof(Mpi2RaidVolPage0_t)))
7247 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307248 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7249 MPI2_IOCSTATUS_MASK;
7250 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7251 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7252 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7253 ioc->name, ioc_status,
7254 le32_to_cpu(mpi_reply.IOCLogInfo));
7255 break;
7256 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307257 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7258 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7259 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7260 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7261 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7262 element.VolDevHandle = volume_pg1.DevHandle;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307263 printk(MPT2SAS_INFO_FMT "\tBEFORE adding volume: "
7264 " handle (0x%04x)\n", ioc->name,
7265 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307266 _scsih_sas_volume_add(ioc, &element);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307267 printk(MPT2SAS_INFO_FMT "\tAFTER adding volume: "
7268 " handle (0x%04x)\n", ioc->name,
7269 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307270 }
7271 }
7272
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307273 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes complete\n",
7274 ioc->name);
7275
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307276 skip_to_sas:
7277
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307278 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices start\n",
7279 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307280 /* sas devices */
7281 handle = 0xFFFF;
7282 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7283 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7284 handle))) {
7285 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7286 MPI2_IOCSTATUS_MASK;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307287 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7288 printk(MPT2SAS_INFO_FMT "\tbreak from end device scan:"
7289 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7290 ioc->name, ioc_status,
7291 le32_to_cpu(mpi_reply.IOCLogInfo));
7292 break;
7293 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307294 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7295 if (!(_scsih_is_end_device(
7296 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7297 continue;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307298 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307299 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7300 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307301 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307302 if (sas_device)
7303 continue;
7304 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7305 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307306 printk(MPT2SAS_INFO_FMT "\tBEFORE adding end device: "
7307 "handle (0x%04x), sas_addr(0x%016llx)\n",
7308 ioc->name, handle, (unsigned long long)
7309 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307310 mpt2sas_transport_update_links(ioc, sas_address, handle,
7311 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307312 retry_count = 0;
7313 /* This will retry adding the end device.
7314 * _scsih_add_device() will decide on retries and
7315 * return "1" when it should be retried
7316 */
7317 while (_scsih_add_device(ioc, handle, retry_count++,
7318 0)) {
7319 ssleep(1);
7320 }
7321 printk(MPT2SAS_INFO_FMT "\tAFTER adding end device: "
7322 "handle (0x%04x), sas_addr(0x%016llx)\n",
7323 ioc->name, handle, (unsigned long long)
7324 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307325 }
7326 }
7327
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307328 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices complete\n",
7329 ioc->name);
7330
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307331 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307332}
7333
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307334
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307335/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307336 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7337 * @ioc: per adapter object
7338 * @reset_phase: phase
7339 *
7340 * The handler for doing any required cleanup or initialization.
7341 *
7342 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7343 * MPT2_IOC_DONE_RESET
7344 *
7345 * Return nothing.
7346 */
7347void
7348mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7349{
7350 switch (reset_phase) {
7351 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307352 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307353 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307354 break;
7355 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307356 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307357 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307358 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7359 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7360 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7361 complete(&ioc->scsih_cmds.done);
7362 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307363 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7364 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7365 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7366 complete(&ioc->tm_cmds.done);
7367 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307368 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307369 _scsih_flush_running_cmds(ioc);
7370 break;
7371 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307372 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307373 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307374 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307375 _scsih_prep_device_scan(ioc);
7376 _scsih_search_responding_sas_devices(ioc);
7377 _scsih_search_responding_raid_devices(ioc);
7378 _scsih_search_responding_expanders(ioc);
sreekanth.reddy@lsi.com14aa7f72012-07-17 15:56:13 +05307379 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7380 !ioc->sas_hba.num_phys)) {
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307381 _scsih_prep_device_scan(ioc);
7382 _scsih_search_responding_sas_devices(ioc);
7383 _scsih_search_responding_raid_devices(ioc);
7384 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307385 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307386 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307387 break;
Eric Moore635374e2009-03-09 01:21:12 -06007388 }
7389}
7390
7391/**
7392 * _firmware_event_work - delayed task for processing firmware events
7393 * @ioc: per adapter object
7394 * @work: equal to the fw_event_work object
7395 * Context: user.
7396 *
7397 * Return nothing.
7398 */
7399static void
7400_firmware_event_work(struct work_struct *work)
7401{
7402 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307403 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007404 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7405
Eric Moore635374e2009-03-09 01:21:12 -06007406 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007407 if (ioc->remove_host || fw_event->cancel_pending_work ||
7408 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007409 _scsih_fw_event_free(ioc, fw_event);
7410 return;
7411 }
Eric Moore635374e2009-03-09 01:21:12 -06007412
Eric Moore635374e2009-03-09 01:21:12 -06007413 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307414 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307415 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307416 ssleep(1);
7417 _scsih_remove_unresponding_sas_devices(ioc);
7418 _scsih_scan_for_devices_after_reset(ioc);
7419 break;
7420 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307421 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307422
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +05307423 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7424 mpt2sas_base_update_missing_delay(ioc, missing_delay[0],
7425 missing_delay[1]);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307426
7427 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7428 "from worker thread\n", ioc->name));
7429 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307430 case MPT2SAS_TURN_ON_FAULT_LED:
7431 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7432 break;
Eric Moore635374e2009-03-09 01:21:12 -06007433 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307434 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007435 break;
7436 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307437 _scsih_sas_device_status_change_event(ioc,
7438 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007439 break;
7440 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307441 _scsih_sas_discovery_event(ioc,
7442 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007443 break;
7444 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Andrew Mortona78e21d2012-02-08 12:52:22 -08007445 _scsih_sas_broadcast_primitive_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307446 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007447 break;
7448 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7449 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307450 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007451 break;
7452 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307453 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007454 break;
7455 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307456 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007457 break;
7458 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307459 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007460 break;
7461 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307462 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007463 break;
Eric Moore635374e2009-03-09 01:21:12 -06007464 }
7465 _scsih_fw_event_free(ioc, fw_event);
7466}
7467
7468/**
7469 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7470 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307471 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007472 * @reply: reply message frame(lower 32bit addr)
7473 * Context: interrupt.
7474 *
7475 * This function merely adds a new work task into ioc->firmware_event_thread.
7476 * The tasks are worked from _firmware_event_work in user context.
7477 *
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307478 * Returns void.
Eric Moore635374e2009-03-09 01:21:12 -06007479 */
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307480void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307481mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7482 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007483{
7484 struct fw_event_work *fw_event;
7485 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007486 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307487 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007488
7489 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007490 if (ioc->remove_host || ioc->pci_error_recovery)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307491 return;
Eric Moore635374e2009-03-09 01:21:12 -06007492
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307493 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05307494
7495 if (unlikely(!mpi_reply)) {
7496 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7497 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307498 return;
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05307499 }
7500
Eric Moore635374e2009-03-09 01:21:12 -06007501 event = le16_to_cpu(mpi_reply->Event);
7502
7503 switch (event) {
7504 /* handle these */
7505 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7506 {
7507 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7508 (Mpi2EventDataSasBroadcastPrimitive_t *)
7509 mpi_reply->EventData;
7510
7511 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307512 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307513 return;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307514
7515 if (ioc->broadcast_aen_busy) {
7516 ioc->broadcast_aen_pending++;
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307517 return;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307518 } else
7519 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007520 break;
7521 }
7522
7523 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7524 _scsih_check_topo_delete_events(ioc,
7525 (Mpi2EventDataSasTopologyChangeList_t *)
7526 mpi_reply->EventData);
7527 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307528 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7529 _scsih_check_ir_config_unhide_events(ioc,
7530 (Mpi2EventDataIrConfigChangeList_t *)
7531 mpi_reply->EventData);
7532 break;
7533 case MPI2_EVENT_IR_VOLUME:
7534 _scsih_check_volume_delete_events(ioc,
7535 (Mpi2EventDataIrVolume_t *)
7536 mpi_reply->EventData);
7537 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307538 case MPI2_EVENT_LOG_ENTRY_ADDED:
7539 {
7540 Mpi2EventDataLogEntryAdded_t *log_entry;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307541 __le32 *log_code;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307542
7543 if (!ioc->is_warpdrive)
7544 break;
7545
7546 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7547 mpi_reply->EventData;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307548 log_code = (__le32 *)log_entry->LogData;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307549
7550 if (le16_to_cpu(log_entry->LogEntryQualifier)
7551 != MPT2_WARPDRIVE_LOGENTRY)
7552 break;
7553
7554 switch (le32_to_cpu(*log_code)) {
7555 case MPT2_WARPDRIVE_LC_SSDT:
7556 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7557 "IO Throttling has occurred in the WarpDrive "
7558 "subsystem. Check WarpDrive documentation for "
7559 "additional details.\n", ioc->name);
7560 break;
7561 case MPT2_WARPDRIVE_LC_SSDLW:
7562 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7563 "Program/Erase Cycles for the WarpDrive subsystem "
7564 "in degraded range. Check WarpDrive documentation "
7565 "for additional details.\n", ioc->name);
7566 break;
7567 case MPT2_WARPDRIVE_LC_SSDLF:
7568 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7569 "There are no Program/Erase Cycles for the "
7570 "WarpDrive subsystem. The storage device will be "
7571 "in read-only mode. Check WarpDrive documentation "
7572 "for additional details.\n", ioc->name);
7573 break;
7574 case MPT2_WARPDRIVE_LC_BRMF:
7575 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7576 "The Backup Rail Monitor has failed on the "
7577 "WarpDrive subsystem. Check WarpDrive "
7578 "documentation for additional details.\n",
7579 ioc->name);
7580 break;
7581 }
7582
7583 break;
7584 }
Eric Moore635374e2009-03-09 01:21:12 -06007585 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7586 case MPI2_EVENT_IR_OPERATION_STATUS:
7587 case MPI2_EVENT_SAS_DISCOVERY:
7588 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007589 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007590 break;
7591
7592 default: /* ignore the rest */
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307593 return;
Eric Moore635374e2009-03-09 01:21:12 -06007594 }
7595
7596 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7597 if (!fw_event) {
7598 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7599 ioc->name, __FILE__, __LINE__, __func__);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307600 return;
Eric Moore635374e2009-03-09 01:21:12 -06007601 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307602 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7603 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007604 if (!fw_event->event_data) {
7605 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7606 ioc->name, __FILE__, __LINE__, __func__);
7607 kfree(fw_event);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307608 return;
Eric Moore635374e2009-03-09 01:21:12 -06007609 }
7610
7611 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307612 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007613 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307614 fw_event->VF_ID = mpi_reply->VF_ID;
7615 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007616 fw_event->event = event;
7617 _scsih_fw_event_add(ioc, fw_event);
Sreekanth Reddy6409a7d2013-07-25 11:24:35 +05307618 return;
Eric Moore635374e2009-03-09 01:21:12 -06007619}
7620
7621/* shost template */
7622static struct scsi_host_template scsih_driver_template = {
7623 .module = THIS_MODULE,
7624 .name = "Fusion MPT SAS Host",
7625 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007626 .queuecommand = _scsih_qcmd,
7627 .target_alloc = _scsih_target_alloc,
7628 .slave_alloc = _scsih_slave_alloc,
7629 .slave_configure = _scsih_slave_configure,
7630 .target_destroy = _scsih_target_destroy,
7631 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307632 .scan_finished = _scsih_scan_finished,
7633 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007634 .change_queue_depth = _scsih_change_queue_depth,
7635 .change_queue_type = _scsih_change_queue_type,
7636 .eh_abort_handler = _scsih_abort,
7637 .eh_device_reset_handler = _scsih_dev_reset,
7638 .eh_target_reset_handler = _scsih_target_reset,
7639 .eh_host_reset_handler = _scsih_host_reset,
7640 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007641 .can_queue = 1,
7642 .this_id = -1,
7643 .sg_tablesize = MPT2SAS_SG_DEPTH,
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05307644 .max_sectors = 32767,
Eric Moore635374e2009-03-09 01:21:12 -06007645 .cmd_per_lun = 7,
7646 .use_clustering = ENABLE_CLUSTERING,
7647 .shost_attrs = mpt2sas_host_attrs,
7648 .sdev_attrs = mpt2sas_dev_attrs,
7649};
7650
7651/**
7652 * _scsih_expander_node_remove - removing expander device from list.
7653 * @ioc: per adapter object
7654 * @sas_expander: the sas_device object
7655 * Context: Calling function should acquire ioc->sas_node_lock.
7656 *
7657 * Removing object and freeing associated memory from the
7658 * ioc->sas_expander_list.
7659 *
7660 * Return nothing.
7661 */
7662static void
7663_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7664 struct _sas_node *sas_expander)
7665{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307666 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007667
7668 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307669 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007670 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307671 if (ioc->shost_recovery)
7672 return;
Eric Moore635374e2009-03-09 01:21:12 -06007673 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307674 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307675 mpt2sas_device_remove_by_sas_address(ioc,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307676 mpt2sas_port->remote_identify.sas_address);
7677 else if (mpt2sas_port->remote_identify.device_type ==
7678 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007679 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307680 SAS_FANOUT_EXPANDER_DEVICE)
7681 mpt2sas_expander_remove(ioc,
7682 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007683 }
7684
7685 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307686 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007687
7688 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7689 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7690 sas_expander->handle, (unsigned long long)
7691 sas_expander->sas_address);
7692
Eric Moore635374e2009-03-09 01:21:12 -06007693 kfree(sas_expander->phy);
7694 kfree(sas_expander);
7695}
7696
7697/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307698 * _scsih_ir_shutdown - IR shutdown notification
7699 * @ioc: per adapter object
7700 *
7701 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7702 * the host system is shutting down.
7703 *
7704 * Return nothing.
7705 */
7706static void
7707_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7708{
7709 Mpi2RaidActionRequest_t *mpi_request;
7710 Mpi2RaidActionReply_t *mpi_reply;
7711 u16 smid;
7712
7713 /* is IR firmware build loaded ? */
7714 if (!ioc->ir_firmware)
7715 return;
7716
Kashyap, Desai744090d2009-10-05 15:56:56 +05307717 mutex_lock(&ioc->scsih_cmds.mutex);
7718
7719 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7720 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7721 ioc->name, __func__);
7722 goto out;
7723 }
7724 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7725
7726 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7727 if (!smid) {
7728 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7729 ioc->name, __func__);
7730 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7731 goto out;
7732 }
7733
7734 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7735 ioc->scsih_cmds.smid = smid;
7736 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7737
7738 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7739 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7740
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307741 if (!ioc->hide_ir_msg)
7742 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307743 init_completion(&ioc->scsih_cmds.done);
7744 mpt2sas_base_put_smid_default(ioc, smid);
7745 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7746
7747 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7748 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7749 ioc->name, __func__);
7750 goto out;
7751 }
7752
7753 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7754 mpi_reply = ioc->scsih_cmds.reply;
7755
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307756 if (!ioc->hide_ir_msg)
7757 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7758 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7759 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7760 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307761 }
7762
7763 out:
7764 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7765 mutex_unlock(&ioc->scsih_cmds.mutex);
7766}
7767
7768/**
7769 * _scsih_shutdown - routine call during system shutdown
7770 * @pdev: PCI device struct
7771 *
7772 * Return nothing.
7773 */
7774static void
7775_scsih_shutdown(struct pci_dev *pdev)
7776{
7777 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7778 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307779 struct workqueue_struct *wq;
7780 unsigned long flags;
7781
7782 ioc->remove_host = 1;
7783 _scsih_fw_event_cleanup_queue(ioc);
7784
7785 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7786 wq = ioc->firmware_event_thread;
7787 ioc->firmware_event_thread = NULL;
7788 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7789 if (wq)
7790 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307791
7792 _scsih_ir_shutdown(ioc);
7793 mpt2sas_base_detach(ioc);
7794}
7795
7796/**
Eric Moored5d135b2009-05-18 13:02:08 -06007797 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007798 * @pdev: PCI device struct
7799 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307800 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007801 * Return nothing.
7802 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007803static void
Eric Moored5d135b2009-05-18 13:02:08 -06007804_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007805{
7806 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7807 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307808 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307809 struct _raid_device *raid_device, *next;
7810 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007811 struct workqueue_struct *wq;
7812 unsigned long flags;
7813
7814 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307815 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007816
7817 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7818 wq = ioc->firmware_event_thread;
7819 ioc->firmware_event_thread = NULL;
7820 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7821 if (wq)
7822 destroy_workqueue(wq);
7823
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307824 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307825 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307826 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7827 list) {
7828 if (raid_device->starget) {
7829 sas_target_priv_data =
7830 raid_device->starget->hostdata;
7831 sas_target_priv_data->deleted = 1;
7832 scsi_remove_target(&raid_device->starget->dev);
7833 }
7834 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7835 "(0x%016llx)\n", ioc->name, raid_device->handle,
7836 (unsigned long long) raid_device->wwid);
7837 _scsih_raid_device_remove(ioc, raid_device);
7838 }
7839
Eric Moore635374e2009-03-09 01:21:12 -06007840 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307841 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007842 &ioc->sas_hba.sas_port_list, port_list) {
7843 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307844 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307845 mpt2sas_device_remove_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007846 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307847 else if (mpt2sas_port->remote_identify.device_type ==
7848 SAS_EDGE_EXPANDER_DEVICE ||
7849 mpt2sas_port->remote_identify.device_type ==
7850 SAS_FANOUT_EXPANDER_DEVICE)
7851 mpt2sas_expander_remove(ioc,
7852 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007853 }
7854
7855 /* free phys attached to the sas_host */
7856 if (ioc->sas_hba.num_phys) {
7857 kfree(ioc->sas_hba.phy);
7858 ioc->sas_hba.phy = NULL;
7859 ioc->sas_hba.num_phys = 0;
7860 }
7861
7862 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307863 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007864 list_del(&ioc->list);
7865 scsi_remove_host(shost);
7866 scsi_host_put(shost);
7867}
7868
7869/**
7870 * _scsih_probe_boot_devices - reports 1st device
7871 * @ioc: per adapter object
7872 *
7873 * If specified in bios page 2, this routine reports the 1st
7874 * device scsi-ml or sas transport for persistent boot device
7875 * purposes. Please refer to function _scsih_determine_boot_device()
7876 */
7877static void
7878_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7879{
7880 u8 is_raid;
7881 void *device;
7882 struct _sas_device *sas_device;
7883 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307884 u16 handle;
7885 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007886 u64 sas_address;
7887 unsigned long flags;
7888 int rc;
7889
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307890 /* no Bios, return immediately */
7891 if (!ioc->bios_pg3.BiosVersion)
7892 return;
7893
Eric Moore635374e2009-03-09 01:21:12 -06007894 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307895 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007896 if (ioc->req_boot_device.device) {
7897 device = ioc->req_boot_device.device;
7898 is_raid = ioc->req_boot_device.is_raid;
7899 } else if (ioc->req_alt_boot_device.device) {
7900 device = ioc->req_alt_boot_device.device;
7901 is_raid = ioc->req_alt_boot_device.is_raid;
7902 } else if (ioc->current_boot_device.device) {
7903 device = ioc->current_boot_device.device;
7904 is_raid = ioc->current_boot_device.is_raid;
7905 }
7906
7907 if (!device)
7908 return;
7909
7910 if (is_raid) {
7911 raid_device = device;
7912 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7913 raid_device->id, 0);
7914 if (rc)
7915 _scsih_raid_device_remove(ioc, raid_device);
7916 } else {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307917 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007918 sas_device = device;
7919 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307920 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007921 sas_address = sas_device->sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06007922 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7923 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307924
7925 if (ioc->hide_drives)
7926 return;
Eric Moore635374e2009-03-09 01:21:12 -06007927 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307928 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007929 _scsih_sas_device_remove(ioc, sas_device);
7930 } else if (!sas_device->starget) {
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307931 if (!ioc->is_driver_loading) {
7932 mpt2sas_transport_port_remove(ioc,
7933 sas_address,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307934 sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307935 _scsih_sas_device_remove(ioc, sas_device);
7936 }
Eric Moore635374e2009-03-09 01:21:12 -06007937 }
7938 }
7939}
7940
7941/**
7942 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7943 * @ioc: per adapter object
7944 *
7945 * Called during initial loading of the driver.
7946 */
7947static void
7948_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7949{
7950 struct _raid_device *raid_device, *raid_next;
7951 int rc;
7952
7953 list_for_each_entry_safe(raid_device, raid_next,
7954 &ioc->raid_device_list, list) {
7955 if (raid_device->starget)
7956 continue;
7957 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7958 raid_device->id, 0);
7959 if (rc)
7960 _scsih_raid_device_remove(ioc, raid_device);
7961 }
7962}
7963
7964/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307965 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007966 * @ioc: per adapter object
7967 *
7968 * Called during initial loading of the driver.
7969 */
7970static void
7971_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7972{
7973 struct _sas_device *sas_device, *next;
7974 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007975
7976 /* SAS Device List */
7977 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7978 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007979
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307980 if (ioc->hide_drives)
7981 continue;
7982
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307983 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7984 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307985 list_del(&sas_device->list);
7986 kfree(sas_device);
7987 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007988 } else if (!sas_device->starget) {
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307989 if (!ioc->is_driver_loading) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307990 mpt2sas_transport_port_remove(ioc,
7991 sas_device->sas_address,
7992 sas_device->sas_address_parent);
Sreekanth Reddybc6d4c32013-07-25 11:31:12 +05307993 list_del(&sas_device->list);
7994 kfree(sas_device);
7995 continue;
7996 }
Eric Moore635374e2009-03-09 01:21:12 -06007997 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307998 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7999 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8000 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06008001 }
8002}
8003
8004/**
8005 * _scsih_probe_devices - probing for devices
8006 * @ioc: per adapter object
8007 *
8008 * Called during initial loading of the driver.
8009 */
8010static void
8011_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
8012{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308013 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06008014
8015 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8016 return; /* return when IOC doesn't support initiator mode */
8017
8018 _scsih_probe_boot_devices(ioc);
8019
8020 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308021 volume_mapping_flags =
8022 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8023 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8024 if (volume_mapping_flags ==
8025 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06008026 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308027 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008028 } else {
Eric Moore635374e2009-03-09 01:21:12 -06008029 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308030 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008031 }
8032 } else
8033 _scsih_probe_sas(ioc);
8034}
8035
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308036
8037/**
8038 * _scsih_scan_start - scsi lld callback for .scan_start
8039 * @shost: SCSI host pointer
8040 *
8041 * The shost has the ability to discover targets on its own instead
8042 * of scanning the entire bus. In our implemention, we will kick off
8043 * firmware discovery.
8044 */
8045static void
8046_scsih_scan_start(struct Scsi_Host *shost)
8047{
8048 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8049 int rc;
8050
8051 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8052 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
8053
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308054 if (disable_discovery > 0)
8055 return;
8056
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308057 ioc->start_scan = 1;
8058 rc = mpt2sas_port_enable(ioc);
8059
8060 if (rc != 0)
8061 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
8062}
8063
8064/**
8065 * _scsih_scan_finished - scsi lld callback for .scan_finished
8066 * @shost: SCSI host pointer
8067 * @time: elapsed time of the scan in jiffies
8068 *
8069 * This function will be called periodically until it returns 1 with the
8070 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8071 * we wait for firmware discovery to complete, then return 1.
8072 */
8073static int
8074_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8075{
8076 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8077
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308078 if (disable_discovery > 0) {
8079 ioc->is_driver_loading = 0;
8080 ioc->wait_for_discovery_to_complete = 0;
8081 return 1;
8082 }
8083
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308084 if (time >= (300 * HZ)) {
8085 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8086 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
8087 "(timeout=300s)\n", ioc->name);
8088 ioc->is_driver_loading = 0;
8089 return 1;
8090 }
8091
8092 if (ioc->start_scan)
8093 return 0;
8094
8095 if (ioc->start_scan_failed) {
8096 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
8097 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
8098 ioc->is_driver_loading = 0;
8099 ioc->wait_for_discovery_to_complete = 0;
8100 ioc->remove_host = 1;
8101 return 1;
8102 }
8103
8104 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
8105 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8106
8107 if (ioc->wait_for_discovery_to_complete) {
8108 ioc->wait_for_discovery_to_complete = 0;
8109 _scsih_probe_devices(ioc);
8110 }
8111 mpt2sas_base_start_watchdog(ioc);
8112 ioc->is_driver_loading = 0;
8113 return 1;
8114}
8115
8116
Eric Moore635374e2009-03-09 01:21:12 -06008117/**
Eric Moored5d135b2009-05-18 13:02:08 -06008118 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06008119 * @pdev: PCI device struct
8120 * @id: pci device id
8121 *
8122 * Returns 0 success, anything else error.
8123 */
8124static int
Eric Moored5d135b2009-05-18 13:02:08 -06008125_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06008126{
8127 struct MPT2SAS_ADAPTER *ioc;
8128 struct Scsi_Host *shost;
8129
8130 shost = scsi_host_alloc(&scsih_driver_template,
8131 sizeof(struct MPT2SAS_ADAPTER));
8132 if (!shost)
8133 return -ENODEV;
8134
8135 /* init local params */
8136 ioc = shost_priv(shost);
8137 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
8138 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06008139 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06008140 ioc->shost = shost;
8141 ioc->id = mpt_ids++;
8142 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
8143 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308144 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8145 ioc->is_warpdrive = 1;
8146 ioc->hide_ir_msg = 1;
8147 } else
8148 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06008149 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8150 ioc->tm_cb_idx = tm_cb_idx;
8151 ioc->ctl_cb_idx = ctl_cb_idx;
8152 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308153 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008154 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05308155 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008156 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308157 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308158 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308159 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008160 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05308161 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06008162 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05308163 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06008164 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8165 spin_lock_init(&ioc->scsi_lookup_lock);
8166 spin_lock_init(&ioc->sas_device_lock);
8167 spin_lock_init(&ioc->sas_node_lock);
8168 spin_lock_init(&ioc->fw_event_lock);
8169 spin_lock_init(&ioc->raid_device_lock);
8170
8171 INIT_LIST_HEAD(&ioc->sas_device_list);
8172 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8173 INIT_LIST_HEAD(&ioc->sas_expander_list);
8174 INIT_LIST_HEAD(&ioc->fw_event_list);
8175 INIT_LIST_HEAD(&ioc->raid_device_list);
8176 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308177 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308178 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Joe Lawrence3b3e6f82013-08-08 16:45:38 -04008179 INIT_LIST_HEAD(&ioc->reply_queue_list);
Eric Moore635374e2009-03-09 01:21:12 -06008180
8181 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06008182 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06008183 shost->max_lun = max_lun;
8184 shost->transportt = mpt2sas_transport_template;
8185 shost->unique_id = ioc->id;
8186
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308187 if (max_sectors != 0xFFFF) {
8188 if (max_sectors < 64) {
8189 shost->max_sectors = 64;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05308190 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8191 "for max_sectors, range is 64 to 32767. Assigning "
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308192 "value of 64.\n", ioc->name, max_sectors);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308193 } else if (max_sectors > 32767) {
8194 shost->max_sectors = 32767;
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308195 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8196 "for max_sectors, range is 64 to 8192. Assigning "
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308197 "default value of 32767.\n", ioc->name,
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308198 max_sectors);
8199 } else {
8200 shost->max_sectors = max_sectors & 0xFFFE;
8201 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
8202 "set to %d\n", ioc->name, shost->max_sectors);
8203 }
8204 }
8205
Eric Moore635374e2009-03-09 01:21:12 -06008206 if ((scsi_add_host(shost, &pdev->dev))) {
8207 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8208 ioc->name, __FILE__, __LINE__, __func__);
8209 list_del(&ioc->list);
8210 goto out_add_shost_fail;
8211 }
8212
Martin K. Petersen5e95e732012-08-28 14:29:37 -04008213 /* register EEDP capabilities with SCSI layer */
8214 if (prot_mask)
8215 scsi_host_set_prot(shost, prot_mask);
8216 else
8217 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8218 | SHOST_DIF_TYPE2_PROTECTION
8219 | SHOST_DIF_TYPE3_PROTECTION);
8220
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308221 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06008222
Eric Moore635374e2009-03-09 01:21:12 -06008223 /* event thread */
8224 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8225 "fw_event%d", ioc->id);
8226 ioc->firmware_event_thread = create_singlethread_workqueue(
8227 ioc->firmware_event_name);
8228 if (!ioc->firmware_event_thread) {
8229 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8230 ioc->name, __FILE__, __LINE__, __func__);
8231 goto out_thread_fail;
8232 }
8233
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308234 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008235 if ((mpt2sas_base_attach(ioc))) {
8236 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8237 ioc->name, __FILE__, __LINE__, __func__);
8238 goto out_attach_fail;
8239 }
8240
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308241 if (ioc->is_warpdrive) {
8242 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8243 ioc->hide_drives = 0;
8244 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8245 ioc->hide_drives = 1;
8246 else {
8247 if (_scsih_get_num_volumes(ioc))
8248 ioc->hide_drives = 1;
8249 else
8250 ioc->hide_drives = 0;
8251 }
8252 } else
8253 ioc->hide_drives = 0;
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05308254 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308255
Eric Moore635374e2009-03-09 01:21:12 -06008256 return 0;
8257
8258 out_attach_fail:
8259 destroy_workqueue(ioc->firmware_event_thread);
8260 out_thread_fail:
8261 list_del(&ioc->list);
8262 scsi_remove_host(shost);
8263 out_add_shost_fail:
Tomas Henzlf555e052012-04-17 15:24:48 +02008264 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008265 return -ENODEV;
8266}
8267
8268#ifdef CONFIG_PM
8269/**
Eric Moored5d135b2009-05-18 13:02:08 -06008270 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008271 * @pdev: PCI device struct
8272 * @state: PM state change to (usually PCI_D3)
8273 *
8274 * Returns 0 success, anything else error.
8275 */
8276static int
Eric Moored5d135b2009-05-18 13:02:08 -06008277_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008278{
8279 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8280 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308281 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008282
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308283 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008284 scsi_block_requests(shost);
Sreekanth Reddy3627dba2013-07-25 11:25:57 +05308285 _scsih_ir_shutdown(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008286 device_state = pci_choose_state(pdev, state);
8287 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8288 "operating state [D%d]\n", ioc->name, pdev,
8289 pci_name(pdev), device_state);
8290
8291 mpt2sas_base_free_resources(ioc);
8292 pci_save_state(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06008293 pci_set_power_state(pdev, device_state);
8294 return 0;
8295}
8296
8297/**
Eric Moored5d135b2009-05-18 13:02:08 -06008298 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008299 * @pdev: PCI device struct
8300 *
8301 * Returns 0 success, anything else error.
8302 */
8303static int
Eric Moored5d135b2009-05-18 13:02:08 -06008304_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008305{
8306 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8307 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308308 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008309 int r;
8310
8311 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8312 "operating state [D%d]\n", ioc->name, pdev,
8313 pci_name(pdev), device_state);
8314
8315 pci_set_power_state(pdev, PCI_D0);
8316 pci_enable_wake(pdev, PCI_D0, 0);
8317 pci_restore_state(pdev);
8318 ioc->pdev = pdev;
8319 r = mpt2sas_base_map_resources(ioc);
8320 if (r)
8321 return r;
8322
8323 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8324 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308325 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008326 return 0;
8327}
8328#endif /* CONFIG_PM */
8329
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308330/**
8331 * _scsih_pci_error_detected - Called when a PCI error is detected.
8332 * @pdev: PCI device struct
8333 * @state: PCI channel state
8334 *
8335 * Description: Called when a PCI error is detected.
8336 *
8337 * Return value:
8338 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8339 */
8340static pci_ers_result_t
8341_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8342{
8343 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8344 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8345
8346 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8347 ioc->name, state);
8348
8349 switch (state) {
8350 case pci_channel_io_normal:
8351 return PCI_ERS_RESULT_CAN_RECOVER;
8352 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008353 /* Fatal error, prepare for slot reset */
8354 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308355 scsi_block_requests(ioc->shost);
8356 mpt2sas_base_stop_watchdog(ioc);
8357 mpt2sas_base_free_resources(ioc);
8358 return PCI_ERS_RESULT_NEED_RESET;
8359 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008360 /* Permanent error, prepare for device removal */
8361 ioc->pci_error_recovery = 1;
8362 mpt2sas_base_stop_watchdog(ioc);
8363 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308364 return PCI_ERS_RESULT_DISCONNECT;
8365 }
8366 return PCI_ERS_RESULT_NEED_RESET;
8367}
8368
8369/**
8370 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8371 * @pdev: PCI device struct
8372 *
8373 * Description: This routine is called by the pci error recovery
8374 * code after the PCI slot has been reset, just before we
8375 * should resume normal operations.
8376 */
8377static pci_ers_result_t
8378_scsih_pci_slot_reset(struct pci_dev *pdev)
8379{
8380 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8381 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8382 int rc;
8383
8384 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8385 ioc->name);
8386
Eric Moore3cb54692010-07-08 14:44:34 -06008387 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308388 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008389 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308390 rc = mpt2sas_base_map_resources(ioc);
8391 if (rc)
8392 return PCI_ERS_RESULT_DISCONNECT;
8393
8394
8395 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8396 FORCE_BIG_HAMMER);
8397
8398 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8399 (rc == 0) ? "success" : "failed");
8400
8401 if (!rc)
8402 return PCI_ERS_RESULT_RECOVERED;
8403 else
8404 return PCI_ERS_RESULT_DISCONNECT;
8405}
8406
8407/**
8408 * _scsih_pci_resume() - resume normal ops after PCI reset
8409 * @pdev: pointer to PCI device
8410 *
8411 * Called when the error recovery driver tells us that its
8412 * OK to resume normal operation. Use completion to allow
8413 * halted scsi ops to resume.
8414 */
8415static void
8416_scsih_pci_resume(struct pci_dev *pdev)
8417{
8418 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8419 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8420
8421 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8422
8423 pci_cleanup_aer_uncorrect_error_status(pdev);
8424 mpt2sas_base_start_watchdog(ioc);
8425 scsi_unblock_requests(ioc->shost);
8426}
8427
8428/**
8429 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8430 * @pdev: pointer to PCI device
8431 */
8432static pci_ers_result_t
8433_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8434{
8435 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8436 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8437
8438 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8439 ioc->name);
8440
8441 /* TODO - dump whatever for debugging purposes */
8442
8443 /* Request a slot reset. */
8444 return PCI_ERS_RESULT_NEED_RESET;
8445}
8446
Stephen Hemmingera55b2d22012-09-07 09:33:16 -07008447static const struct pci_error_handlers _scsih_err_handler = {
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308448 .error_detected = _scsih_pci_error_detected,
8449 .mmio_enabled = _scsih_pci_mmio_enabled,
8450 .slot_reset = _scsih_pci_slot_reset,
8451 .resume = _scsih_pci_resume,
8452};
Eric Moore635374e2009-03-09 01:21:12 -06008453
8454static struct pci_driver scsih_driver = {
8455 .name = MPT2SAS_DRIVER_NAME,
8456 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008457 .probe = _scsih_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008458 .remove = _scsih_remove,
Kashyap, Desai744090d2009-10-05 15:56:56 +05308459 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308460 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008461#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008462 .suspend = _scsih_suspend,
8463 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008464#endif
8465};
8466
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308467/* raid transport support */
8468static struct raid_function_template mpt2sas_raid_functions = {
8469 .cookie = &scsih_driver_template,
8470 .is_raid = _scsih_is_raid,
8471 .get_resync = _scsih_get_resync,
8472 .get_state = _scsih_get_state,
8473};
Eric Moore635374e2009-03-09 01:21:12 -06008474
8475/**
Eric Moored5d135b2009-05-18 13:02:08 -06008476 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008477 *
8478 * Returns 0 success, anything else error.
8479 */
8480static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008481_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008482{
8483 int error;
8484
8485 mpt_ids = 0;
8486 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8487 MPT2SAS_DRIVER_VERSION);
8488
8489 mpt2sas_transport_template =
8490 sas_attach_transport(&mpt2sas_transport_functions);
8491 if (!mpt2sas_transport_template)
8492 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308493 /* raid transport support */
8494 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8495 if (!mpt2sas_raid_template) {
8496 sas_release_transport(mpt2sas_transport_template);
8497 return -ENODEV;
8498 }
Eric Moore635374e2009-03-09 01:21:12 -06008499
8500 mpt2sas_base_initialize_callback_handler();
8501
8502 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008503 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008504
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008505 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008506 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008507
8508 /* base internal commands callback handler */
8509 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308510 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8511 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008512
8513 /* transport internal commands callback handler */
8514 transport_cb_idx = mpt2sas_base_register_callback_handler(
8515 mpt2sas_transport_done);
8516
Kashyap, Desai744090d2009-10-05 15:56:56 +05308517 /* scsih internal commands callback handler */
8518 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8519
Eric Moore635374e2009-03-09 01:21:12 -06008520 /* configuration page API internal commands callback handler */
8521 config_cb_idx = mpt2sas_base_register_callback_handler(
8522 mpt2sas_config_done);
8523
8524 /* ctl module callback handler */
8525 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8526
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308527 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8528 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308529
8530 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8531 _scsih_tm_volume_tr_complete);
8532
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308533 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8534 _scsih_sas_control_complete);
8535
Eric Moore635374e2009-03-09 01:21:12 -06008536 mpt2sas_ctl_init();
8537
8538 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308539 if (error) {
8540 /* raid transport support */
8541 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008542 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308543 }
Eric Moore635374e2009-03-09 01:21:12 -06008544
8545 return error;
8546}
8547
8548/**
Eric Moored5d135b2009-05-18 13:02:08 -06008549 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008550 *
8551 * Returns 0 success, anything else error.
8552 */
8553static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008554_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008555{
8556 printk(KERN_INFO "mpt2sas version %s unloading\n",
8557 MPT2SAS_DRIVER_VERSION);
8558
8559 pci_unregister_driver(&scsih_driver);
8560
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308561 mpt2sas_ctl_exit();
8562
Eric Moore635374e2009-03-09 01:21:12 -06008563 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8564 mpt2sas_base_release_callback_handler(tm_cb_idx);
8565 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308566 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008567 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308568 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008569 mpt2sas_base_release_callback_handler(config_cb_idx);
8570 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8571
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308572 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308573 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308574 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8575
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308576 /* raid transport support */
8577 raid_class_release(mpt2sas_raid_template);
8578 sas_release_transport(mpt2sas_transport_template);
8579
Eric Moore635374e2009-03-09 01:21:12 -06008580}
8581
Eric Moored5d135b2009-05-18 13:02:08 -06008582module_init(_scsih_init);
8583module_exit(_scsih_exit);