blob: d570573b7963ec47179d15ec7ac613221f0ce4bc [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
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05305 * Copyright (C) 2007-2010 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);
102MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 8192 default=8192");
103
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
Eric Moore635374e2009-03-09 01:21:12 -0600122/**
123 * struct sense_info - common structure for obtaining sense keys
124 * @skey: sense key
125 * @asc: additional sense code
126 * @ascq: additional sense code qualifier
127 */
128struct sense_info {
129 u8 skey;
130 u8 asc;
131 u8 ascq;
132};
133
134
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530135#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530136#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
137#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
Eric Moore635374e2009-03-09 01:21:12 -0600138/**
139 * struct fw_event_work - firmware event struct
140 * @list: link list framework
141 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530142 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600143 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530144 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600145 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530146 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600147 * @ignore: flag meaning this event has been marked to ignore
148 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
149 * @event_data: reply event data payload follows
150 *
151 * This object stored on ioc->fw_event_list.
152 */
153struct fw_event_work {
154 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530155 u8 cancel_pending_work;
156 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600157 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530158 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600159 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530160 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600161 u8 ignore;
162 u16 event;
163 void *event_data;
164};
165
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530166/* raid transport support */
167static struct raid_template *mpt2sas_raid_template;
168
Eric Moore635374e2009-03-09 01:21:12 -0600169/**
170 * struct _scsi_io_transfer - scsi io transfer
171 * @handle: sas device handle (assigned by firmware)
172 * @is_raid: flag set for hidden raid components
173 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
174 * @data_length: data transfer length
175 * @data_dma: dma pointer to data
176 * @sense: sense data
177 * @lun: lun number
178 * @cdb_length: cdb length
179 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600180 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530181 * @VF_ID: virtual function id
182 * @VP_ID: virtual port id
183 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600184 * @sense_length: sense length
185 * @ioc_status: ioc status
186 * @scsi_state: scsi state
187 * @scsi_status: scsi staus
188 * @log_info: log information
189 * @transfer_length: data length transfer when there is a reply message
190 *
191 * Used for sending internal scsi commands to devices within this module.
192 * Refer to _scsi_send_scsi_io().
193 */
194struct _scsi_io_transfer {
195 u16 handle;
196 u8 is_raid;
197 enum dma_data_direction dir;
198 u32 data_length;
199 dma_addr_t data_dma;
200 u8 sense[SCSI_SENSE_BUFFERSIZE];
201 u32 lun;
202 u8 cdb_length;
203 u8 cdb[32];
204 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530205 u8 VF_ID;
206 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600207 u8 valid_reply;
208 /* the following bits are only valid when 'valid_reply = 1' */
209 u32 sense_length;
210 u16 ioc_status;
211 u8 scsi_state;
212 u8 scsi_status;
213 u32 log_info;
214 u32 transfer_length;
215};
216
217/*
218 * The pci device ids are defined in mpi/mpi2_cnfg.h.
219 */
220static struct pci_device_id scsih_pci_table[] = {
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
222 PCI_ANY_ID, PCI_ANY_ID },
223 /* Falcon ~ 2008*/
224 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
225 PCI_ANY_ID, PCI_ANY_ID },
226 /* Liberator ~ 2108 */
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
228 PCI_ANY_ID, PCI_ANY_ID },
229 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
230 PCI_ANY_ID, PCI_ANY_ID },
231 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
232 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530233 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
235 PCI_ANY_ID, PCI_ANY_ID },
236 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
237 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530238 /* Thunderbolt ~ 2208 */
239 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
240 PCI_ANY_ID, PCI_ANY_ID },
241 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
242 PCI_ANY_ID, PCI_ANY_ID },
243 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
244 PCI_ANY_ID, PCI_ANY_ID },
245 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
246 PCI_ANY_ID, PCI_ANY_ID },
247 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
248 PCI_ANY_ID, PCI_ANY_ID },
249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
250 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530251 /* Mustang ~ 2308 */
252 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530253 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530254 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
255 PCI_ANY_ID, PCI_ANY_ID },
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530257 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530258 /* SSS6200 */
259 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
260 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600261 {0} /* Terminating entry */
262};
263MODULE_DEVICE_TABLE(pci, scsih_pci_table);
264
265/**
Eric Moored5d135b2009-05-18 13:02:08 -0600266 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600267 *
268 * Note: The logging levels are defined in mpt2sas_debug.h.
269 */
270static int
Eric Moored5d135b2009-05-18 13:02:08 -0600271_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600272{
273 int ret = param_set_int(val, kp);
274 struct MPT2SAS_ADAPTER *ioc;
275
276 if (ret)
277 return ret;
278
279 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600280 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600281 ioc->logging_level = logging_level;
282 return 0;
283}
Eric Moored5d135b2009-05-18 13:02:08 -0600284module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600285 &logging_level, 0644);
286
287/**
288 * _scsih_srch_boot_sas_address - search based on sas_address
289 * @sas_address: sas address
290 * @boot_device: boot device object from bios page 2
291 *
292 * Returns 1 when there's a match, 0 means no match.
293 */
294static inline int
295_scsih_srch_boot_sas_address(u64 sas_address,
296 Mpi2BootDeviceSasWwid_t *boot_device)
297{
298 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
299}
300
301/**
302 * _scsih_srch_boot_device_name - search based on device name
303 * @device_name: device name specified in INDENTIFY fram
304 * @boot_device: boot device object from bios page 2
305 *
306 * Returns 1 when there's a match, 0 means no match.
307 */
308static inline int
309_scsih_srch_boot_device_name(u64 device_name,
310 Mpi2BootDeviceDeviceName_t *boot_device)
311{
312 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
313}
314
315/**
316 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
317 * @enclosure_logical_id: enclosure logical id
318 * @slot_number: slot number
319 * @boot_device: boot device object from bios page 2
320 *
321 * Returns 1 when there's a match, 0 means no match.
322 */
323static inline int
324_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
325 Mpi2BootDeviceEnclosureSlot_t *boot_device)
326{
327 return (enclosure_logical_id == le64_to_cpu(boot_device->
328 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
329 SlotNumber)) ? 1 : 0;
330}
331
332/**
333 * _scsih_is_boot_device - search for matching boot device.
334 * @sas_address: sas address
335 * @device_name: device name specified in INDENTIFY fram
336 * @enclosure_logical_id: enclosure logical id
337 * @slot_number: slot number
338 * @form: specifies boot device form
339 * @boot_device: boot device object from bios page 2
340 *
341 * Returns 1 when there's a match, 0 means no match.
342 */
343static int
344_scsih_is_boot_device(u64 sas_address, u64 device_name,
345 u64 enclosure_logical_id, u16 slot, u8 form,
346 Mpi2BiosPage2BootDevice_t *boot_device)
347{
348 int rc = 0;
349
350 switch (form) {
351 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
352 if (!sas_address)
353 break;
354 rc = _scsih_srch_boot_sas_address(
355 sas_address, &boot_device->SasWwid);
356 break;
357 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
358 if (!enclosure_logical_id)
359 break;
360 rc = _scsih_srch_boot_encl_slot(
361 enclosure_logical_id,
362 slot, &boot_device->EnclosureSlot);
363 break;
364 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
365 if (!device_name)
366 break;
367 rc = _scsih_srch_boot_device_name(
368 device_name, &boot_device->DeviceName);
369 break;
370 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
371 break;
372 }
373
374 return rc;
375}
376
377/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530378 * _scsih_get_sas_address - set the sas_address for given device handle
379 * @handle: device handle
380 * @sas_address: sas address
381 *
382 * Returns 0 success, non-zero when failure
383 */
384static int
385_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
386 u64 *sas_address)
387{
388 Mpi2SasDevicePage0_t sas_device_pg0;
389 Mpi2ConfigReply_t mpi_reply;
390 u32 ioc_status;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530391 *sas_address = 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530392
393 if (handle <= ioc->sas_hba.num_phys) {
394 *sas_address = ioc->sas_hba.sas_address;
395 return 0;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530396 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530397
398 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
399 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530400 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
401 __FILE__, __LINE__, __func__);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530402 return -ENXIO;
403 }
404
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530405 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
406 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
407 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
408 return 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530409 }
410
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530411 /* we hit this becuase the given parent handle doesn't exist */
412 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
413 return -ENXIO;
414 /* else error case */
415 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
416 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
417 __FILE__, __LINE__, __func__);
418 return -EIO;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530419}
420
421/**
Eric Moore635374e2009-03-09 01:21:12 -0600422 * _scsih_determine_boot_device - determine boot device.
423 * @ioc: per adapter object
424 * @device: either sas_device or raid_device object
425 * @is_raid: [flag] 1 = raid object, 0 = sas object
426 *
427 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300428 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600429 * There are primary, alternate, and current entries in bios page 2. The order
430 * priority is primary, alternate, then current. This routine saves
431 * the corresponding device object and is_raid flag in the ioc object.
432 * The saved data to be used later in _scsih_probe_boot_devices().
433 */
434static void
435_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
436 void *device, u8 is_raid)
437{
438 struct _sas_device *sas_device;
439 struct _raid_device *raid_device;
440 u64 sas_address;
441 u64 device_name;
442 u64 enclosure_logical_id;
443 u16 slot;
444
445 /* only process this function when driver loads */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530446 if (!ioc->is_driver_loading)
447 return;
448
449 /* no Bios, return immediately */
450 if (!ioc->bios_pg3.BiosVersion)
Eric Moore635374e2009-03-09 01:21:12 -0600451 return;
452
453 if (!is_raid) {
454 sas_device = device;
455 sas_address = sas_device->sas_address;
456 device_name = sas_device->device_name;
457 enclosure_logical_id = sas_device->enclosure_logical_id;
458 slot = sas_device->slot;
459 } else {
460 raid_device = device;
461 sas_address = raid_device->wwid;
462 device_name = 0;
463 enclosure_logical_id = 0;
464 slot = 0;
465 }
466
467 if (!ioc->req_boot_device.device) {
468 if (_scsih_is_boot_device(sas_address, device_name,
469 enclosure_logical_id, slot,
470 (ioc->bios_pg2.ReqBootDeviceForm &
471 MPI2_BIOSPAGE2_FORM_MASK),
472 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530473 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600474 "%s: req_boot_device(0x%016llx)\n",
475 ioc->name, __func__,
476 (unsigned long long)sas_address));
477 ioc->req_boot_device.device = device;
478 ioc->req_boot_device.is_raid = is_raid;
479 }
480 }
481
482 if (!ioc->req_alt_boot_device.device) {
483 if (_scsih_is_boot_device(sas_address, device_name,
484 enclosure_logical_id, slot,
485 (ioc->bios_pg2.ReqAltBootDeviceForm &
486 MPI2_BIOSPAGE2_FORM_MASK),
487 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530488 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600489 "%s: req_alt_boot_device(0x%016llx)\n",
490 ioc->name, __func__,
491 (unsigned long long)sas_address));
492 ioc->req_alt_boot_device.device = device;
493 ioc->req_alt_boot_device.is_raid = is_raid;
494 }
495 }
496
497 if (!ioc->current_boot_device.device) {
498 if (_scsih_is_boot_device(sas_address, device_name,
499 enclosure_logical_id, slot,
500 (ioc->bios_pg2.CurrentBootDeviceForm &
501 MPI2_BIOSPAGE2_FORM_MASK),
502 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530503 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600504 "%s: current_boot_device(0x%016llx)\n",
505 ioc->name, __func__,
506 (unsigned long long)sas_address));
507 ioc->current_boot_device.device = device;
508 ioc->current_boot_device.is_raid = is_raid;
509 }
510 }
511}
512
513/**
514 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
515 * @ioc: per adapter object
516 * @sas_address: sas address
517 * Context: Calling function should acquire ioc->sas_device_lock
518 *
519 * This searches for sas_device based on sas_address, then return sas_device
520 * object.
521 */
522struct _sas_device *
523mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
524 u64 sas_address)
525{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530526 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600527
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530528 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
529 if (sas_device->sas_address == sas_address)
530 return 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_init_list, list)
533 if (sas_device->sas_address == sas_address)
534 return sas_device;
535
536 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600537}
538
539/**
540 * _scsih_sas_device_find_by_handle - sas device search
541 * @ioc: per adapter object
542 * @handle: sas device handle (assigned by firmware)
543 * Context: Calling function should acquire ioc->sas_device_lock
544 *
545 * This searches for sas_device based on sas_address, then return sas_device
546 * object.
547 */
548static struct _sas_device *
549_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
550{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530551 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600552
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530553 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
554 if (sas_device->handle == handle)
555 return 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_init_list, list)
558 if (sas_device->handle == handle)
559 return sas_device;
560
561 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600562}
563
564/**
565 * _scsih_sas_device_remove - remove sas_device from list.
566 * @ioc: per adapter object
567 * @sas_device: the sas_device object
568 * Context: This function will acquire ioc->sas_device_lock.
569 *
570 * Removing object and freeing associated memory from the ioc->sas_device_list.
571 */
572static void
573_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
574 struct _sas_device *sas_device)
575{
576 unsigned long flags;
577
Kashyap, Desai980ead32010-04-08 17:55:22 +0530578 if (!sas_device)
579 return;
580
Eric Moore635374e2009-03-09 01:21:12 -0600581 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530582 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
583 sas_device->sas_address)) {
584 list_del(&sas_device->list);
585 kfree(sas_device);
586 }
Eric Moore635374e2009-03-09 01:21:12 -0600587 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
588}
589
590/**
591 * _scsih_sas_device_add - insert sas_device to the list.
592 * @ioc: per adapter object
593 * @sas_device: the sas_device object
594 * Context: This function will acquire ioc->sas_device_lock.
595 *
596 * Adding new object to the ioc->sas_device_list.
597 */
598static void
599_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
600 struct _sas_device *sas_device)
601{
602 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600603
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530604 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600605 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
606 sas_device->handle, (unsigned long long)sas_device->sas_address));
607
608 spin_lock_irqsave(&ioc->sas_device_lock, flags);
609 list_add_tail(&sas_device->list, &ioc->sas_device_list);
610 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
611
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530612 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530613 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -0600614 _scsih_sas_device_remove(ioc, sas_device);
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530615 } else if (!sas_device->starget) {
616 if (!ioc->is_driver_loading)
617 mpt2sas_transport_port_remove(ioc,
618 sas_device->sas_address,
619 sas_device->sas_address_parent);
620 _scsih_sas_device_remove(ioc, sas_device);
621 }
Eric Moore635374e2009-03-09 01:21:12 -0600622}
623
624/**
625 * _scsih_sas_device_init_add - insert sas_device to the list.
626 * @ioc: per adapter object
627 * @sas_device: the sas_device object
628 * Context: This function will acquire ioc->sas_device_lock.
629 *
630 * Adding new object at driver load time to the ioc->sas_device_init_list.
631 */
632static void
633_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
634 struct _sas_device *sas_device)
635{
636 unsigned long flags;
637
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530638 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600639 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
640 sas_device->handle, (unsigned long long)sas_device->sas_address));
641
642 spin_lock_irqsave(&ioc->sas_device_lock, flags);
643 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
644 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
645 _scsih_determine_boot_device(ioc, sas_device, 0);
646}
647
648/**
Eric Moore635374e2009-03-09 01:21:12 -0600649 * _scsih_raid_device_find_by_id - raid device search
650 * @ioc: per adapter object
651 * @id: sas device target id
652 * @channel: sas device channel
653 * Context: Calling function should acquire ioc->raid_device_lock
654 *
655 * This searches for raid_device based on target id, then return raid_device
656 * object.
657 */
658static struct _raid_device *
659_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
660{
661 struct _raid_device *raid_device, *r;
662
663 r = NULL;
664 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
665 if (raid_device->id == id && raid_device->channel == channel) {
666 r = raid_device;
667 goto out;
668 }
669 }
670
671 out:
672 return r;
673}
674
675/**
676 * _scsih_raid_device_find_by_handle - raid device search
677 * @ioc: per adapter object
678 * @handle: sas device handle (assigned by firmware)
679 * Context: Calling function should acquire ioc->raid_device_lock
680 *
681 * This searches for raid_device based on handle, then return raid_device
682 * object.
683 */
684static struct _raid_device *
685_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
686{
687 struct _raid_device *raid_device, *r;
688
689 r = NULL;
690 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
691 if (raid_device->handle != handle)
692 continue;
693 r = raid_device;
694 goto out;
695 }
696
697 out:
698 return r;
699}
700
701/**
702 * _scsih_raid_device_find_by_wwid - raid device search
703 * @ioc: per adapter object
704 * @handle: sas device handle (assigned by firmware)
705 * Context: Calling function should acquire ioc->raid_device_lock
706 *
707 * This searches for raid_device based on wwid, then return raid_device
708 * object.
709 */
710static struct _raid_device *
711_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
712{
713 struct _raid_device *raid_device, *r;
714
715 r = NULL;
716 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
717 if (raid_device->wwid != wwid)
718 continue;
719 r = raid_device;
720 goto out;
721 }
722
723 out:
724 return r;
725}
726
727/**
728 * _scsih_raid_device_add - add raid_device object
729 * @ioc: per adapter object
730 * @raid_device: raid_device object
731 *
732 * This is added to the raid_device_list link list.
733 */
734static void
735_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
736 struct _raid_device *raid_device)
737{
738 unsigned long flags;
739
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530740 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600741 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
742 raid_device->handle, (unsigned long long)raid_device->wwid));
743
744 spin_lock_irqsave(&ioc->raid_device_lock, flags);
745 list_add_tail(&raid_device->list, &ioc->raid_device_list);
746 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
747}
748
749/**
750 * _scsih_raid_device_remove - delete raid_device object
751 * @ioc: per adapter object
752 * @raid_device: raid_device object
753 *
754 * This is removed from the raid_device_list link list.
755 */
756static void
757_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
758 struct _raid_device *raid_device)
759{
760 unsigned long flags;
761
762 spin_lock_irqsave(&ioc->raid_device_lock, flags);
763 list_del(&raid_device->list);
764 memset(raid_device, 0, sizeof(struct _raid_device));
765 kfree(raid_device);
766 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
767}
768
769/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530770 * mpt2sas_scsih_expander_find_by_handle - expander device search
771 * @ioc: per adapter object
772 * @handle: expander handle (assigned by firmware)
773 * Context: Calling function should acquire ioc->sas_device_lock
774 *
775 * This searches for expander device based on handle, then returns the
776 * sas_node object.
777 */
778struct _sas_node *
779mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
780{
781 struct _sas_node *sas_expander, *r;
782
783 r = NULL;
784 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
785 if (sas_expander->handle != handle)
786 continue;
787 r = sas_expander;
788 goto out;
789 }
790 out:
791 return r;
792}
793
794/**
Eric Moore635374e2009-03-09 01:21:12 -0600795 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
796 * @ioc: per adapter object
797 * @sas_address: sas address
798 * Context: Calling function should acquire ioc->sas_node_lock.
799 *
800 * This searches for expander device based on sas_address, then returns the
801 * sas_node object.
802 */
803struct _sas_node *
804mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
805 u64 sas_address)
806{
807 struct _sas_node *sas_expander, *r;
808
809 r = NULL;
810 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
811 if (sas_expander->sas_address != sas_address)
812 continue;
813 r = sas_expander;
814 goto out;
815 }
816 out:
817 return r;
818}
819
820/**
821 * _scsih_expander_node_add - insert expander device to the list.
822 * @ioc: per adapter object
823 * @sas_expander: the sas_device object
824 * Context: This function will acquire ioc->sas_node_lock.
825 *
826 * Adding new object to the ioc->sas_expander_list.
827 *
828 * Return nothing.
829 */
830static void
831_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
832 struct _sas_node *sas_expander)
833{
834 unsigned long flags;
835
836 spin_lock_irqsave(&ioc->sas_node_lock, flags);
837 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
838 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
839}
840
841/**
842 * _scsih_is_end_device - determines if device is an end device
843 * @device_info: bitfield providing information about the device.
844 * Context: none
845 *
846 * Returns 1 if end device.
847 */
848static int
849_scsih_is_end_device(u32 device_info)
850{
851 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
852 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
853 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
854 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
855 return 1;
856 else
857 return 0;
858}
859
860/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530861 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600862 * @ioc: per adapter object
863 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600864 *
865 * Returns the smid stored scmd pointer.
866 */
867static struct scsi_cmnd *
868_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
869{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530870 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600871}
872
873/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530874 * _scsih_scsi_lookup_get_clear - returns scmd entry
875 * @ioc: per adapter object
876 * @smid: system request message index
877 *
878 * Returns the smid stored scmd pointer.
879 * Then will derefrence the stored scmd pointer.
880 */
881static inline struct scsi_cmnd *
882_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
883{
884 unsigned long flags;
885 struct scsi_cmnd *scmd;
886
887 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
888 scmd = ioc->scsi_lookup[smid - 1].scmd;
889 ioc->scsi_lookup[smid - 1].scmd = NULL;
890 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
891
892 return scmd;
893}
894
895/**
Eric Moore635374e2009-03-09 01:21:12 -0600896 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
897 * @ioc: per adapter object
898 * @smid: system request message index
899 * @scmd: pointer to scsi command object
900 * Context: This function will acquire ioc->scsi_lookup_lock.
901 *
902 * This will search for a scmd pointer in the scsi_lookup array,
903 * returning the revelent smid. A returned value of zero means invalid.
904 */
905static u16
906_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
907 *scmd)
908{
909 u16 smid;
910 unsigned long flags;
911 int i;
912
913 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
914 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530915 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600916 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530917 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600918 goto out;
919 }
920 }
921 out:
922 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
923 return smid;
924}
925
926/**
927 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
928 * @ioc: per adapter object
929 * @id: target id
930 * @channel: channel
931 * Context: This function will acquire ioc->scsi_lookup_lock.
932 *
933 * This will search for a matching channel:id in the scsi_lookup array,
934 * returning 1 if found.
935 */
936static u8
937_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
938 int channel)
939{
940 u8 found;
941 unsigned long flags;
942 int i;
943
944 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
945 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530946 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600947 if (ioc->scsi_lookup[i].scmd &&
948 (ioc->scsi_lookup[i].scmd->device->id == id &&
949 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
950 found = 1;
951 goto out;
952 }
953 }
954 out:
955 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
956 return found;
957}
958
959/**
Eric Moore993e0da2009-05-18 13:00:45 -0600960 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
961 * @ioc: per adapter object
962 * @id: target id
963 * @lun: lun number
964 * @channel: channel
965 * Context: This function will acquire ioc->scsi_lookup_lock.
966 *
967 * This will search for a matching channel:id:lun in the scsi_lookup array,
968 * returning 1 if found.
969 */
970static u8
971_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
972 unsigned int lun, int channel)
973{
974 u8 found;
975 unsigned long flags;
976 int i;
977
978 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
979 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530980 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600981 if (ioc->scsi_lookup[i].scmd &&
982 (ioc->scsi_lookup[i].scmd->device->id == id &&
983 ioc->scsi_lookup[i].scmd->device->channel == channel &&
984 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
985 found = 1;
986 goto out;
987 }
988 }
989 out:
990 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
991 return found;
992}
993
994/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530995 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -0600996 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530997 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -0600998 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530999 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001000 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301001static struct chain_tracker *
1002_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001003{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301004 struct chain_tracker *chain_req;
1005 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001006
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301007 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1008 if (list_empty(&ioc->free_chain_list)) {
1009 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1010 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
1011 ioc->name);
1012 return NULL;
1013 }
1014 chain_req = list_entry(ioc->free_chain_list.next,
1015 struct chain_tracker, tracker_list);
1016 list_del_init(&chain_req->tracker_list);
1017 list_add_tail(&chain_req->tracker_list,
1018 &ioc->scsi_lookup[smid - 1].chain_list);
1019 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1020 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001021}
1022
1023/**
1024 * _scsih_build_scatter_gather - main sg creation routine
1025 * @ioc: per adapter object
1026 * @scmd: scsi command
1027 * @smid: system request message index
1028 * Context: none.
1029 *
1030 * The main routine that builds scatter gather table from a given
1031 * scsi request sent via the .queuecommand main handler.
1032 *
1033 * Returns 0 success, anything else error
1034 */
1035static int
1036_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1037 struct scsi_cmnd *scmd, u16 smid)
1038{
1039 Mpi2SCSIIORequest_t *mpi_request;
1040 dma_addr_t chain_dma;
1041 struct scatterlist *sg_scmd;
1042 void *sg_local, *chain;
1043 u32 chain_offset;
1044 u32 chain_length;
1045 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001046 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001047 u32 sges_in_segment;
1048 u32 sgl_flags;
1049 u32 sgl_flags_last_element;
1050 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301051 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001052
1053 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1054
1055 /* init scatter gather flags */
1056 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1057 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1058 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1059 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1060 << MPI2_SGE_FLAGS_SHIFT;
1061 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1062 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1063 << MPI2_SGE_FLAGS_SHIFT;
1064 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1065
1066 sg_scmd = scsi_sglist(scmd);
1067 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001068 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001069 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1070 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1071 return -ENOMEM;
1072 }
1073
1074 sg_local = &mpi_request->SGL;
1075 sges_in_segment = ioc->max_sges_in_main_message;
1076 if (sges_left <= sges_in_segment)
1077 goto fill_in_last_segment;
1078
1079 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1080 (sges_in_segment * ioc->sge_size))/4;
1081
1082 /* fill in main message segment when there is a chain following */
1083 while (sges_in_segment) {
1084 if (sges_in_segment == 1)
1085 ioc->base_add_sg_single(sg_local,
1086 sgl_flags_last_element | sg_dma_len(sg_scmd),
1087 sg_dma_address(sg_scmd));
1088 else
1089 ioc->base_add_sg_single(sg_local, sgl_flags |
1090 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1091 sg_scmd = sg_next(sg_scmd);
1092 sg_local += ioc->sge_size;
1093 sges_left--;
1094 sges_in_segment--;
1095 }
1096
1097 /* initializing the chain flags and pointers */
1098 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301099 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1100 if (!chain_req)
1101 return -1;
1102 chain = chain_req->chain_buffer;
1103 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001104 do {
1105 sges_in_segment = (sges_left <=
1106 ioc->max_sges_in_chain_message) ? sges_left :
1107 ioc->max_sges_in_chain_message;
1108 chain_offset = (sges_left == sges_in_segment) ?
1109 0 : (sges_in_segment * ioc->sge_size)/4;
1110 chain_length = sges_in_segment * ioc->sge_size;
1111 if (chain_offset) {
1112 chain_offset = chain_offset <<
1113 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1114 chain_length += ioc->sge_size;
1115 }
1116 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1117 chain_length, chain_dma);
1118 sg_local = chain;
1119 if (!chain_offset)
1120 goto fill_in_last_segment;
1121
1122 /* fill in chain segments */
1123 while (sges_in_segment) {
1124 if (sges_in_segment == 1)
1125 ioc->base_add_sg_single(sg_local,
1126 sgl_flags_last_element |
1127 sg_dma_len(sg_scmd),
1128 sg_dma_address(sg_scmd));
1129 else
1130 ioc->base_add_sg_single(sg_local, sgl_flags |
1131 sg_dma_len(sg_scmd),
1132 sg_dma_address(sg_scmd));
1133 sg_scmd = sg_next(sg_scmd);
1134 sg_local += ioc->sge_size;
1135 sges_left--;
1136 sges_in_segment--;
1137 }
1138
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301139 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1140 if (!chain_req)
1141 return -1;
1142 chain = chain_req->chain_buffer;
1143 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001144 } while (1);
1145
1146
1147 fill_in_last_segment:
1148
1149 /* fill the last segment */
1150 while (sges_left) {
1151 if (sges_left == 1)
1152 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1153 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1154 else
1155 ioc->base_add_sg_single(sg_local, sgl_flags |
1156 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1157 sg_scmd = sg_next(sg_scmd);
1158 sg_local += ioc->sge_size;
1159 sges_left--;
1160 }
1161
1162 return 0;
1163}
1164
1165/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301166 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001167 * @sdev: scsi device struct
1168 * @qdepth: requested queue depth
1169 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301170 *
1171 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001172 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301173static void
1174_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001175{
1176 struct Scsi_Host *shost = sdev->host;
1177 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301178 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1179 struct MPT2SAS_DEVICE *sas_device_priv_data;
1180 struct MPT2SAS_TARGET *sas_target_priv_data;
1181 struct _sas_device *sas_device;
1182 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001183
1184 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301185
1186 /* limit max device queue for SATA to 32 */
1187 sas_device_priv_data = sdev->hostdata;
1188 if (!sas_device_priv_data)
1189 goto not_sata;
1190 sas_target_priv_data = sas_device_priv_data->sas_target;
1191 if (!sas_target_priv_data)
1192 goto not_sata;
1193 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1194 goto not_sata;
1195 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1196 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1197 sas_device_priv_data->sas_target->sas_address);
1198 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1199 if (sas_device && sas_device->device_info &
1200 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1201 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1202
1203 not_sata:
1204
Eric Moore635374e2009-03-09 01:21:12 -06001205 if (!sdev->tagged_supported)
1206 max_depth = 1;
1207 if (qdepth > max_depth)
1208 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301209 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1210}
1211
1212/**
1213 * _scsih_change_queue_depth - setting device queue depth
1214 * @sdev: scsi device struct
1215 * @qdepth: requested queue depth
1216 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1217 * (see include/scsi/scsi_host.h for definition)
1218 *
1219 * Returns queue depth.
1220 */
1221static int
1222_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1223{
1224 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1225 _scsih_adjust_queue_depth(sdev, qdepth);
1226 else if (reason == SCSI_QDEPTH_QFULL)
1227 scsi_track_queue_full(sdev, qdepth);
1228 else
1229 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001230
1231 if (sdev->inquiry_len > 7)
1232 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1233 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1234 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1235 sdev->ordered_tags, sdev->scsi_level,
1236 (sdev->inquiry[7] & 2) >> 1);
1237
1238 return sdev->queue_depth;
1239}
1240
1241/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301242 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001243 * @sdev: scsi device struct
1244 * @tag_type: requested tag type
1245 *
1246 * Returns queue tag type.
1247 */
1248static int
Eric Moored5d135b2009-05-18 13:02:08 -06001249_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001250{
1251 if (sdev->tagged_supported) {
1252 scsi_set_tag_type(sdev, tag_type);
1253 if (tag_type)
1254 scsi_activate_tcq(sdev, sdev->queue_depth);
1255 else
1256 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1257 } else
1258 tag_type = 0;
1259
1260 return tag_type;
1261}
1262
1263/**
Eric Moored5d135b2009-05-18 13:02:08 -06001264 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001265 * @starget: scsi target struct
1266 *
1267 * Returns 0 if ok. Any other return is assumed to be an error and
1268 * the device is ignored.
1269 */
1270static int
Eric Moored5d135b2009-05-18 13:02:08 -06001271_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001272{
1273 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1274 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1275 struct MPT2SAS_TARGET *sas_target_priv_data;
1276 struct _sas_device *sas_device;
1277 struct _raid_device *raid_device;
1278 unsigned long flags;
1279 struct sas_rphy *rphy;
1280
1281 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1282 if (!sas_target_priv_data)
1283 return -ENOMEM;
1284
1285 starget->hostdata = sas_target_priv_data;
1286 sas_target_priv_data->starget = starget;
1287 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1288
1289 /* RAID volumes */
1290 if (starget->channel == RAID_CHANNEL) {
1291 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1292 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1293 starget->channel);
1294 if (raid_device) {
1295 sas_target_priv_data->handle = raid_device->handle;
1296 sas_target_priv_data->sas_address = raid_device->wwid;
1297 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301298 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001299 raid_device->starget = starget;
1300 }
1301 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1302 return 0;
1303 }
1304
1305 /* sas/sata devices */
1306 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1307 rphy = dev_to_rphy(starget->dev.parent);
1308 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1309 rphy->identify.sas_address);
1310
1311 if (sas_device) {
1312 sas_target_priv_data->handle = sas_device->handle;
1313 sas_target_priv_data->sas_address = sas_device->sas_address;
1314 sas_device->starget = starget;
1315 sas_device->id = starget->id;
1316 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301317 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001318 sas_target_priv_data->flags |=
1319 MPT_TARGET_FLAGS_RAID_COMPONENT;
1320 }
1321 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1322
1323 return 0;
1324}
1325
1326/**
Eric Moored5d135b2009-05-18 13:02:08 -06001327 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001328 * @starget: scsi target struct
1329 *
1330 * Returns nothing.
1331 */
1332static void
Eric Moored5d135b2009-05-18 13:02:08 -06001333_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001334{
1335 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1336 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1337 struct MPT2SAS_TARGET *sas_target_priv_data;
1338 struct _sas_device *sas_device;
1339 struct _raid_device *raid_device;
1340 unsigned long flags;
1341 struct sas_rphy *rphy;
1342
1343 sas_target_priv_data = starget->hostdata;
1344 if (!sas_target_priv_data)
1345 return;
1346
1347 if (starget->channel == RAID_CHANNEL) {
1348 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1349 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1350 starget->channel);
1351 if (raid_device) {
1352 raid_device->starget = NULL;
1353 raid_device->sdev = NULL;
1354 }
1355 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1356 goto out;
1357 }
1358
1359 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1360 rphy = dev_to_rphy(starget->dev.parent);
1361 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1362 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001363 if (sas_device && (sas_device->starget == starget) &&
1364 (sas_device->id == starget->id) &&
1365 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001366 sas_device->starget = NULL;
1367
1368 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1369
1370 out:
1371 kfree(sas_target_priv_data);
1372 starget->hostdata = NULL;
1373}
1374
1375/**
Eric Moored5d135b2009-05-18 13:02:08 -06001376 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001377 * @sdev: scsi device struct
1378 *
1379 * Returns 0 if ok. Any other return is assumed to be an error and
1380 * the device is ignored.
1381 */
1382static int
Eric Moored5d135b2009-05-18 13:02:08 -06001383_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001384{
1385 struct Scsi_Host *shost;
1386 struct MPT2SAS_ADAPTER *ioc;
1387 struct MPT2SAS_TARGET *sas_target_priv_data;
1388 struct MPT2SAS_DEVICE *sas_device_priv_data;
1389 struct scsi_target *starget;
1390 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001391 unsigned long flags;
1392
1393 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1394 if (!sas_device_priv_data)
1395 return -ENOMEM;
1396
1397 sas_device_priv_data->lun = sdev->lun;
1398 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1399
1400 starget = scsi_target(sdev);
1401 sas_target_priv_data = starget->hostdata;
1402 sas_target_priv_data->num_luns++;
1403 sas_device_priv_data->sas_target = sas_target_priv_data;
1404 sdev->hostdata = sas_device_priv_data;
1405 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1406 sdev->no_uld_attach = 1;
1407
1408 shost = dev_to_shost(&starget->dev);
1409 ioc = shost_priv(shost);
1410 if (starget->channel == RAID_CHANNEL) {
1411 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1412 raid_device = _scsih_raid_device_find_by_id(ioc,
1413 starget->id, starget->channel);
1414 if (raid_device)
1415 raid_device->sdev = sdev; /* raid is single lun */
1416 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001417 }
1418
Eric Moore635374e2009-03-09 01:21:12 -06001419 return 0;
1420}
1421
1422/**
Eric Moored5d135b2009-05-18 13:02:08 -06001423 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001424 * @sdev: scsi device struct
1425 *
1426 * Returns nothing.
1427 */
1428static void
Eric Moored5d135b2009-05-18 13:02:08 -06001429_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001430{
1431 struct MPT2SAS_TARGET *sas_target_priv_data;
1432 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301433 struct Scsi_Host *shost;
1434 struct MPT2SAS_ADAPTER *ioc;
1435 struct _sas_device *sas_device;
1436 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001437
1438 if (!sdev->hostdata)
1439 return;
1440
1441 starget = scsi_target(sdev);
1442 sas_target_priv_data = starget->hostdata;
1443 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301444
1445 shost = dev_to_shost(&starget->dev);
1446 ioc = shost_priv(shost);
1447
1448 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1449 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1450 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1451 sas_target_priv_data->sas_address);
1452 if (sas_device)
1453 sas_device->starget = NULL;
1454 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1455 }
1456
Eric Moore635374e2009-03-09 01:21:12 -06001457 kfree(sdev->hostdata);
1458 sdev->hostdata = NULL;
1459}
1460
1461/**
Eric Moored5d135b2009-05-18 13:02:08 -06001462 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001463 * @ioc: per adapter object
1464 * @sas_device: the sas_device object
1465 * @sdev: scsi device struct
1466 */
1467static void
Eric Moored5d135b2009-05-18 13:02:08 -06001468_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001469 struct _sas_device *sas_device, struct scsi_device *sdev)
1470{
1471 Mpi2ConfigReply_t mpi_reply;
1472 Mpi2SasDevicePage0_t sas_device_pg0;
1473 u32 ioc_status;
1474 u16 flags;
1475 u32 device_info;
1476
1477 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1478 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1479 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1480 ioc->name, __FILE__, __LINE__, __func__);
1481 return;
1482 }
1483
1484 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1485 MPI2_IOCSTATUS_MASK;
1486 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1487 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1488 ioc->name, __FILE__, __LINE__, __func__);
1489 return;
1490 }
1491
1492 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301493 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001494
1495 sdev_printk(KERN_INFO, sdev,
1496 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1497 "sw_preserve(%s)\n",
1498 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1499 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1500 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1501 "n",
1502 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1503 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1504 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1505}
1506
1507/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301508 * _scsih_is_raid - return boolean indicating device is raid volume
1509 * @dev the device struct object
1510 */
1511static int
1512_scsih_is_raid(struct device *dev)
1513{
1514 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301515 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301516
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301517 if (ioc->is_warpdrive)
1518 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301519 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1520}
1521
1522/**
1523 * _scsih_get_resync - get raid volume resync percent complete
1524 * @dev the device struct object
1525 */
1526static void
1527_scsih_get_resync(struct device *dev)
1528{
1529 struct scsi_device *sdev = to_scsi_device(dev);
1530 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1531 static struct _raid_device *raid_device;
1532 unsigned long flags;
1533 Mpi2RaidVolPage0_t vol_pg0;
1534 Mpi2ConfigReply_t mpi_reply;
1535 u32 volume_status_flags;
1536 u8 percent_complete = 0;
1537
1538 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1539 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1540 sdev->channel);
1541 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1542
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301543 if (!raid_device || ioc->is_warpdrive)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301544 goto out;
1545
1546 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1547 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1548 sizeof(Mpi2RaidVolPage0_t))) {
1549 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1550 ioc->name, __FILE__, __LINE__, __func__);
1551 goto out;
1552 }
1553
1554 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1555 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1556 percent_complete = raid_device->percent_complete;
1557 out:
1558 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1559}
1560
1561/**
1562 * _scsih_get_state - get raid volume level
1563 * @dev the device struct object
1564 */
1565static void
1566_scsih_get_state(struct device *dev)
1567{
1568 struct scsi_device *sdev = to_scsi_device(dev);
1569 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1570 static struct _raid_device *raid_device;
1571 unsigned long flags;
1572 Mpi2RaidVolPage0_t vol_pg0;
1573 Mpi2ConfigReply_t mpi_reply;
1574 u32 volstate;
1575 enum raid_state state = RAID_STATE_UNKNOWN;
1576
1577 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1578 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1579 sdev->channel);
1580 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1581
1582 if (!raid_device)
1583 goto out;
1584
1585 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1586 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1587 sizeof(Mpi2RaidVolPage0_t))) {
1588 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1589 ioc->name, __FILE__, __LINE__, __func__);
1590 goto out;
1591 }
1592
1593 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1594 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1595 state = RAID_STATE_RESYNCING;
1596 goto out;
1597 }
1598
1599 switch (vol_pg0.VolumeState) {
1600 case MPI2_RAID_VOL_STATE_OPTIMAL:
1601 case MPI2_RAID_VOL_STATE_ONLINE:
1602 state = RAID_STATE_ACTIVE;
1603 break;
1604 case MPI2_RAID_VOL_STATE_DEGRADED:
1605 state = RAID_STATE_DEGRADED;
1606 break;
1607 case MPI2_RAID_VOL_STATE_FAILED:
1608 case MPI2_RAID_VOL_STATE_MISSING:
1609 state = RAID_STATE_OFFLINE;
1610 break;
1611 }
1612 out:
1613 raid_set_state(mpt2sas_raid_template, dev, state);
1614}
1615
1616/**
1617 * _scsih_set_level - set raid level
1618 * @sdev: scsi device struct
1619 * @raid_device: raid_device object
1620 */
1621static void
1622_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1623{
1624 enum raid_level level = RAID_LEVEL_UNKNOWN;
1625
1626 switch (raid_device->volume_type) {
1627 case MPI2_RAID_VOL_TYPE_RAID0:
1628 level = RAID_LEVEL_0;
1629 break;
1630 case MPI2_RAID_VOL_TYPE_RAID10:
1631 level = RAID_LEVEL_10;
1632 break;
1633 case MPI2_RAID_VOL_TYPE_RAID1E:
1634 level = RAID_LEVEL_1E;
1635 break;
1636 case MPI2_RAID_VOL_TYPE_RAID1:
1637 level = RAID_LEVEL_1;
1638 break;
1639 }
1640
1641 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1642}
1643
1644/**
Eric Moore635374e2009-03-09 01:21:12 -06001645 * _scsih_get_volume_capabilities - volume capabilities
1646 * @ioc: per adapter object
1647 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301648 *
1649 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001650 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301651static int
Eric Moore635374e2009-03-09 01:21:12 -06001652_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1653 struct _raid_device *raid_device)
1654{
1655 Mpi2RaidVolPage0_t *vol_pg0;
1656 Mpi2RaidPhysDiskPage0_t pd_pg0;
1657 Mpi2SasDevicePage0_t sas_device_pg0;
1658 Mpi2ConfigReply_t mpi_reply;
1659 u16 sz;
1660 u8 num_pds;
1661
1662 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1663 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301664 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1665 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1666 __func__));
1667 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001668 }
1669
1670 raid_device->num_pds = num_pds;
1671 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1672 sizeof(Mpi2RaidVol0PhysDisk_t));
1673 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1674 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301675 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1676 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1677 __func__));
1678 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001679 }
1680
1681 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1682 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301683 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1684 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1685 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001686 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301687 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001688 }
1689
1690 raid_device->volume_type = vol_pg0->VolumeType;
1691
1692 /* figure out what the underlying devices are by
1693 * obtaining the device_info bits for the 1st device
1694 */
1695 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1696 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1697 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1698 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1699 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1700 le16_to_cpu(pd_pg0.DevHandle)))) {
1701 raid_device->device_info =
1702 le32_to_cpu(sas_device_pg0.DeviceInfo);
1703 }
1704 }
1705
1706 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301707 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001708}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301709/**
1710 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1711 * @ioc: per adapter object
1712 */
1713static void
1714_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1715{
1716 Mpi2RaidVolPage1_t vol_pg1;
1717 Mpi2ConfigReply_t mpi_reply;
1718 struct _raid_device *raid_device;
1719 u16 handle;
1720 u16 ioc_status;
1721
1722 handle = 0xFFFF;
1723 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1724 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1725 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1726 MPI2_IOCSTATUS_MASK;
1727 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1728 break;
1729 handle = le16_to_cpu(vol_pg1.DevHandle);
1730 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1731 if (raid_device)
1732 raid_device->direct_io_enabled = 0;
1733 }
1734 return;
1735}
1736
1737
1738/**
1739 * _scsih_get_num_volumes - Get number of volumes in the ioc
1740 * @ioc: per adapter object
1741 */
1742static u8
1743_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1744{
1745 Mpi2RaidVolPage1_t vol_pg1;
1746 Mpi2ConfigReply_t mpi_reply;
1747 u16 handle;
1748 u8 vol_cnt = 0;
1749 u16 ioc_status;
1750
1751 handle = 0xFFFF;
1752 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1753 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1754 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1755 MPI2_IOCSTATUS_MASK;
1756 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1757 break;
1758 vol_cnt++;
1759 handle = le16_to_cpu(vol_pg1.DevHandle);
1760 }
1761 return vol_cnt;
1762}
1763
1764
1765/**
1766 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1767 * @ioc: per adapter object
1768 * @raid_device: the raid_device object
1769 */
1770static void
1771_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1772 struct _raid_device *raid_device)
1773{
1774 Mpi2RaidVolPage0_t *vol_pg0;
1775 Mpi2RaidPhysDiskPage0_t pd_pg0;
1776 Mpi2ConfigReply_t mpi_reply;
1777 u16 sz;
1778 u8 num_pds, count;
1779 u64 mb = 1024 * 1024;
1780 u64 tb_2 = 2 * mb * mb;
1781 u64 capacity;
1782 u32 stripe_sz;
1783 u8 i, stripe_exp;
1784
1785 if (!ioc->is_warpdrive)
1786 return;
1787
1788 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1789 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1790 "globally as drives are exposed\n", ioc->name);
1791 return;
1792 }
1793 if (_scsih_get_num_volumes(ioc) > 1) {
1794 _scsih_disable_ddio(ioc);
1795 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1796 "globally as number of drives > 1\n", ioc->name);
1797 return;
1798 }
1799 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1800 &num_pds)) || !num_pds) {
1801 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1802 "Failure in computing number of drives\n", ioc->name);
1803 return;
1804 }
1805
1806 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1807 sizeof(Mpi2RaidVol0PhysDisk_t));
1808 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1809 if (!vol_pg0) {
1810 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1811 "Memory allocation failure for RVPG0\n", ioc->name);
1812 return;
1813 }
1814
1815 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1816 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1817 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1818 "Failure in retrieving RVPG0\n", ioc->name);
1819 kfree(vol_pg0);
1820 return;
1821 }
1822
1823 /*
1824 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1825 * assumed for WARPDRIVE, disable direct I/O
1826 */
1827 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1828 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1829 "for the drive with handle(0x%04x): num_mem=%d, "
1830 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1831 num_pds, MPT_MAX_WARPDRIVE_PDS);
1832 kfree(vol_pg0);
1833 return;
1834 }
1835 for (count = 0; count < num_pds; count++) {
1836 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1837 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1838 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1839 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1840 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1841 "disabled for the drive with handle(0x%04x) member"
1842 "handle retrieval failed for member number=%d\n",
1843 ioc->name, raid_device->handle,
1844 vol_pg0->PhysDisk[count].PhysDiskNum);
1845 goto out_error;
1846 }
1847 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1848 }
1849
1850 /*
1851 * Assumption for WD: Direct I/O is not supported if the volume is
1852 * not RAID0, if the stripe size is not 64KB, if the block size is
1853 * not 512 and if the volume size is >2TB
1854 */
1855 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1856 le16_to_cpu(vol_pg0->BlockSize) != 512) {
1857 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1858 "for the drive with handle(0x%04x): type=%d, "
1859 "s_sz=%uK, blk_size=%u\n", ioc->name,
1860 raid_device->handle, raid_device->volume_type,
1861 le32_to_cpu(vol_pg0->StripeSize)/2,
1862 le16_to_cpu(vol_pg0->BlockSize));
1863 goto out_error;
1864 }
1865
1866 capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1867 (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1868
1869 if (capacity > tb_2) {
1870 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1871 "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1872 ioc->name, raid_device->handle);
1873 goto out_error;
1874 }
1875
1876 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1877 stripe_exp = 0;
1878 for (i = 0; i < 32; i++) {
1879 if (stripe_sz & 1)
1880 break;
1881 stripe_exp++;
1882 stripe_sz >>= 1;
1883 }
1884 if (i == 32) {
1885 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1886 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1887 ioc->name, raid_device->handle,
1888 le32_to_cpu(vol_pg0->StripeSize)/2);
1889 goto out_error;
1890 }
1891 raid_device->stripe_exponent = stripe_exp;
1892 raid_device->direct_io_enabled = 1;
1893
1894 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1895 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1896 /*
1897 * WARPDRIVE: Though the following fields are not used for direct IO,
1898 * stored for future purpose:
1899 */
1900 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1901 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1902 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1903
1904
1905 kfree(vol_pg0);
1906 return;
1907
1908out_error:
1909 raid_device->direct_io_enabled = 0;
1910 for (count = 0; count < num_pds; count++)
1911 raid_device->pd_handle[count] = 0;
1912 kfree(vol_pg0);
1913 return;
1914}
Eric Moore635374e2009-03-09 01:21:12 -06001915
1916/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301917 * _scsih_enable_tlr - setting TLR flags
1918 * @ioc: per adapter object
1919 * @sdev: scsi device struct
1920 *
1921 * Enabling Transaction Layer Retries for tape devices when
1922 * vpd page 0x90 is present
1923 *
1924 */
1925static void
1926_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1927{
1928 /* only for TAPE */
1929 if (sdev->type != TYPE_TAPE)
1930 return;
1931
1932 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1933 return;
1934
1935 sas_enable_tlr(sdev);
1936 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1937 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1938 return;
1939
1940}
1941
1942/**
Eric Moored5d135b2009-05-18 13:02:08 -06001943 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001944 * @sdev: scsi device struct
1945 *
1946 * Returns 0 if ok. Any other return is assumed to be an error and
1947 * the device is ignored.
1948 */
1949static int
Eric Moored5d135b2009-05-18 13:02:08 -06001950_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001951{
1952 struct Scsi_Host *shost = sdev->host;
1953 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1954 struct MPT2SAS_DEVICE *sas_device_priv_data;
1955 struct MPT2SAS_TARGET *sas_target_priv_data;
1956 struct _sas_device *sas_device;
1957 struct _raid_device *raid_device;
1958 unsigned long flags;
1959 int qdepth;
1960 u8 ssp_target = 0;
1961 char *ds = "";
1962 char *r_level = "";
1963
1964 qdepth = 1;
1965 sas_device_priv_data = sdev->hostdata;
1966 sas_device_priv_data->configured_lun = 1;
1967 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1968 sas_target_priv_data = sas_device_priv_data->sas_target;
1969
1970 /* raid volume handling */
1971 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1972
1973 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1974 raid_device = _scsih_raid_device_find_by_handle(ioc,
1975 sas_target_priv_data->handle);
1976 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1977 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301978 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1979 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1980 __LINE__, __func__));
1981 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001982 }
1983
1984 _scsih_get_volume_capabilities(ioc, raid_device);
1985
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301986 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1987 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1988 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1989 __LINE__, __func__));
1990 return 1;
1991 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301992 /*
1993 * WARPDRIVE: Initialize the required data for Direct IO
1994 */
1995 _scsih_init_warpdrive_properties(ioc, raid_device);
1996
Eric Moore635374e2009-03-09 01:21:12 -06001997 /* RAID Queue Depth Support
1998 * IS volume = underlying qdepth of drive type, either
1999 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2000 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2001 */
2002 if (raid_device->device_info &
2003 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2004 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2005 ds = "SSP";
2006 } else {
2007 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2008 if (raid_device->device_info &
2009 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2010 ds = "SATA";
2011 else
2012 ds = "STP";
2013 }
2014
2015 switch (raid_device->volume_type) {
2016 case MPI2_RAID_VOL_TYPE_RAID0:
2017 r_level = "RAID0";
2018 break;
2019 case MPI2_RAID_VOL_TYPE_RAID1E:
2020 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302021 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302022 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302023 MFG10_GF0_R10_DISPLAY) &&
2024 !(raid_device->num_pds % 2))
2025 r_level = "RAID10";
2026 else
2027 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002028 break;
2029 case MPI2_RAID_VOL_TYPE_RAID1:
2030 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2031 r_level = "RAID1";
2032 break;
2033 case MPI2_RAID_VOL_TYPE_RAID10:
2034 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2035 r_level = "RAID10";
2036 break;
2037 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2038 default:
2039 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2040 r_level = "RAIDX";
2041 break;
2042 }
2043
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302044 if (!ioc->hide_ir_msg)
2045 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2046 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2047 r_level, raid_device->handle,
2048 (unsigned long long)raid_device->wwid,
2049 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002050 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302051 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302052 if (!ioc->is_warpdrive)
2053 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06002054 return 0;
2055 }
2056
2057 /* non-raid handling */
2058 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2059 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2060 sas_device_priv_data->sas_target->sas_address);
2061 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2062 if (sas_device) {
2063 if (sas_target_priv_data->flags &
2064 MPT_TARGET_FLAGS_RAID_COMPONENT) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302065 if (mpt2sas_config_get_volume_handle(ioc,
2066 sas_device->handle, &sas_device->volume_handle)) {
2067 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2068 "failure at %s:%d/%s()!\n", ioc->name,
2069 __FILE__, __LINE__, __func__));
2070 return 1;
2071 }
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05302072 if (sas_device->volume_handle &&
2073 mpt2sas_config_get_volume_wwid(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06002074 sas_device->volume_handle,
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302075 &sas_device->volume_wwid)) {
2076 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2077 "failure at %s:%d/%s()!\n", ioc->name,
2078 __FILE__, __LINE__, __func__));
2079 return 1;
2080 }
Eric Moore635374e2009-03-09 01:21:12 -06002081 }
2082 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2083 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2084 ssp_target = 1;
2085 ds = "SSP";
2086 } else {
2087 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2088 if (sas_device->device_info &
2089 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2090 ds = "STP";
2091 else if (sas_device->device_info &
2092 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2093 ds = "SATA";
2094 }
2095
2096 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302097 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002098 ds, sas_device->handle,
2099 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302100 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06002101 (unsigned long long)sas_device->device_name);
2102 sdev_printk(KERN_INFO, sdev, "%s: "
2103 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2104 (unsigned long long) sas_device->enclosure_logical_id,
2105 sas_device->slot);
2106
2107 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06002108 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302109 } else {
2110 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2111 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
2112 __func__));
2113 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002114 }
2115
Mike Christiee881a172009-10-15 17:46:39 -07002116 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002117
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302118 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002119 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302120 _scsih_enable_tlr(ioc, sdev);
2121 }
Eric Moore635374e2009-03-09 01:21:12 -06002122 return 0;
2123}
2124
2125/**
Eric Moored5d135b2009-05-18 13:02:08 -06002126 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002127 * @sdev: scsi device struct
2128 * @bdev: pointer to block device context
2129 * @capacity: device size (in 512 byte sectors)
2130 * @params: three element array to place output:
2131 * params[0] number of heads (max 255)
2132 * params[1] number of sectors (max 63)
2133 * params[2] number of cylinders
2134 *
2135 * Return nothing.
2136 */
2137static int
Eric Moored5d135b2009-05-18 13:02:08 -06002138_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002139 sector_t capacity, int params[])
2140{
2141 int heads;
2142 int sectors;
2143 sector_t cylinders;
2144 ulong dummy;
2145
2146 heads = 64;
2147 sectors = 32;
2148
2149 dummy = heads * sectors;
2150 cylinders = capacity;
2151 sector_div(cylinders, dummy);
2152
2153 /*
2154 * Handle extended translation size for logical drives
2155 * > 1Gb
2156 */
2157 if ((ulong)capacity >= 0x200000) {
2158 heads = 255;
2159 sectors = 63;
2160 dummy = heads * sectors;
2161 cylinders = capacity;
2162 sector_div(cylinders, dummy);
2163 }
2164
2165 /* return result */
2166 params[0] = heads;
2167 params[1] = sectors;
2168 params[2] = cylinders;
2169
2170 return 0;
2171}
2172
2173/**
2174 * _scsih_response_code - translation of device response code
2175 * @ioc: per adapter object
2176 * @response_code: response code returned by the device
2177 *
2178 * Return nothing.
2179 */
2180static void
2181_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2182{
2183 char *desc;
2184
2185 switch (response_code) {
2186 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2187 desc = "task management request completed";
2188 break;
2189 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2190 desc = "invalid frame";
2191 break;
2192 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2193 desc = "task management request not supported";
2194 break;
2195 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2196 desc = "task management request failed";
2197 break;
2198 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2199 desc = "task management request succeeded";
2200 break;
2201 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2202 desc = "invalid lun";
2203 break;
2204 case 0xA:
2205 desc = "overlapped tag attempted";
2206 break;
2207 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2208 desc = "task queued, however not sent to target";
2209 break;
2210 default:
2211 desc = "unknown";
2212 break;
2213 }
2214 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2215 ioc->name, response_code, desc);
2216}
2217
2218/**
Eric Moored5d135b2009-05-18 13:02:08 -06002219 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002220 * @ioc: per adapter object
2221 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302222 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002223 * @reply: reply message frame(lower 32bit addr)
2224 * Context: none.
2225 *
2226 * The callback handler when using scsih_issue_tm.
2227 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302228 * Return 1 meaning mf should be freed from _base_interrupt
2229 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002230 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302231static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302232_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002233{
2234 MPI2DefaultReply_t *mpi_reply;
2235
2236 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302237 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002238 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302239 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302240 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002241 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2242 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2243 if (mpi_reply) {
2244 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2245 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2246 }
2247 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2248 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302249 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002250}
2251
2252/**
2253 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2254 * @ioc: per adapter object
2255 * @handle: device handle
2256 *
2257 * During taskmangement request, we need to freeze the device queue.
2258 */
2259void
2260mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2261{
2262 struct MPT2SAS_DEVICE *sas_device_priv_data;
2263 struct scsi_device *sdev;
2264 u8 skip = 0;
2265
2266 shost_for_each_device(sdev, ioc->shost) {
2267 if (skip)
2268 continue;
2269 sas_device_priv_data = sdev->hostdata;
2270 if (!sas_device_priv_data)
2271 continue;
2272 if (sas_device_priv_data->sas_target->handle == handle) {
2273 sas_device_priv_data->sas_target->tm_busy = 1;
2274 skip = 1;
2275 ioc->ignore_loginfos = 1;
2276 }
2277 }
2278}
2279
2280/**
2281 * mpt2sas_scsih_clear_tm_flag - clear 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_clear_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 = 0;
2302 skip = 1;
2303 ioc->ignore_loginfos = 0;
2304 }
2305 }
2306}
2307
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302308
Eric Moore635374e2009-03-09 01:21:12 -06002309/**
2310 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2311 * @ioc: per adapter struct
2312 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302313 * @channel: the channel assigned by the OS
2314 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002315 * @lun: lun number
2316 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2317 * @smid_task: smid assigned to the task
2318 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302319 * @serial_number: the serial_number from scmd
2320 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302321 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002322 *
2323 * A generic API for sending task management requests to firmware.
2324 *
Eric Moore635374e2009-03-09 01:21:12 -06002325 * The callback index is set inside `ioc->tm_cb_idx`.
2326 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302327 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002328 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302329int
2330mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2331 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302332 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002333{
2334 Mpi2SCSITaskManagementRequest_t *mpi_request;
2335 Mpi2SCSITaskManagementReply_t *mpi_reply;
2336 u16 smid = 0;
2337 u32 ioc_state;
2338 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302339 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302340 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002341
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302342 if (m_type == TM_MUTEX_ON)
2343 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302344 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2345 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2346 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302347 rc = FAILED;
2348 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302349 }
2350
Eric Moore3cb54692010-07-08 14:44:34 -06002351 if (ioc->shost_recovery || ioc->remove_host ||
2352 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002353 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2354 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302355 rc = FAILED;
2356 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002357 }
Eric Moore635374e2009-03-09 01:21:12 -06002358
2359 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2360 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302361 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002362 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302363 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302364 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302365 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302366 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002367 }
2368
2369 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2370 mpt2sas_base_fault_info(ioc, ioc_state &
2371 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302372 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302373 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302374 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302375 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002376 }
2377
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302378 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002379 if (!smid) {
2380 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2381 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302382 rc = FAILED;
2383 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002384 }
2385
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302386 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2387 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2388
Eric Moore635374e2009-03-09 01:21:12 -06002389 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302390 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2391 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002392 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2393 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2394 ioc->tm_cmds.smid = smid;
2395 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302396 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002397 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2398 mpi_request->DevHandle = cpu_to_le16(handle);
2399 mpi_request->TaskType = type;
2400 mpi_request->TaskMID = cpu_to_le16(smid_task);
2401 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2402 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302403 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302404 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002405 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002406 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2407 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2408 ioc->name, __func__);
2409 _debug_dump_mf(mpi_request,
2410 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302411 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302412 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302413 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302414 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302415 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2416 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2417 goto err_out;
2418 }
Eric Moore635374e2009-03-09 01:21:12 -06002419 }
2420
2421 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2422 mpi_reply = ioc->tm_cmds.reply;
2423 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2424 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2425 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2426 le32_to_cpu(mpi_reply->IOCLogInfo),
2427 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302428 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002429 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302430 if (mpi_reply->IOCStatus)
2431 _debug_dump_mf(mpi_request,
2432 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2433 }
Eric Moore635374e2009-03-09 01:21:12 -06002434 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302435
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302436 switch (type) {
2437 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302438 rc = SUCCESS;
2439 if (scsi_lookup->scmd == NULL)
2440 break;
2441 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302442 break;
2443
2444 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2445 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2446 rc = FAILED;
2447 else
2448 rc = SUCCESS;
2449 break;
2450
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302451 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302452 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2453 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2454 rc = FAILED;
2455 else
2456 rc = SUCCESS;
2457 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302458 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2459 rc = SUCCESS;
2460 break;
2461 default:
2462 rc = FAILED;
2463 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302464 }
2465
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302466 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2467 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302468 if (m_type == TM_MUTEX_ON)
2469 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302470
2471 return rc;
2472
2473 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302474 if (m_type == TM_MUTEX_ON)
2475 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302476 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002477}
2478
2479/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302480 * _scsih_tm_display_info - displays info about the device
2481 * @ioc: per adapter struct
2482 * @scmd: pointer to scsi command object
2483 *
2484 * Called by task management callback handlers.
2485 */
2486static void
2487_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2488{
2489 struct scsi_target *starget = scmd->device->sdev_target;
2490 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2491 struct _sas_device *sas_device = NULL;
2492 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302493 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302494
2495 if (!priv_target)
2496 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302497 if (ioc->hide_ir_msg)
2498 device_str = "WarpDrive";
2499 else
2500 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302501
2502 scsi_print_command(scmd);
2503 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302504 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2505 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2506 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302507 } else {
2508 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2509 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2510 priv_target->sas_address);
2511 if (sas_device) {
2512 if (priv_target->flags &
2513 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2514 starget_printk(KERN_INFO, starget,
2515 "volume handle(0x%04x), "
2516 "volume wwid(0x%016llx)\n",
2517 sas_device->volume_handle,
2518 (unsigned long long)sas_device->volume_wwid);
2519 }
2520 starget_printk(KERN_INFO, starget,
2521 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2522 sas_device->handle,
2523 (unsigned long long)sas_device->sas_address,
2524 sas_device->phy);
2525 starget_printk(KERN_INFO, starget,
2526 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2527 (unsigned long long)sas_device->enclosure_logical_id,
2528 sas_device->slot);
2529 }
2530 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2531 }
2532}
2533
2534/**
Eric Moored5d135b2009-05-18 13:02:08 -06002535 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302536 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002537 *
2538 * Returns SUCCESS if command aborted else FAILED
2539 */
2540static int
Eric Moored5d135b2009-05-18 13:02:08 -06002541_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002542{
2543 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2544 struct MPT2SAS_DEVICE *sas_device_priv_data;
2545 u16 smid;
2546 u16 handle;
2547 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002548
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302549 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2550 "scmd(%p)\n", scmd);
2551 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002552
2553 sas_device_priv_data = scmd->device->hostdata;
2554 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302555 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2556 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002557 scmd->result = DID_NO_CONNECT << 16;
2558 scmd->scsi_done(scmd);
2559 r = SUCCESS;
2560 goto out;
2561 }
2562
2563 /* search for the command */
2564 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2565 if (!smid) {
2566 scmd->result = DID_RESET << 16;
2567 r = SUCCESS;
2568 goto out;
2569 }
2570
2571 /* for hidden raid components and volumes this is not supported */
2572 if (sas_device_priv_data->sas_target->flags &
2573 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2574 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2575 scmd->result = DID_RESET << 16;
2576 r = FAILED;
2577 goto out;
2578 }
2579
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302580 mpt2sas_halt_firmware(ioc);
2581
Eric Moore635374e2009-03-09 01:21:12 -06002582 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302583 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2584 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302585 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2586 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002587
2588 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302589 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2590 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002591 return r;
2592}
2593
Eric Moore635374e2009-03-09 01:21:12 -06002594/**
Eric Moored5d135b2009-05-18 13:02:08 -06002595 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302596 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002597 *
2598 * Returns SUCCESS if command aborted else FAILED
2599 */
2600static int
Eric Moored5d135b2009-05-18 13:02:08 -06002601_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002602{
2603 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2604 struct MPT2SAS_DEVICE *sas_device_priv_data;
2605 struct _sas_device *sas_device;
2606 unsigned long flags;
2607 u16 handle;
2608 int r;
2609
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302610 struct scsi_target *starget = scmd->device->sdev_target;
2611
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302612 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302613 "scmd(%p)\n", scmd);
2614 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002615
2616 sas_device_priv_data = scmd->device->hostdata;
2617 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302618 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302619 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002620 scmd->result = DID_NO_CONNECT << 16;
2621 scmd->scsi_done(scmd);
2622 r = SUCCESS;
2623 goto out;
2624 }
2625
2626 /* for hidden raid components obtain the volume_handle */
2627 handle = 0;
2628 if (sas_device_priv_data->sas_target->flags &
2629 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2630 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2631 sas_device = _scsih_sas_device_find_by_handle(ioc,
2632 sas_device_priv_data->sas_target->handle);
2633 if (sas_device)
2634 handle = sas_device->volume_handle;
2635 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2636 } else
2637 handle = sas_device_priv_data->sas_target->handle;
2638
2639 if (!handle) {
2640 scmd->result = DID_RESET << 16;
2641 r = FAILED;
2642 goto out;
2643 }
2644
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302645 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2646 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302647 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2648 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002649
2650 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302651 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2652 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002653 return r;
2654}
2655
2656/**
Eric Moored5d135b2009-05-18 13:02:08 -06002657 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302658 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002659 *
2660 * Returns SUCCESS if command aborted else FAILED
2661 */
2662static int
Eric Moored5d135b2009-05-18 13:02:08 -06002663_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002664{
2665 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2666 struct MPT2SAS_DEVICE *sas_device_priv_data;
2667 struct _sas_device *sas_device;
2668 unsigned long flags;
2669 u16 handle;
2670 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302671 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002672
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302673 starget_printk(KERN_INFO, starget, "attempting target reset! "
2674 "scmd(%p)\n", scmd);
2675 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002676
2677 sas_device_priv_data = scmd->device->hostdata;
2678 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302679 starget_printk(KERN_INFO, starget, "target been deleted! "
2680 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002681 scmd->result = DID_NO_CONNECT << 16;
2682 scmd->scsi_done(scmd);
2683 r = SUCCESS;
2684 goto out;
2685 }
2686
2687 /* for hidden raid components obtain the volume_handle */
2688 handle = 0;
2689 if (sas_device_priv_data->sas_target->flags &
2690 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2691 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2692 sas_device = _scsih_sas_device_find_by_handle(ioc,
2693 sas_device_priv_data->sas_target->handle);
2694 if (sas_device)
2695 handle = sas_device->volume_handle;
2696 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2697 } else
2698 handle = sas_device_priv_data->sas_target->handle;
2699
2700 if (!handle) {
2701 scmd->result = DID_RESET << 16;
2702 r = FAILED;
2703 goto out;
2704 }
2705
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302706 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2707 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302708 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002709
2710 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302711 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2712 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002713 return r;
2714}
2715
2716/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302717 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302718 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002719 *
2720 * Returns SUCCESS if command aborted else FAILED
2721 */
2722static int
Eric Moored5d135b2009-05-18 13:02:08 -06002723_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002724{
2725 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2726 int r, retval;
2727
2728 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2729 ioc->name, scmd);
2730 scsi_print_command(scmd);
2731
2732 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2733 FORCE_BIG_HAMMER);
2734 r = (retval < 0) ? FAILED : SUCCESS;
2735 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2736 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2737
2738 return r;
2739}
2740
2741/**
2742 * _scsih_fw_event_add - insert and queue up fw_event
2743 * @ioc: per adapter object
2744 * @fw_event: object describing the event
2745 * Context: This function will acquire ioc->fw_event_lock.
2746 *
2747 * This adds the firmware event object into link list, then queues it up to
2748 * be processed from user context.
2749 *
2750 * Return nothing.
2751 */
2752static void
2753_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2754{
2755 unsigned long flags;
2756
2757 if (ioc->firmware_event_thread == NULL)
2758 return;
2759
2760 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2761 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302762 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2763 queue_delayed_work(ioc->firmware_event_thread,
2764 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002765 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2766}
2767
2768/**
2769 * _scsih_fw_event_free - delete fw_event
2770 * @ioc: per adapter object
2771 * @fw_event: object describing the event
2772 * Context: This function will acquire ioc->fw_event_lock.
2773 *
2774 * This removes firmware event object from link list, frees associated memory.
2775 *
2776 * Return nothing.
2777 */
2778static void
2779_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2780 *fw_event)
2781{
2782 unsigned long flags;
2783
2784 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2785 list_del(&fw_event->list);
2786 kfree(fw_event->event_data);
2787 kfree(fw_event);
2788 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2789}
2790
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302791
Eric Moore635374e2009-03-09 01:21:12 -06002792/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302793 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002794 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002795 *
2796 * Return nothing.
2797 */
2798static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302799_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002800{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302801 struct fw_event_work *fw_event;
2802
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302803 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302804 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002805
2806 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2807 if (!fw_event)
2808 return;
2809
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302810 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2811 fw_event->ioc = ioc;
2812 _scsih_fw_event_add(ioc, fw_event);
2813}
2814
2815/**
2816 * mpt2sas_port_enable_complete - port enable completed (fake event)
2817 * @ioc: per adapter object
2818 *
2819 * Return nothing.
2820 */
2821void
2822mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2823{
2824 struct fw_event_work *fw_event;
2825
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302826 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2827 if (!fw_event)
2828 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302829 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302830 fw_event->ioc = ioc;
2831 _scsih_fw_event_add(ioc, fw_event);
2832}
2833
2834/**
2835 * _scsih_fw_event_cleanup_queue - cleanup event queue
2836 * @ioc: per adapter object
2837 *
2838 * Walk the firmware event queue, either killing timers, or waiting
2839 * for outstanding events to complete
2840 *
2841 * Return nothing.
2842 */
2843static void
2844_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2845{
2846 struct fw_event_work *fw_event, *next;
2847
2848 if (list_empty(&ioc->fw_event_list) ||
2849 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002850 return;
2851
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302852 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2853 if (cancel_delayed_work(&fw_event->delayed_work)) {
2854 _scsih_fw_event_free(ioc, fw_event);
2855 continue;
2856 }
2857 fw_event->cancel_pending_work = 1;
2858 }
Eric Moore635374e2009-03-09 01:21:12 -06002859}
2860
Eric Moore635374e2009-03-09 01:21:12 -06002861/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302862 * _scsih_ublock_io_all_device - unblock every device
2863 * @ioc: per adapter object
2864 *
2865 * change the device state from block to running
2866 */
2867static void
2868_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2869{
2870 struct MPT2SAS_DEVICE *sas_device_priv_data;
2871 struct scsi_device *sdev;
2872
2873 shost_for_each_device(sdev, ioc->shost) {
2874 sas_device_priv_data = sdev->hostdata;
2875 if (!sas_device_priv_data)
2876 continue;
2877 if (!sas_device_priv_data->block)
2878 continue;
2879 sas_device_priv_data->block = 0;
2880 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2881 "handle(0x%04x)\n",
2882 sas_device_priv_data->sas_target->handle));
2883 scsi_internal_device_unblock(sdev);
2884 }
2885}
2886/**
Eric Moore635374e2009-03-09 01:21:12 -06002887 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2888 * @ioc: per adapter object
2889 * @handle: device handle
2890 *
2891 * During device pull we need to appropiately set the sdev state.
2892 */
2893static void
2894_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2895{
2896 struct MPT2SAS_DEVICE *sas_device_priv_data;
2897 struct scsi_device *sdev;
2898
2899 shost_for_each_device(sdev, ioc->shost) {
2900 sas_device_priv_data = sdev->hostdata;
2901 if (!sas_device_priv_data)
2902 continue;
2903 if (!sas_device_priv_data->block)
2904 continue;
2905 if (sas_device_priv_data->sas_target->handle == handle) {
2906 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2907 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2908 "handle(0x%04x)\n", ioc->name, handle));
2909 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302910 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002911 }
2912 }
2913}
2914
2915/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302916 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2917 * @ioc: per adapter object
2918 * @handle: device handle
2919 *
2920 * During device pull we need to appropiately set the sdev state.
2921 */
2922static void
2923_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2924{
2925 struct MPT2SAS_DEVICE *sas_device_priv_data;
2926 struct scsi_device *sdev;
2927
2928 shost_for_each_device(sdev, ioc->shost) {
2929 sas_device_priv_data = sdev->hostdata;
2930 if (!sas_device_priv_data)
2931 continue;
2932 if (sas_device_priv_data->block)
2933 continue;
2934 sas_device_priv_data->block = 1;
2935 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2936 "handle(0x%04x)\n",
2937 sas_device_priv_data->sas_target->handle));
2938 scsi_internal_device_block(sdev);
2939 }
2940}
2941
2942
2943/**
Eric Moore635374e2009-03-09 01:21:12 -06002944 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2945 * @ioc: per adapter object
2946 * @handle: device handle
2947 *
2948 * During device pull we need to appropiately set the sdev state.
2949 */
2950static void
2951_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2952{
2953 struct MPT2SAS_DEVICE *sas_device_priv_data;
2954 struct scsi_device *sdev;
2955
2956 shost_for_each_device(sdev, ioc->shost) {
2957 sas_device_priv_data = sdev->hostdata;
2958 if (!sas_device_priv_data)
2959 continue;
2960 if (sas_device_priv_data->block)
2961 continue;
2962 if (sas_device_priv_data->sas_target->handle == handle) {
2963 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2964 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2965 "handle(0x%04x)\n", ioc->name, handle));
2966 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302967 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002968 }
2969 }
2970}
2971
2972/**
2973 * _scsih_block_io_to_children_attached_to_ex
2974 * @ioc: per adapter object
2975 * @sas_expander: the sas_device object
2976 *
2977 * This routine set sdev state to SDEV_BLOCK for all devices
2978 * attached to this expander. This function called when expander is
2979 * pulled.
2980 */
2981static void
2982_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2983 struct _sas_node *sas_expander)
2984{
2985 struct _sas_port *mpt2sas_port;
2986 struct _sas_device *sas_device;
2987 struct _sas_node *expander_sibling;
2988 unsigned long flags;
2989
2990 if (!sas_expander)
2991 return;
2992
2993 list_for_each_entry(mpt2sas_port,
2994 &sas_expander->sas_port_list, port_list) {
2995 if (mpt2sas_port->remote_identify.device_type ==
2996 SAS_END_DEVICE) {
2997 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2998 sas_device =
2999 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3000 mpt2sas_port->remote_identify.sas_address);
3001 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3002 if (!sas_device)
3003 continue;
3004 _scsih_block_io_device(ioc, sas_device->handle);
3005 }
3006 }
3007
3008 list_for_each_entry(mpt2sas_port,
3009 &sas_expander->sas_port_list, port_list) {
3010
3011 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303012 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003013 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303014 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003015
3016 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3017 expander_sibling =
3018 mpt2sas_scsih_expander_find_by_sas_address(
3019 ioc, mpt2sas_port->remote_identify.sas_address);
3020 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3021 _scsih_block_io_to_children_attached_to_ex(ioc,
3022 expander_sibling);
3023 }
3024 }
3025}
3026
3027/**
3028 * _scsih_block_io_to_children_attached_directly
3029 * @ioc: per adapter object
3030 * @event_data: topology change event data
3031 *
3032 * This routine set sdev state to SDEV_BLOCK for all devices
3033 * direct attached during device pull.
3034 */
3035static void
3036_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3037 Mpi2EventDataSasTopologyChangeList_t *event_data)
3038{
3039 int i;
3040 u16 handle;
3041 u16 reason_code;
3042 u8 phy_number;
3043
3044 for (i = 0; i < event_data->NumEntries; i++) {
3045 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3046 if (!handle)
3047 continue;
3048 phy_number = event_data->StartPhyNum + i;
3049 reason_code = event_data->PHY[i].PhyStatus &
3050 MPI2_EVENT_SAS_TOPO_RC_MASK;
3051 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3052 _scsih_block_io_device(ioc, handle);
3053 }
3054}
3055
3056/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303057 * _scsih_tm_tr_send - send task management request
3058 * @ioc: per adapter object
3059 * @handle: device handle
3060 * Context: interrupt time.
3061 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003062 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303063 * with controller firmware. This function will issue target reset
3064 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003065 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303066 *
3067 * This is designed to send muliple task management request at the same
3068 * time to the fifo. If the fifo is full, we will append the request,
3069 * and process it in a future completion.
3070 */
3071static void
3072_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3073{
3074 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303075 u16 smid;
3076 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303077 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3078 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303079 unsigned long flags;
3080 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303081 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303082
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303083 if (ioc->remove_host) {
3084 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3085 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3086 return;
3087 } else if (ioc->pci_error_recovery) {
3088 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3089 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3090 handle));
3091 return;
3092 }
3093 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3094 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3095 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3096 "operational: handle(0x%04x)\n", __func__, ioc->name,
3097 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303098 return;
3099 }
3100
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303101 /* if PD, then return */
3102 if (test_bit(handle, ioc->pd_handles))
3103 return;
3104
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303105 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3106 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303107 if (sas_device && sas_device->starget &&
3108 sas_device->starget->hostdata) {
3109 sas_target_priv_data = sas_device->starget->hostdata;
3110 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303111 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303112 }
3113 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303114
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303115 if (sas_target_priv_data) {
3116 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3117 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3118 (unsigned long long)sas_address));
3119 _scsih_ublock_io_device(ioc, handle);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303120 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303121 }
3122
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303123 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3124 if (!smid) {
3125 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3126 if (!delayed_tr)
3127 return;
3128 INIT_LIST_HEAD(&delayed_tr->list);
3129 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303130 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3131 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3132 "DELAYED:tr:handle(0x%04x), (open)\n",
3133 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303134 return;
3135 }
3136
Kashyap, Desai1278b112010-03-09 17:34:13 +05303137 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3138 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3139 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303140 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3141 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3142 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3143 mpi_request->DevHandle = cpu_to_le16(handle);
3144 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303145 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3146}
3147
3148
3149
3150/**
3151 * _scsih_sas_control_complete - completion routine
3152 * @ioc: per adapter object
3153 * @smid: system request message index
3154 * @msix_index: MSIX table index supplied by the OS
3155 * @reply: reply message frame(lower 32bit addr)
3156 * Context: interrupt time.
3157 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003158 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303159 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003160 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303161 *
3162 * Return 1 meaning mf should be freed from _base_interrupt
3163 * 0 means the mf is freed from this function.
3164 */
3165static u8
3166_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3167 u8 msix_index, u32 reply)
3168{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303169#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303170 Mpi2SasIoUnitControlReply_t *mpi_reply =
3171 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303172#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05303173 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3174 "sc_complete:handle(0x%04x), (open) "
3175 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3176 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3177 le16_to_cpu(mpi_reply->IOCStatus),
3178 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303179 return 1;
3180}
3181
3182/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303183 * _scsih_tm_tr_volume_send - send target reset request for volumes
3184 * @ioc: per adapter object
3185 * @handle: device handle
3186 * Context: interrupt time.
3187 *
3188 * This is designed to send muliple task management request at the same
3189 * time to the fifo. If the fifo is full, we will append the request,
3190 * and process it in a future completion.
3191 */
3192static void
3193_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3194{
3195 Mpi2SCSITaskManagementRequest_t *mpi_request;
3196 u16 smid;
3197 struct _tr_list *delayed_tr;
3198
Eric Moore3cb54692010-07-08 14:44:34 -06003199 if (ioc->shost_recovery || ioc->remove_host ||
3200 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303201 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3202 "progress!\n", __func__, ioc->name));
3203 return;
3204 }
3205
3206 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3207 if (!smid) {
3208 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3209 if (!delayed_tr)
3210 return;
3211 INIT_LIST_HEAD(&delayed_tr->list);
3212 delayed_tr->handle = handle;
3213 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3214 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3215 "DELAYED:tr:handle(0x%04x), (open)\n",
3216 ioc->name, handle));
3217 return;
3218 }
3219
3220 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3221 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3222 ioc->tm_tr_volume_cb_idx));
3223 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3224 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3225 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3226 mpi_request->DevHandle = cpu_to_le16(handle);
3227 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3228 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3229}
3230
3231/**
3232 * _scsih_tm_volume_tr_complete - target reset completion
3233 * @ioc: per adapter object
3234 * @smid: system request message index
3235 * @msix_index: MSIX table index supplied by the OS
3236 * @reply: reply message frame(lower 32bit addr)
3237 * Context: interrupt time.
3238 *
3239 * Return 1 meaning mf should be freed from _base_interrupt
3240 * 0 means the mf is freed from this function.
3241 */
3242static u8
3243_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3244 u8 msix_index, u32 reply)
3245{
3246 u16 handle;
3247 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3248 Mpi2SCSITaskManagementReply_t *mpi_reply =
3249 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3250
Eric Moore3cb54692010-07-08 14:44:34 -06003251 if (ioc->shost_recovery || ioc->remove_host ||
3252 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303253 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3254 "progress!\n", __func__, ioc->name));
3255 return 1;
3256 }
3257
3258 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3259 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3260 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3261 dewtprintk(ioc, printk("spurious interrupt: "
3262 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3263 le16_to_cpu(mpi_reply->DevHandle), smid));
3264 return 0;
3265 }
3266
3267 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3268 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3269 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3270 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3271 le32_to_cpu(mpi_reply->IOCLogInfo),
3272 le32_to_cpu(mpi_reply->TerminationCount)));
3273
3274 return _scsih_check_for_pending_tm(ioc, smid);
3275}
3276
3277/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303278 * _scsih_tm_tr_complete -
3279 * @ioc: per adapter object
3280 * @smid: system request message index
3281 * @msix_index: MSIX table index supplied by the OS
3282 * @reply: reply message frame(lower 32bit addr)
3283 * Context: interrupt time.
3284 *
3285 * This is the target reset completion routine.
3286 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003287 * handshake protocol with controller firmware.
3288 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303289 *
3290 * Return 1 meaning mf should be freed from _base_interrupt
3291 * 0 means the mf is freed from this function.
3292 */
3293static u8
3294_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3295 u32 reply)
3296{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303297 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303298 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303299 Mpi2SCSITaskManagementReply_t *mpi_reply =
3300 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3301 Mpi2SasIoUnitControlRequest_t *mpi_request;
3302 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303303 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303304
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303305 if (ioc->remove_host) {
3306 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3307 "removed\n", __func__, ioc->name));
3308 return 1;
3309 } else if (ioc->pci_error_recovery) {
3310 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3311 "error recovery\n", __func__, ioc->name));
3312 return 1;
3313 }
3314 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3315 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3316 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3317 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303318 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303319 }
3320
Kashyap, Desai1278b112010-03-09 17:34:13 +05303321 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3322 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3323 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3324 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3325 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3326 le16_to_cpu(mpi_reply->DevHandle), smid));
3327 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303328 }
3329
Kashyap, Desai1278b112010-03-09 17:34:13 +05303330 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3331 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3332 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3333 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3334 le32_to_cpu(mpi_reply->IOCLogInfo),
3335 le32_to_cpu(mpi_reply->TerminationCount)));
3336
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303337 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3338 if (!smid_sas_ctrl) {
3339 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3340 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303341 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303342 }
3343
Kashyap, Desai1278b112010-03-09 17:34:13 +05303344 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3345 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3346 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303347 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3348 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3349 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3350 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303351 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303352 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303353
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303354 return _scsih_check_for_pending_tm(ioc, smid);
3355}
3356
3357/**
3358 * _scsih_check_for_pending_tm - check for pending task management
3359 * @ioc: per adapter object
3360 * @smid: system request message index
3361 *
3362 * This will check delayed target reset list, and feed the
3363 * next reqeust.
3364 *
3365 * Return 1 meaning mf should be freed from _base_interrupt
3366 * 0 means the mf is freed from this function.
3367 */
3368static u8
3369_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3370{
3371 struct _tr_list *delayed_tr;
3372
3373 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3374 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3375 struct _tr_list, list);
3376 mpt2sas_base_free_smid(ioc, smid);
3377 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3378 list_del(&delayed_tr->list);
3379 kfree(delayed_tr);
3380 return 0;
3381 }
3382
Kashyap, Desai1278b112010-03-09 17:34:13 +05303383 if (!list_empty(&ioc->delayed_tr_list)) {
3384 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3385 struct _tr_list, list);
3386 mpt2sas_base_free_smid(ioc, smid);
3387 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3388 list_del(&delayed_tr->list);
3389 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303390 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303391 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303392
Kashyap, Desai1278b112010-03-09 17:34:13 +05303393 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303394}
3395
3396/**
Eric Moore635374e2009-03-09 01:21:12 -06003397 * _scsih_check_topo_delete_events - sanity check on topo events
3398 * @ioc: per adapter object
3399 * @event_data: the event data payload
3400 *
3401 * This routine added to better handle cable breaker.
3402 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003403 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003404 * add and delete events in a single shot. When there is a delete event
3405 * the routine will void any pending add events waiting in the event queue.
3406 *
3407 * Return nothing.
3408 */
3409static void
3410_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3411 Mpi2EventDataSasTopologyChangeList_t *event_data)
3412{
3413 struct fw_event_work *fw_event;
3414 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3415 u16 expander_handle;
3416 struct _sas_node *sas_expander;
3417 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303418 int i, reason_code;
3419 u16 handle;
3420
3421 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303422 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3423 if (!handle)
3424 continue;
3425 reason_code = event_data->PHY[i].PhyStatus &
3426 MPI2_EVENT_SAS_TOPO_RC_MASK;
3427 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3428 _scsih_tm_tr_send(ioc, handle);
3429 }
Eric Moore635374e2009-03-09 01:21:12 -06003430
3431 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3432 if (expander_handle < ioc->sas_hba.num_phys) {
3433 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3434 return;
3435 }
3436
3437 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3438 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3439 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3440 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3441 expander_handle);
3442 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3443 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3444 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3445 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3446
3447 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3448 return;
3449
3450 /* mark ignore flag for pending events */
3451 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3452 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3453 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3454 fw_event->ignore)
3455 continue;
3456 local_event_data = fw_event->event_data;
3457 if (local_event_data->ExpStatus ==
3458 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3459 local_event_data->ExpStatus ==
3460 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3461 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3462 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303463 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003464 "setting ignoring flag\n", ioc->name));
3465 fw_event->ignore = 1;
3466 }
3467 }
3468 }
3469 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3470}
3471
3472/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303473 * _scsih_set_volume_delete_flag - setting volume delete flag
3474 * @ioc: per adapter object
3475 * @handle: device handle
3476 *
3477 * This
3478 * Return nothing.
3479 */
3480static void
3481_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3482{
3483 struct _raid_device *raid_device;
3484 struct MPT2SAS_TARGET *sas_target_priv_data;
3485 unsigned long flags;
3486
3487 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3488 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3489 if (raid_device && raid_device->starget &&
3490 raid_device->starget->hostdata) {
3491 sas_target_priv_data =
3492 raid_device->starget->hostdata;
3493 sas_target_priv_data->deleted = 1;
3494 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3495 "setting delete flag: handle(0x%04x), "
3496 "wwid(0x%016llx)\n", ioc->name, handle,
3497 (unsigned long long) raid_device->wwid));
3498 }
3499 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3500}
3501
3502/**
3503 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3504 * @handle: input handle
3505 * @a: handle for volume a
3506 * @b: handle for volume b
3507 *
3508 * IR firmware only supports two raid volumes. The purpose of this
3509 * routine is to set the volume handle in either a or b. When the given
3510 * input handle is non-zero, or when a and b have not been set before.
3511 */
3512static void
3513_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3514{
3515 if (!handle || handle == *a || handle == *b)
3516 return;
3517 if (!*a)
3518 *a = handle;
3519 else if (!*b)
3520 *b = handle;
3521}
3522
3523/**
3524 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3525 * @ioc: per adapter object
3526 * @event_data: the event data payload
3527 * Context: interrupt time.
3528 *
3529 * This routine will send target reset to volume, followed by target
3530 * resets to the PDs. This is called when a PD has been removed, or
3531 * volume has been deleted or removed. When the target reset is sent
3532 * to volume, the PD target resets need to be queued to start upon
3533 * completion of the volume target reset.
3534 *
3535 * Return nothing.
3536 */
3537static void
3538_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3539 Mpi2EventDataIrConfigChangeList_t *event_data)
3540{
3541 Mpi2EventIrConfigElement_t *element;
3542 int i;
3543 u16 handle, volume_handle, a, b;
3544 struct _tr_list *delayed_tr;
3545
3546 a = 0;
3547 b = 0;
3548
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303549 if (ioc->is_warpdrive)
3550 return;
3551
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303552 /* Volume Resets for Deleted or Removed */
3553 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3554 for (i = 0; i < event_data->NumElements; i++, element++) {
3555 if (element->ReasonCode ==
3556 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3557 element->ReasonCode ==
3558 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3559 volume_handle = le16_to_cpu(element->VolDevHandle);
3560 _scsih_set_volume_delete_flag(ioc, volume_handle);
3561 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3562 }
3563 }
3564
3565 /* Volume Resets for UNHIDE events */
3566 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3567 for (i = 0; i < event_data->NumElements; i++, element++) {
3568 if (le32_to_cpu(event_data->Flags) &
3569 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3570 continue;
3571 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3572 volume_handle = le16_to_cpu(element->VolDevHandle);
3573 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3574 }
3575 }
3576
3577 if (a)
3578 _scsih_tm_tr_volume_send(ioc, a);
3579 if (b)
3580 _scsih_tm_tr_volume_send(ioc, b);
3581
3582 /* PD target resets */
3583 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3584 for (i = 0; i < event_data->NumElements; i++, element++) {
3585 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3586 continue;
3587 handle = le16_to_cpu(element->PhysDiskDevHandle);
3588 volume_handle = le16_to_cpu(element->VolDevHandle);
3589 clear_bit(handle, ioc->pd_handles);
3590 if (!volume_handle)
3591 _scsih_tm_tr_send(ioc, handle);
3592 else if (volume_handle == a || volume_handle == b) {
3593 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3594 BUG_ON(!delayed_tr);
3595 INIT_LIST_HEAD(&delayed_tr->list);
3596 delayed_tr->handle = handle;
3597 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3598 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3599 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3600 handle));
3601 } else
3602 _scsih_tm_tr_send(ioc, handle);
3603 }
3604}
3605
3606
3607/**
3608 * _scsih_check_volume_delete_events - set delete flag for volumes
3609 * @ioc: per adapter object
3610 * @event_data: the event data payload
3611 * Context: interrupt time.
3612 *
3613 * This will handle the case when the cable connected to entire volume is
3614 * pulled. We will take care of setting the deleted flag so normal IO will
3615 * not be sent.
3616 *
3617 * Return nothing.
3618 */
3619static void
3620_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3621 Mpi2EventDataIrVolume_t *event_data)
3622{
3623 u32 state;
3624
3625 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3626 return;
3627 state = le32_to_cpu(event_data->NewValue);
3628 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3629 MPI2_RAID_VOL_STATE_FAILED)
3630 _scsih_set_volume_delete_flag(ioc,
3631 le16_to_cpu(event_data->VolDevHandle));
3632}
3633
3634/**
Eric Moore635374e2009-03-09 01:21:12 -06003635 * _scsih_flush_running_cmds - completing outstanding commands.
3636 * @ioc: per adapter object
3637 *
3638 * The flushing out of all pending scmd commands following host reset,
3639 * where all IO is dropped to the floor.
3640 *
3641 * Return nothing.
3642 */
3643static void
3644_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3645{
3646 struct scsi_cmnd *scmd;
3647 u16 smid;
3648 u16 count = 0;
3649
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303650 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303651 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003652 if (!scmd)
3653 continue;
3654 count++;
3655 mpt2sas_base_free_smid(ioc, smid);
3656 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003657 if (ioc->pci_error_recovery)
3658 scmd->result = DID_NO_CONNECT << 16;
3659 else
3660 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003661 scmd->scsi_done(scmd);
3662 }
3663 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3664 ioc->name, count));
3665}
3666
3667/**
Eric Moore3c621b32009-05-18 12:59:41 -06003668 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3669 * @scmd: pointer to scsi command object
3670 * @mpi_request: pointer to the SCSI_IO reqest message frame
3671 *
3672 * Supporting protection 1 and 3.
3673 *
3674 * Returns nothing
3675 */
3676static void
3677_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3678{
3679 u16 eedp_flags;
3680 unsigned char prot_op = scsi_get_prot_op(scmd);
3681 unsigned char prot_type = scsi_get_prot_type(scmd);
3682
Eric Moored334aa72010-04-22 10:47:40 -06003683 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003684 return;
3685
3686 if (prot_op == SCSI_PROT_READ_STRIP)
3687 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3688 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3689 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3690 else
3691 return;
3692
Eric Moore3c621b32009-05-18 12:59:41 -06003693 switch (prot_type) {
3694 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003695 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003696
3697 /*
3698 * enable ref/guard checking
3699 * auto increment ref tag
3700 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303701 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003702 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3703 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3704 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3705 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003706 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003707
Eric Moore3c621b32009-05-18 12:59:41 -06003708 case SCSI_PROT_DIF_TYPE3:
3709
3710 /*
3711 * enable guard checking
3712 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303713 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003714 break;
3715 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303716 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3717 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003718}
3719
3720/**
3721 * _scsih_eedp_error_handling - return sense code for EEDP errors
3722 * @scmd: pointer to scsi command object
3723 * @ioc_status: ioc status
3724 *
3725 * Returns nothing
3726 */
3727static void
3728_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3729{
3730 u8 ascq;
3731 u8 sk;
3732 u8 host_byte;
3733
3734 switch (ioc_status) {
3735 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3736 ascq = 0x01;
3737 break;
3738 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3739 ascq = 0x02;
3740 break;
3741 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3742 ascq = 0x03;
3743 break;
3744 default:
3745 ascq = 0x00;
3746 break;
3747 }
3748
3749 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3750 sk = ILLEGAL_REQUEST;
3751 host_byte = DID_ABORT;
3752 } else {
3753 sk = ABORTED_COMMAND;
3754 host_byte = DID_OK;
3755 }
3756
3757 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3758 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3759 SAM_STAT_CHECK_CONDITION;
3760}
3761
3762/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303763 * _scsih_scsi_direct_io_get - returns direct io flag
3764 * @ioc: per adapter object
3765 * @smid: system request message index
3766 *
3767 * Returns the smid stored scmd pointer.
3768 */
3769static inline u8
3770_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3771{
3772 return ioc->scsi_lookup[smid - 1].direct_io;
3773}
3774
3775/**
3776 * _scsih_scsi_direct_io_set - sets direct io flag
3777 * @ioc: per adapter object
3778 * @smid: system request message index
3779 * @direct_io: Zero or non-zero value to set in the direct_io flag
3780 *
3781 * Returns Nothing.
3782 */
3783static inline void
3784_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3785{
3786 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3787}
3788
3789
3790/**
3791 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3792 * @ioc: per adapter object
3793 * @scmd: pointer to scsi command object
3794 * @raid_device: pointer to raid device data structure
3795 * @mpi_request: pointer to the SCSI_IO reqest message frame
3796 * @smid: system request message index
3797 *
3798 * Returns nothing
3799 */
3800static void
3801_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3802 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3803 u16 smid)
3804{
3805 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3806 u32 stripe_sz, stripe_exp;
3807 u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3808 u8 cdb0 = scmd->cmnd[0];
3809
3810 /*
3811 * Try Direct I/O to RAID memeber disks
3812 */
3813 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3814 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3815 cdb_ptr = mpi_request->CDB.CDB32;
3816
3817 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3818 | cdb_ptr[5])) {
3819 io_size = scsi_bufflen(scmd) >> 9;
3820 /* get virtual lba */
3821 lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3822 &cdb_ptr[6];
3823 tmp_ptr = (u8 *)&v_lba + 3;
3824 *tmp_ptr-- = *lba_ptr1++;
3825 *tmp_ptr-- = *lba_ptr1++;
3826 *tmp_ptr-- = *lba_ptr1++;
3827 *tmp_ptr = *lba_ptr1;
3828
3829 if (((u64)v_lba + (u64)io_size - 1) <=
3830 (u32)raid_device->max_lba) {
3831 stripe_sz = raid_device->stripe_sz;
3832 stripe_exp = raid_device->stripe_exponent;
3833 stripe_off = v_lba & (stripe_sz - 1);
3834
3835 /* Check whether IO falls within a stripe */
3836 if ((stripe_off + io_size) <= stripe_sz) {
3837 num_pds = raid_device->num_pds;
3838 p_lba = v_lba >> stripe_exp;
3839 stripe_unit = p_lba / num_pds;
3840 column = p_lba % num_pds;
3841 p_lba = (stripe_unit << stripe_exp) +
3842 stripe_off;
3843 mpi_request->DevHandle =
3844 cpu_to_le16(raid_device->
3845 pd_handle[column]);
3846 tmp_ptr = (u8 *)&p_lba + 3;
3847 *lba_ptr2++ = *tmp_ptr--;
3848 *lba_ptr2++ = *tmp_ptr--;
3849 *lba_ptr2++ = *tmp_ptr--;
3850 *lba_ptr2 = *tmp_ptr;
3851 /*
3852 * WD: To indicate this I/O is directI/O
3853 */
3854 _scsih_scsi_direct_io_set(ioc, smid, 1);
3855 }
3856 }
3857 }
3858 }
3859}
3860
3861/**
Eric Moored5d135b2009-05-18 13:02:08 -06003862 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003863 * @scmd: pointer to scsi command object
3864 * @done: function pointer to be invoked on completion
3865 *
3866 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3867 *
3868 * Returns 0 on success. If there's a failure, return either:
3869 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3870 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3871 */
3872static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003873_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003874{
3875 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3876 struct MPT2SAS_DEVICE *sas_device_priv_data;
3877 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303878 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003879 Mpi2SCSIIORequest_t *mpi_request;
3880 u32 mpi_control;
3881 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003882
3883 scmd->scsi_done = done;
3884 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303885 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003886 scmd->result = DID_NO_CONNECT << 16;
3887 scmd->scsi_done(scmd);
3888 return 0;
3889 }
3890
Kashyap, Desai78215782011-06-14 10:57:08 +05303891 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003892 scmd->result = DID_NO_CONNECT << 16;
3893 scmd->scsi_done(scmd);
3894 return 0;
3895 }
3896
Eric Moore635374e2009-03-09 01:21:12 -06003897 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303898 /* invalid device handle */
3899 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003900 scmd->result = DID_NO_CONNECT << 16;
3901 scmd->scsi_done(scmd);
3902 return 0;
3903 }
3904
Kashyap, Desai130b9582010-04-08 17:54:32 +05303905 /* host recovery or link resets sent via IOCTLs */
3906 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003907 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003908 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303909 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3910 return SCSI_MLQUEUE_DEVICE_BUSY;
3911 /* device has been deleted */
3912 else if (sas_target_priv_data->deleted) {
3913 scmd->result = DID_NO_CONNECT << 16;
3914 scmd->scsi_done(scmd);
3915 return 0;
3916 }
Eric Moore635374e2009-03-09 01:21:12 -06003917
3918 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3919 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3920 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3921 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3922 else
3923 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3924
3925 /* set tags */
3926 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3927 if (scmd->device->tagged_supported) {
3928 if (scmd->device->ordered_tags)
3929 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3930 else
3931 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3932 } else
3933/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3934/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3935 */
3936 mpi_control |= (0x500);
3937
3938 } else
3939 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303940 /* Make sure Device is not raid volume.
3941 * We do not expose raid functionality to upper layer for warpdrive.
3942 */
3943 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003944 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003945 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3946
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303947 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003948 if (!smid) {
3949 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3950 ioc->name, __func__);
3951 goto out;
3952 }
3953 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3954 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003955 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003956 if (scmd->cmd_len == 32)
3957 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003958 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3959 if (sas_device_priv_data->sas_target->flags &
3960 MPT_TARGET_FLAGS_RAID_COMPONENT)
3961 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3962 else
3963 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3964 mpi_request->DevHandle =
3965 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3966 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3967 mpi_request->Control = cpu_to_le32(mpi_control);
3968 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3969 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3970 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3971 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303972 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003973 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3974 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3975 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303976 mpi_request->VF_ID = 0; /* TODO */
3977 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003978 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3979 mpi_request->LUN);
3980 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3981
3982 if (!mpi_request->DataLength) {
3983 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3984 } else {
3985 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3986 mpt2sas_base_free_smid(ioc, smid);
3987 goto out;
3988 }
3989 }
3990
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303991 raid_device = sas_target_priv_data->raid_device;
3992 if (raid_device && raid_device->direct_io_enabled)
3993 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3994 smid);
3995
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303996 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3997 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303998 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303999 else
4000 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004001 return 0;
4002
4003 out:
4004 return SCSI_MLQUEUE_HOST_BUSY;
4005}
4006
Jeff Garzikf2812332010-11-16 02:10:29 -05004007static DEF_SCSI_QCMD(_scsih_qcmd)
4008
Eric Moore635374e2009-03-09 01:21:12 -06004009/**
4010 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4011 * @sense_buffer: sense data returned by target
4012 * @data: normalized skey/asc/ascq
4013 *
4014 * Return nothing.
4015 */
4016static void
4017_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4018{
4019 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4020 /* descriptor format */
4021 data->skey = sense_buffer[1] & 0x0F;
4022 data->asc = sense_buffer[2];
4023 data->ascq = sense_buffer[3];
4024 } else {
4025 /* fixed format */
4026 data->skey = sense_buffer[2] & 0x0F;
4027 data->asc = sense_buffer[12];
4028 data->ascq = sense_buffer[13];
4029 }
4030}
4031
4032#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4033/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004034 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004035 * @ioc: per adapter object
4036 * @scmd: pointer to scsi command object
4037 * @mpi_reply: reply mf payload returned from firmware
4038 *
4039 * scsi_status - SCSI Status code returned from target device
4040 * scsi_state - state info associated with SCSI_IO determined by ioc
4041 * ioc_status - ioc supplied status info
4042 *
4043 * Return nothing.
4044 */
4045static void
4046_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4047 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4048{
4049 u32 response_info;
4050 u8 *response_bytes;
4051 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4052 MPI2_IOCSTATUS_MASK;
4053 u8 scsi_state = mpi_reply->SCSIState;
4054 u8 scsi_status = mpi_reply->SCSIStatus;
4055 char *desc_ioc_state = NULL;
4056 char *desc_scsi_status = NULL;
4057 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304058 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304059 struct _sas_device *sas_device = NULL;
4060 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304061 struct scsi_target *starget = scmd->device->sdev_target;
4062 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304063 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304064
4065 if (!priv_target)
4066 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304067
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304068 if (ioc->hide_ir_msg)
4069 device_str = "WarpDrive";
4070 else
4071 device_str = "volume";
4072
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304073 if (log_info == 0x31170000)
4074 return;
Eric Moore635374e2009-03-09 01:21:12 -06004075
4076 switch (ioc_status) {
4077 case MPI2_IOCSTATUS_SUCCESS:
4078 desc_ioc_state = "success";
4079 break;
4080 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4081 desc_ioc_state = "invalid function";
4082 break;
4083 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4084 desc_ioc_state = "scsi recovered error";
4085 break;
4086 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4087 desc_ioc_state = "scsi invalid dev handle";
4088 break;
4089 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4090 desc_ioc_state = "scsi device not there";
4091 break;
4092 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4093 desc_ioc_state = "scsi data overrun";
4094 break;
4095 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4096 desc_ioc_state = "scsi data underrun";
4097 break;
4098 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4099 desc_ioc_state = "scsi io data error";
4100 break;
4101 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4102 desc_ioc_state = "scsi protocol error";
4103 break;
4104 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4105 desc_ioc_state = "scsi task terminated";
4106 break;
4107 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4108 desc_ioc_state = "scsi residual mismatch";
4109 break;
4110 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4111 desc_ioc_state = "scsi task mgmt failed";
4112 break;
4113 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4114 desc_ioc_state = "scsi ioc terminated";
4115 break;
4116 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4117 desc_ioc_state = "scsi ext terminated";
4118 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004119 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4120 desc_ioc_state = "eedp guard error";
4121 break;
4122 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4123 desc_ioc_state = "eedp ref tag error";
4124 break;
4125 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4126 desc_ioc_state = "eedp app tag error";
4127 break;
Eric Moore635374e2009-03-09 01:21:12 -06004128 default:
4129 desc_ioc_state = "unknown";
4130 break;
4131 }
4132
4133 switch (scsi_status) {
4134 case MPI2_SCSI_STATUS_GOOD:
4135 desc_scsi_status = "good";
4136 break;
4137 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4138 desc_scsi_status = "check condition";
4139 break;
4140 case MPI2_SCSI_STATUS_CONDITION_MET:
4141 desc_scsi_status = "condition met";
4142 break;
4143 case MPI2_SCSI_STATUS_BUSY:
4144 desc_scsi_status = "busy";
4145 break;
4146 case MPI2_SCSI_STATUS_INTERMEDIATE:
4147 desc_scsi_status = "intermediate";
4148 break;
4149 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4150 desc_scsi_status = "intermediate condmet";
4151 break;
4152 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4153 desc_scsi_status = "reservation conflict";
4154 break;
4155 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4156 desc_scsi_status = "command terminated";
4157 break;
4158 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4159 desc_scsi_status = "task set full";
4160 break;
4161 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4162 desc_scsi_status = "aca active";
4163 break;
4164 case MPI2_SCSI_STATUS_TASK_ABORTED:
4165 desc_scsi_status = "task aborted";
4166 break;
4167 default:
4168 desc_scsi_status = "unknown";
4169 break;
4170 }
4171
4172 desc_scsi_state[0] = '\0';
4173 if (!scsi_state)
4174 desc_scsi_state = " ";
4175 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4176 strcat(desc_scsi_state, "response info ");
4177 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4178 strcat(desc_scsi_state, "state terminated ");
4179 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4180 strcat(desc_scsi_state, "no status ");
4181 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4182 strcat(desc_scsi_state, "autosense failed ");
4183 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4184 strcat(desc_scsi_state, "autosense valid ");
4185
4186 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304187
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304188 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304189 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4190 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304191 } else {
4192 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4193 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4194 priv_target->sas_address);
4195 if (sas_device) {
4196 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4197 "phy(%d)\n", ioc->name, sas_device->sas_address,
4198 sas_device->phy);
4199 printk(MPT2SAS_WARN_FMT
4200 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4201 ioc->name, sas_device->enclosure_logical_id,
4202 sas_device->slot);
4203 }
4204 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304205 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304206
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304207 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4208 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4209 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004210 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4211 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4212 scsi_get_resid(scmd));
4213 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4214 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4215 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4216 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4217 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4218 scsi_status, desc_scsi_state, scsi_state);
4219
4220 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4221 struct sense_info data;
4222 _scsih_normalize_sense(scmd->sense_buffer, &data);
4223 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304224 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4225 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004226 }
4227
4228 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4229 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4230 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304231 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004232 }
4233}
4234#endif
4235
4236/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304237 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004238 * @ioc: per adapter object
4239 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304240 * Context: process
4241 *
4242 * Return nothing.
4243 */
4244static void
4245_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4246{
4247 Mpi2SepReply_t mpi_reply;
4248 Mpi2SepRequest_t mpi_request;
4249
4250 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4251 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4252 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4253 mpi_request.SlotStatus =
4254 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4255 mpi_request.DevHandle = cpu_to_le16(handle);
4256 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4257 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4258 &mpi_request)) != 0) {
4259 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4260 __FILE__, __LINE__, __func__);
4261 return;
4262 }
4263
4264 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4265 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4266 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4267 le16_to_cpu(mpi_reply.IOCStatus),
4268 le32_to_cpu(mpi_reply.IOCLogInfo)));
4269 return;
4270 }
4271}
4272
4273/**
4274 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4275 * @ioc: per adapter object
4276 * @handle: device handle
4277 * Context: interrupt.
4278 *
4279 * Return nothing.
4280 */
4281static void
4282_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4283{
4284 struct fw_event_work *fw_event;
4285
4286 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4287 if (!fw_event)
4288 return;
4289 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4290 fw_event->device_handle = handle;
4291 fw_event->ioc = ioc;
4292 _scsih_fw_event_add(ioc, fw_event);
4293}
4294
4295/**
4296 * _scsih_smart_predicted_fault - process smart errors
4297 * @ioc: per adapter object
4298 * @handle: device handle
4299 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004300 *
4301 * Return nothing.
4302 */
4303static void
4304_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4305{
Eric Moore635374e2009-03-09 01:21:12 -06004306 struct scsi_target *starget;
4307 struct MPT2SAS_TARGET *sas_target_priv_data;
4308 Mpi2EventNotificationReply_t *event_reply;
4309 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4310 struct _sas_device *sas_device;
4311 ssize_t sz;
4312 unsigned long flags;
4313
4314 /* only handle non-raid devices */
4315 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4316 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4317 if (!sas_device) {
4318 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4319 return;
4320 }
4321 starget = sas_device->starget;
4322 sas_target_priv_data = starget->hostdata;
4323
4324 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4325 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4326 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4327 return;
4328 }
4329 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4330 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4331
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304332 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4333 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004334
4335 /* insert into event log */
4336 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4337 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b42011-11-07 22:05:21 +11004338 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004339 if (!event_reply) {
4340 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4341 ioc->name, __FILE__, __LINE__, __func__);
4342 return;
4343 }
4344
4345 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4346 event_reply->Event =
4347 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4348 event_reply->MsgLength = sz/4;
4349 event_reply->EventDataLength =
4350 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4351 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4352 event_reply->EventData;
4353 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4354 event_data->ASC = 0x5D;
4355 event_data->DevHandle = cpu_to_le16(handle);
4356 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4357 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4358 kfree(event_reply);
4359}
4360
4361/**
Eric Moored5d135b2009-05-18 13:02:08 -06004362 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004363 * @ioc: per adapter object
4364 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304365 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004366 * @reply: reply message frame(lower 32bit addr)
4367 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304368 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004369 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304370 * Return 1 meaning mf should be freed from _base_interrupt
4371 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004372 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304373static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304374_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004375{
4376 Mpi2SCSIIORequest_t *mpi_request;
4377 Mpi2SCSIIOReply_t *mpi_reply;
4378 struct scsi_cmnd *scmd;
4379 u16 ioc_status;
4380 u32 xfer_cnt;
4381 u8 scsi_state;
4382 u8 scsi_status;
4383 u32 log_info;
4384 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304385 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304386 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004387
4388 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304389 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004390 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304391 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004392
4393 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4394
4395 if (mpi_reply == NULL) {
4396 scmd->result = DID_OK << 16;
4397 goto out;
4398 }
4399
4400 sas_device_priv_data = scmd->device->hostdata;
4401 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4402 sas_device_priv_data->sas_target->deleted) {
4403 scmd->result = DID_NO_CONNECT << 16;
4404 goto out;
4405 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304406 /*
4407 * WARPDRIVE: If direct_io is set then it is directIO,
4408 * the failed direct I/O should be redirected to volume
4409 */
4410 if (_scsih_scsi_direct_io_get(ioc, smid)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304411 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4412 ioc->scsi_lookup[smid - 1].scmd = scmd;
4413 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304414 _scsih_scsi_direct_io_set(ioc, smid, 0);
4415 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4416 mpi_request->DevHandle =
4417 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4418 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4419 sas_device_priv_data->sas_target->handle);
4420 return 0;
4421 }
4422
Eric Moore635374e2009-03-09 01:21:12 -06004423
4424 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304425 scsi_state = mpi_reply->SCSIState;
4426 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4427 response_code =
4428 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004429 if (!sas_device_priv_data->tlr_snoop_check) {
4430 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304431 /* Make sure Device is not raid volume.
4432 * We do not expose raid functionality to upper layer for warpdrive.
4433 */
4434 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304435 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304436 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4437 sas_disable_tlr(scmd->device);
4438 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4439 }
Eric Moore635374e2009-03-09 01:21:12 -06004440 }
4441
4442 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4443 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4444 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4445 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4446 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4447 else
4448 log_info = 0;
4449 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004450 scsi_status = mpi_reply->SCSIStatus;
4451
4452 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4453 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4454 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4455 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4456 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4457 }
4458
4459 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4460 struct sense_info data;
4461 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4462 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004463 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004464 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004465 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004466 _scsih_normalize_sense(scmd->sense_buffer, &data);
4467 /* failure prediction threshold exceeded */
4468 if (data.asc == 0x5D)
4469 _scsih_smart_predicted_fault(ioc,
4470 le16_to_cpu(mpi_reply->DevHandle));
4471 }
4472
4473 switch (ioc_status) {
4474 case MPI2_IOCSTATUS_BUSY:
4475 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4476 scmd->result = SAM_STAT_BUSY;
4477 break;
4478
4479 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4480 scmd->result = DID_NO_CONNECT << 16;
4481 break;
4482
4483 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4484 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304485 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4486 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004487 }
Eric Moore635374e2009-03-09 01:21:12 -06004488 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4489 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4490 scmd->result = DID_RESET << 16;
4491 break;
4492
4493 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4494 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4495 scmd->result = DID_SOFT_ERROR << 16;
4496 else
4497 scmd->result = (DID_OK << 16) | scsi_status;
4498 break;
4499
4500 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4501 scmd->result = (DID_OK << 16) | scsi_status;
4502
4503 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4504 break;
4505
4506 if (xfer_cnt < scmd->underflow) {
4507 if (scsi_status == SAM_STAT_BUSY)
4508 scmd->result = SAM_STAT_BUSY;
4509 else
4510 scmd->result = DID_SOFT_ERROR << 16;
4511 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4512 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4513 scmd->result = DID_SOFT_ERROR << 16;
4514 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4515 scmd->result = DID_RESET << 16;
4516 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4517 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4518 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4519 scmd->result = (DRIVER_SENSE << 24) |
4520 SAM_STAT_CHECK_CONDITION;
4521 scmd->sense_buffer[0] = 0x70;
4522 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4523 scmd->sense_buffer[12] = 0x20;
4524 scmd->sense_buffer[13] = 0;
4525 }
4526 break;
4527
4528 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4529 scsi_set_resid(scmd, 0);
4530 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4531 case MPI2_IOCSTATUS_SUCCESS:
4532 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304533 if (response_code ==
4534 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4535 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4536 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004537 scmd->result = DID_SOFT_ERROR << 16;
4538 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4539 scmd->result = DID_RESET << 16;
4540 break;
4541
Eric Moore3c621b32009-05-18 12:59:41 -06004542 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4543 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4544 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4545 _scsih_eedp_error_handling(scmd, ioc_status);
4546 break;
Eric Moore635374e2009-03-09 01:21:12 -06004547 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4548 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4549 case MPI2_IOCSTATUS_INVALID_SGL:
4550 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4551 case MPI2_IOCSTATUS_INVALID_FIELD:
4552 case MPI2_IOCSTATUS_INVALID_STATE:
4553 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4554 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4555 default:
4556 scmd->result = DID_SOFT_ERROR << 16;
4557 break;
4558
4559 }
4560
4561#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4562 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4563 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4564#endif
4565
4566 out:
4567 scsi_dma_unmap(scmd);
4568 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304569 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004570}
4571
4572/**
Eric Moore635374e2009-03-09 01:21:12 -06004573 * _scsih_sas_host_refresh - refreshing sas host object contents
4574 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004575 * Context: user
4576 *
4577 * During port enable, fw will send topology events for every device. Its
4578 * possible that the handles may change from the previous setting, so this
4579 * code keeping handles updating if changed.
4580 *
4581 * Return nothing.
4582 */
4583static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304584_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004585{
4586 u16 sz;
4587 u16 ioc_status;
4588 int i;
4589 Mpi2ConfigReply_t mpi_reply;
4590 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304591 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304592 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004593
4594 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4595 "updating handles for sas_host(0x%016llx)\n",
4596 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4597
4598 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4599 * sizeof(Mpi2SasIOUnit0PhyData_t));
4600 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4601 if (!sas_iounit_pg0) {
4602 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4603 ioc->name, __FILE__, __LINE__, __func__);
4604 return;
4605 }
Eric Moore635374e2009-03-09 01:21:12 -06004606
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304607 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4608 sas_iounit_pg0, sz)) != 0)
4609 goto out;
4610 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4611 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4612 goto out;
4613 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304614 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304615 if (i == 0)
4616 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4617 PhyData[0].ControllerDevHandle);
4618 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4619 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4620 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304621 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4622 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304623 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304624 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304625 }
Eric Moore635374e2009-03-09 01:21:12 -06004626 out:
4627 kfree(sas_iounit_pg0);
4628}
4629
4630/**
4631 * _scsih_sas_host_add - create sas host object
4632 * @ioc: per adapter object
4633 *
4634 * Creating host side data object, stored in ioc->sas_hba
4635 *
4636 * Return nothing.
4637 */
4638static void
4639_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4640{
4641 int i;
4642 Mpi2ConfigReply_t mpi_reply;
4643 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4644 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4645 Mpi2SasPhyPage0_t phy_pg0;
4646 Mpi2SasDevicePage0_t sas_device_pg0;
4647 Mpi2SasEnclosurePage0_t enclosure_pg0;
4648 u16 ioc_status;
4649 u16 sz;
4650 u16 device_missing_delay;
4651
4652 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4653 if (!ioc->sas_hba.num_phys) {
4654 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4655 ioc->name, __FILE__, __LINE__, __func__);
4656 return;
4657 }
4658
4659 /* sas_iounit page 0 */
4660 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4661 sizeof(Mpi2SasIOUnit0PhyData_t));
4662 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4663 if (!sas_iounit_pg0) {
4664 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4665 ioc->name, __FILE__, __LINE__, __func__);
4666 return;
4667 }
4668 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4669 sas_iounit_pg0, sz))) {
4670 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4671 ioc->name, __FILE__, __LINE__, __func__);
4672 goto out;
4673 }
4674 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4675 MPI2_IOCSTATUS_MASK;
4676 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4677 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4678 ioc->name, __FILE__, __LINE__, __func__);
4679 goto out;
4680 }
4681
4682 /* sas_iounit page 1 */
4683 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4684 sizeof(Mpi2SasIOUnit1PhyData_t));
4685 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4686 if (!sas_iounit_pg1) {
4687 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4688 ioc->name, __FILE__, __LINE__, __func__);
4689 goto out;
4690 }
4691 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4692 sas_iounit_pg1, sz))) {
4693 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4694 ioc->name, __FILE__, __LINE__, __func__);
4695 goto out;
4696 }
4697 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4698 MPI2_IOCSTATUS_MASK;
4699 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4700 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4701 ioc->name, __FILE__, __LINE__, __func__);
4702 goto out;
4703 }
4704
4705 ioc->io_missing_delay =
4706 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4707 device_missing_delay =
4708 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4709 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4710 ioc->device_missing_delay = (device_missing_delay &
4711 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4712 else
4713 ioc->device_missing_delay = device_missing_delay &
4714 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4715
4716 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4717 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4718 sizeof(struct _sas_phy), GFP_KERNEL);
4719 if (!ioc->sas_hba.phy) {
4720 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4721 ioc->name, __FILE__, __LINE__, __func__);
4722 goto out;
4723 }
4724 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4725 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4726 i))) {
4727 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4728 ioc->name, __FILE__, __LINE__, __func__);
4729 goto out;
4730 }
4731 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4732 MPI2_IOCSTATUS_MASK;
4733 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4734 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4735 ioc->name, __FILE__, __LINE__, __func__);
4736 goto out;
4737 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304738
4739 if (i == 0)
4740 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4741 PhyData[0].ControllerDevHandle);
4742 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004743 ioc->sas_hba.phy[i].phy_id = i;
4744 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4745 phy_pg0, ioc->sas_hba.parent_dev);
4746 }
4747 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304748 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004749 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4750 ioc->name, __FILE__, __LINE__, __func__);
4751 goto out;
4752 }
Eric Moore635374e2009-03-09 01:21:12 -06004753 ioc->sas_hba.enclosure_handle =
4754 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4755 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4756 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4757 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4758 (unsigned long long) ioc->sas_hba.sas_address,
4759 ioc->sas_hba.num_phys) ;
4760
4761 if (ioc->sas_hba.enclosure_handle) {
4762 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4763 &enclosure_pg0,
4764 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4765 ioc->sas_hba.enclosure_handle))) {
4766 ioc->sas_hba.enclosure_logical_id =
4767 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4768 }
4769 }
4770
4771 out:
4772 kfree(sas_iounit_pg1);
4773 kfree(sas_iounit_pg0);
4774}
4775
4776/**
4777 * _scsih_expander_add - creating expander object
4778 * @ioc: per adapter object
4779 * @handle: expander handle
4780 *
4781 * Creating expander object, stored in ioc->sas_expander_list.
4782 *
4783 * Return 0 for success, else error.
4784 */
4785static int
4786_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4787{
4788 struct _sas_node *sas_expander;
4789 Mpi2ConfigReply_t mpi_reply;
4790 Mpi2ExpanderPage0_t expander_pg0;
4791 Mpi2ExpanderPage1_t expander_pg1;
4792 Mpi2SasEnclosurePage0_t enclosure_pg0;
4793 u32 ioc_status;
4794 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304795 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004796 int i;
4797 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304798 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004799 int rc = 0;
4800
4801 if (!handle)
4802 return -1;
4803
Eric Moore3cb54692010-07-08 14:44:34 -06004804 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304805 return -1;
4806
Eric Moore635374e2009-03-09 01:21:12 -06004807 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4808 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4809 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4810 ioc->name, __FILE__, __LINE__, __func__);
4811 return -1;
4812 }
4813
4814 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4815 MPI2_IOCSTATUS_MASK;
4816 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4817 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4818 ioc->name, __FILE__, __LINE__, __func__);
4819 return -1;
4820 }
4821
4822 /* handle out of order topology events */
4823 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304824 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4825 != 0) {
4826 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4827 ioc->name, __FILE__, __LINE__, __func__);
4828 return -1;
4829 }
4830 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004831 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304832 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4833 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004834 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4835 if (!sas_expander) {
4836 rc = _scsih_expander_add(ioc, parent_handle);
4837 if (rc != 0)
4838 return rc;
4839 }
4840 }
4841
Eric Moore635374e2009-03-09 01:21:12 -06004842 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304843 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004844 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4845 sas_address);
4846 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4847
4848 if (sas_expander)
4849 return 0;
4850
4851 sas_expander = kzalloc(sizeof(struct _sas_node),
4852 GFP_KERNEL);
4853 if (!sas_expander) {
4854 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4855 ioc->name, __FILE__, __LINE__, __func__);
4856 return -1;
4857 }
4858
4859 sas_expander->handle = handle;
4860 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304861 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004862 sas_expander->sas_address = sas_address;
4863
4864 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4865 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304866 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004867 sas_expander->sas_address, sas_expander->num_phys);
4868
4869 if (!sas_expander->num_phys)
4870 goto out_fail;
4871 sas_expander->phy = kcalloc(sas_expander->num_phys,
4872 sizeof(struct _sas_phy), GFP_KERNEL);
4873 if (!sas_expander->phy) {
4874 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4875 ioc->name, __FILE__, __LINE__, __func__);
4876 rc = -1;
4877 goto out_fail;
4878 }
4879
4880 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4881 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304882 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004883 if (!mpt2sas_port) {
4884 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4885 ioc->name, __FILE__, __LINE__, __func__);
4886 rc = -1;
4887 goto out_fail;
4888 }
4889 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4890
4891 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4892 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4893 &expander_pg1, i, handle))) {
4894 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4895 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304896 rc = -1;
4897 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004898 }
4899 sas_expander->phy[i].handle = handle;
4900 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304901
4902 if ((mpt2sas_transport_add_expander_phy(ioc,
4903 &sas_expander->phy[i], expander_pg1,
4904 sas_expander->parent_dev))) {
4905 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4906 ioc->name, __FILE__, __LINE__, __func__);
4907 rc = -1;
4908 goto out_fail;
4909 }
Eric Moore635374e2009-03-09 01:21:12 -06004910 }
4911
4912 if (sas_expander->enclosure_handle) {
4913 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4914 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4915 sas_expander->enclosure_handle))) {
4916 sas_expander->enclosure_logical_id =
4917 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4918 }
4919 }
4920
4921 _scsih_expander_node_add(ioc, sas_expander);
4922 return 0;
4923
4924 out_fail:
4925
Kashyap, Desai20f58952009-08-07 19:34:26 +05304926 if (mpt2sas_port)
4927 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304928 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004929 kfree(sas_expander);
4930 return rc;
4931}
4932
4933/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304934 * _scsih_done - scsih callback handler.
4935 * @ioc: per adapter object
4936 * @smid: system request message index
4937 * @msix_index: MSIX table index supplied by the OS
4938 * @reply: reply message frame(lower 32bit addr)
4939 *
4940 * Callback handler when sending internal generated message frames.
4941 * The callback index passed is `ioc->scsih_cb_idx`
4942 *
4943 * Return 1 meaning mf should be freed from _base_interrupt
4944 * 0 means the mf is freed from this function.
4945 */
4946static u8
4947_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4948{
4949 MPI2DefaultReply_t *mpi_reply;
4950
4951 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4952 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4953 return 1;
4954 if (ioc->scsih_cmds.smid != smid)
4955 return 1;
4956 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4957 if (mpi_reply) {
4958 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4959 mpi_reply->MsgLength*4);
4960 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4961 }
4962 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4963 complete(&ioc->scsih_cmds.done);
4964 return 1;
4965}
4966
4967/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304968 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004969 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304970 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004971 *
4972 * Return nothing.
4973 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304974void
4975mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004976{
4977 struct _sas_node *sas_expander;
4978 unsigned long flags;
4979
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304980 if (ioc->shost_recovery)
4981 return;
4982
Eric Moore635374e2009-03-09 01:21:12 -06004983 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304984 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4985 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304986 if (!sas_expander) {
4987 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4988 return;
4989 }
4990 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004991 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4992 _scsih_expander_node_remove(ioc, sas_expander);
4993}
4994
4995/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304996 * _scsih_check_access_status - check access flags
4997 * @ioc: per adapter object
4998 * @sas_address: sas address
4999 * @handle: sas device handle
5000 * @access_flags: errors returned during discovery of the device
5001 *
5002 * Return 0 for success, else failure
5003 */
5004static u8
5005_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5006 u16 handle, u8 access_status)
5007{
5008 u8 rc = 1;
5009 char *desc = NULL;
5010
5011 switch (access_status) {
5012 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5013 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5014 rc = 0;
5015 break;
5016 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5017 desc = "sata capability failed";
5018 break;
5019 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5020 desc = "sata affiliation conflict";
5021 break;
5022 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5023 desc = "route not addressable";
5024 break;
5025 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5026 desc = "smp error not addressable";
5027 break;
5028 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5029 desc = "device blocked";
5030 break;
5031 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5032 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5033 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5034 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5035 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5036 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5037 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5038 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5039 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5040 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5041 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5042 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5043 desc = "sata initialization failed";
5044 break;
5045 default:
5046 desc = "unknown";
5047 break;
5048 }
5049
5050 if (!rc)
5051 return 0;
5052
5053 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5054 "handle(0x%04x)\n", ioc->name, desc,
5055 (unsigned long long)sas_address, handle);
5056 return rc;
5057}
5058
5059static void
5060_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5061{
5062 Mpi2ConfigReply_t mpi_reply;
5063 Mpi2SasDevicePage0_t sas_device_pg0;
5064 struct _sas_device *sas_device;
5065 u32 ioc_status;
5066 unsigned long flags;
5067 u64 sas_address;
5068 struct scsi_target *starget;
5069 struct MPT2SAS_TARGET *sas_target_priv_data;
5070 u32 device_info;
5071
5072 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5073 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5074 return;
5075
5076 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5077 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5078 return;
5079
5080 /* check if this is end device */
5081 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5082 if (!(_scsih_is_end_device(device_info)))
5083 return;
5084
5085 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5086 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5087 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5088 sas_address);
5089
5090 if (!sas_device) {
5091 printk(MPT2SAS_ERR_FMT "device is not present "
5092 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5093 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5094 return;
5095 }
5096
5097 if (unlikely(sas_device->handle != handle)) {
5098 starget = sas_device->starget;
5099 sas_target_priv_data = starget->hostdata;
5100 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5101 " to (0x%04x)!!!\n", sas_device->handle, handle);
5102 sas_target_priv_data->handle = handle;
5103 sas_device->handle = handle;
5104 }
5105 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5106
5107 /* check if device is present */
5108 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5109 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5110 printk(MPT2SAS_ERR_FMT "device is not present "
5111 "handle(0x%04x), flags!!!\n", ioc->name, handle);
5112 return;
5113 }
5114
5115 /* check if there were any issues with discovery */
5116 if (_scsih_check_access_status(ioc, sas_address, handle,
5117 sas_device_pg0.AccessStatus))
5118 return;
5119 _scsih_ublock_io_device(ioc, handle);
5120
5121}
5122
5123/**
Eric Moore635374e2009-03-09 01:21:12 -06005124 * _scsih_add_device - creating sas device object
5125 * @ioc: per adapter object
5126 * @handle: sas device handle
5127 * @phy_num: phy number end device attached to
5128 * @is_pd: is this hidden raid component
5129 *
5130 * Creating end device object, stored in ioc->sas_device_list.
5131 *
5132 * Returns 0 for success, non-zero for failure.
5133 */
5134static int
5135_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5136{
5137 Mpi2ConfigReply_t mpi_reply;
5138 Mpi2SasDevicePage0_t sas_device_pg0;
5139 Mpi2SasEnclosurePage0_t enclosure_pg0;
5140 struct _sas_device *sas_device;
5141 u32 ioc_status;
5142 __le64 sas_address;
5143 u32 device_info;
5144 unsigned long flags;
5145
5146 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5147 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5148 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5149 ioc->name, __FILE__, __LINE__, __func__);
5150 return -1;
5151 }
5152
5153 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5154 MPI2_IOCSTATUS_MASK;
5155 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5156 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5157 ioc->name, __FILE__, __LINE__, __func__);
5158 return -1;
5159 }
5160
Kashyap, Desaib4344272010-03-17 16:24:14 +05305161 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5162
Eric Moore635374e2009-03-09 01:21:12 -06005163 /* check if device is present */
5164 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5165 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5166 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5167 ioc->name, __FILE__, __LINE__, __func__);
5168 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5169 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5170 return -1;
5171 }
5172
Kashyap, Desaib4344272010-03-17 16:24:14 +05305173 /* check if there were any issues with discovery */
5174 if (_scsih_check_access_status(ioc, sas_address, handle,
5175 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005176 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005177
5178 /* check if this is end device */
5179 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5180 if (!(_scsih_is_end_device(device_info))) {
5181 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5182 ioc->name, __FILE__, __LINE__, __func__);
5183 return -1;
5184 }
5185
Eric Moore635374e2009-03-09 01:21:12 -06005186
5187 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5188 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5189 sas_address);
5190 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5191
Kashyap, Desaib4344272010-03-17 16:24:14 +05305192 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005193 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005194
5195 sas_device = kzalloc(sizeof(struct _sas_device),
5196 GFP_KERNEL);
5197 if (!sas_device) {
5198 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5199 ioc->name, __FILE__, __LINE__, __func__);
5200 return -1;
5201 }
5202
5203 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305204 if (_scsih_get_sas_address(ioc, le16_to_cpu
5205 (sas_device_pg0.ParentDevHandle),
5206 &sas_device->sas_address_parent) != 0)
5207 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5208 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005209 sas_device->enclosure_handle =
5210 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5211 sas_device->slot =
5212 le16_to_cpu(sas_device_pg0.Slot);
5213 sas_device->device_info = device_info;
5214 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305215 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005216
5217 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305218 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5219 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5220 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005221 sas_device->enclosure_logical_id =
5222 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005223
5224 /* get device name */
5225 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5226
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305227 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005228 _scsih_sas_device_init_add(ioc, sas_device);
5229 else
5230 _scsih_sas_device_add(ioc, sas_device);
5231
5232 return 0;
5233}
5234
5235/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305236 * _scsih_remove_device - removing sas device object
5237 * @ioc: per adapter object
5238 * @sas_device_delete: the sas_device object
5239 *
5240 * Return nothing.
5241 */
5242static void
5243_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5244 struct _sas_device *sas_device)
5245{
5246 struct _sas_device sas_device_backup;
5247 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305248
Kashyap, Desai1278b112010-03-09 17:34:13 +05305249 if (!sas_device)
5250 return;
Eric Moore635374e2009-03-09 01:21:12 -06005251
Kashyap, Desai1278b112010-03-09 17:34:13 +05305252 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06005253 _scsih_sas_device_remove(ioc, sas_device);
5254
Kashyap, Desai1278b112010-03-09 17:34:13 +05305255 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5256 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5257 sas_device_backup.handle, (unsigned long long)
5258 sas_device_backup.sas_address));
5259
5260 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5261 sas_target_priv_data = sas_device_backup.starget->hostdata;
5262 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305263 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5264 sas_target_priv_data->handle =
5265 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305266 }
5267
Kashyap, Desai1278b112010-03-09 17:34:13 +05305268 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5269
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305270 if (!ioc->hide_drives)
5271 mpt2sas_transport_port_remove(ioc,
5272 sas_device_backup.sas_address,
5273 sas_device_backup.sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305274
5275 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5276 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5277 (unsigned long long) sas_device_backup.sas_address);
5278
5279 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5280 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5281 sas_device_backup.handle, (unsigned long long)
5282 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06005283}
5284
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305285/**
5286 * mpt2sas_device_remove - removing device object
5287 * @ioc: per adapter object
5288 * @sas_address: expander sas_address
5289 *
5290 * Return nothing.
5291 */
5292void
5293mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5294{
5295 struct _sas_device *sas_device;
5296 unsigned long flags;
5297
5298 if (ioc->shost_recovery)
5299 return;
5300
5301 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5302 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5303 sas_address);
5304 if (!sas_device) {
5305 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5306 return;
5307 }
5308 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5309 _scsih_remove_device(ioc, sas_device);
5310}
5311
Eric Moore635374e2009-03-09 01:21:12 -06005312#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5313/**
5314 * _scsih_sas_topology_change_event_debug - debug for topology event
5315 * @ioc: per adapter object
5316 * @event_data: event data payload
5317 * Context: user.
5318 */
5319static void
5320_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5321 Mpi2EventDataSasTopologyChangeList_t *event_data)
5322{
5323 int i;
5324 u16 handle;
5325 u16 reason_code;
5326 u8 phy_number;
5327 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305328 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005329
5330 switch (event_data->ExpStatus) {
5331 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5332 status_str = "add";
5333 break;
5334 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5335 status_str = "remove";
5336 break;
5337 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305338 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005339 status_str = "responding";
5340 break;
5341 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5342 status_str = "remove delay";
5343 break;
5344 default:
5345 status_str = "unknown status";
5346 break;
5347 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305348 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005349 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305350 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005351 "start_phy(%02d), count(%d)\n",
5352 le16_to_cpu(event_data->ExpanderDevHandle),
5353 le16_to_cpu(event_data->EnclosureHandle),
5354 event_data->StartPhyNum, event_data->NumEntries);
5355 for (i = 0; i < event_data->NumEntries; i++) {
5356 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5357 if (!handle)
5358 continue;
5359 phy_number = event_data->StartPhyNum + i;
5360 reason_code = event_data->PHY[i].PhyStatus &
5361 MPI2_EVENT_SAS_TOPO_RC_MASK;
5362 switch (reason_code) {
5363 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305364 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005365 break;
5366 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305367 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005368 break;
5369 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305370 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005371 break;
5372 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305373 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005374 break;
5375 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305376 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005377 break;
5378 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305379 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005380 break;
5381 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305382 link_rate = event_data->PHY[i].LinkRate >> 4;
5383 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305384 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305385 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5386 handle, status_str, link_rate, prev_link_rate);
5387
Eric Moore635374e2009-03-09 01:21:12 -06005388 }
5389}
5390#endif
5391
5392/**
5393 * _scsih_sas_topology_change_event - handle topology changes
5394 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305395 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005396 * Context: user.
5397 *
5398 */
5399static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305400_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005401 struct fw_event_work *fw_event)
5402{
5403 int i;
5404 u16 parent_handle, handle;
5405 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305406 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005407 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305408 struct _sas_device *sas_device;
5409 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005410 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305411 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305412 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005413
5414#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5415 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5416 _scsih_sas_topology_change_event_debug(ioc, event_data);
5417#endif
5418
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305419 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305420 return;
5421
Eric Moore635374e2009-03-09 01:21:12 -06005422 if (!ioc->sas_hba.num_phys)
5423 _scsih_sas_host_add(ioc);
5424 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305425 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005426
5427 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305428 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005429 "event\n", ioc->name));
5430 return;
5431 }
5432
5433 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5434
5435 /* handle expander add */
5436 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5437 if (_scsih_expander_add(ioc, parent_handle) != 0)
5438 return;
5439
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305440 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5441 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5442 parent_handle);
5443 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305444 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305445 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305446 max_phys = sas_expander->num_phys;
5447 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305448 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305449 max_phys = ioc->sas_hba.num_phys;
5450 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305451 return;
5452
Eric Moore635374e2009-03-09 01:21:12 -06005453 /* handle siblings events */
5454 for (i = 0; i < event_data->NumEntries; i++) {
5455 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305456 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005457 "expander event\n", ioc->name));
5458 return;
5459 }
Eric Moore3cb54692010-07-08 14:44:34 -06005460 if (ioc->shost_recovery || ioc->remove_host ||
5461 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305462 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305463 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305464 if (phy_number >= max_phys)
5465 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305466 reason_code = event_data->PHY[i].PhyStatus &
5467 MPI2_EVENT_SAS_TOPO_RC_MASK;
5468 if ((event_data->PHY[i].PhyStatus &
5469 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5470 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005471 continue;
5472 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5473 if (!handle)
5474 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305475 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305476 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005477 switch (reason_code) {
5478 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305479
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305480 if (ioc->shost_recovery)
5481 break;
5482
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305483 if (link_rate == prev_link_rate)
5484 break;
5485
5486 mpt2sas_transport_update_links(ioc, sas_address,
5487 handle, phy_number, link_rate);
5488
Kashyap, Desaib4344272010-03-17 16:24:14 +05305489 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5490 break;
5491
5492 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305493 break;
Eric Moore635374e2009-03-09 01:21:12 -06005494 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305495
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305496 if (ioc->shost_recovery)
5497 break;
5498
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305499 mpt2sas_transport_update_links(ioc, sas_address,
5500 handle, phy_number, link_rate);
5501
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305502 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005503 break;
5504 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305505
5506 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5507 sas_device = _scsih_sas_device_find_by_handle(ioc,
5508 handle);
5509 if (!sas_device) {
5510 spin_unlock_irqrestore(&ioc->sas_device_lock,
5511 flags);
5512 break;
5513 }
5514 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5515 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005516 break;
5517 }
5518 }
5519
5520 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305521 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5522 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305523 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005524
5525}
5526
5527#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5528/**
5529 * _scsih_sas_device_status_change_event_debug - debug for device event
5530 * @event_data: event data payload
5531 * Context: user.
5532 *
5533 * Return nothing.
5534 */
5535static void
5536_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5537 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5538{
5539 char *reason_str = NULL;
5540
5541 switch (event_data->ReasonCode) {
5542 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5543 reason_str = "smart data";
5544 break;
5545 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5546 reason_str = "unsupported device discovered";
5547 break;
5548 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5549 reason_str = "internal device reset";
5550 break;
5551 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5552 reason_str = "internal task abort";
5553 break;
5554 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5555 reason_str = "internal task abort set";
5556 break;
5557 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5558 reason_str = "internal clear task set";
5559 break;
5560 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5561 reason_str = "internal query task";
5562 break;
5563 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5564 reason_str = "sata init failure";
5565 break;
5566 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5567 reason_str = "internal device reset complete";
5568 break;
5569 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5570 reason_str = "internal task abort complete";
5571 break;
5572 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5573 reason_str = "internal async notification";
5574 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305575 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5576 reason_str = "expander reduced functionality";
5577 break;
5578 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5579 reason_str = "expander reduced functionality complete";
5580 break;
Eric Moore635374e2009-03-09 01:21:12 -06005581 default:
5582 reason_str = "unknown reason";
5583 break;
5584 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305585 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305586 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5587 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5588 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5589 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005590 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305591 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005592 event_data->ASC, event_data->ASCQ);
5593 printk(KERN_INFO "\n");
5594}
5595#endif
5596
5597/**
5598 * _scsih_sas_device_status_change_event - handle device status change
5599 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305600 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005601 * Context: user.
5602 *
5603 * Return nothing.
5604 */
5605static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305606_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5607 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005608{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305609 struct MPT2SAS_TARGET *target_priv_data;
5610 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305611 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305612 unsigned long flags;
5613 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5614 fw_event->event_data;
5615
Eric Moore635374e2009-03-09 01:21:12 -06005616#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5617 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305618 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305619 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005620#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305621
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305622 /* In MPI Revision K (0xC), the internal device reset complete was
5623 * implemented, so avoid setting tm_busy flag for older firmware.
5624 */
5625 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5626 return;
5627
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305628 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305629 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305630 event_data->ReasonCode !=
5631 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305632 return;
5633
5634 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5635 sas_address = le64_to_cpu(event_data->SASAddress);
5636 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5637 sas_address);
5638 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5639
5640 if (!sas_device || !sas_device->starget)
5641 return;
5642
5643 target_priv_data = sas_device->starget->hostdata;
5644 if (!target_priv_data)
5645 return;
5646
5647 if (event_data->ReasonCode ==
5648 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5649 target_priv_data->tm_busy = 1;
5650 else
5651 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005652}
5653
5654#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5655/**
5656 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5657 * @ioc: per adapter object
5658 * @event_data: event data payload
5659 * Context: user.
5660 *
5661 * Return nothing.
5662 */
5663static void
5664_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5665 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5666{
5667 char *reason_str = NULL;
5668
5669 switch (event_data->ReasonCode) {
5670 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5671 reason_str = "enclosure add";
5672 break;
5673 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5674 reason_str = "enclosure remove";
5675 break;
5676 default:
5677 reason_str = "unknown reason";
5678 break;
5679 }
5680
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305681 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005682 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5683 " number slots(%d)\n", ioc->name, reason_str,
5684 le16_to_cpu(event_data->EnclosureHandle),
5685 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5686 le16_to_cpu(event_data->StartSlot));
5687}
5688#endif
5689
5690/**
5691 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5692 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305693 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005694 * Context: user.
5695 *
5696 * Return nothing.
5697 */
5698static void
5699_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305700 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005701{
5702#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5703 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5704 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305705 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005706#endif
5707}
5708
5709/**
5710 * _scsih_sas_broadcast_primative_event - handle broadcast events
5711 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305712 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005713 * Context: user.
5714 *
5715 * Return nothing.
5716 */
5717static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305718_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5719 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005720{
5721 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305722 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005723 u16 smid, handle;
5724 u32 lun;
5725 struct MPT2SAS_DEVICE *sas_device_priv_data;
5726 u32 termination_count;
5727 u32 query_count;
5728 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305729 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305730 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305731 unsigned long flags;
5732 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305733 u8 max_retries = 0;
5734 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305735
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305736 mutex_lock(&ioc->tm_cmds.mutex);
5737 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5738 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5739 event_data->PortWidth));
5740
5741 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005742
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305743 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305744 mpi_reply = ioc->tm_cmds.reply;
5745broadcast_aen_retry:
5746
5747 /* sanity checks for retrying this loop */
5748 if (max_retries++ == 5) {
5749 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5750 ioc->name, __func__));
5751 goto out;
5752 } else if (max_retries > 1)
5753 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5754 ioc->name, __func__, max_retries - 1));
5755
Eric Moore635374e2009-03-09 01:21:12 -06005756 termination_count = 0;
5757 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305758 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305759 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305760 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005761 scmd = _scsih_scsi_lookup_get(ioc, smid);
5762 if (!scmd)
5763 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305764 sdev = scmd->device;
5765 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005766 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5767 continue;
5768 /* skip hidden raid components */
5769 if (sas_device_priv_data->sas_target->flags &
5770 MPT_TARGET_FLAGS_RAID_COMPONENT)
5771 continue;
5772 /* skip volumes */
5773 if (sas_device_priv_data->sas_target->flags &
5774 MPT_TARGET_FLAGS_VOLUME)
5775 continue;
5776
5777 handle = sas_device_priv_data->sas_target->handle;
5778 lun = sas_device_priv_data->lun;
5779 query_count++;
5780
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305781 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305782 goto out;
5783
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305784 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305785 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5786 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5787 TM_MUTEX_OFF);
5788 if (r == FAILED) {
5789 sdev_printk(KERN_WARNING, sdev,
5790 "mpt2sas_scsih_issue_tm: FAILED when sending "
5791 "QUERY_TASK: scmd(%p)\n", scmd);
5792 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5793 goto broadcast_aen_retry;
5794 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305795 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5796 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305797 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5798 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5799 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5800 scmd);
5801 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5802 goto broadcast_aen_retry;
5803 }
5804
5805 /* see if IO is still owned by IOC and target */
5806 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005807 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5808 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305809 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305810 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005811 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305812 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305813 task_abort_retries = 0;
5814 tm_retry:
5815 if (task_abort_retries++ == 60) {
5816 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5817 "%s: ABORT_TASK: giving up\n", ioc->name,
5818 __func__));
5819 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5820 goto broadcast_aen_retry;
5821 }
5822
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305823 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305824 goto out_no_lock;
5825
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305826 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5827 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305828 scmd->serial_number, TM_MUTEX_OFF);
5829 if (r == FAILED) {
5830 sdev_printk(KERN_WARNING, sdev,
5831 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305832 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305833 goto tm_retry;
5834 }
5835
5836 if (task_abort_retries > 1)
5837 sdev_printk(KERN_WARNING, sdev,
5838 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5839 " scmd(%p)\n",
5840 task_abort_retries - 1, scmd);
5841
Eric Moore635374e2009-03-09 01:21:12 -06005842 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305843 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005844 }
Eric Moore635374e2009-03-09 01:21:12 -06005845
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305846 if (ioc->broadcast_aen_pending) {
5847 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5848 " pending AEN\n", ioc->name, __func__));
5849 ioc->broadcast_aen_pending = 0;
5850 goto broadcast_aen_retry;
5851 }
5852
5853 out:
5854 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5855 out_no_lock:
5856
5857 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005858 "%s - exit, query_count = %d termination_count = %d\n",
5859 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305860
5861 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305862 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305863 _scsih_ublock_io_all_device(ioc);
5864 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005865}
5866
5867/**
5868 * _scsih_sas_discovery_event - handle discovery events
5869 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305870 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005871 * Context: user.
5872 *
5873 * Return nothing.
5874 */
5875static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305876_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5877 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005878{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305879 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5880
Eric Moore635374e2009-03-09 01:21:12 -06005881#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5882 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305883 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005884 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5885 "start" : "stop");
5886 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305887 printk("discovery_status(0x%08x)",
5888 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005889 printk("\n");
5890 }
5891#endif
5892
5893 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5894 !ioc->sas_hba.num_phys)
5895 _scsih_sas_host_add(ioc);
5896}
5897
5898/**
5899 * _scsih_reprobe_lun - reprobing lun
5900 * @sdev: scsi device struct
5901 * @no_uld_attach: sdev->no_uld_attach flag setting
5902 *
5903 **/
5904static void
5905_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5906{
5907 int rc;
5908
5909 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5910 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5911 sdev->no_uld_attach ? "hidding" : "exposing");
5912 rc = scsi_device_reprobe(sdev);
5913}
5914
5915/**
5916 * _scsih_reprobe_target - reprobing target
5917 * @starget: scsi target struct
5918 * @no_uld_attach: sdev->no_uld_attach flag setting
5919 *
5920 * Note: no_uld_attach flag determines whether the disk device is attached
5921 * to block layer. A value of `1` means to not attach.
5922 **/
5923static void
5924_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5925{
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305926 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06005927
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305928 if (starget == NULL)
5929 return;
5930 sas_target_priv_data = starget->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005931 if (no_uld_attach)
5932 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5933 else
5934 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5935
5936 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5937 _scsih_reprobe_lun);
5938}
5939/**
5940 * _scsih_sas_volume_add - add new volume
5941 * @ioc: per adapter object
5942 * @element: IR config element data
5943 * Context: user.
5944 *
5945 * Return nothing.
5946 */
5947static void
5948_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5949 Mpi2EventIrConfigElement_t *element)
5950{
5951 struct _raid_device *raid_device;
5952 unsigned long flags;
5953 u64 wwid;
5954 u16 handle = le16_to_cpu(element->VolDevHandle);
5955 int rc;
5956
Eric Moore635374e2009-03-09 01:21:12 -06005957 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5958 if (!wwid) {
5959 printk(MPT2SAS_ERR_FMT
5960 "failure at %s:%d/%s()!\n", ioc->name,
5961 __FILE__, __LINE__, __func__);
5962 return;
5963 }
5964
5965 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5966 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5967 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5968
5969 if (raid_device)
5970 return;
5971
5972 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5973 if (!raid_device) {
5974 printk(MPT2SAS_ERR_FMT
5975 "failure at %s:%d/%s()!\n", ioc->name,
5976 __FILE__, __LINE__, __func__);
5977 return;
5978 }
5979
5980 raid_device->id = ioc->sas_id++;
5981 raid_device->channel = RAID_CHANNEL;
5982 raid_device->handle = handle;
5983 raid_device->wwid = wwid;
5984 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305985 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06005986 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5987 raid_device->id, 0);
5988 if (rc)
5989 _scsih_raid_device_remove(ioc, raid_device);
5990 } else
5991 _scsih_determine_boot_device(ioc, raid_device, 1);
5992}
5993
5994/**
5995 * _scsih_sas_volume_delete - delete volume
5996 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305997 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005998 * Context: user.
5999 *
6000 * Return nothing.
6001 */
6002static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306003_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006004{
6005 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006006 unsigned long flags;
6007 struct MPT2SAS_TARGET *sas_target_priv_data;
6008
Eric Moore635374e2009-03-09 01:21:12 -06006009 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6010 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6011 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6012 if (!raid_device)
6013 return;
6014 if (raid_device->starget) {
6015 sas_target_priv_data = raid_device->starget->hostdata;
6016 sas_target_priv_data->deleted = 1;
6017 scsi_remove_target(&raid_device->starget->dev);
6018 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306019 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6020 "(0x%016llx)\n", ioc->name, raid_device->handle,
6021 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006022 _scsih_raid_device_remove(ioc, raid_device);
6023}
6024
6025/**
6026 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6027 * @ioc: per adapter object
6028 * @element: IR config element data
6029 * Context: user.
6030 *
6031 * Return nothing.
6032 */
6033static void
6034_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6035 Mpi2EventIrConfigElement_t *element)
6036{
6037 struct _sas_device *sas_device;
6038 unsigned long flags;
6039 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6040
6041 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6042 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6043 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6044 if (!sas_device)
6045 return;
6046
6047 /* exposing raid component */
6048 sas_device->volume_handle = 0;
6049 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306050 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006051 _scsih_reprobe_target(sas_device->starget, 0);
6052}
6053
6054/**
6055 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6056 * @ioc: per adapter object
6057 * @element: IR config element data
6058 * Context: user.
6059 *
6060 * Return nothing.
6061 */
6062static void
6063_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6064 Mpi2EventIrConfigElement_t *element)
6065{
6066 struct _sas_device *sas_device;
6067 unsigned long flags;
6068 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6069
6070 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6071 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6072 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6073 if (!sas_device)
6074 return;
6075
6076 /* hiding raid component */
6077 mpt2sas_config_get_volume_handle(ioc, handle,
6078 &sas_device->volume_handle);
6079 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
6080 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306081 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006082 _scsih_reprobe_target(sas_device->starget, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306083
Eric Moore635374e2009-03-09 01:21:12 -06006084}
6085
6086/**
6087 * _scsih_sas_pd_delete - delete pd component
6088 * @ioc: per adapter object
6089 * @element: IR config element data
6090 * Context: user.
6091 *
6092 * Return nothing.
6093 */
6094static void
6095_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6096 Mpi2EventIrConfigElement_t *element)
6097{
6098 struct _sas_device *sas_device;
6099 unsigned long flags;
6100 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6101
6102 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6103 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6104 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6105 if (!sas_device)
6106 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306107 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006108}
6109
6110/**
6111 * _scsih_sas_pd_add - remove pd component
6112 * @ioc: per adapter object
6113 * @element: IR config element data
6114 * Context: user.
6115 *
6116 * Return nothing.
6117 */
6118static void
6119_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6120 Mpi2EventIrConfigElement_t *element)
6121{
6122 struct _sas_device *sas_device;
6123 unsigned long flags;
6124 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306125 Mpi2ConfigReply_t mpi_reply;
6126 Mpi2SasDevicePage0_t sas_device_pg0;
6127 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306128 u64 sas_address;
6129 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006130
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306131 set_bit(handle, ioc->pd_handles);
6132
Eric Moore635374e2009-03-09 01:21:12 -06006133 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6134 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6135 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306136 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306137 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306138
6139 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6140 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6141 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6142 ioc->name, __FILE__, __LINE__, __func__);
6143 return;
6144 }
6145
6146 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6147 MPI2_IOCSTATUS_MASK;
6148 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6149 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6150 ioc->name, __FILE__, __LINE__, __func__);
6151 return;
6152 }
6153
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306154 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6155 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6156 mpt2sas_transport_update_links(ioc, sas_address, handle,
6157 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306158
6159 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006160}
6161
6162#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6163/**
6164 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6165 * @ioc: per adapter object
6166 * @event_data: event data payload
6167 * Context: user.
6168 *
6169 * Return nothing.
6170 */
6171static void
6172_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6173 Mpi2EventDataIrConfigChangeList_t *event_data)
6174{
6175 Mpi2EventIrConfigElement_t *element;
6176 u8 element_type;
6177 int i;
6178 char *reason_str = NULL, *element_str = NULL;
6179
6180 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6181
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306182 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006183 ioc->name, (le32_to_cpu(event_data->Flags) &
6184 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6185 "foreign" : "native", event_data->NumElements);
6186 for (i = 0; i < event_data->NumElements; i++, element++) {
6187 switch (element->ReasonCode) {
6188 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6189 reason_str = "add";
6190 break;
6191 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6192 reason_str = "remove";
6193 break;
6194 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6195 reason_str = "no change";
6196 break;
6197 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6198 reason_str = "hide";
6199 break;
6200 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6201 reason_str = "unhide";
6202 break;
6203 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6204 reason_str = "volume_created";
6205 break;
6206 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6207 reason_str = "volume_deleted";
6208 break;
6209 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6210 reason_str = "pd_created";
6211 break;
6212 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6213 reason_str = "pd_deleted";
6214 break;
6215 default:
6216 reason_str = "unknown reason";
6217 break;
6218 }
6219 element_type = le16_to_cpu(element->ElementFlags) &
6220 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6221 switch (element_type) {
6222 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6223 element_str = "volume";
6224 break;
6225 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6226 element_str = "phys disk";
6227 break;
6228 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6229 element_str = "hot spare";
6230 break;
6231 default:
6232 element_str = "unknown element";
6233 break;
6234 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306235 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006236 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6237 reason_str, le16_to_cpu(element->VolDevHandle),
6238 le16_to_cpu(element->PhysDiskDevHandle),
6239 element->PhysDiskNum);
6240 }
6241}
6242#endif
6243
6244/**
6245 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6246 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306247 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006248 * Context: user.
6249 *
6250 * Return nothing.
6251 */
6252static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306253_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6254 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006255{
6256 Mpi2EventIrConfigElement_t *element;
6257 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306258 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306259 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006260
6261#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306262 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6263 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006264 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6265
6266#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306267
6268 if (ioc->shost_recovery)
6269 return;
6270
Kashyap, Desai62727a72009-08-07 19:35:18 +05306271 foreign_config = (le32_to_cpu(event_data->Flags) &
6272 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006273
6274 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6275 for (i = 0; i < event_data->NumElements; i++, element++) {
6276
6277 switch (element->ReasonCode) {
6278 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6279 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306280 if (!foreign_config)
6281 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006282 break;
6283 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6284 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306285 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306286 _scsih_sas_volume_delete(ioc,
6287 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006288 break;
6289 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306290 if (!ioc->is_warpdrive)
6291 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006292 break;
6293 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306294 if (!ioc->is_warpdrive)
6295 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006296 break;
6297 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306298 if (!ioc->is_warpdrive)
6299 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006300 break;
6301 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306302 if (!ioc->is_warpdrive)
6303 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006304 break;
6305 }
6306 }
6307}
6308
6309/**
6310 * _scsih_sas_ir_volume_event - IR volume event
6311 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306312 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006313 * Context: user.
6314 *
6315 * Return nothing.
6316 */
6317static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306318_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6319 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006320{
6321 u64 wwid;
6322 unsigned long flags;
6323 struct _raid_device *raid_device;
6324 u16 handle;
6325 u32 state;
6326 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306327 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006328
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306329 if (ioc->shost_recovery)
6330 return;
6331
Eric Moore635374e2009-03-09 01:21:12 -06006332 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6333 return;
6334
6335 handle = le16_to_cpu(event_data->VolDevHandle);
6336 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306337 if (!ioc->hide_ir_msg)
6338 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6339 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6340 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006341
Eric Moore635374e2009-03-09 01:21:12 -06006342 switch (state) {
6343 case MPI2_RAID_VOL_STATE_MISSING:
6344 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306345 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006346 break;
6347
6348 case MPI2_RAID_VOL_STATE_ONLINE:
6349 case MPI2_RAID_VOL_STATE_DEGRADED:
6350 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306351
6352 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6353 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6354 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6355
Eric Moore635374e2009-03-09 01:21:12 -06006356 if (raid_device)
6357 break;
6358
6359 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6360 if (!wwid) {
6361 printk(MPT2SAS_ERR_FMT
6362 "failure at %s:%d/%s()!\n", ioc->name,
6363 __FILE__, __LINE__, __func__);
6364 break;
6365 }
6366
6367 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6368 if (!raid_device) {
6369 printk(MPT2SAS_ERR_FMT
6370 "failure at %s:%d/%s()!\n", ioc->name,
6371 __FILE__, __LINE__, __func__);
6372 break;
6373 }
6374
6375 raid_device->id = ioc->sas_id++;
6376 raid_device->channel = RAID_CHANNEL;
6377 raid_device->handle = handle;
6378 raid_device->wwid = wwid;
6379 _scsih_raid_device_add(ioc, raid_device);
6380 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6381 raid_device->id, 0);
6382 if (rc)
6383 _scsih_raid_device_remove(ioc, raid_device);
6384 break;
6385
6386 case MPI2_RAID_VOL_STATE_INITIALIZING:
6387 default:
6388 break;
6389 }
6390}
6391
6392/**
6393 * _scsih_sas_ir_physical_disk_event - PD event
6394 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306395 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006396 * Context: user.
6397 *
6398 * Return nothing.
6399 */
6400static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306401_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6402 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006403{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306404 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006405 u32 state;
6406 struct _sas_device *sas_device;
6407 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306408 Mpi2ConfigReply_t mpi_reply;
6409 Mpi2SasDevicePage0_t sas_device_pg0;
6410 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306411 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306412 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006413
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306414 if (ioc->shost_recovery)
6415 return;
6416
Eric Moore635374e2009-03-09 01:21:12 -06006417 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6418 return;
6419
6420 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6421 state = le32_to_cpu(event_data->NewValue);
6422
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306423 if (!ioc->hide_ir_msg)
6424 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6425 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6426 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006427
Eric Moore635374e2009-03-09 01:21:12 -06006428 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006429 case MPI2_RAID_PD_STATE_ONLINE:
6430 case MPI2_RAID_PD_STATE_DEGRADED:
6431 case MPI2_RAID_PD_STATE_REBUILDING:
6432 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306433 case MPI2_RAID_PD_STATE_HOT_SPARE:
6434
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306435 if (!ioc->is_warpdrive)
6436 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306437
6438 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6439 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6440 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6441
6442 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306443 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306444
6445 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6446 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6447 handle))) {
6448 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6449 ioc->name, __FILE__, __LINE__, __func__);
6450 return;
6451 }
6452
6453 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6454 MPI2_IOCSTATUS_MASK;
6455 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6456 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6457 ioc->name, __FILE__, __LINE__, __func__);
6458 return;
6459 }
6460
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306461 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6462 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6463 mpt2sas_transport_update_links(ioc, sas_address, handle,
6464 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306465
6466 _scsih_add_device(ioc, handle, 0, 1);
6467
Eric Moore635374e2009-03-09 01:21:12 -06006468 break;
6469
Kashyap, Desai62727a72009-08-07 19:35:18 +05306470 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006471 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6472 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006473 default:
6474 break;
6475 }
6476}
6477
6478#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6479/**
6480 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6481 * @ioc: per adapter object
6482 * @event_data: event data payload
6483 * Context: user.
6484 *
6485 * Return nothing.
6486 */
6487static void
6488_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6489 Mpi2EventDataIrOperationStatus_t *event_data)
6490{
6491 char *reason_str = NULL;
6492
6493 switch (event_data->RAIDOperation) {
6494 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6495 reason_str = "resync";
6496 break;
6497 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6498 reason_str = "online capacity expansion";
6499 break;
6500 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6501 reason_str = "consistency check";
6502 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306503 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6504 reason_str = "background init";
6505 break;
6506 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6507 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006508 break;
6509 }
6510
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306511 if (!reason_str)
6512 return;
6513
Eric Moore635374e2009-03-09 01:21:12 -06006514 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6515 "\thandle(0x%04x), percent complete(%d)\n",
6516 ioc->name, reason_str,
6517 le16_to_cpu(event_data->VolDevHandle),
6518 event_data->PercentComplete);
6519}
6520#endif
6521
6522/**
6523 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6524 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306525 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006526 * Context: user.
6527 *
6528 * Return nothing.
6529 */
6530static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306531_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6532 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006533{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306534 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6535 static struct _raid_device *raid_device;
6536 unsigned long flags;
6537 u16 handle;
6538
Eric Moore635374e2009-03-09 01:21:12 -06006539#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306540 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6541 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306542 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306543 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006544#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306545
6546 /* code added for raid transport support */
6547 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6548
6549 handle = le16_to_cpu(event_data->VolDevHandle);
6550
6551 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6552 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6553 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6554
6555 if (!raid_device)
6556 return;
6557
6558 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6559 raid_device->percent_complete =
6560 event_data->PercentComplete;
6561 }
Eric Moore635374e2009-03-09 01:21:12 -06006562}
6563
6564/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306565 * _scsih_prep_device_scan - initialize parameters prior to device scan
6566 * @ioc: per adapter object
6567 *
6568 * Set the deleted flag prior to device scan. If the device is found during
6569 * the scan, then we clear the deleted flag.
6570 */
6571static void
6572_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6573{
6574 struct MPT2SAS_DEVICE *sas_device_priv_data;
6575 struct scsi_device *sdev;
6576
6577 shost_for_each_device(sdev, ioc->shost) {
6578 sas_device_priv_data = sdev->hostdata;
6579 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6580 sas_device_priv_data->sas_target->deleted = 1;
6581 }
6582}
6583
6584/**
Eric Moore635374e2009-03-09 01:21:12 -06006585 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6586 * @ioc: per adapter object
6587 * @sas_address: sas address
6588 * @slot: enclosure slot id
6589 * @handle: device handle
6590 *
6591 * After host reset, find out whether devices are still responding.
6592 * Used in _scsi_remove_unresponsive_sas_devices.
6593 *
6594 * Return nothing.
6595 */
6596static void
6597_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6598 u16 slot, u16 handle)
6599{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306600 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006601 struct scsi_target *starget;
6602 struct _sas_device *sas_device;
6603 unsigned long flags;
6604
6605 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6606 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6607 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306608 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006609 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306610 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306611 if (starget && starget->hostdata) {
6612 sas_target_priv_data = starget->hostdata;
6613 sas_target_priv_data->tm_busy = 0;
6614 sas_target_priv_data->deleted = 0;
6615 } else
6616 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306617 if (starget)
6618 starget_printk(KERN_INFO, starget,
6619 "handle(0x%04x), sas_addr(0x%016llx), "
6620 "enclosure logical id(0x%016llx), "
6621 "slot(%d)\n", handle,
6622 (unsigned long long)sas_device->sas_address,
6623 (unsigned long long)
6624 sas_device->enclosure_logical_id,
6625 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006626 if (sas_device->handle == handle)
6627 goto out;
6628 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6629 sas_device->handle);
6630 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306631 if (sas_target_priv_data)
6632 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006633 goto out;
6634 }
6635 }
6636 out:
6637 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6638}
6639
6640/**
6641 * _scsih_search_responding_sas_devices -
6642 * @ioc: per adapter object
6643 *
6644 * After host reset, find out whether devices are still responding.
6645 * If not remove.
6646 *
6647 * Return nothing.
6648 */
6649static void
6650_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6651{
6652 Mpi2SasDevicePage0_t sas_device_pg0;
6653 Mpi2ConfigReply_t mpi_reply;
6654 u16 ioc_status;
6655 __le64 sas_address;
6656 u16 handle;
6657 u32 device_info;
6658 u16 slot;
6659
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306660 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006661
6662 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306663 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006664
6665 handle = 0xFFFF;
6666 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6667 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6668 handle))) {
6669 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6670 MPI2_IOCSTATUS_MASK;
6671 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6672 break;
6673 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6674 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6675 if (!(_scsih_is_end_device(device_info)))
6676 continue;
6677 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6678 slot = le16_to_cpu(sas_device_pg0.Slot);
6679 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6680 handle);
6681 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306682out:
6683 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6684 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006685}
6686
6687/**
6688 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6689 * @ioc: per adapter object
6690 * @wwid: world wide identifier for raid volume
6691 * @handle: device handle
6692 *
6693 * After host reset, find out whether devices are still responding.
6694 * Used in _scsi_remove_unresponsive_raid_devices.
6695 *
6696 * Return nothing.
6697 */
6698static void
6699_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6700 u16 handle)
6701{
6702 struct MPT2SAS_TARGET *sas_target_priv_data;
6703 struct scsi_target *starget;
6704 struct _raid_device *raid_device;
6705 unsigned long flags;
6706
6707 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6708 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6709 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306710 starget = raid_device->starget;
6711 if (starget && starget->hostdata) {
6712 sas_target_priv_data = starget->hostdata;
6713 sas_target_priv_data->deleted = 0;
6714 } else
6715 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006716 raid_device->responding = 1;
6717 starget_printk(KERN_INFO, raid_device->starget,
6718 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6719 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306720 /*
6721 * WARPDRIVE: The handles of the PDs might have changed
6722 * across the host reset so re-initialize the
6723 * required data for Direct IO
6724 */
6725 _scsih_init_warpdrive_properties(ioc, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006726 if (raid_device->handle == handle)
6727 goto out;
6728 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6729 raid_device->handle);
6730 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306731 if (sas_target_priv_data)
6732 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006733 goto out;
6734 }
6735 }
6736 out:
6737 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6738}
6739
6740/**
6741 * _scsih_search_responding_raid_devices -
6742 * @ioc: per adapter object
6743 *
6744 * After host reset, find out whether devices are still responding.
6745 * If not remove.
6746 *
6747 * Return nothing.
6748 */
6749static void
6750_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6751{
6752 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306753 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306754 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006755 Mpi2ConfigReply_t mpi_reply;
6756 u16 ioc_status;
6757 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306758 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006759
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306760 if (!ioc->ir_firmware)
6761 return;
6762
6763 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6764 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006765
6766 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306767 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006768
6769 handle = 0xFFFF;
6770 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6771 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6772 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6773 MPI2_IOCSTATUS_MASK;
6774 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6775 break;
6776 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306777
6778 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6779 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6780 sizeof(Mpi2RaidVolPage0_t)))
6781 continue;
6782
6783 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6784 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6785 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6786 _scsih_mark_responding_raid_device(ioc,
6787 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006788 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306789
6790 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306791 if (!ioc->is_warpdrive) {
6792 phys_disk_num = 0xFF;
6793 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6794 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6795 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6796 phys_disk_num))) {
6797 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6798 MPI2_IOCSTATUS_MASK;
6799 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6800 break;
6801 phys_disk_num = pd_pg0.PhysDiskNum;
6802 handle = le16_to_cpu(pd_pg0.DevHandle);
6803 set_bit(handle, ioc->pd_handles);
6804 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306805 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306806out:
6807 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6808 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006809}
6810
6811/**
6812 * _scsih_mark_responding_expander - mark a expander as responding
6813 * @ioc: per adapter object
6814 * @sas_address: sas address
6815 * @handle:
6816 *
6817 * After host reset, find out whether devices are still responding.
6818 * Used in _scsi_remove_unresponsive_expanders.
6819 *
6820 * Return nothing.
6821 */
6822static void
6823_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6824 u16 handle)
6825{
6826 struct _sas_node *sas_expander;
6827 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306828 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006829
6830 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6831 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306832 if (sas_expander->sas_address != sas_address)
6833 continue;
6834 sas_expander->responding = 1;
6835 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006836 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306837 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6838 " from(0x%04x) to (0x%04x)!!!\n",
6839 (unsigned long long)sas_expander->sas_address,
6840 sas_expander->handle, handle);
6841 sas_expander->handle = handle;
6842 for (i = 0 ; i < sas_expander->num_phys ; i++)
6843 sas_expander->phy[i].handle = handle;
6844 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006845 }
6846 out:
6847 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6848}
6849
6850/**
6851 * _scsih_search_responding_expanders -
6852 * @ioc: per adapter object
6853 *
6854 * After host reset, find out whether devices are still responding.
6855 * If not remove.
6856 *
6857 * Return nothing.
6858 */
6859static void
6860_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6861{
6862 Mpi2ExpanderPage0_t expander_pg0;
6863 Mpi2ConfigReply_t mpi_reply;
6864 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306865 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006866 u16 handle;
6867
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306868 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006869
6870 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306871 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006872
6873 handle = 0xFFFF;
6874 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6875 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6876
6877 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6878 MPI2_IOCSTATUS_MASK;
6879 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6880 break;
6881
6882 handle = le16_to_cpu(expander_pg0.DevHandle);
6883 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6884 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6885 "sas_addr(0x%016llx)\n", handle,
6886 (unsigned long long)sas_address);
6887 _scsih_mark_responding_expander(ioc, sas_address, handle);
6888 }
6889
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306890 out:
6891 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006892}
6893
6894/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306895 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006896 * @ioc: per adapter object
6897 *
6898 * Return nothing.
6899 */
6900static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306901_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006902{
6903 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306904 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006905 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006906
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306907 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
6908 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006909
6910 list_for_each_entry_safe(sas_device, sas_device_next,
6911 &ioc->sas_device_list, list) {
6912 if (sas_device->responding) {
6913 sas_device->responding = 0;
6914 continue;
6915 }
6916 if (sas_device->starget)
6917 starget_printk(KERN_INFO, sas_device->starget,
6918 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6919 "enclosure logical id(0x%016llx), slot(%d)\n",
6920 sas_device->handle,
6921 (unsigned long long)sas_device->sas_address,
6922 (unsigned long long)
6923 sas_device->enclosure_logical_id,
6924 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306925 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006926 }
6927
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306928 if (!ioc->ir_firmware)
6929 goto retry_expander_search;
6930
Eric Moore635374e2009-03-09 01:21:12 -06006931 list_for_each_entry_safe(raid_device, raid_device_next,
6932 &ioc->raid_device_list, list) {
6933 if (raid_device->responding) {
6934 raid_device->responding = 0;
6935 continue;
6936 }
6937 if (raid_device->starget) {
6938 starget_printk(KERN_INFO, raid_device->starget,
6939 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6940 raid_device->handle,
6941 (unsigned long long)raid_device->wwid);
6942 scsi_remove_target(&raid_device->starget->dev);
6943 }
6944 _scsih_raid_device_remove(ioc, raid_device);
6945 }
6946
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306947 retry_expander_search:
6948 sas_expander = NULL;
6949 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006950 if (sas_expander->responding) {
6951 sas_expander->responding = 0;
6952 continue;
6953 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306954 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306955 goto retry_expander_search;
6956 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306957 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
6958 ioc->name);
6959 /* unblock devices */
6960 _scsih_ublock_io_all_device(ioc);
6961}
6962
6963static void
6964_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
6965 struct _sas_node *sas_expander, u16 handle)
6966{
6967 Mpi2ExpanderPage1_t expander_pg1;
6968 Mpi2ConfigReply_t mpi_reply;
6969 int i;
6970
6971 for (i = 0 ; i < sas_expander->num_phys ; i++) {
6972 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
6973 &expander_pg1, i, handle))) {
6974 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6975 ioc->name, __FILE__, __LINE__, __func__);
6976 return;
6977 }
6978
6979 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
6980 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
6981 expander_pg1.NegotiatedLinkRate >> 4);
6982 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306983}
6984
6985/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306986 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306987 * @ioc: per adapter object
6988 *
6989 * Return nothing.
6990 */
6991static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306992_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306993{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306994 Mpi2ExpanderPage0_t expander_pg0;
6995 Mpi2SasDevicePage0_t sas_device_pg0;
6996 Mpi2RaidVolPage1_t volume_pg1;
6997 Mpi2RaidVolPage0_t volume_pg0;
6998 Mpi2RaidPhysDiskPage0_t pd_pg0;
6999 Mpi2EventIrConfigElement_t element;
7000 Mpi2ConfigReply_t mpi_reply;
7001 u8 phys_disk_num;
7002 u16 ioc_status;
7003 u16 handle, parent_handle;
7004 u64 sas_address;
7005 struct _sas_device *sas_device;
7006 struct _sas_node *expander_device;
7007 static struct _raid_device *raid_device;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307008
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307009 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307010
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307011 _scsih_sas_host_refresh(ioc);
7012
7013 /* expanders */
7014 handle = 0xFFFF;
7015 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7016 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7017 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7018 MPI2_IOCSTATUS_MASK;
7019 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7020 break;
7021 handle = le16_to_cpu(expander_pg0.DevHandle);
7022 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7023 ioc, le64_to_cpu(expander_pg0.SASAddress));
7024 if (expander_device)
7025 _scsih_refresh_expander_links(ioc, expander_device,
7026 handle);
7027 else
7028 _scsih_expander_add(ioc, handle);
7029 }
7030
7031 if (!ioc->ir_firmware)
7032 goto skip_to_sas;
7033
7034 /* phys disk */
7035 phys_disk_num = 0xFF;
7036 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7037 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7038 phys_disk_num))) {
7039 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7040 MPI2_IOCSTATUS_MASK;
7041 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7042 break;
7043 phys_disk_num = pd_pg0.PhysDiskNum;
7044 handle = le16_to_cpu(pd_pg0.DevHandle);
7045 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7046 if (sas_device)
7047 continue;
7048 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7049 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7050 handle) != 0)
7051 continue;
7052 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7053 if (!_scsih_get_sas_address(ioc, parent_handle,
7054 &sas_address)) {
7055 mpt2sas_transport_update_links(ioc, sas_address,
7056 handle, sas_device_pg0.PhyNum,
7057 MPI2_SAS_NEG_LINK_RATE_1_5);
7058 set_bit(handle, ioc->pd_handles);
7059 _scsih_add_device(ioc, handle, 0, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307060 }
7061 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307062
7063 /* volumes */
7064 handle = 0xFFFF;
7065 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7066 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7067 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7068 MPI2_IOCSTATUS_MASK;
7069 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7070 break;
7071 handle = le16_to_cpu(volume_pg1.DevHandle);
7072 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7073 le64_to_cpu(volume_pg1.WWID));
7074 if (raid_device)
7075 continue;
7076 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7077 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7078 sizeof(Mpi2RaidVolPage0_t)))
7079 continue;
7080 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7081 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7082 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7083 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7084 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7085 element.VolDevHandle = volume_pg1.DevHandle;
7086 _scsih_sas_volume_add(ioc, &element);
7087 }
7088 }
7089
7090 skip_to_sas:
7091
7092 /* sas devices */
7093 handle = 0xFFFF;
7094 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7095 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7096 handle))) {
7097 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7098 MPI2_IOCSTATUS_MASK;
7099 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7100 break;
7101 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7102 if (!(_scsih_is_end_device(
7103 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7104 continue;
7105 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7106 le64_to_cpu(sas_device_pg0.SASAddress));
7107 if (sas_device)
7108 continue;
7109 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7110 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7111 mpt2sas_transport_update_links(ioc, sas_address, handle,
7112 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7113 _scsih_add_device(ioc, handle, 0, 0);
7114 }
7115 }
7116
7117 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307118}
7119
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307120
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307121/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307122 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7123 * @ioc: per adapter object
7124 * @reset_phase: phase
7125 *
7126 * The handler for doing any required cleanup or initialization.
7127 *
7128 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7129 * MPT2_IOC_DONE_RESET
7130 *
7131 * Return nothing.
7132 */
7133void
7134mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7135{
7136 switch (reset_phase) {
7137 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307138 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307139 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307140 break;
7141 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307142 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307143 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307144 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7145 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7146 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7147 complete(&ioc->scsih_cmds.done);
7148 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307149 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7150 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7151 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7152 complete(&ioc->tm_cmds.done);
7153 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307154 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307155 _scsih_flush_running_cmds(ioc);
7156 break;
7157 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307158 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307159 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307160 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307161 _scsih_prep_device_scan(ioc);
7162 _scsih_search_responding_sas_devices(ioc);
7163 _scsih_search_responding_raid_devices(ioc);
7164 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307165 if (!ioc->is_driver_loading) {
7166 _scsih_prep_device_scan(ioc);
7167 _scsih_search_responding_sas_devices(ioc);
7168 _scsih_search_responding_raid_devices(ioc);
7169 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307170 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307171 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307172 break;
Eric Moore635374e2009-03-09 01:21:12 -06007173 }
7174}
7175
7176/**
7177 * _firmware_event_work - delayed task for processing firmware events
7178 * @ioc: per adapter object
7179 * @work: equal to the fw_event_work object
7180 * Context: user.
7181 *
7182 * Return nothing.
7183 */
7184static void
7185_firmware_event_work(struct work_struct *work)
7186{
7187 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307188 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007189 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7190
Eric Moore635374e2009-03-09 01:21:12 -06007191 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007192 if (ioc->remove_host || fw_event->cancel_pending_work ||
7193 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007194 _scsih_fw_event_free(ioc, fw_event);
7195 return;
7196 }
Eric Moore635374e2009-03-09 01:21:12 -06007197
Eric Moore635374e2009-03-09 01:21:12 -06007198 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307199 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
7200 while (scsi_host_in_recovery(ioc->shost))
7201 ssleep(1);
7202 _scsih_remove_unresponding_sas_devices(ioc);
7203 _scsih_scan_for_devices_after_reset(ioc);
7204 break;
7205 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307206 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307207
7208
7209
7210 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7211 "from worker thread\n", ioc->name));
7212 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307213 case MPT2SAS_TURN_ON_FAULT_LED:
7214 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7215 break;
Eric Moore635374e2009-03-09 01:21:12 -06007216 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307217 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007218 break;
7219 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307220 _scsih_sas_device_status_change_event(ioc,
7221 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007222 break;
7223 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307224 _scsih_sas_discovery_event(ioc,
7225 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007226 break;
7227 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307228 _scsih_sas_broadcast_primative_event(ioc,
7229 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007230 break;
7231 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7232 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307233 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007234 break;
7235 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307236 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007237 break;
7238 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307239 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007240 break;
7241 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307242 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007243 break;
7244 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307245 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007246 break;
Eric Moore635374e2009-03-09 01:21:12 -06007247 }
7248 _scsih_fw_event_free(ioc, fw_event);
7249}
7250
7251/**
7252 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7253 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307254 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007255 * @reply: reply message frame(lower 32bit addr)
7256 * Context: interrupt.
7257 *
7258 * This function merely adds a new work task into ioc->firmware_event_thread.
7259 * The tasks are worked from _firmware_event_work in user context.
7260 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307261 * Return 1 meaning mf should be freed from _base_interrupt
7262 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06007263 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307264u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307265mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7266 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007267{
7268 struct fw_event_work *fw_event;
7269 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007270 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307271 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007272
7273 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007274 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307275 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007276
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307277 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06007278 event = le16_to_cpu(mpi_reply->Event);
7279
7280 switch (event) {
7281 /* handle these */
7282 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7283 {
7284 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7285 (Mpi2EventDataSasBroadcastPrimitive_t *)
7286 mpi_reply->EventData;
7287
7288 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307289 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307290 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307291
7292 if (ioc->broadcast_aen_busy) {
7293 ioc->broadcast_aen_pending++;
7294 return 1;
7295 } else
7296 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007297 break;
7298 }
7299
7300 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7301 _scsih_check_topo_delete_events(ioc,
7302 (Mpi2EventDataSasTopologyChangeList_t *)
7303 mpi_reply->EventData);
7304 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307305 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7306 _scsih_check_ir_config_unhide_events(ioc,
7307 (Mpi2EventDataIrConfigChangeList_t *)
7308 mpi_reply->EventData);
7309 break;
7310 case MPI2_EVENT_IR_VOLUME:
7311 _scsih_check_volume_delete_events(ioc,
7312 (Mpi2EventDataIrVolume_t *)
7313 mpi_reply->EventData);
7314 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307315 case MPI2_EVENT_LOG_ENTRY_ADDED:
7316 {
7317 Mpi2EventDataLogEntryAdded_t *log_entry;
7318 u32 *log_code;
7319
7320 if (!ioc->is_warpdrive)
7321 break;
7322
7323 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7324 mpi_reply->EventData;
7325 log_code = (u32 *)log_entry->LogData;
7326
7327 if (le16_to_cpu(log_entry->LogEntryQualifier)
7328 != MPT2_WARPDRIVE_LOGENTRY)
7329 break;
7330
7331 switch (le32_to_cpu(*log_code)) {
7332 case MPT2_WARPDRIVE_LC_SSDT:
7333 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7334 "IO Throttling has occurred in the WarpDrive "
7335 "subsystem. Check WarpDrive documentation for "
7336 "additional details.\n", ioc->name);
7337 break;
7338 case MPT2_WARPDRIVE_LC_SSDLW:
7339 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7340 "Program/Erase Cycles for the WarpDrive subsystem "
7341 "in degraded range. Check WarpDrive documentation "
7342 "for additional details.\n", ioc->name);
7343 break;
7344 case MPT2_WARPDRIVE_LC_SSDLF:
7345 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7346 "There are no Program/Erase Cycles for the "
7347 "WarpDrive subsystem. The storage device will be "
7348 "in read-only mode. Check WarpDrive documentation "
7349 "for additional details.\n", ioc->name);
7350 break;
7351 case MPT2_WARPDRIVE_LC_BRMF:
7352 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7353 "The Backup Rail Monitor has failed on the "
7354 "WarpDrive subsystem. Check WarpDrive "
7355 "documentation for additional details.\n",
7356 ioc->name);
7357 break;
7358 }
7359
7360 break;
7361 }
Eric Moore635374e2009-03-09 01:21:12 -06007362 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7363 case MPI2_EVENT_IR_OPERATION_STATUS:
7364 case MPI2_EVENT_SAS_DISCOVERY:
7365 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007366 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007367 break;
7368
7369 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307370 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007371 }
7372
7373 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7374 if (!fw_event) {
7375 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7376 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307377 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007378 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307379 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7380 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007381 if (!fw_event->event_data) {
7382 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7383 ioc->name, __FILE__, __LINE__, __func__);
7384 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307385 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007386 }
7387
7388 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307389 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007390 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307391 fw_event->VF_ID = mpi_reply->VF_ID;
7392 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007393 fw_event->event = event;
7394 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307395 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007396}
7397
7398/* shost template */
7399static struct scsi_host_template scsih_driver_template = {
7400 .module = THIS_MODULE,
7401 .name = "Fusion MPT SAS Host",
7402 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007403 .queuecommand = _scsih_qcmd,
7404 .target_alloc = _scsih_target_alloc,
7405 .slave_alloc = _scsih_slave_alloc,
7406 .slave_configure = _scsih_slave_configure,
7407 .target_destroy = _scsih_target_destroy,
7408 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307409 .scan_finished = _scsih_scan_finished,
7410 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007411 .change_queue_depth = _scsih_change_queue_depth,
7412 .change_queue_type = _scsih_change_queue_type,
7413 .eh_abort_handler = _scsih_abort,
7414 .eh_device_reset_handler = _scsih_dev_reset,
7415 .eh_target_reset_handler = _scsih_target_reset,
7416 .eh_host_reset_handler = _scsih_host_reset,
7417 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007418 .can_queue = 1,
7419 .this_id = -1,
7420 .sg_tablesize = MPT2SAS_SG_DEPTH,
7421 .max_sectors = 8192,
7422 .cmd_per_lun = 7,
7423 .use_clustering = ENABLE_CLUSTERING,
7424 .shost_attrs = mpt2sas_host_attrs,
7425 .sdev_attrs = mpt2sas_dev_attrs,
7426};
7427
7428/**
7429 * _scsih_expander_node_remove - removing expander device from list.
7430 * @ioc: per adapter object
7431 * @sas_expander: the sas_device object
7432 * Context: Calling function should acquire ioc->sas_node_lock.
7433 *
7434 * Removing object and freeing associated memory from the
7435 * ioc->sas_expander_list.
7436 *
7437 * Return nothing.
7438 */
7439static void
7440_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7441 struct _sas_node *sas_expander)
7442{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307443 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007444
7445 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307446 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007447 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307448 if (ioc->shost_recovery)
7449 return;
Eric Moore635374e2009-03-09 01:21:12 -06007450 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307451 SAS_END_DEVICE)
7452 mpt2sas_device_remove(ioc,
7453 mpt2sas_port->remote_identify.sas_address);
7454 else if (mpt2sas_port->remote_identify.device_type ==
7455 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007456 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307457 SAS_FANOUT_EXPANDER_DEVICE)
7458 mpt2sas_expander_remove(ioc,
7459 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007460 }
7461
7462 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307463 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007464
7465 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7466 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7467 sas_expander->handle, (unsigned long long)
7468 sas_expander->sas_address);
7469
Eric Moore635374e2009-03-09 01:21:12 -06007470 kfree(sas_expander->phy);
7471 kfree(sas_expander);
7472}
7473
7474/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307475 * _scsih_ir_shutdown - IR shutdown notification
7476 * @ioc: per adapter object
7477 *
7478 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7479 * the host system is shutting down.
7480 *
7481 * Return nothing.
7482 */
7483static void
7484_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7485{
7486 Mpi2RaidActionRequest_t *mpi_request;
7487 Mpi2RaidActionReply_t *mpi_reply;
7488 u16 smid;
7489
7490 /* is IR firmware build loaded ? */
7491 if (!ioc->ir_firmware)
7492 return;
7493
7494 /* are there any volumes ? */
7495 if (list_empty(&ioc->raid_device_list))
7496 return;
7497
7498 mutex_lock(&ioc->scsih_cmds.mutex);
7499
7500 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7501 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7502 ioc->name, __func__);
7503 goto out;
7504 }
7505 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7506
7507 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7508 if (!smid) {
7509 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7510 ioc->name, __func__);
7511 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7512 goto out;
7513 }
7514
7515 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7516 ioc->scsih_cmds.smid = smid;
7517 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7518
7519 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7520 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7521
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307522 if (!ioc->hide_ir_msg)
7523 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307524 init_completion(&ioc->scsih_cmds.done);
7525 mpt2sas_base_put_smid_default(ioc, smid);
7526 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7527
7528 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7529 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7530 ioc->name, __func__);
7531 goto out;
7532 }
7533
7534 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7535 mpi_reply = ioc->scsih_cmds.reply;
7536
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307537 if (!ioc->hide_ir_msg)
7538 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7539 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7540 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7541 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307542 }
7543
7544 out:
7545 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7546 mutex_unlock(&ioc->scsih_cmds.mutex);
7547}
7548
7549/**
7550 * _scsih_shutdown - routine call during system shutdown
7551 * @pdev: PCI device struct
7552 *
7553 * Return nothing.
7554 */
7555static void
7556_scsih_shutdown(struct pci_dev *pdev)
7557{
7558 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7559 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307560 struct workqueue_struct *wq;
7561 unsigned long flags;
7562
7563 ioc->remove_host = 1;
7564 _scsih_fw_event_cleanup_queue(ioc);
7565
7566 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7567 wq = ioc->firmware_event_thread;
7568 ioc->firmware_event_thread = NULL;
7569 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7570 if (wq)
7571 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307572
7573 _scsih_ir_shutdown(ioc);
7574 mpt2sas_base_detach(ioc);
7575}
7576
7577/**
Eric Moored5d135b2009-05-18 13:02:08 -06007578 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007579 * @pdev: PCI device struct
7580 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307581 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007582 * Return nothing.
7583 */
7584static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007585_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007586{
7587 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7588 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307589 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307590 struct _raid_device *raid_device, *next;
7591 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007592 struct workqueue_struct *wq;
7593 unsigned long flags;
7594
7595 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307596 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007597
7598 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7599 wq = ioc->firmware_event_thread;
7600 ioc->firmware_event_thread = NULL;
7601 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7602 if (wq)
7603 destroy_workqueue(wq);
7604
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307605 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307606 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307607 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7608 list) {
7609 if (raid_device->starget) {
7610 sas_target_priv_data =
7611 raid_device->starget->hostdata;
7612 sas_target_priv_data->deleted = 1;
7613 scsi_remove_target(&raid_device->starget->dev);
7614 }
7615 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7616 "(0x%016llx)\n", ioc->name, raid_device->handle,
7617 (unsigned long long) raid_device->wwid);
7618 _scsih_raid_device_remove(ioc, raid_device);
7619 }
7620
Eric Moore635374e2009-03-09 01:21:12 -06007621 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307622 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007623 &ioc->sas_hba.sas_port_list, port_list) {
7624 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307625 SAS_END_DEVICE)
7626 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007627 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307628 else if (mpt2sas_port->remote_identify.device_type ==
7629 SAS_EDGE_EXPANDER_DEVICE ||
7630 mpt2sas_port->remote_identify.device_type ==
7631 SAS_FANOUT_EXPANDER_DEVICE)
7632 mpt2sas_expander_remove(ioc,
7633 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007634 }
7635
7636 /* free phys attached to the sas_host */
7637 if (ioc->sas_hba.num_phys) {
7638 kfree(ioc->sas_hba.phy);
7639 ioc->sas_hba.phy = NULL;
7640 ioc->sas_hba.num_phys = 0;
7641 }
7642
7643 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307644 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007645 list_del(&ioc->list);
7646 scsi_remove_host(shost);
7647 scsi_host_put(shost);
7648}
7649
7650/**
7651 * _scsih_probe_boot_devices - reports 1st device
7652 * @ioc: per adapter object
7653 *
7654 * If specified in bios page 2, this routine reports the 1st
7655 * device scsi-ml or sas transport for persistent boot device
7656 * purposes. Please refer to function _scsih_determine_boot_device()
7657 */
7658static void
7659_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7660{
7661 u8 is_raid;
7662 void *device;
7663 struct _sas_device *sas_device;
7664 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307665 u16 handle;
7666 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007667 u64 sas_address;
7668 unsigned long flags;
7669 int rc;
7670
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307671 /* no Bios, return immediately */
7672 if (!ioc->bios_pg3.BiosVersion)
7673 return;
7674
Eric Moore635374e2009-03-09 01:21:12 -06007675 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307676 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007677 if (ioc->req_boot_device.device) {
7678 device = ioc->req_boot_device.device;
7679 is_raid = ioc->req_boot_device.is_raid;
7680 } else if (ioc->req_alt_boot_device.device) {
7681 device = ioc->req_alt_boot_device.device;
7682 is_raid = ioc->req_alt_boot_device.is_raid;
7683 } else if (ioc->current_boot_device.device) {
7684 device = ioc->current_boot_device.device;
7685 is_raid = ioc->current_boot_device.is_raid;
7686 }
7687
7688 if (!device)
7689 return;
7690
7691 if (is_raid) {
7692 raid_device = device;
7693 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7694 raid_device->id, 0);
7695 if (rc)
7696 _scsih_raid_device_remove(ioc, raid_device);
7697 } else {
7698 sas_device = device;
7699 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307700 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007701 sas_address = sas_device->sas_address;
7702 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7703 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7704 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307705
7706 if (ioc->hide_drives)
7707 return;
Eric Moore635374e2009-03-09 01:21:12 -06007708 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307709 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007710 _scsih_sas_device_remove(ioc, sas_device);
7711 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307712 if (!ioc->is_driver_loading)
7713 mpt2sas_transport_port_remove(ioc, sas_address,
7714 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007715 _scsih_sas_device_remove(ioc, sas_device);
7716 }
7717 }
7718}
7719
7720/**
7721 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7722 * @ioc: per adapter object
7723 *
7724 * Called during initial loading of the driver.
7725 */
7726static void
7727_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7728{
7729 struct _raid_device *raid_device, *raid_next;
7730 int rc;
7731
7732 list_for_each_entry_safe(raid_device, raid_next,
7733 &ioc->raid_device_list, list) {
7734 if (raid_device->starget)
7735 continue;
7736 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7737 raid_device->id, 0);
7738 if (rc)
7739 _scsih_raid_device_remove(ioc, raid_device);
7740 }
7741}
7742
7743/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307744 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007745 * @ioc: per adapter object
7746 *
7747 * Called during initial loading of the driver.
7748 */
7749static void
7750_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7751{
7752 struct _sas_device *sas_device, *next;
7753 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007754
7755 /* SAS Device List */
7756 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7757 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007758
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307759 if (ioc->hide_drives)
7760 continue;
7761
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307762 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7763 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307764 list_del(&sas_device->list);
7765 kfree(sas_device);
7766 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007767 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307768 if (!ioc->is_driver_loading)
7769 mpt2sas_transport_port_remove(ioc,
7770 sas_device->sas_address,
7771 sas_device->sas_address_parent);
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307772 list_del(&sas_device->list);
7773 kfree(sas_device);
7774 continue;
7775
Eric Moore635374e2009-03-09 01:21:12 -06007776 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307777 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7778 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7779 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007780 }
7781}
7782
7783/**
7784 * _scsih_probe_devices - probing for devices
7785 * @ioc: per adapter object
7786 *
7787 * Called during initial loading of the driver.
7788 */
7789static void
7790_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7791{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307792 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06007793
7794 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7795 return; /* return when IOC doesn't support initiator mode */
7796
7797 _scsih_probe_boot_devices(ioc);
7798
7799 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307800 volume_mapping_flags =
7801 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7802 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7803 if (volume_mapping_flags ==
7804 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06007805 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307806 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007807 } else {
Eric Moore635374e2009-03-09 01:21:12 -06007808 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307809 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007810 }
7811 } else
7812 _scsih_probe_sas(ioc);
7813}
7814
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307815
7816/**
7817 * _scsih_scan_start - scsi lld callback for .scan_start
7818 * @shost: SCSI host pointer
7819 *
7820 * The shost has the ability to discover targets on its own instead
7821 * of scanning the entire bus. In our implemention, we will kick off
7822 * firmware discovery.
7823 */
7824static void
7825_scsih_scan_start(struct Scsi_Host *shost)
7826{
7827 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7828 int rc;
7829
7830 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7831 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
7832
7833 ioc->start_scan = 1;
7834 rc = mpt2sas_port_enable(ioc);
7835
7836 if (rc != 0)
7837 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
7838}
7839
7840/**
7841 * _scsih_scan_finished - scsi lld callback for .scan_finished
7842 * @shost: SCSI host pointer
7843 * @time: elapsed time of the scan in jiffies
7844 *
7845 * This function will be called periodically until it returns 1 with the
7846 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7847 * we wait for firmware discovery to complete, then return 1.
7848 */
7849static int
7850_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7851{
7852 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7853
7854 if (time >= (300 * HZ)) {
7855 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7856 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
7857 "(timeout=300s)\n", ioc->name);
7858 ioc->is_driver_loading = 0;
7859 return 1;
7860 }
7861
7862 if (ioc->start_scan)
7863 return 0;
7864
7865 if (ioc->start_scan_failed) {
7866 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
7867 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
7868 ioc->is_driver_loading = 0;
7869 ioc->wait_for_discovery_to_complete = 0;
7870 ioc->remove_host = 1;
7871 return 1;
7872 }
7873
7874 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
7875 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7876
7877 if (ioc->wait_for_discovery_to_complete) {
7878 ioc->wait_for_discovery_to_complete = 0;
7879 _scsih_probe_devices(ioc);
7880 }
7881 mpt2sas_base_start_watchdog(ioc);
7882 ioc->is_driver_loading = 0;
7883 return 1;
7884}
7885
7886
Eric Moore635374e2009-03-09 01:21:12 -06007887/**
Eric Moored5d135b2009-05-18 13:02:08 -06007888 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06007889 * @pdev: PCI device struct
7890 * @id: pci device id
7891 *
7892 * Returns 0 success, anything else error.
7893 */
7894static int
Eric Moored5d135b2009-05-18 13:02:08 -06007895_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06007896{
7897 struct MPT2SAS_ADAPTER *ioc;
7898 struct Scsi_Host *shost;
7899
7900 shost = scsi_host_alloc(&scsih_driver_template,
7901 sizeof(struct MPT2SAS_ADAPTER));
7902 if (!shost)
7903 return -ENODEV;
7904
7905 /* init local params */
7906 ioc = shost_priv(shost);
7907 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7908 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06007909 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06007910 ioc->shost = shost;
7911 ioc->id = mpt_ids++;
7912 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7913 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307914 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7915 ioc->is_warpdrive = 1;
7916 ioc->hide_ir_msg = 1;
7917 } else
7918 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06007919 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7920 ioc->tm_cb_idx = tm_cb_idx;
7921 ioc->ctl_cb_idx = ctl_cb_idx;
7922 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307923 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007924 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05307925 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007926 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307927 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307928 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307929 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007930 ioc->logging_level = logging_level;
7931 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05307932 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06007933 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7934 spin_lock_init(&ioc->scsi_lookup_lock);
7935 spin_lock_init(&ioc->sas_device_lock);
7936 spin_lock_init(&ioc->sas_node_lock);
7937 spin_lock_init(&ioc->fw_event_lock);
7938 spin_lock_init(&ioc->raid_device_lock);
7939
7940 INIT_LIST_HEAD(&ioc->sas_device_list);
7941 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7942 INIT_LIST_HEAD(&ioc->sas_expander_list);
7943 INIT_LIST_HEAD(&ioc->fw_event_list);
7944 INIT_LIST_HEAD(&ioc->raid_device_list);
7945 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307946 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307947 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06007948
7949 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06007950 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06007951 shost->max_lun = max_lun;
7952 shost->transportt = mpt2sas_transport_template;
7953 shost->unique_id = ioc->id;
7954
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05307955 if (max_sectors != 0xFFFF) {
7956 if (max_sectors < 64) {
7957 shost->max_sectors = 64;
7958 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7959 "for max_sectors, range is 64 to 8192. Assigning "
7960 "value of 64.\n", ioc->name, max_sectors);
7961 } else if (max_sectors > 8192) {
7962 shost->max_sectors = 8192;
7963 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7964 "for max_sectors, range is 64 to 8192. Assigning "
7965 "default value of 8192.\n", ioc->name,
7966 max_sectors);
7967 } else {
7968 shost->max_sectors = max_sectors & 0xFFFE;
7969 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
7970 "set to %d\n", ioc->name, shost->max_sectors);
7971 }
7972 }
7973
Eric Moore635374e2009-03-09 01:21:12 -06007974 if ((scsi_add_host(shost, &pdev->dev))) {
7975 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7976 ioc->name, __FILE__, __LINE__, __func__);
7977 list_del(&ioc->list);
7978 goto out_add_shost_fail;
7979 }
7980
Eric Moore3c621b32009-05-18 12:59:41 -06007981 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06007982 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307983 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06007984
Eric Moore635374e2009-03-09 01:21:12 -06007985 /* event thread */
7986 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7987 "fw_event%d", ioc->id);
7988 ioc->firmware_event_thread = create_singlethread_workqueue(
7989 ioc->firmware_event_name);
7990 if (!ioc->firmware_event_thread) {
7991 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7992 ioc->name, __FILE__, __LINE__, __func__);
7993 goto out_thread_fail;
7994 }
7995
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307996 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007997 if ((mpt2sas_base_attach(ioc))) {
7998 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7999 ioc->name, __FILE__, __LINE__, __func__);
8000 goto out_attach_fail;
8001 }
8002
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308003 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308004 if (ioc->is_warpdrive) {
8005 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8006 ioc->hide_drives = 0;
8007 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8008 ioc->hide_drives = 1;
8009 else {
8010 if (_scsih_get_num_volumes(ioc))
8011 ioc->hide_drives = 1;
8012 else
8013 ioc->hide_drives = 0;
8014 }
8015 } else
8016 ioc->hide_drives = 0;
8017
Eric Moore635374e2009-03-09 01:21:12 -06008018 _scsih_probe_devices(ioc);
8019 return 0;
8020
8021 out_attach_fail:
8022 destroy_workqueue(ioc->firmware_event_thread);
8023 out_thread_fail:
8024 list_del(&ioc->list);
8025 scsi_remove_host(shost);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308026 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008027 out_add_shost_fail:
8028 return -ENODEV;
8029}
8030
8031#ifdef CONFIG_PM
8032/**
Eric Moored5d135b2009-05-18 13:02:08 -06008033 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008034 * @pdev: PCI device struct
8035 * @state: PM state change to (usually PCI_D3)
8036 *
8037 * Returns 0 success, anything else error.
8038 */
8039static int
Eric Moored5d135b2009-05-18 13:02:08 -06008040_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008041{
8042 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8043 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308044 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008045
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308046 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008047 scsi_block_requests(shost);
8048 device_state = pci_choose_state(pdev, state);
8049 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8050 "operating state [D%d]\n", ioc->name, pdev,
8051 pci_name(pdev), device_state);
8052
8053 mpt2sas_base_free_resources(ioc);
8054 pci_save_state(pdev);
8055 pci_disable_device(pdev);
8056 pci_set_power_state(pdev, device_state);
8057 return 0;
8058}
8059
8060/**
Eric Moored5d135b2009-05-18 13:02:08 -06008061 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008062 * @pdev: PCI device struct
8063 *
8064 * Returns 0 success, anything else error.
8065 */
8066static int
Eric Moored5d135b2009-05-18 13:02:08 -06008067_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008068{
8069 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8070 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308071 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008072 int r;
8073
8074 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8075 "operating state [D%d]\n", ioc->name, pdev,
8076 pci_name(pdev), device_state);
8077
8078 pci_set_power_state(pdev, PCI_D0);
8079 pci_enable_wake(pdev, PCI_D0, 0);
8080 pci_restore_state(pdev);
8081 ioc->pdev = pdev;
8082 r = mpt2sas_base_map_resources(ioc);
8083 if (r)
8084 return r;
8085
8086 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8087 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308088 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008089 return 0;
8090}
8091#endif /* CONFIG_PM */
8092
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308093/**
8094 * _scsih_pci_error_detected - Called when a PCI error is detected.
8095 * @pdev: PCI device struct
8096 * @state: PCI channel state
8097 *
8098 * Description: Called when a PCI error is detected.
8099 *
8100 * Return value:
8101 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8102 */
8103static pci_ers_result_t
8104_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8105{
8106 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8107 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8108
8109 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8110 ioc->name, state);
8111
8112 switch (state) {
8113 case pci_channel_io_normal:
8114 return PCI_ERS_RESULT_CAN_RECOVER;
8115 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008116 /* Fatal error, prepare for slot reset */
8117 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308118 scsi_block_requests(ioc->shost);
8119 mpt2sas_base_stop_watchdog(ioc);
8120 mpt2sas_base_free_resources(ioc);
8121 return PCI_ERS_RESULT_NEED_RESET;
8122 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008123 /* Permanent error, prepare for device removal */
8124 ioc->pci_error_recovery = 1;
8125 mpt2sas_base_stop_watchdog(ioc);
8126 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308127 return PCI_ERS_RESULT_DISCONNECT;
8128 }
8129 return PCI_ERS_RESULT_NEED_RESET;
8130}
8131
8132/**
8133 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8134 * @pdev: PCI device struct
8135 *
8136 * Description: This routine is called by the pci error recovery
8137 * code after the PCI slot has been reset, just before we
8138 * should resume normal operations.
8139 */
8140static pci_ers_result_t
8141_scsih_pci_slot_reset(struct pci_dev *pdev)
8142{
8143 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8144 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8145 int rc;
8146
8147 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8148 ioc->name);
8149
Eric Moore3cb54692010-07-08 14:44:34 -06008150 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308151 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008152 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308153 rc = mpt2sas_base_map_resources(ioc);
8154 if (rc)
8155 return PCI_ERS_RESULT_DISCONNECT;
8156
8157
8158 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8159 FORCE_BIG_HAMMER);
8160
8161 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8162 (rc == 0) ? "success" : "failed");
8163
8164 if (!rc)
8165 return PCI_ERS_RESULT_RECOVERED;
8166 else
8167 return PCI_ERS_RESULT_DISCONNECT;
8168}
8169
8170/**
8171 * _scsih_pci_resume() - resume normal ops after PCI reset
8172 * @pdev: pointer to PCI device
8173 *
8174 * Called when the error recovery driver tells us that its
8175 * OK to resume normal operation. Use completion to allow
8176 * halted scsi ops to resume.
8177 */
8178static void
8179_scsih_pci_resume(struct pci_dev *pdev)
8180{
8181 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8182 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8183
8184 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8185
8186 pci_cleanup_aer_uncorrect_error_status(pdev);
8187 mpt2sas_base_start_watchdog(ioc);
8188 scsi_unblock_requests(ioc->shost);
8189}
8190
8191/**
8192 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8193 * @pdev: pointer to PCI device
8194 */
8195static pci_ers_result_t
8196_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8197{
8198 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8199 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8200
8201 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8202 ioc->name);
8203
8204 /* TODO - dump whatever for debugging purposes */
8205
8206 /* Request a slot reset. */
8207 return PCI_ERS_RESULT_NEED_RESET;
8208}
8209
8210static struct pci_error_handlers _scsih_err_handler = {
8211 .error_detected = _scsih_pci_error_detected,
8212 .mmio_enabled = _scsih_pci_mmio_enabled,
8213 .slot_reset = _scsih_pci_slot_reset,
8214 .resume = _scsih_pci_resume,
8215};
Eric Moore635374e2009-03-09 01:21:12 -06008216
8217static struct pci_driver scsih_driver = {
8218 .name = MPT2SAS_DRIVER_NAME,
8219 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008220 .probe = _scsih_probe,
8221 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05308222 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308223 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008224#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008225 .suspend = _scsih_suspend,
8226 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008227#endif
8228};
8229
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308230/* raid transport support */
8231static struct raid_function_template mpt2sas_raid_functions = {
8232 .cookie = &scsih_driver_template,
8233 .is_raid = _scsih_is_raid,
8234 .get_resync = _scsih_get_resync,
8235 .get_state = _scsih_get_state,
8236};
Eric Moore635374e2009-03-09 01:21:12 -06008237
8238/**
Eric Moored5d135b2009-05-18 13:02:08 -06008239 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008240 *
8241 * Returns 0 success, anything else error.
8242 */
8243static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008244_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008245{
8246 int error;
8247
8248 mpt_ids = 0;
8249 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8250 MPT2SAS_DRIVER_VERSION);
8251
8252 mpt2sas_transport_template =
8253 sas_attach_transport(&mpt2sas_transport_functions);
8254 if (!mpt2sas_transport_template)
8255 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308256 /* raid transport support */
8257 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8258 if (!mpt2sas_raid_template) {
8259 sas_release_transport(mpt2sas_transport_template);
8260 return -ENODEV;
8261 }
Eric Moore635374e2009-03-09 01:21:12 -06008262
8263 mpt2sas_base_initialize_callback_handler();
8264
8265 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008266 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008267
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008268 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008269 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008270
8271 /* base internal commands callback handler */
8272 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308273 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8274 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008275
8276 /* transport internal commands callback handler */
8277 transport_cb_idx = mpt2sas_base_register_callback_handler(
8278 mpt2sas_transport_done);
8279
Kashyap, Desai744090d2009-10-05 15:56:56 +05308280 /* scsih internal commands callback handler */
8281 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8282
Eric Moore635374e2009-03-09 01:21:12 -06008283 /* configuration page API internal commands callback handler */
8284 config_cb_idx = mpt2sas_base_register_callback_handler(
8285 mpt2sas_config_done);
8286
8287 /* ctl module callback handler */
8288 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8289
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308290 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8291 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308292
8293 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8294 _scsih_tm_volume_tr_complete);
8295
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308296 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8297 _scsih_sas_control_complete);
8298
Eric Moore635374e2009-03-09 01:21:12 -06008299 mpt2sas_ctl_init();
8300
8301 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308302 if (error) {
8303 /* raid transport support */
8304 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008305 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308306 }
Eric Moore635374e2009-03-09 01:21:12 -06008307
8308 return error;
8309}
8310
8311/**
Eric Moored5d135b2009-05-18 13:02:08 -06008312 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008313 *
8314 * Returns 0 success, anything else error.
8315 */
8316static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008317_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008318{
8319 printk(KERN_INFO "mpt2sas version %s unloading\n",
8320 MPT2SAS_DRIVER_VERSION);
8321
8322 pci_unregister_driver(&scsih_driver);
8323
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308324 mpt2sas_ctl_exit();
8325
Eric Moore635374e2009-03-09 01:21:12 -06008326 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8327 mpt2sas_base_release_callback_handler(tm_cb_idx);
8328 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308329 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008330 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308331 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008332 mpt2sas_base_release_callback_handler(config_cb_idx);
8333 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8334
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308335 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308336 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308337 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8338
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308339 /* raid transport support */
8340 raid_class_release(mpt2sas_raid_template);
8341 sas_release_transport(mpt2sas_transport_template);
8342
Eric Moore635374e2009-03-09 01:21:12 -06008343}
8344
Eric Moored5d135b2009-05-18 13:02:08 -06008345module_init(_scsih_init);
8346module_exit(_scsih_exit);