blob: a9efc4551086b99746d7c9aef10b28a176c96998 [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.com23edb6e2011-12-01 07:43:50 +0530615 } else if (!sas_device->starget) {
616 /* When asyn scanning is enabled, its not possible to remove
617 * devices while scanning is turned on due to an oops in
618 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
619 */
620 if (!ioc->is_driver_loading)
621 mpt2sas_transport_port_remove(ioc,
622 sas_device->sas_address,
623 sas_device->sas_address_parent);
624 _scsih_sas_device_remove(ioc, sas_device);
625 }
Eric Moore635374e2009-03-09 01:21:12 -0600626}
627
628/**
629 * _scsih_sas_device_init_add - insert sas_device to the list.
630 * @ioc: per adapter object
631 * @sas_device: the sas_device object
632 * Context: This function will acquire ioc->sas_device_lock.
633 *
634 * Adding new object at driver load time to the ioc->sas_device_init_list.
635 */
636static void
637_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
638 struct _sas_device *sas_device)
639{
640 unsigned long flags;
641
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530642 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600643 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
644 sas_device->handle, (unsigned long long)sas_device->sas_address));
645
646 spin_lock_irqsave(&ioc->sas_device_lock, flags);
647 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
648 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
649 _scsih_determine_boot_device(ioc, sas_device, 0);
650}
651
652/**
Eric Moore635374e2009-03-09 01:21:12 -0600653 * _scsih_raid_device_find_by_id - raid device search
654 * @ioc: per adapter object
655 * @id: sas device target id
656 * @channel: sas device channel
657 * Context: Calling function should acquire ioc->raid_device_lock
658 *
659 * This searches for raid_device based on target id, then return raid_device
660 * object.
661 */
662static struct _raid_device *
663_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
664{
665 struct _raid_device *raid_device, *r;
666
667 r = NULL;
668 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
669 if (raid_device->id == id && raid_device->channel == channel) {
670 r = raid_device;
671 goto out;
672 }
673 }
674
675 out:
676 return r;
677}
678
679/**
680 * _scsih_raid_device_find_by_handle - raid device search
681 * @ioc: per adapter object
682 * @handle: sas device handle (assigned by firmware)
683 * Context: Calling function should acquire ioc->raid_device_lock
684 *
685 * This searches for raid_device based on handle, then return raid_device
686 * object.
687 */
688static struct _raid_device *
689_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
690{
691 struct _raid_device *raid_device, *r;
692
693 r = NULL;
694 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
695 if (raid_device->handle != handle)
696 continue;
697 r = raid_device;
698 goto out;
699 }
700
701 out:
702 return r;
703}
704
705/**
706 * _scsih_raid_device_find_by_wwid - raid device search
707 * @ioc: per adapter object
708 * @handle: sas device handle (assigned by firmware)
709 * Context: Calling function should acquire ioc->raid_device_lock
710 *
711 * This searches for raid_device based on wwid, then return raid_device
712 * object.
713 */
714static struct _raid_device *
715_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
716{
717 struct _raid_device *raid_device, *r;
718
719 r = NULL;
720 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
721 if (raid_device->wwid != wwid)
722 continue;
723 r = raid_device;
724 goto out;
725 }
726
727 out:
728 return r;
729}
730
731/**
732 * _scsih_raid_device_add - add raid_device object
733 * @ioc: per adapter object
734 * @raid_device: raid_device object
735 *
736 * This is added to the raid_device_list link list.
737 */
738static void
739_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
740 struct _raid_device *raid_device)
741{
742 unsigned long flags;
743
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530744 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600745 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
746 raid_device->handle, (unsigned long long)raid_device->wwid));
747
748 spin_lock_irqsave(&ioc->raid_device_lock, flags);
749 list_add_tail(&raid_device->list, &ioc->raid_device_list);
750 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
751}
752
753/**
754 * _scsih_raid_device_remove - delete raid_device object
755 * @ioc: per adapter object
756 * @raid_device: raid_device object
757 *
758 * This is removed from the raid_device_list link list.
759 */
760static void
761_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
762 struct _raid_device *raid_device)
763{
764 unsigned long flags;
765
766 spin_lock_irqsave(&ioc->raid_device_lock, flags);
767 list_del(&raid_device->list);
768 memset(raid_device, 0, sizeof(struct _raid_device));
769 kfree(raid_device);
770 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
771}
772
773/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530774 * mpt2sas_scsih_expander_find_by_handle - expander device search
775 * @ioc: per adapter object
776 * @handle: expander handle (assigned by firmware)
777 * Context: Calling function should acquire ioc->sas_device_lock
778 *
779 * This searches for expander device based on handle, then returns the
780 * sas_node object.
781 */
782struct _sas_node *
783mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
784{
785 struct _sas_node *sas_expander, *r;
786
787 r = NULL;
788 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
789 if (sas_expander->handle != handle)
790 continue;
791 r = sas_expander;
792 goto out;
793 }
794 out:
795 return r;
796}
797
798/**
Eric Moore635374e2009-03-09 01:21:12 -0600799 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
800 * @ioc: per adapter object
801 * @sas_address: sas address
802 * Context: Calling function should acquire ioc->sas_node_lock.
803 *
804 * This searches for expander device based on sas_address, then returns the
805 * sas_node object.
806 */
807struct _sas_node *
808mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
809 u64 sas_address)
810{
811 struct _sas_node *sas_expander, *r;
812
813 r = NULL;
814 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
815 if (sas_expander->sas_address != sas_address)
816 continue;
817 r = sas_expander;
818 goto out;
819 }
820 out:
821 return r;
822}
823
824/**
825 * _scsih_expander_node_add - insert expander device to the list.
826 * @ioc: per adapter object
827 * @sas_expander: the sas_device object
828 * Context: This function will acquire ioc->sas_node_lock.
829 *
830 * Adding new object to the ioc->sas_expander_list.
831 *
832 * Return nothing.
833 */
834static void
835_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
836 struct _sas_node *sas_expander)
837{
838 unsigned long flags;
839
840 spin_lock_irqsave(&ioc->sas_node_lock, flags);
841 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
842 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
843}
844
845/**
846 * _scsih_is_end_device - determines if device is an end device
847 * @device_info: bitfield providing information about the device.
848 * Context: none
849 *
850 * Returns 1 if end device.
851 */
852static int
853_scsih_is_end_device(u32 device_info)
854{
855 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
856 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
857 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
858 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
859 return 1;
860 else
861 return 0;
862}
863
864/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530865 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600866 * @ioc: per adapter object
867 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600868 *
869 * Returns the smid stored scmd pointer.
870 */
871static struct scsi_cmnd *
872_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
873{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530874 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600875}
876
877/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530878 * _scsih_scsi_lookup_get_clear - returns scmd entry
879 * @ioc: per adapter object
880 * @smid: system request message index
881 *
882 * Returns the smid stored scmd pointer.
883 * Then will derefrence the stored scmd pointer.
884 */
885static inline struct scsi_cmnd *
886_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
887{
888 unsigned long flags;
889 struct scsi_cmnd *scmd;
890
891 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
892 scmd = ioc->scsi_lookup[smid - 1].scmd;
893 ioc->scsi_lookup[smid - 1].scmd = NULL;
894 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
895
896 return scmd;
897}
898
899/**
Eric Moore635374e2009-03-09 01:21:12 -0600900 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
901 * @ioc: per adapter object
902 * @smid: system request message index
903 * @scmd: pointer to scsi command object
904 * Context: This function will acquire ioc->scsi_lookup_lock.
905 *
906 * This will search for a scmd pointer in the scsi_lookup array,
907 * returning the revelent smid. A returned value of zero means invalid.
908 */
909static u16
910_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
911 *scmd)
912{
913 u16 smid;
914 unsigned long flags;
915 int i;
916
917 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
918 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530919 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600920 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530921 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600922 goto out;
923 }
924 }
925 out:
926 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
927 return smid;
928}
929
930/**
931 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
932 * @ioc: per adapter object
933 * @id: target id
934 * @channel: channel
935 * Context: This function will acquire ioc->scsi_lookup_lock.
936 *
937 * This will search for a matching channel:id in the scsi_lookup array,
938 * returning 1 if found.
939 */
940static u8
941_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
942 int channel)
943{
944 u8 found;
945 unsigned long flags;
946 int i;
947
948 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
949 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530950 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600951 if (ioc->scsi_lookup[i].scmd &&
952 (ioc->scsi_lookup[i].scmd->device->id == id &&
953 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
954 found = 1;
955 goto out;
956 }
957 }
958 out:
959 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
960 return found;
961}
962
963/**
Eric Moore993e0da2009-05-18 13:00:45 -0600964 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
965 * @ioc: per adapter object
966 * @id: target id
967 * @lun: lun number
968 * @channel: channel
969 * Context: This function will acquire ioc->scsi_lookup_lock.
970 *
971 * This will search for a matching channel:id:lun in the scsi_lookup array,
972 * returning 1 if found.
973 */
974static u8
975_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
976 unsigned int lun, int channel)
977{
978 u8 found;
979 unsigned long flags;
980 int i;
981
982 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
983 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530984 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600985 if (ioc->scsi_lookup[i].scmd &&
986 (ioc->scsi_lookup[i].scmd->device->id == id &&
987 ioc->scsi_lookup[i].scmd->device->channel == channel &&
988 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
989 found = 1;
990 goto out;
991 }
992 }
993 out:
994 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
995 return found;
996}
997
998/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530999 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -06001000 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301001 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -06001002 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301003 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001004 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301005static struct chain_tracker *
1006_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001007{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301008 struct chain_tracker *chain_req;
1009 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001010
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301011 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1012 if (list_empty(&ioc->free_chain_list)) {
1013 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1014 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
1015 ioc->name);
1016 return NULL;
1017 }
1018 chain_req = list_entry(ioc->free_chain_list.next,
1019 struct chain_tracker, tracker_list);
1020 list_del_init(&chain_req->tracker_list);
1021 list_add_tail(&chain_req->tracker_list,
1022 &ioc->scsi_lookup[smid - 1].chain_list);
1023 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1024 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001025}
1026
1027/**
1028 * _scsih_build_scatter_gather - main sg creation routine
1029 * @ioc: per adapter object
1030 * @scmd: scsi command
1031 * @smid: system request message index
1032 * Context: none.
1033 *
1034 * The main routine that builds scatter gather table from a given
1035 * scsi request sent via the .queuecommand main handler.
1036 *
1037 * Returns 0 success, anything else error
1038 */
1039static int
1040_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1041 struct scsi_cmnd *scmd, u16 smid)
1042{
1043 Mpi2SCSIIORequest_t *mpi_request;
1044 dma_addr_t chain_dma;
1045 struct scatterlist *sg_scmd;
1046 void *sg_local, *chain;
1047 u32 chain_offset;
1048 u32 chain_length;
1049 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001050 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001051 u32 sges_in_segment;
1052 u32 sgl_flags;
1053 u32 sgl_flags_last_element;
1054 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301055 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001056
1057 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1058
1059 /* init scatter gather flags */
1060 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1061 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1062 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1063 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1064 << MPI2_SGE_FLAGS_SHIFT;
1065 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1066 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1067 << MPI2_SGE_FLAGS_SHIFT;
1068 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1069
1070 sg_scmd = scsi_sglist(scmd);
1071 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001072 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001073 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1074 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1075 return -ENOMEM;
1076 }
1077
1078 sg_local = &mpi_request->SGL;
1079 sges_in_segment = ioc->max_sges_in_main_message;
1080 if (sges_left <= sges_in_segment)
1081 goto fill_in_last_segment;
1082
1083 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1084 (sges_in_segment * ioc->sge_size))/4;
1085
1086 /* fill in main message segment when there is a chain following */
1087 while (sges_in_segment) {
1088 if (sges_in_segment == 1)
1089 ioc->base_add_sg_single(sg_local,
1090 sgl_flags_last_element | sg_dma_len(sg_scmd),
1091 sg_dma_address(sg_scmd));
1092 else
1093 ioc->base_add_sg_single(sg_local, sgl_flags |
1094 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1095 sg_scmd = sg_next(sg_scmd);
1096 sg_local += ioc->sge_size;
1097 sges_left--;
1098 sges_in_segment--;
1099 }
1100
1101 /* initializing the chain flags and pointers */
1102 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301103 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1104 if (!chain_req)
1105 return -1;
1106 chain = chain_req->chain_buffer;
1107 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001108 do {
1109 sges_in_segment = (sges_left <=
1110 ioc->max_sges_in_chain_message) ? sges_left :
1111 ioc->max_sges_in_chain_message;
1112 chain_offset = (sges_left == sges_in_segment) ?
1113 0 : (sges_in_segment * ioc->sge_size)/4;
1114 chain_length = sges_in_segment * ioc->sge_size;
1115 if (chain_offset) {
1116 chain_offset = chain_offset <<
1117 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1118 chain_length += ioc->sge_size;
1119 }
1120 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1121 chain_length, chain_dma);
1122 sg_local = chain;
1123 if (!chain_offset)
1124 goto fill_in_last_segment;
1125
1126 /* fill in chain segments */
1127 while (sges_in_segment) {
1128 if (sges_in_segment == 1)
1129 ioc->base_add_sg_single(sg_local,
1130 sgl_flags_last_element |
1131 sg_dma_len(sg_scmd),
1132 sg_dma_address(sg_scmd));
1133 else
1134 ioc->base_add_sg_single(sg_local, sgl_flags |
1135 sg_dma_len(sg_scmd),
1136 sg_dma_address(sg_scmd));
1137 sg_scmd = sg_next(sg_scmd);
1138 sg_local += ioc->sge_size;
1139 sges_left--;
1140 sges_in_segment--;
1141 }
1142
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301143 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1144 if (!chain_req)
1145 return -1;
1146 chain = chain_req->chain_buffer;
1147 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001148 } while (1);
1149
1150
1151 fill_in_last_segment:
1152
1153 /* fill the last segment */
1154 while (sges_left) {
1155 if (sges_left == 1)
1156 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1157 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1158 else
1159 ioc->base_add_sg_single(sg_local, sgl_flags |
1160 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1161 sg_scmd = sg_next(sg_scmd);
1162 sg_local += ioc->sge_size;
1163 sges_left--;
1164 }
1165
1166 return 0;
1167}
1168
1169/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301170 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001171 * @sdev: scsi device struct
1172 * @qdepth: requested queue depth
1173 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301174 *
1175 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001176 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301177static void
1178_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001179{
1180 struct Scsi_Host *shost = sdev->host;
1181 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301182 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1183 struct MPT2SAS_DEVICE *sas_device_priv_data;
1184 struct MPT2SAS_TARGET *sas_target_priv_data;
1185 struct _sas_device *sas_device;
1186 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001187
1188 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301189
1190 /* limit max device queue for SATA to 32 */
1191 sas_device_priv_data = sdev->hostdata;
1192 if (!sas_device_priv_data)
1193 goto not_sata;
1194 sas_target_priv_data = sas_device_priv_data->sas_target;
1195 if (!sas_target_priv_data)
1196 goto not_sata;
1197 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1198 goto not_sata;
1199 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1200 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1201 sas_device_priv_data->sas_target->sas_address);
1202 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1203 if (sas_device && sas_device->device_info &
1204 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1205 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1206
1207 not_sata:
1208
Eric Moore635374e2009-03-09 01:21:12 -06001209 if (!sdev->tagged_supported)
1210 max_depth = 1;
1211 if (qdepth > max_depth)
1212 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301213 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1214}
1215
1216/**
1217 * _scsih_change_queue_depth - setting device queue depth
1218 * @sdev: scsi device struct
1219 * @qdepth: requested queue depth
1220 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1221 * (see include/scsi/scsi_host.h for definition)
1222 *
1223 * Returns queue depth.
1224 */
1225static int
1226_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1227{
1228 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1229 _scsih_adjust_queue_depth(sdev, qdepth);
1230 else if (reason == SCSI_QDEPTH_QFULL)
1231 scsi_track_queue_full(sdev, qdepth);
1232 else
1233 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001234
1235 if (sdev->inquiry_len > 7)
1236 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1237 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1238 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1239 sdev->ordered_tags, sdev->scsi_level,
1240 (sdev->inquiry[7] & 2) >> 1);
1241
1242 return sdev->queue_depth;
1243}
1244
1245/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301246 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001247 * @sdev: scsi device struct
1248 * @tag_type: requested tag type
1249 *
1250 * Returns queue tag type.
1251 */
1252static int
Eric Moored5d135b2009-05-18 13:02:08 -06001253_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001254{
1255 if (sdev->tagged_supported) {
1256 scsi_set_tag_type(sdev, tag_type);
1257 if (tag_type)
1258 scsi_activate_tcq(sdev, sdev->queue_depth);
1259 else
1260 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1261 } else
1262 tag_type = 0;
1263
1264 return tag_type;
1265}
1266
1267/**
Eric Moored5d135b2009-05-18 13:02:08 -06001268 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001269 * @starget: scsi target struct
1270 *
1271 * Returns 0 if ok. Any other return is assumed to be an error and
1272 * the device is ignored.
1273 */
1274static int
Eric Moored5d135b2009-05-18 13:02:08 -06001275_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001276{
1277 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1278 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1279 struct MPT2SAS_TARGET *sas_target_priv_data;
1280 struct _sas_device *sas_device;
1281 struct _raid_device *raid_device;
1282 unsigned long flags;
1283 struct sas_rphy *rphy;
1284
1285 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1286 if (!sas_target_priv_data)
1287 return -ENOMEM;
1288
1289 starget->hostdata = sas_target_priv_data;
1290 sas_target_priv_data->starget = starget;
1291 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1292
1293 /* RAID volumes */
1294 if (starget->channel == RAID_CHANNEL) {
1295 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1296 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1297 starget->channel);
1298 if (raid_device) {
1299 sas_target_priv_data->handle = raid_device->handle;
1300 sas_target_priv_data->sas_address = raid_device->wwid;
1301 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301302 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001303 raid_device->starget = starget;
1304 }
1305 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1306 return 0;
1307 }
1308
1309 /* sas/sata devices */
1310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1311 rphy = dev_to_rphy(starget->dev.parent);
1312 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1313 rphy->identify.sas_address);
1314
1315 if (sas_device) {
1316 sas_target_priv_data->handle = sas_device->handle;
1317 sas_target_priv_data->sas_address = sas_device->sas_address;
1318 sas_device->starget = starget;
1319 sas_device->id = starget->id;
1320 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301321 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001322 sas_target_priv_data->flags |=
1323 MPT_TARGET_FLAGS_RAID_COMPONENT;
1324 }
1325 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1326
1327 return 0;
1328}
1329
1330/**
Eric Moored5d135b2009-05-18 13:02:08 -06001331 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001332 * @starget: scsi target struct
1333 *
1334 * Returns nothing.
1335 */
1336static void
Eric Moored5d135b2009-05-18 13:02:08 -06001337_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001338{
1339 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1340 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1341 struct MPT2SAS_TARGET *sas_target_priv_data;
1342 struct _sas_device *sas_device;
1343 struct _raid_device *raid_device;
1344 unsigned long flags;
1345 struct sas_rphy *rphy;
1346
1347 sas_target_priv_data = starget->hostdata;
1348 if (!sas_target_priv_data)
1349 return;
1350
1351 if (starget->channel == RAID_CHANNEL) {
1352 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1353 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1354 starget->channel);
1355 if (raid_device) {
1356 raid_device->starget = NULL;
1357 raid_device->sdev = NULL;
1358 }
1359 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1360 goto out;
1361 }
1362
1363 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1364 rphy = dev_to_rphy(starget->dev.parent);
1365 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1366 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001367 if (sas_device && (sas_device->starget == starget) &&
1368 (sas_device->id == starget->id) &&
1369 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001370 sas_device->starget = NULL;
1371
1372 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1373
1374 out:
1375 kfree(sas_target_priv_data);
1376 starget->hostdata = NULL;
1377}
1378
1379/**
Eric Moored5d135b2009-05-18 13:02:08 -06001380 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001381 * @sdev: scsi device struct
1382 *
1383 * Returns 0 if ok. Any other return is assumed to be an error and
1384 * the device is ignored.
1385 */
1386static int
Eric Moored5d135b2009-05-18 13:02:08 -06001387_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001388{
1389 struct Scsi_Host *shost;
1390 struct MPT2SAS_ADAPTER *ioc;
1391 struct MPT2SAS_TARGET *sas_target_priv_data;
1392 struct MPT2SAS_DEVICE *sas_device_priv_data;
1393 struct scsi_target *starget;
1394 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001395 unsigned long flags;
1396
1397 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1398 if (!sas_device_priv_data)
1399 return -ENOMEM;
1400
1401 sas_device_priv_data->lun = sdev->lun;
1402 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1403
1404 starget = scsi_target(sdev);
1405 sas_target_priv_data = starget->hostdata;
1406 sas_target_priv_data->num_luns++;
1407 sas_device_priv_data->sas_target = sas_target_priv_data;
1408 sdev->hostdata = sas_device_priv_data;
1409 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1410 sdev->no_uld_attach = 1;
1411
1412 shost = dev_to_shost(&starget->dev);
1413 ioc = shost_priv(shost);
1414 if (starget->channel == RAID_CHANNEL) {
1415 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1416 raid_device = _scsih_raid_device_find_by_id(ioc,
1417 starget->id, starget->channel);
1418 if (raid_device)
1419 raid_device->sdev = sdev; /* raid is single lun */
1420 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001421 }
1422
Eric Moore635374e2009-03-09 01:21:12 -06001423 return 0;
1424}
1425
1426/**
Eric Moored5d135b2009-05-18 13:02:08 -06001427 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001428 * @sdev: scsi device struct
1429 *
1430 * Returns nothing.
1431 */
1432static void
Eric Moored5d135b2009-05-18 13:02:08 -06001433_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001434{
1435 struct MPT2SAS_TARGET *sas_target_priv_data;
1436 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301437 struct Scsi_Host *shost;
1438 struct MPT2SAS_ADAPTER *ioc;
1439 struct _sas_device *sas_device;
1440 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001441
1442 if (!sdev->hostdata)
1443 return;
1444
1445 starget = scsi_target(sdev);
1446 sas_target_priv_data = starget->hostdata;
1447 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301448
1449 shost = dev_to_shost(&starget->dev);
1450 ioc = shost_priv(shost);
1451
1452 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1453 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1454 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1455 sas_target_priv_data->sas_address);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +05301456 if (sas_device && !sas_target_priv_data->num_luns)
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301457 sas_device->starget = NULL;
1458 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1459 }
1460
Eric Moore635374e2009-03-09 01:21:12 -06001461 kfree(sdev->hostdata);
1462 sdev->hostdata = NULL;
1463}
1464
1465/**
Eric Moored5d135b2009-05-18 13:02:08 -06001466 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001467 * @ioc: per adapter object
1468 * @sas_device: the sas_device object
1469 * @sdev: scsi device struct
1470 */
1471static void
Eric Moored5d135b2009-05-18 13:02:08 -06001472_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001473 struct _sas_device *sas_device, struct scsi_device *sdev)
1474{
1475 Mpi2ConfigReply_t mpi_reply;
1476 Mpi2SasDevicePage0_t sas_device_pg0;
1477 u32 ioc_status;
1478 u16 flags;
1479 u32 device_info;
1480
1481 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1482 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1483 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1484 ioc->name, __FILE__, __LINE__, __func__);
1485 return;
1486 }
1487
1488 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1489 MPI2_IOCSTATUS_MASK;
1490 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1491 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1492 ioc->name, __FILE__, __LINE__, __func__);
1493 return;
1494 }
1495
1496 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301497 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001498
1499 sdev_printk(KERN_INFO, sdev,
1500 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1501 "sw_preserve(%s)\n",
1502 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1503 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1504 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1505 "n",
1506 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1507 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1508 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1509}
1510
1511/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301512 * _scsih_is_raid - return boolean indicating device is raid volume
1513 * @dev the device struct object
1514 */
1515static int
1516_scsih_is_raid(struct device *dev)
1517{
1518 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301519 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301520
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301521 if (ioc->is_warpdrive)
1522 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301523 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1524}
1525
1526/**
1527 * _scsih_get_resync - get raid volume resync percent complete
1528 * @dev the device struct object
1529 */
1530static void
1531_scsih_get_resync(struct device *dev)
1532{
1533 struct scsi_device *sdev = to_scsi_device(dev);
1534 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1535 static struct _raid_device *raid_device;
1536 unsigned long flags;
1537 Mpi2RaidVolPage0_t vol_pg0;
1538 Mpi2ConfigReply_t mpi_reply;
1539 u32 volume_status_flags;
1540 u8 percent_complete = 0;
1541
1542 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1543 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1544 sdev->channel);
1545 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1546
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301547 if (!raid_device || ioc->is_warpdrive)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301548 goto out;
1549
1550 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1551 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1552 sizeof(Mpi2RaidVolPage0_t))) {
1553 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1554 ioc->name, __FILE__, __LINE__, __func__);
1555 goto out;
1556 }
1557
1558 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1559 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1560 percent_complete = raid_device->percent_complete;
1561 out:
1562 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1563}
1564
1565/**
1566 * _scsih_get_state - get raid volume level
1567 * @dev the device struct object
1568 */
1569static void
1570_scsih_get_state(struct device *dev)
1571{
1572 struct scsi_device *sdev = to_scsi_device(dev);
1573 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1574 static struct _raid_device *raid_device;
1575 unsigned long flags;
1576 Mpi2RaidVolPage0_t vol_pg0;
1577 Mpi2ConfigReply_t mpi_reply;
1578 u32 volstate;
1579 enum raid_state state = RAID_STATE_UNKNOWN;
1580
1581 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1582 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1583 sdev->channel);
1584 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1585
1586 if (!raid_device)
1587 goto out;
1588
1589 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1590 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1591 sizeof(Mpi2RaidVolPage0_t))) {
1592 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1593 ioc->name, __FILE__, __LINE__, __func__);
1594 goto out;
1595 }
1596
1597 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1598 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1599 state = RAID_STATE_RESYNCING;
1600 goto out;
1601 }
1602
1603 switch (vol_pg0.VolumeState) {
1604 case MPI2_RAID_VOL_STATE_OPTIMAL:
1605 case MPI2_RAID_VOL_STATE_ONLINE:
1606 state = RAID_STATE_ACTIVE;
1607 break;
1608 case MPI2_RAID_VOL_STATE_DEGRADED:
1609 state = RAID_STATE_DEGRADED;
1610 break;
1611 case MPI2_RAID_VOL_STATE_FAILED:
1612 case MPI2_RAID_VOL_STATE_MISSING:
1613 state = RAID_STATE_OFFLINE;
1614 break;
1615 }
1616 out:
1617 raid_set_state(mpt2sas_raid_template, dev, state);
1618}
1619
1620/**
1621 * _scsih_set_level - set raid level
1622 * @sdev: scsi device struct
1623 * @raid_device: raid_device object
1624 */
1625static void
1626_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1627{
1628 enum raid_level level = RAID_LEVEL_UNKNOWN;
1629
1630 switch (raid_device->volume_type) {
1631 case MPI2_RAID_VOL_TYPE_RAID0:
1632 level = RAID_LEVEL_0;
1633 break;
1634 case MPI2_RAID_VOL_TYPE_RAID10:
1635 level = RAID_LEVEL_10;
1636 break;
1637 case MPI2_RAID_VOL_TYPE_RAID1E:
1638 level = RAID_LEVEL_1E;
1639 break;
1640 case MPI2_RAID_VOL_TYPE_RAID1:
1641 level = RAID_LEVEL_1;
1642 break;
1643 }
1644
1645 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1646}
1647
1648/**
Eric Moore635374e2009-03-09 01:21:12 -06001649 * _scsih_get_volume_capabilities - volume capabilities
1650 * @ioc: per adapter object
1651 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301652 *
1653 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001654 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301655static int
Eric Moore635374e2009-03-09 01:21:12 -06001656_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1657 struct _raid_device *raid_device)
1658{
1659 Mpi2RaidVolPage0_t *vol_pg0;
1660 Mpi2RaidPhysDiskPage0_t pd_pg0;
1661 Mpi2SasDevicePage0_t sas_device_pg0;
1662 Mpi2ConfigReply_t mpi_reply;
1663 u16 sz;
1664 u8 num_pds;
1665
1666 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1667 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301668 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1669 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1670 __func__));
1671 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001672 }
1673
1674 raid_device->num_pds = num_pds;
1675 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1676 sizeof(Mpi2RaidVol0PhysDisk_t));
1677 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1678 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301679 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1680 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1681 __func__));
1682 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001683 }
1684
1685 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1686 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301687 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1688 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1689 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001690 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301691 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001692 }
1693
1694 raid_device->volume_type = vol_pg0->VolumeType;
1695
1696 /* figure out what the underlying devices are by
1697 * obtaining the device_info bits for the 1st device
1698 */
1699 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1700 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1701 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1702 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1703 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1704 le16_to_cpu(pd_pg0.DevHandle)))) {
1705 raid_device->device_info =
1706 le32_to_cpu(sas_device_pg0.DeviceInfo);
1707 }
1708 }
1709
1710 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301711 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001712}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301713/**
1714 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1715 * @ioc: per adapter object
1716 */
1717static void
1718_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1719{
1720 Mpi2RaidVolPage1_t vol_pg1;
1721 Mpi2ConfigReply_t mpi_reply;
1722 struct _raid_device *raid_device;
1723 u16 handle;
1724 u16 ioc_status;
1725
1726 handle = 0xFFFF;
1727 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1728 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1729 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1730 MPI2_IOCSTATUS_MASK;
1731 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1732 break;
1733 handle = le16_to_cpu(vol_pg1.DevHandle);
1734 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1735 if (raid_device)
1736 raid_device->direct_io_enabled = 0;
1737 }
1738 return;
1739}
1740
1741
1742/**
1743 * _scsih_get_num_volumes - Get number of volumes in the ioc
1744 * @ioc: per adapter object
1745 */
1746static u8
1747_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1748{
1749 Mpi2RaidVolPage1_t vol_pg1;
1750 Mpi2ConfigReply_t mpi_reply;
1751 u16 handle;
1752 u8 vol_cnt = 0;
1753 u16 ioc_status;
1754
1755 handle = 0xFFFF;
1756 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1757 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1758 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1759 MPI2_IOCSTATUS_MASK;
1760 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1761 break;
1762 vol_cnt++;
1763 handle = le16_to_cpu(vol_pg1.DevHandle);
1764 }
1765 return vol_cnt;
1766}
1767
1768
1769/**
1770 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1771 * @ioc: per adapter object
1772 * @raid_device: the raid_device object
1773 */
1774static void
1775_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1776 struct _raid_device *raid_device)
1777{
1778 Mpi2RaidVolPage0_t *vol_pg0;
1779 Mpi2RaidPhysDiskPage0_t pd_pg0;
1780 Mpi2ConfigReply_t mpi_reply;
1781 u16 sz;
1782 u8 num_pds, count;
1783 u64 mb = 1024 * 1024;
1784 u64 tb_2 = 2 * mb * mb;
1785 u64 capacity;
1786 u32 stripe_sz;
1787 u8 i, stripe_exp;
1788
1789 if (!ioc->is_warpdrive)
1790 return;
1791
1792 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1793 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1794 "globally as drives are exposed\n", ioc->name);
1795 return;
1796 }
1797 if (_scsih_get_num_volumes(ioc) > 1) {
1798 _scsih_disable_ddio(ioc);
1799 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1800 "globally as number of drives > 1\n", ioc->name);
1801 return;
1802 }
1803 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1804 &num_pds)) || !num_pds) {
1805 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1806 "Failure in computing number of drives\n", ioc->name);
1807 return;
1808 }
1809
1810 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1811 sizeof(Mpi2RaidVol0PhysDisk_t));
1812 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1813 if (!vol_pg0) {
1814 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1815 "Memory allocation failure for RVPG0\n", ioc->name);
1816 return;
1817 }
1818
1819 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1820 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1821 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1822 "Failure in retrieving RVPG0\n", ioc->name);
1823 kfree(vol_pg0);
1824 return;
1825 }
1826
1827 /*
1828 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1829 * assumed for WARPDRIVE, disable direct I/O
1830 */
1831 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1832 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1833 "for the drive with handle(0x%04x): num_mem=%d, "
1834 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1835 num_pds, MPT_MAX_WARPDRIVE_PDS);
1836 kfree(vol_pg0);
1837 return;
1838 }
1839 for (count = 0; count < num_pds; count++) {
1840 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1841 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1842 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1843 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1844 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1845 "disabled for the drive with handle(0x%04x) member"
1846 "handle retrieval failed for member number=%d\n",
1847 ioc->name, raid_device->handle,
1848 vol_pg0->PhysDisk[count].PhysDiskNum);
1849 goto out_error;
1850 }
1851 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1852 }
1853
1854 /*
1855 * Assumption for WD: Direct I/O is not supported if the volume is
1856 * not RAID0, if the stripe size is not 64KB, if the block size is
1857 * not 512 and if the volume size is >2TB
1858 */
1859 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1860 le16_to_cpu(vol_pg0->BlockSize) != 512) {
1861 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1862 "for the drive with handle(0x%04x): type=%d, "
1863 "s_sz=%uK, blk_size=%u\n", ioc->name,
1864 raid_device->handle, raid_device->volume_type,
1865 le32_to_cpu(vol_pg0->StripeSize)/2,
1866 le16_to_cpu(vol_pg0->BlockSize));
1867 goto out_error;
1868 }
1869
1870 capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1871 (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1872
1873 if (capacity > tb_2) {
1874 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1875 "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1876 ioc->name, raid_device->handle);
1877 goto out_error;
1878 }
1879
1880 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1881 stripe_exp = 0;
1882 for (i = 0; i < 32; i++) {
1883 if (stripe_sz & 1)
1884 break;
1885 stripe_exp++;
1886 stripe_sz >>= 1;
1887 }
1888 if (i == 32) {
1889 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1890 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1891 ioc->name, raid_device->handle,
1892 le32_to_cpu(vol_pg0->StripeSize)/2);
1893 goto out_error;
1894 }
1895 raid_device->stripe_exponent = stripe_exp;
1896 raid_device->direct_io_enabled = 1;
1897
1898 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1899 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1900 /*
1901 * WARPDRIVE: Though the following fields are not used for direct IO,
1902 * stored for future purpose:
1903 */
1904 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1905 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1906 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1907
1908
1909 kfree(vol_pg0);
1910 return;
1911
1912out_error:
1913 raid_device->direct_io_enabled = 0;
1914 for (count = 0; count < num_pds; count++)
1915 raid_device->pd_handle[count] = 0;
1916 kfree(vol_pg0);
1917 return;
1918}
Eric Moore635374e2009-03-09 01:21:12 -06001919
1920/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301921 * _scsih_enable_tlr - setting TLR flags
1922 * @ioc: per adapter object
1923 * @sdev: scsi device struct
1924 *
1925 * Enabling Transaction Layer Retries for tape devices when
1926 * vpd page 0x90 is present
1927 *
1928 */
1929static void
1930_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1931{
1932 /* only for TAPE */
1933 if (sdev->type != TYPE_TAPE)
1934 return;
1935
1936 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1937 return;
1938
1939 sas_enable_tlr(sdev);
1940 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1941 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1942 return;
1943
1944}
1945
1946/**
Eric Moored5d135b2009-05-18 13:02:08 -06001947 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001948 * @sdev: scsi device struct
1949 *
1950 * Returns 0 if ok. Any other return is assumed to be an error and
1951 * the device is ignored.
1952 */
1953static int
Eric Moored5d135b2009-05-18 13:02:08 -06001954_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001955{
1956 struct Scsi_Host *shost = sdev->host;
1957 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1958 struct MPT2SAS_DEVICE *sas_device_priv_data;
1959 struct MPT2SAS_TARGET *sas_target_priv_data;
1960 struct _sas_device *sas_device;
1961 struct _raid_device *raid_device;
1962 unsigned long flags;
1963 int qdepth;
1964 u8 ssp_target = 0;
1965 char *ds = "";
1966 char *r_level = "";
1967
1968 qdepth = 1;
1969 sas_device_priv_data = sdev->hostdata;
1970 sas_device_priv_data->configured_lun = 1;
1971 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1972 sas_target_priv_data = sas_device_priv_data->sas_target;
1973
1974 /* raid volume handling */
1975 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1976
1977 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1978 raid_device = _scsih_raid_device_find_by_handle(ioc,
1979 sas_target_priv_data->handle);
1980 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1981 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301982 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1983 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1984 __LINE__, __func__));
1985 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001986 }
1987
1988 _scsih_get_volume_capabilities(ioc, raid_device);
1989
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301990 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1991 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1992 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1993 __LINE__, __func__));
1994 return 1;
1995 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301996 /*
1997 * WARPDRIVE: Initialize the required data for Direct IO
1998 */
1999 _scsih_init_warpdrive_properties(ioc, raid_device);
2000
Eric Moore635374e2009-03-09 01:21:12 -06002001 /* RAID Queue Depth Support
2002 * IS volume = underlying qdepth of drive type, either
2003 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2004 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2005 */
2006 if (raid_device->device_info &
2007 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2008 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2009 ds = "SSP";
2010 } else {
2011 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2012 if (raid_device->device_info &
2013 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2014 ds = "SATA";
2015 else
2016 ds = "STP";
2017 }
2018
2019 switch (raid_device->volume_type) {
2020 case MPI2_RAID_VOL_TYPE_RAID0:
2021 r_level = "RAID0";
2022 break;
2023 case MPI2_RAID_VOL_TYPE_RAID1E:
2024 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302025 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302026 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302027 MFG10_GF0_R10_DISPLAY) &&
2028 !(raid_device->num_pds % 2))
2029 r_level = "RAID10";
2030 else
2031 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002032 break;
2033 case MPI2_RAID_VOL_TYPE_RAID1:
2034 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2035 r_level = "RAID1";
2036 break;
2037 case MPI2_RAID_VOL_TYPE_RAID10:
2038 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2039 r_level = "RAID10";
2040 break;
2041 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2042 default:
2043 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2044 r_level = "RAIDX";
2045 break;
2046 }
2047
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302048 if (!ioc->hide_ir_msg)
2049 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2050 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2051 r_level, raid_device->handle,
2052 (unsigned long long)raid_device->wwid,
2053 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002054 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302055 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302056 if (!ioc->is_warpdrive)
2057 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06002058 return 0;
2059 }
2060
2061 /* non-raid handling */
2062 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2063 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2064 sas_device_priv_data->sas_target->sas_address);
2065 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2066 if (sas_device) {
2067 if (sas_target_priv_data->flags &
2068 MPT_TARGET_FLAGS_RAID_COMPONENT) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302069 if (mpt2sas_config_get_volume_handle(ioc,
2070 sas_device->handle, &sas_device->volume_handle)) {
2071 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2072 "failure at %s:%d/%s()!\n", ioc->name,
2073 __FILE__, __LINE__, __func__));
2074 return 1;
2075 }
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05302076 if (sas_device->volume_handle &&
2077 mpt2sas_config_get_volume_wwid(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06002078 sas_device->volume_handle,
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302079 &sas_device->volume_wwid)) {
2080 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2081 "failure at %s:%d/%s()!\n", ioc->name,
2082 __FILE__, __LINE__, __func__));
2083 return 1;
2084 }
Eric Moore635374e2009-03-09 01:21:12 -06002085 }
2086 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2087 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2088 ssp_target = 1;
2089 ds = "SSP";
2090 } else {
2091 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2092 if (sas_device->device_info &
2093 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2094 ds = "STP";
2095 else if (sas_device->device_info &
2096 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2097 ds = "SATA";
2098 }
2099
2100 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302101 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002102 ds, sas_device->handle,
2103 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302104 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06002105 (unsigned long long)sas_device->device_name);
2106 sdev_printk(KERN_INFO, sdev, "%s: "
2107 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2108 (unsigned long long) sas_device->enclosure_logical_id,
2109 sas_device->slot);
2110
2111 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06002112 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302113 } else {
2114 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2115 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
2116 __func__));
2117 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002118 }
2119
Mike Christiee881a172009-10-15 17:46:39 -07002120 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002121
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302122 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002123 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302124 _scsih_enable_tlr(ioc, sdev);
2125 }
Eric Moore635374e2009-03-09 01:21:12 -06002126 return 0;
2127}
2128
2129/**
Eric Moored5d135b2009-05-18 13:02:08 -06002130 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002131 * @sdev: scsi device struct
2132 * @bdev: pointer to block device context
2133 * @capacity: device size (in 512 byte sectors)
2134 * @params: three element array to place output:
2135 * params[0] number of heads (max 255)
2136 * params[1] number of sectors (max 63)
2137 * params[2] number of cylinders
2138 *
2139 * Return nothing.
2140 */
2141static int
Eric Moored5d135b2009-05-18 13:02:08 -06002142_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002143 sector_t capacity, int params[])
2144{
2145 int heads;
2146 int sectors;
2147 sector_t cylinders;
2148 ulong dummy;
2149
2150 heads = 64;
2151 sectors = 32;
2152
2153 dummy = heads * sectors;
2154 cylinders = capacity;
2155 sector_div(cylinders, dummy);
2156
2157 /*
2158 * Handle extended translation size for logical drives
2159 * > 1Gb
2160 */
2161 if ((ulong)capacity >= 0x200000) {
2162 heads = 255;
2163 sectors = 63;
2164 dummy = heads * sectors;
2165 cylinders = capacity;
2166 sector_div(cylinders, dummy);
2167 }
2168
2169 /* return result */
2170 params[0] = heads;
2171 params[1] = sectors;
2172 params[2] = cylinders;
2173
2174 return 0;
2175}
2176
2177/**
2178 * _scsih_response_code - translation of device response code
2179 * @ioc: per adapter object
2180 * @response_code: response code returned by the device
2181 *
2182 * Return nothing.
2183 */
2184static void
2185_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2186{
2187 char *desc;
2188
2189 switch (response_code) {
2190 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2191 desc = "task management request completed";
2192 break;
2193 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2194 desc = "invalid frame";
2195 break;
2196 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2197 desc = "task management request not supported";
2198 break;
2199 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2200 desc = "task management request failed";
2201 break;
2202 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2203 desc = "task management request succeeded";
2204 break;
2205 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2206 desc = "invalid lun";
2207 break;
2208 case 0xA:
2209 desc = "overlapped tag attempted";
2210 break;
2211 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2212 desc = "task queued, however not sent to target";
2213 break;
2214 default:
2215 desc = "unknown";
2216 break;
2217 }
2218 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2219 ioc->name, response_code, desc);
2220}
2221
2222/**
Eric Moored5d135b2009-05-18 13:02:08 -06002223 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002224 * @ioc: per adapter object
2225 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302226 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002227 * @reply: reply message frame(lower 32bit addr)
2228 * Context: none.
2229 *
2230 * The callback handler when using scsih_issue_tm.
2231 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302232 * Return 1 meaning mf should be freed from _base_interrupt
2233 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002234 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302235static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302236_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002237{
2238 MPI2DefaultReply_t *mpi_reply;
2239
2240 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302241 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002242 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302243 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302244 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002245 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2246 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2247 if (mpi_reply) {
2248 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2249 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2250 }
2251 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2252 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302253 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002254}
2255
2256/**
2257 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2258 * @ioc: per adapter object
2259 * @handle: device handle
2260 *
2261 * During taskmangement request, we need to freeze the device queue.
2262 */
2263void
2264mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2265{
2266 struct MPT2SAS_DEVICE *sas_device_priv_data;
2267 struct scsi_device *sdev;
2268 u8 skip = 0;
2269
2270 shost_for_each_device(sdev, ioc->shost) {
2271 if (skip)
2272 continue;
2273 sas_device_priv_data = sdev->hostdata;
2274 if (!sas_device_priv_data)
2275 continue;
2276 if (sas_device_priv_data->sas_target->handle == handle) {
2277 sas_device_priv_data->sas_target->tm_busy = 1;
2278 skip = 1;
2279 ioc->ignore_loginfos = 1;
2280 }
2281 }
2282}
2283
2284/**
2285 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2286 * @ioc: per adapter object
2287 * @handle: device handle
2288 *
2289 * During taskmangement request, we need to freeze the device queue.
2290 */
2291void
2292mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2293{
2294 struct MPT2SAS_DEVICE *sas_device_priv_data;
2295 struct scsi_device *sdev;
2296 u8 skip = 0;
2297
2298 shost_for_each_device(sdev, ioc->shost) {
2299 if (skip)
2300 continue;
2301 sas_device_priv_data = sdev->hostdata;
2302 if (!sas_device_priv_data)
2303 continue;
2304 if (sas_device_priv_data->sas_target->handle == handle) {
2305 sas_device_priv_data->sas_target->tm_busy = 0;
2306 skip = 1;
2307 ioc->ignore_loginfos = 0;
2308 }
2309 }
2310}
2311
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302312
Eric Moore635374e2009-03-09 01:21:12 -06002313/**
2314 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2315 * @ioc: per adapter struct
2316 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302317 * @channel: the channel assigned by the OS
2318 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002319 * @lun: lun number
2320 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2321 * @smid_task: smid assigned to the task
2322 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302323 * @serial_number: the serial_number from scmd
2324 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302325 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002326 *
2327 * A generic API for sending task management requests to firmware.
2328 *
Eric Moore635374e2009-03-09 01:21:12 -06002329 * The callback index is set inside `ioc->tm_cb_idx`.
2330 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302331 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002332 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302333int
2334mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2335 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302336 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002337{
2338 Mpi2SCSITaskManagementRequest_t *mpi_request;
2339 Mpi2SCSITaskManagementReply_t *mpi_reply;
2340 u16 smid = 0;
2341 u32 ioc_state;
2342 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302343 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302344 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002345
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302346 if (m_type == TM_MUTEX_ON)
2347 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302348 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2349 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2350 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302351 rc = FAILED;
2352 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302353 }
2354
Eric Moore3cb54692010-07-08 14:44:34 -06002355 if (ioc->shost_recovery || ioc->remove_host ||
2356 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002357 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2358 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302359 rc = FAILED;
2360 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002361 }
Eric Moore635374e2009-03-09 01:21:12 -06002362
2363 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2364 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302365 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002366 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302367 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302368 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302369 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302370 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002371 }
2372
2373 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2374 mpt2sas_base_fault_info(ioc, ioc_state &
2375 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302376 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302377 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302378 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302379 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002380 }
2381
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302382 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002383 if (!smid) {
2384 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2385 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302386 rc = FAILED;
2387 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002388 }
2389
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302390 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2391 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2392
Eric Moore635374e2009-03-09 01:21:12 -06002393 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302394 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2395 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002396 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2397 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2398 ioc->tm_cmds.smid = smid;
2399 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302400 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002401 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2402 mpi_request->DevHandle = cpu_to_le16(handle);
2403 mpi_request->TaskType = type;
2404 mpi_request->TaskMID = cpu_to_le16(smid_task);
2405 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2406 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302407 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302408 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002409 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002410 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2411 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2412 ioc->name, __func__);
2413 _debug_dump_mf(mpi_request,
2414 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302415 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302416 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302417 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302418 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302419 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2420 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2421 goto err_out;
2422 }
Eric Moore635374e2009-03-09 01:21:12 -06002423 }
2424
2425 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2426 mpi_reply = ioc->tm_cmds.reply;
2427 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2428 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2429 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2430 le32_to_cpu(mpi_reply->IOCLogInfo),
2431 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302432 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002433 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302434 if (mpi_reply->IOCStatus)
2435 _debug_dump_mf(mpi_request,
2436 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2437 }
Eric Moore635374e2009-03-09 01:21:12 -06002438 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302439
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302440 switch (type) {
2441 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302442 rc = SUCCESS;
2443 if (scsi_lookup->scmd == NULL)
2444 break;
2445 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302446 break;
2447
2448 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2449 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2450 rc = FAILED;
2451 else
2452 rc = SUCCESS;
2453 break;
2454
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302455 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302456 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2457 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2458 rc = FAILED;
2459 else
2460 rc = SUCCESS;
2461 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302462 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2463 rc = SUCCESS;
2464 break;
2465 default:
2466 rc = FAILED;
2467 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302468 }
2469
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302470 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2471 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302472 if (m_type == TM_MUTEX_ON)
2473 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302474
2475 return rc;
2476
2477 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302478 if (m_type == TM_MUTEX_ON)
2479 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302480 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002481}
2482
2483/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302484 * _scsih_tm_display_info - displays info about the device
2485 * @ioc: per adapter struct
2486 * @scmd: pointer to scsi command object
2487 *
2488 * Called by task management callback handlers.
2489 */
2490static void
2491_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2492{
2493 struct scsi_target *starget = scmd->device->sdev_target;
2494 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2495 struct _sas_device *sas_device = NULL;
2496 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302497 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302498
2499 if (!priv_target)
2500 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302501 if (ioc->hide_ir_msg)
2502 device_str = "WarpDrive";
2503 else
2504 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302505
2506 scsi_print_command(scmd);
2507 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302508 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2509 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2510 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302511 } else {
2512 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2513 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2514 priv_target->sas_address);
2515 if (sas_device) {
2516 if (priv_target->flags &
2517 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2518 starget_printk(KERN_INFO, starget,
2519 "volume handle(0x%04x), "
2520 "volume wwid(0x%016llx)\n",
2521 sas_device->volume_handle,
2522 (unsigned long long)sas_device->volume_wwid);
2523 }
2524 starget_printk(KERN_INFO, starget,
2525 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2526 sas_device->handle,
2527 (unsigned long long)sas_device->sas_address,
2528 sas_device->phy);
2529 starget_printk(KERN_INFO, starget,
2530 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2531 (unsigned long long)sas_device->enclosure_logical_id,
2532 sas_device->slot);
2533 }
2534 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2535 }
2536}
2537
2538/**
Eric Moored5d135b2009-05-18 13:02:08 -06002539 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302540 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002541 *
2542 * Returns SUCCESS if command aborted else FAILED
2543 */
2544static int
Eric Moored5d135b2009-05-18 13:02:08 -06002545_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002546{
2547 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2548 struct MPT2SAS_DEVICE *sas_device_priv_data;
2549 u16 smid;
2550 u16 handle;
2551 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002552
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302553 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2554 "scmd(%p)\n", scmd);
2555 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002556
2557 sas_device_priv_data = scmd->device->hostdata;
2558 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302559 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2560 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002561 scmd->result = DID_NO_CONNECT << 16;
2562 scmd->scsi_done(scmd);
2563 r = SUCCESS;
2564 goto out;
2565 }
2566
2567 /* search for the command */
2568 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2569 if (!smid) {
2570 scmd->result = DID_RESET << 16;
2571 r = SUCCESS;
2572 goto out;
2573 }
2574
2575 /* for hidden raid components and volumes this is not supported */
2576 if (sas_device_priv_data->sas_target->flags &
2577 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2578 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2579 scmd->result = DID_RESET << 16;
2580 r = FAILED;
2581 goto out;
2582 }
2583
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302584 mpt2sas_halt_firmware(ioc);
2585
Eric Moore635374e2009-03-09 01:21:12 -06002586 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302587 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2588 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302589 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2590 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002591
2592 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302593 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2594 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002595 return r;
2596}
2597
Eric Moore635374e2009-03-09 01:21:12 -06002598/**
Eric Moored5d135b2009-05-18 13:02:08 -06002599 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302600 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002601 *
2602 * Returns SUCCESS if command aborted else FAILED
2603 */
2604static int
Eric Moored5d135b2009-05-18 13:02:08 -06002605_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002606{
2607 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2608 struct MPT2SAS_DEVICE *sas_device_priv_data;
2609 struct _sas_device *sas_device;
2610 unsigned long flags;
2611 u16 handle;
2612 int r;
2613
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302614 struct scsi_target *starget = scmd->device->sdev_target;
2615
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302616 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302617 "scmd(%p)\n", scmd);
2618 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002619
2620 sas_device_priv_data = scmd->device->hostdata;
2621 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302622 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302623 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002624 scmd->result = DID_NO_CONNECT << 16;
2625 scmd->scsi_done(scmd);
2626 r = SUCCESS;
2627 goto out;
2628 }
2629
2630 /* for hidden raid components obtain the volume_handle */
2631 handle = 0;
2632 if (sas_device_priv_data->sas_target->flags &
2633 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2634 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2635 sas_device = _scsih_sas_device_find_by_handle(ioc,
2636 sas_device_priv_data->sas_target->handle);
2637 if (sas_device)
2638 handle = sas_device->volume_handle;
2639 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2640 } else
2641 handle = sas_device_priv_data->sas_target->handle;
2642
2643 if (!handle) {
2644 scmd->result = DID_RESET << 16;
2645 r = FAILED;
2646 goto out;
2647 }
2648
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302649 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2650 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302651 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2652 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002653
2654 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302655 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2656 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002657 return r;
2658}
2659
2660/**
Eric Moored5d135b2009-05-18 13:02:08 -06002661 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302662 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002663 *
2664 * Returns SUCCESS if command aborted else FAILED
2665 */
2666static int
Eric Moored5d135b2009-05-18 13:02:08 -06002667_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002668{
2669 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2670 struct MPT2SAS_DEVICE *sas_device_priv_data;
2671 struct _sas_device *sas_device;
2672 unsigned long flags;
2673 u16 handle;
2674 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302675 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002676
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302677 starget_printk(KERN_INFO, starget, "attempting target reset! "
2678 "scmd(%p)\n", scmd);
2679 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002680
2681 sas_device_priv_data = scmd->device->hostdata;
2682 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302683 starget_printk(KERN_INFO, starget, "target been deleted! "
2684 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002685 scmd->result = DID_NO_CONNECT << 16;
2686 scmd->scsi_done(scmd);
2687 r = SUCCESS;
2688 goto out;
2689 }
2690
2691 /* for hidden raid components obtain the volume_handle */
2692 handle = 0;
2693 if (sas_device_priv_data->sas_target->flags &
2694 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2695 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2696 sas_device = _scsih_sas_device_find_by_handle(ioc,
2697 sas_device_priv_data->sas_target->handle);
2698 if (sas_device)
2699 handle = sas_device->volume_handle;
2700 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2701 } else
2702 handle = sas_device_priv_data->sas_target->handle;
2703
2704 if (!handle) {
2705 scmd->result = DID_RESET << 16;
2706 r = FAILED;
2707 goto out;
2708 }
2709
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302710 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2711 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302712 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002713
2714 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302715 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2716 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002717 return r;
2718}
2719
2720/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302721 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302722 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002723 *
2724 * Returns SUCCESS if command aborted else FAILED
2725 */
2726static int
Eric Moored5d135b2009-05-18 13:02:08 -06002727_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002728{
2729 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2730 int r, retval;
2731
2732 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2733 ioc->name, scmd);
2734 scsi_print_command(scmd);
2735
2736 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2737 FORCE_BIG_HAMMER);
2738 r = (retval < 0) ? FAILED : SUCCESS;
2739 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2740 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2741
2742 return r;
2743}
2744
2745/**
2746 * _scsih_fw_event_add - insert and queue up fw_event
2747 * @ioc: per adapter object
2748 * @fw_event: object describing the event
2749 * Context: This function will acquire ioc->fw_event_lock.
2750 *
2751 * This adds the firmware event object into link list, then queues it up to
2752 * be processed from user context.
2753 *
2754 * Return nothing.
2755 */
2756static void
2757_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2758{
2759 unsigned long flags;
2760
2761 if (ioc->firmware_event_thread == NULL)
2762 return;
2763
2764 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2765 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302766 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2767 queue_delayed_work(ioc->firmware_event_thread,
2768 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002769 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2770}
2771
2772/**
2773 * _scsih_fw_event_free - delete fw_event
2774 * @ioc: per adapter object
2775 * @fw_event: object describing the event
2776 * Context: This function will acquire ioc->fw_event_lock.
2777 *
2778 * This removes firmware event object from link list, frees associated memory.
2779 *
2780 * Return nothing.
2781 */
2782static void
2783_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2784 *fw_event)
2785{
2786 unsigned long flags;
2787
2788 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2789 list_del(&fw_event->list);
2790 kfree(fw_event->event_data);
2791 kfree(fw_event);
2792 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2793}
2794
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302795
Eric Moore635374e2009-03-09 01:21:12 -06002796/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302797 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002798 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002799 *
2800 * Return nothing.
2801 */
2802static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302803_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002804{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302805 struct fw_event_work *fw_event;
2806
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302807 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302808 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002809
2810 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2811 if (!fw_event)
2812 return;
2813
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302814 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2815 fw_event->ioc = ioc;
2816 _scsih_fw_event_add(ioc, fw_event);
2817}
2818
2819/**
2820 * mpt2sas_port_enable_complete - port enable completed (fake event)
2821 * @ioc: per adapter object
2822 *
2823 * Return nothing.
2824 */
2825void
2826mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2827{
2828 struct fw_event_work *fw_event;
2829
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302830 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2831 if (!fw_event)
2832 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302833 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302834 fw_event->ioc = ioc;
2835 _scsih_fw_event_add(ioc, fw_event);
2836}
2837
2838/**
2839 * _scsih_fw_event_cleanup_queue - cleanup event queue
2840 * @ioc: per adapter object
2841 *
2842 * Walk the firmware event queue, either killing timers, or waiting
2843 * for outstanding events to complete
2844 *
2845 * Return nothing.
2846 */
2847static void
2848_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2849{
2850 struct fw_event_work *fw_event, *next;
2851
2852 if (list_empty(&ioc->fw_event_list) ||
2853 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002854 return;
2855
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302856 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2857 if (cancel_delayed_work(&fw_event->delayed_work)) {
2858 _scsih_fw_event_free(ioc, fw_event);
2859 continue;
2860 }
2861 fw_event->cancel_pending_work = 1;
2862 }
Eric Moore635374e2009-03-09 01:21:12 -06002863}
2864
Eric Moore635374e2009-03-09 01:21:12 -06002865/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302866 * _scsih_ublock_io_all_device - unblock every device
2867 * @ioc: per adapter object
2868 *
2869 * change the device state from block to running
2870 */
2871static void
2872_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2873{
2874 struct MPT2SAS_DEVICE *sas_device_priv_data;
2875 struct scsi_device *sdev;
2876
2877 shost_for_each_device(sdev, ioc->shost) {
2878 sas_device_priv_data = sdev->hostdata;
2879 if (!sas_device_priv_data)
2880 continue;
2881 if (!sas_device_priv_data->block)
2882 continue;
2883 sas_device_priv_data->block = 0;
2884 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2885 "handle(0x%04x)\n",
2886 sas_device_priv_data->sas_target->handle));
2887 scsi_internal_device_unblock(sdev);
2888 }
2889}
2890/**
Eric Moore635374e2009-03-09 01:21:12 -06002891 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2892 * @ioc: per adapter object
2893 * @handle: device handle
2894 *
2895 * During device pull we need to appropiately set the sdev state.
2896 */
2897static void
2898_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2899{
2900 struct MPT2SAS_DEVICE *sas_device_priv_data;
2901 struct scsi_device *sdev;
2902
2903 shost_for_each_device(sdev, ioc->shost) {
2904 sas_device_priv_data = sdev->hostdata;
2905 if (!sas_device_priv_data)
2906 continue;
2907 if (!sas_device_priv_data->block)
2908 continue;
2909 if (sas_device_priv_data->sas_target->handle == handle) {
2910 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2911 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2912 "handle(0x%04x)\n", ioc->name, handle));
2913 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302914 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002915 }
2916 }
2917}
2918
2919/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302920 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2921 * @ioc: per adapter object
2922 * @handle: device handle
2923 *
2924 * During device pull we need to appropiately set the sdev state.
2925 */
2926static void
2927_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2928{
2929 struct MPT2SAS_DEVICE *sas_device_priv_data;
2930 struct scsi_device *sdev;
2931
2932 shost_for_each_device(sdev, ioc->shost) {
2933 sas_device_priv_data = sdev->hostdata;
2934 if (!sas_device_priv_data)
2935 continue;
2936 if (sas_device_priv_data->block)
2937 continue;
2938 sas_device_priv_data->block = 1;
2939 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2940 "handle(0x%04x)\n",
2941 sas_device_priv_data->sas_target->handle));
2942 scsi_internal_device_block(sdev);
2943 }
2944}
2945
2946
2947/**
Eric Moore635374e2009-03-09 01:21:12 -06002948 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2949 * @ioc: per adapter object
2950 * @handle: device handle
2951 *
2952 * During device pull we need to appropiately set the sdev state.
2953 */
2954static void
2955_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2956{
2957 struct MPT2SAS_DEVICE *sas_device_priv_data;
2958 struct scsi_device *sdev;
2959
2960 shost_for_each_device(sdev, ioc->shost) {
2961 sas_device_priv_data = sdev->hostdata;
2962 if (!sas_device_priv_data)
2963 continue;
2964 if (sas_device_priv_data->block)
2965 continue;
2966 if (sas_device_priv_data->sas_target->handle == handle) {
2967 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2968 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2969 "handle(0x%04x)\n", ioc->name, handle));
2970 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302971 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002972 }
2973 }
2974}
2975
2976/**
2977 * _scsih_block_io_to_children_attached_to_ex
2978 * @ioc: per adapter object
2979 * @sas_expander: the sas_device object
2980 *
2981 * This routine set sdev state to SDEV_BLOCK for all devices
2982 * attached to this expander. This function called when expander is
2983 * pulled.
2984 */
2985static void
2986_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2987 struct _sas_node *sas_expander)
2988{
2989 struct _sas_port *mpt2sas_port;
2990 struct _sas_device *sas_device;
2991 struct _sas_node *expander_sibling;
2992 unsigned long flags;
2993
2994 if (!sas_expander)
2995 return;
2996
2997 list_for_each_entry(mpt2sas_port,
2998 &sas_expander->sas_port_list, port_list) {
2999 if (mpt2sas_port->remote_identify.device_type ==
3000 SAS_END_DEVICE) {
3001 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3002 sas_device =
3003 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3004 mpt2sas_port->remote_identify.sas_address);
3005 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3006 if (!sas_device)
3007 continue;
3008 _scsih_block_io_device(ioc, sas_device->handle);
3009 }
3010 }
3011
3012 list_for_each_entry(mpt2sas_port,
3013 &sas_expander->sas_port_list, port_list) {
3014
3015 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303016 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003017 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303018 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003019
3020 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3021 expander_sibling =
3022 mpt2sas_scsih_expander_find_by_sas_address(
3023 ioc, mpt2sas_port->remote_identify.sas_address);
3024 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3025 _scsih_block_io_to_children_attached_to_ex(ioc,
3026 expander_sibling);
3027 }
3028 }
3029}
3030
3031/**
3032 * _scsih_block_io_to_children_attached_directly
3033 * @ioc: per adapter object
3034 * @event_data: topology change event data
3035 *
3036 * This routine set sdev state to SDEV_BLOCK for all devices
3037 * direct attached during device pull.
3038 */
3039static void
3040_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3041 Mpi2EventDataSasTopologyChangeList_t *event_data)
3042{
3043 int i;
3044 u16 handle;
3045 u16 reason_code;
3046 u8 phy_number;
3047
3048 for (i = 0; i < event_data->NumEntries; i++) {
3049 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3050 if (!handle)
3051 continue;
3052 phy_number = event_data->StartPhyNum + i;
3053 reason_code = event_data->PHY[i].PhyStatus &
3054 MPI2_EVENT_SAS_TOPO_RC_MASK;
3055 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3056 _scsih_block_io_device(ioc, handle);
3057 }
3058}
3059
3060/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303061 * _scsih_tm_tr_send - send task management request
3062 * @ioc: per adapter object
3063 * @handle: device handle
3064 * Context: interrupt time.
3065 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003066 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303067 * with controller firmware. This function will issue target reset
3068 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003069 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303070 *
3071 * This is designed to send muliple task management request at the same
3072 * time to the fifo. If the fifo is full, we will append the request,
3073 * and process it in a future completion.
3074 */
3075static void
3076_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3077{
3078 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303079 u16 smid;
3080 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303081 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3082 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303083 unsigned long flags;
3084 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303085 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303086
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303087 if (ioc->remove_host) {
3088 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3089 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3090 return;
3091 } else if (ioc->pci_error_recovery) {
3092 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3093 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3094 handle));
3095 return;
3096 }
3097 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3098 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3099 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3100 "operational: handle(0x%04x)\n", __func__, ioc->name,
3101 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303102 return;
3103 }
3104
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303105 /* if PD, then return */
3106 if (test_bit(handle, ioc->pd_handles))
3107 return;
3108
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303109 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3110 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303111 if (sas_device && sas_device->starget &&
3112 sas_device->starget->hostdata) {
3113 sas_target_priv_data = sas_device->starget->hostdata;
3114 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303115 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303116 }
3117 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303118
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303119 if (sas_target_priv_data) {
3120 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3121 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3122 (unsigned long long)sas_address));
3123 _scsih_ublock_io_device(ioc, handle);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303124 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303125 }
3126
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303127 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3128 if (!smid) {
3129 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3130 if (!delayed_tr)
3131 return;
3132 INIT_LIST_HEAD(&delayed_tr->list);
3133 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303134 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3135 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3136 "DELAYED:tr:handle(0x%04x), (open)\n",
3137 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303138 return;
3139 }
3140
Kashyap, Desai1278b112010-03-09 17:34:13 +05303141 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3142 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3143 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303144 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3145 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3146 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3147 mpi_request->DevHandle = cpu_to_le16(handle);
3148 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303149 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3150}
3151
3152
3153
3154/**
3155 * _scsih_sas_control_complete - completion routine
3156 * @ioc: per adapter object
3157 * @smid: system request message index
3158 * @msix_index: MSIX table index supplied by the OS
3159 * @reply: reply message frame(lower 32bit addr)
3160 * Context: interrupt time.
3161 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003162 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303163 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003164 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303165 *
3166 * Return 1 meaning mf should be freed from _base_interrupt
3167 * 0 means the mf is freed from this function.
3168 */
3169static u8
3170_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3171 u8 msix_index, u32 reply)
3172{
Kashyap, Desai363fa502010-11-13 04:29:20 +05303173#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303174 Mpi2SasIoUnitControlReply_t *mpi_reply =
3175 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa502010-11-13 04:29:20 +05303176#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05303177 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3178 "sc_complete:handle(0x%04x), (open) "
3179 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3180 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3181 le16_to_cpu(mpi_reply->IOCStatus),
3182 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303183 return 1;
3184}
3185
3186/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303187 * _scsih_tm_tr_volume_send - send target reset request for volumes
3188 * @ioc: per adapter object
3189 * @handle: device handle
3190 * Context: interrupt time.
3191 *
3192 * This is designed to send muliple task management request at the same
3193 * time to the fifo. If the fifo is full, we will append the request,
3194 * and process it in a future completion.
3195 */
3196static void
3197_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3198{
3199 Mpi2SCSITaskManagementRequest_t *mpi_request;
3200 u16 smid;
3201 struct _tr_list *delayed_tr;
3202
Eric Moore3cb54692010-07-08 14:44:34 -06003203 if (ioc->shost_recovery || ioc->remove_host ||
3204 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303205 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3206 "progress!\n", __func__, ioc->name));
3207 return;
3208 }
3209
3210 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3211 if (!smid) {
3212 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3213 if (!delayed_tr)
3214 return;
3215 INIT_LIST_HEAD(&delayed_tr->list);
3216 delayed_tr->handle = handle;
3217 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3218 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3219 "DELAYED:tr:handle(0x%04x), (open)\n",
3220 ioc->name, handle));
3221 return;
3222 }
3223
3224 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3225 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3226 ioc->tm_tr_volume_cb_idx));
3227 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3228 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3229 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3230 mpi_request->DevHandle = cpu_to_le16(handle);
3231 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3232 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3233}
3234
3235/**
3236 * _scsih_tm_volume_tr_complete - target reset completion
3237 * @ioc: per adapter object
3238 * @smid: system request message index
3239 * @msix_index: MSIX table index supplied by the OS
3240 * @reply: reply message frame(lower 32bit addr)
3241 * Context: interrupt time.
3242 *
3243 * Return 1 meaning mf should be freed from _base_interrupt
3244 * 0 means the mf is freed from this function.
3245 */
3246static u8
3247_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3248 u8 msix_index, u32 reply)
3249{
3250 u16 handle;
3251 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3252 Mpi2SCSITaskManagementReply_t *mpi_reply =
3253 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3254
Eric Moore3cb54692010-07-08 14:44:34 -06003255 if (ioc->shost_recovery || ioc->remove_host ||
3256 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303257 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3258 "progress!\n", __func__, ioc->name));
3259 return 1;
3260 }
3261
3262 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3263 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3264 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3265 dewtprintk(ioc, printk("spurious interrupt: "
3266 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3267 le16_to_cpu(mpi_reply->DevHandle), smid));
3268 return 0;
3269 }
3270
3271 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3272 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3273 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3274 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3275 le32_to_cpu(mpi_reply->IOCLogInfo),
3276 le32_to_cpu(mpi_reply->TerminationCount)));
3277
3278 return _scsih_check_for_pending_tm(ioc, smid);
3279}
3280
3281/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303282 * _scsih_tm_tr_complete -
3283 * @ioc: per adapter object
3284 * @smid: system request message index
3285 * @msix_index: MSIX table index supplied by the OS
3286 * @reply: reply message frame(lower 32bit addr)
3287 * Context: interrupt time.
3288 *
3289 * This is the target reset completion routine.
3290 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003291 * handshake protocol with controller firmware.
3292 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303293 *
3294 * Return 1 meaning mf should be freed from _base_interrupt
3295 * 0 means the mf is freed from this function.
3296 */
3297static u8
3298_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3299 u32 reply)
3300{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303301 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303302 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303303 Mpi2SCSITaskManagementReply_t *mpi_reply =
3304 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3305 Mpi2SasIoUnitControlRequest_t *mpi_request;
3306 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303307 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303308
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303309 if (ioc->remove_host) {
3310 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3311 "removed\n", __func__, ioc->name));
3312 return 1;
3313 } else if (ioc->pci_error_recovery) {
3314 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3315 "error recovery\n", __func__, ioc->name));
3316 return 1;
3317 }
3318 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3319 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3320 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3321 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303322 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303323 }
3324
Kashyap, Desai1278b112010-03-09 17:34:13 +05303325 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3326 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3327 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3328 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3329 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3330 le16_to_cpu(mpi_reply->DevHandle), smid));
3331 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303332 }
3333
Kashyap, Desai1278b112010-03-09 17:34:13 +05303334 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3335 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3336 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3337 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3338 le32_to_cpu(mpi_reply->IOCLogInfo),
3339 le32_to_cpu(mpi_reply->TerminationCount)));
3340
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303341 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3342 if (!smid_sas_ctrl) {
3343 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3344 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303345 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303346 }
3347
Kashyap, Desai1278b112010-03-09 17:34:13 +05303348 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3349 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3350 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303351 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3352 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3353 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3354 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303355 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303356 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303357
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303358 return _scsih_check_for_pending_tm(ioc, smid);
3359}
3360
3361/**
3362 * _scsih_check_for_pending_tm - check for pending task management
3363 * @ioc: per adapter object
3364 * @smid: system request message index
3365 *
3366 * This will check delayed target reset list, and feed the
3367 * next reqeust.
3368 *
3369 * Return 1 meaning mf should be freed from _base_interrupt
3370 * 0 means the mf is freed from this function.
3371 */
3372static u8
3373_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3374{
3375 struct _tr_list *delayed_tr;
3376
3377 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3378 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3379 struct _tr_list, list);
3380 mpt2sas_base_free_smid(ioc, smid);
3381 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3382 list_del(&delayed_tr->list);
3383 kfree(delayed_tr);
3384 return 0;
3385 }
3386
Kashyap, Desai1278b112010-03-09 17:34:13 +05303387 if (!list_empty(&ioc->delayed_tr_list)) {
3388 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3389 struct _tr_list, list);
3390 mpt2sas_base_free_smid(ioc, smid);
3391 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3392 list_del(&delayed_tr->list);
3393 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303394 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303395 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303396
Kashyap, Desai1278b112010-03-09 17:34:13 +05303397 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303398}
3399
3400/**
Eric Moore635374e2009-03-09 01:21:12 -06003401 * _scsih_check_topo_delete_events - sanity check on topo events
3402 * @ioc: per adapter object
3403 * @event_data: the event data payload
3404 *
3405 * This routine added to better handle cable breaker.
3406 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003407 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003408 * add and delete events in a single shot. When there is a delete event
3409 * the routine will void any pending add events waiting in the event queue.
3410 *
3411 * Return nothing.
3412 */
3413static void
3414_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3415 Mpi2EventDataSasTopologyChangeList_t *event_data)
3416{
3417 struct fw_event_work *fw_event;
3418 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3419 u16 expander_handle;
3420 struct _sas_node *sas_expander;
3421 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303422 int i, reason_code;
3423 u16 handle;
3424
3425 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303426 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3427 if (!handle)
3428 continue;
3429 reason_code = event_data->PHY[i].PhyStatus &
3430 MPI2_EVENT_SAS_TOPO_RC_MASK;
3431 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3432 _scsih_tm_tr_send(ioc, handle);
3433 }
Eric Moore635374e2009-03-09 01:21:12 -06003434
3435 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3436 if (expander_handle < ioc->sas_hba.num_phys) {
3437 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3438 return;
3439 }
3440
3441 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3442 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3443 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3444 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3445 expander_handle);
3446 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3447 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3448 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3449 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3450
3451 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3452 return;
3453
3454 /* mark ignore flag for pending events */
3455 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3456 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3457 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3458 fw_event->ignore)
3459 continue;
3460 local_event_data = fw_event->event_data;
3461 if (local_event_data->ExpStatus ==
3462 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3463 local_event_data->ExpStatus ==
3464 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3465 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3466 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303467 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003468 "setting ignoring flag\n", ioc->name));
3469 fw_event->ignore = 1;
3470 }
3471 }
3472 }
3473 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3474}
3475
3476/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303477 * _scsih_set_volume_delete_flag - setting volume delete flag
3478 * @ioc: per adapter object
3479 * @handle: device handle
3480 *
3481 * This
3482 * Return nothing.
3483 */
3484static void
3485_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3486{
3487 struct _raid_device *raid_device;
3488 struct MPT2SAS_TARGET *sas_target_priv_data;
3489 unsigned long flags;
3490
3491 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3492 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3493 if (raid_device && raid_device->starget &&
3494 raid_device->starget->hostdata) {
3495 sas_target_priv_data =
3496 raid_device->starget->hostdata;
3497 sas_target_priv_data->deleted = 1;
3498 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3499 "setting delete flag: handle(0x%04x), "
3500 "wwid(0x%016llx)\n", ioc->name, handle,
3501 (unsigned long long) raid_device->wwid));
3502 }
3503 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3504}
3505
3506/**
3507 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3508 * @handle: input handle
3509 * @a: handle for volume a
3510 * @b: handle for volume b
3511 *
3512 * IR firmware only supports two raid volumes. The purpose of this
3513 * routine is to set the volume handle in either a or b. When the given
3514 * input handle is non-zero, or when a and b have not been set before.
3515 */
3516static void
3517_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3518{
3519 if (!handle || handle == *a || handle == *b)
3520 return;
3521 if (!*a)
3522 *a = handle;
3523 else if (!*b)
3524 *b = handle;
3525}
3526
3527/**
3528 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3529 * @ioc: per adapter object
3530 * @event_data: the event data payload
3531 * Context: interrupt time.
3532 *
3533 * This routine will send target reset to volume, followed by target
3534 * resets to the PDs. This is called when a PD has been removed, or
3535 * volume has been deleted or removed. When the target reset is sent
3536 * to volume, the PD target resets need to be queued to start upon
3537 * completion of the volume target reset.
3538 *
3539 * Return nothing.
3540 */
3541static void
3542_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3543 Mpi2EventDataIrConfigChangeList_t *event_data)
3544{
3545 Mpi2EventIrConfigElement_t *element;
3546 int i;
3547 u16 handle, volume_handle, a, b;
3548 struct _tr_list *delayed_tr;
3549
3550 a = 0;
3551 b = 0;
3552
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303553 if (ioc->is_warpdrive)
3554 return;
3555
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303556 /* Volume Resets for Deleted or Removed */
3557 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3558 for (i = 0; i < event_data->NumElements; i++, element++) {
3559 if (element->ReasonCode ==
3560 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3561 element->ReasonCode ==
3562 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3563 volume_handle = le16_to_cpu(element->VolDevHandle);
3564 _scsih_set_volume_delete_flag(ioc, volume_handle);
3565 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3566 }
3567 }
3568
3569 /* Volume Resets for UNHIDE events */
3570 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3571 for (i = 0; i < event_data->NumElements; i++, element++) {
3572 if (le32_to_cpu(event_data->Flags) &
3573 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3574 continue;
3575 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3576 volume_handle = le16_to_cpu(element->VolDevHandle);
3577 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3578 }
3579 }
3580
3581 if (a)
3582 _scsih_tm_tr_volume_send(ioc, a);
3583 if (b)
3584 _scsih_tm_tr_volume_send(ioc, b);
3585
3586 /* PD target resets */
3587 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3588 for (i = 0; i < event_data->NumElements; i++, element++) {
3589 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3590 continue;
3591 handle = le16_to_cpu(element->PhysDiskDevHandle);
3592 volume_handle = le16_to_cpu(element->VolDevHandle);
3593 clear_bit(handle, ioc->pd_handles);
3594 if (!volume_handle)
3595 _scsih_tm_tr_send(ioc, handle);
3596 else if (volume_handle == a || volume_handle == b) {
3597 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3598 BUG_ON(!delayed_tr);
3599 INIT_LIST_HEAD(&delayed_tr->list);
3600 delayed_tr->handle = handle;
3601 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3602 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3603 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3604 handle));
3605 } else
3606 _scsih_tm_tr_send(ioc, handle);
3607 }
3608}
3609
3610
3611/**
3612 * _scsih_check_volume_delete_events - set delete flag for volumes
3613 * @ioc: per adapter object
3614 * @event_data: the event data payload
3615 * Context: interrupt time.
3616 *
3617 * This will handle the case when the cable connected to entire volume is
3618 * pulled. We will take care of setting the deleted flag so normal IO will
3619 * not be sent.
3620 *
3621 * Return nothing.
3622 */
3623static void
3624_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3625 Mpi2EventDataIrVolume_t *event_data)
3626{
3627 u32 state;
3628
3629 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3630 return;
3631 state = le32_to_cpu(event_data->NewValue);
3632 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3633 MPI2_RAID_VOL_STATE_FAILED)
3634 _scsih_set_volume_delete_flag(ioc,
3635 le16_to_cpu(event_data->VolDevHandle));
3636}
3637
3638/**
Eric Moore635374e2009-03-09 01:21:12 -06003639 * _scsih_flush_running_cmds - completing outstanding commands.
3640 * @ioc: per adapter object
3641 *
3642 * The flushing out of all pending scmd commands following host reset,
3643 * where all IO is dropped to the floor.
3644 *
3645 * Return nothing.
3646 */
3647static void
3648_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3649{
3650 struct scsi_cmnd *scmd;
3651 u16 smid;
3652 u16 count = 0;
3653
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303654 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303655 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003656 if (!scmd)
3657 continue;
3658 count++;
3659 mpt2sas_base_free_smid(ioc, smid);
3660 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003661 if (ioc->pci_error_recovery)
3662 scmd->result = DID_NO_CONNECT << 16;
3663 else
3664 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003665 scmd->scsi_done(scmd);
3666 }
3667 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3668 ioc->name, count));
3669}
3670
3671/**
Eric Moore3c621b32009-05-18 12:59:41 -06003672 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3673 * @scmd: pointer to scsi command object
3674 * @mpi_request: pointer to the SCSI_IO reqest message frame
3675 *
3676 * Supporting protection 1 and 3.
3677 *
3678 * Returns nothing
3679 */
3680static void
3681_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3682{
3683 u16 eedp_flags;
3684 unsigned char prot_op = scsi_get_prot_op(scmd);
3685 unsigned char prot_type = scsi_get_prot_type(scmd);
3686
Eric Moored334aa72010-04-22 10:47:40 -06003687 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003688 return;
3689
3690 if (prot_op == SCSI_PROT_READ_STRIP)
3691 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3692 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3693 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3694 else
3695 return;
3696
Eric Moore3c621b32009-05-18 12:59:41 -06003697 switch (prot_type) {
3698 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003699 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003700
3701 /*
3702 * enable ref/guard checking
3703 * auto increment ref tag
3704 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303705 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003706 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3707 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3708 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3709 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003710 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003711
Eric Moore3c621b32009-05-18 12:59:41 -06003712 case SCSI_PROT_DIF_TYPE3:
3713
3714 /*
3715 * enable guard checking
3716 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303717 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003718 break;
3719 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303720 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3721 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003722}
3723
3724/**
3725 * _scsih_eedp_error_handling - return sense code for EEDP errors
3726 * @scmd: pointer to scsi command object
3727 * @ioc_status: ioc status
3728 *
3729 * Returns nothing
3730 */
3731static void
3732_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3733{
3734 u8 ascq;
3735 u8 sk;
3736 u8 host_byte;
3737
3738 switch (ioc_status) {
3739 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3740 ascq = 0x01;
3741 break;
3742 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3743 ascq = 0x02;
3744 break;
3745 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3746 ascq = 0x03;
3747 break;
3748 default:
3749 ascq = 0x00;
3750 break;
3751 }
3752
3753 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3754 sk = ILLEGAL_REQUEST;
3755 host_byte = DID_ABORT;
3756 } else {
3757 sk = ABORTED_COMMAND;
3758 host_byte = DID_OK;
3759 }
3760
3761 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3762 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3763 SAM_STAT_CHECK_CONDITION;
3764}
3765
3766/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303767 * _scsih_scsi_direct_io_get - returns direct io flag
3768 * @ioc: per adapter object
3769 * @smid: system request message index
3770 *
3771 * Returns the smid stored scmd pointer.
3772 */
3773static inline u8
3774_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3775{
3776 return ioc->scsi_lookup[smid - 1].direct_io;
3777}
3778
3779/**
3780 * _scsih_scsi_direct_io_set - sets direct io flag
3781 * @ioc: per adapter object
3782 * @smid: system request message index
3783 * @direct_io: Zero or non-zero value to set in the direct_io flag
3784 *
3785 * Returns Nothing.
3786 */
3787static inline void
3788_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3789{
3790 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3791}
3792
3793
3794/**
3795 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3796 * @ioc: per adapter object
3797 * @scmd: pointer to scsi command object
3798 * @raid_device: pointer to raid device data structure
3799 * @mpi_request: pointer to the SCSI_IO reqest message frame
3800 * @smid: system request message index
3801 *
3802 * Returns nothing
3803 */
3804static void
3805_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3806 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3807 u16 smid)
3808{
3809 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3810 u32 stripe_sz, stripe_exp;
3811 u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3812 u8 cdb0 = scmd->cmnd[0];
3813
3814 /*
3815 * Try Direct I/O to RAID memeber disks
3816 */
3817 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3818 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3819 cdb_ptr = mpi_request->CDB.CDB32;
3820
3821 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3822 | cdb_ptr[5])) {
3823 io_size = scsi_bufflen(scmd) >> 9;
3824 /* get virtual lba */
3825 lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3826 &cdb_ptr[6];
3827 tmp_ptr = (u8 *)&v_lba + 3;
3828 *tmp_ptr-- = *lba_ptr1++;
3829 *tmp_ptr-- = *lba_ptr1++;
3830 *tmp_ptr-- = *lba_ptr1++;
3831 *tmp_ptr = *lba_ptr1;
3832
3833 if (((u64)v_lba + (u64)io_size - 1) <=
3834 (u32)raid_device->max_lba) {
3835 stripe_sz = raid_device->stripe_sz;
3836 stripe_exp = raid_device->stripe_exponent;
3837 stripe_off = v_lba & (stripe_sz - 1);
3838
3839 /* Check whether IO falls within a stripe */
3840 if ((stripe_off + io_size) <= stripe_sz) {
3841 num_pds = raid_device->num_pds;
3842 p_lba = v_lba >> stripe_exp;
3843 stripe_unit = p_lba / num_pds;
3844 column = p_lba % num_pds;
3845 p_lba = (stripe_unit << stripe_exp) +
3846 stripe_off;
3847 mpi_request->DevHandle =
3848 cpu_to_le16(raid_device->
3849 pd_handle[column]);
3850 tmp_ptr = (u8 *)&p_lba + 3;
3851 *lba_ptr2++ = *tmp_ptr--;
3852 *lba_ptr2++ = *tmp_ptr--;
3853 *lba_ptr2++ = *tmp_ptr--;
3854 *lba_ptr2 = *tmp_ptr;
3855 /*
3856 * WD: To indicate this I/O is directI/O
3857 */
3858 _scsih_scsi_direct_io_set(ioc, smid, 1);
3859 }
3860 }
3861 }
3862 }
3863}
3864
3865/**
Eric Moored5d135b2009-05-18 13:02:08 -06003866 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003867 * @scmd: pointer to scsi command object
3868 * @done: function pointer to be invoked on completion
3869 *
3870 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3871 *
3872 * Returns 0 on success. If there's a failure, return either:
3873 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3874 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3875 */
3876static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003877_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003878{
3879 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3880 struct MPT2SAS_DEVICE *sas_device_priv_data;
3881 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303882 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003883 Mpi2SCSIIORequest_t *mpi_request;
3884 u32 mpi_control;
3885 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003886
3887 scmd->scsi_done = done;
3888 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303889 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003890 scmd->result = DID_NO_CONNECT << 16;
3891 scmd->scsi_done(scmd);
3892 return 0;
3893 }
3894
Kashyap, Desai78215782011-06-14 10:57:08 +05303895 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003896 scmd->result = DID_NO_CONNECT << 16;
3897 scmd->scsi_done(scmd);
3898 return 0;
3899 }
3900
Eric Moore635374e2009-03-09 01:21:12 -06003901 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303902 /* invalid device handle */
3903 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003904 scmd->result = DID_NO_CONNECT << 16;
3905 scmd->scsi_done(scmd);
3906 return 0;
3907 }
3908
Kashyap, Desai130b9582010-04-08 17:54:32 +05303909 /* host recovery or link resets sent via IOCTLs */
3910 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003911 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003912 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303913 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3914 return SCSI_MLQUEUE_DEVICE_BUSY;
3915 /* device has been deleted */
3916 else if (sas_target_priv_data->deleted) {
3917 scmd->result = DID_NO_CONNECT << 16;
3918 scmd->scsi_done(scmd);
3919 return 0;
3920 }
Eric Moore635374e2009-03-09 01:21:12 -06003921
3922 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3923 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3924 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3925 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3926 else
3927 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3928
3929 /* set tags */
3930 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3931 if (scmd->device->tagged_supported) {
3932 if (scmd->device->ordered_tags)
3933 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3934 else
3935 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3936 } else
3937/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3938/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3939 */
3940 mpi_control |= (0x500);
3941
3942 } else
3943 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303944 /* Make sure Device is not raid volume.
3945 * We do not expose raid functionality to upper layer for warpdrive.
3946 */
3947 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003948 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003949 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3950
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303951 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003952 if (!smid) {
3953 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3954 ioc->name, __func__);
3955 goto out;
3956 }
3957 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3958 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003959 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003960 if (scmd->cmd_len == 32)
3961 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003962 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3963 if (sas_device_priv_data->sas_target->flags &
3964 MPT_TARGET_FLAGS_RAID_COMPONENT)
3965 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3966 else
3967 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3968 mpi_request->DevHandle =
3969 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3970 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3971 mpi_request->Control = cpu_to_le32(mpi_control);
3972 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3973 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3974 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3975 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303976 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003977 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3978 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3979 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303980 mpi_request->VF_ID = 0; /* TODO */
3981 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003982 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3983 mpi_request->LUN);
3984 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3985
3986 if (!mpi_request->DataLength) {
3987 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3988 } else {
3989 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3990 mpt2sas_base_free_smid(ioc, smid);
3991 goto out;
3992 }
3993 }
3994
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303995 raid_device = sas_target_priv_data->raid_device;
3996 if (raid_device && raid_device->direct_io_enabled)
3997 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3998 smid);
3999
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304000 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4001 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304002 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304003 else
4004 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004005 return 0;
4006
4007 out:
4008 return SCSI_MLQUEUE_HOST_BUSY;
4009}
4010
Jeff Garzikf2812332010-11-16 02:10:29 -05004011static DEF_SCSI_QCMD(_scsih_qcmd)
4012
Eric Moore635374e2009-03-09 01:21:12 -06004013/**
4014 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4015 * @sense_buffer: sense data returned by target
4016 * @data: normalized skey/asc/ascq
4017 *
4018 * Return nothing.
4019 */
4020static void
4021_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4022{
4023 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4024 /* descriptor format */
4025 data->skey = sense_buffer[1] & 0x0F;
4026 data->asc = sense_buffer[2];
4027 data->ascq = sense_buffer[3];
4028 } else {
4029 /* fixed format */
4030 data->skey = sense_buffer[2] & 0x0F;
4031 data->asc = sense_buffer[12];
4032 data->ascq = sense_buffer[13];
4033 }
4034}
4035
4036#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4037/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004038 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004039 * @ioc: per adapter object
4040 * @scmd: pointer to scsi command object
4041 * @mpi_reply: reply mf payload returned from firmware
4042 *
4043 * scsi_status - SCSI Status code returned from target device
4044 * scsi_state - state info associated with SCSI_IO determined by ioc
4045 * ioc_status - ioc supplied status info
4046 *
4047 * Return nothing.
4048 */
4049static void
4050_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4051 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4052{
4053 u32 response_info;
4054 u8 *response_bytes;
4055 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4056 MPI2_IOCSTATUS_MASK;
4057 u8 scsi_state = mpi_reply->SCSIState;
4058 u8 scsi_status = mpi_reply->SCSIStatus;
4059 char *desc_ioc_state = NULL;
4060 char *desc_scsi_status = NULL;
4061 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304062 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304063 struct _sas_device *sas_device = NULL;
4064 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304065 struct scsi_target *starget = scmd->device->sdev_target;
4066 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304067 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304068
4069 if (!priv_target)
4070 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304071
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304072 if (ioc->hide_ir_msg)
4073 device_str = "WarpDrive";
4074 else
4075 device_str = "volume";
4076
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304077 if (log_info == 0x31170000)
4078 return;
Eric Moore635374e2009-03-09 01:21:12 -06004079
4080 switch (ioc_status) {
4081 case MPI2_IOCSTATUS_SUCCESS:
4082 desc_ioc_state = "success";
4083 break;
4084 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4085 desc_ioc_state = "invalid function";
4086 break;
4087 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4088 desc_ioc_state = "scsi recovered error";
4089 break;
4090 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4091 desc_ioc_state = "scsi invalid dev handle";
4092 break;
4093 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4094 desc_ioc_state = "scsi device not there";
4095 break;
4096 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4097 desc_ioc_state = "scsi data overrun";
4098 break;
4099 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4100 desc_ioc_state = "scsi data underrun";
4101 break;
4102 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4103 desc_ioc_state = "scsi io data error";
4104 break;
4105 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4106 desc_ioc_state = "scsi protocol error";
4107 break;
4108 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4109 desc_ioc_state = "scsi task terminated";
4110 break;
4111 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4112 desc_ioc_state = "scsi residual mismatch";
4113 break;
4114 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4115 desc_ioc_state = "scsi task mgmt failed";
4116 break;
4117 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4118 desc_ioc_state = "scsi ioc terminated";
4119 break;
4120 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4121 desc_ioc_state = "scsi ext terminated";
4122 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004123 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4124 desc_ioc_state = "eedp guard error";
4125 break;
4126 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4127 desc_ioc_state = "eedp ref tag error";
4128 break;
4129 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4130 desc_ioc_state = "eedp app tag error";
4131 break;
Eric Moore635374e2009-03-09 01:21:12 -06004132 default:
4133 desc_ioc_state = "unknown";
4134 break;
4135 }
4136
4137 switch (scsi_status) {
4138 case MPI2_SCSI_STATUS_GOOD:
4139 desc_scsi_status = "good";
4140 break;
4141 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4142 desc_scsi_status = "check condition";
4143 break;
4144 case MPI2_SCSI_STATUS_CONDITION_MET:
4145 desc_scsi_status = "condition met";
4146 break;
4147 case MPI2_SCSI_STATUS_BUSY:
4148 desc_scsi_status = "busy";
4149 break;
4150 case MPI2_SCSI_STATUS_INTERMEDIATE:
4151 desc_scsi_status = "intermediate";
4152 break;
4153 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4154 desc_scsi_status = "intermediate condmet";
4155 break;
4156 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4157 desc_scsi_status = "reservation conflict";
4158 break;
4159 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4160 desc_scsi_status = "command terminated";
4161 break;
4162 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4163 desc_scsi_status = "task set full";
4164 break;
4165 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4166 desc_scsi_status = "aca active";
4167 break;
4168 case MPI2_SCSI_STATUS_TASK_ABORTED:
4169 desc_scsi_status = "task aborted";
4170 break;
4171 default:
4172 desc_scsi_status = "unknown";
4173 break;
4174 }
4175
4176 desc_scsi_state[0] = '\0';
4177 if (!scsi_state)
4178 desc_scsi_state = " ";
4179 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4180 strcat(desc_scsi_state, "response info ");
4181 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4182 strcat(desc_scsi_state, "state terminated ");
4183 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4184 strcat(desc_scsi_state, "no status ");
4185 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4186 strcat(desc_scsi_state, "autosense failed ");
4187 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4188 strcat(desc_scsi_state, "autosense valid ");
4189
4190 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304191
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304192 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304193 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4194 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304195 } else {
4196 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4197 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4198 priv_target->sas_address);
4199 if (sas_device) {
4200 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4201 "phy(%d)\n", ioc->name, sas_device->sas_address,
4202 sas_device->phy);
4203 printk(MPT2SAS_WARN_FMT
4204 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4205 ioc->name, sas_device->enclosure_logical_id,
4206 sas_device->slot);
4207 }
4208 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304209 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304210
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304211 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4212 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4213 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004214 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4215 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4216 scsi_get_resid(scmd));
4217 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4218 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4219 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4220 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4221 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4222 scsi_status, desc_scsi_state, scsi_state);
4223
4224 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4225 struct sense_info data;
4226 _scsih_normalize_sense(scmd->sense_buffer, &data);
4227 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304228 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4229 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004230 }
4231
4232 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4233 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4234 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304235 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004236 }
4237}
4238#endif
4239
4240/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304241 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004242 * @ioc: per adapter object
4243 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304244 * Context: process
4245 *
4246 * Return nothing.
4247 */
4248static void
4249_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4250{
4251 Mpi2SepReply_t mpi_reply;
4252 Mpi2SepRequest_t mpi_request;
4253
4254 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4255 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4256 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4257 mpi_request.SlotStatus =
4258 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4259 mpi_request.DevHandle = cpu_to_le16(handle);
4260 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4261 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4262 &mpi_request)) != 0) {
4263 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4264 __FILE__, __LINE__, __func__);
4265 return;
4266 }
4267
4268 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4269 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4270 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4271 le16_to_cpu(mpi_reply.IOCStatus),
4272 le32_to_cpu(mpi_reply.IOCLogInfo)));
4273 return;
4274 }
4275}
4276
4277/**
4278 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4279 * @ioc: per adapter object
4280 * @handle: device handle
4281 * Context: interrupt.
4282 *
4283 * Return nothing.
4284 */
4285static void
4286_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4287{
4288 struct fw_event_work *fw_event;
4289
4290 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4291 if (!fw_event)
4292 return;
4293 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4294 fw_event->device_handle = handle;
4295 fw_event->ioc = ioc;
4296 _scsih_fw_event_add(ioc, fw_event);
4297}
4298
4299/**
4300 * _scsih_smart_predicted_fault - process smart errors
4301 * @ioc: per adapter object
4302 * @handle: device handle
4303 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004304 *
4305 * Return nothing.
4306 */
4307static void
4308_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4309{
Eric Moore635374e2009-03-09 01:21:12 -06004310 struct scsi_target *starget;
4311 struct MPT2SAS_TARGET *sas_target_priv_data;
4312 Mpi2EventNotificationReply_t *event_reply;
4313 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4314 struct _sas_device *sas_device;
4315 ssize_t sz;
4316 unsigned long flags;
4317
4318 /* only handle non-raid devices */
4319 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4320 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4321 if (!sas_device) {
4322 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4323 return;
4324 }
4325 starget = sas_device->starget;
4326 sas_target_priv_data = starget->hostdata;
4327
4328 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4329 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4330 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4331 return;
4332 }
4333 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4334 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4335
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304336 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4337 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004338
4339 /* insert into event log */
4340 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4341 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b2011-11-07 22:05:21 +11004342 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004343 if (!event_reply) {
4344 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4345 ioc->name, __FILE__, __LINE__, __func__);
4346 return;
4347 }
4348
4349 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4350 event_reply->Event =
4351 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4352 event_reply->MsgLength = sz/4;
4353 event_reply->EventDataLength =
4354 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4355 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4356 event_reply->EventData;
4357 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4358 event_data->ASC = 0x5D;
4359 event_data->DevHandle = cpu_to_le16(handle);
4360 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4361 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4362 kfree(event_reply);
4363}
4364
4365/**
Eric Moored5d135b2009-05-18 13:02:08 -06004366 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004367 * @ioc: per adapter object
4368 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304369 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004370 * @reply: reply message frame(lower 32bit addr)
4371 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304372 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004373 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304374 * Return 1 meaning mf should be freed from _base_interrupt
4375 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004376 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304377static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304378_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004379{
4380 Mpi2SCSIIORequest_t *mpi_request;
4381 Mpi2SCSIIOReply_t *mpi_reply;
4382 struct scsi_cmnd *scmd;
4383 u16 ioc_status;
4384 u32 xfer_cnt;
4385 u8 scsi_state;
4386 u8 scsi_status;
4387 u32 log_info;
4388 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304389 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304390 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004391
4392 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304393 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004394 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304395 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004396
4397 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4398
4399 if (mpi_reply == NULL) {
4400 scmd->result = DID_OK << 16;
4401 goto out;
4402 }
4403
4404 sas_device_priv_data = scmd->device->hostdata;
4405 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4406 sas_device_priv_data->sas_target->deleted) {
4407 scmd->result = DID_NO_CONNECT << 16;
4408 goto out;
4409 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304410 /*
4411 * WARPDRIVE: If direct_io is set then it is directIO,
4412 * the failed direct I/O should be redirected to volume
4413 */
4414 if (_scsih_scsi_direct_io_get(ioc, smid)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304415 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4416 ioc->scsi_lookup[smid - 1].scmd = scmd;
4417 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304418 _scsih_scsi_direct_io_set(ioc, smid, 0);
4419 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4420 mpi_request->DevHandle =
4421 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4422 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4423 sas_device_priv_data->sas_target->handle);
4424 return 0;
4425 }
4426
Eric Moore635374e2009-03-09 01:21:12 -06004427
4428 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304429 scsi_state = mpi_reply->SCSIState;
4430 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4431 response_code =
4432 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004433 if (!sas_device_priv_data->tlr_snoop_check) {
4434 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304435 /* Make sure Device is not raid volume.
4436 * We do not expose raid functionality to upper layer for warpdrive.
4437 */
4438 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304439 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304440 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4441 sas_disable_tlr(scmd->device);
4442 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4443 }
Eric Moore635374e2009-03-09 01:21:12 -06004444 }
4445
4446 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4447 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4448 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4449 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4450 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4451 else
4452 log_info = 0;
4453 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004454 scsi_status = mpi_reply->SCSIStatus;
4455
4456 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4457 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4458 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4459 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4460 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4461 }
4462
4463 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4464 struct sense_info data;
4465 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4466 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004467 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004468 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004469 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004470 _scsih_normalize_sense(scmd->sense_buffer, &data);
4471 /* failure prediction threshold exceeded */
4472 if (data.asc == 0x5D)
4473 _scsih_smart_predicted_fault(ioc,
4474 le16_to_cpu(mpi_reply->DevHandle));
4475 }
4476
4477 switch (ioc_status) {
4478 case MPI2_IOCSTATUS_BUSY:
4479 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4480 scmd->result = SAM_STAT_BUSY;
4481 break;
4482
4483 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4484 scmd->result = DID_NO_CONNECT << 16;
4485 break;
4486
4487 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4488 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304489 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4490 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004491 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304492 scmd->result = DID_SOFT_ERROR << 16;
4493 break;
Eric Moore635374e2009-03-09 01:21:12 -06004494 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4495 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4496 scmd->result = DID_RESET << 16;
4497 break;
4498
4499 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4500 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4501 scmd->result = DID_SOFT_ERROR << 16;
4502 else
4503 scmd->result = (DID_OK << 16) | scsi_status;
4504 break;
4505
4506 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4507 scmd->result = (DID_OK << 16) | scsi_status;
4508
4509 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4510 break;
4511
4512 if (xfer_cnt < scmd->underflow) {
4513 if (scsi_status == SAM_STAT_BUSY)
4514 scmd->result = SAM_STAT_BUSY;
4515 else
4516 scmd->result = DID_SOFT_ERROR << 16;
4517 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4518 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4519 scmd->result = DID_SOFT_ERROR << 16;
4520 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4521 scmd->result = DID_RESET << 16;
4522 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4523 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4524 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4525 scmd->result = (DRIVER_SENSE << 24) |
4526 SAM_STAT_CHECK_CONDITION;
4527 scmd->sense_buffer[0] = 0x70;
4528 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4529 scmd->sense_buffer[12] = 0x20;
4530 scmd->sense_buffer[13] = 0;
4531 }
4532 break;
4533
4534 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4535 scsi_set_resid(scmd, 0);
4536 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4537 case MPI2_IOCSTATUS_SUCCESS:
4538 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304539 if (response_code ==
4540 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4541 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4542 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004543 scmd->result = DID_SOFT_ERROR << 16;
4544 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4545 scmd->result = DID_RESET << 16;
4546 break;
4547
Eric Moore3c621b32009-05-18 12:59:41 -06004548 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4549 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4550 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4551 _scsih_eedp_error_handling(scmd, ioc_status);
4552 break;
Eric Moore635374e2009-03-09 01:21:12 -06004553 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4554 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4555 case MPI2_IOCSTATUS_INVALID_SGL:
4556 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4557 case MPI2_IOCSTATUS_INVALID_FIELD:
4558 case MPI2_IOCSTATUS_INVALID_STATE:
4559 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4560 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4561 default:
4562 scmd->result = DID_SOFT_ERROR << 16;
4563 break;
4564
4565 }
4566
4567#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4568 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4569 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4570#endif
4571
4572 out:
4573 scsi_dma_unmap(scmd);
4574 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304575 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004576}
4577
4578/**
Eric Moore635374e2009-03-09 01:21:12 -06004579 * _scsih_sas_host_refresh - refreshing sas host object contents
4580 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004581 * Context: user
4582 *
4583 * During port enable, fw will send topology events for every device. Its
4584 * possible that the handles may change from the previous setting, so this
4585 * code keeping handles updating if changed.
4586 *
4587 * Return nothing.
4588 */
4589static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304590_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004591{
4592 u16 sz;
4593 u16 ioc_status;
4594 int i;
4595 Mpi2ConfigReply_t mpi_reply;
4596 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304597 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304598 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004599
4600 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4601 "updating handles for sas_host(0x%016llx)\n",
4602 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4603
4604 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4605 * sizeof(Mpi2SasIOUnit0PhyData_t));
4606 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4607 if (!sas_iounit_pg0) {
4608 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4609 ioc->name, __FILE__, __LINE__, __func__);
4610 return;
4611 }
Eric Moore635374e2009-03-09 01:21:12 -06004612
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304613 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4614 sas_iounit_pg0, sz)) != 0)
4615 goto out;
4616 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4617 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4618 goto out;
4619 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304620 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304621 if (i == 0)
4622 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4623 PhyData[0].ControllerDevHandle);
4624 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4625 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4626 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304627 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4628 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304629 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304630 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304631 }
Eric Moore635374e2009-03-09 01:21:12 -06004632 out:
4633 kfree(sas_iounit_pg0);
4634}
4635
4636/**
4637 * _scsih_sas_host_add - create sas host object
4638 * @ioc: per adapter object
4639 *
4640 * Creating host side data object, stored in ioc->sas_hba
4641 *
4642 * Return nothing.
4643 */
4644static void
4645_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4646{
4647 int i;
4648 Mpi2ConfigReply_t mpi_reply;
4649 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4650 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4651 Mpi2SasPhyPage0_t phy_pg0;
4652 Mpi2SasDevicePage0_t sas_device_pg0;
4653 Mpi2SasEnclosurePage0_t enclosure_pg0;
4654 u16 ioc_status;
4655 u16 sz;
4656 u16 device_missing_delay;
4657
4658 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4659 if (!ioc->sas_hba.num_phys) {
4660 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4661 ioc->name, __FILE__, __LINE__, __func__);
4662 return;
4663 }
4664
4665 /* sas_iounit page 0 */
4666 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4667 sizeof(Mpi2SasIOUnit0PhyData_t));
4668 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4669 if (!sas_iounit_pg0) {
4670 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4671 ioc->name, __FILE__, __LINE__, __func__);
4672 return;
4673 }
4674 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4675 sas_iounit_pg0, sz))) {
4676 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4677 ioc->name, __FILE__, __LINE__, __func__);
4678 goto out;
4679 }
4680 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4681 MPI2_IOCSTATUS_MASK;
4682 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4683 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4684 ioc->name, __FILE__, __LINE__, __func__);
4685 goto out;
4686 }
4687
4688 /* sas_iounit page 1 */
4689 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4690 sizeof(Mpi2SasIOUnit1PhyData_t));
4691 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4692 if (!sas_iounit_pg1) {
4693 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4694 ioc->name, __FILE__, __LINE__, __func__);
4695 goto out;
4696 }
4697 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4698 sas_iounit_pg1, sz))) {
4699 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4700 ioc->name, __FILE__, __LINE__, __func__);
4701 goto out;
4702 }
4703 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4704 MPI2_IOCSTATUS_MASK;
4705 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4706 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4707 ioc->name, __FILE__, __LINE__, __func__);
4708 goto out;
4709 }
4710
4711 ioc->io_missing_delay =
4712 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4713 device_missing_delay =
4714 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4715 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4716 ioc->device_missing_delay = (device_missing_delay &
4717 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4718 else
4719 ioc->device_missing_delay = device_missing_delay &
4720 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4721
4722 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4723 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4724 sizeof(struct _sas_phy), GFP_KERNEL);
4725 if (!ioc->sas_hba.phy) {
4726 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4727 ioc->name, __FILE__, __LINE__, __func__);
4728 goto out;
4729 }
4730 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4731 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4732 i))) {
4733 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4734 ioc->name, __FILE__, __LINE__, __func__);
4735 goto out;
4736 }
4737 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4738 MPI2_IOCSTATUS_MASK;
4739 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4740 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4741 ioc->name, __FILE__, __LINE__, __func__);
4742 goto out;
4743 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304744
4745 if (i == 0)
4746 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4747 PhyData[0].ControllerDevHandle);
4748 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004749 ioc->sas_hba.phy[i].phy_id = i;
4750 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4751 phy_pg0, ioc->sas_hba.parent_dev);
4752 }
4753 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304754 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004755 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4756 ioc->name, __FILE__, __LINE__, __func__);
4757 goto out;
4758 }
Eric Moore635374e2009-03-09 01:21:12 -06004759 ioc->sas_hba.enclosure_handle =
4760 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4761 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4762 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4763 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4764 (unsigned long long) ioc->sas_hba.sas_address,
4765 ioc->sas_hba.num_phys) ;
4766
4767 if (ioc->sas_hba.enclosure_handle) {
4768 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4769 &enclosure_pg0,
4770 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4771 ioc->sas_hba.enclosure_handle))) {
4772 ioc->sas_hba.enclosure_logical_id =
4773 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4774 }
4775 }
4776
4777 out:
4778 kfree(sas_iounit_pg1);
4779 kfree(sas_iounit_pg0);
4780}
4781
4782/**
4783 * _scsih_expander_add - creating expander object
4784 * @ioc: per adapter object
4785 * @handle: expander handle
4786 *
4787 * Creating expander object, stored in ioc->sas_expander_list.
4788 *
4789 * Return 0 for success, else error.
4790 */
4791static int
4792_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4793{
4794 struct _sas_node *sas_expander;
4795 Mpi2ConfigReply_t mpi_reply;
4796 Mpi2ExpanderPage0_t expander_pg0;
4797 Mpi2ExpanderPage1_t expander_pg1;
4798 Mpi2SasEnclosurePage0_t enclosure_pg0;
4799 u32 ioc_status;
4800 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304801 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004802 int i;
4803 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304804 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004805 int rc = 0;
4806
4807 if (!handle)
4808 return -1;
4809
Eric Moore3cb54692010-07-08 14:44:34 -06004810 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304811 return -1;
4812
Eric Moore635374e2009-03-09 01:21:12 -06004813 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4814 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4815 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4816 ioc->name, __FILE__, __LINE__, __func__);
4817 return -1;
4818 }
4819
4820 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4821 MPI2_IOCSTATUS_MASK;
4822 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4823 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4824 ioc->name, __FILE__, __LINE__, __func__);
4825 return -1;
4826 }
4827
4828 /* handle out of order topology events */
4829 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304830 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4831 != 0) {
4832 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4833 ioc->name, __FILE__, __LINE__, __func__);
4834 return -1;
4835 }
4836 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004837 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304838 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4839 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004840 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4841 if (!sas_expander) {
4842 rc = _scsih_expander_add(ioc, parent_handle);
4843 if (rc != 0)
4844 return rc;
4845 }
4846 }
4847
Eric Moore635374e2009-03-09 01:21:12 -06004848 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304849 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004850 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4851 sas_address);
4852 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4853
4854 if (sas_expander)
4855 return 0;
4856
4857 sas_expander = kzalloc(sizeof(struct _sas_node),
4858 GFP_KERNEL);
4859 if (!sas_expander) {
4860 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4861 ioc->name, __FILE__, __LINE__, __func__);
4862 return -1;
4863 }
4864
4865 sas_expander->handle = handle;
4866 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304867 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004868 sas_expander->sas_address = sas_address;
4869
4870 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4871 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304872 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004873 sas_expander->sas_address, sas_expander->num_phys);
4874
4875 if (!sas_expander->num_phys)
4876 goto out_fail;
4877 sas_expander->phy = kcalloc(sas_expander->num_phys,
4878 sizeof(struct _sas_phy), GFP_KERNEL);
4879 if (!sas_expander->phy) {
4880 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4881 ioc->name, __FILE__, __LINE__, __func__);
4882 rc = -1;
4883 goto out_fail;
4884 }
4885
4886 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4887 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304888 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004889 if (!mpt2sas_port) {
4890 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4891 ioc->name, __FILE__, __LINE__, __func__);
4892 rc = -1;
4893 goto out_fail;
4894 }
4895 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4896
4897 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4898 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4899 &expander_pg1, i, handle))) {
4900 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4901 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304902 rc = -1;
4903 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004904 }
4905 sas_expander->phy[i].handle = handle;
4906 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304907
4908 if ((mpt2sas_transport_add_expander_phy(ioc,
4909 &sas_expander->phy[i], expander_pg1,
4910 sas_expander->parent_dev))) {
4911 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4912 ioc->name, __FILE__, __LINE__, __func__);
4913 rc = -1;
4914 goto out_fail;
4915 }
Eric Moore635374e2009-03-09 01:21:12 -06004916 }
4917
4918 if (sas_expander->enclosure_handle) {
4919 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4920 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4921 sas_expander->enclosure_handle))) {
4922 sas_expander->enclosure_logical_id =
4923 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4924 }
4925 }
4926
4927 _scsih_expander_node_add(ioc, sas_expander);
4928 return 0;
4929
4930 out_fail:
4931
Kashyap, Desai20f58952009-08-07 19:34:26 +05304932 if (mpt2sas_port)
4933 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304934 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004935 kfree(sas_expander);
4936 return rc;
4937}
4938
4939/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304940 * _scsih_done - scsih callback handler.
4941 * @ioc: per adapter object
4942 * @smid: system request message index
4943 * @msix_index: MSIX table index supplied by the OS
4944 * @reply: reply message frame(lower 32bit addr)
4945 *
4946 * Callback handler when sending internal generated message frames.
4947 * The callback index passed is `ioc->scsih_cb_idx`
4948 *
4949 * Return 1 meaning mf should be freed from _base_interrupt
4950 * 0 means the mf is freed from this function.
4951 */
4952static u8
4953_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4954{
4955 MPI2DefaultReply_t *mpi_reply;
4956
4957 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4958 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4959 return 1;
4960 if (ioc->scsih_cmds.smid != smid)
4961 return 1;
4962 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4963 if (mpi_reply) {
4964 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4965 mpi_reply->MsgLength*4);
4966 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4967 }
4968 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4969 complete(&ioc->scsih_cmds.done);
4970 return 1;
4971}
4972
4973/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304974 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004975 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304976 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004977 *
4978 * Return nothing.
4979 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304980void
4981mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004982{
4983 struct _sas_node *sas_expander;
4984 unsigned long flags;
4985
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304986 if (ioc->shost_recovery)
4987 return;
4988
Eric Moore635374e2009-03-09 01:21:12 -06004989 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304990 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4991 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304992 if (!sas_expander) {
4993 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4994 return;
4995 }
4996 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004997 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4998 _scsih_expander_node_remove(ioc, sas_expander);
4999}
5000
5001/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05305002 * _scsih_check_access_status - check access flags
5003 * @ioc: per adapter object
5004 * @sas_address: sas address
5005 * @handle: sas device handle
5006 * @access_flags: errors returned during discovery of the device
5007 *
5008 * Return 0 for success, else failure
5009 */
5010static u8
5011_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5012 u16 handle, u8 access_status)
5013{
5014 u8 rc = 1;
5015 char *desc = NULL;
5016
5017 switch (access_status) {
5018 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5019 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5020 rc = 0;
5021 break;
5022 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5023 desc = "sata capability failed";
5024 break;
5025 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5026 desc = "sata affiliation conflict";
5027 break;
5028 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5029 desc = "route not addressable";
5030 break;
5031 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5032 desc = "smp error not addressable";
5033 break;
5034 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5035 desc = "device blocked";
5036 break;
5037 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5038 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5039 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5040 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5041 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5042 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5043 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5044 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5045 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5046 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5047 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5048 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5049 desc = "sata initialization failed";
5050 break;
5051 default:
5052 desc = "unknown";
5053 break;
5054 }
5055
5056 if (!rc)
5057 return 0;
5058
5059 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5060 "handle(0x%04x)\n", ioc->name, desc,
5061 (unsigned long long)sas_address, handle);
5062 return rc;
5063}
5064
5065static void
5066_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5067{
5068 Mpi2ConfigReply_t mpi_reply;
5069 Mpi2SasDevicePage0_t sas_device_pg0;
5070 struct _sas_device *sas_device;
5071 u32 ioc_status;
5072 unsigned long flags;
5073 u64 sas_address;
5074 struct scsi_target *starget;
5075 struct MPT2SAS_TARGET *sas_target_priv_data;
5076 u32 device_info;
5077
5078 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5079 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5080 return;
5081
5082 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5083 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5084 return;
5085
5086 /* check if this is end device */
5087 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5088 if (!(_scsih_is_end_device(device_info)))
5089 return;
5090
5091 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5092 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5093 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5094 sas_address);
5095
5096 if (!sas_device) {
5097 printk(MPT2SAS_ERR_FMT "device is not present "
5098 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5099 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5100 return;
5101 }
5102
5103 if (unlikely(sas_device->handle != handle)) {
5104 starget = sas_device->starget;
5105 sas_target_priv_data = starget->hostdata;
5106 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5107 " to (0x%04x)!!!\n", sas_device->handle, handle);
5108 sas_target_priv_data->handle = handle;
5109 sas_device->handle = handle;
5110 }
5111 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5112
5113 /* check if device is present */
5114 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5115 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5116 printk(MPT2SAS_ERR_FMT "device is not present "
5117 "handle(0x%04x), flags!!!\n", ioc->name, handle);
5118 return;
5119 }
5120
5121 /* check if there were any issues with discovery */
5122 if (_scsih_check_access_status(ioc, sas_address, handle,
5123 sas_device_pg0.AccessStatus))
5124 return;
5125 _scsih_ublock_io_device(ioc, handle);
5126
5127}
5128
5129/**
Eric Moore635374e2009-03-09 01:21:12 -06005130 * _scsih_add_device - creating sas device object
5131 * @ioc: per adapter object
5132 * @handle: sas device handle
5133 * @phy_num: phy number end device attached to
5134 * @is_pd: is this hidden raid component
5135 *
5136 * Creating end device object, stored in ioc->sas_device_list.
5137 *
5138 * Returns 0 for success, non-zero for failure.
5139 */
5140static int
5141_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5142{
5143 Mpi2ConfigReply_t mpi_reply;
5144 Mpi2SasDevicePage0_t sas_device_pg0;
5145 Mpi2SasEnclosurePage0_t enclosure_pg0;
5146 struct _sas_device *sas_device;
5147 u32 ioc_status;
5148 __le64 sas_address;
5149 u32 device_info;
5150 unsigned long flags;
5151
5152 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5153 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5154 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5155 ioc->name, __FILE__, __LINE__, __func__);
5156 return -1;
5157 }
5158
5159 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5160 MPI2_IOCSTATUS_MASK;
5161 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5162 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5163 ioc->name, __FILE__, __LINE__, __func__);
5164 return -1;
5165 }
5166
Kashyap, Desaib4344272010-03-17 16:24:14 +05305167 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5168
Eric Moore635374e2009-03-09 01:21:12 -06005169 /* check if device is present */
5170 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5171 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5172 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5173 ioc->name, __FILE__, __LINE__, __func__);
5174 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5175 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5176 return -1;
5177 }
5178
Kashyap, Desaib4344272010-03-17 16:24:14 +05305179 /* check if there were any issues with discovery */
5180 if (_scsih_check_access_status(ioc, sas_address, handle,
5181 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005182 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005183
5184 /* check if this is end device */
5185 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5186 if (!(_scsih_is_end_device(device_info))) {
5187 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5188 ioc->name, __FILE__, __LINE__, __func__);
5189 return -1;
5190 }
5191
Eric Moore635374e2009-03-09 01:21:12 -06005192
5193 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5194 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5195 sas_address);
5196 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5197
Kashyap, Desaib4344272010-03-17 16:24:14 +05305198 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005199 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005200
5201 sas_device = kzalloc(sizeof(struct _sas_device),
5202 GFP_KERNEL);
5203 if (!sas_device) {
5204 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5205 ioc->name, __FILE__, __LINE__, __func__);
5206 return -1;
5207 }
5208
5209 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305210 if (_scsih_get_sas_address(ioc, le16_to_cpu
5211 (sas_device_pg0.ParentDevHandle),
5212 &sas_device->sas_address_parent) != 0)
5213 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5214 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005215 sas_device->enclosure_handle =
5216 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5217 sas_device->slot =
5218 le16_to_cpu(sas_device_pg0.Slot);
5219 sas_device->device_info = device_info;
5220 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305221 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005222
5223 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305224 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5225 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5226 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005227 sas_device->enclosure_logical_id =
5228 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005229
5230 /* get device name */
5231 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5232
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305233 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005234 _scsih_sas_device_init_add(ioc, sas_device);
5235 else
5236 _scsih_sas_device_add(ioc, sas_device);
5237
5238 return 0;
5239}
5240
5241/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305242 * _scsih_remove_device - removing sas device object
5243 * @ioc: per adapter object
5244 * @sas_device_delete: the sas_device object
5245 *
5246 * Return nothing.
5247 */
5248static void
5249_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5250 struct _sas_device *sas_device)
5251{
5252 struct _sas_device sas_device_backup;
5253 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305254
Kashyap, Desai1278b112010-03-09 17:34:13 +05305255 if (!sas_device)
5256 return;
Eric Moore635374e2009-03-09 01:21:12 -06005257
Kashyap, Desai1278b112010-03-09 17:34:13 +05305258 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06005259 _scsih_sas_device_remove(ioc, sas_device);
5260
Kashyap, Desai1278b112010-03-09 17:34:13 +05305261 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5262 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5263 sas_device_backup.handle, (unsigned long long)
5264 sas_device_backup.sas_address));
5265
5266 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5267 sas_target_priv_data = sas_device_backup.starget->hostdata;
5268 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305269 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5270 sas_target_priv_data->handle =
5271 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305272 }
5273
Kashyap, Desai1278b112010-03-09 17:34:13 +05305274 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5275
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305276 if (!ioc->hide_drives)
5277 mpt2sas_transport_port_remove(ioc,
5278 sas_device_backup.sas_address,
5279 sas_device_backup.sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305280
5281 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5282 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5283 (unsigned long long) sas_device_backup.sas_address);
5284
5285 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5286 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5287 sas_device_backup.handle, (unsigned long long)
5288 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06005289}
5290
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305291/**
5292 * mpt2sas_device_remove - removing device object
5293 * @ioc: per adapter object
5294 * @sas_address: expander sas_address
5295 *
5296 * Return nothing.
5297 */
5298void
5299mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5300{
5301 struct _sas_device *sas_device;
5302 unsigned long flags;
5303
5304 if (ioc->shost_recovery)
5305 return;
5306
5307 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5308 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5309 sas_address);
5310 if (!sas_device) {
5311 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5312 return;
5313 }
5314 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5315 _scsih_remove_device(ioc, sas_device);
5316}
5317
Eric Moore635374e2009-03-09 01:21:12 -06005318#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5319/**
5320 * _scsih_sas_topology_change_event_debug - debug for topology event
5321 * @ioc: per adapter object
5322 * @event_data: event data payload
5323 * Context: user.
5324 */
5325static void
5326_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5327 Mpi2EventDataSasTopologyChangeList_t *event_data)
5328{
5329 int i;
5330 u16 handle;
5331 u16 reason_code;
5332 u8 phy_number;
5333 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305334 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005335
5336 switch (event_data->ExpStatus) {
5337 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5338 status_str = "add";
5339 break;
5340 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5341 status_str = "remove";
5342 break;
5343 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305344 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005345 status_str = "responding";
5346 break;
5347 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5348 status_str = "remove delay";
5349 break;
5350 default:
5351 status_str = "unknown status";
5352 break;
5353 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305354 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005355 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305356 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005357 "start_phy(%02d), count(%d)\n",
5358 le16_to_cpu(event_data->ExpanderDevHandle),
5359 le16_to_cpu(event_data->EnclosureHandle),
5360 event_data->StartPhyNum, event_data->NumEntries);
5361 for (i = 0; i < event_data->NumEntries; i++) {
5362 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5363 if (!handle)
5364 continue;
5365 phy_number = event_data->StartPhyNum + i;
5366 reason_code = event_data->PHY[i].PhyStatus &
5367 MPI2_EVENT_SAS_TOPO_RC_MASK;
5368 switch (reason_code) {
5369 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305370 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005371 break;
5372 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305373 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005374 break;
5375 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305376 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005377 break;
5378 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305379 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005380 break;
5381 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305382 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005383 break;
5384 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305385 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005386 break;
5387 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305388 link_rate = event_data->PHY[i].LinkRate >> 4;
5389 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305390 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305391 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5392 handle, status_str, link_rate, prev_link_rate);
5393
Eric Moore635374e2009-03-09 01:21:12 -06005394 }
5395}
5396#endif
5397
5398/**
5399 * _scsih_sas_topology_change_event - handle topology changes
5400 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305401 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005402 * Context: user.
5403 *
5404 */
5405static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305406_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005407 struct fw_event_work *fw_event)
5408{
5409 int i;
5410 u16 parent_handle, handle;
5411 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305412 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005413 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305414 struct _sas_device *sas_device;
5415 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005416 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305417 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305418 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005419
5420#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5421 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5422 _scsih_sas_topology_change_event_debug(ioc, event_data);
5423#endif
5424
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305425 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305426 return;
5427
Eric Moore635374e2009-03-09 01:21:12 -06005428 if (!ioc->sas_hba.num_phys)
5429 _scsih_sas_host_add(ioc);
5430 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305431 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005432
5433 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305434 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005435 "event\n", ioc->name));
5436 return;
5437 }
5438
5439 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5440
5441 /* handle expander add */
5442 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5443 if (_scsih_expander_add(ioc, parent_handle) != 0)
5444 return;
5445
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305446 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5447 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5448 parent_handle);
5449 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305450 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305451 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305452 max_phys = sas_expander->num_phys;
5453 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305454 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305455 max_phys = ioc->sas_hba.num_phys;
5456 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305457 return;
5458
Eric Moore635374e2009-03-09 01:21:12 -06005459 /* handle siblings events */
5460 for (i = 0; i < event_data->NumEntries; i++) {
5461 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305462 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005463 "expander event\n", ioc->name));
5464 return;
5465 }
Eric Moore3cb54692010-07-08 14:44:34 -06005466 if (ioc->shost_recovery || ioc->remove_host ||
5467 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305468 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305469 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305470 if (phy_number >= max_phys)
5471 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305472 reason_code = event_data->PHY[i].PhyStatus &
5473 MPI2_EVENT_SAS_TOPO_RC_MASK;
5474 if ((event_data->PHY[i].PhyStatus &
5475 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5476 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005477 continue;
5478 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5479 if (!handle)
5480 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305481 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305482 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005483 switch (reason_code) {
5484 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305485
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305486 if (ioc->shost_recovery)
5487 break;
5488
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305489 if (link_rate == prev_link_rate)
5490 break;
5491
5492 mpt2sas_transport_update_links(ioc, sas_address,
5493 handle, phy_number, link_rate);
5494
Kashyap, Desaib4344272010-03-17 16:24:14 +05305495 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5496 break;
5497
5498 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305499 break;
Eric Moore635374e2009-03-09 01:21:12 -06005500 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305501
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305502 if (ioc->shost_recovery)
5503 break;
5504
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305505 mpt2sas_transport_update_links(ioc, sas_address,
5506 handle, phy_number, link_rate);
5507
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305508 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005509 break;
5510 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305511
5512 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5513 sas_device = _scsih_sas_device_find_by_handle(ioc,
5514 handle);
5515 if (!sas_device) {
5516 spin_unlock_irqrestore(&ioc->sas_device_lock,
5517 flags);
5518 break;
5519 }
5520 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5521 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005522 break;
5523 }
5524 }
5525
5526 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305527 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5528 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305529 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005530
5531}
5532
5533#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5534/**
5535 * _scsih_sas_device_status_change_event_debug - debug for device event
5536 * @event_data: event data payload
5537 * Context: user.
5538 *
5539 * Return nothing.
5540 */
5541static void
5542_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5543 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5544{
5545 char *reason_str = NULL;
5546
5547 switch (event_data->ReasonCode) {
5548 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5549 reason_str = "smart data";
5550 break;
5551 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5552 reason_str = "unsupported device discovered";
5553 break;
5554 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5555 reason_str = "internal device reset";
5556 break;
5557 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5558 reason_str = "internal task abort";
5559 break;
5560 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5561 reason_str = "internal task abort set";
5562 break;
5563 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5564 reason_str = "internal clear task set";
5565 break;
5566 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5567 reason_str = "internal query task";
5568 break;
5569 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5570 reason_str = "sata init failure";
5571 break;
5572 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5573 reason_str = "internal device reset complete";
5574 break;
5575 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5576 reason_str = "internal task abort complete";
5577 break;
5578 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5579 reason_str = "internal async notification";
5580 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305581 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5582 reason_str = "expander reduced functionality";
5583 break;
5584 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5585 reason_str = "expander reduced functionality complete";
5586 break;
Eric Moore635374e2009-03-09 01:21:12 -06005587 default:
5588 reason_str = "unknown reason";
5589 break;
5590 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305591 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305592 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5593 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5594 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5595 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005596 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305597 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005598 event_data->ASC, event_data->ASCQ);
5599 printk(KERN_INFO "\n");
5600}
5601#endif
5602
5603/**
5604 * _scsih_sas_device_status_change_event - handle device status change
5605 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305606 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005607 * Context: user.
5608 *
5609 * Return nothing.
5610 */
5611static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305612_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5613 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005614{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305615 struct MPT2SAS_TARGET *target_priv_data;
5616 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305617 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305618 unsigned long flags;
5619 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5620 fw_event->event_data;
5621
Eric Moore635374e2009-03-09 01:21:12 -06005622#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5623 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305624 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305625 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005626#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305627
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305628 /* In MPI Revision K (0xC), the internal device reset complete was
5629 * implemented, so avoid setting tm_busy flag for older firmware.
5630 */
5631 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5632 return;
5633
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305634 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305635 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305636 event_data->ReasonCode !=
5637 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305638 return;
5639
5640 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5641 sas_address = le64_to_cpu(event_data->SASAddress);
5642 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5643 sas_address);
5644 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5645
5646 if (!sas_device || !sas_device->starget)
5647 return;
5648
5649 target_priv_data = sas_device->starget->hostdata;
5650 if (!target_priv_data)
5651 return;
5652
5653 if (event_data->ReasonCode ==
5654 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5655 target_priv_data->tm_busy = 1;
5656 else
5657 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005658}
5659
5660#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5661/**
5662 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5663 * @ioc: per adapter object
5664 * @event_data: event data payload
5665 * Context: user.
5666 *
5667 * Return nothing.
5668 */
5669static void
5670_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5671 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5672{
5673 char *reason_str = NULL;
5674
5675 switch (event_data->ReasonCode) {
5676 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5677 reason_str = "enclosure add";
5678 break;
5679 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5680 reason_str = "enclosure remove";
5681 break;
5682 default:
5683 reason_str = "unknown reason";
5684 break;
5685 }
5686
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305687 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005688 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5689 " number slots(%d)\n", ioc->name, reason_str,
5690 le16_to_cpu(event_data->EnclosureHandle),
5691 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5692 le16_to_cpu(event_data->StartSlot));
5693}
5694#endif
5695
5696/**
5697 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5698 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305699 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005700 * Context: user.
5701 *
5702 * Return nothing.
5703 */
5704static void
5705_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305706 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005707{
5708#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5709 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5710 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305711 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005712#endif
5713}
5714
5715/**
5716 * _scsih_sas_broadcast_primative_event - handle broadcast events
5717 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305718 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005719 * Context: user.
5720 *
5721 * Return nothing.
5722 */
5723static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305724_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5725 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005726{
5727 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305728 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005729 u16 smid, handle;
5730 u32 lun;
5731 struct MPT2SAS_DEVICE *sas_device_priv_data;
5732 u32 termination_count;
5733 u32 query_count;
5734 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305735 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305736 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305737 unsigned long flags;
5738 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305739 u8 max_retries = 0;
5740 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305741
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305742 mutex_lock(&ioc->tm_cmds.mutex);
5743 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5744 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5745 event_data->PortWidth));
5746
5747 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005748
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305749 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305750 mpi_reply = ioc->tm_cmds.reply;
5751broadcast_aen_retry:
5752
5753 /* sanity checks for retrying this loop */
5754 if (max_retries++ == 5) {
5755 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5756 ioc->name, __func__));
5757 goto out;
5758 } else if (max_retries > 1)
5759 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5760 ioc->name, __func__, max_retries - 1));
5761
Eric Moore635374e2009-03-09 01:21:12 -06005762 termination_count = 0;
5763 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305764 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305765 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305766 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005767 scmd = _scsih_scsi_lookup_get(ioc, smid);
5768 if (!scmd)
5769 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305770 sdev = scmd->device;
5771 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005772 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5773 continue;
5774 /* skip hidden raid components */
5775 if (sas_device_priv_data->sas_target->flags &
5776 MPT_TARGET_FLAGS_RAID_COMPONENT)
5777 continue;
5778 /* skip volumes */
5779 if (sas_device_priv_data->sas_target->flags &
5780 MPT_TARGET_FLAGS_VOLUME)
5781 continue;
5782
5783 handle = sas_device_priv_data->sas_target->handle;
5784 lun = sas_device_priv_data->lun;
5785 query_count++;
5786
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305787 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305788 goto out;
5789
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305790 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305791 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5792 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5793 TM_MUTEX_OFF);
5794 if (r == FAILED) {
5795 sdev_printk(KERN_WARNING, sdev,
5796 "mpt2sas_scsih_issue_tm: FAILED when sending "
5797 "QUERY_TASK: scmd(%p)\n", scmd);
5798 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5799 goto broadcast_aen_retry;
5800 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305801 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5802 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305803 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5804 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5805 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5806 scmd);
5807 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5808 goto broadcast_aen_retry;
5809 }
5810
5811 /* see if IO is still owned by IOC and target */
5812 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005813 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5814 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305815 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305816 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005817 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305818 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305819 task_abort_retries = 0;
5820 tm_retry:
5821 if (task_abort_retries++ == 60) {
5822 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5823 "%s: ABORT_TASK: giving up\n", ioc->name,
5824 __func__));
5825 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5826 goto broadcast_aen_retry;
5827 }
5828
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305829 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305830 goto out_no_lock;
5831
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305832 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5833 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305834 scmd->serial_number, TM_MUTEX_OFF);
5835 if (r == FAILED) {
5836 sdev_printk(KERN_WARNING, sdev,
5837 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305838 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305839 goto tm_retry;
5840 }
5841
5842 if (task_abort_retries > 1)
5843 sdev_printk(KERN_WARNING, sdev,
5844 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5845 " scmd(%p)\n",
5846 task_abort_retries - 1, scmd);
5847
Eric Moore635374e2009-03-09 01:21:12 -06005848 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305849 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005850 }
Eric Moore635374e2009-03-09 01:21:12 -06005851
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305852 if (ioc->broadcast_aen_pending) {
5853 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5854 " pending AEN\n", ioc->name, __func__));
5855 ioc->broadcast_aen_pending = 0;
5856 goto broadcast_aen_retry;
5857 }
5858
5859 out:
5860 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5861 out_no_lock:
5862
5863 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005864 "%s - exit, query_count = %d termination_count = %d\n",
5865 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305866
5867 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305868 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305869 _scsih_ublock_io_all_device(ioc);
5870 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005871}
5872
5873/**
5874 * _scsih_sas_discovery_event - handle discovery events
5875 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305876 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005877 * Context: user.
5878 *
5879 * Return nothing.
5880 */
5881static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305882_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5883 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005884{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305885 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5886
Eric Moore635374e2009-03-09 01:21:12 -06005887#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5888 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305889 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005890 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5891 "start" : "stop");
5892 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305893 printk("discovery_status(0x%08x)",
5894 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005895 printk("\n");
5896 }
5897#endif
5898
5899 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5900 !ioc->sas_hba.num_phys)
5901 _scsih_sas_host_add(ioc);
5902}
5903
5904/**
5905 * _scsih_reprobe_lun - reprobing lun
5906 * @sdev: scsi device struct
5907 * @no_uld_attach: sdev->no_uld_attach flag setting
5908 *
5909 **/
5910static void
5911_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5912{
5913 int rc;
5914
5915 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5916 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5917 sdev->no_uld_attach ? "hidding" : "exposing");
5918 rc = scsi_device_reprobe(sdev);
5919}
5920
5921/**
5922 * _scsih_reprobe_target - reprobing target
5923 * @starget: scsi target struct
5924 * @no_uld_attach: sdev->no_uld_attach flag setting
5925 *
5926 * Note: no_uld_attach flag determines whether the disk device is attached
5927 * to block layer. A value of `1` means to not attach.
5928 **/
5929static void
5930_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5931{
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305932 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06005933
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305934 if (starget == NULL)
5935 return;
5936 sas_target_priv_data = starget->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005937 if (no_uld_attach)
5938 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5939 else
5940 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5941
5942 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5943 _scsih_reprobe_lun);
5944}
5945/**
5946 * _scsih_sas_volume_add - add new volume
5947 * @ioc: per adapter object
5948 * @element: IR config element data
5949 * Context: user.
5950 *
5951 * Return nothing.
5952 */
5953static void
5954_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5955 Mpi2EventIrConfigElement_t *element)
5956{
5957 struct _raid_device *raid_device;
5958 unsigned long flags;
5959 u64 wwid;
5960 u16 handle = le16_to_cpu(element->VolDevHandle);
5961 int rc;
5962
Eric Moore635374e2009-03-09 01:21:12 -06005963 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5964 if (!wwid) {
5965 printk(MPT2SAS_ERR_FMT
5966 "failure at %s:%d/%s()!\n", ioc->name,
5967 __FILE__, __LINE__, __func__);
5968 return;
5969 }
5970
5971 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5972 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5973 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5974
5975 if (raid_device)
5976 return;
5977
5978 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5979 if (!raid_device) {
5980 printk(MPT2SAS_ERR_FMT
5981 "failure at %s:%d/%s()!\n", ioc->name,
5982 __FILE__, __LINE__, __func__);
5983 return;
5984 }
5985
5986 raid_device->id = ioc->sas_id++;
5987 raid_device->channel = RAID_CHANNEL;
5988 raid_device->handle = handle;
5989 raid_device->wwid = wwid;
5990 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305991 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06005992 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5993 raid_device->id, 0);
5994 if (rc)
5995 _scsih_raid_device_remove(ioc, raid_device);
5996 } else
5997 _scsih_determine_boot_device(ioc, raid_device, 1);
5998}
5999
6000/**
6001 * _scsih_sas_volume_delete - delete volume
6002 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306003 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006004 * Context: user.
6005 *
6006 * Return nothing.
6007 */
6008static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306009_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006010{
6011 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006012 unsigned long flags;
6013 struct MPT2SAS_TARGET *sas_target_priv_data;
6014
Eric Moore635374e2009-03-09 01:21:12 -06006015 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6016 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6017 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6018 if (!raid_device)
6019 return;
6020 if (raid_device->starget) {
6021 sas_target_priv_data = raid_device->starget->hostdata;
6022 sas_target_priv_data->deleted = 1;
6023 scsi_remove_target(&raid_device->starget->dev);
6024 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306025 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6026 "(0x%016llx)\n", ioc->name, raid_device->handle,
6027 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006028 _scsih_raid_device_remove(ioc, raid_device);
6029}
6030
6031/**
6032 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6033 * @ioc: per adapter object
6034 * @element: IR config element data
6035 * Context: user.
6036 *
6037 * Return nothing.
6038 */
6039static void
6040_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6041 Mpi2EventIrConfigElement_t *element)
6042{
6043 struct _sas_device *sas_device;
6044 unsigned long flags;
6045 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6046
6047 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6048 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6049 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6050 if (!sas_device)
6051 return;
6052
6053 /* exposing raid component */
6054 sas_device->volume_handle = 0;
6055 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306056 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006057 _scsih_reprobe_target(sas_device->starget, 0);
6058}
6059
6060/**
6061 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6062 * @ioc: per adapter object
6063 * @element: IR config element data
6064 * Context: user.
6065 *
6066 * Return nothing.
6067 */
6068static void
6069_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6070 Mpi2EventIrConfigElement_t *element)
6071{
6072 struct _sas_device *sas_device;
6073 unsigned long flags;
6074 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6075
6076 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6077 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6078 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6079 if (!sas_device)
6080 return;
6081
6082 /* hiding raid component */
6083 mpt2sas_config_get_volume_handle(ioc, handle,
6084 &sas_device->volume_handle);
6085 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
6086 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306087 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006088 _scsih_reprobe_target(sas_device->starget, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306089
Eric Moore635374e2009-03-09 01:21:12 -06006090}
6091
6092/**
6093 * _scsih_sas_pd_delete - delete pd component
6094 * @ioc: per adapter object
6095 * @element: IR config element data
6096 * Context: user.
6097 *
6098 * Return nothing.
6099 */
6100static void
6101_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6102 Mpi2EventIrConfigElement_t *element)
6103{
6104 struct _sas_device *sas_device;
6105 unsigned long flags;
6106 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6107
6108 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6109 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6110 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6111 if (!sas_device)
6112 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306113 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006114}
6115
6116/**
6117 * _scsih_sas_pd_add - remove pd component
6118 * @ioc: per adapter object
6119 * @element: IR config element data
6120 * Context: user.
6121 *
6122 * Return nothing.
6123 */
6124static void
6125_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6126 Mpi2EventIrConfigElement_t *element)
6127{
6128 struct _sas_device *sas_device;
6129 unsigned long flags;
6130 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306131 Mpi2ConfigReply_t mpi_reply;
6132 Mpi2SasDevicePage0_t sas_device_pg0;
6133 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306134 u64 sas_address;
6135 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006136
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306137 set_bit(handle, ioc->pd_handles);
6138
Eric Moore635374e2009-03-09 01:21:12 -06006139 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6140 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6141 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306142 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306143 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306144
6145 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6146 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6147 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6148 ioc->name, __FILE__, __LINE__, __func__);
6149 return;
6150 }
6151
6152 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6153 MPI2_IOCSTATUS_MASK;
6154 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6155 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6156 ioc->name, __FILE__, __LINE__, __func__);
6157 return;
6158 }
6159
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306160 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6161 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6162 mpt2sas_transport_update_links(ioc, sas_address, handle,
6163 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306164
6165 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006166}
6167
6168#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6169/**
6170 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6171 * @ioc: per adapter object
6172 * @event_data: event data payload
6173 * Context: user.
6174 *
6175 * Return nothing.
6176 */
6177static void
6178_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6179 Mpi2EventDataIrConfigChangeList_t *event_data)
6180{
6181 Mpi2EventIrConfigElement_t *element;
6182 u8 element_type;
6183 int i;
6184 char *reason_str = NULL, *element_str = NULL;
6185
6186 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6187
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306188 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006189 ioc->name, (le32_to_cpu(event_data->Flags) &
6190 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6191 "foreign" : "native", event_data->NumElements);
6192 for (i = 0; i < event_data->NumElements; i++, element++) {
6193 switch (element->ReasonCode) {
6194 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6195 reason_str = "add";
6196 break;
6197 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6198 reason_str = "remove";
6199 break;
6200 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6201 reason_str = "no change";
6202 break;
6203 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6204 reason_str = "hide";
6205 break;
6206 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6207 reason_str = "unhide";
6208 break;
6209 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6210 reason_str = "volume_created";
6211 break;
6212 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6213 reason_str = "volume_deleted";
6214 break;
6215 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6216 reason_str = "pd_created";
6217 break;
6218 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6219 reason_str = "pd_deleted";
6220 break;
6221 default:
6222 reason_str = "unknown reason";
6223 break;
6224 }
6225 element_type = le16_to_cpu(element->ElementFlags) &
6226 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6227 switch (element_type) {
6228 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6229 element_str = "volume";
6230 break;
6231 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6232 element_str = "phys disk";
6233 break;
6234 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6235 element_str = "hot spare";
6236 break;
6237 default:
6238 element_str = "unknown element";
6239 break;
6240 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306241 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006242 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6243 reason_str, le16_to_cpu(element->VolDevHandle),
6244 le16_to_cpu(element->PhysDiskDevHandle),
6245 element->PhysDiskNum);
6246 }
6247}
6248#endif
6249
6250/**
6251 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6252 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306253 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006254 * Context: user.
6255 *
6256 * Return nothing.
6257 */
6258static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306259_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6260 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006261{
6262 Mpi2EventIrConfigElement_t *element;
6263 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306264 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306265 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006266
6267#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306268 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6269 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006270 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6271
6272#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306273
6274 if (ioc->shost_recovery)
6275 return;
6276
Kashyap, Desai62727a72009-08-07 19:35:18 +05306277 foreign_config = (le32_to_cpu(event_data->Flags) &
6278 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006279
6280 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6281 for (i = 0; i < event_data->NumElements; i++, element++) {
6282
6283 switch (element->ReasonCode) {
6284 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6285 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306286 if (!foreign_config)
6287 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006288 break;
6289 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6290 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306291 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306292 _scsih_sas_volume_delete(ioc,
6293 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006294 break;
6295 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306296 if (!ioc->is_warpdrive)
6297 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006298 break;
6299 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306300 if (!ioc->is_warpdrive)
6301 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006302 break;
6303 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306304 if (!ioc->is_warpdrive)
6305 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006306 break;
6307 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306308 if (!ioc->is_warpdrive)
6309 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006310 break;
6311 }
6312 }
6313}
6314
6315/**
6316 * _scsih_sas_ir_volume_event - IR volume event
6317 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306318 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006319 * Context: user.
6320 *
6321 * Return nothing.
6322 */
6323static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306324_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6325 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006326{
6327 u64 wwid;
6328 unsigned long flags;
6329 struct _raid_device *raid_device;
6330 u16 handle;
6331 u32 state;
6332 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306333 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006334
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306335 if (ioc->shost_recovery)
6336 return;
6337
Eric Moore635374e2009-03-09 01:21:12 -06006338 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6339 return;
6340
6341 handle = le16_to_cpu(event_data->VolDevHandle);
6342 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306343 if (!ioc->hide_ir_msg)
6344 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6345 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6346 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006347
Eric Moore635374e2009-03-09 01:21:12 -06006348 switch (state) {
6349 case MPI2_RAID_VOL_STATE_MISSING:
6350 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306351 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006352 break;
6353
6354 case MPI2_RAID_VOL_STATE_ONLINE:
6355 case MPI2_RAID_VOL_STATE_DEGRADED:
6356 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306357
6358 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6359 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6360 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6361
Eric Moore635374e2009-03-09 01:21:12 -06006362 if (raid_device)
6363 break;
6364
6365 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6366 if (!wwid) {
6367 printk(MPT2SAS_ERR_FMT
6368 "failure at %s:%d/%s()!\n", ioc->name,
6369 __FILE__, __LINE__, __func__);
6370 break;
6371 }
6372
6373 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6374 if (!raid_device) {
6375 printk(MPT2SAS_ERR_FMT
6376 "failure at %s:%d/%s()!\n", ioc->name,
6377 __FILE__, __LINE__, __func__);
6378 break;
6379 }
6380
6381 raid_device->id = ioc->sas_id++;
6382 raid_device->channel = RAID_CHANNEL;
6383 raid_device->handle = handle;
6384 raid_device->wwid = wwid;
6385 _scsih_raid_device_add(ioc, raid_device);
6386 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6387 raid_device->id, 0);
6388 if (rc)
6389 _scsih_raid_device_remove(ioc, raid_device);
6390 break;
6391
6392 case MPI2_RAID_VOL_STATE_INITIALIZING:
6393 default:
6394 break;
6395 }
6396}
6397
6398/**
6399 * _scsih_sas_ir_physical_disk_event - PD event
6400 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306401 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006402 * Context: user.
6403 *
6404 * Return nothing.
6405 */
6406static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306407_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6408 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006409{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306410 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006411 u32 state;
6412 struct _sas_device *sas_device;
6413 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306414 Mpi2ConfigReply_t mpi_reply;
6415 Mpi2SasDevicePage0_t sas_device_pg0;
6416 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306417 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306418 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006419
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306420 if (ioc->shost_recovery)
6421 return;
6422
Eric Moore635374e2009-03-09 01:21:12 -06006423 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6424 return;
6425
6426 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6427 state = le32_to_cpu(event_data->NewValue);
6428
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306429 if (!ioc->hide_ir_msg)
6430 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6431 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6432 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006433
Eric Moore635374e2009-03-09 01:21:12 -06006434 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006435 case MPI2_RAID_PD_STATE_ONLINE:
6436 case MPI2_RAID_PD_STATE_DEGRADED:
6437 case MPI2_RAID_PD_STATE_REBUILDING:
6438 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306439 case MPI2_RAID_PD_STATE_HOT_SPARE:
6440
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306441 if (!ioc->is_warpdrive)
6442 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306443
6444 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6445 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6446 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6447
6448 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306449 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306450
6451 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6452 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6453 handle))) {
6454 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6455 ioc->name, __FILE__, __LINE__, __func__);
6456 return;
6457 }
6458
6459 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6460 MPI2_IOCSTATUS_MASK;
6461 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6462 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6463 ioc->name, __FILE__, __LINE__, __func__);
6464 return;
6465 }
6466
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306467 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6468 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6469 mpt2sas_transport_update_links(ioc, sas_address, handle,
6470 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306471
6472 _scsih_add_device(ioc, handle, 0, 1);
6473
Eric Moore635374e2009-03-09 01:21:12 -06006474 break;
6475
Kashyap, Desai62727a72009-08-07 19:35:18 +05306476 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006477 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6478 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006479 default:
6480 break;
6481 }
6482}
6483
6484#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6485/**
6486 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6487 * @ioc: per adapter object
6488 * @event_data: event data payload
6489 * Context: user.
6490 *
6491 * Return nothing.
6492 */
6493static void
6494_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6495 Mpi2EventDataIrOperationStatus_t *event_data)
6496{
6497 char *reason_str = NULL;
6498
6499 switch (event_data->RAIDOperation) {
6500 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6501 reason_str = "resync";
6502 break;
6503 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6504 reason_str = "online capacity expansion";
6505 break;
6506 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6507 reason_str = "consistency check";
6508 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306509 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6510 reason_str = "background init";
6511 break;
6512 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6513 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006514 break;
6515 }
6516
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306517 if (!reason_str)
6518 return;
6519
Eric Moore635374e2009-03-09 01:21:12 -06006520 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6521 "\thandle(0x%04x), percent complete(%d)\n",
6522 ioc->name, reason_str,
6523 le16_to_cpu(event_data->VolDevHandle),
6524 event_data->PercentComplete);
6525}
6526#endif
6527
6528/**
6529 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6530 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306531 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006532 * Context: user.
6533 *
6534 * Return nothing.
6535 */
6536static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306537_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6538 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006539{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306540 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6541 static struct _raid_device *raid_device;
6542 unsigned long flags;
6543 u16 handle;
6544
Eric Moore635374e2009-03-09 01:21:12 -06006545#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306546 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6547 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306548 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306549 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006550#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306551
6552 /* code added for raid transport support */
6553 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6554
6555 handle = le16_to_cpu(event_data->VolDevHandle);
6556
6557 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6558 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6559 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6560
6561 if (!raid_device)
6562 return;
6563
6564 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6565 raid_device->percent_complete =
6566 event_data->PercentComplete;
6567 }
Eric Moore635374e2009-03-09 01:21:12 -06006568}
6569
6570/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306571 * _scsih_prep_device_scan - initialize parameters prior to device scan
6572 * @ioc: per adapter object
6573 *
6574 * Set the deleted flag prior to device scan. If the device is found during
6575 * the scan, then we clear the deleted flag.
6576 */
6577static void
6578_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6579{
6580 struct MPT2SAS_DEVICE *sas_device_priv_data;
6581 struct scsi_device *sdev;
6582
6583 shost_for_each_device(sdev, ioc->shost) {
6584 sas_device_priv_data = sdev->hostdata;
6585 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6586 sas_device_priv_data->sas_target->deleted = 1;
6587 }
6588}
6589
6590/**
Eric Moore635374e2009-03-09 01:21:12 -06006591 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6592 * @ioc: per adapter object
6593 * @sas_address: sas address
6594 * @slot: enclosure slot id
6595 * @handle: device handle
6596 *
6597 * After host reset, find out whether devices are still responding.
6598 * Used in _scsi_remove_unresponsive_sas_devices.
6599 *
6600 * Return nothing.
6601 */
6602static void
6603_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6604 u16 slot, u16 handle)
6605{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306606 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006607 struct scsi_target *starget;
6608 struct _sas_device *sas_device;
6609 unsigned long flags;
6610
6611 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6612 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6613 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306614 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006615 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306616 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306617 if (starget && starget->hostdata) {
6618 sas_target_priv_data = starget->hostdata;
6619 sas_target_priv_data->tm_busy = 0;
6620 sas_target_priv_data->deleted = 0;
6621 } else
6622 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306623 if (starget)
6624 starget_printk(KERN_INFO, starget,
6625 "handle(0x%04x), sas_addr(0x%016llx), "
6626 "enclosure logical id(0x%016llx), "
6627 "slot(%d)\n", handle,
6628 (unsigned long long)sas_device->sas_address,
6629 (unsigned long long)
6630 sas_device->enclosure_logical_id,
6631 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006632 if (sas_device->handle == handle)
6633 goto out;
6634 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6635 sas_device->handle);
6636 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306637 if (sas_target_priv_data)
6638 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006639 goto out;
6640 }
6641 }
6642 out:
6643 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6644}
6645
6646/**
6647 * _scsih_search_responding_sas_devices -
6648 * @ioc: per adapter object
6649 *
6650 * After host reset, find out whether devices are still responding.
6651 * If not remove.
6652 *
6653 * Return nothing.
6654 */
6655static void
6656_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6657{
6658 Mpi2SasDevicePage0_t sas_device_pg0;
6659 Mpi2ConfigReply_t mpi_reply;
6660 u16 ioc_status;
6661 __le64 sas_address;
6662 u16 handle;
6663 u32 device_info;
6664 u16 slot;
6665
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306666 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006667
6668 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306669 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006670
6671 handle = 0xFFFF;
6672 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6673 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6674 handle))) {
6675 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6676 MPI2_IOCSTATUS_MASK;
6677 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6678 break;
6679 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6680 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6681 if (!(_scsih_is_end_device(device_info)))
6682 continue;
6683 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6684 slot = le16_to_cpu(sas_device_pg0.Slot);
6685 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6686 handle);
6687 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306688out:
6689 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6690 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006691}
6692
6693/**
6694 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6695 * @ioc: per adapter object
6696 * @wwid: world wide identifier for raid volume
6697 * @handle: device handle
6698 *
6699 * After host reset, find out whether devices are still responding.
6700 * Used in _scsi_remove_unresponsive_raid_devices.
6701 *
6702 * Return nothing.
6703 */
6704static void
6705_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6706 u16 handle)
6707{
6708 struct MPT2SAS_TARGET *sas_target_priv_data;
6709 struct scsi_target *starget;
6710 struct _raid_device *raid_device;
6711 unsigned long flags;
6712
6713 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6714 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6715 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306716 starget = raid_device->starget;
6717 if (starget && starget->hostdata) {
6718 sas_target_priv_data = starget->hostdata;
6719 sas_target_priv_data->deleted = 0;
6720 } else
6721 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006722 raid_device->responding = 1;
6723 starget_printk(KERN_INFO, raid_device->starget,
6724 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6725 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306726 /*
6727 * WARPDRIVE: The handles of the PDs might have changed
6728 * across the host reset so re-initialize the
6729 * required data for Direct IO
6730 */
6731 _scsih_init_warpdrive_properties(ioc, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006732 if (raid_device->handle == handle)
6733 goto out;
6734 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6735 raid_device->handle);
6736 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306737 if (sas_target_priv_data)
6738 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006739 goto out;
6740 }
6741 }
6742 out:
6743 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6744}
6745
6746/**
6747 * _scsih_search_responding_raid_devices -
6748 * @ioc: per adapter object
6749 *
6750 * After host reset, find out whether devices are still responding.
6751 * If not remove.
6752 *
6753 * Return nothing.
6754 */
6755static void
6756_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6757{
6758 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306759 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306760 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006761 Mpi2ConfigReply_t mpi_reply;
6762 u16 ioc_status;
6763 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306764 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006765
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306766 if (!ioc->ir_firmware)
6767 return;
6768
6769 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6770 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006771
6772 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306773 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006774
6775 handle = 0xFFFF;
6776 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6777 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6778 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6779 MPI2_IOCSTATUS_MASK;
6780 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6781 break;
6782 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306783
6784 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6785 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6786 sizeof(Mpi2RaidVolPage0_t)))
6787 continue;
6788
6789 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6790 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6791 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6792 _scsih_mark_responding_raid_device(ioc,
6793 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006794 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306795
6796 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306797 if (!ioc->is_warpdrive) {
6798 phys_disk_num = 0xFF;
6799 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6800 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6801 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6802 phys_disk_num))) {
6803 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6804 MPI2_IOCSTATUS_MASK;
6805 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6806 break;
6807 phys_disk_num = pd_pg0.PhysDiskNum;
6808 handle = le16_to_cpu(pd_pg0.DevHandle);
6809 set_bit(handle, ioc->pd_handles);
6810 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306811 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306812out:
6813 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6814 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006815}
6816
6817/**
6818 * _scsih_mark_responding_expander - mark a expander as responding
6819 * @ioc: per adapter object
6820 * @sas_address: sas address
6821 * @handle:
6822 *
6823 * After host reset, find out whether devices are still responding.
6824 * Used in _scsi_remove_unresponsive_expanders.
6825 *
6826 * Return nothing.
6827 */
6828static void
6829_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6830 u16 handle)
6831{
6832 struct _sas_node *sas_expander;
6833 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306834 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006835
6836 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6837 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306838 if (sas_expander->sas_address != sas_address)
6839 continue;
6840 sas_expander->responding = 1;
6841 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006842 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306843 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6844 " from(0x%04x) to (0x%04x)!!!\n",
6845 (unsigned long long)sas_expander->sas_address,
6846 sas_expander->handle, handle);
6847 sas_expander->handle = handle;
6848 for (i = 0 ; i < sas_expander->num_phys ; i++)
6849 sas_expander->phy[i].handle = handle;
6850 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006851 }
6852 out:
6853 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6854}
6855
6856/**
6857 * _scsih_search_responding_expanders -
6858 * @ioc: per adapter object
6859 *
6860 * After host reset, find out whether devices are still responding.
6861 * If not remove.
6862 *
6863 * Return nothing.
6864 */
6865static void
6866_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6867{
6868 Mpi2ExpanderPage0_t expander_pg0;
6869 Mpi2ConfigReply_t mpi_reply;
6870 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306871 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006872 u16 handle;
6873
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306874 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006875
6876 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306877 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006878
6879 handle = 0xFFFF;
6880 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6881 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6882
6883 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6884 MPI2_IOCSTATUS_MASK;
6885 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6886 break;
6887
6888 handle = le16_to_cpu(expander_pg0.DevHandle);
6889 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6890 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6891 "sas_addr(0x%016llx)\n", handle,
6892 (unsigned long long)sas_address);
6893 _scsih_mark_responding_expander(ioc, sas_address, handle);
6894 }
6895
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306896 out:
6897 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006898}
6899
6900/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306901 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006902 * @ioc: per adapter object
6903 *
6904 * Return nothing.
6905 */
6906static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306907_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006908{
6909 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306910 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006911 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006912
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306913 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
6914 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006915
6916 list_for_each_entry_safe(sas_device, sas_device_next,
6917 &ioc->sas_device_list, list) {
6918 if (sas_device->responding) {
6919 sas_device->responding = 0;
6920 continue;
6921 }
6922 if (sas_device->starget)
6923 starget_printk(KERN_INFO, sas_device->starget,
6924 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6925 "enclosure logical id(0x%016llx), slot(%d)\n",
6926 sas_device->handle,
6927 (unsigned long long)sas_device->sas_address,
6928 (unsigned long long)
6929 sas_device->enclosure_logical_id,
6930 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306931 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006932 }
6933
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306934 if (!ioc->ir_firmware)
6935 goto retry_expander_search;
6936
Eric Moore635374e2009-03-09 01:21:12 -06006937 list_for_each_entry_safe(raid_device, raid_device_next,
6938 &ioc->raid_device_list, list) {
6939 if (raid_device->responding) {
6940 raid_device->responding = 0;
6941 continue;
6942 }
6943 if (raid_device->starget) {
6944 starget_printk(KERN_INFO, raid_device->starget,
6945 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6946 raid_device->handle,
6947 (unsigned long long)raid_device->wwid);
6948 scsi_remove_target(&raid_device->starget->dev);
6949 }
6950 _scsih_raid_device_remove(ioc, raid_device);
6951 }
6952
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306953 retry_expander_search:
6954 sas_expander = NULL;
6955 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006956 if (sas_expander->responding) {
6957 sas_expander->responding = 0;
6958 continue;
6959 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306960 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306961 goto retry_expander_search;
6962 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306963 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
6964 ioc->name);
6965 /* unblock devices */
6966 _scsih_ublock_io_all_device(ioc);
6967}
6968
6969static void
6970_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
6971 struct _sas_node *sas_expander, u16 handle)
6972{
6973 Mpi2ExpanderPage1_t expander_pg1;
6974 Mpi2ConfigReply_t mpi_reply;
6975 int i;
6976
6977 for (i = 0 ; i < sas_expander->num_phys ; i++) {
6978 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
6979 &expander_pg1, i, handle))) {
6980 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6981 ioc->name, __FILE__, __LINE__, __func__);
6982 return;
6983 }
6984
6985 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
6986 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
6987 expander_pg1.NegotiatedLinkRate >> 4);
6988 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306989}
6990
6991/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306992 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306993 * @ioc: per adapter object
6994 *
6995 * Return nothing.
6996 */
6997static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306998_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306999{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307000 Mpi2ExpanderPage0_t expander_pg0;
7001 Mpi2SasDevicePage0_t sas_device_pg0;
7002 Mpi2RaidVolPage1_t volume_pg1;
7003 Mpi2RaidVolPage0_t volume_pg0;
7004 Mpi2RaidPhysDiskPage0_t pd_pg0;
7005 Mpi2EventIrConfigElement_t element;
7006 Mpi2ConfigReply_t mpi_reply;
7007 u8 phys_disk_num;
7008 u16 ioc_status;
7009 u16 handle, parent_handle;
7010 u64 sas_address;
7011 struct _sas_device *sas_device;
7012 struct _sas_node *expander_device;
7013 static struct _raid_device *raid_device;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307014
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307015 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307016
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307017 _scsih_sas_host_refresh(ioc);
7018
7019 /* expanders */
7020 handle = 0xFFFF;
7021 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7022 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7023 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7024 MPI2_IOCSTATUS_MASK;
7025 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7026 break;
7027 handle = le16_to_cpu(expander_pg0.DevHandle);
7028 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7029 ioc, le64_to_cpu(expander_pg0.SASAddress));
7030 if (expander_device)
7031 _scsih_refresh_expander_links(ioc, expander_device,
7032 handle);
7033 else
7034 _scsih_expander_add(ioc, handle);
7035 }
7036
7037 if (!ioc->ir_firmware)
7038 goto skip_to_sas;
7039
7040 /* phys disk */
7041 phys_disk_num = 0xFF;
7042 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7043 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7044 phys_disk_num))) {
7045 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7046 MPI2_IOCSTATUS_MASK;
7047 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7048 break;
7049 phys_disk_num = pd_pg0.PhysDiskNum;
7050 handle = le16_to_cpu(pd_pg0.DevHandle);
7051 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7052 if (sas_device)
7053 continue;
7054 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7055 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7056 handle) != 0)
7057 continue;
7058 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7059 if (!_scsih_get_sas_address(ioc, parent_handle,
7060 &sas_address)) {
7061 mpt2sas_transport_update_links(ioc, sas_address,
7062 handle, sas_device_pg0.PhyNum,
7063 MPI2_SAS_NEG_LINK_RATE_1_5);
7064 set_bit(handle, ioc->pd_handles);
7065 _scsih_add_device(ioc, handle, 0, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307066 }
7067 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307068
7069 /* volumes */
7070 handle = 0xFFFF;
7071 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7072 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7073 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7074 MPI2_IOCSTATUS_MASK;
7075 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7076 break;
7077 handle = le16_to_cpu(volume_pg1.DevHandle);
7078 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7079 le64_to_cpu(volume_pg1.WWID));
7080 if (raid_device)
7081 continue;
7082 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7083 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7084 sizeof(Mpi2RaidVolPage0_t)))
7085 continue;
7086 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7087 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7088 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7089 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7090 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7091 element.VolDevHandle = volume_pg1.DevHandle;
7092 _scsih_sas_volume_add(ioc, &element);
7093 }
7094 }
7095
7096 skip_to_sas:
7097
7098 /* sas devices */
7099 handle = 0xFFFF;
7100 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7101 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7102 handle))) {
7103 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7104 MPI2_IOCSTATUS_MASK;
7105 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7106 break;
7107 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7108 if (!(_scsih_is_end_device(
7109 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7110 continue;
7111 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7112 le64_to_cpu(sas_device_pg0.SASAddress));
7113 if (sas_device)
7114 continue;
7115 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7116 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7117 mpt2sas_transport_update_links(ioc, sas_address, handle,
7118 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7119 _scsih_add_device(ioc, handle, 0, 0);
7120 }
7121 }
7122
7123 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307124}
7125
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307126
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307127/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307128 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7129 * @ioc: per adapter object
7130 * @reset_phase: phase
7131 *
7132 * The handler for doing any required cleanup or initialization.
7133 *
7134 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7135 * MPT2_IOC_DONE_RESET
7136 *
7137 * Return nothing.
7138 */
7139void
7140mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7141{
7142 switch (reset_phase) {
7143 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307144 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307145 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307146 break;
7147 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307148 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307149 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307150 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7151 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7152 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7153 complete(&ioc->scsih_cmds.done);
7154 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307155 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7156 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7157 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7158 complete(&ioc->tm_cmds.done);
7159 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307160 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307161 _scsih_flush_running_cmds(ioc);
7162 break;
7163 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307164 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307165 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307166 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307167 _scsih_prep_device_scan(ioc);
7168 _scsih_search_responding_sas_devices(ioc);
7169 _scsih_search_responding_raid_devices(ioc);
7170 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307171 if (!ioc->is_driver_loading) {
7172 _scsih_prep_device_scan(ioc);
7173 _scsih_search_responding_sas_devices(ioc);
7174 _scsih_search_responding_raid_devices(ioc);
7175 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307176 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307177 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307178 break;
Eric Moore635374e2009-03-09 01:21:12 -06007179 }
7180}
7181
7182/**
7183 * _firmware_event_work - delayed task for processing firmware events
7184 * @ioc: per adapter object
7185 * @work: equal to the fw_event_work object
7186 * Context: user.
7187 *
7188 * Return nothing.
7189 */
7190static void
7191_firmware_event_work(struct work_struct *work)
7192{
7193 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307194 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007195 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7196
Eric Moore635374e2009-03-09 01:21:12 -06007197 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007198 if (ioc->remove_host || fw_event->cancel_pending_work ||
7199 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007200 _scsih_fw_event_free(ioc, fw_event);
7201 return;
7202 }
Eric Moore635374e2009-03-09 01:21:12 -06007203
Eric Moore635374e2009-03-09 01:21:12 -06007204 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307205 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
7206 while (scsi_host_in_recovery(ioc->shost))
7207 ssleep(1);
7208 _scsih_remove_unresponding_sas_devices(ioc);
7209 _scsih_scan_for_devices_after_reset(ioc);
7210 break;
7211 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307212 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307213
7214
7215
7216 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7217 "from worker thread\n", ioc->name));
7218 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307219 case MPT2SAS_TURN_ON_FAULT_LED:
7220 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7221 break;
Eric Moore635374e2009-03-09 01:21:12 -06007222 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307223 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007224 break;
7225 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307226 _scsih_sas_device_status_change_event(ioc,
7227 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007228 break;
7229 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307230 _scsih_sas_discovery_event(ioc,
7231 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007232 break;
7233 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307234 _scsih_sas_broadcast_primative_event(ioc,
7235 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007236 break;
7237 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7238 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307239 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007240 break;
7241 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307242 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007243 break;
7244 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307245 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007246 break;
7247 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307248 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007249 break;
7250 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307251 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007252 break;
Eric Moore635374e2009-03-09 01:21:12 -06007253 }
7254 _scsih_fw_event_free(ioc, fw_event);
7255}
7256
7257/**
7258 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7259 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307260 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007261 * @reply: reply message frame(lower 32bit addr)
7262 * Context: interrupt.
7263 *
7264 * This function merely adds a new work task into ioc->firmware_event_thread.
7265 * The tasks are worked from _firmware_event_work in user context.
7266 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307267 * Return 1 meaning mf should be freed from _base_interrupt
7268 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06007269 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307270u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307271mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7272 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007273{
7274 struct fw_event_work *fw_event;
7275 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007276 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307277 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007278
7279 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007280 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307281 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007282
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307283 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06007284 event = le16_to_cpu(mpi_reply->Event);
7285
7286 switch (event) {
7287 /* handle these */
7288 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7289 {
7290 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7291 (Mpi2EventDataSasBroadcastPrimitive_t *)
7292 mpi_reply->EventData;
7293
7294 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307295 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307296 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307297
7298 if (ioc->broadcast_aen_busy) {
7299 ioc->broadcast_aen_pending++;
7300 return 1;
7301 } else
7302 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007303 break;
7304 }
7305
7306 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7307 _scsih_check_topo_delete_events(ioc,
7308 (Mpi2EventDataSasTopologyChangeList_t *)
7309 mpi_reply->EventData);
7310 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307311 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7312 _scsih_check_ir_config_unhide_events(ioc,
7313 (Mpi2EventDataIrConfigChangeList_t *)
7314 mpi_reply->EventData);
7315 break;
7316 case MPI2_EVENT_IR_VOLUME:
7317 _scsih_check_volume_delete_events(ioc,
7318 (Mpi2EventDataIrVolume_t *)
7319 mpi_reply->EventData);
7320 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307321 case MPI2_EVENT_LOG_ENTRY_ADDED:
7322 {
7323 Mpi2EventDataLogEntryAdded_t *log_entry;
7324 u32 *log_code;
7325
7326 if (!ioc->is_warpdrive)
7327 break;
7328
7329 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7330 mpi_reply->EventData;
7331 log_code = (u32 *)log_entry->LogData;
7332
7333 if (le16_to_cpu(log_entry->LogEntryQualifier)
7334 != MPT2_WARPDRIVE_LOGENTRY)
7335 break;
7336
7337 switch (le32_to_cpu(*log_code)) {
7338 case MPT2_WARPDRIVE_LC_SSDT:
7339 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7340 "IO Throttling has occurred in the WarpDrive "
7341 "subsystem. Check WarpDrive documentation for "
7342 "additional details.\n", ioc->name);
7343 break;
7344 case MPT2_WARPDRIVE_LC_SSDLW:
7345 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7346 "Program/Erase Cycles for the WarpDrive subsystem "
7347 "in degraded range. Check WarpDrive documentation "
7348 "for additional details.\n", ioc->name);
7349 break;
7350 case MPT2_WARPDRIVE_LC_SSDLF:
7351 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7352 "There are no Program/Erase Cycles for the "
7353 "WarpDrive subsystem. The storage device will be "
7354 "in read-only mode. Check WarpDrive documentation "
7355 "for additional details.\n", ioc->name);
7356 break;
7357 case MPT2_WARPDRIVE_LC_BRMF:
7358 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7359 "The Backup Rail Monitor has failed on the "
7360 "WarpDrive subsystem. Check WarpDrive "
7361 "documentation for additional details.\n",
7362 ioc->name);
7363 break;
7364 }
7365
7366 break;
7367 }
Eric Moore635374e2009-03-09 01:21:12 -06007368 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7369 case MPI2_EVENT_IR_OPERATION_STATUS:
7370 case MPI2_EVENT_SAS_DISCOVERY:
7371 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007372 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007373 break;
7374
7375 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307376 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007377 }
7378
7379 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7380 if (!fw_event) {
7381 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7382 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307383 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007384 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307385 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7386 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007387 if (!fw_event->event_data) {
7388 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7389 ioc->name, __FILE__, __LINE__, __func__);
7390 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307391 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007392 }
7393
7394 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307395 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007396 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307397 fw_event->VF_ID = mpi_reply->VF_ID;
7398 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007399 fw_event->event = event;
7400 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307401 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007402}
7403
7404/* shost template */
7405static struct scsi_host_template scsih_driver_template = {
7406 .module = THIS_MODULE,
7407 .name = "Fusion MPT SAS Host",
7408 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007409 .queuecommand = _scsih_qcmd,
7410 .target_alloc = _scsih_target_alloc,
7411 .slave_alloc = _scsih_slave_alloc,
7412 .slave_configure = _scsih_slave_configure,
7413 .target_destroy = _scsih_target_destroy,
7414 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307415 .scan_finished = _scsih_scan_finished,
7416 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007417 .change_queue_depth = _scsih_change_queue_depth,
7418 .change_queue_type = _scsih_change_queue_type,
7419 .eh_abort_handler = _scsih_abort,
7420 .eh_device_reset_handler = _scsih_dev_reset,
7421 .eh_target_reset_handler = _scsih_target_reset,
7422 .eh_host_reset_handler = _scsih_host_reset,
7423 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007424 .can_queue = 1,
7425 .this_id = -1,
7426 .sg_tablesize = MPT2SAS_SG_DEPTH,
7427 .max_sectors = 8192,
7428 .cmd_per_lun = 7,
7429 .use_clustering = ENABLE_CLUSTERING,
7430 .shost_attrs = mpt2sas_host_attrs,
7431 .sdev_attrs = mpt2sas_dev_attrs,
7432};
7433
7434/**
7435 * _scsih_expander_node_remove - removing expander device from list.
7436 * @ioc: per adapter object
7437 * @sas_expander: the sas_device object
7438 * Context: Calling function should acquire ioc->sas_node_lock.
7439 *
7440 * Removing object and freeing associated memory from the
7441 * ioc->sas_expander_list.
7442 *
7443 * Return nothing.
7444 */
7445static void
7446_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7447 struct _sas_node *sas_expander)
7448{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307449 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007450
7451 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307452 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007453 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307454 if (ioc->shost_recovery)
7455 return;
Eric Moore635374e2009-03-09 01:21:12 -06007456 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307457 SAS_END_DEVICE)
7458 mpt2sas_device_remove(ioc,
7459 mpt2sas_port->remote_identify.sas_address);
7460 else if (mpt2sas_port->remote_identify.device_type ==
7461 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007462 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307463 SAS_FANOUT_EXPANDER_DEVICE)
7464 mpt2sas_expander_remove(ioc,
7465 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007466 }
7467
7468 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307469 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007470
7471 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7472 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7473 sas_expander->handle, (unsigned long long)
7474 sas_expander->sas_address);
7475
Eric Moore635374e2009-03-09 01:21:12 -06007476 kfree(sas_expander->phy);
7477 kfree(sas_expander);
7478}
7479
7480/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307481 * _scsih_ir_shutdown - IR shutdown notification
7482 * @ioc: per adapter object
7483 *
7484 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7485 * the host system is shutting down.
7486 *
7487 * Return nothing.
7488 */
7489static void
7490_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7491{
7492 Mpi2RaidActionRequest_t *mpi_request;
7493 Mpi2RaidActionReply_t *mpi_reply;
7494 u16 smid;
7495
7496 /* is IR firmware build loaded ? */
7497 if (!ioc->ir_firmware)
7498 return;
7499
7500 /* are there any volumes ? */
7501 if (list_empty(&ioc->raid_device_list))
7502 return;
7503
7504 mutex_lock(&ioc->scsih_cmds.mutex);
7505
7506 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7507 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7508 ioc->name, __func__);
7509 goto out;
7510 }
7511 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7512
7513 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7514 if (!smid) {
7515 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7516 ioc->name, __func__);
7517 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7518 goto out;
7519 }
7520
7521 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7522 ioc->scsih_cmds.smid = smid;
7523 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7524
7525 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7526 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7527
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307528 if (!ioc->hide_ir_msg)
7529 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307530 init_completion(&ioc->scsih_cmds.done);
7531 mpt2sas_base_put_smid_default(ioc, smid);
7532 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7533
7534 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7535 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7536 ioc->name, __func__);
7537 goto out;
7538 }
7539
7540 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7541 mpi_reply = ioc->scsih_cmds.reply;
7542
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307543 if (!ioc->hide_ir_msg)
7544 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7545 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7546 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7547 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307548 }
7549
7550 out:
7551 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7552 mutex_unlock(&ioc->scsih_cmds.mutex);
7553}
7554
7555/**
7556 * _scsih_shutdown - routine call during system shutdown
7557 * @pdev: PCI device struct
7558 *
7559 * Return nothing.
7560 */
7561static void
7562_scsih_shutdown(struct pci_dev *pdev)
7563{
7564 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7565 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307566 struct workqueue_struct *wq;
7567 unsigned long flags;
7568
7569 ioc->remove_host = 1;
7570 _scsih_fw_event_cleanup_queue(ioc);
7571
7572 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7573 wq = ioc->firmware_event_thread;
7574 ioc->firmware_event_thread = NULL;
7575 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7576 if (wq)
7577 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307578
7579 _scsih_ir_shutdown(ioc);
7580 mpt2sas_base_detach(ioc);
7581}
7582
7583/**
Eric Moored5d135b2009-05-18 13:02:08 -06007584 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007585 * @pdev: PCI device struct
7586 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307587 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007588 * Return nothing.
7589 */
7590static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007591_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007592{
7593 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7594 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307595 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307596 struct _raid_device *raid_device, *next;
7597 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007598 struct workqueue_struct *wq;
7599 unsigned long flags;
7600
7601 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307602 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007603
7604 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7605 wq = ioc->firmware_event_thread;
7606 ioc->firmware_event_thread = NULL;
7607 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7608 if (wq)
7609 destroy_workqueue(wq);
7610
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307611 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307612 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307613 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7614 list) {
7615 if (raid_device->starget) {
7616 sas_target_priv_data =
7617 raid_device->starget->hostdata;
7618 sas_target_priv_data->deleted = 1;
7619 scsi_remove_target(&raid_device->starget->dev);
7620 }
7621 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7622 "(0x%016llx)\n", ioc->name, raid_device->handle,
7623 (unsigned long long) raid_device->wwid);
7624 _scsih_raid_device_remove(ioc, raid_device);
7625 }
7626
Eric Moore635374e2009-03-09 01:21:12 -06007627 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307628 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007629 &ioc->sas_hba.sas_port_list, port_list) {
7630 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307631 SAS_END_DEVICE)
7632 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007633 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307634 else if (mpt2sas_port->remote_identify.device_type ==
7635 SAS_EDGE_EXPANDER_DEVICE ||
7636 mpt2sas_port->remote_identify.device_type ==
7637 SAS_FANOUT_EXPANDER_DEVICE)
7638 mpt2sas_expander_remove(ioc,
7639 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007640 }
7641
7642 /* free phys attached to the sas_host */
7643 if (ioc->sas_hba.num_phys) {
7644 kfree(ioc->sas_hba.phy);
7645 ioc->sas_hba.phy = NULL;
7646 ioc->sas_hba.num_phys = 0;
7647 }
7648
7649 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307650 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007651 list_del(&ioc->list);
7652 scsi_remove_host(shost);
7653 scsi_host_put(shost);
7654}
7655
7656/**
7657 * _scsih_probe_boot_devices - reports 1st device
7658 * @ioc: per adapter object
7659 *
7660 * If specified in bios page 2, this routine reports the 1st
7661 * device scsi-ml or sas transport for persistent boot device
7662 * purposes. Please refer to function _scsih_determine_boot_device()
7663 */
7664static void
7665_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7666{
7667 u8 is_raid;
7668 void *device;
7669 struct _sas_device *sas_device;
7670 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307671 u16 handle;
7672 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007673 u64 sas_address;
7674 unsigned long flags;
7675 int rc;
7676
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307677 /* no Bios, return immediately */
7678 if (!ioc->bios_pg3.BiosVersion)
7679 return;
7680
Eric Moore635374e2009-03-09 01:21:12 -06007681 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307682 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007683 if (ioc->req_boot_device.device) {
7684 device = ioc->req_boot_device.device;
7685 is_raid = ioc->req_boot_device.is_raid;
7686 } else if (ioc->req_alt_boot_device.device) {
7687 device = ioc->req_alt_boot_device.device;
7688 is_raid = ioc->req_alt_boot_device.is_raid;
7689 } else if (ioc->current_boot_device.device) {
7690 device = ioc->current_boot_device.device;
7691 is_raid = ioc->current_boot_device.is_raid;
7692 }
7693
7694 if (!device)
7695 return;
7696
7697 if (is_raid) {
7698 raid_device = device;
7699 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7700 raid_device->id, 0);
7701 if (rc)
7702 _scsih_raid_device_remove(ioc, raid_device);
7703 } else {
7704 sas_device = device;
7705 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307706 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007707 sas_address = sas_device->sas_address;
7708 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7709 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7710 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307711
7712 if (ioc->hide_drives)
7713 return;
Eric Moore635374e2009-03-09 01:21:12 -06007714 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307715 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007716 _scsih_sas_device_remove(ioc, sas_device);
7717 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307718 if (!ioc->is_driver_loading)
7719 mpt2sas_transport_port_remove(ioc, sas_address,
7720 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007721 _scsih_sas_device_remove(ioc, sas_device);
7722 }
7723 }
7724}
7725
7726/**
7727 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7728 * @ioc: per adapter object
7729 *
7730 * Called during initial loading of the driver.
7731 */
7732static void
7733_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7734{
7735 struct _raid_device *raid_device, *raid_next;
7736 int rc;
7737
7738 list_for_each_entry_safe(raid_device, raid_next,
7739 &ioc->raid_device_list, list) {
7740 if (raid_device->starget)
7741 continue;
7742 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7743 raid_device->id, 0);
7744 if (rc)
7745 _scsih_raid_device_remove(ioc, raid_device);
7746 }
7747}
7748
7749/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307750 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007751 * @ioc: per adapter object
7752 *
7753 * Called during initial loading of the driver.
7754 */
7755static void
7756_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7757{
7758 struct _sas_device *sas_device, *next;
7759 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007760
7761 /* SAS Device List */
7762 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7763 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007764
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307765 if (ioc->hide_drives)
7766 continue;
7767
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307768 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7769 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307770 list_del(&sas_device->list);
7771 kfree(sas_device);
7772 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007773 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307774 if (!ioc->is_driver_loading)
7775 mpt2sas_transport_port_remove(ioc,
7776 sas_device->sas_address,
7777 sas_device->sas_address_parent);
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307778 list_del(&sas_device->list);
7779 kfree(sas_device);
7780 continue;
7781
Eric Moore635374e2009-03-09 01:21:12 -06007782 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307783 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7784 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7785 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007786 }
7787}
7788
7789/**
7790 * _scsih_probe_devices - probing for devices
7791 * @ioc: per adapter object
7792 *
7793 * Called during initial loading of the driver.
7794 */
7795static void
7796_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7797{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307798 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06007799
7800 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7801 return; /* return when IOC doesn't support initiator mode */
7802
7803 _scsih_probe_boot_devices(ioc);
7804
7805 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307806 volume_mapping_flags =
7807 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7808 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7809 if (volume_mapping_flags ==
7810 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06007811 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307812 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007813 } else {
Eric Moore635374e2009-03-09 01:21:12 -06007814 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307815 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007816 }
7817 } else
7818 _scsih_probe_sas(ioc);
7819}
7820
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307821
7822/**
7823 * _scsih_scan_start - scsi lld callback for .scan_start
7824 * @shost: SCSI host pointer
7825 *
7826 * The shost has the ability to discover targets on its own instead
7827 * of scanning the entire bus. In our implemention, we will kick off
7828 * firmware discovery.
7829 */
7830static void
7831_scsih_scan_start(struct Scsi_Host *shost)
7832{
7833 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7834 int rc;
7835
7836 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7837 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
7838
7839 ioc->start_scan = 1;
7840 rc = mpt2sas_port_enable(ioc);
7841
7842 if (rc != 0)
7843 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
7844}
7845
7846/**
7847 * _scsih_scan_finished - scsi lld callback for .scan_finished
7848 * @shost: SCSI host pointer
7849 * @time: elapsed time of the scan in jiffies
7850 *
7851 * This function will be called periodically until it returns 1 with the
7852 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7853 * we wait for firmware discovery to complete, then return 1.
7854 */
7855static int
7856_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7857{
7858 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7859
7860 if (time >= (300 * HZ)) {
7861 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7862 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
7863 "(timeout=300s)\n", ioc->name);
7864 ioc->is_driver_loading = 0;
7865 return 1;
7866 }
7867
7868 if (ioc->start_scan)
7869 return 0;
7870
7871 if (ioc->start_scan_failed) {
7872 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
7873 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
7874 ioc->is_driver_loading = 0;
7875 ioc->wait_for_discovery_to_complete = 0;
7876 ioc->remove_host = 1;
7877 return 1;
7878 }
7879
7880 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
7881 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7882
7883 if (ioc->wait_for_discovery_to_complete) {
7884 ioc->wait_for_discovery_to_complete = 0;
7885 _scsih_probe_devices(ioc);
7886 }
7887 mpt2sas_base_start_watchdog(ioc);
7888 ioc->is_driver_loading = 0;
7889 return 1;
7890}
7891
7892
Eric Moore635374e2009-03-09 01:21:12 -06007893/**
Eric Moored5d135b2009-05-18 13:02:08 -06007894 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06007895 * @pdev: PCI device struct
7896 * @id: pci device id
7897 *
7898 * Returns 0 success, anything else error.
7899 */
7900static int
Eric Moored5d135b2009-05-18 13:02:08 -06007901_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06007902{
7903 struct MPT2SAS_ADAPTER *ioc;
7904 struct Scsi_Host *shost;
7905
7906 shost = scsi_host_alloc(&scsih_driver_template,
7907 sizeof(struct MPT2SAS_ADAPTER));
7908 if (!shost)
7909 return -ENODEV;
7910
7911 /* init local params */
7912 ioc = shost_priv(shost);
7913 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7914 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06007915 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06007916 ioc->shost = shost;
7917 ioc->id = mpt_ids++;
7918 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7919 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307920 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7921 ioc->is_warpdrive = 1;
7922 ioc->hide_ir_msg = 1;
7923 } else
7924 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06007925 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7926 ioc->tm_cb_idx = tm_cb_idx;
7927 ioc->ctl_cb_idx = ctl_cb_idx;
7928 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307929 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007930 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05307931 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007932 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307933 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307934 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307935 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007936 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05307937 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06007938 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05307939 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06007940 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7941 spin_lock_init(&ioc->scsi_lookup_lock);
7942 spin_lock_init(&ioc->sas_device_lock);
7943 spin_lock_init(&ioc->sas_node_lock);
7944 spin_lock_init(&ioc->fw_event_lock);
7945 spin_lock_init(&ioc->raid_device_lock);
7946
7947 INIT_LIST_HEAD(&ioc->sas_device_list);
7948 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7949 INIT_LIST_HEAD(&ioc->sas_expander_list);
7950 INIT_LIST_HEAD(&ioc->fw_event_list);
7951 INIT_LIST_HEAD(&ioc->raid_device_list);
7952 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307953 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307954 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06007955
7956 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06007957 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06007958 shost->max_lun = max_lun;
7959 shost->transportt = mpt2sas_transport_template;
7960 shost->unique_id = ioc->id;
7961
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05307962 if (max_sectors != 0xFFFF) {
7963 if (max_sectors < 64) {
7964 shost->max_sectors = 64;
7965 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7966 "for max_sectors, range is 64 to 8192. Assigning "
7967 "value of 64.\n", ioc->name, max_sectors);
7968 } else if (max_sectors > 8192) {
7969 shost->max_sectors = 8192;
7970 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7971 "for max_sectors, range is 64 to 8192. Assigning "
7972 "default value of 8192.\n", ioc->name,
7973 max_sectors);
7974 } else {
7975 shost->max_sectors = max_sectors & 0xFFFE;
7976 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
7977 "set to %d\n", ioc->name, shost->max_sectors);
7978 }
7979 }
7980
Eric Moore635374e2009-03-09 01:21:12 -06007981 if ((scsi_add_host(shost, &pdev->dev))) {
7982 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7983 ioc->name, __FILE__, __LINE__, __func__);
7984 list_del(&ioc->list);
7985 goto out_add_shost_fail;
7986 }
7987
Eric Moore3c621b32009-05-18 12:59:41 -06007988 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06007989 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307990 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06007991
Eric Moore635374e2009-03-09 01:21:12 -06007992 /* event thread */
7993 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7994 "fw_event%d", ioc->id);
7995 ioc->firmware_event_thread = create_singlethread_workqueue(
7996 ioc->firmware_event_name);
7997 if (!ioc->firmware_event_thread) {
7998 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7999 ioc->name, __FILE__, __LINE__, __func__);
8000 goto out_thread_fail;
8001 }
8002
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308003 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008004 if ((mpt2sas_base_attach(ioc))) {
8005 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8006 ioc->name, __FILE__, __LINE__, __func__);
8007 goto out_attach_fail;
8008 }
8009
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308010 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308011 if (ioc->is_warpdrive) {
8012 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8013 ioc->hide_drives = 0;
8014 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8015 ioc->hide_drives = 1;
8016 else {
8017 if (_scsih_get_num_volumes(ioc))
8018 ioc->hide_drives = 1;
8019 else
8020 ioc->hide_drives = 0;
8021 }
8022 } else
8023 ioc->hide_drives = 0;
8024
Eric Moore635374e2009-03-09 01:21:12 -06008025 _scsih_probe_devices(ioc);
8026 return 0;
8027
8028 out_attach_fail:
8029 destroy_workqueue(ioc->firmware_event_thread);
8030 out_thread_fail:
8031 list_del(&ioc->list);
8032 scsi_remove_host(shost);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308033 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008034 out_add_shost_fail:
8035 return -ENODEV;
8036}
8037
8038#ifdef CONFIG_PM
8039/**
Eric Moored5d135b2009-05-18 13:02:08 -06008040 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008041 * @pdev: PCI device struct
8042 * @state: PM state change to (usually PCI_D3)
8043 *
8044 * Returns 0 success, anything else error.
8045 */
8046static int
Eric Moored5d135b2009-05-18 13:02:08 -06008047_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008048{
8049 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8050 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308051 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008052
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308053 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008054 scsi_block_requests(shost);
8055 device_state = pci_choose_state(pdev, state);
8056 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8057 "operating state [D%d]\n", ioc->name, pdev,
8058 pci_name(pdev), device_state);
8059
8060 mpt2sas_base_free_resources(ioc);
8061 pci_save_state(pdev);
8062 pci_disable_device(pdev);
8063 pci_set_power_state(pdev, device_state);
8064 return 0;
8065}
8066
8067/**
Eric Moored5d135b2009-05-18 13:02:08 -06008068 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008069 * @pdev: PCI device struct
8070 *
8071 * Returns 0 success, anything else error.
8072 */
8073static int
Eric Moored5d135b2009-05-18 13:02:08 -06008074_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008075{
8076 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8077 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308078 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008079 int r;
8080
8081 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8082 "operating state [D%d]\n", ioc->name, pdev,
8083 pci_name(pdev), device_state);
8084
8085 pci_set_power_state(pdev, PCI_D0);
8086 pci_enable_wake(pdev, PCI_D0, 0);
8087 pci_restore_state(pdev);
8088 ioc->pdev = pdev;
8089 r = mpt2sas_base_map_resources(ioc);
8090 if (r)
8091 return r;
8092
8093 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8094 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308095 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008096 return 0;
8097}
8098#endif /* CONFIG_PM */
8099
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308100/**
8101 * _scsih_pci_error_detected - Called when a PCI error is detected.
8102 * @pdev: PCI device struct
8103 * @state: PCI channel state
8104 *
8105 * Description: Called when a PCI error is detected.
8106 *
8107 * Return value:
8108 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8109 */
8110static pci_ers_result_t
8111_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8112{
8113 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8114 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8115
8116 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8117 ioc->name, state);
8118
8119 switch (state) {
8120 case pci_channel_io_normal:
8121 return PCI_ERS_RESULT_CAN_RECOVER;
8122 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008123 /* Fatal error, prepare for slot reset */
8124 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308125 scsi_block_requests(ioc->shost);
8126 mpt2sas_base_stop_watchdog(ioc);
8127 mpt2sas_base_free_resources(ioc);
8128 return PCI_ERS_RESULT_NEED_RESET;
8129 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008130 /* Permanent error, prepare for device removal */
8131 ioc->pci_error_recovery = 1;
8132 mpt2sas_base_stop_watchdog(ioc);
8133 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308134 return PCI_ERS_RESULT_DISCONNECT;
8135 }
8136 return PCI_ERS_RESULT_NEED_RESET;
8137}
8138
8139/**
8140 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8141 * @pdev: PCI device struct
8142 *
8143 * Description: This routine is called by the pci error recovery
8144 * code after the PCI slot has been reset, just before we
8145 * should resume normal operations.
8146 */
8147static pci_ers_result_t
8148_scsih_pci_slot_reset(struct pci_dev *pdev)
8149{
8150 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8151 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8152 int rc;
8153
8154 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8155 ioc->name);
8156
Eric Moore3cb54692010-07-08 14:44:34 -06008157 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308158 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008159 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308160 rc = mpt2sas_base_map_resources(ioc);
8161 if (rc)
8162 return PCI_ERS_RESULT_DISCONNECT;
8163
8164
8165 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8166 FORCE_BIG_HAMMER);
8167
8168 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8169 (rc == 0) ? "success" : "failed");
8170
8171 if (!rc)
8172 return PCI_ERS_RESULT_RECOVERED;
8173 else
8174 return PCI_ERS_RESULT_DISCONNECT;
8175}
8176
8177/**
8178 * _scsih_pci_resume() - resume normal ops after PCI reset
8179 * @pdev: pointer to PCI device
8180 *
8181 * Called when the error recovery driver tells us that its
8182 * OK to resume normal operation. Use completion to allow
8183 * halted scsi ops to resume.
8184 */
8185static void
8186_scsih_pci_resume(struct pci_dev *pdev)
8187{
8188 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8189 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8190
8191 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8192
8193 pci_cleanup_aer_uncorrect_error_status(pdev);
8194 mpt2sas_base_start_watchdog(ioc);
8195 scsi_unblock_requests(ioc->shost);
8196}
8197
8198/**
8199 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8200 * @pdev: pointer to PCI device
8201 */
8202static pci_ers_result_t
8203_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8204{
8205 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8206 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8207
8208 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8209 ioc->name);
8210
8211 /* TODO - dump whatever for debugging purposes */
8212
8213 /* Request a slot reset. */
8214 return PCI_ERS_RESULT_NEED_RESET;
8215}
8216
8217static struct pci_error_handlers _scsih_err_handler = {
8218 .error_detected = _scsih_pci_error_detected,
8219 .mmio_enabled = _scsih_pci_mmio_enabled,
8220 .slot_reset = _scsih_pci_slot_reset,
8221 .resume = _scsih_pci_resume,
8222};
Eric Moore635374e2009-03-09 01:21:12 -06008223
8224static struct pci_driver scsih_driver = {
8225 .name = MPT2SAS_DRIVER_NAME,
8226 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008227 .probe = _scsih_probe,
8228 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05308229 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308230 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008231#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008232 .suspend = _scsih_suspend,
8233 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008234#endif
8235};
8236
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308237/* raid transport support */
8238static struct raid_function_template mpt2sas_raid_functions = {
8239 .cookie = &scsih_driver_template,
8240 .is_raid = _scsih_is_raid,
8241 .get_resync = _scsih_get_resync,
8242 .get_state = _scsih_get_state,
8243};
Eric Moore635374e2009-03-09 01:21:12 -06008244
8245/**
Eric Moored5d135b2009-05-18 13:02:08 -06008246 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008247 *
8248 * Returns 0 success, anything else error.
8249 */
8250static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008251_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008252{
8253 int error;
8254
8255 mpt_ids = 0;
8256 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8257 MPT2SAS_DRIVER_VERSION);
8258
8259 mpt2sas_transport_template =
8260 sas_attach_transport(&mpt2sas_transport_functions);
8261 if (!mpt2sas_transport_template)
8262 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308263 /* raid transport support */
8264 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8265 if (!mpt2sas_raid_template) {
8266 sas_release_transport(mpt2sas_transport_template);
8267 return -ENODEV;
8268 }
Eric Moore635374e2009-03-09 01:21:12 -06008269
8270 mpt2sas_base_initialize_callback_handler();
8271
8272 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008273 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008274
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008275 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008276 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008277
8278 /* base internal commands callback handler */
8279 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308280 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8281 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008282
8283 /* transport internal commands callback handler */
8284 transport_cb_idx = mpt2sas_base_register_callback_handler(
8285 mpt2sas_transport_done);
8286
Kashyap, Desai744090d2009-10-05 15:56:56 +05308287 /* scsih internal commands callback handler */
8288 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8289
Eric Moore635374e2009-03-09 01:21:12 -06008290 /* configuration page API internal commands callback handler */
8291 config_cb_idx = mpt2sas_base_register_callback_handler(
8292 mpt2sas_config_done);
8293
8294 /* ctl module callback handler */
8295 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8296
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308297 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8298 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308299
8300 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8301 _scsih_tm_volume_tr_complete);
8302
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308303 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8304 _scsih_sas_control_complete);
8305
Eric Moore635374e2009-03-09 01:21:12 -06008306 mpt2sas_ctl_init();
8307
8308 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308309 if (error) {
8310 /* raid transport support */
8311 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008312 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308313 }
Eric Moore635374e2009-03-09 01:21:12 -06008314
8315 return error;
8316}
8317
8318/**
Eric Moored5d135b2009-05-18 13:02:08 -06008319 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008320 *
8321 * Returns 0 success, anything else error.
8322 */
8323static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008324_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008325{
8326 printk(KERN_INFO "mpt2sas version %s unloading\n",
8327 MPT2SAS_DRIVER_VERSION);
8328
8329 pci_unregister_driver(&scsih_driver);
8330
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308331 mpt2sas_ctl_exit();
8332
Eric Moore635374e2009-03-09 01:21:12 -06008333 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8334 mpt2sas_base_release_callback_handler(tm_cb_idx);
8335 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308336 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008337 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308338 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008339 mpt2sas_base_release_callback_handler(config_cb_idx);
8340 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8341
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308342 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308343 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308344 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8345
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308346 /* raid transport support */
8347 raid_class_release(mpt2sas_raid_template);
8348 sas_release_transport(mpt2sas_transport_template);
8349
Eric Moore635374e2009-03-09 01:21:12 -06008350}
8351
Eric Moored5d135b2009-05-18 13:02:08 -06008352module_init(_scsih_init);
8353module_exit(_scsih_exit);