blob: 66c773662694c590623b0724d60801afb2850526 [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
Eric Moore635374e2009-03-09 01:21:12 -0600104/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
105#define MPT2SAS_MAX_LUN (16895)
106static int max_lun = MPT2SAS_MAX_LUN;
107module_param(max_lun, int, 0);
108MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
109
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530110/* diag_buffer_enable is bitwise
111 * bit 0 set = TRACE
112 * bit 1 set = SNAPSHOT
113 * bit 2 set = EXTENDED
114 *
115 * Either bit can be set, or both
116 */
117static int diag_buffer_enable = -1;
118module_param(diag_buffer_enable, int, 0);
119MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
120 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
121
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +0530122static int disable_discovery = -1;
123module_param(disable_discovery, int, 0);
124MODULE_PARM_DESC(disable_discovery, " disable discovery ");
125
Eric Moore635374e2009-03-09 01:21:12 -0600126/**
127 * struct sense_info - common structure for obtaining sense keys
128 * @skey: sense key
129 * @asc: additional sense code
130 * @ascq: additional sense code qualifier
131 */
132struct sense_info {
133 u8 skey;
134 u8 asc;
135 u8 ascq;
136};
137
138
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530139#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530140#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
141#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
Eric Moore635374e2009-03-09 01:21:12 -0600142/**
143 * struct fw_event_work - firmware event struct
144 * @list: link list framework
145 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530146 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600147 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530148 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600149 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530150 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600151 * @ignore: flag meaning this event has been marked to ignore
152 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
153 * @event_data: reply event data payload follows
154 *
155 * This object stored on ioc->fw_event_list.
156 */
157struct fw_event_work {
158 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530159 u8 cancel_pending_work;
160 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600161 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530162 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600163 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530164 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600165 u8 ignore;
166 u16 event;
167 void *event_data;
168};
169
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530170/* raid transport support */
171static struct raid_template *mpt2sas_raid_template;
172
Eric Moore635374e2009-03-09 01:21:12 -0600173/**
174 * struct _scsi_io_transfer - scsi io transfer
175 * @handle: sas device handle (assigned by firmware)
176 * @is_raid: flag set for hidden raid components
177 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
178 * @data_length: data transfer length
179 * @data_dma: dma pointer to data
180 * @sense: sense data
181 * @lun: lun number
182 * @cdb_length: cdb length
183 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600184 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530185 * @VF_ID: virtual function id
186 * @VP_ID: virtual port id
187 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600188 * @sense_length: sense length
189 * @ioc_status: ioc status
190 * @scsi_state: scsi state
191 * @scsi_status: scsi staus
192 * @log_info: log information
193 * @transfer_length: data length transfer when there is a reply message
194 *
195 * Used for sending internal scsi commands to devices within this module.
196 * Refer to _scsi_send_scsi_io().
197 */
198struct _scsi_io_transfer {
199 u16 handle;
200 u8 is_raid;
201 enum dma_data_direction dir;
202 u32 data_length;
203 dma_addr_t data_dma;
204 u8 sense[SCSI_SENSE_BUFFERSIZE];
205 u32 lun;
206 u8 cdb_length;
207 u8 cdb[32];
208 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530209 u8 VF_ID;
210 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600211 u8 valid_reply;
212 /* the following bits are only valid when 'valid_reply = 1' */
213 u32 sense_length;
214 u16 ioc_status;
215 u8 scsi_state;
216 u8 scsi_status;
217 u32 log_info;
218 u32 transfer_length;
219};
220
221/*
222 * The pci device ids are defined in mpi/mpi2_cnfg.h.
223 */
224static struct pci_device_id scsih_pci_table[] = {
225 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
226 PCI_ANY_ID, PCI_ANY_ID },
227 /* Falcon ~ 2008*/
228 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
229 PCI_ANY_ID, PCI_ANY_ID },
230 /* Liberator ~ 2108 */
231 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
232 PCI_ANY_ID, PCI_ANY_ID },
233 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
234 PCI_ANY_ID, PCI_ANY_ID },
235 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
236 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530237 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600238 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
239 PCI_ANY_ID, PCI_ANY_ID },
240 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
241 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530242 /* Thunderbolt ~ 2208 */
243 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
244 PCI_ANY_ID, PCI_ANY_ID },
245 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
246 PCI_ANY_ID, PCI_ANY_ID },
247 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
248 PCI_ANY_ID, PCI_ANY_ID },
249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
250 PCI_ANY_ID, PCI_ANY_ID },
251 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
252 PCI_ANY_ID, PCI_ANY_ID },
253 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
254 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530255 /* Mustang ~ 2308 */
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530257 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530258 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
259 PCI_ANY_ID, PCI_ANY_ID },
260 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530261 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530262 /* SSS6200 */
263 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
264 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600265 {0} /* Terminating entry */
266};
267MODULE_DEVICE_TABLE(pci, scsih_pci_table);
268
269/**
Eric Moored5d135b2009-05-18 13:02:08 -0600270 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600271 *
272 * Note: The logging levels are defined in mpt2sas_debug.h.
273 */
274static int
Eric Moored5d135b2009-05-18 13:02:08 -0600275_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600276{
277 int ret = param_set_int(val, kp);
278 struct MPT2SAS_ADAPTER *ioc;
279
280 if (ret)
281 return ret;
282
283 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600284 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600285 ioc->logging_level = logging_level;
286 return 0;
287}
Eric Moored5d135b2009-05-18 13:02:08 -0600288module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600289 &logging_level, 0644);
290
291/**
292 * _scsih_srch_boot_sas_address - search based on sas_address
293 * @sas_address: sas address
294 * @boot_device: boot device object from bios page 2
295 *
296 * Returns 1 when there's a match, 0 means no match.
297 */
298static inline int
299_scsih_srch_boot_sas_address(u64 sas_address,
300 Mpi2BootDeviceSasWwid_t *boot_device)
301{
302 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
303}
304
305/**
306 * _scsih_srch_boot_device_name - search based on device name
307 * @device_name: device name specified in INDENTIFY fram
308 * @boot_device: boot device object from bios page 2
309 *
310 * Returns 1 when there's a match, 0 means no match.
311 */
312static inline int
313_scsih_srch_boot_device_name(u64 device_name,
314 Mpi2BootDeviceDeviceName_t *boot_device)
315{
316 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
317}
318
319/**
320 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
321 * @enclosure_logical_id: enclosure logical id
322 * @slot_number: slot number
323 * @boot_device: boot device object from bios page 2
324 *
325 * Returns 1 when there's a match, 0 means no match.
326 */
327static inline int
328_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
329 Mpi2BootDeviceEnclosureSlot_t *boot_device)
330{
331 return (enclosure_logical_id == le64_to_cpu(boot_device->
332 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
333 SlotNumber)) ? 1 : 0;
334}
335
336/**
337 * _scsih_is_boot_device - search for matching boot device.
338 * @sas_address: sas address
339 * @device_name: device name specified in INDENTIFY fram
340 * @enclosure_logical_id: enclosure logical id
341 * @slot_number: slot number
342 * @form: specifies boot device form
343 * @boot_device: boot device object from bios page 2
344 *
345 * Returns 1 when there's a match, 0 means no match.
346 */
347static int
348_scsih_is_boot_device(u64 sas_address, u64 device_name,
349 u64 enclosure_logical_id, u16 slot, u8 form,
350 Mpi2BiosPage2BootDevice_t *boot_device)
351{
352 int rc = 0;
353
354 switch (form) {
355 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
356 if (!sas_address)
357 break;
358 rc = _scsih_srch_boot_sas_address(
359 sas_address, &boot_device->SasWwid);
360 break;
361 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
362 if (!enclosure_logical_id)
363 break;
364 rc = _scsih_srch_boot_encl_slot(
365 enclosure_logical_id,
366 slot, &boot_device->EnclosureSlot);
367 break;
368 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
369 if (!device_name)
370 break;
371 rc = _scsih_srch_boot_device_name(
372 device_name, &boot_device->DeviceName);
373 break;
374 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
375 break;
376 }
377
378 return rc;
379}
380
381/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530382 * _scsih_get_sas_address - set the sas_address for given device handle
383 * @handle: device handle
384 * @sas_address: sas address
385 *
386 * Returns 0 success, non-zero when failure
387 */
388static int
389_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
390 u64 *sas_address)
391{
392 Mpi2SasDevicePage0_t sas_device_pg0;
393 Mpi2ConfigReply_t mpi_reply;
394 u32 ioc_status;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530395 *sas_address = 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530396
397 if (handle <= ioc->sas_hba.num_phys) {
398 *sas_address = ioc->sas_hba.sas_address;
399 return 0;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530400 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530401
402 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
403 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530404 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
405 __FILE__, __LINE__, __func__);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530406 return -ENXIO;
407 }
408
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530409 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
410 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
411 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
412 return 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530413 }
414
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530415 /* we hit this becuase the given parent handle doesn't exist */
416 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
417 return -ENXIO;
418 /* else error case */
419 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
420 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
421 __FILE__, __LINE__, __func__);
422 return -EIO;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530423}
424
425/**
Eric Moore635374e2009-03-09 01:21:12 -0600426 * _scsih_determine_boot_device - determine boot device.
427 * @ioc: per adapter object
428 * @device: either sas_device or raid_device object
429 * @is_raid: [flag] 1 = raid object, 0 = sas object
430 *
431 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300432 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600433 * There are primary, alternate, and current entries in bios page 2. The order
434 * priority is primary, alternate, then current. This routine saves
435 * the corresponding device object and is_raid flag in the ioc object.
436 * The saved data to be used later in _scsih_probe_boot_devices().
437 */
438static void
439_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
440 void *device, u8 is_raid)
441{
442 struct _sas_device *sas_device;
443 struct _raid_device *raid_device;
444 u64 sas_address;
445 u64 device_name;
446 u64 enclosure_logical_id;
447 u16 slot;
448
449 /* only process this function when driver loads */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530450 if (!ioc->is_driver_loading)
451 return;
452
453 /* no Bios, return immediately */
454 if (!ioc->bios_pg3.BiosVersion)
Eric Moore635374e2009-03-09 01:21:12 -0600455 return;
456
457 if (!is_raid) {
458 sas_device = device;
459 sas_address = sas_device->sas_address;
460 device_name = sas_device->device_name;
461 enclosure_logical_id = sas_device->enclosure_logical_id;
462 slot = sas_device->slot;
463 } else {
464 raid_device = device;
465 sas_address = raid_device->wwid;
466 device_name = 0;
467 enclosure_logical_id = 0;
468 slot = 0;
469 }
470
471 if (!ioc->req_boot_device.device) {
472 if (_scsih_is_boot_device(sas_address, device_name,
473 enclosure_logical_id, slot,
474 (ioc->bios_pg2.ReqBootDeviceForm &
475 MPI2_BIOSPAGE2_FORM_MASK),
476 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530477 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600478 "%s: req_boot_device(0x%016llx)\n",
479 ioc->name, __func__,
480 (unsigned long long)sas_address));
481 ioc->req_boot_device.device = device;
482 ioc->req_boot_device.is_raid = is_raid;
483 }
484 }
485
486 if (!ioc->req_alt_boot_device.device) {
487 if (_scsih_is_boot_device(sas_address, device_name,
488 enclosure_logical_id, slot,
489 (ioc->bios_pg2.ReqAltBootDeviceForm &
490 MPI2_BIOSPAGE2_FORM_MASK),
491 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530492 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600493 "%s: req_alt_boot_device(0x%016llx)\n",
494 ioc->name, __func__,
495 (unsigned long long)sas_address));
496 ioc->req_alt_boot_device.device = device;
497 ioc->req_alt_boot_device.is_raid = is_raid;
498 }
499 }
500
501 if (!ioc->current_boot_device.device) {
502 if (_scsih_is_boot_device(sas_address, device_name,
503 enclosure_logical_id, slot,
504 (ioc->bios_pg2.CurrentBootDeviceForm &
505 MPI2_BIOSPAGE2_FORM_MASK),
506 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530507 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600508 "%s: current_boot_device(0x%016llx)\n",
509 ioc->name, __func__,
510 (unsigned long long)sas_address));
511 ioc->current_boot_device.device = device;
512 ioc->current_boot_device.is_raid = is_raid;
513 }
514 }
515}
516
517/**
518 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
519 * @ioc: per adapter object
520 * @sas_address: sas address
521 * Context: Calling function should acquire ioc->sas_device_lock
522 *
523 * This searches for sas_device based on sas_address, then return sas_device
524 * object.
525 */
526struct _sas_device *
527mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
528 u64 sas_address)
529{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530530 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600531
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530532 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
533 if (sas_device->sas_address == sas_address)
534 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600535
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530536 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
537 if (sas_device->sas_address == sas_address)
538 return sas_device;
539
540 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600541}
542
543/**
544 * _scsih_sas_device_find_by_handle - sas device search
545 * @ioc: per adapter object
546 * @handle: sas device handle (assigned by firmware)
547 * Context: Calling function should acquire ioc->sas_device_lock
548 *
549 * This searches for sas_device based on sas_address, then return sas_device
550 * object.
551 */
552static struct _sas_device *
553_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
554{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530555 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600556
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530557 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
558 if (sas_device->handle == handle)
559 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600560
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530561 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
562 if (sas_device->handle == handle)
563 return sas_device;
564
565 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600566}
567
568/**
569 * _scsih_sas_device_remove - remove sas_device from list.
570 * @ioc: per adapter object
571 * @sas_device: the sas_device object
572 * Context: This function will acquire ioc->sas_device_lock.
573 *
574 * Removing object and freeing associated memory from the ioc->sas_device_list.
575 */
576static void
577_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
578 struct _sas_device *sas_device)
579{
580 unsigned long flags;
581
Kashyap, Desai980ead32010-04-08 17:55:22 +0530582 if (!sas_device)
583 return;
584
Eric Moore635374e2009-03-09 01:21:12 -0600585 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530586 list_del(&sas_device->list);
587 kfree(sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600588 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
589}
590
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530591
Eric Moore635374e2009-03-09 01:21:12 -0600592/**
593 * _scsih_sas_device_add - insert sas_device to the list.
594 * @ioc: per adapter object
595 * @sas_device: the sas_device object
596 * Context: This function will acquire ioc->sas_device_lock.
597 *
598 * Adding new object to the ioc->sas_device_list.
599 */
600static void
601_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
602 struct _sas_device *sas_device)
603{
604 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600605
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530606 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600607 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
608 sas_device->handle, (unsigned long long)sas_device->sas_address));
609
610 spin_lock_irqsave(&ioc->sas_device_lock, flags);
611 list_add_tail(&sas_device->list, &ioc->sas_device_list);
612 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
613
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530614 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530615 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -0600616 _scsih_sas_device_remove(ioc, sas_device);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530617 } else if (!sas_device->starget) {
618 /* When asyn scanning is enabled, its not possible to remove
619 * devices while scanning is turned on due to an oops in
620 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
621 */
622 if (!ioc->is_driver_loading)
623 mpt2sas_transport_port_remove(ioc,
624 sas_device->sas_address,
625 sas_device->sas_address_parent);
626 _scsih_sas_device_remove(ioc, sas_device);
627 }
Eric Moore635374e2009-03-09 01:21:12 -0600628}
629
630/**
631 * _scsih_sas_device_init_add - insert sas_device to the list.
632 * @ioc: per adapter object
633 * @sas_device: the sas_device object
634 * Context: This function will acquire ioc->sas_device_lock.
635 *
636 * Adding new object at driver load time to the ioc->sas_device_init_list.
637 */
638static void
639_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
640 struct _sas_device *sas_device)
641{
642 unsigned long flags;
643
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530644 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600645 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
646 sas_device->handle, (unsigned long long)sas_device->sas_address));
647
648 spin_lock_irqsave(&ioc->sas_device_lock, flags);
649 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
Eric Moore635374e2009-03-09 01:21:12 -0600650 _scsih_determine_boot_device(ioc, sas_device, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +0530651 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -0600652}
653
654/**
Eric Moore635374e2009-03-09 01:21:12 -0600655 * _scsih_raid_device_find_by_id - raid device search
656 * @ioc: per adapter object
657 * @id: sas device target id
658 * @channel: sas device channel
659 * Context: Calling function should acquire ioc->raid_device_lock
660 *
661 * This searches for raid_device based on target id, then return raid_device
662 * object.
663 */
664static struct _raid_device *
665_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
666{
667 struct _raid_device *raid_device, *r;
668
669 r = NULL;
670 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
671 if (raid_device->id == id && raid_device->channel == channel) {
672 r = raid_device;
673 goto out;
674 }
675 }
676
677 out:
678 return r;
679}
680
681/**
682 * _scsih_raid_device_find_by_handle - raid device search
683 * @ioc: per adapter object
684 * @handle: sas device handle (assigned by firmware)
685 * Context: Calling function should acquire ioc->raid_device_lock
686 *
687 * This searches for raid_device based on handle, then return raid_device
688 * object.
689 */
690static struct _raid_device *
691_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
692{
693 struct _raid_device *raid_device, *r;
694
695 r = NULL;
696 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
697 if (raid_device->handle != handle)
698 continue;
699 r = raid_device;
700 goto out;
701 }
702
703 out:
704 return r;
705}
706
707/**
708 * _scsih_raid_device_find_by_wwid - raid device search
709 * @ioc: per adapter object
710 * @handle: sas device handle (assigned by firmware)
711 * Context: Calling function should acquire ioc->raid_device_lock
712 *
713 * This searches for raid_device based on wwid, then return raid_device
714 * object.
715 */
716static struct _raid_device *
717_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
718{
719 struct _raid_device *raid_device, *r;
720
721 r = NULL;
722 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
723 if (raid_device->wwid != wwid)
724 continue;
725 r = raid_device;
726 goto out;
727 }
728
729 out:
730 return r;
731}
732
733/**
734 * _scsih_raid_device_add - add raid_device object
735 * @ioc: per adapter object
736 * @raid_device: raid_device object
737 *
738 * This is added to the raid_device_list link list.
739 */
740static void
741_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
742 struct _raid_device *raid_device)
743{
744 unsigned long flags;
745
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530746 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600747 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
748 raid_device->handle, (unsigned long long)raid_device->wwid));
749
750 spin_lock_irqsave(&ioc->raid_device_lock, flags);
751 list_add_tail(&raid_device->list, &ioc->raid_device_list);
752 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
753}
754
755/**
756 * _scsih_raid_device_remove - delete raid_device object
757 * @ioc: per adapter object
758 * @raid_device: raid_device object
759 *
Eric Moore635374e2009-03-09 01:21:12 -0600760 */
761static void
762_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
763 struct _raid_device *raid_device)
764{
765 unsigned long flags;
766
767 spin_lock_irqsave(&ioc->raid_device_lock, flags);
768 list_del(&raid_device->list);
Eric Moore635374e2009-03-09 01:21:12 -0600769 kfree(raid_device);
770 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
771}
772
773/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530774 * mpt2sas_scsih_expander_find_by_handle - expander device search
775 * @ioc: per adapter object
776 * @handle: expander handle (assigned by firmware)
777 * Context: Calling function should acquire ioc->sas_device_lock
778 *
779 * This searches for expander device based on handle, then returns the
780 * sas_node object.
781 */
782struct _sas_node *
783mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
784{
785 struct _sas_node *sas_expander, *r;
786
787 r = NULL;
788 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
789 if (sas_expander->handle != handle)
790 continue;
791 r = sas_expander;
792 goto out;
793 }
794 out:
795 return r;
796}
797
798/**
Eric Moore635374e2009-03-09 01:21:12 -0600799 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
800 * @ioc: per adapter object
801 * @sas_address: sas address
802 * Context: Calling function should acquire ioc->sas_node_lock.
803 *
804 * This searches for expander device based on sas_address, then returns the
805 * sas_node object.
806 */
807struct _sas_node *
808mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
809 u64 sas_address)
810{
811 struct _sas_node *sas_expander, *r;
812
813 r = NULL;
814 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
815 if (sas_expander->sas_address != sas_address)
816 continue;
817 r = sas_expander;
818 goto out;
819 }
820 out:
821 return r;
822}
823
824/**
825 * _scsih_expander_node_add - insert expander device to the list.
826 * @ioc: per adapter object
827 * @sas_expander: the sas_device object
828 * Context: This function will acquire ioc->sas_node_lock.
829 *
830 * Adding new object to the ioc->sas_expander_list.
831 *
832 * Return nothing.
833 */
834static void
835_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
836 struct _sas_node *sas_expander)
837{
838 unsigned long flags;
839
840 spin_lock_irqsave(&ioc->sas_node_lock, flags);
841 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
842 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
843}
844
845/**
846 * _scsih_is_end_device - determines if device is an end device
847 * @device_info: bitfield providing information about the device.
848 * Context: none
849 *
850 * Returns 1 if end device.
851 */
852static int
853_scsih_is_end_device(u32 device_info)
854{
855 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
856 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
857 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
858 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
859 return 1;
860 else
861 return 0;
862}
863
864/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530865 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600866 * @ioc: per adapter object
867 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600868 *
869 * Returns the smid stored scmd pointer.
870 */
871static struct scsi_cmnd *
872_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
873{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530874 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600875}
876
877/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530878 * _scsih_scsi_lookup_get_clear - returns scmd entry
879 * @ioc: per adapter object
880 * @smid: system request message index
881 *
882 * Returns the smid stored scmd pointer.
883 * Then will derefrence the stored scmd pointer.
884 */
885static inline struct scsi_cmnd *
886_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
887{
888 unsigned long flags;
889 struct scsi_cmnd *scmd;
890
891 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
892 scmd = ioc->scsi_lookup[smid - 1].scmd;
893 ioc->scsi_lookup[smid - 1].scmd = NULL;
894 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
895
896 return scmd;
897}
898
899/**
Eric Moore635374e2009-03-09 01:21:12 -0600900 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
901 * @ioc: per adapter object
902 * @smid: system request message index
903 * @scmd: pointer to scsi command object
904 * Context: This function will acquire ioc->scsi_lookup_lock.
905 *
906 * This will search for a scmd pointer in the scsi_lookup array,
907 * returning the revelent smid. A returned value of zero means invalid.
908 */
909static u16
910_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
911 *scmd)
912{
913 u16 smid;
914 unsigned long flags;
915 int i;
916
917 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
918 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530919 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600920 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530921 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600922 goto out;
923 }
924 }
925 out:
926 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
927 return smid;
928}
929
930/**
931 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
932 * @ioc: per adapter object
933 * @id: target id
934 * @channel: channel
935 * Context: This function will acquire ioc->scsi_lookup_lock.
936 *
937 * This will search for a matching channel:id in the scsi_lookup array,
938 * returning 1 if found.
939 */
940static u8
941_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
942 int channel)
943{
944 u8 found;
945 unsigned long flags;
946 int i;
947
948 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
949 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530950 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600951 if (ioc->scsi_lookup[i].scmd &&
952 (ioc->scsi_lookup[i].scmd->device->id == id &&
953 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
954 found = 1;
955 goto out;
956 }
957 }
958 out:
959 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
960 return found;
961}
962
963/**
Eric Moore993e0da2009-05-18 13:00:45 -0600964 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
965 * @ioc: per adapter object
966 * @id: target id
967 * @lun: lun number
968 * @channel: channel
969 * Context: This function will acquire ioc->scsi_lookup_lock.
970 *
971 * This will search for a matching channel:id:lun in the scsi_lookup array,
972 * returning 1 if found.
973 */
974static u8
975_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
976 unsigned int lun, int channel)
977{
978 u8 found;
979 unsigned long flags;
980 int i;
981
982 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
983 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530984 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600985 if (ioc->scsi_lookup[i].scmd &&
986 (ioc->scsi_lookup[i].scmd->device->id == id &&
987 ioc->scsi_lookup[i].scmd->device->channel == channel &&
988 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
989 found = 1;
990 goto out;
991 }
992 }
993 out:
994 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
995 return found;
996}
997
998/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530999 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -06001000 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301001 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -06001002 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301003 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001004 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301005static struct chain_tracker *
1006_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001007{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301008 struct chain_tracker *chain_req;
1009 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001010
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301011 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1012 if (list_empty(&ioc->free_chain_list)) {
1013 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
nagalakshmi.nandigama@lsi.comaff132d2011-12-01 07:53:08 +05301014 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT "chain buffers not "
1015 "available\n", ioc->name));
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301016 return NULL;
1017 }
1018 chain_req = list_entry(ioc->free_chain_list.next,
1019 struct chain_tracker, tracker_list);
1020 list_del_init(&chain_req->tracker_list);
1021 list_add_tail(&chain_req->tracker_list,
1022 &ioc->scsi_lookup[smid - 1].chain_list);
1023 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1024 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001025}
1026
1027/**
1028 * _scsih_build_scatter_gather - main sg creation routine
1029 * @ioc: per adapter object
1030 * @scmd: scsi command
1031 * @smid: system request message index
1032 * Context: none.
1033 *
1034 * The main routine that builds scatter gather table from a given
1035 * scsi request sent via the .queuecommand main handler.
1036 *
1037 * Returns 0 success, anything else error
1038 */
1039static int
1040_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1041 struct scsi_cmnd *scmd, u16 smid)
1042{
1043 Mpi2SCSIIORequest_t *mpi_request;
1044 dma_addr_t chain_dma;
1045 struct scatterlist *sg_scmd;
1046 void *sg_local, *chain;
1047 u32 chain_offset;
1048 u32 chain_length;
1049 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001050 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001051 u32 sges_in_segment;
1052 u32 sgl_flags;
1053 u32 sgl_flags_last_element;
1054 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301055 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001056
1057 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1058
1059 /* init scatter gather flags */
1060 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1061 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1062 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1063 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1064 << MPI2_SGE_FLAGS_SHIFT;
1065 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1066 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1067 << MPI2_SGE_FLAGS_SHIFT;
1068 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1069
1070 sg_scmd = scsi_sglist(scmd);
1071 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001072 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001073 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1074 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1075 return -ENOMEM;
1076 }
1077
1078 sg_local = &mpi_request->SGL;
1079 sges_in_segment = ioc->max_sges_in_main_message;
1080 if (sges_left <= sges_in_segment)
1081 goto fill_in_last_segment;
1082
1083 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1084 (sges_in_segment * ioc->sge_size))/4;
1085
1086 /* fill in main message segment when there is a chain following */
1087 while (sges_in_segment) {
1088 if (sges_in_segment == 1)
1089 ioc->base_add_sg_single(sg_local,
1090 sgl_flags_last_element | sg_dma_len(sg_scmd),
1091 sg_dma_address(sg_scmd));
1092 else
1093 ioc->base_add_sg_single(sg_local, sgl_flags |
1094 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1095 sg_scmd = sg_next(sg_scmd);
1096 sg_local += ioc->sge_size;
1097 sges_left--;
1098 sges_in_segment--;
1099 }
1100
1101 /* initializing the chain flags and pointers */
1102 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301103 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1104 if (!chain_req)
1105 return -1;
1106 chain = chain_req->chain_buffer;
1107 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001108 do {
1109 sges_in_segment = (sges_left <=
1110 ioc->max_sges_in_chain_message) ? sges_left :
1111 ioc->max_sges_in_chain_message;
1112 chain_offset = (sges_left == sges_in_segment) ?
1113 0 : (sges_in_segment * ioc->sge_size)/4;
1114 chain_length = sges_in_segment * ioc->sge_size;
1115 if (chain_offset) {
1116 chain_offset = chain_offset <<
1117 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1118 chain_length += ioc->sge_size;
1119 }
1120 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1121 chain_length, chain_dma);
1122 sg_local = chain;
1123 if (!chain_offset)
1124 goto fill_in_last_segment;
1125
1126 /* fill in chain segments */
1127 while (sges_in_segment) {
1128 if (sges_in_segment == 1)
1129 ioc->base_add_sg_single(sg_local,
1130 sgl_flags_last_element |
1131 sg_dma_len(sg_scmd),
1132 sg_dma_address(sg_scmd));
1133 else
1134 ioc->base_add_sg_single(sg_local, sgl_flags |
1135 sg_dma_len(sg_scmd),
1136 sg_dma_address(sg_scmd));
1137 sg_scmd = sg_next(sg_scmd);
1138 sg_local += ioc->sge_size;
1139 sges_left--;
1140 sges_in_segment--;
1141 }
1142
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301143 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1144 if (!chain_req)
1145 return -1;
1146 chain = chain_req->chain_buffer;
1147 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001148 } while (1);
1149
1150
1151 fill_in_last_segment:
1152
1153 /* fill the last segment */
1154 while (sges_left) {
1155 if (sges_left == 1)
1156 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1157 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1158 else
1159 ioc->base_add_sg_single(sg_local, sgl_flags |
1160 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1161 sg_scmd = sg_next(sg_scmd);
1162 sg_local += ioc->sge_size;
1163 sges_left--;
1164 }
1165
1166 return 0;
1167}
1168
1169/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301170 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001171 * @sdev: scsi device struct
1172 * @qdepth: requested queue depth
1173 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301174 *
1175 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001176 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301177static void
1178_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001179{
1180 struct Scsi_Host *shost = sdev->host;
1181 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301182 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1183 struct MPT2SAS_DEVICE *sas_device_priv_data;
1184 struct MPT2SAS_TARGET *sas_target_priv_data;
1185 struct _sas_device *sas_device;
1186 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001187
1188 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301189
1190 /* limit max device queue for SATA to 32 */
1191 sas_device_priv_data = sdev->hostdata;
1192 if (!sas_device_priv_data)
1193 goto not_sata;
1194 sas_target_priv_data = sas_device_priv_data->sas_target;
1195 if (!sas_target_priv_data)
1196 goto not_sata;
1197 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1198 goto not_sata;
1199 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1200 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1201 sas_device_priv_data->sas_target->sas_address);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301202 if (sas_device && sas_device->device_info &
1203 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1204 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301205 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301206
1207 not_sata:
1208
Eric Moore635374e2009-03-09 01:21:12 -06001209 if (!sdev->tagged_supported)
1210 max_depth = 1;
1211 if (qdepth > max_depth)
1212 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301213 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1214}
1215
1216/**
1217 * _scsih_change_queue_depth - setting device queue depth
1218 * @sdev: scsi device struct
1219 * @qdepth: requested queue depth
1220 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1221 * (see include/scsi/scsi_host.h for definition)
1222 *
1223 * Returns queue depth.
1224 */
1225static int
1226_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1227{
1228 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1229 _scsih_adjust_queue_depth(sdev, qdepth);
1230 else if (reason == SCSI_QDEPTH_QFULL)
1231 scsi_track_queue_full(sdev, qdepth);
1232 else
1233 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001234
1235 if (sdev->inquiry_len > 7)
1236 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1237 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1238 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1239 sdev->ordered_tags, sdev->scsi_level,
1240 (sdev->inquiry[7] & 2) >> 1);
1241
1242 return sdev->queue_depth;
1243}
1244
1245/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301246 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001247 * @sdev: scsi device struct
1248 * @tag_type: requested tag type
1249 *
1250 * Returns queue tag type.
1251 */
1252static int
Eric Moored5d135b2009-05-18 13:02:08 -06001253_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001254{
1255 if (sdev->tagged_supported) {
1256 scsi_set_tag_type(sdev, tag_type);
1257 if (tag_type)
1258 scsi_activate_tcq(sdev, sdev->queue_depth);
1259 else
1260 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1261 } else
1262 tag_type = 0;
1263
1264 return tag_type;
1265}
1266
1267/**
Eric Moored5d135b2009-05-18 13:02:08 -06001268 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001269 * @starget: scsi target struct
1270 *
1271 * Returns 0 if ok. Any other return is assumed to be an error and
1272 * the device is ignored.
1273 */
1274static int
Eric Moored5d135b2009-05-18 13:02:08 -06001275_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001276{
1277 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1278 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1279 struct MPT2SAS_TARGET *sas_target_priv_data;
1280 struct _sas_device *sas_device;
1281 struct _raid_device *raid_device;
1282 unsigned long flags;
1283 struct sas_rphy *rphy;
1284
1285 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1286 if (!sas_target_priv_data)
1287 return -ENOMEM;
1288
1289 starget->hostdata = sas_target_priv_data;
1290 sas_target_priv_data->starget = starget;
1291 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1292
1293 /* RAID volumes */
1294 if (starget->channel == RAID_CHANNEL) {
1295 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1296 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1297 starget->channel);
1298 if (raid_device) {
1299 sas_target_priv_data->handle = raid_device->handle;
1300 sas_target_priv_data->sas_address = raid_device->wwid;
1301 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301302 if (ioc->is_warpdrive)
1303 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001304 raid_device->starget = starget;
1305 }
1306 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1307 return 0;
1308 }
1309
1310 /* sas/sata devices */
1311 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1312 rphy = dev_to_rphy(starget->dev.parent);
1313 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1314 rphy->identify.sas_address);
1315
1316 if (sas_device) {
1317 sas_target_priv_data->handle = sas_device->handle;
1318 sas_target_priv_data->sas_address = sas_device->sas_address;
1319 sas_device->starget = starget;
1320 sas_device->id = starget->id;
1321 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301322 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001323 sas_target_priv_data->flags |=
1324 MPT_TARGET_FLAGS_RAID_COMPONENT;
1325 }
1326 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1327
1328 return 0;
1329}
1330
1331/**
Eric Moored5d135b2009-05-18 13:02:08 -06001332 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001333 * @starget: scsi target struct
1334 *
1335 * Returns nothing.
1336 */
1337static void
Eric Moored5d135b2009-05-18 13:02:08 -06001338_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001339{
1340 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1341 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1342 struct MPT2SAS_TARGET *sas_target_priv_data;
1343 struct _sas_device *sas_device;
1344 struct _raid_device *raid_device;
1345 unsigned long flags;
1346 struct sas_rphy *rphy;
1347
1348 sas_target_priv_data = starget->hostdata;
1349 if (!sas_target_priv_data)
1350 return;
1351
1352 if (starget->channel == RAID_CHANNEL) {
1353 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1354 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1355 starget->channel);
1356 if (raid_device) {
1357 raid_device->starget = NULL;
1358 raid_device->sdev = NULL;
1359 }
1360 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1361 goto out;
1362 }
1363
1364 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1365 rphy = dev_to_rphy(starget->dev.parent);
1366 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1367 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001368 if (sas_device && (sas_device->starget == starget) &&
1369 (sas_device->id == starget->id) &&
1370 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001371 sas_device->starget = NULL;
1372
1373 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1374
1375 out:
1376 kfree(sas_target_priv_data);
1377 starget->hostdata = NULL;
1378}
1379
1380/**
Eric Moored5d135b2009-05-18 13:02:08 -06001381 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001382 * @sdev: scsi device struct
1383 *
1384 * Returns 0 if ok. Any other return is assumed to be an error and
1385 * the device is ignored.
1386 */
1387static int
Eric Moored5d135b2009-05-18 13:02:08 -06001388_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001389{
1390 struct Scsi_Host *shost;
1391 struct MPT2SAS_ADAPTER *ioc;
1392 struct MPT2SAS_TARGET *sas_target_priv_data;
1393 struct MPT2SAS_DEVICE *sas_device_priv_data;
1394 struct scsi_target *starget;
1395 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001396 unsigned long flags;
1397
1398 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1399 if (!sas_device_priv_data)
1400 return -ENOMEM;
1401
1402 sas_device_priv_data->lun = sdev->lun;
1403 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1404
1405 starget = scsi_target(sdev);
1406 sas_target_priv_data = starget->hostdata;
1407 sas_target_priv_data->num_luns++;
1408 sas_device_priv_data->sas_target = sas_target_priv_data;
1409 sdev->hostdata = sas_device_priv_data;
1410 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1411 sdev->no_uld_attach = 1;
1412
1413 shost = dev_to_shost(&starget->dev);
1414 ioc = shost_priv(shost);
1415 if (starget->channel == RAID_CHANNEL) {
1416 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1417 raid_device = _scsih_raid_device_find_by_id(ioc,
1418 starget->id, starget->channel);
1419 if (raid_device)
1420 raid_device->sdev = sdev; /* raid is single lun */
1421 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001422 }
1423
Eric Moore635374e2009-03-09 01:21:12 -06001424 return 0;
1425}
1426
1427/**
Eric Moored5d135b2009-05-18 13:02:08 -06001428 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001429 * @sdev: scsi device struct
1430 *
1431 * Returns nothing.
1432 */
1433static void
Eric Moored5d135b2009-05-18 13:02:08 -06001434_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001435{
1436 struct MPT2SAS_TARGET *sas_target_priv_data;
1437 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301438 struct Scsi_Host *shost;
1439 struct MPT2SAS_ADAPTER *ioc;
1440 struct _sas_device *sas_device;
1441 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001442
1443 if (!sdev->hostdata)
1444 return;
1445
1446 starget = scsi_target(sdev);
1447 sas_target_priv_data = starget->hostdata;
1448 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301449
1450 shost = dev_to_shost(&starget->dev);
1451 ioc = shost_priv(shost);
1452
1453 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1454 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1455 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1456 sas_target_priv_data->sas_address);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +05301457 if (sas_device && !sas_target_priv_data->num_luns)
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301458 sas_device->starget = NULL;
1459 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1460 }
1461
Eric Moore635374e2009-03-09 01:21:12 -06001462 kfree(sdev->hostdata);
1463 sdev->hostdata = NULL;
1464}
1465
1466/**
Eric Moored5d135b2009-05-18 13:02:08 -06001467 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001468 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301469 * @handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -06001470 * @sdev: scsi device struct
1471 */
1472static void
Eric Moored5d135b2009-05-18 13:02:08 -06001473_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301474 u16 handle, struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001475{
1476 Mpi2ConfigReply_t mpi_reply;
1477 Mpi2SasDevicePage0_t sas_device_pg0;
1478 u32 ioc_status;
1479 u16 flags;
1480 u32 device_info;
1481
1482 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301483 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06001484 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1485 ioc->name, __FILE__, __LINE__, __func__);
1486 return;
1487 }
1488
1489 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1490 MPI2_IOCSTATUS_MASK;
1491 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1492 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1493 ioc->name, __FILE__, __LINE__, __func__);
1494 return;
1495 }
1496
1497 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301498 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001499
1500 sdev_printk(KERN_INFO, sdev,
1501 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1502 "sw_preserve(%s)\n",
1503 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1504 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1505 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1506 "n",
1507 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1508 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1509 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1510}
1511
1512/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301513 * _scsih_is_raid - return boolean indicating device is raid volume
1514 * @dev the device struct object
1515 */
1516static int
1517_scsih_is_raid(struct device *dev)
1518{
1519 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301520 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301521
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301522 if (ioc->is_warpdrive)
1523 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301524 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1525}
1526
1527/**
1528 * _scsih_get_resync - get raid volume resync percent complete
1529 * @dev the device struct object
1530 */
1531static void
1532_scsih_get_resync(struct device *dev)
1533{
1534 struct scsi_device *sdev = to_scsi_device(dev);
1535 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1536 static struct _raid_device *raid_device;
1537 unsigned long flags;
1538 Mpi2RaidVolPage0_t vol_pg0;
1539 Mpi2ConfigReply_t mpi_reply;
1540 u32 volume_status_flags;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301541 u8 percent_complete;
1542 u16 handle;
1543
1544 percent_complete = 0;
1545 handle = 0;
1546 if (ioc->is_warpdrive)
1547 goto out;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301548
1549 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1550 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1551 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301552 if (raid_device) {
1553 handle = raid_device->handle;
1554 percent_complete = raid_device->percent_complete;
1555 }
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301556 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1557
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301558 if (!handle)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301559 goto out;
1560
1561 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301562 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301563 sizeof(Mpi2RaidVolPage0_t))) {
1564 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1565 ioc->name, __FILE__, __LINE__, __func__);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301566 percent_complete = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301567 goto out;
1568 }
1569
1570 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301571 if (!(volume_status_flags &
1572 MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS))
1573 percent_complete = 0;
1574
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301575 out:
1576 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1577}
1578
1579/**
1580 * _scsih_get_state - get raid volume level
1581 * @dev the device struct object
1582 */
1583static void
1584_scsih_get_state(struct device *dev)
1585{
1586 struct scsi_device *sdev = to_scsi_device(dev);
1587 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1588 static struct _raid_device *raid_device;
1589 unsigned long flags;
1590 Mpi2RaidVolPage0_t vol_pg0;
1591 Mpi2ConfigReply_t mpi_reply;
1592 u32 volstate;
1593 enum raid_state state = RAID_STATE_UNKNOWN;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301594 u16 handle = 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301595
1596 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1597 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1598 sdev->channel);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301599 if (raid_device)
1600 handle = raid_device->handle;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301601 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1602
1603 if (!raid_device)
1604 goto out;
1605
1606 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301607 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301608 sizeof(Mpi2RaidVolPage0_t))) {
1609 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1610 ioc->name, __FILE__, __LINE__, __func__);
1611 goto out;
1612 }
1613
1614 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1615 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1616 state = RAID_STATE_RESYNCING;
1617 goto out;
1618 }
1619
1620 switch (vol_pg0.VolumeState) {
1621 case MPI2_RAID_VOL_STATE_OPTIMAL:
1622 case MPI2_RAID_VOL_STATE_ONLINE:
1623 state = RAID_STATE_ACTIVE;
1624 break;
1625 case MPI2_RAID_VOL_STATE_DEGRADED:
1626 state = RAID_STATE_DEGRADED;
1627 break;
1628 case MPI2_RAID_VOL_STATE_FAILED:
1629 case MPI2_RAID_VOL_STATE_MISSING:
1630 state = RAID_STATE_OFFLINE;
1631 break;
1632 }
1633 out:
1634 raid_set_state(mpt2sas_raid_template, dev, state);
1635}
1636
1637/**
1638 * _scsih_set_level - set raid level
1639 * @sdev: scsi device struct
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301640 * @volume_type: volume type
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301641 */
1642static void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301643_scsih_set_level(struct scsi_device *sdev, u8 volume_type)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301644{
1645 enum raid_level level = RAID_LEVEL_UNKNOWN;
1646
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301647 switch (volume_type) {
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301648 case MPI2_RAID_VOL_TYPE_RAID0:
1649 level = RAID_LEVEL_0;
1650 break;
1651 case MPI2_RAID_VOL_TYPE_RAID10:
1652 level = RAID_LEVEL_10;
1653 break;
1654 case MPI2_RAID_VOL_TYPE_RAID1E:
1655 level = RAID_LEVEL_1E;
1656 break;
1657 case MPI2_RAID_VOL_TYPE_RAID1:
1658 level = RAID_LEVEL_1;
1659 break;
1660 }
1661
1662 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1663}
1664
1665/**
Eric Moore635374e2009-03-09 01:21:12 -06001666 * _scsih_get_volume_capabilities - volume capabilities
1667 * @ioc: per adapter object
1668 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301669 *
1670 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001671 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301672static int
Eric Moore635374e2009-03-09 01:21:12 -06001673_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1674 struct _raid_device *raid_device)
1675{
1676 Mpi2RaidVolPage0_t *vol_pg0;
1677 Mpi2RaidPhysDiskPage0_t pd_pg0;
1678 Mpi2SasDevicePage0_t sas_device_pg0;
1679 Mpi2ConfigReply_t mpi_reply;
1680 u16 sz;
1681 u8 num_pds;
1682
1683 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1684 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301685 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1686 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1687 __func__));
1688 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001689 }
1690
1691 raid_device->num_pds = num_pds;
1692 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1693 sizeof(Mpi2RaidVol0PhysDisk_t));
1694 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1695 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301696 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1697 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1698 __func__));
1699 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001700 }
1701
1702 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1703 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301704 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1705 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1706 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001707 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301708 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001709 }
1710
1711 raid_device->volume_type = vol_pg0->VolumeType;
1712
1713 /* figure out what the underlying devices are by
1714 * obtaining the device_info bits for the 1st device
1715 */
1716 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1717 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1718 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1719 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1720 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1721 le16_to_cpu(pd_pg0.DevHandle)))) {
1722 raid_device->device_info =
1723 le32_to_cpu(sas_device_pg0.DeviceInfo);
1724 }
1725 }
1726
1727 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301728 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001729}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301730/**
1731 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1732 * @ioc: per adapter object
1733 */
1734static void
1735_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1736{
1737 Mpi2RaidVolPage1_t vol_pg1;
1738 Mpi2ConfigReply_t mpi_reply;
1739 struct _raid_device *raid_device;
1740 u16 handle;
1741 u16 ioc_status;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301742 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301743
1744 handle = 0xFFFF;
1745 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1746 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1747 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1748 MPI2_IOCSTATUS_MASK;
1749 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1750 break;
1751 handle = le16_to_cpu(vol_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301752 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301753 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1754 if (raid_device)
1755 raid_device->direct_io_enabled = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301756 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301757 }
1758 return;
1759}
1760
1761
1762/**
1763 * _scsih_get_num_volumes - Get number of volumes in the ioc
1764 * @ioc: per adapter object
1765 */
1766static u8
1767_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1768{
1769 Mpi2RaidVolPage1_t vol_pg1;
1770 Mpi2ConfigReply_t mpi_reply;
1771 u16 handle;
1772 u8 vol_cnt = 0;
1773 u16 ioc_status;
1774
1775 handle = 0xFFFF;
1776 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1777 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1778 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1779 MPI2_IOCSTATUS_MASK;
1780 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1781 break;
1782 vol_cnt++;
1783 handle = le16_to_cpu(vol_pg1.DevHandle);
1784 }
1785 return vol_cnt;
1786}
1787
1788
1789/**
1790 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1791 * @ioc: per adapter object
1792 * @raid_device: the raid_device object
1793 */
1794static void
1795_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1796 struct _raid_device *raid_device)
1797{
1798 Mpi2RaidVolPage0_t *vol_pg0;
1799 Mpi2RaidPhysDiskPage0_t pd_pg0;
1800 Mpi2ConfigReply_t mpi_reply;
1801 u16 sz;
1802 u8 num_pds, count;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301803 unsigned long stripe_sz, block_sz;
1804 u8 stripe_exp, block_exp;
1805 u64 dev_max_lba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301806
1807 if (!ioc->is_warpdrive)
1808 return;
1809
1810 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1811 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1812 "globally as drives are exposed\n", ioc->name);
1813 return;
1814 }
1815 if (_scsih_get_num_volumes(ioc) > 1) {
1816 _scsih_disable_ddio(ioc);
1817 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1818 "globally as number of drives > 1\n", ioc->name);
1819 return;
1820 }
1821 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1822 &num_pds)) || !num_pds) {
1823 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1824 "Failure in computing number of drives\n", ioc->name);
1825 return;
1826 }
1827
1828 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1829 sizeof(Mpi2RaidVol0PhysDisk_t));
1830 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1831 if (!vol_pg0) {
1832 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1833 "Memory allocation failure for RVPG0\n", ioc->name);
1834 return;
1835 }
1836
1837 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1838 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1839 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1840 "Failure in retrieving RVPG0\n", ioc->name);
1841 kfree(vol_pg0);
1842 return;
1843 }
1844
1845 /*
1846 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1847 * assumed for WARPDRIVE, disable direct I/O
1848 */
1849 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1850 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1851 "for the drive with handle(0x%04x): num_mem=%d, "
1852 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1853 num_pds, MPT_MAX_WARPDRIVE_PDS);
1854 kfree(vol_pg0);
1855 return;
1856 }
1857 for (count = 0; count < num_pds; count++) {
1858 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1859 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1860 vol_pg0->PhysDisk[count].PhysDiskNum) ||
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05301861 le16_to_cpu(pd_pg0.DevHandle) ==
1862 MPT2SAS_INVALID_DEVICE_HANDLE) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301863 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1864 "disabled for the drive with handle(0x%04x) member"
1865 "handle retrieval failed for member number=%d\n",
1866 ioc->name, raid_device->handle,
1867 vol_pg0->PhysDisk[count].PhysDiskNum);
1868 goto out_error;
1869 }
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301870 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1871 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1872 if (dev_max_lba >> 32) {
1873 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1874 "disabled for the drive with handle(0x%04x) member"
1875 "handle (0x%04x) unsupported max lba 0x%016llx\n",
1876 ioc->name, raid_device->handle,
1877 le16_to_cpu(pd_pg0.DevHandle),
1878 (unsigned long long)dev_max_lba);
1879 goto out_error;
1880 }
1881
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301882 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1883 }
1884
1885 /*
1886 * Assumption for WD: Direct I/O is not supported if the volume is
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301887 * not RAID0
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301888 */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301889 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301890 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1891 "for the drive with handle(0x%04x): type=%d, "
1892 "s_sz=%uK, blk_size=%u\n", ioc->name,
1893 raid_device->handle, raid_device->volume_type,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301894 (le32_to_cpu(vol_pg0->StripeSize) *
1895 le16_to_cpu(vol_pg0->BlockSize)) / 1024,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301896 le16_to_cpu(vol_pg0->BlockSize));
1897 goto out_error;
1898 }
1899
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301900 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301901 stripe_exp = find_first_bit(&stripe_sz, 32);
1902 if (stripe_exp == 32) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301903 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301904 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301905 ioc->name, raid_device->handle,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301906 (le32_to_cpu(vol_pg0->StripeSize) *
1907 le16_to_cpu(vol_pg0->BlockSize)) / 1024);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301908 goto out_error;
1909 }
1910 raid_device->stripe_exponent = stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301911 block_sz = le16_to_cpu(vol_pg0->BlockSize);
1912 block_exp = find_first_bit(&block_sz, 16);
1913 if (block_exp == 16) {
1914 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1915 "for the drive with handle(0x%04x) invalid block sz %u\n",
1916 ioc->name, raid_device->handle,
1917 le16_to_cpu(vol_pg0->BlockSize));
1918 goto out_error;
1919 }
1920 raid_device->block_exponent = block_exp;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301921 raid_device->direct_io_enabled = 1;
1922
1923 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1924 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1925 /*
1926 * WARPDRIVE: Though the following fields are not used for direct IO,
1927 * stored for future purpose:
1928 */
1929 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1930 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1931 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1932
1933
1934 kfree(vol_pg0);
1935 return;
1936
1937out_error:
1938 raid_device->direct_io_enabled = 0;
1939 for (count = 0; count < num_pds; count++)
1940 raid_device->pd_handle[count] = 0;
1941 kfree(vol_pg0);
1942 return;
1943}
Eric Moore635374e2009-03-09 01:21:12 -06001944
1945/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301946 * _scsih_enable_tlr - setting TLR flags
1947 * @ioc: per adapter object
1948 * @sdev: scsi device struct
1949 *
1950 * Enabling Transaction Layer Retries for tape devices when
1951 * vpd page 0x90 is present
1952 *
1953 */
1954static void
1955_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1956{
1957 /* only for TAPE */
1958 if (sdev->type != TYPE_TAPE)
1959 return;
1960
1961 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1962 return;
1963
1964 sas_enable_tlr(sdev);
1965 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1966 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1967 return;
1968
1969}
1970
1971/**
Eric Moored5d135b2009-05-18 13:02:08 -06001972 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001973 * @sdev: scsi device struct
1974 *
1975 * Returns 0 if ok. Any other return is assumed to be an error and
1976 * the device is ignored.
1977 */
1978static int
Eric Moored5d135b2009-05-18 13:02:08 -06001979_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001980{
1981 struct Scsi_Host *shost = sdev->host;
1982 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1983 struct MPT2SAS_DEVICE *sas_device_priv_data;
1984 struct MPT2SAS_TARGET *sas_target_priv_data;
1985 struct _sas_device *sas_device;
1986 struct _raid_device *raid_device;
1987 unsigned long flags;
1988 int qdepth;
1989 u8 ssp_target = 0;
1990 char *ds = "";
1991 char *r_level = "";
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05301992 u16 handle, volume_handle = 0;
1993 u64 volume_wwid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06001994
1995 qdepth = 1;
1996 sas_device_priv_data = sdev->hostdata;
1997 sas_device_priv_data->configured_lun = 1;
1998 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1999 sas_target_priv_data = sas_device_priv_data->sas_target;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302000 handle = sas_target_priv_data->handle;
Eric Moore635374e2009-03-09 01:21:12 -06002001
2002 /* raid volume handling */
2003 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
2004
2005 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302006 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06002007 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
2008 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302009 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2010 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2011 __LINE__, __func__));
2012 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002013 }
2014
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302015 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
2016 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2017 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2018 __LINE__, __func__));
2019 return 1;
2020 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302021 /*
2022 * WARPDRIVE: Initialize the required data for Direct IO
2023 */
2024 _scsih_init_warpdrive_properties(ioc, raid_device);
2025
Eric Moore635374e2009-03-09 01:21:12 -06002026 /* RAID Queue Depth Support
2027 * IS volume = underlying qdepth of drive type, either
2028 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2029 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2030 */
2031 if (raid_device->device_info &
2032 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2033 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2034 ds = "SSP";
2035 } else {
2036 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2037 if (raid_device->device_info &
2038 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2039 ds = "SATA";
2040 else
2041 ds = "STP";
2042 }
2043
2044 switch (raid_device->volume_type) {
2045 case MPI2_RAID_VOL_TYPE_RAID0:
2046 r_level = "RAID0";
2047 break;
2048 case MPI2_RAID_VOL_TYPE_RAID1E:
2049 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302050 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302051 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302052 MFG10_GF0_R10_DISPLAY) &&
2053 !(raid_device->num_pds % 2))
2054 r_level = "RAID10";
2055 else
2056 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002057 break;
2058 case MPI2_RAID_VOL_TYPE_RAID1:
2059 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2060 r_level = "RAID1";
2061 break;
2062 case MPI2_RAID_VOL_TYPE_RAID10:
2063 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2064 r_level = "RAID10";
2065 break;
2066 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2067 default:
2068 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2069 r_level = "RAIDX";
2070 break;
2071 }
2072
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302073 if (!ioc->hide_ir_msg)
2074 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2075 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2076 r_level, raid_device->handle,
2077 (unsigned long long)raid_device->wwid,
2078 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002079 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302080 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302081 if (!ioc->is_warpdrive)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302082 _scsih_set_level(sdev, raid_device->volume_type);
Eric Moore635374e2009-03-09 01:21:12 -06002083 return 0;
2084 }
2085
2086 /* non-raid handling */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302087 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) {
2088 if (mpt2sas_config_get_volume_handle(ioc, handle,
2089 &volume_handle)) {
2090 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2091 "failure at %s:%d/%s()!\n", ioc->name,
2092 __FILE__, __LINE__, __func__));
2093 return 1;
2094 }
2095 if (volume_handle && mpt2sas_config_get_volume_wwid(ioc,
2096 volume_handle, &volume_wwid)) {
2097 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2098 "failure at %s:%d/%s()!\n", ioc->name,
2099 __FILE__, __LINE__, __func__));
2100 return 1;
2101 }
2102 }
2103
Eric Moore635374e2009-03-09 01:21:12 -06002104 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2105 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2106 sas_device_priv_data->sas_target->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302107 if (!sas_device) {
2108 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302109 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302110 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
2111 __LINE__, __func__));
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302112 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002113 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05302114 sas_device->volume_handle = volume_handle;
2115 sas_device->volume_wwid = volume_wwid;
2116 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2117 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2118 ssp_target = 1;
2119 ds = "SSP";
2120 } else {
2121 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2122 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET)
2123 ds = "STP";
2124 else if (sas_device->device_info &
2125 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2126 ds = "SATA";
2127 }
2128 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2129 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
2130 ds, sas_device->handle,
2131 (unsigned long long)sas_device->sas_address,
2132 sas_device->phy,
2133 (unsigned long long)sas_device->device_name);
2134 sdev_printk(KERN_INFO, sdev, "%s: "
2135 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2136 (unsigned long long) sas_device->enclosure_logical_id,
2137 sas_device->slot);
2138
2139 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2140 if (!ssp_target)
2141 _scsih_display_sata_capabilities(ioc, handle, sdev);
2142
Eric Moore635374e2009-03-09 01:21:12 -06002143
Mike Christiee881a172009-10-15 17:46:39 -07002144 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002145
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302146 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002147 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302148 _scsih_enable_tlr(ioc, sdev);
2149 }
Eric Moore635374e2009-03-09 01:21:12 -06002150 return 0;
2151}
2152
2153/**
Eric Moored5d135b2009-05-18 13:02:08 -06002154 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002155 * @sdev: scsi device struct
2156 * @bdev: pointer to block device context
2157 * @capacity: device size (in 512 byte sectors)
2158 * @params: three element array to place output:
2159 * params[0] number of heads (max 255)
2160 * params[1] number of sectors (max 63)
2161 * params[2] number of cylinders
2162 *
2163 * Return nothing.
2164 */
2165static int
Eric Moored5d135b2009-05-18 13:02:08 -06002166_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002167 sector_t capacity, int params[])
2168{
2169 int heads;
2170 int sectors;
2171 sector_t cylinders;
2172 ulong dummy;
2173
2174 heads = 64;
2175 sectors = 32;
2176
2177 dummy = heads * sectors;
2178 cylinders = capacity;
2179 sector_div(cylinders, dummy);
2180
2181 /*
2182 * Handle extended translation size for logical drives
2183 * > 1Gb
2184 */
2185 if ((ulong)capacity >= 0x200000) {
2186 heads = 255;
2187 sectors = 63;
2188 dummy = heads * sectors;
2189 cylinders = capacity;
2190 sector_div(cylinders, dummy);
2191 }
2192
2193 /* return result */
2194 params[0] = heads;
2195 params[1] = sectors;
2196 params[2] = cylinders;
2197
2198 return 0;
2199}
2200
2201/**
2202 * _scsih_response_code - translation of device response code
2203 * @ioc: per adapter object
2204 * @response_code: response code returned by the device
2205 *
2206 * Return nothing.
2207 */
2208static void
2209_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2210{
2211 char *desc;
2212
2213 switch (response_code) {
2214 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2215 desc = "task management request completed";
2216 break;
2217 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2218 desc = "invalid frame";
2219 break;
2220 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2221 desc = "task management request not supported";
2222 break;
2223 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2224 desc = "task management request failed";
2225 break;
2226 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2227 desc = "task management request succeeded";
2228 break;
2229 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2230 desc = "invalid lun";
2231 break;
2232 case 0xA:
2233 desc = "overlapped tag attempted";
2234 break;
2235 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2236 desc = "task queued, however not sent to target";
2237 break;
2238 default:
2239 desc = "unknown";
2240 break;
2241 }
2242 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2243 ioc->name, response_code, desc);
2244}
2245
2246/**
Eric Moored5d135b2009-05-18 13:02:08 -06002247 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002248 * @ioc: per adapter object
2249 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302250 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002251 * @reply: reply message frame(lower 32bit addr)
2252 * Context: none.
2253 *
2254 * The callback handler when using scsih_issue_tm.
2255 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302256 * Return 1 meaning mf should be freed from _base_interrupt
2257 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002258 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302259static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302260_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002261{
2262 MPI2DefaultReply_t *mpi_reply;
2263
2264 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302265 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002266 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302267 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302268 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002269 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2270 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2271 if (mpi_reply) {
2272 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2273 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2274 }
2275 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2276 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302277 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002278}
2279
2280/**
2281 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2282 * @ioc: per adapter object
2283 * @handle: device handle
2284 *
2285 * During taskmangement request, we need to freeze the device queue.
2286 */
2287void
2288mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2289{
2290 struct MPT2SAS_DEVICE *sas_device_priv_data;
2291 struct scsi_device *sdev;
2292 u8 skip = 0;
2293
2294 shost_for_each_device(sdev, ioc->shost) {
2295 if (skip)
2296 continue;
2297 sas_device_priv_data = sdev->hostdata;
2298 if (!sas_device_priv_data)
2299 continue;
2300 if (sas_device_priv_data->sas_target->handle == handle) {
2301 sas_device_priv_data->sas_target->tm_busy = 1;
2302 skip = 1;
2303 ioc->ignore_loginfos = 1;
2304 }
2305 }
2306}
2307
2308/**
2309 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2310 * @ioc: per adapter object
2311 * @handle: device handle
2312 *
2313 * During taskmangement request, we need to freeze the device queue.
2314 */
2315void
2316mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2317{
2318 struct MPT2SAS_DEVICE *sas_device_priv_data;
2319 struct scsi_device *sdev;
2320 u8 skip = 0;
2321
2322 shost_for_each_device(sdev, ioc->shost) {
2323 if (skip)
2324 continue;
2325 sas_device_priv_data = sdev->hostdata;
2326 if (!sas_device_priv_data)
2327 continue;
2328 if (sas_device_priv_data->sas_target->handle == handle) {
2329 sas_device_priv_data->sas_target->tm_busy = 0;
2330 skip = 1;
2331 ioc->ignore_loginfos = 0;
2332 }
2333 }
2334}
2335
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302336
Eric Moore635374e2009-03-09 01:21:12 -06002337/**
2338 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2339 * @ioc: per adapter struct
2340 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302341 * @channel: the channel assigned by the OS
2342 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002343 * @lun: lun number
2344 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2345 * @smid_task: smid assigned to the task
2346 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302347 * @serial_number: the serial_number from scmd
2348 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302349 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002350 *
2351 * A generic API for sending task management requests to firmware.
2352 *
Eric Moore635374e2009-03-09 01:21:12 -06002353 * The callback index is set inside `ioc->tm_cb_idx`.
2354 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302355 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002356 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302357int
2358mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2359 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302360 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002361{
2362 Mpi2SCSITaskManagementRequest_t *mpi_request;
2363 Mpi2SCSITaskManagementReply_t *mpi_reply;
2364 u16 smid = 0;
2365 u32 ioc_state;
2366 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302367 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302368 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002369
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302370 if (m_type == TM_MUTEX_ON)
2371 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302372 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2373 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2374 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302375 rc = FAILED;
2376 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302377 }
2378
Eric Moore3cb54692010-07-08 14:44:34 -06002379 if (ioc->shost_recovery || ioc->remove_host ||
2380 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002381 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2382 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302383 rc = FAILED;
2384 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002385 }
Eric Moore635374e2009-03-09 01:21:12 -06002386
2387 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2388 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302389 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002390 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302391 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302392 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302393 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302394 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002395 }
2396
2397 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2398 mpt2sas_base_fault_info(ioc, ioc_state &
2399 MPI2_DOORBELL_DATA_MASK);
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
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302406 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002407 if (!smid) {
2408 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2409 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302410 rc = FAILED;
2411 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002412 }
2413
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302414 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2415 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2416
Eric Moore635374e2009-03-09 01:21:12 -06002417 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302418 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2419 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002420 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2421 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2422 ioc->tm_cmds.smid = smid;
2423 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302424 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002425 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2426 mpi_request->DevHandle = cpu_to_le16(handle);
2427 mpi_request->TaskType = type;
2428 mpi_request->TaskMID = cpu_to_le16(smid_task);
2429 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2430 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302431 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302432 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002433 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002434 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2435 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2436 ioc->name, __func__);
2437 _debug_dump_mf(mpi_request,
2438 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302439 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302440 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302441 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302442 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302443 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2444 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2445 goto err_out;
2446 }
Eric Moore635374e2009-03-09 01:21:12 -06002447 }
2448
2449 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2450 mpi_reply = ioc->tm_cmds.reply;
2451 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2452 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2453 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2454 le32_to_cpu(mpi_reply->IOCLogInfo),
2455 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302456 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002457 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302458 if (mpi_reply->IOCStatus)
2459 _debug_dump_mf(mpi_request,
2460 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2461 }
Eric Moore635374e2009-03-09 01:21:12 -06002462 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302463
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302464 switch (type) {
2465 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302466 rc = SUCCESS;
2467 if (scsi_lookup->scmd == NULL)
2468 break;
2469 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302470 break;
2471
2472 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2473 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2474 rc = FAILED;
2475 else
2476 rc = SUCCESS;
2477 break;
2478
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302479 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302480 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2481 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2482 rc = FAILED;
2483 else
2484 rc = SUCCESS;
2485 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302486 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2487 rc = SUCCESS;
2488 break;
2489 default:
2490 rc = FAILED;
2491 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302492 }
2493
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302494 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2495 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302496 if (m_type == TM_MUTEX_ON)
2497 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302498
2499 return rc;
2500
2501 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302502 if (m_type == TM_MUTEX_ON)
2503 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302504 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002505}
2506
2507/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302508 * _scsih_tm_display_info - displays info about the device
2509 * @ioc: per adapter struct
2510 * @scmd: pointer to scsi command object
2511 *
2512 * Called by task management callback handlers.
2513 */
2514static void
2515_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2516{
2517 struct scsi_target *starget = scmd->device->sdev_target;
2518 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2519 struct _sas_device *sas_device = NULL;
2520 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302521 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302522
2523 if (!priv_target)
2524 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302525 if (ioc->hide_ir_msg)
2526 device_str = "WarpDrive";
2527 else
2528 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302529
2530 scsi_print_command(scmd);
2531 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302532 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2533 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2534 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302535 } else {
2536 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2537 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2538 priv_target->sas_address);
2539 if (sas_device) {
2540 if (priv_target->flags &
2541 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2542 starget_printk(KERN_INFO, starget,
2543 "volume handle(0x%04x), "
2544 "volume wwid(0x%016llx)\n",
2545 sas_device->volume_handle,
2546 (unsigned long long)sas_device->volume_wwid);
2547 }
2548 starget_printk(KERN_INFO, starget,
2549 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2550 sas_device->handle,
2551 (unsigned long long)sas_device->sas_address,
2552 sas_device->phy);
2553 starget_printk(KERN_INFO, starget,
2554 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2555 (unsigned long long)sas_device->enclosure_logical_id,
2556 sas_device->slot);
2557 }
2558 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2559 }
2560}
2561
2562/**
Eric Moored5d135b2009-05-18 13:02:08 -06002563 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302564 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002565 *
2566 * Returns SUCCESS if command aborted else FAILED
2567 */
2568static int
Eric Moored5d135b2009-05-18 13:02:08 -06002569_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002570{
2571 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2572 struct MPT2SAS_DEVICE *sas_device_priv_data;
2573 u16 smid;
2574 u16 handle;
2575 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002576
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302577 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2578 "scmd(%p)\n", scmd);
2579 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002580
2581 sas_device_priv_data = scmd->device->hostdata;
2582 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302583 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2584 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002585 scmd->result = DID_NO_CONNECT << 16;
2586 scmd->scsi_done(scmd);
2587 r = SUCCESS;
2588 goto out;
2589 }
2590
2591 /* search for the command */
2592 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2593 if (!smid) {
2594 scmd->result = DID_RESET << 16;
2595 r = SUCCESS;
2596 goto out;
2597 }
2598
2599 /* for hidden raid components and volumes this is not supported */
2600 if (sas_device_priv_data->sas_target->flags &
2601 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2602 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2603 scmd->result = DID_RESET << 16;
2604 r = FAILED;
2605 goto out;
2606 }
2607
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302608 mpt2sas_halt_firmware(ioc);
2609
Eric Moore635374e2009-03-09 01:21:12 -06002610 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302611 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2612 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302613 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2614 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002615
2616 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302617 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2618 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002619 return r;
2620}
2621
Eric Moore635374e2009-03-09 01:21:12 -06002622/**
Eric Moored5d135b2009-05-18 13:02:08 -06002623 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302624 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002625 *
2626 * Returns SUCCESS if command aborted else FAILED
2627 */
2628static int
Eric Moored5d135b2009-05-18 13:02:08 -06002629_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002630{
2631 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2632 struct MPT2SAS_DEVICE *sas_device_priv_data;
2633 struct _sas_device *sas_device;
2634 unsigned long flags;
2635 u16 handle;
2636 int r;
2637
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302638 struct scsi_target *starget = scmd->device->sdev_target;
2639
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302640 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302641 "scmd(%p)\n", scmd);
2642 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002643
2644 sas_device_priv_data = scmd->device->hostdata;
2645 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302646 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302647 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002648 scmd->result = DID_NO_CONNECT << 16;
2649 scmd->scsi_done(scmd);
2650 r = SUCCESS;
2651 goto out;
2652 }
2653
2654 /* for hidden raid components obtain the volume_handle */
2655 handle = 0;
2656 if (sas_device_priv_data->sas_target->flags &
2657 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2658 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2659 sas_device = _scsih_sas_device_find_by_handle(ioc,
2660 sas_device_priv_data->sas_target->handle);
2661 if (sas_device)
2662 handle = sas_device->volume_handle;
2663 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2664 } else
2665 handle = sas_device_priv_data->sas_target->handle;
2666
2667 if (!handle) {
2668 scmd->result = DID_RESET << 16;
2669 r = FAILED;
2670 goto out;
2671 }
2672
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302673 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2674 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302675 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2676 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002677
2678 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302679 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2680 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002681 return r;
2682}
2683
2684/**
Eric Moored5d135b2009-05-18 13:02:08 -06002685 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302686 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002687 *
2688 * Returns SUCCESS if command aborted else FAILED
2689 */
2690static int
Eric Moored5d135b2009-05-18 13:02:08 -06002691_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002692{
2693 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2694 struct MPT2SAS_DEVICE *sas_device_priv_data;
2695 struct _sas_device *sas_device;
2696 unsigned long flags;
2697 u16 handle;
2698 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302699 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002700
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302701 starget_printk(KERN_INFO, starget, "attempting target reset! "
2702 "scmd(%p)\n", scmd);
2703 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002704
2705 sas_device_priv_data = scmd->device->hostdata;
2706 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302707 starget_printk(KERN_INFO, starget, "target been deleted! "
2708 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002709 scmd->result = DID_NO_CONNECT << 16;
2710 scmd->scsi_done(scmd);
2711 r = SUCCESS;
2712 goto out;
2713 }
2714
2715 /* for hidden raid components obtain the volume_handle */
2716 handle = 0;
2717 if (sas_device_priv_data->sas_target->flags &
2718 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2719 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2720 sas_device = _scsih_sas_device_find_by_handle(ioc,
2721 sas_device_priv_data->sas_target->handle);
2722 if (sas_device)
2723 handle = sas_device->volume_handle;
2724 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2725 } else
2726 handle = sas_device_priv_data->sas_target->handle;
2727
2728 if (!handle) {
2729 scmd->result = DID_RESET << 16;
2730 r = FAILED;
2731 goto out;
2732 }
2733
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302734 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2735 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302736 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002737
2738 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302739 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2740 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002741 return r;
2742}
2743
2744/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302745 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302746 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002747 *
2748 * Returns SUCCESS if command aborted else FAILED
2749 */
2750static int
Eric Moored5d135b2009-05-18 13:02:08 -06002751_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002752{
2753 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2754 int r, retval;
2755
2756 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2757 ioc->name, scmd);
2758 scsi_print_command(scmd);
2759
2760 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2761 FORCE_BIG_HAMMER);
2762 r = (retval < 0) ? FAILED : SUCCESS;
2763 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2764 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2765
2766 return r;
2767}
2768
2769/**
2770 * _scsih_fw_event_add - insert and queue up fw_event
2771 * @ioc: per adapter object
2772 * @fw_event: object describing the event
2773 * Context: This function will acquire ioc->fw_event_lock.
2774 *
2775 * This adds the firmware event object into link list, then queues it up to
2776 * be processed from user context.
2777 *
2778 * Return nothing.
2779 */
2780static void
2781_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2782{
2783 unsigned long flags;
2784
2785 if (ioc->firmware_event_thread == NULL)
2786 return;
2787
2788 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2789 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302790 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2791 queue_delayed_work(ioc->firmware_event_thread,
2792 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002793 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2794}
2795
2796/**
2797 * _scsih_fw_event_free - delete fw_event
2798 * @ioc: per adapter object
2799 * @fw_event: object describing the event
2800 * Context: This function will acquire ioc->fw_event_lock.
2801 *
2802 * This removes firmware event object from link list, frees associated memory.
2803 *
2804 * Return nothing.
2805 */
2806static void
2807_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2808 *fw_event)
2809{
2810 unsigned long flags;
2811
2812 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2813 list_del(&fw_event->list);
2814 kfree(fw_event->event_data);
2815 kfree(fw_event);
2816 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2817}
2818
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302819
Eric Moore635374e2009-03-09 01:21:12 -06002820/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302821 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002822 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002823 *
2824 * Return nothing.
2825 */
2826static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302827_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002828{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302829 struct fw_event_work *fw_event;
2830
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302831 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302832 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002833
2834 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2835 if (!fw_event)
2836 return;
2837
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302838 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2839 fw_event->ioc = ioc;
2840 _scsih_fw_event_add(ioc, fw_event);
2841}
2842
2843/**
2844 * mpt2sas_port_enable_complete - port enable completed (fake event)
2845 * @ioc: per adapter object
2846 *
2847 * Return nothing.
2848 */
2849void
2850mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2851{
2852 struct fw_event_work *fw_event;
2853
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302854 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2855 if (!fw_event)
2856 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302857 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302858 fw_event->ioc = ioc;
2859 _scsih_fw_event_add(ioc, fw_event);
2860}
2861
2862/**
2863 * _scsih_fw_event_cleanup_queue - cleanup event queue
2864 * @ioc: per adapter object
2865 *
2866 * Walk the firmware event queue, either killing timers, or waiting
2867 * for outstanding events to complete
2868 *
2869 * Return nothing.
2870 */
2871static void
2872_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2873{
2874 struct fw_event_work *fw_event, *next;
2875
2876 if (list_empty(&ioc->fw_event_list) ||
2877 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002878 return;
2879
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302880 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2881 if (cancel_delayed_work(&fw_event->delayed_work)) {
2882 _scsih_fw_event_free(ioc, fw_event);
2883 continue;
2884 }
2885 fw_event->cancel_pending_work = 1;
2886 }
Eric Moore635374e2009-03-09 01:21:12 -06002887}
2888
Eric Moore635374e2009-03-09 01:21:12 -06002889/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302890 * _scsih_ublock_io_all_device - unblock every device
2891 * @ioc: per adapter object
2892 *
2893 * change the device state from block to running
2894 */
2895static void
2896_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2897{
2898 struct MPT2SAS_DEVICE *sas_device_priv_data;
2899 struct scsi_device *sdev;
2900
2901 shost_for_each_device(sdev, ioc->shost) {
2902 sas_device_priv_data = sdev->hostdata;
2903 if (!sas_device_priv_data)
2904 continue;
2905 if (!sas_device_priv_data->block)
2906 continue;
2907 sas_device_priv_data->block = 0;
2908 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2909 "handle(0x%04x)\n",
2910 sas_device_priv_data->sas_target->handle));
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002911 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302912 }
2913}
2914/**
Eric Moore635374e2009-03-09 01:21:12 -06002915 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2916 * @ioc: per adapter object
2917 * @handle: device handle
2918 *
2919 * During device pull we need to appropiately set the sdev state.
2920 */
2921static void
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302922_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06002923{
2924 struct MPT2SAS_DEVICE *sas_device_priv_data;
2925 struct scsi_device *sdev;
2926
2927 shost_for_each_device(sdev, ioc->shost) {
2928 sas_device_priv_data = sdev->hostdata;
2929 if (!sas_device_priv_data)
2930 continue;
2931 if (!sas_device_priv_data->block)
2932 continue;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302933 if (sas_device_priv_data->sas_target->sas_address ==
2934 sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06002935 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2936 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05302937 "sas address(0x%016llx)\n", ioc->name,
2938 (unsigned long long)sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06002939 sas_device_priv_data->block = 0;
Mike Christie5d9fb5c2012-05-17 23:56:57 -05002940 scsi_internal_device_unblock(sdev, SDEV_RUNNING);
Eric Moore635374e2009-03-09 01:21:12 -06002941 }
2942 }
2943}
2944
2945/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302946 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2947 * @ioc: per adapter object
2948 * @handle: device handle
2949 *
2950 * During device pull we need to appropiately set the sdev state.
2951 */
2952static void
2953_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2954{
2955 struct MPT2SAS_DEVICE *sas_device_priv_data;
2956 struct scsi_device *sdev;
2957
2958 shost_for_each_device(sdev, ioc->shost) {
2959 sas_device_priv_data = sdev->hostdata;
2960 if (!sas_device_priv_data)
2961 continue;
2962 if (sas_device_priv_data->block)
2963 continue;
2964 sas_device_priv_data->block = 1;
2965 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2966 "handle(0x%04x)\n",
2967 sas_device_priv_data->sas_target->handle));
2968 scsi_internal_device_block(sdev);
2969 }
2970}
2971
2972
2973/**
Eric Moore635374e2009-03-09 01:21:12 -06002974 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2975 * @ioc: per adapter object
2976 * @handle: device handle
2977 *
2978 * During device pull we need to appropiately set the sdev state.
2979 */
2980static void
2981_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2982{
2983 struct MPT2SAS_DEVICE *sas_device_priv_data;
2984 struct scsi_device *sdev;
2985
2986 shost_for_each_device(sdev, ioc->shost) {
2987 sas_device_priv_data = sdev->hostdata;
2988 if (!sas_device_priv_data)
2989 continue;
2990 if (sas_device_priv_data->block)
2991 continue;
2992 if (sas_device_priv_data->sas_target->handle == handle) {
2993 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2994 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2995 "handle(0x%04x)\n", ioc->name, handle));
2996 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302997 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002998 }
2999 }
3000}
3001
3002/**
3003 * _scsih_block_io_to_children_attached_to_ex
3004 * @ioc: per adapter object
3005 * @sas_expander: the sas_device object
3006 *
3007 * This routine set sdev state to SDEV_BLOCK for all devices
3008 * attached to this expander. This function called when expander is
3009 * pulled.
3010 */
3011static void
3012_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
3013 struct _sas_node *sas_expander)
3014{
3015 struct _sas_port *mpt2sas_port;
3016 struct _sas_device *sas_device;
3017 struct _sas_node *expander_sibling;
3018 unsigned long flags;
3019
3020 if (!sas_expander)
3021 return;
3022
3023 list_for_each_entry(mpt2sas_port,
3024 &sas_expander->sas_port_list, port_list) {
3025 if (mpt2sas_port->remote_identify.device_type ==
3026 SAS_END_DEVICE) {
3027 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3028 sas_device =
3029 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3030 mpt2sas_port->remote_identify.sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303031 if (sas_device)
3032 set_bit(sas_device->handle,
3033 ioc->blocking_handles);
Eric Moore635374e2009-03-09 01:21:12 -06003034 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06003035 }
3036 }
3037
3038 list_for_each_entry(mpt2sas_port,
3039 &sas_expander->sas_port_list, port_list) {
3040
3041 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303042 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003043 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303044 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003045 expander_sibling =
3046 mpt2sas_scsih_expander_find_by_sas_address(
3047 ioc, mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06003048 _scsih_block_io_to_children_attached_to_ex(ioc,
3049 expander_sibling);
3050 }
3051 }
3052}
3053
3054/**
3055 * _scsih_block_io_to_children_attached_directly
3056 * @ioc: per adapter object
3057 * @event_data: topology change event data
3058 *
3059 * This routine set sdev state to SDEV_BLOCK for all devices
3060 * direct attached during device pull.
3061 */
3062static void
3063_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3064 Mpi2EventDataSasTopologyChangeList_t *event_data)
3065{
3066 int i;
3067 u16 handle;
3068 u16 reason_code;
3069 u8 phy_number;
3070
3071 for (i = 0; i < event_data->NumEntries; i++) {
3072 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3073 if (!handle)
3074 continue;
3075 phy_number = event_data->StartPhyNum + i;
3076 reason_code = event_data->PHY[i].PhyStatus &
3077 MPI2_EVENT_SAS_TOPO_RC_MASK;
3078 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3079 _scsih_block_io_device(ioc, handle);
3080 }
3081}
3082
3083/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303084 * _scsih_tm_tr_send - send task management request
3085 * @ioc: per adapter object
3086 * @handle: device handle
3087 * Context: interrupt time.
3088 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003089 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303090 * with controller firmware. This function will issue target reset
3091 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003092 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303093 *
3094 * This is designed to send muliple task management request at the same
3095 * time to the fifo. If the fifo is full, we will append the request,
3096 * and process it in a future completion.
3097 */
3098static void
3099_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3100{
3101 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303102 u16 smid;
3103 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303104 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3105 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303106 unsigned long flags;
3107 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303108 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303109
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303110 if (ioc->remove_host) {
3111 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3112 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3113 return;
3114 } else if (ioc->pci_error_recovery) {
3115 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3116 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3117 handle));
3118 return;
3119 }
3120 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3121 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3122 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3123 "operational: handle(0x%04x)\n", __func__, ioc->name,
3124 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303125 return;
3126 }
3127
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303128 /* if PD, then return */
3129 if (test_bit(handle, ioc->pd_handles))
3130 return;
3131
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303132 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3133 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303134 if (sas_device && sas_device->starget &&
3135 sas_device->starget->hostdata) {
3136 sas_target_priv_data = sas_device->starget->hostdata;
3137 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303138 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303139 }
3140 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303141
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303142 if (sas_target_priv_data) {
3143 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3144 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3145 (unsigned long long)sas_address));
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05303146 _scsih_ublock_io_device(ioc, sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303147 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303148 }
3149
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303150 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3151 if (!smid) {
3152 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3153 if (!delayed_tr)
3154 return;
3155 INIT_LIST_HEAD(&delayed_tr->list);
3156 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303157 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3158 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3159 "DELAYED:tr:handle(0x%04x), (open)\n",
3160 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303161 return;
3162 }
3163
Kashyap, Desai1278b112010-03-09 17:34:13 +05303164 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3165 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3166 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303167 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3168 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3169 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3170 mpi_request->DevHandle = cpu_to_le16(handle);
3171 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303172 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3173}
3174
3175
3176
3177/**
3178 * _scsih_sas_control_complete - completion routine
3179 * @ioc: per adapter object
3180 * @smid: system request message index
3181 * @msix_index: MSIX table index supplied by the OS
3182 * @reply: reply message frame(lower 32bit addr)
3183 * Context: interrupt time.
3184 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003185 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303186 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003187 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303188 *
3189 * Return 1 meaning mf should be freed from _base_interrupt
3190 * 0 means the mf is freed from this function.
3191 */
3192static u8
3193_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3194 u8 msix_index, u32 reply)
3195{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303196 Mpi2SasIoUnitControlReply_t *mpi_reply =
3197 mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303198 if (likely(mpi_reply)) {
3199 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3200 "sc_complete:handle(0x%04x), (open) "
3201 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3202 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3203 le16_to_cpu(mpi_reply->IOCStatus),
3204 le32_to_cpu(mpi_reply->IOCLogInfo)));
3205 } else {
3206 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3207 ioc->name, __FILE__, __LINE__, __func__);
3208 }
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303209 return 1;
3210}
3211
3212/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303213 * _scsih_tm_tr_volume_send - send target reset request for volumes
3214 * @ioc: per adapter object
3215 * @handle: device handle
3216 * Context: interrupt time.
3217 *
3218 * This is designed to send muliple task management request at the same
3219 * time to the fifo. If the fifo is full, we will append the request,
3220 * and process it in a future completion.
3221 */
3222static void
3223_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3224{
3225 Mpi2SCSITaskManagementRequest_t *mpi_request;
3226 u16 smid;
3227 struct _tr_list *delayed_tr;
3228
Eric Moore3cb54692010-07-08 14:44:34 -06003229 if (ioc->shost_recovery || ioc->remove_host ||
3230 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303231 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3232 "progress!\n", __func__, ioc->name));
3233 return;
3234 }
3235
3236 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3237 if (!smid) {
3238 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3239 if (!delayed_tr)
3240 return;
3241 INIT_LIST_HEAD(&delayed_tr->list);
3242 delayed_tr->handle = handle;
3243 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3244 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3245 "DELAYED:tr:handle(0x%04x), (open)\n",
3246 ioc->name, handle));
3247 return;
3248 }
3249
3250 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3251 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3252 ioc->tm_tr_volume_cb_idx));
3253 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3254 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3255 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3256 mpi_request->DevHandle = cpu_to_le16(handle);
3257 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3258 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3259}
3260
3261/**
3262 * _scsih_tm_volume_tr_complete - target reset completion
3263 * @ioc: per adapter object
3264 * @smid: system request message index
3265 * @msix_index: MSIX table index supplied by the OS
3266 * @reply: reply message frame(lower 32bit addr)
3267 * Context: interrupt time.
3268 *
3269 * Return 1 meaning mf should be freed from _base_interrupt
3270 * 0 means the mf is freed from this function.
3271 */
3272static u8
3273_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3274 u8 msix_index, u32 reply)
3275{
3276 u16 handle;
3277 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3278 Mpi2SCSITaskManagementReply_t *mpi_reply =
3279 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3280
Eric Moore3cb54692010-07-08 14:44:34 -06003281 if (ioc->shost_recovery || ioc->remove_host ||
3282 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303283 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3284 "progress!\n", __func__, ioc->name));
3285 return 1;
3286 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303287 if (unlikely(!mpi_reply)) {
3288 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3289 ioc->name, __FILE__, __LINE__, __func__);
3290 return 1;
3291 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303292 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3293 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3294 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3295 dewtprintk(ioc, printk("spurious interrupt: "
3296 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3297 le16_to_cpu(mpi_reply->DevHandle), smid));
3298 return 0;
3299 }
3300
3301 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3302 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3303 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3304 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3305 le32_to_cpu(mpi_reply->IOCLogInfo),
3306 le32_to_cpu(mpi_reply->TerminationCount)));
3307
3308 return _scsih_check_for_pending_tm(ioc, smid);
3309}
3310
3311/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303312 * _scsih_tm_tr_complete -
3313 * @ioc: per adapter object
3314 * @smid: system request message index
3315 * @msix_index: MSIX table index supplied by the OS
3316 * @reply: reply message frame(lower 32bit addr)
3317 * Context: interrupt time.
3318 *
3319 * This is the target reset completion routine.
3320 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003321 * handshake protocol with controller firmware.
3322 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303323 *
3324 * Return 1 meaning mf should be freed from _base_interrupt
3325 * 0 means the mf is freed from this function.
3326 */
3327static u8
3328_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3329 u32 reply)
3330{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303331 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303332 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303333 Mpi2SCSITaskManagementReply_t *mpi_reply =
3334 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3335 Mpi2SasIoUnitControlRequest_t *mpi_request;
3336 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303337 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303338
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303339 if (ioc->remove_host) {
3340 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3341 "removed\n", __func__, ioc->name));
3342 return 1;
3343 } else if (ioc->pci_error_recovery) {
3344 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3345 "error recovery\n", __func__, ioc->name));
3346 return 1;
3347 }
3348 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3349 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3350 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3351 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303352 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303353 }
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05303354 if (unlikely(!mpi_reply)) {
3355 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
3356 ioc->name, __FILE__, __LINE__, __func__);
3357 return 1;
3358 }
Kashyap, Desai1278b112010-03-09 17:34:13 +05303359 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3360 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3361 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3362 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3363 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3364 le16_to_cpu(mpi_reply->DevHandle), smid));
3365 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303366 }
3367
Kashyap, Desai1278b112010-03-09 17:34:13 +05303368 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3369 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3370 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3371 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3372 le32_to_cpu(mpi_reply->IOCLogInfo),
3373 le32_to_cpu(mpi_reply->TerminationCount)));
3374
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303375 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3376 if (!smid_sas_ctrl) {
3377 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3378 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303379 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303380 }
3381
Kashyap, Desai1278b112010-03-09 17:34:13 +05303382 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3383 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3384 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303385 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3386 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3387 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3388 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303389 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303390 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303391
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303392 return _scsih_check_for_pending_tm(ioc, smid);
3393}
3394
3395/**
3396 * _scsih_check_for_pending_tm - check for pending task management
3397 * @ioc: per adapter object
3398 * @smid: system request message index
3399 *
3400 * This will check delayed target reset list, and feed the
3401 * next reqeust.
3402 *
3403 * Return 1 meaning mf should be freed from _base_interrupt
3404 * 0 means the mf is freed from this function.
3405 */
3406static u8
3407_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3408{
3409 struct _tr_list *delayed_tr;
3410
3411 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3412 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3413 struct _tr_list, list);
3414 mpt2sas_base_free_smid(ioc, smid);
3415 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3416 list_del(&delayed_tr->list);
3417 kfree(delayed_tr);
3418 return 0;
3419 }
3420
Kashyap, Desai1278b112010-03-09 17:34:13 +05303421 if (!list_empty(&ioc->delayed_tr_list)) {
3422 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3423 struct _tr_list, list);
3424 mpt2sas_base_free_smid(ioc, smid);
3425 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3426 list_del(&delayed_tr->list);
3427 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303428 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303429 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303430
Kashyap, Desai1278b112010-03-09 17:34:13 +05303431 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303432}
3433
3434/**
Eric Moore635374e2009-03-09 01:21:12 -06003435 * _scsih_check_topo_delete_events - sanity check on topo events
3436 * @ioc: per adapter object
3437 * @event_data: the event data payload
3438 *
3439 * This routine added to better handle cable breaker.
3440 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003441 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003442 * add and delete events in a single shot. When there is a delete event
3443 * the routine will void any pending add events waiting in the event queue.
3444 *
3445 * Return nothing.
3446 */
3447static void
3448_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3449 Mpi2EventDataSasTopologyChangeList_t *event_data)
3450{
3451 struct fw_event_work *fw_event;
3452 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3453 u16 expander_handle;
3454 struct _sas_node *sas_expander;
3455 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303456 int i, reason_code;
3457 u16 handle;
3458
3459 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303460 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3461 if (!handle)
3462 continue;
3463 reason_code = event_data->PHY[i].PhyStatus &
3464 MPI2_EVENT_SAS_TOPO_RC_MASK;
3465 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3466 _scsih_tm_tr_send(ioc, handle);
3467 }
Eric Moore635374e2009-03-09 01:21:12 -06003468
3469 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3470 if (expander_handle < ioc->sas_hba.num_phys) {
3471 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3472 return;
3473 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303474 if (event_data->ExpStatus ==
3475 MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING) {
3476 /* put expander attached devices into blocking state */
Eric Moore635374e2009-03-09 01:21:12 -06003477 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3478 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3479 expander_handle);
Eric Moore635374e2009-03-09 01:21:12 -06003480 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05303481 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3482 do {
3483 handle = find_first_bit(ioc->blocking_handles,
3484 ioc->facts.MaxDevHandle);
3485 if (handle < ioc->facts.MaxDevHandle)
3486 _scsih_block_io_device(ioc, handle);
3487 } while (test_and_clear_bit(handle, ioc->blocking_handles));
Eric Moore635374e2009-03-09 01:21:12 -06003488 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3489 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3490
3491 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3492 return;
3493
3494 /* mark ignore flag for pending events */
3495 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3496 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3497 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3498 fw_event->ignore)
3499 continue;
3500 local_event_data = fw_event->event_data;
3501 if (local_event_data->ExpStatus ==
3502 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3503 local_event_data->ExpStatus ==
3504 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3505 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3506 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303507 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003508 "setting ignoring flag\n", ioc->name));
3509 fw_event->ignore = 1;
3510 }
3511 }
3512 }
3513 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3514}
3515
3516/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303517 * _scsih_set_volume_delete_flag - setting volume delete flag
3518 * @ioc: per adapter object
3519 * @handle: device handle
3520 *
3521 * This
3522 * Return nothing.
3523 */
3524static void
3525_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3526{
3527 struct _raid_device *raid_device;
3528 struct MPT2SAS_TARGET *sas_target_priv_data;
3529 unsigned long flags;
3530
3531 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3532 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3533 if (raid_device && raid_device->starget &&
3534 raid_device->starget->hostdata) {
3535 sas_target_priv_data =
3536 raid_device->starget->hostdata;
3537 sas_target_priv_data->deleted = 1;
3538 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3539 "setting delete flag: handle(0x%04x), "
3540 "wwid(0x%016llx)\n", ioc->name, handle,
3541 (unsigned long long) raid_device->wwid));
3542 }
3543 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3544}
3545
3546/**
3547 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3548 * @handle: input handle
3549 * @a: handle for volume a
3550 * @b: handle for volume b
3551 *
3552 * IR firmware only supports two raid volumes. The purpose of this
3553 * routine is to set the volume handle in either a or b. When the given
3554 * input handle is non-zero, or when a and b have not been set before.
3555 */
3556static void
3557_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3558{
3559 if (!handle || handle == *a || handle == *b)
3560 return;
3561 if (!*a)
3562 *a = handle;
3563 else if (!*b)
3564 *b = handle;
3565}
3566
3567/**
3568 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3569 * @ioc: per adapter object
3570 * @event_data: the event data payload
3571 * Context: interrupt time.
3572 *
3573 * This routine will send target reset to volume, followed by target
3574 * resets to the PDs. This is called when a PD has been removed, or
3575 * volume has been deleted or removed. When the target reset is sent
3576 * to volume, the PD target resets need to be queued to start upon
3577 * completion of the volume target reset.
3578 *
3579 * Return nothing.
3580 */
3581static void
3582_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3583 Mpi2EventDataIrConfigChangeList_t *event_data)
3584{
3585 Mpi2EventIrConfigElement_t *element;
3586 int i;
3587 u16 handle, volume_handle, a, b;
3588 struct _tr_list *delayed_tr;
3589
3590 a = 0;
3591 b = 0;
3592
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303593 if (ioc->is_warpdrive)
3594 return;
3595
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303596 /* Volume Resets for Deleted or Removed */
3597 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3598 for (i = 0; i < event_data->NumElements; i++, element++) {
3599 if (element->ReasonCode ==
3600 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3601 element->ReasonCode ==
3602 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3603 volume_handle = le16_to_cpu(element->VolDevHandle);
3604 _scsih_set_volume_delete_flag(ioc, volume_handle);
3605 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3606 }
3607 }
3608
3609 /* Volume Resets for UNHIDE events */
3610 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3611 for (i = 0; i < event_data->NumElements; i++, element++) {
3612 if (le32_to_cpu(event_data->Flags) &
3613 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3614 continue;
3615 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3616 volume_handle = le16_to_cpu(element->VolDevHandle);
3617 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3618 }
3619 }
3620
3621 if (a)
3622 _scsih_tm_tr_volume_send(ioc, a);
3623 if (b)
3624 _scsih_tm_tr_volume_send(ioc, b);
3625
3626 /* PD target resets */
3627 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3628 for (i = 0; i < event_data->NumElements; i++, element++) {
3629 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3630 continue;
3631 handle = le16_to_cpu(element->PhysDiskDevHandle);
3632 volume_handle = le16_to_cpu(element->VolDevHandle);
3633 clear_bit(handle, ioc->pd_handles);
3634 if (!volume_handle)
3635 _scsih_tm_tr_send(ioc, handle);
3636 else if (volume_handle == a || volume_handle == b) {
3637 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3638 BUG_ON(!delayed_tr);
3639 INIT_LIST_HEAD(&delayed_tr->list);
3640 delayed_tr->handle = handle;
3641 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3642 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3643 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3644 handle));
3645 } else
3646 _scsih_tm_tr_send(ioc, handle);
3647 }
3648}
3649
3650
3651/**
3652 * _scsih_check_volume_delete_events - set delete flag for volumes
3653 * @ioc: per adapter object
3654 * @event_data: the event data payload
3655 * Context: interrupt time.
3656 *
3657 * This will handle the case when the cable connected to entire volume is
3658 * pulled. We will take care of setting the deleted flag so normal IO will
3659 * not be sent.
3660 *
3661 * Return nothing.
3662 */
3663static void
3664_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3665 Mpi2EventDataIrVolume_t *event_data)
3666{
3667 u32 state;
3668
3669 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3670 return;
3671 state = le32_to_cpu(event_data->NewValue);
3672 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3673 MPI2_RAID_VOL_STATE_FAILED)
3674 _scsih_set_volume_delete_flag(ioc,
3675 le16_to_cpu(event_data->VolDevHandle));
3676}
3677
3678/**
Eric Moore635374e2009-03-09 01:21:12 -06003679 * _scsih_flush_running_cmds - completing outstanding commands.
3680 * @ioc: per adapter object
3681 *
3682 * The flushing out of all pending scmd commands following host reset,
3683 * where all IO is dropped to the floor.
3684 *
3685 * Return nothing.
3686 */
3687static void
3688_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3689{
3690 struct scsi_cmnd *scmd;
3691 u16 smid;
3692 u16 count = 0;
3693
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303694 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303695 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003696 if (!scmd)
3697 continue;
3698 count++;
3699 mpt2sas_base_free_smid(ioc, smid);
3700 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003701 if (ioc->pci_error_recovery)
3702 scmd->result = DID_NO_CONNECT << 16;
3703 else
3704 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003705 scmd->scsi_done(scmd);
3706 }
3707 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3708 ioc->name, count));
3709}
3710
3711/**
Eric Moore3c621b32009-05-18 12:59:41 -06003712 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3713 * @scmd: pointer to scsi command object
3714 * @mpi_request: pointer to the SCSI_IO reqest message frame
3715 *
3716 * Supporting protection 1 and 3.
3717 *
3718 * Returns nothing
3719 */
3720static void
3721_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3722{
3723 u16 eedp_flags;
3724 unsigned char prot_op = scsi_get_prot_op(scmd);
3725 unsigned char prot_type = scsi_get_prot_type(scmd);
3726
Eric Moored334aa72010-04-22 10:47:40 -06003727 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003728 return;
3729
3730 if (prot_op == SCSI_PROT_READ_STRIP)
3731 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3732 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3733 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3734 else
3735 return;
3736
Eric Moore3c621b32009-05-18 12:59:41 -06003737 switch (prot_type) {
3738 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003739 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003740
3741 /*
3742 * enable ref/guard checking
3743 * auto increment ref tag
3744 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303745 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003746 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3747 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3748 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3749 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003750 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003751
Eric Moore3c621b32009-05-18 12:59:41 -06003752 case SCSI_PROT_DIF_TYPE3:
3753
3754 /*
3755 * enable guard checking
3756 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303757 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003758 break;
3759 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303760 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3761 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003762}
3763
3764/**
3765 * _scsih_eedp_error_handling - return sense code for EEDP errors
3766 * @scmd: pointer to scsi command object
3767 * @ioc_status: ioc status
3768 *
3769 * Returns nothing
3770 */
3771static void
3772_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3773{
3774 u8 ascq;
3775 u8 sk;
3776 u8 host_byte;
3777
3778 switch (ioc_status) {
3779 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3780 ascq = 0x01;
3781 break;
3782 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3783 ascq = 0x02;
3784 break;
3785 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3786 ascq = 0x03;
3787 break;
3788 default:
3789 ascq = 0x00;
3790 break;
3791 }
3792
3793 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3794 sk = ILLEGAL_REQUEST;
3795 host_byte = DID_ABORT;
3796 } else {
3797 sk = ABORTED_COMMAND;
3798 host_byte = DID_OK;
3799 }
3800
3801 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3802 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3803 SAM_STAT_CHECK_CONDITION;
3804}
3805
3806/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303807 * _scsih_scsi_direct_io_get - returns direct io flag
3808 * @ioc: per adapter object
3809 * @smid: system request message index
3810 *
3811 * Returns the smid stored scmd pointer.
3812 */
3813static inline u8
3814_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3815{
3816 return ioc->scsi_lookup[smid - 1].direct_io;
3817}
3818
3819/**
3820 * _scsih_scsi_direct_io_set - sets direct io flag
3821 * @ioc: per adapter object
3822 * @smid: system request message index
3823 * @direct_io: Zero or non-zero value to set in the direct_io flag
3824 *
3825 * Returns Nothing.
3826 */
3827static inline void
3828_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3829{
3830 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3831}
3832
3833
3834/**
3835 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3836 * @ioc: per adapter object
3837 * @scmd: pointer to scsi command object
3838 * @raid_device: pointer to raid device data structure
3839 * @mpi_request: pointer to the SCSI_IO reqest message frame
3840 * @smid: system request message index
3841 *
3842 * Returns nothing
3843 */
3844static void
3845_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3846 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3847 u16 smid)
3848{
3849 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3850 u32 stripe_sz, stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303851 u8 num_pds, *cdb_ptr, i;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303852 u8 cdb0 = scmd->cmnd[0];
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303853 u64 v_llba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303854
3855 /*
3856 * Try Direct I/O to RAID memeber disks
3857 */
3858 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3859 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3860 cdb_ptr = mpi_request->CDB.CDB32;
3861
3862 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3863 | cdb_ptr[5])) {
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303864 io_size = scsi_bufflen(scmd) >>
3865 raid_device->block_exponent;
3866 i = (cdb0 < READ_16) ? 2 : 6;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303867 /* get virtual lba */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303868 v_lba = be32_to_cpu(*(__be32 *)(&cdb_ptr[i]));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303869
3870 if (((u64)v_lba + (u64)io_size - 1) <=
3871 (u32)raid_device->max_lba) {
3872 stripe_sz = raid_device->stripe_sz;
3873 stripe_exp = raid_device->stripe_exponent;
3874 stripe_off = v_lba & (stripe_sz - 1);
3875
3876 /* Check whether IO falls within a stripe */
3877 if ((stripe_off + io_size) <= stripe_sz) {
3878 num_pds = raid_device->num_pds;
3879 p_lba = v_lba >> stripe_exp;
3880 stripe_unit = p_lba / num_pds;
3881 column = p_lba % num_pds;
3882 p_lba = (stripe_unit << stripe_exp) +
3883 stripe_off;
3884 mpi_request->DevHandle =
3885 cpu_to_le16(raid_device->
3886 pd_handle[column]);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303887 (*(__be32 *)(&cdb_ptr[i])) =
3888 cpu_to_be32(p_lba);
3889 /*
3890 * WD: To indicate this I/O is directI/O
3891 */
3892 _scsih_scsi_direct_io_set(ioc, smid, 1);
3893 }
3894 }
3895 } else {
3896 io_size = scsi_bufflen(scmd) >>
3897 raid_device->block_exponent;
3898 /* get virtual lba */
3899 v_llba = be64_to_cpu(*(__be64 *)(&cdb_ptr[2]));
3900
3901 if ((v_llba + (u64)io_size - 1) <=
3902 raid_device->max_lba) {
3903 stripe_sz = raid_device->stripe_sz;
3904 stripe_exp = raid_device->stripe_exponent;
3905 stripe_off = (u32) (v_llba & (stripe_sz - 1));
3906
3907 /* Check whether IO falls within a stripe */
3908 if ((stripe_off + io_size) <= stripe_sz) {
3909 num_pds = raid_device->num_pds;
3910 p_lba = (u32)(v_llba >> stripe_exp);
3911 stripe_unit = p_lba / num_pds;
3912 column = p_lba % num_pds;
3913 p_lba = (stripe_unit << stripe_exp) +
3914 stripe_off;
3915 mpi_request->DevHandle =
3916 cpu_to_le16(raid_device->
3917 pd_handle[column]);
3918 (*(__be64 *)(&cdb_ptr[2])) =
3919 cpu_to_be64((u64)p_lba);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303920 /*
3921 * WD: To indicate this I/O is directI/O
3922 */
3923 _scsih_scsi_direct_io_set(ioc, smid, 1);
3924 }
3925 }
3926 }
3927 }
3928}
3929
3930/**
Eric Moored5d135b2009-05-18 13:02:08 -06003931 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003932 * @scmd: pointer to scsi command object
3933 * @done: function pointer to be invoked on completion
3934 *
3935 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3936 *
3937 * Returns 0 on success. If there's a failure, return either:
3938 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3939 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3940 */
3941static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003942_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003943{
3944 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3945 struct MPT2SAS_DEVICE *sas_device_priv_data;
3946 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303947 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003948 Mpi2SCSIIORequest_t *mpi_request;
3949 u32 mpi_control;
3950 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003951
3952 scmd->scsi_done = done;
3953 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303954 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003955 scmd->result = DID_NO_CONNECT << 16;
3956 scmd->scsi_done(scmd);
3957 return 0;
3958 }
3959
Kashyap, Desai78215782011-06-14 10:57:08 +05303960 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003961 scmd->result = DID_NO_CONNECT << 16;
3962 scmd->scsi_done(scmd);
3963 return 0;
3964 }
3965
Eric Moore635374e2009-03-09 01:21:12 -06003966 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303967 /* invalid device handle */
3968 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003969 scmd->result = DID_NO_CONNECT << 16;
3970 scmd->scsi_done(scmd);
3971 return 0;
3972 }
3973
Kashyap, Desai130b9582010-04-08 17:54:32 +05303974 /* host recovery or link resets sent via IOCTLs */
3975 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003976 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003977 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303978 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3979 return SCSI_MLQUEUE_DEVICE_BUSY;
3980 /* device has been deleted */
3981 else if (sas_target_priv_data->deleted) {
3982 scmd->result = DID_NO_CONNECT << 16;
3983 scmd->scsi_done(scmd);
3984 return 0;
3985 }
Eric Moore635374e2009-03-09 01:21:12 -06003986
3987 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3988 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3989 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3990 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3991 else
3992 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3993
3994 /* set tags */
3995 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3996 if (scmd->device->tagged_supported) {
3997 if (scmd->device->ordered_tags)
3998 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3999 else
4000 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
4001 } else
4002/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
4003/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
4004 */
4005 mpi_control |= (0x500);
4006
4007 } else
4008 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304009 /* Make sure Device is not raid volume.
4010 * We do not expose raid functionality to upper layer for warpdrive.
4011 */
4012 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06004013 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06004014 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
4015
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304016 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06004017 if (!smid) {
4018 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
4019 ioc->name, __func__);
4020 goto out;
4021 }
4022 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4023 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06004024 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06004025 if (scmd->cmd_len == 32)
4026 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06004027 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4028 if (sas_device_priv_data->sas_target->flags &
4029 MPT_TARGET_FLAGS_RAID_COMPONENT)
4030 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
4031 else
4032 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
4033 mpi_request->DevHandle =
4034 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4035 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4036 mpi_request->Control = cpu_to_le32(mpi_control);
4037 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4038 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4039 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4040 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05304041 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004042 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4043 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
4044 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304045 mpi_request->VF_ID = 0; /* TODO */
4046 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004047 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4048 mpi_request->LUN);
4049 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4050
4051 if (!mpi_request->DataLength) {
4052 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
4053 } else {
4054 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
4055 mpt2sas_base_free_smid(ioc, smid);
4056 goto out;
4057 }
4058 }
4059
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304060 raid_device = sas_target_priv_data->raid_device;
4061 if (raid_device && raid_device->direct_io_enabled)
4062 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4063 smid);
4064
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304065 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4066 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304067 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304068 else
4069 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004070 return 0;
4071
4072 out:
4073 return SCSI_MLQUEUE_HOST_BUSY;
4074}
4075
Jeff Garzikf2812332010-11-16 02:10:29 -05004076static DEF_SCSI_QCMD(_scsih_qcmd)
4077
Eric Moore635374e2009-03-09 01:21:12 -06004078/**
4079 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4080 * @sense_buffer: sense data returned by target
4081 * @data: normalized skey/asc/ascq
4082 *
4083 * Return nothing.
4084 */
4085static void
4086_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4087{
4088 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4089 /* descriptor format */
4090 data->skey = sense_buffer[1] & 0x0F;
4091 data->asc = sense_buffer[2];
4092 data->ascq = sense_buffer[3];
4093 } else {
4094 /* fixed format */
4095 data->skey = sense_buffer[2] & 0x0F;
4096 data->asc = sense_buffer[12];
4097 data->ascq = sense_buffer[13];
4098 }
4099}
4100
4101#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4102/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004103 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004104 * @ioc: per adapter object
4105 * @scmd: pointer to scsi command object
4106 * @mpi_reply: reply mf payload returned from firmware
4107 *
4108 * scsi_status - SCSI Status code returned from target device
4109 * scsi_state - state info associated with SCSI_IO determined by ioc
4110 * ioc_status - ioc supplied status info
4111 *
4112 * Return nothing.
4113 */
4114static void
4115_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4116 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4117{
4118 u32 response_info;
4119 u8 *response_bytes;
4120 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4121 MPI2_IOCSTATUS_MASK;
4122 u8 scsi_state = mpi_reply->SCSIState;
4123 u8 scsi_status = mpi_reply->SCSIStatus;
4124 char *desc_ioc_state = NULL;
4125 char *desc_scsi_status = NULL;
4126 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304127 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304128 struct _sas_device *sas_device = NULL;
4129 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304130 struct scsi_target *starget = scmd->device->sdev_target;
4131 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304132 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304133
4134 if (!priv_target)
4135 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304136
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304137 if (ioc->hide_ir_msg)
4138 device_str = "WarpDrive";
4139 else
4140 device_str = "volume";
4141
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304142 if (log_info == 0x31170000)
4143 return;
Eric Moore635374e2009-03-09 01:21:12 -06004144
4145 switch (ioc_status) {
4146 case MPI2_IOCSTATUS_SUCCESS:
4147 desc_ioc_state = "success";
4148 break;
4149 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4150 desc_ioc_state = "invalid function";
4151 break;
4152 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4153 desc_ioc_state = "scsi recovered error";
4154 break;
4155 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4156 desc_ioc_state = "scsi invalid dev handle";
4157 break;
4158 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4159 desc_ioc_state = "scsi device not there";
4160 break;
4161 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4162 desc_ioc_state = "scsi data overrun";
4163 break;
4164 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4165 desc_ioc_state = "scsi data underrun";
4166 break;
4167 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4168 desc_ioc_state = "scsi io data error";
4169 break;
4170 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4171 desc_ioc_state = "scsi protocol error";
4172 break;
4173 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4174 desc_ioc_state = "scsi task terminated";
4175 break;
4176 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4177 desc_ioc_state = "scsi residual mismatch";
4178 break;
4179 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4180 desc_ioc_state = "scsi task mgmt failed";
4181 break;
4182 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4183 desc_ioc_state = "scsi ioc terminated";
4184 break;
4185 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4186 desc_ioc_state = "scsi ext terminated";
4187 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004188 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4189 desc_ioc_state = "eedp guard error";
4190 break;
4191 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4192 desc_ioc_state = "eedp ref tag error";
4193 break;
4194 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4195 desc_ioc_state = "eedp app tag error";
4196 break;
Eric Moore635374e2009-03-09 01:21:12 -06004197 default:
4198 desc_ioc_state = "unknown";
4199 break;
4200 }
4201
4202 switch (scsi_status) {
4203 case MPI2_SCSI_STATUS_GOOD:
4204 desc_scsi_status = "good";
4205 break;
4206 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4207 desc_scsi_status = "check condition";
4208 break;
4209 case MPI2_SCSI_STATUS_CONDITION_MET:
4210 desc_scsi_status = "condition met";
4211 break;
4212 case MPI2_SCSI_STATUS_BUSY:
4213 desc_scsi_status = "busy";
4214 break;
4215 case MPI2_SCSI_STATUS_INTERMEDIATE:
4216 desc_scsi_status = "intermediate";
4217 break;
4218 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4219 desc_scsi_status = "intermediate condmet";
4220 break;
4221 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4222 desc_scsi_status = "reservation conflict";
4223 break;
4224 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4225 desc_scsi_status = "command terminated";
4226 break;
4227 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4228 desc_scsi_status = "task set full";
4229 break;
4230 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4231 desc_scsi_status = "aca active";
4232 break;
4233 case MPI2_SCSI_STATUS_TASK_ABORTED:
4234 desc_scsi_status = "task aborted";
4235 break;
4236 default:
4237 desc_scsi_status = "unknown";
4238 break;
4239 }
4240
4241 desc_scsi_state[0] = '\0';
4242 if (!scsi_state)
4243 desc_scsi_state = " ";
4244 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4245 strcat(desc_scsi_state, "response info ");
4246 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4247 strcat(desc_scsi_state, "state terminated ");
4248 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4249 strcat(desc_scsi_state, "no status ");
4250 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4251 strcat(desc_scsi_state, "autosense failed ");
4252 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4253 strcat(desc_scsi_state, "autosense valid ");
4254
4255 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304256
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304257 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304258 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4259 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304260 } else {
4261 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4262 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4263 priv_target->sas_address);
4264 if (sas_device) {
4265 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4266 "phy(%d)\n", ioc->name, sas_device->sas_address,
4267 sas_device->phy);
4268 printk(MPT2SAS_WARN_FMT
4269 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4270 ioc->name, sas_device->enclosure_logical_id,
4271 sas_device->slot);
4272 }
4273 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304274 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304275
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304276 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4277 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4278 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004279 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4280 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4281 scsi_get_resid(scmd));
4282 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4283 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4284 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4285 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4286 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4287 scsi_status, desc_scsi_state, scsi_state);
4288
4289 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4290 struct sense_info data;
4291 _scsih_normalize_sense(scmd->sense_buffer, &data);
4292 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304293 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4294 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004295 }
4296
4297 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4298 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4299 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304300 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004301 }
4302}
4303#endif
4304
4305/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304306 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004307 * @ioc: per adapter object
4308 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304309 * Context: process
4310 *
4311 * Return nothing.
4312 */
4313static void
4314_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4315{
4316 Mpi2SepReply_t mpi_reply;
4317 Mpi2SepRequest_t mpi_request;
4318
4319 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4320 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4321 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4322 mpi_request.SlotStatus =
4323 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4324 mpi_request.DevHandle = cpu_to_le16(handle);
4325 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4326 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4327 &mpi_request)) != 0) {
4328 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4329 __FILE__, __LINE__, __func__);
4330 return;
4331 }
4332
4333 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4334 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4335 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4336 le16_to_cpu(mpi_reply.IOCStatus),
4337 le32_to_cpu(mpi_reply.IOCLogInfo)));
4338 return;
4339 }
4340}
4341
4342/**
4343 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4344 * @ioc: per adapter object
4345 * @handle: device handle
4346 * Context: interrupt.
4347 *
4348 * Return nothing.
4349 */
4350static void
4351_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4352{
4353 struct fw_event_work *fw_event;
4354
4355 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4356 if (!fw_event)
4357 return;
4358 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4359 fw_event->device_handle = handle;
4360 fw_event->ioc = ioc;
4361 _scsih_fw_event_add(ioc, fw_event);
4362}
4363
4364/**
4365 * _scsih_smart_predicted_fault - process smart errors
4366 * @ioc: per adapter object
4367 * @handle: device handle
4368 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004369 *
4370 * Return nothing.
4371 */
4372static void
4373_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4374{
Eric Moore635374e2009-03-09 01:21:12 -06004375 struct scsi_target *starget;
4376 struct MPT2SAS_TARGET *sas_target_priv_data;
4377 Mpi2EventNotificationReply_t *event_reply;
4378 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4379 struct _sas_device *sas_device;
4380 ssize_t sz;
4381 unsigned long flags;
4382
4383 /* only handle non-raid devices */
4384 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4385 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4386 if (!sas_device) {
4387 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4388 return;
4389 }
4390 starget = sas_device->starget;
4391 sas_target_priv_data = starget->hostdata;
4392
4393 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4394 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4395 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4396 return;
4397 }
4398 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4399 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4400
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304401 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4402 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004403
4404 /* insert into event log */
4405 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4406 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b42011-11-07 22:05:21 +11004407 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004408 if (!event_reply) {
4409 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4410 ioc->name, __FILE__, __LINE__, __func__);
4411 return;
4412 }
4413
4414 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4415 event_reply->Event =
4416 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4417 event_reply->MsgLength = sz/4;
4418 event_reply->EventDataLength =
4419 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4420 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4421 event_reply->EventData;
4422 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4423 event_data->ASC = 0x5D;
4424 event_data->DevHandle = cpu_to_le16(handle);
4425 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4426 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4427 kfree(event_reply);
4428}
4429
4430/**
Eric Moored5d135b2009-05-18 13:02:08 -06004431 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004432 * @ioc: per adapter object
4433 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304434 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004435 * @reply: reply message frame(lower 32bit addr)
4436 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304437 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004438 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304439 * Return 1 meaning mf should be freed from _base_interrupt
4440 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004441 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304442static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304443_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004444{
4445 Mpi2SCSIIORequest_t *mpi_request;
4446 Mpi2SCSIIOReply_t *mpi_reply;
4447 struct scsi_cmnd *scmd;
4448 u16 ioc_status;
4449 u32 xfer_cnt;
4450 u8 scsi_state;
4451 u8 scsi_status;
4452 u32 log_info;
4453 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304454 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304455 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004456
4457 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304458 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004459 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304460 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004461
4462 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4463
4464 if (mpi_reply == NULL) {
4465 scmd->result = DID_OK << 16;
4466 goto out;
4467 }
4468
4469 sas_device_priv_data = scmd->device->hostdata;
4470 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4471 sas_device_priv_data->sas_target->deleted) {
4472 scmd->result = DID_NO_CONNECT << 16;
4473 goto out;
4474 }
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304475 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304476 /*
4477 * WARPDRIVE: If direct_io is set then it is directIO,
4478 * the failed direct I/O should be redirected to volume
4479 */
nagalakshmi.nandigama@lsi.com4da7af92011-12-01 07:53:02 +05304480 if (_scsih_scsi_direct_io_get(ioc, smid) &&
4481 ((ioc_status & MPI2_IOCSTATUS_MASK)
4482 != MPI2_IOCSTATUS_SCSI_TASK_TERMINATED)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304483 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4484 ioc->scsi_lookup[smid - 1].scmd = scmd;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304485 _scsih_scsi_direct_io_set(ioc, smid, 0);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05304486 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304487 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4488 mpi_request->DevHandle =
4489 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4490 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4491 sas_device_priv_data->sas_target->handle);
4492 return 0;
4493 }
4494
Eric Moore635374e2009-03-09 01:21:12 -06004495
4496 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304497 scsi_state = mpi_reply->SCSIState;
4498 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4499 response_code =
4500 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004501 if (!sas_device_priv_data->tlr_snoop_check) {
4502 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304503 /* Make sure Device is not raid volume.
4504 * We do not expose raid functionality to upper layer for warpdrive.
4505 */
4506 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304507 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304508 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4509 sas_disable_tlr(scmd->device);
4510 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4511 }
Eric Moore635374e2009-03-09 01:21:12 -06004512 }
4513
4514 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4515 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
Eric Moore635374e2009-03-09 01:21:12 -06004516 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4517 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4518 else
4519 log_info = 0;
4520 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004521 scsi_status = mpi_reply->SCSIStatus;
4522
4523 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4524 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4525 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4526 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4527 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4528 }
4529
4530 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4531 struct sense_info data;
4532 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4533 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004534 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004535 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004536 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004537 _scsih_normalize_sense(scmd->sense_buffer, &data);
4538 /* failure prediction threshold exceeded */
4539 if (data.asc == 0x5D)
4540 _scsih_smart_predicted_fault(ioc,
4541 le16_to_cpu(mpi_reply->DevHandle));
4542 }
4543
4544 switch (ioc_status) {
4545 case MPI2_IOCSTATUS_BUSY:
4546 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4547 scmd->result = SAM_STAT_BUSY;
4548 break;
4549
4550 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4551 scmd->result = DID_NO_CONNECT << 16;
4552 break;
4553
4554 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4555 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304556 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4557 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004558 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304559 scmd->result = DID_SOFT_ERROR << 16;
4560 break;
Eric Moore635374e2009-03-09 01:21:12 -06004561 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4562 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4563 scmd->result = DID_RESET << 16;
4564 break;
4565
4566 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4567 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4568 scmd->result = DID_SOFT_ERROR << 16;
4569 else
4570 scmd->result = (DID_OK << 16) | scsi_status;
4571 break;
4572
4573 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4574 scmd->result = (DID_OK << 16) | scsi_status;
4575
4576 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4577 break;
4578
4579 if (xfer_cnt < scmd->underflow) {
4580 if (scsi_status == SAM_STAT_BUSY)
4581 scmd->result = SAM_STAT_BUSY;
4582 else
4583 scmd->result = DID_SOFT_ERROR << 16;
4584 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4585 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4586 scmd->result = DID_SOFT_ERROR << 16;
4587 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4588 scmd->result = DID_RESET << 16;
4589 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4590 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4591 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4592 scmd->result = (DRIVER_SENSE << 24) |
4593 SAM_STAT_CHECK_CONDITION;
4594 scmd->sense_buffer[0] = 0x70;
4595 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4596 scmd->sense_buffer[12] = 0x20;
4597 scmd->sense_buffer[13] = 0;
4598 }
4599 break;
4600
4601 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4602 scsi_set_resid(scmd, 0);
4603 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4604 case MPI2_IOCSTATUS_SUCCESS:
4605 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304606 if (response_code ==
4607 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4608 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4609 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004610 scmd->result = DID_SOFT_ERROR << 16;
4611 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4612 scmd->result = DID_RESET << 16;
4613 break;
4614
Eric Moore3c621b32009-05-18 12:59:41 -06004615 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4616 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4617 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4618 _scsih_eedp_error_handling(scmd, ioc_status);
4619 break;
Eric Moore635374e2009-03-09 01:21:12 -06004620 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4621 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4622 case MPI2_IOCSTATUS_INVALID_SGL:
4623 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4624 case MPI2_IOCSTATUS_INVALID_FIELD:
4625 case MPI2_IOCSTATUS_INVALID_STATE:
4626 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4627 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4628 default:
4629 scmd->result = DID_SOFT_ERROR << 16;
4630 break;
4631
4632 }
4633
4634#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4635 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4636 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4637#endif
4638
4639 out:
4640 scsi_dma_unmap(scmd);
4641 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304642 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004643}
4644
4645/**
Eric Moore635374e2009-03-09 01:21:12 -06004646 * _scsih_sas_host_refresh - refreshing sas host object contents
4647 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004648 * Context: user
4649 *
4650 * During port enable, fw will send topology events for every device. Its
4651 * possible that the handles may change from the previous setting, so this
4652 * code keeping handles updating if changed.
4653 *
4654 * Return nothing.
4655 */
4656static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304657_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004658{
4659 u16 sz;
4660 u16 ioc_status;
4661 int i;
4662 Mpi2ConfigReply_t mpi_reply;
4663 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304664 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304665 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004666
4667 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4668 "updating handles for sas_host(0x%016llx)\n",
4669 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4670
4671 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4672 * sizeof(Mpi2SasIOUnit0PhyData_t));
4673 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4674 if (!sas_iounit_pg0) {
4675 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4676 ioc->name, __FILE__, __LINE__, __func__);
4677 return;
4678 }
Eric Moore635374e2009-03-09 01:21:12 -06004679
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304680 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4681 sas_iounit_pg0, sz)) != 0)
4682 goto out;
4683 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4684 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4685 goto out;
4686 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304687 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304688 if (i == 0)
4689 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4690 PhyData[0].ControllerDevHandle);
4691 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4692 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4693 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304694 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4695 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304696 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304697 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304698 }
Eric Moore635374e2009-03-09 01:21:12 -06004699 out:
4700 kfree(sas_iounit_pg0);
4701}
4702
4703/**
4704 * _scsih_sas_host_add - create sas host object
4705 * @ioc: per adapter object
4706 *
4707 * Creating host side data object, stored in ioc->sas_hba
4708 *
4709 * Return nothing.
4710 */
4711static void
4712_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4713{
4714 int i;
4715 Mpi2ConfigReply_t mpi_reply;
4716 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4717 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4718 Mpi2SasPhyPage0_t phy_pg0;
4719 Mpi2SasDevicePage0_t sas_device_pg0;
4720 Mpi2SasEnclosurePage0_t enclosure_pg0;
4721 u16 ioc_status;
4722 u16 sz;
4723 u16 device_missing_delay;
4724
4725 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4726 if (!ioc->sas_hba.num_phys) {
4727 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4728 ioc->name, __FILE__, __LINE__, __func__);
4729 return;
4730 }
4731
4732 /* sas_iounit page 0 */
4733 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4734 sizeof(Mpi2SasIOUnit0PhyData_t));
4735 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4736 if (!sas_iounit_pg0) {
4737 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4738 ioc->name, __FILE__, __LINE__, __func__);
4739 return;
4740 }
4741 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4742 sas_iounit_pg0, sz))) {
4743 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4744 ioc->name, __FILE__, __LINE__, __func__);
4745 goto out;
4746 }
4747 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4748 MPI2_IOCSTATUS_MASK;
4749 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4750 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4751 ioc->name, __FILE__, __LINE__, __func__);
4752 goto out;
4753 }
4754
4755 /* sas_iounit page 1 */
4756 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4757 sizeof(Mpi2SasIOUnit1PhyData_t));
4758 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4759 if (!sas_iounit_pg1) {
4760 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4761 ioc->name, __FILE__, __LINE__, __func__);
4762 goto out;
4763 }
4764 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4765 sas_iounit_pg1, sz))) {
4766 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4767 ioc->name, __FILE__, __LINE__, __func__);
4768 goto out;
4769 }
4770 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4771 MPI2_IOCSTATUS_MASK;
4772 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4773 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4774 ioc->name, __FILE__, __LINE__, __func__);
4775 goto out;
4776 }
4777
4778 ioc->io_missing_delay =
4779 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4780 device_missing_delay =
4781 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4782 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4783 ioc->device_missing_delay = (device_missing_delay &
4784 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4785 else
4786 ioc->device_missing_delay = device_missing_delay &
4787 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4788
4789 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4790 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4791 sizeof(struct _sas_phy), GFP_KERNEL);
4792 if (!ioc->sas_hba.phy) {
4793 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4794 ioc->name, __FILE__, __LINE__, __func__);
4795 goto out;
4796 }
4797 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4798 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4799 i))) {
4800 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4801 ioc->name, __FILE__, __LINE__, __func__);
4802 goto out;
4803 }
4804 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4805 MPI2_IOCSTATUS_MASK;
4806 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4807 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4808 ioc->name, __FILE__, __LINE__, __func__);
4809 goto out;
4810 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304811
4812 if (i == 0)
4813 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4814 PhyData[0].ControllerDevHandle);
4815 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004816 ioc->sas_hba.phy[i].phy_id = i;
4817 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4818 phy_pg0, ioc->sas_hba.parent_dev);
4819 }
4820 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304821 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004822 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4823 ioc->name, __FILE__, __LINE__, __func__);
4824 goto out;
4825 }
Eric Moore635374e2009-03-09 01:21:12 -06004826 ioc->sas_hba.enclosure_handle =
4827 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4828 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4829 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4830 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4831 (unsigned long long) ioc->sas_hba.sas_address,
4832 ioc->sas_hba.num_phys) ;
4833
4834 if (ioc->sas_hba.enclosure_handle) {
4835 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4836 &enclosure_pg0,
4837 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4838 ioc->sas_hba.enclosure_handle))) {
4839 ioc->sas_hba.enclosure_logical_id =
4840 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4841 }
4842 }
4843
4844 out:
4845 kfree(sas_iounit_pg1);
4846 kfree(sas_iounit_pg0);
4847}
4848
4849/**
4850 * _scsih_expander_add - creating expander object
4851 * @ioc: per adapter object
4852 * @handle: expander handle
4853 *
4854 * Creating expander object, stored in ioc->sas_expander_list.
4855 *
4856 * Return 0 for success, else error.
4857 */
4858static int
4859_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4860{
4861 struct _sas_node *sas_expander;
4862 Mpi2ConfigReply_t mpi_reply;
4863 Mpi2ExpanderPage0_t expander_pg0;
4864 Mpi2ExpanderPage1_t expander_pg1;
4865 Mpi2SasEnclosurePage0_t enclosure_pg0;
4866 u32 ioc_status;
4867 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304868 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004869 int i;
4870 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304871 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004872 int rc = 0;
4873
4874 if (!handle)
4875 return -1;
4876
Eric Moore3cb54692010-07-08 14:44:34 -06004877 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304878 return -1;
4879
Eric Moore635374e2009-03-09 01:21:12 -06004880 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4881 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4882 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4883 ioc->name, __FILE__, __LINE__, __func__);
4884 return -1;
4885 }
4886
4887 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4888 MPI2_IOCSTATUS_MASK;
4889 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4890 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4891 ioc->name, __FILE__, __LINE__, __func__);
4892 return -1;
4893 }
4894
4895 /* handle out of order topology events */
4896 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304897 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4898 != 0) {
4899 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4900 ioc->name, __FILE__, __LINE__, __func__);
4901 return -1;
4902 }
4903 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004904 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304905 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4906 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004907 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4908 if (!sas_expander) {
4909 rc = _scsih_expander_add(ioc, parent_handle);
4910 if (rc != 0)
4911 return rc;
4912 }
4913 }
4914
Eric Moore635374e2009-03-09 01:21:12 -06004915 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304916 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004917 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4918 sas_address);
4919 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4920
4921 if (sas_expander)
4922 return 0;
4923
4924 sas_expander = kzalloc(sizeof(struct _sas_node),
4925 GFP_KERNEL);
4926 if (!sas_expander) {
4927 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4928 ioc->name, __FILE__, __LINE__, __func__);
4929 return -1;
4930 }
4931
4932 sas_expander->handle = handle;
4933 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304934 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004935 sas_expander->sas_address = sas_address;
4936
4937 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4938 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304939 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004940 sas_expander->sas_address, sas_expander->num_phys);
4941
4942 if (!sas_expander->num_phys)
4943 goto out_fail;
4944 sas_expander->phy = kcalloc(sas_expander->num_phys,
4945 sizeof(struct _sas_phy), GFP_KERNEL);
4946 if (!sas_expander->phy) {
4947 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4948 ioc->name, __FILE__, __LINE__, __func__);
4949 rc = -1;
4950 goto out_fail;
4951 }
4952
4953 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4954 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304955 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004956 if (!mpt2sas_port) {
4957 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4958 ioc->name, __FILE__, __LINE__, __func__);
4959 rc = -1;
4960 goto out_fail;
4961 }
4962 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4963
4964 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4965 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4966 &expander_pg1, i, handle))) {
4967 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4968 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304969 rc = -1;
4970 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004971 }
4972 sas_expander->phy[i].handle = handle;
4973 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304974
4975 if ((mpt2sas_transport_add_expander_phy(ioc,
4976 &sas_expander->phy[i], expander_pg1,
4977 sas_expander->parent_dev))) {
4978 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4979 ioc->name, __FILE__, __LINE__, __func__);
4980 rc = -1;
4981 goto out_fail;
4982 }
Eric Moore635374e2009-03-09 01:21:12 -06004983 }
4984
4985 if (sas_expander->enclosure_handle) {
4986 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4987 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4988 sas_expander->enclosure_handle))) {
4989 sas_expander->enclosure_logical_id =
4990 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4991 }
4992 }
4993
4994 _scsih_expander_node_add(ioc, sas_expander);
4995 return 0;
4996
4997 out_fail:
4998
Kashyap, Desai20f58952009-08-07 19:34:26 +05304999 if (mpt2sas_port)
5000 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305001 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06005002 kfree(sas_expander);
5003 return rc;
5004}
5005
5006/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05305007 * _scsih_done - scsih callback handler.
5008 * @ioc: per adapter object
5009 * @smid: system request message index
5010 * @msix_index: MSIX table index supplied by the OS
5011 * @reply: reply message frame(lower 32bit addr)
5012 *
5013 * Callback handler when sending internal generated message frames.
5014 * The callback index passed is `ioc->scsih_cb_idx`
5015 *
5016 * Return 1 meaning mf should be freed from _base_interrupt
5017 * 0 means the mf is freed from this function.
5018 */
5019static u8
5020_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
5021{
5022 MPI2DefaultReply_t *mpi_reply;
5023
5024 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
5025 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
5026 return 1;
5027 if (ioc->scsih_cmds.smid != smid)
5028 return 1;
5029 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
5030 if (mpi_reply) {
5031 memcpy(ioc->scsih_cmds.reply, mpi_reply,
5032 mpi_reply->MsgLength*4);
5033 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
5034 }
5035 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
5036 complete(&ioc->scsih_cmds.done);
5037 return 1;
5038}
5039
5040/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305041 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06005042 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305043 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06005044 *
5045 * Return nothing.
5046 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305047void
5048mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06005049{
5050 struct _sas_node *sas_expander;
5051 unsigned long flags;
5052
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305053 if (ioc->shost_recovery)
5054 return;
5055
Eric Moore635374e2009-03-09 01:21:12 -06005056 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305057 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5058 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305059 if (sas_expander)
5060 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06005061 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305062 if (sas_expander)
5063 _scsih_expander_node_remove(ioc, sas_expander);
Eric Moore635374e2009-03-09 01:21:12 -06005064}
5065
5066/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05305067 * _scsih_check_access_status - check access flags
5068 * @ioc: per adapter object
5069 * @sas_address: sas address
5070 * @handle: sas device handle
5071 * @access_flags: errors returned during discovery of the device
5072 *
5073 * Return 0 for success, else failure
5074 */
5075static u8
5076_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5077 u16 handle, u8 access_status)
5078{
5079 u8 rc = 1;
5080 char *desc = NULL;
5081
5082 switch (access_status) {
5083 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5084 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5085 rc = 0;
5086 break;
5087 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5088 desc = "sata capability failed";
5089 break;
5090 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5091 desc = "sata affiliation conflict";
5092 break;
5093 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5094 desc = "route not addressable";
5095 break;
5096 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5097 desc = "smp error not addressable";
5098 break;
5099 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5100 desc = "device blocked";
5101 break;
5102 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5103 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5104 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5105 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5106 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5107 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5108 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5109 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5110 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5111 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5112 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5113 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5114 desc = "sata initialization failed";
5115 break;
5116 default:
5117 desc = "unknown";
5118 break;
5119 }
5120
5121 if (!rc)
5122 return 0;
5123
5124 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5125 "handle(0x%04x)\n", ioc->name, desc,
5126 (unsigned long long)sas_address, handle);
5127 return rc;
5128}
5129
5130static void
5131_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5132{
5133 Mpi2ConfigReply_t mpi_reply;
5134 Mpi2SasDevicePage0_t sas_device_pg0;
5135 struct _sas_device *sas_device;
5136 u32 ioc_status;
5137 unsigned long flags;
5138 u64 sas_address;
5139 struct scsi_target *starget;
5140 struct MPT2SAS_TARGET *sas_target_priv_data;
5141 u32 device_info;
5142
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305143
Kashyap, Desaib4344272010-03-17 16:24:14 +05305144 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5145 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5146 return;
5147
5148 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5149 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5150 return;
5151
5152 /* check if this is end device */
5153 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5154 if (!(_scsih_is_end_device(device_info)))
5155 return;
5156
5157 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5158 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5159 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5160 sas_address);
5161
5162 if (!sas_device) {
5163 printk(MPT2SAS_ERR_FMT "device is not present "
5164 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5165 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5166 return;
5167 }
5168
5169 if (unlikely(sas_device->handle != handle)) {
5170 starget = sas_device->starget;
5171 sas_target_priv_data = starget->hostdata;
5172 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5173 " to (0x%04x)!!!\n", sas_device->handle, handle);
5174 sas_target_priv_data->handle = handle;
5175 sas_device->handle = handle;
5176 }
Kashyap, Desaib4344272010-03-17 16:24:14 +05305177
5178 /* check if device is present */
5179 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5180 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5181 printk(MPT2SAS_ERR_FMT "device is not present "
5182 "handle(0x%04x), flags!!!\n", ioc->name, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305183 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305184 return;
5185 }
5186
5187 /* check if there were any issues with discovery */
5188 if (_scsih_check_access_status(ioc, sas_address, handle,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305189 sas_device_pg0.AccessStatus)) {
5190 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305191 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305192 }
5193 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305194 _scsih_ublock_io_device(ioc, sas_address);
Kashyap, Desaib4344272010-03-17 16:24:14 +05305195
5196}
5197
5198/**
Eric Moore635374e2009-03-09 01:21:12 -06005199 * _scsih_add_device - creating sas device object
5200 * @ioc: per adapter object
5201 * @handle: sas device handle
5202 * @phy_num: phy number end device attached to
5203 * @is_pd: is this hidden raid component
5204 *
5205 * Creating end device object, stored in ioc->sas_device_list.
5206 *
5207 * Returns 0 for success, non-zero for failure.
5208 */
5209static int
5210_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5211{
5212 Mpi2ConfigReply_t mpi_reply;
5213 Mpi2SasDevicePage0_t sas_device_pg0;
5214 Mpi2SasEnclosurePage0_t enclosure_pg0;
5215 struct _sas_device *sas_device;
5216 u32 ioc_status;
5217 __le64 sas_address;
5218 u32 device_info;
5219 unsigned long flags;
5220
5221 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5222 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5223 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5224 ioc->name, __FILE__, __LINE__, __func__);
5225 return -1;
5226 }
5227
5228 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5229 MPI2_IOCSTATUS_MASK;
5230 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5231 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5232 ioc->name, __FILE__, __LINE__, __func__);
5233 return -1;
5234 }
5235
Kashyap, Desaib4344272010-03-17 16:24:14 +05305236 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5237
Eric Moore635374e2009-03-09 01:21:12 -06005238 /* check if device is present */
5239 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5240 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5241 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5242 ioc->name, __FILE__, __LINE__, __func__);
5243 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5244 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5245 return -1;
5246 }
5247
Kashyap, Desaib4344272010-03-17 16:24:14 +05305248 /* check if there were any issues with discovery */
5249 if (_scsih_check_access_status(ioc, sas_address, handle,
5250 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005251 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005252
5253 /* check if this is end device */
5254 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5255 if (!(_scsih_is_end_device(device_info))) {
5256 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5257 ioc->name, __FILE__, __LINE__, __func__);
5258 return -1;
5259 }
5260
Eric Moore635374e2009-03-09 01:21:12 -06005261
5262 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5263 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5264 sas_address);
5265 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5266
Kashyap, Desaib4344272010-03-17 16:24:14 +05305267 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005268 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005269
5270 sas_device = kzalloc(sizeof(struct _sas_device),
5271 GFP_KERNEL);
5272 if (!sas_device) {
5273 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5274 ioc->name, __FILE__, __LINE__, __func__);
5275 return -1;
5276 }
5277
5278 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305279 if (_scsih_get_sas_address(ioc, le16_to_cpu
5280 (sas_device_pg0.ParentDevHandle),
5281 &sas_device->sas_address_parent) != 0)
5282 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5283 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005284 sas_device->enclosure_handle =
5285 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5286 sas_device->slot =
5287 le16_to_cpu(sas_device_pg0.Slot);
5288 sas_device->device_info = device_info;
5289 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305290 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005291
5292 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305293 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5294 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5295 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005296 sas_device->enclosure_logical_id =
5297 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005298
5299 /* get device name */
5300 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5301
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305302 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005303 _scsih_sas_device_init_add(ioc, sas_device);
5304 else
5305 _scsih_sas_device_add(ioc, sas_device);
5306
5307 return 0;
5308}
5309
5310/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305311 * _scsih_remove_device - removing sas device object
5312 * @ioc: per adapter object
5313 * @sas_device_delete: the sas_device object
5314 *
5315 * Return nothing.
5316 */
5317static void
5318_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5319 struct _sas_device *sas_device)
5320{
Kashyap, Desai1278b112010-03-09 17:34:13 +05305321 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305322
Kashyap, Desai1278b112010-03-09 17:34:13 +05305323 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5324 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305325 sas_device->handle, (unsigned long long)
5326 sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05305327
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305328 if (sas_device->starget && sas_device->starget->hostdata) {
5329 sas_target_priv_data = sas_device->starget->hostdata;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305330 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05305331 _scsih_ublock_io_device(ioc, sas_device->sas_address);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305332 sas_target_priv_data->handle =
5333 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305334 }
5335
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305336 if (!ioc->hide_drives)
5337 mpt2sas_transport_port_remove(ioc,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305338 sas_device->sas_address,
5339 sas_device->sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305340
5341 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305342 "(0x%016llx)\n", ioc->name, sas_device->handle,
5343 (unsigned long long) sas_device->sas_address);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305344
5345 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5346 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305347 sas_device->handle, (unsigned long long)
5348 sas_device->sas_address));
5349 kfree(sas_device);
5350}
5351/**
5352 * _scsih_device_remove_by_handle - removing device object by handle
5353 * @ioc: per adapter object
5354 * @handle: device handle
5355 *
5356 * Return nothing.
5357 */
5358static void
5359_scsih_device_remove_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5360{
5361 struct _sas_device *sas_device;
5362 unsigned long flags;
5363
5364 if (ioc->shost_recovery)
5365 return;
5366
5367 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5368 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5369 if (sas_device)
5370 list_del(&sas_device->list);
5371 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5372 if (sas_device)
5373 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005374}
5375
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305376/**
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305377 * mpt2sas_device_remove_by_sas_address - removing device object by sas address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305378 * @ioc: per adapter object
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305379 * @sas_address: device sas_address
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305380 *
5381 * Return nothing.
5382 */
5383void
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305384mpt2sas_device_remove_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
5385 u64 sas_address)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305386{
5387 struct _sas_device *sas_device;
5388 unsigned long flags;
5389
5390 if (ioc->shost_recovery)
5391 return;
5392
5393 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5394 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5395 sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305396 if (sas_device)
5397 list_del(&sas_device->list);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305398 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305399 if (sas_device)
5400 _scsih_remove_device(ioc, sas_device);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305401}
Eric Moore635374e2009-03-09 01:21:12 -06005402#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5403/**
5404 * _scsih_sas_topology_change_event_debug - debug for topology event
5405 * @ioc: per adapter object
5406 * @event_data: event data payload
5407 * Context: user.
5408 */
5409static void
5410_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5411 Mpi2EventDataSasTopologyChangeList_t *event_data)
5412{
5413 int i;
5414 u16 handle;
5415 u16 reason_code;
5416 u8 phy_number;
5417 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305418 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005419
5420 switch (event_data->ExpStatus) {
5421 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5422 status_str = "add";
5423 break;
5424 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5425 status_str = "remove";
5426 break;
5427 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305428 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005429 status_str = "responding";
5430 break;
5431 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5432 status_str = "remove delay";
5433 break;
5434 default:
5435 status_str = "unknown status";
5436 break;
5437 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305438 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005439 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305440 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005441 "start_phy(%02d), count(%d)\n",
5442 le16_to_cpu(event_data->ExpanderDevHandle),
5443 le16_to_cpu(event_data->EnclosureHandle),
5444 event_data->StartPhyNum, event_data->NumEntries);
5445 for (i = 0; i < event_data->NumEntries; i++) {
5446 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5447 if (!handle)
5448 continue;
5449 phy_number = event_data->StartPhyNum + i;
5450 reason_code = event_data->PHY[i].PhyStatus &
5451 MPI2_EVENT_SAS_TOPO_RC_MASK;
5452 switch (reason_code) {
5453 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305454 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005455 break;
5456 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305457 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005458 break;
5459 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305460 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005461 break;
5462 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305463 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005464 break;
5465 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305466 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005467 break;
5468 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305469 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005470 break;
5471 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305472 link_rate = event_data->PHY[i].LinkRate >> 4;
5473 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305474 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305475 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5476 handle, status_str, link_rate, prev_link_rate);
5477
Eric Moore635374e2009-03-09 01:21:12 -06005478 }
5479}
5480#endif
5481
5482/**
5483 * _scsih_sas_topology_change_event - handle topology changes
5484 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305485 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005486 * Context: user.
5487 *
5488 */
5489static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305490_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005491 struct fw_event_work *fw_event)
5492{
5493 int i;
5494 u16 parent_handle, handle;
5495 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305496 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005497 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305498 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005499 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305500 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305501 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005502
5503#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5504 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5505 _scsih_sas_topology_change_event_debug(ioc, event_data);
5506#endif
5507
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305508 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305509 return;
5510
Eric Moore635374e2009-03-09 01:21:12 -06005511 if (!ioc->sas_hba.num_phys)
5512 _scsih_sas_host_add(ioc);
5513 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305514 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005515
5516 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305517 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005518 "event\n", ioc->name));
5519 return;
5520 }
5521
5522 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5523
5524 /* handle expander add */
5525 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5526 if (_scsih_expander_add(ioc, parent_handle) != 0)
5527 return;
5528
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305529 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5530 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5531 parent_handle);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305532 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305533 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305534 max_phys = sas_expander->num_phys;
5535 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305536 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305537 max_phys = ioc->sas_hba.num_phys;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305538 } else {
5539 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305540 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305541 }
5542 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305543
Eric Moore635374e2009-03-09 01:21:12 -06005544 /* handle siblings events */
5545 for (i = 0; i < event_data->NumEntries; i++) {
5546 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305547 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005548 "expander event\n", ioc->name));
5549 return;
5550 }
Eric Moore3cb54692010-07-08 14:44:34 -06005551 if (ioc->shost_recovery || ioc->remove_host ||
5552 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305553 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305554 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305555 if (phy_number >= max_phys)
5556 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305557 reason_code = event_data->PHY[i].PhyStatus &
5558 MPI2_EVENT_SAS_TOPO_RC_MASK;
5559 if ((event_data->PHY[i].PhyStatus &
5560 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5561 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005562 continue;
5563 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5564 if (!handle)
5565 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305566 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305567 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005568 switch (reason_code) {
5569 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305570
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305571 if (ioc->shost_recovery)
5572 break;
5573
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305574 if (link_rate == prev_link_rate)
5575 break;
5576
5577 mpt2sas_transport_update_links(ioc, sas_address,
5578 handle, phy_number, link_rate);
5579
Kashyap, Desaib4344272010-03-17 16:24:14 +05305580 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5581 break;
5582
5583 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305584 break;
Eric Moore635374e2009-03-09 01:21:12 -06005585 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305586
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305587 if (ioc->shost_recovery)
5588 break;
5589
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305590 mpt2sas_transport_update_links(ioc, sas_address,
5591 handle, phy_number, link_rate);
5592
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305593 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005594 break;
5595 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305596
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305597 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06005598 break;
5599 }
5600 }
5601
5602 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305603 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5604 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305605 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005606
5607}
5608
5609#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5610/**
5611 * _scsih_sas_device_status_change_event_debug - debug for device event
5612 * @event_data: event data payload
5613 * Context: user.
5614 *
5615 * Return nothing.
5616 */
5617static void
5618_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5619 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5620{
5621 char *reason_str = NULL;
5622
5623 switch (event_data->ReasonCode) {
5624 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5625 reason_str = "smart data";
5626 break;
5627 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5628 reason_str = "unsupported device discovered";
5629 break;
5630 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5631 reason_str = "internal device reset";
5632 break;
5633 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5634 reason_str = "internal task abort";
5635 break;
5636 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5637 reason_str = "internal task abort set";
5638 break;
5639 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5640 reason_str = "internal clear task set";
5641 break;
5642 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5643 reason_str = "internal query task";
5644 break;
5645 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5646 reason_str = "sata init failure";
5647 break;
5648 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5649 reason_str = "internal device reset complete";
5650 break;
5651 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5652 reason_str = "internal task abort complete";
5653 break;
5654 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5655 reason_str = "internal async notification";
5656 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305657 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5658 reason_str = "expander reduced functionality";
5659 break;
5660 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5661 reason_str = "expander reduced functionality complete";
5662 break;
Eric Moore635374e2009-03-09 01:21:12 -06005663 default:
5664 reason_str = "unknown reason";
5665 break;
5666 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305667 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305668 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5669 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5670 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5671 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005672 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305673 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005674 event_data->ASC, event_data->ASCQ);
5675 printk(KERN_INFO "\n");
5676}
5677#endif
5678
5679/**
5680 * _scsih_sas_device_status_change_event - handle device status change
5681 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305682 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005683 * Context: user.
5684 *
5685 * Return nothing.
5686 */
5687static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305688_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5689 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005690{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305691 struct MPT2SAS_TARGET *target_priv_data;
5692 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305693 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305694 unsigned long flags;
5695 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5696 fw_event->event_data;
5697
Eric Moore635374e2009-03-09 01:21:12 -06005698#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5699 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305700 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305701 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005702#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305703
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305704 /* In MPI Revision K (0xC), the internal device reset complete was
5705 * implemented, so avoid setting tm_busy flag for older firmware.
5706 */
5707 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5708 return;
5709
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305710 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305711 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305712 event_data->ReasonCode !=
5713 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305714 return;
5715
5716 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5717 sas_address = le64_to_cpu(event_data->SASAddress);
5718 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5719 sas_address);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305720
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305721 if (!sas_device || !sas_device->starget) {
5722 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305723 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305724 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305725
5726 target_priv_data = sas_device->starget->hostdata;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305727 if (!target_priv_data) {
5728 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305729 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305730 }
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305731
5732 if (event_data->ReasonCode ==
5733 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5734 target_priv_data->tm_busy = 1;
5735 else
5736 target_priv_data->tm_busy = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05305737 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005738}
5739
5740#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5741/**
5742 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5743 * @ioc: per adapter object
5744 * @event_data: event data payload
5745 * Context: user.
5746 *
5747 * Return nothing.
5748 */
5749static void
5750_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5751 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5752{
5753 char *reason_str = NULL;
5754
5755 switch (event_data->ReasonCode) {
5756 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5757 reason_str = "enclosure add";
5758 break;
5759 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5760 reason_str = "enclosure remove";
5761 break;
5762 default:
5763 reason_str = "unknown reason";
5764 break;
5765 }
5766
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305767 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005768 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5769 " number slots(%d)\n", ioc->name, reason_str,
5770 le16_to_cpu(event_data->EnclosureHandle),
5771 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5772 le16_to_cpu(event_data->StartSlot));
5773}
5774#endif
5775
5776/**
5777 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5778 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305779 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005780 * Context: user.
5781 *
5782 * Return nothing.
5783 */
5784static void
5785_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305786 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005787{
5788#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5789 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5790 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305791 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005792#endif
5793}
5794
5795/**
Andrew Mortona78e21d2012-02-08 12:52:22 -08005796 * _scsih_sas_broadcast_primitive_event - handle broadcast events
Eric Moore635374e2009-03-09 01:21:12 -06005797 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305798 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005799 * Context: user.
5800 *
5801 * Return nothing.
5802 */
5803static void
Andrew Mortona78e21d2012-02-08 12:52:22 -08005804_scsih_sas_broadcast_primitive_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305805 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005806{
5807 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305808 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005809 u16 smid, handle;
5810 u32 lun;
5811 struct MPT2SAS_DEVICE *sas_device_priv_data;
5812 u32 termination_count;
5813 u32 query_count;
5814 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305815 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305816 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305817 unsigned long flags;
5818 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305819 u8 max_retries = 0;
5820 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305821
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305822 mutex_lock(&ioc->tm_cmds.mutex);
5823 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5824 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5825 event_data->PortWidth));
5826
5827 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005828
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305829 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305830 mpi_reply = ioc->tm_cmds.reply;
5831broadcast_aen_retry:
5832
5833 /* sanity checks for retrying this loop */
5834 if (max_retries++ == 5) {
5835 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5836 ioc->name, __func__));
5837 goto out;
5838 } else if (max_retries > 1)
5839 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5840 ioc->name, __func__, max_retries - 1));
5841
Eric Moore635374e2009-03-09 01:21:12 -06005842 termination_count = 0;
5843 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305844 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305845 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305846 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005847 scmd = _scsih_scsi_lookup_get(ioc, smid);
5848 if (!scmd)
5849 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305850 sdev = scmd->device;
5851 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005852 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5853 continue;
5854 /* skip hidden raid components */
5855 if (sas_device_priv_data->sas_target->flags &
5856 MPT_TARGET_FLAGS_RAID_COMPONENT)
5857 continue;
5858 /* skip volumes */
5859 if (sas_device_priv_data->sas_target->flags &
5860 MPT_TARGET_FLAGS_VOLUME)
5861 continue;
5862
5863 handle = sas_device_priv_data->sas_target->handle;
5864 lun = sas_device_priv_data->lun;
5865 query_count++;
5866
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305867 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305868 goto out;
5869
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305870 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305871 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5872 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5873 TM_MUTEX_OFF);
5874 if (r == FAILED) {
5875 sdev_printk(KERN_WARNING, sdev,
5876 "mpt2sas_scsih_issue_tm: FAILED when sending "
5877 "QUERY_TASK: scmd(%p)\n", scmd);
5878 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5879 goto broadcast_aen_retry;
5880 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305881 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5882 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305883 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5884 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5885 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5886 scmd);
5887 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5888 goto broadcast_aen_retry;
5889 }
5890
5891 /* see if IO is still owned by IOC and target */
5892 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005893 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5894 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305895 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305896 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005897 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305898 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305899 task_abort_retries = 0;
5900 tm_retry:
5901 if (task_abort_retries++ == 60) {
5902 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5903 "%s: ABORT_TASK: giving up\n", ioc->name,
5904 __func__));
5905 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5906 goto broadcast_aen_retry;
5907 }
5908
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305909 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305910 goto out_no_lock;
5911
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305912 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5913 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305914 scmd->serial_number, TM_MUTEX_OFF);
5915 if (r == FAILED) {
5916 sdev_printk(KERN_WARNING, sdev,
5917 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305918 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305919 goto tm_retry;
5920 }
5921
5922 if (task_abort_retries > 1)
5923 sdev_printk(KERN_WARNING, sdev,
5924 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5925 " scmd(%p)\n",
5926 task_abort_retries - 1, scmd);
5927
Eric Moore635374e2009-03-09 01:21:12 -06005928 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305929 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005930 }
Eric Moore635374e2009-03-09 01:21:12 -06005931
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305932 if (ioc->broadcast_aen_pending) {
5933 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5934 " pending AEN\n", ioc->name, __func__));
5935 ioc->broadcast_aen_pending = 0;
5936 goto broadcast_aen_retry;
5937 }
5938
5939 out:
5940 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5941 out_no_lock:
5942
5943 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005944 "%s - exit, query_count = %d termination_count = %d\n",
5945 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305946
5947 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305948 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305949 _scsih_ublock_io_all_device(ioc);
5950 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005951}
5952
5953/**
5954 * _scsih_sas_discovery_event - handle discovery events
5955 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305956 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005957 * Context: user.
5958 *
5959 * Return nothing.
5960 */
5961static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305962_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5963 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005964{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305965 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5966
Eric Moore635374e2009-03-09 01:21:12 -06005967#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5968 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305969 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005970 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5971 "start" : "stop");
5972 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305973 printk("discovery_status(0x%08x)",
5974 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005975 printk("\n");
5976 }
5977#endif
5978
5979 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305980 !ioc->sas_hba.num_phys) {
5981 if (disable_discovery > 0 && ioc->shost_recovery) {
5982 /* Wait for the reset to complete */
5983 while (ioc->shost_recovery)
5984 ssleep(1);
5985 }
Eric Moore635374e2009-03-09 01:21:12 -06005986 _scsih_sas_host_add(ioc);
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05305987 }
Eric Moore635374e2009-03-09 01:21:12 -06005988}
5989
5990/**
5991 * _scsih_reprobe_lun - reprobing lun
5992 * @sdev: scsi device struct
5993 * @no_uld_attach: sdev->no_uld_attach flag setting
5994 *
5995 **/
5996static void
5997_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5998{
5999 int rc;
6000
6001 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
6002 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
6003 sdev->no_uld_attach ? "hidding" : "exposing");
6004 rc = scsi_device_reprobe(sdev);
6005}
6006
6007/**
Eric Moore635374e2009-03-09 01:21:12 -06006008 * _scsih_sas_volume_add - add new volume
6009 * @ioc: per adapter object
6010 * @element: IR config element data
6011 * Context: user.
6012 *
6013 * Return nothing.
6014 */
6015static void
6016_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
6017 Mpi2EventIrConfigElement_t *element)
6018{
6019 struct _raid_device *raid_device;
6020 unsigned long flags;
6021 u64 wwid;
6022 u16 handle = le16_to_cpu(element->VolDevHandle);
6023 int rc;
6024
Eric Moore635374e2009-03-09 01:21:12 -06006025 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6026 if (!wwid) {
6027 printk(MPT2SAS_ERR_FMT
6028 "failure at %s:%d/%s()!\n", ioc->name,
6029 __FILE__, __LINE__, __func__);
6030 return;
6031 }
6032
6033 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6034 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6035 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6036
6037 if (raid_device)
6038 return;
6039
6040 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6041 if (!raid_device) {
6042 printk(MPT2SAS_ERR_FMT
6043 "failure at %s:%d/%s()!\n", ioc->name,
6044 __FILE__, __LINE__, __func__);
6045 return;
6046 }
6047
6048 raid_device->id = ioc->sas_id++;
6049 raid_device->channel = RAID_CHANNEL;
6050 raid_device->handle = handle;
6051 raid_device->wwid = wwid;
6052 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306053 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06006054 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6055 raid_device->id, 0);
6056 if (rc)
6057 _scsih_raid_device_remove(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306058 } else {
6059 spin_lock_irqsave(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006060 _scsih_determine_boot_device(ioc, raid_device, 1);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306061 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6062 }
Eric Moore635374e2009-03-09 01:21:12 -06006063}
6064
6065/**
6066 * _scsih_sas_volume_delete - delete volume
6067 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306068 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006069 * Context: user.
6070 *
6071 * Return nothing.
6072 */
6073static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306074_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006075{
6076 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006077 unsigned long flags;
6078 struct MPT2SAS_TARGET *sas_target_priv_data;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306079 struct scsi_target *starget = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006080
Eric Moore635374e2009-03-09 01:21:12 -06006081 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6082 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306083 if (raid_device) {
6084 if (raid_device->starget) {
6085 starget = raid_device->starget;
6086 sas_target_priv_data = starget->hostdata;
6087 sas_target_priv_data->deleted = 1;
6088 }
6089 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6090 "(0x%016llx)\n", ioc->name, raid_device->handle,
6091 (unsigned long long) raid_device->wwid);
6092 list_del(&raid_device->list);
6093 kfree(raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006094 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306095 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6096 if (starget)
6097 scsi_remove_target(&starget->dev);
Eric Moore635374e2009-03-09 01:21:12 -06006098}
6099
6100/**
6101 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6102 * @ioc: per adapter object
6103 * @element: IR config element data
6104 * Context: user.
6105 *
6106 * Return nothing.
6107 */
6108static void
6109_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6110 Mpi2EventIrConfigElement_t *element)
6111{
6112 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306113 struct scsi_target *starget = NULL;
6114 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006115 unsigned long flags;
6116 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6117
6118 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6119 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306120 if (sas_device) {
6121 sas_device->volume_handle = 0;
6122 sas_device->volume_wwid = 0;
6123 clear_bit(handle, ioc->pd_handles);
6124 if (sas_device->starget && sas_device->starget->hostdata) {
6125 starget = sas_device->starget;
6126 sas_target_priv_data = starget->hostdata;
6127 sas_target_priv_data->flags &=
6128 ~MPT_TARGET_FLAGS_RAID_COMPONENT;
6129 }
6130 }
Eric Moore635374e2009-03-09 01:21:12 -06006131 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6132 if (!sas_device)
6133 return;
6134
6135 /* exposing raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306136 if (starget)
6137 starget_for_each_device(starget, NULL, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006138}
6139
6140/**
6141 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6142 * @ioc: per adapter object
6143 * @element: IR config element data
6144 * Context: user.
6145 *
6146 * Return nothing.
6147 */
6148static void
6149_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6150 Mpi2EventIrConfigElement_t *element)
6151{
6152 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306153 struct scsi_target *starget = NULL;
6154 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06006155 unsigned long flags;
6156 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306157 u16 volume_handle = 0;
6158 u64 volume_wwid = 0;
6159
6160 mpt2sas_config_get_volume_handle(ioc, handle, &volume_handle);
6161 if (volume_handle)
6162 mpt2sas_config_get_volume_wwid(ioc, volume_handle,
6163 &volume_wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006164
6165 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6166 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306167 if (sas_device) {
6168 set_bit(handle, ioc->pd_handles);
6169 if (sas_device->starget && sas_device->starget->hostdata) {
6170 starget = sas_device->starget;
6171 sas_target_priv_data = starget->hostdata;
6172 sas_target_priv_data->flags |=
6173 MPT_TARGET_FLAGS_RAID_COMPONENT;
6174 sas_device->volume_handle = volume_handle;
6175 sas_device->volume_wwid = volume_wwid;
6176 }
6177 }
Eric Moore635374e2009-03-09 01:21:12 -06006178 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6179 if (!sas_device)
6180 return;
6181
6182 /* hiding raid component */
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306183 if (starget)
6184 starget_for_each_device(starget, (void *)1, _scsih_reprobe_lun);
Eric Moore635374e2009-03-09 01:21:12 -06006185}
6186
6187/**
6188 * _scsih_sas_pd_delete - delete pd component
6189 * @ioc: per adapter object
6190 * @element: IR config element data
6191 * Context: user.
6192 *
6193 * Return nothing.
6194 */
6195static void
6196_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6197 Mpi2EventIrConfigElement_t *element)
6198{
Eric Moore635374e2009-03-09 01:21:12 -06006199 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6200
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306201 _scsih_device_remove_by_handle(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006202}
6203
6204/**
6205 * _scsih_sas_pd_add - remove pd component
6206 * @ioc: per adapter object
6207 * @element: IR config element data
6208 * Context: user.
6209 *
6210 * Return nothing.
6211 */
6212static void
6213_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6214 Mpi2EventIrConfigElement_t *element)
6215{
6216 struct _sas_device *sas_device;
6217 unsigned long flags;
6218 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306219 Mpi2ConfigReply_t mpi_reply;
6220 Mpi2SasDevicePage0_t sas_device_pg0;
6221 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306222 u64 sas_address;
6223 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006224
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306225 set_bit(handle, ioc->pd_handles);
6226
Eric Moore635374e2009-03-09 01:21:12 -06006227 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6228 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6229 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306230 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306231 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306232
6233 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6234 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6235 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6236 ioc->name, __FILE__, __LINE__, __func__);
6237 return;
6238 }
6239
6240 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6241 MPI2_IOCSTATUS_MASK;
6242 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6243 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6244 ioc->name, __FILE__, __LINE__, __func__);
6245 return;
6246 }
6247
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306248 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6249 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6250 mpt2sas_transport_update_links(ioc, sas_address, handle,
6251 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306252
6253 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006254}
6255
6256#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6257/**
6258 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6259 * @ioc: per adapter object
6260 * @event_data: event data payload
6261 * Context: user.
6262 *
6263 * Return nothing.
6264 */
6265static void
6266_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6267 Mpi2EventDataIrConfigChangeList_t *event_data)
6268{
6269 Mpi2EventIrConfigElement_t *element;
6270 u8 element_type;
6271 int i;
6272 char *reason_str = NULL, *element_str = NULL;
6273
6274 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6275
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306276 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006277 ioc->name, (le32_to_cpu(event_data->Flags) &
6278 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6279 "foreign" : "native", event_data->NumElements);
6280 for (i = 0; i < event_data->NumElements; i++, element++) {
6281 switch (element->ReasonCode) {
6282 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6283 reason_str = "add";
6284 break;
6285 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6286 reason_str = "remove";
6287 break;
6288 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6289 reason_str = "no change";
6290 break;
6291 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6292 reason_str = "hide";
6293 break;
6294 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6295 reason_str = "unhide";
6296 break;
6297 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6298 reason_str = "volume_created";
6299 break;
6300 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6301 reason_str = "volume_deleted";
6302 break;
6303 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6304 reason_str = "pd_created";
6305 break;
6306 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6307 reason_str = "pd_deleted";
6308 break;
6309 default:
6310 reason_str = "unknown reason";
6311 break;
6312 }
6313 element_type = le16_to_cpu(element->ElementFlags) &
6314 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6315 switch (element_type) {
6316 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6317 element_str = "volume";
6318 break;
6319 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6320 element_str = "phys disk";
6321 break;
6322 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6323 element_str = "hot spare";
6324 break;
6325 default:
6326 element_str = "unknown element";
6327 break;
6328 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306329 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006330 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6331 reason_str, le16_to_cpu(element->VolDevHandle),
6332 le16_to_cpu(element->PhysDiskDevHandle),
6333 element->PhysDiskNum);
6334 }
6335}
6336#endif
6337
6338/**
6339 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6340 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306341 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006342 * Context: user.
6343 *
6344 * Return nothing.
6345 */
6346static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306347_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6348 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006349{
6350 Mpi2EventIrConfigElement_t *element;
6351 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306352 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306353 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006354
6355#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306356 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6357 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006358 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6359
6360#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306361
6362 if (ioc->shost_recovery)
6363 return;
6364
Kashyap, Desai62727a72009-08-07 19:35:18 +05306365 foreign_config = (le32_to_cpu(event_data->Flags) &
6366 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006367
6368 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6369 for (i = 0; i < event_data->NumElements; i++, element++) {
6370
6371 switch (element->ReasonCode) {
6372 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6373 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306374 if (!foreign_config)
6375 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006376 break;
6377 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6378 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306379 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306380 _scsih_sas_volume_delete(ioc,
6381 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006382 break;
6383 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306384 if (!ioc->is_warpdrive)
6385 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006386 break;
6387 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306388 if (!ioc->is_warpdrive)
6389 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006390 break;
6391 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306392 if (!ioc->is_warpdrive)
6393 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006394 break;
6395 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306396 if (!ioc->is_warpdrive)
6397 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006398 break;
6399 }
6400 }
6401}
6402
6403/**
6404 * _scsih_sas_ir_volume_event - IR volume event
6405 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306406 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006407 * Context: user.
6408 *
6409 * Return nothing.
6410 */
6411static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306412_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6413 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006414{
6415 u64 wwid;
6416 unsigned long flags;
6417 struct _raid_device *raid_device;
6418 u16 handle;
6419 u32 state;
6420 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306421 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006422
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306423 if (ioc->shost_recovery)
6424 return;
6425
Eric Moore635374e2009-03-09 01:21:12 -06006426 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6427 return;
6428
6429 handle = le16_to_cpu(event_data->VolDevHandle);
6430 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306431 if (!ioc->hide_ir_msg)
6432 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6433 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6434 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006435
Eric Moore635374e2009-03-09 01:21:12 -06006436 switch (state) {
6437 case MPI2_RAID_VOL_STATE_MISSING:
6438 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306439 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006440 break;
6441
6442 case MPI2_RAID_VOL_STATE_ONLINE:
6443 case MPI2_RAID_VOL_STATE_DEGRADED:
6444 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306445
6446 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6447 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6448 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6449
Eric Moore635374e2009-03-09 01:21:12 -06006450 if (raid_device)
6451 break;
6452
6453 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6454 if (!wwid) {
6455 printk(MPT2SAS_ERR_FMT
6456 "failure at %s:%d/%s()!\n", ioc->name,
6457 __FILE__, __LINE__, __func__);
6458 break;
6459 }
6460
6461 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6462 if (!raid_device) {
6463 printk(MPT2SAS_ERR_FMT
6464 "failure at %s:%d/%s()!\n", ioc->name,
6465 __FILE__, __LINE__, __func__);
6466 break;
6467 }
6468
6469 raid_device->id = ioc->sas_id++;
6470 raid_device->channel = RAID_CHANNEL;
6471 raid_device->handle = handle;
6472 raid_device->wwid = wwid;
6473 _scsih_raid_device_add(ioc, raid_device);
6474 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6475 raid_device->id, 0);
6476 if (rc)
6477 _scsih_raid_device_remove(ioc, raid_device);
6478 break;
6479
6480 case MPI2_RAID_VOL_STATE_INITIALIZING:
6481 default:
6482 break;
6483 }
6484}
6485
6486/**
6487 * _scsih_sas_ir_physical_disk_event - PD event
6488 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306489 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006490 * Context: user.
6491 *
6492 * Return nothing.
6493 */
6494static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306495_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6496 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006497{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306498 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006499 u32 state;
6500 struct _sas_device *sas_device;
6501 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306502 Mpi2ConfigReply_t mpi_reply;
6503 Mpi2SasDevicePage0_t sas_device_pg0;
6504 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306505 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306506 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006507
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306508 if (ioc->shost_recovery)
6509 return;
6510
Eric Moore635374e2009-03-09 01:21:12 -06006511 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6512 return;
6513
6514 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6515 state = le32_to_cpu(event_data->NewValue);
6516
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306517 if (!ioc->hide_ir_msg)
6518 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6519 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6520 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006521
Eric Moore635374e2009-03-09 01:21:12 -06006522 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006523 case MPI2_RAID_PD_STATE_ONLINE:
6524 case MPI2_RAID_PD_STATE_DEGRADED:
6525 case MPI2_RAID_PD_STATE_REBUILDING:
6526 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306527 case MPI2_RAID_PD_STATE_HOT_SPARE:
6528
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306529 if (!ioc->is_warpdrive)
6530 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306531
6532 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6533 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6534 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6535
6536 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306537 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306538
6539 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6540 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6541 handle))) {
6542 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6543 ioc->name, __FILE__, __LINE__, __func__);
6544 return;
6545 }
6546
6547 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6548 MPI2_IOCSTATUS_MASK;
6549 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6550 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6551 ioc->name, __FILE__, __LINE__, __func__);
6552 return;
6553 }
6554
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306555 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6556 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6557 mpt2sas_transport_update_links(ioc, sas_address, handle,
6558 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306559
6560 _scsih_add_device(ioc, handle, 0, 1);
6561
Eric Moore635374e2009-03-09 01:21:12 -06006562 break;
6563
Kashyap, Desai62727a72009-08-07 19:35:18 +05306564 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006565 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6566 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006567 default:
6568 break;
6569 }
6570}
6571
6572#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6573/**
6574 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6575 * @ioc: per adapter object
6576 * @event_data: event data payload
6577 * Context: user.
6578 *
6579 * Return nothing.
6580 */
6581static void
6582_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6583 Mpi2EventDataIrOperationStatus_t *event_data)
6584{
6585 char *reason_str = NULL;
6586
6587 switch (event_data->RAIDOperation) {
6588 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6589 reason_str = "resync";
6590 break;
6591 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6592 reason_str = "online capacity expansion";
6593 break;
6594 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6595 reason_str = "consistency check";
6596 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306597 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6598 reason_str = "background init";
6599 break;
6600 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6601 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006602 break;
6603 }
6604
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306605 if (!reason_str)
6606 return;
6607
Eric Moore635374e2009-03-09 01:21:12 -06006608 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6609 "\thandle(0x%04x), percent complete(%d)\n",
6610 ioc->name, reason_str,
6611 le16_to_cpu(event_data->VolDevHandle),
6612 event_data->PercentComplete);
6613}
6614#endif
6615
6616/**
6617 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6618 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306619 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006620 * Context: user.
6621 *
6622 * Return nothing.
6623 */
6624static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306625_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6626 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006627{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306628 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6629 static struct _raid_device *raid_device;
6630 unsigned long flags;
6631 u16 handle;
6632
Eric Moore635374e2009-03-09 01:21:12 -06006633#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306634 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6635 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306636 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306637 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006638#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306639
6640 /* code added for raid transport support */
6641 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6642
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306643 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306644 handle = le16_to_cpu(event_data->VolDevHandle);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306645 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306646 if (raid_device)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306647 raid_device->percent_complete =
6648 event_data->PercentComplete;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306649 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306650 }
Eric Moore635374e2009-03-09 01:21:12 -06006651}
6652
6653/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306654 * _scsih_prep_device_scan - initialize parameters prior to device scan
6655 * @ioc: per adapter object
6656 *
6657 * Set the deleted flag prior to device scan. If the device is found during
6658 * the scan, then we clear the deleted flag.
6659 */
6660static void
6661_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6662{
6663 struct MPT2SAS_DEVICE *sas_device_priv_data;
6664 struct scsi_device *sdev;
6665
6666 shost_for_each_device(sdev, ioc->shost) {
6667 sas_device_priv_data = sdev->hostdata;
6668 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6669 sas_device_priv_data->sas_target->deleted = 1;
6670 }
6671}
6672
6673/**
Eric Moore635374e2009-03-09 01:21:12 -06006674 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6675 * @ioc: per adapter object
6676 * @sas_address: sas address
6677 * @slot: enclosure slot id
6678 * @handle: device handle
6679 *
6680 * After host reset, find out whether devices are still responding.
6681 * Used in _scsi_remove_unresponsive_sas_devices.
6682 *
6683 * Return nothing.
6684 */
6685static void
6686_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6687 u16 slot, u16 handle)
6688{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306689 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006690 struct scsi_target *starget;
6691 struct _sas_device *sas_device;
6692 unsigned long flags;
6693
6694 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6695 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6696 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306697 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006698 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306699 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306700 if (starget && starget->hostdata) {
6701 sas_target_priv_data = starget->hostdata;
6702 sas_target_priv_data->tm_busy = 0;
6703 sas_target_priv_data->deleted = 0;
6704 } else
6705 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306706 if (starget)
6707 starget_printk(KERN_INFO, starget,
6708 "handle(0x%04x), sas_addr(0x%016llx), "
6709 "enclosure logical id(0x%016llx), "
6710 "slot(%d)\n", handle,
6711 (unsigned long long)sas_device->sas_address,
6712 (unsigned long long)
6713 sas_device->enclosure_logical_id,
6714 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006715 if (sas_device->handle == handle)
6716 goto out;
6717 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6718 sas_device->handle);
6719 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306720 if (sas_target_priv_data)
6721 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006722 goto out;
6723 }
6724 }
6725 out:
6726 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6727}
6728
6729/**
6730 * _scsih_search_responding_sas_devices -
6731 * @ioc: per adapter object
6732 *
6733 * After host reset, find out whether devices are still responding.
6734 * If not remove.
6735 *
6736 * Return nothing.
6737 */
6738static void
6739_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6740{
6741 Mpi2SasDevicePage0_t sas_device_pg0;
6742 Mpi2ConfigReply_t mpi_reply;
6743 u16 ioc_status;
6744 __le64 sas_address;
6745 u16 handle;
6746 u32 device_info;
6747 u16 slot;
6748
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306749 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006750
6751 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306752 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006753
6754 handle = 0xFFFF;
6755 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6756 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6757 handle))) {
6758 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6759 MPI2_IOCSTATUS_MASK;
6760 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6761 break;
6762 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6763 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6764 if (!(_scsih_is_end_device(device_info)))
6765 continue;
6766 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6767 slot = le16_to_cpu(sas_device_pg0.Slot);
6768 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6769 handle);
6770 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306771out:
6772 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6773 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006774}
6775
6776/**
6777 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6778 * @ioc: per adapter object
6779 * @wwid: world wide identifier for raid volume
6780 * @handle: device handle
6781 *
6782 * After host reset, find out whether devices are still responding.
6783 * Used in _scsi_remove_unresponsive_raid_devices.
6784 *
6785 * Return nothing.
6786 */
6787static void
6788_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6789 u16 handle)
6790{
6791 struct MPT2SAS_TARGET *sas_target_priv_data;
6792 struct scsi_target *starget;
6793 struct _raid_device *raid_device;
6794 unsigned long flags;
6795
6796 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6797 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6798 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306799 starget = raid_device->starget;
6800 if (starget && starget->hostdata) {
6801 sas_target_priv_data = starget->hostdata;
6802 sas_target_priv_data->deleted = 0;
6803 } else
6804 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006805 raid_device->responding = 1;
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306806 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006807 starget_printk(KERN_INFO, raid_device->starget,
6808 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6809 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306810 /*
6811 * WARPDRIVE: The handles of the PDs might have changed
6812 * across the host reset so re-initialize the
6813 * required data for Direct IO
6814 */
6815 _scsih_init_warpdrive_properties(ioc, raid_device);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306816 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6817 if (raid_device->handle == handle) {
6818 spin_unlock_irqrestore(&ioc->raid_device_lock,
6819 flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306820 return;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306821 }
Eric Moore635374e2009-03-09 01:21:12 -06006822 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6823 raid_device->handle);
6824 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306825 if (sas_target_priv_data)
6826 sas_target_priv_data->handle = handle;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306827 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306828 return;
Eric Moore635374e2009-03-09 01:21:12 -06006829 }
6830 }
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306831
Eric Moore635374e2009-03-09 01:21:12 -06006832 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6833}
6834
6835/**
6836 * _scsih_search_responding_raid_devices -
6837 * @ioc: per adapter object
6838 *
6839 * After host reset, find out whether devices are still responding.
6840 * If not remove.
6841 *
6842 * Return nothing.
6843 */
6844static void
6845_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6846{
6847 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306848 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306849 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006850 Mpi2ConfigReply_t mpi_reply;
6851 u16 ioc_status;
6852 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306853 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006854
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306855 if (!ioc->ir_firmware)
6856 return;
6857
6858 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6859 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006860
6861 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306862 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006863
6864 handle = 0xFFFF;
6865 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6866 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6867 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6868 MPI2_IOCSTATUS_MASK;
6869 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6870 break;
6871 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306872
6873 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6874 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6875 sizeof(Mpi2RaidVolPage0_t)))
6876 continue;
6877
6878 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6879 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6880 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6881 _scsih_mark_responding_raid_device(ioc,
6882 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006883 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306884
6885 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306886 if (!ioc->is_warpdrive) {
6887 phys_disk_num = 0xFF;
6888 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6889 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6890 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6891 phys_disk_num))) {
6892 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6893 MPI2_IOCSTATUS_MASK;
6894 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6895 break;
6896 phys_disk_num = pd_pg0.PhysDiskNum;
6897 handle = le16_to_cpu(pd_pg0.DevHandle);
6898 set_bit(handle, ioc->pd_handles);
6899 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306900 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306901out:
6902 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6903 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006904}
6905
6906/**
6907 * _scsih_mark_responding_expander - mark a expander as responding
6908 * @ioc: per adapter object
6909 * @sas_address: sas address
6910 * @handle:
6911 *
6912 * After host reset, find out whether devices are still responding.
6913 * Used in _scsi_remove_unresponsive_expanders.
6914 *
6915 * Return nothing.
6916 */
6917static void
6918_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6919 u16 handle)
6920{
6921 struct _sas_node *sas_expander;
6922 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306923 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006924
6925 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6926 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306927 if (sas_expander->sas_address != sas_address)
6928 continue;
6929 sas_expander->responding = 1;
6930 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006931 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306932 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6933 " from(0x%04x) to (0x%04x)!!!\n",
6934 (unsigned long long)sas_expander->sas_address,
6935 sas_expander->handle, handle);
6936 sas_expander->handle = handle;
6937 for (i = 0 ; i < sas_expander->num_phys ; i++)
6938 sas_expander->phy[i].handle = handle;
6939 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006940 }
6941 out:
6942 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6943}
6944
6945/**
6946 * _scsih_search_responding_expanders -
6947 * @ioc: per adapter object
6948 *
6949 * After host reset, find out whether devices are still responding.
6950 * If not remove.
6951 *
6952 * Return nothing.
6953 */
6954static void
6955_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6956{
6957 Mpi2ExpanderPage0_t expander_pg0;
6958 Mpi2ConfigReply_t mpi_reply;
6959 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306960 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006961 u16 handle;
6962
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306963 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006964
6965 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306966 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006967
6968 handle = 0xFFFF;
6969 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6970 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6971
6972 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6973 MPI2_IOCSTATUS_MASK;
6974 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6975 break;
6976
6977 handle = le16_to_cpu(expander_pg0.DevHandle);
6978 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6979 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6980 "sas_addr(0x%016llx)\n", handle,
6981 (unsigned long long)sas_address);
6982 _scsih_mark_responding_expander(ioc, sas_address, handle);
6983 }
6984
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306985 out:
6986 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006987}
6988
6989/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306990 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006991 * @ioc: per adapter object
6992 *
6993 * Return nothing.
6994 */
6995static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306996_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006997{
6998 struct _sas_device *sas_device, *sas_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05306999 struct _sas_node *sas_expander, *sas_expander_next;
Eric Moore635374e2009-03-09 01:21:12 -06007000 struct _raid_device *raid_device, *raid_device_next;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307001 struct list_head tmp_list;
7002 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007003
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307004 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
7005 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007006
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307007 /* removing unresponding end devices */
7008 printk(MPT2SAS_INFO_FMT "removing unresponding devices: end-devices\n",
7009 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06007010 list_for_each_entry_safe(sas_device, sas_device_next,
7011 &ioc->sas_device_list, list) {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307012 if (!sas_device->responding)
nagalakshmi.nandigama@lsi.com39af7a92012-03-20 12:09:26 +05307013 mpt2sas_device_remove_by_sas_address(ioc,
7014 sas_device->sas_address);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307015 else
Eric Moore635374e2009-03-09 01:21:12 -06007016 sas_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007017 }
7018
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307019 /* removing unresponding volumes */
7020 if (ioc->ir_firmware) {
7021 printk(MPT2SAS_INFO_FMT "removing unresponding devices: "
7022 "volumes\n", ioc->name);
7023 list_for_each_entry_safe(raid_device, raid_device_next,
7024 &ioc->raid_device_list, list) {
7025 if (!raid_device->responding)
7026 _scsih_sas_volume_delete(ioc,
7027 raid_device->handle);
7028 else
7029 raid_device->responding = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007030 }
Eric Moore635374e2009-03-09 01:21:12 -06007031 }
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307032 /* removing unresponding expanders */
7033 printk(MPT2SAS_INFO_FMT "removing unresponding devices: expanders\n",
7034 ioc->name);
7035 spin_lock_irqsave(&ioc->sas_node_lock, flags);
7036 INIT_LIST_HEAD(&tmp_list);
7037 list_for_each_entry_safe(sas_expander, sas_expander_next,
7038 &ioc->sas_expander_list, list) {
7039 if (!sas_expander->responding)
7040 list_move_tail(&sas_expander->list, &tmp_list);
7041 else
Eric Moore635374e2009-03-09 01:21:12 -06007042 sas_expander->responding = 0;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307043 }
7044 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
7045 list_for_each_entry_safe(sas_expander, sas_expander_next, &tmp_list,
7046 list) {
7047 list_del(&sas_expander->list);
7048 _scsih_expander_node_remove(ioc, sas_expander);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307049 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307050 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
7051 ioc->name);
7052 /* unblock devices */
7053 _scsih_ublock_io_all_device(ioc);
7054}
7055
7056static void
7057_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
7058 struct _sas_node *sas_expander, u16 handle)
7059{
7060 Mpi2ExpanderPage1_t expander_pg1;
7061 Mpi2ConfigReply_t mpi_reply;
7062 int i;
7063
7064 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7065 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
7066 &expander_pg1, i, handle))) {
7067 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7068 ioc->name, __FILE__, __LINE__, __func__);
7069 return;
7070 }
7071
7072 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
7073 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7074 expander_pg1.NegotiatedLinkRate >> 4);
7075 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307076}
7077
7078/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307079 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307080 * @ioc: per adapter object
7081 *
7082 * Return nothing.
7083 */
7084static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307085_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307086{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307087 Mpi2ExpanderPage0_t expander_pg0;
7088 Mpi2SasDevicePage0_t sas_device_pg0;
7089 Mpi2RaidVolPage1_t volume_pg1;
7090 Mpi2RaidVolPage0_t volume_pg0;
7091 Mpi2RaidPhysDiskPage0_t pd_pg0;
7092 Mpi2EventIrConfigElement_t element;
7093 Mpi2ConfigReply_t mpi_reply;
7094 u8 phys_disk_num;
7095 u16 ioc_status;
7096 u16 handle, parent_handle;
7097 u64 sas_address;
7098 struct _sas_device *sas_device;
7099 struct _sas_node *expander_device;
7100 static struct _raid_device *raid_device;
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
7107 /* expanders */
7108 handle = 0xFFFF;
7109 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7110 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7111 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7112 MPI2_IOCSTATUS_MASK;
7113 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7114 break;
7115 handle = le16_to_cpu(expander_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307116 spin_lock_irqsave(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307117 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7118 ioc, le64_to_cpu(expander_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307119 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307120 if (expander_device)
7121 _scsih_refresh_expander_links(ioc, expander_device,
7122 handle);
7123 else
7124 _scsih_expander_add(ioc, handle);
7125 }
7126
7127 if (!ioc->ir_firmware)
7128 goto skip_to_sas;
7129
7130 /* phys disk */
7131 phys_disk_num = 0xFF;
7132 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7133 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7134 phys_disk_num))) {
7135 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7136 MPI2_IOCSTATUS_MASK;
7137 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7138 break;
7139 phys_disk_num = pd_pg0.PhysDiskNum;
7140 handle = le16_to_cpu(pd_pg0.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307141 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307142 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307143 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307144 if (sas_device)
7145 continue;
7146 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7147 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7148 handle) != 0)
7149 continue;
7150 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7151 if (!_scsih_get_sas_address(ioc, parent_handle,
7152 &sas_address)) {
7153 mpt2sas_transport_update_links(ioc, sas_address,
7154 handle, sas_device_pg0.PhyNum,
7155 MPI2_SAS_NEG_LINK_RATE_1_5);
7156 set_bit(handle, ioc->pd_handles);
7157 _scsih_add_device(ioc, handle, 0, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307158 }
7159 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307160
7161 /* volumes */
7162 handle = 0xFFFF;
7163 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7164 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7165 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7166 MPI2_IOCSTATUS_MASK;
7167 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7168 break;
7169 handle = le16_to_cpu(volume_pg1.DevHandle);
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307170 spin_lock_irqsave(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307171 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7172 le64_to_cpu(volume_pg1.WWID));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307173 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307174 if (raid_device)
7175 continue;
7176 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7177 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7178 sizeof(Mpi2RaidVolPage0_t)))
7179 continue;
7180 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7181 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7182 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7183 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7184 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7185 element.VolDevHandle = volume_pg1.DevHandle;
7186 _scsih_sas_volume_add(ioc, &element);
7187 }
7188 }
7189
7190 skip_to_sas:
7191
7192 /* sas devices */
7193 handle = 0xFFFF;
7194 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7195 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7196 handle))) {
7197 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7198 MPI2_IOCSTATUS_MASK;
7199 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7200 break;
7201 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7202 if (!(_scsih_is_end_device(
7203 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7204 continue;
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307205 spin_lock_irqsave(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307206 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7207 le64_to_cpu(sas_device_pg0.SASAddress));
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307208 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307209 if (sas_device)
7210 continue;
7211 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7212 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7213 mpt2sas_transport_update_links(ioc, sas_address, handle,
7214 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7215 _scsih_add_device(ioc, handle, 0, 0);
7216 }
7217 }
7218
7219 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307220}
7221
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307222
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307223/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307224 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7225 * @ioc: per adapter object
7226 * @reset_phase: phase
7227 *
7228 * The handler for doing any required cleanup or initialization.
7229 *
7230 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7231 * MPT2_IOC_DONE_RESET
7232 *
7233 * Return nothing.
7234 */
7235void
7236mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7237{
7238 switch (reset_phase) {
7239 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307240 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307241 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307242 break;
7243 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307244 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307245 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307246 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7247 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7248 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7249 complete(&ioc->scsih_cmds.done);
7250 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307251 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7252 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7253 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7254 complete(&ioc->tm_cmds.done);
7255 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307256 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307257 _scsih_flush_running_cmds(ioc);
7258 break;
7259 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307260 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307261 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307262 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307263 _scsih_prep_device_scan(ioc);
7264 _scsih_search_responding_sas_devices(ioc);
7265 _scsih_search_responding_raid_devices(ioc);
7266 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307267 if (!ioc->is_driver_loading) {
7268 _scsih_prep_device_scan(ioc);
7269 _scsih_search_responding_sas_devices(ioc);
7270 _scsih_search_responding_raid_devices(ioc);
7271 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307272 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307273 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307274 break;
Eric Moore635374e2009-03-09 01:21:12 -06007275 }
7276}
7277
7278/**
7279 * _firmware_event_work - delayed task for processing firmware events
7280 * @ioc: per adapter object
7281 * @work: equal to the fw_event_work object
7282 * Context: user.
7283 *
7284 * Return nothing.
7285 */
7286static void
7287_firmware_event_work(struct work_struct *work)
7288{
7289 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307290 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007291 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7292
Eric Moore635374e2009-03-09 01:21:12 -06007293 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007294 if (ioc->remove_host || fw_event->cancel_pending_work ||
7295 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007296 _scsih_fw_event_free(ioc, fw_event);
7297 return;
7298 }
Eric Moore635374e2009-03-09 01:21:12 -06007299
Eric Moore635374e2009-03-09 01:21:12 -06007300 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307301 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307302 while (scsi_host_in_recovery(ioc->shost) || ioc->shost_recovery)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307303 ssleep(1);
7304 _scsih_remove_unresponding_sas_devices(ioc);
7305 _scsih_scan_for_devices_after_reset(ioc);
7306 break;
7307 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307308 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307309
7310
7311
7312 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7313 "from worker thread\n", ioc->name));
7314 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307315 case MPT2SAS_TURN_ON_FAULT_LED:
7316 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7317 break;
Eric Moore635374e2009-03-09 01:21:12 -06007318 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307319 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007320 break;
7321 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307322 _scsih_sas_device_status_change_event(ioc,
7323 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007324 break;
7325 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307326 _scsih_sas_discovery_event(ioc,
7327 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007328 break;
7329 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Andrew Mortona78e21d2012-02-08 12:52:22 -08007330 _scsih_sas_broadcast_primitive_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307331 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007332 break;
7333 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7334 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307335 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007336 break;
7337 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307338 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007339 break;
7340 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307341 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007342 break;
7343 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307344 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007345 break;
7346 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307347 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007348 break;
Eric Moore635374e2009-03-09 01:21:12 -06007349 }
7350 _scsih_fw_event_free(ioc, fw_event);
7351}
7352
7353/**
7354 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7355 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307356 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007357 * @reply: reply message frame(lower 32bit addr)
7358 * Context: interrupt.
7359 *
7360 * This function merely adds a new work task into ioc->firmware_event_thread.
7361 * The tasks are worked from _firmware_event_work in user context.
7362 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307363 * Return 1 meaning mf should be freed from _base_interrupt
7364 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06007365 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307366u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307367mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7368 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007369{
7370 struct fw_event_work *fw_event;
7371 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007372 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307373 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007374
7375 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007376 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307377 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007378
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307379 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
nagalakshmi.nandigama@lsi.com298c7942012-03-20 12:07:17 +05307380
7381 if (unlikely(!mpi_reply)) {
7382 printk(MPT2SAS_ERR_FMT "mpi_reply not valid at %s:%d/%s()!\n",
7383 ioc->name, __FILE__, __LINE__, __func__);
7384 return 1;
7385 }
7386
Eric Moore635374e2009-03-09 01:21:12 -06007387 event = le16_to_cpu(mpi_reply->Event);
7388
7389 switch (event) {
7390 /* handle these */
7391 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7392 {
7393 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7394 (Mpi2EventDataSasBroadcastPrimitive_t *)
7395 mpi_reply->EventData;
7396
7397 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307398 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307399 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307400
7401 if (ioc->broadcast_aen_busy) {
7402 ioc->broadcast_aen_pending++;
7403 return 1;
7404 } else
7405 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007406 break;
7407 }
7408
7409 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7410 _scsih_check_topo_delete_events(ioc,
7411 (Mpi2EventDataSasTopologyChangeList_t *)
7412 mpi_reply->EventData);
7413 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307414 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7415 _scsih_check_ir_config_unhide_events(ioc,
7416 (Mpi2EventDataIrConfigChangeList_t *)
7417 mpi_reply->EventData);
7418 break;
7419 case MPI2_EVENT_IR_VOLUME:
7420 _scsih_check_volume_delete_events(ioc,
7421 (Mpi2EventDataIrVolume_t *)
7422 mpi_reply->EventData);
7423 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307424 case MPI2_EVENT_LOG_ENTRY_ADDED:
7425 {
7426 Mpi2EventDataLogEntryAdded_t *log_entry;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307427 __le32 *log_code;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307428
7429 if (!ioc->is_warpdrive)
7430 break;
7431
7432 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7433 mpi_reply->EventData;
nagalakshmi.nandigama@lsi.comd838c362012-03-20 12:07:48 +05307434 log_code = (__le32 *)log_entry->LogData;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307435
7436 if (le16_to_cpu(log_entry->LogEntryQualifier)
7437 != MPT2_WARPDRIVE_LOGENTRY)
7438 break;
7439
7440 switch (le32_to_cpu(*log_code)) {
7441 case MPT2_WARPDRIVE_LC_SSDT:
7442 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7443 "IO Throttling has occurred in the WarpDrive "
7444 "subsystem. Check WarpDrive documentation for "
7445 "additional details.\n", ioc->name);
7446 break;
7447 case MPT2_WARPDRIVE_LC_SSDLW:
7448 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7449 "Program/Erase Cycles for the WarpDrive subsystem "
7450 "in degraded range. Check WarpDrive documentation "
7451 "for additional details.\n", ioc->name);
7452 break;
7453 case MPT2_WARPDRIVE_LC_SSDLF:
7454 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7455 "There are no Program/Erase Cycles for the "
7456 "WarpDrive subsystem. The storage device will be "
7457 "in read-only mode. Check WarpDrive documentation "
7458 "for additional details.\n", ioc->name);
7459 break;
7460 case MPT2_WARPDRIVE_LC_BRMF:
7461 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7462 "The Backup Rail Monitor has failed on the "
7463 "WarpDrive subsystem. Check WarpDrive "
7464 "documentation for additional details.\n",
7465 ioc->name);
7466 break;
7467 }
7468
7469 break;
7470 }
Eric Moore635374e2009-03-09 01:21:12 -06007471 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7472 case MPI2_EVENT_IR_OPERATION_STATUS:
7473 case MPI2_EVENT_SAS_DISCOVERY:
7474 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007475 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007476 break;
7477
7478 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307479 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007480 }
7481
7482 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7483 if (!fw_event) {
7484 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7485 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307486 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007487 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307488 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7489 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007490 if (!fw_event->event_data) {
7491 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7492 ioc->name, __FILE__, __LINE__, __func__);
7493 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307494 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007495 }
7496
7497 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307498 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007499 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307500 fw_event->VF_ID = mpi_reply->VF_ID;
7501 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007502 fw_event->event = event;
7503 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307504 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007505}
7506
7507/* shost template */
7508static struct scsi_host_template scsih_driver_template = {
7509 .module = THIS_MODULE,
7510 .name = "Fusion MPT SAS Host",
7511 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007512 .queuecommand = _scsih_qcmd,
7513 .target_alloc = _scsih_target_alloc,
7514 .slave_alloc = _scsih_slave_alloc,
7515 .slave_configure = _scsih_slave_configure,
7516 .target_destroy = _scsih_target_destroy,
7517 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307518 .scan_finished = _scsih_scan_finished,
7519 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007520 .change_queue_depth = _scsih_change_queue_depth,
7521 .change_queue_type = _scsih_change_queue_type,
7522 .eh_abort_handler = _scsih_abort,
7523 .eh_device_reset_handler = _scsih_dev_reset,
7524 .eh_target_reset_handler = _scsih_target_reset,
7525 .eh_host_reset_handler = _scsih_host_reset,
7526 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007527 .can_queue = 1,
7528 .this_id = -1,
7529 .sg_tablesize = MPT2SAS_SG_DEPTH,
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05307530 .max_sectors = 32767,
Eric Moore635374e2009-03-09 01:21:12 -06007531 .cmd_per_lun = 7,
7532 .use_clustering = ENABLE_CLUSTERING,
7533 .shost_attrs = mpt2sas_host_attrs,
7534 .sdev_attrs = mpt2sas_dev_attrs,
7535};
7536
7537/**
7538 * _scsih_expander_node_remove - removing expander device from list.
7539 * @ioc: per adapter object
7540 * @sas_expander: the sas_device object
7541 * Context: Calling function should acquire ioc->sas_node_lock.
7542 *
7543 * Removing object and freeing associated memory from the
7544 * ioc->sas_expander_list.
7545 *
7546 * Return nothing.
7547 */
7548static void
7549_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7550 struct _sas_node *sas_expander)
7551{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307552 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007553
7554 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307555 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007556 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307557 if (ioc->shost_recovery)
7558 return;
Eric Moore635374e2009-03-09 01:21:12 -06007559 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307560 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307561 mpt2sas_device_remove_by_sas_address(ioc,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307562 mpt2sas_port->remote_identify.sas_address);
7563 else if (mpt2sas_port->remote_identify.device_type ==
7564 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007565 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307566 SAS_FANOUT_EXPANDER_DEVICE)
7567 mpt2sas_expander_remove(ioc,
7568 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007569 }
7570
7571 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307572 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007573
7574 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7575 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7576 sas_expander->handle, (unsigned long long)
7577 sas_expander->sas_address);
7578
Eric Moore635374e2009-03-09 01:21:12 -06007579 kfree(sas_expander->phy);
7580 kfree(sas_expander);
7581}
7582
7583/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307584 * _scsih_ir_shutdown - IR shutdown notification
7585 * @ioc: per adapter object
7586 *
7587 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7588 * the host system is shutting down.
7589 *
7590 * Return nothing.
7591 */
7592static void
7593_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7594{
7595 Mpi2RaidActionRequest_t *mpi_request;
7596 Mpi2RaidActionReply_t *mpi_reply;
7597 u16 smid;
7598
7599 /* is IR firmware build loaded ? */
7600 if (!ioc->ir_firmware)
7601 return;
7602
7603 /* are there any volumes ? */
7604 if (list_empty(&ioc->raid_device_list))
7605 return;
7606
7607 mutex_lock(&ioc->scsih_cmds.mutex);
7608
7609 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7610 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7611 ioc->name, __func__);
7612 goto out;
7613 }
7614 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7615
7616 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7617 if (!smid) {
7618 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7619 ioc->name, __func__);
7620 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7621 goto out;
7622 }
7623
7624 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7625 ioc->scsih_cmds.smid = smid;
7626 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7627
7628 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7629 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7630
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307631 if (!ioc->hide_ir_msg)
7632 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307633 init_completion(&ioc->scsih_cmds.done);
7634 mpt2sas_base_put_smid_default(ioc, smid);
7635 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7636
7637 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7638 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7639 ioc->name, __func__);
7640 goto out;
7641 }
7642
7643 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7644 mpi_reply = ioc->scsih_cmds.reply;
7645
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307646 if (!ioc->hide_ir_msg)
7647 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7648 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7649 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7650 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307651 }
7652
7653 out:
7654 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7655 mutex_unlock(&ioc->scsih_cmds.mutex);
7656}
7657
7658/**
7659 * _scsih_shutdown - routine call during system shutdown
7660 * @pdev: PCI device struct
7661 *
7662 * Return nothing.
7663 */
7664static void
7665_scsih_shutdown(struct pci_dev *pdev)
7666{
7667 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7668 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307669 struct workqueue_struct *wq;
7670 unsigned long flags;
7671
7672 ioc->remove_host = 1;
7673 _scsih_fw_event_cleanup_queue(ioc);
7674
7675 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7676 wq = ioc->firmware_event_thread;
7677 ioc->firmware_event_thread = NULL;
7678 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7679 if (wq)
7680 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307681
7682 _scsih_ir_shutdown(ioc);
7683 mpt2sas_base_detach(ioc);
7684}
7685
7686/**
Eric Moored5d135b2009-05-18 13:02:08 -06007687 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007688 * @pdev: PCI device struct
7689 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307690 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007691 * Return nothing.
7692 */
7693static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007694_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007695{
7696 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7697 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307698 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307699 struct _raid_device *raid_device, *next;
7700 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007701 struct workqueue_struct *wq;
7702 unsigned long flags;
7703
7704 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307705 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007706
7707 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7708 wq = ioc->firmware_event_thread;
7709 ioc->firmware_event_thread = NULL;
7710 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7711 if (wq)
7712 destroy_workqueue(wq);
7713
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307714 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307715 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307716 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7717 list) {
7718 if (raid_device->starget) {
7719 sas_target_priv_data =
7720 raid_device->starget->hostdata;
7721 sas_target_priv_data->deleted = 1;
7722 scsi_remove_target(&raid_device->starget->dev);
7723 }
7724 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7725 "(0x%016llx)\n", ioc->name, raid_device->handle,
7726 (unsigned long long) raid_device->wwid);
7727 _scsih_raid_device_remove(ioc, raid_device);
7728 }
7729
Eric Moore635374e2009-03-09 01:21:12 -06007730 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307731 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007732 &ioc->sas_hba.sas_port_list, port_list) {
7733 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307734 SAS_END_DEVICE)
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307735 mpt2sas_device_remove_by_sas_address(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007736 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307737 else if (mpt2sas_port->remote_identify.device_type ==
7738 SAS_EDGE_EXPANDER_DEVICE ||
7739 mpt2sas_port->remote_identify.device_type ==
7740 SAS_FANOUT_EXPANDER_DEVICE)
7741 mpt2sas_expander_remove(ioc,
7742 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007743 }
7744
7745 /* free phys attached to the sas_host */
7746 if (ioc->sas_hba.num_phys) {
7747 kfree(ioc->sas_hba.phy);
7748 ioc->sas_hba.phy = NULL;
7749 ioc->sas_hba.num_phys = 0;
7750 }
7751
7752 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307753 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007754 list_del(&ioc->list);
7755 scsi_remove_host(shost);
7756 scsi_host_put(shost);
7757}
7758
7759/**
7760 * _scsih_probe_boot_devices - reports 1st device
7761 * @ioc: per adapter object
7762 *
7763 * If specified in bios page 2, this routine reports the 1st
7764 * device scsi-ml or sas transport for persistent boot device
7765 * purposes. Please refer to function _scsih_determine_boot_device()
7766 */
7767static void
7768_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7769{
7770 u8 is_raid;
7771 void *device;
7772 struct _sas_device *sas_device;
7773 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307774 u16 handle;
7775 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007776 u64 sas_address;
7777 unsigned long flags;
7778 int rc;
7779
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307780 /* no Bios, return immediately */
7781 if (!ioc->bios_pg3.BiosVersion)
7782 return;
7783
Eric Moore635374e2009-03-09 01:21:12 -06007784 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307785 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007786 if (ioc->req_boot_device.device) {
7787 device = ioc->req_boot_device.device;
7788 is_raid = ioc->req_boot_device.is_raid;
7789 } else if (ioc->req_alt_boot_device.device) {
7790 device = ioc->req_alt_boot_device.device;
7791 is_raid = ioc->req_alt_boot_device.is_raid;
7792 } else if (ioc->current_boot_device.device) {
7793 device = ioc->current_boot_device.device;
7794 is_raid = ioc->current_boot_device.is_raid;
7795 }
7796
7797 if (!device)
7798 return;
7799
7800 if (is_raid) {
7801 raid_device = device;
7802 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7803 raid_device->id, 0);
7804 if (rc)
7805 _scsih_raid_device_remove(ioc, raid_device);
7806 } else {
nagalakshmi.nandigama@lsi.com09da0b32012-03-20 12:06:50 +05307807 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007808 sas_device = device;
7809 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307810 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007811 sas_address = sas_device->sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06007812 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7813 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307814
7815 if (ioc->hide_drives)
7816 return;
Eric Moore635374e2009-03-09 01:21:12 -06007817 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307818 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007819 _scsih_sas_device_remove(ioc, sas_device);
7820 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307821 if (!ioc->is_driver_loading)
7822 mpt2sas_transport_port_remove(ioc, sas_address,
7823 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007824 _scsih_sas_device_remove(ioc, sas_device);
7825 }
7826 }
7827}
7828
7829/**
7830 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7831 * @ioc: per adapter object
7832 *
7833 * Called during initial loading of the driver.
7834 */
7835static void
7836_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7837{
7838 struct _raid_device *raid_device, *raid_next;
7839 int rc;
7840
7841 list_for_each_entry_safe(raid_device, raid_next,
7842 &ioc->raid_device_list, list) {
7843 if (raid_device->starget)
7844 continue;
7845 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7846 raid_device->id, 0);
7847 if (rc)
7848 _scsih_raid_device_remove(ioc, raid_device);
7849 }
7850}
7851
7852/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307853 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007854 * @ioc: per adapter object
7855 *
7856 * Called during initial loading of the driver.
7857 */
7858static void
7859_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7860{
7861 struct _sas_device *sas_device, *next;
7862 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007863
7864 /* SAS Device List */
7865 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7866 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007867
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307868 if (ioc->hide_drives)
7869 continue;
7870
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307871 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7872 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307873 list_del(&sas_device->list);
7874 kfree(sas_device);
7875 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007876 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307877 if (!ioc->is_driver_loading)
7878 mpt2sas_transport_port_remove(ioc,
7879 sas_device->sas_address,
7880 sas_device->sas_address_parent);
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307881 list_del(&sas_device->list);
7882 kfree(sas_device);
7883 continue;
7884
Eric Moore635374e2009-03-09 01:21:12 -06007885 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307886 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7887 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7888 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007889 }
7890}
7891
7892/**
7893 * _scsih_probe_devices - probing for devices
7894 * @ioc: per adapter object
7895 *
7896 * Called during initial loading of the driver.
7897 */
7898static void
7899_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7900{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307901 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06007902
7903 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7904 return; /* return when IOC doesn't support initiator mode */
7905
7906 _scsih_probe_boot_devices(ioc);
7907
7908 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307909 volume_mapping_flags =
7910 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7911 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7912 if (volume_mapping_flags ==
7913 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06007914 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307915 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007916 } else {
Eric Moore635374e2009-03-09 01:21:12 -06007917 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307918 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007919 }
7920 } else
7921 _scsih_probe_sas(ioc);
7922}
7923
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307924
7925/**
7926 * _scsih_scan_start - scsi lld callback for .scan_start
7927 * @shost: SCSI host pointer
7928 *
7929 * The shost has the ability to discover targets on its own instead
7930 * of scanning the entire bus. In our implemention, we will kick off
7931 * firmware discovery.
7932 */
7933static void
7934_scsih_scan_start(struct Scsi_Host *shost)
7935{
7936 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7937 int rc;
7938
7939 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7940 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
7941
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05307942 if (disable_discovery > 0)
7943 return;
7944
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307945 ioc->start_scan = 1;
7946 rc = mpt2sas_port_enable(ioc);
7947
7948 if (rc != 0)
7949 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
7950}
7951
7952/**
7953 * _scsih_scan_finished - scsi lld callback for .scan_finished
7954 * @shost: SCSI host pointer
7955 * @time: elapsed time of the scan in jiffies
7956 *
7957 * This function will be called periodically until it returns 1 with the
7958 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7959 * we wait for firmware discovery to complete, then return 1.
7960 */
7961static int
7962_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7963{
7964 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7965
sreekanth.reddy@lsi.com4b193172012-07-17 15:56:12 +05307966 if (disable_discovery > 0) {
7967 ioc->is_driver_loading = 0;
7968 ioc->wait_for_discovery_to_complete = 0;
7969 return 1;
7970 }
7971
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307972 if (time >= (300 * HZ)) {
7973 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7974 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
7975 "(timeout=300s)\n", ioc->name);
7976 ioc->is_driver_loading = 0;
7977 return 1;
7978 }
7979
7980 if (ioc->start_scan)
7981 return 0;
7982
7983 if (ioc->start_scan_failed) {
7984 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
7985 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
7986 ioc->is_driver_loading = 0;
7987 ioc->wait_for_discovery_to_complete = 0;
7988 ioc->remove_host = 1;
7989 return 1;
7990 }
7991
7992 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
7993 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7994
7995 if (ioc->wait_for_discovery_to_complete) {
7996 ioc->wait_for_discovery_to_complete = 0;
7997 _scsih_probe_devices(ioc);
7998 }
7999 mpt2sas_base_start_watchdog(ioc);
8000 ioc->is_driver_loading = 0;
8001 return 1;
8002}
8003
8004
Eric Moore635374e2009-03-09 01:21:12 -06008005/**
Eric Moored5d135b2009-05-18 13:02:08 -06008006 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06008007 * @pdev: PCI device struct
8008 * @id: pci device id
8009 *
8010 * Returns 0 success, anything else error.
8011 */
8012static int
Eric Moored5d135b2009-05-18 13:02:08 -06008013_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06008014{
8015 struct MPT2SAS_ADAPTER *ioc;
8016 struct Scsi_Host *shost;
8017
8018 shost = scsi_host_alloc(&scsih_driver_template,
8019 sizeof(struct MPT2SAS_ADAPTER));
8020 if (!shost)
8021 return -ENODEV;
8022
8023 /* init local params */
8024 ioc = shost_priv(shost);
8025 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
8026 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06008027 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06008028 ioc->shost = shost;
8029 ioc->id = mpt_ids++;
8030 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
8031 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308032 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
8033 ioc->is_warpdrive = 1;
8034 ioc->hide_ir_msg = 1;
8035 } else
8036 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06008037 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
8038 ioc->tm_cb_idx = tm_cb_idx;
8039 ioc->ctl_cb_idx = ctl_cb_idx;
8040 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308041 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008042 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05308043 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008044 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308045 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308046 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308047 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06008048 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05308049 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06008050 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05308051 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06008052 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
8053 spin_lock_init(&ioc->scsi_lookup_lock);
8054 spin_lock_init(&ioc->sas_device_lock);
8055 spin_lock_init(&ioc->sas_node_lock);
8056 spin_lock_init(&ioc->fw_event_lock);
8057 spin_lock_init(&ioc->raid_device_lock);
8058
8059 INIT_LIST_HEAD(&ioc->sas_device_list);
8060 INIT_LIST_HEAD(&ioc->sas_device_init_list);
8061 INIT_LIST_HEAD(&ioc->sas_expander_list);
8062 INIT_LIST_HEAD(&ioc->fw_event_list);
8063 INIT_LIST_HEAD(&ioc->raid_device_list);
8064 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308065 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308066 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06008067
8068 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06008069 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06008070 shost->max_lun = max_lun;
8071 shost->transportt = mpt2sas_transport_template;
8072 shost->unique_id = ioc->id;
8073
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308074 if (max_sectors != 0xFFFF) {
8075 if (max_sectors < 64) {
8076 shost->max_sectors = 64;
sreekanth.reddy@lsi.comeece5de2012-07-17 15:56:09 +05308077 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "\
8078 "for max_sectors, range is 64 to 32767. Assigning "\
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308079 "value of 64.\n", ioc->name, max_sectors);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308080 } else if (max_sectors > 32767) {
8081 shost->max_sectors = 32767;
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308082 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8083 "for max_sectors, range is 64 to 8192. Assigning "
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308084 "default value of 32767.\n", ioc->name,
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308085 max_sectors);
8086 } else {
8087 shost->max_sectors = max_sectors & 0xFFFE;
8088 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
8089 "set to %d\n", ioc->name, shost->max_sectors);
8090 }
8091 }
8092
Eric Moore635374e2009-03-09 01:21:12 -06008093 if ((scsi_add_host(shost, &pdev->dev))) {
8094 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8095 ioc->name, __FILE__, __LINE__, __func__);
8096 list_del(&ioc->list);
8097 goto out_add_shost_fail;
8098 }
8099
Eric Moore3c621b32009-05-18 12:59:41 -06008100 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06008101 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308102 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06008103
Eric Moore635374e2009-03-09 01:21:12 -06008104 /* event thread */
8105 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8106 "fw_event%d", ioc->id);
8107 ioc->firmware_event_thread = create_singlethread_workqueue(
8108 ioc->firmware_event_name);
8109 if (!ioc->firmware_event_thread) {
8110 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8111 ioc->name, __FILE__, __LINE__, __func__);
8112 goto out_thread_fail;
8113 }
8114
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308115 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008116 if ((mpt2sas_base_attach(ioc))) {
8117 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8118 ioc->name, __FILE__, __LINE__, __func__);
8119 goto out_attach_fail;
8120 }
8121
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308122 if (ioc->is_warpdrive) {
8123 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8124 ioc->hide_drives = 0;
8125 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8126 ioc->hide_drives = 1;
8127 else {
8128 if (_scsih_get_num_volumes(ioc))
8129 ioc->hide_drives = 1;
8130 else
8131 ioc->hide_drives = 0;
8132 }
8133 } else
8134 ioc->hide_drives = 0;
nagalakshmi.nandigama@lsi.com2cb6fc82011-12-13 09:29:15 +05308135 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308136
Eric Moore635374e2009-03-09 01:21:12 -06008137 return 0;
8138
8139 out_attach_fail:
8140 destroy_workqueue(ioc->firmware_event_thread);
8141 out_thread_fail:
8142 list_del(&ioc->list);
8143 scsi_remove_host(shost);
8144 out_add_shost_fail:
Tomas Henzlf555e052012-04-17 15:24:48 +02008145 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008146 return -ENODEV;
8147}
8148
8149#ifdef CONFIG_PM
8150/**
Eric Moored5d135b2009-05-18 13:02:08 -06008151 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008152 * @pdev: PCI device struct
8153 * @state: PM state change to (usually PCI_D3)
8154 *
8155 * Returns 0 success, anything else error.
8156 */
8157static int
Eric Moored5d135b2009-05-18 13:02:08 -06008158_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008159{
8160 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8161 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308162 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008163
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308164 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008165 scsi_block_requests(shost);
8166 device_state = pci_choose_state(pdev, state);
8167 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8168 "operating state [D%d]\n", ioc->name, pdev,
8169 pci_name(pdev), device_state);
8170
8171 mpt2sas_base_free_resources(ioc);
8172 pci_save_state(pdev);
8173 pci_disable_device(pdev);
8174 pci_set_power_state(pdev, device_state);
8175 return 0;
8176}
8177
8178/**
Eric Moored5d135b2009-05-18 13:02:08 -06008179 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008180 * @pdev: PCI device struct
8181 *
8182 * Returns 0 success, anything else error.
8183 */
8184static int
Eric Moored5d135b2009-05-18 13:02:08 -06008185_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008186{
8187 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8188 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308189 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008190 int r;
8191
8192 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8193 "operating state [D%d]\n", ioc->name, pdev,
8194 pci_name(pdev), device_state);
8195
8196 pci_set_power_state(pdev, PCI_D0);
8197 pci_enable_wake(pdev, PCI_D0, 0);
8198 pci_restore_state(pdev);
8199 ioc->pdev = pdev;
8200 r = mpt2sas_base_map_resources(ioc);
8201 if (r)
8202 return r;
8203
8204 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8205 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308206 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008207 return 0;
8208}
8209#endif /* CONFIG_PM */
8210
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308211/**
8212 * _scsih_pci_error_detected - Called when a PCI error is detected.
8213 * @pdev: PCI device struct
8214 * @state: PCI channel state
8215 *
8216 * Description: Called when a PCI error is detected.
8217 *
8218 * Return value:
8219 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8220 */
8221static pci_ers_result_t
8222_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8223{
8224 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8225 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8226
8227 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8228 ioc->name, state);
8229
8230 switch (state) {
8231 case pci_channel_io_normal:
8232 return PCI_ERS_RESULT_CAN_RECOVER;
8233 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008234 /* Fatal error, prepare for slot reset */
8235 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308236 scsi_block_requests(ioc->shost);
8237 mpt2sas_base_stop_watchdog(ioc);
8238 mpt2sas_base_free_resources(ioc);
8239 return PCI_ERS_RESULT_NEED_RESET;
8240 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008241 /* Permanent error, prepare for device removal */
8242 ioc->pci_error_recovery = 1;
8243 mpt2sas_base_stop_watchdog(ioc);
8244 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308245 return PCI_ERS_RESULT_DISCONNECT;
8246 }
8247 return PCI_ERS_RESULT_NEED_RESET;
8248}
8249
8250/**
8251 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8252 * @pdev: PCI device struct
8253 *
8254 * Description: This routine is called by the pci error recovery
8255 * code after the PCI slot has been reset, just before we
8256 * should resume normal operations.
8257 */
8258static pci_ers_result_t
8259_scsih_pci_slot_reset(struct pci_dev *pdev)
8260{
8261 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8262 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8263 int rc;
8264
8265 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8266 ioc->name);
8267
Eric Moore3cb54692010-07-08 14:44:34 -06008268 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308269 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008270 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308271 rc = mpt2sas_base_map_resources(ioc);
8272 if (rc)
8273 return PCI_ERS_RESULT_DISCONNECT;
8274
8275
8276 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8277 FORCE_BIG_HAMMER);
8278
8279 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8280 (rc == 0) ? "success" : "failed");
8281
8282 if (!rc)
8283 return PCI_ERS_RESULT_RECOVERED;
8284 else
8285 return PCI_ERS_RESULT_DISCONNECT;
8286}
8287
8288/**
8289 * _scsih_pci_resume() - resume normal ops after PCI reset
8290 * @pdev: pointer to PCI device
8291 *
8292 * Called when the error recovery driver tells us that its
8293 * OK to resume normal operation. Use completion to allow
8294 * halted scsi ops to resume.
8295 */
8296static void
8297_scsih_pci_resume(struct pci_dev *pdev)
8298{
8299 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8300 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8301
8302 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8303
8304 pci_cleanup_aer_uncorrect_error_status(pdev);
8305 mpt2sas_base_start_watchdog(ioc);
8306 scsi_unblock_requests(ioc->shost);
8307}
8308
8309/**
8310 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8311 * @pdev: pointer to PCI device
8312 */
8313static pci_ers_result_t
8314_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8315{
8316 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8317 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8318
8319 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8320 ioc->name);
8321
8322 /* TODO - dump whatever for debugging purposes */
8323
8324 /* Request a slot reset. */
8325 return PCI_ERS_RESULT_NEED_RESET;
8326}
8327
8328static struct pci_error_handlers _scsih_err_handler = {
8329 .error_detected = _scsih_pci_error_detected,
8330 .mmio_enabled = _scsih_pci_mmio_enabled,
8331 .slot_reset = _scsih_pci_slot_reset,
8332 .resume = _scsih_pci_resume,
8333};
Eric Moore635374e2009-03-09 01:21:12 -06008334
8335static struct pci_driver scsih_driver = {
8336 .name = MPT2SAS_DRIVER_NAME,
8337 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008338 .probe = _scsih_probe,
8339 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05308340 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308341 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008342#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008343 .suspend = _scsih_suspend,
8344 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008345#endif
8346};
8347
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308348/* raid transport support */
8349static struct raid_function_template mpt2sas_raid_functions = {
8350 .cookie = &scsih_driver_template,
8351 .is_raid = _scsih_is_raid,
8352 .get_resync = _scsih_get_resync,
8353 .get_state = _scsih_get_state,
8354};
Eric Moore635374e2009-03-09 01:21:12 -06008355
8356/**
Eric Moored5d135b2009-05-18 13:02:08 -06008357 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008358 *
8359 * Returns 0 success, anything else error.
8360 */
8361static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008362_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008363{
8364 int error;
8365
8366 mpt_ids = 0;
8367 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8368 MPT2SAS_DRIVER_VERSION);
8369
8370 mpt2sas_transport_template =
8371 sas_attach_transport(&mpt2sas_transport_functions);
8372 if (!mpt2sas_transport_template)
8373 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308374 /* raid transport support */
8375 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8376 if (!mpt2sas_raid_template) {
8377 sas_release_transport(mpt2sas_transport_template);
8378 return -ENODEV;
8379 }
Eric Moore635374e2009-03-09 01:21:12 -06008380
8381 mpt2sas_base_initialize_callback_handler();
8382
8383 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008384 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008385
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008386 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008387 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008388
8389 /* base internal commands callback handler */
8390 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308391 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8392 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008393
8394 /* transport internal commands callback handler */
8395 transport_cb_idx = mpt2sas_base_register_callback_handler(
8396 mpt2sas_transport_done);
8397
Kashyap, Desai744090d2009-10-05 15:56:56 +05308398 /* scsih internal commands callback handler */
8399 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8400
Eric Moore635374e2009-03-09 01:21:12 -06008401 /* configuration page API internal commands callback handler */
8402 config_cb_idx = mpt2sas_base_register_callback_handler(
8403 mpt2sas_config_done);
8404
8405 /* ctl module callback handler */
8406 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8407
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308408 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8409 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308410
8411 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8412 _scsih_tm_volume_tr_complete);
8413
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308414 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8415 _scsih_sas_control_complete);
8416
Eric Moore635374e2009-03-09 01:21:12 -06008417 mpt2sas_ctl_init();
8418
8419 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308420 if (error) {
8421 /* raid transport support */
8422 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008423 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308424 }
Eric Moore635374e2009-03-09 01:21:12 -06008425
8426 return error;
8427}
8428
8429/**
Eric Moored5d135b2009-05-18 13:02:08 -06008430 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008431 *
8432 * Returns 0 success, anything else error.
8433 */
8434static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008435_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008436{
8437 printk(KERN_INFO "mpt2sas version %s unloading\n",
8438 MPT2SAS_DRIVER_VERSION);
8439
8440 pci_unregister_driver(&scsih_driver);
8441
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308442 mpt2sas_ctl_exit();
8443
Eric Moore635374e2009-03-09 01:21:12 -06008444 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8445 mpt2sas_base_release_callback_handler(tm_cb_idx);
8446 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308447 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008448 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308449 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008450 mpt2sas_base_release_callback_handler(config_cb_idx);
8451 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8452
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308453 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308454 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308455 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8456
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308457 /* raid transport support */
8458 raid_class_release(mpt2sas_raid_template);
8459 sas_release_transport(mpt2sas_transport_template);
8460
Eric Moore635374e2009-03-09 01:21:12 -06008461}
8462
Eric Moored5d135b2009-05-18 13:02:08 -06008463module_init(_scsih_init);
8464module_exit(_scsih_exit);