blob: 8889b1babcacaebdc97d8333d5bdaebf2dd93b88 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05305 * Copyright (C) 2007-2010 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
Eric Moore635374e2009-03-09 01:21:12 -060044#include <linux/module.h>
45#include <linux/kernel.h>
46#include <linux/init.h>
47#include <linux/errno.h>
48#include <linux/blkdev.h>
49#include <linux/sched.h>
50#include <linux/workqueue.h>
51#include <linux/delay.h>
52#include <linux/pci.h>
53#include <linux/interrupt.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053054#include <linux/aer.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053055#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060057
58#include "mpt2sas_base.h"
59
60MODULE_AUTHOR(MPT2SAS_AUTHOR);
61MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
62MODULE_LICENSE("GPL");
63MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
64
65#define RAID_CHANNEL 1
66
67/* forward proto's */
68static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
69 struct _sas_node *sas_expander);
70static void _firmware_event_work(struct work_struct *work);
71
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053072static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
73
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053074static void _scsih_scan_start(struct Scsi_Host *shost);
75static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
76
Eric Moore635374e2009-03-09 01:21:12 -060077/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060078LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060079
80/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060081static u8 scsi_io_cb_idx = -1;
82static u8 tm_cb_idx = -1;
83static u8 ctl_cb_idx = -1;
84static u8 base_cb_idx = -1;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053085static u8 port_enable_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060086static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053087static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060088static u8 config_cb_idx = -1;
89static int mpt_ids;
90
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053091static u8 tm_tr_cb_idx = -1 ;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053092static u8 tm_tr_volume_cb_idx = -1 ;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053093static u8 tm_sas_control_cb_idx = -1;
94
Eric Moore635374e2009-03-09 01:21:12 -060095/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060096static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060097MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
98 "(default=0)");
99
Kashyap, Desaia3e1e552011-06-14 10:56:12 +0530100static ushort max_sectors = 0xFFFF;
101module_param(max_sectors, ushort, 0);
102MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 8192 default=8192");
103
Eric Moore635374e2009-03-09 01:21:12 -0600104/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
105#define MPT2SAS_MAX_LUN (16895)
106static int max_lun = MPT2SAS_MAX_LUN;
107module_param(max_lun, int, 0);
108MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
109
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530110/* diag_buffer_enable is bitwise
111 * bit 0 set = TRACE
112 * bit 1 set = SNAPSHOT
113 * bit 2 set = EXTENDED
114 *
115 * Either bit can be set, or both
116 */
117static int diag_buffer_enable = -1;
118module_param(diag_buffer_enable, int, 0);
119MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
120 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
121
Eric Moore635374e2009-03-09 01:21:12 -0600122/**
123 * struct sense_info - common structure for obtaining sense keys
124 * @skey: sense key
125 * @asc: additional sense code
126 * @ascq: additional sense code qualifier
127 */
128struct sense_info {
129 u8 skey;
130 u8 asc;
131 u8 ascq;
132};
133
134
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530135#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530136#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
137#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
Eric Moore635374e2009-03-09 01:21:12 -0600138/**
139 * struct fw_event_work - firmware event struct
140 * @list: link list framework
141 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530142 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600143 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530144 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600145 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530146 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600147 * @ignore: flag meaning this event has been marked to ignore
148 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
149 * @event_data: reply event data payload follows
150 *
151 * This object stored on ioc->fw_event_list.
152 */
153struct fw_event_work {
154 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530155 u8 cancel_pending_work;
156 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600157 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530158 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600159 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530160 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600161 u8 ignore;
162 u16 event;
163 void *event_data;
164};
165
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530166/* raid transport support */
167static struct raid_template *mpt2sas_raid_template;
168
Eric Moore635374e2009-03-09 01:21:12 -0600169/**
170 * struct _scsi_io_transfer - scsi io transfer
171 * @handle: sas device handle (assigned by firmware)
172 * @is_raid: flag set for hidden raid components
173 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
174 * @data_length: data transfer length
175 * @data_dma: dma pointer to data
176 * @sense: sense data
177 * @lun: lun number
178 * @cdb_length: cdb length
179 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600180 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530181 * @VF_ID: virtual function id
182 * @VP_ID: virtual port id
183 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600184 * @sense_length: sense length
185 * @ioc_status: ioc status
186 * @scsi_state: scsi state
187 * @scsi_status: scsi staus
188 * @log_info: log information
189 * @transfer_length: data length transfer when there is a reply message
190 *
191 * Used for sending internal scsi commands to devices within this module.
192 * Refer to _scsi_send_scsi_io().
193 */
194struct _scsi_io_transfer {
195 u16 handle;
196 u8 is_raid;
197 enum dma_data_direction dir;
198 u32 data_length;
199 dma_addr_t data_dma;
200 u8 sense[SCSI_SENSE_BUFFERSIZE];
201 u32 lun;
202 u8 cdb_length;
203 u8 cdb[32];
204 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530205 u8 VF_ID;
206 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600207 u8 valid_reply;
208 /* the following bits are only valid when 'valid_reply = 1' */
209 u32 sense_length;
210 u16 ioc_status;
211 u8 scsi_state;
212 u8 scsi_status;
213 u32 log_info;
214 u32 transfer_length;
215};
216
217/*
218 * The pci device ids are defined in mpi/mpi2_cnfg.h.
219 */
220static struct pci_device_id scsih_pci_table[] = {
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
222 PCI_ANY_ID, PCI_ANY_ID },
223 /* Falcon ~ 2008*/
224 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
225 PCI_ANY_ID, PCI_ANY_ID },
226 /* Liberator ~ 2108 */
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
228 PCI_ANY_ID, PCI_ANY_ID },
229 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
230 PCI_ANY_ID, PCI_ANY_ID },
231 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
232 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530233 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
235 PCI_ANY_ID, PCI_ANY_ID },
236 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
237 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530238 /* Thunderbolt ~ 2208 */
239 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
240 PCI_ANY_ID, PCI_ANY_ID },
241 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
242 PCI_ANY_ID, PCI_ANY_ID },
243 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
244 PCI_ANY_ID, PCI_ANY_ID },
245 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
246 PCI_ANY_ID, PCI_ANY_ID },
247 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
248 PCI_ANY_ID, PCI_ANY_ID },
249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
250 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530251 /* Mustang ~ 2308 */
252 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530253 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530254 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
255 PCI_ANY_ID, PCI_ANY_ID },
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530257 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530258 /* SSS6200 */
259 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
260 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600261 {0} /* Terminating entry */
262};
263MODULE_DEVICE_TABLE(pci, scsih_pci_table);
264
265/**
Eric Moored5d135b2009-05-18 13:02:08 -0600266 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600267 *
268 * Note: The logging levels are defined in mpt2sas_debug.h.
269 */
270static int
Eric Moored5d135b2009-05-18 13:02:08 -0600271_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600272{
273 int ret = param_set_int(val, kp);
274 struct MPT2SAS_ADAPTER *ioc;
275
276 if (ret)
277 return ret;
278
279 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600280 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600281 ioc->logging_level = logging_level;
282 return 0;
283}
Eric Moored5d135b2009-05-18 13:02:08 -0600284module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600285 &logging_level, 0644);
286
287/**
288 * _scsih_srch_boot_sas_address - search based on sas_address
289 * @sas_address: sas address
290 * @boot_device: boot device object from bios page 2
291 *
292 * Returns 1 when there's a match, 0 means no match.
293 */
294static inline int
295_scsih_srch_boot_sas_address(u64 sas_address,
296 Mpi2BootDeviceSasWwid_t *boot_device)
297{
298 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
299}
300
301/**
302 * _scsih_srch_boot_device_name - search based on device name
303 * @device_name: device name specified in INDENTIFY fram
304 * @boot_device: boot device object from bios page 2
305 *
306 * Returns 1 when there's a match, 0 means no match.
307 */
308static inline int
309_scsih_srch_boot_device_name(u64 device_name,
310 Mpi2BootDeviceDeviceName_t *boot_device)
311{
312 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
313}
314
315/**
316 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
317 * @enclosure_logical_id: enclosure logical id
318 * @slot_number: slot number
319 * @boot_device: boot device object from bios page 2
320 *
321 * Returns 1 when there's a match, 0 means no match.
322 */
323static inline int
324_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
325 Mpi2BootDeviceEnclosureSlot_t *boot_device)
326{
327 return (enclosure_logical_id == le64_to_cpu(boot_device->
328 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
329 SlotNumber)) ? 1 : 0;
330}
331
332/**
333 * _scsih_is_boot_device - search for matching boot device.
334 * @sas_address: sas address
335 * @device_name: device name specified in INDENTIFY fram
336 * @enclosure_logical_id: enclosure logical id
337 * @slot_number: slot number
338 * @form: specifies boot device form
339 * @boot_device: boot device object from bios page 2
340 *
341 * Returns 1 when there's a match, 0 means no match.
342 */
343static int
344_scsih_is_boot_device(u64 sas_address, u64 device_name,
345 u64 enclosure_logical_id, u16 slot, u8 form,
346 Mpi2BiosPage2BootDevice_t *boot_device)
347{
348 int rc = 0;
349
350 switch (form) {
351 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
352 if (!sas_address)
353 break;
354 rc = _scsih_srch_boot_sas_address(
355 sas_address, &boot_device->SasWwid);
356 break;
357 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
358 if (!enclosure_logical_id)
359 break;
360 rc = _scsih_srch_boot_encl_slot(
361 enclosure_logical_id,
362 slot, &boot_device->EnclosureSlot);
363 break;
364 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
365 if (!device_name)
366 break;
367 rc = _scsih_srch_boot_device_name(
368 device_name, &boot_device->DeviceName);
369 break;
370 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
371 break;
372 }
373
374 return rc;
375}
376
377/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530378 * _scsih_get_sas_address - set the sas_address for given device handle
379 * @handle: device handle
380 * @sas_address: sas address
381 *
382 * Returns 0 success, non-zero when failure
383 */
384static int
385_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
386 u64 *sas_address)
387{
388 Mpi2SasDevicePage0_t sas_device_pg0;
389 Mpi2ConfigReply_t mpi_reply;
390 u32 ioc_status;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530391 *sas_address = 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530392
393 if (handle <= ioc->sas_hba.num_phys) {
394 *sas_address = ioc->sas_hba.sas_address;
395 return 0;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530396 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530397
398 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
399 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530400 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
401 __FILE__, __LINE__, __func__);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530402 return -ENXIO;
403 }
404
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530405 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
406 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
407 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
408 return 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530409 }
410
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530411 /* we hit this becuase the given parent handle doesn't exist */
412 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
413 return -ENXIO;
414 /* else error case */
415 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
416 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
417 __FILE__, __LINE__, __func__);
418 return -EIO;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530419}
420
421/**
Eric Moore635374e2009-03-09 01:21:12 -0600422 * _scsih_determine_boot_device - determine boot device.
423 * @ioc: per adapter object
424 * @device: either sas_device or raid_device object
425 * @is_raid: [flag] 1 = raid object, 0 = sas object
426 *
427 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300428 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600429 * There are primary, alternate, and current entries in bios page 2. The order
430 * priority is primary, alternate, then current. This routine saves
431 * the corresponding device object and is_raid flag in the ioc object.
432 * The saved data to be used later in _scsih_probe_boot_devices().
433 */
434static void
435_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
436 void *device, u8 is_raid)
437{
438 struct _sas_device *sas_device;
439 struct _raid_device *raid_device;
440 u64 sas_address;
441 u64 device_name;
442 u64 enclosure_logical_id;
443 u16 slot;
444
445 /* only process this function when driver loads */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530446 if (!ioc->is_driver_loading)
447 return;
448
449 /* no Bios, return immediately */
450 if (!ioc->bios_pg3.BiosVersion)
Eric Moore635374e2009-03-09 01:21:12 -0600451 return;
452
453 if (!is_raid) {
454 sas_device = device;
455 sas_address = sas_device->sas_address;
456 device_name = sas_device->device_name;
457 enclosure_logical_id = sas_device->enclosure_logical_id;
458 slot = sas_device->slot;
459 } else {
460 raid_device = device;
461 sas_address = raid_device->wwid;
462 device_name = 0;
463 enclosure_logical_id = 0;
464 slot = 0;
465 }
466
467 if (!ioc->req_boot_device.device) {
468 if (_scsih_is_boot_device(sas_address, device_name,
469 enclosure_logical_id, slot,
470 (ioc->bios_pg2.ReqBootDeviceForm &
471 MPI2_BIOSPAGE2_FORM_MASK),
472 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530473 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600474 "%s: req_boot_device(0x%016llx)\n",
475 ioc->name, __func__,
476 (unsigned long long)sas_address));
477 ioc->req_boot_device.device = device;
478 ioc->req_boot_device.is_raid = is_raid;
479 }
480 }
481
482 if (!ioc->req_alt_boot_device.device) {
483 if (_scsih_is_boot_device(sas_address, device_name,
484 enclosure_logical_id, slot,
485 (ioc->bios_pg2.ReqAltBootDeviceForm &
486 MPI2_BIOSPAGE2_FORM_MASK),
487 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530488 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600489 "%s: req_alt_boot_device(0x%016llx)\n",
490 ioc->name, __func__,
491 (unsigned long long)sas_address));
492 ioc->req_alt_boot_device.device = device;
493 ioc->req_alt_boot_device.is_raid = is_raid;
494 }
495 }
496
497 if (!ioc->current_boot_device.device) {
498 if (_scsih_is_boot_device(sas_address, device_name,
499 enclosure_logical_id, slot,
500 (ioc->bios_pg2.CurrentBootDeviceForm &
501 MPI2_BIOSPAGE2_FORM_MASK),
502 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530503 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600504 "%s: current_boot_device(0x%016llx)\n",
505 ioc->name, __func__,
506 (unsigned long long)sas_address));
507 ioc->current_boot_device.device = device;
508 ioc->current_boot_device.is_raid = is_raid;
509 }
510 }
511}
512
513/**
514 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
515 * @ioc: per adapter object
516 * @sas_address: sas address
517 * Context: Calling function should acquire ioc->sas_device_lock
518 *
519 * This searches for sas_device based on sas_address, then return sas_device
520 * object.
521 */
522struct _sas_device *
523mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
524 u64 sas_address)
525{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530526 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600527
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530528 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
529 if (sas_device->sas_address == sas_address)
530 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600531
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530532 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
533 if (sas_device->sas_address == sas_address)
534 return sas_device;
535
536 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600537}
538
539/**
540 * _scsih_sas_device_find_by_handle - sas device search
541 * @ioc: per adapter object
542 * @handle: sas device handle (assigned by firmware)
543 * Context: Calling function should acquire ioc->sas_device_lock
544 *
545 * This searches for sas_device based on sas_address, then return sas_device
546 * object.
547 */
548static struct _sas_device *
549_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
550{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530551 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600552
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530553 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
554 if (sas_device->handle == handle)
555 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600556
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530557 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
558 if (sas_device->handle == handle)
559 return sas_device;
560
561 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600562}
563
564/**
565 * _scsih_sas_device_remove - remove sas_device from list.
566 * @ioc: per adapter object
567 * @sas_device: the sas_device object
568 * Context: This function will acquire ioc->sas_device_lock.
569 *
570 * Removing object and freeing associated memory from the ioc->sas_device_list.
571 */
572static void
573_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
574 struct _sas_device *sas_device)
575{
576 unsigned long flags;
577
Kashyap, Desai980ead32010-04-08 17:55:22 +0530578 if (!sas_device)
579 return;
580
Eric Moore635374e2009-03-09 01:21:12 -0600581 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530582 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
583 sas_device->sas_address)) {
584 list_del(&sas_device->list);
585 kfree(sas_device);
586 }
Eric Moore635374e2009-03-09 01:21:12 -0600587 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
588}
589
590/**
591 * _scsih_sas_device_add - insert sas_device to the list.
592 * @ioc: per adapter object
593 * @sas_device: the sas_device object
594 * Context: This function will acquire ioc->sas_device_lock.
595 *
596 * Adding new object to the ioc->sas_device_list.
597 */
598static void
599_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
600 struct _sas_device *sas_device)
601{
602 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600603
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530604 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600605 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
606 sas_device->handle, (unsigned long long)sas_device->sas_address));
607
608 spin_lock_irqsave(&ioc->sas_device_lock, flags);
609 list_add_tail(&sas_device->list, &ioc->sas_device_list);
610 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
611
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530612 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530613 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -0600614 _scsih_sas_device_remove(ioc, sas_device);
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530615 } else if (!sas_device->starget) {
616 if (!ioc->is_driver_loading)
617 mpt2sas_transport_port_remove(ioc,
618 sas_device->sas_address,
619 sas_device->sas_address_parent);
620 _scsih_sas_device_remove(ioc, sas_device);
621 }
Eric Moore635374e2009-03-09 01:21:12 -0600622}
623
624/**
625 * _scsih_sas_device_init_add - insert sas_device to the list.
626 * @ioc: per adapter object
627 * @sas_device: the sas_device object
628 * Context: This function will acquire ioc->sas_device_lock.
629 *
630 * Adding new object at driver load time to the ioc->sas_device_init_list.
631 */
632static void
633_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
634 struct _sas_device *sas_device)
635{
636 unsigned long flags;
637
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530638 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600639 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
640 sas_device->handle, (unsigned long long)sas_device->sas_address));
641
642 spin_lock_irqsave(&ioc->sas_device_lock, flags);
643 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
644 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
645 _scsih_determine_boot_device(ioc, sas_device, 0);
646}
647
648/**
Eric Moore635374e2009-03-09 01:21:12 -0600649 * _scsih_raid_device_find_by_id - raid device search
650 * @ioc: per adapter object
651 * @id: sas device target id
652 * @channel: sas device channel
653 * Context: Calling function should acquire ioc->raid_device_lock
654 *
655 * This searches for raid_device based on target id, then return raid_device
656 * object.
657 */
658static struct _raid_device *
659_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
660{
661 struct _raid_device *raid_device, *r;
662
663 r = NULL;
664 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
665 if (raid_device->id == id && raid_device->channel == channel) {
666 r = raid_device;
667 goto out;
668 }
669 }
670
671 out:
672 return r;
673}
674
675/**
676 * _scsih_raid_device_find_by_handle - raid device search
677 * @ioc: per adapter object
678 * @handle: sas device handle (assigned by firmware)
679 * Context: Calling function should acquire ioc->raid_device_lock
680 *
681 * This searches for raid_device based on handle, then return raid_device
682 * object.
683 */
684static struct _raid_device *
685_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
686{
687 struct _raid_device *raid_device, *r;
688
689 r = NULL;
690 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
691 if (raid_device->handle != handle)
692 continue;
693 r = raid_device;
694 goto out;
695 }
696
697 out:
698 return r;
699}
700
701/**
702 * _scsih_raid_device_find_by_wwid - raid device search
703 * @ioc: per adapter object
704 * @handle: sas device handle (assigned by firmware)
705 * Context: Calling function should acquire ioc->raid_device_lock
706 *
707 * This searches for raid_device based on wwid, then return raid_device
708 * object.
709 */
710static struct _raid_device *
711_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
712{
713 struct _raid_device *raid_device, *r;
714
715 r = NULL;
716 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
717 if (raid_device->wwid != wwid)
718 continue;
719 r = raid_device;
720 goto out;
721 }
722
723 out:
724 return r;
725}
726
727/**
728 * _scsih_raid_device_add - add raid_device object
729 * @ioc: per adapter object
730 * @raid_device: raid_device object
731 *
732 * This is added to the raid_device_list link list.
733 */
734static void
735_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
736 struct _raid_device *raid_device)
737{
738 unsigned long flags;
739
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530740 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600741 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
742 raid_device->handle, (unsigned long long)raid_device->wwid));
743
744 spin_lock_irqsave(&ioc->raid_device_lock, flags);
745 list_add_tail(&raid_device->list, &ioc->raid_device_list);
746 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
747}
748
749/**
750 * _scsih_raid_device_remove - delete raid_device object
751 * @ioc: per adapter object
752 * @raid_device: raid_device object
753 *
754 * This is removed from the raid_device_list link list.
755 */
756static void
757_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
758 struct _raid_device *raid_device)
759{
760 unsigned long flags;
761
762 spin_lock_irqsave(&ioc->raid_device_lock, flags);
763 list_del(&raid_device->list);
764 memset(raid_device, 0, sizeof(struct _raid_device));
765 kfree(raid_device);
766 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
767}
768
769/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530770 * mpt2sas_scsih_expander_find_by_handle - expander device search
771 * @ioc: per adapter object
772 * @handle: expander handle (assigned by firmware)
773 * Context: Calling function should acquire ioc->sas_device_lock
774 *
775 * This searches for expander device based on handle, then returns the
776 * sas_node object.
777 */
778struct _sas_node *
779mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
780{
781 struct _sas_node *sas_expander, *r;
782
783 r = NULL;
784 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
785 if (sas_expander->handle != handle)
786 continue;
787 r = sas_expander;
788 goto out;
789 }
790 out:
791 return r;
792}
793
794/**
Eric Moore635374e2009-03-09 01:21:12 -0600795 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
796 * @ioc: per adapter object
797 * @sas_address: sas address
798 * Context: Calling function should acquire ioc->sas_node_lock.
799 *
800 * This searches for expander device based on sas_address, then returns the
801 * sas_node object.
802 */
803struct _sas_node *
804mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
805 u64 sas_address)
806{
807 struct _sas_node *sas_expander, *r;
808
809 r = NULL;
810 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
811 if (sas_expander->sas_address != sas_address)
812 continue;
813 r = sas_expander;
814 goto out;
815 }
816 out:
817 return r;
818}
819
820/**
821 * _scsih_expander_node_add - insert expander device to the list.
822 * @ioc: per adapter object
823 * @sas_expander: the sas_device object
824 * Context: This function will acquire ioc->sas_node_lock.
825 *
826 * Adding new object to the ioc->sas_expander_list.
827 *
828 * Return nothing.
829 */
830static void
831_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
832 struct _sas_node *sas_expander)
833{
834 unsigned long flags;
835
836 spin_lock_irqsave(&ioc->sas_node_lock, flags);
837 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
838 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
839}
840
841/**
842 * _scsih_is_end_device - determines if device is an end device
843 * @device_info: bitfield providing information about the device.
844 * Context: none
845 *
846 * Returns 1 if end device.
847 */
848static int
849_scsih_is_end_device(u32 device_info)
850{
851 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
852 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
853 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
854 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
855 return 1;
856 else
857 return 0;
858}
859
860/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530861 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600862 * @ioc: per adapter object
863 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600864 *
865 * Returns the smid stored scmd pointer.
866 */
867static struct scsi_cmnd *
868_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
869{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530870 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600871}
872
873/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530874 * _scsih_scsi_lookup_get_clear - returns scmd entry
875 * @ioc: per adapter object
876 * @smid: system request message index
877 *
878 * Returns the smid stored scmd pointer.
879 * Then will derefrence the stored scmd pointer.
880 */
881static inline struct scsi_cmnd *
882_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
883{
884 unsigned long flags;
885 struct scsi_cmnd *scmd;
886
887 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
888 scmd = ioc->scsi_lookup[smid - 1].scmd;
889 ioc->scsi_lookup[smid - 1].scmd = NULL;
890 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
891
892 return scmd;
893}
894
895/**
Eric Moore635374e2009-03-09 01:21:12 -0600896 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
897 * @ioc: per adapter object
898 * @smid: system request message index
899 * @scmd: pointer to scsi command object
900 * Context: This function will acquire ioc->scsi_lookup_lock.
901 *
902 * This will search for a scmd pointer in the scsi_lookup array,
903 * returning the revelent smid. A returned value of zero means invalid.
904 */
905static u16
906_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
907 *scmd)
908{
909 u16 smid;
910 unsigned long flags;
911 int i;
912
913 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
914 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530915 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600916 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530917 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600918 goto out;
919 }
920 }
921 out:
922 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
923 return smid;
924}
925
926/**
927 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
928 * @ioc: per adapter object
929 * @id: target id
930 * @channel: channel
931 * Context: This function will acquire ioc->scsi_lookup_lock.
932 *
933 * This will search for a matching channel:id in the scsi_lookup array,
934 * returning 1 if found.
935 */
936static u8
937_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
938 int channel)
939{
940 u8 found;
941 unsigned long flags;
942 int i;
943
944 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
945 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530946 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600947 if (ioc->scsi_lookup[i].scmd &&
948 (ioc->scsi_lookup[i].scmd->device->id == id &&
949 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
950 found = 1;
951 goto out;
952 }
953 }
954 out:
955 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
956 return found;
957}
958
959/**
Eric Moore993e0da2009-05-18 13:00:45 -0600960 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
961 * @ioc: per adapter object
962 * @id: target id
963 * @lun: lun number
964 * @channel: channel
965 * Context: This function will acquire ioc->scsi_lookup_lock.
966 *
967 * This will search for a matching channel:id:lun in the scsi_lookup array,
968 * returning 1 if found.
969 */
970static u8
971_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
972 unsigned int lun, int channel)
973{
974 u8 found;
975 unsigned long flags;
976 int i;
977
978 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
979 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530980 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600981 if (ioc->scsi_lookup[i].scmd &&
982 (ioc->scsi_lookup[i].scmd->device->id == id &&
983 ioc->scsi_lookup[i].scmd->device->channel == channel &&
984 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
985 found = 1;
986 goto out;
987 }
988 }
989 out:
990 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
991 return found;
992}
993
994/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530995 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -0600996 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530997 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -0600998 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530999 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001000 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301001static struct chain_tracker *
1002_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001003{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301004 struct chain_tracker *chain_req;
1005 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001006
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301007 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1008 if (list_empty(&ioc->free_chain_list)) {
1009 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1010 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
1011 ioc->name);
1012 return NULL;
1013 }
1014 chain_req = list_entry(ioc->free_chain_list.next,
1015 struct chain_tracker, tracker_list);
1016 list_del_init(&chain_req->tracker_list);
1017 list_add_tail(&chain_req->tracker_list,
1018 &ioc->scsi_lookup[smid - 1].chain_list);
1019 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1020 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001021}
1022
1023/**
1024 * _scsih_build_scatter_gather - main sg creation routine
1025 * @ioc: per adapter object
1026 * @scmd: scsi command
1027 * @smid: system request message index
1028 * Context: none.
1029 *
1030 * The main routine that builds scatter gather table from a given
1031 * scsi request sent via the .queuecommand main handler.
1032 *
1033 * Returns 0 success, anything else error
1034 */
1035static int
1036_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1037 struct scsi_cmnd *scmd, u16 smid)
1038{
1039 Mpi2SCSIIORequest_t *mpi_request;
1040 dma_addr_t chain_dma;
1041 struct scatterlist *sg_scmd;
1042 void *sg_local, *chain;
1043 u32 chain_offset;
1044 u32 chain_length;
1045 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001046 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001047 u32 sges_in_segment;
1048 u32 sgl_flags;
1049 u32 sgl_flags_last_element;
1050 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301051 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001052
1053 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1054
1055 /* init scatter gather flags */
1056 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1057 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1058 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1059 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1060 << MPI2_SGE_FLAGS_SHIFT;
1061 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1062 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1063 << MPI2_SGE_FLAGS_SHIFT;
1064 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1065
1066 sg_scmd = scsi_sglist(scmd);
1067 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001068 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001069 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1070 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1071 return -ENOMEM;
1072 }
1073
1074 sg_local = &mpi_request->SGL;
1075 sges_in_segment = ioc->max_sges_in_main_message;
1076 if (sges_left <= sges_in_segment)
1077 goto fill_in_last_segment;
1078
1079 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1080 (sges_in_segment * ioc->sge_size))/4;
1081
1082 /* fill in main message segment when there is a chain following */
1083 while (sges_in_segment) {
1084 if (sges_in_segment == 1)
1085 ioc->base_add_sg_single(sg_local,
1086 sgl_flags_last_element | sg_dma_len(sg_scmd),
1087 sg_dma_address(sg_scmd));
1088 else
1089 ioc->base_add_sg_single(sg_local, sgl_flags |
1090 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1091 sg_scmd = sg_next(sg_scmd);
1092 sg_local += ioc->sge_size;
1093 sges_left--;
1094 sges_in_segment--;
1095 }
1096
1097 /* initializing the chain flags and pointers */
1098 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301099 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1100 if (!chain_req)
1101 return -1;
1102 chain = chain_req->chain_buffer;
1103 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001104 do {
1105 sges_in_segment = (sges_left <=
1106 ioc->max_sges_in_chain_message) ? sges_left :
1107 ioc->max_sges_in_chain_message;
1108 chain_offset = (sges_left == sges_in_segment) ?
1109 0 : (sges_in_segment * ioc->sge_size)/4;
1110 chain_length = sges_in_segment * ioc->sge_size;
1111 if (chain_offset) {
1112 chain_offset = chain_offset <<
1113 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1114 chain_length += ioc->sge_size;
1115 }
1116 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1117 chain_length, chain_dma);
1118 sg_local = chain;
1119 if (!chain_offset)
1120 goto fill_in_last_segment;
1121
1122 /* fill in chain segments */
1123 while (sges_in_segment) {
1124 if (sges_in_segment == 1)
1125 ioc->base_add_sg_single(sg_local,
1126 sgl_flags_last_element |
1127 sg_dma_len(sg_scmd),
1128 sg_dma_address(sg_scmd));
1129 else
1130 ioc->base_add_sg_single(sg_local, sgl_flags |
1131 sg_dma_len(sg_scmd),
1132 sg_dma_address(sg_scmd));
1133 sg_scmd = sg_next(sg_scmd);
1134 sg_local += ioc->sge_size;
1135 sges_left--;
1136 sges_in_segment--;
1137 }
1138
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301139 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1140 if (!chain_req)
1141 return -1;
1142 chain = chain_req->chain_buffer;
1143 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001144 } while (1);
1145
1146
1147 fill_in_last_segment:
1148
1149 /* fill the last segment */
1150 while (sges_left) {
1151 if (sges_left == 1)
1152 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1153 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1154 else
1155 ioc->base_add_sg_single(sg_local, sgl_flags |
1156 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1157 sg_scmd = sg_next(sg_scmd);
1158 sg_local += ioc->sge_size;
1159 sges_left--;
1160 }
1161
1162 return 0;
1163}
1164
1165/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301166 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001167 * @sdev: scsi device struct
1168 * @qdepth: requested queue depth
1169 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301170 *
1171 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001172 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301173static void
1174_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001175{
1176 struct Scsi_Host *shost = sdev->host;
1177 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301178 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1179 struct MPT2SAS_DEVICE *sas_device_priv_data;
1180 struct MPT2SAS_TARGET *sas_target_priv_data;
1181 struct _sas_device *sas_device;
1182 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001183
1184 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301185
1186 /* limit max device queue for SATA to 32 */
1187 sas_device_priv_data = sdev->hostdata;
1188 if (!sas_device_priv_data)
1189 goto not_sata;
1190 sas_target_priv_data = sas_device_priv_data->sas_target;
1191 if (!sas_target_priv_data)
1192 goto not_sata;
1193 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1194 goto not_sata;
1195 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1196 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1197 sas_device_priv_data->sas_target->sas_address);
1198 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1199 if (sas_device && sas_device->device_info &
1200 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1201 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1202
1203 not_sata:
1204
Eric Moore635374e2009-03-09 01:21:12 -06001205 if (!sdev->tagged_supported)
1206 max_depth = 1;
1207 if (qdepth > max_depth)
1208 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301209 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1210}
1211
1212/**
1213 * _scsih_change_queue_depth - setting device queue depth
1214 * @sdev: scsi device struct
1215 * @qdepth: requested queue depth
1216 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1217 * (see include/scsi/scsi_host.h for definition)
1218 *
1219 * Returns queue depth.
1220 */
1221static int
1222_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1223{
1224 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1225 _scsih_adjust_queue_depth(sdev, qdepth);
1226 else if (reason == SCSI_QDEPTH_QFULL)
1227 scsi_track_queue_full(sdev, qdepth);
1228 else
1229 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001230
1231 if (sdev->inquiry_len > 7)
1232 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1233 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1234 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1235 sdev->ordered_tags, sdev->scsi_level,
1236 (sdev->inquiry[7] & 2) >> 1);
1237
1238 return sdev->queue_depth;
1239}
1240
1241/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301242 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001243 * @sdev: scsi device struct
1244 * @tag_type: requested tag type
1245 *
1246 * Returns queue tag type.
1247 */
1248static int
Eric Moored5d135b2009-05-18 13:02:08 -06001249_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001250{
1251 if (sdev->tagged_supported) {
1252 scsi_set_tag_type(sdev, tag_type);
1253 if (tag_type)
1254 scsi_activate_tcq(sdev, sdev->queue_depth);
1255 else
1256 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1257 } else
1258 tag_type = 0;
1259
1260 return tag_type;
1261}
1262
1263/**
Eric Moored5d135b2009-05-18 13:02:08 -06001264 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001265 * @starget: scsi target struct
1266 *
1267 * Returns 0 if ok. Any other return is assumed to be an error and
1268 * the device is ignored.
1269 */
1270static int
Eric Moored5d135b2009-05-18 13:02:08 -06001271_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001272{
1273 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1274 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1275 struct MPT2SAS_TARGET *sas_target_priv_data;
1276 struct _sas_device *sas_device;
1277 struct _raid_device *raid_device;
1278 unsigned long flags;
1279 struct sas_rphy *rphy;
1280
1281 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1282 if (!sas_target_priv_data)
1283 return -ENOMEM;
1284
1285 starget->hostdata = sas_target_priv_data;
1286 sas_target_priv_data->starget = starget;
1287 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1288
1289 /* RAID volumes */
1290 if (starget->channel == RAID_CHANNEL) {
1291 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1292 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1293 starget->channel);
1294 if (raid_device) {
1295 sas_target_priv_data->handle = raid_device->handle;
1296 sas_target_priv_data->sas_address = raid_device->wwid;
1297 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301298 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001299 raid_device->starget = starget;
1300 }
1301 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1302 return 0;
1303 }
1304
1305 /* sas/sata devices */
1306 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1307 rphy = dev_to_rphy(starget->dev.parent);
1308 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1309 rphy->identify.sas_address);
1310
1311 if (sas_device) {
1312 sas_target_priv_data->handle = sas_device->handle;
1313 sas_target_priv_data->sas_address = sas_device->sas_address;
1314 sas_device->starget = starget;
1315 sas_device->id = starget->id;
1316 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301317 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001318 sas_target_priv_data->flags |=
1319 MPT_TARGET_FLAGS_RAID_COMPONENT;
1320 }
1321 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1322
1323 return 0;
1324}
1325
1326/**
Eric Moored5d135b2009-05-18 13:02:08 -06001327 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001328 * @starget: scsi target struct
1329 *
1330 * Returns nothing.
1331 */
1332static void
Eric Moored5d135b2009-05-18 13:02:08 -06001333_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001334{
1335 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1336 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1337 struct MPT2SAS_TARGET *sas_target_priv_data;
1338 struct _sas_device *sas_device;
1339 struct _raid_device *raid_device;
1340 unsigned long flags;
1341 struct sas_rphy *rphy;
1342
1343 sas_target_priv_data = starget->hostdata;
1344 if (!sas_target_priv_data)
1345 return;
1346
1347 if (starget->channel == RAID_CHANNEL) {
1348 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1349 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1350 starget->channel);
1351 if (raid_device) {
1352 raid_device->starget = NULL;
1353 raid_device->sdev = NULL;
1354 }
1355 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1356 goto out;
1357 }
1358
1359 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1360 rphy = dev_to_rphy(starget->dev.parent);
1361 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1362 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001363 if (sas_device && (sas_device->starget == starget) &&
1364 (sas_device->id == starget->id) &&
1365 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001366 sas_device->starget = NULL;
1367
1368 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1369
1370 out:
1371 kfree(sas_target_priv_data);
1372 starget->hostdata = NULL;
1373}
1374
1375/**
Eric Moored5d135b2009-05-18 13:02:08 -06001376 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001377 * @sdev: scsi device struct
1378 *
1379 * Returns 0 if ok. Any other return is assumed to be an error and
1380 * the device is ignored.
1381 */
1382static int
Eric Moored5d135b2009-05-18 13:02:08 -06001383_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001384{
1385 struct Scsi_Host *shost;
1386 struct MPT2SAS_ADAPTER *ioc;
1387 struct MPT2SAS_TARGET *sas_target_priv_data;
1388 struct MPT2SAS_DEVICE *sas_device_priv_data;
1389 struct scsi_target *starget;
1390 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001391 unsigned long flags;
1392
1393 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1394 if (!sas_device_priv_data)
1395 return -ENOMEM;
1396
1397 sas_device_priv_data->lun = sdev->lun;
1398 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1399
1400 starget = scsi_target(sdev);
1401 sas_target_priv_data = starget->hostdata;
1402 sas_target_priv_data->num_luns++;
1403 sas_device_priv_data->sas_target = sas_target_priv_data;
1404 sdev->hostdata = sas_device_priv_data;
1405 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1406 sdev->no_uld_attach = 1;
1407
1408 shost = dev_to_shost(&starget->dev);
1409 ioc = shost_priv(shost);
1410 if (starget->channel == RAID_CHANNEL) {
1411 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1412 raid_device = _scsih_raid_device_find_by_id(ioc,
1413 starget->id, starget->channel);
1414 if (raid_device)
1415 raid_device->sdev = sdev; /* raid is single lun */
1416 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001417 }
1418
Eric Moore635374e2009-03-09 01:21:12 -06001419 return 0;
1420}
1421
1422/**
Eric Moored5d135b2009-05-18 13:02:08 -06001423 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001424 * @sdev: scsi device struct
1425 *
1426 * Returns nothing.
1427 */
1428static void
Eric Moored5d135b2009-05-18 13:02:08 -06001429_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001430{
1431 struct MPT2SAS_TARGET *sas_target_priv_data;
1432 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301433 struct Scsi_Host *shost;
1434 struct MPT2SAS_ADAPTER *ioc;
1435 struct _sas_device *sas_device;
1436 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001437
1438 if (!sdev->hostdata)
1439 return;
1440
1441 starget = scsi_target(sdev);
1442 sas_target_priv_data = starget->hostdata;
1443 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301444
1445 shost = dev_to_shost(&starget->dev);
1446 ioc = shost_priv(shost);
1447
1448 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1449 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1450 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1451 sas_target_priv_data->sas_address);
1452 if (sas_device)
1453 sas_device->starget = NULL;
1454 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1455 }
1456
Eric Moore635374e2009-03-09 01:21:12 -06001457 kfree(sdev->hostdata);
1458 sdev->hostdata = NULL;
1459}
1460
1461/**
Eric Moored5d135b2009-05-18 13:02:08 -06001462 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001463 * @ioc: per adapter object
1464 * @sas_device: the sas_device object
1465 * @sdev: scsi device struct
1466 */
1467static void
Eric Moored5d135b2009-05-18 13:02:08 -06001468_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001469 struct _sas_device *sas_device, struct scsi_device *sdev)
1470{
1471 Mpi2ConfigReply_t mpi_reply;
1472 Mpi2SasDevicePage0_t sas_device_pg0;
1473 u32 ioc_status;
1474 u16 flags;
1475 u32 device_info;
1476
1477 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1478 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1479 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1480 ioc->name, __FILE__, __LINE__, __func__);
1481 return;
1482 }
1483
1484 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1485 MPI2_IOCSTATUS_MASK;
1486 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1487 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1488 ioc->name, __FILE__, __LINE__, __func__);
1489 return;
1490 }
1491
1492 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301493 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001494
1495 sdev_printk(KERN_INFO, sdev,
1496 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1497 "sw_preserve(%s)\n",
1498 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1499 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1500 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1501 "n",
1502 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1503 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1504 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1505}
1506
1507/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301508 * _scsih_is_raid - return boolean indicating device is raid volume
1509 * @dev the device struct object
1510 */
1511static int
1512_scsih_is_raid(struct device *dev)
1513{
1514 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301515 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301516
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301517 if (ioc->is_warpdrive)
1518 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301519 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1520}
1521
1522/**
1523 * _scsih_get_resync - get raid volume resync percent complete
1524 * @dev the device struct object
1525 */
1526static void
1527_scsih_get_resync(struct device *dev)
1528{
1529 struct scsi_device *sdev = to_scsi_device(dev);
1530 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1531 static struct _raid_device *raid_device;
1532 unsigned long flags;
1533 Mpi2RaidVolPage0_t vol_pg0;
1534 Mpi2ConfigReply_t mpi_reply;
1535 u32 volume_status_flags;
1536 u8 percent_complete = 0;
1537
1538 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1539 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1540 sdev->channel);
1541 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1542
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301543 if (!raid_device || ioc->is_warpdrive)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301544 goto out;
1545
1546 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1547 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1548 sizeof(Mpi2RaidVolPage0_t))) {
1549 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1550 ioc->name, __FILE__, __LINE__, __func__);
1551 goto out;
1552 }
1553
1554 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1555 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1556 percent_complete = raid_device->percent_complete;
1557 out:
1558 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1559}
1560
1561/**
1562 * _scsih_get_state - get raid volume level
1563 * @dev the device struct object
1564 */
1565static void
1566_scsih_get_state(struct device *dev)
1567{
1568 struct scsi_device *sdev = to_scsi_device(dev);
1569 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1570 static struct _raid_device *raid_device;
1571 unsigned long flags;
1572 Mpi2RaidVolPage0_t vol_pg0;
1573 Mpi2ConfigReply_t mpi_reply;
1574 u32 volstate;
1575 enum raid_state state = RAID_STATE_UNKNOWN;
1576
1577 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1578 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1579 sdev->channel);
1580 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1581
1582 if (!raid_device)
1583 goto out;
1584
1585 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1586 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1587 sizeof(Mpi2RaidVolPage0_t))) {
1588 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1589 ioc->name, __FILE__, __LINE__, __func__);
1590 goto out;
1591 }
1592
1593 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1594 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1595 state = RAID_STATE_RESYNCING;
1596 goto out;
1597 }
1598
1599 switch (vol_pg0.VolumeState) {
1600 case MPI2_RAID_VOL_STATE_OPTIMAL:
1601 case MPI2_RAID_VOL_STATE_ONLINE:
1602 state = RAID_STATE_ACTIVE;
1603 break;
1604 case MPI2_RAID_VOL_STATE_DEGRADED:
1605 state = RAID_STATE_DEGRADED;
1606 break;
1607 case MPI2_RAID_VOL_STATE_FAILED:
1608 case MPI2_RAID_VOL_STATE_MISSING:
1609 state = RAID_STATE_OFFLINE;
1610 break;
1611 }
1612 out:
1613 raid_set_state(mpt2sas_raid_template, dev, state);
1614}
1615
1616/**
1617 * _scsih_set_level - set raid level
1618 * @sdev: scsi device struct
1619 * @raid_device: raid_device object
1620 */
1621static void
1622_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1623{
1624 enum raid_level level = RAID_LEVEL_UNKNOWN;
1625
1626 switch (raid_device->volume_type) {
1627 case MPI2_RAID_VOL_TYPE_RAID0:
1628 level = RAID_LEVEL_0;
1629 break;
1630 case MPI2_RAID_VOL_TYPE_RAID10:
1631 level = RAID_LEVEL_10;
1632 break;
1633 case MPI2_RAID_VOL_TYPE_RAID1E:
1634 level = RAID_LEVEL_1E;
1635 break;
1636 case MPI2_RAID_VOL_TYPE_RAID1:
1637 level = RAID_LEVEL_1;
1638 break;
1639 }
1640
1641 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1642}
1643
1644/**
Eric Moore635374e2009-03-09 01:21:12 -06001645 * _scsih_get_volume_capabilities - volume capabilities
1646 * @ioc: per adapter object
1647 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301648 *
1649 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001650 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301651static int
Eric Moore635374e2009-03-09 01:21:12 -06001652_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1653 struct _raid_device *raid_device)
1654{
1655 Mpi2RaidVolPage0_t *vol_pg0;
1656 Mpi2RaidPhysDiskPage0_t pd_pg0;
1657 Mpi2SasDevicePage0_t sas_device_pg0;
1658 Mpi2ConfigReply_t mpi_reply;
1659 u16 sz;
1660 u8 num_pds;
1661
1662 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1663 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301664 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1665 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1666 __func__));
1667 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001668 }
1669
1670 raid_device->num_pds = num_pds;
1671 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1672 sizeof(Mpi2RaidVol0PhysDisk_t));
1673 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1674 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301675 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1676 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1677 __func__));
1678 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001679 }
1680
1681 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1682 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301683 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1684 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1685 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001686 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301687 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001688 }
1689
1690 raid_device->volume_type = vol_pg0->VolumeType;
1691
1692 /* figure out what the underlying devices are by
1693 * obtaining the device_info bits for the 1st device
1694 */
1695 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1696 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1697 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1698 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1699 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1700 le16_to_cpu(pd_pg0.DevHandle)))) {
1701 raid_device->device_info =
1702 le32_to_cpu(sas_device_pg0.DeviceInfo);
1703 }
1704 }
1705
1706 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301707 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001708}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301709/**
1710 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1711 * @ioc: per adapter object
1712 */
1713static void
1714_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1715{
1716 Mpi2RaidVolPage1_t vol_pg1;
1717 Mpi2ConfigReply_t mpi_reply;
1718 struct _raid_device *raid_device;
1719 u16 handle;
1720 u16 ioc_status;
1721
1722 handle = 0xFFFF;
1723 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1724 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1725 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1726 MPI2_IOCSTATUS_MASK;
1727 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1728 break;
1729 handle = le16_to_cpu(vol_pg1.DevHandle);
1730 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1731 if (raid_device)
1732 raid_device->direct_io_enabled = 0;
1733 }
1734 return;
1735}
1736
1737
1738/**
1739 * _scsih_get_num_volumes - Get number of volumes in the ioc
1740 * @ioc: per adapter object
1741 */
1742static u8
1743_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1744{
1745 Mpi2RaidVolPage1_t vol_pg1;
1746 Mpi2ConfigReply_t mpi_reply;
1747 u16 handle;
1748 u8 vol_cnt = 0;
1749 u16 ioc_status;
1750
1751 handle = 0xFFFF;
1752 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1753 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1754 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1755 MPI2_IOCSTATUS_MASK;
1756 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1757 break;
1758 vol_cnt++;
1759 handle = le16_to_cpu(vol_pg1.DevHandle);
1760 }
1761 return vol_cnt;
1762}
1763
1764
1765/**
1766 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1767 * @ioc: per adapter object
1768 * @raid_device: the raid_device object
1769 */
1770static void
1771_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1772 struct _raid_device *raid_device)
1773{
1774 Mpi2RaidVolPage0_t *vol_pg0;
1775 Mpi2RaidPhysDiskPage0_t pd_pg0;
1776 Mpi2ConfigReply_t mpi_reply;
1777 u16 sz;
1778 u8 num_pds, count;
1779 u64 mb = 1024 * 1024;
1780 u64 tb_2 = 2 * mb * mb;
1781 u64 capacity;
1782 u32 stripe_sz;
1783 u8 i, stripe_exp;
1784
1785 if (!ioc->is_warpdrive)
1786 return;
1787
1788 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1789 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1790 "globally as drives are exposed\n", ioc->name);
1791 return;
1792 }
1793 if (_scsih_get_num_volumes(ioc) > 1) {
1794 _scsih_disable_ddio(ioc);
1795 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1796 "globally as number of drives > 1\n", ioc->name);
1797 return;
1798 }
1799 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1800 &num_pds)) || !num_pds) {
1801 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1802 "Failure in computing number of drives\n", ioc->name);
1803 return;
1804 }
1805
1806 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1807 sizeof(Mpi2RaidVol0PhysDisk_t));
1808 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1809 if (!vol_pg0) {
1810 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1811 "Memory allocation failure for RVPG0\n", ioc->name);
1812 return;
1813 }
1814
1815 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1816 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1817 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1818 "Failure in retrieving RVPG0\n", ioc->name);
1819 kfree(vol_pg0);
1820 return;
1821 }
1822
1823 /*
1824 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1825 * assumed for WARPDRIVE, disable direct I/O
1826 */
1827 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1828 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1829 "for the drive with handle(0x%04x): num_mem=%d, "
1830 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1831 num_pds, MPT_MAX_WARPDRIVE_PDS);
1832 kfree(vol_pg0);
1833 return;
1834 }
1835 for (count = 0; count < num_pds; count++) {
1836 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1837 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1838 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1839 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1840 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1841 "disabled for the drive with handle(0x%04x) member"
1842 "handle retrieval failed for member number=%d\n",
1843 ioc->name, raid_device->handle,
1844 vol_pg0->PhysDisk[count].PhysDiskNum);
1845 goto out_error;
1846 }
1847 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1848 }
1849
1850 /*
1851 * Assumption for WD: Direct I/O is not supported if the volume is
1852 * not RAID0, if the stripe size is not 64KB, if the block size is
1853 * not 512 and if the volume size is >2TB
1854 */
1855 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1856 le16_to_cpu(vol_pg0->BlockSize) != 512) {
1857 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1858 "for the drive with handle(0x%04x): type=%d, "
1859 "s_sz=%uK, blk_size=%u\n", ioc->name,
1860 raid_device->handle, raid_device->volume_type,
1861 le32_to_cpu(vol_pg0->StripeSize)/2,
1862 le16_to_cpu(vol_pg0->BlockSize));
1863 goto out_error;
1864 }
1865
1866 capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1867 (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1868
1869 if (capacity > tb_2) {
1870 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1871 "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1872 ioc->name, raid_device->handle);
1873 goto out_error;
1874 }
1875
1876 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1877 stripe_exp = 0;
1878 for (i = 0; i < 32; i++) {
1879 if (stripe_sz & 1)
1880 break;
1881 stripe_exp++;
1882 stripe_sz >>= 1;
1883 }
1884 if (i == 32) {
1885 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1886 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1887 ioc->name, raid_device->handle,
1888 le32_to_cpu(vol_pg0->StripeSize)/2);
1889 goto out_error;
1890 }
1891 raid_device->stripe_exponent = stripe_exp;
1892 raid_device->direct_io_enabled = 1;
1893
1894 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1895 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1896 /*
1897 * WARPDRIVE: Though the following fields are not used for direct IO,
1898 * stored for future purpose:
1899 */
1900 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1901 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1902 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1903
1904
1905 kfree(vol_pg0);
1906 return;
1907
1908out_error:
1909 raid_device->direct_io_enabled = 0;
1910 for (count = 0; count < num_pds; count++)
1911 raid_device->pd_handle[count] = 0;
1912 kfree(vol_pg0);
1913 return;
1914}
Eric Moore635374e2009-03-09 01:21:12 -06001915
1916/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301917 * _scsih_enable_tlr - setting TLR flags
1918 * @ioc: per adapter object
1919 * @sdev: scsi device struct
1920 *
1921 * Enabling Transaction Layer Retries for tape devices when
1922 * vpd page 0x90 is present
1923 *
1924 */
1925static void
1926_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1927{
1928 /* only for TAPE */
1929 if (sdev->type != TYPE_TAPE)
1930 return;
1931
1932 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1933 return;
1934
1935 sas_enable_tlr(sdev);
1936 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1937 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1938 return;
1939
1940}
1941
1942/**
Eric Moored5d135b2009-05-18 13:02:08 -06001943 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001944 * @sdev: scsi device struct
1945 *
1946 * Returns 0 if ok. Any other return is assumed to be an error and
1947 * the device is ignored.
1948 */
1949static int
Eric Moored5d135b2009-05-18 13:02:08 -06001950_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001951{
1952 struct Scsi_Host *shost = sdev->host;
1953 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1954 struct MPT2SAS_DEVICE *sas_device_priv_data;
1955 struct MPT2SAS_TARGET *sas_target_priv_data;
1956 struct _sas_device *sas_device;
1957 struct _raid_device *raid_device;
1958 unsigned long flags;
1959 int qdepth;
1960 u8 ssp_target = 0;
1961 char *ds = "";
1962 char *r_level = "";
1963
1964 qdepth = 1;
1965 sas_device_priv_data = sdev->hostdata;
1966 sas_device_priv_data->configured_lun = 1;
1967 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1968 sas_target_priv_data = sas_device_priv_data->sas_target;
1969
1970 /* raid volume handling */
1971 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1972
1973 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1974 raid_device = _scsih_raid_device_find_by_handle(ioc,
1975 sas_target_priv_data->handle);
1976 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1977 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301978 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1979 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1980 __LINE__, __func__));
1981 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001982 }
1983
1984 _scsih_get_volume_capabilities(ioc, raid_device);
1985
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301986 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1987 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1988 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1989 __LINE__, __func__));
1990 return 1;
1991 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301992 /*
1993 * WARPDRIVE: Initialize the required data for Direct IO
1994 */
1995 _scsih_init_warpdrive_properties(ioc, raid_device);
1996
Eric Moore635374e2009-03-09 01:21:12 -06001997 /* RAID Queue Depth Support
1998 * IS volume = underlying qdepth of drive type, either
1999 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2000 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2001 */
2002 if (raid_device->device_info &
2003 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2004 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2005 ds = "SSP";
2006 } else {
2007 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2008 if (raid_device->device_info &
2009 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2010 ds = "SATA";
2011 else
2012 ds = "STP";
2013 }
2014
2015 switch (raid_device->volume_type) {
2016 case MPI2_RAID_VOL_TYPE_RAID0:
2017 r_level = "RAID0";
2018 break;
2019 case MPI2_RAID_VOL_TYPE_RAID1E:
2020 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302021 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302022 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302023 MFG10_GF0_R10_DISPLAY) &&
2024 !(raid_device->num_pds % 2))
2025 r_level = "RAID10";
2026 else
2027 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002028 break;
2029 case MPI2_RAID_VOL_TYPE_RAID1:
2030 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2031 r_level = "RAID1";
2032 break;
2033 case MPI2_RAID_VOL_TYPE_RAID10:
2034 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2035 r_level = "RAID10";
2036 break;
2037 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2038 default:
2039 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2040 r_level = "RAIDX";
2041 break;
2042 }
2043
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302044 if (!ioc->hide_ir_msg)
2045 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2046 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2047 r_level, raid_device->handle,
2048 (unsigned long long)raid_device->wwid,
2049 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002050 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302051 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302052 if (!ioc->is_warpdrive)
2053 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06002054 return 0;
2055 }
2056
2057 /* non-raid handling */
2058 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2059 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2060 sas_device_priv_data->sas_target->sas_address);
2061 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2062 if (sas_device) {
2063 if (sas_target_priv_data->flags &
2064 MPT_TARGET_FLAGS_RAID_COMPONENT) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302065 if (mpt2sas_config_get_volume_handle(ioc,
2066 sas_device->handle, &sas_device->volume_handle)) {
2067 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2068 "failure at %s:%d/%s()!\n", ioc->name,
2069 __FILE__, __LINE__, __func__));
2070 return 1;
2071 }
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05302072 if (sas_device->volume_handle &&
2073 mpt2sas_config_get_volume_wwid(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06002074 sas_device->volume_handle,
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302075 &sas_device->volume_wwid)) {
2076 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2077 "failure at %s:%d/%s()!\n", ioc->name,
2078 __FILE__, __LINE__, __func__));
2079 return 1;
2080 }
Eric Moore635374e2009-03-09 01:21:12 -06002081 }
2082 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2083 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2084 ssp_target = 1;
2085 ds = "SSP";
2086 } else {
2087 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2088 if (sas_device->device_info &
2089 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2090 ds = "STP";
2091 else if (sas_device->device_info &
2092 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2093 ds = "SATA";
2094 }
2095
2096 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302097 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002098 ds, sas_device->handle,
2099 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302100 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06002101 (unsigned long long)sas_device->device_name);
2102 sdev_printk(KERN_INFO, sdev, "%s: "
2103 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2104 (unsigned long long) sas_device->enclosure_logical_id,
2105 sas_device->slot);
2106
2107 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06002108 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302109 } else {
2110 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2111 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
2112 __func__));
2113 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002114 }
2115
Mike Christiee881a172009-10-15 17:46:39 -07002116 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002117
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302118 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002119 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302120 _scsih_enable_tlr(ioc, sdev);
2121 }
Eric Moore635374e2009-03-09 01:21:12 -06002122 return 0;
2123}
2124
2125/**
Eric Moored5d135b2009-05-18 13:02:08 -06002126 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002127 * @sdev: scsi device struct
2128 * @bdev: pointer to block device context
2129 * @capacity: device size (in 512 byte sectors)
2130 * @params: three element array to place output:
2131 * params[0] number of heads (max 255)
2132 * params[1] number of sectors (max 63)
2133 * params[2] number of cylinders
2134 *
2135 * Return nothing.
2136 */
2137static int
Eric Moored5d135b2009-05-18 13:02:08 -06002138_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002139 sector_t capacity, int params[])
2140{
2141 int heads;
2142 int sectors;
2143 sector_t cylinders;
2144 ulong dummy;
2145
2146 heads = 64;
2147 sectors = 32;
2148
2149 dummy = heads * sectors;
2150 cylinders = capacity;
2151 sector_div(cylinders, dummy);
2152
2153 /*
2154 * Handle extended translation size for logical drives
2155 * > 1Gb
2156 */
2157 if ((ulong)capacity >= 0x200000) {
2158 heads = 255;
2159 sectors = 63;
2160 dummy = heads * sectors;
2161 cylinders = capacity;
2162 sector_div(cylinders, dummy);
2163 }
2164
2165 /* return result */
2166 params[0] = heads;
2167 params[1] = sectors;
2168 params[2] = cylinders;
2169
2170 return 0;
2171}
2172
2173/**
2174 * _scsih_response_code - translation of device response code
2175 * @ioc: per adapter object
2176 * @response_code: response code returned by the device
2177 *
2178 * Return nothing.
2179 */
2180static void
2181_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2182{
2183 char *desc;
2184
2185 switch (response_code) {
2186 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2187 desc = "task management request completed";
2188 break;
2189 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2190 desc = "invalid frame";
2191 break;
2192 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2193 desc = "task management request not supported";
2194 break;
2195 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2196 desc = "task management request failed";
2197 break;
2198 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2199 desc = "task management request succeeded";
2200 break;
2201 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2202 desc = "invalid lun";
2203 break;
2204 case 0xA:
2205 desc = "overlapped tag attempted";
2206 break;
2207 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2208 desc = "task queued, however not sent to target";
2209 break;
2210 default:
2211 desc = "unknown";
2212 break;
2213 }
2214 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2215 ioc->name, response_code, desc);
2216}
2217
2218/**
Eric Moored5d135b2009-05-18 13:02:08 -06002219 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002220 * @ioc: per adapter object
2221 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302222 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002223 * @reply: reply message frame(lower 32bit addr)
2224 * Context: none.
2225 *
2226 * The callback handler when using scsih_issue_tm.
2227 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302228 * Return 1 meaning mf should be freed from _base_interrupt
2229 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002230 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302231static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302232_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002233{
2234 MPI2DefaultReply_t *mpi_reply;
2235
2236 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302237 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002238 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302239 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302240 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002241 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2242 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2243 if (mpi_reply) {
2244 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2245 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2246 }
2247 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2248 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302249 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002250}
2251
2252/**
2253 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2254 * @ioc: per adapter object
2255 * @handle: device handle
2256 *
2257 * During taskmangement request, we need to freeze the device queue.
2258 */
2259void
2260mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2261{
2262 struct MPT2SAS_DEVICE *sas_device_priv_data;
2263 struct scsi_device *sdev;
2264 u8 skip = 0;
2265
2266 shost_for_each_device(sdev, ioc->shost) {
2267 if (skip)
2268 continue;
2269 sas_device_priv_data = sdev->hostdata;
2270 if (!sas_device_priv_data)
2271 continue;
2272 if (sas_device_priv_data->sas_target->handle == handle) {
2273 sas_device_priv_data->sas_target->tm_busy = 1;
2274 skip = 1;
2275 ioc->ignore_loginfos = 1;
2276 }
2277 }
2278}
2279
2280/**
2281 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2282 * @ioc: per adapter object
2283 * @handle: device handle
2284 *
2285 * During taskmangement request, we need to freeze the device queue.
2286 */
2287void
2288mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2289{
2290 struct MPT2SAS_DEVICE *sas_device_priv_data;
2291 struct scsi_device *sdev;
2292 u8 skip = 0;
2293
2294 shost_for_each_device(sdev, ioc->shost) {
2295 if (skip)
2296 continue;
2297 sas_device_priv_data = sdev->hostdata;
2298 if (!sas_device_priv_data)
2299 continue;
2300 if (sas_device_priv_data->sas_target->handle == handle) {
2301 sas_device_priv_data->sas_target->tm_busy = 0;
2302 skip = 1;
2303 ioc->ignore_loginfos = 0;
2304 }
2305 }
2306}
2307
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302308
Eric Moore635374e2009-03-09 01:21:12 -06002309/**
2310 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2311 * @ioc: per adapter struct
2312 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302313 * @channel: the channel assigned by the OS
2314 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002315 * @lun: lun number
2316 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2317 * @smid_task: smid assigned to the task
2318 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302319 * @serial_number: the serial_number from scmd
2320 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302321 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002322 *
2323 * A generic API for sending task management requests to firmware.
2324 *
Eric Moore635374e2009-03-09 01:21:12 -06002325 * The callback index is set inside `ioc->tm_cb_idx`.
2326 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302327 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002328 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302329int
2330mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2331 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302332 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002333{
2334 Mpi2SCSITaskManagementRequest_t *mpi_request;
2335 Mpi2SCSITaskManagementReply_t *mpi_reply;
2336 u16 smid = 0;
2337 u32 ioc_state;
2338 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302339 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302340 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002341
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302342 if (m_type == TM_MUTEX_ON)
2343 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302344 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2345 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2346 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302347 rc = FAILED;
2348 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302349 }
2350
Eric Moore3cb54692010-07-08 14:44:34 -06002351 if (ioc->shost_recovery || ioc->remove_host ||
2352 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002353 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2354 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302355 rc = FAILED;
2356 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002357 }
Eric Moore635374e2009-03-09 01:21:12 -06002358
2359 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2360 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302361 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002362 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302363 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302364 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302365 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302366 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002367 }
2368
2369 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2370 mpt2sas_base_fault_info(ioc, ioc_state &
2371 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302372 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302373 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302374 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302375 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002376 }
2377
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302378 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002379 if (!smid) {
2380 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2381 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302382 rc = FAILED;
2383 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002384 }
2385
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302386 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2387 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2388
Eric Moore635374e2009-03-09 01:21:12 -06002389 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302390 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2391 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002392 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2393 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2394 ioc->tm_cmds.smid = smid;
2395 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302396 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002397 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2398 mpi_request->DevHandle = cpu_to_le16(handle);
2399 mpi_request->TaskType = type;
2400 mpi_request->TaskMID = cpu_to_le16(smid_task);
2401 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2402 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302403 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302404 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002405 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002406 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2407 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2408 ioc->name, __func__);
2409 _debug_dump_mf(mpi_request,
2410 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302411 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302412 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302413 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302414 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302415 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2416 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2417 goto err_out;
2418 }
Eric Moore635374e2009-03-09 01:21:12 -06002419 }
2420
2421 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2422 mpi_reply = ioc->tm_cmds.reply;
2423 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2424 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2425 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2426 le32_to_cpu(mpi_reply->IOCLogInfo),
2427 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302428 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002429 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302430 if (mpi_reply->IOCStatus)
2431 _debug_dump_mf(mpi_request,
2432 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2433 }
Eric Moore635374e2009-03-09 01:21:12 -06002434 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302435
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302436 switch (type) {
2437 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302438 rc = SUCCESS;
2439 if (scsi_lookup->scmd == NULL)
2440 break;
2441 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302442 break;
2443
2444 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2445 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2446 rc = FAILED;
2447 else
2448 rc = SUCCESS;
2449 break;
2450
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302451 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302452 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2453 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2454 rc = FAILED;
2455 else
2456 rc = SUCCESS;
2457 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302458 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2459 rc = SUCCESS;
2460 break;
2461 default:
2462 rc = FAILED;
2463 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302464 }
2465
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302466 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2467 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302468 if (m_type == TM_MUTEX_ON)
2469 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302470
2471 return rc;
2472
2473 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302474 if (m_type == TM_MUTEX_ON)
2475 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302476 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002477}
2478
2479/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302480 * _scsih_tm_display_info - displays info about the device
2481 * @ioc: per adapter struct
2482 * @scmd: pointer to scsi command object
2483 *
2484 * Called by task management callback handlers.
2485 */
2486static void
2487_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2488{
2489 struct scsi_target *starget = scmd->device->sdev_target;
2490 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2491 struct _sas_device *sas_device = NULL;
2492 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302493 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302494
2495 if (!priv_target)
2496 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302497 if (ioc->hide_ir_msg)
2498 device_str = "WarpDrive";
2499 else
2500 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302501
2502 scsi_print_command(scmd);
2503 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302504 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2505 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2506 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302507 } else {
2508 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2509 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2510 priv_target->sas_address);
2511 if (sas_device) {
2512 if (priv_target->flags &
2513 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2514 starget_printk(KERN_INFO, starget,
2515 "volume handle(0x%04x), "
2516 "volume wwid(0x%016llx)\n",
2517 sas_device->volume_handle,
2518 (unsigned long long)sas_device->volume_wwid);
2519 }
2520 starget_printk(KERN_INFO, starget,
2521 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2522 sas_device->handle,
2523 (unsigned long long)sas_device->sas_address,
2524 sas_device->phy);
2525 starget_printk(KERN_INFO, starget,
2526 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2527 (unsigned long long)sas_device->enclosure_logical_id,
2528 sas_device->slot);
2529 }
2530 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2531 }
2532}
2533
2534/**
Eric Moored5d135b2009-05-18 13:02:08 -06002535 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302536 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002537 *
2538 * Returns SUCCESS if command aborted else FAILED
2539 */
2540static int
Eric Moored5d135b2009-05-18 13:02:08 -06002541_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002542{
2543 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2544 struct MPT2SAS_DEVICE *sas_device_priv_data;
2545 u16 smid;
2546 u16 handle;
2547 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002548
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302549 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2550 "scmd(%p)\n", scmd);
2551 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002552
2553 sas_device_priv_data = scmd->device->hostdata;
2554 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302555 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2556 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002557 scmd->result = DID_NO_CONNECT << 16;
2558 scmd->scsi_done(scmd);
2559 r = SUCCESS;
2560 goto out;
2561 }
2562
2563 /* search for the command */
2564 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2565 if (!smid) {
2566 scmd->result = DID_RESET << 16;
2567 r = SUCCESS;
2568 goto out;
2569 }
2570
2571 /* for hidden raid components and volumes this is not supported */
2572 if (sas_device_priv_data->sas_target->flags &
2573 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2574 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2575 scmd->result = DID_RESET << 16;
2576 r = FAILED;
2577 goto out;
2578 }
2579
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302580 mpt2sas_halt_firmware(ioc);
2581
Eric Moore635374e2009-03-09 01:21:12 -06002582 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302583 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2584 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302585 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2586 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002587
2588 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302589 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2590 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002591 return r;
2592}
2593
Eric Moore635374e2009-03-09 01:21:12 -06002594/**
Eric Moored5d135b2009-05-18 13:02:08 -06002595 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302596 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002597 *
2598 * Returns SUCCESS if command aborted else FAILED
2599 */
2600static int
Eric Moored5d135b2009-05-18 13:02:08 -06002601_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002602{
2603 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2604 struct MPT2SAS_DEVICE *sas_device_priv_data;
2605 struct _sas_device *sas_device;
2606 unsigned long flags;
2607 u16 handle;
2608 int r;
2609
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302610 struct scsi_target *starget = scmd->device->sdev_target;
2611
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302612 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302613 "scmd(%p)\n", scmd);
2614 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002615
2616 sas_device_priv_data = scmd->device->hostdata;
2617 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302618 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302619 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002620 scmd->result = DID_NO_CONNECT << 16;
2621 scmd->scsi_done(scmd);
2622 r = SUCCESS;
2623 goto out;
2624 }
2625
2626 /* for hidden raid components obtain the volume_handle */
2627 handle = 0;
2628 if (sas_device_priv_data->sas_target->flags &
2629 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2630 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2631 sas_device = _scsih_sas_device_find_by_handle(ioc,
2632 sas_device_priv_data->sas_target->handle);
2633 if (sas_device)
2634 handle = sas_device->volume_handle;
2635 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2636 } else
2637 handle = sas_device_priv_data->sas_target->handle;
2638
2639 if (!handle) {
2640 scmd->result = DID_RESET << 16;
2641 r = FAILED;
2642 goto out;
2643 }
2644
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302645 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2646 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302647 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2648 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002649
2650 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302651 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2652 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002653 return r;
2654}
2655
2656/**
Eric Moored5d135b2009-05-18 13:02:08 -06002657 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302658 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002659 *
2660 * Returns SUCCESS if command aborted else FAILED
2661 */
2662static int
Eric Moored5d135b2009-05-18 13:02:08 -06002663_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002664{
2665 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2666 struct MPT2SAS_DEVICE *sas_device_priv_data;
2667 struct _sas_device *sas_device;
2668 unsigned long flags;
2669 u16 handle;
2670 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302671 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002672
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302673 starget_printk(KERN_INFO, starget, "attempting target reset! "
2674 "scmd(%p)\n", scmd);
2675 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002676
2677 sas_device_priv_data = scmd->device->hostdata;
2678 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302679 starget_printk(KERN_INFO, starget, "target been deleted! "
2680 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002681 scmd->result = DID_NO_CONNECT << 16;
2682 scmd->scsi_done(scmd);
2683 r = SUCCESS;
2684 goto out;
2685 }
2686
2687 /* for hidden raid components obtain the volume_handle */
2688 handle = 0;
2689 if (sas_device_priv_data->sas_target->flags &
2690 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2691 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2692 sas_device = _scsih_sas_device_find_by_handle(ioc,
2693 sas_device_priv_data->sas_target->handle);
2694 if (sas_device)
2695 handle = sas_device->volume_handle;
2696 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2697 } else
2698 handle = sas_device_priv_data->sas_target->handle;
2699
2700 if (!handle) {
2701 scmd->result = DID_RESET << 16;
2702 r = FAILED;
2703 goto out;
2704 }
2705
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302706 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2707 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302708 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002709
2710 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302711 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2712 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002713 return r;
2714}
2715
2716/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302717 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302718 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002719 *
2720 * Returns SUCCESS if command aborted else FAILED
2721 */
2722static int
Eric Moored5d135b2009-05-18 13:02:08 -06002723_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002724{
2725 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2726 int r, retval;
2727
2728 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2729 ioc->name, scmd);
2730 scsi_print_command(scmd);
2731
2732 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2733 FORCE_BIG_HAMMER);
2734 r = (retval < 0) ? FAILED : SUCCESS;
2735 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2736 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2737
2738 return r;
2739}
2740
2741/**
2742 * _scsih_fw_event_add - insert and queue up fw_event
2743 * @ioc: per adapter object
2744 * @fw_event: object describing the event
2745 * Context: This function will acquire ioc->fw_event_lock.
2746 *
2747 * This adds the firmware event object into link list, then queues it up to
2748 * be processed from user context.
2749 *
2750 * Return nothing.
2751 */
2752static void
2753_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2754{
2755 unsigned long flags;
2756
2757 if (ioc->firmware_event_thread == NULL)
2758 return;
2759
2760 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2761 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302762 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2763 queue_delayed_work(ioc->firmware_event_thread,
2764 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002765 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2766}
2767
2768/**
2769 * _scsih_fw_event_free - delete fw_event
2770 * @ioc: per adapter object
2771 * @fw_event: object describing the event
2772 * Context: This function will acquire ioc->fw_event_lock.
2773 *
2774 * This removes firmware event object from link list, frees associated memory.
2775 *
2776 * Return nothing.
2777 */
2778static void
2779_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2780 *fw_event)
2781{
2782 unsigned long flags;
2783
2784 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2785 list_del(&fw_event->list);
2786 kfree(fw_event->event_data);
2787 kfree(fw_event);
2788 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2789}
2790
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302791
Eric Moore635374e2009-03-09 01:21:12 -06002792/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302793 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002794 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002795 *
2796 * Return nothing.
2797 */
2798static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302799_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002800{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302801 struct fw_event_work *fw_event;
2802
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302803 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302804 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302805 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2806 fw_event->ioc = ioc;
2807 _scsih_fw_event_add(ioc, fw_event);
2808}
2809
2810/**
2811 * mpt2sas_port_enable_complete - port enable completed (fake event)
2812 * @ioc: per adapter object
2813 *
2814 * Return nothing.
2815 */
2816void
2817mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2818{
2819 struct fw_event_work *fw_event;
2820
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302821 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2822 if (!fw_event)
2823 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302824 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302825 fw_event->ioc = ioc;
2826 _scsih_fw_event_add(ioc, fw_event);
2827}
2828
2829/**
2830 * _scsih_fw_event_cleanup_queue - cleanup event queue
2831 * @ioc: per adapter object
2832 *
2833 * Walk the firmware event queue, either killing timers, or waiting
2834 * for outstanding events to complete
2835 *
2836 * Return nothing.
2837 */
2838static void
2839_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2840{
2841 struct fw_event_work *fw_event, *next;
2842
2843 if (list_empty(&ioc->fw_event_list) ||
2844 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002845 return;
2846
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302847 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2848 if (cancel_delayed_work(&fw_event->delayed_work)) {
2849 _scsih_fw_event_free(ioc, fw_event);
2850 continue;
2851 }
2852 fw_event->cancel_pending_work = 1;
2853 }
Eric Moore635374e2009-03-09 01:21:12 -06002854}
2855
Eric Moore635374e2009-03-09 01:21:12 -06002856/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302857 * _scsih_ublock_io_all_device - unblock every device
2858 * @ioc: per adapter object
2859 *
2860 * change the device state from block to running
2861 */
2862static void
2863_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2864{
2865 struct MPT2SAS_DEVICE *sas_device_priv_data;
2866 struct scsi_device *sdev;
2867
2868 shost_for_each_device(sdev, ioc->shost) {
2869 sas_device_priv_data = sdev->hostdata;
2870 if (!sas_device_priv_data)
2871 continue;
2872 if (!sas_device_priv_data->block)
2873 continue;
2874 sas_device_priv_data->block = 0;
2875 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2876 "handle(0x%04x)\n",
2877 sas_device_priv_data->sas_target->handle));
2878 scsi_internal_device_unblock(sdev);
2879 }
2880}
2881/**
Eric Moore635374e2009-03-09 01:21:12 -06002882 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2883 * @ioc: per adapter object
2884 * @handle: device handle
2885 *
2886 * During device pull we need to appropiately set the sdev state.
2887 */
2888static void
2889_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2890{
2891 struct MPT2SAS_DEVICE *sas_device_priv_data;
2892 struct scsi_device *sdev;
2893
2894 shost_for_each_device(sdev, ioc->shost) {
2895 sas_device_priv_data = sdev->hostdata;
2896 if (!sas_device_priv_data)
2897 continue;
2898 if (!sas_device_priv_data->block)
2899 continue;
2900 if (sas_device_priv_data->sas_target->handle == handle) {
2901 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2902 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2903 "handle(0x%04x)\n", ioc->name, handle));
2904 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302905 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002906 }
2907 }
2908}
2909
2910/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302911 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2912 * @ioc: per adapter object
2913 * @handle: device handle
2914 *
2915 * During device pull we need to appropiately set the sdev state.
2916 */
2917static void
2918_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2919{
2920 struct MPT2SAS_DEVICE *sas_device_priv_data;
2921 struct scsi_device *sdev;
2922
2923 shost_for_each_device(sdev, ioc->shost) {
2924 sas_device_priv_data = sdev->hostdata;
2925 if (!sas_device_priv_data)
2926 continue;
2927 if (sas_device_priv_data->block)
2928 continue;
2929 sas_device_priv_data->block = 1;
2930 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2931 "handle(0x%04x)\n",
2932 sas_device_priv_data->sas_target->handle));
2933 scsi_internal_device_block(sdev);
2934 }
2935}
2936
2937
2938/**
Eric Moore635374e2009-03-09 01:21:12 -06002939 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2940 * @ioc: per adapter object
2941 * @handle: device handle
2942 *
2943 * During device pull we need to appropiately set the sdev state.
2944 */
2945static void
2946_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2947{
2948 struct MPT2SAS_DEVICE *sas_device_priv_data;
2949 struct scsi_device *sdev;
2950
2951 shost_for_each_device(sdev, ioc->shost) {
2952 sas_device_priv_data = sdev->hostdata;
2953 if (!sas_device_priv_data)
2954 continue;
2955 if (sas_device_priv_data->block)
2956 continue;
2957 if (sas_device_priv_data->sas_target->handle == handle) {
2958 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2959 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2960 "handle(0x%04x)\n", ioc->name, handle));
2961 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302962 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002963 }
2964 }
2965}
2966
2967/**
2968 * _scsih_block_io_to_children_attached_to_ex
2969 * @ioc: per adapter object
2970 * @sas_expander: the sas_device object
2971 *
2972 * This routine set sdev state to SDEV_BLOCK for all devices
2973 * attached to this expander. This function called when expander is
2974 * pulled.
2975 */
2976static void
2977_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2978 struct _sas_node *sas_expander)
2979{
2980 struct _sas_port *mpt2sas_port;
2981 struct _sas_device *sas_device;
2982 struct _sas_node *expander_sibling;
2983 unsigned long flags;
2984
2985 if (!sas_expander)
2986 return;
2987
2988 list_for_each_entry(mpt2sas_port,
2989 &sas_expander->sas_port_list, port_list) {
2990 if (mpt2sas_port->remote_identify.device_type ==
2991 SAS_END_DEVICE) {
2992 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2993 sas_device =
2994 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2995 mpt2sas_port->remote_identify.sas_address);
2996 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2997 if (!sas_device)
2998 continue;
2999 _scsih_block_io_device(ioc, sas_device->handle);
3000 }
3001 }
3002
3003 list_for_each_entry(mpt2sas_port,
3004 &sas_expander->sas_port_list, port_list) {
3005
3006 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303007 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003008 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303009 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003010
3011 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3012 expander_sibling =
3013 mpt2sas_scsih_expander_find_by_sas_address(
3014 ioc, mpt2sas_port->remote_identify.sas_address);
3015 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3016 _scsih_block_io_to_children_attached_to_ex(ioc,
3017 expander_sibling);
3018 }
3019 }
3020}
3021
3022/**
3023 * _scsih_block_io_to_children_attached_directly
3024 * @ioc: per adapter object
3025 * @event_data: topology change event data
3026 *
3027 * This routine set sdev state to SDEV_BLOCK for all devices
3028 * direct attached during device pull.
3029 */
3030static void
3031_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3032 Mpi2EventDataSasTopologyChangeList_t *event_data)
3033{
3034 int i;
3035 u16 handle;
3036 u16 reason_code;
3037 u8 phy_number;
3038
3039 for (i = 0; i < event_data->NumEntries; i++) {
3040 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3041 if (!handle)
3042 continue;
3043 phy_number = event_data->StartPhyNum + i;
3044 reason_code = event_data->PHY[i].PhyStatus &
3045 MPI2_EVENT_SAS_TOPO_RC_MASK;
3046 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3047 _scsih_block_io_device(ioc, handle);
3048 }
3049}
3050
3051/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303052 * _scsih_tm_tr_send - send task management request
3053 * @ioc: per adapter object
3054 * @handle: device handle
3055 * Context: interrupt time.
3056 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003057 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303058 * with controller firmware. This function will issue target reset
3059 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003060 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303061 *
3062 * This is designed to send muliple task management request at the same
3063 * time to the fifo. If the fifo is full, we will append the request,
3064 * and process it in a future completion.
3065 */
3066static void
3067_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3068{
3069 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303070 u16 smid;
3071 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303072 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3073 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303074 unsigned long flags;
3075 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303076 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303077
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303078 if (ioc->remove_host) {
3079 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3080 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3081 return;
3082 } else if (ioc->pci_error_recovery) {
3083 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3084 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3085 handle));
3086 return;
3087 }
3088 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3089 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3090 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3091 "operational: handle(0x%04x)\n", __func__, ioc->name,
3092 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303093 return;
3094 }
3095
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303096 /* if PD, then return */
3097 if (test_bit(handle, ioc->pd_handles))
3098 return;
3099
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303100 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3101 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303102 if (sas_device && sas_device->starget &&
3103 sas_device->starget->hostdata) {
3104 sas_target_priv_data = sas_device->starget->hostdata;
3105 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303106 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303107 }
3108 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303109
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303110 if (sas_target_priv_data) {
3111 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3112 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3113 (unsigned long long)sas_address));
3114 _scsih_ublock_io_device(ioc, handle);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303115 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303116 }
3117
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303118 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3119 if (!smid) {
3120 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3121 if (!delayed_tr)
3122 return;
3123 INIT_LIST_HEAD(&delayed_tr->list);
3124 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303125 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3126 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3127 "DELAYED:tr:handle(0x%04x), (open)\n",
3128 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303129 return;
3130 }
3131
Kashyap, Desai1278b112010-03-09 17:34:13 +05303132 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3133 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3134 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303135 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3136 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3137 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3138 mpi_request->DevHandle = cpu_to_le16(handle);
3139 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303140 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3141}
3142
3143
3144
3145/**
3146 * _scsih_sas_control_complete - completion routine
3147 * @ioc: per adapter object
3148 * @smid: system request message index
3149 * @msix_index: MSIX table index supplied by the OS
3150 * @reply: reply message frame(lower 32bit addr)
3151 * Context: interrupt time.
3152 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003153 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303154 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003155 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303156 *
3157 * Return 1 meaning mf should be freed from _base_interrupt
3158 * 0 means the mf is freed from this function.
3159 */
3160static u8
3161_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3162 u8 msix_index, u32 reply)
3163{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303164#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303165 Mpi2SasIoUnitControlReply_t *mpi_reply =
3166 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303167#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05303168 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3169 "sc_complete:handle(0x%04x), (open) "
3170 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3171 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3172 le16_to_cpu(mpi_reply->IOCStatus),
3173 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303174 return 1;
3175}
3176
3177/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303178 * _scsih_tm_tr_volume_send - send target reset request for volumes
3179 * @ioc: per adapter object
3180 * @handle: device handle
3181 * Context: interrupt time.
3182 *
3183 * This is designed to send muliple task management request at the same
3184 * time to the fifo. If the fifo is full, we will append the request,
3185 * and process it in a future completion.
3186 */
3187static void
3188_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3189{
3190 Mpi2SCSITaskManagementRequest_t *mpi_request;
3191 u16 smid;
3192 struct _tr_list *delayed_tr;
3193
Eric Moore3cb54692010-07-08 14:44:34 -06003194 if (ioc->shost_recovery || ioc->remove_host ||
3195 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303196 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3197 "progress!\n", __func__, ioc->name));
3198 return;
3199 }
3200
3201 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3202 if (!smid) {
3203 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3204 if (!delayed_tr)
3205 return;
3206 INIT_LIST_HEAD(&delayed_tr->list);
3207 delayed_tr->handle = handle;
3208 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3209 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3210 "DELAYED:tr:handle(0x%04x), (open)\n",
3211 ioc->name, handle));
3212 return;
3213 }
3214
3215 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3216 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3217 ioc->tm_tr_volume_cb_idx));
3218 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3219 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3220 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3221 mpi_request->DevHandle = cpu_to_le16(handle);
3222 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3223 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3224}
3225
3226/**
3227 * _scsih_tm_volume_tr_complete - target reset completion
3228 * @ioc: per adapter object
3229 * @smid: system request message index
3230 * @msix_index: MSIX table index supplied by the OS
3231 * @reply: reply message frame(lower 32bit addr)
3232 * Context: interrupt time.
3233 *
3234 * Return 1 meaning mf should be freed from _base_interrupt
3235 * 0 means the mf is freed from this function.
3236 */
3237static u8
3238_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3239 u8 msix_index, u32 reply)
3240{
3241 u16 handle;
3242 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3243 Mpi2SCSITaskManagementReply_t *mpi_reply =
3244 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3245
Eric Moore3cb54692010-07-08 14:44:34 -06003246 if (ioc->shost_recovery || ioc->remove_host ||
3247 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303248 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3249 "progress!\n", __func__, ioc->name));
3250 return 1;
3251 }
3252
3253 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3254 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3255 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3256 dewtprintk(ioc, printk("spurious interrupt: "
3257 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3258 le16_to_cpu(mpi_reply->DevHandle), smid));
3259 return 0;
3260 }
3261
3262 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3263 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3264 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3265 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3266 le32_to_cpu(mpi_reply->IOCLogInfo),
3267 le32_to_cpu(mpi_reply->TerminationCount)));
3268
3269 return _scsih_check_for_pending_tm(ioc, smid);
3270}
3271
3272/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303273 * _scsih_tm_tr_complete -
3274 * @ioc: per adapter object
3275 * @smid: system request message index
3276 * @msix_index: MSIX table index supplied by the OS
3277 * @reply: reply message frame(lower 32bit addr)
3278 * Context: interrupt time.
3279 *
3280 * This is the target reset completion routine.
3281 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003282 * handshake protocol with controller firmware.
3283 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303284 *
3285 * Return 1 meaning mf should be freed from _base_interrupt
3286 * 0 means the mf is freed from this function.
3287 */
3288static u8
3289_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3290 u32 reply)
3291{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303292 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303293 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303294 Mpi2SCSITaskManagementReply_t *mpi_reply =
3295 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3296 Mpi2SasIoUnitControlRequest_t *mpi_request;
3297 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303298 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303299
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303300 if (ioc->remove_host) {
3301 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3302 "removed\n", __func__, ioc->name));
3303 return 1;
3304 } else if (ioc->pci_error_recovery) {
3305 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3306 "error recovery\n", __func__, ioc->name));
3307 return 1;
3308 }
3309 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3310 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3311 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3312 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303313 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303314 }
3315
Kashyap, Desai1278b112010-03-09 17:34:13 +05303316 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3317 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3318 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3319 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3320 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3321 le16_to_cpu(mpi_reply->DevHandle), smid));
3322 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303323 }
3324
Kashyap, Desai1278b112010-03-09 17:34:13 +05303325 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3326 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3327 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3328 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3329 le32_to_cpu(mpi_reply->IOCLogInfo),
3330 le32_to_cpu(mpi_reply->TerminationCount)));
3331
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303332 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3333 if (!smid_sas_ctrl) {
3334 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3335 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303336 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303337 }
3338
Kashyap, Desai1278b112010-03-09 17:34:13 +05303339 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3340 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3341 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303342 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3343 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3344 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3345 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303346 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303347 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303348
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303349 return _scsih_check_for_pending_tm(ioc, smid);
3350}
3351
3352/**
3353 * _scsih_check_for_pending_tm - check for pending task management
3354 * @ioc: per adapter object
3355 * @smid: system request message index
3356 *
3357 * This will check delayed target reset list, and feed the
3358 * next reqeust.
3359 *
3360 * Return 1 meaning mf should be freed from _base_interrupt
3361 * 0 means the mf is freed from this function.
3362 */
3363static u8
3364_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3365{
3366 struct _tr_list *delayed_tr;
3367
3368 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3369 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3370 struct _tr_list, list);
3371 mpt2sas_base_free_smid(ioc, smid);
3372 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3373 list_del(&delayed_tr->list);
3374 kfree(delayed_tr);
3375 return 0;
3376 }
3377
Kashyap, Desai1278b112010-03-09 17:34:13 +05303378 if (!list_empty(&ioc->delayed_tr_list)) {
3379 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3380 struct _tr_list, list);
3381 mpt2sas_base_free_smid(ioc, smid);
3382 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3383 list_del(&delayed_tr->list);
3384 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303385 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303386 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303387
Kashyap, Desai1278b112010-03-09 17:34:13 +05303388 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303389}
3390
3391/**
Eric Moore635374e2009-03-09 01:21:12 -06003392 * _scsih_check_topo_delete_events - sanity check on topo events
3393 * @ioc: per adapter object
3394 * @event_data: the event data payload
3395 *
3396 * This routine added to better handle cable breaker.
3397 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003398 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003399 * add and delete events in a single shot. When there is a delete event
3400 * the routine will void any pending add events waiting in the event queue.
3401 *
3402 * Return nothing.
3403 */
3404static void
3405_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3406 Mpi2EventDataSasTopologyChangeList_t *event_data)
3407{
3408 struct fw_event_work *fw_event;
3409 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3410 u16 expander_handle;
3411 struct _sas_node *sas_expander;
3412 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303413 int i, reason_code;
3414 u16 handle;
3415
3416 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303417 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3418 if (!handle)
3419 continue;
3420 reason_code = event_data->PHY[i].PhyStatus &
3421 MPI2_EVENT_SAS_TOPO_RC_MASK;
3422 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3423 _scsih_tm_tr_send(ioc, handle);
3424 }
Eric Moore635374e2009-03-09 01:21:12 -06003425
3426 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3427 if (expander_handle < ioc->sas_hba.num_phys) {
3428 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3429 return;
3430 }
3431
3432 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3433 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3434 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3435 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3436 expander_handle);
3437 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3438 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3439 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3440 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3441
3442 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3443 return;
3444
3445 /* mark ignore flag for pending events */
3446 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3447 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3448 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3449 fw_event->ignore)
3450 continue;
3451 local_event_data = fw_event->event_data;
3452 if (local_event_data->ExpStatus ==
3453 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3454 local_event_data->ExpStatus ==
3455 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3456 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3457 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303458 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003459 "setting ignoring flag\n", ioc->name));
3460 fw_event->ignore = 1;
3461 }
3462 }
3463 }
3464 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3465}
3466
3467/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303468 * _scsih_set_volume_delete_flag - setting volume delete flag
3469 * @ioc: per adapter object
3470 * @handle: device handle
3471 *
3472 * This
3473 * Return nothing.
3474 */
3475static void
3476_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3477{
3478 struct _raid_device *raid_device;
3479 struct MPT2SAS_TARGET *sas_target_priv_data;
3480 unsigned long flags;
3481
3482 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3483 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3484 if (raid_device && raid_device->starget &&
3485 raid_device->starget->hostdata) {
3486 sas_target_priv_data =
3487 raid_device->starget->hostdata;
3488 sas_target_priv_data->deleted = 1;
3489 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3490 "setting delete flag: handle(0x%04x), "
3491 "wwid(0x%016llx)\n", ioc->name, handle,
3492 (unsigned long long) raid_device->wwid));
3493 }
3494 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3495}
3496
3497/**
3498 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3499 * @handle: input handle
3500 * @a: handle for volume a
3501 * @b: handle for volume b
3502 *
3503 * IR firmware only supports two raid volumes. The purpose of this
3504 * routine is to set the volume handle in either a or b. When the given
3505 * input handle is non-zero, or when a and b have not been set before.
3506 */
3507static void
3508_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3509{
3510 if (!handle || handle == *a || handle == *b)
3511 return;
3512 if (!*a)
3513 *a = handle;
3514 else if (!*b)
3515 *b = handle;
3516}
3517
3518/**
3519 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3520 * @ioc: per adapter object
3521 * @event_data: the event data payload
3522 * Context: interrupt time.
3523 *
3524 * This routine will send target reset to volume, followed by target
3525 * resets to the PDs. This is called when a PD has been removed, or
3526 * volume has been deleted or removed. When the target reset is sent
3527 * to volume, the PD target resets need to be queued to start upon
3528 * completion of the volume target reset.
3529 *
3530 * Return nothing.
3531 */
3532static void
3533_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3534 Mpi2EventDataIrConfigChangeList_t *event_data)
3535{
3536 Mpi2EventIrConfigElement_t *element;
3537 int i;
3538 u16 handle, volume_handle, a, b;
3539 struct _tr_list *delayed_tr;
3540
3541 a = 0;
3542 b = 0;
3543
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303544 if (ioc->is_warpdrive)
3545 return;
3546
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303547 /* Volume Resets for Deleted or Removed */
3548 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3549 for (i = 0; i < event_data->NumElements; i++, element++) {
3550 if (element->ReasonCode ==
3551 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3552 element->ReasonCode ==
3553 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3554 volume_handle = le16_to_cpu(element->VolDevHandle);
3555 _scsih_set_volume_delete_flag(ioc, volume_handle);
3556 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3557 }
3558 }
3559
3560 /* Volume Resets for UNHIDE events */
3561 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3562 for (i = 0; i < event_data->NumElements; i++, element++) {
3563 if (le32_to_cpu(event_data->Flags) &
3564 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3565 continue;
3566 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3567 volume_handle = le16_to_cpu(element->VolDevHandle);
3568 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3569 }
3570 }
3571
3572 if (a)
3573 _scsih_tm_tr_volume_send(ioc, a);
3574 if (b)
3575 _scsih_tm_tr_volume_send(ioc, b);
3576
3577 /* PD target resets */
3578 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3579 for (i = 0; i < event_data->NumElements; i++, element++) {
3580 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3581 continue;
3582 handle = le16_to_cpu(element->PhysDiskDevHandle);
3583 volume_handle = le16_to_cpu(element->VolDevHandle);
3584 clear_bit(handle, ioc->pd_handles);
3585 if (!volume_handle)
3586 _scsih_tm_tr_send(ioc, handle);
3587 else if (volume_handle == a || volume_handle == b) {
3588 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3589 BUG_ON(!delayed_tr);
3590 INIT_LIST_HEAD(&delayed_tr->list);
3591 delayed_tr->handle = handle;
3592 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3593 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3594 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3595 handle));
3596 } else
3597 _scsih_tm_tr_send(ioc, handle);
3598 }
3599}
3600
3601
3602/**
3603 * _scsih_check_volume_delete_events - set delete flag for volumes
3604 * @ioc: per adapter object
3605 * @event_data: the event data payload
3606 * Context: interrupt time.
3607 *
3608 * This will handle the case when the cable connected to entire volume is
3609 * pulled. We will take care of setting the deleted flag so normal IO will
3610 * not be sent.
3611 *
3612 * Return nothing.
3613 */
3614static void
3615_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3616 Mpi2EventDataIrVolume_t *event_data)
3617{
3618 u32 state;
3619
3620 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3621 return;
3622 state = le32_to_cpu(event_data->NewValue);
3623 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3624 MPI2_RAID_VOL_STATE_FAILED)
3625 _scsih_set_volume_delete_flag(ioc,
3626 le16_to_cpu(event_data->VolDevHandle));
3627}
3628
3629/**
Eric Moore635374e2009-03-09 01:21:12 -06003630 * _scsih_flush_running_cmds - completing outstanding commands.
3631 * @ioc: per adapter object
3632 *
3633 * The flushing out of all pending scmd commands following host reset,
3634 * where all IO is dropped to the floor.
3635 *
3636 * Return nothing.
3637 */
3638static void
3639_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3640{
3641 struct scsi_cmnd *scmd;
3642 u16 smid;
3643 u16 count = 0;
3644
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303645 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303646 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003647 if (!scmd)
3648 continue;
3649 count++;
3650 mpt2sas_base_free_smid(ioc, smid);
3651 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003652 if (ioc->pci_error_recovery)
3653 scmd->result = DID_NO_CONNECT << 16;
3654 else
3655 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003656 scmd->scsi_done(scmd);
3657 }
3658 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3659 ioc->name, count));
3660}
3661
3662/**
Eric Moore3c621b32009-05-18 12:59:41 -06003663 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3664 * @scmd: pointer to scsi command object
3665 * @mpi_request: pointer to the SCSI_IO reqest message frame
3666 *
3667 * Supporting protection 1 and 3.
3668 *
3669 * Returns nothing
3670 */
3671static void
3672_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3673{
3674 u16 eedp_flags;
3675 unsigned char prot_op = scsi_get_prot_op(scmd);
3676 unsigned char prot_type = scsi_get_prot_type(scmd);
3677
Eric Moored334aa72010-04-22 10:47:40 -06003678 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003679 return;
3680
3681 if (prot_op == SCSI_PROT_READ_STRIP)
3682 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3683 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3684 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3685 else
3686 return;
3687
Eric Moore3c621b32009-05-18 12:59:41 -06003688 switch (prot_type) {
3689 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003690 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003691
3692 /*
3693 * enable ref/guard checking
3694 * auto increment ref tag
3695 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303696 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003697 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3698 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3699 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3700 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003701 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003702
Eric Moore3c621b32009-05-18 12:59:41 -06003703 case SCSI_PROT_DIF_TYPE3:
3704
3705 /*
3706 * enable guard checking
3707 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303708 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003709 break;
3710 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303711 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3712 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003713}
3714
3715/**
3716 * _scsih_eedp_error_handling - return sense code for EEDP errors
3717 * @scmd: pointer to scsi command object
3718 * @ioc_status: ioc status
3719 *
3720 * Returns nothing
3721 */
3722static void
3723_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3724{
3725 u8 ascq;
3726 u8 sk;
3727 u8 host_byte;
3728
3729 switch (ioc_status) {
3730 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3731 ascq = 0x01;
3732 break;
3733 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3734 ascq = 0x02;
3735 break;
3736 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3737 ascq = 0x03;
3738 break;
3739 default:
3740 ascq = 0x00;
3741 break;
3742 }
3743
3744 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3745 sk = ILLEGAL_REQUEST;
3746 host_byte = DID_ABORT;
3747 } else {
3748 sk = ABORTED_COMMAND;
3749 host_byte = DID_OK;
3750 }
3751
3752 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3753 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3754 SAM_STAT_CHECK_CONDITION;
3755}
3756
3757/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303758 * _scsih_scsi_direct_io_get - returns direct io flag
3759 * @ioc: per adapter object
3760 * @smid: system request message index
3761 *
3762 * Returns the smid stored scmd pointer.
3763 */
3764static inline u8
3765_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3766{
3767 return ioc->scsi_lookup[smid - 1].direct_io;
3768}
3769
3770/**
3771 * _scsih_scsi_direct_io_set - sets direct io flag
3772 * @ioc: per adapter object
3773 * @smid: system request message index
3774 * @direct_io: Zero or non-zero value to set in the direct_io flag
3775 *
3776 * Returns Nothing.
3777 */
3778static inline void
3779_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3780{
3781 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3782}
3783
3784
3785/**
3786 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3787 * @ioc: per adapter object
3788 * @scmd: pointer to scsi command object
3789 * @raid_device: pointer to raid device data structure
3790 * @mpi_request: pointer to the SCSI_IO reqest message frame
3791 * @smid: system request message index
3792 *
3793 * Returns nothing
3794 */
3795static void
3796_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3797 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3798 u16 smid)
3799{
3800 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3801 u32 stripe_sz, stripe_exp;
3802 u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3803 u8 cdb0 = scmd->cmnd[0];
3804
3805 /*
3806 * Try Direct I/O to RAID memeber disks
3807 */
3808 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3809 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3810 cdb_ptr = mpi_request->CDB.CDB32;
3811
3812 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3813 | cdb_ptr[5])) {
3814 io_size = scsi_bufflen(scmd) >> 9;
3815 /* get virtual lba */
3816 lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3817 &cdb_ptr[6];
3818 tmp_ptr = (u8 *)&v_lba + 3;
3819 *tmp_ptr-- = *lba_ptr1++;
3820 *tmp_ptr-- = *lba_ptr1++;
3821 *tmp_ptr-- = *lba_ptr1++;
3822 *tmp_ptr = *lba_ptr1;
3823
3824 if (((u64)v_lba + (u64)io_size - 1) <=
3825 (u32)raid_device->max_lba) {
3826 stripe_sz = raid_device->stripe_sz;
3827 stripe_exp = raid_device->stripe_exponent;
3828 stripe_off = v_lba & (stripe_sz - 1);
3829
3830 /* Check whether IO falls within a stripe */
3831 if ((stripe_off + io_size) <= stripe_sz) {
3832 num_pds = raid_device->num_pds;
3833 p_lba = v_lba >> stripe_exp;
3834 stripe_unit = p_lba / num_pds;
3835 column = p_lba % num_pds;
3836 p_lba = (stripe_unit << stripe_exp) +
3837 stripe_off;
3838 mpi_request->DevHandle =
3839 cpu_to_le16(raid_device->
3840 pd_handle[column]);
3841 tmp_ptr = (u8 *)&p_lba + 3;
3842 *lba_ptr2++ = *tmp_ptr--;
3843 *lba_ptr2++ = *tmp_ptr--;
3844 *lba_ptr2++ = *tmp_ptr--;
3845 *lba_ptr2 = *tmp_ptr;
3846 /*
3847 * WD: To indicate this I/O is directI/O
3848 */
3849 _scsih_scsi_direct_io_set(ioc, smid, 1);
3850 }
3851 }
3852 }
3853 }
3854}
3855
3856/**
Eric Moored5d135b2009-05-18 13:02:08 -06003857 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003858 * @scmd: pointer to scsi command object
3859 * @done: function pointer to be invoked on completion
3860 *
3861 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3862 *
3863 * Returns 0 on success. If there's a failure, return either:
3864 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3865 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3866 */
3867static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003868_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003869{
3870 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3871 struct MPT2SAS_DEVICE *sas_device_priv_data;
3872 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303873 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003874 Mpi2SCSIIORequest_t *mpi_request;
3875 u32 mpi_control;
3876 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003877
3878 scmd->scsi_done = done;
3879 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303880 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003881 scmd->result = DID_NO_CONNECT << 16;
3882 scmd->scsi_done(scmd);
3883 return 0;
3884 }
3885
Kashyap, Desai78215782011-06-14 10:57:08 +05303886 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003887 scmd->result = DID_NO_CONNECT << 16;
3888 scmd->scsi_done(scmd);
3889 return 0;
3890 }
3891
Eric Moore635374e2009-03-09 01:21:12 -06003892 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303893 /* invalid device handle */
3894 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003895 scmd->result = DID_NO_CONNECT << 16;
3896 scmd->scsi_done(scmd);
3897 return 0;
3898 }
3899
Kashyap, Desai130b9582010-04-08 17:54:32 +05303900 /* host recovery or link resets sent via IOCTLs */
3901 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003902 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003903 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303904 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3905 return SCSI_MLQUEUE_DEVICE_BUSY;
3906 /* device has been deleted */
3907 else if (sas_target_priv_data->deleted) {
3908 scmd->result = DID_NO_CONNECT << 16;
3909 scmd->scsi_done(scmd);
3910 return 0;
3911 }
Eric Moore635374e2009-03-09 01:21:12 -06003912
3913 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3914 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3915 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3916 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3917 else
3918 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3919
3920 /* set tags */
3921 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3922 if (scmd->device->tagged_supported) {
3923 if (scmd->device->ordered_tags)
3924 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3925 else
3926 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3927 } else
3928/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3929/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3930 */
3931 mpi_control |= (0x500);
3932
3933 } else
3934 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303935 /* Make sure Device is not raid volume.
3936 * We do not expose raid functionality to upper layer for warpdrive.
3937 */
3938 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003939 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003940 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3941
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303942 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003943 if (!smid) {
3944 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3945 ioc->name, __func__);
3946 goto out;
3947 }
3948 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3949 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003950 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003951 if (scmd->cmd_len == 32)
3952 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003953 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3954 if (sas_device_priv_data->sas_target->flags &
3955 MPT_TARGET_FLAGS_RAID_COMPONENT)
3956 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3957 else
3958 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3959 mpi_request->DevHandle =
3960 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3961 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3962 mpi_request->Control = cpu_to_le32(mpi_control);
3963 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3964 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3965 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3966 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303967 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003968 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3969 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3970 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303971 mpi_request->VF_ID = 0; /* TODO */
3972 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003973 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3974 mpi_request->LUN);
3975 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3976
3977 if (!mpi_request->DataLength) {
3978 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3979 } else {
3980 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3981 mpt2sas_base_free_smid(ioc, smid);
3982 goto out;
3983 }
3984 }
3985
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303986 raid_device = sas_target_priv_data->raid_device;
3987 if (raid_device && raid_device->direct_io_enabled)
3988 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3989 smid);
3990
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303991 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3992 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303993 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303994 else
3995 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003996 return 0;
3997
3998 out:
3999 return SCSI_MLQUEUE_HOST_BUSY;
4000}
4001
Jeff Garzikf2812332010-11-16 02:10:29 -05004002static DEF_SCSI_QCMD(_scsih_qcmd)
4003
Eric Moore635374e2009-03-09 01:21:12 -06004004/**
4005 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4006 * @sense_buffer: sense data returned by target
4007 * @data: normalized skey/asc/ascq
4008 *
4009 * Return nothing.
4010 */
4011static void
4012_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4013{
4014 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4015 /* descriptor format */
4016 data->skey = sense_buffer[1] & 0x0F;
4017 data->asc = sense_buffer[2];
4018 data->ascq = sense_buffer[3];
4019 } else {
4020 /* fixed format */
4021 data->skey = sense_buffer[2] & 0x0F;
4022 data->asc = sense_buffer[12];
4023 data->ascq = sense_buffer[13];
4024 }
4025}
4026
4027#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4028/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004029 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004030 * @ioc: per adapter object
4031 * @scmd: pointer to scsi command object
4032 * @mpi_reply: reply mf payload returned from firmware
4033 *
4034 * scsi_status - SCSI Status code returned from target device
4035 * scsi_state - state info associated with SCSI_IO determined by ioc
4036 * ioc_status - ioc supplied status info
4037 *
4038 * Return nothing.
4039 */
4040static void
4041_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4042 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4043{
4044 u32 response_info;
4045 u8 *response_bytes;
4046 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4047 MPI2_IOCSTATUS_MASK;
4048 u8 scsi_state = mpi_reply->SCSIState;
4049 u8 scsi_status = mpi_reply->SCSIStatus;
4050 char *desc_ioc_state = NULL;
4051 char *desc_scsi_status = NULL;
4052 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304053 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304054 struct _sas_device *sas_device = NULL;
4055 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304056 struct scsi_target *starget = scmd->device->sdev_target;
4057 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304058 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304059
4060 if (!priv_target)
4061 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304062
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304063 if (ioc->hide_ir_msg)
4064 device_str = "WarpDrive";
4065 else
4066 device_str = "volume";
4067
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304068 if (log_info == 0x31170000)
4069 return;
Eric Moore635374e2009-03-09 01:21:12 -06004070
4071 switch (ioc_status) {
4072 case MPI2_IOCSTATUS_SUCCESS:
4073 desc_ioc_state = "success";
4074 break;
4075 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4076 desc_ioc_state = "invalid function";
4077 break;
4078 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4079 desc_ioc_state = "scsi recovered error";
4080 break;
4081 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4082 desc_ioc_state = "scsi invalid dev handle";
4083 break;
4084 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4085 desc_ioc_state = "scsi device not there";
4086 break;
4087 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4088 desc_ioc_state = "scsi data overrun";
4089 break;
4090 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4091 desc_ioc_state = "scsi data underrun";
4092 break;
4093 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4094 desc_ioc_state = "scsi io data error";
4095 break;
4096 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4097 desc_ioc_state = "scsi protocol error";
4098 break;
4099 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4100 desc_ioc_state = "scsi task terminated";
4101 break;
4102 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4103 desc_ioc_state = "scsi residual mismatch";
4104 break;
4105 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4106 desc_ioc_state = "scsi task mgmt failed";
4107 break;
4108 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4109 desc_ioc_state = "scsi ioc terminated";
4110 break;
4111 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4112 desc_ioc_state = "scsi ext terminated";
4113 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004114 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4115 desc_ioc_state = "eedp guard error";
4116 break;
4117 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4118 desc_ioc_state = "eedp ref tag error";
4119 break;
4120 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4121 desc_ioc_state = "eedp app tag error";
4122 break;
Eric Moore635374e2009-03-09 01:21:12 -06004123 default:
4124 desc_ioc_state = "unknown";
4125 break;
4126 }
4127
4128 switch (scsi_status) {
4129 case MPI2_SCSI_STATUS_GOOD:
4130 desc_scsi_status = "good";
4131 break;
4132 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4133 desc_scsi_status = "check condition";
4134 break;
4135 case MPI2_SCSI_STATUS_CONDITION_MET:
4136 desc_scsi_status = "condition met";
4137 break;
4138 case MPI2_SCSI_STATUS_BUSY:
4139 desc_scsi_status = "busy";
4140 break;
4141 case MPI2_SCSI_STATUS_INTERMEDIATE:
4142 desc_scsi_status = "intermediate";
4143 break;
4144 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4145 desc_scsi_status = "intermediate condmet";
4146 break;
4147 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4148 desc_scsi_status = "reservation conflict";
4149 break;
4150 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4151 desc_scsi_status = "command terminated";
4152 break;
4153 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4154 desc_scsi_status = "task set full";
4155 break;
4156 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4157 desc_scsi_status = "aca active";
4158 break;
4159 case MPI2_SCSI_STATUS_TASK_ABORTED:
4160 desc_scsi_status = "task aborted";
4161 break;
4162 default:
4163 desc_scsi_status = "unknown";
4164 break;
4165 }
4166
4167 desc_scsi_state[0] = '\0';
4168 if (!scsi_state)
4169 desc_scsi_state = " ";
4170 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4171 strcat(desc_scsi_state, "response info ");
4172 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4173 strcat(desc_scsi_state, "state terminated ");
4174 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4175 strcat(desc_scsi_state, "no status ");
4176 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4177 strcat(desc_scsi_state, "autosense failed ");
4178 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4179 strcat(desc_scsi_state, "autosense valid ");
4180
4181 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304182
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304183 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304184 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4185 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304186 } else {
4187 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4188 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4189 priv_target->sas_address);
4190 if (sas_device) {
4191 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4192 "phy(%d)\n", ioc->name, sas_device->sas_address,
4193 sas_device->phy);
4194 printk(MPT2SAS_WARN_FMT
4195 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4196 ioc->name, sas_device->enclosure_logical_id,
4197 sas_device->slot);
4198 }
4199 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304200 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304201
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304202 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4203 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4204 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004205 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4206 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4207 scsi_get_resid(scmd));
4208 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4209 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4210 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4211 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4212 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4213 scsi_status, desc_scsi_state, scsi_state);
4214
4215 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4216 struct sense_info data;
4217 _scsih_normalize_sense(scmd->sense_buffer, &data);
4218 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304219 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4220 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004221 }
4222
4223 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4224 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4225 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304226 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004227 }
4228}
4229#endif
4230
4231/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304232 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004233 * @ioc: per adapter object
4234 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304235 * Context: process
4236 *
4237 * Return nothing.
4238 */
4239static void
4240_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4241{
4242 Mpi2SepReply_t mpi_reply;
4243 Mpi2SepRequest_t mpi_request;
4244
4245 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4246 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4247 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4248 mpi_request.SlotStatus =
4249 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4250 mpi_request.DevHandle = cpu_to_le16(handle);
4251 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4252 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4253 &mpi_request)) != 0) {
4254 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4255 __FILE__, __LINE__, __func__);
4256 return;
4257 }
4258
4259 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4260 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4261 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4262 le16_to_cpu(mpi_reply.IOCStatus),
4263 le32_to_cpu(mpi_reply.IOCLogInfo)));
4264 return;
4265 }
4266}
4267
4268/**
4269 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4270 * @ioc: per adapter object
4271 * @handle: device handle
4272 * Context: interrupt.
4273 *
4274 * Return nothing.
4275 */
4276static void
4277_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4278{
4279 struct fw_event_work *fw_event;
4280
4281 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4282 if (!fw_event)
4283 return;
4284 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4285 fw_event->device_handle = handle;
4286 fw_event->ioc = ioc;
4287 _scsih_fw_event_add(ioc, fw_event);
4288}
4289
4290/**
4291 * _scsih_smart_predicted_fault - process smart errors
4292 * @ioc: per adapter object
4293 * @handle: device handle
4294 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004295 *
4296 * Return nothing.
4297 */
4298static void
4299_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4300{
Eric Moore635374e2009-03-09 01:21:12 -06004301 struct scsi_target *starget;
4302 struct MPT2SAS_TARGET *sas_target_priv_data;
4303 Mpi2EventNotificationReply_t *event_reply;
4304 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4305 struct _sas_device *sas_device;
4306 ssize_t sz;
4307 unsigned long flags;
4308
4309 /* only handle non-raid devices */
4310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4311 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4312 if (!sas_device) {
4313 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4314 return;
4315 }
4316 starget = sas_device->starget;
4317 sas_target_priv_data = starget->hostdata;
4318
4319 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4320 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4321 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4322 return;
4323 }
4324 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4325 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4326
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304327 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4328 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004329
4330 /* insert into event log */
4331 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4332 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4333 event_reply = kzalloc(sz, GFP_KERNEL);
4334 if (!event_reply) {
4335 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4336 ioc->name, __FILE__, __LINE__, __func__);
4337 return;
4338 }
4339
4340 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4341 event_reply->Event =
4342 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4343 event_reply->MsgLength = sz/4;
4344 event_reply->EventDataLength =
4345 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4346 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4347 event_reply->EventData;
4348 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4349 event_data->ASC = 0x5D;
4350 event_data->DevHandle = cpu_to_le16(handle);
4351 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4352 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4353 kfree(event_reply);
4354}
4355
4356/**
Eric Moored5d135b2009-05-18 13:02:08 -06004357 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004358 * @ioc: per adapter object
4359 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304360 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004361 * @reply: reply message frame(lower 32bit addr)
4362 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304363 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004364 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304365 * Return 1 meaning mf should be freed from _base_interrupt
4366 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004367 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304368static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304369_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004370{
4371 Mpi2SCSIIORequest_t *mpi_request;
4372 Mpi2SCSIIOReply_t *mpi_reply;
4373 struct scsi_cmnd *scmd;
4374 u16 ioc_status;
4375 u32 xfer_cnt;
4376 u8 scsi_state;
4377 u8 scsi_status;
4378 u32 log_info;
4379 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304380 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304381 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004382
4383 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304384 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004385 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304386 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004387
4388 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4389
4390 if (mpi_reply == NULL) {
4391 scmd->result = DID_OK << 16;
4392 goto out;
4393 }
4394
4395 sas_device_priv_data = scmd->device->hostdata;
4396 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4397 sas_device_priv_data->sas_target->deleted) {
4398 scmd->result = DID_NO_CONNECT << 16;
4399 goto out;
4400 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304401 /*
4402 * WARPDRIVE: If direct_io is set then it is directIO,
4403 * the failed direct I/O should be redirected to volume
4404 */
4405 if (_scsih_scsi_direct_io_get(ioc, smid)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304406 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4407 ioc->scsi_lookup[smid - 1].scmd = scmd;
4408 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304409 _scsih_scsi_direct_io_set(ioc, smid, 0);
4410 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4411 mpi_request->DevHandle =
4412 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4413 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4414 sas_device_priv_data->sas_target->handle);
4415 return 0;
4416 }
4417
Eric Moore635374e2009-03-09 01:21:12 -06004418
4419 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304420 scsi_state = mpi_reply->SCSIState;
4421 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4422 response_code =
4423 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004424 if (!sas_device_priv_data->tlr_snoop_check) {
4425 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304426 /* Make sure Device is not raid volume.
4427 * We do not expose raid functionality to upper layer for warpdrive.
4428 */
4429 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304430 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304431 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4432 sas_disable_tlr(scmd->device);
4433 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4434 }
Eric Moore635374e2009-03-09 01:21:12 -06004435 }
4436
4437 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4438 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4439 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4440 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4441 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4442 else
4443 log_info = 0;
4444 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004445 scsi_status = mpi_reply->SCSIStatus;
4446
4447 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4448 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4449 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4450 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4451 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4452 }
4453
4454 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4455 struct sense_info data;
4456 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4457 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004458 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004459 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004460 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004461 _scsih_normalize_sense(scmd->sense_buffer, &data);
4462 /* failure prediction threshold exceeded */
4463 if (data.asc == 0x5D)
4464 _scsih_smart_predicted_fault(ioc,
4465 le16_to_cpu(mpi_reply->DevHandle));
4466 }
4467
4468 switch (ioc_status) {
4469 case MPI2_IOCSTATUS_BUSY:
4470 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4471 scmd->result = SAM_STAT_BUSY;
4472 break;
4473
4474 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4475 scmd->result = DID_NO_CONNECT << 16;
4476 break;
4477
4478 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4479 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304480 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4481 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004482 }
Eric Moore635374e2009-03-09 01:21:12 -06004483 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4484 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4485 scmd->result = DID_RESET << 16;
4486 break;
4487
4488 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4489 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4490 scmd->result = DID_SOFT_ERROR << 16;
4491 else
4492 scmd->result = (DID_OK << 16) | scsi_status;
4493 break;
4494
4495 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4496 scmd->result = (DID_OK << 16) | scsi_status;
4497
4498 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4499 break;
4500
4501 if (xfer_cnt < scmd->underflow) {
4502 if (scsi_status == SAM_STAT_BUSY)
4503 scmd->result = SAM_STAT_BUSY;
4504 else
4505 scmd->result = DID_SOFT_ERROR << 16;
4506 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4507 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4508 scmd->result = DID_SOFT_ERROR << 16;
4509 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4510 scmd->result = DID_RESET << 16;
4511 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4512 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4513 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4514 scmd->result = (DRIVER_SENSE << 24) |
4515 SAM_STAT_CHECK_CONDITION;
4516 scmd->sense_buffer[0] = 0x70;
4517 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4518 scmd->sense_buffer[12] = 0x20;
4519 scmd->sense_buffer[13] = 0;
4520 }
4521 break;
4522
4523 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4524 scsi_set_resid(scmd, 0);
4525 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4526 case MPI2_IOCSTATUS_SUCCESS:
4527 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304528 if (response_code ==
4529 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4530 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4531 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004532 scmd->result = DID_SOFT_ERROR << 16;
4533 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4534 scmd->result = DID_RESET << 16;
4535 break;
4536
Eric Moore3c621b32009-05-18 12:59:41 -06004537 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4538 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4539 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4540 _scsih_eedp_error_handling(scmd, ioc_status);
4541 break;
Eric Moore635374e2009-03-09 01:21:12 -06004542 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4543 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4544 case MPI2_IOCSTATUS_INVALID_SGL:
4545 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4546 case MPI2_IOCSTATUS_INVALID_FIELD:
4547 case MPI2_IOCSTATUS_INVALID_STATE:
4548 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4549 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4550 default:
4551 scmd->result = DID_SOFT_ERROR << 16;
4552 break;
4553
4554 }
4555
4556#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4557 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4558 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4559#endif
4560
4561 out:
4562 scsi_dma_unmap(scmd);
4563 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304564 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004565}
4566
4567/**
Eric Moore635374e2009-03-09 01:21:12 -06004568 * _scsih_sas_host_refresh - refreshing sas host object contents
4569 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004570 * Context: user
4571 *
4572 * During port enable, fw will send topology events for every device. Its
4573 * possible that the handles may change from the previous setting, so this
4574 * code keeping handles updating if changed.
4575 *
4576 * Return nothing.
4577 */
4578static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304579_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004580{
4581 u16 sz;
4582 u16 ioc_status;
4583 int i;
4584 Mpi2ConfigReply_t mpi_reply;
4585 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304586 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304587 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004588
4589 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4590 "updating handles for sas_host(0x%016llx)\n",
4591 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4592
4593 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4594 * sizeof(Mpi2SasIOUnit0PhyData_t));
4595 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4596 if (!sas_iounit_pg0) {
4597 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4598 ioc->name, __FILE__, __LINE__, __func__);
4599 return;
4600 }
Eric Moore635374e2009-03-09 01:21:12 -06004601
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304602 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4603 sas_iounit_pg0, sz)) != 0)
4604 goto out;
4605 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4606 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4607 goto out;
4608 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304609 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304610 if (i == 0)
4611 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4612 PhyData[0].ControllerDevHandle);
4613 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4614 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4615 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304616 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4617 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304618 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304619 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304620 }
Eric Moore635374e2009-03-09 01:21:12 -06004621 out:
4622 kfree(sas_iounit_pg0);
4623}
4624
4625/**
4626 * _scsih_sas_host_add - create sas host object
4627 * @ioc: per adapter object
4628 *
4629 * Creating host side data object, stored in ioc->sas_hba
4630 *
4631 * Return nothing.
4632 */
4633static void
4634_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4635{
4636 int i;
4637 Mpi2ConfigReply_t mpi_reply;
4638 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4639 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4640 Mpi2SasPhyPage0_t phy_pg0;
4641 Mpi2SasDevicePage0_t sas_device_pg0;
4642 Mpi2SasEnclosurePage0_t enclosure_pg0;
4643 u16 ioc_status;
4644 u16 sz;
4645 u16 device_missing_delay;
4646
4647 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4648 if (!ioc->sas_hba.num_phys) {
4649 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4650 ioc->name, __FILE__, __LINE__, __func__);
4651 return;
4652 }
4653
4654 /* sas_iounit page 0 */
4655 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4656 sizeof(Mpi2SasIOUnit0PhyData_t));
4657 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4658 if (!sas_iounit_pg0) {
4659 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4660 ioc->name, __FILE__, __LINE__, __func__);
4661 return;
4662 }
4663 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4664 sas_iounit_pg0, sz))) {
4665 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4666 ioc->name, __FILE__, __LINE__, __func__);
4667 goto out;
4668 }
4669 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4670 MPI2_IOCSTATUS_MASK;
4671 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4672 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4673 ioc->name, __FILE__, __LINE__, __func__);
4674 goto out;
4675 }
4676
4677 /* sas_iounit page 1 */
4678 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4679 sizeof(Mpi2SasIOUnit1PhyData_t));
4680 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4681 if (!sas_iounit_pg1) {
4682 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4683 ioc->name, __FILE__, __LINE__, __func__);
4684 goto out;
4685 }
4686 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4687 sas_iounit_pg1, sz))) {
4688 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4689 ioc->name, __FILE__, __LINE__, __func__);
4690 goto out;
4691 }
4692 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4693 MPI2_IOCSTATUS_MASK;
4694 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4695 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4696 ioc->name, __FILE__, __LINE__, __func__);
4697 goto out;
4698 }
4699
4700 ioc->io_missing_delay =
4701 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4702 device_missing_delay =
4703 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4704 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4705 ioc->device_missing_delay = (device_missing_delay &
4706 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4707 else
4708 ioc->device_missing_delay = device_missing_delay &
4709 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4710
4711 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4712 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4713 sizeof(struct _sas_phy), GFP_KERNEL);
4714 if (!ioc->sas_hba.phy) {
4715 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4716 ioc->name, __FILE__, __LINE__, __func__);
4717 goto out;
4718 }
4719 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4720 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4721 i))) {
4722 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4723 ioc->name, __FILE__, __LINE__, __func__);
4724 goto out;
4725 }
4726 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4727 MPI2_IOCSTATUS_MASK;
4728 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4729 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4730 ioc->name, __FILE__, __LINE__, __func__);
4731 goto out;
4732 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304733
4734 if (i == 0)
4735 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4736 PhyData[0].ControllerDevHandle);
4737 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004738 ioc->sas_hba.phy[i].phy_id = i;
4739 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4740 phy_pg0, ioc->sas_hba.parent_dev);
4741 }
4742 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304743 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004744 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4745 ioc->name, __FILE__, __LINE__, __func__);
4746 goto out;
4747 }
Eric Moore635374e2009-03-09 01:21:12 -06004748 ioc->sas_hba.enclosure_handle =
4749 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4750 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4751 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4752 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4753 (unsigned long long) ioc->sas_hba.sas_address,
4754 ioc->sas_hba.num_phys) ;
4755
4756 if (ioc->sas_hba.enclosure_handle) {
4757 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4758 &enclosure_pg0,
4759 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4760 ioc->sas_hba.enclosure_handle))) {
4761 ioc->sas_hba.enclosure_logical_id =
4762 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4763 }
4764 }
4765
4766 out:
4767 kfree(sas_iounit_pg1);
4768 kfree(sas_iounit_pg0);
4769}
4770
4771/**
4772 * _scsih_expander_add - creating expander object
4773 * @ioc: per adapter object
4774 * @handle: expander handle
4775 *
4776 * Creating expander object, stored in ioc->sas_expander_list.
4777 *
4778 * Return 0 for success, else error.
4779 */
4780static int
4781_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4782{
4783 struct _sas_node *sas_expander;
4784 Mpi2ConfigReply_t mpi_reply;
4785 Mpi2ExpanderPage0_t expander_pg0;
4786 Mpi2ExpanderPage1_t expander_pg1;
4787 Mpi2SasEnclosurePage0_t enclosure_pg0;
4788 u32 ioc_status;
4789 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304790 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004791 int i;
4792 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304793 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004794 int rc = 0;
4795
4796 if (!handle)
4797 return -1;
4798
Eric Moore3cb54692010-07-08 14:44:34 -06004799 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304800 return -1;
4801
Eric Moore635374e2009-03-09 01:21:12 -06004802 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4803 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4804 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4805 ioc->name, __FILE__, __LINE__, __func__);
4806 return -1;
4807 }
4808
4809 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4810 MPI2_IOCSTATUS_MASK;
4811 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4812 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4813 ioc->name, __FILE__, __LINE__, __func__);
4814 return -1;
4815 }
4816
4817 /* handle out of order topology events */
4818 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304819 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4820 != 0) {
4821 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4822 ioc->name, __FILE__, __LINE__, __func__);
4823 return -1;
4824 }
4825 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004826 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304827 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4828 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004829 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4830 if (!sas_expander) {
4831 rc = _scsih_expander_add(ioc, parent_handle);
4832 if (rc != 0)
4833 return rc;
4834 }
4835 }
4836
Eric Moore635374e2009-03-09 01:21:12 -06004837 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304838 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004839 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4840 sas_address);
4841 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4842
4843 if (sas_expander)
4844 return 0;
4845
4846 sas_expander = kzalloc(sizeof(struct _sas_node),
4847 GFP_KERNEL);
4848 if (!sas_expander) {
4849 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4850 ioc->name, __FILE__, __LINE__, __func__);
4851 return -1;
4852 }
4853
4854 sas_expander->handle = handle;
4855 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304856 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004857 sas_expander->sas_address = sas_address;
4858
4859 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4860 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304861 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004862 sas_expander->sas_address, sas_expander->num_phys);
4863
4864 if (!sas_expander->num_phys)
4865 goto out_fail;
4866 sas_expander->phy = kcalloc(sas_expander->num_phys,
4867 sizeof(struct _sas_phy), GFP_KERNEL);
4868 if (!sas_expander->phy) {
4869 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4870 ioc->name, __FILE__, __LINE__, __func__);
4871 rc = -1;
4872 goto out_fail;
4873 }
4874
4875 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4876 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304877 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004878 if (!mpt2sas_port) {
4879 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4880 ioc->name, __FILE__, __LINE__, __func__);
4881 rc = -1;
4882 goto out_fail;
4883 }
4884 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4885
4886 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4887 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4888 &expander_pg1, i, handle))) {
4889 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4890 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304891 rc = -1;
4892 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004893 }
4894 sas_expander->phy[i].handle = handle;
4895 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304896
4897 if ((mpt2sas_transport_add_expander_phy(ioc,
4898 &sas_expander->phy[i], expander_pg1,
4899 sas_expander->parent_dev))) {
4900 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4901 ioc->name, __FILE__, __LINE__, __func__);
4902 rc = -1;
4903 goto out_fail;
4904 }
Eric Moore635374e2009-03-09 01:21:12 -06004905 }
4906
4907 if (sas_expander->enclosure_handle) {
4908 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4909 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4910 sas_expander->enclosure_handle))) {
4911 sas_expander->enclosure_logical_id =
4912 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4913 }
4914 }
4915
4916 _scsih_expander_node_add(ioc, sas_expander);
4917 return 0;
4918
4919 out_fail:
4920
Kashyap, Desai20f58952009-08-07 19:34:26 +05304921 if (mpt2sas_port)
4922 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304923 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004924 kfree(sas_expander);
4925 return rc;
4926}
4927
4928/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304929 * _scsih_done - scsih callback handler.
4930 * @ioc: per adapter object
4931 * @smid: system request message index
4932 * @msix_index: MSIX table index supplied by the OS
4933 * @reply: reply message frame(lower 32bit addr)
4934 *
4935 * Callback handler when sending internal generated message frames.
4936 * The callback index passed is `ioc->scsih_cb_idx`
4937 *
4938 * Return 1 meaning mf should be freed from _base_interrupt
4939 * 0 means the mf is freed from this function.
4940 */
4941static u8
4942_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4943{
4944 MPI2DefaultReply_t *mpi_reply;
4945
4946 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4947 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4948 return 1;
4949 if (ioc->scsih_cmds.smid != smid)
4950 return 1;
4951 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4952 if (mpi_reply) {
4953 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4954 mpi_reply->MsgLength*4);
4955 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4956 }
4957 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4958 complete(&ioc->scsih_cmds.done);
4959 return 1;
4960}
4961
4962/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304963 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004964 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304965 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004966 *
4967 * Return nothing.
4968 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304969void
4970mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004971{
4972 struct _sas_node *sas_expander;
4973 unsigned long flags;
4974
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304975 if (ioc->shost_recovery)
4976 return;
4977
Eric Moore635374e2009-03-09 01:21:12 -06004978 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304979 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4980 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304981 if (!sas_expander) {
4982 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4983 return;
4984 }
4985 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004986 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4987 _scsih_expander_node_remove(ioc, sas_expander);
4988}
4989
4990/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304991 * _scsih_check_access_status - check access flags
4992 * @ioc: per adapter object
4993 * @sas_address: sas address
4994 * @handle: sas device handle
4995 * @access_flags: errors returned during discovery of the device
4996 *
4997 * Return 0 for success, else failure
4998 */
4999static u8
5000_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5001 u16 handle, u8 access_status)
5002{
5003 u8 rc = 1;
5004 char *desc = NULL;
5005
5006 switch (access_status) {
5007 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5008 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5009 rc = 0;
5010 break;
5011 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5012 desc = "sata capability failed";
5013 break;
5014 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5015 desc = "sata affiliation conflict";
5016 break;
5017 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5018 desc = "route not addressable";
5019 break;
5020 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5021 desc = "smp error not addressable";
5022 break;
5023 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5024 desc = "device blocked";
5025 break;
5026 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5027 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5028 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5029 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5030 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5031 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5032 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5033 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5034 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5035 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5036 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5037 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5038 desc = "sata initialization failed";
5039 break;
5040 default:
5041 desc = "unknown";
5042 break;
5043 }
5044
5045 if (!rc)
5046 return 0;
5047
5048 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5049 "handle(0x%04x)\n", ioc->name, desc,
5050 (unsigned long long)sas_address, handle);
5051 return rc;
5052}
5053
5054static void
5055_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5056{
5057 Mpi2ConfigReply_t mpi_reply;
5058 Mpi2SasDevicePage0_t sas_device_pg0;
5059 struct _sas_device *sas_device;
5060 u32 ioc_status;
5061 unsigned long flags;
5062 u64 sas_address;
5063 struct scsi_target *starget;
5064 struct MPT2SAS_TARGET *sas_target_priv_data;
5065 u32 device_info;
5066
5067 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5068 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5069 return;
5070
5071 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5072 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5073 return;
5074
5075 /* check if this is end device */
5076 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5077 if (!(_scsih_is_end_device(device_info)))
5078 return;
5079
5080 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5081 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5082 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5083 sas_address);
5084
5085 if (!sas_device) {
5086 printk(MPT2SAS_ERR_FMT "device is not present "
5087 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5088 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5089 return;
5090 }
5091
5092 if (unlikely(sas_device->handle != handle)) {
5093 starget = sas_device->starget;
5094 sas_target_priv_data = starget->hostdata;
5095 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5096 " to (0x%04x)!!!\n", sas_device->handle, handle);
5097 sas_target_priv_data->handle = handle;
5098 sas_device->handle = handle;
5099 }
5100 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5101
5102 /* check if device is present */
5103 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5104 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5105 printk(MPT2SAS_ERR_FMT "device is not present "
5106 "handle(0x%04x), flags!!!\n", ioc->name, handle);
5107 return;
5108 }
5109
5110 /* check if there were any issues with discovery */
5111 if (_scsih_check_access_status(ioc, sas_address, handle,
5112 sas_device_pg0.AccessStatus))
5113 return;
5114 _scsih_ublock_io_device(ioc, handle);
5115
5116}
5117
5118/**
Eric Moore635374e2009-03-09 01:21:12 -06005119 * _scsih_add_device - creating sas device object
5120 * @ioc: per adapter object
5121 * @handle: sas device handle
5122 * @phy_num: phy number end device attached to
5123 * @is_pd: is this hidden raid component
5124 *
5125 * Creating end device object, stored in ioc->sas_device_list.
5126 *
5127 * Returns 0 for success, non-zero for failure.
5128 */
5129static int
5130_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5131{
5132 Mpi2ConfigReply_t mpi_reply;
5133 Mpi2SasDevicePage0_t sas_device_pg0;
5134 Mpi2SasEnclosurePage0_t enclosure_pg0;
5135 struct _sas_device *sas_device;
5136 u32 ioc_status;
5137 __le64 sas_address;
5138 u32 device_info;
5139 unsigned long flags;
5140
5141 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5142 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5143 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5144 ioc->name, __FILE__, __LINE__, __func__);
5145 return -1;
5146 }
5147
5148 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5149 MPI2_IOCSTATUS_MASK;
5150 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5152 ioc->name, __FILE__, __LINE__, __func__);
5153 return -1;
5154 }
5155
Kashyap, Desaib4344272010-03-17 16:24:14 +05305156 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5157
Eric Moore635374e2009-03-09 01:21:12 -06005158 /* check if device is present */
5159 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5160 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5161 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5162 ioc->name, __FILE__, __LINE__, __func__);
5163 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5164 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5165 return -1;
5166 }
5167
Kashyap, Desaib4344272010-03-17 16:24:14 +05305168 /* check if there were any issues with discovery */
5169 if (_scsih_check_access_status(ioc, sas_address, handle,
5170 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005171 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005172
5173 /* check if this is end device */
5174 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5175 if (!(_scsih_is_end_device(device_info))) {
5176 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5177 ioc->name, __FILE__, __LINE__, __func__);
5178 return -1;
5179 }
5180
Eric Moore635374e2009-03-09 01:21:12 -06005181
5182 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5183 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5184 sas_address);
5185 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5186
Kashyap, Desaib4344272010-03-17 16:24:14 +05305187 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005188 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005189
5190 sas_device = kzalloc(sizeof(struct _sas_device),
5191 GFP_KERNEL);
5192 if (!sas_device) {
5193 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5194 ioc->name, __FILE__, __LINE__, __func__);
5195 return -1;
5196 }
5197
5198 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305199 if (_scsih_get_sas_address(ioc, le16_to_cpu
5200 (sas_device_pg0.ParentDevHandle),
5201 &sas_device->sas_address_parent) != 0)
5202 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5203 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005204 sas_device->enclosure_handle =
5205 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5206 sas_device->slot =
5207 le16_to_cpu(sas_device_pg0.Slot);
5208 sas_device->device_info = device_info;
5209 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305210 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005211
5212 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305213 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5214 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5215 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005216 sas_device->enclosure_logical_id =
5217 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005218
5219 /* get device name */
5220 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5221
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305222 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005223 _scsih_sas_device_init_add(ioc, sas_device);
5224 else
5225 _scsih_sas_device_add(ioc, sas_device);
5226
5227 return 0;
5228}
5229
5230/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305231 * _scsih_remove_device - removing sas device object
5232 * @ioc: per adapter object
5233 * @sas_device_delete: the sas_device object
5234 *
5235 * Return nothing.
5236 */
5237static void
5238_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5239 struct _sas_device *sas_device)
5240{
5241 struct _sas_device sas_device_backup;
5242 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305243
Kashyap, Desai1278b112010-03-09 17:34:13 +05305244 if (!sas_device)
5245 return;
Eric Moore635374e2009-03-09 01:21:12 -06005246
Kashyap, Desai1278b112010-03-09 17:34:13 +05305247 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06005248 _scsih_sas_device_remove(ioc, sas_device);
5249
Kashyap, Desai1278b112010-03-09 17:34:13 +05305250 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5251 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5252 sas_device_backup.handle, (unsigned long long)
5253 sas_device_backup.sas_address));
5254
5255 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5256 sas_target_priv_data = sas_device_backup.starget->hostdata;
5257 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305258 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5259 sas_target_priv_data->handle =
5260 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305261 }
5262
Kashyap, Desai1278b112010-03-09 17:34:13 +05305263 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5264
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305265 if (!ioc->hide_drives)
5266 mpt2sas_transport_port_remove(ioc,
5267 sas_device_backup.sas_address,
5268 sas_device_backup.sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305269
5270 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5271 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5272 (unsigned long long) sas_device_backup.sas_address);
5273
5274 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5275 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5276 sas_device_backup.handle, (unsigned long long)
5277 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06005278}
5279
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305280/**
5281 * mpt2sas_device_remove - removing device object
5282 * @ioc: per adapter object
5283 * @sas_address: expander sas_address
5284 *
5285 * Return nothing.
5286 */
5287void
5288mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5289{
5290 struct _sas_device *sas_device;
5291 unsigned long flags;
5292
5293 if (ioc->shost_recovery)
5294 return;
5295
5296 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5297 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5298 sas_address);
5299 if (!sas_device) {
5300 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5301 return;
5302 }
5303 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5304 _scsih_remove_device(ioc, sas_device);
5305}
5306
Eric Moore635374e2009-03-09 01:21:12 -06005307#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5308/**
5309 * _scsih_sas_topology_change_event_debug - debug for topology event
5310 * @ioc: per adapter object
5311 * @event_data: event data payload
5312 * Context: user.
5313 */
5314static void
5315_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5316 Mpi2EventDataSasTopologyChangeList_t *event_data)
5317{
5318 int i;
5319 u16 handle;
5320 u16 reason_code;
5321 u8 phy_number;
5322 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305323 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005324
5325 switch (event_data->ExpStatus) {
5326 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5327 status_str = "add";
5328 break;
5329 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5330 status_str = "remove";
5331 break;
5332 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305333 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005334 status_str = "responding";
5335 break;
5336 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5337 status_str = "remove delay";
5338 break;
5339 default:
5340 status_str = "unknown status";
5341 break;
5342 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305343 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005344 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305345 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005346 "start_phy(%02d), count(%d)\n",
5347 le16_to_cpu(event_data->ExpanderDevHandle),
5348 le16_to_cpu(event_data->EnclosureHandle),
5349 event_data->StartPhyNum, event_data->NumEntries);
5350 for (i = 0; i < event_data->NumEntries; i++) {
5351 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5352 if (!handle)
5353 continue;
5354 phy_number = event_data->StartPhyNum + i;
5355 reason_code = event_data->PHY[i].PhyStatus &
5356 MPI2_EVENT_SAS_TOPO_RC_MASK;
5357 switch (reason_code) {
5358 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305359 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005360 break;
5361 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305362 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005363 break;
5364 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305365 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005366 break;
5367 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305368 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005369 break;
5370 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305371 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005372 break;
5373 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305374 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005375 break;
5376 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305377 link_rate = event_data->PHY[i].LinkRate >> 4;
5378 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305379 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305380 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5381 handle, status_str, link_rate, prev_link_rate);
5382
Eric Moore635374e2009-03-09 01:21:12 -06005383 }
5384}
5385#endif
5386
5387/**
5388 * _scsih_sas_topology_change_event - handle topology changes
5389 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305390 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005391 * Context: user.
5392 *
5393 */
5394static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305395_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005396 struct fw_event_work *fw_event)
5397{
5398 int i;
5399 u16 parent_handle, handle;
5400 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305401 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005402 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305403 struct _sas_device *sas_device;
5404 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005405 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305406 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305407 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005408
5409#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5410 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5411 _scsih_sas_topology_change_event_debug(ioc, event_data);
5412#endif
5413
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305414 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305415 return;
5416
Eric Moore635374e2009-03-09 01:21:12 -06005417 if (!ioc->sas_hba.num_phys)
5418 _scsih_sas_host_add(ioc);
5419 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305420 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005421
5422 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305423 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005424 "event\n", ioc->name));
5425 return;
5426 }
5427
5428 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5429
5430 /* handle expander add */
5431 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5432 if (_scsih_expander_add(ioc, parent_handle) != 0)
5433 return;
5434
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305435 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5436 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5437 parent_handle);
5438 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305439 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305440 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305441 max_phys = sas_expander->num_phys;
5442 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305443 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305444 max_phys = ioc->sas_hba.num_phys;
5445 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305446 return;
5447
Eric Moore635374e2009-03-09 01:21:12 -06005448 /* handle siblings events */
5449 for (i = 0; i < event_data->NumEntries; i++) {
5450 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305451 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005452 "expander event\n", ioc->name));
5453 return;
5454 }
Eric Moore3cb54692010-07-08 14:44:34 -06005455 if (ioc->shost_recovery || ioc->remove_host ||
5456 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305457 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305458 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305459 if (phy_number >= max_phys)
5460 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305461 reason_code = event_data->PHY[i].PhyStatus &
5462 MPI2_EVENT_SAS_TOPO_RC_MASK;
5463 if ((event_data->PHY[i].PhyStatus &
5464 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5465 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005466 continue;
5467 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5468 if (!handle)
5469 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305470 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305471 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005472 switch (reason_code) {
5473 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305474
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305475 if (ioc->shost_recovery)
5476 break;
5477
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305478 if (link_rate == prev_link_rate)
5479 break;
5480
5481 mpt2sas_transport_update_links(ioc, sas_address,
5482 handle, phy_number, link_rate);
5483
Kashyap, Desaib4344272010-03-17 16:24:14 +05305484 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5485 break;
5486
5487 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305488 break;
Eric Moore635374e2009-03-09 01:21:12 -06005489 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305490
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305491 if (ioc->shost_recovery)
5492 break;
5493
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305494 mpt2sas_transport_update_links(ioc, sas_address,
5495 handle, phy_number, link_rate);
5496
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305497 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005498 break;
5499 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305500
5501 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5502 sas_device = _scsih_sas_device_find_by_handle(ioc,
5503 handle);
5504 if (!sas_device) {
5505 spin_unlock_irqrestore(&ioc->sas_device_lock,
5506 flags);
5507 break;
5508 }
5509 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5510 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005511 break;
5512 }
5513 }
5514
5515 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305516 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5517 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305518 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005519
5520}
5521
5522#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5523/**
5524 * _scsih_sas_device_status_change_event_debug - debug for device event
5525 * @event_data: event data payload
5526 * Context: user.
5527 *
5528 * Return nothing.
5529 */
5530static void
5531_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5532 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5533{
5534 char *reason_str = NULL;
5535
5536 switch (event_data->ReasonCode) {
5537 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5538 reason_str = "smart data";
5539 break;
5540 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5541 reason_str = "unsupported device discovered";
5542 break;
5543 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5544 reason_str = "internal device reset";
5545 break;
5546 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5547 reason_str = "internal task abort";
5548 break;
5549 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5550 reason_str = "internal task abort set";
5551 break;
5552 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5553 reason_str = "internal clear task set";
5554 break;
5555 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5556 reason_str = "internal query task";
5557 break;
5558 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5559 reason_str = "sata init failure";
5560 break;
5561 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5562 reason_str = "internal device reset complete";
5563 break;
5564 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5565 reason_str = "internal task abort complete";
5566 break;
5567 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5568 reason_str = "internal async notification";
5569 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305570 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5571 reason_str = "expander reduced functionality";
5572 break;
5573 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5574 reason_str = "expander reduced functionality complete";
5575 break;
Eric Moore635374e2009-03-09 01:21:12 -06005576 default:
5577 reason_str = "unknown reason";
5578 break;
5579 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305580 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305581 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5582 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5583 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5584 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005585 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305586 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005587 event_data->ASC, event_data->ASCQ);
5588 printk(KERN_INFO "\n");
5589}
5590#endif
5591
5592/**
5593 * _scsih_sas_device_status_change_event - handle device status change
5594 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305595 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005596 * Context: user.
5597 *
5598 * Return nothing.
5599 */
5600static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305601_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5602 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005603{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305604 struct MPT2SAS_TARGET *target_priv_data;
5605 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305606 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305607 unsigned long flags;
5608 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5609 fw_event->event_data;
5610
Eric Moore635374e2009-03-09 01:21:12 -06005611#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5612 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305613 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305614 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005615#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305616
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305617 /* In MPI Revision K (0xC), the internal device reset complete was
5618 * implemented, so avoid setting tm_busy flag for older firmware.
5619 */
5620 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5621 return;
5622
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305623 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305624 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305625 event_data->ReasonCode !=
5626 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305627 return;
5628
5629 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5630 sas_address = le64_to_cpu(event_data->SASAddress);
5631 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5632 sas_address);
5633 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5634
5635 if (!sas_device || !sas_device->starget)
5636 return;
5637
5638 target_priv_data = sas_device->starget->hostdata;
5639 if (!target_priv_data)
5640 return;
5641
5642 if (event_data->ReasonCode ==
5643 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5644 target_priv_data->tm_busy = 1;
5645 else
5646 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005647}
5648
5649#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5650/**
5651 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5652 * @ioc: per adapter object
5653 * @event_data: event data payload
5654 * Context: user.
5655 *
5656 * Return nothing.
5657 */
5658static void
5659_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5660 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5661{
5662 char *reason_str = NULL;
5663
5664 switch (event_data->ReasonCode) {
5665 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5666 reason_str = "enclosure add";
5667 break;
5668 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5669 reason_str = "enclosure remove";
5670 break;
5671 default:
5672 reason_str = "unknown reason";
5673 break;
5674 }
5675
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305676 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005677 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5678 " number slots(%d)\n", ioc->name, reason_str,
5679 le16_to_cpu(event_data->EnclosureHandle),
5680 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5681 le16_to_cpu(event_data->StartSlot));
5682}
5683#endif
5684
5685/**
5686 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5687 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305688 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005689 * Context: user.
5690 *
5691 * Return nothing.
5692 */
5693static void
5694_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305695 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005696{
5697#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5698 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5699 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305700 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005701#endif
5702}
5703
5704/**
5705 * _scsih_sas_broadcast_primative_event - handle broadcast events
5706 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305707 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005708 * Context: user.
5709 *
5710 * Return nothing.
5711 */
5712static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305713_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5714 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005715{
5716 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305717 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005718 u16 smid, handle;
5719 u32 lun;
5720 struct MPT2SAS_DEVICE *sas_device_priv_data;
5721 u32 termination_count;
5722 u32 query_count;
5723 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305724 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305725 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305726 unsigned long flags;
5727 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305728 u8 max_retries = 0;
5729 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305730
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305731 mutex_lock(&ioc->tm_cmds.mutex);
5732 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5733 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5734 event_data->PortWidth));
5735
5736 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005737
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305738 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305739 mpi_reply = ioc->tm_cmds.reply;
5740broadcast_aen_retry:
5741
5742 /* sanity checks for retrying this loop */
5743 if (max_retries++ == 5) {
5744 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5745 ioc->name, __func__));
5746 goto out;
5747 } else if (max_retries > 1)
5748 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5749 ioc->name, __func__, max_retries - 1));
5750
Eric Moore635374e2009-03-09 01:21:12 -06005751 termination_count = 0;
5752 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305753 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305754 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305755 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005756 scmd = _scsih_scsi_lookup_get(ioc, smid);
5757 if (!scmd)
5758 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305759 sdev = scmd->device;
5760 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005761 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5762 continue;
5763 /* skip hidden raid components */
5764 if (sas_device_priv_data->sas_target->flags &
5765 MPT_TARGET_FLAGS_RAID_COMPONENT)
5766 continue;
5767 /* skip volumes */
5768 if (sas_device_priv_data->sas_target->flags &
5769 MPT_TARGET_FLAGS_VOLUME)
5770 continue;
5771
5772 handle = sas_device_priv_data->sas_target->handle;
5773 lun = sas_device_priv_data->lun;
5774 query_count++;
5775
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305776 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305777 goto out;
5778
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305779 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305780 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5781 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5782 TM_MUTEX_OFF);
5783 if (r == FAILED) {
5784 sdev_printk(KERN_WARNING, sdev,
5785 "mpt2sas_scsih_issue_tm: FAILED when sending "
5786 "QUERY_TASK: scmd(%p)\n", scmd);
5787 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5788 goto broadcast_aen_retry;
5789 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305790 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5791 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305792 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5793 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5794 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5795 scmd);
5796 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5797 goto broadcast_aen_retry;
5798 }
5799
5800 /* see if IO is still owned by IOC and target */
5801 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005802 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5803 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305804 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305805 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005806 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305807 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305808 task_abort_retries = 0;
5809 tm_retry:
5810 if (task_abort_retries++ == 60) {
5811 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5812 "%s: ABORT_TASK: giving up\n", ioc->name,
5813 __func__));
5814 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5815 goto broadcast_aen_retry;
5816 }
5817
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305818 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305819 goto out_no_lock;
5820
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305821 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5822 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305823 scmd->serial_number, TM_MUTEX_OFF);
5824 if (r == FAILED) {
5825 sdev_printk(KERN_WARNING, sdev,
5826 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305827 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305828 goto tm_retry;
5829 }
5830
5831 if (task_abort_retries > 1)
5832 sdev_printk(KERN_WARNING, sdev,
5833 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5834 " scmd(%p)\n",
5835 task_abort_retries - 1, scmd);
5836
Eric Moore635374e2009-03-09 01:21:12 -06005837 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305838 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005839 }
Eric Moore635374e2009-03-09 01:21:12 -06005840
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305841 if (ioc->broadcast_aen_pending) {
5842 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5843 " pending AEN\n", ioc->name, __func__));
5844 ioc->broadcast_aen_pending = 0;
5845 goto broadcast_aen_retry;
5846 }
5847
5848 out:
5849 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5850 out_no_lock:
5851
5852 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005853 "%s - exit, query_count = %d termination_count = %d\n",
5854 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305855
5856 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305857 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305858 _scsih_ublock_io_all_device(ioc);
5859 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005860}
5861
5862/**
5863 * _scsih_sas_discovery_event - handle discovery events
5864 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305865 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005866 * Context: user.
5867 *
5868 * Return nothing.
5869 */
5870static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305871_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5872 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005873{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305874 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5875
Eric Moore635374e2009-03-09 01:21:12 -06005876#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5877 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305878 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005879 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5880 "start" : "stop");
5881 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305882 printk("discovery_status(0x%08x)",
5883 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005884 printk("\n");
5885 }
5886#endif
5887
5888 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5889 !ioc->sas_hba.num_phys)
5890 _scsih_sas_host_add(ioc);
5891}
5892
5893/**
5894 * _scsih_reprobe_lun - reprobing lun
5895 * @sdev: scsi device struct
5896 * @no_uld_attach: sdev->no_uld_attach flag setting
5897 *
5898 **/
5899static void
5900_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5901{
5902 int rc;
5903
5904 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5905 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5906 sdev->no_uld_attach ? "hidding" : "exposing");
5907 rc = scsi_device_reprobe(sdev);
5908}
5909
5910/**
5911 * _scsih_reprobe_target - reprobing target
5912 * @starget: scsi target struct
5913 * @no_uld_attach: sdev->no_uld_attach flag setting
5914 *
5915 * Note: no_uld_attach flag determines whether the disk device is attached
5916 * to block layer. A value of `1` means to not attach.
5917 **/
5918static void
5919_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5920{
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305921 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06005922
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305923 if (starget == NULL)
5924 return;
5925 sas_target_priv_data = starget->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005926 if (no_uld_attach)
5927 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5928 else
5929 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5930
5931 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5932 _scsih_reprobe_lun);
5933}
5934/**
5935 * _scsih_sas_volume_add - add new volume
5936 * @ioc: per adapter object
5937 * @element: IR config element data
5938 * Context: user.
5939 *
5940 * Return nothing.
5941 */
5942static void
5943_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5944 Mpi2EventIrConfigElement_t *element)
5945{
5946 struct _raid_device *raid_device;
5947 unsigned long flags;
5948 u64 wwid;
5949 u16 handle = le16_to_cpu(element->VolDevHandle);
5950 int rc;
5951
Eric Moore635374e2009-03-09 01:21:12 -06005952 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5953 if (!wwid) {
5954 printk(MPT2SAS_ERR_FMT
5955 "failure at %s:%d/%s()!\n", ioc->name,
5956 __FILE__, __LINE__, __func__);
5957 return;
5958 }
5959
5960 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5961 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5962 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5963
5964 if (raid_device)
5965 return;
5966
5967 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5968 if (!raid_device) {
5969 printk(MPT2SAS_ERR_FMT
5970 "failure at %s:%d/%s()!\n", ioc->name,
5971 __FILE__, __LINE__, __func__);
5972 return;
5973 }
5974
5975 raid_device->id = ioc->sas_id++;
5976 raid_device->channel = RAID_CHANNEL;
5977 raid_device->handle = handle;
5978 raid_device->wwid = wwid;
5979 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305980 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06005981 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5982 raid_device->id, 0);
5983 if (rc)
5984 _scsih_raid_device_remove(ioc, raid_device);
5985 } else
5986 _scsih_determine_boot_device(ioc, raid_device, 1);
5987}
5988
5989/**
5990 * _scsih_sas_volume_delete - delete volume
5991 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305992 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005993 * Context: user.
5994 *
5995 * Return nothing.
5996 */
5997static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305998_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005999{
6000 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006001 unsigned long flags;
6002 struct MPT2SAS_TARGET *sas_target_priv_data;
6003
Eric Moore635374e2009-03-09 01:21:12 -06006004 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6005 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6006 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6007 if (!raid_device)
6008 return;
6009 if (raid_device->starget) {
6010 sas_target_priv_data = raid_device->starget->hostdata;
6011 sas_target_priv_data->deleted = 1;
6012 scsi_remove_target(&raid_device->starget->dev);
6013 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306014 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6015 "(0x%016llx)\n", ioc->name, raid_device->handle,
6016 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006017 _scsih_raid_device_remove(ioc, raid_device);
6018}
6019
6020/**
6021 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6022 * @ioc: per adapter object
6023 * @element: IR config element data
6024 * Context: user.
6025 *
6026 * Return nothing.
6027 */
6028static void
6029_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6030 Mpi2EventIrConfigElement_t *element)
6031{
6032 struct _sas_device *sas_device;
6033 unsigned long flags;
6034 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6035
6036 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6037 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6038 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6039 if (!sas_device)
6040 return;
6041
6042 /* exposing raid component */
6043 sas_device->volume_handle = 0;
6044 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306045 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006046 _scsih_reprobe_target(sas_device->starget, 0);
6047}
6048
6049/**
6050 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6051 * @ioc: per adapter object
6052 * @element: IR config element data
6053 * Context: user.
6054 *
6055 * Return nothing.
6056 */
6057static void
6058_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6059 Mpi2EventIrConfigElement_t *element)
6060{
6061 struct _sas_device *sas_device;
6062 unsigned long flags;
6063 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6064
6065 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6066 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6067 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6068 if (!sas_device)
6069 return;
6070
6071 /* hiding raid component */
6072 mpt2sas_config_get_volume_handle(ioc, handle,
6073 &sas_device->volume_handle);
6074 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
6075 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306076 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006077 _scsih_reprobe_target(sas_device->starget, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306078
Eric Moore635374e2009-03-09 01:21:12 -06006079}
6080
6081/**
6082 * _scsih_sas_pd_delete - delete pd component
6083 * @ioc: per adapter object
6084 * @element: IR config element data
6085 * Context: user.
6086 *
6087 * Return nothing.
6088 */
6089static void
6090_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6091 Mpi2EventIrConfigElement_t *element)
6092{
6093 struct _sas_device *sas_device;
6094 unsigned long flags;
6095 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6096
6097 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6098 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6099 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6100 if (!sas_device)
6101 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306102 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006103}
6104
6105/**
6106 * _scsih_sas_pd_add - remove pd component
6107 * @ioc: per adapter object
6108 * @element: IR config element data
6109 * Context: user.
6110 *
6111 * Return nothing.
6112 */
6113static void
6114_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6115 Mpi2EventIrConfigElement_t *element)
6116{
6117 struct _sas_device *sas_device;
6118 unsigned long flags;
6119 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306120 Mpi2ConfigReply_t mpi_reply;
6121 Mpi2SasDevicePage0_t sas_device_pg0;
6122 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306123 u64 sas_address;
6124 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006125
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306126 set_bit(handle, ioc->pd_handles);
6127
Eric Moore635374e2009-03-09 01:21:12 -06006128 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6129 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6130 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306131 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306132 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306133
6134 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6135 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6136 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6137 ioc->name, __FILE__, __LINE__, __func__);
6138 return;
6139 }
6140
6141 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6142 MPI2_IOCSTATUS_MASK;
6143 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6144 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6145 ioc->name, __FILE__, __LINE__, __func__);
6146 return;
6147 }
6148
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306149 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6150 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6151 mpt2sas_transport_update_links(ioc, sas_address, handle,
6152 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306153
6154 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006155}
6156
6157#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6158/**
6159 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6160 * @ioc: per adapter object
6161 * @event_data: event data payload
6162 * Context: user.
6163 *
6164 * Return nothing.
6165 */
6166static void
6167_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6168 Mpi2EventDataIrConfigChangeList_t *event_data)
6169{
6170 Mpi2EventIrConfigElement_t *element;
6171 u8 element_type;
6172 int i;
6173 char *reason_str = NULL, *element_str = NULL;
6174
6175 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6176
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306177 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006178 ioc->name, (le32_to_cpu(event_data->Flags) &
6179 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6180 "foreign" : "native", event_data->NumElements);
6181 for (i = 0; i < event_data->NumElements; i++, element++) {
6182 switch (element->ReasonCode) {
6183 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6184 reason_str = "add";
6185 break;
6186 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6187 reason_str = "remove";
6188 break;
6189 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6190 reason_str = "no change";
6191 break;
6192 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6193 reason_str = "hide";
6194 break;
6195 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6196 reason_str = "unhide";
6197 break;
6198 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6199 reason_str = "volume_created";
6200 break;
6201 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6202 reason_str = "volume_deleted";
6203 break;
6204 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6205 reason_str = "pd_created";
6206 break;
6207 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6208 reason_str = "pd_deleted";
6209 break;
6210 default:
6211 reason_str = "unknown reason";
6212 break;
6213 }
6214 element_type = le16_to_cpu(element->ElementFlags) &
6215 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6216 switch (element_type) {
6217 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6218 element_str = "volume";
6219 break;
6220 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6221 element_str = "phys disk";
6222 break;
6223 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6224 element_str = "hot spare";
6225 break;
6226 default:
6227 element_str = "unknown element";
6228 break;
6229 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306230 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006231 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6232 reason_str, le16_to_cpu(element->VolDevHandle),
6233 le16_to_cpu(element->PhysDiskDevHandle),
6234 element->PhysDiskNum);
6235 }
6236}
6237#endif
6238
6239/**
6240 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6241 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306242 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006243 * Context: user.
6244 *
6245 * Return nothing.
6246 */
6247static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306248_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6249 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006250{
6251 Mpi2EventIrConfigElement_t *element;
6252 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306253 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306254 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006255
6256#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306257 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6258 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006259 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6260
6261#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306262
6263 if (ioc->shost_recovery)
6264 return;
6265
Kashyap, Desai62727a72009-08-07 19:35:18 +05306266 foreign_config = (le32_to_cpu(event_data->Flags) &
6267 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006268
6269 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6270 for (i = 0; i < event_data->NumElements; i++, element++) {
6271
6272 switch (element->ReasonCode) {
6273 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6274 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306275 if (!foreign_config)
6276 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006277 break;
6278 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6279 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306280 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306281 _scsih_sas_volume_delete(ioc,
6282 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006283 break;
6284 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306285 if (!ioc->is_warpdrive)
6286 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006287 break;
6288 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306289 if (!ioc->is_warpdrive)
6290 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006291 break;
6292 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306293 if (!ioc->is_warpdrive)
6294 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006295 break;
6296 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306297 if (!ioc->is_warpdrive)
6298 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006299 break;
6300 }
6301 }
6302}
6303
6304/**
6305 * _scsih_sas_ir_volume_event - IR volume event
6306 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306307 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006308 * Context: user.
6309 *
6310 * Return nothing.
6311 */
6312static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306313_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6314 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006315{
6316 u64 wwid;
6317 unsigned long flags;
6318 struct _raid_device *raid_device;
6319 u16 handle;
6320 u32 state;
6321 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306322 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006323
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306324 if (ioc->shost_recovery)
6325 return;
6326
Eric Moore635374e2009-03-09 01:21:12 -06006327 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6328 return;
6329
6330 handle = le16_to_cpu(event_data->VolDevHandle);
6331 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306332 if (!ioc->hide_ir_msg)
6333 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6334 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6335 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006336
Eric Moore635374e2009-03-09 01:21:12 -06006337 switch (state) {
6338 case MPI2_RAID_VOL_STATE_MISSING:
6339 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306340 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006341 break;
6342
6343 case MPI2_RAID_VOL_STATE_ONLINE:
6344 case MPI2_RAID_VOL_STATE_DEGRADED:
6345 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306346
6347 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6348 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6349 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6350
Eric Moore635374e2009-03-09 01:21:12 -06006351 if (raid_device)
6352 break;
6353
6354 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6355 if (!wwid) {
6356 printk(MPT2SAS_ERR_FMT
6357 "failure at %s:%d/%s()!\n", ioc->name,
6358 __FILE__, __LINE__, __func__);
6359 break;
6360 }
6361
6362 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6363 if (!raid_device) {
6364 printk(MPT2SAS_ERR_FMT
6365 "failure at %s:%d/%s()!\n", ioc->name,
6366 __FILE__, __LINE__, __func__);
6367 break;
6368 }
6369
6370 raid_device->id = ioc->sas_id++;
6371 raid_device->channel = RAID_CHANNEL;
6372 raid_device->handle = handle;
6373 raid_device->wwid = wwid;
6374 _scsih_raid_device_add(ioc, raid_device);
6375 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6376 raid_device->id, 0);
6377 if (rc)
6378 _scsih_raid_device_remove(ioc, raid_device);
6379 break;
6380
6381 case MPI2_RAID_VOL_STATE_INITIALIZING:
6382 default:
6383 break;
6384 }
6385}
6386
6387/**
6388 * _scsih_sas_ir_physical_disk_event - PD event
6389 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306390 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006391 * Context: user.
6392 *
6393 * Return nothing.
6394 */
6395static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306396_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6397 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006398{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306399 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006400 u32 state;
6401 struct _sas_device *sas_device;
6402 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306403 Mpi2ConfigReply_t mpi_reply;
6404 Mpi2SasDevicePage0_t sas_device_pg0;
6405 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306406 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306407 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006408
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306409 if (ioc->shost_recovery)
6410 return;
6411
Eric Moore635374e2009-03-09 01:21:12 -06006412 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6413 return;
6414
6415 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6416 state = le32_to_cpu(event_data->NewValue);
6417
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306418 if (!ioc->hide_ir_msg)
6419 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6420 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6421 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006422
Eric Moore635374e2009-03-09 01:21:12 -06006423 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006424 case MPI2_RAID_PD_STATE_ONLINE:
6425 case MPI2_RAID_PD_STATE_DEGRADED:
6426 case MPI2_RAID_PD_STATE_REBUILDING:
6427 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306428 case MPI2_RAID_PD_STATE_HOT_SPARE:
6429
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306430 if (!ioc->is_warpdrive)
6431 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306432
6433 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6434 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6435 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6436
6437 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306438 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306439
6440 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6441 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6442 handle))) {
6443 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6444 ioc->name, __FILE__, __LINE__, __func__);
6445 return;
6446 }
6447
6448 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6449 MPI2_IOCSTATUS_MASK;
6450 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6451 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6452 ioc->name, __FILE__, __LINE__, __func__);
6453 return;
6454 }
6455
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306456 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6457 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6458 mpt2sas_transport_update_links(ioc, sas_address, handle,
6459 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306460
6461 _scsih_add_device(ioc, handle, 0, 1);
6462
Eric Moore635374e2009-03-09 01:21:12 -06006463 break;
6464
Kashyap, Desai62727a72009-08-07 19:35:18 +05306465 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006466 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6467 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006468 default:
6469 break;
6470 }
6471}
6472
6473#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6474/**
6475 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6476 * @ioc: per adapter object
6477 * @event_data: event data payload
6478 * Context: user.
6479 *
6480 * Return nothing.
6481 */
6482static void
6483_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6484 Mpi2EventDataIrOperationStatus_t *event_data)
6485{
6486 char *reason_str = NULL;
6487
6488 switch (event_data->RAIDOperation) {
6489 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6490 reason_str = "resync";
6491 break;
6492 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6493 reason_str = "online capacity expansion";
6494 break;
6495 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6496 reason_str = "consistency check";
6497 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306498 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6499 reason_str = "background init";
6500 break;
6501 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6502 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006503 break;
6504 }
6505
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306506 if (!reason_str)
6507 return;
6508
Eric Moore635374e2009-03-09 01:21:12 -06006509 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6510 "\thandle(0x%04x), percent complete(%d)\n",
6511 ioc->name, reason_str,
6512 le16_to_cpu(event_data->VolDevHandle),
6513 event_data->PercentComplete);
6514}
6515#endif
6516
6517/**
6518 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6519 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306520 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006521 * Context: user.
6522 *
6523 * Return nothing.
6524 */
6525static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306526_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6527 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006528{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306529 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6530 static struct _raid_device *raid_device;
6531 unsigned long flags;
6532 u16 handle;
6533
Eric Moore635374e2009-03-09 01:21:12 -06006534#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306535 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6536 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306537 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306538 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006539#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306540
6541 /* code added for raid transport support */
6542 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6543
6544 handle = le16_to_cpu(event_data->VolDevHandle);
6545
6546 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6547 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6548 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6549
6550 if (!raid_device)
6551 return;
6552
6553 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6554 raid_device->percent_complete =
6555 event_data->PercentComplete;
6556 }
Eric Moore635374e2009-03-09 01:21:12 -06006557}
6558
6559/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306560 * _scsih_prep_device_scan - initialize parameters prior to device scan
6561 * @ioc: per adapter object
6562 *
6563 * Set the deleted flag prior to device scan. If the device is found during
6564 * the scan, then we clear the deleted flag.
6565 */
6566static void
6567_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6568{
6569 struct MPT2SAS_DEVICE *sas_device_priv_data;
6570 struct scsi_device *sdev;
6571
6572 shost_for_each_device(sdev, ioc->shost) {
6573 sas_device_priv_data = sdev->hostdata;
6574 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6575 sas_device_priv_data->sas_target->deleted = 1;
6576 }
6577}
6578
6579/**
Eric Moore635374e2009-03-09 01:21:12 -06006580 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6581 * @ioc: per adapter object
6582 * @sas_address: sas address
6583 * @slot: enclosure slot id
6584 * @handle: device handle
6585 *
6586 * After host reset, find out whether devices are still responding.
6587 * Used in _scsi_remove_unresponsive_sas_devices.
6588 *
6589 * Return nothing.
6590 */
6591static void
6592_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6593 u16 slot, u16 handle)
6594{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306595 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006596 struct scsi_target *starget;
6597 struct _sas_device *sas_device;
6598 unsigned long flags;
6599
6600 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6601 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6602 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306603 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006604 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306605 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306606 if (starget && starget->hostdata) {
6607 sas_target_priv_data = starget->hostdata;
6608 sas_target_priv_data->tm_busy = 0;
6609 sas_target_priv_data->deleted = 0;
6610 } else
6611 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306612 if (starget)
6613 starget_printk(KERN_INFO, starget,
6614 "handle(0x%04x), sas_addr(0x%016llx), "
6615 "enclosure logical id(0x%016llx), "
6616 "slot(%d)\n", handle,
6617 (unsigned long long)sas_device->sas_address,
6618 (unsigned long long)
6619 sas_device->enclosure_logical_id,
6620 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006621 if (sas_device->handle == handle)
6622 goto out;
6623 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6624 sas_device->handle);
6625 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306626 if (sas_target_priv_data)
6627 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006628 goto out;
6629 }
6630 }
6631 out:
6632 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6633}
6634
6635/**
6636 * _scsih_search_responding_sas_devices -
6637 * @ioc: per adapter object
6638 *
6639 * After host reset, find out whether devices are still responding.
6640 * If not remove.
6641 *
6642 * Return nothing.
6643 */
6644static void
6645_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6646{
6647 Mpi2SasDevicePage0_t sas_device_pg0;
6648 Mpi2ConfigReply_t mpi_reply;
6649 u16 ioc_status;
6650 __le64 sas_address;
6651 u16 handle;
6652 u32 device_info;
6653 u16 slot;
6654
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306655 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006656
6657 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306658 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006659
6660 handle = 0xFFFF;
6661 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6662 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6663 handle))) {
6664 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6665 MPI2_IOCSTATUS_MASK;
6666 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6667 break;
6668 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6669 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6670 if (!(_scsih_is_end_device(device_info)))
6671 continue;
6672 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6673 slot = le16_to_cpu(sas_device_pg0.Slot);
6674 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6675 handle);
6676 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306677out:
6678 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6679 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006680}
6681
6682/**
6683 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6684 * @ioc: per adapter object
6685 * @wwid: world wide identifier for raid volume
6686 * @handle: device handle
6687 *
6688 * After host reset, find out whether devices are still responding.
6689 * Used in _scsi_remove_unresponsive_raid_devices.
6690 *
6691 * Return nothing.
6692 */
6693static void
6694_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6695 u16 handle)
6696{
6697 struct MPT2SAS_TARGET *sas_target_priv_data;
6698 struct scsi_target *starget;
6699 struct _raid_device *raid_device;
6700 unsigned long flags;
6701
6702 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6703 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6704 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306705 starget = raid_device->starget;
6706 if (starget && starget->hostdata) {
6707 sas_target_priv_data = starget->hostdata;
6708 sas_target_priv_data->deleted = 0;
6709 } else
6710 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006711 raid_device->responding = 1;
6712 starget_printk(KERN_INFO, raid_device->starget,
6713 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6714 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306715 /*
6716 * WARPDRIVE: The handles of the PDs might have changed
6717 * across the host reset so re-initialize the
6718 * required data for Direct IO
6719 */
6720 _scsih_init_warpdrive_properties(ioc, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006721 if (raid_device->handle == handle)
6722 goto out;
6723 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6724 raid_device->handle);
6725 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306726 if (sas_target_priv_data)
6727 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006728 goto out;
6729 }
6730 }
6731 out:
6732 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6733}
6734
6735/**
6736 * _scsih_search_responding_raid_devices -
6737 * @ioc: per adapter object
6738 *
6739 * After host reset, find out whether devices are still responding.
6740 * If not remove.
6741 *
6742 * Return nothing.
6743 */
6744static void
6745_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6746{
6747 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306748 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306749 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006750 Mpi2ConfigReply_t mpi_reply;
6751 u16 ioc_status;
6752 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306753 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006754
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306755 if (!ioc->ir_firmware)
6756 return;
6757
6758 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6759 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006760
6761 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306762 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006763
6764 handle = 0xFFFF;
6765 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6766 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6767 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6768 MPI2_IOCSTATUS_MASK;
6769 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6770 break;
6771 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306772
6773 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6774 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6775 sizeof(Mpi2RaidVolPage0_t)))
6776 continue;
6777
6778 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6779 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6780 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6781 _scsih_mark_responding_raid_device(ioc,
6782 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006783 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306784
6785 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306786 if (!ioc->is_warpdrive) {
6787 phys_disk_num = 0xFF;
6788 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6789 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6790 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6791 phys_disk_num))) {
6792 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6793 MPI2_IOCSTATUS_MASK;
6794 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6795 break;
6796 phys_disk_num = pd_pg0.PhysDiskNum;
6797 handle = le16_to_cpu(pd_pg0.DevHandle);
6798 set_bit(handle, ioc->pd_handles);
6799 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306800 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306801out:
6802 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6803 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006804}
6805
6806/**
6807 * _scsih_mark_responding_expander - mark a expander as responding
6808 * @ioc: per adapter object
6809 * @sas_address: sas address
6810 * @handle:
6811 *
6812 * After host reset, find out whether devices are still responding.
6813 * Used in _scsi_remove_unresponsive_expanders.
6814 *
6815 * Return nothing.
6816 */
6817static void
6818_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6819 u16 handle)
6820{
6821 struct _sas_node *sas_expander;
6822 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306823 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006824
6825 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6826 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306827 if (sas_expander->sas_address != sas_address)
6828 continue;
6829 sas_expander->responding = 1;
6830 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006831 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306832 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6833 " from(0x%04x) to (0x%04x)!!!\n",
6834 (unsigned long long)sas_expander->sas_address,
6835 sas_expander->handle, handle);
6836 sas_expander->handle = handle;
6837 for (i = 0 ; i < sas_expander->num_phys ; i++)
6838 sas_expander->phy[i].handle = handle;
6839 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006840 }
6841 out:
6842 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6843}
6844
6845/**
6846 * _scsih_search_responding_expanders -
6847 * @ioc: per adapter object
6848 *
6849 * After host reset, find out whether devices are still responding.
6850 * If not remove.
6851 *
6852 * Return nothing.
6853 */
6854static void
6855_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6856{
6857 Mpi2ExpanderPage0_t expander_pg0;
6858 Mpi2ConfigReply_t mpi_reply;
6859 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306860 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006861 u16 handle;
6862
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306863 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006864
6865 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306866 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006867
6868 handle = 0xFFFF;
6869 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6870 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6871
6872 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6873 MPI2_IOCSTATUS_MASK;
6874 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6875 break;
6876
6877 handle = le16_to_cpu(expander_pg0.DevHandle);
6878 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6879 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6880 "sas_addr(0x%016llx)\n", handle,
6881 (unsigned long long)sas_address);
6882 _scsih_mark_responding_expander(ioc, sas_address, handle);
6883 }
6884
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306885 out:
6886 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006887}
6888
6889/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306890 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006891 * @ioc: per adapter object
6892 *
6893 * Return nothing.
6894 */
6895static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306896_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006897{
6898 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306899 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006900 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006901
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306902 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
6903 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006904
6905 list_for_each_entry_safe(sas_device, sas_device_next,
6906 &ioc->sas_device_list, list) {
6907 if (sas_device->responding) {
6908 sas_device->responding = 0;
6909 continue;
6910 }
6911 if (sas_device->starget)
6912 starget_printk(KERN_INFO, sas_device->starget,
6913 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6914 "enclosure logical id(0x%016llx), slot(%d)\n",
6915 sas_device->handle,
6916 (unsigned long long)sas_device->sas_address,
6917 (unsigned long long)
6918 sas_device->enclosure_logical_id,
6919 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306920 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006921 }
6922
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306923 if (!ioc->ir_firmware)
6924 goto retry_expander_search;
6925
Eric Moore635374e2009-03-09 01:21:12 -06006926 list_for_each_entry_safe(raid_device, raid_device_next,
6927 &ioc->raid_device_list, list) {
6928 if (raid_device->responding) {
6929 raid_device->responding = 0;
6930 continue;
6931 }
6932 if (raid_device->starget) {
6933 starget_printk(KERN_INFO, raid_device->starget,
6934 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6935 raid_device->handle,
6936 (unsigned long long)raid_device->wwid);
6937 scsi_remove_target(&raid_device->starget->dev);
6938 }
6939 _scsih_raid_device_remove(ioc, raid_device);
6940 }
6941
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306942 retry_expander_search:
6943 sas_expander = NULL;
6944 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006945 if (sas_expander->responding) {
6946 sas_expander->responding = 0;
6947 continue;
6948 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306949 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306950 goto retry_expander_search;
6951 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306952 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
6953 ioc->name);
6954 /* unblock devices */
6955 _scsih_ublock_io_all_device(ioc);
6956}
6957
6958static void
6959_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
6960 struct _sas_node *sas_expander, u16 handle)
6961{
6962 Mpi2ExpanderPage1_t expander_pg1;
6963 Mpi2ConfigReply_t mpi_reply;
6964 int i;
6965
6966 for (i = 0 ; i < sas_expander->num_phys ; i++) {
6967 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
6968 &expander_pg1, i, handle))) {
6969 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6970 ioc->name, __FILE__, __LINE__, __func__);
6971 return;
6972 }
6973
6974 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
6975 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
6976 expander_pg1.NegotiatedLinkRate >> 4);
6977 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306978}
6979
6980/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306981 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306982 * @ioc: per adapter object
6983 *
6984 * Return nothing.
6985 */
6986static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306987_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306988{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306989 Mpi2ExpanderPage0_t expander_pg0;
6990 Mpi2SasDevicePage0_t sas_device_pg0;
6991 Mpi2RaidVolPage1_t volume_pg1;
6992 Mpi2RaidVolPage0_t volume_pg0;
6993 Mpi2RaidPhysDiskPage0_t pd_pg0;
6994 Mpi2EventIrConfigElement_t element;
6995 Mpi2ConfigReply_t mpi_reply;
6996 u8 phys_disk_num;
6997 u16 ioc_status;
6998 u16 handle, parent_handle;
6999 u64 sas_address;
7000 struct _sas_device *sas_device;
7001 struct _sas_node *expander_device;
7002 static struct _raid_device *raid_device;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307003
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307004 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307005
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307006 _scsih_sas_host_refresh(ioc);
7007
7008 /* expanders */
7009 handle = 0xFFFF;
7010 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7011 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7012 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7013 MPI2_IOCSTATUS_MASK;
7014 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7015 break;
7016 handle = le16_to_cpu(expander_pg0.DevHandle);
7017 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7018 ioc, le64_to_cpu(expander_pg0.SASAddress));
7019 if (expander_device)
7020 _scsih_refresh_expander_links(ioc, expander_device,
7021 handle);
7022 else
7023 _scsih_expander_add(ioc, handle);
7024 }
7025
7026 if (!ioc->ir_firmware)
7027 goto skip_to_sas;
7028
7029 /* phys disk */
7030 phys_disk_num = 0xFF;
7031 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7032 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7033 phys_disk_num))) {
7034 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7035 MPI2_IOCSTATUS_MASK;
7036 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7037 break;
7038 phys_disk_num = pd_pg0.PhysDiskNum;
7039 handle = le16_to_cpu(pd_pg0.DevHandle);
7040 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7041 if (sas_device)
7042 continue;
7043 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7044 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7045 handle) != 0)
7046 continue;
7047 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7048 if (!_scsih_get_sas_address(ioc, parent_handle,
7049 &sas_address)) {
7050 mpt2sas_transport_update_links(ioc, sas_address,
7051 handle, sas_device_pg0.PhyNum,
7052 MPI2_SAS_NEG_LINK_RATE_1_5);
7053 set_bit(handle, ioc->pd_handles);
7054 _scsih_add_device(ioc, handle, 0, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307055 }
7056 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307057
7058 /* volumes */
7059 handle = 0xFFFF;
7060 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7061 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7062 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7063 MPI2_IOCSTATUS_MASK;
7064 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7065 break;
7066 handle = le16_to_cpu(volume_pg1.DevHandle);
7067 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7068 le64_to_cpu(volume_pg1.WWID));
7069 if (raid_device)
7070 continue;
7071 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7072 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7073 sizeof(Mpi2RaidVolPage0_t)))
7074 continue;
7075 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7076 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7077 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7078 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7079 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7080 element.VolDevHandle = volume_pg1.DevHandle;
7081 _scsih_sas_volume_add(ioc, &element);
7082 }
7083 }
7084
7085 skip_to_sas:
7086
7087 /* sas devices */
7088 handle = 0xFFFF;
7089 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7090 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7091 handle))) {
7092 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7093 MPI2_IOCSTATUS_MASK;
7094 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7095 break;
7096 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7097 if (!(_scsih_is_end_device(
7098 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7099 continue;
7100 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7101 le64_to_cpu(sas_device_pg0.SASAddress));
7102 if (sas_device)
7103 continue;
7104 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7105 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7106 mpt2sas_transport_update_links(ioc, sas_address, handle,
7107 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7108 _scsih_add_device(ioc, handle, 0, 0);
7109 }
7110 }
7111
7112 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307113}
7114
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307115
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307116/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307117 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7118 * @ioc: per adapter object
7119 * @reset_phase: phase
7120 *
7121 * The handler for doing any required cleanup or initialization.
7122 *
7123 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7124 * MPT2_IOC_DONE_RESET
7125 *
7126 * Return nothing.
7127 */
7128void
7129mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7130{
7131 switch (reset_phase) {
7132 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307133 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307134 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307135 break;
7136 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307137 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307138 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307139 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7140 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7141 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7142 complete(&ioc->scsih_cmds.done);
7143 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307144 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7145 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7146 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7147 complete(&ioc->tm_cmds.done);
7148 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307149 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307150 _scsih_flush_running_cmds(ioc);
7151 break;
7152 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307153 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307154 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307155 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307156 _scsih_prep_device_scan(ioc);
7157 _scsih_search_responding_sas_devices(ioc);
7158 _scsih_search_responding_raid_devices(ioc);
7159 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307160 if (!ioc->is_driver_loading) {
7161 _scsih_prep_device_scan(ioc);
7162 _scsih_search_responding_sas_devices(ioc);
7163 _scsih_search_responding_raid_devices(ioc);
7164 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307165 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307166 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307167 break;
Eric Moore635374e2009-03-09 01:21:12 -06007168 }
7169}
7170
7171/**
7172 * _firmware_event_work - delayed task for processing firmware events
7173 * @ioc: per adapter object
7174 * @work: equal to the fw_event_work object
7175 * Context: user.
7176 *
7177 * Return nothing.
7178 */
7179static void
7180_firmware_event_work(struct work_struct *work)
7181{
7182 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307183 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007184 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7185
Eric Moore635374e2009-03-09 01:21:12 -06007186 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007187 if (ioc->remove_host || fw_event->cancel_pending_work ||
7188 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007189 _scsih_fw_event_free(ioc, fw_event);
7190 return;
7191 }
Eric Moore635374e2009-03-09 01:21:12 -06007192
Eric Moore635374e2009-03-09 01:21:12 -06007193 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307194 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
7195 while (scsi_host_in_recovery(ioc->shost))
7196 ssleep(1);
7197 _scsih_remove_unresponding_sas_devices(ioc);
7198 _scsih_scan_for_devices_after_reset(ioc);
7199 break;
7200 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307201 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307202
7203
7204
7205 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7206 "from worker thread\n", ioc->name));
7207 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307208 case MPT2SAS_TURN_ON_FAULT_LED:
7209 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7210 break;
Eric Moore635374e2009-03-09 01:21:12 -06007211 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307212 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007213 break;
7214 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307215 _scsih_sas_device_status_change_event(ioc,
7216 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007217 break;
7218 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307219 _scsih_sas_discovery_event(ioc,
7220 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007221 break;
7222 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307223 _scsih_sas_broadcast_primative_event(ioc,
7224 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007225 break;
7226 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7227 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307228 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007229 break;
7230 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307231 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007232 break;
7233 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307234 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007235 break;
7236 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307237 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007238 break;
7239 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307240 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007241 break;
Eric Moore635374e2009-03-09 01:21:12 -06007242 }
7243 _scsih_fw_event_free(ioc, fw_event);
7244}
7245
7246/**
7247 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7248 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307249 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007250 * @reply: reply message frame(lower 32bit addr)
7251 * Context: interrupt.
7252 *
7253 * This function merely adds a new work task into ioc->firmware_event_thread.
7254 * The tasks are worked from _firmware_event_work in user context.
7255 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307256 * Return 1 meaning mf should be freed from _base_interrupt
7257 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06007258 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307259u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307260mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7261 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007262{
7263 struct fw_event_work *fw_event;
7264 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007265 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307266 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007267
7268 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007269 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307270 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007271
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307272 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06007273 event = le16_to_cpu(mpi_reply->Event);
7274
7275 switch (event) {
7276 /* handle these */
7277 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7278 {
7279 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7280 (Mpi2EventDataSasBroadcastPrimitive_t *)
7281 mpi_reply->EventData;
7282
7283 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307284 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307285 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307286
7287 if (ioc->broadcast_aen_busy) {
7288 ioc->broadcast_aen_pending++;
7289 return 1;
7290 } else
7291 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007292 break;
7293 }
7294
7295 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7296 _scsih_check_topo_delete_events(ioc,
7297 (Mpi2EventDataSasTopologyChangeList_t *)
7298 mpi_reply->EventData);
7299 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307300 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7301 _scsih_check_ir_config_unhide_events(ioc,
7302 (Mpi2EventDataIrConfigChangeList_t *)
7303 mpi_reply->EventData);
7304 break;
7305 case MPI2_EVENT_IR_VOLUME:
7306 _scsih_check_volume_delete_events(ioc,
7307 (Mpi2EventDataIrVolume_t *)
7308 mpi_reply->EventData);
7309 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307310 case MPI2_EVENT_LOG_ENTRY_ADDED:
7311 {
7312 Mpi2EventDataLogEntryAdded_t *log_entry;
7313 u32 *log_code;
7314
7315 if (!ioc->is_warpdrive)
7316 break;
7317
7318 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7319 mpi_reply->EventData;
7320 log_code = (u32 *)log_entry->LogData;
7321
7322 if (le16_to_cpu(log_entry->LogEntryQualifier)
7323 != MPT2_WARPDRIVE_LOGENTRY)
7324 break;
7325
7326 switch (le32_to_cpu(*log_code)) {
7327 case MPT2_WARPDRIVE_LC_SSDT:
7328 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7329 "IO Throttling has occurred in the WarpDrive "
7330 "subsystem. Check WarpDrive documentation for "
7331 "additional details.\n", ioc->name);
7332 break;
7333 case MPT2_WARPDRIVE_LC_SSDLW:
7334 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7335 "Program/Erase Cycles for the WarpDrive subsystem "
7336 "in degraded range. Check WarpDrive documentation "
7337 "for additional details.\n", ioc->name);
7338 break;
7339 case MPT2_WARPDRIVE_LC_SSDLF:
7340 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7341 "There are no Program/Erase Cycles for the "
7342 "WarpDrive subsystem. The storage device will be "
7343 "in read-only mode. Check WarpDrive documentation "
7344 "for additional details.\n", ioc->name);
7345 break;
7346 case MPT2_WARPDRIVE_LC_BRMF:
7347 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7348 "The Backup Rail Monitor has failed on the "
7349 "WarpDrive subsystem. Check WarpDrive "
7350 "documentation for additional details.\n",
7351 ioc->name);
7352 break;
7353 }
7354
7355 break;
7356 }
Eric Moore635374e2009-03-09 01:21:12 -06007357 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7358 case MPI2_EVENT_IR_OPERATION_STATUS:
7359 case MPI2_EVENT_SAS_DISCOVERY:
7360 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007361 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007362 break;
7363
7364 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307365 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007366 }
7367
7368 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7369 if (!fw_event) {
7370 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7371 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307372 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007373 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307374 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7375 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007376 if (!fw_event->event_data) {
7377 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7378 ioc->name, __FILE__, __LINE__, __func__);
7379 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307380 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007381 }
7382
7383 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307384 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007385 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307386 fw_event->VF_ID = mpi_reply->VF_ID;
7387 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007388 fw_event->event = event;
7389 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307390 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007391}
7392
7393/* shost template */
7394static struct scsi_host_template scsih_driver_template = {
7395 .module = THIS_MODULE,
7396 .name = "Fusion MPT SAS Host",
7397 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007398 .queuecommand = _scsih_qcmd,
7399 .target_alloc = _scsih_target_alloc,
7400 .slave_alloc = _scsih_slave_alloc,
7401 .slave_configure = _scsih_slave_configure,
7402 .target_destroy = _scsih_target_destroy,
7403 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307404 .scan_finished = _scsih_scan_finished,
7405 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007406 .change_queue_depth = _scsih_change_queue_depth,
7407 .change_queue_type = _scsih_change_queue_type,
7408 .eh_abort_handler = _scsih_abort,
7409 .eh_device_reset_handler = _scsih_dev_reset,
7410 .eh_target_reset_handler = _scsih_target_reset,
7411 .eh_host_reset_handler = _scsih_host_reset,
7412 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007413 .can_queue = 1,
7414 .this_id = -1,
7415 .sg_tablesize = MPT2SAS_SG_DEPTH,
7416 .max_sectors = 8192,
7417 .cmd_per_lun = 7,
7418 .use_clustering = ENABLE_CLUSTERING,
7419 .shost_attrs = mpt2sas_host_attrs,
7420 .sdev_attrs = mpt2sas_dev_attrs,
7421};
7422
7423/**
7424 * _scsih_expander_node_remove - removing expander device from list.
7425 * @ioc: per adapter object
7426 * @sas_expander: the sas_device object
7427 * Context: Calling function should acquire ioc->sas_node_lock.
7428 *
7429 * Removing object and freeing associated memory from the
7430 * ioc->sas_expander_list.
7431 *
7432 * Return nothing.
7433 */
7434static void
7435_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7436 struct _sas_node *sas_expander)
7437{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307438 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007439
7440 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307441 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007442 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307443 if (ioc->shost_recovery)
7444 return;
Eric Moore635374e2009-03-09 01:21:12 -06007445 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307446 SAS_END_DEVICE)
7447 mpt2sas_device_remove(ioc,
7448 mpt2sas_port->remote_identify.sas_address);
7449 else if (mpt2sas_port->remote_identify.device_type ==
7450 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007451 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307452 SAS_FANOUT_EXPANDER_DEVICE)
7453 mpt2sas_expander_remove(ioc,
7454 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007455 }
7456
7457 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307458 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007459
7460 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7461 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7462 sas_expander->handle, (unsigned long long)
7463 sas_expander->sas_address);
7464
Eric Moore635374e2009-03-09 01:21:12 -06007465 kfree(sas_expander->phy);
7466 kfree(sas_expander);
7467}
7468
7469/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307470 * _scsih_ir_shutdown - IR shutdown notification
7471 * @ioc: per adapter object
7472 *
7473 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7474 * the host system is shutting down.
7475 *
7476 * Return nothing.
7477 */
7478static void
7479_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7480{
7481 Mpi2RaidActionRequest_t *mpi_request;
7482 Mpi2RaidActionReply_t *mpi_reply;
7483 u16 smid;
7484
7485 /* is IR firmware build loaded ? */
7486 if (!ioc->ir_firmware)
7487 return;
7488
7489 /* are there any volumes ? */
7490 if (list_empty(&ioc->raid_device_list))
7491 return;
7492
7493 mutex_lock(&ioc->scsih_cmds.mutex);
7494
7495 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7496 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7497 ioc->name, __func__);
7498 goto out;
7499 }
7500 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7501
7502 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7503 if (!smid) {
7504 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7505 ioc->name, __func__);
7506 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7507 goto out;
7508 }
7509
7510 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7511 ioc->scsih_cmds.smid = smid;
7512 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7513
7514 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7515 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7516
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307517 if (!ioc->hide_ir_msg)
7518 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307519 init_completion(&ioc->scsih_cmds.done);
7520 mpt2sas_base_put_smid_default(ioc, smid);
7521 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7522
7523 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7524 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7525 ioc->name, __func__);
7526 goto out;
7527 }
7528
7529 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7530 mpi_reply = ioc->scsih_cmds.reply;
7531
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307532 if (!ioc->hide_ir_msg)
7533 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7534 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7535 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7536 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307537 }
7538
7539 out:
7540 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7541 mutex_unlock(&ioc->scsih_cmds.mutex);
7542}
7543
7544/**
7545 * _scsih_shutdown - routine call during system shutdown
7546 * @pdev: PCI device struct
7547 *
7548 * Return nothing.
7549 */
7550static void
7551_scsih_shutdown(struct pci_dev *pdev)
7552{
7553 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7554 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307555 struct workqueue_struct *wq;
7556 unsigned long flags;
7557
7558 ioc->remove_host = 1;
7559 _scsih_fw_event_cleanup_queue(ioc);
7560
7561 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7562 wq = ioc->firmware_event_thread;
7563 ioc->firmware_event_thread = NULL;
7564 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7565 if (wq)
7566 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307567
7568 _scsih_ir_shutdown(ioc);
7569 mpt2sas_base_detach(ioc);
7570}
7571
7572/**
Eric Moored5d135b2009-05-18 13:02:08 -06007573 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007574 * @pdev: PCI device struct
7575 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307576 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007577 * Return nothing.
7578 */
7579static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007580_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007581{
7582 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7583 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307584 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307585 struct _raid_device *raid_device, *next;
7586 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007587 struct workqueue_struct *wq;
7588 unsigned long flags;
7589
7590 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307591 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007592
7593 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7594 wq = ioc->firmware_event_thread;
7595 ioc->firmware_event_thread = NULL;
7596 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7597 if (wq)
7598 destroy_workqueue(wq);
7599
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307600 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307601 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307602 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7603 list) {
7604 if (raid_device->starget) {
7605 sas_target_priv_data =
7606 raid_device->starget->hostdata;
7607 sas_target_priv_data->deleted = 1;
7608 scsi_remove_target(&raid_device->starget->dev);
7609 }
7610 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7611 "(0x%016llx)\n", ioc->name, raid_device->handle,
7612 (unsigned long long) raid_device->wwid);
7613 _scsih_raid_device_remove(ioc, raid_device);
7614 }
7615
Eric Moore635374e2009-03-09 01:21:12 -06007616 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307617 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007618 &ioc->sas_hba.sas_port_list, port_list) {
7619 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307620 SAS_END_DEVICE)
7621 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007622 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307623 else if (mpt2sas_port->remote_identify.device_type ==
7624 SAS_EDGE_EXPANDER_DEVICE ||
7625 mpt2sas_port->remote_identify.device_type ==
7626 SAS_FANOUT_EXPANDER_DEVICE)
7627 mpt2sas_expander_remove(ioc,
7628 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007629 }
7630
7631 /* free phys attached to the sas_host */
7632 if (ioc->sas_hba.num_phys) {
7633 kfree(ioc->sas_hba.phy);
7634 ioc->sas_hba.phy = NULL;
7635 ioc->sas_hba.num_phys = 0;
7636 }
7637
7638 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307639 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007640 list_del(&ioc->list);
7641 scsi_remove_host(shost);
7642 scsi_host_put(shost);
7643}
7644
7645/**
7646 * _scsih_probe_boot_devices - reports 1st device
7647 * @ioc: per adapter object
7648 *
7649 * If specified in bios page 2, this routine reports the 1st
7650 * device scsi-ml or sas transport for persistent boot device
7651 * purposes. Please refer to function _scsih_determine_boot_device()
7652 */
7653static void
7654_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7655{
7656 u8 is_raid;
7657 void *device;
7658 struct _sas_device *sas_device;
7659 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307660 u16 handle;
7661 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007662 u64 sas_address;
7663 unsigned long flags;
7664 int rc;
7665
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307666 /* no Bios, return immediately */
7667 if (!ioc->bios_pg3.BiosVersion)
7668 return;
7669
Eric Moore635374e2009-03-09 01:21:12 -06007670 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307671 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007672 if (ioc->req_boot_device.device) {
7673 device = ioc->req_boot_device.device;
7674 is_raid = ioc->req_boot_device.is_raid;
7675 } else if (ioc->req_alt_boot_device.device) {
7676 device = ioc->req_alt_boot_device.device;
7677 is_raid = ioc->req_alt_boot_device.is_raid;
7678 } else if (ioc->current_boot_device.device) {
7679 device = ioc->current_boot_device.device;
7680 is_raid = ioc->current_boot_device.is_raid;
7681 }
7682
7683 if (!device)
7684 return;
7685
7686 if (is_raid) {
7687 raid_device = device;
7688 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7689 raid_device->id, 0);
7690 if (rc)
7691 _scsih_raid_device_remove(ioc, raid_device);
7692 } else {
7693 sas_device = device;
7694 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307695 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007696 sas_address = sas_device->sas_address;
7697 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7698 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7699 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307700
7701 if (ioc->hide_drives)
7702 return;
Eric Moore635374e2009-03-09 01:21:12 -06007703 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307704 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007705 _scsih_sas_device_remove(ioc, sas_device);
7706 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307707 if (!ioc->is_driver_loading)
7708 mpt2sas_transport_port_remove(ioc, sas_address,
7709 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007710 _scsih_sas_device_remove(ioc, sas_device);
7711 }
7712 }
7713}
7714
7715/**
7716 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7717 * @ioc: per adapter object
7718 *
7719 * Called during initial loading of the driver.
7720 */
7721static void
7722_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7723{
7724 struct _raid_device *raid_device, *raid_next;
7725 int rc;
7726
7727 list_for_each_entry_safe(raid_device, raid_next,
7728 &ioc->raid_device_list, list) {
7729 if (raid_device->starget)
7730 continue;
7731 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7732 raid_device->id, 0);
7733 if (rc)
7734 _scsih_raid_device_remove(ioc, raid_device);
7735 }
7736}
7737
7738/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307739 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007740 * @ioc: per adapter object
7741 *
7742 * Called during initial loading of the driver.
7743 */
7744static void
7745_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7746{
7747 struct _sas_device *sas_device, *next;
7748 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007749
7750 /* SAS Device List */
7751 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7752 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007753
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307754 if (ioc->hide_drives)
7755 continue;
7756
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307757 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7758 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307759 list_del(&sas_device->list);
7760 kfree(sas_device);
7761 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007762 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307763 if (!ioc->is_driver_loading)
7764 mpt2sas_transport_port_remove(ioc,
7765 sas_device->sas_address,
7766 sas_device->sas_address_parent);
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307767 list_del(&sas_device->list);
7768 kfree(sas_device);
7769 continue;
7770
Eric Moore635374e2009-03-09 01:21:12 -06007771 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307772 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7773 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7774 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007775 }
7776}
7777
7778/**
7779 * _scsih_probe_devices - probing for devices
7780 * @ioc: per adapter object
7781 *
7782 * Called during initial loading of the driver.
7783 */
7784static void
7785_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7786{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307787 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06007788
7789 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7790 return; /* return when IOC doesn't support initiator mode */
7791
7792 _scsih_probe_boot_devices(ioc);
7793
7794 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307795 volume_mapping_flags =
7796 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7797 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7798 if (volume_mapping_flags ==
7799 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06007800 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307801 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007802 } else {
Eric Moore635374e2009-03-09 01:21:12 -06007803 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307804 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007805 }
7806 } else
7807 _scsih_probe_sas(ioc);
7808}
7809
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307810
7811/**
7812 * _scsih_scan_start - scsi lld callback for .scan_start
7813 * @shost: SCSI host pointer
7814 *
7815 * The shost has the ability to discover targets on its own instead
7816 * of scanning the entire bus. In our implemention, we will kick off
7817 * firmware discovery.
7818 */
7819static void
7820_scsih_scan_start(struct Scsi_Host *shost)
7821{
7822 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7823 int rc;
7824
7825 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7826 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
7827
7828 ioc->start_scan = 1;
7829 rc = mpt2sas_port_enable(ioc);
7830
7831 if (rc != 0)
7832 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
7833}
7834
7835/**
7836 * _scsih_scan_finished - scsi lld callback for .scan_finished
7837 * @shost: SCSI host pointer
7838 * @time: elapsed time of the scan in jiffies
7839 *
7840 * This function will be called periodically until it returns 1 with the
7841 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7842 * we wait for firmware discovery to complete, then return 1.
7843 */
7844static int
7845_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7846{
7847 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7848
7849 if (time >= (300 * HZ)) {
7850 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7851 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
7852 "(timeout=300s)\n", ioc->name);
7853 ioc->is_driver_loading = 0;
7854 return 1;
7855 }
7856
7857 if (ioc->start_scan)
7858 return 0;
7859
7860 if (ioc->start_scan_failed) {
7861 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
7862 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
7863 ioc->is_driver_loading = 0;
7864 ioc->wait_for_discovery_to_complete = 0;
7865 ioc->remove_host = 1;
7866 return 1;
7867 }
7868
7869 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
7870 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7871
7872 if (ioc->wait_for_discovery_to_complete) {
7873 ioc->wait_for_discovery_to_complete = 0;
7874 _scsih_probe_devices(ioc);
7875 }
7876 mpt2sas_base_start_watchdog(ioc);
7877 ioc->is_driver_loading = 0;
7878 return 1;
7879}
7880
7881
Eric Moore635374e2009-03-09 01:21:12 -06007882/**
Eric Moored5d135b2009-05-18 13:02:08 -06007883 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06007884 * @pdev: PCI device struct
7885 * @id: pci device id
7886 *
7887 * Returns 0 success, anything else error.
7888 */
7889static int
Eric Moored5d135b2009-05-18 13:02:08 -06007890_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06007891{
7892 struct MPT2SAS_ADAPTER *ioc;
7893 struct Scsi_Host *shost;
7894
7895 shost = scsi_host_alloc(&scsih_driver_template,
7896 sizeof(struct MPT2SAS_ADAPTER));
7897 if (!shost)
7898 return -ENODEV;
7899
7900 /* init local params */
7901 ioc = shost_priv(shost);
7902 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7903 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06007904 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06007905 ioc->shost = shost;
7906 ioc->id = mpt_ids++;
7907 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7908 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307909 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7910 ioc->is_warpdrive = 1;
7911 ioc->hide_ir_msg = 1;
7912 } else
7913 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06007914 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7915 ioc->tm_cb_idx = tm_cb_idx;
7916 ioc->ctl_cb_idx = ctl_cb_idx;
7917 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307918 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007919 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05307920 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007921 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307922 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307923 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307924 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007925 ioc->logging_level = logging_level;
7926 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05307927 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06007928 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7929 spin_lock_init(&ioc->scsi_lookup_lock);
7930 spin_lock_init(&ioc->sas_device_lock);
7931 spin_lock_init(&ioc->sas_node_lock);
7932 spin_lock_init(&ioc->fw_event_lock);
7933 spin_lock_init(&ioc->raid_device_lock);
7934
7935 INIT_LIST_HEAD(&ioc->sas_device_list);
7936 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7937 INIT_LIST_HEAD(&ioc->sas_expander_list);
7938 INIT_LIST_HEAD(&ioc->fw_event_list);
7939 INIT_LIST_HEAD(&ioc->raid_device_list);
7940 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307941 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307942 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06007943
7944 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06007945 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06007946 shost->max_lun = max_lun;
7947 shost->transportt = mpt2sas_transport_template;
7948 shost->unique_id = ioc->id;
7949
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05307950 if (max_sectors != 0xFFFF) {
7951 if (max_sectors < 64) {
7952 shost->max_sectors = 64;
7953 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7954 "for max_sectors, range is 64 to 8192. Assigning "
7955 "value of 64.\n", ioc->name, max_sectors);
7956 } else if (max_sectors > 8192) {
7957 shost->max_sectors = 8192;
7958 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7959 "for max_sectors, range is 64 to 8192. Assigning "
7960 "default value of 8192.\n", ioc->name,
7961 max_sectors);
7962 } else {
7963 shost->max_sectors = max_sectors & 0xFFFE;
7964 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
7965 "set to %d\n", ioc->name, shost->max_sectors);
7966 }
7967 }
7968
Eric Moore635374e2009-03-09 01:21:12 -06007969 if ((scsi_add_host(shost, &pdev->dev))) {
7970 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7971 ioc->name, __FILE__, __LINE__, __func__);
7972 list_del(&ioc->list);
7973 goto out_add_shost_fail;
7974 }
7975
Eric Moore3c621b32009-05-18 12:59:41 -06007976 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06007977 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307978 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06007979
Eric Moore635374e2009-03-09 01:21:12 -06007980 /* event thread */
7981 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7982 "fw_event%d", ioc->id);
7983 ioc->firmware_event_thread = create_singlethread_workqueue(
7984 ioc->firmware_event_name);
7985 if (!ioc->firmware_event_thread) {
7986 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7987 ioc->name, __FILE__, __LINE__, __func__);
7988 goto out_thread_fail;
7989 }
7990
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307991 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007992 if ((mpt2sas_base_attach(ioc))) {
7993 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7994 ioc->name, __FILE__, __LINE__, __func__);
7995 goto out_attach_fail;
7996 }
7997
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307998 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307999 if (ioc->is_warpdrive) {
8000 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8001 ioc->hide_drives = 0;
8002 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8003 ioc->hide_drives = 1;
8004 else {
8005 if (_scsih_get_num_volumes(ioc))
8006 ioc->hide_drives = 1;
8007 else
8008 ioc->hide_drives = 0;
8009 }
8010 } else
8011 ioc->hide_drives = 0;
8012
Eric Moore635374e2009-03-09 01:21:12 -06008013 _scsih_probe_devices(ioc);
8014 return 0;
8015
8016 out_attach_fail:
8017 destroy_workqueue(ioc->firmware_event_thread);
8018 out_thread_fail:
8019 list_del(&ioc->list);
8020 scsi_remove_host(shost);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308021 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008022 out_add_shost_fail:
8023 return -ENODEV;
8024}
8025
8026#ifdef CONFIG_PM
8027/**
Eric Moored5d135b2009-05-18 13:02:08 -06008028 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008029 * @pdev: PCI device struct
8030 * @state: PM state change to (usually PCI_D3)
8031 *
8032 * Returns 0 success, anything else error.
8033 */
8034static int
Eric Moored5d135b2009-05-18 13:02:08 -06008035_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008036{
8037 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8038 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308039 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008040
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308041 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008042 scsi_block_requests(shost);
8043 device_state = pci_choose_state(pdev, state);
8044 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8045 "operating state [D%d]\n", ioc->name, pdev,
8046 pci_name(pdev), device_state);
8047
8048 mpt2sas_base_free_resources(ioc);
8049 pci_save_state(pdev);
8050 pci_disable_device(pdev);
8051 pci_set_power_state(pdev, device_state);
8052 return 0;
8053}
8054
8055/**
Eric Moored5d135b2009-05-18 13:02:08 -06008056 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008057 * @pdev: PCI device struct
8058 *
8059 * Returns 0 success, anything else error.
8060 */
8061static int
Eric Moored5d135b2009-05-18 13:02:08 -06008062_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008063{
8064 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8065 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308066 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008067 int r;
8068
8069 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8070 "operating state [D%d]\n", ioc->name, pdev,
8071 pci_name(pdev), device_state);
8072
8073 pci_set_power_state(pdev, PCI_D0);
8074 pci_enable_wake(pdev, PCI_D0, 0);
8075 pci_restore_state(pdev);
8076 ioc->pdev = pdev;
8077 r = mpt2sas_base_map_resources(ioc);
8078 if (r)
8079 return r;
8080
8081 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8082 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308083 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008084 return 0;
8085}
8086#endif /* CONFIG_PM */
8087
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308088/**
8089 * _scsih_pci_error_detected - Called when a PCI error is detected.
8090 * @pdev: PCI device struct
8091 * @state: PCI channel state
8092 *
8093 * Description: Called when a PCI error is detected.
8094 *
8095 * Return value:
8096 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8097 */
8098static pci_ers_result_t
8099_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8100{
8101 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8102 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8103
8104 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8105 ioc->name, state);
8106
8107 switch (state) {
8108 case pci_channel_io_normal:
8109 return PCI_ERS_RESULT_CAN_RECOVER;
8110 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008111 /* Fatal error, prepare for slot reset */
8112 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308113 scsi_block_requests(ioc->shost);
8114 mpt2sas_base_stop_watchdog(ioc);
8115 mpt2sas_base_free_resources(ioc);
8116 return PCI_ERS_RESULT_NEED_RESET;
8117 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008118 /* Permanent error, prepare for device removal */
8119 ioc->pci_error_recovery = 1;
8120 mpt2sas_base_stop_watchdog(ioc);
8121 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308122 return PCI_ERS_RESULT_DISCONNECT;
8123 }
8124 return PCI_ERS_RESULT_NEED_RESET;
8125}
8126
8127/**
8128 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8129 * @pdev: PCI device struct
8130 *
8131 * Description: This routine is called by the pci error recovery
8132 * code after the PCI slot has been reset, just before we
8133 * should resume normal operations.
8134 */
8135static pci_ers_result_t
8136_scsih_pci_slot_reset(struct pci_dev *pdev)
8137{
8138 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8139 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8140 int rc;
8141
8142 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8143 ioc->name);
8144
Eric Moore3cb54692010-07-08 14:44:34 -06008145 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308146 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008147 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308148 rc = mpt2sas_base_map_resources(ioc);
8149 if (rc)
8150 return PCI_ERS_RESULT_DISCONNECT;
8151
8152
8153 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8154 FORCE_BIG_HAMMER);
8155
8156 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8157 (rc == 0) ? "success" : "failed");
8158
8159 if (!rc)
8160 return PCI_ERS_RESULT_RECOVERED;
8161 else
8162 return PCI_ERS_RESULT_DISCONNECT;
8163}
8164
8165/**
8166 * _scsih_pci_resume() - resume normal ops after PCI reset
8167 * @pdev: pointer to PCI device
8168 *
8169 * Called when the error recovery driver tells us that its
8170 * OK to resume normal operation. Use completion to allow
8171 * halted scsi ops to resume.
8172 */
8173static void
8174_scsih_pci_resume(struct pci_dev *pdev)
8175{
8176 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8177 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8178
8179 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8180
8181 pci_cleanup_aer_uncorrect_error_status(pdev);
8182 mpt2sas_base_start_watchdog(ioc);
8183 scsi_unblock_requests(ioc->shost);
8184}
8185
8186/**
8187 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8188 * @pdev: pointer to PCI device
8189 */
8190static pci_ers_result_t
8191_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8192{
8193 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8194 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8195
8196 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8197 ioc->name);
8198
8199 /* TODO - dump whatever for debugging purposes */
8200
8201 /* Request a slot reset. */
8202 return PCI_ERS_RESULT_NEED_RESET;
8203}
8204
8205static struct pci_error_handlers _scsih_err_handler = {
8206 .error_detected = _scsih_pci_error_detected,
8207 .mmio_enabled = _scsih_pci_mmio_enabled,
8208 .slot_reset = _scsih_pci_slot_reset,
8209 .resume = _scsih_pci_resume,
8210};
Eric Moore635374e2009-03-09 01:21:12 -06008211
8212static struct pci_driver scsih_driver = {
8213 .name = MPT2SAS_DRIVER_NAME,
8214 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008215 .probe = _scsih_probe,
8216 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05308217 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308218 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008219#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008220 .suspend = _scsih_suspend,
8221 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008222#endif
8223};
8224
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308225/* raid transport support */
8226static struct raid_function_template mpt2sas_raid_functions = {
8227 .cookie = &scsih_driver_template,
8228 .is_raid = _scsih_is_raid,
8229 .get_resync = _scsih_get_resync,
8230 .get_state = _scsih_get_state,
8231};
Eric Moore635374e2009-03-09 01:21:12 -06008232
8233/**
Eric Moored5d135b2009-05-18 13:02:08 -06008234 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008235 *
8236 * Returns 0 success, anything else error.
8237 */
8238static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008239_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008240{
8241 int error;
8242
8243 mpt_ids = 0;
8244 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8245 MPT2SAS_DRIVER_VERSION);
8246
8247 mpt2sas_transport_template =
8248 sas_attach_transport(&mpt2sas_transport_functions);
8249 if (!mpt2sas_transport_template)
8250 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308251 /* raid transport support */
8252 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8253 if (!mpt2sas_raid_template) {
8254 sas_release_transport(mpt2sas_transport_template);
8255 return -ENODEV;
8256 }
Eric Moore635374e2009-03-09 01:21:12 -06008257
8258 mpt2sas_base_initialize_callback_handler();
8259
8260 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008261 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008262
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008263 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008264 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008265
8266 /* base internal commands callback handler */
8267 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308268 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8269 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008270
8271 /* transport internal commands callback handler */
8272 transport_cb_idx = mpt2sas_base_register_callback_handler(
8273 mpt2sas_transport_done);
8274
Kashyap, Desai744090d2009-10-05 15:56:56 +05308275 /* scsih internal commands callback handler */
8276 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8277
Eric Moore635374e2009-03-09 01:21:12 -06008278 /* configuration page API internal commands callback handler */
8279 config_cb_idx = mpt2sas_base_register_callback_handler(
8280 mpt2sas_config_done);
8281
8282 /* ctl module callback handler */
8283 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8284
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308285 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8286 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308287
8288 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8289 _scsih_tm_volume_tr_complete);
8290
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308291 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8292 _scsih_sas_control_complete);
8293
Eric Moore635374e2009-03-09 01:21:12 -06008294 mpt2sas_ctl_init();
8295
8296 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308297 if (error) {
8298 /* raid transport support */
8299 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008300 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308301 }
Eric Moore635374e2009-03-09 01:21:12 -06008302
8303 return error;
8304}
8305
8306/**
Eric Moored5d135b2009-05-18 13:02:08 -06008307 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008308 *
8309 * Returns 0 success, anything else error.
8310 */
8311static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008312_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008313{
8314 printk(KERN_INFO "mpt2sas version %s unloading\n",
8315 MPT2SAS_DRIVER_VERSION);
8316
8317 pci_unregister_driver(&scsih_driver);
8318
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308319 mpt2sas_ctl_exit();
8320
Eric Moore635374e2009-03-09 01:21:12 -06008321 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8322 mpt2sas_base_release_callback_handler(tm_cb_idx);
8323 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308324 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008325 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308326 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008327 mpt2sas_base_release_callback_handler(config_cb_idx);
8328 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8329
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308330 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308331 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308332 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8333
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308334 /* raid transport support */
8335 raid_class_release(mpt2sas_raid_template);
8336 sas_release_transport(mpt2sas_transport_template);
8337
Eric Moore635374e2009-03-09 01:21:12 -06008338}
8339
Eric Moored5d135b2009-05-18 13:02:08 -06008340module_init(_scsih_init);
8341module_exit(_scsih_exit);