blob: 01dfbabc1c17c1ec52609f010edd63165d16e0bb [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.reddy@lsi.com433d7172012-07-17 15:56:03 +05305 * Copyright (C) 2007-2012 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 */
631 if (!ioc->is_driver_loading)
632 mpt2sas_transport_port_remove(ioc,
633 sas_device->sas_address,
634 sas_device->sas_address_parent);
635 _scsih_sas_device_remove(ioc, sas_device);
636 }
Eric Moore635374e2009-03-09 01:21:12 -0600637}
638
639/**
640 * _scsih_sas_device_init_add - insert sas_device to the list.
641 * @ioc: per adapter object
642 * @sas_device: the sas_device object
643 * Context: This function will acquire ioc->sas_device_lock.
644 *
645 * Adding new object at driver load time to the ioc->sas_device_init_list.
646 */
647static void
648_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
649 struct _sas_device *sas_device)
650{
651 unsigned long flags;
652
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530653 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600654 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
655 sas_device->handle, (unsigned long long)sas_device->sas_address));
656
657 spin_lock_irqsave(&ioc->sas_device_lock, flags);
658 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
Eric Moore635374e2009-03-09 01:21:12 -0600659 _scsih_determine_boot_device(ioc, sas_device, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530660 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600661}
662
663/**
Eric Moore635374e2009-03-09 01:21:12 -0600664 * _scsih_raid_device_find_by_id - raid device search
665 * @ioc: per adapter object
666 * @id: sas device target id
667 * @channel: sas device channel
668 * Context: Calling function should acquire ioc->raid_device_lock
669 *
670 * This searches for raid_device based on target id, then return raid_device
671 * object.
672 */
673static struct _raid_device *
674_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
675{
676 struct _raid_device *raid_device, *r;
677
678 r = NULL;
679 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
680 if (raid_device->id == id && raid_device->channel == channel) {
681 r = raid_device;
682 goto out;
683 }
684 }
685
686 out:
687 return r;
688}
689
690/**
691 * _scsih_raid_device_find_by_handle - raid device search
692 * @ioc: per adapter object
693 * @handle: sas device handle (assigned by firmware)
694 * Context: Calling function should acquire ioc->raid_device_lock
695 *
696 * This searches for raid_device based on handle, then return raid_device
697 * object.
698 */
699static struct _raid_device *
700_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
701{
702 struct _raid_device *raid_device, *r;
703
704 r = NULL;
705 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
706 if (raid_device->handle != handle)
707 continue;
708 r = raid_device;
709 goto out;
710 }
711
712 out:
713 return r;
714}
715
716/**
717 * _scsih_raid_device_find_by_wwid - raid device search
718 * @ioc: per adapter object
719 * @handle: sas device handle (assigned by firmware)
720 * Context: Calling function should acquire ioc->raid_device_lock
721 *
722 * This searches for raid_device based on wwid, then return raid_device
723 * object.
724 */
725static struct _raid_device *
726_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
727{
728 struct _raid_device *raid_device, *r;
729
730 r = NULL;
731 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
732 if (raid_device->wwid != wwid)
733 continue;
734 r = raid_device;
735 goto out;
736 }
737
738 out:
739 return r;
740}
741
742/**
743 * _scsih_raid_device_add - add raid_device object
744 * @ioc: per adapter object
745 * @raid_device: raid_device object
746 *
747 * This is added to the raid_device_list link list.
748 */
749static void
750_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
751 struct _raid_device *raid_device)
752{
753 unsigned long flags;
754
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530755 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600756 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
757 raid_device->handle, (unsigned long long)raid_device->wwid));
758
759 spin_lock_irqsave(&ioc->raid_device_lock, flags);
760 list_add_tail(&raid_device->list, &ioc->raid_device_list);
761 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
762}
763
764/**
765 * _scsih_raid_device_remove - delete raid_device object
766 * @ioc: per adapter object
767 * @raid_device: raid_device object
768 *
Eric Moore635374e2009-03-09 01:21:12 -0600769 */
770static void
771_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
772 struct _raid_device *raid_device)
773{
774 unsigned long flags;
775
776 spin_lock_irqsave(&ioc->raid_device_lock, flags);
777 list_del(&raid_device->list);
Eric Moore635374e2009-03-09 01:21:12 -0600778 kfree(raid_device);
779 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
780}
781
782/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530783 * mpt2sas_scsih_expander_find_by_handle - expander device search
784 * @ioc: per adapter object
785 * @handle: expander handle (assigned by firmware)
786 * Context: Calling function should acquire ioc->sas_device_lock
787 *
788 * This searches for expander device based on handle, then returns the
789 * sas_node object.
790 */
791struct _sas_node *
792mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
793{
794 struct _sas_node *sas_expander, *r;
795
796 r = NULL;
797 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
798 if (sas_expander->handle != handle)
799 continue;
800 r = sas_expander;
801 goto out;
802 }
803 out:
804 return r;
805}
806
807/**
Eric Moore635374e2009-03-09 01:21:12 -0600808 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
809 * @ioc: per adapter object
810 * @sas_address: sas address
811 * Context: Calling function should acquire ioc->sas_node_lock.
812 *
813 * This searches for expander device based on sas_address, then returns the
814 * sas_node object.
815 */
816struct _sas_node *
817mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
818 u64 sas_address)
819{
820 struct _sas_node *sas_expander, *r;
821
822 r = NULL;
823 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
824 if (sas_expander->sas_address != sas_address)
825 continue;
826 r = sas_expander;
827 goto out;
828 }
829 out:
830 return r;
831}
832
833/**
834 * _scsih_expander_node_add - insert expander device to the list.
835 * @ioc: per adapter object
836 * @sas_expander: the sas_device object
837 * Context: This function will acquire ioc->sas_node_lock.
838 *
839 * Adding new object to the ioc->sas_expander_list.
840 *
841 * Return nothing.
842 */
843static void
844_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
845 struct _sas_node *sas_expander)
846{
847 unsigned long flags;
848
849 spin_lock_irqsave(&ioc->sas_node_lock, flags);
850 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
851 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
852}
853
854/**
855 * _scsih_is_end_device - determines if device is an end device
856 * @device_info: bitfield providing information about the device.
857 * Context: none
858 *
859 * Returns 1 if end device.
860 */
861static int
862_scsih_is_end_device(u32 device_info)
863{
864 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
865 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
866 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
867 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
868 return 1;
869 else
870 return 0;
871}
872
873/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530874 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600875 * @ioc: per adapter object
876 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600877 *
878 * Returns the smid stored scmd pointer.
879 */
880static struct scsi_cmnd *
881_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
882{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530883 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600884}
885
886/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530887 * _scsih_scsi_lookup_get_clear - returns scmd entry
888 * @ioc: per adapter object
889 * @smid: system request message index
890 *
891 * Returns the smid stored scmd pointer.
892 * Then will derefrence the stored scmd pointer.
893 */
894static inline struct scsi_cmnd *
895_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
896{
897 unsigned long flags;
898 struct scsi_cmnd *scmd;
899
900 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
901 scmd = ioc->scsi_lookup[smid - 1].scmd;
902 ioc->scsi_lookup[smid - 1].scmd = NULL;
903 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
904
905 return scmd;
906}
907
908/**
Eric Moore635374e2009-03-09 01:21:12 -0600909 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
910 * @ioc: per adapter object
911 * @smid: system request message index
912 * @scmd: pointer to scsi command object
913 * Context: This function will acquire ioc->scsi_lookup_lock.
914 *
915 * This will search for a scmd pointer in the scsi_lookup array,
916 * returning the revelent smid. A returned value of zero means invalid.
917 */
918static u16
919_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
920 *scmd)
921{
922 u16 smid;
923 unsigned long flags;
924 int i;
925
926 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
927 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530928 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600929 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530930 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600931 goto out;
932 }
933 }
934 out:
935 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
936 return smid;
937}
938
939/**
940 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
941 * @ioc: per adapter object
942 * @id: target id
943 * @channel: channel
944 * Context: This function will acquire ioc->scsi_lookup_lock.
945 *
946 * This will search for a matching channel:id in the scsi_lookup array,
947 * returning 1 if found.
948 */
949static u8
950_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
951 int channel)
952{
953 u8 found;
954 unsigned long flags;
955 int i;
956
957 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
958 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530959 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600960 if (ioc->scsi_lookup[i].scmd &&
961 (ioc->scsi_lookup[i].scmd->device->id == id &&
962 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
963 found = 1;
964 goto out;
965 }
966 }
967 out:
968 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
969 return found;
970}
971
972/**
Eric Moore993e0da2009-05-18 13:00:45 -0600973 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
974 * @ioc: per adapter object
975 * @id: target id
976 * @lun: lun number
977 * @channel: channel
978 * Context: This function will acquire ioc->scsi_lookup_lock.
979 *
980 * This will search for a matching channel:id:lun in the scsi_lookup array,
981 * returning 1 if found.
982 */
983static u8
984_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
985 unsigned int lun, int channel)
986{
987 u8 found;
988 unsigned long flags;
989 int i;
990
991 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
992 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530993 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600994 if (ioc->scsi_lookup[i].scmd &&
995 (ioc->scsi_lookup[i].scmd->device->id == id &&
996 ioc->scsi_lookup[i].scmd->device->channel == channel &&
997 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
998 found = 1;
999 goto out;
1000 }
1001 }
1002 out:
1003 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1004 return found;
1005}
1006
1007/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301008 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -06001009 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301010 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -06001011 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301012 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001013 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301014static struct chain_tracker *
1015_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001016{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301017 struct chain_tracker *chain_req;
1018 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001019
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301020 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1021 if (list_empty(&ioc->free_chain_list)) {
1022 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05301023 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
1024 "available\n", ioc->name));
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301025 return NULL;
1026 }
1027 chain_req = list_entry(ioc->free_chain_list.next,
1028 struct chain_tracker, tracker_list);
1029 list_del_init(&chain_req->tracker_list);
1030 list_add_tail(&chain_req->tracker_list,
1031 &ioc->scsi_lookup[smid - 1].chain_list);
1032 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1033 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001034}
1035
1036/**
1037 * _scsih_build_scatter_gather - main sg creation routine
1038 * @ioc: per adapter object
1039 * @scmd: scsi command
1040 * @smid: system request message index
1041 * Context: none.
1042 *
1043 * The main routine that builds scatter gather table from a given
1044 * scsi request sent via the .queuecommand main handler.
1045 *
1046 * Returns 0 success, anything else error
1047 */
1048static int
1049_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1050 struct scsi_cmnd *scmd, u16 smid)
1051{
1052 Mpi2SCSIIORequest_t *mpi_request;
1053 dma_addr_t chain_dma;
1054 struct scatterlist *sg_scmd;
1055 void *sg_local, *chain;
1056 u32 chain_offset;
1057 u32 chain_length;
1058 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001059 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001060 u32 sges_in_segment;
1061 u32 sgl_flags;
1062 u32 sgl_flags_last_element;
1063 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301064 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001065
1066 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1067
1068 /* init scatter gather flags */
1069 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1070 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1071 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1072 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1073 << MPI2_SGE_FLAGS_SHIFT;
1074 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1075 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1076 << MPI2_SGE_FLAGS_SHIFT;
1077 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1078
1079 sg_scmd = scsi_sglist(scmd);
1080 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001081 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001082 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1083 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1084 return -ENOMEM;
1085 }
1086
1087 sg_local = &mpi_request->SGL;
1088 sges_in_segment = ioc->max_sges_in_main_message;
1089 if (sges_left <= sges_in_segment)
1090 goto fill_in_last_segment;
1091
1092 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1093 (sges_in_segment * ioc->sge_size))/4;
1094
1095 /* fill in main message segment when there is a chain following */
1096 while (sges_in_segment) {
1097 if (sges_in_segment == 1)
1098 ioc->base_add_sg_single(sg_local,
1099 sgl_flags_last_element | sg_dma_len(sg_scmd),
1100 sg_dma_address(sg_scmd));
1101 else
1102 ioc->base_add_sg_single(sg_local, sgl_flags |
1103 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1104 sg_scmd = sg_next(sg_scmd);
1105 sg_local += ioc->sge_size;
1106 sges_left--;
1107 sges_in_segment--;
1108 }
1109
1110 /* initializing the chain flags and pointers */
1111 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301112 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1113 if (!chain_req)
1114 return -1;
1115 chain = chain_req->chain_buffer;
1116 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001117 do {
1118 sges_in_segment = (sges_left <=
1119 ioc->max_sges_in_chain_message) ? sges_left :
1120 ioc->max_sges_in_chain_message;
1121 chain_offset = (sges_left == sges_in_segment) ?
1122 0 : (sges_in_segment * ioc->sge_size)/4;
1123 chain_length = sges_in_segment * ioc->sge_size;
1124 if (chain_offset) {
1125 chain_offset = chain_offset <<
1126 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1127 chain_length += ioc->sge_size;
1128 }
1129 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1130 chain_length, chain_dma);
1131 sg_local = chain;
1132 if (!chain_offset)
1133 goto fill_in_last_segment;
1134
1135 /* fill in chain segments */
1136 while (sges_in_segment) {
1137 if (sges_in_segment == 1)
1138 ioc->base_add_sg_single(sg_local,
1139 sgl_flags_last_element |
1140 sg_dma_len(sg_scmd),
1141 sg_dma_address(sg_scmd));
1142 else
1143 ioc->base_add_sg_single(sg_local, sgl_flags |
1144 sg_dma_len(sg_scmd),
1145 sg_dma_address(sg_scmd));
1146 sg_scmd = sg_next(sg_scmd);
1147 sg_local += ioc->sge_size;
1148 sges_left--;
1149 sges_in_segment--;
1150 }
1151
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301152 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1153 if (!chain_req)
1154 return -1;
1155 chain = chain_req->chain_buffer;
1156 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001157 } while (1);
1158
1159
1160 fill_in_last_segment:
1161
1162 /* fill the last segment */
1163 while (sges_left) {
1164 if (sges_left == 1)
1165 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1166 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1167 else
1168 ioc->base_add_sg_single(sg_local, sgl_flags |
1169 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1170 sg_scmd = sg_next(sg_scmd);
1171 sg_local += ioc->sge_size;
1172 sges_left--;
1173 }
1174
1175 return 0;
1176}
1177
1178/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301179 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001180 * @sdev: scsi device struct
1181 * @qdepth: requested queue depth
1182 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301183 *
1184 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001185 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301186static void
1187_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001188{
1189 struct Scsi_Host *shost = sdev->host;
1190 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301191 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1192 struct MPT2SAS_DEVICE *sas_device_priv_data;
1193 struct MPT2SAS_TARGET *sas_target_priv_data;
1194 struct _sas_device *sas_device;
1195 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001196
1197 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301198
1199 /* limit max device queue for SATA to 32 */
1200 sas_device_priv_data = sdev->hostdata;
1201 if (!sas_device_priv_data)
1202 goto not_sata;
1203 sas_target_priv_data = sas_device_priv_data->sas_target;
1204 if (!sas_target_priv_data)
1205 goto not_sata;
1206 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1207 goto not_sata;
1208 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1209 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1210 sas_device_priv_data->sas_target->sas_address);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301211 if (sas_device && sas_device->device_info &
1212 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1213 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301214 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301215
1216 not_sata:
1217
Eric Moore635374e2009-03-09 01:21:12 -06001218 if (!sdev->tagged_supported)
1219 max_depth = 1;
1220 if (qdepth > max_depth)
1221 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301222 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1223}
1224
1225/**
1226 * _scsih_change_queue_depth - setting device queue depth
1227 * @sdev: scsi device struct
1228 * @qdepth: requested queue depth
1229 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1230 * (see include/scsi/scsi_host.h for definition)
1231 *
1232 * Returns queue depth.
1233 */
1234static int
1235_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1236{
1237 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1238 _scsih_adjust_queue_depth(sdev, qdepth);
1239 else if (reason == SCSI_QDEPTH_QFULL)
1240 scsi_track_queue_full(sdev, qdepth);
1241 else
1242 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001243
1244 if (sdev->inquiry_len > 7)
1245 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1246 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1247 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1248 sdev->ordered_tags, sdev->scsi_level,
1249 (sdev->inquiry[7] & 2) >> 1);
1250
1251 return sdev->queue_depth;
1252}
1253
1254/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301255 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001256 * @sdev: scsi device struct
1257 * @tag_type: requested tag type
1258 *
1259 * Returns queue tag type.
1260 */
1261static int
Eric Moored5d135b2009-05-18 13:02:08 -06001262_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001263{
1264 if (sdev->tagged_supported) {
1265 scsi_set_tag_type(sdev, tag_type);
1266 if (tag_type)
1267 scsi_activate_tcq(sdev, sdev->queue_depth);
1268 else
1269 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1270 } else
1271 tag_type = 0;
1272
1273 return tag_type;
1274}
1275
1276/**
Eric Moored5d135b2009-05-18 13:02:08 -06001277 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001278 * @starget: scsi target struct
1279 *
1280 * Returns 0 if ok. Any other return is assumed to be an error and
1281 * the device is ignored.
1282 */
1283static int
Eric Moored5d135b2009-05-18 13:02:08 -06001284_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001285{
1286 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1287 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1288 struct MPT2SAS_TARGET *sas_target_priv_data;
1289 struct _sas_device *sas_device;
1290 struct _raid_device *raid_device;
1291 unsigned long flags;
1292 struct sas_rphy *rphy;
1293
1294 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1295 if (!sas_target_priv_data)
1296 return -ENOMEM;
1297
1298 starget->hostdata = sas_target_priv_data;
1299 sas_target_priv_data->starget = starget;
1300 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1301
1302 /* RAID volumes */
1303 if (starget->channel == RAID_CHANNEL) {
1304 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1305 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1306 starget->channel);
1307 if (raid_device) {
1308 sas_target_priv_data->handle = raid_device->handle;
1309 sas_target_priv_data->sas_address = raid_device->wwid;
1310 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301311 if (ioc->is_warpdrive)
1312 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001313 raid_device->starget = starget;
1314 }
1315 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1316 return 0;
1317 }
1318
1319 /* sas/sata devices */
1320 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1321 rphy = dev_to_rphy(starget->dev.parent);
1322 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1323 rphy->identify.sas_address);
1324
1325 if (sas_device) {
1326 sas_target_priv_data->handle = sas_device->handle;
1327 sas_target_priv_data->sas_address = sas_device->sas_address;
1328 sas_device->starget = starget;
1329 sas_device->id = starget->id;
1330 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301331 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001332 sas_target_priv_data->flags |=
1333 MPT_TARGET_FLAGS_RAID_COMPONENT;
1334 }
1335 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1336
1337 return 0;
1338}
1339
1340/**
Eric Moored5d135b2009-05-18 13:02:08 -06001341 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001342 * @starget: scsi target struct
1343 *
1344 * Returns nothing.
1345 */
1346static void
Eric Moored5d135b2009-05-18 13:02:08 -06001347_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001348{
1349 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1350 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1351 struct MPT2SAS_TARGET *sas_target_priv_data;
1352 struct _sas_device *sas_device;
1353 struct _raid_device *raid_device;
1354 unsigned long flags;
1355 struct sas_rphy *rphy;
1356
1357 sas_target_priv_data = starget->hostdata;
1358 if (!sas_target_priv_data)
1359 return;
1360
1361 if (starget->channel == RAID_CHANNEL) {
1362 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1363 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1364 starget->channel);
1365 if (raid_device) {
1366 raid_device->starget = NULL;
1367 raid_device->sdev = NULL;
1368 }
1369 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1370 goto out;
1371 }
1372
1373 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1374 rphy = dev_to_rphy(starget->dev.parent);
1375 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1376 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001377 if (sas_device && (sas_device->starget == starget) &&
1378 (sas_device->id == starget->id) &&
1379 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001380 sas_device->starget = NULL;
1381
1382 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1383
1384 out:
1385 kfree(sas_target_priv_data);
1386 starget->hostdata = NULL;
1387}
1388
1389/**
Eric Moored5d135b2009-05-18 13:02:08 -06001390 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001391 * @sdev: scsi device struct
1392 *
1393 * Returns 0 if ok. Any other return is assumed to be an error and
1394 * the device is ignored.
1395 */
1396static int
Eric Moored5d135b2009-05-18 13:02:08 -06001397_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001398{
1399 struct Scsi_Host *shost;
1400 struct MPT2SAS_ADAPTER *ioc;
1401 struct MPT2SAS_TARGET *sas_target_priv_data;
1402 struct MPT2SAS_DEVICE *sas_device_priv_data;
1403 struct scsi_target *starget;
1404 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001405 unsigned long flags;
1406
1407 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1408 if (!sas_device_priv_data)
1409 return -ENOMEM;
1410
1411 sas_device_priv_data->lun = sdev->lun;
1412 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1413
1414 starget = scsi_target(sdev);
1415 sas_target_priv_data = starget->hostdata;
1416 sas_target_priv_data->num_luns++;
1417 sas_device_priv_data->sas_target = sas_target_priv_data;
1418 sdev->hostdata = sas_device_priv_data;
1419 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1420 sdev->no_uld_attach = 1;
1421
1422 shost = dev_to_shost(&starget->dev);
1423 ioc = shost_priv(shost);
1424 if (starget->channel == RAID_CHANNEL) {
1425 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1426 raid_device = _scsih_raid_device_find_by_id(ioc,
1427 starget->id, starget->channel);
1428 if (raid_device)
1429 raid_device->sdev = sdev; /* raid is single lun */
1430 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001431 }
1432
Eric Moore635374e2009-03-09 01:21:12 -06001433 return 0;
1434}
1435
1436/**
Eric Moored5d135b2009-05-18 13:02:08 -06001437 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001438 * @sdev: scsi device struct
1439 *
1440 * Returns nothing.
1441 */
1442static void
Eric Moored5d135b2009-05-18 13:02:08 -06001443_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001444{
1445 struct MPT2SAS_TARGET *sas_target_priv_data;
1446 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301447 struct Scsi_Host *shost;
1448 struct MPT2SAS_ADAPTER *ioc;
1449 struct _sas_device *sas_device;
1450 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001451
1452 if (!sdev->hostdata)
1453 return;
1454
1455 starget = scsi_target(sdev);
1456 sas_target_priv_data = starget->hostdata;
1457 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301458
1459 shost = dev_to_shost(&starget->dev);
1460 ioc = shost_priv(shost);
1461
1462 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1463 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1464 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1465 sas_target_priv_data->sas_address);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +05301466 if (sas_device && !sas_target_priv_data->num_luns)
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301467 sas_device->starget = NULL;
1468 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1469 }
1470
Eric Moore635374e2009-03-09 01:21:12 -06001471 kfree(sdev->hostdata);
1472 sdev->hostdata = NULL;
1473}
1474
1475/**
Eric Moored5d135b2009-05-18 13:02:08 -06001476 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001477 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301478 * @handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -06001479 * @sdev: scsi device struct
1480 */
1481static void
Eric Moored5d135b2009-05-18 13:02:08 -06001482_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301483 u16 handle, struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001484{
1485 Mpi2ConfigReply_t mpi_reply;
1486 Mpi2SasDevicePage0_t sas_device_pg0;
1487 u32 ioc_status;
1488 u16 flags;
1489 u32 device_info;
1490
1491 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301492 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06001493 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1494 ioc->name, __FILE__, __LINE__, __func__);
1495 return;
1496 }
1497
1498 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1499 MPI2_IOCSTATUS_MASK;
1500 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1501 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1502 ioc->name, __FILE__, __LINE__, __func__);
1503 return;
1504 }
1505
1506 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301507 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001508
1509 sdev_printk(KERN_INFO, sdev,
1510 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1511 "sw_preserve(%s)\n",
1512 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1513 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1514 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1515 "n",
1516 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1517 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1518 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1519}
1520
1521/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301522 * _scsih_is_raid - return boolean indicating device is raid volume
1523 * @dev the device struct object
1524 */
1525static int
1526_scsih_is_raid(struct device *dev)
1527{
1528 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301529 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301530
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301531 if (ioc->is_warpdrive)
1532 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301533 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1534}
1535
1536/**
1537 * _scsih_get_resync - get raid volume resync percent complete
1538 * @dev the device struct object
1539 */
1540static void
1541_scsih_get_resync(struct device *dev)
1542{
1543 struct scsi_device *sdev = to_scsi_device(dev);
1544 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1545 static struct _raid_device *raid_device;
1546 unsigned long flags;
1547 Mpi2RaidVolPage0_t vol_pg0;
1548 Mpi2ConfigReply_t mpi_reply;
1549 u32 volume_status_flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301550 u8 percent_complete;
1551 u16 handle;
1552
1553 percent_complete = 0;
1554 handle = 0;
1555 if (ioc->is_warpdrive)
1556 goto out;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301557
1558 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1559 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1560 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301561 if (raid_device) {
1562 handle = raid_device->handle;
1563 percent_complete = raid_device->percent_complete;
1564 }
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301565 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1566
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301567 if (!handle)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301568 goto out;
1569
1570 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301571 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301572 sizeof(Mpi2RaidVolPage0_t))) {
1573 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1574 ioc->name, __FILE__, __LINE__, __func__);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301575 percent_complete = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301576 goto out;
1577 }
1578
1579 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301580 if (!(volume_status_flags &
1581 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1582 percent_complete = 0;
1583
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301584 out:
1585 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1586}
1587
1588/**
1589 * _scsih_get_state - get raid volume level
1590 * @dev the device struct object
1591 */
1592static void
1593_scsih_get_state(struct device *dev)
1594{
1595 struct scsi_device *sdev = to_scsi_device(dev);
1596 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1597 static struct _raid_device *raid_device;
1598 unsigned long flags;
1599 Mpi2RaidVolPage0_t vol_pg0;
1600 Mpi2ConfigReply_t mpi_reply;
1601 u32 volstate;
1602 enum raid_state state = RAID_STATE_UNKNOWN;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301603 u16 handle = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301604
1605 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1606 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1607 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301608 if (raid_device)
1609 handle = raid_device->handle;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301610 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1611
1612 if (!raid_device)
1613 goto out;
1614
1615 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301616 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301617 sizeof(Mpi2RaidVolPage0_t))) {
1618 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1619 ioc->name, __FILE__, __LINE__, __func__);
1620 goto out;
1621 }
1622
1623 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1624 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1625 state = RAID_STATE_RESYNCING;
1626 goto out;
1627 }
1628
1629 switch (vol_pg0.VolumeState) {
1630 case MPI2_RAID_VOL_STATE_OPTIMAL:
1631 case MPI2_RAID_VOL_STATE_ONLINE:
1632 state = RAID_STATE_ACTIVE;
1633 break;
1634 case MPI2_RAID_VOL_STATE_DEGRADED:
1635 state = RAID_STATE_DEGRADED;
1636 break;
1637 case MPI2_RAID_VOL_STATE_FAILED:
1638 case MPI2_RAID_VOL_STATE_MISSING:
1639 state = RAID_STATE_OFFLINE;
1640 break;
1641 }
1642 out:
1643 raid_set_state(mpt2sas_raid_template, dev, state);
1644}
1645
1646/**
1647 * _scsih_set_level - set raid level
1648 * @sdev: scsi device struct
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301649 * @volume_type: volume type
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301650 */
1651static void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301652_scsih_set_level(struct scsi_device *sdev, u8 volume_type)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301653{
1654 enum raid_level level = RAID_LEVEL_UNKNOWN;
1655
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301656 switch (volume_type) {
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301657 case MPI2_RAID_VOL_TYPE_RAID0:
1658 level = RAID_LEVEL_0;
1659 break;
1660 case MPI2_RAID_VOL_TYPE_RAID10:
1661 level = RAID_LEVEL_10;
1662 break;
1663 case MPI2_RAID_VOL_TYPE_RAID1E:
1664 level = RAID_LEVEL_1E;
1665 break;
1666 case MPI2_RAID_VOL_TYPE_RAID1:
1667 level = RAID_LEVEL_1;
1668 break;
1669 }
1670
1671 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1672}
1673
1674/**
Eric Moore635374e2009-03-09 01:21:12 -06001675 * _scsih_get_volume_capabilities - volume capabilities
1676 * @ioc: per adapter object
1677 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301678 *
1679 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001680 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301681static int
Eric Moore635374e2009-03-09 01:21:12 -06001682_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1683 struct _raid_device *raid_device)
1684{
1685 Mpi2RaidVolPage0_t *vol_pg0;
1686 Mpi2RaidPhysDiskPage0_t pd_pg0;
1687 Mpi2SasDevicePage0_t sas_device_pg0;
1688 Mpi2ConfigReply_t mpi_reply;
1689 u16 sz;
1690 u8 num_pds;
1691
1692 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1693 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301694 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1695 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1696 __func__));
1697 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001698 }
1699
1700 raid_device->num_pds = num_pds;
1701 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1702 sizeof(Mpi2RaidVol0PhysDisk_t));
1703 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1704 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301705 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1706 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1707 __func__));
1708 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001709 }
1710
1711 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1712 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301713 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1714 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1715 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001716 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301717 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001718 }
1719
1720 raid_device->volume_type = vol_pg0->VolumeType;
1721
1722 /* figure out what the underlying devices are by
1723 * obtaining the device_info bits for the 1st device
1724 */
1725 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1726 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1727 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1728 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1729 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1730 le16_to_cpu(pd_pg0.DevHandle)))) {
1731 raid_device->device_info =
1732 le32_to_cpu(sas_device_pg0.DeviceInfo);
1733 }
1734 }
1735
1736 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301737 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001738}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301739/**
1740 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1741 * @ioc: per adapter object
1742 */
1743static void
1744_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1745{
1746 Mpi2RaidVolPage1_t vol_pg1;
1747 Mpi2ConfigReply_t mpi_reply;
1748 struct _raid_device *raid_device;
1749 u16 handle;
1750 u16 ioc_status;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301751 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301752
1753 handle = 0xFFFF;
1754 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1755 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1756 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1757 MPI2_IOCSTATUS_MASK;
1758 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1759 break;
1760 handle = le16_to_cpu(vol_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301761 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301762 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1763 if (raid_device)
1764 raid_device->direct_io_enabled = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301765 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301766 }
1767 return;
1768}
1769
1770
1771/**
1772 * _scsih_get_num_volumes - Get number of volumes in the ioc
1773 * @ioc: per adapter object
1774 */
1775static u8
1776_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1777{
1778 Mpi2RaidVolPage1_t vol_pg1;
1779 Mpi2ConfigReply_t mpi_reply;
1780 u16 handle;
1781 u8 vol_cnt = 0;
1782 u16 ioc_status;
1783
1784 handle = 0xFFFF;
1785 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1786 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1787 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1788 MPI2_IOCSTATUS_MASK;
1789 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1790 break;
1791 vol_cnt++;
1792 handle = le16_to_cpu(vol_pg1.DevHandle);
1793 }
1794 return vol_cnt;
1795}
1796
1797
1798/**
1799 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1800 * @ioc: per adapter object
1801 * @raid_device: the raid_device object
1802 */
1803static void
1804_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1805 struct _raid_device *raid_device)
1806{
1807 Mpi2RaidVolPage0_t *vol_pg0;
1808 Mpi2RaidPhysDiskPage0_t pd_pg0;
1809 Mpi2ConfigReply_t mpi_reply;
1810 u16 sz;
1811 u8 num_pds, count;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301812 unsigned long stripe_sz, block_sz;
1813 u8 stripe_exp, block_exp;
1814 u64 dev_max_lba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301815
1816 if (!ioc->is_warpdrive)
1817 return;
1818
1819 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1820 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1821 "globally as drives are exposed\n", ioc->name);
1822 return;
1823 }
1824 if (_scsih_get_num_volumes(ioc) > 1) {
1825 _scsih_disable_ddio(ioc);
1826 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1827 "globally as number of drives > 1\n", ioc->name);
1828 return;
1829 }
1830 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1831 &num_pds)) || !num_pds) {
1832 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1833 "Failure in computing number of drives\n", ioc->name);
1834 return;
1835 }
1836
1837 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1838 sizeof(Mpi2RaidVol0PhysDisk_t));
1839 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1840 if (!vol_pg0) {
1841 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1842 "Memory allocation failure for RVPG0\n", ioc->name);
1843 return;
1844 }
1845
1846 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1847 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1848 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1849 "Failure in retrieving RVPG0\n", ioc->name);
1850 kfree(vol_pg0);
1851 return;
1852 }
1853
1854 /*
1855 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1856 * assumed for WARPDRIVE, disable direct I/O
1857 */
1858 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1859 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1860 "for the drive with handle(0x%04x): num_mem=%d, "
1861 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1862 num_pds, MPT_MAX_WARPDRIVE_PDS);
1863 kfree(vol_pg0);
1864 return;
1865 }
1866 for (count = 0; count < num_pds; count++) {
1867 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1868 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1869 vol_pg0->PhysDisk[count].PhysDiskNum) ||
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05301870 le16_to_cpu(pd_pg0.DevHandle) ==
1871 MPT2SAS_INVALID_DEVICE_HANDLE) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301872 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1873 "disabled for the drive with handle(0x%04x) member"
1874 "handle retrieval failed for member number=%d\n",
1875 ioc->name, raid_device->handle,
1876 vol_pg0->PhysDisk[count].PhysDiskNum);
1877 goto out_error;
1878 }
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301879 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1880 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1881 if (dev_max_lba >> 32) {
1882 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1883 "disabled for the drive with handle(0x%04x) member"
1884 "handle (0x%04x) unsupported max lba 0x%016llx\n",
1885 ioc->name, raid_device->handle,
1886 le16_to_cpu(pd_pg0.DevHandle),
1887 (unsigned long long)dev_max_lba);
1888 goto out_error;
1889 }
1890
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301891 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1892 }
1893
1894 /*
1895 * Assumption for WD: Direct I/O is not supported if the volume is
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301896 * not RAID0
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301897 */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301898 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301899 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1900 "for the drive with handle(0x%04x): type=%d, "
1901 "s_sz=%uK, blk_size=%u\n", ioc->name,
1902 raid_device->handle, raid_device->volume_type,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301903 (le32_to_cpu(vol_pg0->StripeSize) *
1904 le16_to_cpu(vol_pg0->BlockSize)) / 1024,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301905 le16_to_cpu(vol_pg0->BlockSize));
1906 goto out_error;
1907 }
1908
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301909 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301910 stripe_exp = find_first_bit(&stripe_sz, 32);
1911 if (stripe_exp == 32) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301912 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301913 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301914 ioc->name, raid_device->handle,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301915 (le32_to_cpu(vol_pg0->StripeSize) *
1916 le16_to_cpu(vol_pg0->BlockSize)) / 1024);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301917 goto out_error;
1918 }
1919 raid_device->stripe_exponent = stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301920 block_sz = le16_to_cpu(vol_pg0->BlockSize);
1921 block_exp = find_first_bit(&block_sz, 16);
1922 if (block_exp == 16) {
1923 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1924 "for the drive with handle(0x%04x) invalid block sz %u\n",
1925 ioc->name, raid_device->handle,
1926 le16_to_cpu(vol_pg0->BlockSize));
1927 goto out_error;
1928 }
1929 raid_device->block_exponent = block_exp;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301930 raid_device->direct_io_enabled = 1;
1931
1932 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1933 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1934 /*
1935 * WARPDRIVE: Though the following fields are not used for direct IO,
1936 * stored for future purpose:
1937 */
1938 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1939 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1940 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1941
1942
1943 kfree(vol_pg0);
1944 return;
1945
1946out_error:
1947 raid_device->direct_io_enabled = 0;
1948 for (count = 0; count < num_pds; count++)
1949 raid_device->pd_handle[count] = 0;
1950 kfree(vol_pg0);
1951 return;
1952}
Eric Moore635374e2009-03-09 01:21:12 -06001953
1954/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301955 * _scsih_enable_tlr - setting TLR flags
1956 * @ioc: per adapter object
1957 * @sdev: scsi device struct
1958 *
1959 * Enabling Transaction Layer Retries for tape devices when
1960 * vpd page 0x90 is present
1961 *
1962 */
1963static void
1964_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1965{
1966 /* only for TAPE */
1967 if (sdev->type != TYPE_TAPE)
1968 return;
1969
1970 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1971 return;
1972
1973 sas_enable_tlr(sdev);
1974 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1975 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1976 return;
1977
1978}
1979
1980/**
Eric Moored5d135b2009-05-18 13:02:08 -06001981 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001982 * @sdev: scsi device struct
1983 *
1984 * Returns 0 if ok. Any other return is assumed to be an error and
1985 * the device is ignored.
1986 */
1987static int
Eric Moored5d135b2009-05-18 13:02:08 -06001988_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001989{
1990 struct Scsi_Host *shost = sdev->host;
1991 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1992 struct MPT2SAS_DEVICE *sas_device_priv_data;
1993 struct MPT2SAS_TARGET *sas_target_priv_data;
1994 struct _sas_device *sas_device;
1995 struct _raid_device *raid_device;
1996 unsigned long flags;
1997 int qdepth;
1998 u8 ssp_target = 0;
1999 char *ds = "";
2000 char *r_level = "";
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302001 u16 handle, volume_handle = 0;
2002 u64 volume_wwid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06002003
2004 qdepth = 1;
2005 sas_device_priv_data = sdev->hostdata;
2006 sas_device_priv_data->configured_lun = 1;
2007 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
2008 sas_target_priv_data = sas_device_priv_data->sas_target;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302009 handle = sas_target_priv_data->handle;
Eric Moore635374e2009-03-09 01:21:12 -06002010
2011 /* raid volume handling */
2012 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
2013
2014 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302015 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06002016 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2017 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302018 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2019 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2020 __LINE__, __func__));
2021 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002022 }
2023
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302024 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2025 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2026 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2027 __LINE__, __func__));
2028 return 1;
2029 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302030 /*
2031 * WARPDRIVE: Initialize the required data for Direct IO
2032 */
2033 _scsih_init_warpdrive_properties(ioc, raid_device);
2034
Eric Moore635374e2009-03-09 01:21:12 -06002035 /* RAID Queue Depth Support
2036 * IS volume = underlying qdepth of drive type, either
2037 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2038 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2039 */
2040 if (raid_device->device_info &
2041 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2042 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2043 ds = "SSP";
2044 } else {
2045 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2046 if (raid_device->device_info &
2047 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2048 ds = "SATA";
2049 else
2050 ds = "STP";
2051 }
2052
2053 switch (raid_device->volume_type) {
2054 case MPI2_RAID_VOL_TYPE_RAID0:
2055 r_level = "RAID0";
2056 break;
2057 case MPI2_RAID_VOL_TYPE_RAID1E:
2058 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302059 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302060 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302061 MFG10_GF0_R10_DISPLAY) &&
2062 !(raid_device->num_pds % 2))
2063 r_level = "RAID10";
2064 else
2065 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002066 break;
2067 case MPI2_RAID_VOL_TYPE_RAID1:
2068 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2069 r_level = "RAID1";
2070 break;
2071 case MPI2_RAID_VOL_TYPE_RAID10:
2072 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2073 r_level = "RAID10";
2074 break;
2075 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2076 default:
2077 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2078 r_level = "RAIDX";
2079 break;
2080 }
2081
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302082 if (!ioc->hide_ir_msg)
2083 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2084 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2085 r_level, raid_device->handle,
2086 (unsigned long long)raid_device->wwid,
2087 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002088 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302089 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302090 if (!ioc->is_warpdrive)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302091 _scsih_set_level(sdev, raid_device->volume_type);
Eric Moore635374e2009-03-09 01:21:12 -06002092 return 0;
2093 }
2094
2095 /* non-raid handling */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302096 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2097 if (mpt2sas_config_get_volume_handle(ioc, handle,
2098 &volume_handle)) {
2099 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2100 "failure at %s:%d/%s()!\n", ioc->name,
2101 __FILE__, __LINE__, __func__));
2102 return 1;
2103 }
2104 if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
2105 volume_handle, &volume_wwid)) {
2106 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2107 "failure at %s:%d/%s()!\n", ioc->name,
2108 __FILE__, __LINE__, __func__));
2109 return 1;
2110 }
2111 }
2112
Eric Moore635374e2009-03-09 01:21:12 -06002113 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2114 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2115 sas_device_priv_data->sas_target->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302116 if (!sas_device) {
2117 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302118 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302119 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2120 __LINE__, __func__));
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302121 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002122 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302123 sas_device->volume_handle = volume_handle;
2124 sas_device->volume_wwid = volume_wwid;
2125 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2126 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2127 ssp_target = 1;
2128 ds = "SSP";
2129 } else {
2130 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2131 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2132 ds = "STP";
2133 else if (sas_device->device_info &
2134 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2135 ds = "SATA";
2136 }
2137 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2138 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2139 ds, sas_device->handle,
2140 (unsigned long long)sas_device->sas_address,
2141 sas_device->phy,
2142 (unsigned long long)sas_device->device_name);
2143 sdev_printk(KERN_INFO, sdev, "%s: "
2144 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2145 (unsigned long long) sas_device->enclosure_logical_id,
2146 sas_device->slot);
2147
2148 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2149 if (!ssp_target)
2150 _scsih_display_sata_capabilities(ioc, handle, sdev);
2151
Eric Moore635374e2009-03-09 01:21:12 -06002152
Mike Christiee881a172009-10-15 17:46:39 -07002153 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002154
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302155 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002156 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302157 _scsih_enable_tlr(ioc, sdev);
2158 }
Eric Moore635374e2009-03-09 01:21:12 -06002159 return 0;
2160}
2161
2162/**
Eric Moored5d135b2009-05-18 13:02:08 -06002163 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002164 * @sdev: scsi device struct
2165 * @bdev: pointer to block device context
2166 * @capacity: device size (in 512 byte sectors)
2167 * @params: three element array to place output:
2168 * params[0] number of heads (max 255)
2169 * params[1] number of sectors (max 63)
2170 * params[2] number of cylinders
2171 *
2172 * Return nothing.
2173 */
2174static int
Eric Moored5d135b2009-05-18 13:02:08 -06002175_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002176 sector_t capacity, int params[])
2177{
2178 int heads;
2179 int sectors;
2180 sector_t cylinders;
2181 ulong dummy;
2182
2183 heads = 64;
2184 sectors = 32;
2185
2186 dummy = heads * sectors;
2187 cylinders = capacity;
2188 sector_div(cylinders, dummy);
2189
2190 /*
2191 * Handle extended translation size for logical drives
2192 * > 1Gb
2193 */
2194 if ((ulong)capacity >= 0x200000) {
2195 heads = 255;
2196 sectors = 63;
2197 dummy = heads * sectors;
2198 cylinders = capacity;
2199 sector_div(cylinders, dummy);
2200 }
2201
2202 /* return result */
2203 params[0] = heads;
2204 params[1] = sectors;
2205 params[2] = cylinders;
2206
2207 return 0;
2208}
2209
2210/**
2211 * _scsih_response_code - translation of device response code
2212 * @ioc: per adapter object
2213 * @response_code: response code returned by the device
2214 *
2215 * Return nothing.
2216 */
2217static void
2218_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2219{
2220 char *desc;
2221
2222 switch (response_code) {
2223 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2224 desc = "task management request completed";
2225 break;
2226 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2227 desc = "invalid frame";
2228 break;
2229 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2230 desc = "task management request not supported";
2231 break;
2232 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2233 desc = "task management request failed";
2234 break;
2235 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2236 desc = "task management request succeeded";
2237 break;
2238 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2239 desc = "invalid lun";
2240 break;
2241 case 0xA:
2242 desc = "overlapped tag attempted";
2243 break;
2244 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2245 desc = "task queued, however not sent to target";
2246 break;
2247 default:
2248 desc = "unknown";
2249 break;
2250 }
2251 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2252 ioc->name, response_code, desc);
2253}
2254
2255/**
Eric Moored5d135b2009-05-18 13:02:08 -06002256 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002257 * @ioc: per adapter object
2258 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302259 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002260 * @reply: reply message frame(lower 32bit addr)
2261 * Context: none.
2262 *
2263 * The callback handler when using scsih_issue_tm.
2264 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302265 * Return 1 meaning mf should be freed from _base_interrupt
2266 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002267 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302268static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302269_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002270{
2271 MPI2DefaultReply_t *mpi_reply;
2272
2273 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302274 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002275 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302276 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302277 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002278 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2279 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2280 if (mpi_reply) {
2281 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2282 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2283 }
2284 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2285 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302286 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002287}
2288
2289/**
2290 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2291 * @ioc: per adapter object
2292 * @handle: device handle
2293 *
2294 * During taskmangement request, we need to freeze the device queue.
2295 */
2296void
2297mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2298{
2299 struct MPT2SAS_DEVICE *sas_device_priv_data;
2300 struct scsi_device *sdev;
2301 u8 skip = 0;
2302
2303 shost_for_each_device(sdev, ioc->shost) {
2304 if (skip)
2305 continue;
2306 sas_device_priv_data = sdev->hostdata;
2307 if (!sas_device_priv_data)
2308 continue;
2309 if (sas_device_priv_data->sas_target->handle == handle) {
2310 sas_device_priv_data->sas_target->tm_busy = 1;
2311 skip = 1;
2312 ioc->ignore_loginfos = 1;
2313 }
2314 }
2315}
2316
2317/**
2318 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2319 * @ioc: per adapter object
2320 * @handle: device handle
2321 *
2322 * During taskmangement request, we need to freeze the device queue.
2323 */
2324void
2325mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2326{
2327 struct MPT2SAS_DEVICE *sas_device_priv_data;
2328 struct scsi_device *sdev;
2329 u8 skip = 0;
2330
2331 shost_for_each_device(sdev, ioc->shost) {
2332 if (skip)
2333 continue;
2334 sas_device_priv_data = sdev->hostdata;
2335 if (!sas_device_priv_data)
2336 continue;
2337 if (sas_device_priv_data->sas_target->handle == handle) {
2338 sas_device_priv_data->sas_target->tm_busy = 0;
2339 skip = 1;
2340 ioc->ignore_loginfos = 0;
2341 }
2342 }
2343}
2344
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302345
Eric Moore635374e2009-03-09 01:21:12 -06002346/**
2347 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2348 * @ioc: per adapter struct
2349 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302350 * @channel: the channel assigned by the OS
2351 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002352 * @lun: lun number
2353 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2354 * @smid_task: smid assigned to the task
2355 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302356 * @serial_number: the serial_number from scmd
2357 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302358 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002359 *
2360 * A generic API for sending task management requests to firmware.
2361 *
Eric Moore635374e2009-03-09 01:21:12 -06002362 * The callback index is set inside `ioc->tm_cb_idx`.
2363 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302364 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002365 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302366int
2367mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2368 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302369 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002370{
2371 Mpi2SCSITaskManagementRequest_t *mpi_request;
2372 Mpi2SCSITaskManagementReply_t *mpi_reply;
2373 u16 smid = 0;
2374 u32 ioc_state;
2375 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302376 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302377 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002378
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302379 if (m_type == TM_MUTEX_ON)
2380 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302381 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2382 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2383 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302384 rc = FAILED;
2385 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302386 }
2387
Eric Moore3cb54692010-07-08 14:44:34 -06002388 if (ioc->shost_recovery || ioc->remove_host ||
2389 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002390 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2391 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302392 rc = FAILED;
2393 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002394 }
Eric Moore635374e2009-03-09 01:21:12 -06002395
2396 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2397 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302398 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002399 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302400 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302401 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302402 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302403 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002404 }
2405
2406 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2407 mpt2sas_base_fault_info(ioc, ioc_state &
2408 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302409 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302410 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302411 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302412 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002413 }
2414
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302415 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002416 if (!smid) {
2417 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2418 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302419 rc = FAILED;
2420 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002421 }
2422
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302423 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2424 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2425
Eric Moore635374e2009-03-09 01:21:12 -06002426 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302427 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2428 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002429 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2430 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2431 ioc->tm_cmds.smid = smid;
2432 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302433 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002434 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2435 mpi_request->DevHandle = cpu_to_le16(handle);
2436 mpi_request->TaskType = type;
2437 mpi_request->TaskMID = cpu_to_le16(smid_task);
2438 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2439 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302440 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302441 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002442 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002443 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2444 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2445 ioc->name, __func__);
2446 _debug_dump_mf(mpi_request,
2447 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302448 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302449 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302450 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302451 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302452 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2453 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2454 goto err_out;
2455 }
Eric Moore635374e2009-03-09 01:21:12 -06002456 }
2457
2458 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2459 mpi_reply = ioc->tm_cmds.reply;
2460 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2461 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2462 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2463 le32_to_cpu(mpi_reply->IOCLogInfo),
2464 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302465 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002466 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302467 if (mpi_reply->IOCStatus)
2468 _debug_dump_mf(mpi_request,
2469 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2470 }
Eric Moore635374e2009-03-09 01:21:12 -06002471 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302472
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302473 switch (type) {
2474 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302475 rc = SUCCESS;
2476 if (scsi_lookup->scmd == NULL)
2477 break;
2478 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302479 break;
2480
2481 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2482 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2483 rc = FAILED;
2484 else
2485 rc = SUCCESS;
2486 break;
2487
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302488 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302489 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2490 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2491 rc = FAILED;
2492 else
2493 rc = SUCCESS;
2494 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302495 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2496 rc = SUCCESS;
2497 break;
2498 default:
2499 rc = FAILED;
2500 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302501 }
2502
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302503 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2504 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302505 if (m_type == TM_MUTEX_ON)
2506 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302507
2508 return rc;
2509
2510 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302511 if (m_type == TM_MUTEX_ON)
2512 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302513 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002514}
2515
2516/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302517 * _scsih_tm_display_info - displays info about the device
2518 * @ioc: per adapter struct
2519 * @scmd: pointer to scsi command object
2520 *
2521 * Called by task management callback handlers.
2522 */
2523static void
2524_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2525{
2526 struct scsi_target *starget = scmd->device->sdev_target;
2527 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2528 struct _sas_device *sas_device = NULL;
2529 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302530 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302531
2532 if (!priv_target)
2533 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302534 if (ioc->hide_ir_msg)
2535 device_str = "WarpDrive";
2536 else
2537 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302538
2539 scsi_print_command(scmd);
2540 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302541 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2542 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2543 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302544 } else {
2545 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2546 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2547 priv_target->sas_address);
2548 if (sas_device) {
2549 if (priv_target->flags &
2550 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2551 starget_printk(KERN_INFO, starget,
2552 "volume handle(0x%04x), "
2553 "volume wwid(0x%016llx)\n",
2554 sas_device->volume_handle,
2555 (unsigned long long)sas_device->volume_wwid);
2556 }
2557 starget_printk(KERN_INFO, starget,
2558 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2559 sas_device->handle,
2560 (unsigned long long)sas_device->sas_address,
2561 sas_device->phy);
2562 starget_printk(KERN_INFO, starget,
2563 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2564 (unsigned long long)sas_device->enclosure_logical_id,
2565 sas_device->slot);
2566 }
2567 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2568 }
2569}
2570
2571/**
Eric Moored5d135b2009-05-18 13:02:08 -06002572 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302573 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002574 *
2575 * Returns SUCCESS if command aborted else FAILED
2576 */
2577static int
Eric Moored5d135b2009-05-18 13:02:08 -06002578_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002579{
2580 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2581 struct MPT2SAS_DEVICE *sas_device_priv_data;
2582 u16 smid;
2583 u16 handle;
2584 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002585
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302586 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2587 "scmd(%p)\n", scmd);
2588 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002589
2590 sas_device_priv_data = scmd->device->hostdata;
2591 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302592 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2593 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002594 scmd->result = DID_NO_CONNECT << 16;
2595 scmd->scsi_done(scmd);
2596 r = SUCCESS;
2597 goto out;
2598 }
2599
2600 /* search for the command */
2601 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2602 if (!smid) {
2603 scmd->result = DID_RESET << 16;
2604 r = SUCCESS;
2605 goto out;
2606 }
2607
2608 /* for hidden raid components and volumes this is not supported */
2609 if (sas_device_priv_data->sas_target->flags &
2610 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2611 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2612 scmd->result = DID_RESET << 16;
2613 r = FAILED;
2614 goto out;
2615 }
2616
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302617 mpt2sas_halt_firmware(ioc);
2618
Eric Moore635374e2009-03-09 01:21:12 -06002619 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302620 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2621 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302622 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2623 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002624
2625 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302626 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2627 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002628 return r;
2629}
2630
Eric Moore635374e2009-03-09 01:21:12 -06002631/**
Eric Moored5d135b2009-05-18 13:02:08 -06002632 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302633 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002634 *
2635 * Returns SUCCESS if command aborted else FAILED
2636 */
2637static int
Eric Moored5d135b2009-05-18 13:02:08 -06002638_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002639{
2640 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2641 struct MPT2SAS_DEVICE *sas_device_priv_data;
2642 struct _sas_device *sas_device;
2643 unsigned long flags;
2644 u16 handle;
2645 int r;
2646
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302647 struct scsi_target *starget = scmd->device->sdev_target;
2648
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302649 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302650 "scmd(%p)\n", scmd);
2651 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002652
2653 sas_device_priv_data = scmd->device->hostdata;
2654 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302655 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302656 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002657 scmd->result = DID_NO_CONNECT << 16;
2658 scmd->scsi_done(scmd);
2659 r = SUCCESS;
2660 goto out;
2661 }
2662
2663 /* for hidden raid components obtain the volume_handle */
2664 handle = 0;
2665 if (sas_device_priv_data->sas_target->flags &
2666 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2667 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2668 sas_device = _scsih_sas_device_find_by_handle(ioc,
2669 sas_device_priv_data->sas_target->handle);
2670 if (sas_device)
2671 handle = sas_device->volume_handle;
2672 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2673 } else
2674 handle = sas_device_priv_data->sas_target->handle;
2675
2676 if (!handle) {
2677 scmd->result = DID_RESET << 16;
2678 r = FAILED;
2679 goto out;
2680 }
2681
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302682 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2683 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302684 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2685 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002686
2687 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302688 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2689 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002690 return r;
2691}
2692
2693/**
Eric Moored5d135b2009-05-18 13:02:08 -06002694 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302695 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002696 *
2697 * Returns SUCCESS if command aborted else FAILED
2698 */
2699static int
Eric Moored5d135b2009-05-18 13:02:08 -06002700_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002701{
2702 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2703 struct MPT2SAS_DEVICE *sas_device_priv_data;
2704 struct _sas_device *sas_device;
2705 unsigned long flags;
2706 u16 handle;
2707 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302708 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002709
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302710 starget_printk(KERN_INFO, starget, "attempting target reset! "
2711 "scmd(%p)\n", scmd);
2712 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002713
2714 sas_device_priv_data = scmd->device->hostdata;
2715 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302716 starget_printk(KERN_INFO, starget, "target been deleted! "
2717 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002718 scmd->result = DID_NO_CONNECT << 16;
2719 scmd->scsi_done(scmd);
2720 r = SUCCESS;
2721 goto out;
2722 }
2723
2724 /* for hidden raid components obtain the volume_handle */
2725 handle = 0;
2726 if (sas_device_priv_data->sas_target->flags &
2727 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2728 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2729 sas_device = _scsih_sas_device_find_by_handle(ioc,
2730 sas_device_priv_data->sas_target->handle);
2731 if (sas_device)
2732 handle = sas_device->volume_handle;
2733 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2734 } else
2735 handle = sas_device_priv_data->sas_target->handle;
2736
2737 if (!handle) {
2738 scmd->result = DID_RESET << 16;
2739 r = FAILED;
2740 goto out;
2741 }
2742
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302743 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2744 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302745 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002746
2747 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302748 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2749 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002750 return r;
2751}
2752
2753/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302754 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302755 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002756 *
2757 * Returns SUCCESS if command aborted else FAILED
2758 */
2759static int
Eric Moored5d135b2009-05-18 13:02:08 -06002760_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002761{
2762 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2763 int r, retval;
2764
2765 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2766 ioc->name, scmd);
2767 scsi_print_command(scmd);
2768
2769 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2770 FORCE_BIG_HAMMER);
2771 r = (retval < 0) ? FAILED : SUCCESS;
2772 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2773 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2774
2775 return r;
2776}
2777
2778/**
2779 * _scsih_fw_event_add - insert and queue up fw_event
2780 * @ioc: per adapter object
2781 * @fw_event: object describing the event
2782 * Context: This function will acquire ioc->fw_event_lock.
2783 *
2784 * This adds the firmware event object into link list, then queues it up to
2785 * be processed from user context.
2786 *
2787 * Return nothing.
2788 */
2789static void
2790_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2791{
2792 unsigned long flags;
2793
2794 if (ioc->firmware_event_thread == NULL)
2795 return;
2796
2797 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2798 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302799 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2800 queue_delayed_work(ioc->firmware_event_thread,
2801 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002802 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2803}
2804
2805/**
2806 * _scsih_fw_event_free - delete fw_event
2807 * @ioc: per adapter object
2808 * @fw_event: object describing the event
2809 * Context: This function will acquire ioc->fw_event_lock.
2810 *
2811 * This removes firmware event object from link list, frees associated memory.
2812 *
2813 * Return nothing.
2814 */
2815static void
2816_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2817 *fw_event)
2818{
2819 unsigned long flags;
2820
2821 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2822 list_del(&fw_event->list);
2823 kfree(fw_event->event_data);
2824 kfree(fw_event);
2825 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2826}
2827
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302828
Eric Moore635374e2009-03-09 01:21:12 -06002829/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302830 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002831 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002832 *
2833 * Return nothing.
2834 */
2835static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302836_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002837{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302838 struct fw_event_work *fw_event;
2839
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302840 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302841 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002842
2843 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2844 if (!fw_event)
2845 return;
2846
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302847 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2848 fw_event->ioc = ioc;
2849 _scsih_fw_event_add(ioc, fw_event);
2850}
2851
2852/**
2853 * mpt2sas_port_enable_complete - port enable completed (fake event)
2854 * @ioc: per adapter object
2855 *
2856 * Return nothing.
2857 */
2858void
2859mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2860{
2861 struct fw_event_work *fw_event;
2862
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302863 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2864 if (!fw_event)
2865 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302866 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302867 fw_event->ioc = ioc;
2868 _scsih_fw_event_add(ioc, fw_event);
2869}
2870
2871/**
2872 * _scsih_fw_event_cleanup_queue - cleanup event queue
2873 * @ioc: per adapter object
2874 *
2875 * Walk the firmware event queue, either killing timers, or waiting
2876 * for outstanding events to complete
2877 *
2878 * Return nothing.
2879 */
2880static void
2881_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2882{
2883 struct fw_event_work *fw_event, *next;
2884
2885 if (list_empty(&ioc->fw_event_list) ||
2886 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002887 return;
2888
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302889 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2890 if (cancel_delayed_work(&fw_event->delayed_work)) {
2891 _scsih_fw_event_free(ioc, fw_event);
2892 continue;
2893 }
2894 fw_event->cancel_pending_work = 1;
2895 }
Eric Moore635374e2009-03-09 01:21:12 -06002896}
2897
Eric Moore635374e2009-03-09 01:21:12 -06002898/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302899 * _scsih_ublock_io_all_device - unblock every device
2900 * @ioc: per adapter object
2901 *
2902 * change the device state from block to running
2903 */
2904static void
2905_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2906{
2907 struct MPT2SAS_DEVICE *sas_device_priv_data;
2908 struct scsi_device *sdev;
2909
2910 shost_for_each_device(sdev, ioc->shost) {
2911 sas_device_priv_data = sdev->hostdata;
2912 if (!sas_device_priv_data)
2913 continue;
2914 if (!sas_device_priv_data->block)
2915 continue;
2916 sas_device_priv_data->block = 0;
2917 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2918 "handle(0x%04x)\n",
2919 sas_device_priv_data->sas_target->handle));
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002920 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302921 }
2922}
2923/**
Eric Moore635374e2009-03-09 01:21:12 -06002924 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2925 * @ioc: per adapter object
2926 * @handle: device handle
2927 *
2928 * During device pull we need to appropiately set the sdev state.
2929 */
2930static void
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302931_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06002932{
2933 struct MPT2SAS_DEVICE *sas_device_priv_data;
2934 struct scsi_device *sdev;
2935
2936 shost_for_each_device(sdev, ioc->shost) {
2937 sas_device_priv_data = sdev->hostdata;
2938 if (!sas_device_priv_data)
2939 continue;
2940 if (!sas_device_priv_data->block)
2941 continue;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302942 if (sas_device_priv_data->sas_target->sas_address ==
2943 sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06002944 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2945 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302946 "sas address(0x%016llx)\n", ioc->name,
2947 (unsigned long long)sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06002948 sas_device_priv_data->block = 0;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002949 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Eric Moore635374e2009-03-09 01:21:12 -06002950 }
2951 }
2952}
2953
2954/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302955 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2956 * @ioc: per adapter object
2957 * @handle: device handle
2958 *
2959 * During device pull we need to appropiately set the sdev state.
2960 */
2961static void
2962_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2963{
2964 struct MPT2SAS_DEVICE *sas_device_priv_data;
2965 struct scsi_device *sdev;
2966
2967 shost_for_each_device(sdev, ioc->shost) {
2968 sas_device_priv_data = sdev->hostdata;
2969 if (!sas_device_priv_data)
2970 continue;
2971 if (sas_device_priv_data->block)
2972 continue;
2973 sas_device_priv_data->block = 1;
2974 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2975 "handle(0x%04x)\n",
2976 sas_device_priv_data->sas_target->handle));
2977 scsi_internal_device_block(sdev);
2978 }
2979}
2980
2981
2982/**
Eric Moore635374e2009-03-09 01:21:12 -06002983 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2984 * @ioc: per adapter object
2985 * @handle: device handle
2986 *
2987 * During device pull we need to appropiately set the sdev state.
2988 */
2989static void
2990_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2991{
2992 struct MPT2SAS_DEVICE *sas_device_priv_data;
2993 struct scsi_device *sdev;
2994
2995 shost_for_each_device(sdev, ioc->shost) {
2996 sas_device_priv_data = sdev->hostdata;
2997 if (!sas_device_priv_data)
2998 continue;
2999 if (sas_device_priv_data->block)
3000 continue;
3001 if (sas_device_priv_data->sas_target->handle == handle) {
3002 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
3003 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
3004 "handle(0x%04x)\n", ioc->name, handle));
3005 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05303006 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06003007 }
3008 }
3009}
3010
3011/**
3012 * _scsih_block_io_to_children_attached_to_ex
3013 * @ioc: per adapter object
3014 * @sas_expander: the sas_device object
3015 *
3016 * This routine set sdev state to SDEV_BLOCK for all devices
3017 * attached to this expander. This function called when expander is
3018 * pulled.
3019 */
3020static void
3021_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
3022 struct _sas_node *sas_expander)
3023{
3024 struct _sas_port *mpt2sas_port;
3025 struct _sas_device *sas_device;
3026 struct _sas_node *expander_sibling;
3027 unsigned long flags;
3028
3029 if (!sas_expander)
3030 return;
3031
3032 list_for_each_entry(mpt2sas_port,
3033 &sas_expander->sas_port_list, port_list) {
3034 if (mpt2sas_port->remote_identify.device_type ==
3035 SAS_END_DEVICE) {
3036 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3037 sas_device =
3038 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3039 mpt2sas_port->remote_identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303040 if (sas_device)
3041 set_bit(sas_device->handle,
3042 ioc->blocking_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003043 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06003044 }
3045 }
3046
3047 list_for_each_entry(mpt2sas_port,
3048 &sas_expander->sas_port_list, port_list) {
3049
3050 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303051 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003052 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303053 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003054 expander_sibling =
3055 mpt2sas_scsih_expander_find_by_sas_address(
3056 ioc, mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003057 _scsih_block_io_to_children_attached_to_ex(ioc,
3058 expander_sibling);
3059 }
3060 }
3061}
3062
3063/**
3064 * _scsih_block_io_to_children_attached_directly
3065 * @ioc: per adapter object
3066 * @event_data: topology change event data
3067 *
3068 * This routine set sdev state to SDEV_BLOCK for all devices
3069 * direct attached during device pull.
3070 */
3071static void
3072_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3073 Mpi2EventDataSasTopologyChangeList_t *event_data)
3074{
3075 int i;
3076 u16 handle;
3077 u16 reason_code;
3078 u8 phy_number;
3079
3080 for (i = 0; i < event_data->NumEntries; i++) {
3081 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3082 if (!handle)
3083 continue;
3084 phy_number = event_data->StartPhyNum + i;
3085 reason_code = event_data->PHY[i].PhyStatus &
3086 MPI2_EVENT_SAS_TOPO_RC_MASK;
3087 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3088 _scsih_block_io_device(ioc, handle);
3089 }
3090}
3091
3092/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303093 * _scsih_tm_tr_send - send task management request
3094 * @ioc: per adapter object
3095 * @handle: device handle
3096 * Context: interrupt time.
3097 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003098 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303099 * with controller firmware. This function will issue target reset
3100 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003101 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303102 *
3103 * This is designed to send muliple task management request at the same
3104 * time to the fifo. If the fifo is full, we will append the request,
3105 * and process it in a future completion.
3106 */
3107static void
3108_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3109{
3110 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303111 u16 smid;
3112 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303113 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3114 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303115 unsigned long flags;
3116 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303117 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303118
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303119 if (ioc->remove_host) {
3120 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3121 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3122 return;
3123 } else if (ioc->pci_error_recovery) {
3124 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3125 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3126 handle));
3127 return;
3128 }
3129 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3130 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3131 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3132 "operational: handle(0x%04x)\n", __func__, ioc->name,
3133 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303134 return;
3135 }
3136
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303137 /* if PD, then return */
3138 if (test_bit(handle, ioc->pd_handles))
3139 return;
3140
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303141 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3142 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303143 if (sas_device && sas_device->starget &&
3144 sas_device->starget->hostdata) {
3145 sas_target_priv_data = sas_device->starget->hostdata;
3146 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303147 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303148 }
3149 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303150
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303151 if (sas_target_priv_data) {
3152 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3153 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3154 (unsigned long long)sas_address));
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05303155 _scsih_ublock_io_device(ioc, sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303156 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303157 }
3158
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303159 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3160 if (!smid) {
3161 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3162 if (!delayed_tr)
3163 return;
3164 INIT_LIST_HEAD(&delayed_tr->list);
3165 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303166 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3167 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3168 "DELAYED:tr:handle(0x%04x), (open)\n",
3169 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303170 return;
3171 }
3172
Kashyap, Desai1278b112010-03-09 17:34:13 +05303173 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3174 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3175 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303176 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3177 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3178 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3179 mpi_request->DevHandle = cpu_to_le16(handle);
3180 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303181 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3182}
3183
3184
3185
3186/**
3187 * _scsih_sas_control_complete - completion routine
3188 * @ioc: per adapter object
3189 * @smid: system request message index
3190 * @msix_index: MSIX table index supplied by the OS
3191 * @reply: reply message frame(lower 32bit addr)
3192 * Context: interrupt time.
3193 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003194 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303195 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003196 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303197 *
3198 * Return 1 meaning mf should be freed from _base_interrupt
3199 * 0 means the mf is freed from this function.
3200 */
3201static u8
3202_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3203 u8 msix_index, u32 reply)
3204{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303205 Mpi2SasIoUnitControlReply_t *mpi_reply =
3206 mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303207 if (likely(mpi_reply)) {
3208 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3209 "sc_complete:handle(0x%04x), (open) "
3210 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3211 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3212 le16_to_cpu(mpi_reply->IOCStatus),
3213 le32_to_cpu(mpi_reply->IOCLogInfo)));
3214 } else {
3215 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3216 ioc->name, __FILE__, __LINE__, __func__);
3217 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303218 return 1;
3219}
3220
3221/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303222 * _scsih_tm_tr_volume_send - send target reset request for volumes
3223 * @ioc: per adapter object
3224 * @handle: device handle
3225 * Context: interrupt time.
3226 *
3227 * This is designed to send muliple task management request at the same
3228 * time to the fifo. If the fifo is full, we will append the request,
3229 * and process it in a future completion.
3230 */
3231static void
3232_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3233{
3234 Mpi2SCSITaskManagementRequest_t *mpi_request;
3235 u16 smid;
3236 struct _tr_list *delayed_tr;
3237
Eric Moore3cb54692010-07-08 14:44:34 -06003238 if (ioc->shost_recovery || ioc->remove_host ||
3239 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303240 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3241 "progress!\n", __func__, ioc->name));
3242 return;
3243 }
3244
3245 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3246 if (!smid) {
3247 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3248 if (!delayed_tr)
3249 return;
3250 INIT_LIST_HEAD(&delayed_tr->list);
3251 delayed_tr->handle = handle;
3252 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3253 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3254 "DELAYED:tr:handle(0x%04x), (open)\n",
3255 ioc->name, handle));
3256 return;
3257 }
3258
3259 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3260 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3261 ioc->tm_tr_volume_cb_idx));
3262 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3263 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3264 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3265 mpi_request->DevHandle = cpu_to_le16(handle);
3266 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3267 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3268}
3269
3270/**
3271 * _scsih_tm_volume_tr_complete - target reset completion
3272 * @ioc: per adapter object
3273 * @smid: system request message index
3274 * @msix_index: MSIX table index supplied by the OS
3275 * @reply: reply message frame(lower 32bit addr)
3276 * Context: interrupt time.
3277 *
3278 * Return 1 meaning mf should be freed from _base_interrupt
3279 * 0 means the mf is freed from this function.
3280 */
3281static u8
3282_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3283 u8 msix_index, u32 reply)
3284{
3285 u16 handle;
3286 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3287 Mpi2SCSITaskManagementReply_t *mpi_reply =
3288 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3289
Eric Moore3cb54692010-07-08 14:44:34 -06003290 if (ioc->shost_recovery || ioc->remove_host ||
3291 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303292 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3293 "progress!\n", __func__, ioc->name));
3294 return 1;
3295 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303296 if (unlikely(!mpi_reply)) {
3297 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3298 ioc->name, __FILE__, __LINE__, __func__);
3299 return 1;
3300 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303301 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3302 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3303 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3304 dewtprintk(ioc, printk("spurious interrupt: "
3305 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3306 le16_to_cpu(mpi_reply->DevHandle), smid));
3307 return 0;
3308 }
3309
3310 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3311 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3312 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3313 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3314 le32_to_cpu(mpi_reply->IOCLogInfo),
3315 le32_to_cpu(mpi_reply->TerminationCount)));
3316
3317 return _scsih_check_for_pending_tm(ioc, smid);
3318}
3319
3320/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303321 * _scsih_tm_tr_complete -
3322 * @ioc: per adapter object
3323 * @smid: system request message index
3324 * @msix_index: MSIX table index supplied by the OS
3325 * @reply: reply message frame(lower 32bit addr)
3326 * Context: interrupt time.
3327 *
3328 * This is the target reset completion routine.
3329 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003330 * handshake protocol with controller firmware.
3331 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303332 *
3333 * Return 1 meaning mf should be freed from _base_interrupt
3334 * 0 means the mf is freed from this function.
3335 */
3336static u8
3337_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3338 u32 reply)
3339{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303340 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303341 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303342 Mpi2SCSITaskManagementReply_t *mpi_reply =
3343 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3344 Mpi2SasIoUnitControlRequest_t *mpi_request;
3345 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303346 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303347
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303348 if (ioc->remove_host) {
3349 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3350 "removed\n", __func__, ioc->name));
3351 return 1;
3352 } else if (ioc->pci_error_recovery) {
3353 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3354 "error recovery\n", __func__, ioc->name));
3355 return 1;
3356 }
3357 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3358 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3359 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3360 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303361 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303362 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303363 if (unlikely(!mpi_reply)) {
3364 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3365 ioc->name, __FILE__, __LINE__, __func__);
3366 return 1;
3367 }
Kashyap, Desai1278b112010-03-09 17:34:13 +05303368 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3369 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3370 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3371 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3372 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3373 le16_to_cpu(mpi_reply->DevHandle), smid));
3374 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303375 }
3376
Kashyap, Desai1278b112010-03-09 17:34:13 +05303377 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3378 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3379 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3380 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3381 le32_to_cpu(mpi_reply->IOCLogInfo),
3382 le32_to_cpu(mpi_reply->TerminationCount)));
3383
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303384 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3385 if (!smid_sas_ctrl) {
3386 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3387 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303388 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303389 }
3390
Kashyap, Desai1278b112010-03-09 17:34:13 +05303391 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3392 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3393 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303394 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3395 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3396 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3397 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303398 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303399 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303400
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303401 return _scsih_check_for_pending_tm(ioc, smid);
3402}
3403
3404/**
3405 * _scsih_check_for_pending_tm - check for pending task management
3406 * @ioc: per adapter object
3407 * @smid: system request message index
3408 *
3409 * This will check delayed target reset list, and feed the
3410 * next reqeust.
3411 *
3412 * Return 1 meaning mf should be freed from _base_interrupt
3413 * 0 means the mf is freed from this function.
3414 */
3415static u8
3416_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3417{
3418 struct _tr_list *delayed_tr;
3419
3420 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3421 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3422 struct _tr_list, list);
3423 mpt2sas_base_free_smid(ioc, smid);
3424 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3425 list_del(&delayed_tr->list);
3426 kfree(delayed_tr);
3427 return 0;
3428 }
3429
Kashyap, Desai1278b112010-03-09 17:34:13 +05303430 if (!list_empty(&ioc->delayed_tr_list)) {
3431 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3432 struct _tr_list, list);
3433 mpt2sas_base_free_smid(ioc, smid);
3434 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3435 list_del(&delayed_tr->list);
3436 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303437 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303438 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303439
Kashyap, Desai1278b112010-03-09 17:34:13 +05303440 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303441}
3442
3443/**
Eric Moore635374e2009-03-09 01:21:12 -06003444 * _scsih_check_topo_delete_events - sanity check on topo events
3445 * @ioc: per adapter object
3446 * @event_data: the event data payload
3447 *
3448 * This routine added to better handle cable breaker.
3449 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003450 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003451 * add and delete events in a single shot. When there is a delete event
3452 * the routine will void any pending add events waiting in the event queue.
3453 *
3454 * Return nothing.
3455 */
3456static void
3457_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3458 Mpi2EventDataSasTopologyChangeList_t *event_data)
3459{
3460 struct fw_event_work *fw_event;
3461 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3462 u16 expander_handle;
3463 struct _sas_node *sas_expander;
3464 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303465 int i, reason_code;
3466 u16 handle;
3467
3468 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303469 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3470 if (!handle)
3471 continue;
3472 reason_code = event_data->PHY[i].PhyStatus &
3473 MPI2_EVENT_SAS_TOPO_RC_MASK;
3474 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3475 _scsih_tm_tr_send(ioc, handle);
3476 }
Eric Moore635374e2009-03-09 01:21:12 -06003477
3478 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3479 if (expander_handle < ioc->sas_hba.num_phys) {
3480 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3481 return;
3482 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303483 if (event_data->ExpStatus ==
3484 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3485 /* put expander attached devices into blocking state */
Eric Moore635374e2009-03-09 01:21:12 -06003486 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3487 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3488 expander_handle);
Eric Moore635374e2009-03-09 01:21:12 -06003489 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303490 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3491 do {
3492 handle = find_first_bit(ioc->blocking_handles,
3493 ioc->facts.MaxDevHandle);
3494 if (handle < ioc->facts.MaxDevHandle)
3495 _scsih_block_io_device(ioc, handle);
3496 } while (test_and_clear_bit(handle, ioc->blocking_handles));
Eric Moore635374e2009-03-09 01:21:12 -06003497 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3498 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3499
3500 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3501 return;
3502
3503 /* mark ignore flag for pending events */
3504 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3505 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3506 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3507 fw_event->ignore)
3508 continue;
3509 local_event_data = fw_event->event_data;
3510 if (local_event_data->ExpStatus ==
3511 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3512 local_event_data->ExpStatus ==
3513 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3514 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3515 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303516 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003517 "setting ignoring flag\n", ioc->name));
3518 fw_event->ignore = 1;
3519 }
3520 }
3521 }
3522 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3523}
3524
3525/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303526 * _scsih_set_volume_delete_flag - setting volume delete flag
3527 * @ioc: per adapter object
3528 * @handle: device handle
3529 *
3530 * This
3531 * Return nothing.
3532 */
3533static void
3534_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3535{
3536 struct _raid_device *raid_device;
3537 struct MPT2SAS_TARGET *sas_target_priv_data;
3538 unsigned long flags;
3539
3540 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3541 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3542 if (raid_device && raid_device->starget &&
3543 raid_device->starget->hostdata) {
3544 sas_target_priv_data =
3545 raid_device->starget->hostdata;
3546 sas_target_priv_data->deleted = 1;
3547 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3548 "setting delete flag: handle(0x%04x), "
3549 "wwid(0x%016llx)\n", ioc->name, handle,
3550 (unsigned long long) raid_device->wwid));
3551 }
3552 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3553}
3554
3555/**
3556 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3557 * @handle: input handle
3558 * @a: handle for volume a
3559 * @b: handle for volume b
3560 *
3561 * IR firmware only supports two raid volumes. The purpose of this
3562 * routine is to set the volume handle in either a or b. When the given
3563 * input handle is non-zero, or when a and b have not been set before.
3564 */
3565static void
3566_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3567{
3568 if (!handle || handle == *a || handle == *b)
3569 return;
3570 if (!*a)
3571 *a = handle;
3572 else if (!*b)
3573 *b = handle;
3574}
3575
3576/**
3577 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3578 * @ioc: per adapter object
3579 * @event_data: the event data payload
3580 * Context: interrupt time.
3581 *
3582 * This routine will send target reset to volume, followed by target
3583 * resets to the PDs. This is called when a PD has been removed, or
3584 * volume has been deleted or removed. When the target reset is sent
3585 * to volume, the PD target resets need to be queued to start upon
3586 * completion of the volume target reset.
3587 *
3588 * Return nothing.
3589 */
3590static void
3591_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3592 Mpi2EventDataIrConfigChangeList_t *event_data)
3593{
3594 Mpi2EventIrConfigElement_t *element;
3595 int i;
3596 u16 handle, volume_handle, a, b;
3597 struct _tr_list *delayed_tr;
3598
3599 a = 0;
3600 b = 0;
3601
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303602 if (ioc->is_warpdrive)
3603 return;
3604
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303605 /* Volume Resets for Deleted or Removed */
3606 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3607 for (i = 0; i < event_data->NumElements; i++, element++) {
3608 if (element->ReasonCode ==
3609 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3610 element->ReasonCode ==
3611 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3612 volume_handle = le16_to_cpu(element->VolDevHandle);
3613 _scsih_set_volume_delete_flag(ioc, volume_handle);
3614 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3615 }
3616 }
3617
3618 /* Volume Resets for UNHIDE events */
3619 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3620 for (i = 0; i < event_data->NumElements; i++, element++) {
3621 if (le32_to_cpu(event_data->Flags) &
3622 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3623 continue;
3624 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3625 volume_handle = le16_to_cpu(element->VolDevHandle);
3626 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3627 }
3628 }
3629
3630 if (a)
3631 _scsih_tm_tr_volume_send(ioc, a);
3632 if (b)
3633 _scsih_tm_tr_volume_send(ioc, b);
3634
3635 /* PD target resets */
3636 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3637 for (i = 0; i < event_data->NumElements; i++, element++) {
3638 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3639 continue;
3640 handle = le16_to_cpu(element->PhysDiskDevHandle);
3641 volume_handle = le16_to_cpu(element->VolDevHandle);
3642 clear_bit(handle, ioc->pd_handles);
3643 if (!volume_handle)
3644 _scsih_tm_tr_send(ioc, handle);
3645 else if (volume_handle == a || volume_handle == b) {
3646 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3647 BUG_ON(!delayed_tr);
3648 INIT_LIST_HEAD(&delayed_tr->list);
3649 delayed_tr->handle = handle;
3650 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3651 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3652 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3653 handle));
3654 } else
3655 _scsih_tm_tr_send(ioc, handle);
3656 }
3657}
3658
3659
3660/**
3661 * _scsih_check_volume_delete_events - set delete flag for volumes
3662 * @ioc: per adapter object
3663 * @event_data: the event data payload
3664 * Context: interrupt time.
3665 *
3666 * This will handle the case when the cable connected to entire volume is
3667 * pulled. We will take care of setting the deleted flag so normal IO will
3668 * not be sent.
3669 *
3670 * Return nothing.
3671 */
3672static void
3673_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3674 Mpi2EventDataIrVolume_t *event_data)
3675{
3676 u32 state;
3677
3678 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3679 return;
3680 state = le32_to_cpu(event_data->NewValue);
3681 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3682 MPI2_RAID_VOL_STATE_FAILED)
3683 _scsih_set_volume_delete_flag(ioc,
3684 le16_to_cpu(event_data->VolDevHandle));
3685}
3686
3687/**
Eric Moore635374e2009-03-09 01:21:12 -06003688 * _scsih_flush_running_cmds - completing outstanding commands.
3689 * @ioc: per adapter object
3690 *
3691 * The flushing out of all pending scmd commands following host reset,
3692 * where all IO is dropped to the floor.
3693 *
3694 * Return nothing.
3695 */
3696static void
3697_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3698{
3699 struct scsi_cmnd *scmd;
3700 u16 smid;
3701 u16 count = 0;
3702
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303703 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303704 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003705 if (!scmd)
3706 continue;
3707 count++;
3708 mpt2sas_base_free_smid(ioc, smid);
3709 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003710 if (ioc->pci_error_recovery)
3711 scmd->result = DID_NO_CONNECT << 16;
3712 else
3713 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003714 scmd->scsi_done(scmd);
3715 }
3716 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3717 ioc->name, count));
3718}
3719
3720/**
Eric Moore3c621b32009-05-18 12:59:41 -06003721 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3722 * @scmd: pointer to scsi command object
3723 * @mpi_request: pointer to the SCSI_IO reqest message frame
3724 *
3725 * Supporting protection 1 and 3.
3726 *
3727 * Returns nothing
3728 */
3729static void
3730_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3731{
3732 u16 eedp_flags;
3733 unsigned char prot_op = scsi_get_prot_op(scmd);
3734 unsigned char prot_type = scsi_get_prot_type(scmd);
3735
Eric Moored334aa72010-04-22 10:47:40 -06003736 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003737 return;
3738
3739 if (prot_op == SCSI_PROT_READ_STRIP)
3740 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3741 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3742 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3743 else
3744 return;
3745
Eric Moore3c621b32009-05-18 12:59:41 -06003746 switch (prot_type) {
3747 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003748 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003749
3750 /*
3751 * enable ref/guard checking
3752 * auto increment ref tag
3753 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303754 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003755 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3756 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3757 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3758 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003759 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003760
Eric Moore3c621b32009-05-18 12:59:41 -06003761 case SCSI_PROT_DIF_TYPE3:
3762
3763 /*
3764 * enable guard checking
3765 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303766 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003767 break;
3768 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303769 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3770 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003771}
3772
3773/**
3774 * _scsih_eedp_error_handling - return sense code for EEDP errors
3775 * @scmd: pointer to scsi command object
3776 * @ioc_status: ioc status
3777 *
3778 * Returns nothing
3779 */
3780static void
3781_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3782{
3783 u8 ascq;
Eric Moore3c621b32009-05-18 12:59:41 -06003784
3785 switch (ioc_status) {
3786 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3787 ascq = 0x01;
3788 break;
3789 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3790 ascq = 0x02;
3791 break;
3792 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3793 ascq = 0x03;
3794 break;
3795 default:
3796 ascq = 0x00;
3797 break;
3798 }
3799
Martin K. Petersen9b5cd132012-08-28 14:29:36 -04003800 scsi_build_sense_buffer(0, scmd->sense_buffer, ILLEGAL_REQUEST, 0x10, ascq);
3801 scmd->result = DRIVER_SENSE << 24 | (DID_ABORT << 16) |
Eric Moore3c621b32009-05-18 12:59:41 -06003802 SAM_STAT_CHECK_CONDITION;
3803}
3804
3805/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303806 * _scsih_scsi_direct_io_get - returns direct io flag
3807 * @ioc: per adapter object
3808 * @smid: system request message index
3809 *
3810 * Returns the smid stored scmd pointer.
3811 */
3812static inline u8
3813_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3814{
3815 return ioc->scsi_lookup[smid - 1].direct_io;
3816}
3817
3818/**
3819 * _scsih_scsi_direct_io_set - sets direct io flag
3820 * @ioc: per adapter object
3821 * @smid: system request message index
3822 * @direct_io: Zero or non-zero value to set in the direct_io flag
3823 *
3824 * Returns Nothing.
3825 */
3826static inline void
3827_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3828{
3829 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3830}
3831
3832
3833/**
3834 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3835 * @ioc: per adapter object
3836 * @scmd: pointer to scsi command object
3837 * @raid_device: pointer to raid device data structure
3838 * @mpi_request: pointer to the SCSI_IO reqest message frame
3839 * @smid: system request message index
3840 *
3841 * Returns nothing
3842 */
3843static void
3844_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3845 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3846 u16 smid)
3847{
3848 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3849 u32 stripe_sz, stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303850 u8 num_pds, *cdb_ptr, i;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303851 u8 cdb0 = scmd->cmnd[0];
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303852 u64 v_llba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303853
3854 /*
3855 * Try Direct I/O to RAID memeber disks
3856 */
3857 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3858 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3859 cdb_ptr = mpi_request->CDB.CDB32;
3860
3861 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3862 | cdb_ptr[5])) {
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303863 io_size = scsi_bufflen(scmd) >>
3864 raid_device->block_exponent;
3865 i = (cdb0 < READ_16) ? 2 : 6;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303866 /* get virtual lba */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303867 v_lba = be32_to_cpu(*(__be32 *)(&cdb_ptr[i]));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303868
3869 if (((u64)v_lba + (u64)io_size - 1) <=
3870 (u32)raid_device->max_lba) {
3871 stripe_sz = raid_device->stripe_sz;
3872 stripe_exp = raid_device->stripe_exponent;
3873 stripe_off = v_lba & (stripe_sz - 1);
3874
3875 /* Check whether IO falls within a stripe */
3876 if ((stripe_off + io_size) <= stripe_sz) {
3877 num_pds = raid_device->num_pds;
3878 p_lba = v_lba >> stripe_exp;
3879 stripe_unit = p_lba / num_pds;
3880 column = p_lba % num_pds;
3881 p_lba = (stripe_unit << stripe_exp) +
3882 stripe_off;
3883 mpi_request->DevHandle =
3884 cpu_to_le16(raid_device->
3885 pd_handle[column]);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303886 (*(__be32 *)(&cdb_ptr[i])) =
3887 cpu_to_be32(p_lba);
3888 /*
3889 * WD: To indicate this I/O is directI/O
3890 */
3891 _scsih_scsi_direct_io_set(ioc, smid, 1);
3892 }
3893 }
3894 } else {
3895 io_size = scsi_bufflen(scmd) >>
3896 raid_device->block_exponent;
3897 /* get virtual lba */
3898 v_llba = be64_to_cpu(*(__be64 *)(&cdb_ptr[2]));
3899
3900 if ((v_llba + (u64)io_size - 1) <=
3901 raid_device->max_lba) {
3902 stripe_sz = raid_device->stripe_sz;
3903 stripe_exp = raid_device->stripe_exponent;
3904 stripe_off = (u32) (v_llba & (stripe_sz - 1));
3905
3906 /* Check whether IO falls within a stripe */
3907 if ((stripe_off + io_size) <= stripe_sz) {
3908 num_pds = raid_device->num_pds;
3909 p_lba = (u32)(v_llba >> stripe_exp);
3910 stripe_unit = p_lba / num_pds;
3911 column = p_lba % num_pds;
3912 p_lba = (stripe_unit << stripe_exp) +
3913 stripe_off;
3914 mpi_request->DevHandle =
3915 cpu_to_le16(raid_device->
3916 pd_handle[column]);
3917 (*(__be64 *)(&cdb_ptr[2])) =
3918 cpu_to_be64((u64)p_lba);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303919 /*
3920 * WD: To indicate this I/O is directI/O
3921 */
3922 _scsih_scsi_direct_io_set(ioc, smid, 1);
3923 }
3924 }
3925 }
3926 }
3927}
3928
3929/**
Eric Moored5d135b2009-05-18 13:02:08 -06003930 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003931 * @scmd: pointer to scsi command object
3932 * @done: function pointer to be invoked on completion
3933 *
3934 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3935 *
3936 * Returns 0 on success. If there's a failure, return either:
3937 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3938 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3939 */
3940static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003941_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003942{
3943 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3944 struct MPT2SAS_DEVICE *sas_device_priv_data;
3945 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303946 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003947 Mpi2SCSIIORequest_t *mpi_request;
3948 u32 mpi_control;
3949 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003950
3951 scmd->scsi_done = done;
3952 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303953 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003954 scmd->result = DID_NO_CONNECT << 16;
3955 scmd->scsi_done(scmd);
3956 return 0;
3957 }
3958
Kashyap, Desai78215782011-06-14 10:57:08 +05303959 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003960 scmd->result = DID_NO_CONNECT << 16;
3961 scmd->scsi_done(scmd);
3962 return 0;
3963 }
3964
Eric Moore635374e2009-03-09 01:21:12 -06003965 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303966 /* invalid device handle */
3967 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003968 scmd->result = DID_NO_CONNECT << 16;
3969 scmd->scsi_done(scmd);
3970 return 0;
3971 }
3972
Kashyap, Desai130b9582010-04-08 17:54:32 +05303973 /* host recovery or link resets sent via IOCTLs */
3974 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003975 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003976 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303977 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3978 return SCSI_MLQUEUE_DEVICE_BUSY;
3979 /* device has been deleted */
3980 else if (sas_target_priv_data->deleted) {
3981 scmd->result = DID_NO_CONNECT << 16;
3982 scmd->scsi_done(scmd);
3983 return 0;
3984 }
Eric Moore635374e2009-03-09 01:21:12 -06003985
3986 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3987 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3988 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3989 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3990 else
3991 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3992
3993 /* set tags */
3994 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3995 if (scmd->device->tagged_supported) {
3996 if (scmd->device->ordered_tags)
3997 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3998 else
3999 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
4000 } else
4001/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
4002/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
4003 */
4004 mpi_control |= (0x500);
4005
4006 } else
4007 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304008 /* Make sure Device is not raid volume.
4009 * We do not expose raid functionality to upper layer for warpdrive.
4010 */
4011 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06004012 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06004013 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4014
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304015 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06004016 if (!smid) {
4017 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
4018 ioc->name, __func__);
4019 goto out;
4020 }
4021 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4022 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06004023 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06004024 if (scmd->cmd_len == 32)
4025 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06004026 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4027 if (sas_device_priv_data->sas_target->flags &
4028 MPT_TARGET_FLAGS_RAID_COMPONENT)
4029 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4030 else
4031 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4032 mpi_request->DevHandle =
4033 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4034 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4035 mpi_request->Control = cpu_to_le32(mpi_control);
4036 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4037 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4038 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4039 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05304040 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004041 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4042 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
4043 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304044 mpi_request->VF_ID = 0; /* TODO */
4045 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004046 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4047 mpi_request->LUN);
4048 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4049
4050 if (!mpi_request->DataLength) {
4051 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
4052 } else {
4053 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
4054 mpt2sas_base_free_smid(ioc, smid);
4055 goto out;
4056 }
4057 }
4058
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304059 raid_device = sas_target_priv_data->raid_device;
4060 if (raid_device && raid_device->direct_io_enabled)
4061 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4062 smid);
4063
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304064 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4065 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304066 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304067 else
4068 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004069 return 0;
4070
4071 out:
4072 return SCSI_MLQUEUE_HOST_BUSY;
4073}
4074
Jeff Garzikf2812332010-11-16 02:10:29 -05004075static DEF_SCSI_QCMD(_scsih_qcmd)
4076
Eric Moore635374e2009-03-09 01:21:12 -06004077/**
4078 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4079 * @sense_buffer: sense data returned by target
4080 * @data: normalized skey/asc/ascq
4081 *
4082 * Return nothing.
4083 */
4084static void
4085_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4086{
4087 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4088 /* descriptor format */
4089 data->skey = sense_buffer[1] & 0x0F;
4090 data->asc = sense_buffer[2];
4091 data->ascq = sense_buffer[3];
4092 } else {
4093 /* fixed format */
4094 data->skey = sense_buffer[2] & 0x0F;
4095 data->asc = sense_buffer[12];
4096 data->ascq = sense_buffer[13];
4097 }
4098}
4099
4100#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4101/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004102 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004103 * @ioc: per adapter object
4104 * @scmd: pointer to scsi command object
4105 * @mpi_reply: reply mf payload returned from firmware
4106 *
4107 * scsi_status - SCSI Status code returned from target device
4108 * scsi_state - state info associated with SCSI_IO determined by ioc
4109 * ioc_status - ioc supplied status info
4110 *
4111 * Return nothing.
4112 */
4113static void
4114_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4115 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4116{
4117 u32 response_info;
4118 u8 *response_bytes;
4119 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4120 MPI2_IOCSTATUS_MASK;
4121 u8 scsi_state = mpi_reply->SCSIState;
4122 u8 scsi_status = mpi_reply->SCSIStatus;
4123 char *desc_ioc_state = NULL;
4124 char *desc_scsi_status = NULL;
4125 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304126 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304127 struct _sas_device *sas_device = NULL;
4128 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304129 struct scsi_target *starget = scmd->device->sdev_target;
4130 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304131 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304132
4133 if (!priv_target)
4134 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304135
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304136 if (ioc->hide_ir_msg)
4137 device_str = "WarpDrive";
4138 else
4139 device_str = "volume";
4140
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304141 if (log_info == 0x31170000)
4142 return;
Eric Moore635374e2009-03-09 01:21:12 -06004143
4144 switch (ioc_status) {
4145 case MPI2_IOCSTATUS_SUCCESS:
4146 desc_ioc_state = "success";
4147 break;
4148 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4149 desc_ioc_state = "invalid function";
4150 break;
4151 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4152 desc_ioc_state = "scsi recovered error";
4153 break;
4154 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4155 desc_ioc_state = "scsi invalid dev handle";
4156 break;
4157 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4158 desc_ioc_state = "scsi device not there";
4159 break;
4160 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4161 desc_ioc_state = "scsi data overrun";
4162 break;
4163 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4164 desc_ioc_state = "scsi data underrun";
4165 break;
4166 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4167 desc_ioc_state = "scsi io data error";
4168 break;
4169 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4170 desc_ioc_state = "scsi protocol error";
4171 break;
4172 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4173 desc_ioc_state = "scsi task terminated";
4174 break;
4175 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4176 desc_ioc_state = "scsi residual mismatch";
4177 break;
4178 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4179 desc_ioc_state = "scsi task mgmt failed";
4180 break;
4181 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4182 desc_ioc_state = "scsi ioc terminated";
4183 break;
4184 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4185 desc_ioc_state = "scsi ext terminated";
4186 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004187 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4188 desc_ioc_state = "eedp guard error";
4189 break;
4190 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4191 desc_ioc_state = "eedp ref tag error";
4192 break;
4193 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4194 desc_ioc_state = "eedp app tag error";
4195 break;
Eric Moore635374e2009-03-09 01:21:12 -06004196 default:
4197 desc_ioc_state = "unknown";
4198 break;
4199 }
4200
4201 switch (scsi_status) {
4202 case MPI2_SCSI_STATUS_GOOD:
4203 desc_scsi_status = "good";
4204 break;
4205 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4206 desc_scsi_status = "check condition";
4207 break;
4208 case MPI2_SCSI_STATUS_CONDITION_MET:
4209 desc_scsi_status = "condition met";
4210 break;
4211 case MPI2_SCSI_STATUS_BUSY:
4212 desc_scsi_status = "busy";
4213 break;
4214 case MPI2_SCSI_STATUS_INTERMEDIATE:
4215 desc_scsi_status = "intermediate";
4216 break;
4217 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4218 desc_scsi_status = "intermediate condmet";
4219 break;
4220 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4221 desc_scsi_status = "reservation conflict";
4222 break;
4223 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4224 desc_scsi_status = "command terminated";
4225 break;
4226 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4227 desc_scsi_status = "task set full";
4228 break;
4229 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4230 desc_scsi_status = "aca active";
4231 break;
4232 case MPI2_SCSI_STATUS_TASK_ABORTED:
4233 desc_scsi_status = "task aborted";
4234 break;
4235 default:
4236 desc_scsi_status = "unknown";
4237 break;
4238 }
4239
4240 desc_scsi_state[0] = '\0';
4241 if (!scsi_state)
4242 desc_scsi_state = " ";
4243 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4244 strcat(desc_scsi_state, "response info ");
4245 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4246 strcat(desc_scsi_state, "state terminated ");
4247 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4248 strcat(desc_scsi_state, "no status ");
4249 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4250 strcat(desc_scsi_state, "autosense failed ");
4251 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4252 strcat(desc_scsi_state, "autosense valid ");
4253
4254 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304255
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304256 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304257 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4258 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304259 } else {
4260 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4261 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4262 priv_target->sas_address);
4263 if (sas_device) {
4264 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4265 "phy(%d)\n", ioc->name, sas_device->sas_address,
4266 sas_device->phy);
4267 printk(MPT2SAS_WARN_FMT
4268 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4269 ioc->name, sas_device->enclosure_logical_id,
4270 sas_device->slot);
4271 }
4272 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304273 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304274
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304275 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4276 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4277 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004278 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4279 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4280 scsi_get_resid(scmd));
4281 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4282 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4283 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4284 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4285 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4286 scsi_status, desc_scsi_state, scsi_state);
4287
4288 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4289 struct sense_info data;
4290 _scsih_normalize_sense(scmd->sense_buffer, &data);
4291 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304292 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4293 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004294 }
4295
4296 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4297 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4298 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304299 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004300 }
4301}
4302#endif
4303
4304/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304305 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004306 * @ioc: per adapter object
4307 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304308 * Context: process
4309 *
4310 * Return nothing.
4311 */
4312static void
4313_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4314{
4315 Mpi2SepReply_t mpi_reply;
4316 Mpi2SepRequest_t mpi_request;
4317
4318 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4319 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4320 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4321 mpi_request.SlotStatus =
4322 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4323 mpi_request.DevHandle = cpu_to_le16(handle);
4324 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4325 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4326 &mpi_request)) != 0) {
4327 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4328 __FILE__, __LINE__, __func__);
4329 return;
4330 }
4331
4332 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4333 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4334 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4335 le16_to_cpu(mpi_reply.IOCStatus),
4336 le32_to_cpu(mpi_reply.IOCLogInfo)));
4337 return;
4338 }
4339}
4340
4341/**
4342 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4343 * @ioc: per adapter object
4344 * @handle: device handle
4345 * Context: interrupt.
4346 *
4347 * Return nothing.
4348 */
4349static void
4350_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4351{
4352 struct fw_event_work *fw_event;
4353
4354 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4355 if (!fw_event)
4356 return;
4357 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4358 fw_event->device_handle = handle;
4359 fw_event->ioc = ioc;
4360 _scsih_fw_event_add(ioc, fw_event);
4361}
4362
4363/**
4364 * _scsih_smart_predicted_fault - process smart errors
4365 * @ioc: per adapter object
4366 * @handle: device handle
4367 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004368 *
4369 * Return nothing.
4370 */
4371static void
4372_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4373{
Eric Moore635374e2009-03-09 01:21:12 -06004374 struct scsi_target *starget;
4375 struct MPT2SAS_TARGET *sas_target_priv_data;
4376 Mpi2EventNotificationReply_t *event_reply;
4377 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4378 struct _sas_device *sas_device;
4379 ssize_t sz;
4380 unsigned long flags;
4381
4382 /* only handle non-raid devices */
4383 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4384 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4385 if (!sas_device) {
4386 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4387 return;
4388 }
4389 starget = sas_device->starget;
4390 sas_target_priv_data = starget->hostdata;
4391
4392 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4393 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4394 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4395 return;
4396 }
4397 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4398 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4399
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304400 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4401 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004402
4403 /* insert into event log */
4404 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4405 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b42011-11-07 22:05:21 +11004406 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004407 if (!event_reply) {
4408 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4409 ioc->name, __FILE__, __LINE__, __func__);
4410 return;
4411 }
4412
4413 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4414 event_reply->Event =
4415 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4416 event_reply->MsgLength = sz/4;
4417 event_reply->EventDataLength =
4418 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4419 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4420 event_reply->EventData;
4421 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4422 event_data->ASC = 0x5D;
4423 event_data->DevHandle = cpu_to_le16(handle);
4424 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4425 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4426 kfree(event_reply);
4427}
4428
4429/**
Eric Moored5d135b2009-05-18 13:02:08 -06004430 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004431 * @ioc: per adapter object
4432 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304433 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004434 * @reply: reply message frame(lower 32bit addr)
4435 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304436 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004437 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304438 * Return 1 meaning mf should be freed from _base_interrupt
4439 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004440 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304441static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304442_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004443{
4444 Mpi2SCSIIORequest_t *mpi_request;
4445 Mpi2SCSIIOReply_t *mpi_reply;
4446 struct scsi_cmnd *scmd;
4447 u16 ioc_status;
4448 u32 xfer_cnt;
4449 u8 scsi_state;
4450 u8 scsi_status;
4451 u32 log_info;
4452 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304453 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304454 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004455
4456 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304457 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004458 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304459 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004460
4461 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4462
4463 if (mpi_reply == NULL) {
4464 scmd->result = DID_OK << 16;
4465 goto out;
4466 }
4467
4468 sas_device_priv_data = scmd->device->hostdata;
4469 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4470 sas_device_priv_data->sas_target->deleted) {
4471 scmd->result = DID_NO_CONNECT << 16;
4472 goto out;
4473 }
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304474 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304475 /*
4476 * WARPDRIVE: If direct_io is set then it is directIO,
4477 * the failed direct I/O should be redirected to volume
4478 */
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304479 if (_scsih_scsi_direct_io_get(ioc, smid) &&
4480 ((ioc_status & MPI2_IOCSTATUS_MASK)
4481 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304482 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4483 ioc->scsi_lookup[smid - 1].scmd = scmd;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304484 _scsih_scsi_direct_io_set(ioc, smid, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304485 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304486 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4487 mpi_request->DevHandle =
4488 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4489 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4490 sas_device_priv_data->sas_target->handle);
4491 return 0;
4492 }
4493
Eric Moore635374e2009-03-09 01:21:12 -06004494
4495 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304496 scsi_state = mpi_reply->SCSIState;
4497 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4498 response_code =
4499 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004500 if (!sas_device_priv_data->tlr_snoop_check) {
4501 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304502 /* Make sure Device is not raid volume.
4503 * We do not expose raid functionality to upper layer for warpdrive.
4504 */
4505 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304506 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304507 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4508 sas_disable_tlr(scmd->device);
4509 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4510 }
Eric Moore635374e2009-03-09 01:21:12 -06004511 }
4512
4513 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4514 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
Eric Moore635374e2009-03-09 01:21:12 -06004515 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4516 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4517 else
4518 log_info = 0;
4519 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004520 scsi_status = mpi_reply->SCSIStatus;
4521
4522 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4523 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4524 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4525 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4526 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4527 }
4528
4529 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4530 struct sense_info data;
4531 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4532 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004533 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004534 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004535 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004536 _scsih_normalize_sense(scmd->sense_buffer, &data);
4537 /* failure prediction threshold exceeded */
4538 if (data.asc == 0x5D)
4539 _scsih_smart_predicted_fault(ioc,
4540 le16_to_cpu(mpi_reply->DevHandle));
4541 }
4542
4543 switch (ioc_status) {
4544 case MPI2_IOCSTATUS_BUSY:
4545 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4546 scmd->result = SAM_STAT_BUSY;
4547 break;
4548
4549 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4550 scmd->result = DID_NO_CONNECT << 16;
4551 break;
4552
4553 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4554 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304555 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4556 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004557 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304558 scmd->result = DID_SOFT_ERROR << 16;
4559 break;
Eric Moore635374e2009-03-09 01:21:12 -06004560 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4561 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4562 scmd->result = DID_RESET << 16;
4563 break;
4564
4565 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4566 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4567 scmd->result = DID_SOFT_ERROR << 16;
4568 else
4569 scmd->result = (DID_OK << 16) | scsi_status;
4570 break;
4571
4572 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4573 scmd->result = (DID_OK << 16) | scsi_status;
4574
4575 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4576 break;
4577
4578 if (xfer_cnt < scmd->underflow) {
4579 if (scsi_status == SAM_STAT_BUSY)
4580 scmd->result = SAM_STAT_BUSY;
4581 else
4582 scmd->result = DID_SOFT_ERROR << 16;
4583 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4584 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4585 scmd->result = DID_SOFT_ERROR << 16;
4586 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4587 scmd->result = DID_RESET << 16;
4588 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4589 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4590 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4591 scmd->result = (DRIVER_SENSE << 24) |
4592 SAM_STAT_CHECK_CONDITION;
4593 scmd->sense_buffer[0] = 0x70;
4594 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4595 scmd->sense_buffer[12] = 0x20;
4596 scmd->sense_buffer[13] = 0;
4597 }
4598 break;
4599
4600 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4601 scsi_set_resid(scmd, 0);
4602 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4603 case MPI2_IOCSTATUS_SUCCESS:
4604 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304605 if (response_code ==
4606 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4607 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4608 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004609 scmd->result = DID_SOFT_ERROR << 16;
4610 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4611 scmd->result = DID_RESET << 16;
4612 break;
4613
Eric Moore3c621b32009-05-18 12:59:41 -06004614 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4615 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4616 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4617 _scsih_eedp_error_handling(scmd, ioc_status);
4618 break;
Eric Moore635374e2009-03-09 01:21:12 -06004619 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4620 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4621 case MPI2_IOCSTATUS_INVALID_SGL:
4622 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4623 case MPI2_IOCSTATUS_INVALID_FIELD:
4624 case MPI2_IOCSTATUS_INVALID_STATE:
4625 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4626 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4627 default:
4628 scmd->result = DID_SOFT_ERROR << 16;
4629 break;
4630
4631 }
4632
4633#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4634 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4635 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4636#endif
4637
4638 out:
4639 scsi_dma_unmap(scmd);
4640 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304641 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004642}
4643
4644/**
Eric Moore635374e2009-03-09 01:21:12 -06004645 * _scsih_sas_host_refresh - refreshing sas host object contents
4646 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004647 * Context: user
4648 *
4649 * During port enable, fw will send topology events for every device. Its
4650 * possible that the handles may change from the previous setting, so this
4651 * code keeping handles updating if changed.
4652 *
4653 * Return nothing.
4654 */
4655static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304656_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004657{
4658 u16 sz;
4659 u16 ioc_status;
4660 int i;
4661 Mpi2ConfigReply_t mpi_reply;
4662 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304663 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304664 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004665
4666 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4667 "updating handles for sas_host(0x%016llx)\n",
4668 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4669
4670 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4671 * sizeof(Mpi2SasIOUnit0PhyData_t));
4672 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4673 if (!sas_iounit_pg0) {
4674 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4675 ioc->name, __FILE__, __LINE__, __func__);
4676 return;
4677 }
Eric Moore635374e2009-03-09 01:21:12 -06004678
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304679 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4680 sas_iounit_pg0, sz)) != 0)
4681 goto out;
4682 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4683 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4684 goto out;
4685 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304686 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304687 if (i == 0)
4688 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4689 PhyData[0].ControllerDevHandle);
4690 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4691 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4692 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304693 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4694 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304695 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304696 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304697 }
Eric Moore635374e2009-03-09 01:21:12 -06004698 out:
4699 kfree(sas_iounit_pg0);
4700}
4701
4702/**
4703 * _scsih_sas_host_add - create sas host object
4704 * @ioc: per adapter object
4705 *
4706 * Creating host side data object, stored in ioc->sas_hba
4707 *
4708 * Return nothing.
4709 */
4710static void
4711_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4712{
4713 int i;
4714 Mpi2ConfigReply_t mpi_reply;
4715 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4716 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4717 Mpi2SasPhyPage0_t phy_pg0;
4718 Mpi2SasDevicePage0_t sas_device_pg0;
4719 Mpi2SasEnclosurePage0_t enclosure_pg0;
4720 u16 ioc_status;
4721 u16 sz;
4722 u16 device_missing_delay;
4723
4724 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4725 if (!ioc->sas_hba.num_phys) {
4726 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4727 ioc->name, __FILE__, __LINE__, __func__);
4728 return;
4729 }
4730
4731 /* sas_iounit page 0 */
4732 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4733 sizeof(Mpi2SasIOUnit0PhyData_t));
4734 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4735 if (!sas_iounit_pg0) {
4736 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4737 ioc->name, __FILE__, __LINE__, __func__);
4738 return;
4739 }
4740 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4741 sas_iounit_pg0, sz))) {
4742 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4743 ioc->name, __FILE__, __LINE__, __func__);
4744 goto out;
4745 }
4746 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4747 MPI2_IOCSTATUS_MASK;
4748 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4749 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4750 ioc->name, __FILE__, __LINE__, __func__);
4751 goto out;
4752 }
4753
4754 /* sas_iounit page 1 */
4755 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4756 sizeof(Mpi2SasIOUnit1PhyData_t));
4757 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4758 if (!sas_iounit_pg1) {
4759 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4760 ioc->name, __FILE__, __LINE__, __func__);
4761 goto out;
4762 }
4763 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4764 sas_iounit_pg1, sz))) {
4765 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4766 ioc->name, __FILE__, __LINE__, __func__);
4767 goto out;
4768 }
4769 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4770 MPI2_IOCSTATUS_MASK;
4771 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4772 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4773 ioc->name, __FILE__, __LINE__, __func__);
4774 goto out;
4775 }
4776
4777 ioc->io_missing_delay =
4778 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4779 device_missing_delay =
4780 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4781 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4782 ioc->device_missing_delay = (device_missing_delay &
4783 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4784 else
4785 ioc->device_missing_delay = device_missing_delay &
4786 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4787
4788 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4789 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4790 sizeof(struct _sas_phy), GFP_KERNEL);
4791 if (!ioc->sas_hba.phy) {
4792 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4793 ioc->name, __FILE__, __LINE__, __func__);
4794 goto out;
4795 }
4796 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4797 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4798 i))) {
4799 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4800 ioc->name, __FILE__, __LINE__, __func__);
4801 goto out;
4802 }
4803 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4804 MPI2_IOCSTATUS_MASK;
4805 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4806 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4807 ioc->name, __FILE__, __LINE__, __func__);
4808 goto out;
4809 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304810
4811 if (i == 0)
4812 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4813 PhyData[0].ControllerDevHandle);
4814 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004815 ioc->sas_hba.phy[i].phy_id = i;
4816 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4817 phy_pg0, ioc->sas_hba.parent_dev);
4818 }
4819 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304820 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004821 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4822 ioc->name, __FILE__, __LINE__, __func__);
4823 goto out;
4824 }
Eric Moore635374e2009-03-09 01:21:12 -06004825 ioc->sas_hba.enclosure_handle =
4826 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4827 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4828 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4829 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4830 (unsigned long long) ioc->sas_hba.sas_address,
4831 ioc->sas_hba.num_phys) ;
4832
4833 if (ioc->sas_hba.enclosure_handle) {
4834 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4835 &enclosure_pg0,
4836 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4837 ioc->sas_hba.enclosure_handle))) {
4838 ioc->sas_hba.enclosure_logical_id =
4839 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4840 }
4841 }
4842
4843 out:
4844 kfree(sas_iounit_pg1);
4845 kfree(sas_iounit_pg0);
4846}
4847
4848/**
4849 * _scsih_expander_add - creating expander object
4850 * @ioc: per adapter object
4851 * @handle: expander handle
4852 *
4853 * Creating expander object, stored in ioc->sas_expander_list.
4854 *
4855 * Return 0 for success, else error.
4856 */
4857static int
4858_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4859{
4860 struct _sas_node *sas_expander;
4861 Mpi2ConfigReply_t mpi_reply;
4862 Mpi2ExpanderPage0_t expander_pg0;
4863 Mpi2ExpanderPage1_t expander_pg1;
4864 Mpi2SasEnclosurePage0_t enclosure_pg0;
4865 u32 ioc_status;
4866 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304867 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004868 int i;
4869 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304870 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004871 int rc = 0;
4872
4873 if (!handle)
4874 return -1;
4875
Eric Moore3cb54692010-07-08 14:44:34 -06004876 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304877 return -1;
4878
Eric Moore635374e2009-03-09 01:21:12 -06004879 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4880 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4881 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4882 ioc->name, __FILE__, __LINE__, __func__);
4883 return -1;
4884 }
4885
4886 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4887 MPI2_IOCSTATUS_MASK;
4888 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4889 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4890 ioc->name, __FILE__, __LINE__, __func__);
4891 return -1;
4892 }
4893
4894 /* handle out of order topology events */
4895 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304896 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4897 != 0) {
4898 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4899 ioc->name, __FILE__, __LINE__, __func__);
4900 return -1;
4901 }
4902 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004903 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304904 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4905 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004906 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4907 if (!sas_expander) {
4908 rc = _scsih_expander_add(ioc, parent_handle);
4909 if (rc != 0)
4910 return rc;
4911 }
4912 }
4913
Eric Moore635374e2009-03-09 01:21:12 -06004914 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304915 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004916 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4917 sas_address);
4918 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4919
4920 if (sas_expander)
4921 return 0;
4922
4923 sas_expander = kzalloc(sizeof(struct _sas_node),
4924 GFP_KERNEL);
4925 if (!sas_expander) {
4926 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4927 ioc->name, __FILE__, __LINE__, __func__);
4928 return -1;
4929 }
4930
4931 sas_expander->handle = handle;
4932 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304933 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004934 sas_expander->sas_address = sas_address;
4935
4936 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4937 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304938 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004939 sas_expander->sas_address, sas_expander->num_phys);
4940
4941 if (!sas_expander->num_phys)
4942 goto out_fail;
4943 sas_expander->phy = kcalloc(sas_expander->num_phys,
4944 sizeof(struct _sas_phy), GFP_KERNEL);
4945 if (!sas_expander->phy) {
4946 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4947 ioc->name, __FILE__, __LINE__, __func__);
4948 rc = -1;
4949 goto out_fail;
4950 }
4951
4952 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4953 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304954 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004955 if (!mpt2sas_port) {
4956 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4957 ioc->name, __FILE__, __LINE__, __func__);
4958 rc = -1;
4959 goto out_fail;
4960 }
4961 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4962
4963 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4964 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4965 &expander_pg1, i, handle))) {
4966 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4967 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304968 rc = -1;
4969 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004970 }
4971 sas_expander->phy[i].handle = handle;
4972 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304973
4974 if ((mpt2sas_transport_add_expander_phy(ioc,
4975 &sas_expander->phy[i], expander_pg1,
4976 sas_expander->parent_dev))) {
4977 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4978 ioc->name, __FILE__, __LINE__, __func__);
4979 rc = -1;
4980 goto out_fail;
4981 }
Eric Moore635374e2009-03-09 01:21:12 -06004982 }
4983
4984 if (sas_expander->enclosure_handle) {
4985 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4986 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4987 sas_expander->enclosure_handle))) {
4988 sas_expander->enclosure_logical_id =
4989 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4990 }
4991 }
4992
4993 _scsih_expander_node_add(ioc, sas_expander);
4994 return 0;
4995
4996 out_fail:
4997
Kashyap, Desai20f58952009-08-07 19:34:26 +05304998 if (mpt2sas_port)
4999 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305000 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005001 kfree(sas_expander);
5002 return rc;
5003}
5004
5005/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05305006 * _scsih_done - scsih callback handler.
5007 * @ioc: per adapter object
5008 * @smid: system request message index
5009 * @msix_index: MSIX table index supplied by the OS
5010 * @reply: reply message frame(lower 32bit addr)
5011 *
5012 * Callback handler when sending internal generated message frames.
5013 * The callback index passed is `ioc->scsih_cb_idx`
5014 *
5015 * Return 1 meaning mf should be freed from _base_interrupt
5016 * 0 means the mf is freed from this function.
5017 */
5018static u8
5019_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5020{
5021 MPI2DefaultReply_t *mpi_reply;
5022
5023 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5024 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
5025 return 1;
5026 if (ioc->scsih_cmds.smid != smid)
5027 return 1;
5028 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
5029 if (mpi_reply) {
5030 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5031 mpi_reply->MsgLength*4);
5032 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
5033 }
5034 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
5035 complete(&ioc->scsih_cmds.done);
5036 return 1;
5037}
5038
5039/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305040 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06005041 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305042 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06005043 *
5044 * Return nothing.
5045 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305046void
5047mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06005048{
5049 struct _sas_node *sas_expander;
5050 unsigned long flags;
5051
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305052 if (ioc->shost_recovery)
5053 return;
5054
Eric Moore635374e2009-03-09 01:21:12 -06005055 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305056 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5057 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305058 if (sas_expander)
5059 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06005060 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305061 if (sas_expander)
5062 _scsih_expander_node_remove(ioc, sas_expander);
Eric Moore635374e2009-03-09 01:21:12 -06005063}
5064
5065/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05305066 * _scsih_check_access_status - check access flags
5067 * @ioc: per adapter object
5068 * @sas_address: sas address
5069 * @handle: sas device handle
5070 * @access_flags: errors returned during discovery of the device
5071 *
5072 * Return 0 for success, else failure
5073 */
5074static u8
5075_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5076 u16 handle, u8 access_status)
5077{
5078 u8 rc = 1;
5079 char *desc = NULL;
5080
5081 switch (access_status) {
5082 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5083 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5084 rc = 0;
5085 break;
5086 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5087 desc = "sata capability failed";
5088 break;
5089 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5090 desc = "sata affiliation conflict";
5091 break;
5092 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5093 desc = "route not addressable";
5094 break;
5095 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5096 desc = "smp error not addressable";
5097 break;
5098 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5099 desc = "device blocked";
5100 break;
5101 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5102 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5103 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5104 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5105 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5106 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5107 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5108 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5109 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5110 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5111 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5112 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5113 desc = "sata initialization failed";
5114 break;
5115 default:
5116 desc = "unknown";
5117 break;
5118 }
5119
5120 if (!rc)
5121 return 0;
5122
5123 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5124 "handle(0x%04x)\n", ioc->name, desc,
5125 (unsigned long long)sas_address, handle);
5126 return rc;
5127}
5128
5129static void
5130_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5131{
5132 Mpi2ConfigReply_t mpi_reply;
5133 Mpi2SasDevicePage0_t sas_device_pg0;
5134 struct _sas_device *sas_device;
5135 u32 ioc_status;
5136 unsigned long flags;
5137 u64 sas_address;
5138 struct scsi_target *starget;
5139 struct MPT2SAS_TARGET *sas_target_priv_data;
5140 u32 device_info;
5141
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305142
Kashyap, Desaib4344272010-03-17 16:24:14 +05305143 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5144 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5145 return;
5146
5147 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5148 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5149 return;
5150
5151 /* check if this is end device */
5152 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5153 if (!(_scsih_is_end_device(device_info)))
5154 return;
5155
5156 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5157 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5158 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5159 sas_address);
5160
5161 if (!sas_device) {
5162 printk(MPT2SAS_ERR_FMT "device is not present "
5163 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5164 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5165 return;
5166 }
5167
5168 if (unlikely(sas_device->handle != handle)) {
5169 starget = sas_device->starget;
5170 sas_target_priv_data = starget->hostdata;
5171 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5172 " to (0x%04x)!!!\n", sas_device->handle, handle);
5173 sas_target_priv_data->handle = handle;
5174 sas_device->handle = handle;
5175 }
Kashyap, Desaib4344272010-03-17 16:24:14 +05305176
5177 /* check if device is present */
5178 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5179 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5180 printk(MPT2SAS_ERR_FMT "device is not present "
5181 "handle(0x%04x), flags!!!\n", ioc->name, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305182 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305183 return;
5184 }
5185
5186 /* check if there were any issues with discovery */
5187 if (_scsih_check_access_status(ioc, sas_address, handle,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305188 sas_device_pg0.AccessStatus)) {
5189 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305190 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305191 }
5192 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305193 _scsih_ublock_io_device(ioc, sas_address);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305194
5195}
5196
5197/**
Eric Moore635374e2009-03-09 01:21:12 -06005198 * _scsih_add_device - creating sas device object
5199 * @ioc: per adapter object
5200 * @handle: sas device handle
5201 * @phy_num: phy number end device attached to
5202 * @is_pd: is this hidden raid component
5203 *
5204 * Creating end device object, stored in ioc->sas_device_list.
5205 *
5206 * Returns 0 for success, non-zero for failure.
5207 */
5208static int
5209_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5210{
5211 Mpi2ConfigReply_t mpi_reply;
5212 Mpi2SasDevicePage0_t sas_device_pg0;
5213 Mpi2SasEnclosurePage0_t enclosure_pg0;
5214 struct _sas_device *sas_device;
5215 u32 ioc_status;
5216 __le64 sas_address;
5217 u32 device_info;
5218 unsigned long flags;
5219
5220 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5221 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5222 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5223 ioc->name, __FILE__, __LINE__, __func__);
5224 return -1;
5225 }
5226
5227 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5228 MPI2_IOCSTATUS_MASK;
5229 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5230 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5231 ioc->name, __FILE__, __LINE__, __func__);
5232 return -1;
5233 }
5234
Kashyap, Desaib4344272010-03-17 16:24:14 +05305235 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5236
Eric Moore635374e2009-03-09 01:21:12 -06005237 /* check if device is present */
5238 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5239 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5240 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5241 ioc->name, __FILE__, __LINE__, __func__);
5242 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5243 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5244 return -1;
5245 }
5246
Kashyap, Desaib4344272010-03-17 16:24:14 +05305247 /* check if there were any issues with discovery */
5248 if (_scsih_check_access_status(ioc, sas_address, handle,
5249 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005250 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005251
5252 /* check if this is end device */
5253 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5254 if (!(_scsih_is_end_device(device_info))) {
5255 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5256 ioc->name, __FILE__, __LINE__, __func__);
5257 return -1;
5258 }
5259
Eric Moore635374e2009-03-09 01:21:12 -06005260
5261 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5262 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5263 sas_address);
5264 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5265
Kashyap, Desaib4344272010-03-17 16:24:14 +05305266 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005267 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005268
5269 sas_device = kzalloc(sizeof(struct _sas_device),
5270 GFP_KERNEL);
5271 if (!sas_device) {
5272 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5273 ioc->name, __FILE__, __LINE__, __func__);
5274 return -1;
5275 }
5276
5277 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305278 if (_scsih_get_sas_address(ioc, le16_to_cpu
5279 (sas_device_pg0.ParentDevHandle),
5280 &sas_device->sas_address_parent) != 0)
5281 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5282 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005283 sas_device->enclosure_handle =
5284 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5285 sas_device->slot =
5286 le16_to_cpu(sas_device_pg0.Slot);
5287 sas_device->device_info = device_info;
5288 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305289 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005290
5291 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305292 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5293 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5294 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005295 sas_device->enclosure_logical_id =
5296 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005297
5298 /* get device name */
5299 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5300
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305301 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005302 _scsih_sas_device_init_add(ioc, sas_device);
5303 else
5304 _scsih_sas_device_add(ioc, sas_device);
5305
5306 return 0;
5307}
5308
5309/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305310 * _scsih_remove_device - removing sas device object
5311 * @ioc: per adapter object
5312 * @sas_device_delete: the sas_device object
5313 *
5314 * Return nothing.
5315 */
5316static void
5317_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5318 struct _sas_device *sas_device)
5319{
Kashyap, Desai1278b112010-03-09 17:34:13 +05305320 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305321
Kashyap, Desai1278b112010-03-09 17:34:13 +05305322 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5323 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305324 sas_device->handle, (unsigned long long)
5325 sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05305326
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305327 if (sas_device->starget && sas_device->starget->hostdata) {
5328 sas_target_priv_data = sas_device->starget->hostdata;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305329 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305330 _scsih_ublock_io_device(ioc, sas_device->sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305331 sas_target_priv_data->handle =
5332 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305333 }
5334
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305335 if (!ioc->hide_drives)
5336 mpt2sas_transport_port_remove(ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305337 sas_device->sas_address,
5338 sas_device->sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305339
5340 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305341 "(0x%016llx)\n", ioc->name, sas_device->handle,
5342 (unsigned long long) sas_device->sas_address);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305343
5344 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5345 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305346 sas_device->handle, (unsigned long long)
5347 sas_device->sas_address));
5348 kfree(sas_device);
5349}
5350/**
5351 * _scsih_device_remove_by_handle - removing device object by handle
5352 * @ioc: per adapter object
5353 * @handle: device handle
5354 *
5355 * Return nothing.
5356 */
5357static void
5358_scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5359{
5360 struct _sas_device *sas_device;
5361 unsigned long flags;
5362
5363 if (ioc->shost_recovery)
5364 return;
5365
5366 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5367 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5368 if (sas_device)
5369 list_del(&sas_device->list);
5370 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5371 if (sas_device)
5372 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005373}
5374
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305375/**
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305376 * mpt2sas_device_remove_by_sas_address - removing device object by sas address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305377 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305378 * @sas_address: device sas_address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305379 *
5380 * Return nothing.
5381 */
5382void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305383mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
5384 u64 sas_address)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305385{
5386 struct _sas_device *sas_device;
5387 unsigned long flags;
5388
5389 if (ioc->shost_recovery)
5390 return;
5391
5392 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5393 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5394 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305395 if (sas_device)
5396 list_del(&sas_device->list);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305397 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305398 if (sas_device)
5399 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305400}
Eric Moore635374e2009-03-09 01:21:12 -06005401#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5402/**
5403 * _scsih_sas_topology_change_event_debug - debug for topology event
5404 * @ioc: per adapter object
5405 * @event_data: event data payload
5406 * Context: user.
5407 */
5408static void
5409_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5410 Mpi2EventDataSasTopologyChangeList_t *event_data)
5411{
5412 int i;
5413 u16 handle;
5414 u16 reason_code;
5415 u8 phy_number;
5416 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305417 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005418
5419 switch (event_data->ExpStatus) {
5420 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5421 status_str = "add";
5422 break;
5423 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5424 status_str = "remove";
5425 break;
5426 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305427 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005428 status_str = "responding";
5429 break;
5430 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5431 status_str = "remove delay";
5432 break;
5433 default:
5434 status_str = "unknown status";
5435 break;
5436 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305437 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005438 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305439 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005440 "start_phy(%02d), count(%d)\n",
5441 le16_to_cpu(event_data->ExpanderDevHandle),
5442 le16_to_cpu(event_data->EnclosureHandle),
5443 event_data->StartPhyNum, event_data->NumEntries);
5444 for (i = 0; i < event_data->NumEntries; i++) {
5445 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5446 if (!handle)
5447 continue;
5448 phy_number = event_data->StartPhyNum + i;
5449 reason_code = event_data->PHY[i].PhyStatus &
5450 MPI2_EVENT_SAS_TOPO_RC_MASK;
5451 switch (reason_code) {
5452 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305453 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005454 break;
5455 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305456 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005457 break;
5458 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305459 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005460 break;
5461 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305462 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005463 break;
5464 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305465 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005466 break;
5467 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305468 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005469 break;
5470 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305471 link_rate = event_data->PHY[i].LinkRate >> 4;
5472 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305473 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305474 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5475 handle, status_str, link_rate, prev_link_rate);
5476
Eric Moore635374e2009-03-09 01:21:12 -06005477 }
5478}
5479#endif
5480
5481/**
5482 * _scsih_sas_topology_change_event - handle topology changes
5483 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305484 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005485 * Context: user.
5486 *
5487 */
5488static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305489_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005490 struct fw_event_work *fw_event)
5491{
5492 int i;
5493 u16 parent_handle, handle;
5494 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305495 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005496 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305497 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005498 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305499 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305500 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005501
5502#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5503 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5504 _scsih_sas_topology_change_event_debug(ioc, event_data);
5505#endif
5506
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305507 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305508 return;
5509
Eric Moore635374e2009-03-09 01:21:12 -06005510 if (!ioc->sas_hba.num_phys)
5511 _scsih_sas_host_add(ioc);
5512 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305513 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005514
5515 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305516 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005517 "event\n", ioc->name));
5518 return;
5519 }
5520
5521 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5522
5523 /* handle expander add */
5524 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5525 if (_scsih_expander_add(ioc, parent_handle) != 0)
5526 return;
5527
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305528 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5529 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5530 parent_handle);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305531 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305532 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305533 max_phys = sas_expander->num_phys;
5534 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305535 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305536 max_phys = ioc->sas_hba.num_phys;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305537 } else {
5538 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305539 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305540 }
5541 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305542
Eric Moore635374e2009-03-09 01:21:12 -06005543 /* handle siblings events */
5544 for (i = 0; i < event_data->NumEntries; i++) {
5545 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305546 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005547 "expander event\n", ioc->name));
5548 return;
5549 }
Eric Moore3cb54692010-07-08 14:44:34 -06005550 if (ioc->shost_recovery || ioc->remove_host ||
5551 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305552 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305553 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305554 if (phy_number >= max_phys)
5555 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305556 reason_code = event_data->PHY[i].PhyStatus &
5557 MPI2_EVENT_SAS_TOPO_RC_MASK;
5558 if ((event_data->PHY[i].PhyStatus &
5559 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5560 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005561 continue;
5562 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5563 if (!handle)
5564 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305565 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305566 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005567 switch (reason_code) {
5568 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305569
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305570 if (ioc->shost_recovery)
5571 break;
5572
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305573 if (link_rate == prev_link_rate)
5574 break;
5575
5576 mpt2sas_transport_update_links(ioc, sas_address,
5577 handle, phy_number, link_rate);
5578
Kashyap, Desaib4344272010-03-17 16:24:14 +05305579 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5580 break;
5581
5582 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305583 break;
Eric Moore635374e2009-03-09 01:21:12 -06005584 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305585
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305586 if (ioc->shost_recovery)
5587 break;
5588
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305589 mpt2sas_transport_update_links(ioc, sas_address,
5590 handle, phy_number, link_rate);
5591
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305592 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005593 break;
5594 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305595
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305596 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005597 break;
5598 }
5599 }
5600
5601 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305602 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5603 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305604 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005605
5606}
5607
5608#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5609/**
5610 * _scsih_sas_device_status_change_event_debug - debug for device event
5611 * @event_data: event data payload
5612 * Context: user.
5613 *
5614 * Return nothing.
5615 */
5616static void
5617_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5618 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5619{
5620 char *reason_str = NULL;
5621
5622 switch (event_data->ReasonCode) {
5623 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5624 reason_str = "smart data";
5625 break;
5626 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5627 reason_str = "unsupported device discovered";
5628 break;
5629 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5630 reason_str = "internal device reset";
5631 break;
5632 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5633 reason_str = "internal task abort";
5634 break;
5635 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5636 reason_str = "internal task abort set";
5637 break;
5638 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5639 reason_str = "internal clear task set";
5640 break;
5641 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5642 reason_str = "internal query task";
5643 break;
5644 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5645 reason_str = "sata init failure";
5646 break;
5647 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5648 reason_str = "internal device reset complete";
5649 break;
5650 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5651 reason_str = "internal task abort complete";
5652 break;
5653 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5654 reason_str = "internal async notification";
5655 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305656 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5657 reason_str = "expander reduced functionality";
5658 break;
5659 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5660 reason_str = "expander reduced functionality complete";
5661 break;
Eric Moore635374e2009-03-09 01:21:12 -06005662 default:
5663 reason_str = "unknown reason";
5664 break;
5665 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305666 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305667 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5668 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5669 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5670 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005671 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305672 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005673 event_data->ASC, event_data->ASCQ);
5674 printk(KERN_INFO "\n");
5675}
5676#endif
5677
5678/**
5679 * _scsih_sas_device_status_change_event - handle device status change
5680 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305681 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005682 * Context: user.
5683 *
5684 * Return nothing.
5685 */
5686static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305687_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5688 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005689{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305690 struct MPT2SAS_TARGET *target_priv_data;
5691 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305692 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305693 unsigned long flags;
5694 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5695 fw_event->event_data;
5696
Eric Moore635374e2009-03-09 01:21:12 -06005697#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5698 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305699 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305700 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005701#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305702
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305703 /* In MPI Revision K (0xC), the internal device reset complete was
5704 * implemented, so avoid setting tm_busy flag for older firmware.
5705 */
5706 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5707 return;
5708
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305709 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305710 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305711 event_data->ReasonCode !=
5712 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305713 return;
5714
5715 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5716 sas_address = le64_to_cpu(event_data->SASAddress);
5717 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5718 sas_address);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305719
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305720 if (!sas_device || !sas_device->starget) {
5721 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305722 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305723 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305724
5725 target_priv_data = sas_device->starget->hostdata;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305726 if (!target_priv_data) {
5727 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305728 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305729 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305730
5731 if (event_data->ReasonCode ==
5732 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5733 target_priv_data->tm_busy = 1;
5734 else
5735 target_priv_data->tm_busy = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305736 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005737}
5738
5739#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5740/**
5741 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5742 * @ioc: per adapter object
5743 * @event_data: event data payload
5744 * Context: user.
5745 *
5746 * Return nothing.
5747 */
5748static void
5749_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5750 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5751{
5752 char *reason_str = NULL;
5753
5754 switch (event_data->ReasonCode) {
5755 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5756 reason_str = "enclosure add";
5757 break;
5758 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5759 reason_str = "enclosure remove";
5760 break;
5761 default:
5762 reason_str = "unknown reason";
5763 break;
5764 }
5765
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305766 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005767 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5768 " number slots(%d)\n", ioc->name, reason_str,
5769 le16_to_cpu(event_data->EnclosureHandle),
5770 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5771 le16_to_cpu(event_data->StartSlot));
5772}
5773#endif
5774
5775/**
5776 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5777 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305778 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005779 * Context: user.
5780 *
5781 * Return nothing.
5782 */
5783static void
5784_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305785 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005786{
5787#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5788 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5789 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305790 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005791#endif
5792}
5793
5794/**
Andrew Mortona78e21d2012-02-08 12:52:22 -08005795 * _scsih_sas_broadcast_primitive_event - handle broadcast events
Eric Moore635374e2009-03-09 01:21:12 -06005796 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305797 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005798 * Context: user.
5799 *
5800 * Return nothing.
5801 */
5802static void
Andrew Mortona78e21d2012-02-08 12:52:22 -08005803_scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305804 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005805{
5806 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305807 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005808 u16 smid, handle;
5809 u32 lun;
5810 struct MPT2SAS_DEVICE *sas_device_priv_data;
5811 u32 termination_count;
5812 u32 query_count;
5813 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305814 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305815 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305816 unsigned long flags;
5817 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305818 u8 max_retries = 0;
5819 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305820
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305821 mutex_lock(&ioc->tm_cmds.mutex);
5822 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5823 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5824 event_data->PortWidth));
5825
5826 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005827
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305828 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305829 mpi_reply = ioc->tm_cmds.reply;
5830broadcast_aen_retry:
5831
5832 /* sanity checks for retrying this loop */
5833 if (max_retries++ == 5) {
5834 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5835 ioc->name, __func__));
5836 goto out;
5837 } else if (max_retries > 1)
5838 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5839 ioc->name, __func__, max_retries - 1));
5840
Eric Moore635374e2009-03-09 01:21:12 -06005841 termination_count = 0;
5842 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305843 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305844 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305845 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005846 scmd = _scsih_scsi_lookup_get(ioc, smid);
5847 if (!scmd)
5848 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305849 sdev = scmd->device;
5850 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005851 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5852 continue;
5853 /* skip hidden raid components */
5854 if (sas_device_priv_data->sas_target->flags &
5855 MPT_TARGET_FLAGS_RAID_COMPONENT)
5856 continue;
5857 /* skip volumes */
5858 if (sas_device_priv_data->sas_target->flags &
5859 MPT_TARGET_FLAGS_VOLUME)
5860 continue;
5861
5862 handle = sas_device_priv_data->sas_target->handle;
5863 lun = sas_device_priv_data->lun;
5864 query_count++;
5865
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305866 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305867 goto out;
5868
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305869 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305870 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5871 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5872 TM_MUTEX_OFF);
5873 if (r == FAILED) {
5874 sdev_printk(KERN_WARNING, sdev,
5875 "mpt2sas_scsih_issue_tm: FAILED when sending "
5876 "QUERY_TASK: scmd(%p)\n", scmd);
5877 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5878 goto broadcast_aen_retry;
5879 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305880 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5881 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305882 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5883 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5884 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5885 scmd);
5886 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5887 goto broadcast_aen_retry;
5888 }
5889
5890 /* see if IO is still owned by IOC and target */
5891 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005892 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5893 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305894 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305895 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005896 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305897 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305898 task_abort_retries = 0;
5899 tm_retry:
5900 if (task_abort_retries++ == 60) {
5901 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5902 "%s: ABORT_TASK: giving up\n", ioc->name,
5903 __func__));
5904 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5905 goto broadcast_aen_retry;
5906 }
5907
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305908 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305909 goto out_no_lock;
5910
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305911 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5912 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305913 scmd->serial_number, TM_MUTEX_OFF);
5914 if (r == FAILED) {
5915 sdev_printk(KERN_WARNING, sdev,
5916 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305917 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305918 goto tm_retry;
5919 }
5920
5921 if (task_abort_retries > 1)
5922 sdev_printk(KERN_WARNING, sdev,
5923 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5924 " scmd(%p)\n",
5925 task_abort_retries - 1, scmd);
5926
Eric Moore635374e2009-03-09 01:21:12 -06005927 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305928 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005929 }
Eric Moore635374e2009-03-09 01:21:12 -06005930
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305931 if (ioc->broadcast_aen_pending) {
5932 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5933 " pending AEN\n", ioc->name, __func__));
5934 ioc->broadcast_aen_pending = 0;
5935 goto broadcast_aen_retry;
5936 }
5937
5938 out:
5939 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5940 out_no_lock:
5941
5942 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005943 "%s - exit, query_count = %d termination_count = %d\n",
5944 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305945
5946 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305947 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305948 _scsih_ublock_io_all_device(ioc);
5949 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005950}
5951
5952/**
5953 * _scsih_sas_discovery_event - handle discovery events
5954 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305955 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005956 * Context: user.
5957 *
5958 * Return nothing.
5959 */
5960static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305961_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5962 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005963{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305964 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5965
Eric Moore635374e2009-03-09 01:21:12 -06005966#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5967 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305968 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005969 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5970 "start" : "stop");
5971 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305972 printk("discovery_status(0x%08x)",
5973 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005974 printk("\n");
5975 }
5976#endif
5977
5978 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305979 !ioc->sas_hba.num_phys) {
5980 if (disable_discovery > 0 && ioc->shost_recovery) {
5981 /* Wait for the reset to complete */
5982 while (ioc->shost_recovery)
5983 ssleep(1);
5984 }
Eric Moore635374e2009-03-09 01:21:12 -06005985 _scsih_sas_host_add(ioc);
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305986 }
Eric Moore635374e2009-03-09 01:21:12 -06005987}
5988
5989/**
5990 * _scsih_reprobe_lun - reprobing lun
5991 * @sdev: scsi device struct
5992 * @no_uld_attach: sdev->no_uld_attach flag setting
5993 *
5994 **/
5995static void
5996_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5997{
5998 int rc;
5999
6000 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6001 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6002 sdev->no_uld_attach ? "hidding" : "exposing");
6003 rc = scsi_device_reprobe(sdev);
6004}
6005
6006/**
Eric Moore635374e2009-03-09 01:21:12 -06006007 * _scsih_sas_volume_add - add new volume
6008 * @ioc: per adapter object
6009 * @element: IR config element data
6010 * Context: user.
6011 *
6012 * Return nothing.
6013 */
6014static void
6015_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
6016 Mpi2EventIrConfigElement_t *element)
6017{
6018 struct _raid_device *raid_device;
6019 unsigned long flags;
6020 u64 wwid;
6021 u16 handle = le16_to_cpu(element->VolDevHandle);
6022 int rc;
6023
Eric Moore635374e2009-03-09 01:21:12 -06006024 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6025 if (!wwid) {
6026 printk(MPT2SAS_ERR_FMT
6027 "failure at %s:%d/%s()!\n", ioc->name,
6028 __FILE__, __LINE__, __func__);
6029 return;
6030 }
6031
6032 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6033 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6034 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6035
6036 if (raid_device)
6037 return;
6038
6039 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6040 if (!raid_device) {
6041 printk(MPT2SAS_ERR_FMT
6042 "failure at %s:%d/%s()!\n", ioc->name,
6043 __FILE__, __LINE__, __func__);
6044 return;
6045 }
6046
6047 raid_device->id = ioc->sas_id++;
6048 raid_device->channel = RAID_CHANNEL;
6049 raid_device->handle = handle;
6050 raid_device->wwid = wwid;
6051 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306052 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06006053 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6054 raid_device->id, 0);
6055 if (rc)
6056 _scsih_raid_device_remove(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306057 } else {
6058 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006059 _scsih_determine_boot_device(ioc, raid_device, 1);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306060 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6061 }
Eric Moore635374e2009-03-09 01:21:12 -06006062}
6063
6064/**
6065 * _scsih_sas_volume_delete - delete volume
6066 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306067 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006068 * Context: user.
6069 *
6070 * Return nothing.
6071 */
6072static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306073_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006074{
6075 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006076 unsigned long flags;
6077 struct MPT2SAS_TARGET *sas_target_priv_data;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306078 struct scsi_target *starget = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006079
Eric Moore635374e2009-03-09 01:21:12 -06006080 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6081 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306082 if (raid_device) {
6083 if (raid_device->starget) {
6084 starget = raid_device->starget;
6085 sas_target_priv_data = starget->hostdata;
6086 sas_target_priv_data->deleted = 1;
6087 }
6088 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6089 "(0x%016llx)\n", ioc->name, raid_device->handle,
6090 (unsigned long long) raid_device->wwid);
6091 list_del(&raid_device->list);
6092 kfree(raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006093 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306094 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6095 if (starget)
6096 scsi_remove_target(&starget->dev);
Eric Moore635374e2009-03-09 01:21:12 -06006097}
6098
6099/**
6100 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6101 * @ioc: per adapter object
6102 * @element: IR config element data
6103 * Context: user.
6104 *
6105 * Return nothing.
6106 */
6107static void
6108_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6109 Mpi2EventIrConfigElement_t *element)
6110{
6111 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306112 struct scsi_target *starget = NULL;
6113 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006114 unsigned long flags;
6115 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6116
6117 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6118 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306119 if (sas_device) {
6120 sas_device->volume_handle = 0;
6121 sas_device->volume_wwid = 0;
6122 clear_bit(handle, ioc->pd_handles);
6123 if (sas_device->starget && sas_device->starget->hostdata) {
6124 starget = sas_device->starget;
6125 sas_target_priv_data = starget->hostdata;
6126 sas_target_priv_data->flags &=
6127 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6128 }
6129 }
Eric Moore635374e2009-03-09 01:21:12 -06006130 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6131 if (!sas_device)
6132 return;
6133
6134 /* exposing raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306135 if (starget)
6136 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006137}
6138
6139/**
6140 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6141 * @ioc: per adapter object
6142 * @element: IR config element data
6143 * Context: user.
6144 *
6145 * Return nothing.
6146 */
6147static void
6148_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6149 Mpi2EventIrConfigElement_t *element)
6150{
6151 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306152 struct scsi_target *starget = NULL;
6153 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006154 unsigned long flags;
6155 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306156 u16 volume_handle = 0;
6157 u64 volume_wwid = 0;
6158
6159 mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
6160 if (volume_handle)
6161 mpt2sas_config_get_volume_wwid(ioc, volume_handle,
6162 &volume_wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006163
6164 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6165 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306166 if (sas_device) {
6167 set_bit(handle, ioc->pd_handles);
6168 if (sas_device->starget && sas_device->starget->hostdata) {
6169 starget = sas_device->starget;
6170 sas_target_priv_data = starget->hostdata;
6171 sas_target_priv_data->flags |=
6172 MPT_TARGET_FLAGS_RAID_COMPONENT;
6173 sas_device->volume_handle = volume_handle;
6174 sas_device->volume_wwid = volume_wwid;
6175 }
6176 }
Eric Moore635374e2009-03-09 01:21:12 -06006177 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6178 if (!sas_device)
6179 return;
6180
6181 /* hiding raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306182 if (starget)
6183 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006184}
6185
6186/**
6187 * _scsih_sas_pd_delete - delete pd component
6188 * @ioc: per adapter object
6189 * @element: IR config element data
6190 * Context: user.
6191 *
6192 * Return nothing.
6193 */
6194static void
6195_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6196 Mpi2EventIrConfigElement_t *element)
6197{
Eric Moore635374e2009-03-09 01:21:12 -06006198 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6199
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306200 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006201}
6202
6203/**
6204 * _scsih_sas_pd_add - remove pd component
6205 * @ioc: per adapter object
6206 * @element: IR config element data
6207 * Context: user.
6208 *
6209 * Return nothing.
6210 */
6211static void
6212_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6213 Mpi2EventIrConfigElement_t *element)
6214{
6215 struct _sas_device *sas_device;
6216 unsigned long flags;
6217 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306218 Mpi2ConfigReply_t mpi_reply;
6219 Mpi2SasDevicePage0_t sas_device_pg0;
6220 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306221 u64 sas_address;
6222 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006223
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306224 set_bit(handle, ioc->pd_handles);
6225
Eric Moore635374e2009-03-09 01:21:12 -06006226 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6227 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6228 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306229 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306230 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306231
6232 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6233 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6234 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6235 ioc->name, __FILE__, __LINE__, __func__);
6236 return;
6237 }
6238
6239 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6240 MPI2_IOCSTATUS_MASK;
6241 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6242 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6243 ioc->name, __FILE__, __LINE__, __func__);
6244 return;
6245 }
6246
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306247 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6248 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6249 mpt2sas_transport_update_links(ioc, sas_address, handle,
6250 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306251
6252 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006253}
6254
6255#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6256/**
6257 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6258 * @ioc: per adapter object
6259 * @event_data: event data payload
6260 * Context: user.
6261 *
6262 * Return nothing.
6263 */
6264static void
6265_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6266 Mpi2EventDataIrConfigChangeList_t *event_data)
6267{
6268 Mpi2EventIrConfigElement_t *element;
6269 u8 element_type;
6270 int i;
6271 char *reason_str = NULL, *element_str = NULL;
6272
6273 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6274
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306275 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006276 ioc->name, (le32_to_cpu(event_data->Flags) &
6277 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6278 "foreign" : "native", event_data->NumElements);
6279 for (i = 0; i < event_data->NumElements; i++, element++) {
6280 switch (element->ReasonCode) {
6281 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6282 reason_str = "add";
6283 break;
6284 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6285 reason_str = "remove";
6286 break;
6287 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6288 reason_str = "no change";
6289 break;
6290 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6291 reason_str = "hide";
6292 break;
6293 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6294 reason_str = "unhide";
6295 break;
6296 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6297 reason_str = "volume_created";
6298 break;
6299 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6300 reason_str = "volume_deleted";
6301 break;
6302 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6303 reason_str = "pd_created";
6304 break;
6305 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6306 reason_str = "pd_deleted";
6307 break;
6308 default:
6309 reason_str = "unknown reason";
6310 break;
6311 }
6312 element_type = le16_to_cpu(element->ElementFlags) &
6313 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6314 switch (element_type) {
6315 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6316 element_str = "volume";
6317 break;
6318 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6319 element_str = "phys disk";
6320 break;
6321 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6322 element_str = "hot spare";
6323 break;
6324 default:
6325 element_str = "unknown element";
6326 break;
6327 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306328 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006329 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6330 reason_str, le16_to_cpu(element->VolDevHandle),
6331 le16_to_cpu(element->PhysDiskDevHandle),
6332 element->PhysDiskNum);
6333 }
6334}
6335#endif
6336
6337/**
6338 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6339 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306340 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006341 * Context: user.
6342 *
6343 * Return nothing.
6344 */
6345static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306346_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6347 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006348{
6349 Mpi2EventIrConfigElement_t *element;
6350 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306351 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306352 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006353
6354#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306355 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6356 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006357 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6358
6359#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306360
6361 if (ioc->shost_recovery)
6362 return;
6363
Kashyap, Desai62727a72009-08-07 19:35:18 +05306364 foreign_config = (le32_to_cpu(event_data->Flags) &
6365 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006366
6367 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6368 for (i = 0; i < event_data->NumElements; i++, element++) {
6369
6370 switch (element->ReasonCode) {
6371 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6372 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306373 if (!foreign_config)
6374 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006375 break;
6376 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6377 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306378 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306379 _scsih_sas_volume_delete(ioc,
6380 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006381 break;
6382 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306383 if (!ioc->is_warpdrive)
6384 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006385 break;
6386 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306387 if (!ioc->is_warpdrive)
6388 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006389 break;
6390 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306391 if (!ioc->is_warpdrive)
6392 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006393 break;
6394 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306395 if (!ioc->is_warpdrive)
6396 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006397 break;
6398 }
6399 }
6400}
6401
6402/**
6403 * _scsih_sas_ir_volume_event - IR volume event
6404 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306405 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006406 * Context: user.
6407 *
6408 * Return nothing.
6409 */
6410static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306411_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6412 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006413{
6414 u64 wwid;
6415 unsigned long flags;
6416 struct _raid_device *raid_device;
6417 u16 handle;
6418 u32 state;
6419 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306420 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006421
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306422 if (ioc->shost_recovery)
6423 return;
6424
Eric Moore635374e2009-03-09 01:21:12 -06006425 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6426 return;
6427
6428 handle = le16_to_cpu(event_data->VolDevHandle);
6429 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306430 if (!ioc->hide_ir_msg)
6431 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6432 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6433 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006434
Eric Moore635374e2009-03-09 01:21:12 -06006435 switch (state) {
6436 case MPI2_RAID_VOL_STATE_MISSING:
6437 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306438 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006439 break;
6440
6441 case MPI2_RAID_VOL_STATE_ONLINE:
6442 case MPI2_RAID_VOL_STATE_DEGRADED:
6443 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306444
6445 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6446 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6447 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6448
Eric Moore635374e2009-03-09 01:21:12 -06006449 if (raid_device)
6450 break;
6451
6452 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6453 if (!wwid) {
6454 printk(MPT2SAS_ERR_FMT
6455 "failure at %s:%d/%s()!\n", ioc->name,
6456 __FILE__, __LINE__, __func__);
6457 break;
6458 }
6459
6460 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6461 if (!raid_device) {
6462 printk(MPT2SAS_ERR_FMT
6463 "failure at %s:%d/%s()!\n", ioc->name,
6464 __FILE__, __LINE__, __func__);
6465 break;
6466 }
6467
6468 raid_device->id = ioc->sas_id++;
6469 raid_device->channel = RAID_CHANNEL;
6470 raid_device->handle = handle;
6471 raid_device->wwid = wwid;
6472 _scsih_raid_device_add(ioc, raid_device);
6473 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6474 raid_device->id, 0);
6475 if (rc)
6476 _scsih_raid_device_remove(ioc, raid_device);
6477 break;
6478
6479 case MPI2_RAID_VOL_STATE_INITIALIZING:
6480 default:
6481 break;
6482 }
6483}
6484
6485/**
6486 * _scsih_sas_ir_physical_disk_event - PD event
6487 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306488 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006489 * Context: user.
6490 *
6491 * Return nothing.
6492 */
6493static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306494_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6495 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006496{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306497 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006498 u32 state;
6499 struct _sas_device *sas_device;
6500 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306501 Mpi2ConfigReply_t mpi_reply;
6502 Mpi2SasDevicePage0_t sas_device_pg0;
6503 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306504 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306505 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006506
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306507 if (ioc->shost_recovery)
6508 return;
6509
Eric Moore635374e2009-03-09 01:21:12 -06006510 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6511 return;
6512
6513 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6514 state = le32_to_cpu(event_data->NewValue);
6515
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306516 if (!ioc->hide_ir_msg)
6517 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6518 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6519 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006520
Eric Moore635374e2009-03-09 01:21:12 -06006521 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006522 case MPI2_RAID_PD_STATE_ONLINE:
6523 case MPI2_RAID_PD_STATE_DEGRADED:
6524 case MPI2_RAID_PD_STATE_REBUILDING:
6525 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306526 case MPI2_RAID_PD_STATE_HOT_SPARE:
6527
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306528 if (!ioc->is_warpdrive)
6529 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306530
6531 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6532 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6533 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6534
6535 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306536 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306537
6538 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6539 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6540 handle))) {
6541 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6542 ioc->name, __FILE__, __LINE__, __func__);
6543 return;
6544 }
6545
6546 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6547 MPI2_IOCSTATUS_MASK;
6548 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6549 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6550 ioc->name, __FILE__, __LINE__, __func__);
6551 return;
6552 }
6553
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306554 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6555 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6556 mpt2sas_transport_update_links(ioc, sas_address, handle,
6557 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306558
6559 _scsih_add_device(ioc, handle, 0, 1);
6560
Eric Moore635374e2009-03-09 01:21:12 -06006561 break;
6562
Kashyap, Desai62727a72009-08-07 19:35:18 +05306563 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006564 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6565 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006566 default:
6567 break;
6568 }
6569}
6570
6571#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6572/**
6573 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6574 * @ioc: per adapter object
6575 * @event_data: event data payload
6576 * Context: user.
6577 *
6578 * Return nothing.
6579 */
6580static void
6581_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6582 Mpi2EventDataIrOperationStatus_t *event_data)
6583{
6584 char *reason_str = NULL;
6585
6586 switch (event_data->RAIDOperation) {
6587 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6588 reason_str = "resync";
6589 break;
6590 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6591 reason_str = "online capacity expansion";
6592 break;
6593 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6594 reason_str = "consistency check";
6595 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306596 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6597 reason_str = "background init";
6598 break;
6599 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6600 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006601 break;
6602 }
6603
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306604 if (!reason_str)
6605 return;
6606
Eric Moore635374e2009-03-09 01:21:12 -06006607 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6608 "\thandle(0x%04x), percent complete(%d)\n",
6609 ioc->name, reason_str,
6610 le16_to_cpu(event_data->VolDevHandle),
6611 event_data->PercentComplete);
6612}
6613#endif
6614
6615/**
6616 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6617 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306618 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006619 * Context: user.
6620 *
6621 * Return nothing.
6622 */
6623static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306624_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6625 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006626{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306627 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6628 static struct _raid_device *raid_device;
6629 unsigned long flags;
6630 u16 handle;
6631
Eric Moore635374e2009-03-09 01:21:12 -06006632#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306633 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6634 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306635 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306636 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006637#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306638
6639 /* code added for raid transport support */
6640 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6641
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306642 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306643 handle = le16_to_cpu(event_data->VolDevHandle);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306644 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306645 if (raid_device)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306646 raid_device->percent_complete =
6647 event_data->PercentComplete;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306648 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306649 }
Eric Moore635374e2009-03-09 01:21:12 -06006650}
6651
6652/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306653 * _scsih_prep_device_scan - initialize parameters prior to device scan
6654 * @ioc: per adapter object
6655 *
6656 * Set the deleted flag prior to device scan. If the device is found during
6657 * the scan, then we clear the deleted flag.
6658 */
6659static void
6660_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6661{
6662 struct MPT2SAS_DEVICE *sas_device_priv_data;
6663 struct scsi_device *sdev;
6664
6665 shost_for_each_device(sdev, ioc->shost) {
6666 sas_device_priv_data = sdev->hostdata;
6667 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6668 sas_device_priv_data->sas_target->deleted = 1;
6669 }
6670}
6671
6672/**
Eric Moore635374e2009-03-09 01:21:12 -06006673 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6674 * @ioc: per adapter object
6675 * @sas_address: sas address
6676 * @slot: enclosure slot id
6677 * @handle: device handle
6678 *
6679 * After host reset, find out whether devices are still responding.
6680 * Used in _scsi_remove_unresponsive_sas_devices.
6681 *
6682 * Return nothing.
6683 */
6684static void
6685_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6686 u16 slot, u16 handle)
6687{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306688 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006689 struct scsi_target *starget;
6690 struct _sas_device *sas_device;
6691 unsigned long flags;
6692
6693 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6694 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6695 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306696 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006697 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306698 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306699 if (starget && starget->hostdata) {
6700 sas_target_priv_data = starget->hostdata;
6701 sas_target_priv_data->tm_busy = 0;
6702 sas_target_priv_data->deleted = 0;
6703 } else
6704 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306705 if (starget)
6706 starget_printk(KERN_INFO, starget,
6707 "handle(0x%04x), sas_addr(0x%016llx), "
6708 "enclosure logical id(0x%016llx), "
6709 "slot(%d)\n", handle,
6710 (unsigned long long)sas_device->sas_address,
6711 (unsigned long long)
6712 sas_device->enclosure_logical_id,
6713 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006714 if (sas_device->handle == handle)
6715 goto out;
6716 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6717 sas_device->handle);
6718 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306719 if (sas_target_priv_data)
6720 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006721 goto out;
6722 }
6723 }
6724 out:
6725 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6726}
6727
6728/**
6729 * _scsih_search_responding_sas_devices -
6730 * @ioc: per adapter object
6731 *
6732 * After host reset, find out whether devices are still responding.
6733 * If not remove.
6734 *
6735 * Return nothing.
6736 */
6737static void
6738_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6739{
6740 Mpi2SasDevicePage0_t sas_device_pg0;
6741 Mpi2ConfigReply_t mpi_reply;
6742 u16 ioc_status;
6743 __le64 sas_address;
6744 u16 handle;
6745 u32 device_info;
6746 u16 slot;
6747
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306748 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006749
6750 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306751 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006752
6753 handle = 0xFFFF;
6754 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6755 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6756 handle))) {
6757 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6758 MPI2_IOCSTATUS_MASK;
6759 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6760 break;
6761 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6762 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6763 if (!(_scsih_is_end_device(device_info)))
6764 continue;
6765 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6766 slot = le16_to_cpu(sas_device_pg0.Slot);
6767 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6768 handle);
6769 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306770out:
6771 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6772 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006773}
6774
6775/**
6776 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6777 * @ioc: per adapter object
6778 * @wwid: world wide identifier for raid volume
6779 * @handle: device handle
6780 *
6781 * After host reset, find out whether devices are still responding.
6782 * Used in _scsi_remove_unresponsive_raid_devices.
6783 *
6784 * Return nothing.
6785 */
6786static void
6787_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6788 u16 handle)
6789{
6790 struct MPT2SAS_TARGET *sas_target_priv_data;
6791 struct scsi_target *starget;
6792 struct _raid_device *raid_device;
6793 unsigned long flags;
6794
6795 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6796 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6797 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306798 starget = raid_device->starget;
6799 if (starget && starget->hostdata) {
6800 sas_target_priv_data = starget->hostdata;
6801 sas_target_priv_data->deleted = 0;
6802 } else
6803 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006804 raid_device->responding = 1;
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306805 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006806 starget_printk(KERN_INFO, raid_device->starget,
6807 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6808 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306809 /*
6810 * WARPDRIVE: The handles of the PDs might have changed
6811 * across the host reset so re-initialize the
6812 * required data for Direct IO
6813 */
6814 _scsih_init_warpdrive_properties(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306815 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6816 if (raid_device->handle == handle) {
6817 spin_unlock_irqrestore(&ioc->raid_device_lock,
6818 flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306819 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306820 }
Eric Moore635374e2009-03-09 01:21:12 -06006821 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6822 raid_device->handle);
6823 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306824 if (sas_target_priv_data)
6825 sas_target_priv_data->handle = handle;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306826 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306827 return;
Eric Moore635374e2009-03-09 01:21:12 -06006828 }
6829 }
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306830
Eric Moore635374e2009-03-09 01:21:12 -06006831 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6832}
6833
6834/**
6835 * _scsih_search_responding_raid_devices -
6836 * @ioc: per adapter object
6837 *
6838 * After host reset, find out whether devices are still responding.
6839 * If not remove.
6840 *
6841 * Return nothing.
6842 */
6843static void
6844_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6845{
6846 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306847 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306848 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006849 Mpi2ConfigReply_t mpi_reply;
6850 u16 ioc_status;
6851 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306852 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006853
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306854 if (!ioc->ir_firmware)
6855 return;
6856
6857 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6858 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006859
6860 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306861 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006862
6863 handle = 0xFFFF;
6864 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6865 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6866 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6867 MPI2_IOCSTATUS_MASK;
6868 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6869 break;
6870 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306871
6872 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6873 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6874 sizeof(Mpi2RaidVolPage0_t)))
6875 continue;
6876
6877 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6878 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6879 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6880 _scsih_mark_responding_raid_device(ioc,
6881 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006882 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306883
6884 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306885 if (!ioc->is_warpdrive) {
6886 phys_disk_num = 0xFF;
6887 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6888 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6889 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6890 phys_disk_num))) {
6891 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6892 MPI2_IOCSTATUS_MASK;
6893 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6894 break;
6895 phys_disk_num = pd_pg0.PhysDiskNum;
6896 handle = le16_to_cpu(pd_pg0.DevHandle);
6897 set_bit(handle, ioc->pd_handles);
6898 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306899 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306900out:
6901 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6902 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006903}
6904
6905/**
6906 * _scsih_mark_responding_expander - mark a expander as responding
6907 * @ioc: per adapter object
6908 * @sas_address: sas address
6909 * @handle:
6910 *
6911 * After host reset, find out whether devices are still responding.
6912 * Used in _scsi_remove_unresponsive_expanders.
6913 *
6914 * Return nothing.
6915 */
6916static void
6917_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6918 u16 handle)
6919{
6920 struct _sas_node *sas_expander;
6921 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306922 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006923
6924 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6925 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306926 if (sas_expander->sas_address != sas_address)
6927 continue;
6928 sas_expander->responding = 1;
6929 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006930 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306931 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6932 " from(0x%04x) to (0x%04x)!!!\n",
6933 (unsigned long long)sas_expander->sas_address,
6934 sas_expander->handle, handle);
6935 sas_expander->handle = handle;
6936 for (i = 0 ; i < sas_expander->num_phys ; i++)
6937 sas_expander->phy[i].handle = handle;
6938 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006939 }
6940 out:
6941 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6942}
6943
6944/**
6945 * _scsih_search_responding_expanders -
6946 * @ioc: per adapter object
6947 *
6948 * After host reset, find out whether devices are still responding.
6949 * If not remove.
6950 *
6951 * Return nothing.
6952 */
6953static void
6954_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6955{
6956 Mpi2ExpanderPage0_t expander_pg0;
6957 Mpi2ConfigReply_t mpi_reply;
6958 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306959 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006960 u16 handle;
6961
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306962 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006963
6964 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306965 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006966
6967 handle = 0xFFFF;
6968 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6969 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6970
6971 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6972 MPI2_IOCSTATUS_MASK;
6973 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6974 break;
6975
6976 handle = le16_to_cpu(expander_pg0.DevHandle);
6977 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6978 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6979 "sas_addr(0x%016llx)\n", handle,
6980 (unsigned long long)sas_address);
6981 _scsih_mark_responding_expander(ioc, sas_address, handle);
6982 }
6983
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306984 out:
6985 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006986}
6987
6988/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306989 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006990 * @ioc: per adapter object
6991 *
6992 * Return nothing.
6993 */
6994static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306995_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006996{
6997 struct _sas_device *sas_device, *sas_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306998 struct _sas_node *sas_expander, *sas_expander_next;
Eric Moore635374e2009-03-09 01:21:12 -06006999 struct _raid_device *raid_device, *raid_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307000 struct list_head tmp_list;
7001 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007002
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307003 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
7004 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007005
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307006 /* removing unresponding end devices */
7007 printk(MPT2SAS_INFO_FMT "removing unresponding devices: end-devices\n",
7008 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007009 list_for_each_entry_safe(sas_device, sas_device_next,
7010 &ioc->sas_device_list, list) {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307011 if (!sas_device->responding)
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05307012 mpt2sas_device_remove_by_sas_address(ioc,
7013 sas_device->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307014 else
Eric Moore635374e2009-03-09 01:21:12 -06007015 sas_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007016 }
7017
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307018 /* removing unresponding volumes */
7019 if (ioc->ir_firmware) {
7020 printk(MPT2SAS_INFO_FMT "removing unresponding devices: "
7021 "volumes\n", ioc->name);
7022 list_for_each_entry_safe(raid_device, raid_device_next,
7023 &ioc->raid_device_list, list) {
7024 if (!raid_device->responding)
7025 _scsih_sas_volume_delete(ioc,
7026 raid_device->handle);
7027 else
7028 raid_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007029 }
Eric Moore635374e2009-03-09 01:21:12 -06007030 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307031 /* removing unresponding expanders */
7032 printk(MPT2SAS_INFO_FMT "removing unresponding devices: expanders\n",
7033 ioc->name);
7034 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7035 INIT_LIST_HEAD(&tmp_list);
7036 list_for_each_entry_safe(sas_expander, sas_expander_next,
7037 &ioc->sas_expander_list, list) {
7038 if (!sas_expander->responding)
7039 list_move_tail(&sas_expander->list, &tmp_list);
7040 else
Eric Moore635374e2009-03-09 01:21:12 -06007041 sas_expander->responding = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307042 }
7043 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7044 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7045 list) {
7046 list_del(&sas_expander->list);
7047 _scsih_expander_node_remove(ioc, sas_expander);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307048 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307049 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
7050 ioc->name);
7051 /* unblock devices */
7052 _scsih_ublock_io_all_device(ioc);
7053}
7054
7055static void
7056_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
7057 struct _sas_node *sas_expander, u16 handle)
7058{
7059 Mpi2ExpanderPage1_t expander_pg1;
7060 Mpi2ConfigReply_t mpi_reply;
7061 int i;
7062
7063 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7064 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
7065 &expander_pg1, i, handle))) {
7066 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7067 ioc->name, __FILE__, __LINE__, __func__);
7068 return;
7069 }
7070
7071 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
7072 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7073 expander_pg1.NegotiatedLinkRate >> 4);
7074 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307075}
7076
7077/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307078 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307079 * @ioc: per adapter object
7080 *
7081 * Return nothing.
7082 */
7083static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307084_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307085{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307086 Mpi2ExpanderPage0_t expander_pg0;
7087 Mpi2SasDevicePage0_t sas_device_pg0;
7088 Mpi2RaidVolPage1_t volume_pg1;
7089 Mpi2RaidVolPage0_t volume_pg0;
7090 Mpi2RaidPhysDiskPage0_t pd_pg0;
7091 Mpi2EventIrConfigElement_t element;
7092 Mpi2ConfigReply_t mpi_reply;
7093 u8 phys_disk_num;
7094 u16 ioc_status;
7095 u16 handle, parent_handle;
7096 u64 sas_address;
7097 struct _sas_device *sas_device;
7098 struct _sas_node *expander_device;
7099 static struct _raid_device *raid_device;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307100 u8 retry_count;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307101 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307102
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307103 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307104
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307105 _scsih_sas_host_refresh(ioc);
7106
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307107 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders start\n",
7108 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307109 /* expanders */
7110 handle = 0xFFFF;
7111 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7112 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7113 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7114 MPI2_IOCSTATUS_MASK;
7115 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7116 break;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307117 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7118 printk(MPT2SAS_INFO_FMT "\tbreak from expander scan: "
7119 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7120 ioc->name, ioc_status,
7121 le32_to_cpu(mpi_reply.IOCLogInfo));
7122 break;
7123 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307124 handle = le16_to_cpu(expander_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307125 spin_lock_irqsave(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307126 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7127 ioc, le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307128 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307129 if (expander_device)
7130 _scsih_refresh_expander_links(ioc, expander_device,
7131 handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307132 else {
7133 printk(MPT2SAS_INFO_FMT "\tBEFORE adding expander: "
7134 "handle (0x%04x), sas_addr(0x%016llx)\n",
7135 ioc->name, handle, (unsigned long long)
7136 le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307137 _scsih_expander_add(ioc, handle);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307138 printk(MPT2SAS_INFO_FMT "\tAFTER adding expander: "
7139 "handle (0x%04x), sas_addr(0x%016llx)\n",
7140 ioc->name, handle, (unsigned long long)
7141 le64_to_cpu(expander_pg0.SASAddress));
7142 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307143 }
7144
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307145 printk(MPT2SAS_INFO_FMT "\tscan devices: expanders complete\n",
7146 ioc->name);
7147
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307148 if (!ioc->ir_firmware)
7149 goto skip_to_sas;
7150
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307151 printk(MPT2SAS_INFO_FMT "\tscan devices phys disk start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307152 /* phys disk */
7153 phys_disk_num = 0xFF;
7154 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7155 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7156 phys_disk_num))) {
7157 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7158 MPI2_IOCSTATUS_MASK;
7159 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7160 break;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307161 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7162 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan:"
7163 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7164 ioc->name, ioc_status,
7165 le32_to_cpu(mpi_reply.IOCLogInfo));
7166 break;
7167 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307168 phys_disk_num = pd_pg0.PhysDiskNum;
7169 handle = le16_to_cpu(pd_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307170 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307171 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307172 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307173 if (sas_device)
7174 continue;
7175 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7176 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7177 handle) != 0)
7178 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307179 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7180 MPI2_IOCSTATUS_MASK;
7181 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7182 printk(MPT2SAS_INFO_FMT "\tbreak from phys disk scan "
7183 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7184 ioc->name, ioc_status,
7185 le32_to_cpu(mpi_reply.IOCLogInfo));
7186 break;
7187 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307188 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7189 if (!_scsih_get_sas_address(ioc, parent_handle,
7190 &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307191 printk(MPT2SAS_INFO_FMT "\tBEFORE adding phys disk: "
7192 " handle (0x%04x), sas_addr(0x%016llx)\n",
7193 ioc->name, handle, (unsigned long long)
7194 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307195 mpt2sas_transport_update_links(ioc, sas_address,
7196 handle, sas_device_pg0.PhyNum,
7197 MPI2_SAS_NEG_LINK_RATE_1_5);
7198 set_bit(handle, ioc->pd_handles);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307199 retry_count = 0;
7200 /* This will retry adding the end device.
7201 * _scsih_add_device() will decide on retries and
7202 * return "1" when it should be retried
7203 */
7204 while (_scsih_add_device(ioc, handle, retry_count++,
7205 1)) {
7206 ssleep(1);
7207 }
7208 printk(MPT2SAS_INFO_FMT "\tAFTER adding phys disk: "
7209 " handle (0x%04x), sas_addr(0x%016llx)\n",
7210 ioc->name, handle, (unsigned long long)
7211 le64_to_cpu(sas_device_pg0.SASAddress));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307212 }
7213 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307214
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307215 printk(MPT2SAS_INFO_FMT "\tscan devices: phys disk complete\n",
7216 ioc->name);
7217
7218 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes start\n", ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307219 /* volumes */
7220 handle = 0xFFFF;
7221 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7222 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7223 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7224 MPI2_IOCSTATUS_MASK;
7225 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7226 break;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307227 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7228 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7229 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7230 ioc->name, ioc_status,
7231 le32_to_cpu(mpi_reply.IOCLogInfo));
7232 break;
7233 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307234 handle = le16_to_cpu(volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307235 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307236 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7237 le64_to_cpu(volume_pg1.WWID));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307238 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307239 if (raid_device)
7240 continue;
7241 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7242 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7243 sizeof(Mpi2RaidVolPage0_t)))
7244 continue;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307245 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7246 MPI2_IOCSTATUS_MASK;
7247 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7248 printk(MPT2SAS_INFO_FMT "\tbreak from volume scan: "
7249 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7250 ioc->name, ioc_status,
7251 le32_to_cpu(mpi_reply.IOCLogInfo));
7252 break;
7253 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307254 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7255 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7256 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7257 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7258 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7259 element.VolDevHandle = volume_pg1.DevHandle;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307260 printk(MPT2SAS_INFO_FMT "\tBEFORE adding volume: "
7261 " handle (0x%04x)\n", ioc->name,
7262 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307263 _scsih_sas_volume_add(ioc, &element);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307264 printk(MPT2SAS_INFO_FMT "\tAFTER adding volume: "
7265 " handle (0x%04x)\n", ioc->name,
7266 volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307267 }
7268 }
7269
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307270 printk(MPT2SAS_INFO_FMT "\tscan devices: volumes complete\n",
7271 ioc->name);
7272
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307273 skip_to_sas:
7274
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307275 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices start\n",
7276 ioc->name);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307277 /* sas devices */
7278 handle = 0xFFFF;
7279 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7280 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7281 handle))) {
7282 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7283 MPI2_IOCSTATUS_MASK;
7284 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7285 break;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307286 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
7287 printk(MPT2SAS_INFO_FMT "\tbreak from end device scan:"
7288 " ioc_status(0x%04x), loginfo(0x%08x)\n",
7289 ioc->name, ioc_status,
7290 le32_to_cpu(mpi_reply.IOCLogInfo));
7291 break;
7292 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307293 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7294 if (!(_scsih_is_end_device(
7295 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7296 continue;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307297 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307298 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7299 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307300 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307301 if (sas_device)
7302 continue;
7303 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7304 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307305 printk(MPT2SAS_INFO_FMT "\tBEFORE adding end device: "
7306 "handle (0x%04x), sas_addr(0x%016llx)\n",
7307 ioc->name, handle, (unsigned long long)
7308 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307309 mpt2sas_transport_update_links(ioc, sas_address, handle,
7310 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307311 retry_count = 0;
7312 /* This will retry adding the end device.
7313 * _scsih_add_device() will decide on retries and
7314 * return "1" when it should be retried
7315 */
7316 while (_scsih_add_device(ioc, handle, retry_count++,
7317 0)) {
7318 ssleep(1);
7319 }
7320 printk(MPT2SAS_INFO_FMT "\tAFTER adding end device: "
7321 "handle (0x%04x), sas_addr(0x%016llx)\n",
7322 ioc->name, handle, (unsigned long long)
7323 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307324 }
7325 }
7326
Sreekanth Reddy6241f222013-02-02 00:56:18 +05307327 printk(MPT2SAS_INFO_FMT "\tscan devices: end devices complete\n",
7328 ioc->name);
7329
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307330 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307331}
7332
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307333
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307334/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307335 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7336 * @ioc: per adapter object
7337 * @reset_phase: phase
7338 *
7339 * The handler for doing any required cleanup or initialization.
7340 *
7341 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7342 * MPT2_IOC_DONE_RESET
7343 *
7344 * Return nothing.
7345 */
7346void
7347mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7348{
7349 switch (reset_phase) {
7350 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307351 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307352 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307353 break;
7354 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307355 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307356 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307357 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7358 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7359 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7360 complete(&ioc->scsih_cmds.done);
7361 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307362 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7363 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7364 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7365 complete(&ioc->tm_cmds.done);
7366 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307367 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307368 _scsih_flush_running_cmds(ioc);
7369 break;
7370 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307371 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307372 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307373 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307374 _scsih_prep_device_scan(ioc);
7375 _scsih_search_responding_sas_devices(ioc);
7376 _scsih_search_responding_raid_devices(ioc);
7377 _scsih_search_responding_expanders(ioc);
sreekanth.reddy@lsi.com14aa7f72012-07-17 15:56:13 +05307378 if ((!ioc->is_driver_loading) && !(disable_discovery > 0 &&
7379 !ioc->sas_hba.num_phys)) {
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307380 _scsih_prep_device_scan(ioc);
7381 _scsih_search_responding_sas_devices(ioc);
7382 _scsih_search_responding_raid_devices(ioc);
7383 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307384 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307385 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307386 break;
Eric Moore635374e2009-03-09 01:21:12 -06007387 }
7388}
7389
7390/**
7391 * _firmware_event_work - delayed task for processing firmware events
7392 * @ioc: per adapter object
7393 * @work: equal to the fw_event_work object
7394 * Context: user.
7395 *
7396 * Return nothing.
7397 */
7398static void
7399_firmware_event_work(struct work_struct *work)
7400{
7401 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307402 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007403 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7404
Eric Moore635374e2009-03-09 01:21:12 -06007405 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007406 if (ioc->remove_host || fw_event->cancel_pending_work ||
7407 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007408 _scsih_fw_event_free(ioc, fw_event);
7409 return;
7410 }
Eric Moore635374e2009-03-09 01:21:12 -06007411
Eric Moore635374e2009-03-09 01:21:12 -06007412 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307413 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307414 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307415 ssleep(1);
7416 _scsih_remove_unresponding_sas_devices(ioc);
7417 _scsih_scan_for_devices_after_reset(ioc);
7418 break;
7419 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307420 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307421
Reddy, Sreekanthb0df96a02013-02-26 16:59:59 +05307422 if (missing_delay[0] != -1 && missing_delay[1] != -1)
7423 mpt2sas_base_update_missing_delay(ioc, missing_delay[0],
7424 missing_delay[1]);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307425
7426 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7427 "from worker thread\n", ioc->name));
7428 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307429 case MPT2SAS_TURN_ON_FAULT_LED:
7430 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7431 break;
Eric Moore635374e2009-03-09 01:21:12 -06007432 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307433 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007434 break;
7435 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307436 _scsih_sas_device_status_change_event(ioc,
7437 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007438 break;
7439 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307440 _scsih_sas_discovery_event(ioc,
7441 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007442 break;
7443 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Andrew Mortona78e21d2012-02-08 12:52:22 -08007444 _scsih_sas_broadcast_primitive_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307445 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007446 break;
7447 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7448 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307449 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007450 break;
7451 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307452 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007453 break;
7454 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307455 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007456 break;
7457 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307458 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007459 break;
7460 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307461 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007462 break;
Eric Moore635374e2009-03-09 01:21:12 -06007463 }
7464 _scsih_fw_event_free(ioc, fw_event);
7465}
7466
7467/**
7468 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7469 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307470 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007471 * @reply: reply message frame(lower 32bit addr)
7472 * Context: interrupt.
7473 *
7474 * This function merely adds a new work task into ioc->firmware_event_thread.
7475 * The tasks are worked from _firmware_event_work in user context.
7476 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307477 * Return 1 meaning mf should be freed from _base_interrupt
7478 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06007479 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307480u8
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)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307491 return 1;
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__);
7498 return 1;
7499 }
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)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307513 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307514
7515 if (ioc->broadcast_aen_busy) {
7516 ioc->broadcast_aen_pending++;
7517 return 1;
7518 } 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 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307593 return 1;
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__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307600 return 1;
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);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307608 return 1;
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);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307618 return 1;
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
7717 /* are there any volumes ? */
7718 if (list_empty(&ioc->raid_device_list))
7719 return;
7720
7721 mutex_lock(&ioc->scsih_cmds.mutex);
7722
7723 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7724 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7725 ioc->name, __func__);
7726 goto out;
7727 }
7728 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7729
7730 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7731 if (!smid) {
7732 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7733 ioc->name, __func__);
7734 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7735 goto out;
7736 }
7737
7738 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7739 ioc->scsih_cmds.smid = smid;
7740 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7741
7742 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7743 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7744
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307745 if (!ioc->hide_ir_msg)
7746 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307747 init_completion(&ioc->scsih_cmds.done);
7748 mpt2sas_base_put_smid_default(ioc, smid);
7749 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7750
7751 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7752 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7753 ioc->name, __func__);
7754 goto out;
7755 }
7756
7757 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7758 mpi_reply = ioc->scsih_cmds.reply;
7759
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307760 if (!ioc->hide_ir_msg)
7761 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7762 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7763 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7764 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307765 }
7766
7767 out:
7768 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7769 mutex_unlock(&ioc->scsih_cmds.mutex);
7770}
7771
7772/**
7773 * _scsih_shutdown - routine call during system shutdown
7774 * @pdev: PCI device struct
7775 *
7776 * Return nothing.
7777 */
7778static void
7779_scsih_shutdown(struct pci_dev *pdev)
7780{
7781 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7782 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307783 struct workqueue_struct *wq;
7784 unsigned long flags;
7785
7786 ioc->remove_host = 1;
7787 _scsih_fw_event_cleanup_queue(ioc);
7788
7789 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7790 wq = ioc->firmware_event_thread;
7791 ioc->firmware_event_thread = NULL;
7792 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7793 if (wq)
7794 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307795
7796 _scsih_ir_shutdown(ioc);
7797 mpt2sas_base_detach(ioc);
7798}
7799
7800/**
Eric Moored5d135b2009-05-18 13:02:08 -06007801 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007802 * @pdev: PCI device struct
7803 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307804 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007805 * Return nothing.
7806 */
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08007807static void
Eric Moored5d135b2009-05-18 13:02:08 -06007808_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007809{
7810 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7811 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307812 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307813 struct _raid_device *raid_device, *next;
7814 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007815 struct workqueue_struct *wq;
7816 unsigned long flags;
7817
7818 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307819 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007820
7821 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7822 wq = ioc->firmware_event_thread;
7823 ioc->firmware_event_thread = NULL;
7824 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7825 if (wq)
7826 destroy_workqueue(wq);
7827
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307828 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307829 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307830 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7831 list) {
7832 if (raid_device->starget) {
7833 sas_target_priv_data =
7834 raid_device->starget->hostdata;
7835 sas_target_priv_data->deleted = 1;
7836 scsi_remove_target(&raid_device->starget->dev);
7837 }
7838 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7839 "(0x%016llx)\n", ioc->name, raid_device->handle,
7840 (unsigned long long) raid_device->wwid);
7841 _scsih_raid_device_remove(ioc, raid_device);
7842 }
7843
Eric Moore635374e2009-03-09 01:21:12 -06007844 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307845 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007846 &ioc->sas_hba.sas_port_list, port_list) {
7847 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307848 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307849 mpt2sas_device_remove_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007850 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307851 else if (mpt2sas_port->remote_identify.device_type ==
7852 SAS_EDGE_EXPANDER_DEVICE ||
7853 mpt2sas_port->remote_identify.device_type ==
7854 SAS_FANOUT_EXPANDER_DEVICE)
7855 mpt2sas_expander_remove(ioc,
7856 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007857 }
7858
7859 /* free phys attached to the sas_host */
7860 if (ioc->sas_hba.num_phys) {
7861 kfree(ioc->sas_hba.phy);
7862 ioc->sas_hba.phy = NULL;
7863 ioc->sas_hba.num_phys = 0;
7864 }
7865
7866 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307867 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007868 list_del(&ioc->list);
7869 scsi_remove_host(shost);
7870 scsi_host_put(shost);
7871}
7872
7873/**
7874 * _scsih_probe_boot_devices - reports 1st device
7875 * @ioc: per adapter object
7876 *
7877 * If specified in bios page 2, this routine reports the 1st
7878 * device scsi-ml or sas transport for persistent boot device
7879 * purposes. Please refer to function _scsih_determine_boot_device()
7880 */
7881static void
7882_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7883{
7884 u8 is_raid;
7885 void *device;
7886 struct _sas_device *sas_device;
7887 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307888 u16 handle;
7889 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007890 u64 sas_address;
7891 unsigned long flags;
7892 int rc;
7893
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307894 /* no Bios, return immediately */
7895 if (!ioc->bios_pg3.BiosVersion)
7896 return;
7897
Eric Moore635374e2009-03-09 01:21:12 -06007898 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307899 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007900 if (ioc->req_boot_device.device) {
7901 device = ioc->req_boot_device.device;
7902 is_raid = ioc->req_boot_device.is_raid;
7903 } else if (ioc->req_alt_boot_device.device) {
7904 device = ioc->req_alt_boot_device.device;
7905 is_raid = ioc->req_alt_boot_device.is_raid;
7906 } else if (ioc->current_boot_device.device) {
7907 device = ioc->current_boot_device.device;
7908 is_raid = ioc->current_boot_device.is_raid;
7909 }
7910
7911 if (!device)
7912 return;
7913
7914 if (is_raid) {
7915 raid_device = device;
7916 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7917 raid_device->id, 0);
7918 if (rc)
7919 _scsih_raid_device_remove(ioc, raid_device);
7920 } else {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307921 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007922 sas_device = device;
7923 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307924 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007925 sas_address = sas_device->sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06007926 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7927 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307928
7929 if (ioc->hide_drives)
7930 return;
Eric Moore635374e2009-03-09 01:21:12 -06007931 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307932 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007933 _scsih_sas_device_remove(ioc, sas_device);
7934 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307935 if (!ioc->is_driver_loading)
7936 mpt2sas_transport_port_remove(ioc, sas_address,
7937 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007938 _scsih_sas_device_remove(ioc, sas_device);
7939 }
7940 }
7941}
7942
7943/**
7944 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7945 * @ioc: per adapter object
7946 *
7947 * Called during initial loading of the driver.
7948 */
7949static void
7950_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7951{
7952 struct _raid_device *raid_device, *raid_next;
7953 int rc;
7954
7955 list_for_each_entry_safe(raid_device, raid_next,
7956 &ioc->raid_device_list, list) {
7957 if (raid_device->starget)
7958 continue;
7959 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7960 raid_device->id, 0);
7961 if (rc)
7962 _scsih_raid_device_remove(ioc, raid_device);
7963 }
7964}
7965
7966/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307967 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007968 * @ioc: per adapter object
7969 *
7970 * Called during initial loading of the driver.
7971 */
7972static void
7973_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7974{
7975 struct _sas_device *sas_device, *next;
7976 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007977
7978 /* SAS Device List */
7979 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7980 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007981
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307982 if (ioc->hide_drives)
7983 continue;
7984
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307985 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7986 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307987 list_del(&sas_device->list);
7988 kfree(sas_device);
7989 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007990 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307991 if (!ioc->is_driver_loading)
7992 mpt2sas_transport_port_remove(ioc,
7993 sas_device->sas_address,
7994 sas_device->sas_address_parent);
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307995 list_del(&sas_device->list);
7996 kfree(sas_device);
7997 continue;
7998
Eric Moore635374e2009-03-09 01:21:12 -06007999 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05308000 spin_lock_irqsave(&ioc->sas_device_lock, flags);
8001 list_move_tail(&sas_device->list, &ioc->sas_device_list);
8002 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06008003 }
8004}
8005
8006/**
8007 * _scsih_probe_devices - probing for devices
8008 * @ioc: per adapter object
8009 *
8010 * Called during initial loading of the driver.
8011 */
8012static void
8013_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
8014{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308015 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06008016
8017 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
8018 return; /* return when IOC doesn't support initiator mode */
8019
8020 _scsih_probe_boot_devices(ioc);
8021
8022 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308023 volume_mapping_flags =
8024 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
8025 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
8026 if (volume_mapping_flags ==
8027 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06008028 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308029 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008030 } else {
Eric Moore635374e2009-03-09 01:21:12 -06008031 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308032 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008033 }
8034 } else
8035 _scsih_probe_sas(ioc);
8036}
8037
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308038
8039/**
8040 * _scsih_scan_start - scsi lld callback for .scan_start
8041 * @shost: SCSI host pointer
8042 *
8043 * The shost has the ability to discover targets on its own instead
8044 * of scanning the entire bus. In our implemention, we will kick off
8045 * firmware discovery.
8046 */
8047static void
8048_scsih_scan_start(struct Scsi_Host *shost)
8049{
8050 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8051 int rc;
8052
8053 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
8054 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
8055
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308056 if (disable_discovery > 0)
8057 return;
8058
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308059 ioc->start_scan = 1;
8060 rc = mpt2sas_port_enable(ioc);
8061
8062 if (rc != 0)
8063 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
8064}
8065
8066/**
8067 * _scsih_scan_finished - scsi lld callback for .scan_finished
8068 * @shost: SCSI host pointer
8069 * @time: elapsed time of the scan in jiffies
8070 *
8071 * This function will be called periodically until it returns 1 with the
8072 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
8073 * we wait for firmware discovery to complete, then return 1.
8074 */
8075static int
8076_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
8077{
8078 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8079
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05308080 if (disable_discovery > 0) {
8081 ioc->is_driver_loading = 0;
8082 ioc->wait_for_discovery_to_complete = 0;
8083 return 1;
8084 }
8085
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308086 if (time >= (300 * HZ)) {
8087 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8088 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
8089 "(timeout=300s)\n", ioc->name);
8090 ioc->is_driver_loading = 0;
8091 return 1;
8092 }
8093
8094 if (ioc->start_scan)
8095 return 0;
8096
8097 if (ioc->start_scan_failed) {
8098 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
8099 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
8100 ioc->is_driver_loading = 0;
8101 ioc->wait_for_discovery_to_complete = 0;
8102 ioc->remove_host = 1;
8103 return 1;
8104 }
8105
8106 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
8107 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
8108
8109 if (ioc->wait_for_discovery_to_complete) {
8110 ioc->wait_for_discovery_to_complete = 0;
8111 _scsih_probe_devices(ioc);
8112 }
8113 mpt2sas_base_start_watchdog(ioc);
8114 ioc->is_driver_loading = 0;
8115 return 1;
8116}
8117
8118
Eric Moore635374e2009-03-09 01:21:12 -06008119/**
Eric Moored5d135b2009-05-18 13:02:08 -06008120 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06008121 * @pdev: PCI device struct
8122 * @id: pci device id
8123 *
8124 * Returns 0 success, anything else error.
8125 */
8126static int
Eric Moored5d135b2009-05-18 13:02:08 -06008127_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06008128{
8129 struct MPT2SAS_ADAPTER *ioc;
8130 struct Scsi_Host *shost;
8131
8132 shost = scsi_host_alloc(&scsih_driver_template,
8133 sizeof(struct MPT2SAS_ADAPTER));
8134 if (!shost)
8135 return -ENODEV;
8136
8137 /* init local params */
8138 ioc = shost_priv(shost);
8139 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
8140 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06008141 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06008142 ioc->shost = shost;
8143 ioc->id = mpt_ids++;
8144 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
8145 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308146 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8147 ioc->is_warpdrive = 1;
8148 ioc->hide_ir_msg = 1;
8149 } else
8150 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06008151 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8152 ioc->tm_cb_idx = tm_cb_idx;
8153 ioc->ctl_cb_idx = ctl_cb_idx;
8154 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308155 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008156 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05308157 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008158 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308159 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308160 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308161 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008162 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05308163 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06008164 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05308165 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06008166 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8167 spin_lock_init(&ioc->scsi_lookup_lock);
8168 spin_lock_init(&ioc->sas_device_lock);
8169 spin_lock_init(&ioc->sas_node_lock);
8170 spin_lock_init(&ioc->fw_event_lock);
8171 spin_lock_init(&ioc->raid_device_lock);
8172
8173 INIT_LIST_HEAD(&ioc->sas_device_list);
8174 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8175 INIT_LIST_HEAD(&ioc->sas_expander_list);
8176 INIT_LIST_HEAD(&ioc->fw_event_list);
8177 INIT_LIST_HEAD(&ioc->raid_device_list);
8178 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308179 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308180 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06008181
8182 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06008183 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06008184 shost->max_lun = max_lun;
8185 shost->transportt = mpt2sas_transport_template;
8186 shost->unique_id = ioc->id;
8187
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308188 if (max_sectors != 0xFFFF) {
8189 if (max_sectors < 64) {
8190 shost->max_sectors = 64;
Sreekanth Reddy6241f222013-02-02 00:56:18 +05308191 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8192 "for max_sectors, range is 64 to 32767. Assigning "
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308193 "value of 64.\n", ioc->name, max_sectors);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308194 } else if (max_sectors > 32767) {
8195 shost->max_sectors = 32767;
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308196 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8197 "for max_sectors, range is 64 to 8192. Assigning "
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308198 "default value of 32767.\n", ioc->name,
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308199 max_sectors);
8200 } else {
8201 shost->max_sectors = max_sectors & 0xFFFE;
8202 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
8203 "set to %d\n", ioc->name, shost->max_sectors);
8204 }
8205 }
8206
Eric Moore635374e2009-03-09 01:21:12 -06008207 if ((scsi_add_host(shost, &pdev->dev))) {
8208 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8209 ioc->name, __FILE__, __LINE__, __func__);
8210 list_del(&ioc->list);
8211 goto out_add_shost_fail;
8212 }
8213
Martin K. Petersen5e95e732012-08-28 14:29:37 -04008214 /* register EEDP capabilities with SCSI layer */
8215 if (prot_mask)
8216 scsi_host_set_prot(shost, prot_mask);
8217 else
8218 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
8219 | SHOST_DIF_TYPE2_PROTECTION
8220 | SHOST_DIF_TYPE3_PROTECTION);
8221
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308222 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06008223
Eric Moore635374e2009-03-09 01:21:12 -06008224 /* event thread */
8225 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8226 "fw_event%d", ioc->id);
8227 ioc->firmware_event_thread = create_singlethread_workqueue(
8228 ioc->firmware_event_name);
8229 if (!ioc->firmware_event_thread) {
8230 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8231 ioc->name, __FILE__, __LINE__, __func__);
8232 goto out_thread_fail;
8233 }
8234
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308235 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008236 if ((mpt2sas_base_attach(ioc))) {
8237 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8238 ioc->name, __FILE__, __LINE__, __func__);
8239 goto out_attach_fail;
8240 }
8241
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308242 if (ioc->is_warpdrive) {
8243 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8244 ioc->hide_drives = 0;
8245 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8246 ioc->hide_drives = 1;
8247 else {
8248 if (_scsih_get_num_volumes(ioc))
8249 ioc->hide_drives = 1;
8250 else
8251 ioc->hide_drives = 0;
8252 }
8253 } else
8254 ioc->hide_drives = 0;
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05308255 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308256
Eric Moore635374e2009-03-09 01:21:12 -06008257 return 0;
8258
8259 out_attach_fail:
8260 destroy_workqueue(ioc->firmware_event_thread);
8261 out_thread_fail:
8262 list_del(&ioc->list);
8263 scsi_remove_host(shost);
8264 out_add_shost_fail:
Tomas Henzlf555e052012-04-17 15:24:48 +02008265 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008266 return -ENODEV;
8267}
8268
8269#ifdef CONFIG_PM
8270/**
Eric Moored5d135b2009-05-18 13:02:08 -06008271 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008272 * @pdev: PCI device struct
8273 * @state: PM state change to (usually PCI_D3)
8274 *
8275 * Returns 0 success, anything else error.
8276 */
8277static int
Eric Moored5d135b2009-05-18 13:02:08 -06008278_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008279{
8280 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8281 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308282 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008283
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308284 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008285 scsi_block_requests(shost);
8286 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);
8293 pci_disable_device(pdev);
8294 pci_set_power_state(pdev, device_state);
8295 return 0;
8296}
8297
8298/**
Eric Moored5d135b2009-05-18 13:02:08 -06008299 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008300 * @pdev: PCI device struct
8301 *
8302 * Returns 0 success, anything else error.
8303 */
8304static int
Eric Moored5d135b2009-05-18 13:02:08 -06008305_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008306{
8307 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8308 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308309 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008310 int r;
8311
8312 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8313 "operating state [D%d]\n", ioc->name, pdev,
8314 pci_name(pdev), device_state);
8315
8316 pci_set_power_state(pdev, PCI_D0);
8317 pci_enable_wake(pdev, PCI_D0, 0);
8318 pci_restore_state(pdev);
8319 ioc->pdev = pdev;
8320 r = mpt2sas_base_map_resources(ioc);
8321 if (r)
8322 return r;
8323
8324 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8325 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308326 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008327 return 0;
8328}
8329#endif /* CONFIG_PM */
8330
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308331/**
8332 * _scsih_pci_error_detected - Called when a PCI error is detected.
8333 * @pdev: PCI device struct
8334 * @state: PCI channel state
8335 *
8336 * Description: Called when a PCI error is detected.
8337 *
8338 * Return value:
8339 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8340 */
8341static pci_ers_result_t
8342_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8343{
8344 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8345 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8346
8347 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8348 ioc->name, state);
8349
8350 switch (state) {
8351 case pci_channel_io_normal:
8352 return PCI_ERS_RESULT_CAN_RECOVER;
8353 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008354 /* Fatal error, prepare for slot reset */
8355 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308356 scsi_block_requests(ioc->shost);
8357 mpt2sas_base_stop_watchdog(ioc);
8358 mpt2sas_base_free_resources(ioc);
8359 return PCI_ERS_RESULT_NEED_RESET;
8360 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008361 /* Permanent error, prepare for device removal */
8362 ioc->pci_error_recovery = 1;
8363 mpt2sas_base_stop_watchdog(ioc);
8364 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308365 return PCI_ERS_RESULT_DISCONNECT;
8366 }
8367 return PCI_ERS_RESULT_NEED_RESET;
8368}
8369
8370/**
8371 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8372 * @pdev: PCI device struct
8373 *
8374 * Description: This routine is called by the pci error recovery
8375 * code after the PCI slot has been reset, just before we
8376 * should resume normal operations.
8377 */
8378static pci_ers_result_t
8379_scsih_pci_slot_reset(struct pci_dev *pdev)
8380{
8381 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8382 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8383 int rc;
8384
8385 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8386 ioc->name);
8387
Eric Moore3cb54692010-07-08 14:44:34 -06008388 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308389 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008390 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308391 rc = mpt2sas_base_map_resources(ioc);
8392 if (rc)
8393 return PCI_ERS_RESULT_DISCONNECT;
8394
8395
8396 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8397 FORCE_BIG_HAMMER);
8398
8399 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8400 (rc == 0) ? "success" : "failed");
8401
8402 if (!rc)
8403 return PCI_ERS_RESULT_RECOVERED;
8404 else
8405 return PCI_ERS_RESULT_DISCONNECT;
8406}
8407
8408/**
8409 * _scsih_pci_resume() - resume normal ops after PCI reset
8410 * @pdev: pointer to PCI device
8411 *
8412 * Called when the error recovery driver tells us that its
8413 * OK to resume normal operation. Use completion to allow
8414 * halted scsi ops to resume.
8415 */
8416static void
8417_scsih_pci_resume(struct pci_dev *pdev)
8418{
8419 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8420 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8421
8422 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8423
8424 pci_cleanup_aer_uncorrect_error_status(pdev);
8425 mpt2sas_base_start_watchdog(ioc);
8426 scsi_unblock_requests(ioc->shost);
8427}
8428
8429/**
8430 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8431 * @pdev: pointer to PCI device
8432 */
8433static pci_ers_result_t
8434_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8435{
8436 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8437 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8438
8439 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8440 ioc->name);
8441
8442 /* TODO - dump whatever for debugging purposes */
8443
8444 /* Request a slot reset. */
8445 return PCI_ERS_RESULT_NEED_RESET;
8446}
8447
Stephen Hemmingera55b2d22012-09-07 09:33:16 -07008448static const struct pci_error_handlers _scsih_err_handler = {
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308449 .error_detected = _scsih_pci_error_detected,
8450 .mmio_enabled = _scsih_pci_mmio_enabled,
8451 .slot_reset = _scsih_pci_slot_reset,
8452 .resume = _scsih_pci_resume,
8453};
Eric Moore635374e2009-03-09 01:21:12 -06008454
8455static struct pci_driver scsih_driver = {
8456 .name = MPT2SAS_DRIVER_NAME,
8457 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008458 .probe = _scsih_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08008459 .remove = _scsih_remove,
Kashyap, Desai744090d2009-10-05 15:56:56 +05308460 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308461 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008462#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008463 .suspend = _scsih_suspend,
8464 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008465#endif
8466};
8467
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308468/* raid transport support */
8469static struct raid_function_template mpt2sas_raid_functions = {
8470 .cookie = &scsih_driver_template,
8471 .is_raid = _scsih_is_raid,
8472 .get_resync = _scsih_get_resync,
8473 .get_state = _scsih_get_state,
8474};
Eric Moore635374e2009-03-09 01:21:12 -06008475
8476/**
Eric Moored5d135b2009-05-18 13:02:08 -06008477 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008478 *
8479 * Returns 0 success, anything else error.
8480 */
8481static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008482_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008483{
8484 int error;
8485
8486 mpt_ids = 0;
8487 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8488 MPT2SAS_DRIVER_VERSION);
8489
8490 mpt2sas_transport_template =
8491 sas_attach_transport(&mpt2sas_transport_functions);
8492 if (!mpt2sas_transport_template)
8493 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308494 /* raid transport support */
8495 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8496 if (!mpt2sas_raid_template) {
8497 sas_release_transport(mpt2sas_transport_template);
8498 return -ENODEV;
8499 }
Eric Moore635374e2009-03-09 01:21:12 -06008500
8501 mpt2sas_base_initialize_callback_handler();
8502
8503 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008504 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008505
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008506 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008507 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008508
8509 /* base internal commands callback handler */
8510 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308511 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8512 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008513
8514 /* transport internal commands callback handler */
8515 transport_cb_idx = mpt2sas_base_register_callback_handler(
8516 mpt2sas_transport_done);
8517
Kashyap, Desai744090d2009-10-05 15:56:56 +05308518 /* scsih internal commands callback handler */
8519 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8520
Eric Moore635374e2009-03-09 01:21:12 -06008521 /* configuration page API internal commands callback handler */
8522 config_cb_idx = mpt2sas_base_register_callback_handler(
8523 mpt2sas_config_done);
8524
8525 /* ctl module callback handler */
8526 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8527
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308528 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8529 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308530
8531 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8532 _scsih_tm_volume_tr_complete);
8533
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308534 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8535 _scsih_sas_control_complete);
8536
Eric Moore635374e2009-03-09 01:21:12 -06008537 mpt2sas_ctl_init();
8538
8539 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308540 if (error) {
8541 /* raid transport support */
8542 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008543 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308544 }
Eric Moore635374e2009-03-09 01:21:12 -06008545
8546 return error;
8547}
8548
8549/**
Eric Moored5d135b2009-05-18 13:02:08 -06008550 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008551 *
8552 * Returns 0 success, anything else error.
8553 */
8554static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008555_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008556{
8557 printk(KERN_INFO "mpt2sas version %s unloading\n",
8558 MPT2SAS_DRIVER_VERSION);
8559
8560 pci_unregister_driver(&scsih_driver);
8561
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308562 mpt2sas_ctl_exit();
8563
Eric Moore635374e2009-03-09 01:21:12 -06008564 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8565 mpt2sas_base_release_callback_handler(tm_cb_idx);
8566 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308567 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008568 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308569 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008570 mpt2sas_base_release_callback_handler(config_cb_idx);
8571 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8572
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308573 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308574 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308575 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8576
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308577 /* raid transport support */
8578 raid_class_release(mpt2sas_raid_template);
8579 sas_release_transport(mpt2sas_transport_template);
8580
Eric Moore635374e2009-03-09 01:21:12 -06008581}
8582
Eric Moored5d135b2009-05-18 13:02:08 -06008583module_init(_scsih_init);
8584module_exit(_scsih_exit);