blob: e97363c75074a9a916f27ea7734b74e1ec7fbb8f [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
44#include <linux/version.h>
45#include <linux/module.h>
46#include <linux/kernel.h>
47#include <linux/init.h>
48#include <linux/errno.h>
49#include <linux/blkdev.h>
50#include <linux/sched.h>
51#include <linux/workqueue.h>
52#include <linux/delay.h>
53#include <linux/pci.h>
54#include <linux/interrupt.h>
Kashyap, Desaief7c80c2010-04-05 14:20:07 +053055#include <linux/aer.h>
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +053056#include <linux/raid_class.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090057#include <linux/slab.h>
Eric Moore635374e2009-03-09 01:21:12 -060058
59#include "mpt2sas_base.h"
60
61MODULE_AUTHOR(MPT2SAS_AUTHOR);
62MODULE_DESCRIPTION(MPT2SAS_DESCRIPTION);
63MODULE_LICENSE("GPL");
64MODULE_VERSION(MPT2SAS_DRIVER_VERSION);
65
66#define RAID_CHANNEL 1
67
68/* forward proto's */
69static void _scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
70 struct _sas_node *sas_expander);
71static void _firmware_event_work(struct work_struct *work);
72
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053073static u8 _scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid);
74
Eric Moore635374e2009-03-09 01:21:12 -060075/* global parameters */
Eric Mooreba33fad2009-03-15 21:37:18 -060076LIST_HEAD(mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -060077
78/* local parameters */
Eric Moore635374e2009-03-09 01:21:12 -060079static u8 scsi_io_cb_idx = -1;
80static u8 tm_cb_idx = -1;
81static u8 ctl_cb_idx = -1;
82static u8 base_cb_idx = -1;
83static u8 transport_cb_idx = -1;
Kashyap, Desai744090d2009-10-05 15:56:56 +053084static u8 scsih_cb_idx = -1;
Eric Moore635374e2009-03-09 01:21:12 -060085static u8 config_cb_idx = -1;
86static int mpt_ids;
87
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053088static u8 tm_tr_cb_idx = -1 ;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +053089static u8 tm_tr_volume_cb_idx = -1 ;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +053090static u8 tm_sas_control_cb_idx = -1;
91
Eric Moore635374e2009-03-09 01:21:12 -060092/* command line options */
Eric Mooreba33fad2009-03-15 21:37:18 -060093static u32 logging_level;
Eric Moore635374e2009-03-09 01:21:12 -060094MODULE_PARM_DESC(logging_level, " bits for enabling additional logging info "
95 "(default=0)");
96
97/* scsi-mid layer global parmeter is max_report_luns, which is 511 */
98#define MPT2SAS_MAX_LUN (16895)
99static int max_lun = MPT2SAS_MAX_LUN;
100module_param(max_lun, int, 0);
101MODULE_PARM_DESC(max_lun, " max lun, default=16895 ");
102
103/**
104 * struct sense_info - common structure for obtaining sense keys
105 * @skey: sense key
106 * @asc: additional sense code
107 * @ascq: additional sense code qualifier
108 */
109struct sense_info {
110 u8 skey;
111 u8 asc;
112 u8 ascq;
113};
114
115
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530116#define MPT2SAS_TURN_ON_FAULT_LED (0xFFFC)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530117#define MPT2SAS_RESCAN_AFTER_HOST_RESET (0xFFFF)
118
Eric Moore635374e2009-03-09 01:21:12 -0600119/**
120 * struct fw_event_work - firmware event struct
121 * @list: link list framework
122 * @work: work object (ioc->fault_reset_work_q)
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530123 * @cancel_pending_work: flag set during reset handling
Eric Moore635374e2009-03-09 01:21:12 -0600124 * @ioc: per adapter object
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530125 * @device_handle: device handle
Eric Moore635374e2009-03-09 01:21:12 -0600126 * @VF_ID: virtual function id
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530127 * @VP_ID: virtual port id
Eric Moore635374e2009-03-09 01:21:12 -0600128 * @ignore: flag meaning this event has been marked to ignore
129 * @event: firmware event MPI2_EVENT_XXX defined in mpt2_ioc.h
130 * @event_data: reply event data payload follows
131 *
132 * This object stored on ioc->fw_event_list.
133 */
134struct fw_event_work {
135 struct list_head list;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +0530136 u8 cancel_pending_work;
137 struct delayed_work delayed_work;
Eric Moore635374e2009-03-09 01:21:12 -0600138 struct MPT2SAS_ADAPTER *ioc;
Kashyap, Desai3ace8e02011-05-04 16:35:58 +0530139 u16 device_handle;
Eric Moore635374e2009-03-09 01:21:12 -0600140 u8 VF_ID;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530141 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600142 u8 ignore;
143 u16 event;
144 void *event_data;
145};
146
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +0530147/* raid transport support */
148static struct raid_template *mpt2sas_raid_template;
149
Eric Moore635374e2009-03-09 01:21:12 -0600150/**
151 * struct _scsi_io_transfer - scsi io transfer
152 * @handle: sas device handle (assigned by firmware)
153 * @is_raid: flag set for hidden raid components
154 * @dir: DMA_TO_DEVICE, DMA_FROM_DEVICE,
155 * @data_length: data transfer length
156 * @data_dma: dma pointer to data
157 * @sense: sense data
158 * @lun: lun number
159 * @cdb_length: cdb length
160 * @cdb: cdb contents
Eric Moore635374e2009-03-09 01:21:12 -0600161 * @timeout: timeout for this command
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530162 * @VF_ID: virtual function id
163 * @VP_ID: virtual port id
164 * @valid_reply: flag set for reply message
Eric Moore635374e2009-03-09 01:21:12 -0600165 * @sense_length: sense length
166 * @ioc_status: ioc status
167 * @scsi_state: scsi state
168 * @scsi_status: scsi staus
169 * @log_info: log information
170 * @transfer_length: data length transfer when there is a reply message
171 *
172 * Used for sending internal scsi commands to devices within this module.
173 * Refer to _scsi_send_scsi_io().
174 */
175struct _scsi_io_transfer {
176 u16 handle;
177 u8 is_raid;
178 enum dma_data_direction dir;
179 u32 data_length;
180 dma_addr_t data_dma;
181 u8 sense[SCSI_SENSE_BUFFERSIZE];
182 u32 lun;
183 u8 cdb_length;
184 u8 cdb[32];
185 u8 timeout;
Kashyap, Desai7b936b02009-09-25 11:44:41 +0530186 u8 VF_ID;
187 u8 VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -0600188 u8 valid_reply;
189 /* the following bits are only valid when 'valid_reply = 1' */
190 u32 sense_length;
191 u16 ioc_status;
192 u8 scsi_state;
193 u8 scsi_status;
194 u32 log_info;
195 u32 transfer_length;
196};
197
198/*
199 * The pci device ids are defined in mpi/mpi2_cnfg.h.
200 */
201static struct pci_device_id scsih_pci_table[] = {
202 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2004,
203 PCI_ANY_ID, PCI_ANY_ID },
204 /* Falcon ~ 2008*/
205 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2008,
206 PCI_ANY_ID, PCI_ANY_ID },
207 /* Liberator ~ 2108 */
208 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_1,
209 PCI_ANY_ID, PCI_ANY_ID },
210 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_2,
211 PCI_ANY_ID, PCI_ANY_ID },
212 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2108_3,
213 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530214 /* Meteor ~ 2116 */
Eric Moore635374e2009-03-09 01:21:12 -0600215 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_1,
216 PCI_ANY_ID, PCI_ANY_ID },
217 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2116_2,
218 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desaidb271362009-09-23 17:24:27 +0530219 /* Thunderbolt ~ 2208 */
220 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_1,
221 PCI_ANY_ID, PCI_ANY_ID },
222 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_2,
223 PCI_ANY_ID, PCI_ANY_ID },
224 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_3,
225 PCI_ANY_ID, PCI_ANY_ID },
226 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_4,
227 PCI_ANY_ID, PCI_ANY_ID },
228 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_5,
229 PCI_ANY_ID, PCI_ANY_ID },
230 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2208_6,
231 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530232 /* Mustang ~ 2308 */
233 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_1,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530234 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai203d65b2010-06-17 13:37:59 +0530235 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_2,
236 PCI_ANY_ID, PCI_ANY_ID },
237 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SAS2308_3,
Kashyap, Desaidb271362009-09-23 17:24:27 +0530238 PCI_ANY_ID, PCI_ANY_ID },
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +0530239 /* SSS6200 */
240 { MPI2_MFGPAGE_VENDORID_LSI, MPI2_MFGPAGE_DEVID_SSS6200,
241 PCI_ANY_ID, PCI_ANY_ID },
Eric Moore635374e2009-03-09 01:21:12 -0600242 {0} /* Terminating entry */
243};
244MODULE_DEVICE_TABLE(pci, scsih_pci_table);
245
246/**
Eric Moored5d135b2009-05-18 13:02:08 -0600247 * _scsih_set_debug_level - global setting of ioc->logging_level.
Eric Moore635374e2009-03-09 01:21:12 -0600248 *
249 * Note: The logging levels are defined in mpt2sas_debug.h.
250 */
251static int
Eric Moored5d135b2009-05-18 13:02:08 -0600252_scsih_set_debug_level(const char *val, struct kernel_param *kp)
Eric Moore635374e2009-03-09 01:21:12 -0600253{
254 int ret = param_set_int(val, kp);
255 struct MPT2SAS_ADAPTER *ioc;
256
257 if (ret)
258 return ret;
259
260 printk(KERN_INFO "setting logging_level(0x%08x)\n", logging_level);
Eric Mooreba33fad2009-03-15 21:37:18 -0600261 list_for_each_entry(ioc, &mpt2sas_ioc_list, list)
Eric Moore635374e2009-03-09 01:21:12 -0600262 ioc->logging_level = logging_level;
263 return 0;
264}
Eric Moored5d135b2009-05-18 13:02:08 -0600265module_param_call(logging_level, _scsih_set_debug_level, param_get_int,
Eric Moore635374e2009-03-09 01:21:12 -0600266 &logging_level, 0644);
267
268/**
269 * _scsih_srch_boot_sas_address - search based on sas_address
270 * @sas_address: sas address
271 * @boot_device: boot device object from bios page 2
272 *
273 * Returns 1 when there's a match, 0 means no match.
274 */
275static inline int
276_scsih_srch_boot_sas_address(u64 sas_address,
277 Mpi2BootDeviceSasWwid_t *boot_device)
278{
279 return (sas_address == le64_to_cpu(boot_device->SASAddress)) ? 1 : 0;
280}
281
282/**
283 * _scsih_srch_boot_device_name - search based on device name
284 * @device_name: device name specified in INDENTIFY fram
285 * @boot_device: boot device object from bios page 2
286 *
287 * Returns 1 when there's a match, 0 means no match.
288 */
289static inline int
290_scsih_srch_boot_device_name(u64 device_name,
291 Mpi2BootDeviceDeviceName_t *boot_device)
292{
293 return (device_name == le64_to_cpu(boot_device->DeviceName)) ? 1 : 0;
294}
295
296/**
297 * _scsih_srch_boot_encl_slot - search based on enclosure_logical_id/slot
298 * @enclosure_logical_id: enclosure logical id
299 * @slot_number: slot number
300 * @boot_device: boot device object from bios page 2
301 *
302 * Returns 1 when there's a match, 0 means no match.
303 */
304static inline int
305_scsih_srch_boot_encl_slot(u64 enclosure_logical_id, u16 slot_number,
306 Mpi2BootDeviceEnclosureSlot_t *boot_device)
307{
308 return (enclosure_logical_id == le64_to_cpu(boot_device->
309 EnclosureLogicalID) && slot_number == le16_to_cpu(boot_device->
310 SlotNumber)) ? 1 : 0;
311}
312
313/**
314 * _scsih_is_boot_device - search for matching boot device.
315 * @sas_address: sas address
316 * @device_name: device name specified in INDENTIFY fram
317 * @enclosure_logical_id: enclosure logical id
318 * @slot_number: slot number
319 * @form: specifies boot device form
320 * @boot_device: boot device object from bios page 2
321 *
322 * Returns 1 when there's a match, 0 means no match.
323 */
324static int
325_scsih_is_boot_device(u64 sas_address, u64 device_name,
326 u64 enclosure_logical_id, u16 slot, u8 form,
327 Mpi2BiosPage2BootDevice_t *boot_device)
328{
329 int rc = 0;
330
331 switch (form) {
332 case MPI2_BIOSPAGE2_FORM_SAS_WWID:
333 if (!sas_address)
334 break;
335 rc = _scsih_srch_boot_sas_address(
336 sas_address, &boot_device->SasWwid);
337 break;
338 case MPI2_BIOSPAGE2_FORM_ENCLOSURE_SLOT:
339 if (!enclosure_logical_id)
340 break;
341 rc = _scsih_srch_boot_encl_slot(
342 enclosure_logical_id,
343 slot, &boot_device->EnclosureSlot);
344 break;
345 case MPI2_BIOSPAGE2_FORM_DEVICE_NAME:
346 if (!device_name)
347 break;
348 rc = _scsih_srch_boot_device_name(
349 device_name, &boot_device->DeviceName);
350 break;
351 case MPI2_BIOSPAGE2_FORM_NO_DEVICE_SPECIFIED:
352 break;
353 }
354
355 return rc;
356}
357
358/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530359 * _scsih_get_sas_address - set the sas_address for given device handle
360 * @handle: device handle
361 * @sas_address: sas address
362 *
363 * Returns 0 success, non-zero when failure
364 */
365static int
366_scsih_get_sas_address(struct MPT2SAS_ADAPTER *ioc, u16 handle,
367 u64 *sas_address)
368{
369 Mpi2SasDevicePage0_t sas_device_pg0;
370 Mpi2ConfigReply_t mpi_reply;
371 u32 ioc_status;
372
373 if (handle <= ioc->sas_hba.num_phys) {
374 *sas_address = ioc->sas_hba.sas_address;
375 return 0;
376 } else
377 *sas_address = 0;
378
379 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
380 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
381 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
382 ioc->name, __FILE__, __LINE__, __func__);
383 return -ENXIO;
384 }
385
386 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
387 MPI2_IOCSTATUS_MASK;
388 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
389 printk(MPT2SAS_ERR_FMT "handle(0x%04x), ioc_status(0x%04x)"
390 "\nfailure at %s:%d/%s()!\n", ioc->name, handle, ioc_status,
391 __FILE__, __LINE__, __func__);
392 return -EIO;
393 }
394
395 *sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
396 return 0;
397}
398
399/**
Eric Moore635374e2009-03-09 01:21:12 -0600400 * _scsih_determine_boot_device - determine boot device.
401 * @ioc: per adapter object
402 * @device: either sas_device or raid_device object
403 * @is_raid: [flag] 1 = raid object, 0 = sas object
404 *
405 * Determines whether this device should be first reported device to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300406 * to scsi-ml or sas transport, this purpose is for persistent boot device.
Eric Moore635374e2009-03-09 01:21:12 -0600407 * There are primary, alternate, and current entries in bios page 2. The order
408 * priority is primary, alternate, then current. This routine saves
409 * the corresponding device object and is_raid flag in the ioc object.
410 * The saved data to be used later in _scsih_probe_boot_devices().
411 */
412static void
413_scsih_determine_boot_device(struct MPT2SAS_ADAPTER *ioc,
414 void *device, u8 is_raid)
415{
416 struct _sas_device *sas_device;
417 struct _raid_device *raid_device;
418 u64 sas_address;
419 u64 device_name;
420 u64 enclosure_logical_id;
421 u16 slot;
422
423 /* only process this function when driver loads */
424 if (!ioc->wait_for_port_enable_to_complete)
425 return;
426
427 if (!is_raid) {
428 sas_device = device;
429 sas_address = sas_device->sas_address;
430 device_name = sas_device->device_name;
431 enclosure_logical_id = sas_device->enclosure_logical_id;
432 slot = sas_device->slot;
433 } else {
434 raid_device = device;
435 sas_address = raid_device->wwid;
436 device_name = 0;
437 enclosure_logical_id = 0;
438 slot = 0;
439 }
440
441 if (!ioc->req_boot_device.device) {
442 if (_scsih_is_boot_device(sas_address, device_name,
443 enclosure_logical_id, slot,
444 (ioc->bios_pg2.ReqBootDeviceForm &
445 MPI2_BIOSPAGE2_FORM_MASK),
446 &ioc->bios_pg2.RequestedBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530447 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600448 "%s: req_boot_device(0x%016llx)\n",
449 ioc->name, __func__,
450 (unsigned long long)sas_address));
451 ioc->req_boot_device.device = device;
452 ioc->req_boot_device.is_raid = is_raid;
453 }
454 }
455
456 if (!ioc->req_alt_boot_device.device) {
457 if (_scsih_is_boot_device(sas_address, device_name,
458 enclosure_logical_id, slot,
459 (ioc->bios_pg2.ReqAltBootDeviceForm &
460 MPI2_BIOSPAGE2_FORM_MASK),
461 &ioc->bios_pg2.RequestedAltBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530462 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600463 "%s: req_alt_boot_device(0x%016llx)\n",
464 ioc->name, __func__,
465 (unsigned long long)sas_address));
466 ioc->req_alt_boot_device.device = device;
467 ioc->req_alt_boot_device.is_raid = is_raid;
468 }
469 }
470
471 if (!ioc->current_boot_device.device) {
472 if (_scsih_is_boot_device(sas_address, device_name,
473 enclosure_logical_id, slot,
474 (ioc->bios_pg2.CurrentBootDeviceForm &
475 MPI2_BIOSPAGE2_FORM_MASK),
476 &ioc->bios_pg2.CurrentBootDevice)) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530477 dinitprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -0600478 "%s: current_boot_device(0x%016llx)\n",
479 ioc->name, __func__,
480 (unsigned long long)sas_address));
481 ioc->current_boot_device.device = device;
482 ioc->current_boot_device.is_raid = is_raid;
483 }
484 }
485}
486
487/**
488 * mpt2sas_scsih_sas_device_find_by_sas_address - sas device search
489 * @ioc: per adapter object
490 * @sas_address: sas address
491 * Context: Calling function should acquire ioc->sas_device_lock
492 *
493 * This searches for sas_device based on sas_address, then return sas_device
494 * object.
495 */
496struct _sas_device *
497mpt2sas_scsih_sas_device_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
498 u64 sas_address)
499{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530500 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600501
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530502 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
503 if (sas_device->sas_address == sas_address)
504 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600505
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530506 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
507 if (sas_device->sas_address == sas_address)
508 return sas_device;
509
510 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600511}
512
513/**
514 * _scsih_sas_device_find_by_handle - sas device search
515 * @ioc: per adapter object
516 * @handle: sas device handle (assigned by firmware)
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 */
522static struct _sas_device *
523_scsih_sas_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
524{
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530525 struct _sas_device *sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600526
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530527 list_for_each_entry(sas_device, &ioc->sas_device_list, list)
528 if (sas_device->handle == handle)
529 return sas_device;
Eric Moore635374e2009-03-09 01:21:12 -0600530
Kashyap, Desaicd9843f2010-03-09 16:32:17 +0530531 list_for_each_entry(sas_device, &ioc->sas_device_init_list, list)
532 if (sas_device->handle == handle)
533 return sas_device;
534
535 return NULL;
Eric Moore635374e2009-03-09 01:21:12 -0600536}
537
538/**
539 * _scsih_sas_device_remove - remove sas_device from list.
540 * @ioc: per adapter object
541 * @sas_device: the sas_device object
542 * Context: This function will acquire ioc->sas_device_lock.
543 *
544 * Removing object and freeing associated memory from the ioc->sas_device_list.
545 */
546static void
547_scsih_sas_device_remove(struct MPT2SAS_ADAPTER *ioc,
548 struct _sas_device *sas_device)
549{
550 unsigned long flags;
551
Kashyap, Desai980ead32010-04-08 17:55:22 +0530552 if (!sas_device)
553 return;
554
Eric Moore635374e2009-03-09 01:21:12 -0600555 spin_lock_irqsave(&ioc->sas_device_lock, flags);
Kashyap, Desai980ead32010-04-08 17:55:22 +0530556 if (mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
557 sas_device->sas_address)) {
558 list_del(&sas_device->list);
559 kfree(sas_device);
560 }
Eric Moore635374e2009-03-09 01:21:12 -0600561 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
562}
563
564/**
565 * _scsih_sas_device_add - insert sas_device to the 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 * Adding new object to the ioc->sas_device_list.
571 */
572static void
573_scsih_sas_device_add(struct MPT2SAS_ADAPTER *ioc,
574 struct _sas_device *sas_device)
575{
576 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600577
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530578 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600579 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
580 sas_device->handle, (unsigned long long)sas_device->sas_address));
581
582 spin_lock_irqsave(&ioc->sas_device_lock, flags);
583 list_add_tail(&sas_device->list, &ioc->sas_device_list);
584 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
585
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530586 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
587 sas_device->sas_address_parent))
Eric Moore635374e2009-03-09 01:21:12 -0600588 _scsih_sas_device_remove(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -0600589}
590
591/**
592 * _scsih_sas_device_init_add - insert sas_device to the list.
593 * @ioc: per adapter object
594 * @sas_device: the sas_device object
595 * Context: This function will acquire ioc->sas_device_lock.
596 *
597 * Adding new object at driver load time to the ioc->sas_device_init_list.
598 */
599static void
600_scsih_sas_device_init_add(struct MPT2SAS_ADAPTER *ioc,
601 struct _sas_device *sas_device)
602{
603 unsigned long flags;
604
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530605 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600606 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
607 sas_device->handle, (unsigned long long)sas_device->sas_address));
608
609 spin_lock_irqsave(&ioc->sas_device_lock, flags);
610 list_add_tail(&sas_device->list, &ioc->sas_device_init_list);
611 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
612 _scsih_determine_boot_device(ioc, sas_device, 0);
613}
614
615/**
Eric Moore635374e2009-03-09 01:21:12 -0600616 * _scsih_raid_device_find_by_id - raid device search
617 * @ioc: per adapter object
618 * @id: sas device target id
619 * @channel: sas device channel
620 * Context: Calling function should acquire ioc->raid_device_lock
621 *
622 * This searches for raid_device based on target id, then return raid_device
623 * object.
624 */
625static struct _raid_device *
626_scsih_raid_device_find_by_id(struct MPT2SAS_ADAPTER *ioc, int id, int channel)
627{
628 struct _raid_device *raid_device, *r;
629
630 r = NULL;
631 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
632 if (raid_device->id == id && raid_device->channel == channel) {
633 r = raid_device;
634 goto out;
635 }
636 }
637
638 out:
639 return r;
640}
641
642/**
643 * _scsih_raid_device_find_by_handle - raid device search
644 * @ioc: per adapter object
645 * @handle: sas device handle (assigned by firmware)
646 * Context: Calling function should acquire ioc->raid_device_lock
647 *
648 * This searches for raid_device based on handle, then return raid_device
649 * object.
650 */
651static struct _raid_device *
652_scsih_raid_device_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
653{
654 struct _raid_device *raid_device, *r;
655
656 r = NULL;
657 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
658 if (raid_device->handle != handle)
659 continue;
660 r = raid_device;
661 goto out;
662 }
663
664 out:
665 return r;
666}
667
668/**
669 * _scsih_raid_device_find_by_wwid - raid device search
670 * @ioc: per adapter object
671 * @handle: sas device handle (assigned by firmware)
672 * Context: Calling function should acquire ioc->raid_device_lock
673 *
674 * This searches for raid_device based on wwid, then return raid_device
675 * object.
676 */
677static struct _raid_device *
678_scsih_raid_device_find_by_wwid(struct MPT2SAS_ADAPTER *ioc, u64 wwid)
679{
680 struct _raid_device *raid_device, *r;
681
682 r = NULL;
683 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
684 if (raid_device->wwid != wwid)
685 continue;
686 r = raid_device;
687 goto out;
688 }
689
690 out:
691 return r;
692}
693
694/**
695 * _scsih_raid_device_add - add raid_device object
696 * @ioc: per adapter object
697 * @raid_device: raid_device object
698 *
699 * This is added to the raid_device_list link list.
700 */
701static void
702_scsih_raid_device_add(struct MPT2SAS_ADAPTER *ioc,
703 struct _raid_device *raid_device)
704{
705 unsigned long flags;
706
Kashyap, Desaieabb08a2010-06-17 13:43:57 +0530707 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle"
Eric Moore635374e2009-03-09 01:21:12 -0600708 "(0x%04x), wwid(0x%016llx)\n", ioc->name, __func__,
709 raid_device->handle, (unsigned long long)raid_device->wwid));
710
711 spin_lock_irqsave(&ioc->raid_device_lock, flags);
712 list_add_tail(&raid_device->list, &ioc->raid_device_list);
713 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
714}
715
716/**
717 * _scsih_raid_device_remove - delete raid_device object
718 * @ioc: per adapter object
719 * @raid_device: raid_device object
720 *
721 * This is removed from the raid_device_list link list.
722 */
723static void
724_scsih_raid_device_remove(struct MPT2SAS_ADAPTER *ioc,
725 struct _raid_device *raid_device)
726{
727 unsigned long flags;
728
729 spin_lock_irqsave(&ioc->raid_device_lock, flags);
730 list_del(&raid_device->list);
731 memset(raid_device, 0, sizeof(struct _raid_device));
732 kfree(raid_device);
733 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
734}
735
736/**
Kashyap, Desaic5e039b2009-09-23 17:21:29 +0530737 * mpt2sas_scsih_expander_find_by_handle - expander device search
738 * @ioc: per adapter object
739 * @handle: expander handle (assigned by firmware)
740 * Context: Calling function should acquire ioc->sas_device_lock
741 *
742 * This searches for expander device based on handle, then returns the
743 * sas_node object.
744 */
745struct _sas_node *
746mpt2sas_scsih_expander_find_by_handle(struct MPT2SAS_ADAPTER *ioc, u16 handle)
747{
748 struct _sas_node *sas_expander, *r;
749
750 r = NULL;
751 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
752 if (sas_expander->handle != handle)
753 continue;
754 r = sas_expander;
755 goto out;
756 }
757 out:
758 return r;
759}
760
761/**
Eric Moore635374e2009-03-09 01:21:12 -0600762 * mpt2sas_scsih_expander_find_by_sas_address - expander device search
763 * @ioc: per adapter object
764 * @sas_address: sas address
765 * Context: Calling function should acquire ioc->sas_node_lock.
766 *
767 * This searches for expander device based on sas_address, then returns the
768 * sas_node object.
769 */
770struct _sas_node *
771mpt2sas_scsih_expander_find_by_sas_address(struct MPT2SAS_ADAPTER *ioc,
772 u64 sas_address)
773{
774 struct _sas_node *sas_expander, *r;
775
776 r = NULL;
777 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
778 if (sas_expander->sas_address != sas_address)
779 continue;
780 r = sas_expander;
781 goto out;
782 }
783 out:
784 return r;
785}
786
787/**
788 * _scsih_expander_node_add - insert expander device to the list.
789 * @ioc: per adapter object
790 * @sas_expander: the sas_device object
791 * Context: This function will acquire ioc->sas_node_lock.
792 *
793 * Adding new object to the ioc->sas_expander_list.
794 *
795 * Return nothing.
796 */
797static void
798_scsih_expander_node_add(struct MPT2SAS_ADAPTER *ioc,
799 struct _sas_node *sas_expander)
800{
801 unsigned long flags;
802
803 spin_lock_irqsave(&ioc->sas_node_lock, flags);
804 list_add_tail(&sas_expander->list, &ioc->sas_expander_list);
805 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
806}
807
808/**
809 * _scsih_is_end_device - determines if device is an end device
810 * @device_info: bitfield providing information about the device.
811 * Context: none
812 *
813 * Returns 1 if end device.
814 */
815static int
816_scsih_is_end_device(u32 device_info)
817{
818 if (device_info & MPI2_SAS_DEVICE_INFO_END_DEVICE &&
819 ((device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) |
820 (device_info & MPI2_SAS_DEVICE_INFO_STP_TARGET) |
821 (device_info & MPI2_SAS_DEVICE_INFO_SATA_DEVICE)))
822 return 1;
823 else
824 return 0;
825}
826
827/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530828 * _scsih_scsi_lookup_get - returns scmd entry
Eric Moore635374e2009-03-09 01:21:12 -0600829 * @ioc: per adapter object
830 * @smid: system request message index
Eric Moore635374e2009-03-09 01:21:12 -0600831 *
832 * Returns the smid stored scmd pointer.
833 */
834static struct scsi_cmnd *
835_scsih_scsi_lookup_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
836{
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530837 return ioc->scsi_lookup[smid - 1].scmd;
Eric Moore635374e2009-03-09 01:21:12 -0600838}
839
840/**
Kashyap, Desaiec07a052011-01-05 17:54:32 +0530841 * _scsih_scsi_lookup_get_clear - returns scmd entry
842 * @ioc: per adapter object
843 * @smid: system request message index
844 *
845 * Returns the smid stored scmd pointer.
846 * Then will derefrence the stored scmd pointer.
847 */
848static inline struct scsi_cmnd *
849_scsih_scsi_lookup_get_clear(struct MPT2SAS_ADAPTER *ioc, u16 smid)
850{
851 unsigned long flags;
852 struct scsi_cmnd *scmd;
853
854 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
855 scmd = ioc->scsi_lookup[smid - 1].scmd;
856 ioc->scsi_lookup[smid - 1].scmd = NULL;
857 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
858
859 return scmd;
860}
861
862/**
Eric Moore635374e2009-03-09 01:21:12 -0600863 * _scsih_scsi_lookup_find_by_scmd - scmd lookup
864 * @ioc: per adapter object
865 * @smid: system request message index
866 * @scmd: pointer to scsi command object
867 * Context: This function will acquire ioc->scsi_lookup_lock.
868 *
869 * This will search for a scmd pointer in the scsi_lookup array,
870 * returning the revelent smid. A returned value of zero means invalid.
871 */
872static u16
873_scsih_scsi_lookup_find_by_scmd(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd
874 *scmd)
875{
876 u16 smid;
877 unsigned long flags;
878 int i;
879
880 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
881 smid = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530882 for (i = 0; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600883 if (ioc->scsi_lookup[i].scmd == scmd) {
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530884 smid = ioc->scsi_lookup[i].smid;
Eric Moore635374e2009-03-09 01:21:12 -0600885 goto out;
886 }
887 }
888 out:
889 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
890 return smid;
891}
892
893/**
894 * _scsih_scsi_lookup_find_by_target - search for matching channel:id
895 * @ioc: per adapter object
896 * @id: target id
897 * @channel: channel
898 * Context: This function will acquire ioc->scsi_lookup_lock.
899 *
900 * This will search for a matching channel:id in the scsi_lookup array,
901 * returning 1 if found.
902 */
903static u8
904_scsih_scsi_lookup_find_by_target(struct MPT2SAS_ADAPTER *ioc, int id,
905 int channel)
906{
907 u8 found;
908 unsigned long flags;
909 int i;
910
911 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
912 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530913 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore635374e2009-03-09 01:21:12 -0600914 if (ioc->scsi_lookup[i].scmd &&
915 (ioc->scsi_lookup[i].scmd->device->id == id &&
916 ioc->scsi_lookup[i].scmd->device->channel == channel)) {
917 found = 1;
918 goto out;
919 }
920 }
921 out:
922 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
923 return found;
924}
925
926/**
Eric Moore993e0da2009-05-18 13:00:45 -0600927 * _scsih_scsi_lookup_find_by_lun - search for matching channel:id:lun
928 * @ioc: per adapter object
929 * @id: target id
930 * @lun: lun number
931 * @channel: channel
932 * Context: This function will acquire ioc->scsi_lookup_lock.
933 *
934 * This will search for a matching channel:id:lun in the scsi_lookup array,
935 * returning 1 if found.
936 */
937static u8
938_scsih_scsi_lookup_find_by_lun(struct MPT2SAS_ADAPTER *ioc, int id,
939 unsigned int lun, int channel)
940{
941 u8 found;
942 unsigned long flags;
943 int i;
944
945 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
946 found = 0;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +0530947 for (i = 0 ; i < ioc->scsiio_depth; i++) {
Eric Moore993e0da2009-05-18 13:00:45 -0600948 if (ioc->scsi_lookup[i].scmd &&
949 (ioc->scsi_lookup[i].scmd->device->id == id &&
950 ioc->scsi_lookup[i].scmd->device->channel == channel &&
951 ioc->scsi_lookup[i].scmd->device->lun == lun)) {
952 found = 1;
953 goto out;
954 }
955 }
956 out:
957 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
958 return found;
959}
960
961/**
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530962 * _scsih_get_chain_buffer_tracker - obtain chain tracker
Eric Moore635374e2009-03-09 01:21:12 -0600963 * @ioc: per adapter object
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530964 * @smid: smid associated to an IO request
Eric Moore635374e2009-03-09 01:21:12 -0600965 *
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530966 * Returns chain tracker(from ioc->free_chain_list)
Eric Moore635374e2009-03-09 01:21:12 -0600967 */
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530968static struct chain_tracker *
969_scsih_get_chain_buffer_tracker(struct MPT2SAS_ADAPTER *ioc, u16 smid)
Eric Moore635374e2009-03-09 01:21:12 -0600970{
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530971 struct chain_tracker *chain_req;
972 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -0600973
Kashyap, Desai35f805b2010-11-13 04:34:06 +0530974 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
975 if (list_empty(&ioc->free_chain_list)) {
976 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
977 printk(MPT2SAS_WARN_FMT "chain buffers not available\n",
978 ioc->name);
979 return NULL;
980 }
981 chain_req = list_entry(ioc->free_chain_list.next,
982 struct chain_tracker, tracker_list);
983 list_del_init(&chain_req->tracker_list);
984 list_add_tail(&chain_req->tracker_list,
985 &ioc->scsi_lookup[smid - 1].chain_list);
986 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
987 return chain_req;
Eric Moore635374e2009-03-09 01:21:12 -0600988}
989
990/**
991 * _scsih_build_scatter_gather - main sg creation routine
992 * @ioc: per adapter object
993 * @scmd: scsi command
994 * @smid: system request message index
995 * Context: none.
996 *
997 * The main routine that builds scatter gather table from a given
998 * scsi request sent via the .queuecommand main handler.
999 *
1000 * Returns 0 success, anything else error
1001 */
1002static int
1003_scsih_build_scatter_gather(struct MPT2SAS_ADAPTER *ioc,
1004 struct scsi_cmnd *scmd, u16 smid)
1005{
1006 Mpi2SCSIIORequest_t *mpi_request;
1007 dma_addr_t chain_dma;
1008 struct scatterlist *sg_scmd;
1009 void *sg_local, *chain;
1010 u32 chain_offset;
1011 u32 chain_length;
1012 u32 chain_flags;
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001013 int sges_left;
Eric Moore635374e2009-03-09 01:21:12 -06001014 u32 sges_in_segment;
1015 u32 sgl_flags;
1016 u32 sgl_flags_last_element;
1017 u32 sgl_flags_end_buffer;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301018 struct chain_tracker *chain_req;
Eric Moore635374e2009-03-09 01:21:12 -06001019
1020 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
1021
1022 /* init scatter gather flags */
1023 sgl_flags = MPI2_SGE_FLAGS_SIMPLE_ELEMENT;
1024 if (scmd->sc_data_direction == DMA_TO_DEVICE)
1025 sgl_flags |= MPI2_SGE_FLAGS_HOST_TO_IOC;
1026 sgl_flags_last_element = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT)
1027 << MPI2_SGE_FLAGS_SHIFT;
1028 sgl_flags_end_buffer = (sgl_flags | MPI2_SGE_FLAGS_LAST_ELEMENT |
1029 MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST)
1030 << MPI2_SGE_FLAGS_SHIFT;
1031 sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
1032
1033 sg_scmd = scsi_sglist(scmd);
1034 sges_left = scsi_dma_map(scmd);
FUJITA Tomonoribb789d02010-03-09 11:09:50 +09001035 if (sges_left < 0) {
Eric Moore635374e2009-03-09 01:21:12 -06001036 sdev_printk(KERN_ERR, scmd->device, "pci_map_sg"
1037 " failed: request for %d bytes!\n", scsi_bufflen(scmd));
1038 return -ENOMEM;
1039 }
1040
1041 sg_local = &mpi_request->SGL;
1042 sges_in_segment = ioc->max_sges_in_main_message;
1043 if (sges_left <= sges_in_segment)
1044 goto fill_in_last_segment;
1045
1046 mpi_request->ChainOffset = (offsetof(Mpi2SCSIIORequest_t, SGL) +
1047 (sges_in_segment * ioc->sge_size))/4;
1048
1049 /* fill in main message segment when there is a chain following */
1050 while (sges_in_segment) {
1051 if (sges_in_segment == 1)
1052 ioc->base_add_sg_single(sg_local,
1053 sgl_flags_last_element | sg_dma_len(sg_scmd),
1054 sg_dma_address(sg_scmd));
1055 else
1056 ioc->base_add_sg_single(sg_local, sgl_flags |
1057 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1058 sg_scmd = sg_next(sg_scmd);
1059 sg_local += ioc->sge_size;
1060 sges_left--;
1061 sges_in_segment--;
1062 }
1063
1064 /* initializing the chain flags and pointers */
1065 chain_flags = MPI2_SGE_FLAGS_CHAIN_ELEMENT << MPI2_SGE_FLAGS_SHIFT;
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301066 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1067 if (!chain_req)
1068 return -1;
1069 chain = chain_req->chain_buffer;
1070 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001071 do {
1072 sges_in_segment = (sges_left <=
1073 ioc->max_sges_in_chain_message) ? sges_left :
1074 ioc->max_sges_in_chain_message;
1075 chain_offset = (sges_left == sges_in_segment) ?
1076 0 : (sges_in_segment * ioc->sge_size)/4;
1077 chain_length = sges_in_segment * ioc->sge_size;
1078 if (chain_offset) {
1079 chain_offset = chain_offset <<
1080 MPI2_SGE_CHAIN_OFFSET_SHIFT;
1081 chain_length += ioc->sge_size;
1082 }
1083 ioc->base_add_sg_single(sg_local, chain_flags | chain_offset |
1084 chain_length, chain_dma);
1085 sg_local = chain;
1086 if (!chain_offset)
1087 goto fill_in_last_segment;
1088
1089 /* fill in chain segments */
1090 while (sges_in_segment) {
1091 if (sges_in_segment == 1)
1092 ioc->base_add_sg_single(sg_local,
1093 sgl_flags_last_element |
1094 sg_dma_len(sg_scmd),
1095 sg_dma_address(sg_scmd));
1096 else
1097 ioc->base_add_sg_single(sg_local, sgl_flags |
1098 sg_dma_len(sg_scmd),
1099 sg_dma_address(sg_scmd));
1100 sg_scmd = sg_next(sg_scmd);
1101 sg_local += ioc->sge_size;
1102 sges_left--;
1103 sges_in_segment--;
1104 }
1105
Kashyap, Desai35f805b2010-11-13 04:34:06 +05301106 chain_req = _scsih_get_chain_buffer_tracker(ioc, smid);
1107 if (!chain_req)
1108 return -1;
1109 chain = chain_req->chain_buffer;
1110 chain_dma = chain_req->chain_buffer_dma;
Eric Moore635374e2009-03-09 01:21:12 -06001111 } while (1);
1112
1113
1114 fill_in_last_segment:
1115
1116 /* fill the last segment */
1117 while (sges_left) {
1118 if (sges_left == 1)
1119 ioc->base_add_sg_single(sg_local, sgl_flags_end_buffer |
1120 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1121 else
1122 ioc->base_add_sg_single(sg_local, sgl_flags |
1123 sg_dma_len(sg_scmd), sg_dma_address(sg_scmd));
1124 sg_scmd = sg_next(sg_scmd);
1125 sg_local += ioc->sge_size;
1126 sges_left--;
1127 }
1128
1129 return 0;
1130}
1131
1132/**
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301133 * _scsih_adjust_queue_depth - setting device queue depth
Eric Moore635374e2009-03-09 01:21:12 -06001134 * @sdev: scsi device struct
1135 * @qdepth: requested queue depth
1136 *
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301137 *
1138 * Returns nothing
Eric Moore635374e2009-03-09 01:21:12 -06001139 */
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301140static void
1141_scsih_adjust_queue_depth(struct scsi_device *sdev, int qdepth)
Eric Moore635374e2009-03-09 01:21:12 -06001142{
1143 struct Scsi_Host *shost = sdev->host;
1144 int max_depth;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301145 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1146 struct MPT2SAS_DEVICE *sas_device_priv_data;
1147 struct MPT2SAS_TARGET *sas_target_priv_data;
1148 struct _sas_device *sas_device;
1149 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06001150
1151 max_depth = shost->can_queue;
Kashyap, Desaie0077d62009-09-23 17:30:22 +05301152
1153 /* limit max device queue for SATA to 32 */
1154 sas_device_priv_data = sdev->hostdata;
1155 if (!sas_device_priv_data)
1156 goto not_sata;
1157 sas_target_priv_data = sas_device_priv_data->sas_target;
1158 if (!sas_target_priv_data)
1159 goto not_sata;
1160 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))
1161 goto not_sata;
1162 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1163 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1164 sas_device_priv_data->sas_target->sas_address);
1165 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1166 if (sas_device && sas_device->device_info &
1167 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1168 max_depth = MPT2SAS_SATA_QUEUE_DEPTH;
1169
1170 not_sata:
1171
Eric Moore635374e2009-03-09 01:21:12 -06001172 if (!sdev->tagged_supported)
1173 max_depth = 1;
1174 if (qdepth > max_depth)
1175 qdepth = max_depth;
Kashyap, Desaia93c6b42010-11-13 04:39:11 +05301176 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
1177}
1178
1179/**
1180 * _scsih_change_queue_depth - setting device queue depth
1181 * @sdev: scsi device struct
1182 * @qdepth: requested queue depth
1183 * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
1184 * (see include/scsi/scsi_host.h for definition)
1185 *
1186 * Returns queue depth.
1187 */
1188static int
1189_scsih_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
1190{
1191 if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP)
1192 _scsih_adjust_queue_depth(sdev, qdepth);
1193 else if (reason == SCSI_QDEPTH_QFULL)
1194 scsi_track_queue_full(sdev, qdepth);
1195 else
1196 return -EOPNOTSUPP;
Eric Moore635374e2009-03-09 01:21:12 -06001197
1198 if (sdev->inquiry_len > 7)
1199 sdev_printk(KERN_INFO, sdev, "qdepth(%d), tagged(%d), "
1200 "simple(%d), ordered(%d), scsi_level(%d), cmd_que(%d)\n",
1201 sdev->queue_depth, sdev->tagged_supported, sdev->simple_tags,
1202 sdev->ordered_tags, sdev->scsi_level,
1203 (sdev->inquiry[7] & 2) >> 1);
1204
1205 return sdev->queue_depth;
1206}
1207
1208/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05301209 * _scsih_change_queue_type - changing device queue tag type
Eric Moore635374e2009-03-09 01:21:12 -06001210 * @sdev: scsi device struct
1211 * @tag_type: requested tag type
1212 *
1213 * Returns queue tag type.
1214 */
1215static int
Eric Moored5d135b2009-05-18 13:02:08 -06001216_scsih_change_queue_type(struct scsi_device *sdev, int tag_type)
Eric Moore635374e2009-03-09 01:21:12 -06001217{
1218 if (sdev->tagged_supported) {
1219 scsi_set_tag_type(sdev, tag_type);
1220 if (tag_type)
1221 scsi_activate_tcq(sdev, sdev->queue_depth);
1222 else
1223 scsi_deactivate_tcq(sdev, sdev->queue_depth);
1224 } else
1225 tag_type = 0;
1226
1227 return tag_type;
1228}
1229
1230/**
Eric Moored5d135b2009-05-18 13:02:08 -06001231 * _scsih_target_alloc - target add routine
Eric Moore635374e2009-03-09 01:21:12 -06001232 * @starget: scsi target struct
1233 *
1234 * Returns 0 if ok. Any other return is assumed to be an error and
1235 * the device is ignored.
1236 */
1237static int
Eric Moored5d135b2009-05-18 13:02:08 -06001238_scsih_target_alloc(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001239{
1240 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1241 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1242 struct MPT2SAS_TARGET *sas_target_priv_data;
1243 struct _sas_device *sas_device;
1244 struct _raid_device *raid_device;
1245 unsigned long flags;
1246 struct sas_rphy *rphy;
1247
1248 sas_target_priv_data = kzalloc(sizeof(struct scsi_target), GFP_KERNEL);
1249 if (!sas_target_priv_data)
1250 return -ENOMEM;
1251
1252 starget->hostdata = sas_target_priv_data;
1253 sas_target_priv_data->starget = starget;
1254 sas_target_priv_data->handle = MPT2SAS_INVALID_DEVICE_HANDLE;
1255
1256 /* RAID volumes */
1257 if (starget->channel == RAID_CHANNEL) {
1258 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1259 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1260 starget->channel);
1261 if (raid_device) {
1262 sas_target_priv_data->handle = raid_device->handle;
1263 sas_target_priv_data->sas_address = raid_device->wwid;
1264 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_VOLUME;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301265 sas_target_priv_data->raid_device = raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001266 raid_device->starget = starget;
1267 }
1268 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1269 return 0;
1270 }
1271
1272 /* sas/sata devices */
1273 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1274 rphy = dev_to_rphy(starget->dev.parent);
1275 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1276 rphy->identify.sas_address);
1277
1278 if (sas_device) {
1279 sas_target_priv_data->handle = sas_device->handle;
1280 sas_target_priv_data->sas_address = sas_device->sas_address;
1281 sas_device->starget = starget;
1282 sas_device->id = starget->id;
1283 sas_device->channel = starget->channel;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05301284 if (test_bit(sas_device->handle, ioc->pd_handles))
Eric Moore635374e2009-03-09 01:21:12 -06001285 sas_target_priv_data->flags |=
1286 MPT_TARGET_FLAGS_RAID_COMPONENT;
1287 }
1288 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1289
1290 return 0;
1291}
1292
1293/**
Eric Moored5d135b2009-05-18 13:02:08 -06001294 * _scsih_target_destroy - target destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001295 * @starget: scsi target struct
1296 *
1297 * Returns nothing.
1298 */
1299static void
Eric Moored5d135b2009-05-18 13:02:08 -06001300_scsih_target_destroy(struct scsi_target *starget)
Eric Moore635374e2009-03-09 01:21:12 -06001301{
1302 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1303 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1304 struct MPT2SAS_TARGET *sas_target_priv_data;
1305 struct _sas_device *sas_device;
1306 struct _raid_device *raid_device;
1307 unsigned long flags;
1308 struct sas_rphy *rphy;
1309
1310 sas_target_priv_data = starget->hostdata;
1311 if (!sas_target_priv_data)
1312 return;
1313
1314 if (starget->channel == RAID_CHANNEL) {
1315 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1316 raid_device = _scsih_raid_device_find_by_id(ioc, starget->id,
1317 starget->channel);
1318 if (raid_device) {
1319 raid_device->starget = NULL;
1320 raid_device->sdev = NULL;
1321 }
1322 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1323 goto out;
1324 }
1325
1326 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1327 rphy = dev_to_rphy(starget->dev.parent);
1328 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1329 rphy->identify.sas_address);
Eric Moore8901cbb2009-04-21 15:41:32 -06001330 if (sas_device && (sas_device->starget == starget) &&
1331 (sas_device->id == starget->id) &&
1332 (sas_device->channel == starget->channel))
Eric Moore635374e2009-03-09 01:21:12 -06001333 sas_device->starget = NULL;
1334
1335 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1336
1337 out:
1338 kfree(sas_target_priv_data);
1339 starget->hostdata = NULL;
1340}
1341
1342/**
Eric Moored5d135b2009-05-18 13:02:08 -06001343 * _scsih_slave_alloc - device add routine
Eric Moore635374e2009-03-09 01:21:12 -06001344 * @sdev: scsi device struct
1345 *
1346 * Returns 0 if ok. Any other return is assumed to be an error and
1347 * the device is ignored.
1348 */
1349static int
Eric Moored5d135b2009-05-18 13:02:08 -06001350_scsih_slave_alloc(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001351{
1352 struct Scsi_Host *shost;
1353 struct MPT2SAS_ADAPTER *ioc;
1354 struct MPT2SAS_TARGET *sas_target_priv_data;
1355 struct MPT2SAS_DEVICE *sas_device_priv_data;
1356 struct scsi_target *starget;
1357 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06001358 unsigned long flags;
1359
1360 sas_device_priv_data = kzalloc(sizeof(struct scsi_device), GFP_KERNEL);
1361 if (!sas_device_priv_data)
1362 return -ENOMEM;
1363
1364 sas_device_priv_data->lun = sdev->lun;
1365 sas_device_priv_data->flags = MPT_DEVICE_FLAGS_INIT;
1366
1367 starget = scsi_target(sdev);
1368 sas_target_priv_data = starget->hostdata;
1369 sas_target_priv_data->num_luns++;
1370 sas_device_priv_data->sas_target = sas_target_priv_data;
1371 sdev->hostdata = sas_device_priv_data;
1372 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT))
1373 sdev->no_uld_attach = 1;
1374
1375 shost = dev_to_shost(&starget->dev);
1376 ioc = shost_priv(shost);
1377 if (starget->channel == RAID_CHANNEL) {
1378 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1379 raid_device = _scsih_raid_device_find_by_id(ioc,
1380 starget->id, starget->channel);
1381 if (raid_device)
1382 raid_device->sdev = sdev; /* raid is single lun */
1383 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06001384 }
1385
Eric Moore635374e2009-03-09 01:21:12 -06001386 return 0;
1387}
1388
1389/**
Eric Moored5d135b2009-05-18 13:02:08 -06001390 * _scsih_slave_destroy - device destroy routine
Eric Moore635374e2009-03-09 01:21:12 -06001391 * @sdev: scsi device struct
1392 *
1393 * Returns nothing.
1394 */
1395static void
Eric Moored5d135b2009-05-18 13:02:08 -06001396_scsih_slave_destroy(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001397{
1398 struct MPT2SAS_TARGET *sas_target_priv_data;
1399 struct scsi_target *starget;
1400
1401 if (!sdev->hostdata)
1402 return;
1403
1404 starget = scsi_target(sdev);
1405 sas_target_priv_data = starget->hostdata;
1406 sas_target_priv_data->num_luns--;
1407 kfree(sdev->hostdata);
1408 sdev->hostdata = NULL;
1409}
1410
1411/**
Eric Moored5d135b2009-05-18 13:02:08 -06001412 * _scsih_display_sata_capabilities - sata capabilities
Eric Moore635374e2009-03-09 01:21:12 -06001413 * @ioc: per adapter object
1414 * @sas_device: the sas_device object
1415 * @sdev: scsi device struct
1416 */
1417static void
Eric Moored5d135b2009-05-18 13:02:08 -06001418_scsih_display_sata_capabilities(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06001419 struct _sas_device *sas_device, struct scsi_device *sdev)
1420{
1421 Mpi2ConfigReply_t mpi_reply;
1422 Mpi2SasDevicePage0_t sas_device_pg0;
1423 u32 ioc_status;
1424 u16 flags;
1425 u32 device_info;
1426
1427 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
1428 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, sas_device->handle))) {
1429 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1430 ioc->name, __FILE__, __LINE__, __func__);
1431 return;
1432 }
1433
1434 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1435 MPI2_IOCSTATUS_MASK;
1436 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
1437 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1438 ioc->name, __FILE__, __LINE__, __func__);
1439 return;
1440 }
1441
1442 flags = le16_to_cpu(sas_device_pg0.Flags);
Kashyap, Desaie94f6742010-03-17 16:24:52 +05301443 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
Eric Moore635374e2009-03-09 01:21:12 -06001444
1445 sdev_printk(KERN_INFO, sdev,
1446 "atapi(%s), ncq(%s), asyn_notify(%s), smart(%s), fua(%s), "
1447 "sw_preserve(%s)\n",
1448 (device_info & MPI2_SAS_DEVICE_INFO_ATAPI_DEVICE) ? "y" : "n",
1449 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_NCQ_SUPPORTED) ? "y" : "n",
1450 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_ASYNCHRONOUS_NOTIFY) ? "y" :
1451 "n",
1452 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SMART_SUPPORTED) ? "y" : "n",
1453 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_FUA_SUPPORTED) ? "y" : "n",
1454 (flags & MPI2_SAS_DEVICE0_FLAGS_SATA_SW_PRESERVE) ? "y" : "n");
1455}
1456
1457/**
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301458 * _scsih_is_raid - return boolean indicating device is raid volume
1459 * @dev the device struct object
1460 */
1461static int
1462_scsih_is_raid(struct device *dev)
1463{
1464 struct scsi_device *sdev = to_scsi_device(dev);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301465 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301466
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301467 if (ioc->is_warpdrive)
1468 return 0;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301469 return (sdev->channel == RAID_CHANNEL) ? 1 : 0;
1470}
1471
1472/**
1473 * _scsih_get_resync - get raid volume resync percent complete
1474 * @dev the device struct object
1475 */
1476static void
1477_scsih_get_resync(struct device *dev)
1478{
1479 struct scsi_device *sdev = to_scsi_device(dev);
1480 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1481 static struct _raid_device *raid_device;
1482 unsigned long flags;
1483 Mpi2RaidVolPage0_t vol_pg0;
1484 Mpi2ConfigReply_t mpi_reply;
1485 u32 volume_status_flags;
1486 u8 percent_complete = 0;
1487
1488 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1489 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1490 sdev->channel);
1491 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1492
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301493 if (!raid_device || ioc->is_warpdrive)
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301494 goto out;
1495
1496 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1497 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1498 sizeof(Mpi2RaidVolPage0_t))) {
1499 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1500 ioc->name, __FILE__, __LINE__, __func__);
1501 goto out;
1502 }
1503
1504 volume_status_flags = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1505 if (volume_status_flags & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS)
1506 percent_complete = raid_device->percent_complete;
1507 out:
1508 raid_set_resync(mpt2sas_raid_template, dev, percent_complete);
1509}
1510
1511/**
1512 * _scsih_get_state - get raid volume level
1513 * @dev the device struct object
1514 */
1515static void
1516_scsih_get_state(struct device *dev)
1517{
1518 struct scsi_device *sdev = to_scsi_device(dev);
1519 struct MPT2SAS_ADAPTER *ioc = shost_priv(sdev->host);
1520 static struct _raid_device *raid_device;
1521 unsigned long flags;
1522 Mpi2RaidVolPage0_t vol_pg0;
1523 Mpi2ConfigReply_t mpi_reply;
1524 u32 volstate;
1525 enum raid_state state = RAID_STATE_UNKNOWN;
1526
1527 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1528 raid_device = _scsih_raid_device_find_by_id(ioc, sdev->id,
1529 sdev->channel);
1530 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1531
1532 if (!raid_device)
1533 goto out;
1534
1535 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, &vol_pg0,
1536 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle,
1537 sizeof(Mpi2RaidVolPage0_t))) {
1538 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1539 ioc->name, __FILE__, __LINE__, __func__);
1540 goto out;
1541 }
1542
1543 volstate = le32_to_cpu(vol_pg0.VolumeStatusFlags);
1544 if (volstate & MPI2_RAIDVOL0_STATUS_FLAG_RESYNC_IN_PROGRESS) {
1545 state = RAID_STATE_RESYNCING;
1546 goto out;
1547 }
1548
1549 switch (vol_pg0.VolumeState) {
1550 case MPI2_RAID_VOL_STATE_OPTIMAL:
1551 case MPI2_RAID_VOL_STATE_ONLINE:
1552 state = RAID_STATE_ACTIVE;
1553 break;
1554 case MPI2_RAID_VOL_STATE_DEGRADED:
1555 state = RAID_STATE_DEGRADED;
1556 break;
1557 case MPI2_RAID_VOL_STATE_FAILED:
1558 case MPI2_RAID_VOL_STATE_MISSING:
1559 state = RAID_STATE_OFFLINE;
1560 break;
1561 }
1562 out:
1563 raid_set_state(mpt2sas_raid_template, dev, state);
1564}
1565
1566/**
1567 * _scsih_set_level - set raid level
1568 * @sdev: scsi device struct
1569 * @raid_device: raid_device object
1570 */
1571static void
1572_scsih_set_level(struct scsi_device *sdev, struct _raid_device *raid_device)
1573{
1574 enum raid_level level = RAID_LEVEL_UNKNOWN;
1575
1576 switch (raid_device->volume_type) {
1577 case MPI2_RAID_VOL_TYPE_RAID0:
1578 level = RAID_LEVEL_0;
1579 break;
1580 case MPI2_RAID_VOL_TYPE_RAID10:
1581 level = RAID_LEVEL_10;
1582 break;
1583 case MPI2_RAID_VOL_TYPE_RAID1E:
1584 level = RAID_LEVEL_1E;
1585 break;
1586 case MPI2_RAID_VOL_TYPE_RAID1:
1587 level = RAID_LEVEL_1;
1588 break;
1589 }
1590
1591 raid_set_level(mpt2sas_raid_template, &sdev->sdev_gendev, level);
1592}
1593
1594/**
Eric Moore635374e2009-03-09 01:21:12 -06001595 * _scsih_get_volume_capabilities - volume capabilities
1596 * @ioc: per adapter object
1597 * @sas_device: the raid_device object
1598 */
1599static void
1600_scsih_get_volume_capabilities(struct MPT2SAS_ADAPTER *ioc,
1601 struct _raid_device *raid_device)
1602{
1603 Mpi2RaidVolPage0_t *vol_pg0;
1604 Mpi2RaidPhysDiskPage0_t pd_pg0;
1605 Mpi2SasDevicePage0_t sas_device_pg0;
1606 Mpi2ConfigReply_t mpi_reply;
1607 u16 sz;
1608 u8 num_pds;
1609
1610 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1611 &num_pds)) || !num_pds) {
1612 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1613 ioc->name, __FILE__, __LINE__, __func__);
1614 return;
1615 }
1616
1617 raid_device->num_pds = num_pds;
1618 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1619 sizeof(Mpi2RaidVol0PhysDisk_t));
1620 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1621 if (!vol_pg0) {
1622 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1623 ioc->name, __FILE__, __LINE__, __func__);
1624 return;
1625 }
1626
1627 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1628 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1629 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1630 ioc->name, __FILE__, __LINE__, __func__);
1631 kfree(vol_pg0);
1632 return;
1633 }
1634
1635 raid_device->volume_type = vol_pg0->VolumeType;
1636
1637 /* figure out what the underlying devices are by
1638 * obtaining the device_info bits for the 1st device
1639 */
1640 if (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1641 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1642 vol_pg0->PhysDisk[0].PhysDiskNum))) {
1643 if (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
1644 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
1645 le16_to_cpu(pd_pg0.DevHandle)))) {
1646 raid_device->device_info =
1647 le32_to_cpu(sas_device_pg0.DeviceInfo);
1648 }
1649 }
1650
1651 kfree(vol_pg0);
1652}
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301653/**
1654 * _scsih_disable_ddio - Disable direct I/O for all the volumes
1655 * @ioc: per adapter object
1656 */
1657static void
1658_scsih_disable_ddio(struct MPT2SAS_ADAPTER *ioc)
1659{
1660 Mpi2RaidVolPage1_t vol_pg1;
1661 Mpi2ConfigReply_t mpi_reply;
1662 struct _raid_device *raid_device;
1663 u16 handle;
1664 u16 ioc_status;
1665
1666 handle = 0xFFFF;
1667 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1668 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1669 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1670 MPI2_IOCSTATUS_MASK;
1671 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1672 break;
1673 handle = le16_to_cpu(vol_pg1.DevHandle);
1674 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
1675 if (raid_device)
1676 raid_device->direct_io_enabled = 0;
1677 }
1678 return;
1679}
1680
1681
1682/**
1683 * _scsih_get_num_volumes - Get number of volumes in the ioc
1684 * @ioc: per adapter object
1685 */
1686static u8
1687_scsih_get_num_volumes(struct MPT2SAS_ADAPTER *ioc)
1688{
1689 Mpi2RaidVolPage1_t vol_pg1;
1690 Mpi2ConfigReply_t mpi_reply;
1691 u16 handle;
1692 u8 vol_cnt = 0;
1693 u16 ioc_status;
1694
1695 handle = 0xFFFF;
1696 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
1697 &vol_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
1698 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
1699 MPI2_IOCSTATUS_MASK;
1700 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
1701 break;
1702 vol_cnt++;
1703 handle = le16_to_cpu(vol_pg1.DevHandle);
1704 }
1705 return vol_cnt;
1706}
1707
1708
1709/**
1710 * _scsih_init_warpdrive_properties - Set properties for warpdrive direct I/O.
1711 * @ioc: per adapter object
1712 * @raid_device: the raid_device object
1713 */
1714static void
1715_scsih_init_warpdrive_properties(struct MPT2SAS_ADAPTER *ioc,
1716 struct _raid_device *raid_device)
1717{
1718 Mpi2RaidVolPage0_t *vol_pg0;
1719 Mpi2RaidPhysDiskPage0_t pd_pg0;
1720 Mpi2ConfigReply_t mpi_reply;
1721 u16 sz;
1722 u8 num_pds, count;
1723 u64 mb = 1024 * 1024;
1724 u64 tb_2 = 2 * mb * mb;
1725 u64 capacity;
1726 u32 stripe_sz;
1727 u8 i, stripe_exp;
1728
1729 if (!ioc->is_warpdrive)
1730 return;
1731
1732 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS) {
1733 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1734 "globally as drives are exposed\n", ioc->name);
1735 return;
1736 }
1737 if (_scsih_get_num_volumes(ioc) > 1) {
1738 _scsih_disable_ddio(ioc);
1739 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1740 "globally as number of drives > 1\n", ioc->name);
1741 return;
1742 }
1743 if ((mpt2sas_config_get_number_pds(ioc, raid_device->handle,
1744 &num_pds)) || !num_pds) {
1745 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1746 "Failure in computing number of drives\n", ioc->name);
1747 return;
1748 }
1749
1750 sz = offsetof(Mpi2RaidVolPage0_t, PhysDisk) + (num_pds *
1751 sizeof(Mpi2RaidVol0PhysDisk_t));
1752 vol_pg0 = kzalloc(sz, GFP_KERNEL);
1753 if (!vol_pg0) {
1754 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1755 "Memory allocation failure for RVPG0\n", ioc->name);
1756 return;
1757 }
1758
1759 if ((mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply, vol_pg0,
1760 MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, raid_device->handle, sz))) {
1761 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1762 "Failure in retrieving RVPG0\n", ioc->name);
1763 kfree(vol_pg0);
1764 return;
1765 }
1766
1767 /*
1768 * WARPDRIVE:If number of physical disks in a volume exceeds the max pds
1769 * assumed for WARPDRIVE, disable direct I/O
1770 */
1771 if (num_pds > MPT_MAX_WARPDRIVE_PDS) {
1772 printk(MPT2SAS_WARN_FMT "WarpDrive : Direct IO is disabled "
1773 "for the drive with handle(0x%04x): num_mem=%d, "
1774 "max_mem_allowed=%d\n", ioc->name, raid_device->handle,
1775 num_pds, MPT_MAX_WARPDRIVE_PDS);
1776 kfree(vol_pg0);
1777 return;
1778 }
1779 for (count = 0; count < num_pds; count++) {
1780 if (mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
1781 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_PHYSDISKNUM,
1782 vol_pg0->PhysDisk[count].PhysDiskNum) ||
1783 pd_pg0.DevHandle == MPT2SAS_INVALID_DEVICE_HANDLE) {
1784 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is "
1785 "disabled for the drive with handle(0x%04x) member"
1786 "handle retrieval failed for member number=%d\n",
1787 ioc->name, raid_device->handle,
1788 vol_pg0->PhysDisk[count].PhysDiskNum);
1789 goto out_error;
1790 }
1791 raid_device->pd_handle[count] = le16_to_cpu(pd_pg0.DevHandle);
1792 }
1793
1794 /*
1795 * Assumption for WD: Direct I/O is not supported if the volume is
1796 * not RAID0, if the stripe size is not 64KB, if the block size is
1797 * not 512 and if the volume size is >2TB
1798 */
1799 if (raid_device->volume_type != MPI2_RAID_VOL_TYPE_RAID0 ||
1800 le16_to_cpu(vol_pg0->BlockSize) != 512) {
1801 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1802 "for the drive with handle(0x%04x): type=%d, "
1803 "s_sz=%uK, blk_size=%u\n", ioc->name,
1804 raid_device->handle, raid_device->volume_type,
1805 le32_to_cpu(vol_pg0->StripeSize)/2,
1806 le16_to_cpu(vol_pg0->BlockSize));
1807 goto out_error;
1808 }
1809
1810 capacity = (u64) le16_to_cpu(vol_pg0->BlockSize) *
1811 (le64_to_cpu(vol_pg0->MaxLBA) + 1);
1812
1813 if (capacity > tb_2) {
1814 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1815 "for the drive with handle(0x%04x) since drive sz > 2TB\n",
1816 ioc->name, raid_device->handle);
1817 goto out_error;
1818 }
1819
1820 stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1821 stripe_exp = 0;
1822 for (i = 0; i < 32; i++) {
1823 if (stripe_sz & 1)
1824 break;
1825 stripe_exp++;
1826 stripe_sz >>= 1;
1827 }
1828 if (i == 32) {
1829 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is disabled "
1830 "for the drive with handle(0x%04x) invalid stripe sz %uK\n",
1831 ioc->name, raid_device->handle,
1832 le32_to_cpu(vol_pg0->StripeSize)/2);
1833 goto out_error;
1834 }
1835 raid_device->stripe_exponent = stripe_exp;
1836 raid_device->direct_io_enabled = 1;
1837
1838 printk(MPT2SAS_INFO_FMT "WarpDrive : Direct IO is Enabled for the drive"
1839 " with handle(0x%04x)\n", ioc->name, raid_device->handle);
1840 /*
1841 * WARPDRIVE: Though the following fields are not used for direct IO,
1842 * stored for future purpose:
1843 */
1844 raid_device->max_lba = le64_to_cpu(vol_pg0->MaxLBA);
1845 raid_device->stripe_sz = le32_to_cpu(vol_pg0->StripeSize);
1846 raid_device->block_sz = le16_to_cpu(vol_pg0->BlockSize);
1847
1848
1849 kfree(vol_pg0);
1850 return;
1851
1852out_error:
1853 raid_device->direct_io_enabled = 0;
1854 for (count = 0; count < num_pds; count++)
1855 raid_device->pd_handle[count] = 0;
1856 kfree(vol_pg0);
1857 return;
1858}
Eric Moore635374e2009-03-09 01:21:12 -06001859
1860/**
Kashyap, Desai84f0b042009-12-16 18:56:28 +05301861 * _scsih_enable_tlr - setting TLR flags
1862 * @ioc: per adapter object
1863 * @sdev: scsi device struct
1864 *
1865 * Enabling Transaction Layer Retries for tape devices when
1866 * vpd page 0x90 is present
1867 *
1868 */
1869static void
1870_scsih_enable_tlr(struct MPT2SAS_ADAPTER *ioc, struct scsi_device *sdev)
1871{
1872 /* only for TAPE */
1873 if (sdev->type != TYPE_TAPE)
1874 return;
1875
1876 if (!(ioc->facts.IOCCapabilities & MPI2_IOCFACTS_CAPABILITY_TLR))
1877 return;
1878
1879 sas_enable_tlr(sdev);
1880 sdev_printk(KERN_INFO, sdev, "TLR %s\n",
1881 sas_is_tlr_enabled(sdev) ? "Enabled" : "Disabled");
1882 return;
1883
1884}
1885
1886/**
Eric Moored5d135b2009-05-18 13:02:08 -06001887 * _scsih_slave_configure - device configure routine.
Eric Moore635374e2009-03-09 01:21:12 -06001888 * @sdev: scsi device struct
1889 *
1890 * Returns 0 if ok. Any other return is assumed to be an error and
1891 * the device is ignored.
1892 */
1893static int
Eric Moored5d135b2009-05-18 13:02:08 -06001894_scsih_slave_configure(struct scsi_device *sdev)
Eric Moore635374e2009-03-09 01:21:12 -06001895{
1896 struct Scsi_Host *shost = sdev->host;
1897 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
1898 struct MPT2SAS_DEVICE *sas_device_priv_data;
1899 struct MPT2SAS_TARGET *sas_target_priv_data;
1900 struct _sas_device *sas_device;
1901 struct _raid_device *raid_device;
1902 unsigned long flags;
1903 int qdepth;
1904 u8 ssp_target = 0;
1905 char *ds = "";
1906 char *r_level = "";
1907
1908 qdepth = 1;
1909 sas_device_priv_data = sdev->hostdata;
1910 sas_device_priv_data->configured_lun = 1;
1911 sas_device_priv_data->flags &= ~MPT_DEVICE_FLAGS_INIT;
1912 sas_target_priv_data = sas_device_priv_data->sas_target;
1913
1914 /* raid volume handling */
1915 if (sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME) {
1916
1917 spin_lock_irqsave(&ioc->raid_device_lock, flags);
1918 raid_device = _scsih_raid_device_find_by_handle(ioc,
1919 sas_target_priv_data->handle);
1920 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
1921 if (!raid_device) {
1922 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
1923 ioc->name, __FILE__, __LINE__, __func__);
1924 return 0;
1925 }
1926
1927 _scsih_get_volume_capabilities(ioc, raid_device);
1928
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301929 /*
1930 * WARPDRIVE: Initialize the required data for Direct IO
1931 */
1932 _scsih_init_warpdrive_properties(ioc, raid_device);
1933
Eric Moore635374e2009-03-09 01:21:12 -06001934 /* RAID Queue Depth Support
1935 * IS volume = underlying qdepth of drive type, either
1936 * MPT2SAS_SAS_QUEUE_DEPTH or MPT2SAS_SATA_QUEUE_DEPTH
1937 * IM/IME/R10 = 128 (MPT2SAS_RAID_QUEUE_DEPTH)
1938 */
1939 if (raid_device->device_info &
1940 MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
1941 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
1942 ds = "SSP";
1943 } else {
1944 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
1945 if (raid_device->device_info &
1946 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
1947 ds = "SATA";
1948 else
1949 ds = "STP";
1950 }
1951
1952 switch (raid_device->volume_type) {
1953 case MPI2_RAID_VOL_TYPE_RAID0:
1954 r_level = "RAID0";
1955 break;
1956 case MPI2_RAID_VOL_TYPE_RAID1E:
1957 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
Kashyap, Desaied79f122009-08-20 13:23:49 +05301958 if (ioc->manu_pg10.OEMIdentifier &&
1959 (ioc->manu_pg10.GenericFlags0 &
1960 MFG10_GF0_R10_DISPLAY) &&
1961 !(raid_device->num_pds % 2))
1962 r_level = "RAID10";
1963 else
1964 r_level = "RAID1E";
Eric Moore635374e2009-03-09 01:21:12 -06001965 break;
1966 case MPI2_RAID_VOL_TYPE_RAID1:
1967 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1968 r_level = "RAID1";
1969 break;
1970 case MPI2_RAID_VOL_TYPE_RAID10:
1971 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1972 r_level = "RAID10";
1973 break;
1974 case MPI2_RAID_VOL_TYPE_UNKNOWN:
1975 default:
1976 qdepth = MPT2SAS_RAID_QUEUE_DEPTH;
1977 r_level = "RAIDX";
1978 break;
1979 }
1980
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301981 if (!ioc->hide_ir_msg)
1982 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
1983 "wwid(0x%016llx), pd_count(%d), type(%s)\n",
1984 r_level, raid_device->handle,
1985 (unsigned long long)raid_device->wwid,
1986 raid_device->num_pds, ds);
Mike Christiee881a172009-10-15 17:46:39 -07001987 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05301988 /* raid transport support */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05301989 if (!ioc->is_warpdrive)
1990 _scsih_set_level(sdev, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06001991 return 0;
1992 }
1993
1994 /* non-raid handling */
1995 spin_lock_irqsave(&ioc->sas_device_lock, flags);
1996 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
1997 sas_device_priv_data->sas_target->sas_address);
1998 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
1999 if (sas_device) {
2000 if (sas_target_priv_data->flags &
2001 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2002 mpt2sas_config_get_volume_handle(ioc,
2003 sas_device->handle, &sas_device->volume_handle);
2004 mpt2sas_config_get_volume_wwid(ioc,
2005 sas_device->volume_handle,
2006 &sas_device->volume_wwid);
2007 }
2008 if (sas_device->device_info & MPI2_SAS_DEVICE_INFO_SSP_TARGET) {
2009 qdepth = MPT2SAS_SAS_QUEUE_DEPTH;
2010 ssp_target = 1;
2011 ds = "SSP";
2012 } else {
2013 qdepth = MPT2SAS_SATA_QUEUE_DEPTH;
2014 if (sas_device->device_info &
2015 MPI2_SAS_DEVICE_INFO_STP_TARGET)
2016 ds = "STP";
2017 else if (sas_device->device_info &
2018 MPI2_SAS_DEVICE_INFO_SATA_DEVICE)
2019 ds = "SATA";
2020 }
2021
2022 sdev_printk(KERN_INFO, sdev, "%s: handle(0x%04x), "
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302023 "sas_addr(0x%016llx), phy(%d), device_name(0x%016llx)\n",
Eric Moore635374e2009-03-09 01:21:12 -06002024 ds, sas_device->handle,
2025 (unsigned long long)sas_device->sas_address,
Kashyap, Desai7fbae672010-06-17 13:45:17 +05302026 sas_device->phy,
Eric Moore635374e2009-03-09 01:21:12 -06002027 (unsigned long long)sas_device->device_name);
2028 sdev_printk(KERN_INFO, sdev, "%s: "
2029 "enclosure_logical_id(0x%016llx), slot(%d)\n", ds,
2030 (unsigned long long) sas_device->enclosure_logical_id,
2031 sas_device->slot);
2032
2033 if (!ssp_target)
Eric Moored5d135b2009-05-18 13:02:08 -06002034 _scsih_display_sata_capabilities(ioc, sas_device, sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002035 }
2036
Mike Christiee881a172009-10-15 17:46:39 -07002037 _scsih_change_queue_depth(sdev, qdepth, SCSI_QDEPTH_DEFAULT);
Eric Moore635374e2009-03-09 01:21:12 -06002038
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302039 if (ssp_target) {
Eric Moore635374e2009-03-09 01:21:12 -06002040 sas_read_port_mode_page(sdev);
Kashyap, Desai84f0b042009-12-16 18:56:28 +05302041 _scsih_enable_tlr(ioc, sdev);
2042 }
Eric Moore635374e2009-03-09 01:21:12 -06002043 return 0;
2044}
2045
2046/**
Eric Moored5d135b2009-05-18 13:02:08 -06002047 * _scsih_bios_param - fetch head, sector, cylinder info for a disk
Eric Moore635374e2009-03-09 01:21:12 -06002048 * @sdev: scsi device struct
2049 * @bdev: pointer to block device context
2050 * @capacity: device size (in 512 byte sectors)
2051 * @params: three element array to place output:
2052 * params[0] number of heads (max 255)
2053 * params[1] number of sectors (max 63)
2054 * params[2] number of cylinders
2055 *
2056 * Return nothing.
2057 */
2058static int
Eric Moored5d135b2009-05-18 13:02:08 -06002059_scsih_bios_param(struct scsi_device *sdev, struct block_device *bdev,
Eric Moore635374e2009-03-09 01:21:12 -06002060 sector_t capacity, int params[])
2061{
2062 int heads;
2063 int sectors;
2064 sector_t cylinders;
2065 ulong dummy;
2066
2067 heads = 64;
2068 sectors = 32;
2069
2070 dummy = heads * sectors;
2071 cylinders = capacity;
2072 sector_div(cylinders, dummy);
2073
2074 /*
2075 * Handle extended translation size for logical drives
2076 * > 1Gb
2077 */
2078 if ((ulong)capacity >= 0x200000) {
2079 heads = 255;
2080 sectors = 63;
2081 dummy = heads * sectors;
2082 cylinders = capacity;
2083 sector_div(cylinders, dummy);
2084 }
2085
2086 /* return result */
2087 params[0] = heads;
2088 params[1] = sectors;
2089 params[2] = cylinders;
2090
2091 return 0;
2092}
2093
2094/**
2095 * _scsih_response_code - translation of device response code
2096 * @ioc: per adapter object
2097 * @response_code: response code returned by the device
2098 *
2099 * Return nothing.
2100 */
2101static void
2102_scsih_response_code(struct MPT2SAS_ADAPTER *ioc, u8 response_code)
2103{
2104 char *desc;
2105
2106 switch (response_code) {
2107 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
2108 desc = "task management request completed";
2109 break;
2110 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
2111 desc = "invalid frame";
2112 break;
2113 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
2114 desc = "task management request not supported";
2115 break;
2116 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
2117 desc = "task management request failed";
2118 break;
2119 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
2120 desc = "task management request succeeded";
2121 break;
2122 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
2123 desc = "invalid lun";
2124 break;
2125 case 0xA:
2126 desc = "overlapped tag attempted";
2127 break;
2128 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
2129 desc = "task queued, however not sent to target";
2130 break;
2131 default:
2132 desc = "unknown";
2133 break;
2134 }
2135 printk(MPT2SAS_WARN_FMT "response_code(0x%01x): %s\n",
2136 ioc->name, response_code, desc);
2137}
2138
2139/**
Eric Moored5d135b2009-05-18 13:02:08 -06002140 * _scsih_tm_done - tm completion routine
Eric Moore635374e2009-03-09 01:21:12 -06002141 * @ioc: per adapter object
2142 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302143 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002144 * @reply: reply message frame(lower 32bit addr)
2145 * Context: none.
2146 *
2147 * The callback handler when using scsih_issue_tm.
2148 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302149 * Return 1 meaning mf should be freed from _base_interrupt
2150 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06002151 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302152static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302153_scsih_tm_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06002154{
2155 MPI2DefaultReply_t *mpi_reply;
2156
2157 if (ioc->tm_cmds.status == MPT2_CMD_NOT_USED)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302158 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002159 if (ioc->tm_cmds.smid != smid)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302160 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002161 ioc->tm_cmds.status |= MPT2_CMD_COMPLETE;
2162 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
2163 if (mpi_reply) {
2164 memcpy(ioc->tm_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
2165 ioc->tm_cmds.status |= MPT2_CMD_REPLY_VALID;
2166 }
2167 ioc->tm_cmds.status &= ~MPT2_CMD_PENDING;
2168 complete(&ioc->tm_cmds.done);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302169 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06002170}
2171
2172/**
2173 * mpt2sas_scsih_set_tm_flag - set per target tm_busy
2174 * @ioc: per adapter object
2175 * @handle: device handle
2176 *
2177 * During taskmangement request, we need to freeze the device queue.
2178 */
2179void
2180mpt2sas_scsih_set_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2181{
2182 struct MPT2SAS_DEVICE *sas_device_priv_data;
2183 struct scsi_device *sdev;
2184 u8 skip = 0;
2185
2186 shost_for_each_device(sdev, ioc->shost) {
2187 if (skip)
2188 continue;
2189 sas_device_priv_data = sdev->hostdata;
2190 if (!sas_device_priv_data)
2191 continue;
2192 if (sas_device_priv_data->sas_target->handle == handle) {
2193 sas_device_priv_data->sas_target->tm_busy = 1;
2194 skip = 1;
2195 ioc->ignore_loginfos = 1;
2196 }
2197 }
2198}
2199
2200/**
2201 * mpt2sas_scsih_clear_tm_flag - clear per target tm_busy
2202 * @ioc: per adapter object
2203 * @handle: device handle
2204 *
2205 * During taskmangement request, we need to freeze the device queue.
2206 */
2207void
2208mpt2sas_scsih_clear_tm_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2209{
2210 struct MPT2SAS_DEVICE *sas_device_priv_data;
2211 struct scsi_device *sdev;
2212 u8 skip = 0;
2213
2214 shost_for_each_device(sdev, ioc->shost) {
2215 if (skip)
2216 continue;
2217 sas_device_priv_data = sdev->hostdata;
2218 if (!sas_device_priv_data)
2219 continue;
2220 if (sas_device_priv_data->sas_target->handle == handle) {
2221 sas_device_priv_data->sas_target->tm_busy = 0;
2222 skip = 1;
2223 ioc->ignore_loginfos = 0;
2224 }
2225 }
2226}
2227
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302228
Eric Moore635374e2009-03-09 01:21:12 -06002229/**
2230 * mpt2sas_scsih_issue_tm - main routine for sending tm requests
2231 * @ioc: per adapter struct
2232 * @device_handle: device handle
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302233 * @channel: the channel assigned by the OS
2234 * @id: the id assigned by the OS
Eric Moore635374e2009-03-09 01:21:12 -06002235 * @lun: lun number
2236 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in mpi2_init.h)
2237 * @smid_task: smid assigned to the task
2238 * @timeout: timeout in seconds
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302239 * Context: user
Eric Moore635374e2009-03-09 01:21:12 -06002240 *
2241 * A generic API for sending task management requests to firmware.
2242 *
Eric Moore635374e2009-03-09 01:21:12 -06002243 * The callback index is set inside `ioc->tm_cb_idx`.
2244 *
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302245 * Return SUCCESS or FAILED.
Eric Moore635374e2009-03-09 01:21:12 -06002246 */
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302247int
2248mpt2sas_scsih_issue_tm(struct MPT2SAS_ADAPTER *ioc, u16 handle, uint channel,
2249 uint id, uint lun, u8 type, u16 smid_task, ulong timeout,
2250 struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002251{
2252 Mpi2SCSITaskManagementRequest_t *mpi_request;
2253 Mpi2SCSITaskManagementReply_t *mpi_reply;
2254 u16 smid = 0;
2255 u32 ioc_state;
2256 unsigned long timeleft;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302257 struct scsi_cmnd *scmd_lookup;
2258 int rc;
Eric Moore635374e2009-03-09 01:21:12 -06002259
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302260 mutex_lock(&ioc->tm_cmds.mutex);
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302261 if (ioc->tm_cmds.status != MPT2_CMD_NOT_USED) {
2262 printk(MPT2SAS_INFO_FMT "%s: tm_cmd busy!!!\n",
2263 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302264 rc = FAILED;
2265 goto err_out;
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05302266 }
2267
Eric Moore3cb54692010-07-08 14:44:34 -06002268 if (ioc->shost_recovery || ioc->remove_host ||
2269 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06002270 printk(MPT2SAS_INFO_FMT "%s: host reset in progress!\n",
2271 __func__, ioc->name);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302272 rc = FAILED;
2273 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002274 }
Eric Moore635374e2009-03-09 01:21:12 -06002275
2276 ioc_state = mpt2sas_base_get_iocstate(ioc, 0);
2277 if (ioc_state & MPI2_DOORBELL_USED) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05302278 dhsprintk(ioc, printk(MPT2SAS_INFO_FMT "unexpected doorbell "
Eric Moore635374e2009-03-09 01:21:12 -06002279 "active!\n", ioc->name));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302280 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2281 FORCE_BIG_HAMMER);
2282 rc = SUCCESS;
2283 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002284 }
2285
2286 if ((ioc_state & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
2287 mpt2sas_base_fault_info(ioc, ioc_state &
2288 MPI2_DOORBELL_DATA_MASK);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302289 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2290 FORCE_BIG_HAMMER);
2291 rc = SUCCESS;
2292 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002293 }
2294
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302295 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06002296 if (!smid) {
2297 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
2298 ioc->name, __func__);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302299 rc = FAILED;
2300 goto err_out;
Eric Moore635374e2009-03-09 01:21:12 -06002301 }
2302
2303 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "sending tm: handle(0x%04x),"
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302304 " task_type(0x%02x), smid(%d)\n", ioc->name, handle, type,
2305 smid_task));
Eric Moore635374e2009-03-09 01:21:12 -06002306 ioc->tm_cmds.status = MPT2_CMD_PENDING;
2307 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2308 ioc->tm_cmds.smid = smid;
2309 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2310 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2311 mpi_request->DevHandle = cpu_to_le16(handle);
2312 mpi_request->TaskType = type;
2313 mpi_request->TaskMID = cpu_to_le16(smid_task);
2314 int_to_scsilun(lun, (struct scsi_lun *)mpi_request->LUN);
2315 mpt2sas_scsih_set_tm_flag(ioc, handle);
Kashyap, Desai5b768582009-08-20 13:24:31 +05302316 init_completion(&ioc->tm_cmds.done);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05302317 mpt2sas_base_put_smid_hi_priority(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06002318 timeleft = wait_for_completion_timeout(&ioc->tm_cmds.done, timeout*HZ);
Eric Moore635374e2009-03-09 01:21:12 -06002319 if (!(ioc->tm_cmds.status & MPT2_CMD_COMPLETE)) {
2320 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
2321 ioc->name, __func__);
2322 _debug_dump_mf(mpi_request,
2323 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302324 if (!(ioc->tm_cmds.status & MPT2_CMD_RESET)) {
2325 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2326 FORCE_BIG_HAMMER);
2327 rc = SUCCESS;
2328 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2329 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2330 goto err_out;
2331 }
Eric Moore635374e2009-03-09 01:21:12 -06002332 }
2333
2334 if (ioc->tm_cmds.status & MPT2_CMD_REPLY_VALID) {
2335 mpi_reply = ioc->tm_cmds.reply;
2336 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "complete tm: "
2337 "ioc_status(0x%04x), loginfo(0x%08x), term_count(0x%08x)\n",
2338 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
2339 le32_to_cpu(mpi_reply->IOCLogInfo),
2340 le32_to_cpu(mpi_reply->TerminationCount)));
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302341 if (ioc->logging_level & MPT_DEBUG_TM) {
Eric Moore635374e2009-03-09 01:21:12 -06002342 _scsih_response_code(ioc, mpi_reply->ResponseCode);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302343 if (mpi_reply->IOCStatus)
2344 _debug_dump_mf(mpi_request,
2345 sizeof(Mpi2SCSITaskManagementRequest_t)/4);
2346 }
Eric Moore635374e2009-03-09 01:21:12 -06002347 }
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302348
2349 /* sanity check:
2350 * Check to see the commands were terminated.
2351 * This is only needed for eh callbacks, hence the scmd check.
2352 */
2353 rc = FAILED;
2354 if (scmd == NULL)
2355 goto bypass_sanity_checks;
2356 switch (type) {
2357 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
2358 scmd_lookup = _scsih_scsi_lookup_get(ioc, smid_task);
Christoph Hellwiga7c44d42011-04-04 09:42:30 -04002359 if (scmd_lookup)
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302360 rc = FAILED;
2361 else
2362 rc = SUCCESS;
2363 break;
2364
2365 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
2366 if (_scsih_scsi_lookup_find_by_target(ioc, id, channel))
2367 rc = FAILED;
2368 else
2369 rc = SUCCESS;
2370 break;
2371
2372 case MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET:
2373 if (_scsih_scsi_lookup_find_by_lun(ioc, id, lun, channel))
2374 rc = FAILED;
2375 else
2376 rc = SUCCESS;
2377 break;
2378 }
2379
2380 bypass_sanity_checks:
2381
2382 mpt2sas_scsih_clear_tm_flag(ioc, handle);
2383 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
2384 mutex_unlock(&ioc->tm_cmds.mutex);
2385
2386 return rc;
2387
2388 err_out:
2389 mutex_unlock(&ioc->tm_cmds.mutex);
2390 return rc;
Eric Moore635374e2009-03-09 01:21:12 -06002391}
2392
2393/**
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302394 * _scsih_tm_display_info - displays info about the device
2395 * @ioc: per adapter struct
2396 * @scmd: pointer to scsi command object
2397 *
2398 * Called by task management callback handlers.
2399 */
2400static void
2401_scsih_tm_display_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd)
2402{
2403 struct scsi_target *starget = scmd->device->sdev_target;
2404 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
2405 struct _sas_device *sas_device = NULL;
2406 unsigned long flags;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302407 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302408
2409 if (!priv_target)
2410 return;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302411 if (ioc->hide_ir_msg)
2412 device_str = "WarpDrive";
2413 else
2414 device_str = "volume";
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302415
2416 scsi_print_command(scmd);
2417 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05302418 starget_printk(KERN_INFO, starget, "%s handle(0x%04x), "
2419 "%s wwid(0x%016llx)\n", device_str, priv_target->handle,
2420 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302421 } else {
2422 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2423 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2424 priv_target->sas_address);
2425 if (sas_device) {
2426 if (priv_target->flags &
2427 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2428 starget_printk(KERN_INFO, starget,
2429 "volume handle(0x%04x), "
2430 "volume wwid(0x%016llx)\n",
2431 sas_device->volume_handle,
2432 (unsigned long long)sas_device->volume_wwid);
2433 }
2434 starget_printk(KERN_INFO, starget,
2435 "handle(0x%04x), sas_address(0x%016llx), phy(%d)\n",
2436 sas_device->handle,
2437 (unsigned long long)sas_device->sas_address,
2438 sas_device->phy);
2439 starget_printk(KERN_INFO, starget,
2440 "enclosure_logical_id(0x%016llx), slot(%d)\n",
2441 (unsigned long long)sas_device->enclosure_logical_id,
2442 sas_device->slot);
2443 }
2444 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2445 }
2446}
2447
2448/**
Eric Moored5d135b2009-05-18 13:02:08 -06002449 * _scsih_abort - eh threads main abort routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302450 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002451 *
2452 * Returns SUCCESS if command aborted else FAILED
2453 */
2454static int
Eric Moored5d135b2009-05-18 13:02:08 -06002455_scsih_abort(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002456{
2457 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2458 struct MPT2SAS_DEVICE *sas_device_priv_data;
2459 u16 smid;
2460 u16 handle;
2461 int r;
Eric Moore635374e2009-03-09 01:21:12 -06002462
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302463 sdev_printk(KERN_INFO, scmd->device, "attempting task abort! "
2464 "scmd(%p)\n", scmd);
2465 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002466
2467 sas_device_priv_data = scmd->device->hostdata;
2468 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302469 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
2470 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002471 scmd->result = DID_NO_CONNECT << 16;
2472 scmd->scsi_done(scmd);
2473 r = SUCCESS;
2474 goto out;
2475 }
2476
2477 /* search for the command */
2478 smid = _scsih_scsi_lookup_find_by_scmd(ioc, scmd);
2479 if (!smid) {
2480 scmd->result = DID_RESET << 16;
2481 r = SUCCESS;
2482 goto out;
2483 }
2484
2485 /* for hidden raid components and volumes this is not supported */
2486 if (sas_device_priv_data->sas_target->flags &
2487 MPT_TARGET_FLAGS_RAID_COMPONENT ||
2488 sas_device_priv_data->sas_target->flags & MPT_TARGET_FLAGS_VOLUME) {
2489 scmd->result = DID_RESET << 16;
2490 r = FAILED;
2491 goto out;
2492 }
2493
Kashyap, Desaifa7f3162009-09-23 17:26:58 +05302494 mpt2sas_halt_firmware(ioc);
2495
Eric Moore635374e2009-03-09 01:21:12 -06002496 handle = sas_device_priv_data->sas_target->handle;
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302497 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2498 scmd->device->id, scmd->device->lun,
2499 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002500
2501 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302502 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
2503 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002504 return r;
2505}
2506
Eric Moore635374e2009-03-09 01:21:12 -06002507/**
Eric Moored5d135b2009-05-18 13:02:08 -06002508 * _scsih_dev_reset - eh threads main device reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302509 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002510 *
2511 * Returns SUCCESS if command aborted else FAILED
2512 */
2513static int
Eric Moored5d135b2009-05-18 13:02:08 -06002514_scsih_dev_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002515{
2516 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2517 struct MPT2SAS_DEVICE *sas_device_priv_data;
2518 struct _sas_device *sas_device;
2519 unsigned long flags;
2520 u16 handle;
2521 int r;
2522
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302523 struct scsi_target *starget = scmd->device->sdev_target;
2524
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302525 starget_printk(KERN_INFO, starget, "attempting device reset! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302526 "scmd(%p)\n", scmd);
2527 _scsih_tm_display_info(ioc, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002528
2529 sas_device_priv_data = scmd->device->hostdata;
2530 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai37aaa782010-11-13 04:41:32 +05302531 starget_printk(KERN_INFO, starget, "device been deleted! "
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302532 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002533 scmd->result = DID_NO_CONNECT << 16;
2534 scmd->scsi_done(scmd);
2535 r = SUCCESS;
2536 goto out;
2537 }
2538
2539 /* for hidden raid components obtain the volume_handle */
2540 handle = 0;
2541 if (sas_device_priv_data->sas_target->flags &
2542 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2543 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2544 sas_device = _scsih_sas_device_find_by_handle(ioc,
2545 sas_device_priv_data->sas_target->handle);
2546 if (sas_device)
2547 handle = sas_device->volume_handle;
2548 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2549 } else
2550 handle = sas_device_priv_data->sas_target->handle;
2551
2552 if (!handle) {
2553 scmd->result = DID_RESET << 16;
2554 r = FAILED;
2555 goto out;
2556 }
2557
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302558 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2559 scmd->device->id, scmd->device->lun,
2560 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET, 0, 30, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002561
2562 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302563 sdev_printk(KERN_INFO, scmd->device, "device reset: %s scmd(%p)\n",
2564 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002565 return r;
2566}
2567
2568/**
Eric Moored5d135b2009-05-18 13:02:08 -06002569 * _scsih_target_reset - eh threads main target reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302570 * @scmd: pointer to scsi command object
Eric Moore993e0da2009-05-18 13:00:45 -06002571 *
2572 * Returns SUCCESS if command aborted else FAILED
2573 */
2574static int
Eric Moored5d135b2009-05-18 13:02:08 -06002575_scsih_target_reset(struct scsi_cmnd *scmd)
Eric Moore993e0da2009-05-18 13:00:45 -06002576{
2577 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2578 struct MPT2SAS_DEVICE *sas_device_priv_data;
2579 struct _sas_device *sas_device;
2580 unsigned long flags;
2581 u16 handle;
2582 int r;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302583 struct scsi_target *starget = scmd->device->sdev_target;
Eric Moore993e0da2009-05-18 13:00:45 -06002584
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302585 starget_printk(KERN_INFO, starget, "attempting target reset! "
2586 "scmd(%p)\n", scmd);
2587 _scsih_tm_display_info(ioc, scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002588
2589 sas_device_priv_data = scmd->device->hostdata;
2590 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302591 starget_printk(KERN_INFO, starget, "target been deleted! "
2592 "scmd(%p)\n", scmd);
Eric Moore993e0da2009-05-18 13:00:45 -06002593 scmd->result = DID_NO_CONNECT << 16;
2594 scmd->scsi_done(scmd);
2595 r = SUCCESS;
2596 goto out;
2597 }
2598
2599 /* for hidden raid components obtain the volume_handle */
2600 handle = 0;
2601 if (sas_device_priv_data->sas_target->flags &
2602 MPT_TARGET_FLAGS_RAID_COMPONENT) {
2603 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2604 sas_device = _scsih_sas_device_find_by_handle(ioc,
2605 sas_device_priv_data->sas_target->handle);
2606 if (sas_device)
2607 handle = sas_device->volume_handle;
2608 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2609 } else
2610 handle = sas_device_priv_data->sas_target->handle;
2611
2612 if (!handle) {
2613 scmd->result = DID_RESET << 16;
2614 r = FAILED;
2615 goto out;
2616 }
2617
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05302618 r = mpt2sas_scsih_issue_tm(ioc, handle, scmd->device->channel,
2619 scmd->device->id, 0, MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 0,
2620 30, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002621
2622 out:
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302623 starget_printk(KERN_INFO, starget, "target reset: %s scmd(%p)\n",
2624 ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
Eric Moore635374e2009-03-09 01:21:12 -06002625 return r;
2626}
2627
2628/**
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05302629 * _scsih_host_reset - eh threads main host reset routine
Kashyap, Desai8e864a82010-06-17 13:48:46 +05302630 * @scmd: pointer to scsi command object
Eric Moore635374e2009-03-09 01:21:12 -06002631 *
2632 * Returns SUCCESS if command aborted else FAILED
2633 */
2634static int
Eric Moored5d135b2009-05-18 13:02:08 -06002635_scsih_host_reset(struct scsi_cmnd *scmd)
Eric Moore635374e2009-03-09 01:21:12 -06002636{
2637 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
2638 int r, retval;
2639
2640 printk(MPT2SAS_INFO_FMT "attempting host reset! scmd(%p)\n",
2641 ioc->name, scmd);
2642 scsi_print_command(scmd);
2643
2644 retval = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
2645 FORCE_BIG_HAMMER);
2646 r = (retval < 0) ? FAILED : SUCCESS;
2647 printk(MPT2SAS_INFO_FMT "host reset: %s scmd(%p)\n",
2648 ioc->name, ((r == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
2649
2650 return r;
2651}
2652
2653/**
2654 * _scsih_fw_event_add - insert and queue up fw_event
2655 * @ioc: per adapter object
2656 * @fw_event: object describing the event
2657 * Context: This function will acquire ioc->fw_event_lock.
2658 *
2659 * This adds the firmware event object into link list, then queues it up to
2660 * be processed from user context.
2661 *
2662 * Return nothing.
2663 */
2664static void
2665_scsih_fw_event_add(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work *fw_event)
2666{
2667 unsigned long flags;
2668
2669 if (ioc->firmware_event_thread == NULL)
2670 return;
2671
2672 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2673 list_add_tail(&fw_event->list, &ioc->fw_event_list);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302674 INIT_DELAYED_WORK(&fw_event->delayed_work, _firmware_event_work);
2675 queue_delayed_work(ioc->firmware_event_thread,
2676 &fw_event->delayed_work, 0);
Eric Moore635374e2009-03-09 01:21:12 -06002677 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2678}
2679
2680/**
2681 * _scsih_fw_event_free - delete fw_event
2682 * @ioc: per adapter object
2683 * @fw_event: object describing the event
2684 * Context: This function will acquire ioc->fw_event_lock.
2685 *
2686 * This removes firmware event object from link list, frees associated memory.
2687 *
2688 * Return nothing.
2689 */
2690static void
2691_scsih_fw_event_free(struct MPT2SAS_ADAPTER *ioc, struct fw_event_work
2692 *fw_event)
2693{
2694 unsigned long flags;
2695
2696 spin_lock_irqsave(&ioc->fw_event_lock, flags);
2697 list_del(&fw_event->list);
2698 kfree(fw_event->event_data);
2699 kfree(fw_event);
2700 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
2701}
2702
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302703
Eric Moore635374e2009-03-09 01:21:12 -06002704/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302705 * _scsih_queue_rescan - queue a topology rescan from user context
Eric Moore635374e2009-03-09 01:21:12 -06002706 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06002707 *
2708 * Return nothing.
2709 */
2710static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302711_scsih_queue_rescan(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06002712{
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302713 struct fw_event_work *fw_event;
2714
2715 if (ioc->wait_for_port_enable_to_complete)
2716 return;
2717 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
2718 if (!fw_event)
2719 return;
2720 fw_event->event = MPT2SAS_RESCAN_AFTER_HOST_RESET;
2721 fw_event->ioc = ioc;
2722 _scsih_fw_event_add(ioc, fw_event);
2723}
2724
2725/**
2726 * _scsih_fw_event_cleanup_queue - cleanup event queue
2727 * @ioc: per adapter object
2728 *
2729 * Walk the firmware event queue, either killing timers, or waiting
2730 * for outstanding events to complete
2731 *
2732 * Return nothing.
2733 */
2734static void
2735_scsih_fw_event_cleanup_queue(struct MPT2SAS_ADAPTER *ioc)
2736{
2737 struct fw_event_work *fw_event, *next;
2738
2739 if (list_empty(&ioc->fw_event_list) ||
2740 !ioc->firmware_event_thread || in_interrupt())
Eric Moore635374e2009-03-09 01:21:12 -06002741 return;
2742
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05302743 list_for_each_entry_safe(fw_event, next, &ioc->fw_event_list, list) {
2744 if (cancel_delayed_work(&fw_event->delayed_work)) {
2745 _scsih_fw_event_free(ioc, fw_event);
2746 continue;
2747 }
2748 fw_event->cancel_pending_work = 1;
2749 }
Eric Moore635374e2009-03-09 01:21:12 -06002750}
2751
Eric Moore635374e2009-03-09 01:21:12 -06002752/**
2753 * _scsih_ublock_io_device - set the device state to SDEV_RUNNING
2754 * @ioc: per adapter object
2755 * @handle: device handle
2756 *
2757 * During device pull we need to appropiately set the sdev state.
2758 */
2759static void
2760_scsih_ublock_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2761{
2762 struct MPT2SAS_DEVICE *sas_device_priv_data;
2763 struct scsi_device *sdev;
2764
2765 shost_for_each_device(sdev, ioc->shost) {
2766 sas_device_priv_data = sdev->hostdata;
2767 if (!sas_device_priv_data)
2768 continue;
2769 if (!sas_device_priv_data->block)
2770 continue;
2771 if (sas_device_priv_data->sas_target->handle == handle) {
2772 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2773 MPT2SAS_INFO_FMT "SDEV_RUNNING: "
2774 "handle(0x%04x)\n", ioc->name, handle));
2775 sas_device_priv_data->block = 0;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302776 scsi_internal_device_unblock(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002777 }
2778 }
2779}
2780
2781/**
2782 * _scsih_block_io_device - set the device state to SDEV_BLOCK
2783 * @ioc: per adapter object
2784 * @handle: device handle
2785 *
2786 * During device pull we need to appropiately set the sdev state.
2787 */
2788static void
2789_scsih_block_io_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2790{
2791 struct MPT2SAS_DEVICE *sas_device_priv_data;
2792 struct scsi_device *sdev;
2793
2794 shost_for_each_device(sdev, ioc->shost) {
2795 sas_device_priv_data = sdev->hostdata;
2796 if (!sas_device_priv_data)
2797 continue;
2798 if (sas_device_priv_data->block)
2799 continue;
2800 if (sas_device_priv_data->sas_target->handle == handle) {
2801 dewtprintk(ioc, sdev_printk(KERN_INFO, sdev,
2802 MPT2SAS_INFO_FMT "SDEV_BLOCK: "
2803 "handle(0x%04x)\n", ioc->name, handle));
2804 sas_device_priv_data->block = 1;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05302805 scsi_internal_device_block(sdev);
Eric Moore635374e2009-03-09 01:21:12 -06002806 }
2807 }
2808}
2809
2810/**
2811 * _scsih_block_io_to_children_attached_to_ex
2812 * @ioc: per adapter object
2813 * @sas_expander: the sas_device object
2814 *
2815 * This routine set sdev state to SDEV_BLOCK for all devices
2816 * attached to this expander. This function called when expander is
2817 * pulled.
2818 */
2819static void
2820_scsih_block_io_to_children_attached_to_ex(struct MPT2SAS_ADAPTER *ioc,
2821 struct _sas_node *sas_expander)
2822{
2823 struct _sas_port *mpt2sas_port;
2824 struct _sas_device *sas_device;
2825 struct _sas_node *expander_sibling;
2826 unsigned long flags;
2827
2828 if (!sas_expander)
2829 return;
2830
2831 list_for_each_entry(mpt2sas_port,
2832 &sas_expander->sas_port_list, port_list) {
2833 if (mpt2sas_port->remote_identify.device_type ==
2834 SAS_END_DEVICE) {
2835 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2836 sas_device =
2837 mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
2838 mpt2sas_port->remote_identify.sas_address);
2839 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
2840 if (!sas_device)
2841 continue;
2842 _scsih_block_io_device(ioc, sas_device->handle);
2843 }
2844 }
2845
2846 list_for_each_entry(mpt2sas_port,
2847 &sas_expander->sas_port_list, port_list) {
2848
2849 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302850 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06002851 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05302852 SAS_FANOUT_EXPANDER_DEVICE) {
Eric Moore635374e2009-03-09 01:21:12 -06002853
2854 spin_lock_irqsave(&ioc->sas_node_lock, flags);
2855 expander_sibling =
2856 mpt2sas_scsih_expander_find_by_sas_address(
2857 ioc, mpt2sas_port->remote_identify.sas_address);
2858 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
2859 _scsih_block_io_to_children_attached_to_ex(ioc,
2860 expander_sibling);
2861 }
2862 }
2863}
2864
2865/**
2866 * _scsih_block_io_to_children_attached_directly
2867 * @ioc: per adapter object
2868 * @event_data: topology change event data
2869 *
2870 * This routine set sdev state to SDEV_BLOCK for all devices
2871 * direct attached during device pull.
2872 */
2873static void
2874_scsih_block_io_to_children_attached_directly(struct MPT2SAS_ADAPTER *ioc,
2875 Mpi2EventDataSasTopologyChangeList_t *event_data)
2876{
2877 int i;
2878 u16 handle;
2879 u16 reason_code;
2880 u8 phy_number;
2881
2882 for (i = 0; i < event_data->NumEntries; i++) {
2883 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
2884 if (!handle)
2885 continue;
2886 phy_number = event_data->StartPhyNum + i;
2887 reason_code = event_data->PHY[i].PhyStatus &
2888 MPI2_EVENT_SAS_TOPO_RC_MASK;
2889 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING)
2890 _scsih_block_io_device(ioc, handle);
2891 }
2892}
2893
2894/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302895 * _scsih_tm_tr_send - send task management request
2896 * @ioc: per adapter object
2897 * @handle: device handle
2898 * Context: interrupt time.
2899 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002900 * This code is to initiate the device removal handshake protocol
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302901 * with controller firmware. This function will issue target reset
2902 * using high priority request queue. It will send a sas iounit
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002903 * control request (MPI2_SAS_OP_REMOVE_DEVICE) from this completion.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302904 *
2905 * This is designed to send muliple task management request at the same
2906 * time to the fifo. If the fifo is full, we will append the request,
2907 * and process it in a future completion.
2908 */
2909static void
2910_scsih_tm_tr_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
2911{
2912 Mpi2SCSITaskManagementRequest_t *mpi_request;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302913 u16 smid;
2914 struct _sas_device *sas_device;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302915 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302916 unsigned long flags;
2917 struct _tr_list *delayed_tr;
2918
Eric Moore3cb54692010-07-08 14:44:34 -06002919 if (ioc->shost_recovery || ioc->remove_host ||
2920 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05302921 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
2922 "progress!\n", __func__, ioc->name));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302923 return;
2924 }
2925
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302926 /* if PD, then return */
2927 if (test_bit(handle, ioc->pd_handles))
2928 return;
2929
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302930 spin_lock_irqsave(&ioc->sas_device_lock, flags);
2931 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05302932 if (sas_device && sas_device->starget &&
2933 sas_device->starget->hostdata) {
2934 sas_target_priv_data = sas_device->starget->hostdata;
2935 sas_target_priv_data->deleted = 1;
2936 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2937 "setting delete flag: handle(0x%04x), "
2938 "sas_addr(0x%016llx)\n", ioc->name, handle,
2939 (unsigned long long) sas_device->sas_address));
Kashyap, Desai1278b112010-03-09 17:34:13 +05302940 }
2941 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302942
2943 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_cb_idx);
2944 if (!smid) {
2945 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
2946 if (!delayed_tr)
2947 return;
2948 INIT_LIST_HEAD(&delayed_tr->list);
2949 delayed_tr->handle = handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05302950 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
2951 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2952 "DELAYED:tr:handle(0x%04x), (open)\n",
2953 ioc->name, handle));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302954 return;
2955 }
2956
Kashyap, Desai1278b112010-03-09 17:34:13 +05302957 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
2958 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
2959 ioc->tm_tr_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302960 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
2961 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
2962 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
2963 mpi_request->DevHandle = cpu_to_le16(handle);
2964 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302965 mpt2sas_base_put_smid_hi_priority(ioc, smid);
2966}
2967
2968
2969
2970/**
2971 * _scsih_sas_control_complete - completion routine
2972 * @ioc: per adapter object
2973 * @smid: system request message index
2974 * @msix_index: MSIX table index supplied by the OS
2975 * @reply: reply message frame(lower 32bit addr)
2976 * Context: interrupt time.
2977 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002978 * This is the sas iounit control completion routine.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302979 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002980 * handshake protocol with controller firmware.
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302981 *
2982 * Return 1 meaning mf should be freed from _base_interrupt
2983 * 0 means the mf is freed from this function.
2984 */
2985static u8
2986_scsih_sas_control_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
2987 u8 msix_index, u32 reply)
2988{
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302989#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302990 Mpi2SasIoUnitControlReply_t *mpi_reply =
2991 mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desai363fa50f2010-11-13 04:29:20 +05302992#endif
Kashyap, Desai1278b112010-03-09 17:34:13 +05302993 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
2994 "sc_complete:handle(0x%04x), (open) "
2995 "smid(%d), ioc_status(0x%04x), loginfo(0x%08x)\n",
2996 ioc->name, le16_to_cpu(mpi_reply->DevHandle), smid,
2997 le16_to_cpu(mpi_reply->IOCStatus),
2998 le32_to_cpu(mpi_reply->IOCLogInfo)));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05302999 return 1;
3000}
3001
3002/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303003 * _scsih_tm_tr_volume_send - send target reset request for volumes
3004 * @ioc: per adapter object
3005 * @handle: device handle
3006 * Context: interrupt time.
3007 *
3008 * This is designed to send muliple task management request at the same
3009 * time to the fifo. If the fifo is full, we will append the request,
3010 * and process it in a future completion.
3011 */
3012static void
3013_scsih_tm_tr_volume_send(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3014{
3015 Mpi2SCSITaskManagementRequest_t *mpi_request;
3016 u16 smid;
3017 struct _tr_list *delayed_tr;
3018
Eric Moore3cb54692010-07-08 14:44:34 -06003019 if (ioc->shost_recovery || ioc->remove_host ||
3020 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303021 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3022 "progress!\n", __func__, ioc->name));
3023 return;
3024 }
3025
3026 smid = mpt2sas_base_get_smid_hpr(ioc, ioc->tm_tr_volume_cb_idx);
3027 if (!smid) {
3028 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3029 if (!delayed_tr)
3030 return;
3031 INIT_LIST_HEAD(&delayed_tr->list);
3032 delayed_tr->handle = handle;
3033 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_volume_list);
3034 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3035 "DELAYED:tr:handle(0x%04x), (open)\n",
3036 ioc->name, handle));
3037 return;
3038 }
3039
3040 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "tr_send:handle(0x%04x), "
3041 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid,
3042 ioc->tm_tr_volume_cb_idx));
3043 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3044 memset(mpi_request, 0, sizeof(Mpi2SCSITaskManagementRequest_t));
3045 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3046 mpi_request->DevHandle = cpu_to_le16(handle);
3047 mpi_request->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3048 mpt2sas_base_put_smid_hi_priority(ioc, smid);
3049}
3050
3051/**
3052 * _scsih_tm_volume_tr_complete - target reset completion
3053 * @ioc: per adapter object
3054 * @smid: system request message index
3055 * @msix_index: MSIX table index supplied by the OS
3056 * @reply: reply message frame(lower 32bit addr)
3057 * Context: interrupt time.
3058 *
3059 * Return 1 meaning mf should be freed from _base_interrupt
3060 * 0 means the mf is freed from this function.
3061 */
3062static u8
3063_scsih_tm_volume_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid,
3064 u8 msix_index, u32 reply)
3065{
3066 u16 handle;
3067 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
3068 Mpi2SCSITaskManagementReply_t *mpi_reply =
3069 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3070
Eric Moore3cb54692010-07-08 14:44:34 -06003071 if (ioc->shost_recovery || ioc->remove_host ||
3072 ioc->pci_error_recovery) {
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303073 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3074 "progress!\n", __func__, ioc->name));
3075 return 1;
3076 }
3077
3078 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3079 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3080 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3081 dewtprintk(ioc, printk("spurious interrupt: "
3082 "handle(0x%04x:0x%04x), smid(%d)!!!\n", handle,
3083 le16_to_cpu(mpi_reply->DevHandle), smid));
3084 return 0;
3085 }
3086
3087 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3088 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3089 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3090 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3091 le32_to_cpu(mpi_reply->IOCLogInfo),
3092 le32_to_cpu(mpi_reply->TerminationCount)));
3093
3094 return _scsih_check_for_pending_tm(ioc, smid);
3095}
3096
3097/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303098 * _scsih_tm_tr_complete -
3099 * @ioc: per adapter object
3100 * @smid: system request message index
3101 * @msix_index: MSIX table index supplied by the OS
3102 * @reply: reply message frame(lower 32bit addr)
3103 * Context: interrupt time.
3104 *
3105 * This is the target reset completion routine.
3106 * This code is part of the code to initiate the device removal
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003107 * handshake protocol with controller firmware.
3108 * It will send a sas iounit control request (MPI2_SAS_OP_REMOVE_DEVICE)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303109 *
3110 * Return 1 meaning mf should be freed from _base_interrupt
3111 * 0 means the mf is freed from this function.
3112 */
3113static u8
3114_scsih_tm_tr_complete(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
3115 u32 reply)
3116{
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303117 u16 handle;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303118 Mpi2SCSITaskManagementRequest_t *mpi_request_tm;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303119 Mpi2SCSITaskManagementReply_t *mpi_reply =
3120 mpt2sas_base_get_reply_virt_addr(ioc, reply);
3121 Mpi2SasIoUnitControlRequest_t *mpi_request;
3122 u16 smid_sas_ctrl;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303123
Eric Moore3cb54692010-07-08 14:44:34 -06003124 if (ioc->shost_recovery || ioc->remove_host ||
3125 ioc->pci_error_recovery) {
Kashyap, Desai1278b112010-03-09 17:34:13 +05303126 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: host reset in "
3127 "progress!\n", __func__, ioc->name));
3128 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303129 }
3130
Kashyap, Desai1278b112010-03-09 17:34:13 +05303131 mpi_request_tm = mpt2sas_base_get_msg_frame(ioc, smid);
3132 handle = le16_to_cpu(mpi_request_tm->DevHandle);
3133 if (handle != le16_to_cpu(mpi_reply->DevHandle)) {
3134 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "spurious interrupt: "
3135 "handle(0x%04x:0x%04x), smid(%d)!!!\n", ioc->name, handle,
3136 le16_to_cpu(mpi_reply->DevHandle), smid));
3137 return 0;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303138 }
3139
Kashyap, Desai1278b112010-03-09 17:34:13 +05303140 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3141 "tr_complete:handle(0x%04x), (open) smid(%d), ioc_status(0x%04x), "
3142 "loginfo(0x%08x), completed(%d)\n", ioc->name,
3143 handle, smid, le16_to_cpu(mpi_reply->IOCStatus),
3144 le32_to_cpu(mpi_reply->IOCLogInfo),
3145 le32_to_cpu(mpi_reply->TerminationCount)));
3146
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303147 smid_sas_ctrl = mpt2sas_base_get_smid(ioc, ioc->tm_sas_control_cb_idx);
3148 if (!smid_sas_ctrl) {
3149 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3150 ioc->name, __func__);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303151 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303152 }
3153
Kashyap, Desai1278b112010-03-09 17:34:13 +05303154 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "sc_send:handle(0x%04x), "
3155 "(open), smid(%d), cb(%d)\n", ioc->name, handle, smid_sas_ctrl,
3156 ioc->tm_sas_control_cb_idx));
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303157 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid_sas_ctrl);
3158 memset(mpi_request, 0, sizeof(Mpi2SasIoUnitControlRequest_t));
3159 mpi_request->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
3160 mpi_request->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303161 mpi_request->DevHandle = mpi_request_tm->DevHandle;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303162 mpt2sas_base_put_smid_default(ioc, smid_sas_ctrl);
Kashyap, Desai1278b112010-03-09 17:34:13 +05303163
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303164 return _scsih_check_for_pending_tm(ioc, smid);
3165}
3166
3167/**
3168 * _scsih_check_for_pending_tm - check for pending task management
3169 * @ioc: per adapter object
3170 * @smid: system request message index
3171 *
3172 * This will check delayed target reset list, and feed the
3173 * next reqeust.
3174 *
3175 * Return 1 meaning mf should be freed from _base_interrupt
3176 * 0 means the mf is freed from this function.
3177 */
3178static u8
3179_scsih_check_for_pending_tm(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3180{
3181 struct _tr_list *delayed_tr;
3182
3183 if (!list_empty(&ioc->delayed_tr_volume_list)) {
3184 delayed_tr = list_entry(ioc->delayed_tr_volume_list.next,
3185 struct _tr_list, list);
3186 mpt2sas_base_free_smid(ioc, smid);
3187 _scsih_tm_tr_volume_send(ioc, delayed_tr->handle);
3188 list_del(&delayed_tr->list);
3189 kfree(delayed_tr);
3190 return 0;
3191 }
3192
Kashyap, Desai1278b112010-03-09 17:34:13 +05303193 if (!list_empty(&ioc->delayed_tr_list)) {
3194 delayed_tr = list_entry(ioc->delayed_tr_list.next,
3195 struct _tr_list, list);
3196 mpt2sas_base_free_smid(ioc, smid);
3197 _scsih_tm_tr_send(ioc, delayed_tr->handle);
3198 list_del(&delayed_tr->list);
3199 kfree(delayed_tr);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303200 return 0;
Kashyap, Desai1278b112010-03-09 17:34:13 +05303201 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303202
Kashyap, Desai1278b112010-03-09 17:34:13 +05303203 return 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303204}
3205
3206/**
Eric Moore635374e2009-03-09 01:21:12 -06003207 * _scsih_check_topo_delete_events - sanity check on topo events
3208 * @ioc: per adapter object
3209 * @event_data: the event data payload
3210 *
3211 * This routine added to better handle cable breaker.
3212 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003213 * This handles the case where driver receives multiple expander
Eric Moore635374e2009-03-09 01:21:12 -06003214 * add and delete events in a single shot. When there is a delete event
3215 * the routine will void any pending add events waiting in the event queue.
3216 *
3217 * Return nothing.
3218 */
3219static void
3220_scsih_check_topo_delete_events(struct MPT2SAS_ADAPTER *ioc,
3221 Mpi2EventDataSasTopologyChangeList_t *event_data)
3222{
3223 struct fw_event_work *fw_event;
3224 Mpi2EventDataSasTopologyChangeList_t *local_event_data;
3225 u16 expander_handle;
3226 struct _sas_node *sas_expander;
3227 unsigned long flags;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303228 int i, reason_code;
3229 u16 handle;
3230
3231 for (i = 0 ; i < event_data->NumEntries; i++) {
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05303232 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
3233 if (!handle)
3234 continue;
3235 reason_code = event_data->PHY[i].PhyStatus &
3236 MPI2_EVENT_SAS_TOPO_RC_MASK;
3237 if (reason_code == MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING)
3238 _scsih_tm_tr_send(ioc, handle);
3239 }
Eric Moore635374e2009-03-09 01:21:12 -06003240
3241 expander_handle = le16_to_cpu(event_data->ExpanderDevHandle);
3242 if (expander_handle < ioc->sas_hba.num_phys) {
3243 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3244 return;
3245 }
3246
3247 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING
3248 || event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING) {
3249 spin_lock_irqsave(&ioc->sas_node_lock, flags);
3250 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
3251 expander_handle);
3252 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
3253 _scsih_block_io_to_children_attached_to_ex(ioc, sas_expander);
3254 } else if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_RESPONDING)
3255 _scsih_block_io_to_children_attached_directly(ioc, event_data);
3256
3257 if (event_data->ExpStatus != MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING)
3258 return;
3259
3260 /* mark ignore flag for pending events */
3261 spin_lock_irqsave(&ioc->fw_event_lock, flags);
3262 list_for_each_entry(fw_event, &ioc->fw_event_list, list) {
3263 if (fw_event->event != MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST ||
3264 fw_event->ignore)
3265 continue;
3266 local_event_data = fw_event->event_data;
3267 if (local_event_data->ExpStatus ==
3268 MPI2_EVENT_SAS_TOPO_ES_ADDED ||
3269 local_event_data->ExpStatus ==
3270 MPI2_EVENT_SAS_TOPO_ES_RESPONDING) {
3271 if (le16_to_cpu(local_event_data->ExpanderDevHandle) ==
3272 expander_handle) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05303273 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06003274 "setting ignoring flag\n", ioc->name));
3275 fw_event->ignore = 1;
3276 }
3277 }
3278 }
3279 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
3280}
3281
3282/**
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303283 * _scsih_set_volume_delete_flag - setting volume delete flag
3284 * @ioc: per adapter object
3285 * @handle: device handle
3286 *
3287 * This
3288 * Return nothing.
3289 */
3290static void
3291_scsih_set_volume_delete_flag(struct MPT2SAS_ADAPTER *ioc, u16 handle)
3292{
3293 struct _raid_device *raid_device;
3294 struct MPT2SAS_TARGET *sas_target_priv_data;
3295 unsigned long flags;
3296
3297 spin_lock_irqsave(&ioc->raid_device_lock, flags);
3298 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
3299 if (raid_device && raid_device->starget &&
3300 raid_device->starget->hostdata) {
3301 sas_target_priv_data =
3302 raid_device->starget->hostdata;
3303 sas_target_priv_data->deleted = 1;
3304 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3305 "setting delete flag: handle(0x%04x), "
3306 "wwid(0x%016llx)\n", ioc->name, handle,
3307 (unsigned long long) raid_device->wwid));
3308 }
3309 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
3310}
3311
3312/**
3313 * _scsih_set_volume_handle_for_tr - set handle for target reset to volume
3314 * @handle: input handle
3315 * @a: handle for volume a
3316 * @b: handle for volume b
3317 *
3318 * IR firmware only supports two raid volumes. The purpose of this
3319 * routine is to set the volume handle in either a or b. When the given
3320 * input handle is non-zero, or when a and b have not been set before.
3321 */
3322static void
3323_scsih_set_volume_handle_for_tr(u16 handle, u16 *a, u16 *b)
3324{
3325 if (!handle || handle == *a || handle == *b)
3326 return;
3327 if (!*a)
3328 *a = handle;
3329 else if (!*b)
3330 *b = handle;
3331}
3332
3333/**
3334 * _scsih_check_ir_config_unhide_events - check for UNHIDE events
3335 * @ioc: per adapter object
3336 * @event_data: the event data payload
3337 * Context: interrupt time.
3338 *
3339 * This routine will send target reset to volume, followed by target
3340 * resets to the PDs. This is called when a PD has been removed, or
3341 * volume has been deleted or removed. When the target reset is sent
3342 * to volume, the PD target resets need to be queued to start upon
3343 * completion of the volume target reset.
3344 *
3345 * Return nothing.
3346 */
3347static void
3348_scsih_check_ir_config_unhide_events(struct MPT2SAS_ADAPTER *ioc,
3349 Mpi2EventDataIrConfigChangeList_t *event_data)
3350{
3351 Mpi2EventIrConfigElement_t *element;
3352 int i;
3353 u16 handle, volume_handle, a, b;
3354 struct _tr_list *delayed_tr;
3355
3356 a = 0;
3357 b = 0;
3358
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303359 if (ioc->is_warpdrive)
3360 return;
3361
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05303362 /* Volume Resets for Deleted or Removed */
3363 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3364 for (i = 0; i < event_data->NumElements; i++, element++) {
3365 if (element->ReasonCode ==
3366 MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED ||
3367 element->ReasonCode ==
3368 MPI2_EVENT_IR_CHANGE_RC_REMOVED) {
3369 volume_handle = le16_to_cpu(element->VolDevHandle);
3370 _scsih_set_volume_delete_flag(ioc, volume_handle);
3371 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3372 }
3373 }
3374
3375 /* Volume Resets for UNHIDE events */
3376 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3377 for (i = 0; i < event_data->NumElements; i++, element++) {
3378 if (le32_to_cpu(event_data->Flags) &
3379 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG)
3380 continue;
3381 if (element->ReasonCode == MPI2_EVENT_IR_CHANGE_RC_UNHIDE) {
3382 volume_handle = le16_to_cpu(element->VolDevHandle);
3383 _scsih_set_volume_handle_for_tr(volume_handle, &a, &b);
3384 }
3385 }
3386
3387 if (a)
3388 _scsih_tm_tr_volume_send(ioc, a);
3389 if (b)
3390 _scsih_tm_tr_volume_send(ioc, b);
3391
3392 /* PD target resets */
3393 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
3394 for (i = 0; i < event_data->NumElements; i++, element++) {
3395 if (element->ReasonCode != MPI2_EVENT_IR_CHANGE_RC_UNHIDE)
3396 continue;
3397 handle = le16_to_cpu(element->PhysDiskDevHandle);
3398 volume_handle = le16_to_cpu(element->VolDevHandle);
3399 clear_bit(handle, ioc->pd_handles);
3400 if (!volume_handle)
3401 _scsih_tm_tr_send(ioc, handle);
3402 else if (volume_handle == a || volume_handle == b) {
3403 delayed_tr = kzalloc(sizeof(*delayed_tr), GFP_ATOMIC);
3404 BUG_ON(!delayed_tr);
3405 INIT_LIST_HEAD(&delayed_tr->list);
3406 delayed_tr->handle = handle;
3407 list_add_tail(&delayed_tr->list, &ioc->delayed_tr_list);
3408 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT
3409 "DELAYED:tr:handle(0x%04x), (open)\n", ioc->name,
3410 handle));
3411 } else
3412 _scsih_tm_tr_send(ioc, handle);
3413 }
3414}
3415
3416
3417/**
3418 * _scsih_check_volume_delete_events - set delete flag for volumes
3419 * @ioc: per adapter object
3420 * @event_data: the event data payload
3421 * Context: interrupt time.
3422 *
3423 * This will handle the case when the cable connected to entire volume is
3424 * pulled. We will take care of setting the deleted flag so normal IO will
3425 * not be sent.
3426 *
3427 * Return nothing.
3428 */
3429static void
3430_scsih_check_volume_delete_events(struct MPT2SAS_ADAPTER *ioc,
3431 Mpi2EventDataIrVolume_t *event_data)
3432{
3433 u32 state;
3434
3435 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
3436 return;
3437 state = le32_to_cpu(event_data->NewValue);
3438 if (state == MPI2_RAID_VOL_STATE_MISSING || state ==
3439 MPI2_RAID_VOL_STATE_FAILED)
3440 _scsih_set_volume_delete_flag(ioc,
3441 le16_to_cpu(event_data->VolDevHandle));
3442}
3443
3444/**
Eric Moore635374e2009-03-09 01:21:12 -06003445 * _scsih_flush_running_cmds - completing outstanding commands.
3446 * @ioc: per adapter object
3447 *
3448 * The flushing out of all pending scmd commands following host reset,
3449 * where all IO is dropped to the floor.
3450 *
3451 * Return nothing.
3452 */
3453static void
3454_scsih_flush_running_cmds(struct MPT2SAS_ADAPTER *ioc)
3455{
3456 struct scsi_cmnd *scmd;
3457 u16 smid;
3458 u16 count = 0;
3459
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303460 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Kashyap, Desaiec07a052011-01-05 17:54:32 +05303461 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003462 if (!scmd)
3463 continue;
3464 count++;
3465 mpt2sas_base_free_smid(ioc, smid);
3466 scsi_dma_unmap(scmd);
Eric Moore3cb54692010-07-08 14:44:34 -06003467 if (ioc->pci_error_recovery)
3468 scmd->result = DID_NO_CONNECT << 16;
3469 else
3470 scmd->result = DID_RESET << 16;
Eric Moore635374e2009-03-09 01:21:12 -06003471 scmd->scsi_done(scmd);
3472 }
3473 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "completing %d cmds\n",
3474 ioc->name, count));
3475}
3476
3477/**
Eric Moore3c621b32009-05-18 12:59:41 -06003478 * _scsih_setup_eedp - setup MPI request for EEDP transfer
3479 * @scmd: pointer to scsi command object
3480 * @mpi_request: pointer to the SCSI_IO reqest message frame
3481 *
3482 * Supporting protection 1 and 3.
3483 *
3484 * Returns nothing
3485 */
3486static void
3487_scsih_setup_eedp(struct scsi_cmnd *scmd, Mpi2SCSIIORequest_t *mpi_request)
3488{
3489 u16 eedp_flags;
3490 unsigned char prot_op = scsi_get_prot_op(scmd);
3491 unsigned char prot_type = scsi_get_prot_type(scmd);
3492
Eric Moored334aa72010-04-22 10:47:40 -06003493 if (prot_type == SCSI_PROT_DIF_TYPE0 || prot_op == SCSI_PROT_NORMAL)
Eric Moore3c621b32009-05-18 12:59:41 -06003494 return;
3495
3496 if (prot_op == SCSI_PROT_READ_STRIP)
3497 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP;
3498 else if (prot_op == SCSI_PROT_WRITE_INSERT)
3499 eedp_flags = MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
3500 else
3501 return;
3502
Eric Moore3c621b32009-05-18 12:59:41 -06003503 switch (prot_type) {
3504 case SCSI_PROT_DIF_TYPE1:
3505
3506 /*
3507 * enable ref/guard checking
3508 * auto increment ref tag
3509 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303510 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Eric Moore3c621b32009-05-18 12:59:41 -06003511 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3512 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
3513 mpi_request->CDB.EEDP32.PrimaryReferenceTag =
3514 cpu_to_be32(scsi_get_lba(scmd));
Eric Moored334aa72010-04-22 10:47:40 -06003515 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003516
Eric Moored334aa72010-04-22 10:47:40 -06003517 case SCSI_PROT_DIF_TYPE2:
3518
3519 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
3520 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
3521 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003522 break;
3523
3524 case SCSI_PROT_DIF_TYPE3:
3525
3526 /*
3527 * enable guard checking
3528 */
Kashyap, Desai463217b2009-10-05 15:53:06 +05303529 eedp_flags |= MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
Eric Moore3c621b32009-05-18 12:59:41 -06003530 break;
3531 }
Kashyap, Desai463217b2009-10-05 15:53:06 +05303532 mpi_request->EEDPBlockSize = cpu_to_le32(scmd->device->sector_size);
3533 mpi_request->EEDPFlags = cpu_to_le16(eedp_flags);
Eric Moore3c621b32009-05-18 12:59:41 -06003534}
3535
3536/**
3537 * _scsih_eedp_error_handling - return sense code for EEDP errors
3538 * @scmd: pointer to scsi command object
3539 * @ioc_status: ioc status
3540 *
3541 * Returns nothing
3542 */
3543static void
3544_scsih_eedp_error_handling(struct scsi_cmnd *scmd, u16 ioc_status)
3545{
3546 u8 ascq;
3547 u8 sk;
3548 u8 host_byte;
3549
3550 switch (ioc_status) {
3551 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3552 ascq = 0x01;
3553 break;
3554 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3555 ascq = 0x02;
3556 break;
3557 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3558 ascq = 0x03;
3559 break;
3560 default:
3561 ascq = 0x00;
3562 break;
3563 }
3564
3565 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3566 sk = ILLEGAL_REQUEST;
3567 host_byte = DID_ABORT;
3568 } else {
3569 sk = ABORTED_COMMAND;
3570 host_byte = DID_OK;
3571 }
3572
3573 scsi_build_sense_buffer(0, scmd->sense_buffer, sk, 0x10, ascq);
3574 scmd->result = DRIVER_SENSE << 24 | (host_byte << 16) |
3575 SAM_STAT_CHECK_CONDITION;
3576}
3577
3578/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303579 * _scsih_scsi_direct_io_get - returns direct io flag
3580 * @ioc: per adapter object
3581 * @smid: system request message index
3582 *
3583 * Returns the smid stored scmd pointer.
3584 */
3585static inline u8
3586_scsih_scsi_direct_io_get(struct MPT2SAS_ADAPTER *ioc, u16 smid)
3587{
3588 return ioc->scsi_lookup[smid - 1].direct_io;
3589}
3590
3591/**
3592 * _scsih_scsi_direct_io_set - sets direct io flag
3593 * @ioc: per adapter object
3594 * @smid: system request message index
3595 * @direct_io: Zero or non-zero value to set in the direct_io flag
3596 *
3597 * Returns Nothing.
3598 */
3599static inline void
3600_scsih_scsi_direct_io_set(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 direct_io)
3601{
3602 ioc->scsi_lookup[smid - 1].direct_io = direct_io;
3603}
3604
3605
3606/**
3607 * _scsih_setup_direct_io - setup MPI request for WARPDRIVE Direct I/O
3608 * @ioc: per adapter object
3609 * @scmd: pointer to scsi command object
3610 * @raid_device: pointer to raid device data structure
3611 * @mpi_request: pointer to the SCSI_IO reqest message frame
3612 * @smid: system request message index
3613 *
3614 * Returns nothing
3615 */
3616static void
3617_scsih_setup_direct_io(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3618 struct _raid_device *raid_device, Mpi2SCSIIORequest_t *mpi_request,
3619 u16 smid)
3620{
3621 u32 v_lba, p_lba, stripe_off, stripe_unit, column, io_size;
3622 u32 stripe_sz, stripe_exp;
3623 u8 num_pds, *cdb_ptr, *tmp_ptr, *lba_ptr1, *lba_ptr2;
3624 u8 cdb0 = scmd->cmnd[0];
3625
3626 /*
3627 * Try Direct I/O to RAID memeber disks
3628 */
3629 if (cdb0 == READ_16 || cdb0 == READ_10 ||
3630 cdb0 == WRITE_16 || cdb0 == WRITE_10) {
3631 cdb_ptr = mpi_request->CDB.CDB32;
3632
3633 if ((cdb0 < READ_16) || !(cdb_ptr[2] | cdb_ptr[3] | cdb_ptr[4]
3634 | cdb_ptr[5])) {
3635 io_size = scsi_bufflen(scmd) >> 9;
3636 /* get virtual lba */
3637 lba_ptr1 = lba_ptr2 = (cdb0 < READ_16) ? &cdb_ptr[2] :
3638 &cdb_ptr[6];
3639 tmp_ptr = (u8 *)&v_lba + 3;
3640 *tmp_ptr-- = *lba_ptr1++;
3641 *tmp_ptr-- = *lba_ptr1++;
3642 *tmp_ptr-- = *lba_ptr1++;
3643 *tmp_ptr = *lba_ptr1;
3644
3645 if (((u64)v_lba + (u64)io_size - 1) <=
3646 (u32)raid_device->max_lba) {
3647 stripe_sz = raid_device->stripe_sz;
3648 stripe_exp = raid_device->stripe_exponent;
3649 stripe_off = v_lba & (stripe_sz - 1);
3650
3651 /* Check whether IO falls within a stripe */
3652 if ((stripe_off + io_size) <= stripe_sz) {
3653 num_pds = raid_device->num_pds;
3654 p_lba = v_lba >> stripe_exp;
3655 stripe_unit = p_lba / num_pds;
3656 column = p_lba % num_pds;
3657 p_lba = (stripe_unit << stripe_exp) +
3658 stripe_off;
3659 mpi_request->DevHandle =
3660 cpu_to_le16(raid_device->
3661 pd_handle[column]);
3662 tmp_ptr = (u8 *)&p_lba + 3;
3663 *lba_ptr2++ = *tmp_ptr--;
3664 *lba_ptr2++ = *tmp_ptr--;
3665 *lba_ptr2++ = *tmp_ptr--;
3666 *lba_ptr2 = *tmp_ptr;
3667 /*
3668 * WD: To indicate this I/O is directI/O
3669 */
3670 _scsih_scsi_direct_io_set(ioc, smid, 1);
3671 }
3672 }
3673 }
3674 }
3675}
3676
3677/**
Eric Moored5d135b2009-05-18 13:02:08 -06003678 * _scsih_qcmd - main scsi request entry point
Eric Moore635374e2009-03-09 01:21:12 -06003679 * @scmd: pointer to scsi command object
3680 * @done: function pointer to be invoked on completion
3681 *
3682 * The callback index is set inside `ioc->scsi_io_cb_idx`.
3683 *
3684 * Returns 0 on success. If there's a failure, return either:
3685 * SCSI_MLQUEUE_DEVICE_BUSY if the device queue is full, or
3686 * SCSI_MLQUEUE_HOST_BUSY if the entire host queue is full
3687 */
3688static int
Jeff Garzikf2812332010-11-16 02:10:29 -05003689_scsih_qcmd_lck(struct scsi_cmnd *scmd, void (*done)(struct scsi_cmnd *))
Eric Moore635374e2009-03-09 01:21:12 -06003690{
3691 struct MPT2SAS_ADAPTER *ioc = shost_priv(scmd->device->host);
3692 struct MPT2SAS_DEVICE *sas_device_priv_data;
3693 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303694 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06003695 Mpi2SCSIIORequest_t *mpi_request;
3696 u32 mpi_control;
3697 u16 smid;
Eric Moore635374e2009-03-09 01:21:12 -06003698
3699 scmd->scsi_done = done;
3700 sas_device_priv_data = scmd->device->hostdata;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303701 if (!sas_device_priv_data || !sas_device_priv_data->sas_target) {
Eric Moore635374e2009-03-09 01:21:12 -06003702 scmd->result = DID_NO_CONNECT << 16;
3703 scmd->scsi_done(scmd);
3704 return 0;
3705 }
3706
Eric Moore3cb54692010-07-08 14:44:34 -06003707 if (ioc->pci_error_recovery) {
3708 scmd->result = DID_NO_CONNECT << 16;
3709 scmd->scsi_done(scmd);
3710 return 0;
3711 }
3712
Eric Moore635374e2009-03-09 01:21:12 -06003713 sas_target_priv_data = sas_device_priv_data->sas_target;
Kashyap, Desai130b9582010-04-08 17:54:32 +05303714 /* invalid device handle */
3715 if (sas_target_priv_data->handle == MPT2SAS_INVALID_DEVICE_HANDLE) {
Eric Moore635374e2009-03-09 01:21:12 -06003716 scmd->result = DID_NO_CONNECT << 16;
3717 scmd->scsi_done(scmd);
3718 return 0;
3719 }
3720
Kashyap, Desai130b9582010-04-08 17:54:32 +05303721 /* host recovery or link resets sent via IOCTLs */
3722 if (ioc->shost_recovery || ioc->ioc_link_reset_in_progress)
Eric Moore635374e2009-03-09 01:21:12 -06003723 return SCSI_MLQUEUE_HOST_BUSY;
Uwe Kleine-König65155b32010-06-11 12:17:01 +02003724 /* device busy with task management */
Kashyap, Desai130b9582010-04-08 17:54:32 +05303725 else if (sas_device_priv_data->block || sas_target_priv_data->tm_busy)
3726 return SCSI_MLQUEUE_DEVICE_BUSY;
3727 /* device has been deleted */
3728 else if (sas_target_priv_data->deleted) {
3729 scmd->result = DID_NO_CONNECT << 16;
3730 scmd->scsi_done(scmd);
3731 return 0;
3732 }
Eric Moore635374e2009-03-09 01:21:12 -06003733
3734 if (scmd->sc_data_direction == DMA_FROM_DEVICE)
3735 mpi_control = MPI2_SCSIIO_CONTROL_READ;
3736 else if (scmd->sc_data_direction == DMA_TO_DEVICE)
3737 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
3738 else
3739 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
3740
3741 /* set tags */
3742 if (!(sas_device_priv_data->flags & MPT_DEVICE_FLAGS_INIT)) {
3743 if (scmd->device->tagged_supported) {
3744 if (scmd->device->ordered_tags)
3745 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
3746 else
3747 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
3748 } else
3749/* MPI Revision I (UNIT = 0xA) - removed MPI2_SCSIIO_CONTROL_UNTAGGED */
3750/* mpi_control |= MPI2_SCSIIO_CONTROL_UNTAGGED;
3751 */
3752 mpi_control |= (0x500);
3753
3754 } else
3755 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303756 /* Make sure Device is not raid volume.
3757 * We do not expose raid functionality to upper layer for warpdrive.
3758 */
3759 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Eric Moored334aa72010-04-22 10:47:40 -06003760 sas_is_tlr_enabled(scmd->device) && scmd->cmd_len != 32)
Eric Moore635374e2009-03-09 01:21:12 -06003761 mpi_control |= MPI2_SCSIIO_CONTROL_TLR_ON;
3762
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05303763 smid = mpt2sas_base_get_smid_scsiio(ioc, ioc->scsi_io_cb_idx, scmd);
Eric Moore635374e2009-03-09 01:21:12 -06003764 if (!smid) {
3765 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
3766 ioc->name, __func__);
3767 goto out;
3768 }
3769 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
3770 memset(mpi_request, 0, sizeof(Mpi2SCSIIORequest_t));
Eric Moore3c621b32009-05-18 12:59:41 -06003771 _scsih_setup_eedp(scmd, mpi_request);
Eric Moored334aa72010-04-22 10:47:40 -06003772 if (scmd->cmd_len == 32)
3773 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
Eric Moore635374e2009-03-09 01:21:12 -06003774 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3775 if (sas_device_priv_data->sas_target->flags &
3776 MPT_TARGET_FLAGS_RAID_COMPONENT)
3777 mpi_request->Function = MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
3778 else
3779 mpi_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3780 mpi_request->DevHandle =
3781 cpu_to_le16(sas_device_priv_data->sas_target->handle);
3782 mpi_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3783 mpi_request->Control = cpu_to_le32(mpi_control);
3784 mpi_request->IoFlags = cpu_to_le16(scmd->cmd_len);
3785 mpi_request->MsgFlags = MPI2_SCSIIO_MSGFLAGS_SYSTEM_SENSE_ADDR;
3786 mpi_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3787 mpi_request->SenseBufferLowAddress =
Kashyap, Desaiec9472c2009-09-23 17:34:13 +05303788 mpt2sas_base_get_sense_buffer_dma(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003789 mpi_request->SGLOffset0 = offsetof(Mpi2SCSIIORequest_t, SGL) / 4;
3790 mpi_request->SGLFlags = cpu_to_le16(MPI2_SCSIIO_SGLFLAGS_TYPE_MPI +
3791 MPI2_SCSIIO_SGLFLAGS_SYSTEM_ADDR);
Kashyap, Desai7b936b02009-09-25 11:44:41 +05303792 mpi_request->VF_ID = 0; /* TODO */
3793 mpi_request->VP_ID = 0;
Eric Moore635374e2009-03-09 01:21:12 -06003794 int_to_scsilun(sas_device_priv_data->lun, (struct scsi_lun *)
3795 mpi_request->LUN);
3796 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
3797
3798 if (!mpi_request->DataLength) {
3799 mpt2sas_base_build_zero_len_sge(ioc, &mpi_request->SGL);
3800 } else {
3801 if (_scsih_build_scatter_gather(ioc, scmd, smid)) {
3802 mpt2sas_base_free_smid(ioc, smid);
3803 goto out;
3804 }
3805 }
3806
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303807 raid_device = sas_target_priv_data->raid_device;
3808 if (raid_device && raid_device->direct_io_enabled)
3809 _scsih_setup_direct_io(ioc, scmd, raid_device, mpi_request,
3810 smid);
3811
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303812 if (likely(mpi_request->Function == MPI2_FUNCTION_SCSI_IO_REQUEST))
3813 mpt2sas_base_put_smid_scsi_io(ioc, smid,
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303814 le16_to_cpu(mpi_request->DevHandle));
Kashyap, Desai58287fd2010-03-17 16:27:25 +05303815 else
3816 mpt2sas_base_put_smid_default(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06003817 return 0;
3818
3819 out:
3820 return SCSI_MLQUEUE_HOST_BUSY;
3821}
3822
Jeff Garzikf2812332010-11-16 02:10:29 -05003823static DEF_SCSI_QCMD(_scsih_qcmd)
3824
Eric Moore635374e2009-03-09 01:21:12 -06003825/**
3826 * _scsih_normalize_sense - normalize descriptor and fixed format sense data
3827 * @sense_buffer: sense data returned by target
3828 * @data: normalized skey/asc/ascq
3829 *
3830 * Return nothing.
3831 */
3832static void
3833_scsih_normalize_sense(char *sense_buffer, struct sense_info *data)
3834{
3835 if ((sense_buffer[0] & 0x7F) >= 0x72) {
3836 /* descriptor format */
3837 data->skey = sense_buffer[1] & 0x0F;
3838 data->asc = sense_buffer[2];
3839 data->ascq = sense_buffer[3];
3840 } else {
3841 /* fixed format */
3842 data->skey = sense_buffer[2] & 0x0F;
3843 data->asc = sense_buffer[12];
3844 data->ascq = sense_buffer[13];
3845 }
3846}
3847
3848#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
3849/**
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003850 * _scsih_scsi_ioc_info - translated non-successful SCSI_IO request
Eric Moore635374e2009-03-09 01:21:12 -06003851 * @ioc: per adapter object
3852 * @scmd: pointer to scsi command object
3853 * @mpi_reply: reply mf payload returned from firmware
3854 *
3855 * scsi_status - SCSI Status code returned from target device
3856 * scsi_state - state info associated with SCSI_IO determined by ioc
3857 * ioc_status - ioc supplied status info
3858 *
3859 * Return nothing.
3860 */
3861static void
3862_scsih_scsi_ioc_info(struct MPT2SAS_ADAPTER *ioc, struct scsi_cmnd *scmd,
3863 Mpi2SCSIIOReply_t *mpi_reply, u16 smid)
3864{
3865 u32 response_info;
3866 u8 *response_bytes;
3867 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
3868 MPI2_IOCSTATUS_MASK;
3869 u8 scsi_state = mpi_reply->SCSIState;
3870 u8 scsi_status = mpi_reply->SCSIStatus;
3871 char *desc_ioc_state = NULL;
3872 char *desc_scsi_status = NULL;
3873 char *desc_scsi_state = ioc->tmp_string;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303874 u32 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05303875 struct _sas_device *sas_device = NULL;
3876 unsigned long flags;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303877 struct scsi_target *starget = scmd->device->sdev_target;
3878 struct MPT2SAS_TARGET *priv_target = starget->hostdata;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303879 char *device_str = NULL;
Kashyap, Desai8e864a82010-06-17 13:48:46 +05303880
3881 if (!priv_target)
3882 return;
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303883
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05303884 if (ioc->hide_ir_msg)
3885 device_str = "WarpDrive";
3886 else
3887 device_str = "volume";
3888
Kashyap, Desaibe9e8cd72009-08-07 19:36:43 +05303889 if (log_info == 0x31170000)
3890 return;
Eric Moore635374e2009-03-09 01:21:12 -06003891
3892 switch (ioc_status) {
3893 case MPI2_IOCSTATUS_SUCCESS:
3894 desc_ioc_state = "success";
3895 break;
3896 case MPI2_IOCSTATUS_INVALID_FUNCTION:
3897 desc_ioc_state = "invalid function";
3898 break;
3899 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
3900 desc_ioc_state = "scsi recovered error";
3901 break;
3902 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
3903 desc_ioc_state = "scsi invalid dev handle";
3904 break;
3905 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3906 desc_ioc_state = "scsi device not there";
3907 break;
3908 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
3909 desc_ioc_state = "scsi data overrun";
3910 break;
3911 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
3912 desc_ioc_state = "scsi data underrun";
3913 break;
3914 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
3915 desc_ioc_state = "scsi io data error";
3916 break;
3917 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3918 desc_ioc_state = "scsi protocol error";
3919 break;
3920 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
3921 desc_ioc_state = "scsi task terminated";
3922 break;
3923 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3924 desc_ioc_state = "scsi residual mismatch";
3925 break;
3926 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3927 desc_ioc_state = "scsi task mgmt failed";
3928 break;
3929 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
3930 desc_ioc_state = "scsi ioc terminated";
3931 break;
3932 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
3933 desc_ioc_state = "scsi ext terminated";
3934 break;
Eric Moore3c621b32009-05-18 12:59:41 -06003935 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
3936 desc_ioc_state = "eedp guard error";
3937 break;
3938 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
3939 desc_ioc_state = "eedp ref tag error";
3940 break;
3941 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
3942 desc_ioc_state = "eedp app tag error";
3943 break;
Eric Moore635374e2009-03-09 01:21:12 -06003944 default:
3945 desc_ioc_state = "unknown";
3946 break;
3947 }
3948
3949 switch (scsi_status) {
3950 case MPI2_SCSI_STATUS_GOOD:
3951 desc_scsi_status = "good";
3952 break;
3953 case MPI2_SCSI_STATUS_CHECK_CONDITION:
3954 desc_scsi_status = "check condition";
3955 break;
3956 case MPI2_SCSI_STATUS_CONDITION_MET:
3957 desc_scsi_status = "condition met";
3958 break;
3959 case MPI2_SCSI_STATUS_BUSY:
3960 desc_scsi_status = "busy";
3961 break;
3962 case MPI2_SCSI_STATUS_INTERMEDIATE:
3963 desc_scsi_status = "intermediate";
3964 break;
3965 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
3966 desc_scsi_status = "intermediate condmet";
3967 break;
3968 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
3969 desc_scsi_status = "reservation conflict";
3970 break;
3971 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
3972 desc_scsi_status = "command terminated";
3973 break;
3974 case MPI2_SCSI_STATUS_TASK_SET_FULL:
3975 desc_scsi_status = "task set full";
3976 break;
3977 case MPI2_SCSI_STATUS_ACA_ACTIVE:
3978 desc_scsi_status = "aca active";
3979 break;
3980 case MPI2_SCSI_STATUS_TASK_ABORTED:
3981 desc_scsi_status = "task aborted";
3982 break;
3983 default:
3984 desc_scsi_status = "unknown";
3985 break;
3986 }
3987
3988 desc_scsi_state[0] = '\0';
3989 if (!scsi_state)
3990 desc_scsi_state = " ";
3991 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
3992 strcat(desc_scsi_state, "response info ");
3993 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
3994 strcat(desc_scsi_state, "state terminated ");
3995 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
3996 strcat(desc_scsi_state, "no status ");
3997 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
3998 strcat(desc_scsi_state, "autosense failed ");
3999 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
4000 strcat(desc_scsi_state, "autosense valid ");
4001
4002 scsi_print_command(scmd);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304003
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304004 if (priv_target->flags & MPT_TARGET_FLAGS_VOLUME) {
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304005 printk(MPT2SAS_WARN_FMT "\t%s wwid(0x%016llx)\n", ioc->name,
4006 device_str, (unsigned long long)priv_target->sas_address);
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304007 } else {
4008 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4009 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4010 priv_target->sas_address);
4011 if (sas_device) {
4012 printk(MPT2SAS_WARN_FMT "\tsas_address(0x%016llx), "
4013 "phy(%d)\n", ioc->name, sas_device->sas_address,
4014 sas_device->phy);
4015 printk(MPT2SAS_WARN_FMT
4016 "\tenclosure_logical_id(0x%016llx), slot(%d)\n",
4017 ioc->name, sas_device->enclosure_logical_id,
4018 sas_device->slot);
4019 }
4020 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304021 }
Kashyap, Desai7fbae672010-06-17 13:45:17 +05304022
Kashyap, Desai8e864a82010-06-17 13:48:46 +05304023 printk(MPT2SAS_WARN_FMT "\thandle(0x%04x), ioc_status(%s)(0x%04x), "
4024 "smid(%d)\n", ioc->name, le16_to_cpu(mpi_reply->DevHandle),
4025 desc_ioc_state, ioc_status, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004026 printk(MPT2SAS_WARN_FMT "\trequest_len(%d), underflow(%d), "
4027 "resid(%d)\n", ioc->name, scsi_bufflen(scmd), scmd->underflow,
4028 scsi_get_resid(scmd));
4029 printk(MPT2SAS_WARN_FMT "\ttag(%d), transfer_count(%d), "
4030 "sc->result(0x%08x)\n", ioc->name, le16_to_cpu(mpi_reply->TaskTag),
4031 le32_to_cpu(mpi_reply->TransferCount), scmd->result);
4032 printk(MPT2SAS_WARN_FMT "\tscsi_status(%s)(0x%02x), "
4033 "scsi_state(%s)(0x%02x)\n", ioc->name, desc_scsi_status,
4034 scsi_status, desc_scsi_state, scsi_state);
4035
4036 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4037 struct sense_info data;
4038 _scsih_normalize_sense(scmd->sense_buffer, &data);
4039 printk(MPT2SAS_WARN_FMT "\t[sense_key,asc,ascq]: "
Kashyap, Desaie94f6742010-03-17 16:24:52 +05304040 "[0x%02x,0x%02x,0x%02x], count(%d)\n", ioc->name, data.skey,
4041 data.asc, data.ascq, le32_to_cpu(mpi_reply->SenseCount));
Eric Moore635374e2009-03-09 01:21:12 -06004042 }
4043
4044 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
4045 response_info = le32_to_cpu(mpi_reply->ResponseInfo);
4046 response_bytes = (u8 *)&response_info;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304047 _scsih_response_code(ioc, response_bytes[0]);
Eric Moore635374e2009-03-09 01:21:12 -06004048 }
4049}
4050#endif
4051
4052/**
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304053 * _scsih_turn_on_fault_led - illuminate Fault LED
Eric Moore635374e2009-03-09 01:21:12 -06004054 * @ioc: per adapter object
4055 * @handle: device handle
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304056 * Context: process
4057 *
4058 * Return nothing.
4059 */
4060static void
4061_scsih_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4062{
4063 Mpi2SepReply_t mpi_reply;
4064 Mpi2SepRequest_t mpi_request;
4065
4066 memset(&mpi_request, 0, sizeof(Mpi2SepRequest_t));
4067 mpi_request.Function = MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR;
4068 mpi_request.Action = MPI2_SEP_REQ_ACTION_WRITE_STATUS;
4069 mpi_request.SlotStatus =
4070 cpu_to_le32(MPI2_SEP_REQ_SLOTSTATUS_PREDICTED_FAULT);
4071 mpi_request.DevHandle = cpu_to_le16(handle);
4072 mpi_request.Flags = MPI2_SEP_REQ_FLAGS_DEVHANDLE_ADDRESS;
4073 if ((mpt2sas_base_scsi_enclosure_processor(ioc, &mpi_reply,
4074 &mpi_request)) != 0) {
4075 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n", ioc->name,
4076 __FILE__, __LINE__, __func__);
4077 return;
4078 }
4079
4080 if (mpi_reply.IOCStatus || mpi_reply.IOCLogInfo) {
4081 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "enclosure_processor: "
4082 "ioc_status (0x%04x), loginfo(0x%08x)\n", ioc->name,
4083 le16_to_cpu(mpi_reply.IOCStatus),
4084 le32_to_cpu(mpi_reply.IOCLogInfo)));
4085 return;
4086 }
4087}
4088
4089/**
4090 * _scsih_send_event_to_turn_on_fault_led - fire delayed event
4091 * @ioc: per adapter object
4092 * @handle: device handle
4093 * Context: interrupt.
4094 *
4095 * Return nothing.
4096 */
4097static void
4098_scsih_send_event_to_turn_on_fault_led(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4099{
4100 struct fw_event_work *fw_event;
4101
4102 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
4103 if (!fw_event)
4104 return;
4105 fw_event->event = MPT2SAS_TURN_ON_FAULT_LED;
4106 fw_event->device_handle = handle;
4107 fw_event->ioc = ioc;
4108 _scsih_fw_event_add(ioc, fw_event);
4109}
4110
4111/**
4112 * _scsih_smart_predicted_fault - process smart errors
4113 * @ioc: per adapter object
4114 * @handle: device handle
4115 * Context: interrupt.
Eric Moore635374e2009-03-09 01:21:12 -06004116 *
4117 * Return nothing.
4118 */
4119static void
4120_scsih_smart_predicted_fault(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4121{
Eric Moore635374e2009-03-09 01:21:12 -06004122 struct scsi_target *starget;
4123 struct MPT2SAS_TARGET *sas_target_priv_data;
4124 Mpi2EventNotificationReply_t *event_reply;
4125 Mpi2EventDataSasDeviceStatusChange_t *event_data;
4126 struct _sas_device *sas_device;
4127 ssize_t sz;
4128 unsigned long flags;
4129
4130 /* only handle non-raid devices */
4131 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4132 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
4133 if (!sas_device) {
4134 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4135 return;
4136 }
4137 starget = sas_device->starget;
4138 sas_target_priv_data = starget->hostdata;
4139
4140 if ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_RAID_COMPONENT) ||
4141 ((sas_target_priv_data->flags & MPT_TARGET_FLAGS_VOLUME))) {
4142 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4143 return;
4144 }
4145 starget_printk(KERN_WARNING, starget, "predicted fault\n");
4146 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4147
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05304148 if (ioc->pdev->subsystem_vendor == PCI_VENDOR_ID_IBM)
4149 _scsih_send_event_to_turn_on_fault_led(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06004150
4151 /* insert into event log */
4152 sz = offsetof(Mpi2EventNotificationReply_t, EventData) +
4153 sizeof(Mpi2EventDataSasDeviceStatusChange_t);
4154 event_reply = kzalloc(sz, GFP_KERNEL);
4155 if (!event_reply) {
4156 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4157 ioc->name, __FILE__, __LINE__, __func__);
4158 return;
4159 }
4160
4161 event_reply->Function = MPI2_FUNCTION_EVENT_NOTIFICATION;
4162 event_reply->Event =
4163 cpu_to_le16(MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
4164 event_reply->MsgLength = sz/4;
4165 event_reply->EventDataLength =
4166 cpu_to_le16(sizeof(Mpi2EventDataSasDeviceStatusChange_t)/4);
4167 event_data = (Mpi2EventDataSasDeviceStatusChange_t *)
4168 event_reply->EventData;
4169 event_data->ReasonCode = MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA;
4170 event_data->ASC = 0x5D;
4171 event_data->DevHandle = cpu_to_le16(handle);
4172 event_data->SASAddress = cpu_to_le64(sas_target_priv_data->sas_address);
4173 mpt2sas_ctl_add_to_event_log(ioc, event_reply);
4174 kfree(event_reply);
4175}
4176
4177/**
Eric Moored5d135b2009-05-18 13:02:08 -06004178 * _scsih_io_done - scsi request callback
Eric Moore635374e2009-03-09 01:21:12 -06004179 * @ioc: per adapter object
4180 * @smid: system request message index
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304181 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06004182 * @reply: reply message frame(lower 32bit addr)
4183 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304184 * Callback handler when using _scsih_qcmd.
Eric Moore635374e2009-03-09 01:21:12 -06004185 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304186 * Return 1 meaning mf should be freed from _base_interrupt
4187 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06004188 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304189static u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05304190_scsih_io_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06004191{
4192 Mpi2SCSIIORequest_t *mpi_request;
4193 Mpi2SCSIIOReply_t *mpi_reply;
4194 struct scsi_cmnd *scmd;
4195 u16 ioc_status;
4196 u32 xfer_cnt;
4197 u8 scsi_state;
4198 u8 scsi_status;
4199 u32 log_info;
4200 struct MPT2SAS_DEVICE *sas_device_priv_data;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304201 u32 response_code = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004202
4203 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05304204 scmd = _scsih_scsi_lookup_get_clear(ioc, smid);
Eric Moore635374e2009-03-09 01:21:12 -06004205 if (scmd == NULL)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304206 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004207
4208 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
4209
4210 if (mpi_reply == NULL) {
4211 scmd->result = DID_OK << 16;
4212 goto out;
4213 }
4214
4215 sas_device_priv_data = scmd->device->hostdata;
4216 if (!sas_device_priv_data || !sas_device_priv_data->sas_target ||
4217 sas_device_priv_data->sas_target->deleted) {
4218 scmd->result = DID_NO_CONNECT << 16;
4219 goto out;
4220 }
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304221 /*
4222 * WARPDRIVE: If direct_io is set then it is directIO,
4223 * the failed direct I/O should be redirected to volume
4224 */
4225 if (_scsih_scsi_direct_io_get(ioc, smid)) {
4226 _scsih_scsi_direct_io_set(ioc, smid, 0);
4227 memcpy(mpi_request->CDB.CDB32, scmd->cmnd, scmd->cmd_len);
4228 mpi_request->DevHandle =
4229 cpu_to_le16(sas_device_priv_data->sas_target->handle);
4230 mpt2sas_base_put_smid_scsi_io(ioc, smid,
4231 sas_device_priv_data->sas_target->handle);
4232 return 0;
4233 }
4234
Eric Moore635374e2009-03-09 01:21:12 -06004235
4236 /* turning off TLR */
Kashyap, Desai9982f592009-09-23 17:23:07 +05304237 scsi_state = mpi_reply->SCSIState;
4238 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
4239 response_code =
4240 le32_to_cpu(mpi_reply->ResponseInfo) & 0xFF;
Eric Moore635374e2009-03-09 01:21:12 -06004241 if (!sas_device_priv_data->tlr_snoop_check) {
4242 sas_device_priv_data->tlr_snoop_check++;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05304243 /* Make sure Device is not raid volume.
4244 * We do not expose raid functionality to upper layer for warpdrive.
4245 */
4246 if (!ioc->is_warpdrive && !_scsih_is_raid(&scmd->device->sdev_gendev) &&
Kashyap, Desai3ed21522010-02-17 16:08:36 +05304247 sas_is_tlr_enabled(scmd->device) &&
Kashyap, Desai84f0b042009-12-16 18:56:28 +05304248 response_code == MPI2_SCSITASKMGMT_RSP_INVALID_FRAME) {
4249 sas_disable_tlr(scmd->device);
4250 sdev_printk(KERN_INFO, scmd->device, "TLR disabled\n");
4251 }
Eric Moore635374e2009-03-09 01:21:12 -06004252 }
4253
4254 xfer_cnt = le32_to_cpu(mpi_reply->TransferCount);
4255 scsi_set_resid(scmd, scsi_bufflen(scmd) - xfer_cnt);
4256 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);
4257 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE)
4258 log_info = le32_to_cpu(mpi_reply->IOCLogInfo);
4259 else
4260 log_info = 0;
4261 ioc_status &= MPI2_IOCSTATUS_MASK;
Eric Moore635374e2009-03-09 01:21:12 -06004262 scsi_status = mpi_reply->SCSIStatus;
4263
4264 if (ioc_status == MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN && xfer_cnt == 0 &&
4265 (scsi_status == MPI2_SCSI_STATUS_BUSY ||
4266 scsi_status == MPI2_SCSI_STATUS_RESERVATION_CONFLICT ||
4267 scsi_status == MPI2_SCSI_STATUS_TASK_SET_FULL)) {
4268 ioc_status = MPI2_IOCSTATUS_SUCCESS;
4269 }
4270
4271 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
4272 struct sense_info data;
4273 const void *sense_data = mpt2sas_base_get_sense_buffer(ioc,
4274 smid);
Eric Moore0d04df92009-04-21 15:38:43 -06004275 u32 sz = min_t(u32, SCSI_SENSE_BUFFERSIZE,
Eric Moore635374e2009-03-09 01:21:12 -06004276 le32_to_cpu(mpi_reply->SenseCount));
Eric Moore0d04df92009-04-21 15:38:43 -06004277 memcpy(scmd->sense_buffer, sense_data, sz);
Eric Moore635374e2009-03-09 01:21:12 -06004278 _scsih_normalize_sense(scmd->sense_buffer, &data);
4279 /* failure prediction threshold exceeded */
4280 if (data.asc == 0x5D)
4281 _scsih_smart_predicted_fault(ioc,
4282 le16_to_cpu(mpi_reply->DevHandle));
4283 }
4284
4285 switch (ioc_status) {
4286 case MPI2_IOCSTATUS_BUSY:
4287 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
4288 scmd->result = SAM_STAT_BUSY;
4289 break;
4290
4291 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
4292 scmd->result = DID_NO_CONNECT << 16;
4293 break;
4294
4295 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
4296 if (sas_device_priv_data->block) {
Kashyap, Desaie4e7c7e2009-09-23 17:33:14 +05304297 scmd->result = DID_TRANSPORT_DISRUPTED << 16;
4298 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06004299 }
Eric Moore635374e2009-03-09 01:21:12 -06004300 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
4301 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
4302 scmd->result = DID_RESET << 16;
4303 break;
4304
4305 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
4306 if ((xfer_cnt == 0) || (scmd->underflow > xfer_cnt))
4307 scmd->result = DID_SOFT_ERROR << 16;
4308 else
4309 scmd->result = (DID_OK << 16) | scsi_status;
4310 break;
4311
4312 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
4313 scmd->result = (DID_OK << 16) | scsi_status;
4314
4315 if ((scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID))
4316 break;
4317
4318 if (xfer_cnt < scmd->underflow) {
4319 if (scsi_status == SAM_STAT_BUSY)
4320 scmd->result = SAM_STAT_BUSY;
4321 else
4322 scmd->result = DID_SOFT_ERROR << 16;
4323 } else if (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4324 MPI2_SCSI_STATE_NO_SCSI_STATUS))
4325 scmd->result = DID_SOFT_ERROR << 16;
4326 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4327 scmd->result = DID_RESET << 16;
4328 else if (!xfer_cnt && scmd->cmnd[0] == REPORT_LUNS) {
4329 mpi_reply->SCSIState = MPI2_SCSI_STATE_AUTOSENSE_VALID;
4330 mpi_reply->SCSIStatus = SAM_STAT_CHECK_CONDITION;
4331 scmd->result = (DRIVER_SENSE << 24) |
4332 SAM_STAT_CHECK_CONDITION;
4333 scmd->sense_buffer[0] = 0x70;
4334 scmd->sense_buffer[2] = ILLEGAL_REQUEST;
4335 scmd->sense_buffer[12] = 0x20;
4336 scmd->sense_buffer[13] = 0;
4337 }
4338 break;
4339
4340 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
4341 scsi_set_resid(scmd, 0);
4342 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
4343 case MPI2_IOCSTATUS_SUCCESS:
4344 scmd->result = (DID_OK << 16) | scsi_status;
Kashyap, Desai9982f592009-09-23 17:23:07 +05304345 if (response_code ==
4346 MPI2_SCSITASKMGMT_RSP_INVALID_FRAME ||
4347 (scsi_state & (MPI2_SCSI_STATE_AUTOSENSE_FAILED |
4348 MPI2_SCSI_STATE_NO_SCSI_STATUS)))
Eric Moore635374e2009-03-09 01:21:12 -06004349 scmd->result = DID_SOFT_ERROR << 16;
4350 else if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
4351 scmd->result = DID_RESET << 16;
4352 break;
4353
Eric Moore3c621b32009-05-18 12:59:41 -06004354 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
4355 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
4356 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
4357 _scsih_eedp_error_handling(scmd, ioc_status);
4358 break;
Eric Moore635374e2009-03-09 01:21:12 -06004359 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
4360 case MPI2_IOCSTATUS_INVALID_FUNCTION:
4361 case MPI2_IOCSTATUS_INVALID_SGL:
4362 case MPI2_IOCSTATUS_INTERNAL_ERROR:
4363 case MPI2_IOCSTATUS_INVALID_FIELD:
4364 case MPI2_IOCSTATUS_INVALID_STATE:
4365 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
4366 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
4367 default:
4368 scmd->result = DID_SOFT_ERROR << 16;
4369 break;
4370
4371 }
4372
4373#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
4374 if (scmd->result && (ioc->logging_level & MPT_DEBUG_REPLY))
4375 _scsih_scsi_ioc_info(ioc , scmd, mpi_reply, smid);
4376#endif
4377
4378 out:
4379 scsi_dma_unmap(scmd);
4380 scmd->scsi_done(scmd);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05304381 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06004382}
4383
4384/**
Eric Moore635374e2009-03-09 01:21:12 -06004385 * _scsih_sas_host_refresh - refreshing sas host object contents
4386 * @ioc: per adapter object
Eric Moore635374e2009-03-09 01:21:12 -06004387 * Context: user
4388 *
4389 * During port enable, fw will send topology events for every device. Its
4390 * possible that the handles may change from the previous setting, so this
4391 * code keeping handles updating if changed.
4392 *
4393 * Return nothing.
4394 */
4395static void
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304396_scsih_sas_host_refresh(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06004397{
4398 u16 sz;
4399 u16 ioc_status;
4400 int i;
4401 Mpi2ConfigReply_t mpi_reply;
4402 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304403 u16 attached_handle;
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304404 u8 link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06004405
4406 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
4407 "updating handles for sas_host(0x%016llx)\n",
4408 ioc->name, (unsigned long long)ioc->sas_hba.sas_address));
4409
4410 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys
4411 * sizeof(Mpi2SasIOUnit0PhyData_t));
4412 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4413 if (!sas_iounit_pg0) {
4414 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4415 ioc->name, __FILE__, __LINE__, __func__);
4416 return;
4417 }
Eric Moore635374e2009-03-09 01:21:12 -06004418
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304419 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4420 sas_iounit_pg0, sz)) != 0)
4421 goto out;
4422 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4423 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4424 goto out;
4425 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304426 link_rate = sas_iounit_pg0->PhyData[i].NegotiatedLinkRate >> 4;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304427 if (i == 0)
4428 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4429 PhyData[0].ControllerDevHandle);
4430 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
4431 attached_handle = le16_to_cpu(sas_iounit_pg0->PhyData[i].
4432 AttachedDevHandle);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304433 if (attached_handle && link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
4434 link_rate = MPI2_SAS_NEG_LINK_RATE_1_5;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304435 mpt2sas_transport_update_links(ioc, ioc->sas_hba.sas_address,
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304436 attached_handle, i, link_rate);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304437 }
Eric Moore635374e2009-03-09 01:21:12 -06004438 out:
4439 kfree(sas_iounit_pg0);
4440}
4441
4442/**
4443 * _scsih_sas_host_add - create sas host object
4444 * @ioc: per adapter object
4445 *
4446 * Creating host side data object, stored in ioc->sas_hba
4447 *
4448 * Return nothing.
4449 */
4450static void
4451_scsih_sas_host_add(struct MPT2SAS_ADAPTER *ioc)
4452{
4453 int i;
4454 Mpi2ConfigReply_t mpi_reply;
4455 Mpi2SasIOUnitPage0_t *sas_iounit_pg0 = NULL;
4456 Mpi2SasIOUnitPage1_t *sas_iounit_pg1 = NULL;
4457 Mpi2SasPhyPage0_t phy_pg0;
4458 Mpi2SasDevicePage0_t sas_device_pg0;
4459 Mpi2SasEnclosurePage0_t enclosure_pg0;
4460 u16 ioc_status;
4461 u16 sz;
4462 u16 device_missing_delay;
4463
4464 mpt2sas_config_get_number_hba_phys(ioc, &ioc->sas_hba.num_phys);
4465 if (!ioc->sas_hba.num_phys) {
4466 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4467 ioc->name, __FILE__, __LINE__, __func__);
4468 return;
4469 }
4470
4471 /* sas_iounit page 0 */
4472 sz = offsetof(Mpi2SasIOUnitPage0_t, PhyData) + (ioc->sas_hba.num_phys *
4473 sizeof(Mpi2SasIOUnit0PhyData_t));
4474 sas_iounit_pg0 = kzalloc(sz, GFP_KERNEL);
4475 if (!sas_iounit_pg0) {
4476 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4477 ioc->name, __FILE__, __LINE__, __func__);
4478 return;
4479 }
4480 if ((mpt2sas_config_get_sas_iounit_pg0(ioc, &mpi_reply,
4481 sas_iounit_pg0, sz))) {
4482 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4483 ioc->name, __FILE__, __LINE__, __func__);
4484 goto out;
4485 }
4486 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4487 MPI2_IOCSTATUS_MASK;
4488 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4489 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4490 ioc->name, __FILE__, __LINE__, __func__);
4491 goto out;
4492 }
4493
4494 /* sas_iounit page 1 */
4495 sz = offsetof(Mpi2SasIOUnitPage1_t, PhyData) + (ioc->sas_hba.num_phys *
4496 sizeof(Mpi2SasIOUnit1PhyData_t));
4497 sas_iounit_pg1 = kzalloc(sz, GFP_KERNEL);
4498 if (!sas_iounit_pg1) {
4499 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4500 ioc->name, __FILE__, __LINE__, __func__);
4501 goto out;
4502 }
4503 if ((mpt2sas_config_get_sas_iounit_pg1(ioc, &mpi_reply,
4504 sas_iounit_pg1, sz))) {
4505 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4506 ioc->name, __FILE__, __LINE__, __func__);
4507 goto out;
4508 }
4509 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4510 MPI2_IOCSTATUS_MASK;
4511 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4512 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4513 ioc->name, __FILE__, __LINE__, __func__);
4514 goto out;
4515 }
4516
4517 ioc->io_missing_delay =
4518 le16_to_cpu(sas_iounit_pg1->IODeviceMissingDelay);
4519 device_missing_delay =
4520 le16_to_cpu(sas_iounit_pg1->ReportDeviceMissingDelay);
4521 if (device_missing_delay & MPI2_SASIOUNIT1_REPORT_MISSING_UNIT_16)
4522 ioc->device_missing_delay = (device_missing_delay &
4523 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK) * 16;
4524 else
4525 ioc->device_missing_delay = device_missing_delay &
4526 MPI2_SASIOUNIT1_REPORT_MISSING_TIMEOUT_MASK;
4527
4528 ioc->sas_hba.parent_dev = &ioc->shost->shost_gendev;
4529 ioc->sas_hba.phy = kcalloc(ioc->sas_hba.num_phys,
4530 sizeof(struct _sas_phy), GFP_KERNEL);
4531 if (!ioc->sas_hba.phy) {
4532 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4533 ioc->name, __FILE__, __LINE__, __func__);
4534 goto out;
4535 }
4536 for (i = 0; i < ioc->sas_hba.num_phys ; i++) {
4537 if ((mpt2sas_config_get_phy_pg0(ioc, &mpi_reply, &phy_pg0,
4538 i))) {
4539 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4540 ioc->name, __FILE__, __LINE__, __func__);
4541 goto out;
4542 }
4543 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4544 MPI2_IOCSTATUS_MASK;
4545 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4546 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4547 ioc->name, __FILE__, __LINE__, __func__);
4548 goto out;
4549 }
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304550
4551 if (i == 0)
4552 ioc->sas_hba.handle = le16_to_cpu(sas_iounit_pg0->
4553 PhyData[0].ControllerDevHandle);
4554 ioc->sas_hba.phy[i].handle = ioc->sas_hba.handle;
Eric Moore635374e2009-03-09 01:21:12 -06004555 ioc->sas_hba.phy[i].phy_id = i;
4556 mpt2sas_transport_add_host_phy(ioc, &ioc->sas_hba.phy[i],
4557 phy_pg0, ioc->sas_hba.parent_dev);
4558 }
4559 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304560 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, ioc->sas_hba.handle))) {
Eric Moore635374e2009-03-09 01:21:12 -06004561 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4562 ioc->name, __FILE__, __LINE__, __func__);
4563 goto out;
4564 }
Eric Moore635374e2009-03-09 01:21:12 -06004565 ioc->sas_hba.enclosure_handle =
4566 le16_to_cpu(sas_device_pg0.EnclosureHandle);
4567 ioc->sas_hba.sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4568 printk(MPT2SAS_INFO_FMT "host_add: handle(0x%04x), "
4569 "sas_addr(0x%016llx), phys(%d)\n", ioc->name, ioc->sas_hba.handle,
4570 (unsigned long long) ioc->sas_hba.sas_address,
4571 ioc->sas_hba.num_phys) ;
4572
4573 if (ioc->sas_hba.enclosure_handle) {
4574 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4575 &enclosure_pg0,
4576 MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4577 ioc->sas_hba.enclosure_handle))) {
4578 ioc->sas_hba.enclosure_logical_id =
4579 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4580 }
4581 }
4582
4583 out:
4584 kfree(sas_iounit_pg1);
4585 kfree(sas_iounit_pg0);
4586}
4587
4588/**
4589 * _scsih_expander_add - creating expander object
4590 * @ioc: per adapter object
4591 * @handle: expander handle
4592 *
4593 * Creating expander object, stored in ioc->sas_expander_list.
4594 *
4595 * Return 0 for success, else error.
4596 */
4597static int
4598_scsih_expander_add(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4599{
4600 struct _sas_node *sas_expander;
4601 Mpi2ConfigReply_t mpi_reply;
4602 Mpi2ExpanderPage0_t expander_pg0;
4603 Mpi2ExpanderPage1_t expander_pg1;
4604 Mpi2SasEnclosurePage0_t enclosure_pg0;
4605 u32 ioc_status;
4606 u16 parent_handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304607 __le64 sas_address, sas_address_parent = 0;
Eric Moore635374e2009-03-09 01:21:12 -06004608 int i;
4609 unsigned long flags;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304610 struct _sas_port *mpt2sas_port = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06004611 int rc = 0;
4612
4613 if (!handle)
4614 return -1;
4615
Eric Moore3cb54692010-07-08 14:44:34 -06004616 if (ioc->shost_recovery || ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304617 return -1;
4618
Eric Moore635374e2009-03-09 01:21:12 -06004619 if ((mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
4620 MPI2_SAS_EXPAND_PGAD_FORM_HNDL, handle))) {
4621 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4622 ioc->name, __FILE__, __LINE__, __func__);
4623 return -1;
4624 }
4625
4626 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4627 MPI2_IOCSTATUS_MASK;
4628 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4629 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4630 ioc->name, __FILE__, __LINE__, __func__);
4631 return -1;
4632 }
4633
4634 /* handle out of order topology events */
4635 parent_handle = le16_to_cpu(expander_pg0.ParentDevHandle);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304636 if (_scsih_get_sas_address(ioc, parent_handle, &sas_address_parent)
4637 != 0) {
4638 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4639 ioc->name, __FILE__, __LINE__, __func__);
4640 return -1;
4641 }
4642 if (sas_address_parent != ioc->sas_hba.sas_address) {
Eric Moore635374e2009-03-09 01:21:12 -06004643 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304644 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4645 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004646 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4647 if (!sas_expander) {
4648 rc = _scsih_expander_add(ioc, parent_handle);
4649 if (rc != 0)
4650 return rc;
4651 }
4652 }
4653
Eric Moore635374e2009-03-09 01:21:12 -06004654 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05304655 sas_address = le64_to_cpu(expander_pg0.SASAddress);
Eric Moore635374e2009-03-09 01:21:12 -06004656 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4657 sas_address);
4658 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4659
4660 if (sas_expander)
4661 return 0;
4662
4663 sas_expander = kzalloc(sizeof(struct _sas_node),
4664 GFP_KERNEL);
4665 if (!sas_expander) {
4666 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4667 ioc->name, __FILE__, __LINE__, __func__);
4668 return -1;
4669 }
4670
4671 sas_expander->handle = handle;
4672 sas_expander->num_phys = expander_pg0.NumPhys;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304673 sas_expander->sas_address_parent = sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06004674 sas_expander->sas_address = sas_address;
4675
4676 printk(MPT2SAS_INFO_FMT "expander_add: handle(0x%04x),"
4677 " parent(0x%04x), sas_addr(0x%016llx), phys(%d)\n", ioc->name,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304678 handle, parent_handle, (unsigned long long)
Eric Moore635374e2009-03-09 01:21:12 -06004679 sas_expander->sas_address, sas_expander->num_phys);
4680
4681 if (!sas_expander->num_phys)
4682 goto out_fail;
4683 sas_expander->phy = kcalloc(sas_expander->num_phys,
4684 sizeof(struct _sas_phy), GFP_KERNEL);
4685 if (!sas_expander->phy) {
4686 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4687 ioc->name, __FILE__, __LINE__, __func__);
4688 rc = -1;
4689 goto out_fail;
4690 }
4691
4692 INIT_LIST_HEAD(&sas_expander->sas_port_list);
4693 mpt2sas_port = mpt2sas_transport_port_add(ioc, handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304694 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004695 if (!mpt2sas_port) {
4696 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4697 ioc->name, __FILE__, __LINE__, __func__);
4698 rc = -1;
4699 goto out_fail;
4700 }
4701 sas_expander->parent_dev = &mpt2sas_port->rphy->dev;
4702
4703 for (i = 0 ; i < sas_expander->num_phys ; i++) {
4704 if ((mpt2sas_config_get_expander_pg1(ioc, &mpi_reply,
4705 &expander_pg1, i, handle))) {
4706 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4707 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai20f58952009-08-07 19:34:26 +05304708 rc = -1;
4709 goto out_fail;
Eric Moore635374e2009-03-09 01:21:12 -06004710 }
4711 sas_expander->phy[i].handle = handle;
4712 sas_expander->phy[i].phy_id = i;
Kashyap, Desai20f58952009-08-07 19:34:26 +05304713
4714 if ((mpt2sas_transport_add_expander_phy(ioc,
4715 &sas_expander->phy[i], expander_pg1,
4716 sas_expander->parent_dev))) {
4717 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4718 ioc->name, __FILE__, __LINE__, __func__);
4719 rc = -1;
4720 goto out_fail;
4721 }
Eric Moore635374e2009-03-09 01:21:12 -06004722 }
4723
4724 if (sas_expander->enclosure_handle) {
4725 if (!(mpt2sas_config_get_enclosure_pg0(ioc, &mpi_reply,
4726 &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
4727 sas_expander->enclosure_handle))) {
4728 sas_expander->enclosure_logical_id =
4729 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
4730 }
4731 }
4732
4733 _scsih_expander_node_add(ioc, sas_expander);
4734 return 0;
4735
4736 out_fail:
4737
Kashyap, Desai20f58952009-08-07 19:34:26 +05304738 if (mpt2sas_port)
4739 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304740 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06004741 kfree(sas_expander);
4742 return rc;
4743}
4744
4745/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05304746 * _scsih_done - scsih callback handler.
4747 * @ioc: per adapter object
4748 * @smid: system request message index
4749 * @msix_index: MSIX table index supplied by the OS
4750 * @reply: reply message frame(lower 32bit addr)
4751 *
4752 * Callback handler when sending internal generated message frames.
4753 * The callback index passed is `ioc->scsih_cb_idx`
4754 *
4755 * Return 1 meaning mf should be freed from _base_interrupt
4756 * 0 means the mf is freed from this function.
4757 */
4758static u8
4759_scsih_done(struct MPT2SAS_ADAPTER *ioc, u16 smid, u8 msix_index, u32 reply)
4760{
4761 MPI2DefaultReply_t *mpi_reply;
4762
4763 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
4764 if (ioc->scsih_cmds.status == MPT2_CMD_NOT_USED)
4765 return 1;
4766 if (ioc->scsih_cmds.smid != smid)
4767 return 1;
4768 ioc->scsih_cmds.status |= MPT2_CMD_COMPLETE;
4769 if (mpi_reply) {
4770 memcpy(ioc->scsih_cmds.reply, mpi_reply,
4771 mpi_reply->MsgLength*4);
4772 ioc->scsih_cmds.status |= MPT2_CMD_REPLY_VALID;
4773 }
4774 ioc->scsih_cmds.status &= ~MPT2_CMD_PENDING;
4775 complete(&ioc->scsih_cmds.done);
4776 return 1;
4777}
4778
4779/**
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304780 * mpt2sas_expander_remove - removing expander object
Eric Moore635374e2009-03-09 01:21:12 -06004781 * @ioc: per adapter object
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304782 * @sas_address: expander sas_address
Eric Moore635374e2009-03-09 01:21:12 -06004783 *
4784 * Return nothing.
4785 */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304786void
4787mpt2sas_expander_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
Eric Moore635374e2009-03-09 01:21:12 -06004788{
4789 struct _sas_node *sas_expander;
4790 unsigned long flags;
4791
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05304792 if (ioc->shost_recovery)
4793 return;
4794
Eric Moore635374e2009-03-09 01:21:12 -06004795 spin_lock_irqsave(&ioc->sas_node_lock, flags);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05304796 sas_expander = mpt2sas_scsih_expander_find_by_sas_address(ioc,
4797 sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05304798 if (!sas_expander) {
4799 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4800 return;
4801 }
4802 list_del(&sas_expander->list);
Eric Moore635374e2009-03-09 01:21:12 -06004803 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
4804 _scsih_expander_node_remove(ioc, sas_expander);
4805}
4806
4807/**
Kashyap, Desaib4344272010-03-17 16:24:14 +05304808 * _scsih_check_access_status - check access flags
4809 * @ioc: per adapter object
4810 * @sas_address: sas address
4811 * @handle: sas device handle
4812 * @access_flags: errors returned during discovery of the device
4813 *
4814 * Return 0 for success, else failure
4815 */
4816static u8
4817_scsih_check_access_status(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
4818 u16 handle, u8 access_status)
4819{
4820 u8 rc = 1;
4821 char *desc = NULL;
4822
4823 switch (access_status) {
4824 case MPI2_SAS_DEVICE0_ASTATUS_NO_ERRORS:
4825 case MPI2_SAS_DEVICE0_ASTATUS_SATA_NEEDS_INITIALIZATION:
4826 rc = 0;
4827 break;
4828 case MPI2_SAS_DEVICE0_ASTATUS_SATA_CAPABILITY_FAILED:
4829 desc = "sata capability failed";
4830 break;
4831 case MPI2_SAS_DEVICE0_ASTATUS_SATA_AFFILIATION_CONFLICT:
4832 desc = "sata affiliation conflict";
4833 break;
4834 case MPI2_SAS_DEVICE0_ASTATUS_ROUTE_NOT_ADDRESSABLE:
4835 desc = "route not addressable";
4836 break;
4837 case MPI2_SAS_DEVICE0_ASTATUS_SMP_ERROR_NOT_ADDRESSABLE:
4838 desc = "smp error not addressable";
4839 break;
4840 case MPI2_SAS_DEVICE0_ASTATUS_DEVICE_BLOCKED:
4841 desc = "device blocked";
4842 break;
4843 case MPI2_SAS_DEVICE0_ASTATUS_SATA_INIT_FAILED:
4844 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UNKNOWN:
4845 case MPI2_SAS_DEVICE0_ASTATUS_SIF_AFFILIATION_CONFLICT:
4846 case MPI2_SAS_DEVICE0_ASTATUS_SIF_DIAG:
4847 case MPI2_SAS_DEVICE0_ASTATUS_SIF_IDENTIFICATION:
4848 case MPI2_SAS_DEVICE0_ASTATUS_SIF_CHECK_POWER:
4849 case MPI2_SAS_DEVICE0_ASTATUS_SIF_PIO_SN:
4850 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MDMA_SN:
4851 case MPI2_SAS_DEVICE0_ASTATUS_SIF_UDMA_SN:
4852 case MPI2_SAS_DEVICE0_ASTATUS_SIF_ZONING_VIOLATION:
4853 case MPI2_SAS_DEVICE0_ASTATUS_SIF_NOT_ADDRESSABLE:
4854 case MPI2_SAS_DEVICE0_ASTATUS_SIF_MAX:
4855 desc = "sata initialization failed";
4856 break;
4857 default:
4858 desc = "unknown";
4859 break;
4860 }
4861
4862 if (!rc)
4863 return 0;
4864
4865 printk(MPT2SAS_ERR_FMT "discovery errors(%s): sas_address(0x%016llx), "
4866 "handle(0x%04x)\n", ioc->name, desc,
4867 (unsigned long long)sas_address, handle);
4868 return rc;
4869}
4870
4871static void
4872_scsih_check_device(struct MPT2SAS_ADAPTER *ioc, u16 handle)
4873{
4874 Mpi2ConfigReply_t mpi_reply;
4875 Mpi2SasDevicePage0_t sas_device_pg0;
4876 struct _sas_device *sas_device;
4877 u32 ioc_status;
4878 unsigned long flags;
4879 u64 sas_address;
4880 struct scsi_target *starget;
4881 struct MPT2SAS_TARGET *sas_target_priv_data;
4882 u32 device_info;
4883
4884 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4885 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle)))
4886 return;
4887
4888 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) & MPI2_IOCSTATUS_MASK;
4889 if (ioc_status != MPI2_IOCSTATUS_SUCCESS)
4890 return;
4891
4892 /* check if this is end device */
4893 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4894 if (!(_scsih_is_end_device(device_info)))
4895 return;
4896
4897 spin_lock_irqsave(&ioc->sas_device_lock, flags);
4898 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4899 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
4900 sas_address);
4901
4902 if (!sas_device) {
4903 printk(MPT2SAS_ERR_FMT "device is not present "
4904 "handle(0x%04x), no sas_device!!!\n", ioc->name, handle);
4905 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4906 return;
4907 }
4908
4909 if (unlikely(sas_device->handle != handle)) {
4910 starget = sas_device->starget;
4911 sas_target_priv_data = starget->hostdata;
4912 starget_printk(KERN_INFO, starget, "handle changed from(0x%04x)"
4913 " to (0x%04x)!!!\n", sas_device->handle, handle);
4914 sas_target_priv_data->handle = handle;
4915 sas_device->handle = handle;
4916 }
4917 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
4918
4919 /* check if device is present */
4920 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4921 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4922 printk(MPT2SAS_ERR_FMT "device is not present "
4923 "handle(0x%04x), flags!!!\n", ioc->name, handle);
4924 return;
4925 }
4926
4927 /* check if there were any issues with discovery */
4928 if (_scsih_check_access_status(ioc, sas_address, handle,
4929 sas_device_pg0.AccessStatus))
4930 return;
4931 _scsih_ublock_io_device(ioc, handle);
4932
4933}
4934
4935/**
Eric Moore635374e2009-03-09 01:21:12 -06004936 * _scsih_add_device - creating sas device object
4937 * @ioc: per adapter object
4938 * @handle: sas device handle
4939 * @phy_num: phy number end device attached to
4940 * @is_pd: is this hidden raid component
4941 *
4942 * Creating end device object, stored in ioc->sas_device_list.
4943 *
4944 * Returns 0 for success, non-zero for failure.
4945 */
4946static int
4947_scsih_add_device(struct MPT2SAS_ADAPTER *ioc, u16 handle, u8 phy_num, u8 is_pd)
4948{
4949 Mpi2ConfigReply_t mpi_reply;
4950 Mpi2SasDevicePage0_t sas_device_pg0;
4951 Mpi2SasEnclosurePage0_t enclosure_pg0;
4952 struct _sas_device *sas_device;
4953 u32 ioc_status;
4954 __le64 sas_address;
4955 u32 device_info;
4956 unsigned long flags;
4957
4958 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
4959 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
4960 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4961 ioc->name, __FILE__, __LINE__, __func__);
4962 return -1;
4963 }
4964
4965 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
4966 MPI2_IOCSTATUS_MASK;
4967 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
4968 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4969 ioc->name, __FILE__, __LINE__, __func__);
4970 return -1;
4971 }
4972
Kashyap, Desaib4344272010-03-17 16:24:14 +05304973 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
4974
Eric Moore635374e2009-03-09 01:21:12 -06004975 /* check if device is present */
4976 if (!(le16_to_cpu(sas_device_pg0.Flags) &
4977 MPI2_SAS_DEVICE0_FLAGS_DEVICE_PRESENT)) {
4978 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4979 ioc->name, __FILE__, __LINE__, __func__);
4980 printk(MPT2SAS_ERR_FMT "Flags = 0x%04x\n",
4981 ioc->name, le16_to_cpu(sas_device_pg0.Flags));
4982 return -1;
4983 }
4984
Kashyap, Desaib4344272010-03-17 16:24:14 +05304985 /* check if there were any issues with discovery */
4986 if (_scsih_check_access_status(ioc, sas_address, handle,
4987 sas_device_pg0.AccessStatus))
Eric Moore635374e2009-03-09 01:21:12 -06004988 return -1;
Eric Moore635374e2009-03-09 01:21:12 -06004989
4990 /* check if this is end device */
4991 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
4992 if (!(_scsih_is_end_device(device_info))) {
4993 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
4994 ioc->name, __FILE__, __LINE__, __func__);
4995 return -1;
4996 }
4997
Eric Moore635374e2009-03-09 01:21:12 -06004998
4999 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5000 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5001 sas_address);
5002 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5003
Kashyap, Desaib4344272010-03-17 16:24:14 +05305004 if (sas_device)
Eric Moore635374e2009-03-09 01:21:12 -06005005 return 0;
Eric Moore635374e2009-03-09 01:21:12 -06005006
5007 sas_device = kzalloc(sizeof(struct _sas_device),
5008 GFP_KERNEL);
5009 if (!sas_device) {
5010 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5011 ioc->name, __FILE__, __LINE__, __func__);
5012 return -1;
5013 }
5014
5015 sas_device->handle = handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305016 if (_scsih_get_sas_address(ioc, le16_to_cpu
5017 (sas_device_pg0.ParentDevHandle),
5018 &sas_device->sas_address_parent) != 0)
5019 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5020 ioc->name, __FILE__, __LINE__, __func__);
Eric Moore635374e2009-03-09 01:21:12 -06005021 sas_device->enclosure_handle =
5022 le16_to_cpu(sas_device_pg0.EnclosureHandle);
5023 sas_device->slot =
5024 le16_to_cpu(sas_device_pg0.Slot);
5025 sas_device->device_info = device_info;
5026 sas_device->sas_address = sas_address;
Kashyap, Desai7fbae672010-06-17 13:45:17 +05305027 sas_device->phy = sas_device_pg0.PhyNum;
Eric Moore635374e2009-03-09 01:21:12 -06005028
5029 /* get enclosure_logical_id */
Kashyap, Desai15052c92009-08-07 19:33:17 +05305030 if (sas_device->enclosure_handle && !(mpt2sas_config_get_enclosure_pg0(
5031 ioc, &mpi_reply, &enclosure_pg0, MPI2_SAS_ENCLOS_PGAD_FORM_HANDLE,
5032 sas_device->enclosure_handle)))
Eric Moore635374e2009-03-09 01:21:12 -06005033 sas_device->enclosure_logical_id =
5034 le64_to_cpu(enclosure_pg0.EnclosureLogicalID);
Eric Moore635374e2009-03-09 01:21:12 -06005035
5036 /* get device name */
5037 sas_device->device_name = le64_to_cpu(sas_device_pg0.DeviceName);
5038
5039 if (ioc->wait_for_port_enable_to_complete)
5040 _scsih_sas_device_init_add(ioc, sas_device);
5041 else
5042 _scsih_sas_device_add(ioc, sas_device);
5043
5044 return 0;
5045}
5046
5047/**
Kashyap, Desai1278b112010-03-09 17:34:13 +05305048 * _scsih_remove_device - removing sas device object
5049 * @ioc: per adapter object
5050 * @sas_device_delete: the sas_device object
5051 *
5052 * Return nothing.
5053 */
5054static void
5055_scsih_remove_device(struct MPT2SAS_ADAPTER *ioc,
5056 struct _sas_device *sas_device)
5057{
5058 struct _sas_device sas_device_backup;
5059 struct MPT2SAS_TARGET *sas_target_priv_data;
Kashyap, Desai34a03be2009-08-20 13:23:19 +05305060
Kashyap, Desai1278b112010-03-09 17:34:13 +05305061 if (!sas_device)
5062 return;
Eric Moore635374e2009-03-09 01:21:12 -06005063
Kashyap, Desai1278b112010-03-09 17:34:13 +05305064 memcpy(&sas_device_backup, sas_device, sizeof(struct _sas_device));
Eric Moore635374e2009-03-09 01:21:12 -06005065 _scsih_sas_device_remove(ioc, sas_device);
5066
Kashyap, Desai1278b112010-03-09 17:34:13 +05305067 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter: "
5068 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5069 sas_device_backup.handle, (unsigned long long)
5070 sas_device_backup.sas_address));
5071
5072 if (sas_device_backup.starget && sas_device_backup.starget->hostdata) {
5073 sas_target_priv_data = sas_device_backup.starget->hostdata;
5074 sas_target_priv_data->deleted = 1;
5075 }
5076
Kashyap, Desai1278b112010-03-09 17:34:13 +05305077 _scsih_ublock_io_device(ioc, sas_device_backup.handle);
5078
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305079 if (!ioc->hide_drives)
5080 mpt2sas_transport_port_remove(ioc,
5081 sas_device_backup.sas_address,
5082 sas_device_backup.sas_address_parent);
Kashyap, Desai1278b112010-03-09 17:34:13 +05305083
5084 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), sas_addr"
5085 "(0x%016llx)\n", ioc->name, sas_device_backup.handle,
5086 (unsigned long long) sas_device_backup.sas_address);
5087
5088 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: exit: "
5089 "handle(0x%04x), sas_addr(0x%016llx)\n", ioc->name, __func__,
5090 sas_device_backup.handle, (unsigned long long)
5091 sas_device_backup.sas_address));
Eric Moore635374e2009-03-09 01:21:12 -06005092}
5093
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305094/**
5095 * mpt2sas_device_remove - removing device object
5096 * @ioc: per adapter object
5097 * @sas_address: expander sas_address
5098 *
5099 * Return nothing.
5100 */
5101void
5102mpt2sas_device_remove(struct MPT2SAS_ADAPTER *ioc, u64 sas_address)
5103{
5104 struct _sas_device *sas_device;
5105 unsigned long flags;
5106
5107 if (ioc->shost_recovery)
5108 return;
5109
5110 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5111 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5112 sas_address);
5113 if (!sas_device) {
5114 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5115 return;
5116 }
5117 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5118 _scsih_remove_device(ioc, sas_device);
5119}
5120
Eric Moore635374e2009-03-09 01:21:12 -06005121#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5122/**
5123 * _scsih_sas_topology_change_event_debug - debug for topology event
5124 * @ioc: per adapter object
5125 * @event_data: event data payload
5126 * Context: user.
5127 */
5128static void
5129_scsih_sas_topology_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5130 Mpi2EventDataSasTopologyChangeList_t *event_data)
5131{
5132 int i;
5133 u16 handle;
5134 u16 reason_code;
5135 u8 phy_number;
5136 char *status_str = NULL;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305137 u8 link_rate, prev_link_rate;
Eric Moore635374e2009-03-09 01:21:12 -06005138
5139 switch (event_data->ExpStatus) {
5140 case MPI2_EVENT_SAS_TOPO_ES_ADDED:
5141 status_str = "add";
5142 break;
5143 case MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING:
5144 status_str = "remove";
5145 break;
5146 case MPI2_EVENT_SAS_TOPO_ES_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305147 case 0:
Eric Moore635374e2009-03-09 01:21:12 -06005148 status_str = "responding";
5149 break;
5150 case MPI2_EVENT_SAS_TOPO_ES_DELAY_NOT_RESPONDING:
5151 status_str = "remove delay";
5152 break;
5153 default:
5154 status_str = "unknown status";
5155 break;
5156 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305157 printk(MPT2SAS_INFO_FMT "sas topology change: (%s)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005158 ioc->name, status_str);
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305159 printk(KERN_INFO "\thandle(0x%04x), enclosure_handle(0x%04x) "
Eric Moore635374e2009-03-09 01:21:12 -06005160 "start_phy(%02d), count(%d)\n",
5161 le16_to_cpu(event_data->ExpanderDevHandle),
5162 le16_to_cpu(event_data->EnclosureHandle),
5163 event_data->StartPhyNum, event_data->NumEntries);
5164 for (i = 0; i < event_data->NumEntries; i++) {
5165 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5166 if (!handle)
5167 continue;
5168 phy_number = event_data->StartPhyNum + i;
5169 reason_code = event_data->PHY[i].PhyStatus &
5170 MPI2_EVENT_SAS_TOPO_RC_MASK;
5171 switch (reason_code) {
5172 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305173 status_str = "target add";
Eric Moore635374e2009-03-09 01:21:12 -06005174 break;
5175 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305176 status_str = "target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005177 break;
5178 case MPI2_EVENT_SAS_TOPO_RC_DELAY_NOT_RESPONDING:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305179 status_str = "delay target remove";
Eric Moore635374e2009-03-09 01:21:12 -06005180 break;
5181 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305182 status_str = "link rate change";
Eric Moore635374e2009-03-09 01:21:12 -06005183 break;
5184 case MPI2_EVENT_SAS_TOPO_RC_NO_CHANGE:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305185 status_str = "target responding";
Eric Moore635374e2009-03-09 01:21:12 -06005186 break;
5187 default:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305188 status_str = "unknown";
Eric Moore635374e2009-03-09 01:21:12 -06005189 break;
5190 }
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305191 link_rate = event_data->PHY[i].LinkRate >> 4;
5192 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305193 printk(KERN_INFO "\tphy(%02d), attached_handle(0x%04x): %s:"
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305194 " link rate: new(0x%02x), old(0x%02x)\n", phy_number,
5195 handle, status_str, link_rate, prev_link_rate);
5196
Eric Moore635374e2009-03-09 01:21:12 -06005197 }
5198}
5199#endif
5200
5201/**
5202 * _scsih_sas_topology_change_event - handle topology changes
5203 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305204 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005205 * Context: user.
5206 *
5207 */
5208static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305209_scsih_sas_topology_change_event(struct MPT2SAS_ADAPTER *ioc,
Eric Moore635374e2009-03-09 01:21:12 -06005210 struct fw_event_work *fw_event)
5211{
5212 int i;
5213 u16 parent_handle, handle;
5214 u16 reason_code;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305215 u8 phy_number, max_phys;
Eric Moore635374e2009-03-09 01:21:12 -06005216 struct _sas_node *sas_expander;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305217 struct _sas_device *sas_device;
5218 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06005219 unsigned long flags;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305220 u8 link_rate, prev_link_rate;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305221 Mpi2EventDataSasTopologyChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005222
5223#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5224 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5225 _scsih_sas_topology_change_event_debug(ioc, event_data);
5226#endif
5227
Eric Moore3cb54692010-07-08 14:44:34 -06005228 if (ioc->shost_recovery || ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305229 return;
5230
Eric Moore635374e2009-03-09 01:21:12 -06005231 if (!ioc->sas_hba.num_phys)
5232 _scsih_sas_host_add(ioc);
5233 else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305234 _scsih_sas_host_refresh(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06005235
5236 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305237 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring expander "
Eric Moore635374e2009-03-09 01:21:12 -06005238 "event\n", ioc->name));
5239 return;
5240 }
5241
5242 parent_handle = le16_to_cpu(event_data->ExpanderDevHandle);
5243
5244 /* handle expander add */
5245 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_ADDED)
5246 if (_scsih_expander_add(ioc, parent_handle) != 0)
5247 return;
5248
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305249 spin_lock_irqsave(&ioc->sas_node_lock, flags);
5250 sas_expander = mpt2sas_scsih_expander_find_by_handle(ioc,
5251 parent_handle);
5252 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305253 if (sas_expander) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305254 sas_address = sas_expander->sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305255 max_phys = sas_expander->num_phys;
5256 } else if (parent_handle < ioc->sas_hba.num_phys) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305257 sas_address = ioc->sas_hba.sas_address;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305258 max_phys = ioc->sas_hba.num_phys;
5259 } else
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305260 return;
5261
Eric Moore635374e2009-03-09 01:21:12 -06005262 /* handle siblings events */
5263 for (i = 0; i < event_data->NumEntries; i++) {
5264 if (fw_event->ignore) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305265 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "ignoring "
Eric Moore635374e2009-03-09 01:21:12 -06005266 "expander event\n", ioc->name));
5267 return;
5268 }
Eric Moore3cb54692010-07-08 14:44:34 -06005269 if (ioc->shost_recovery || ioc->remove_host ||
5270 ioc->pci_error_recovery)
Kashyap, Desai155dd4c2009-08-20 13:22:00 +05305271 return;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305272 phy_number = event_data->StartPhyNum + i;
Kashyap, Desaib41c09d2010-11-13 04:40:51 +05305273 if (phy_number >= max_phys)
5274 continue;
Kashyap, Desai308609c2009-09-14 11:07:23 +05305275 reason_code = event_data->PHY[i].PhyStatus &
5276 MPI2_EVENT_SAS_TOPO_RC_MASK;
5277 if ((event_data->PHY[i].PhyStatus &
5278 MPI2_EVENT_SAS_TOPO_PHYSTATUS_VACANT) && (reason_code !=
5279 MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING))
Eric Moore635374e2009-03-09 01:21:12 -06005280 continue;
5281 handle = le16_to_cpu(event_data->PHY[i].AttachedDevHandle);
5282 if (!handle)
5283 continue;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305284 link_rate = event_data->PHY[i].LinkRate >> 4;
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305285 prev_link_rate = event_data->PHY[i].LinkRate & 0xF;
Eric Moore635374e2009-03-09 01:21:12 -06005286 switch (reason_code) {
5287 case MPI2_EVENT_SAS_TOPO_RC_PHY_CHANGED:
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305288
5289 if (link_rate == prev_link_rate)
5290 break;
5291
5292 mpt2sas_transport_update_links(ioc, sas_address,
5293 handle, phy_number, link_rate);
5294
Kashyap, Desaib4344272010-03-17 16:24:14 +05305295 if (link_rate < MPI2_SAS_NEG_LINK_RATE_1_5)
5296 break;
5297
5298 _scsih_check_device(ioc, handle);
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305299 break;
Eric Moore635374e2009-03-09 01:21:12 -06005300 case MPI2_EVENT_SAS_TOPO_RC_TARG_ADDED:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305301
5302 mpt2sas_transport_update_links(ioc, sas_address,
5303 handle, phy_number, link_rate);
5304
Kashyap, Desaie7d59c12009-09-23 17:36:52 +05305305 _scsih_add_device(ioc, handle, phy_number, 0);
Eric Moore635374e2009-03-09 01:21:12 -06005306 break;
5307 case MPI2_EVENT_SAS_TOPO_RC_TARG_NOT_RESPONDING:
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305308
5309 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5310 sas_device = _scsih_sas_device_find_by_handle(ioc,
5311 handle);
5312 if (!sas_device) {
5313 spin_unlock_irqrestore(&ioc->sas_device_lock,
5314 flags);
5315 break;
5316 }
5317 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5318 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005319 break;
5320 }
5321 }
5322
5323 /* handle expander removal */
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305324 if (event_data->ExpStatus == MPI2_EVENT_SAS_TOPO_ES_NOT_RESPONDING &&
5325 sas_expander)
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05305326 mpt2sas_expander_remove(ioc, sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06005327
5328}
5329
5330#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5331/**
5332 * _scsih_sas_device_status_change_event_debug - debug for device event
5333 * @event_data: event data payload
5334 * Context: user.
5335 *
5336 * Return nothing.
5337 */
5338static void
5339_scsih_sas_device_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5340 Mpi2EventDataSasDeviceStatusChange_t *event_data)
5341{
5342 char *reason_str = NULL;
5343
5344 switch (event_data->ReasonCode) {
5345 case MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA:
5346 reason_str = "smart data";
5347 break;
5348 case MPI2_EVENT_SAS_DEV_STAT_RC_UNSUPPORTED:
5349 reason_str = "unsupported device discovered";
5350 break;
5351 case MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET:
5352 reason_str = "internal device reset";
5353 break;
5354 case MPI2_EVENT_SAS_DEV_STAT_RC_TASK_ABORT_INTERNAL:
5355 reason_str = "internal task abort";
5356 break;
5357 case MPI2_EVENT_SAS_DEV_STAT_RC_ABORT_TASK_SET_INTERNAL:
5358 reason_str = "internal task abort set";
5359 break;
5360 case MPI2_EVENT_SAS_DEV_STAT_RC_CLEAR_TASK_SET_INTERNAL:
5361 reason_str = "internal clear task set";
5362 break;
5363 case MPI2_EVENT_SAS_DEV_STAT_RC_QUERY_TASK_INTERNAL:
5364 reason_str = "internal query task";
5365 break;
5366 case MPI2_EVENT_SAS_DEV_STAT_RC_SATA_INIT_FAILURE:
5367 reason_str = "sata init failure";
5368 break;
5369 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET:
5370 reason_str = "internal device reset complete";
5371 break;
5372 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_TASK_ABORT_INTERNAL:
5373 reason_str = "internal task abort complete";
5374 break;
5375 case MPI2_EVENT_SAS_DEV_STAT_RC_ASYNC_NOTIFICATION:
5376 reason_str = "internal async notification";
5377 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05305378 case MPI2_EVENT_SAS_DEV_STAT_RC_EXPANDER_REDUCED_FUNCTIONALITY:
5379 reason_str = "expander reduced functionality";
5380 break;
5381 case MPI2_EVENT_SAS_DEV_STAT_RC_CMP_EXPANDER_REDUCED_FUNCTIONALITY:
5382 reason_str = "expander reduced functionality complete";
5383 break;
Eric Moore635374e2009-03-09 01:21:12 -06005384 default:
5385 reason_str = "unknown reason";
5386 break;
5387 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305388 printk(MPT2SAS_INFO_FMT "device status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005389 "\thandle(0x%04x), sas address(0x%016llx)", ioc->name,
5390 reason_str, le16_to_cpu(event_data->DevHandle),
5391 (unsigned long long)le64_to_cpu(event_data->SASAddress));
5392 if (event_data->ReasonCode == MPI2_EVENT_SAS_DEV_STAT_RC_SMART_DATA)
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305393 printk(MPT2SAS_INFO_FMT ", ASC(0x%x), ASCQ(0x%x)\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005394 event_data->ASC, event_data->ASCQ);
5395 printk(KERN_INFO "\n");
5396}
5397#endif
5398
5399/**
5400 * _scsih_sas_device_status_change_event - handle device status change
5401 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305402 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005403 * Context: user.
5404 *
5405 * Return nothing.
5406 */
5407static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305408_scsih_sas_device_status_change_event(struct MPT2SAS_ADAPTER *ioc,
5409 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005410{
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305411 struct MPT2SAS_TARGET *target_priv_data;
5412 struct _sas_device *sas_device;
5413 __le64 sas_address;
5414 unsigned long flags;
5415 Mpi2EventDataSasDeviceStatusChange_t *event_data =
5416 fw_event->event_data;
5417
Eric Moore635374e2009-03-09 01:21:12 -06005418#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5419 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305420 _scsih_sas_device_status_change_event_debug(ioc,
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305421 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005422#endif
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305423
Kashyap, Desaiefe82a12011-01-04 11:34:17 +05305424 /* In MPI Revision K (0xC), the internal device reset complete was
5425 * implemented, so avoid setting tm_busy flag for older firmware.
5426 */
5427 if ((ioc->facts.HeaderVersion >> 8) < 0xC)
5428 return;
5429
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305430 if (event_data->ReasonCode !=
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305431 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET &&
Kashyap, Desaif891dcf2010-03-17 16:22:21 +05305432 event_data->ReasonCode !=
5433 MPI2_EVENT_SAS_DEV_STAT_RC_CMP_INTERNAL_DEV_RESET)
Kashyap, Desai8ffc4572009-09-23 17:35:41 +05305434 return;
5435
5436 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5437 sas_address = le64_to_cpu(event_data->SASAddress);
5438 sas_device = mpt2sas_scsih_sas_device_find_by_sas_address(ioc,
5439 sas_address);
5440 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5441
5442 if (!sas_device || !sas_device->starget)
5443 return;
5444
5445 target_priv_data = sas_device->starget->hostdata;
5446 if (!target_priv_data)
5447 return;
5448
5449 if (event_data->ReasonCode ==
5450 MPI2_EVENT_SAS_DEV_STAT_RC_INTERNAL_DEVICE_RESET)
5451 target_priv_data->tm_busy = 1;
5452 else
5453 target_priv_data->tm_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005454}
5455
5456#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5457/**
5458 * _scsih_sas_enclosure_dev_status_change_event_debug - debug for enclosure event
5459 * @ioc: per adapter object
5460 * @event_data: event data payload
5461 * Context: user.
5462 *
5463 * Return nothing.
5464 */
5465static void
5466_scsih_sas_enclosure_dev_status_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5467 Mpi2EventDataSasEnclDevStatusChange_t *event_data)
5468{
5469 char *reason_str = NULL;
5470
5471 switch (event_data->ReasonCode) {
5472 case MPI2_EVENT_SAS_ENCL_RC_ADDED:
5473 reason_str = "enclosure add";
5474 break;
5475 case MPI2_EVENT_SAS_ENCL_RC_NOT_RESPONDING:
5476 reason_str = "enclosure remove";
5477 break;
5478 default:
5479 reason_str = "unknown reason";
5480 break;
5481 }
5482
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305483 printk(MPT2SAS_INFO_FMT "enclosure status change: (%s)\n"
Eric Moore635374e2009-03-09 01:21:12 -06005484 "\thandle(0x%04x), enclosure logical id(0x%016llx)"
5485 " number slots(%d)\n", ioc->name, reason_str,
5486 le16_to_cpu(event_data->EnclosureHandle),
5487 (unsigned long long)le64_to_cpu(event_data->EnclosureLogicalID),
5488 le16_to_cpu(event_data->StartSlot));
5489}
5490#endif
5491
5492/**
5493 * _scsih_sas_enclosure_dev_status_change_event - handle enclosure events
5494 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305495 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005496 * Context: user.
5497 *
5498 * Return nothing.
5499 */
5500static void
5501_scsih_sas_enclosure_dev_status_change_event(struct MPT2SAS_ADAPTER *ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305502 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005503{
5504#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5505 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5506 _scsih_sas_enclosure_dev_status_change_event_debug(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305507 fw_event->event_data);
Eric Moore635374e2009-03-09 01:21:12 -06005508#endif
5509}
5510
5511/**
5512 * _scsih_sas_broadcast_primative_event - handle broadcast events
5513 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305514 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005515 * Context: user.
5516 *
5517 * Return nothing.
5518 */
5519static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305520_scsih_sas_broadcast_primative_event(struct MPT2SAS_ADAPTER *ioc,
5521 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005522{
5523 struct scsi_cmnd *scmd;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305524 struct scsi_device *sdev;
Eric Moore635374e2009-03-09 01:21:12 -06005525 u16 smid, handle;
5526 u32 lun;
5527 struct MPT2SAS_DEVICE *sas_device_priv_data;
5528 u32 termination_count;
5529 u32 query_count;
5530 Mpi2SCSITaskManagementReply_t *mpi_reply;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305531#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5532 Mpi2EventDataSasBroadcastPrimitive_t *event_data = fw_event->event_data;
5533#endif
Kashyap, Desai463217b2009-10-05 15:53:06 +05305534 u16 ioc_status;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305535 unsigned long flags;
5536 int r;
5537
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005538 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "broadcast primitive: "
Eric Moore635374e2009-03-09 01:21:12 -06005539 "phy number(%d), width(%d)\n", ioc->name, event_data->PhyNum,
5540 event_data->PortWidth));
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305541 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: enter\n", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005542 __func__));
5543
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305544 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
5545 ioc->broadcast_aen_busy = 0;
Eric Moore635374e2009-03-09 01:21:12 -06005546 termination_count = 0;
5547 query_count = 0;
5548 mpi_reply = ioc->tm_cmds.reply;
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305549 for (smid = 1; smid <= ioc->scsiio_depth; smid++) {
Eric Moore635374e2009-03-09 01:21:12 -06005550 scmd = _scsih_scsi_lookup_get(ioc, smid);
5551 if (!scmd)
5552 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305553 sdev = scmd->device;
5554 sas_device_priv_data = sdev->hostdata;
Eric Moore635374e2009-03-09 01:21:12 -06005555 if (!sas_device_priv_data || !sas_device_priv_data->sas_target)
5556 continue;
5557 /* skip hidden raid components */
5558 if (sas_device_priv_data->sas_target->flags &
5559 MPT_TARGET_FLAGS_RAID_COMPONENT)
5560 continue;
5561 /* skip volumes */
5562 if (sas_device_priv_data->sas_target->flags &
5563 MPT_TARGET_FLAGS_VOLUME)
5564 continue;
5565
5566 handle = sas_device_priv_data->sas_target->handle;
5567 lun = sas_device_priv_data->lun;
5568 query_count++;
5569
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305570 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Kashyap, Desai8ed9a032010-03-17 16:25:59 +05305571 mpt2sas_scsih_issue_tm(ioc, handle, 0, 0, lun,
5572 MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK, smid, 30, NULL);
Eric Moore8901cbb2009-04-21 15:41:32 -06005573 ioc->tm_cmds.status = MPT2_CMD_NOT_USED;
Kashyap, Desai463217b2009-10-05 15:53:06 +05305574 ioc_status = le16_to_cpu(mpi_reply->IOCStatus)
5575 & MPI2_IOCSTATUS_MASK;
5576 if ((ioc_status == MPI2_IOCSTATUS_SUCCESS) &&
Eric Moore635374e2009-03-09 01:21:12 -06005577 (mpi_reply->ResponseCode ==
5578 MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED ||
5579 mpi_reply->ResponseCode ==
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305580 MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC)) {
5581 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005582 continue;
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305583 }
5584 r = mpt2sas_scsih_issue_tm(ioc, handle, sdev->channel, sdev->id,
5585 sdev->lun, MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, smid, 30,
5586 scmd);
5587 if (r == FAILED)
5588 sdev_printk(KERN_WARNING, sdev, "task abort: FAILED "
5589 "scmd(%p)\n", scmd);
Eric Moore635374e2009-03-09 01:21:12 -06005590 termination_count += le32_to_cpu(mpi_reply->TerminationCount);
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305591 spin_lock_irqsave(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005592 }
Kashyap, Desaiec07a052011-01-05 17:54:32 +05305593 spin_unlock_irqrestore(&ioc->scsi_lookup_lock, flags);
Eric Moore635374e2009-03-09 01:21:12 -06005594
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305595 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT
Eric Moore635374e2009-03-09 01:21:12 -06005596 "%s - exit, query_count = %d termination_count = %d\n",
5597 ioc->name, __func__, query_count, termination_count));
5598}
5599
5600/**
5601 * _scsih_sas_discovery_event - handle discovery events
5602 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305603 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005604 * Context: user.
5605 *
5606 * Return nothing.
5607 */
5608static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305609_scsih_sas_discovery_event(struct MPT2SAS_ADAPTER *ioc,
5610 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005611{
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305612 Mpi2EventDataSasDiscovery_t *event_data = fw_event->event_data;
5613
Eric Moore635374e2009-03-09 01:21:12 -06005614#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5615 if (ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK) {
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305616 printk(MPT2SAS_INFO_FMT "discovery event: (%s)", ioc->name,
Eric Moore635374e2009-03-09 01:21:12 -06005617 (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED) ?
5618 "start" : "stop");
5619 if (event_data->DiscoveryStatus)
Kashyap, Desai595bb0b2009-09-14 11:02:48 +05305620 printk("discovery_status(0x%08x)",
5621 le32_to_cpu(event_data->DiscoveryStatus));
Eric Moore635374e2009-03-09 01:21:12 -06005622 printk("\n");
5623 }
5624#endif
5625
5626 if (event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED &&
5627 !ioc->sas_hba.num_phys)
5628 _scsih_sas_host_add(ioc);
5629}
5630
5631/**
5632 * _scsih_reprobe_lun - reprobing lun
5633 * @sdev: scsi device struct
5634 * @no_uld_attach: sdev->no_uld_attach flag setting
5635 *
5636 **/
5637static void
5638_scsih_reprobe_lun(struct scsi_device *sdev, void *no_uld_attach)
5639{
5640 int rc;
5641
5642 sdev->no_uld_attach = no_uld_attach ? 1 : 0;
5643 sdev_printk(KERN_INFO, sdev, "%s raid component\n",
5644 sdev->no_uld_attach ? "hidding" : "exposing");
5645 rc = scsi_device_reprobe(sdev);
5646}
5647
5648/**
5649 * _scsih_reprobe_target - reprobing target
5650 * @starget: scsi target struct
5651 * @no_uld_attach: sdev->no_uld_attach flag setting
5652 *
5653 * Note: no_uld_attach flag determines whether the disk device is attached
5654 * to block layer. A value of `1` means to not attach.
5655 **/
5656static void
5657_scsih_reprobe_target(struct scsi_target *starget, int no_uld_attach)
5658{
5659 struct MPT2SAS_TARGET *sas_target_priv_data = starget->hostdata;
5660
5661 if (no_uld_attach)
5662 sas_target_priv_data->flags |= MPT_TARGET_FLAGS_RAID_COMPONENT;
5663 else
5664 sas_target_priv_data->flags &= ~MPT_TARGET_FLAGS_RAID_COMPONENT;
5665
5666 starget_for_each_device(starget, no_uld_attach ? (void *)1 : NULL,
5667 _scsih_reprobe_lun);
5668}
5669/**
5670 * _scsih_sas_volume_add - add new volume
5671 * @ioc: per adapter object
5672 * @element: IR config element data
5673 * Context: user.
5674 *
5675 * Return nothing.
5676 */
5677static void
5678_scsih_sas_volume_add(struct MPT2SAS_ADAPTER *ioc,
5679 Mpi2EventIrConfigElement_t *element)
5680{
5681 struct _raid_device *raid_device;
5682 unsigned long flags;
5683 u64 wwid;
5684 u16 handle = le16_to_cpu(element->VolDevHandle);
5685 int rc;
5686
Eric Moore635374e2009-03-09 01:21:12 -06005687 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
5688 if (!wwid) {
5689 printk(MPT2SAS_ERR_FMT
5690 "failure at %s:%d/%s()!\n", ioc->name,
5691 __FILE__, __LINE__, __func__);
5692 return;
5693 }
5694
5695 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5696 raid_device = _scsih_raid_device_find_by_wwid(ioc, wwid);
5697 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5698
5699 if (raid_device)
5700 return;
5701
5702 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
5703 if (!raid_device) {
5704 printk(MPT2SAS_ERR_FMT
5705 "failure at %s:%d/%s()!\n", ioc->name,
5706 __FILE__, __LINE__, __func__);
5707 return;
5708 }
5709
5710 raid_device->id = ioc->sas_id++;
5711 raid_device->channel = RAID_CHANNEL;
5712 raid_device->handle = handle;
5713 raid_device->wwid = wwid;
5714 _scsih_raid_device_add(ioc, raid_device);
5715 if (!ioc->wait_for_port_enable_to_complete) {
5716 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
5717 raid_device->id, 0);
5718 if (rc)
5719 _scsih_raid_device_remove(ioc, raid_device);
5720 } else
5721 _scsih_determine_boot_device(ioc, raid_device, 1);
5722}
5723
5724/**
5725 * _scsih_sas_volume_delete - delete volume
5726 * @ioc: per adapter object
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305727 * @handle: volume device handle
Eric Moore635374e2009-03-09 01:21:12 -06005728 * Context: user.
5729 *
5730 * Return nothing.
5731 */
5732static void
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305733_scsih_sas_volume_delete(struct MPT2SAS_ADAPTER *ioc, u16 handle)
Eric Moore635374e2009-03-09 01:21:12 -06005734{
5735 struct _raid_device *raid_device;
Eric Moore635374e2009-03-09 01:21:12 -06005736 unsigned long flags;
5737 struct MPT2SAS_TARGET *sas_target_priv_data;
5738
Eric Moore635374e2009-03-09 01:21:12 -06005739 spin_lock_irqsave(&ioc->raid_device_lock, flags);
5740 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
5741 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
5742 if (!raid_device)
5743 return;
5744 if (raid_device->starget) {
5745 sas_target_priv_data = raid_device->starget->hostdata;
5746 sas_target_priv_data->deleted = 1;
5747 scsi_remove_target(&raid_device->starget->dev);
5748 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305749 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
5750 "(0x%016llx)\n", ioc->name, raid_device->handle,
5751 (unsigned long long) raid_device->wwid);
Eric Moore635374e2009-03-09 01:21:12 -06005752 _scsih_raid_device_remove(ioc, raid_device);
5753}
5754
5755/**
5756 * _scsih_sas_pd_expose - expose pd component to /dev/sdX
5757 * @ioc: per adapter object
5758 * @element: IR config element data
5759 * Context: user.
5760 *
5761 * Return nothing.
5762 */
5763static void
5764_scsih_sas_pd_expose(struct MPT2SAS_ADAPTER *ioc,
5765 Mpi2EventIrConfigElement_t *element)
5766{
5767 struct _sas_device *sas_device;
5768 unsigned long flags;
5769 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5770
5771 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5772 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5773 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5774 if (!sas_device)
5775 return;
5776
5777 /* exposing raid component */
5778 sas_device->volume_handle = 0;
5779 sas_device->volume_wwid = 0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305780 clear_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005781 _scsih_reprobe_target(sas_device->starget, 0);
5782}
5783
5784/**
5785 * _scsih_sas_pd_hide - hide pd component from /dev/sdX
5786 * @ioc: per adapter object
5787 * @element: IR config element data
5788 * Context: user.
5789 *
5790 * Return nothing.
5791 */
5792static void
5793_scsih_sas_pd_hide(struct MPT2SAS_ADAPTER *ioc,
5794 Mpi2EventIrConfigElement_t *element)
5795{
5796 struct _sas_device *sas_device;
5797 unsigned long flags;
5798 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5799
5800 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5801 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5802 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5803 if (!sas_device)
5804 return;
5805
5806 /* hiding raid component */
5807 mpt2sas_config_get_volume_handle(ioc, handle,
5808 &sas_device->volume_handle);
5809 mpt2sas_config_get_volume_wwid(ioc, sas_device->volume_handle,
5810 &sas_device->volume_wwid);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305811 set_bit(handle, ioc->pd_handles);
Eric Moore635374e2009-03-09 01:21:12 -06005812 _scsih_reprobe_target(sas_device->starget, 1);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305813
Eric Moore635374e2009-03-09 01:21:12 -06005814}
5815
5816/**
5817 * _scsih_sas_pd_delete - delete pd component
5818 * @ioc: per adapter object
5819 * @element: IR config element data
5820 * Context: user.
5821 *
5822 * Return nothing.
5823 */
5824static void
5825_scsih_sas_pd_delete(struct MPT2SAS_ADAPTER *ioc,
5826 Mpi2EventIrConfigElement_t *element)
5827{
5828 struct _sas_device *sas_device;
5829 unsigned long flags;
5830 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
5831
5832 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5833 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5834 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
5835 if (!sas_device)
5836 return;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305837 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06005838}
5839
5840/**
5841 * _scsih_sas_pd_add - remove pd component
5842 * @ioc: per adapter object
5843 * @element: IR config element data
5844 * Context: user.
5845 *
5846 * Return nothing.
5847 */
5848static void
5849_scsih_sas_pd_add(struct MPT2SAS_ADAPTER *ioc,
5850 Mpi2EventIrConfigElement_t *element)
5851{
5852 struct _sas_device *sas_device;
5853 unsigned long flags;
5854 u16 handle = le16_to_cpu(element->PhysDiskDevHandle);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305855 Mpi2ConfigReply_t mpi_reply;
5856 Mpi2SasDevicePage0_t sas_device_pg0;
5857 u32 ioc_status;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305858 u64 sas_address;
5859 u16 parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06005860
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305861 set_bit(handle, ioc->pd_handles);
5862
Eric Moore635374e2009-03-09 01:21:12 -06005863 spin_lock_irqsave(&ioc->sas_device_lock, flags);
5864 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
5865 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05305866 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05305867 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305868
5869 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply, &sas_device_pg0,
5870 MPI2_SAS_DEVICE_PGAD_FORM_HANDLE, handle))) {
5871 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5872 ioc->name, __FILE__, __LINE__, __func__);
5873 return;
5874 }
5875
5876 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
5877 MPI2_IOCSTATUS_MASK;
5878 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
5879 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
5880 ioc->name, __FILE__, __LINE__, __func__);
5881 return;
5882 }
5883
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05305884 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
5885 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
5886 mpt2sas_transport_update_links(ioc, sas_address, handle,
5887 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05305888
5889 _scsih_add_device(ioc, handle, 0, 1);
Eric Moore635374e2009-03-09 01:21:12 -06005890}
5891
5892#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
5893/**
5894 * _scsih_sas_ir_config_change_event_debug - debug for IR Config Change events
5895 * @ioc: per adapter object
5896 * @event_data: event data payload
5897 * Context: user.
5898 *
5899 * Return nothing.
5900 */
5901static void
5902_scsih_sas_ir_config_change_event_debug(struct MPT2SAS_ADAPTER *ioc,
5903 Mpi2EventDataIrConfigChangeList_t *event_data)
5904{
5905 Mpi2EventIrConfigElement_t *element;
5906 u8 element_type;
5907 int i;
5908 char *reason_str = NULL, *element_str = NULL;
5909
5910 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
5911
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305912 printk(MPT2SAS_INFO_FMT "raid config change: (%s), elements(%d)\n",
Eric Moore635374e2009-03-09 01:21:12 -06005913 ioc->name, (le32_to_cpu(event_data->Flags) &
5914 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ?
5915 "foreign" : "native", event_data->NumElements);
5916 for (i = 0; i < event_data->NumElements; i++, element++) {
5917 switch (element->ReasonCode) {
5918 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
5919 reason_str = "add";
5920 break;
5921 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
5922 reason_str = "remove";
5923 break;
5924 case MPI2_EVENT_IR_CHANGE_RC_NO_CHANGE:
5925 reason_str = "no change";
5926 break;
5927 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
5928 reason_str = "hide";
5929 break;
5930 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
5931 reason_str = "unhide";
5932 break;
5933 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
5934 reason_str = "volume_created";
5935 break;
5936 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
5937 reason_str = "volume_deleted";
5938 break;
5939 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
5940 reason_str = "pd_created";
5941 break;
5942 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
5943 reason_str = "pd_deleted";
5944 break;
5945 default:
5946 reason_str = "unknown reason";
5947 break;
5948 }
5949 element_type = le16_to_cpu(element->ElementFlags) &
5950 MPI2_EVENT_IR_CHANGE_EFLAGS_ELEMENT_TYPE_MASK;
5951 switch (element_type) {
5952 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLUME_ELEMENT:
5953 element_str = "volume";
5954 break;
5955 case MPI2_EVENT_IR_CHANGE_EFLAGS_VOLPHYSDISK_ELEMENT:
5956 element_str = "phys disk";
5957 break;
5958 case MPI2_EVENT_IR_CHANGE_EFLAGS_HOTSPARE_ELEMENT:
5959 element_str = "hot spare";
5960 break;
5961 default:
5962 element_str = "unknown element";
5963 break;
5964 }
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05305965 printk(KERN_INFO "\t(%s:%s), vol handle(0x%04x), "
Eric Moore635374e2009-03-09 01:21:12 -06005966 "pd handle(0x%04x), pd num(0x%02x)\n", element_str,
5967 reason_str, le16_to_cpu(element->VolDevHandle),
5968 le16_to_cpu(element->PhysDiskDevHandle),
5969 element->PhysDiskNum);
5970 }
5971}
5972#endif
5973
5974/**
5975 * _scsih_sas_ir_config_change_event - handle ir configuration change events
5976 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305977 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06005978 * Context: user.
5979 *
5980 * Return nothing.
5981 */
5982static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305983_scsih_sas_ir_config_change_event(struct MPT2SAS_ADAPTER *ioc,
5984 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06005985{
5986 Mpi2EventIrConfigElement_t *element;
5987 int i;
Kashyap, Desai62727a72009-08-07 19:35:18 +05305988 u8 foreign_config;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05305989 Mpi2EventDataIrConfigChangeList_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06005990
5991#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05305992 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
5993 && !ioc->hide_ir_msg)
Eric Moore635374e2009-03-09 01:21:12 -06005994 _scsih_sas_ir_config_change_event_debug(ioc, event_data);
5995
5996#endif
Kashyap, Desai62727a72009-08-07 19:35:18 +05305997 foreign_config = (le32_to_cpu(event_data->Flags) &
5998 MPI2_EVENT_IR_CHANGE_FLAGS_FOREIGN_CONFIG) ? 1 : 0;
Eric Moore635374e2009-03-09 01:21:12 -06005999
6000 element = (Mpi2EventIrConfigElement_t *)&event_data->ConfigElement[0];
6001 for (i = 0; i < event_data->NumElements; i++, element++) {
6002
6003 switch (element->ReasonCode) {
6004 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_CREATED:
6005 case MPI2_EVENT_IR_CHANGE_RC_ADDED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306006 if (!foreign_config)
6007 _scsih_sas_volume_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006008 break;
6009 case MPI2_EVENT_IR_CHANGE_RC_VOLUME_DELETED:
6010 case MPI2_EVENT_IR_CHANGE_RC_REMOVED:
Kashyap, Desai62727a72009-08-07 19:35:18 +05306011 if (!foreign_config)
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306012 _scsih_sas_volume_delete(ioc,
6013 le16_to_cpu(element->VolDevHandle));
Eric Moore635374e2009-03-09 01:21:12 -06006014 break;
6015 case MPI2_EVENT_IR_CHANGE_RC_PD_CREATED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306016 if (!ioc->is_warpdrive)
6017 _scsih_sas_pd_hide(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006018 break;
6019 case MPI2_EVENT_IR_CHANGE_RC_PD_DELETED:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306020 if (!ioc->is_warpdrive)
6021 _scsih_sas_pd_expose(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006022 break;
6023 case MPI2_EVENT_IR_CHANGE_RC_HIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306024 if (!ioc->is_warpdrive)
6025 _scsih_sas_pd_add(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006026 break;
6027 case MPI2_EVENT_IR_CHANGE_RC_UNHIDE:
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306028 if (!ioc->is_warpdrive)
6029 _scsih_sas_pd_delete(ioc, element);
Eric Moore635374e2009-03-09 01:21:12 -06006030 break;
6031 }
6032 }
6033}
6034
6035/**
6036 * _scsih_sas_ir_volume_event - IR volume event
6037 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306038 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006039 * Context: user.
6040 *
6041 * Return nothing.
6042 */
6043static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306044_scsih_sas_ir_volume_event(struct MPT2SAS_ADAPTER *ioc,
6045 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006046{
6047 u64 wwid;
6048 unsigned long flags;
6049 struct _raid_device *raid_device;
6050 u16 handle;
6051 u32 state;
6052 int rc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306053 Mpi2EventDataIrVolume_t *event_data = fw_event->event_data;
Eric Moore635374e2009-03-09 01:21:12 -06006054
6055 if (event_data->ReasonCode != MPI2_EVENT_IR_VOLUME_RC_STATE_CHANGED)
6056 return;
6057
6058 handle = le16_to_cpu(event_data->VolDevHandle);
6059 state = le32_to_cpu(event_data->NewValue);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306060 if (!ioc->hide_ir_msg)
6061 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6062 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6063 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006064
Eric Moore635374e2009-03-09 01:21:12 -06006065 switch (state) {
6066 case MPI2_RAID_VOL_STATE_MISSING:
6067 case MPI2_RAID_VOL_STATE_FAILED:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306068 _scsih_sas_volume_delete(ioc, handle);
Eric Moore635374e2009-03-09 01:21:12 -06006069 break;
6070
6071 case MPI2_RAID_VOL_STATE_ONLINE:
6072 case MPI2_RAID_VOL_STATE_DEGRADED:
6073 case MPI2_RAID_VOL_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306074
6075 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6076 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6077 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6078
Eric Moore635374e2009-03-09 01:21:12 -06006079 if (raid_device)
6080 break;
6081
6082 mpt2sas_config_get_volume_wwid(ioc, handle, &wwid);
6083 if (!wwid) {
6084 printk(MPT2SAS_ERR_FMT
6085 "failure at %s:%d/%s()!\n", ioc->name,
6086 __FILE__, __LINE__, __func__);
6087 break;
6088 }
6089
6090 raid_device = kzalloc(sizeof(struct _raid_device), GFP_KERNEL);
6091 if (!raid_device) {
6092 printk(MPT2SAS_ERR_FMT
6093 "failure at %s:%d/%s()!\n", ioc->name,
6094 __FILE__, __LINE__, __func__);
6095 break;
6096 }
6097
6098 raid_device->id = ioc->sas_id++;
6099 raid_device->channel = RAID_CHANNEL;
6100 raid_device->handle = handle;
6101 raid_device->wwid = wwid;
6102 _scsih_raid_device_add(ioc, raid_device);
6103 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
6104 raid_device->id, 0);
6105 if (rc)
6106 _scsih_raid_device_remove(ioc, raid_device);
6107 break;
6108
6109 case MPI2_RAID_VOL_STATE_INITIALIZING:
6110 default:
6111 break;
6112 }
6113}
6114
6115/**
6116 * _scsih_sas_ir_physical_disk_event - PD event
6117 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306118 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006119 * Context: user.
6120 *
6121 * Return nothing.
6122 */
6123static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306124_scsih_sas_ir_physical_disk_event(struct MPT2SAS_ADAPTER *ioc,
6125 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006126{
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306127 u16 handle, parent_handle;
Eric Moore635374e2009-03-09 01:21:12 -06006128 u32 state;
6129 struct _sas_device *sas_device;
6130 unsigned long flags;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306131 Mpi2ConfigReply_t mpi_reply;
6132 Mpi2SasDevicePage0_t sas_device_pg0;
6133 u32 ioc_status;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306134 Mpi2EventDataIrPhysicalDisk_t *event_data = fw_event->event_data;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306135 u64 sas_address;
Eric Moore635374e2009-03-09 01:21:12 -06006136
6137 if (event_data->ReasonCode != MPI2_EVENT_IR_PHYSDISK_RC_STATE_CHANGED)
6138 return;
6139
6140 handle = le16_to_cpu(event_data->PhysDiskDevHandle);
6141 state = le32_to_cpu(event_data->NewValue);
6142
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306143 if (!ioc->hide_ir_msg)
6144 dewtprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: handle(0x%04x), "
6145 "old(0x%08x), new(0x%08x)\n", ioc->name, __func__, handle,
6146 le32_to_cpu(event_data->PreviousValue), state));
Eric Moore635374e2009-03-09 01:21:12 -06006147
Eric Moore635374e2009-03-09 01:21:12 -06006148 switch (state) {
Eric Moore635374e2009-03-09 01:21:12 -06006149 case MPI2_RAID_PD_STATE_ONLINE:
6150 case MPI2_RAID_PD_STATE_DEGRADED:
6151 case MPI2_RAID_PD_STATE_REBUILDING:
6152 case MPI2_RAID_PD_STATE_OPTIMAL:
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306153 case MPI2_RAID_PD_STATE_HOT_SPARE:
6154
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306155 if (!ioc->is_warpdrive)
6156 set_bit(handle, ioc->pd_handles);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306157
6158 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6159 sas_device = _scsih_sas_device_find_by_handle(ioc, handle);
6160 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6161
6162 if (sas_device)
Kashyap, Desai62727a72009-08-07 19:35:18 +05306163 return;
Kashyap, Desai62727a72009-08-07 19:35:18 +05306164
6165 if ((mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6166 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_HANDLE,
6167 handle))) {
6168 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6169 ioc->name, __FILE__, __LINE__, __func__);
6170 return;
6171 }
6172
6173 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6174 MPI2_IOCSTATUS_MASK;
6175 if (ioc_status != MPI2_IOCSTATUS_SUCCESS) {
6176 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6177 ioc->name, __FILE__, __LINE__, __func__);
6178 return;
6179 }
6180
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306181 parent_handle = le16_to_cpu(sas_device_pg0.ParentDevHandle);
6182 if (!_scsih_get_sas_address(ioc, parent_handle, &sas_address))
6183 mpt2sas_transport_update_links(ioc, sas_address, handle,
6184 sas_device_pg0.PhyNum, MPI2_SAS_NEG_LINK_RATE_1_5);
Kashyap, Desai62727a72009-08-07 19:35:18 +05306185
6186 _scsih_add_device(ioc, handle, 0, 1);
6187
Eric Moore635374e2009-03-09 01:21:12 -06006188 break;
6189
Kashyap, Desai62727a72009-08-07 19:35:18 +05306190 case MPI2_RAID_PD_STATE_OFFLINE:
Eric Moore635374e2009-03-09 01:21:12 -06006191 case MPI2_RAID_PD_STATE_NOT_CONFIGURED:
6192 case MPI2_RAID_PD_STATE_NOT_COMPATIBLE:
Eric Moore635374e2009-03-09 01:21:12 -06006193 default:
6194 break;
6195 }
6196}
6197
6198#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
6199/**
6200 * _scsih_sas_ir_operation_status_event_debug - debug for IR op event
6201 * @ioc: per adapter object
6202 * @event_data: event data payload
6203 * Context: user.
6204 *
6205 * Return nothing.
6206 */
6207static void
6208_scsih_sas_ir_operation_status_event_debug(struct MPT2SAS_ADAPTER *ioc,
6209 Mpi2EventDataIrOperationStatus_t *event_data)
6210{
6211 char *reason_str = NULL;
6212
6213 switch (event_data->RAIDOperation) {
6214 case MPI2_EVENT_IR_RAIDOP_RESYNC:
6215 reason_str = "resync";
6216 break;
6217 case MPI2_EVENT_IR_RAIDOP_ONLINE_CAP_EXPANSION:
6218 reason_str = "online capacity expansion";
6219 break;
6220 case MPI2_EVENT_IR_RAIDOP_CONSISTENCY_CHECK:
6221 reason_str = "consistency check";
6222 break;
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306223 case MPI2_EVENT_IR_RAIDOP_BACKGROUND_INIT:
6224 reason_str = "background init";
6225 break;
6226 case MPI2_EVENT_IR_RAIDOP_MAKE_DATA_CONSISTENT:
6227 reason_str = "make data consistent";
Eric Moore635374e2009-03-09 01:21:12 -06006228 break;
6229 }
6230
Kashyap, Desaiec6c2b42009-09-23 17:31:01 +05306231 if (!reason_str)
6232 return;
6233
Eric Moore635374e2009-03-09 01:21:12 -06006234 printk(MPT2SAS_INFO_FMT "raid operational status: (%s)"
6235 "\thandle(0x%04x), percent complete(%d)\n",
6236 ioc->name, reason_str,
6237 le16_to_cpu(event_data->VolDevHandle),
6238 event_data->PercentComplete);
6239}
6240#endif
6241
6242/**
6243 * _scsih_sas_ir_operation_status_event - handle RAID operation events
6244 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306245 * @fw_event: The fw_event_work object
Eric Moore635374e2009-03-09 01:21:12 -06006246 * Context: user.
6247 *
6248 * Return nothing.
6249 */
6250static void
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306251_scsih_sas_ir_operation_status_event(struct MPT2SAS_ADAPTER *ioc,
6252 struct fw_event_work *fw_event)
Eric Moore635374e2009-03-09 01:21:12 -06006253{
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306254 Mpi2EventDataIrOperationStatus_t *event_data = fw_event->event_data;
6255 static struct _raid_device *raid_device;
6256 unsigned long flags;
6257 u16 handle;
6258
Eric Moore635374e2009-03-09 01:21:12 -06006259#ifdef CONFIG_SCSI_MPT2SAS_LOGGING
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306260 if ((ioc->logging_level & MPT_DEBUG_EVENT_WORK_TASK)
6261 && !ioc->hide_ir_msg)
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306262 _scsih_sas_ir_operation_status_event_debug(ioc,
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306263 event_data);
Eric Moore635374e2009-03-09 01:21:12 -06006264#endif
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05306265
6266 /* code added for raid transport support */
6267 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC) {
6268
6269 handle = le16_to_cpu(event_data->VolDevHandle);
6270
6271 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6272 raid_device = _scsih_raid_device_find_by_handle(ioc, handle);
6273 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6274
6275 if (!raid_device)
6276 return;
6277
6278 if (event_data->RAIDOperation == MPI2_EVENT_IR_RAIDOP_RESYNC)
6279 raid_device->percent_complete =
6280 event_data->PercentComplete;
6281 }
Eric Moore635374e2009-03-09 01:21:12 -06006282}
6283
6284/**
Kashyap, Desai14695852010-03-30 10:52:44 +05306285 * _scsih_prep_device_scan - initialize parameters prior to device scan
6286 * @ioc: per adapter object
6287 *
6288 * Set the deleted flag prior to device scan. If the device is found during
6289 * the scan, then we clear the deleted flag.
6290 */
6291static void
6292_scsih_prep_device_scan(struct MPT2SAS_ADAPTER *ioc)
6293{
6294 struct MPT2SAS_DEVICE *sas_device_priv_data;
6295 struct scsi_device *sdev;
6296
6297 shost_for_each_device(sdev, ioc->shost) {
6298 sas_device_priv_data = sdev->hostdata;
6299 if (sas_device_priv_data && sas_device_priv_data->sas_target)
6300 sas_device_priv_data->sas_target->deleted = 1;
6301 }
6302}
6303
6304/**
Eric Moore635374e2009-03-09 01:21:12 -06006305 * _scsih_mark_responding_sas_device - mark a sas_devices as responding
6306 * @ioc: per adapter object
6307 * @sas_address: sas address
6308 * @slot: enclosure slot id
6309 * @handle: device handle
6310 *
6311 * After host reset, find out whether devices are still responding.
6312 * Used in _scsi_remove_unresponsive_sas_devices.
6313 *
6314 * Return nothing.
6315 */
6316static void
6317_scsih_mark_responding_sas_device(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6318 u16 slot, u16 handle)
6319{
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306320 struct MPT2SAS_TARGET *sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006321 struct scsi_target *starget;
6322 struct _sas_device *sas_device;
6323 unsigned long flags;
6324
6325 spin_lock_irqsave(&ioc->sas_device_lock, flags);
6326 list_for_each_entry(sas_device, &ioc->sas_device_list, list) {
6327 if (sas_device->sas_address == sas_address &&
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306328 sas_device->slot == slot) {
Eric Moore635374e2009-03-09 01:21:12 -06006329 sas_device->responding = 1;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306330 starget = sas_device->starget;
Kashyap, Desai14695852010-03-30 10:52:44 +05306331 if (starget && starget->hostdata) {
6332 sas_target_priv_data = starget->hostdata;
6333 sas_target_priv_data->tm_busy = 0;
6334 sas_target_priv_data->deleted = 0;
6335 } else
6336 sas_target_priv_data = NULL;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306337 if (starget)
6338 starget_printk(KERN_INFO, starget,
6339 "handle(0x%04x), sas_addr(0x%016llx), "
6340 "enclosure logical id(0x%016llx), "
6341 "slot(%d)\n", handle,
6342 (unsigned long long)sas_device->sas_address,
6343 (unsigned long long)
6344 sas_device->enclosure_logical_id,
6345 sas_device->slot);
Eric Moore635374e2009-03-09 01:21:12 -06006346 if (sas_device->handle == handle)
6347 goto out;
6348 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6349 sas_device->handle);
6350 sas_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306351 if (sas_target_priv_data)
6352 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006353 goto out;
6354 }
6355 }
6356 out:
6357 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
6358}
6359
6360/**
6361 * _scsih_search_responding_sas_devices -
6362 * @ioc: per adapter object
6363 *
6364 * After host reset, find out whether devices are still responding.
6365 * If not remove.
6366 *
6367 * Return nothing.
6368 */
6369static void
6370_scsih_search_responding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6371{
6372 Mpi2SasDevicePage0_t sas_device_pg0;
6373 Mpi2ConfigReply_t mpi_reply;
6374 u16 ioc_status;
6375 __le64 sas_address;
6376 u16 handle;
6377 u32 device_info;
6378 u16 slot;
6379
6380 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6381
6382 if (list_empty(&ioc->sas_device_list))
6383 return;
6384
6385 handle = 0xFFFF;
6386 while (!(mpt2sas_config_get_sas_device_pg0(ioc, &mpi_reply,
6387 &sas_device_pg0, MPI2_SAS_DEVICE_PGAD_FORM_GET_NEXT_HANDLE,
6388 handle))) {
6389 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6390 MPI2_IOCSTATUS_MASK;
6391 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6392 break;
6393 handle = le16_to_cpu(sas_device_pg0.DevHandle);
6394 device_info = le32_to_cpu(sas_device_pg0.DeviceInfo);
6395 if (!(_scsih_is_end_device(device_info)))
6396 continue;
6397 sas_address = le64_to_cpu(sas_device_pg0.SASAddress);
6398 slot = le16_to_cpu(sas_device_pg0.Slot);
6399 _scsih_mark_responding_sas_device(ioc, sas_address, slot,
6400 handle);
6401 }
6402}
6403
6404/**
6405 * _scsih_mark_responding_raid_device - mark a raid_device as responding
6406 * @ioc: per adapter object
6407 * @wwid: world wide identifier for raid volume
6408 * @handle: device handle
6409 *
6410 * After host reset, find out whether devices are still responding.
6411 * Used in _scsi_remove_unresponsive_raid_devices.
6412 *
6413 * Return nothing.
6414 */
6415static void
6416_scsih_mark_responding_raid_device(struct MPT2SAS_ADAPTER *ioc, u64 wwid,
6417 u16 handle)
6418{
6419 struct MPT2SAS_TARGET *sas_target_priv_data;
6420 struct scsi_target *starget;
6421 struct _raid_device *raid_device;
6422 unsigned long flags;
6423
6424 spin_lock_irqsave(&ioc->raid_device_lock, flags);
6425 list_for_each_entry(raid_device, &ioc->raid_device_list, list) {
6426 if (raid_device->wwid == wwid && raid_device->starget) {
Kashyap, Desai14695852010-03-30 10:52:44 +05306427 starget = raid_device->starget;
6428 if (starget && starget->hostdata) {
6429 sas_target_priv_data = starget->hostdata;
6430 sas_target_priv_data->deleted = 0;
6431 } else
6432 sas_target_priv_data = NULL;
Eric Moore635374e2009-03-09 01:21:12 -06006433 raid_device->responding = 1;
6434 starget_printk(KERN_INFO, raid_device->starget,
6435 "handle(0x%04x), wwid(0x%016llx)\n", handle,
6436 (unsigned long long)raid_device->wwid);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306437 /*
6438 * WARPDRIVE: The handles of the PDs might have changed
6439 * across the host reset so re-initialize the
6440 * required data for Direct IO
6441 */
6442 _scsih_init_warpdrive_properties(ioc, raid_device);
Eric Moore635374e2009-03-09 01:21:12 -06006443 if (raid_device->handle == handle)
6444 goto out;
6445 printk(KERN_INFO "\thandle changed from(0x%04x)!!!\n",
6446 raid_device->handle);
6447 raid_device->handle = handle;
Kashyap, Desai14695852010-03-30 10:52:44 +05306448 if (sas_target_priv_data)
6449 sas_target_priv_data->handle = handle;
Eric Moore635374e2009-03-09 01:21:12 -06006450 goto out;
6451 }
6452 }
6453 out:
6454 spin_unlock_irqrestore(&ioc->raid_device_lock, flags);
6455}
6456
6457/**
6458 * _scsih_search_responding_raid_devices -
6459 * @ioc: per adapter object
6460 *
6461 * After host reset, find out whether devices are still responding.
6462 * If not remove.
6463 *
6464 * Return nothing.
6465 */
6466static void
6467_scsih_search_responding_raid_devices(struct MPT2SAS_ADAPTER *ioc)
6468{
6469 Mpi2RaidVolPage1_t volume_pg1;
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306470 Mpi2RaidVolPage0_t volume_pg0;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306471 Mpi2RaidPhysDiskPage0_t pd_pg0;
Eric Moore635374e2009-03-09 01:21:12 -06006472 Mpi2ConfigReply_t mpi_reply;
6473 u16 ioc_status;
6474 u16 handle;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306475 u8 phys_disk_num;
Eric Moore635374e2009-03-09 01:21:12 -06006476
6477 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6478
6479 if (list_empty(&ioc->raid_device_list))
6480 return;
6481
6482 handle = 0xFFFF;
6483 while (!(mpt2sas_config_get_raid_volume_pg1(ioc, &mpi_reply,
6484 &volume_pg1, MPI2_RAID_VOLUME_PGAD_FORM_GET_NEXT_HANDLE, handle))) {
6485 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6486 MPI2_IOCSTATUS_MASK;
6487 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6488 break;
6489 handle = le16_to_cpu(volume_pg1.DevHandle);
Kashyap, Desaid417d1c2010-06-17 13:48:10 +05306490
6491 if (mpt2sas_config_get_raid_volume_pg0(ioc, &mpi_reply,
6492 &volume_pg0, MPI2_RAID_VOLUME_PGAD_FORM_HANDLE, handle,
6493 sizeof(Mpi2RaidVolPage0_t)))
6494 continue;
6495
6496 if (volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_OPTIMAL ||
6497 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_ONLINE ||
6498 volume_pg0.VolumeState == MPI2_RAID_VOL_STATE_DEGRADED)
6499 _scsih_mark_responding_raid_device(ioc,
6500 le64_to_cpu(volume_pg1.WWID), handle);
Eric Moore635374e2009-03-09 01:21:12 -06006501 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306502
6503 /* refresh the pd_handles */
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306504 if (!ioc->is_warpdrive) {
6505 phys_disk_num = 0xFF;
6506 memset(ioc->pd_handles, 0, ioc->pd_handles_sz);
6507 while (!(mpt2sas_config_get_phys_disk_pg0(ioc, &mpi_reply,
6508 &pd_pg0, MPI2_PHYSDISK_PGAD_FORM_GET_NEXT_PHYSDISKNUM,
6509 phys_disk_num))) {
6510 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6511 MPI2_IOCSTATUS_MASK;
6512 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6513 break;
6514 phys_disk_num = pd_pg0.PhysDiskNum;
6515 handle = le16_to_cpu(pd_pg0.DevHandle);
6516 set_bit(handle, ioc->pd_handles);
6517 }
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306518 }
Eric Moore635374e2009-03-09 01:21:12 -06006519}
6520
6521/**
6522 * _scsih_mark_responding_expander - mark a expander as responding
6523 * @ioc: per adapter object
6524 * @sas_address: sas address
6525 * @handle:
6526 *
6527 * After host reset, find out whether devices are still responding.
6528 * Used in _scsi_remove_unresponsive_expanders.
6529 *
6530 * Return nothing.
6531 */
6532static void
6533_scsih_mark_responding_expander(struct MPT2SAS_ADAPTER *ioc, u64 sas_address,
6534 u16 handle)
6535{
6536 struct _sas_node *sas_expander;
6537 unsigned long flags;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306538 int i;
Eric Moore635374e2009-03-09 01:21:12 -06006539
6540 spin_lock_irqsave(&ioc->sas_node_lock, flags);
6541 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306542 if (sas_expander->sas_address != sas_address)
6543 continue;
6544 sas_expander->responding = 1;
6545 if (sas_expander->handle == handle)
Eric Moore635374e2009-03-09 01:21:12 -06006546 goto out;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306547 printk(KERN_INFO "\texpander(0x%016llx): handle changed"
6548 " from(0x%04x) to (0x%04x)!!!\n",
6549 (unsigned long long)sas_expander->sas_address,
6550 sas_expander->handle, handle);
6551 sas_expander->handle = handle;
6552 for (i = 0 ; i < sas_expander->num_phys ; i++)
6553 sas_expander->phy[i].handle = handle;
6554 goto out;
Eric Moore635374e2009-03-09 01:21:12 -06006555 }
6556 out:
6557 spin_unlock_irqrestore(&ioc->sas_node_lock, flags);
6558}
6559
6560/**
6561 * _scsih_search_responding_expanders -
6562 * @ioc: per adapter object
6563 *
6564 * After host reset, find out whether devices are still responding.
6565 * If not remove.
6566 *
6567 * Return nothing.
6568 */
6569static void
6570_scsih_search_responding_expanders(struct MPT2SAS_ADAPTER *ioc)
6571{
6572 Mpi2ExpanderPage0_t expander_pg0;
6573 Mpi2ConfigReply_t mpi_reply;
6574 u16 ioc_status;
6575 __le64 sas_address;
6576 u16 handle;
6577
6578 printk(MPT2SAS_INFO_FMT "%s\n", ioc->name, __func__);
6579
6580 if (list_empty(&ioc->sas_expander_list))
6581 return;
6582
6583 handle = 0xFFFF;
6584 while (!(mpt2sas_config_get_expander_pg0(ioc, &mpi_reply, &expander_pg0,
6585 MPI2_SAS_EXPAND_PGAD_FORM_GET_NEXT_HNDL, handle))) {
6586
6587 ioc_status = le16_to_cpu(mpi_reply.IOCStatus) &
6588 MPI2_IOCSTATUS_MASK;
6589 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
6590 break;
6591
6592 handle = le16_to_cpu(expander_pg0.DevHandle);
6593 sas_address = le64_to_cpu(expander_pg0.SASAddress);
6594 printk(KERN_INFO "\texpander present: handle(0x%04x), "
6595 "sas_addr(0x%016llx)\n", handle,
6596 (unsigned long long)sas_address);
6597 _scsih_mark_responding_expander(ioc, sas_address, handle);
6598 }
6599
6600}
6601
6602/**
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306603 * _scsih_remove_unresponding_sas_devices - removing unresponding devices
Eric Moore635374e2009-03-09 01:21:12 -06006604 * @ioc: per adapter object
6605 *
6606 * Return nothing.
6607 */
6608static void
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306609_scsih_remove_unresponding_sas_devices(struct MPT2SAS_ADAPTER *ioc)
Eric Moore635374e2009-03-09 01:21:12 -06006610{
6611 struct _sas_device *sas_device, *sas_device_next;
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306612 struct _sas_node *sas_expander;
Eric Moore635374e2009-03-09 01:21:12 -06006613 struct _raid_device *raid_device, *raid_device_next;
Eric Moore635374e2009-03-09 01:21:12 -06006614
Eric Moore635374e2009-03-09 01:21:12 -06006615
6616 list_for_each_entry_safe(sas_device, sas_device_next,
6617 &ioc->sas_device_list, list) {
6618 if (sas_device->responding) {
6619 sas_device->responding = 0;
6620 continue;
6621 }
6622 if (sas_device->starget)
6623 starget_printk(KERN_INFO, sas_device->starget,
6624 "removing: handle(0x%04x), sas_addr(0x%016llx), "
6625 "enclosure logical id(0x%016llx), slot(%d)\n",
6626 sas_device->handle,
6627 (unsigned long long)sas_device->sas_address,
6628 (unsigned long long)
6629 sas_device->enclosure_logical_id,
6630 sas_device->slot);
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306631 _scsih_remove_device(ioc, sas_device);
Eric Moore635374e2009-03-09 01:21:12 -06006632 }
6633
6634 list_for_each_entry_safe(raid_device, raid_device_next,
6635 &ioc->raid_device_list, list) {
6636 if (raid_device->responding) {
6637 raid_device->responding = 0;
6638 continue;
6639 }
6640 if (raid_device->starget) {
6641 starget_printk(KERN_INFO, raid_device->starget,
6642 "removing: handle(0x%04x), wwid(0x%016llx)\n",
6643 raid_device->handle,
6644 (unsigned long long)raid_device->wwid);
6645 scsi_remove_target(&raid_device->starget->dev);
6646 }
6647 _scsih_raid_device_remove(ioc, raid_device);
6648 }
6649
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306650 retry_expander_search:
6651 sas_expander = NULL;
6652 list_for_each_entry(sas_expander, &ioc->sas_expander_list, list) {
Eric Moore635374e2009-03-09 01:21:12 -06006653 if (sas_expander->responding) {
6654 sas_expander->responding = 0;
6655 continue;
6656 }
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05306657 mpt2sas_expander_remove(ioc, sas_expander->sas_address);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306658 goto retry_expander_search;
6659 }
6660}
6661
6662/**
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306663 * _scsih_hide_unhide_sas_devices - add/remove device to/from OS
6664 * @ioc: per adapter object
6665 *
6666 * Return nothing.
6667 */
6668static void
6669_scsih_hide_unhide_sas_devices(struct MPT2SAS_ADAPTER *ioc)
6670{
6671 struct _sas_device *sas_device, *sas_device_next;
6672
6673 if (!ioc->is_warpdrive || ioc->mfg_pg10_hide_flag !=
6674 MFG_PAGE10_HIDE_IF_VOL_PRESENT)
6675 return;
6676
6677 if (ioc->hide_drives) {
6678 if (_scsih_get_num_volumes(ioc))
6679 return;
6680 ioc->hide_drives = 0;
6681 list_for_each_entry_safe(sas_device, sas_device_next,
6682 &ioc->sas_device_list, list) {
6683 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
6684 sas_device->sas_address_parent)) {
6685 _scsih_sas_device_remove(ioc, sas_device);
6686 } else if (!sas_device->starget) {
6687 mpt2sas_transport_port_remove(ioc,
6688 sas_device->sas_address,
6689 sas_device->sas_address_parent);
6690 _scsih_sas_device_remove(ioc, sas_device);
6691 }
6692 }
6693 } else {
6694 if (!_scsih_get_num_volumes(ioc))
6695 return;
6696 ioc->hide_drives = 1;
6697 list_for_each_entry_safe(sas_device, sas_device_next,
6698 &ioc->sas_device_list, list) {
6699 mpt2sas_transport_port_remove(ioc,
6700 sas_device->sas_address,
6701 sas_device->sas_address_parent);
6702 }
6703 }
6704}
6705
6706/**
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306707 * mpt2sas_scsih_reset_handler - reset callback handler (for scsih)
6708 * @ioc: per adapter object
6709 * @reset_phase: phase
6710 *
6711 * The handler for doing any required cleanup or initialization.
6712 *
6713 * The reset phase can be MPT2_IOC_PRE_RESET, MPT2_IOC_AFTER_RESET,
6714 * MPT2_IOC_DONE_RESET
6715 *
6716 * Return nothing.
6717 */
6718void
6719mpt2sas_scsih_reset_handler(struct MPT2SAS_ADAPTER *ioc, int reset_phase)
6720{
6721 switch (reset_phase) {
6722 case MPT2_IOC_PRE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306723 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306724 "MPT2_IOC_PRE_RESET\n", ioc->name, __func__));
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306725 break;
6726 case MPT2_IOC_AFTER_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306727 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306728 "MPT2_IOC_AFTER_RESET\n", ioc->name, __func__));
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306729 if (ioc->scsih_cmds.status & MPT2_CMD_PENDING) {
6730 ioc->scsih_cmds.status |= MPT2_CMD_RESET;
6731 mpt2sas_base_free_smid(ioc, ioc->scsih_cmds.smid);
6732 complete(&ioc->scsih_cmds.done);
6733 }
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306734 if (ioc->tm_cmds.status & MPT2_CMD_PENDING) {
6735 ioc->tm_cmds.status |= MPT2_CMD_RESET;
6736 mpt2sas_base_free_smid(ioc, ioc->tm_cmds.smid);
6737 complete(&ioc->tm_cmds.done);
6738 }
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306739 _scsih_fw_event_cleanup_queue(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306740 _scsih_flush_running_cmds(ioc);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306741 _scsih_queue_rescan(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306742 break;
6743 case MPT2_IOC_DONE_RESET:
Kashyap, Desaieabb08a2010-06-17 13:43:57 +05306744 dtmprintk(ioc, printk(MPT2SAS_INFO_FMT "%s: "
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306745 "MPT2_IOC_DONE_RESET\n", ioc->name, __func__));
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05306746 _scsih_sas_host_refresh(ioc);
Kashyap, Desai14695852010-03-30 10:52:44 +05306747 _scsih_prep_device_scan(ioc);
6748 _scsih_search_responding_sas_devices(ioc);
6749 _scsih_search_responding_raid_devices(ioc);
6750 _scsih_search_responding_expanders(ioc);
Kashyap, Desaicd4e12e2009-08-20 13:20:54 +05306751 break;
Eric Moore635374e2009-03-09 01:21:12 -06006752 }
6753}
6754
6755/**
6756 * _firmware_event_work - delayed task for processing firmware events
6757 * @ioc: per adapter object
6758 * @work: equal to the fw_event_work object
6759 * Context: user.
6760 *
6761 * Return nothing.
6762 */
6763static void
6764_firmware_event_work(struct work_struct *work)
6765{
6766 struct fw_event_work *fw_event = container_of(work,
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306767 struct fw_event_work, delayed_work.work);
Eric Moore635374e2009-03-09 01:21:12 -06006768 unsigned long flags;
6769 struct MPT2SAS_ADAPTER *ioc = fw_event->ioc;
6770
Eric Moore635374e2009-03-09 01:21:12 -06006771 /* the queue is being flushed so ignore this event */
Eric Moore3cb54692010-07-08 14:44:34 -06006772 if (ioc->remove_host || fw_event->cancel_pending_work ||
6773 ioc->pci_error_recovery) {
Eric Moore635374e2009-03-09 01:21:12 -06006774 _scsih_fw_event_free(ioc, fw_event);
6775 return;
6776 }
Eric Moore635374e2009-03-09 01:21:12 -06006777
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306778 if (fw_event->event == MPT2SAS_RESCAN_AFTER_HOST_RESET) {
6779 _scsih_fw_event_free(ioc, fw_event);
6780 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
6781 if (ioc->shost_recovery) {
6782 init_completion(&ioc->shost_recovery_done);
6783 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6784 flags);
6785 wait_for_completion(&ioc->shost_recovery_done);
6786 } else
6787 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock,
6788 flags);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05306789 _scsih_remove_unresponding_sas_devices(ioc);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306790 _scsih_hide_unhide_sas_devices(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06006791 return;
6792 }
Eric Moore635374e2009-03-09 01:21:12 -06006793
6794 switch (fw_event->event) {
Kashyap, Desai3ace8e02011-05-04 16:35:58 +05306795 case MPT2SAS_TURN_ON_FAULT_LED:
6796 _scsih_turn_on_fault_led(ioc, fw_event->device_handle);
6797 break;
Eric Moore635374e2009-03-09 01:21:12 -06006798 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306799 _scsih_sas_topology_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006800 break;
6801 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306802 _scsih_sas_device_status_change_event(ioc,
6803 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006804 break;
6805 case MPI2_EVENT_SAS_DISCOVERY:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306806 _scsih_sas_discovery_event(ioc,
6807 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006808 break;
6809 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306810 _scsih_sas_broadcast_primative_event(ioc,
6811 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006812 break;
6813 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
6814 _scsih_sas_enclosure_dev_status_change_event(ioc,
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306815 fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006816 break;
6817 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306818 _scsih_sas_ir_config_change_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006819 break;
6820 case MPI2_EVENT_IR_VOLUME:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306821 _scsih_sas_ir_volume_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006822 break;
6823 case MPI2_EVENT_IR_PHYSICAL_DISK:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306824 _scsih_sas_ir_physical_disk_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006825 break;
6826 case MPI2_EVENT_IR_OPERATION_STATUS:
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306827 _scsih_sas_ir_operation_status_event(ioc, fw_event);
Eric Moore635374e2009-03-09 01:21:12 -06006828 break;
Eric Moore635374e2009-03-09 01:21:12 -06006829 }
6830 _scsih_fw_event_free(ioc, fw_event);
6831}
6832
6833/**
6834 * mpt2sas_scsih_event_callback - firmware event handler (called at ISR time)
6835 * @ioc: per adapter object
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306836 * @msix_index: MSIX table index supplied by the OS
Eric Moore635374e2009-03-09 01:21:12 -06006837 * @reply: reply message frame(lower 32bit addr)
6838 * Context: interrupt.
6839 *
6840 * This function merely adds a new work task into ioc->firmware_event_thread.
6841 * The tasks are worked from _firmware_event_work in user context.
6842 *
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306843 * Return 1 meaning mf should be freed from _base_interrupt
6844 * 0 means the mf is freed from this function.
Eric Moore635374e2009-03-09 01:21:12 -06006845 */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306846u8
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306847mpt2sas_scsih_event_callback(struct MPT2SAS_ADAPTER *ioc, u8 msix_index,
6848 u32 reply)
Eric Moore635374e2009-03-09 01:21:12 -06006849{
6850 struct fw_event_work *fw_event;
6851 Mpi2EventNotificationReply_t *mpi_reply;
Eric Moore635374e2009-03-09 01:21:12 -06006852 u16 event;
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306853 u16 sz;
Eric Moore635374e2009-03-09 01:21:12 -06006854
6855 /* events turned off due to host reset or driver unloading */
Eric Moore3cb54692010-07-08 14:44:34 -06006856 if (ioc->remove_host || ioc->pci_error_recovery)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306857 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006858
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306859 mpi_reply = mpt2sas_base_get_reply_virt_addr(ioc, reply);
Eric Moore635374e2009-03-09 01:21:12 -06006860 event = le16_to_cpu(mpi_reply->Event);
6861
6862 switch (event) {
6863 /* handle these */
6864 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
6865 {
6866 Mpi2EventDataSasBroadcastPrimitive_t *baen_data =
6867 (Mpi2EventDataSasBroadcastPrimitive_t *)
6868 mpi_reply->EventData;
6869
6870 if (baen_data->Primitive !=
6871 MPI2_EVENT_PRIMITIVE_ASYNCHRONOUS_EVENT ||
6872 ioc->broadcast_aen_busy)
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306873 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006874 ioc->broadcast_aen_busy = 1;
6875 break;
6876 }
6877
6878 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
6879 _scsih_check_topo_delete_events(ioc,
6880 (Mpi2EventDataSasTopologyChangeList_t *)
6881 mpi_reply->EventData);
6882 break;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05306883 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
6884 _scsih_check_ir_config_unhide_events(ioc,
6885 (Mpi2EventDataIrConfigChangeList_t *)
6886 mpi_reply->EventData);
6887 break;
6888 case MPI2_EVENT_IR_VOLUME:
6889 _scsih_check_volume_delete_events(ioc,
6890 (Mpi2EventDataIrVolume_t *)
6891 mpi_reply->EventData);
6892 break;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05306893 case MPI2_EVENT_LOG_ENTRY_ADDED:
6894 {
6895 Mpi2EventDataLogEntryAdded_t *log_entry;
6896 u32 *log_code;
6897
6898 if (!ioc->is_warpdrive)
6899 break;
6900
6901 log_entry = (Mpi2EventDataLogEntryAdded_t *)
6902 mpi_reply->EventData;
6903 log_code = (u32 *)log_entry->LogData;
6904
6905 if (le16_to_cpu(log_entry->LogEntryQualifier)
6906 != MPT2_WARPDRIVE_LOGENTRY)
6907 break;
6908
6909 switch (le32_to_cpu(*log_code)) {
6910 case MPT2_WARPDRIVE_LC_SSDT:
6911 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
6912 "IO Throttling has occurred in the WarpDrive "
6913 "subsystem. Check WarpDrive documentation for "
6914 "additional details.\n", ioc->name);
6915 break;
6916 case MPT2_WARPDRIVE_LC_SSDLW:
6917 printk(MPT2SAS_WARN_FMT "WarpDrive Warning: "
6918 "Program/Erase Cycles for the WarpDrive subsystem "
6919 "in degraded range. Check WarpDrive documentation "
6920 "for additional details.\n", ioc->name);
6921 break;
6922 case MPT2_WARPDRIVE_LC_SSDLF:
6923 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
6924 "There are no Program/Erase Cycles for the "
6925 "WarpDrive subsystem. The storage device will be "
6926 "in read-only mode. Check WarpDrive documentation "
6927 "for additional details.\n", ioc->name);
6928 break;
6929 case MPT2_WARPDRIVE_LC_BRMF:
6930 printk(MPT2SAS_ERR_FMT "WarpDrive Fatal Error: "
6931 "The Backup Rail Monitor has failed on the "
6932 "WarpDrive subsystem. Check WarpDrive "
6933 "documentation for additional details.\n",
6934 ioc->name);
6935 break;
6936 }
6937
6938 break;
6939 }
Eric Moore635374e2009-03-09 01:21:12 -06006940 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
6941 case MPI2_EVENT_IR_OPERATION_STATUS:
6942 case MPI2_EVENT_SAS_DISCOVERY:
6943 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
Eric Moore635374e2009-03-09 01:21:12 -06006944 case MPI2_EVENT_IR_PHYSICAL_DISK:
Eric Moore635374e2009-03-09 01:21:12 -06006945 break;
6946
6947 default: /* ignore the rest */
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306948 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006949 }
6950
6951 fw_event = kzalloc(sizeof(struct fw_event_work), GFP_ATOMIC);
6952 if (!fw_event) {
6953 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6954 ioc->name, __FILE__, __LINE__, __func__);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306955 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006956 }
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306957 sz = le16_to_cpu(mpi_reply->EventDataLength) * 4;
6958 fw_event->event_data = kzalloc(sz, GFP_ATOMIC);
Eric Moore635374e2009-03-09 01:21:12 -06006959 if (!fw_event->event_data) {
6960 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
6961 ioc->name, __FILE__, __LINE__, __func__);
6962 kfree(fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306963 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006964 }
6965
6966 memcpy(fw_event->event_data, mpi_reply->EventData,
Kashyap, Desaie94f6742010-03-17 16:24:52 +05306967 sz);
Eric Moore635374e2009-03-09 01:21:12 -06006968 fw_event->ioc = ioc;
Kashyap, Desai7b936b02009-09-25 11:44:41 +05306969 fw_event->VF_ID = mpi_reply->VF_ID;
6970 fw_event->VP_ID = mpi_reply->VP_ID;
Eric Moore635374e2009-03-09 01:21:12 -06006971 fw_event->event = event;
6972 _scsih_fw_event_add(ioc, fw_event);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05306973 return 1;
Eric Moore635374e2009-03-09 01:21:12 -06006974}
6975
6976/* shost template */
6977static struct scsi_host_template scsih_driver_template = {
6978 .module = THIS_MODULE,
6979 .name = "Fusion MPT SAS Host",
6980 .proc_name = MPT2SAS_DRIVER_NAME,
Eric Moored5d135b2009-05-18 13:02:08 -06006981 .queuecommand = _scsih_qcmd,
6982 .target_alloc = _scsih_target_alloc,
6983 .slave_alloc = _scsih_slave_alloc,
6984 .slave_configure = _scsih_slave_configure,
6985 .target_destroy = _scsih_target_destroy,
6986 .slave_destroy = _scsih_slave_destroy,
6987 .change_queue_depth = _scsih_change_queue_depth,
6988 .change_queue_type = _scsih_change_queue_type,
6989 .eh_abort_handler = _scsih_abort,
6990 .eh_device_reset_handler = _scsih_dev_reset,
6991 .eh_target_reset_handler = _scsih_target_reset,
6992 .eh_host_reset_handler = _scsih_host_reset,
6993 .bios_param = _scsih_bios_param,
Eric Moore635374e2009-03-09 01:21:12 -06006994 .can_queue = 1,
6995 .this_id = -1,
6996 .sg_tablesize = MPT2SAS_SG_DEPTH,
6997 .max_sectors = 8192,
6998 .cmd_per_lun = 7,
6999 .use_clustering = ENABLE_CLUSTERING,
7000 .shost_attrs = mpt2sas_host_attrs,
7001 .sdev_attrs = mpt2sas_dev_attrs,
7002};
7003
7004/**
7005 * _scsih_expander_node_remove - removing expander device from list.
7006 * @ioc: per adapter object
7007 * @sas_expander: the sas_device object
7008 * Context: Calling function should acquire ioc->sas_node_lock.
7009 *
7010 * Removing object and freeing associated memory from the
7011 * ioc->sas_expander_list.
7012 *
7013 * Return nothing.
7014 */
7015static void
7016_scsih_expander_node_remove(struct MPT2SAS_ADAPTER *ioc,
7017 struct _sas_node *sas_expander)
7018{
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307019 struct _sas_port *mpt2sas_port, *next;
Eric Moore635374e2009-03-09 01:21:12 -06007020
7021 /* remove sibling ports attached to this expander */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307022 list_for_each_entry_safe(mpt2sas_port, next,
Eric Moore635374e2009-03-09 01:21:12 -06007023 &sas_expander->sas_port_list, port_list) {
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307024 if (ioc->shost_recovery)
7025 return;
Eric Moore635374e2009-03-09 01:21:12 -06007026 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307027 SAS_END_DEVICE)
7028 mpt2sas_device_remove(ioc,
7029 mpt2sas_port->remote_identify.sas_address);
7030 else if (mpt2sas_port->remote_identify.device_type ==
7031 SAS_EDGE_EXPANDER_DEVICE ||
Eric Moore635374e2009-03-09 01:21:12 -06007032 mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307033 SAS_FANOUT_EXPANDER_DEVICE)
7034 mpt2sas_expander_remove(ioc,
7035 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007036 }
7037
7038 mpt2sas_transport_port_remove(ioc, sas_expander->sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307039 sas_expander->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007040
7041 printk(MPT2SAS_INFO_FMT "expander_remove: handle"
7042 "(0x%04x), sas_addr(0x%016llx)\n", ioc->name,
7043 sas_expander->handle, (unsigned long long)
7044 sas_expander->sas_address);
7045
Eric Moore635374e2009-03-09 01:21:12 -06007046 kfree(sas_expander->phy);
7047 kfree(sas_expander);
7048}
7049
7050/**
Kashyap, Desai744090d2009-10-05 15:56:56 +05307051 * _scsih_ir_shutdown - IR shutdown notification
7052 * @ioc: per adapter object
7053 *
7054 * Sending RAID Action to alert the Integrated RAID subsystem of the IOC that
7055 * the host system is shutting down.
7056 *
7057 * Return nothing.
7058 */
7059static void
7060_scsih_ir_shutdown(struct MPT2SAS_ADAPTER *ioc)
7061{
7062 Mpi2RaidActionRequest_t *mpi_request;
7063 Mpi2RaidActionReply_t *mpi_reply;
7064 u16 smid;
7065
7066 /* is IR firmware build loaded ? */
7067 if (!ioc->ir_firmware)
7068 return;
7069
7070 /* are there any volumes ? */
7071 if (list_empty(&ioc->raid_device_list))
7072 return;
7073
7074 mutex_lock(&ioc->scsih_cmds.mutex);
7075
7076 if (ioc->scsih_cmds.status != MPT2_CMD_NOT_USED) {
7077 printk(MPT2SAS_ERR_FMT "%s: scsih_cmd in use\n",
7078 ioc->name, __func__);
7079 goto out;
7080 }
7081 ioc->scsih_cmds.status = MPT2_CMD_PENDING;
7082
7083 smid = mpt2sas_base_get_smid(ioc, ioc->scsih_cb_idx);
7084 if (!smid) {
7085 printk(MPT2SAS_ERR_FMT "%s: failed obtaining a smid\n",
7086 ioc->name, __func__);
7087 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7088 goto out;
7089 }
7090
7091 mpi_request = mpt2sas_base_get_msg_frame(ioc, smid);
7092 ioc->scsih_cmds.smid = smid;
7093 memset(mpi_request, 0, sizeof(Mpi2RaidActionRequest_t));
7094
7095 mpi_request->Function = MPI2_FUNCTION_RAID_ACTION;
7096 mpi_request->Action = MPI2_RAID_ACTION_SYSTEM_SHUTDOWN_INITIATED;
7097
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307098 if (!ioc->hide_ir_msg)
7099 printk(MPT2SAS_INFO_FMT "IR shutdown (sending)\n", ioc->name);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307100 init_completion(&ioc->scsih_cmds.done);
7101 mpt2sas_base_put_smid_default(ioc, smid);
7102 wait_for_completion_timeout(&ioc->scsih_cmds.done, 10*HZ);
7103
7104 if (!(ioc->scsih_cmds.status & MPT2_CMD_COMPLETE)) {
7105 printk(MPT2SAS_ERR_FMT "%s: timeout\n",
7106 ioc->name, __func__);
7107 goto out;
7108 }
7109
7110 if (ioc->scsih_cmds.status & MPT2_CMD_REPLY_VALID) {
7111 mpi_reply = ioc->scsih_cmds.reply;
7112
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307113 if (!ioc->hide_ir_msg)
7114 printk(MPT2SAS_INFO_FMT "IR shutdown (complete): "
7115 "ioc_status(0x%04x), loginfo(0x%08x)\n",
7116 ioc->name, le16_to_cpu(mpi_reply->IOCStatus),
7117 le32_to_cpu(mpi_reply->IOCLogInfo));
Kashyap, Desai744090d2009-10-05 15:56:56 +05307118 }
7119
7120 out:
7121 ioc->scsih_cmds.status = MPT2_CMD_NOT_USED;
7122 mutex_unlock(&ioc->scsih_cmds.mutex);
7123}
7124
7125/**
7126 * _scsih_shutdown - routine call during system shutdown
7127 * @pdev: PCI device struct
7128 *
7129 * Return nothing.
7130 */
7131static void
7132_scsih_shutdown(struct pci_dev *pdev)
7133{
7134 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7135 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307136 struct workqueue_struct *wq;
7137 unsigned long flags;
7138
7139 ioc->remove_host = 1;
7140 _scsih_fw_event_cleanup_queue(ioc);
7141
7142 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7143 wq = ioc->firmware_event_thread;
7144 ioc->firmware_event_thread = NULL;
7145 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7146 if (wq)
7147 destroy_workqueue(wq);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307148
7149 _scsih_ir_shutdown(ioc);
7150 mpt2sas_base_detach(ioc);
7151}
7152
7153/**
Eric Moored5d135b2009-05-18 13:02:08 -06007154 * _scsih_remove - detach and remove add host
Eric Moore635374e2009-03-09 01:21:12 -06007155 * @pdev: PCI device struct
7156 *
Kashyap, Desai744090d2009-10-05 15:56:56 +05307157 * Routine called when unloading the driver.
Eric Moore635374e2009-03-09 01:21:12 -06007158 * Return nothing.
7159 */
7160static void __devexit
Eric Moored5d135b2009-05-18 13:02:08 -06007161_scsih_remove(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007162{
7163 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7164 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307165 struct _sas_port *mpt2sas_port, *next_port;
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307166 struct _raid_device *raid_device, *next;
7167 struct MPT2SAS_TARGET *sas_target_priv_data;
Eric Moore635374e2009-03-09 01:21:12 -06007168 struct workqueue_struct *wq;
7169 unsigned long flags;
7170
7171 ioc->remove_host = 1;
Kashyap, Desaif1c35e62010-03-09 16:31:43 +05307172 _scsih_fw_event_cleanup_queue(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007173
7174 spin_lock_irqsave(&ioc->fw_event_lock, flags);
7175 wq = ioc->firmware_event_thread;
7176 ioc->firmware_event_thread = NULL;
7177 spin_unlock_irqrestore(&ioc->fw_event_lock, flags);
7178 if (wq)
7179 destroy_workqueue(wq);
7180
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307181 /* release all the volumes */
Kashyap, Desai3a9c9132011-01-04 11:40:23 +05307182 _scsih_ir_shutdown(ioc);
Kashyap, Desaid7384b22009-12-16 18:50:06 +05307183 list_for_each_entry_safe(raid_device, next, &ioc->raid_device_list,
7184 list) {
7185 if (raid_device->starget) {
7186 sas_target_priv_data =
7187 raid_device->starget->hostdata;
7188 sas_target_priv_data->deleted = 1;
7189 scsi_remove_target(&raid_device->starget->dev);
7190 }
7191 printk(MPT2SAS_INFO_FMT "removing handle(0x%04x), wwid"
7192 "(0x%016llx)\n", ioc->name, raid_device->handle,
7193 (unsigned long long) raid_device->wwid);
7194 _scsih_raid_device_remove(ioc, raid_device);
7195 }
7196
Eric Moore635374e2009-03-09 01:21:12 -06007197 /* free ports attached to the sas_host */
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307198 list_for_each_entry_safe(mpt2sas_port, next_port,
Eric Moore635374e2009-03-09 01:21:12 -06007199 &ioc->sas_hba.sas_port_list, port_list) {
7200 if (mpt2sas_port->remote_identify.device_type ==
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307201 SAS_END_DEVICE)
7202 mpt2sas_device_remove(ioc,
Eric Moore635374e2009-03-09 01:21:12 -06007203 mpt2sas_port->remote_identify.sas_address);
Kashyap, Desai7f6f7942010-11-13 04:35:30 +05307204 else if (mpt2sas_port->remote_identify.device_type ==
7205 SAS_EDGE_EXPANDER_DEVICE ||
7206 mpt2sas_port->remote_identify.device_type ==
7207 SAS_FANOUT_EXPANDER_DEVICE)
7208 mpt2sas_expander_remove(ioc,
7209 mpt2sas_port->remote_identify.sas_address);
Eric Moore635374e2009-03-09 01:21:12 -06007210 }
7211
7212 /* free phys attached to the sas_host */
7213 if (ioc->sas_hba.num_phys) {
7214 kfree(ioc->sas_hba.phy);
7215 ioc->sas_hba.phy = NULL;
7216 ioc->sas_hba.num_phys = 0;
7217 }
7218
7219 sas_remove_host(shost);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307220 _scsih_shutdown(pdev);
Eric Moore635374e2009-03-09 01:21:12 -06007221 list_del(&ioc->list);
7222 scsi_remove_host(shost);
7223 scsi_host_put(shost);
7224}
7225
7226/**
7227 * _scsih_probe_boot_devices - reports 1st device
7228 * @ioc: per adapter object
7229 *
7230 * If specified in bios page 2, this routine reports the 1st
7231 * device scsi-ml or sas transport for persistent boot device
7232 * purposes. Please refer to function _scsih_determine_boot_device()
7233 */
7234static void
7235_scsih_probe_boot_devices(struct MPT2SAS_ADAPTER *ioc)
7236{
7237 u8 is_raid;
7238 void *device;
7239 struct _sas_device *sas_device;
7240 struct _raid_device *raid_device;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307241 u16 handle;
7242 u64 sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007243 u64 sas_address;
7244 unsigned long flags;
7245 int rc;
7246
7247 device = NULL;
7248 if (ioc->req_boot_device.device) {
7249 device = ioc->req_boot_device.device;
7250 is_raid = ioc->req_boot_device.is_raid;
7251 } else if (ioc->req_alt_boot_device.device) {
7252 device = ioc->req_alt_boot_device.device;
7253 is_raid = ioc->req_alt_boot_device.is_raid;
7254 } else if (ioc->current_boot_device.device) {
7255 device = ioc->current_boot_device.device;
7256 is_raid = ioc->current_boot_device.is_raid;
7257 }
7258
7259 if (!device)
7260 return;
7261
7262 if (is_raid) {
7263 raid_device = device;
7264 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7265 raid_device->id, 0);
7266 if (rc)
7267 _scsih_raid_device_remove(ioc, raid_device);
7268 } else {
7269 sas_device = device;
7270 handle = sas_device->handle;
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307271 sas_address_parent = sas_device->sas_address_parent;
Eric Moore635374e2009-03-09 01:21:12 -06007272 sas_address = sas_device->sas_address;
7273 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7274 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7275 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307276
7277 if (ioc->hide_drives)
7278 return;
Eric Moore635374e2009-03-09 01:21:12 -06007279 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307280 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007281 _scsih_sas_device_remove(ioc, sas_device);
7282 } else if (!sas_device->starget) {
7283 mpt2sas_transport_port_remove(ioc, sas_address,
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307284 sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007285 _scsih_sas_device_remove(ioc, sas_device);
7286 }
7287 }
7288}
7289
7290/**
7291 * _scsih_probe_raid - reporting raid volumes to scsi-ml
7292 * @ioc: per adapter object
7293 *
7294 * Called during initial loading of the driver.
7295 */
7296static void
7297_scsih_probe_raid(struct MPT2SAS_ADAPTER *ioc)
7298{
7299 struct _raid_device *raid_device, *raid_next;
7300 int rc;
7301
7302 list_for_each_entry_safe(raid_device, raid_next,
7303 &ioc->raid_device_list, list) {
7304 if (raid_device->starget)
7305 continue;
7306 rc = scsi_add_device(ioc->shost, RAID_CHANNEL,
7307 raid_device->id, 0);
7308 if (rc)
7309 _scsih_raid_device_remove(ioc, raid_device);
7310 }
7311}
7312
7313/**
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307314 * _scsih_probe_sas - reporting sas devices to sas transport
Eric Moore635374e2009-03-09 01:21:12 -06007315 * @ioc: per adapter object
7316 *
7317 * Called during initial loading of the driver.
7318 */
7319static void
7320_scsih_probe_sas(struct MPT2SAS_ADAPTER *ioc)
7321{
7322 struct _sas_device *sas_device, *next;
7323 unsigned long flags;
Eric Moore635374e2009-03-09 01:21:12 -06007324
7325 /* SAS Device List */
7326 list_for_each_entry_safe(sas_device, next, &ioc->sas_device_init_list,
7327 list) {
7328 spin_lock_irqsave(&ioc->sas_device_lock, flags);
7329 list_move_tail(&sas_device->list, &ioc->sas_device_list);
7330 spin_unlock_irqrestore(&ioc->sas_device_lock, flags);
7331
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307332 if (ioc->hide_drives)
7333 continue;
7334
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307335 if (!mpt2sas_transport_port_add(ioc, sas_device->handle,
7336 sas_device->sas_address_parent)) {
Eric Moore635374e2009-03-09 01:21:12 -06007337 _scsih_sas_device_remove(ioc, sas_device);
7338 } else if (!sas_device->starget) {
Kashyap, Desaic5e039b2009-09-23 17:21:29 +05307339 mpt2sas_transport_port_remove(ioc,
7340 sas_device->sas_address,
7341 sas_device->sas_address_parent);
Eric Moore635374e2009-03-09 01:21:12 -06007342 _scsih_sas_device_remove(ioc, sas_device);
7343 }
7344 }
7345}
7346
7347/**
7348 * _scsih_probe_devices - probing for devices
7349 * @ioc: per adapter object
7350 *
7351 * Called during initial loading of the driver.
7352 */
7353static void
7354_scsih_probe_devices(struct MPT2SAS_ADAPTER *ioc)
7355{
7356 u16 volume_mapping_flags =
7357 le16_to_cpu(ioc->ioc_pg8.IRVolumeMappingFlags) &
7358 MPI2_IOCPAGE8_IRFLAGS_MASK_VOLUME_MAPPING_MODE;
7359
7360 if (!(ioc->facts.ProtocolFlags & MPI2_IOCFACTS_PROTOCOL_SCSI_INITIATOR))
7361 return; /* return when IOC doesn't support initiator mode */
7362
7363 _scsih_probe_boot_devices(ioc);
7364
7365 if (ioc->ir_firmware) {
7366 if ((volume_mapping_flags &
7367 MPI2_IOCPAGE8_IRFLAGS_HIGH_VOLUME_MAPPING)) {
7368 _scsih_probe_sas(ioc);
7369 _scsih_probe_raid(ioc);
7370 } else {
7371 _scsih_probe_raid(ioc);
7372 _scsih_probe_sas(ioc);
7373 }
7374 } else
7375 _scsih_probe_sas(ioc);
7376}
7377
7378/**
Eric Moored5d135b2009-05-18 13:02:08 -06007379 * _scsih_probe - attach and add scsi host
Eric Moore635374e2009-03-09 01:21:12 -06007380 * @pdev: PCI device struct
7381 * @id: pci device id
7382 *
7383 * Returns 0 success, anything else error.
7384 */
7385static int
Eric Moored5d135b2009-05-18 13:02:08 -06007386_scsih_probe(struct pci_dev *pdev, const struct pci_device_id *id)
Eric Moore635374e2009-03-09 01:21:12 -06007387{
7388 struct MPT2SAS_ADAPTER *ioc;
7389 struct Scsi_Host *shost;
7390
7391 shost = scsi_host_alloc(&scsih_driver_template,
7392 sizeof(struct MPT2SAS_ADAPTER));
7393 if (!shost)
7394 return -ENODEV;
7395
7396 /* init local params */
7397 ioc = shost_priv(shost);
7398 memset(ioc, 0, sizeof(struct MPT2SAS_ADAPTER));
7399 INIT_LIST_HEAD(&ioc->list);
Eric Mooreba33fad2009-03-15 21:37:18 -06007400 list_add_tail(&ioc->list, &mpt2sas_ioc_list);
Eric Moore635374e2009-03-09 01:21:12 -06007401 ioc->shost = shost;
7402 ioc->id = mpt_ids++;
7403 sprintf(ioc->name, "%s%d", MPT2SAS_DRIVER_NAME, ioc->id);
7404 ioc->pdev = pdev;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307405 if (id->device == MPI2_MFGPAGE_DEVID_SSS6200) {
7406 ioc->is_warpdrive = 1;
7407 ioc->hide_ir_msg = 1;
7408 } else
7409 ioc->mfg_pg10_hide_flag = MFG_PAGE10_EXPOSE_ALL_DISKS;
Eric Moore635374e2009-03-09 01:21:12 -06007410 ioc->scsi_io_cb_idx = scsi_io_cb_idx;
7411 ioc->tm_cb_idx = tm_cb_idx;
7412 ioc->ctl_cb_idx = ctl_cb_idx;
7413 ioc->base_cb_idx = base_cb_idx;
7414 ioc->transport_cb_idx = transport_cb_idx;
Kashyap, Desai744090d2009-10-05 15:56:56 +05307415 ioc->scsih_cb_idx = scsih_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007416 ioc->config_cb_idx = config_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307417 ioc->tm_tr_cb_idx = tm_tr_cb_idx;
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307418 ioc->tm_tr_volume_cb_idx = tm_tr_volume_cb_idx;
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307419 ioc->tm_sas_control_cb_idx = tm_sas_control_cb_idx;
Eric Moore635374e2009-03-09 01:21:12 -06007420 ioc->logging_level = logging_level;
7421 /* misc semaphores and spin locks */
Kashyap, Desaid2742132010-06-17 13:28:55 +05307422 mutex_init(&ioc->reset_in_progress_mutex);
Eric Moore635374e2009-03-09 01:21:12 -06007423 spin_lock_init(&ioc->ioc_reset_in_progress_lock);
7424 spin_lock_init(&ioc->scsi_lookup_lock);
7425 spin_lock_init(&ioc->sas_device_lock);
7426 spin_lock_init(&ioc->sas_node_lock);
7427 spin_lock_init(&ioc->fw_event_lock);
7428 spin_lock_init(&ioc->raid_device_lock);
7429
7430 INIT_LIST_HEAD(&ioc->sas_device_list);
7431 INIT_LIST_HEAD(&ioc->sas_device_init_list);
7432 INIT_LIST_HEAD(&ioc->sas_expander_list);
7433 INIT_LIST_HEAD(&ioc->fw_event_list);
7434 INIT_LIST_HEAD(&ioc->raid_device_list);
7435 INIT_LIST_HEAD(&ioc->sas_hba.sas_port_list);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307436 INIT_LIST_HEAD(&ioc->delayed_tr_list);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307437 INIT_LIST_HEAD(&ioc->delayed_tr_volume_list);
Eric Moore635374e2009-03-09 01:21:12 -06007438
7439 /* init shost parameters */
Eric Moored334aa72010-04-22 10:47:40 -06007440 shost->max_cmd_len = 32;
Eric Moore635374e2009-03-09 01:21:12 -06007441 shost->max_lun = max_lun;
7442 shost->transportt = mpt2sas_transport_template;
7443 shost->unique_id = ioc->id;
7444
7445 if ((scsi_add_host(shost, &pdev->dev))) {
7446 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7447 ioc->name, __FILE__, __LINE__, __func__);
7448 list_del(&ioc->list);
7449 goto out_add_shost_fail;
7450 }
7451
Eric Moore3c621b32009-05-18 12:59:41 -06007452 scsi_host_set_prot(shost, SHOST_DIF_TYPE1_PROTECTION
Eric Moored334aa72010-04-22 10:47:40 -06007453 | SHOST_DIF_TYPE2_PROTECTION | SHOST_DIF_TYPE3_PROTECTION);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307454 scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
Eric Moore3c621b32009-05-18 12:59:41 -06007455
Eric Moore635374e2009-03-09 01:21:12 -06007456 /* event thread */
7457 snprintf(ioc->firmware_event_name, sizeof(ioc->firmware_event_name),
7458 "fw_event%d", ioc->id);
7459 ioc->firmware_event_thread = create_singlethread_workqueue(
7460 ioc->firmware_event_name);
7461 if (!ioc->firmware_event_thread) {
7462 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7463 ioc->name, __FILE__, __LINE__, __func__);
7464 goto out_thread_fail;
7465 }
7466
7467 ioc->wait_for_port_enable_to_complete = 1;
7468 if ((mpt2sas_base_attach(ioc))) {
7469 printk(MPT2SAS_ERR_FMT "failure at %s:%d/%s()!\n",
7470 ioc->name, __FILE__, __LINE__, __func__);
7471 goto out_attach_fail;
7472 }
7473
7474 ioc->wait_for_port_enable_to_complete = 0;
Kashyap, Desai0bdccdb2011-04-07 12:32:49 +05307475 if (ioc->is_warpdrive) {
7476 if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_EXPOSE_ALL_DISKS)
7477 ioc->hide_drives = 0;
7478 else if (ioc->mfg_pg10_hide_flag == MFG_PAGE10_HIDE_ALL_DISKS)
7479 ioc->hide_drives = 1;
7480 else {
7481 if (_scsih_get_num_volumes(ioc))
7482 ioc->hide_drives = 1;
7483 else
7484 ioc->hide_drives = 0;
7485 }
7486 } else
7487 ioc->hide_drives = 0;
7488
Eric Moore635374e2009-03-09 01:21:12 -06007489 _scsih_probe_devices(ioc);
7490 return 0;
7491
7492 out_attach_fail:
7493 destroy_workqueue(ioc->firmware_event_thread);
7494 out_thread_fail:
7495 list_del(&ioc->list);
7496 scsi_remove_host(shost);
7497 out_add_shost_fail:
7498 return -ENODEV;
7499}
7500
7501#ifdef CONFIG_PM
7502/**
Eric Moored5d135b2009-05-18 13:02:08 -06007503 * _scsih_suspend - power management suspend main entry point
Eric Moore635374e2009-03-09 01:21:12 -06007504 * @pdev: PCI device struct
7505 * @state: PM state change to (usually PCI_D3)
7506 *
7507 * Returns 0 success, anything else error.
7508 */
7509static int
Eric Moored5d135b2009-05-18 13:02:08 -06007510_scsih_suspend(struct pci_dev *pdev, pm_message_t state)
Eric Moore635374e2009-03-09 01:21:12 -06007511{
7512 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7513 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7514 u32 device_state;
7515
Kashyap, Desaie4750c92009-08-07 19:37:59 +05307516 mpt2sas_base_stop_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007517 scsi_block_requests(shost);
7518 device_state = pci_choose_state(pdev, state);
7519 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, entering "
7520 "operating state [D%d]\n", ioc->name, pdev,
7521 pci_name(pdev), device_state);
7522
7523 mpt2sas_base_free_resources(ioc);
7524 pci_save_state(pdev);
7525 pci_disable_device(pdev);
7526 pci_set_power_state(pdev, device_state);
7527 return 0;
7528}
7529
7530/**
Eric Moored5d135b2009-05-18 13:02:08 -06007531 * _scsih_resume - power management resume main entry point
Eric Moore635374e2009-03-09 01:21:12 -06007532 * @pdev: PCI device struct
7533 *
7534 * Returns 0 success, anything else error.
7535 */
7536static int
Eric Moored5d135b2009-05-18 13:02:08 -06007537_scsih_resume(struct pci_dev *pdev)
Eric Moore635374e2009-03-09 01:21:12 -06007538{
7539 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7540 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7541 u32 device_state = pdev->current_state;
7542 int r;
7543
7544 printk(MPT2SAS_INFO_FMT "pdev=0x%p, slot=%s, previous "
7545 "operating state [D%d]\n", ioc->name, pdev,
7546 pci_name(pdev), device_state);
7547
7548 pci_set_power_state(pdev, PCI_D0);
7549 pci_enable_wake(pdev, PCI_D0, 0);
7550 pci_restore_state(pdev);
7551 ioc->pdev = pdev;
7552 r = mpt2sas_base_map_resources(ioc);
7553 if (r)
7554 return r;
7555
7556 mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP, SOFT_RESET);
7557 scsi_unblock_requests(shost);
Kashyap, Desaie4750c92009-08-07 19:37:59 +05307558 mpt2sas_base_start_watchdog(ioc);
Eric Moore635374e2009-03-09 01:21:12 -06007559 return 0;
7560}
7561#endif /* CONFIG_PM */
7562
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307563/**
7564 * _scsih_pci_error_detected - Called when a PCI error is detected.
7565 * @pdev: PCI device struct
7566 * @state: PCI channel state
7567 *
7568 * Description: Called when a PCI error is detected.
7569 *
7570 * Return value:
7571 * PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT
7572 */
7573static pci_ers_result_t
7574_scsih_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
7575{
7576 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7577 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7578
7579 printk(MPT2SAS_INFO_FMT "PCI error: detected callback, state(%d)!!\n",
7580 ioc->name, state);
7581
7582 switch (state) {
7583 case pci_channel_io_normal:
7584 return PCI_ERS_RESULT_CAN_RECOVER;
7585 case pci_channel_io_frozen:
Eric Moore3cb54692010-07-08 14:44:34 -06007586 /* Fatal error, prepare for slot reset */
7587 ioc->pci_error_recovery = 1;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307588 scsi_block_requests(ioc->shost);
7589 mpt2sas_base_stop_watchdog(ioc);
7590 mpt2sas_base_free_resources(ioc);
7591 return PCI_ERS_RESULT_NEED_RESET;
7592 case pci_channel_io_perm_failure:
Eric Moore3cb54692010-07-08 14:44:34 -06007593 /* Permanent error, prepare for device removal */
7594 ioc->pci_error_recovery = 1;
7595 mpt2sas_base_stop_watchdog(ioc);
7596 _scsih_flush_running_cmds(ioc);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307597 return PCI_ERS_RESULT_DISCONNECT;
7598 }
7599 return PCI_ERS_RESULT_NEED_RESET;
7600}
7601
7602/**
7603 * _scsih_pci_slot_reset - Called when PCI slot has been reset.
7604 * @pdev: PCI device struct
7605 *
7606 * Description: This routine is called by the pci error recovery
7607 * code after the PCI slot has been reset, just before we
7608 * should resume normal operations.
7609 */
7610static pci_ers_result_t
7611_scsih_pci_slot_reset(struct pci_dev *pdev)
7612{
7613 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7614 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7615 int rc;
7616
7617 printk(MPT2SAS_INFO_FMT "PCI error: slot reset callback!!\n",
7618 ioc->name);
7619
Eric Moore3cb54692010-07-08 14:44:34 -06007620 ioc->pci_error_recovery = 0;
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307621 ioc->pdev = pdev;
Eric Moore3cb54692010-07-08 14:44:34 -06007622 pci_restore_state(pdev);
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307623 rc = mpt2sas_base_map_resources(ioc);
7624 if (rc)
7625 return PCI_ERS_RESULT_DISCONNECT;
7626
7627
7628 rc = mpt2sas_base_hard_reset_handler(ioc, CAN_SLEEP,
7629 FORCE_BIG_HAMMER);
7630
7631 printk(MPT2SAS_WARN_FMT "hard reset: %s\n", ioc->name,
7632 (rc == 0) ? "success" : "failed");
7633
7634 if (!rc)
7635 return PCI_ERS_RESULT_RECOVERED;
7636 else
7637 return PCI_ERS_RESULT_DISCONNECT;
7638}
7639
7640/**
7641 * _scsih_pci_resume() - resume normal ops after PCI reset
7642 * @pdev: pointer to PCI device
7643 *
7644 * Called when the error recovery driver tells us that its
7645 * OK to resume normal operation. Use completion to allow
7646 * halted scsi ops to resume.
7647 */
7648static void
7649_scsih_pci_resume(struct pci_dev *pdev)
7650{
7651 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7652 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7653
7654 printk(MPT2SAS_INFO_FMT "PCI error: resume callback!!\n", ioc->name);
7655
7656 pci_cleanup_aer_uncorrect_error_status(pdev);
7657 mpt2sas_base_start_watchdog(ioc);
7658 scsi_unblock_requests(ioc->shost);
7659}
7660
7661/**
7662 * _scsih_pci_mmio_enabled - Enable MMIO and dump debug registers
7663 * @pdev: pointer to PCI device
7664 */
7665static pci_ers_result_t
7666_scsih_pci_mmio_enabled(struct pci_dev *pdev)
7667{
7668 struct Scsi_Host *shost = pci_get_drvdata(pdev);
7669 struct MPT2SAS_ADAPTER *ioc = shost_priv(shost);
7670
7671 printk(MPT2SAS_INFO_FMT "PCI error: mmio enabled callback!!\n",
7672 ioc->name);
7673
7674 /* TODO - dump whatever for debugging purposes */
7675
7676 /* Request a slot reset. */
7677 return PCI_ERS_RESULT_NEED_RESET;
7678}
7679
7680static struct pci_error_handlers _scsih_err_handler = {
7681 .error_detected = _scsih_pci_error_detected,
7682 .mmio_enabled = _scsih_pci_mmio_enabled,
7683 .slot_reset = _scsih_pci_slot_reset,
7684 .resume = _scsih_pci_resume,
7685};
Eric Moore635374e2009-03-09 01:21:12 -06007686
7687static struct pci_driver scsih_driver = {
7688 .name = MPT2SAS_DRIVER_NAME,
7689 .id_table = scsih_pci_table,
Eric Moored5d135b2009-05-18 13:02:08 -06007690 .probe = _scsih_probe,
7691 .remove = __devexit_p(_scsih_remove),
Kashyap, Desai744090d2009-10-05 15:56:56 +05307692 .shutdown = _scsih_shutdown,
Kashyap, Desaief7c80c2010-04-05 14:20:07 +05307693 .err_handler = &_scsih_err_handler,
Eric Moore635374e2009-03-09 01:21:12 -06007694#ifdef CONFIG_PM
Eric Moored5d135b2009-05-18 13:02:08 -06007695 .suspend = _scsih_suspend,
7696 .resume = _scsih_resume,
Eric Moore635374e2009-03-09 01:21:12 -06007697#endif
7698};
7699
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307700/* raid transport support */
7701static struct raid_function_template mpt2sas_raid_functions = {
7702 .cookie = &scsih_driver_template,
7703 .is_raid = _scsih_is_raid,
7704 .get_resync = _scsih_get_resync,
7705 .get_state = _scsih_get_state,
7706};
Eric Moore635374e2009-03-09 01:21:12 -06007707
7708/**
Eric Moored5d135b2009-05-18 13:02:08 -06007709 * _scsih_init - main entry point for this driver.
Eric Moore635374e2009-03-09 01:21:12 -06007710 *
7711 * Returns 0 success, anything else error.
7712 */
7713static int __init
Eric Moored5d135b2009-05-18 13:02:08 -06007714_scsih_init(void)
Eric Moore635374e2009-03-09 01:21:12 -06007715{
7716 int error;
7717
7718 mpt_ids = 0;
7719 printk(KERN_INFO "%s version %s loaded\n", MPT2SAS_DRIVER_NAME,
7720 MPT2SAS_DRIVER_VERSION);
7721
7722 mpt2sas_transport_template =
7723 sas_attach_transport(&mpt2sas_transport_functions);
7724 if (!mpt2sas_transport_template)
7725 return -ENODEV;
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307726 /* raid transport support */
7727 mpt2sas_raid_template = raid_class_attach(&mpt2sas_raid_functions);
7728 if (!mpt2sas_raid_template) {
7729 sas_release_transport(mpt2sas_transport_template);
7730 return -ENODEV;
7731 }
Eric Moore635374e2009-03-09 01:21:12 -06007732
7733 mpt2sas_base_initialize_callback_handler();
7734
7735 /* queuecommand callback hander */
Eric Moored5d135b2009-05-18 13:02:08 -06007736 scsi_io_cb_idx = mpt2sas_base_register_callback_handler(_scsih_io_done);
Eric Moore635374e2009-03-09 01:21:12 -06007737
Uwe Kleine-König65155b32010-06-11 12:17:01 +02007738 /* task management callback handler */
Eric Moored5d135b2009-05-18 13:02:08 -06007739 tm_cb_idx = mpt2sas_base_register_callback_handler(_scsih_tm_done);
Eric Moore635374e2009-03-09 01:21:12 -06007740
7741 /* base internal commands callback handler */
7742 base_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_base_done);
7743
7744 /* transport internal commands callback handler */
7745 transport_cb_idx = mpt2sas_base_register_callback_handler(
7746 mpt2sas_transport_done);
7747
Kashyap, Desai744090d2009-10-05 15:56:56 +05307748 /* scsih internal commands callback handler */
7749 scsih_cb_idx = mpt2sas_base_register_callback_handler(_scsih_done);
7750
Eric Moore635374e2009-03-09 01:21:12 -06007751 /* configuration page API internal commands callback handler */
7752 config_cb_idx = mpt2sas_base_register_callback_handler(
7753 mpt2sas_config_done);
7754
7755 /* ctl module callback handler */
7756 ctl_cb_idx = mpt2sas_base_register_callback_handler(mpt2sas_ctl_done);
7757
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307758 tm_tr_cb_idx = mpt2sas_base_register_callback_handler(
7759 _scsih_tm_tr_complete);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307760
7761 tm_tr_volume_cb_idx = mpt2sas_base_register_callback_handler(
7762 _scsih_tm_volume_tr_complete);
7763
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307764 tm_sas_control_cb_idx = mpt2sas_base_register_callback_handler(
7765 _scsih_sas_control_complete);
7766
Eric Moore635374e2009-03-09 01:21:12 -06007767 mpt2sas_ctl_init();
7768
7769 error = pci_register_driver(&scsih_driver);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307770 if (error) {
7771 /* raid transport support */
7772 raid_class_release(mpt2sas_raid_template);
Eric Moore635374e2009-03-09 01:21:12 -06007773 sas_release_transport(mpt2sas_transport_template);
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307774 }
Eric Moore635374e2009-03-09 01:21:12 -06007775
7776 return error;
7777}
7778
7779/**
Eric Moored5d135b2009-05-18 13:02:08 -06007780 * _scsih_exit - exit point for this driver (when it is a module).
Eric Moore635374e2009-03-09 01:21:12 -06007781 *
7782 * Returns 0 success, anything else error.
7783 */
7784static void __exit
Eric Moored5d135b2009-05-18 13:02:08 -06007785_scsih_exit(void)
Eric Moore635374e2009-03-09 01:21:12 -06007786{
7787 printk(KERN_INFO "mpt2sas version %s unloading\n",
7788 MPT2SAS_DRIVER_VERSION);
7789
7790 pci_unregister_driver(&scsih_driver);
7791
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307792 mpt2sas_ctl_exit();
7793
Eric Moore635374e2009-03-09 01:21:12 -06007794 mpt2sas_base_release_callback_handler(scsi_io_cb_idx);
7795 mpt2sas_base_release_callback_handler(tm_cb_idx);
7796 mpt2sas_base_release_callback_handler(base_cb_idx);
7797 mpt2sas_base_release_callback_handler(transport_cb_idx);
Kashyap, Desai744090d2009-10-05 15:56:56 +05307798 mpt2sas_base_release_callback_handler(scsih_cb_idx);
Eric Moore635374e2009-03-09 01:21:12 -06007799 mpt2sas_base_release_callback_handler(config_cb_idx);
7800 mpt2sas_base_release_callback_handler(ctl_cb_idx);
7801
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307802 mpt2sas_base_release_callback_handler(tm_tr_cb_idx);
Kashyap, Desaif3eedd62010-06-17 13:46:13 +05307803 mpt2sas_base_release_callback_handler(tm_tr_volume_cb_idx);
Kashyap, Desai77e63ed2009-09-14 11:04:23 +05307804 mpt2sas_base_release_callback_handler(tm_sas_control_cb_idx);
7805
Kashyap, Desaif7c95ef2009-12-16 18:54:42 +05307806 /* raid transport support */
7807 raid_class_release(mpt2sas_raid_template);
7808 sas_release_transport(mpt2sas_transport_template);
7809
Eric Moore635374e2009-03-09 01:21:12 -06007810}
7811
Eric Moored5d135b2009-05-18 13:02:08 -06007812module_init(_scsih_init);
7813module_exit(_scsih_exit);