blob: 8e2e9973df294664514010439841150728b8e094 [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);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +0530102MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 32767 default=32767");
Kashyap, Desaia3e1e552011-06-14 10:56:12 +0530103
Eric Moore635374e2009-03-09 01:21:12 -0600104/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
105#define MPT2SAS_MAX_LUN (16895)
106static int max_lun = MPT2SAS_MAX_LUN;
107module_param(max_lun, int, 0);
108MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
109
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530110/* diag_buffer_enable is bitwise
111 * bit 0 set = TRACE
112 * bit 1 set = SNAPSHOT
113 * bit 2 set = EXTENDED
114 *
115 * Either bit can be set, or both
116 */
117static int diag_buffer_enable = -1;
118module_param(diag_buffer_enable, int, 0);
119MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
120 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
121
Eric Moore635374e2009-03-09 01:21:12 -0600122/**
123 * struct sense_info - common structure for obtaining sense keys
124 * @skey: sense key
125 * @asc: additional sense code
126 * @ascq: additional sense code qualifier
127 */
128struct sense_info {
129 u8 skey;
130 u8 asc;
131 u8 ascq;
132};
133
134
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530135#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530136#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
137#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
Eric Moore635374e2009-03-09 01:21:12 -0600138/**
139 * struct fw_event_work - firmware event struct
140 * @list: link list framework
141 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530142 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600143 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530144 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600145 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530146 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600147 * @ignore: flag meaning this event has been marked to ignore
148 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
149 * @event_data: reply event data payload follows
150 *
151 * This object stored on ioc->fw_event_list.
152 */
153struct fw_event_work {
154 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530155 u8 cancel_pending_work;
156 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600157 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530158 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600159 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530160 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600161 u8 ignore;
162 u16 event;
163 void *event_data;
164};
165
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530166/* raid transport support */
167static struct raid_template *mpt2sas_raid_template;
168
Eric Moore635374e2009-03-09 01:21:12 -0600169/**
170 * struct _scsi_io_transfer - scsi io transfer
171 * @handle: sas device handle (assigned by firmware)
172 * @is_raid: flag set for hidden raid components
173 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
174 * @data_length: data transfer length
175 * @data_dma: dma pointer to data
176 * @sense: sense data
177 * @lun: lun number
178 * @cdb_length: cdb length
179 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600180 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530181 * @VF_ID: virtual function id
182 * @VP_ID: virtual port id
183 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600184 * @sense_length: sense length
185 * @ioc_status: ioc status
186 * @scsi_state: scsi state
187 * @scsi_status: scsi staus
188 * @log_info: log information
189 * @transfer_length: data length transfer when there is a reply message
190 *
191 * Used for sending internal scsi commands to devices within this module.
192 * Refer to _scsi_send_scsi_io().
193 */
194struct _scsi_io_transfer {
195 u16 handle;
196 u8 is_raid;
197 enum dma_data_direction dir;
198 u32 data_length;
199 dma_addr_t data_dma;
200 u8 sense[SCSI_SENSE_BUFFERSIZE];
201 u32 lun;
202 u8 cdb_length;
203 u8 cdb[32];
204 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530205 u8 VF_ID;
206 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600207 u8 valid_reply;
208 /* the following bits are only valid when 'valid_reply = 1' */
209 u32 sense_length;
210 u16 ioc_status;
211 u8 scsi_state;
212 u8 scsi_status;
213 u32 log_info;
214 u32 transfer_length;
215};
216
217/*
218 * The pci device ids are defined in mpi/mpi2_cnfg.h.
219 */
220static struct pci_device_id scsih_pci_table[] = {
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
222 PCI_ANY_ID, PCI_ANY_ID },
223 /* Falcon ~ 2008*/
224 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
225 PCI_ANY_ID, PCI_ANY_ID },
226 /* Liberator ~ 2108 */
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
228 PCI_ANY_ID, PCI_ANY_ID },
229 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
230 PCI_ANY_ID, PCI_ANY_ID },
231 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
232 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530233 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
235 PCI_ANY_ID, PCI_ANY_ID },
236 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
237 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530238 /* Thunderbolt ~ 2208 */
239 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
240 PCI_ANY_ID, PCI_ANY_ID },
241 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
242 PCI_ANY_ID, PCI_ANY_ID },
243 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
244 PCI_ANY_ID, PCI_ANY_ID },
245 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
246 PCI_ANY_ID, PCI_ANY_ID },
247 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
248 PCI_ANY_ID, PCI_ANY_ID },
249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
250 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530251 /* Mustang ~ 2308 */
252 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530253 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530254 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
255 PCI_ANY_ID, PCI_ANY_ID },
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530257 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530258 /* SSS6200 */
259 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
260 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600261 {0} /* Terminating entry */
262};
263MODULE_DEVICE_TABLE(pci, scsih_pci_table);
264
265/**
Eric Moored5d135b2009-05-18 13:02:08 -0600266 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600267 *
268 * Note: The logging levels are defined in mpt2sas_debug.h.
269 */
270static int
Eric Moored5d135b2009-05-18 13:02:08 -0600271_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600272{
273 int ret = param_set_int(val, kp);
274 struct MPT2SAS_ADAPTER *ioc;
275
276 if (ret)
277 return ret;
278
279 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600280 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600281 ioc->logging_level = logging_level;
282 return 0;
283}
Eric Moored5d135b2009-05-18 13:02:08 -0600284module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600285 &logging_level, 0644);
286
287/**
288 * _scsih_srch_boot_sas_address - search based on sas_address
289 * @sas_address: sas address
290 * @boot_device: boot device object from bios page 2
291 *
292 * Returns 1 when there's a match, 0 means no match.
293 */
294static inline int
295_scsih_srch_boot_sas_address(u64 sas_address,
296 Mpi2BootDeviceSasWwid_t *boot_device)
297{
298 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
299}
300
301/**
302 * _scsih_srch_boot_device_name - search based on device name
303 * @device_name: device name specified in INDENTIFY fram
304 * @boot_device: boot device object from bios page 2
305 *
306 * Returns 1 when there's a match, 0 means no match.
307 */
308static inline int
309_scsih_srch_boot_device_name(u64 device_name,
310 Mpi2BootDeviceDeviceName_t *boot_device)
311{
312 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
313}
314
315/**
316 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
317 * @enclosure_logical_id: enclosure logical id
318 * @slot_number: slot number
319 * @boot_device: boot device object from bios page 2
320 *
321 * Returns 1 when there's a match, 0 means no match.
322 */
323static inline int
324_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
325 Mpi2BootDeviceEnclosureSlot_t *boot_device)
326{
327 return (enclosure_logical_id == le64_to_cpu(boot_device->
328 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
329 SlotNumber)) ? 1 : 0;
330}
331
332/**
333 * _scsih_is_boot_device - search for matching boot device.
334 * @sas_address: sas address
335 * @device_name: device name specified in INDENTIFY fram
336 * @enclosure_logical_id: enclosure logical id
337 * @slot_number: slot number
338 * @form: specifies boot device form
339 * @boot_device: boot device object from bios page 2
340 *
341 * Returns 1 when there's a match, 0 means no match.
342 */
343static int
344_scsih_is_boot_device(u64 sas_address, u64 device_name,
345 u64 enclosure_logical_id, u16 slot, u8 form,
346 Mpi2BiosPage2BootDevice_t *boot_device)
347{
348 int rc = 0;
349
350 switch (form) {
351 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
352 if (!sas_address)
353 break;
354 rc = _scsih_srch_boot_sas_address(
355 sas_address, &boot_device->SasWwid);
356 break;
357 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
358 if (!enclosure_logical_id)
359 break;
360 rc = _scsih_srch_boot_encl_slot(
361 enclosure_logical_id,
362 slot, &boot_device->EnclosureSlot);
363 break;
364 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
365 if (!device_name)
366 break;
367 rc = _scsih_srch_boot_device_name(
368 device_name, &boot_device->DeviceName);
369 break;
370 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
371 break;
372 }
373
374 return rc;
375}
376
377/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530378 * _scsih_get_sas_address - set the sas_address for given device handle
379 * @handle: device handle
380 * @sas_address: sas address
381 *
382 * Returns 0 success, non-zero when failure
383 */
384static int
385_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
386 u64 *sas_address)
387{
388 Mpi2SasDevicePage0_t sas_device_pg0;
389 Mpi2ConfigReply_t mpi_reply;
390 u32 ioc_status;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530391 *sas_address = 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530392
393 if (handle <= ioc->sas_hba.num_phys) {
394 *sas_address = ioc->sas_hba.sas_address;
395 return 0;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530396 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530397
398 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
399 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530400 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
401 __FILE__, __LINE__, __func__);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530402 return -ENXIO;
403 }
404
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530405 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
406 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
407 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
408 return 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530409 }
410
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530411 /* we hit this becuase the given parent handle doesn't exist */
412 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
413 return -ENXIO;
414 /* else error case */
415 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
416 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
417 __FILE__, __LINE__, __func__);
418 return -EIO;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530419}
420
421/**
Eric Moore635374e2009-03-09 01:21:12 -0600422 * _scsih_determine_boot_device - determine boot device.
423 * @ioc: per adapter object
424 * @device: either sas_device or raid_device object
425 * @is_raid: [flag] 1 = raid object, 0 = sas object
426 *
427 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300428 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600429 * There are primary, alternate, and current entries in bios page 2. The order
430 * priority is primary, alternate, then current. This routine saves
431 * the corresponding device object and is_raid flag in the ioc object.
432 * The saved data to be used later in _scsih_probe_boot_devices().
433 */
434static void
435_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
436 void *device, u8 is_raid)
437{
438 struct _sas_device *sas_device;
439 struct _raid_device *raid_device;
440 u64 sas_address;
441 u64 device_name;
442 u64 enclosure_logical_id;
443 u16 slot;
444
445 /* only process this function when driver loads */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530446 if (!ioc->is_driver_loading)
447 return;
448
449 /* no Bios, return immediately */
450 if (!ioc->bios_pg3.BiosVersion)
Eric Moore635374e2009-03-09 01:21:12 -0600451 return;
452
453 if (!is_raid) {
454 sas_device = device;
455 sas_address = sas_device->sas_address;
456 device_name = sas_device->device_name;
457 enclosure_logical_id = sas_device->enclosure_logical_id;
458 slot = sas_device->slot;
459 } else {
460 raid_device = device;
461 sas_address = raid_device->wwid;
462 device_name = 0;
463 enclosure_logical_id = 0;
464 slot = 0;
465 }
466
467 if (!ioc->req_boot_device.device) {
468 if (_scsih_is_boot_device(sas_address, device_name,
469 enclosure_logical_id, slot,
470 (ioc->bios_pg2.ReqBootDeviceForm &
471 MPI2_BIOSPAGE2_FORM_MASK),
472 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530473 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600474 "%s: req_boot_device(0x%016llx)\n",
475 ioc->name, __func__,
476 (unsigned long long)sas_address));
477 ioc->req_boot_device.device = device;
478 ioc->req_boot_device.is_raid = is_raid;
479 }
480 }
481
482 if (!ioc->req_alt_boot_device.device) {
483 if (_scsih_is_boot_device(sas_address, device_name,
484 enclosure_logical_id, slot,
485 (ioc->bios_pg2.ReqAltBootDeviceForm &
486 MPI2_BIOSPAGE2_FORM_MASK),
487 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530488 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600489 "%s: req_alt_boot_device(0x%016llx)\n",
490 ioc->name, __func__,
491 (unsigned long long)sas_address));
492 ioc->req_alt_boot_device.device = device;
493 ioc->req_alt_boot_device.is_raid = is_raid;
494 }
495 }
496
497 if (!ioc->current_boot_device.device) {
498 if (_scsih_is_boot_device(sas_address, device_name,
499 enclosure_logical_id, slot,
500 (ioc->bios_pg2.CurrentBootDeviceForm &
501 MPI2_BIOSPAGE2_FORM_MASK),
502 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530503 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600504 "%s: current_boot_device(0x%016llx)\n",
505 ioc->name, __func__,
506 (unsigned long long)sas_address));
507 ioc->current_boot_device.device = device;
508 ioc->current_boot_device.is_raid = is_raid;
509 }
510 }
511}
512
513/**
514 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
515 * @ioc: per adapter object
516 * @sas_address: sas address
517 * Context: Calling function should acquire ioc->sas_device_lock
518 *
519 * This searches for sas_device based on sas_address, then return sas_device
520 * object.
521 */
522struct _sas_device *
523mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
524 u64 sas_address)
525{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530526 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600527
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530528 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
529 if (sas_device->sas_address == sas_address)
530 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600531
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530532 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
533 if (sas_device->sas_address == sas_address)
534 return sas_device;
535
536 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600537}
538
539/**
540 * _scsih_sas_device_find_by_handle - sas device search
541 * @ioc: per adapter object
542 * @handle: sas device handle (assigned by firmware)
543 * Context: Calling function should acquire ioc->sas_device_lock
544 *
545 * This searches for sas_device based on sas_address, then return sas_device
546 * object.
547 */
548static struct _sas_device *
549_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
550{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530551 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600552
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530553 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
554 if (sas_device->handle == handle)
555 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600556
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530557 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
558 if (sas_device->handle == handle)
559 return sas_device;
560
561 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600562}
563
564/**
565 * _scsih_sas_device_remove - remove sas_device from list.
566 * @ioc: per adapter object
567 * @sas_device: the sas_device object
568 * Context: This function will acquire ioc->sas_device_lock.
569 *
570 * Removing object and freeing associated memory from the ioc->sas_device_list.
571 */
572static void
573_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
574 struct _sas_device *sas_device)
575{
576 unsigned long flags;
577
Kashyap, Desai980ead32010-04-08 17:55:22 +0530578 if (!sas_device)
579 return;
580
Eric Moore635374e2009-03-09 01:21:12 -0600581 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530582 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
583 sas_device->sas_address)) {
584 list_del(&sas_device->list);
585 kfree(sas_device);
586 }
Eric Moore635374e2009-03-09 01:21:12 -0600587 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
588}
589
590/**
591 * _scsih_sas_device_add - insert sas_device to the list.
592 * @ioc: per adapter object
593 * @sas_device: the sas_device object
594 * Context: This function will acquire ioc->sas_device_lock.
595 *
596 * Adding new object to the ioc->sas_device_list.
597 */
598static void
599_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
600 struct _sas_device *sas_device)
601{
602 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600603
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530604 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600605 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
606 sas_device->handle, (unsigned long long)sas_device->sas_address));
607
608 spin_lock_irqsave(&ioc->sas_device_lock, flags);
609 list_add_tail(&sas_device->list, &ioc->sas_device_list);
610 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
611
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530612 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530613 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -0600614 _scsih_sas_device_remove(ioc, sas_device);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +0530615 } else if (!sas_device->starget) {
616 /* When asyn scanning is enabled, its not possible to remove
617 * devices while scanning is turned on due to an oops in
618 * scsi_sysfs_add_sdev()->add_device()->sysfs_addrm_start()
619 */
620 if (!ioc->is_driver_loading)
621 mpt2sas_transport_port_remove(ioc,
622 sas_device->sas_address,
623 sas_device->sas_address_parent);
624 _scsih_sas_device_remove(ioc, sas_device);
625 }
Eric Moore635374e2009-03-09 01:21:12 -0600626}
627
628/**
629 * _scsih_sas_device_init_add - insert sas_device to the list.
630 * @ioc: per adapter object
631 * @sas_device: the sas_device object
632 * Context: This function will acquire ioc->sas_device_lock.
633 *
634 * Adding new object at driver load time to the ioc->sas_device_init_list.
635 */
636static void
637_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
638 struct _sas_device *sas_device)
639{
640 unsigned long flags;
641
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530642 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600643 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
644 sas_device->handle, (unsigned long long)sas_device->sas_address));
645
646 spin_lock_irqsave(&ioc->sas_device_lock, flags);
647 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
648 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
649 _scsih_determine_boot_device(ioc, sas_device, 0);
650}
651
652/**
Eric Moore635374e2009-03-09 01:21:12 -0600653 * _scsih_raid_device_find_by_id - raid device search
654 * @ioc: per adapter object
655 * @id: sas device target id
656 * @channel: sas device channel
657 * Context: Calling function should acquire ioc->raid_device_lock
658 *
659 * This searches for raid_device based on target id, then return raid_device
660 * object.
661 */
662static struct _raid_device *
663_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
664{
665 struct _raid_device *raid_device, *r;
666
667 r = NULL;
668 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
669 if (raid_device->id == id && raid_device->channel == channel) {
670 r = raid_device;
671 goto out;
672 }
673 }
674
675 out:
676 return r;
677}
678
679/**
680 * _scsih_raid_device_find_by_handle - raid device search
681 * @ioc: per adapter object
682 * @handle: sas device handle (assigned by firmware)
683 * Context: Calling function should acquire ioc->raid_device_lock
684 *
685 * This searches for raid_device based on handle, then return raid_device
686 * object.
687 */
688static struct _raid_device *
689_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
690{
691 struct _raid_device *raid_device, *r;
692
693 r = NULL;
694 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
695 if (raid_device->handle != handle)
696 continue;
697 r = raid_device;
698 goto out;
699 }
700
701 out:
702 return r;
703}
704
705/**
706 * _scsih_raid_device_find_by_wwid - raid device search
707 * @ioc: per adapter object
708 * @handle: sas device handle (assigned by firmware)
709 * Context: Calling function should acquire ioc->raid_device_lock
710 *
711 * This searches for raid_device based on wwid, then return raid_device
712 * object.
713 */
714static struct _raid_device *
715_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
716{
717 struct _raid_device *raid_device, *r;
718
719 r = NULL;
720 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
721 if (raid_device->wwid != wwid)
722 continue;
723 r = raid_device;
724 goto out;
725 }
726
727 out:
728 return r;
729}
730
731/**
732 * _scsih_raid_device_add - add raid_device object
733 * @ioc: per adapter object
734 * @raid_device: raid_device object
735 *
736 * This is added to the raid_device_list link list.
737 */
738static void
739_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
740 struct _raid_device *raid_device)
741{
742 unsigned long flags;
743
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530744 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600745 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
746 raid_device->handle, (unsigned long long)raid_device->wwid));
747
748 spin_lock_irqsave(&ioc->raid_device_lock, flags);
749 list_add_tail(&raid_device->list, &ioc->raid_device_list);
750 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
751}
752
753/**
754 * _scsih_raid_device_remove - delete raid_device object
755 * @ioc: per adapter object
756 * @raid_device: raid_device object
757 *
758 * This is removed from the raid_device_list link list.
759 */
760static void
761_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
762 struct _raid_device *raid_device)
763{
764 unsigned long flags;
765
766 spin_lock_irqsave(&ioc->raid_device_lock, flags);
767 list_del(&raid_device->list);
768 memset(raid_device, 0, sizeof(struct _raid_device));
769 kfree(raid_device);
770 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
771}
772
773/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530774 * mpt2sas_scsih_expander_find_by_handle - expander device search
775 * @ioc: per adapter object
776 * @handle: expander handle (assigned by firmware)
777 * Context: Calling function should acquire ioc->sas_device_lock
778 *
779 * This searches for expander device based on handle, then returns the
780 * sas_node object.
781 */
782struct _sas_node *
783mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
784{
785 struct _sas_node *sas_expander, *r;
786
787 r = NULL;
788 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
789 if (sas_expander->handle != handle)
790 continue;
791 r = sas_expander;
792 goto out;
793 }
794 out:
795 return r;
796}
797
798/**
Eric Moore635374e2009-03-09 01:21:12 -0600799 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
800 * @ioc: per adapter object
801 * @sas_address: sas address
802 * Context: Calling function should acquire ioc->sas_node_lock.
803 *
804 * This searches for expander device based on sas_address, then returns the
805 * sas_node object.
806 */
807struct _sas_node *
808mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
809 u64 sas_address)
810{
811 struct _sas_node *sas_expander, *r;
812
813 r = NULL;
814 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
815 if (sas_expander->sas_address != sas_address)
816 continue;
817 r = sas_expander;
818 goto out;
819 }
820 out:
821 return r;
822}
823
824/**
825 * _scsih_expander_node_add - insert expander device to the list.
826 * @ioc: per adapter object
827 * @sas_expander: the sas_device object
828 * Context: This function will acquire ioc->sas_node_lock.
829 *
830 * Adding new object to the ioc->sas_expander_list.
831 *
832 * Return nothing.
833 */
834static void
835_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
836 struct _sas_node *sas_expander)
837{
838 unsigned long flags;
839
840 spin_lock_irqsave(&ioc->sas_node_lock, flags);
841 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
842 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
843}
844
845/**
846 * _scsih_is_end_device - determines if device is an end device
847 * @device_info: bitfield providing information about the device.
848 * Context: none
849 *
850 * Returns 1 if end device.
851 */
852static int
853_scsih_is_end_device(u32 device_info)
854{
855 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
856 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
857 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
858 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
859 return 1;
860 else
861 return 0;
862}
863
864/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530865 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600866 * @ioc: per adapter object
867 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600868 *
869 * Returns the smid stored scmd pointer.
870 */
871static struct scsi_cmnd *
872_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
873{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530874 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600875}
876
877/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530878 * _scsih_scsi_lookup_get_clear - returns scmd entry
879 * @ioc: per adapter object
880 * @smid: system request message index
881 *
882 * Returns the smid stored scmd pointer.
883 * Then will derefrence the stored scmd pointer.
884 */
885static inline struct scsi_cmnd *
886_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
887{
888 unsigned long flags;
889 struct scsi_cmnd *scmd;
890
891 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
892 scmd = ioc->scsi_lookup[smid - 1].scmd;
893 ioc->scsi_lookup[smid - 1].scmd = NULL;
894 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
895
896 return scmd;
897}
898
899/**
Eric Moore635374e2009-03-09 01:21:12 -0600900 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
901 * @ioc: per adapter object
902 * @smid: system request message index
903 * @scmd: pointer to scsi command object
904 * Context: This function will acquire ioc->scsi_lookup_lock.
905 *
906 * This will search for a scmd pointer in the scsi_lookup array,
907 * returning the revelent smid. A returned value of zero means invalid.
908 */
909static u16
910_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
911 *scmd)
912{
913 u16 smid;
914 unsigned long flags;
915 int i;
916
917 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
918 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530919 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600920 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530921 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600922 goto out;
923 }
924 }
925 out:
926 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
927 return smid;
928}
929
930/**
931 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
932 * @ioc: per adapter object
933 * @id: target id
934 * @channel: channel
935 * Context: This function will acquire ioc->scsi_lookup_lock.
936 *
937 * This will search for a matching channel:id in the scsi_lookup array,
938 * returning 1 if found.
939 */
940static u8
941_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
942 int channel)
943{
944 u8 found;
945 unsigned long flags;
946 int i;
947
948 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
949 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530950 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600951 if (ioc->scsi_lookup[i].scmd &&
952 (ioc->scsi_lookup[i].scmd->device->id == id &&
953 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
954 found = 1;
955 goto out;
956 }
957 }
958 out:
959 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
960 return found;
961}
962
963/**
Eric Moore993e0da2009-05-18 13:00:45 -0600964 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
965 * @ioc: per adapter object
966 * @id: target id
967 * @lun: lun number
968 * @channel: channel
969 * Context: This function will acquire ioc->scsi_lookup_lock.
970 *
971 * This will search for a matching channel:id:lun in the scsi_lookup array,
972 * returning 1 if found.
973 */
974static u8
975_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
976 unsigned int lun, int channel)
977{
978 u8 found;
979 unsigned long flags;
980 int i;
981
982 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
983 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530984 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600985 if (ioc->scsi_lookup[i].scmd &&
986 (ioc->scsi_lookup[i].scmd->device->id == id &&
987 ioc->scsi_lookup[i].scmd->device->channel == channel &&
988 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
989 found = 1;
990 goto out;
991 }
992 }
993 out:
994 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
995 return found;
996}
997
998/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530999 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -06001000 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301001 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -06001002 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301003 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001004 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301005static struct chain_tracker *
1006_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001007{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301008 struct chain_tracker *chain_req;
1009 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001010
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301011 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1012 if (list_empty(&ioc->free_chain_list)) {
1013 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1014 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
1015 ioc->name);
1016 return NULL;
1017 }
1018 chain_req = list_entry(ioc->free_chain_list.next,
1019 struct chain_tracker, tracker_list);
1020 list_del_init(&chain_req->tracker_list);
1021 list_add_tail(&chain_req->tracker_list,
1022 &ioc->scsi_lookup[smid - 1].chain_list);
1023 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1024 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001025}
1026
1027/**
1028 * _scsih_build_scatter_gather - main sg creation routine
1029 * @ioc: per adapter object
1030 * @scmd: scsi command
1031 * @smid: system request message index
1032 * Context: none.
1033 *
1034 * The main routine that builds scatter gather table from a given
1035 * scsi request sent via the .queuecommand main handler.
1036 *
1037 * Returns 0 success, anything else error
1038 */
1039static int
1040_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1041 struct scsi_cmnd *scmd, u16 smid)
1042{
1043 Mpi2SCSIIORequest_t *mpi_request;
1044 dma_addr_t chain_dma;
1045 struct scatterlist *sg_scmd;
1046 void *sg_local, *chain;
1047 u32 chain_offset;
1048 u32 chain_length;
1049 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001050 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001051 u32 sges_in_segment;
1052 u32 sgl_flags;
1053 u32 sgl_flags_last_element;
1054 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301055 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001056
1057 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1058
1059 /* init scatter gather flags */
1060 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1061 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1062 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1063 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1064 << MPI2_SGE_FLAGS_SHIFT;
1065 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1066 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1067 << MPI2_SGE_FLAGS_SHIFT;
1068 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1069
1070 sg_scmd = scsi_sglist(scmd);
1071 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001072 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001073 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1074 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1075 return -ENOMEM;
1076 }
1077
1078 sg_local = &mpi_request->SGL;
1079 sges_in_segment = ioc->max_sges_in_main_message;
1080 if (sges_left <= sges_in_segment)
1081 goto fill_in_last_segment;
1082
1083 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1084 (sges_in_segment * ioc->sge_size))/4;
1085
1086 /* fill in main message segment when there is a chain following */
1087 while (sges_in_segment) {
1088 if (sges_in_segment == 1)
1089 ioc->base_add_sg_single(sg_local,
1090 sgl_flags_last_element | sg_dma_len(sg_scmd),
1091 sg_dma_address(sg_scmd));
1092 else
1093 ioc->base_add_sg_single(sg_local, sgl_flags |
1094 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1095 sg_scmd = sg_next(sg_scmd);
1096 sg_local += ioc->sge_size;
1097 sges_left--;
1098 sges_in_segment--;
1099 }
1100
1101 /* initializing the chain flags and pointers */
1102 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301103 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1104 if (!chain_req)
1105 return -1;
1106 chain = chain_req->chain_buffer;
1107 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001108 do {
1109 sges_in_segment = (sges_left <=
1110 ioc->max_sges_in_chain_message) ? sges_left :
1111 ioc->max_sges_in_chain_message;
1112 chain_offset = (sges_left == sges_in_segment) ?
1113 0 : (sges_in_segment * ioc->sge_size)/4;
1114 chain_length = sges_in_segment * ioc->sge_size;
1115 if (chain_offset) {
1116 chain_offset = chain_offset <<
1117 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1118 chain_length += ioc->sge_size;
1119 }
1120 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1121 chain_length, chain_dma);
1122 sg_local = chain;
1123 if (!chain_offset)
1124 goto fill_in_last_segment;
1125
1126 /* fill in chain segments */
1127 while (sges_in_segment) {
1128 if (sges_in_segment == 1)
1129 ioc->base_add_sg_single(sg_local,
1130 sgl_flags_last_element |
1131 sg_dma_len(sg_scmd),
1132 sg_dma_address(sg_scmd));
1133 else
1134 ioc->base_add_sg_single(sg_local, sgl_flags |
1135 sg_dma_len(sg_scmd),
1136 sg_dma_address(sg_scmd));
1137 sg_scmd = sg_next(sg_scmd);
1138 sg_local += ioc->sge_size;
1139 sges_left--;
1140 sges_in_segment--;
1141 }
1142
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301143 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1144 if (!chain_req)
1145 return -1;
1146 chain = chain_req->chain_buffer;
1147 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001148 } while (1);
1149
1150
1151 fill_in_last_segment:
1152
1153 /* fill the last segment */
1154 while (sges_left) {
1155 if (sges_left == 1)
1156 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1157 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1158 else
1159 ioc->base_add_sg_single(sg_local, sgl_flags |
1160 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1161 sg_scmd = sg_next(sg_scmd);
1162 sg_local += ioc->sge_size;
1163 sges_left--;
1164 }
1165
1166 return 0;
1167}
1168
1169/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301170 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001171 * @sdev: scsi device struct
1172 * @qdepth: requested queue depth
1173 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301174 *
1175 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001176 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301177static void
1178_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001179{
1180 struct Scsi_Host *shost = sdev->host;
1181 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301182 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1183 struct MPT2SAS_DEVICE *sas_device_priv_data;
1184 struct MPT2SAS_TARGET *sas_target_priv_data;
1185 struct _sas_device *sas_device;
1186 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001187
1188 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301189
1190 /* limit max device queue for SATA to 32 */
1191 sas_device_priv_data = sdev->hostdata;
1192 if (!sas_device_priv_data)
1193 goto not_sata;
1194 sas_target_priv_data = sas_device_priv_data->sas_target;
1195 if (!sas_target_priv_data)
1196 goto not_sata;
1197 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1198 goto not_sata;
1199 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1200 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1201 sas_device_priv_data->sas_target->sas_address);
1202 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1203 if (sas_device && sas_device->device_info &
1204 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1205 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1206
1207 not_sata:
1208
Eric Moore635374e2009-03-09 01:21:12 -06001209 if (!sdev->tagged_supported)
1210 max_depth = 1;
1211 if (qdepth > max_depth)
1212 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301213 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1214}
1215
1216/**
1217 * _scsih_change_queue_depth - setting device queue depth
1218 * @sdev: scsi device struct
1219 * @qdepth: requested queue depth
1220 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1221 * (see include/scsi/scsi_host.h for definition)
1222 *
1223 * Returns queue depth.
1224 */
1225static int
1226_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1227{
1228 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1229 _scsih_adjust_queue_depth(sdev, qdepth);
1230 else if (reason == SCSI_QDEPTH_QFULL)
1231 scsi_track_queue_full(sdev, qdepth);
1232 else
1233 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001234
1235 if (sdev->inquiry_len > 7)
1236 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1237 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1238 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1239 sdev->ordered_tags, sdev->scsi_level,
1240 (sdev->inquiry[7] & 2) >> 1);
1241
1242 return sdev->queue_depth;
1243}
1244
1245/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301246 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001247 * @sdev: scsi device struct
1248 * @tag_type: requested tag type
1249 *
1250 * Returns queue tag type.
1251 */
1252static int
Eric Moored5d135b2009-05-18 13:02:08 -06001253_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001254{
1255 if (sdev->tagged_supported) {
1256 scsi_set_tag_type(sdev, tag_type);
1257 if (tag_type)
1258 scsi_activate_tcq(sdev, sdev->queue_depth);
1259 else
1260 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1261 } else
1262 tag_type = 0;
1263
1264 return tag_type;
1265}
1266
1267/**
Eric Moored5d135b2009-05-18 13:02:08 -06001268 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001269 * @starget: scsi target struct
1270 *
1271 * Returns 0 if ok. Any other return is assumed to be an error and
1272 * the device is ignored.
1273 */
1274static int
Eric Moored5d135b2009-05-18 13:02:08 -06001275_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001276{
1277 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1278 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1279 struct MPT2SAS_TARGET *sas_target_priv_data;
1280 struct _sas_device *sas_device;
1281 struct _raid_device *raid_device;
1282 unsigned long flags;
1283 struct sas_rphy *rphy;
1284
1285 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1286 if (!sas_target_priv_data)
1287 return -ENOMEM;
1288
1289 starget->hostdata = sas_target_priv_data;
1290 sas_target_priv_data->starget = starget;
1291 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1292
1293 /* RAID volumes */
1294 if (starget->channel == RAID_CHANNEL) {
1295 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1296 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1297 starget->channel);
1298 if (raid_device) {
1299 sas_target_priv_data->handle = raid_device->handle;
1300 sas_target_priv_data->sas_address = raid_device->wwid;
1301 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301302 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001303 raid_device->starget = starget;
1304 }
1305 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1306 return 0;
1307 }
1308
1309 /* sas/sata devices */
1310 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1311 rphy = dev_to_rphy(starget->dev.parent);
1312 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1313 rphy->identify.sas_address);
1314
1315 if (sas_device) {
1316 sas_target_priv_data->handle = sas_device->handle;
1317 sas_target_priv_data->sas_address = sas_device->sas_address;
1318 sas_device->starget = starget;
1319 sas_device->id = starget->id;
1320 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301321 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001322 sas_target_priv_data->flags |=
1323 MPT_TARGET_FLAGS_RAID_COMPONENT;
1324 }
1325 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1326
1327 return 0;
1328}
1329
1330/**
Eric Moored5d135b2009-05-18 13:02:08 -06001331 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001332 * @starget: scsi target struct
1333 *
1334 * Returns nothing.
1335 */
1336static void
Eric Moored5d135b2009-05-18 13:02:08 -06001337_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001338{
1339 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1340 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1341 struct MPT2SAS_TARGET *sas_target_priv_data;
1342 struct _sas_device *sas_device;
1343 struct _raid_device *raid_device;
1344 unsigned long flags;
1345 struct sas_rphy *rphy;
1346
1347 sas_target_priv_data = starget->hostdata;
1348 if (!sas_target_priv_data)
1349 return;
1350
1351 if (starget->channel == RAID_CHANNEL) {
1352 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1353 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1354 starget->channel);
1355 if (raid_device) {
1356 raid_device->starget = NULL;
1357 raid_device->sdev = NULL;
1358 }
1359 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1360 goto out;
1361 }
1362
1363 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1364 rphy = dev_to_rphy(starget->dev.parent);
1365 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1366 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001367 if (sas_device && (sas_device->starget == starget) &&
1368 (sas_device->id == starget->id) &&
1369 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001370 sas_device->starget = NULL;
1371
1372 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1373
1374 out:
1375 kfree(sas_target_priv_data);
1376 starget->hostdata = NULL;
1377}
1378
1379/**
Eric Moored5d135b2009-05-18 13:02:08 -06001380 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001381 * @sdev: scsi device struct
1382 *
1383 * Returns 0 if ok. Any other return is assumed to be an error and
1384 * the device is ignored.
1385 */
1386static int
Eric Moored5d135b2009-05-18 13:02:08 -06001387_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001388{
1389 struct Scsi_Host *shost;
1390 struct MPT2SAS_ADAPTER *ioc;
1391 struct MPT2SAS_TARGET *sas_target_priv_data;
1392 struct MPT2SAS_DEVICE *sas_device_priv_data;
1393 struct scsi_target *starget;
1394 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001395 unsigned long flags;
1396
1397 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1398 if (!sas_device_priv_data)
1399 return -ENOMEM;
1400
1401 sas_device_priv_data->lun = sdev->lun;
1402 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1403
1404 starget = scsi_target(sdev);
1405 sas_target_priv_data = starget->hostdata;
1406 sas_target_priv_data->num_luns++;
1407 sas_device_priv_data->sas_target = sas_target_priv_data;
1408 sdev->hostdata = sas_device_priv_data;
1409 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1410 sdev->no_uld_attach = 1;
1411
1412 shost = dev_to_shost(&starget->dev);
1413 ioc = shost_priv(shost);
1414 if (starget->channel == RAID_CHANNEL) {
1415 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1416 raid_device = _scsih_raid_device_find_by_id(ioc,
1417 starget->id, starget->channel);
1418 if (raid_device)
1419 raid_device->sdev = sdev; /* raid is single lun */
1420 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001421 }
1422
Eric Moore635374e2009-03-09 01:21:12 -06001423 return 0;
1424}
1425
1426/**
Eric Moored5d135b2009-05-18 13:02:08 -06001427 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001428 * @sdev: scsi device struct
1429 *
1430 * Returns nothing.
1431 */
1432static void
Eric Moored5d135b2009-05-18 13:02:08 -06001433_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001434{
1435 struct MPT2SAS_TARGET *sas_target_priv_data;
1436 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301437 struct Scsi_Host *shost;
1438 struct MPT2SAS_ADAPTER *ioc;
1439 struct _sas_device *sas_device;
1440 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001441
1442 if (!sdev->hostdata)
1443 return;
1444
1445 starget = scsi_target(sdev);
1446 sas_target_priv_data = starget->hostdata;
1447 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301448
1449 shost = dev_to_shost(&starget->dev);
1450 ioc = shost_priv(shost);
1451
1452 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1453 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1454 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1455 sas_target_priv_data->sas_address);
nagalakshmi.nandigama@lsi.com23edb6e2011-12-01 07:43:50 +05301456 if (sas_device && !sas_target_priv_data->num_luns)
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301457 sas_device->starget = NULL;
1458 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1459 }
1460
Eric Moore635374e2009-03-09 01:21:12 -06001461 kfree(sdev->hostdata);
1462 sdev->hostdata = NULL;
1463}
1464
1465/**
Eric Moored5d135b2009-05-18 13:02:08 -06001466 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001467 * @ioc: per adapter object
1468 * @sas_device: the sas_device object
1469 * @sdev: scsi device struct
1470 */
1471static void
Eric Moored5d135b2009-05-18 13:02:08 -06001472_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001473 struct _sas_device *sas_device, struct scsi_device *sdev)
1474{
1475 Mpi2ConfigReply_t mpi_reply;
1476 Mpi2SasDevicePage0_t sas_device_pg0;
1477 u32 ioc_status;
1478 u16 flags;
1479 u32 device_info;
1480
1481 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1482 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1483 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1484 ioc->name, __FILE__, __LINE__, __func__);
1485 return;
1486 }
1487
1488 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1489 MPI2_IOCSTATUS_MASK;
1490 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1491 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1492 ioc->name, __FILE__, __LINE__, __func__);
1493 return;
1494 }
1495
1496 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301497 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001498
1499 sdev_printk(KERN_INFO, sdev,
1500 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1501 "sw_preserve(%s)\n",
1502 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1503 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1504 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1505 "n",
1506 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1507 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1508 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1509}
1510
1511/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301512 * _scsih_is_raid - return boolean indicating device is raid volume
1513 * @dev the device struct object
1514 */
1515static int
1516_scsih_is_raid(struct device *dev)
1517{
1518 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301519 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301520
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301521 if (ioc->is_warpdrive)
1522 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301523 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1524}
1525
1526/**
1527 * _scsih_get_resync - get raid volume resync percent complete
1528 * @dev the device struct object
1529 */
1530static void
1531_scsih_get_resync(struct device *dev)
1532{
1533 struct scsi_device *sdev = to_scsi_device(dev);
1534 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1535 static struct _raid_device *raid_device;
1536 unsigned long flags;
1537 Mpi2RaidVolPage0_t vol_pg0;
1538 Mpi2ConfigReply_t mpi_reply;
1539 u32 volume_status_flags;
1540 u8 percent_complete = 0;
1541
1542 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1543 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1544 sdev->channel);
1545 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1546
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301547 if (!raid_device || ioc->is_warpdrive)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301548 goto out;
1549
1550 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1551 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1552 sizeof(Mpi2RaidVolPage0_t))) {
1553 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1554 ioc->name, __FILE__, __LINE__, __func__);
1555 goto out;
1556 }
1557
1558 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1559 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1560 percent_complete = raid_device->percent_complete;
1561 out:
1562 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1563}
1564
1565/**
1566 * _scsih_get_state - get raid volume level
1567 * @dev the device struct object
1568 */
1569static void
1570_scsih_get_state(struct device *dev)
1571{
1572 struct scsi_device *sdev = to_scsi_device(dev);
1573 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1574 static struct _raid_device *raid_device;
1575 unsigned long flags;
1576 Mpi2RaidVolPage0_t vol_pg0;
1577 Mpi2ConfigReply_t mpi_reply;
1578 u32 volstate;
1579 enum raid_state state = RAID_STATE_UNKNOWN;
1580
1581 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1582 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1583 sdev->channel);
1584 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1585
1586 if (!raid_device)
1587 goto out;
1588
1589 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1590 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1591 sizeof(Mpi2RaidVolPage0_t))) {
1592 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1593 ioc->name, __FILE__, __LINE__, __func__);
1594 goto out;
1595 }
1596
1597 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1598 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1599 state = RAID_STATE_RESYNCING;
1600 goto out;
1601 }
1602
1603 switch (vol_pg0.VolumeState) {
1604 case MPI2_RAID_VOL_STATE_OPTIMAL:
1605 case MPI2_RAID_VOL_STATE_ONLINE:
1606 state = RAID_STATE_ACTIVE;
1607 break;
1608 case MPI2_RAID_VOL_STATE_DEGRADED:
1609 state = RAID_STATE_DEGRADED;
1610 break;
1611 case MPI2_RAID_VOL_STATE_FAILED:
1612 case MPI2_RAID_VOL_STATE_MISSING:
1613 state = RAID_STATE_OFFLINE;
1614 break;
1615 }
1616 out:
1617 raid_set_state(mpt2sas_raid_template, dev, state);
1618}
1619
1620/**
1621 * _scsih_set_level - set raid level
1622 * @sdev: scsi device struct
1623 * @raid_device: raid_device object
1624 */
1625static void
1626_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1627{
1628 enum raid_level level = RAID_LEVEL_UNKNOWN;
1629
1630 switch (raid_device->volume_type) {
1631 case MPI2_RAID_VOL_TYPE_RAID0:
1632 level = RAID_LEVEL_0;
1633 break;
1634 case MPI2_RAID_VOL_TYPE_RAID10:
1635 level = RAID_LEVEL_10;
1636 break;
1637 case MPI2_RAID_VOL_TYPE_RAID1E:
1638 level = RAID_LEVEL_1E;
1639 break;
1640 case MPI2_RAID_VOL_TYPE_RAID1:
1641 level = RAID_LEVEL_1;
1642 break;
1643 }
1644
1645 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1646}
1647
1648/**
Eric Moore635374e2009-03-09 01:21:12 -06001649 * _scsih_get_volume_capabilities - volume capabilities
1650 * @ioc: per adapter object
1651 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301652 *
1653 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001654 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301655static int
Eric Moore635374e2009-03-09 01:21:12 -06001656_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1657 struct _raid_device *raid_device)
1658{
1659 Mpi2RaidVolPage0_t *vol_pg0;
1660 Mpi2RaidPhysDiskPage0_t pd_pg0;
1661 Mpi2SasDevicePage0_t sas_device_pg0;
1662 Mpi2ConfigReply_t mpi_reply;
1663 u16 sz;
1664 u8 num_pds;
1665
1666 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1667 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301668 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1669 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1670 __func__));
1671 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001672 }
1673
1674 raid_device->num_pds = num_pds;
1675 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1676 sizeof(Mpi2RaidVol0PhysDisk_t));
1677 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1678 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301679 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1680 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1681 __func__));
1682 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001683 }
1684
1685 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1686 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301687 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1688 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1689 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001690 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301691 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001692 }
1693
1694 raid_device->volume_type = vol_pg0->VolumeType;
1695
1696 /* figure out what the underlying devices are by
1697 * obtaining the device_info bits for the 1st device
1698 */
1699 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1700 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1701 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1702 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1703 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1704 le16_to_cpu(pd_pg0.DevHandle)))) {
1705 raid_device->device_info =
1706 le32_to_cpu(sas_device_pg0.DeviceInfo);
1707 }
1708 }
1709
1710 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301711 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001712}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301713/**
1714 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1715 * @ioc: per adapter object
1716 */
1717static void
1718_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1719{
1720 Mpi2RaidVolPage1_t vol_pg1;
1721 Mpi2ConfigReply_t mpi_reply;
1722 struct _raid_device *raid_device;
1723 u16 handle;
1724 u16 ioc_status;
1725
1726 handle = 0xFFFF;
1727 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1728 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1729 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1730 MPI2_IOCSTATUS_MASK;
1731 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1732 break;
1733 handle = le16_to_cpu(vol_pg1.DevHandle);
1734 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1735 if (raid_device)
1736 raid_device->direct_io_enabled = 0;
1737 }
1738 return;
1739}
1740
1741
1742/**
1743 * _scsih_get_num_volumes - Get number of volumes in the ioc
1744 * @ioc: per adapter object
1745 */
1746static u8
1747_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1748{
1749 Mpi2RaidVolPage1_t vol_pg1;
1750 Mpi2ConfigReply_t mpi_reply;
1751 u16 handle;
1752 u8 vol_cnt = 0;
1753 u16 ioc_status;
1754
1755 handle = 0xFFFF;
1756 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1757 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1758 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1759 MPI2_IOCSTATUS_MASK;
1760 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1761 break;
1762 vol_cnt++;
1763 handle = le16_to_cpu(vol_pg1.DevHandle);
1764 }
1765 return vol_cnt;
1766}
1767
1768
1769/**
1770 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1771 * @ioc: per adapter object
1772 * @raid_device: the raid_device object
1773 */
1774static void
1775_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1776 struct _raid_device *raid_device)
1777{
1778 Mpi2RaidVolPage0_t *vol_pg0;
1779 Mpi2RaidPhysDiskPage0_t pd_pg0;
1780 Mpi2ConfigReply_t mpi_reply;
1781 u16 sz;
1782 u8 num_pds, count;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301783 unsigned long stripe_sz, block_sz;
1784 u8 stripe_exp, block_exp;
1785 u64 dev_max_lba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301786
1787 if (!ioc->is_warpdrive)
1788 return;
1789
1790 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1791 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1792 "globally as drives are exposed\n", ioc->name);
1793 return;
1794 }
1795 if (_scsih_get_num_volumes(ioc) > 1) {
1796 _scsih_disable_ddio(ioc);
1797 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1798 "globally as number of drives > 1\n", ioc->name);
1799 return;
1800 }
1801 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1802 &num_pds)) || !num_pds) {
1803 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1804 "Failure in computing number of drives\n", ioc->name);
1805 return;
1806 }
1807
1808 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1809 sizeof(Mpi2RaidVol0PhysDisk_t));
1810 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1811 if (!vol_pg0) {
1812 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1813 "Memory allocation failure for RVPG0\n", ioc->name);
1814 return;
1815 }
1816
1817 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1818 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1819 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1820 "Failure in retrieving RVPG0\n", ioc->name);
1821 kfree(vol_pg0);
1822 return;
1823 }
1824
1825 /*
1826 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1827 * assumed for WARPDRIVE, disable direct I/O
1828 */
1829 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1830 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1831 "for the drive with handle(0x%04x): num_mem=%d, "
1832 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1833 num_pds, MPT_MAX_WARPDRIVE_PDS);
1834 kfree(vol_pg0);
1835 return;
1836 }
1837 for (count = 0; count < num_pds; count++) {
1838 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1839 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1840 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1841 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1842 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1843 "disabled for the drive with handle(0x%04x) member"
1844 "handle retrieval failed for member number=%d\n",
1845 ioc->name, raid_device->handle,
1846 vol_pg0->PhysDisk[count].PhysDiskNum);
1847 goto out_error;
1848 }
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301849 /* Disable direct I/O if member drive lba exceeds 4 bytes */
1850 dev_max_lba = le64_to_cpu(pd_pg0.DeviceMaxLBA);
1851 if (dev_max_lba >> 32) {
1852 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1853 "disabled for the drive with handle(0x%04x) member"
1854 "handle (0x%04x) unsupported max lba 0x%016llx\n",
1855 ioc->name, raid_device->handle,
1856 le16_to_cpu(pd_pg0.DevHandle),
1857 (unsigned long long)dev_max_lba);
1858 goto out_error;
1859 }
1860
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301861 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1862 }
1863
1864 /*
1865 * Assumption for WD: Direct I/O is not supported if the volume is
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301866 * not RAID0
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301867 */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301868 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301869 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1870 "for the drive with handle(0x%04x): type=%d, "
1871 "s_sz=%uK, blk_size=%u\n", ioc->name,
1872 raid_device->handle, raid_device->volume_type,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301873 (le32_to_cpu(vol_pg0->StripeSize) *
1874 le16_to_cpu(vol_pg0->BlockSize)) / 1024,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301875 le16_to_cpu(vol_pg0->BlockSize));
1876 goto out_error;
1877 }
1878
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301879 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301880 stripe_exp = find_first_bit(&stripe_sz, 32);
1881 if (stripe_exp == 32) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301882 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301883 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301884 ioc->name, raid_device->handle,
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301885 (le32_to_cpu(vol_pg0->StripeSize) *
1886 le16_to_cpu(vol_pg0->BlockSize)) / 1024);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301887 goto out_error;
1888 }
1889 raid_device->stripe_exponent = stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05301890 block_sz = le16_to_cpu(vol_pg0->BlockSize);
1891 block_exp = find_first_bit(&block_sz, 16);
1892 if (block_exp == 16) {
1893 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1894 "for the drive with handle(0x%04x) invalid block sz %u\n",
1895 ioc->name, raid_device->handle,
1896 le16_to_cpu(vol_pg0->BlockSize));
1897 goto out_error;
1898 }
1899 raid_device->block_exponent = block_exp;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301900 raid_device->direct_io_enabled = 1;
1901
1902 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1903 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1904 /*
1905 * WARPDRIVE: Though the following fields are not used for direct IO,
1906 * stored for future purpose:
1907 */
1908 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1909 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1910 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1911
1912
1913 kfree(vol_pg0);
1914 return;
1915
1916out_error:
1917 raid_device->direct_io_enabled = 0;
1918 for (count = 0; count < num_pds; count++)
1919 raid_device->pd_handle[count] = 0;
1920 kfree(vol_pg0);
1921 return;
1922}
Eric Moore635374e2009-03-09 01:21:12 -06001923
1924/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301925 * _scsih_enable_tlr - setting TLR flags
1926 * @ioc: per adapter object
1927 * @sdev: scsi device struct
1928 *
1929 * Enabling Transaction Layer Retries for tape devices when
1930 * vpd page 0x90 is present
1931 *
1932 */
1933static void
1934_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1935{
1936 /* only for TAPE */
1937 if (sdev->type != TYPE_TAPE)
1938 return;
1939
1940 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1941 return;
1942
1943 sas_enable_tlr(sdev);
1944 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1945 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1946 return;
1947
1948}
1949
1950/**
Eric Moored5d135b2009-05-18 13:02:08 -06001951 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001952 * @sdev: scsi device struct
1953 *
1954 * Returns 0 if ok. Any other return is assumed to be an error and
1955 * the device is ignored.
1956 */
1957static int
Eric Moored5d135b2009-05-18 13:02:08 -06001958_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001959{
1960 struct Scsi_Host *shost = sdev->host;
1961 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1962 struct MPT2SAS_DEVICE *sas_device_priv_data;
1963 struct MPT2SAS_TARGET *sas_target_priv_data;
1964 struct _sas_device *sas_device;
1965 struct _raid_device *raid_device;
1966 unsigned long flags;
1967 int qdepth;
1968 u8 ssp_target = 0;
1969 char *ds = "";
1970 char *r_level = "";
1971
1972 qdepth = 1;
1973 sas_device_priv_data = sdev->hostdata;
1974 sas_device_priv_data->configured_lun = 1;
1975 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1976 sas_target_priv_data = sas_device_priv_data->sas_target;
1977
1978 /* raid volume handling */
1979 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1980
1981 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1982 raid_device = _scsih_raid_device_find_by_handle(ioc,
1983 sas_target_priv_data->handle);
1984 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1985 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301986 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1987 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1988 __LINE__, __func__));
1989 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001990 }
1991
1992 _scsih_get_volume_capabilities(ioc, raid_device);
1993
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301994 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1995 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1996 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1997 __LINE__, __func__));
1998 return 1;
1999 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302000 /*
2001 * WARPDRIVE: Initialize the required data for Direct IO
2002 */
2003 _scsih_init_warpdrive_properties(ioc, raid_device);
2004
Eric Moore635374e2009-03-09 01:21:12 -06002005 /* RAID Queue Depth Support
2006 * IS volume = underlying qdepth of drive type, either
2007 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2008 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2009 */
2010 if (raid_device->device_info &
2011 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2012 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2013 ds = "SSP";
2014 } else {
2015 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2016 if (raid_device->device_info &
2017 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2018 ds = "SATA";
2019 else
2020 ds = "STP";
2021 }
2022
2023 switch (raid_device->volume_type) {
2024 case MPI2_RAID_VOL_TYPE_RAID0:
2025 r_level = "RAID0";
2026 break;
2027 case MPI2_RAID_VOL_TYPE_RAID1E:
2028 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302029 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302030 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302031 MFG10_GF0_R10_DISPLAY) &&
2032 !(raid_device->num_pds % 2))
2033 r_level = "RAID10";
2034 else
2035 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002036 break;
2037 case MPI2_RAID_VOL_TYPE_RAID1:
2038 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2039 r_level = "RAID1";
2040 break;
2041 case MPI2_RAID_VOL_TYPE_RAID10:
2042 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2043 r_level = "RAID10";
2044 break;
2045 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2046 default:
2047 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2048 r_level = "RAIDX";
2049 break;
2050 }
2051
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302052 if (!ioc->hide_ir_msg)
2053 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2054 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2055 r_level, raid_device->handle,
2056 (unsigned long long)raid_device->wwid,
2057 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002058 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302059 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302060 if (!ioc->is_warpdrive)
2061 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06002062 return 0;
2063 }
2064
2065 /* non-raid handling */
2066 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2067 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2068 sas_device_priv_data->sas_target->sas_address);
2069 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2070 if (sas_device) {
2071 if (sas_target_priv_data->flags &
2072 MPT_TARGET_FLAGS_RAID_COMPONENT) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302073 if (mpt2sas_config_get_volume_handle(ioc,
2074 sas_device->handle, &sas_device->volume_handle)) {
2075 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2076 "failure at %s:%d/%s()!\n", ioc->name,
2077 __FILE__, __LINE__, __func__));
2078 return 1;
2079 }
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05302080 if (sas_device->volume_handle &&
2081 mpt2sas_config_get_volume_wwid(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06002082 sas_device->volume_handle,
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302083 &sas_device->volume_wwid)) {
2084 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2085 "failure at %s:%d/%s()!\n", ioc->name,
2086 __FILE__, __LINE__, __func__));
2087 return 1;
2088 }
Eric Moore635374e2009-03-09 01:21:12 -06002089 }
2090 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2091 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2092 ssp_target = 1;
2093 ds = "SSP";
2094 } else {
2095 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2096 if (sas_device->device_info &
2097 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2098 ds = "STP";
2099 else if (sas_device->device_info &
2100 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2101 ds = "SATA";
2102 }
2103
2104 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302105 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002106 ds, sas_device->handle,
2107 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302108 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06002109 (unsigned long long)sas_device->device_name);
2110 sdev_printk(KERN_INFO, sdev, "%s: "
2111 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2112 (unsigned long long) sas_device->enclosure_logical_id,
2113 sas_device->slot);
2114
2115 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06002116 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302117 } else {
2118 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2119 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
2120 __func__));
2121 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002122 }
2123
Mike Christiee881a172009-10-15 17:46:39 -07002124 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002125
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302126 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002127 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302128 _scsih_enable_tlr(ioc, sdev);
2129 }
Eric Moore635374e2009-03-09 01:21:12 -06002130 return 0;
2131}
2132
2133/**
Eric Moored5d135b2009-05-18 13:02:08 -06002134 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002135 * @sdev: scsi device struct
2136 * @bdev: pointer to block device context
2137 * @capacity: device size (in 512 byte sectors)
2138 * @params: three element array to place output:
2139 * params[0] number of heads (max 255)
2140 * params[1] number of sectors (max 63)
2141 * params[2] number of cylinders
2142 *
2143 * Return nothing.
2144 */
2145static int
Eric Moored5d135b2009-05-18 13:02:08 -06002146_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002147 sector_t capacity, int params[])
2148{
2149 int heads;
2150 int sectors;
2151 sector_t cylinders;
2152 ulong dummy;
2153
2154 heads = 64;
2155 sectors = 32;
2156
2157 dummy = heads * sectors;
2158 cylinders = capacity;
2159 sector_div(cylinders, dummy);
2160
2161 /*
2162 * Handle extended translation size for logical drives
2163 * > 1Gb
2164 */
2165 if ((ulong)capacity >= 0x200000) {
2166 heads = 255;
2167 sectors = 63;
2168 dummy = heads * sectors;
2169 cylinders = capacity;
2170 sector_div(cylinders, dummy);
2171 }
2172
2173 /* return result */
2174 params[0] = heads;
2175 params[1] = sectors;
2176 params[2] = cylinders;
2177
2178 return 0;
2179}
2180
2181/**
2182 * _scsih_response_code - translation of device response code
2183 * @ioc: per adapter object
2184 * @response_code: response code returned by the device
2185 *
2186 * Return nothing.
2187 */
2188static void
2189_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2190{
2191 char *desc;
2192
2193 switch (response_code) {
2194 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2195 desc = "task management request completed";
2196 break;
2197 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2198 desc = "invalid frame";
2199 break;
2200 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2201 desc = "task management request not supported";
2202 break;
2203 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2204 desc = "task management request failed";
2205 break;
2206 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2207 desc = "task management request succeeded";
2208 break;
2209 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2210 desc = "invalid lun";
2211 break;
2212 case 0xA:
2213 desc = "overlapped tag attempted";
2214 break;
2215 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2216 desc = "task queued, however not sent to target";
2217 break;
2218 default:
2219 desc = "unknown";
2220 break;
2221 }
2222 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2223 ioc->name, response_code, desc);
2224}
2225
2226/**
Eric Moored5d135b2009-05-18 13:02:08 -06002227 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002228 * @ioc: per adapter object
2229 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302230 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002231 * @reply: reply message frame(lower 32bit addr)
2232 * Context: none.
2233 *
2234 * The callback handler when using scsih_issue_tm.
2235 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302236 * Return 1 meaning mf should be freed from _base_interrupt
2237 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002238 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302239static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302240_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002241{
2242 MPI2DefaultReply_t *mpi_reply;
2243
2244 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302245 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002246 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302247 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302248 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002249 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2250 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2251 if (mpi_reply) {
2252 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2253 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2254 }
2255 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2256 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302257 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002258}
2259
2260/**
2261 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2262 * @ioc: per adapter object
2263 * @handle: device handle
2264 *
2265 * During taskmangement request, we need to freeze the device queue.
2266 */
2267void
2268mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2269{
2270 struct MPT2SAS_DEVICE *sas_device_priv_data;
2271 struct scsi_device *sdev;
2272 u8 skip = 0;
2273
2274 shost_for_each_device(sdev, ioc->shost) {
2275 if (skip)
2276 continue;
2277 sas_device_priv_data = sdev->hostdata;
2278 if (!sas_device_priv_data)
2279 continue;
2280 if (sas_device_priv_data->sas_target->handle == handle) {
2281 sas_device_priv_data->sas_target->tm_busy = 1;
2282 skip = 1;
2283 ioc->ignore_loginfos = 1;
2284 }
2285 }
2286}
2287
2288/**
2289 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2290 * @ioc: per adapter object
2291 * @handle: device handle
2292 *
2293 * During taskmangement request, we need to freeze the device queue.
2294 */
2295void
2296mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2297{
2298 struct MPT2SAS_DEVICE *sas_device_priv_data;
2299 struct scsi_device *sdev;
2300 u8 skip = 0;
2301
2302 shost_for_each_device(sdev, ioc->shost) {
2303 if (skip)
2304 continue;
2305 sas_device_priv_data = sdev->hostdata;
2306 if (!sas_device_priv_data)
2307 continue;
2308 if (sas_device_priv_data->sas_target->handle == handle) {
2309 sas_device_priv_data->sas_target->tm_busy = 0;
2310 skip = 1;
2311 ioc->ignore_loginfos = 0;
2312 }
2313 }
2314}
2315
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302316
Eric Moore635374e2009-03-09 01:21:12 -06002317/**
2318 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2319 * @ioc: per adapter struct
2320 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302321 * @channel: the channel assigned by the OS
2322 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002323 * @lun: lun number
2324 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2325 * @smid_task: smid assigned to the task
2326 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302327 * @serial_number: the serial_number from scmd
2328 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302329 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002330 *
2331 * A generic API for sending task management requests to firmware.
2332 *
Eric Moore635374e2009-03-09 01:21:12 -06002333 * The callback index is set inside `ioc->tm_cb_idx`.
2334 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302335 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002336 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302337int
2338mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2339 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302340 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002341{
2342 Mpi2SCSITaskManagementRequest_t *mpi_request;
2343 Mpi2SCSITaskManagementReply_t *mpi_reply;
2344 u16 smid = 0;
2345 u32 ioc_state;
2346 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302347 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302348 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002349
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302350 if (m_type == TM_MUTEX_ON)
2351 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302352 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2353 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2354 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302355 rc = FAILED;
2356 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302357 }
2358
Eric Moore3cb54692010-07-08 14:44:34 -06002359 if (ioc->shost_recovery || ioc->remove_host ||
2360 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002361 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2362 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302363 rc = FAILED;
2364 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002365 }
Eric Moore635374e2009-03-09 01:21:12 -06002366
2367 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2368 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302369 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002370 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302371 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302372 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302373 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302374 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002375 }
2376
2377 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2378 mpt2sas_base_fault_info(ioc, ioc_state &
2379 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302380 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302381 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302382 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302383 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002384 }
2385
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302386 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002387 if (!smid) {
2388 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2389 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302390 rc = FAILED;
2391 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002392 }
2393
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302394 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2395 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2396
Eric Moore635374e2009-03-09 01:21:12 -06002397 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302398 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2399 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002400 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2401 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2402 ioc->tm_cmds.smid = smid;
2403 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302404 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002405 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2406 mpi_request->DevHandle = cpu_to_le16(handle);
2407 mpi_request->TaskType = type;
2408 mpi_request->TaskMID = cpu_to_le16(smid_task);
2409 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2410 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302411 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302412 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002413 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002414 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2415 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2416 ioc->name, __func__);
2417 _debug_dump_mf(mpi_request,
2418 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302419 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302420 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302421 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302422 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302423 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2424 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2425 goto err_out;
2426 }
Eric Moore635374e2009-03-09 01:21:12 -06002427 }
2428
2429 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2430 mpi_reply = ioc->tm_cmds.reply;
2431 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2432 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2433 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2434 le32_to_cpu(mpi_reply->IOCLogInfo),
2435 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302436 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002437 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302438 if (mpi_reply->IOCStatus)
2439 _debug_dump_mf(mpi_request,
2440 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2441 }
Eric Moore635374e2009-03-09 01:21:12 -06002442 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302443
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302444 switch (type) {
2445 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302446 rc = SUCCESS;
2447 if (scsi_lookup->scmd == NULL)
2448 break;
2449 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302450 break;
2451
2452 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2453 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2454 rc = FAILED;
2455 else
2456 rc = SUCCESS;
2457 break;
2458
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302459 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302460 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2461 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2462 rc = FAILED;
2463 else
2464 rc = SUCCESS;
2465 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302466 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2467 rc = SUCCESS;
2468 break;
2469 default:
2470 rc = FAILED;
2471 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302472 }
2473
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302474 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2475 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302476 if (m_type == TM_MUTEX_ON)
2477 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302478
2479 return rc;
2480
2481 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302482 if (m_type == TM_MUTEX_ON)
2483 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302484 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002485}
2486
2487/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302488 * _scsih_tm_display_info - displays info about the device
2489 * @ioc: per adapter struct
2490 * @scmd: pointer to scsi command object
2491 *
2492 * Called by task management callback handlers.
2493 */
2494static void
2495_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2496{
2497 struct scsi_target *starget = scmd->device->sdev_target;
2498 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2499 struct _sas_device *sas_device = NULL;
2500 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302501 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302502
2503 if (!priv_target)
2504 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302505 if (ioc->hide_ir_msg)
2506 device_str = "WarpDrive";
2507 else
2508 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302509
2510 scsi_print_command(scmd);
2511 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302512 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2513 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2514 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302515 } else {
2516 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2517 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2518 priv_target->sas_address);
2519 if (sas_device) {
2520 if (priv_target->flags &
2521 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2522 starget_printk(KERN_INFO, starget,
2523 "volume handle(0x%04x), "
2524 "volume wwid(0x%016llx)\n",
2525 sas_device->volume_handle,
2526 (unsigned long long)sas_device->volume_wwid);
2527 }
2528 starget_printk(KERN_INFO, starget,
2529 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2530 sas_device->handle,
2531 (unsigned long long)sas_device->sas_address,
2532 sas_device->phy);
2533 starget_printk(KERN_INFO, starget,
2534 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2535 (unsigned long long)sas_device->enclosure_logical_id,
2536 sas_device->slot);
2537 }
2538 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2539 }
2540}
2541
2542/**
Eric Moored5d135b2009-05-18 13:02:08 -06002543 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302544 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002545 *
2546 * Returns SUCCESS if command aborted else FAILED
2547 */
2548static int
Eric Moored5d135b2009-05-18 13:02:08 -06002549_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002550{
2551 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2552 struct MPT2SAS_DEVICE *sas_device_priv_data;
2553 u16 smid;
2554 u16 handle;
2555 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002556
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302557 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2558 "scmd(%p)\n", scmd);
2559 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002560
2561 sas_device_priv_data = scmd->device->hostdata;
2562 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302563 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2564 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002565 scmd->result = DID_NO_CONNECT << 16;
2566 scmd->scsi_done(scmd);
2567 r = SUCCESS;
2568 goto out;
2569 }
2570
2571 /* search for the command */
2572 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2573 if (!smid) {
2574 scmd->result = DID_RESET << 16;
2575 r = SUCCESS;
2576 goto out;
2577 }
2578
2579 /* for hidden raid components and volumes this is not supported */
2580 if (sas_device_priv_data->sas_target->flags &
2581 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2582 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2583 scmd->result = DID_RESET << 16;
2584 r = FAILED;
2585 goto out;
2586 }
2587
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302588 mpt2sas_halt_firmware(ioc);
2589
Eric Moore635374e2009-03-09 01:21:12 -06002590 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302591 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2592 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302593 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2594 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002595
2596 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302597 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2598 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002599 return r;
2600}
2601
Eric Moore635374e2009-03-09 01:21:12 -06002602/**
Eric Moored5d135b2009-05-18 13:02:08 -06002603 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302604 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002605 *
2606 * Returns SUCCESS if command aborted else FAILED
2607 */
2608static int
Eric Moored5d135b2009-05-18 13:02:08 -06002609_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002610{
2611 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2612 struct MPT2SAS_DEVICE *sas_device_priv_data;
2613 struct _sas_device *sas_device;
2614 unsigned long flags;
2615 u16 handle;
2616 int r;
2617
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302618 struct scsi_target *starget = scmd->device->sdev_target;
2619
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302620 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302621 "scmd(%p)\n", scmd);
2622 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002623
2624 sas_device_priv_data = scmd->device->hostdata;
2625 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302626 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302627 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002628 scmd->result = DID_NO_CONNECT << 16;
2629 scmd->scsi_done(scmd);
2630 r = SUCCESS;
2631 goto out;
2632 }
2633
2634 /* for hidden raid components obtain the volume_handle */
2635 handle = 0;
2636 if (sas_device_priv_data->sas_target->flags &
2637 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2638 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2639 sas_device = _scsih_sas_device_find_by_handle(ioc,
2640 sas_device_priv_data->sas_target->handle);
2641 if (sas_device)
2642 handle = sas_device->volume_handle;
2643 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2644 } else
2645 handle = sas_device_priv_data->sas_target->handle;
2646
2647 if (!handle) {
2648 scmd->result = DID_RESET << 16;
2649 r = FAILED;
2650 goto out;
2651 }
2652
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302653 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2654 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302655 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2656 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002657
2658 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302659 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2660 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002661 return r;
2662}
2663
2664/**
Eric Moored5d135b2009-05-18 13:02:08 -06002665 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302666 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002667 *
2668 * Returns SUCCESS if command aborted else FAILED
2669 */
2670static int
Eric Moored5d135b2009-05-18 13:02:08 -06002671_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002672{
2673 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2674 struct MPT2SAS_DEVICE *sas_device_priv_data;
2675 struct _sas_device *sas_device;
2676 unsigned long flags;
2677 u16 handle;
2678 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302679 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002680
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302681 starget_printk(KERN_INFO, starget, "attempting target reset! "
2682 "scmd(%p)\n", scmd);
2683 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002684
2685 sas_device_priv_data = scmd->device->hostdata;
2686 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302687 starget_printk(KERN_INFO, starget, "target been deleted! "
2688 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002689 scmd->result = DID_NO_CONNECT << 16;
2690 scmd->scsi_done(scmd);
2691 r = SUCCESS;
2692 goto out;
2693 }
2694
2695 /* for hidden raid components obtain the volume_handle */
2696 handle = 0;
2697 if (sas_device_priv_data->sas_target->flags &
2698 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2699 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2700 sas_device = _scsih_sas_device_find_by_handle(ioc,
2701 sas_device_priv_data->sas_target->handle);
2702 if (sas_device)
2703 handle = sas_device->volume_handle;
2704 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2705 } else
2706 handle = sas_device_priv_data->sas_target->handle;
2707
2708 if (!handle) {
2709 scmd->result = DID_RESET << 16;
2710 r = FAILED;
2711 goto out;
2712 }
2713
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302714 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2715 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302716 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002717
2718 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302719 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2720 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002721 return r;
2722}
2723
2724/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302725 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302726 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002727 *
2728 * Returns SUCCESS if command aborted else FAILED
2729 */
2730static int
Eric Moored5d135b2009-05-18 13:02:08 -06002731_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002732{
2733 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2734 int r, retval;
2735
2736 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2737 ioc->name, scmd);
2738 scsi_print_command(scmd);
2739
2740 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2741 FORCE_BIG_HAMMER);
2742 r = (retval < 0) ? FAILED : SUCCESS;
2743 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2744 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2745
2746 return r;
2747}
2748
2749/**
2750 * _scsih_fw_event_add - insert and queue up fw_event
2751 * @ioc: per adapter object
2752 * @fw_event: object describing the event
2753 * Context: This function will acquire ioc->fw_event_lock.
2754 *
2755 * This adds the firmware event object into link list, then queues it up to
2756 * be processed from user context.
2757 *
2758 * Return nothing.
2759 */
2760static void
2761_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2762{
2763 unsigned long flags;
2764
2765 if (ioc->firmware_event_thread == NULL)
2766 return;
2767
2768 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2769 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302770 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2771 queue_delayed_work(ioc->firmware_event_thread,
2772 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002773 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2774}
2775
2776/**
2777 * _scsih_fw_event_free - delete fw_event
2778 * @ioc: per adapter object
2779 * @fw_event: object describing the event
2780 * Context: This function will acquire ioc->fw_event_lock.
2781 *
2782 * This removes firmware event object from link list, frees associated memory.
2783 *
2784 * Return nothing.
2785 */
2786static void
2787_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2788 *fw_event)
2789{
2790 unsigned long flags;
2791
2792 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2793 list_del(&fw_event->list);
2794 kfree(fw_event->event_data);
2795 kfree(fw_event);
2796 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2797}
2798
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302799
Eric Moore635374e2009-03-09 01:21:12 -06002800/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302801 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002802 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002803 *
2804 * Return nothing.
2805 */
2806static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302807_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002808{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302809 struct fw_event_work *fw_event;
2810
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302811 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302812 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002813
2814 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2815 if (!fw_event)
2816 return;
2817
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302818 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2819 fw_event->ioc = ioc;
2820 _scsih_fw_event_add(ioc, fw_event);
2821}
2822
2823/**
2824 * mpt2sas_port_enable_complete - port enable completed (fake event)
2825 * @ioc: per adapter object
2826 *
2827 * Return nothing.
2828 */
2829void
2830mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2831{
2832 struct fw_event_work *fw_event;
2833
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302834 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2835 if (!fw_event)
2836 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302837 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302838 fw_event->ioc = ioc;
2839 _scsih_fw_event_add(ioc, fw_event);
2840}
2841
2842/**
2843 * _scsih_fw_event_cleanup_queue - cleanup event queue
2844 * @ioc: per adapter object
2845 *
2846 * Walk the firmware event queue, either killing timers, or waiting
2847 * for outstanding events to complete
2848 *
2849 * Return nothing.
2850 */
2851static void
2852_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2853{
2854 struct fw_event_work *fw_event, *next;
2855
2856 if (list_empty(&ioc->fw_event_list) ||
2857 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002858 return;
2859
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302860 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2861 if (cancel_delayed_work(&fw_event->delayed_work)) {
2862 _scsih_fw_event_free(ioc, fw_event);
2863 continue;
2864 }
2865 fw_event->cancel_pending_work = 1;
2866 }
Eric Moore635374e2009-03-09 01:21:12 -06002867}
2868
Eric Moore635374e2009-03-09 01:21:12 -06002869/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302870 * _scsih_ublock_io_all_device - unblock every device
2871 * @ioc: per adapter object
2872 *
2873 * change the device state from block to running
2874 */
2875static void
2876_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2877{
2878 struct MPT2SAS_DEVICE *sas_device_priv_data;
2879 struct scsi_device *sdev;
2880
2881 shost_for_each_device(sdev, ioc->shost) {
2882 sas_device_priv_data = sdev->hostdata;
2883 if (!sas_device_priv_data)
2884 continue;
2885 if (!sas_device_priv_data->block)
2886 continue;
2887 sas_device_priv_data->block = 0;
2888 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2889 "handle(0x%04x)\n",
2890 sas_device_priv_data->sas_target->handle));
2891 scsi_internal_device_unblock(sdev);
2892 }
2893}
2894/**
Eric Moore635374e2009-03-09 01:21:12 -06002895 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2896 * @ioc: per adapter object
2897 * @handle: device handle
2898 *
2899 * During device pull we need to appropiately set the sdev state.
2900 */
2901static void
2902_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2903{
2904 struct MPT2SAS_DEVICE *sas_device_priv_data;
2905 struct scsi_device *sdev;
2906
2907 shost_for_each_device(sdev, ioc->shost) {
2908 sas_device_priv_data = sdev->hostdata;
2909 if (!sas_device_priv_data)
2910 continue;
2911 if (!sas_device_priv_data->block)
2912 continue;
2913 if (sas_device_priv_data->sas_target->handle == handle) {
2914 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2915 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2916 "handle(0x%04x)\n", ioc->name, handle));
2917 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302918 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002919 }
2920 }
2921}
2922
2923/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302924 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2925 * @ioc: per adapter object
2926 * @handle: device handle
2927 *
2928 * During device pull we need to appropiately set the sdev state.
2929 */
2930static void
2931_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2932{
2933 struct MPT2SAS_DEVICE *sas_device_priv_data;
2934 struct scsi_device *sdev;
2935
2936 shost_for_each_device(sdev, ioc->shost) {
2937 sas_device_priv_data = sdev->hostdata;
2938 if (!sas_device_priv_data)
2939 continue;
2940 if (sas_device_priv_data->block)
2941 continue;
2942 sas_device_priv_data->block = 1;
2943 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2944 "handle(0x%04x)\n",
2945 sas_device_priv_data->sas_target->handle));
2946 scsi_internal_device_block(sdev);
2947 }
2948}
2949
2950
2951/**
Eric Moore635374e2009-03-09 01:21:12 -06002952 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2953 * @ioc: per adapter object
2954 * @handle: device handle
2955 *
2956 * During device pull we need to appropiately set the sdev state.
2957 */
2958static void
2959_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2960{
2961 struct MPT2SAS_DEVICE *sas_device_priv_data;
2962 struct scsi_device *sdev;
2963
2964 shost_for_each_device(sdev, ioc->shost) {
2965 sas_device_priv_data = sdev->hostdata;
2966 if (!sas_device_priv_data)
2967 continue;
2968 if (sas_device_priv_data->block)
2969 continue;
2970 if (sas_device_priv_data->sas_target->handle == handle) {
2971 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2972 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2973 "handle(0x%04x)\n", ioc->name, handle));
2974 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302975 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002976 }
2977 }
2978}
2979
2980/**
2981 * _scsih_block_io_to_children_attached_to_ex
2982 * @ioc: per adapter object
2983 * @sas_expander: the sas_device object
2984 *
2985 * This routine set sdev state to SDEV_BLOCK for all devices
2986 * attached to this expander. This function called when expander is
2987 * pulled.
2988 */
2989static void
2990_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2991 struct _sas_node *sas_expander)
2992{
2993 struct _sas_port *mpt2sas_port;
2994 struct _sas_device *sas_device;
2995 struct _sas_node *expander_sibling;
2996 unsigned long flags;
2997
2998 if (!sas_expander)
2999 return;
3000
3001 list_for_each_entry(mpt2sas_port,
3002 &sas_expander->sas_port_list, port_list) {
3003 if (mpt2sas_port->remote_identify.device_type ==
3004 SAS_END_DEVICE) {
3005 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3006 sas_device =
3007 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3008 mpt2sas_port->remote_identify.sas_address);
3009 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3010 if (!sas_device)
3011 continue;
3012 _scsih_block_io_device(ioc, sas_device->handle);
3013 }
3014 }
3015
3016 list_for_each_entry(mpt2sas_port,
3017 &sas_expander->sas_port_list, port_list) {
3018
3019 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303020 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003021 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303022 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003023
3024 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3025 expander_sibling =
3026 mpt2sas_scsih_expander_find_by_sas_address(
3027 ioc, mpt2sas_port->remote_identify.sas_address);
3028 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3029 _scsih_block_io_to_children_attached_to_ex(ioc,
3030 expander_sibling);
3031 }
3032 }
3033}
3034
3035/**
3036 * _scsih_block_io_to_children_attached_directly
3037 * @ioc: per adapter object
3038 * @event_data: topology change event data
3039 *
3040 * This routine set sdev state to SDEV_BLOCK for all devices
3041 * direct attached during device pull.
3042 */
3043static void
3044_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3045 Mpi2EventDataSasTopologyChangeList_t *event_data)
3046{
3047 int i;
3048 u16 handle;
3049 u16 reason_code;
3050 u8 phy_number;
3051
3052 for (i = 0; i < event_data->NumEntries; i++) {
3053 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3054 if (!handle)
3055 continue;
3056 phy_number = event_data->StartPhyNum + i;
3057 reason_code = event_data->PHY[i].PhyStatus &
3058 MPI2_EVENT_SAS_TOPO_RC_MASK;
3059 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3060 _scsih_block_io_device(ioc, handle);
3061 }
3062}
3063
3064/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303065 * _scsih_tm_tr_send - send task management request
3066 * @ioc: per adapter object
3067 * @handle: device handle
3068 * Context: interrupt time.
3069 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003070 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303071 * with controller firmware. This function will issue target reset
3072 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003073 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303074 *
3075 * This is designed to send muliple task management request at the same
3076 * time to the fifo. If the fifo is full, we will append the request,
3077 * and process it in a future completion.
3078 */
3079static void
3080_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3081{
3082 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303083 u16 smid;
3084 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303085 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3086 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303087 unsigned long flags;
3088 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303089 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303090
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303091 if (ioc->remove_host) {
3092 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3093 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3094 return;
3095 } else if (ioc->pci_error_recovery) {
3096 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3097 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3098 handle));
3099 return;
3100 }
3101 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3102 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3103 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3104 "operational: handle(0x%04x)\n", __func__, ioc->name,
3105 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303106 return;
3107 }
3108
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303109 /* if PD, then return */
3110 if (test_bit(handle, ioc->pd_handles))
3111 return;
3112
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303113 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3114 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303115 if (sas_device && sas_device->starget &&
3116 sas_device->starget->hostdata) {
3117 sas_target_priv_data = sas_device->starget->hostdata;
3118 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303119 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303120 }
3121 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303122
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303123 if (sas_target_priv_data) {
3124 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3125 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3126 (unsigned long long)sas_address));
3127 _scsih_ublock_io_device(ioc, handle);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303128 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303129 }
3130
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303131 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3132 if (!smid) {
3133 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3134 if (!delayed_tr)
3135 return;
3136 INIT_LIST_HEAD(&delayed_tr->list);
3137 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303138 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3139 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3140 "DELAYED:tr:handle(0x%04x), (open)\n",
3141 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303142 return;
3143 }
3144
Kashyap, Desai1278b112010-03-09 17:34:13 +05303145 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3146 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3147 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303148 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3149 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3150 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3151 mpi_request->DevHandle = cpu_to_le16(handle);
3152 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303153 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3154}
3155
3156
3157
3158/**
3159 * _scsih_sas_control_complete - completion routine
3160 * @ioc: per adapter object
3161 * @smid: system request message index
3162 * @msix_index: MSIX table index supplied by the OS
3163 * @reply: reply message frame(lower 32bit addr)
3164 * Context: interrupt time.
3165 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003166 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303167 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003168 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303169 *
3170 * Return 1 meaning mf should be freed from _base_interrupt
3171 * 0 means the mf is freed from this function.
3172 */
3173static u8
3174_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3175 u8 msix_index, u32 reply)
3176{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303177#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303178 Mpi2SasIoUnitControlReply_t *mpi_reply =
3179 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05303180#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05303181 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3182 "sc_complete:handle(0x%04x), (open) "
3183 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3184 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3185 le16_to_cpu(mpi_reply->IOCStatus),
3186 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303187 return 1;
3188}
3189
3190/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303191 * _scsih_tm_tr_volume_send - send target reset request for volumes
3192 * @ioc: per adapter object
3193 * @handle: device handle
3194 * Context: interrupt time.
3195 *
3196 * This is designed to send muliple task management request at the same
3197 * time to the fifo. If the fifo is full, we will append the request,
3198 * and process it in a future completion.
3199 */
3200static void
3201_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3202{
3203 Mpi2SCSITaskManagementRequest_t *mpi_request;
3204 u16 smid;
3205 struct _tr_list *delayed_tr;
3206
Eric Moore3cb54692010-07-08 14:44:34 -06003207 if (ioc->shost_recovery || ioc->remove_host ||
3208 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303209 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3210 "progress!\n", __func__, ioc->name));
3211 return;
3212 }
3213
3214 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3215 if (!smid) {
3216 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3217 if (!delayed_tr)
3218 return;
3219 INIT_LIST_HEAD(&delayed_tr->list);
3220 delayed_tr->handle = handle;
3221 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3222 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3223 "DELAYED:tr:handle(0x%04x), (open)\n",
3224 ioc->name, handle));
3225 return;
3226 }
3227
3228 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3229 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3230 ioc->tm_tr_volume_cb_idx));
3231 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3232 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3233 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3234 mpi_request->DevHandle = cpu_to_le16(handle);
3235 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3236 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3237}
3238
3239/**
3240 * _scsih_tm_volume_tr_complete - target reset completion
3241 * @ioc: per adapter object
3242 * @smid: system request message index
3243 * @msix_index: MSIX table index supplied by the OS
3244 * @reply: reply message frame(lower 32bit addr)
3245 * Context: interrupt time.
3246 *
3247 * Return 1 meaning mf should be freed from _base_interrupt
3248 * 0 means the mf is freed from this function.
3249 */
3250static u8
3251_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3252 u8 msix_index, u32 reply)
3253{
3254 u16 handle;
3255 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3256 Mpi2SCSITaskManagementReply_t *mpi_reply =
3257 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3258
Eric Moore3cb54692010-07-08 14:44:34 -06003259 if (ioc->shost_recovery || ioc->remove_host ||
3260 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303261 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3262 "progress!\n", __func__, ioc->name));
3263 return 1;
3264 }
3265
3266 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3267 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3268 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3269 dewtprintk(ioc, printk("spurious interrupt: "
3270 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3271 le16_to_cpu(mpi_reply->DevHandle), smid));
3272 return 0;
3273 }
3274
3275 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3276 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3277 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3278 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3279 le32_to_cpu(mpi_reply->IOCLogInfo),
3280 le32_to_cpu(mpi_reply->TerminationCount)));
3281
3282 return _scsih_check_for_pending_tm(ioc, smid);
3283}
3284
3285/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303286 * _scsih_tm_tr_complete -
3287 * @ioc: per adapter object
3288 * @smid: system request message index
3289 * @msix_index: MSIX table index supplied by the OS
3290 * @reply: reply message frame(lower 32bit addr)
3291 * Context: interrupt time.
3292 *
3293 * This is the target reset completion routine.
3294 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003295 * handshake protocol with controller firmware.
3296 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303297 *
3298 * Return 1 meaning mf should be freed from _base_interrupt
3299 * 0 means the mf is freed from this function.
3300 */
3301static u8
3302_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3303 u32 reply)
3304{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303305 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303306 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303307 Mpi2SCSITaskManagementReply_t *mpi_reply =
3308 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3309 Mpi2SasIoUnitControlRequest_t *mpi_request;
3310 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303311 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303312
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303313 if (ioc->remove_host) {
3314 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3315 "removed\n", __func__, ioc->name));
3316 return 1;
3317 } else if (ioc->pci_error_recovery) {
3318 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3319 "error recovery\n", __func__, ioc->name));
3320 return 1;
3321 }
3322 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3323 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3324 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3325 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303326 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303327 }
3328
Kashyap, Desai1278b112010-03-09 17:34:13 +05303329 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3330 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3331 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3332 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3333 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3334 le16_to_cpu(mpi_reply->DevHandle), smid));
3335 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303336 }
3337
Kashyap, Desai1278b112010-03-09 17:34:13 +05303338 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3339 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3340 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3341 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3342 le32_to_cpu(mpi_reply->IOCLogInfo),
3343 le32_to_cpu(mpi_reply->TerminationCount)));
3344
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303345 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3346 if (!smid_sas_ctrl) {
3347 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3348 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303349 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303350 }
3351
Kashyap, Desai1278b112010-03-09 17:34:13 +05303352 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3353 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3354 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303355 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3356 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3357 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3358 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303359 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303360 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303361
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303362 return _scsih_check_for_pending_tm(ioc, smid);
3363}
3364
3365/**
3366 * _scsih_check_for_pending_tm - check for pending task management
3367 * @ioc: per adapter object
3368 * @smid: system request message index
3369 *
3370 * This will check delayed target reset list, and feed the
3371 * next reqeust.
3372 *
3373 * Return 1 meaning mf should be freed from _base_interrupt
3374 * 0 means the mf is freed from this function.
3375 */
3376static u8
3377_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3378{
3379 struct _tr_list *delayed_tr;
3380
3381 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3382 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3383 struct _tr_list, list);
3384 mpt2sas_base_free_smid(ioc, smid);
3385 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3386 list_del(&delayed_tr->list);
3387 kfree(delayed_tr);
3388 return 0;
3389 }
3390
Kashyap, Desai1278b112010-03-09 17:34:13 +05303391 if (!list_empty(&ioc->delayed_tr_list)) {
3392 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3393 struct _tr_list, list);
3394 mpt2sas_base_free_smid(ioc, smid);
3395 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3396 list_del(&delayed_tr->list);
3397 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303398 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303399 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303400
Kashyap, Desai1278b112010-03-09 17:34:13 +05303401 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303402}
3403
3404/**
Eric Moore635374e2009-03-09 01:21:12 -06003405 * _scsih_check_topo_delete_events - sanity check on topo events
3406 * @ioc: per adapter object
3407 * @event_data: the event data payload
3408 *
3409 * This routine added to better handle cable breaker.
3410 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003411 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003412 * add and delete events in a single shot. When there is a delete event
3413 * the routine will void any pending add events waiting in the event queue.
3414 *
3415 * Return nothing.
3416 */
3417static void
3418_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3419 Mpi2EventDataSasTopologyChangeList_t *event_data)
3420{
3421 struct fw_event_work *fw_event;
3422 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3423 u16 expander_handle;
3424 struct _sas_node *sas_expander;
3425 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303426 int i, reason_code;
3427 u16 handle;
3428
3429 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303430 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3431 if (!handle)
3432 continue;
3433 reason_code = event_data->PHY[i].PhyStatus &
3434 MPI2_EVENT_SAS_TOPO_RC_MASK;
3435 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3436 _scsih_tm_tr_send(ioc, handle);
3437 }
Eric Moore635374e2009-03-09 01:21:12 -06003438
3439 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3440 if (expander_handle < ioc->sas_hba.num_phys) {
3441 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3442 return;
3443 }
3444
3445 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3446 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3447 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3448 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3449 expander_handle);
3450 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3451 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3452 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3453 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3454
3455 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3456 return;
3457
3458 /* mark ignore flag for pending events */
3459 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3460 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3461 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3462 fw_event->ignore)
3463 continue;
3464 local_event_data = fw_event->event_data;
3465 if (local_event_data->ExpStatus ==
3466 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3467 local_event_data->ExpStatus ==
3468 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3469 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3470 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303471 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003472 "setting ignoring flag\n", ioc->name));
3473 fw_event->ignore = 1;
3474 }
3475 }
3476 }
3477 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3478}
3479
3480/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303481 * _scsih_set_volume_delete_flag - setting volume delete flag
3482 * @ioc: per adapter object
3483 * @handle: device handle
3484 *
3485 * This
3486 * Return nothing.
3487 */
3488static void
3489_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3490{
3491 struct _raid_device *raid_device;
3492 struct MPT2SAS_TARGET *sas_target_priv_data;
3493 unsigned long flags;
3494
3495 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3496 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3497 if (raid_device && raid_device->starget &&
3498 raid_device->starget->hostdata) {
3499 sas_target_priv_data =
3500 raid_device->starget->hostdata;
3501 sas_target_priv_data->deleted = 1;
3502 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3503 "setting delete flag: handle(0x%04x), "
3504 "wwid(0x%016llx)\n", ioc->name, handle,
3505 (unsigned long long) raid_device->wwid));
3506 }
3507 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3508}
3509
3510/**
3511 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3512 * @handle: input handle
3513 * @a: handle for volume a
3514 * @b: handle for volume b
3515 *
3516 * IR firmware only supports two raid volumes. The purpose of this
3517 * routine is to set the volume handle in either a or b. When the given
3518 * input handle is non-zero, or when a and b have not been set before.
3519 */
3520static void
3521_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3522{
3523 if (!handle || handle == *a || handle == *b)
3524 return;
3525 if (!*a)
3526 *a = handle;
3527 else if (!*b)
3528 *b = handle;
3529}
3530
3531/**
3532 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3533 * @ioc: per adapter object
3534 * @event_data: the event data payload
3535 * Context: interrupt time.
3536 *
3537 * This routine will send target reset to volume, followed by target
3538 * resets to the PDs. This is called when a PD has been removed, or
3539 * volume has been deleted or removed. When the target reset is sent
3540 * to volume, the PD target resets need to be queued to start upon
3541 * completion of the volume target reset.
3542 *
3543 * Return nothing.
3544 */
3545static void
3546_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3547 Mpi2EventDataIrConfigChangeList_t *event_data)
3548{
3549 Mpi2EventIrConfigElement_t *element;
3550 int i;
3551 u16 handle, volume_handle, a, b;
3552 struct _tr_list *delayed_tr;
3553
3554 a = 0;
3555 b = 0;
3556
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303557 if (ioc->is_warpdrive)
3558 return;
3559
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303560 /* Volume Resets for Deleted or Removed */
3561 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3562 for (i = 0; i < event_data->NumElements; i++, element++) {
3563 if (element->ReasonCode ==
3564 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3565 element->ReasonCode ==
3566 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3567 volume_handle = le16_to_cpu(element->VolDevHandle);
3568 _scsih_set_volume_delete_flag(ioc, volume_handle);
3569 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3570 }
3571 }
3572
3573 /* Volume Resets for UNHIDE events */
3574 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3575 for (i = 0; i < event_data->NumElements; i++, element++) {
3576 if (le32_to_cpu(event_data->Flags) &
3577 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3578 continue;
3579 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3580 volume_handle = le16_to_cpu(element->VolDevHandle);
3581 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3582 }
3583 }
3584
3585 if (a)
3586 _scsih_tm_tr_volume_send(ioc, a);
3587 if (b)
3588 _scsih_tm_tr_volume_send(ioc, b);
3589
3590 /* PD target resets */
3591 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3592 for (i = 0; i < event_data->NumElements; i++, element++) {
3593 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3594 continue;
3595 handle = le16_to_cpu(element->PhysDiskDevHandle);
3596 volume_handle = le16_to_cpu(element->VolDevHandle);
3597 clear_bit(handle, ioc->pd_handles);
3598 if (!volume_handle)
3599 _scsih_tm_tr_send(ioc, handle);
3600 else if (volume_handle == a || volume_handle == b) {
3601 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3602 BUG_ON(!delayed_tr);
3603 INIT_LIST_HEAD(&delayed_tr->list);
3604 delayed_tr->handle = handle;
3605 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3606 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3607 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3608 handle));
3609 } else
3610 _scsih_tm_tr_send(ioc, handle);
3611 }
3612}
3613
3614
3615/**
3616 * _scsih_check_volume_delete_events - set delete flag for volumes
3617 * @ioc: per adapter object
3618 * @event_data: the event data payload
3619 * Context: interrupt time.
3620 *
3621 * This will handle the case when the cable connected to entire volume is
3622 * pulled. We will take care of setting the deleted flag so normal IO will
3623 * not be sent.
3624 *
3625 * Return nothing.
3626 */
3627static void
3628_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3629 Mpi2EventDataIrVolume_t *event_data)
3630{
3631 u32 state;
3632
3633 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3634 return;
3635 state = le32_to_cpu(event_data->NewValue);
3636 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3637 MPI2_RAID_VOL_STATE_FAILED)
3638 _scsih_set_volume_delete_flag(ioc,
3639 le16_to_cpu(event_data->VolDevHandle));
3640}
3641
3642/**
Eric Moore635374e2009-03-09 01:21:12 -06003643 * _scsih_flush_running_cmds - completing outstanding commands.
3644 * @ioc: per adapter object
3645 *
3646 * The flushing out of all pending scmd commands following host reset,
3647 * where all IO is dropped to the floor.
3648 *
3649 * Return nothing.
3650 */
3651static void
3652_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3653{
3654 struct scsi_cmnd *scmd;
3655 u16 smid;
3656 u16 count = 0;
3657
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303658 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303659 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003660 if (!scmd)
3661 continue;
3662 count++;
3663 mpt2sas_base_free_smid(ioc, smid);
3664 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003665 if (ioc->pci_error_recovery)
3666 scmd->result = DID_NO_CONNECT << 16;
3667 else
3668 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003669 scmd->scsi_done(scmd);
3670 }
3671 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3672 ioc->name, count));
3673}
3674
3675/**
Eric Moore3c621b32009-05-18 12:59:41 -06003676 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3677 * @scmd: pointer to scsi command object
3678 * @mpi_request: pointer to the SCSI_IO reqest message frame
3679 *
3680 * Supporting protection 1 and 3.
3681 *
3682 * Returns nothing
3683 */
3684static void
3685_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3686{
3687 u16 eedp_flags;
3688 unsigned char prot_op = scsi_get_prot_op(scmd);
3689 unsigned char prot_type = scsi_get_prot_type(scmd);
3690
Eric Moored334aa72010-04-22 10:47:40 -06003691 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003692 return;
3693
3694 if (prot_op == SCSI_PROT_READ_STRIP)
3695 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3696 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3697 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3698 else
3699 return;
3700
Eric Moore3c621b32009-05-18 12:59:41 -06003701 switch (prot_type) {
3702 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003703 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003704
3705 /*
3706 * enable ref/guard checking
3707 * auto increment ref tag
3708 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303709 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003710 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3711 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3712 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3713 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003714 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003715
Eric Moore3c621b32009-05-18 12:59:41 -06003716 case SCSI_PROT_DIF_TYPE3:
3717
3718 /*
3719 * enable guard checking
3720 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303721 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003722 break;
3723 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303724 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3725 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003726}
3727
3728/**
3729 * _scsih_eedp_error_handling - return sense code for EEDP errors
3730 * @scmd: pointer to scsi command object
3731 * @ioc_status: ioc status
3732 *
3733 * Returns nothing
3734 */
3735static void
3736_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3737{
3738 u8 ascq;
3739 u8 sk;
3740 u8 host_byte;
3741
3742 switch (ioc_status) {
3743 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3744 ascq = 0x01;
3745 break;
3746 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3747 ascq = 0x02;
3748 break;
3749 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3750 ascq = 0x03;
3751 break;
3752 default:
3753 ascq = 0x00;
3754 break;
3755 }
3756
3757 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3758 sk = ILLEGAL_REQUEST;
3759 host_byte = DID_ABORT;
3760 } else {
3761 sk = ABORTED_COMMAND;
3762 host_byte = DID_OK;
3763 }
3764
3765 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3766 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3767 SAM_STAT_CHECK_CONDITION;
3768}
3769
3770/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303771 * _scsih_scsi_direct_io_get - returns direct io flag
3772 * @ioc: per adapter object
3773 * @smid: system request message index
3774 *
3775 * Returns the smid stored scmd pointer.
3776 */
3777static inline u8
3778_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3779{
3780 return ioc->scsi_lookup[smid - 1].direct_io;
3781}
3782
3783/**
3784 * _scsih_scsi_direct_io_set - sets direct io flag
3785 * @ioc: per adapter object
3786 * @smid: system request message index
3787 * @direct_io: Zero or non-zero value to set in the direct_io flag
3788 *
3789 * Returns Nothing.
3790 */
3791static inline void
3792_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3793{
3794 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3795}
3796
3797
3798/**
3799 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3800 * @ioc: per adapter object
3801 * @scmd: pointer to scsi command object
3802 * @raid_device: pointer to raid device data structure
3803 * @mpi_request: pointer to the SCSI_IO reqest message frame
3804 * @smid: system request message index
3805 *
3806 * Returns nothing
3807 */
3808static void
3809_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3810 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3811 u16 smid)
3812{
3813 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3814 u32 stripe_sz, stripe_exp;
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303815 u8 num_pds, *cdb_ptr, i;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303816 u8 cdb0 = scmd->cmnd[0];
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303817 u64 v_llba;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303818
3819 /*
3820 * Try Direct I/O to RAID memeber disks
3821 */
3822 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3823 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3824 cdb_ptr = mpi_request->CDB.CDB32;
3825
3826 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3827 | cdb_ptr[5])) {
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303828 io_size = scsi_bufflen(scmd) >>
3829 raid_device->block_exponent;
3830 i = (cdb0 < READ_16) ? 2 : 6;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303831 /* get virtual lba */
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303832 v_lba = be32_to_cpu(*(__be32 *)(&cdb_ptr[i]));
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303833
3834 if (((u64)v_lba + (u64)io_size - 1) <=
3835 (u32)raid_device->max_lba) {
3836 stripe_sz = raid_device->stripe_sz;
3837 stripe_exp = raid_device->stripe_exponent;
3838 stripe_off = v_lba & (stripe_sz - 1);
3839
3840 /* Check whether IO falls within a stripe */
3841 if ((stripe_off + io_size) <= stripe_sz) {
3842 num_pds = raid_device->num_pds;
3843 p_lba = v_lba >> stripe_exp;
3844 stripe_unit = p_lba / num_pds;
3845 column = p_lba % num_pds;
3846 p_lba = (stripe_unit << stripe_exp) +
3847 stripe_off;
3848 mpi_request->DevHandle =
3849 cpu_to_le16(raid_device->
3850 pd_handle[column]);
nagalakshmi.nandigama@lsi.comba96bd02011-12-01 07:51:55 +05303851 (*(__be32 *)(&cdb_ptr[i])) =
3852 cpu_to_be32(p_lba);
3853 /*
3854 * WD: To indicate this I/O is directI/O
3855 */
3856 _scsih_scsi_direct_io_set(ioc, smid, 1);
3857 }
3858 }
3859 } else {
3860 io_size = scsi_bufflen(scmd) >>
3861 raid_device->block_exponent;
3862 /* get virtual lba */
3863 v_llba = be64_to_cpu(*(__be64 *)(&cdb_ptr[2]));
3864
3865 if ((v_llba + (u64)io_size - 1) <=
3866 raid_device->max_lba) {
3867 stripe_sz = raid_device->stripe_sz;
3868 stripe_exp = raid_device->stripe_exponent;
3869 stripe_off = (u32) (v_llba & (stripe_sz - 1));
3870
3871 /* Check whether IO falls within a stripe */
3872 if ((stripe_off + io_size) <= stripe_sz) {
3873 num_pds = raid_device->num_pds;
3874 p_lba = (u32)(v_llba >> stripe_exp);
3875 stripe_unit = p_lba / num_pds;
3876 column = p_lba % num_pds;
3877 p_lba = (stripe_unit << stripe_exp) +
3878 stripe_off;
3879 mpi_request->DevHandle =
3880 cpu_to_le16(raid_device->
3881 pd_handle[column]);
3882 (*(__be64 *)(&cdb_ptr[2])) =
3883 cpu_to_be64((u64)p_lba);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303884 /*
3885 * WD: To indicate this I/O is directI/O
3886 */
3887 _scsih_scsi_direct_io_set(ioc, smid, 1);
3888 }
3889 }
3890 }
3891 }
3892}
3893
3894/**
Eric Moored5d135b2009-05-18 13:02:08 -06003895 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003896 * @scmd: pointer to scsi command object
3897 * @done: function pointer to be invoked on completion
3898 *
3899 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3900 *
3901 * Returns 0 on success. If there's a failure, return either:
3902 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3903 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3904 */
3905static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003906_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003907{
3908 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3909 struct MPT2SAS_DEVICE *sas_device_priv_data;
3910 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303911 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003912 Mpi2SCSIIORequest_t *mpi_request;
3913 u32 mpi_control;
3914 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003915
3916 scmd->scsi_done = done;
3917 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303918 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003919 scmd->result = DID_NO_CONNECT << 16;
3920 scmd->scsi_done(scmd);
3921 return 0;
3922 }
3923
Kashyap, Desai78215782011-06-14 10:57:08 +05303924 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003925 scmd->result = DID_NO_CONNECT << 16;
3926 scmd->scsi_done(scmd);
3927 return 0;
3928 }
3929
Eric Moore635374e2009-03-09 01:21:12 -06003930 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303931 /* invalid device handle */
3932 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003933 scmd->result = DID_NO_CONNECT << 16;
3934 scmd->scsi_done(scmd);
3935 return 0;
3936 }
3937
Kashyap, Desai130b9582010-04-08 17:54:32 +05303938 /* host recovery or link resets sent via IOCTLs */
3939 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003940 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003941 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303942 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3943 return SCSI_MLQUEUE_DEVICE_BUSY;
3944 /* device has been deleted */
3945 else if (sas_target_priv_data->deleted) {
3946 scmd->result = DID_NO_CONNECT << 16;
3947 scmd->scsi_done(scmd);
3948 return 0;
3949 }
Eric Moore635374e2009-03-09 01:21:12 -06003950
3951 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3952 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3953 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3954 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3955 else
3956 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3957
3958 /* set tags */
3959 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3960 if (scmd->device->tagged_supported) {
3961 if (scmd->device->ordered_tags)
3962 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3963 else
3964 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3965 } else
3966/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3967/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3968 */
3969 mpi_control |= (0x500);
3970
3971 } else
3972 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303973 /* Make sure Device is not raid volume.
3974 * We do not expose raid functionality to upper layer for warpdrive.
3975 */
3976 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003977 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003978 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3979
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303980 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003981 if (!smid) {
3982 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3983 ioc->name, __func__);
3984 goto out;
3985 }
3986 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3987 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003988 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003989 if (scmd->cmd_len == 32)
3990 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003991 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3992 if (sas_device_priv_data->sas_target->flags &
3993 MPT_TARGET_FLAGS_RAID_COMPONENT)
3994 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3995 else
3996 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3997 mpi_request->DevHandle =
3998 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3999 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4000 mpi_request->Control = cpu_to_le32(mpi_control);
4001 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
4002 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
4003 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
4004 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05304005 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004006 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
4007 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
4008 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304009 mpi_request->VF_ID = 0; /* TODO */
4010 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004011 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
4012 mpi_request->LUN);
4013 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4014
4015 if (!mpi_request->DataLength) {
4016 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
4017 } else {
4018 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
4019 mpt2sas_base_free_smid(ioc, smid);
4020 goto out;
4021 }
4022 }
4023
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304024 raid_device = sas_target_priv_data->raid_device;
4025 if (raid_device && raid_device->direct_io_enabled)
4026 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
4027 smid);
4028
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304029 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
4030 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304031 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05304032 else
4033 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004034 return 0;
4035
4036 out:
4037 return SCSI_MLQUEUE_HOST_BUSY;
4038}
4039
Jeff Garzikf2812332010-11-16 02:10:29 -05004040static DEF_SCSI_QCMD(_scsih_qcmd)
4041
Eric Moore635374e2009-03-09 01:21:12 -06004042/**
4043 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4044 * @sense_buffer: sense data returned by target
4045 * @data: normalized skey/asc/ascq
4046 *
4047 * Return nothing.
4048 */
4049static void
4050_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4051{
4052 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4053 /* descriptor format */
4054 data->skey = sense_buffer[1] & 0x0F;
4055 data->asc = sense_buffer[2];
4056 data->ascq = sense_buffer[3];
4057 } else {
4058 /* fixed format */
4059 data->skey = sense_buffer[2] & 0x0F;
4060 data->asc = sense_buffer[12];
4061 data->ascq = sense_buffer[13];
4062 }
4063}
4064
4065#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4066/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004067 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004068 * @ioc: per adapter object
4069 * @scmd: pointer to scsi command object
4070 * @mpi_reply: reply mf payload returned from firmware
4071 *
4072 * scsi_status - SCSI Status code returned from target device
4073 * scsi_state - state info associated with SCSI_IO determined by ioc
4074 * ioc_status - ioc supplied status info
4075 *
4076 * Return nothing.
4077 */
4078static void
4079_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4080 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4081{
4082 u32 response_info;
4083 u8 *response_bytes;
4084 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4085 MPI2_IOCSTATUS_MASK;
4086 u8 scsi_state = mpi_reply->SCSIState;
4087 u8 scsi_status = mpi_reply->SCSIStatus;
4088 char *desc_ioc_state = NULL;
4089 char *desc_scsi_status = NULL;
4090 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304091 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304092 struct _sas_device *sas_device = NULL;
4093 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304094 struct scsi_target *starget = scmd->device->sdev_target;
4095 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304096 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304097
4098 if (!priv_target)
4099 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304100
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304101 if (ioc->hide_ir_msg)
4102 device_str = "WarpDrive";
4103 else
4104 device_str = "volume";
4105
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304106 if (log_info == 0x31170000)
4107 return;
Eric Moore635374e2009-03-09 01:21:12 -06004108
4109 switch (ioc_status) {
4110 case MPI2_IOCSTATUS_SUCCESS:
4111 desc_ioc_state = "success";
4112 break;
4113 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4114 desc_ioc_state = "invalid function";
4115 break;
4116 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4117 desc_ioc_state = "scsi recovered error";
4118 break;
4119 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4120 desc_ioc_state = "scsi invalid dev handle";
4121 break;
4122 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4123 desc_ioc_state = "scsi device not there";
4124 break;
4125 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4126 desc_ioc_state = "scsi data overrun";
4127 break;
4128 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4129 desc_ioc_state = "scsi data underrun";
4130 break;
4131 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4132 desc_ioc_state = "scsi io data error";
4133 break;
4134 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4135 desc_ioc_state = "scsi protocol error";
4136 break;
4137 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4138 desc_ioc_state = "scsi task terminated";
4139 break;
4140 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4141 desc_ioc_state = "scsi residual mismatch";
4142 break;
4143 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4144 desc_ioc_state = "scsi task mgmt failed";
4145 break;
4146 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4147 desc_ioc_state = "scsi ioc terminated";
4148 break;
4149 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4150 desc_ioc_state = "scsi ext terminated";
4151 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004152 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4153 desc_ioc_state = "eedp guard error";
4154 break;
4155 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4156 desc_ioc_state = "eedp ref tag error";
4157 break;
4158 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4159 desc_ioc_state = "eedp app tag error";
4160 break;
Eric Moore635374e2009-03-09 01:21:12 -06004161 default:
4162 desc_ioc_state = "unknown";
4163 break;
4164 }
4165
4166 switch (scsi_status) {
4167 case MPI2_SCSI_STATUS_GOOD:
4168 desc_scsi_status = "good";
4169 break;
4170 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4171 desc_scsi_status = "check condition";
4172 break;
4173 case MPI2_SCSI_STATUS_CONDITION_MET:
4174 desc_scsi_status = "condition met";
4175 break;
4176 case MPI2_SCSI_STATUS_BUSY:
4177 desc_scsi_status = "busy";
4178 break;
4179 case MPI2_SCSI_STATUS_INTERMEDIATE:
4180 desc_scsi_status = "intermediate";
4181 break;
4182 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4183 desc_scsi_status = "intermediate condmet";
4184 break;
4185 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4186 desc_scsi_status = "reservation conflict";
4187 break;
4188 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4189 desc_scsi_status = "command terminated";
4190 break;
4191 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4192 desc_scsi_status = "task set full";
4193 break;
4194 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4195 desc_scsi_status = "aca active";
4196 break;
4197 case MPI2_SCSI_STATUS_TASK_ABORTED:
4198 desc_scsi_status = "task aborted";
4199 break;
4200 default:
4201 desc_scsi_status = "unknown";
4202 break;
4203 }
4204
4205 desc_scsi_state[0] = '\0';
4206 if (!scsi_state)
4207 desc_scsi_state = " ";
4208 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4209 strcat(desc_scsi_state, "response info ");
4210 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4211 strcat(desc_scsi_state, "state terminated ");
4212 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4213 strcat(desc_scsi_state, "no status ");
4214 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4215 strcat(desc_scsi_state, "autosense failed ");
4216 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4217 strcat(desc_scsi_state, "autosense valid ");
4218
4219 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304220
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304221 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304222 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4223 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304224 } else {
4225 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4226 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4227 priv_target->sas_address);
4228 if (sas_device) {
4229 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4230 "phy(%d)\n", ioc->name, sas_device->sas_address,
4231 sas_device->phy);
4232 printk(MPT2SAS_WARN_FMT
4233 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4234 ioc->name, sas_device->enclosure_logical_id,
4235 sas_device->slot);
4236 }
4237 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304238 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304239
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304240 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4241 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4242 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004243 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4244 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4245 scsi_get_resid(scmd));
4246 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4247 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4248 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4249 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4250 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4251 scsi_status, desc_scsi_state, scsi_state);
4252
4253 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4254 struct sense_info data;
4255 _scsih_normalize_sense(scmd->sense_buffer, &data);
4256 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304257 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4258 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004259 }
4260
4261 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4262 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4263 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304264 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004265 }
4266}
4267#endif
4268
4269/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304270 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004271 * @ioc: per adapter object
4272 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304273 * Context: process
4274 *
4275 * Return nothing.
4276 */
4277static void
4278_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4279{
4280 Mpi2SepReply_t mpi_reply;
4281 Mpi2SepRequest_t mpi_request;
4282
4283 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4284 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4285 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4286 mpi_request.SlotStatus =
4287 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4288 mpi_request.DevHandle = cpu_to_le16(handle);
4289 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4290 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4291 &mpi_request)) != 0) {
4292 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4293 __FILE__, __LINE__, __func__);
4294 return;
4295 }
4296
4297 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4298 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4299 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4300 le16_to_cpu(mpi_reply.IOCStatus),
4301 le32_to_cpu(mpi_reply.IOCLogInfo)));
4302 return;
4303 }
4304}
4305
4306/**
4307 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4308 * @ioc: per adapter object
4309 * @handle: device handle
4310 * Context: interrupt.
4311 *
4312 * Return nothing.
4313 */
4314static void
4315_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4316{
4317 struct fw_event_work *fw_event;
4318
4319 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4320 if (!fw_event)
4321 return;
4322 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4323 fw_event->device_handle = handle;
4324 fw_event->ioc = ioc;
4325 _scsih_fw_event_add(ioc, fw_event);
4326}
4327
4328/**
4329 * _scsih_smart_predicted_fault - process smart errors
4330 * @ioc: per adapter object
4331 * @handle: device handle
4332 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004333 *
4334 * Return nothing.
4335 */
4336static void
4337_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4338{
Eric Moore635374e2009-03-09 01:21:12 -06004339 struct scsi_target *starget;
4340 struct MPT2SAS_TARGET *sas_target_priv_data;
4341 Mpi2EventNotificationReply_t *event_reply;
4342 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4343 struct _sas_device *sas_device;
4344 ssize_t sz;
4345 unsigned long flags;
4346
4347 /* only handle non-raid devices */
4348 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4349 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4350 if (!sas_device) {
4351 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4352 return;
4353 }
4354 starget = sas_device->starget;
4355 sas_target_priv_data = starget->hostdata;
4356
4357 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4358 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4359 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4360 return;
4361 }
4362 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4363 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4364
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304365 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4366 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004367
4368 /* insert into event log */
4369 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4370 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b42011-11-07 22:05:21 +11004371 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004372 if (!event_reply) {
4373 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4374 ioc->name, __FILE__, __LINE__, __func__);
4375 return;
4376 }
4377
4378 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4379 event_reply->Event =
4380 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4381 event_reply->MsgLength = sz/4;
4382 event_reply->EventDataLength =
4383 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4384 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4385 event_reply->EventData;
4386 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4387 event_data->ASC = 0x5D;
4388 event_data->DevHandle = cpu_to_le16(handle);
4389 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4390 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4391 kfree(event_reply);
4392}
4393
4394/**
Eric Moored5d135b2009-05-18 13:02:08 -06004395 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004396 * @ioc: per adapter object
4397 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304398 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004399 * @reply: reply message frame(lower 32bit addr)
4400 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304401 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004402 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304403 * Return 1 meaning mf should be freed from _base_interrupt
4404 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004405 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304406static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304407_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004408{
4409 Mpi2SCSIIORequest_t *mpi_request;
4410 Mpi2SCSIIOReply_t *mpi_reply;
4411 struct scsi_cmnd *scmd;
4412 u16 ioc_status;
4413 u32 xfer_cnt;
4414 u8 scsi_state;
4415 u8 scsi_status;
4416 u32 log_info;
4417 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304418 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304419 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004420
4421 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304422 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004423 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304424 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004425
4426 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4427
4428 if (mpi_reply == NULL) {
4429 scmd->result = DID_OK << 16;
4430 goto out;
4431 }
4432
4433 sas_device_priv_data = scmd->device->hostdata;
4434 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4435 sas_device_priv_data->sas_target->deleted) {
4436 scmd->result = DID_NO_CONNECT << 16;
4437 goto out;
4438 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304439 /*
4440 * WARPDRIVE: If direct_io is set then it is directIO,
4441 * the failed direct I/O should be redirected to volume
4442 */
4443 if (_scsih_scsi_direct_io_get(ioc, smid)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304444 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4445 ioc->scsi_lookup[smid - 1].scmd = scmd;
4446 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304447 _scsih_scsi_direct_io_set(ioc, smid, 0);
4448 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4449 mpi_request->DevHandle =
4450 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4451 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4452 sas_device_priv_data->sas_target->handle);
4453 return 0;
4454 }
4455
Eric Moore635374e2009-03-09 01:21:12 -06004456
4457 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304458 scsi_state = mpi_reply->SCSIState;
4459 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4460 response_code =
4461 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004462 if (!sas_device_priv_data->tlr_snoop_check) {
4463 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304464 /* Make sure Device is not raid volume.
4465 * We do not expose raid functionality to upper layer for warpdrive.
4466 */
4467 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304468 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304469 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4470 sas_disable_tlr(scmd->device);
4471 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4472 }
Eric Moore635374e2009-03-09 01:21:12 -06004473 }
4474
4475 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4476 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4477 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4478 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4479 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4480 else
4481 log_info = 0;
4482 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004483 scsi_status = mpi_reply->SCSIStatus;
4484
4485 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4486 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4487 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4488 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4489 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4490 }
4491
4492 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4493 struct sense_info data;
4494 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4495 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004496 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004497 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004498 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004499 _scsih_normalize_sense(scmd->sense_buffer, &data);
4500 /* failure prediction threshold exceeded */
4501 if (data.asc == 0x5D)
4502 _scsih_smart_predicted_fault(ioc,
4503 le16_to_cpu(mpi_reply->DevHandle));
4504 }
4505
4506 switch (ioc_status) {
4507 case MPI2_IOCSTATUS_BUSY:
4508 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4509 scmd->result = SAM_STAT_BUSY;
4510 break;
4511
4512 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4513 scmd->result = DID_NO_CONNECT << 16;
4514 break;
4515
4516 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4517 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304518 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4519 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004520 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304521 scmd->result = DID_SOFT_ERROR << 16;
4522 break;
Eric Moore635374e2009-03-09 01:21:12 -06004523 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4524 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4525 scmd->result = DID_RESET << 16;
4526 break;
4527
4528 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4529 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4530 scmd->result = DID_SOFT_ERROR << 16;
4531 else
4532 scmd->result = (DID_OK << 16) | scsi_status;
4533 break;
4534
4535 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4536 scmd->result = (DID_OK << 16) | scsi_status;
4537
4538 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4539 break;
4540
4541 if (xfer_cnt < scmd->underflow) {
4542 if (scsi_status == SAM_STAT_BUSY)
4543 scmd->result = SAM_STAT_BUSY;
4544 else
4545 scmd->result = DID_SOFT_ERROR << 16;
4546 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4547 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4548 scmd->result = DID_SOFT_ERROR << 16;
4549 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4550 scmd->result = DID_RESET << 16;
4551 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4552 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4553 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4554 scmd->result = (DRIVER_SENSE << 24) |
4555 SAM_STAT_CHECK_CONDITION;
4556 scmd->sense_buffer[0] = 0x70;
4557 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4558 scmd->sense_buffer[12] = 0x20;
4559 scmd->sense_buffer[13] = 0;
4560 }
4561 break;
4562
4563 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4564 scsi_set_resid(scmd, 0);
4565 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4566 case MPI2_IOCSTATUS_SUCCESS:
4567 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304568 if (response_code ==
4569 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4570 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4571 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004572 scmd->result = DID_SOFT_ERROR << 16;
4573 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4574 scmd->result = DID_RESET << 16;
4575 break;
4576
Eric Moore3c621b32009-05-18 12:59:41 -06004577 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4578 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4579 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4580 _scsih_eedp_error_handling(scmd, ioc_status);
4581 break;
Eric Moore635374e2009-03-09 01:21:12 -06004582 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4583 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4584 case MPI2_IOCSTATUS_INVALID_SGL:
4585 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4586 case MPI2_IOCSTATUS_INVALID_FIELD:
4587 case MPI2_IOCSTATUS_INVALID_STATE:
4588 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4589 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4590 default:
4591 scmd->result = DID_SOFT_ERROR << 16;
4592 break;
4593
4594 }
4595
4596#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4597 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4598 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4599#endif
4600
4601 out:
4602 scsi_dma_unmap(scmd);
4603 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304604 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004605}
4606
4607/**
Eric Moore635374e2009-03-09 01:21:12 -06004608 * _scsih_sas_host_refresh - refreshing sas host object contents
4609 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004610 * Context: user
4611 *
4612 * During port enable, fw will send topology events for every device. Its
4613 * possible that the handles may change from the previous setting, so this
4614 * code keeping handles updating if changed.
4615 *
4616 * Return nothing.
4617 */
4618static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304619_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004620{
4621 u16 sz;
4622 u16 ioc_status;
4623 int i;
4624 Mpi2ConfigReply_t mpi_reply;
4625 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304626 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304627 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004628
4629 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4630 "updating handles for sas_host(0x%016llx)\n",
4631 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4632
4633 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4634 * sizeof(Mpi2SasIOUnit0PhyData_t));
4635 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4636 if (!sas_iounit_pg0) {
4637 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4638 ioc->name, __FILE__, __LINE__, __func__);
4639 return;
4640 }
Eric Moore635374e2009-03-09 01:21:12 -06004641
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304642 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4643 sas_iounit_pg0, sz)) != 0)
4644 goto out;
4645 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4646 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4647 goto out;
4648 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304649 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304650 if (i == 0)
4651 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4652 PhyData[0].ControllerDevHandle);
4653 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4654 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4655 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304656 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4657 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304658 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304659 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304660 }
Eric Moore635374e2009-03-09 01:21:12 -06004661 out:
4662 kfree(sas_iounit_pg0);
4663}
4664
4665/**
4666 * _scsih_sas_host_add - create sas host object
4667 * @ioc: per adapter object
4668 *
4669 * Creating host side data object, stored in ioc->sas_hba
4670 *
4671 * Return nothing.
4672 */
4673static void
4674_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4675{
4676 int i;
4677 Mpi2ConfigReply_t mpi_reply;
4678 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4679 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4680 Mpi2SasPhyPage0_t phy_pg0;
4681 Mpi2SasDevicePage0_t sas_device_pg0;
4682 Mpi2SasEnclosurePage0_t enclosure_pg0;
4683 u16 ioc_status;
4684 u16 sz;
4685 u16 device_missing_delay;
4686
4687 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4688 if (!ioc->sas_hba.num_phys) {
4689 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4690 ioc->name, __FILE__, __LINE__, __func__);
4691 return;
4692 }
4693
4694 /* sas_iounit page 0 */
4695 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4696 sizeof(Mpi2SasIOUnit0PhyData_t));
4697 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4698 if (!sas_iounit_pg0) {
4699 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4700 ioc->name, __FILE__, __LINE__, __func__);
4701 return;
4702 }
4703 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4704 sas_iounit_pg0, sz))) {
4705 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4706 ioc->name, __FILE__, __LINE__, __func__);
4707 goto out;
4708 }
4709 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4710 MPI2_IOCSTATUS_MASK;
4711 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4712 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4713 ioc->name, __FILE__, __LINE__, __func__);
4714 goto out;
4715 }
4716
4717 /* sas_iounit page 1 */
4718 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4719 sizeof(Mpi2SasIOUnit1PhyData_t));
4720 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4721 if (!sas_iounit_pg1) {
4722 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4723 ioc->name, __FILE__, __LINE__, __func__);
4724 goto out;
4725 }
4726 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4727 sas_iounit_pg1, sz))) {
4728 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4729 ioc->name, __FILE__, __LINE__, __func__);
4730 goto out;
4731 }
4732 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4733 MPI2_IOCSTATUS_MASK;
4734 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4735 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4736 ioc->name, __FILE__, __LINE__, __func__);
4737 goto out;
4738 }
4739
4740 ioc->io_missing_delay =
4741 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4742 device_missing_delay =
4743 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4744 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4745 ioc->device_missing_delay = (device_missing_delay &
4746 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4747 else
4748 ioc->device_missing_delay = device_missing_delay &
4749 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4750
4751 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4752 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4753 sizeof(struct _sas_phy), GFP_KERNEL);
4754 if (!ioc->sas_hba.phy) {
4755 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4756 ioc->name, __FILE__, __LINE__, __func__);
4757 goto out;
4758 }
4759 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4760 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4761 i))) {
4762 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4763 ioc->name, __FILE__, __LINE__, __func__);
4764 goto out;
4765 }
4766 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4767 MPI2_IOCSTATUS_MASK;
4768 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4769 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4770 ioc->name, __FILE__, __LINE__, __func__);
4771 goto out;
4772 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304773
4774 if (i == 0)
4775 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4776 PhyData[0].ControllerDevHandle);
4777 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004778 ioc->sas_hba.phy[i].phy_id = i;
4779 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4780 phy_pg0, ioc->sas_hba.parent_dev);
4781 }
4782 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304783 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004784 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4785 ioc->name, __FILE__, __LINE__, __func__);
4786 goto out;
4787 }
Eric Moore635374e2009-03-09 01:21:12 -06004788 ioc->sas_hba.enclosure_handle =
4789 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4790 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4791 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4792 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4793 (unsigned long long) ioc->sas_hba.sas_address,
4794 ioc->sas_hba.num_phys) ;
4795
4796 if (ioc->sas_hba.enclosure_handle) {
4797 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4798 &enclosure_pg0,
4799 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4800 ioc->sas_hba.enclosure_handle))) {
4801 ioc->sas_hba.enclosure_logical_id =
4802 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4803 }
4804 }
4805
4806 out:
4807 kfree(sas_iounit_pg1);
4808 kfree(sas_iounit_pg0);
4809}
4810
4811/**
4812 * _scsih_expander_add - creating expander object
4813 * @ioc: per adapter object
4814 * @handle: expander handle
4815 *
4816 * Creating expander object, stored in ioc->sas_expander_list.
4817 *
4818 * Return 0 for success, else error.
4819 */
4820static int
4821_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4822{
4823 struct _sas_node *sas_expander;
4824 Mpi2ConfigReply_t mpi_reply;
4825 Mpi2ExpanderPage0_t expander_pg0;
4826 Mpi2ExpanderPage1_t expander_pg1;
4827 Mpi2SasEnclosurePage0_t enclosure_pg0;
4828 u32 ioc_status;
4829 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304830 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004831 int i;
4832 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304833 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004834 int rc = 0;
4835
4836 if (!handle)
4837 return -1;
4838
Eric Moore3cb54692010-07-08 14:44:34 -06004839 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304840 return -1;
4841
Eric Moore635374e2009-03-09 01:21:12 -06004842 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4843 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4844 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4845 ioc->name, __FILE__, __LINE__, __func__);
4846 return -1;
4847 }
4848
4849 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4850 MPI2_IOCSTATUS_MASK;
4851 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4852 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4853 ioc->name, __FILE__, __LINE__, __func__);
4854 return -1;
4855 }
4856
4857 /* handle out of order topology events */
4858 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304859 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4860 != 0) {
4861 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4862 ioc->name, __FILE__, __LINE__, __func__);
4863 return -1;
4864 }
4865 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004866 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304867 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4868 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004869 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4870 if (!sas_expander) {
4871 rc = _scsih_expander_add(ioc, parent_handle);
4872 if (rc != 0)
4873 return rc;
4874 }
4875 }
4876
Eric Moore635374e2009-03-09 01:21:12 -06004877 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304878 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004879 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4880 sas_address);
4881 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4882
4883 if (sas_expander)
4884 return 0;
4885
4886 sas_expander = kzalloc(sizeof(struct _sas_node),
4887 GFP_KERNEL);
4888 if (!sas_expander) {
4889 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4890 ioc->name, __FILE__, __LINE__, __func__);
4891 return -1;
4892 }
4893
4894 sas_expander->handle = handle;
4895 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304896 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004897 sas_expander->sas_address = sas_address;
4898
4899 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4900 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304901 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004902 sas_expander->sas_address, sas_expander->num_phys);
4903
4904 if (!sas_expander->num_phys)
4905 goto out_fail;
4906 sas_expander->phy = kcalloc(sas_expander->num_phys,
4907 sizeof(struct _sas_phy), GFP_KERNEL);
4908 if (!sas_expander->phy) {
4909 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4910 ioc->name, __FILE__, __LINE__, __func__);
4911 rc = -1;
4912 goto out_fail;
4913 }
4914
4915 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4916 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304917 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004918 if (!mpt2sas_port) {
4919 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4920 ioc->name, __FILE__, __LINE__, __func__);
4921 rc = -1;
4922 goto out_fail;
4923 }
4924 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4925
4926 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4927 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4928 &expander_pg1, i, handle))) {
4929 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4930 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304931 rc = -1;
4932 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004933 }
4934 sas_expander->phy[i].handle = handle;
4935 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304936
4937 if ((mpt2sas_transport_add_expander_phy(ioc,
4938 &sas_expander->phy[i], expander_pg1,
4939 sas_expander->parent_dev))) {
4940 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4941 ioc->name, __FILE__, __LINE__, __func__);
4942 rc = -1;
4943 goto out_fail;
4944 }
Eric Moore635374e2009-03-09 01:21:12 -06004945 }
4946
4947 if (sas_expander->enclosure_handle) {
4948 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4949 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4950 sas_expander->enclosure_handle))) {
4951 sas_expander->enclosure_logical_id =
4952 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4953 }
4954 }
4955
4956 _scsih_expander_node_add(ioc, sas_expander);
4957 return 0;
4958
4959 out_fail:
4960
Kashyap, Desai20f58952009-08-07 19:34:26 +05304961 if (mpt2sas_port)
4962 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304963 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004964 kfree(sas_expander);
4965 return rc;
4966}
4967
4968/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304969 * _scsih_done - scsih callback handler.
4970 * @ioc: per adapter object
4971 * @smid: system request message index
4972 * @msix_index: MSIX table index supplied by the OS
4973 * @reply: reply message frame(lower 32bit addr)
4974 *
4975 * Callback handler when sending internal generated message frames.
4976 * The callback index passed is `ioc->scsih_cb_idx`
4977 *
4978 * Return 1 meaning mf should be freed from _base_interrupt
4979 * 0 means the mf is freed from this function.
4980 */
4981static u8
4982_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4983{
4984 MPI2DefaultReply_t *mpi_reply;
4985
4986 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4987 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4988 return 1;
4989 if (ioc->scsih_cmds.smid != smid)
4990 return 1;
4991 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4992 if (mpi_reply) {
4993 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4994 mpi_reply->MsgLength*4);
4995 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4996 }
4997 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4998 complete(&ioc->scsih_cmds.done);
4999 return 1;
5000}
5001
5002/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305003 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06005004 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305005 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06005006 *
5007 * Return nothing.
5008 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305009void
5010mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06005011{
5012 struct _sas_node *sas_expander;
5013 unsigned long flags;
5014
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305015 if (ioc->shost_recovery)
5016 return;
5017
Eric Moore635374e2009-03-09 01:21:12 -06005018 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305019 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
5020 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305021 if (!sas_expander) {
5022 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5023 return;
5024 }
5025 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06005026 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
5027 _scsih_expander_node_remove(ioc, sas_expander);
5028}
5029
5030/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05305031 * _scsih_check_access_status - check access flags
5032 * @ioc: per adapter object
5033 * @sas_address: sas address
5034 * @handle: sas device handle
5035 * @access_flags: errors returned during discovery of the device
5036 *
5037 * Return 0 for success, else failure
5038 */
5039static u8
5040_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5041 u16 handle, u8 access_status)
5042{
5043 u8 rc = 1;
5044 char *desc = NULL;
5045
5046 switch (access_status) {
5047 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5048 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5049 rc = 0;
5050 break;
5051 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5052 desc = "sata capability failed";
5053 break;
5054 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5055 desc = "sata affiliation conflict";
5056 break;
5057 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5058 desc = "route not addressable";
5059 break;
5060 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5061 desc = "smp error not addressable";
5062 break;
5063 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5064 desc = "device blocked";
5065 break;
5066 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5067 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5068 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5069 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5070 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5071 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5072 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5073 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5074 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5075 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5076 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5077 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5078 desc = "sata initialization failed";
5079 break;
5080 default:
5081 desc = "unknown";
5082 break;
5083 }
5084
5085 if (!rc)
5086 return 0;
5087
5088 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5089 "handle(0x%04x)\n", ioc->name, desc,
5090 (unsigned long long)sas_address, handle);
5091 return rc;
5092}
5093
5094static void
5095_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5096{
5097 Mpi2ConfigReply_t mpi_reply;
5098 Mpi2SasDevicePage0_t sas_device_pg0;
5099 struct _sas_device *sas_device;
5100 u32 ioc_status;
5101 unsigned long flags;
5102 u64 sas_address;
5103 struct scsi_target *starget;
5104 struct MPT2SAS_TARGET *sas_target_priv_data;
5105 u32 device_info;
5106
5107 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5108 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5109 return;
5110
5111 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5112 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5113 return;
5114
5115 /* check if this is end device */
5116 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5117 if (!(_scsih_is_end_device(device_info)))
5118 return;
5119
5120 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5121 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5122 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5123 sas_address);
5124
5125 if (!sas_device) {
5126 printk(MPT2SAS_ERR_FMT "device is not present "
5127 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5128 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5129 return;
5130 }
5131
5132 if (unlikely(sas_device->handle != handle)) {
5133 starget = sas_device->starget;
5134 sas_target_priv_data = starget->hostdata;
5135 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5136 " to (0x%04x)!!!\n", sas_device->handle, handle);
5137 sas_target_priv_data->handle = handle;
5138 sas_device->handle = handle;
5139 }
5140 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5141
5142 /* check if device is present */
5143 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5144 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5145 printk(MPT2SAS_ERR_FMT "device is not present "
5146 "handle(0x%04x), flags!!!\n", ioc->name, handle);
5147 return;
5148 }
5149
5150 /* check if there were any issues with discovery */
5151 if (_scsih_check_access_status(ioc, sas_address, handle,
5152 sas_device_pg0.AccessStatus))
5153 return;
5154 _scsih_ublock_io_device(ioc, handle);
5155
5156}
5157
5158/**
Eric Moore635374e2009-03-09 01:21:12 -06005159 * _scsih_add_device - creating sas device object
5160 * @ioc: per adapter object
5161 * @handle: sas device handle
5162 * @phy_num: phy number end device attached to
5163 * @is_pd: is this hidden raid component
5164 *
5165 * Creating end device object, stored in ioc->sas_device_list.
5166 *
5167 * Returns 0 for success, non-zero for failure.
5168 */
5169static int
5170_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5171{
5172 Mpi2ConfigReply_t mpi_reply;
5173 Mpi2SasDevicePage0_t sas_device_pg0;
5174 Mpi2SasEnclosurePage0_t enclosure_pg0;
5175 struct _sas_device *sas_device;
5176 u32 ioc_status;
5177 __le64 sas_address;
5178 u32 device_info;
5179 unsigned long flags;
5180
5181 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5182 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5183 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5184 ioc->name, __FILE__, __LINE__, __func__);
5185 return -1;
5186 }
5187
5188 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5189 MPI2_IOCSTATUS_MASK;
5190 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5191 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5192 ioc->name, __FILE__, __LINE__, __func__);
5193 return -1;
5194 }
5195
Kashyap, Desaib4344272010-03-17 16:24:14 +05305196 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5197
Eric Moore635374e2009-03-09 01:21:12 -06005198 /* check if device is present */
5199 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5200 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5201 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5202 ioc->name, __FILE__, __LINE__, __func__);
5203 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5204 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5205 return -1;
5206 }
5207
Kashyap, Desaib4344272010-03-17 16:24:14 +05305208 /* check if there were any issues with discovery */
5209 if (_scsih_check_access_status(ioc, sas_address, handle,
5210 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005211 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005212
5213 /* check if this is end device */
5214 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5215 if (!(_scsih_is_end_device(device_info))) {
5216 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5217 ioc->name, __FILE__, __LINE__, __func__);
5218 return -1;
5219 }
5220
Eric Moore635374e2009-03-09 01:21:12 -06005221
5222 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5223 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5224 sas_address);
5225 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5226
Kashyap, Desaib4344272010-03-17 16:24:14 +05305227 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005228 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005229
5230 sas_device = kzalloc(sizeof(struct _sas_device),
5231 GFP_KERNEL);
5232 if (!sas_device) {
5233 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5234 ioc->name, __FILE__, __LINE__, __func__);
5235 return -1;
5236 }
5237
5238 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305239 if (_scsih_get_sas_address(ioc, le16_to_cpu
5240 (sas_device_pg0.ParentDevHandle),
5241 &sas_device->sas_address_parent) != 0)
5242 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5243 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005244 sas_device->enclosure_handle =
5245 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5246 sas_device->slot =
5247 le16_to_cpu(sas_device_pg0.Slot);
5248 sas_device->device_info = device_info;
5249 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305250 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005251
5252 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305253 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5254 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5255 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005256 sas_device->enclosure_logical_id =
5257 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005258
5259 /* get device name */
5260 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5261
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305262 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005263 _scsih_sas_device_init_add(ioc, sas_device);
5264 else
5265 _scsih_sas_device_add(ioc, sas_device);
5266
5267 return 0;
5268}
5269
5270/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305271 * _scsih_remove_device - removing sas device object
5272 * @ioc: per adapter object
5273 * @sas_device_delete: the sas_device object
5274 *
5275 * Return nothing.
5276 */
5277static void
5278_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5279 struct _sas_device *sas_device)
5280{
5281 struct _sas_device sas_device_backup;
5282 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305283
Kashyap, Desai1278b112010-03-09 17:34:13 +05305284 if (!sas_device)
5285 return;
Eric Moore635374e2009-03-09 01:21:12 -06005286
Kashyap, Desai1278b112010-03-09 17:34:13 +05305287 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06005288 _scsih_sas_device_remove(ioc, sas_device);
5289
Kashyap, Desai1278b112010-03-09 17:34:13 +05305290 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5291 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5292 sas_device_backup.handle, (unsigned long long)
5293 sas_device_backup.sas_address));
5294
5295 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5296 sas_target_priv_data = sas_device_backup.starget->hostdata;
5297 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305298 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5299 sas_target_priv_data->handle =
5300 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305301 }
5302
Kashyap, Desai1278b112010-03-09 17:34:13 +05305303 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5304
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305305 if (!ioc->hide_drives)
5306 mpt2sas_transport_port_remove(ioc,
5307 sas_device_backup.sas_address,
5308 sas_device_backup.sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305309
5310 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5311 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5312 (unsigned long long) sas_device_backup.sas_address);
5313
5314 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5315 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5316 sas_device_backup.handle, (unsigned long long)
5317 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06005318}
5319
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305320/**
5321 * mpt2sas_device_remove - removing device object
5322 * @ioc: per adapter object
5323 * @sas_address: expander sas_address
5324 *
5325 * Return nothing.
5326 */
5327void
5328mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5329{
5330 struct _sas_device *sas_device;
5331 unsigned long flags;
5332
5333 if (ioc->shost_recovery)
5334 return;
5335
5336 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5337 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5338 sas_address);
5339 if (!sas_device) {
5340 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5341 return;
5342 }
5343 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5344 _scsih_remove_device(ioc, sas_device);
5345}
5346
Eric Moore635374e2009-03-09 01:21:12 -06005347#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5348/**
5349 * _scsih_sas_topology_change_event_debug - debug for topology event
5350 * @ioc: per adapter object
5351 * @event_data: event data payload
5352 * Context: user.
5353 */
5354static void
5355_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5356 Mpi2EventDataSasTopologyChangeList_t *event_data)
5357{
5358 int i;
5359 u16 handle;
5360 u16 reason_code;
5361 u8 phy_number;
5362 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305363 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005364
5365 switch (event_data->ExpStatus) {
5366 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5367 status_str = "add";
5368 break;
5369 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5370 status_str = "remove";
5371 break;
5372 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305373 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005374 status_str = "responding";
5375 break;
5376 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5377 status_str = "remove delay";
5378 break;
5379 default:
5380 status_str = "unknown status";
5381 break;
5382 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305383 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005384 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305385 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005386 "start_phy(%02d), count(%d)\n",
5387 le16_to_cpu(event_data->ExpanderDevHandle),
5388 le16_to_cpu(event_data->EnclosureHandle),
5389 event_data->StartPhyNum, event_data->NumEntries);
5390 for (i = 0; i < event_data->NumEntries; i++) {
5391 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5392 if (!handle)
5393 continue;
5394 phy_number = event_data->StartPhyNum + i;
5395 reason_code = event_data->PHY[i].PhyStatus &
5396 MPI2_EVENT_SAS_TOPO_RC_MASK;
5397 switch (reason_code) {
5398 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305399 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005400 break;
5401 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305402 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005403 break;
5404 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305405 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005406 break;
5407 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305408 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005409 break;
5410 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305411 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005412 break;
5413 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305414 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005415 break;
5416 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305417 link_rate = event_data->PHY[i].LinkRate >> 4;
5418 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305419 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305420 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5421 handle, status_str, link_rate, prev_link_rate);
5422
Eric Moore635374e2009-03-09 01:21:12 -06005423 }
5424}
5425#endif
5426
5427/**
5428 * _scsih_sas_topology_change_event - handle topology changes
5429 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305430 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005431 * Context: user.
5432 *
5433 */
5434static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305435_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005436 struct fw_event_work *fw_event)
5437{
5438 int i;
5439 u16 parent_handle, handle;
5440 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305441 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005442 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305443 struct _sas_device *sas_device;
5444 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005445 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305446 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305447 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005448
5449#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5450 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5451 _scsih_sas_topology_change_event_debug(ioc, event_data);
5452#endif
5453
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305454 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305455 return;
5456
Eric Moore635374e2009-03-09 01:21:12 -06005457 if (!ioc->sas_hba.num_phys)
5458 _scsih_sas_host_add(ioc);
5459 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305460 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005461
5462 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305463 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005464 "event\n", ioc->name));
5465 return;
5466 }
5467
5468 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5469
5470 /* handle expander add */
5471 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5472 if (_scsih_expander_add(ioc, parent_handle) != 0)
5473 return;
5474
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305475 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5476 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5477 parent_handle);
5478 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305479 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305480 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305481 max_phys = sas_expander->num_phys;
5482 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305483 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305484 max_phys = ioc->sas_hba.num_phys;
5485 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305486 return;
5487
Eric Moore635374e2009-03-09 01:21:12 -06005488 /* handle siblings events */
5489 for (i = 0; i < event_data->NumEntries; i++) {
5490 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305491 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005492 "expander event\n", ioc->name));
5493 return;
5494 }
Eric Moore3cb54692010-07-08 14:44:34 -06005495 if (ioc->shost_recovery || ioc->remove_host ||
5496 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305497 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305498 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305499 if (phy_number >= max_phys)
5500 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305501 reason_code = event_data->PHY[i].PhyStatus &
5502 MPI2_EVENT_SAS_TOPO_RC_MASK;
5503 if ((event_data->PHY[i].PhyStatus &
5504 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5505 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005506 continue;
5507 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5508 if (!handle)
5509 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305510 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305511 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005512 switch (reason_code) {
5513 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305514
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305515 if (ioc->shost_recovery)
5516 break;
5517
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305518 if (link_rate == prev_link_rate)
5519 break;
5520
5521 mpt2sas_transport_update_links(ioc, sas_address,
5522 handle, phy_number, link_rate);
5523
Kashyap, Desaib4344272010-03-17 16:24:14 +05305524 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5525 break;
5526
5527 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305528 break;
Eric Moore635374e2009-03-09 01:21:12 -06005529 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305530
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305531 if (ioc->shost_recovery)
5532 break;
5533
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305534 mpt2sas_transport_update_links(ioc, sas_address,
5535 handle, phy_number, link_rate);
5536
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305537 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005538 break;
5539 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305540
5541 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5542 sas_device = _scsih_sas_device_find_by_handle(ioc,
5543 handle);
5544 if (!sas_device) {
5545 spin_unlock_irqrestore(&ioc->sas_device_lock,
5546 flags);
5547 break;
5548 }
5549 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5550 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005551 break;
5552 }
5553 }
5554
5555 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305556 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5557 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305558 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005559
5560}
5561
5562#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5563/**
5564 * _scsih_sas_device_status_change_event_debug - debug for device event
5565 * @event_data: event data payload
5566 * Context: user.
5567 *
5568 * Return nothing.
5569 */
5570static void
5571_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5572 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5573{
5574 char *reason_str = NULL;
5575
5576 switch (event_data->ReasonCode) {
5577 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5578 reason_str = "smart data";
5579 break;
5580 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5581 reason_str = "unsupported device discovered";
5582 break;
5583 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5584 reason_str = "internal device reset";
5585 break;
5586 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5587 reason_str = "internal task abort";
5588 break;
5589 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5590 reason_str = "internal task abort set";
5591 break;
5592 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5593 reason_str = "internal clear task set";
5594 break;
5595 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5596 reason_str = "internal query task";
5597 break;
5598 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5599 reason_str = "sata init failure";
5600 break;
5601 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5602 reason_str = "internal device reset complete";
5603 break;
5604 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5605 reason_str = "internal task abort complete";
5606 break;
5607 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5608 reason_str = "internal async notification";
5609 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305610 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5611 reason_str = "expander reduced functionality";
5612 break;
5613 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5614 reason_str = "expander reduced functionality complete";
5615 break;
Eric Moore635374e2009-03-09 01:21:12 -06005616 default:
5617 reason_str = "unknown reason";
5618 break;
5619 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305620 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305621 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5622 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5623 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5624 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005625 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305626 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005627 event_data->ASC, event_data->ASCQ);
5628 printk(KERN_INFO "\n");
5629}
5630#endif
5631
5632/**
5633 * _scsih_sas_device_status_change_event - handle device status change
5634 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305635 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005636 * Context: user.
5637 *
5638 * Return nothing.
5639 */
5640static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305641_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5642 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005643{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305644 struct MPT2SAS_TARGET *target_priv_data;
5645 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305646 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305647 unsigned long flags;
5648 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5649 fw_event->event_data;
5650
Eric Moore635374e2009-03-09 01:21:12 -06005651#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5652 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305653 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305654 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005655#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305656
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305657 /* In MPI Revision K (0xC), the internal device reset complete was
5658 * implemented, so avoid setting tm_busy flag for older firmware.
5659 */
5660 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5661 return;
5662
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305663 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305664 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305665 event_data->ReasonCode !=
5666 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305667 return;
5668
5669 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5670 sas_address = le64_to_cpu(event_data->SASAddress);
5671 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5672 sas_address);
5673 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5674
5675 if (!sas_device || !sas_device->starget)
5676 return;
5677
5678 target_priv_data = sas_device->starget->hostdata;
5679 if (!target_priv_data)
5680 return;
5681
5682 if (event_data->ReasonCode ==
5683 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5684 target_priv_data->tm_busy = 1;
5685 else
5686 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005687}
5688
5689#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5690/**
5691 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5692 * @ioc: per adapter object
5693 * @event_data: event data payload
5694 * Context: user.
5695 *
5696 * Return nothing.
5697 */
5698static void
5699_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5700 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5701{
5702 char *reason_str = NULL;
5703
5704 switch (event_data->ReasonCode) {
5705 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5706 reason_str = "enclosure add";
5707 break;
5708 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5709 reason_str = "enclosure remove";
5710 break;
5711 default:
5712 reason_str = "unknown reason";
5713 break;
5714 }
5715
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305716 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005717 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5718 " number slots(%d)\n", ioc->name, reason_str,
5719 le16_to_cpu(event_data->EnclosureHandle),
5720 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5721 le16_to_cpu(event_data->StartSlot));
5722}
5723#endif
5724
5725/**
5726 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5727 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305728 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005729 * Context: user.
5730 *
5731 * Return nothing.
5732 */
5733static void
5734_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305735 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005736{
5737#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5738 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5739 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305740 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005741#endif
5742}
5743
5744/**
5745 * _scsih_sas_broadcast_primative_event - handle broadcast events
5746 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305747 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005748 * Context: user.
5749 *
5750 * Return nothing.
5751 */
5752static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305753_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5754 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005755{
5756 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305757 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005758 u16 smid, handle;
5759 u32 lun;
5760 struct MPT2SAS_DEVICE *sas_device_priv_data;
5761 u32 termination_count;
5762 u32 query_count;
5763 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305764 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305765 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305766 unsigned long flags;
5767 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305768 u8 max_retries = 0;
5769 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305770
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305771 mutex_lock(&ioc->tm_cmds.mutex);
5772 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5773 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5774 event_data->PortWidth));
5775
5776 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005777
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305778 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305779 mpi_reply = ioc->tm_cmds.reply;
5780broadcast_aen_retry:
5781
5782 /* sanity checks for retrying this loop */
5783 if (max_retries++ == 5) {
5784 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5785 ioc->name, __func__));
5786 goto out;
5787 } else if (max_retries > 1)
5788 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5789 ioc->name, __func__, max_retries - 1));
5790
Eric Moore635374e2009-03-09 01:21:12 -06005791 termination_count = 0;
5792 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305793 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305794 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305795 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005796 scmd = _scsih_scsi_lookup_get(ioc, smid);
5797 if (!scmd)
5798 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305799 sdev = scmd->device;
5800 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005801 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5802 continue;
5803 /* skip hidden raid components */
5804 if (sas_device_priv_data->sas_target->flags &
5805 MPT_TARGET_FLAGS_RAID_COMPONENT)
5806 continue;
5807 /* skip volumes */
5808 if (sas_device_priv_data->sas_target->flags &
5809 MPT_TARGET_FLAGS_VOLUME)
5810 continue;
5811
5812 handle = sas_device_priv_data->sas_target->handle;
5813 lun = sas_device_priv_data->lun;
5814 query_count++;
5815
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305816 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305817 goto out;
5818
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305819 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305820 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5821 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5822 TM_MUTEX_OFF);
5823 if (r == FAILED) {
5824 sdev_printk(KERN_WARNING, sdev,
5825 "mpt2sas_scsih_issue_tm: FAILED when sending "
5826 "QUERY_TASK: scmd(%p)\n", scmd);
5827 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5828 goto broadcast_aen_retry;
5829 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305830 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5831 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305832 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5833 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5834 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5835 scmd);
5836 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5837 goto broadcast_aen_retry;
5838 }
5839
5840 /* see if IO is still owned by IOC and target */
5841 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005842 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5843 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305844 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305845 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005846 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305847 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305848 task_abort_retries = 0;
5849 tm_retry:
5850 if (task_abort_retries++ == 60) {
5851 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5852 "%s: ABORT_TASK: giving up\n", ioc->name,
5853 __func__));
5854 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5855 goto broadcast_aen_retry;
5856 }
5857
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305858 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305859 goto out_no_lock;
5860
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305861 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5862 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305863 scmd->serial_number, TM_MUTEX_OFF);
5864 if (r == FAILED) {
5865 sdev_printk(KERN_WARNING, sdev,
5866 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305867 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305868 goto tm_retry;
5869 }
5870
5871 if (task_abort_retries > 1)
5872 sdev_printk(KERN_WARNING, sdev,
5873 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5874 " scmd(%p)\n",
5875 task_abort_retries - 1, scmd);
5876
Eric Moore635374e2009-03-09 01:21:12 -06005877 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305878 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005879 }
Eric Moore635374e2009-03-09 01:21:12 -06005880
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305881 if (ioc->broadcast_aen_pending) {
5882 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5883 " pending AEN\n", ioc->name, __func__));
5884 ioc->broadcast_aen_pending = 0;
5885 goto broadcast_aen_retry;
5886 }
5887
5888 out:
5889 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5890 out_no_lock:
5891
5892 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005893 "%s - exit, query_count = %d termination_count = %d\n",
5894 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305895
5896 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305897 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305898 _scsih_ublock_io_all_device(ioc);
5899 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005900}
5901
5902/**
5903 * _scsih_sas_discovery_event - handle discovery events
5904 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305905 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005906 * Context: user.
5907 *
5908 * Return nothing.
5909 */
5910static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305911_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5912 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005913{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305914 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5915
Eric Moore635374e2009-03-09 01:21:12 -06005916#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5917 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305918 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005919 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5920 "start" : "stop");
5921 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305922 printk("discovery_status(0x%08x)",
5923 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005924 printk("\n");
5925 }
5926#endif
5927
5928 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5929 !ioc->sas_hba.num_phys)
5930 _scsih_sas_host_add(ioc);
5931}
5932
5933/**
5934 * _scsih_reprobe_lun - reprobing lun
5935 * @sdev: scsi device struct
5936 * @no_uld_attach: sdev->no_uld_attach flag setting
5937 *
5938 **/
5939static void
5940_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5941{
5942 int rc;
5943
5944 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5945 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5946 sdev->no_uld_attach ? "hidding" : "exposing");
5947 rc = scsi_device_reprobe(sdev);
5948}
5949
5950/**
5951 * _scsih_reprobe_target - reprobing target
5952 * @starget: scsi target struct
5953 * @no_uld_attach: sdev->no_uld_attach flag setting
5954 *
5955 * Note: no_uld_attach flag determines whether the disk device is attached
5956 * to block layer. A value of `1` means to not attach.
5957 **/
5958static void
5959_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5960{
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305961 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06005962
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305963 if (starget == NULL)
5964 return;
5965 sas_target_priv_data = starget->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005966 if (no_uld_attach)
5967 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5968 else
5969 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5970
5971 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5972 _scsih_reprobe_lun);
5973}
5974/**
5975 * _scsih_sas_volume_add - add new volume
5976 * @ioc: per adapter object
5977 * @element: IR config element data
5978 * Context: user.
5979 *
5980 * Return nothing.
5981 */
5982static void
5983_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5984 Mpi2EventIrConfigElement_t *element)
5985{
5986 struct _raid_device *raid_device;
5987 unsigned long flags;
5988 u64 wwid;
5989 u16 handle = le16_to_cpu(element->VolDevHandle);
5990 int rc;
5991
Eric Moore635374e2009-03-09 01:21:12 -06005992 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5993 if (!wwid) {
5994 printk(MPT2SAS_ERR_FMT
5995 "failure at %s:%d/%s()!\n", ioc->name,
5996 __FILE__, __LINE__, __func__);
5997 return;
5998 }
5999
6000 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6001 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
6002 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6003
6004 if (raid_device)
6005 return;
6006
6007 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6008 if (!raid_device) {
6009 printk(MPT2SAS_ERR_FMT
6010 "failure at %s:%d/%s()!\n", ioc->name,
6011 __FILE__, __LINE__, __func__);
6012 return;
6013 }
6014
6015 raid_device->id = ioc->sas_id++;
6016 raid_device->channel = RAID_CHANNEL;
6017 raid_device->handle = handle;
6018 raid_device->wwid = wwid;
6019 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306020 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06006021 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6022 raid_device->id, 0);
6023 if (rc)
6024 _scsih_raid_device_remove(ioc, raid_device);
6025 } else
6026 _scsih_determine_boot_device(ioc, raid_device, 1);
6027}
6028
6029/**
6030 * _scsih_sas_volume_delete - delete volume
6031 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306032 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006033 * Context: user.
6034 *
6035 * Return nothing.
6036 */
6037static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306038_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006039{
6040 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006041 unsigned long flags;
6042 struct MPT2SAS_TARGET *sas_target_priv_data;
6043
Eric Moore635374e2009-03-09 01:21:12 -06006044 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6045 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6046 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6047 if (!raid_device)
6048 return;
6049 if (raid_device->starget) {
6050 sas_target_priv_data = raid_device->starget->hostdata;
6051 sas_target_priv_data->deleted = 1;
6052 scsi_remove_target(&raid_device->starget->dev);
6053 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306054 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6055 "(0x%016llx)\n", ioc->name, raid_device->handle,
6056 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006057 _scsih_raid_device_remove(ioc, raid_device);
6058}
6059
6060/**
6061 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6062 * @ioc: per adapter object
6063 * @element: IR config element data
6064 * Context: user.
6065 *
6066 * Return nothing.
6067 */
6068static void
6069_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6070 Mpi2EventIrConfigElement_t *element)
6071{
6072 struct _sas_device *sas_device;
6073 unsigned long flags;
6074 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6075
6076 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6077 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6078 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6079 if (!sas_device)
6080 return;
6081
6082 /* exposing raid component */
6083 sas_device->volume_handle = 0;
6084 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306085 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006086 _scsih_reprobe_target(sas_device->starget, 0);
6087}
6088
6089/**
6090 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6091 * @ioc: per adapter object
6092 * @element: IR config element data
6093 * Context: user.
6094 *
6095 * Return nothing.
6096 */
6097static void
6098_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6099 Mpi2EventIrConfigElement_t *element)
6100{
6101 struct _sas_device *sas_device;
6102 unsigned long flags;
6103 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6104
6105 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6106 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6107 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6108 if (!sas_device)
6109 return;
6110
6111 /* hiding raid component */
6112 mpt2sas_config_get_volume_handle(ioc, handle,
6113 &sas_device->volume_handle);
6114 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
6115 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306116 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006117 _scsih_reprobe_target(sas_device->starget, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306118
Eric Moore635374e2009-03-09 01:21:12 -06006119}
6120
6121/**
6122 * _scsih_sas_pd_delete - delete pd component
6123 * @ioc: per adapter object
6124 * @element: IR config element data
6125 * Context: user.
6126 *
6127 * Return nothing.
6128 */
6129static void
6130_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6131 Mpi2EventIrConfigElement_t *element)
6132{
6133 struct _sas_device *sas_device;
6134 unsigned long flags;
6135 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6136
6137 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6138 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6139 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6140 if (!sas_device)
6141 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306142 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006143}
6144
6145/**
6146 * _scsih_sas_pd_add - remove pd component
6147 * @ioc: per adapter object
6148 * @element: IR config element data
6149 * Context: user.
6150 *
6151 * Return nothing.
6152 */
6153static void
6154_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6155 Mpi2EventIrConfigElement_t *element)
6156{
6157 struct _sas_device *sas_device;
6158 unsigned long flags;
6159 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306160 Mpi2ConfigReply_t mpi_reply;
6161 Mpi2SasDevicePage0_t sas_device_pg0;
6162 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306163 u64 sas_address;
6164 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006165
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306166 set_bit(handle, ioc->pd_handles);
6167
Eric Moore635374e2009-03-09 01:21:12 -06006168 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6169 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6170 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306171 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306172 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306173
6174 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6175 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6176 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6177 ioc->name, __FILE__, __LINE__, __func__);
6178 return;
6179 }
6180
6181 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6182 MPI2_IOCSTATUS_MASK;
6183 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6184 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6185 ioc->name, __FILE__, __LINE__, __func__);
6186 return;
6187 }
6188
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306189 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6190 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6191 mpt2sas_transport_update_links(ioc, sas_address, handle,
6192 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306193
6194 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006195}
6196
6197#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6198/**
6199 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6200 * @ioc: per adapter object
6201 * @event_data: event data payload
6202 * Context: user.
6203 *
6204 * Return nothing.
6205 */
6206static void
6207_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6208 Mpi2EventDataIrConfigChangeList_t *event_data)
6209{
6210 Mpi2EventIrConfigElement_t *element;
6211 u8 element_type;
6212 int i;
6213 char *reason_str = NULL, *element_str = NULL;
6214
6215 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6216
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306217 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006218 ioc->name, (le32_to_cpu(event_data->Flags) &
6219 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6220 "foreign" : "native", event_data->NumElements);
6221 for (i = 0; i < event_data->NumElements; i++, element++) {
6222 switch (element->ReasonCode) {
6223 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6224 reason_str = "add";
6225 break;
6226 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6227 reason_str = "remove";
6228 break;
6229 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6230 reason_str = "no change";
6231 break;
6232 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6233 reason_str = "hide";
6234 break;
6235 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6236 reason_str = "unhide";
6237 break;
6238 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6239 reason_str = "volume_created";
6240 break;
6241 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6242 reason_str = "volume_deleted";
6243 break;
6244 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6245 reason_str = "pd_created";
6246 break;
6247 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6248 reason_str = "pd_deleted";
6249 break;
6250 default:
6251 reason_str = "unknown reason";
6252 break;
6253 }
6254 element_type = le16_to_cpu(element->ElementFlags) &
6255 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6256 switch (element_type) {
6257 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6258 element_str = "volume";
6259 break;
6260 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6261 element_str = "phys disk";
6262 break;
6263 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6264 element_str = "hot spare";
6265 break;
6266 default:
6267 element_str = "unknown element";
6268 break;
6269 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306270 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006271 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6272 reason_str, le16_to_cpu(element->VolDevHandle),
6273 le16_to_cpu(element->PhysDiskDevHandle),
6274 element->PhysDiskNum);
6275 }
6276}
6277#endif
6278
6279/**
6280 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6281 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306282 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006283 * Context: user.
6284 *
6285 * Return nothing.
6286 */
6287static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306288_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6289 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006290{
6291 Mpi2EventIrConfigElement_t *element;
6292 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306293 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306294 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006295
6296#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306297 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6298 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006299 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6300
6301#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306302
6303 if (ioc->shost_recovery)
6304 return;
6305
Kashyap, Desai62727a72009-08-07 19:35:18 +05306306 foreign_config = (le32_to_cpu(event_data->Flags) &
6307 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006308
6309 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6310 for (i = 0; i < event_data->NumElements; i++, element++) {
6311
6312 switch (element->ReasonCode) {
6313 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6314 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306315 if (!foreign_config)
6316 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006317 break;
6318 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6319 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306320 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306321 _scsih_sas_volume_delete(ioc,
6322 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006323 break;
6324 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306325 if (!ioc->is_warpdrive)
6326 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006327 break;
6328 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306329 if (!ioc->is_warpdrive)
6330 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006331 break;
6332 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306333 if (!ioc->is_warpdrive)
6334 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006335 break;
6336 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306337 if (!ioc->is_warpdrive)
6338 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006339 break;
6340 }
6341 }
6342}
6343
6344/**
6345 * _scsih_sas_ir_volume_event - IR volume event
6346 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306347 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006348 * Context: user.
6349 *
6350 * Return nothing.
6351 */
6352static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306353_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6354 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006355{
6356 u64 wwid;
6357 unsigned long flags;
6358 struct _raid_device *raid_device;
6359 u16 handle;
6360 u32 state;
6361 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306362 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006363
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306364 if (ioc->shost_recovery)
6365 return;
6366
Eric Moore635374e2009-03-09 01:21:12 -06006367 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6368 return;
6369
6370 handle = le16_to_cpu(event_data->VolDevHandle);
6371 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306372 if (!ioc->hide_ir_msg)
6373 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6374 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6375 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006376
Eric Moore635374e2009-03-09 01:21:12 -06006377 switch (state) {
6378 case MPI2_RAID_VOL_STATE_MISSING:
6379 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306380 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006381 break;
6382
6383 case MPI2_RAID_VOL_STATE_ONLINE:
6384 case MPI2_RAID_VOL_STATE_DEGRADED:
6385 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306386
6387 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6388 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6389 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6390
Eric Moore635374e2009-03-09 01:21:12 -06006391 if (raid_device)
6392 break;
6393
6394 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6395 if (!wwid) {
6396 printk(MPT2SAS_ERR_FMT
6397 "failure at %s:%d/%s()!\n", ioc->name,
6398 __FILE__, __LINE__, __func__);
6399 break;
6400 }
6401
6402 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6403 if (!raid_device) {
6404 printk(MPT2SAS_ERR_FMT
6405 "failure at %s:%d/%s()!\n", ioc->name,
6406 __FILE__, __LINE__, __func__);
6407 break;
6408 }
6409
6410 raid_device->id = ioc->sas_id++;
6411 raid_device->channel = RAID_CHANNEL;
6412 raid_device->handle = handle;
6413 raid_device->wwid = wwid;
6414 _scsih_raid_device_add(ioc, raid_device);
6415 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6416 raid_device->id, 0);
6417 if (rc)
6418 _scsih_raid_device_remove(ioc, raid_device);
6419 break;
6420
6421 case MPI2_RAID_VOL_STATE_INITIALIZING:
6422 default:
6423 break;
6424 }
6425}
6426
6427/**
6428 * _scsih_sas_ir_physical_disk_event - PD event
6429 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306430 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006431 * Context: user.
6432 *
6433 * Return nothing.
6434 */
6435static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306436_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6437 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006438{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306439 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006440 u32 state;
6441 struct _sas_device *sas_device;
6442 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306443 Mpi2ConfigReply_t mpi_reply;
6444 Mpi2SasDevicePage0_t sas_device_pg0;
6445 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306446 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306447 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006448
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306449 if (ioc->shost_recovery)
6450 return;
6451
Eric Moore635374e2009-03-09 01:21:12 -06006452 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6453 return;
6454
6455 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6456 state = le32_to_cpu(event_data->NewValue);
6457
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306458 if (!ioc->hide_ir_msg)
6459 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6460 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6461 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006462
Eric Moore635374e2009-03-09 01:21:12 -06006463 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006464 case MPI2_RAID_PD_STATE_ONLINE:
6465 case MPI2_RAID_PD_STATE_DEGRADED:
6466 case MPI2_RAID_PD_STATE_REBUILDING:
6467 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306468 case MPI2_RAID_PD_STATE_HOT_SPARE:
6469
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306470 if (!ioc->is_warpdrive)
6471 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306472
6473 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6474 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6475 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6476
6477 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306478 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306479
6480 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6481 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6482 handle))) {
6483 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6484 ioc->name, __FILE__, __LINE__, __func__);
6485 return;
6486 }
6487
6488 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6489 MPI2_IOCSTATUS_MASK;
6490 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6491 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6492 ioc->name, __FILE__, __LINE__, __func__);
6493 return;
6494 }
6495
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306496 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6497 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6498 mpt2sas_transport_update_links(ioc, sas_address, handle,
6499 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306500
6501 _scsih_add_device(ioc, handle, 0, 1);
6502
Eric Moore635374e2009-03-09 01:21:12 -06006503 break;
6504
Kashyap, Desai62727a72009-08-07 19:35:18 +05306505 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006506 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6507 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006508 default:
6509 break;
6510 }
6511}
6512
6513#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6514/**
6515 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6516 * @ioc: per adapter object
6517 * @event_data: event data payload
6518 * Context: user.
6519 *
6520 * Return nothing.
6521 */
6522static void
6523_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6524 Mpi2EventDataIrOperationStatus_t *event_data)
6525{
6526 char *reason_str = NULL;
6527
6528 switch (event_data->RAIDOperation) {
6529 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6530 reason_str = "resync";
6531 break;
6532 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6533 reason_str = "online capacity expansion";
6534 break;
6535 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6536 reason_str = "consistency check";
6537 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306538 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6539 reason_str = "background init";
6540 break;
6541 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6542 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006543 break;
6544 }
6545
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306546 if (!reason_str)
6547 return;
6548
Eric Moore635374e2009-03-09 01:21:12 -06006549 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6550 "\thandle(0x%04x), percent complete(%d)\n",
6551 ioc->name, reason_str,
6552 le16_to_cpu(event_data->VolDevHandle),
6553 event_data->PercentComplete);
6554}
6555#endif
6556
6557/**
6558 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6559 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306560 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006561 * Context: user.
6562 *
6563 * Return nothing.
6564 */
6565static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306566_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6567 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006568{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306569 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6570 static struct _raid_device *raid_device;
6571 unsigned long flags;
6572 u16 handle;
6573
Eric Moore635374e2009-03-09 01:21:12 -06006574#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306575 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6576 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306577 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306578 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006579#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306580
6581 /* code added for raid transport support */
6582 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6583
6584 handle = le16_to_cpu(event_data->VolDevHandle);
6585
6586 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6587 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6588 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6589
6590 if (!raid_device)
6591 return;
6592
6593 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6594 raid_device->percent_complete =
6595 event_data->PercentComplete;
6596 }
Eric Moore635374e2009-03-09 01:21:12 -06006597}
6598
6599/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306600 * _scsih_prep_device_scan - initialize parameters prior to device scan
6601 * @ioc: per adapter object
6602 *
6603 * Set the deleted flag prior to device scan. If the device is found during
6604 * the scan, then we clear the deleted flag.
6605 */
6606static void
6607_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6608{
6609 struct MPT2SAS_DEVICE *sas_device_priv_data;
6610 struct scsi_device *sdev;
6611
6612 shost_for_each_device(sdev, ioc->shost) {
6613 sas_device_priv_data = sdev->hostdata;
6614 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6615 sas_device_priv_data->sas_target->deleted = 1;
6616 }
6617}
6618
6619/**
Eric Moore635374e2009-03-09 01:21:12 -06006620 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6621 * @ioc: per adapter object
6622 * @sas_address: sas address
6623 * @slot: enclosure slot id
6624 * @handle: device handle
6625 *
6626 * After host reset, find out whether devices are still responding.
6627 * Used in _scsi_remove_unresponsive_sas_devices.
6628 *
6629 * Return nothing.
6630 */
6631static void
6632_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6633 u16 slot, u16 handle)
6634{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306635 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006636 struct scsi_target *starget;
6637 struct _sas_device *sas_device;
6638 unsigned long flags;
6639
6640 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6641 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6642 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306643 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006644 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306645 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306646 if (starget && starget->hostdata) {
6647 sas_target_priv_data = starget->hostdata;
6648 sas_target_priv_data->tm_busy = 0;
6649 sas_target_priv_data->deleted = 0;
6650 } else
6651 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306652 if (starget)
6653 starget_printk(KERN_INFO, starget,
6654 "handle(0x%04x), sas_addr(0x%016llx), "
6655 "enclosure logical id(0x%016llx), "
6656 "slot(%d)\n", handle,
6657 (unsigned long long)sas_device->sas_address,
6658 (unsigned long long)
6659 sas_device->enclosure_logical_id,
6660 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006661 if (sas_device->handle == handle)
6662 goto out;
6663 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6664 sas_device->handle);
6665 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306666 if (sas_target_priv_data)
6667 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006668 goto out;
6669 }
6670 }
6671 out:
6672 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6673}
6674
6675/**
6676 * _scsih_search_responding_sas_devices -
6677 * @ioc: per adapter object
6678 *
6679 * After host reset, find out whether devices are still responding.
6680 * If not remove.
6681 *
6682 * Return nothing.
6683 */
6684static void
6685_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6686{
6687 Mpi2SasDevicePage0_t sas_device_pg0;
6688 Mpi2ConfigReply_t mpi_reply;
6689 u16 ioc_status;
6690 __le64 sas_address;
6691 u16 handle;
6692 u32 device_info;
6693 u16 slot;
6694
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306695 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006696
6697 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306698 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006699
6700 handle = 0xFFFF;
6701 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6702 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6703 handle))) {
6704 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6705 MPI2_IOCSTATUS_MASK;
6706 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6707 break;
6708 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6709 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6710 if (!(_scsih_is_end_device(device_info)))
6711 continue;
6712 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6713 slot = le16_to_cpu(sas_device_pg0.Slot);
6714 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6715 handle);
6716 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306717out:
6718 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6719 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006720}
6721
6722/**
6723 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6724 * @ioc: per adapter object
6725 * @wwid: world wide identifier for raid volume
6726 * @handle: device handle
6727 *
6728 * After host reset, find out whether devices are still responding.
6729 * Used in _scsi_remove_unresponsive_raid_devices.
6730 *
6731 * Return nothing.
6732 */
6733static void
6734_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6735 u16 handle)
6736{
6737 struct MPT2SAS_TARGET *sas_target_priv_data;
6738 struct scsi_target *starget;
6739 struct _raid_device *raid_device;
6740 unsigned long flags;
6741
6742 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6743 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6744 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306745 starget = raid_device->starget;
6746 if (starget && starget->hostdata) {
6747 sas_target_priv_data = starget->hostdata;
6748 sas_target_priv_data->deleted = 0;
6749 } else
6750 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006751 raid_device->responding = 1;
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306752 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06006753 starget_printk(KERN_INFO, raid_device->starget,
6754 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6755 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306756 /*
6757 * WARPDRIVE: The handles of the PDs might have changed
6758 * across the host reset so re-initialize the
6759 * required data for Direct IO
6760 */
6761 _scsih_init_warpdrive_properties(ioc, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006762 if (raid_device->handle == handle)
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306763 return;
Eric Moore635374e2009-03-09 01:21:12 -06006764 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6765 raid_device->handle);
6766 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306767 if (sas_target_priv_data)
6768 sas_target_priv_data->handle = handle;
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306769 return;
Eric Moore635374e2009-03-09 01:21:12 -06006770 }
6771 }
nagalakshmi.nandigama@lsi.com30c43282011-12-01 07:52:56 +05306772
Eric Moore635374e2009-03-09 01:21:12 -06006773 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6774}
6775
6776/**
6777 * _scsih_search_responding_raid_devices -
6778 * @ioc: per adapter object
6779 *
6780 * After host reset, find out whether devices are still responding.
6781 * If not remove.
6782 *
6783 * Return nothing.
6784 */
6785static void
6786_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6787{
6788 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306789 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306790 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006791 Mpi2ConfigReply_t mpi_reply;
6792 u16 ioc_status;
6793 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306794 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006795
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306796 if (!ioc->ir_firmware)
6797 return;
6798
6799 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6800 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006801
6802 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306803 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006804
6805 handle = 0xFFFF;
6806 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6807 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6808 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6809 MPI2_IOCSTATUS_MASK;
6810 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6811 break;
6812 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306813
6814 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6815 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6816 sizeof(Mpi2RaidVolPage0_t)))
6817 continue;
6818
6819 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6820 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6821 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6822 _scsih_mark_responding_raid_device(ioc,
6823 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006824 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306825
6826 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306827 if (!ioc->is_warpdrive) {
6828 phys_disk_num = 0xFF;
6829 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6830 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6831 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6832 phys_disk_num))) {
6833 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6834 MPI2_IOCSTATUS_MASK;
6835 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6836 break;
6837 phys_disk_num = pd_pg0.PhysDiskNum;
6838 handle = le16_to_cpu(pd_pg0.DevHandle);
6839 set_bit(handle, ioc->pd_handles);
6840 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306841 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306842out:
6843 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6844 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006845}
6846
6847/**
6848 * _scsih_mark_responding_expander - mark a expander as responding
6849 * @ioc: per adapter object
6850 * @sas_address: sas address
6851 * @handle:
6852 *
6853 * After host reset, find out whether devices are still responding.
6854 * Used in _scsi_remove_unresponsive_expanders.
6855 *
6856 * Return nothing.
6857 */
6858static void
6859_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6860 u16 handle)
6861{
6862 struct _sas_node *sas_expander;
6863 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306864 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006865
6866 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6867 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306868 if (sas_expander->sas_address != sas_address)
6869 continue;
6870 sas_expander->responding = 1;
6871 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006872 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306873 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6874 " from(0x%04x) to (0x%04x)!!!\n",
6875 (unsigned long long)sas_expander->sas_address,
6876 sas_expander->handle, handle);
6877 sas_expander->handle = handle;
6878 for (i = 0 ; i < sas_expander->num_phys ; i++)
6879 sas_expander->phy[i].handle = handle;
6880 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006881 }
6882 out:
6883 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6884}
6885
6886/**
6887 * _scsih_search_responding_expanders -
6888 * @ioc: per adapter object
6889 *
6890 * After host reset, find out whether devices are still responding.
6891 * If not remove.
6892 *
6893 * Return nothing.
6894 */
6895static void
6896_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6897{
6898 Mpi2ExpanderPage0_t expander_pg0;
6899 Mpi2ConfigReply_t mpi_reply;
6900 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306901 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006902 u16 handle;
6903
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306904 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006905
6906 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306907 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006908
6909 handle = 0xFFFF;
6910 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6911 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6912
6913 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6914 MPI2_IOCSTATUS_MASK;
6915 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6916 break;
6917
6918 handle = le16_to_cpu(expander_pg0.DevHandle);
6919 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6920 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6921 "sas_addr(0x%016llx)\n", handle,
6922 (unsigned long long)sas_address);
6923 _scsih_mark_responding_expander(ioc, sas_address, handle);
6924 }
6925
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306926 out:
6927 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006928}
6929
6930/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306931 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006932 * @ioc: per adapter object
6933 *
6934 * Return nothing.
6935 */
6936static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306937_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006938{
6939 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306940 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006941 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006942
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306943 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
6944 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006945
6946 list_for_each_entry_safe(sas_device, sas_device_next,
6947 &ioc->sas_device_list, list) {
6948 if (sas_device->responding) {
6949 sas_device->responding = 0;
6950 continue;
6951 }
6952 if (sas_device->starget)
6953 starget_printk(KERN_INFO, sas_device->starget,
6954 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6955 "enclosure logical id(0x%016llx), slot(%d)\n",
6956 sas_device->handle,
6957 (unsigned long long)sas_device->sas_address,
6958 (unsigned long long)
6959 sas_device->enclosure_logical_id,
6960 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306961 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006962 }
6963
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306964 if (!ioc->ir_firmware)
6965 goto retry_expander_search;
6966
Eric Moore635374e2009-03-09 01:21:12 -06006967 list_for_each_entry_safe(raid_device, raid_device_next,
6968 &ioc->raid_device_list, list) {
6969 if (raid_device->responding) {
6970 raid_device->responding = 0;
6971 continue;
6972 }
6973 if (raid_device->starget) {
6974 starget_printk(KERN_INFO, raid_device->starget,
6975 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6976 raid_device->handle,
6977 (unsigned long long)raid_device->wwid);
6978 scsi_remove_target(&raid_device->starget->dev);
6979 }
6980 _scsih_raid_device_remove(ioc, raid_device);
6981 }
6982
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306983 retry_expander_search:
6984 sas_expander = NULL;
6985 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006986 if (sas_expander->responding) {
6987 sas_expander->responding = 0;
6988 continue;
6989 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306990 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306991 goto retry_expander_search;
6992 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306993 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
6994 ioc->name);
6995 /* unblock devices */
6996 _scsih_ublock_io_all_device(ioc);
6997}
6998
6999static void
7000_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
7001 struct _sas_node *sas_expander, u16 handle)
7002{
7003 Mpi2ExpanderPage1_t expander_pg1;
7004 Mpi2ConfigReply_t mpi_reply;
7005 int i;
7006
7007 for (i = 0 ; i < sas_expander->num_phys ; i++) {
7008 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
7009 &expander_pg1, i, handle))) {
7010 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7011 ioc->name, __FILE__, __LINE__, __func__);
7012 return;
7013 }
7014
7015 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
7016 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
7017 expander_pg1.NegotiatedLinkRate >> 4);
7018 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307019}
7020
7021/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307022 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307023 * @ioc: per adapter object
7024 *
7025 * Return nothing.
7026 */
7027static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307028_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307029{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307030 Mpi2ExpanderPage0_t expander_pg0;
7031 Mpi2SasDevicePage0_t sas_device_pg0;
7032 Mpi2RaidVolPage1_t volume_pg1;
7033 Mpi2RaidVolPage0_t volume_pg0;
7034 Mpi2RaidPhysDiskPage0_t pd_pg0;
7035 Mpi2EventIrConfigElement_t element;
7036 Mpi2ConfigReply_t mpi_reply;
7037 u8 phys_disk_num;
7038 u16 ioc_status;
7039 u16 handle, parent_handle;
7040 u64 sas_address;
7041 struct _sas_device *sas_device;
7042 struct _sas_node *expander_device;
7043 static struct _raid_device *raid_device;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307044
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307045 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307046
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307047 _scsih_sas_host_refresh(ioc);
7048
7049 /* expanders */
7050 handle = 0xFFFF;
7051 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7052 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7053 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7054 MPI2_IOCSTATUS_MASK;
7055 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7056 break;
7057 handle = le16_to_cpu(expander_pg0.DevHandle);
7058 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7059 ioc, le64_to_cpu(expander_pg0.SASAddress));
7060 if (expander_device)
7061 _scsih_refresh_expander_links(ioc, expander_device,
7062 handle);
7063 else
7064 _scsih_expander_add(ioc, handle);
7065 }
7066
7067 if (!ioc->ir_firmware)
7068 goto skip_to_sas;
7069
7070 /* phys disk */
7071 phys_disk_num = 0xFF;
7072 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7073 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7074 phys_disk_num))) {
7075 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7076 MPI2_IOCSTATUS_MASK;
7077 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7078 break;
7079 phys_disk_num = pd_pg0.PhysDiskNum;
7080 handle = le16_to_cpu(pd_pg0.DevHandle);
7081 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7082 if (sas_device)
7083 continue;
7084 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7085 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7086 handle) != 0)
7087 continue;
7088 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7089 if (!_scsih_get_sas_address(ioc, parent_handle,
7090 &sas_address)) {
7091 mpt2sas_transport_update_links(ioc, sas_address,
7092 handle, sas_device_pg0.PhyNum,
7093 MPI2_SAS_NEG_LINK_RATE_1_5);
7094 set_bit(handle, ioc->pd_handles);
7095 _scsih_add_device(ioc, handle, 0, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307096 }
7097 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307098
7099 /* volumes */
7100 handle = 0xFFFF;
7101 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7102 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7103 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7104 MPI2_IOCSTATUS_MASK;
7105 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7106 break;
7107 handle = le16_to_cpu(volume_pg1.DevHandle);
7108 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7109 le64_to_cpu(volume_pg1.WWID));
7110 if (raid_device)
7111 continue;
7112 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7113 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7114 sizeof(Mpi2RaidVolPage0_t)))
7115 continue;
7116 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7117 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7118 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7119 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7120 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7121 element.VolDevHandle = volume_pg1.DevHandle;
7122 _scsih_sas_volume_add(ioc, &element);
7123 }
7124 }
7125
7126 skip_to_sas:
7127
7128 /* sas devices */
7129 handle = 0xFFFF;
7130 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7131 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7132 handle))) {
7133 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7134 MPI2_IOCSTATUS_MASK;
7135 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7136 break;
7137 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7138 if (!(_scsih_is_end_device(
7139 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7140 continue;
7141 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7142 le64_to_cpu(sas_device_pg0.SASAddress));
7143 if (sas_device)
7144 continue;
7145 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7146 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7147 mpt2sas_transport_update_links(ioc, sas_address, handle,
7148 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7149 _scsih_add_device(ioc, handle, 0, 0);
7150 }
7151 }
7152
7153 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307154}
7155
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307156
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307157/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307158 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7159 * @ioc: per adapter object
7160 * @reset_phase: phase
7161 *
7162 * The handler for doing any required cleanup or initialization.
7163 *
7164 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7165 * MPT2_IOC_DONE_RESET
7166 *
7167 * Return nothing.
7168 */
7169void
7170mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7171{
7172 switch (reset_phase) {
7173 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307174 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307175 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307176 break;
7177 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307178 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307179 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307180 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7181 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7182 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7183 complete(&ioc->scsih_cmds.done);
7184 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307185 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7186 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7187 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7188 complete(&ioc->tm_cmds.done);
7189 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307190 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307191 _scsih_flush_running_cmds(ioc);
7192 break;
7193 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307194 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307195 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307196 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307197 _scsih_prep_device_scan(ioc);
7198 _scsih_search_responding_sas_devices(ioc);
7199 _scsih_search_responding_raid_devices(ioc);
7200 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307201 if (!ioc->is_driver_loading) {
7202 _scsih_prep_device_scan(ioc);
7203 _scsih_search_responding_sas_devices(ioc);
7204 _scsih_search_responding_raid_devices(ioc);
7205 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307206 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307207 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307208 break;
Eric Moore635374e2009-03-09 01:21:12 -06007209 }
7210}
7211
7212/**
7213 * _firmware_event_work - delayed task for processing firmware events
7214 * @ioc: per adapter object
7215 * @work: equal to the fw_event_work object
7216 * Context: user.
7217 *
7218 * Return nothing.
7219 */
7220static void
7221_firmware_event_work(struct work_struct *work)
7222{
7223 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307224 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007225 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7226
Eric Moore635374e2009-03-09 01:21:12 -06007227 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007228 if (ioc->remove_host || fw_event->cancel_pending_work ||
7229 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007230 _scsih_fw_event_free(ioc, fw_event);
7231 return;
7232 }
Eric Moore635374e2009-03-09 01:21:12 -06007233
Eric Moore635374e2009-03-09 01:21:12 -06007234 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307235 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
7236 while (scsi_host_in_recovery(ioc->shost))
7237 ssleep(1);
7238 _scsih_remove_unresponding_sas_devices(ioc);
7239 _scsih_scan_for_devices_after_reset(ioc);
7240 break;
7241 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307242 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307243
7244
7245
7246 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7247 "from worker thread\n", ioc->name));
7248 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307249 case MPT2SAS_TURN_ON_FAULT_LED:
7250 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7251 break;
Eric Moore635374e2009-03-09 01:21:12 -06007252 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307253 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007254 break;
7255 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307256 _scsih_sas_device_status_change_event(ioc,
7257 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007258 break;
7259 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307260 _scsih_sas_discovery_event(ioc,
7261 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007262 break;
7263 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307264 _scsih_sas_broadcast_primative_event(ioc,
7265 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007266 break;
7267 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7268 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307269 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007270 break;
7271 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307272 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007273 break;
7274 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307275 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007276 break;
7277 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307278 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007279 break;
7280 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307281 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007282 break;
Eric Moore635374e2009-03-09 01:21:12 -06007283 }
7284 _scsih_fw_event_free(ioc, fw_event);
7285}
7286
7287/**
7288 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7289 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307290 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007291 * @reply: reply message frame(lower 32bit addr)
7292 * Context: interrupt.
7293 *
7294 * This function merely adds a new work task into ioc->firmware_event_thread.
7295 * The tasks are worked from _firmware_event_work in user context.
7296 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307297 * Return 1 meaning mf should be freed from _base_interrupt
7298 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06007299 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307300u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307301mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7302 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007303{
7304 struct fw_event_work *fw_event;
7305 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007306 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307307 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007308
7309 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007310 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307311 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007312
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307313 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06007314 event = le16_to_cpu(mpi_reply->Event);
7315
7316 switch (event) {
7317 /* handle these */
7318 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7319 {
7320 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7321 (Mpi2EventDataSasBroadcastPrimitive_t *)
7322 mpi_reply->EventData;
7323
7324 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307325 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307326 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307327
7328 if (ioc->broadcast_aen_busy) {
7329 ioc->broadcast_aen_pending++;
7330 return 1;
7331 } else
7332 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007333 break;
7334 }
7335
7336 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7337 _scsih_check_topo_delete_events(ioc,
7338 (Mpi2EventDataSasTopologyChangeList_t *)
7339 mpi_reply->EventData);
7340 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307341 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7342 _scsih_check_ir_config_unhide_events(ioc,
7343 (Mpi2EventDataIrConfigChangeList_t *)
7344 mpi_reply->EventData);
7345 break;
7346 case MPI2_EVENT_IR_VOLUME:
7347 _scsih_check_volume_delete_events(ioc,
7348 (Mpi2EventDataIrVolume_t *)
7349 mpi_reply->EventData);
7350 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307351 case MPI2_EVENT_LOG_ENTRY_ADDED:
7352 {
7353 Mpi2EventDataLogEntryAdded_t *log_entry;
7354 u32 *log_code;
7355
7356 if (!ioc->is_warpdrive)
7357 break;
7358
7359 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7360 mpi_reply->EventData;
7361 log_code = (u32 *)log_entry->LogData;
7362
7363 if (le16_to_cpu(log_entry->LogEntryQualifier)
7364 != MPT2_WARPDRIVE_LOGENTRY)
7365 break;
7366
7367 switch (le32_to_cpu(*log_code)) {
7368 case MPT2_WARPDRIVE_LC_SSDT:
7369 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7370 "IO Throttling has occurred in the WarpDrive "
7371 "subsystem. Check WarpDrive documentation for "
7372 "additional details.\n", ioc->name);
7373 break;
7374 case MPT2_WARPDRIVE_LC_SSDLW:
7375 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7376 "Program/Erase Cycles for the WarpDrive subsystem "
7377 "in degraded range. Check WarpDrive documentation "
7378 "for additional details.\n", ioc->name);
7379 break;
7380 case MPT2_WARPDRIVE_LC_SSDLF:
7381 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7382 "There are no Program/Erase Cycles for the "
7383 "WarpDrive subsystem. The storage device will be "
7384 "in read-only mode. Check WarpDrive documentation "
7385 "for additional details.\n", ioc->name);
7386 break;
7387 case MPT2_WARPDRIVE_LC_BRMF:
7388 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7389 "The Backup Rail Monitor has failed on the "
7390 "WarpDrive subsystem. Check WarpDrive "
7391 "documentation for additional details.\n",
7392 ioc->name);
7393 break;
7394 }
7395
7396 break;
7397 }
Eric Moore635374e2009-03-09 01:21:12 -06007398 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7399 case MPI2_EVENT_IR_OPERATION_STATUS:
7400 case MPI2_EVENT_SAS_DISCOVERY:
7401 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007402 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007403 break;
7404
7405 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307406 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007407 }
7408
7409 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7410 if (!fw_event) {
7411 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7412 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307413 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007414 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307415 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7416 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007417 if (!fw_event->event_data) {
7418 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7419 ioc->name, __FILE__, __LINE__, __func__);
7420 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307421 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007422 }
7423
7424 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307425 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007426 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307427 fw_event->VF_ID = mpi_reply->VF_ID;
7428 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007429 fw_event->event = event;
7430 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307431 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007432}
7433
7434/* shost template */
7435static struct scsi_host_template scsih_driver_template = {
7436 .module = THIS_MODULE,
7437 .name = "Fusion MPT SAS Host",
7438 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007439 .queuecommand = _scsih_qcmd,
7440 .target_alloc = _scsih_target_alloc,
7441 .slave_alloc = _scsih_slave_alloc,
7442 .slave_configure = _scsih_slave_configure,
7443 .target_destroy = _scsih_target_destroy,
7444 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307445 .scan_finished = _scsih_scan_finished,
7446 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007447 .change_queue_depth = _scsih_change_queue_depth,
7448 .change_queue_type = _scsih_change_queue_type,
7449 .eh_abort_handler = _scsih_abort,
7450 .eh_device_reset_handler = _scsih_dev_reset,
7451 .eh_target_reset_handler = _scsih_target_reset,
7452 .eh_host_reset_handler = _scsih_host_reset,
7453 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007454 .can_queue = 1,
7455 .this_id = -1,
7456 .sg_tablesize = MPT2SAS_SG_DEPTH,
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05307457 .max_sectors = 32767,
Eric Moore635374e2009-03-09 01:21:12 -06007458 .cmd_per_lun = 7,
7459 .use_clustering = ENABLE_CLUSTERING,
7460 .shost_attrs = mpt2sas_host_attrs,
7461 .sdev_attrs = mpt2sas_dev_attrs,
7462};
7463
7464/**
7465 * _scsih_expander_node_remove - removing expander device from list.
7466 * @ioc: per adapter object
7467 * @sas_expander: the sas_device object
7468 * Context: Calling function should acquire ioc->sas_node_lock.
7469 *
7470 * Removing object and freeing associated memory from the
7471 * ioc->sas_expander_list.
7472 *
7473 * Return nothing.
7474 */
7475static void
7476_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7477 struct _sas_node *sas_expander)
7478{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307479 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007480
7481 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307482 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007483 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307484 if (ioc->shost_recovery)
7485 return;
Eric Moore635374e2009-03-09 01:21:12 -06007486 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307487 SAS_END_DEVICE)
7488 mpt2sas_device_remove(ioc,
7489 mpt2sas_port->remote_identify.sas_address);
7490 else if (mpt2sas_port->remote_identify.device_type ==
7491 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007492 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307493 SAS_FANOUT_EXPANDER_DEVICE)
7494 mpt2sas_expander_remove(ioc,
7495 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007496 }
7497
7498 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307499 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007500
7501 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7502 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7503 sas_expander->handle, (unsigned long long)
7504 sas_expander->sas_address);
7505
Eric Moore635374e2009-03-09 01:21:12 -06007506 kfree(sas_expander->phy);
7507 kfree(sas_expander);
7508}
7509
7510/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307511 * _scsih_ir_shutdown - IR shutdown notification
7512 * @ioc: per adapter object
7513 *
7514 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7515 * the host system is shutting down.
7516 *
7517 * Return nothing.
7518 */
7519static void
7520_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7521{
7522 Mpi2RaidActionRequest_t *mpi_request;
7523 Mpi2RaidActionReply_t *mpi_reply;
7524 u16 smid;
7525
7526 /* is IR firmware build loaded ? */
7527 if (!ioc->ir_firmware)
7528 return;
7529
7530 /* are there any volumes ? */
7531 if (list_empty(&ioc->raid_device_list))
7532 return;
7533
7534 mutex_lock(&ioc->scsih_cmds.mutex);
7535
7536 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7537 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7538 ioc->name, __func__);
7539 goto out;
7540 }
7541 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7542
7543 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7544 if (!smid) {
7545 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7546 ioc->name, __func__);
7547 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7548 goto out;
7549 }
7550
7551 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7552 ioc->scsih_cmds.smid = smid;
7553 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7554
7555 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7556 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7557
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307558 if (!ioc->hide_ir_msg)
7559 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307560 init_completion(&ioc->scsih_cmds.done);
7561 mpt2sas_base_put_smid_default(ioc, smid);
7562 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7563
7564 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7565 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7566 ioc->name, __func__);
7567 goto out;
7568 }
7569
7570 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7571 mpi_reply = ioc->scsih_cmds.reply;
7572
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307573 if (!ioc->hide_ir_msg)
7574 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7575 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7576 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7577 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307578 }
7579
7580 out:
7581 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7582 mutex_unlock(&ioc->scsih_cmds.mutex);
7583}
7584
7585/**
7586 * _scsih_shutdown - routine call during system shutdown
7587 * @pdev: PCI device struct
7588 *
7589 * Return nothing.
7590 */
7591static void
7592_scsih_shutdown(struct pci_dev *pdev)
7593{
7594 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7595 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307596 struct workqueue_struct *wq;
7597 unsigned long flags;
7598
7599 ioc->remove_host = 1;
7600 _scsih_fw_event_cleanup_queue(ioc);
7601
7602 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7603 wq = ioc->firmware_event_thread;
7604 ioc->firmware_event_thread = NULL;
7605 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7606 if (wq)
7607 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307608
7609 _scsih_ir_shutdown(ioc);
7610 mpt2sas_base_detach(ioc);
7611}
7612
7613/**
Eric Moored5d135b2009-05-18 13:02:08 -06007614 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007615 * @pdev: PCI device struct
7616 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307617 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007618 * Return nothing.
7619 */
7620static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007621_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007622{
7623 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7624 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307625 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307626 struct _raid_device *raid_device, *next;
7627 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007628 struct workqueue_struct *wq;
7629 unsigned long flags;
7630
7631 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307632 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007633
7634 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7635 wq = ioc->firmware_event_thread;
7636 ioc->firmware_event_thread = NULL;
7637 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7638 if (wq)
7639 destroy_workqueue(wq);
7640
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307641 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307642 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307643 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7644 list) {
7645 if (raid_device->starget) {
7646 sas_target_priv_data =
7647 raid_device->starget->hostdata;
7648 sas_target_priv_data->deleted = 1;
7649 scsi_remove_target(&raid_device->starget->dev);
7650 }
7651 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7652 "(0x%016llx)\n", ioc->name, raid_device->handle,
7653 (unsigned long long) raid_device->wwid);
7654 _scsih_raid_device_remove(ioc, raid_device);
7655 }
7656
Eric Moore635374e2009-03-09 01:21:12 -06007657 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307658 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007659 &ioc->sas_hba.sas_port_list, port_list) {
7660 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307661 SAS_END_DEVICE)
7662 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007663 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307664 else if (mpt2sas_port->remote_identify.device_type ==
7665 SAS_EDGE_EXPANDER_DEVICE ||
7666 mpt2sas_port->remote_identify.device_type ==
7667 SAS_FANOUT_EXPANDER_DEVICE)
7668 mpt2sas_expander_remove(ioc,
7669 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007670 }
7671
7672 /* free phys attached to the sas_host */
7673 if (ioc->sas_hba.num_phys) {
7674 kfree(ioc->sas_hba.phy);
7675 ioc->sas_hba.phy = NULL;
7676 ioc->sas_hba.num_phys = 0;
7677 }
7678
7679 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307680 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007681 list_del(&ioc->list);
7682 scsi_remove_host(shost);
7683 scsi_host_put(shost);
7684}
7685
7686/**
7687 * _scsih_probe_boot_devices - reports 1st device
7688 * @ioc: per adapter object
7689 *
7690 * If specified in bios page 2, this routine reports the 1st
7691 * device scsi-ml or sas transport for persistent boot device
7692 * purposes. Please refer to function _scsih_determine_boot_device()
7693 */
7694static void
7695_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7696{
7697 u8 is_raid;
7698 void *device;
7699 struct _sas_device *sas_device;
7700 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307701 u16 handle;
7702 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007703 u64 sas_address;
7704 unsigned long flags;
7705 int rc;
7706
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307707 /* no Bios, return immediately */
7708 if (!ioc->bios_pg3.BiosVersion)
7709 return;
7710
Eric Moore635374e2009-03-09 01:21:12 -06007711 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307712 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007713 if (ioc->req_boot_device.device) {
7714 device = ioc->req_boot_device.device;
7715 is_raid = ioc->req_boot_device.is_raid;
7716 } else if (ioc->req_alt_boot_device.device) {
7717 device = ioc->req_alt_boot_device.device;
7718 is_raid = ioc->req_alt_boot_device.is_raid;
7719 } else if (ioc->current_boot_device.device) {
7720 device = ioc->current_boot_device.device;
7721 is_raid = ioc->current_boot_device.is_raid;
7722 }
7723
7724 if (!device)
7725 return;
7726
7727 if (is_raid) {
7728 raid_device = device;
7729 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7730 raid_device->id, 0);
7731 if (rc)
7732 _scsih_raid_device_remove(ioc, raid_device);
7733 } else {
7734 sas_device = device;
7735 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307736 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007737 sas_address = sas_device->sas_address;
7738 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7739 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7740 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307741
7742 if (ioc->hide_drives)
7743 return;
Eric Moore635374e2009-03-09 01:21:12 -06007744 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307745 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007746 _scsih_sas_device_remove(ioc, sas_device);
7747 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307748 if (!ioc->is_driver_loading)
7749 mpt2sas_transport_port_remove(ioc, sas_address,
7750 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007751 _scsih_sas_device_remove(ioc, sas_device);
7752 }
7753 }
7754}
7755
7756/**
7757 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7758 * @ioc: per adapter object
7759 *
7760 * Called during initial loading of the driver.
7761 */
7762static void
7763_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7764{
7765 struct _raid_device *raid_device, *raid_next;
7766 int rc;
7767
7768 list_for_each_entry_safe(raid_device, raid_next,
7769 &ioc->raid_device_list, list) {
7770 if (raid_device->starget)
7771 continue;
7772 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7773 raid_device->id, 0);
7774 if (rc)
7775 _scsih_raid_device_remove(ioc, raid_device);
7776 }
7777}
7778
7779/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307780 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007781 * @ioc: per adapter object
7782 *
7783 * Called during initial loading of the driver.
7784 */
7785static void
7786_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7787{
7788 struct _sas_device *sas_device, *next;
7789 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007790
7791 /* SAS Device List */
7792 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7793 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007794
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307795 if (ioc->hide_drives)
7796 continue;
7797
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307798 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7799 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307800 list_del(&sas_device->list);
7801 kfree(sas_device);
7802 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007803 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307804 if (!ioc->is_driver_loading)
7805 mpt2sas_transport_port_remove(ioc,
7806 sas_device->sas_address,
7807 sas_device->sas_address_parent);
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307808 list_del(&sas_device->list);
7809 kfree(sas_device);
7810 continue;
7811
Eric Moore635374e2009-03-09 01:21:12 -06007812 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307813 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7814 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7815 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007816 }
7817}
7818
7819/**
7820 * _scsih_probe_devices - probing for devices
7821 * @ioc: per adapter object
7822 *
7823 * Called during initial loading of the driver.
7824 */
7825static void
7826_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7827{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307828 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06007829
7830 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7831 return; /* return when IOC doesn't support initiator mode */
7832
7833 _scsih_probe_boot_devices(ioc);
7834
7835 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307836 volume_mapping_flags =
7837 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7838 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7839 if (volume_mapping_flags ==
7840 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06007841 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307842 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007843 } else {
Eric Moore635374e2009-03-09 01:21:12 -06007844 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307845 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007846 }
7847 } else
7848 _scsih_probe_sas(ioc);
7849}
7850
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307851
7852/**
7853 * _scsih_scan_start - scsi lld callback for .scan_start
7854 * @shost: SCSI host pointer
7855 *
7856 * The shost has the ability to discover targets on its own instead
7857 * of scanning the entire bus. In our implemention, we will kick off
7858 * firmware discovery.
7859 */
7860static void
7861_scsih_scan_start(struct Scsi_Host *shost)
7862{
7863 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7864 int rc;
7865
7866 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7867 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
7868
7869 ioc->start_scan = 1;
7870 rc = mpt2sas_port_enable(ioc);
7871
7872 if (rc != 0)
7873 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
7874}
7875
7876/**
7877 * _scsih_scan_finished - scsi lld callback for .scan_finished
7878 * @shost: SCSI host pointer
7879 * @time: elapsed time of the scan in jiffies
7880 *
7881 * This function will be called periodically until it returns 1 with the
7882 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7883 * we wait for firmware discovery to complete, then return 1.
7884 */
7885static int
7886_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7887{
7888 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7889
7890 if (time >= (300 * HZ)) {
7891 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7892 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
7893 "(timeout=300s)\n", ioc->name);
7894 ioc->is_driver_loading = 0;
7895 return 1;
7896 }
7897
7898 if (ioc->start_scan)
7899 return 0;
7900
7901 if (ioc->start_scan_failed) {
7902 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
7903 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
7904 ioc->is_driver_loading = 0;
7905 ioc->wait_for_discovery_to_complete = 0;
7906 ioc->remove_host = 1;
7907 return 1;
7908 }
7909
7910 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
7911 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7912
7913 if (ioc->wait_for_discovery_to_complete) {
7914 ioc->wait_for_discovery_to_complete = 0;
7915 _scsih_probe_devices(ioc);
7916 }
7917 mpt2sas_base_start_watchdog(ioc);
7918 ioc->is_driver_loading = 0;
7919 return 1;
7920}
7921
7922
Eric Moore635374e2009-03-09 01:21:12 -06007923/**
Eric Moored5d135b2009-05-18 13:02:08 -06007924 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06007925 * @pdev: PCI device struct
7926 * @id: pci device id
7927 *
7928 * Returns 0 success, anything else error.
7929 */
7930static int
Eric Moored5d135b2009-05-18 13:02:08 -06007931_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06007932{
7933 struct MPT2SAS_ADAPTER *ioc;
7934 struct Scsi_Host *shost;
7935
7936 shost = scsi_host_alloc(&scsih_driver_template,
7937 sizeof(struct MPT2SAS_ADAPTER));
7938 if (!shost)
7939 return -ENODEV;
7940
7941 /* init local params */
7942 ioc = shost_priv(shost);
7943 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7944 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06007945 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06007946 ioc->shost = shost;
7947 ioc->id = mpt_ids++;
7948 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7949 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307950 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7951 ioc->is_warpdrive = 1;
7952 ioc->hide_ir_msg = 1;
7953 } else
7954 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06007955 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7956 ioc->tm_cb_idx = tm_cb_idx;
7957 ioc->ctl_cb_idx = ctl_cb_idx;
7958 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307959 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007960 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05307961 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007962 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307963 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307964 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307965 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007966 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05307967 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06007968 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05307969 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06007970 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7971 spin_lock_init(&ioc->scsi_lookup_lock);
7972 spin_lock_init(&ioc->sas_device_lock);
7973 spin_lock_init(&ioc->sas_node_lock);
7974 spin_lock_init(&ioc->fw_event_lock);
7975 spin_lock_init(&ioc->raid_device_lock);
7976
7977 INIT_LIST_HEAD(&ioc->sas_device_list);
7978 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7979 INIT_LIST_HEAD(&ioc->sas_expander_list);
7980 INIT_LIST_HEAD(&ioc->fw_event_list);
7981 INIT_LIST_HEAD(&ioc->raid_device_list);
7982 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307983 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307984 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06007985
7986 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06007987 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06007988 shost->max_lun = max_lun;
7989 shost->transportt = mpt2sas_transport_template;
7990 shost->unique_id = ioc->id;
7991
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05307992 if (max_sectors != 0xFFFF) {
7993 if (max_sectors < 64) {
7994 shost->max_sectors = 64;
7995 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7996 "for max_sectors, range is 64 to 8192. Assigning "
7997 "value of 64.\n", ioc->name, max_sectors);
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05307998 } else if (max_sectors > 32767) {
7999 shost->max_sectors = 32767;
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308000 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
8001 "for max_sectors, range is 64 to 8192. Assigning "
nagalakshmi.nandigama@lsi.com9ac49d32011-12-01 07:52:08 +05308002 "default value of 32767.\n", ioc->name,
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05308003 max_sectors);
8004 } else {
8005 shost->max_sectors = max_sectors & 0xFFFE;
8006 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
8007 "set to %d\n", ioc->name, shost->max_sectors);
8008 }
8009 }
8010
Eric Moore635374e2009-03-09 01:21:12 -06008011 if ((scsi_add_host(shost, &pdev->dev))) {
8012 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8013 ioc->name, __FILE__, __LINE__, __func__);
8014 list_del(&ioc->list);
8015 goto out_add_shost_fail;
8016 }
8017
Eric Moore3c621b32009-05-18 12:59:41 -06008018 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06008019 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308020 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06008021
Eric Moore635374e2009-03-09 01:21:12 -06008022 /* event thread */
8023 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
8024 "fw_event%d", ioc->id);
8025 ioc->firmware_event_thread = create_singlethread_workqueue(
8026 ioc->firmware_event_name);
8027 if (!ioc->firmware_event_thread) {
8028 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8029 ioc->name, __FILE__, __LINE__, __func__);
8030 goto out_thread_fail;
8031 }
8032
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308033 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008034 if ((mpt2sas_base_attach(ioc))) {
8035 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8036 ioc->name, __FILE__, __LINE__, __func__);
8037 goto out_attach_fail;
8038 }
8039
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308040 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308041 if (ioc->is_warpdrive) {
8042 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8043 ioc->hide_drives = 0;
8044 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8045 ioc->hide_drives = 1;
8046 else {
8047 if (_scsih_get_num_volumes(ioc))
8048 ioc->hide_drives = 1;
8049 else
8050 ioc->hide_drives = 0;
8051 }
8052 } else
8053 ioc->hide_drives = 0;
8054
Eric Moore635374e2009-03-09 01:21:12 -06008055 _scsih_probe_devices(ioc);
8056 return 0;
8057
8058 out_attach_fail:
8059 destroy_workqueue(ioc->firmware_event_thread);
8060 out_thread_fail:
8061 list_del(&ioc->list);
8062 scsi_remove_host(shost);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308063 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008064 out_add_shost_fail:
8065 return -ENODEV;
8066}
8067
8068#ifdef CONFIG_PM
8069/**
Eric Moored5d135b2009-05-18 13:02:08 -06008070 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008071 * @pdev: PCI device struct
8072 * @state: PM state change to (usually PCI_D3)
8073 *
8074 * Returns 0 success, anything else error.
8075 */
8076static int
Eric Moored5d135b2009-05-18 13:02:08 -06008077_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008078{
8079 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8080 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308081 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008082
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308083 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008084 scsi_block_requests(shost);
8085 device_state = pci_choose_state(pdev, state);
8086 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8087 "operating state [D%d]\n", ioc->name, pdev,
8088 pci_name(pdev), device_state);
8089
8090 mpt2sas_base_free_resources(ioc);
8091 pci_save_state(pdev);
8092 pci_disable_device(pdev);
8093 pci_set_power_state(pdev, device_state);
8094 return 0;
8095}
8096
8097/**
Eric Moored5d135b2009-05-18 13:02:08 -06008098 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008099 * @pdev: PCI device struct
8100 *
8101 * Returns 0 success, anything else error.
8102 */
8103static int
Eric Moored5d135b2009-05-18 13:02:08 -06008104_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008105{
8106 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8107 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308108 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008109 int r;
8110
8111 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8112 "operating state [D%d]\n", ioc->name, pdev,
8113 pci_name(pdev), device_state);
8114
8115 pci_set_power_state(pdev, PCI_D0);
8116 pci_enable_wake(pdev, PCI_D0, 0);
8117 pci_restore_state(pdev);
8118 ioc->pdev = pdev;
8119 r = mpt2sas_base_map_resources(ioc);
8120 if (r)
8121 return r;
8122
8123 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8124 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308125 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008126 return 0;
8127}
8128#endif /* CONFIG_PM */
8129
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308130/**
8131 * _scsih_pci_error_detected - Called when a PCI error is detected.
8132 * @pdev: PCI device struct
8133 * @state: PCI channel state
8134 *
8135 * Description: Called when a PCI error is detected.
8136 *
8137 * Return value:
8138 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8139 */
8140static pci_ers_result_t
8141_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8142{
8143 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8144 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8145
8146 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8147 ioc->name, state);
8148
8149 switch (state) {
8150 case pci_channel_io_normal:
8151 return PCI_ERS_RESULT_CAN_RECOVER;
8152 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008153 /* Fatal error, prepare for slot reset */
8154 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308155 scsi_block_requests(ioc->shost);
8156 mpt2sas_base_stop_watchdog(ioc);
8157 mpt2sas_base_free_resources(ioc);
8158 return PCI_ERS_RESULT_NEED_RESET;
8159 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008160 /* Permanent error, prepare for device removal */
8161 ioc->pci_error_recovery = 1;
8162 mpt2sas_base_stop_watchdog(ioc);
8163 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308164 return PCI_ERS_RESULT_DISCONNECT;
8165 }
8166 return PCI_ERS_RESULT_NEED_RESET;
8167}
8168
8169/**
8170 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8171 * @pdev: PCI device struct
8172 *
8173 * Description: This routine is called by the pci error recovery
8174 * code after the PCI slot has been reset, just before we
8175 * should resume normal operations.
8176 */
8177static pci_ers_result_t
8178_scsih_pci_slot_reset(struct pci_dev *pdev)
8179{
8180 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8181 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8182 int rc;
8183
8184 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8185 ioc->name);
8186
Eric Moore3cb54692010-07-08 14:44:34 -06008187 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308188 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008189 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308190 rc = mpt2sas_base_map_resources(ioc);
8191 if (rc)
8192 return PCI_ERS_RESULT_DISCONNECT;
8193
8194
8195 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8196 FORCE_BIG_HAMMER);
8197
8198 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8199 (rc == 0) ? "success" : "failed");
8200
8201 if (!rc)
8202 return PCI_ERS_RESULT_RECOVERED;
8203 else
8204 return PCI_ERS_RESULT_DISCONNECT;
8205}
8206
8207/**
8208 * _scsih_pci_resume() - resume normal ops after PCI reset
8209 * @pdev: pointer to PCI device
8210 *
8211 * Called when the error recovery driver tells us that its
8212 * OK to resume normal operation. Use completion to allow
8213 * halted scsi ops to resume.
8214 */
8215static void
8216_scsih_pci_resume(struct pci_dev *pdev)
8217{
8218 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8219 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8220
8221 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8222
8223 pci_cleanup_aer_uncorrect_error_status(pdev);
8224 mpt2sas_base_start_watchdog(ioc);
8225 scsi_unblock_requests(ioc->shost);
8226}
8227
8228/**
8229 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8230 * @pdev: pointer to PCI device
8231 */
8232static pci_ers_result_t
8233_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8234{
8235 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8236 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8237
8238 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8239 ioc->name);
8240
8241 /* TODO - dump whatever for debugging purposes */
8242
8243 /* Request a slot reset. */
8244 return PCI_ERS_RESULT_NEED_RESET;
8245}
8246
8247static struct pci_error_handlers _scsih_err_handler = {
8248 .error_detected = _scsih_pci_error_detected,
8249 .mmio_enabled = _scsih_pci_mmio_enabled,
8250 .slot_reset = _scsih_pci_slot_reset,
8251 .resume = _scsih_pci_resume,
8252};
Eric Moore635374e2009-03-09 01:21:12 -06008253
8254static struct pci_driver scsih_driver = {
8255 .name = MPT2SAS_DRIVER_NAME,
8256 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008257 .probe = _scsih_probe,
8258 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05308259 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308260 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008261#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008262 .suspend = _scsih_suspend,
8263 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008264#endif
8265};
8266
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308267/* raid transport support */
8268static struct raid_function_template mpt2sas_raid_functions = {
8269 .cookie = &scsih_driver_template,
8270 .is_raid = _scsih_is_raid,
8271 .get_resync = _scsih_get_resync,
8272 .get_state = _scsih_get_state,
8273};
Eric Moore635374e2009-03-09 01:21:12 -06008274
8275/**
Eric Moored5d135b2009-05-18 13:02:08 -06008276 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008277 *
8278 * Returns 0 success, anything else error.
8279 */
8280static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008281_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008282{
8283 int error;
8284
8285 mpt_ids = 0;
8286 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8287 MPT2SAS_DRIVER_VERSION);
8288
8289 mpt2sas_transport_template =
8290 sas_attach_transport(&mpt2sas_transport_functions);
8291 if (!mpt2sas_transport_template)
8292 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308293 /* raid transport support */
8294 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8295 if (!mpt2sas_raid_template) {
8296 sas_release_transport(mpt2sas_transport_template);
8297 return -ENODEV;
8298 }
Eric Moore635374e2009-03-09 01:21:12 -06008299
8300 mpt2sas_base_initialize_callback_handler();
8301
8302 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008303 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008304
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008305 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008306 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008307
8308 /* base internal commands callback handler */
8309 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308310 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8311 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008312
8313 /* transport internal commands callback handler */
8314 transport_cb_idx = mpt2sas_base_register_callback_handler(
8315 mpt2sas_transport_done);
8316
Kashyap, Desai744090d2009-10-05 15:56:56 +05308317 /* scsih internal commands callback handler */
8318 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8319
Eric Moore635374e2009-03-09 01:21:12 -06008320 /* configuration page API internal commands callback handler */
8321 config_cb_idx = mpt2sas_base_register_callback_handler(
8322 mpt2sas_config_done);
8323
8324 /* ctl module callback handler */
8325 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8326
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308327 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8328 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308329
8330 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8331 _scsih_tm_volume_tr_complete);
8332
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308333 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8334 _scsih_sas_control_complete);
8335
Eric Moore635374e2009-03-09 01:21:12 -06008336 mpt2sas_ctl_init();
8337
8338 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308339 if (error) {
8340 /* raid transport support */
8341 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008342 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308343 }
Eric Moore635374e2009-03-09 01:21:12 -06008344
8345 return error;
8346}
8347
8348/**
Eric Moored5d135b2009-05-18 13:02:08 -06008349 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008350 *
8351 * Returns 0 success, anything else error.
8352 */
8353static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008354_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008355{
8356 printk(KERN_INFO "mpt2sas version %s unloading\n",
8357 MPT2SAS_DRIVER_VERSION);
8358
8359 pci_unregister_driver(&scsih_driver);
8360
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308361 mpt2sas_ctl_exit();
8362
Eric Moore635374e2009-03-09 01:21:12 -06008363 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8364 mpt2sas_base_release_callback_handler(tm_cb_idx);
8365 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308366 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008367 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308368 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008369 mpt2sas_base_release_callback_handler(config_cb_idx);
8370 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8371
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308372 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308373 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308374 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8375
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308376 /* raid transport support */
8377 raid_class_release(mpt2sas_raid_template);
8378 sas_release_transport(mpt2sas_transport_template);
8379
Eric Moore635374e2009-03-09 01:21:12 -06008380}
8381
Eric Moored5d135b2009-05-18 13:02:08 -06008382module_init(_scsih_init);
8383module_exit(_scsih_exit);