blob: 5060634f7651aa2b9cd59c05df6db83e863fb620 [file] [log] [blame]
Eric Moore635374e2009-03-09 01:21:12 -06001/*
2 * Scsi Host Layer for MPT (Message Passing Technology) based controllers
3 *
4 * This code is based on drivers/scsi/mpt2sas/mpt2_scsih.c
Kashyap, Desai31b7f2e2010-03-17 16:28:04 +05305 * Copyright (C) 2007-2010 LSI Corporation
Eric Moore635374e2009-03-09 01:21:12 -06006 * (mailto:DL-MPTFusionLinux@lsi.com)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * NO WARRANTY
19 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
20 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
21 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
22 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
23 * solely responsible for determining the appropriateness of using and
24 * distributing the Program and assumes all risks associated with its
25 * exercise of rights under this Agreement, including but not limited to
26 * the risks and costs of program errors, damage to or loss of data,
27 * programs or equipment, and unavailability or interruption of operations.
28
29 * DISCLAIMER OF LIABILITY
30 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
31 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
33 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
35 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
36 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES
37
38 * You should have received a copy of the GNU General Public License
39 * along with this program; if not, write to the Free Software
40 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
41 * USA.
42 */
43
Eric Moore635374e2009-03-09 01:21:12 -060044#include <linux/module.h>
45#include <linux/kernel.h>
46#include <linux/init.h>
47#include <linux/errno.h>
48#include <linux/blkdev.h>
49#include <linux/sched.h>
50#include <linux/workqueue.h>
51#include <linux/delay.h>
52#include <linux/pci.h>
53#include <linux/interrupt.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053054#include <linux/aer.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053055#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090056#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060057
58#include "mpt2sas_base.h"
59
60MODULE_AUTHOR(MPT2SAS_AUTHOR);
61MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
62MODULE_LICENSE("GPL");
63MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
64
65#define RAID_CHANNEL 1
66
67/* forward proto's */
68static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
69 struct _sas_node *sas_expander);
70static void _firmware_event_work(struct work_struct *work);
71
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053072static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
73
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053074static void _scsih_scan_start(struct Scsi_Host *shost);
75static int _scsih_scan_finished(struct Scsi_Host *shost, unsigned long time);
76
Eric Moore635374e2009-03-09 01:21:12 -060077/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060078LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060079
80/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060081static u8 scsi_io_cb_idx = -1;
82static u8 tm_cb_idx = -1;
83static u8 ctl_cb_idx = -1;
84static u8 base_cb_idx = -1;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +053085static u8 port_enable_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060086static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053087static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060088static u8 config_cb_idx = -1;
89static int mpt_ids;
90
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053091static u8 tm_tr_cb_idx = -1 ;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053092static u8 tm_tr_volume_cb_idx = -1 ;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053093static u8 tm_sas_control_cb_idx = -1;
94
Eric Moore635374e2009-03-09 01:21:12 -060095/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060096static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060097MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
98 "(default=0)");
99
Kashyap, Desaia3e1e552011-06-14 10:56:12 +0530100static ushort max_sectors = 0xFFFF;
101module_param(max_sectors, ushort, 0);
102MODULE_PARM_DESC(max_sectors, "max sectors, range 64 to 8192 default=8192");
103
Eric Moore635374e2009-03-09 01:21:12 -0600104/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
105#define MPT2SAS_MAX_LUN (16895)
106static int max_lun = MPT2SAS_MAX_LUN;
107module_param(max_lun, int, 0);
108MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
109
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530110/* diag_buffer_enable is bitwise
111 * bit 0 set = TRACE
112 * bit 1 set = SNAPSHOT
113 * bit 2 set = EXTENDED
114 *
115 * Either bit can be set, or both
116 */
117static int diag_buffer_enable = -1;
118module_param(diag_buffer_enable, int, 0);
119MODULE_PARM_DESC(diag_buffer_enable, " post diag buffers "
120 "(TRACE=1/SNAPSHOT=2/EXTENDED=4/default=0)");
121
Eric Moore635374e2009-03-09 01:21:12 -0600122/**
123 * struct sense_info - common structure for obtaining sense keys
124 * @skey: sense key
125 * @asc: additional sense code
126 * @ascq: additional sense code qualifier
127 */
128struct sense_info {
129 u8 skey;
130 u8 asc;
131 u8 ascq;
132};
133
134
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530135#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530136#define MPT2SAS_PORT_ENABLE_COMPLETE (0xFFFD)
137#define MPT2SAS_REMOVE_UNRESPONDING_DEVICES (0xFFFF)
Eric Moore635374e2009-03-09 01:21:12 -0600138/**
139 * struct fw_event_work - firmware event struct
140 * @list: link list framework
141 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530142 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600143 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530144 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600145 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530146 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600147 * @ignore: flag meaning this event has been marked to ignore
148 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
149 * @event_data: reply event data payload follows
150 *
151 * This object stored on ioc->fw_event_list.
152 */
153struct fw_event_work {
154 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530155 u8 cancel_pending_work;
156 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600157 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530158 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600159 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530160 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600161 u8 ignore;
162 u16 event;
163 void *event_data;
164};
165
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530166/* raid transport support */
167static struct raid_template *mpt2sas_raid_template;
168
Eric Moore635374e2009-03-09 01:21:12 -0600169/**
170 * struct _scsi_io_transfer - scsi io transfer
171 * @handle: sas device handle (assigned by firmware)
172 * @is_raid: flag set for hidden raid components
173 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
174 * @data_length: data transfer length
175 * @data_dma: dma pointer to data
176 * @sense: sense data
177 * @lun: lun number
178 * @cdb_length: cdb length
179 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600180 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530181 * @VF_ID: virtual function id
182 * @VP_ID: virtual port id
183 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600184 * @sense_length: sense length
185 * @ioc_status: ioc status
186 * @scsi_state: scsi state
187 * @scsi_status: scsi staus
188 * @log_info: log information
189 * @transfer_length: data length transfer when there is a reply message
190 *
191 * Used for sending internal scsi commands to devices within this module.
192 * Refer to _scsi_send_scsi_io().
193 */
194struct _scsi_io_transfer {
195 u16 handle;
196 u8 is_raid;
197 enum dma_data_direction dir;
198 u32 data_length;
199 dma_addr_t data_dma;
200 u8 sense[SCSI_SENSE_BUFFERSIZE];
201 u32 lun;
202 u8 cdb_length;
203 u8 cdb[32];
204 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530205 u8 VF_ID;
206 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600207 u8 valid_reply;
208 /* the following bits are only valid when 'valid_reply = 1' */
209 u32 sense_length;
210 u16 ioc_status;
211 u8 scsi_state;
212 u8 scsi_status;
213 u32 log_info;
214 u32 transfer_length;
215};
216
217/*
218 * The pci device ids are defined in mpi/mpi2_cnfg.h.
219 */
220static struct pci_device_id scsih_pci_table[] = {
221 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
222 PCI_ANY_ID, PCI_ANY_ID },
223 /* Falcon ~ 2008*/
224 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
225 PCI_ANY_ID, PCI_ANY_ID },
226 /* Liberator ~ 2108 */
227 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
228 PCI_ANY_ID, PCI_ANY_ID },
229 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
230 PCI_ANY_ID, PCI_ANY_ID },
231 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
232 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530233 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600234 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
235 PCI_ANY_ID, PCI_ANY_ID },
236 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
237 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530238 /* Thunderbolt ~ 2208 */
239 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
240 PCI_ANY_ID, PCI_ANY_ID },
241 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
242 PCI_ANY_ID, PCI_ANY_ID },
243 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
244 PCI_ANY_ID, PCI_ANY_ID },
245 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
246 PCI_ANY_ID, PCI_ANY_ID },
247 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
248 PCI_ANY_ID, PCI_ANY_ID },
249 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
250 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530251 /* Mustang ~ 2308 */
252 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530253 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530254 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
255 PCI_ANY_ID, PCI_ANY_ID },
256 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530257 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530258 /* SSS6200 */
259 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
260 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600261 {0} /* Terminating entry */
262};
263MODULE_DEVICE_TABLE(pci, scsih_pci_table);
264
265/**
Eric Moored5d135b2009-05-18 13:02:08 -0600266 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600267 *
268 * Note: The logging levels are defined in mpt2sas_debug.h.
269 */
270static int
Eric Moored5d135b2009-05-18 13:02:08 -0600271_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600272{
273 int ret = param_set_int(val, kp);
274 struct MPT2SAS_ADAPTER *ioc;
275
276 if (ret)
277 return ret;
278
279 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600280 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600281 ioc->logging_level = logging_level;
282 return 0;
283}
Eric Moored5d135b2009-05-18 13:02:08 -0600284module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600285 &logging_level, 0644);
286
287/**
288 * _scsih_srch_boot_sas_address - search based on sas_address
289 * @sas_address: sas address
290 * @boot_device: boot device object from bios page 2
291 *
292 * Returns 1 when there's a match, 0 means no match.
293 */
294static inline int
295_scsih_srch_boot_sas_address(u64 sas_address,
296 Mpi2BootDeviceSasWwid_t *boot_device)
297{
298 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
299}
300
301/**
302 * _scsih_srch_boot_device_name - search based on device name
303 * @device_name: device name specified in INDENTIFY fram
304 * @boot_device: boot device object from bios page 2
305 *
306 * Returns 1 when there's a match, 0 means no match.
307 */
308static inline int
309_scsih_srch_boot_device_name(u64 device_name,
310 Mpi2BootDeviceDeviceName_t *boot_device)
311{
312 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
313}
314
315/**
316 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
317 * @enclosure_logical_id: enclosure logical id
318 * @slot_number: slot number
319 * @boot_device: boot device object from bios page 2
320 *
321 * Returns 1 when there's a match, 0 means no match.
322 */
323static inline int
324_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
325 Mpi2BootDeviceEnclosureSlot_t *boot_device)
326{
327 return (enclosure_logical_id == le64_to_cpu(boot_device->
328 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
329 SlotNumber)) ? 1 : 0;
330}
331
332/**
333 * _scsih_is_boot_device - search for matching boot device.
334 * @sas_address: sas address
335 * @device_name: device name specified in INDENTIFY fram
336 * @enclosure_logical_id: enclosure logical id
337 * @slot_number: slot number
338 * @form: specifies boot device form
339 * @boot_device: boot device object from bios page 2
340 *
341 * Returns 1 when there's a match, 0 means no match.
342 */
343static int
344_scsih_is_boot_device(u64 sas_address, u64 device_name,
345 u64 enclosure_logical_id, u16 slot, u8 form,
346 Mpi2BiosPage2BootDevice_t *boot_device)
347{
348 int rc = 0;
349
350 switch (form) {
351 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
352 if (!sas_address)
353 break;
354 rc = _scsih_srch_boot_sas_address(
355 sas_address, &boot_device->SasWwid);
356 break;
357 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
358 if (!enclosure_logical_id)
359 break;
360 rc = _scsih_srch_boot_encl_slot(
361 enclosure_logical_id,
362 slot, &boot_device->EnclosureSlot);
363 break;
364 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
365 if (!device_name)
366 break;
367 rc = _scsih_srch_boot_device_name(
368 device_name, &boot_device->DeviceName);
369 break;
370 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
371 break;
372 }
373
374 return rc;
375}
376
377/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530378 * _scsih_get_sas_address - set the sas_address for given device handle
379 * @handle: device handle
380 * @sas_address: sas address
381 *
382 * Returns 0 success, non-zero when failure
383 */
384static int
385_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
386 u64 *sas_address)
387{
388 Mpi2SasDevicePage0_t sas_device_pg0;
389 Mpi2ConfigReply_t mpi_reply;
390 u32 ioc_status;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530391 *sas_address = 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530392
393 if (handle <= ioc->sas_hba.num_phys) {
394 *sas_address = ioc->sas_hba.sas_address;
395 return 0;
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530396 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530397
398 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
399 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530400 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
401 __FILE__, __LINE__, __func__);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530402 return -ENXIO;
403 }
404
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530405 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
406 if (ioc_status == MPI2_IOCSTATUS_SUCCESS) {
407 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
408 return 0;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530409 }
410
nagalakshmi.nandigama@lsi.com24f09b52011-10-19 15:36:47 +0530411 /* we hit this becuase the given parent handle doesn't exist */
412 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
413 return -ENXIO;
414 /* else error case */
415 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x), "
416 "failure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
417 __FILE__, __LINE__, __func__);
418 return -EIO;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530419}
420
421/**
Eric Moore635374e2009-03-09 01:21:12 -0600422 * _scsih_determine_boot_device - determine boot device.
423 * @ioc: per adapter object
424 * @device: either sas_device or raid_device object
425 * @is_raid: [flag] 1 = raid object, 0 = sas object
426 *
427 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300428 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600429 * There are primary, alternate, and current entries in bios page 2. The order
430 * priority is primary, alternate, then current. This routine saves
431 * the corresponding device object and is_raid flag in the ioc object.
432 * The saved data to be used later in _scsih_probe_boot_devices().
433 */
434static void
435_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
436 void *device, u8 is_raid)
437{
438 struct _sas_device *sas_device;
439 struct _raid_device *raid_device;
440 u64 sas_address;
441 u64 device_name;
442 u64 enclosure_logical_id;
443 u16 slot;
444
445 /* only process this function when driver loads */
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +0530446 if (!ioc->is_driver_loading)
447 return;
448
449 /* no Bios, return immediately */
450 if (!ioc->bios_pg3.BiosVersion)
Eric Moore635374e2009-03-09 01:21:12 -0600451 return;
452
453 if (!is_raid) {
454 sas_device = device;
455 sas_address = sas_device->sas_address;
456 device_name = sas_device->device_name;
457 enclosure_logical_id = sas_device->enclosure_logical_id;
458 slot = sas_device->slot;
459 } else {
460 raid_device = device;
461 sas_address = raid_device->wwid;
462 device_name = 0;
463 enclosure_logical_id = 0;
464 slot = 0;
465 }
466
467 if (!ioc->req_boot_device.device) {
468 if (_scsih_is_boot_device(sas_address, device_name,
469 enclosure_logical_id, slot,
470 (ioc->bios_pg2.ReqBootDeviceForm &
471 MPI2_BIOSPAGE2_FORM_MASK),
472 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530473 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600474 "%s: req_boot_device(0x%016llx)\n",
475 ioc->name, __func__,
476 (unsigned long long)sas_address));
477 ioc->req_boot_device.device = device;
478 ioc->req_boot_device.is_raid = is_raid;
479 }
480 }
481
482 if (!ioc->req_alt_boot_device.device) {
483 if (_scsih_is_boot_device(sas_address, device_name,
484 enclosure_logical_id, slot,
485 (ioc->bios_pg2.ReqAltBootDeviceForm &
486 MPI2_BIOSPAGE2_FORM_MASK),
487 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530488 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600489 "%s: req_alt_boot_device(0x%016llx)\n",
490 ioc->name, __func__,
491 (unsigned long long)sas_address));
492 ioc->req_alt_boot_device.device = device;
493 ioc->req_alt_boot_device.is_raid = is_raid;
494 }
495 }
496
497 if (!ioc->current_boot_device.device) {
498 if (_scsih_is_boot_device(sas_address, device_name,
499 enclosure_logical_id, slot,
500 (ioc->bios_pg2.CurrentBootDeviceForm &
501 MPI2_BIOSPAGE2_FORM_MASK),
502 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530503 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600504 "%s: current_boot_device(0x%016llx)\n",
505 ioc->name, __func__,
506 (unsigned long long)sas_address));
507 ioc->current_boot_device.device = device;
508 ioc->current_boot_device.is_raid = is_raid;
509 }
510 }
511}
512
513/**
514 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
515 * @ioc: per adapter object
516 * @sas_address: sas address
517 * Context: Calling function should acquire ioc->sas_device_lock
518 *
519 * This searches for sas_device based on sas_address, then return sas_device
520 * object.
521 */
522struct _sas_device *
523mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
524 u64 sas_address)
525{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530526 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600527
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530528 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
529 if (sas_device->sas_address == sas_address)
530 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600531
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530532 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
533 if (sas_device->sas_address == sas_address)
534 return sas_device;
535
536 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600537}
538
539/**
540 * _scsih_sas_device_find_by_handle - sas device search
541 * @ioc: per adapter object
542 * @handle: sas device handle (assigned by firmware)
543 * Context: Calling function should acquire ioc->sas_device_lock
544 *
545 * This searches for sas_device based on sas_address, then return sas_device
546 * object.
547 */
548static struct _sas_device *
549_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
550{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530551 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600552
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530553 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
554 if (sas_device->handle == handle)
555 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600556
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530557 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
558 if (sas_device->handle == handle)
559 return sas_device;
560
561 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600562}
563
564/**
565 * _scsih_sas_device_remove - remove sas_device from list.
566 * @ioc: per adapter object
567 * @sas_device: the sas_device object
568 * Context: This function will acquire ioc->sas_device_lock.
569 *
570 * Removing object and freeing associated memory from the ioc->sas_device_list.
571 */
572static void
573_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
574 struct _sas_device *sas_device)
575{
576 unsigned long flags;
577
Kashyap, Desai980ead32010-04-08 17:55:22 +0530578 if (!sas_device)
579 return;
580
Eric Moore635374e2009-03-09 01:21:12 -0600581 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530582 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
583 sas_device->sas_address)) {
584 list_del(&sas_device->list);
585 kfree(sas_device);
586 }
Eric Moore635374e2009-03-09 01:21:12 -0600587 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
588}
589
590/**
591 * _scsih_sas_device_add - insert sas_device to the list.
592 * @ioc: per adapter object
593 * @sas_device: the sas_device object
594 * Context: This function will acquire ioc->sas_device_lock.
595 *
596 * Adding new object to the ioc->sas_device_list.
597 */
598static void
599_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
600 struct _sas_device *sas_device)
601{
602 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600603
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530604 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600605 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
606 sas_device->handle, (unsigned long long)sas_device->sas_address));
607
608 spin_lock_irqsave(&ioc->sas_device_lock, flags);
609 list_add_tail(&sas_device->list, &ioc->sas_device_list);
610 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
611
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530612 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530613 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -0600614 _scsih_sas_device_remove(ioc, sas_device);
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +0530615 } else if (!sas_device->starget) {
616 if (!ioc->is_driver_loading)
617 mpt2sas_transport_port_remove(ioc,
618 sas_device->sas_address,
619 sas_device->sas_address_parent);
620 _scsih_sas_device_remove(ioc, sas_device);
621 }
Eric Moore635374e2009-03-09 01:21:12 -0600622}
623
624/**
625 * _scsih_sas_device_init_add - insert sas_device to the list.
626 * @ioc: per adapter object
627 * @sas_device: the sas_device object
628 * Context: This function will acquire ioc->sas_device_lock.
629 *
630 * Adding new object at driver load time to the ioc->sas_device_init_list.
631 */
632static void
633_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
634 struct _sas_device *sas_device)
635{
636 unsigned long flags;
637
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530638 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600639 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
640 sas_device->handle, (unsigned long long)sas_device->sas_address));
641
642 spin_lock_irqsave(&ioc->sas_device_lock, flags);
643 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
644 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
645 _scsih_determine_boot_device(ioc, sas_device, 0);
646}
647
648/**
Eric Moore635374e2009-03-09 01:21:12 -0600649 * _scsih_raid_device_find_by_id - raid device search
650 * @ioc: per adapter object
651 * @id: sas device target id
652 * @channel: sas device channel
653 * Context: Calling function should acquire ioc->raid_device_lock
654 *
655 * This searches for raid_device based on target id, then return raid_device
656 * object.
657 */
658static struct _raid_device *
659_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
660{
661 struct _raid_device *raid_device, *r;
662
663 r = NULL;
664 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
665 if (raid_device->id == id && raid_device->channel == channel) {
666 r = raid_device;
667 goto out;
668 }
669 }
670
671 out:
672 return r;
673}
674
675/**
676 * _scsih_raid_device_find_by_handle - raid device search
677 * @ioc: per adapter object
678 * @handle: sas device handle (assigned by firmware)
679 * Context: Calling function should acquire ioc->raid_device_lock
680 *
681 * This searches for raid_device based on handle, then return raid_device
682 * object.
683 */
684static struct _raid_device *
685_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
686{
687 struct _raid_device *raid_device, *r;
688
689 r = NULL;
690 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
691 if (raid_device->handle != handle)
692 continue;
693 r = raid_device;
694 goto out;
695 }
696
697 out:
698 return r;
699}
700
701/**
702 * _scsih_raid_device_find_by_wwid - raid device search
703 * @ioc: per adapter object
704 * @handle: sas device handle (assigned by firmware)
705 * Context: Calling function should acquire ioc->raid_device_lock
706 *
707 * This searches for raid_device based on wwid, then return raid_device
708 * object.
709 */
710static struct _raid_device *
711_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
712{
713 struct _raid_device *raid_device, *r;
714
715 r = NULL;
716 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
717 if (raid_device->wwid != wwid)
718 continue;
719 r = raid_device;
720 goto out;
721 }
722
723 out:
724 return r;
725}
726
727/**
728 * _scsih_raid_device_add - add raid_device object
729 * @ioc: per adapter object
730 * @raid_device: raid_device object
731 *
732 * This is added to the raid_device_list link list.
733 */
734static void
735_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
736 struct _raid_device *raid_device)
737{
738 unsigned long flags;
739
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530740 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600741 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
742 raid_device->handle, (unsigned long long)raid_device->wwid));
743
744 spin_lock_irqsave(&ioc->raid_device_lock, flags);
745 list_add_tail(&raid_device->list, &ioc->raid_device_list);
746 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
747}
748
749/**
750 * _scsih_raid_device_remove - delete raid_device object
751 * @ioc: per adapter object
752 * @raid_device: raid_device object
753 *
754 * This is removed from the raid_device_list link list.
755 */
756static void
757_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
758 struct _raid_device *raid_device)
759{
760 unsigned long flags;
761
762 spin_lock_irqsave(&ioc->raid_device_lock, flags);
763 list_del(&raid_device->list);
764 memset(raid_device, 0, sizeof(struct _raid_device));
765 kfree(raid_device);
766 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
767}
768
769/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530770 * mpt2sas_scsih_expander_find_by_handle - expander device search
771 * @ioc: per adapter object
772 * @handle: expander handle (assigned by firmware)
773 * Context: Calling function should acquire ioc->sas_device_lock
774 *
775 * This searches for expander device based on handle, then returns the
776 * sas_node object.
777 */
778struct _sas_node *
779mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
780{
781 struct _sas_node *sas_expander, *r;
782
783 r = NULL;
784 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
785 if (sas_expander->handle != handle)
786 continue;
787 r = sas_expander;
788 goto out;
789 }
790 out:
791 return r;
792}
793
794/**
Eric Moore635374e2009-03-09 01:21:12 -0600795 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
796 * @ioc: per adapter object
797 * @sas_address: sas address
798 * Context: Calling function should acquire ioc->sas_node_lock.
799 *
800 * This searches for expander device based on sas_address, then returns the
801 * sas_node object.
802 */
803struct _sas_node *
804mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
805 u64 sas_address)
806{
807 struct _sas_node *sas_expander, *r;
808
809 r = NULL;
810 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
811 if (sas_expander->sas_address != sas_address)
812 continue;
813 r = sas_expander;
814 goto out;
815 }
816 out:
817 return r;
818}
819
820/**
821 * _scsih_expander_node_add - insert expander device to the list.
822 * @ioc: per adapter object
823 * @sas_expander: the sas_device object
824 * Context: This function will acquire ioc->sas_node_lock.
825 *
826 * Adding new object to the ioc->sas_expander_list.
827 *
828 * Return nothing.
829 */
830static void
831_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
832 struct _sas_node *sas_expander)
833{
834 unsigned long flags;
835
836 spin_lock_irqsave(&ioc->sas_node_lock, flags);
837 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
838 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
839}
840
841/**
842 * _scsih_is_end_device - determines if device is an end device
843 * @device_info: bitfield providing information about the device.
844 * Context: none
845 *
846 * Returns 1 if end device.
847 */
848static int
849_scsih_is_end_device(u32 device_info)
850{
851 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
852 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
853 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
854 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
855 return 1;
856 else
857 return 0;
858}
859
860/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530861 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600862 * @ioc: per adapter object
863 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600864 *
865 * Returns the smid stored scmd pointer.
866 */
867static struct scsi_cmnd *
868_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
869{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530870 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600871}
872
873/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530874 * _scsih_scsi_lookup_get_clear - returns scmd entry
875 * @ioc: per adapter object
876 * @smid: system request message index
877 *
878 * Returns the smid stored scmd pointer.
879 * Then will derefrence the stored scmd pointer.
880 */
881static inline struct scsi_cmnd *
882_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
883{
884 unsigned long flags;
885 struct scsi_cmnd *scmd;
886
887 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
888 scmd = ioc->scsi_lookup[smid - 1].scmd;
889 ioc->scsi_lookup[smid - 1].scmd = NULL;
890 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
891
892 return scmd;
893}
894
895/**
Eric Moore635374e2009-03-09 01:21:12 -0600896 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
897 * @ioc: per adapter object
898 * @smid: system request message index
899 * @scmd: pointer to scsi command object
900 * Context: This function will acquire ioc->scsi_lookup_lock.
901 *
902 * This will search for a scmd pointer in the scsi_lookup array,
903 * returning the revelent smid. A returned value of zero means invalid.
904 */
905static u16
906_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
907 *scmd)
908{
909 u16 smid;
910 unsigned long flags;
911 int i;
912
913 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
914 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530915 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600916 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530917 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600918 goto out;
919 }
920 }
921 out:
922 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
923 return smid;
924}
925
926/**
927 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
928 * @ioc: per adapter object
929 * @id: target id
930 * @channel: channel
931 * Context: This function will acquire ioc->scsi_lookup_lock.
932 *
933 * This will search for a matching channel:id in the scsi_lookup array,
934 * returning 1 if found.
935 */
936static u8
937_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
938 int channel)
939{
940 u8 found;
941 unsigned long flags;
942 int i;
943
944 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
945 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530946 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600947 if (ioc->scsi_lookup[i].scmd &&
948 (ioc->scsi_lookup[i].scmd->device->id == id &&
949 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
950 found = 1;
951 goto out;
952 }
953 }
954 out:
955 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
956 return found;
957}
958
959/**
Eric Moore993e0da2009-05-18 13:00:45 -0600960 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
961 * @ioc: per adapter object
962 * @id: target id
963 * @lun: lun number
964 * @channel: channel
965 * Context: This function will acquire ioc->scsi_lookup_lock.
966 *
967 * This will search for a matching channel:id:lun in the scsi_lookup array,
968 * returning 1 if found.
969 */
970static u8
971_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
972 unsigned int lun, int channel)
973{
974 u8 found;
975 unsigned long flags;
976 int i;
977
978 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
979 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530980 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600981 if (ioc->scsi_lookup[i].scmd &&
982 (ioc->scsi_lookup[i].scmd->device->id == id &&
983 ioc->scsi_lookup[i].scmd->device->channel == channel &&
984 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
985 found = 1;
986 goto out;
987 }
988 }
989 out:
990 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
991 return found;
992}
993
994/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530995 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -0600996 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530997 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -0600998 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530999 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -06001000 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301001static struct chain_tracker *
1002_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -06001003{
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301004 struct chain_tracker *chain_req;
1005 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001006
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301007 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
1008 if (list_empty(&ioc->free_chain_list)) {
1009 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1010 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
1011 ioc->name);
1012 return NULL;
1013 }
1014 chain_req = list_entry(ioc->free_chain_list.next,
1015 struct chain_tracker, tracker_list);
1016 list_del_init(&chain_req->tracker_list);
1017 list_add_tail(&chain_req->tracker_list,
1018 &ioc->scsi_lookup[smid - 1].chain_list);
1019 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
1020 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001021}
1022
1023/**
1024 * _scsih_build_scatter_gather - main sg creation routine
1025 * @ioc: per adapter object
1026 * @scmd: scsi command
1027 * @smid: system request message index
1028 * Context: none.
1029 *
1030 * The main routine that builds scatter gather table from a given
1031 * scsi request sent via the .queuecommand main handler.
1032 *
1033 * Returns 0 success, anything else error
1034 */
1035static int
1036_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1037 struct scsi_cmnd *scmd, u16 smid)
1038{
1039 Mpi2SCSIIORequest_t *mpi_request;
1040 dma_addr_t chain_dma;
1041 struct scatterlist *sg_scmd;
1042 void *sg_local, *chain;
1043 u32 chain_offset;
1044 u32 chain_length;
1045 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001046 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001047 u32 sges_in_segment;
1048 u32 sgl_flags;
1049 u32 sgl_flags_last_element;
1050 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301051 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001052
1053 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1054
1055 /* init scatter gather flags */
1056 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1057 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1058 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1059 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1060 << MPI2_SGE_FLAGS_SHIFT;
1061 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1062 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1063 << MPI2_SGE_FLAGS_SHIFT;
1064 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1065
1066 sg_scmd = scsi_sglist(scmd);
1067 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001068 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001069 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1070 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1071 return -ENOMEM;
1072 }
1073
1074 sg_local = &mpi_request->SGL;
1075 sges_in_segment = ioc->max_sges_in_main_message;
1076 if (sges_left <= sges_in_segment)
1077 goto fill_in_last_segment;
1078
1079 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1080 (sges_in_segment * ioc->sge_size))/4;
1081
1082 /* fill in main message segment when there is a chain following */
1083 while (sges_in_segment) {
1084 if (sges_in_segment == 1)
1085 ioc->base_add_sg_single(sg_local,
1086 sgl_flags_last_element | sg_dma_len(sg_scmd),
1087 sg_dma_address(sg_scmd));
1088 else
1089 ioc->base_add_sg_single(sg_local, sgl_flags |
1090 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1091 sg_scmd = sg_next(sg_scmd);
1092 sg_local += ioc->sge_size;
1093 sges_left--;
1094 sges_in_segment--;
1095 }
1096
1097 /* initializing the chain flags and pointers */
1098 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301099 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1100 if (!chain_req)
1101 return -1;
1102 chain = chain_req->chain_buffer;
1103 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001104 do {
1105 sges_in_segment = (sges_left <=
1106 ioc->max_sges_in_chain_message) ? sges_left :
1107 ioc->max_sges_in_chain_message;
1108 chain_offset = (sges_left == sges_in_segment) ?
1109 0 : (sges_in_segment * ioc->sge_size)/4;
1110 chain_length = sges_in_segment * ioc->sge_size;
1111 if (chain_offset) {
1112 chain_offset = chain_offset <<
1113 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1114 chain_length += ioc->sge_size;
1115 }
1116 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1117 chain_length, chain_dma);
1118 sg_local = chain;
1119 if (!chain_offset)
1120 goto fill_in_last_segment;
1121
1122 /* fill in chain segments */
1123 while (sges_in_segment) {
1124 if (sges_in_segment == 1)
1125 ioc->base_add_sg_single(sg_local,
1126 sgl_flags_last_element |
1127 sg_dma_len(sg_scmd),
1128 sg_dma_address(sg_scmd));
1129 else
1130 ioc->base_add_sg_single(sg_local, sgl_flags |
1131 sg_dma_len(sg_scmd),
1132 sg_dma_address(sg_scmd));
1133 sg_scmd = sg_next(sg_scmd);
1134 sg_local += ioc->sge_size;
1135 sges_left--;
1136 sges_in_segment--;
1137 }
1138
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301139 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1140 if (!chain_req)
1141 return -1;
1142 chain = chain_req->chain_buffer;
1143 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001144 } while (1);
1145
1146
1147 fill_in_last_segment:
1148
1149 /* fill the last segment */
1150 while (sges_left) {
1151 if (sges_left == 1)
1152 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1153 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1154 else
1155 ioc->base_add_sg_single(sg_local, sgl_flags |
1156 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1157 sg_scmd = sg_next(sg_scmd);
1158 sg_local += ioc->sge_size;
1159 sges_left--;
1160 }
1161
1162 return 0;
1163}
1164
1165/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301166 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001167 * @sdev: scsi device struct
1168 * @qdepth: requested queue depth
1169 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301170 *
1171 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001172 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301173static void
1174_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001175{
1176 struct Scsi_Host *shost = sdev->host;
1177 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301178 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1179 struct MPT2SAS_DEVICE *sas_device_priv_data;
1180 struct MPT2SAS_TARGET *sas_target_priv_data;
1181 struct _sas_device *sas_device;
1182 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001183
1184 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301185
1186 /* limit max device queue for SATA to 32 */
1187 sas_device_priv_data = sdev->hostdata;
1188 if (!sas_device_priv_data)
1189 goto not_sata;
1190 sas_target_priv_data = sas_device_priv_data->sas_target;
1191 if (!sas_target_priv_data)
1192 goto not_sata;
1193 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1194 goto not_sata;
1195 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1196 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1197 sas_device_priv_data->sas_target->sas_address);
1198 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1199 if (sas_device && sas_device->device_info &
1200 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1201 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1202
1203 not_sata:
1204
Eric Moore635374e2009-03-09 01:21:12 -06001205 if (!sdev->tagged_supported)
1206 max_depth = 1;
1207 if (qdepth > max_depth)
1208 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301209 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1210}
1211
1212/**
1213 * _scsih_change_queue_depth - setting device queue depth
1214 * @sdev: scsi device struct
1215 * @qdepth: requested queue depth
1216 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1217 * (see include/scsi/scsi_host.h for definition)
1218 *
1219 * Returns queue depth.
1220 */
1221static int
1222_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1223{
1224 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1225 _scsih_adjust_queue_depth(sdev, qdepth);
1226 else if (reason == SCSI_QDEPTH_QFULL)
1227 scsi_track_queue_full(sdev, qdepth);
1228 else
1229 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001230
1231 if (sdev->inquiry_len > 7)
1232 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1233 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1234 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1235 sdev->ordered_tags, sdev->scsi_level,
1236 (sdev->inquiry[7] & 2) >> 1);
1237
1238 return sdev->queue_depth;
1239}
1240
1241/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301242 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001243 * @sdev: scsi device struct
1244 * @tag_type: requested tag type
1245 *
1246 * Returns queue tag type.
1247 */
1248static int
Eric Moored5d135b2009-05-18 13:02:08 -06001249_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001250{
1251 if (sdev->tagged_supported) {
1252 scsi_set_tag_type(sdev, tag_type);
1253 if (tag_type)
1254 scsi_activate_tcq(sdev, sdev->queue_depth);
1255 else
1256 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1257 } else
1258 tag_type = 0;
1259
1260 return tag_type;
1261}
1262
1263/**
Eric Moored5d135b2009-05-18 13:02:08 -06001264 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001265 * @starget: scsi target struct
1266 *
1267 * Returns 0 if ok. Any other return is assumed to be an error and
1268 * the device is ignored.
1269 */
1270static int
Eric Moored5d135b2009-05-18 13:02:08 -06001271_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001272{
1273 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1274 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1275 struct MPT2SAS_TARGET *sas_target_priv_data;
1276 struct _sas_device *sas_device;
1277 struct _raid_device *raid_device;
1278 unsigned long flags;
1279 struct sas_rphy *rphy;
1280
1281 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1282 if (!sas_target_priv_data)
1283 return -ENOMEM;
1284
1285 starget->hostdata = sas_target_priv_data;
1286 sas_target_priv_data->starget = starget;
1287 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1288
1289 /* RAID volumes */
1290 if (starget->channel == RAID_CHANNEL) {
1291 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1292 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1293 starget->channel);
1294 if (raid_device) {
1295 sas_target_priv_data->handle = raid_device->handle;
1296 sas_target_priv_data->sas_address = raid_device->wwid;
1297 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301298 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001299 raid_device->starget = starget;
1300 }
1301 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1302 return 0;
1303 }
1304
1305 /* sas/sata devices */
1306 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1307 rphy = dev_to_rphy(starget->dev.parent);
1308 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1309 rphy->identify.sas_address);
1310
1311 if (sas_device) {
1312 sas_target_priv_data->handle = sas_device->handle;
1313 sas_target_priv_data->sas_address = sas_device->sas_address;
1314 sas_device->starget = starget;
1315 sas_device->id = starget->id;
1316 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301317 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001318 sas_target_priv_data->flags |=
1319 MPT_TARGET_FLAGS_RAID_COMPONENT;
1320 }
1321 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1322
1323 return 0;
1324}
1325
1326/**
Eric Moored5d135b2009-05-18 13:02:08 -06001327 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001328 * @starget: scsi target struct
1329 *
1330 * Returns nothing.
1331 */
1332static void
Eric Moored5d135b2009-05-18 13:02:08 -06001333_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001334{
1335 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1336 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1337 struct MPT2SAS_TARGET *sas_target_priv_data;
1338 struct _sas_device *sas_device;
1339 struct _raid_device *raid_device;
1340 unsigned long flags;
1341 struct sas_rphy *rphy;
1342
1343 sas_target_priv_data = starget->hostdata;
1344 if (!sas_target_priv_data)
1345 return;
1346
1347 if (starget->channel == RAID_CHANNEL) {
1348 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1349 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1350 starget->channel);
1351 if (raid_device) {
1352 raid_device->starget = NULL;
1353 raid_device->sdev = NULL;
1354 }
1355 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1356 goto out;
1357 }
1358
1359 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1360 rphy = dev_to_rphy(starget->dev.parent);
1361 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1362 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001363 if (sas_device && (sas_device->starget == starget) &&
1364 (sas_device->id == starget->id) &&
1365 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001366 sas_device->starget = NULL;
1367
1368 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1369
1370 out:
1371 kfree(sas_target_priv_data);
1372 starget->hostdata = NULL;
1373}
1374
1375/**
Eric Moored5d135b2009-05-18 13:02:08 -06001376 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001377 * @sdev: scsi device struct
1378 *
1379 * Returns 0 if ok. Any other return is assumed to be an error and
1380 * the device is ignored.
1381 */
1382static int
Eric Moored5d135b2009-05-18 13:02:08 -06001383_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001384{
1385 struct Scsi_Host *shost;
1386 struct MPT2SAS_ADAPTER *ioc;
1387 struct MPT2SAS_TARGET *sas_target_priv_data;
1388 struct MPT2SAS_DEVICE *sas_device_priv_data;
1389 struct scsi_target *starget;
1390 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001391 unsigned long flags;
1392
1393 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1394 if (!sas_device_priv_data)
1395 return -ENOMEM;
1396
1397 sas_device_priv_data->lun = sdev->lun;
1398 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1399
1400 starget = scsi_target(sdev);
1401 sas_target_priv_data = starget->hostdata;
1402 sas_target_priv_data->num_luns++;
1403 sas_device_priv_data->sas_target = sas_target_priv_data;
1404 sdev->hostdata = sas_device_priv_data;
1405 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1406 sdev->no_uld_attach = 1;
1407
1408 shost = dev_to_shost(&starget->dev);
1409 ioc = shost_priv(shost);
1410 if (starget->channel == RAID_CHANNEL) {
1411 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1412 raid_device = _scsih_raid_device_find_by_id(ioc,
1413 starget->id, starget->channel);
1414 if (raid_device)
1415 raid_device->sdev = sdev; /* raid is single lun */
1416 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001417 }
1418
Eric Moore635374e2009-03-09 01:21:12 -06001419 return 0;
1420}
1421
1422/**
Eric Moored5d135b2009-05-18 13:02:08 -06001423 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001424 * @sdev: scsi device struct
1425 *
1426 * Returns nothing.
1427 */
1428static void
Eric Moored5d135b2009-05-18 13:02:08 -06001429_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001430{
1431 struct MPT2SAS_TARGET *sas_target_priv_data;
1432 struct scsi_target *starget;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301433 struct Scsi_Host *shost;
1434 struct MPT2SAS_ADAPTER *ioc;
1435 struct _sas_device *sas_device;
1436 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001437
1438 if (!sdev->hostdata)
1439 return;
1440
1441 starget = scsi_target(sdev);
1442 sas_target_priv_data = starget->hostdata;
1443 sas_target_priv_data->num_luns--;
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05301444
1445 shost = dev_to_shost(&starget->dev);
1446 ioc = shost_priv(shost);
1447
1448 if (!(sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME)) {
1449 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1450 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1451 sas_target_priv_data->sas_address);
1452 if (sas_device)
1453 sas_device->starget = NULL;
1454 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1455 }
1456
Eric Moore635374e2009-03-09 01:21:12 -06001457 kfree(sdev->hostdata);
1458 sdev->hostdata = NULL;
1459}
1460
1461/**
Eric Moored5d135b2009-05-18 13:02:08 -06001462 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001463 * @ioc: per adapter object
1464 * @sas_device: the sas_device object
1465 * @sdev: scsi device struct
1466 */
1467static void
Eric Moored5d135b2009-05-18 13:02:08 -06001468_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001469 struct _sas_device *sas_device, struct scsi_device *sdev)
1470{
1471 Mpi2ConfigReply_t mpi_reply;
1472 Mpi2SasDevicePage0_t sas_device_pg0;
1473 u32 ioc_status;
1474 u16 flags;
1475 u32 device_info;
1476
1477 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1478 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1479 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1480 ioc->name, __FILE__, __LINE__, __func__);
1481 return;
1482 }
1483
1484 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1485 MPI2_IOCSTATUS_MASK;
1486 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1487 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1488 ioc->name, __FILE__, __LINE__, __func__);
1489 return;
1490 }
1491
1492 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301493 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001494
1495 sdev_printk(KERN_INFO, sdev,
1496 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1497 "sw_preserve(%s)\n",
1498 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1499 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1500 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1501 "n",
1502 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1503 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1504 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1505}
1506
1507/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301508 * _scsih_is_raid - return boolean indicating device is raid volume
1509 * @dev the device struct object
1510 */
1511static int
1512_scsih_is_raid(struct device *dev)
1513{
1514 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301515 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301516
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301517 if (ioc->is_warpdrive)
1518 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301519 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1520}
1521
1522/**
1523 * _scsih_get_resync - get raid volume resync percent complete
1524 * @dev the device struct object
1525 */
1526static void
1527_scsih_get_resync(struct device *dev)
1528{
1529 struct scsi_device *sdev = to_scsi_device(dev);
1530 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1531 static struct _raid_device *raid_device;
1532 unsigned long flags;
1533 Mpi2RaidVolPage0_t vol_pg0;
1534 Mpi2ConfigReply_t mpi_reply;
1535 u32 volume_status_flags;
1536 u8 percent_complete = 0;
1537
1538 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1539 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1540 sdev->channel);
1541 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1542
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301543 if (!raid_device || ioc->is_warpdrive)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301544 goto out;
1545
1546 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1547 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1548 sizeof(Mpi2RaidVolPage0_t))) {
1549 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1550 ioc->name, __FILE__, __LINE__, __func__);
1551 goto out;
1552 }
1553
1554 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1555 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1556 percent_complete = raid_device->percent_complete;
1557 out:
1558 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1559}
1560
1561/**
1562 * _scsih_get_state - get raid volume level
1563 * @dev the device struct object
1564 */
1565static void
1566_scsih_get_state(struct device *dev)
1567{
1568 struct scsi_device *sdev = to_scsi_device(dev);
1569 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1570 static struct _raid_device *raid_device;
1571 unsigned long flags;
1572 Mpi2RaidVolPage0_t vol_pg0;
1573 Mpi2ConfigReply_t mpi_reply;
1574 u32 volstate;
1575 enum raid_state state = RAID_STATE_UNKNOWN;
1576
1577 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1578 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1579 sdev->channel);
1580 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1581
1582 if (!raid_device)
1583 goto out;
1584
1585 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1586 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1587 sizeof(Mpi2RaidVolPage0_t))) {
1588 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1589 ioc->name, __FILE__, __LINE__, __func__);
1590 goto out;
1591 }
1592
1593 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1594 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1595 state = RAID_STATE_RESYNCING;
1596 goto out;
1597 }
1598
1599 switch (vol_pg0.VolumeState) {
1600 case MPI2_RAID_VOL_STATE_OPTIMAL:
1601 case MPI2_RAID_VOL_STATE_ONLINE:
1602 state = RAID_STATE_ACTIVE;
1603 break;
1604 case MPI2_RAID_VOL_STATE_DEGRADED:
1605 state = RAID_STATE_DEGRADED;
1606 break;
1607 case MPI2_RAID_VOL_STATE_FAILED:
1608 case MPI2_RAID_VOL_STATE_MISSING:
1609 state = RAID_STATE_OFFLINE;
1610 break;
1611 }
1612 out:
1613 raid_set_state(mpt2sas_raid_template, dev, state);
1614}
1615
1616/**
1617 * _scsih_set_level - set raid level
1618 * @sdev: scsi device struct
1619 * @raid_device: raid_device object
1620 */
1621static void
1622_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1623{
1624 enum raid_level level = RAID_LEVEL_UNKNOWN;
1625
1626 switch (raid_device->volume_type) {
1627 case MPI2_RAID_VOL_TYPE_RAID0:
1628 level = RAID_LEVEL_0;
1629 break;
1630 case MPI2_RAID_VOL_TYPE_RAID10:
1631 level = RAID_LEVEL_10;
1632 break;
1633 case MPI2_RAID_VOL_TYPE_RAID1E:
1634 level = RAID_LEVEL_1E;
1635 break;
1636 case MPI2_RAID_VOL_TYPE_RAID1:
1637 level = RAID_LEVEL_1;
1638 break;
1639 }
1640
1641 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1642}
1643
1644/**
Eric Moore635374e2009-03-09 01:21:12 -06001645 * _scsih_get_volume_capabilities - volume capabilities
1646 * @ioc: per adapter object
1647 * @sas_device: the raid_device object
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301648 *
1649 * Returns 0 for success, else 1
Eric Moore635374e2009-03-09 01:21:12 -06001650 */
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301651static int
Eric Moore635374e2009-03-09 01:21:12 -06001652_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1653 struct _raid_device *raid_device)
1654{
1655 Mpi2RaidVolPage0_t *vol_pg0;
1656 Mpi2RaidPhysDiskPage0_t pd_pg0;
1657 Mpi2SasDevicePage0_t sas_device_pg0;
1658 Mpi2ConfigReply_t mpi_reply;
1659 u16 sz;
1660 u8 num_pds;
1661
1662 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1663 &num_pds)) || !num_pds) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301664 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1665 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1666 __func__));
1667 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001668 }
1669
1670 raid_device->num_pds = num_pds;
1671 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1672 sizeof(Mpi2RaidVol0PhysDisk_t));
1673 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1674 if (!vol_pg0) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301675 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1676 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1677 __func__));
1678 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001679 }
1680
1681 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1682 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301683 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1684 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
1685 __func__));
Eric Moore635374e2009-03-09 01:21:12 -06001686 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301687 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001688 }
1689
1690 raid_device->volume_type = vol_pg0->VolumeType;
1691
1692 /* figure out what the underlying devices are by
1693 * obtaining the device_info bits for the 1st device
1694 */
1695 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1696 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1697 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1698 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1699 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1700 le16_to_cpu(pd_pg0.DevHandle)))) {
1701 raid_device->device_info =
1702 le32_to_cpu(sas_device_pg0.DeviceInfo);
1703 }
1704 }
1705
1706 kfree(vol_pg0);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301707 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06001708}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301709/**
1710 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1711 * @ioc: per adapter object
1712 */
1713static void
1714_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1715{
1716 Mpi2RaidVolPage1_t vol_pg1;
1717 Mpi2ConfigReply_t mpi_reply;
1718 struct _raid_device *raid_device;
1719 u16 handle;
1720 u16 ioc_status;
1721
1722 handle = 0xFFFF;
1723 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1724 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1725 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1726 MPI2_IOCSTATUS_MASK;
1727 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1728 break;
1729 handle = le16_to_cpu(vol_pg1.DevHandle);
1730 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1731 if (raid_device)
1732 raid_device->direct_io_enabled = 0;
1733 }
1734 return;
1735}
1736
1737
1738/**
1739 * _scsih_get_num_volumes - Get number of volumes in the ioc
1740 * @ioc: per adapter object
1741 */
1742static u8
1743_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1744{
1745 Mpi2RaidVolPage1_t vol_pg1;
1746 Mpi2ConfigReply_t mpi_reply;
1747 u16 handle;
1748 u8 vol_cnt = 0;
1749 u16 ioc_status;
1750
1751 handle = 0xFFFF;
1752 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1753 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1754 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1755 MPI2_IOCSTATUS_MASK;
1756 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1757 break;
1758 vol_cnt++;
1759 handle = le16_to_cpu(vol_pg1.DevHandle);
1760 }
1761 return vol_cnt;
1762}
1763
1764
1765/**
1766 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1767 * @ioc: per adapter object
1768 * @raid_device: the raid_device object
1769 */
1770static void
1771_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1772 struct _raid_device *raid_device)
1773{
1774 Mpi2RaidVolPage0_t *vol_pg0;
1775 Mpi2RaidPhysDiskPage0_t pd_pg0;
1776 Mpi2ConfigReply_t mpi_reply;
1777 u16 sz;
1778 u8 num_pds, count;
1779 u64 mb = 1024 * 1024;
1780 u64 tb_2 = 2 * mb * mb;
1781 u64 capacity;
1782 u32 stripe_sz;
1783 u8 i, stripe_exp;
1784
1785 if (!ioc->is_warpdrive)
1786 return;
1787
1788 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1789 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1790 "globally as drives are exposed\n", ioc->name);
1791 return;
1792 }
1793 if (_scsih_get_num_volumes(ioc) > 1) {
1794 _scsih_disable_ddio(ioc);
1795 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1796 "globally as number of drives > 1\n", ioc->name);
1797 return;
1798 }
1799 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1800 &num_pds)) || !num_pds) {
1801 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1802 "Failure in computing number of drives\n", ioc->name);
1803 return;
1804 }
1805
1806 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1807 sizeof(Mpi2RaidVol0PhysDisk_t));
1808 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1809 if (!vol_pg0) {
1810 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1811 "Memory allocation failure for RVPG0\n", ioc->name);
1812 return;
1813 }
1814
1815 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1816 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1817 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1818 "Failure in retrieving RVPG0\n", ioc->name);
1819 kfree(vol_pg0);
1820 return;
1821 }
1822
1823 /*
1824 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1825 * assumed for WARPDRIVE, disable direct I/O
1826 */
1827 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1828 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1829 "for the drive with handle(0x%04x): num_mem=%d, "
1830 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1831 num_pds, MPT_MAX_WARPDRIVE_PDS);
1832 kfree(vol_pg0);
1833 return;
1834 }
1835 for (count = 0; count < num_pds; count++) {
1836 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1837 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1838 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1839 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1840 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1841 "disabled for the drive with handle(0x%04x) member"
1842 "handle retrieval failed for member number=%d\n",
1843 ioc->name, raid_device->handle,
1844 vol_pg0->PhysDisk[count].PhysDiskNum);
1845 goto out_error;
1846 }
1847 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1848 }
1849
1850 /*
1851 * Assumption for WD: Direct I/O is not supported if the volume is
1852 * not RAID0, if the stripe size is not 64KB, if the block size is
1853 * not 512 and if the volume size is >2TB
1854 */
1855 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1856 le16_to_cpu(vol_pg0->BlockSize) != 512) {
1857 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1858 "for the drive with handle(0x%04x): type=%d, "
1859 "s_sz=%uK, blk_size=%u\n", ioc->name,
1860 raid_device->handle, raid_device->volume_type,
1861 le32_to_cpu(vol_pg0->StripeSize)/2,
1862 le16_to_cpu(vol_pg0->BlockSize));
1863 goto out_error;
1864 }
1865
1866 capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1867 (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1868
1869 if (capacity > tb_2) {
1870 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1871 "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1872 ioc->name, raid_device->handle);
1873 goto out_error;
1874 }
1875
1876 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1877 stripe_exp = 0;
1878 for (i = 0; i < 32; i++) {
1879 if (stripe_sz & 1)
1880 break;
1881 stripe_exp++;
1882 stripe_sz >>= 1;
1883 }
1884 if (i == 32) {
1885 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1886 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1887 ioc->name, raid_device->handle,
1888 le32_to_cpu(vol_pg0->StripeSize)/2);
1889 goto out_error;
1890 }
1891 raid_device->stripe_exponent = stripe_exp;
1892 raid_device->direct_io_enabled = 1;
1893
1894 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1895 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1896 /*
1897 * WARPDRIVE: Though the following fields are not used for direct IO,
1898 * stored for future purpose:
1899 */
1900 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1901 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1902 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1903
1904
1905 kfree(vol_pg0);
1906 return;
1907
1908out_error:
1909 raid_device->direct_io_enabled = 0;
1910 for (count = 0; count < num_pds; count++)
1911 raid_device->pd_handle[count] = 0;
1912 kfree(vol_pg0);
1913 return;
1914}
Eric Moore635374e2009-03-09 01:21:12 -06001915
1916/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301917 * _scsih_enable_tlr - setting TLR flags
1918 * @ioc: per adapter object
1919 * @sdev: scsi device struct
1920 *
1921 * Enabling Transaction Layer Retries for tape devices when
1922 * vpd page 0x90 is present
1923 *
1924 */
1925static void
1926_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1927{
1928 /* only for TAPE */
1929 if (sdev->type != TYPE_TAPE)
1930 return;
1931
1932 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1933 return;
1934
1935 sas_enable_tlr(sdev);
1936 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1937 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1938 return;
1939
1940}
1941
1942/**
Eric Moored5d135b2009-05-18 13:02:08 -06001943 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001944 * @sdev: scsi device struct
1945 *
1946 * Returns 0 if ok. Any other return is assumed to be an error and
1947 * the device is ignored.
1948 */
1949static int
Eric Moored5d135b2009-05-18 13:02:08 -06001950_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001951{
1952 struct Scsi_Host *shost = sdev->host;
1953 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1954 struct MPT2SAS_DEVICE *sas_device_priv_data;
1955 struct MPT2SAS_TARGET *sas_target_priv_data;
1956 struct _sas_device *sas_device;
1957 struct _raid_device *raid_device;
1958 unsigned long flags;
1959 int qdepth;
1960 u8 ssp_target = 0;
1961 char *ds = "";
1962 char *r_level = "";
1963
1964 qdepth = 1;
1965 sas_device_priv_data = sdev->hostdata;
1966 sas_device_priv_data->configured_lun = 1;
1967 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1968 sas_target_priv_data = sas_device_priv_data->sas_target;
1969
1970 /* raid volume handling */
1971 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1972
1973 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1974 raid_device = _scsih_raid_device_find_by_handle(ioc,
1975 sas_target_priv_data->handle);
1976 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1977 if (!raid_device) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301978 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1979 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1980 __LINE__, __func__));
1981 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06001982 }
1983
1984 _scsih_get_volume_capabilities(ioc, raid_device);
1985
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05301986 if (_scsih_get_volume_capabilities(ioc, raid_device)) {
1987 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
1988 "failure at %s:%d/%s()!\n", ioc->name, __FILE__,
1989 __LINE__, __func__));
1990 return 1;
1991 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301992 /*
1993 * WARPDRIVE: Initialize the required data for Direct IO
1994 */
1995 _scsih_init_warpdrive_properties(ioc, raid_device);
1996
Eric Moore635374e2009-03-09 01:21:12 -06001997 /* RAID Queue Depth Support
1998 * IS volume = underlying qdepth of drive type, either
1999 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
2000 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
2001 */
2002 if (raid_device->device_info &
2003 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2004 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2005 ds = "SSP";
2006 } else {
2007 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2008 if (raid_device->device_info &
2009 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2010 ds = "SATA";
2011 else
2012 ds = "STP";
2013 }
2014
2015 switch (raid_device->volume_type) {
2016 case MPI2_RAID_VOL_TYPE_RAID0:
2017 r_level = "RAID0";
2018 break;
2019 case MPI2_RAID_VOL_TYPE_RAID1E:
2020 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05302021 if (ioc->manu_pg10.OEMIdentifier &&
Kashyap, Desaic97951e2011-06-14 10:54:56 +05302022 (le32_to_cpu(ioc->manu_pg10.GenericFlags0) &
Kashyap, Desaied79f122009-08-20 13:23:49 +05302023 MFG10_GF0_R10_DISPLAY) &&
2024 !(raid_device->num_pds % 2))
2025 r_level = "RAID10";
2026 else
2027 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06002028 break;
2029 case MPI2_RAID_VOL_TYPE_RAID1:
2030 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2031 r_level = "RAID1";
2032 break;
2033 case MPI2_RAID_VOL_TYPE_RAID10:
2034 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2035 r_level = "RAID10";
2036 break;
2037 case MPI2_RAID_VOL_TYPE_UNKNOWN:
2038 default:
2039 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
2040 r_level = "RAIDX";
2041 break;
2042 }
2043
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302044 if (!ioc->hide_ir_msg)
2045 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
2046 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
2047 r_level, raid_device->handle,
2048 (unsigned long long)raid_device->wwid,
2049 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07002050 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05302051 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302052 if (!ioc->is_warpdrive)
2053 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06002054 return 0;
2055 }
2056
2057 /* non-raid handling */
2058 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2059 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2060 sas_device_priv_data->sas_target->sas_address);
2061 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2062 if (sas_device) {
2063 if (sas_target_priv_data->flags &
2064 MPT_TARGET_FLAGS_RAID_COMPONENT) {
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302065 if (mpt2sas_config_get_volume_handle(ioc,
2066 sas_device->handle, &sas_device->volume_handle)) {
2067 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2068 "failure at %s:%d/%s()!\n", ioc->name,
2069 __FILE__, __LINE__, __func__));
2070 return 1;
2071 }
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05302072 if (sas_device->volume_handle &&
2073 mpt2sas_config_get_volume_wwid(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06002074 sas_device->volume_handle,
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302075 &sas_device->volume_wwid)) {
2076 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2077 "failure at %s:%d/%s()!\n", ioc->name,
2078 __FILE__, __LINE__, __func__));
2079 return 1;
2080 }
Eric Moore635374e2009-03-09 01:21:12 -06002081 }
2082 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2083 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2084 ssp_target = 1;
2085 ds = "SSP";
2086 } else {
2087 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2088 if (sas_device->device_info &
2089 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2090 ds = "STP";
2091 else if (sas_device->device_info &
2092 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2093 ds = "SATA";
2094 }
2095
2096 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302097 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002098 ds, sas_device->handle,
2099 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302100 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06002101 (unsigned long long)sas_device->device_name);
2102 sdev_printk(KERN_INFO, sdev, "%s: "
2103 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2104 (unsigned long long) sas_device->enclosure_logical_id,
2105 sas_device->slot);
2106
2107 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06002108 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
nagalakshmi.nandigama@lsi.com6faace22011-10-19 15:37:37 +05302109 } else {
2110 dfailprintk(ioc, printk(MPT2SAS_WARN_FMT
2111 "failure at %s:%d/%s()!\n", ioc->name, __FILE__, __LINE__,
2112 __func__));
2113 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002114 }
2115
Mike Christiee881a172009-10-15 17:46:39 -07002116 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002117
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302118 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002119 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302120 _scsih_enable_tlr(ioc, sdev);
2121 }
Eric Moore635374e2009-03-09 01:21:12 -06002122 return 0;
2123}
2124
2125/**
Eric Moored5d135b2009-05-18 13:02:08 -06002126 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002127 * @sdev: scsi device struct
2128 * @bdev: pointer to block device context
2129 * @capacity: device size (in 512 byte sectors)
2130 * @params: three element array to place output:
2131 * params[0] number of heads (max 255)
2132 * params[1] number of sectors (max 63)
2133 * params[2] number of cylinders
2134 *
2135 * Return nothing.
2136 */
2137static int
Eric Moored5d135b2009-05-18 13:02:08 -06002138_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002139 sector_t capacity, int params[])
2140{
2141 int heads;
2142 int sectors;
2143 sector_t cylinders;
2144 ulong dummy;
2145
2146 heads = 64;
2147 sectors = 32;
2148
2149 dummy = heads * sectors;
2150 cylinders = capacity;
2151 sector_div(cylinders, dummy);
2152
2153 /*
2154 * Handle extended translation size for logical drives
2155 * > 1Gb
2156 */
2157 if ((ulong)capacity >= 0x200000) {
2158 heads = 255;
2159 sectors = 63;
2160 dummy = heads * sectors;
2161 cylinders = capacity;
2162 sector_div(cylinders, dummy);
2163 }
2164
2165 /* return result */
2166 params[0] = heads;
2167 params[1] = sectors;
2168 params[2] = cylinders;
2169
2170 return 0;
2171}
2172
2173/**
2174 * _scsih_response_code - translation of device response code
2175 * @ioc: per adapter object
2176 * @response_code: response code returned by the device
2177 *
2178 * Return nothing.
2179 */
2180static void
2181_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2182{
2183 char *desc;
2184
2185 switch (response_code) {
2186 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2187 desc = "task management request completed";
2188 break;
2189 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2190 desc = "invalid frame";
2191 break;
2192 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2193 desc = "task management request not supported";
2194 break;
2195 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2196 desc = "task management request failed";
2197 break;
2198 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2199 desc = "task management request succeeded";
2200 break;
2201 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2202 desc = "invalid lun";
2203 break;
2204 case 0xA:
2205 desc = "overlapped tag attempted";
2206 break;
2207 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2208 desc = "task queued, however not sent to target";
2209 break;
2210 default:
2211 desc = "unknown";
2212 break;
2213 }
2214 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2215 ioc->name, response_code, desc);
2216}
2217
2218/**
Eric Moored5d135b2009-05-18 13:02:08 -06002219 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002220 * @ioc: per adapter object
2221 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302222 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002223 * @reply: reply message frame(lower 32bit addr)
2224 * Context: none.
2225 *
2226 * The callback handler when using scsih_issue_tm.
2227 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302228 * Return 1 meaning mf should be freed from _base_interrupt
2229 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002230 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302231static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302232_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002233{
2234 MPI2DefaultReply_t *mpi_reply;
2235
2236 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302237 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002238 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302239 return 1;
nagalakshmi.nandigama@lsi.com911ae942011-09-08 06:18:50 +05302240 mpt2sas_base_flush_reply_queues(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06002241 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2242 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2243 if (mpi_reply) {
2244 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2245 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2246 }
2247 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2248 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302249 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002250}
2251
2252/**
2253 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2254 * @ioc: per adapter object
2255 * @handle: device handle
2256 *
2257 * During taskmangement request, we need to freeze the device queue.
2258 */
2259void
2260mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2261{
2262 struct MPT2SAS_DEVICE *sas_device_priv_data;
2263 struct scsi_device *sdev;
2264 u8 skip = 0;
2265
2266 shost_for_each_device(sdev, ioc->shost) {
2267 if (skip)
2268 continue;
2269 sas_device_priv_data = sdev->hostdata;
2270 if (!sas_device_priv_data)
2271 continue;
2272 if (sas_device_priv_data->sas_target->handle == handle) {
2273 sas_device_priv_data->sas_target->tm_busy = 1;
2274 skip = 1;
2275 ioc->ignore_loginfos = 1;
2276 }
2277 }
2278}
2279
2280/**
2281 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2282 * @ioc: per adapter object
2283 * @handle: device handle
2284 *
2285 * During taskmangement request, we need to freeze the device queue.
2286 */
2287void
2288mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2289{
2290 struct MPT2SAS_DEVICE *sas_device_priv_data;
2291 struct scsi_device *sdev;
2292 u8 skip = 0;
2293
2294 shost_for_each_device(sdev, ioc->shost) {
2295 if (skip)
2296 continue;
2297 sas_device_priv_data = sdev->hostdata;
2298 if (!sas_device_priv_data)
2299 continue;
2300 if (sas_device_priv_data->sas_target->handle == handle) {
2301 sas_device_priv_data->sas_target->tm_busy = 0;
2302 skip = 1;
2303 ioc->ignore_loginfos = 0;
2304 }
2305 }
2306}
2307
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302308
Eric Moore635374e2009-03-09 01:21:12 -06002309/**
2310 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2311 * @ioc: per adapter struct
2312 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302313 * @channel: the channel assigned by the OS
2314 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002315 * @lun: lun number
2316 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2317 * @smid_task: smid assigned to the task
2318 * @timeout: timeout in seconds
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302319 * @serial_number: the serial_number from scmd
2320 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302321 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002322 *
2323 * A generic API for sending task management requests to firmware.
2324 *
Eric Moore635374e2009-03-09 01:21:12 -06002325 * The callback index is set inside `ioc->tm_cb_idx`.
2326 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302327 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002328 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302329int
2330mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2331 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302332 unsigned long serial_number, enum mutex_type m_type)
Eric Moore635374e2009-03-09 01:21:12 -06002333{
2334 Mpi2SCSITaskManagementRequest_t *mpi_request;
2335 Mpi2SCSITaskManagementReply_t *mpi_reply;
2336 u16 smid = 0;
2337 u32 ioc_state;
2338 unsigned long timeleft;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302339 struct scsiio_tracker *scsi_lookup = NULL;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302340 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002341
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302342 if (m_type == TM_MUTEX_ON)
2343 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302344 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2345 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2346 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302347 rc = FAILED;
2348 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302349 }
2350
Eric Moore3cb54692010-07-08 14:44:34 -06002351 if (ioc->shost_recovery || ioc->remove_host ||
2352 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002353 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2354 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302355 rc = FAILED;
2356 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002357 }
Eric Moore635374e2009-03-09 01:21:12 -06002358
2359 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2360 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302361 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002362 "active!\n", ioc->name));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302363 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302364 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302365 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302366 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002367 }
2368
2369 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2370 mpt2sas_base_fault_info(ioc, ioc_state &
2371 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302372 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302373 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302374 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302375 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002376 }
2377
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302378 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002379 if (!smid) {
2380 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2381 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302382 rc = FAILED;
2383 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002384 }
2385
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302386 if (type == MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK)
2387 scsi_lookup = &ioc->scsi_lookup[smid_task - 1];
2388
Eric Moore635374e2009-03-09 01:21:12 -06002389 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302390 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2391 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002392 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2393 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2394 ioc->tm_cmds.smid = smid;
2395 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302396 memset(ioc->tm_cmds.reply, 0, sizeof(Mpi2SCSITaskManagementReply_t));
Eric Moore635374e2009-03-09 01:21:12 -06002397 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2398 mpi_request->DevHandle = cpu_to_le16(handle);
2399 mpi_request->TaskType = type;
2400 mpi_request->TaskMID = cpu_to_le16(smid_task);
2401 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2402 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302403 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302404 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002405 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002406 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2407 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2408 ioc->name, __func__);
2409 _debug_dump_mf(mpi_request,
2410 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302411 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302412 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302413 FORCE_BIG_HAMMER);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302414 rc = (!rc) ? SUCCESS : FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302415 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2416 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2417 goto err_out;
2418 }
Eric Moore635374e2009-03-09 01:21:12 -06002419 }
2420
2421 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2422 mpi_reply = ioc->tm_cmds.reply;
2423 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2424 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2425 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2426 le32_to_cpu(mpi_reply->IOCLogInfo),
2427 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302428 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002429 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302430 if (mpi_reply->IOCStatus)
2431 _debug_dump_mf(mpi_request,
2432 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2433 }
Eric Moore635374e2009-03-09 01:21:12 -06002434 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302435
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302436 switch (type) {
2437 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302438 rc = SUCCESS;
2439 if (scsi_lookup->scmd == NULL)
2440 break;
2441 rc = FAILED;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302442 break;
2443
2444 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2445 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2446 rc = FAILED;
2447 else
2448 rc = SUCCESS;
2449 break;
2450
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302451 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302452 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2453 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2454 rc = FAILED;
2455 else
2456 rc = SUCCESS;
2457 break;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302458 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
2459 rc = SUCCESS;
2460 break;
2461 default:
2462 rc = FAILED;
2463 break;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302464 }
2465
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302466 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2467 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302468 if (m_type == TM_MUTEX_ON)
2469 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302470
2471 return rc;
2472
2473 err_out:
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302474 if (m_type == TM_MUTEX_ON)
2475 mutex_unlock(&ioc->tm_cmds.mutex);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302476 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002477}
2478
2479/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302480 * _scsih_tm_display_info - displays info about the device
2481 * @ioc: per adapter struct
2482 * @scmd: pointer to scsi command object
2483 *
2484 * Called by task management callback handlers.
2485 */
2486static void
2487_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2488{
2489 struct scsi_target *starget = scmd->device->sdev_target;
2490 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2491 struct _sas_device *sas_device = NULL;
2492 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302493 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302494
2495 if (!priv_target)
2496 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302497 if (ioc->hide_ir_msg)
2498 device_str = "WarpDrive";
2499 else
2500 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302501
2502 scsi_print_command(scmd);
2503 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302504 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2505 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2506 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302507 } else {
2508 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2509 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2510 priv_target->sas_address);
2511 if (sas_device) {
2512 if (priv_target->flags &
2513 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2514 starget_printk(KERN_INFO, starget,
2515 "volume handle(0x%04x), "
2516 "volume wwid(0x%016llx)\n",
2517 sas_device->volume_handle,
2518 (unsigned long long)sas_device->volume_wwid);
2519 }
2520 starget_printk(KERN_INFO, starget,
2521 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2522 sas_device->handle,
2523 (unsigned long long)sas_device->sas_address,
2524 sas_device->phy);
2525 starget_printk(KERN_INFO, starget,
2526 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2527 (unsigned long long)sas_device->enclosure_logical_id,
2528 sas_device->slot);
2529 }
2530 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2531 }
2532}
2533
2534/**
Eric Moored5d135b2009-05-18 13:02:08 -06002535 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302536 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002537 *
2538 * Returns SUCCESS if command aborted else FAILED
2539 */
2540static int
Eric Moored5d135b2009-05-18 13:02:08 -06002541_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002542{
2543 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2544 struct MPT2SAS_DEVICE *sas_device_priv_data;
2545 u16 smid;
2546 u16 handle;
2547 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002548
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302549 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2550 "scmd(%p)\n", scmd);
2551 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002552
2553 sas_device_priv_data = scmd->device->hostdata;
2554 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302555 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2556 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002557 scmd->result = DID_NO_CONNECT << 16;
2558 scmd->scsi_done(scmd);
2559 r = SUCCESS;
2560 goto out;
2561 }
2562
2563 /* search for the command */
2564 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2565 if (!smid) {
2566 scmd->result = DID_RESET << 16;
2567 r = SUCCESS;
2568 goto out;
2569 }
2570
2571 /* for hidden raid components and volumes this is not supported */
2572 if (sas_device_priv_data->sas_target->flags &
2573 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2574 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2575 scmd->result = DID_RESET << 16;
2576 r = FAILED;
2577 goto out;
2578 }
2579
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302580 mpt2sas_halt_firmware(ioc);
2581
Eric Moore635374e2009-03-09 01:21:12 -06002582 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302583 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2584 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302585 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
2586 scmd->serial_number, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002587
2588 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302589 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2590 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002591 return r;
2592}
2593
Eric Moore635374e2009-03-09 01:21:12 -06002594/**
Eric Moored5d135b2009-05-18 13:02:08 -06002595 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302596 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002597 *
2598 * Returns SUCCESS if command aborted else FAILED
2599 */
2600static int
Eric Moored5d135b2009-05-18 13:02:08 -06002601_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002602{
2603 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2604 struct MPT2SAS_DEVICE *sas_device_priv_data;
2605 struct _sas_device *sas_device;
2606 unsigned long flags;
2607 u16 handle;
2608 int r;
2609
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302610 struct scsi_target *starget = scmd->device->sdev_target;
2611
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302612 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302613 "scmd(%p)\n", scmd);
2614 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002615
2616 sas_device_priv_data = scmd->device->hostdata;
2617 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302618 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302619 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002620 scmd->result = DID_NO_CONNECT << 16;
2621 scmd->scsi_done(scmd);
2622 r = SUCCESS;
2623 goto out;
2624 }
2625
2626 /* for hidden raid components obtain the volume_handle */
2627 handle = 0;
2628 if (sas_device_priv_data->sas_target->flags &
2629 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2630 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2631 sas_device = _scsih_sas_device_find_by_handle(ioc,
2632 sas_device_priv_data->sas_target->handle);
2633 if (sas_device)
2634 handle = sas_device->volume_handle;
2635 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2636 } else
2637 handle = sas_device_priv_data->sas_target->handle;
2638
2639 if (!handle) {
2640 scmd->result = DID_RESET << 16;
2641 r = FAILED;
2642 goto out;
2643 }
2644
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302645 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2646 scmd->device->id, scmd->device->lun,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302647 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, 0,
2648 TM_MUTEX_ON);
Eric Moore993e0da2009-05-18 13:00:45 -06002649
2650 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302651 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2652 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002653 return r;
2654}
2655
2656/**
Eric Moored5d135b2009-05-18 13:02:08 -06002657 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302658 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002659 *
2660 * Returns SUCCESS if command aborted else FAILED
2661 */
2662static int
Eric Moored5d135b2009-05-18 13:02:08 -06002663_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002664{
2665 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2666 struct MPT2SAS_DEVICE *sas_device_priv_data;
2667 struct _sas_device *sas_device;
2668 unsigned long flags;
2669 u16 handle;
2670 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302671 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002672
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302673 starget_printk(KERN_INFO, starget, "attempting target reset! "
2674 "scmd(%p)\n", scmd);
2675 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002676
2677 sas_device_priv_data = scmd->device->hostdata;
2678 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302679 starget_printk(KERN_INFO, starget, "target been deleted! "
2680 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002681 scmd->result = DID_NO_CONNECT << 16;
2682 scmd->scsi_done(scmd);
2683 r = SUCCESS;
2684 goto out;
2685 }
2686
2687 /* for hidden raid components obtain the volume_handle */
2688 handle = 0;
2689 if (sas_device_priv_data->sas_target->flags &
2690 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2691 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2692 sas_device = _scsih_sas_device_find_by_handle(ioc,
2693 sas_device_priv_data->sas_target->handle);
2694 if (sas_device)
2695 handle = sas_device->volume_handle;
2696 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2697 } else
2698 handle = sas_device_priv_data->sas_target->handle;
2699
2700 if (!handle) {
2701 scmd->result = DID_RESET << 16;
2702 r = FAILED;
2703 goto out;
2704 }
2705
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302706 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2707 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302708 30, 0, TM_MUTEX_ON);
Eric Moore635374e2009-03-09 01:21:12 -06002709
2710 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302711 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2712 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002713 return r;
2714}
2715
2716/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302717 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302718 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002719 *
2720 * Returns SUCCESS if command aborted else FAILED
2721 */
2722static int
Eric Moored5d135b2009-05-18 13:02:08 -06002723_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002724{
2725 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2726 int r, retval;
2727
2728 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2729 ioc->name, scmd);
2730 scsi_print_command(scmd);
2731
2732 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2733 FORCE_BIG_HAMMER);
2734 r = (retval < 0) ? FAILED : SUCCESS;
2735 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2736 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2737
2738 return r;
2739}
2740
2741/**
2742 * _scsih_fw_event_add - insert and queue up fw_event
2743 * @ioc: per adapter object
2744 * @fw_event: object describing the event
2745 * Context: This function will acquire ioc->fw_event_lock.
2746 *
2747 * This adds the firmware event object into link list, then queues it up to
2748 * be processed from user context.
2749 *
2750 * Return nothing.
2751 */
2752static void
2753_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2754{
2755 unsigned long flags;
2756
2757 if (ioc->firmware_event_thread == NULL)
2758 return;
2759
2760 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2761 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302762 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2763 queue_delayed_work(ioc->firmware_event_thread,
2764 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002765 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2766}
2767
2768/**
2769 * _scsih_fw_event_free - delete fw_event
2770 * @ioc: per adapter object
2771 * @fw_event: object describing the event
2772 * Context: This function will acquire ioc->fw_event_lock.
2773 *
2774 * This removes firmware event object from link list, frees associated memory.
2775 *
2776 * Return nothing.
2777 */
2778static void
2779_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2780 *fw_event)
2781{
2782 unsigned long flags;
2783
2784 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2785 list_del(&fw_event->list);
2786 kfree(fw_event->event_data);
2787 kfree(fw_event);
2788 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2789}
2790
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302791
Eric Moore635374e2009-03-09 01:21:12 -06002792/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302793 * _scsih_error_recovery_delete_devices - remove devices not responding
Eric Moore635374e2009-03-09 01:21:12 -06002794 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002795 *
2796 * Return nothing.
2797 */
2798static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302799_scsih_error_recovery_delete_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002800{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302801 struct fw_event_work *fw_event;
2802
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302803 if (ioc->is_driver_loading)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302804 return;
Dan Carpenter181a9d72011-11-04 21:25:01 +03002805
2806 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2807 if (!fw_event)
2808 return;
2809
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302810 fw_event->event = MPT2SAS_REMOVE_UNRESPONDING_DEVICES;
2811 fw_event->ioc = ioc;
2812 _scsih_fw_event_add(ioc, fw_event);
2813}
2814
2815/**
2816 * mpt2sas_port_enable_complete - port enable completed (fake event)
2817 * @ioc: per adapter object
2818 *
2819 * Return nothing.
2820 */
2821void
2822mpt2sas_port_enable_complete(struct MPT2SAS_ADAPTER *ioc)
2823{
2824 struct fw_event_work *fw_event;
2825
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302826 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2827 if (!fw_event)
2828 return;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05302829 fw_event->event = MPT2SAS_PORT_ENABLE_COMPLETE;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302830 fw_event->ioc = ioc;
2831 _scsih_fw_event_add(ioc, fw_event);
2832}
2833
2834/**
2835 * _scsih_fw_event_cleanup_queue - cleanup event queue
2836 * @ioc: per adapter object
2837 *
2838 * Walk the firmware event queue, either killing timers, or waiting
2839 * for outstanding events to complete
2840 *
2841 * Return nothing.
2842 */
2843static void
2844_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2845{
2846 struct fw_event_work *fw_event, *next;
2847
2848 if (list_empty(&ioc->fw_event_list) ||
2849 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002850 return;
2851
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302852 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2853 if (cancel_delayed_work(&fw_event->delayed_work)) {
2854 _scsih_fw_event_free(ioc, fw_event);
2855 continue;
2856 }
2857 fw_event->cancel_pending_work = 1;
2858 }
Eric Moore635374e2009-03-09 01:21:12 -06002859}
2860
Eric Moore635374e2009-03-09 01:21:12 -06002861/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302862 * _scsih_ublock_io_all_device - unblock every device
2863 * @ioc: per adapter object
2864 *
2865 * change the device state from block to running
2866 */
2867static void
2868_scsih_ublock_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2869{
2870 struct MPT2SAS_DEVICE *sas_device_priv_data;
2871 struct scsi_device *sdev;
2872
2873 shost_for_each_device(sdev, ioc->shost) {
2874 sas_device_priv_data = sdev->hostdata;
2875 if (!sas_device_priv_data)
2876 continue;
2877 if (!sas_device_priv_data->block)
2878 continue;
2879 sas_device_priv_data->block = 0;
2880 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_running, "
2881 "handle(0x%04x)\n",
2882 sas_device_priv_data->sas_target->handle));
2883 scsi_internal_device_unblock(sdev);
2884 }
2885}
2886/**
Eric Moore635374e2009-03-09 01:21:12 -06002887 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2888 * @ioc: per adapter object
2889 * @handle: device handle
2890 *
2891 * During device pull we need to appropiately set the sdev state.
2892 */
2893static void
2894_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2895{
2896 struct MPT2SAS_DEVICE *sas_device_priv_data;
2897 struct scsi_device *sdev;
2898
2899 shost_for_each_device(sdev, ioc->shost) {
2900 sas_device_priv_data = sdev->hostdata;
2901 if (!sas_device_priv_data)
2902 continue;
2903 if (!sas_device_priv_data->block)
2904 continue;
2905 if (sas_device_priv_data->sas_target->handle == handle) {
2906 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2907 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2908 "handle(0x%04x)\n", ioc->name, handle));
2909 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302910 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002911 }
2912 }
2913}
2914
2915/**
Kashyap, Desaif93213d2011-06-14 10:56:43 +05302916 * _scsih_block_io_all_device - set the device state to SDEV_BLOCK
2917 * @ioc: per adapter object
2918 * @handle: device handle
2919 *
2920 * During device pull we need to appropiately set the sdev state.
2921 */
2922static void
2923_scsih_block_io_all_device(struct MPT2SAS_ADAPTER *ioc)
2924{
2925 struct MPT2SAS_DEVICE *sas_device_priv_data;
2926 struct scsi_device *sdev;
2927
2928 shost_for_each_device(sdev, ioc->shost) {
2929 sas_device_priv_data = sdev->hostdata;
2930 if (!sas_device_priv_data)
2931 continue;
2932 if (sas_device_priv_data->block)
2933 continue;
2934 sas_device_priv_data->block = 1;
2935 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev, "device_blocked, "
2936 "handle(0x%04x)\n",
2937 sas_device_priv_data->sas_target->handle));
2938 scsi_internal_device_block(sdev);
2939 }
2940}
2941
2942
2943/**
Eric Moore635374e2009-03-09 01:21:12 -06002944 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2945 * @ioc: per adapter object
2946 * @handle: device handle
2947 *
2948 * During device pull we need to appropiately set the sdev state.
2949 */
2950static void
2951_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2952{
2953 struct MPT2SAS_DEVICE *sas_device_priv_data;
2954 struct scsi_device *sdev;
2955
2956 shost_for_each_device(sdev, ioc->shost) {
2957 sas_device_priv_data = sdev->hostdata;
2958 if (!sas_device_priv_data)
2959 continue;
2960 if (sas_device_priv_data->block)
2961 continue;
2962 if (sas_device_priv_data->sas_target->handle == handle) {
2963 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2964 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2965 "handle(0x%04x)\n", ioc->name, handle));
2966 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302967 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002968 }
2969 }
2970}
2971
2972/**
2973 * _scsih_block_io_to_children_attached_to_ex
2974 * @ioc: per adapter object
2975 * @sas_expander: the sas_device object
2976 *
2977 * This routine set sdev state to SDEV_BLOCK for all devices
2978 * attached to this expander. This function called when expander is
2979 * pulled.
2980 */
2981static void
2982_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2983 struct _sas_node *sas_expander)
2984{
2985 struct _sas_port *mpt2sas_port;
2986 struct _sas_device *sas_device;
2987 struct _sas_node *expander_sibling;
2988 unsigned long flags;
2989
2990 if (!sas_expander)
2991 return;
2992
2993 list_for_each_entry(mpt2sas_port,
2994 &sas_expander->sas_port_list, port_list) {
2995 if (mpt2sas_port->remote_identify.device_type ==
2996 SAS_END_DEVICE) {
2997 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2998 sas_device =
2999 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
3000 mpt2sas_port->remote_identify.sas_address);
3001 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
3002 if (!sas_device)
3003 continue;
3004 _scsih_block_io_device(ioc, sas_device->handle);
3005 }
3006 }
3007
3008 list_for_each_entry(mpt2sas_port,
3009 &sas_expander->sas_port_list, port_list) {
3010
3011 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303012 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06003013 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05303014 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06003015
3016 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3017 expander_sibling =
3018 mpt2sas_scsih_expander_find_by_sas_address(
3019 ioc, mpt2sas_port->remote_identify.sas_address);
3020 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3021 _scsih_block_io_to_children_attached_to_ex(ioc,
3022 expander_sibling);
3023 }
3024 }
3025}
3026
3027/**
3028 * _scsih_block_io_to_children_attached_directly
3029 * @ioc: per adapter object
3030 * @event_data: topology change event data
3031 *
3032 * This routine set sdev state to SDEV_BLOCK for all devices
3033 * direct attached during device pull.
3034 */
3035static void
3036_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
3037 Mpi2EventDataSasTopologyChangeList_t *event_data)
3038{
3039 int i;
3040 u16 handle;
3041 u16 reason_code;
3042 u8 phy_number;
3043
3044 for (i = 0; i < event_data->NumEntries; i++) {
3045 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3046 if (!handle)
3047 continue;
3048 phy_number = event_data->StartPhyNum + i;
3049 reason_code = event_data->PHY[i].PhyStatus &
3050 MPI2_EVENT_SAS_TOPO_RC_MASK;
3051 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
3052 _scsih_block_io_device(ioc, handle);
3053 }
3054}
3055
3056/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303057 * _scsih_tm_tr_send - send task management request
3058 * @ioc: per adapter object
3059 * @handle: device handle
3060 * Context: interrupt time.
3061 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003062 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303063 * with controller firmware. This function will issue target reset
3064 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003065 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303066 *
3067 * This is designed to send muliple task management request at the same
3068 * time to the fifo. If the fifo is full, we will append the request,
3069 * and process it in a future completion.
3070 */
3071static void
3072_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3073{
3074 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303075 u16 smid;
3076 struct _sas_device *sas_device;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303077 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
3078 u64 sas_address = 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303079 unsigned long flags;
3080 struct _tr_list *delayed_tr;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303081 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303082
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303083 if (ioc->remove_host) {
3084 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3085 "removed: handle(0x%04x)\n", __func__, ioc->name, handle));
3086 return;
3087 } else if (ioc->pci_error_recovery) {
3088 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3089 "error recovery: handle(0x%04x)\n", __func__, ioc->name,
3090 handle));
3091 return;
3092 }
3093 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3094 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3095 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3096 "operational: handle(0x%04x)\n", __func__, ioc->name,
3097 handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303098 return;
3099 }
3100
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303101 /* if PD, then return */
3102 if (test_bit(handle, ioc->pd_handles))
3103 return;
3104
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303105 spin_lock_irqsave(&ioc->sas_device_lock, flags);
3106 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303107 if (sas_device && sas_device->starget &&
3108 sas_device->starget->hostdata) {
3109 sas_target_priv_data = sas_device->starget->hostdata;
3110 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303111 sas_address = sas_device->sas_address;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303112 }
3113 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303114
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303115 if (sas_target_priv_data) {
3116 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "setting delete flag: "
3117 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, handle,
3118 (unsigned long long)sas_address));
3119 _scsih_ublock_io_device(ioc, handle);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05303120 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
nagalakshmi.nandigama@lsi.comf3db0322011-10-19 15:37:14 +05303121 }
3122
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303123 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
3124 if (!smid) {
3125 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3126 if (!delayed_tr)
3127 return;
3128 INIT_LIST_HEAD(&delayed_tr->list);
3129 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303130 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3131 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3132 "DELAYED:tr:handle(0x%04x), (open)\n",
3133 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303134 return;
3135 }
3136
Kashyap, Desai1278b112010-03-09 17:34:13 +05303137 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3138 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3139 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303140 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3141 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3142 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3143 mpi_request->DevHandle = cpu_to_le16(handle);
3144 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303145 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3146}
3147
3148
3149
3150/**
3151 * _scsih_sas_control_complete - completion routine
3152 * @ioc: per adapter object
3153 * @smid: system request message index
3154 * @msix_index: MSIX table index supplied by the OS
3155 * @reply: reply message frame(lower 32bit addr)
3156 * Context: interrupt time.
3157 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003158 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303159 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003160 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303161 *
3162 * Return 1 meaning mf should be freed from _base_interrupt
3163 * 0 means the mf is freed from this function.
3164 */
3165static u8
3166_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3167 u8 msix_index, u32 reply)
3168{
Kashyap, Desai363fa502010-11-13 04:29:20 +05303169#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303170 Mpi2SasIoUnitControlReply_t *mpi_reply =
3171 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa502010-11-13 04:29:20 +05303172#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05303173 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3174 "sc_complete:handle(0x%04x), (open) "
3175 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
3176 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
3177 le16_to_cpu(mpi_reply->IOCStatus),
3178 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303179 return 1;
3180}
3181
3182/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303183 * _scsih_tm_tr_volume_send - send target reset request for volumes
3184 * @ioc: per adapter object
3185 * @handle: device handle
3186 * Context: interrupt time.
3187 *
3188 * This is designed to send muliple task management request at the same
3189 * time to the fifo. If the fifo is full, we will append the request,
3190 * and process it in a future completion.
3191 */
3192static void
3193_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3194{
3195 Mpi2SCSITaskManagementRequest_t *mpi_request;
3196 u16 smid;
3197 struct _tr_list *delayed_tr;
3198
Eric Moore3cb54692010-07-08 14:44:34 -06003199 if (ioc->shost_recovery || ioc->remove_host ||
3200 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303201 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3202 "progress!\n", __func__, ioc->name));
3203 return;
3204 }
3205
3206 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3207 if (!smid) {
3208 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3209 if (!delayed_tr)
3210 return;
3211 INIT_LIST_HEAD(&delayed_tr->list);
3212 delayed_tr->handle = handle;
3213 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3214 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3215 "DELAYED:tr:handle(0x%04x), (open)\n",
3216 ioc->name, handle));
3217 return;
3218 }
3219
3220 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3221 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3222 ioc->tm_tr_volume_cb_idx));
3223 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3224 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3225 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3226 mpi_request->DevHandle = cpu_to_le16(handle);
3227 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3228 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3229}
3230
3231/**
3232 * _scsih_tm_volume_tr_complete - target reset completion
3233 * @ioc: per adapter object
3234 * @smid: system request message index
3235 * @msix_index: MSIX table index supplied by the OS
3236 * @reply: reply message frame(lower 32bit addr)
3237 * Context: interrupt time.
3238 *
3239 * Return 1 meaning mf should be freed from _base_interrupt
3240 * 0 means the mf is freed from this function.
3241 */
3242static u8
3243_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3244 u8 msix_index, u32 reply)
3245{
3246 u16 handle;
3247 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3248 Mpi2SCSITaskManagementReply_t *mpi_reply =
3249 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3250
Eric Moore3cb54692010-07-08 14:44:34 -06003251 if (ioc->shost_recovery || ioc->remove_host ||
3252 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303253 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3254 "progress!\n", __func__, ioc->name));
3255 return 1;
3256 }
3257
3258 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3259 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3260 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3261 dewtprintk(ioc, printk("spurious interrupt: "
3262 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3263 le16_to_cpu(mpi_reply->DevHandle), smid));
3264 return 0;
3265 }
3266
3267 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3268 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3269 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3270 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3271 le32_to_cpu(mpi_reply->IOCLogInfo),
3272 le32_to_cpu(mpi_reply->TerminationCount)));
3273
3274 return _scsih_check_for_pending_tm(ioc, smid);
3275}
3276
3277/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303278 * _scsih_tm_tr_complete -
3279 * @ioc: per adapter object
3280 * @smid: system request message index
3281 * @msix_index: MSIX table index supplied by the OS
3282 * @reply: reply message frame(lower 32bit addr)
3283 * Context: interrupt time.
3284 *
3285 * This is the target reset completion routine.
3286 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003287 * handshake protocol with controller firmware.
3288 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303289 *
3290 * Return 1 meaning mf should be freed from _base_interrupt
3291 * 0 means the mf is freed from this function.
3292 */
3293static u8
3294_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3295 u32 reply)
3296{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303297 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303298 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303299 Mpi2SCSITaskManagementReply_t *mpi_reply =
3300 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3301 Mpi2SasIoUnitControlRequest_t *mpi_request;
3302 u16 smid_sas_ctrl;
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303303 u32 ioc_state;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303304
nagalakshmi.nandigama@lsi.comf881cea2011-10-19 15:37:00 +05303305 if (ioc->remove_host) {
3306 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host has been "
3307 "removed\n", __func__, ioc->name));
3308 return 1;
3309 } else if (ioc->pci_error_recovery) {
3310 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host in pci "
3311 "error recovery\n", __func__, ioc->name));
3312 return 1;
3313 }
3314 ioc_state = mpt2sas_base_get_iocstate(ioc, 1);
3315 if (ioc_state != MPI2_IOC_STATE_OPERATIONAL) {
3316 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host is not "
3317 "operational\n", __func__, ioc->name));
Kashyap, Desai1278b112010-03-09 17:34:13 +05303318 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303319 }
3320
Kashyap, Desai1278b112010-03-09 17:34:13 +05303321 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3322 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3323 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3324 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3325 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3326 le16_to_cpu(mpi_reply->DevHandle), smid));
3327 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303328 }
3329
Kashyap, Desai1278b112010-03-09 17:34:13 +05303330 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3331 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3332 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3333 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3334 le32_to_cpu(mpi_reply->IOCLogInfo),
3335 le32_to_cpu(mpi_reply->TerminationCount)));
3336
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303337 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3338 if (!smid_sas_ctrl) {
3339 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3340 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303341 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303342 }
3343
Kashyap, Desai1278b112010-03-09 17:34:13 +05303344 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3345 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3346 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303347 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3348 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3349 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3350 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303351 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303352 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303353
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303354 return _scsih_check_for_pending_tm(ioc, smid);
3355}
3356
3357/**
3358 * _scsih_check_for_pending_tm - check for pending task management
3359 * @ioc: per adapter object
3360 * @smid: system request message index
3361 *
3362 * This will check delayed target reset list, and feed the
3363 * next reqeust.
3364 *
3365 * Return 1 meaning mf should be freed from _base_interrupt
3366 * 0 means the mf is freed from this function.
3367 */
3368static u8
3369_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3370{
3371 struct _tr_list *delayed_tr;
3372
3373 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3374 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3375 struct _tr_list, list);
3376 mpt2sas_base_free_smid(ioc, smid);
3377 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3378 list_del(&delayed_tr->list);
3379 kfree(delayed_tr);
3380 return 0;
3381 }
3382
Kashyap, Desai1278b112010-03-09 17:34:13 +05303383 if (!list_empty(&ioc->delayed_tr_list)) {
3384 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3385 struct _tr_list, list);
3386 mpt2sas_base_free_smid(ioc, smid);
3387 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3388 list_del(&delayed_tr->list);
3389 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303390 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303391 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303392
Kashyap, Desai1278b112010-03-09 17:34:13 +05303393 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303394}
3395
3396/**
Eric Moore635374e2009-03-09 01:21:12 -06003397 * _scsih_check_topo_delete_events - sanity check on topo events
3398 * @ioc: per adapter object
3399 * @event_data: the event data payload
3400 *
3401 * This routine added to better handle cable breaker.
3402 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003403 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003404 * add and delete events in a single shot. When there is a delete event
3405 * the routine will void any pending add events waiting in the event queue.
3406 *
3407 * Return nothing.
3408 */
3409static void
3410_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3411 Mpi2EventDataSasTopologyChangeList_t *event_data)
3412{
3413 struct fw_event_work *fw_event;
3414 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3415 u16 expander_handle;
3416 struct _sas_node *sas_expander;
3417 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303418 int i, reason_code;
3419 u16 handle;
3420
3421 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303422 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3423 if (!handle)
3424 continue;
3425 reason_code = event_data->PHY[i].PhyStatus &
3426 MPI2_EVENT_SAS_TOPO_RC_MASK;
3427 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3428 _scsih_tm_tr_send(ioc, handle);
3429 }
Eric Moore635374e2009-03-09 01:21:12 -06003430
3431 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3432 if (expander_handle < ioc->sas_hba.num_phys) {
3433 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3434 return;
3435 }
3436
3437 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3438 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3439 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3440 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3441 expander_handle);
3442 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3443 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3444 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3445 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3446
3447 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3448 return;
3449
3450 /* mark ignore flag for pending events */
3451 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3452 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3453 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3454 fw_event->ignore)
3455 continue;
3456 local_event_data = fw_event->event_data;
3457 if (local_event_data->ExpStatus ==
3458 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3459 local_event_data->ExpStatus ==
3460 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3461 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3462 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303463 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003464 "setting ignoring flag\n", ioc->name));
3465 fw_event->ignore = 1;
3466 }
3467 }
3468 }
3469 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3470}
3471
3472/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303473 * _scsih_set_volume_delete_flag - setting volume delete flag
3474 * @ioc: per adapter object
3475 * @handle: device handle
3476 *
3477 * This
3478 * Return nothing.
3479 */
3480static void
3481_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3482{
3483 struct _raid_device *raid_device;
3484 struct MPT2SAS_TARGET *sas_target_priv_data;
3485 unsigned long flags;
3486
3487 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3488 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3489 if (raid_device && raid_device->starget &&
3490 raid_device->starget->hostdata) {
3491 sas_target_priv_data =
3492 raid_device->starget->hostdata;
3493 sas_target_priv_data->deleted = 1;
3494 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3495 "setting delete flag: handle(0x%04x), "
3496 "wwid(0x%016llx)\n", ioc->name, handle,
3497 (unsigned long long) raid_device->wwid));
3498 }
3499 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3500}
3501
3502/**
3503 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3504 * @handle: input handle
3505 * @a: handle for volume a
3506 * @b: handle for volume b
3507 *
3508 * IR firmware only supports two raid volumes. The purpose of this
3509 * routine is to set the volume handle in either a or b. When the given
3510 * input handle is non-zero, or when a and b have not been set before.
3511 */
3512static void
3513_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3514{
3515 if (!handle || handle == *a || handle == *b)
3516 return;
3517 if (!*a)
3518 *a = handle;
3519 else if (!*b)
3520 *b = handle;
3521}
3522
3523/**
3524 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3525 * @ioc: per adapter object
3526 * @event_data: the event data payload
3527 * Context: interrupt time.
3528 *
3529 * This routine will send target reset to volume, followed by target
3530 * resets to the PDs. This is called when a PD has been removed, or
3531 * volume has been deleted or removed. When the target reset is sent
3532 * to volume, the PD target resets need to be queued to start upon
3533 * completion of the volume target reset.
3534 *
3535 * Return nothing.
3536 */
3537static void
3538_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3539 Mpi2EventDataIrConfigChangeList_t *event_data)
3540{
3541 Mpi2EventIrConfigElement_t *element;
3542 int i;
3543 u16 handle, volume_handle, a, b;
3544 struct _tr_list *delayed_tr;
3545
3546 a = 0;
3547 b = 0;
3548
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303549 if (ioc->is_warpdrive)
3550 return;
3551
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303552 /* Volume Resets for Deleted or Removed */
3553 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3554 for (i = 0; i < event_data->NumElements; i++, element++) {
3555 if (element->ReasonCode ==
3556 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3557 element->ReasonCode ==
3558 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3559 volume_handle = le16_to_cpu(element->VolDevHandle);
3560 _scsih_set_volume_delete_flag(ioc, volume_handle);
3561 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3562 }
3563 }
3564
3565 /* Volume Resets for UNHIDE events */
3566 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3567 for (i = 0; i < event_data->NumElements; i++, element++) {
3568 if (le32_to_cpu(event_data->Flags) &
3569 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3570 continue;
3571 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3572 volume_handle = le16_to_cpu(element->VolDevHandle);
3573 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3574 }
3575 }
3576
3577 if (a)
3578 _scsih_tm_tr_volume_send(ioc, a);
3579 if (b)
3580 _scsih_tm_tr_volume_send(ioc, b);
3581
3582 /* PD target resets */
3583 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3584 for (i = 0; i < event_data->NumElements; i++, element++) {
3585 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3586 continue;
3587 handle = le16_to_cpu(element->PhysDiskDevHandle);
3588 volume_handle = le16_to_cpu(element->VolDevHandle);
3589 clear_bit(handle, ioc->pd_handles);
3590 if (!volume_handle)
3591 _scsih_tm_tr_send(ioc, handle);
3592 else if (volume_handle == a || volume_handle == b) {
3593 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3594 BUG_ON(!delayed_tr);
3595 INIT_LIST_HEAD(&delayed_tr->list);
3596 delayed_tr->handle = handle;
3597 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3598 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3599 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3600 handle));
3601 } else
3602 _scsih_tm_tr_send(ioc, handle);
3603 }
3604}
3605
3606
3607/**
3608 * _scsih_check_volume_delete_events - set delete flag for volumes
3609 * @ioc: per adapter object
3610 * @event_data: the event data payload
3611 * Context: interrupt time.
3612 *
3613 * This will handle the case when the cable connected to entire volume is
3614 * pulled. We will take care of setting the deleted flag so normal IO will
3615 * not be sent.
3616 *
3617 * Return nothing.
3618 */
3619static void
3620_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3621 Mpi2EventDataIrVolume_t *event_data)
3622{
3623 u32 state;
3624
3625 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3626 return;
3627 state = le32_to_cpu(event_data->NewValue);
3628 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3629 MPI2_RAID_VOL_STATE_FAILED)
3630 _scsih_set_volume_delete_flag(ioc,
3631 le16_to_cpu(event_data->VolDevHandle));
3632}
3633
3634/**
Eric Moore635374e2009-03-09 01:21:12 -06003635 * _scsih_flush_running_cmds - completing outstanding commands.
3636 * @ioc: per adapter object
3637 *
3638 * The flushing out of all pending scmd commands following host reset,
3639 * where all IO is dropped to the floor.
3640 *
3641 * Return nothing.
3642 */
3643static void
3644_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3645{
3646 struct scsi_cmnd *scmd;
3647 u16 smid;
3648 u16 count = 0;
3649
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303650 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303651 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003652 if (!scmd)
3653 continue;
3654 count++;
3655 mpt2sas_base_free_smid(ioc, smid);
3656 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003657 if (ioc->pci_error_recovery)
3658 scmd->result = DID_NO_CONNECT << 16;
3659 else
3660 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003661 scmd->scsi_done(scmd);
3662 }
3663 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3664 ioc->name, count));
3665}
3666
3667/**
Eric Moore3c621b32009-05-18 12:59:41 -06003668 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3669 * @scmd: pointer to scsi command object
3670 * @mpi_request: pointer to the SCSI_IO reqest message frame
3671 *
3672 * Supporting protection 1 and 3.
3673 *
3674 * Returns nothing
3675 */
3676static void
3677_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3678{
3679 u16 eedp_flags;
3680 unsigned char prot_op = scsi_get_prot_op(scmd);
3681 unsigned char prot_type = scsi_get_prot_type(scmd);
3682
Eric Moored334aa72010-04-22 10:47:40 -06003683 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003684 return;
3685
3686 if (prot_op == SCSI_PROT_READ_STRIP)
3687 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3688 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3689 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3690 else
3691 return;
3692
Eric Moore3c621b32009-05-18 12:59:41 -06003693 switch (prot_type) {
3694 case SCSI_PROT_DIF_TYPE1:
Martin K. Petersen756aca72011-05-18 00:45:22 -04003695 case SCSI_PROT_DIF_TYPE2:
Eric Moore3c621b32009-05-18 12:59:41 -06003696
3697 /*
3698 * enable ref/guard checking
3699 * auto increment ref tag
3700 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303701 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003702 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3703 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3704 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3705 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003706 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003707
Eric Moore3c621b32009-05-18 12:59:41 -06003708 case SCSI_PROT_DIF_TYPE3:
3709
3710 /*
3711 * enable guard checking
3712 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303713 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003714 break;
3715 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303716 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3717 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003718}
3719
3720/**
3721 * _scsih_eedp_error_handling - return sense code for EEDP errors
3722 * @scmd: pointer to scsi command object
3723 * @ioc_status: ioc status
3724 *
3725 * Returns nothing
3726 */
3727static void
3728_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3729{
3730 u8 ascq;
3731 u8 sk;
3732 u8 host_byte;
3733
3734 switch (ioc_status) {
3735 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3736 ascq = 0x01;
3737 break;
3738 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3739 ascq = 0x02;
3740 break;
3741 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3742 ascq = 0x03;
3743 break;
3744 default:
3745 ascq = 0x00;
3746 break;
3747 }
3748
3749 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3750 sk = ILLEGAL_REQUEST;
3751 host_byte = DID_ABORT;
3752 } else {
3753 sk = ABORTED_COMMAND;
3754 host_byte = DID_OK;
3755 }
3756
3757 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3758 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3759 SAM_STAT_CHECK_CONDITION;
3760}
3761
3762/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303763 * _scsih_scsi_direct_io_get - returns direct io flag
3764 * @ioc: per adapter object
3765 * @smid: system request message index
3766 *
3767 * Returns the smid stored scmd pointer.
3768 */
3769static inline u8
3770_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3771{
3772 return ioc->scsi_lookup[smid - 1].direct_io;
3773}
3774
3775/**
3776 * _scsih_scsi_direct_io_set - sets direct io flag
3777 * @ioc: per adapter object
3778 * @smid: system request message index
3779 * @direct_io: Zero or non-zero value to set in the direct_io flag
3780 *
3781 * Returns Nothing.
3782 */
3783static inline void
3784_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3785{
3786 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3787}
3788
3789
3790/**
3791 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3792 * @ioc: per adapter object
3793 * @scmd: pointer to scsi command object
3794 * @raid_device: pointer to raid device data structure
3795 * @mpi_request: pointer to the SCSI_IO reqest message frame
3796 * @smid: system request message index
3797 *
3798 * Returns nothing
3799 */
3800static void
3801_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3802 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3803 u16 smid)
3804{
3805 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3806 u32 stripe_sz, stripe_exp;
3807 u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3808 u8 cdb0 = scmd->cmnd[0];
3809
3810 /*
3811 * Try Direct I/O to RAID memeber disks
3812 */
3813 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3814 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3815 cdb_ptr = mpi_request->CDB.CDB32;
3816
3817 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3818 | cdb_ptr[5])) {
3819 io_size = scsi_bufflen(scmd) >> 9;
3820 /* get virtual lba */
3821 lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3822 &cdb_ptr[6];
3823 tmp_ptr = (u8 *)&v_lba + 3;
3824 *tmp_ptr-- = *lba_ptr1++;
3825 *tmp_ptr-- = *lba_ptr1++;
3826 *tmp_ptr-- = *lba_ptr1++;
3827 *tmp_ptr = *lba_ptr1;
3828
3829 if (((u64)v_lba + (u64)io_size - 1) <=
3830 (u32)raid_device->max_lba) {
3831 stripe_sz = raid_device->stripe_sz;
3832 stripe_exp = raid_device->stripe_exponent;
3833 stripe_off = v_lba & (stripe_sz - 1);
3834
3835 /* Check whether IO falls within a stripe */
3836 if ((stripe_off + io_size) <= stripe_sz) {
3837 num_pds = raid_device->num_pds;
3838 p_lba = v_lba >> stripe_exp;
3839 stripe_unit = p_lba / num_pds;
3840 column = p_lba % num_pds;
3841 p_lba = (stripe_unit << stripe_exp) +
3842 stripe_off;
3843 mpi_request->DevHandle =
3844 cpu_to_le16(raid_device->
3845 pd_handle[column]);
3846 tmp_ptr = (u8 *)&p_lba + 3;
3847 *lba_ptr2++ = *tmp_ptr--;
3848 *lba_ptr2++ = *tmp_ptr--;
3849 *lba_ptr2++ = *tmp_ptr--;
3850 *lba_ptr2 = *tmp_ptr;
3851 /*
3852 * WD: To indicate this I/O is directI/O
3853 */
3854 _scsih_scsi_direct_io_set(ioc, smid, 1);
3855 }
3856 }
3857 }
3858 }
3859}
3860
3861/**
Eric Moored5d135b2009-05-18 13:02:08 -06003862 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003863 * @scmd: pointer to scsi command object
3864 * @done: function pointer to be invoked on completion
3865 *
3866 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3867 *
3868 * Returns 0 on success. If there's a failure, return either:
3869 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3870 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3871 */
3872static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003873_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003874{
3875 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3876 struct MPT2SAS_DEVICE *sas_device_priv_data;
3877 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303878 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003879 Mpi2SCSIIORequest_t *mpi_request;
3880 u32 mpi_control;
3881 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003882
3883 scmd->scsi_done = done;
3884 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303885 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003886 scmd->result = DID_NO_CONNECT << 16;
3887 scmd->scsi_done(scmd);
3888 return 0;
3889 }
3890
Kashyap, Desai78215782011-06-14 10:57:08 +05303891 if (ioc->pci_error_recovery || ioc->remove_host) {
Eric Moore3cb54692010-07-08 14:44:34 -06003892 scmd->result = DID_NO_CONNECT << 16;
3893 scmd->scsi_done(scmd);
3894 return 0;
3895 }
3896
Eric Moore635374e2009-03-09 01:21:12 -06003897 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303898 /* invalid device handle */
3899 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003900 scmd->result = DID_NO_CONNECT << 16;
3901 scmd->scsi_done(scmd);
3902 return 0;
3903 }
3904
Kashyap, Desai130b9582010-04-08 17:54:32 +05303905 /* host recovery or link resets sent via IOCTLs */
3906 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003907 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003908 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303909 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3910 return SCSI_MLQUEUE_DEVICE_BUSY;
3911 /* device has been deleted */
3912 else if (sas_target_priv_data->deleted) {
3913 scmd->result = DID_NO_CONNECT << 16;
3914 scmd->scsi_done(scmd);
3915 return 0;
3916 }
Eric Moore635374e2009-03-09 01:21:12 -06003917
3918 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3919 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3920 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3921 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3922 else
3923 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3924
3925 /* set tags */
3926 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3927 if (scmd->device->tagged_supported) {
3928 if (scmd->device->ordered_tags)
3929 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3930 else
3931 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3932 } else
3933/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3934/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3935 */
3936 mpi_control |= (0x500);
3937
3938 } else
3939 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303940 /* Make sure Device is not raid volume.
3941 * We do not expose raid functionality to upper layer for warpdrive.
3942 */
3943 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003944 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003945 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3946
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303947 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003948 if (!smid) {
3949 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3950 ioc->name, __func__);
3951 goto out;
3952 }
3953 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3954 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003955 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003956 if (scmd->cmd_len == 32)
3957 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003958 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3959 if (sas_device_priv_data->sas_target->flags &
3960 MPT_TARGET_FLAGS_RAID_COMPONENT)
3961 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3962 else
3963 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3964 mpi_request->DevHandle =
3965 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3966 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3967 mpi_request->Control = cpu_to_le32(mpi_control);
3968 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3969 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3970 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3971 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303972 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003973 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3974 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3975 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303976 mpi_request->VF_ID = 0; /* TODO */
3977 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003978 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3979 mpi_request->LUN);
3980 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3981
3982 if (!mpi_request->DataLength) {
3983 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3984 } else {
3985 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3986 mpt2sas_base_free_smid(ioc, smid);
3987 goto out;
3988 }
3989 }
3990
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303991 raid_device = sas_target_priv_data->raid_device;
3992 if (raid_device && raid_device->direct_io_enabled)
3993 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3994 smid);
3995
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303996 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3997 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303998 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303999 else
4000 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004001 return 0;
4002
4003 out:
4004 return SCSI_MLQUEUE_HOST_BUSY;
4005}
4006
Jeff Garzikf2812332010-11-16 02:10:29 -05004007static DEF_SCSI_QCMD(_scsih_qcmd)
4008
Eric Moore635374e2009-03-09 01:21:12 -06004009/**
4010 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
4011 * @sense_buffer: sense data returned by target
4012 * @data: normalized skey/asc/ascq
4013 *
4014 * Return nothing.
4015 */
4016static void
4017_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
4018{
4019 if ((sense_buffer[0] & 0x7F) >= 0x72) {
4020 /* descriptor format */
4021 data->skey = sense_buffer[1] & 0x0F;
4022 data->asc = sense_buffer[2];
4023 data->ascq = sense_buffer[3];
4024 } else {
4025 /* fixed format */
4026 data->skey = sense_buffer[2] & 0x0F;
4027 data->asc = sense_buffer[12];
4028 data->ascq = sense_buffer[13];
4029 }
4030}
4031
4032#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4033/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004034 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06004035 * @ioc: per adapter object
4036 * @scmd: pointer to scsi command object
4037 * @mpi_reply: reply mf payload returned from firmware
4038 *
4039 * scsi_status - SCSI Status code returned from target device
4040 * scsi_state - state info associated with SCSI_IO determined by ioc
4041 * ioc_status - ioc supplied status info
4042 *
4043 * Return nothing.
4044 */
4045static void
4046_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
4047 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
4048{
4049 u32 response_info;
4050 u8 *response_bytes;
4051 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
4052 MPI2_IOCSTATUS_MASK;
4053 u8 scsi_state = mpi_reply->SCSIState;
4054 u8 scsi_status = mpi_reply->SCSIStatus;
4055 char *desc_ioc_state = NULL;
4056 char *desc_scsi_status = NULL;
4057 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304058 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304059 struct _sas_device *sas_device = NULL;
4060 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304061 struct scsi_target *starget = scmd->device->sdev_target;
4062 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304063 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304064
4065 if (!priv_target)
4066 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304067
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304068 if (ioc->hide_ir_msg)
4069 device_str = "WarpDrive";
4070 else
4071 device_str = "volume";
4072
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05304073 if (log_info == 0x31170000)
4074 return;
Eric Moore635374e2009-03-09 01:21:12 -06004075
4076 switch (ioc_status) {
4077 case MPI2_IOCSTATUS_SUCCESS:
4078 desc_ioc_state = "success";
4079 break;
4080 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4081 desc_ioc_state = "invalid function";
4082 break;
4083 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4084 desc_ioc_state = "scsi recovered error";
4085 break;
4086 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
4087 desc_ioc_state = "scsi invalid dev handle";
4088 break;
4089 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4090 desc_ioc_state = "scsi device not there";
4091 break;
4092 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4093 desc_ioc_state = "scsi data overrun";
4094 break;
4095 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4096 desc_ioc_state = "scsi data underrun";
4097 break;
4098 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4099 desc_ioc_state = "scsi io data error";
4100 break;
4101 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4102 desc_ioc_state = "scsi protocol error";
4103 break;
4104 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4105 desc_ioc_state = "scsi task terminated";
4106 break;
4107 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4108 desc_ioc_state = "scsi residual mismatch";
4109 break;
4110 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4111 desc_ioc_state = "scsi task mgmt failed";
4112 break;
4113 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4114 desc_ioc_state = "scsi ioc terminated";
4115 break;
4116 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4117 desc_ioc_state = "scsi ext terminated";
4118 break;
Eric Moore3c621b32009-05-18 12:59:41 -06004119 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4120 desc_ioc_state = "eedp guard error";
4121 break;
4122 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4123 desc_ioc_state = "eedp ref tag error";
4124 break;
4125 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4126 desc_ioc_state = "eedp app tag error";
4127 break;
Eric Moore635374e2009-03-09 01:21:12 -06004128 default:
4129 desc_ioc_state = "unknown";
4130 break;
4131 }
4132
4133 switch (scsi_status) {
4134 case MPI2_SCSI_STATUS_GOOD:
4135 desc_scsi_status = "good";
4136 break;
4137 case MPI2_SCSI_STATUS_CHECK_CONDITION:
4138 desc_scsi_status = "check condition";
4139 break;
4140 case MPI2_SCSI_STATUS_CONDITION_MET:
4141 desc_scsi_status = "condition met";
4142 break;
4143 case MPI2_SCSI_STATUS_BUSY:
4144 desc_scsi_status = "busy";
4145 break;
4146 case MPI2_SCSI_STATUS_INTERMEDIATE:
4147 desc_scsi_status = "intermediate";
4148 break;
4149 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
4150 desc_scsi_status = "intermediate condmet";
4151 break;
4152 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
4153 desc_scsi_status = "reservation conflict";
4154 break;
4155 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
4156 desc_scsi_status = "command terminated";
4157 break;
4158 case MPI2_SCSI_STATUS_TASK_SET_FULL:
4159 desc_scsi_status = "task set full";
4160 break;
4161 case MPI2_SCSI_STATUS_ACA_ACTIVE:
4162 desc_scsi_status = "aca active";
4163 break;
4164 case MPI2_SCSI_STATUS_TASK_ABORTED:
4165 desc_scsi_status = "task aborted";
4166 break;
4167 default:
4168 desc_scsi_status = "unknown";
4169 break;
4170 }
4171
4172 desc_scsi_state[0] = '\0';
4173 if (!scsi_state)
4174 desc_scsi_state = " ";
4175 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4176 strcat(desc_scsi_state, "response info ");
4177 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4178 strcat(desc_scsi_state, "state terminated ");
4179 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
4180 strcat(desc_scsi_state, "no status ");
4181 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
4182 strcat(desc_scsi_state, "autosense failed ");
4183 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4184 strcat(desc_scsi_state, "autosense valid ");
4185
4186 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304187
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304188 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304189 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4190 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304191 } else {
4192 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4193 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4194 priv_target->sas_address);
4195 if (sas_device) {
4196 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4197 "phy(%d)\n", ioc->name, sas_device->sas_address,
4198 sas_device->phy);
4199 printk(MPT2SAS_WARN_FMT
4200 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4201 ioc->name, sas_device->enclosure_logical_id,
4202 sas_device->slot);
4203 }
4204 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304205 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304206
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304207 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4208 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4209 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004210 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4211 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4212 scsi_get_resid(scmd));
4213 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4214 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4215 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4216 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4217 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4218 scsi_status, desc_scsi_state, scsi_state);
4219
4220 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4221 struct sense_info data;
4222 _scsih_normalize_sense(scmd->sense_buffer, &data);
4223 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304224 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4225 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004226 }
4227
4228 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4229 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4230 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304231 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004232 }
4233}
4234#endif
4235
4236/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304237 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004238 * @ioc: per adapter object
4239 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304240 * Context: process
4241 *
4242 * Return nothing.
4243 */
4244static void
4245_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4246{
4247 Mpi2SepReply_t mpi_reply;
4248 Mpi2SepRequest_t mpi_request;
4249
4250 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4251 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4252 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4253 mpi_request.SlotStatus =
4254 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4255 mpi_request.DevHandle = cpu_to_le16(handle);
4256 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4257 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4258 &mpi_request)) != 0) {
4259 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4260 __FILE__, __LINE__, __func__);
4261 return;
4262 }
4263
4264 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4265 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4266 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4267 le16_to_cpu(mpi_reply.IOCStatus),
4268 le32_to_cpu(mpi_reply.IOCLogInfo)));
4269 return;
4270 }
4271}
4272
4273/**
4274 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4275 * @ioc: per adapter object
4276 * @handle: device handle
4277 * Context: interrupt.
4278 *
4279 * Return nothing.
4280 */
4281static void
4282_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4283{
4284 struct fw_event_work *fw_event;
4285
4286 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4287 if (!fw_event)
4288 return;
4289 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4290 fw_event->device_handle = handle;
4291 fw_event->ioc = ioc;
4292 _scsih_fw_event_add(ioc, fw_event);
4293}
4294
4295/**
4296 * _scsih_smart_predicted_fault - process smart errors
4297 * @ioc: per adapter object
4298 * @handle: device handle
4299 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004300 *
4301 * Return nothing.
4302 */
4303static void
4304_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4305{
Eric Moore635374e2009-03-09 01:21:12 -06004306 struct scsi_target *starget;
4307 struct MPT2SAS_TARGET *sas_target_priv_data;
4308 Mpi2EventNotificationReply_t *event_reply;
4309 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4310 struct _sas_device *sas_device;
4311 ssize_t sz;
4312 unsigned long flags;
4313
4314 /* only handle non-raid devices */
4315 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4316 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4317 if (!sas_device) {
4318 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4319 return;
4320 }
4321 starget = sas_device->starget;
4322 sas_target_priv_data = starget->hostdata;
4323
4324 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4325 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4326 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4327 return;
4328 }
4329 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4330 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4331
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304332 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4333 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004334
4335 /* insert into event log */
4336 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4337 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
Anton Blanchardf6a290b2011-11-07 22:05:21 +11004338 event_reply = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06004339 if (!event_reply) {
4340 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4341 ioc->name, __FILE__, __LINE__, __func__);
4342 return;
4343 }
4344
4345 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4346 event_reply->Event =
4347 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4348 event_reply->MsgLength = sz/4;
4349 event_reply->EventDataLength =
4350 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4351 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4352 event_reply->EventData;
4353 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4354 event_data->ASC = 0x5D;
4355 event_data->DevHandle = cpu_to_le16(handle);
4356 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4357 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4358 kfree(event_reply);
4359}
4360
4361/**
Eric Moored5d135b2009-05-18 13:02:08 -06004362 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004363 * @ioc: per adapter object
4364 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304365 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004366 * @reply: reply message frame(lower 32bit addr)
4367 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304368 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004369 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304370 * Return 1 meaning mf should be freed from _base_interrupt
4371 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004372 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304373static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304374_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004375{
4376 Mpi2SCSIIORequest_t *mpi_request;
4377 Mpi2SCSIIOReply_t *mpi_reply;
4378 struct scsi_cmnd *scmd;
4379 u16 ioc_status;
4380 u32 xfer_cnt;
4381 u8 scsi_state;
4382 u8 scsi_status;
4383 u32 log_info;
4384 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304385 u32 response_code = 0;
Kashyap, Desai82a45252011-07-05 12:40:23 +05304386 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06004387
4388 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304389 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004390 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304391 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004392
4393 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4394
4395 if (mpi_reply == NULL) {
4396 scmd->result = DID_OK << 16;
4397 goto out;
4398 }
4399
4400 sas_device_priv_data = scmd->device->hostdata;
4401 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4402 sas_device_priv_data->sas_target->deleted) {
4403 scmd->result = DID_NO_CONNECT << 16;
4404 goto out;
4405 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304406 /*
4407 * WARPDRIVE: If direct_io is set then it is directIO,
4408 * the failed direct I/O should be redirected to volume
4409 */
4410 if (_scsih_scsi_direct_io_get(ioc, smid)) {
Kashyap, Desai82a45252011-07-05 12:40:23 +05304411 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
4412 ioc->scsi_lookup[smid - 1].scmd = scmd;
4413 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304414 _scsih_scsi_direct_io_set(ioc, smid, 0);
4415 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4416 mpi_request->DevHandle =
4417 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4418 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4419 sas_device_priv_data->sas_target->handle);
4420 return 0;
4421 }
4422
Eric Moore635374e2009-03-09 01:21:12 -06004423
4424 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304425 scsi_state = mpi_reply->SCSIState;
4426 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4427 response_code =
4428 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004429 if (!sas_device_priv_data->tlr_snoop_check) {
4430 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304431 /* Make sure Device is not raid volume.
4432 * We do not expose raid functionality to upper layer for warpdrive.
4433 */
4434 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304435 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304436 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4437 sas_disable_tlr(scmd->device);
4438 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4439 }
Eric Moore635374e2009-03-09 01:21:12 -06004440 }
4441
4442 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4443 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4444 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4445 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4446 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4447 else
4448 log_info = 0;
4449 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004450 scsi_status = mpi_reply->SCSIStatus;
4451
4452 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4453 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4454 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4455 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4456 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4457 }
4458
4459 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4460 struct sense_info data;
4461 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4462 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004463 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004464 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004465 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004466 _scsih_normalize_sense(scmd->sense_buffer, &data);
4467 /* failure prediction threshold exceeded */
4468 if (data.asc == 0x5D)
4469 _scsih_smart_predicted_fault(ioc,
4470 le16_to_cpu(mpi_reply->DevHandle));
4471 }
4472
4473 switch (ioc_status) {
4474 case MPI2_IOCSTATUS_BUSY:
4475 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4476 scmd->result = SAM_STAT_BUSY;
4477 break;
4478
4479 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4480 scmd->result = DID_NO_CONNECT << 16;
4481 break;
4482
4483 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4484 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304485 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4486 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004487 }
nagalakshmi.nandigama@lsi.com3ade1ca2011-12-01 07:42:40 +05304488 scmd->result = DID_SOFT_ERROR << 16;
4489 break;
Eric Moore635374e2009-03-09 01:21:12 -06004490 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4491 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4492 scmd->result = DID_RESET << 16;
4493 break;
4494
4495 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4496 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4497 scmd->result = DID_SOFT_ERROR << 16;
4498 else
4499 scmd->result = (DID_OK << 16) | scsi_status;
4500 break;
4501
4502 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4503 scmd->result = (DID_OK << 16) | scsi_status;
4504
4505 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4506 break;
4507
4508 if (xfer_cnt < scmd->underflow) {
4509 if (scsi_status == SAM_STAT_BUSY)
4510 scmd->result = SAM_STAT_BUSY;
4511 else
4512 scmd->result = DID_SOFT_ERROR << 16;
4513 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4514 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4515 scmd->result = DID_SOFT_ERROR << 16;
4516 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4517 scmd->result = DID_RESET << 16;
4518 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4519 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4520 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4521 scmd->result = (DRIVER_SENSE << 24) |
4522 SAM_STAT_CHECK_CONDITION;
4523 scmd->sense_buffer[0] = 0x70;
4524 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4525 scmd->sense_buffer[12] = 0x20;
4526 scmd->sense_buffer[13] = 0;
4527 }
4528 break;
4529
4530 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4531 scsi_set_resid(scmd, 0);
4532 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4533 case MPI2_IOCSTATUS_SUCCESS:
4534 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304535 if (response_code ==
4536 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4537 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4538 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004539 scmd->result = DID_SOFT_ERROR << 16;
4540 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4541 scmd->result = DID_RESET << 16;
4542 break;
4543
Eric Moore3c621b32009-05-18 12:59:41 -06004544 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4545 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4546 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4547 _scsih_eedp_error_handling(scmd, ioc_status);
4548 break;
Eric Moore635374e2009-03-09 01:21:12 -06004549 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4550 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4551 case MPI2_IOCSTATUS_INVALID_SGL:
4552 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4553 case MPI2_IOCSTATUS_INVALID_FIELD:
4554 case MPI2_IOCSTATUS_INVALID_STATE:
4555 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4556 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4557 default:
4558 scmd->result = DID_SOFT_ERROR << 16;
4559 break;
4560
4561 }
4562
4563#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4564 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4565 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4566#endif
4567
4568 out:
4569 scsi_dma_unmap(scmd);
4570 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304571 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004572}
4573
4574/**
Eric Moore635374e2009-03-09 01:21:12 -06004575 * _scsih_sas_host_refresh - refreshing sas host object contents
4576 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004577 * Context: user
4578 *
4579 * During port enable, fw will send topology events for every device. Its
4580 * possible that the handles may change from the previous setting, so this
4581 * code keeping handles updating if changed.
4582 *
4583 * Return nothing.
4584 */
4585static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304586_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004587{
4588 u16 sz;
4589 u16 ioc_status;
4590 int i;
4591 Mpi2ConfigReply_t mpi_reply;
4592 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304593 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304594 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004595
4596 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4597 "updating handles for sas_host(0x%016llx)\n",
4598 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4599
4600 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4601 * sizeof(Mpi2SasIOUnit0PhyData_t));
4602 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4603 if (!sas_iounit_pg0) {
4604 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4605 ioc->name, __FILE__, __LINE__, __func__);
4606 return;
4607 }
Eric Moore635374e2009-03-09 01:21:12 -06004608
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304609 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4610 sas_iounit_pg0, sz)) != 0)
4611 goto out;
4612 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4613 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4614 goto out;
4615 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304616 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304617 if (i == 0)
4618 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4619 PhyData[0].ControllerDevHandle);
4620 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4621 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4622 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304623 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4624 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304625 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304626 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304627 }
Eric Moore635374e2009-03-09 01:21:12 -06004628 out:
4629 kfree(sas_iounit_pg0);
4630}
4631
4632/**
4633 * _scsih_sas_host_add - create sas host object
4634 * @ioc: per adapter object
4635 *
4636 * Creating host side data object, stored in ioc->sas_hba
4637 *
4638 * Return nothing.
4639 */
4640static void
4641_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4642{
4643 int i;
4644 Mpi2ConfigReply_t mpi_reply;
4645 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4646 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4647 Mpi2SasPhyPage0_t phy_pg0;
4648 Mpi2SasDevicePage0_t sas_device_pg0;
4649 Mpi2SasEnclosurePage0_t enclosure_pg0;
4650 u16 ioc_status;
4651 u16 sz;
4652 u16 device_missing_delay;
4653
4654 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4655 if (!ioc->sas_hba.num_phys) {
4656 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4657 ioc->name, __FILE__, __LINE__, __func__);
4658 return;
4659 }
4660
4661 /* sas_iounit page 0 */
4662 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4663 sizeof(Mpi2SasIOUnit0PhyData_t));
4664 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4665 if (!sas_iounit_pg0) {
4666 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4667 ioc->name, __FILE__, __LINE__, __func__);
4668 return;
4669 }
4670 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4671 sas_iounit_pg0, sz))) {
4672 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4673 ioc->name, __FILE__, __LINE__, __func__);
4674 goto out;
4675 }
4676 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4677 MPI2_IOCSTATUS_MASK;
4678 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4679 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4680 ioc->name, __FILE__, __LINE__, __func__);
4681 goto out;
4682 }
4683
4684 /* sas_iounit page 1 */
4685 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4686 sizeof(Mpi2SasIOUnit1PhyData_t));
4687 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4688 if (!sas_iounit_pg1) {
4689 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4690 ioc->name, __FILE__, __LINE__, __func__);
4691 goto out;
4692 }
4693 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4694 sas_iounit_pg1, sz))) {
4695 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4696 ioc->name, __FILE__, __LINE__, __func__);
4697 goto out;
4698 }
4699 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4700 MPI2_IOCSTATUS_MASK;
4701 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4702 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4703 ioc->name, __FILE__, __LINE__, __func__);
4704 goto out;
4705 }
4706
4707 ioc->io_missing_delay =
4708 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4709 device_missing_delay =
4710 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4711 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4712 ioc->device_missing_delay = (device_missing_delay &
4713 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4714 else
4715 ioc->device_missing_delay = device_missing_delay &
4716 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4717
4718 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4719 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4720 sizeof(struct _sas_phy), GFP_KERNEL);
4721 if (!ioc->sas_hba.phy) {
4722 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4723 ioc->name, __FILE__, __LINE__, __func__);
4724 goto out;
4725 }
4726 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4727 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4728 i))) {
4729 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4730 ioc->name, __FILE__, __LINE__, __func__);
4731 goto out;
4732 }
4733 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4734 MPI2_IOCSTATUS_MASK;
4735 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4736 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4737 ioc->name, __FILE__, __LINE__, __func__);
4738 goto out;
4739 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304740
4741 if (i == 0)
4742 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4743 PhyData[0].ControllerDevHandle);
4744 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004745 ioc->sas_hba.phy[i].phy_id = i;
4746 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4747 phy_pg0, ioc->sas_hba.parent_dev);
4748 }
4749 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304750 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004751 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4752 ioc->name, __FILE__, __LINE__, __func__);
4753 goto out;
4754 }
Eric Moore635374e2009-03-09 01:21:12 -06004755 ioc->sas_hba.enclosure_handle =
4756 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4757 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4758 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4759 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4760 (unsigned long long) ioc->sas_hba.sas_address,
4761 ioc->sas_hba.num_phys) ;
4762
4763 if (ioc->sas_hba.enclosure_handle) {
4764 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4765 &enclosure_pg0,
4766 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4767 ioc->sas_hba.enclosure_handle))) {
4768 ioc->sas_hba.enclosure_logical_id =
4769 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4770 }
4771 }
4772
4773 out:
4774 kfree(sas_iounit_pg1);
4775 kfree(sas_iounit_pg0);
4776}
4777
4778/**
4779 * _scsih_expander_add - creating expander object
4780 * @ioc: per adapter object
4781 * @handle: expander handle
4782 *
4783 * Creating expander object, stored in ioc->sas_expander_list.
4784 *
4785 * Return 0 for success, else error.
4786 */
4787static int
4788_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4789{
4790 struct _sas_node *sas_expander;
4791 Mpi2ConfigReply_t mpi_reply;
4792 Mpi2ExpanderPage0_t expander_pg0;
4793 Mpi2ExpanderPage1_t expander_pg1;
4794 Mpi2SasEnclosurePage0_t enclosure_pg0;
4795 u32 ioc_status;
4796 u16 parent_handle;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05304797 u64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004798 int i;
4799 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304800 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004801 int rc = 0;
4802
4803 if (!handle)
4804 return -1;
4805
Eric Moore3cb54692010-07-08 14:44:34 -06004806 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304807 return -1;
4808
Eric Moore635374e2009-03-09 01:21:12 -06004809 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4810 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4811 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4812 ioc->name, __FILE__, __LINE__, __func__);
4813 return -1;
4814 }
4815
4816 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4817 MPI2_IOCSTATUS_MASK;
4818 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4819 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4820 ioc->name, __FILE__, __LINE__, __func__);
4821 return -1;
4822 }
4823
4824 /* handle out of order topology events */
4825 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304826 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4827 != 0) {
4828 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4829 ioc->name, __FILE__, __LINE__, __func__);
4830 return -1;
4831 }
4832 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004833 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304834 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4835 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004836 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4837 if (!sas_expander) {
4838 rc = _scsih_expander_add(ioc, parent_handle);
4839 if (rc != 0)
4840 return rc;
4841 }
4842 }
4843
Eric Moore635374e2009-03-09 01:21:12 -06004844 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304845 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004846 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4847 sas_address);
4848 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4849
4850 if (sas_expander)
4851 return 0;
4852
4853 sas_expander = kzalloc(sizeof(struct _sas_node),
4854 GFP_KERNEL);
4855 if (!sas_expander) {
4856 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4857 ioc->name, __FILE__, __LINE__, __func__);
4858 return -1;
4859 }
4860
4861 sas_expander->handle = handle;
4862 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304863 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004864 sas_expander->sas_address = sas_address;
4865
4866 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4867 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304868 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004869 sas_expander->sas_address, sas_expander->num_phys);
4870
4871 if (!sas_expander->num_phys)
4872 goto out_fail;
4873 sas_expander->phy = kcalloc(sas_expander->num_phys,
4874 sizeof(struct _sas_phy), GFP_KERNEL);
4875 if (!sas_expander->phy) {
4876 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4877 ioc->name, __FILE__, __LINE__, __func__);
4878 rc = -1;
4879 goto out_fail;
4880 }
4881
4882 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4883 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304884 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004885 if (!mpt2sas_port) {
4886 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4887 ioc->name, __FILE__, __LINE__, __func__);
4888 rc = -1;
4889 goto out_fail;
4890 }
4891 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4892
4893 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4894 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4895 &expander_pg1, i, handle))) {
4896 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4897 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304898 rc = -1;
4899 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004900 }
4901 sas_expander->phy[i].handle = handle;
4902 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304903
4904 if ((mpt2sas_transport_add_expander_phy(ioc,
4905 &sas_expander->phy[i], expander_pg1,
4906 sas_expander->parent_dev))) {
4907 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4908 ioc->name, __FILE__, __LINE__, __func__);
4909 rc = -1;
4910 goto out_fail;
4911 }
Eric Moore635374e2009-03-09 01:21:12 -06004912 }
4913
4914 if (sas_expander->enclosure_handle) {
4915 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4916 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4917 sas_expander->enclosure_handle))) {
4918 sas_expander->enclosure_logical_id =
4919 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4920 }
4921 }
4922
4923 _scsih_expander_node_add(ioc, sas_expander);
4924 return 0;
4925
4926 out_fail:
4927
Kashyap, Desai20f58952009-08-07 19:34:26 +05304928 if (mpt2sas_port)
4929 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304930 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004931 kfree(sas_expander);
4932 return rc;
4933}
4934
4935/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304936 * _scsih_done - scsih callback handler.
4937 * @ioc: per adapter object
4938 * @smid: system request message index
4939 * @msix_index: MSIX table index supplied by the OS
4940 * @reply: reply message frame(lower 32bit addr)
4941 *
4942 * Callback handler when sending internal generated message frames.
4943 * The callback index passed is `ioc->scsih_cb_idx`
4944 *
4945 * Return 1 meaning mf should be freed from _base_interrupt
4946 * 0 means the mf is freed from this function.
4947 */
4948static u8
4949_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4950{
4951 MPI2DefaultReply_t *mpi_reply;
4952
4953 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4954 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4955 return 1;
4956 if (ioc->scsih_cmds.smid != smid)
4957 return 1;
4958 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4959 if (mpi_reply) {
4960 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4961 mpi_reply->MsgLength*4);
4962 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4963 }
4964 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4965 complete(&ioc->scsih_cmds.done);
4966 return 1;
4967}
4968
4969/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304970 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004971 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304972 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004973 *
4974 * Return nothing.
4975 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304976void
4977mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004978{
4979 struct _sas_node *sas_expander;
4980 unsigned long flags;
4981
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304982 if (ioc->shost_recovery)
4983 return;
4984
Eric Moore635374e2009-03-09 01:21:12 -06004985 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304986 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4987 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304988 if (!sas_expander) {
4989 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4990 return;
4991 }
4992 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004993 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4994 _scsih_expander_node_remove(ioc, sas_expander);
4995}
4996
4997/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304998 * _scsih_check_access_status - check access flags
4999 * @ioc: per adapter object
5000 * @sas_address: sas address
5001 * @handle: sas device handle
5002 * @access_flags: errors returned during discovery of the device
5003 *
5004 * Return 0 for success, else failure
5005 */
5006static u8
5007_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
5008 u16 handle, u8 access_status)
5009{
5010 u8 rc = 1;
5011 char *desc = NULL;
5012
5013 switch (access_status) {
5014 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
5015 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
5016 rc = 0;
5017 break;
5018 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
5019 desc = "sata capability failed";
5020 break;
5021 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
5022 desc = "sata affiliation conflict";
5023 break;
5024 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
5025 desc = "route not addressable";
5026 break;
5027 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
5028 desc = "smp error not addressable";
5029 break;
5030 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
5031 desc = "device blocked";
5032 break;
5033 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
5034 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
5035 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
5036 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
5037 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
5038 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
5039 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
5040 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
5041 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
5042 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
5043 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
5044 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
5045 desc = "sata initialization failed";
5046 break;
5047 default:
5048 desc = "unknown";
5049 break;
5050 }
5051
5052 if (!rc)
5053 return 0;
5054
5055 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
5056 "handle(0x%04x)\n", ioc->name, desc,
5057 (unsigned long long)sas_address, handle);
5058 return rc;
5059}
5060
5061static void
5062_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
5063{
5064 Mpi2ConfigReply_t mpi_reply;
5065 Mpi2SasDevicePage0_t sas_device_pg0;
5066 struct _sas_device *sas_device;
5067 u32 ioc_status;
5068 unsigned long flags;
5069 u64 sas_address;
5070 struct scsi_target *starget;
5071 struct MPT2SAS_TARGET *sas_target_priv_data;
5072 u32 device_info;
5073
5074 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5075 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
5076 return;
5077
5078 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
5079 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
5080 return;
5081
5082 /* check if this is end device */
5083 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5084 if (!(_scsih_is_end_device(device_info)))
5085 return;
5086
5087 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5088 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5089 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5090 sas_address);
5091
5092 if (!sas_device) {
5093 printk(MPT2SAS_ERR_FMT "device is not present "
5094 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
5095 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5096 return;
5097 }
5098
5099 if (unlikely(sas_device->handle != handle)) {
5100 starget = sas_device->starget;
5101 sas_target_priv_data = starget->hostdata;
5102 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
5103 " to (0x%04x)!!!\n", sas_device->handle, handle);
5104 sas_target_priv_data->handle = handle;
5105 sas_device->handle = handle;
5106 }
5107 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5108
5109 /* check if device is present */
5110 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5111 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5112 printk(MPT2SAS_ERR_FMT "device is not present "
5113 "handle(0x%04x), flags!!!\n", ioc->name, handle);
5114 return;
5115 }
5116
5117 /* check if there were any issues with discovery */
5118 if (_scsih_check_access_status(ioc, sas_address, handle,
5119 sas_device_pg0.AccessStatus))
5120 return;
5121 _scsih_ublock_io_device(ioc, handle);
5122
5123}
5124
5125/**
Eric Moore635374e2009-03-09 01:21:12 -06005126 * _scsih_add_device - creating sas device object
5127 * @ioc: per adapter object
5128 * @handle: sas device handle
5129 * @phy_num: phy number end device attached to
5130 * @is_pd: is this hidden raid component
5131 *
5132 * Creating end device object, stored in ioc->sas_device_list.
5133 *
5134 * Returns 0 for success, non-zero for failure.
5135 */
5136static int
5137_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
5138{
5139 Mpi2ConfigReply_t mpi_reply;
5140 Mpi2SasDevicePage0_t sas_device_pg0;
5141 Mpi2SasEnclosurePage0_t enclosure_pg0;
5142 struct _sas_device *sas_device;
5143 u32 ioc_status;
5144 __le64 sas_address;
5145 u32 device_info;
5146 unsigned long flags;
5147
5148 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5149 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5150 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5151 ioc->name, __FILE__, __LINE__, __func__);
5152 return -1;
5153 }
5154
5155 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5156 MPI2_IOCSTATUS_MASK;
5157 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5158 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5159 ioc->name, __FILE__, __LINE__, __func__);
5160 return -1;
5161 }
5162
Kashyap, Desaib4344272010-03-17 16:24:14 +05305163 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
5164
Eric Moore635374e2009-03-09 01:21:12 -06005165 /* check if device is present */
5166 if (!(le16_to_cpu(sas_device_pg0.Flags) &
5167 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
5168 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5169 ioc->name, __FILE__, __LINE__, __func__);
5170 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
5171 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
5172 return -1;
5173 }
5174
Kashyap, Desaib4344272010-03-17 16:24:14 +05305175 /* check if there were any issues with discovery */
5176 if (_scsih_check_access_status(ioc, sas_address, handle,
5177 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06005178 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06005179
5180 /* check if this is end device */
5181 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
5182 if (!(_scsih_is_end_device(device_info))) {
5183 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5184 ioc->name, __FILE__, __LINE__, __func__);
5185 return -1;
5186 }
5187
Eric Moore635374e2009-03-09 01:21:12 -06005188
5189 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5190 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5191 sas_address);
5192 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5193
Kashyap, Desaib4344272010-03-17 16:24:14 +05305194 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005195 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005196
5197 sas_device = kzalloc(sizeof(struct _sas_device),
5198 GFP_KERNEL);
5199 if (!sas_device) {
5200 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5201 ioc->name, __FILE__, __LINE__, __func__);
5202 return -1;
5203 }
5204
5205 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305206 if (_scsih_get_sas_address(ioc, le16_to_cpu
5207 (sas_device_pg0.ParentDevHandle),
5208 &sas_device->sas_address_parent) != 0)
5209 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5210 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005211 sas_device->enclosure_handle =
5212 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5213 sas_device->slot =
5214 le16_to_cpu(sas_device_pg0.Slot);
5215 sas_device->device_info = device_info;
5216 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305217 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005218
5219 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305220 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5221 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5222 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005223 sas_device->enclosure_logical_id =
5224 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005225
5226 /* get device name */
5227 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5228
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305229 if (ioc->wait_for_discovery_to_complete)
Eric Moore635374e2009-03-09 01:21:12 -06005230 _scsih_sas_device_init_add(ioc, sas_device);
5231 else
5232 _scsih_sas_device_add(ioc, sas_device);
5233
5234 return 0;
5235}
5236
5237/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305238 * _scsih_remove_device - removing sas device object
5239 * @ioc: per adapter object
5240 * @sas_device_delete: the sas_device object
5241 *
5242 * Return nothing.
5243 */
5244static void
5245_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5246 struct _sas_device *sas_device)
5247{
5248 struct _sas_device sas_device_backup;
5249 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305250
Kashyap, Desai1278b112010-03-09 17:34:13 +05305251 if (!sas_device)
5252 return;
Eric Moore635374e2009-03-09 01:21:12 -06005253
Kashyap, Desai1278b112010-03-09 17:34:13 +05305254 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06005255 _scsih_sas_device_remove(ioc, sas_device);
5256
Kashyap, Desai1278b112010-03-09 17:34:13 +05305257 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5258 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5259 sas_device_backup.handle, (unsigned long long)
5260 sas_device_backup.sas_address));
5261
5262 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5263 sas_target_priv_data = sas_device_backup.starget->hostdata;
5264 sas_target_priv_data->deleted = 1;
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305265 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5266 sas_target_priv_data->handle =
5267 MPT2SAS_INVALID_DEVICE_HANDLE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05305268 }
5269
Kashyap, Desai1278b112010-03-09 17:34:13 +05305270 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5271
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305272 if (!ioc->hide_drives)
5273 mpt2sas_transport_port_remove(ioc,
5274 sas_device_backup.sas_address,
5275 sas_device_backup.sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305276
5277 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5278 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5279 (unsigned long long) sas_device_backup.sas_address);
5280
5281 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5282 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5283 sas_device_backup.handle, (unsigned long long)
5284 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06005285}
5286
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305287/**
5288 * mpt2sas_device_remove - removing device object
5289 * @ioc: per adapter object
5290 * @sas_address: expander sas_address
5291 *
5292 * Return nothing.
5293 */
5294void
5295mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5296{
5297 struct _sas_device *sas_device;
5298 unsigned long flags;
5299
5300 if (ioc->shost_recovery)
5301 return;
5302
5303 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5304 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5305 sas_address);
5306 if (!sas_device) {
5307 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5308 return;
5309 }
5310 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5311 _scsih_remove_device(ioc, sas_device);
5312}
5313
Eric Moore635374e2009-03-09 01:21:12 -06005314#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5315/**
5316 * _scsih_sas_topology_change_event_debug - debug for topology event
5317 * @ioc: per adapter object
5318 * @event_data: event data payload
5319 * Context: user.
5320 */
5321static void
5322_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5323 Mpi2EventDataSasTopologyChangeList_t *event_data)
5324{
5325 int i;
5326 u16 handle;
5327 u16 reason_code;
5328 u8 phy_number;
5329 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305330 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005331
5332 switch (event_data->ExpStatus) {
5333 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5334 status_str = "add";
5335 break;
5336 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5337 status_str = "remove";
5338 break;
5339 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305340 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005341 status_str = "responding";
5342 break;
5343 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5344 status_str = "remove delay";
5345 break;
5346 default:
5347 status_str = "unknown status";
5348 break;
5349 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305350 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005351 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305352 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005353 "start_phy(%02d), count(%d)\n",
5354 le16_to_cpu(event_data->ExpanderDevHandle),
5355 le16_to_cpu(event_data->EnclosureHandle),
5356 event_data->StartPhyNum, event_data->NumEntries);
5357 for (i = 0; i < event_data->NumEntries; i++) {
5358 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5359 if (!handle)
5360 continue;
5361 phy_number = event_data->StartPhyNum + i;
5362 reason_code = event_data->PHY[i].PhyStatus &
5363 MPI2_EVENT_SAS_TOPO_RC_MASK;
5364 switch (reason_code) {
5365 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305366 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005367 break;
5368 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305369 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005370 break;
5371 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305372 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005373 break;
5374 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305375 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005376 break;
5377 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305378 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005379 break;
5380 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305381 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005382 break;
5383 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305384 link_rate = event_data->PHY[i].LinkRate >> 4;
5385 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305386 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305387 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5388 handle, status_str, link_rate, prev_link_rate);
5389
Eric Moore635374e2009-03-09 01:21:12 -06005390 }
5391}
5392#endif
5393
5394/**
5395 * _scsih_sas_topology_change_event - handle topology changes
5396 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305397 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005398 * Context: user.
5399 *
5400 */
5401static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305402_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005403 struct fw_event_work *fw_event)
5404{
5405 int i;
5406 u16 parent_handle, handle;
5407 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305408 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005409 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305410 struct _sas_device *sas_device;
5411 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005412 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305413 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305414 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005415
5416#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5417 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5418 _scsih_sas_topology_change_event_debug(ioc, event_data);
5419#endif
5420
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305421 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305422 return;
5423
Eric Moore635374e2009-03-09 01:21:12 -06005424 if (!ioc->sas_hba.num_phys)
5425 _scsih_sas_host_add(ioc);
5426 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305427 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005428
5429 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305430 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005431 "event\n", ioc->name));
5432 return;
5433 }
5434
5435 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5436
5437 /* handle expander add */
5438 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5439 if (_scsih_expander_add(ioc, parent_handle) != 0)
5440 return;
5441
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305442 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5443 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5444 parent_handle);
5445 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305446 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305447 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305448 max_phys = sas_expander->num_phys;
5449 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305450 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305451 max_phys = ioc->sas_hba.num_phys;
5452 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305453 return;
5454
Eric Moore635374e2009-03-09 01:21:12 -06005455 /* handle siblings events */
5456 for (i = 0; i < event_data->NumEntries; i++) {
5457 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305458 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005459 "expander event\n", ioc->name));
5460 return;
5461 }
Eric Moore3cb54692010-07-08 14:44:34 -06005462 if (ioc->shost_recovery || ioc->remove_host ||
5463 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305464 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305465 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305466 if (phy_number >= max_phys)
5467 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305468 reason_code = event_data->PHY[i].PhyStatus &
5469 MPI2_EVENT_SAS_TOPO_RC_MASK;
5470 if ((event_data->PHY[i].PhyStatus &
5471 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5472 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005473 continue;
5474 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5475 if (!handle)
5476 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305477 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305478 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005479 switch (reason_code) {
5480 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305481
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305482 if (ioc->shost_recovery)
5483 break;
5484
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305485 if (link_rate == prev_link_rate)
5486 break;
5487
5488 mpt2sas_transport_update_links(ioc, sas_address,
5489 handle, phy_number, link_rate);
5490
Kashyap, Desaib4344272010-03-17 16:24:14 +05305491 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5492 break;
5493
5494 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305495 break;
Eric Moore635374e2009-03-09 01:21:12 -06005496 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305497
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05305498 if (ioc->shost_recovery)
5499 break;
5500
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305501 mpt2sas_transport_update_links(ioc, sas_address,
5502 handle, phy_number, link_rate);
5503
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305504 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005505 break;
5506 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305507
5508 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5509 sas_device = _scsih_sas_device_find_by_handle(ioc,
5510 handle);
5511 if (!sas_device) {
5512 spin_unlock_irqrestore(&ioc->sas_device_lock,
5513 flags);
5514 break;
5515 }
5516 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5517 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005518 break;
5519 }
5520 }
5521
5522 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305523 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5524 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305525 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005526
5527}
5528
5529#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5530/**
5531 * _scsih_sas_device_status_change_event_debug - debug for device event
5532 * @event_data: event data payload
5533 * Context: user.
5534 *
5535 * Return nothing.
5536 */
5537static void
5538_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5539 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5540{
5541 char *reason_str = NULL;
5542
5543 switch (event_data->ReasonCode) {
5544 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5545 reason_str = "smart data";
5546 break;
5547 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5548 reason_str = "unsupported device discovered";
5549 break;
5550 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5551 reason_str = "internal device reset";
5552 break;
5553 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5554 reason_str = "internal task abort";
5555 break;
5556 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5557 reason_str = "internal task abort set";
5558 break;
5559 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5560 reason_str = "internal clear task set";
5561 break;
5562 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5563 reason_str = "internal query task";
5564 break;
5565 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5566 reason_str = "sata init failure";
5567 break;
5568 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5569 reason_str = "internal device reset complete";
5570 break;
5571 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5572 reason_str = "internal task abort complete";
5573 break;
5574 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5575 reason_str = "internal async notification";
5576 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305577 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5578 reason_str = "expander reduced functionality";
5579 break;
5580 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5581 reason_str = "expander reduced functionality complete";
5582 break;
Eric Moore635374e2009-03-09 01:21:12 -06005583 default:
5584 reason_str = "unknown reason";
5585 break;
5586 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305587 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305588 "\thandle(0x%04x), sas address(0x%016llx), tag(%d)",
5589 ioc->name, reason_str, le16_to_cpu(event_data->DevHandle),
5590 (unsigned long long)le64_to_cpu(event_data->SASAddress),
5591 le16_to_cpu(event_data->TaskTag));
Eric Moore635374e2009-03-09 01:21:12 -06005592 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305593 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005594 event_data->ASC, event_data->ASCQ);
5595 printk(KERN_INFO "\n");
5596}
5597#endif
5598
5599/**
5600 * _scsih_sas_device_status_change_event - handle device status change
5601 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305602 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005603 * Context: user.
5604 *
5605 * Return nothing.
5606 */
5607static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305608_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5609 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005610{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305611 struct MPT2SAS_TARGET *target_priv_data;
5612 struct _sas_device *sas_device;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05305613 u64 sas_address;
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305614 unsigned long flags;
5615 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5616 fw_event->event_data;
5617
Eric Moore635374e2009-03-09 01:21:12 -06005618#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5619 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305620 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305621 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005622#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305623
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305624 /* In MPI Revision K (0xC), the internal device reset complete was
5625 * implemented, so avoid setting tm_busy flag for older firmware.
5626 */
5627 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5628 return;
5629
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305630 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305631 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305632 event_data->ReasonCode !=
5633 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305634 return;
5635
5636 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5637 sas_address = le64_to_cpu(event_data->SASAddress);
5638 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5639 sas_address);
5640 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5641
5642 if (!sas_device || !sas_device->starget)
5643 return;
5644
5645 target_priv_data = sas_device->starget->hostdata;
5646 if (!target_priv_data)
5647 return;
5648
5649 if (event_data->ReasonCode ==
5650 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5651 target_priv_data->tm_busy = 1;
5652 else
5653 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005654}
5655
5656#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5657/**
5658 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5659 * @ioc: per adapter object
5660 * @event_data: event data payload
5661 * Context: user.
5662 *
5663 * Return nothing.
5664 */
5665static void
5666_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5667 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5668{
5669 char *reason_str = NULL;
5670
5671 switch (event_data->ReasonCode) {
5672 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5673 reason_str = "enclosure add";
5674 break;
5675 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5676 reason_str = "enclosure remove";
5677 break;
5678 default:
5679 reason_str = "unknown reason";
5680 break;
5681 }
5682
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305683 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005684 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5685 " number slots(%d)\n", ioc->name, reason_str,
5686 le16_to_cpu(event_data->EnclosureHandle),
5687 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5688 le16_to_cpu(event_data->StartSlot));
5689}
5690#endif
5691
5692/**
5693 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5694 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305695 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005696 * Context: user.
5697 *
5698 * Return nothing.
5699 */
5700static void
5701_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305702 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005703{
5704#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5705 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5706 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305707 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005708#endif
5709}
5710
5711/**
5712 * _scsih_sas_broadcast_primative_event - handle broadcast events
5713 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305714 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005715 * Context: user.
5716 *
5717 * Return nothing.
5718 */
5719static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305720_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5721 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005722{
5723 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305724 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005725 u16 smid, handle;
5726 u32 lun;
5727 struct MPT2SAS_DEVICE *sas_device_priv_data;
5728 u32 termination_count;
5729 u32 query_count;
5730 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305731 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305732 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305733 unsigned long flags;
5734 int r;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305735 u8 max_retries = 0;
5736 u8 task_abort_retries;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305737
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305738 mutex_lock(&ioc->tm_cmds.mutex);
5739 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: phy number(%d), "
5740 "width(%d)\n", ioc->name, __func__, event_data->PhyNum,
5741 event_data->PortWidth));
5742
5743 _scsih_block_io_all_device(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005744
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305745 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305746 mpi_reply = ioc->tm_cmds.reply;
5747broadcast_aen_retry:
5748
5749 /* sanity checks for retrying this loop */
5750 if (max_retries++ == 5) {
5751 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: giving up\n",
5752 ioc->name, __func__));
5753 goto out;
5754 } else if (max_retries > 1)
5755 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: %d retry\n",
5756 ioc->name, __func__, max_retries - 1));
5757
Eric Moore635374e2009-03-09 01:21:12 -06005758 termination_count = 0;
5759 query_count = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305760 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305761 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305762 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06005763 scmd = _scsih_scsi_lookup_get(ioc, smid);
5764 if (!scmd)
5765 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305766 sdev = scmd->device;
5767 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005768 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5769 continue;
5770 /* skip hidden raid components */
5771 if (sas_device_priv_data->sas_target->flags &
5772 MPT_TARGET_FLAGS_RAID_COMPONENT)
5773 continue;
5774 /* skip volumes */
5775 if (sas_device_priv_data->sas_target->flags &
5776 MPT_TARGET_FLAGS_VOLUME)
5777 continue;
5778
5779 handle = sas_device_priv_data->sas_target->handle;
5780 lun = sas_device_priv_data->lun;
5781 query_count++;
5782
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305783 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305784 goto out;
5785
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305786 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305787 r = mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5788 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, 0,
5789 TM_MUTEX_OFF);
5790 if (r == FAILED) {
5791 sdev_printk(KERN_WARNING, sdev,
5792 "mpt2sas_scsih_issue_tm: FAILED when sending "
5793 "QUERY_TASK: scmd(%p)\n", scmd);
5794 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5795 goto broadcast_aen_retry;
5796 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05305797 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5798 & MPI2_IOCSTATUS_MASK;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305799 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5800 sdev_printk(KERN_WARNING, sdev, "query task: FAILED "
5801 "with IOCSTATUS(0x%04x), scmd(%p)\n", ioc_status,
5802 scmd);
5803 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5804 goto broadcast_aen_retry;
5805 }
5806
5807 /* see if IO is still owned by IOC and target */
5808 if (mpi_reply->ResponseCode ==
Eric Moore635374e2009-03-09 01:21:12 -06005809 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5810 mpi_reply->ResponseCode ==
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305811 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305812 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005813 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305814 }
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305815 task_abort_retries = 0;
5816 tm_retry:
5817 if (task_abort_retries++ == 60) {
5818 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
5819 "%s: ABORT_TASK: giving up\n", ioc->name,
5820 __func__));
5821 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5822 goto broadcast_aen_retry;
5823 }
5824
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305825 if (ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305826 goto out_no_lock;
5827
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305828 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5829 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305830 scmd->serial_number, TM_MUTEX_OFF);
5831 if (r == FAILED) {
5832 sdev_printk(KERN_WARNING, sdev,
5833 "mpt2sas_scsih_issue_tm: ABORT_TASK: FAILED : "
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305834 "scmd(%p)\n", scmd);
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305835 goto tm_retry;
5836 }
5837
5838 if (task_abort_retries > 1)
5839 sdev_printk(KERN_WARNING, sdev,
5840 "mpt2sas_scsih_issue_tm: ABORT_TASK: RETRIES (%d):"
5841 " scmd(%p)\n",
5842 task_abort_retries - 1, scmd);
5843
Eric Moore635374e2009-03-09 01:21:12 -06005844 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305845 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005846 }
Eric Moore635374e2009-03-09 01:21:12 -06005847
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305848 if (ioc->broadcast_aen_pending) {
5849 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: loop back due to"
5850 " pending AEN\n", ioc->name, __func__));
5851 ioc->broadcast_aen_pending = 0;
5852 goto broadcast_aen_retry;
5853 }
5854
5855 out:
5856 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
5857 out_no_lock:
5858
5859 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005860 "%s - exit, query_count = %d termination_count = %d\n",
5861 ioc->name, __func__, query_count, termination_count));
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305862
5863 ioc->broadcast_aen_busy = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305864 if (!ioc->shost_recovery)
Kashyap, Desaif93213d2011-06-14 10:56:43 +05305865 _scsih_ublock_io_all_device(ioc);
5866 mutex_unlock(&ioc->tm_cmds.mutex);
Eric Moore635374e2009-03-09 01:21:12 -06005867}
5868
5869/**
5870 * _scsih_sas_discovery_event - handle discovery events
5871 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305872 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005873 * Context: user.
5874 *
5875 * Return nothing.
5876 */
5877static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305878_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5879 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005880{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305881 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5882
Eric Moore635374e2009-03-09 01:21:12 -06005883#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5884 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305885 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005886 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5887 "start" : "stop");
5888 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305889 printk("discovery_status(0x%08x)",
5890 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005891 printk("\n");
5892 }
5893#endif
5894
5895 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5896 !ioc->sas_hba.num_phys)
5897 _scsih_sas_host_add(ioc);
5898}
5899
5900/**
5901 * _scsih_reprobe_lun - reprobing lun
5902 * @sdev: scsi device struct
5903 * @no_uld_attach: sdev->no_uld_attach flag setting
5904 *
5905 **/
5906static void
5907_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5908{
5909 int rc;
5910
5911 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5912 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5913 sdev->no_uld_attach ? "hidding" : "exposing");
5914 rc = scsi_device_reprobe(sdev);
5915}
5916
5917/**
5918 * _scsih_reprobe_target - reprobing target
5919 * @starget: scsi target struct
5920 * @no_uld_attach: sdev->no_uld_attach flag setting
5921 *
5922 * Note: no_uld_attach flag determines whether the disk device is attached
5923 * to block layer. A value of `1` means to not attach.
5924 **/
5925static void
5926_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5927{
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305928 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06005929
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05305930 if (starget == NULL)
5931 return;
5932 sas_target_priv_data = starget->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005933 if (no_uld_attach)
5934 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5935 else
5936 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5937
5938 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5939 _scsih_reprobe_lun);
5940}
5941/**
5942 * _scsih_sas_volume_add - add new volume
5943 * @ioc: per adapter object
5944 * @element: IR config element data
5945 * Context: user.
5946 *
5947 * Return nothing.
5948 */
5949static void
5950_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5951 Mpi2EventIrConfigElement_t *element)
5952{
5953 struct _raid_device *raid_device;
5954 unsigned long flags;
5955 u64 wwid;
5956 u16 handle = le16_to_cpu(element->VolDevHandle);
5957 int rc;
5958
Eric Moore635374e2009-03-09 01:21:12 -06005959 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5960 if (!wwid) {
5961 printk(MPT2SAS_ERR_FMT
5962 "failure at %s:%d/%s()!\n", ioc->name,
5963 __FILE__, __LINE__, __func__);
5964 return;
5965 }
5966
5967 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5968 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5969 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5970
5971 if (raid_device)
5972 return;
5973
5974 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5975 if (!raid_device) {
5976 printk(MPT2SAS_ERR_FMT
5977 "failure at %s:%d/%s()!\n", ioc->name,
5978 __FILE__, __LINE__, __func__);
5979 return;
5980 }
5981
5982 raid_device->id = ioc->sas_id++;
5983 raid_device->channel = RAID_CHANNEL;
5984 raid_device->handle = handle;
5985 raid_device->wwid = wwid;
5986 _scsih_raid_device_add(ioc, raid_device);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05305987 if (!ioc->wait_for_discovery_to_complete) {
Eric Moore635374e2009-03-09 01:21:12 -06005988 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5989 raid_device->id, 0);
5990 if (rc)
5991 _scsih_raid_device_remove(ioc, raid_device);
5992 } else
5993 _scsih_determine_boot_device(ioc, raid_device, 1);
5994}
5995
5996/**
5997 * _scsih_sas_volume_delete - delete volume
5998 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305999 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06006000 * Context: user.
6001 *
6002 * Return nothing.
6003 */
6004static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306005_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06006006{
6007 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06006008 unsigned long flags;
6009 struct MPT2SAS_TARGET *sas_target_priv_data;
6010
Eric Moore635374e2009-03-09 01:21:12 -06006011 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6012 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6013 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6014 if (!raid_device)
6015 return;
6016 if (raid_device->starget) {
6017 sas_target_priv_data = raid_device->starget->hostdata;
6018 sas_target_priv_data->deleted = 1;
6019 scsi_remove_target(&raid_device->starget->dev);
6020 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306021 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
6022 "(0x%016llx)\n", ioc->name, raid_device->handle,
6023 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06006024 _scsih_raid_device_remove(ioc, raid_device);
6025}
6026
6027/**
6028 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
6029 * @ioc: per adapter object
6030 * @element: IR config element data
6031 * Context: user.
6032 *
6033 * Return nothing.
6034 */
6035static void
6036_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
6037 Mpi2EventIrConfigElement_t *element)
6038{
6039 struct _sas_device *sas_device;
6040 unsigned long flags;
6041 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6042
6043 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6044 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6045 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6046 if (!sas_device)
6047 return;
6048
6049 /* exposing raid component */
6050 sas_device->volume_handle = 0;
6051 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306052 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006053 _scsih_reprobe_target(sas_device->starget, 0);
6054}
6055
6056/**
6057 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
6058 * @ioc: per adapter object
6059 * @element: IR config element data
6060 * Context: user.
6061 *
6062 * Return nothing.
6063 */
6064static void
6065_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
6066 Mpi2EventIrConfigElement_t *element)
6067{
6068 struct _sas_device *sas_device;
6069 unsigned long flags;
6070 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6071
6072 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6073 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6074 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6075 if (!sas_device)
6076 return;
6077
6078 /* hiding raid component */
6079 mpt2sas_config_get_volume_handle(ioc, handle,
6080 &sas_device->volume_handle);
6081 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
6082 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306083 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06006084 _scsih_reprobe_target(sas_device->starget, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306085
Eric Moore635374e2009-03-09 01:21:12 -06006086}
6087
6088/**
6089 * _scsih_sas_pd_delete - delete pd component
6090 * @ioc: per adapter object
6091 * @element: IR config element data
6092 * Context: user.
6093 *
6094 * Return nothing.
6095 */
6096static void
6097_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
6098 Mpi2EventIrConfigElement_t *element)
6099{
6100 struct _sas_device *sas_device;
6101 unsigned long flags;
6102 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
6103
6104 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6105 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6106 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6107 if (!sas_device)
6108 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306109 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006110}
6111
6112/**
6113 * _scsih_sas_pd_add - remove pd component
6114 * @ioc: per adapter object
6115 * @element: IR config element data
6116 * Context: user.
6117 *
6118 * Return nothing.
6119 */
6120static void
6121_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
6122 Mpi2EventIrConfigElement_t *element)
6123{
6124 struct _sas_device *sas_device;
6125 unsigned long flags;
6126 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306127 Mpi2ConfigReply_t mpi_reply;
6128 Mpi2SasDevicePage0_t sas_device_pg0;
6129 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306130 u64 sas_address;
6131 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006132
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306133 set_bit(handle, ioc->pd_handles);
6134
Eric Moore635374e2009-03-09 01:21:12 -06006135 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6136 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6137 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306138 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306139 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306140
6141 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
6142 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
6143 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6144 ioc->name, __FILE__, __LINE__, __func__);
6145 return;
6146 }
6147
6148 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6149 MPI2_IOCSTATUS_MASK;
6150 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6151 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6152 ioc->name, __FILE__, __LINE__, __func__);
6153 return;
6154 }
6155
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306156 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6157 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6158 mpt2sas_transport_update_links(ioc, sas_address, handle,
6159 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306160
6161 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06006162}
6163
6164#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6165/**
6166 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
6167 * @ioc: per adapter object
6168 * @event_data: event data payload
6169 * Context: user.
6170 *
6171 * Return nothing.
6172 */
6173static void
6174_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
6175 Mpi2EventDataIrConfigChangeList_t *event_data)
6176{
6177 Mpi2EventIrConfigElement_t *element;
6178 u8 element_type;
6179 int i;
6180 char *reason_str = NULL, *element_str = NULL;
6181
6182 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6183
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306184 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06006185 ioc->name, (le32_to_cpu(event_data->Flags) &
6186 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
6187 "foreign" : "native", event_data->NumElements);
6188 for (i = 0; i < event_data->NumElements; i++, element++) {
6189 switch (element->ReasonCode) {
6190 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
6191 reason_str = "add";
6192 break;
6193 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
6194 reason_str = "remove";
6195 break;
6196 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
6197 reason_str = "no change";
6198 break;
6199 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
6200 reason_str = "hide";
6201 break;
6202 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
6203 reason_str = "unhide";
6204 break;
6205 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6206 reason_str = "volume_created";
6207 break;
6208 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6209 reason_str = "volume_deleted";
6210 break;
6211 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
6212 reason_str = "pd_created";
6213 break;
6214 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
6215 reason_str = "pd_deleted";
6216 break;
6217 default:
6218 reason_str = "unknown reason";
6219 break;
6220 }
6221 element_type = le16_to_cpu(element->ElementFlags) &
6222 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
6223 switch (element_type) {
6224 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
6225 element_str = "volume";
6226 break;
6227 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
6228 element_str = "phys disk";
6229 break;
6230 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
6231 element_str = "hot spare";
6232 break;
6233 default:
6234 element_str = "unknown element";
6235 break;
6236 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306237 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06006238 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
6239 reason_str, le16_to_cpu(element->VolDevHandle),
6240 le16_to_cpu(element->PhysDiskDevHandle),
6241 element->PhysDiskNum);
6242 }
6243}
6244#endif
6245
6246/**
6247 * _scsih_sas_ir_config_change_event - handle ir configuration change events
6248 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306249 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006250 * Context: user.
6251 *
6252 * Return nothing.
6253 */
6254static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306255_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
6256 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006257{
6258 Mpi2EventIrConfigElement_t *element;
6259 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306260 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306261 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006262
6263#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306264 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6265 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06006266 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
6267
6268#endif
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306269
6270 if (ioc->shost_recovery)
6271 return;
6272
Kashyap, Desai62727a72009-08-07 19:35:18 +05306273 foreign_config = (le32_to_cpu(event_data->Flags) &
6274 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06006275
6276 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6277 for (i = 0; i < event_data->NumElements; i++, element++) {
6278
6279 switch (element->ReasonCode) {
6280 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6281 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306282 if (!foreign_config)
6283 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006284 break;
6285 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6286 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306287 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306288 _scsih_sas_volume_delete(ioc,
6289 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006290 break;
6291 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306292 if (!ioc->is_warpdrive)
6293 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006294 break;
6295 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306296 if (!ioc->is_warpdrive)
6297 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006298 break;
6299 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306300 if (!ioc->is_warpdrive)
6301 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006302 break;
6303 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306304 if (!ioc->is_warpdrive)
6305 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006306 break;
6307 }
6308 }
6309}
6310
6311/**
6312 * _scsih_sas_ir_volume_event - IR volume event
6313 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306314 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006315 * Context: user.
6316 *
6317 * Return nothing.
6318 */
6319static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306320_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6321 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006322{
6323 u64 wwid;
6324 unsigned long flags;
6325 struct _raid_device *raid_device;
6326 u16 handle;
6327 u32 state;
6328 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306329 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006330
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306331 if (ioc->shost_recovery)
6332 return;
6333
Eric Moore635374e2009-03-09 01:21:12 -06006334 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6335 return;
6336
6337 handle = le16_to_cpu(event_data->VolDevHandle);
6338 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306339 if (!ioc->hide_ir_msg)
6340 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6341 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6342 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006343
Eric Moore635374e2009-03-09 01:21:12 -06006344 switch (state) {
6345 case MPI2_RAID_VOL_STATE_MISSING:
6346 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306347 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006348 break;
6349
6350 case MPI2_RAID_VOL_STATE_ONLINE:
6351 case MPI2_RAID_VOL_STATE_DEGRADED:
6352 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306353
6354 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6355 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6356 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6357
Eric Moore635374e2009-03-09 01:21:12 -06006358 if (raid_device)
6359 break;
6360
6361 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6362 if (!wwid) {
6363 printk(MPT2SAS_ERR_FMT
6364 "failure at %s:%d/%s()!\n", ioc->name,
6365 __FILE__, __LINE__, __func__);
6366 break;
6367 }
6368
6369 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6370 if (!raid_device) {
6371 printk(MPT2SAS_ERR_FMT
6372 "failure at %s:%d/%s()!\n", ioc->name,
6373 __FILE__, __LINE__, __func__);
6374 break;
6375 }
6376
6377 raid_device->id = ioc->sas_id++;
6378 raid_device->channel = RAID_CHANNEL;
6379 raid_device->handle = handle;
6380 raid_device->wwid = wwid;
6381 _scsih_raid_device_add(ioc, raid_device);
6382 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6383 raid_device->id, 0);
6384 if (rc)
6385 _scsih_raid_device_remove(ioc, raid_device);
6386 break;
6387
6388 case MPI2_RAID_VOL_STATE_INITIALIZING:
6389 default:
6390 break;
6391 }
6392}
6393
6394/**
6395 * _scsih_sas_ir_physical_disk_event - PD event
6396 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306397 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006398 * Context: user.
6399 *
6400 * Return nothing.
6401 */
6402static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306403_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6404 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006405{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306406 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006407 u32 state;
6408 struct _sas_device *sas_device;
6409 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306410 Mpi2ConfigReply_t mpi_reply;
6411 Mpi2SasDevicePage0_t sas_device_pg0;
6412 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306413 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306414 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006415
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306416 if (ioc->shost_recovery)
6417 return;
6418
Eric Moore635374e2009-03-09 01:21:12 -06006419 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6420 return;
6421
6422 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6423 state = le32_to_cpu(event_data->NewValue);
6424
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306425 if (!ioc->hide_ir_msg)
6426 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6427 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6428 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006429
Eric Moore635374e2009-03-09 01:21:12 -06006430 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006431 case MPI2_RAID_PD_STATE_ONLINE:
6432 case MPI2_RAID_PD_STATE_DEGRADED:
6433 case MPI2_RAID_PD_STATE_REBUILDING:
6434 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306435 case MPI2_RAID_PD_STATE_HOT_SPARE:
6436
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306437 if (!ioc->is_warpdrive)
6438 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306439
6440 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6441 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6442 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6443
6444 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306445 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306446
6447 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6448 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6449 handle))) {
6450 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6451 ioc->name, __FILE__, __LINE__, __func__);
6452 return;
6453 }
6454
6455 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6456 MPI2_IOCSTATUS_MASK;
6457 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6458 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6459 ioc->name, __FILE__, __LINE__, __func__);
6460 return;
6461 }
6462
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306463 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6464 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6465 mpt2sas_transport_update_links(ioc, sas_address, handle,
6466 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306467
6468 _scsih_add_device(ioc, handle, 0, 1);
6469
Eric Moore635374e2009-03-09 01:21:12 -06006470 break;
6471
Kashyap, Desai62727a72009-08-07 19:35:18 +05306472 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006473 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6474 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006475 default:
6476 break;
6477 }
6478}
6479
6480#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6481/**
6482 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6483 * @ioc: per adapter object
6484 * @event_data: event data payload
6485 * Context: user.
6486 *
6487 * Return nothing.
6488 */
6489static void
6490_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6491 Mpi2EventDataIrOperationStatus_t *event_data)
6492{
6493 char *reason_str = NULL;
6494
6495 switch (event_data->RAIDOperation) {
6496 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6497 reason_str = "resync";
6498 break;
6499 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6500 reason_str = "online capacity expansion";
6501 break;
6502 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6503 reason_str = "consistency check";
6504 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306505 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6506 reason_str = "background init";
6507 break;
6508 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6509 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006510 break;
6511 }
6512
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306513 if (!reason_str)
6514 return;
6515
Eric Moore635374e2009-03-09 01:21:12 -06006516 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6517 "\thandle(0x%04x), percent complete(%d)\n",
6518 ioc->name, reason_str,
6519 le16_to_cpu(event_data->VolDevHandle),
6520 event_data->PercentComplete);
6521}
6522#endif
6523
6524/**
6525 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6526 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306527 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006528 * Context: user.
6529 *
6530 * Return nothing.
6531 */
6532static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306533_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6534 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006535{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306536 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6537 static struct _raid_device *raid_device;
6538 unsigned long flags;
6539 u16 handle;
6540
Eric Moore635374e2009-03-09 01:21:12 -06006541#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306542 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6543 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306544 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306545 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006546#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306547
6548 /* code added for raid transport support */
6549 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6550
6551 handle = le16_to_cpu(event_data->VolDevHandle);
6552
6553 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6554 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6555 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6556
6557 if (!raid_device)
6558 return;
6559
6560 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6561 raid_device->percent_complete =
6562 event_data->PercentComplete;
6563 }
Eric Moore635374e2009-03-09 01:21:12 -06006564}
6565
6566/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306567 * _scsih_prep_device_scan - initialize parameters prior to device scan
6568 * @ioc: per adapter object
6569 *
6570 * Set the deleted flag prior to device scan. If the device is found during
6571 * the scan, then we clear the deleted flag.
6572 */
6573static void
6574_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6575{
6576 struct MPT2SAS_DEVICE *sas_device_priv_data;
6577 struct scsi_device *sdev;
6578
6579 shost_for_each_device(sdev, ioc->shost) {
6580 sas_device_priv_data = sdev->hostdata;
6581 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6582 sas_device_priv_data->sas_target->deleted = 1;
6583 }
6584}
6585
6586/**
Eric Moore635374e2009-03-09 01:21:12 -06006587 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6588 * @ioc: per adapter object
6589 * @sas_address: sas address
6590 * @slot: enclosure slot id
6591 * @handle: device handle
6592 *
6593 * After host reset, find out whether devices are still responding.
6594 * Used in _scsi_remove_unresponsive_sas_devices.
6595 *
6596 * Return nothing.
6597 */
6598static void
6599_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6600 u16 slot, u16 handle)
6601{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306602 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006603 struct scsi_target *starget;
6604 struct _sas_device *sas_device;
6605 unsigned long flags;
6606
6607 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6608 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6609 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306610 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006611 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306612 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306613 if (starget && starget->hostdata) {
6614 sas_target_priv_data = starget->hostdata;
6615 sas_target_priv_data->tm_busy = 0;
6616 sas_target_priv_data->deleted = 0;
6617 } else
6618 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306619 if (starget)
6620 starget_printk(KERN_INFO, starget,
6621 "handle(0x%04x), sas_addr(0x%016llx), "
6622 "enclosure logical id(0x%016llx), "
6623 "slot(%d)\n", handle,
6624 (unsigned long long)sas_device->sas_address,
6625 (unsigned long long)
6626 sas_device->enclosure_logical_id,
6627 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006628 if (sas_device->handle == handle)
6629 goto out;
6630 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6631 sas_device->handle);
6632 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306633 if (sas_target_priv_data)
6634 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006635 goto out;
6636 }
6637 }
6638 out:
6639 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6640}
6641
6642/**
6643 * _scsih_search_responding_sas_devices -
6644 * @ioc: per adapter object
6645 *
6646 * After host reset, find out whether devices are still responding.
6647 * If not remove.
6648 *
6649 * Return nothing.
6650 */
6651static void
6652_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6653{
6654 Mpi2SasDevicePage0_t sas_device_pg0;
6655 Mpi2ConfigReply_t mpi_reply;
6656 u16 ioc_status;
6657 __le64 sas_address;
6658 u16 handle;
6659 u32 device_info;
6660 u16 slot;
6661
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306662 printk(MPT2SAS_INFO_FMT "search for end-devices: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006663
6664 if (list_empty(&ioc->sas_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306665 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006666
6667 handle = 0xFFFF;
6668 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6669 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6670 handle))) {
6671 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6672 MPI2_IOCSTATUS_MASK;
6673 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6674 break;
6675 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6676 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6677 if (!(_scsih_is_end_device(device_info)))
6678 continue;
6679 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6680 slot = le16_to_cpu(sas_device_pg0.Slot);
6681 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6682 handle);
6683 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306684out:
6685 printk(MPT2SAS_INFO_FMT "search for end-devices: complete\n",
6686 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006687}
6688
6689/**
6690 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6691 * @ioc: per adapter object
6692 * @wwid: world wide identifier for raid volume
6693 * @handle: device handle
6694 *
6695 * After host reset, find out whether devices are still responding.
6696 * Used in _scsi_remove_unresponsive_raid_devices.
6697 *
6698 * Return nothing.
6699 */
6700static void
6701_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6702 u16 handle)
6703{
6704 struct MPT2SAS_TARGET *sas_target_priv_data;
6705 struct scsi_target *starget;
6706 struct _raid_device *raid_device;
6707 unsigned long flags;
6708
6709 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6710 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6711 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306712 starget = raid_device->starget;
6713 if (starget && starget->hostdata) {
6714 sas_target_priv_data = starget->hostdata;
6715 sas_target_priv_data->deleted = 0;
6716 } else
6717 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006718 raid_device->responding = 1;
6719 starget_printk(KERN_INFO, raid_device->starget,
6720 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6721 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306722 /*
6723 * WARPDRIVE: The handles of the PDs might have changed
6724 * across the host reset so re-initialize the
6725 * required data for Direct IO
6726 */
6727 _scsih_init_warpdrive_properties(ioc, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006728 if (raid_device->handle == handle)
6729 goto out;
6730 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6731 raid_device->handle);
6732 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306733 if (sas_target_priv_data)
6734 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006735 goto out;
6736 }
6737 }
6738 out:
6739 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6740}
6741
6742/**
6743 * _scsih_search_responding_raid_devices -
6744 * @ioc: per adapter object
6745 *
6746 * After host reset, find out whether devices are still responding.
6747 * If not remove.
6748 *
6749 * Return nothing.
6750 */
6751static void
6752_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6753{
6754 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306755 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306756 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006757 Mpi2ConfigReply_t mpi_reply;
6758 u16 ioc_status;
6759 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306760 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006761
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306762 if (!ioc->ir_firmware)
6763 return;
6764
6765 printk(MPT2SAS_INFO_FMT "search for raid volumes: start\n",
6766 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006767
6768 if (list_empty(&ioc->raid_device_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306769 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006770
6771 handle = 0xFFFF;
6772 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6773 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6774 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6775 MPI2_IOCSTATUS_MASK;
6776 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6777 break;
6778 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306779
6780 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6781 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6782 sizeof(Mpi2RaidVolPage0_t)))
6783 continue;
6784
6785 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6786 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6787 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6788 _scsih_mark_responding_raid_device(ioc,
6789 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006790 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306791
6792 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306793 if (!ioc->is_warpdrive) {
6794 phys_disk_num = 0xFF;
6795 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6796 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6797 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6798 phys_disk_num))) {
6799 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6800 MPI2_IOCSTATUS_MASK;
6801 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6802 break;
6803 phys_disk_num = pd_pg0.PhysDiskNum;
6804 handle = le16_to_cpu(pd_pg0.DevHandle);
6805 set_bit(handle, ioc->pd_handles);
6806 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306807 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306808out:
6809 printk(MPT2SAS_INFO_FMT "search for responding raid volumes: "
6810 "complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006811}
6812
6813/**
6814 * _scsih_mark_responding_expander - mark a expander as responding
6815 * @ioc: per adapter object
6816 * @sas_address: sas address
6817 * @handle:
6818 *
6819 * After host reset, find out whether devices are still responding.
6820 * Used in _scsi_remove_unresponsive_expanders.
6821 *
6822 * Return nothing.
6823 */
6824static void
6825_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6826 u16 handle)
6827{
6828 struct _sas_node *sas_expander;
6829 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306830 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006831
6832 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6833 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306834 if (sas_expander->sas_address != sas_address)
6835 continue;
6836 sas_expander->responding = 1;
6837 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006838 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306839 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6840 " from(0x%04x) to (0x%04x)!!!\n",
6841 (unsigned long long)sas_expander->sas_address,
6842 sas_expander->handle, handle);
6843 sas_expander->handle = handle;
6844 for (i = 0 ; i < sas_expander->num_phys ; i++)
6845 sas_expander->phy[i].handle = handle;
6846 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006847 }
6848 out:
6849 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6850}
6851
6852/**
6853 * _scsih_search_responding_expanders -
6854 * @ioc: per adapter object
6855 *
6856 * After host reset, find out whether devices are still responding.
6857 * If not remove.
6858 *
6859 * Return nothing.
6860 */
6861static void
6862_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6863{
6864 Mpi2ExpanderPage0_t expander_pg0;
6865 Mpi2ConfigReply_t mpi_reply;
6866 u16 ioc_status;
Kashyap, Desaic97951e2011-06-14 10:54:56 +05306867 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006868 u16 handle;
6869
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306870 printk(MPT2SAS_INFO_FMT "search for expanders: start\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006871
6872 if (list_empty(&ioc->sas_expander_list))
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306873 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006874
6875 handle = 0xFFFF;
6876 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6877 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6878
6879 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6880 MPI2_IOCSTATUS_MASK;
6881 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6882 break;
6883
6884 handle = le16_to_cpu(expander_pg0.DevHandle);
6885 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6886 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6887 "sas_addr(0x%016llx)\n", handle,
6888 (unsigned long long)sas_address);
6889 _scsih_mark_responding_expander(ioc, sas_address, handle);
6890 }
6891
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306892 out:
6893 printk(MPT2SAS_INFO_FMT "search for expanders: complete\n", ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006894}
6895
6896/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306897 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006898 * @ioc: per adapter object
6899 *
6900 * Return nothing.
6901 */
6902static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306903_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006904{
6905 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306906 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006907 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006908
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306909 printk(MPT2SAS_INFO_FMT "removing unresponding devices: start\n",
6910 ioc->name);
Eric Moore635374e2009-03-09 01:21:12 -06006911
6912 list_for_each_entry_safe(sas_device, sas_device_next,
6913 &ioc->sas_device_list, list) {
6914 if (sas_device->responding) {
6915 sas_device->responding = 0;
6916 continue;
6917 }
6918 if (sas_device->starget)
6919 starget_printk(KERN_INFO, sas_device->starget,
6920 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6921 "enclosure logical id(0x%016llx), slot(%d)\n",
6922 sas_device->handle,
6923 (unsigned long long)sas_device->sas_address,
6924 (unsigned long long)
6925 sas_device->enclosure_logical_id,
6926 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306927 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006928 }
6929
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306930 if (!ioc->ir_firmware)
6931 goto retry_expander_search;
6932
Eric Moore635374e2009-03-09 01:21:12 -06006933 list_for_each_entry_safe(raid_device, raid_device_next,
6934 &ioc->raid_device_list, list) {
6935 if (raid_device->responding) {
6936 raid_device->responding = 0;
6937 continue;
6938 }
6939 if (raid_device->starget) {
6940 starget_printk(KERN_INFO, raid_device->starget,
6941 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6942 raid_device->handle,
6943 (unsigned long long)raid_device->wwid);
6944 scsi_remove_target(&raid_device->starget->dev);
6945 }
6946 _scsih_raid_device_remove(ioc, raid_device);
6947 }
6948
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306949 retry_expander_search:
6950 sas_expander = NULL;
6951 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006952 if (sas_expander->responding) {
6953 sas_expander->responding = 0;
6954 continue;
6955 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306956 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306957 goto retry_expander_search;
6958 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306959 printk(MPT2SAS_INFO_FMT "removing unresponding devices: complete\n",
6960 ioc->name);
6961 /* unblock devices */
6962 _scsih_ublock_io_all_device(ioc);
6963}
6964
6965static void
6966_scsih_refresh_expander_links(struct MPT2SAS_ADAPTER *ioc,
6967 struct _sas_node *sas_expander, u16 handle)
6968{
6969 Mpi2ExpanderPage1_t expander_pg1;
6970 Mpi2ConfigReply_t mpi_reply;
6971 int i;
6972
6973 for (i = 0 ; i < sas_expander->num_phys ; i++) {
6974 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
6975 &expander_pg1, i, handle))) {
6976 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6977 ioc->name, __FILE__, __LINE__, __func__);
6978 return;
6979 }
6980
6981 mpt2sas_transport_update_links(ioc, sas_expander->sas_address,
6982 le16_to_cpu(expander_pg1.AttachedDevHandle), i,
6983 expander_pg1.NegotiatedLinkRate >> 4);
6984 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306985}
6986
6987/**
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306988 * _scsih_scan_for_devices_after_reset - scan for devices after host reset
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306989 * @ioc: per adapter object
6990 *
6991 * Return nothing.
6992 */
6993static void
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306994_scsih_scan_for_devices_after_reset(struct MPT2SAS_ADAPTER *ioc)
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306995{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05306996 Mpi2ExpanderPage0_t expander_pg0;
6997 Mpi2SasDevicePage0_t sas_device_pg0;
6998 Mpi2RaidVolPage1_t volume_pg1;
6999 Mpi2RaidVolPage0_t volume_pg0;
7000 Mpi2RaidPhysDiskPage0_t pd_pg0;
7001 Mpi2EventIrConfigElement_t element;
7002 Mpi2ConfigReply_t mpi_reply;
7003 u8 phys_disk_num;
7004 u16 ioc_status;
7005 u16 handle, parent_handle;
7006 u64 sas_address;
7007 struct _sas_device *sas_device;
7008 struct _sas_node *expander_device;
7009 static struct _raid_device *raid_device;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307010
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307011 printk(MPT2SAS_INFO_FMT "scan devices: start\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307012
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307013 _scsih_sas_host_refresh(ioc);
7014
7015 /* expanders */
7016 handle = 0xFFFF;
7017 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
7018 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
7019 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7020 MPI2_IOCSTATUS_MASK;
7021 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7022 break;
7023 handle = le16_to_cpu(expander_pg0.DevHandle);
7024 expander_device = mpt2sas_scsih_expander_find_by_sas_address(
7025 ioc, le64_to_cpu(expander_pg0.SASAddress));
7026 if (expander_device)
7027 _scsih_refresh_expander_links(ioc, expander_device,
7028 handle);
7029 else
7030 _scsih_expander_add(ioc, handle);
7031 }
7032
7033 if (!ioc->ir_firmware)
7034 goto skip_to_sas;
7035
7036 /* phys disk */
7037 phys_disk_num = 0xFF;
7038 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
7039 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
7040 phys_disk_num))) {
7041 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7042 MPI2_IOCSTATUS_MASK;
7043 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7044 break;
7045 phys_disk_num = pd_pg0.PhysDiskNum;
7046 handle = le16_to_cpu(pd_pg0.DevHandle);
7047 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
7048 if (sas_device)
7049 continue;
7050 if (mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7051 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
7052 handle) != 0)
7053 continue;
7054 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7055 if (!_scsih_get_sas_address(ioc, parent_handle,
7056 &sas_address)) {
7057 mpt2sas_transport_update_links(ioc, sas_address,
7058 handle, sas_device_pg0.PhyNum,
7059 MPI2_SAS_NEG_LINK_RATE_1_5);
7060 set_bit(handle, ioc->pd_handles);
7061 _scsih_add_device(ioc, handle, 0, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307062 }
7063 }
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307064
7065 /* volumes */
7066 handle = 0xFFFF;
7067 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
7068 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
7069 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7070 MPI2_IOCSTATUS_MASK;
7071 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7072 break;
7073 handle = le16_to_cpu(volume_pg1.DevHandle);
7074 raid_device = _scsih_raid_device_find_by_wwid(ioc,
7075 le64_to_cpu(volume_pg1.WWID));
7076 if (raid_device)
7077 continue;
7078 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
7079 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
7080 sizeof(Mpi2RaidVolPage0_t)))
7081 continue;
7082 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
7083 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
7084 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED) {
7085 memset(&element, 0, sizeof(Mpi2EventIrConfigElement_t));
7086 element.ReasonCode = MPI2_EVENT_IR_CHANGE_RC_ADDED;
7087 element.VolDevHandle = volume_pg1.DevHandle;
7088 _scsih_sas_volume_add(ioc, &element);
7089 }
7090 }
7091
7092 skip_to_sas:
7093
7094 /* sas devices */
7095 handle = 0xFFFF;
7096 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
7097 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
7098 handle))) {
7099 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
7100 MPI2_IOCSTATUS_MASK;
7101 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
7102 break;
7103 handle = le16_to_cpu(sas_device_pg0.DevHandle);
7104 if (!(_scsih_is_end_device(
7105 le32_to_cpu(sas_device_pg0.DeviceInfo))))
7106 continue;
7107 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
7108 le64_to_cpu(sas_device_pg0.SASAddress));
7109 if (sas_device)
7110 continue;
7111 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
7112 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address)) {
7113 mpt2sas_transport_update_links(ioc, sas_address, handle,
7114 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
7115 _scsih_add_device(ioc, handle, 0, 0);
7116 }
7117 }
7118
7119 printk(MPT2SAS_INFO_FMT "scan devices: complete\n", ioc->name);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307120}
7121
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307122
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307123/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307124 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
7125 * @ioc: per adapter object
7126 * @reset_phase: phase
7127 *
7128 * The handler for doing any required cleanup or initialization.
7129 *
7130 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
7131 * MPT2_IOC_DONE_RESET
7132 *
7133 * Return nothing.
7134 */
7135void
7136mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
7137{
7138 switch (reset_phase) {
7139 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307140 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307141 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307142 break;
7143 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307144 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307145 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307146 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
7147 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
7148 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
7149 complete(&ioc->scsih_cmds.done);
7150 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307151 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
7152 ioc->tm_cmds.status |= MPT2_CMD_RESET;
7153 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
7154 complete(&ioc->tm_cmds.done);
7155 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307156 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307157 _scsih_flush_running_cmds(ioc);
7158 break;
7159 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05307160 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307161 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307162 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05307163 _scsih_prep_device_scan(ioc);
7164 _scsih_search_responding_sas_devices(ioc);
7165 _scsih_search_responding_raid_devices(ioc);
7166 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307167 if (!ioc->is_driver_loading) {
7168 _scsih_prep_device_scan(ioc);
7169 _scsih_search_responding_sas_devices(ioc);
7170 _scsih_search_responding_raid_devices(ioc);
7171 _scsih_search_responding_expanders(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307172 _scsih_error_recovery_delete_devices(ioc);
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307173 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05307174 break;
Eric Moore635374e2009-03-09 01:21:12 -06007175 }
7176}
7177
7178/**
7179 * _firmware_event_work - delayed task for processing firmware events
7180 * @ioc: per adapter object
7181 * @work: equal to the fw_event_work object
7182 * Context: user.
7183 *
7184 * Return nothing.
7185 */
7186static void
7187_firmware_event_work(struct work_struct *work)
7188{
7189 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307190 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06007191 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
7192
Eric Moore635374e2009-03-09 01:21:12 -06007193 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06007194 if (ioc->remove_host || fw_event->cancel_pending_work ||
7195 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06007196 _scsih_fw_event_free(ioc, fw_event);
7197 return;
7198 }
Eric Moore635374e2009-03-09 01:21:12 -06007199
Eric Moore635374e2009-03-09 01:21:12 -06007200 switch (fw_event->event) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307201 case MPT2SAS_REMOVE_UNRESPONDING_DEVICES:
7202 while (scsi_host_in_recovery(ioc->shost))
7203 ssleep(1);
7204 _scsih_remove_unresponding_sas_devices(ioc);
7205 _scsih_scan_for_devices_after_reset(ioc);
7206 break;
7207 case MPT2SAS_PORT_ENABLE_COMPLETE:
nagalakshmi.nandigama@lsi.com918134e2011-10-19 15:37:24 +05307208 ioc->start_scan = 0;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307209
7210
7211
7212 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "port enable: complete "
7213 "from worker thread\n", ioc->name));
7214 break;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05307215 case MPT2SAS_TURN_ON_FAULT_LED:
7216 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
7217 break;
Eric Moore635374e2009-03-09 01:21:12 -06007218 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307219 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007220 break;
7221 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307222 _scsih_sas_device_status_change_event(ioc,
7223 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007224 break;
7225 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307226 _scsih_sas_discovery_event(ioc,
7227 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007228 break;
7229 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307230 _scsih_sas_broadcast_primative_event(ioc,
7231 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007232 break;
7233 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
7234 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307235 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007236 break;
7237 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307238 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007239 break;
7240 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307241 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007242 break;
7243 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307244 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007245 break;
7246 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307247 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06007248 break;
Eric Moore635374e2009-03-09 01:21:12 -06007249 }
7250 _scsih_fw_event_free(ioc, fw_event);
7251}
7252
7253/**
7254 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
7255 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307256 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06007257 * @reply: reply message frame(lower 32bit addr)
7258 * Context: interrupt.
7259 *
7260 * This function merely adds a new work task into ioc->firmware_event_thread.
7261 * The tasks are worked from _firmware_event_work in user context.
7262 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307263 * Return 1 meaning mf should be freed from _base_interrupt
7264 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06007265 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307266u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307267mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
7268 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06007269{
7270 struct fw_event_work *fw_event;
7271 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06007272 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307273 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06007274
7275 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06007276 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307277 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007278
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307279 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06007280 event = le16_to_cpu(mpi_reply->Event);
7281
7282 switch (event) {
7283 /* handle these */
7284 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
7285 {
7286 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
7287 (Mpi2EventDataSasBroadcastPrimitive_t *)
7288 mpi_reply->EventData;
7289
7290 if (baen_data->Primitive !=
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307291 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307292 return 1;
Kashyap, Desaif93213d2011-06-14 10:56:43 +05307293
7294 if (ioc->broadcast_aen_busy) {
7295 ioc->broadcast_aen_pending++;
7296 return 1;
7297 } else
7298 ioc->broadcast_aen_busy = 1;
Eric Moore635374e2009-03-09 01:21:12 -06007299 break;
7300 }
7301
7302 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
7303 _scsih_check_topo_delete_events(ioc,
7304 (Mpi2EventDataSasTopologyChangeList_t *)
7305 mpi_reply->EventData);
7306 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307307 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
7308 _scsih_check_ir_config_unhide_events(ioc,
7309 (Mpi2EventDataIrConfigChangeList_t *)
7310 mpi_reply->EventData);
7311 break;
7312 case MPI2_EVENT_IR_VOLUME:
7313 _scsih_check_volume_delete_events(ioc,
7314 (Mpi2EventDataIrVolume_t *)
7315 mpi_reply->EventData);
7316 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307317 case MPI2_EVENT_LOG_ENTRY_ADDED:
7318 {
7319 Mpi2EventDataLogEntryAdded_t *log_entry;
7320 u32 *log_code;
7321
7322 if (!ioc->is_warpdrive)
7323 break;
7324
7325 log_entry = (Mpi2EventDataLogEntryAdded_t *)
7326 mpi_reply->EventData;
7327 log_code = (u32 *)log_entry->LogData;
7328
7329 if (le16_to_cpu(log_entry->LogEntryQualifier)
7330 != MPT2_WARPDRIVE_LOGENTRY)
7331 break;
7332
7333 switch (le32_to_cpu(*log_code)) {
7334 case MPT2_WARPDRIVE_LC_SSDT:
7335 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7336 "IO Throttling has occurred in the WarpDrive "
7337 "subsystem. Check WarpDrive documentation for "
7338 "additional details.\n", ioc->name);
7339 break;
7340 case MPT2_WARPDRIVE_LC_SSDLW:
7341 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
7342 "Program/Erase Cycles for the WarpDrive subsystem "
7343 "in degraded range. Check WarpDrive documentation "
7344 "for additional details.\n", ioc->name);
7345 break;
7346 case MPT2_WARPDRIVE_LC_SSDLF:
7347 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7348 "There are no Program/Erase Cycles for the "
7349 "WarpDrive subsystem. The storage device will be "
7350 "in read-only mode. Check WarpDrive documentation "
7351 "for additional details.\n", ioc->name);
7352 break;
7353 case MPT2_WARPDRIVE_LC_BRMF:
7354 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
7355 "The Backup Rail Monitor has failed on the "
7356 "WarpDrive subsystem. Check WarpDrive "
7357 "documentation for additional details.\n",
7358 ioc->name);
7359 break;
7360 }
7361
7362 break;
7363 }
Eric Moore635374e2009-03-09 01:21:12 -06007364 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
7365 case MPI2_EVENT_IR_OPERATION_STATUS:
7366 case MPI2_EVENT_SAS_DISCOVERY:
7367 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06007368 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06007369 break;
7370
7371 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307372 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007373 }
7374
7375 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
7376 if (!fw_event) {
7377 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7378 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307379 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007380 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307381 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
7382 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06007383 if (!fw_event->event_data) {
7384 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7385 ioc->name, __FILE__, __LINE__, __func__);
7386 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307387 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007388 }
7389
7390 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05307391 sz);
Eric Moore635374e2009-03-09 01:21:12 -06007392 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05307393 fw_event->VF_ID = mpi_reply->VF_ID;
7394 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06007395 fw_event->event = event;
7396 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307397 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06007398}
7399
7400/* shost template */
7401static struct scsi_host_template scsih_driver_template = {
7402 .module = THIS_MODULE,
7403 .name = "Fusion MPT SAS Host",
7404 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06007405 .queuecommand = _scsih_qcmd,
7406 .target_alloc = _scsih_target_alloc,
7407 .slave_alloc = _scsih_slave_alloc,
7408 .slave_configure = _scsih_slave_configure,
7409 .target_destroy = _scsih_target_destroy,
7410 .slave_destroy = _scsih_slave_destroy,
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307411 .scan_finished = _scsih_scan_finished,
7412 .scan_start = _scsih_scan_start,
Eric Moored5d135b2009-05-18 13:02:08 -06007413 .change_queue_depth = _scsih_change_queue_depth,
7414 .change_queue_type = _scsih_change_queue_type,
7415 .eh_abort_handler = _scsih_abort,
7416 .eh_device_reset_handler = _scsih_dev_reset,
7417 .eh_target_reset_handler = _scsih_target_reset,
7418 .eh_host_reset_handler = _scsih_host_reset,
7419 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06007420 .can_queue = 1,
7421 .this_id = -1,
7422 .sg_tablesize = MPT2SAS_SG_DEPTH,
7423 .max_sectors = 8192,
7424 .cmd_per_lun = 7,
7425 .use_clustering = ENABLE_CLUSTERING,
7426 .shost_attrs = mpt2sas_host_attrs,
7427 .sdev_attrs = mpt2sas_dev_attrs,
7428};
7429
7430/**
7431 * _scsih_expander_node_remove - removing expander device from list.
7432 * @ioc: per adapter object
7433 * @sas_expander: the sas_device object
7434 * Context: Calling function should acquire ioc->sas_node_lock.
7435 *
7436 * Removing object and freeing associated memory from the
7437 * ioc->sas_expander_list.
7438 *
7439 * Return nothing.
7440 */
7441static void
7442_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7443 struct _sas_node *sas_expander)
7444{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307445 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007446
7447 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307448 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007449 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307450 if (ioc->shost_recovery)
7451 return;
Eric Moore635374e2009-03-09 01:21:12 -06007452 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307453 SAS_END_DEVICE)
7454 mpt2sas_device_remove(ioc,
7455 mpt2sas_port->remote_identify.sas_address);
7456 else if (mpt2sas_port->remote_identify.device_type ==
7457 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007458 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307459 SAS_FANOUT_EXPANDER_DEVICE)
7460 mpt2sas_expander_remove(ioc,
7461 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007462 }
7463
7464 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307465 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007466
7467 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7468 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7469 sas_expander->handle, (unsigned long long)
7470 sas_expander->sas_address);
7471
Eric Moore635374e2009-03-09 01:21:12 -06007472 kfree(sas_expander->phy);
7473 kfree(sas_expander);
7474}
7475
7476/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307477 * _scsih_ir_shutdown - IR shutdown notification
7478 * @ioc: per adapter object
7479 *
7480 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7481 * the host system is shutting down.
7482 *
7483 * Return nothing.
7484 */
7485static void
7486_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7487{
7488 Mpi2RaidActionRequest_t *mpi_request;
7489 Mpi2RaidActionReply_t *mpi_reply;
7490 u16 smid;
7491
7492 /* is IR firmware build loaded ? */
7493 if (!ioc->ir_firmware)
7494 return;
7495
7496 /* are there any volumes ? */
7497 if (list_empty(&ioc->raid_device_list))
7498 return;
7499
7500 mutex_lock(&ioc->scsih_cmds.mutex);
7501
7502 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7503 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7504 ioc->name, __func__);
7505 goto out;
7506 }
7507 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7508
7509 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7510 if (!smid) {
7511 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7512 ioc->name, __func__);
7513 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7514 goto out;
7515 }
7516
7517 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7518 ioc->scsih_cmds.smid = smid;
7519 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7520
7521 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7522 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7523
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307524 if (!ioc->hide_ir_msg)
7525 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307526 init_completion(&ioc->scsih_cmds.done);
7527 mpt2sas_base_put_smid_default(ioc, smid);
7528 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7529
7530 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7531 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7532 ioc->name, __func__);
7533 goto out;
7534 }
7535
7536 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7537 mpi_reply = ioc->scsih_cmds.reply;
7538
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307539 if (!ioc->hide_ir_msg)
7540 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7541 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7542 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7543 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307544 }
7545
7546 out:
7547 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7548 mutex_unlock(&ioc->scsih_cmds.mutex);
7549}
7550
7551/**
7552 * _scsih_shutdown - routine call during system shutdown
7553 * @pdev: PCI device struct
7554 *
7555 * Return nothing.
7556 */
7557static void
7558_scsih_shutdown(struct pci_dev *pdev)
7559{
7560 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7561 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307562 struct workqueue_struct *wq;
7563 unsigned long flags;
7564
7565 ioc->remove_host = 1;
7566 _scsih_fw_event_cleanup_queue(ioc);
7567
7568 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7569 wq = ioc->firmware_event_thread;
7570 ioc->firmware_event_thread = NULL;
7571 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7572 if (wq)
7573 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307574
7575 _scsih_ir_shutdown(ioc);
7576 mpt2sas_base_detach(ioc);
7577}
7578
7579/**
Eric Moored5d135b2009-05-18 13:02:08 -06007580 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007581 * @pdev: PCI device struct
7582 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307583 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007584 * Return nothing.
7585 */
7586static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007587_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007588{
7589 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7590 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307591 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307592 struct _raid_device *raid_device, *next;
7593 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007594 struct workqueue_struct *wq;
7595 unsigned long flags;
7596
7597 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307598 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007599
7600 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7601 wq = ioc->firmware_event_thread;
7602 ioc->firmware_event_thread = NULL;
7603 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7604 if (wq)
7605 destroy_workqueue(wq);
7606
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307607 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307608 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307609 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7610 list) {
7611 if (raid_device->starget) {
7612 sas_target_priv_data =
7613 raid_device->starget->hostdata;
7614 sas_target_priv_data->deleted = 1;
7615 scsi_remove_target(&raid_device->starget->dev);
7616 }
7617 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7618 "(0x%016llx)\n", ioc->name, raid_device->handle,
7619 (unsigned long long) raid_device->wwid);
7620 _scsih_raid_device_remove(ioc, raid_device);
7621 }
7622
Eric Moore635374e2009-03-09 01:21:12 -06007623 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307624 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007625 &ioc->sas_hba.sas_port_list, port_list) {
7626 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307627 SAS_END_DEVICE)
7628 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007629 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307630 else if (mpt2sas_port->remote_identify.device_type ==
7631 SAS_EDGE_EXPANDER_DEVICE ||
7632 mpt2sas_port->remote_identify.device_type ==
7633 SAS_FANOUT_EXPANDER_DEVICE)
7634 mpt2sas_expander_remove(ioc,
7635 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007636 }
7637
7638 /* free phys attached to the sas_host */
7639 if (ioc->sas_hba.num_phys) {
7640 kfree(ioc->sas_hba.phy);
7641 ioc->sas_hba.phy = NULL;
7642 ioc->sas_hba.num_phys = 0;
7643 }
7644
7645 sas_remove_host(shost);
kashyap.desai@lsi.com9ae89b02011-08-04 16:47:50 +05307646 mpt2sas_base_detach(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007647 list_del(&ioc->list);
7648 scsi_remove_host(shost);
7649 scsi_host_put(shost);
7650}
7651
7652/**
7653 * _scsih_probe_boot_devices - reports 1st device
7654 * @ioc: per adapter object
7655 *
7656 * If specified in bios page 2, this routine reports the 1st
7657 * device scsi-ml or sas transport for persistent boot device
7658 * purposes. Please refer to function _scsih_determine_boot_device()
7659 */
7660static void
7661_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7662{
7663 u8 is_raid;
7664 void *device;
7665 struct _sas_device *sas_device;
7666 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307667 u16 handle;
7668 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007669 u64 sas_address;
7670 unsigned long flags;
7671 int rc;
7672
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307673 /* no Bios, return immediately */
7674 if (!ioc->bios_pg3.BiosVersion)
7675 return;
7676
Eric Moore635374e2009-03-09 01:21:12 -06007677 device = NULL;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307678 is_raid = 0;
Eric Moore635374e2009-03-09 01:21:12 -06007679 if (ioc->req_boot_device.device) {
7680 device = ioc->req_boot_device.device;
7681 is_raid = ioc->req_boot_device.is_raid;
7682 } else if (ioc->req_alt_boot_device.device) {
7683 device = ioc->req_alt_boot_device.device;
7684 is_raid = ioc->req_alt_boot_device.is_raid;
7685 } else if (ioc->current_boot_device.device) {
7686 device = ioc->current_boot_device.device;
7687 is_raid = ioc->current_boot_device.is_raid;
7688 }
7689
7690 if (!device)
7691 return;
7692
7693 if (is_raid) {
7694 raid_device = device;
7695 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7696 raid_device->id, 0);
7697 if (rc)
7698 _scsih_raid_device_remove(ioc, raid_device);
7699 } else {
7700 sas_device = device;
7701 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307702 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007703 sas_address = sas_device->sas_address;
7704 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7705 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7706 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307707
7708 if (ioc->hide_drives)
7709 return;
Eric Moore635374e2009-03-09 01:21:12 -06007710 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307711 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007712 _scsih_sas_device_remove(ioc, sas_device);
7713 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307714 if (!ioc->is_driver_loading)
7715 mpt2sas_transport_port_remove(ioc, sas_address,
7716 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007717 _scsih_sas_device_remove(ioc, sas_device);
7718 }
7719 }
7720}
7721
7722/**
7723 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7724 * @ioc: per adapter object
7725 *
7726 * Called during initial loading of the driver.
7727 */
7728static void
7729_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7730{
7731 struct _raid_device *raid_device, *raid_next;
7732 int rc;
7733
7734 list_for_each_entry_safe(raid_device, raid_next,
7735 &ioc->raid_device_list, list) {
7736 if (raid_device->starget)
7737 continue;
7738 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7739 raid_device->id, 0);
7740 if (rc)
7741 _scsih_raid_device_remove(ioc, raid_device);
7742 }
7743}
7744
7745/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307746 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007747 * @ioc: per adapter object
7748 *
7749 * Called during initial loading of the driver.
7750 */
7751static void
7752_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7753{
7754 struct _sas_device *sas_device, *next;
7755 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007756
7757 /* SAS Device List */
7758 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7759 list) {
Eric Moore635374e2009-03-09 01:21:12 -06007760
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307761 if (ioc->hide_drives)
7762 continue;
7763
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307764 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7765 sas_device->sas_address_parent)) {
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307766 list_del(&sas_device->list);
7767 kfree(sas_device);
7768 continue;
Eric Moore635374e2009-03-09 01:21:12 -06007769 } else if (!sas_device->starget) {
nagalakshmi.nandigama@lsi.com35116db2011-10-21 10:08:07 +05307770 if (!ioc->is_driver_loading)
7771 mpt2sas_transport_port_remove(ioc,
7772 sas_device->sas_address,
7773 sas_device->sas_address_parent);
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307774 list_del(&sas_device->list);
7775 kfree(sas_device);
7776 continue;
7777
Eric Moore635374e2009-03-09 01:21:12 -06007778 }
nagalakshmi.nandigama@lsi.com0167ac62011-10-21 10:06:33 +05307779 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7780 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7781 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06007782 }
7783}
7784
7785/**
7786 * _scsih_probe_devices - probing for devices
7787 * @ioc: per adapter object
7788 *
7789 * Called during initial loading of the driver.
7790 */
7791static void
7792_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7793{
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307794 u16 volume_mapping_flags;
Eric Moore635374e2009-03-09 01:21:12 -06007795
7796 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7797 return; /* return when IOC doesn't support initiator mode */
7798
7799 _scsih_probe_boot_devices(ioc);
7800
7801 if (ioc->ir_firmware) {
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307802 volume_mapping_flags =
7803 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7804 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7805 if (volume_mapping_flags ==
7806 MPI2_IOCPAGE8_IRFLAGS_LOW_VOLUME_MAPPING) {
Eric Moore635374e2009-03-09 01:21:12 -06007807 _scsih_probe_raid(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307808 _scsih_probe_sas(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007809 } else {
Eric Moore635374e2009-03-09 01:21:12 -06007810 _scsih_probe_sas(ioc);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307811 _scsih_probe_raid(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007812 }
7813 } else
7814 _scsih_probe_sas(ioc);
7815}
7816
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307817
7818/**
7819 * _scsih_scan_start - scsi lld callback for .scan_start
7820 * @shost: SCSI host pointer
7821 *
7822 * The shost has the ability to discover targets on its own instead
7823 * of scanning the entire bus. In our implemention, we will kick off
7824 * firmware discovery.
7825 */
7826static void
7827_scsih_scan_start(struct Scsi_Host *shost)
7828{
7829 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7830 int rc;
7831
7832 if (diag_buffer_enable != -1 && diag_buffer_enable != 0)
7833 mpt2sas_enable_diag_buffer(ioc, diag_buffer_enable);
7834
7835 ioc->start_scan = 1;
7836 rc = mpt2sas_port_enable(ioc);
7837
7838 if (rc != 0)
7839 printk(MPT2SAS_INFO_FMT "port enable: FAILED\n", ioc->name);
7840}
7841
7842/**
7843 * _scsih_scan_finished - scsi lld callback for .scan_finished
7844 * @shost: SCSI host pointer
7845 * @time: elapsed time of the scan in jiffies
7846 *
7847 * This function will be called periodically until it returns 1 with the
7848 * scsi_host and the elapsed time of the scan in jiffies. In our implemention,
7849 * we wait for firmware discovery to complete, then return 1.
7850 */
7851static int
7852_scsih_scan_finished(struct Scsi_Host *shost, unsigned long time)
7853{
7854 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7855
7856 if (time >= (300 * HZ)) {
7857 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7858 printk(MPT2SAS_INFO_FMT "port enable: FAILED with timeout "
7859 "(timeout=300s)\n", ioc->name);
7860 ioc->is_driver_loading = 0;
7861 return 1;
7862 }
7863
7864 if (ioc->start_scan)
7865 return 0;
7866
7867 if (ioc->start_scan_failed) {
7868 printk(MPT2SAS_INFO_FMT "port enable: FAILED with "
7869 "(ioc_status=0x%08x)\n", ioc->name, ioc->start_scan_failed);
7870 ioc->is_driver_loading = 0;
7871 ioc->wait_for_discovery_to_complete = 0;
7872 ioc->remove_host = 1;
7873 return 1;
7874 }
7875
7876 printk(MPT2SAS_INFO_FMT "port enable: SUCCESS\n", ioc->name);
7877 ioc->base_cmds.status = MPT2_CMD_NOT_USED;
7878
7879 if (ioc->wait_for_discovery_to_complete) {
7880 ioc->wait_for_discovery_to_complete = 0;
7881 _scsih_probe_devices(ioc);
7882 }
7883 mpt2sas_base_start_watchdog(ioc);
7884 ioc->is_driver_loading = 0;
7885 return 1;
7886}
7887
7888
Eric Moore635374e2009-03-09 01:21:12 -06007889/**
Eric Moored5d135b2009-05-18 13:02:08 -06007890 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06007891 * @pdev: PCI device struct
7892 * @id: pci device id
7893 *
7894 * Returns 0 success, anything else error.
7895 */
7896static int
Eric Moored5d135b2009-05-18 13:02:08 -06007897_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06007898{
7899 struct MPT2SAS_ADAPTER *ioc;
7900 struct Scsi_Host *shost;
7901
7902 shost = scsi_host_alloc(&scsih_driver_template,
7903 sizeof(struct MPT2SAS_ADAPTER));
7904 if (!shost)
7905 return -ENODEV;
7906
7907 /* init local params */
7908 ioc = shost_priv(shost);
7909 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7910 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06007911 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06007912 ioc->shost = shost;
7913 ioc->id = mpt_ids++;
7914 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7915 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307916 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7917 ioc->is_warpdrive = 1;
7918 ioc->hide_ir_msg = 1;
7919 } else
7920 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06007921 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7922 ioc->tm_cb_idx = tm_cb_idx;
7923 ioc->ctl_cb_idx = ctl_cb_idx;
7924 ioc->base_cb_idx = base_cb_idx;
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307925 ioc->port_enable_cb_idx = port_enable_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007926 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05307927 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007928 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307929 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307930 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307931 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007932 ioc->logging_level = logging_level;
nagalakshmi.nandigama@lsi.com845a0e42011-12-01 07:42:04 +05307933 ioc->schedule_dead_ioc_flush_running_cmds = &_scsih_flush_running_cmds;
Eric Moore635374e2009-03-09 01:21:12 -06007934 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05307935 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06007936 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7937 spin_lock_init(&ioc->scsi_lookup_lock);
7938 spin_lock_init(&ioc->sas_device_lock);
7939 spin_lock_init(&ioc->sas_node_lock);
7940 spin_lock_init(&ioc->fw_event_lock);
7941 spin_lock_init(&ioc->raid_device_lock);
7942
7943 INIT_LIST_HEAD(&ioc->sas_device_list);
7944 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7945 INIT_LIST_HEAD(&ioc->sas_expander_list);
7946 INIT_LIST_HEAD(&ioc->fw_event_list);
7947 INIT_LIST_HEAD(&ioc->raid_device_list);
7948 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307949 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307950 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06007951
7952 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06007953 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06007954 shost->max_lun = max_lun;
7955 shost->transportt = mpt2sas_transport_template;
7956 shost->unique_id = ioc->id;
7957
Kashyap, Desaia3e1e552011-06-14 10:56:12 +05307958 if (max_sectors != 0xFFFF) {
7959 if (max_sectors < 64) {
7960 shost->max_sectors = 64;
7961 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7962 "for max_sectors, range is 64 to 8192. Assigning "
7963 "value of 64.\n", ioc->name, max_sectors);
7964 } else if (max_sectors > 8192) {
7965 shost->max_sectors = 8192;
7966 printk(MPT2SAS_WARN_FMT "Invalid value %d passed "
7967 "for max_sectors, range is 64 to 8192. Assigning "
7968 "default value of 8192.\n", ioc->name,
7969 max_sectors);
7970 } else {
7971 shost->max_sectors = max_sectors & 0xFFFE;
7972 printk(MPT2SAS_INFO_FMT "The max_sectors value is "
7973 "set to %d\n", ioc->name, shost->max_sectors);
7974 }
7975 }
7976
Eric Moore635374e2009-03-09 01:21:12 -06007977 if ((scsi_add_host(shost, &pdev->dev))) {
7978 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7979 ioc->name, __FILE__, __LINE__, __func__);
7980 list_del(&ioc->list);
7981 goto out_add_shost_fail;
7982 }
7983
Eric Moore3c621b32009-05-18 12:59:41 -06007984 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06007985 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307986 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06007987
Eric Moore635374e2009-03-09 01:21:12 -06007988 /* event thread */
7989 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7990 "fw_event%d", ioc->id);
7991 ioc->firmware_event_thread = create_singlethread_workqueue(
7992 ioc->firmware_event_name);
7993 if (!ioc->firmware_event_thread) {
7994 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7995 ioc->name, __FILE__, __LINE__, __func__);
7996 goto out_thread_fail;
7997 }
7998
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05307999 ioc->is_driver_loading = 1;
Eric Moore635374e2009-03-09 01:21:12 -06008000 if ((mpt2sas_base_attach(ioc))) {
8001 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
8002 ioc->name, __FILE__, __LINE__, __func__);
8003 goto out_attach_fail;
8004 }
8005
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308006 scsi_scan_host(shost);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05308007 if (ioc->is_warpdrive) {
8008 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
8009 ioc->hide_drives = 0;
8010 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
8011 ioc->hide_drives = 1;
8012 else {
8013 if (_scsih_get_num_volumes(ioc))
8014 ioc->hide_drives = 1;
8015 else
8016 ioc->hide_drives = 0;
8017 }
8018 } else
8019 ioc->hide_drives = 0;
8020
Eric Moore635374e2009-03-09 01:21:12 -06008021 _scsih_probe_devices(ioc);
8022 return 0;
8023
8024 out_attach_fail:
8025 destroy_workqueue(ioc->firmware_event_thread);
8026 out_thread_fail:
8027 list_del(&ioc->list);
8028 scsi_remove_host(shost);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308029 scsi_host_put(shost);
Eric Moore635374e2009-03-09 01:21:12 -06008030 out_add_shost_fail:
8031 return -ENODEV;
8032}
8033
8034#ifdef CONFIG_PM
8035/**
Eric Moored5d135b2009-05-18 13:02:08 -06008036 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008037 * @pdev: PCI device struct
8038 * @state: PM state change to (usually PCI_D3)
8039 *
8040 * Returns 0 success, anything else error.
8041 */
8042static int
Eric Moored5d135b2009-05-18 13:02:08 -06008043_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06008044{
8045 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8046 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308047 pci_power_t device_state;
Eric Moore635374e2009-03-09 01:21:12 -06008048
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308049 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008050 scsi_block_requests(shost);
8051 device_state = pci_choose_state(pdev, state);
8052 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
8053 "operating state [D%d]\n", ioc->name, pdev,
8054 pci_name(pdev), device_state);
8055
8056 mpt2sas_base_free_resources(ioc);
8057 pci_save_state(pdev);
8058 pci_disable_device(pdev);
8059 pci_set_power_state(pdev, device_state);
8060 return 0;
8061}
8062
8063/**
Eric Moored5d135b2009-05-18 13:02:08 -06008064 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06008065 * @pdev: PCI device struct
8066 *
8067 * Returns 0 success, anything else error.
8068 */
8069static int
Eric Moored5d135b2009-05-18 13:02:08 -06008070_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06008071{
8072 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8073 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaic97951e2011-06-14 10:54:56 +05308074 pci_power_t device_state = pdev->current_state;
Eric Moore635374e2009-03-09 01:21:12 -06008075 int r;
8076
8077 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
8078 "operating state [D%d]\n", ioc->name, pdev,
8079 pci_name(pdev), device_state);
8080
8081 pci_set_power_state(pdev, PCI_D0);
8082 pci_enable_wake(pdev, PCI_D0, 0);
8083 pci_restore_state(pdev);
8084 ioc->pdev = pdev;
8085 r = mpt2sas_base_map_resources(ioc);
8086 if (r)
8087 return r;
8088
8089 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
8090 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05308091 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06008092 return 0;
8093}
8094#endif /* CONFIG_PM */
8095
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308096/**
8097 * _scsih_pci_error_detected - Called when a PCI error is detected.
8098 * @pdev: PCI device struct
8099 * @state: PCI channel state
8100 *
8101 * Description: Called when a PCI error is detected.
8102 *
8103 * Return value:
8104 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
8105 */
8106static pci_ers_result_t
8107_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
8108{
8109 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8110 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8111
8112 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
8113 ioc->name, state);
8114
8115 switch (state) {
8116 case pci_channel_io_normal:
8117 return PCI_ERS_RESULT_CAN_RECOVER;
8118 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06008119 /* Fatal error, prepare for slot reset */
8120 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308121 scsi_block_requests(ioc->shost);
8122 mpt2sas_base_stop_watchdog(ioc);
8123 mpt2sas_base_free_resources(ioc);
8124 return PCI_ERS_RESULT_NEED_RESET;
8125 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06008126 /* Permanent error, prepare for device removal */
8127 ioc->pci_error_recovery = 1;
8128 mpt2sas_base_stop_watchdog(ioc);
8129 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308130 return PCI_ERS_RESULT_DISCONNECT;
8131 }
8132 return PCI_ERS_RESULT_NEED_RESET;
8133}
8134
8135/**
8136 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
8137 * @pdev: PCI device struct
8138 *
8139 * Description: This routine is called by the pci error recovery
8140 * code after the PCI slot has been reset, just before we
8141 * should resume normal operations.
8142 */
8143static pci_ers_result_t
8144_scsih_pci_slot_reset(struct pci_dev *pdev)
8145{
8146 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8147 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8148 int rc;
8149
8150 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
8151 ioc->name);
8152
Eric Moore3cb54692010-07-08 14:44:34 -06008153 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308154 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06008155 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308156 rc = mpt2sas_base_map_resources(ioc);
8157 if (rc)
8158 return PCI_ERS_RESULT_DISCONNECT;
8159
8160
8161 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
8162 FORCE_BIG_HAMMER);
8163
8164 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
8165 (rc == 0) ? "success" : "failed");
8166
8167 if (!rc)
8168 return PCI_ERS_RESULT_RECOVERED;
8169 else
8170 return PCI_ERS_RESULT_DISCONNECT;
8171}
8172
8173/**
8174 * _scsih_pci_resume() - resume normal ops after PCI reset
8175 * @pdev: pointer to PCI device
8176 *
8177 * Called when the error recovery driver tells us that its
8178 * OK to resume normal operation. Use completion to allow
8179 * halted scsi ops to resume.
8180 */
8181static void
8182_scsih_pci_resume(struct pci_dev *pdev)
8183{
8184 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8185 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8186
8187 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
8188
8189 pci_cleanup_aer_uncorrect_error_status(pdev);
8190 mpt2sas_base_start_watchdog(ioc);
8191 scsi_unblock_requests(ioc->shost);
8192}
8193
8194/**
8195 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
8196 * @pdev: pointer to PCI device
8197 */
8198static pci_ers_result_t
8199_scsih_pci_mmio_enabled(struct pci_dev *pdev)
8200{
8201 struct Scsi_Host *shost = pci_get_drvdata(pdev);
8202 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
8203
8204 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
8205 ioc->name);
8206
8207 /* TODO - dump whatever for debugging purposes */
8208
8209 /* Request a slot reset. */
8210 return PCI_ERS_RESULT_NEED_RESET;
8211}
8212
8213static struct pci_error_handlers _scsih_err_handler = {
8214 .error_detected = _scsih_pci_error_detected,
8215 .mmio_enabled = _scsih_pci_mmio_enabled,
8216 .slot_reset = _scsih_pci_slot_reset,
8217 .resume = _scsih_pci_resume,
8218};
Eric Moore635374e2009-03-09 01:21:12 -06008219
8220static struct pci_driver scsih_driver = {
8221 .name = MPT2SAS_DRIVER_NAME,
8222 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06008223 .probe = _scsih_probe,
8224 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05308225 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05308226 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06008227#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06008228 .suspend = _scsih_suspend,
8229 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06008230#endif
8231};
8232
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308233/* raid transport support */
8234static struct raid_function_template mpt2sas_raid_functions = {
8235 .cookie = &scsih_driver_template,
8236 .is_raid = _scsih_is_raid,
8237 .get_resync = _scsih_get_resync,
8238 .get_state = _scsih_get_state,
8239};
Eric Moore635374e2009-03-09 01:21:12 -06008240
8241/**
Eric Moored5d135b2009-05-18 13:02:08 -06008242 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06008243 *
8244 * Returns 0 success, anything else error.
8245 */
8246static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06008247_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06008248{
8249 int error;
8250
8251 mpt_ids = 0;
8252 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
8253 MPT2SAS_DRIVER_VERSION);
8254
8255 mpt2sas_transport_template =
8256 sas_attach_transport(&mpt2sas_transport_functions);
8257 if (!mpt2sas_transport_template)
8258 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308259 /* raid transport support */
8260 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
8261 if (!mpt2sas_raid_template) {
8262 sas_release_transport(mpt2sas_transport_template);
8263 return -ENODEV;
8264 }
Eric Moore635374e2009-03-09 01:21:12 -06008265
8266 mpt2sas_base_initialize_callback_handler();
8267
8268 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06008269 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06008270
Uwe Kleine-König65155b32010-06-11 12:17:01 +02008271 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06008272 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06008273
8274 /* base internal commands callback handler */
8275 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308276 port_enable_cb_idx = mpt2sas_base_register_callback_handler(
8277 mpt2sas_port_enable_done);
Eric Moore635374e2009-03-09 01:21:12 -06008278
8279 /* transport internal commands callback handler */
8280 transport_cb_idx = mpt2sas_base_register_callback_handler(
8281 mpt2sas_transport_done);
8282
Kashyap, Desai744090d2009-10-05 15:56:56 +05308283 /* scsih internal commands callback handler */
8284 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
8285
Eric Moore635374e2009-03-09 01:21:12 -06008286 /* configuration page API internal commands callback handler */
8287 config_cb_idx = mpt2sas_base_register_callback_handler(
8288 mpt2sas_config_done);
8289
8290 /* ctl module callback handler */
8291 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
8292
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308293 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
8294 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308295
8296 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
8297 _scsih_tm_volume_tr_complete);
8298
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308299 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
8300 _scsih_sas_control_complete);
8301
Eric Moore635374e2009-03-09 01:21:12 -06008302 mpt2sas_ctl_init();
8303
8304 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308305 if (error) {
8306 /* raid transport support */
8307 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06008308 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308309 }
Eric Moore635374e2009-03-09 01:21:12 -06008310
8311 return error;
8312}
8313
8314/**
Eric Moored5d135b2009-05-18 13:02:08 -06008315 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06008316 *
8317 * Returns 0 success, anything else error.
8318 */
8319static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06008320_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06008321{
8322 printk(KERN_INFO "mpt2sas version %s unloading\n",
8323 MPT2SAS_DRIVER_VERSION);
8324
8325 pci_unregister_driver(&scsih_driver);
8326
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308327 mpt2sas_ctl_exit();
8328
Eric Moore635374e2009-03-09 01:21:12 -06008329 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
8330 mpt2sas_base_release_callback_handler(tm_cb_idx);
8331 mpt2sas_base_release_callback_handler(base_cb_idx);
nagalakshmi.nandigama@lsi.com921cd802011-10-19 15:36:26 +05308332 mpt2sas_base_release_callback_handler(port_enable_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008333 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05308334 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06008335 mpt2sas_base_release_callback_handler(config_cb_idx);
8336 mpt2sas_base_release_callback_handler(ctl_cb_idx);
8337
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308338 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05308339 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05308340 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
8341
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05308342 /* raid transport support */
8343 raid_class_release(mpt2sas_raid_template);
8344 sas_release_transport(mpt2sas_transport_template);
8345
Eric Moore635374e2009-03-09 01:21:12 -06008346}
8347
Eric Moored5d135b2009-05-18 13:02:08 -06008348module_init(_scsih_init);
8349module_exit(_scsih_exit);